Bug Summary

File:s/lib/sqlite/sqlite3.c
Warning:line 68653, column 11
Value stored to 'rc' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name sqlite3.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/var/lib/jenkins/workspace/nss-scan-build/nss/lib/sqlite -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/var/lib/jenkins/workspace/nss-scan-build/nss/lib/sqlite -resource-dir /usr/lib/llvm-18/lib/clang/18 -D HAVE_STRERROR -D LINUX -D linux -D XP_UNIX -D XP_UNIX -D SQLITE_THREADSAFE=1 -D DEBUG -U NDEBUG -D _DEFAULT_SOURCE -D _BSD_SOURCE -D _POSIX_SOURCE -D SDB_MEASURE_USE_TEMP_DIR -D _REENTRANT -D DEBUG -U NDEBUG -D _DEFAULT_SOURCE -D _BSD_SOURCE -D _POSIX_SOURCE -D SDB_MEASURE_USE_TEMP_DIR -D _REENTRANT -D NSS_DISABLE_SSE3 -D NSS_NO_INIT_SUPPORT -D USE_UTIL_DIRECTLY -D NO_NSPR_10_SUPPORT -D SSL_DISABLE_DEPRECATED_CIPHER_SUITE_NAMES -I ../../../dist/Linux4.19_x86_64_gcc_glibc_PTH_64_DBG.OBJ/include -I ../../../dist/public/nss -I ../../../dist/private/nss -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c99 -ferror-limit 19 -fgnuc-version=4.2.1 -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2024-05-18-082241-28900-1 -x c sqlite3.c
1/******************************************************************************
2** This file is an amalgamation of many separate C source files from SQLite
3** version 3.29.0. By combining all the individual C code files into this
4** single large file, the entire code can be compiled as a single translation
5** unit. This allows many compilers to do optimizations that would not be
6** possible if the files were compiled separately. Performance improvements
7** of 5% or more are commonly seen when SQLite is compiled as a single
8** translation unit.
9**
10** This file is all you need to compile SQLite. To use SQLite in other
11** programs, you need this file and the "sqlite3.h" header file that defines
12** the programming interface to the SQLite library. (If you do not have
13** the "sqlite3.h" header file at hand, you will find a copy embedded within
14** the text of this file. Search for "Begin file sqlite3.h" to find the start
15** of the embedded sqlite3.h header file.) Additional code files may be needed
16** if you want a wrapper to interface SQLite with your choice of programming
17** language. The code for the "sqlite3" command-line shell is also in a
18** separate file. This file contains only code for the core SQLite library.
19*/
20#define SQLITE_CORE1 1
21#define SQLITE_AMALGAMATION1 1
22#ifndef SQLITE_PRIVATEstatic
23# define SQLITE_PRIVATEstatic static
24#endif
25/************** Begin file ctime.c *******************************************/
26/*
27** 2010 February 23
28**
29** The author disclaims copyright to this source code. In place of
30** a legal notice, here is a blessing:
31**
32** May you do good and not evil.
33** May you find forgiveness for yourself and forgive others.
34** May you share freely, never taking more than you give.
35**
36*************************************************************************
37**
38** This file implements routines used to report what compile-time options
39** SQLite was built with.
40*/
41
42#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
43
44/*
45** Include the configuration header output by 'configure' if we're using the
46** autoconf-based build
47*/
48#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
49#include "config.h"
50#define SQLITECONFIG_H 1
51#endif
52
53/* These macros are provided to "stringify" the value of the define
54** for those options in which the value is meaningful. */
55#define CTIMEOPT_VAL_(opt)"opt" #opt
56#define CTIMEOPT_VAL(opt)"opt" CTIMEOPT_VAL_(opt)"opt"
57
58/* Like CTIMEOPT_VAL, but especially for SQLITE_DEFAULT_LOOKASIDE. This
59** option requires a separate macro because legal values contain a single
60** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
61#define CTIMEOPT_VAL2_(opt1,opt2)"opt1" "," "opt2" #opt1 "," #opt2
62#define CTIMEOPT_VAL2(opt)CTIMEOPT_VAL2_ CTIMEOPT_VAL2_(opt)
63
64/*
65** An array of names of all compile-time options. This array should
66** be sorted A-Z.
67**
68** This array looks large, but in a typical installation actually uses
69** only a handful of compile-time options, so most times this array is usually
70** rather short and uses little memory space.
71*/
72static const char * const sqlite3azCompileOpt[] = {
73
74/*
75** BEGIN CODE GENERATED BY tool/mkctime.tcl
76*/
77#if SQLITE_32BIT_ROWID
78 "32BIT_ROWID",
79#endif
80#if SQLITE_4_BYTE_ALIGNED_MALLOC
81 "4_BYTE_ALIGNED_MALLOC",
82#endif
83#if SQLITE_64BIT_STATS
84 "64BIT_STATS",
85#endif
86#if SQLITE_ALLOW_COVERING_INDEX_SCAN1
87 "ALLOW_COVERING_INDEX_SCAN",
88#endif
89#if SQLITE_ALLOW_URI_AUTHORITY
90 "ALLOW_URI_AUTHORITY",
91#endif
92#ifdef SQLITE_BITMASK_TYPE
93 "BITMASK_TYPE=" CTIMEOPT_VAL(SQLITE_BITMASK_TYPE)"SQLITE_BITMASK_TYPE",
94#endif
95#if SQLITE_BUG_COMPATIBLE_20160819
96 "BUG_COMPATIBLE_20160819",
97#endif
98#if SQLITE_CASE_SENSITIVE_LIKE
99 "CASE_SENSITIVE_LIKE",
100#endif
101#if SQLITE_CHECK_PAGES
102 "CHECK_PAGES",
103#endif
104#if defined(__clang__1) && defined(__clang_major__18)
105 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__)"18" "."
106 CTIMEOPT_VAL(__clang_minor__)"0" "."
107 CTIMEOPT_VAL(__clang_patchlevel__)"0",
108#elif defined(_MSC_VER)
109 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER)"_MSC_VER",
110#elif defined(__GNUC__4) && defined(__VERSION__"Debian Clang 18.0.0 (++20240123100859+dc410f94f602-1~exp1~20240123221021.1839)")
111 "COMPILER=gcc-" __VERSION__"Debian Clang 18.0.0 (++20240123100859+dc410f94f602-1~exp1~20240123221021.1839)",
112#endif
113#if SQLITE_COVERAGE_TEST
114 "COVERAGE_TEST",
115#endif
116#if SQLITE_DEBUG
117 "DEBUG",
118#endif
119#if SQLITE_DEFAULT_AUTOMATIC_INDEX
120 "DEFAULT_AUTOMATIC_INDEX",
121#endif
122#if SQLITE_DEFAULT_AUTOVACUUM0
123 "DEFAULT_AUTOVACUUM",
124#endif
125#ifdef SQLITE_DEFAULT_CACHE_SIZE-2000
126 "DEFAULT_CACHE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_CACHE_SIZE)"-2000",
127#endif
128#if SQLITE_DEFAULT_CKPTFULLFSYNC
129 "DEFAULT_CKPTFULLFSYNC",
130#endif
131#ifdef SQLITE_DEFAULT_FILE_FORMAT4
132 "DEFAULT_FILE_FORMAT=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_FORMAT)"4",
133#endif
134#ifdef SQLITE_DEFAULT_FILE_PERMISSIONS0644
135 "DEFAULT_FILE_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_FILE_PERMISSIONS)"0644",
136#endif
137#if SQLITE_DEFAULT_FOREIGN_KEYS
138 "DEFAULT_FOREIGN_KEYS",
139#endif
140#ifdef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT-1
141 "DEFAULT_JOURNAL_SIZE_LIMIT=" CTIMEOPT_VAL(SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT)"-1",
142#endif
143#ifdef SQLITE_DEFAULT_LOCKING_MODE
144 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE)"SQLITE_DEFAULT_LOCKING_MODE",
145#endif
146#ifdef SQLITE_DEFAULT_LOOKASIDE1200,100
147 "DEFAULT_LOOKASIDE=" CTIMEOPT_VAL2(SQLITE_DEFAULT_LOOKASIDE)"1200" "," "100",
148#endif
149#if SQLITE_DEFAULT_MEMSTATUS1
150 "DEFAULT_MEMSTATUS",
151#endif
152#ifdef SQLITE_DEFAULT_MMAP_SIZE0
153 "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE)"0",
154#endif
155#ifdef SQLITE_DEFAULT_PAGE_SIZE4096
156 "DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_PAGE_SIZE)"4096",
157#endif
158#ifdef SQLITE_DEFAULT_PCACHE_INITSZ20
159 "DEFAULT_PCACHE_INITSZ=" CTIMEOPT_VAL(SQLITE_DEFAULT_PCACHE_INITSZ)"20",
160#endif
161#ifdef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755
162 "DEFAULT_PROXYDIR_PERMISSIONS=" CTIMEOPT_VAL(SQLITE_DEFAULT_PROXYDIR_PERMISSIONS)"0755",
163#endif
164#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
165 "DEFAULT_RECURSIVE_TRIGGERS",
166#endif
167#ifdef SQLITE_DEFAULT_ROWEST
168 "DEFAULT_ROWEST=" CTIMEOPT_VAL(SQLITE_DEFAULT_ROWEST)"SQLITE_DEFAULT_ROWEST",
169#endif
170#ifdef SQLITE_DEFAULT_SECTOR_SIZE4096
171 "DEFAULT_SECTOR_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_SECTOR_SIZE)"4096",
172#endif
173#ifdef SQLITE_DEFAULT_SYNCHRONOUS2
174 "DEFAULT_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_SYNCHRONOUS)"2",
175#endif
176#ifdef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000
177 "DEFAULT_WAL_AUTOCHECKPOINT=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_AUTOCHECKPOINT)"1000",
178#endif
179#ifdef SQLITE_DEFAULT_WAL_SYNCHRONOUS2
180 "DEFAULT_WAL_SYNCHRONOUS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WAL_SYNCHRONOUS)"2",
181#endif
182#ifdef SQLITE_DEFAULT_WORKER_THREADS0
183 "DEFAULT_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_DEFAULT_WORKER_THREADS)"0",
184#endif
185#if SQLITE_DIRECT_OVERFLOW_READ
186 "DIRECT_OVERFLOW_READ",
187#endif
188#if SQLITE_DISABLE_DIRSYNC
189 "DISABLE_DIRSYNC",
190#endif
191#if SQLITE_DISABLE_FTS3_UNICODE
192 "DISABLE_FTS3_UNICODE",
193#endif
194#if SQLITE_DISABLE_FTS4_DEFERRED
195 "DISABLE_FTS4_DEFERRED",
196#endif
197#if SQLITE_DISABLE_INTRINSIC
198 "DISABLE_INTRINSIC",
199#endif
200#if SQLITE_DISABLE_LFS
201 "DISABLE_LFS",
202#endif
203#if SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
204 "DISABLE_PAGECACHE_OVERFLOW_STATS",
205#endif
206#if SQLITE_DISABLE_SKIPAHEAD_DISTINCT
207 "DISABLE_SKIPAHEAD_DISTINCT",
208#endif
209#ifdef SQLITE_ENABLE_8_3_NAMES
210 "ENABLE_8_3_NAMES=" CTIMEOPT_VAL(SQLITE_ENABLE_8_3_NAMES)"SQLITE_ENABLE_8_3_NAMES",
211#endif
212#if SQLITE_ENABLE_API_ARMOR
213 "ENABLE_API_ARMOR",
214#endif
215#if SQLITE_ENABLE_ATOMIC_WRITE
216 "ENABLE_ATOMIC_WRITE",
217#endif
218#if SQLITE_ENABLE_BATCH_ATOMIC_WRITE
219 "ENABLE_BATCH_ATOMIC_WRITE",
220#endif
221#if SQLITE_ENABLE_CEROD
222 "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD)"SQLITE_ENABLE_CEROD",
223#endif
224#if SQLITE_ENABLE_COLUMN_METADATA
225 "ENABLE_COLUMN_METADATA",
226#endif
227#if SQLITE_ENABLE_COLUMN_USED_MASK
228 "ENABLE_COLUMN_USED_MASK",
229#endif
230#if SQLITE_ENABLE_COSTMULT
231 "ENABLE_COSTMULT",
232#endif
233#if SQLITE_ENABLE_CURSOR_HINTS
234 "ENABLE_CURSOR_HINTS",
235#endif
236#if SQLITE_ENABLE_DBSTAT_VTAB
237 "ENABLE_DBSTAT_VTAB",
238#endif
239#if SQLITE_ENABLE_EXPENSIVE_ASSERT
240 "ENABLE_EXPENSIVE_ASSERT",
241#endif
242#if SQLITE_ENABLE_FTS1
243 "ENABLE_FTS1",
244#endif
245#if SQLITE_ENABLE_FTS2
246 "ENABLE_FTS2",
247#endif
248#if SQLITE_ENABLE_FTS3
249 "ENABLE_FTS3",
250#endif
251#if SQLITE_ENABLE_FTS3_PARENTHESIS
252 "ENABLE_FTS3_PARENTHESIS",
253#endif
254#if SQLITE_ENABLE_FTS3_TOKENIZER
255 "ENABLE_FTS3_TOKENIZER",
256#endif
257#if SQLITE_ENABLE_FTS4
258 "ENABLE_FTS4",
259#endif
260#if SQLITE_ENABLE_FTS5
261 "ENABLE_FTS5",
262#endif
263#if SQLITE_ENABLE_GEOPOLY
264 "ENABLE_GEOPOLY",
265#endif
266#if SQLITE_ENABLE_HIDDEN_COLUMNS
267 "ENABLE_HIDDEN_COLUMNS",
268#endif
269#if SQLITE_ENABLE_ICU
270 "ENABLE_ICU",
271#endif
272#if SQLITE_ENABLE_IOTRACE
273 "ENABLE_IOTRACE",
274#endif
275#if SQLITE_ENABLE_JSON1
276 "ENABLE_JSON1",
277#endif
278#if SQLITE_ENABLE_LOAD_EXTENSION
279 "ENABLE_LOAD_EXTENSION",
280#endif
281#ifdef SQLITE_ENABLE_LOCKING_STYLE0
282 "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE)"0",
283#endif
284#if SQLITE_ENABLE_MEMORY_MANAGEMENT
285 "ENABLE_MEMORY_MANAGEMENT",
286#endif
287#if SQLITE_ENABLE_MEMSYS3
288 "ENABLE_MEMSYS3",
289#endif
290#if SQLITE_ENABLE_MEMSYS5
291 "ENABLE_MEMSYS5",
292#endif
293#if SQLITE_ENABLE_MULTIPLEX
294 "ENABLE_MULTIPLEX",
295#endif
296#if SQLITE_ENABLE_NORMALIZE
297 "ENABLE_NORMALIZE",
298#endif
299#if SQLITE_ENABLE_NULL_TRIM
300 "ENABLE_NULL_TRIM",
301#endif
302#if SQLITE_ENABLE_OVERSIZE_CELL_CHECK
303 "ENABLE_OVERSIZE_CELL_CHECK",
304#endif
305#if SQLITE_ENABLE_PREUPDATE_HOOK
306 "ENABLE_PREUPDATE_HOOK",
307#endif
308#if SQLITE_ENABLE_QPSG
309 "ENABLE_QPSG",
310#endif
311#if SQLITE_ENABLE_RBU
312 "ENABLE_RBU",
313#endif
314#if SQLITE_ENABLE_RTREE
315 "ENABLE_RTREE",
316#endif
317#if SQLITE_ENABLE_SELECTTRACE
318 "ENABLE_SELECTTRACE",
319#endif
320#if SQLITE_ENABLE_SESSION
321 "ENABLE_SESSION",
322#endif
323#if SQLITE_ENABLE_SNAPSHOT
324 "ENABLE_SNAPSHOT",
325#endif
326#if SQLITE_ENABLE_SORTER_REFERENCES
327 "ENABLE_SORTER_REFERENCES",
328#endif
329#if SQLITE_ENABLE_SQLLOG
330 "ENABLE_SQLLOG",
331#endif
332#if defined(SQLITE_ENABLE_STAT4)
333 "ENABLE_STAT4",
334#elif defined(SQLITE_ENABLE_STAT3)
335 "ENABLE_STAT3",
336#endif
337#if SQLITE_ENABLE_STMTVTAB
338 "ENABLE_STMTVTAB",
339#endif
340#if SQLITE_ENABLE_STMT_SCANSTATUS
341 "ENABLE_STMT_SCANSTATUS",
342#endif
343#if SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
344 "ENABLE_UNKNOWN_SQL_FUNCTION",
345#endif
346#if SQLITE_ENABLE_UNLOCK_NOTIFY
347 "ENABLE_UNLOCK_NOTIFY",
348#endif
349#if SQLITE_ENABLE_UPDATE_DELETE_LIMIT
350 "ENABLE_UPDATE_DELETE_LIMIT",
351#endif
352#if SQLITE_ENABLE_URI_00_ERROR
353 "ENABLE_URI_00_ERROR",
354#endif
355#if SQLITE_ENABLE_VFSTRACE
356 "ENABLE_VFSTRACE",
357#endif
358#if SQLITE_ENABLE_WHERETRACE
359 "ENABLE_WHERETRACE",
360#endif
361#if SQLITE_ENABLE_ZIPVFS
362 "ENABLE_ZIPVFS",
363#endif
364#if SQLITE_EXPLAIN_ESTIMATED_ROWS
365 "EXPLAIN_ESTIMATED_ROWS",
366#endif
367#if SQLITE_EXTRA_IFNULLROW
368 "EXTRA_IFNULLROW",
369#endif
370#ifdef SQLITE_EXTRA_INIT
371 "EXTRA_INIT=" CTIMEOPT_VAL(SQLITE_EXTRA_INIT)"SQLITE_EXTRA_INIT",
372#endif
373#ifdef SQLITE_EXTRA_SHUTDOWN
374 "EXTRA_SHUTDOWN=" CTIMEOPT_VAL(SQLITE_EXTRA_SHUTDOWN)"SQLITE_EXTRA_SHUTDOWN",
375#endif
376#ifdef SQLITE_FTS3_MAX_EXPR_DEPTH
377 "FTS3_MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_FTS3_MAX_EXPR_DEPTH)"SQLITE_FTS3_MAX_EXPR_DEPTH",
378#endif
379#if SQLITE_FTS5_ENABLE_TEST_MI
380 "FTS5_ENABLE_TEST_MI",
381#endif
382#if SQLITE_FTS5_NO_WITHOUT_ROWID
383 "FTS5_NO_WITHOUT_ROWID",
384#endif
385#if SQLITE_HAS_CODEC
386 "HAS_CODEC",
387#endif
388#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
389 "HAVE_ISNAN",
390#endif
391#if SQLITE_HOMEGROWN_RECURSIVE_MUTEX
392 "HOMEGROWN_RECURSIVE_MUTEX",
393#endif
394#if SQLITE_IGNORE_AFP_LOCK_ERRORS
395 "IGNORE_AFP_LOCK_ERRORS",
396#endif
397#if SQLITE_IGNORE_FLOCK_LOCK_ERRORS
398 "IGNORE_FLOCK_LOCK_ERRORS",
399#endif
400#if SQLITE_INLINE_MEMCPY
401 "INLINE_MEMCPY",
402#endif
403#if SQLITE_INT64_TYPE
404 "INT64_TYPE",
405#endif
406#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX100
407 "INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX)"100",
408#endif
409#if SQLITE_LIKE_DOESNT_MATCH_BLOBS
410 "LIKE_DOESNT_MATCH_BLOBS",
411#endif
412#if SQLITE_LOCK_TRACE
413 "LOCK_TRACE",
414#endif
415#if SQLITE_LOG_CACHE_SPILL
416 "LOG_CACHE_SPILL",
417#endif
418#ifdef SQLITE_MALLOC_SOFT_LIMIT1024
419 "MALLOC_SOFT_LIMIT=" CTIMEOPT_VAL(SQLITE_MALLOC_SOFT_LIMIT)"1024",
420#endif
421#ifdef SQLITE_MAX_ATTACHED10
422 "MAX_ATTACHED=" CTIMEOPT_VAL(SQLITE_MAX_ATTACHED)"10",
423#endif
424#ifdef SQLITE_MAX_COLUMN2000
425 "MAX_COLUMN=" CTIMEOPT_VAL(SQLITE_MAX_COLUMN)"2000",
426#endif
427#ifdef SQLITE_MAX_COMPOUND_SELECT500
428 "MAX_COMPOUND_SELECT=" CTIMEOPT_VAL(SQLITE_MAX_COMPOUND_SELECT)"500",
429#endif
430#ifdef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
431 "MAX_DEFAULT_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_DEFAULT_PAGE_SIZE)"8192",
432#endif
433#ifdef SQLITE_MAX_EXPR_DEPTH1000
434 "MAX_EXPR_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_EXPR_DEPTH)"1000",
435#endif
436#ifdef SQLITE_MAX_FUNCTION_ARG127
437 "MAX_FUNCTION_ARG=" CTIMEOPT_VAL(SQLITE_MAX_FUNCTION_ARG)"127",
438#endif
439#ifdef SQLITE_MAX_LENGTH1000000000
440 "MAX_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LENGTH)"1000000000",
441#endif
442#ifdef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
443 "MAX_LIKE_PATTERN_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_LIKE_PATTERN_LENGTH)"50000",
444#endif
445#ifdef SQLITE_MAX_MEMORY
446 "MAX_MEMORY=" CTIMEOPT_VAL(SQLITE_MAX_MEMORY)"SQLITE_MAX_MEMORY",
447#endif
448#ifdef SQLITE_MAX_MMAP_SIZE0x7fff0000
449 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE)"0x7fff0000",
450#endif
451#ifdef SQLITE_MAX_MMAP_SIZE_
452 "MAX_MMAP_SIZE_=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE_)"SQLITE_MAX_MMAP_SIZE_",
453#endif
454#ifdef SQLITE_MAX_PAGE_COUNT1073741823
455 "MAX_PAGE_COUNT=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_COUNT)"1073741823",
456#endif
457#ifdef SQLITE_MAX_PAGE_SIZE65536
458 "MAX_PAGE_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_PAGE_SIZE)"65536",
459#endif
460#ifdef SQLITE_MAX_SCHEMA_RETRY50
461 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY)"50",
462#endif
463#ifdef SQLITE_MAX_SQL_LENGTH1000000000
464 "MAX_SQL_LENGTH=" CTIMEOPT_VAL(SQLITE_MAX_SQL_LENGTH)"1000000000",
465#endif
466#ifdef SQLITE_MAX_TRIGGER_DEPTH1000
467 "MAX_TRIGGER_DEPTH=" CTIMEOPT_VAL(SQLITE_MAX_TRIGGER_DEPTH)"1000",
468#endif
469#ifdef SQLITE_MAX_VARIABLE_NUMBER999
470 "MAX_VARIABLE_NUMBER=" CTIMEOPT_VAL(SQLITE_MAX_VARIABLE_NUMBER)"999",
471#endif
472#ifdef SQLITE_MAX_VDBE_OP250000000
473 "MAX_VDBE_OP=" CTIMEOPT_VAL(SQLITE_MAX_VDBE_OP)"250000000",
474#endif
475#ifdef SQLITE_MAX_WORKER_THREADS8
476 "MAX_WORKER_THREADS=" CTIMEOPT_VAL(SQLITE_MAX_WORKER_THREADS)"8",
477#endif
478#if SQLITE_MEMDEBUG
479 "MEMDEBUG",
480#endif
481#if SQLITE_MIXED_ENDIAN_64BIT_FLOAT
482 "MIXED_ENDIAN_64BIT_FLOAT",
483#endif
484#if SQLITE_MMAP_READWRITE
485 "MMAP_READWRITE",
486#endif
487#if SQLITE_MUTEX_NOOP
488 "MUTEX_NOOP",
489#endif
490#if SQLITE_MUTEX_NREF0
491 "MUTEX_NREF",
492#endif
493#if SQLITE_MUTEX_OMIT
494 "MUTEX_OMIT",
495#endif
496#if SQLITE_MUTEX_PTHREADS
497 "MUTEX_PTHREADS",
498#endif
499#if SQLITE_MUTEX_W32
500 "MUTEX_W32",
501#endif
502#if SQLITE_NEED_ERR_NAME
503 "NEED_ERR_NAME",
504#endif
505#if SQLITE_NOINLINE__attribute__((noinline))
506 "NOINLINE",
507#endif
508#if SQLITE_NO_SYNC
509 "NO_SYNC",
510#endif
511#if SQLITE_OMIT_ALTERTABLE
512 "OMIT_ALTERTABLE",
513#endif
514#if SQLITE_OMIT_ANALYZE
515 "OMIT_ANALYZE",
516#endif
517#if SQLITE_OMIT_ATTACH
518 "OMIT_ATTACH",
519#endif
520#if SQLITE_OMIT_AUTHORIZATION
521 "OMIT_AUTHORIZATION",
522#endif
523#if SQLITE_OMIT_AUTOINCREMENT
524 "OMIT_AUTOINCREMENT",
525#endif
526#if SQLITE_OMIT_AUTOINIT
527 "OMIT_AUTOINIT",
528#endif
529#if SQLITE_OMIT_AUTOMATIC_INDEX
530 "OMIT_AUTOMATIC_INDEX",
531#endif
532#if SQLITE_OMIT_AUTORESET
533 "OMIT_AUTORESET",
534#endif
535#if SQLITE_OMIT_AUTOVACUUM
536 "OMIT_AUTOVACUUM",
537#endif
538#if SQLITE_OMIT_BETWEEN_OPTIMIZATION
539 "OMIT_BETWEEN_OPTIMIZATION",
540#endif
541#if SQLITE_OMIT_BLOB_LITERAL
542 "OMIT_BLOB_LITERAL",
543#endif
544#if SQLITE_OMIT_BTREECOUNT
545 "OMIT_BTREECOUNT",
546#endif
547#if SQLITE_OMIT_CAST
548 "OMIT_CAST",
549#endif
550#if SQLITE_OMIT_CHECK
551 "OMIT_CHECK",
552#endif
553#if SQLITE_OMIT_COMPLETE
554 "OMIT_COMPLETE",
555#endif
556#if SQLITE_OMIT_COMPOUND_SELECT
557 "OMIT_COMPOUND_SELECT",
558#endif
559#if SQLITE_OMIT_CONFLICT_CLAUSE
560 "OMIT_CONFLICT_CLAUSE",
561#endif
562#if SQLITE_OMIT_CTE
563 "OMIT_CTE",
564#endif
565#if SQLITE_OMIT_DATETIME_FUNCS
566 "OMIT_DATETIME_FUNCS",
567#endif
568#if SQLITE_OMIT_DECLTYPE
569 "OMIT_DECLTYPE",
570#endif
571#if SQLITE_OMIT_DEPRECATED
572 "OMIT_DEPRECATED",
573#endif
574#if SQLITE_OMIT_DISKIO
575 "OMIT_DISKIO",
576#endif
577#if SQLITE_OMIT_EXPLAIN
578 "OMIT_EXPLAIN",
579#endif
580#if SQLITE_OMIT_FLAG_PRAGMAS
581 "OMIT_FLAG_PRAGMAS",
582#endif
583#if SQLITE_OMIT_FLOATING_POINT
584 "OMIT_FLOATING_POINT",
585#endif
586#if SQLITE_OMIT_FOREIGN_KEY
587 "OMIT_FOREIGN_KEY",
588#endif
589#if SQLITE_OMIT_GET_TABLE
590 "OMIT_GET_TABLE",
591#endif
592#if SQLITE_OMIT_HEX_INTEGER
593 "OMIT_HEX_INTEGER",
594#endif
595#if SQLITE_OMIT_INCRBLOB
596 "OMIT_INCRBLOB",
597#endif
598#if SQLITE_OMIT_INTEGRITY_CHECK
599 "OMIT_INTEGRITY_CHECK",
600#endif
601#if SQLITE_OMIT_LIKE_OPTIMIZATION
602 "OMIT_LIKE_OPTIMIZATION",
603#endif
604#if SQLITE_OMIT_LOAD_EXTENSION
605 "OMIT_LOAD_EXTENSION",
606#endif
607#if SQLITE_OMIT_LOCALTIME
608 "OMIT_LOCALTIME",
609#endif
610#if SQLITE_OMIT_LOOKASIDE
611 "OMIT_LOOKASIDE",
612#endif
613#if SQLITE_OMIT_MEMORYDB
614 "OMIT_MEMORYDB",
615#endif
616#if SQLITE_OMIT_OR_OPTIMIZATION
617 "OMIT_OR_OPTIMIZATION",
618#endif
619#if SQLITE_OMIT_PAGER_PRAGMAS
620 "OMIT_PAGER_PRAGMAS",
621#endif
622#if SQLITE_OMIT_PARSER_TRACE
623 "OMIT_PARSER_TRACE",
624#endif
625#if SQLITE_OMIT_POPEN
626 "OMIT_POPEN",
627#endif
628#if SQLITE_OMIT_PRAGMA
629 "OMIT_PRAGMA",
630#endif
631#if SQLITE_OMIT_PROGRESS_CALLBACK
632 "OMIT_PROGRESS_CALLBACK",
633#endif
634#if SQLITE_OMIT_QUICKBALANCE
635 "OMIT_QUICKBALANCE",
636#endif
637#if SQLITE_OMIT_REINDEX
638 "OMIT_REINDEX",
639#endif
640#if SQLITE_OMIT_SCHEMA_PRAGMAS
641 "OMIT_SCHEMA_PRAGMAS",
642#endif
643#if SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
644 "OMIT_SCHEMA_VERSION_PRAGMAS",
645#endif
646#if SQLITE_OMIT_SHARED_CACHE
647 "OMIT_SHARED_CACHE",
648#endif
649#if SQLITE_OMIT_SHUTDOWN_DIRECTORIES
650 "OMIT_SHUTDOWN_DIRECTORIES",
651#endif
652#if SQLITE_OMIT_SUBQUERY
653 "OMIT_SUBQUERY",
654#endif
655#if SQLITE_OMIT_TCL_VARIABLE
656 "OMIT_TCL_VARIABLE",
657#endif
658#if SQLITE_OMIT_TEMPDB
659 "OMIT_TEMPDB",
660#endif
661#if SQLITE_OMIT_TEST_CONTROL
662 "OMIT_TEST_CONTROL",
663#endif
664#if SQLITE_OMIT_TRACE
665 "OMIT_TRACE",
666#endif
667#if SQLITE_OMIT_TRIGGER
668 "OMIT_TRIGGER",
669#endif
670#if SQLITE_OMIT_TRUNCATE_OPTIMIZATION
671 "OMIT_TRUNCATE_OPTIMIZATION",
672#endif
673#if SQLITE_OMIT_UTF16
674 "OMIT_UTF16",
675#endif
676#if SQLITE_OMIT_VACUUM
677 "OMIT_VACUUM",
678#endif
679#if SQLITE_OMIT_VIEW
680 "OMIT_VIEW",
681#endif
682#if SQLITE_OMIT_VIRTUALTABLE
683 "OMIT_VIRTUALTABLE",
684#endif
685#if SQLITE_OMIT_WAL
686 "OMIT_WAL",
687#endif
688#if SQLITE_OMIT_WSD
689 "OMIT_WSD",
690#endif
691#if SQLITE_OMIT_XFER_OPT
692 "OMIT_XFER_OPT",
693#endif
694#if SQLITE_PCACHE_SEPARATE_HEADER
695 "PCACHE_SEPARATE_HEADER",
696#endif
697#if SQLITE_PERFORMANCE_TRACE
698 "PERFORMANCE_TRACE",
699#endif
700#if SQLITE_POWERSAFE_OVERWRITE1
701 "POWERSAFE_OVERWRITE",
702#endif
703#if SQLITE_PREFER_PROXY_LOCKING
704 "PREFER_PROXY_LOCKING",
705#endif
706#if SQLITE_PROXY_DEBUG
707 "PROXY_DEBUG",
708#endif
709#if SQLITE_REVERSE_UNORDERED_SELECTS
710 "REVERSE_UNORDERED_SELECTS",
711#endif
712#if SQLITE_RTREE_INT_ONLY
713 "RTREE_INT_ONLY",
714#endif
715#if SQLITE_SECURE_DELETE
716 "SECURE_DELETE",
717#endif
718#if SQLITE_SMALL_STACK
719 "SMALL_STACK",
720#endif
721#ifdef SQLITE_SORTER_PMASZ250
722 "SORTER_PMASZ=" CTIMEOPT_VAL(SQLITE_SORTER_PMASZ)"250",
723#endif
724#if SQLITE_SOUNDEX
725 "SOUNDEX",
726#endif
727#ifdef SQLITE_STAT4_SAMPLES1
728 "STAT4_SAMPLES=" CTIMEOPT_VAL(SQLITE_STAT4_SAMPLES)"1",
729#endif
730#ifdef SQLITE_STMTJRNL_SPILL(64*1024)
731 "STMTJRNL_SPILL=" CTIMEOPT_VAL(SQLITE_STMTJRNL_SPILL)"(64*1024)",
732#endif
733#if SQLITE_SUBSTR_COMPATIBILITY
734 "SUBSTR_COMPATIBILITY",
735#endif
736#if SQLITE_SYSTEM_MALLOC1
737 "SYSTEM_MALLOC",
738#endif
739#if SQLITE_TCL
740 "TCL",
741#endif
742#ifdef SQLITE_TEMP_STORE1
743 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE)"1",
744#endif
745#if SQLITE_TEST
746 "TEST",
747#endif
748#if defined(SQLITE_THREADSAFE1)
749 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE)"1",
750#elif defined(THREADSAFE)
751 "THREADSAFE=" CTIMEOPT_VAL(THREADSAFE)"THREADSAFE",
752#else
753 "THREADSAFE=1",
754#endif
755#if SQLITE_UNLINK_AFTER_CLOSE
756 "UNLINK_AFTER_CLOSE",
757#endif
758#if SQLITE_UNTESTABLE
759 "UNTESTABLE",
760#endif
761#if SQLITE_USER_AUTHENTICATION
762 "USER_AUTHENTICATION",
763#endif
764#if SQLITE_USE_ALLOCA
765 "USE_ALLOCA",
766#endif
767#if SQLITE_USE_FCNTL_TRACE
768 "USE_FCNTL_TRACE",
769#endif
770#if SQLITE_USE_URI0
771 "USE_URI",
772#endif
773#if SQLITE_VDBE_COVERAGE
774 "VDBE_COVERAGE",
775#endif
776#if SQLITE_WIN32_MALLOC
777 "WIN32_MALLOC",
778#endif
779#if SQLITE_ZERO_MALLOC
780 "ZERO_MALLOC",
781#endif
782/*
783** END CODE GENERATED BY tool/mkctime.tcl
784*/
785};
786
787SQLITE_PRIVATEstatic const char **sqlite3CompileOptions(int *pnOpt){
788 *pnOpt = sizeof(sqlite3azCompileOpt) / sizeof(sqlite3azCompileOpt[0]);
789 return (const char**)sqlite3azCompileOpt;
790}
791
792#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
793
794/************** End of ctime.c ***********************************************/
795/************** Begin file sqliteInt.h ***************************************/
796/*
797** 2001 September 15
798**
799** The author disclaims copyright to this source code. In place of
800** a legal notice, here is a blessing:
801**
802** May you do good and not evil.
803** May you find forgiveness for yourself and forgive others.
804** May you share freely, never taking more than you give.
805**
806*************************************************************************
807** Internal interface definitions for SQLite.
808**
809*/
810#ifndef SQLITEINT_H
811#define SQLITEINT_H
812
813/* Special Comments:
814**
815** Some comments have special meaning to the tools that measure test
816** coverage:
817**
818** NO_TEST - The branches on this line are not
819** measured by branch coverage. This is
820** used on lines of code that actually
821** implement parts of coverage testing.
822**
823** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
824** and the correct answer is still obtained,
825** though perhaps more slowly.
826**
827** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
828** and the correct answer is still obtained,
829** though perhaps more slowly.
830**
831** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
832** that would be harmless and undetectable
833** if it did occur.
834**
835** In all cases, the special comment must be enclosed in the usual
836** slash-asterisk...asterisk-slash comment marks, with no spaces between the
837** asterisks and the comment text.
838*/
839
840/*
841** Make sure the Tcl calling convention macro is defined. This macro is
842** only used by test code and Tcl integration code.
843*/
844#ifndef SQLITE_TCLAPI
845# define SQLITE_TCLAPI
846#endif
847
848/*
849** Include the header file used to customize the compiler options for MSVC.
850** This should be done first so that it can successfully prevent spurious
851** compiler warnings due to subsequent content in this file and other files
852** that are included by this file.
853*/
854/************** Include msvc.h in the middle of sqliteInt.h ******************/
855/************** Begin file msvc.h ********************************************/
856/*
857** 2015 January 12
858**
859** The author disclaims copyright to this source code. In place of
860** a legal notice, here is a blessing:
861**
862** May you do good and not evil.
863** May you find forgiveness for yourself and forgive others.
864** May you share freely, never taking more than you give.
865**
866******************************************************************************
867**
868** This file contains code that is specific to MSVC.
869*/
870#ifndef SQLITE_MSVC_H
871#define SQLITE_MSVC_H
872
873#if defined(_MSC_VER)
874#pragma warning(disable : 4054)
875#pragma warning(disable : 4055)
876#pragma warning(disable : 4100)
877#pragma warning(disable : 4127)
878#pragma warning(disable : 4130)
879#pragma warning(disable : 4152)
880#pragma warning(disable : 4189)
881#pragma warning(disable : 4206)
882#pragma warning(disable : 4210)
883#pragma warning(disable : 4232)
884#pragma warning(disable : 4244)
885#pragma warning(disable : 4305)
886#pragma warning(disable : 4306)
887#pragma warning(disable : 4702)
888#pragma warning(disable : 4706)
889#endif /* defined(_MSC_VER) */
890
891#if defined(_MSC_VER) && !defined(_WIN64)
892#undef SQLITE_4_BYTE_ALIGNED_MALLOC
893#define SQLITE_4_BYTE_ALIGNED_MALLOC
894#endif /* defined(_MSC_VER) && !defined(_WIN64) */
895
896#endif /* SQLITE_MSVC_H */
897
898/************** End of msvc.h ************************************************/
899/************** Continuing where we left off in sqliteInt.h ******************/
900
901/*
902** Special setup for VxWorks
903*/
904/************** Include vxworks.h in the middle of sqliteInt.h ***************/
905/************** Begin file vxworks.h *****************************************/
906/*
907** 2015-03-02
908**
909** The author disclaims copyright to this source code. In place of
910** a legal notice, here is a blessing:
911**
912** May you do good and not evil.
913** May you find forgiveness for yourself and forgive others.
914** May you share freely, never taking more than you give.
915**
916******************************************************************************
917**
918** This file contains code that is specific to Wind River's VxWorks
919*/
920#if defined(__RTP__) || defined(_WRS_KERNEL)
921/* This is VxWorks. Set up things specially for that OS
922*/
923#include <vxWorks.h>
924#include <pthread.h> /* amalgamator: dontcache */
925#define OS_VXWORKS0 1
926#define SQLITE_OS_OTHER0 0
927#define SQLITE_HOMEGROWN_RECURSIVE_MUTEX 1
928#define SQLITE_OMIT_LOAD_EXTENSION 1
929#define SQLITE_ENABLE_LOCKING_STYLE0 0
930#define HAVE_UTIME 1
931#else
932/* This is not VxWorks. */
933#define OS_VXWORKS0 0
934#define HAVE_FCHOWN1 1
935#define HAVE_READLINK1 1
936#define HAVE_LSTAT1 1
937#endif /* defined(_WRS_KERNEL) */
938
939/************** End of vxworks.h *********************************************/
940/************** Continuing where we left off in sqliteInt.h ******************/
941
942/*
943** These #defines should enable >2GB file support on POSIX if the
944** underlying operating system supports it. If the OS lacks
945** large file support, or if the OS is windows, these should be no-ops.
946**
947** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
948** system #includes. Hence, this block of code must be the very first
949** code in all source files.
950**
951** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
952** on the compiler command line. This is necessary if you are compiling
953** on a recent machine (ex: Red Hat 7.2) but you want your code to work
954** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
955** without this option, LFS is enable. But LFS does not exist in the kernel
956** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
957** portability you should omit LFS.
958**
959** The previous paragraph was written in 2005. (This paragraph is written
960** on 2008-11-28.) These days, all Linux kernels support large files, so
961** you should probably leave LFS enabled. But some embedded platforms might
962** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
963**
964** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
965*/
966#ifndef SQLITE_DISABLE_LFS
967# define _LARGE_FILE1 1
968# ifndef _FILE_OFFSET_BITS64
969# define _FILE_OFFSET_BITS64 64
970# endif
971# define _LARGEFILE_SOURCE1 1
972#endif
973
974/* The GCC_VERSION and MSVC_VERSION macros are used to
975** conditionally include optimizations for each of these compilers. A
976** value of 0 means that compiler is not being used. The
977** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
978** optimizations, and hence set all compiler macros to 0
979**
980** There was once also a CLANG_VERSION macro. However, we learn that the
981** version numbers in clang are for "marketing" only and are inconsistent
982** and unreliable. Fortunately, all versions of clang also recognize the
983** gcc version numbers and have reasonable settings for gcc version numbers,
984** so the GCC_VERSION macro will be set to a correct non-zero value even
985** when compiling with clang.
986*/
987#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
988# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
989#else
990# define GCC_VERSION(4*1000000+2*1000+1) 0
991#endif
992#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
993# define MSVC_VERSION0 _MSC_VER
994#else
995# define MSVC_VERSION0 0
996#endif
997
998/* Needed for various definitions... */
999#if defined(__GNUC__4) && !defined(_GNU_SOURCE)
1000# define _GNU_SOURCE
1001#endif
1002
1003#if defined(__OpenBSD__) && !defined(_BSD_SOURCE1)
1004# define _BSD_SOURCE1
1005#endif
1006
1007/*
1008** For MinGW, check to see if we can include the header file containing its
1009** version information, among other things. Normally, this internal MinGW
1010** header file would [only] be included automatically by other MinGW header
1011** files; however, the contained version information is now required by this
1012** header file to work around binary compatibility issues (see below) and
1013** this is the only known way to reliably obtain it. This entire #if block
1014** would be completely unnecessary if there was any other way of detecting
1015** MinGW via their preprocessor (e.g. if they customized their GCC to define
1016** some MinGW-specific macros). When compiling for MinGW, either the
1017** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
1018** defined; otherwise, detection of conditions specific to MinGW will be
1019** disabled.
1020*/
1021#if defined(_HAVE_MINGW_H)
1022# include "mingw.h"
1023#elif defined(_HAVE__MINGW_H)
1024# include "_mingw.h"
1025#endif
1026
1027/*
1028** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
1029** define is required to maintain binary compatibility with the MSVC runtime
1030** library in use (e.g. for Windows XP).
1031*/
1032#if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
1033 defined(_WIN32) && !defined(_WIN64) && \
1034 defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
1035 defined(__MSVCRT__)
1036# define _USE_32BIT_TIME_T
1037#endif
1038
1039/* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
1040** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
1041** MinGW.
1042*/
1043/************** Include sqlite3.h in the middle of sqliteInt.h ***************/
1044/************** Begin file sqlite3.h *****************************************/
1045/*
1046** 2001-09-15
1047**
1048** The author disclaims copyright to this source code. In place of
1049** a legal notice, here is a blessing:
1050**
1051** May you do good and not evil.
1052** May you find forgiveness for yourself and forgive others.
1053** May you share freely, never taking more than you give.
1054**
1055*************************************************************************
1056** This header file defines the interface that the SQLite library
1057** presents to client programs. If a C-function, structure, datatype,
1058** or constant definition does not appear in this file, then it is
1059** not a published API of SQLite, is subject to change without
1060** notice, and should not be referenced by programs that use SQLite.
1061**
1062** Some of the definitions that are in this file are marked as
1063** "experimental". Experimental interfaces are normally new
1064** features recently added to SQLite. We do not anticipate changes
1065** to experimental interfaces but reserve the right to make minor changes
1066** if experience from use "in the wild" suggest such changes are prudent.
1067**
1068** The official C-language API documentation for SQLite is derived
1069** from comments in this file. This file is the authoritative source
1070** on how SQLite interfaces are supposed to operate.
1071**
1072** The name of this file under configuration management is "sqlite.h.in".
1073** The makefile makes some minor changes to this file (such as inserting
1074** the version number) and changes its name to "sqlite3.h" as
1075** part of the build process.
1076*/
1077#ifndef SQLITE3_H
1078#define SQLITE3_H
1079#include <stdarg.h> /* Needed for the definition of va_list */
1080
1081/*
1082** Make sure we can call this stuff from C++.
1083*/
1084#if 0
1085extern "C" {
1086#endif
1087
1088
1089/*
1090** Provide the ability to override linkage features of the interface.
1091*/
1092#ifndef SQLITE_EXTERNextern
1093# define SQLITE_EXTERNextern extern
1094#endif
1095#ifndef SQLITE_API
1096# define SQLITE_API
1097#endif
1098#ifndef SQLITE_CDECL
1099# define SQLITE_CDECL
1100#endif
1101#ifndef SQLITE_APICALL
1102# define SQLITE_APICALL
1103#endif
1104#ifndef SQLITE_STDCALL
1105# define SQLITE_STDCALL SQLITE_APICALL
1106#endif
1107#ifndef SQLITE_CALLBACK
1108# define SQLITE_CALLBACK
1109#endif
1110#ifndef SQLITE_SYSAPI
1111# define SQLITE_SYSAPI
1112#endif
1113
1114/*
1115** These no-op macros are used in front of interfaces to mark those
1116** interfaces as either deprecated or experimental. New applications
1117** should not use deprecated interfaces - they are supported for backwards
1118** compatibility only. Application writers should be aware that
1119** experimental interfaces are subject to change in point releases.
1120**
1121** These macros used to resolve to various kinds of compiler magic that
1122** would generate warning messages when they were used. But that
1123** compiler magic ended up generating such a flurry of bug reports
1124** that we have taken it all out and gone back to using simple
1125** noop macros.
1126*/
1127#define SQLITE_DEPRECATED
1128#define SQLITE_EXPERIMENTAL
1129
1130/*
1131** Ensure these symbols were not defined by some previous header file.
1132*/
1133#ifdef SQLITE_VERSION"3.29.0"
1134# undef SQLITE_VERSION"3.29.0"
1135#endif
1136#ifdef SQLITE_VERSION_NUMBER3029000
1137# undef SQLITE_VERSION_NUMBER3029000
1138#endif
1139
1140/*
1141** CAPI3REF: Compile-Time Library Version Numbers
1142**
1143** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
1144** evaluates to a string literal that is the SQLite version in the
1145** format "X.Y.Z" where X is the major version number (always 3 for
1146** SQLite3) and Y is the minor version number and Z is the release number.)^
1147** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
1148** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
1149** numbers used in [SQLITE_VERSION].)^
1150** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
1151** be larger than the release from which it is derived. Either Y will
1152** be held constant and Z will be incremented or else Y will be incremented
1153** and Z will be reset to zero.
1154**
1155** Since [version 3.6.18] ([dateof:3.6.18]),
1156** SQLite source code has been stored in the
1157** <a href="http://www.fossil-scm.org/">Fossil configuration management
1158** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
1159** a string which identifies a particular check-in of SQLite
1160** within its configuration management system. ^The SQLITE_SOURCE_ID
1161** string contains the date and time of the check-in (UTC) and a SHA1
1162** or SHA3-256 hash of the entire source tree. If the source code has
1163** been edited in any way since it was last checked in, then the last
1164** four hexadecimal digits of the hash may be modified.
1165**
1166** See also: [sqlite3_libversion()],
1167** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1168** [sqlite_version()] and [sqlite_source_id()].
1169*/
1170#define SQLITE_VERSION"3.29.0" "3.29.0"
1171#define SQLITE_VERSION_NUMBER3029000 3029000
1172#define SQLITE_SOURCE_ID"2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88bfa6" "2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88bfa6"
1173
1174/*
1175** CAPI3REF: Run-Time Library Version Numbers
1176** KEYWORDS: sqlite3_version sqlite3_sourceid
1177**
1178** These interfaces provide the same information as the [SQLITE_VERSION],
1179** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
1180** but are associated with the library instead of the header file. ^(Cautious
1181** programmers might include assert() statements in their application to
1182** verify that values returned by these interfaces match the macros in
1183** the header, and thus ensure that the application is
1184** compiled with matching library and header files.
1185**
1186** <blockquote><pre>
1187** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
1188** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
1189** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
1190** </pre></blockquote>)^
1191**
1192** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
1193** macro. ^The sqlite3_libversion() function returns a pointer to the
1194** to the sqlite3_version[] string constant. The sqlite3_libversion()
1195** function is provided for use in DLLs since DLL users usually do not have
1196** direct access to string constants within the DLL. ^The
1197** sqlite3_libversion_number() function returns an integer equal to
1198** [SQLITE_VERSION_NUMBER]. ^(The sqlite3_sourceid() function returns
1199** a pointer to a string constant whose value is the same as the
1200** [SQLITE_SOURCE_ID] C preprocessor macro. Except if SQLite is built
1201** using an edited copy of [the amalgamation], then the last four characters
1202** of the hash might be different from [SQLITE_SOURCE_ID].)^
1203**
1204** See also: [sqlite_version()] and [sqlite_source_id()].
1205*/
1206SQLITE_API const char sqlite3_version[] = SQLITE_VERSION"3.29.0";
1207SQLITE_API const char *sqlite3_libversion(void);
1208SQLITE_API const char *sqlite3_sourceid(void);
1209SQLITE_API int sqlite3_libversion_number(void);
1210
1211/*
1212** CAPI3REF: Run-Time Library Compilation Options Diagnostics
1213**
1214** ^The sqlite3_compileoption_used() function returns 0 or 1
1215** indicating whether the specified option was defined at
1216** compile time. ^The SQLITE_ prefix may be omitted from the
1217** option name passed to sqlite3_compileoption_used().
1218**
1219** ^The sqlite3_compileoption_get() function allows iterating
1220** over the list of options that were defined at compile time by
1221** returning the N-th compile time option string. ^If N is out of range,
1222** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
1223** prefix is omitted from any strings returned by
1224** sqlite3_compileoption_get().
1225**
1226** ^Support for the diagnostic functions sqlite3_compileoption_used()
1227** and sqlite3_compileoption_get() may be omitted by specifying the
1228** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
1229**
1230** See also: SQL functions [sqlite_compileoption_used()] and
1231** [sqlite_compileoption_get()] and the [compile_options pragma].
1232*/
1233#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1234SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
1235SQLITE_API const char *sqlite3_compileoption_get(int N);
1236#else
1237# define sqlite3_compileoption_used(X) 0
1238# define sqlite3_compileoption_get(X) ((void*)0)
1239#endif
1240
1241/*
1242** CAPI3REF: Test To See If The Library Is Threadsafe
1243**
1244** ^The sqlite3_threadsafe() function returns zero if and only if
1245** SQLite was compiled with mutexing code omitted due to the
1246** [SQLITE_THREADSAFE] compile-time option being set to 0.
1247**
1248** SQLite can be compiled with or without mutexes. When
1249** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
1250** are enabled and SQLite is threadsafe. When the
1251** [SQLITE_THREADSAFE] macro is 0,
1252** the mutexes are omitted. Without the mutexes, it is not safe
1253** to use SQLite concurrently from more than one thread.
1254**
1255** Enabling mutexes incurs a measurable performance penalty.
1256** So if speed is of utmost importance, it makes sense to disable
1257** the mutexes. But for maximum safety, mutexes should be enabled.
1258** ^The default behavior is for mutexes to be enabled.
1259**
1260** This interface can be used by an application to make sure that the
1261** version of SQLite that it is linking against was compiled with
1262** the desired setting of the [SQLITE_THREADSAFE] macro.
1263**
1264** This interface only reports on the compile-time mutex setting
1265** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
1266** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
1267** can be fully or partially disabled using a call to [sqlite3_config()]
1268** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
1269** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the
1270** sqlite3_threadsafe() function shows only the compile-time setting of
1271** thread safety, not any run-time changes to that setting made by
1272** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
1273** is unchanged by calls to sqlite3_config().)^
1274**
1275** See the [threading mode] documentation for additional information.
1276*/
1277SQLITE_API int sqlite3_threadsafe(void);
1278
1279/*
1280** CAPI3REF: Database Connection Handle
1281** KEYWORDS: {database connection} {database connections}
1282**
1283** Each open SQLite database is represented by a pointer to an instance of
1284** the opaque structure named "sqlite3". It is useful to think of an sqlite3
1285** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
1286** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
1287** and [sqlite3_close_v2()] are its destructors. There are many other
1288** interfaces (such as
1289** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
1290** [sqlite3_busy_timeout()] to name but three) that are methods on an
1291** sqlite3 object.
1292*/
1293typedef struct sqlite3 sqlite3;
1294
1295/*
1296** CAPI3REF: 64-Bit Integer Types
1297** KEYWORDS: sqlite_int64 sqlite_uint64
1298**
1299** Because there is no cross-platform way to specify 64-bit integer types
1300** SQLite includes typedefs for 64-bit signed and unsigned integers.
1301**
1302** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
1303** The sqlite_int64 and sqlite_uint64 types are supported for backwards
1304** compatibility only.
1305**
1306** ^The sqlite3_int64 and sqlite_int64 types can store integer values
1307** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
1308** sqlite3_uint64 and sqlite_uint64 types can store integer values
1309** between 0 and +18446744073709551615 inclusive.
1310*/
1311#ifdef SQLITE_INT64_TYPE
1312 typedef SQLITE_INT64_TYPE sqlite_int64;
1313# ifdef SQLITE_UINT64_TYPE
1314 typedef SQLITE_UINT64_TYPE sqlite_uint64;
1315# else
1316 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
1317# endif
1318#elif defined(_MSC_VER) || defined(__BORLANDC__)
1319 typedef __int64 sqlite_int64;
1320 typedef unsigned __int64 sqlite_uint64;
1321#else
1322 typedef long long int sqlite_int64;
1323 typedef unsigned long long int sqlite_uint64;
1324#endif
1325typedef sqlite_int64 sqlite3_int64;
1326typedef sqlite_uint64 sqlite3_uint64;
1327
1328/*
1329** If compiling for a processor that lacks floating point support,
1330** substitute integer for floating-point.
1331*/
1332#ifdef SQLITE_OMIT_FLOATING_POINT
1333# define double sqlite3_int64
1334#endif
1335
1336/*
1337** CAPI3REF: Closing A Database Connection
1338** DESTRUCTOR: sqlite3
1339**
1340** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
1341** for the [sqlite3] object.
1342** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
1343** the [sqlite3] object is successfully destroyed and all associated
1344** resources are deallocated.
1345**
1346** ^If the database connection is associated with unfinalized prepared
1347** statements or unfinished sqlite3_backup objects then sqlite3_close()
1348** will leave the database connection open and return [SQLITE_BUSY].
1349** ^If sqlite3_close_v2() is called with unfinalized prepared statements
1350** and/or unfinished sqlite3_backups, then the database connection becomes
1351** an unusable "zombie" which will automatically be deallocated when the
1352** last prepared statement is finalized or the last sqlite3_backup is
1353** finished. The sqlite3_close_v2() interface is intended for use with
1354** host languages that are garbage collected, and where the order in which
1355** destructors are called is arbitrary.
1356**
1357** Applications should [sqlite3_finalize | finalize] all [prepared statements],
1358** [sqlite3_blob_close | close] all [BLOB handles], and
1359** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
1360** with the [sqlite3] object prior to attempting to close the object. ^If
1361** sqlite3_close_v2() is called on a [database connection] that still has
1362** outstanding [prepared statements], [BLOB handles], and/or
1363** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
1364** of resources is deferred until all [prepared statements], [BLOB handles],
1365** and [sqlite3_backup] objects are also destroyed.
1366**
1367** ^If an [sqlite3] object is destroyed while a transaction is open,
1368** the transaction is automatically rolled back.
1369**
1370** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
1371** must be either a NULL
1372** pointer or an [sqlite3] object pointer obtained
1373** from [sqlite3_open()], [sqlite3_open16()], or
1374** [sqlite3_open_v2()], and not previously closed.
1375** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
1376** argument is a harmless no-op.
1377*/
1378SQLITE_API int sqlite3_close(sqlite3*);
1379SQLITE_API int sqlite3_close_v2(sqlite3*);
1380
1381/*
1382** The type for a callback function.
1383** This is legacy and deprecated. It is included for historical
1384** compatibility and is not documented.
1385*/
1386typedef int (*sqlite3_callback)(void*,int,char**, char**);
1387
1388/*
1389** CAPI3REF: One-Step Query Execution Interface
1390** METHOD: sqlite3
1391**
1392** The sqlite3_exec() interface is a convenience wrapper around
1393** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
1394** that allows an application to run multiple statements of SQL
1395** without having to use a lot of C code.
1396**
1397** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
1398** semicolon-separate SQL statements passed into its 2nd argument,
1399** in the context of the [database connection] passed in as its 1st
1400** argument. ^If the callback function of the 3rd argument to
1401** sqlite3_exec() is not NULL, then it is invoked for each result row
1402** coming out of the evaluated SQL statements. ^The 4th argument to
1403** sqlite3_exec() is relayed through to the 1st argument of each
1404** callback invocation. ^If the callback pointer to sqlite3_exec()
1405** is NULL, then no callback is ever invoked and result rows are
1406** ignored.
1407**
1408** ^If an error occurs while evaluating the SQL statements passed into
1409** sqlite3_exec(), then execution of the current statement stops and
1410** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
1411** is not NULL then any error message is written into memory obtained
1412** from [sqlite3_malloc()] and passed back through the 5th parameter.
1413** To avoid memory leaks, the application should invoke [sqlite3_free()]
1414** on error message strings returned through the 5th parameter of
1415** sqlite3_exec() after the error message string is no longer needed.
1416** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
1417** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
1418** NULL before returning.
1419**
1420** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
1421** routine returns SQLITE_ABORT without invoking the callback again and
1422** without running any subsequent SQL statements.
1423**
1424** ^The 2nd argument to the sqlite3_exec() callback function is the
1425** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
1426** callback is an array of pointers to strings obtained as if from
1427** [sqlite3_column_text()], one for each column. ^If an element of a
1428** result row is NULL then the corresponding string pointer for the
1429** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
1430** sqlite3_exec() callback is an array of pointers to strings where each
1431** entry represents the name of corresponding result column as obtained
1432** from [sqlite3_column_name()].
1433**
1434** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
1435** to an empty string, or a pointer that contains only whitespace and/or
1436** SQL comments, then no SQL statements are evaluated and the database
1437** is not changed.
1438**
1439** Restrictions:
1440**
1441** <ul>
1442** <li> The application must ensure that the 1st parameter to sqlite3_exec()
1443** is a valid and open [database connection].
1444** <li> The application must not close the [database connection] specified by
1445** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
1446** <li> The application must not modify the SQL statement text passed into
1447** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
1448** </ul>
1449*/
1450SQLITE_API int sqlite3_exec(
1451 sqlite3*, /* An open database */
1452 const char *sql, /* SQL to be evaluated */
1453 int (*callback)(void*,int,char**,char**), /* Callback function */
1454 void *, /* 1st argument to callback */
1455 char **errmsg /* Error msg written here */
1456);
1457
1458/*
1459** CAPI3REF: Result Codes
1460** KEYWORDS: {result code definitions}
1461**
1462** Many SQLite functions return an integer result code from the set shown
1463** here in order to indicate success or failure.
1464**
1465** New error codes may be added in future versions of SQLite.
1466**
1467** See also: [extended result code definitions]
1468*/
1469#define SQLITE_OK0 0 /* Successful result */
1470/* beginning-of-error-codes */
1471#define SQLITE_ERROR1 1 /* Generic error */
1472#define SQLITE_INTERNAL2 2 /* Internal logic error in SQLite */
1473#define SQLITE_PERM3 3 /* Access permission denied */
1474#define SQLITE_ABORT4 4 /* Callback routine requested an abort */
1475#define SQLITE_BUSY5 5 /* The database file is locked */
1476#define SQLITE_LOCKED6 6 /* A table in the database is locked */
1477#define SQLITE_NOMEM7 7 /* A malloc() failed */
1478#define SQLITE_READONLY8 8 /* Attempt to write a readonly database */
1479#define SQLITE_INTERRUPT9 9 /* Operation terminated by sqlite3_interrupt()*/
1480#define SQLITE_IOERR10 10 /* Some kind of disk I/O error occurred */
1481#define SQLITE_CORRUPT11 11 /* The database disk image is malformed */
1482#define SQLITE_NOTFOUND12 12 /* Unknown opcode in sqlite3_file_control() */
1483#define SQLITE_FULL13 13 /* Insertion failed because database is full */
1484#define SQLITE_CANTOPEN14 14 /* Unable to open the database file */
1485#define SQLITE_PROTOCOL15 15 /* Database lock protocol error */
1486#define SQLITE_EMPTY16 16 /* Internal use only */
1487#define SQLITE_SCHEMA17 17 /* The database schema changed */
1488#define SQLITE_TOOBIG18 18 /* String or BLOB exceeds size limit */
1489#define SQLITE_CONSTRAINT19 19 /* Abort due to constraint violation */
1490#define SQLITE_MISMATCH20 20 /* Data type mismatch */
1491#define SQLITE_MISUSE21 21 /* Library used incorrectly */
1492#define SQLITE_NOLFS22 22 /* Uses OS features not supported on host */
1493#define SQLITE_AUTH23 23 /* Authorization denied */
1494#define SQLITE_FORMAT24 24 /* Not used */
1495#define SQLITE_RANGE25 25 /* 2nd parameter to sqlite3_bind out of range */
1496#define SQLITE_NOTADB26 26 /* File opened that is not a database file */
1497#define SQLITE_NOTICE27 27 /* Notifications from sqlite3_log() */
1498#define SQLITE_WARNING28 28 /* Warnings from sqlite3_log() */
1499#define SQLITE_ROW100 100 /* sqlite3_step() has another row ready */
1500#define SQLITE_DONE101 101 /* sqlite3_step() has finished executing */
1501/* end-of-error-codes */
1502
1503/*
1504** CAPI3REF: Extended Result Codes
1505** KEYWORDS: {extended result code definitions}
1506**
1507** In its default configuration, SQLite API routines return one of 30 integer
1508** [result codes]. However, experience has shown that many of
1509** these result codes are too coarse-grained. They do not provide as
1510** much information about problems as programmers might like. In an effort to
1511** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
1512** and later) include
1513** support for additional result codes that provide more detailed information
1514** about errors. These [extended result codes] are enabled or disabled
1515** on a per database connection basis using the
1516** [sqlite3_extended_result_codes()] API. Or, the extended code for
1517** the most recent error can be obtained using
1518** [sqlite3_extended_errcode()].
1519*/
1520#define SQLITE_ERROR_MISSING_COLLSEQ(1 | (1<<8)) (SQLITE_ERROR1 | (1<<8))
1521#define SQLITE_ERROR_RETRY(1 | (2<<8)) (SQLITE_ERROR1 | (2<<8))
1522#define SQLITE_ERROR_SNAPSHOT(1 | (3<<8)) (SQLITE_ERROR1 | (3<<8))
1523#define SQLITE_IOERR_READ(10 | (1<<8)) (SQLITE_IOERR10 | (1<<8))
1524#define SQLITE_IOERR_SHORT_READ(10 | (2<<8)) (SQLITE_IOERR10 | (2<<8))
1525#define SQLITE_IOERR_WRITE(10 | (3<<8)) (SQLITE_IOERR10 | (3<<8))
1526#define SQLITE_IOERR_FSYNC(10 | (4<<8)) (SQLITE_IOERR10 | (4<<8))
1527#define SQLITE_IOERR_DIR_FSYNC(10 | (5<<8)) (SQLITE_IOERR10 | (5<<8))
1528#define SQLITE_IOERR_TRUNCATE(10 | (6<<8)) (SQLITE_IOERR10 | (6<<8))
1529#define SQLITE_IOERR_FSTAT(10 | (7<<8)) (SQLITE_IOERR10 | (7<<8))
1530#define SQLITE_IOERR_UNLOCK(10 | (8<<8)) (SQLITE_IOERR10 | (8<<8))
1531#define SQLITE_IOERR_RDLOCK(10 | (9<<8)) (SQLITE_IOERR10 | (9<<8))
1532#define SQLITE_IOERR_DELETE(10 | (10<<8)) (SQLITE_IOERR10 | (10<<8))
1533#define SQLITE_IOERR_BLOCKED(10 | (11<<8)) (SQLITE_IOERR10 | (11<<8))
1534#define SQLITE_IOERR_NOMEM(10 | (12<<8)) (SQLITE_IOERR10 | (12<<8))
1535#define SQLITE_IOERR_ACCESS(10 | (13<<8)) (SQLITE_IOERR10 | (13<<8))
1536#define SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)) (SQLITE_IOERR10 | (14<<8))
1537#define SQLITE_IOERR_LOCK(10 | (15<<8)) (SQLITE_IOERR10 | (15<<8))
1538#define SQLITE_IOERR_CLOSE(10 | (16<<8)) (SQLITE_IOERR10 | (16<<8))
1539#define SQLITE_IOERR_DIR_CLOSE(10 | (17<<8)) (SQLITE_IOERR10 | (17<<8))
1540#define SQLITE_IOERR_SHMOPEN(10 | (18<<8)) (SQLITE_IOERR10 | (18<<8))
1541#define SQLITE_IOERR_SHMSIZE(10 | (19<<8)) (SQLITE_IOERR10 | (19<<8))
1542#define SQLITE_IOERR_SHMLOCK(10 | (20<<8)) (SQLITE_IOERR10 | (20<<8))
1543#define SQLITE_IOERR_SHMMAP(10 | (21<<8)) (SQLITE_IOERR10 | (21<<8))
1544#define SQLITE_IOERR_SEEK(10 | (22<<8)) (SQLITE_IOERR10 | (22<<8))
1545#define SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)) (SQLITE_IOERR10 | (23<<8))
1546#define SQLITE_IOERR_MMAP(10 | (24<<8)) (SQLITE_IOERR10 | (24<<8))
1547#define SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)) (SQLITE_IOERR10 | (25<<8))
1548#define SQLITE_IOERR_CONVPATH(10 | (26<<8)) (SQLITE_IOERR10 | (26<<8))
1549#define SQLITE_IOERR_VNODE(10 | (27<<8)) (SQLITE_IOERR10 | (27<<8))
1550#define SQLITE_IOERR_AUTH(10 | (28<<8)) (SQLITE_IOERR10 | (28<<8))
1551#define SQLITE_IOERR_BEGIN_ATOMIC(10 | (29<<8)) (SQLITE_IOERR10 | (29<<8))
1552#define SQLITE_IOERR_COMMIT_ATOMIC(10 | (30<<8)) (SQLITE_IOERR10 | (30<<8))
1553#define SQLITE_IOERR_ROLLBACK_ATOMIC(10 | (31<<8)) (SQLITE_IOERR10 | (31<<8))
1554#define SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8)) (SQLITE_LOCKED6 | (1<<8))
1555#define SQLITE_LOCKED_VTAB(6 | (2<<8)) (SQLITE_LOCKED6 | (2<<8))
1556#define SQLITE_BUSY_RECOVERY(5 | (1<<8)) (SQLITE_BUSY5 | (1<<8))
1557#define SQLITE_BUSY_SNAPSHOT(5 | (2<<8)) (SQLITE_BUSY5 | (2<<8))
1558#define SQLITE_CANTOPEN_NOTEMPDIR(14 | (1<<8)) (SQLITE_CANTOPEN14 | (1<<8))
1559#define SQLITE_CANTOPEN_ISDIR(14 | (2<<8)) (SQLITE_CANTOPEN14 | (2<<8))
1560#define SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)) (SQLITE_CANTOPEN14 | (3<<8))
1561#define SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)) (SQLITE_CANTOPEN14 | (4<<8))
1562#define SQLITE_CANTOPEN_DIRTYWAL(14 | (5<<8)) (SQLITE_CANTOPEN14 | (5<<8)) /* Not Used */
1563#define SQLITE_CORRUPT_VTAB(11 | (1<<8)) (SQLITE_CORRUPT11 | (1<<8))
1564#define SQLITE_CORRUPT_SEQUENCE(11 | (2<<8)) (SQLITE_CORRUPT11 | (2<<8))
1565#define SQLITE_READONLY_RECOVERY(8 | (1<<8)) (SQLITE_READONLY8 | (1<<8))
1566#define SQLITE_READONLY_CANTLOCK(8 | (2<<8)) (SQLITE_READONLY8 | (2<<8))
1567#define SQLITE_READONLY_ROLLBACK(8 | (3<<8)) (SQLITE_READONLY8 | (3<<8))
1568#define SQLITE_READONLY_DBMOVED(8 | (4<<8)) (SQLITE_READONLY8 | (4<<8))
1569#define SQLITE_READONLY_CANTINIT(8 | (5<<8)) (SQLITE_READONLY8 | (5<<8))
1570#define SQLITE_READONLY_DIRECTORY(8 | (6<<8)) (SQLITE_READONLY8 | (6<<8))
1571#define SQLITE_ABORT_ROLLBACK(4 | (2<<8)) (SQLITE_ABORT4 | (2<<8))
1572#define SQLITE_CONSTRAINT_CHECK(19 | (1<<8)) (SQLITE_CONSTRAINT19 | (1<<8))
1573#define SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8)) (SQLITE_CONSTRAINT19 | (2<<8))
1574#define SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)) (SQLITE_CONSTRAINT19 | (3<<8))
1575#define SQLITE_CONSTRAINT_FUNCTION(19 | (4<<8)) (SQLITE_CONSTRAINT19 | (4<<8))
1576#define SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)) (SQLITE_CONSTRAINT19 | (5<<8))
1577#define SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8)) (SQLITE_CONSTRAINT19 | (6<<8))
1578#define SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)) (SQLITE_CONSTRAINT19 | (7<<8))
1579#define SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)) (SQLITE_CONSTRAINT19 | (8<<8))
1580#define SQLITE_CONSTRAINT_VTAB(19 | (9<<8)) (SQLITE_CONSTRAINT19 | (9<<8))
1581#define SQLITE_CONSTRAINT_ROWID(19 |(10<<8)) (SQLITE_CONSTRAINT19 |(10<<8))
1582#define SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)) (SQLITE_NOTICE27 | (1<<8))
1583#define SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)) (SQLITE_NOTICE27 | (2<<8))
1584#define SQLITE_WARNING_AUTOINDEX(28 | (1<<8)) (SQLITE_WARNING28 | (1<<8))
1585#define SQLITE_AUTH_USER(23 | (1<<8)) (SQLITE_AUTH23 | (1<<8))
1586#define SQLITE_OK_LOAD_PERMANENTLY(0 | (1<<8)) (SQLITE_OK0 | (1<<8))
1587
1588/*
1589** CAPI3REF: Flags For File Open Operations
1590**
1591** These bit values are intended for use in the
1592** 3rd parameter to the [sqlite3_open_v2()] interface and
1593** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
1594*/
1595#define SQLITE_OPEN_READONLY0x00000001 0x00000001 /* Ok for sqlite3_open_v2() */
1596#define SQLITE_OPEN_READWRITE0x00000002 0x00000002 /* Ok for sqlite3_open_v2() */
1597#define SQLITE_OPEN_CREATE0x00000004 0x00000004 /* Ok for sqlite3_open_v2() */
1598#define SQLITE_OPEN_DELETEONCLOSE0x00000008 0x00000008 /* VFS only */
1599#define SQLITE_OPEN_EXCLUSIVE0x00000010 0x00000010 /* VFS only */
1600#define SQLITE_OPEN_AUTOPROXY0x00000020 0x00000020 /* VFS only */
1601#define SQLITE_OPEN_URI0x00000040 0x00000040 /* Ok for sqlite3_open_v2() */
1602#define SQLITE_OPEN_MEMORY0x00000080 0x00000080 /* Ok for sqlite3_open_v2() */
1603#define SQLITE_OPEN_MAIN_DB0x00000100 0x00000100 /* VFS only */
1604#define SQLITE_OPEN_TEMP_DB0x00000200 0x00000200 /* VFS only */
1605#define SQLITE_OPEN_TRANSIENT_DB0x00000400 0x00000400 /* VFS only */
1606#define SQLITE_OPEN_MAIN_JOURNAL0x00000800 0x00000800 /* VFS only */
1607#define SQLITE_OPEN_TEMP_JOURNAL0x00001000 0x00001000 /* VFS only */
1608#define SQLITE_OPEN_SUBJOURNAL0x00002000 0x00002000 /* VFS only */
1609#define SQLITE_OPEN_MASTER_JOURNAL0x00004000 0x00004000 /* VFS only */
1610#define SQLITE_OPEN_NOMUTEX0x00008000 0x00008000 /* Ok for sqlite3_open_v2() */
1611#define SQLITE_OPEN_FULLMUTEX0x00010000 0x00010000 /* Ok for sqlite3_open_v2() */
1612#define SQLITE_OPEN_SHAREDCACHE0x00020000 0x00020000 /* Ok for sqlite3_open_v2() */
1613#define SQLITE_OPEN_PRIVATECACHE0x00040000 0x00040000 /* Ok for sqlite3_open_v2() */
1614#define SQLITE_OPEN_WAL0x00080000 0x00080000 /* VFS only */
1615
1616/* Reserved: 0x00F00000 */
1617
1618/*
1619** CAPI3REF: Device Characteristics
1620**
1621** The xDeviceCharacteristics method of the [sqlite3_io_methods]
1622** object returns an integer which is a vector of these
1623** bit values expressing I/O characteristics of the mass storage
1624** device that holds the file that the [sqlite3_io_methods]
1625** refers to.
1626**
1627** The SQLITE_IOCAP_ATOMIC property means that all writes of
1628** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1629** mean that writes of blocks that are nnn bytes in size and
1630** are aligned to an address which is an integer multiple of
1631** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1632** that when data is appended to a file, the data is appended
1633** first then the size of the file is extended, never the other
1634** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1635** information is written to disk in the same order as calls
1636** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
1637** after reboot following a crash or power loss, the only bytes in a
1638** file that were written at the application level might have changed
1639** and that adjacent bytes, even bytes within the same sector are
1640** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
1641** flag indicates that a file cannot be deleted when open. The
1642** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
1643** read-only media and cannot be changed even by processes with
1644** elevated privileges.
1645**
1646** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
1647** filesystem supports doing multiple write operations atomically when those
1648** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
1649** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
1650*/
1651#define SQLITE_IOCAP_ATOMIC0x00000001 0x00000001
1652#define SQLITE_IOCAP_ATOMIC5120x00000002 0x00000002
1653#define SQLITE_IOCAP_ATOMIC1K0x00000004 0x00000004
1654#define SQLITE_IOCAP_ATOMIC2K0x00000008 0x00000008
1655#define SQLITE_IOCAP_ATOMIC4K0x00000010 0x00000010
1656#define SQLITE_IOCAP_ATOMIC8K0x00000020 0x00000020
1657#define SQLITE_IOCAP_ATOMIC16K0x00000040 0x00000040
1658#define SQLITE_IOCAP_ATOMIC32K0x00000080 0x00000080
1659#define SQLITE_IOCAP_ATOMIC64K0x00000100 0x00000100
1660#define SQLITE_IOCAP_SAFE_APPEND0x00000200 0x00000200
1661#define SQLITE_IOCAP_SEQUENTIAL0x00000400 0x00000400
1662#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800 0x00000800
1663#define SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 0x00001000
1664#define SQLITE_IOCAP_IMMUTABLE0x00002000 0x00002000
1665#define SQLITE_IOCAP_BATCH_ATOMIC0x00004000 0x00004000
1666
1667/*
1668** CAPI3REF: File Locking Levels
1669**
1670** SQLite uses one of these integer values as the second
1671** argument to calls it makes to the xLock() and xUnlock() methods
1672** of an [sqlite3_io_methods] object.
1673*/
1674#define SQLITE_LOCK_NONE0 0
1675#define SQLITE_LOCK_SHARED1 1
1676#define SQLITE_LOCK_RESERVED2 2
1677#define SQLITE_LOCK_PENDING3 3
1678#define SQLITE_LOCK_EXCLUSIVE4 4
1679
1680/*
1681** CAPI3REF: Synchronization Type Flags
1682**
1683** When SQLite invokes the xSync() method of an
1684** [sqlite3_io_methods] object it uses a combination of
1685** these integer values as the second argument.
1686**
1687** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
1688** sync operation only needs to flush data to mass storage. Inode
1689** information need not be flushed. If the lower four bits of the flag
1690** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
1691** If the lower four bits equal SQLITE_SYNC_FULL, that means
1692** to use Mac OS X style fullsync instead of fsync().
1693**
1694** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
1695** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
1696** settings. The [synchronous pragma] determines when calls to the
1697** xSync VFS method occur and applies uniformly across all platforms.
1698** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
1699** energetic or rigorous or forceful the sync operations are and
1700** only make a difference on Mac OSX for the default SQLite code.
1701** (Third-party VFS implementations might also make the distinction
1702** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
1703** operating systems natively supported by SQLite, only Mac OSX
1704** cares about the difference.)
1705*/
1706#define SQLITE_SYNC_NORMAL0x00002 0x00002
1707#define SQLITE_SYNC_FULL0x00003 0x00003
1708#define SQLITE_SYNC_DATAONLY0x00010 0x00010
1709
1710/*
1711** CAPI3REF: OS Interface Open File Handle
1712**
1713** An [sqlite3_file] object represents an open file in the
1714** [sqlite3_vfs | OS interface layer]. Individual OS interface
1715** implementations will
1716** want to subclass this object by appending additional fields
1717** for their own use. The pMethods entry is a pointer to an
1718** [sqlite3_io_methods] object that defines methods for performing
1719** I/O operations on the open file.
1720*/
1721typedef struct sqlite3_file sqlite3_file;
1722struct sqlite3_file {
1723 const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
1724};
1725
1726/*
1727** CAPI3REF: OS Interface File Virtual Methods Object
1728**
1729** Every file opened by the [sqlite3_vfs.xOpen] method populates an
1730** [sqlite3_file] object (or, more commonly, a subclass of the
1731** [sqlite3_file] object) with a pointer to an instance of this object.
1732** This object defines the methods used to perform various operations
1733** against the open file represented by the [sqlite3_file] object.
1734**
1735** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
1736** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1737** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
1738** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
1739** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
1740** to NULL.
1741**
1742** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
1743** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
1744** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
1745** flag may be ORed in to indicate that only the data of the file
1746** and not its inode needs to be synced.
1747**
1748** The integer values to xLock() and xUnlock() are one of
1749** <ul>
1750** <li> [SQLITE_LOCK_NONE],
1751** <li> [SQLITE_LOCK_SHARED],
1752** <li> [SQLITE_LOCK_RESERVED],
1753** <li> [SQLITE_LOCK_PENDING], or
1754** <li> [SQLITE_LOCK_EXCLUSIVE].
1755** </ul>
1756** xLock() increases the lock. xUnlock() decreases the lock.
1757** The xCheckReservedLock() method checks whether any database connection,
1758** either in this process or in some other process, is holding a RESERVED,
1759** PENDING, or EXCLUSIVE lock on the file. It returns true
1760** if such a lock exists and false otherwise.
1761**
1762** The xFileControl() method is a generic interface that allows custom
1763** VFS implementations to directly control an open file using the
1764** [sqlite3_file_control()] interface. The second "op" argument is an
1765** integer opcode. The third argument is a generic pointer intended to
1766** point to a structure that may contain arguments or space in which to
1767** write return values. Potential uses for xFileControl() might be
1768** functions to enable blocking locks with timeouts, to change the
1769** locking strategy (for example to use dot-file locks), to inquire
1770** about the status of a lock, or to break stale locks. The SQLite
1771** core reserves all opcodes less than 100 for its own use.
1772** A [file control opcodes | list of opcodes] less than 100 is available.
1773** Applications that define a custom xFileControl method should use opcodes
1774** greater than 100 to avoid conflicts. VFS implementations should
1775** return [SQLITE_NOTFOUND] for file control opcodes that they do not
1776** recognize.
1777**
1778** The xSectorSize() method returns the sector size of the
1779** device that underlies the file. The sector size is the
1780** minimum write that can be performed without disturbing
1781** other bytes in the file. The xDeviceCharacteristics()
1782** method returns a bit vector describing behaviors of the
1783** underlying device:
1784**
1785** <ul>
1786** <li> [SQLITE_IOCAP_ATOMIC]
1787** <li> [SQLITE_IOCAP_ATOMIC512]
1788** <li> [SQLITE_IOCAP_ATOMIC1K]
1789** <li> [SQLITE_IOCAP_ATOMIC2K]
1790** <li> [SQLITE_IOCAP_ATOMIC4K]
1791** <li> [SQLITE_IOCAP_ATOMIC8K]
1792** <li> [SQLITE_IOCAP_ATOMIC16K]
1793** <li> [SQLITE_IOCAP_ATOMIC32K]
1794** <li> [SQLITE_IOCAP_ATOMIC64K]
1795** <li> [SQLITE_IOCAP_SAFE_APPEND]
1796** <li> [SQLITE_IOCAP_SEQUENTIAL]
1797** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
1798** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
1799** <li> [SQLITE_IOCAP_IMMUTABLE]
1800** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
1801** </ul>
1802**
1803** The SQLITE_IOCAP_ATOMIC property means that all writes of
1804** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
1805** mean that writes of blocks that are nnn bytes in size and
1806** are aligned to an address which is an integer multiple of
1807** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1808** that when data is appended to a file, the data is appended
1809** first then the size of the file is extended, never the other
1810** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1811** information is written to disk in the same order as calls
1812** to xWrite().
1813**
1814** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
1815** in the unread portions of the buffer with zeros. A VFS that
1816** fails to zero-fill short reads might seem to work. However,
1817** failure to zero-fill short reads will eventually lead to
1818** database corruption.
1819*/
1820typedef struct sqlite3_io_methods sqlite3_io_methods;
1821struct sqlite3_io_methods {
1822 int iVersion;
1823 int (*xClose)(sqlite3_file*);
1824 int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
1825 int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
1826 int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
1827 int (*xSync)(sqlite3_file*, int flags);
1828 int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
1829 int (*xLock)(sqlite3_file*, int);
1830 int (*xUnlock)(sqlite3_file*, int);
1831 int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
1832 int (*xFileControl)(sqlite3_file*, int op, void *pArg);
1833 int (*xSectorSize)(sqlite3_file*);
1834 int (*xDeviceCharacteristics)(sqlite3_file*);
1835 /* Methods above are valid for version 1 */
1836 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1837 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1838 void (*xShmBarrier)(sqlite3_file*);
1839 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1840 /* Methods above are valid for version 2 */
1841 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1842 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1843 /* Methods above are valid for version 3 */
1844 /* Additional methods may be added in future releases */
1845};
1846
1847/*
1848** CAPI3REF: Standard File Control Opcodes
1849** KEYWORDS: {file control opcodes} {file control opcode}
1850**
1851** These integer constants are opcodes for the xFileControl method
1852** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
1853** interface.
1854**
1855** <ul>
1856** <li>[[SQLITE_FCNTL_LOCKSTATE]]
1857** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
1858** opcode causes the xFileControl method to write the current state of
1859** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
1860** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
1861** into an integer that the pArg argument points to. This capability
1862** is used during testing and is only available when the SQLITE_TEST
1863** compile-time option is used.
1864**
1865** <li>[[SQLITE_FCNTL_SIZE_HINT]]
1866** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
1867** layer a hint of how large the database file will grow to be during the
1868** current transaction. This hint is not guaranteed to be accurate but it
1869** is often close. The underlying VFS might choose to preallocate database
1870** file space based on this hint in order to help writes to the database
1871** file run faster.
1872**
1873** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
1874** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1875** implements [sqlite3_deserialize()] to set an upper bound on the size
1876** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1877** If the integer pointed to is negative, then it is filled in with the
1878** current limit. Otherwise the limit is set to the larger of the value
1879** of the integer pointed to and the current database size. The integer
1880** pointed to is set to the new limit.
1881**
1882** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
1883** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
1884** extends and truncates the database file in chunks of a size specified
1885** by the user. The fourth argument to [sqlite3_file_control()] should
1886** point to an integer (type int) containing the new chunk-size to use
1887** for the nominated database. Allocating database file space in large
1888** chunks (say 1MB at a time), may reduce file-system fragmentation and
1889** improve performance on some systems.
1890**
1891** <li>[[SQLITE_FCNTL_FILE_POINTER]]
1892** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
1893** to the [sqlite3_file] object associated with a particular database
1894** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER].
1895**
1896** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
1897** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
1898** to the [sqlite3_file] object associated with the journal file (either
1899** the [rollback journal] or the [write-ahead log]) for a particular database
1900** connection. See also [SQLITE_FCNTL_FILE_POINTER].
1901**
1902** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
1903** No longer in use.
1904**
1905** <li>[[SQLITE_FCNTL_SYNC]]
1906** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
1907** sent to the VFS immediately before the xSync method is invoked on a
1908** database file descriptor. Or, if the xSync method is not invoked
1909** because the user has configured SQLite with
1910** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
1911** of the xSync method. In most cases, the pointer argument passed with
1912** this file-control is NULL. However, if the database file is being synced
1913** as part of a multi-database commit, the argument points to a nul-terminated
1914** string containing the transactions master-journal file name. VFSes that
1915** do not need this signal should silently ignore this opcode. Applications
1916** should not call [sqlite3_file_control()] with this opcode as doing so may
1917** disrupt the operation of the specialized VFSes that do require it.
1918**
1919** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
1920** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
1921** and sent to the VFS after a transaction has been committed immediately
1922** but before the database is unlocked. VFSes that do not need this signal
1923** should silently ignore this opcode. Applications should not call
1924** [sqlite3_file_control()] with this opcode as doing so may disrupt the
1925** operation of the specialized VFSes that do require it.
1926**
1927** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
1928** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1929** retry counts and intervals for certain disk I/O operations for the
1930** windows [VFS] in order to provide robustness in the presence of
1931** anti-virus programs. By default, the windows VFS will retry file read,
1932** file write, and file delete operations up to 10 times, with a delay
1933** of 25 milliseconds before the first retry and with the delay increasing
1934** by an additional 25 milliseconds with each subsequent retry. This
1935** opcode allows these two values (10 retries and 25 milliseconds of delay)
1936** to be adjusted. The values are changed for all database connections
1937** within the same process. The argument is a pointer to an array of two
1938** integers where the first integer is the new retry count and the second
1939** integer is the delay. If either integer is negative, then the setting
1940** is not changed but instead the prior value of that setting is written
1941** into the array entry, allowing the current retry settings to be
1942** interrogated. The zDbName parameter is ignored.
1943**
1944** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
1945** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
1946** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary
1947** write ahead log ([WAL file]) and shared memory
1948** files used for transaction control
1949** are automatically deleted when the latest connection to the database
1950** closes. Setting persistent WAL mode causes those files to persist after
1951** close. Persisting the files is useful when other processes that do not
1952** have write permission on the directory containing the database file want
1953** to read the database file, as the WAL and shared memory files must exist
1954** in order for the database to be readable. The fourth parameter to
1955** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1956** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1957** WAL mode. If the integer is -1, then it is overwritten with the current
1958** WAL persistence setting.
1959**
1960** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
1961** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1962** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1963** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1964** xDeviceCharacteristics methods. The fourth parameter to
1965** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1966** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1967** mode. If the integer is -1, then it is overwritten with the current
1968** zero-damage mode setting.
1969**
1970** <li>[[SQLITE_FCNTL_OVERWRITE]]
1971** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1972** a write transaction to indicate that, unless it is rolled back for some
1973** reason, the entire database file will be overwritten by the current
1974** transaction. This is used by VACUUM operations.
1975**
1976** <li>[[SQLITE_FCNTL_VFSNAME]]
1977** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1978** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1979** final bottom-level VFS are written into memory obtained from
1980** [sqlite3_malloc()] and the result is stored in the char* variable
1981** that the fourth parameter of [sqlite3_file_control()] points to.
1982** The caller is responsible for freeing the memory when done. As with
1983** all file-control actions, there is no guarantee that this will actually
1984** do anything. Callers should initialize the char* variable to a NULL
1985** pointer in case this file-control is not implemented. This file-control
1986** is intended for diagnostic use only.
1987**
1988** <li>[[SQLITE_FCNTL_VFS_POINTER]]
1989** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1990** [VFSes] currently in use. ^(The argument X in
1991** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
1992** of type "[sqlite3_vfs] **". This opcodes will set *X
1993** to a pointer to the top-level VFS.)^
1994** ^When there are multiple VFS shims in the stack, this opcode finds the
1995** upper-most shim only.
1996**
1997** <li>[[SQLITE_FCNTL_PRAGMA]]
1998** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
1999** file control is sent to the open [sqlite3_file] object corresponding
2000** to the database file to which the pragma statement refers. ^The argument
2001** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
2002** pointers to strings (char**) in which the second element of the array
2003** is the name of the pragma and the third element is the argument to the
2004** pragma or NULL if the pragma has no argument. ^The handler for an
2005** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
2006** of the char** argument point to a string obtained from [sqlite3_mprintf()]
2007** or the equivalent and that string will become the result of the pragma or
2008** the error message if the pragma fails. ^If the
2009** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
2010** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA]
2011** file control returns [SQLITE_OK], then the parser assumes that the
2012** VFS has handled the PRAGMA itself and the parser generates a no-op
2013** prepared statement if result string is NULL, or that returns a copy
2014** of the result string if the string is non-NULL.
2015** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
2016** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
2017** that the VFS encountered an error while handling the [PRAGMA] and the
2018** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
2019** file control occurs at the beginning of pragma statement analysis and so
2020** it is able to override built-in [PRAGMA] statements.
2021**
2022** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
2023** ^The [SQLITE_FCNTL_BUSYHANDLER]
2024** file-control may be invoked by SQLite on the database file handle
2025** shortly after it is opened in order to provide a custom VFS with access
2026** to the connections busy-handler callback. The argument is of type (void **)
2027** - an array of two (void *) values. The first (void *) actually points
2028** to a function of type (int (*)(void *)). In order to invoke the connections
2029** busy-handler, this function should be invoked with the second (void *) in
2030** the array as the only argument. If it returns non-zero, then the operation
2031** should be retried. If it returns zero, the custom VFS should abandon the
2032** current operation.
2033**
2034** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
2035** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
2036** to have SQLite generate a
2037** temporary filename using the same algorithm that is followed to generate
2038** temporary filenames for TEMP tables and other internal uses. The
2039** argument should be a char** which will be filled with the filename
2040** written into memory obtained from [sqlite3_malloc()]. The caller should
2041** invoke [sqlite3_free()] on the result to avoid a memory leak.
2042**
2043** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
2044** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
2045** maximum number of bytes that will be used for memory-mapped I/O.
2046** The argument is a pointer to a value of type sqlite3_int64 that
2047** is an advisory maximum number of bytes in the file to memory map. The
2048** pointer is overwritten with the old value. The limit is not changed if
2049** the value originally pointed to is negative, and so the current limit
2050** can be queried by passing in a pointer to a negative number. This
2051** file-control is used internally to implement [PRAGMA mmap_size].
2052**
2053** <li>[[SQLITE_FCNTL_TRACE]]
2054** The [SQLITE_FCNTL_TRACE] file control provides advisory information
2055** to the VFS about what the higher layers of the SQLite stack are doing.
2056** This file control is used by some VFS activity tracing [shims].
2057** The argument is a zero-terminated string. Higher layers in the
2058** SQLite stack may generate instances of this file control if
2059** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
2060**
2061** <li>[[SQLITE_FCNTL_HAS_MOVED]]
2062** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
2063** pointer to an integer and it writes a boolean into that integer depending
2064** on whether or not the file has been renamed, moved, or deleted since it
2065** was first opened.
2066**
2067** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
2068** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
2069** underlying native file handle associated with a file handle. This file
2070** control interprets its argument as a pointer to a native file handle and
2071** writes the resulting value there.
2072**
2073** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
2074** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This
2075** opcode causes the xFileControl method to swap the file handle with the one
2076** pointed to by the pArg argument. This capability is used during testing
2077** and only needs to be supported when SQLITE_TEST is defined.
2078**
2079** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
2080** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
2081** be advantageous to block on the next WAL lock if the lock is not immediately
2082** available. The WAL subsystem issues this signal during rare
2083** circumstances in order to fix a problem with priority inversion.
2084** Applications should <em>not</em> use this file-control.
2085**
2086** <li>[[SQLITE_FCNTL_ZIPVFS]]
2087** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
2088** VFS should return SQLITE_NOTFOUND for this opcode.
2089**
2090** <li>[[SQLITE_FCNTL_RBU]]
2091** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
2092** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
2093** this opcode.
2094**
2095** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
2096** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
2097** the file descriptor is placed in "batch write mode", which
2098** means all subsequent write operations will be deferred and done
2099** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]. Systems
2100** that do not support batch atomic writes will return SQLITE_NOTFOUND.
2101** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
2102** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
2103** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
2104** no VFS interface calls on the same [sqlite3_file] file descriptor
2105** except for calls to the xWrite method and the xFileControl method
2106** with [SQLITE_FCNTL_SIZE_HINT].
2107**
2108** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
2109** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
2110** operations since the previous successful call to
2111** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
2112** This file control returns [SQLITE_OK] if and only if the writes were
2113** all performed successfully and have been committed to persistent storage.
2114** ^Regardless of whether or not it is successful, this file control takes
2115** the file descriptor out of batch write mode so that all subsequent
2116** write operations are independent.
2117** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
2118** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2119**
2120** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
2121** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
2122** operations since the previous successful call to
2123** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
2124** ^This file control takes the file descriptor out of batch write mode
2125** so that all subsequent write operations are independent.
2126** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
2127** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
2128**
2129** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
2130** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
2131** a file lock using the xLock or xShmLock methods of the VFS to wait
2132** for up to M milliseconds before failing, where M is the single
2133** unsigned integer parameter.
2134**
2135** <li>[[SQLITE_FCNTL_DATA_VERSION]]
2136** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
2137** a database file. The argument is a pointer to a 32-bit unsigned integer.
2138** The "data version" for the pager is written into the pointer. The
2139** "data version" changes whenever any change occurs to the corresponding
2140** database file, either through SQL statements on the same database
2141** connection or through transactions committed by separate database
2142** connections possibly in other processes. The [sqlite3_total_changes()]
2143** interface can be used to find if any database on the connection has changed,
2144** but that interface responds to changes on TEMP as well as MAIN and does
2145** not provide a mechanism to detect changes to MAIN only. Also, the
2146** [sqlite3_total_changes()] interface responds to internal changes only and
2147** omits changes made by other database connections. The
2148** [PRAGMA data_version] command provide a mechanism to detect changes to
2149** a single attached database that occur due to other database connections,
2150** but omits changes implemented by the database connection on which it is
2151** called. This file control is the only mechanism to detect changes that
2152** happen either internally or externally and that are associated with
2153** a particular attached database.
2154** </ul>
2155*/
2156#define SQLITE_FCNTL_LOCKSTATE1 1
2157#define SQLITE_FCNTL_GET_LOCKPROXYFILE2 2
2158#define SQLITE_FCNTL_SET_LOCKPROXYFILE3 3
2159#define SQLITE_FCNTL_LAST_ERRNO4 4
2160#define SQLITE_FCNTL_SIZE_HINT5 5
2161#define SQLITE_FCNTL_CHUNK_SIZE6 6
2162#define SQLITE_FCNTL_FILE_POINTER7 7
2163#define SQLITE_FCNTL_SYNC_OMITTED8 8
2164#define SQLITE_FCNTL_WIN32_AV_RETRY9 9
2165#define SQLITE_FCNTL_PERSIST_WAL10 10
2166#define SQLITE_FCNTL_OVERWRITE11 11
2167#define SQLITE_FCNTL_VFSNAME12 12
2168#define SQLITE_FCNTL_POWERSAFE_OVERWRITE13 13
2169#define SQLITE_FCNTL_PRAGMA14 14
2170#define SQLITE_FCNTL_BUSYHANDLER15 15
2171#define SQLITE_FCNTL_TEMPFILENAME16 16
2172#define SQLITE_FCNTL_MMAP_SIZE18 18
2173#define SQLITE_FCNTL_TRACE19 19
2174#define SQLITE_FCNTL_HAS_MOVED20 20
2175#define SQLITE_FCNTL_SYNC21 21
2176#define SQLITE_FCNTL_COMMIT_PHASETWO22 22
2177#define SQLITE_FCNTL_WIN32_SET_HANDLE23 23
2178#define SQLITE_FCNTL_WAL_BLOCK24 24
2179#define SQLITE_FCNTL_ZIPVFS25 25
2180#define SQLITE_FCNTL_RBU26 26
2181#define SQLITE_FCNTL_VFS_POINTER27 27
2182#define SQLITE_FCNTL_JOURNAL_POINTER28 28
2183#define SQLITE_FCNTL_WIN32_GET_HANDLE29 29
2184#define SQLITE_FCNTL_PDB30 30
2185#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31 31
2186#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32 32
2187#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33 33
2188#define SQLITE_FCNTL_LOCK_TIMEOUT34 34
2189#define SQLITE_FCNTL_DATA_VERSION35 35
2190#define SQLITE_FCNTL_SIZE_LIMIT36 36
2191
2192/* deprecated names */
2193#define SQLITE_GET_LOCKPROXYFILE2 SQLITE_FCNTL_GET_LOCKPROXYFILE2
2194#define SQLITE_SET_LOCKPROXYFILE3 SQLITE_FCNTL_SET_LOCKPROXYFILE3
2195#define SQLITE_LAST_ERRNO4 SQLITE_FCNTL_LAST_ERRNO4
2196
2197
2198/*
2199** CAPI3REF: Mutex Handle
2200**
2201** The mutex module within SQLite defines [sqlite3_mutex] to be an
2202** abstract type for a mutex object. The SQLite core never looks
2203** at the internal representation of an [sqlite3_mutex]. It only
2204** deals with pointers to the [sqlite3_mutex] object.
2205**
2206** Mutexes are created using [sqlite3_mutex_alloc()].
2207*/
2208typedef struct sqlite3_mutex sqlite3_mutex;
2209
2210/*
2211** CAPI3REF: Loadable Extension Thunk
2212**
2213** A pointer to the opaque sqlite3_api_routines structure is passed as
2214** the third parameter to entry points of [loadable extensions]. This
2215** structure must be typedefed in order to work around compiler warnings
2216** on some platforms.
2217*/
2218typedef struct sqlite3_api_routines sqlite3_api_routines;
2219
2220/*
2221** CAPI3REF: OS Interface Object
2222**
2223** An instance of the sqlite3_vfs object defines the interface between
2224** the SQLite core and the underlying operating system. The "vfs"
2225** in the name of the object stands for "virtual file system". See
2226** the [VFS | VFS documentation] for further information.
2227**
2228** The VFS interface is sometimes extended by adding new methods onto
2229** the end. Each time such an extension occurs, the iVersion field
2230** is incremented. The iVersion value started out as 1 in
2231** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
2232** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
2233** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6]. Additional fields
2234** may be appended to the sqlite3_vfs object and the iVersion value
2235** may increase again in future versions of SQLite.
2236** Note that the structure
2237** of the sqlite3_vfs object changes in the transition from
2238** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
2239** and yet the iVersion field was not modified.
2240**
2241** The szOsFile field is the size of the subclassed [sqlite3_file]
2242** structure used by this VFS. mxPathname is the maximum length of
2243** a pathname in this VFS.
2244**
2245** Registered sqlite3_vfs objects are kept on a linked list formed by
2246** the pNext pointer. The [sqlite3_vfs_register()]
2247** and [sqlite3_vfs_unregister()] interfaces manage this list
2248** in a thread-safe way. The [sqlite3_vfs_find()] interface
2249** searches the list. Neither the application code nor the VFS
2250** implementation should use the pNext pointer.
2251**
2252** The pNext field is the only field in the sqlite3_vfs
2253** structure that SQLite will ever modify. SQLite will only access
2254** or modify this field while holding a particular static mutex.
2255** The application should never modify anything within the sqlite3_vfs
2256** object once the object has been registered.
2257**
2258** The zName field holds the name of the VFS module. The name must
2259** be unique across all VFS modules.
2260**
2261** [[sqlite3_vfs.xOpen]]
2262** ^SQLite guarantees that the zFilename parameter to xOpen
2263** is either a NULL pointer or string obtained
2264** from xFullPathname() with an optional suffix added.
2265** ^If a suffix is added to the zFilename parameter, it will
2266** consist of a single "-" character followed by no more than
2267** 11 alphanumeric and/or "-" characters.
2268** ^SQLite further guarantees that
2269** the string will be valid and unchanged until xClose() is
2270** called. Because of the previous sentence,
2271** the [sqlite3_file] can safely store a pointer to the
2272** filename if it needs to remember the filename for some reason.
2273** If the zFilename parameter to xOpen is a NULL pointer then xOpen
2274** must invent its own temporary name for the file. ^Whenever the
2275** xFilename parameter is NULL it will also be the case that the
2276** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
2277**
2278** The flags argument to xOpen() includes all bits set in
2279** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
2280** or [sqlite3_open16()] is used, then flags includes at least
2281** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
2282** If xOpen() opens a file read-only then it sets *pOutFlags to
2283** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
2284**
2285** ^(SQLite will also add one of the following flags to the xOpen()
2286** call, depending on the object being opened:
2287**
2288** <ul>
2289** <li> [SQLITE_OPEN_MAIN_DB]
2290** <li> [SQLITE_OPEN_MAIN_JOURNAL]
2291** <li> [SQLITE_OPEN_TEMP_DB]
2292** <li> [SQLITE_OPEN_TEMP_JOURNAL]
2293** <li> [SQLITE_OPEN_TRANSIENT_DB]
2294** <li> [SQLITE_OPEN_SUBJOURNAL]
2295** <li> [SQLITE_OPEN_MASTER_JOURNAL]
2296** <li> [SQLITE_OPEN_WAL]
2297** </ul>)^
2298**
2299** The file I/O implementation can use the object type flags to
2300** change the way it deals with files. For example, an application
2301** that does not care about crash recovery or rollback might make
2302** the open of a journal file a no-op. Writes to this journal would
2303** also be no-ops, and any attempt to read the journal would return
2304** SQLITE_IOERR. Or the implementation might recognize that a database
2305** file will be doing page-aligned sector reads and writes in a random
2306** order and set up its I/O subsystem accordingly.
2307**
2308** SQLite might also add one of the following flags to the xOpen method:
2309**
2310** <ul>
2311** <li> [SQLITE_OPEN_DELETEONCLOSE]
2312** <li> [SQLITE_OPEN_EXCLUSIVE]
2313** </ul>
2314**
2315** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
2316** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
2317** will be set for TEMP databases and their journals, transient
2318** databases, and subjournals.
2319**
2320** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
2321** with the [SQLITE_OPEN_CREATE] flag, which are both directly
2322** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
2323** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
2324** SQLITE_OPEN_CREATE, is used to indicate that file should always
2325** be created, and that it is an error if it already exists.
2326** It is <i>not</i> used to indicate the file should be opened
2327** for exclusive access.
2328**
2329** ^At least szOsFile bytes of memory are allocated by SQLite
2330** to hold the [sqlite3_file] structure passed as the third
2331** argument to xOpen. The xOpen method does not have to
2332** allocate the structure; it should just fill it in. Note that
2333** the xOpen method must set the sqlite3_file.pMethods to either
2334** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
2335** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
2336** element will be valid after xOpen returns regardless of the success
2337** or failure of the xOpen call.
2338**
2339** [[sqlite3_vfs.xAccess]]
2340** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
2341** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
2342** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
2343** to test whether a file is at least readable. The SQLITE_ACCESS_READ
2344** flag is never actually used and is not implemented in the built-in
2345** VFSes of SQLite. The file is named by the second argument and can be a
2346** directory. The xAccess method returns [SQLITE_OK] on success or some
2347** non-zero error code if there is an I/O error or if the name of
2348** the file given in the second argument is illegal. If SQLITE_OK
2349** is returned, then non-zero or zero is written into *pResOut to indicate
2350** whether or not the file is accessible.
2351**
2352** ^SQLite will always allocate at least mxPathname+1 bytes for the
2353** output buffer xFullPathname. The exact size of the output buffer
2354** is also passed as a parameter to both methods. If the output buffer
2355** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
2356** handled as a fatal error by SQLite, vfs implementations should endeavor
2357** to prevent this by setting mxPathname to a sufficiently large value.
2358**
2359** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
2360** interfaces are not strictly a part of the filesystem, but they are
2361** included in the VFS structure for completeness.
2362** The xRandomness() function attempts to return nBytes bytes
2363** of good-quality randomness into zOut. The return value is
2364** the actual number of bytes of randomness obtained.
2365** The xSleep() method causes the calling thread to sleep for at
2366** least the number of microseconds given. ^The xCurrentTime()
2367** method returns a Julian Day Number for the current date and time as
2368** a floating point value.
2369** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
2370** Day Number multiplied by 86400000 (the number of milliseconds in
2371** a 24-hour day).
2372** ^SQLite will use the xCurrentTimeInt64() method to get the current
2373** date and time if that method is available (if iVersion is 2 or
2374** greater and the function pointer is not NULL) and will fall back
2375** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
2376**
2377** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
2378** are not used by the SQLite core. These optional interfaces are provided
2379** by some VFSes to facilitate testing of the VFS code. By overriding
2380** system calls with functions under its control, a test program can
2381** simulate faults and error conditions that would otherwise be difficult
2382** or impossible to induce. The set of system calls that can be overridden
2383** varies from one VFS to another, and from one version of the same VFS to the
2384** next. Applications that use these interfaces must be prepared for any
2385** or all of these interfaces to be NULL or for their behavior to change
2386** from one release to the next. Applications must not attempt to access
2387** any of these methods if the iVersion of the VFS is less than 3.
2388*/
2389typedef struct sqlite3_vfs sqlite3_vfs;
2390typedef void (*sqlite3_syscall_ptr)(void);
2391struct sqlite3_vfs {
2392 int iVersion; /* Structure version number (currently 3) */
2393 int szOsFile; /* Size of subclassed sqlite3_file */
2394 int mxPathname; /* Maximum file pathname length */
2395 sqlite3_vfs *pNext; /* Next registered VFS */
2396 const char *zName; /* Name of this virtual file system */
2397 void *pAppData; /* Pointer to application-specific data */
2398 int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
2399 int flags, int *pOutFlags);
2400 int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
2401 int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
2402 int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
2403 void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
2404 void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
2405 void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
2406 void (*xDlClose)(sqlite3_vfs*, void*);
2407 int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
2408 int (*xSleep)(sqlite3_vfs*, int microseconds);
2409 int (*xCurrentTime)(sqlite3_vfs*, double*);
2410 int (*xGetLastError)(sqlite3_vfs*, int, char *);
2411 /*
2412 ** The methods above are in version 1 of the sqlite_vfs object
2413 ** definition. Those that follow are added in version 2 or later
2414 */
2415 int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
2416 /*
2417 ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
2418 ** Those below are for version 3 and greater.
2419 */
2420 int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
2421 sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
2422 const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
2423 /*
2424 ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
2425 ** New fields may be appended in future versions. The iVersion
2426 ** value will increment whenever this happens.
2427 */
2428};
2429
2430/*
2431** CAPI3REF: Flags for the xAccess VFS method
2432**
2433** These integer constants can be used as the third parameter to
2434** the xAccess method of an [sqlite3_vfs] object. They determine
2435** what kind of permissions the xAccess method is looking for.
2436** With SQLITE_ACCESS_EXISTS, the xAccess method
2437** simply checks whether the file exists.
2438** With SQLITE_ACCESS_READWRITE, the xAccess method
2439** checks whether the named directory is both readable and writable
2440** (in other words, if files can be added, removed, and renamed within
2441** the directory).
2442** The SQLITE_ACCESS_READWRITE constant is currently used only by the
2443** [temp_store_directory pragma], though this could change in a future
2444** release of SQLite.
2445** With SQLITE_ACCESS_READ, the xAccess method
2446** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
2447** currently unused, though it might be used in a future release of
2448** SQLite.
2449*/
2450#define SQLITE_ACCESS_EXISTS0 0
2451#define SQLITE_ACCESS_READWRITE1 1 /* Used by PRAGMA temp_store_directory */
2452#define SQLITE_ACCESS_READ2 2 /* Unused */
2453
2454/*
2455** CAPI3REF: Flags for the xShmLock VFS method
2456**
2457** These integer constants define the various locking operations
2458** allowed by the xShmLock method of [sqlite3_io_methods]. The
2459** following are the only legal combinations of flags to the
2460** xShmLock method:
2461**
2462** <ul>
2463** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
2464** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
2465** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
2466** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
2467** </ul>
2468**
2469** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
2470** was given on the corresponding lock.
2471**
2472** The xShmLock method can transition between unlocked and SHARED or
2473** between unlocked and EXCLUSIVE. It cannot transition between SHARED
2474** and EXCLUSIVE.
2475*/
2476#define SQLITE_SHM_UNLOCK1 1
2477#define SQLITE_SHM_LOCK2 2
2478#define SQLITE_SHM_SHARED4 4
2479#define SQLITE_SHM_EXCLUSIVE8 8
2480
2481/*
2482** CAPI3REF: Maximum xShmLock index
2483**
2484** The xShmLock method on [sqlite3_io_methods] may use values
2485** between 0 and this upper bound as its "offset" argument.
2486** The SQLite core will never attempt to acquire or release a
2487** lock outside of this range
2488*/
2489#define SQLITE_SHM_NLOCK8 8
2490
2491
2492/*
2493** CAPI3REF: Initialize The SQLite Library
2494**
2495** ^The sqlite3_initialize() routine initializes the
2496** SQLite library. ^The sqlite3_shutdown() routine
2497** deallocates any resources that were allocated by sqlite3_initialize().
2498** These routines are designed to aid in process initialization and
2499** shutdown on embedded systems. Workstation applications using
2500** SQLite normally do not need to invoke either of these routines.
2501**
2502** A call to sqlite3_initialize() is an "effective" call if it is
2503** the first time sqlite3_initialize() is invoked during the lifetime of
2504** the process, or if it is the first time sqlite3_initialize() is invoked
2505** following a call to sqlite3_shutdown(). ^(Only an effective call
2506** of sqlite3_initialize() does any initialization. All other calls
2507** are harmless no-ops.)^
2508**
2509** A call to sqlite3_shutdown() is an "effective" call if it is the first
2510** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
2511** an effective call to sqlite3_shutdown() does any deinitialization.
2512** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
2513**
2514** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
2515** is not. The sqlite3_shutdown() interface must only be called from a
2516** single thread. All open [database connections] must be closed and all
2517** other SQLite resources must be deallocated prior to invoking
2518** sqlite3_shutdown().
2519**
2520** Among other things, ^sqlite3_initialize() will invoke
2521** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
2522** will invoke sqlite3_os_end().
2523**
2524** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
2525** ^If for some reason, sqlite3_initialize() is unable to initialize
2526** the library (perhaps it is unable to allocate a needed resource such
2527** as a mutex) it returns an [error code] other than [SQLITE_OK].
2528**
2529** ^The sqlite3_initialize() routine is called internally by many other
2530** SQLite interfaces so that an application usually does not need to
2531** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
2532** calls sqlite3_initialize() so the SQLite library will be automatically
2533** initialized when [sqlite3_open()] is called if it has not be initialized
2534** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
2535** compile-time option, then the automatic calls to sqlite3_initialize()
2536** are omitted and the application must call sqlite3_initialize() directly
2537** prior to using any other SQLite interface. For maximum portability,
2538** it is recommended that applications always invoke sqlite3_initialize()
2539** directly prior to using any other SQLite interface. Future releases
2540** of SQLite may require this. In other words, the behavior exhibited
2541** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
2542** default behavior in some future release of SQLite.
2543**
2544** The sqlite3_os_init() routine does operating-system specific
2545** initialization of the SQLite library. The sqlite3_os_end()
2546** routine undoes the effect of sqlite3_os_init(). Typical tasks
2547** performed by these routines include allocation or deallocation
2548** of static resources, initialization of global variables,
2549** setting up a default [sqlite3_vfs] module, or setting up
2550** a default configuration using [sqlite3_config()].
2551**
2552** The application should never invoke either sqlite3_os_init()
2553** or sqlite3_os_end() directly. The application should only invoke
2554** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
2555** interface is called automatically by sqlite3_initialize() and
2556** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
2557** implementations for sqlite3_os_init() and sqlite3_os_end()
2558** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
2559** When [custom builds | built for other platforms]
2560** (using the [SQLITE_OS_OTHER=1] compile-time
2561** option) the application must supply a suitable implementation for
2562** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
2563** implementation of sqlite3_os_init() or sqlite3_os_end()
2564** must return [SQLITE_OK] on success and some other [error code] upon
2565** failure.
2566*/
2567SQLITE_API int sqlite3_initialize(void);
2568SQLITE_API int sqlite3_shutdown(void);
2569SQLITE_API int sqlite3_os_init(void);
2570SQLITE_API int sqlite3_os_end(void);
2571
2572/*
2573** CAPI3REF: Configuring The SQLite Library
2574**
2575** The sqlite3_config() interface is used to make global configuration
2576** changes to SQLite in order to tune SQLite to the specific needs of
2577** the application. The default configuration is recommended for most
2578** applications and so this routine is usually not necessary. It is
2579** provided to support rare applications with unusual needs.
2580**
2581** <b>The sqlite3_config() interface is not threadsafe. The application
2582** must ensure that no other SQLite interfaces are invoked by other
2583** threads while sqlite3_config() is running.</b>
2584**
2585** The sqlite3_config() interface
2586** may only be invoked prior to library initialization using
2587** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
2588** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
2589** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
2590** Note, however, that ^sqlite3_config() can be called as part of the
2591** implementation of an application-defined [sqlite3_os_init()].
2592**
2593** The first argument to sqlite3_config() is an integer
2594** [configuration option] that determines
2595** what property of SQLite is to be configured. Subsequent arguments
2596** vary depending on the [configuration option]
2597** in the first argument.
2598**
2599** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
2600** ^If the option is unknown or SQLite is unable to set the option
2601** then this routine returns a non-zero [error code].
2602*/
2603SQLITE_API int sqlite3_config(int, ...);
2604
2605/*
2606** CAPI3REF: Configure database connections
2607** METHOD: sqlite3
2608**
2609** The sqlite3_db_config() interface is used to make configuration
2610** changes to a [database connection]. The interface is similar to
2611** [sqlite3_config()] except that the changes apply to a single
2612** [database connection] (specified in the first argument).
2613**
2614** The second argument to sqlite3_db_config(D,V,...) is the
2615** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2616** that indicates what aspect of the [database connection] is being configured.
2617** Subsequent arguments vary depending on the configuration verb.
2618**
2619** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
2620** the call is considered successful.
2621*/
2622SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
2623
2624/*
2625** CAPI3REF: Memory Allocation Routines
2626**
2627** An instance of this object defines the interface between SQLite
2628** and low-level memory allocation routines.
2629**
2630** This object is used in only one place in the SQLite interface.
2631** A pointer to an instance of this object is the argument to
2632** [sqlite3_config()] when the configuration option is
2633** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
2634** By creating an instance of this object
2635** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
2636** during configuration, an application can specify an alternative
2637** memory allocation subsystem for SQLite to use for all of its
2638** dynamic memory needs.
2639**
2640** Note that SQLite comes with several [built-in memory allocators]
2641** that are perfectly adequate for the overwhelming majority of applications
2642** and that this object is only useful to a tiny minority of applications
2643** with specialized memory allocation requirements. This object is
2644** also used during testing of SQLite in order to specify an alternative
2645** memory allocator that simulates memory out-of-memory conditions in
2646** order to verify that SQLite recovers gracefully from such
2647** conditions.
2648**
2649** The xMalloc, xRealloc, and xFree methods must work like the
2650** malloc(), realloc() and free() functions from the standard C library.
2651** ^SQLite guarantees that the second argument to
2652** xRealloc is always a value returned by a prior call to xRoundup.
2653**
2654** xSize should return the allocated size of a memory allocation
2655** previously obtained from xMalloc or xRealloc. The allocated size
2656** is always at least as big as the requested size but may be larger.
2657**
2658** The xRoundup method returns what would be the allocated size of
2659** a memory allocation given a particular requested size. Most memory
2660** allocators round up memory allocations at least to the next multiple
2661** of 8. Some allocators round up to a larger multiple or to a power of 2.
2662** Every memory allocation request coming in through [sqlite3_malloc()]
2663** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
2664** that causes the corresponding memory allocation to fail.
2665**
2666** The xInit method initializes the memory allocator. For example,
2667** it might allocate any require mutexes or initialize internal data
2668** structures. The xShutdown method is invoked (indirectly) by
2669** [sqlite3_shutdown()] and should deallocate any resources acquired
2670** by xInit. The pAppData pointer is used as the only parameter to
2671** xInit and xShutdown.
2672**
2673** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
2674** the xInit method, so the xInit method need not be threadsafe. The
2675** xShutdown method is only called from [sqlite3_shutdown()] so it does
2676** not need to be threadsafe either. For all other methods, SQLite
2677** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
2678** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
2679** it is by default) and so the methods are automatically serialized.
2680** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
2681** methods must be threadsafe or else make their own arrangements for
2682** serialization.
2683**
2684** SQLite will never invoke xInit() more than once without an intervening
2685** call to xShutdown().
2686*/
2687typedef struct sqlite3_mem_methods sqlite3_mem_methods;
2688struct sqlite3_mem_methods {
2689 void *(*xMalloc)(int); /* Memory allocation function */
2690 void (*xFree)(void*); /* Free a prior allocation */
2691 void *(*xRealloc)(void*,int); /* Resize an allocation */
2692 int (*xSize)(void*); /* Return the size of an allocation */
2693 int (*xRoundup)(int); /* Round up request size to allocation size */
2694 int (*xInit)(void*); /* Initialize the memory allocator */
2695 void (*xShutdown)(void*); /* Deinitialize the memory allocator */
2696 void *pAppData; /* Argument to xInit() and xShutdown() */
2697};
2698
2699/*
2700** CAPI3REF: Configuration Options
2701** KEYWORDS: {configuration option}
2702**
2703** These constants are the available integer configuration options that
2704** can be passed as the first argument to the [sqlite3_config()] interface.
2705**
2706** New configuration options may be added in future releases of SQLite.
2707** Existing configuration options might be discontinued. Applications
2708** should check the return code from [sqlite3_config()] to make sure that
2709** the call worked. The [sqlite3_config()] interface will return a
2710** non-zero [error code] if a discontinued or unsupported configuration option
2711** is invoked.
2712**
2713** <dl>
2714** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
2715** <dd>There are no arguments to this option. ^This option sets the
2716** [threading mode] to Single-thread. In other words, it disables
2717** all mutexing and puts SQLite into a mode where it can only be used
2718** by a single thread. ^If SQLite is compiled with
2719** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2720** it is not possible to change the [threading mode] from its default
2721** value of Single-thread and so [sqlite3_config()] will return
2722** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
2723** configuration option.</dd>
2724**
2725** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
2726** <dd>There are no arguments to this option. ^This option sets the
2727** [threading mode] to Multi-thread. In other words, it disables
2728** mutexing on [database connection] and [prepared statement] objects.
2729** The application is responsible for serializing access to
2730** [database connections] and [prepared statements]. But other mutexes
2731** are enabled so that SQLite will be safe to use in a multi-threaded
2732** environment as long as no two threads attempt to use the same
2733** [database connection] at the same time. ^If SQLite is compiled with
2734** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2735** it is not possible to set the Multi-thread [threading mode] and
2736** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2737** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
2738**
2739** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
2740** <dd>There are no arguments to this option. ^This option sets the
2741** [threading mode] to Serialized. In other words, this option enables
2742** all mutexes including the recursive
2743** mutexes on [database connection] and [prepared statement] objects.
2744** In this mode (which is the default when SQLite is compiled with
2745** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
2746** to [database connections] and [prepared statements] so that the
2747** application is free to use the same [database connection] or the
2748** same [prepared statement] in different threads at the same time.
2749** ^If SQLite is compiled with
2750** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2751** it is not possible to set the Serialized [threading mode] and
2752** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
2753** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
2754**
2755** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
2756** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
2757** a pointer to an instance of the [sqlite3_mem_methods] structure.
2758** The argument specifies
2759** alternative low-level memory allocation routines to be used in place of
2760** the memory allocation routines built into SQLite.)^ ^SQLite makes
2761** its own private copy of the content of the [sqlite3_mem_methods] structure
2762** before the [sqlite3_config()] call returns.</dd>
2763**
2764** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
2765** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
2766** is a pointer to an instance of the [sqlite3_mem_methods] structure.
2767** The [sqlite3_mem_methods]
2768** structure is filled with the currently defined memory allocation routines.)^
2769** This option can be used to overload the default memory allocation
2770** routines with a wrapper that simulations memory allocation failure or
2771** tracks memory usage, for example. </dd>
2772**
2773** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
2774** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
2775** type int, interpreted as a boolean, which if true provides a hint to
2776** SQLite that it should avoid large memory allocations if possible.
2777** SQLite will run faster if it is free to make large memory allocations,
2778** but some application might prefer to run slower in exchange for
2779** guarantees about memory fragmentation that are possible if large
2780** allocations are avoided. This hint is normally off.
2781** </dd>
2782**
2783** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
2784** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
2785** interpreted as a boolean, which enables or disables the collection of
2786** memory allocation statistics. ^(When memory allocation statistics are
2787** disabled, the following SQLite interfaces become non-operational:
2788** <ul>
2789** <li> [sqlite3_memory_used()]
2790** <li> [sqlite3_memory_highwater()]
2791** <li> [sqlite3_soft_heap_limit64()]
2792** <li> [sqlite3_status64()]
2793** </ul>)^
2794** ^Memory allocation statistics are enabled by default unless SQLite is
2795** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
2796** allocation statistics are disabled by default.
2797** </dd>
2798**
2799** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
2800** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
2801** </dd>
2802**
2803** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
2804** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
2805** that SQLite can use for the database page cache with the default page
2806** cache implementation.
2807** This configuration option is a no-op if an application-define page
2808** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
2809** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
2810** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2811** and the number of cache lines (N).
2812** The sz argument should be the size of the largest database page
2813** (a power of two between 512 and 65536) plus some extra bytes for each
2814** page header. ^The number of extra bytes needed by the page header
2815** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
2816** ^It is harmless, apart from the wasted memory,
2817** for the sz parameter to be larger than necessary. The pMem
2818** argument must be either a NULL pointer or a pointer to an 8-byte
2819** aligned block of memory of at least sz*N bytes, otherwise
2820** subsequent behavior is undefined.
2821** ^When pMem is not NULL, SQLite will strive to use the memory provided
2822** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
2823** a page cache line is larger than sz bytes or if all of the pMem buffer
2824** is exhausted.
2825** ^If pMem is NULL and N is non-zero, then each database connection
2826** does an initial bulk allocation for page cache memory
2827** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
2828** of -1024*N bytes if N is negative, . ^If additional
2829** page cache memory is needed beyond what is provided by the initial
2830** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
2831** additional cache line. </dd>
2832**
2833** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
2834** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
2835** that SQLite will use for all of its dynamic memory allocation needs
2836** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
2837** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
2838** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
2839** [SQLITE_ERROR] if invoked otherwise.
2840** ^There are three arguments to SQLITE_CONFIG_HEAP:
2841** An 8-byte aligned pointer to the memory,
2842** the number of bytes in the memory buffer, and the minimum allocation size.
2843** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
2844** to using its default memory allocator (the system malloc() implementation),
2845** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
2846** memory pointer is not NULL then the alternative memory
2847** allocator is engaged to handle all of SQLites memory allocation needs.
2848** The first pointer (the memory pointer) must be aligned to an 8-byte
2849** boundary or subsequent behavior of SQLite will be undefined.
2850** The minimum allocation size is capped at 2**12. Reasonable values
2851** for the minimum allocation size are 2**5 through 2**8.</dd>
2852**
2853** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
2854** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
2855** pointer to an instance of the [sqlite3_mutex_methods] structure.
2856** The argument specifies alternative low-level mutex routines to be used
2857** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of
2858** the content of the [sqlite3_mutex_methods] structure before the call to
2859** [sqlite3_config()] returns. ^If SQLite is compiled with
2860** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2861** the entire mutexing subsystem is omitted from the build and hence calls to
2862** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
2863** return [SQLITE_ERROR].</dd>
2864**
2865** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
2866** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
2867** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The
2868** [sqlite3_mutex_methods]
2869** structure is filled with the currently defined mutex routines.)^
2870** This option can be used to overload the default mutex allocation
2871** routines with a wrapper used to track mutex usage for performance
2872** profiling or testing, for example. ^If SQLite is compiled with
2873** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2874** the entire mutexing subsystem is omitted from the build and hence calls to
2875** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
2876** return [SQLITE_ERROR].</dd>
2877**
2878** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
2879** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
2880** the default size of lookaside memory on each [database connection].
2881** The first argument is the
2882** size of each lookaside buffer slot and the second is the number of
2883** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE
2884** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2885** option to [sqlite3_db_config()] can be used to change the lookaside
2886** configuration on individual connections.)^ </dd>
2887**
2888** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2889** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
2890** a pointer to an [sqlite3_pcache_methods2] object. This object specifies
2891** the interface to a custom page cache implementation.)^
2892** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
2893**
2894** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2895** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
2896** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of
2897** the current page cache implementation into that object.)^ </dd>
2898**
2899** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2900** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
2901** global [error log].
2902** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2903** function with a call signature of void(*)(void*,int,const char*),
2904** and a pointer to void. ^If the function pointer is not NULL, it is
2905** invoked by [sqlite3_log()] to process each logging event. ^If the
2906** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2907** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
2908** passed through as the first parameter to the application-defined logger
2909** function whenever that function is invoked. ^The second parameter to
2910** the logger function is a copy of the first parameter to the corresponding
2911** [sqlite3_log()] call and is intended to be a [result code] or an
2912** [extended result code]. ^The third parameter passed to the logger is
2913** log message after formatting via [sqlite3_snprintf()].
2914** The SQLite logging interface is not reentrant; the logger function
2915** supplied by the application must not invoke any SQLite interface.
2916** In a multi-threaded application, the application-defined logger
2917** function must be threadsafe. </dd>
2918**
2919** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
2920** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
2921** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2922** then URI handling is globally disabled.)^ ^If URI handling is globally
2923** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
2924** [sqlite3_open16()] or
2925** specified as part of [ATTACH] commands are interpreted as URIs, regardless
2926** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
2927** connection is opened. ^If it is globally disabled, filenames are
2928** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2929** database connection is opened. ^(By default, URI handling is globally
2930** disabled. The default value may be changed by compiling with the
2931** [SQLITE_USE_URI] symbol defined.)^
2932**
2933** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
2934** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
2935** argument which is interpreted as a boolean in order to enable or disable
2936** the use of covering indices for full table scans in the query optimizer.
2937** ^The default setting is determined
2938** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2939** if that compile-time option is omitted.
2940** The ability to disable the use of covering indices for full table scans
2941** is because some incorrectly coded legacy applications might malfunction
2942** when the optimization is enabled. Providing the ability to
2943** disable the optimization allows the older, buggy application code to work
2944** without change even with newer versions of SQLite.
2945**
2946** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2947** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2948** <dd> These options are obsolete and should not be used by new code.
2949** They are retained for backwards compatibility but are now no-ops.
2950** </dd>
2951**
2952** [[SQLITE_CONFIG_SQLLOG]]
2953** <dt>SQLITE_CONFIG_SQLLOG
2954** <dd>This option is only available if sqlite is compiled with the
2955** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2956** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2957** The second should be of type (void*). The callback is invoked by the library
2958** in three separate circumstances, identified by the value passed as the
2959** fourth parameter. If the fourth parameter is 0, then the database connection
2960** passed as the second argument has just been opened. The third argument
2961** points to a buffer containing the name of the main database file. If the
2962** fourth parameter is 1, then the SQL statement that the third parameter
2963** points to has just been executed. Or, if the fourth parameter is 2, then
2964** the connection being passed as the second parameter is being closed. The
2965** third parameter is passed NULL In this case. An example of using this
2966** configuration option can be seen in the "test_sqllog.c" source file in
2967** the canonical SQLite source tree.</dd>
2968**
2969** [[SQLITE_CONFIG_MMAP_SIZE]]
2970** <dt>SQLITE_CONFIG_MMAP_SIZE
2971** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2972** that are the default mmap size limit (the default setting for
2973** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2974** ^The default setting can be overridden by each database connection using
2975** either the [PRAGMA mmap_size] command, or by using the
2976** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size
2977** will be silently truncated if necessary so that it does not exceed the
2978** compile-time maximum mmap size set by the
2979** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2980** ^If either argument to this option is negative, then that argument is
2981** changed to its compile-time default.
2982**
2983** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
2984** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
2985** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
2986** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2987** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2988** that specifies the maximum size of the created heap.
2989**
2990** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
2991** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
2992** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
2993** is a pointer to an integer and writes into that integer the number of extra
2994** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
2995** The amount of extra space required can change depending on the compiler,
2996** target platform, and SQLite version.
2997**
2998** [[SQLITE_CONFIG_PMASZ]]
2999** <dt>SQLITE_CONFIG_PMASZ
3000** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
3001** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
3002** sorter to that integer. The default minimum PMA Size is set by the
3003** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
3004** to help with sort operations when multithreaded sorting
3005** is enabled (using the [PRAGMA threads] command) and the amount of content
3006** to be sorted exceeds the page size times the minimum of the
3007** [PRAGMA cache_size] setting and this value.
3008**
3009** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
3010** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
3011** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
3012** becomes the [statement journal] spill-to-disk threshold.
3013** [Statement journals] are held in memory until their size (in bytes)
3014** exceeds this threshold, at which point they are written to disk.
3015** Or if the threshold is -1, statement journals are always held
3016** exclusively in memory.
3017** Since many statement journals never become large, setting the spill
3018** threshold to a value such as 64KiB can greatly reduce the amount of
3019** I/O required to support statement rollback.
3020** The default value for this setting is controlled by the
3021** [SQLITE_STMTJRNL_SPILL] compile-time option.
3022**
3023** [[SQLITE_CONFIG_SORTERREF_SIZE]]
3024** <dt>SQLITE_CONFIG_SORTERREF_SIZE
3025** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
3026** of type (int) - the new value of the sorter-reference size threshold.
3027** Usually, when SQLite uses an external sort to order records according
3028** to an ORDER BY clause, all fields required by the caller are present in the
3029** sorted records. However, if SQLite determines based on the declared type
3030** of a table column that its values are likely to be very large - larger
3031** than the configured sorter-reference size threshold - then a reference
3032** is stored in each sorted record and the required column values loaded
3033** from the database as records are returned in sorted order. The default
3034** value for this option is to never use this optimization. Specifying a
3035** negative value for this option restores the default behaviour.
3036** This option is only available if SQLite is compiled with the
3037** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
3038**
3039** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
3040** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
3041** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
3042** [sqlite3_int64] parameter which is the default maximum size for an in-memory
3043** database created using [sqlite3_deserialize()]. This default maximum
3044** size can be adjusted up or down for individual databases using the
3045** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
3046** configuration setting is never used, then the default maximum is determined
3047** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
3048** compile-time option is not set, then the default maximum is 1073741824.
3049** </dl>
3050*/
3051#define SQLITE_CONFIG_SINGLETHREAD1 1 /* nil */
3052#define SQLITE_CONFIG_MULTITHREAD2 2 /* nil */
3053#define SQLITE_CONFIG_SERIALIZED3 3 /* nil */
3054#define SQLITE_CONFIG_MALLOC4 4 /* sqlite3_mem_methods* */
3055#define SQLITE_CONFIG_GETMALLOC5 5 /* sqlite3_mem_methods* */
3056#define SQLITE_CONFIG_SCRATCH6 6 /* No longer used */
3057#define SQLITE_CONFIG_PAGECACHE7 7 /* void*, int sz, int N */
3058#define SQLITE_CONFIG_HEAP8 8 /* void*, int nByte, int min */
3059#define SQLITE_CONFIG_MEMSTATUS9 9 /* boolean */
3060#define SQLITE_CONFIG_MUTEX10 10 /* sqlite3_mutex_methods* */
3061#define SQLITE_CONFIG_GETMUTEX11 11 /* sqlite3_mutex_methods* */
3062/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
3063#define SQLITE_CONFIG_LOOKASIDE13 13 /* int int */
3064#define SQLITE_CONFIG_PCACHE14 14 /* no-op */
3065#define SQLITE_CONFIG_GETPCACHE15 15 /* no-op */
3066#define SQLITE_CONFIG_LOG16 16 /* xFunc, void* */
3067#define SQLITE_CONFIG_URI17 17 /* int */
3068#define SQLITE_CONFIG_PCACHE218 18 /* sqlite3_pcache_methods2* */
3069#define SQLITE_CONFIG_GETPCACHE219 19 /* sqlite3_pcache_methods2* */
3070#define SQLITE_CONFIG_COVERING_INDEX_SCAN20 20 /* int */
3071#define SQLITE_CONFIG_SQLLOG21 21 /* xSqllog, void* */
3072#define SQLITE_CONFIG_MMAP_SIZE22 22 /* sqlite3_int64, sqlite3_int64 */
3073#define SQLITE_CONFIG_WIN32_HEAPSIZE23 23 /* int nByte */
3074#define SQLITE_CONFIG_PCACHE_HDRSZ24 24 /* int *psz */
3075#define SQLITE_CONFIG_PMASZ25 25 /* unsigned int szPma */
3076#define SQLITE_CONFIG_STMTJRNL_SPILL26 26 /* int nByte */
3077#define SQLITE_CONFIG_SMALL_MALLOC27 27 /* boolean */
3078#define SQLITE_CONFIG_SORTERREF_SIZE28 28 /* int nByte */
3079#define SQLITE_CONFIG_MEMDB_MAXSIZE29 29 /* sqlite3_int64 */
3080
3081/*
3082** CAPI3REF: Database Connection Configuration Options
3083**
3084** These constants are the available integer configuration options that
3085** can be passed as the second argument to the [sqlite3_db_config()] interface.
3086**
3087** New configuration options may be added in future releases of SQLite.
3088** Existing configuration options might be discontinued. Applications
3089** should check the return code from [sqlite3_db_config()] to make sure that
3090** the call worked. ^The [sqlite3_db_config()] interface will return a
3091** non-zero [error code] if a discontinued or unsupported configuration option
3092** is invoked.
3093**
3094** <dl>
3095** [[SQLITE_DBCONFIG_LOOKASIDE]]
3096** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
3097** <dd> ^This option takes three additional arguments that determine the
3098** [lookaside memory allocator] configuration for the [database connection].
3099** ^The first argument (the third parameter to [sqlite3_db_config()] is a
3100** pointer to a memory buffer to use for lookaside memory.
3101** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
3102** may be NULL in which case SQLite will allocate the
3103** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
3104** size of each lookaside buffer slot. ^The third argument is the number of
3105** slots. The size of the buffer in the first argument must be greater than
3106** or equal to the product of the second and third arguments. The buffer
3107** must be aligned to an 8-byte boundary. ^If the second argument to
3108** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
3109** rounded down to the next smaller multiple of 8. ^(The lookaside memory
3110** configuration for a database connection can only be changed when that
3111** connection is not currently using lookaside memory, or in other words
3112** when the "current value" returned by
3113** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
3114** Any attempt to change the lookaside memory configuration when lookaside
3115** memory is in use leaves the configuration unchanged and returns
3116** [SQLITE_BUSY].)^</dd>
3117**
3118** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
3119** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
3120** <dd> ^This option is used to enable or disable the enforcement of
3121** [foreign key constraints]. There should be two additional arguments.
3122** The first argument is an integer which is 0 to disable FK enforcement,
3123** positive to enable FK enforcement or negative to leave FK enforcement
3124** unchanged. The second parameter is a pointer to an integer into which
3125** is written 0 or 1 to indicate whether FK enforcement is off or on
3126** following this call. The second parameter may be a NULL pointer, in
3127** which case the FK enforcement setting is not reported back. </dd>
3128**
3129** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
3130** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
3131** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
3132** There should be two additional arguments.
3133** The first argument is an integer which is 0 to disable triggers,
3134** positive to enable triggers or negative to leave the setting unchanged.
3135** The second parameter is a pointer to an integer into which
3136** is written 0 or 1 to indicate whether triggers are disabled or enabled
3137** following this call. The second parameter may be a NULL pointer, in
3138** which case the trigger setting is not reported back. </dd>
3139**
3140** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
3141** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
3142** <dd> ^This option is used to enable or disable the
3143** [fts3_tokenizer()] function which is part of the
3144** [FTS3] full-text search engine extension.
3145** There should be two additional arguments.
3146** The first argument is an integer which is 0 to disable fts3_tokenizer() or
3147** positive to enable fts3_tokenizer() or negative to leave the setting
3148** unchanged.
3149** The second parameter is a pointer to an integer into which
3150** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
3151** following this call. The second parameter may be a NULL pointer, in
3152** which case the new setting is not reported back. </dd>
3153**
3154** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
3155** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
3156** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
3157** interface independently of the [load_extension()] SQL function.
3158** The [sqlite3_enable_load_extension()] API enables or disables both the
3159** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
3160** There should be two additional arguments.
3161** When the first argument to this interface is 1, then only the C-API is
3162** enabled and the SQL function remains disabled. If the first argument to
3163** this interface is 0, then both the C-API and the SQL function are disabled.
3164** If the first argument is -1, then no changes are made to state of either the
3165** C-API or the SQL function.
3166** The second parameter is a pointer to an integer into which
3167** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
3168** is disabled or enabled following this call. The second parameter may
3169** be a NULL pointer, in which case the new setting is not reported back.
3170** </dd>
3171**
3172** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
3173** <dd> ^This option is used to change the name of the "main" database
3174** schema. ^The sole argument is a pointer to a constant UTF8 string
3175** which will become the new schema name in place of "main". ^SQLite
3176** does not make a copy of the new main schema name string, so the application
3177** must ensure that the argument passed into this DBCONFIG option is unchanged
3178** until after the database connection closes.
3179** </dd>
3180**
3181** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
3182** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
3183** <dd> Usually, when a database in wal mode is closed or detached from a
3184** database handle, SQLite checks if this will mean that there are now no
3185** connections at all to the database. If so, it performs a checkpoint
3186** operation before closing the connection. This option may be used to
3187** override this behaviour. The first parameter passed to this operation
3188** is an integer - positive to disable checkpoints-on-close, or zero (the
3189** default) to enable them, and negative to leave the setting unchanged.
3190** The second parameter is a pointer to an integer
3191** into which is written 0 or 1 to indicate whether checkpoints-on-close
3192** have been disabled - 0 if they are not disabled, 1 if they are.
3193** </dd>
3194**
3195** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
3196** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
3197** the [query planner stability guarantee] (QPSG). When the QPSG is active,
3198** a single SQL query statement will always use the same algorithm regardless
3199** of values of [bound parameters].)^ The QPSG disables some query optimizations
3200** that look at the values of bound parameters, which can make some queries
3201** slower. But the QPSG has the advantage of more predictable behavior. With
3202** the QPSG active, SQLite will always use the same query plan in the field as
3203** was used during testing in the lab.
3204** The first argument to this setting is an integer which is 0 to disable
3205** the QPSG, positive to enable QPSG, or negative to leave the setting
3206** unchanged. The second parameter is a pointer to an integer into which
3207** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
3208** following this call.
3209** </dd>
3210**
3211** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
3212** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
3213** include output for any operations performed by trigger programs. This
3214** option is used to set or clear (the default) a flag that governs this
3215** behavior. The first parameter passed to this operation is an integer -
3216** positive to enable output for trigger programs, or zero to disable it,
3217** or negative to leave the setting unchanged.
3218** The second parameter is a pointer to an integer into which is written
3219** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
3220** it is not disabled, 1 if it is.
3221** </dd>
3222**
3223** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
3224** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
3225** [VACUUM] in order to reset a database back to an empty database
3226** with no schema and no content. The following process works even for
3227** a badly corrupted database file:
3228** <ol>
3229** <li> If the database connection is newly opened, make sure it has read the
3230** database schema by preparing then discarding some query against the
3231** database, or calling sqlite3_table_column_metadata(), ignoring any
3232** errors. This step is only necessary if the application desires to keep
3233** the database in WAL mode after the reset if it was in WAL mode before
3234** the reset.
3235** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
3236** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
3237** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
3238** </ol>
3239** Because resetting a database is destructive and irreversible, the
3240** process requires the use of this obscure API and multiple steps to help
3241** ensure that it does not happen by accident.
3242**
3243** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
3244** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
3245** "defensive" flag for a database connection. When the defensive
3246** flag is enabled, language features that allow ordinary SQL to
3247** deliberately corrupt the database file are disabled. The disabled
3248** features include but are not limited to the following:
3249** <ul>
3250** <li> The [PRAGMA writable_schema=ON] statement.
3251** <li> The [PRAGMA journal_mode=OFF] statement.
3252** <li> Writes to the [sqlite_dbpage] virtual table.
3253** <li> Direct writes to [shadow tables].
3254** </ul>
3255** </dd>
3256**
3257** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
3258** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
3259** "writable_schema" flag. This has the same effect and is logically equivalent
3260** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
3261** The first argument to this setting is an integer which is 0 to disable
3262** the writable_schema, positive to enable writable_schema, or negative to
3263** leave the setting unchanged. The second parameter is a pointer to an
3264** integer into which is written 0 or 1 to indicate whether the writable_schema
3265** is enabled or disabled following this call.
3266** </dd>
3267**
3268** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]]
3269** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt>
3270** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates
3271** the legacy behavior of the [ALTER TABLE RENAME] command such it
3272** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
3273** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for
3274** additional information. This feature can also be turned on and off
3275** using the [PRAGMA legacy_alter_table] statement.
3276** </dd>
3277**
3278** [[SQLITE_DBCONFIG_DQS_DML]]
3279** <dt>SQLITE_DBCONFIG_DQS_DML</td>
3280** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates
3281** the legacy [double-quoted string literal] misfeature for DML statement
3282** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The
3283** default value of this setting is determined by the [-DSQLITE_DQS]
3284** compile-time option.
3285** </dd>
3286**
3287** [[SQLITE_DBCONFIG_DQS_DDL]]
3288** <dt>SQLITE_DBCONFIG_DQS_DDL</td>
3289** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates
3290** the legacy [double-quoted string literal] misfeature for DDL statements,
3291** such as CREATE TABLE and CREATE INDEX. The
3292** default value of this setting is determined by the [-DSQLITE_DQS]
3293** compile-time option.
3294** </dd>
3295** </dl>
3296*/
3297#define SQLITE_DBCONFIG_MAINDBNAME1000 1000 /* const char* */
3298#define SQLITE_DBCONFIG_LOOKASIDE1001 1001 /* void* int int */
3299#define SQLITE_DBCONFIG_ENABLE_FKEY1002 1002 /* int int* */
3300#define SQLITE_DBCONFIG_ENABLE_TRIGGER1003 1003 /* int int* */
3301#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004 1004 /* int int* */
3302#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION1005 1005 /* int int* */
3303#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE1006 1006 /* int int* */
3304#define SQLITE_DBCONFIG_ENABLE_QPSG1007 1007 /* int int* */
3305#define SQLITE_DBCONFIG_TRIGGER_EQP1008 1008 /* int int* */
3306#define SQLITE_DBCONFIG_RESET_DATABASE1009 1009 /* int int* */
3307#define SQLITE_DBCONFIG_DEFENSIVE1010 1010 /* int int* */
3308#define SQLITE_DBCONFIG_WRITABLE_SCHEMA1011 1011 /* int int* */
3309#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE1012 1012 /* int int* */
3310#define SQLITE_DBCONFIG_DQS_DML1013 1013 /* int int* */
3311#define SQLITE_DBCONFIG_DQS_DDL1014 1014 /* int int* */
3312#define SQLITE_DBCONFIG_MAX1014 1014 /* Largest DBCONFIG */
3313
3314/*
3315** CAPI3REF: Enable Or Disable Extended Result Codes
3316** METHOD: sqlite3
3317**
3318** ^The sqlite3_extended_result_codes() routine enables or disables the
3319** [extended result codes] feature of SQLite. ^The extended result
3320** codes are disabled by default for historical compatibility.
3321*/
3322SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
3323
3324/*
3325** CAPI3REF: Last Insert Rowid
3326** METHOD: sqlite3
3327**
3328** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
3329** has a unique 64-bit signed
3330** integer key called the [ROWID | "rowid"]. ^The rowid is always available
3331** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
3332** names are not also used by explicitly declared columns. ^If
3333** the table has a column of type [INTEGER PRIMARY KEY] then that column
3334** is another alias for the rowid.
3335**
3336** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
3337** the most recent successful [INSERT] into a rowid table or [virtual table]
3338** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
3339** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
3340** on the database connection D, then sqlite3_last_insert_rowid(D) returns
3341** zero.
3342**
3343** As well as being set automatically as rows are inserted into database
3344** tables, the value returned by this function may be set explicitly by
3345** [sqlite3_set_last_insert_rowid()]
3346**
3347** Some virtual table implementations may INSERT rows into rowid tables as
3348** part of committing a transaction (e.g. to flush data accumulated in memory
3349** to disk). In this case subsequent calls to this function return the rowid
3350** associated with these internal INSERT operations, which leads to
3351** unintuitive results. Virtual table implementations that do write to rowid
3352** tables in this way can avoid this problem by restoring the original
3353** rowid value using [sqlite3_set_last_insert_rowid()] before returning
3354** control to the user.
3355**
3356** ^(If an [INSERT] occurs within a trigger then this routine will
3357** return the [rowid] of the inserted row as long as the trigger is
3358** running. Once the trigger program ends, the value returned
3359** by this routine reverts to what it was before the trigger was fired.)^
3360**
3361** ^An [INSERT] that fails due to a constraint violation is not a
3362** successful [INSERT] and does not change the value returned by this
3363** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
3364** and INSERT OR ABORT make no changes to the return value of this
3365** routine when their insertion fails. ^(When INSERT OR REPLACE
3366** encounters a constraint violation, it does not fail. The
3367** INSERT continues to completion after deleting rows that caused
3368** the constraint problem so INSERT OR REPLACE will always change
3369** the return value of this interface.)^
3370**
3371** ^For the purposes of this routine, an [INSERT] is considered to
3372** be successful even if it is subsequently rolled back.
3373**
3374** This function is accessible to SQL statements via the
3375** [last_insert_rowid() SQL function].
3376**
3377** If a separate thread performs a new [INSERT] on the same
3378** database connection while the [sqlite3_last_insert_rowid()]
3379** function is running and thus changes the last insert [rowid],
3380** then the value returned by [sqlite3_last_insert_rowid()] is
3381** unpredictable and might not equal either the old or the new
3382** last insert [rowid].
3383*/
3384SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
3385
3386/*
3387** CAPI3REF: Set the Last Insert Rowid value.
3388** METHOD: sqlite3
3389**
3390** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
3391** set the value returned by calling sqlite3_last_insert_rowid(D) to R
3392** without inserting a row into the database.
3393*/
3394SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
3395
3396/*
3397** CAPI3REF: Count The Number Of Rows Modified
3398** METHOD: sqlite3
3399**
3400** ^This function returns the number of rows modified, inserted or
3401** deleted by the most recently completed INSERT, UPDATE or DELETE
3402** statement on the database connection specified by the only parameter.
3403** ^Executing any other type of SQL statement does not modify the value
3404** returned by this function.
3405**
3406** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
3407** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3408** [foreign key actions] or [REPLACE] constraint resolution are not counted.
3409**
3410** Changes to a view that are intercepted by
3411** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
3412** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
3413** DELETE statement run on a view is always zero. Only changes made to real
3414** tables are counted.
3415**
3416** Things are more complicated if the sqlite3_changes() function is
3417** executed while a trigger program is running. This may happen if the
3418** program uses the [changes() SQL function], or if some other callback
3419** function invokes sqlite3_changes() directly. Essentially:
3420**
3421** <ul>
3422** <li> ^(Before entering a trigger program the value returned by
3423** sqlite3_changes() function is saved. After the trigger program
3424** has finished, the original value is restored.)^
3425**
3426** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
3427** statement sets the value returned by sqlite3_changes()
3428** upon completion as normal. Of course, this value will not include
3429** any changes performed by sub-triggers, as the sqlite3_changes()
3430** value will be saved and restored after each sub-trigger has run.)^
3431** </ul>
3432**
3433** ^This means that if the changes() SQL function (or similar) is used
3434** by the first INSERT, UPDATE or DELETE statement within a trigger, it
3435** returns the value as set when the calling statement began executing.
3436** ^If it is used by the second or subsequent such statement within a trigger
3437** program, the value returned reflects the number of rows modified by the
3438** previous INSERT, UPDATE or DELETE statement within the same trigger.
3439**
3440** If a separate thread makes changes on the same database connection
3441** while [sqlite3_changes()] is running then the value returned
3442** is unpredictable and not meaningful.
3443**
3444** See also:
3445** <ul>
3446** <li> the [sqlite3_total_changes()] interface
3447** <li> the [count_changes pragma]
3448** <li> the [changes() SQL function]
3449** <li> the [data_version pragma]
3450** </ul>
3451*/
3452SQLITE_API int sqlite3_changes(sqlite3*);
3453
3454/*
3455** CAPI3REF: Total Number Of Rows Modified
3456** METHOD: sqlite3
3457**
3458** ^This function returns the total number of rows inserted, modified or
3459** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
3460** since the database connection was opened, including those executed as
3461** part of trigger programs. ^Executing any other type of SQL statement
3462** does not affect the value returned by sqlite3_total_changes().
3463**
3464** ^Changes made as part of [foreign key actions] are included in the
3465** count, but those made as part of REPLACE constraint resolution are
3466** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
3467** are not counted.
3468**
3469** The [sqlite3_total_changes(D)] interface only reports the number
3470** of rows that changed due to SQL statement run against database
3471** connection D. Any changes by other database connections are ignored.
3472** To detect changes against a database file from other database
3473** connections use the [PRAGMA data_version] command or the
3474** [SQLITE_FCNTL_DATA_VERSION] [file control].
3475**
3476** If a separate thread makes changes on the same database connection
3477** while [sqlite3_total_changes()] is running then the value
3478** returned is unpredictable and not meaningful.
3479**
3480** See also:
3481** <ul>
3482** <li> the [sqlite3_changes()] interface
3483** <li> the [count_changes pragma]
3484** <li> the [changes() SQL function]
3485** <li> the [data_version pragma]
3486** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
3487** </ul>
3488*/
3489SQLITE_API int sqlite3_total_changes(sqlite3*);
3490
3491/*
3492** CAPI3REF: Interrupt A Long-Running Query
3493** METHOD: sqlite3
3494**
3495** ^This function causes any pending database operation to abort and
3496** return at its earliest opportunity. This routine is typically
3497** called in response to a user action such as pressing "Cancel"
3498** or Ctrl-C where the user wants a long query operation to halt
3499** immediately.
3500**
3501** ^It is safe to call this routine from a thread different from the
3502** thread that is currently running the database operation. But it
3503** is not safe to call this routine with a [database connection] that
3504** is closed or might close before sqlite3_interrupt() returns.
3505**
3506** ^If an SQL operation is very nearly finished at the time when
3507** sqlite3_interrupt() is called, then it might not have an opportunity
3508** to be interrupted and might continue to completion.
3509**
3510** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
3511** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
3512** that is inside an explicit transaction, then the entire transaction
3513** will be rolled back automatically.
3514**
3515** ^The sqlite3_interrupt(D) call is in effect until all currently running
3516** SQL statements on [database connection] D complete. ^Any new SQL statements
3517** that are started after the sqlite3_interrupt() call and before the
3518** running statements reaches zero are interrupted as if they had been
3519** running prior to the sqlite3_interrupt() call. ^New SQL statements
3520** that are started after the running statement count reaches zero are
3521** not effected by the sqlite3_interrupt().
3522** ^A call to sqlite3_interrupt(D) that occurs when there are no running
3523** SQL statements is a no-op and has no effect on SQL statements
3524** that are started after the sqlite3_interrupt() call returns.
3525*/
3526SQLITE_API void sqlite3_interrupt(sqlite3*);
3527
3528/*
3529** CAPI3REF: Determine If An SQL Statement Is Complete
3530**
3531** These routines are useful during command-line input to determine if the
3532** currently entered text seems to form a complete SQL statement or
3533** if additional input is needed before sending the text into
3534** SQLite for parsing. ^These routines return 1 if the input string
3535** appears to be a complete SQL statement. ^A statement is judged to be
3536** complete if it ends with a semicolon token and is not a prefix of a
3537** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3538** string literals or quoted identifier names or comments are not
3539** independent tokens (they are part of the token in which they are
3540** embedded) and thus do not count as a statement terminator. ^Whitespace
3541** and comments that follow the final semicolon are ignored.
3542**
3543** ^These routines return 0 if the statement is incomplete. ^If a
3544** memory allocation fails, then SQLITE_NOMEM is returned.
3545**
3546** ^These routines do not parse the SQL statements thus
3547** will not detect syntactically incorrect SQL.
3548**
3549** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
3550** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
3551** automatically by sqlite3_complete16(). If that initialization fails,
3552** then the return value from sqlite3_complete16() will be non-zero
3553** regardless of whether or not the input SQL is complete.)^
3554**
3555** The input to [sqlite3_complete()] must be a zero-terminated
3556** UTF-8 string.
3557**
3558** The input to [sqlite3_complete16()] must be a zero-terminated
3559** UTF-16 string in native byte order.
3560*/
3561SQLITE_API int sqlite3_complete(const char *sql);
3562SQLITE_API int sqlite3_complete16(const void *sql);
3563
3564/*
3565** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
3566** KEYWORDS: {busy-handler callback} {busy handler}
3567** METHOD: sqlite3
3568**
3569** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
3570** that might be invoked with argument P whenever
3571** an attempt is made to access a database table associated with
3572** [database connection] D when another thread
3573** or process has the table locked.
3574** The sqlite3_busy_handler() interface is used to implement
3575** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
3576**
3577** ^If the busy callback is NULL, then [SQLITE_BUSY]
3578** is returned immediately upon encountering the lock. ^If the busy callback
3579** is not NULL, then the callback might be invoked with two arguments.
3580**
3581** ^The first argument to the busy handler is a copy of the void* pointer which
3582** is the third argument to sqlite3_busy_handler(). ^The second argument to
3583** the busy handler callback is the number of times that the busy handler has
3584** been invoked previously for the same locking event. ^If the
3585** busy callback returns 0, then no additional attempts are made to
3586** access the database and [SQLITE_BUSY] is returned
3587** to the application.
3588** ^If the callback returns non-zero, then another attempt
3589** is made to access the database and the cycle repeats.
3590**
3591** The presence of a busy handler does not guarantee that it will be invoked
3592** when there is lock contention. ^If SQLite determines that invoking the busy
3593** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
3594** to the application instead of invoking the
3595** busy handler.
3596** Consider a scenario where one process is holding a read lock that
3597** it is trying to promote to a reserved lock and
3598** a second process is holding a reserved lock that it is trying
3599** to promote to an exclusive lock. The first process cannot proceed
3600** because it is blocked by the second and the second process cannot
3601** proceed because it is blocked by the first. If both processes
3602** invoke the busy handlers, neither will make any progress. Therefore,
3603** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
3604** will induce the first process to release its read lock and allow
3605** the second process to proceed.
3606**
3607** ^The default busy callback is NULL.
3608**
3609** ^(There can only be a single busy handler defined for each
3610** [database connection]. Setting a new busy handler clears any
3611** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
3612** or evaluating [PRAGMA busy_timeout=N] will change the
3613** busy handler and thus clear any previously set busy handler.
3614**
3615** The busy callback should not take any actions which modify the
3616** database connection that invoked the busy handler. In other words,
3617** the busy handler is not reentrant. Any such actions
3618** result in undefined behavior.
3619**
3620** A busy handler must not close the database connection
3621** or [prepared statement] that invoked the busy handler.
3622*/
3623SQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
3624
3625/*
3626** CAPI3REF: Set A Busy Timeout
3627** METHOD: sqlite3
3628**
3629** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
3630** for a specified amount of time when a table is locked. ^The handler
3631** will sleep multiple times until at least "ms" milliseconds of sleeping
3632** have accumulated. ^After at least "ms" milliseconds of sleeping,
3633** the handler returns 0 which causes [sqlite3_step()] to return
3634** [SQLITE_BUSY].
3635**
3636** ^Calling this routine with an argument less than or equal to zero
3637** turns off all busy handlers.
3638**
3639** ^(There can only be a single busy handler for a particular
3640** [database connection] at any given moment. If another busy handler
3641** was defined (using [sqlite3_busy_handler()]) prior to calling
3642** this routine, that other busy handler is cleared.)^
3643**
3644** See also: [PRAGMA busy_timeout]
3645*/
3646SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3647
3648/*
3649** CAPI3REF: Convenience Routines For Running Queries
3650** METHOD: sqlite3
3651**
3652** This is a legacy interface that is preserved for backwards compatibility.
3653** Use of this interface is not recommended.
3654**
3655** Definition: A <b>result table</b> is memory data structure created by the
3656** [sqlite3_get_table()] interface. A result table records the
3657** complete query results from one or more queries.
3658**
3659** The table conceptually has a number of rows and columns. But
3660** these numbers are not part of the result table itself. These
3661** numbers are obtained separately. Let N be the number of rows
3662** and M be the number of columns.
3663**
3664** A result table is an array of pointers to zero-terminated UTF-8 strings.
3665** There are (N+1)*M elements in the array. The first M pointers point
3666** to zero-terminated strings that contain the names of the columns.
3667** The remaining entries all point to query results. NULL values result
3668** in NULL pointers. All other values are in their UTF-8 zero-terminated
3669** string representation as returned by [sqlite3_column_text()].
3670**
3671** A result table might consist of one or more memory allocations.
3672** It is not safe to pass a result table directly to [sqlite3_free()].
3673** A result table should be deallocated using [sqlite3_free_table()].
3674**
3675** ^(As an example of the result table format, suppose a query result
3676** is as follows:
3677**
3678** <blockquote><pre>
3679** Name | Age
3680** -----------------------
3681** Alice | 43
3682** Bob | 28
3683** Cindy | 21
3684** </pre></blockquote>
3685**
3686** There are two column (M==2) and three rows (N==3). Thus the
3687** result table has 8 entries. Suppose the result table is stored
3688** in an array names azResult. Then azResult holds this content:
3689**
3690** <blockquote><pre>
3691** azResult&#91;0] = "Name";
3692** azResult&#91;1] = "Age";
3693** azResult&#91;2] = "Alice";
3694** azResult&#91;3] = "43";
3695** azResult&#91;4] = "Bob";
3696** azResult&#91;5] = "28";
3697** azResult&#91;6] = "Cindy";
3698** azResult&#91;7] = "21";
3699** </pre></blockquote>)^
3700**
3701** ^The sqlite3_get_table() function evaluates one or more
3702** semicolon-separated SQL statements in the zero-terminated UTF-8
3703** string of its 2nd parameter and returns a result table to the
3704** pointer given in its 3rd parameter.
3705**
3706** After the application has finished with the result from sqlite3_get_table(),
3707** it must pass the result table pointer to sqlite3_free_table() in order to
3708** release the memory that was malloced. Because of the way the
3709** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
3710** function must not try to call [sqlite3_free()] directly. Only
3711** [sqlite3_free_table()] is able to release the memory properly and safely.
3712**
3713** The sqlite3_get_table() interface is implemented as a wrapper around
3714** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
3715** to any internal data structures of SQLite. It uses only the public
3716** interface defined here. As a consequence, errors that occur in the
3717** wrapper layer outside of the internal [sqlite3_exec()] call are not
3718** reflected in subsequent calls to [sqlite3_errcode()] or
3719** [sqlite3_errmsg()].
3720*/
3721SQLITE_API int sqlite3_get_table(
3722 sqlite3 *db, /* An open database */
3723 const char *zSql, /* SQL to be evaluated */
3724 char ***pazResult, /* Results of the query */
3725 int *pnRow, /* Number of result rows written here */
3726 int *pnColumn, /* Number of result columns written here */
3727 char **pzErrmsg /* Error msg written here */
3728);
3729SQLITE_API void sqlite3_free_table(char **result);
3730
3731/*
3732** CAPI3REF: Formatted String Printing Functions
3733**
3734** These routines are work-alikes of the "printf()" family of functions
3735** from the standard C library.
3736** These routines understand most of the common formatting options from
3737** the standard library printf()
3738** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3739** See the [built-in printf()] documentation for details.
3740**
3741** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
3742** results into memory obtained from [sqlite3_malloc64()].
3743** The strings returned by these two routines should be
3744** released by [sqlite3_free()]. ^Both routines return a
3745** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
3746** memory to hold the resulting string.
3747**
3748** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
3749** the standard C library. The result is written into the
3750** buffer supplied as the second parameter whose size is given by
3751** the first parameter. Note that the order of the
3752** first two parameters is reversed from snprintf().)^ This is an
3753** historical accident that cannot be fixed without breaking
3754** backwards compatibility. ^(Note also that sqlite3_snprintf()
3755** returns a pointer to its buffer instead of the number of
3756** characters actually written into the buffer.)^ We admit that
3757** the number of characters written would be a more useful return
3758** value but we cannot change the implementation of sqlite3_snprintf()
3759** now without breaking compatibility.
3760**
3761** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
3762** guarantees that the buffer is always zero-terminated. ^The first
3763** parameter "n" is the total size of the buffer, including space for
3764** the zero terminator. So the longest string that can be completely
3765** written will be n-1 characters.
3766**
3767** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
3768**
3769** See also: [built-in printf()], [printf() SQL function]
3770*/
3771SQLITE_API char *sqlite3_mprintf(const char*,...);
3772SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
3773SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
3774SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
3775
3776/*
3777** CAPI3REF: Memory Allocation Subsystem
3778**
3779** The SQLite core uses these three routines for all of its own
3780** internal memory allocation needs. "Core" in the previous sentence
3781** does not include operating-system specific VFS implementation. The
3782** Windows VFS uses native malloc() and free() for some operations.
3783**
3784** ^The sqlite3_malloc() routine returns a pointer to a block
3785** of memory at least N bytes in length, where N is the parameter.
3786** ^If sqlite3_malloc() is unable to obtain sufficient free
3787** memory, it returns a NULL pointer. ^If the parameter N to
3788** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
3789** a NULL pointer.
3790**
3791** ^The sqlite3_malloc64(N) routine works just like
3792** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3793** of a signed 32-bit integer.
3794**
3795** ^Calling sqlite3_free() with a pointer previously returned
3796** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
3797** that it might be reused. ^The sqlite3_free() routine is
3798** a no-op if is called with a NULL pointer. Passing a NULL pointer
3799** to sqlite3_free() is harmless. After being freed, memory
3800** should neither be read nor written. Even reading previously freed
3801** memory might result in a segmentation fault or other severe error.
3802** Memory corruption, a segmentation fault, or other severe error
3803** might result if sqlite3_free() is called with a non-NULL pointer that
3804** was not obtained from sqlite3_malloc() or sqlite3_realloc().
3805**
3806** ^The sqlite3_realloc(X,N) interface attempts to resize a
3807** prior memory allocation X to be at least N bytes.
3808** ^If the X parameter to sqlite3_realloc(X,N)
3809** is a NULL pointer then its behavior is identical to calling
3810** sqlite3_malloc(N).
3811** ^If the N parameter to sqlite3_realloc(X,N) is zero or
3812** negative then the behavior is exactly the same as calling
3813** sqlite3_free(X).
3814** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
3815** of at least N bytes in size or NULL if insufficient memory is available.
3816** ^If M is the size of the prior allocation, then min(N,M) bytes
3817** of the prior allocation are copied into the beginning of buffer returned
3818** by sqlite3_realloc(X,N) and the prior allocation is freed.
3819** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
3820** prior allocation is not freed.
3821**
3822** ^The sqlite3_realloc64(X,N) interfaces works the same as
3823** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3824** of a 32-bit signed integer.
3825**
3826** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
3827** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
3828** sqlite3_msize(X) returns the size of that memory allocation in bytes.
3829** ^The value returned by sqlite3_msize(X) might be larger than the number
3830** of bytes requested when X was allocated. ^If X is a NULL pointer then
3831** sqlite3_msize(X) returns zero. If X points to something that is not
3832** the beginning of memory allocation, or if it points to a formerly
3833** valid memory allocation that has now been freed, then the behavior
3834** of sqlite3_msize(X) is undefined and possibly harmful.
3835**
3836** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
3837** sqlite3_malloc64(), and sqlite3_realloc64()
3838** is always aligned to at least an 8 byte boundary, or to a
3839** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3840** option is used.
3841**
3842** In SQLite version 3.5.0 and 3.5.1, it was possible to define
3843** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
3844** implementation of these routines to be omitted. That capability
3845** is no longer provided. Only built-in memory allocators can be used.
3846**
3847** Prior to SQLite version 3.7.10, the Windows OS interface layer called
3848** the system malloc() and free() directly when converting
3849** filenames between the UTF-8 encoding used by SQLite
3850** and whatever filename encoding is used by the particular Windows
3851** installation. Memory allocation errors were detected, but
3852** they were reported back as [SQLITE_CANTOPEN] or
3853** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
3854**
3855** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
3856** must be either NULL or else pointers obtained from a prior
3857** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
3858** not yet been released.
3859**
3860** The application must not read or write any part of
3861** a block of memory after it has been released using
3862** [sqlite3_free()] or [sqlite3_realloc()].
3863*/
3864SQLITE_API void *sqlite3_malloc(int);
3865SQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
3866SQLITE_API void *sqlite3_realloc(void*, int);
3867SQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
3868SQLITE_API void sqlite3_free(void*);
3869SQLITE_API sqlite3_uint64 sqlite3_msize(void*);
3870
3871/*
3872** CAPI3REF: Memory Allocator Statistics
3873**
3874** SQLite provides these two interfaces for reporting on the status
3875** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
3876** routines, which form the built-in memory allocation subsystem.
3877**
3878** ^The [sqlite3_memory_used()] routine returns the number of bytes
3879** of memory currently outstanding (malloced but not freed).
3880** ^The [sqlite3_memory_highwater()] routine returns the maximum
3881** value of [sqlite3_memory_used()] since the high-water mark
3882** was last reset. ^The values returned by [sqlite3_memory_used()] and
3883** [sqlite3_memory_highwater()] include any overhead
3884** added by SQLite in its implementation of [sqlite3_malloc()],
3885** but not overhead added by the any underlying system library
3886** routines that [sqlite3_malloc()] may call.
3887**
3888** ^The memory high-water mark is reset to the current value of
3889** [sqlite3_memory_used()] if and only if the parameter to
3890** [sqlite3_memory_highwater()] is true. ^The value returned
3891** by [sqlite3_memory_highwater(1)] is the high-water mark
3892** prior to the reset.
3893*/
3894SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
3895SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
3896
3897/*
3898** CAPI3REF: Pseudo-Random Number Generator
3899**
3900** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3901** select random [ROWID | ROWIDs] when inserting new records into a table that
3902** already uses the largest possible [ROWID]. The PRNG is also used for
3903** the build-in random() and randomblob() SQL functions. This interface allows
3904** applications to access the same PRNG for other purposes.
3905**
3906** ^A call to this routine stores N bytes of randomness into buffer P.
3907** ^The P parameter can be a NULL pointer.
3908**
3909** ^If this routine has not been previously called or if the previous
3910** call had N less than one or a NULL pointer for P, then the PRNG is
3911** seeded using randomness obtained from the xRandomness method of
3912** the default [sqlite3_vfs] object.
3913** ^If the previous call to this routine had an N of 1 or more and a
3914** non-NULL P then the pseudo-randomness is generated
3915** internally and without recourse to the [sqlite3_vfs] xRandomness
3916** method.
3917*/
3918SQLITE_API void sqlite3_randomness(int N, void *P);
3919
3920/*
3921** CAPI3REF: Compile-Time Authorization Callbacks
3922** METHOD: sqlite3
3923** KEYWORDS: {authorizer callback}
3924**
3925** ^This routine registers an authorizer callback with a particular
3926** [database connection], supplied in the first argument.
3927** ^The authorizer callback is invoked as SQL statements are being compiled
3928** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
3929** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
3930** and [sqlite3_prepare16_v3()]. ^At various
3931** points during the compilation process, as logic is being created
3932** to perform various actions, the authorizer callback is invoked to
3933** see if those actions are allowed. ^The authorizer callback should
3934** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
3935** specific action but allow the SQL statement to continue to be
3936** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
3937** rejected with an error. ^If the authorizer callback returns
3938** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
3939** then the [sqlite3_prepare_v2()] or equivalent call that triggered
3940** the authorizer will fail with an error message.
3941**
3942** When the callback returns [SQLITE_OK], that means the operation
3943** requested is ok. ^When the callback returns [SQLITE_DENY], the
3944** [sqlite3_prepare_v2()] or equivalent call that triggered the
3945** authorizer will fail with an error message explaining that
3946** access is denied.
3947**
3948** ^The first parameter to the authorizer callback is a copy of the third
3949** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
3950** to the callback is an integer [SQLITE_COPY | action code] that specifies
3951** the particular action to be authorized. ^The third through sixth parameters
3952** to the callback are either NULL pointers or zero-terminated strings
3953** that contain additional details about the action to be authorized.
3954** Applications must always be prepared to encounter a NULL pointer in any
3955** of the third through the sixth parameters of the authorization callback.
3956**
3957** ^If the action code is [SQLITE_READ]
3958** and the callback returns [SQLITE_IGNORE] then the
3959** [prepared statement] statement is constructed to substitute
3960** a NULL value in place of the table column that would have
3961** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
3962** return can be used to deny an untrusted user access to individual
3963** columns of a table.
3964** ^When a table is referenced by a [SELECT] but no column values are
3965** extracted from that table (for example in a query like
3966** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
3967** is invoked once for that table with a column name that is an empty string.
3968** ^If the action code is [SQLITE_DELETE] and the callback returns
3969** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
3970** [truncate optimization] is disabled and all rows are deleted individually.
3971**
3972** An authorizer is used when [sqlite3_prepare | preparing]
3973** SQL statements from an untrusted source, to ensure that the SQL statements
3974** do not try to access data they are not allowed to see, or that they do not
3975** try to execute malicious statements that damage the database. For
3976** example, an application may allow a user to enter arbitrary
3977** SQL queries for evaluation by a database. But the application does
3978** not want the user to be able to make arbitrary changes to the
3979** database. An authorizer could then be put in place while the
3980** user-entered SQL is being [sqlite3_prepare | prepared] that
3981** disallows everything except [SELECT] statements.
3982**
3983** Applications that need to process SQL from untrusted sources
3984** might also consider lowering resource limits using [sqlite3_limit()]
3985** and limiting database size using the [max_page_count] [PRAGMA]
3986** in addition to using an authorizer.
3987**
3988** ^(Only a single authorizer can be in place on a database connection
3989** at a time. Each call to sqlite3_set_authorizer overrides the
3990** previous call.)^ ^Disable the authorizer by installing a NULL callback.
3991** The authorizer is disabled by default.
3992**
3993** The authorizer callback must not do anything that will modify
3994** the database connection that invoked the authorizer callback.
3995** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3996** database connections for the meaning of "modify" in this paragraph.
3997**
3998** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3999** statement might be re-prepared during [sqlite3_step()] due to a
4000** schema change. Hence, the application should ensure that the
4001** correct authorizer callback remains in place during the [sqlite3_step()].
4002**
4003** ^Note that the authorizer callback is invoked only during
4004** [sqlite3_prepare()] or its variants. Authorization is not
4005** performed during statement evaluation in [sqlite3_step()], unless
4006** as stated in the previous paragraph, sqlite3_step() invokes
4007** sqlite3_prepare_v2() to reprepare a statement after a schema change.
4008*/
4009SQLITE_API int sqlite3_set_authorizer(
4010 sqlite3*,
4011 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
4012 void *pUserData
4013);
4014
4015/*
4016** CAPI3REF: Authorizer Return Codes
4017**
4018** The [sqlite3_set_authorizer | authorizer callback function] must
4019** return either [SQLITE_OK] or one of these two constants in order
4020** to signal SQLite whether or not the action is permitted. See the
4021** [sqlite3_set_authorizer | authorizer documentation] for additional
4022** information.
4023**
4024** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
4025** returned from the [sqlite3_vtab_on_conflict()] interface.
4026*/
4027#define SQLITE_DENY1 1 /* Abort the SQL statement with an error */
4028#define SQLITE_IGNORE2 2 /* Don't allow access, but don't generate an error */
4029
4030/*
4031** CAPI3REF: Authorizer Action Codes
4032**
4033** The [sqlite3_set_authorizer()] interface registers a callback function
4034** that is invoked to authorize certain SQL statement actions. The
4035** second parameter to the callback is an integer code that specifies
4036** what action is being authorized. These are the integer action codes that
4037** the authorizer callback may be passed.
4038**
4039** These action code values signify what kind of operation is to be
4040** authorized. The 3rd and 4th parameters to the authorization
4041** callback function will be parameters or NULL depending on which of these
4042** codes is used as the second parameter. ^(The 5th parameter to the
4043** authorizer callback is the name of the database ("main", "temp",
4044** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
4045** is the name of the inner-most trigger or view that is responsible for
4046** the access attempt or NULL if this access attempt is directly from
4047** top-level SQL code.
4048*/
4049/******************************************* 3rd ************ 4th ***********/
4050#define SQLITE_CREATE_INDEX1 1 /* Index Name Table Name */
4051#define SQLITE_CREATE_TABLE2 2 /* Table Name NULL */
4052#define SQLITE_CREATE_TEMP_INDEX3 3 /* Index Name Table Name */
4053#define SQLITE_CREATE_TEMP_TABLE4 4 /* Table Name NULL */
4054#define SQLITE_CREATE_TEMP_TRIGGER5 5 /* Trigger Name Table Name */
4055#define SQLITE_CREATE_TEMP_VIEW6 6 /* View Name NULL */
4056#define SQLITE_CREATE_TRIGGER7 7 /* Trigger Name Table Name */
4057#define SQLITE_CREATE_VIEW8 8 /* View Name NULL */
4058#define SQLITE_DELETE9 9 /* Table Name NULL */
4059#define SQLITE_DROP_INDEX10 10 /* Index Name Table Name */
4060#define SQLITE_DROP_TABLE11 11 /* Table Name NULL */
4061#define SQLITE_DROP_TEMP_INDEX12 12 /* Index Name Table Name */
4062#define SQLITE_DROP_TEMP_TABLE13 13 /* Table Name NULL */
4063#define SQLITE_DROP_TEMP_TRIGGER14 14 /* Trigger Name Table Name */
4064#define SQLITE_DROP_TEMP_VIEW15 15 /* View Name NULL */
4065#define SQLITE_DROP_TRIGGER16 16 /* Trigger Name Table Name */
4066#define SQLITE_DROP_VIEW17 17 /* View Name NULL */
4067#define SQLITE_INSERT18 18 /* Table Name NULL */
4068#define SQLITE_PRAGMA19 19 /* Pragma Name 1st arg or NULL */
4069#define SQLITE_READ20 20 /* Table Name Column Name */
4070#define SQLITE_SELECT21 21 /* NULL NULL */
4071#define SQLITE_TRANSACTION22 22 /* Operation NULL */
4072#define SQLITE_UPDATE23 23 /* Table Name Column Name */
4073#define SQLITE_ATTACH24 24 /* Filename NULL */
4074#define SQLITE_DETACH25 25 /* Database Name NULL */
4075#define SQLITE_ALTER_TABLE26 26 /* Database Name Table Name */
4076#define SQLITE_REINDEX27 27 /* Index Name NULL */
4077#define SQLITE_ANALYZE28 28 /* Table Name NULL */
4078#define SQLITE_CREATE_VTABLE29 29 /* Table Name Module Name */
4079#define SQLITE_DROP_VTABLE30 30 /* Table Name Module Name */
4080#define SQLITE_FUNCTION31 31 /* NULL Function Name */
4081#define SQLITE_SAVEPOINT32 32 /* Operation Savepoint Name */
4082#define SQLITE_COPY0 0 /* No longer used */
4083#define SQLITE_RECURSIVE33 33 /* NULL NULL */
4084
4085/*
4086** CAPI3REF: Tracing And Profiling Functions
4087** METHOD: sqlite3
4088**
4089** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
4090** instead of the routines described here.
4091**
4092** These routines register callback functions that can be used for
4093** tracing and profiling the execution of SQL statements.
4094**
4095** ^The callback function registered by sqlite3_trace() is invoked at
4096** various times when an SQL statement is being run by [sqlite3_step()].
4097** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
4098** SQL statement text as the statement first begins executing.
4099** ^(Additional sqlite3_trace() callbacks might occur
4100** as each triggered subprogram is entered. The callbacks for triggers
4101** contain a UTF-8 SQL comment that identifies the trigger.)^
4102**
4103** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
4104** the length of [bound parameter] expansion in the output of sqlite3_trace().
4105**
4106** ^The callback function registered by sqlite3_profile() is invoked
4107** as each SQL statement finishes. ^The profile callback contains
4108** the original statement text and an estimate of wall-clock time
4109** of how long that statement took to run. ^The profile callback
4110** time is in units of nanoseconds, however the current implementation
4111** is only capable of millisecond resolution so the six least significant
4112** digits in the time are meaningless. Future versions of SQLite
4113** might provide greater resolution on the profiler callback. Invoking
4114** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
4115** profile callback.
4116*/
4117SQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
4118 void(*xTrace)(void*,const char*), void*);
4119SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
4120 void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
4121
4122/*
4123** CAPI3REF: SQL Trace Event Codes
4124** KEYWORDS: SQLITE_TRACE
4125**
4126** These constants identify classes of events that can be monitored
4127** using the [sqlite3_trace_v2()] tracing logic. The M argument
4128** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
4129** the following constants. ^The first argument to the trace callback
4130** is one of the following constants.
4131**
4132** New tracing constants may be added in future releases.
4133**
4134** ^A trace callback has four arguments: xCallback(T,C,P,X).
4135** ^The T argument is one of the integer type codes above.
4136** ^The C argument is a copy of the context pointer passed in as the
4137** fourth argument to [sqlite3_trace_v2()].
4138** The P and X arguments are pointers whose meanings depend on T.
4139**
4140** <dl>
4141** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
4142** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
4143** first begins running and possibly at other times during the
4144** execution of the prepared statement, such as at the start of each
4145** trigger subprogram. ^The P argument is a pointer to the
4146** [prepared statement]. ^The X argument is a pointer to a string which
4147** is the unexpanded SQL text of the prepared statement or an SQL comment
4148** that indicates the invocation of a trigger. ^The callback can compute
4149** the same text that would have been returned by the legacy [sqlite3_trace()]
4150** interface by using the X argument when X begins with "--" and invoking
4151** [sqlite3_expanded_sql(P)] otherwise.
4152**
4153** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
4154** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
4155** information as is provided by the [sqlite3_profile()] callback.
4156** ^The P argument is a pointer to the [prepared statement] and the
4157** X argument points to a 64-bit integer which is the estimated of
4158** the number of nanosecond that the prepared statement took to run.
4159** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
4160**
4161** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
4162** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
4163** statement generates a single row of result.
4164** ^The P argument is a pointer to the [prepared statement] and the
4165** X argument is unused.
4166**
4167** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
4168** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
4169** connection closes.
4170** ^The P argument is a pointer to the [database connection] object
4171** and the X argument is unused.
4172** </dl>
4173*/
4174#define SQLITE_TRACE_STMT0x01 0x01
4175#define SQLITE_TRACE_PROFILE0x02 0x02
4176#define SQLITE_TRACE_ROW0x04 0x04
4177#define SQLITE_TRACE_CLOSE0x08 0x08
4178
4179/*
4180** CAPI3REF: SQL Trace Hook
4181** METHOD: sqlite3
4182**
4183** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
4184** function X against [database connection] D, using property mask M
4185** and context pointer P. ^If the X callback is
4186** NULL or if the M mask is zero, then tracing is disabled. The
4187** M argument should be the bitwise OR-ed combination of
4188** zero or more [SQLITE_TRACE] constants.
4189**
4190** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
4191** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
4192**
4193** ^The X callback is invoked whenever any of the events identified by
4194** mask M occur. ^The integer return value from the callback is currently
4195** ignored, though this may change in future releases. Callback
4196** implementations should return zero to ensure future compatibility.
4197**
4198** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
4199** ^The T argument is one of the [SQLITE_TRACE]
4200** constants to indicate why the callback was invoked.
4201** ^The C argument is a copy of the context pointer.
4202** The P and X arguments are pointers whose meanings depend on T.
4203**
4204** The sqlite3_trace_v2() interface is intended to replace the legacy
4205** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
4206** are deprecated.
4207*/
4208SQLITE_API int sqlite3_trace_v2(
4209 sqlite3*,
4210 unsigned uMask,
4211 int(*xCallback)(unsigned,void*,void*,void*),
4212 void *pCtx
4213);
4214
4215/*
4216** CAPI3REF: Query Progress Callbacks
4217** METHOD: sqlite3
4218**
4219** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
4220** function X to be invoked periodically during long running calls to
4221** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
4222** database connection D. An example use for this
4223** interface is to keep a GUI updated during a large query.
4224**
4225** ^The parameter P is passed through as the only parameter to the
4226** callback function X. ^The parameter N is the approximate number of
4227** [virtual machine instructions] that are evaluated between successive
4228** invocations of the callback X. ^If N is less than one then the progress
4229** handler is disabled.
4230**
4231** ^Only a single progress handler may be defined at one time per
4232** [database connection]; setting a new progress handler cancels the
4233** old one. ^Setting parameter X to NULL disables the progress handler.
4234** ^The progress handler is also disabled by setting N to a value less
4235** than 1.
4236**
4237** ^If the progress callback returns non-zero, the operation is
4238** interrupted. This feature can be used to implement a
4239** "Cancel" button on a GUI progress dialog box.
4240**
4241** The progress handler callback must not do anything that will modify
4242** the database connection that invoked the progress handler.
4243** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4244** database connections for the meaning of "modify" in this paragraph.
4245**
4246*/
4247SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
4248
4249/*
4250** CAPI3REF: Opening A New Database Connection
4251** CONSTRUCTOR: sqlite3
4252**
4253** ^These routines open an SQLite database file as specified by the
4254** filename argument. ^The filename argument is interpreted as UTF-8 for
4255** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
4256** order for sqlite3_open16(). ^(A [database connection] handle is usually
4257** returned in *ppDb, even if an error occurs. The only exception is that
4258** if SQLite is unable to allocate memory to hold the [sqlite3] object,
4259** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
4260** object.)^ ^(If the database is opened (and/or created) successfully, then
4261** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
4262** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
4263** an English language description of the error following a failure of any
4264** of the sqlite3_open() routines.
4265**
4266** ^The default encoding will be UTF-8 for databases created using
4267** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases
4268** created using sqlite3_open16() will be UTF-16 in the native byte order.
4269**
4270** Whether or not an error occurs when it is opened, resources
4271** associated with the [database connection] handle should be released by
4272** passing it to [sqlite3_close()] when it is no longer required.
4273**
4274** The sqlite3_open_v2() interface works like sqlite3_open()
4275** except that it accepts two additional parameters for additional control
4276** over the new database connection. ^(The flags parameter to
4277** sqlite3_open_v2() can take one of
4278** the following three values, optionally combined with the
4279** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
4280** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
4281**
4282** <dl>
4283** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
4284** <dd>The database is opened in read-only mode. If the database does not
4285** already exist, an error is returned.</dd>)^
4286**
4287** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
4288** <dd>The database is opened for reading and writing if possible, or reading
4289** only if the file is write protected by the operating system. In either
4290** case the database must already exist, otherwise an error is returned.</dd>)^
4291**
4292** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
4293** <dd>The database is opened for reading and writing, and is created if
4294** it does not already exist. This is the behavior that is always used for
4295** sqlite3_open() and sqlite3_open16().</dd>)^
4296** </dl>
4297**
4298** If the 3rd parameter to sqlite3_open_v2() is not one of the
4299** combinations shown above optionally combined with other
4300** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
4301** then the behavior is undefined.
4302**
4303** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
4304** opens in the multi-thread [threading mode] as long as the single-thread
4305** mode has not been set at compile-time or start-time. ^If the
4306** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
4307** in the serialized [threading mode] unless single-thread was
4308** previously selected at compile-time or start-time.
4309** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
4310** eligible to use [shared cache mode], regardless of whether or not shared
4311** cache is enabled using [sqlite3_enable_shared_cache()]. ^The
4312** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
4313** participate in [shared cache mode] even if it is enabled.
4314**
4315** ^The fourth parameter to sqlite3_open_v2() is the name of the
4316** [sqlite3_vfs] object that defines the operating system interface that
4317** the new database connection should use. ^If the fourth parameter is
4318** a NULL pointer then the default [sqlite3_vfs] object is used.
4319**
4320** ^If the filename is ":memory:", then a private, temporary in-memory database
4321** is created for the connection. ^This in-memory database will vanish when
4322** the database connection is closed. Future versions of SQLite might
4323** make use of additional special filenames that begin with the ":" character.
4324** It is recommended that when a database filename actually does begin with
4325** a ":" character you should prefix the filename with a pathname such as
4326** "./" to avoid ambiguity.
4327**
4328** ^If the filename is an empty string, then a private, temporary
4329** on-disk database will be created. ^This private database will be
4330** automatically deleted as soon as the database connection is closed.
4331**
4332** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
4333**
4334** ^If [URI filename] interpretation is enabled, and the filename argument
4335** begins with "file:", then the filename is interpreted as a URI. ^URI
4336** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
4337** set in the third argument to sqlite3_open_v2(), or if it has
4338** been enabled globally using the [SQLITE_CONFIG_URI] option with the
4339** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4340** URI filename interpretation is turned off
4341** by default, but future releases of SQLite might enable URI filename
4342** interpretation by default. See "[URI filenames]" for additional
4343** information.
4344**
4345** URI filenames are parsed according to RFC 3986. ^If the URI contains an
4346** authority, then it must be either an empty string or the string
4347** "localhost". ^If the authority is not an empty string or "localhost", an
4348** error is returned to the caller. ^The fragment component of a URI, if
4349** present, is ignored.
4350**
4351** ^SQLite uses the path component of the URI as the name of the disk file
4352** which contains the database. ^If the path begins with a '/' character,
4353** then it is interpreted as an absolute path. ^If the path does not begin
4354** with a '/' (meaning that the authority section is omitted from the URI)
4355** then the path is interpreted as a relative path.
4356** ^(On windows, the first component of an absolute path
4357** is a drive specification (e.g. "C:").)^
4358**
4359** [[core URI query parameters]]
4360** The query component of a URI may contain parameters that are interpreted
4361** either by SQLite itself, or by a [VFS | custom VFS implementation].
4362** SQLite and its built-in [VFSes] interpret the
4363** following query parameters:
4364**
4365** <ul>
4366** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
4367** a VFS object that provides the operating system interface that should
4368** be used to access the database file on disk. ^If this option is set to
4369** an empty string the default VFS object is used. ^Specifying an unknown
4370** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
4371** present, then the VFS specified by the option takes precedence over
4372** the value passed as the fourth parameter to sqlite3_open_v2().
4373**
4374** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
4375** "rwc", or "memory". Attempting to set it to any other value is
4376** an error)^.
4377** ^If "ro" is specified, then the database is opened for read-only
4378** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
4379** third argument to sqlite3_open_v2(). ^If the mode option is set to
4380** "rw", then the database is opened for read-write (but not create)
4381** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
4382** been set. ^Value "rwc" is equivalent to setting both
4383** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is
4384** set to "memory" then a pure [in-memory database] that never reads
4385** or writes from disk is used. ^It is an error to specify a value for
4386** the mode parameter that is less restrictive than that specified by
4387** the flags passed in the third parameter to sqlite3_open_v2().
4388**
4389** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
4390** "private". ^Setting it to "shared" is equivalent to setting the
4391** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
4392** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
4393** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
4394** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
4395** a URI filename, its value overrides any behavior requested by setting
4396** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
4397**
4398** <li> <b>psow</b>: ^The psow parameter indicates whether or not the
4399** [powersafe overwrite] property does or does not apply to the
4400** storage media on which the database file resides.
4401**
4402** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
4403** which if set disables file locking in rollback journal modes. This
4404** is useful for accessing a database on a filesystem that does not
4405** support locking. Caution: Database corruption might result if two
4406** or more processes write to the same database and any one of those
4407** processes uses nolock=1.
4408**
4409** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
4410** parameter that indicates that the database file is stored on
4411** read-only media. ^When immutable is set, SQLite assumes that the
4412** database file cannot be changed, even by a process with higher
4413** privilege, and so the database is opened read-only and all locking
4414** and change detection is disabled. Caution: Setting the immutable
4415** property on a database file that does in fact change can result
4416** in incorrect query results and/or [SQLITE_CORRUPT] errors.
4417** See also: [SQLITE_IOCAP_IMMUTABLE].
4418**
4419** </ul>
4420**
4421** ^Specifying an unknown parameter in the query component of a URI is not an
4422** error. Future versions of SQLite might understand additional query
4423** parameters. See "[query parameters with special meaning to SQLite]" for
4424** additional information.
4425**
4426** [[URI filename examples]] <h3>URI filename examples</h3>
4427**
4428** <table border="1" align=center cellpadding=5>
4429** <tr><th> URI filenames <th> Results
4430** <tr><td> file:data.db <td>
4431** Open the file "data.db" in the current directory.
4432** <tr><td> file:/home/fred/data.db<br>
4433** file:///home/fred/data.db <br>
4434** file://localhost/home/fred/data.db <br> <td>
4435** Open the database file "/home/fred/data.db".
4436** <tr><td> file://darkstar/home/fred/data.db <td>
4437** An error. "darkstar" is not a recognized authority.
4438** <tr><td style="white-space:nowrap">
4439** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
4440** <td> Windows only: Open the file "data.db" on fred's desktop on drive
4441** C:. Note that the %20 escaping in this example is not strictly
4442** necessary - space characters can be used literally
4443** in URI filenames.
4444** <tr><td> file:data.db?mode=ro&cache=private <td>
4445** Open file "data.db" in the current directory for read-only access.
4446** Regardless of whether or not shared-cache mode is enabled by
4447** default, use a private cache.
4448** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4449** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4450** that uses dot-files in place of posix advisory locking.
4451** <tr><td> file:data.db?mode=readonly <td>
4452** An error. "readonly" is not a valid option for the "mode" parameter.
4453** </table>
4454**
4455** ^URI hexadecimal escape sequences (%HH) are supported within the path and
4456** query components of a URI. A hexadecimal escape sequence consists of a
4457** percent sign - "%" - followed by exactly two hexadecimal digits
4458** specifying an octet value. ^Before the path or query components of a
4459** URI filename are interpreted, they are encoded using UTF-8 and all
4460** hexadecimal escape sequences replaced by a single byte containing the
4461** corresponding octet. If this process generates an invalid UTF-8 encoding,
4462** the results are undefined.
4463**
4464** <b>Note to Windows users:</b> The encoding used for the filename argument
4465** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4466** codepage is currently defined. Filenames containing international
4467** characters must be converted to UTF-8 prior to passing them into
4468** sqlite3_open() or sqlite3_open_v2().
4469**
4470** <b>Note to Windows Runtime users:</b> The temporary directory must be set
4471** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various
4472** features that require the use of temporary files may fail.
4473**
4474** See also: [sqlite3_temp_directory]
4475*/
4476SQLITE_API int sqlite3_open(
4477 const char *filename, /* Database filename (UTF-8) */
4478 sqlite3 **ppDb /* OUT: SQLite db handle */
4479);
4480SQLITE_API int sqlite3_open16(
4481 const void *filename, /* Database filename (UTF-16) */
4482 sqlite3 **ppDb /* OUT: SQLite db handle */
4483);
4484SQLITE_API int sqlite3_open_v2(
4485 const char *filename, /* Database filename (UTF-8) */
4486 sqlite3 **ppDb, /* OUT: SQLite db handle */
4487 int flags, /* Flags */
4488 const char *zVfs /* Name of VFS module to use */
4489);
4490
4491/*
4492** CAPI3REF: Obtain Values For URI Parameters
4493**
4494** These are utility routines, useful to VFS implementations, that check
4495** to see if a database file was a URI that contained a specific query
4496** parameter, and if so obtains the value of that query parameter.
4497**
4498** If F is the database filename pointer passed into the xOpen() method of
4499** a VFS implementation when the flags parameter to xOpen() has one or
4500** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
4501** P is the name of the query parameter, then
4502** sqlite3_uri_parameter(F,P) returns the value of the P
4503** parameter if it exists or a NULL pointer if P does not appear as a
4504** query parameter on F. If P is a query parameter of F
4505** has no explicit value, then sqlite3_uri_parameter(F,P) returns
4506** a pointer to an empty string.
4507**
4508** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
4509** parameter and returns true (1) or false (0) according to the value
4510** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
4511** value of query parameter P is one of "yes", "true", or "on" in any
4512** case or if the value begins with a non-zero number. The
4513** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
4514** query parameter P is one of "no", "false", or "off" in any case or
4515** if the value begins with a numeric zero. If P is not a query
4516** parameter on F or if the value of P is does not match any of the
4517** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
4518**
4519** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
4520** 64-bit signed integer and returns that integer, or D if P does not
4521** exist. If the value of P is something other than an integer, then
4522** zero is returned.
4523**
4524** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
4525** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
4526** is not a database file pathname pointer that SQLite passed into the xOpen
4527** VFS method, then the behavior of this routine is undefined and probably
4528** undesirable.
4529**
4530** See the [URI filename] documentation for additional information.
4531*/
4532SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
4533SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
4534SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
4535
4536
4537/*
4538** CAPI3REF: Error Codes And Messages
4539** METHOD: sqlite3
4540**
4541** ^If the most recent sqlite3_* API call associated with
4542** [database connection] D failed, then the sqlite3_errcode(D) interface
4543** returns the numeric [result code] or [extended result code] for that
4544** API call.
4545** ^The sqlite3_extended_errcode()
4546** interface is the same except that it always returns the
4547** [extended result code] even when extended result codes are
4548** disabled.
4549**
4550** The values returned by sqlite3_errcode() and/or
4551** sqlite3_extended_errcode() might change with each API call.
4552** Except, there are some interfaces that are guaranteed to never
4553** change the value of the error code. The error-code preserving
4554** interfaces are:
4555**
4556** <ul>
4557** <li> sqlite3_errcode()
4558** <li> sqlite3_extended_errcode()
4559** <li> sqlite3_errmsg()
4560** <li> sqlite3_errmsg16()
4561** </ul>
4562**
4563** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4564** text that describes the error, as either UTF-8 or UTF-16 respectively.
4565** ^(Memory to hold the error message string is managed internally.
4566** The application does not need to worry about freeing the result.
4567** However, the error string might be overwritten or deallocated by
4568** subsequent calls to other SQLite interface functions.)^
4569**
4570** ^The sqlite3_errstr() interface returns the English-language text
4571** that describes the [result code], as UTF-8.
4572** ^(Memory to hold the error message string is managed internally
4573** and must not be freed by the application)^.
4574**
4575** When the serialized [threading mode] is in use, it might be the
4576** case that a second error occurs on a separate thread in between
4577** the time of the first error and the call to these interfaces.
4578** When that happens, the second error will be reported since these
4579** interfaces always report the most recent result. To avoid
4580** this, each thread can obtain exclusive use of the [database connection] D
4581** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
4582** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
4583** all calls to the interfaces listed here are completed.
4584**
4585** If an interface fails with SQLITE_MISUSE, that means the interface
4586** was invoked incorrectly by the application. In that case, the
4587** error code and message may or may not be set.
4588*/
4589SQLITE_API int sqlite3_errcode(sqlite3 *db);
4590SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
4591SQLITE_API const char *sqlite3_errmsg(sqlite3*);
4592SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
4593SQLITE_API const char *sqlite3_errstr(int);
4594
4595/*
4596** CAPI3REF: Prepared Statement Object
4597** KEYWORDS: {prepared statement} {prepared statements}
4598**
4599** An instance of this object represents a single SQL statement that
4600** has been compiled into binary form and is ready to be evaluated.
4601**
4602** Think of each SQL statement as a separate computer program. The
4603** original SQL text is source code. A prepared statement object
4604** is the compiled object code. All SQL must be converted into a
4605** prepared statement before it can be run.
4606**
4607** The life-cycle of a prepared statement object usually goes like this:
4608**
4609** <ol>
4610** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
4611** <li> Bind values to [parameters] using the sqlite3_bind_*()
4612** interfaces.
4613** <li> Run the SQL by calling [sqlite3_step()] one or more times.
4614** <li> Reset the prepared statement using [sqlite3_reset()] then go back
4615** to step 2. Do this zero or more times.
4616** <li> Destroy the object using [sqlite3_finalize()].
4617** </ol>
4618*/
4619typedef struct sqlite3_stmt sqlite3_stmt;
4620
4621/*
4622** CAPI3REF: Run-time Limits
4623** METHOD: sqlite3
4624**
4625** ^(This interface allows the size of various constructs to be limited
4626** on a connection by connection basis. The first parameter is the
4627** [database connection] whose limit is to be set or queried. The
4628** second parameter is one of the [limit categories] that define a
4629** class of constructs to be size limited. The third parameter is the
4630** new limit for that construct.)^
4631**
4632** ^If the new limit is a negative number, the limit is unchanged.
4633** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
4634** [limits | hard upper bound]
4635** set at compile-time by a C preprocessor macro called
4636** [limits | SQLITE_MAX_<i>NAME</i>].
4637** (The "_LIMIT_" in the name is changed to "_MAX_".))^
4638** ^Attempts to increase a limit above its hard upper bound are
4639** silently truncated to the hard upper bound.
4640**
4641** ^Regardless of whether or not the limit was changed, the
4642** [sqlite3_limit()] interface returns the prior value of the limit.
4643** ^Hence, to find the current value of a limit without changing it,
4644** simply invoke this interface with the third parameter set to -1.
4645**
4646** Run-time limits are intended for use in applications that manage
4647** both their own internal database and also databases that are controlled
4648** by untrusted external sources. An example application might be a
4649** web browser that has its own databases for storing history and
4650** separate databases controlled by JavaScript applications downloaded
4651** off the Internet. The internal databases can be given the
4652** large, default limits. Databases managed by external sources can
4653** be given much smaller limits designed to prevent a denial of service
4654** attack. Developers might also want to use the [sqlite3_set_authorizer()]
4655** interface to further control untrusted SQL. The size of the database
4656** created by an untrusted script can be contained using the
4657** [max_page_count] [PRAGMA].
4658**
4659** New run-time limit categories may be added in future releases.
4660*/
4661SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
4662
4663/*
4664** CAPI3REF: Run-Time Limit Categories
4665** KEYWORDS: {limit category} {*limit categories}
4666**
4667** These constants define various performance limits
4668** that can be lowered at run-time using [sqlite3_limit()].
4669** The synopsis of the meanings of the various limits is shown below.
4670** Additional information is available at [limits | Limits in SQLite].
4671**
4672** <dl>
4673** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
4674** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
4675**
4676** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
4677** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4678**
4679** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
4680** <dd>The maximum number of columns in a table definition or in the
4681** result set of a [SELECT] or the maximum number of columns in an index
4682** or in an ORDER BY or GROUP BY clause.</dd>)^
4683**
4684** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
4685** <dd>The maximum depth of the parse tree on any expression.</dd>)^
4686**
4687** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
4688** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
4689**
4690** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
4691** <dd>The maximum number of instructions in a virtual machine program
4692** used to implement an SQL statement. If [sqlite3_prepare_v2()] or
4693** the equivalent tries to allocate space for more than this many opcodes
4694** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
4695**
4696** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
4697** <dd>The maximum number of arguments on a function.</dd>)^
4698**
4699** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
4700** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
4701**
4702** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
4703** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
4704** <dd>The maximum length of the pattern argument to the [LIKE] or
4705** [GLOB] operators.</dd>)^
4706**
4707** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
4708** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
4709** <dd>The maximum index number of any [parameter] in an SQL statement.)^
4710**
4711** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
4712** <dd>The maximum depth of recursion for triggers.</dd>)^
4713**
4714** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
4715** <dd>The maximum number of auxiliary worker threads that a single
4716** [prepared statement] may start.</dd>)^
4717** </dl>
4718*/
4719#define SQLITE_LIMIT_LENGTH0 0
4720#define SQLITE_LIMIT_SQL_LENGTH1 1
4721#define SQLITE_LIMIT_COLUMN2 2
4722#define SQLITE_LIMIT_EXPR_DEPTH3 3
4723#define SQLITE_LIMIT_COMPOUND_SELECT4 4
4724#define SQLITE_LIMIT_VDBE_OP5 5
4725#define SQLITE_LIMIT_FUNCTION_ARG6 6
4726#define SQLITE_LIMIT_ATTACHED7 7
4727#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH8 8
4728#define SQLITE_LIMIT_VARIABLE_NUMBER9 9
4729#define SQLITE_LIMIT_TRIGGER_DEPTH10 10
4730#define SQLITE_LIMIT_WORKER_THREADS11 11
4731
4732/*
4733** CAPI3REF: Prepare Flags
4734**
4735** These constants define various flags that can be passed into
4736** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
4737** [sqlite3_prepare16_v3()] interfaces.
4738**
4739** New flags may be added in future releases of SQLite.
4740**
4741** <dl>
4742** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
4743** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
4744** that the prepared statement will be retained for a long time and
4745** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
4746** and [sqlite3_prepare16_v3()] assume that the prepared statement will
4747** be used just once or at most a few times and then destroyed using
4748** [sqlite3_finalize()] relatively soon. The current implementation acts
4749** on this hint by avoiding the use of [lookaside memory] so as not to
4750** deplete the limited store of lookaside memory. Future versions of
4751** SQLite may act on this hint differently.
4752**
4753** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
4754** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4755** to be required for any prepared statement that wanted to use the
4756** [sqlite3_normalized_sql()] interface. However, the
4757** [sqlite3_normalized_sql()] interface is now available to all
4758** prepared statements, regardless of whether or not they use this
4759** flag.
4760**
4761** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
4762** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
4763** to return an error (error code SQLITE_ERROR) if the statement uses
4764** any virtual tables.
4765** </dl>
4766*/
4767#define SQLITE_PREPARE_PERSISTENT0x01 0x01
4768#define SQLITE_PREPARE_NORMALIZE0x02 0x02
4769#define SQLITE_PREPARE_NO_VTAB0x04 0x04
4770
4771/*
4772** CAPI3REF: Compiling An SQL Statement
4773** KEYWORDS: {SQL statement compiler}
4774** METHOD: sqlite3
4775** CONSTRUCTOR: sqlite3_stmt
4776**
4777** To execute an SQL statement, it must first be compiled into a byte-code
4778** program using one of these routines. Or, in other words, these routines
4779** are constructors for the [prepared statement] object.
4780**
4781** The preferred routine to use is [sqlite3_prepare_v2()]. The
4782** [sqlite3_prepare()] interface is legacy and should be avoided.
4783** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
4784** for special purposes.
4785**
4786** The use of the UTF-8 interfaces is preferred, as SQLite currently
4787** does all parsing using UTF-8. The UTF-16 interfaces are provided
4788** as a convenience. The UTF-16 interfaces work by converting the
4789** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4790**
4791** The first argument, "db", is a [database connection] obtained from a
4792** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
4793** [sqlite3_open16()]. The database connection must not have been closed.
4794**
4795** The second argument, "zSql", is the statement to be compiled, encoded
4796** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4797** and sqlite3_prepare_v3()
4798** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4799** and sqlite3_prepare16_v3() use UTF-16.
4800**
4801** ^If the nByte argument is negative, then zSql is read up to the
4802** first zero terminator. ^If nByte is positive, then it is the
4803** number of bytes read from zSql. ^If nByte is zero, then no prepared
4804** statement is generated.
4805** If the caller knows that the supplied string is nul-terminated, then
4806** there is a small performance advantage to passing an nByte parameter that
4807** is the number of bytes in the input string <i>including</i>
4808** the nul-terminator.
4809**
4810** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4811** past the end of the first SQL statement in zSql. These routines only
4812** compile the first statement in zSql, so *pzTail is left pointing to
4813** what remains uncompiled.
4814**
4815** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4816** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
4817** to NULL. ^If the input text contains no SQL (if the input is an empty
4818** string or a comment) then *ppStmt is set to NULL.
4819** The calling procedure is responsible for deleting the compiled
4820** SQL statement using [sqlite3_finalize()] after it has finished with it.
4821** ppStmt may not be NULL.
4822**
4823** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
4824** otherwise an [error code] is returned.
4825**
4826** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
4827** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
4828** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
4829** are retained for backwards compatibility, but their use is discouraged.
4830** ^In the "vX" interfaces, the prepared statement
4831** that is returned (the [sqlite3_stmt] object) contains a copy of the
4832** original SQL text. This causes the [sqlite3_step()] interface to
4833** behave differently in three ways:
4834**
4835** <ol>
4836** <li>
4837** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
4838** always used to do, [sqlite3_step()] will automatically recompile the SQL
4839** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
4840** retries will occur before sqlite3_step() gives up and returns an error.
4841** </li>
4842**
4843** <li>
4844** ^When an error occurs, [sqlite3_step()] will return one of the detailed
4845** [error codes] or [extended error codes]. ^The legacy behavior was that
4846** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4847** and the application would have to make a second call to [sqlite3_reset()]
4848** in order to find the underlying cause of the problem. With the "v2" prepare
4849** interfaces, the underlying reason for the error is returned immediately.
4850** </li>
4851**
4852** <li>
4853** ^If the specific value bound to [parameter | host parameter] in the
4854** WHERE clause might influence the choice of query plan for a statement,
4855** then the statement will be automatically recompiled, as if there had been
4856** a schema change, on the first [sqlite3_step()] call following any change
4857** to the [sqlite3_bind_text | bindings] of that [parameter].
4858** ^The specific value of WHERE-clause [parameter] might influence the
4859** choice of query plan if the parameter is the left-hand side of a [LIKE]
4860** or [GLOB] operator or if the parameter is compared to an indexed column
4861** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
4862** </li>
4863** </ol>
4864**
4865** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4866** the extra prepFlags parameter, which is a bit array consisting of zero or
4867** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The
4868** sqlite3_prepare_v2() interface works exactly the same as
4869** sqlite3_prepare_v3() with a zero prepFlags parameter.
4870*/
4871SQLITE_API int sqlite3_prepare(
4872 sqlite3 *db, /* Database handle */
4873 const char *zSql, /* SQL statement, UTF-8 encoded */
4874 int nByte, /* Maximum length of zSql in bytes. */
4875 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4876 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4877);
4878SQLITE_API int sqlite3_prepare_v2(
4879 sqlite3 *db, /* Database handle */
4880 const char *zSql, /* SQL statement, UTF-8 encoded */
4881 int nByte, /* Maximum length of zSql in bytes. */
4882 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4883 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4884);
4885SQLITE_API int sqlite3_prepare_v3(
4886 sqlite3 *db, /* Database handle */
4887 const char *zSql, /* SQL statement, UTF-8 encoded */
4888 int nByte, /* Maximum length of zSql in bytes. */
4889 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4890 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4891 const char **pzTail /* OUT: Pointer to unused portion of zSql */
4892);
4893SQLITE_API int sqlite3_prepare16(
4894 sqlite3 *db, /* Database handle */
4895 const void *zSql, /* SQL statement, UTF-16 encoded */
4896 int nByte, /* Maximum length of zSql in bytes. */
4897 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4898 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4899);
4900SQLITE_API int sqlite3_prepare16_v2(
4901 sqlite3 *db, /* Database handle */
4902 const void *zSql, /* SQL statement, UTF-16 encoded */
4903 int nByte, /* Maximum length of zSql in bytes. */
4904 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4905 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4906);
4907SQLITE_API int sqlite3_prepare16_v3(
4908 sqlite3 *db, /* Database handle */
4909 const void *zSql, /* SQL statement, UTF-16 encoded */
4910 int nByte, /* Maximum length of zSql in bytes. */
4911 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4912 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
4913 const void **pzTail /* OUT: Pointer to unused portion of zSql */
4914);
4915
4916/*
4917** CAPI3REF: Retrieving Statement SQL
4918** METHOD: sqlite3_stmt
4919**
4920** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4921** SQL text used to create [prepared statement] P if P was
4922** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
4923** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4924** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4925** string containing the SQL text of prepared statement P with
4926** [bound parameters] expanded.
4927** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4928** string containing the normalized SQL text of prepared statement P. The
4929** semantics used to normalize a SQL statement are unspecified and subject
4930** to change. At a minimum, literal values will be replaced with suitable
4931** placeholders.
4932**
4933** ^(For example, if a prepared statement is created using the SQL
4934** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
4935** and parameter :xyz is unbound, then sqlite3_sql() will return
4936** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
4937** will return "SELECT 2345,NULL".)^
4938**
4939** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
4940** is available to hold the result, or if the result would exceed the
4941** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4942**
4943** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4944** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4945** option causes sqlite3_expanded_sql() to always return NULL.
4946**
4947** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
4948** are managed by SQLite and are automatically freed when the prepared
4949** statement is finalized.
4950** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
4951** is obtained from [sqlite3_malloc()] and must be free by the application
4952** by passing it to [sqlite3_free()].
4953*/
4954SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
4955SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
4956SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
4957
4958/*
4959** CAPI3REF: Determine If An SQL Statement Writes The Database
4960** METHOD: sqlite3_stmt
4961**
4962** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4963** and only if the [prepared statement] X makes no direct changes to
4964** the content of the database file.
4965**
4966** Note that [application-defined SQL functions] or
4967** [virtual tables] might change the database indirectly as a side effect.
4968** ^(For example, if an application defines a function "eval()" that
4969** calls [sqlite3_exec()], then the following SQL statement would
4970** change the database file through side-effects:
4971**
4972** <blockquote><pre>
4973** SELECT eval('DELETE FROM t1') FROM t2;
4974** </pre></blockquote>
4975**
4976** But because the [SELECT] statement does not change the database file
4977** directly, sqlite3_stmt_readonly() would still return true.)^
4978**
4979** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
4980** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
4981** since the statements themselves do not actually modify the database but
4982** rather they control the timing of when other statements modify the
4983** database. ^The [ATTACH] and [DETACH] statements also cause
4984** sqlite3_stmt_readonly() to return true since, while those statements
4985** change the configuration of a database connection, they do not make
4986** changes to the content of the database files on disk.
4987** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
4988** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
4989** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
4990** sqlite3_stmt_readonly() returns false for those commands.
4991*/
4992SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4993
4994/*
4995** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
4996** METHOD: sqlite3_stmt
4997**
4998** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
4999** prepared statement S is an EXPLAIN statement, or 2 if the
5000** statement S is an EXPLAIN QUERY PLAN.
5001** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
5002** an ordinary statement or a NULL pointer.
5003*/
5004SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
5005
5006/*
5007** CAPI3REF: Determine If A Prepared Statement Has Been Reset
5008** METHOD: sqlite3_stmt
5009**
5010** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
5011** [prepared statement] S has been stepped at least once using
5012** [sqlite3_step(S)] but has neither run to completion (returned
5013** [SQLITE_DONE] from [sqlite3_step(S)]) nor
5014** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
5015** interface returns false if S is a NULL pointer. If S is not a
5016** NULL pointer and is not a pointer to a valid [prepared statement]
5017** object, then the behavior is undefined and probably undesirable.
5018**
5019** This interface can be used in combination [sqlite3_next_stmt()]
5020** to locate all prepared statements associated with a database
5021** connection that are in need of being reset. This can be used,
5022** for example, in diagnostic routines to search for prepared
5023** statements that are holding a transaction open.
5024*/
5025SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
5026
5027/*
5028** CAPI3REF: Dynamically Typed Value Object
5029** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
5030**
5031** SQLite uses the sqlite3_value object to represent all values
5032** that can be stored in a database table. SQLite uses dynamic typing
5033** for the values it stores. ^Values stored in sqlite3_value objects
5034** can be integers, floating point values, strings, BLOBs, or NULL.
5035**
5036** An sqlite3_value object may be either "protected" or "unprotected".
5037** Some interfaces require a protected sqlite3_value. Other interfaces
5038** will accept either a protected or an unprotected sqlite3_value.
5039** Every interface that accepts sqlite3_value arguments specifies
5040** whether or not it requires a protected sqlite3_value. The
5041** [sqlite3_value_dup()] interface can be used to construct a new
5042** protected sqlite3_value from an unprotected sqlite3_value.
5043**
5044** The terms "protected" and "unprotected" refer to whether or not
5045** a mutex is held. An internal mutex is held for a protected
5046** sqlite3_value object but no mutex is held for an unprotected
5047** sqlite3_value object. If SQLite is compiled to be single-threaded
5048** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
5049** or if SQLite is run in one of reduced mutex modes
5050** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
5051** then there is no distinction between protected and unprotected
5052** sqlite3_value objects and they can be used interchangeably. However,
5053** for maximum code portability it is recommended that applications
5054** still make the distinction between protected and unprotected
5055** sqlite3_value objects even when not strictly required.
5056**
5057** ^The sqlite3_value objects that are passed as parameters into the
5058** implementation of [application-defined SQL functions] are protected.
5059** ^The sqlite3_value object returned by
5060** [sqlite3_column_value()] is unprotected.
5061** Unprotected sqlite3_value objects may only be used as arguments
5062** to [sqlite3_result_value()], [sqlite3_bind_value()], and
5063** [sqlite3_value_dup()].
5064** The [sqlite3_value_blob | sqlite3_value_type()] family of
5065** interfaces require protected sqlite3_value objects.
5066*/
5067typedef struct sqlite3_value sqlite3_value;
5068
5069/*
5070** CAPI3REF: SQL Function Context Object
5071**
5072** The context in which an SQL function executes is stored in an
5073** sqlite3_context object. ^A pointer to an sqlite3_context object
5074** is always first parameter to [application-defined SQL functions].
5075** The application-defined SQL function implementation will pass this
5076** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
5077** [sqlite3_aggregate_context()], [sqlite3_user_data()],
5078** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
5079** and/or [sqlite3_set_auxdata()].
5080*/
5081typedef struct sqlite3_context sqlite3_context;
5082
5083/*
5084** CAPI3REF: Binding Values To Prepared Statements
5085** KEYWORDS: {host parameter} {host parameters} {host parameter name}
5086** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
5087** METHOD: sqlite3_stmt
5088**
5089** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
5090** literals may be replaced by a [parameter] that matches one of following
5091** templates:
5092**
5093** <ul>
5094** <li> ?
5095** <li> ?NNN
5096** <li> :VVV
5097** <li> @VVV
5098** <li> $VVV
5099** </ul>
5100**
5101** In the templates above, NNN represents an integer literal,
5102** and VVV represents an alphanumeric identifier.)^ ^The values of these
5103** parameters (also called "host parameter names" or "SQL parameters")
5104** can be set using the sqlite3_bind_*() routines defined here.
5105**
5106** ^The first argument to the sqlite3_bind_*() routines is always
5107** a pointer to the [sqlite3_stmt] object returned from
5108** [sqlite3_prepare_v2()] or its variants.
5109**
5110** ^The second argument is the index of the SQL parameter to be set.
5111** ^The leftmost SQL parameter has an index of 1. ^When the same named
5112** SQL parameter is used more than once, second and subsequent
5113** occurrences have the same index as the first occurrence.
5114** ^The index for named parameters can be looked up using the
5115** [sqlite3_bind_parameter_index()] API if desired. ^The index
5116** for "?NNN" parameters is the value of NNN.
5117** ^The NNN value must be between 1 and the [sqlite3_limit()]
5118** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
5119**
5120** ^The third argument is the value to bind to the parameter.
5121** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5122** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
5123** is ignored and the end result is the same as sqlite3_bind_null().
5124**
5125** ^(In those routines that have a fourth argument, its value is the
5126** number of bytes in the parameter. To be clear: the value is the
5127** number of <u>bytes</u> in the value, not the number of characters.)^
5128** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
5129** is negative, then the length of the string is
5130** the number of bytes up to the first zero terminator.
5131** If the fourth parameter to sqlite3_bind_blob() is negative, then
5132** the behavior is undefined.
5133** If a non-negative fourth parameter is provided to sqlite3_bind_text()
5134** or sqlite3_bind_text16() or sqlite3_bind_text64() then
5135** that parameter must be the byte offset
5136** where the NUL terminator would occur assuming the string were NUL
5137** terminated. If any NUL characters occur at byte offsets less than
5138** the value of the fourth parameter then the resulting string value will
5139** contain embedded NULs. The result of expressions involving strings
5140** with embedded NULs is undefined.
5141**
5142** ^The fifth argument to the BLOB and string binding interfaces
5143** is a destructor used to dispose of the BLOB or
5144** string after SQLite has finished with it. ^The destructor is called
5145** to dispose of the BLOB or string even if the call to the bind API fails,
5146** except the destructor is not called if the third parameter is a NULL
5147** pointer or the fourth parameter is negative.
5148** ^If the fifth argument is
5149** the special value [SQLITE_STATIC], then SQLite assumes that the
5150** information is in static, unmanaged space and does not need to be freed.
5151** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
5152** SQLite makes its own private copy of the data immediately, before
5153** the sqlite3_bind_*() routine returns.
5154**
5155** ^The sixth argument to sqlite3_bind_text64() must be one of
5156** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
5157** to specify the encoding of the text in the third parameter. If
5158** the sixth argument to sqlite3_bind_text64() is not one of the
5159** allowed values shown above, or if the text encoding is different
5160** from the encoding specified by the sixth parameter, then the behavior
5161** is undefined.
5162**
5163** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
5164** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
5165** (just an integer to hold its size) while it is being processed.
5166** Zeroblobs are intended to serve as placeholders for BLOBs whose
5167** content is later written using
5168** [sqlite3_blob_open | incremental BLOB I/O] routines.
5169** ^A negative value for the zeroblob results in a zero-length BLOB.
5170**
5171** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5172** [prepared statement] S to have an SQL value of NULL, but to also be
5173** associated with the pointer P of type T. ^D is either a NULL pointer or
5174** a pointer to a destructor function for P. ^SQLite will invoke the
5175** destructor D with a single argument of P when it is finished using
5176** P. The T parameter should be a static string, preferably a string
5177** literal. The sqlite3_bind_pointer() routine is part of the
5178** [pointer passing interface] added for SQLite 3.20.0.
5179**
5180** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
5181** for the [prepared statement] or with a prepared statement for which
5182** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
5183** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
5184** routine is passed a [prepared statement] that has been finalized, the
5185** result is undefined and probably harmful.
5186**
5187** ^Bindings are not cleared by the [sqlite3_reset()] routine.
5188** ^Unbound parameters are interpreted as NULL.
5189**
5190** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
5191** [error code] if anything goes wrong.
5192** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
5193** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
5194** [SQLITE_MAX_LENGTH].
5195** ^[SQLITE_RANGE] is returned if the parameter
5196** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
5197**
5198** See also: [sqlite3_bind_parameter_count()],
5199** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
5200*/
5201SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
5202SQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
5203 void(*)(void*));
5204SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
5205SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
5206SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
5207SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
5208SQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
5209SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
5210SQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
5211 void(*)(void*), unsigned char encoding);
5212SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
5213SQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
5214SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
5215SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
5216
5217/*
5218** CAPI3REF: Number Of SQL Parameters
5219** METHOD: sqlite3_stmt
5220**
5221** ^This routine can be used to find the number of [SQL parameters]
5222** in a [prepared statement]. SQL parameters are tokens of the
5223** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
5224** placeholders for values that are [sqlite3_bind_blob | bound]
5225** to the parameters at a later time.
5226**
5227** ^(This routine actually returns the index of the largest (rightmost)
5228** parameter. For all forms except ?NNN, this will correspond to the
5229** number of unique parameters. If parameters of the ?NNN form are used,
5230** there may be gaps in the list.)^
5231**
5232** See also: [sqlite3_bind_blob|sqlite3_bind()],
5233** [sqlite3_bind_parameter_name()], and
5234** [sqlite3_bind_parameter_index()].
5235*/
5236SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
5237
5238/*
5239** CAPI3REF: Name Of A Host Parameter
5240** METHOD: sqlite3_stmt
5241**
5242** ^The sqlite3_bind_parameter_name(P,N) interface returns
5243** the name of the N-th [SQL parameter] in the [prepared statement] P.
5244** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
5245** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
5246** respectively.
5247** In other words, the initial ":" or "$" or "@" or "?"
5248** is included as part of the name.)^
5249** ^Parameters of the form "?" without a following integer have no name
5250** and are referred to as "nameless" or "anonymous parameters".
5251**
5252** ^The first host parameter has an index of 1, not 0.
5253**
5254** ^If the value N is out of range or if the N-th parameter is
5255** nameless, then NULL is returned. ^The returned string is
5256** always in UTF-8 encoding even if the named parameter was
5257** originally specified as UTF-16 in [sqlite3_prepare16()],
5258** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
5259**
5260** See also: [sqlite3_bind_blob|sqlite3_bind()],
5261** [sqlite3_bind_parameter_count()], and
5262** [sqlite3_bind_parameter_index()].
5263*/
5264SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
5265
5266/*
5267** CAPI3REF: Index Of A Parameter With A Given Name
5268** METHOD: sqlite3_stmt
5269**
5270** ^Return the index of an SQL parameter given its name. ^The
5271** index value returned is suitable for use as the second
5272** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
5273** is returned if no matching parameter is found. ^The parameter
5274** name must be given in UTF-8 even if the original statement
5275** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5276** [sqlite3_prepare16_v3()].
5277**
5278** See also: [sqlite3_bind_blob|sqlite3_bind()],
5279** [sqlite3_bind_parameter_count()], and
5280** [sqlite3_bind_parameter_name()].
5281*/
5282SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
5283
5284/*
5285** CAPI3REF: Reset All Bindings On A Prepared Statement
5286** METHOD: sqlite3_stmt
5287**
5288** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
5289** the [sqlite3_bind_blob | bindings] on a [prepared statement].
5290** ^Use this routine to reset all host parameters to NULL.
5291*/
5292SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
5293
5294/*
5295** CAPI3REF: Number Of Columns In A Result Set
5296** METHOD: sqlite3_stmt
5297**
5298** ^Return the number of columns in the result set returned by the
5299** [prepared statement]. ^If this routine returns 0, that means the
5300** [prepared statement] returns no data (for example an [UPDATE]).
5301** ^However, just because this routine returns a positive number does not
5302** mean that one or more rows of data will be returned. ^A SELECT statement
5303** will always have a positive sqlite3_column_count() but depending on the
5304** WHERE clause constraints and the table content, it might return no rows.
5305**
5306** See also: [sqlite3_data_count()]
5307*/
5308SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
5309
5310/*
5311** CAPI3REF: Column Names In A Result Set
5312** METHOD: sqlite3_stmt
5313**
5314** ^These routines return the name assigned to a particular column
5315** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
5316** interface returns a pointer to a zero-terminated UTF-8 string
5317** and sqlite3_column_name16() returns a pointer to a zero-terminated
5318** UTF-16 string. ^The first parameter is the [prepared statement]
5319** that implements the [SELECT] statement. ^The second parameter is the
5320** column number. ^The leftmost column is number 0.
5321**
5322** ^The returned string pointer is valid until either the [prepared statement]
5323** is destroyed by [sqlite3_finalize()] or until the statement is automatically
5324** reprepared by the first call to [sqlite3_step()] for a particular run
5325** or until the next call to
5326** sqlite3_column_name() or sqlite3_column_name16() on the same column.
5327**
5328** ^If sqlite3_malloc() fails during the processing of either routine
5329** (for example during a conversion from UTF-8 to UTF-16) then a
5330** NULL pointer is returned.
5331**
5332** ^The name of a result column is the value of the "AS" clause for
5333** that column, if there is an AS clause. If there is no AS clause
5334** then the name of the column is unspecified and may change from
5335** one release of SQLite to the next.
5336*/
5337SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
5338SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
5339
5340/*
5341** CAPI3REF: Source Of Data In A Query Result
5342** METHOD: sqlite3_stmt
5343**
5344** ^These routines provide a means to determine the database, table, and
5345** table column that is the origin of a particular result column in
5346** [SELECT] statement.
5347** ^The name of the database or table or column can be returned as
5348** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5349** the database name, the _table_ routines return the table name, and
5350** the origin_ routines return the column name.
5351** ^The returned string is valid until the [prepared statement] is destroyed
5352** using [sqlite3_finalize()] or until the statement is automatically
5353** reprepared by the first call to [sqlite3_step()] for a particular run
5354** or until the same information is requested
5355** again in a different encoding.
5356**
5357** ^The names returned are the original un-aliased names of the
5358** database, table, and column.
5359**
5360** ^The first argument to these interfaces is a [prepared statement].
5361** ^These functions return information about the Nth result column returned by
5362** the statement, where N is the second function argument.
5363** ^The left-most column is column 0 for these routines.
5364**
5365** ^If the Nth column returned by the statement is an expression or
5366** subquery and is not a column value, then all of these functions return
5367** NULL. ^These routine might also return NULL if a memory allocation error
5368** occurs. ^Otherwise, they return the name of the attached database, table,
5369** or column that query result column was extracted from.
5370**
5371** ^As with all other SQLite APIs, those whose names end with "16" return
5372** UTF-16 encoded strings and the other functions return UTF-8.
5373**
5374** ^These APIs are only available if the library was compiled with the
5375** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5376**
5377** If two or more threads call one or more of these routines against the same
5378** prepared statement and column at the same time then the results are
5379** undefined.
5380**
5381** If two or more threads call one or more
5382** [sqlite3_column_database_name | column metadata interfaces]
5383** for the same [prepared statement] and result column
5384** at the same time then the results are undefined.
5385*/
5386SQLITE_API const char *sqlite3_column_database_name0(sqlite3_stmt*,int);
5387SQLITE_API const void *sqlite3_column_database_name160(sqlite3_stmt*,int);
5388SQLITE_API const char *sqlite3_column_table_name0(sqlite3_stmt*,int);
5389SQLITE_API const void *sqlite3_column_table_name160(sqlite3_stmt*,int);
5390SQLITE_API const char *sqlite3_column_origin_name0(sqlite3_stmt*,int);
5391SQLITE_API const void *sqlite3_column_origin_name160(sqlite3_stmt*,int);
5392
5393/*
5394** CAPI3REF: Declared Datatype Of A Query Result
5395** METHOD: sqlite3_stmt
5396**
5397** ^(The first parameter is a [prepared statement].
5398** If this statement is a [SELECT] statement and the Nth column of the
5399** returned result set of that [SELECT] is a table column (not an
5400** expression or subquery) then the declared type of the table
5401** column is returned.)^ ^If the Nth column of the result set is an
5402** expression or subquery, then a NULL pointer is returned.
5403** ^The returned string is always UTF-8 encoded.
5404**
5405** ^(For example, given the database schema:
5406**
5407** CREATE TABLE t1(c1 VARIANT);
5408**
5409** and the following statement to be compiled:
5410**
5411** SELECT c1 + 1, c1 FROM t1;
5412**
5413** this routine would return the string "VARIANT" for the second result
5414** column (i==1), and a NULL pointer for the first result column (i==0).)^
5415**
5416** ^SQLite uses dynamic run-time typing. ^So just because a column
5417** is declared to contain a particular type does not mean that the
5418** data stored in that column is of the declared type. SQLite is
5419** strongly typed, but the typing is dynamic not static. ^Type
5420** is associated with individual values, not with the containers
5421** used to hold those values.
5422*/
5423SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
5424SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
5425
5426/*
5427** CAPI3REF: Evaluate An SQL Statement
5428** METHOD: sqlite3_stmt
5429**
5430** After a [prepared statement] has been prepared using any of
5431** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
5432** or [sqlite3_prepare16_v3()] or one of the legacy
5433** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
5434** must be called one or more times to evaluate the statement.
5435**
5436** The details of the behavior of the sqlite3_step() interface depend
5437** on whether the statement was prepared using the newer "vX" interfaces
5438** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
5439** [sqlite3_prepare16_v2()] or the older legacy
5440** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
5441** new "vX" interface is recommended for new applications but the legacy
5442** interface will continue to be supported.
5443**
5444** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
5445** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
5446** ^With the "v2" interface, any of the other [result codes] or
5447** [extended result codes] might be returned as well.
5448**
5449** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
5450** database locks it needs to do its job. ^If the statement is a [COMMIT]
5451** or occurs outside of an explicit transaction, then you can retry the
5452** statement. If the statement is not a [COMMIT] and occurs within an
5453** explicit transaction then you should rollback the transaction before
5454** continuing.
5455**
5456** ^[SQLITE_DONE] means that the statement has finished executing
5457** successfully. sqlite3_step() should not be called again on this virtual
5458** machine without first calling [sqlite3_reset()] to reset the virtual
5459** machine back to its initial state.
5460**
5461** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
5462** is returned each time a new row of data is ready for processing by the
5463** caller. The values may be accessed using the [column access functions].
5464** sqlite3_step() is called again to retrieve the next row of data.
5465**
5466** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5467** violation) has occurred. sqlite3_step() should not be called again on
5468** the VM. More information may be found by calling [sqlite3_errmsg()].
5469** ^With the legacy interface, a more specific error code (for example,
5470** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
5471** can be obtained by calling [sqlite3_reset()] on the
5472** [prepared statement]. ^In the "v2" interface,
5473** the more specific error code is returned directly by sqlite3_step().
5474**
5475** [SQLITE_MISUSE] means that the this routine was called inappropriately.
5476** Perhaps it was called on a [prepared statement] that has
5477** already been [sqlite3_finalize | finalized] or on one that had
5478** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
5479** be the case that the same database connection is being used by two or
5480** more threads at the same moment in time.
5481**
5482** For all versions of SQLite up to and including 3.6.23.1, a call to
5483** [sqlite3_reset()] was required after sqlite3_step() returned anything
5484** other than [SQLITE_ROW] before any subsequent invocation of
5485** sqlite3_step(). Failure to reset the prepared statement using
5486** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
5487** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1],
5488** sqlite3_step() began
5489** calling [sqlite3_reset()] automatically in this circumstance rather
5490** than returning [SQLITE_MISUSE]. This is not considered a compatibility
5491** break because any application that ever receives an SQLITE_MISUSE error
5492** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5493** can be used to restore the legacy behavior.
5494**
5495** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
5496** API always returns a generic error code, [SQLITE_ERROR], following any
5497** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
5498** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
5499** specific [error codes] that better describes the error.
5500** We admit that this is a goofy design. The problem has been fixed
5501** with the "v2" interface. If you prepare all of your SQL statements
5502** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
5503** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
5504** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
5505** then the more specific [error codes] are returned directly
5506** by sqlite3_step(). The use of the "vX" interfaces is recommended.
5507*/
5508SQLITE_API int sqlite3_step(sqlite3_stmt*);
5509
5510/*
5511** CAPI3REF: Number of columns in a result set
5512** METHOD: sqlite3_stmt
5513**
5514** ^The sqlite3_data_count(P) interface returns the number of columns in the
5515** current row of the result set of [prepared statement] P.
5516** ^If prepared statement P does not have results ready to return
5517** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
5518** interfaces) then sqlite3_data_count(P) returns 0.
5519** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
5520** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
5521** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
5522** will return non-zero if previous call to [sqlite3_step](P) returned
5523** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
5524** where it always returns zero since each step of that multi-step
5525** pragma returns 0 columns of data.
5526**
5527** See also: [sqlite3_column_count()]
5528*/
5529SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
5530
5531/*
5532** CAPI3REF: Fundamental Datatypes
5533** KEYWORDS: SQLITE_TEXT
5534**
5535** ^(Every value in SQLite has one of five fundamental datatypes:
5536**
5537** <ul>
5538** <li> 64-bit signed integer
5539** <li> 64-bit IEEE floating point number
5540** <li> string
5541** <li> BLOB
5542** <li> NULL
5543** </ul>)^
5544**
5545** These constants are codes for each of those types.
5546**
5547** Note that the SQLITE_TEXT constant was also used in SQLite version 2
5548** for a completely different meaning. Software that links against both
5549** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
5550** SQLITE_TEXT.
5551*/
5552#define SQLITE_INTEGER1 1
5553#define SQLITE_FLOAT2 2
5554#define SQLITE_BLOB4 4
5555#define SQLITE_NULL5 5
5556#ifdef SQLITE_TEXT3
5557# undef SQLITE_TEXT3
5558#else
5559# define SQLITE_TEXT3 3
5560#endif
5561#define SQLITE3_TEXT3 3
5562
5563/*
5564** CAPI3REF: Result Values From A Query
5565** KEYWORDS: {column access functions}
5566** METHOD: sqlite3_stmt
5567**
5568** <b>Summary:</b>
5569** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5570** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
5571** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
5572** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5573** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5574** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5575** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5576** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
5577** [sqlite3_value|unprotected sqlite3_value] object.
5578** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5579** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
5580** or a UTF-8 TEXT result in bytes
5581** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
5582** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5583** TEXT in bytes
5584** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
5585** datatype of the result
5586** </table></blockquote>
5587**
5588** <b>Details:</b>
5589**
5590** ^These routines return information about a single column of the current
5591** result row of a query. ^In every case the first argument is a pointer
5592** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
5593** that was returned from [sqlite3_prepare_v2()] or one of its variants)
5594** and the second argument is the index of the column for which information
5595** should be returned. ^The leftmost column of the result set has the index 0.
5596** ^The number of columns in the result can be determined using
5597** [sqlite3_column_count()].
5598**
5599** If the SQL statement does not currently point to a valid row, or if the
5600** column index is out of range, the result is undefined.
5601** These routines may only be called when the most recent call to
5602** [sqlite3_step()] has returned [SQLITE_ROW] and neither
5603** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
5604** If any of these routines are called after [sqlite3_reset()] or
5605** [sqlite3_finalize()] or after [sqlite3_step()] has returned
5606** something other than [SQLITE_ROW], the results are undefined.
5607** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
5608** are called from a different thread while any of these routines
5609** are pending, then the results are undefined.
5610**
5611** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
5612** each return the value of a result column in a specific data format. If
5613** the result column is not initially in the requested format (for example,
5614** if the query returns an integer but the sqlite3_column_text() interface
5615** is used to extract the value) then an automatic type conversion is performed.
5616**
5617** ^The sqlite3_column_type() routine returns the
5618** [SQLITE_INTEGER | datatype code] for the initial data type
5619** of the result column. ^The returned value is one of [SQLITE_INTEGER],
5620** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
5621** The return value of sqlite3_column_type() can be used to decide which
5622** of the first six interface should be used to extract the column value.
5623** The value returned by sqlite3_column_type() is only meaningful if no
5624** automatic type conversions have occurred for the value in question.
5625** After a type conversion, the result of calling sqlite3_column_type()
5626** is undefined, though harmless. Future
5627** versions of SQLite may change the behavior of sqlite3_column_type()
5628** following a type conversion.
5629**
5630** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
5631** or sqlite3_column_bytes16() interfaces can be used to determine the size
5632** of that BLOB or string.
5633**
5634** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5635** routine returns the number of bytes in that BLOB or string.
5636** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5637** the string to UTF-8 and then returns the number of bytes.
5638** ^If the result is a numeric value then sqlite3_column_bytes() uses
5639** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5640** the number of bytes in that string.
5641** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
5642**
5643** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5644** routine returns the number of bytes in that BLOB or string.
5645** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5646** the string to UTF-16 and then returns the number of bytes.
5647** ^If the result is a numeric value then sqlite3_column_bytes16() uses
5648** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5649** the number of bytes in that string.
5650** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
5651**
5652** ^The values returned by [sqlite3_column_bytes()] and
5653** [sqlite3_column_bytes16()] do not include the zero terminators at the end
5654** of the string. ^For clarity: the values returned by
5655** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
5656** bytes in the string, not the number of characters.
5657**
5658** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
5659** even empty strings, are always zero-terminated. ^The return
5660** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5661**
5662** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
5663** [unprotected sqlite3_value] object. In a multithreaded environment,
5664** an unprotected sqlite3_value object may only be used safely with
5665** [sqlite3_bind_value()] and [sqlite3_result_value()].
5666** If the [unprotected sqlite3_value] object returned by
5667** [sqlite3_column_value()] is used in any other way, including calls
5668** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
5669** or [sqlite3_value_bytes()], the behavior is not threadsafe.
5670** Hence, the sqlite3_column_value() interface
5671** is normally only useful within the implementation of
5672** [application-defined SQL functions] or [virtual tables], not within
5673** top-level application code.
5674**
5675** The these routines may attempt to convert the datatype of the result.
5676** ^For example, if the internal representation is FLOAT and a text result
5677** is requested, [sqlite3_snprintf()] is used internally to perform the
5678** conversion automatically. ^(The following table details the conversions
5679** that are applied:
5680**
5681** <blockquote>
5682** <table border="1">
5683** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
5684**
5685** <tr><td> NULL <td> INTEGER <td> Result is 0
5686** <tr><td> NULL <td> FLOAT <td> Result is 0.0
5687** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer
5688** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer
5689** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
5690** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
5691** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5692** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER
5693** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
5694** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB
5695** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER
5696** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL
5697** <tr><td> TEXT <td> BLOB <td> No change
5698** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER
5699** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL
5700** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
5701** </table>
5702** </blockquote>)^
5703**
5704** Note that when type conversions occur, pointers returned by prior
5705** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
5706** sqlite3_column_text16() may be invalidated.
5707** Type conversions and pointer invalidations might occur
5708** in the following cases:
5709**
5710** <ul>
5711** <li> The initial content is a BLOB and sqlite3_column_text() or
5712** sqlite3_column_text16() is called. A zero-terminator might
5713** need to be added to the string.</li>
5714** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5715** sqlite3_column_text16() is called. The content must be converted
5716** to UTF-16.</li>
5717** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5718** sqlite3_column_text() is called. The content must be converted
5719** to UTF-8.</li>
5720** </ul>
5721**
5722** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5723** not invalidate a prior pointer, though of course the content of the buffer
5724** that the prior pointer references will have been modified. Other kinds
5725** of conversion are done in place when it is possible, but sometimes they
5726** are not possible and in those cases prior pointers are invalidated.
5727**
5728** The safest policy is to invoke these routines
5729** in one of the following ways:
5730**
5731** <ul>
5732** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
5733** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
5734** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
5735** </ul>
5736**
5737** In other words, you should call sqlite3_column_text(),
5738** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
5739** into the desired format, then invoke sqlite3_column_bytes() or
5740** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
5741** to sqlite3_column_text() or sqlite3_column_blob() with calls to
5742** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
5743** with calls to sqlite3_column_bytes().
5744**
5745** ^The pointers returned are valid until a type conversion occurs as
5746** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
5747** [sqlite3_finalize()] is called. ^The memory space used to hold strings
5748** and BLOBs is freed automatically. Do not pass the pointers returned
5749** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
5750** [sqlite3_free()].
5751**
5752** As long as the input parameters are correct, these routines will only
5753** fail if an out-of-memory error occurs during a format conversion.
5754** Only the following subset of interfaces are subject to out-of-memory
5755** errors:
5756**
5757** <ul>
5758** <li> sqlite3_column_blob()
5759** <li> sqlite3_column_text()
5760** <li> sqlite3_column_text16()
5761** <li> sqlite3_column_bytes()
5762** <li> sqlite3_column_bytes16()
5763** </ul>
5764**
5765** If an out-of-memory error occurs, then the return value from these
5766** routines is the same as if the column had contained an SQL NULL value.
5767** Valid SQL NULL returns can be distinguished from out-of-memory errors
5768** by invoking the [sqlite3_errcode()] immediately after the suspect
5769** return value is obtained and before any
5770** other SQLite interface is called on the same [database connection].
5771*/
5772SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
5773SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
5774SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
5775SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
5776SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
5777SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
5778SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
5779SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
5780SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
5781SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
5782
5783/*
5784** CAPI3REF: Destroy A Prepared Statement Object
5785** DESTRUCTOR: sqlite3_stmt
5786**
5787** ^The sqlite3_finalize() function is called to delete a [prepared statement].
5788** ^If the most recent evaluation of the statement encountered no errors
5789** or if the statement is never been evaluated, then sqlite3_finalize() returns
5790** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
5791** sqlite3_finalize(S) returns the appropriate [error code] or
5792** [extended error code].
5793**
5794** ^The sqlite3_finalize(S) routine can be called at any point during
5795** the life cycle of [prepared statement] S:
5796** before statement S is ever evaluated, after
5797** one or more calls to [sqlite3_reset()], or after any call
5798** to [sqlite3_step()] regardless of whether or not the statement has
5799** completed execution.
5800**
5801** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5802**
5803** The application must finalize every [prepared statement] in order to avoid
5804** resource leaks. It is a grievous error for the application to try to use
5805** a prepared statement after it has been finalized. Any use of a prepared
5806** statement after it has been finalized can result in undefined and
5807** undesirable behavior such as segfaults and heap corruption.
5808*/
5809SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5810
5811/*
5812** CAPI3REF: Reset A Prepared Statement Object
5813** METHOD: sqlite3_stmt
5814**
5815** The sqlite3_reset() function is called to reset a [prepared statement]
5816** object back to its initial state, ready to be re-executed.
5817** ^Any SQL statement variables that had values bound to them using
5818** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
5819** Use [sqlite3_clear_bindings()] to reset the bindings.
5820**
5821** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5822** back to the beginning of its program.
5823**
5824** ^If the most recent call to [sqlite3_step(S)] for the
5825** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
5826** or if [sqlite3_step(S)] has never before been called on S,
5827** then [sqlite3_reset(S)] returns [SQLITE_OK].
5828**
5829** ^If the most recent call to [sqlite3_step(S)] for the
5830** [prepared statement] S indicated an error, then
5831** [sqlite3_reset(S)] returns an appropriate [error code].
5832**
5833** ^The [sqlite3_reset(S)] interface does not change the values
5834** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5835*/
5836SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5837
5838/*
5839** CAPI3REF: Create Or Redefine SQL Functions
5840** KEYWORDS: {function creation routines}
5841** KEYWORDS: {application-defined SQL function}
5842** KEYWORDS: {application-defined SQL functions}
5843** METHOD: sqlite3
5844**
5845** ^These functions (collectively known as "function creation routines")
5846** are used to add SQL functions or aggregates or to redefine the behavior
5847** of existing SQL functions or aggregates. The only differences between
5848** the three "sqlite3_create_function*" routines are the text encoding
5849** expected for the second parameter (the name of the function being
5850** created) and the presence or absence of a destructor callback for
5851** the application data pointer. Function sqlite3_create_window_function()
5852** is similar, but allows the user to supply the extra callback functions
5853** needed by [aggregate window functions].
5854**
5855** ^The first parameter is the [database connection] to which the SQL
5856** function is to be added. ^If an application uses more than one database
5857** connection then application-defined SQL functions must be added
5858** to each database connection separately.
5859**
5860** ^The second parameter is the name of the SQL function to be created or
5861** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5862** representation, exclusive of the zero-terminator. ^Note that the name
5863** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5864** ^Any attempt to create a function with a longer name
5865** will result in [SQLITE_MISUSE] being returned.
5866**
5867** ^The third parameter (nArg)
5868** is the number of arguments that the SQL function or
5869** aggregate takes. ^If this parameter is -1, then the SQL function or
5870** aggregate may take any number of arguments between 0 and the limit
5871** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
5872** parameter is less than -1 or greater than 127 then the behavior is
5873** undefined.
5874**
5875** ^The fourth parameter, eTextRep, specifies what
5876** [SQLITE_UTF8 | text encoding] this SQL function prefers for
5877** its parameters. The application should set this parameter to
5878** [SQLITE_UTF16LE] if the function implementation invokes
5879** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
5880** implementation invokes [sqlite3_value_text16be()] on an input, or
5881** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
5882** otherwise. ^The same SQL function may be registered multiple times using
5883** different preferred text encodings, with different implementations for
5884** each encoding.
5885** ^When multiple implementations of the same function are available, SQLite
5886** will pick the one that involves the least amount of data conversion.
5887**
5888** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
5889** to signal that the function will always return the same result given
5890** the same inputs within a single SQL statement. Most SQL functions are
5891** deterministic. The built-in [random()] SQL function is an example of a
5892** function that is not deterministic. The SQLite query planner is able to
5893** perform additional optimizations on deterministic functions, so use
5894** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5895**
5896** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5897** function can gain access to this pointer using [sqlite3_user_data()].)^
5898**
5899** ^The sixth, seventh and eighth parameters passed to the three
5900** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
5901** pointers to C-language functions that implement the SQL function or
5902** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5903** callback only; NULL pointers must be passed as the xStep and xFinal
5904** parameters. ^An aggregate SQL function requires an implementation of xStep
5905** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
5906** SQL function or aggregate, pass NULL pointers for all three function
5907** callbacks.
5908**
5909** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
5910** and xInverse) passed to sqlite3_create_window_function are pointers to
5911** C-language callbacks that implement the new function. xStep and xFinal
5912** must both be non-NULL. xValue and xInverse may either both be NULL, in
5913** which case a regular aggregate function is created, or must both be
5914** non-NULL, in which case the new function may be used as either an aggregate
5915** or aggregate window function. More details regarding the implementation
5916** of aggregate window functions are
5917** [user-defined window functions|available here].
5918**
5919** ^(If the final parameter to sqlite3_create_function_v2() or
5920** sqlite3_create_window_function() is not NULL, then it is destructor for
5921** the application data pointer. The destructor is invoked when the function
5922** is deleted, either by being overloaded or when the database connection
5923** closes.)^ ^The destructor is also invoked if the call to
5924** sqlite3_create_function_v2() fails. ^When the destructor callback is
5925** invoked, it is passed a single argument which is a copy of the application
5926** data pointer which was the fifth parameter to sqlite3_create_function_v2().
5927**
5928** ^It is permitted to register multiple implementations of the same
5929** functions with the same name but with either differing numbers of
5930** arguments or differing preferred text encodings. ^SQLite will use
5931** the implementation that most closely matches the way in which the
5932** SQL function is used. ^A function implementation with a non-negative
5933** nArg parameter is a better match than a function implementation with
5934** a negative nArg. ^A function where the preferred text encoding
5935** matches the database encoding is a better
5936** match than a function where the encoding is different.
5937** ^A function where the encoding difference is between UTF16le and UTF16be
5938** is a closer match than a function where the encoding difference is
5939** between UTF8 and UTF16.
5940**
5941** ^Built-in functions may be overloaded by new application-defined functions.
5942**
5943** ^An application-defined function is permitted to call other
5944** SQLite interfaces. However, such calls must not
5945** close the database connection nor finalize or reset the prepared
5946** statement in which the function is running.
5947*/
5948SQLITE_API int sqlite3_create_function(
5949 sqlite3 *db,
5950 const char *zFunctionName,
5951 int nArg,
5952 int eTextRep,
5953 void *pApp,
5954 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5955 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5956 void (*xFinal)(sqlite3_context*)
5957);
5958SQLITE_API int sqlite3_create_function16(
5959 sqlite3 *db,
5960 const void *zFunctionName,
5961 int nArg,
5962 int eTextRep,
5963 void *pApp,
5964 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5965 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5966 void (*xFinal)(sqlite3_context*)
5967);
5968SQLITE_API int sqlite3_create_function_v2(
5969 sqlite3 *db,
5970 const char *zFunctionName,
5971 int nArg,
5972 int eTextRep,
5973 void *pApp,
5974 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5975 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5976 void (*xFinal)(sqlite3_context*),
5977 void(*xDestroy)(void*)
5978);
5979SQLITE_API int sqlite3_create_window_function(
5980 sqlite3 *db,
5981 const char *zFunctionName,
5982 int nArg,
5983 int eTextRep,
5984 void *pApp,
5985 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5986 void (*xFinal)(sqlite3_context*),
5987 void (*xValue)(sqlite3_context*),
5988 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
5989 void(*xDestroy)(void*)
5990);
5991
5992/*
5993** CAPI3REF: Text Encodings
5994**
5995** These constant define integer codes that represent the various
5996** text encodings supported by SQLite.
5997*/
5998#define SQLITE_UTF81 1 /* IMP: R-37514-35566 */
5999#define SQLITE_UTF16LE2 2 /* IMP: R-03371-37637 */
6000#define SQLITE_UTF16BE3 3 /* IMP: R-51971-34154 */
6001#define SQLITE_UTF164 4 /* Use native byte order */
6002#define SQLITE_ANY5 5 /* Deprecated */
6003#define SQLITE_UTF16_ALIGNED8 8 /* sqlite3_create_collation only */
6004
6005/*
6006** CAPI3REF: Function Flags
6007**
6008** These constants may be ORed together with the
6009** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
6010** to [sqlite3_create_function()], [sqlite3_create_function16()], or
6011** [sqlite3_create_function_v2()].
6012*/
6013#define SQLITE_DETERMINISTIC0x800 0x800
6014
6015/*
6016** CAPI3REF: Deprecated Functions
6017** DEPRECATED
6018**
6019** These functions are [deprecated]. In order to maintain
6020** backwards compatibility with older code, these functions continue
6021** to be supported. However, new applications should avoid
6022** the use of these functions. To encourage programmers to avoid
6023** these functions, we will not explain what they do.
6024*/
6025#ifndef SQLITE_OMIT_DEPRECATED
6026SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
6027SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
6028SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
6029SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
6030SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
6031SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
6032 void*,sqlite3_int64);
6033#endif
6034
6035/*
6036** CAPI3REF: Obtaining SQL Values
6037** METHOD: sqlite3_value
6038**
6039** <b>Summary:</b>
6040** <blockquote><table border=0 cellpadding=0 cellspacing=0>
6041** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
6042** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
6043** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
6044** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
6045** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
6046** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
6047** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
6048** the native byteorder
6049** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
6050** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
6051** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
6052** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
6053** or a UTF-8 TEXT in bytes
6054** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
6055** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
6056** TEXT in bytes
6057** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
6058** datatype of the value
6059** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
6060** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
6061** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
6062** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
6063** against a virtual table.
6064** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
6065** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
6066** </table></blockquote>
6067**
6068** <b>Details:</b>
6069**
6070** These routines extract type, size, and content information from
6071** [protected sqlite3_value] objects. Protected sqlite3_value objects
6072** are used to pass parameter information into implementation of
6073** [application-defined SQL functions] and [virtual tables].
6074**
6075** These routines work only with [protected sqlite3_value] objects.
6076** Any attempt to use these routines on an [unprotected sqlite3_value]
6077** is not threadsafe.
6078**
6079** ^These routines work just like the corresponding [column access functions]
6080** except that these routines take a single [protected sqlite3_value] object
6081** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
6082**
6083** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6084** in the native byte-order of the host machine. ^The
6085** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
6086** extract UTF-16 strings as big-endian and little-endian respectively.
6087**
6088** ^If [sqlite3_value] object V was initialized
6089** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
6090** and if X and Y are strings that compare equal according to strcmp(X,Y),
6091** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise,
6092** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
6093** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6094**
6095** ^(The sqlite3_value_type(V) interface returns the
6096** [SQLITE_INTEGER | datatype code] for the initial datatype of the
6097** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
6098** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
6099** Other interfaces might change the datatype for an sqlite3_value object.
6100** For example, if the datatype is initially SQLITE_INTEGER and
6101** sqlite3_value_text(V) is called to extract a text value for that
6102** integer, then subsequent calls to sqlite3_value_type(V) might return
6103** SQLITE_TEXT. Whether or not a persistent internal datatype conversion
6104** occurs is undefined and may change from one release of SQLite to the next.
6105**
6106** ^(The sqlite3_value_numeric_type() interface attempts to apply
6107** numeric affinity to the value. This means that an attempt is
6108** made to convert the value to an integer or floating point. If
6109** such a conversion is possible without loss of information (in other
6110** words, if the value is a string that looks like a number)
6111** then the conversion is performed. Otherwise no conversion occurs.
6112** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
6113**
6114** ^Within the [xUpdate] method of a [virtual table], the
6115** sqlite3_value_nochange(X) interface returns true if and only if
6116** the column corresponding to X is unchanged by the UPDATE operation
6117** that the xUpdate method call was invoked to implement and if
6118** and the prior [xColumn] method call that was invoked to extracted
6119** the value for that column returned without setting a result (probably
6120** because it queried [sqlite3_vtab_nochange()] and found that the column
6121** was unchanging). ^Within an [xUpdate] method, any value for which
6122** sqlite3_value_nochange(X) is true will in all other respects appear
6123** to be a NULL value. If sqlite3_value_nochange(X) is invoked anywhere other
6124** than within an [xUpdate] method call for an UPDATE statement, then
6125** the return value is arbitrary and meaningless.
6126**
6127** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6128** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
6129** interfaces. ^If X comes from an SQL literal value, or a table column,
6130** and expression, then sqlite3_value_frombind(X) returns zero.
6131**
6132** Please pay particular attention to the fact that the pointer returned
6133** from [sqlite3_value_blob()], [sqlite3_value_text()], or
6134** [sqlite3_value_text16()] can be invalidated by a subsequent call to
6135** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
6136** or [sqlite3_value_text16()].
6137**
6138** These routines must be called from the same thread as
6139** the SQL function that supplied the [sqlite3_value*] parameters.
6140**
6141** As long as the input parameter is correct, these routines can only
6142** fail if an out-of-memory error occurs during a format conversion.
6143** Only the following subset of interfaces are subject to out-of-memory
6144** errors:
6145**
6146** <ul>
6147** <li> sqlite3_value_blob()
6148** <li> sqlite3_value_text()
6149** <li> sqlite3_value_text16()
6150** <li> sqlite3_value_text16le()
6151** <li> sqlite3_value_text16be()
6152** <li> sqlite3_value_bytes()
6153** <li> sqlite3_value_bytes16()
6154** </ul>
6155**
6156** If an out-of-memory error occurs, then the return value from these
6157** routines is the same as if the column had contained an SQL NULL value.
6158** Valid SQL NULL returns can be distinguished from out-of-memory errors
6159** by invoking the [sqlite3_errcode()] immediately after the suspect
6160** return value is obtained and before any
6161** other SQLite interface is called on the same [database connection].
6162*/
6163SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
6164SQLITE_API double sqlite3_value_double(sqlite3_value*);
6165SQLITE_API int sqlite3_value_int(sqlite3_value*);
6166SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
6167SQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
6168SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
6169SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
6170SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
6171SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
6172SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
6173SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
6174SQLITE_API int sqlite3_value_type(sqlite3_value*);
6175SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
6176SQLITE_API int sqlite3_value_nochange(sqlite3_value*);
6177SQLITE_API int sqlite3_value_frombind(sqlite3_value*);
6178
6179/*
6180** CAPI3REF: Finding The Subtype Of SQL Values
6181** METHOD: sqlite3_value
6182**
6183** The sqlite3_value_subtype(V) function returns the subtype for
6184** an [application-defined SQL function] argument V. The subtype
6185** information can be used to pass a limited amount of context from
6186** one SQL function to another. Use the [sqlite3_result_subtype()]
6187** routine to set the subtype for the return value of an SQL function.
6188*/
6189SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6190
6191/*
6192** CAPI3REF: Copy And Free SQL Values
6193** METHOD: sqlite3_value
6194**
6195** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
6196** object D and returns a pointer to that copy. ^The [sqlite3_value] returned
6197** is a [protected sqlite3_value] object even if the input is not.
6198** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
6199** memory allocation fails.
6200**
6201** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
6202** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer
6203** then sqlite3_value_free(V) is a harmless no-op.
6204*/
6205SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
6206SQLITE_API void sqlite3_value_free(sqlite3_value*);
6207
6208/*
6209** CAPI3REF: Obtain Aggregate Function Context
6210** METHOD: sqlite3_context
6211**
6212** Implementations of aggregate SQL functions use this
6213** routine to allocate memory for storing their state.
6214**
6215** ^The first time the sqlite3_aggregate_context(C,N) routine is called
6216** for a particular aggregate function, SQLite
6217** allocates N of memory, zeroes out that memory, and returns a pointer
6218** to the new memory. ^On second and subsequent calls to
6219** sqlite3_aggregate_context() for the same aggregate function instance,
6220** the same buffer is returned. Sqlite3_aggregate_context() is normally
6221** called once for each invocation of the xStep callback and then one
6222** last time when the xFinal callback is invoked. ^(When no rows match
6223** an aggregate query, the xStep() callback of the aggregate function
6224** implementation is never called and xFinal() is called exactly once.
6225** In those cases, sqlite3_aggregate_context() might be called for the
6226** first time from within xFinal().)^
6227**
6228** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
6229** when first called if N is less than or equal to zero or if a memory
6230** allocate error occurs.
6231**
6232** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
6233** determined by the N parameter on first successful call. Changing the
6234** value of N in subsequent call to sqlite3_aggregate_context() within
6235** the same aggregate function instance will not resize the memory
6236** allocation.)^ Within the xFinal callback, it is customary to set
6237** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
6238** pointless memory allocations occur.
6239**
6240** ^SQLite automatically frees the memory allocated by
6241** sqlite3_aggregate_context() when the aggregate query concludes.
6242**
6243** The first parameter must be a copy of the
6244** [sqlite3_context | SQL function context] that is the first parameter
6245** to the xStep or xFinal callback routine that implements the aggregate
6246** function.
6247**
6248** This routine must be called from the same thread in which
6249** the aggregate SQL function is running.
6250*/
6251SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
6252
6253/*
6254** CAPI3REF: User Data For Functions
6255** METHOD: sqlite3_context
6256**
6257** ^The sqlite3_user_data() interface returns a copy of
6258** the pointer that was the pUserData parameter (the 5th parameter)
6259** of the [sqlite3_create_function()]
6260** and [sqlite3_create_function16()] routines that originally
6261** registered the application defined function.
6262**
6263** This routine must be called from the same thread in which
6264** the application-defined function is running.
6265*/
6266SQLITE_API void *sqlite3_user_data(sqlite3_context*);
6267
6268/*
6269** CAPI3REF: Database Connection For Functions
6270** METHOD: sqlite3_context
6271**
6272** ^The sqlite3_context_db_handle() interface returns a copy of
6273** the pointer to the [database connection] (the 1st parameter)
6274** of the [sqlite3_create_function()]
6275** and [sqlite3_create_function16()] routines that originally
6276** registered the application defined function.
6277*/
6278SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6279
6280/*
6281** CAPI3REF: Function Auxiliary Data
6282** METHOD: sqlite3_context
6283**
6284** These functions may be used by (non-aggregate) SQL functions to
6285** associate metadata with argument values. If the same value is passed to
6286** multiple invocations of the same SQL function during query execution, under
6287** some circumstances the associated metadata may be preserved. An example
6288** of where this might be useful is in a regular-expression matching
6289** function. The compiled version of the regular expression can be stored as
6290** metadata associated with the pattern string.
6291** Then as long as the pattern string remains the same,
6292** the compiled regular expression can be reused on multiple
6293** invocations of the same function.
6294**
6295** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
6296** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6297** value to the application-defined function. ^N is zero for the left-most
6298** function argument. ^If there is no metadata
6299** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6300** returns a NULL pointer.
6301**
6302** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6303** argument of the application-defined function. ^Subsequent
6304** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6305** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
6306** NULL if the metadata has been discarded.
6307** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6308** SQLite will invoke the destructor function X with parameter P exactly
6309** once, when the metadata is discarded.
6310** SQLite is free to discard the metadata at any time, including: <ul>
6311** <li> ^(when the corresponding function parameter changes)^, or
6312** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6313** SQL statement)^, or
6314** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6315** parameter)^, or
6316** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6317** allocation error occurs.)^ </ul>
6318**
6319** Note the last bullet in particular. The destructor X in
6320** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6321** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6322** should be called near the end of the function implementation and the
6323** function implementation should not make any use of P after
6324** sqlite3_set_auxdata() has been called.
6325**
6326** ^(In practice, metadata is preserved between function calls for
6327** function parameters that are compile-time constants, including literal
6328** values and [parameters] and expressions composed from the same.)^
6329**
6330** The value of the N parameter to these interfaces should be non-negative.
6331** Future enhancements may make use of negative N values to define new
6332** kinds of function caching behavior.
6333**
6334** These routines must be called from the same thread in which
6335** the SQL function is running.
6336*/
6337SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6338SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6339
6340
6341/*
6342** CAPI3REF: Constants Defining Special Destructor Behavior
6343**
6344** These are special values for the destructor that is passed in as the
6345** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
6346** argument is SQLITE_STATIC, it means that the content pointer is constant
6347** and will never change. It does not need to be destroyed. ^The
6348** SQLITE_TRANSIENT value means that the content will likely change in
6349** the near future and that SQLite should make its own private copy of
6350** the content before returning.
6351**
6352** The typedef is necessary to work around problems in certain
6353** C++ compilers.
6354*/
6355typedef void (*sqlite3_destructor_type)(void*);
6356#define SQLITE_STATIC((sqlite3_destructor_type)0) ((sqlite3_destructor_type)0)
6357#define SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ((sqlite3_destructor_type)-1)
6358
6359/*
6360** CAPI3REF: Setting The Result Of An SQL Function
6361** METHOD: sqlite3_context
6362**
6363** These routines are used by the xFunc or xFinal callbacks that
6364** implement SQL functions and aggregates. See
6365** [sqlite3_create_function()] and [sqlite3_create_function16()]
6366** for additional information.
6367**
6368** These functions work very much like the [parameter binding] family of
6369** functions used to bind values to host parameters in prepared statements.
6370** Refer to the [SQL parameter] documentation for additional information.
6371**
6372** ^The sqlite3_result_blob() interface sets the result from
6373** an application-defined function to be the BLOB whose content is pointed
6374** to by the second parameter and which is N bytes long where N is the
6375** third parameter.
6376**
6377** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
6378** interfaces set the result of the application-defined function to be
6379** a BLOB containing all zero bytes and N bytes in size.
6380**
6381** ^The sqlite3_result_double() interface sets the result from
6382** an application-defined function to be a floating point value specified
6383** by its 2nd argument.
6384**
6385** ^The sqlite3_result_error() and sqlite3_result_error16() functions
6386** cause the implemented SQL function to throw an exception.
6387** ^SQLite uses the string pointed to by the
6388** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
6389** as the text of an error message. ^SQLite interprets the error
6390** message string from sqlite3_result_error() as UTF-8. ^SQLite
6391** interprets the string from sqlite3_result_error16() as UTF-16 in native
6392** byte order. ^If the third parameter to sqlite3_result_error()
6393** or sqlite3_result_error16() is negative then SQLite takes as the error
6394** message all text up through the first zero character.
6395** ^If the third parameter to sqlite3_result_error() or
6396** sqlite3_result_error16() is non-negative then SQLite takes that many
6397** bytes (not characters) from the 2nd parameter as the error message.
6398** ^The sqlite3_result_error() and sqlite3_result_error16()
6399** routines make a private copy of the error message text before
6400** they return. Hence, the calling function can deallocate or
6401** modify the text after they return without harm.
6402** ^The sqlite3_result_error_code() function changes the error code
6403** returned by SQLite as a result of an error in a function. ^By default,
6404** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
6405** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
6406**
6407** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
6408** error indicating that a string or BLOB is too long to represent.
6409**
6410** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
6411** error indicating that a memory allocation failed.
6412**
6413** ^The sqlite3_result_int() interface sets the return value
6414** of the application-defined function to be the 32-bit signed integer
6415** value given in the 2nd argument.
6416** ^The sqlite3_result_int64() interface sets the return value
6417** of the application-defined function to be the 64-bit signed integer
6418** value given in the 2nd argument.
6419**
6420** ^The sqlite3_result_null() interface sets the return value
6421** of the application-defined function to be NULL.
6422**
6423** ^The sqlite3_result_text(), sqlite3_result_text16(),
6424** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
6425** set the return value of the application-defined function to be
6426** a text string which is represented as UTF-8, UTF-16 native byte order,
6427** UTF-16 little endian, or UTF-16 big endian, respectively.
6428** ^The sqlite3_result_text64() interface sets the return value of an
6429** application-defined function to be a text string in an encoding
6430** specified by the fifth (and last) parameter, which must be one
6431** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
6432** ^SQLite takes the text result from the application from
6433** the 2nd parameter of the sqlite3_result_text* interfaces.
6434** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6435** is negative, then SQLite takes result text from the 2nd parameter
6436** through the first zero character.
6437** ^If the 3rd parameter to the sqlite3_result_text* interfaces
6438** is non-negative, then as many bytes (not characters) of the text
6439** pointed to by the 2nd parameter are taken as the application-defined
6440** function result. If the 3rd parameter is non-negative, then it
6441** must be the byte offset into the string where the NUL terminator would
6442** appear if the string where NUL terminated. If any NUL characters occur
6443** in the string at a byte offset that is less than the value of the 3rd
6444** parameter, then the resulting string will contain embedded NULs and the
6445** result of expressions operating on strings with embedded NULs is undefined.
6446** ^If the 4th parameter to the sqlite3_result_text* interfaces
6447** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6448** function as the destructor on the text or BLOB result when it has
6449** finished using that result.
6450** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
6451** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
6452** assumes that the text or BLOB result is in constant space and does not
6453** copy the content of the parameter nor call a destructor on the content
6454** when it has finished using that result.
6455** ^If the 4th parameter to the sqlite3_result_text* interfaces
6456** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
6457** then SQLite makes a copy of the result into space obtained
6458** from [sqlite3_malloc()] before it returns.
6459**
6460** ^The sqlite3_result_value() interface sets the result of
6461** the application-defined function to be a copy of the
6462** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
6463** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
6464** so that the [sqlite3_value] specified in the parameter may change or
6465** be deallocated after sqlite3_result_value() returns without harm.
6466** ^A [protected sqlite3_value] object may always be used where an
6467** [unprotected sqlite3_value] object is required, so either
6468** kind of [sqlite3_value] object can be used with this interface.
6469**
6470** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
6471** SQL NULL value, just like [sqlite3_result_null(C)], except that it
6472** also associates the host-language pointer P or type T with that
6473** NULL value such that the pointer can be retrieved within an
6474** [application-defined SQL function] using [sqlite3_value_pointer()].
6475** ^If the D parameter is not NULL, then it is a pointer to a destructor
6476** for the P parameter. ^SQLite invokes D with P as its only argument
6477** when SQLite is finished with P. The T parameter should be a static
6478** string and preferably a string literal. The sqlite3_result_pointer()
6479** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
6480**
6481** If these routines are called from within the different thread
6482** than the one containing the application-defined function that received
6483** the [sqlite3_context] pointer, the results are undefined.
6484*/
6485SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
6486SQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
6487 sqlite3_uint64,void(*)(void*));
6488SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
6489SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
6490SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
6491SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
6492SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
6493SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
6494SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
6495SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
6496SQLITE_API void sqlite3_result_null(sqlite3_context*);
6497SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
6498SQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
6499 void(*)(void*), unsigned char encoding);
6500SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
6501SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
6502SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
6503SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
6504SQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
6505SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
6506SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6507
6508
6509/*
6510** CAPI3REF: Setting The Subtype Of An SQL Function
6511** METHOD: sqlite3_context
6512**
6513** The sqlite3_result_subtype(C,T) function causes the subtype of
6514** the result from the [application-defined SQL function] with
6515** [sqlite3_context] C to be the value T. Only the lower 8 bits
6516** of the subtype T are preserved in current versions of SQLite;
6517** higher order bits are discarded.
6518** The number of subtype bytes preserved by SQLite might increase
6519** in future releases of SQLite.
6520*/
6521SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6522
6523/*
6524** CAPI3REF: Define New Collating Sequences
6525** METHOD: sqlite3
6526**
6527** ^These functions add, remove, or modify a [collation] associated
6528** with the [database connection] specified as the first argument.
6529**
6530** ^The name of the collation is a UTF-8 string
6531** for sqlite3_create_collation() and sqlite3_create_collation_v2()
6532** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6533** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
6534** considered to be the same name.
6535**
6536** ^(The third argument (eTextRep) must be one of the constants:
6537** <ul>
6538** <li> [SQLITE_UTF8],
6539** <li> [SQLITE_UTF16LE],
6540** <li> [SQLITE_UTF16BE],
6541** <li> [SQLITE_UTF16], or
6542** <li> [SQLITE_UTF16_ALIGNED].
6543** </ul>)^
6544** ^The eTextRep argument determines the encoding of strings passed
6545** to the collating function callback, xCallback.
6546** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
6547** force strings to be UTF16 with native byte order.
6548** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
6549** on an even byte address.
6550**
6551** ^The fourth argument, pArg, is an application data pointer that is passed
6552** through as the first argument to the collating function callback.
6553**
6554** ^The fifth argument, xCallback, is a pointer to the collating function.
6555** ^Multiple collating functions can be registered using the same name but
6556** with different eTextRep parameters and SQLite will use whichever
6557** function requires the least amount of data transformation.
6558** ^If the xCallback argument is NULL then the collating function is
6559** deleted. ^When all collating functions having the same name are deleted,
6560** that collation is no longer usable.
6561**
6562** ^The collating function callback is invoked with a copy of the pArg
6563** application data pointer and with two strings in the encoding specified
6564** by the eTextRep argument. The collating function must return an
6565** integer that is negative, zero, or positive
6566** if the first string is less than, equal to, or greater than the second,
6567** respectively. A collating function must always return the same answer
6568** given the same inputs. If two or more collating functions are registered
6569** to the same collation name (using different eTextRep values) then all
6570** must give an equivalent answer when invoked with equivalent strings.
6571** The collating function must obey the following properties for all
6572** strings A, B, and C:
6573**
6574** <ol>
6575** <li> If A==B then B==A.
6576** <li> If A==B and B==C then A==C.
6577** <li> If A&lt;B THEN B&gt;A.
6578** <li> If A&lt;B and B&lt;C then A&lt;C.
6579** </ol>
6580**
6581** If a collating function fails any of the above constraints and that
6582** collating function is registered and used, then the behavior of SQLite
6583** is undefined.
6584**
6585** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
6586** with the addition that the xDestroy callback is invoked on pArg when
6587** the collating function is deleted.
6588** ^Collating functions are deleted when they are overridden by later
6589** calls to the collation creation functions or when the
6590** [database connection] is closed using [sqlite3_close()].
6591**
6592** ^The xDestroy callback is <u>not</u> called if the
6593** sqlite3_create_collation_v2() function fails. Applications that invoke
6594** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6595** check the return code and dispose of the application data pointer
6596** themselves rather than expecting SQLite to deal with it for them.
6597** This is different from every other SQLite interface. The inconsistency
6598** is unfortunate but cannot be changed without breaking backwards
6599** compatibility.
6600**
6601** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
6602*/
6603SQLITE_API int sqlite3_create_collation(
6604 sqlite3*,
6605 const char *zName,
6606 int eTextRep,
6607 void *pArg,
6608 int(*xCompare)(void*,int,const void*,int,const void*)
6609);
6610SQLITE_API int sqlite3_create_collation_v2(
6611 sqlite3*,
6612 const char *zName,
6613 int eTextRep,
6614 void *pArg,
6615 int(*xCompare)(void*,int,const void*,int,const void*),
6616 void(*xDestroy)(void*)
6617);
6618SQLITE_API int sqlite3_create_collation16(
6619 sqlite3*,
6620 const void *zName,
6621 int eTextRep,
6622 void *pArg,
6623 int(*xCompare)(void*,int,const void*,int,const void*)
6624);
6625
6626/*
6627** CAPI3REF: Collation Needed Callbacks
6628** METHOD: sqlite3
6629**
6630** ^To avoid having to register all collation sequences before a database
6631** can be used, a single callback function may be registered with the
6632** [database connection] to be invoked whenever an undefined collation
6633** sequence is required.
6634**
6635** ^If the function is registered using the sqlite3_collation_needed() API,
6636** then it is passed the names of undefined collation sequences as strings
6637** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6638** the names are passed as UTF-16 in machine native byte order.
6639** ^A call to either function replaces the existing collation-needed callback.
6640**
6641** ^(When the callback is invoked, the first argument passed is a copy
6642** of the second argument to sqlite3_collation_needed() or
6643** sqlite3_collation_needed16(). The second argument is the database
6644** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
6645** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
6646** sequence function required. The fourth parameter is the name of the
6647** required collation sequence.)^
6648**
6649** The callback function should register the desired collation using
6650** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
6651** [sqlite3_create_collation_v2()].
6652*/
6653SQLITE_API int sqlite3_collation_needed(
6654 sqlite3*,
6655 void*,
6656 void(*)(void*,sqlite3*,int eTextRep,const char*)
6657);
6658SQLITE_API int sqlite3_collation_needed16(
6659 sqlite3*,
6660 void*,
6661 void(*)(void*,sqlite3*,int eTextRep,const void*)
6662);
6663
6664#ifdef SQLITE_HAS_CODEC
6665/*
6666** Specify the key for an encrypted database. This routine should be
6667** called right after sqlite3_open().
6668**
6669** The code to implement this API is not available in the public release
6670** of SQLite.
6671*/
6672SQLITE_API int sqlite3_key(
6673 sqlite3 *db, /* Database to be rekeyed */
6674 const void *pKey, int nKey /* The key */
6675);
6676SQLITE_API int sqlite3_key_v2(
6677 sqlite3 *db, /* Database to be rekeyed */
6678 const char *zDbName, /* Name of the database */
6679 const void *pKey, int nKey /* The key */
6680);
6681
6682/*
6683** Change the key on an open database. If the current database is not
6684** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the
6685** database is decrypted.
6686**
6687** The code to implement this API is not available in the public release
6688** of SQLite.
6689*/
6690SQLITE_API int sqlite3_rekey(
6691 sqlite3 *db, /* Database to be rekeyed */
6692 const void *pKey, int nKey /* The new key */
6693);
6694SQLITE_API int sqlite3_rekey_v2(
6695 sqlite3 *db, /* Database to be rekeyed */
6696 const char *zDbName, /* Name of the database */
6697 const void *pKey, int nKey /* The new key */
6698);
6699
6700/*
6701** Specify the activation key for a SEE database. Unless
6702** activated, none of the SEE routines will work.
6703*/
6704SQLITE_API void sqlite3_activate_see(
6705 const char *zPassPhrase /* Activation phrase */
6706);
6707#endif
6708
6709#ifdef SQLITE_ENABLE_CEROD
6710/*
6711** Specify the activation key for a CEROD database. Unless
6712** activated, none of the CEROD routines will work.
6713*/
6714SQLITE_API void sqlite3_activate_cerod(
6715 const char *zPassPhrase /* Activation phrase */
6716);
6717#endif
6718
6719/*
6720** CAPI3REF: Suspend Execution For A Short Time
6721**
6722** The sqlite3_sleep() function causes the current thread to suspend execution
6723** for at least a number of milliseconds specified in its parameter.
6724**
6725** If the operating system does not support sleep requests with
6726** millisecond time resolution, then the time will be rounded up to
6727** the nearest second. The number of milliseconds of sleep actually
6728** requested from the operating system is returned.
6729**
6730** ^SQLite implements this interface by calling the xSleep()
6731** method of the default [sqlite3_vfs] object. If the xSleep() method
6732** of the default VFS is not implemented correctly, or not implemented at
6733** all, then the behavior of sqlite3_sleep() may deviate from the description
6734** in the previous paragraphs.
6735*/
6736SQLITE_API int sqlite3_sleep(int);
6737
6738/*
6739** CAPI3REF: Name Of The Folder Holding Temporary Files
6740**
6741** ^(If this global variable is made to point to a string which is
6742** the name of a folder (a.k.a. directory), then all temporary files
6743** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6744** will be placed in that directory.)^ ^If this variable
6745** is a NULL pointer, then SQLite performs a search for an appropriate
6746** temporary file directory.
6747**
6748** Applications are strongly discouraged from using this global variable.
6749** It is required to set a temporary folder on Windows Runtime (WinRT).
6750** But for all other platforms, it is highly recommended that applications
6751** neither read nor write this variable. This global variable is a relic
6752** that exists for backwards compatibility of legacy applications and should
6753** be avoided in new projects.
6754**
6755** It is not safe to read or modify this variable in more than one
6756** thread at a time. It is not safe to read or modify this variable
6757** if a [database connection] is being used at the same time in a separate
6758** thread.
6759** It is intended that this variable be set once
6760** as part of process initialization and before any SQLite interface
6761** routines have been called and that this variable remain unchanged
6762** thereafter.
6763**
6764** ^The [temp_store_directory pragma] may modify this variable and cause
6765** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6766** the [temp_store_directory pragma] always assumes that any string
6767** that this variable points to is held in memory obtained from
6768** [sqlite3_malloc] and the pragma may attempt to free that memory
6769** using [sqlite3_free].
6770** Hence, if this variable is modified directly, either it should be
6771** made NULL or made to point to memory obtained from [sqlite3_malloc]
6772** or else the use of the [temp_store_directory pragma] should be avoided.
6773** Except when requested by the [temp_store_directory pragma], SQLite
6774** does not free the memory that sqlite3_temp_directory points to. If
6775** the application wants that memory to be freed, it must do
6776** so itself, taking care to only do so after all [database connection]
6777** objects have been destroyed.
6778**
6779** <b>Note to Windows Runtime users:</b> The temporary directory must be set
6780** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various
6781** features that require the use of temporary files may fail. Here is an
6782** example of how to do this using C++ with the Windows Runtime:
6783**
6784** <blockquote><pre>
6785** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6786** &nbsp; TemporaryFolder->Path->Data();
6787** char zPathBuf&#91;MAX_PATH + 1&#93;;
6788** memset(zPathBuf, 0, sizeof(zPathBuf));
6789** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6790** &nbsp; NULL, NULL);
6791** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
6792** </pre></blockquote>
6793*/
6794SQLITE_API char *sqlite3_temp_directory;
6795
6796/*
6797** CAPI3REF: Name Of The Folder Holding Database Files
6798**
6799** ^(If this global variable is made to point to a string which is
6800** the name of a folder (a.k.a. directory), then all database files
6801** specified with a relative pathname and created or accessed by
6802** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6803** to be relative to that directory.)^ ^If this variable is a NULL
6804** pointer, then SQLite assumes that all database files specified
6805** with a relative pathname are relative to the current directory
6806** for the process. Only the windows VFS makes use of this global
6807** variable; it is ignored by the unix VFS.
6808**
6809** Changing the value of this variable while a database connection is
6810** open can result in a corrupt database.
6811**
6812** It is not safe to read or modify this variable in more than one
6813** thread at a time. It is not safe to read or modify this variable
6814** if a [database connection] is being used at the same time in a separate
6815** thread.
6816** It is intended that this variable be set once
6817** as part of process initialization and before any SQLite interface
6818** routines have been called and that this variable remain unchanged
6819** thereafter.
6820**
6821** ^The [data_store_directory pragma] may modify this variable and cause
6822** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
6823** the [data_store_directory pragma] always assumes that any string
6824** that this variable points to is held in memory obtained from
6825** [sqlite3_malloc] and the pragma may attempt to free that memory
6826** using [sqlite3_free].
6827** Hence, if this variable is modified directly, either it should be
6828** made NULL or made to point to memory obtained from [sqlite3_malloc]
6829** or else the use of the [data_store_directory pragma] should be avoided.
6830*/
6831SQLITE_API char *sqlite3_data_directory;
6832
6833/*
6834** CAPI3REF: Win32 Specific Interface
6835**
6836** These interfaces are available only on Windows. The
6837** [sqlite3_win32_set_directory] interface is used to set the value associated
6838** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6839** zValue, depending on the value of the type parameter. The zValue parameter
6840** should be NULL to cause the previous value to be freed via [sqlite3_free];
6841** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6842** prior to being used. The [sqlite3_win32_set_directory] interface returns
6843** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6844** or [SQLITE_NOMEM] if memory could not be allocated. The value of the
6845** [sqlite3_data_directory] variable is intended to act as a replacement for
6846** the current directory on the sub-platforms of Win32 where that concept is
6847** not present, e.g. WinRT and UWP. The [sqlite3_win32_set_directory8] and
6848** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6849** sqlite3_win32_set_directory interface except the string parameter must be
6850** UTF-8 or UTF-16, respectively.
6851*/
6852SQLITE_API int sqlite3_win32_set_directory(
6853 unsigned long type, /* Identifier for directory being set or reset */
6854 void *zValue /* New value for directory being set or reset */
6855);
6856SQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6857SQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6858
6859/*
6860** CAPI3REF: Win32 Directory Types
6861**
6862** These macros are only available on Windows. They define the allowed values
6863** for the type argument to the [sqlite3_win32_set_directory] interface.
6864*/
6865#define SQLITE_WIN32_DATA_DIRECTORY_TYPE1 1
6866#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE2 2
6867
6868/*
6869** CAPI3REF: Test For Auto-Commit Mode
6870** KEYWORDS: {autocommit mode}
6871** METHOD: sqlite3
6872**
6873** ^The sqlite3_get_autocommit() interface returns non-zero or
6874** zero if the given database connection is or is not in autocommit mode,
6875** respectively. ^Autocommit mode is on by default.
6876** ^Autocommit mode is disabled by a [BEGIN] statement.
6877** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6878**
6879** If certain kinds of errors occur on a statement within a multi-statement
6880** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
6881** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
6882** transaction might be rolled back automatically. The only way to
6883** find out whether SQLite automatically rolled back the transaction after
6884** an error is to use this function.
6885**
6886** If another thread changes the autocommit status of the database
6887** connection while this routine is running, then the return value
6888** is undefined.
6889*/
6890SQLITE_API int sqlite3_get_autocommit(sqlite3*);
6891
6892/*
6893** CAPI3REF: Find The Database Handle Of A Prepared Statement
6894** METHOD: sqlite3_stmt
6895**
6896** ^The sqlite3_db_handle interface returns the [database connection] handle
6897** to which a [prepared statement] belongs. ^The [database connection]
6898** returned by sqlite3_db_handle is the same [database connection]
6899** that was the first argument
6900** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6901** create the statement in the first place.
6902*/
6903SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6904
6905/*
6906** CAPI3REF: Return The Filename For A Database Connection
6907** METHOD: sqlite3
6908**
6909** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
6910** associated with database N of connection D. ^The main database file
6911** has the name "main". If there is no attached database N on the database
6912** connection D, or if database N is a temporary or in-memory database, then
6913** this function will return either a NULL pointer or an empty string.
6914**
6915** ^The filename returned by this function is the output of the
6916** xFullPathname method of the [VFS]. ^In other words, the filename
6917** will be an absolute pathname, even if the filename used
6918** to open the database originally was a URI or relative pathname.
6919*/
6920SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6921
6922/*
6923** CAPI3REF: Determine if a database is read-only
6924** METHOD: sqlite3
6925**
6926** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
6927** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6928** the name of a database on connection D.
6929*/
6930SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
6931
6932/*
6933** CAPI3REF: Find the next prepared statement
6934** METHOD: sqlite3
6935**
6936** ^This interface returns a pointer to the next [prepared statement] after
6937** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
6938** then this interface returns a pointer to the first prepared statement
6939** associated with the database connection pDb. ^If no prepared statement
6940** satisfies the conditions of this routine, it returns NULL.
6941**
6942** The [database connection] pointer D in a call to
6943** [sqlite3_next_stmt(D,S)] must refer to an open database
6944** connection and in particular must not be a NULL pointer.
6945*/
6946SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
6947
6948/*
6949** CAPI3REF: Commit And Rollback Notification Callbacks
6950** METHOD: sqlite3
6951**
6952** ^The sqlite3_commit_hook() interface registers a callback
6953** function to be invoked whenever a transaction is [COMMIT | committed].
6954** ^Any callback set by a previous call to sqlite3_commit_hook()
6955** for the same database connection is overridden.
6956** ^The sqlite3_rollback_hook() interface registers a callback
6957** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
6958** ^Any callback set by a previous call to sqlite3_rollback_hook()
6959** for the same database connection is overridden.
6960** ^The pArg argument is passed through to the callback.
6961** ^If the callback on a commit hook function returns non-zero,
6962** then the commit is converted into a rollback.
6963**
6964** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
6965** return the P argument from the previous call of the same function
6966** on the same [database connection] D, or NULL for
6967** the first call for each function on D.
6968**
6969** The commit and rollback hook callbacks are not reentrant.
6970** The callback implementation must not do anything that will modify
6971** the database connection that invoked the callback. Any actions
6972** to modify the database connection must be deferred until after the
6973** completion of the [sqlite3_step()] call that triggered the commit
6974** or rollback hook in the first place.
6975** Note that running any other SQL statements, including SELECT statements,
6976** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
6977** the database connections for the meaning of "modify" in this paragraph.
6978**
6979** ^Registering a NULL function disables the callback.
6980**
6981** ^When the commit hook callback routine returns zero, the [COMMIT]
6982** operation is allowed to continue normally. ^If the commit hook
6983** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6984** ^The rollback hook is invoked on a rollback that results from a commit
6985** hook returning non-zero, just as it would be with any other rollback.
6986**
6987** ^For the purposes of this API, a transaction is said to have been
6988** rolled back if an explicit "ROLLBACK" statement is executed, or
6989** an error or constraint causes an implicit rollback to occur.
6990** ^The rollback callback is not invoked if a transaction is
6991** automatically rolled back because the database connection is closed.
6992**
6993** See also the [sqlite3_update_hook()] interface.
6994*/
6995SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
6996SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
6997
6998/*
6999** CAPI3REF: Data Change Notification Callbacks
7000** METHOD: sqlite3
7001**
7002** ^The sqlite3_update_hook() interface registers a callback function
7003** with the [database connection] identified by the first argument
7004** to be invoked whenever a row is updated, inserted or deleted in
7005** a [rowid table].
7006** ^Any callback set by a previous call to this function
7007** for the same database connection is overridden.
7008**
7009** ^The second argument is a pointer to the function to invoke when a
7010** row is updated, inserted or deleted in a rowid table.
7011** ^The first argument to the callback is a copy of the third argument
7012** to sqlite3_update_hook().
7013** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
7014** or [SQLITE_UPDATE], depending on the operation that caused the callback
7015** to be invoked.
7016** ^The third and fourth arguments to the callback contain pointers to the
7017** database and table name containing the affected row.
7018** ^The final callback parameter is the [rowid] of the row.
7019** ^In the case of an update, this is the [rowid] after the update takes place.
7020**
7021** ^(The update hook is not invoked when internal system tables are
7022** modified (i.e. sqlite_master and sqlite_sequence).)^
7023** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
7024**
7025** ^In the current implementation, the update hook
7026** is not invoked when conflicting rows are deleted because of an
7027** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
7028** invoked when rows are deleted using the [truncate optimization].
7029** The exceptions defined in this paragraph might change in a future
7030** release of SQLite.
7031**
7032** The update hook implementation must not do anything that will modify
7033** the database connection that invoked the update hook. Any actions
7034** to modify the database connection must be deferred until after the
7035** completion of the [sqlite3_step()] call that triggered the update hook.
7036** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
7037** database connections for the meaning of "modify" in this paragraph.
7038**
7039** ^The sqlite3_update_hook(D,C,P) function
7040** returns the P argument from the previous call
7041** on the same [database connection] D, or NULL for
7042** the first call on D.
7043**
7044** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
7045** and [sqlite3_preupdate_hook()] interfaces.
7046*/
7047SQLITE_API void *sqlite3_update_hook(
7048 sqlite3*,
7049 void(*)(void *,int ,char const *,char const *,sqlite3_int64),
7050 void*
7051);
7052
7053/*
7054** CAPI3REF: Enable Or Disable Shared Pager Cache
7055**
7056** ^(This routine enables or disables the sharing of the database cache
7057** and schema data structures between [database connection | connections]
7058** to the same database. Sharing is enabled if the argument is true
7059** and disabled if the argument is false.)^
7060**
7061** ^Cache sharing is enabled and disabled for an entire process.
7062** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
7063** In prior versions of SQLite,
7064** sharing was enabled or disabled for each thread separately.
7065**
7066** ^(The cache sharing mode set by this interface effects all subsequent
7067** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
7068** Existing database connections continue use the sharing mode
7069** that was in effect at the time they were opened.)^
7070**
7071** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
7072** successfully. An [error code] is returned otherwise.)^
7073**
7074** ^Shared cache is disabled by default. But this might change in
7075** future releases of SQLite. Applications that care about shared
7076** cache setting should set it explicitly.
7077**
7078** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
7079** and will always return SQLITE_MISUSE. On those systems,
7080** shared cache mode should be enabled per-database connection via
7081** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
7082**
7083** This interface is threadsafe on processors where writing a
7084** 32-bit integer is atomic.
7085**
7086** See Also: [SQLite Shared-Cache Mode]
7087*/
7088SQLITE_API int sqlite3_enable_shared_cache(int);
7089
7090/*
7091** CAPI3REF: Attempt To Free Heap Memory
7092**
7093** ^The sqlite3_release_memory() interface attempts to free N bytes
7094** of heap memory by deallocating non-essential memory allocations
7095** held by the database library. Memory used to cache database
7096** pages to improve performance is an example of non-essential memory.
7097** ^sqlite3_release_memory() returns the number of bytes actually freed,
7098** which might be more or less than the amount requested.
7099** ^The sqlite3_release_memory() routine is a no-op returning zero
7100** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
7101**
7102** See also: [sqlite3_db_release_memory()]
7103*/
7104SQLITE_API int sqlite3_release_memory(int);
7105
7106/*
7107** CAPI3REF: Free Memory Used By A Database Connection
7108** METHOD: sqlite3
7109**
7110** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
7111** memory as possible from database connection D. Unlike the
7112** [sqlite3_release_memory()] interface, this interface is in effect even
7113** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7114** omitted.
7115**
7116** See also: [sqlite3_release_memory()]
7117*/
7118SQLITE_API int sqlite3_db_release_memory(sqlite3*);
7119
7120/*
7121** CAPI3REF: Impose A Limit On Heap Size
7122**
7123** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
7124** soft limit on the amount of heap memory that may be allocated by SQLite.
7125** ^SQLite strives to keep heap memory utilization below the soft heap
7126** limit by reducing the number of pages held in the page cache
7127** as heap memory usages approaches the limit.
7128** ^The soft heap limit is "soft" because even though SQLite strives to stay
7129** below the limit, it will exceed the limit rather than generate
7130** an [SQLITE_NOMEM] error. In other words, the soft heap limit
7131** is advisory only.
7132**
7133** ^The return value from sqlite3_soft_heap_limit64() is the size of
7134** the soft heap limit prior to the call, or negative in the case of an
7135** error. ^If the argument N is negative
7136** then no change is made to the soft heap limit. Hence, the current
7137** size of the soft heap limit can be determined by invoking
7138** sqlite3_soft_heap_limit64() with a negative argument.
7139**
7140** ^If the argument N is zero then the soft heap limit is disabled.
7141**
7142** ^(The soft heap limit is not enforced in the current implementation
7143** if one or more of following conditions are true:
7144**
7145** <ul>
7146** <li> The soft heap limit is set to zero.
7147** <li> Memory accounting is disabled using a combination of the
7148** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7149** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7150** <li> An alternative page cache implementation is specified using
7151** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
7152** <li> The page cache allocates from its own memory pool supplied
7153** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
7154** from the heap.
7155** </ul>)^
7156**
7157** Beginning with SQLite [version 3.7.3] ([dateof:3.7.3]),
7158** the soft heap limit is enforced
7159** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
7160** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
7161** the soft heap limit is enforced on every memory allocation. Without
7162** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
7163** when memory is allocated by the page cache. Testing suggests that because
7164** the page cache is the predominate memory user in SQLite, most
7165** applications will achieve adequate soft heap limit enforcement without
7166** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
7167**
7168** The circumstances under which SQLite will enforce the soft heap limit may
7169** changes in future releases of SQLite.
7170*/
7171SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
7172
7173/*
7174** CAPI3REF: Deprecated Soft Heap Limit Interface
7175** DEPRECATED
7176**
7177** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
7178** interface. This routine is provided for historical compatibility
7179** only. All new applications should use the
7180** [sqlite3_soft_heap_limit64()] interface rather than this one.
7181*/
7182SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
7183
7184
7185/*
7186** CAPI3REF: Extract Metadata About A Column Of A Table
7187** METHOD: sqlite3
7188**
7189** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
7190** information about column C of table T in database D
7191** on [database connection] X.)^ ^The sqlite3_table_column_metadata()
7192** interface returns SQLITE_OK and fills in the non-NULL pointers in
7193** the final five arguments with appropriate values if the specified
7194** column exists. ^The sqlite3_table_column_metadata() interface returns
7195** SQLITE_ERROR and if the specified column does not exist.
7196** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7197** NULL pointer, then this routine simply checks for the existence of the
7198** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
7199** does not. If the table name parameter T in a call to
7200** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
7201** undefined behavior.
7202**
7203** ^The column is identified by the second, third and fourth parameters to
7204** this function. ^(The second parameter is either the name of the database
7205** (i.e. "main", "temp", or an attached database) containing the specified
7206** table or NULL.)^ ^If it is NULL, then all attached databases are searched
7207** for the table using the same algorithm used by the database engine to
7208** resolve unqualified table references.
7209**
7210** ^The third and fourth parameters to this function are the table and column
7211** name of the desired column, respectively.
7212**
7213** ^Metadata is returned by writing to the memory locations passed as the 5th
7214** and subsequent parameters to this function. ^Any of these arguments may be
7215** NULL, in which case the corresponding element of metadata is omitted.
7216**
7217** ^(<blockquote>
7218** <table border="1">
7219** <tr><th> Parameter <th> Output<br>Type <th> Description
7220**
7221** <tr><td> 5th <td> const char* <td> Data type
7222** <tr><td> 6th <td> const char* <td> Name of default collation sequence
7223** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
7224** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
7225** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
7226** </table>
7227** </blockquote>)^
7228**
7229** ^The memory pointed to by the character pointers returned for the
7230** declaration type and collation sequence is valid until the next
7231** call to any SQLite API function.
7232**
7233** ^If the specified table is actually a view, an [error code] is returned.
7234**
7235** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
7236** is not a [WITHOUT ROWID] table and an
7237** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
7238** parameters are set for the explicitly declared column. ^(If there is no
7239** [INTEGER PRIMARY KEY] column, then the outputs
7240** for the [rowid] are set as follows:
7241**
7242** <pre>
7243** data type: "INTEGER"
7244** collation sequence: "BINARY"
7245** not null: 0
7246** primary key: 1
7247** auto increment: 0
7248** </pre>)^
7249**
7250** ^This function causes all database schemas to be read from disk and
7251** parsed, if that has not already been done, and returns an error if
7252** any errors are encountered while loading the schema.
7253*/
7254SQLITE_API int sqlite3_table_column_metadata(
7255 sqlite3 *db, /* Connection handle */
7256 const char *zDbName, /* Database name or NULL */
7257 const char *zTableName, /* Table name */
7258 const char *zColumnName, /* Column name */
7259 char const **pzDataType, /* OUTPUT: Declared data type */
7260 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
7261 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
7262 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
7263 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7264);
7265
7266/*
7267** CAPI3REF: Load An Extension
7268** METHOD: sqlite3
7269**
7270** ^This interface loads an SQLite extension library from the named file.
7271**
7272** ^The sqlite3_load_extension() interface attempts to load an
7273** [SQLite extension] library contained in the file zFile. If
7274** the file cannot be loaded directly, attempts are made to load
7275** with various operating-system specific extensions added.
7276** So for example, if "samplelib" cannot be loaded, then names like
7277** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
7278** be tried also.
7279**
7280** ^The entry point is zProc.
7281** ^(zProc may be 0, in which case SQLite will try to come up with an
7282** entry point name on its own. It first tries "sqlite3_extension_init".
7283** If that does not work, it constructs a name "sqlite3_X_init" where the
7284** X is consists of the lower-case equivalent of all ASCII alphabetic
7285** characters in the filename from the last "/" to the first following
7286** "." and omitting any initial "lib".)^
7287** ^The sqlite3_load_extension() interface returns
7288** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
7289** ^If an error occurs and pzErrMsg is not 0, then the
7290** [sqlite3_load_extension()] interface shall attempt to
7291** fill *pzErrMsg with error message text stored in memory
7292** obtained from [sqlite3_malloc()]. The calling function
7293** should free this memory by calling [sqlite3_free()].
7294**
7295** ^Extension loading must be enabled using
7296** [sqlite3_enable_load_extension()] or
7297** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
7298** prior to calling this API,
7299** otherwise an error will be returned.
7300**
7301** <b>Security warning:</b> It is recommended that the
7302** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
7303** interface. The use of the [sqlite3_enable_load_extension()] interface
7304** should be avoided. This will keep the SQL function [load_extension()]
7305** disabled and prevent SQL injections from giving attackers
7306** access to extension loading capabilities.
7307**
7308** See also the [load_extension() SQL function].
7309*/
7310SQLITE_API int sqlite3_load_extension(
7311 sqlite3 *db, /* Load the extension into this database connection */
7312 const char *zFile, /* Name of the shared library containing extension */
7313 const char *zProc, /* Entry point. Derived from zFile if 0 */
7314 char **pzErrMsg /* Put error message here if not 0 */
7315);
7316
7317/*
7318** CAPI3REF: Enable Or Disable Extension Loading
7319** METHOD: sqlite3
7320**
7321** ^So as not to open security holes in older applications that are
7322** unprepared to deal with [extension loading], and as a means of disabling
7323** [extension loading] while evaluating user-entered SQL, the following API
7324** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
7325**
7326** ^Extension loading is off by default.
7327** ^Call the sqlite3_enable_load_extension() routine with onoff==1
7328** to turn extension loading on and call it with onoff==0 to turn
7329** it back off again.
7330**
7331** ^This interface enables or disables both the C-API
7332** [sqlite3_load_extension()] and the SQL function [load_extension()].
7333** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
7334** to enable or disable only the C-API.)^
7335**
7336** <b>Security warning:</b> It is recommended that extension loading
7337** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
7338** rather than this interface, so the [load_extension()] SQL function
7339** remains disabled. This will prevent SQL injections from giving attackers
7340** access to extension loading capabilities.
7341*/
7342SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
7343
7344/*
7345** CAPI3REF: Automatically Load Statically Linked Extensions
7346**
7347** ^This interface causes the xEntryPoint() function to be invoked for
7348** each new [database connection] that is created. The idea here is that
7349** xEntryPoint() is the entry point for a statically linked [SQLite extension]
7350** that is to be automatically loaded into all new database connections.
7351**
7352** ^(Even though the function prototype shows that xEntryPoint() takes
7353** no arguments and returns void, SQLite invokes xEntryPoint() with three
7354** arguments and expects an integer result as if the signature of the
7355** entry point where as follows:
7356**
7357** <blockquote><pre>
7358** &nbsp; int xEntryPoint(
7359** &nbsp; sqlite3 *db,
7360** &nbsp; const char **pzErrMsg,
7361** &nbsp; const struct sqlite3_api_routines *pThunk
7362** &nbsp; );
7363** </pre></blockquote>)^
7364**
7365** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
7366** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7367** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
7368** is NULL before calling the xEntryPoint(). ^SQLite will invoke
7369** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
7370** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
7371** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
7372**
7373** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
7374** on the list of automatic extensions is a harmless no-op. ^No entry point
7375** will be called more than once for each database connection that is opened.
7376**
7377** See also: [sqlite3_reset_auto_extension()]
7378** and [sqlite3_cancel_auto_extension()]
7379*/
7380SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
7381
7382/*
7383** CAPI3REF: Cancel Automatic Extension Loading
7384**
7385** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
7386** initialization routine X that was registered using a prior call to
7387** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)]
7388** routine returns 1 if initialization routine X was successfully
7389** unregistered and it returns 0 if X was not on the list of initialization
7390** routines.
7391*/
7392SQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
7393
7394/*
7395** CAPI3REF: Reset Automatic Extension Loading
7396**
7397** ^This interface disables all automatic extensions previously
7398** registered using [sqlite3_auto_extension()].
7399*/
7400SQLITE_API void sqlite3_reset_auto_extension(void);
7401
7402/*
7403** The interface to the virtual-table mechanism is currently considered
7404** to be experimental. The interface might change in incompatible ways.
7405** If this is a problem for you, do not use the interface at this time.
7406**
7407** When the virtual-table mechanism stabilizes, we will declare the
7408** interface fixed, support it indefinitely, and remove this comment.
7409*/
7410
7411/*
7412** Structures used by the virtual table interface
7413*/
7414typedef struct sqlite3_vtab sqlite3_vtab;
7415typedef struct sqlite3_index_info sqlite3_index_info;
7416typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
7417typedef struct sqlite3_module sqlite3_module;
7418
7419/*
7420** CAPI3REF: Virtual Table Object
7421** KEYWORDS: sqlite3_module {virtual table module}
7422**
7423** This structure, sometimes called a "virtual table module",
7424** defines the implementation of a [virtual tables].
7425** This structure consists mostly of methods for the module.
7426**
7427** ^A virtual table module is created by filling in a persistent
7428** instance of this structure and passing a pointer to that instance
7429** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
7430** ^The registration remains valid until it is replaced by a different
7431** module or until the [database connection] closes. The content
7432** of this structure must not change while it is registered with
7433** any database connection.
7434*/
7435struct sqlite3_module {
7436 int iVersion;
7437 int (*xCreate)(sqlite3*, void *pAux,
7438 int argc, const char *const*argv,
7439 sqlite3_vtab **ppVTab, char**);
7440 int (*xConnect)(sqlite3*, void *pAux,
7441 int argc, const char *const*argv,
7442 sqlite3_vtab **ppVTab, char**);
7443 int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
7444 int (*xDisconnect)(sqlite3_vtab *pVTab);
7445 int (*xDestroy)(sqlite3_vtab *pVTab);
7446 int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
7447 int (*xClose)(sqlite3_vtab_cursor*);
7448 int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
7449 int argc, sqlite3_value **argv);
7450 int (*xNext)(sqlite3_vtab_cursor*);
7451 int (*xEof)(sqlite3_vtab_cursor*);
7452 int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
7453 int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
7454 int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
7455 int (*xBegin)(sqlite3_vtab *pVTab);
7456 int (*xSync)(sqlite3_vtab *pVTab);
7457 int (*xCommit)(sqlite3_vtab *pVTab);
7458 int (*xRollback)(sqlite3_vtab *pVTab);
7459 int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
7460 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
7461 void **ppArg);
7462 int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
7463 /* The methods above are in version 1 of the sqlite_module object. Those
7464 ** below are for version 2 and greater. */
7465 int (*xSavepoint)(sqlite3_vtab *pVTab, int);
7466 int (*xRelease)(sqlite3_vtab *pVTab, int);
7467 int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
7468 /* The methods above are in versions 1 and 2 of the sqlite_module object.
7469 ** Those below are for version 3 and greater. */
7470 int (*xShadowName)(const char*);
7471};
7472
7473/*
7474** CAPI3REF: Virtual Table Indexing Information
7475** KEYWORDS: sqlite3_index_info
7476**
7477** The sqlite3_index_info structure and its substructures is used as part
7478** of the [virtual table] interface to
7479** pass information into and receive the reply from the [xBestIndex]
7480** method of a [virtual table module]. The fields under **Inputs** are the
7481** inputs to xBestIndex and are read-only. xBestIndex inserts its
7482** results into the **Outputs** fields.
7483**
7484** ^(The aConstraint[] array records WHERE clause constraints of the form:
7485**
7486** <blockquote>column OP expr</blockquote>
7487**
7488** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
7489** stored in aConstraint[].op using one of the
7490** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
7491** ^(The index of the column is stored in
7492** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
7493** expr on the right-hand side can be evaluated (and thus the constraint
7494** is usable) and false if it cannot.)^
7495**
7496** ^The optimizer automatically inverts terms of the form "expr OP column"
7497** and makes other simplifications to the WHERE clause in an attempt to
7498** get as many WHERE clause terms into the form shown above as possible.
7499** ^The aConstraint[] array only reports WHERE clause terms that are
7500** relevant to the particular virtual table being queried.
7501**
7502** ^Information about the ORDER BY clause is stored in aOrderBy[].
7503** ^Each term of aOrderBy records a column of the ORDER BY clause.
7504**
7505** The colUsed field indicates which columns of the virtual table may be
7506** required by the current scan. Virtual table columns are numbered from
7507** zero in the order in which they appear within the CREATE TABLE statement
7508** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7509** the corresponding bit is set within the colUsed mask if the column may be
7510** required by SQLite. If the table has at least 64 columns and any column
7511** to the right of the first 63 is required, then bit 63 of colUsed is also
7512** set. In other words, column iCol may be required if the expression
7513** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
7514** non-zero.
7515**
7516** The [xBestIndex] method must fill aConstraintUsage[] with information
7517** about what parameters to pass to xFilter. ^If argvIndex>0 then
7518** the right-hand side of the corresponding aConstraint[] is evaluated
7519** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7520** is true, then the constraint is assumed to be fully handled by the
7521** virtual table and is not checked again by SQLite.)^
7522**
7523** ^The idxNum and idxPtr values are recorded and passed into the
7524** [xFilter] method.
7525** ^[sqlite3_free()] is used to free idxPtr if and only if
7526** needToFreeIdxPtr is true.
7527**
7528** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
7529** the correct order to satisfy the ORDER BY clause so that no separate
7530** sorting step is required.
7531**
7532** ^The estimatedCost value is an estimate of the cost of a particular
7533** strategy. A cost of N indicates that the cost of the strategy is similar
7534** to a linear scan of an SQLite table with N rows. A cost of log(N)
7535** indicates that the expense of the operation is similar to that of a
7536** binary search on a unique indexed field of an SQLite table with N rows.
7537**
7538** ^The estimatedRows value is an estimate of the number of rows that
7539** will be returned by the strategy.
7540**
7541** The xBestIndex method may optionally populate the idxFlags field with a
7542** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7543** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
7544** assumes that the strategy may visit at most one row.
7545**
7546** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
7547** SQLite also assumes that if a call to the xUpdate() method is made as
7548** part of the same statement to delete or update a virtual table row and the
7549** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
7550** any database changes. In other words, if the xUpdate() returns
7551** SQLITE_CONSTRAINT, the database contents must be exactly as they were
7552** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
7553** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
7554** the xUpdate method are automatically rolled back by SQLite.
7555**
7556** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
7557** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
7558** If a virtual table extension is
7559** used with an SQLite version earlier than 3.8.2, the results of attempting
7560** to read or write the estimatedRows field are undefined (but are likely
7561** to included crashing the application). The estimatedRows field should
7562** therefore only be used if [sqlite3_libversion_number()] returns a
7563** value greater than or equal to 3008002. Similarly, the idxFlags field
7564** was added for [version 3.9.0] ([dateof:3.9.0]).
7565** It may therefore only be used if
7566** sqlite3_libversion_number() returns a value greater than or equal to
7567** 3009000.
7568*/
7569struct sqlite3_index_info {
7570 /* Inputs */
7571 int nConstraint; /* Number of entries in aConstraint */
7572 struct sqlite3_index_constraint {
7573 int iColumn; /* Column constrained. -1 for ROWID */
7574 unsigned char op; /* Constraint operator */
7575 unsigned char usable; /* True if this constraint is usable */
7576 int iTermOffset; /* Used internally - xBestIndex should ignore */
7577 } *aConstraint; /* Table of WHERE clause constraints */
7578 int nOrderBy; /* Number of terms in the ORDER BY clause */
7579 struct sqlite3_index_orderby {
7580 int iColumn; /* Column number */
7581 unsigned char desc; /* True for DESC. False for ASC. */
7582 } *aOrderBy; /* The ORDER BY clause */
7583 /* Outputs */
7584 struct sqlite3_index_constraint_usage {
7585 int argvIndex; /* if >0, constraint is part of argv to xFilter */
7586 unsigned char omit; /* Do not code a test for this constraint */
7587 } *aConstraintUsage;
7588 int idxNum; /* Number used to identify the index */
7589 char *idxStr; /* String, possibly obtained from sqlite3_malloc */
7590 int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
7591 int orderByConsumed; /* True if output is already ordered */
7592 double estimatedCost; /* Estimated cost of using this index */
7593 /* Fields below are only available in SQLite 3.8.2 and later */
7594 sqlite3_int64 estimatedRows; /* Estimated number of rows returned */
7595 /* Fields below are only available in SQLite 3.9.0 and later */
7596 int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */
7597 /* Fields below are only available in SQLite 3.10.0 and later */
7598 sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */
7599};
7600
7601/*
7602** CAPI3REF: Virtual Table Scan Flags
7603**
7604** Virtual table implementations are allowed to set the
7605** [sqlite3_index_info].idxFlags field to some combination of
7606** these bits.
7607*/
7608#define SQLITE_INDEX_SCAN_UNIQUE1 1 /* Scan visits at most 1 row */
7609
7610/*
7611** CAPI3REF: Virtual Table Constraint Operator Codes
7612**
7613** These macros defined the allowed values for the
7614** [sqlite3_index_info].aConstraint[].op field. Each value represents
7615** an operator that is part of a constraint term in the wHERE clause of
7616** a query that uses a [virtual table].
7617*/
7618#define SQLITE_INDEX_CONSTRAINT_EQ2 2
7619#define SQLITE_INDEX_CONSTRAINT_GT4 4
7620#define SQLITE_INDEX_CONSTRAINT_LE8 8
7621#define SQLITE_INDEX_CONSTRAINT_LT16 16
7622#define SQLITE_INDEX_CONSTRAINT_GE32 32
7623#define SQLITE_INDEX_CONSTRAINT_MATCH64 64
7624#define SQLITE_INDEX_CONSTRAINT_LIKE65 65
7625#define SQLITE_INDEX_CONSTRAINT_GLOB66 66
7626#define SQLITE_INDEX_CONSTRAINT_REGEXP67 67
7627#define SQLITE_INDEX_CONSTRAINT_NE68 68
7628#define SQLITE_INDEX_CONSTRAINT_ISNOT69 69
7629#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL70 70
7630#define SQLITE_INDEX_CONSTRAINT_ISNULL71 71
7631#define SQLITE_INDEX_CONSTRAINT_IS72 72
7632#define SQLITE_INDEX_CONSTRAINT_FUNCTION150 150
7633
7634/*
7635** CAPI3REF: Register A Virtual Table Implementation
7636** METHOD: sqlite3
7637**
7638** ^These routines are used to register a new [virtual table module] name.
7639** ^Module names must be registered before
7640** creating a new [virtual table] using the module and before using a
7641** preexisting [virtual table] for the module.
7642**
7643** ^The module name is registered on the [database connection] specified
7644** by the first parameter. ^The name of the module is given by the
7645** second parameter. ^The third parameter is a pointer to
7646** the implementation of the [virtual table module]. ^The fourth
7647** parameter is an arbitrary client data pointer that is passed through
7648** into the [xCreate] and [xConnect] methods of the virtual table module
7649** when a new virtual table is be being created or reinitialized.
7650**
7651** ^The sqlite3_create_module_v2() interface has a fifth parameter which
7652** is a pointer to a destructor for the pClientData. ^SQLite will
7653** invoke the destructor function (if it is not NULL) when SQLite
7654** no longer needs the pClientData pointer. ^The destructor will also
7655** be invoked if the call to sqlite3_create_module_v2() fails.
7656** ^The sqlite3_create_module()
7657** interface is equivalent to sqlite3_create_module_v2() with a NULL
7658** destructor.
7659*/
7660SQLITE_API int sqlite3_create_module(
7661 sqlite3 *db, /* SQLite connection to register module with */
7662 const char *zName, /* Name of the module */
7663 const sqlite3_module *p, /* Methods for the module */
7664 void *pClientData /* Client data for xCreate/xConnect */
7665);
7666SQLITE_API int sqlite3_create_module_v2(
7667 sqlite3 *db, /* SQLite connection to register module with */
7668 const char *zName, /* Name of the module */
7669 const sqlite3_module *p, /* Methods for the module */
7670 void *pClientData, /* Client data for xCreate/xConnect */
7671 void(*xDestroy)(void*) /* Module destructor function */
7672);
7673
7674/*
7675** CAPI3REF: Virtual Table Instance Object
7676** KEYWORDS: sqlite3_vtab
7677**
7678** Every [virtual table module] implementation uses a subclass
7679** of this object to describe a particular instance
7680** of the [virtual table]. Each subclass will
7681** be tailored to the specific needs of the module implementation.
7682** The purpose of this superclass is to define certain fields that are
7683** common to all module implementations.
7684**
7685** ^Virtual tables methods can set an error message by assigning a
7686** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
7687** take care that any prior string is freed by a call to [sqlite3_free()]
7688** prior to assigning a new string to zErrMsg. ^After the error message
7689** is delivered up to the client application, the string will be automatically
7690** freed by sqlite3_free() and the zErrMsg field will be zeroed.
7691*/
7692struct sqlite3_vtab {
7693 const sqlite3_module *pModule; /* The module for this virtual table */
7694 int nRef; /* Number of open cursors */
7695 char *zErrMsg; /* Error message from sqlite3_mprintf() */
7696 /* Virtual table implementations will typically add additional fields */
7697};
7698
7699/*
7700** CAPI3REF: Virtual Table Cursor Object
7701** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
7702**
7703** Every [virtual table module] implementation uses a subclass of the
7704** following structure to describe cursors that point into the
7705** [virtual table] and are used
7706** to loop through the virtual table. Cursors are created using the
7707** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
7708** by the [sqlite3_module.xClose | xClose] method. Cursors are used
7709** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
7710** of the module. Each module implementation will define
7711** the content of a cursor structure to suit its own needs.
7712**
7713** This superclass exists in order to define fields of the cursor that
7714** are common to all implementations.
7715*/
7716struct sqlite3_vtab_cursor {
7717 sqlite3_vtab *pVtab; /* Virtual table of this cursor */
7718 /* Virtual table implementations will typically add additional fields */
7719};
7720
7721/*
7722** CAPI3REF: Declare The Schema Of A Virtual Table
7723**
7724** ^The [xCreate] and [xConnect] methods of a
7725** [virtual table module] call this interface
7726** to declare the format (the names and datatypes of the columns) of
7727** the virtual tables they implement.
7728*/
7729SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
7730
7731/*
7732** CAPI3REF: Overload A Function For A Virtual Table
7733** METHOD: sqlite3
7734**
7735** ^(Virtual tables can provide alternative implementations of functions
7736** using the [xFindFunction] method of the [virtual table module].
7737** But global versions of those functions
7738** must exist in order to be overloaded.)^
7739**
7740** ^(This API makes sure a global version of a function with a particular
7741** name and number of parameters exists. If no such function exists
7742** before this API is called, a new function is created.)^ ^The implementation
7743** of the new function always causes an exception to be thrown. So
7744** the new function is not good for anything by itself. Its only
7745** purpose is to be a placeholder function that can be overloaded
7746** by a [virtual table].
7747*/
7748SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
7749
7750/*
7751** The interface to the virtual-table mechanism defined above (back up
7752** to a comment remarkably similar to this one) is currently considered
7753** to be experimental. The interface might change in incompatible ways.
7754** If this is a problem for you, do not use the interface at this time.
7755**
7756** When the virtual-table mechanism stabilizes, we will declare the
7757** interface fixed, support it indefinitely, and remove this comment.
7758*/
7759
7760/*
7761** CAPI3REF: A Handle To An Open BLOB
7762** KEYWORDS: {BLOB handle} {BLOB handles}
7763**
7764** An instance of this object represents an open BLOB on which
7765** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
7766** ^Objects of this type are created by [sqlite3_blob_open()]
7767** and destroyed by [sqlite3_blob_close()].
7768** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
7769** can be used to read or write small subsections of the BLOB.
7770** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
7771*/
7772typedef struct sqlite3_blob sqlite3_blob;
7773
7774/*
7775** CAPI3REF: Open A BLOB For Incremental I/O
7776** METHOD: sqlite3
7777** CONSTRUCTOR: sqlite3_blob
7778**
7779** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
7780** in row iRow, column zColumn, table zTable in database zDb;
7781** in other words, the same BLOB that would be selected by:
7782**
7783** <pre>
7784** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
7785** </pre>)^
7786**
7787** ^(Parameter zDb is not the filename that contains the database, but
7788** rather the symbolic name of the database. For attached databases, this is
7789** the name that appears after the AS keyword in the [ATTACH] statement.
7790** For the main database file, the database name is "main". For TEMP
7791** tables, the database name is "temp".)^
7792**
7793** ^If the flags parameter is non-zero, then the BLOB is opened for read
7794** and write access. ^If the flags parameter is zero, the BLOB is opened for
7795** read-only access.
7796**
7797** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
7798** in *ppBlob. Otherwise an [error code] is returned and, unless the error
7799** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
7800** the API is not misused, it is always safe to call [sqlite3_blob_close()]
7801** on *ppBlob after this function it returns.
7802**
7803** This function fails with SQLITE_ERROR if any of the following are true:
7804** <ul>
7805** <li> ^(Database zDb does not exist)^,
7806** <li> ^(Table zTable does not exist within database zDb)^,
7807** <li> ^(Table zTable is a WITHOUT ROWID table)^,
7808** <li> ^(Column zColumn does not exist)^,
7809** <li> ^(Row iRow is not present in the table)^,
7810** <li> ^(The specified column of row iRow contains a value that is not
7811** a TEXT or BLOB value)^,
7812** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
7813** constraint and the blob is being opened for read/write access)^,
7814** <li> ^([foreign key constraints | Foreign key constraints] are enabled,
7815** column zColumn is part of a [child key] definition and the blob is
7816** being opened for read/write access)^.
7817** </ul>
7818**
7819** ^Unless it returns SQLITE_MISUSE, this function sets the
7820** [database connection] error code and message accessible via
7821** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7822**
7823** A BLOB referenced by sqlite3_blob_open() may be read using the
7824** [sqlite3_blob_read()] interface and modified by using
7825** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a
7826** different row of the same table using the [sqlite3_blob_reopen()]
7827** interface. However, the column, table, or database of a [BLOB handle]
7828** cannot be changed after the [BLOB handle] is opened.
7829**
7830** ^(If the row that a BLOB handle points to is modified by an
7831** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7832** then the BLOB handle is marked as "expired".
7833** This is true if any column of the row is changed, even a column
7834** other than the one the BLOB handle is open on.)^
7835** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
7836** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
7837** ^(Changes written into a BLOB prior to the BLOB expiring are not
7838** rolled back by the expiration of the BLOB. Such changes will eventually
7839** commit if the transaction continues to completion.)^
7840**
7841** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
7842** the opened blob. ^The size of a blob may not be changed by this
7843** interface. Use the [UPDATE] SQL command to change the size of a
7844** blob.
7845**
7846** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
7847** and the built-in [zeroblob] SQL function may be used to create a
7848** zero-filled blob to read or write using the incremental-blob interface.
7849**
7850** To avoid a resource leak, every open [BLOB handle] should eventually
7851** be released by a call to [sqlite3_blob_close()].
7852**
7853** See also: [sqlite3_blob_close()],
7854** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
7855** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
7856*/
7857SQLITE_API int sqlite3_blob_open(
7858 sqlite3*,
7859 const char *zDb,
7860 const char *zTable,
7861 const char *zColumn,
7862 sqlite3_int64 iRow,
7863 int flags,
7864 sqlite3_blob **ppBlob
7865);
7866
7867/*
7868** CAPI3REF: Move a BLOB Handle to a New Row
7869** METHOD: sqlite3_blob
7870**
7871** ^This function is used to move an existing [BLOB handle] so that it points
7872** to a different row of the same database table. ^The new row is identified
7873** by the rowid value passed as the second argument. Only the row can be
7874** changed. ^The database, table and column on which the blob handle is open
7875** remain the same. Moving an existing [BLOB handle] to a new row is
7876** faster than closing the existing handle and opening a new one.
7877**
7878** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7879** it must exist and there must be either a blob or text value stored in
7880** the nominated column.)^ ^If the new row is not present in the table, or if
7881** it does not contain a blob or text value, or if another error occurs, an
7882** SQLite error code is returned and the blob handle is considered aborted.
7883** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
7884** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
7885** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
7886** always returns zero.
7887**
7888** ^This function sets the database handle error code and message.
7889*/
7890SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
7891
7892/*
7893** CAPI3REF: Close A BLOB Handle
7894** DESTRUCTOR: sqlite3_blob
7895**
7896** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
7897** unconditionally. Even if this routine returns an error code, the
7898** handle is still closed.)^
7899**
7900** ^If the blob handle being closed was opened for read-write access, and if
7901** the database is in auto-commit mode and there are no other open read-write
7902** blob handles or active write statements, the current transaction is
7903** committed. ^If an error occurs while committing the transaction, an error
7904** code is returned and the transaction rolled back.
7905**
7906** Calling this function with an argument that is not a NULL pointer or an
7907** open blob handle results in undefined behaviour. ^Calling this routine
7908** with a null pointer (such as would be returned by a failed call to
7909** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7910** is passed a valid open blob handle, the values returned by the
7911** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
7912*/
7913SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
7914
7915/*
7916** CAPI3REF: Return The Size Of An Open BLOB
7917** METHOD: sqlite3_blob
7918**
7919** ^Returns the size in bytes of the BLOB accessible via the
7920** successfully opened [BLOB handle] in its only argument. ^The
7921** incremental blob I/O routines can only read or overwriting existing
7922** blob content; they cannot change the size of a blob.
7923**
7924** This routine only works on a [BLOB handle] which has been created
7925** by a prior successful call to [sqlite3_blob_open()] and which has not
7926** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7927** to this routine results in undefined and probably undesirable behavior.
7928*/
7929SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
7930
7931/*
7932** CAPI3REF: Read Data From A BLOB Incrementally
7933** METHOD: sqlite3_blob
7934**
7935** ^(This function is used to read data from an open [BLOB handle] into a
7936** caller-supplied buffer. N bytes of data are copied into buffer Z
7937** from the open BLOB, starting at offset iOffset.)^
7938**
7939** ^If offset iOffset is less than N bytes from the end of the BLOB,
7940** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
7941** less than zero, [SQLITE_ERROR] is returned and no data is read.
7942** ^The size of the blob (and hence the maximum value of N+iOffset)
7943** can be determined using the [sqlite3_blob_bytes()] interface.
7944**
7945** ^An attempt to read from an expired [BLOB handle] fails with an
7946** error code of [SQLITE_ABORT].
7947**
7948** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
7949** Otherwise, an [error code] or an [extended error code] is returned.)^
7950**
7951** This routine only works on a [BLOB handle] which has been created
7952** by a prior successful call to [sqlite3_blob_open()] and which has not
7953** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7954** to this routine results in undefined and probably undesirable behavior.
7955**
7956** See also: [sqlite3_blob_write()].
7957*/
7958SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
7959
7960/*
7961** CAPI3REF: Write Data Into A BLOB Incrementally
7962** METHOD: sqlite3_blob
7963**
7964** ^(This function is used to write data into an open [BLOB handle] from a
7965** caller-supplied buffer. N bytes of data are copied from the buffer Z
7966** into the open BLOB, starting at offset iOffset.)^
7967**
7968** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
7969** Otherwise, an [error code] or an [extended error code] is returned.)^
7970** ^Unless SQLITE_MISUSE is returned, this function sets the
7971** [database connection] error code and message accessible via
7972** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7973**
7974** ^If the [BLOB handle] passed as the first argument was not opened for
7975** writing (the flags parameter to [sqlite3_blob_open()] was zero),
7976** this function returns [SQLITE_READONLY].
7977**
7978** This function may only modify the contents of the BLOB; it is
7979** not possible to increase the size of a BLOB using this API.
7980** ^If offset iOffset is less than N bytes from the end of the BLOB,
7981** [SQLITE_ERROR] is returned and no data is written. The size of the
7982** BLOB (and hence the maximum value of N+iOffset) can be determined
7983** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
7984** than zero [SQLITE_ERROR] is returned and no data is written.
7985**
7986** ^An attempt to write to an expired [BLOB handle] fails with an
7987** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
7988** before the [BLOB handle] expired are not rolled back by the
7989** expiration of the handle, though of course those changes might
7990** have been overwritten by the statement that expired the BLOB handle
7991** or by other independent statements.
7992**
7993** This routine only works on a [BLOB handle] which has been created
7994** by a prior successful call to [sqlite3_blob_open()] and which has not
7995** been closed by [sqlite3_blob_close()]. Passing any other pointer in
7996** to this routine results in undefined and probably undesirable behavior.
7997**
7998** See also: [sqlite3_blob_read()].
7999*/
8000SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
8001
8002/*
8003** CAPI3REF: Virtual File System Objects
8004**
8005** A virtual filesystem (VFS) is an [sqlite3_vfs] object
8006** that SQLite uses to interact
8007** with the underlying operating system. Most SQLite builds come with a
8008** single default VFS that is appropriate for the host computer.
8009** New VFSes can be registered and existing VFSes can be unregistered.
8010** The following interfaces are provided.
8011**
8012** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
8013** ^Names are case sensitive.
8014** ^Names are zero-terminated UTF-8 strings.
8015** ^If there is no match, a NULL pointer is returned.
8016** ^If zVfsName is NULL then the default VFS is returned.
8017**
8018** ^New VFSes are registered with sqlite3_vfs_register().
8019** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
8020** ^The same VFS can be registered multiple times without injury.
8021** ^To make an existing VFS into the default VFS, register it again
8022** with the makeDflt flag set. If two different VFSes with the
8023** same name are registered, the behavior is undefined. If a
8024** VFS is registered with a name that is NULL or an empty string,
8025** then the behavior is undefined.
8026**
8027** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
8028** ^(If the default VFS is unregistered, another VFS is chosen as
8029** the default. The choice for the new VFS is arbitrary.)^
8030*/
8031SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
8032SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
8033SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
8034
8035/*
8036** CAPI3REF: Mutexes
8037**
8038** The SQLite core uses these routines for thread
8039** synchronization. Though they are intended for internal
8040** use by SQLite, code that links against SQLite is
8041** permitted to use any of these routines.
8042**
8043** The SQLite source code contains multiple implementations
8044** of these mutex routines. An appropriate implementation
8045** is selected automatically at compile-time. The following
8046** implementations are available in the SQLite core:
8047**
8048** <ul>
8049** <li> SQLITE_MUTEX_PTHREADS
8050** <li> SQLITE_MUTEX_W32
8051** <li> SQLITE_MUTEX_NOOP
8052** </ul>
8053**
8054** The SQLITE_MUTEX_NOOP implementation is a set of routines
8055** that does no real locking and is appropriate for use in
8056** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8057** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
8058** and Windows.
8059**
8060** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
8061** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8062** implementation is included with the library. In this case the
8063** application must supply a custom mutex implementation using the
8064** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
8065** before calling sqlite3_initialize() or any other public sqlite3_
8066** function that calls sqlite3_initialize().
8067**
8068** ^The sqlite3_mutex_alloc() routine allocates a new
8069** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
8070** routine returns NULL if it is unable to allocate the requested
8071** mutex. The argument to sqlite3_mutex_alloc() must one of these
8072** integer constants:
8073**
8074** <ul>
8075** <li> SQLITE_MUTEX_FAST
8076** <li> SQLITE_MUTEX_RECURSIVE
8077** <li> SQLITE_MUTEX_STATIC_MASTER
8078** <li> SQLITE_MUTEX_STATIC_MEM
8079** <li> SQLITE_MUTEX_STATIC_OPEN
8080** <li> SQLITE_MUTEX_STATIC_PRNG
8081** <li> SQLITE_MUTEX_STATIC_LRU
8082** <li> SQLITE_MUTEX_STATIC_PMEM
8083** <li> SQLITE_MUTEX_STATIC_APP1
8084** <li> SQLITE_MUTEX_STATIC_APP2
8085** <li> SQLITE_MUTEX_STATIC_APP3
8086** <li> SQLITE_MUTEX_STATIC_VFS1
8087** <li> SQLITE_MUTEX_STATIC_VFS2
8088** <li> SQLITE_MUTEX_STATIC_VFS3
8089** </ul>
8090**
8091** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
8092** cause sqlite3_mutex_alloc() to create
8093** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
8094** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
8095** The mutex implementation does not need to make a distinction
8096** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
8097** not want to. SQLite will only request a recursive mutex in
8098** cases where it really needs one. If a faster non-recursive mutex
8099** implementation is available on the host platform, the mutex subsystem
8100** might return such a mutex in response to SQLITE_MUTEX_FAST.
8101**
8102** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
8103** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
8104** a pointer to a static preexisting mutex. ^Nine static mutexes are
8105** used by the current version of SQLite. Future versions of SQLite
8106** may add additional static mutexes. Static mutexes are for internal
8107** use by SQLite only. Applications that use SQLite mutexes should
8108** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
8109** SQLITE_MUTEX_RECURSIVE.
8110**
8111** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
8112** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
8113** returns a different mutex on every call. ^For the static
8114** mutex types, the same mutex is returned on every call that has
8115** the same type number.
8116**
8117** ^The sqlite3_mutex_free() routine deallocates a previously
8118** allocated dynamic mutex. Attempting to deallocate a static
8119** mutex results in undefined behavior.
8120**
8121** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
8122** to enter a mutex. ^If another thread is already within the mutex,
8123** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
8124** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
8125** upon successful entry. ^(Mutexes created using
8126** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
8127** In such cases, the
8128** mutex must be exited an equal number of times before another thread
8129** can enter.)^ If the same thread tries to enter any mutex other
8130** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
8131**
8132** ^(Some systems (for example, Windows 95) do not support the operation
8133** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8134** will always return SQLITE_BUSY. The SQLite core only ever uses
8135** sqlite3_mutex_try() as an optimization so this is acceptable
8136** behavior.)^
8137**
8138** ^The sqlite3_mutex_leave() routine exits a mutex that was
8139** previously entered by the same thread. The behavior
8140** is undefined if the mutex is not currently entered by the
8141** calling thread or is not currently allocated.
8142**
8143** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
8144** sqlite3_mutex_leave() is a NULL pointer, then all three routines
8145** behave as no-ops.
8146**
8147** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
8148*/
8149SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
8150SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
8151SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
8152SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
8153SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
8154
8155/*
8156** CAPI3REF: Mutex Methods Object
8157**
8158** An instance of this structure defines the low-level routines
8159** used to allocate and use mutexes.
8160**
8161** Usually, the default mutex implementations provided by SQLite are
8162** sufficient, however the application has the option of substituting a custom
8163** implementation for specialized deployments or systems for which SQLite
8164** does not provide a suitable implementation. In this case, the application
8165** creates and populates an instance of this structure to pass
8166** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
8167** Additionally, an instance of this structure can be used as an
8168** output variable when querying the system for the current mutex
8169** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
8170**
8171** ^The xMutexInit method defined by this structure is invoked as
8172** part of system initialization by the sqlite3_initialize() function.
8173** ^The xMutexInit routine is called by SQLite exactly once for each
8174** effective call to [sqlite3_initialize()].
8175**
8176** ^The xMutexEnd method defined by this structure is invoked as
8177** part of system shutdown by the sqlite3_shutdown() function. The
8178** implementation of this method is expected to release all outstanding
8179** resources obtained by the mutex methods implementation, especially
8180** those obtained by the xMutexInit method. ^The xMutexEnd()
8181** interface is invoked exactly once for each call to [sqlite3_shutdown()].
8182**
8183** ^(The remaining seven methods defined by this structure (xMutexAlloc,
8184** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
8185** xMutexNotheld) implement the following interfaces (respectively):
8186**
8187** <ul>
8188** <li> [sqlite3_mutex_alloc()] </li>
8189** <li> [sqlite3_mutex_free()] </li>
8190** <li> [sqlite3_mutex_enter()] </li>
8191** <li> [sqlite3_mutex_try()] </li>
8192** <li> [sqlite3_mutex_leave()] </li>
8193** <li> [sqlite3_mutex_held()] </li>
8194** <li> [sqlite3_mutex_notheld()] </li>
8195** </ul>)^
8196**
8197** The only difference is that the public sqlite3_XXX functions enumerated
8198** above silently ignore any invocations that pass a NULL pointer instead
8199** of a valid mutex handle. The implementations of the methods defined
8200** by this structure are not required to handle this case, the results
8201** of passing a NULL pointer instead of a valid mutex handle are undefined
8202** (i.e. it is acceptable to provide an implementation that segfaults if
8203** it is passed a NULL pointer).
8204**
8205** The xMutexInit() method must be threadsafe. It must be harmless to
8206** invoke xMutexInit() multiple times within the same process and without
8207** intervening calls to xMutexEnd(). Second and subsequent calls to
8208** xMutexInit() must be no-ops.
8209**
8210** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
8211** and its associates). Similarly, xMutexAlloc() must not use SQLite memory
8212** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
8213** memory allocation for a fast or recursive mutex.
8214**
8215** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
8216** called, but only if the prior call to xMutexInit returned SQLITE_OK.
8217** If xMutexInit fails in any way, it is expected to clean up after itself
8218** prior to returning.
8219*/
8220typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
8221struct sqlite3_mutex_methods {
8222 int (*xMutexInit)(void);
8223 int (*xMutexEnd)(void);
8224 sqlite3_mutex *(*xMutexAlloc)(int);
8225 void (*xMutexFree)(sqlite3_mutex *);
8226 void (*xMutexEnter)(sqlite3_mutex *);
8227 int (*xMutexTry)(sqlite3_mutex *);
8228 void (*xMutexLeave)(sqlite3_mutex *);
8229 int (*xMutexHeld)(sqlite3_mutex *);
8230 int (*xMutexNotheld)(sqlite3_mutex *);
8231};
8232
8233/*
8234** CAPI3REF: Mutex Verification Routines
8235**
8236** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
8237** are intended for use inside assert() statements. The SQLite core
8238** never uses these routines except inside an assert() and applications
8239** are advised to follow the lead of the core. The SQLite core only
8240** provides implementations for these routines when it is compiled
8241** with the SQLITE_DEBUG flag. External mutex implementations
8242** are only required to provide these routines if SQLITE_DEBUG is
8243** defined and if NDEBUG is not defined.
8244**
8245** These routines should return true if the mutex in their argument
8246** is held or not held, respectively, by the calling thread.
8247**
8248** The implementation is not required to provide versions of these
8249** routines that actually work. If the implementation does not provide working
8250** versions of these routines, it should at least provide stubs that always
8251** return true so that one does not get spurious assertion failures.
8252**
8253** If the argument to sqlite3_mutex_held() is a NULL pointer then
8254** the routine should return 1. This seems counter-intuitive since
8255** clearly the mutex cannot be held if it does not exist. But
8256** the reason the mutex does not exist is because the build is not
8257** using mutexes. And we do not want the assert() containing the
8258** call to sqlite3_mutex_held() to fail, so a non-zero return is
8259** the appropriate thing to do. The sqlite3_mutex_notheld()
8260** interface should also return 1 when given a NULL pointer.
8261*/
8262#ifndef NDEBUG1
8263SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
8264SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
8265#endif
8266
8267/*
8268** CAPI3REF: Mutex Types
8269**
8270** The [sqlite3_mutex_alloc()] interface takes a single argument
8271** which is one of these integer constants.
8272**
8273** The set of static mutexes may change from one SQLite release to the
8274** next. Applications that override the built-in mutex logic must be
8275** prepared to accommodate additional static mutexes.
8276*/
8277#define SQLITE_MUTEX_FAST0 0
8278#define SQLITE_MUTEX_RECURSIVE1 1
8279#define SQLITE_MUTEX_STATIC_MASTER2 2
8280#define SQLITE_MUTEX_STATIC_MEM3 3 /* sqlite3_malloc() */
8281#define SQLITE_MUTEX_STATIC_MEM24 4 /* NOT USED */
8282#define SQLITE_MUTEX_STATIC_OPEN4 4 /* sqlite3BtreeOpen() */
8283#define SQLITE_MUTEX_STATIC_PRNG5 5 /* sqlite3_randomness() */
8284#define SQLITE_MUTEX_STATIC_LRU6 6 /* lru page list */
8285#define SQLITE_MUTEX_STATIC_LRU27 7 /* NOT USED */
8286#define SQLITE_MUTEX_STATIC_PMEM7 7 /* sqlite3PageMalloc() */
8287#define SQLITE_MUTEX_STATIC_APP18 8 /* For use by application */
8288#define SQLITE_MUTEX_STATIC_APP29 9 /* For use by application */
8289#define SQLITE_MUTEX_STATIC_APP310 10 /* For use by application */
8290#define SQLITE_MUTEX_STATIC_VFS111 11 /* For use by built-in VFS */
8291#define SQLITE_MUTEX_STATIC_VFS212 12 /* For use by extension VFS */
8292#define SQLITE_MUTEX_STATIC_VFS313 13 /* For use by application VFS */
8293
8294/*
8295** CAPI3REF: Retrieve the mutex for a database connection
8296** METHOD: sqlite3
8297**
8298** ^This interface returns a pointer the [sqlite3_mutex] object that
8299** serializes access to the [database connection] given in the argument
8300** when the [threading mode] is Serialized.
8301** ^If the [threading mode] is Single-thread or Multi-thread then this
8302** routine returns a NULL pointer.
8303*/
8304SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
8305
8306/*
8307** CAPI3REF: Low-Level Control Of Database Files
8308** METHOD: sqlite3
8309** KEYWORDS: {file control}
8310**
8311** ^The [sqlite3_file_control()] interface makes a direct call to the
8312** xFileControl method for the [sqlite3_io_methods] object associated
8313** with a particular database identified by the second argument. ^The
8314** name of the database is "main" for the main database or "temp" for the
8315** TEMP database, or the name that appears after the AS keyword for
8316** databases that are added using the [ATTACH] SQL command.
8317** ^A NULL pointer can be used in place of "main" to refer to the
8318** main database file.
8319** ^The third and fourth parameters to this routine
8320** are passed directly through to the second and third parameters of
8321** the xFileControl method. ^The return value of the xFileControl
8322** method becomes the return value of this routine.
8323**
8324** A few opcodes for [sqlite3_file_control()] are handled directly
8325** by the SQLite core and never invoke the
8326** sqlite3_io_methods.xFileControl method.
8327** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
8328** a pointer to the underlying [sqlite3_file] object to be written into
8329** the space pointed to by the 4th parameter. The
8330** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
8331** the [sqlite3_file] object associated with the journal file instead of
8332** the main database. The [SQLITE_FCNTL_VFS_POINTER] opcode returns
8333** a pointer to the underlying [sqlite3_vfs] object for the file.
8334** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
8335** from the pager.
8336**
8337** ^If the second parameter (zDbName) does not match the name of any
8338** open database file, then SQLITE_ERROR is returned. ^This error
8339** code is not remembered and will not be recalled by [sqlite3_errcode()]
8340** or [sqlite3_errmsg()]. The underlying xFileControl method might
8341** also return SQLITE_ERROR. There is no way to distinguish between
8342** an incorrect zDbName and an SQLITE_ERROR return from the underlying
8343** xFileControl method.
8344**
8345** See also: [file control opcodes]
8346*/
8347SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
8348
8349/*
8350** CAPI3REF: Testing Interface
8351**
8352** ^The sqlite3_test_control() interface is used to read out internal
8353** state of SQLite and to inject faults into SQLite for testing
8354** purposes. ^The first parameter is an operation code that determines
8355** the number, meaning, and operation of all subsequent parameters.
8356**
8357** This interface is not for use by applications. It exists solely
8358** for verifying the correct operation of the SQLite library. Depending
8359** on how the SQLite library is compiled, this interface might not exist.
8360**
8361** The details of the operation codes, their meanings, the parameters
8362** they take, and what they do are all subject to change without notice.
8363** Unlike most of the SQLite API, this function is not guaranteed to
8364** operate consistently from one release to the next.
8365*/
8366SQLITE_API int sqlite3_test_control(int op, ...);
8367
8368/*
8369** CAPI3REF: Testing Interface Operation Codes
8370**
8371** These constants are the valid operation code parameters used
8372** as the first argument to [sqlite3_test_control()].
8373**
8374** These parameters and their meanings are subject to change
8375** without notice. These values are for testing purposes only.
8376** Applications should not use any of these parameters or the
8377** [sqlite3_test_control()] interface.
8378*/
8379#define SQLITE_TESTCTRL_FIRST5 5
8380#define SQLITE_TESTCTRL_PRNG_SAVE5 5
8381#define SQLITE_TESTCTRL_PRNG_RESTORE6 6
8382#define SQLITE_TESTCTRL_PRNG_RESET7 7
8383#define SQLITE_TESTCTRL_BITVEC_TEST8 8
8384#define SQLITE_TESTCTRL_FAULT_INSTALL9 9
8385#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS10 10
8386#define SQLITE_TESTCTRL_PENDING_BYTE11 11
8387#define SQLITE_TESTCTRL_ASSERT12 12
8388#define SQLITE_TESTCTRL_ALWAYS13 13
8389#define SQLITE_TESTCTRL_RESERVE14 14
8390#define SQLITE_TESTCTRL_OPTIMIZATIONS15 15
8391#define SQLITE_TESTCTRL_ISKEYWORD16 16 /* NOT USED */
8392#define SQLITE_TESTCTRL_SCRATCHMALLOC17 17 /* NOT USED */
8393#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS17 17
8394#define SQLITE_TESTCTRL_LOCALTIME_FAULT18 18
8395#define SQLITE_TESTCTRL_EXPLAIN_STMT19 19 /* NOT USED */
8396#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD19 19
8397#define SQLITE_TESTCTRL_NEVER_CORRUPT20 20
8398#define SQLITE_TESTCTRL_VDBE_COVERAGE21 21
8399#define SQLITE_TESTCTRL_BYTEORDER22 22
8400#define SQLITE_TESTCTRL_ISINIT23 23
8401#define SQLITE_TESTCTRL_SORTER_MMAP24 24
8402#define SQLITE_TESTCTRL_IMPOSTER25 25
8403#define SQLITE_TESTCTRL_PARSER_COVERAGE26 26
8404#define SQLITE_TESTCTRL_RESULT_INTREAL27 27
8405#define SQLITE_TESTCTRL_LAST27 27 /* Largest TESTCTRL */
8406
8407/*
8408** CAPI3REF: SQL Keyword Checking
8409**
8410** These routines provide access to the set of SQL language keywords
8411** recognized by SQLite. Applications can uses these routines to determine
8412** whether or not a specific identifier needs to be escaped (for example,
8413** by enclosing in double-quotes) so as not to confuse the parser.
8414**
8415** The sqlite3_keyword_count() interface returns the number of distinct
8416** keywords understood by SQLite.
8417**
8418** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8419** makes *Z point to that keyword expressed as UTF8 and writes the number
8420** of bytes in the keyword into *L. The string that *Z points to is not
8421** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8422** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
8423** or L are NULL or invalid pointers then calls to
8424** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
8425**
8426** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
8427** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8428** if it is and zero if not.
8429**
8430** The parser used by SQLite is forgiving. It is often possible to use
8431** a keyword as an identifier as long as such use does not result in a
8432** parsing ambiguity. For example, the statement
8433** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
8434** creates a new table named "BEGIN" with three columns named
8435** "REPLACE", "PRAGMA", and "END". Nevertheless, best practice is to avoid
8436** using keywords as identifiers. Common techniques used to avoid keyword
8437** name collisions include:
8438** <ul>
8439** <li> Put all identifier names inside double-quotes. This is the official
8440** SQL way to escape identifier names.
8441** <li> Put identifier names inside &#91;...&#93;. This is not standard SQL,
8442** but it is what SQL Server does and so lots of programmers use this
8443** technique.
8444** <li> Begin every identifier with the letter "Z" as no SQL keywords start
8445** with "Z".
8446** <li> Include a digit somewhere in every identifier name.
8447** </ul>
8448**
8449** Note that the number of keywords understood by SQLite can depend on
8450** compile-time options. For example, "VACUUM" is not a keyword if
8451** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8452** new keywords may be added to future releases of SQLite.
8453*/
8454SQLITE_API int sqlite3_keyword_count(void);
8455SQLITE_API int sqlite3_keyword_name(int,const char**,int*);
8456SQLITE_API int sqlite3_keyword_check(const char*,int);
8457
8458/*
8459** CAPI3REF: Dynamic String Object
8460** KEYWORDS: {dynamic string}
8461**
8462** An instance of the sqlite3_str object contains a dynamically-sized
8463** string under construction.
8464**
8465** The lifecycle of an sqlite3_str object is as follows:
8466** <ol>
8467** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
8468** <li> ^Text is appended to the sqlite3_str object using various
8469** methods, such as [sqlite3_str_appendf()].
8470** <li> ^The sqlite3_str object is destroyed and the string it created
8471** is returned using the [sqlite3_str_finish()] interface.
8472** </ol>
8473*/
8474typedef struct sqlite3_str sqlite3_str;
8475
8476/*
8477** CAPI3REF: Create A New Dynamic String Object
8478** CONSTRUCTOR: sqlite3_str
8479**
8480** ^The [sqlite3_str_new(D)] interface allocates and initializes
8481** a new [sqlite3_str] object. To avoid memory leaks, the object returned by
8482** [sqlite3_str_new()] must be freed by a subsequent call to
8483** [sqlite3_str_finish(X)].
8484**
8485** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
8486** valid [sqlite3_str] object, though in the event of an out-of-memory
8487** error the returned object might be a special singleton that will
8488** silently reject new text, always return SQLITE_NOMEM from
8489** [sqlite3_str_errcode()], always return 0 for
8490** [sqlite3_str_length()], and always return NULL from
8491** [sqlite3_str_finish(X)]. It is always safe to use the value
8492** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
8493** to any of the other [sqlite3_str] methods.
8494**
8495** The D parameter to [sqlite3_str_new(D)] may be NULL. If the
8496** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
8497** length of the string contained in the [sqlite3_str] object will be
8498** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
8499** of [SQLITE_MAX_LENGTH].
8500*/
8501SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
8502
8503/*
8504** CAPI3REF: Finalize A Dynamic String
8505** DESTRUCTOR: sqlite3_str
8506**
8507** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
8508** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
8509** that contains the constructed string. The calling application should
8510** pass the returned value to [sqlite3_free()] to avoid a memory leak.
8511** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
8512** errors were encountered during construction of the string. ^The
8513** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
8514** string in [sqlite3_str] object X is zero bytes long.
8515*/
8516SQLITE_API char *sqlite3_str_finish(sqlite3_str*);
8517
8518/*
8519** CAPI3REF: Add Content To A Dynamic String
8520** METHOD: sqlite3_str
8521**
8522** These interfaces add content to an sqlite3_str object previously obtained
8523** from [sqlite3_str_new()].
8524**
8525** ^The [sqlite3_str_appendf(X,F,...)] and
8526** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8527** functionality of SQLite to append formatted text onto the end of
8528** [sqlite3_str] object X.
8529**
8530** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
8531** onto the end of the [sqlite3_str] object X. N must be non-negative.
8532** S must contain at least N non-zero bytes of content. To append a
8533** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8534** method instead.
8535**
8536** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
8537** zero-terminated string S onto the end of [sqlite3_str] object X.
8538**
8539** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
8540** single-byte character C onto the end of [sqlite3_str] object X.
8541** ^This method can be used, for example, to add whitespace indentation.
8542**
8543** ^The [sqlite3_str_reset(X)] method resets the string under construction
8544** inside [sqlite3_str] object X back to zero bytes in length.
8545**
8546** These methods do not return a result code. ^If an error occurs, that fact
8547** is recorded in the [sqlite3_str] object and can be recovered by a
8548** subsequent call to [sqlite3_str_errcode(X)].
8549*/
8550SQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
8551SQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
8552SQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
8553SQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
8554SQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
8555SQLITE_API void sqlite3_str_reset(sqlite3_str*);
8556
8557/*
8558** CAPI3REF: Status Of A Dynamic String
8559** METHOD: sqlite3_str
8560**
8561** These interfaces return the current status of an [sqlite3_str] object.
8562**
8563** ^If any prior errors have occurred while constructing the dynamic string
8564** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
8565** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns
8566** [SQLITE_NOMEM] following any out-of-memory error, or
8567** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
8568** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
8569**
8570** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
8571** of the dynamic string under construction in [sqlite3_str] object X.
8572** ^The length returned by [sqlite3_str_length(X)] does not include the
8573** zero-termination byte.
8574**
8575** ^The [sqlite3_str_value(X)] method returns a pointer to the current
8576** content of the dynamic string under construction in X. The value
8577** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
8578** and might be freed or altered by any subsequent method on the same
8579** [sqlite3_str] object. Applications must not used the pointer returned
8580** [sqlite3_str_value(X)] after any subsequent method call on the same
8581** object. ^Applications may change the content of the string returned
8582** by [sqlite3_str_value(X)] as long as they do not write into any bytes
8583** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
8584** write any byte after any subsequent sqlite3_str method call.
8585*/
8586SQLITE_API int sqlite3_str_errcode(sqlite3_str*);
8587SQLITE_API int sqlite3_str_length(sqlite3_str*);
8588SQLITE_API char *sqlite3_str_value(sqlite3_str*);
8589
8590/*
8591** CAPI3REF: SQLite Runtime Status
8592**
8593** ^These interfaces are used to retrieve runtime status information
8594** about the performance of SQLite, and optionally to reset various
8595** highwater marks. ^The first argument is an integer code for
8596** the specific parameter to measure. ^(Recognized integer codes
8597** are of the form [status parameters | SQLITE_STATUS_...].)^
8598** ^The current value of the parameter is returned into *pCurrent.
8599** ^The highest recorded value is returned in *pHighwater. ^If the
8600** resetFlag is true, then the highest record value is reset after
8601** *pHighwater is written. ^(Some parameters do not record the highest
8602** value. For those parameters
8603** nothing is written into *pHighwater and the resetFlag is ignored.)^
8604** ^(Other parameters record only the highwater mark and not the current
8605** value. For these latter parameters nothing is written into *pCurrent.)^
8606**
8607** ^The sqlite3_status() and sqlite3_status64() routines return
8608** SQLITE_OK on success and a non-zero [error code] on failure.
8609**
8610** If either the current value or the highwater mark is too large to
8611** be represented by a 32-bit integer, then the values returned by
8612** sqlite3_status() are undefined.
8613**
8614** See also: [sqlite3_db_status()]
8615*/
8616SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
8617SQLITE_API int sqlite3_status64(
8618 int op,
8619 sqlite3_int64 *pCurrent,
8620 sqlite3_int64 *pHighwater,
8621 int resetFlag
8622);
8623
8624
8625/*
8626** CAPI3REF: Status Parameters
8627** KEYWORDS: {status parameters}
8628**
8629** These integer constants designate various run-time status parameters
8630** that can be returned by [sqlite3_status()].
8631**
8632** <dl>
8633** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8634** <dd>This parameter is the current amount of memory checked out
8635** using [sqlite3_malloc()], either directly or indirectly. The
8636** figure includes calls made to [sqlite3_malloc()] by the application
8637** and internal memory usage by the SQLite library. Auxiliary page-cache
8638** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8639** this parameter. The amount returned is the sum of the allocation
8640** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8641**
8642** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
8643** <dd>This parameter records the largest memory allocation request
8644** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
8645** internal equivalents). Only the value returned in the
8646** *pHighwater parameter to [sqlite3_status()] is of interest.
8647** The value written into the *pCurrent parameter is undefined.</dd>)^
8648**
8649** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
8650** <dd>This parameter records the number of separate memory allocations
8651** currently checked out.</dd>)^
8652**
8653** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
8654** <dd>This parameter returns the number of pages used out of the
8655** [pagecache memory allocator] that was configured using
8656** [SQLITE_CONFIG_PAGECACHE]. The
8657** value returned is in pages, not in bytes.</dd>)^
8658**
8659** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
8660** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
8661** <dd>This parameter returns the number of bytes of page cache
8662** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
8663** buffer and where forced to overflow to [sqlite3_malloc()]. The
8664** returned value includes allocations that overflowed because they
8665** where too large (they were larger than the "sz" parameter to
8666** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
8667** no space was left in the page cache.</dd>)^
8668**
8669** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
8670** <dd>This parameter records the largest memory allocation request
8671** handed to [pagecache memory allocator]. Only the value returned in the
8672** *pHighwater parameter to [sqlite3_status()] is of interest.
8673** The value written into the *pCurrent parameter is undefined.</dd>)^
8674**
8675** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
8676** <dd>No longer used.</dd>
8677**
8678** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
8679** <dd>No longer used.</dd>
8680**
8681** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
8682** <dd>No longer used.</dd>
8683**
8684** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
8685** <dd>The *pHighwater parameter records the deepest parser stack.
8686** The *pCurrent value is undefined. The *pHighwater value is only
8687** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
8688** </dl>
8689**
8690** New status parameters may be added from time to time.
8691*/
8692#define SQLITE_STATUS_MEMORY_USED0 0
8693#define SQLITE_STATUS_PAGECACHE_USED1 1
8694#define SQLITE_STATUS_PAGECACHE_OVERFLOW2 2
8695#define SQLITE_STATUS_SCRATCH_USED3 3 /* NOT USED */
8696#define SQLITE_STATUS_SCRATCH_OVERFLOW4 4 /* NOT USED */
8697#define SQLITE_STATUS_MALLOC_SIZE5 5
8698#define SQLITE_STATUS_PARSER_STACK6 6
8699#define SQLITE_STATUS_PAGECACHE_SIZE7 7
8700#define SQLITE_STATUS_SCRATCH_SIZE8 8 /* NOT USED */
8701#define SQLITE_STATUS_MALLOC_COUNT9 9
8702
8703/*
8704** CAPI3REF: Database Connection Status
8705** METHOD: sqlite3
8706**
8707** ^This interface is used to retrieve runtime status information
8708** about a single [database connection]. ^The first argument is the
8709** database connection object to be interrogated. ^The second argument
8710** is an integer constant, taken from the set of
8711** [SQLITE_DBSTATUS options], that
8712** determines the parameter to interrogate. The set of
8713** [SQLITE_DBSTATUS options] is likely
8714** to grow in future releases of SQLite.
8715**
8716** ^The current value of the requested parameter is written into *pCur
8717** and the highest instantaneous value is written into *pHiwtr. ^If
8718** the resetFlg is true, then the highest instantaneous value is
8719** reset back down to the current value.
8720**
8721** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
8722** non-zero [error code] on failure.
8723**
8724** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
8725*/
8726SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
8727
8728/*
8729** CAPI3REF: Status Parameters for database connections
8730** KEYWORDS: {SQLITE_DBSTATUS options}
8731**
8732** These constants are the available integer "verbs" that can be passed as
8733** the second argument to the [sqlite3_db_status()] interface.
8734**
8735** New verbs may be added in future releases of SQLite. Existing verbs
8736** might be discontinued. Applications should check the return code from
8737** [sqlite3_db_status()] to make sure that the call worked.
8738** The [sqlite3_db_status()] interface will return a non-zero error code
8739** if a discontinued or unsupported verb is invoked.
8740**
8741** <dl>
8742** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
8743** <dd>This parameter returns the number of lookaside memory slots currently
8744** checked out.</dd>)^
8745**
8746** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
8747** <dd>This parameter returns the number malloc attempts that were
8748** satisfied using lookaside memory. Only the high-water value is meaningful;
8749** the current value is always zero.)^
8750**
8751** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
8752** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
8753** <dd>This parameter returns the number malloc attempts that might have
8754** been satisfied using lookaside memory but failed due to the amount of
8755** memory requested being larger than the lookaside slot size.
8756** Only the high-water value is meaningful;
8757** the current value is always zero.)^
8758**
8759** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
8760** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
8761** <dd>This parameter returns the number malloc attempts that might have
8762** been satisfied using lookaside memory but failed due to all lookaside
8763** memory already being in use.
8764** Only the high-water value is meaningful;
8765** the current value is always zero.)^
8766**
8767** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
8768** <dd>This parameter returns the approximate number of bytes of heap
8769** memory used by all pager caches associated with the database connection.)^
8770** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
8771**
8772** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
8773** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
8774** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
8775** pager cache is shared between two or more connections the bytes of heap
8776** memory used by that pager cache is divided evenly between the attached
8777** connections.)^ In other words, if none of the pager caches associated
8778** with the database connection are shared, this request returns the same
8779** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
8780** shared, the value returned by this call will be smaller than that returned
8781** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
8782** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
8783**
8784** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
8785** <dd>This parameter returns the approximate number of bytes of heap
8786** memory used to store the schema for all databases associated
8787** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8788** ^The full amount of memory used by the schemas is reported, even if the
8789** schema memory is shared with other database connections due to
8790** [shared cache mode] being enabled.
8791** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
8792**
8793** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
8794** <dd>This parameter returns the approximate number of bytes of heap
8795** and lookaside memory used by all prepared statements associated with
8796** the database connection.)^
8797** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
8798** </dd>
8799**
8800** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
8801** <dd>This parameter returns the number of pager cache hits that have
8802** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
8803** is always 0.
8804** </dd>
8805**
8806** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
8807** <dd>This parameter returns the number of pager cache misses that have
8808** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
8809** is always 0.
8810** </dd>
8811**
8812** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
8813** <dd>This parameter returns the number of dirty cache entries that have
8814** been written to disk. Specifically, the number of pages written to the
8815** wal file in wal mode databases, or the number of pages written to the
8816** database file in rollback mode databases. Any pages written as part of
8817** transaction rollback or database recovery operations are not included.
8818** If an IO or other error occurs while writing a page to disk, the effect
8819** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
8820** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
8821** </dd>
8822**
8823** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
8824** <dd>This parameter returns the number of dirty cache entries that have
8825** been written to disk in the middle of a transaction due to the page
8826** cache overflowing. Transactions are more efficient if they are written
8827** to disk all at once. When pages spill mid-transaction, that introduces
8828** additional overhead. This parameter can be used help identify
8829** inefficiencies that can be resolve by increasing the cache size.
8830** </dd>
8831**
8832** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
8833** <dd>This parameter returns zero for the current value if and only if
8834** all foreign key constraints (deferred or immediate) have been
8835** resolved.)^ ^The highwater mark is always 0.
8836** </dd>
8837** </dl>
8838*/
8839#define SQLITE_DBSTATUS_LOOKASIDE_USED0 0
8840#define SQLITE_DBSTATUS_CACHE_USED1 1
8841#define SQLITE_DBSTATUS_SCHEMA_USED2 2
8842#define SQLITE_DBSTATUS_STMT_USED3 3
8843#define SQLITE_DBSTATUS_LOOKASIDE_HIT4 4
8844#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE5 5
8845#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL6 6
8846#define SQLITE_DBSTATUS_CACHE_HIT7 7
8847#define SQLITE_DBSTATUS_CACHE_MISS8 8
8848#define SQLITE_DBSTATUS_CACHE_WRITE9 9
8849#define SQLITE_DBSTATUS_DEFERRED_FKS10 10
8850#define SQLITE_DBSTATUS_CACHE_USED_SHARED11 11
8851#define SQLITE_DBSTATUS_CACHE_SPILL12 12
8852#define SQLITE_DBSTATUS_MAX12 12 /* Largest defined DBSTATUS */
8853
8854
8855/*
8856** CAPI3REF: Prepared Statement Status
8857** METHOD: sqlite3_stmt
8858**
8859** ^(Each prepared statement maintains various
8860** [SQLITE_STMTSTATUS counters] that measure the number
8861** of times it has performed specific operations.)^ These counters can
8862** be used to monitor the performance characteristics of the prepared
8863** statements. For example, if the number of table steps greatly exceeds
8864** the number of table searches or result rows, that would tend to indicate
8865** that the prepared statement is using a full table scan rather than
8866** an index.
8867**
8868** ^(This interface is used to retrieve and reset counter values from
8869** a [prepared statement]. The first argument is the prepared statement
8870** object to be interrogated. The second argument
8871** is an integer code for a specific [SQLITE_STMTSTATUS counter]
8872** to be interrogated.)^
8873** ^The current value of the requested counter is returned.
8874** ^If the resetFlg is true, then the counter is reset to zero after this
8875** interface call returns.
8876**
8877** See also: [sqlite3_status()] and [sqlite3_db_status()].
8878*/
8879SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
8880
8881/*
8882** CAPI3REF: Status Parameters for prepared statements
8883** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
8884**
8885** These preprocessor macros define integer codes that name counter
8886** values associated with the [sqlite3_stmt_status()] interface.
8887** The meanings of the various counters are as follows:
8888**
8889** <dl>
8890** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
8891** <dd>^This is the number of times that SQLite has stepped forward in
8892** a table as part of a full table scan. Large numbers for this counter
8893** may indicate opportunities for performance improvement through
8894** careful use of indices.</dd>
8895**
8896** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
8897** <dd>^This is the number of sort operations that have occurred.
8898** A non-zero value in this counter may indicate an opportunity to
8899** improvement performance through careful use of indices.</dd>
8900**
8901** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
8902** <dd>^This is the number of rows inserted into transient indices that
8903** were created automatically in order to help joins run faster.
8904** A non-zero value in this counter may indicate an opportunity to
8905** improvement performance by adding permanent indices that do not
8906** need to be reinitialized each time the statement is run.</dd>
8907**
8908** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
8909** <dd>^This is the number of virtual machine operations executed
8910** by the prepared statement if that number is less than or equal
8911** to 2147483647. The number of virtual machine operations can be
8912** used as a proxy for the total work done by the prepared statement.
8913** If the number of virtual machine operations exceeds 2147483647
8914** then the value returned by this statement status code is undefined.
8915**
8916** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
8917** <dd>^This is the number of times that the prepare statement has been
8918** automatically regenerated due to schema changes or change to
8919** [bound parameters] that might affect the query plan.
8920**
8921** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
8922** <dd>^This is the number of times that the prepared statement has
8923** been run. A single "run" for the purposes of this counter is one
8924** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
8925** The counter is incremented on the first [sqlite3_step()] call of each
8926** cycle.
8927**
8928** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
8929** <dd>^This is the approximate number of bytes of heap memory
8930** used to store the prepared statement. ^This value is not actually
8931** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
8932** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
8933** </dd>
8934** </dl>
8935*/
8936#define SQLITE_STMTSTATUS_FULLSCAN_STEP1 1
8937#define SQLITE_STMTSTATUS_SORT2 2
8938#define SQLITE_STMTSTATUS_AUTOINDEX3 3
8939#define SQLITE_STMTSTATUS_VM_STEP4 4
8940#define SQLITE_STMTSTATUS_REPREPARE5 5
8941#define SQLITE_STMTSTATUS_RUN6 6
8942#define SQLITE_STMTSTATUS_MEMUSED99 99
8943
8944/*
8945** CAPI3REF: Custom Page Cache Object
8946**
8947** The sqlite3_pcache type is opaque. It is implemented by
8948** the pluggable module. The SQLite core has no knowledge of
8949** its size or internal structure and never deals with the
8950** sqlite3_pcache object except by holding and passing pointers
8951** to the object.
8952**
8953** See [sqlite3_pcache_methods2] for additional information.
8954*/
8955typedef struct sqlite3_pcache sqlite3_pcache;
8956
8957/*
8958** CAPI3REF: Custom Page Cache Object
8959**
8960** The sqlite3_pcache_page object represents a single page in the
8961** page cache. The page cache will allocate instances of this
8962** object. Various methods of the page cache use pointers to instances
8963** of this object as parameters or as their return value.
8964**
8965** See [sqlite3_pcache_methods2] for additional information.
8966*/
8967typedef struct sqlite3_pcache_page sqlite3_pcache_page;
8968struct sqlite3_pcache_page {
8969 void *pBuf; /* The content of the page */
8970 void *pExtra; /* Extra information associated with the page */
8971};
8972
8973/*
8974** CAPI3REF: Application Defined Page Cache.
8975** KEYWORDS: {page cache}
8976**
8977** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
8978** register an alternative page cache implementation by passing in an
8979** instance of the sqlite3_pcache_methods2 structure.)^
8980** In many applications, most of the heap memory allocated by
8981** SQLite is used for the page cache.
8982** By implementing a
8983** custom page cache using this API, an application can better control
8984** the amount of memory consumed by SQLite, the way in which
8985** that memory is allocated and released, and the policies used to
8986** determine exactly which parts of a database file are cached and for
8987** how long.
8988**
8989** The alternative page cache mechanism is an
8990** extreme measure that is only needed by the most demanding applications.
8991** The built-in page cache is recommended for most uses.
8992**
8993** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
8994** internal buffer by SQLite within the call to [sqlite3_config]. Hence
8995** the application may discard the parameter after the call to
8996** [sqlite3_config()] returns.)^
8997**
8998** [[the xInit() page cache method]]
8999** ^(The xInit() method is called once for each effective
9000** call to [sqlite3_initialize()])^
9001** (usually only once during the lifetime of the process). ^(The xInit()
9002** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
9003** The intent of the xInit() method is to set up global data structures
9004** required by the custom page cache implementation.
9005** ^(If the xInit() method is NULL, then the
9006** built-in default page cache is used instead of the application defined
9007** page cache.)^
9008**
9009** [[the xShutdown() page cache method]]
9010** ^The xShutdown() method is called by [sqlite3_shutdown()].
9011** It can be used to clean up
9012** any outstanding resources before process shutdown, if required.
9013** ^The xShutdown() method may be NULL.
9014**
9015** ^SQLite automatically serializes calls to the xInit method,
9016** so the xInit method need not be threadsafe. ^The
9017** xShutdown method is only called from [sqlite3_shutdown()] so it does
9018** not need to be threadsafe either. All other methods must be threadsafe
9019** in multithreaded applications.
9020**
9021** ^SQLite will never invoke xInit() more than once without an intervening
9022** call to xShutdown().
9023**
9024** [[the xCreate() page cache methods]]
9025** ^SQLite invokes the xCreate() method to construct a new cache instance.
9026** SQLite will typically create one cache instance for each open database file,
9027** though this is not guaranteed. ^The
9028** first parameter, szPage, is the size in bytes of the pages that must
9029** be allocated by the cache. ^szPage will always a power of two. ^The
9030** second parameter szExtra is a number of bytes of extra storage
9031** associated with each page cache entry. ^The szExtra parameter will
9032** a number less than 250. SQLite will use the
9033** extra szExtra bytes on each page to store metadata about the underlying
9034** database page on disk. The value passed into szExtra depends
9035** on the SQLite version, the target platform, and how SQLite was compiled.
9036** ^The third argument to xCreate(), bPurgeable, is true if the cache being
9037** created will be used to cache database pages of a file stored on disk, or
9038** false if it is used for an in-memory database. The cache implementation
9039** does not have to do anything special based with the value of bPurgeable;
9040** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
9041** never invoke xUnpin() except to deliberately delete a page.
9042** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
9043** false will always have the "discard" flag set to true.
9044** ^Hence, a cache created with bPurgeable false will
9045** never contain any unpinned pages.
9046**
9047** [[the xCachesize() page cache method]]
9048** ^(The xCachesize() method may be called at any time by SQLite to set the
9049** suggested maximum cache-size (number of pages stored by) the cache
9050** instance passed as the first argument. This is the value configured using
9051** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
9052** parameter, the implementation is not required to do anything with this
9053** value; it is advisory only.
9054**
9055** [[the xPagecount() page cache methods]]
9056** The xPagecount() method must return the number of pages currently
9057** stored in the cache, both pinned and unpinned.
9058**
9059** [[the xFetch() page cache methods]]
9060** The xFetch() method locates a page in the cache and returns a pointer to
9061** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
9062** The pBuf element of the returned sqlite3_pcache_page object will be a
9063** pointer to a buffer of szPage bytes used to store the content of a
9064** single database page. The pExtra element of sqlite3_pcache_page will be
9065** a pointer to the szExtra bytes of extra storage that SQLite has requested
9066** for each entry in the page cache.
9067**
9068** The page to be fetched is determined by the key. ^The minimum key value
9069** is 1. After it has been retrieved using xFetch, the page is considered
9070** to be "pinned".
9071**
9072** If the requested page is already in the page cache, then the page cache
9073** implementation must return a pointer to the page buffer with its content
9074** intact. If the requested page is not already in the cache, then the
9075** cache implementation should use the value of the createFlag
9076** parameter to help it determined what action to take:
9077**
9078** <table border=1 width=85% align=center>
9079** <tr><th> createFlag <th> Behavior when page is not already in cache
9080** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
9081** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
9082** Otherwise return NULL.
9083** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
9084** NULL if allocating a new page is effectively impossible.
9085** </table>
9086**
9087** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
9088** will only use a createFlag of 2 after a prior call with a createFlag of 1
9089** failed.)^ In between the to xFetch() calls, SQLite may
9090** attempt to unpin one or more cache pages by spilling the content of
9091** pinned pages to disk and synching the operating system disk cache.
9092**
9093** [[the xUnpin() page cache method]]
9094** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
9095** as its second argument. If the third parameter, discard, is non-zero,
9096** then the page must be evicted from the cache.
9097** ^If the discard parameter is
9098** zero, then the page may be discarded or retained at the discretion of
9099** page cache implementation. ^The page cache implementation
9100** may choose to evict unpinned pages at any time.
9101**
9102** The cache must not perform any reference counting. A single
9103** call to xUnpin() unpins the page regardless of the number of prior calls
9104** to xFetch().
9105**
9106** [[the xRekey() page cache methods]]
9107** The xRekey() method is used to change the key value associated with the
9108** page passed as the second argument. If the cache
9109** previously contains an entry associated with newKey, it must be
9110** discarded. ^Any prior cache entry associated with newKey is guaranteed not
9111** to be pinned.
9112**
9113** When SQLite calls the xTruncate() method, the cache must discard all
9114** existing cache entries with page numbers (keys) greater than or equal
9115** to the value of the iLimit parameter passed to xTruncate(). If any
9116** of these pages are pinned, they are implicitly unpinned, meaning that
9117** they can be safely discarded.
9118**
9119** [[the xDestroy() page cache method]]
9120** ^The xDestroy() method is used to delete a cache allocated by xCreate().
9121** All resources associated with the specified cache should be freed. ^After
9122** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
9123** handle invalid, and will not use it with any other sqlite3_pcache_methods2
9124** functions.
9125**
9126** [[the xShrink() page cache method]]
9127** ^SQLite invokes the xShrink() method when it wants the page cache to
9128** free up as much of heap memory as possible. The page cache implementation
9129** is not obligated to free any memory, but well-behaved implementations should
9130** do their best.
9131*/
9132typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
9133struct sqlite3_pcache_methods2 {
9134 int iVersion;
9135 void *pArg;
9136 int (*xInit)(void*);
9137 void (*xShutdown)(void*);
9138 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
9139 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9140 int (*xPagecount)(sqlite3_pcache*);
9141 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9142 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
9143 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
9144 unsigned oldKey, unsigned newKey);
9145 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9146 void (*xDestroy)(sqlite3_pcache*);
9147 void (*xShrink)(sqlite3_pcache*);
9148};
9149
9150/*
9151** This is the obsolete pcache_methods object that has now been replaced
9152** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
9153** retained in the header file for backwards compatibility only.
9154*/
9155typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
9156struct sqlite3_pcache_methods {
9157 void *pArg;
9158 int (*xInit)(void*);
9159 void (*xShutdown)(void*);
9160 sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
9161 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
9162 int (*xPagecount)(sqlite3_pcache*);
9163 void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
9164 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
9165 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
9166 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
9167 void (*xDestroy)(sqlite3_pcache*);
9168};
9169
9170
9171/*
9172** CAPI3REF: Online Backup Object
9173**
9174** The sqlite3_backup object records state information about an ongoing
9175** online backup operation. ^The sqlite3_backup object is created by
9176** a call to [sqlite3_backup_init()] and is destroyed by a call to
9177** [sqlite3_backup_finish()].
9178**
9179** See Also: [Using the SQLite Online Backup API]
9180*/
9181typedef struct sqlite3_backup sqlite3_backup;
9182
9183/*
9184** CAPI3REF: Online Backup API.
9185**
9186** The backup API copies the content of one database into another.
9187** It is useful either for creating backups of databases or
9188** for copying in-memory databases to or from persistent files.
9189**
9190** See Also: [Using the SQLite Online Backup API]
9191**
9192** ^SQLite holds a write transaction open on the destination database file
9193** for the duration of the backup operation.
9194** ^The source database is read-locked only while it is being read;
9195** it is not locked continuously for the entire backup operation.
9196** ^Thus, the backup may be performed on a live source database without
9197** preventing other database connections from
9198** reading or writing to the source database while the backup is underway.
9199**
9200** ^(To perform a backup operation:
9201** <ol>
9202** <li><b>sqlite3_backup_init()</b> is called once to initialize the
9203** backup,
9204** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
9205** the data between the two databases, and finally
9206** <li><b>sqlite3_backup_finish()</b> is called to release all resources
9207** associated with the backup operation.
9208** </ol>)^
9209** There should be exactly one call to sqlite3_backup_finish() for each
9210** successful call to sqlite3_backup_init().
9211**
9212** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
9213**
9214** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
9215** [database connection] associated with the destination database
9216** and the database name, respectively.
9217** ^The database name is "main" for the main database, "temp" for the
9218** temporary database, or the name specified after the AS keyword in
9219** an [ATTACH] statement for an attached database.
9220** ^The S and M arguments passed to
9221** sqlite3_backup_init(D,N,S,M) identify the [database connection]
9222** and database name of the source database, respectively.
9223** ^The source and destination [database connections] (parameters S and D)
9224** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
9225** an error.
9226**
9227** ^A call to sqlite3_backup_init() will fail, returning NULL, if
9228** there is already a read or read-write transaction open on the
9229** destination database.
9230**
9231** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
9232** returned and an error code and error message are stored in the
9233** destination [database connection] D.
9234** ^The error code and message for the failed call to sqlite3_backup_init()
9235** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
9236** [sqlite3_errmsg16()] functions.
9237** ^A successful call to sqlite3_backup_init() returns a pointer to an
9238** [sqlite3_backup] object.
9239** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
9240** sqlite3_backup_finish() functions to perform the specified backup
9241** operation.
9242**
9243** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
9244**
9245** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
9246** the source and destination databases specified by [sqlite3_backup] object B.
9247** ^If N is negative, all remaining source pages are copied.
9248** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
9249** are still more pages to be copied, then the function returns [SQLITE_OK].
9250** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
9251** from source to destination, then it returns [SQLITE_DONE].
9252** ^If an error occurs while running sqlite3_backup_step(B,N),
9253** then an [error code] is returned. ^As well as [SQLITE_OK] and
9254** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
9255** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
9256** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
9257**
9258** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
9259** <ol>
9260** <li> the destination database was opened read-only, or
9261** <li> the destination database is using write-ahead-log journaling
9262** and the destination and source page sizes differ, or
9263** <li> the destination database is an in-memory database and the
9264** destination and source page sizes differ.
9265** </ol>)^
9266**
9267** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9268** the [sqlite3_busy_handler | busy-handler function]
9269** is invoked (if one is specified). ^If the
9270** busy-handler returns non-zero before the lock is available, then
9271** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
9272** sqlite3_backup_step() can be retried later. ^If the source
9273** [database connection]
9274** is being used to write to the source database when sqlite3_backup_step()
9275** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
9276** case the call to sqlite3_backup_step() can be retried later on. ^(If
9277** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
9278** [SQLITE_READONLY] is returned, then
9279** there is no point in retrying the call to sqlite3_backup_step(). These
9280** errors are considered fatal.)^ The application must accept
9281** that the backup operation has failed and pass the backup operation handle
9282** to the sqlite3_backup_finish() to release associated resources.
9283**
9284** ^The first call to sqlite3_backup_step() obtains an exclusive lock
9285** on the destination file. ^The exclusive lock is not released until either
9286** sqlite3_backup_finish() is called or the backup operation is complete
9287** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
9288** sqlite3_backup_step() obtains a [shared lock] on the source database that
9289** lasts for the duration of the sqlite3_backup_step() call.
9290** ^Because the source database is not locked between calls to
9291** sqlite3_backup_step(), the source database may be modified mid-way
9292** through the backup process. ^If the source database is modified by an
9293** external process or via a database connection other than the one being
9294** used by the backup operation, then the backup will be automatically
9295** restarted by the next call to sqlite3_backup_step(). ^If the source
9296** database is modified by the using the same database connection as is used
9297** by the backup operation, then the backup database is automatically
9298** updated at the same time.
9299**
9300** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
9301**
9302** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
9303** application wishes to abandon the backup operation, the application
9304** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
9305** ^The sqlite3_backup_finish() interfaces releases all
9306** resources associated with the [sqlite3_backup] object.
9307** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
9308** active write-transaction on the destination database is rolled back.
9309** The [sqlite3_backup] object is invalid
9310** and may not be used following a call to sqlite3_backup_finish().
9311**
9312** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
9313** sqlite3_backup_step() errors occurred, regardless or whether or not
9314** sqlite3_backup_step() completed.
9315** ^If an out-of-memory condition or IO error occurred during any prior
9316** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
9317** sqlite3_backup_finish() returns the corresponding [error code].
9318**
9319** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
9320** is not a permanent error and does not affect the return value of
9321** sqlite3_backup_finish().
9322**
9323** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
9324** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
9325**
9326** ^The sqlite3_backup_remaining() routine returns the number of pages still
9327** to be backed up at the conclusion of the most recent sqlite3_backup_step().
9328** ^The sqlite3_backup_pagecount() routine returns the total number of pages
9329** in the source database at the conclusion of the most recent
9330** sqlite3_backup_step().
9331** ^(The values returned by these functions are only updated by
9332** sqlite3_backup_step(). If the source database is modified in a way that
9333** changes the size of the source database or the number of pages remaining,
9334** those changes are not reflected in the output of sqlite3_backup_pagecount()
9335** and sqlite3_backup_remaining() until after the next
9336** sqlite3_backup_step().)^
9337**
9338** <b>Concurrent Usage of Database Handles</b>
9339**
9340** ^The source [database connection] may be used by the application for other
9341** purposes while a backup operation is underway or being initialized.
9342** ^If SQLite is compiled and configured to support threadsafe database
9343** connections, then the source database connection may be used concurrently
9344** from within other threads.
9345**
9346** However, the application must guarantee that the destination
9347** [database connection] is not passed to any other API (by any thread) after
9348** sqlite3_backup_init() is called and before the corresponding call to
9349** sqlite3_backup_finish(). SQLite does not currently check to see
9350** if the application incorrectly accesses the destination [database connection]
9351** and so no error code is reported, but the operations may malfunction
9352** nevertheless. Use of the destination database connection while a
9353** backup is in progress might also also cause a mutex deadlock.
9354**
9355** If running in [shared cache mode], the application must
9356** guarantee that the shared cache used by the destination database
9357** is not accessed while the backup is running. In practice this means
9358** that the application must guarantee that the disk file being
9359** backed up to is not accessed by any connection within the process,
9360** not just the specific connection that was passed to sqlite3_backup_init().
9361**
9362** The [sqlite3_backup] object itself is partially threadsafe. Multiple
9363** threads may safely make multiple concurrent calls to sqlite3_backup_step().
9364** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
9365** APIs are not strictly speaking threadsafe. If they are invoked at the
9366** same time as another thread is invoking sqlite3_backup_step() it is
9367** possible that they return invalid values.
9368*/
9369SQLITE_API sqlite3_backup *sqlite3_backup_init(
9370 sqlite3 *pDest, /* Destination database handle */
9371 const char *zDestName, /* Destination database name */
9372 sqlite3 *pSource, /* Source database handle */
9373 const char *zSourceName /* Source database name */
9374);
9375SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
9376SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
9377SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
9378SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
9379
9380/*
9381** CAPI3REF: Unlock Notification
9382** METHOD: sqlite3
9383**
9384** ^When running in shared-cache mode, a database operation may fail with
9385** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9386** individual tables within the shared-cache cannot be obtained. See
9387** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9388** ^This API may be used to register a callback that SQLite will invoke
9389** when the connection currently holding the required lock relinquishes it.
9390** ^This API is only available if the library was compiled with the
9391** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9392**
9393** See Also: [Using the SQLite Unlock Notification Feature].
9394**
9395** ^Shared-cache locks are released when a database connection concludes
9396** its current transaction, either by committing it or rolling it back.
9397**
9398** ^When a connection (known as the blocked connection) fails to obtain a
9399** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9400** identity of the database connection (the blocking connection) that
9401** has locked the required resource is stored internally. ^After an
9402** application receives an SQLITE_LOCKED error, it may call the
9403** sqlite3_unlock_notify() method with the blocked connection handle as
9404** the first argument to register for a callback that will be invoked
9405** when the blocking connections current transaction is concluded. ^The
9406** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
9407** call that concludes the blocking connections transaction.
9408**
9409** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9410** there is a chance that the blocking connection will have already
9411** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
9412** If this happens, then the specified callback is invoked immediately,
9413** from within the call to sqlite3_unlock_notify().)^
9414**
9415** ^If the blocked connection is attempting to obtain a write-lock on a
9416** shared-cache table, and more than one other connection currently holds
9417** a read-lock on the same table, then SQLite arbitrarily selects one of
9418** the other connections to use as the blocking connection.
9419**
9420** ^(There may be at most one unlock-notify callback registered by a
9421** blocked connection. If sqlite3_unlock_notify() is called when the
9422** blocked connection already has a registered unlock-notify callback,
9423** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
9424** called with a NULL pointer as its second argument, then any existing
9425** unlock-notify callback is canceled. ^The blocked connections
9426** unlock-notify callback may also be canceled by closing the blocked
9427** connection using [sqlite3_close()].
9428**
9429** The unlock-notify callback is not reentrant. If an application invokes
9430** any sqlite3_xxx API functions from within an unlock-notify callback, a
9431** crash or deadlock may be the result.
9432**
9433** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
9434** returns SQLITE_OK.
9435**
9436** <b>Callback Invocation Details</b>
9437**
9438** When an unlock-notify callback is registered, the application provides a
9439** single void* pointer that is passed to the callback when it is invoked.
9440** However, the signature of the callback function allows SQLite to pass
9441** it an array of void* context pointers. The first argument passed to
9442** an unlock-notify callback is a pointer to an array of void* pointers,
9443** and the second is the number of entries in the array.
9444**
9445** When a blocking connections transaction is concluded, there may be
9446** more than one blocked connection that has registered for an unlock-notify
9447** callback. ^If two or more such blocked connections have specified the
9448** same callback function, then instead of invoking the callback function
9449** multiple times, it is invoked once with the set of void* context pointers
9450** specified by the blocked connections bundled together into an array.
9451** This gives the application an opportunity to prioritize any actions
9452** related to the set of unblocked database connections.
9453**
9454** <b>Deadlock Detection</b>
9455**
9456** Assuming that after registering for an unlock-notify callback a
9457** database waits for the callback to be issued before taking any further
9458** action (a reasonable assumption), then using this API may cause the
9459** application to deadlock. For example, if connection X is waiting for
9460** connection Y's transaction to be concluded, and similarly connection
9461** Y is waiting on connection X's transaction, then neither connection
9462** will proceed and the system may remain deadlocked indefinitely.
9463**
9464** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
9465** detection. ^If a given call to sqlite3_unlock_notify() would put the
9466** system in a deadlocked state, then SQLITE_LOCKED is returned and no
9467** unlock-notify callback is registered. The system is said to be in
9468** a deadlocked state if connection A has registered for an unlock-notify
9469** callback on the conclusion of connection B's transaction, and connection
9470** B has itself registered for an unlock-notify callback when connection
9471** A's transaction is concluded. ^Indirect deadlock is also detected, so
9472** the system is also considered to be deadlocked if connection B has
9473** registered for an unlock-notify callback on the conclusion of connection
9474** C's transaction, where connection C is waiting on connection A. ^Any
9475** number of levels of indirection are allowed.
9476**
9477** <b>The "DROP TABLE" Exception</b>
9478**
9479** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
9480** always appropriate to call sqlite3_unlock_notify(). There is however,
9481** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
9482** SQLite checks if there are any currently executing SELECT statements
9483** that belong to the same connection. If there are, SQLITE_LOCKED is
9484** returned. In this case there is no "blocking connection", so invoking
9485** sqlite3_unlock_notify() results in the unlock-notify callback being
9486** invoked immediately. If the application then re-attempts the "DROP TABLE"
9487** or "DROP INDEX" query, an infinite loop might be the result.
9488**
9489** One way around this problem is to check the extended error code returned
9490** by an sqlite3_step() call. ^(If there is a blocking connection, then the
9491** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
9492** the special "DROP TABLE/INDEX" case, the extended error code is just
9493** SQLITE_LOCKED.)^
9494*/
9495SQLITE_API int sqlite3_unlock_notify(
9496 sqlite3 *pBlocked, /* Waiting connection */
9497 void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
9498 void *pNotifyArg /* Argument to pass to xNotify */
9499);
9500
9501
9502/*
9503** CAPI3REF: String Comparison
9504**
9505** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
9506** and extensions to compare the contents of two buffers containing UTF-8
9507** strings in a case-independent fashion, using the same definition of "case
9508** independence" that SQLite uses internally when comparing identifiers.
9509*/
9510SQLITE_API int sqlite3_stricmp(const char *, const char *);
9511SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
9512
9513/*
9514** CAPI3REF: String Globbing
9515*
9516** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
9517** string X matches the [GLOB] pattern P.
9518** ^The definition of [GLOB] pattern matching used in
9519** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
9520** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function
9521** is case sensitive.
9522**
9523** Note that this routine returns zero on a match and non-zero if the strings
9524** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9525**
9526** See also: [sqlite3_strlike()].
9527*/
9528SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
9529
9530/*
9531** CAPI3REF: String LIKE Matching
9532*
9533** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
9534** string X matches the [LIKE] pattern P with escape character E.
9535** ^The definition of [LIKE] pattern matching used in
9536** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
9537** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without
9538** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
9539** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
9540** insensitive - equivalent upper and lower case ASCII characters match
9541** one another.
9542**
9543** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
9544** only ASCII characters are case folded.
9545**
9546** Note that this routine returns zero on a match and non-zero if the strings
9547** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
9548**
9549** See also: [sqlite3_strglob()].
9550*/
9551SQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
9552
9553/*
9554** CAPI3REF: Error Logging Interface
9555**
9556** ^The [sqlite3_log()] interface writes a message into the [error log]
9557** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
9558** ^If logging is enabled, the zFormat string and subsequent arguments are
9559** used with [sqlite3_snprintf()] to generate the final output string.
9560**
9561** The sqlite3_log() interface is intended for use by extensions such as
9562** virtual tables, collating functions, and SQL functions. While there is
9563** nothing to prevent an application from calling sqlite3_log(), doing so
9564** is considered bad form.
9565**
9566** The zFormat string must not be NULL.
9567**
9568** To avoid deadlocks and other threading problems, the sqlite3_log() routine
9569** will not use dynamically allocated memory. The log message is stored in
9570** a fixed-length buffer on the stack. If the log message is longer than
9571** a few hundred characters, it will be truncated to the length of the
9572** buffer.
9573*/
9574SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
9575
9576/*
9577** CAPI3REF: Write-Ahead Log Commit Hook
9578** METHOD: sqlite3
9579**
9580** ^The [sqlite3_wal_hook()] function is used to register a callback that
9581** is invoked each time data is committed to a database in wal mode.
9582**
9583** ^(The callback is invoked by SQLite after the commit has taken place and
9584** the associated write-lock on the database released)^, so the implementation
9585** may read, write or [checkpoint] the database as required.
9586**
9587** ^The first parameter passed to the callback function when it is invoked
9588** is a copy of the third parameter passed to sqlite3_wal_hook() when
9589** registering the callback. ^The second is a copy of the database handle.
9590** ^The third parameter is the name of the database that was written to -
9591** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9592** is the number of pages currently in the write-ahead log file,
9593** including those that were just committed.
9594**
9595** The callback function should normally return [SQLITE_OK]. ^If an error
9596** code is returned, that error will propagate back up through the
9597** SQLite code base to cause the statement that provoked the callback
9598** to report an error, though the commit will have still occurred. If the
9599** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
9600** that does not correspond to any valid SQLite error code, the results
9601** are undefined.
9602**
9603** A single database handle may have at most a single write-ahead log callback
9604** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
9605** previously registered write-ahead log callback. ^Note that the
9606** [sqlite3_wal_autocheckpoint()] interface and the
9607** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
9608** overwrite any prior [sqlite3_wal_hook()] settings.
9609*/
9610SQLITE_API void *sqlite3_wal_hook(
9611 sqlite3*,
9612 int(*)(void *,sqlite3*,const char*,int),
9613 void*
9614);
9615
9616/*
9617** CAPI3REF: Configure an auto-checkpoint
9618** METHOD: sqlite3
9619**
9620** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
9621** [sqlite3_wal_hook()] that causes any database on [database connection] D
9622** to automatically [checkpoint]
9623** after committing a transaction if there are N or
9624** more frames in the [write-ahead log] file. ^Passing zero or
9625** a negative value as the nFrame parameter disables automatic
9626** checkpoints entirely.
9627**
9628** ^The callback registered by this function replaces any existing callback
9629** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
9630** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9631** configured by this function.
9632**
9633** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9634** from SQL.
9635**
9636** ^Checkpoints initiated by this mechanism are
9637** [sqlite3_wal_checkpoint_v2|PASSIVE].
9638**
9639** ^Every new [database connection] defaults to having the auto-checkpoint
9640** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9641** pages. The use of this interface
9642** is only necessary if the default setting is found to be suboptimal
9643** for a particular application.
9644*/
9645SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9646
9647/*
9648** CAPI3REF: Checkpoint a database
9649** METHOD: sqlite3
9650**
9651** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
9652** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
9653**
9654** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
9655** [write-ahead log] for database X on [database connection] D to be
9656** transferred into the database file and for the write-ahead log to
9657** be reset. See the [checkpointing] documentation for addition
9658** information.
9659**
9660** This interface used to be the only way to cause a checkpoint to
9661** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
9662** interface was added. This interface is retained for backwards
9663** compatibility and as a convenience for applications that need to manually
9664** start a callback but which do not need the full power (and corresponding
9665** complication) of [sqlite3_wal_checkpoint_v2()].
9666*/
9667SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
9668
9669/*
9670** CAPI3REF: Checkpoint a database
9671** METHOD: sqlite3
9672**
9673** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
9674** operation on database X of [database connection] D in mode M. Status
9675** information is written back into integers pointed to by L and C.)^
9676** ^(The M parameter must be a valid [checkpoint mode]:)^
9677**
9678** <dl>
9679** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
9680** ^Checkpoint as many frames as possible without waiting for any database
9681** readers or writers to finish, then sync the database file if all frames
9682** in the log were checkpointed. ^The [busy-handler callback]
9683** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
9684** ^On the other hand, passive mode might leave the checkpoint unfinished
9685** if there are concurrent readers or writers.
9686**
9687** <dt>SQLITE_CHECKPOINT_FULL<dd>
9688** ^This mode blocks (it invokes the
9689** [sqlite3_busy_handler|busy-handler callback]) until there is no
9690** database writer and all readers are reading from the most recent database
9691** snapshot. ^It then checkpoints all frames in the log file and syncs the
9692** database file. ^This mode blocks new database writers while it is pending,
9693** but new database readers are allowed to continue unimpeded.
9694**
9695** <dt>SQLITE_CHECKPOINT_RESTART<dd>
9696** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
9697** that after checkpointing the log file it blocks (calls the
9698** [busy-handler callback])
9699** until all readers are reading from the database file only. ^This ensures
9700** that the next writer will restart the log file from the beginning.
9701** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
9702** database writer attempts while it is pending, but does not impede readers.
9703**
9704** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
9705** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
9706** addition that it also truncates the log file to zero bytes just prior
9707** to a successful return.
9708** </dl>
9709**
9710** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
9711** the log file or to -1 if the checkpoint could not run because
9712** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
9713** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
9714** log file (including any that were already checkpointed before the function
9715** was called) or to -1 if the checkpoint could not run due to an error or
9716** because the database is not in WAL mode. ^Note that upon successful
9717** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
9718** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
9719**
9720** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
9721** any other process is running a checkpoint operation at the same time, the
9722** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
9723** busy-handler configured, it will not be invoked in this case.
9724**
9725** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
9726** exclusive "writer" lock on the database file. ^If the writer lock cannot be
9727** obtained immediately, and a busy-handler is configured, it is invoked and
9728** the writer lock retried until either the busy-handler returns 0 or the lock
9729** is successfully obtained. ^The busy-handler is also invoked while waiting for
9730** database readers as described above. ^If the busy-handler returns 0 before
9731** the writer lock is obtained or while waiting for database readers, the
9732** checkpoint operation proceeds from that point in the same way as
9733** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9734** without blocking any further. ^SQLITE_BUSY is returned in this case.
9735**
9736** ^If parameter zDb is NULL or points to a zero length string, then the
9737** specified operation is attempted on all WAL databases [attached] to
9738** [database connection] db. In this case the
9739** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
9740** an SQLITE_BUSY error is encountered when processing one or more of the
9741** attached WAL databases, the operation is still attempted on any remaining
9742** attached databases and SQLITE_BUSY is returned at the end. ^If any other
9743** error occurs while processing an attached database, processing is abandoned
9744** and the error code is returned to the caller immediately. ^If no error
9745** (SQLITE_BUSY or otherwise) is encountered while processing the attached
9746** databases, SQLITE_OK is returned.
9747**
9748** ^If database zDb is the name of an attached database that is not in WAL
9749** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9750** zDb is not NULL (or a zero length string) and is not the name of any
9751** attached database, SQLITE_ERROR is returned to the caller.
9752**
9753** ^Unless it returns SQLITE_MISUSE,
9754** the sqlite3_wal_checkpoint_v2() interface
9755** sets the error information that is queried by
9756** [sqlite3_errcode()] and [sqlite3_errmsg()].
9757**
9758** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
9759** from SQL.
9760*/
9761SQLITE_API int sqlite3_wal_checkpoint_v2(
9762 sqlite3 *db, /* Database handle */
9763 const char *zDb, /* Name of attached database (or NULL) */
9764 int eMode, /* SQLITE_CHECKPOINT_* value */
9765 int *pnLog, /* OUT: Size of WAL log in frames */
9766 int *pnCkpt /* OUT: Total number of frames checkpointed */
9767);
9768
9769/*
9770** CAPI3REF: Checkpoint Mode Values
9771** KEYWORDS: {checkpoint mode}
9772**
9773** These constants define all valid values for the "checkpoint mode" passed
9774** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
9775** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9776** meaning of each of these checkpoint modes.
9777*/
9778#define SQLITE_CHECKPOINT_PASSIVE0 0 /* Do as much as possible w/o blocking */
9779#define SQLITE_CHECKPOINT_FULL1 1 /* Wait for writers, then checkpoint */
9780#define SQLITE_CHECKPOINT_RESTART2 2 /* Like FULL but wait for for readers */
9781#define SQLITE_CHECKPOINT_TRUNCATE3 3 /* Like RESTART but also truncate WAL */
9782
9783/*
9784** CAPI3REF: Virtual Table Interface Configuration
9785**
9786** This function may be called by either the [xConnect] or [xCreate] method
9787** of a [virtual table] implementation to configure
9788** various facets of the virtual table interface.
9789**
9790** If this interface is invoked outside the context of an xConnect or
9791** xCreate virtual table method then the behavior is undefined.
9792**
9793** At present, there is only one option that may be configured using
9794** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].) Further options
9795** may be added in the future.
9796*/
9797SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
9798
9799/*
9800** CAPI3REF: Virtual Table Configuration Options
9801**
9802** These macros define the various options to the
9803** [sqlite3_vtab_config()] interface that [virtual table] implementations
9804** can use to customize and optimize their behavior.
9805**
9806** <dl>
9807** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
9808** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
9809** <dd>Calls of the form
9810** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
9811** where X is an integer. If X is zero, then the [virtual table] whose
9812** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
9813** support constraints. In this configuration (which is the default) if
9814** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
9815** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
9816** specified as part of the users SQL statement, regardless of the actual
9817** ON CONFLICT mode specified.
9818**
9819** If X is non-zero, then the virtual table implementation guarantees
9820** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
9821** any modifications to internal or persistent data structures have been made.
9822** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
9823** is able to roll back a statement or database transaction, and abandon
9824** or continue processing the current SQL statement as appropriate.
9825** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
9826** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
9827** had been ABORT.
9828**
9829** Virtual table implementations that are required to handle OR REPLACE
9830** must do so within the [xUpdate] method. If a call to the
9831** [sqlite3_vtab_on_conflict()] function indicates that the current ON
9832** CONFLICT policy is REPLACE, the virtual table implementation should
9833** silently replace the appropriate rows within the xUpdate callback and
9834** return SQLITE_OK. Or, if this is not possible, it may return
9835** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
9836** constraint handling.
9837** </dl>
9838*/
9839#define SQLITE_VTAB_CONSTRAINT_SUPPORT1 1
9840
9841/*
9842** CAPI3REF: Determine The Virtual Table Conflict Policy
9843**
9844** This function may only be called from within a call to the [xUpdate] method
9845** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
9846** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
9847** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
9848** of the SQL statement that triggered the call to the [xUpdate] method of the
9849** [virtual table].
9850*/
9851SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
9852
9853/*
9854** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
9855**
9856** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
9857** method of a [virtual table], then it returns true if and only if the
9858** column is being fetched as part of an UPDATE operation during which the
9859** column value will not change. Applications might use this to substitute
9860** a return value that is less expensive to compute and that the corresponding
9861** [xUpdate] method understands as a "no-change" value.
9862**
9863** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
9864** the column is not changed by the UPDATE statement, then the xColumn
9865** method can optionally return without setting a result, without calling
9866** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
9867** In that case, [sqlite3_value_nochange(X)] will return true for the
9868** same column in the [xUpdate] method.
9869*/
9870SQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
9871
9872/*
9873** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9874**
9875** This function may only be called from within a call to the [xBestIndex]
9876** method of a [virtual table].
9877**
9878** The first argument must be the sqlite3_index_info object that is the
9879** first parameter to the xBestIndex() method. The second argument must be
9880** an index into the aConstraint[] array belonging to the sqlite3_index_info
9881** structure passed to xBestIndex. This function returns a pointer to a buffer
9882** containing the name of the collation sequence for the corresponding
9883** constraint.
9884*/
9885SQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9886
9887/*
9888** CAPI3REF: Conflict resolution modes
9889** KEYWORDS: {conflict resolution mode}
9890**
9891** These constants are returned by [sqlite3_vtab_on_conflict()] to
9892** inform a [virtual table] implementation what the [ON CONFLICT] mode
9893** is for the SQL statement being evaluated.
9894**
9895** Note that the [SQLITE_IGNORE] constant is also used as a potential
9896** return value from the [sqlite3_set_authorizer()] callback and that
9897** [SQLITE_ABORT] is also a [result code].
9898*/
9899#define SQLITE_ROLLBACK1 1
9900/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
9901#define SQLITE_FAIL3 3
9902/* #define SQLITE_ABORT 4 // Also an error code */
9903#define SQLITE_REPLACE5 5
9904
9905/*
9906** CAPI3REF: Prepared Statement Scan Status Opcodes
9907** KEYWORDS: {scanstatus options}
9908**
9909** The following constants can be used for the T parameter to the
9910** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a
9911** different metric for sqlite3_stmt_scanstatus() to return.
9912**
9913** When the value returned to V is a string, space to hold that string is
9914** managed by the prepared statement S and will be automatically freed when
9915** S is finalized.
9916**
9917** <dl>
9918** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
9919** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be
9920** set to the total number of times that the X-th loop has run.</dd>
9921**
9922** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
9923** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set
9924** to the total number of rows examined by all iterations of the X-th loop.</dd>
9925**
9926** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
9927** <dd>^The "double" variable pointed to by the T parameter will be set to the
9928** query planner's estimate for the average number of rows output from each
9929** iteration of the X-th loop. If the query planner's estimates was accurate,
9930** then this value will approximate the quotient NVISIT/NLOOP and the
9931** product of this value for all prior loops with the same SELECTID will
9932** be the NLOOP value for the current loop.
9933**
9934** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
9935** <dd>^The "const char *" variable pointed to by the T parameter will be set
9936** to a zero-terminated UTF-8 string containing the name of the index or table
9937** used for the X-th loop.
9938**
9939** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
9940** <dd>^The "const char *" variable pointed to by the T parameter will be set
9941** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
9942** description for the X-th loop.
9943**
9944** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
9945** <dd>^The "int" variable pointed to by the T parameter will be set to the
9946** "select-id" for the X-th loop. The select-id identifies which query or
9947** subquery the loop is part of. The main query has a select-id of zero.
9948** The select-id is the same value as is output in the first column
9949** of an [EXPLAIN QUERY PLAN] query.
9950** </dl>
9951*/
9952#define SQLITE_SCANSTAT_NLOOP0 0
9953#define SQLITE_SCANSTAT_NVISIT1 1
9954#define SQLITE_SCANSTAT_EST2 2
9955#define SQLITE_SCANSTAT_NAME3 3
9956#define SQLITE_SCANSTAT_EXPLAIN4 4
9957#define SQLITE_SCANSTAT_SELECTID5 5
9958
9959/*
9960** CAPI3REF: Prepared Statement Scan Status
9961** METHOD: sqlite3_stmt
9962**
9963** This interface returns information about the predicted and measured
9964** performance for pStmt. Advanced applications can use this
9965** interface to compare the predicted and the measured performance and
9966** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
9967**
9968** Since this interface is expected to be rarely used, it is only
9969** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
9970** compile-time option.
9971**
9972** The "iScanStatusOp" parameter determines which status information to return.
9973** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
9974** of this interface is undefined.
9975** ^The requested measurement is written into a variable pointed to by
9976** the "pOut" parameter.
9977** Parameter "idx" identifies the specific loop to retrieve statistics for.
9978** Loops are numbered starting from zero. ^If idx is out of range - less than
9979** zero or greater than or equal to the total number of loops used to implement
9980** the statement - a non-zero value is returned and the variable that pOut
9981** points to is unchanged.
9982**
9983** ^Statistics might not be available for all loops in all statements. ^In cases
9984** where there exist loops with no available statistics, this function behaves
9985** as if the loop did not exist - it returns non-zero and leave the variable
9986** that pOut points to unchanged.
9987**
9988** See also: [sqlite3_stmt_scanstatus_reset()]
9989*/
9990SQLITE_API int sqlite3_stmt_scanstatus(
9991 sqlite3_stmt *pStmt, /* Prepared statement for which info desired */
9992 int idx, /* Index of loop to report on */
9993 int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */
9994 void *pOut /* Result written here */
9995);
9996
9997/*
9998** CAPI3REF: Zero Scan-Status Counters
9999** METHOD: sqlite3_stmt
10000**
10001** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
10002**
10003** This API is only available if the library is built with pre-processor
10004** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
10005*/
10006SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
10007
10008/*
10009** CAPI3REF: Flush caches to disk mid-transaction
10010**
10011** ^If a write-transaction is open on [database connection] D when the
10012** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
10013** pages in the pager-cache that are not currently in use are written out
10014** to disk. A dirty page may be in use if a database cursor created by an
10015** active SQL statement is reading from it, or if it is page 1 of a database
10016** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)]
10017** interface flushes caches for all schemas - "main", "temp", and
10018** any [attached] databases.
10019**
10020** ^If this function needs to obtain extra database locks before dirty pages
10021** can be flushed to disk, it does so. ^If those locks cannot be obtained
10022** immediately and there is a busy-handler callback configured, it is invoked
10023** in the usual manner. ^If the required lock still cannot be obtained, then
10024** the database is skipped and an attempt made to flush any dirty pages
10025** belonging to the next (if any) database. ^If any databases are skipped
10026** because locks cannot be obtained, but no other error occurs, this
10027** function returns SQLITE_BUSY.
10028**
10029** ^If any other error occurs while flushing dirty pages to disk (for
10030** example an IO error or out-of-memory condition), then processing is
10031** abandoned and an SQLite [error code] is returned to the caller immediately.
10032**
10033** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
10034**
10035** ^This function does not set the database handle error code or message
10036** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
10037*/
10038SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10039
10040/*
10041** CAPI3REF: The pre-update hook.
10042**
10043** ^These interfaces are only available if SQLite is compiled using the
10044** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10045**
10046** ^The [sqlite3_preupdate_hook()] interface registers a callback function
10047** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
10048** on a database table.
10049** ^At most one preupdate hook may be registered at a time on a single
10050** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
10051** the previous setting.
10052** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
10053** with a NULL pointer as the second parameter.
10054** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
10055** the first parameter to callbacks.
10056**
10057** ^The preupdate hook only fires for changes to real database tables; the
10058** preupdate hook is not invoked for changes to [virtual tables] or to
10059** system tables like sqlite_master or sqlite_stat1.
10060**
10061** ^The second parameter to the preupdate callback is a pointer to
10062** the [database connection] that registered the preupdate hook.
10063** ^The third parameter to the preupdate callback is one of the constants
10064** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
10065** kind of update operation that is about to occur.
10066** ^(The fourth parameter to the preupdate callback is the name of the
10067** database within the database connection that is being modified. This
10068** will be "main" for the main database or "temp" for TEMP tables or
10069** the name given after the AS keyword in the [ATTACH] statement for attached
10070** databases.)^
10071** ^The fifth parameter to the preupdate callback is the name of the
10072** table that is being modified.
10073**
10074** For an UPDATE or DELETE operation on a [rowid table], the sixth
10075** parameter passed to the preupdate callback is the initial [rowid] of the
10076** row being modified or deleted. For an INSERT operation on a rowid table,
10077** or any operation on a WITHOUT ROWID table, the value of the sixth
10078** parameter is undefined. For an INSERT or UPDATE on a rowid table the
10079** seventh parameter is the final rowid value of the row being inserted
10080** or updated. The value of the seventh parameter passed to the callback
10081** function is not defined for operations on WITHOUT ROWID tables, or for
10082** INSERT operations on rowid tables.
10083**
10084** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
10085** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
10086** provide additional information about a preupdate event. These routines
10087** may only be called from within a preupdate callback. Invoking any of
10088** these routines from outside of a preupdate callback or with a
10089** [database connection] pointer that is different from the one supplied
10090** to the preupdate callback results in undefined and probably undesirable
10091** behavior.
10092**
10093** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
10094** in the row that is being inserted, updated, or deleted.
10095**
10096** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
10097** a [protected sqlite3_value] that contains the value of the Nth column of
10098** the table row before it is updated. The N parameter must be between 0
10099** and one less than the number of columns or the behavior will be
10100** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
10101** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
10102** behavior is undefined. The [sqlite3_value] that P points to
10103** will be destroyed when the preupdate callback returns.
10104**
10105** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
10106** a [protected sqlite3_value] that contains the value of the Nth column of
10107** the table row after it is updated. The N parameter must be between 0
10108** and one less than the number of columns or the behavior will be
10109** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
10110** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
10111** behavior is undefined. The [sqlite3_value] that P points to
10112** will be destroyed when the preupdate callback returns.
10113**
10114** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
10115** callback was invoked as a result of a direct insert, update, or delete
10116** operation; or 1 for inserts, updates, or deletes invoked by top-level
10117** triggers; or 2 for changes resulting from triggers called by top-level
10118** triggers; and so forth.
10119**
10120** See also: [sqlite3_update_hook()]
10121*/
10122#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
10123SQLITE_API void *sqlite3_preupdate_hook(
10124 sqlite3 *db,
10125 void(*xPreUpdate)(
10126 void *pCtx, /* Copy of third arg to preupdate_hook() */
10127 sqlite3 *db, /* Database handle */
10128 int op, /* SQLITE_UPDATE, DELETE or INSERT */
10129 char const *zDb, /* Database name */
10130 char const *zName, /* Table name */
10131 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
10132 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
10133 ),
10134 void*
10135);
10136SQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
10137SQLITE_API int sqlite3_preupdate_count(sqlite3 *);
10138SQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
10139SQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
10140#endif
10141
10142/*
10143** CAPI3REF: Low-level system error code
10144**
10145** ^Attempt to return the underlying operating system error code or error
10146** number that caused the most recent I/O error or failure to open a file.
10147** The return value is OS-dependent. For example, on unix systems, after
10148** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
10149** called to get back the underlying "errno" that caused the problem, such
10150** as ENOSPC, EAUTH, EISDIR, and so forth.
10151*/
10152SQLITE_API int sqlite3_system_errno(sqlite3*);
10153
10154/*
10155** CAPI3REF: Database Snapshot
10156** KEYWORDS: {snapshot} {sqlite3_snapshot}
10157**
10158** An instance of the snapshot object records the state of a [WAL mode]
10159** database for some specific point in history.
10160**
10161** In [WAL mode], multiple [database connections] that are open on the
10162** same database file can each be reading a different historical version
10163** of the database file. When a [database connection] begins a read
10164** transaction, that connection sees an unchanging copy of the database
10165** as it existed for the point in time when the transaction first started.
10166** Subsequent changes to the database from other connections are not seen
10167** by the reader until a new read transaction is started.
10168**
10169** The sqlite3_snapshot object records state information about an historical
10170** version of the database file so that it is possible to later open a new read
10171** transaction that sees that historical version of the database rather than
10172** the most recent version.
10173*/
10174typedef struct sqlite3_snapshot {
10175 unsigned char hidden[48];
10176} sqlite3_snapshot;
10177
10178/*
10179** CAPI3REF: Record A Database Snapshot
10180** CONSTRUCTOR: sqlite3_snapshot
10181**
10182** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
10183** new [sqlite3_snapshot] object that records the current state of
10184** schema S in database connection D. ^On success, the
10185** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
10186** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
10187** If there is not already a read-transaction open on schema S when
10188** this function is called, one is opened automatically.
10189**
10190** The following must be true for this function to succeed. If any of
10191** the following statements are false when sqlite3_snapshot_get() is
10192** called, SQLITE_ERROR is returned. The final value of *P is undefined
10193** in this case.
10194**
10195** <ul>
10196** <li> The database handle must not be in [autocommit mode].
10197**
10198** <li> Schema S of [database connection] D must be a [WAL mode] database.
10199**
10200** <li> There must not be a write transaction open on schema S of database
10201** connection D.
10202**
10203** <li> One or more transactions must have been written to the current wal
10204** file since it was created on disk (by any connection). This means
10205** that a snapshot cannot be taken on a wal mode database with no wal
10206** file immediately after it is first opened. At least one transaction
10207** must be written to it first.
10208** </ul>
10209**
10210** This function may also return SQLITE_NOMEM. If it is called with the
10211** database handle in autocommit mode but fails for some other reason,
10212** whether or not a read transaction is opened on schema S is undefined.
10213**
10214** The [sqlite3_snapshot] object returned from a successful call to
10215** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
10216** to avoid a memory leak.
10217**
10218** The [sqlite3_snapshot_get()] interface is only available when the
10219** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10220*/
10221SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
10222 sqlite3 *db,
10223 const char *zSchema,
10224 sqlite3_snapshot **ppSnapshot
10225);
10226
10227/*
10228** CAPI3REF: Start a read transaction on an historical snapshot
10229** METHOD: sqlite3_snapshot
10230**
10231** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
10232** transaction or upgrades an existing one for schema S of
10233** [database connection] D such that the read transaction refers to
10234** historical [snapshot] P, rather than the most recent change to the
10235** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
10236** on success or an appropriate [error code] if it fails.
10237**
10238** ^In order to succeed, the database connection must not be in
10239** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
10240** is already a read transaction open on schema S, then the database handle
10241** must have no active statements (SELECT statements that have been passed
10242** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
10243** SQLITE_ERROR is returned if either of these conditions is violated, or
10244** if schema S does not exist, or if the snapshot object is invalid.
10245**
10246** ^A call to sqlite3_snapshot_open() will fail to open if the specified
10247** snapshot has been overwritten by a [checkpoint]. In this case
10248** SQLITE_ERROR_SNAPSHOT is returned.
10249**
10250** If there is already a read transaction open when this function is
10251** invoked, then the same read transaction remains open (on the same
10252** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
10253** is returned. If another error code - for example SQLITE_PROTOCOL or an
10254** SQLITE_IOERR error code - is returned, then the final state of the
10255** read transaction is undefined. If SQLITE_OK is returned, then the
10256** read transaction is now open on database snapshot P.
10257**
10258** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
10259** database connection D does not know that the database file for
10260** schema S is in [WAL mode]. A database connection might not know
10261** that the database file is in [WAL mode] if there has been no prior
10262** I/O on that database connection, or if the database entered [WAL mode]
10263** after the most recent I/O on the database connection.)^
10264** (Hint: Run "[PRAGMA application_id]" against a newly opened
10265** database connection in order to make it ready to use snapshots.)
10266**
10267** The [sqlite3_snapshot_open()] interface is only available when the
10268** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10269*/
10270SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
10271 sqlite3 *db,
10272 const char *zSchema,
10273 sqlite3_snapshot *pSnapshot
10274);
10275
10276/*
10277** CAPI3REF: Destroy a snapshot
10278** DESTRUCTOR: sqlite3_snapshot
10279**
10280** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
10281** The application must eventually free every [sqlite3_snapshot] object
10282** using this routine to avoid a memory leak.
10283**
10284** The [sqlite3_snapshot_free()] interface is only available when the
10285** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10286*/
10287SQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
10288
10289/*
10290** CAPI3REF: Compare the ages of two snapshot handles.
10291** METHOD: sqlite3_snapshot
10292**
10293** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
10294** of two valid snapshot handles.
10295**
10296** If the two snapshot handles are not associated with the same database
10297** file, the result of the comparison is undefined.
10298**
10299** Additionally, the result of the comparison is only valid if both of the
10300** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
10301** last time the wal file was deleted. The wal file is deleted when the
10302** database is changed back to rollback mode or when the number of database
10303** clients drops to zero. If either snapshot handle was obtained before the
10304** wal file was last deleted, the value returned by this function
10305** is undefined.
10306**
10307** Otherwise, this API returns a negative value if P1 refers to an older
10308** snapshot than P2, zero if the two handles refer to the same database
10309** snapshot, and a positive value if P1 is a newer snapshot than P2.
10310**
10311** This interface is only available if SQLite is compiled with the
10312** [SQLITE_ENABLE_SNAPSHOT] option.
10313*/
10314SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
10315 sqlite3_snapshot *p1,
10316 sqlite3_snapshot *p2
10317);
10318
10319/*
10320** CAPI3REF: Recover snapshots from a wal file
10321** METHOD: sqlite3_snapshot
10322**
10323** If a [WAL file] remains on disk after all database connections close
10324** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
10325** or because the last process to have the database opened exited without
10326** calling [sqlite3_close()]) and a new connection is subsequently opened
10327** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
10328** will only be able to open the last transaction added to the WAL file
10329** even though the WAL file contains other valid transactions.
10330**
10331** This function attempts to scan the WAL file associated with database zDb
10332** of database handle db and make all valid snapshots available to
10333** sqlite3_snapshot_open(). It is an error if there is already a read
10334** transaction open on the database, or if the database is not a WAL mode
10335** database.
10336**
10337** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
10338**
10339** This interface is only available if SQLite is compiled with the
10340** [SQLITE_ENABLE_SNAPSHOT] option.
10341*/
10342SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
10343
10344/*
10345** CAPI3REF: Serialize a database
10346**
10347** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
10348** that is a serialization of the S database on [database connection] D.
10349** If P is not a NULL pointer, then the size of the database in bytes
10350** is written into *P.
10351**
10352** For an ordinary on-disk database file, the serialization is just a
10353** copy of the disk file. For an in-memory database or a "TEMP" database,
10354** the serialization is the same sequence of bytes which would be written
10355** to disk if that database where backed up to disk.
10356**
10357** The usual case is that sqlite3_serialize() copies the serialization of
10358** the database into memory obtained from [sqlite3_malloc64()] and returns
10359** a pointer to that memory. The caller is responsible for freeing the
10360** returned value to avoid a memory leak. However, if the F argument
10361** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
10362** are made, and the sqlite3_serialize() function will return a pointer
10363** to the contiguous memory representation of the database that SQLite
10364** is currently using for that database, or NULL if the no such contiguous
10365** memory representation of the database exists. A contiguous memory
10366** representation of the database will usually only exist if there has
10367** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
10368** values of D and S.
10369** The size of the database is written into *P even if the
10370** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
10371** of the database exists.
10372**
10373** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
10374** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
10375** allocation error occurs.
10376**
10377** This interface is only available if SQLite is compiled with the
10378** [SQLITE_ENABLE_DESERIALIZE] option.
10379*/
10380SQLITE_API unsigned char *sqlite3_serialize(
10381 sqlite3 *db, /* The database connection */
10382 const char *zSchema, /* Which DB to serialize. ex: "main", "temp", ... */
10383 sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
10384 unsigned int mFlags /* Zero or more SQLITE_SERIALIZE_* flags */
10385);
10386
10387/*
10388** CAPI3REF: Flags for sqlite3_serialize
10389**
10390** Zero or more of the following constants can be OR-ed together for
10391** the F argument to [sqlite3_serialize(D,S,P,F)].
10392**
10393** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
10394** a pointer to contiguous in-memory database that it is currently using,
10395** without making a copy of the database. If SQLite is not currently using
10396** a contiguous in-memory database, then this option causes
10397** [sqlite3_serialize()] to return a NULL pointer. SQLite will only be
10398** using a contiguous in-memory database if it has been initialized by a
10399** prior call to [sqlite3_deserialize()].
10400*/
10401#define SQLITE_SERIALIZE_NOCOPY0x001 0x001 /* Do no memory allocations */
10402
10403/*
10404** CAPI3REF: Deserialize a database
10405**
10406** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
10407** [database connection] D to disconnect from database S and then
10408** reopen S as an in-memory database based on the serialization contained
10409** in P. The serialized database P is N bytes in size. M is the size of
10410** the buffer P, which might be larger than N. If M is larger than N, and
10411** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
10412** permitted to add content to the in-memory database as long as the total
10413** size does not exceed M bytes.
10414**
10415** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
10416** invoke sqlite3_free() on the serialization buffer when the database
10417** connection closes. If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
10418** SQLite will try to increase the buffer size using sqlite3_realloc64()
10419** if writes on the database cause it to grow larger than M bytes.
10420**
10421** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
10422** database is currently in a read transaction or is involved in a backup
10423** operation.
10424**
10425** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
10426** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
10427** [sqlite3_free()] is invoked on argument P prior to returning.
10428**
10429** This interface is only available if SQLite is compiled with the
10430** [SQLITE_ENABLE_DESERIALIZE] option.
10431*/
10432SQLITE_API int sqlite3_deserialize(
10433 sqlite3 *db, /* The database connection */
10434 const char *zSchema, /* Which DB to reopen with the deserialization */
10435 unsigned char *pData, /* The serialized database content */
10436 sqlite3_int64 szDb, /* Number bytes in the deserialization */
10437 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
10438 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
10439);
10440
10441/*
10442** CAPI3REF: Flags for sqlite3_deserialize()
10443**
10444** The following are allowed values for 6th argument (the F argument) to
10445** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
10446**
10447** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
10448** in the P argument is held in memory obtained from [sqlite3_malloc64()]
10449** and that SQLite should take ownership of this memory and automatically
10450** free it when it has finished using it. Without this flag, the caller
10451** is responsible for freeing any dynamically allocated memory.
10452**
10453** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
10454** grow the size of the database using calls to [sqlite3_realloc64()]. This
10455** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
10456** Without this flag, the deserialized database cannot increase in size beyond
10457** the number of bytes specified by the M parameter.
10458**
10459** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
10460** should be treated as read-only.
10461*/
10462#define SQLITE_DESERIALIZE_FREEONCLOSE1 1 /* Call sqlite3_free() on close */
10463#define SQLITE_DESERIALIZE_RESIZEABLE2 2 /* Resize using sqlite3_realloc64() */
10464#define SQLITE_DESERIALIZE_READONLY4 4 /* Database is read-only */
10465
10466/*
10467** Undo the hack that converts floating point types to integer for
10468** builds on processors without floating point support.
10469*/
10470#ifdef SQLITE_OMIT_FLOATING_POINT
10471# undef double
10472#endif
10473
10474#if 0
10475} /* End of the 'extern "C"' block */
10476#endif
10477#endif /* SQLITE3_H */
10478
10479/******** Begin file sqlite3rtree.h *********/
10480/*
10481** 2010 August 30
10482**
10483** The author disclaims copyright to this source code. In place of
10484** a legal notice, here is a blessing:
10485**
10486** May you do good and not evil.
10487** May you find forgiveness for yourself and forgive others.
10488** May you share freely, never taking more than you give.
10489**
10490*************************************************************************
10491*/
10492
10493#ifndef _SQLITE3RTREE_H_
10494#define _SQLITE3RTREE_H_
10495
10496
10497#if 0
10498extern "C" {
10499#endif
10500
10501typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
10502typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
10503
10504/* The double-precision datatype used by RTree depends on the
10505** SQLITE_RTREE_INT_ONLY compile-time option.
10506*/
10507#ifdef SQLITE_RTREE_INT_ONLY
10508 typedef sqlite3_int64 sqlite3_rtree_dbl;
10509#else
10510 typedef double sqlite3_rtree_dbl;
10511#endif
10512
10513/*
10514** Register a geometry callback named zGeom that can be used as part of an
10515** R-Tree geometry query as follows:
10516**
10517** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
10518*/
10519SQLITE_API int sqlite3_rtree_geometry_callback(
10520 sqlite3 *db,
10521 const char *zGeom,
10522 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
10523 void *pContext
10524);
10525
10526
10527/*
10528** A pointer to a structure of the following type is passed as the first
10529** argument to callbacks registered using rtree_geometry_callback().
10530*/
10531struct sqlite3_rtree_geometry {
10532 void *pContext; /* Copy of pContext passed to s_r_g_c() */
10533 int nParam; /* Size of array aParam[] */
10534 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
10535 void *pUser; /* Callback implementation user data */
10536 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
10537};
10538
10539/*
10540** Register a 2nd-generation geometry callback named zScore that can be
10541** used as part of an R-Tree geometry query as follows:
10542**
10543** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
10544*/
10545SQLITE_API int sqlite3_rtree_query_callback(
10546 sqlite3 *db,
10547 const char *zQueryFunc,
10548 int (*xQueryFunc)(sqlite3_rtree_query_info*),
10549 void *pContext,
10550 void (*xDestructor)(void*)
10551);
10552
10553
10554/*
10555** A pointer to a structure of the following type is passed as the
10556** argument to scored geometry callback registered using
10557** sqlite3_rtree_query_callback().
10558**
10559** Note that the first 5 fields of this structure are identical to
10560** sqlite3_rtree_geometry. This structure is a subclass of
10561** sqlite3_rtree_geometry.
10562*/
10563struct sqlite3_rtree_query_info {
10564 void *pContext; /* pContext from when function registered */
10565 int nParam; /* Number of function parameters */
10566 sqlite3_rtree_dbl *aParam; /* value of function parameters */
10567 void *pUser; /* callback can use this, if desired */
10568 void (*xDelUser)(void*); /* function to free pUser */
10569 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
10570 unsigned int *anQueue; /* Number of pending entries in the queue */
10571 int nCoord; /* Number of coordinates */
10572 int iLevel; /* Level of current node or entry */
10573 int mxLevel; /* The largest iLevel value in the tree */
10574 sqlite3_int64 iRowid; /* Rowid for current entry */
10575 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
10576 int eParentWithin; /* Visibility of parent node */
10577 int eWithin; /* OUT: Visibility */
10578 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
10579 /* The following fields are only available in 3.8.11 and later */
10580 sqlite3_value **apSqlParam; /* Original SQL values of parameters */
10581};
10582
10583/*
10584** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
10585*/
10586#define NOT_WITHIN0 0 /* Object completely outside of query region */
10587#define PARTLY_WITHIN1 1 /* Object partially overlaps query region */
10588#define FULLY_WITHIN2 2 /* Object fully contained within query region */
10589
10590
10591#if 0
10592} /* end of the 'extern "C"' block */
10593#endif
10594
10595#endif /* ifndef _SQLITE3RTREE_H_ */
10596
10597/******** End of sqlite3rtree.h *********/
10598/******** Begin file sqlite3session.h *********/
10599
10600#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
10601#define __SQLITESESSION_H_ 1
10602
10603/*
10604** Make sure we can call this stuff from C++.
10605*/
10606#if 0
10607extern "C" {
10608#endif
10609
10610
10611/*
10612** CAPI3REF: Session Object Handle
10613**
10614** An instance of this object is a [session] that can be used to
10615** record changes to a database.
10616*/
10617typedef struct sqlite3_session sqlite3_session;
10618
10619/*
10620** CAPI3REF: Changeset Iterator Handle
10621**
10622** An instance of this object acts as a cursor for iterating
10623** over the elements of a [changeset] or [patchset].
10624*/
10625typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
10626
10627/*
10628** CAPI3REF: Create A New Session Object
10629** CONSTRUCTOR: sqlite3_session
10630**
10631** Create a new session object attached to database handle db. If successful,
10632** a pointer to the new object is written to *ppSession and SQLITE_OK is
10633** returned. If an error occurs, *ppSession is set to NULL and an SQLite
10634** error code (e.g. SQLITE_NOMEM) is returned.
10635**
10636** It is possible to create multiple session objects attached to a single
10637** database handle.
10638**
10639** Session objects created using this function should be deleted using the
10640** [sqlite3session_delete()] function before the database handle that they
10641** are attached to is itself closed. If the database handle is closed before
10642** the session object is deleted, then the results of calling any session
10643** module function, including [sqlite3session_delete()] on the session object
10644** are undefined.
10645**
10646** Because the session module uses the [sqlite3_preupdate_hook()] API, it
10647** is not possible for an application to register a pre-update hook on a
10648** database handle that has one or more session objects attached. Nor is
10649** it possible to create a session object attached to a database handle for
10650** which a pre-update hook is already defined. The results of attempting
10651** either of these things are undefined.
10652**
10653** The session object will be used to create changesets for tables in
10654** database zDb, where zDb is either "main", or "temp", or the name of an
10655** attached database. It is not an error if database zDb is not attached
10656** to the database when the session object is created.
10657*/
10658SQLITE_API int sqlite3session_create(
10659 sqlite3 *db, /* Database handle */
10660 const char *zDb, /* Name of db (e.g. "main") */
10661 sqlite3_session **ppSession /* OUT: New session object */
10662);
10663
10664/*
10665** CAPI3REF: Delete A Session Object
10666** DESTRUCTOR: sqlite3_session
10667**
10668** Delete a session object previously allocated using
10669** [sqlite3session_create()]. Once a session object has been deleted, the
10670** results of attempting to use pSession with any other session module
10671** function are undefined.
10672**
10673** Session objects must be deleted before the database handle to which they
10674** are attached is closed. Refer to the documentation for
10675** [sqlite3session_create()] for details.
10676*/
10677SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
10678
10679
10680/*
10681** CAPI3REF: Enable Or Disable A Session Object
10682** METHOD: sqlite3_session
10683**
10684** Enable or disable the recording of changes by a session object. When
10685** enabled, a session object records changes made to the database. When
10686** disabled - it does not. A newly created session object is enabled.
10687** Refer to the documentation for [sqlite3session_changeset()] for further
10688** details regarding how enabling and disabling a session object affects
10689** the eventual changesets.
10690**
10691** Passing zero to this function disables the session. Passing a value
10692** greater than zero enables it. Passing a value less than zero is a
10693** no-op, and may be used to query the current state of the session.
10694**
10695** The return value indicates the final state of the session object: 0 if
10696** the session is disabled, or 1 if it is enabled.
10697*/
10698SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
10699
10700/*
10701** CAPI3REF: Set Or Clear the Indirect Change Flag
10702** METHOD: sqlite3_session
10703**
10704** Each change recorded by a session object is marked as either direct or
10705** indirect. A change is marked as indirect if either:
10706**
10707** <ul>
10708** <li> The session object "indirect" flag is set when the change is
10709** made, or
10710** <li> The change is made by an SQL trigger or foreign key action
10711** instead of directly as a result of a users SQL statement.
10712** </ul>
10713**
10714** If a single row is affected by more than one operation within a session,
10715** then the change is considered indirect if all operations meet the criteria
10716** for an indirect change above, or direct otherwise.
10717**
10718** This function is used to set, clear or query the session object indirect
10719** flag. If the second argument passed to this function is zero, then the
10720** indirect flag is cleared. If it is greater than zero, the indirect flag
10721** is set. Passing a value less than zero does not modify the current value
10722** of the indirect flag, and may be used to query the current state of the
10723** indirect flag for the specified session object.
10724**
10725** The return value indicates the final state of the indirect flag: 0 if
10726** it is clear, or 1 if it is set.
10727*/
10728SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
10729
10730/*
10731** CAPI3REF: Attach A Table To A Session Object
10732** METHOD: sqlite3_session
10733**
10734** If argument zTab is not NULL, then it is the name of a table to attach
10735** to the session object passed as the first argument. All subsequent changes
10736** made to the table while the session object is enabled will be recorded. See
10737** documentation for [sqlite3session_changeset()] for further details.
10738**
10739** Or, if argument zTab is NULL, then changes are recorded for all tables
10740** in the database. If additional tables are added to the database (by
10741** executing "CREATE TABLE" statements) after this call is made, changes for
10742** the new tables are also recorded.
10743**
10744** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
10745** defined as part of their CREATE TABLE statement. It does not matter if the
10746** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
10747** KEY may consist of a single column, or may be a composite key.
10748**
10749** It is not an error if the named table does not exist in the database. Nor
10750** is it an error if the named table does not have a PRIMARY KEY. However,
10751** no changes will be recorded in either of these scenarios.
10752**
10753** Changes are not recorded for individual rows that have NULL values stored
10754** in one or more of their PRIMARY KEY columns.
10755**
10756** SQLITE_OK is returned if the call completes without error. Or, if an error
10757** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
10758**
10759** <h3>Special sqlite_stat1 Handling</h3>
10760**
10761** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
10762** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
10763** <pre>
10764** &nbsp; CREATE TABLE sqlite_stat1(tbl,idx,stat)
10765** </pre>
10766**
10767** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
10768** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
10769** are recorded for rows for which (idx IS NULL) is true. However, for such
10770** rows a zero-length blob (SQL value X'') is stored in the changeset or
10771** patchset instead of a NULL value. This allows such changesets to be
10772** manipulated by legacy implementations of sqlite3changeset_invert(),
10773** concat() and similar.
10774**
10775** The sqlite3changeset_apply() function automatically converts the
10776** zero-length blob back to a NULL value when updating the sqlite_stat1
10777** table. However, if the application calls sqlite3changeset_new(),
10778** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
10779** iterator directly (including on a changeset iterator passed to a
10780** conflict-handler callback) then the X'' value is returned. The application
10781** must translate X'' to NULL itself if required.
10782**
10783** Legacy (older than 3.22.0) versions of the sessions module cannot capture
10784** changes made to the sqlite_stat1 table. Legacy versions of the
10785** sqlite3changeset_apply() function silently ignore any modifications to the
10786** sqlite_stat1 table that are part of a changeset or patchset.
10787*/
10788SQLITE_API int sqlite3session_attach(
10789 sqlite3_session *pSession, /* Session object */
10790 const char *zTab /* Table name */
10791);
10792
10793/*
10794** CAPI3REF: Set a table filter on a Session Object.
10795** METHOD: sqlite3_session
10796**
10797** The second argument (xFilter) is the "filter callback". For changes to rows
10798** in tables that are not attached to the Session object, the filter is called
10799** to determine whether changes to the table's rows should be tracked or not.
10800** If xFilter returns 0, changes is not tracked. Note that once a table is
10801** attached, xFilter will not be called again.
10802*/
10803SQLITE_API void sqlite3session_table_filter(
10804 sqlite3_session *pSession, /* Session object */
10805 int(*xFilter)(
10806 void *pCtx, /* Copy of third arg to _filter_table() */
10807 const char *zTab /* Table name */
10808 ),
10809 void *pCtx /* First argument passed to xFilter */
10810);
10811
10812/*
10813** CAPI3REF: Generate A Changeset From A Session Object
10814** METHOD: sqlite3_session
10815**
10816** Obtain a changeset containing changes to the tables attached to the
10817** session object passed as the first argument. If successful,
10818** set *ppChangeset to point to a buffer containing the changeset
10819** and *pnChangeset to the size of the changeset in bytes before returning
10820** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
10821** zero and return an SQLite error code.
10822**
10823** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
10824** each representing a change to a single row of an attached table. An INSERT
10825** change contains the values of each field of a new database row. A DELETE
10826** contains the original values of each field of a deleted database row. An
10827** UPDATE change contains the original values of each field of an updated
10828** database row along with the updated values for each updated non-primary-key
10829** column. It is not possible for an UPDATE change to represent a change that
10830** modifies the values of primary key columns. If such a change is made, it
10831** is represented in a changeset as a DELETE followed by an INSERT.
10832**
10833** Changes are not recorded for rows that have NULL values stored in one or
10834** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
10835** no corresponding change is present in the changesets returned by this
10836** function. If an existing row with one or more NULL values stored in
10837** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
10838** only an INSERT is appears in the changeset. Similarly, if an existing row
10839** with non-NULL PRIMARY KEY values is updated so that one or more of its
10840** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
10841** DELETE change only.
10842**
10843** The contents of a changeset may be traversed using an iterator created
10844** using the [sqlite3changeset_start()] API. A changeset may be applied to
10845** a database with a compatible schema using the [sqlite3changeset_apply()]
10846** API.
10847**
10848** Within a changeset generated by this function, all changes related to a
10849** single table are grouped together. In other words, when iterating through
10850** a changeset or when applying a changeset to a database, all changes related
10851** to a single table are processed before moving on to the next table. Tables
10852** are sorted in the same order in which they were attached (or auto-attached)
10853** to the sqlite3_session object. The order in which the changes related to
10854** a single table are stored is undefined.
10855**
10856** Following a successful call to this function, it is the responsibility of
10857** the caller to eventually free the buffer that *ppChangeset points to using
10858** [sqlite3_free()].
10859**
10860** <h3>Changeset Generation</h3>
10861**
10862** Once a table has been attached to a session object, the session object
10863** records the primary key values of all new rows inserted into the table.
10864** It also records the original primary key and other column values of any
10865** deleted or updated rows. For each unique primary key value, data is only
10866** recorded once - the first time a row with said primary key is inserted,
10867** updated or deleted in the lifetime of the session.
10868**
10869** There is one exception to the previous paragraph: when a row is inserted,
10870** updated or deleted, if one or more of its primary key columns contain a
10871** NULL value, no record of the change is made.
10872**
10873** The session object therefore accumulates two types of records - those
10874** that consist of primary key values only (created when the user inserts
10875** a new record) and those that consist of the primary key values and the
10876** original values of other table columns (created when the users deletes
10877** or updates a record).
10878**
10879** When this function is called, the requested changeset is created using
10880** both the accumulated records and the current contents of the database
10881** file. Specifically:
10882**
10883** <ul>
10884** <li> For each record generated by an insert, the database is queried
10885** for a row with a matching primary key. If one is found, an INSERT
10886** change is added to the changeset. If no such row is found, no change
10887** is added to the changeset.
10888**
10889** <li> For each record generated by an update or delete, the database is
10890** queried for a row with a matching primary key. If such a row is
10891** found and one or more of the non-primary key fields have been
10892** modified from their original values, an UPDATE change is added to
10893** the changeset. Or, if no such row is found in the table, a DELETE
10894** change is added to the changeset. If there is a row with a matching
10895** primary key in the database, but all fields contain their original
10896** values, no change is added to the changeset.
10897** </ul>
10898**
10899** This means, amongst other things, that if a row is inserted and then later
10900** deleted while a session object is active, neither the insert nor the delete
10901** will be present in the changeset. Or if a row is deleted and then later a
10902** row with the same primary key values inserted while a session object is
10903** active, the resulting changeset will contain an UPDATE change instead of
10904** a DELETE and an INSERT.
10905**
10906** When a session object is disabled (see the [sqlite3session_enable()] API),
10907** it does not accumulate records when rows are inserted, updated or deleted.
10908** This may appear to have some counter-intuitive effects if a single row
10909** is written to more than once during a session. For example, if a row
10910** is inserted while a session object is enabled, then later deleted while
10911** the same session object is disabled, no INSERT record will appear in the
10912** changeset, even though the delete took place while the session was disabled.
10913** Or, if one field of a row is updated while a session is disabled, and
10914** another field of the same row is updated while the session is enabled, the
10915** resulting changeset will contain an UPDATE change that updates both fields.
10916*/
10917SQLITE_API int sqlite3session_changeset(
10918 sqlite3_session *pSession, /* Session object */
10919 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
10920 void **ppChangeset /* OUT: Buffer containing changeset */
10921);
10922
10923/*
10924** CAPI3REF: Load The Difference Between Tables Into A Session
10925** METHOD: sqlite3_session
10926**
10927** If it is not already attached to the session object passed as the first
10928** argument, this function attaches table zTbl in the same manner as the
10929** [sqlite3session_attach()] function. If zTbl does not exist, or if it
10930** does not have a primary key, this function is a no-op (but does not return
10931** an error).
10932**
10933** Argument zFromDb must be the name of a database ("main", "temp" etc.)
10934** attached to the same database handle as the session object that contains
10935** a table compatible with the table attached to the session by this function.
10936** A table is considered compatible if it:
10937**
10938** <ul>
10939** <li> Has the same name,
10940** <li> Has the same set of columns declared in the same order, and
10941** <li> Has the same PRIMARY KEY definition.
10942** </ul>
10943**
10944** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
10945** are compatible but do not have any PRIMARY KEY columns, it is not an error
10946** but no changes are added to the session object. As with other session
10947** APIs, tables without PRIMARY KEYs are simply ignored.
10948**
10949** This function adds a set of changes to the session object that could be
10950** used to update the table in database zFrom (call this the "from-table")
10951** so that its content is the same as the table attached to the session
10952** object (call this the "to-table"). Specifically:
10953**
10954** <ul>
10955** <li> For each row (primary key) that exists in the to-table but not in
10956** the from-table, an INSERT record is added to the session object.
10957**
10958** <li> For each row (primary key) that exists in the to-table but not in
10959** the from-table, a DELETE record is added to the session object.
10960**
10961** <li> For each row (primary key) that exists in both tables, but features
10962** different non-PK values in each, an UPDATE record is added to the
10963** session.
10964** </ul>
10965**
10966** To clarify, if this function is called and then a changeset constructed
10967** using [sqlite3session_changeset()], then after applying that changeset to
10968** database zFrom the contents of the two compatible tables would be
10969** identical.
10970**
10971** It an error if database zFrom does not exist or does not contain the
10972** required compatible table.
10973**
10974** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite
10975** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
10976** may be set to point to a buffer containing an English language error
10977** message. It is the responsibility of the caller to free this buffer using
10978** sqlite3_free().
10979*/
10980SQLITE_API int sqlite3session_diff(
10981 sqlite3_session *pSession,
10982 const char *zFromDb,
10983 const char *zTbl,
10984 char **pzErrMsg
10985);
10986
10987
10988/*
10989** CAPI3REF: Generate A Patchset From A Session Object
10990** METHOD: sqlite3_session
10991**
10992** The differences between a patchset and a changeset are that:
10993**
10994** <ul>
10995** <li> DELETE records consist of the primary key fields only. The
10996** original values of other fields are omitted.
10997** <li> The original values of any modified fields are omitted from
10998** UPDATE records.
10999** </ul>
11000**
11001** A patchset blob may be used with up to date versions of all
11002** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
11003** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
11004** attempting to use a patchset blob with old versions of the
11005** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
11006**
11007** Because the non-primary key "old.*" fields are omitted, no
11008** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
11009** is passed to the sqlite3changeset_apply() API. Other conflict types work
11010** in the same way as for changesets.
11011**
11012** Changes within a patchset are ordered in the same way as for changesets
11013** generated by the sqlite3session_changeset() function (i.e. all changes for
11014** a single table are grouped together, tables appear in the order in which
11015** they were attached to the session object).
11016*/
11017SQLITE_API int sqlite3session_patchset(
11018 sqlite3_session *pSession, /* Session object */
11019 int *pnPatchset, /* OUT: Size of buffer at *ppPatchset */
11020 void **ppPatchset /* OUT: Buffer containing patchset */
11021);
11022
11023/*
11024** CAPI3REF: Test if a changeset has recorded any changes.
11025**
11026** Return non-zero if no changes to attached tables have been recorded by
11027** the session object passed as the first argument. Otherwise, if one or
11028** more changes have been recorded, return zero.
11029**
11030** Even if this function returns zero, it is possible that calling
11031** [sqlite3session_changeset()] on the session handle may still return a
11032** changeset that contains no changes. This can happen when a row in
11033** an attached table is modified and then later on the original values
11034** are restored. However, if this function returns non-zero, then it is
11035** guaranteed that a call to sqlite3session_changeset() will return a
11036** changeset containing zero changes.
11037*/
11038SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
11039
11040/*
11041** CAPI3REF: Create An Iterator To Traverse A Changeset
11042** CONSTRUCTOR: sqlite3_changeset_iter
11043**
11044** Create an iterator used to iterate through the contents of a changeset.
11045** If successful, *pp is set to point to the iterator handle and SQLITE_OK
11046** is returned. Otherwise, if an error occurs, *pp is set to zero and an
11047** SQLite error code is returned.
11048**
11049** The following functions can be used to advance and query a changeset
11050** iterator created by this function:
11051**
11052** <ul>
11053** <li> [sqlite3changeset_next()]
11054** <li> [sqlite3changeset_op()]
11055** <li> [sqlite3changeset_new()]
11056** <li> [sqlite3changeset_old()]
11057** </ul>
11058**
11059** It is the responsibility of the caller to eventually destroy the iterator
11060** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
11061** changeset (pChangeset) must remain valid until after the iterator is
11062** destroyed.
11063**
11064** Assuming the changeset blob was created by one of the
11065** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
11066** [sqlite3changeset_invert()] functions, all changes within the changeset
11067** that apply to a single table are grouped together. This means that when
11068** an application iterates through a changeset using an iterator created by
11069** this function, all changes that relate to a single table are visited
11070** consecutively. There is no chance that the iterator will visit a change
11071** the applies to table X, then one for table Y, and then later on visit
11072** another change for table X.
11073**
11074** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
11075** may be modified by passing a combination of
11076** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
11077**
11078** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
11079** and therefore subject to change.
11080*/
11081SQLITE_API int sqlite3changeset_start(
11082 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11083 int nChangeset, /* Size of changeset blob in bytes */
11084 void *pChangeset /* Pointer to blob containing changeset */
11085);
11086SQLITE_API int sqlite3changeset_start_v2(
11087 sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */
11088 int nChangeset, /* Size of changeset blob in bytes */
11089 void *pChangeset, /* Pointer to blob containing changeset */
11090 int flags /* SESSION_CHANGESETSTART_* flags */
11091);
11092
11093/*
11094** CAPI3REF: Flags for sqlite3changeset_start_v2
11095**
11096** The following flags may passed via the 4th parameter to
11097** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
11098**
11099** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11100** Invert the changeset while iterating through it. This is equivalent to
11101** inverting a changeset using sqlite3changeset_invert() before applying it.
11102** It is an error to specify this flag with a patchset.
11103*/
11104#define SQLITE_CHANGESETSTART_INVERT 0x0002
11105
11106
11107/*
11108** CAPI3REF: Advance A Changeset Iterator
11109** METHOD: sqlite3_changeset_iter
11110**
11111** This function may only be used with iterators created by function
11112** [sqlite3changeset_start()]. If it is called on an iterator passed to
11113** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11114** is returned and the call has no effect.
11115**
11116** Immediately after an iterator is created by sqlite3changeset_start(), it
11117** does not point to any change in the changeset. Assuming the changeset
11118** is not empty, the first call to this function advances the iterator to
11119** point to the first change in the changeset. Each subsequent call advances
11120** the iterator to point to the next change in the changeset (if any). If
11121** no error occurs and the iterator points to a valid change after a call
11122** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
11123** Otherwise, if all changes in the changeset have already been visited,
11124** SQLITE_DONE is returned.
11125**
11126** If an error occurs, an SQLite error code is returned. Possible error
11127** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
11128** SQLITE_NOMEM.
11129*/
11130SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
11131
11132/*
11133** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
11134** METHOD: sqlite3_changeset_iter
11135**
11136** The pIter argument passed to this function may either be an iterator
11137** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11138** created by [sqlite3changeset_start()]. In the latter case, the most recent
11139** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
11140** is not the case, this function returns [SQLITE_MISUSE].
11141**
11142** If argument pzTab is not NULL, then *pzTab is set to point to a
11143** nul-terminated utf-8 encoded string containing the name of the table
11144** affected by the current change. The buffer remains valid until either
11145** sqlite3changeset_next() is called on the iterator or until the
11146** conflict-handler function returns. If pnCol is not NULL, then *pnCol is
11147** set to the number of columns in the table affected by the change. If
11148** pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
11149** is an indirect change, or false (0) otherwise. See the documentation for
11150** [sqlite3session_indirect()] for a description of direct and indirect
11151** changes. Finally, if pOp is not NULL, then *pOp is set to one of
11152** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the
11153** type of change that the iterator currently points to.
11154**
11155** If no error occurs, SQLITE_OK is returned. If an error does occur, an
11156** SQLite error code is returned. The values of the output variables may not
11157** be trusted in this case.
11158*/
11159SQLITE_API int sqlite3changeset_op(
11160 sqlite3_changeset_iter *pIter, /* Iterator object */
11161 const char **pzTab, /* OUT: Pointer to table name */
11162 int *pnCol, /* OUT: Number of columns in table */
11163 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
11164 int *pbIndirect /* OUT: True for an 'indirect' change */
11165);
11166
11167/*
11168** CAPI3REF: Obtain The Primary Key Definition Of A Table
11169** METHOD: sqlite3_changeset_iter
11170**
11171** For each modified table, a changeset includes the following:
11172**
11173** <ul>
11174** <li> The number of columns in the table, and
11175** <li> Which of those columns make up the tables PRIMARY KEY.
11176** </ul>
11177**
11178** This function is used to find which columns comprise the PRIMARY KEY of
11179** the table modified by the change that iterator pIter currently points to.
11180** If successful, *pabPK is set to point to an array of nCol entries, where
11181** nCol is the number of columns in the table. Elements of *pabPK are set to
11182** 0x01 if the corresponding column is part of the tables primary key, or
11183** 0x00 if it is not.
11184**
11185** If argument pnCol is not NULL, then *pnCol is set to the number of columns
11186** in the table.
11187**
11188** If this function is called when the iterator does not point to a valid
11189** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
11190** SQLITE_OK is returned and the output variables populated as described
11191** above.
11192*/
11193SQLITE_API int sqlite3changeset_pk(
11194 sqlite3_changeset_iter *pIter, /* Iterator object */
11195 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11196 int *pnCol /* OUT: Number of entries in output array */
11197);
11198
11199/*
11200** CAPI3REF: Obtain old.* Values From A Changeset Iterator
11201** METHOD: sqlite3_changeset_iter
11202**
11203** The pIter argument passed to this function may either be an iterator
11204** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11205** created by [sqlite3changeset_start()]. In the latter case, the most recent
11206** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11207** Furthermore, it may only be called if the type of change that the iterator
11208** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
11209** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11210**
11211** Argument iVal must be greater than or equal to 0, and less than the number
11212** of columns in the table affected by the current change. Otherwise,
11213** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11214**
11215** If successful, this function sets *ppValue to point to a protected
11216** sqlite3_value object containing the iVal'th value from the vector of
11217** original row values stored as part of the UPDATE or DELETE change and
11218** returns SQLITE_OK. The name of the function comes from the fact that this
11219** is similar to the "old.*" columns available to update or delete triggers.
11220**
11221** If some other error occurs (e.g. an OOM condition), an SQLite error code
11222** is returned and *ppValue is set to NULL.
11223*/
11224SQLITE_API int sqlite3changeset_old(
11225 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11226 int iVal, /* Column number */
11227 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
11228);
11229
11230/*
11231** CAPI3REF: Obtain new.* Values From A Changeset Iterator
11232** METHOD: sqlite3_changeset_iter
11233**
11234** The pIter argument passed to this function may either be an iterator
11235** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11236** created by [sqlite3changeset_start()]. In the latter case, the most recent
11237** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
11238** Furthermore, it may only be called if the type of change that the iterator
11239** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
11240** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
11241**
11242** Argument iVal must be greater than or equal to 0, and less than the number
11243** of columns in the table affected by the current change. Otherwise,
11244** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11245**
11246** If successful, this function sets *ppValue to point to a protected
11247** sqlite3_value object containing the iVal'th value from the vector of
11248** new row values stored as part of the UPDATE or INSERT change and
11249** returns SQLITE_OK. If the change is an UPDATE and does not include
11250** a new value for the requested column, *ppValue is set to NULL and
11251** SQLITE_OK returned. The name of the function comes from the fact that
11252** this is similar to the "new.*" columns available to update or delete
11253** triggers.
11254**
11255** If some other error occurs (e.g. an OOM condition), an SQLite error code
11256** is returned and *ppValue is set to NULL.
11257*/
11258SQLITE_API int sqlite3changeset_new(
11259 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11260 int iVal, /* Column number */
11261 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
11262);
11263
11264/*
11265** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
11266** METHOD: sqlite3_changeset_iter
11267**
11268** This function should only be used with iterator objects passed to a
11269** conflict-handler callback by [sqlite3changeset_apply()] with either
11270** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
11271** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
11272** is set to NULL.
11273**
11274** Argument iVal must be greater than or equal to 0, and less than the number
11275** of columns in the table affected by the current change. Otherwise,
11276** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
11277**
11278** If successful, this function sets *ppValue to point to a protected
11279** sqlite3_value object containing the iVal'th value from the
11280** "conflicting row" associated with the current conflict-handler callback
11281** and returns SQLITE_OK.
11282**
11283** If some other error occurs (e.g. an OOM condition), an SQLite error code
11284** is returned and *ppValue is set to NULL.
11285*/
11286SQLITE_API int sqlite3changeset_conflict(
11287 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11288 int iVal, /* Column number */
11289 sqlite3_value **ppValue /* OUT: Value from conflicting row */
11290);
11291
11292/*
11293** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
11294** METHOD: sqlite3_changeset_iter
11295**
11296** This function may only be called with an iterator passed to an
11297** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
11298** it sets the output variable to the total number of known foreign key
11299** violations in the destination database and returns SQLITE_OK.
11300**
11301** In all other cases this function returns SQLITE_MISUSE.
11302*/
11303SQLITE_API int sqlite3changeset_fk_conflicts(
11304 sqlite3_changeset_iter *pIter, /* Changeset iterator */
11305 int *pnOut /* OUT: Number of FK violations */
11306);
11307
11308
11309/*
11310** CAPI3REF: Finalize A Changeset Iterator
11311** METHOD: sqlite3_changeset_iter
11312**
11313** This function is used to finalize an iterator allocated with
11314** [sqlite3changeset_start()].
11315**
11316** This function should only be called on iterators created using the
11317** [sqlite3changeset_start()] function. If an application calls this
11318** function with an iterator passed to a conflict-handler by
11319** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
11320** call has no effect.
11321**
11322** If an error was encountered within a call to an sqlite3changeset_xxx()
11323** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
11324** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
11325** to that error is returned by this function. Otherwise, SQLITE_OK is
11326** returned. This is to allow the following pattern (pseudo-code):
11327**
11328** <pre>
11329** sqlite3changeset_start();
11330** while( SQLITE_ROW==sqlite3changeset_next() ){
11331** // Do something with change.
11332** }
11333** rc = sqlite3changeset_finalize();
11334** if( rc!=SQLITE_OK ){
11335** // An error has occurred
11336** }
11337** </pre>
11338*/
11339SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
11340
11341/*
11342** CAPI3REF: Invert A Changeset
11343**
11344** This function is used to "invert" a changeset object. Applying an inverted
11345** changeset to a database reverses the effects of applying the uninverted
11346** changeset. Specifically:
11347**
11348** <ul>
11349** <li> Each DELETE change is changed to an INSERT, and
11350** <li> Each INSERT change is changed to a DELETE, and
11351** <li> For each UPDATE change, the old.* and new.* values are exchanged.
11352** </ul>
11353**
11354** This function does not change the order in which changes appear within
11355** the changeset. It merely reverses the sense of each individual change.
11356**
11357** If successful, a pointer to a buffer containing the inverted changeset
11358** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
11359** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
11360** zeroed and an SQLite error code returned.
11361**
11362** It is the responsibility of the caller to eventually call sqlite3_free()
11363** on the *ppOut pointer to free the buffer allocation following a successful
11364** call to this function.
11365**
11366** WARNING/TODO: This function currently assumes that the input is a valid
11367** changeset. If it is not, the results are undefined.
11368*/
11369SQLITE_API int sqlite3changeset_invert(
11370 int nIn, const void *pIn, /* Input changeset */
11371 int *pnOut, void **ppOut /* OUT: Inverse of input */
11372);
11373
11374/*
11375** CAPI3REF: Concatenate Two Changeset Objects
11376**
11377** This function is used to concatenate two changesets, A and B, into a
11378** single changeset. The result is a changeset equivalent to applying
11379** changeset A followed by changeset B.
11380**
11381** This function combines the two input changesets using an
11382** sqlite3_changegroup object. Calling it produces similar results as the
11383** following code fragment:
11384**
11385** <pre>
11386** sqlite3_changegroup *pGrp;
11387** rc = sqlite3_changegroup_new(&pGrp);
11388** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
11389** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
11390** if( rc==SQLITE_OK ){
11391** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
11392** }else{
11393** *ppOut = 0;
11394** *pnOut = 0;
11395** }
11396** </pre>
11397**
11398** Refer to the sqlite3_changegroup documentation below for details.
11399*/
11400SQLITE_API int sqlite3changeset_concat(
11401 int nA, /* Number of bytes in buffer pA */
11402 void *pA, /* Pointer to buffer containing changeset A */
11403 int nB, /* Number of bytes in buffer pB */
11404 void *pB, /* Pointer to buffer containing changeset B */
11405 int *pnOut, /* OUT: Number of bytes in output changeset */
11406 void **ppOut /* OUT: Buffer containing output changeset */
11407);
11408
11409
11410/*
11411** CAPI3REF: Changegroup Handle
11412**
11413** A changegroup is an object used to combine two or more
11414** [changesets] or [patchsets]
11415*/
11416typedef struct sqlite3_changegroup sqlite3_changegroup;
11417
11418/*
11419** CAPI3REF: Create A New Changegroup Object
11420** CONSTRUCTOR: sqlite3_changegroup
11421**
11422** An sqlite3_changegroup object is used to combine two or more changesets
11423** (or patchsets) into a single changeset (or patchset). A single changegroup
11424** object may combine changesets or patchsets, but not both. The output is
11425** always in the same format as the input.
11426**
11427** If successful, this function returns SQLITE_OK and populates (*pp) with
11428** a pointer to a new sqlite3_changegroup object before returning. The caller
11429** should eventually free the returned object using a call to
11430** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
11431** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
11432**
11433** The usual usage pattern for an sqlite3_changegroup object is as follows:
11434**
11435** <ul>
11436** <li> It is created using a call to sqlite3changegroup_new().
11437**
11438** <li> Zero or more changesets (or patchsets) are added to the object
11439** by calling sqlite3changegroup_add().
11440**
11441** <li> The result of combining all input changesets together is obtained
11442** by the application via a call to sqlite3changegroup_output().
11443**
11444** <li> The object is deleted using a call to sqlite3changegroup_delete().
11445** </ul>
11446**
11447** Any number of calls to add() and output() may be made between the calls to
11448** new() and delete(), and in any order.
11449**
11450** As well as the regular sqlite3changegroup_add() and
11451** sqlite3changegroup_output() functions, also available are the streaming
11452** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
11453*/
11454SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
11455
11456/*
11457** CAPI3REF: Add A Changeset To A Changegroup
11458** METHOD: sqlite3_changegroup
11459**
11460** Add all changes within the changeset (or patchset) in buffer pData (size
11461** nData bytes) to the changegroup.
11462**
11463** If the buffer contains a patchset, then all prior calls to this function
11464** on the same changegroup object must also have specified patchsets. Or, if
11465** the buffer contains a changeset, so must have the earlier calls to this
11466** function. Otherwise, SQLITE_ERROR is returned and no changes are added
11467** to the changegroup.
11468**
11469** Rows within the changeset and changegroup are identified by the values in
11470** their PRIMARY KEY columns. A change in the changeset is considered to
11471** apply to the same row as a change already present in the changegroup if
11472** the two rows have the same primary key.
11473**
11474** Changes to rows that do not already appear in the changegroup are
11475** simply copied into it. Or, if both the new changeset and the changegroup
11476** contain changes that apply to a single row, the final contents of the
11477** changegroup depends on the type of each change, as follows:
11478**
11479** <table border=1 style="margin-left:8ex;margin-right:8ex">
11480** <tr><th style="white-space:pre">Existing Change </th>
11481** <th style="white-space:pre">New Change </th>
11482** <th>Output Change
11483** <tr><td>INSERT <td>INSERT <td>
11484** The new change is ignored. This case does not occur if the new
11485** changeset was recorded immediately after the changesets already
11486** added to the changegroup.
11487** <tr><td>INSERT <td>UPDATE <td>
11488** The INSERT change remains in the changegroup. The values in the
11489** INSERT change are modified as if the row was inserted by the
11490** existing change and then updated according to the new change.
11491** <tr><td>INSERT <td>DELETE <td>
11492** The existing INSERT is removed from the changegroup. The DELETE is
11493** not added.
11494** <tr><td>UPDATE <td>INSERT <td>
11495** The new change is ignored. This case does not occur if the new
11496** changeset was recorded immediately after the changesets already
11497** added to the changegroup.
11498** <tr><td>UPDATE <td>UPDATE <td>
11499** The existing UPDATE remains within the changegroup. It is amended
11500** so that the accompanying values are as if the row was updated once
11501** by the existing change and then again by the new change.
11502** <tr><td>UPDATE <td>DELETE <td>
11503** The existing UPDATE is replaced by the new DELETE within the
11504** changegroup.
11505** <tr><td>DELETE <td>INSERT <td>
11506** If one or more of the column values in the row inserted by the
11507** new change differ from those in the row deleted by the existing
11508** change, the existing DELETE is replaced by an UPDATE within the
11509** changegroup. Otherwise, if the inserted row is exactly the same
11510** as the deleted row, the existing DELETE is simply discarded.
11511** <tr><td>DELETE <td>UPDATE <td>
11512** The new change is ignored. This case does not occur if the new
11513** changeset was recorded immediately after the changesets already
11514** added to the changegroup.
11515** <tr><td>DELETE <td>DELETE <td>
11516** The new change is ignored. This case does not occur if the new
11517** changeset was recorded immediately after the changesets already
11518** added to the changegroup.
11519** </table>
11520**
11521** If the new changeset contains changes to a table that is already present
11522** in the changegroup, then the number of columns and the position of the
11523** primary key columns for the table must be consistent. If this is not the
11524** case, this function fails with SQLITE_SCHEMA. If the input changeset
11525** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
11526** returned. Or, if an out-of-memory condition occurs during processing, this
11527** function returns SQLITE_NOMEM. In all cases, if an error occurs the
11528** final contents of the changegroup is undefined.
11529**
11530** If no error occurs, SQLITE_OK is returned.
11531*/
11532SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
11533
11534/*
11535** CAPI3REF: Obtain A Composite Changeset From A Changegroup
11536** METHOD: sqlite3_changegroup
11537**
11538** Obtain a buffer containing a changeset (or patchset) representing the
11539** current contents of the changegroup. If the inputs to the changegroup
11540** were themselves changesets, the output is a changeset. Or, if the
11541** inputs were patchsets, the output is also a patchset.
11542**
11543** As with the output of the sqlite3session_changeset() and
11544** sqlite3session_patchset() functions, all changes related to a single
11545** table are grouped together in the output of this function. Tables appear
11546** in the same order as for the very first changeset added to the changegroup.
11547** If the second or subsequent changesets added to the changegroup contain
11548** changes for tables that do not appear in the first changeset, they are
11549** appended onto the end of the output changeset, again in the order in
11550** which they are first encountered.
11551**
11552** If an error occurs, an SQLite error code is returned and the output
11553** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
11554** is returned and the output variables are set to the size of and a
11555** pointer to the output buffer, respectively. In this case it is the
11556** responsibility of the caller to eventually free the buffer using a
11557** call to sqlite3_free().
11558*/
11559SQLITE_API int sqlite3changegroup_output(
11560 sqlite3_changegroup*,
11561 int *pnData, /* OUT: Size of output buffer in bytes */
11562 void **ppData /* OUT: Pointer to output buffer */
11563);
11564
11565/*
11566** CAPI3REF: Delete A Changegroup Object
11567** DESTRUCTOR: sqlite3_changegroup
11568*/
11569SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
11570
11571/*
11572** CAPI3REF: Apply A Changeset To A Database
11573**
11574** Apply a changeset or patchset to a database. These functions attempt to
11575** update the "main" database attached to handle db with the changes found in
11576** the changeset passed via the second and third arguments.
11577**
11578** The fourth argument (xFilter) passed to these functions is the "filter
11579** callback". If it is not NULL, then for each table affected by at least one
11580** change in the changeset, the filter callback is invoked with
11581** the table name as the second argument, and a copy of the context pointer
11582** passed as the sixth argument as the first. If the "filter callback"
11583** returns zero, then no attempt is made to apply any changes to the table.
11584** Otherwise, if the return value is non-zero or the xFilter argument to
11585** is NULL, all changes related to the table are attempted.
11586**
11587** For each table that is not excluded by the filter callback, this function
11588** tests that the target database contains a compatible table. A table is
11589** considered compatible if all of the following are true:
11590**
11591** <ul>
11592** <li> The table has the same name as the name recorded in the
11593** changeset, and
11594** <li> The table has at least as many columns as recorded in the
11595** changeset, and
11596** <li> The table has primary key columns in the same position as
11597** recorded in the changeset.
11598** </ul>
11599**
11600** If there is no compatible table, it is not an error, but none of the
11601** changes associated with the table are applied. A warning message is issued
11602** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
11603** one such warning is issued for each table in the changeset.
11604**
11605** For each change for which there is a compatible table, an attempt is made
11606** to modify the table contents according to the UPDATE, INSERT or DELETE
11607** change. If a change cannot be applied cleanly, the conflict handler
11608** function passed as the fifth argument to sqlite3changeset_apply() may be
11609** invoked. A description of exactly when the conflict handler is invoked for
11610** each type of change is below.
11611**
11612** Unlike the xFilter argument, xConflict may not be passed NULL. The results
11613** of passing anything other than a valid function pointer as the xConflict
11614** argument are undefined.
11615**
11616** Each time the conflict handler function is invoked, it must return one
11617** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
11618** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
11619** if the second argument passed to the conflict handler is either
11620** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
11621** returns an illegal value, any changes already made are rolled back and
11622** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
11623** actions are taken by sqlite3changeset_apply() depending on the value
11624** returned by each invocation of the conflict-handler function. Refer to
11625** the documentation for the three
11626** [SQLITE_CHANGESET_OMIT|available return values] for details.
11627**
11628** <dl>
11629** <dt>DELETE Changes<dd>
11630** For each DELETE change, the function checks if the target database
11631** contains a row with the same primary key value (or values) as the
11632** original row values stored in the changeset. If it does, and the values
11633** stored in all non-primary key columns also match the values stored in
11634** the changeset the row is deleted from the target database.
11635**
11636** If a row with matching primary key values is found, but one or more of
11637** the non-primary key fields contains a value different from the original
11638** row value stored in the changeset, the conflict-handler function is
11639** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
11640** database table has more columns than are recorded in the changeset,
11641** only the values of those non-primary key fields are compared against
11642** the current database contents - any trailing database table columns
11643** are ignored.
11644**
11645** If no row with matching primary key values is found in the database,
11646** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11647** passed as the second argument.
11648**
11649** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
11650** (which can only happen if a foreign key constraint is violated), the
11651** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
11652** passed as the second argument. This includes the case where the DELETE
11653** operation is attempted because an earlier call to the conflict handler
11654** function returned [SQLITE_CHANGESET_REPLACE].
11655**
11656** <dt>INSERT Changes<dd>
11657** For each INSERT change, an attempt is made to insert the new row into
11658** the database. If the changeset row contains fewer fields than the
11659** database table, the trailing fields are populated with their default
11660** values.
11661**
11662** If the attempt to insert the row fails because the database already
11663** contains a row with the same primary key values, the conflict handler
11664** function is invoked with the second argument set to
11665** [SQLITE_CHANGESET_CONFLICT].
11666**
11667** If the attempt to insert the row fails because of some other constraint
11668** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
11669** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
11670** This includes the case where the INSERT operation is re-attempted because
11671** an earlier call to the conflict handler function returned
11672** [SQLITE_CHANGESET_REPLACE].
11673**
11674** <dt>UPDATE Changes<dd>
11675** For each UPDATE change, the function checks if the target database
11676** contains a row with the same primary key value (or values) as the
11677** original row values stored in the changeset. If it does, and the values
11678** stored in all modified non-primary key columns also match the values
11679** stored in the changeset the row is updated within the target database.
11680**
11681** If a row with matching primary key values is found, but one or more of
11682** the modified non-primary key fields contains a value different from an
11683** original row value stored in the changeset, the conflict-handler function
11684** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
11685** UPDATE changes only contain values for non-primary key fields that are
11686** to be modified, only those fields need to match the original values to
11687** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
11688**
11689** If no row with matching primary key values is found in the database,
11690** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11691** passed as the second argument.
11692**
11693** If the UPDATE operation is attempted, but SQLite returns
11694** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
11695** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
11696** This includes the case where the UPDATE operation is attempted after
11697** an earlier call to the conflict handler function returned
11698** [SQLITE_CHANGESET_REPLACE].
11699** </dl>
11700**
11701** It is safe to execute SQL statements, including those that write to the
11702** table that the callback related to, from within the xConflict callback.
11703** This can be used to further customize the applications conflict
11704** resolution strategy.
11705**
11706** All changes made by these functions are enclosed in a savepoint transaction.
11707** If any other error (aside from a constraint failure when attempting to
11708** write to the target database) occurs, then the savepoint transaction is
11709** rolled back, restoring the target database to its original state, and an
11710** SQLite error code returned.
11711**
11712** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
11713** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
11714** may set (*ppRebase) to point to a "rebase" that may be used with the
11715** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
11716** is set to the size of the buffer in bytes. It is the responsibility of the
11717** caller to eventually free any such buffer using sqlite3_free(). The buffer
11718** is only allocated and populated if one or more conflicts were encountered
11719** while applying the patchset. See comments surrounding the sqlite3_rebaser
11720** APIs for further details.
11721**
11722** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
11723** may be modified by passing a combination of
11724** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
11725**
11726** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
11727** and therefore subject to change.
11728*/
11729SQLITE_API int sqlite3changeset_apply(
11730 sqlite3 *db, /* Apply change to "main" db of this handle */
11731 int nChangeset, /* Size of changeset in bytes */
11732 void *pChangeset, /* Changeset blob */
11733 int(*xFilter)(
11734 void *pCtx, /* Copy of sixth arg to _apply() */
11735 const char *zTab /* Table name */
11736 ),
11737 int(*xConflict)(
11738 void *pCtx, /* Copy of sixth arg to _apply() */
11739 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11740 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11741 ),
11742 void *pCtx /* First argument passed to xConflict */
11743);
11744SQLITE_API int sqlite3changeset_apply_v2(
11745 sqlite3 *db, /* Apply change to "main" db of this handle */
11746 int nChangeset, /* Size of changeset in bytes */
11747 void *pChangeset, /* Changeset blob */
11748 int(*xFilter)(
11749 void *pCtx, /* Copy of sixth arg to _apply() */
11750 const char *zTab /* Table name */
11751 ),
11752 int(*xConflict)(
11753 void *pCtx, /* Copy of sixth arg to _apply() */
11754 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
11755 sqlite3_changeset_iter *p /* Handle describing change and conflict */
11756 ),
11757 void *pCtx, /* First argument passed to xConflict */
11758 void **ppRebase, int *pnRebase, /* OUT: Rebase data */
11759 int flags /* SESSION_CHANGESETAPPLY_* flags */
11760);
11761
11762/*
11763** CAPI3REF: Flags for sqlite3changeset_apply_v2
11764**
11765** The following flags may passed via the 9th parameter to
11766** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
11767**
11768** <dl>
11769** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
11770** Usually, the sessions module encloses all operations performed by
11771** a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
11772** SAVEPOINT is committed if the changeset or patchset is successfully
11773** applied, or rolled back if an error occurs. Specifying this flag
11774** causes the sessions module to omit this savepoint. In this case, if the
11775** caller has an open transaction or savepoint when apply_v2() is called,
11776** it may revert the partially applied changeset by rolling it back.
11777**
11778** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11779** Invert the changeset before applying it. This is equivalent to inverting
11780** a changeset using sqlite3changeset_invert() before applying it. It is
11781** an error to specify this flag with a patchset.
11782*/
11783#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
11784#define SQLITE_CHANGESETAPPLY_INVERT 0x0002
11785
11786/*
11787** CAPI3REF: Constants Passed To The Conflict Handler
11788**
11789** Values that may be passed as the second argument to a conflict-handler.
11790**
11791** <dl>
11792** <dt>SQLITE_CHANGESET_DATA<dd>
11793** The conflict handler is invoked with CHANGESET_DATA as the second argument
11794** when processing a DELETE or UPDATE change if a row with the required
11795** PRIMARY KEY fields is present in the database, but one or more other
11796** (non primary-key) fields modified by the update do not contain the
11797** expected "before" values.
11798**
11799** The conflicting row, in this case, is the database row with the matching
11800** primary key.
11801**
11802** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
11803** The conflict handler is invoked with CHANGESET_NOTFOUND as the second
11804** argument when processing a DELETE or UPDATE change if a row with the
11805** required PRIMARY KEY fields is not present in the database.
11806**
11807** There is no conflicting row in this case. The results of invoking the
11808** sqlite3changeset_conflict() API are undefined.
11809**
11810** <dt>SQLITE_CHANGESET_CONFLICT<dd>
11811** CHANGESET_CONFLICT is passed as the second argument to the conflict
11812** handler while processing an INSERT change if the operation would result
11813** in duplicate primary key values.
11814**
11815** The conflicting row in this case is the database row with the matching
11816** primary key.
11817**
11818** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
11819** If foreign key handling is enabled, and applying a changeset leaves the
11820** database in a state containing foreign key violations, the conflict
11821** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
11822** exactly once before the changeset is committed. If the conflict handler
11823** returns CHANGESET_OMIT, the changes, including those that caused the
11824** foreign key constraint violation, are committed. Or, if it returns
11825** CHANGESET_ABORT, the changeset is rolled back.
11826**
11827** No current or conflicting row information is provided. The only function
11828** it is possible to call on the supplied sqlite3_changeset_iter handle
11829** is sqlite3changeset_fk_conflicts().
11830**
11831** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
11832** If any other constraint violation occurs while applying a change (i.e.
11833** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
11834** invoked with CHANGESET_CONSTRAINT as the second argument.
11835**
11836** There is no conflicting row in this case. The results of invoking the
11837** sqlite3changeset_conflict() API are undefined.
11838**
11839** </dl>
11840*/
11841#define SQLITE_CHANGESET_DATA 1
11842#define SQLITE_CHANGESET_NOTFOUND 2
11843#define SQLITE_CHANGESET_CONFLICT 3
11844#define SQLITE_CHANGESET_CONSTRAINT 4
11845#define SQLITE_CHANGESET_FOREIGN_KEY 5
11846
11847/*
11848** CAPI3REF: Constants Returned By The Conflict Handler
11849**
11850** A conflict handler callback must return one of the following three values.
11851**
11852** <dl>
11853** <dt>SQLITE_CHANGESET_OMIT<dd>
11854** If a conflict handler returns this value no special action is taken. The
11855** change that caused the conflict is not applied. The session module
11856** continues to the next change in the changeset.
11857**
11858** <dt>SQLITE_CHANGESET_REPLACE<dd>
11859** This value may only be returned if the second argument to the conflict
11860** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
11861** is not the case, any changes applied so far are rolled back and the
11862** call to sqlite3changeset_apply() returns SQLITE_MISUSE.
11863**
11864** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
11865** handler, then the conflicting row is either updated or deleted, depending
11866** on the type of change.
11867**
11868** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
11869** handler, then the conflicting row is removed from the database and a
11870** second attempt to apply the change is made. If this second attempt fails,
11871** the original row is restored to the database before continuing.
11872**
11873** <dt>SQLITE_CHANGESET_ABORT<dd>
11874** If this value is returned, any changes applied so far are rolled back
11875** and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
11876** </dl>
11877*/
11878#define SQLITE_CHANGESET_OMIT 0
11879#define SQLITE_CHANGESET_REPLACE 1
11880#define SQLITE_CHANGESET_ABORT 2
11881
11882/*
11883** CAPI3REF: Rebasing changesets
11884** EXPERIMENTAL
11885**
11886** Suppose there is a site hosting a database in state S0. And that
11887** modifications are made that move that database to state S1 and a
11888** changeset recorded (the "local" changeset). Then, a changeset based
11889** on S0 is received from another site (the "remote" changeset) and
11890** applied to the database. The database is then in state
11891** (S1+"remote"), where the exact state depends on any conflict
11892** resolution decisions (OMIT or REPLACE) made while applying "remote".
11893** Rebasing a changeset is to update it to take those conflict
11894** resolution decisions into account, so that the same conflicts
11895** do not have to be resolved elsewhere in the network.
11896**
11897** For example, if both the local and remote changesets contain an
11898** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
11899**
11900** local: INSERT INTO t1 VALUES(1, 'v1');
11901** remote: INSERT INTO t1 VALUES(1, 'v2');
11902**
11903** and the conflict resolution is REPLACE, then the INSERT change is
11904** removed from the local changeset (it was overridden). Or, if the
11905** conflict resolution was "OMIT", then the local changeset is modified
11906** to instead contain:
11907**
11908** UPDATE t1 SET b = 'v2' WHERE a=1;
11909**
11910** Changes within the local changeset are rebased as follows:
11911**
11912** <dl>
11913** <dt>Local INSERT<dd>
11914** This may only conflict with a remote INSERT. If the conflict
11915** resolution was OMIT, then add an UPDATE change to the rebased
11916** changeset. Or, if the conflict resolution was REPLACE, add
11917** nothing to the rebased changeset.
11918**
11919** <dt>Local DELETE<dd>
11920** This may conflict with a remote UPDATE or DELETE. In both cases the
11921** only possible resolution is OMIT. If the remote operation was a
11922** DELETE, then add no change to the rebased changeset. If the remote
11923** operation was an UPDATE, then the old.* fields of change are updated
11924** to reflect the new.* values in the UPDATE.
11925**
11926** <dt>Local UPDATE<dd>
11927** This may conflict with a remote UPDATE or DELETE. If it conflicts
11928** with a DELETE, and the conflict resolution was OMIT, then the update
11929** is changed into an INSERT. Any undefined values in the new.* record
11930** from the update change are filled in using the old.* values from
11931** the conflicting DELETE. Or, if the conflict resolution was REPLACE,
11932** the UPDATE change is simply omitted from the rebased changeset.
11933**
11934** If conflict is with a remote UPDATE and the resolution is OMIT, then
11935** the old.* values are rebased using the new.* values in the remote
11936** change. Or, if the resolution is REPLACE, then the change is copied
11937** into the rebased changeset with updates to columns also updated by
11938** the conflicting remote UPDATE removed. If this means no columns would
11939** be updated, the change is omitted.
11940** </dl>
11941**
11942** A local change may be rebased against multiple remote changes
11943** simultaneously. If a single key is modified by multiple remote
11944** changesets, they are combined as follows before the local changeset
11945** is rebased:
11946**
11947** <ul>
11948** <li> If there has been one or more REPLACE resolutions on a
11949** key, it is rebased according to a REPLACE.
11950**
11951** <li> If there have been no REPLACE resolutions on a key, then
11952** the local changeset is rebased according to the most recent
11953** of the OMIT resolutions.
11954** </ul>
11955**
11956** Note that conflict resolutions from multiple remote changesets are
11957** combined on a per-field basis, not per-row. This means that in the
11958** case of multiple remote UPDATE operations, some fields of a single
11959** local change may be rebased for REPLACE while others are rebased for
11960** OMIT.
11961**
11962** In order to rebase a local changeset, the remote changeset must first
11963** be applied to the local database using sqlite3changeset_apply_v2() and
11964** the buffer of rebase information captured. Then:
11965**
11966** <ol>
11967** <li> An sqlite3_rebaser object is created by calling
11968** sqlite3rebaser_create().
11969** <li> The new object is configured with the rebase buffer obtained from
11970** sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
11971** If the local changeset is to be rebased against multiple remote
11972** changesets, then sqlite3rebaser_configure() should be called
11973** multiple times, in the same order that the multiple
11974** sqlite3changeset_apply_v2() calls were made.
11975** <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
11976** <li> The sqlite3_rebaser object is deleted by calling
11977** sqlite3rebaser_delete().
11978** </ol>
11979*/
11980typedef struct sqlite3_rebaser sqlite3_rebaser;
11981
11982/*
11983** CAPI3REF: Create a changeset rebaser object.
11984** EXPERIMENTAL
11985**
11986** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
11987** point to the new object and return SQLITE_OK. Otherwise, if an error
11988** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
11989** to NULL.
11990*/
11991SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
11992
11993/*
11994** CAPI3REF: Configure a changeset rebaser object.
11995** EXPERIMENTAL
11996**
11997** Configure the changeset rebaser object to rebase changesets according
11998** to the conflict resolutions described by buffer pRebase (size nRebase
11999** bytes), which must have been obtained from a previous call to
12000** sqlite3changeset_apply_v2().
12001*/
12002SQLITE_API int sqlite3rebaser_configure(
12003 sqlite3_rebaser*,
12004 int nRebase, const void *pRebase
12005);
12006
12007/*
12008** CAPI3REF: Rebase a changeset
12009** EXPERIMENTAL
12010**
12011** Argument pIn must point to a buffer containing a changeset nIn bytes
12012** in size. This function allocates and populates a buffer with a copy
12013** of the changeset rebased rebased according to the configuration of the
12014** rebaser object passed as the first argument. If successful, (*ppOut)
12015** is set to point to the new buffer containing the rebased changeset and
12016** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
12017** responsibility of the caller to eventually free the new buffer using
12018** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
12019** are set to zero and an SQLite error code returned.
12020*/
12021SQLITE_API int sqlite3rebaser_rebase(
12022 sqlite3_rebaser*,
12023 int nIn, const void *pIn,
12024 int *pnOut, void **ppOut
12025);
12026
12027/*
12028** CAPI3REF: Delete a changeset rebaser object.
12029** EXPERIMENTAL
12030**
12031** Delete the changeset rebaser object and all associated resources. There
12032** should be one call to this function for each successful invocation
12033** of sqlite3rebaser_create().
12034*/
12035SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
12036
12037/*
12038** CAPI3REF: Streaming Versions of API functions.
12039**
12040** The six streaming API xxx_strm() functions serve similar purposes to the
12041** corresponding non-streaming API functions:
12042**
12043** <table border=1 style="margin-left:8ex;margin-right:8ex">
12044** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12045** <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
12046** <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
12047** <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
12048** <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
12049** <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
12050** <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
12051** <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
12052** </table>
12053**
12054** Non-streaming functions that accept changesets (or patchsets) as input
12055** require that the entire changeset be stored in a single buffer in memory.
12056** Similarly, those that return a changeset or patchset do so by returning
12057** a pointer to a single large buffer allocated using sqlite3_malloc().
12058** Normally this is convenient. However, if an application running in a
12059** low-memory environment is required to handle very large changesets, the
12060** large contiguous memory allocations required can become onerous.
12061**
12062** In order to avoid this problem, instead of a single large buffer, input
12063** is passed to a streaming API functions by way of a callback function that
12064** the sessions module invokes to incrementally request input data as it is
12065** required. In all cases, a pair of API function parameters such as
12066**
12067** <pre>
12068** &nbsp; int nChangeset,
12069** &nbsp; void *pChangeset,
12070** </pre>
12071**
12072** Is replaced by:
12073**
12074** <pre>
12075** &nbsp; int (*xInput)(void *pIn, void *pData, int *pnData),
12076** &nbsp; void *pIn,
12077** </pre>
12078**
12079** Each time the xInput callback is invoked by the sessions module, the first
12080** argument passed is a copy of the supplied pIn context pointer. The second
12081** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
12082** error occurs the xInput method should copy up to (*pnData) bytes of data
12083** into the buffer and set (*pnData) to the actual number of bytes copied
12084** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
12085** should be set to zero to indicate this. Or, if an error occurs, an SQLite
12086** error code should be returned. In all cases, if an xInput callback returns
12087** an error, all processing is abandoned and the streaming API function
12088** returns a copy of the error code to the caller.
12089**
12090** In the case of sqlite3changeset_start_strm(), the xInput callback may be
12091** invoked by the sessions module at any point during the lifetime of the
12092** iterator. If such an xInput callback returns an error, the iterator enters
12093** an error state, whereby all subsequent calls to iterator functions
12094** immediately fail with the same error code as returned by xInput.
12095**
12096** Similarly, streaming API functions that return changesets (or patchsets)
12097** return them in chunks by way of a callback function instead of via a
12098** pointer to a single large buffer. In this case, a pair of parameters such
12099** as:
12100**
12101** <pre>
12102** &nbsp; int *pnChangeset,
12103** &nbsp; void **ppChangeset,
12104** </pre>
12105**
12106** Is replaced by:
12107**
12108** <pre>
12109** &nbsp; int (*xOutput)(void *pOut, const void *pData, int nData),
12110** &nbsp; void *pOut
12111** </pre>
12112**
12113** The xOutput callback is invoked zero or more times to return data to
12114** the application. The first parameter passed to each call is a copy of the
12115** pOut pointer supplied by the application. The second parameter, pData,
12116** points to a buffer nData bytes in size containing the chunk of output
12117** data being returned. If the xOutput callback successfully processes the
12118** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
12119** it should return some other SQLite error code. In this case processing
12120** is immediately abandoned and the streaming API function returns a copy
12121** of the xOutput error code to the application.
12122**
12123** The sessions module never invokes an xOutput callback with the third
12124** parameter set to a value less than or equal to zero. Other than this,
12125** no guarantees are made as to the size of the chunks of data returned.
12126*/
12127SQLITE_API int sqlite3changeset_apply_strm(
12128 sqlite3 *db, /* Apply change to "main" db of this handle */
12129 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12130 void *pIn, /* First arg for xInput */
12131 int(*xFilter)(
12132 void *pCtx, /* Copy of sixth arg to _apply() */
12133 const char *zTab /* Table name */
12134 ),
12135 int(*xConflict)(
12136 void *pCtx, /* Copy of sixth arg to _apply() */
12137 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12138 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12139 ),
12140 void *pCtx /* First argument passed to xConflict */
12141);
12142SQLITE_API int sqlite3changeset_apply_v2_strm(
12143 sqlite3 *db, /* Apply change to "main" db of this handle */
12144 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
12145 void *pIn, /* First arg for xInput */
12146 int(*xFilter)(
12147 void *pCtx, /* Copy of sixth arg to _apply() */
12148 const char *zTab /* Table name */
12149 ),
12150 int(*xConflict)(
12151 void *pCtx, /* Copy of sixth arg to _apply() */
12152 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
12153 sqlite3_changeset_iter *p /* Handle describing change and conflict */
12154 ),
12155 void *pCtx, /* First argument passed to xConflict */
12156 void **ppRebase, int *pnRebase,
12157 int flags
12158);
12159SQLITE_API int sqlite3changeset_concat_strm(
12160 int (*xInputA)(void *pIn, void *pData, int *pnData),
12161 void *pInA,
12162 int (*xInputB)(void *pIn, void *pData, int *pnData),
12163 void *pInB,
12164 int (*xOutput)(void *pOut, const void *pData, int nData),
12165 void *pOut
12166);
12167SQLITE_API int sqlite3changeset_invert_strm(
12168 int (*xInput)(void *pIn, void *pData, int *pnData),
12169 void *pIn,
12170 int (*xOutput)(void *pOut, const void *pData, int nData),
12171 void *pOut
12172);
12173SQLITE_API int sqlite3changeset_start_strm(
12174 sqlite3_changeset_iter **pp,
12175 int (*xInput)(void *pIn, void *pData, int *pnData),
12176 void *pIn
12177);
12178SQLITE_API int sqlite3changeset_start_v2_strm(
12179 sqlite3_changeset_iter **pp,
12180 int (*xInput)(void *pIn, void *pData, int *pnData),
12181 void *pIn,
12182 int flags
12183);
12184SQLITE_API int sqlite3session_changeset_strm(
12185 sqlite3_session *pSession,
12186 int (*xOutput)(void *pOut, const void *pData, int nData),
12187 void *pOut
12188);
12189SQLITE_API int sqlite3session_patchset_strm(
12190 sqlite3_session *pSession,
12191 int (*xOutput)(void *pOut, const void *pData, int nData),
12192 void *pOut
12193);
12194SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
12195 int (*xInput)(void *pIn, void *pData, int *pnData),
12196 void *pIn
12197);
12198SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
12199 int (*xOutput)(void *pOut, const void *pData, int nData),
12200 void *pOut
12201);
12202SQLITE_API int sqlite3rebaser_rebase_strm(
12203 sqlite3_rebaser *pRebaser,
12204 int (*xInput)(void *pIn, void *pData, int *pnData),
12205 void *pIn,
12206 int (*xOutput)(void *pOut, const void *pData, int nData),
12207 void *pOut
12208);
12209
12210/*
12211** CAPI3REF: Configure global parameters
12212**
12213** The sqlite3session_config() interface is used to make global configuration
12214** changes to the sessions module in order to tune it to the specific needs
12215** of the application.
12216**
12217** The sqlite3session_config() interface is not threadsafe. If it is invoked
12218** while any other thread is inside any other sessions method then the
12219** results are undefined. Furthermore, if it is invoked after any sessions
12220** related objects have been created, the results are also undefined.
12221**
12222** The first argument to the sqlite3session_config() function must be one
12223** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
12224** interpretation of the (void*) value passed as the second parameter and
12225** the effect of calling this function depends on the value of the first
12226** parameter.
12227**
12228** <dl>
12229** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
12230** By default, the sessions module streaming interfaces attempt to input
12231** and output data in approximately 1 KiB chunks. This operand may be used
12232** to set and query the value of this configuration setting. The pointer
12233** passed as the second argument must point to a value of type (int).
12234** If this value is greater than 0, it is used as the new streaming data
12235** chunk size for both input and output. Before returning, the (int) value
12236** pointed to by pArg is set to the final value of the streaming interface
12237** chunk size.
12238** </dl>
12239**
12240** This function returns SQLITE_OK if successful, or an SQLite error code
12241** otherwise.
12242*/
12243SQLITE_API int sqlite3session_config(int op, void *pArg);
12244
12245/*
12246** CAPI3REF: Values for sqlite3session_config().
12247*/
12248#define SQLITE_SESSION_CONFIG_STRMSIZE 1
12249
12250/*
12251** Make sure we can call this stuff from C++.
12252*/
12253#if 0
12254}
12255#endif
12256
12257#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
12258
12259/******** End of sqlite3session.h *********/
12260/******** Begin file fts5.h *********/
12261/*
12262** 2014 May 31
12263**
12264** The author disclaims copyright to this source code. In place of
12265** a legal notice, here is a blessing:
12266**
12267** May you do good and not evil.
12268** May you find forgiveness for yourself and forgive others.
12269** May you share freely, never taking more than you give.
12270**
12271******************************************************************************
12272**
12273** Interfaces to extend FTS5. Using the interfaces defined in this file,
12274** FTS5 may be extended with:
12275**
12276** * custom tokenizers, and
12277** * custom auxiliary functions.
12278*/
12279
12280
12281#ifndef _FTS5_H
12282#define _FTS5_H
12283
12284
12285#if 0
12286extern "C" {
12287#endif
12288
12289/*************************************************************************
12290** CUSTOM AUXILIARY FUNCTIONS
12291**
12292** Virtual table implementations may overload SQL functions by implementing
12293** the sqlite3_module.xFindFunction() method.
12294*/
12295
12296typedef struct Fts5ExtensionApi Fts5ExtensionApi;
12297typedef struct Fts5Context Fts5Context;
12298typedef struct Fts5PhraseIter Fts5PhraseIter;
12299
12300typedef void (*fts5_extension_function)(
12301 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
12302 Fts5Context *pFts, /* First arg to pass to pApi functions */
12303 sqlite3_context *pCtx, /* Context for returning result/error */
12304 int nVal, /* Number of values in apVal[] array */
12305 sqlite3_value **apVal /* Array of trailing arguments */
12306);
12307
12308struct Fts5PhraseIter {
12309 const unsigned char *a;
12310 const unsigned char *b;
12311};
12312
12313/*
12314** EXTENSION API FUNCTIONS
12315**
12316** xUserData(pFts):
12317** Return a copy of the context pointer the extension function was
12318** registered with.
12319**
12320** xColumnTotalSize(pFts, iCol, pnToken):
12321** If parameter iCol is less than zero, set output variable *pnToken
12322** to the total number of tokens in the FTS5 table. Or, if iCol is
12323** non-negative but less than the number of columns in the table, return
12324** the total number of tokens in column iCol, considering all rows in
12325** the FTS5 table.
12326**
12327** If parameter iCol is greater than or equal to the number of columns
12328** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12329** an OOM condition or IO error), an appropriate SQLite error code is
12330** returned.
12331**
12332** xColumnCount(pFts):
12333** Return the number of columns in the table.
12334**
12335** xColumnSize(pFts, iCol, pnToken):
12336** If parameter iCol is less than zero, set output variable *pnToken
12337** to the total number of tokens in the current row. Or, if iCol is
12338** non-negative but less than the number of columns in the table, set
12339** *pnToken to the number of tokens in column iCol of the current row.
12340**
12341** If parameter iCol is greater than or equal to the number of columns
12342** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
12343** an OOM condition or IO error), an appropriate SQLite error code is
12344** returned.
12345**
12346** This function may be quite inefficient if used with an FTS5 table
12347** created with the "columnsize=0" option.
12348**
12349** xColumnText:
12350** This function attempts to retrieve the text of column iCol of the
12351** current document. If successful, (*pz) is set to point to a buffer
12352** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12353** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12354** if an error occurs, an SQLite error code is returned and the final values
12355** of (*pz) and (*pn) are undefined.
12356**
12357** xPhraseCount:
12358** Returns the number of phrases in the current query expression.
12359**
12360** xPhraseSize:
12361** Returns the number of tokens in phrase iPhrase of the query. Phrases
12362** are numbered starting from zero.
12363**
12364** xInstCount:
12365** Set *pnInst to the total number of occurrences of all phrases within
12366** the query within the current row. Return SQLITE_OK if successful, or
12367** an error code (i.e. SQLITE_NOMEM) if an error occurs.
12368**
12369** This API can be quite slow if used with an FTS5 table created with the
12370** "detail=none" or "detail=column" option. If the FTS5 table is created
12371** with either "detail=none" or "detail=column" and "content=" option
12372** (i.e. if it is a contentless table), then this API always returns 0.
12373**
12374** xInst:
12375** Query for the details of phrase match iIdx within the current row.
12376** Phrase matches are numbered starting from zero, so the iIdx argument
12377** should be greater than or equal to zero and smaller than the value
12378** output by xInstCount().
12379**
12380** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12381** to the column in which it occurs and *piOff the token offset of the
12382** first token of the phrase. Returns SQLITE_OK if successful, or an error
12383** code (i.e. SQLITE_NOMEM) if an error occurs.
12384**
12385** This API can be quite slow if used with an FTS5 table created with the
12386** "detail=none" or "detail=column" option.
12387**
12388** xRowid:
12389** Returns the rowid of the current row.
12390**
12391** xTokenize:
12392** Tokenize text using the tokenizer belonging to the FTS5 table.
12393**
12394** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
12395** This API function is used to query the FTS table for phrase iPhrase
12396** of the current query. Specifically, a query equivalent to:
12397**
12398** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
12399**
12400** with $p set to a phrase equivalent to the phrase iPhrase of the
12401** current query is executed. Any column filter that applies to
12402** phrase iPhrase of the current query is included in $p. For each
12403** row visited, the callback function passed as the fourth argument
12404** is invoked. The context and API objects passed to the callback
12405** function may be used to access the properties of each matched row.
12406** Invoking Api.xUserData() returns a copy of the pointer passed as
12407** the third argument to pUserData.
12408**
12409** If the callback function returns any value other than SQLITE_OK, the
12410** query is abandoned and the xQueryPhrase function returns immediately.
12411** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
12412** Otherwise, the error code is propagated upwards.
12413**
12414** If the query runs to completion without incident, SQLITE_OK is returned.
12415** Or, if some error occurs before the query completes or is aborted by
12416** the callback, an SQLite error code is returned.
12417**
12418**
12419** xSetAuxdata(pFts5, pAux, xDelete)
12420**
12421** Save the pointer passed as the second argument as the extension functions
12422** "auxiliary data". The pointer may then be retrieved by the current or any
12423** future invocation of the same fts5 extension function made as part of
12424** the same MATCH query using the xGetAuxdata() API.
12425**
12426** Each extension function is allocated a single auxiliary data slot for
12427** each FTS query (MATCH expression). If the extension function is invoked
12428** more than once for a single FTS query, then all invocations share a
12429** single auxiliary data context.
12430**
12431** If there is already an auxiliary data pointer when this function is
12432** invoked, then it is replaced by the new pointer. If an xDelete callback
12433** was specified along with the original pointer, it is invoked at this
12434** point.
12435**
12436** The xDelete callback, if one is specified, is also invoked on the
12437** auxiliary data pointer after the FTS5 query has finished.
12438**
12439** If an error (e.g. an OOM condition) occurs within this function,
12440** the auxiliary data is set to NULL and an error code returned. If the
12441** xDelete parameter was not NULL, it is invoked on the auxiliary data
12442** pointer before returning.
12443**
12444**
12445** xGetAuxdata(pFts5, bClear)
12446**
12447** Returns the current auxiliary data pointer for the fts5 extension
12448** function. See the xSetAuxdata() method for details.
12449**
12450** If the bClear argument is non-zero, then the auxiliary data is cleared
12451** (set to NULL) before this function returns. In this case the xDelete,
12452** if any, is not invoked.
12453**
12454**
12455** xRowCount(pFts5, pnRow)
12456**
12457** This function is used to retrieve the total number of rows in the table.
12458** In other words, the same value that would be returned by:
12459**
12460** SELECT count(*) FROM ftstable;
12461**
12462** xPhraseFirst()
12463** This function is used, along with type Fts5PhraseIter and the xPhraseNext
12464** method, to iterate through all instances of a single query phrase within
12465** the current row. This is the same information as is accessible via the
12466** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
12467** to use, this API may be faster under some circumstances. To iterate
12468** through instances of phrase iPhrase, use the following code:
12469**
12470** Fts5PhraseIter iter;
12471** int iCol, iOff;
12472** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12473** iCol>=0;
12474** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12475** ){
12476** // An instance of phrase iPhrase at offset iOff of column iCol
12477** }
12478**
12479** The Fts5PhraseIter structure is defined above. Applications should not
12480** modify this structure directly - it should only be used as shown above
12481** with the xPhraseFirst() and xPhraseNext() API methods (and by
12482** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
12483**
12484** This API can be quite slow if used with an FTS5 table created with the
12485** "detail=none" or "detail=column" option. If the FTS5 table is created
12486** with either "detail=none" or "detail=column" and "content=" option
12487** (i.e. if it is a contentless table), then this API always iterates
12488** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12489**
12490** xPhraseNext()
12491** See xPhraseFirst above.
12492**
12493** xPhraseFirstColumn()
12494** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
12495** and xPhraseNext() APIs described above. The difference is that instead
12496** of iterating through all instances of a phrase in the current row, these
12497** APIs are used to iterate through the set of columns in the current row
12498** that contain one or more instances of a specified phrase. For example:
12499**
12500** Fts5PhraseIter iter;
12501** int iCol;
12502** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12503** iCol>=0;
12504** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12505** ){
12506** // Column iCol contains at least one instance of phrase iPhrase
12507** }
12508**
12509** This API can be quite slow if used with an FTS5 table created with the
12510** "detail=none" option. If the FTS5 table is created with either
12511** "detail=none" "content=" option (i.e. if it is a contentless table),
12512** then this API always iterates through an empty set (all calls to
12513** xPhraseFirstColumn() set iCol to -1).
12514**
12515** The information accessed using this API and its companion
12516** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
12517** (or xInst/xInstCount). The chief advantage of this API is that it is
12518** significantly more efficient than those alternatives when used with
12519** "detail=column" tables.
12520**
12521** xPhraseNextColumn()
12522** See xPhraseFirstColumn above.
12523*/
12524struct Fts5ExtensionApi {
12525 int iVersion; /* Currently always set to 3 */
12526
12527 void *(*xUserData)(Fts5Context*);
12528
12529 int (*xColumnCount)(Fts5Context*);
12530 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
12531 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
12532
12533 int (*xTokenize)(Fts5Context*,
12534 const char *pText, int nText, /* Text to tokenize */
12535 void *pCtx, /* Context passed to xToken() */
12536 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
12537 );
12538
12539 int (*xPhraseCount)(Fts5Context*);
12540 int (*xPhraseSize)(Fts5Context*, int iPhrase);
12541
12542 int (*xInstCount)(Fts5Context*, int *pnInst);
12543 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
12544
12545 sqlite3_int64 (*xRowid)(Fts5Context*);
12546 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
12547 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
12548
12549 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
12550 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
12551 );
12552 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
12553 void *(*xGetAuxdata)(Fts5Context*, int bClear);
12554
12555 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
12556 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
12557
12558 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
12559 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
12560};
12561
12562/*
12563** CUSTOM AUXILIARY FUNCTIONS
12564*************************************************************************/
12565
12566/*************************************************************************
12567** CUSTOM TOKENIZERS
12568**
12569** Applications may also register custom tokenizer types. A tokenizer
12570** is registered by providing fts5 with a populated instance of the
12571** following structure. All structure methods must be defined, setting
12572** any member of the fts5_tokenizer struct to NULL leads to undefined
12573** behaviour. The structure methods are expected to function as follows:
12574**
12575** xCreate:
12576** This function is used to allocate and initialize a tokenizer instance.
12577** A tokenizer instance is required to actually tokenize text.
12578**
12579** The first argument passed to this function is a copy of the (void*)
12580** pointer provided by the application when the fts5_tokenizer object
12581** was registered with FTS5 (the third argument to xCreateTokenizer()).
12582** The second and third arguments are an array of nul-terminated strings
12583** containing the tokenizer arguments, if any, specified following the
12584** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
12585** to create the FTS5 table.
12586**
12587** The final argument is an output variable. If successful, (*ppOut)
12588** should be set to point to the new tokenizer handle and SQLITE_OK
12589** returned. If an error occurs, some value other than SQLITE_OK should
12590** be returned. In this case, fts5 assumes that the final value of *ppOut
12591** is undefined.
12592**
12593** xDelete:
12594** This function is invoked to delete a tokenizer handle previously
12595** allocated using xCreate(). Fts5 guarantees that this function will
12596** be invoked exactly once for each successful call to xCreate().
12597**
12598** xTokenize:
12599** This function is expected to tokenize the nText byte string indicated
12600** by argument pText. pText may or may not be nul-terminated. The first
12601** argument passed to this function is a pointer to an Fts5Tokenizer object
12602** returned by an earlier call to xCreate().
12603**
12604** The second argument indicates the reason that FTS5 is requesting
12605** tokenization of the supplied text. This is always one of the following
12606** four values:
12607**
12608** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
12609** or removed from the FTS table. The tokenizer is being invoked to
12610** determine the set of tokens to add to (or delete from) the
12611** FTS index.
12612**
12613** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
12614** against the FTS index. The tokenizer is being called to tokenize
12615** a bareword or quoted string specified as part of the query.
12616**
12617** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
12618** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
12619** followed by a "*" character, indicating that the last token
12620** returned by the tokenizer will be treated as a token prefix.
12621**
12622** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
12623** satisfy an fts5_api.xTokenize() request made by an auxiliary
12624** function. Or an fts5_api.xColumnSize() request made by the same
12625** on a columnsize=0 database.
12626** </ul>
12627**
12628** For each token in the input string, the supplied callback xToken() must
12629** be invoked. The first argument to it should be a copy of the pointer
12630** passed as the second argument to xTokenize(). The third and fourth
12631** arguments are a pointer to a buffer containing the token text, and the
12632** size of the token in bytes. The 4th and 5th arguments are the byte offsets
12633** of the first byte of and first byte immediately following the text from
12634** which the token is derived within the input.
12635**
12636** The second argument passed to the xToken() callback ("tflags") should
12637** normally be set to 0. The exception is if the tokenizer supports
12638** synonyms. In this case see the discussion below for details.
12639**
12640** FTS5 assumes the xToken() callback is invoked for each token in the
12641** order that they occur within the input text.
12642**
12643** If an xToken() callback returns any value other than SQLITE_OK, then
12644** the tokenization should be abandoned and the xTokenize() method should
12645** immediately return a copy of the xToken() return value. Or, if the
12646** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
12647** if an error occurs with the xTokenize() implementation itself, it
12648** may abandon the tokenization and return any error code other than
12649** SQLITE_OK or SQLITE_DONE.
12650**
12651** SYNONYM SUPPORT
12652**
12653** Custom tokenizers may also support synonyms. Consider a case in which a
12654** user wishes to query for a phrase such as "first place". Using the
12655** built-in tokenizers, the FTS5 query 'first + place' will match instances
12656** of "first place" within the document set, but not alternative forms
12657** such as "1st place". In some applications, it would be better to match
12658** all instances of "first place" or "1st place" regardless of which form
12659** the user specified in the MATCH query text.
12660**
12661** There are several ways to approach this in FTS5:
12662**
12663** <ol><li> By mapping all synonyms to a single token. In this case, the
12664** In the above example, this means that the tokenizer returns the
12665** same token for inputs "first" and "1st". Say that token is in
12666** fact "first", so that when the user inserts the document "I won
12667** 1st place" entries are added to the index for tokens "i", "won",
12668** "first" and "place". If the user then queries for '1st + place',
12669** the tokenizer substitutes "first" for "1st" and the query works
12670** as expected.
12671**
12672** <li> By querying the index for all synonyms of each query term
12673** separately. In this case, when tokenizing query text, the
12674** tokenizer may provide multiple synonyms for a single term
12675** within the document. FTS5 then queries the index for each
12676** synonym individually. For example, faced with the query:
12677**
12678** <codeblock>
12679** ... MATCH 'first place'</codeblock>
12680**
12681** the tokenizer offers both "1st" and "first" as synonyms for the
12682** first token in the MATCH query and FTS5 effectively runs a query
12683** similar to:
12684**
12685** <codeblock>
12686** ... MATCH '(first OR 1st) place'</codeblock>
12687**
12688** except that, for the purposes of auxiliary functions, the query
12689** still appears to contain just two phrases - "(first OR 1st)"
12690** being treated as a single phrase.
12691**
12692** <li> By adding multiple synonyms for a single term to the FTS index.
12693** Using this method, when tokenizing document text, the tokenizer
12694** provides multiple synonyms for each token. So that when a
12695** document such as "I won first place" is tokenized, entries are
12696** added to the FTS index for "i", "won", "first", "1st" and
12697** "place".
12698**
12699** This way, even if the tokenizer does not provide synonyms
12700** when tokenizing query text (it should not - to do so would be
12701** inefficient), it doesn't matter if the user queries for
12702** 'first + place' or '1st + place', as there are entries in the
12703** FTS index corresponding to both forms of the first token.
12704** </ol>
12705**
12706** Whether it is parsing document or query text, any call to xToken that
12707** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
12708** is considered to supply a synonym for the previous token. For example,
12709** when parsing the document "I won first place", a tokenizer that supports
12710** synonyms would call xToken() 5 times, as follows:
12711**
12712** <codeblock>
12713** xToken(pCtx, 0, "i", 1, 0, 1);
12714** xToken(pCtx, 0, "won", 3, 2, 5);
12715** xToken(pCtx, 0, "first", 5, 6, 11);
12716** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
12717** xToken(pCtx, 0, "place", 5, 12, 17);
12718**</codeblock>
12719**
12720** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
12721** xToken() is called. Multiple synonyms may be specified for a single token
12722** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
12723** There is no limit to the number of synonyms that may be provided for a
12724** single token.
12725**
12726** In many cases, method (1) above is the best approach. It does not add
12727** extra data to the FTS index or require FTS5 to query for multiple terms,
12728** so it is efficient in terms of disk space and query speed. However, it
12729** does not support prefix queries very well. If, as suggested above, the
12730** token "first" is substituted for "1st" by the tokenizer, then the query:
12731**
12732** <codeblock>
12733** ... MATCH '1s*'</codeblock>
12734**
12735** will not match documents that contain the token "1st" (as the tokenizer
12736** will probably not map "1s" to any prefix of "first").
12737**
12738** For full prefix support, method (3) may be preferred. In this case,
12739** because the index contains entries for both "first" and "1st", prefix
12740** queries such as 'fi*' or '1s*' will match correctly. However, because
12741** extra entries are added to the FTS index, this method uses more space
12742** within the database.
12743**
12744** Method (2) offers a midpoint between (1) and (3). Using this method,
12745** a query such as '1s*' will match documents that contain the literal
12746** token "1st", but not "first" (assuming the tokenizer is not able to
12747** provide synonyms for prefixes). However, a non-prefix query like '1st'
12748** will match against "1st" and "first". This method does not require
12749** extra disk space, as no extra entries are added to the FTS index.
12750** On the other hand, it may require more CPU cycles to run MATCH queries,
12751** as separate queries of the FTS index are required for each synonym.
12752**
12753** When using methods (2) or (3), it is important that the tokenizer only
12754** provide synonyms when tokenizing document text (method (2)) or query
12755** text (method (3)), not both. Doing so will not cause any errors, but is
12756** inefficient.
12757*/
12758typedef struct Fts5Tokenizer Fts5Tokenizer;
12759typedef struct fts5_tokenizer fts5_tokenizer;
12760struct fts5_tokenizer {
12761 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
12762 void (*xDelete)(Fts5Tokenizer*);
12763 int (*xTokenize)(Fts5Tokenizer*,
12764 void *pCtx,
12765 int flags, /* Mask of FTS5_TOKENIZE_* flags */
12766 const char *pText, int nText,
12767 int (*xToken)(
12768 void *pCtx, /* Copy of 2nd argument to xTokenize() */
12769 int tflags, /* Mask of FTS5_TOKEN_* flags */
12770 const char *pToken, /* Pointer to buffer containing token */
12771 int nToken, /* Size of token in bytes */
12772 int iStart, /* Byte offset of token within input text */
12773 int iEnd /* Byte offset of end of token within input text */
12774 )
12775 );
12776};
12777
12778/* Flags that may be passed as the third argument to xTokenize() */
12779#define FTS5_TOKENIZE_QUERY0x0001 0x0001
12780#define FTS5_TOKENIZE_PREFIX0x0002 0x0002
12781#define FTS5_TOKENIZE_DOCUMENT0x0004 0x0004
12782#define FTS5_TOKENIZE_AUX0x0008 0x0008
12783
12784/* Flags that may be passed by the tokenizer implementation back to FTS5
12785** as the third argument to the supplied xToken callback. */
12786#define FTS5_TOKEN_COLOCATED0x0001 0x0001 /* Same position as prev. token */
12787
12788/*
12789** END OF CUSTOM TOKENIZERS
12790*************************************************************************/
12791
12792/*************************************************************************
12793** FTS5 EXTENSION REGISTRATION API
12794*/
12795typedef struct fts5_api fts5_api;
12796struct fts5_api {
12797 int iVersion; /* Currently always set to 2 */
12798
12799 /* Create a new tokenizer */
12800 int (*xCreateTokenizer)(
12801 fts5_api *pApi,
12802 const char *zName,
12803 void *pContext,
12804 fts5_tokenizer *pTokenizer,
12805 void (*xDestroy)(void*)
12806 );
12807
12808 /* Find an existing tokenizer */
12809 int (*xFindTokenizer)(
12810 fts5_api *pApi,
12811 const char *zName,
12812 void **ppContext,
12813 fts5_tokenizer *pTokenizer
12814 );
12815
12816 /* Create a new auxiliary function */
12817 int (*xCreateFunction)(
12818 fts5_api *pApi,
12819 const char *zName,
12820 void *pContext,
12821 fts5_extension_function xFunction,
12822 void (*xDestroy)(void*)
12823 );
12824};
12825
12826/*
12827** END OF REGISTRATION API
12828*************************************************************************/
12829
12830#if 0
12831} /* end of the 'extern "C"' block */
12832#endif
12833
12834#endif /* _FTS5_H */
12835
12836/******** End of fts5.h *********/
12837
12838/************** End of sqlite3.h *********************************************/
12839/************** Continuing where we left off in sqliteInt.h ******************/
12840
12841/*
12842** Include the configuration header output by 'configure' if we're using the
12843** autoconf-based build
12844*/
12845#if defined(_HAVE_SQLITE_CONFIG_H) && !defined(SQLITECONFIG_H)
12846/* #include "config.h" */
12847#define SQLITECONFIG_H 1
12848#endif
12849
12850/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
12851/************** Begin file sqliteLimit.h *************************************/
12852/*
12853** 2007 May 7
12854**
12855** The author disclaims copyright to this source code. In place of
12856** a legal notice, here is a blessing:
12857**
12858** May you do good and not evil.
12859** May you find forgiveness for yourself and forgive others.
12860** May you share freely, never taking more than you give.
12861**
12862*************************************************************************
12863**
12864** This file defines various limits of what SQLite can process.
12865*/
12866
12867/*
12868** The maximum length of a TEXT or BLOB in bytes. This also
12869** limits the size of a row in a table or index.
12870**
12871** The hard limit is the ability of a 32-bit signed integer
12872** to count the size: 2^31-1 or 2147483647.
12873*/
12874#ifndef SQLITE_MAX_LENGTH1000000000
12875# define SQLITE_MAX_LENGTH1000000000 1000000000
12876#endif
12877
12878/*
12879** This is the maximum number of
12880**
12881** * Columns in a table
12882** * Columns in an index
12883** * Columns in a view
12884** * Terms in the SET clause of an UPDATE statement
12885** * Terms in the result set of a SELECT statement
12886** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
12887** * Terms in the VALUES clause of an INSERT statement
12888**
12889** The hard upper limit here is 32676. Most database people will
12890** tell you that in a well-normalized database, you usually should
12891** not have more than a dozen or so columns in any table. And if
12892** that is the case, there is no point in having more than a few
12893** dozen values in any of the other situations described above.
12894*/
12895#ifndef SQLITE_MAX_COLUMN2000
12896# define SQLITE_MAX_COLUMN2000 2000
12897#endif
12898
12899/*
12900** The maximum length of a single SQL statement in bytes.
12901**
12902** It used to be the case that setting this value to zero would
12903** turn the limit off. That is no longer true. It is not possible
12904** to turn this limit off.
12905*/
12906#ifndef SQLITE_MAX_SQL_LENGTH1000000000
12907# define SQLITE_MAX_SQL_LENGTH1000000000 1000000000
12908#endif
12909
12910/*
12911** The maximum depth of an expression tree. This is limited to
12912** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
12913** want to place more severe limits on the complexity of an
12914** expression.
12915**
12916** A value of 0 used to mean that the limit was not enforced.
12917** But that is no longer true. The limit is now strictly enforced
12918** at all times.
12919*/
12920#ifndef SQLITE_MAX_EXPR_DEPTH1000
12921# define SQLITE_MAX_EXPR_DEPTH1000 1000
12922#endif
12923
12924/*
12925** The maximum number of terms in a compound SELECT statement.
12926** The code generator for compound SELECT statements does one
12927** level of recursion for each term. A stack overflow can result
12928** if the number of terms is too large. In practice, most SQL
12929** never has more than 3 or 4 terms. Use a value of 0 to disable
12930** any limit on the number of terms in a compount SELECT.
12931*/
12932#ifndef SQLITE_MAX_COMPOUND_SELECT500
12933# define SQLITE_MAX_COMPOUND_SELECT500 500
12934#endif
12935
12936/*
12937** The maximum number of opcodes in a VDBE program.
12938** Not currently enforced.
12939*/
12940#ifndef SQLITE_MAX_VDBE_OP250000000
12941# define SQLITE_MAX_VDBE_OP250000000 250000000
12942#endif
12943
12944/*
12945** The maximum number of arguments to an SQL function.
12946*/
12947#ifndef SQLITE_MAX_FUNCTION_ARG127
12948# define SQLITE_MAX_FUNCTION_ARG127 127
12949#endif
12950
12951/*
12952** The suggested maximum number of in-memory pages to use for
12953** the main database table and for temporary tables.
12954**
12955** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
12956** which means the cache size is limited to 2048000 bytes of memory.
12957** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
12958** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
12959*/
12960#ifndef SQLITE_DEFAULT_CACHE_SIZE-2000
12961# define SQLITE_DEFAULT_CACHE_SIZE-2000 -2000
12962#endif
12963
12964/*
12965** The default number of frames to accumulate in the log file before
12966** checkpointing the database in WAL mode.
12967*/
12968#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000
12969# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000 1000
12970#endif
12971
12972/*
12973** The maximum number of attached databases. This must be between 0
12974** and 125. The upper bound of 125 is because the attached databases are
12975** counted using a signed 8-bit integer which has a maximum value of 127
12976** and we have to allow 2 extra counts for the "main" and "temp" databases.
12977*/
12978#ifndef SQLITE_MAX_ATTACHED10
12979# define SQLITE_MAX_ATTACHED10 10
12980#endif
12981
12982
12983/*
12984** The maximum value of a ?nnn wildcard that the parser will accept.
12985*/
12986#ifndef SQLITE_MAX_VARIABLE_NUMBER999
12987# define SQLITE_MAX_VARIABLE_NUMBER999 999
12988#endif
12989
12990/* Maximum page size. The upper bound on this value is 65536. This a limit
12991** imposed by the use of 16-bit offsets within each page.
12992**
12993** Earlier versions of SQLite allowed the user to change this value at
12994** compile time. This is no longer permitted, on the grounds that it creates
12995** a library that is technically incompatible with an SQLite library
12996** compiled with a different limit. If a process operating on a database
12997** with a page-size of 65536 bytes crashes, then an instance of SQLite
12998** compiled with the default page-size limit will not be able to rollback
12999** the aborted transaction. This could lead to database corruption.
13000*/
13001#ifdef SQLITE_MAX_PAGE_SIZE65536
13002# undef SQLITE_MAX_PAGE_SIZE65536
13003#endif
13004#define SQLITE_MAX_PAGE_SIZE65536 65536
13005
13006
13007/*
13008** The default size of a database page.
13009*/
13010#ifndef SQLITE_DEFAULT_PAGE_SIZE4096
13011# define SQLITE_DEFAULT_PAGE_SIZE4096 4096
13012#endif
13013#if SQLITE_DEFAULT_PAGE_SIZE4096>SQLITE_MAX_PAGE_SIZE65536
13014# undef SQLITE_DEFAULT_PAGE_SIZE4096
13015# define SQLITE_DEFAULT_PAGE_SIZE4096 SQLITE_MAX_PAGE_SIZE65536
13016#endif
13017
13018/*
13019** Ordinarily, if no value is explicitly provided, SQLite creates databases
13020** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
13021** device characteristics (sector-size and atomic write() support),
13022** SQLite may choose a larger value. This constant is the maximum value
13023** SQLite will choose on its own.
13024*/
13025#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
13026# define SQLITE_MAX_DEFAULT_PAGE_SIZE8192 8192
13027#endif
13028#if SQLITE_MAX_DEFAULT_PAGE_SIZE8192>SQLITE_MAX_PAGE_SIZE65536
13029# undef SQLITE_MAX_DEFAULT_PAGE_SIZE8192
13030# define SQLITE_MAX_DEFAULT_PAGE_SIZE8192 SQLITE_MAX_PAGE_SIZE65536
13031#endif
13032
13033
13034/*
13035** Maximum number of pages in one database file.
13036**
13037** This is really just the default value for the max_page_count pragma.
13038** This value can be lowered (or raised) at run-time using that the
13039** max_page_count macro.
13040*/
13041#ifndef SQLITE_MAX_PAGE_COUNT1073741823
13042# define SQLITE_MAX_PAGE_COUNT1073741823 1073741823
13043#endif
13044
13045/*
13046** Maximum length (in bytes) of the pattern in a LIKE or GLOB
13047** operator.
13048*/
13049#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
13050# define SQLITE_MAX_LIKE_PATTERN_LENGTH50000 50000
13051#endif
13052
13053/*
13054** Maximum depth of recursion for triggers.
13055**
13056** A value of 1 means that a trigger program will not be able to itself
13057** fire any triggers. A value of 0 means that no trigger programs at all
13058** may be executed.
13059*/
13060#ifndef SQLITE_MAX_TRIGGER_DEPTH1000
13061# define SQLITE_MAX_TRIGGER_DEPTH1000 1000
13062#endif
13063
13064/************** End of sqliteLimit.h *****************************************/
13065/************** Continuing where we left off in sqliteInt.h ******************/
13066
13067/* Disable nuisance warnings on Borland compilers */
13068#if defined(__BORLANDC__)
13069#pragma warn -rch /* unreachable code */
13070#pragma warn -ccc /* Condition is always true or false */
13071#pragma warn -aus /* Assigned value is never used */
13072#pragma warn -csu /* Comparing signed and unsigned */
13073#pragma warn -spa /* Suspicious pointer arithmetic */
13074#endif
13075
13076/*
13077** Include standard header files as necessary
13078*/
13079#ifdef HAVE_STDINT_H
13080#include <stdint.h>
13081#endif
13082#ifdef HAVE_INTTYPES_H
13083#include <inttypes.h>
13084#endif
13085
13086/*
13087** The following macros are used to cast pointers to integers and
13088** integers to pointers. The way you do this varies from one compiler
13089** to the next, so we have developed the following set of #if statements
13090** to generate appropriate macros for a wide range of compilers.
13091**
13092** The correct "ANSI" way to do this is to use the intptr_t type.
13093** Unfortunately, that typedef is not available on all compilers, or
13094** if it is available, it requires an #include of specific headers
13095** that vary from one machine to the next.
13096**
13097** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13098** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
13099** So we have to define the macros in different ways depending on the
13100** compiler.
13101*/
13102#if defined(__PTRDIFF_TYPE__long int) /* This case should work for GCC */
13103# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(__PTRDIFF_TYPE__long int)(X))
13104# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(__PTRDIFF_TYPE__long int)(X))
13105#elif !defined(__GNUC__4) /* Works for compilers other than LLVM */
13106# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)&((char*)0)[X])
13107# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(((char*)X)-(char*)0))
13108#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
13109# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(intptr_t)(X))
13110# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(intptr_t)(X))
13111#else /* Generates a warning - but it always works */
13112# define SQLITE_INT_TO_PTR(X)((void*)(long int)(X)) ((void*)(X))
13113# define SQLITE_PTR_TO_INT(X)((int)(long int)(X)) ((int)(X))
13114#endif
13115
13116/*
13117** A macro to hint to the compiler that a function should not be
13118** inlined.
13119*/
13120#if defined(__GNUC__4)
13121# define SQLITE_NOINLINE__attribute__((noinline)) __attribute__((noinline))
13122#elif defined(_MSC_VER) && _MSC_VER>=1310
13123# define SQLITE_NOINLINE__attribute__((noinline)) __declspec(noinline)
13124#else
13125# define SQLITE_NOINLINE__attribute__((noinline))
13126#endif
13127
13128/*
13129** Make sure that the compiler intrinsics we desire are enabled when
13130** compiling with an appropriate version of MSVC unless prevented by
13131** the SQLITE_DISABLE_INTRINSIC define.
13132*/
13133#if !defined(SQLITE_DISABLE_INTRINSIC)
13134# if defined(_MSC_VER) && _MSC_VER>=1400
13135# if !defined(_WIN32_WCE)
13136# include <intrin.h>
13137# pragma intrinsic(_byteswap_ushort)
13138# pragma intrinsic(_byteswap_ulong)
13139# pragma intrinsic(_byteswap_uint64)
13140# pragma intrinsic(_ReadWriteBarrier)
13141# else
13142# include <cmnintrin.h>
13143# endif
13144# endif
13145#endif
13146
13147/*
13148** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
13149** 0 means mutexes are permanently disable and the library is never
13150** threadsafe. 1 means the library is serialized which is the highest
13151** level of threadsafety. 2 means the library is multithreaded - multiple
13152** threads can use SQLite as long as no two threads try to use the same
13153** database connection at the same time.
13154**
13155** Older versions of SQLite used an optional THREADSAFE macro.
13156** We support that for legacy.
13157**
13158** To ensure that the correct value of "THREADSAFE" is reported when querying
13159** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13160** logic is partially replicated in ctime.c. If it is updated here, it should
13161** also be updated there.
13162*/
13163#if !defined(SQLITE_THREADSAFE1)
13164# if defined(THREADSAFE)
13165# define SQLITE_THREADSAFE1 THREADSAFE
13166# else
13167# define SQLITE_THREADSAFE1 1 /* IMP: R-07272-22309 */
13168# endif
13169#endif
13170
13171/*
13172** Powersafe overwrite is on by default. But can be turned off using
13173** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13174*/
13175#ifndef SQLITE_POWERSAFE_OVERWRITE1
13176# define SQLITE_POWERSAFE_OVERWRITE1 1
13177#endif
13178
13179/*
13180** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13181** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
13182** which case memory allocation statistics are disabled by default.
13183*/
13184#if !defined(SQLITE_DEFAULT_MEMSTATUS1)
13185# define SQLITE_DEFAULT_MEMSTATUS1 1
13186#endif
13187
13188/*
13189** Exactly one of the following macros must be defined in order to
13190** specify which memory allocation subsystem to use.
13191**
13192** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
13193** SQLITE_WIN32_MALLOC // Use Win32 native heap API
13194** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
13195** SQLITE_MEMDEBUG // Debugging version of system malloc()
13196**
13197** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
13198** assert() macro is enabled, each call into the Win32 native heap subsystem
13199** will cause HeapValidate to be called. If heap validation should fail, an
13200** assertion will be triggered.
13201**
13202** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
13203** the default.
13204*/
13205#if defined(SQLITE_SYSTEM_MALLOC1) \
13206 + defined(SQLITE_WIN32_MALLOC) \
13207 + defined(SQLITE_ZERO_MALLOC) \
13208 + defined(SQLITE_MEMDEBUG)>1
13209# error "Two or more of the following compile-time configuration options\
13210 are defined but at most one is allowed:\
13211 SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
13212 SQLITE_ZERO_MALLOC"
13213#endif
13214#if defined(SQLITE_SYSTEM_MALLOC1) \
13215 + defined(SQLITE_WIN32_MALLOC) \
13216 + defined(SQLITE_ZERO_MALLOC) \
13217 + defined(SQLITE_MEMDEBUG)==0
13218# define SQLITE_SYSTEM_MALLOC1 1
13219#endif
13220
13221/*
13222** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
13223** sizes of memory allocations below this value where possible.
13224*/
13225#if !defined(SQLITE_MALLOC_SOFT_LIMIT1024)
13226# define SQLITE_MALLOC_SOFT_LIMIT1024 1024
13227#endif
13228
13229/*
13230** We need to define _XOPEN_SOURCE as follows in order to enable
13231** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
13232** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
13233** it.
13234*/
13235#if !defined(_XOPEN_SOURCE700) && !defined(__DARWIN__) && !defined(__APPLE__)
13236# define _XOPEN_SOURCE700 600
13237#endif
13238
13239/*
13240** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
13241** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
13242** make it true by defining or undefining NDEBUG.
13243**
13244** Setting NDEBUG makes the code smaller and faster by disabling the
13245** assert() statements in the code. So we want the default action
13246** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
13247** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13248** feature.
13249*/
13250#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
13251# define NDEBUG1 1
13252#endif
13253#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
13254# undef NDEBUG1
13255#endif
13256
13257/*
13258** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
13259*/
13260#if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
13261# define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
13262#endif
13263
13264/*
13265** The testcase() macro is used to aid in coverage testing. When
13266** doing coverage testing, the condition inside the argument to
13267** testcase() must be evaluated both true and false in order to
13268** get full branch coverage. The testcase() macro is inserted
13269** to help ensure adequate test coverage in places where simple
13270** condition/decision coverage is inadequate. For example, testcase()
13271** can be used to make sure boundary values are tested. For
13272** bitmask tests, testcase() can be used to make sure each bit
13273** is significant and used at least once. On switch statements
13274** where multiple cases go to the same block of code, testcase()
13275** can insure that all cases are evaluated.
13276**
13277*/
13278#ifdef SQLITE_COVERAGE_TEST
13279SQLITE_PRIVATEstatic void sqlite3Coverage(int);
13280# define testcase(X) if( X ){ sqlite3Coverage(__LINE__13280); }
13281#else
13282# define testcase(X)
13283#endif
13284
13285/*
13286** The TESTONLY macro is used to enclose variable declarations or
13287** other bits of code that are needed to support the arguments
13288** within testcase() and assert() macros.
13289*/
13290#if !defined(NDEBUG1) || defined(SQLITE_COVERAGE_TEST)
13291# define TESTONLY(X) X
13292#else
13293# define TESTONLY(X)
13294#endif
13295
13296/*
13297** Sometimes we need a small amount of code such as a variable initialization
13298** to setup for a later assert() statement. We do not want this code to
13299** appear when assert() is disabled. The following macro is therefore
13300** used to contain that setup code. The "VVA" acronym stands for
13301** "Verification, Validation, and Accreditation". In other words, the
13302** code within VVA_ONLY() will only run during verification processes.
13303*/
13304#ifndef NDEBUG1
13305# define VVA_ONLY(X) X
13306#else
13307# define VVA_ONLY(X)
13308#endif
13309
13310/*
13311** The ALWAYS and NEVER macros surround boolean expressions which
13312** are intended to always be true or false, respectively. Such
13313** expressions could be omitted from the code completely. But they
13314** are included in a few cases in order to enhance the resilience
13315** of SQLite to unexpected behavior - to make the code "self-healing"
13316** or "ductile" rather than being "brittle" and crashing at the first
13317** hint of unplanned behavior.
13318**
13319** In other words, ALWAYS and NEVER are added for defensive code.
13320**
13321** When doing coverage testing ALWAYS and NEVER are hard-coded to
13322** be true and false so that the unreachable code they specify will
13323** not be counted as untested code.
13324*/
13325#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
13326# define ALWAYS(X)(X) (1)
13327# define NEVER(X)(X) (0)
13328#elif !defined(NDEBUG1)
13329# define ALWAYS(X)(X) ((X)?1:(assert(0)((void) (0)),0))
13330# define NEVER(X)(X) ((X)?(assert(0)((void) (0)),1):0)
13331#else
13332# define ALWAYS(X)(X) (X)
13333# define NEVER(X)(X) (X)
13334#endif
13335
13336/*
13337** Some conditionals are optimizations only. In other words, if the
13338** conditionals are replaced with a constant 1 (true) or 0 (false) then
13339** the correct answer is still obtained, though perhaps not as quickly.
13340**
13341** The following macros mark these optimizations conditionals.
13342*/
13343#if defined(SQLITE_MUTATION_TEST)
13344# define OK_IF_ALWAYS_TRUE(X)(X) (1)
13345# define OK_IF_ALWAYS_FALSE(X)(X) (0)
13346#else
13347# define OK_IF_ALWAYS_TRUE(X)(X) (X)
13348# define OK_IF_ALWAYS_FALSE(X)(X) (X)
13349#endif
13350
13351/*
13352** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
13353** defined. We need to defend against those failures when testing with
13354** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
13355** during a normal build. The following macro can be used to disable tests
13356** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
13357*/
13358#if defined(SQLITE_TEST_REALLOC_STRESS)
13359# define ONLY_IF_REALLOC_STRESS(X)(0) (X)
13360#elif !defined(NDEBUG1)
13361# define ONLY_IF_REALLOC_STRESS(X)(0) ((X)?(assert(0)((void) (0)),1):0)
13362#else
13363# define ONLY_IF_REALLOC_STRESS(X)(0) (0)
13364#endif
13365
13366/*
13367** Declarations used for tracing the operating system interfaces.
13368*/
13369#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
13370 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN0)
13371 extern int sqlite3OSTrace;
13372# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
13373# define SQLITE_HAVE_OS_TRACE
13374#else
13375# define OSTRACE(X)
13376# undef SQLITE_HAVE_OS_TRACE
13377#endif
13378
13379/*
13380** Is the sqlite3ErrName() function needed in the build? Currently,
13381** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
13382** OSTRACE is enabled), and by several "test*.c" files (which are
13383** compiled using SQLITE_TEST).
13384*/
13385#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
13386 (defined(SQLITE_DEBUG) && SQLITE_OS_WIN0)
13387# define SQLITE_NEED_ERR_NAME
13388#else
13389# undef SQLITE_NEED_ERR_NAME
13390#endif
13391
13392/*
13393** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
13394*/
13395#ifdef SQLITE_OMIT_EXPLAIN
13396# undef SQLITE_ENABLE_EXPLAIN_COMMENTS
13397#endif
13398
13399/*
13400** Return true (non-zero) if the input is an integer that is too large
13401** to fit in 32-bits. This macro is used inside of various testcase()
13402** macros to verify that we have tested SQLite for large-file support.
13403*/
13404#define IS_BIG_INT(X)(((X)&~(i64)0xffffffff)!=0) (((X)&~(i64)0xffffffff)!=0)
13405
13406/*
13407** The macro unlikely() is a hint that surrounds a boolean
13408** expression that is usually false. Macro likely() surrounds
13409** a boolean expression that is usually true. These hints could,
13410** in theory, be used by the compiler to generate better code, but
13411** currently they are just comments for human readers.
13412*/
13413#define likely(X)(X) (X)
13414#define unlikely(X)(X) (X)
13415
13416/************** Include hash.h in the middle of sqliteInt.h ******************/
13417/************** Begin file hash.h ********************************************/
13418/*
13419** 2001 September 22
13420**
13421** The author disclaims copyright to this source code. In place of
13422** a legal notice, here is a blessing:
13423**
13424** May you do good and not evil.
13425** May you find forgiveness for yourself and forgive others.
13426** May you share freely, never taking more than you give.
13427**
13428*************************************************************************
13429** This is the header file for the generic hash-table implementation
13430** used in SQLite.
13431*/
13432#ifndef SQLITE_HASH_H
13433#define SQLITE_HASH_H
13434
13435/* Forward declarations of structures. */
13436typedef struct Hash Hash;
13437typedef struct HashElem HashElem;
13438
13439/* A complete hash table is an instance of the following structure.
13440** The internals of this structure are intended to be opaque -- client
13441** code should not attempt to access or modify the fields of this structure
13442** directly. Change this structure only by using the routines below.
13443** However, some of the "procedures" and "functions" for modifying and
13444** accessing this structure are really macros, so we can't really make
13445** this structure opaque.
13446**
13447** All elements of the hash table are on a single doubly-linked list.
13448** Hash.first points to the head of this list.
13449**
13450** There are Hash.htsize buckets. Each bucket points to a spot in
13451** the global doubly-linked list. The contents of the bucket are the
13452** element pointed to plus the next _ht.count-1 elements in the list.
13453**
13454** Hash.htsize and Hash.ht may be zero. In that case lookup is done
13455** by a linear search of the global list. For small tables, the
13456** Hash.ht table is never allocated because if there are few elements
13457** in the table, it is faster to do a linear search than to manage
13458** the hash table.
13459*/
13460struct Hash {
13461 unsigned int htsize; /* Number of buckets in the hash table */
13462 unsigned int count; /* Number of entries in this table */
13463 HashElem *first; /* The first element of the array */
13464 struct _ht { /* the hash table */
13465 unsigned int count; /* Number of entries with this hash */
13466 HashElem *chain; /* Pointer to first entry with this hash */
13467 } *ht;
13468};
13469
13470/* Each element in the hash table is an instance of the following
13471** structure. All elements are stored on a single doubly-linked list.
13472**
13473** Again, this structure is intended to be opaque, but it can't really
13474** be opaque because it is used by macros.
13475*/
13476struct HashElem {
13477 HashElem *next, *prev; /* Next and previous elements in the table */
13478 void *data; /* Data associated with this element */
13479 const char *pKey; /* Key associated with this element */
13480};
13481
13482/*
13483** Access routines. To delete, insert a NULL pointer.
13484*/
13485SQLITE_PRIVATEstatic void sqlite3HashInit(Hash*);
13486SQLITE_PRIVATEstatic void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
13487SQLITE_PRIVATEstatic void *sqlite3HashFind(const Hash*, const char *pKey);
13488SQLITE_PRIVATEstatic void sqlite3HashClear(Hash*);
13489
13490/*
13491** Macros for looping over all elements of a hash table. The idiom is
13492** like this:
13493**
13494** Hash h;
13495** HashElem *p;
13496** ...
13497** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
13498** SomeStructure *pData = sqliteHashData(p);
13499** // do something with pData
13500** }
13501*/
13502#define sqliteHashFirst(H)((H)->first) ((H)->first)
13503#define sqliteHashNext(E)((E)->next) ((E)->next)
13504#define sqliteHashData(E)((E)->data) ((E)->data)
13505/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
13506/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
13507
13508/*
13509** Number of entries in a hash table
13510*/
13511/* #define sqliteHashCount(H) ((H)->count) // NOT USED */
13512
13513#endif /* SQLITE_HASH_H */
13514
13515/************** End of hash.h ************************************************/
13516/************** Continuing where we left off in sqliteInt.h ******************/
13517/************** Include parse.h in the middle of sqliteInt.h *****************/
13518/************** Begin file parse.h *******************************************/
13519#define TK_SEMI1 1
13520#define TK_EXPLAIN2 2
13521#define TK_QUERY3 3
13522#define TK_PLAN4 4
13523#define TK_BEGIN5 5
13524#define TK_TRANSACTION6 6
13525#define TK_DEFERRED7 7
13526#define TK_IMMEDIATE8 8
13527#define TK_EXCLUSIVE9 9
13528#define TK_COMMIT10 10
13529#define TK_END11 11
13530#define TK_ROLLBACK12 12
13531#define TK_SAVEPOINT13 13
13532#define TK_RELEASE14 14
13533#define TK_TO15 15
13534#define TK_TABLE16 16
13535#define TK_CREATE17 17
13536#define TK_IF18 18
13537#define TK_NOT19 19
13538#define TK_EXISTS20 20
13539#define TK_TEMP21 21
13540#define TK_LP22 22
13541#define TK_RP23 23
13542#define TK_AS24 24
13543#define TK_WITHOUT25 25
13544#define TK_COMMA26 26
13545#define TK_ABORT27 27
13546#define TK_ACTION28 28
13547#define TK_AFTER29 29
13548#define TK_ANALYZE30 30
13549#define TK_ASC31 31
13550#define TK_ATTACH32 32
13551#define TK_BEFORE33 33
13552#define TK_BY34 34
13553#define TK_CASCADE35 35
13554#define TK_CAST36 36
13555#define TK_CONFLICT37 37
13556#define TK_DATABASE38 38
13557#define TK_DESC39 39
13558#define TK_DETACH40 40
13559#define TK_EACH41 41
13560#define TK_FAIL42 42
13561#define TK_OR43 43
13562#define TK_AND44 44
13563#define TK_IS45 45
13564#define TK_MATCH46 46
13565#define TK_LIKE_KW47 47
13566#define TK_BETWEEN48 48
13567#define TK_IN49 49
13568#define TK_ISNULL50 50
13569#define TK_NOTNULL51 51
13570#define TK_NE52 52
13571#define TK_EQ53 53
13572#define TK_GT54 54
13573#define TK_LE55 55
13574#define TK_LT56 56
13575#define TK_GE57 57
13576#define TK_ESCAPE58 58
13577#define TK_ID59 59
13578#define TK_COLUMNKW60 60
13579#define TK_DO61 61
13580#define TK_FOR62 62
13581#define TK_IGNORE63 63
13582#define TK_INITIALLY64 64
13583#define TK_INSTEAD65 65
13584#define TK_NO66 66
13585#define TK_KEY67 67
13586#define TK_OF68 68
13587#define TK_OFFSET69 69
13588#define TK_PRAGMA70 70
13589#define TK_RAISE71 71
13590#define TK_RECURSIVE72 72
13591#define TK_REPLACE73 73
13592#define TK_RESTRICT74 74
13593#define TK_ROW75 75
13594#define TK_ROWS76 76
13595#define TK_TRIGGER77 77
13596#define TK_VACUUM78 78
13597#define TK_VIEW79 79
13598#define TK_VIRTUAL80 80
13599#define TK_WITH81 81
13600#define TK_CURRENT82 82
13601#define TK_FOLLOWING83 83
13602#define TK_PARTITION84 84
13603#define TK_PRECEDING85 85
13604#define TK_RANGE86 86
13605#define TK_UNBOUNDED87 87
13606#define TK_EXCLUDE88 88
13607#define TK_GROUPS89 89
13608#define TK_OTHERS90 90
13609#define TK_TIES91 91
13610#define TK_REINDEX92 92
13611#define TK_RENAME93 93
13612#define TK_CTIME_KW94 94
13613#define TK_ANY95 95
13614#define TK_BITAND96 96
13615#define TK_BITOR97 97
13616#define TK_LSHIFT98 98
13617#define TK_RSHIFT99 99
13618#define TK_PLUS100 100
13619#define TK_MINUS101 101
13620#define TK_STAR102 102
13621#define TK_SLASH103 103
13622#define TK_REM104 104
13623#define TK_CONCAT105 105
13624#define TK_COLLATE106 106
13625#define TK_BITNOT107 107
13626#define TK_ON108 108
13627#define TK_INDEXED109 109
13628#define TK_STRING110 110
13629#define TK_JOIN_KW111 111
13630#define TK_CONSTRAINT112 112
13631#define TK_DEFAULT113 113
13632#define TK_NULL114 114
13633#define TK_PRIMARY115 115
13634#define TK_UNIQUE116 116
13635#define TK_CHECK117 117
13636#define TK_REFERENCES118 118
13637#define TK_AUTOINCR119 119
13638#define TK_INSERT120 120
13639#define TK_DELETE121 121
13640#define TK_UPDATE122 122
13641#define TK_SET123 123
13642#define TK_DEFERRABLE124 124
13643#define TK_FOREIGN125 125
13644#define TK_DROP126 126
13645#define TK_UNION127 127
13646#define TK_ALL128 128
13647#define TK_EXCEPT129 129
13648#define TK_INTERSECT130 130
13649#define TK_SELECT131 131
13650#define TK_VALUES132 132
13651#define TK_DISTINCT133 133
13652#define TK_DOT134 134
13653#define TK_FROM135 135
13654#define TK_JOIN136 136
13655#define TK_USING137 137
13656#define TK_ORDER138 138
13657#define TK_GROUP139 139
13658#define TK_HAVING140 140
13659#define TK_LIMIT141 141
13660#define TK_WHERE142 142
13661#define TK_INTO143 143
13662#define TK_NOTHING144 144
13663#define TK_FLOAT145 145
13664#define TK_BLOB146 146
13665#define TK_INTEGER147 147
13666#define TK_VARIABLE148 148
13667#define TK_CASE149 149
13668#define TK_WHEN150 150
13669#define TK_THEN151 151
13670#define TK_ELSE152 152
13671#define TK_INDEX153 153
13672#define TK_ALTER154 154
13673#define TK_ADD155 155
13674#define TK_WINDOW156 156
13675#define TK_OVER157 157
13676#define TK_FILTER158 158
13677#define TK_TRUEFALSE159 159
13678#define TK_ISNOT160 160
13679#define TK_FUNCTION161 161
13680#define TK_COLUMN162 162
13681#define TK_AGG_FUNCTION163 163
13682#define TK_AGG_COLUMN164 164
13683#define TK_UMINUS165 165
13684#define TK_UPLUS166 166
13685#define TK_TRUTH167 167
13686#define TK_REGISTER168 168
13687#define TK_VECTOR169 169
13688#define TK_SELECT_COLUMN170 170
13689#define TK_IF_NULL_ROW171 171
13690#define TK_ASTERISK172 172
13691#define TK_SPAN173 173
13692#define TK_SPACE174 174
13693#define TK_ILLEGAL175 175
13694
13695/************** End of parse.h ***********************************************/
13696/************** Continuing where we left off in sqliteInt.h ******************/
13697#include <stdio.h>
13698#include <stdlib.h>
13699#include <string.h>
13700#include <assert.h>
13701#include <stddef.h>
13702
13703/*
13704** Use a macro to replace memcpy() if compiled with SQLITE_INLINE_MEMCPY.
13705** This allows better measurements of where memcpy() is used when running
13706** cachegrind. But this macro version of memcpy() is very slow so it
13707** should not be used in production. This is a performance measurement
13708** hack only.
13709*/
13710#ifdef SQLITE_INLINE_MEMCPY
13711# define memcpy(D,S,N) {char*xxd=(char*)(D);const char*xxs=(const char*)(S);\
13712 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
13713#endif
13714
13715/*
13716** If compiling for a processor that lacks floating point support,
13717** substitute integer for floating-point
13718*/
13719#ifdef SQLITE_OMIT_FLOATING_POINT
13720# define double sqlite_int64
13721# define float sqlite_int64
13722# define LONGDOUBLE_TYPElong double sqlite_int64
13723# ifndef SQLITE_BIG_DBL(1e99)
13724# define SQLITE_BIG_DBL(1e99) (((sqlite3_int64)1)<<50)
13725# endif
13726# define SQLITE_OMIT_DATETIME_FUNCS 1
13727# define SQLITE_OMIT_TRACE 1
13728# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
13729# undef SQLITE_HAVE_ISNAN
13730#endif
13731#ifndef SQLITE_BIG_DBL(1e99)
13732# define SQLITE_BIG_DBL(1e99) (1e99)
13733#endif
13734
13735/*
13736** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
13737** afterward. Having this macro allows us to cause the C compiler
13738** to omit code used by TEMP tables without messy #ifndef statements.
13739*/
13740#ifdef SQLITE_OMIT_TEMPDB
13741#define OMIT_TEMPDB0 1
13742#else
13743#define OMIT_TEMPDB0 0
13744#endif
13745
13746/*
13747** The "file format" number is an integer that is incremented whenever
13748** the VDBE-level file format changes. The following macros define the
13749** the default file format for new databases and the maximum file format
13750** that the library can read.
13751*/
13752#define SQLITE_MAX_FILE_FORMAT4 4
13753#ifndef SQLITE_DEFAULT_FILE_FORMAT4
13754# define SQLITE_DEFAULT_FILE_FORMAT4 4
13755#endif
13756
13757/*
13758** Determine whether triggers are recursive by default. This can be
13759** changed at run-time using a pragma.
13760*/
13761#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
13762# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS0 0
13763#endif
13764
13765/*
13766** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
13767** on the command-line
13768*/
13769#ifndef SQLITE_TEMP_STORE1
13770# define SQLITE_TEMP_STORE1 1
13771#endif
13772
13773/*
13774** If no value has been provided for SQLITE_MAX_WORKER_THREADS, or if
13775** SQLITE_TEMP_STORE is set to 3 (never use temporary files), set it
13776** to zero.
13777*/
13778#if SQLITE_TEMP_STORE1==3 || SQLITE_THREADSAFE1==0
13779# undef SQLITE_MAX_WORKER_THREADS8
13780# define SQLITE_MAX_WORKER_THREADS8 0
13781#endif
13782#ifndef SQLITE_MAX_WORKER_THREADS8
13783# define SQLITE_MAX_WORKER_THREADS8 8
13784#endif
13785#ifndef SQLITE_DEFAULT_WORKER_THREADS0
13786# define SQLITE_DEFAULT_WORKER_THREADS0 0
13787#endif
13788#if SQLITE_DEFAULT_WORKER_THREADS0>SQLITE_MAX_WORKER_THREADS8
13789# undef SQLITE_MAX_WORKER_THREADS8
13790# define SQLITE_MAX_WORKER_THREADS8 SQLITE_DEFAULT_WORKER_THREADS0
13791#endif
13792
13793/*
13794** The default initial allocation for the pagecache when using separate
13795** pagecaches for each database connection. A positive number is the
13796** number of pages. A negative number N translations means that a buffer
13797** of -1024*N bytes is allocated and used for as many pages as it will hold.
13798**
13799** The default value of "20" was choosen to minimize the run-time of the
13800** speedtest1 test program with options: --shrink-memory --reprepare
13801*/
13802#ifndef SQLITE_DEFAULT_PCACHE_INITSZ20
13803# define SQLITE_DEFAULT_PCACHE_INITSZ20 20
13804#endif
13805
13806/*
13807** Default value for the SQLITE_CONFIG_SORTERREF_SIZE option.
13808*/
13809#ifndef SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff
13810# define SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff 0x7fffffff
13811#endif
13812
13813/*
13814** The compile-time options SQLITE_MMAP_READWRITE and
13815** SQLITE_ENABLE_BATCH_ATOMIC_WRITE are not compatible with one another.
13816** You must choose one or the other (or neither) but not both.
13817*/
13818#if defined(SQLITE_MMAP_READWRITE) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
13819#error Cannot use both SQLITE_MMAP_READWRITE and SQLITE_ENABLE_BATCH_ATOMIC_WRITE
13820#endif
13821
13822/*
13823** GCC does not define the offsetof() macro so we'll have to do it
13824** ourselves.
13825*/
13826#ifndef offsetof
13827#define offsetof(STRUCTURE,FIELD)__builtin_offsetof(STRUCTURE, FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
13828#endif
13829
13830/*
13831** Macros to compute minimum and maximum of two numbers.
13832*/
13833#ifndef MIN
13834# define MIN(A,B)((A)<(B)?(A):(B)) ((A)<(B)?(A):(B))
13835#endif
13836#ifndef MAX
13837# define MAX(A,B)((A)>(B)?(A):(B)) ((A)>(B)?(A):(B))
13838#endif
13839
13840/*
13841** Swap two objects of type TYPE.
13842*/
13843#define SWAP(TYPE,A,B){TYPE t=A; A=B; B=t;} {TYPE t=A; A=B; B=t;}
13844
13845/*
13846** Check to see if this machine uses EBCDIC. (Yes, believe it or
13847** not, there are still machines out there that use EBCDIC.)
13848*/
13849#if 'A' == '\301'
13850# define SQLITE_EBCDIC 1
13851#else
13852# define SQLITE_ASCII1 1
13853#endif
13854
13855/*
13856** Integers of known sizes. These typedefs might change for architectures
13857** where the sizes very. Preprocessor macros are available so that the
13858** types can be conveniently redefined at compile-type. Like this:
13859**
13860** cc '-DUINTPTR_TYPE=long long int' ...
13861*/
13862#ifndef UINT32_TYPEunsigned int
13863# ifdef HAVE_UINT32_T
13864# define UINT32_TYPEunsigned int uint32_t
13865# else
13866# define UINT32_TYPEunsigned int unsigned int
13867# endif
13868#endif
13869#ifndef UINT16_TYPEunsigned short int
13870# ifdef HAVE_UINT16_T
13871# define UINT16_TYPEunsigned short int uint16_t
13872# else
13873# define UINT16_TYPEunsigned short int unsigned short int
13874# endif
13875#endif
13876#ifndef INT16_TYPEshort int
13877# ifdef HAVE_INT16_T
13878# define INT16_TYPEshort int int16_t
13879# else
13880# define INT16_TYPEshort int short int
13881# endif
13882#endif
13883#ifndef UINT8_TYPEunsigned char
13884# ifdef HAVE_UINT8_T
13885# define UINT8_TYPEunsigned char uint8_t
13886# else
13887# define UINT8_TYPEunsigned char unsigned char
13888# endif
13889#endif
13890#ifndef INT8_TYPEsigned char
13891# ifdef HAVE_INT8_T
13892# define INT8_TYPEsigned char int8_t
13893# else
13894# define INT8_TYPEsigned char signed char
13895# endif
13896#endif
13897#ifndef LONGDOUBLE_TYPElong double
13898# define LONGDOUBLE_TYPElong double long double
13899#endif
13900typedef sqlite_int64 i64; /* 8-byte signed integer */
13901typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
13902typedef UINT32_TYPEunsigned int u32; /* 4-byte unsigned integer */
13903typedef UINT16_TYPEunsigned short int u16; /* 2-byte unsigned integer */
13904typedef INT16_TYPEshort int i16; /* 2-byte signed integer */
13905typedef UINT8_TYPEunsigned char u8; /* 1-byte unsigned integer */
13906typedef INT8_TYPEsigned char i8; /* 1-byte signed integer */
13907
13908/*
13909** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
13910** that can be stored in a u32 without loss of data. The value
13911** is 0x00000000ffffffff. But because of quirks of some compilers, we
13912** have to specify the value in the less intuitive manner shown:
13913*/
13914#define SQLITE_MAX_U32((((u64)1)<<32)-1) ((((u64)1)<<32)-1)
13915
13916/*
13917** The datatype used to store estimates of the number of rows in a
13918** table or index. This is an unsigned integer type. For 99.9% of
13919** the world, a 32-bit integer is sufficient. But a 64-bit integer
13920** can be used at compile-time if desired.
13921*/
13922#ifdef SQLITE_64BIT_STATS
13923 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
13924#else
13925 typedef u32 tRowcnt; /* 32-bit is the default */
13926#endif
13927
13928/*
13929** Estimated quantities used for query planning are stored as 16-bit
13930** logarithms. For quantity X, the value stored is 10*log2(X). This
13931** gives a possible range of values of approximately 1.0e986 to 1e-986.
13932** But the allowed values are "grainy". Not every value is representable.
13933** For example, quantities 16 and 17 are both represented by a LogEst
13934** of 40. However, since LogEst quantities are suppose to be estimates,
13935** not exact values, this imprecision is not a problem.
13936**
13937** "LogEst" is short for "Logarithmic Estimate".
13938**
13939** Examples:
13940** 1 -> 0 20 -> 43 10000 -> 132
13941** 2 -> 10 25 -> 46 25000 -> 146
13942** 3 -> 16 100 -> 66 1000000 -> 199
13943** 4 -> 20 1000 -> 99 1048576 -> 200
13944** 10 -> 33 1024 -> 100 4294967296 -> 320
13945**
13946** The LogEst can be negative to indicate fractional values.
13947** Examples:
13948**
13949** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
13950*/
13951typedef INT16_TYPEshort int LogEst;
13952
13953/*
13954** Set the SQLITE_PTRSIZE macro to the number of bytes in a pointer
13955*/
13956#ifndef SQLITE_PTRSIZE8
13957# if defined(__SIZEOF_POINTER__8)
13958# define SQLITE_PTRSIZE8 __SIZEOF_POINTER__8
13959# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
13960 defined(_M_ARM) || defined(__arm__) || defined(__x86) || \
13961 (defined(__TOS_AIX__) && !defined(__64BIT__))
13962# define SQLITE_PTRSIZE8 4
13963# else
13964# define SQLITE_PTRSIZE8 8
13965# endif
13966#endif
13967
13968/* The uptr type is an unsigned integer large enough to hold a pointer
13969*/
13970#if defined(HAVE_STDINT_H)
13971 typedef uintptr_t uptr;
13972#elif SQLITE_PTRSIZE8==4
13973 typedef u32 uptr;
13974#else
13975 typedef u64 uptr;
13976#endif
13977
13978/*
13979** The SQLITE_WITHIN(P,S,E) macro checks to see if pointer P points to
13980** something between S (inclusive) and E (exclusive).
13981**
13982** In other words, S is a buffer and E is a pointer to the first byte after
13983** the end of buffer S. This macro returns true if P points to something
13984** contained within the buffer S.
13985*/
13986#define SQLITE_WITHIN(P,S,E)(((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E))) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
13987
13988
13989/*
13990** Macros to determine whether the machine is big or little endian,
13991** and whether or not that determination is run-time or compile-time.
13992**
13993** For best performance, an attempt is made to guess at the byte-order
13994** using C-preprocessor macros. If that is unsuccessful, or if
13995** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
13996** at run-time.
13997*/
13998#ifndef SQLITE_BYTEORDER1234
13999# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14000 defined(__x86_641) || defined(__x86_64__1) || defined(_M_X64) || \
14001 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
14002 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
14003# define SQLITE_BYTEORDER1234 1234
14004# elif defined(sparc) || defined(__ppc__) || \
14005 defined(__ARMEB__) || defined(__AARCH64EB__)
14006# define SQLITE_BYTEORDER1234 4321
14007# else
14008# define SQLITE_BYTEORDER1234 0
14009# endif
14010#endif
14011#if SQLITE_BYTEORDER1234==4321
14012# define SQLITE_BIGENDIAN0 1
14013# define SQLITE_LITTLEENDIAN1 0
14014# define SQLITE_UTF16NATIVE2 SQLITE_UTF16BE3
14015#elif SQLITE_BYTEORDER1234==1234
14016# define SQLITE_BIGENDIAN0 0
14017# define SQLITE_LITTLEENDIAN1 1
14018# define SQLITE_UTF16NATIVE2 SQLITE_UTF16LE2
14019#else
14020# ifdef SQLITE_AMALGAMATION1
14021 const int sqlite3one = 1;
14022# else
14023 extern const int sqlite3one;
14024# endif
14025# define SQLITE_BIGENDIAN0 (*(char *)(&sqlite3one)==0)
14026# define SQLITE_LITTLEENDIAN1 (*(char *)(&sqlite3one)==1)
14027# define SQLITE_UTF16NATIVE2 (SQLITE_BIGENDIAN0?SQLITE_UTF16BE3:SQLITE_UTF16LE2)
14028#endif
14029
14030/*
14031** Constants for the largest and smallest possible 64-bit signed integers.
14032** These macros are designed to work correctly on both 32-bit and 64-bit
14033** compilers.
14034*/
14035#define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
14036#define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
14037
14038/*
14039** Round up a number to the next larger multiple of 8. This is used
14040** to force 8-byte alignment on 64-bit architectures.
14041*/
14042#define ROUND8(x)(((x)+7)&~7) (((x)+7)&~7)
14043
14044/*
14045** Round down to the nearest multiple of 8
14046*/
14047#define ROUNDDOWN8(x)((x)&~7) ((x)&~7)
14048
14049/*
14050** Assert that the pointer X is aligned to an 8-byte boundary. This
14051** macro is used only within assert() to verify that the code gets
14052** all alignment restrictions correct.
14053**
14054** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
14055** underlying malloc() implementation might return us 4-byte aligned
14056** pointers. In that case, only verify 4-byte alignment.
14057*/
14058#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
14059# define EIGHT_BYTE_ALIGNMENT(X)((((char*)(X) - (char*)0)&7)==0) ((((char*)(X) - (char*)0)&3)==0)
14060#else
14061# define EIGHT_BYTE_ALIGNMENT(X)((((char*)(X) - (char*)0)&7)==0) ((((char*)(X) - (char*)0)&7)==0)
14062#endif
14063
14064/*
14065** Disable MMAP on platforms where it is known to not work
14066*/
14067#if defined(__OpenBSD__) || defined(__QNXNTO__)
14068# undef SQLITE_MAX_MMAP_SIZE0x7fff0000
14069# define SQLITE_MAX_MMAP_SIZE0x7fff0000 0
14070#endif
14071
14072/*
14073** Default maximum size of memory used by memory-mapped I/O in the VFS
14074*/
14075#ifdef __APPLE__
14076# include <TargetConditionals.h>
14077#endif
14078#ifndef SQLITE_MAX_MMAP_SIZE0x7fff0000
14079# if defined(__linux__1) \
14080 || defined(_WIN32) \
14081 || (defined(__APPLE__) && defined(__MACH__)) \
14082 || defined(__sun) \
14083 || defined(__FreeBSD__) \
14084 || defined(__DragonFly__)
14085# define SQLITE_MAX_MMAP_SIZE0x7fff0000 0x7fff0000 /* 2147418112 */
14086# else
14087# define SQLITE_MAX_MMAP_SIZE0x7fff0000 0
14088# endif
14089#endif
14090
14091/*
14092** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
14093** default MMAP_SIZE is specified at compile-time, make sure that it does
14094** not exceed the maximum mmap size.
14095*/
14096#ifndef SQLITE_DEFAULT_MMAP_SIZE0
14097# define SQLITE_DEFAULT_MMAP_SIZE0 0
14098#endif
14099#if SQLITE_DEFAULT_MMAP_SIZE0>SQLITE_MAX_MMAP_SIZE0x7fff0000
14100# undef SQLITE_DEFAULT_MMAP_SIZE0
14101# define SQLITE_DEFAULT_MMAP_SIZE0 SQLITE_MAX_MMAP_SIZE0x7fff0000
14102#endif
14103
14104/*
14105** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
14106** Priority is given to SQLITE_ENABLE_STAT4. If either are defined, also
14107** define SQLITE_ENABLE_STAT3_OR_STAT4
14108*/
14109#ifdef SQLITE_ENABLE_STAT4
14110# undef SQLITE_ENABLE_STAT3
14111# define SQLITE_ENABLE_STAT3_OR_STAT4 1
14112#elif SQLITE_ENABLE_STAT3
14113# define SQLITE_ENABLE_STAT3_OR_STAT4 1
14114#elif SQLITE_ENABLE_STAT3_OR_STAT4
14115# undef SQLITE_ENABLE_STAT3_OR_STAT4
14116#endif
14117
14118/*
14119** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
14120** the Select query generator tracing logic is turned on.
14121*/
14122#if defined(SQLITE_ENABLE_SELECTTRACE)
14123# define SELECTTRACE_ENABLED0 1
14124#else
14125# define SELECTTRACE_ENABLED0 0
14126#endif
14127
14128/*
14129** An instance of the following structure is used to store the busy-handler
14130** callback for a given sqlite handle.
14131**
14132** The sqlite.busyHandler member of the sqlite struct contains the busy
14133** callback for the database handle. Each pager opened via the sqlite
14134** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14135** callback is currently invoked only from within pager.c.
14136*/
14137typedef struct BusyHandler BusyHandler;
14138struct BusyHandler {
14139 int (*xBusyHandler)(void *,int); /* The busy callback */
14140 void *pBusyArg; /* First arg to busy callback */
14141 int nBusy; /* Incremented with each busy call */
14142 u8 bExtraFileArg; /* Include sqlite3_file as callback arg */
14143};
14144
14145/*
14146** Name of the master database table. The master database table
14147** is a special table that holds the names and attributes of all
14148** user tables and indices.
14149*/
14150#define MASTER_NAME"sqlite_master" "sqlite_master"
14151#define TEMP_MASTER_NAME"sqlite_temp_master" "sqlite_temp_master"
14152
14153/*
14154** The root-page of the master database table.
14155*/
14156#define MASTER_ROOT1 1
14157
14158/*
14159** The name of the schema table.
14160*/
14161#define SCHEMA_TABLE(x)((!0)&&(x==1)?"sqlite_temp_master":"sqlite_master") ((!OMIT_TEMPDB0)&&(x==1)?TEMP_MASTER_NAME"sqlite_temp_master":MASTER_NAME"sqlite_master")
14162
14163/*
14164** A convenience macro that returns the number of elements in
14165** an array.
14166*/
14167#define ArraySize(X)((int)(sizeof(X)/sizeof(X[0]))) ((int)(sizeof(X)/sizeof(X[0])))
14168
14169/*
14170** Determine if the argument is a power of two
14171*/
14172#define IsPowerOfTwo(X)(((X)&((X)-1))==0) (((X)&((X)-1))==0)
14173
14174/*
14175** The following value as a destructor means to use sqlite3DbFree().
14176** The sqlite3DbFree() routine requires two parameters instead of the
14177** one parameter that destructors normally want. So we have to introduce
14178** this magic value that the code knows to handle differently. Any
14179** pointer will work here as long as it is distinct from SQLITE_STATIC
14180** and SQLITE_TRANSIENT.
14181*/
14182#define SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3MallocSize) ((sqlite3_destructor_type)sqlite3MallocSize)
14183
14184/*
14185** When SQLITE_OMIT_WSD is defined, it means that the target platform does
14186** not support Writable Static Data (WSD) such as global and static variables.
14187** All variables must either be on the stack or dynamically allocated from
14188** the heap. When WSD is unsupported, the variable declarations scattered
14189** throughout the SQLite code must become constants instead. The SQLITE_WSD
14190** macro is used for this purpose. And instead of referencing the variable
14191** directly, we use its constant as a key to lookup the run-time allocated
14192** buffer that holds real variable. The constant is also the initializer
14193** for the run-time allocated buffer.
14194**
14195** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
14196** macros become no-ops and have zero performance impact.
14197*/
14198#ifdef SQLITE_OMIT_WSD
14199 #define SQLITE_WSD const
14200 #define GLOBAL(t,v)v (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
14201 #define sqlite3GlobalConfigsqlite3Config GLOBAL(struct Sqlite3Config, sqlite3Config)sqlite3Config
14202SQLITE_API int sqlite3_wsd_init(int N, int J);
14203SQLITE_API void *sqlite3_wsd_find(void *K, int L);
14204#else
14205 #define SQLITE_WSD
14206 #define GLOBAL(t,v)v v
14207 #define sqlite3GlobalConfigsqlite3Config sqlite3Config
14208#endif
14209
14210/*
14211** The following macros are used to suppress compiler warnings and to
14212** make it clear to human readers when a function parameter is deliberately
14213** left unused within the body of a function. This usually happens when
14214** a function is called via a function pointer. For example the
14215** implementation of an SQL aggregate step callback may not use the
14216** parameter indicating the number of arguments passed to the aggregate,
14217** if it knows that this is enforced elsewhere.
14218**
14219** When a function parameter is not used at all within the body of a function,
14220** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
14221** However, these macros may also be used to suppress warnings related to
14222** parameters that may or may not be used depending on compilation options.
14223** For example those parameters only used in assert() statements. In these
14224** cases the parameters are named as per the usual conventions.
14225*/
14226#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
14227#define UNUSED_PARAMETER2(x,y)(void)(x),(void)(y) UNUSED_PARAMETER(x)(void)(x),UNUSED_PARAMETER(y)(void)(y)
14228
14229/*
14230** Forward references to structures
14231*/
14232typedef struct AggInfo AggInfo;
14233typedef struct AuthContext AuthContext;
14234typedef struct AutoincInfo AutoincInfo;
14235typedef struct Bitvec Bitvec;
14236typedef struct CollSeq CollSeq;
14237typedef struct Column Column;
14238typedef struct Db Db;
14239typedef struct Schema Schema;
14240typedef struct Expr Expr;
14241typedef struct ExprList ExprList;
14242typedef struct FKey FKey;
14243typedef struct FuncDestructor FuncDestructor;
14244typedef struct FuncDef FuncDef;
14245typedef struct FuncDefHash FuncDefHash;
14246typedef struct IdList IdList;
14247typedef struct Index Index;
14248typedef struct IndexSample IndexSample;
14249typedef struct KeyClass KeyClass;
14250typedef struct KeyInfo KeyInfo;
14251typedef struct Lookaside Lookaside;
14252typedef struct LookasideSlot LookasideSlot;
14253typedef struct Module Module;
14254typedef struct NameContext NameContext;
14255typedef struct Parse Parse;
14256typedef struct PreUpdate PreUpdate;
14257typedef struct PrintfArguments PrintfArguments;
14258typedef struct RenameToken RenameToken;
14259typedef struct RowSet RowSet;
14260typedef struct Savepoint Savepoint;
14261typedef struct Select Select;
14262typedef struct SQLiteThread SQLiteThread;
14263typedef struct SelectDest SelectDest;
14264typedef struct SrcList SrcList;
14265typedef struct sqlite3_str StrAccum; /* Internal alias for sqlite3_str */
14266typedef struct Table Table;
14267typedef struct TableLock TableLock;
14268typedef struct Token Token;
14269typedef struct TreeView TreeView;
14270typedef struct Trigger Trigger;
14271typedef struct TriggerPrg TriggerPrg;
14272typedef struct TriggerStep TriggerStep;
14273typedef struct UnpackedRecord UnpackedRecord;
14274typedef struct Upsert Upsert;
14275typedef struct VTable VTable;
14276typedef struct VtabCtx VtabCtx;
14277typedef struct Walker Walker;
14278typedef struct WhereInfo WhereInfo;
14279typedef struct Window Window;
14280typedef struct With With;
14281
14282
14283/*
14284** The bitmask datatype defined below is used for various optimizations.
14285**
14286** Changing this from a 64-bit to a 32-bit type limits the number of
14287** tables in a join to 32 instead of 64. But it also reduces the size
14288** of the library by 738 bytes on ix86.
14289*/
14290#ifdef SQLITE_BITMASK_TYPE
14291 typedef SQLITE_BITMASK_TYPE Bitmask;
14292#else
14293 typedef u64 Bitmask;
14294#endif
14295
14296/*
14297** The number of bits in a Bitmask. "BMS" means "BitMask Size".
14298*/
14299#define BMS((int)(sizeof(Bitmask)*8)) ((int)(sizeof(Bitmask)*8))
14300
14301/*
14302** A bit in a Bitmask
14303*/
14304#define MASKBIT(n)(((Bitmask)1)<<(n)) (((Bitmask)1)<<(n))
14305#define MASKBIT32(n)(((unsigned int)1)<<(n)) (((unsigned int)1)<<(n))
14306#define ALLBITS((Bitmask)-1) ((Bitmask)-1)
14307
14308/* A VList object records a mapping between parameters/variables/wildcards
14309** in the SQL statement (such as $abc, @pqr, or :xyz) and the integer
14310** variable number associated with that parameter. See the format description
14311** on the sqlite3VListAdd() routine for more information. A VList is really
14312** just an array of integers.
14313*/
14314typedef int VList;
14315
14316/*
14317** Defer sourcing vdbe.h and btree.h until after the "u8" and
14318** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
14319** pointer types (i.e. FuncDef) defined above.
14320*/
14321/************** Include btree.h in the middle of sqliteInt.h *****************/
14322/************** Begin file btree.h *******************************************/
14323/*
14324** 2001 September 15
14325**
14326** The author disclaims copyright to this source code. In place of
14327** a legal notice, here is a blessing:
14328**
14329** May you do good and not evil.
14330** May you find forgiveness for yourself and forgive others.
14331** May you share freely, never taking more than you give.
14332**
14333*************************************************************************
14334** This header file defines the interface that the sqlite B-Tree file
14335** subsystem. See comments in the source code for a detailed description
14336** of what each interface routine does.
14337*/
14338#ifndef SQLITE_BTREE_H
14339#define SQLITE_BTREE_H
14340
14341/* TODO: This definition is just included so other modules compile. It
14342** needs to be revisited.
14343*/
14344#define SQLITE_N_BTREE_META16 16
14345
14346/*
14347** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
14348** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
14349*/
14350#ifndef SQLITE_DEFAULT_AUTOVACUUM0
14351 #define SQLITE_DEFAULT_AUTOVACUUM0 0
14352#endif
14353
14354#define BTREE_AUTOVACUUM_NONE0 0 /* Do not do auto-vacuum */
14355#define BTREE_AUTOVACUUM_FULL1 1 /* Do full auto-vacuum */
14356#define BTREE_AUTOVACUUM_INCR2 2 /* Incremental vacuum */
14357
14358/*
14359** Forward declarations of structure
14360*/
14361typedef struct Btree Btree;
14362typedef struct BtCursor BtCursor;
14363typedef struct BtShared BtShared;
14364typedef struct BtreePayload BtreePayload;
14365
14366
14367SQLITE_PRIVATEstatic int sqlite3BtreeOpen(
14368 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
14369 const char *zFilename, /* Name of database file to open */
14370 sqlite3 *db, /* Associated database connection */
14371 Btree **ppBtree, /* Return open Btree* here */
14372 int flags, /* Flags */
14373 int vfsFlags /* Flags passed through to VFS open */
14374);
14375
14376/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
14377** following values.
14378**
14379** NOTE: These values must match the corresponding PAGER_ values in
14380** pager.h.
14381*/
14382#define BTREE_OMIT_JOURNAL1 1 /* Do not create or use a rollback journal */
14383#define BTREE_MEMORY2 2 /* This is an in-memory DB */
14384#define BTREE_SINGLE4 4 /* The file contains at most 1 b-tree */
14385#define BTREE_UNORDERED8 8 /* Use of a hash implementation is OK */
14386
14387SQLITE_PRIVATEstatic int sqlite3BtreeClose(Btree*);
14388SQLITE_PRIVATEstatic int sqlite3BtreeSetCacheSize(Btree*,int);
14389SQLITE_PRIVATEstatic int sqlite3BtreeSetSpillSize(Btree*,int);
14390#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
14391SQLITE_PRIVATEstatic int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
14392#endif
14393SQLITE_PRIVATEstatic int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
14394SQLITE_PRIVATEstatic int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
14395SQLITE_PRIVATEstatic int sqlite3BtreeGetPageSize(Btree*);
14396SQLITE_PRIVATEstatic int sqlite3BtreeMaxPageCount(Btree*,int);
14397SQLITE_PRIVATEstatic u32 sqlite3BtreeLastPage(Btree*);
14398SQLITE_PRIVATEstatic int sqlite3BtreeSecureDelete(Btree*,int);
14399SQLITE_PRIVATEstatic int sqlite3BtreeGetOptimalReserve(Btree*);
14400SQLITE_PRIVATEstatic int sqlite3BtreeGetReserveNoMutex(Btree *p);
14401SQLITE_PRIVATEstatic int sqlite3BtreeSetAutoVacuum(Btree *, int);
14402SQLITE_PRIVATEstatic int sqlite3BtreeGetAutoVacuum(Btree *);
14403SQLITE_PRIVATEstatic int sqlite3BtreeBeginTrans(Btree*,int,int*);
14404SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
14405SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseTwo(Btree*, int);
14406SQLITE_PRIVATEstatic int sqlite3BtreeCommit(Btree*);
14407SQLITE_PRIVATEstatic int sqlite3BtreeRollback(Btree*,int,int);
14408SQLITE_PRIVATEstatic int sqlite3BtreeBeginStmt(Btree*,int);
14409SQLITE_PRIVATEstatic int sqlite3BtreeCreateTable(Btree*, int*, int flags);
14410SQLITE_PRIVATEstatic int sqlite3BtreeIsInTrans(Btree*);
14411SQLITE_PRIVATEstatic int sqlite3BtreeIsInReadTrans(Btree*);
14412SQLITE_PRIVATEstatic int sqlite3BtreeIsInBackup(Btree*);
14413SQLITE_PRIVATEstatic void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
14414SQLITE_PRIVATEstatic int sqlite3BtreeSchemaLocked(Btree *pBtree);
14415#ifndef SQLITE_OMIT_SHARED_CACHE
14416SQLITE_PRIVATEstatic int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
14417#endif
14418SQLITE_PRIVATEstatic int sqlite3BtreeSavepoint(Btree *, int, int);
14419
14420SQLITE_PRIVATEstatic const char *sqlite3BtreeGetFilename(Btree *);
14421SQLITE_PRIVATEstatic const char *sqlite3BtreeGetJournalname(Btree *);
14422SQLITE_PRIVATEstatic int sqlite3BtreeCopyFile(Btree *, Btree *);
14423
14424SQLITE_PRIVATEstatic int sqlite3BtreeIncrVacuum(Btree *);
14425
14426/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
14427** of the flags shown below.
14428**
14429** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
14430** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
14431** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
14432** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
14433** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
14434** indices.)
14435*/
14436#define BTREE_INTKEY1 1 /* Table has only 64-bit signed integer keys */
14437#define BTREE_BLOBKEY2 2 /* Table has keys only - no data */
14438
14439SQLITE_PRIVATEstatic int sqlite3BtreeDropTable(Btree*, int, int*);
14440SQLITE_PRIVATEstatic int sqlite3BtreeClearTable(Btree*, int, int*);
14441SQLITE_PRIVATEstatic int sqlite3BtreeClearTableOfCursor(BtCursor*);
14442SQLITE_PRIVATEstatic int sqlite3BtreeTripAllCursors(Btree*, int, int);
14443
14444SQLITE_PRIVATEstatic void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
14445SQLITE_PRIVATEstatic int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
14446
14447SQLITE_PRIVATEstatic int sqlite3BtreeNewDb(Btree *p);
14448
14449/*
14450** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
14451** should be one of the following values. The integer values are assigned
14452** to constants so that the offset of the corresponding field in an
14453** SQLite database header may be found using the following formula:
14454**
14455** offset = 36 + (idx * 4)
14456**
14457** For example, the free-page-count field is located at byte offset 36 of
14458** the database file header. The incr-vacuum-flag field is located at
14459** byte offset 64 (== 36+4*7).
14460**
14461** The BTREE_DATA_VERSION value is not really a value stored in the header.
14462** It is a read-only number computed by the pager. But we merge it with
14463** the header value access routines since its access pattern is the same.
14464** Call it a "virtual meta value".
14465*/
14466#define BTREE_FREE_PAGE_COUNT0 0
14467#define BTREE_SCHEMA_VERSION1 1
14468#define BTREE_FILE_FORMAT2 2
14469#define BTREE_DEFAULT_CACHE_SIZE3 3
14470#define BTREE_LARGEST_ROOT_PAGE4 4
14471#define BTREE_TEXT_ENCODING5 5
14472#define BTREE_USER_VERSION6 6
14473#define BTREE_INCR_VACUUM7 7
14474#define BTREE_APPLICATION_ID8 8
14475#define BTREE_DATA_VERSION15 15 /* A virtual meta-value */
14476
14477/*
14478** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
14479** interface.
14480**
14481** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
14482**
14483** The first argument is an Expr* (which is guaranteed to be constant for
14484** the lifetime of the cursor) that defines constraints on which rows
14485** might be fetched with this cursor. The Expr* tree may contain
14486** TK_REGISTER nodes that refer to values stored in the array of registers
14487** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
14488** then the value of the node is the value in Mem[pExpr.iTable]. Any
14489** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
14490** column of the b-tree of the cursor. The Expr tree will not contain
14491** any function calls nor subqueries nor references to b-trees other than
14492** the cursor being hinted.
14493**
14494** The design of the _RANGE hint is aid b-tree implementations that try
14495** to prefetch content from remote machines - to provide those
14496** implementations with limits on what needs to be prefetched and thereby
14497** reduce network bandwidth.
14498**
14499** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
14500** standard SQLite. The other hints are provided for extentions that use
14501** the SQLite parser and code generator but substitute their own storage
14502** engine.
14503*/
14504#define BTREE_HINT_RANGE0 0 /* Range constraints on queries */
14505
14506/*
14507** Values that may be OR'd together to form the argument to the
14508** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
14509**
14510** The BTREE_BULKLOAD flag is set on index cursors when the index is going
14511** to be filled with content that is already in sorted order.
14512**
14513** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
14514** OP_SeekLE opcodes for a range search, but where the range of entries
14515** selected will all have the same key. In other words, the cursor will
14516** be used only for equality key searches.
14517**
14518*/
14519#define BTREE_BULKLOAD0x00000001 0x00000001 /* Used to full index in sorted order */
14520#define BTREE_SEEK_EQ0x00000002 0x00000002 /* EQ seeks only - no range seeks */
14521
14522/*
14523** Flags passed as the third argument to sqlite3BtreeCursor().
14524**
14525** For read-only cursors the wrFlag argument is always zero. For read-write
14526** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
14527** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
14528** only be used by SQLite for the following:
14529**
14530** * to seek to and then delete specific entries, and/or
14531**
14532** * to read values that will be used to create keys that other
14533** BTREE_FORDELETE cursors will seek to and delete.
14534**
14535** The BTREE_FORDELETE flag is an optimization hint. It is not used by
14536** by this, the native b-tree engine of SQLite, but it is available to
14537** alternative storage engines that might be substituted in place of this
14538** b-tree system. For alternative storage engines in which a delete of
14539** the main table row automatically deletes corresponding index rows,
14540** the FORDELETE flag hint allows those alternative storage engines to
14541** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
14542** and DELETE operations as no-ops, and any READ operation against a
14543** FORDELETE cursor may return a null row: 0x01 0x00.
14544*/
14545#define BTREE_WRCSR0x00000004 0x00000004 /* read-write cursor */
14546#define BTREE_FORDELETE0x00000008 0x00000008 /* Cursor is for seek/delete only */
14547
14548SQLITE_PRIVATEstatic int sqlite3BtreeCursor(
14549 Btree*, /* BTree containing table to open */
14550 int iTable, /* Index of root page */
14551 int wrFlag, /* 1 for writing. 0 for read-only */
14552 struct KeyInfo*, /* First argument to compare function */
14553 BtCursor *pCursor /* Space to write cursor structure */
14554);
14555SQLITE_PRIVATEstatic BtCursor *sqlite3BtreeFakeValidCursor(void);
14556SQLITE_PRIVATEstatic int sqlite3BtreeCursorSize(void);
14557SQLITE_PRIVATEstatic void sqlite3BtreeCursorZero(BtCursor*);
14558SQLITE_PRIVATEstatic void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
14559#ifdef SQLITE_ENABLE_CURSOR_HINTS
14560SQLITE_PRIVATEstatic void sqlite3BtreeCursorHint(BtCursor*, int, ...);
14561#endif
14562
14563SQLITE_PRIVATEstatic int sqlite3BtreeCloseCursor(BtCursor*);
14564SQLITE_PRIVATEstatic int sqlite3BtreeMovetoUnpacked(
14565 BtCursor*,
14566 UnpackedRecord *pUnKey,
14567 i64 intKey,
14568 int bias,
14569 int *pRes
14570);
14571SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasMoved(BtCursor*);
14572SQLITE_PRIVATEstatic int sqlite3BtreeCursorRestore(BtCursor*, int*);
14573SQLITE_PRIVATEstatic int sqlite3BtreeDelete(BtCursor*, u8 flags);
14574
14575/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
14576#define BTREE_SAVEPOSITION0x02 0x02 /* Leave cursor pointing at NEXT or PREV */
14577#define BTREE_AUXDELETE0x04 0x04 /* not the primary delete operation */
14578#define BTREE_APPEND0x08 0x08 /* Insert is likely an append */
14579
14580/* An instance of the BtreePayload object describes the content of a single
14581** entry in either an index or table btree.
14582**
14583** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
14584** an arbitrary key and no data. These btrees have pKey,nKey set to the
14585** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
14586** fields give an array of Mem objects that are a decomposition of the key.
14587** The nMem field might be zero, indicating that no decomposition is available.
14588**
14589** Table btrees (used for rowid tables) contain an integer rowid used as
14590** the key and passed in the nKey field. The pKey field is zero.
14591** pData,nData hold the content of the new entry. nZero extra zero bytes
14592** are appended to the end of the content when constructing the entry.
14593** The aMem,nMem fields are uninitialized for table btrees.
14594**
14595** Field usage summary:
14596**
14597** Table BTrees Index Btrees
14598**
14599** pKey always NULL encoded key
14600** nKey the ROWID length of pKey
14601** pData data not used
14602** aMem not used decomposed key value
14603** nMem not used entries in aMem
14604** nData length of pData not used
14605** nZero extra zeros after pData not used
14606**
14607** This object is used to pass information into sqlite3BtreeInsert(). The
14608** same information used to be passed as five separate parameters. But placing
14609** the information into this object helps to keep the interface more
14610** organized and understandable, and it also helps the resulting code to
14611** run a little faster by using fewer registers for parameter passing.
14612*/
14613struct BtreePayload {
14614 const void *pKey; /* Key content for indexes. NULL for tables */
14615 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
14616 const void *pData; /* Data for tables. */
14617 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
14618 u16 nMem; /* Number of aMem[] value. Might be zero */
14619 int nData; /* Size of pData. 0 if none. */
14620 int nZero; /* Extra zero data appended after pData,nData */
14621};
14622
14623SQLITE_PRIVATEstatic int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
14624 int flags, int seekResult);
14625SQLITE_PRIVATEstatic int sqlite3BtreeFirst(BtCursor*, int *pRes);
14626SQLITE_PRIVATEstatic int sqlite3BtreeLast(BtCursor*, int *pRes);
14627SQLITE_PRIVATEstatic int sqlite3BtreeNext(BtCursor*, int flags);
14628SQLITE_PRIVATEstatic int sqlite3BtreeEof(BtCursor*);
14629SQLITE_PRIVATEstatic int sqlite3BtreePrevious(BtCursor*, int flags);
14630SQLITE_PRIVATEstatic i64 sqlite3BtreeIntegerKey(BtCursor*);
14631#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
14632SQLITE_PRIVATEstatic i64 sqlite3BtreeOffset(BtCursor*);
14633#endif
14634SQLITE_PRIVATEstatic int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
14635SQLITE_PRIVATEstatic const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
14636SQLITE_PRIVATEstatic u32 sqlite3BtreePayloadSize(BtCursor*);
14637SQLITE_PRIVATEstatic sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
14638
14639SQLITE_PRIVATEstatic char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
14640SQLITE_PRIVATEstatic struct Pager *sqlite3BtreePager(Btree*);
14641SQLITE_PRIVATEstatic i64 sqlite3BtreeRowCountEst(BtCursor*);
14642
14643#ifndef SQLITE_OMIT_INCRBLOB
14644SQLITE_PRIVATEstatic int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
14645SQLITE_PRIVATEstatic int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
14646SQLITE_PRIVATEstatic void sqlite3BtreeIncrblobCursor(BtCursor *);
14647#endif
14648SQLITE_PRIVATEstatic void sqlite3BtreeClearCursor(BtCursor *);
14649SQLITE_PRIVATEstatic int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
14650SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
14651SQLITE_PRIVATEstatic int sqlite3BtreeIsReadonly(Btree *pBt);
14652SQLITE_PRIVATEstatic int sqlite3HeaderSizeBtree(void);
14653
14654#ifndef NDEBUG1
14655SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValid(BtCursor*);
14656#endif
14657SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValidNN(BtCursor*);
14658
14659#ifndef SQLITE_OMIT_BTREECOUNT
14660SQLITE_PRIVATEstatic int sqlite3BtreeCount(BtCursor *, i64 *);
14661#endif
14662
14663#ifdef SQLITE_TEST
14664SQLITE_PRIVATEstatic int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
14665SQLITE_PRIVATEstatic void sqlite3BtreeCursorList(Btree*);
14666#endif
14667
14668#ifndef SQLITE_OMIT_WAL
14669SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
14670#endif
14671
14672/*
14673** If we are not using shared cache, then there is no need to
14674** use mutexes to access the BtShared structures. So make the
14675** Enter and Leave procedures no-ops.
14676*/
14677#ifndef SQLITE_OMIT_SHARED_CACHE
14678SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree*);
14679SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3*);
14680SQLITE_PRIVATEstatic int sqlite3BtreeSharable(Btree*);
14681SQLITE_PRIVATEstatic void sqlite3BtreeEnterCursor(BtCursor*);
14682SQLITE_PRIVATEstatic int sqlite3BtreeConnectionCount(Btree*);
14683#else
14684# define sqlite3BtreeEnter(X)
14685# define sqlite3BtreeEnterAll(X)
14686# define sqlite3BtreeSharable(X) 0
14687# define sqlite3BtreeEnterCursor(X)
14688# define sqlite3BtreeConnectionCount(X) 1
14689#endif
14690
14691#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE1
14692SQLITE_PRIVATEstatic void sqlite3BtreeLeave(Btree*);
14693SQLITE_PRIVATEstatic void sqlite3BtreeLeaveCursor(BtCursor*);
14694SQLITE_PRIVATEstatic void sqlite3BtreeLeaveAll(sqlite3*);
14695#ifndef NDEBUG1
14696 /* These routines are used inside assert() statements only. */
14697SQLITE_PRIVATEstatic int sqlite3BtreeHoldsMutex(Btree*);
14698SQLITE_PRIVATEstatic int sqlite3BtreeHoldsAllMutexes(sqlite3*);
14699SQLITE_PRIVATEstatic int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
14700#endif
14701#else
14702
14703# define sqlite3BtreeLeave(X)
14704# define sqlite3BtreeLeaveCursor(X)
14705# define sqlite3BtreeLeaveAll(X)
14706
14707# define sqlite3BtreeHoldsMutex(X) 1
14708# define sqlite3BtreeHoldsAllMutexes(X) 1
14709# define sqlite3SchemaMutexHeld(X,Y,Z) 1
14710#endif
14711
14712
14713#endif /* SQLITE_BTREE_H */
14714
14715/************** End of btree.h ***********************************************/
14716/************** Continuing where we left off in sqliteInt.h ******************/
14717/************** Include vdbe.h in the middle of sqliteInt.h ******************/
14718/************** Begin file vdbe.h ********************************************/
14719/*
14720** 2001 September 15
14721**
14722** The author disclaims copyright to this source code. In place of
14723** a legal notice, here is a blessing:
14724**
14725** May you do good and not evil.
14726** May you find forgiveness for yourself and forgive others.
14727** May you share freely, never taking more than you give.
14728**
14729*************************************************************************
14730** Header file for the Virtual DataBase Engine (VDBE)
14731**
14732** This header defines the interface to the virtual database engine
14733** or VDBE. The VDBE implements an abstract machine that runs a
14734** simple program to access and modify the underlying database.
14735*/
14736#ifndef SQLITE_VDBE_H
14737#define SQLITE_VDBE_H
14738/* #include <stdio.h> */
14739
14740/*
14741** A single VDBE is an opaque structure named "Vdbe". Only routines
14742** in the source file sqliteVdbe.c are allowed to see the insides
14743** of this structure.
14744*/
14745typedef struct Vdbe Vdbe;
14746
14747/*
14748** The names of the following types declared in vdbeInt.h are required
14749** for the VdbeOp definition.
14750*/
14751typedef struct sqlite3_value Mem;
14752typedef struct SubProgram SubProgram;
14753
14754/*
14755** A single instruction of the virtual machine has an opcode
14756** and as many as three operands. The instruction is recorded
14757** as an instance of the following structure:
14758*/
14759struct VdbeOp {
14760 u8 opcode; /* What operation to perform */
14761 signed char p4type; /* One of the P4_xxx constants for p4 */
14762 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
14763 int p1; /* First operand */
14764 int p2; /* Second parameter (often the jump destination) */
14765 int p3; /* The third parameter */
14766 union p4union { /* fourth parameter */
14767 int i; /* Integer value if p4type==P4_INT32 */
14768 void *p; /* Generic pointer */
14769 char *z; /* Pointer to data for string (char array) types */
14770 i64 *pI64; /* Used when p4type is P4_INT64 */
14771 double *pReal; /* Used when p4type is P4_REAL */
14772 FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
14773 sqlite3_context *pCtx; /* Used when p4type is P4_FUNCCTX */
14774 CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
14775 Mem *pMem; /* Used when p4type is P4_MEM */
14776 VTable *pVtab; /* Used when p4type is P4_VTAB */
14777 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
14778 int *ai; /* Used when p4type is P4_INTARRAY */
14779 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
14780 Table *pTab; /* Used when p4type is P4_TABLE */
14781#ifdef SQLITE_ENABLE_CURSOR_HINTS
14782 Expr *pExpr; /* Used when p4type is P4_EXPR */
14783#endif
14784 int (*xAdvance)(BtCursor *, int);
14785 } p4;
14786#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
14787 char *zComment; /* Comment to improve readability */
14788#endif
14789#ifdef VDBE_PROFILE
14790 u32 cnt; /* Number of times this instruction was executed */
14791 u64 cycles; /* Total time spent executing this instruction */
14792#endif
14793#ifdef SQLITE_VDBE_COVERAGE
14794 u32 iSrcLine; /* Source-code line that generated this opcode
14795 ** with flags in the upper 8 bits */
14796#endif
14797};
14798typedef struct VdbeOp VdbeOp;
14799
14800
14801/*
14802** A sub-routine used to implement a trigger program.
14803*/
14804struct SubProgram {
14805 VdbeOp *aOp; /* Array of opcodes for sub-program */
14806 int nOp; /* Elements in aOp[] */
14807 int nMem; /* Number of memory cells required */
14808 int nCsr; /* Number of cursors required */
14809 u8 *aOnce; /* Array of OP_Once flags */
14810 void *token; /* id that may be used to recursive triggers */
14811 SubProgram *pNext; /* Next sub-program already visited */
14812};
14813
14814/*
14815** A smaller version of VdbeOp used for the VdbeAddOpList() function because
14816** it takes up less space.
14817*/
14818struct VdbeOpList {
14819 u8 opcode; /* What operation to perform */
14820 signed char p1; /* First operand */
14821 signed char p2; /* Second parameter (often the jump destination) */
14822 signed char p3; /* Third parameter */
14823};
14824typedef struct VdbeOpList VdbeOpList;
14825
14826/*
14827** Allowed values of VdbeOp.p4type
14828*/
14829#define P4_NOTUSED0 0 /* The P4 parameter is not used */
14830#define P4_TRANSIENT0 0 /* P4 is a pointer to a transient string */
14831#define P4_STATIC(-1) (-1) /* Pointer to a static string */
14832#define P4_COLLSEQ(-2) (-2) /* P4 is a pointer to a CollSeq structure */
14833#define P4_INT32(-3) (-3) /* P4 is a 32-bit signed integer */
14834#define P4_SUBPROGRAM(-4) (-4) /* P4 is a pointer to a SubProgram structure */
14835#define P4_ADVANCE(-5) (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */
14836#define P4_TABLE(-6) (-6) /* P4 is a pointer to a Table structure */
14837/* Above do not own any resources. Must free those below */
14838#define P4_FREE_IF_LE(-7) (-7)
14839#define P4_DYNAMIC(-7) (-7) /* Pointer to memory from sqliteMalloc() */
14840#define P4_FUNCDEF(-8) (-8) /* P4 is a pointer to a FuncDef structure */
14841#define P4_KEYINFO(-9) (-9) /* P4 is a pointer to a KeyInfo structure */
14842#define P4_EXPR(-10) (-10) /* P4 is a pointer to an Expr tree */
14843#define P4_MEM(-11) (-11) /* P4 is a pointer to a Mem* structure */
14844#define P4_VTAB(-12) (-12) /* P4 is a pointer to an sqlite3_vtab structure */
14845#define P4_REAL(-13) (-13) /* P4 is a 64-bit floating point value */
14846#define P4_INT64(-14) (-14) /* P4 is a 64-bit signed integer */
14847#define P4_INTARRAY(-15) (-15) /* P4 is a vector of 32-bit integers */
14848#define P4_FUNCCTX(-16) (-16) /* P4 is a pointer to an sqlite3_context object */
14849#define P4_DYNBLOB(-17) (-17) /* Pointer to memory from sqliteMalloc() */
14850
14851/* Error message codes for OP_Halt */
14852#define P5_ConstraintNotNull1 1
14853#define P5_ConstraintUnique2 2
14854#define P5_ConstraintCheck3 3
14855#define P5_ConstraintFK4 4
14856
14857/*
14858** The Vdbe.aColName array contains 5n Mem structures, where n is the
14859** number of columns of data returned by the statement.
14860*/
14861#define COLNAME_NAME0 0
14862#define COLNAME_DECLTYPE1 1
14863#define COLNAME_DATABASE2 2
14864#define COLNAME_TABLE3 3
14865#define COLNAME_COLUMN4 4
14866#ifdef SQLITE_ENABLE_COLUMN_METADATA
14867# define COLNAME_N2 5 /* Number of COLNAME_xxx symbols */
14868#else
14869# ifdef SQLITE_OMIT_DECLTYPE
14870# define COLNAME_N2 1 /* Store only the name */
14871# else
14872# define COLNAME_N2 2 /* Store the name and decltype */
14873# endif
14874#endif
14875
14876/*
14877** The following macro converts a label returned by sqlite3VdbeMakeLabel()
14878** into an index into the Parse.aLabel[] array that contains the resolved
14879** address of that label.
14880*/
14881#define ADDR(X)(~(X)) (~(X))
14882
14883/*
14884** The makefile scans the vdbe.c source file and creates the "opcodes.h"
14885** header file that defines a number for each opcode used by the VDBE.
14886*/
14887/************** Include opcodes.h in the middle of vdbe.h ********************/
14888/************** Begin file opcodes.h *****************************************/
14889/* Automatically generated. Do not edit */
14890/* See the tool/mkopcodeh.tcl script for details */
14891#define OP_Savepoint0 0
14892#define OP_AutoCommit1 1
14893#define OP_Transaction2 2
14894#define OP_SorterNext3 3 /* jump */
14895#define OP_Prev4 4 /* jump */
14896#define OP_Next5 5 /* jump */
14897#define OP_Checkpoint6 6
14898#define OP_JournalMode7 7
14899#define OP_Vacuum8 8
14900#define OP_VFilter9 9 /* jump, synopsis: iplan=r[P3] zplan='P4' */
14901#define OP_VUpdate10 10 /* synopsis: data=r[P3@P2] */
14902#define OP_Goto11 11 /* jump */
14903#define OP_Gosub12 12 /* jump */
14904#define OP_InitCoroutine13 13 /* jump */
14905#define OP_Yield14 14 /* jump */
14906#define OP_MustBeInt15 15 /* jump */
14907#define OP_Jump16 16 /* jump */
14908#define OP_Once17 17 /* jump */
14909#define OP_If18 18 /* jump */
14910#define OP_Not19 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
14911#define OP_IfNot20 20 /* jump */
14912#define OP_IfNullRow21 21 /* jump, synopsis: if P1.nullRow then r[P3]=NULL, goto P2 */
14913#define OP_SeekLT22 22 /* jump, synopsis: key=r[P3@P4] */
14914#define OP_SeekLE23 23 /* jump, synopsis: key=r[P3@P4] */
14915#define OP_SeekGE24 24 /* jump, synopsis: key=r[P3@P4] */
14916#define OP_SeekGT25 25 /* jump, synopsis: key=r[P3@P4] */
14917#define OP_IfNoHope26 26 /* jump, synopsis: key=r[P3@P4] */
14918#define OP_NoConflict27 27 /* jump, synopsis: key=r[P3@P4] */
14919#define OP_NotFound28 28 /* jump, synopsis: key=r[P3@P4] */
14920#define OP_Found29 29 /* jump, synopsis: key=r[P3@P4] */
14921#define OP_SeekRowid30 30 /* jump, synopsis: intkey=r[P3] */
14922#define OP_NotExists31 31 /* jump, synopsis: intkey=r[P3] */
14923#define OP_Last32 32 /* jump */
14924#define OP_IfSmaller33 33 /* jump */
14925#define OP_SorterSort34 34 /* jump */
14926#define OP_Sort35 35 /* jump */
14927#define OP_Rewind36 36 /* jump */
14928#define OP_IdxLE37 37 /* jump, synopsis: key=r[P3@P4] */
14929#define OP_IdxGT38 38 /* jump, synopsis: key=r[P3@P4] */
14930#define OP_IdxLT39 39 /* jump, synopsis: key=r[P3@P4] */
14931#define OP_IdxGE40 40 /* jump, synopsis: key=r[P3@P4] */
14932#define OP_RowSetRead41 41 /* jump, synopsis: r[P3]=rowset(P1) */
14933#define OP_RowSetTest42 42 /* jump, synopsis: if r[P3] in rowset(P1) goto P2 */
14934#define OP_Or43 43 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
14935#define OP_And44 44 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
14936#define OP_Program45 45 /* jump */
14937#define OP_FkIfZero46 46 /* jump, synopsis: if fkctr[P1]==0 goto P2 */
14938#define OP_IfPos47 47 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
14939#define OP_IfNotZero48 48 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
14940#define OP_DecrJumpZero49 49 /* jump, synopsis: if (--r[P1])==0 goto P2 */
14941#define OP_IsNull50 50 /* jump, same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
14942#define OP_NotNull51 51 /* jump, same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
14943#define OP_Ne52 52 /* jump, same as TK_NE, synopsis: IF r[P3]!=r[P1] */
14944#define OP_Eq53 53 /* jump, same as TK_EQ, synopsis: IF r[P3]==r[P1] */
14945#define OP_Gt54 54 /* jump, same as TK_GT, synopsis: IF r[P3]>r[P1] */
14946#define OP_Le55 55 /* jump, same as TK_LE, synopsis: IF r[P3]<=r[P1] */
14947#define OP_Lt56 56 /* jump, same as TK_LT, synopsis: IF r[P3]<r[P1] */
14948#define OP_Ge57 57 /* jump, same as TK_GE, synopsis: IF r[P3]>=r[P1] */
14949#define OP_ElseNotEq58 58 /* jump, same as TK_ESCAPE */
14950#define OP_IncrVacuum59 59 /* jump */
14951#define OP_VNext60 60 /* jump */
14952#define OP_Init61 61 /* jump, synopsis: Start at P2 */
14953#define OP_PureFunc062 62
14954#define OP_Function063 63 /* synopsis: r[P3]=func(r[P2@P5]) */
14955#define OP_PureFunc64 64
14956#define OP_Function65 65 /* synopsis: r[P3]=func(r[P2@P5]) */
14957#define OP_Return66 66
14958#define OP_EndCoroutine67 67
14959#define OP_HaltIfNull68 68 /* synopsis: if r[P3]=null halt */
14960#define OP_Halt69 69
14961#define OP_Integer70 70 /* synopsis: r[P2]=P1 */
14962#define OP_Int6471 71 /* synopsis: r[P2]=P4 */
14963#define OP_String72 72 /* synopsis: r[P2]='P4' (len=P1) */
14964#define OP_Null73 73 /* synopsis: r[P2..P3]=NULL */
14965#define OP_SoftNull74 74 /* synopsis: r[P1]=NULL */
14966#define OP_Blob75 75 /* synopsis: r[P2]=P4 (len=P1) */
14967#define OP_Variable76 76 /* synopsis: r[P2]=parameter(P1,P4) */
14968#define OP_Move77 77 /* synopsis: r[P2@P3]=r[P1@P3] */
14969#define OP_Copy78 78 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
14970#define OP_SCopy79 79 /* synopsis: r[P2]=r[P1] */
14971#define OP_IntCopy80 80 /* synopsis: r[P2]=r[P1] */
14972#define OP_ResultRow81 81 /* synopsis: output=r[P1@P2] */
14973#define OP_CollSeq82 82
14974#define OP_AddImm83 83 /* synopsis: r[P1]=r[P1]+P2 */
14975#define OP_RealAffinity84 84
14976#define OP_Cast85 85 /* synopsis: affinity(r[P1]) */
14977#define OP_Permutation86 86
14978#define OP_Compare87 87 /* synopsis: r[P1@P3] <-> r[P2@P3] */
14979#define OP_IsTrue88 88 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
14980#define OP_Offset89 89 /* synopsis: r[P3] = sqlite_offset(P1) */
14981#define OP_Column90 90 /* synopsis: r[P3]=PX */
14982#define OP_Affinity91 91 /* synopsis: affinity(r[P1@P2]) */
14983#define OP_MakeRecord92 92 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
14984#define OP_Count93 93 /* synopsis: r[P2]=count() */
14985#define OP_ReadCookie94 94
14986#define OP_SetCookie95 95
14987#define OP_BitAnd96 96 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
14988#define OP_BitOr97 97 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
14989#define OP_ShiftLeft98 98 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
14990#define OP_ShiftRight99 99 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
14991#define OP_Add100 100 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
14992#define OP_Subtract101 101 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
14993#define OP_Multiply102 102 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
14994#define OP_Divide103 103 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
14995#define OP_Remainder104 104 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
14996#define OP_Concat105 105 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
14997#define OP_ReopenIdx106 106 /* synopsis: root=P2 iDb=P3 */
14998#define OP_BitNot107 107 /* same as TK_BITNOT, synopsis: r[P2]= ~r[P1] */
14999#define OP_OpenRead108 108 /* synopsis: root=P2 iDb=P3 */
15000#define OP_OpenWrite109 109 /* synopsis: root=P2 iDb=P3 */
15001#define OP_String8110 110 /* same as TK_STRING, synopsis: r[P2]='P4' */
15002#define OP_OpenDup111 111
15003#define OP_OpenAutoindex112 112 /* synopsis: nColumn=P2 */
15004#define OP_OpenEphemeral113 113 /* synopsis: nColumn=P2 */
15005#define OP_SorterOpen114 114
15006#define OP_SequenceTest115 115 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
15007#define OP_OpenPseudo116 116 /* synopsis: P3 columns in r[P2] */
15008#define OP_Close117 117
15009#define OP_ColumnsUsed118 118
15010#define OP_SeekHit119 119 /* synopsis: seekHit=P2 */
15011#define OP_Sequence120 120 /* synopsis: r[P2]=cursor[P1].ctr++ */
15012#define OP_NewRowid121 121 /* synopsis: r[P2]=rowid */
15013#define OP_Insert122 122 /* synopsis: intkey=r[P3] data=r[P2] */
15014#define OP_Delete123 123
15015#define OP_ResetCount124 124
15016#define OP_SorterCompare125 125 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
15017#define OP_SorterData126 126 /* synopsis: r[P2]=data */
15018#define OP_RowData127 127 /* synopsis: r[P2]=data */
15019#define OP_Rowid128 128 /* synopsis: r[P2]=rowid */
15020#define OP_NullRow129 129
15021#define OP_SeekEnd130 130
15022#define OP_SorterInsert131 131 /* synopsis: key=r[P2] */
15023#define OP_IdxInsert132 132 /* synopsis: key=r[P2] */
15024#define OP_IdxDelete133 133 /* synopsis: key=r[P2@P3] */
15025#define OP_DeferredSeek134 134 /* synopsis: Move P3 to P1.rowid if needed */
15026#define OP_IdxRowid135 135 /* synopsis: r[P2]=rowid */
15027#define OP_Destroy136 136
15028#define OP_Clear137 137
15029#define OP_ResetSorter138 138
15030#define OP_CreateBtree139 139 /* synopsis: r[P2]=root iDb=P1 flags=P3 */
15031#define OP_SqlExec140 140
15032#define OP_ParseSchema141 141
15033#define OP_LoadAnalysis142 142
15034#define OP_DropTable143 143
15035#define OP_DropIndex144 144
15036#define OP_Real145 145 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
15037#define OP_DropTrigger146 146
15038#define OP_IntegrityCk147 147
15039#define OP_RowSetAdd148 148 /* synopsis: rowset(P1)=r[P2] */
15040#define OP_Param149 149
15041#define OP_FkCounter150 150 /* synopsis: fkctr[P1]+=P2 */
15042#define OP_MemMax151 151 /* synopsis: r[P1]=max(r[P1],r[P2]) */
15043#define OP_OffsetLimit152 152 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
15044#define OP_AggInverse153 153 /* synopsis: accum=r[P3] inverse(r[P2@P5]) */
15045#define OP_AggStep154 154 /* synopsis: accum=r[P3] step(r[P2@P5]) */
15046#define OP_AggStep1155 155 /* synopsis: accum=r[P3] step(r[P2@P5]) */
15047#define OP_AggValue156 156 /* synopsis: r[P3]=value N=P2 */
15048#define OP_AggFinal157 157 /* synopsis: accum=r[P1] N=P2 */
15049#define OP_Expire158 158
15050#define OP_TableLock159 159 /* synopsis: iDb=P1 root=P2 write=P3 */
15051#define OP_VBegin160 160
15052#define OP_VCreate161 161
15053#define OP_VDestroy162 162
15054#define OP_VOpen163 163
15055#define OP_VColumn164 164 /* synopsis: r[P3]=vcolumn(P2) */
15056#define OP_VRename165 165
15057#define OP_Pagecount166 166
15058#define OP_MaxPgcnt167 167
15059#define OP_Trace168 168
15060#define OP_CursorHint169 169
15061#define OP_Noop170 170
15062#define OP_Explain171 171
15063#define OP_Abortable172 172
15064
15065/* Properties such as "out2" or "jump" that are specified in
15066** comments following the "case" for each opcode in the vdbe.c
15067** are encoded into bitvectors as follows:
15068*/
15069#define OPFLG_JUMP0x01 0x01 /* jump: P2 holds jmp target */
15070#define OPFLG_IN10x02 0x02 /* in1: P1 is an input */
15071#define OPFLG_IN20x04 0x04 /* in2: P2 is an input */
15072#define OPFLG_IN30x08 0x08 /* in3: P3 is an input */
15073#define OPFLG_OUT20x10 0x10 /* out2: P2 is an output */
15074#define OPFLG_OUT30x20 0x20 /* out3: P3 is an output */
15075#define OPFLG_INITIALIZER{ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10, 0x00, 0x01,
0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01, 0x03, 0x12, 0x03
, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x23
, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00
, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10, 0x10,
0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x02
, 0x02, 0x02, 0x00, 0x00, 0x12, 0x20, 0x00, 0x00, 0x00, 0x10,
0x10, 0x00, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26
, 0x26, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,
0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00
, 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10
, 0x00, 0x00, 0x00, 0x00, 0x00,}
{\
15076/* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10,\
15077/* 8 */ 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03,\
15078/* 16 */ 0x01, 0x01, 0x03, 0x12, 0x03, 0x01, 0x09, 0x09,\
15079/* 24 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,\
15080/* 32 */ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,\
15081/* 40 */ 0x01, 0x23, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03,\
15082/* 48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
15083/* 56 */ 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,\
15084/* 64 */ 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10,\
15085/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
15086/* 80 */ 0x10, 0x00, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,\
15087/* 88 */ 0x12, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
15088/* 96 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
15089/* 104 */ 0x26, 0x26, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00,\
15090/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15091/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15092/* 128 */ 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,\
15093/* 136 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
15094/* 144 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
15095/* 152 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
15096/* 160 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10,\
15097/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00,}
15098
15099/* The sqlite3P2Values() routine is able to run faster if it knows
15100** the value of the largest JUMP opcode. The smaller the maximum
15101** JUMP opcode the better, so the mkopcodeh.tcl script that
15102** generated this include file strives to group all JUMP opcodes
15103** together near the beginning of the list.
15104*/
15105#define SQLITE_MX_JUMP_OPCODE61 61 /* Maximum JUMP opcode */
15106
15107/************** End of opcodes.h *********************************************/
15108/************** Continuing where we left off in vdbe.h ***********************/
15109
15110/*
15111** Additional non-public SQLITE_PREPARE_* flags
15112*/
15113#define SQLITE_PREPARE_SAVESQL0x80 0x80 /* Preserve SQL text */
15114#define SQLITE_PREPARE_MASK0x0f 0x0f /* Mask of public flags */
15115
15116/*
15117** Prototypes for the VDBE interface. See comments on the implementation
15118** for a description of what each of these routines does.
15119*/
15120SQLITE_PRIVATEstatic Vdbe *sqlite3VdbeCreate(Parse*);
15121SQLITE_PRIVATEstatic int sqlite3VdbeAddOp0(Vdbe*,int);
15122SQLITE_PRIVATEstatic int sqlite3VdbeAddOp1(Vdbe*,int,int);
15123SQLITE_PRIVATEstatic int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
15124SQLITE_PRIVATEstatic int sqlite3VdbeGoto(Vdbe*,int);
15125SQLITE_PRIVATEstatic int sqlite3VdbeLoadString(Vdbe*,int,const char*);
15126SQLITE_PRIVATEstatic void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
15127SQLITE_PRIVATEstatic int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
15128SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
15129SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
15130SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
15131SQLITE_PRIVATEstatic void sqlite3VdbeEndCoroutine(Vdbe*,int);
15132#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
15133SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
15134SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
15135#else
15136# define sqlite3VdbeVerifyNoMallocRequired(A,B)
15137# define sqlite3VdbeVerifyNoResultRow(A)
15138#endif
15139#if defined(SQLITE_DEBUG)
15140SQLITE_PRIVATEstatic void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
15141#else
15142# define sqlite3VdbeVerifyAbortable(A,B)
15143#endif
15144SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
15145#ifndef SQLITE_OMIT_EXPLAIN
15146SQLITE_PRIVATEstatic void sqlite3VdbeExplain(Parse*,u8,const char*,...);
15147SQLITE_PRIVATEstatic void sqlite3VdbeExplainPop(Parse*);
15148SQLITE_PRIVATEstatic int sqlite3VdbeExplainParent(Parse*);
15149# define ExplainQueryPlan(P)sqlite3VdbeExplain P sqlite3VdbeExplain P
15150# define ExplainQueryPlanPop(P)sqlite3VdbeExplainPop(P) sqlite3VdbeExplainPop(P)
15151# define ExplainQueryPlanParent(P)sqlite3VdbeExplainParent(P) sqlite3VdbeExplainParent(P)
15152#else
15153# define ExplainQueryPlan(P)sqlite3VdbeExplain P
15154# define ExplainQueryPlanPop(P)sqlite3VdbeExplainPop(P)
15155# define ExplainQueryPlanParent(P)sqlite3VdbeExplainParent(P) 0
15156# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15157#endif
15158#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
15159SQLITE_PRIVATEstatic void sqlite3ExplainBreakpoint(const char*,const char*);
15160#else
15161# define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15162#endif
15163SQLITE_PRIVATEstatic void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
15164SQLITE_PRIVATEstatic void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
15165SQLITE_PRIVATEstatic void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
15166SQLITE_PRIVATEstatic void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
15167SQLITE_PRIVATEstatic void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
15168SQLITE_PRIVATEstatic void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
15169SQLITE_PRIVATEstatic void sqlite3VdbeJumpHere(Vdbe*, int addr);
15170SQLITE_PRIVATEstatic int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
15171SQLITE_PRIVATEstatic int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
15172SQLITE_PRIVATEstatic void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
15173SQLITE_PRIVATEstatic void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
15174SQLITE_PRIVATEstatic void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
15175SQLITE_PRIVATEstatic void sqlite3VdbeUsesBtree(Vdbe*, int);
15176SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
15177SQLITE_PRIVATEstatic int sqlite3VdbeMakeLabel(Parse*);
15178SQLITE_PRIVATEstatic void sqlite3VdbeRunOnlyOnce(Vdbe*);
15179SQLITE_PRIVATEstatic void sqlite3VdbeReusable(Vdbe*);
15180SQLITE_PRIVATEstatic void sqlite3VdbeDelete(Vdbe*);
15181SQLITE_PRIVATEstatic void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
15182SQLITE_PRIVATEstatic void sqlite3VdbeMakeReady(Vdbe*,Parse*);
15183SQLITE_PRIVATEstatic int sqlite3VdbeFinalize(Vdbe*);
15184SQLITE_PRIVATEstatic void sqlite3VdbeResolveLabel(Vdbe*, int);
15185SQLITE_PRIVATEstatic int sqlite3VdbeCurrentAddr(Vdbe*);
15186#ifdef SQLITE_DEBUG
15187SQLITE_PRIVATEstatic int sqlite3VdbeAssertMayAbort(Vdbe *, int);
15188#endif
15189SQLITE_PRIVATEstatic void sqlite3VdbeResetStepResult(Vdbe*);
15190SQLITE_PRIVATEstatic void sqlite3VdbeRewind(Vdbe*);
15191SQLITE_PRIVATEstatic int sqlite3VdbeReset(Vdbe*);
15192SQLITE_PRIVATEstatic void sqlite3VdbeSetNumCols(Vdbe*,int);
15193SQLITE_PRIVATEstatic int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
15194SQLITE_PRIVATEstatic void sqlite3VdbeCountChanges(Vdbe*);
15195SQLITE_PRIVATEstatic sqlite3 *sqlite3VdbeDb(Vdbe*);
15196SQLITE_PRIVATEstatic u8 sqlite3VdbePrepareFlags(Vdbe*);
15197SQLITE_PRIVATEstatic void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
15198#ifdef SQLITE_ENABLE_NORMALIZE
15199SQLITE_PRIVATEstatic void sqlite3VdbeAddDblquoteStr(sqlite3*,Vdbe*,const char*);
15200SQLITE_PRIVATEstatic int sqlite3VdbeUsesDoubleQuotedString(Vdbe*,const char*);
15201#endif
15202SQLITE_PRIVATEstatic void sqlite3VdbeSwap(Vdbe*,Vdbe*);
15203SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
15204SQLITE_PRIVATEstatic sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
15205SQLITE_PRIVATEstatic void sqlite3VdbeSetVarmask(Vdbe*, int);
15206#ifndef SQLITE_OMIT_TRACE
15207SQLITE_PRIVATEstatic char *sqlite3VdbeExpandSql(Vdbe*, const char*);
15208#endif
15209SQLITE_PRIVATEstatic int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
15210SQLITE_PRIVATEstatic int sqlite3BlobCompare(const Mem*, const Mem*);
15211
15212SQLITE_PRIVATEstatic void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
15213SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
15214SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
15215SQLITE_PRIVATEstatic UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
15216
15217typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
15218SQLITE_PRIVATEstatic RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
15219
15220#ifndef SQLITE_OMIT_TRIGGER
15221SQLITE_PRIVATEstatic void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
15222#endif
15223
15224SQLITE_PRIVATEstatic int sqlite3NotPureFunc(sqlite3_context*);
15225
15226/* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
15227** each VDBE opcode.
15228**
15229** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
15230** comments in VDBE programs that show key decision points in the code
15231** generator.
15232*/
15233#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
15234SQLITE_PRIVATEstatic void sqlite3VdbeComment(Vdbe*, const char*, ...);
15235# define VdbeComment(X) sqlite3VdbeComment X
15236SQLITE_PRIVATEstatic void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
15237# define VdbeNoopComment(X) sqlite3VdbeNoopComment X
15238# ifdef SQLITE_ENABLE_MODULE_COMMENTS
15239# define VdbeModuleComment(X) sqlite3VdbeNoopComment X
15240# else
15241# define VdbeModuleComment(X)
15242# endif
15243#else
15244# define VdbeComment(X)
15245# define VdbeNoopComment(X)
15246# define VdbeModuleComment(X)
15247#endif
15248
15249/*
15250** The VdbeCoverage macros are used to set a coverage testing point
15251** for VDBE branch instructions. The coverage testing points are line
15252** numbers in the sqlite3.c source file. VDBE branch coverage testing
15253** only works with an amalagmation build. That's ok since a VDBE branch
15254** coverage build designed for testing the test suite only. No application
15255** should ever ship with VDBE branch coverage measuring turned on.
15256**
15257** VdbeCoverage(v) // Mark the previously coded instruction
15258** // as a branch
15259**
15260** VdbeCoverageIf(v, conditional) // Mark previous if conditional true
15261**
15262** VdbeCoverageAlwaysTaken(v) // Previous branch is always taken
15263**
15264** VdbeCoverageNeverTaken(v) // Previous branch is never taken
15265**
15266** VdbeCoverageNeverNull(v) // Previous three-way branch is only
15267** // taken on the first two ways. The
15268** // NULL option is not possible
15269**
15270** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
15271** // in distingishing equal and not-equal.
15272**
15273** Every VDBE branch operation must be tagged with one of the macros above.
15274** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
15275** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
15276** routine in vdbe.c, alerting the developer to the missed tag.
15277**
15278** During testing, the test application will invoke
15279** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
15280** routine that is invoked as each bytecode branch is taken. The callback
15281** contains the sqlite3.c source line number ov the VdbeCoverage macro and
15282** flags to indicate whether or not the branch was taken. The test application
15283** is responsible for keeping track of this and reporting byte-code branches
15284** that are never taken.
15285**
15286** See the VdbeBranchTaken() macro and vdbeTakeBranch() function in the
15287** vdbe.c source file for additional information.
15288*/
15289#ifdef SQLITE_VDBE_COVERAGE
15290SQLITE_PRIVATEstatic void sqlite3VdbeSetLineNumber(Vdbe*,int);
15291# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__15291)
15292# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__15292)
15293# define VdbeCoverageAlwaysTaken(v) \
15294 sqlite3VdbeSetLineNumber(v,__LINE__15294|0x5000000);
15295# define VdbeCoverageNeverTaken(v) \
15296 sqlite3VdbeSetLineNumber(v,__LINE__15296|0x6000000);
15297# define VdbeCoverageNeverNull(v) \
15298 sqlite3VdbeSetLineNumber(v,__LINE__15298|0x4000000);
15299# define VdbeCoverageNeverNullIf(v,x) \
15300 if(x)sqlite3VdbeSetLineNumber(v,__LINE__15300|0x4000000);
15301# define VdbeCoverageEqNe(v) \
15302 sqlite3VdbeSetLineNumber(v,__LINE__15302|0x8000000);
15303# define VDBE_OFFSET_LINENO(x)0 (__LINE__15303+x)
15304#else
15305# define VdbeCoverage(v)
15306# define VdbeCoverageIf(v,x)
15307# define VdbeCoverageAlwaysTaken(v)
15308# define VdbeCoverageNeverTaken(v)
15309# define VdbeCoverageNeverNull(v)
15310# define VdbeCoverageNeverNullIf(v,x)
15311# define VdbeCoverageEqNe(v)
15312# define VDBE_OFFSET_LINENO(x)0 0
15313#endif
15314
15315#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
15316SQLITE_PRIVATEstatic void sqlite3VdbeScanStatus(Vdbe*, int, int, int, LogEst, const char*);
15317#else
15318# define sqlite3VdbeScanStatus(a,b,c,d,e)
15319#endif
15320
15321#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
15322SQLITE_PRIVATEstatic void sqlite3VdbePrintOp(FILE*, int, VdbeOp*);
15323#endif
15324
15325#endif /* SQLITE_VDBE_H */
15326
15327/************** End of vdbe.h ************************************************/
15328/************** Continuing where we left off in sqliteInt.h ******************/
15329/************** Include pager.h in the middle of sqliteInt.h *****************/
15330/************** Begin file pager.h *******************************************/
15331/*
15332** 2001 September 15
15333**
15334** The author disclaims copyright to this source code. In place of
15335** a legal notice, here is a blessing:
15336**
15337** May you do good and not evil.
15338** May you find forgiveness for yourself and forgive others.
15339** May you share freely, never taking more than you give.
15340**
15341*************************************************************************
15342** This header file defines the interface that the sqlite page cache
15343** subsystem. The page cache subsystem reads and writes a file a page
15344** at a time and provides a journal for rollback.
15345*/
15346
15347#ifndef SQLITE_PAGER_H
15348#define SQLITE_PAGER_H
15349
15350/*
15351** Default maximum size for persistent journal files. A negative
15352** value means no limit. This value may be overridden using the
15353** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
15354*/
15355#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT-1
15356 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT-1 -1
15357#endif
15358
15359/*
15360** The type used to represent a page number. The first page in a file
15361** is called page 1. 0 is used to represent "not a page".
15362*/
15363typedef u32 Pgno;
15364
15365/*
15366** Each open file is managed by a separate instance of the "Pager" structure.
15367*/
15368typedef struct Pager Pager;
15369
15370/*
15371** Handle type for pages.
15372*/
15373typedef struct PgHdr DbPage;
15374
15375/*
15376** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
15377** reserved for working around a windows/posix incompatibility). It is
15378** used in the journal to signify that the remainder of the journal file
15379** is devoted to storing a master journal name - there are no more pages to
15380** roll back. See comments for function writeMasterJournal() in pager.c
15381** for details.
15382*/
15383#define PAGER_MJ_PGNO(x)((Pgno)((sqlite3PendingByte/((x)->pageSize))+1)) ((Pgno)((PENDING_BYTEsqlite3PendingByte/((x)->pageSize))+1))
15384
15385/*
15386** Allowed values for the flags parameter to sqlite3PagerOpen().
15387**
15388** NOTE: These values must match the corresponding BTREE_ values in btree.h.
15389*/
15390#define PAGER_OMIT_JOURNAL0x0001 0x0001 /* Do not use a rollback journal */
15391#define PAGER_MEMORY0x0002 0x0002 /* In-memory database */
15392
15393/*
15394** Valid values for the second argument to sqlite3PagerLockingMode().
15395*/
15396#define PAGER_LOCKINGMODE_QUERY-1 -1
15397#define PAGER_LOCKINGMODE_NORMAL0 0
15398#define PAGER_LOCKINGMODE_EXCLUSIVE1 1
15399
15400/*
15401** Numeric constants that encode the journalmode.
15402**
15403** The numeric values encoded here (other than PAGER_JOURNALMODE_QUERY)
15404** are exposed in the API via the "PRAGMA journal_mode" command and
15405** therefore cannot be changed without a compatibility break.
15406*/
15407#define PAGER_JOURNALMODE_QUERY(-1) (-1) /* Query the value of journalmode */
15408#define PAGER_JOURNALMODE_DELETE0 0 /* Commit by deleting journal file */
15409#define PAGER_JOURNALMODE_PERSIST1 1 /* Commit by zeroing journal header */
15410#define PAGER_JOURNALMODE_OFF2 2 /* Journal omitted. */
15411#define PAGER_JOURNALMODE_TRUNCATE3 3 /* Commit by truncating journal */
15412#define PAGER_JOURNALMODE_MEMORY4 4 /* In-memory journal file */
15413#define PAGER_JOURNALMODE_WAL5 5 /* Use write-ahead logging */
15414
15415/*
15416** Flags that make up the mask passed to sqlite3PagerGet().
15417*/
15418#define PAGER_GET_NOCONTENT0x01 0x01 /* Do not load data from disk */
15419#define PAGER_GET_READONLY0x02 0x02 /* Read-only page is acceptable */
15420
15421/*
15422** Flags for sqlite3PagerSetFlags()
15423**
15424** Value constraints (enforced via assert()):
15425** PAGER_FULLFSYNC == SQLITE_FullFSync
15426** PAGER_CKPT_FULLFSYNC == SQLITE_CkptFullFSync
15427** PAGER_CACHE_SPILL == SQLITE_CacheSpill
15428*/
15429#define PAGER_SYNCHRONOUS_OFF0x01 0x01 /* PRAGMA synchronous=OFF */
15430#define PAGER_SYNCHRONOUS_NORMAL0x02 0x02 /* PRAGMA synchronous=NORMAL */
15431#define PAGER_SYNCHRONOUS_FULL0x03 0x03 /* PRAGMA synchronous=FULL */
15432#define PAGER_SYNCHRONOUS_EXTRA0x04 0x04 /* PRAGMA synchronous=EXTRA */
15433#define PAGER_SYNCHRONOUS_MASK0x07 0x07 /* Mask for four values above */
15434#define PAGER_FULLFSYNC0x08 0x08 /* PRAGMA fullfsync=ON */
15435#define PAGER_CKPT_FULLFSYNC0x10 0x10 /* PRAGMA checkpoint_fullfsync=ON */
15436#define PAGER_CACHESPILL0x20 0x20 /* PRAGMA cache_spill=ON */
15437#define PAGER_FLAGS_MASK0x38 0x38 /* All above except SYNCHRONOUS */
15438
15439/*
15440** The remainder of this file contains the declarations of the functions
15441** that make up the Pager sub-system API. See source code comments for
15442** a detailed description of each routine.
15443*/
15444
15445/* Open and close a Pager connection. */
15446SQLITE_PRIVATEstatic int sqlite3PagerOpen(
15447 sqlite3_vfs*,
15448 Pager **ppPager,
15449 const char*,
15450 int,
15451 int,
15452 int,
15453 void(*)(DbPage*)
15454);
15455SQLITE_PRIVATEstatic int sqlite3PagerClose(Pager *pPager, sqlite3*);
15456SQLITE_PRIVATEstatic int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
15457
15458/* Functions used to configure a Pager object. */
15459SQLITE_PRIVATEstatic void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
15460SQLITE_PRIVATEstatic int sqlite3PagerSetPagesize(Pager*, u32*, int);
15461#ifdef SQLITE_HAS_CODEC
15462SQLITE_PRIVATEstatic void sqlite3PagerAlignReserve(Pager*,Pager*);
15463#endif
15464SQLITE_PRIVATEstatic int sqlite3PagerMaxPageCount(Pager*, int);
15465SQLITE_PRIVATEstatic void sqlite3PagerSetCachesize(Pager*, int);
15466SQLITE_PRIVATEstatic int sqlite3PagerSetSpillsize(Pager*, int);
15467SQLITE_PRIVATEstatic void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
15468SQLITE_PRIVATEstatic void sqlite3PagerShrink(Pager*);
15469SQLITE_PRIVATEstatic void sqlite3PagerSetFlags(Pager*,unsigned);
15470SQLITE_PRIVATEstatic int sqlite3PagerLockingMode(Pager *, int);
15471SQLITE_PRIVATEstatic int sqlite3PagerSetJournalMode(Pager *, int);
15472SQLITE_PRIVATEstatic int sqlite3PagerGetJournalMode(Pager*);
15473SQLITE_PRIVATEstatic int sqlite3PagerOkToChangeJournalMode(Pager*);
15474SQLITE_PRIVATEstatic i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
15475SQLITE_PRIVATEstatic sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
15476SQLITE_PRIVATEstatic int sqlite3PagerFlush(Pager*);
15477
15478/* Functions used to obtain and release page references. */
15479SQLITE_PRIVATEstatic int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
15480SQLITE_PRIVATEstatic DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
15481SQLITE_PRIVATEstatic void sqlite3PagerRef(DbPage*);
15482SQLITE_PRIVATEstatic void sqlite3PagerUnref(DbPage*);
15483SQLITE_PRIVATEstatic void sqlite3PagerUnrefNotNull(DbPage*);
15484SQLITE_PRIVATEstatic void sqlite3PagerUnrefPageOne(DbPage*);
15485
15486/* Operations on page references. */
15487SQLITE_PRIVATEstatic int sqlite3PagerWrite(DbPage*);
15488SQLITE_PRIVATEstatic void sqlite3PagerDontWrite(DbPage*);
15489SQLITE_PRIVATEstatic int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
15490SQLITE_PRIVATEstatic int sqlite3PagerPageRefcount(DbPage*);
15491SQLITE_PRIVATEstatic void *sqlite3PagerGetData(DbPage *);
15492SQLITE_PRIVATEstatic void *sqlite3PagerGetExtra(DbPage *);
15493
15494/* Functions used to manage pager transactions and savepoints. */
15495SQLITE_PRIVATEstatic void sqlite3PagerPagecount(Pager*, int*);
15496SQLITE_PRIVATEstatic int sqlite3PagerBegin(Pager*, int exFlag, int);
15497SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
15498SQLITE_PRIVATEstatic int sqlite3PagerExclusiveLock(Pager*);
15499SQLITE_PRIVATEstatic int sqlite3PagerSync(Pager *pPager, const char *zMaster);
15500SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseTwo(Pager*);
15501SQLITE_PRIVATEstatic int sqlite3PagerRollback(Pager*);
15502SQLITE_PRIVATEstatic int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
15503SQLITE_PRIVATEstatic int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
15504SQLITE_PRIVATEstatic int sqlite3PagerSharedLock(Pager *pPager);
15505
15506#ifndef SQLITE_OMIT_WAL
15507SQLITE_PRIVATEstatic int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
15508SQLITE_PRIVATEstatic int sqlite3PagerWalSupported(Pager *pPager);
15509SQLITE_PRIVATEstatic int sqlite3PagerWalCallback(Pager *pPager);
15510SQLITE_PRIVATEstatic int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
15511SQLITE_PRIVATEstatic int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
15512# ifdef SQLITE_ENABLE_SNAPSHOT
15513SQLITE_PRIVATEstatic int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot);
15514SQLITE_PRIVATEstatic int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot);
15515SQLITE_PRIVATEstatic int sqlite3PagerSnapshotRecover(Pager *pPager);
15516SQLITE_PRIVATEstatic int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot);
15517SQLITE_PRIVATEstatic void sqlite3PagerSnapshotUnlock(Pager *pPager);
15518# endif
15519#endif
15520
15521#ifdef SQLITE_DIRECT_OVERFLOW_READ
15522SQLITE_PRIVATEstatic int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno);
15523#endif
15524
15525#ifdef SQLITE_ENABLE_ZIPVFS
15526SQLITE_PRIVATEstatic int sqlite3PagerWalFramesize(Pager *pPager);
15527#endif
15528
15529/* Functions used to query pager state and configuration. */
15530SQLITE_PRIVATEstatic u8 sqlite3PagerIsreadonly(Pager*);
15531SQLITE_PRIVATEstatic u32 sqlite3PagerDataVersion(Pager*);
15532#ifdef SQLITE_DEBUG
15533SQLITE_PRIVATEstatic int sqlite3PagerRefcount(Pager*);
15534#endif
15535SQLITE_PRIVATEstatic int sqlite3PagerMemUsed(Pager*);
15536SQLITE_PRIVATEstatic const char *sqlite3PagerFilename(Pager*, int);
15537SQLITE_PRIVATEstatic sqlite3_vfs *sqlite3PagerVfs(Pager*);
15538SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerFile(Pager*);
15539SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerJrnlFile(Pager*);
15540SQLITE_PRIVATEstatic const char *sqlite3PagerJournalname(Pager*);
15541SQLITE_PRIVATEstatic void *sqlite3PagerTempSpace(Pager*);
15542SQLITE_PRIVATEstatic int sqlite3PagerIsMemdb(Pager*);
15543SQLITE_PRIVATEstatic void sqlite3PagerCacheStat(Pager *, int, int, int *);
15544SQLITE_PRIVATEstatic void sqlite3PagerClearCache(Pager*);
15545SQLITE_PRIVATEstatic int sqlite3SectorSize(sqlite3_file *);
15546#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
15547SQLITE_PRIVATEstatic void sqlite3PagerResetLockTimeout(Pager *pPager);
15548#else
15549# define sqlite3PagerResetLockTimeout(X)
15550#endif
15551
15552/* Functions used to truncate the database file. */
15553SQLITE_PRIVATEstatic void sqlite3PagerTruncateImage(Pager*,Pgno);
15554
15555SQLITE_PRIVATEstatic void sqlite3PagerRekey(DbPage*, Pgno, u16);
15556
15557#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
15558SQLITE_PRIVATEstatic void *sqlite3PagerCodec(DbPage *);
15559#endif
15560
15561/* Functions to support testing and debugging. */
15562#if !defined(NDEBUG1) || defined(SQLITE_TEST)
15563SQLITE_PRIVATEstatic Pgno sqlite3PagerPagenumber(DbPage*);
15564SQLITE_PRIVATEstatic int sqlite3PagerIswriteable(DbPage*);
15565#endif
15566#ifdef SQLITE_TEST
15567SQLITE_PRIVATEstatic int *sqlite3PagerStats(Pager*);
15568SQLITE_PRIVATEstatic void sqlite3PagerRefdump(Pager*);
15569 void disable_simulated_io_errors(void);
15570 void enable_simulated_io_errors(void);
15571#else
15572# define disable_simulated_io_errors()
15573# define enable_simulated_io_errors()
15574#endif
15575
15576#endif /* SQLITE_PAGER_H */
15577
15578/************** End of pager.h ***********************************************/
15579/************** Continuing where we left off in sqliteInt.h ******************/
15580/************** Include pcache.h in the middle of sqliteInt.h ****************/
15581/************** Begin file pcache.h ******************************************/
15582/*
15583** 2008 August 05
15584**
15585** The author disclaims copyright to this source code. In place of
15586** a legal notice, here is a blessing:
15587**
15588** May you do good and not evil.
15589** May you find forgiveness for yourself and forgive others.
15590** May you share freely, never taking more than you give.
15591**
15592*************************************************************************
15593** This header file defines the interface that the sqlite page cache
15594** subsystem.
15595*/
15596
15597#ifndef _PCACHE_H_
15598
15599typedef struct PgHdr PgHdr;
15600typedef struct PCache PCache;
15601
15602/*
15603** Every page in the cache is controlled by an instance of the following
15604** structure.
15605*/
15606struct PgHdr {
15607 sqlite3_pcache_page *pPage; /* Pcache object page handle */
15608 void *pData; /* Page data */
15609 void *pExtra; /* Extra content */
15610 PCache *pCache; /* PRIVATE: Cache that owns this page */
15611 PgHdr *pDirty; /* Transient list of dirty sorted by pgno */
15612 Pager *pPager; /* The pager this page is part of */
15613 Pgno pgno; /* Page number for this page */
15614#ifdef SQLITE_CHECK_PAGES
15615 u32 pageHash; /* Hash of page content */
15616#endif
15617 u16 flags; /* PGHDR flags defined below */
15618
15619 /**********************************************************************
15620 ** Elements above, except pCache, are public. All that follow are
15621 ** private to pcache.c and should not be accessed by other modules.
15622 ** pCache is grouped with the public elements for efficiency.
15623 */
15624 i16 nRef; /* Number of users of this page */
15625 PgHdr *pDirtyNext; /* Next element in list of dirty pages */
15626 PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
15627 /* NB: pDirtyNext and pDirtyPrev are undefined if the
15628 ** PgHdr object is not dirty */
15629};
15630
15631/* Bit values for PgHdr.flags */
15632#define PGHDR_CLEAN0x001 0x001 /* Page not on the PCache.pDirty list */
15633#define PGHDR_DIRTY0x002 0x002 /* Page is on the PCache.pDirty list */
15634#define PGHDR_WRITEABLE0x004 0x004 /* Journaled and ready to modify */
15635#define PGHDR_NEED_SYNC0x008 0x008 /* Fsync the rollback journal before
15636 ** writing this page to the database */
15637#define PGHDR_DONT_WRITE0x010 0x010 /* Do not write content to disk */
15638#define PGHDR_MMAP0x020 0x020 /* This is an mmap page object */
15639
15640#define PGHDR_WAL_APPEND0x040 0x040 /* Appended to wal file */
15641
15642/* Initialize and shutdown the page cache subsystem */
15643SQLITE_PRIVATEstatic int sqlite3PcacheInitialize(void);
15644SQLITE_PRIVATEstatic void sqlite3PcacheShutdown(void);
15645
15646/* Page cache buffer management:
15647** These routines implement SQLITE_CONFIG_PAGECACHE.
15648*/
15649SQLITE_PRIVATEstatic void sqlite3PCacheBufferSetup(void *, int sz, int n);
15650
15651/* Create a new pager cache.
15652** Under memory stress, invoke xStress to try to make pages clean.
15653** Only clean and unpinned pages can be reclaimed.
15654*/
15655SQLITE_PRIVATEstatic int sqlite3PcacheOpen(
15656 int szPage, /* Size of every page */
15657 int szExtra, /* Extra space associated with each page */
15658 int bPurgeable, /* True if pages are on backing store */
15659 int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
15660 void *pStress, /* Argument to xStress */
15661 PCache *pToInit /* Preallocated space for the PCache */
15662);
15663
15664/* Modify the page-size after the cache has been created. */
15665SQLITE_PRIVATEstatic int sqlite3PcacheSetPageSize(PCache *, int);
15666
15667/* Return the size in bytes of a PCache object. Used to preallocate
15668** storage space.
15669*/
15670SQLITE_PRIVATEstatic int sqlite3PcacheSize(void);
15671
15672/* One release per successful fetch. Page is pinned until released.
15673** Reference counted.
15674*/
15675SQLITE_PRIVATEstatic sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
15676SQLITE_PRIVATEstatic int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
15677SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
15678SQLITE_PRIVATEstatic void sqlite3PcacheRelease(PgHdr*);
15679
15680SQLITE_PRIVATEstatic void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
15681SQLITE_PRIVATEstatic void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
15682SQLITE_PRIVATEstatic void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
15683SQLITE_PRIVATEstatic void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
15684SQLITE_PRIVATEstatic void sqlite3PcacheClearWritable(PCache*);
15685
15686/* Change a page number. Used by incr-vacuum. */
15687SQLITE_PRIVATEstatic void sqlite3PcacheMove(PgHdr*, Pgno);
15688
15689/* Remove all pages with pgno>x. Reset the cache if x==0 */
15690SQLITE_PRIVATEstatic void sqlite3PcacheTruncate(PCache*, Pgno x);
15691
15692/* Get a list of all dirty pages in the cache, sorted by page number */
15693SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheDirtyList(PCache*);
15694
15695/* Reset and close the cache object */
15696SQLITE_PRIVATEstatic void sqlite3PcacheClose(PCache*);
15697
15698/* Clear flags from pages of the page cache */
15699SQLITE_PRIVATEstatic void sqlite3PcacheClearSyncFlags(PCache *);
15700
15701/* Discard the contents of the cache */
15702SQLITE_PRIVATEstatic void sqlite3PcacheClear(PCache*);
15703
15704/* Return the total number of outstanding page references */
15705SQLITE_PRIVATEstatic int sqlite3PcacheRefCount(PCache*);
15706
15707/* Increment the reference count of an existing page */
15708SQLITE_PRIVATEstatic void sqlite3PcacheRef(PgHdr*);
15709
15710SQLITE_PRIVATEstatic int sqlite3PcachePageRefcount(PgHdr*);
15711
15712/* Return the total number of pages stored in the cache */
15713SQLITE_PRIVATEstatic int sqlite3PcachePagecount(PCache*);
15714
15715#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
15716/* Iterate through all dirty pages currently stored in the cache. This
15717** interface is only available if SQLITE_CHECK_PAGES is defined when the
15718** library is built.
15719*/
15720SQLITE_PRIVATEstatic void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
15721#endif
15722
15723#if defined(SQLITE_DEBUG)
15724/* Check invariants on a PgHdr object */
15725SQLITE_PRIVATEstatic int sqlite3PcachePageSanity(PgHdr*);
15726#endif
15727
15728/* Set and get the suggested cache-size for the specified pager-cache.
15729**
15730** If no global maximum is configured, then the system attempts to limit
15731** the total number of pages cached by purgeable pager-caches to the sum
15732** of the suggested cache-sizes.
15733*/
15734SQLITE_PRIVATEstatic void sqlite3PcacheSetCachesize(PCache *, int);
15735#ifdef SQLITE_TEST
15736SQLITE_PRIVATEstatic int sqlite3PcacheGetCachesize(PCache *);
15737#endif
15738
15739/* Set or get the suggested spill-size for the specified pager-cache.
15740**
15741** The spill-size is the minimum number of pages in cache before the cache
15742** will attempt to spill dirty pages by calling xStress.
15743*/
15744SQLITE_PRIVATEstatic int sqlite3PcacheSetSpillsize(PCache *, int);
15745
15746/* Free up as much memory as possible from the page cache */
15747SQLITE_PRIVATEstatic void sqlite3PcacheShrink(PCache*);
15748
15749#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
15750/* Try to return memory used by the pcache module to the main memory heap */
15751SQLITE_PRIVATEstatic int sqlite3PcacheReleaseMemory(int);
15752#endif
15753
15754#ifdef SQLITE_TEST
15755SQLITE_PRIVATEstatic void sqlite3PcacheStats(int*,int*,int*,int*);
15756#endif
15757
15758SQLITE_PRIVATEstatic void sqlite3PCacheSetDefault(void);
15759
15760/* Return the header size */
15761SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache(void);
15762SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache1(void);
15763
15764/* Number of dirty pages as a percentage of the configured cache size */
15765SQLITE_PRIVATEstatic int sqlite3PCachePercentDirty(PCache*);
15766
15767#ifdef SQLITE_DIRECT_OVERFLOW_READ
15768SQLITE_PRIVATEstatic int sqlite3PCacheIsDirty(PCache *pCache);
15769#endif
15770
15771#endif /* _PCACHE_H_ */
15772
15773/************** End of pcache.h **********************************************/
15774/************** Continuing where we left off in sqliteInt.h ******************/
15775/************** Include os.h in the middle of sqliteInt.h ********************/
15776/************** Begin file os.h **********************************************/
15777/*
15778** 2001 September 16
15779**
15780** The author disclaims copyright to this source code. In place of
15781** a legal notice, here is a blessing:
15782**
15783** May you do good and not evil.
15784** May you find forgiveness for yourself and forgive others.
15785** May you share freely, never taking more than you give.
15786**
15787******************************************************************************
15788**
15789** This header file (together with is companion C source-code file
15790** "os.c") attempt to abstract the underlying operating system so that
15791** the SQLite library will work on both POSIX and windows systems.
15792**
15793** This header file is #include-ed by sqliteInt.h and thus ends up
15794** being included by every source file.
15795*/
15796#ifndef _SQLITE_OS_H_
15797#define _SQLITE_OS_H_
15798
15799/*
15800** Attempt to automatically detect the operating system and setup the
15801** necessary pre-processor macros for it.
15802*/
15803/************** Include os_setup.h in the middle of os.h *********************/
15804/************** Begin file os_setup.h ****************************************/
15805/*
15806** 2013 November 25
15807**
15808** The author disclaims copyright to this source code. In place of
15809** a legal notice, here is a blessing:
15810**
15811** May you do good and not evil.
15812** May you find forgiveness for yourself and forgive others.
15813** May you share freely, never taking more than you give.
15814**
15815******************************************************************************
15816**
15817** This file contains pre-processor directives related to operating system
15818** detection and/or setup.
15819*/
15820#ifndef SQLITE_OS_SETUP_H
15821#define SQLITE_OS_SETUP_H
15822
15823/*
15824** Figure out if we are dealing with Unix, Windows, or some other operating
15825** system.
15826**
15827** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
15828** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
15829** the three will be 1. The other two will be 0.
15830*/
15831#if defined(SQLITE_OS_OTHER0)
15832# if SQLITE_OS_OTHER0==1
15833# undef SQLITE_OS_UNIX1
15834# define SQLITE_OS_UNIX1 0
15835# undef SQLITE_OS_WIN0
15836# define SQLITE_OS_WIN0 0
15837# else
15838# undef SQLITE_OS_OTHER0
15839# endif
15840#endif
15841#if !defined(SQLITE_OS_UNIX1) && !defined(SQLITE_OS_OTHER0)
15842# define SQLITE_OS_OTHER0 0
15843# ifndef SQLITE_OS_WIN0
15844# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
15845 defined(__MINGW32__) || defined(__BORLANDC__)
15846# define SQLITE_OS_WIN0 1
15847# define SQLITE_OS_UNIX1 0
15848# else
15849# define SQLITE_OS_WIN0 0
15850# define SQLITE_OS_UNIX1 1
15851# endif
15852# else
15853# define SQLITE_OS_UNIX1 0
15854# endif
15855#else
15856# ifndef SQLITE_OS_WIN0
15857# define SQLITE_OS_WIN0 0
15858# endif
15859#endif
15860
15861#endif /* SQLITE_OS_SETUP_H */
15862
15863/************** End of os_setup.h ********************************************/
15864/************** Continuing where we left off in os.h *************************/
15865
15866/* If the SET_FULLSYNC macro is not defined above, then make it
15867** a no-op
15868*/
15869#ifndef SET_FULLSYNC
15870# define SET_FULLSYNC(x,y)
15871#endif
15872
15873/*
15874** The default size of a disk sector
15875*/
15876#ifndef SQLITE_DEFAULT_SECTOR_SIZE4096
15877# define SQLITE_DEFAULT_SECTOR_SIZE4096 4096
15878#endif
15879
15880/*
15881** Temporary files are named starting with this prefix followed by 16 random
15882** alphanumeric characters, and no file extension. They are stored in the
15883** OS's standard temporary file directory, and are deleted prior to exit.
15884** If sqlite is being embedded in another program, you may wish to change the
15885** prefix to reflect your program's name, so that if your program exits
15886** prematurely, old temporary files can be easily identified. This can be done
15887** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15888**
15889** 2006-10-31: The default prefix used to be "sqlite_". But then
15890** Mcafee started using SQLite in their anti-virus product and it
15891** started putting files with the "sqlite" name in the c:/temp folder.
15892** This annoyed many windows users. Those users would then do a
15893** Google search for "sqlite", find the telephone numbers of the
15894** developers and call to wake them up at night and complain.
15895** For this reason, the default name prefix is changed to be "sqlite"
15896** spelled backwards. So the temp files are still identified, but
15897** anybody smart enough to figure out the code is also likely smart
15898** enough to know that calling the developer will not help get rid
15899** of the file.
15900*/
15901#ifndef SQLITE_TEMP_FILE_PREFIX"etilqs_"
15902# define SQLITE_TEMP_FILE_PREFIX"etilqs_" "etilqs_"
15903#endif
15904
15905/*
15906** The following values may be passed as the second argument to
15907** sqlite3OsLock(). The various locks exhibit the following semantics:
15908**
15909** SHARED: Any number of processes may hold a SHARED lock simultaneously.
15910** RESERVED: A single process may hold a RESERVED lock on a file at
15911** any time. Other processes may hold and obtain new SHARED locks.
15912** PENDING: A single process may hold a PENDING lock on a file at
15913** any one time. Existing SHARED locks may persist, but no new
15914** SHARED locks may be obtained by other processes.
15915** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
15916**
15917** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
15918** process that requests an EXCLUSIVE lock may actually obtain a PENDING
15919** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
15920** sqlite3OsLock().
15921*/
15922#define NO_LOCK0 0
15923#define SHARED_LOCK1 1
15924#define RESERVED_LOCK2 2
15925#define PENDING_LOCK3 3
15926#define EXCLUSIVE_LOCK4 4
15927
15928/*
15929** File Locking Notes: (Mostly about windows but also some info for Unix)
15930**
15931** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
15932** those functions are not available. So we use only LockFile() and
15933** UnlockFile().
15934**
15935** LockFile() prevents not just writing but also reading by other processes.
15936** A SHARED_LOCK is obtained by locking a single randomly-chosen
15937** byte out of a specific range of bytes. The lock byte is obtained at
15938** random so two separate readers can probably access the file at the
15939** same time, unless they are unlucky and choose the same lock byte.
15940** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
15941** There can only be one writer. A RESERVED_LOCK is obtained by locking
15942** a single byte of the file that is designated as the reserved lock byte.
15943** A PENDING_LOCK is obtained by locking a designated byte different from
15944** the RESERVED_LOCK byte.
15945**
15946** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
15947** which means we can use reader/writer locks. When reader/writer locks
15948** are used, the lock is placed on the same range of bytes that is used
15949** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
15950** will support two or more Win95 readers or two or more WinNT readers.
15951** But a single Win95 reader will lock out all WinNT readers and a single
15952** WinNT reader will lock out all other Win95 readers.
15953**
15954** The following #defines specify the range of bytes used for locking.
15955** SHARED_SIZE is the number of bytes available in the pool from which
15956** a random byte is selected for a shared lock. The pool of bytes for
15957** shared locks begins at SHARED_FIRST.
15958**
15959** The same locking strategy and
15960** byte ranges are used for Unix. This leaves open the possibility of having
15961** clients on win95, winNT, and unix all talking to the same shared file
15962** and all locking correctly. To do so would require that samba (or whatever
15963** tool is being used for file sharing) implements locks correctly between
15964** windows and unix. I'm guessing that isn't likely to happen, but by
15965** using the same locking range we are at least open to the possibility.
15966**
15967** Locking in windows is manditory. For this reason, we cannot store
15968** actual data in the bytes used for locking. The pager never allocates
15969** the pages involved in locking therefore. SHARED_SIZE is selected so
15970** that all locks will fit on a single page even at the minimum page size.
15971** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
15972** is set high so that we don't have to allocate an unused page except
15973** for very large databases. But one should test the page skipping logic
15974** by setting PENDING_BYTE low and running the entire regression suite.
15975**
15976** Changing the value of PENDING_BYTE results in a subtly incompatible
15977** file format. Depending on how it is changed, you might not notice
15978** the incompatibility right away, even running a full regression test.
15979** The default location of PENDING_BYTE is the first byte past the
15980** 1GB boundary.
15981**
15982*/
15983#ifdef SQLITE_OMIT_WSD
15984# define PENDING_BYTEsqlite3PendingByte (0x40000000)
15985#else
15986# define PENDING_BYTEsqlite3PendingByte sqlite3PendingByte
15987#endif
15988#define RESERVED_BYTE(sqlite3PendingByte+1) (PENDING_BYTEsqlite3PendingByte+1)
15989#define SHARED_FIRST(sqlite3PendingByte+2) (PENDING_BYTEsqlite3PendingByte+2)
15990#define SHARED_SIZE510 510
15991
15992/*
15993** Wrapper around OS specific sqlite3_os_init() function.
15994*/
15995SQLITE_PRIVATEstatic int sqlite3OsInit(void);
15996
15997/*
15998** Functions for accessing sqlite3_file methods
15999*/
16000SQLITE_PRIVATEstatic void sqlite3OsClose(sqlite3_file*);
16001SQLITE_PRIVATEstatic int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
16002SQLITE_PRIVATEstatic int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
16003SQLITE_PRIVATEstatic int sqlite3OsTruncate(sqlite3_file*, i64 size);
16004SQLITE_PRIVATEstatic int sqlite3OsSync(sqlite3_file*, int);
16005SQLITE_PRIVATEstatic int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
16006SQLITE_PRIVATEstatic int sqlite3OsLock(sqlite3_file*, int);
16007SQLITE_PRIVATEstatic int sqlite3OsUnlock(sqlite3_file*, int);
16008SQLITE_PRIVATEstatic int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
16009SQLITE_PRIVATEstatic int sqlite3OsFileControl(sqlite3_file*,int,void*);
16010SQLITE_PRIVATEstatic void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
16011#define SQLITE_FCNTL_DB_UNCHANGED0xca093fa0 0xca093fa0
16012SQLITE_PRIVATEstatic int sqlite3OsSectorSize(sqlite3_file *id);
16013SQLITE_PRIVATEstatic int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
16014#ifndef SQLITE_OMIT_WAL
16015SQLITE_PRIVATEstatic int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
16016SQLITE_PRIVATEstatic int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
16017SQLITE_PRIVATEstatic void sqlite3OsShmBarrier(sqlite3_file *id);
16018SQLITE_PRIVATEstatic int sqlite3OsShmUnmap(sqlite3_file *id, int);
16019#endif /* SQLITE_OMIT_WAL */
16020SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
16021SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
16022
16023
16024/*
16025** Functions for accessing sqlite3_vfs methods
16026*/
16027SQLITE_PRIVATEstatic int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
16028SQLITE_PRIVATEstatic int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
16029SQLITE_PRIVATEstatic int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
16030SQLITE_PRIVATEstatic int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
16031#ifndef SQLITE_OMIT_LOAD_EXTENSION
16032SQLITE_PRIVATEstatic void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
16033SQLITE_PRIVATEstatic void sqlite3OsDlError(sqlite3_vfs *, int, char *);
16034SQLITE_PRIVATEstatic void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
16035SQLITE_PRIVATEstatic void sqlite3OsDlClose(sqlite3_vfs *, void *);
16036#endif /* SQLITE_OMIT_LOAD_EXTENSION */
16037SQLITE_PRIVATEstatic int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
16038SQLITE_PRIVATEstatic int sqlite3OsSleep(sqlite3_vfs *, int);
16039SQLITE_PRIVATEstatic int sqlite3OsGetLastError(sqlite3_vfs*);
16040SQLITE_PRIVATEstatic int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
16041
16042/*
16043** Convenience functions for opening and closing files using
16044** sqlite3_malloc() to obtain space for the file-handle structure.
16045*/
16046SQLITE_PRIVATEstatic int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
16047SQLITE_PRIVATEstatic void sqlite3OsCloseFree(sqlite3_file *);
16048
16049#endif /* _SQLITE_OS_H_ */
16050
16051/************** End of os.h **************************************************/
16052/************** Continuing where we left off in sqliteInt.h ******************/
16053/************** Include mutex.h in the middle of sqliteInt.h *****************/
16054/************** Begin file mutex.h *******************************************/
16055/*
16056** 2007 August 28
16057**
16058** The author disclaims copyright to this source code. In place of
16059** a legal notice, here is a blessing:
16060**
16061** May you do good and not evil.
16062** May you find forgiveness for yourself and forgive others.
16063** May you share freely, never taking more than you give.
16064**
16065*************************************************************************
16066**
16067** This file contains the common header for all mutex implementations.
16068** The sqliteInt.h header #includes this file so that it is available
16069** to all source files. We break it out in an effort to keep the code
16070** better organized.
16071**
16072** NOTE: source files should *not* #include this header file directly.
16073** Source files should #include the sqliteInt.h file and let that file
16074** include this one indirectly.
16075*/
16076
16077
16078/*
16079** Figure out what version of the code to use. The choices are
16080**
16081** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
16082** mutexes implementation cannot be overridden
16083** at start-time.
16084**
16085** SQLITE_MUTEX_NOOP For single-threaded applications. No
16086** mutual exclusion is provided. But this
16087** implementation can be overridden at
16088** start-time.
16089**
16090** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16091**
16092** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16093*/
16094#if !SQLITE_THREADSAFE1
16095# define SQLITE_MUTEX_OMIT
16096#endif
16097#if SQLITE_THREADSAFE1 && !defined(SQLITE_MUTEX_NOOP)
16098# if SQLITE_OS_UNIX1
16099# define SQLITE_MUTEX_PTHREADS
16100# elif SQLITE_OS_WIN0
16101# define SQLITE_MUTEX_W32
16102# else
16103# define SQLITE_MUTEX_NOOP
16104# endif
16105#endif
16106
16107#ifdef SQLITE_MUTEX_OMIT
16108/*
16109** If this is a no-op implementation, implement everything as macros.
16110*/
16111#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
16112#define sqlite3_mutex_free(X)
16113#define sqlite3_mutex_enter(X)
16114#define sqlite3_mutex_try(X) SQLITE_OK0
16115#define sqlite3_mutex_leave(X)
16116#define sqlite3_mutex_held(X) ((void)(X),1)
16117#define sqlite3_mutex_notheld(X) ((void)(X),1)
16118#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
16119#define sqlite3MutexInit() SQLITE_OK0
16120#define sqlite3MutexEnd()
16121#define MUTEX_LOGIC(X)X
16122#else
16123#define MUTEX_LOGIC(X)X X
16124#endif /* defined(SQLITE_MUTEX_OMIT) */
16125
16126/************** End of mutex.h ***********************************************/
16127/************** Continuing where we left off in sqliteInt.h ******************/
16128
16129/* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16130** synchronous setting to EXTRA. It is no longer supported.
16131*/
16132#ifdef SQLITE_EXTRA_DURABLE
16133# warning Use SQLITE_DEFAULT_SYNCHRONOUS2=3 instead of SQLITE_EXTRA_DURABLE
16134# define SQLITE_DEFAULT_SYNCHRONOUS2 3
16135#endif
16136
16137/*
16138** Default synchronous levels.
16139**
16140** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
16141** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
16142**
16143** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
16144** OFF 1 0
16145** NORMAL 2 1
16146** FULL 3 2
16147** EXTRA 4 3
16148**
16149** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16150** In other words, the zero-based numbers are used for all external interfaces
16151** and the one-based values are used internally.
16152*/
16153#ifndef SQLITE_DEFAULT_SYNCHRONOUS2
16154# define SQLITE_DEFAULT_SYNCHRONOUS2 2
16155#endif
16156#ifndef SQLITE_DEFAULT_WAL_SYNCHRONOUS2
16157# define SQLITE_DEFAULT_WAL_SYNCHRONOUS2 SQLITE_DEFAULT_SYNCHRONOUS2
16158#endif
16159
16160/*
16161** Each database file to be accessed by the system is an instance
16162** of the following structure. There are normally two of these structures
16163** in the sqlite.aDb[] array. aDb[0] is the main database file and
16164** aDb[1] is the database file used to hold temporary tables. Additional
16165** databases may be attached.
16166*/
16167struct Db {
16168 char *zDbSName; /* Name of this database. (schema name, not filename) */
16169 Btree *pBt; /* The B*Tree structure for this database file */
16170 u8 safety_level; /* How aggressive at syncing data to disk */
16171 u8 bSyncSet; /* True if "PRAGMA synchronous=N" has been run */
16172 Schema *pSchema; /* Pointer to database schema (possibly shared) */
16173};
16174
16175/*
16176** An instance of the following structure stores a database schema.
16177**
16178** Most Schema objects are associated with a Btree. The exception is
16179** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16180** In shared cache mode, a single Schema object can be shared by multiple
16181** Btrees that refer to the same underlying BtShared object.
16182**
16183** Schema objects are automatically deallocated when the last Btree that
16184** references them is destroyed. The TEMP Schema is manually freed by
16185** sqlite3_close().
16186*
16187** A thread must be holding a mutex on the corresponding Btree in order
16188** to access Schema content. This implies that the thread must also be
16189** holding a mutex on the sqlite3 connection pointer that owns the Btree.
16190** For a TEMP Schema, only the connection mutex is required.
16191*/
16192struct Schema {
16193 int schema_cookie; /* Database schema version number for this file */
16194 int iGeneration; /* Generation counter. Incremented with each change */
16195 Hash tblHash; /* All tables indexed by name */
16196 Hash idxHash; /* All (named) indices indexed by name */
16197 Hash trigHash; /* All triggers indexed by name */
16198 Hash fkeyHash; /* All foreign keys by referenced table name */
16199 Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
16200 u8 file_format; /* Schema format version for this file */
16201 u8 enc; /* Text encoding used by this database */
16202 u16 schemaFlags; /* Flags associated with this schema */
16203 int cache_size; /* Number of pages to use in the cache */
16204};
16205
16206/*
16207** These macros can be used to test, set, or clear bits in the
16208** Db.pSchema->flags field.
16209*/
16210#define DbHasProperty(D,I,P)(((D)->aDb[I].pSchema->schemaFlags&(P))==(P)) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16211#define DbHasAnyProperty(D,I,P)(((D)->aDb[I].pSchema->schemaFlags&(P))!=0) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16212#define DbSetProperty(D,I,P)(D)->aDb[I].pSchema->schemaFlags|=(P) (D)->aDb[I].pSchema->schemaFlags|=(P)
16213#define DbClearProperty(D,I,P)(D)->aDb[I].pSchema->schemaFlags&=~(P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
16214
16215/*
16216** Allowed values for the DB.pSchema->flags field.
16217**
16218** The DB_SchemaLoaded flag is set after the database schema has been
16219** read into internal hash tables.
16220**
16221** DB_UnresetViews means that one or more views have column names that
16222** have been filled out. If the schema changes, these column names might
16223** changes and so the view will need to be reset.
16224*/
16225#define DB_SchemaLoaded0x0001 0x0001 /* The schema has been loaded */
16226#define DB_UnresetViews0x0002 0x0002 /* Some views have defined column names */
16227#define DB_Empty0x0004 0x0004 /* The file is empty (length 0 bytes) */
16228#define DB_ResetWanted0x0008 0x0008 /* Reset the schema when nSchemaLock==0 */
16229
16230/*
16231** The number of different kinds of things that can be limited
16232** using the sqlite3_limit() interface.
16233*/
16234#define SQLITE_N_LIMIT(11 +1) (SQLITE_LIMIT_WORKER_THREADS11+1)
16235
16236/*
16237** Lookaside malloc is a set of fixed-size buffers that can be used
16238** to satisfy small transient memory allocation requests for objects
16239** associated with a particular database connection. The use of
16240** lookaside malloc provides a significant performance enhancement
16241** (approx 10%) by avoiding numerous malloc/free requests while parsing
16242** SQL statements.
16243**
16244** The Lookaside structure holds configuration information about the
16245** lookaside malloc subsystem. Each available memory allocation in
16246** the lookaside subsystem is stored on a linked list of LookasideSlot
16247** objects.
16248**
16249** Lookaside allocations are only allowed for objects that are associated
16250** with a particular database connection. Hence, schema information cannot
16251** be stored in lookaside because in shared cache mode the schema information
16252** is shared by multiple database connections. Therefore, while parsing
16253** schema information, the Lookaside.bEnabled flag is cleared so that
16254** lookaside allocations are not used to construct the schema objects.
16255*/
16256struct Lookaside {
16257 u32 bDisable; /* Only operate the lookaside when zero */
16258 u16 sz; /* Size of each buffer in bytes */
16259 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
16260 u32 nSlot; /* Number of lookaside slots allocated */
16261 u32 anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
16262 LookasideSlot *pInit; /* List of buffers not previously used */
16263 LookasideSlot *pFree; /* List of available buffers */
16264 void *pStart; /* First byte of available memory space */
16265 void *pEnd; /* First byte past end of available space */
16266};
16267struct LookasideSlot {
16268 LookasideSlot *pNext; /* Next buffer in the list of free buffers */
16269};
16270
16271/*
16272** A hash table for built-in function definitions. (Application-defined
16273** functions use a regular table table from hash.h.)
16274**
16275** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
16276** Collisions are on the FuncDef.u.pHash chain. Use the SQLITE_FUNC_HASH()
16277** macro to compute a hash on the function name.
16278*/
16279#define SQLITE_FUNC_HASH_SZ23 23
16280struct FuncDefHash {
16281 FuncDef *a[SQLITE_FUNC_HASH_SZ23]; /* Hash table for functions */
16282};
16283#define SQLITE_FUNC_HASH(C,L)(((C)+(L))%23) (((C)+(L))%SQLITE_FUNC_HASH_SZ23)
16284
16285#ifdef SQLITE_USER_AUTHENTICATION
16286/*
16287** Information held in the "sqlite3" database connection object and used
16288** to manage user authentication.
16289*/
16290typedef struct sqlite3_userauth sqlite3_userauth;
16291struct sqlite3_userauth {
16292 u8 authLevel; /* Current authentication level */
16293 int nAuthPW; /* Size of the zAuthPW in bytes */
16294 char *zAuthPW; /* Password used to authenticate */
16295 char *zAuthUser; /* User name used to authenticate */
16296};
16297
16298/* Allowed values for sqlite3_userauth.authLevel */
16299#define UAUTH_Unknown 0 /* Authentication not yet checked */
16300#define UAUTH_Fail 1 /* User authentication failed */
16301#define UAUTH_User 2 /* Authenticated as a normal user */
16302#define UAUTH_Admin 3 /* Authenticated as an administrator */
16303
16304/* Functions used only by user authorization logic */
16305SQLITE_PRIVATEstatic int sqlite3UserAuthTable(const char*);
16306SQLITE_PRIVATEstatic int sqlite3UserAuthCheckLogin(sqlite3*,const char*,u8*);
16307SQLITE_PRIVATEstatic void sqlite3UserAuthInit(sqlite3*);
16308SQLITE_PRIVATEstatic void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
16309
16310#endif /* SQLITE_USER_AUTHENTICATION */
16311
16312/*
16313** typedef for the authorization callback function.
16314*/
16315#ifdef SQLITE_USER_AUTHENTICATION
16316 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16317 const char*, const char*);
16318#else
16319 typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
16320 const char*);
16321#endif
16322
16323#ifndef SQLITE_OMIT_DEPRECATED
16324/* This is an extra SQLITE_TRACE macro that indicates "legacy" tracing
16325** in the style of sqlite3_trace()
16326*/
16327#define SQLITE_TRACE_LEGACY0x40 0x40 /* Use the legacy xTrace */
16328#define SQLITE_TRACE_XPROFILE0x80 0x80 /* Use the legacy xProfile */
16329#else
16330#define SQLITE_TRACE_LEGACY0x40 0
16331#define SQLITE_TRACE_XPROFILE0x80 0
16332#endif /* SQLITE_OMIT_DEPRECATED */
16333#define SQLITE_TRACE_NONLEGACY_MASK0x0f 0x0f /* Normal flags */
16334
16335
16336/*
16337** Each database connection is an instance of the following structure.
16338*/
16339struct sqlite3 {
16340 sqlite3_vfs *pVfs; /* OS Interface */
16341 struct Vdbe *pVdbe; /* List of active virtual machines */
16342 CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
16343 sqlite3_mutex *mutex; /* Connection mutex */
16344 Db *aDb; /* All backends */
16345 int nDb; /* Number of backends currently in use */
16346 u32 mDbFlags; /* flags recording internal state */
16347 u64 flags; /* flags settable by pragmas. See below */
16348 i64 lastRowid; /* ROWID of most recent insert (see above) */
16349 i64 szMmap; /* Default mmap_size setting */
16350 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16351 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
16352 int errCode; /* Most recent error code (SQLITE_*) */
16353 int errMask; /* & result codes with this before returning */
16354 int iSysErrno; /* Errno value from last system error */
16355 u16 dbOptFlags; /* Flags to enable/disable optimizations */
16356 u8 enc; /* Text encoding */
16357 u8 autoCommit; /* The auto-commit flag. */
16358 u8 temp_store; /* 1: file 2: memory 0: default */
16359 u8 mallocFailed; /* True if we have seen a malloc failure */
16360 u8 bBenignMalloc; /* Do not require OOMs if true */
16361 u8 dfltLockMode; /* Default locking-mode for attached dbs */
16362 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
16363 u8 suppressErr; /* Do not issue error messages if true */
16364 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
16365 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
16366 u8 mTrace; /* zero or more SQLITE_TRACE flags */
16367 u8 noSharedCache; /* True if no shared-cache backends */
16368 u8 nSqlExec; /* Number of pending OP_SqlExec opcodes */
16369 int nextPagesize; /* Pagesize after VACUUM if >0 */
16370 u32 magic; /* Magic number for detect library misuse */
16371 int nChange; /* Value returned by sqlite3_changes() */
16372 int nTotalChange; /* Value returned by sqlite3_total_changes() */
16373 int aLimit[SQLITE_N_LIMIT(11 +1)]; /* Limits */
16374 int nMaxSorterMmap; /* Maximum size of regions mapped by sorter */
16375 struct sqlite3InitInfo { /* Information used during initialization */
16376 int newTnum; /* Rootpage of table being initialized */
16377 u8 iDb; /* Which db file is being initialized */
16378 u8 busy; /* TRUE if currently initializing */
16379 unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
16380 unsigned imposterTable : 1; /* Building an imposter table */
16381 unsigned reopenMemdb : 1; /* ATTACH is really a reopen using MemDB */
16382 } init;
16383 int nVdbeActive; /* Number of VDBEs currently running */
16384 int nVdbeRead; /* Number of active VDBEs that read or write */
16385 int nVdbeWrite; /* Number of active VDBEs that read and write */
16386 int nVdbeExec; /* Number of nested calls to VdbeExec() */
16387 int nVDestroy; /* Number of active OP_VDestroy operations */
16388 int nExtension; /* Number of loaded extensions */
16389 void **aExtension; /* Array of shared library handles */
16390 int (*xTrace)(u32,void*,void*,void*); /* Trace function */
16391 void *pTraceArg; /* Argument to the trace function */
16392#ifndef SQLITE_OMIT_DEPRECATED
16393 void (*xProfile)(void*,const char*,u64); /* Profiling function */
16394 void *pProfileArg; /* Argument to profile function */
16395#endif
16396 void *pCommitArg; /* Argument to xCommitCallback() */
16397 int (*xCommitCallback)(void*); /* Invoked at every commit. */
16398 void *pRollbackArg; /* Argument to xRollbackCallback() */
16399 void (*xRollbackCallback)(void*); /* Invoked at every commit. */
16400 void *pUpdateArg;
16401 void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
16402 Parse *pParse; /* Current parse */
16403#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
16404 void *pPreUpdateArg; /* First argument to xPreUpdateCallback */
16405 void (*xPreUpdateCallback)( /* Registered using sqlite3_preupdate_hook() */
16406 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64
16407 );
16408 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
16409#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
16410#ifndef SQLITE_OMIT_WAL
16411 int (*xWalCallback)(void *, sqlite3 *, const char *, int);
16412 void *pWalArg;
16413#endif
16414 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
16415 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
16416 void *pCollNeededArg;
16417 sqlite3_value *pErr; /* Most recent error message */
16418 union {
16419 volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
16420 double notUsed1; /* Spacer */
16421 } u1;
16422 Lookaside lookaside; /* Lookaside malloc configuration */
16423#ifndef SQLITE_OMIT_AUTHORIZATION
16424 sqlite3_xauth xAuth; /* Access authorization function */
16425 void *pAuthArg; /* 1st argument to the access auth function */
16426#endif
16427#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
16428 int (*xProgress)(void *); /* The progress callback */
16429 void *pProgressArg; /* Argument to the progress callback */
16430 unsigned nProgressOps; /* Number of opcodes for progress callback */
16431#endif
16432#ifndef SQLITE_OMIT_VIRTUALTABLE
16433 int nVTrans; /* Allocated size of aVTrans */
16434 Hash aModule; /* populated by sqlite3_create_module() */
16435 VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
16436 VTable **aVTrans; /* Virtual tables with open transactions */
16437 VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
16438#endif
16439 Hash aFunc; /* Hash table of connection functions */
16440 Hash aCollSeq; /* All collating sequences */
16441 BusyHandler busyHandler; /* Busy callback */
16442 Db aDbStatic[2]; /* Static space for the 2 default backends */
16443 Savepoint *pSavepoint; /* List of active savepoints */
16444 int busyTimeout; /* Busy handler timeout, in msec */
16445 int nSavepoint; /* Number of non-transaction savepoints */
16446 int nStatement; /* Number of nested statement-transactions */
16447 i64 nDeferredCons; /* Net deferred constraints this transaction. */
16448 i64 nDeferredImmCons; /* Net deferred immediate constraints */
16449 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
16450#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
16451 /* The following variables are all protected by the STATIC_MASTER
16452 ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
16453 **
16454 ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
16455 ** unlock so that it can proceed.
16456 **
16457 ** When X.pBlockingConnection==Y, that means that something that X tried
16458 ** tried to do recently failed with an SQLITE_LOCKED error due to locks
16459 ** held by Y.
16460 */
16461 sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
16462 sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
16463 void *pUnlockArg; /* Argument to xUnlockNotify */
16464 void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
16465 sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
16466#endif
16467#ifdef SQLITE_USER_AUTHENTICATION
16468 sqlite3_userauth auth; /* User authentication information */
16469#endif
16470};
16471
16472/*
16473** A macro to discover the encoding of a database.
16474*/
16475#define SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc) ((db)->aDb[0].pSchema->enc)
16476#define ENC(db)((db)->enc) ((db)->enc)
16477
16478/*
16479** Possible values for the sqlite3.flags.
16480**
16481** Value constraints (enforced via assert()):
16482** SQLITE_FullFSync == PAGER_FULLFSYNC
16483** SQLITE_CkptFullFSync == PAGER_CKPT_FULLFSYNC
16484** SQLITE_CacheSpill == PAGER_CACHE_SPILL
16485*/
16486#define SQLITE_WriteSchema0x00000001 0x00000001 /* OK to update SQLITE_MASTER */
16487#define SQLITE_LegacyFileFmt0x00000002 0x00000002 /* Create new databases in format 1 */
16488#define SQLITE_FullColNames0x00000004 0x00000004 /* Show full column names on SELECT */
16489#define SQLITE_FullFSync0x00000008 0x00000008 /* Use full fsync on the backend */
16490#define SQLITE_CkptFullFSync0x00000010 0x00000010 /* Use full fsync for checkpoint */
16491#define SQLITE_CacheSpill0x00000020 0x00000020 /* OK to spill pager cache */
16492#define SQLITE_ShortColNames0x00000040 0x00000040 /* Show short columns names */
16493#define SQLITE_CountRows0x00000080 0x00000080 /* Count rows changed by INSERT, */
16494 /* DELETE, or UPDATE and return */
16495 /* the count using a callback. */
16496#define SQLITE_NullCallback0x00000100 0x00000100 /* Invoke the callback once if the */
16497 /* result set is empty */
16498#define SQLITE_IgnoreChecks0x00000200 0x00000200 /* Do not enforce check constraints */
16499#define SQLITE_ReadUncommit0x00000400 0x00000400 /* READ UNCOMMITTED in shared-cache */
16500#define SQLITE_NoCkptOnClose0x00000800 0x00000800 /* No checkpoint on close()/DETACH */
16501#define SQLITE_ReverseOrder0x00001000 0x00001000 /* Reverse unordered SELECTs */
16502#define SQLITE_RecTriggers0x00002000 0x00002000 /* Enable recursive triggers */
16503#define SQLITE_ForeignKeys0x00004000 0x00004000 /* Enforce foreign key constraints */
16504#define SQLITE_AutoIndex0x00008000 0x00008000 /* Enable automatic indexes */
16505#define SQLITE_LoadExtension0x00010000 0x00010000 /* Enable load_extension */
16506#define SQLITE_LoadExtFunc0x00020000 0x00020000 /* Enable load_extension() SQL func */
16507#define SQLITE_EnableTrigger0x00040000 0x00040000 /* True to enable triggers */
16508#define SQLITE_DeferFKs0x00080000 0x00080000 /* Defer all FK constraints */
16509#define SQLITE_QueryOnly0x00100000 0x00100000 /* Disable database changes */
16510#define SQLITE_CellSizeCk0x00200000 0x00200000 /* Check btree cell sizes on load */
16511#define SQLITE_Fts3Tokenizer0x00400000 0x00400000 /* Enable fts3_tokenizer(2) */
16512#define SQLITE_EnableQPSG0x00800000 0x00800000 /* Query Planner Stability Guarantee*/
16513#define SQLITE_TriggerEQP0x01000000 0x01000000 /* Show trigger EXPLAIN QUERY PLAN */
16514#define SQLITE_ResetDatabase0x02000000 0x02000000 /* Reset the database */
16515#define SQLITE_LegacyAlter0x04000000 0x04000000 /* Legacy ALTER TABLE behaviour */
16516#define SQLITE_NoSchemaError0x08000000 0x08000000 /* Do not report schema parse errors*/
16517#define SQLITE_Defensive0x10000000 0x10000000 /* Input SQL is likely hostile */
16518#define SQLITE_DqsDDL0x20000000 0x20000000 /* dbl-quoted strings allowed in DDL*/
16519#define SQLITE_DqsDML0x40000000 0x40000000 /* dbl-quoted strings allowed in DML*/
16520
16521/* Flags used only if debugging */
16522#define HI(X)((u64)(X)<<32) ((u64)(X)<<32)
16523#ifdef SQLITE_DEBUG
16524#define SQLITE_SqlTrace HI(0x0001)((u64)(0x0001)<<32) /* Debug print SQL as it executes */
16525#define SQLITE_VdbeListing HI(0x0002)((u64)(0x0002)<<32) /* Debug listings of VDBE progs */
16526#define SQLITE_VdbeTrace HI(0x0004)((u64)(0x0004)<<32) /* True to trace VDBE execution */
16527#define SQLITE_VdbeAddopTrace HI(0x0008)((u64)(0x0008)<<32) /* Trace sqlite3VdbeAddOp() calls */
16528#define SQLITE_VdbeEQP HI(0x0010)((u64)(0x0010)<<32) /* Debug EXPLAIN QUERY PLAN */
16529#define SQLITE_ParserTrace HI(0x0020)((u64)(0x0020)<<32) /* PRAGMA parser_trace=ON */
16530#endif
16531
16532/*
16533** Allowed values for sqlite3.mDbFlags
16534*/
16535#define DBFLAG_SchemaChange0x0001 0x0001 /* Uncommitted Hash table changes */
16536#define DBFLAG_PreferBuiltin0x0002 0x0002 /* Preference to built-in funcs */
16537#define DBFLAG_Vacuum0x0004 0x0004 /* Currently in a VACUUM */
16538#define DBFLAG_VacuumInto0x0008 0x0008 /* Currently running VACUUM INTO */
16539#define DBFLAG_SchemaKnownOk0x0010 0x0010 /* Schema is known to be valid */
16540
16541/*
16542** Bits of the sqlite3.dbOptFlags field that are used by the
16543** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
16544** selectively disable various optimizations.
16545*/
16546#define SQLITE_QueryFlattener0x0001 0x0001 /* Query flattening */
16547#define SQLITE_WindowFunc0x0002 0x0002 /* Use xInverse for window functions */
16548#define SQLITE_GroupByOrder0x0004 0x0004 /* GROUPBY cover of ORDERBY */
16549#define SQLITE_FactorOutConst0x0008 0x0008 /* Constant factoring */
16550#define SQLITE_DistinctOpt0x0010 0x0010 /* DISTINCT using indexes */
16551#define SQLITE_CoverIdxScan0x0020 0x0020 /* Covering index scans */
16552#define SQLITE_OrderByIdxJoin0x0040 0x0040 /* ORDER BY of joins via index */
16553#define SQLITE_Transitive0x0080 0x0080 /* Transitive constraints */
16554#define SQLITE_OmitNoopJoin0x0100 0x0100 /* Omit unused tables in joins */
16555#define SQLITE_CountOfView0x0200 0x0200 /* The count-of-view optimization */
16556#define SQLITE_CursorHints0x0400 0x0400 /* Add OP_CursorHint opcodes */
16557#define SQLITE_Stat340x0800 0x0800 /* Use STAT3 or STAT4 data */
16558 /* TH3 expects the Stat34 ^^^^^^ value to be 0x0800. Don't change it */
16559#define SQLITE_PushDown0x1000 0x1000 /* The push-down optimization */
16560#define SQLITE_SimplifyJoin0x2000 0x2000 /* Convert LEFT JOIN to JOIN */
16561#define SQLITE_SkipScan0x4000 0x4000 /* Skip-scans */
16562#define SQLITE_PropagateConst0x8000 0x8000 /* The constant propagation opt */
16563#define SQLITE_AllOpts0xffff 0xffff /* All optimizations */
16564
16565/*
16566** Macros for testing whether or not optimizations are enabled or disabled.
16567*/
16568#define OptimizationDisabled(db, mask)(((db)->dbOptFlags&(mask))!=0) (((db)->dbOptFlags&(mask))!=0)
16569#define OptimizationEnabled(db, mask)(((db)->dbOptFlags&(mask))==0) (((db)->dbOptFlags&(mask))==0)
16570
16571/*
16572** Return true if it OK to factor constant expressions into the initialization
16573** code. The argument is a Parse object for the code generator.
16574*/
16575#define ConstFactorOk(P)((P)->okConstFactor) ((P)->okConstFactor)
16576
16577/*
16578** Possible values for the sqlite.magic field.
16579** The numbers are obtained at random and have no special meaning, other
16580** than being distinct from one another.
16581*/
16582#define SQLITE_MAGIC_OPEN0xa029a697 0xa029a697 /* Database is open */
16583#define SQLITE_MAGIC_CLOSED0x9f3c2d33 0x9f3c2d33 /* Database is closed */
16584#define SQLITE_MAGIC_SICK0x4b771290 0x4b771290 /* Error and awaiting close */
16585#define SQLITE_MAGIC_BUSY0xf03b7906 0xf03b7906 /* Database currently in use */
16586#define SQLITE_MAGIC_ERROR0xb5357930 0xb5357930 /* An SQLITE_MISUSE error occurred */
16587#define SQLITE_MAGIC_ZOMBIE0x64cffc7f 0x64cffc7f /* Close with last statement close */
16588
16589/*
16590** Each SQL function is defined by an instance of the following
16591** structure. For global built-in functions (ex: substr(), max(), count())
16592** a pointer to this structure is held in the sqlite3BuiltinFunctions object.
16593** For per-connection application-defined functions, a pointer to this
16594** structure is held in the db->aHash hash table.
16595**
16596** The u.pHash field is used by the global built-ins. The u.pDestructor
16597** field is used by per-connection app-def functions.
16598*/
16599struct FuncDef {
16600 i8 nArg; /* Number of arguments. -1 means unlimited */
16601 u32 funcFlags; /* Some combination of SQLITE_FUNC_* */
16602 void *pUserData; /* User data parameter */
16603 FuncDef *pNext; /* Next function with same name */
16604 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
16605 void (*xFinalize)(sqlite3_context*); /* Agg finalizer */
16606 void (*xValue)(sqlite3_context*); /* Current agg value */
16607 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
16608 const char *zName; /* SQL name of the function. */
16609 union {
16610 FuncDef *pHash; /* Next with a different name but the same hash */
16611 FuncDestructor *pDestructor; /* Reference counted destructor function */
16612 } u;
16613};
16614
16615/*
16616** This structure encapsulates a user-function destructor callback (as
16617** configured using create_function_v2()) and a reference counter. When
16618** create_function_v2() is called to create a function with a destructor,
16619** a single object of this type is allocated. FuncDestructor.nRef is set to
16620** the number of FuncDef objects created (either 1 or 3, depending on whether
16621** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
16622** member of each of the new FuncDef objects is set to point to the allocated
16623** FuncDestructor.
16624**
16625** Thereafter, when one of the FuncDef objects is deleted, the reference
16626** count on this object is decremented. When it reaches 0, the destructor
16627** is invoked and the FuncDestructor structure freed.
16628*/
16629struct FuncDestructor {
16630 int nRef;
16631 void (*xDestroy)(void *);
16632 void *pUserData;
16633};
16634
16635/*
16636** Possible values for FuncDef.flags. Note that the _LENGTH and _TYPEOF
16637** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG. And
16638** SQLITE_FUNC_CONSTANT must be the same as SQLITE_DETERMINISTIC. There
16639** are assert() statements in the code to verify this.
16640**
16641** Value constraints (enforced via assert()):
16642** SQLITE_FUNC_MINMAX == NC_MinMaxAgg == SF_MinMaxAgg
16643** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
16644** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
16645** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
16646** SQLITE_FUNC_ENCMASK depends on SQLITE_UTF* macros in the API
16647*/
16648#define SQLITE_FUNC_ENCMASK0x0003 0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
16649#define SQLITE_FUNC_LIKE0x0004 0x0004 /* Candidate for the LIKE optimization */
16650#define SQLITE_FUNC_CASE0x0008 0x0008 /* Case-sensitive LIKE-type function */
16651#define SQLITE_FUNC_EPHEM0x0010 0x0010 /* Ephemeral. Delete with VDBE */
16652#define SQLITE_FUNC_NEEDCOLL0x0020 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
16653#define SQLITE_FUNC_LENGTH0x0040 0x0040 /* Built-in length() function */
16654#define SQLITE_FUNC_TYPEOF0x0080 0x0080 /* Built-in typeof() function */
16655#define SQLITE_FUNC_COUNT0x0100 0x0100 /* Built-in count(*) aggregate */
16656#define SQLITE_FUNC_COALESCE0x0200 0x0200 /* Built-in coalesce() or ifnull() */
16657#define SQLITE_FUNC_UNLIKELY0x0400 0x0400 /* Built-in unlikely() function */
16658#define SQLITE_FUNC_CONSTANT0x0800 0x0800 /* Constant inputs give a constant output */
16659#define SQLITE_FUNC_MINMAX0x1000 0x1000 /* True for min() and max() aggregates */
16660#define SQLITE_FUNC_SLOCHNG0x2000 0x2000 /* "Slow Change". Value constant during a
16661 ** single query - might change over time */
16662#define SQLITE_FUNC_AFFINITY0x4000 0x4000 /* Built-in affinity() function */
16663#define SQLITE_FUNC_OFFSET0x8000 0x8000 /* Built-in sqlite_offset() function */
16664#define SQLITE_FUNC_WINDOW0x00010000 0x00010000 /* Built-in window-only function */
16665#define SQLITE_FUNC_INTERNAL0x00040000 0x00040000 /* For use by NestedParse() only */
16666
16667/*
16668** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
16669** used to create the initializers for the FuncDef structures.
16670**
16671** FUNCTION(zName, nArg, iArg, bNC, xFunc)
16672** Used to create a scalar function definition of a function zName
16673** implemented by C function xFunc that accepts nArg arguments. The
16674** value passed as iArg is cast to a (void*) and made available
16675** as the user-data (sqlite3_user_data()) for the function. If
16676** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
16677**
16678** VFUNCTION(zName, nArg, iArg, bNC, xFunc)
16679** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
16680**
16681** DFUNCTION(zName, nArg, iArg, bNC, xFunc)
16682** Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag and
16683** adds the SQLITE_FUNC_SLOCHNG flag. Used for date & time functions
16684** and functions like sqlite_version() that can change, but not during
16685** a single query. The iArg is ignored. The user-data is always set
16686** to a NULL pointer. The bNC parameter is not used.
16687**
16688** PURE_DATE(zName, nArg, iArg, bNC, xFunc)
16689** Used for "pure" date/time functions, this macro is like DFUNCTION
16690** except that it does set the SQLITE_FUNC_CONSTANT flags. iArg is
16691** ignored and the user-data for these functions is set to an
16692** arbitrary non-NULL pointer. The bNC parameter is not used.
16693**
16694** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
16695** Used to create an aggregate function definition implemented by
16696** the C functions xStep and xFinal. The first four parameters
16697** are interpreted in the same way as the first 4 parameters to
16698** FUNCTION().
16699**
16700** WFUNCTION(zName, nArg, iArg, xStep, xFinal, xValue, xInverse)
16701** Used to create an aggregate function definition implemented by
16702** the C functions xStep and xFinal. The first four parameters
16703** are interpreted in the same way as the first 4 parameters to
16704** FUNCTION().
16705**
16706** LIKEFUNC(zName, nArg, pArg, flags)
16707** Used to create a scalar function definition of a function zName
16708** that accepts nArg arguments and is implemented by a call to C
16709** function likeFunc. Argument pArg is cast to a (void *) and made
16710** available as the function user-data (sqlite3_user_data()). The
16711** FuncDef.flags variable is set to the value passed as the flags
16712** parameter.
16713*/
16714#define FUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x0800|1|(bNC*0x0020), ((void*)(long int)(iArg)), 0, xFunc
, 0, 0, 0, "zName", {0} }
\
16715 {nArg, SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
16716 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
16717#define VFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 1|(bNC*0x0020), ((void*)(long int)(iArg)), 0, xFunc, 0
, 0, 0, "zName", {0} }
\
16718 {nArg, SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
16719 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
16720#define DFUNCTION(zName, nArg, iArg, bNC, xFunc){nArg, 0x2000|1, 0, 0, xFunc, 0, 0, 0, "zName", {0} } \
16721 {nArg, SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81, \
16722 0, 0, xFunc, 0, 0, 0, #zName, {0} }
16723#define PURE_DATE(zName, nArg, iArg, bNC, xFunc){nArg, 0x2000|1|0x0800, (void*)&sqlite3Config, 0, xFunc, 0
, 0, 0, "zName", {0} }
\
16724 {nArg, SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81|SQLITE_FUNC_CONSTANT0x0800, \
16725 (void*)&sqlite3Config, 0, xFunc, 0, 0, 0, #zName, {0} }
16726#define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags){nArg,0x0800|1|(bNC*0x0020)|extraFlags, ((void*)(long int)(iArg
)), 0, xFunc, 0, 0, 0, "zName", {0} }
\
16727 {nArg,SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020)|extraFlags,\
16728 SQLITE_INT_TO_PTR(iArg)((void*)(long int)(iArg)), 0, xFunc, 0, 0, 0, #zName, {0} }
16729#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc){nArg, 0x2000|1|(bNC*0x0020), pArg, 0, xFunc, 0, 0, 0, "zName"
, }
\
16730 {nArg, SQLITE_FUNC_SLOCHNG0x2000|SQLITE_UTF81|(bNC*SQLITE_FUNC_NEEDCOLL0x0020), \
16731 pArg, 0, xFunc, 0, 0, 0, #zName, }
16732#define LIKEFUNC(zName, nArg, arg, flags){nArg, 0x0800|1|flags, (void *)arg, 0, likeFunc, 0, 0, 0, "zName"
, {0} }
\
16733 {nArg, SQLITE_FUNC_CONSTANT0x0800|SQLITE_UTF81|flags, \
16734 (void *)arg, 0, likeFunc, 0, 0, 0, #zName, {0} }
16735#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue){nArg, 1|(nc*0x0020), ((void*)(long int)(arg)), 0, xStep,xFinal
,xValue,0,"zName", {0}}
\
16736 {nArg, SQLITE_UTF81|(nc*SQLITE_FUNC_NEEDCOLL0x0020), \
16737 SQLITE_INT_TO_PTR(arg)((void*)(long int)(arg)), 0, xStep,xFinal,xValue,0,#zName, {0}}
16738#define AGGREGATE2(zName, nArg, arg, nc, xStep, xFinal, extraFlags){nArg, 1|(nc*0x0020)|extraFlags, ((void*)(long int)(arg)), 0,
xStep,xFinal,xFinal,0,"zName", {0}}
\
16739 {nArg, SQLITE_UTF81|(nc*SQLITE_FUNC_NEEDCOLL0x0020)|extraFlags, \
16740 SQLITE_INT_TO_PTR(arg)((void*)(long int)(arg)), 0, xStep,xFinal,xFinal,0,#zName, {0}}
16741#define WAGGREGATE(zName, nArg, arg, nc, xStep, xFinal, xValue, xInverse, f){nArg, 1|(nc*0x0020)|f, ((void*)(long int)(arg)), 0, xStep,xFinal
,xValue,xInverse,"zName", {0}}
\
16742 {nArg, SQLITE_UTF81|(nc*SQLITE_FUNC_NEEDCOLL0x0020)|f, \
16743 SQLITE_INT_TO_PTR(arg)((void*)(long int)(arg)), 0, xStep,xFinal,xValue,xInverse,#zName, {0}}
16744#define INTERNAL_FUNCTION(zName, nArg, xFunc){nArg, 0x00040000|1|0x0800, 0, 0, xFunc, 0, 0, 0, "zName", {0
} }
\
16745 {nArg, SQLITE_FUNC_INTERNAL0x00040000|SQLITE_UTF81|SQLITE_FUNC_CONSTANT0x0800, \
16746 0, 0, xFunc, 0, 0, 0, #zName, {0} }
16747
16748
16749/*
16750** All current savepoints are stored in a linked list starting at
16751** sqlite3.pSavepoint. The first element in the list is the most recently
16752** opened savepoint. Savepoints are added to the list by the vdbe
16753** OP_Savepoint instruction.
16754*/
16755struct Savepoint {
16756 char *zName; /* Savepoint name (nul-terminated) */
16757 i64 nDeferredCons; /* Number of deferred fk violations */
16758 i64 nDeferredImmCons; /* Number of deferred imm fk. */
16759 Savepoint *pNext; /* Parent savepoint (if any) */
16760};
16761
16762/*
16763** The following are used as the second parameter to sqlite3Savepoint(),
16764** and as the P1 argument to the OP_Savepoint instruction.
16765*/
16766#define SAVEPOINT_BEGIN0 0
16767#define SAVEPOINT_RELEASE1 1
16768#define SAVEPOINT_ROLLBACK2 2
16769
16770
16771/*
16772** Each SQLite module (virtual table definition) is defined by an
16773** instance of the following structure, stored in the sqlite3.aModule
16774** hash table.
16775*/
16776struct Module {
16777 const sqlite3_module *pModule; /* Callback pointers */
16778 const char *zName; /* Name passed to create_module() */
16779 void *pAux; /* pAux passed to create_module() */
16780 void (*xDestroy)(void *); /* Module destructor function */
16781 Table *pEpoTab; /* Eponymous table for this module */
16782};
16783
16784/*
16785** information about each column of an SQL table is held in an instance
16786** of this structure.
16787*/
16788struct Column {
16789 char *zName; /* Name of this column, \000, then the type */
16790 Expr *pDflt; /* Default value of this column */
16791 char *zColl; /* Collating sequence. If NULL, use the default */
16792 u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
16793 char affinity; /* One of the SQLITE_AFF_... values */
16794 u8 szEst; /* Estimated size of value in this column. sizeof(INT)==1 */
16795 u8 colFlags; /* Boolean properties. See COLFLAG_ defines below */
16796};
16797
16798/* Allowed values for Column.colFlags:
16799*/
16800#define COLFLAG_PRIMKEY0x0001 0x0001 /* Column is part of the primary key */
16801#define COLFLAG_HIDDEN0x0002 0x0002 /* A hidden column in a virtual table */
16802#define COLFLAG_HASTYPE0x0004 0x0004 /* Type name follows column name */
16803#define COLFLAG_UNIQUE0x0008 0x0008 /* Column def contains "UNIQUE" or "PK" */
16804#define COLFLAG_SORTERREF0x0010 0x0010 /* Use sorter-refs with this column */
16805
16806/*
16807** A "Collating Sequence" is defined by an instance of the following
16808** structure. Conceptually, a collating sequence consists of a name and
16809** a comparison routine that defines the order of that sequence.
16810**
16811** If CollSeq.xCmp is NULL, it means that the
16812** collating sequence is undefined. Indices built on an undefined
16813** collating sequence may not be read or written.
16814*/
16815struct CollSeq {
16816 char *zName; /* Name of the collating sequence, UTF-8 encoded */
16817 u8 enc; /* Text encoding handled by xCmp() */
16818 void *pUser; /* First argument to xCmp() */
16819 int (*xCmp)(void*,int, const void*, int, const void*);
16820 void (*xDel)(void*); /* Destructor for pUser */
16821};
16822
16823/*
16824** A sort order can be either ASC or DESC.
16825*/
16826#define SQLITE_SO_ASC0 0 /* Sort in ascending order */
16827#define SQLITE_SO_DESC1 1 /* Sort in ascending order */
16828#define SQLITE_SO_UNDEFINED-1 -1 /* No sort order specified */
16829
16830/*
16831** Column affinity types.
16832**
16833** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
16834** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
16835** the speed a little by numbering the values consecutively.
16836**
16837** But rather than start with 0 or 1, we begin with 'A'. That way,
16838** when multiple affinity types are concatenated into a string and
16839** used as the P4 operand, they will be more readable.
16840**
16841** Note also that the numeric types are grouped together so that testing
16842** for a numeric type is a single comparison. And the BLOB type is first.
16843*/
16844#define SQLITE_AFF_BLOB'A' 'A'
16845#define SQLITE_AFF_TEXT'B' 'B'
16846#define SQLITE_AFF_NUMERIC'C' 'C'
16847#define SQLITE_AFF_INTEGER'D' 'D'
16848#define SQLITE_AFF_REAL'E' 'E'
16849
16850#define sqlite3IsNumericAffinity(X)((X)>='C') ((X)>=SQLITE_AFF_NUMERIC'C')
16851
16852/*
16853** The SQLITE_AFF_MASK values masks off the significant bits of an
16854** affinity value.
16855*/
16856#define SQLITE_AFF_MASK0x47 0x47
16857
16858/*
16859** Additional bit values that can be ORed with an affinity without
16860** changing the affinity.
16861**
16862** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
16863** It causes an assert() to fire if either operand to a comparison
16864** operator is NULL. It is added to certain comparison operators to
16865** prove that the operands are always NOT NULL.
16866*/
16867#define SQLITE_KEEPNULL0x08 0x08 /* Used by vector == or <> */
16868#define SQLITE_JUMPIFNULL0x10 0x10 /* jumps if either operand is NULL */
16869#define SQLITE_STOREP20x20 0x20 /* Store result in reg[P2] rather than jump */
16870#define SQLITE_NULLEQ0x80 0x80 /* NULL=NULL */
16871#define SQLITE_NOTNULL0x90 0x90 /* Assert that operands are never NULL */
16872
16873/*
16874** An object of this type is created for each virtual table present in
16875** the database schema.
16876**
16877** If the database schema is shared, then there is one instance of this
16878** structure for each database connection (sqlite3*) that uses the shared
16879** schema. This is because each database connection requires its own unique
16880** instance of the sqlite3_vtab* handle used to access the virtual table
16881** implementation. sqlite3_vtab* handles can not be shared between
16882** database connections, even when the rest of the in-memory database
16883** schema is shared, as the implementation often stores the database
16884** connection handle passed to it via the xConnect() or xCreate() method
16885** during initialization internally. This database connection handle may
16886** then be used by the virtual table implementation to access real tables
16887** within the database. So that they appear as part of the callers
16888** transaction, these accesses need to be made via the same database
16889** connection as that used to execute SQL operations on the virtual table.
16890**
16891** All VTable objects that correspond to a single table in a shared
16892** database schema are initially stored in a linked-list pointed to by
16893** the Table.pVTable member variable of the corresponding Table object.
16894** When an sqlite3_prepare() operation is required to access the virtual
16895** table, it searches the list for the VTable that corresponds to the
16896** database connection doing the preparing so as to use the correct
16897** sqlite3_vtab* handle in the compiled query.
16898**
16899** When an in-memory Table object is deleted (for example when the
16900** schema is being reloaded for some reason), the VTable objects are not
16901** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
16902** immediately. Instead, they are moved from the Table.pVTable list to
16903** another linked list headed by the sqlite3.pDisconnect member of the
16904** corresponding sqlite3 structure. They are then deleted/xDisconnected
16905** next time a statement is prepared using said sqlite3*. This is done
16906** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
16907** Refer to comments above function sqlite3VtabUnlockList() for an
16908** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
16909** list without holding the corresponding sqlite3.mutex mutex.
16910**
16911** The memory for objects of this type is always allocated by
16912** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
16913** the first argument.
16914*/
16915struct VTable {
16916 sqlite3 *db; /* Database connection associated with this table */
16917 Module *pMod; /* Pointer to module implementation */
16918 sqlite3_vtab *pVtab; /* Pointer to vtab instance */
16919 int nRef; /* Number of pointers to this structure */
16920 u8 bConstraint; /* True if constraints are supported */
16921 int iSavepoint; /* Depth of the SAVEPOINT stack */
16922 VTable *pNext; /* Next in linked list (see above) */
16923};
16924
16925/*
16926** The schema for each SQL table and view is represented in memory
16927** by an instance of the following structure.
16928*/
16929struct Table {
16930 char *zName; /* Name of the table or view */
16931 Column *aCol; /* Information about each column */
16932 Index *pIndex; /* List of SQL indexes on this table. */
16933 Select *pSelect; /* NULL for tables. Points to definition if a view. */
16934 FKey *pFKey; /* Linked list of all foreign keys in this table */
16935 char *zColAff; /* String defining the affinity of each column */
16936 ExprList *pCheck; /* All CHECK constraints */
16937 /* ... also used as column name list in a VIEW */
16938 int tnum; /* Root BTree page for this table */
16939 u32 nTabRef; /* Number of pointers to this Table */
16940 u32 tabFlags; /* Mask of TF_* values */
16941 i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */
16942 i16 nCol; /* Number of columns in this table */
16943 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
16944 LogEst szTabRow; /* Estimated size of each table row in bytes */
16945#ifdef SQLITE_ENABLE_COSTMULT
16946 LogEst costMult; /* Cost multiplier for using this table */
16947#endif
16948 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
16949#ifndef SQLITE_OMIT_ALTERTABLE
16950 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
16951#endif
16952#ifndef SQLITE_OMIT_VIRTUALTABLE
16953 int nModuleArg; /* Number of arguments to the module */
16954 char **azModuleArg; /* 0: module 1: schema 2: vtab name 3...: args */
16955 VTable *pVTable; /* List of VTable objects. */
16956#endif
16957 Trigger *pTrigger; /* List of triggers stored in pSchema */
16958 Schema *pSchema; /* Schema that contains this table */
16959 Table *pNextZombie; /* Next on the Parse.pZombieTab list */
16960};
16961
16962/*
16963** Allowed values for Table.tabFlags.
16964**
16965** TF_OOOHidden applies to tables or view that have hidden columns that are
16966** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
16967** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
16968** the TF_OOOHidden attribute would apply in this case. Such tables require
16969** special handling during INSERT processing.
16970*/
16971#define TF_Readonly0x0001 0x0001 /* Read-only system table */
16972#define TF_Ephemeral0x0002 0x0002 /* An ephemeral table */
16973#define TF_HasPrimaryKey0x0004 0x0004 /* Table has a primary key */
16974#define TF_Autoincrement0x0008 0x0008 /* Integer primary key is autoincrement */
16975#define TF_HasStat10x0010 0x0010 /* nRowLogEst set from sqlite_stat1 */
16976#define TF_WithoutRowid0x0020 0x0020 /* No rowid. PRIMARY KEY is the key */
16977#define TF_NoVisibleRowid0x0040 0x0040 /* No user-visible "rowid" column */
16978#define TF_OOOHidden0x0080 0x0080 /* Out-of-Order hidden columns */
16979#define TF_StatsUsed0x0100 0x0100 /* Query planner decisions affected by
16980 ** Index.aiRowLogEst[] values */
16981#define TF_HasNotNull0x0200 0x0200 /* Contains NOT NULL constraints */
16982#define TF_Shadow0x0400 0x0400 /* True for a shadow table */
16983
16984/*
16985** Test to see whether or not a table is a virtual table. This is
16986** done as a macro so that it will be optimized out when virtual
16987** table support is omitted from the build.
16988*/
16989#ifndef SQLITE_OMIT_VIRTUALTABLE
16990# define IsVirtual(X)((X)->nModuleArg) ((X)->nModuleArg)
16991#else
16992# define IsVirtual(X)((X)->nModuleArg) 0
16993#endif
16994
16995/*
16996** Macros to determine if a column is hidden. IsOrdinaryHiddenColumn()
16997** only works for non-virtual tables (ordinary tables and views) and is
16998** always false unless SQLITE_ENABLE_HIDDEN_COLUMNS is defined. The
16999** IsHiddenColumn() macro is general purpose.
17000*/
17001#if defined(SQLITE_ENABLE_HIDDEN_COLUMNS)
17002# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
17003# define IsOrdinaryHiddenColumn(X)0 (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
17004#elif !defined(SQLITE_OMIT_VIRTUALTABLE)
17005# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) (((X)->colFlags & COLFLAG_HIDDEN0x0002)!=0)
17006# define IsOrdinaryHiddenColumn(X)0 0
17007#else
17008# define IsHiddenColumn(X)(((X)->colFlags & 0x0002)!=0) 0
17009# define IsOrdinaryHiddenColumn(X)0 0
17010#endif
17011
17012
17013/* Does the table have a rowid */
17014#define HasRowid(X)(((X)->tabFlags & 0x0020)==0) (((X)->tabFlags & TF_WithoutRowid0x0020)==0)
17015#define VisibleRowid(X)(((X)->tabFlags & 0x0040)==0) (((X)->tabFlags & TF_NoVisibleRowid0x0040)==0)
17016
17017/*
17018** Each foreign key constraint is an instance of the following structure.
17019**
17020** A foreign key is associated with two tables. The "from" table is
17021** the table that contains the REFERENCES clause that creates the foreign
17022** key. The "to" table is the table that is named in the REFERENCES clause.
17023** Consider this example:
17024**
17025** CREATE TABLE ex1(
17026** a INTEGER PRIMARY KEY,
17027** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
17028** );
17029**
17030** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
17031** Equivalent names:
17032**
17033** from-table == child-table
17034** to-table == parent-table
17035**
17036** Each REFERENCES clause generates an instance of the following structure
17037** which is attached to the from-table. The to-table need not exist when
17038** the from-table is created. The existence of the to-table is not checked.
17039**
17040** The list of all parents for child Table X is held at X.pFKey.
17041**
17042** A list of all children for a table named Z (which might not even exist)
17043** is held in Schema.fkeyHash with a hash key of Z.
17044*/
17045struct FKey {
17046 Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
17047 FKey *pNextFrom; /* Next FKey with the same in pFrom. Next parent of pFrom */
17048 char *zTo; /* Name of table that the key points to (aka: Parent) */
17049 FKey *pNextTo; /* Next with the same zTo. Next child of zTo. */
17050 FKey *pPrevTo; /* Previous with the same zTo */
17051 int nCol; /* Number of columns in this key */
17052 /* EV: R-30323-21917 */
17053 u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
17054 u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
17055 Trigger *apTrigger[2];/* Triggers for aAction[] actions */
17056 struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
17057 int iFrom; /* Index of column in pFrom */
17058 char *zCol; /* Name of column in zTo. If NULL use PRIMARY KEY */
17059 } aCol[1]; /* One entry for each of nCol columns */
17060};
17061
17062/*
17063** SQLite supports many different ways to resolve a constraint
17064** error. ROLLBACK processing means that a constraint violation
17065** causes the operation in process to fail and for the current transaction
17066** to be rolled back. ABORT processing means the operation in process
17067** fails and any prior changes from that one operation are backed out,
17068** but the transaction is not rolled back. FAIL processing means that
17069** the operation in progress stops and returns an error code. But prior
17070** changes due to the same operation are not backed out and no rollback
17071** occurs. IGNORE means that the particular row that caused the constraint
17072** error is not inserted or updated. Processing continues and no error
17073** is returned. REPLACE means that preexisting database rows that caused
17074** a UNIQUE constraint violation are removed so that the new insert or
17075** update can proceed. Processing continues and no error is reported.
17076**
17077** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
17078** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
17079** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
17080** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
17081** referenced table row is propagated into the row that holds the
17082** foreign key.
17083**
17084** The following symbolic values are used to record which type
17085** of action to take.
17086*/
17087#define OE_None0 0 /* There is no constraint to check */
17088#define OE_Rollback1 1 /* Fail the operation and rollback the transaction */
17089#define OE_Abort2 2 /* Back out changes but do no rollback transaction */
17090#define OE_Fail3 3 /* Stop the operation but leave all prior changes */
17091#define OE_Ignore4 4 /* Ignore the error. Do not do the INSERT or UPDATE */
17092#define OE_Replace5 5 /* Delete existing record, then do INSERT or UPDATE */
17093#define OE_Update6 6 /* Process as a DO UPDATE in an upsert */
17094#define OE_Restrict7 7 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
17095#define OE_SetNull8 8 /* Set the foreign key value to NULL */
17096#define OE_SetDflt9 9 /* Set the foreign key value to its default */
17097#define OE_Cascade10 10 /* Cascade the changes */
17098#define OE_Default11 11 /* Do whatever the default action is */
17099
17100
17101/*
17102** An instance of the following structure is passed as the first
17103** argument to sqlite3VdbeKeyCompare and is used to control the
17104** comparison of the two index keys.
17105**
17106** Note that aSortOrder[] and aColl[] have nField+1 slots. There
17107** are nField slots for the columns of an index then one extra slot
17108** for the rowid at the end.
17109*/
17110struct KeyInfo {
17111 u32 nRef; /* Number of references to this KeyInfo object */
17112 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
17113 u16 nKeyField; /* Number of key columns in the index */
17114 u16 nAllField; /* Total columns, including key plus others */
17115 sqlite3 *db; /* The database connection */
17116 u8 *aSortOrder; /* Sort order for each column. */
17117 CollSeq *aColl[1]; /* Collating sequence for each term of the key */
17118};
17119
17120/*
17121** This object holds a record which has been parsed out into individual
17122** fields, for the purposes of doing a comparison.
17123**
17124** A record is an object that contains one or more fields of data.
17125** Records are used to store the content of a table row and to store
17126** the key of an index. A blob encoding of a record is created by
17127** the OP_MakeRecord opcode of the VDBE and is disassembled by the
17128** OP_Column opcode.
17129**
17130** An instance of this object serves as a "key" for doing a search on
17131** an index b+tree. The goal of the search is to find the entry that
17132** is closed to the key described by this object. This object might hold
17133** just a prefix of the key. The number of fields is given by
17134** pKeyInfo->nField.
17135**
17136** The r1 and r2 fields are the values to return if this key is less than
17137** or greater than a key in the btree, respectively. These are normally
17138** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
17139** is in DESC order.
17140**
17141** The key comparison functions actually return default_rc when they find
17142** an equals comparison. default_rc can be -1, 0, or +1. If there are
17143** multiple entries in the b-tree with the same key (when only looking
17144** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
17145** cause the search to find the last match, or +1 to cause the search to
17146** find the first match.
17147**
17148** The key comparison functions will set eqSeen to true if they ever
17149** get and equal results when comparing this structure to a b-tree record.
17150** When default_rc!=0, the search might end up on the record immediately
17151** before the first match or immediately after the last match. The
17152** eqSeen field will indicate whether or not an exact match exists in the
17153** b-tree.
17154*/
17155struct UnpackedRecord {
17156 KeyInfo *pKeyInfo; /* Collation and sort-order information */
17157 Mem *aMem; /* Values */
17158 u16 nField; /* Number of entries in apMem[] */
17159 i8 default_rc; /* Comparison result if keys are equal */
17160 u8 errCode; /* Error detected by xRecordCompare (CORRUPT or NOMEM) */
17161 i8 r1; /* Value to return if (lhs < rhs) */
17162 i8 r2; /* Value to return if (lhs > rhs) */
17163 u8 eqSeen; /* True if an equality comparison has been seen */
17164};
17165
17166
17167/*
17168** Each SQL index is represented in memory by an
17169** instance of the following structure.
17170**
17171** The columns of the table that are to be indexed are described
17172** by the aiColumn[] field of this structure. For example, suppose
17173** we have the following table and index:
17174**
17175** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
17176** CREATE INDEX Ex2 ON Ex1(c3,c1);
17177**
17178** In the Table structure describing Ex1, nCol==3 because there are
17179** three columns in the table. In the Index structure describing
17180** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
17181** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
17182** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
17183** The second column to be indexed (c1) has an index of 0 in
17184** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
17185**
17186** The Index.onError field determines whether or not the indexed columns
17187** must be unique and what to do if they are not. When Index.onError=OE_None,
17188** it means this is not a unique index. Otherwise it is a unique index
17189** and the value of Index.onError indicate the which conflict resolution
17190** algorithm to employ whenever an attempt is made to insert a non-unique
17191** element.
17192**
17193** While parsing a CREATE TABLE or CREATE INDEX statement in order to
17194** generate VDBE code (as opposed to parsing one read from an sqlite_master
17195** table as part of parsing an existing database schema), transient instances
17196** of this structure may be created. In this case the Index.tnum variable is
17197** used to store the address of a VDBE instruction, not a database page
17198** number (it cannot - the database page is not allocated until the VDBE
17199** program is executed). See convertToWithoutRowidTable() for details.
17200*/
17201struct Index {
17202 char *zName; /* Name of this index */
17203 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
17204 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
17205 Table *pTable; /* The SQL table being indexed */
17206 char *zColAff; /* String defining the affinity of each column */
17207 Index *pNext; /* The next index associated with the same table */
17208 Schema *pSchema; /* Schema containing this index */
17209 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
17210 const char **azColl; /* Array of collation sequence names for index */
17211 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
17212 ExprList *aColExpr; /* Column expressions */
17213 int tnum; /* DB Page containing root of this index */
17214 LogEst szIdxRow; /* Estimated average row size in bytes */
17215 u16 nKeyCol; /* Number of columns forming the key */
17216 u16 nColumn; /* Number of columns stored in the index */
17217 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
17218 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
17219 unsigned bUnordered:1; /* Use this index for == or IN queries only */
17220 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
17221 unsigned isResized:1; /* True if resizeIndexObject() has been called */
17222 unsigned isCovering:1; /* True if this is a covering index */
17223 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
17224 unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
17225 unsigned bNoQuery:1; /* Do not use this index to optimize queries */
17226 unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
17227#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
17228 int nSample; /* Number of elements in aSample[] */
17229 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
17230 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
17231 IndexSample *aSample; /* Samples of the left-most key */
17232 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
17233 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
17234#endif
17235 Bitmask colNotIdxed; /* 0 for unindexed columns in pTab */
17236};
17237
17238/*
17239** Allowed values for Index.idxType
17240*/
17241#define SQLITE_IDXTYPE_APPDEF0 0 /* Created using CREATE INDEX */
17242#define SQLITE_IDXTYPE_UNIQUE1 1 /* Implements a UNIQUE constraint */
17243#define SQLITE_IDXTYPE_PRIMARYKEY2 2 /* Is the PRIMARY KEY for the table */
17244#define SQLITE_IDXTYPE_IPK3 3 /* INTEGER PRIMARY KEY index */
17245
17246/* Return true if index X is a PRIMARY KEY index */
17247#define IsPrimaryKeyIndex(X)((X)->idxType==2) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY2)
17248
17249/* Return true if index X is a UNIQUE index */
17250#define IsUniqueIndex(X)((X)->onError!=0) ((X)->onError!=OE_None0)
17251
17252/* The Index.aiColumn[] values are normally positive integer. But
17253** there are some negative values that have special meaning:
17254*/
17255#define XN_ROWID(-1) (-1) /* Indexed column is the rowid */
17256#define XN_EXPR(-2) (-2) /* Indexed column is an expression */
17257
17258/*
17259** Each sample stored in the sqlite_stat3 table is represented in memory
17260** using a structure of this type. See documentation at the top of the
17261** analyze.c source file for additional information.
17262*/
17263struct IndexSample {
17264 void *p; /* Pointer to sampled record */
17265 int n; /* Size of record in bytes */
17266 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
17267 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
17268 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
17269};
17270
17271/*
17272** Possible values to use within the flags argument to sqlite3GetToken().
17273*/
17274#define SQLITE_TOKEN_QUOTED0x1 0x1 /* Token is a quoted identifier. */
17275#define SQLITE_TOKEN_KEYWORD0x2 0x2 /* Token is a keyword. */
17276
17277/*
17278** Each token coming out of the lexer is an instance of
17279** this structure. Tokens are also used as part of an expression.
17280**
17281** The memory that "z" points to is owned by other objects. Take care
17282** that the owner of the "z" string does not deallocate the string before
17283** the Token goes out of scope! Very often, the "z" points to some place
17284** in the middle of the Parse.zSql text. But it might also point to a
17285** static string.
17286*/
17287struct Token {
17288 const char *z; /* Text of the token. Not NULL-terminated! */
17289 unsigned int n; /* Number of characters in this token */
17290};
17291
17292/*
17293** An instance of this structure contains information needed to generate
17294** code for a SELECT that contains aggregate functions.
17295**
17296** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
17297** pointer to this structure. The Expr.iColumn field is the index in
17298** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
17299** code for that node.
17300**
17301** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
17302** original Select structure that describes the SELECT statement. These
17303** fields do not need to be freed when deallocating the AggInfo structure.
17304*/
17305struct AggInfo {
17306 u8 directMode; /* Direct rendering mode means take data directly
17307 ** from source tables rather than from accumulators */
17308 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
17309 ** than the source table */
17310 int sortingIdx; /* Cursor number of the sorting index */
17311 int sortingIdxPTab; /* Cursor number of pseudo-table */
17312 int nSortingColumn; /* Number of columns in the sorting index */
17313 int mnReg, mxReg; /* Range of registers allocated for aCol and aFunc */
17314 ExprList *pGroupBy; /* The group by clause */
17315 struct AggInfo_col { /* For each column used in source tables */
17316 Table *pTab; /* Source table */
17317 int iTable; /* Cursor number of the source table */
17318 int iColumn; /* Column number within the source table */
17319 int iSorterColumn; /* Column number in the sorting index */
17320 int iMem; /* Memory location that acts as accumulator */
17321 Expr *pExpr; /* The original expression */
17322 } *aCol;
17323 int nColumn; /* Number of used entries in aCol[] */
17324 int nAccumulator; /* Number of columns that show through to the output.
17325 ** Additional columns are used only as parameters to
17326 ** aggregate functions */
17327 struct AggInfo_func { /* For each aggregate function */
17328 Expr *pExpr; /* Expression encoding the function */
17329 FuncDef *pFunc; /* The aggregate function implementation */
17330 int iMem; /* Memory location that acts as accumulator */
17331 int iDistinct; /* Ephemeral table used to enforce DISTINCT */
17332 } *aFunc;
17333 int nFunc; /* Number of entries in aFunc[] */
17334};
17335
17336/*
17337** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
17338** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
17339** than 32767 we have to make it 32-bit. 16-bit is preferred because
17340** it uses less memory in the Expr object, which is a big memory user
17341** in systems with lots of prepared statements. And few applications
17342** need more than about 10 or 20 variables. But some extreme users want
17343** to have prepared statements with over 32767 variables, and for them
17344** the option is available (at compile-time).
17345*/
17346#if SQLITE_MAX_VARIABLE_NUMBER999<=32767
17347typedef i16 ynVar;
17348#else
17349typedef int ynVar;
17350#endif
17351
17352/*
17353** Each node of an expression in the parse tree is an instance
17354** of this structure.
17355**
17356** Expr.op is the opcode. The integer parser token codes are reused
17357** as opcodes here. For example, the parser defines TK_GE to be an integer
17358** code representing the ">=" operator. This same integer code is reused
17359** to represent the greater-than-or-equal-to operator in the expression
17360** tree.
17361**
17362** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
17363** or TK_STRING), then Expr.token contains the text of the SQL literal. If
17364** the expression is a variable (TK_VARIABLE), then Expr.token contains the
17365** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
17366** then Expr.token contains the name of the function.
17367**
17368** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
17369** binary operator. Either or both may be NULL.
17370**
17371** Expr.x.pList is a list of arguments if the expression is an SQL function,
17372** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
17373** Expr.x.pSelect is used if the expression is a sub-select or an expression of
17374** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
17375** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
17376** valid.
17377**
17378** An expression of the form ID or ID.ID refers to a column in a table.
17379** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
17380** the integer cursor number of a VDBE cursor pointing to that table and
17381** Expr.iColumn is the column number for the specific column. If the
17382** expression is used as a result in an aggregate SELECT, then the
17383** value is also stored in the Expr.iAgg column in the aggregate so that
17384** it can be accessed after all aggregates are computed.
17385**
17386** If the expression is an unbound variable marker (a question mark
17387** character '?' in the original SQL) then the Expr.iTable holds the index
17388** number for that variable.
17389**
17390** If the expression is a subquery then Expr.iColumn holds an integer
17391** register number containing the result of the subquery. If the
17392** subquery gives a constant result, then iTable is -1. If the subquery
17393** gives a different answer at different times during statement processing
17394** then iTable is the address of a subroutine that computes the subquery.
17395**
17396** If the Expr is of type OP_Column, and the table it is selecting from
17397** is a disk table or the "old.*" pseudo-table, then pTab points to the
17398** corresponding table definition.
17399**
17400** ALLOCATION NOTES:
17401**
17402** Expr objects can use a lot of memory space in database schema. To
17403** help reduce memory requirements, sometimes an Expr object will be
17404** truncated. And to reduce the number of memory allocations, sometimes
17405** two or more Expr objects will be stored in a single memory allocation,
17406** together with Expr.zToken strings.
17407**
17408** If the EP_Reduced and EP_TokenOnly flags are set when
17409** an Expr object is truncated. When EP_Reduced is set, then all
17410** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
17411** are contained within the same memory allocation. Note, however, that
17412** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
17413** allocated, regardless of whether or not EP_Reduced is set.
17414*/
17415struct Expr {
17416 u8 op; /* Operation performed by this node */
17417 char affinity; /* The affinity of the column or 0 if not a column */
17418 u32 flags; /* Various flags. EP_* See below */
17419 union {
17420 char *zToken; /* Token value. Zero terminated and dequoted */
17421 int iValue; /* Non-negative integer value if EP_IntValue */
17422 } u;
17423
17424 /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
17425 ** space is allocated for the fields below this point. An attempt to
17426 ** access them will result in a segfault or malfunction.
17427 *********************************************************************/
17428
17429 Expr *pLeft; /* Left subnode */
17430 Expr *pRight; /* Right subnode */
17431 union {
17432 ExprList *pList; /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
17433 Select *pSelect; /* EP_xIsSelect and op = IN, EXISTS, SELECT */
17434 } x;
17435
17436 /* If the EP_Reduced flag is set in the Expr.flags mask, then no
17437 ** space is allocated for the fields below this point. An attempt to
17438 ** access them will result in a segfault or malfunction.
17439 *********************************************************************/
17440
17441#if SQLITE_MAX_EXPR_DEPTH1000>0
17442 int nHeight; /* Height of the tree headed by this node */
17443#endif
17444 int iTable; /* TK_COLUMN: cursor number of table holding column
17445 ** TK_REGISTER: register number
17446 ** TK_TRIGGER: 1 -> new, 0 -> old
17447 ** EP_Unlikely: 134217728 times likelihood
17448 ** TK_SELECT: 1st register of result vector */
17449 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
17450 ** TK_VARIABLE: variable number (always >= 1).
17451 ** TK_SELECT_COLUMN: column of the result vector */
17452 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
17453 i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
17454 u8 op2; /* TK_REGISTER/TK_TRUTH: original value of Expr.op
17455 ** TK_COLUMN: the value of p5 for OP_Column
17456 ** TK_AGG_FUNCTION: nesting depth */
17457 AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
17458 union {
17459 Table *pTab; /* TK_COLUMN: Table containing column. Can be NULL
17460 ** for a column of an index on an expression */
17461 Window *pWin; /* TK_FUNCTION: Window definition for the func */
17462 struct { /* TK_IN, TK_SELECT, and TK_EXISTS */
17463 int iAddr; /* Subroutine entry address */
17464 int regReturn; /* Register used to hold return address */
17465 } sub;
17466 } y;
17467};
17468
17469/*
17470** The following are the meanings of bits in the Expr.flags field.
17471** Value restrictions:
17472**
17473** EP_Agg == NC_HasAgg == SF_HasAgg
17474** EP_Win == NC_HasWin
17475*/
17476#define EP_FromJoin0x000001 0x000001 /* Originates in ON/USING clause of outer join */
17477#define EP_Distinct0x000002 0x000002 /* Aggregate function with DISTINCT keyword */
17478#define EP_HasFunc0x000004 0x000004 /* Contains one or more functions of any kind */
17479#define EP_FixedCol0x000008 0x000008 /* TK_Column with a known fixed value */
17480#define EP_Agg0x000010 0x000010 /* Contains one or more aggregate functions */
17481#define EP_VarSelect0x000020 0x000020 /* pSelect is correlated, not constant */
17482#define EP_DblQuoted0x000040 0x000040 /* token.z was originally in "..." */
17483#define EP_InfixFunc0x000080 0x000080 /* True for an infix function: LIKE, GLOB, etc */
17484#define EP_Collate0x000100 0x000100 /* Tree contains a TK_COLLATE operator */
17485#define EP_Generic0x000200 0x000200 /* Ignore COLLATE or affinity on this tree */
17486#define EP_IntValue0x000400 0x000400 /* Integer value contained in u.iValue */
17487#define EP_xIsSelect0x000800 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
17488#define EP_Skip0x001000 0x001000 /* Operator does not contribute to affinity */
17489#define EP_Reduced0x002000 0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
17490#define EP_TokenOnly0x004000 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
17491#define EP_Win0x008000 0x008000 /* Contains window functions */
17492#define EP_MemToken0x010000 0x010000 /* Need to sqlite3DbFree() Expr.zToken */
17493#define EP_NoReduce0x020000 0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
17494#define EP_Unlikely0x040000 0x040000 /* unlikely() or likelihood() function */
17495#define EP_ConstFunc0x080000 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
17496#define EP_CanBeNull0x100000 0x100000 /* Can be null despite NOT NULL constraint */
17497#define EP_Subquery0x200000 0x200000 /* Tree contains a TK_SELECT operator */
17498#define EP_Alias0x400000 0x400000 /* Is an alias for a result set column */
17499#define EP_Leaf0x800000 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
17500#define EP_WinFunc0x1000000 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
17501#define EP_Subrtn0x2000000 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
17502#define EP_Quoted0x4000000 0x4000000 /* TK_ID was originally quoted */
17503#define EP_Static0x8000000 0x8000000 /* Held in memory not obtained from malloc() */
17504#define EP_IsTrue0x10000000 0x10000000 /* Always has boolean value of TRUE */
17505#define EP_IsFalse0x20000000 0x20000000 /* Always has boolean value of FALSE */
17506
17507/*
17508** The EP_Propagate mask is a set of properties that automatically propagate
17509** upwards into parent nodes.
17510*/
17511#define EP_Propagate(0x000100|0x200000|0x000004) (EP_Collate0x000100|EP_Subquery0x200000|EP_HasFunc0x000004)
17512
17513/*
17514** These macros can be used to test, set, or clear bits in the
17515** Expr.flags field.
17516*/
17517#define ExprHasProperty(E,P)(((E)->flags&(P))!=0) (((E)->flags&(P))!=0)
17518#define ExprHasAllProperty(E,P)(((E)->flags&(P))==(P)) (((E)->flags&(P))==(P))
17519#define ExprSetProperty(E,P)(E)->flags|=(P) (E)->flags|=(P)
17520#define ExprClearProperty(E,P)(E)->flags&=~(P) (E)->flags&=~(P)
17521#define ExprAlwaysTrue(E)(((E)->flags&(0x000001|0x10000000))==0x10000000) (((E)->flags&(EP_FromJoin0x000001|EP_IsTrue0x10000000))==EP_IsTrue0x10000000)
17522#define ExprAlwaysFalse(E)(((E)->flags&(0x000001|0x20000000))==0x20000000) (((E)->flags&(EP_FromJoin0x000001|EP_IsFalse0x20000000))==EP_IsFalse0x20000000)
17523
17524/* The ExprSetVVAProperty() macro is used for Verification, Validation,
17525** and Accreditation only. It works like ExprSetProperty() during VVA
17526** processes but is a no-op for delivery.
17527*/
17528#ifdef SQLITE_DEBUG
17529# define ExprSetVVAProperty(E,P) (E)->flags|=(P)
17530#else
17531# define ExprSetVVAProperty(E,P)
17532#endif
17533
17534/*
17535** Macros to determine the number of bytes required by a normal Expr
17536** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
17537** and an Expr struct with the EP_TokenOnly flag set.
17538*/
17539#define EXPR_FULLSIZEsizeof(Expr) sizeof(Expr) /* Full size */
17540#define EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable) offsetof(Expr,iTable)__builtin_offsetof(Expr, iTable) /* Common features */
17541#define EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft) offsetof(Expr,pLeft)__builtin_offsetof(Expr, pLeft) /* Fewer features */
17542
17543/*
17544** Flags passed to the sqlite3ExprDup() function. See the header comment
17545** above sqlite3ExprDup() for details.
17546*/
17547#define EXPRDUP_REDUCE0x0001 0x0001 /* Used reduced-size Expr nodes */
17548
17549/*
17550** A list of expressions. Each expression may optionally have a
17551** name. An expr/name combination can be used in several ways, such
17552** as the list of "expr AS ID" fields following a "SELECT" or in the
17553** list of "ID = expr" items in an UPDATE. A list of expressions can
17554** also be used as the argument to a function, in which case the a.zName
17555** field is not used.
17556**
17557** By default the Expr.zSpan field holds a human-readable description of
17558** the expression that is used in the generation of error messages and
17559** column labels. In this case, Expr.zSpan is typically the text of a
17560** column expression as it exists in a SELECT statement. However, if
17561** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
17562** of the result column in the form: DATABASE.TABLE.COLUMN. This later
17563** form is used for name resolution with nested FROM clauses.
17564*/
17565struct ExprList {
17566 int nExpr; /* Number of expressions on the list */
17567 struct ExprList_item { /* For each expression in the list */
17568 Expr *pExpr; /* The parse tree for this expression */
17569 char *zName; /* Token associated with this expression */
17570 char *zSpan; /* Original text of the expression */
17571 u8 sortOrder; /* 1 for DESC or 0 for ASC */
17572 unsigned done :1; /* A flag to indicate when processing is finished */
17573 unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
17574 unsigned reusable :1; /* Constant expression is reusable */
17575 unsigned bSorterRef :1; /* Defer evaluation until after sorting */
17576 union {
17577 struct {
17578 u16 iOrderByCol; /* For ORDER BY, column number in result set */
17579 u16 iAlias; /* Index into Parse.aAlias[] for zName */
17580 } x;
17581 int iConstExprReg; /* Register in which Expr value is cached */
17582 } u;
17583 } a[1]; /* One slot for each expression in the list */
17584};
17585
17586/*
17587** An instance of this structure can hold a simple list of identifiers,
17588** such as the list "a,b,c" in the following statements:
17589**
17590** INSERT INTO t(a,b,c) VALUES ...;
17591** CREATE INDEX idx ON t(a,b,c);
17592** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
17593**
17594** The IdList.a.idx field is used when the IdList represents the list of
17595** column names after a table name in an INSERT statement. In the statement
17596**
17597** INSERT INTO t(a,b,c) ...
17598**
17599** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
17600*/
17601struct IdList {
17602 struct IdList_item {
17603 char *zName; /* Name of the identifier */
17604 int idx; /* Index in some Table.aCol[] of a column named zName */
17605 } *a;
17606 int nId; /* Number of identifiers on the list */
17607};
17608
17609/*
17610** The following structure describes the FROM clause of a SELECT statement.
17611** Each table or subquery in the FROM clause is a separate element of
17612** the SrcList.a[] array.
17613**
17614** With the addition of multiple database support, the following structure
17615** can also be used to describe a particular table such as the table that
17616** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
17617** such a table must be a simple name: ID. But in SQLite, the table can
17618** now be identified by a database name, a dot, then the table name: ID.ID.
17619**
17620** The jointype starts out showing the join type between the current table
17621** and the next table on the list. The parser builds the list this way.
17622** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
17623** jointype expresses the join between the table and the previous table.
17624**
17625** In the colUsed field, the high-order bit (bit 63) is set if the table
17626** contains more than 63 columns and the 64-th or later column is used.
17627*/
17628struct SrcList {
17629 int nSrc; /* Number of tables or subqueries in the FROM clause */
17630 u32 nAlloc; /* Number of entries allocated in a[] below */
17631 struct SrcList_item {
17632 Schema *pSchema; /* Schema to which this item is fixed */
17633 char *zDatabase; /* Name of database holding this table */
17634 char *zName; /* Name of the table */
17635 char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
17636 Table *pTab; /* An SQL table corresponding to zName */
17637 Select *pSelect; /* A SELECT statement used in place of a table name */
17638 int addrFillSub; /* Address of subroutine to manifest a subquery */
17639 int regReturn; /* Register holding return address of addrFillSub */
17640 int regResult; /* Registers holding results of a co-routine */
17641 struct {
17642 u8 jointype; /* Type of join between this table and the previous */
17643 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
17644 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
17645 unsigned isTabFunc :1; /* True if table-valued-function syntax */
17646 unsigned isCorrelated :1; /* True if sub-query is correlated */
17647 unsigned viaCoroutine :1; /* Implemented as a co-routine */
17648 unsigned isRecursive :1; /* True for recursive reference in WITH */
17649 } fg;
17650 int iCursor; /* The VDBE cursor number used to access this table */
17651 Expr *pOn; /* The ON clause of a join */
17652 IdList *pUsing; /* The USING clause of a join */
17653 Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
17654 union {
17655 char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
17656 ExprList *pFuncArg; /* Arguments to table-valued-function */
17657 } u1;
17658 Index *pIBIndex; /* Index structure corresponding to u1.zIndexedBy */
17659 } a[1]; /* One entry for each identifier on the list */
17660};
17661
17662/*
17663** Permitted values of the SrcList.a.jointype field
17664*/
17665#define JT_INNER0x0001 0x0001 /* Any kind of inner or cross join */
17666#define JT_CROSS0x0002 0x0002 /* Explicit use of the CROSS keyword */
17667#define JT_NATURAL0x0004 0x0004 /* True for a "natural" join */
17668#define JT_LEFT0x0008 0x0008 /* Left outer join */
17669#define JT_RIGHT0x0010 0x0010 /* Right outer join */
17670#define JT_OUTER0x0020 0x0020 /* The "OUTER" keyword is present */
17671#define JT_ERROR0x0040 0x0040 /* unknown or unsupported join type */
17672
17673
17674/*
17675** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
17676** and the WhereInfo.wctrlFlags member.
17677**
17678** Value constraints (enforced via assert()):
17679** WHERE_USE_LIMIT == SF_FixedLimit
17680*/
17681#define WHERE_ORDERBY_NORMAL0x0000 0x0000 /* No-op */
17682#define WHERE_ORDERBY_MIN0x0001 0x0001 /* ORDER BY processing for min() func */
17683#define WHERE_ORDERBY_MAX0x0002 0x0002 /* ORDER BY processing for max() func */
17684#define WHERE_ONEPASS_DESIRED0x0004 0x0004 /* Want to do one-pass UPDATE/DELETE */
17685#define WHERE_ONEPASS_MULTIROW0x0008 0x0008 /* ONEPASS is ok with multiple rows */
17686#define WHERE_DUPLICATES_OK0x0010 0x0010 /* Ok to return a row more than once */
17687#define WHERE_OR_SUBCLAUSE0x0020 0x0020 /* Processing a sub-WHERE as part of
17688 ** the OR optimization */
17689#define WHERE_GROUPBY0x0040 0x0040 /* pOrderBy is really a GROUP BY */
17690#define WHERE_DISTINCTBY0x0080 0x0080 /* pOrderby is really a DISTINCT clause */
17691#define WHERE_WANT_DISTINCT0x0100 0x0100 /* All output needs to be distinct */
17692#define WHERE_SORTBYGROUP0x0200 0x0200 /* Support sqlite3WhereIsSorted() */
17693#define WHERE_SEEK_TABLE0x0400 0x0400 /* Do not defer seeks on main table */
17694#define WHERE_ORDERBY_LIMIT0x0800 0x0800 /* ORDERBY+LIMIT on the inner loop */
17695#define WHERE_SEEK_UNIQ_TABLE0x1000 0x1000 /* Do not defer seeks if unique */
17696 /* 0x2000 not currently used */
17697#define WHERE_USE_LIMIT0x4000 0x4000 /* Use the LIMIT in cost estimates */
17698 /* 0x8000 not currently used */
17699
17700/* Allowed return values from sqlite3WhereIsDistinct()
17701*/
17702#define WHERE_DISTINCT_NOOP0 0 /* DISTINCT keyword not used */
17703#define WHERE_DISTINCT_UNIQUE1 1 /* No duplicates */
17704#define WHERE_DISTINCT_ORDERED2 2 /* All duplicates are adjacent */
17705#define WHERE_DISTINCT_UNORDERED3 3 /* Duplicates are scattered */
17706
17707/*
17708** A NameContext defines a context in which to resolve table and column
17709** names. The context consists of a list of tables (the pSrcList) field and
17710** a list of named expression (pEList). The named expression list may
17711** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
17712** to the table being operated on by INSERT, UPDATE, or DELETE. The
17713** pEList corresponds to the result set of a SELECT and is NULL for
17714** other statements.
17715**
17716** NameContexts can be nested. When resolving names, the inner-most
17717** context is searched first. If no match is found, the next outer
17718** context is checked. If there is still no match, the next context
17719** is checked. This process continues until either a match is found
17720** or all contexts are check. When a match is found, the nRef member of
17721** the context containing the match is incremented.
17722**
17723** Each subquery gets a new NameContext. The pNext field points to the
17724** NameContext in the parent query. Thus the process of scanning the
17725** NameContext list corresponds to searching through successively outer
17726** subqueries looking for a match.
17727*/
17728struct NameContext {
17729 Parse *pParse; /* The parser */
17730 SrcList *pSrcList; /* One or more tables used to resolve names */
17731 union {
17732 ExprList *pEList; /* Optional list of result-set columns */
17733 AggInfo *pAggInfo; /* Information about aggregates at this level */
17734 Upsert *pUpsert; /* ON CONFLICT clause information from an upsert */
17735 } uNC;
17736 NameContext *pNext; /* Next outer name context. NULL for outermost */
17737 int nRef; /* Number of names resolved by this context */
17738 int nErr; /* Number of errors encountered while resolving names */
17739 int ncFlags; /* Zero or more NC_* flags defined below */
17740 Select *pWinSelect; /* SELECT statement for any window functions */
17741};
17742
17743/*
17744** Allowed values for the NameContext, ncFlags field.
17745**
17746** Value constraints (all checked via assert()):
17747** NC_HasAgg == SF_HasAgg == EP_Agg
17748** NC_MinMaxAgg == SF_MinMaxAgg == SQLITE_FUNC_MINMAX
17749** NC_HasWin == EP_Win
17750**
17751*/
17752#define NC_AllowAgg0x0001 0x0001 /* Aggregate functions are allowed here */
17753#define NC_PartIdx0x0002 0x0002 /* True if resolving a partial index WHERE */
17754#define NC_IsCheck0x0004 0x0004 /* True if resolving names in a CHECK constraint */
17755#define NC_InAggFunc0x0008 0x0008 /* True if analyzing arguments to an agg func */
17756#define NC_HasAgg0x0010 0x0010 /* One or more aggregate functions seen */
17757#define NC_IdxExpr0x0020 0x0020 /* True if resolving columns of CREATE INDEX */
17758#define NC_VarSelect0x0040 0x0040 /* A correlated subquery has been seen */
17759#define NC_UEList0x0080 0x0080 /* True if uNC.pEList is used */
17760#define NC_UAggInfo0x0100 0x0100 /* True if uNC.pAggInfo is used */
17761#define NC_UUpsert0x0200 0x0200 /* True if uNC.pUpsert is used */
17762#define NC_MinMaxAgg0x1000 0x1000 /* min/max aggregates seen. See note above */
17763#define NC_Complex0x2000 0x2000 /* True if a function or subquery seen */
17764#define NC_AllowWin0x4000 0x4000 /* Window functions are allowed here */
17765#define NC_HasWin0x8000 0x8000 /* One or more window functions seen */
17766#define NC_IsDDL0x10000 0x10000 /* Resolving names in a CREATE statement */
17767
17768/*
17769** An instance of the following object describes a single ON CONFLICT
17770** clause in an upsert.
17771**
17772** The pUpsertTarget field is only set if the ON CONFLICT clause includes
17773** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
17774** conflict-target clause.) The pUpsertTargetWhere is the optional
17775** WHERE clause used to identify partial unique indexes.
17776**
17777** pUpsertSet is the list of column=expr terms of the UPDATE statement.
17778** The pUpsertSet field is NULL for a ON CONFLICT DO NOTHING. The
17779** pUpsertWhere is the WHERE clause for the UPDATE and is NULL if the
17780** WHERE clause is omitted.
17781*/
17782struct Upsert {
17783 ExprList *pUpsertTarget; /* Optional description of conflicting index */
17784 Expr *pUpsertTargetWhere; /* WHERE clause for partial index targets */
17785 ExprList *pUpsertSet; /* The SET clause from an ON CONFLICT UPDATE */
17786 Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
17787 /* The fields above comprise the parse tree for the upsert clause.
17788 ** The fields below are used to transfer information from the INSERT
17789 ** processing down into the UPDATE processing while generating code.
17790 ** Upsert owns the memory allocated above, but not the memory below. */
17791 Index *pUpsertIdx; /* Constraint that pUpsertTarget identifies */
17792 SrcList *pUpsertSrc; /* Table to be updated */
17793 int regData; /* First register holding array of VALUES */
17794 int iDataCur; /* Index of the data cursor */
17795 int iIdxCur; /* Index of the first index cursor */
17796};
17797
17798/*
17799** An instance of the following structure contains all information
17800** needed to generate code for a single SELECT statement.
17801**
17802** See the header comment on the computeLimitRegisters() routine for a
17803** detailed description of the meaning of the iLimit and iOffset fields.
17804**
17805** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
17806** These addresses must be stored so that we can go back and fill in
17807** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
17808** the number of columns in P2 can be computed at the same time
17809** as the OP_OpenEphm instruction is coded because not
17810** enough information about the compound query is known at that point.
17811** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
17812** for the result set. The KeyInfo for addrOpenEphm[2] contains collating
17813** sequences for the ORDER BY clause.
17814*/
17815struct Select {
17816 ExprList *pEList; /* The fields of the result */
17817 u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
17818 LogEst nSelectRow; /* Estimated number of result rows */
17819 u32 selFlags; /* Various SF_* values */
17820 int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
17821 u32 selId; /* Unique identifier number for this SELECT */
17822 int addrOpenEphm[2]; /* OP_OpenEphem opcodes related to this select */
17823 SrcList *pSrc; /* The FROM clause */
17824 Expr *pWhere; /* The WHERE clause */
17825 ExprList *pGroupBy; /* The GROUP BY clause */
17826 Expr *pHaving; /* The HAVING clause */
17827 ExprList *pOrderBy; /* The ORDER BY clause */
17828 Select *pPrior; /* Prior select in a compound select statement */
17829 Select *pNext; /* Next select to the left in a compound */
17830 Expr *pLimit; /* LIMIT expression. NULL means not used. */
17831 With *pWith; /* WITH clause attached to this select. Or NULL. */
17832#ifndef SQLITE_OMIT_WINDOWFUNC
17833 Window *pWin; /* List of window functions */
17834 Window *pWinDefn; /* List of named window definitions */
17835#endif
17836};
17837
17838/*
17839** Allowed values for Select.selFlags. The "SF" prefix stands for
17840** "Select Flag".
17841**
17842** Value constraints (all checked via assert())
17843** SF_HasAgg == NC_HasAgg
17844** SF_MinMaxAgg == NC_MinMaxAgg == SQLITE_FUNC_MINMAX
17845** SF_FixedLimit == WHERE_USE_LIMIT
17846*/
17847#define SF_Distinct0x00001 0x00001 /* Output should be DISTINCT */
17848#define SF_All0x00002 0x00002 /* Includes the ALL keyword */
17849#define SF_Resolved0x00004 0x00004 /* Identifiers have been resolved */
17850#define SF_Aggregate0x00008 0x00008 /* Contains agg functions or a GROUP BY */
17851#define SF_HasAgg0x00010 0x00010 /* Contains aggregate functions */
17852#define SF_UsesEphemeral0x00020 0x00020 /* Uses the OpenEphemeral opcode */
17853#define SF_Expanded0x00040 0x00040 /* sqlite3SelectExpand() called on this */
17854#define SF_HasTypeInfo0x00080 0x00080 /* FROM subqueries have Table metadata */
17855#define SF_Compound0x00100 0x00100 /* Part of a compound query */
17856#define SF_Values0x00200 0x00200 /* Synthesized from VALUES clause */
17857#define SF_MultiValue0x00400 0x00400 /* Single VALUES term with multiple rows */
17858#define SF_NestedFrom0x00800 0x00800 /* Part of a parenthesized FROM clause */
17859#define SF_MinMaxAgg0x01000 0x01000 /* Aggregate containing min() or max() */
17860#define SF_Recursive0x02000 0x02000 /* The recursive part of a recursive CTE */
17861#define SF_FixedLimit0x04000 0x04000 /* nSelectRow set by a constant LIMIT */
17862#define SF_MaybeConvert0x08000 0x08000 /* Need convertCompoundSelectToSubquery() */
17863#define SF_Converted0x10000 0x10000 /* By convertCompoundSelectToSubquery() */
17864#define SF_IncludeHidden0x20000 0x20000 /* Include hidden columns in output */
17865#define SF_ComplexResult0x40000 0x40000 /* Result contains subquery or function */
17866
17867/*
17868** The results of a SELECT can be distributed in several ways, as defined
17869** by one of the following macros. The "SRT" prefix means "SELECT Result
17870** Type".
17871**
17872** SRT_Union Store results as a key in a temporary index
17873** identified by pDest->iSDParm.
17874**
17875** SRT_Except Remove results from the temporary index pDest->iSDParm.
17876**
17877** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
17878** set is not empty.
17879**
17880** SRT_Discard Throw the results away. This is used by SELECT
17881** statements within triggers whose only purpose is
17882** the side-effects of functions.
17883**
17884** All of the above are free to ignore their ORDER BY clause. Those that
17885** follow must honor the ORDER BY clause.
17886**
17887** SRT_Output Generate a row of output (using the OP_ResultRow
17888** opcode) for each row in the result set.
17889**
17890** SRT_Mem Only valid if the result is a single column.
17891** Store the first column of the first result row
17892** in register pDest->iSDParm then abandon the rest
17893** of the query. This destination implies "LIMIT 1".
17894**
17895** SRT_Set The result must be a single column. Store each
17896** row of result as the key in table pDest->iSDParm.
17897** Apply the affinity pDest->affSdst before storing
17898** results. Used to implement "IN (SELECT ...)".
17899**
17900** SRT_EphemTab Create an temporary table pDest->iSDParm and store
17901** the result there. The cursor is left open after
17902** returning. This is like SRT_Table except that
17903** this destination uses OP_OpenEphemeral to create
17904** the table first.
17905**
17906** SRT_Coroutine Generate a co-routine that returns a new row of
17907** results each time it is invoked. The entry point
17908** of the co-routine is stored in register pDest->iSDParm
17909** and the result row is stored in pDest->nDest registers
17910** starting with pDest->iSdst.
17911**
17912** SRT_Table Store results in temporary table pDest->iSDParm.
17913** SRT_Fifo This is like SRT_EphemTab except that the table
17914** is assumed to already be open. SRT_Fifo has
17915** the additional property of being able to ignore
17916** the ORDER BY clause.
17917**
17918** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
17919** But also use temporary table pDest->iSDParm+1 as
17920** a record of all prior results and ignore any duplicate
17921** rows. Name means: "Distinct Fifo".
17922**
17923** SRT_Queue Store results in priority queue pDest->iSDParm (really
17924** an index). Append a sequence number so that all entries
17925** are distinct.
17926**
17927** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
17928** the same record has never been stored before. The
17929** index at pDest->iSDParm+1 hold all prior stores.
17930*/
17931#define SRT_Union1 1 /* Store result as keys in an index */
17932#define SRT_Except2 2 /* Remove result from a UNION index */
17933#define SRT_Exists3 3 /* Store 1 if the result is not empty */
17934#define SRT_Discard4 4 /* Do not save the results anywhere */
17935#define SRT_Fifo5 5 /* Store result as data with an automatic rowid */
17936#define SRT_DistFifo6 6 /* Like SRT_Fifo, but unique results only */
17937#define SRT_Queue7 7 /* Store result in an queue */
17938#define SRT_DistQueue8 8 /* Like SRT_Queue, but unique results only */
17939
17940/* The ORDER BY clause is ignored for all of the above */
17941#define IgnorableOrderby(X)((X->eDest)<=8) ((X->eDest)<=SRT_DistQueue8)
17942
17943#define SRT_Output9 9 /* Output each row of result */
17944#define SRT_Mem10 10 /* Store result in a memory cell */
17945#define SRT_Set11 11 /* Store results as keys in an index */
17946#define SRT_EphemTab12 12 /* Create transient tab and store like SRT_Table */
17947#define SRT_Coroutine13 13 /* Generate a single row of result */
17948#define SRT_Table14 14 /* Store result as data with an automatic rowid */
17949
17950/*
17951** An instance of this object describes where to put of the results of
17952** a SELECT statement.
17953*/
17954struct SelectDest {
17955 u8 eDest; /* How to dispose of the results. On of SRT_* above. */
17956 int iSDParm; /* A parameter used by the eDest disposal method */
17957 int iSdst; /* Base register where results are written */
17958 int nSdst; /* Number of registers allocated */
17959 char *zAffSdst; /* Affinity used when eDest==SRT_Set */
17960 ExprList *pOrderBy; /* Key columns for SRT_Queue and SRT_DistQueue */
17961};
17962
17963/*
17964** During code generation of statements that do inserts into AUTOINCREMENT
17965** tables, the following information is attached to the Table.u.autoInc.p
17966** pointer of each autoincrement table to record some side information that
17967** the code generator needs. We have to keep per-table autoincrement
17968** information in case inserts are done within triggers. Triggers do not
17969** normally coordinate their activities, but we do need to coordinate the
17970** loading and saving of autoincrement information.
17971*/
17972struct AutoincInfo {
17973 AutoincInfo *pNext; /* Next info block in a list of them all */
17974 Table *pTab; /* Table this info block refers to */
17975 int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
17976 int regCtr; /* Memory register holding the rowid counter */
17977};
17978
17979/*
17980** At least one instance of the following structure is created for each
17981** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
17982** statement. All such objects are stored in the linked list headed at
17983** Parse.pTriggerPrg and deleted once statement compilation has been
17984** completed.
17985**
17986** A Vdbe sub-program that implements the body and WHEN clause of trigger
17987** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
17988** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
17989** The Parse.pTriggerPrg list never contains two entries with the same
17990** values for both pTrigger and orconf.
17991**
17992** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
17993** accessed (or set to 0 for triggers fired as a result of INSERT
17994** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
17995** a mask of new.* columns used by the program.
17996*/
17997struct TriggerPrg {
17998 Trigger *pTrigger; /* Trigger this program was coded from */
17999 TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
18000 SubProgram *pProgram; /* Program implementing pTrigger/orconf */
18001 int orconf; /* Default ON CONFLICT policy */
18002 u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
18003};
18004
18005/*
18006** The yDbMask datatype for the bitmask of all attached databases.
18007*/
18008#if SQLITE_MAX_ATTACHED10>30
18009 typedef unsigned char yDbMask[(SQLITE_MAX_ATTACHED10+9)/8];
18010# define DbMaskTest(M,I)(((M)&(((yDbMask)1)<<(I)))!=0) (((M)[(I)/8]&(1<<((I)&7)))!=0)
18011# define DbMaskZero(M)(M)=0 memset((M),0,sizeof(M))
18012# define DbMaskSet(M,I)(M)|=(((yDbMask)1)<<(I)) (M)[(I)/8]|=(1<<((I)&7))
18013# define DbMaskAllZero(M)(M)==0 sqlite3DbMaskAllZero(M)
18014# define DbMaskNonZero(M)(M)!=0 (sqlite3DbMaskAllZero(M)==0)
18015#else
18016 typedef unsigned int yDbMask;
18017# define DbMaskTest(M,I)(((M)&(((yDbMask)1)<<(I)))!=0) (((M)&(((yDbMask)1)<<(I)))!=0)
18018# define DbMaskZero(M)(M)=0 (M)=0
18019# define DbMaskSet(M,I)(M)|=(((yDbMask)1)<<(I)) (M)|=(((yDbMask)1)<<(I))
18020# define DbMaskAllZero(M)(M)==0 (M)==0
18021# define DbMaskNonZero(M)(M)!=0 (M)!=0
18022#endif
18023
18024/*
18025** An SQL parser context. A copy of this structure is passed through
18026** the parser and down into all the parser action routine in order to
18027** carry around information that is global to the entire parse.
18028**
18029** The structure is divided into two parts. When the parser and code
18030** generate call themselves recursively, the first part of the structure
18031** is constant but the second part is reset at the beginning and end of
18032** each recursion.
18033**
18034** The nTableLock and aTableLock variables are only used if the shared-cache
18035** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
18036** used to store the set of table-locks required by the statement being
18037** compiled. Function sqlite3TableLock() is used to add entries to the
18038** list.
18039*/
18040struct Parse {
18041 sqlite3 *db; /* The main database structure */
18042 char *zErrMsg; /* An error message */
18043 Vdbe *pVdbe; /* An engine for executing database bytecode */
18044 int rc; /* Return code from execution */
18045 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
18046 u8 checkSchema; /* Causes schema cookie check after an error */
18047 u8 nested; /* Number of nested calls to the parser/code generator */
18048 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
18049 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
18050 u8 mayAbort; /* True if statement may throw an ABORT exception */
18051 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
18052 u8 okConstFactor; /* OK to factor out constants */
18053 u8 disableLookaside; /* Number of times lookaside has been disabled */
18054 u8 disableVtab; /* Disable all virtual tables for this parse */
18055 int nRangeReg; /* Size of the temporary register block */
18056 int iRangeReg; /* First register in temporary register block */
18057 int nErr; /* Number of errors seen */
18058 int nTab; /* Number of previously allocated VDBE cursors */
18059 int nMem; /* Number of memory cells used so far */
18060 int szOpAlloc; /* Bytes of memory space allocated for Vdbe.aOp[] */
18061 int iSelfTab; /* Table associated with an index on expr, or negative
18062 ** of the base register during check-constraint eval */
18063 int nLabel; /* The *negative* of the number of labels used */
18064 int nLabelAlloc; /* Number of slots in aLabel */
18065 int *aLabel; /* Space to hold the labels */
18066 ExprList *pConstExpr;/* Constant expressions */
18067 Token constraintName;/* Name of the constraint currently being parsed */
18068 yDbMask writeMask; /* Start a write transaction on these databases */
18069 yDbMask cookieMask; /* Bitmask of schema verified databases */
18070 int regRowid; /* Register holding rowid of CREATE TABLE entry */
18071 int regRoot; /* Register holding root page number for new objects */
18072 int nMaxArg; /* Max args passed to user function by sub-program */
18073 int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
18074#ifndef SQLITE_OMIT_SHARED_CACHE
18075 int nTableLock; /* Number of locks in aTableLock */
18076 TableLock *aTableLock; /* Required table locks for shared-cache mode */
18077#endif
18078 AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
18079 Parse *pToplevel; /* Parse structure for main program (or NULL) */
18080 Table *pTriggerTab; /* Table triggers are being coded for */
18081 Parse *pParentParse; /* Parent parser if this parser is nested */
18082 int addrCrTab; /* Address of OP_CreateBtree opcode on CREATE TABLE */
18083 u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
18084 u32 oldmask; /* Mask of old.* columns referenced */
18085 u32 newmask; /* Mask of new.* columns referenced */
18086 u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
18087 u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
18088 u8 disableTriggers; /* True to disable triggers */
18089
18090 /**************************************************************************
18091 ** Fields above must be initialized to zero. The fields that follow,
18092 ** down to the beginning of the recursive section, do not need to be
18093 ** initialized as they will be set before being used. The boundary is
18094 ** determined by offsetof(Parse,aTempReg).
18095 **************************************************************************/
18096
18097 int aTempReg[8]; /* Holding area for temporary registers */
18098 Token sNameToken; /* Token with unqualified schema object name */
18099
18100 /************************************************************************
18101 ** Above is constant between recursions. Below is reset before and after
18102 ** each recursion. The boundary between these two regions is determined
18103 ** using offsetof(Parse,sLastToken) so the sLastToken field must be the
18104 ** first field in the recursive region.
18105 ************************************************************************/
18106
18107 Token sLastToken; /* The last token parsed */
18108 ynVar nVar; /* Number of '?' variables seen in the SQL so far */
18109 u8 iPkSortOrder; /* ASC or DESC for INTEGER PRIMARY KEY */
18110 u8 explain; /* True if the EXPLAIN flag is found on the query */
18111#if !(defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE))
18112 u8 eParseMode; /* PARSE_MODE_XXX constant */
18113#endif
18114#ifndef SQLITE_OMIT_VIRTUALTABLE
18115 int nVtabLock; /* Number of virtual tables to lock */
18116#endif
18117 int nHeight; /* Expression tree height of current sub-select */
18118#ifndef SQLITE_OMIT_EXPLAIN
18119 int addrExplain; /* Address of current OP_Explain opcode */
18120#endif
18121 VList *pVList; /* Mapping between variable names and numbers */
18122 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
18123 const char *zTail; /* All SQL text past the last semicolon parsed */
18124 Table *pNewTable; /* A table being constructed by CREATE TABLE */
18125 Index *pNewIndex; /* An index being constructed by CREATE INDEX.
18126 ** Also used to hold redundant UNIQUE constraints
18127 ** during a RENAME COLUMN */
18128 Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
18129 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
18130#ifndef SQLITE_OMIT_VIRTUALTABLE
18131 Token sArg; /* Complete text of a module argument */
18132 Table **apVtabLock; /* Pointer to virtual tables needing locking */
18133#endif
18134 Table *pZombieTab; /* List of Table objects to delete after code gen */
18135 TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
18136 With *pWith; /* Current WITH clause, or NULL */
18137 With *pWithToFree; /* Free this WITH object at the end of the parse */
18138#ifndef SQLITE_OMIT_ALTERTABLE
18139 RenameToken *pRename; /* Tokens subject to renaming by ALTER TABLE */
18140#endif
18141};
18142
18143#define PARSE_MODE_NORMAL0 0
18144#define PARSE_MODE_DECLARE_VTAB1 1
18145#define PARSE_MODE_RENAME_COLUMN2 2
18146#define PARSE_MODE_RENAME_TABLE3 3
18147
18148/*
18149** Sizes and pointers of various parts of the Parse object.
18150*/
18151#define PARSE_HDR_SZ__builtin_offsetof(Parse, aTempReg) offsetof(Parse,aTempReg)__builtin_offsetof(Parse, aTempReg) /* Recursive part w/o aColCache*/
18152#define PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken) offsetof(Parse,sLastToken)__builtin_offsetof(Parse, sLastToken) /* Recursive part */
18153#define PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)) (sizeof(Parse)-PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken)) /* Non-recursive part */
18154#define PARSE_TAIL(X)(((char*)(X))+__builtin_offsetof(Parse, sLastToken)) (((char*)(X))+PARSE_RECURSE_SZ__builtin_offsetof(Parse, sLastToken)) /* Pointer to tail */
18155
18156/*
18157** Return true if currently inside an sqlite3_declare_vtab() call.
18158*/
18159#ifdef SQLITE_OMIT_VIRTUALTABLE
18160 #define IN_DECLARE_VTAB(pParse->eParseMode==1) 0
18161#else
18162 #define IN_DECLARE_VTAB(pParse->eParseMode==1) (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB1)
18163#endif
18164
18165#if defined(SQLITE_OMIT_ALTERTABLE)
18166 #define IN_RENAME_OBJECT(pParse->eParseMode>=2) 0
18167#else
18168 #define IN_RENAME_OBJECT(pParse->eParseMode>=2) (pParse->eParseMode>=PARSE_MODE_RENAME_COLUMN2)
18169#endif
18170
18171#if defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_OMIT_ALTERTABLE)
18172 #define IN_SPECIAL_PARSE(pParse->eParseMode!=0) 0
18173#else
18174 #define IN_SPECIAL_PARSE(pParse->eParseMode!=0) (pParse->eParseMode!=PARSE_MODE_NORMAL0)
18175#endif
18176
18177/*
18178** An instance of the following structure can be declared on a stack and used
18179** to save the Parse.zAuthContext value so that it can be restored later.
18180*/
18181struct AuthContext {
18182 const char *zAuthContext; /* Put saved Parse.zAuthContext here */
18183 Parse *pParse; /* The Parse structure */
18184};
18185
18186/*
18187** Bitfield flags for P5 value in various opcodes.
18188**
18189** Value constraints (enforced via assert()):
18190** OPFLAG_LENGTHARG == SQLITE_FUNC_LENGTH
18191** OPFLAG_TYPEOFARG == SQLITE_FUNC_TYPEOF
18192** OPFLAG_BULKCSR == BTREE_BULKLOAD
18193** OPFLAG_SEEKEQ == BTREE_SEEK_EQ
18194** OPFLAG_FORDELETE == BTREE_FORDELETE
18195** OPFLAG_SAVEPOSITION == BTREE_SAVEPOSITION
18196** OPFLAG_AUXDELETE == BTREE_AUXDELETE
18197*/
18198#define OPFLAG_NCHANGE0x01 0x01 /* OP_Insert: Set to update db->nChange */
18199 /* Also used in P2 (not P5) of OP_Delete */
18200#define OPFLAG_NOCHNG0x01 0x01 /* OP_VColumn nochange for UPDATE */
18201#define OPFLAG_EPHEM0x01 0x01 /* OP_Column: Ephemeral output is ok */
18202#define OPFLAG_LASTROWID0x20 0x20 /* Set to update db->lastRowid */
18203#define OPFLAG_ISUPDATE0x04 0x04 /* This OP_Insert is an sql UPDATE */
18204#define OPFLAG_APPEND0x08 0x08 /* This is likely to be an append */
18205#define OPFLAG_USESEEKRESULT0x10 0x10 /* Try to avoid a seek in BtreeInsert() */
18206#define OPFLAG_ISNOOP0x40 0x40 /* OP_Delete does pre-update-hook only */
18207#define OPFLAG_LENGTHARG0x40 0x40 /* OP_Column only used for length() */
18208#define OPFLAG_TYPEOFARG0x80 0x80 /* OP_Column only used for typeof() */
18209#define OPFLAG_BULKCSR0x01 0x01 /* OP_Open** used to open bulk cursor */
18210#define OPFLAG_SEEKEQ0x02 0x02 /* OP_Open** cursor uses EQ seek only */
18211#define OPFLAG_FORDELETE0x08 0x08 /* OP_Open should use BTREE_FORDELETE */
18212#define OPFLAG_P2ISREG0x10 0x10 /* P2 to OP_Open** is a register number */
18213#define OPFLAG_PERMUTE0x01 0x01 /* OP_Compare: use the permutation */
18214#define OPFLAG_SAVEPOSITION0x02 0x02 /* OP_Delete/Insert: save cursor pos */
18215#define OPFLAG_AUXDELETE0x04 0x04 /* OP_Delete: index in a DELETE op */
18216#define OPFLAG_NOCHNG_MAGIC0x6d 0x6d /* OP_MakeRecord: serialtype 10 is ok */
18217
18218/*
18219 * Each trigger present in the database schema is stored as an instance of
18220 * struct Trigger.
18221 *
18222 * Pointers to instances of struct Trigger are stored in two ways.
18223 * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
18224 * database). This allows Trigger structures to be retrieved by name.
18225 * 2. All triggers associated with a single table form a linked list, using the
18226 * pNext member of struct Trigger. A pointer to the first element of the
18227 * linked list is stored as the "pTrigger" member of the associated
18228 * struct Table.
18229 *
18230 * The "step_list" member points to the first element of a linked list
18231 * containing the SQL statements specified as the trigger program.
18232 */
18233struct Trigger {
18234 char *zName; /* The name of the trigger */
18235 char *table; /* The table or view to which the trigger applies */
18236 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
18237 u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
18238 Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
18239 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
18240 the <column-list> is stored here */
18241 Schema *pSchema; /* Schema containing the trigger */
18242 Schema *pTabSchema; /* Schema containing the table */
18243 TriggerStep *step_list; /* Link list of trigger program steps */
18244 Trigger *pNext; /* Next trigger associated with the table */
18245};
18246
18247/*
18248** A trigger is either a BEFORE or an AFTER trigger. The following constants
18249** determine which.
18250**
18251** If there are multiple triggers, you might of some BEFORE and some AFTER.
18252** In that cases, the constants below can be ORed together.
18253*/
18254#define TRIGGER_BEFORE1 1
18255#define TRIGGER_AFTER2 2
18256
18257/*
18258 * An instance of struct TriggerStep is used to store a single SQL statement
18259 * that is a part of a trigger-program.
18260 *
18261 * Instances of struct TriggerStep are stored in a singly linked list (linked
18262 * using the "pNext" member) referenced by the "step_list" member of the
18263 * associated struct Trigger instance. The first element of the linked list is
18264 * the first step of the trigger-program.
18265 *
18266 * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
18267 * "SELECT" statement. The meanings of the other members is determined by the
18268 * value of "op" as follows:
18269 *
18270 * (op == TK_INSERT)
18271 * orconf -> stores the ON CONFLICT algorithm
18272 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
18273 * this stores a pointer to the SELECT statement. Otherwise NULL.
18274 * zTarget -> Dequoted name of the table to insert into.
18275 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
18276 * this stores values to be inserted. Otherwise NULL.
18277 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
18278 * statement, then this stores the column-names to be
18279 * inserted into.
18280 *
18281 * (op == TK_DELETE)
18282 * zTarget -> Dequoted name of the table to delete from.
18283 * pWhere -> The WHERE clause of the DELETE statement if one is specified.
18284 * Otherwise NULL.
18285 *
18286 * (op == TK_UPDATE)
18287 * zTarget -> Dequoted name of the table to update.
18288 * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
18289 * Otherwise NULL.
18290 * pExprList -> A list of the columns to update and the expressions to update
18291 * them to. See sqlite3Update() documentation of "pChanges"
18292 * argument.
18293 *
18294 */
18295struct TriggerStep {
18296 u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
18297 u8 orconf; /* OE_Rollback etc. */
18298 Trigger *pTrig; /* The trigger that this step is a part of */
18299 Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */
18300 char *zTarget; /* Target table for DELETE, UPDATE, INSERT */
18301 Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
18302 ExprList *pExprList; /* SET clause for UPDATE */
18303 IdList *pIdList; /* Column names for INSERT */
18304 Upsert *pUpsert; /* Upsert clauses on an INSERT */
18305 char *zSpan; /* Original SQL text of this command */
18306 TriggerStep *pNext; /* Next in the link-list */
18307 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
18308};
18309
18310/*
18311** The following structure contains information used by the sqliteFix...
18312** routines as they walk the parse tree to make database references
18313** explicit.
18314*/
18315typedef struct DbFixer DbFixer;
18316struct DbFixer {
18317 Parse *pParse; /* The parsing context. Error messages written here */
18318 Schema *pSchema; /* Fix items to this schema */
18319 int bVarOnly; /* Check for variable references only */
18320 const char *zDb; /* Make sure all objects are contained in this database */
18321 const char *zType; /* Type of the container - used for error messages */
18322 const Token *pName; /* Name of the container - used for error messages */
18323};
18324
18325/*
18326** An objected used to accumulate the text of a string where we
18327** do not necessarily know how big the string will be in the end.
18328*/
18329struct sqlite3_str {
18330 sqlite3 *db; /* Optional database for lookaside. Can be NULL */
18331 char *zText; /* The string collected so far */
18332 u32 nAlloc; /* Amount of space allocated in zText */
18333 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
18334 u32 nChar; /* Length of the string so far */
18335 u8 accError; /* SQLITE_NOMEM or SQLITE_TOOBIG */
18336 u8 printfFlags; /* SQLITE_PRINTF flags below */
18337};
18338#define SQLITE_PRINTF_INTERNAL0x01 0x01 /* Internal-use-only converters allowed */
18339#define SQLITE_PRINTF_SQLFUNC0x02 0x02 /* SQL function arguments to VXPrintf */
18340#define SQLITE_PRINTF_MALLOCED0x04 0x04 /* True if xText is allocated space */
18341
18342#define isMalloced(X)(((X)->printfFlags & 0x04)!=0) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED0x04)!=0)
18343
18344
18345/*
18346** A pointer to this structure is used to communicate information
18347** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
18348*/
18349typedef struct {
18350 sqlite3 *db; /* The database being initialized */
18351 char **pzErrMsg; /* Error message stored here */
18352 int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
18353 int rc; /* Result code stored here */
18354 u32 mInitFlags; /* Flags controlling error messages */
18355 u32 nInitRow; /* Number of rows processed */
18356} InitData;
18357
18358/*
18359** Allowed values for mInitFlags
18360*/
18361#define INITFLAG_AlterTable0x0001 0x0001 /* This is a reparse after ALTER TABLE */
18362
18363/*
18364** Structure containing global configuration data for the SQLite library.
18365**
18366** This structure also contains some state information.
18367*/
18368struct Sqlite3Config {
18369 int bMemstat; /* True to enable memory status */
18370 int bCoreMutex; /* True to enable core mutexing */
18371 int bFullMutex; /* True to enable full mutexing */
18372 int bOpenUri; /* True to interpret filenames as URIs */
18373 int bUseCis; /* Use covering indices for full-scans */
18374 int bSmallMalloc; /* Avoid large memory allocations if true */
18375 int mxStrlen; /* Maximum string length */
18376 int neverCorrupt; /* Database is always well-formed */
18377 int szLookaside; /* Default lookaside buffer size */
18378 int nLookaside; /* Default lookaside buffer count */
18379 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
18380 sqlite3_mem_methods m; /* Low-level memory allocation interface */
18381 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
18382 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
18383 void *pHeap; /* Heap storage space */
18384 int nHeap; /* Size of pHeap[] */
18385 int mnReq, mxReq; /* Min and max heap requests sizes */
18386 sqlite3_int64 szMmap; /* mmap() space per open file */
18387 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
18388 void *pPage; /* Page cache memory */
18389 int szPage; /* Size of each page in pPage[] */
18390 int nPage; /* Number of pages in pPage[] */
18391 int mxParserStack; /* maximum depth of the parser stack */
18392 int sharedCacheEnabled; /* true if shared-cache mode enabled */
18393 u32 szPma; /* Maximum Sorter PMA size */
18394 /* The above might be initialized to non-zero. The following need to always
18395 ** initially be zero, however. */
18396 int isInit; /* True after initialization has finished */
18397 int inProgress; /* True while initialization in progress */
18398 int isMutexInit; /* True after mutexes are initialized */
18399 int isMallocInit; /* True after malloc is initialized */
18400 int isPCacheInit; /* True after malloc is initialized */
18401 int nRefInitMutex; /* Number of users of pInitMutex */
18402 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
18403 void (*xLog)(void*,int,const char*); /* Function for logging */
18404 void *pLogArg; /* First argument to xLog() */
18405#ifdef SQLITE_ENABLE_SQLLOG
18406 void(*xSqllog)(void*,sqlite3*,const char*, int);
18407 void *pSqllogArg;
18408#endif
18409#ifdef SQLITE_VDBE_COVERAGE
18410 /* The following callback (if not NULL) is invoked on every VDBE branch
18411 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
18412 */
18413 void (*xVdbeBranch)(void*,unsigned iSrcLine,u8 eThis,u8 eMx); /* Callback */
18414 void *pVdbeBranchArg; /* 1st argument */
18415#endif
18416#ifdef SQLITE_ENABLE_DESERIALIZE
18417 sqlite3_int64 mxMemdbSize; /* Default max memdb size */
18418#endif
18419#ifndef SQLITE_UNTESTABLE
18420 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
18421#endif
18422 int bLocaltimeFault; /* True to fail localtime() calls */
18423 int bInternalFunctions; /* Internal SQL functions are visible */
18424 int iOnceResetThreshold; /* When to reset OP_Once counters */
18425 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
18426};
18427
18428/*
18429** This macro is used inside of assert() statements to indicate that
18430** the assert is only valid on a well-formed database. Instead of:
18431**
18432** assert( X );
18433**
18434** One writes:
18435**
18436** assert( X || CORRUPT_DB );
18437**
18438** CORRUPT_DB is true during normal operation. CORRUPT_DB does not indicate
18439** that the database is definitely corrupt, only that it might be corrupt.
18440** For most test cases, CORRUPT_DB is set to false using a special
18441** sqlite3_test_control(). This enables assert() statements to prove
18442** things that are always true for well-formed databases.
18443*/
18444#define CORRUPT_DB(sqlite3Config.neverCorrupt==0) (sqlite3Config.neverCorrupt==0)
18445
18446/*
18447** Context pointer passed down through the tree-walk.
18448*/
18449struct Walker {
18450 Parse *pParse; /* Parser context. */
18451 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
18452 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
18453 void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
18454 int walkerDepth; /* Number of subqueries */
18455 u8 eCode; /* A small processing code */
18456 union { /* Extra data for callback */
18457 NameContext *pNC; /* Naming context */
18458 int n; /* A counter */
18459 int iCur; /* A cursor number */
18460 SrcList *pSrcList; /* FROM clause */
18461 struct SrcCount *pSrcCount; /* Counting column references */
18462 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
18463 int *aiCol; /* array of column indexes */
18464 struct IdxCover *pIdxCover; /* Check for index coverage */
18465 struct IdxExprTrans *pIdxTrans; /* Convert idxed expr to column */
18466 ExprList *pGroupBy; /* GROUP BY clause */
18467 Select *pSelect; /* HAVING to WHERE clause ctx */
18468 struct WindowRewrite *pRewrite; /* Window rewrite context */
18469 struct WhereConst *pConst; /* WHERE clause constants */
18470 struct RenameCtx *pRename; /* RENAME COLUMN context */
18471 } u;
18472};
18473
18474/* Forward declarations */
18475SQLITE_PRIVATEstatic int sqlite3WalkExpr(Walker*, Expr*);
18476SQLITE_PRIVATEstatic int sqlite3WalkExprList(Walker*, ExprList*);
18477SQLITE_PRIVATEstatic int sqlite3WalkSelect(Walker*, Select*);
18478SQLITE_PRIVATEstatic int sqlite3WalkSelectExpr(Walker*, Select*);
18479SQLITE_PRIVATEstatic int sqlite3WalkSelectFrom(Walker*, Select*);
18480SQLITE_PRIVATEstatic int sqlite3ExprWalkNoop(Walker*, Expr*);
18481SQLITE_PRIVATEstatic int sqlite3SelectWalkNoop(Walker*, Select*);
18482SQLITE_PRIVATEstatic int sqlite3SelectWalkFail(Walker*, Select*);
18483#ifdef SQLITE_DEBUG
18484SQLITE_PRIVATEstatic void sqlite3SelectWalkAssert2(Walker*, Select*);
18485#endif
18486
18487/*
18488** Return code from the parse-tree walking primitives and their
18489** callbacks.
18490*/
18491#define WRC_Continue0 0 /* Continue down into children */
18492#define WRC_Prune1 1 /* Omit children but continue walking siblings */
18493#define WRC_Abort2 2 /* Abandon the tree walk */
18494
18495/*
18496** An instance of this structure represents a set of one or more CTEs
18497** (common table expressions) created by a single WITH clause.
18498*/
18499struct With {
18500 int nCte; /* Number of CTEs in the WITH clause */
18501 With *pOuter; /* Containing WITH clause, or NULL */
18502 struct Cte { /* For each CTE in the WITH clause.... */
18503 char *zName; /* Name of this CTE */
18504 ExprList *pCols; /* List of explicit column names, or NULL */
18505 Select *pSelect; /* The definition of this CTE */
18506 const char *zCteErr; /* Error message for circular references */
18507 } a[1];
18508};
18509
18510#ifdef SQLITE_DEBUG
18511/*
18512** An instance of the TreeView object is used for printing the content of
18513** data structures on sqlite3DebugPrintf() using a tree-like view.
18514*/
18515struct TreeView {
18516 int iLevel; /* Which level of the tree we are on */
18517 u8 bLine[100]; /* Draw vertical in column i if bLine[i] is true */
18518};
18519#endif /* SQLITE_DEBUG */
18520
18521/*
18522** This object is used in various ways, all related to window functions
18523**
18524** (1) A single instance of this structure is attached to the
18525** the Expr.pWin field for each window function in an expression tree.
18526** This object holds the information contained in the OVER clause,
18527** plus additional fields used during code generation.
18528**
18529** (2) All window functions in a single SELECT form a linked-list
18530** attached to Select.pWin. The Window.pFunc and Window.pExpr
18531** fields point back to the expression that is the window function.
18532**
18533** (3) The terms of the WINDOW clause of a SELECT are instances of this
18534** object on a linked list attached to Select.pWinDefn.
18535**
18536** The uses (1) and (2) are really the same Window object that just happens
18537** to be accessible in two different ways. Use case (3) are separate objects.
18538*/
18539struct Window {
18540 char *zName; /* Name of window (may be NULL) */
18541 char *zBase; /* Name of base window for chaining (may be NULL) */
18542 ExprList *pPartition; /* PARTITION BY clause */
18543 ExprList *pOrderBy; /* ORDER BY clause */
18544 u8 eFrmType; /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
18545 u8 eStart; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
18546 u8 eEnd; /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
18547 u8 bImplicitFrame; /* True if frame was implicitly specified */
18548 u8 eExclude; /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
18549 Expr *pStart; /* Expression for "<expr> PRECEDING" */
18550 Expr *pEnd; /* Expression for "<expr> FOLLOWING" */
18551 Window *pNextWin; /* Next window function belonging to this SELECT */
18552 Expr *pFilter; /* The FILTER expression */
18553 FuncDef *pFunc; /* The function */
18554 int iEphCsr; /* Partition buffer or Peer buffer */
18555 int regAccum;
18556 int regResult;
18557 int csrApp; /* Function cursor (used by min/max) */
18558 int regApp; /* Function register (also used by min/max) */
18559 int regPart; /* Array of registers for PARTITION BY values */
18560 Expr *pOwner; /* Expression object this window is attached to */
18561 int nBufferCol; /* Number of columns in buffer table */
18562 int iArgCol; /* Offset of first argument for this function */
18563 int regOne; /* Register containing constant value 1 */
18564 int regStartRowid;
18565 int regEndRowid;
18566};
18567
18568#ifndef SQLITE_OMIT_WINDOWFUNC
18569SQLITE_PRIVATEstatic void sqlite3WindowDelete(sqlite3*, Window*);
18570SQLITE_PRIVATEstatic void sqlite3WindowListDelete(sqlite3 *db, Window *p);
18571SQLITE_PRIVATEstatic Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
18572SQLITE_PRIVATEstatic void sqlite3WindowAttach(Parse*, Expr*, Window*);
18573SQLITE_PRIVATEstatic int sqlite3WindowCompare(Parse*, Window*, Window*);
18574SQLITE_PRIVATEstatic void sqlite3WindowCodeInit(Parse*, Window*);
18575SQLITE_PRIVATEstatic void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
18576SQLITE_PRIVATEstatic int sqlite3WindowRewrite(Parse*, Select*);
18577SQLITE_PRIVATEstatic int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
18578SQLITE_PRIVATEstatic void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
18579SQLITE_PRIVATEstatic Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
18580SQLITE_PRIVATEstatic Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
18581SQLITE_PRIVATEstatic void sqlite3WindowFunctions(void);
18582SQLITE_PRIVATEstatic void sqlite3WindowChain(Parse*, Window*, Window*);
18583SQLITE_PRIVATEstatic Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
18584#else
18585# define sqlite3WindowDelete(a,b)
18586# define sqlite3WindowFunctions()
18587# define sqlite3WindowAttach(a,b,c)
18588#endif
18589
18590/*
18591** Assuming zIn points to the first byte of a UTF-8 character,
18592** advance zIn to point to the first byte of the next UTF-8 character.
18593*/
18594#define SQLITE_SKIP_UTF8(zIn){ if( (*(zIn++))>=0xc0 ){ while( (*zIn & 0xc0)==0x80 )
{ zIn++; } } }
{ \
18595 if( (*(zIn++))>=0xc0 ){ \
18596 while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
18597 } \
18598}
18599
18600/*
18601** The SQLITE_*_BKPT macros are substitutes for the error codes with
18602** the same name but without the _BKPT suffix. These macros invoke
18603** routines that report the line-number on which the error originated
18604** using sqlite3_log(). The routines also provide a convenient place
18605** to set a debugger breakpoint.
18606*/
18607SQLITE_PRIVATEstatic int sqlite3ReportError(int iErr, int lineno, const char *zType);
18608SQLITE_PRIVATEstatic int sqlite3CorruptError(int);
18609SQLITE_PRIVATEstatic int sqlite3MisuseError(int);
18610SQLITE_PRIVATEstatic int sqlite3CantopenError(int);
18611#define SQLITE_CORRUPT_BKPTsqlite3CorruptError(18611) sqlite3CorruptError(__LINE__18611)
18612#define SQLITE_MISUSE_BKPTsqlite3MisuseError(18612) sqlite3MisuseError(__LINE__18612)
18613#define SQLITE_CANTOPEN_BKPTsqlite3CantopenError(18613) sqlite3CantopenError(__LINE__18613)
18614#ifdef SQLITE_DEBUG
18615SQLITE_PRIVATEstatic int sqlite3NomemError(int);
18616SQLITE_PRIVATEstatic int sqlite3IoerrnomemError(int);
18617SQLITE_PRIVATEstatic int sqlite3CorruptPgnoError(int,Pgno);
18618# define SQLITE_NOMEM_BKPT7 sqlite3NomemError(__LINE__18618)
18619# define SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)) sqlite3IoerrnomemError(__LINE__18619)
18620# define SQLITE_CORRUPT_PGNO(P)sqlite3CorruptError(18620) sqlite3CorruptPgnoError(__LINE__18620,(P))
18621#else
18622# define SQLITE_NOMEM_BKPT7 SQLITE_NOMEM7
18623# define SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)) SQLITE_IOERR_NOMEM(10 | (12<<8))
18624# define SQLITE_CORRUPT_PGNO(P)sqlite3CorruptError(18624) sqlite3CorruptError(__LINE__18624)
18625#endif
18626
18627/*
18628** FTS3 and FTS4 both require virtual table support
18629*/
18630#if defined(SQLITE_OMIT_VIRTUALTABLE)
18631# undef SQLITE_ENABLE_FTS3
18632# undef SQLITE_ENABLE_FTS4
18633#endif
18634
18635/*
18636** FTS4 is really an extension for FTS3. It is enabled using the
18637** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
18638** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
18639*/
18640#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
18641# define SQLITE_ENABLE_FTS3 1
18642#endif
18643
18644/*
18645** The ctype.h header is needed for non-ASCII systems. It is also
18646** needed by FTS3 when FTS3 is included in the amalgamation.
18647*/
18648#if !defined(SQLITE_ASCII1) || \
18649 (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION1))
18650# include <ctype.h>
18651#endif
18652
18653/*
18654** The following macros mimic the standard library functions toupper(),
18655** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
18656** sqlite versions only work for ASCII characters, regardless of locale.
18657*/
18658#ifdef SQLITE_ASCII1
18659# define sqlite3Toupper(x)((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20)) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
18660# define sqlite3Isspace(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x01) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
18661# define sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
18662# define sqlite3Isalpha(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x02) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
18663# define sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
18664# define sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
18665# define sqlite3Tolower(x)(sqlite3UpperToLower[(unsigned char)(x)]) (sqlite3UpperToLower[(unsigned char)(x)])
18666# define sqlite3Isquote(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x80) (sqlite3CtypeMap[(unsigned char)(x)]&0x80)
18667#else
18668# define sqlite3Toupper(x)((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20)) toupper((unsigned char)(x))
18669# define sqlite3Isspace(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x01) isspace((unsigned char)(x))
18670# define sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06) isalnum((unsigned char)(x))
18671# define sqlite3Isalpha(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x02) isalpha((unsigned char)(x))
18672# define sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04) isdigit((unsigned char)(x))
18673# define sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08) isxdigit((unsigned char)(x))
18674# define sqlite3Tolower(x)(sqlite3UpperToLower[(unsigned char)(x)]) tolower((unsigned char)(x))
18675# define sqlite3Isquote(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x80) ((x)=='"'||(x)=='\''||(x)=='['||(x)=='`')
18676#endif
18677SQLITE_PRIVATEstatic int sqlite3IsIdChar(u8);
18678
18679/*
18680** Internal function prototypes
18681*/
18682SQLITE_PRIVATEstatic int sqlite3StrICmp(const char*,const char*);
18683SQLITE_PRIVATEstatic int sqlite3Strlen30(const char*);
18684#define sqlite3Strlen30NN(C)(strlen(C)&0x3fffffff) (strlen(C)&0x3fffffff)
18685SQLITE_PRIVATEstatic char *sqlite3ColumnType(Column*,char*);
18686#define sqlite3StrNICmpsqlite3_strnicmp sqlite3_strnicmp
18687
18688SQLITE_PRIVATEstatic int sqlite3MallocInit(void);
18689SQLITE_PRIVATEstatic void sqlite3MallocEnd(void);
18690SQLITE_PRIVATEstatic void *sqlite3Malloc(u64);
18691SQLITE_PRIVATEstatic void *sqlite3MallocZero(u64);
18692SQLITE_PRIVATEstatic void *sqlite3DbMallocZero(sqlite3*, u64);
18693SQLITE_PRIVATEstatic void *sqlite3DbMallocRaw(sqlite3*, u64);
18694SQLITE_PRIVATEstatic void *sqlite3DbMallocRawNN(sqlite3*, u64);
18695SQLITE_PRIVATEstatic char *sqlite3DbStrDup(sqlite3*,const char*);
18696SQLITE_PRIVATEstatic char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
18697SQLITE_PRIVATEstatic char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
18698SQLITE_PRIVATEstatic void *sqlite3Realloc(void*, u64);
18699SQLITE_PRIVATEstatic void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
18700SQLITE_PRIVATEstatic void *sqlite3DbRealloc(sqlite3 *, void *, u64);
18701SQLITE_PRIVATEstatic void sqlite3DbFree(sqlite3*, void*);
18702SQLITE_PRIVATEstatic void sqlite3DbFreeNN(sqlite3*, void*);
18703SQLITE_PRIVATEstatic int sqlite3MallocSize(void*);
18704SQLITE_PRIVATEstatic int sqlite3DbMallocSize(sqlite3*, void*);
18705SQLITE_PRIVATEstatic void *sqlite3PageMalloc(int);
18706SQLITE_PRIVATEstatic void sqlite3PageFree(void*);
18707SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void);
18708#ifndef SQLITE_UNTESTABLE
18709SQLITE_PRIVATEstatic void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
18710#endif
18711SQLITE_PRIVATEstatic int sqlite3HeapNearlyFull(void);
18712
18713/*
18714** On systems with ample stack space and that support alloca(), make
18715** use of alloca() to obtain space for large automatic objects. By default,
18716** obtain space from malloc().
18717**
18718** The alloca() routine never returns NULL. This will cause code paths
18719** that deal with sqlite3StackAlloc() failures to be unreachable.
18720*/
18721#ifdef SQLITE_USE_ALLOCA
18722# define sqlite3StackAllocRaw(D,N)sqlite3DbMallocRaw(D,N) alloca(N)__builtin_alloca (N)
18723# define sqlite3StackAllocZero(D,N)sqlite3DbMallocZero(D,N) memset(alloca(N)__builtin_alloca (N), 0, N)
18724# define sqlite3StackFree(D,P)sqlite3DbFree(D,P)
18725#else
18726# define sqlite3StackAllocRaw(D,N)sqlite3DbMallocRaw(D,N) sqlite3DbMallocRaw(D,N)
18727# define sqlite3StackAllocZero(D,N)sqlite3DbMallocZero(D,N) sqlite3DbMallocZero(D,N)
18728# define sqlite3StackFree(D,P)sqlite3DbFree(D,P) sqlite3DbFree(D,P)
18729#endif
18730
18731/* Do not allow both MEMSYS5 and MEMSYS3 to be defined together. If they
18732** are, disable MEMSYS3
18733*/
18734#ifdef SQLITE_ENABLE_MEMSYS5
18735SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
18736#undef SQLITE_ENABLE_MEMSYS3
18737#endif
18738#ifdef SQLITE_ENABLE_MEMSYS3
18739SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
18740#endif
18741
18742
18743#ifndef SQLITE_MUTEX_OMIT
18744SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
18745SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void);
18746SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MutexAlloc(int);
18747SQLITE_PRIVATEstatic int sqlite3MutexInit(void);
18748SQLITE_PRIVATEstatic int sqlite3MutexEnd(void);
18749#endif
18750#if !defined(SQLITE_MUTEX_OMIT) && !defined(SQLITE_MUTEX_NOOP)
18751SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void);
18752#else
18753# define sqlite3MemoryBarrier()
18754#endif
18755
18756SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StatusValue(int);
18757SQLITE_PRIVATEstatic void sqlite3StatusUp(int, int);
18758SQLITE_PRIVATEstatic void sqlite3StatusDown(int, int);
18759SQLITE_PRIVATEstatic void sqlite3StatusHighwater(int, int);
18760SQLITE_PRIVATEstatic int sqlite3LookasideUsed(sqlite3*,int*);
18761
18762/* Access to mutexes used by sqlite3_status() */
18763SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3Pcache1Mutex(void);
18764SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MallocMutex(void);
18765
18766#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
18767SQLITE_PRIVATEstatic void sqlite3MutexWarnOnContention(sqlite3_mutex*);
18768#else
18769# define sqlite3MutexWarnOnContention(x)
18770#endif
18771
18772#ifndef SQLITE_OMIT_FLOATING_POINT
18773# define EXP754(((u64)0x7ff)<<52) (((u64)0x7ff)<<52)
18774# define MAN754((((u64)1)<<52)-1) ((((u64)1)<<52)-1)
18775# define IsNaN(X)(((X)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((X)&((((u64)1)<<52)-1))!=0)
(((X)&EXP754(((u64)0x7ff)<<52))==EXP754(((u64)0x7ff)<<52) && ((X)&MAN754((((u64)1)<<52)-1))!=0)
18776SQLITE_PRIVATEstatic int sqlite3IsNaN(double);
18777#else
18778# define IsNaN(X)(((X)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((X)&((((u64)1)<<52)-1))!=0)
0
18779# define sqlite3IsNaN(X) 0
18780#endif
18781
18782/*
18783** An instance of the following structure holds information about SQL
18784** functions arguments that are the parameters to the printf() function.
18785*/
18786struct PrintfArguments {
18787 int nArg; /* Total number of arguments */
18788 int nUsed; /* Number of arguments used so far */
18789 sqlite3_value **apArg; /* The argument values */
18790};
18791
18792SQLITE_PRIVATEstatic char *sqlite3MPrintf(sqlite3*,const char*, ...);
18793SQLITE_PRIVATEstatic char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
18794#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
18795SQLITE_PRIVATEstatic void sqlite3DebugPrintf(const char*, ...);
18796#endif
18797#if defined(SQLITE_TEST)
18798SQLITE_PRIVATEstatic void *sqlite3TestTextToPtr(const char*);
18799#endif
18800
18801#if defined(SQLITE_DEBUG)
18802SQLITE_PRIVATEstatic void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
18803SQLITE_PRIVATEstatic void sqlite3TreeViewBareExprList(TreeView*, const ExprList*, const char*);
18804SQLITE_PRIVATEstatic void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
18805SQLITE_PRIVATEstatic void sqlite3TreeViewSrcList(TreeView*, const SrcList*);
18806SQLITE_PRIVATEstatic void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
18807SQLITE_PRIVATEstatic void sqlite3TreeViewWith(TreeView*, const With*, u8);
18808#ifndef SQLITE_OMIT_WINDOWFUNC
18809SQLITE_PRIVATEstatic void sqlite3TreeViewWindow(TreeView*, const Window*, u8);
18810SQLITE_PRIVATEstatic void sqlite3TreeViewWinFunc(TreeView*, const Window*, u8);
18811#endif
18812#endif
18813
18814
18815SQLITE_PRIVATEstatic void sqlite3SetString(char **, sqlite3*, const char*);
18816SQLITE_PRIVATEstatic void sqlite3ErrorMsg(Parse*, const char*, ...);
18817SQLITE_PRIVATEstatic int sqlite3ErrorToParser(sqlite3*,int);
18818SQLITE_PRIVATEstatic void sqlite3Dequote(char*);
18819SQLITE_PRIVATEstatic void sqlite3DequoteExpr(Expr*);
18820SQLITE_PRIVATEstatic void sqlite3TokenInit(Token*,char*);
18821SQLITE_PRIVATEstatic int sqlite3KeywordCode(const unsigned char*, int);
18822SQLITE_PRIVATEstatic int sqlite3RunParser(Parse*, const char*, char **);
18823SQLITE_PRIVATEstatic void sqlite3FinishCoding(Parse*);
18824SQLITE_PRIVATEstatic int sqlite3GetTempReg(Parse*);
18825SQLITE_PRIVATEstatic void sqlite3ReleaseTempReg(Parse*,int);
18826SQLITE_PRIVATEstatic int sqlite3GetTempRange(Parse*,int);
18827SQLITE_PRIVATEstatic void sqlite3ReleaseTempRange(Parse*,int,int);
18828SQLITE_PRIVATEstatic void sqlite3ClearTempRegCache(Parse*);
18829#ifdef SQLITE_DEBUG
18830SQLITE_PRIVATEstatic int sqlite3NoTempsInRange(Parse*,int,int);
18831#endif
18832SQLITE_PRIVATEstatic Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
18833SQLITE_PRIVATEstatic Expr *sqlite3Expr(sqlite3*,int,const char*);
18834SQLITE_PRIVATEstatic void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
18835SQLITE_PRIVATEstatic Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
18836SQLITE_PRIVATEstatic void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
18837SQLITE_PRIVATEstatic Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
18838SQLITE_PRIVATEstatic Expr *sqlite3ExprSimplifiedAndOr(Expr*);
18839SQLITE_PRIVATEstatic Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
18840SQLITE_PRIVATEstatic void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
18841SQLITE_PRIVATEstatic void sqlite3ExprDelete(sqlite3*, Expr*);
18842SQLITE_PRIVATEstatic void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
18843SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
18844SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
18845SQLITE_PRIVATEstatic void sqlite3ExprListSetSortOrder(ExprList*,int);
18846SQLITE_PRIVATEstatic void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
18847SQLITE_PRIVATEstatic void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
18848SQLITE_PRIVATEstatic void sqlite3ExprListDelete(sqlite3*, ExprList*);
18849SQLITE_PRIVATEstatic u32 sqlite3ExprListFlags(const ExprList*);
18850SQLITE_PRIVATEstatic int sqlite3IndexHasDuplicateRootPage(Index*);
18851SQLITE_PRIVATEstatic int sqlite3Init(sqlite3*, char**);
18852SQLITE_PRIVATEstatic int sqlite3InitCallback(void*, int, char**, char**);
18853SQLITE_PRIVATEstatic int sqlite3InitOne(sqlite3*, int, char**, u32);
18854SQLITE_PRIVATEstatic void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
18855#ifndef SQLITE_OMIT_VIRTUALTABLE
18856SQLITE_PRIVATEstatic Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
18857#endif
18858SQLITE_PRIVATEstatic void sqlite3ResetAllSchemasOfConnection(sqlite3*);
18859SQLITE_PRIVATEstatic void sqlite3ResetOneSchema(sqlite3*,int);
18860SQLITE_PRIVATEstatic void sqlite3CollapseDatabaseArray(sqlite3*);
18861SQLITE_PRIVATEstatic void sqlite3CommitInternalChanges(sqlite3*);
18862SQLITE_PRIVATEstatic void sqlite3DeleteColumnNames(sqlite3*,Table*);
18863SQLITE_PRIVATEstatic int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
18864SQLITE_PRIVATEstatic void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*);
18865SQLITE_PRIVATEstatic Table *sqlite3ResultSetOfSelect(Parse*,Select*);
18866SQLITE_PRIVATEstatic void sqlite3OpenMasterTable(Parse *, int);
18867SQLITE_PRIVATEstatic Index *sqlite3PrimaryKeyIndex(Table*);
18868SQLITE_PRIVATEstatic i16 sqlite3ColumnOfIndex(Index*, i16);
18869SQLITE_PRIVATEstatic void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
18870#if SQLITE_ENABLE_HIDDEN_COLUMNS
18871SQLITE_PRIVATEstatic void sqlite3ColumnPropertiesFromName(Table*, Column*);
18872#else
18873# define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
18874#endif
18875SQLITE_PRIVATEstatic void sqlite3AddColumn(Parse*,Token*,Token*);
18876SQLITE_PRIVATEstatic void sqlite3AddNotNull(Parse*, int);
18877SQLITE_PRIVATEstatic void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
18878SQLITE_PRIVATEstatic void sqlite3AddCheckConstraint(Parse*, Expr*);
18879SQLITE_PRIVATEstatic void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
18880SQLITE_PRIVATEstatic void sqlite3AddCollateType(Parse*, Token*);
18881SQLITE_PRIVATEstatic void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
18882SQLITE_PRIVATEstatic int sqlite3ParseUri(const char*,const char*,unsigned int*,
18883 sqlite3_vfs**,char**,char **);
18884#ifdef SQLITE_HAS_CODEC
18885SQLITE_PRIVATEstatic int sqlite3CodecQueryParameters(sqlite3*,const char*,const char*)0;
18886#else
18887# define sqlite3CodecQueryParameters(A,B,C)0 0
18888#endif
18889SQLITE_PRIVATEstatic Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
18890
18891#ifdef SQLITE_UNTESTABLE
18892# define sqlite3FaultSim(X) SQLITE_OK0
18893#else
18894SQLITE_PRIVATEstatic int sqlite3FaultSim(int);
18895#endif
18896
18897SQLITE_PRIVATEstatic Bitvec *sqlite3BitvecCreate(u32);
18898SQLITE_PRIVATEstatic int sqlite3BitvecTest(Bitvec*, u32);
18899SQLITE_PRIVATEstatic int sqlite3BitvecTestNotNull(Bitvec*, u32);
18900SQLITE_PRIVATEstatic int sqlite3BitvecSet(Bitvec*, u32);
18901SQLITE_PRIVATEstatic void sqlite3BitvecClear(Bitvec*, u32, void*);
18902SQLITE_PRIVATEstatic void sqlite3BitvecDestroy(Bitvec*);
18903SQLITE_PRIVATEstatic u32 sqlite3BitvecSize(Bitvec*);
18904#ifndef SQLITE_UNTESTABLE
18905SQLITE_PRIVATEstatic int sqlite3BitvecBuiltinTest(int,int*);
18906#endif
18907
18908SQLITE_PRIVATEstatic RowSet *sqlite3RowSetInit(sqlite3*);
18909SQLITE_PRIVATEstatic void sqlite3RowSetDelete(void*);
18910SQLITE_PRIVATEstatic void sqlite3RowSetClear(void*);
18911SQLITE_PRIVATEstatic void sqlite3RowSetInsert(RowSet*, i64);
18912SQLITE_PRIVATEstatic int sqlite3RowSetTest(RowSet*, int iBatch, i64);
18913SQLITE_PRIVATEstatic int sqlite3RowSetNext(RowSet*, i64*);
18914
18915SQLITE_PRIVATEstatic void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
18916
18917#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
18918SQLITE_PRIVATEstatic int sqlite3ViewGetColumnNames(Parse*,Table*);
18919#else
18920# define sqlite3ViewGetColumnNames(A,B) 0
18921#endif
18922
18923#if SQLITE_MAX_ATTACHED10>30
18924SQLITE_PRIVATEstatic int sqlite3DbMaskAllZero(yDbMask);
18925#endif
18926SQLITE_PRIVATEstatic void sqlite3DropTable(Parse*, SrcList*, int, int);
18927SQLITE_PRIVATEstatic void sqlite3CodeDropTable(Parse*, Table*, int, int);
18928SQLITE_PRIVATEstatic void sqlite3DeleteTable(sqlite3*, Table*);
18929SQLITE_PRIVATEstatic void sqlite3FreeIndex(sqlite3*, Index*);
18930#ifndef SQLITE_OMIT_AUTOINCREMENT
18931SQLITE_PRIVATEstatic void sqlite3AutoincrementBegin(Parse *pParse);
18932SQLITE_PRIVATEstatic void sqlite3AutoincrementEnd(Parse *pParse);
18933#else
18934# define sqlite3AutoincrementBegin(X)
18935# define sqlite3AutoincrementEnd(X)
18936#endif
18937SQLITE_PRIVATEstatic void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
18938SQLITE_PRIVATEstatic void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
18939SQLITE_PRIVATEstatic IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
18940SQLITE_PRIVATEstatic int sqlite3IdListIndex(IdList*,const char*);
18941SQLITE_PRIVATEstatic SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
18942SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
18943SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
18944 Token*, Select*, Expr*, IdList*);
18945SQLITE_PRIVATEstatic void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
18946SQLITE_PRIVATEstatic void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
18947SQLITE_PRIVATEstatic int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
18948SQLITE_PRIVATEstatic void sqlite3SrcListShiftJoinType(SrcList*);
18949SQLITE_PRIVATEstatic void sqlite3SrcListAssignCursors(Parse*, SrcList*);
18950SQLITE_PRIVATEstatic void sqlite3IdListDelete(sqlite3*, IdList*);
18951SQLITE_PRIVATEstatic void sqlite3SrcListDelete(sqlite3*, SrcList*);
18952SQLITE_PRIVATEstatic Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
18953SQLITE_PRIVATEstatic void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
18954 Expr*, int, int, u8);
18955SQLITE_PRIVATEstatic void sqlite3DropIndex(Parse*, SrcList*, int);
18956SQLITE_PRIVATEstatic int sqlite3Select(Parse*, Select*, SelectDest*);
18957SQLITE_PRIVATEstatic Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
18958 Expr*,ExprList*,u32,Expr*);
18959SQLITE_PRIVATEstatic void sqlite3SelectDelete(sqlite3*, Select*);
18960SQLITE_PRIVATEstatic Table *sqlite3SrcListLookup(Parse*, SrcList*);
18961SQLITE_PRIVATEstatic int sqlite3IsReadOnly(Parse*, Table*, int);
18962SQLITE_PRIVATEstatic void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
18963#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
18964SQLITE_PRIVATEstatic Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
18965#endif
18966SQLITE_PRIVATEstatic void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
18967SQLITE_PRIVATEstatic void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
18968 Upsert*);
18969SQLITE_PRIVATEstatic WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
18970SQLITE_PRIVATEstatic void sqlite3WhereEnd(WhereInfo*);
18971SQLITE_PRIVATEstatic LogEst sqlite3WhereOutputRowCount(WhereInfo*);
18972SQLITE_PRIVATEstatic int sqlite3WhereIsDistinct(WhereInfo*);
18973SQLITE_PRIVATEstatic int sqlite3WhereIsOrdered(WhereInfo*);
18974SQLITE_PRIVATEstatic int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
18975SQLITE_PRIVATEstatic int sqlite3WhereIsSorted(WhereInfo*);
18976SQLITE_PRIVATEstatic int sqlite3WhereContinueLabel(WhereInfo*);
18977SQLITE_PRIVATEstatic int sqlite3WhereBreakLabel(WhereInfo*);
18978SQLITE_PRIVATEstatic int sqlite3WhereOkOnePass(WhereInfo*, int*);
18979#define ONEPASS_OFF0 0 /* Use of ONEPASS not allowed */
18980#define ONEPASS_SINGLE1 1 /* ONEPASS valid for a single row update */
18981#define ONEPASS_MULTI2 2 /* ONEPASS is valid for multiple rows */
18982SQLITE_PRIVATEstatic void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
18983SQLITE_PRIVATEstatic int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
18984SQLITE_PRIVATEstatic void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
18985SQLITE_PRIVATEstatic void sqlite3ExprCodeMove(Parse*, int, int, int);
18986SQLITE_PRIVATEstatic void sqlite3ExprCode(Parse*, Expr*, int);
18987SQLITE_PRIVATEstatic void sqlite3ExprCodeCopy(Parse*, Expr*, int);
18988SQLITE_PRIVATEstatic void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
18989SQLITE_PRIVATEstatic int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
18990SQLITE_PRIVATEstatic int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
18991SQLITE_PRIVATEstatic int sqlite3ExprCodeTarget(Parse*, Expr*, int);
18992SQLITE_PRIVATEstatic void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
18993SQLITE_PRIVATEstatic int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
18994#define SQLITE_ECEL_DUP0x01 0x01 /* Deep, not shallow copies */
18995#define SQLITE_ECEL_FACTOR0x02 0x02 /* Factor out constant terms */
18996#define SQLITE_ECEL_REF0x04 0x04 /* Use ExprList.u.x.iOrderByCol */
18997#define SQLITE_ECEL_OMITREF0x08 0x08 /* Omit if ExprList.u.x.iOrderByCol */
18998SQLITE_PRIVATEstatic void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
18999SQLITE_PRIVATEstatic void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
19000SQLITE_PRIVATEstatic void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
19001SQLITE_PRIVATEstatic Table *sqlite3FindTable(sqlite3*,const char*, const char*);
19002#define LOCATE_VIEW0x01 0x01
19003#define LOCATE_NOERR0x02 0x02
19004SQLITE_PRIVATEstatic Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
19005SQLITE_PRIVATEstatic Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
19006SQLITE_PRIVATEstatic Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
19007SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
19008SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
19009SQLITE_PRIVATEstatic void sqlite3Vacuum(Parse*,Token*,Expr*);
19010SQLITE_PRIVATEstatic int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
19011SQLITE_PRIVATEstatic char *sqlite3NameFromToken(sqlite3*, Token*);
19012SQLITE_PRIVATEstatic int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
19013SQLITE_PRIVATEstatic int sqlite3ExprCompareSkip(Expr*, Expr*, int);
19014SQLITE_PRIVATEstatic int sqlite3ExprListCompare(ExprList*, ExprList*, int);
19015SQLITE_PRIVATEstatic int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
19016SQLITE_PRIVATEstatic int sqlite3ExprImpliesNonNullRow(Expr*,int);
19017SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
19018SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
19019SQLITE_PRIVATEstatic int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
19020SQLITE_PRIVATEstatic int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
19021SQLITE_PRIVATEstatic Vdbe *sqlite3GetVdbe(Parse*);
19022#ifndef SQLITE_UNTESTABLE
19023SQLITE_PRIVATEstatic void sqlite3PrngSaveState(void);
19024SQLITE_PRIVATEstatic void sqlite3PrngRestoreState(void);
19025#endif
19026SQLITE_PRIVATEstatic void sqlite3RollbackAll(sqlite3*,int);
19027SQLITE_PRIVATEstatic void sqlite3CodeVerifySchema(Parse*, int);
19028SQLITE_PRIVATEstatic void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
19029SQLITE_PRIVATEstatic void sqlite3BeginTransaction(Parse*, int);
19030SQLITE_PRIVATEstatic void sqlite3EndTransaction(Parse*,int);
19031SQLITE_PRIVATEstatic void sqlite3Savepoint(Parse*, int, Token*);
19032SQLITE_PRIVATEstatic void sqlite3CloseSavepoints(sqlite3 *);
19033SQLITE_PRIVATEstatic void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
19034SQLITE_PRIVATEstatic int sqlite3ExprIdToTrueFalse(Expr*);
19035SQLITE_PRIVATEstatic int sqlite3ExprTruthValue(const Expr*);
19036SQLITE_PRIVATEstatic int sqlite3ExprIsConstant(Expr*);
19037SQLITE_PRIVATEstatic int sqlite3ExprIsConstantNotJoin(Expr*);
19038SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrFunction(Expr*, u8);
19039SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
19040SQLITE_PRIVATEstatic int sqlite3ExprIsTableConstant(Expr*,int);
19041#ifdef SQLITE_ENABLE_CURSOR_HINTS
19042SQLITE_PRIVATEstatic int sqlite3ExprContainsSubquery(Expr*);
19043#endif
19044SQLITE_PRIVATEstatic int sqlite3ExprIsInteger(Expr*, int*);
19045SQLITE_PRIVATEstatic int sqlite3ExprCanBeNull(const Expr*);
19046SQLITE_PRIVATEstatic int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
19047SQLITE_PRIVATEstatic int sqlite3IsRowid(const char*);
19048SQLITE_PRIVATEstatic void sqlite3GenerateRowDelete(
19049 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
19050SQLITE_PRIVATEstatic void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
19051SQLITE_PRIVATEstatic int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
19052SQLITE_PRIVATEstatic void sqlite3ResolvePartIdxLabel(Parse*,int);
19053SQLITE_PRIVATEstatic int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
19054SQLITE_PRIVATEstatic void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
19055 u8,u8,int,int*,int*,Upsert*);
19056#ifdef SQLITE_ENABLE_NULL_TRIM
19057SQLITE_PRIVATEstatic void sqlite3SetMakeRecordP5(Vdbe*,Table*);
19058#else
19059# define sqlite3SetMakeRecordP5(A,B)
19060#endif
19061SQLITE_PRIVATEstatic void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
19062SQLITE_PRIVATEstatic int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
19063SQLITE_PRIVATEstatic void sqlite3BeginWriteOperation(Parse*, int, int);
19064SQLITE_PRIVATEstatic void sqlite3MultiWrite(Parse*);
19065SQLITE_PRIVATEstatic void sqlite3MayAbort(Parse*);
19066SQLITE_PRIVATEstatic void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
19067SQLITE_PRIVATEstatic void sqlite3UniqueConstraint(Parse*, int, Index*);
19068SQLITE_PRIVATEstatic void sqlite3RowidConstraint(Parse*, int, Table*);
19069SQLITE_PRIVATEstatic Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
19070SQLITE_PRIVATEstatic ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
19071SQLITE_PRIVATEstatic SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
19072SQLITE_PRIVATEstatic IdList *sqlite3IdListDup(sqlite3*,IdList*);
19073SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3*,Select*,int);
19074SQLITE_PRIVATEstatic FuncDef *sqlite3FunctionSearch(int,const char*);
19075SQLITE_PRIVATEstatic void sqlite3InsertBuiltinFuncs(FuncDef*,int);
19076SQLITE_PRIVATEstatic FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
19077SQLITE_PRIVATEstatic void sqlite3RegisterBuiltinFunctions(void);
19078SQLITE_PRIVATEstatic void sqlite3RegisterDateTimeFunctions(void);
19079SQLITE_PRIVATEstatic void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
19080SQLITE_PRIVATEstatic int sqlite3SafetyCheckOk(sqlite3*);
19081SQLITE_PRIVATEstatic int sqlite3SafetyCheckSickOrOk(sqlite3*);
19082SQLITE_PRIVATEstatic void sqlite3ChangeCookie(Parse*, int);
19083
19084#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
19085SQLITE_PRIVATEstatic void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
19086#endif
19087
19088#ifndef SQLITE_OMIT_TRIGGER
19089SQLITE_PRIVATEstatic void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
19090 Expr*,int, int);
19091SQLITE_PRIVATEstatic void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
19092SQLITE_PRIVATEstatic void sqlite3DropTrigger(Parse*, SrcList*, int);
19093SQLITE_PRIVATEstatic void sqlite3DropTriggerPtr(Parse*, Trigger*);
19094SQLITE_PRIVATEstatic Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
19095SQLITE_PRIVATEstatic Trigger *sqlite3TriggerList(Parse *, Table *);
19096SQLITE_PRIVATEstatic void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
19097 int, int, int);
19098SQLITE_PRIVATEstatic void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
19099 void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
19100SQLITE_PRIVATEstatic void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
19101SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
19102 const char*,const char*);
19103SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
19104 Select*,u8,Upsert*,
19105 const char*,const char*);
19106SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,ExprList*, Expr*, u8,
19107 const char*,const char*);
19108SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
19109 const char*,const char*);
19110SQLITE_PRIVATEstatic void sqlite3DeleteTrigger(sqlite3*, Trigger*);
19111SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
19112SQLITE_PRIVATEstatic u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
19113# define sqlite3ParseToplevel(p)((p)->pToplevel ? (p)->pToplevel : (p)) ((p)->pToplevel ? (p)->pToplevel : (p))
19114# define sqlite3IsToplevel(p)((p)->pToplevel==0) ((p)->pToplevel==0)
19115#else
19116# define sqlite3TriggersExist(B,C,D,E,F) 0
19117# define sqlite3DeleteTrigger(A,B)
19118# define sqlite3DropTriggerPtr(A,B)
19119# define sqlite3UnlinkAndDeleteTrigger(A,B,C)
19120# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
19121# define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
19122# define sqlite3TriggerList(X, Y) 0
19123# define sqlite3ParseToplevel(p)((p)->pToplevel ? (p)->pToplevel : (p)) p
19124# define sqlite3IsToplevel(p)((p)->pToplevel==0) 1
19125# define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
19126#endif
19127
19128SQLITE_PRIVATEstatic int sqlite3JoinType(Parse*, Token*, Token*, Token*);
19129SQLITE_PRIVATEstatic void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
19130SQLITE_PRIVATEstatic void sqlite3DeferForeignKey(Parse*, int);
19131#ifndef SQLITE_OMIT_AUTHORIZATION
19132SQLITE_PRIVATEstatic void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
19133SQLITE_PRIVATEstatic int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
19134SQLITE_PRIVATEstatic void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
19135SQLITE_PRIVATEstatic void sqlite3AuthContextPop(AuthContext*);
19136SQLITE_PRIVATEstatic int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
19137#else
19138# define sqlite3AuthRead(a,b,c,d)
19139# define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK0
19140# define sqlite3AuthContextPush(a,b,c)
19141# define sqlite3AuthContextPop(a) ((void)(a))
19142#endif
19143SQLITE_PRIVATEstatic void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
19144SQLITE_PRIVATEstatic void sqlite3Detach(Parse*, Expr*);
19145SQLITE_PRIVATEstatic void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
19146SQLITE_PRIVATEstatic int sqlite3FixSrcList(DbFixer*, SrcList*);
19147SQLITE_PRIVATEstatic int sqlite3FixSelect(DbFixer*, Select*);
19148SQLITE_PRIVATEstatic int sqlite3FixExpr(DbFixer*, Expr*);
19149SQLITE_PRIVATEstatic int sqlite3FixExprList(DbFixer*, ExprList*);
19150SQLITE_PRIVATEstatic int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
19151SQLITE_PRIVATEstatic int sqlite3RealSameAsInt(double,sqlite3_int64);
19152SQLITE_PRIVATEstatic int sqlite3AtoF(const char *z, double*, int, u8);
19153SQLITE_PRIVATEstatic int sqlite3GetInt32(const char *, int*);
19154SQLITE_PRIVATEstatic int sqlite3Atoi(const char*);
19155#ifndef SQLITE_OMIT_UTF16
19156SQLITE_PRIVATEstatic int sqlite3Utf16ByteLen(const void *pData, int nChar);
19157#endif
19158SQLITE_PRIVATEstatic int sqlite3Utf8CharLen(const char *pData, int nByte);
19159SQLITE_PRIVATEstatic u32 sqlite3Utf8Read(const u8**);
19160SQLITE_PRIVATEstatic LogEst sqlite3LogEst(u64);
19161SQLITE_PRIVATEstatic LogEst sqlite3LogEstAdd(LogEst,LogEst);
19162#ifndef SQLITE_OMIT_VIRTUALTABLE
19163SQLITE_PRIVATEstatic LogEst sqlite3LogEstFromDouble(double);
19164#endif
19165#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
19166 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
19167 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
19168SQLITE_PRIVATEstatic u64 sqlite3LogEstToInt(LogEst);
19169#endif
19170SQLITE_PRIVATEstatic VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
19171SQLITE_PRIVATEstatic const char *sqlite3VListNumToName(VList*,int);
19172SQLITE_PRIVATEstatic int sqlite3VListNameToNum(VList*,const char*,int);
19173
19174/*
19175** Routines to read and write variable-length integers. These used to
19176** be defined locally, but now we use the varint routines in the util.c
19177** file.
19178*/
19179SQLITE_PRIVATEstatic int sqlite3PutVarint(unsigned char*, u64);
19180SQLITE_PRIVATEstatic u8 sqlite3GetVarint(const unsigned char *, u64 *);
19181SQLITE_PRIVATEstatic u8 sqlite3GetVarint32(const unsigned char *, u32 *);
19182SQLITE_PRIVATEstatic int sqlite3VarintLen(u64 v);
19183
19184/*
19185** The common case is for a varint to be a single byte. They following
19186** macros handle the common case without a procedure call, but then call
19187** the procedure for larger varints.
19188*/
19189#define getVarint32(A,B)(u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32(
(A),(u32 *)&(B)))
\
19190 (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
19191#define putVarint32(A,B)(u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1: sqlite3PutVarint
((A),(B)))
\
19192 (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
19193 sqlite3PutVarint((A),(B)))
19194#define getVarintsqlite3GetVarint sqlite3GetVarint
19195#define putVarintsqlite3PutVarint sqlite3PutVarint
19196
19197
19198SQLITE_PRIVATEstatic const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
19199SQLITE_PRIVATEstatic void sqlite3TableAffinity(Vdbe*, Table*, int);
19200SQLITE_PRIVATEstatic char sqlite3CompareAffinity(Expr *pExpr, char aff2);
19201SQLITE_PRIVATEstatic int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
19202SQLITE_PRIVATEstatic char sqlite3TableColumnAffinity(Table*,int);
19203SQLITE_PRIVATEstatic char sqlite3ExprAffinity(Expr *pExpr);
19204SQLITE_PRIVATEstatic int sqlite3Atoi64(const char*, i64*, int, u8);
19205SQLITE_PRIVATEstatic int sqlite3DecOrHexToI64(const char*, i64*);
19206SQLITE_PRIVATEstatic void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
19207SQLITE_PRIVATEstatic void sqlite3Error(sqlite3*,int);
19208SQLITE_PRIVATEstatic void sqlite3SystemError(sqlite3*,int);
19209SQLITE_PRIVATEstatic void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
19210SQLITE_PRIVATEstatic u8 sqlite3HexToInt(int h);
19211SQLITE_PRIVATEstatic int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
19212
19213#if defined(SQLITE_NEED_ERR_NAME)
19214SQLITE_PRIVATEstatic const char *sqlite3ErrName(int);
19215#endif
19216
19217#ifdef SQLITE_ENABLE_DESERIALIZE
19218SQLITE_PRIVATEstatic int sqlite3MemdbInit(void);
19219#endif
19220
19221SQLITE_PRIVATEstatic const char *sqlite3ErrStr(int);
19222SQLITE_PRIVATEstatic int sqlite3ReadSchema(Parse *pParse);
19223SQLITE_PRIVATEstatic CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
19224SQLITE_PRIVATEstatic int sqlite3IsBinary(const CollSeq*);
19225SQLITE_PRIVATEstatic CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
19226SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
19227SQLITE_PRIVATEstatic CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
19228SQLITE_PRIVATEstatic int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
19229SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
19230SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
19231SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollate(Expr*);
19232SQLITE_PRIVATEstatic int sqlite3CheckCollSeq(Parse *, CollSeq *);
19233SQLITE_PRIVATEstatic int sqlite3WritableSchema(sqlite3*);
19234SQLITE_PRIVATEstatic int sqlite3CheckObjectName(Parse *, const char *);
19235SQLITE_PRIVATEstatic void sqlite3VdbeSetChanges(sqlite3 *, int);
19236SQLITE_PRIVATEstatic int sqlite3AddInt64(i64*,i64);
19237SQLITE_PRIVATEstatic int sqlite3SubInt64(i64*,i64);
19238SQLITE_PRIVATEstatic int sqlite3MulInt64(i64*,i64);
19239SQLITE_PRIVATEstatic int sqlite3AbsInt32(int);
19240#ifdef SQLITE_ENABLE_8_3_NAMES
19241SQLITE_PRIVATEstatic void sqlite3FileSuffix3(const char*, char*);
19242#else
19243# define sqlite3FileSuffix3(X,Y)
19244#endif
19245SQLITE_PRIVATEstatic u8 sqlite3GetBoolean(const char *z,u8);
19246
19247SQLITE_PRIVATEstatic const void *sqlite3ValueText(sqlite3_value*, u8);
19248SQLITE_PRIVATEstatic int sqlite3ValueBytes(sqlite3_value*, u8);
19249SQLITE_PRIVATEstatic void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
19250 void(*)(void*));
19251SQLITE_PRIVATEstatic void sqlite3ValueSetNull(sqlite3_value*);
19252SQLITE_PRIVATEstatic void sqlite3ValueFree(sqlite3_value*);
19253#ifndef SQLITE_UNTESTABLE
19254SQLITE_PRIVATEstatic void sqlite3ResultIntReal(sqlite3_context*);
19255#endif
19256SQLITE_PRIVATEstatic sqlite3_value *sqlite3ValueNew(sqlite3 *);
19257#ifndef SQLITE_OMIT_UTF16
19258SQLITE_PRIVATEstatic char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
19259#endif
19260SQLITE_PRIVATEstatic int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
19261SQLITE_PRIVATEstatic void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
19262#ifndef SQLITE_AMALGAMATION1
19263SQLITE_PRIVATEstatic const unsigned char sqlite3OpcodeProperty[];
19264SQLITE_PRIVATEstatic const char sqlite3StrBINARY[];
19265SQLITE_PRIVATEstatic const unsigned char sqlite3UpperToLower[];
19266SQLITE_PRIVATEstatic const unsigned char sqlite3CtypeMap[];
19267SQLITE_PRIVATEstatic const Token sqlite3IntTokens[];
19268SQLITE_PRIVATEstatic SQLITE_WSD struct Sqlite3Config sqlite3Config;
19269SQLITE_PRIVATEstatic FuncDefHash sqlite3BuiltinFunctions;
19270#ifndef SQLITE_OMIT_WSD
19271SQLITE_PRIVATEstatic int sqlite3PendingByte;
19272#endif
19273#endif
19274#ifdef VDBE_PROFILE
19275SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3NProfileCnt;
19276#endif
19277SQLITE_PRIVATEstatic void sqlite3RootPageMoved(sqlite3*, int, int, int);
19278SQLITE_PRIVATEstatic void sqlite3Reindex(Parse*, Token*, Token*);
19279SQLITE_PRIVATEstatic void sqlite3AlterFunctions(void);
19280SQLITE_PRIVATEstatic void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
19281SQLITE_PRIVATEstatic void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
19282SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char *, int *);
19283SQLITE_PRIVATEstatic void sqlite3NestedParse(Parse*, const char*, ...);
19284SQLITE_PRIVATEstatic void sqlite3ExpirePreparedStatements(sqlite3*, int);
19285SQLITE_PRIVATEstatic void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
19286SQLITE_PRIVATEstatic int sqlite3CodeSubselect(Parse*, Expr*);
19287SQLITE_PRIVATEstatic void sqlite3SelectPrep(Parse*, Select*, NameContext*);
19288SQLITE_PRIVATEstatic void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
19289SQLITE_PRIVATEstatic int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
19290SQLITE_PRIVATEstatic int sqlite3ResolveExprNames(NameContext*, Expr*);
19291SQLITE_PRIVATEstatic int sqlite3ResolveExprListNames(NameContext*, ExprList*);
19292SQLITE_PRIVATEstatic void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
19293SQLITE_PRIVATEstatic int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
19294SQLITE_PRIVATEstatic int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
19295SQLITE_PRIVATEstatic void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
19296SQLITE_PRIVATEstatic void sqlite3AlterFinishAddColumn(Parse *, Token *);
19297SQLITE_PRIVATEstatic void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
19298SQLITE_PRIVATEstatic void *sqlite3RenameTokenMap(Parse*, void*, Token*);
19299SQLITE_PRIVATEstatic void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
19300SQLITE_PRIVATEstatic void sqlite3RenameExprUnmap(Parse*, Expr*);
19301SQLITE_PRIVATEstatic void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
19302SQLITE_PRIVATEstatic CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
19303SQLITE_PRIVATEstatic char sqlite3AffinityType(const char*, Column*);
19304SQLITE_PRIVATEstatic void sqlite3Analyze(Parse*, Token*, Token*);
19305SQLITE_PRIVATEstatic int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
19306SQLITE_PRIVATEstatic int sqlite3FindDb(sqlite3*, Token*);
19307SQLITE_PRIVATEstatic int sqlite3FindDbName(sqlite3 *, const char *);
19308SQLITE_PRIVATEstatic int sqlite3AnalysisLoad(sqlite3*,int iDB);
19309SQLITE_PRIVATEstatic void sqlite3DeleteIndexSamples(sqlite3*,Index*);
19310SQLITE_PRIVATEstatic void sqlite3DefaultRowEst(Index*);
19311SQLITE_PRIVATEstatic void sqlite3RegisterLikeFunctions(sqlite3*, int);
19312SQLITE_PRIVATEstatic int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
19313SQLITE_PRIVATEstatic void sqlite3SchemaClear(void *);
19314SQLITE_PRIVATEstatic Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
19315SQLITE_PRIVATEstatic int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
19316SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
19317SQLITE_PRIVATEstatic void sqlite3KeyInfoUnref(KeyInfo*);
19318SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
19319SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
19320SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
19321
19322#ifdef SQLITE_DEBUG
19323SQLITE_PRIVATEstatic int sqlite3KeyInfoIsWriteable(KeyInfo*);
19324#endif
19325SQLITE_PRIVATEstatic int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
19326 void (*)(sqlite3_context*,int,sqlite3_value **),
19327 void (*)(sqlite3_context*,int,sqlite3_value **),
19328 void (*)(sqlite3_context*),
19329 void (*)(sqlite3_context*),
19330 void (*)(sqlite3_context*,int,sqlite3_value **),
19331 FuncDestructor *pDestructor
19332);
19333SQLITE_PRIVATEstatic void sqlite3NoopDestructor(void*);
19334SQLITE_PRIVATEstatic void sqlite3OomFault(sqlite3*);
19335SQLITE_PRIVATEstatic void sqlite3OomClear(sqlite3*);
19336SQLITE_PRIVATEstatic int sqlite3ApiExit(sqlite3 *db, int);
19337SQLITE_PRIVATEstatic int sqlite3OpenTempDatabase(Parse *);
19338
19339SQLITE_PRIVATEstatic void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
19340SQLITE_PRIVATEstatic char *sqlite3StrAccumFinish(StrAccum*);
19341SQLITE_PRIVATEstatic void sqlite3SelectDestInit(SelectDest*,int,int);
19342SQLITE_PRIVATEstatic Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
19343
19344SQLITE_PRIVATEstatic void sqlite3BackupRestart(sqlite3_backup *);
19345SQLITE_PRIVATEstatic void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
19346
19347#ifndef SQLITE_OMIT_SUBQUERY
19348SQLITE_PRIVATEstatic int sqlite3ExprCheckIN(Parse*, Expr*);
19349#else
19350# define sqlite3ExprCheckIN(x,y) SQLITE_OK0
19351#endif
19352
19353#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
19354SQLITE_PRIVATEstatic void sqlite3AnalyzeFunctions(void);
19355SQLITE_PRIVATEstatic int sqlite3Stat4ProbeSetValue(
19356 Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
19357SQLITE_PRIVATEstatic int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
19358SQLITE_PRIVATEstatic void sqlite3Stat4ProbeFree(UnpackedRecord*);
19359SQLITE_PRIVATEstatic int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
19360SQLITE_PRIVATEstatic char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
19361#endif
19362
19363/*
19364** The interface to the LEMON-generated parser
19365*/
19366#ifndef SQLITE_AMALGAMATION1
19367SQLITE_PRIVATEstatic void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
19368SQLITE_PRIVATEstatic void sqlite3ParserFree(void*, void(*)(void*));
19369#endif
19370SQLITE_PRIVATEstatic void sqlite3Parser(void*, int, Token);
19371SQLITE_PRIVATEstatic int sqlite3ParserFallback(int);
19372#ifdef YYTRACKMAXSTACKDEPTH
19373SQLITE_PRIVATEstatic int sqlite3ParserStackPeak(void*);
19374#endif
19375
19376SQLITE_PRIVATEstatic void sqlite3AutoLoadExtensions(sqlite3*);
19377#ifndef SQLITE_OMIT_LOAD_EXTENSION
19378SQLITE_PRIVATEstatic void sqlite3CloseExtensions(sqlite3*);
19379#else
19380# define sqlite3CloseExtensions(X)
19381#endif
19382
19383#ifndef SQLITE_OMIT_SHARED_CACHE
19384SQLITE_PRIVATEstatic void sqlite3TableLock(Parse *, int, int, u8, const char *);
19385#else
19386 #define sqlite3TableLock(v,w,x,y,z)
19387#endif
19388
19389#ifdef SQLITE_TEST
19390SQLITE_PRIVATEstatic int sqlite3Utf8To8(unsigned char*);
19391#endif
19392
19393#ifdef SQLITE_OMIT_VIRTUALTABLE
19394# define sqlite3VtabClear(Y)
19395# define sqlite3VtabSync(X,Y) SQLITE_OK0
19396# define sqlite3VtabRollback(X)
19397# define sqlite3VtabCommit(X)
19398# define sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) 0
19399# define sqlite3VtabLock(X)
19400# define sqlite3VtabUnlock(X)
19401# define sqlite3VtabUnlockList(X)
19402# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK0
19403# define sqlite3GetVTable(X,Y) ((VTable*)0)
19404#else
19405SQLITE_PRIVATEstatic void sqlite3VtabClear(sqlite3 *db, Table*);
19406SQLITE_PRIVATEstatic void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
19407SQLITE_PRIVATEstatic int sqlite3VtabSync(sqlite3 *db, Vdbe*);
19408SQLITE_PRIVATEstatic int sqlite3VtabRollback(sqlite3 *db);
19409SQLITE_PRIVATEstatic int sqlite3VtabCommit(sqlite3 *db);
19410SQLITE_PRIVATEstatic void sqlite3VtabLock(VTable *);
19411SQLITE_PRIVATEstatic void sqlite3VtabUnlock(VTable *);
19412SQLITE_PRIVATEstatic void sqlite3VtabUnlockList(sqlite3*);
19413SQLITE_PRIVATEstatic int sqlite3VtabSavepoint(sqlite3 *, int, int);
19414SQLITE_PRIVATEstatic void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
19415SQLITE_PRIVATEstatic VTable *sqlite3GetVTable(sqlite3*, Table*);
19416SQLITE_PRIVATEstatic Module *sqlite3VtabCreateModule(
19417 sqlite3*,
19418 const char*,
19419 const sqlite3_module*,
19420 void*,
19421 void(*)(void*)
19422 );
19423# define sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) ((db)->nVTrans>0 && (db)->aVTrans==0)
19424#endif
19425SQLITE_PRIVATEstatic int sqlite3VtabEponymousTableInit(Parse*,Module*);
19426SQLITE_PRIVATEstatic void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
19427SQLITE_PRIVATEstatic void sqlite3VtabMakeWritable(Parse*,Table*);
19428SQLITE_PRIVATEstatic void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
19429SQLITE_PRIVATEstatic void sqlite3VtabFinishParse(Parse*, Token*);
19430SQLITE_PRIVATEstatic void sqlite3VtabArgInit(Parse*);
19431SQLITE_PRIVATEstatic void sqlite3VtabArgExtend(Parse*, Token*);
19432SQLITE_PRIVATEstatic int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
19433SQLITE_PRIVATEstatic int sqlite3VtabCallConnect(Parse*, Table*);
19434SQLITE_PRIVATEstatic int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
19435SQLITE_PRIVATEstatic int sqlite3VtabBegin(sqlite3 *, VTable *);
19436SQLITE_PRIVATEstatic FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
19437SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
19438SQLITE_PRIVATEstatic int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
19439SQLITE_PRIVATEstatic int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
19440SQLITE_PRIVATEstatic void sqlite3ParserReset(Parse*);
19441#ifdef SQLITE_ENABLE_NORMALIZE
19442SQLITE_PRIVATEstatic char *sqlite3Normalize(Vdbe*, const char*);
19443#endif
19444SQLITE_PRIVATEstatic int sqlite3Reprepare(Vdbe*);
19445SQLITE_PRIVATEstatic void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
19446SQLITE_PRIVATEstatic CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
19447SQLITE_PRIVATEstatic int sqlite3TempInMemory(const sqlite3*);
19448SQLITE_PRIVATEstatic const char *sqlite3JournalModename(int);
19449#ifndef SQLITE_OMIT_WAL
19450SQLITE_PRIVATEstatic int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
19451SQLITE_PRIVATEstatic int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
19452#endif
19453#ifndef SQLITE_OMIT_CTE
19454SQLITE_PRIVATEstatic With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
19455SQLITE_PRIVATEstatic void sqlite3WithDelete(sqlite3*,With*);
19456SQLITE_PRIVATEstatic void sqlite3WithPush(Parse*, With*, u8);
19457#else
19458#define sqlite3WithPush(x,y,z)
19459#define sqlite3WithDelete(x,y)
19460#endif
19461#ifndef SQLITE_OMIT_UPSERT
19462SQLITE_PRIVATEstatic Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
19463SQLITE_PRIVATEstatic void sqlite3UpsertDelete(sqlite3*,Upsert*);
19464SQLITE_PRIVATEstatic Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
19465SQLITE_PRIVATEstatic int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
19466SQLITE_PRIVATEstatic void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
19467#else
19468#define sqlite3UpsertNew(v,w,x,y,z) ((Upsert*)0)
19469#define sqlite3UpsertDelete(x,y)
19470#define sqlite3UpsertDup(x,y) ((Upsert*)0)
19471#endif
19472
19473
19474/* Declarations for functions in fkey.c. All of these are replaced by
19475** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
19476** key functionality is available. If OMIT_TRIGGER is defined but
19477** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
19478** this case foreign keys are parsed, but no other functionality is
19479** provided (enforcement of FK constraints requires the triggers sub-system).
19480*/
19481#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
19482SQLITE_PRIVATEstatic void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
19483SQLITE_PRIVATEstatic void sqlite3FkDropTable(Parse*, SrcList *, Table*);
19484SQLITE_PRIVATEstatic void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
19485SQLITE_PRIVATEstatic int sqlite3FkRequired(Parse*, Table*, int*, int);
19486SQLITE_PRIVATEstatic u32 sqlite3FkOldmask(Parse*, Table*);
19487SQLITE_PRIVATEstatic FKey *sqlite3FkReferences(Table *);
19488#else
19489 #define sqlite3FkActions(a,b,c,d,e,f)
19490 #define sqlite3FkCheck(a,b,c,d,e,f)
19491 #define sqlite3FkDropTable(a,b,c)
19492 #define sqlite3FkOldmask(a,b) 0
19493 #define sqlite3FkRequired(a,b,c,d) 0
19494 #define sqlite3FkReferences(a) 0
19495#endif
19496#ifndef SQLITE_OMIT_FOREIGN_KEY
19497SQLITE_PRIVATEstatic void sqlite3FkDelete(sqlite3 *, Table*);
19498SQLITE_PRIVATEstatic int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
19499#else
19500 #define sqlite3FkDelete(a,b)
19501 #define sqlite3FkLocateIndex(a,b,c,d,e)
19502#endif
19503
19504
19505/*
19506** Available fault injectors. Should be numbered beginning with 0.
19507*/
19508#define SQLITE_FAULTINJECTOR_MALLOC0 0
19509#define SQLITE_FAULTINJECTOR_COUNT1 1
19510
19511/*
19512** The interface to the code in fault.c used for identifying "benign"
19513** malloc failures. This is only present if SQLITE_UNTESTABLE
19514** is not defined.
19515*/
19516#ifndef SQLITE_UNTESTABLE
19517SQLITE_PRIVATEstatic void sqlite3BeginBenignMalloc(void);
19518SQLITE_PRIVATEstatic void sqlite3EndBenignMalloc(void);
19519#else
19520 #define sqlite3BeginBenignMalloc()
19521 #define sqlite3EndBenignMalloc()
19522#endif
19523
19524/*
19525** Allowed return values from sqlite3FindInIndex()
19526*/
19527#define IN_INDEX_ROWID1 1 /* Search the rowid of the table */
19528#define IN_INDEX_EPH2 2 /* Search an ephemeral b-tree */
19529#define IN_INDEX_INDEX_ASC3 3 /* Existing index ASCENDING */
19530#define IN_INDEX_INDEX_DESC4 4 /* Existing index DESCENDING */
19531#define IN_INDEX_NOOP5 5 /* No table available. Use comparisons */
19532/*
19533** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
19534*/
19535#define IN_INDEX_NOOP_OK0x0001 0x0001 /* OK to return IN_INDEX_NOOP */
19536#define IN_INDEX_MEMBERSHIP0x0002 0x0002 /* IN operator used for membership test */
19537#define IN_INDEX_LOOP0x0004 0x0004 /* IN operator used as a loop */
19538SQLITE_PRIVATEstatic int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
19539
19540SQLITE_PRIVATEstatic int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
19541SQLITE_PRIVATEstatic int sqlite3JournalSize(sqlite3_vfs *);
19542#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
19543 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
19544SQLITE_PRIVATEstatic int sqlite3JournalCreate(sqlite3_file *);
19545#endif
19546
19547SQLITE_PRIVATEstatic int sqlite3JournalIsInMemory(sqlite3_file *p);
19548SQLITE_PRIVATEstatic void sqlite3MemJournalOpen(sqlite3_file *);
19549
19550SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
19551#if SQLITE_MAX_EXPR_DEPTH1000>0
19552SQLITE_PRIVATEstatic int sqlite3SelectExprHeight(Select *);
19553SQLITE_PRIVATEstatic int sqlite3ExprCheckHeight(Parse*, int);
19554#else
19555 #define sqlite3SelectExprHeight(x) 0
19556 #define sqlite3ExprCheckHeight(x,y)
19557#endif
19558
19559SQLITE_PRIVATEstatic u32 sqlite3Get4byte(const u8*);
19560SQLITE_PRIVATEstatic void sqlite3Put4byte(u8*, u32);
19561
19562#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
19563SQLITE_PRIVATEstatic void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
19564SQLITE_PRIVATEstatic void sqlite3ConnectionUnlocked(sqlite3 *db);
19565SQLITE_PRIVATEstatic void sqlite3ConnectionClosed(sqlite3 *db);
19566#else
19567 #define sqlite3ConnectionBlocked(x,y)
19568 #define sqlite3ConnectionUnlocked(x)
19569 #define sqlite3ConnectionClosed(x)
19570#endif
19571
19572#ifdef SQLITE_DEBUG
19573SQLITE_PRIVATEstatic void sqlite3ParserTrace(FILE*, char *);
19574#endif
19575#if defined(YYCOVERAGE)
19576SQLITE_PRIVATEstatic int sqlite3ParserCoverage(FILE*);
19577#endif
19578
19579/*
19580** If the SQLITE_ENABLE IOTRACE exists then the global variable
19581** sqlite3IoTrace is a pointer to a printf-like routine used to
19582** print I/O tracing messages.
19583*/
19584#ifdef SQLITE_ENABLE_IOTRACE
19585# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
19586SQLITE_PRIVATEstatic void sqlite3VdbeIOTraceSql(Vdbe*);
19587SQLITE_API SQLITE_EXTERNextern void (SQLITE_CDECL *sqlite3IoTrace)(const char*,...);
19588#else
19589# define IOTRACE(A)
19590# define sqlite3VdbeIOTraceSql(X)
19591#endif
19592
19593/*
19594** These routines are available for the mem2.c debugging memory allocator
19595** only. They are used to verify that different "types" of memory
19596** allocations are properly tracked by the system.
19597**
19598** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
19599** the MEMTYPE_* macros defined below. The type must be a bitmask with
19600** a single bit set.
19601**
19602** sqlite3MemdebugHasType() returns true if any of the bits in its second
19603** argument match the type set by the previous sqlite3MemdebugSetType().
19604** sqlite3MemdebugHasType() is intended for use inside assert() statements.
19605**
19606** sqlite3MemdebugNoType() returns true if none of the bits in its second
19607** argument match the type set by the previous sqlite3MemdebugSetType().
19608**
19609** Perhaps the most important point is the difference between MEMTYPE_HEAP
19610** and MEMTYPE_LOOKASIDE. If an allocation is MEMTYPE_LOOKASIDE, that means
19611** it might have been allocated by lookaside, except the allocation was
19612** too large or lookaside was already full. It is important to verify
19613** that allocations that might have been satisfied by lookaside are not
19614** passed back to non-lookaside free() routines. Asserts such as the
19615** example above are placed on the non-lookaside free() routines to verify
19616** this constraint.
19617**
19618** All of this is no-op for a production build. It only comes into
19619** play when the SQLITE_MEMDEBUG compile-time option is used.
19620*/
19621#ifdef SQLITE_MEMDEBUG
19622SQLITE_PRIVATEstatic void sqlite3MemdebugSetType(void*,u8);
19623SQLITE_PRIVATEstatic int sqlite3MemdebugHasType(void*,u8)1;
19624SQLITE_PRIVATEstatic int sqlite3MemdebugNoType(void*,u8)1;
19625#else
19626# define sqlite3MemdebugSetType(X,Y) /* no-op */
19627# define sqlite3MemdebugHasType(X,Y)1 1
19628# define sqlite3MemdebugNoType(X,Y)1 1
19629#endif
19630#define MEMTYPE_HEAP0x01 0x01 /* General heap allocations */
19631#define MEMTYPE_LOOKASIDE0x02 0x02 /* Heap that might have been lookaside */
19632#define MEMTYPE_PCACHE0x04 0x04 /* Page cache allocations */
19633
19634/*
19635** Threading interface
19636*/
19637#if SQLITE_MAX_WORKER_THREADS8>0
19638SQLITE_PRIVATEstatic int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
19639SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread*, void**);
19640#endif
19641
19642#if defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)
19643SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3*);
19644#endif
19645#if defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)
19646SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3*);
19647#endif
19648
19649SQLITE_PRIVATEstatic int sqlite3ExprVectorSize(Expr *pExpr);
19650SQLITE_PRIVATEstatic int sqlite3ExprIsVector(Expr *pExpr);
19651SQLITE_PRIVATEstatic Expr *sqlite3VectorFieldSubexpr(Expr*, int);
19652SQLITE_PRIVATEstatic Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
19653SQLITE_PRIVATEstatic void sqlite3VectorErrorMsg(Parse*, Expr*);
19654
19655#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
19656SQLITE_PRIVATEstatic const char **sqlite3CompileOptions(int *pnOpt);
19657#endif
19658
19659#endif /* SQLITEINT_H */
19660
19661/************** End of sqliteInt.h *******************************************/
19662/************** Begin file global.c ******************************************/
19663/*
19664** 2008 June 13
19665**
19666** The author disclaims copyright to this source code. In place of
19667** a legal notice, here is a blessing:
19668**
19669** May you do good and not evil.
19670** May you find forgiveness for yourself and forgive others.
19671** May you share freely, never taking more than you give.
19672**
19673*************************************************************************
19674**
19675** This file contains definitions of global variables and constants.
19676*/
19677/* #include "sqliteInt.h" */
19678
19679/* An array to map all upper-case characters into their corresponding
19680** lower-case character.
19681**
19682** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
19683** handle case conversions for the UTF character set since the tables
19684** involved are nearly as big or bigger than SQLite itself.
19685*/
19686SQLITE_PRIVATEstatic const unsigned char sqlite3UpperToLower[] = {
19687#ifdef SQLITE_ASCII1
19688 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
19689 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
19690 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
19691 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
19692 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
19693 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
19694 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
19695 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
19696 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
19697 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
19698 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
19699 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
19700 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
19701 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
19702 252,253,254,255
19703#endif
19704#ifdef SQLITE_EBCDIC
19705 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
19706 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
19707 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
19708 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
19709 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
19710 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
19711 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, /* 6x */
19712 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, /* 7x */
19713 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
19714 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, /* 9x */
19715 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
19716 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
19717 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
19718 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
19719 224,225,162,163,164,165,166,167,168,169,234,235,236,237,238,239, /* Ex */
19720 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, /* Fx */
19721#endif
19722};
19723
19724/*
19725** The following 256 byte lookup table is used to support SQLites built-in
19726** equivalents to the following standard library functions:
19727**
19728** isspace() 0x01
19729** isalpha() 0x02
19730** isdigit() 0x04
19731** isalnum() 0x06
19732** isxdigit() 0x08
19733** toupper() 0x20
19734** SQLite identifier character 0x40
19735** Quote character 0x80
19736**
19737** Bit 0x20 is set if the mapped character requires translation to upper
19738** case. i.e. if the character is a lower-case ASCII character.
19739** If x is a lower-case ASCII character, then its upper-case equivalent
19740** is (x - 0x20). Therefore toupper() can be implemented as:
19741**
19742** (x & ~(map[x]&0x20))
19743**
19744** The equivalent of tolower() is implemented using the sqlite3UpperToLower[]
19745** array. tolower() is used more often than toupper() by SQLite.
19746**
19747** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
19748** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
19749** non-ASCII UTF character. Hence the test for whether or not a character is
19750** part of an identifier is 0x46.
19751*/
19752#ifdef SQLITE_ASCII1
19753SQLITE_PRIVATEstatic const unsigned char sqlite3CtypeMap[256] = {
19754 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
19755 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
19756 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
19757 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
19758 0x01, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x80, /* 20..27 !"#$%&' */
19759 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
19760 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
19761 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
19762
19763 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
19764 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
19765 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
19766 0x02, 0x02, 0x02, 0x80, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
19767 0x80, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
19768 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
19769 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
19770 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
19771
19772 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
19773 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
19774 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
19775 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
19776 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
19777 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
19778 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
19779 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
19780
19781 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
19782 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
19783 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
19784 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
19785 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
19786 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
19787 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
19788 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
19789};
19790#endif
19791
19792/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
19793** compatibility for legacy applications, the URI filename capability is
19794** disabled by default.
19795**
19796** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
19797** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
19798**
19799** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
19800** disabled. The default value may be changed by compiling with the
19801** SQLITE_USE_URI symbol defined.
19802**
19803** URI filenames are enabled by default if SQLITE_HAS_CODEC is
19804** enabled.
19805*/
19806#ifndef SQLITE_USE_URI0
19807# ifdef SQLITE_HAS_CODEC
19808# define SQLITE_USE_URI0 1
19809# else
19810# define SQLITE_USE_URI0 0
19811# endif
19812#endif
19813
19814/* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
19815** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
19816** that compile-time option is omitted.
19817*/
19818#if !defined(SQLITE_ALLOW_COVERING_INDEX_SCAN1)
19819# define SQLITE_ALLOW_COVERING_INDEX_SCAN1 1
19820#else
19821# if !SQLITE_ALLOW_COVERING_INDEX_SCAN1
19822# error "Compile-time disabling of covering index scan using the\
19823 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
19824 Contact SQLite developers if this is a problem for you, and\
19825 delete this #error macro to continue with your build."
19826# endif
19827#endif
19828
19829/* The minimum PMA size is set to this value multiplied by the database
19830** page size in bytes.
19831*/
19832#ifndef SQLITE_SORTER_PMASZ250
19833# define SQLITE_SORTER_PMASZ250 250
19834#endif
19835
19836/* Statement journals spill to disk when their size exceeds the following
19837** threshold (in bytes). 0 means that statement journals are created and
19838** written to disk immediately (the default behavior for SQLite versions
19839** before 3.12.0). -1 means always keep the entire statement journal in
19840** memory. (The statement journal is also always held entirely in memory
19841** if journal_mode=MEMORY or if temp_store=MEMORY, regardless of this
19842** setting.)
19843*/
19844#ifndef SQLITE_STMTJRNL_SPILL(64*1024)
19845# define SQLITE_STMTJRNL_SPILL(64*1024) (64*1024)
19846#endif
19847
19848/*
19849** The default lookaside-configuration, the format "SZ,N". SZ is the
19850** number of bytes in each lookaside slot (should be a multiple of 8)
19851** and N is the number of slots. The lookaside-configuration can be
19852** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
19853** or at run-time for an individual database connection using
19854** sqlite3_db_config(db, SQLITE_DBCONFIG_LOOKASIDE);
19855*/
19856#ifndef SQLITE_DEFAULT_LOOKASIDE1200,100
19857# define SQLITE_DEFAULT_LOOKASIDE1200,100 1200,100
19858#endif
19859
19860
19861/* The default maximum size of an in-memory database created using
19862** sqlite3_deserialize()
19863*/
19864#ifndef SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824
19865# define SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824 1073741824
19866#endif
19867
19868/*
19869** The following singleton contains the global configuration for
19870** the SQLite library.
19871*/
19872SQLITE_PRIVATEstatic SQLITE_WSD struct Sqlite3Config sqlite3Config = {
19873 SQLITE_DEFAULT_MEMSTATUS1, /* bMemstat */
19874 1, /* bCoreMutex */
19875 SQLITE_THREADSAFE1==1, /* bFullMutex */
19876 SQLITE_USE_URI0, /* bOpenUri */
19877 SQLITE_ALLOW_COVERING_INDEX_SCAN1, /* bUseCis */
19878 0, /* bSmallMalloc */
19879 0x7ffffffe, /* mxStrlen */
19880 0, /* neverCorrupt */
19881 SQLITE_DEFAULT_LOOKASIDE1200,100, /* szLookaside, nLookaside */
19882 SQLITE_STMTJRNL_SPILL(64*1024), /* nStmtSpill */
19883 {0,0,0,0,0,0,0,0}, /* m */
19884 {0,0,0,0,0,0,0,0,0}, /* mutex */
19885 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
19886 (void*)0, /* pHeap */
19887 0, /* nHeap */
19888 0, 0, /* mnHeap, mxHeap */
19889 SQLITE_DEFAULT_MMAP_SIZE0, /* szMmap */
19890 SQLITE_MAX_MMAP_SIZE0x7fff0000, /* mxMmap */
19891 (void*)0, /* pPage */
19892 0, /* szPage */
19893 SQLITE_DEFAULT_PCACHE_INITSZ20, /* nPage */
19894 0, /* mxParserStack */
19895 0, /* sharedCacheEnabled */
19896 SQLITE_SORTER_PMASZ250, /* szPma */
19897 /* All the rest should always be initialized to zero */
19898 0, /* isInit */
19899 0, /* inProgress */
19900 0, /* isMutexInit */
19901 0, /* isMallocInit */
19902 0, /* isPCacheInit */
19903 0, /* nRefInitMutex */
19904 0, /* pInitMutex */
19905 0, /* xLog */
19906 0, /* pLogArg */
19907#ifdef SQLITE_ENABLE_SQLLOG
19908 0, /* xSqllog */
19909 0, /* pSqllogArg */
19910#endif
19911#ifdef SQLITE_VDBE_COVERAGE
19912 0, /* xVdbeBranch */
19913 0, /* pVbeBranchArg */
19914#endif
19915#ifdef SQLITE_ENABLE_DESERIALIZE
19916 SQLITE_MEMDB_DEFAULT_MAXSIZE1073741824, /* mxMemdbSize */
19917#endif
19918#ifndef SQLITE_UNTESTABLE
19919 0, /* xTestCallback */
19920#endif
19921 0, /* bLocaltimeFault */
19922 0, /* bInternalFunctions */
19923 0x7ffffffe, /* iOnceResetThreshold */
19924 SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff, /* szSorterRef */
19925};
19926
19927/*
19928** Hash table for global functions - functions common to all
19929** database connections. After initialization, this table is
19930** read-only.
19931*/
19932SQLITE_PRIVATEstatic FuncDefHash sqlite3BuiltinFunctions;
19933
19934/*
19935** Constant tokens for values 0 and 1.
19936*/
19937SQLITE_PRIVATEstatic const Token sqlite3IntTokens[] = {
19938 { "0", 1 },
19939 { "1", 1 }
19940};
19941
19942#ifdef VDBE_PROFILE
19943/*
19944** The following performance counter can be used in place of
19945** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
19946*/
19947SQLITE_PRIVATEstatic sqlite3_uint64 sqlite3NProfileCnt = 0;
19948#endif
19949
19950/*
19951** The value of the "pending" byte must be 0x40000000 (1 byte past the
19952** 1-gibabyte boundary) in a compatible database. SQLite never uses
19953** the database page that contains the pending byte. It never attempts
19954** to read or write that page. The pending byte page is set aside
19955** for use by the VFS layers as space for managing file locks.
19956**
19957** During testing, it is often desirable to move the pending byte to
19958** a different position in the file. This allows code that has to
19959** deal with the pending byte to run on files that are much smaller
19960** than 1 GiB. The sqlite3_test_control() interface can be used to
19961** move the pending byte.
19962**
19963** IMPORTANT: Changing the pending byte to any value other than
19964** 0x40000000 results in an incompatible database file format!
19965** Changing the pending byte during operation will result in undefined
19966** and incorrect behavior.
19967*/
19968#ifndef SQLITE_OMIT_WSD
19969SQLITE_PRIVATEstatic int sqlite3PendingByte = 0x40000000;
19970#endif
19971
19972/* #include "opcodes.h" */
19973/*
19974** Properties of opcodes. The OPFLG_INITIALIZER macro is
19975** created by mkopcodeh.awk during compilation. Data is obtained
19976** from the comments following the "case OP_xxxx:" statements in
19977** the vdbe.c file.
19978*/
19979SQLITE_PRIVATEstatic const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER{ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x10, 0x00, 0x01,
0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01, 0x03, 0x12, 0x03
, 0x01, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x23
, 0x0b, 0x26, 0x26, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03,
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x01, 0x01, 0x01, 0x01, 0x00
, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10, 0x10, 0x10,
0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x02
, 0x02, 0x02, 0x00, 0x00, 0x12, 0x20, 0x00, 0x00, 0x00, 0x10,
0x10, 0x00, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26
, 0x26, 0x00, 0x12, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00
, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x10,
0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00
, 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10
, 0x00, 0x00, 0x00, 0x00, 0x00,}
;
19980
19981/*
19982** Name of the default collating sequence
19983*/
19984SQLITE_PRIVATEstatic const char sqlite3StrBINARY[] = "BINARY";
19985
19986/************** End of global.c **********************************************/
19987/************** Begin file status.c ******************************************/
19988/*
19989** 2008 June 18
19990**
19991** The author disclaims copyright to this source code. In place of
19992** a legal notice, here is a blessing:
19993**
19994** May you do good and not evil.
19995** May you find forgiveness for yourself and forgive others.
19996** May you share freely, never taking more than you give.
19997**
19998*************************************************************************
19999**
20000** This module implements the sqlite3_status() interface and related
20001** functionality.
20002*/
20003/* #include "sqliteInt.h" */
20004/************** Include vdbeInt.h in the middle of status.c ******************/
20005/************** Begin file vdbeInt.h *****************************************/
20006/*
20007** 2003 September 6
20008**
20009** The author disclaims copyright to this source code. In place of
20010** a legal notice, here is a blessing:
20011**
20012** May you do good and not evil.
20013** May you find forgiveness for yourself and forgive others.
20014** May you share freely, never taking more than you give.
20015**
20016*************************************************************************
20017** This is the header file for information that is private to the
20018** VDBE. This information used to all be at the top of the single
20019** source code file "vdbe.c". When that file became too big (over
20020** 6000 lines long) it was split up into several smaller files and
20021** this header information was factored out.
20022*/
20023#ifndef SQLITE_VDBEINT_H
20024#define SQLITE_VDBEINT_H
20025
20026/*
20027** The maximum number of times that a statement will try to reparse
20028** itself before giving up and returning SQLITE_SCHEMA.
20029*/
20030#ifndef SQLITE_MAX_SCHEMA_RETRY50
20031# define SQLITE_MAX_SCHEMA_RETRY50 50
20032#endif
20033
20034/*
20035** VDBE_DISPLAY_P4 is true or false depending on whether or not the
20036** "explain" P4 display logic is enabled.
20037*/
20038#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG1) \
20039 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
20040# define VDBE_DISPLAY_P41 1
20041#else
20042# define VDBE_DISPLAY_P41 0
20043#endif
20044
20045/*
20046** SQL is translated into a sequence of instructions to be
20047** executed by a virtual machine. Each instruction is an instance
20048** of the following structure.
20049*/
20050typedef struct VdbeOp Op;
20051
20052/*
20053** Boolean values
20054*/
20055typedef unsigned Bool;
20056
20057/* Opaque type used by code in vdbesort.c */
20058typedef struct VdbeSorter VdbeSorter;
20059
20060/* Elements of the linked list at Vdbe.pAuxData */
20061typedef struct AuxData AuxData;
20062
20063/* Types of VDBE cursors */
20064#define CURTYPE_BTREE0 0
20065#define CURTYPE_SORTER1 1
20066#define CURTYPE_VTAB2 2
20067#define CURTYPE_PSEUDO3 3
20068
20069/*
20070** A VdbeCursor is an superclass (a wrapper) for various cursor objects:
20071**
20072** * A b-tree cursor
20073** - In the main database or in an ephemeral database
20074** - On either an index or a table
20075** * A sorter
20076** * A virtual table
20077** * A one-row "pseudotable" stored in a single register
20078*/
20079typedef struct VdbeCursor VdbeCursor;
20080struct VdbeCursor {
20081 u8 eCurType; /* One of the CURTYPE_* values above */
20082 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
20083 u8 nullRow; /* True if pointing to a row with no data */
20084 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
20085 u8 isTable; /* True for rowid tables. False for indexes */
20086#ifdef SQLITE_DEBUG
20087 u8 seekOp; /* Most recent seek operation on this cursor */
20088 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
20089#endif
20090 Bool isEphemeral:1; /* True for an ephemeral table */
20091 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
20092 Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
20093 Bool seekHit:1; /* See the OP_SeekHit and OP_IfNoHope opcodes */
20094 Btree *pBtx; /* Separate file holding temporary table */
20095 i64 seqCount; /* Sequence counter */
20096 int *aAltMap; /* Mapping from table to index column numbers */
20097
20098 /* Cached OP_Column parse information is only valid if cacheStatus matches
20099 ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of
20100 ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
20101 ** the cache is out of date. */
20102 u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */
20103 int seekResult; /* Result of previous sqlite3BtreeMoveto() or 0
20104 ** if there have been no prior seeks on the cursor. */
20105 /* seekResult does not distinguish between "no seeks have ever occurred
20106 ** on this cursor" and "the most recent seek was an exact match".
20107 ** For CURTYPE_PSEUDO, seekResult is the register holding the record */
20108
20109 /* When a new VdbeCursor is allocated, only the fields above are zeroed.
20110 ** The fields that follow are uninitialized, and must be individually
20111 ** initialized prior to first use. */
20112 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
20113 union {
20114 BtCursor *pCursor; /* CURTYPE_BTREE or _PSEUDO. Btree cursor */
20115 sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB. Vtab cursor */
20116 VdbeSorter *pSorter; /* CURTYPE_SORTER. Sorter object */
20117 } uc;
20118 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
20119 u32 iHdrOffset; /* Offset to next unparsed byte of the header */
20120 Pgno pgnoRoot; /* Root page of the open btree cursor */
20121 i16 nField; /* Number of fields in the header */
20122 u16 nHdrParsed; /* Number of header fields parsed so far */
20123 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
20124 u32 *aOffset; /* Pointer to aType[nField] */
20125 const u8 *aRow; /* Data for the current row, if all on one page */
20126 u32 payloadSize; /* Total number of bytes in the record */
20127 u32 szRow; /* Byte available in aRow */
20128#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
20129 u64 maskUsed; /* Mask of columns used by this cursor */
20130#endif
20131
20132 /* 2*nField extra array elements allocated for aType[], beyond the one
20133 ** static element declared in the structure. nField total array slots for
20134 ** aType[] and nField+1 array slots for aOffset[] */
20135 u32 aType[1]; /* Type values record decode. MUST BE LAST */
20136};
20137
20138
20139/*
20140** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
20141*/
20142#define CACHE_STALE0 0
20143
20144/*
20145** When a sub-program is executed (OP_Program), a structure of this type
20146** is allocated to store the current value of the program counter, as
20147** well as the current memory cell array and various other frame specific
20148** values stored in the Vdbe struct. When the sub-program is finished,
20149** these values are copied back to the Vdbe from the VdbeFrame structure,
20150** restoring the state of the VM to as it was before the sub-program
20151** began executing.
20152**
20153** The memory for a VdbeFrame object is allocated and managed by a memory
20154** cell in the parent (calling) frame. When the memory cell is deleted or
20155** overwritten, the VdbeFrame object is not freed immediately. Instead, it
20156** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
20157** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
20158** this instead of deleting the VdbeFrame immediately is to avoid recursive
20159** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
20160** child frame are released.
20161**
20162** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
20163** set to NULL if the currently executing frame is the main program.
20164*/
20165typedef struct VdbeFrame VdbeFrame;
20166struct VdbeFrame {
20167 Vdbe *v; /* VM this frame belongs to */
20168 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
20169 Op *aOp; /* Program instructions for parent frame */
20170 i64 *anExec; /* Event counters from parent frame */
20171 Mem *aMem; /* Array of memory cells for parent frame */
20172 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
20173 u8 *aOnce; /* Bitmask used by OP_Once */
20174 void *token; /* Copy of SubProgram.token */
20175 i64 lastRowid; /* Last insert rowid (sqlite3.lastRowid) */
20176 AuxData *pAuxData; /* Linked list of auxdata allocations */
20177#if SQLITE_DEBUG
20178 u32 iFrameMagic; /* magic number for sanity checking */
20179#endif
20180 int nCursor; /* Number of entries in apCsr */
20181 int pc; /* Program Counter in parent (calling) frame */
20182 int nOp; /* Size of aOp array */
20183 int nMem; /* Number of entries in aMem */
20184 int nChildMem; /* Number of memory cells for child frame */
20185 int nChildCsr; /* Number of cursors for child frame */
20186 int nChange; /* Statement changes (Vdbe.nChange) */
20187 int nDbChange; /* Value of db->nChange */
20188};
20189
20190/* Magic number for sanity checking on VdbeFrame objects */
20191#define SQLITE_FRAME_MAGIC0x879fb71e 0x879fb71e
20192
20193/*
20194** Return a pointer to the array of registers allocated for use
20195** by a VdbeFrame.
20196*/
20197#define VdbeFrameMem(p)((Mem *)&((u8 *)p)[(((sizeof(VdbeFrame))+7)&~7)]) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))(((sizeof(VdbeFrame))+7)&~7)])
20198
20199/*
20200** Internally, the vdbe manipulates nearly all SQL values as Mem
20201** structures. Each Mem struct may cache multiple representations (string,
20202** integer etc.) of the same value.
20203*/
20204struct sqlite3_value {
20205 union MemValue {
20206 double r; /* Real value used when MEM_Real is set in flags */
20207 i64 i; /* Integer value used when MEM_Int is set in flags */
20208 int nZero; /* Extra zero bytes when MEM_Zero and MEM_Blob set */
20209 const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
20210 FuncDef *pDef; /* Used only when flags==MEM_Agg */
20211 } u;
20212 u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
20213 u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
20214 u8 eSubtype; /* Subtype for this value */
20215 int n; /* Number of characters in string value, excluding '\0' */
20216 char *z; /* String or BLOB value */
20217 /* ShallowCopy only needs to copy the information above */
20218 char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
20219 int szMalloc; /* Size of the zMalloc allocation */
20220 u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */
20221 sqlite3 *db; /* The associated database connection */
20222 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
20223#ifdef SQLITE_DEBUG
20224 Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */
20225 u16 mScopyFlags; /* flags value immediately after the shallow copy */
20226#endif
20227};
20228
20229/*
20230** Size of struct Mem not including the Mem.zMalloc member or anything that
20231** follows.
20232*/
20233#define MEMCELLSIZE__builtin_offsetof(Mem, zMalloc) offsetof(Mem,zMalloc)__builtin_offsetof(Mem, zMalloc)
20234
20235/* One or more of the following flags are set to indicate the validOK
20236** representations of the value stored in the Mem struct.
20237**
20238** If the MEM_Null flag is set, then the value is an SQL NULL value.
20239** For a pointer type created using sqlite3_bind_pointer() or
20240** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
20241**
20242** If the MEM_Str flag is set then Mem.z points at a string representation.
20243** Usually this is encoded in the same unicode encoding as the main
20244** database (see below for exceptions). If the MEM_Term flag is also
20245** set, then the string is nul terminated. The MEM_Int and MEM_Real
20246** flags may coexist with the MEM_Str flag.
20247*/
20248#define MEM_Null0x0001 0x0001 /* Value is NULL (or a pointer) */
20249#define MEM_Str0x0002 0x0002 /* Value is a string */
20250#define MEM_Int0x0004 0x0004 /* Value is an integer */
20251#define MEM_Real0x0008 0x0008 /* Value is a real number */
20252#define MEM_Blob0x0010 0x0010 /* Value is a BLOB */
20253#define MEM_IntReal0x0020 0x0020 /* MEM_Int that stringifies like MEM_Real */
20254#define MEM_AffMask0x003f 0x003f /* Mask of affinity bits */
20255#define MEM_FromBind0x0040 0x0040 /* Value originates from sqlite3_bind() */
20256#define MEM_Undefined0x0080 0x0080 /* Value is undefined */
20257#define MEM_Cleared0x0100 0x0100 /* NULL set by OP_Null, not from data */
20258#define MEM_TypeMask0xc1bf 0xc1bf /* Mask of type bits */
20259
20260
20261/* Whenever Mem contains a valid string or blob representation, one of
20262** the following flags must be set to determine the memory management
20263** policy for Mem.z. The MEM_Term flag tells us whether or not the
20264** string is \000 or \u0000 terminated
20265*/
20266#define MEM_Term0x0200 0x0200 /* String in Mem.z is zero terminated */
20267#define MEM_Dyn0x0400 0x0400 /* Need to call Mem.xDel() on Mem.z */
20268#define MEM_Static0x0800 0x0800 /* Mem.z points to a static string */
20269#define MEM_Ephem0x1000 0x1000 /* Mem.z points to an ephemeral string */
20270#define MEM_Agg0x2000 0x2000 /* Mem.z points to an agg function context */
20271#define MEM_Zero0x4000 0x4000 /* Mem.i contains count of 0s appended to blob */
20272#define MEM_Subtype0x8000 0x8000 /* Mem.eSubtype is valid */
20273#ifdef SQLITE_OMIT_INCRBLOB
20274 #undef MEM_Zero0x4000
20275 #define MEM_Zero0x4000 0x0000
20276#endif
20277
20278/* Return TRUE if Mem X contains dynamically allocated content - anything
20279** that needs to be deallocated to avoid a leak.
20280*/
20281#define VdbeMemDynamic(X)(((X)->flags&(0x2000|0x0400))!=0) \
20282 (((X)->flags&(MEM_Agg0x2000|MEM_Dyn0x0400))!=0)
20283
20284/*
20285** Clear any existing type flags from a Mem and replace them with f
20286*/
20287#define MemSetTypeFlag(p, f)((p)->flags = ((p)->flags&~(0xc1bf|0x4000))|f) \
20288 ((p)->flags = ((p)->flags&~(MEM_TypeMask0xc1bf|MEM_Zero0x4000))|f)
20289
20290/*
20291** True if Mem X is a NULL-nochng type.
20292*/
20293#define MemNullNochng(X)((X)->flags==(0x0001|0x4000) && (X)->n==0 &&
(X)->u.nZero==0)
\
20294 ((X)->flags==(MEM_Null0x0001|MEM_Zero0x4000) && (X)->n==0 && (X)->u.nZero==0)
20295
20296/*
20297** Return true if a memory cell is not marked as invalid. This macro
20298** is for use inside assert() statements only.
20299*/
20300#ifdef SQLITE_DEBUG
20301#define memIsValid(M) ((M)->flags & MEM_Undefined0x0080)==0
20302#endif
20303
20304/*
20305** Each auxiliary data pointer stored by a user defined function
20306** implementation calling sqlite3_set_auxdata() is stored in an instance
20307** of this structure. All such structures associated with a single VM
20308** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
20309** when the VM is halted (if not before).
20310*/
20311struct AuxData {
20312 int iAuxOp; /* Instruction number of OP_Function opcode */
20313 int iAuxArg; /* Index of function argument. */
20314 void *pAux; /* Aux data pointer */
20315 void (*xDeleteAux)(void*); /* Destructor for the aux data */
20316 AuxData *pNextAux; /* Next element in list */
20317};
20318
20319/*
20320** The "context" argument for an installable function. A pointer to an
20321** instance of this structure is the first argument to the routines used
20322** implement the SQL functions.
20323**
20324** There is a typedef for this structure in sqlite.h. So all routines,
20325** even the public interface to SQLite, can use a pointer to this structure.
20326** But this file is the only place where the internal details of this
20327** structure are known.
20328**
20329** This structure is defined inside of vdbeInt.h because it uses substructures
20330** (Mem) which are only defined there.
20331*/
20332struct sqlite3_context {
20333 Mem *pOut; /* The return value is stored here */
20334 FuncDef *pFunc; /* Pointer to function information */
20335 Mem *pMem; /* Memory cell used to store aggregate context */
20336 Vdbe *pVdbe; /* The VM that owns this context */
20337 int iOp; /* Instruction number of OP_Function */
20338 int isError; /* Error code returned by the function. */
20339 u8 skipFlag; /* Skip accumulator loading if true */
20340 u8 argc; /* Number of arguments */
20341 sqlite3_value *argv[1]; /* Argument set */
20342};
20343
20344/* A bitfield type for use inside of structures. Always follow with :N where
20345** N is the number of bits.
20346*/
20347typedef unsigned bft; /* Bit Field Type */
20348
20349/* The ScanStatus object holds a single value for the
20350** sqlite3_stmt_scanstatus() interface.
20351*/
20352typedef struct ScanStatus ScanStatus;
20353struct ScanStatus {
20354 int addrExplain; /* OP_Explain for loop */
20355 int addrLoop; /* Address of "loops" counter */
20356 int addrVisit; /* Address of "rows visited" counter */
20357 int iSelectID; /* The "Select-ID" for this loop */
20358 LogEst nEst; /* Estimated output rows per loop */
20359 char *zName; /* Name of table or index */
20360};
20361
20362/* The DblquoteStr object holds the text of a double-quoted
20363** string for a prepared statement. A linked list of these objects
20364** is constructed during statement parsing and is held on Vdbe.pDblStr.
20365** When computing a normalized SQL statement for an SQL statement, that
20366** list is consulted for each double-quoted identifier to see if the
20367** identifier should really be a string literal.
20368*/
20369typedef struct DblquoteStr DblquoteStr;
20370struct DblquoteStr {
20371 DblquoteStr *pNextStr; /* Next string literal in the list */
20372 char z[8]; /* Dequoted value for the string */
20373};
20374
20375/*
20376** An instance of the virtual machine. This structure contains the complete
20377** state of the virtual machine.
20378**
20379** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
20380** is really a pointer to an instance of this structure.
20381*/
20382struct Vdbe {
20383 sqlite3 *db; /* The database connection that owns this statement */
20384 Vdbe *pPrev,*pNext; /* Linked list of VDBEs with the same Vdbe.db */
20385 Parse *pParse; /* Parsing context used to create this Vdbe */
20386 ynVar nVar; /* Number of entries in aVar[] */
20387 u32 magic; /* Magic number for sanity checking */
20388 int nMem; /* Number of memory locations currently allocated */
20389 int nCursor; /* Number of slots in apCsr[] */
20390 u32 cacheCtr; /* VdbeCursor row cache generation counter */
20391 int pc; /* The program counter */
20392 int rc; /* Value to return */
20393 int nChange; /* Number of db changes made since last reset */
20394 int iStatement; /* Statement number (or 0 if has no opened stmt) */
20395 i64 iCurrentTime; /* Value of julianday('now') for this statement */
20396 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
20397 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
20398 i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */
20399 Mem *aMem; /* The memory locations */
20400 Mem **apArg; /* Arguments to currently executing user function */
20401 VdbeCursor **apCsr; /* One element of this array for each open cursor */
20402 Mem *aVar; /* Values for the OP_Variable opcode. */
20403
20404 /* When allocating a new Vdbe object, all of the fields below should be
20405 ** initialized to zero or NULL */
20406
20407 Op *aOp; /* Space to hold the virtual machine's program */
20408 int nOp; /* Number of instructions in the program */
20409 int nOpAlloc; /* Slots allocated for aOp[] */
20410 Mem *aColName; /* Column names to return */
20411 Mem *pResultSet; /* Pointer to an array of results */
20412 char *zErrMsg; /* Error message written here */
20413 VList *pVList; /* Name of variables */
20414#ifndef SQLITE_OMIT_TRACE
20415 i64 startTime; /* Time when query started - used for profiling */
20416#endif
20417#ifdef SQLITE_DEBUG
20418 int rcApp; /* errcode set by sqlite3_result_error_code() */
20419 u32 nWrite; /* Number of write operations that have occurred */
20420#endif
20421 u16 nResColumn; /* Number of columns in one row of the result set */
20422 u8 errorAction; /* Recovery action to do in case of an error */
20423 u8 minWriteFileFormat; /* Minimum file format for writable database files */
20424 u8 prepFlags; /* SQLITE_PREPARE_* flags */
20425 bft expired:2; /* 1: recompile VM immediately 2: when convenient */
20426 bft explain:2; /* True if EXPLAIN present on SQL command */
20427 bft doingRerun:1; /* True if rerunning after an auto-reprepare */
20428 bft changeCntOn:1; /* True to update the change-counter */
20429 bft runOnlyOnce:1; /* Automatically expire on reset */
20430 bft usesStmtJournal:1; /* True if uses a statement journal */
20431 bft readOnly:1; /* True for statements that do not write */
20432 bft bIsReader:1; /* True for statements that read */
20433 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
20434 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
20435 u32 aCounter[7]; /* Counters used by sqlite3_stmt_status() */
20436 char *zSql; /* Text of the SQL statement that generated this */
20437#ifdef SQLITE_ENABLE_NORMALIZE
20438 char *zNormSql; /* Normalization of the associated SQL statement */
20439 DblquoteStr *pDblStr; /* List of double-quoted string literals */
20440#endif
20441 void *pFree; /* Free this when deleting the vdbe */
20442 VdbeFrame *pFrame; /* Parent frame */
20443 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
20444 int nFrame; /* Number of frames in pFrame list */
20445 u32 expmask; /* Binding to these vars invalidates VM */
20446 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
20447 AuxData *pAuxData; /* Linked list of auxdata allocations */
20448#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
20449 i64 *anExec; /* Number of times each op has been executed */
20450 int nScan; /* Entries in aScan[] */
20451 ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */
20452#endif
20453};
20454
20455/*
20456** The following are allowed values for Vdbe.magic
20457*/
20458#define VDBE_MAGIC_INIT0x16bceaa5 0x16bceaa5 /* Building a VDBE program */
20459#define VDBE_MAGIC_RUN0x2df20da3 0x2df20da3 /* VDBE is ready to execute */
20460#define VDBE_MAGIC_HALT0x319c2973 0x319c2973 /* VDBE has completed execution */
20461#define VDBE_MAGIC_RESET0x48fa9f76 0x48fa9f76 /* Reset and ready to run again */
20462#define VDBE_MAGIC_DEAD0x5606c3c8 0x5606c3c8 /* The VDBE has been deallocated */
20463
20464/*
20465** Structure used to store the context required by the
20466** sqlite3_preupdate_*() API functions.
20467*/
20468struct PreUpdate {
20469 Vdbe *v;
20470 VdbeCursor *pCsr; /* Cursor to read old values from */
20471 int op; /* One of SQLITE_INSERT, UPDATE, DELETE */
20472 u8 *aRecord; /* old.* database record */
20473 KeyInfo keyinfo;
20474 UnpackedRecord *pUnpacked; /* Unpacked version of aRecord[] */
20475 UnpackedRecord *pNewUnpacked; /* Unpacked version of new.* record */
20476 int iNewReg; /* Register for new.* values */
20477 i64 iKey1; /* First key value passed to hook */
20478 i64 iKey2; /* Second key value passed to hook */
20479 Mem *aNew; /* Array of new.* values */
20480 Table *pTab; /* Schema object being upated */
20481 Index *pPk; /* PK index if pTab is WITHOUT ROWID */
20482};
20483
20484/*
20485** Function prototypes
20486*/
20487SQLITE_PRIVATEstatic void sqlite3VdbeError(Vdbe*, const char *, ...);
20488SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
20489void sqliteVdbePopStack(Vdbe*,int);
20490SQLITE_PRIVATEstatic int sqlite3VdbeCursorMoveto(VdbeCursor**, int*);
20491SQLITE_PRIVATEstatic int sqlite3VdbeCursorRestore(VdbeCursor*);
20492SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialTypeLen(u32);
20493SQLITE_PRIVATEstatic u8 sqlite3VdbeOneByteSerialTypeLen(u8);
20494SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialType(Mem*, int, u32*);
20495SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
20496SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
20497SQLITE_PRIVATEstatic void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
20498
20499int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
20500SQLITE_PRIVATEstatic int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
20501SQLITE_PRIVATEstatic int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);
20502SQLITE_PRIVATEstatic int sqlite3VdbeExec(Vdbe*);
20503#ifndef SQLITE_OMIT_EXPLAIN
20504SQLITE_PRIVATEstatic int sqlite3VdbeList(Vdbe*);
20505#endif
20506SQLITE_PRIVATEstatic int sqlite3VdbeHalt(Vdbe*);
20507SQLITE_PRIVATEstatic int sqlite3VdbeChangeEncoding(Mem *, int);
20508SQLITE_PRIVATEstatic int sqlite3VdbeMemTooBig(Mem*);
20509SQLITE_PRIVATEstatic int sqlite3VdbeMemCopy(Mem*, const Mem*);
20510SQLITE_PRIVATEstatic void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
20511SQLITE_PRIVATEstatic void sqlite3VdbeMemMove(Mem*, Mem*);
20512SQLITE_PRIVATEstatic int sqlite3VdbeMemNulTerminate(Mem*);
20513SQLITE_PRIVATEstatic int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
20514SQLITE_PRIVATEstatic void sqlite3VdbeMemSetInt64(Mem*, i64);
20515#ifdef SQLITE_OMIT_FLOATING_POINT
20516# define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
20517#else
20518SQLITE_PRIVATEstatic void sqlite3VdbeMemSetDouble(Mem*, double);
20519#endif
20520SQLITE_PRIVATEstatic void sqlite3VdbeMemSetPointer(Mem*, void*, const char*, void(*)(void*));
20521SQLITE_PRIVATEstatic void sqlite3VdbeMemInit(Mem*,sqlite3*,u16);
20522SQLITE_PRIVATEstatic void sqlite3VdbeMemSetNull(Mem*);
20523SQLITE_PRIVATEstatic void sqlite3VdbeMemSetZeroBlob(Mem*,int);
20524#ifdef SQLITE_DEBUG
20525SQLITE_PRIVATEstatic int sqlite3VdbeMemIsRowSet(const Mem*);
20526#endif
20527SQLITE_PRIVATEstatic int sqlite3VdbeMemSetRowSet(Mem*);
20528SQLITE_PRIVATEstatic int sqlite3VdbeMemMakeWriteable(Mem*);
20529SQLITE_PRIVATEstatic int sqlite3VdbeMemStringify(Mem*, u8, u8);
20530SQLITE_PRIVATEstatic i64 sqlite3VdbeIntValue(Mem*);
20531SQLITE_PRIVATEstatic int sqlite3VdbeMemIntegerify(Mem*);
20532SQLITE_PRIVATEstatic double sqlite3VdbeRealValue(Mem*);
20533SQLITE_PRIVATEstatic int sqlite3VdbeBooleanValue(Mem*, int ifNull);
20534SQLITE_PRIVATEstatic void sqlite3VdbeIntegerAffinity(Mem*);
20535SQLITE_PRIVATEstatic int sqlite3VdbeMemRealify(Mem*);
20536SQLITE_PRIVATEstatic int sqlite3VdbeMemNumerify(Mem*);
20537SQLITE_PRIVATEstatic void sqlite3VdbeMemCast(Mem*,u8,u8);
20538SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
20539SQLITE_PRIVATEstatic void sqlite3VdbeMemRelease(Mem *p);
20540SQLITE_PRIVATEstatic int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
20541#ifndef SQLITE_OMIT_WINDOWFUNC
20542SQLITE_PRIVATEstatic int sqlite3VdbeMemAggValue(Mem*, Mem*, FuncDef*);
20543#endif
20544#ifndef SQLITE_OMIT_EXPLAIN
20545SQLITE_PRIVATEstatic const char *sqlite3OpcodeName(int);
20546#endif
20547SQLITE_PRIVATEstatic int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
20548SQLITE_PRIVATEstatic int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
20549SQLITE_PRIVATEstatic int sqlite3VdbeCloseStatement(Vdbe *, int);
20550#ifdef SQLITE_DEBUG
20551SQLITE_PRIVATEstatic int sqlite3VdbeFrameIsValid(VdbeFrame*);
20552#endif
20553SQLITE_PRIVATEstatic void sqlite3VdbeFrameMemDel(void*); /* Destructor on Mem */
20554SQLITE_PRIVATEstatic void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
20555SQLITE_PRIVATEstatic int sqlite3VdbeFrameRestore(VdbeFrame *);
20556#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
20557SQLITE_PRIVATEstatic void sqlite3VdbePreUpdateHook(Vdbe*,VdbeCursor*,int,const char*,Table*,i64,int);
20558#endif
20559SQLITE_PRIVATEstatic int sqlite3VdbeTransferError(Vdbe *p);
20560
20561SQLITE_PRIVATEstatic int sqlite3VdbeSorterInit(sqlite3 *, int, VdbeCursor *);
20562SQLITE_PRIVATEstatic void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
20563SQLITE_PRIVATEstatic void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
20564SQLITE_PRIVATEstatic int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
20565SQLITE_PRIVATEstatic int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
20566SQLITE_PRIVATEstatic int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
20567SQLITE_PRIVATEstatic int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
20568SQLITE_PRIVATEstatic int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
20569
20570#ifdef SQLITE_DEBUG
20571SQLITE_PRIVATEstatic void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
20572SQLITE_PRIVATEstatic void sqlite3VdbeAssertAbortable(Vdbe*);
20573#else
20574# define sqlite3VdbeIncrWriteCounter(V,C)
20575# define sqlite3VdbeAssertAbortable(V)
20576#endif
20577
20578#if !defined(SQLITE_OMIT_SHARED_CACHE)
20579SQLITE_PRIVATEstatic void sqlite3VdbeEnter(Vdbe*);
20580#else
20581# define sqlite3VdbeEnter(X)
20582#endif
20583
20584#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE1>0
20585SQLITE_PRIVATEstatic void sqlite3VdbeLeave(Vdbe*);
20586#else
20587# define sqlite3VdbeLeave(X)
20588#endif
20589
20590#ifdef SQLITE_DEBUG
20591SQLITE_PRIVATEstatic void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
20592SQLITE_PRIVATEstatic int sqlite3VdbeCheckMemInvariants(Mem*);
20593#endif
20594
20595#ifndef SQLITE_OMIT_FOREIGN_KEY
20596SQLITE_PRIVATEstatic int sqlite3VdbeCheckFk(Vdbe *, int);
20597#else
20598# define sqlite3VdbeCheckFk(p,i) 0
20599#endif
20600
20601#ifdef SQLITE_DEBUG
20602SQLITE_PRIVATEstatic void sqlite3VdbePrintSql(Vdbe*);
20603SQLITE_PRIVATEstatic void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
20604#endif
20605#ifndef SQLITE_OMIT_UTF16
20606SQLITE_PRIVATEstatic int sqlite3VdbeMemTranslate(Mem*, u8);
20607SQLITE_PRIVATEstatic int sqlite3VdbeMemHandleBom(Mem *pMem);
20608#endif
20609
20610#ifndef SQLITE_OMIT_INCRBLOB
20611SQLITE_PRIVATEstatic int sqlite3VdbeMemExpandBlob(Mem *);
20612 #define ExpandBlob(P)(((P)->flags&0x4000)?sqlite3VdbeMemExpandBlob(P):0) (((P)->flags&MEM_Zero0x4000)?sqlite3VdbeMemExpandBlob(P):0)
20613#else
20614 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK0
20615 #define ExpandBlob(P)(((P)->flags&0x4000)?sqlite3VdbeMemExpandBlob(P):0) SQLITE_OK0
20616#endif
20617
20618#endif /* !defined(SQLITE_VDBEINT_H) */
20619
20620/************** End of vdbeInt.h *********************************************/
20621/************** Continuing where we left off in status.c *********************/
20622
20623/*
20624** Variables in which to record status information.
20625*/
20626#if SQLITE_PTRSIZE8>4
20627typedef sqlite3_int64 sqlite3StatValueType;
20628#else
20629typedef u32 sqlite3StatValueType;
20630#endif
20631typedef struct sqlite3StatType sqlite3StatType;
20632static SQLITE_WSD struct sqlite3StatType {
20633 sqlite3StatValueType nowValue[10]; /* Current value */
20634 sqlite3StatValueType mxValue[10]; /* Maximum value */
20635} sqlite3Stat = { {0,}, {0,} };
20636
20637/*
20638** Elements of sqlite3Stat[] are protected by either the memory allocator
20639** mutex, or by the pcache1 mutex. The following array determines which.
20640*/
20641static const char statMutex[] = {
20642 0, /* SQLITE_STATUS_MEMORY_USED */
20643 1, /* SQLITE_STATUS_PAGECACHE_USED */
20644 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
20645 0, /* SQLITE_STATUS_SCRATCH_USED */
20646 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
20647 0, /* SQLITE_STATUS_MALLOC_SIZE */
20648 0, /* SQLITE_STATUS_PARSER_STACK */
20649 1, /* SQLITE_STATUS_PAGECACHE_SIZE */
20650 0, /* SQLITE_STATUS_SCRATCH_SIZE */
20651 0, /* SQLITE_STATUS_MALLOC_COUNT */
20652};
20653
20654
20655/* The "wsdStat" macro will resolve to the status information
20656** state vector. If writable static data is unsupported on the target,
20657** we have to locate the state vector at run-time. In the more common
20658** case where writable static data is supported, wsdStat can refer directly
20659** to the "sqlite3Stat" state vector declared above.
20660*/
20661#ifdef SQLITE_OMIT_WSD
20662# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)sqlite3Stat
20663# define wsdStatsqlite3Stat x[0]
20664#else
20665# define wsdStatInit
20666# define wsdStatsqlite3Stat sqlite3Stat
20667#endif
20668
20669/*
20670** Return the current value of a status parameter. The caller must
20671** be holding the appropriate mutex.
20672*/
20673SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StatusValue(int op){
20674 wsdStatInit;
20675 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
20676 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
20677 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
20678 : sqlite3MallocMutex()) )((void) (0));
20679 return wsdStatsqlite3Stat.nowValue[op];
20680}
20681
20682/*
20683** Add N to the value of a status record. The caller must hold the
20684** appropriate mutex. (Locking is checked by assert()).
20685**
20686** The StatusUp() routine can accept positive or negative values for N.
20687** The value of N is added to the current status value and the high-water
20688** mark is adjusted if necessary.
20689**
20690** The StatusDown() routine lowers the current value by N. The highwater
20691** mark is unchanged. N must be non-negative for StatusDown().
20692*/
20693SQLITE_PRIVATEstatic void sqlite3StatusUp(int op, int N){
20694 wsdStatInit;
20695 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
20696 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
20697 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
20698 : sqlite3MallocMutex()) )((void) (0));
20699 wsdStatsqlite3Stat.nowValue[op] += N;
20700 if( wsdStatsqlite3Stat.nowValue[op]>wsdStatsqlite3Stat.mxValue[op] ){
20701 wsdStatsqlite3Stat.mxValue[op] = wsdStatsqlite3Stat.nowValue[op];
20702 }
20703}
20704SQLITE_PRIVATEstatic void sqlite3StatusDown(int op, int N){
20705 wsdStatInit;
20706 assert( N>=0 )((void) (0));
20707 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
20708 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
20709 : sqlite3MallocMutex()) )((void) (0));
20710 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
20711 wsdStatsqlite3Stat.nowValue[op] -= N;
20712}
20713
20714/*
20715** Adjust the highwater mark if necessary.
20716** The caller must hold the appropriate mutex.
20717*/
20718SQLITE_PRIVATEstatic void sqlite3StatusHighwater(int op, int X){
20719 sqlite3StatValueType newValue;
20720 wsdStatInit;
20721 assert( X>=0 )((void) (0));
20722 newValue = (sqlite3StatValueType)X;
20723 assert( op>=0 && op<ArraySize(wsdStat.nowValue) )((void) (0));
20724 assert( op>=0 && op<ArraySize(statMutex) )((void) (0));
20725 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()((void) (0))
20726 : sqlite3MallocMutex()) )((void) (0));
20727 assert( op==SQLITE_STATUS_MALLOC_SIZE((void) (0))
20728 || op==SQLITE_STATUS_PAGECACHE_SIZE((void) (0))
20729 || op==SQLITE_STATUS_PARSER_STACK )((void) (0));
20730 if( newValue>wsdStatsqlite3Stat.mxValue[op] ){
20731 wsdStatsqlite3Stat.mxValue[op] = newValue;
20732 }
20733}
20734
20735/*
20736** Query status information.
20737*/
20738SQLITE_API int sqlite3_status64(
20739 int op,
20740 sqlite3_int64 *pCurrent,
20741 sqlite3_int64 *pHighwater,
20742 int resetFlag
20743){
20744 sqlite3_mutex *pMutex;
20745 wsdStatInit;
20746 if( op<0 || op>=ArraySize(wsdStat.nowValue)((int)(sizeof(sqlite3Stat.nowValue)/sizeof(sqlite3Stat.nowValue
[0])))
){
20747 return SQLITE_MISUSE_BKPTsqlite3MisuseError(20747);
20748 }
20749#ifdef SQLITE_ENABLE_API_ARMOR
20750 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(20750);
20751#endif
20752 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
20753 sqlite3_mutex_enter(pMutex);
20754 *pCurrent = wsdStatsqlite3Stat.nowValue[op];
20755 *pHighwater = wsdStatsqlite3Stat.mxValue[op];
20756 if( resetFlag ){
20757 wsdStatsqlite3Stat.mxValue[op] = wsdStatsqlite3Stat.nowValue[op];
20758 }
20759 sqlite3_mutex_leave(pMutex);
20760 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
20761 return SQLITE_OK0;
20762}
20763SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
20764 sqlite3_int64 iCur = 0, iHwtr = 0;
20765 int rc;
20766#ifdef SQLITE_ENABLE_API_ARMOR
20767 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(20767);
20768#endif
20769 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
20770 if( rc==0 ){
20771 *pCurrent = (int)iCur;
20772 *pHighwater = (int)iHwtr;
20773 }
20774 return rc;
20775}
20776
20777/*
20778** Return the number of LookasideSlot elements on the linked list
20779*/
20780static u32 countLookasideSlots(LookasideSlot *p){
20781 u32 cnt = 0;
20782 while( p ){
20783 p = p->pNext;
20784 cnt++;
20785 }
20786 return cnt;
20787}
20788
20789/*
20790** Count the number of slots of lookaside memory that are outstanding
20791*/
20792SQLITE_PRIVATEstatic int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
20793 u32 nInit = countLookasideSlots(db->lookaside.pInit);
20794 u32 nFree = countLookasideSlots(db->lookaside.pFree);
20795 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
20796 return db->lookaside.nSlot - (nInit+nFree);
20797}
20798
20799/*
20800** Query status information for a single database connection
20801*/
20802SQLITE_API int sqlite3_db_status(
20803 sqlite3 *db, /* The database connection whose status is desired */
20804 int op, /* Status verb */
20805 int *pCurrent, /* Write current value here */
20806 int *pHighwater, /* Write high-water mark here */
20807 int resetFlag /* Reset high-water mark if true */
20808){
20809 int rc = SQLITE_OK0; /* Return code */
20810#ifdef SQLITE_ENABLE_API_ARMOR
20811 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
20812 return SQLITE_MISUSE_BKPTsqlite3MisuseError(20812);
20813 }
20814#endif
20815 sqlite3_mutex_enter(db->mutex);
20816 switch( op ){
20817 case SQLITE_DBSTATUS_LOOKASIDE_USED0: {
20818 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
20819 if( resetFlag ){
20820 LookasideSlot *p = db->lookaside.pFree;
20821 if( p ){
20822 while( p->pNext ) p = p->pNext;
20823 p->pNext = db->lookaside.pInit;
20824 db->lookaside.pInit = db->lookaside.pFree;
20825 db->lookaside.pFree = 0;
20826 }
20827 }
20828 break;
20829 }
20830
20831 case SQLITE_DBSTATUS_LOOKASIDE_HIT4:
20832 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE5:
20833 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL6: {
20834 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
20835 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
20836 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
20837 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 )((void) (0));
20838 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 )((void) (0));
20839 *pCurrent = 0;
20840 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT4];
20841 if( resetFlag ){
20842 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT4] = 0;
20843 }
20844 break;
20845 }
20846
20847 /*
20848 ** Return an approximation for the amount of memory currently used
20849 ** by all pagers associated with the given database connection. The
20850 ** highwater mark is meaningless and is returned as zero.
20851 */
20852 case SQLITE_DBSTATUS_CACHE_USED_SHARED11:
20853 case SQLITE_DBSTATUS_CACHE_USED1: {
20854 int totalUsed = 0;
20855 int i;
20856 sqlite3BtreeEnterAll(db);
20857 for(i=0; i<db->nDb; i++){
20858 Btree *pBt = db->aDb[i].pBt;
20859 if( pBt ){
20860 Pager *pPager = sqlite3BtreePager(pBt);
20861 int nByte = sqlite3PagerMemUsed(pPager);
20862 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED11 ){
20863 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
20864 }
20865 totalUsed += nByte;
20866 }
20867 }
20868 sqlite3BtreeLeaveAll(db);
20869 *pCurrent = totalUsed;
20870 *pHighwater = 0;
20871 break;
20872 }
20873
20874 /*
20875 ** *pCurrent gets an accurate estimate of the amount of memory used
20876 ** to store the schema for all databases (main, temp, and any ATTACHed
20877 ** databases. *pHighwater is set to zero.
20878 */
20879 case SQLITE_DBSTATUS_SCHEMA_USED2: {
20880 int i; /* Used to iterate through schemas */
20881 int nByte = 0; /* Used to accumulate return value */
20882
20883 sqlite3BtreeEnterAll(db);
20884 db->pnBytesFreed = &nByte;
20885 for(i=0; i<db->nDb; i++){
20886 Schema *pSchema = db->aDb[i].pSchema;
20887 if( ALWAYS(pSchema!=0)(pSchema!=0) ){
20888 HashElem *p;
20889
20890 nByte += sqlite3GlobalConfigsqlite3Config.m.xRoundup(sizeof(HashElem)) * (
20891 pSchema->tblHash.count
20892 + pSchema->trigHash.count
20893 + pSchema->idxHash.count
20894 + pSchema->fkeyHash.count
20895 );
20896 nByte += sqlite3_msize(pSchema->tblHash.ht);
20897 nByte += sqlite3_msize(pSchema->trigHash.ht);
20898 nByte += sqlite3_msize(pSchema->idxHash.ht);
20899 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
20900
20901 for(p=sqliteHashFirst(&pSchema->trigHash)((&pSchema->trigHash)->first); p; p=sqliteHashNext(p)((p)->next)){
20902 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)((p)->data));
20903 }
20904 for(p=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); p; p=sqliteHashNext(p)((p)->next)){
20905 sqlite3DeleteTable(db, (Table *)sqliteHashData(p)((p)->data));
20906 }
20907 }
20908 }
20909 db->pnBytesFreed = 0;
20910 sqlite3BtreeLeaveAll(db);
20911
20912 *pHighwater = 0;
20913 *pCurrent = nByte;
20914 break;
20915 }
20916
20917 /*
20918 ** *pCurrent gets an accurate estimate of the amount of memory used
20919 ** to store all prepared statements.
20920 ** *pHighwater is set to zero.
20921 */
20922 case SQLITE_DBSTATUS_STMT_USED3: {
20923 struct Vdbe *pVdbe; /* Used to iterate through VMs */
20924 int nByte = 0; /* Used to accumulate return value */
20925
20926 db->pnBytesFreed = &nByte;
20927 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
20928 sqlite3VdbeClearObject(db, pVdbe);
20929 sqlite3DbFree(db, pVdbe);
20930 }
20931 db->pnBytesFreed = 0;
20932
20933 *pHighwater = 0; /* IMP: R-64479-57858 */
20934 *pCurrent = nByte;
20935
20936 break;
20937 }
20938
20939 /*
20940 ** Set *pCurrent to the total cache hits or misses encountered by all
20941 ** pagers the database handle is connected to. *pHighwater is always set
20942 ** to zero.
20943 */
20944 case SQLITE_DBSTATUS_CACHE_SPILL12:
20945 op = SQLITE_DBSTATUS_CACHE_WRITE9+1;
20946 /* Fall through into the next case */
20947 case SQLITE_DBSTATUS_CACHE_HIT7:
20948 case SQLITE_DBSTATUS_CACHE_MISS8:
20949 case SQLITE_DBSTATUS_CACHE_WRITE9:{
20950 int i;
20951 int nRet = 0;
20952 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 )((void) (0));
20953 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 )((void) (0));
20954
20955 for(i=0; i<db->nDb; i++){
20956 if( db->aDb[i].pBt ){
20957 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
20958 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
20959 }
20960 }
20961 *pHighwater = 0; /* IMP: R-42420-56072 */
20962 /* IMP: R-54100-20147 */
20963 /* IMP: R-29431-39229 */
20964 *pCurrent = nRet;
20965 break;
20966 }
20967
20968 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
20969 ** key constraints. Set *pCurrent to zero if all foreign key constraints
20970 ** have been satisfied. The *pHighwater is always set to zero.
20971 */
20972 case SQLITE_DBSTATUS_DEFERRED_FKS10: {
20973 *pHighwater = 0; /* IMP: R-11967-56545 */
20974 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
20975 break;
20976 }
20977
20978 default: {
20979 rc = SQLITE_ERROR1;
20980 }
20981 }
20982 sqlite3_mutex_leave(db->mutex);
20983 return rc;
20984}
20985
20986/************** End of status.c **********************************************/
20987/************** Begin file date.c ********************************************/
20988/*
20989** 2003 October 31
20990**
20991** The author disclaims copyright to this source code. In place of
20992** a legal notice, here is a blessing:
20993**
20994** May you do good and not evil.
20995** May you find forgiveness for yourself and forgive others.
20996** May you share freely, never taking more than you give.
20997**
20998*************************************************************************
20999** This file contains the C functions that implement date and time
21000** functions for SQLite.
21001**
21002** There is only one exported symbol in this file - the function
21003** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
21004** All other code has file scope.
21005**
21006** SQLite processes all times and dates as julian day numbers. The
21007** dates and times are stored as the number of days since noon
21008** in Greenwich on November 24, 4714 B.C. according to the Gregorian
21009** calendar system.
21010**
21011** 1970-01-01 00:00:00 is JD 2440587.5
21012** 2000-01-01 00:00:00 is JD 2451544.5
21013**
21014** This implementation requires years to be expressed as a 4-digit number
21015** which means that only dates between 0000-01-01 and 9999-12-31 can
21016** be represented, even though julian day numbers allow a much wider
21017** range of dates.
21018**
21019** The Gregorian calendar system is used for all dates and times,
21020** even those that predate the Gregorian calendar. Historians usually
21021** use the julian calendar for dates prior to 1582-10-15 and for some
21022** dates afterwards, depending on locale. Beware of this difference.
21023**
21024** The conversion algorithms are implemented based on descriptions
21025** in the following text:
21026**
21027** Jean Meeus
21028** Astronomical Algorithms, 2nd Edition, 1998
21029** ISBN 0-943396-61-1
21030** Willmann-Bell, Inc
21031** Richmond, Virginia (USA)
21032*/
21033/* #include "sqliteInt.h" */
21034/* #include <stdlib.h> */
21035/* #include <assert.h> */
21036#include <time.h>
21037
21038#ifndef SQLITE_OMIT_DATETIME_FUNCS
21039
21040/*
21041** The MSVC CRT on Windows CE may not have a localtime() function.
21042** So declare a substitute. The substitute function itself is
21043** defined in "os_win.c".
21044*/
21045#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
21046 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
21047struct tm *__cdecl localtime(const time_t *);
21048#endif
21049
21050/*
21051** A structure for holding a single date and time.
21052*/
21053typedef struct DateTime DateTime;
21054struct DateTime {
21055 sqlite3_int64 iJD; /* The julian day number times 86400000 */
21056 int Y, M, D; /* Year, month, and day */
21057 int h, m; /* Hour and minutes */
21058 int tz; /* Timezone offset in minutes */
21059 double s; /* Seconds */
21060 char validJD; /* True (1) if iJD is valid */
21061 char rawS; /* Raw numeric value stored in s */
21062 char validYMD; /* True (1) if Y,M,D are valid */
21063 char validHMS; /* True (1) if h,m,s are valid */
21064 char validTZ; /* True (1) if tz is valid */
21065 char tzSet; /* Timezone was set explicitly */
21066 char isError; /* An overflow has occurred */
21067};
21068
21069
21070/*
21071** Convert zDate into one or more integers according to the conversion
21072** specifier zFormat.
21073**
21074** zFormat[] contains 4 characters for each integer converted, except for
21075** the last integer which is specified by three characters. The meaning
21076** of a four-character format specifiers ABCD is:
21077**
21078** A: number of digits to convert. Always "2" or "4".
21079** B: minimum value. Always "0" or "1".
21080** C: maximum value, decoded as:
21081** a: 12
21082** b: 14
21083** c: 24
21084** d: 31
21085** e: 59
21086** f: 9999
21087** D: the separator character, or \000 to indicate this is the
21088** last number to convert.
21089**
21090** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
21091** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
21092** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
21093** the 2-digit day which is the last integer in the set.
21094**
21095** The function returns the number of successful conversions.
21096*/
21097static int getDigits(const char *zDate, const char *zFormat, ...){
21098 /* The aMx[] array translates the 3rd character of each format
21099 ** spec into a max size: a b c d e f */
21100 static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
21101 va_list ap;
21102 int cnt = 0;
21103 char nextC;
21104 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
21105 do{
21106 char N = zFormat[0] - '0';
21107 char min = zFormat[1] - '0';
21108 int val = 0;
21109 u16 max;
21110
21111 assert( zFormat[2]>='a' && zFormat[2]<='f' )((void) (0));
21112 max = aMx[zFormat[2] - 'a'];
21113 nextC = zFormat[3];
21114 val = 0;
21115 while( N-- ){
21116 if( !sqlite3Isdigit(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x04) ){
21117 goto end_getDigits;
21118 }
21119 val = val*10 + *zDate - '0';
21120 zDate++;
21121 }
21122 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
21123 goto end_getDigits;
21124 }
21125 *va_arg(ap,int*)__builtin_va_arg(ap, int*) = val;
21126 zDate++;
21127 cnt++;
21128 zFormat += 4;
21129 }while( nextC );
21130end_getDigits:
21131 va_end(ap)__builtin_va_end(ap);
21132 return cnt;
21133}
21134
21135/*
21136** Parse a timezone extension on the end of a date-time.
21137** The extension is of the form:
21138**
21139** (+/-)HH:MM
21140**
21141** Or the "zulu" notation:
21142**
21143** Z
21144**
21145** If the parse is successful, write the number of minutes
21146** of change in p->tz and return 0. If a parser error occurs,
21147** return non-zero.
21148**
21149** A missing specifier is not considered an error.
21150*/
21151static int parseTimezone(const char *zDate, DateTime *p){
21152 int sgn = 0;
21153 int nHr, nMn;
21154 int c;
21155 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) ){ zDate++; }
21156 p->tz = 0;
21157 c = *zDate;
21158 if( c=='-' ){
21159 sgn = -1;
21160 }else if( c=='+' ){
21161 sgn = +1;
21162 }else if( c=='Z' || c=='z' ){
21163 zDate++;
21164 goto zulu_time;
21165 }else{
21166 return c!=0;
21167 }
21168 zDate++;
21169 if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
21170 return 1;
21171 }
21172 zDate += 5;
21173 p->tz = sgn*(nMn + nHr*60);
21174zulu_time:
21175 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) ){ zDate++; }
21176 p->tzSet = 1;
21177 return *zDate!=0;
21178}
21179
21180/*
21181** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
21182** The HH, MM, and SS must each be exactly 2 digits. The
21183** fractional seconds FFFF can be one or more digits.
21184**
21185** Return 1 if there is a parsing error and 0 on success.
21186*/
21187static int parseHhMmSs(const char *zDate, DateTime *p){
21188 int h, m, s;
21189 double ms = 0.0;
21190 if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
21191 return 1;
21192 }
21193 zDate += 5;
21194 if( *zDate==':' ){
21195 zDate++;
21196 if( getDigits(zDate, "20e", &s)!=1 ){
21197 return 1;
21198 }
21199 zDate += 2;
21200 if( *zDate=='.' && sqlite3Isdigit(zDate[1])(sqlite3CtypeMap[(unsigned char)(zDate[1])]&0x04) ){
21201 double rScale = 1.0;
21202 zDate++;
21203 while( sqlite3Isdigit(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x04) ){
21204 ms = ms*10.0 + *zDate - '0';
21205 rScale *= 10.0;
21206 zDate++;
21207 }
21208 ms /= rScale;
21209 }
21210 }else{
21211 s = 0;
21212 }
21213 p->validJD = 0;
21214 p->rawS = 0;
21215 p->validHMS = 1;
21216 p->h = h;
21217 p->m = m;
21218 p->s = s + ms;
21219 if( parseTimezone(zDate, p) ) return 1;
21220 p->validTZ = (p->tz!=0)?1:0;
21221 return 0;
21222}
21223
21224/*
21225** Put the DateTime object into its error state.
21226*/
21227static void datetimeError(DateTime *p){
21228 memset(p, 0, sizeof(*p));
21229 p->isError = 1;
21230}
21231
21232/*
21233** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
21234** that the YYYY-MM-DD is according to the Gregorian calendar.
21235**
21236** Reference: Meeus page 61
21237*/
21238static void computeJD(DateTime *p){
21239 int Y, M, D, A, B, X1, X2;
21240
21241 if( p->validJD ) return;
21242 if( p->validYMD ){
21243 Y = p->Y;
21244 M = p->M;
21245 D = p->D;
21246 }else{
21247 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
21248 M = 1;
21249 D = 1;
21250 }
21251 if( Y<-4713 || Y>9999 || p->rawS ){
21252 datetimeError(p);
21253 return;
21254 }
21255 if( M<=2 ){
21256 Y--;
21257 M += 12;
21258 }
21259 A = Y/100;
21260 B = 2 - A + (A/4);
21261 X1 = 36525*(Y+4716)/100;
21262 X2 = 306001*(M+1)/10000;
21263 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
21264 p->validJD = 1;
21265 if( p->validHMS ){
21266 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
21267 if( p->validTZ ){
21268 p->iJD -= p->tz*60000;
21269 p->validYMD = 0;
21270 p->validHMS = 0;
21271 p->validTZ = 0;
21272 }
21273 }
21274}
21275
21276/*
21277** Parse dates of the form
21278**
21279** YYYY-MM-DD HH:MM:SS.FFF
21280** YYYY-MM-DD HH:MM:SS
21281** YYYY-MM-DD HH:MM
21282** YYYY-MM-DD
21283**
21284** Write the result into the DateTime structure and return 0
21285** on success and 1 if the input string is not a well-formed
21286** date.
21287*/
21288static int parseYyyyMmDd(const char *zDate, DateTime *p){
21289 int Y, M, D, neg;
21290
21291 if( zDate[0]=='-' ){
21292 zDate++;
21293 neg = 1;
21294 }else{
21295 neg = 0;
21296 }
21297 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
21298 return 1;
21299 }
21300 zDate += 10;
21301 while( sqlite3Isspace(*zDate)(sqlite3CtypeMap[(unsigned char)(*zDate)]&0x01) || 'T'==*(u8*)zDate ){ zDate++; }
21302 if( parseHhMmSs(zDate, p)==0 ){
21303 /* We got the time */
21304 }else if( *zDate==0 ){
21305 p->validHMS = 0;
21306 }else{
21307 return 1;
21308 }
21309 p->validJD = 0;
21310 p->validYMD = 1;
21311 p->Y = neg ? -Y : Y;
21312 p->M = M;
21313 p->D = D;
21314 if( p->validTZ ){
21315 computeJD(p);
21316 }
21317 return 0;
21318}
21319
21320/*
21321** Set the time to the current time reported by the VFS.
21322**
21323** Return the number of errors.
21324*/
21325static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
21326 p->iJD = sqlite3StmtCurrentTime(context);
21327 if( p->iJD>0 ){
21328 p->validJD = 1;
21329 return 0;
21330 }else{
21331 return 1;
21332 }
21333}
21334
21335/*
21336** Input "r" is a numeric quantity which might be a julian day number,
21337** or the number of seconds since 1970. If the value if r is within
21338** range of a julian day number, install it as such and set validJD.
21339** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
21340*/
21341static void setRawDateNumber(DateTime *p, double r){
21342 p->s = r;
21343 p->rawS = 1;
21344 if( r>=0.0 && r<5373484.5 ){
21345 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
21346 p->validJD = 1;
21347 }
21348}
21349
21350/*
21351** Attempt to parse the given string into a julian day number. Return
21352** the number of errors.
21353**
21354** The following are acceptable forms for the input string:
21355**
21356** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
21357** DDDD.DD
21358** now
21359**
21360** In the first form, the +/-HH:MM is always optional. The fractional
21361** seconds extension (the ".FFF") is optional. The seconds portion
21362** (":SS.FFF") is option. The year and date can be omitted as long
21363** as there is a time string. The time string can be omitted as long
21364** as there is a year and date.
21365*/
21366static int parseDateOrTime(
21367 sqlite3_context *context,
21368 const char *zDate,
21369 DateTime *p
21370){
21371 double r;
21372 if( parseYyyyMmDd(zDate,p)==0 ){
21373 return 0;
21374 }else if( parseHhMmSs(zDate, p)==0 ){
21375 return 0;
21376 }else if( sqlite3StrICmp(zDate,"now")==0 && sqlite3NotPureFunc(context) ){
21377 return setDateTimeToCurrent(context, p);
21378 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF81)>0 ){
21379 setRawDateNumber(p, r);
21380 return 0;
21381 }
21382 return 1;
21383}
21384
21385/* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
21386** Multiplying this by 86400000 gives 464269060799999 as the maximum value
21387** for DateTime.iJD.
21388**
21389** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
21390** such a large integer literal, so we have to encode it.
21391*/
21392#define INT_464269060799999((((i64)0x1a640)<<32)|0x1072fdff) ((((i64)0x1a640)<<32)|0x1072fdff)
21393
21394/*
21395** Return TRUE if the given julian day number is within range.
21396**
21397** The input is the JulianDay times 86400000.
21398*/
21399static int validJulianDay(sqlite3_int64 iJD){
21400 return iJD>=0 && iJD<=INT_464269060799999((((i64)0x1a640)<<32)|0x1072fdff);
21401}
21402
21403/*
21404** Compute the Year, Month, and Day from the julian day number.
21405*/
21406static void computeYMD(DateTime *p){
21407 int Z, A, B, C, D, E, X1;
21408 if( p->validYMD ) return;
21409 if( !p->validJD ){
21410 p->Y = 2000;
21411 p->M = 1;
21412 p->D = 1;
21413 }else if( !validJulianDay(p->iJD) ){
21414 datetimeError(p);
21415 return;
21416 }else{
21417 Z = (int)((p->iJD + 43200000)/86400000);
21418 A = (int)((Z - 1867216.25)/36524.25);
21419 A = Z + 1 + A - (A/4);
21420 B = A + 1524;
21421 C = (int)((B - 122.1)/365.25);
21422 D = (36525*(C&32767))/100;
21423 E = (int)((B-D)/30.6001);
21424 X1 = (int)(30.6001*E);
21425 p->D = B - D - X1;
21426 p->M = E<14 ? E-1 : E-13;
21427 p->Y = p->M>2 ? C - 4716 : C - 4715;
21428 }
21429 p->validYMD = 1;
21430}
21431
21432/*
21433** Compute the Hour, Minute, and Seconds from the julian day number.
21434*/
21435static void computeHMS(DateTime *p){
21436 int s;
21437 if( p->validHMS ) return;
21438 computeJD(p);
21439 s = (int)((p->iJD + 43200000) % 86400000);
21440 p->s = s/1000.0;
21441 s = (int)p->s;
21442 p->s -= s;
21443 p->h = s/3600;
21444 s -= p->h*3600;
21445 p->m = s/60;
21446 p->s += s - p->m*60;
21447 p->rawS = 0;
21448 p->validHMS = 1;
21449}
21450
21451/*
21452** Compute both YMD and HMS
21453*/
21454static void computeYMD_HMS(DateTime *p){
21455 computeYMD(p);
21456 computeHMS(p);
21457}
21458
21459/*
21460** Clear the YMD and HMS and the TZ
21461*/
21462static void clearYMD_HMS_TZ(DateTime *p){
21463 p->validYMD = 0;
21464 p->validHMS = 0;
21465 p->validTZ = 0;
21466}
21467
21468#ifndef SQLITE_OMIT_LOCALTIME
21469/*
21470** On recent Windows platforms, the localtime_s() function is available
21471** as part of the "Secure CRT". It is essentially equivalent to
21472** localtime_r() available under most POSIX platforms, except that the
21473** order of the parameters is reversed.
21474**
21475** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
21476**
21477** If the user has not indicated to use localtime_r() or localtime_s()
21478** already, check for an MSVC build environment that provides
21479** localtime_s().
21480*/
21481#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
21482 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
21483#undef HAVE_LOCALTIME_S
21484#define HAVE_LOCALTIME_S 1
21485#endif
21486
21487/*
21488** The following routine implements the rough equivalent of localtime_r()
21489** using whatever operating-system specific localtime facility that
21490** is available. This routine returns 0 on success and
21491** non-zero on any kind of error.
21492**
21493** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
21494** routine will always fail.
21495**
21496** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
21497** library function localtime_r() is used to assist in the calculation of
21498** local time.
21499*/
21500static int osLocaltime(time_t *t, struct tm *pTm){
21501 int rc;
21502#if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
21503 struct tm *pX;
21504#if SQLITE_THREADSAFE1>0
21505 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2);
21506#endif
21507 sqlite3_mutex_enter(mutex);
21508 pX = localtime(t);
21509#ifndef SQLITE_UNTESTABLE
21510 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault ) pX = 0;
21511#endif
21512 if( pX ) *pTm = *pX;
21513 sqlite3_mutex_leave(mutex);
21514 rc = pX==0;
21515#else
21516#ifndef SQLITE_UNTESTABLE
21517 if( sqlite3GlobalConfigsqlite3Config.bLocaltimeFault ) return 1;
21518#endif
21519#if HAVE_LOCALTIME_R
21520 rc = localtime_r(t, pTm)==0;
21521#else
21522 rc = localtime_s(pTm, t);
21523#endif /* HAVE_LOCALTIME_R */
21524#endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
21525 return rc;
21526}
21527#endif /* SQLITE_OMIT_LOCALTIME */
21528
21529
21530#ifndef SQLITE_OMIT_LOCALTIME
21531/*
21532** Compute the difference (in milliseconds) between localtime and UTC
21533** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
21534** return this value and set *pRc to SQLITE_OK.
21535**
21536** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
21537** is undefined in this case.
21538*/
21539static sqlite3_int64 localtimeOffset(
21540 DateTime *p, /* Date at which to calculate offset */
21541 sqlite3_context *pCtx, /* Write error here if one occurs */
21542 int *pRc /* OUT: Error code. SQLITE_OK or ERROR */
21543){
21544 DateTime x, y;
21545 time_t t;
21546 struct tm sLocal;
21547
21548 /* Initialize the contents of sLocal to avoid a compiler warning. */
21549 memset(&sLocal, 0, sizeof(sLocal));
21550
21551 x = *p;
21552 computeYMD_HMS(&x);
21553 if( x.Y<1971 || x.Y>=2038 ){
21554 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
21555 ** works for years between 1970 and 2037. For dates outside this range,
21556 ** SQLite attempts to map the year into an equivalent year within this
21557 ** range, do the calculation, then map the year back.
21558 */
21559 x.Y = 2000;
21560 x.M = 1;
21561 x.D = 1;
21562 x.h = 0;
21563 x.m = 0;
21564 x.s = 0.0;
21565 } else {
21566 int s = (int)(x.s + 0.5);
21567 x.s = s;
21568 }
21569 x.tz = 0;
21570 x.validJD = 0;
21571 computeJD(&x);
21572 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
21573 if( osLocaltime(&t, &sLocal) ){
21574 sqlite3_result_error(pCtx, "local time unavailable", -1);
21575 *pRc = SQLITE_ERROR1;
21576 return 0;
21577 }
21578 y.Y = sLocal.tm_year + 1900;
21579 y.M = sLocal.tm_mon + 1;
21580 y.D = sLocal.tm_mday;
21581 y.h = sLocal.tm_hour;
21582 y.m = sLocal.tm_min;
21583 y.s = sLocal.tm_sec;
21584 y.validYMD = 1;
21585 y.validHMS = 1;
21586 y.validJD = 0;
21587 y.rawS = 0;
21588 y.validTZ = 0;
21589 y.isError = 0;
21590 computeJD(&y);
21591 *pRc = SQLITE_OK0;
21592 return y.iJD - x.iJD;
21593}
21594#endif /* SQLITE_OMIT_LOCALTIME */
21595
21596/*
21597** The following table defines various date transformations of the form
21598**
21599** 'NNN days'
21600**
21601** Where NNN is an arbitrary floating-point number and "days" can be one
21602** of several units of time.
21603*/
21604static const struct {
21605 u8 eType; /* Transformation type code */
21606 u8 nName; /* Length of th name */
21607 char *zName; /* Name of the transformation */
21608 double rLimit; /* Maximum NNN value for this transform */
21609 double rXform; /* Constant used for this transform */
21610} aXformType[] = {
21611 { 0, 6, "second", 464269060800.0, 86400000.0/(24.0*60.0*60.0) },
21612 { 0, 6, "minute", 7737817680.0, 86400000.0/(24.0*60.0) },
21613 { 0, 4, "hour", 128963628.0, 86400000.0/24.0 },
21614 { 0, 3, "day", 5373485.0, 86400000.0 },
21615 { 1, 5, "month", 176546.0, 30.0*86400000.0 },
21616 { 2, 4, "year", 14713.0, 365.0*86400000.0 },
21617};
21618
21619/*
21620** Process a modifier to a date-time stamp. The modifiers are
21621** as follows:
21622**
21623** NNN days
21624** NNN hours
21625** NNN minutes
21626** NNN.NNNN seconds
21627** NNN months
21628** NNN years
21629** start of month
21630** start of year
21631** start of week
21632** start of day
21633** weekday N
21634** unixepoch
21635** localtime
21636** utc
21637**
21638** Return 0 on success and 1 if there is any kind of error. If the error
21639** is in a system call (i.e. localtime()), then an error message is written
21640** to context pCtx. If the error is an unrecognized modifier, no error is
21641** written to pCtx.
21642*/
21643static int parseModifier(
21644 sqlite3_context *pCtx, /* Function context */
21645 const char *z, /* The text of the modifier */
21646 int n, /* Length of zMod in bytes */
21647 DateTime *p /* The date/time value to be modified */
21648){
21649 int rc = 1;
21650 double r;
21651 switch(sqlite3UpperToLower[(u8)z[0]] ){
21652#ifndef SQLITE_OMIT_LOCALTIME
21653 case 'l': {
21654 /* localtime
21655 **
21656 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
21657 ** show local time.
21658 */
21659 if( sqlite3_stricmp(z, "localtime")==0 && sqlite3NotPureFunc(pCtx) ){
21660 computeJD(p);
21661 p->iJD += localtimeOffset(p, pCtx, &rc);
21662 clearYMD_HMS_TZ(p);
21663 }
21664 break;
21665 }
21666#endif
21667 case 'u': {
21668 /*
21669 ** unixepoch
21670 **
21671 ** Treat the current value of p->s as the number of
21672 ** seconds since 1970. Convert to a real julian day number.
21673 */
21674 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
21675 r = p->s*1000.0 + 210866760000000.0;
21676 if( r>=0.0 && r<464269060800000.0 ){
21677 clearYMD_HMS_TZ(p);
21678 p->iJD = (sqlite3_int64)r;
21679 p->validJD = 1;
21680 p->rawS = 0;
21681 rc = 0;
21682 }
21683 }
21684#ifndef SQLITE_OMIT_LOCALTIME
21685 else if( sqlite3_stricmp(z, "utc")==0 && sqlite3NotPureFunc(pCtx) ){
21686 if( p->tzSet==0 ){
21687 sqlite3_int64 c1;
21688 computeJD(p);
21689 c1 = localtimeOffset(p, pCtx, &rc);
21690 if( rc==SQLITE_OK0 ){
21691 p->iJD -= c1;
21692 clearYMD_HMS_TZ(p);
21693 p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
21694 }
21695 p->tzSet = 1;
21696 }else{
21697 rc = SQLITE_OK0;
21698 }
21699 }
21700#endif
21701 break;
21702 }
21703 case 'w': {
21704 /*
21705 ** weekday N
21706 **
21707 ** Move the date to the same time on the next occurrence of
21708 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
21709 ** date is already on the appropriate weekday, this is a no-op.
21710 */
21711 if( sqlite3_strnicmp(z, "weekday ", 8)==0
21712 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF81)>0
21713 && (n=(int)r)==r && n>=0 && r<7 ){
21714 sqlite3_int64 Z;
21715 computeYMD_HMS(p);
21716 p->validTZ = 0;
21717 p->validJD = 0;
21718 computeJD(p);
21719 Z = ((p->iJD + 129600000)/86400000) % 7;
21720 if( Z>n ) Z -= 7;
21721 p->iJD += (n - Z)*86400000;
21722 clearYMD_HMS_TZ(p);
21723 rc = 0;
21724 }
21725 break;
21726 }
21727 case 's': {
21728 /*
21729 ** start of TTTTT
21730 **
21731 ** Move the date backwards to the beginning of the current day,
21732 ** or month or year.
21733 */
21734 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
21735 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
21736 z += 9;
21737 computeYMD(p);
21738 p->validHMS = 1;
21739 p->h = p->m = 0;
21740 p->s = 0.0;
21741 p->rawS = 0;
21742 p->validTZ = 0;
21743 p->validJD = 0;
21744 if( sqlite3_stricmp(z,"month")==0 ){
21745 p->D = 1;
21746 rc = 0;
21747 }else if( sqlite3_stricmp(z,"year")==0 ){
21748 p->M = 1;
21749 p->D = 1;
21750 rc = 0;
21751 }else if( sqlite3_stricmp(z,"day")==0 ){
21752 rc = 0;
21753 }
21754 break;
21755 }
21756 case '+':
21757 case '-':
21758 case '0':
21759 case '1':
21760 case '2':
21761 case '3':
21762 case '4':
21763 case '5':
21764 case '6':
21765 case '7':
21766 case '8':
21767 case '9': {
21768 double rRounder;
21769 int i;
21770 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n])(sqlite3CtypeMap[(unsigned char)(z[n])]&0x01); n++){}
21771 if( sqlite3AtoF(z, &r, n, SQLITE_UTF81)<=0 ){
21772 rc = 1;
21773 break;
21774 }
21775 if( z[n]==':' ){
21776 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
21777 ** specified number of hours, minutes, seconds, and fractional seconds
21778 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
21779 ** omitted.
21780 */
21781 const char *z2 = z;
21782 DateTime tx;
21783 sqlite3_int64 day;
21784 if( !sqlite3Isdigit(*z2)(sqlite3CtypeMap[(unsigned char)(*z2)]&0x04) ) z2++;
21785 memset(&tx, 0, sizeof(tx));
21786 if( parseHhMmSs(z2, &tx) ) break;
21787 computeJD(&tx);
21788 tx.iJD -= 43200000;
21789 day = tx.iJD/86400000;
21790 tx.iJD -= day*86400000;
21791 if( z[0]=='-' ) tx.iJD = -tx.iJD;
21792 computeJD(p);
21793 clearYMD_HMS_TZ(p);
21794 p->iJD += tx.iJD;
21795 rc = 0;
21796 break;
21797 }
21798
21799 /* If control reaches this point, it means the transformation is
21800 ** one of the forms like "+NNN days". */
21801 z += n;
21802 while( sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z++;
21803 n = sqlite3Strlen30(z);
21804 if( n>10 || n<3 ) break;
21805 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
21806 computeJD(p);
21807 rc = 1;
21808 rRounder = r<0 ? -0.5 : +0.5;
21809 for(i=0; i<ArraySize(aXformType)((int)(sizeof(aXformType)/sizeof(aXformType[0]))); i++){
21810 if( aXformType[i].nName==n
21811 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
21812 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
21813 ){
21814 switch( aXformType[i].eType ){
21815 case 1: { /* Special processing to add months */
21816 int x;
21817 computeYMD_HMS(p);
21818 p->M += (int)r;
21819 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
21820 p->Y += x;
21821 p->M -= x*12;
21822 p->validJD = 0;
21823 r -= (int)r;
21824 break;
21825 }
21826 case 2: { /* Special processing to add years */
21827 int y = (int)r;
21828 computeYMD_HMS(p);
21829 p->Y += y;
21830 p->validJD = 0;
21831 r -= (int)r;
21832 break;
21833 }
21834 }
21835 computeJD(p);
21836 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
21837 rc = 0;
21838 break;
21839 }
21840 }
21841 clearYMD_HMS_TZ(p);
21842 break;
21843 }
21844 default: {
21845 break;
21846 }
21847 }
21848 return rc;
21849}
21850
21851/*
21852** Process time function arguments. argv[0] is a date-time stamp.
21853** argv[1] and following are modifiers. Parse them all and write
21854** the resulting time into the DateTime structure p. Return 0
21855** on success and 1 if there are any errors.
21856**
21857** If there are zero parameters (if even argv[0] is undefined)
21858** then assume a default value of "now" for argv[0].
21859*/
21860static int isDate(
21861 sqlite3_context *context,
21862 int argc,
21863 sqlite3_value **argv,
21864 DateTime *p
21865){
21866 int i, n;
21867 const unsigned char *z;
21868 int eType;
21869 memset(p, 0, sizeof(*p));
21870 if( argc==0 ){
21871 return setDateTimeToCurrent(context, p);
21872 }
21873 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT2
21874 || eType==SQLITE_INTEGER1 ){
21875 setRawDateNumber(p, sqlite3_value_double(argv[0]));
21876 }else{
21877 z = sqlite3_value_text(argv[0]);
21878 if( !z || parseDateOrTime(context, (char*)z, p) ){
21879 return 1;
21880 }
21881 }
21882 for(i=1; i<argc; i++){
21883 z = sqlite3_value_text(argv[i]);
21884 n = sqlite3_value_bytes(argv[i]);
21885 if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1;
21886 }
21887 computeJD(p);
21888 if( p->isError || !validJulianDay(p->iJD) ) return 1;
21889 return 0;
21890}
21891
21892
21893/*
21894** The following routines implement the various date and time functions
21895** of SQLite.
21896*/
21897
21898/*
21899** julianday( TIMESTRING, MOD, MOD, ...)
21900**
21901** Return the julian day number of the date specified in the arguments
21902*/
21903static void juliandayFunc(
21904 sqlite3_context *context,
21905 int argc,
21906 sqlite3_value **argv
21907){
21908 DateTime x;
21909 if( isDate(context, argc, argv, &x)==0 ){
21910 computeJD(&x);
21911 sqlite3_result_double(context, x.iJD/86400000.0);
21912 }
21913}
21914
21915/*
21916** datetime( TIMESTRING, MOD, MOD, ...)
21917**
21918** Return YYYY-MM-DD HH:MM:SS
21919*/
21920static void datetimeFunc(
21921 sqlite3_context *context,
21922 int argc,
21923 sqlite3_value **argv
21924){
21925 DateTime x;
21926 if( isDate(context, argc, argv, &x)==0 ){
21927 char zBuf[100];
21928 computeYMD_HMS(&x);
21929 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
21930 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
21931 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
21932 }
21933}
21934
21935/*
21936** time( TIMESTRING, MOD, MOD, ...)
21937**
21938** Return HH:MM:SS
21939*/
21940static void timeFunc(
21941 sqlite3_context *context,
21942 int argc,
21943 sqlite3_value **argv
21944){
21945 DateTime x;
21946 if( isDate(context, argc, argv, &x)==0 ){
21947 char zBuf[100];
21948 computeHMS(&x);
21949 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
21950 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
21951 }
21952}
21953
21954/*
21955** date( TIMESTRING, MOD, MOD, ...)
21956**
21957** Return YYYY-MM-DD
21958*/
21959static void dateFunc(
21960 sqlite3_context *context,
21961 int argc,
21962 sqlite3_value **argv
21963){
21964 DateTime x;
21965 if( isDate(context, argc, argv, &x)==0 ){
21966 char zBuf[100];
21967 computeYMD(&x);
21968 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
21969 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
21970 }
21971}
21972
21973/*
21974** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
21975**
21976** Return a string described by FORMAT. Conversions as follows:
21977**
21978** %d day of month
21979** %f ** fractional seconds SS.SSS
21980** %H hour 00-24
21981** %j day of year 000-366
21982** %J ** julian day number
21983** %m month 01-12
21984** %M minute 00-59
21985** %s seconds since 1970-01-01
21986** %S seconds 00-59
21987** %w day of week 0-6 sunday==0
21988** %W week of year 00-53
21989** %Y year 0000-9999
21990** %% %
21991*/
21992static void strftimeFunc(
21993 sqlite3_context *context,
21994 int argc,
21995 sqlite3_value **argv
21996){
21997 DateTime x;
21998 u64 n;
21999 size_t i,j;
22000 char *z;
22001 sqlite3 *db;
22002 const char *zFmt;
22003 char zBuf[100];
22004 if( argc==0 ) return;
22005 zFmt = (const char*)sqlite3_value_text(argv[0]);
22006 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
22007 db = sqlite3_context_db_handle(context);
22008 for(i=0, n=1; zFmt[i]; i++, n++){
22009 if( zFmt[i]=='%' ){
22010 switch( zFmt[i+1] ){
22011 case 'd':
22012 case 'H':
22013 case 'm':
22014 case 'M':
22015 case 'S':
22016 case 'W':
22017 n++;
22018 /* fall thru */
22019 case 'w':
22020 case '%':
22021 break;
22022 case 'f':
22023 n += 8;
22024 break;
22025 case 'j':
22026 n += 3;
22027 break;
22028 case 'Y':
22029 n += 8;
22030 break;
22031 case 's':
22032 case 'J':
22033 n += 50;
22034 break;
22035 default:
22036 return; /* ERROR. return a NULL */
22037 }
22038 i++;
22039 }
22040 }
22041 testcase( n==sizeof(zBuf)-1 );
22042 testcase( n==sizeof(zBuf) );
22043 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
22044 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
22045 if( n<sizeof(zBuf) ){
22046 z = zBuf;
22047 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH0] ){
22048 sqlite3_result_error_toobig(context);
22049 return;
22050 }else{
22051 z = sqlite3DbMallocRawNN(db, (int)n);
22052 if( z==0 ){
22053 sqlite3_result_error_nomem(context);
22054 return;
22055 }
22056 }
22057 computeJD(&x);
22058 computeYMD_HMS(&x);
22059 for(i=j=0; zFmt[i]; i++){
22060 if( zFmt[i]!='%' ){
22061 z[j++] = zFmt[i];
22062 }else{
22063 i++;
22064 switch( zFmt[i] ){
22065 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
22066 case 'f': {
22067 double s = x.s;
22068 if( s>59.999 ) s = 59.999;
22069 sqlite3_snprintf(7, &z[j],"%06.3f", s);
22070 j += sqlite3Strlen30(&z[j]);
22071 break;
22072 }
22073 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
22074 case 'W': /* Fall thru */
22075 case 'j': {
22076 int nDay; /* Number of days since 1st day of year */
22077 DateTime y = x;
22078 y.validJD = 0;
22079 y.M = 1;
22080 y.D = 1;
22081 computeJD(&y);
22082 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
22083 if( zFmt[i]=='W' ){
22084 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
22085 wd = (int)(((x.iJD+43200000)/86400000)%7);
22086 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
22087 j += 2;
22088 }else{
22089 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
22090 j += 3;
22091 }
22092 break;
22093 }
22094 case 'J': {
22095 sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
22096 j+=sqlite3Strlen30(&z[j]);
22097 break;
22098 }
22099 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
22100 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
22101 case 's': {
22102 sqlite3_snprintf(30,&z[j],"%lld",
22103 (i64)(x.iJD/1000 - 21086676*(i64)10000));
22104 j += sqlite3Strlen30(&z[j]);
22105 break;
22106 }
22107 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
22108 case 'w': {
22109 z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
22110 break;
22111 }
22112 case 'Y': {
22113 sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
22114 break;
22115 }
22116 default: z[j++] = '%'; break;
22117 }
22118 }
22119 }
22120 z[j] = 0;
22121 sqlite3_result_text(context, z, -1,
22122 z==zBuf ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) : SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3MallocSize));
22123}
22124
22125/*
22126** current_time()
22127**
22128** This function returns the same value as time('now').
22129*/
22130static void ctimeFunc(
22131 sqlite3_context *context,
22132 int NotUsed,
22133 sqlite3_value **NotUsed2
22134){
22135 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
22136 timeFunc(context, 0, 0);
22137}
22138
22139/*
22140** current_date()
22141**
22142** This function returns the same value as date('now').
22143*/
22144static void cdateFunc(
22145 sqlite3_context *context,
22146 int NotUsed,
22147 sqlite3_value **NotUsed2
22148){
22149 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
22150 dateFunc(context, 0, 0);
22151}
22152
22153/*
22154** current_timestamp()
22155**
22156** This function returns the same value as datetime('now').
22157*/
22158static void ctimestampFunc(
22159 sqlite3_context *context,
22160 int NotUsed,
22161 sqlite3_value **NotUsed2
22162){
22163 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
22164 datetimeFunc(context, 0, 0);
22165}
22166#endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
22167
22168#ifdef SQLITE_OMIT_DATETIME_FUNCS
22169/*
22170** If the library is compiled to omit the full-scale date and time
22171** handling (to get a smaller binary), the following minimal version
22172** of the functions current_time(), current_date() and current_timestamp()
22173** are included instead. This is to support column declarations that
22174** include "DEFAULT CURRENT_TIME" etc.
22175**
22176** This function uses the C-library functions time(), gmtime()
22177** and strftime(). The format string to pass to strftime() is supplied
22178** as the user-data for the function.
22179*/
22180static void currentTimeFunc(
22181 sqlite3_context *context,
22182 int argc,
22183 sqlite3_value **argv
22184){
22185 time_t t;
22186 char *zFormat = (char *)sqlite3_user_data(context);
22187 sqlite3_int64 iT;
22188 struct tm *pTm;
22189 struct tm sNow;
22190 char zBuf[20];
22191
22192 UNUSED_PARAMETER(argc)(void)(argc);
22193 UNUSED_PARAMETER(argv)(void)(argv);
22194
22195 iT = sqlite3StmtCurrentTime(context);
22196 if( iT<=0 ) return;
22197 t = iT/1000 - 10000*(sqlite3_int64)21086676;
22198#if HAVE_GMTIME_R
22199 pTm = gmtime_r(&t, &sNow);
22200#else
22201 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2));
22202 pTm = gmtime(&t);
22203 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
22204 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2));
22205#endif
22206 if( pTm ){
22207 strftime(zBuf, 20, zFormat, &sNow);
22208 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
22209 }
22210}
22211#endif
22212
22213/*
22214** This function registered all of the above C functions as SQL
22215** functions. This should be the only routine in this file with
22216** external linkage.
22217*/
22218SQLITE_PRIVATEstatic void sqlite3RegisterDateTimeFunctions(void){
22219 static FuncDef aDateTimeFuncs[] = {
22220#ifndef SQLITE_OMIT_DATETIME_FUNCS
22221 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ){-1, 0x2000|1|0x0800, (void*)&sqlite3Config, 0, juliandayFunc
, 0, 0, 0, "julianday", {0} }
,
22222 PURE_DATE(date, -1, 0, 0, dateFunc ){-1, 0x2000|1|0x0800, (void*)&sqlite3Config, 0, dateFunc,
0, 0, 0, "date", {0} }
,
22223 PURE_DATE(time, -1, 0, 0, timeFunc ){-1, 0x2000|1|0x0800, (void*)&sqlite3Config, 0, timeFunc,
0, 0, 0, "time", {0} }
,
22224 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ){-1, 0x2000|1|0x0800, (void*)&sqlite3Config, 0, datetimeFunc
, 0, 0, 0, "datetime", {0} }
,
22225 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ){-1, 0x2000|1|0x0800, (void*)&sqlite3Config, 0, strftimeFunc
, 0, 0, 0, "strftime", {0} }
,
22226 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ){0, 0x2000|1, 0, 0, ctimeFunc, 0, 0, 0, "current_time", {0} },
22227 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc){0, 0x2000|1, 0, 0, ctimestampFunc, 0, 0, 0, "current_timestamp"
, {0} }
,
22228 DFUNCTION(current_date, 0, 0, 0, cdateFunc ){0, 0x2000|1, 0, 0, cdateFunc, 0, 0, 0, "current_date", {0} },
22229#else
22230 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc){0, 0x2000|1|(0*0x0020), "%H:%M:%S", 0, currentTimeFunc, 0, 0
, 0, "current_time", }
,
22231 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc){0, 0x2000|1|(0*0x0020), "%Y-%m-%d", 0, currentTimeFunc, 0, 0
, 0, "current_date", }
,
22232 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc){0, 0x2000|1|(0*0x0020), "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc
, 0, 0, 0, "current_timestamp", }
,
22233#endif
22234 };
22235 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs)((int)(sizeof(aDateTimeFuncs)/sizeof(aDateTimeFuncs[0]))));
22236}
22237
22238/************** End of date.c ************************************************/
22239/************** Begin file os.c **********************************************/
22240/*
22241** 2005 November 29
22242**
22243** The author disclaims copyright to this source code. In place of
22244** a legal notice, here is a blessing:
22245**
22246** May you do good and not evil.
22247** May you find forgiveness for yourself and forgive others.
22248** May you share freely, never taking more than you give.
22249**
22250******************************************************************************
22251**
22252** This file contains OS interface code that is common to all
22253** architectures.
22254*/
22255/* #include "sqliteInt.h" */
22256
22257/*
22258** If we compile with the SQLITE_TEST macro set, then the following block
22259** of code will give us the ability to simulate a disk I/O error. This
22260** is used for testing the I/O recovery logic.
22261*/
22262#if defined(SQLITE_TEST)
22263SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
22264SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
22265SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
22266SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
22267SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
22268SQLITE_API int sqlite3_diskfull_pending = 0;
22269SQLITE_API int sqlite3_diskfull = 0;
22270#endif /* defined(SQLITE_TEST) */
22271
22272/*
22273** When testing, also keep a count of the number of open files.
22274*/
22275#if defined(SQLITE_TEST)
22276SQLITE_API int sqlite3_open_file_count = 0;
22277#endif /* defined(SQLITE_TEST) */
22278
22279/*
22280** The default SQLite sqlite3_vfs implementations do not allocate
22281** memory (actually, os_unix.c allocates a small amount of memory
22282** from within OsOpen()), but some third-party implementations may.
22283** So we test the effects of a malloc() failing and the sqlite3OsXXX()
22284** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
22285**
22286** The following functions are instrumented for malloc() failure
22287** testing:
22288**
22289** sqlite3OsRead()
22290** sqlite3OsWrite()
22291** sqlite3OsSync()
22292** sqlite3OsFileSize()
22293** sqlite3OsLock()
22294** sqlite3OsCheckReservedLock()
22295** sqlite3OsFileControl()
22296** sqlite3OsShmMap()
22297** sqlite3OsOpen()
22298** sqlite3OsDelete()
22299** sqlite3OsAccess()
22300** sqlite3OsFullPathname()
22301**
22302*/
22303#if defined(SQLITE_TEST)
22304SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
22305 #define DO_OS_MALLOC_TEST(x) \
22306 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
22307 void *pTstAlloc = sqlite3Malloc(10); \
22308 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8)); \
22309 sqlite3_free(pTstAlloc); \
22310 }
22311#else
22312 #define DO_OS_MALLOC_TEST(x)
22313#endif
22314
22315/*
22316** The following routines are convenience wrappers around methods
22317** of the sqlite3_file object. This is mostly just syntactic sugar. All
22318** of this would be completely automatic if SQLite were coded using
22319** C++ instead of plain old C.
22320*/
22321SQLITE_PRIVATEstatic void sqlite3OsClose(sqlite3_file *pId){
22322 if( pId->pMethods ){
22323 pId->pMethods->xClose(pId);
22324 pId->pMethods = 0;
22325 }
22326}
22327SQLITE_PRIVATEstatic int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
22328 DO_OS_MALLOC_TEST(id);
22329 return id->pMethods->xRead(id, pBuf, amt, offset);
22330}
22331SQLITE_PRIVATEstatic int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
22332 DO_OS_MALLOC_TEST(id);
22333 return id->pMethods->xWrite(id, pBuf, amt, offset);
22334}
22335SQLITE_PRIVATEstatic int sqlite3OsTruncate(sqlite3_file *id, i64 size){
22336 return id->pMethods->xTruncate(id, size);
22337}
22338SQLITE_PRIVATEstatic int sqlite3OsSync(sqlite3_file *id, int flags){
22339 DO_OS_MALLOC_TEST(id);
22340 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK0;
22341}
22342SQLITE_PRIVATEstatic int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
22343 DO_OS_MALLOC_TEST(id);
22344 return id->pMethods->xFileSize(id, pSize);
22345}
22346SQLITE_PRIVATEstatic int sqlite3OsLock(sqlite3_file *id, int lockType){
22347 DO_OS_MALLOC_TEST(id);
22348 return id->pMethods->xLock(id, lockType);
22349}
22350SQLITE_PRIVATEstatic int sqlite3OsUnlock(sqlite3_file *id, int lockType){
22351 return id->pMethods->xUnlock(id, lockType);
22352}
22353SQLITE_PRIVATEstatic int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
22354 DO_OS_MALLOC_TEST(id);
22355 return id->pMethods->xCheckReservedLock(id, pResOut);
22356}
22357
22358/*
22359** Use sqlite3OsFileControl() when we are doing something that might fail
22360** and we need to know about the failures. Use sqlite3OsFileControlHint()
22361** when simply tossing information over the wall to the VFS and we do not
22362** really care if the VFS receives and understands the information since it
22363** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
22364** routine has no return value since the return value would be meaningless.
22365*/
22366SQLITE_PRIVATEstatic int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
22367 if( id->pMethods==0 ) return SQLITE_NOTFOUND12;
22368#ifdef SQLITE_TEST
22369 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO22
22370 && op!=SQLITE_FCNTL_LOCK_TIMEOUT34
22371 ){
22372 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
22373 ** is using a regular VFS, it is called after the corresponding
22374 ** transaction has been committed. Injecting a fault at this point
22375 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
22376 ** but the transaction is committed anyway.
22377 **
22378 ** The core must call OsFileControl() though, not OsFileControlHint(),
22379 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
22380 ** means the commit really has failed and an error should be returned
22381 ** to the user. */
22382 DO_OS_MALLOC_TEST(id);
22383 }
22384#endif
22385 return id->pMethods->xFileControl(id, op, pArg);
22386}
22387SQLITE_PRIVATEstatic void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
22388 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
22389}
22390
22391SQLITE_PRIVATEstatic int sqlite3OsSectorSize(sqlite3_file *id){
22392 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
22393 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE4096);
22394}
22395SQLITE_PRIVATEstatic int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
22396 return id->pMethods->xDeviceCharacteristics(id);
22397}
22398#ifndef SQLITE_OMIT_WAL
22399SQLITE_PRIVATEstatic int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
22400 return id->pMethods->xShmLock(id, offset, n, flags);
22401}
22402SQLITE_PRIVATEstatic void sqlite3OsShmBarrier(sqlite3_file *id){
22403 id->pMethods->xShmBarrier(id);
22404}
22405SQLITE_PRIVATEstatic int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
22406 return id->pMethods->xShmUnmap(id, deleteFlag);
22407}
22408SQLITE_PRIVATEstatic int sqlite3OsShmMap(
22409 sqlite3_file *id, /* Database file handle */
22410 int iPage,
22411 int pgsz,
22412 int bExtend, /* True to extend file if necessary */
22413 void volatile **pp /* OUT: Pointer to mapping */
22414){
22415 DO_OS_MALLOC_TEST(id);
22416 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
22417}
22418#endif /* SQLITE_OMIT_WAL */
22419
22420#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
22421/* The real implementation of xFetch and xUnfetch */
22422SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
22423 DO_OS_MALLOC_TEST(id);
22424 return id->pMethods->xFetch(id, iOff, iAmt, pp);
22425}
22426SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
22427 return id->pMethods->xUnfetch(id, iOff, p);
22428}
22429#else
22430/* No-op stubs to use when memory-mapped I/O is disabled */
22431SQLITE_PRIVATEstatic int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
22432 *pp = 0;
22433 return SQLITE_OK0;
22434}
22435SQLITE_PRIVATEstatic int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
22436 return SQLITE_OK0;
22437}
22438#endif
22439
22440/*
22441** The next group of routines are convenience wrappers around the
22442** VFS methods.
22443*/
22444SQLITE_PRIVATEstatic int sqlite3OsOpen(
22445 sqlite3_vfs *pVfs,
22446 const char *zPath,
22447 sqlite3_file *pFile,
22448 int flags,
22449 int *pFlagsOut
22450){
22451 int rc;
22452 DO_OS_MALLOC_TEST(0);
22453 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
22454 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
22455 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
22456 ** reaching the VFS. */
22457 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
22458 assert( rc==SQLITE_OK || pFile->pMethods==0 )((void) (0));
22459 return rc;
22460}
22461SQLITE_PRIVATEstatic int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
22462 DO_OS_MALLOC_TEST(0);
22463 assert( dirSync==0 || dirSync==1 )((void) (0));
22464 return pVfs->xDelete(pVfs, zPath, dirSync);
22465}
22466SQLITE_PRIVATEstatic int sqlite3OsAccess(
22467 sqlite3_vfs *pVfs,
22468 const char *zPath,
22469 int flags,
22470 int *pResOut
22471){
22472 DO_OS_MALLOC_TEST(0);
22473 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
22474}
22475SQLITE_PRIVATEstatic int sqlite3OsFullPathname(
22476 sqlite3_vfs *pVfs,
22477 const char *zPath,
22478 int nPathOut,
22479 char *zPathOut
22480){
22481 DO_OS_MALLOC_TEST(0);
22482 zPathOut[0] = 0;
22483 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
22484}
22485#ifndef SQLITE_OMIT_LOAD_EXTENSION
22486SQLITE_PRIVATEstatic void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
22487 return pVfs->xDlOpen(pVfs, zPath);
22488}
22489SQLITE_PRIVATEstatic void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
22490 pVfs->xDlError(pVfs, nByte, zBufOut);
22491}
22492SQLITE_PRIVATEstatic void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
22493 return pVfs->xDlSym(pVfs, pHdle, zSym);
22494}
22495SQLITE_PRIVATEstatic void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
22496 pVfs->xDlClose(pVfs, pHandle);
22497}
22498#endif /* SQLITE_OMIT_LOAD_EXTENSION */
22499SQLITE_PRIVATEstatic int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
22500 return pVfs->xRandomness(pVfs, nByte, zBufOut);
22501}
22502SQLITE_PRIVATEstatic int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
22503 return pVfs->xSleep(pVfs, nMicro);
22504}
22505SQLITE_PRIVATEstatic int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
22506 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
22507}
22508SQLITE_PRIVATEstatic int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
22509 int rc;
22510 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
22511 ** method to get the current date and time if that method is available
22512 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
22513 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
22514 ** unavailable.
22515 */
22516 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
22517 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
22518 }else{
22519 double r;
22520 rc = pVfs->xCurrentTime(pVfs, &r);
22521 *pTimeOut = (sqlite3_int64)(r*86400000.0);
22522 }
22523 return rc;
22524}
22525
22526SQLITE_PRIVATEstatic int sqlite3OsOpenMalloc(
22527 sqlite3_vfs *pVfs,
22528 const char *zFile,
22529 sqlite3_file **ppFile,
22530 int flags,
22531 int *pOutFlags
22532){
22533 int rc;
22534 sqlite3_file *pFile;
22535 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
22536 if( pFile ){
22537 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
22538 if( rc!=SQLITE_OK0 ){
22539 sqlite3_free(pFile);
22540 }else{
22541 *ppFile = pFile;
22542 }
22543 }else{
22544 rc = SQLITE_NOMEM_BKPT7;
22545 }
22546 return rc;
22547}
22548SQLITE_PRIVATEstatic void sqlite3OsCloseFree(sqlite3_file *pFile){
22549 assert( pFile )((void) (0));
22550 sqlite3OsClose(pFile);
22551 sqlite3_free(pFile);
22552}
22553
22554/*
22555** This function is a wrapper around the OS specific implementation of
22556** sqlite3_os_init(). The purpose of the wrapper is to provide the
22557** ability to simulate a malloc failure, so that the handling of an
22558** error in sqlite3_os_init() by the upper layers can be tested.
22559*/
22560SQLITE_PRIVATEstatic int sqlite3OsInit(void){
22561 void *p = sqlite3_malloc(10);
22562 if( p==0 ) return SQLITE_NOMEM_BKPT7;
22563 sqlite3_free(p);
22564 return sqlite3_os_init();
22565}
22566
22567/*
22568** The list of all registered VFS implementations.
22569*/
22570static sqlite3_vfs * SQLITE_WSD vfsListvfsList = 0;
22571#define vfsListvfsList GLOBAL(sqlite3_vfs *, vfsList)vfsList
22572
22573/*
22574** Locate a VFS by name. If no name is given, simply return the
22575** first VFS on the list.
22576*/
22577SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
22578 sqlite3_vfs *pVfs = 0;
22579#if SQLITE_THREADSAFE1
22580 sqlite3_mutex *mutex;
22581#endif
22582#ifndef SQLITE_OMIT_AUTOINIT
22583 int rc = sqlite3_initialize();
22584 if( rc ) return 0;
22585#endif
22586#if SQLITE_THREADSAFE1
22587 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2);
22588#endif
22589 sqlite3_mutex_enter(mutex);
22590 for(pVfs = vfsListvfsList; pVfs; pVfs=pVfs->pNext){
22591 if( zVfs==0 ) break;
22592 if( strcmp(zVfs, pVfs->zName)==0 ) break;
22593 }
22594 sqlite3_mutex_leave(mutex);
22595 return pVfs;
22596}
22597
22598/*
22599** Unlink a VFS from the linked list
22600*/
22601static void vfsUnlink(sqlite3_vfs *pVfs){
22602 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )((void) (0));
22603 if( pVfs==0 ){
22604 /* No-op */
22605 }else if( vfsListvfsList==pVfs ){
22606 vfsListvfsList = pVfs->pNext;
22607 }else if( vfsListvfsList ){
22608 sqlite3_vfs *p = vfsListvfsList;
22609 while( p->pNext && p->pNext!=pVfs ){
22610 p = p->pNext;
22611 }
22612 if( p->pNext==pVfs ){
22613 p->pNext = pVfs->pNext;
22614 }
22615 }
22616}
22617
22618/*
22619** Register a VFS with the system. It is harmless to register the same
22620** VFS multiple times. The new VFS becomes the default if makeDflt is
22621** true.
22622*/
22623SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
22624 MUTEX_LOGIC(sqlite3_mutex *mutex;)sqlite3_mutex *mutex;
22625#ifndef SQLITE_OMIT_AUTOINIT
22626 int rc = sqlite3_initialize();
22627 if( rc ) return rc;
22628#endif
22629#ifdef SQLITE_ENABLE_API_ARMOR
22630 if( pVfs==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(22630);
22631#endif
22632
22633 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )mutex = sqlite3MutexAlloc(2);
22634 sqlite3_mutex_enter(mutex);
22635 vfsUnlink(pVfs);
22636 if( makeDflt || vfsListvfsList==0 ){
22637 pVfs->pNext = vfsListvfsList;
22638 vfsListvfsList = pVfs;
22639 }else{
22640 pVfs->pNext = vfsListvfsList->pNext;
22641 vfsListvfsList->pNext = pVfs;
22642 }
22643 assert(vfsList)((void) (0));
22644 sqlite3_mutex_leave(mutex);
22645 return SQLITE_OK0;
22646}
22647
22648/*
22649** Unregister a VFS so that it is no longer accessible.
22650*/
22651SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
22652 MUTEX_LOGIC(sqlite3_mutex *mutex;)sqlite3_mutex *mutex;
22653#ifndef SQLITE_OMIT_AUTOINIT
22654 int rc = sqlite3_initialize();
22655 if( rc ) return rc;
22656#endif
22657 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )mutex = sqlite3MutexAlloc(2);
22658 sqlite3_mutex_enter(mutex);
22659 vfsUnlink(pVfs);
22660 sqlite3_mutex_leave(mutex);
22661 return SQLITE_OK0;
22662}
22663
22664/************** End of os.c **************************************************/
22665/************** Begin file fault.c *******************************************/
22666/*
22667** 2008 Jan 22
22668**
22669** The author disclaims copyright to this source code. In place of
22670** a legal notice, here is a blessing:
22671**
22672** May you do good and not evil.
22673** May you find forgiveness for yourself and forgive others.
22674** May you share freely, never taking more than you give.
22675**
22676*************************************************************************
22677**
22678** This file contains code to support the concept of "benign"
22679** malloc failures (when the xMalloc() or xRealloc() method of the
22680** sqlite3_mem_methods structure fails to allocate a block of memory
22681** and returns 0).
22682**
22683** Most malloc failures are non-benign. After they occur, SQLite
22684** abandons the current operation and returns an error code (usually
22685** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
22686** fatal. For example, if a malloc fails while resizing a hash table, this
22687** is completely recoverable simply by not carrying out the resize. The
22688** hash table will continue to function normally. So a malloc failure
22689** during a hash table resize is a benign fault.
22690*/
22691
22692/* #include "sqliteInt.h" */
22693
22694#ifndef SQLITE_UNTESTABLE
22695
22696/*
22697** Global variables.
22698*/
22699typedef struct BenignMallocHooks BenignMallocHooks;
22700static SQLITE_WSD struct BenignMallocHooks {
22701 void (*xBenignBegin)(void);
22702 void (*xBenignEnd)(void);
22703} sqlite3Hooks = { 0, 0 };
22704
22705/* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
22706** structure. If writable static data is unsupported on the target,
22707** we have to locate the state vector at run-time. In the more common
22708** case where writable static data is supported, wsdHooks can refer directly
22709** to the "sqlite3Hooks" state vector declared above.
22710*/
22711#ifdef SQLITE_OMIT_WSD
22712# define wsdHooksInit \
22713 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)sqlite3Hooks
22714# define wsdHookssqlite3Hooks x[0]
22715#else
22716# define wsdHooksInit
22717# define wsdHookssqlite3Hooks sqlite3Hooks
22718#endif
22719
22720
22721/*
22722** Register hooks to call when sqlite3BeginBenignMalloc() and
22723** sqlite3EndBenignMalloc() are called, respectively.
22724*/
22725SQLITE_PRIVATEstatic void sqlite3BenignMallocHooks(
22726 void (*xBenignBegin)(void),
22727 void (*xBenignEnd)(void)
22728){
22729 wsdHooksInit;
22730 wsdHookssqlite3Hooks.xBenignBegin = xBenignBegin;
22731 wsdHookssqlite3Hooks.xBenignEnd = xBenignEnd;
22732}
22733
22734/*
22735** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
22736** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
22737** indicates that subsequent malloc failures are non-benign.
22738*/
22739SQLITE_PRIVATEstatic void sqlite3BeginBenignMalloc(void){
22740 wsdHooksInit;
22741 if( wsdHookssqlite3Hooks.xBenignBegin ){
22742 wsdHookssqlite3Hooks.xBenignBegin();
22743 }
22744}
22745SQLITE_PRIVATEstatic void sqlite3EndBenignMalloc(void){
22746 wsdHooksInit;
22747 if( wsdHookssqlite3Hooks.xBenignEnd ){
22748 wsdHookssqlite3Hooks.xBenignEnd();
22749 }
22750}
22751
22752#endif /* #ifndef SQLITE_UNTESTABLE */
22753
22754/************** End of fault.c ***********************************************/
22755/************** Begin file mem0.c ********************************************/
22756/*
22757** 2008 October 28
22758**
22759** The author disclaims copyright to this source code. In place of
22760** a legal notice, here is a blessing:
22761**
22762** May you do good and not evil.
22763** May you find forgiveness for yourself and forgive others.
22764** May you share freely, never taking more than you give.
22765**
22766*************************************************************************
22767**
22768** This file contains a no-op memory allocation drivers for use when
22769** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
22770** here always fail. SQLite will not operate with these drivers. These
22771** are merely placeholders. Real drivers must be substituted using
22772** sqlite3_config() before SQLite will operate.
22773*/
22774/* #include "sqliteInt.h" */
22775
22776/*
22777** This version of the memory allocator is the default. It is
22778** used when no other memory allocator is specified using compile-time
22779** macros.
22780*/
22781#ifdef SQLITE_ZERO_MALLOC
22782
22783/*
22784** No-op versions of all memory allocation routines
22785*/
22786static void *sqlite3MemMalloc(int nByte){ return 0; }
22787static void sqlite3MemFree(void *pPrior){ return; }
22788static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
22789static int sqlite3MemSize(void *pPrior){ return 0; }
22790static int sqlite3MemRoundup(int n){ return n; }
22791static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK0; }
22792static void sqlite3MemShutdown(void *NotUsed){ return; }
22793
22794/*
22795** This routine is the only routine in this file with external linkage.
22796**
22797** Populate the low-level memory allocation function pointers in
22798** sqlite3GlobalConfig.m with pointers to the routines in this file.
22799*/
22800SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
22801 static const sqlite3_mem_methods defaultMethods = {
22802 sqlite3MemMalloc,
22803 sqlite3MemFree,
22804 sqlite3MemRealloc,
22805 sqlite3MemSize,
22806 sqlite3MemRoundup,
22807 sqlite3MemInit,
22808 sqlite3MemShutdown,
22809 0
22810 };
22811 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
22812}
22813
22814#endif /* SQLITE_ZERO_MALLOC */
22815
22816/************** End of mem0.c ************************************************/
22817/************** Begin file mem1.c ********************************************/
22818/*
22819** 2007 August 14
22820**
22821** The author disclaims copyright to this source code. In place of
22822** a legal notice, here is a blessing:
22823**
22824** May you do good and not evil.
22825** May you find forgiveness for yourself and forgive others.
22826** May you share freely, never taking more than you give.
22827**
22828*************************************************************************
22829**
22830** This file contains low-level memory allocation drivers for when
22831** SQLite will use the standard C-library malloc/realloc/free interface
22832** to obtain the memory it needs.
22833**
22834** This file contains implementations of the low-level memory allocation
22835** routines specified in the sqlite3_mem_methods object. The content of
22836** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
22837** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
22838** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
22839** default configuration is to use memory allocation routines in this
22840** file.
22841**
22842** C-preprocessor macro summary:
22843**
22844** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
22845** the malloc_usable_size() interface exists
22846** on the target platform. Or, this symbol
22847** can be set manually, if desired.
22848** If an equivalent interface exists by
22849** a different name, using a separate -D
22850** option to rename it.
22851**
22852** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
22853** memory allocator. Set this symbol to enable
22854** building on older macs.
22855**
22856** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
22857** _msize() on windows systems. This might
22858** be necessary when compiling for Delphi,
22859** for example.
22860*/
22861/* #include "sqliteInt.h" */
22862
22863/*
22864** This version of the memory allocator is the default. It is
22865** used when no other memory allocator is specified using compile-time
22866** macros.
22867*/
22868#ifdef SQLITE_SYSTEM_MALLOC1
22869#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
22870
22871/*
22872** Use the zone allocator available on apple products unless the
22873** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
22874*/
22875#include <sys/sysctl.h>
22876#include <malloc/malloc.h>
22877#ifdef SQLITE_MIGHT_BE_SINGLE_CORE
22878#include <libkern/OSAtomic.h>
22879#endif /* SQLITE_MIGHT_BE_SINGLE_CORE */
22880static malloc_zone_t* _sqliteZone_;
22881#define SQLITE_MALLOC(x)malloc(x) malloc_zone_malloc(_sqliteZone_, (x))
22882#define SQLITE_FREE(x)free(x) malloc_zone_free(_sqliteZone_, (x));
22883#define SQLITE_REALLOC(x,y)realloc((x),(y)) malloc_zone_realloc(_sqliteZone_, (x), (y))
22884#define SQLITE_MALLOCSIZE(x) \
22885 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
22886
22887#else /* if not __APPLE__ */
22888
22889/*
22890** Use standard C library malloc and free on non-Apple systems.
22891** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
22892*/
22893#define SQLITE_MALLOC(x)malloc(x) malloc(x)
22894#define SQLITE_FREE(x)free(x) free(x)
22895#define SQLITE_REALLOC(x,y)realloc((x),(y)) realloc((x),(y))
22896
22897/*
22898** The malloc.h header file is needed for malloc_usable_size() function
22899** on some systems (e.g. Linux).
22900*/
22901#if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
22902# define SQLITE_USE_MALLOC_H 1
22903# define SQLITE_USE_MALLOC_USABLE_SIZE 1
22904/*
22905** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
22906** use of _msize() is automatic, but can be disabled by compiling with
22907** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
22908** the malloc.h header file.
22909*/
22910#elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
22911# define SQLITE_USE_MALLOC_H
22912# define SQLITE_USE_MSIZE
22913#endif
22914
22915/*
22916** Include the malloc.h header file, if necessary. Also set define macro
22917** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
22918** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
22919** The memory size function can always be overridden manually by defining
22920** the macro SQLITE_MALLOCSIZE to the desired function name.
22921*/
22922#if defined(SQLITE_USE_MALLOC_H)
22923# include <malloc.h>
22924# if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
22925# if !defined(SQLITE_MALLOCSIZE)
22926# define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
22927# endif
22928# elif defined(SQLITE_USE_MSIZE)
22929# if !defined(SQLITE_MALLOCSIZE)
22930# define SQLITE_MALLOCSIZE _msize
22931# endif
22932# endif
22933#endif /* defined(SQLITE_USE_MALLOC_H) */
22934
22935#endif /* __APPLE__ or not __APPLE__ */
22936
22937/*
22938** Like malloc(), but remember the size of the allocation
22939** so that we can find it later using sqlite3MemSize().
22940**
22941** For this low-level routine, we are guaranteed that nByte>0 because
22942** cases of nByte<=0 will be intercepted and dealt with by higher level
22943** routines.
22944*/
22945static void *sqlite3MemMalloc(int nByte){
22946#ifdef SQLITE_MALLOCSIZE
22947 void *p;
22948 testcase( ROUND8(nByte)==nByte );
22949 p = SQLITE_MALLOC( nByte )malloc(nByte);
22950 if( p==0 ){
22951 testcase( sqlite3GlobalConfig.xLog!=0 );
22952 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes of memory", nByte);
22953 }
22954 return p;
22955#else
22956 sqlite3_int64 *p;
22957 assert( nByte>0 )((void) (0));
22958 testcase( ROUND8(nByte)!=nByte );
22959 p = SQLITE_MALLOC( nByte+8 )malloc(nByte+8);
22960 if( p ){
22961 p[0] = nByte;
22962 p++;
22963 }else{
22964 testcase( sqlite3GlobalConfig.xLog!=0 );
22965 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes of memory", nByte);
22966 }
22967 return (void *)p;
22968#endif
22969}
22970
22971/*
22972** Like free() but works for allocations obtained from sqlite3MemMalloc()
22973** or sqlite3MemRealloc().
22974**
22975** For this low-level routine, we already know that pPrior!=0 since
22976** cases where pPrior==0 will have been intecepted and dealt with
22977** by higher-level routines.
22978*/
22979static void sqlite3MemFree(void *pPrior){
22980#ifdef SQLITE_MALLOCSIZE
22981 SQLITE_FREE(pPrior)free(pPrior);
22982#else
22983 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
22984 assert( pPrior!=0 )((void) (0));
22985 p--;
22986 SQLITE_FREE(p)free(p);
22987#endif
22988}
22989
22990/*
22991** Report the allocated size of a prior return from xMalloc()
22992** or xRealloc().
22993*/
22994static int sqlite3MemSize(void *pPrior){
22995#ifdef SQLITE_MALLOCSIZE
22996 assert( pPrior!=0 )((void) (0));
22997 return (int)SQLITE_MALLOCSIZE(pPrior);
22998#else
22999 sqlite3_int64 *p;
23000 assert( pPrior!=0 )((void) (0));
23001 p = (sqlite3_int64*)pPrior;
23002 p--;
23003 return (int)p[0];
23004#endif
23005}
23006
23007/*
23008** Like realloc(). Resize an allocation previously obtained from
23009** sqlite3MemMalloc().
23010**
23011** For this low-level interface, we know that pPrior!=0. Cases where
23012** pPrior==0 while have been intercepted by higher-level routine and
23013** redirected to xMalloc. Similarly, we know that nByte>0 because
23014** cases where nByte<=0 will have been intercepted by higher-level
23015** routines and redirected to xFree.
23016*/
23017static void *sqlite3MemRealloc(void *pPrior, int nByte){
23018#ifdef SQLITE_MALLOCSIZE
23019 void *p = SQLITE_REALLOC(pPrior, nByte)realloc((pPrior),(nByte));
23020 if( p==0 ){
23021 testcase( sqlite3GlobalConfig.xLog!=0 );
23022 sqlite3_log(SQLITE_NOMEM7,
23023 "failed memory resize %u to %u bytes",
23024 SQLITE_MALLOCSIZE(pPrior), nByte);
23025 }
23026 return p;
23027#else
23028 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
23029 assert( pPrior!=0 && nByte>0 )((void) (0));
23030 assert( nByte==ROUND8(nByte) )((void) (0)); /* EV: R-46199-30249 */
23031 p--;
23032 p = SQLITE_REALLOC(p, nByte+8 )realloc((p),(nByte+8));
23033 if( p ){
23034 p[0] = nByte;
23035 p++;
23036 }else{
23037 testcase( sqlite3GlobalConfig.xLog!=0 );
23038 sqlite3_log(SQLITE_NOMEM7,
23039 "failed memory resize %u to %u bytes",
23040 sqlite3MemSize(pPrior), nByte);
23041 }
23042 return (void*)p;
23043#endif
23044}
23045
23046/*
23047** Round up a request size to the next valid allocation size.
23048*/
23049static int sqlite3MemRoundup(int n){
23050 return ROUND8(n)(((n)+7)&~7);
23051}
23052
23053/*
23054** Initialize this module.
23055*/
23056static int sqlite3MemInit(void *NotUsed){
23057#if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
23058 int cpuCount;
23059 size_t len;
23060 if( _sqliteZone_ ){
23061 return SQLITE_OK0;
23062 }
23063 len = sizeof(cpuCount);
23064 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
23065 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL((void*)0), 0);
23066 if( cpuCount>1 ){
23067 /* defer MT decisions to system malloc */
23068 _sqliteZone_ = malloc_default_zone();
23069 }else{
23070 /* only 1 core, use our own zone to contention over global locks,
23071 ** e.g. we have our own dedicated locks */
23072 _sqliteZone_ = malloc_create_zone(4096, 0);
23073 malloc_set_zone_name(_sqliteZone_, "Sqlite_Heap");
23074 }
23075#endif /* defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC) */
23076 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
23077 return SQLITE_OK0;
23078}
23079
23080/*
23081** Deinitialize this module.
23082*/
23083static void sqlite3MemShutdown(void *NotUsed){
23084 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
23085 return;
23086}
23087
23088/*
23089** This routine is the only routine in this file with external linkage.
23090**
23091** Populate the low-level memory allocation function pointers in
23092** sqlite3GlobalConfig.m with pointers to the routines in this file.
23093*/
23094SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
23095 static const sqlite3_mem_methods defaultMethods = {
23096 sqlite3MemMalloc,
23097 sqlite3MemFree,
23098 sqlite3MemRealloc,
23099 sqlite3MemSize,
23100 sqlite3MemRoundup,
23101 sqlite3MemInit,
23102 sqlite3MemShutdown,
23103 0
23104 };
23105 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
23106}
23107
23108#endif /* SQLITE_SYSTEM_MALLOC */
23109
23110/************** End of mem1.c ************************************************/
23111/************** Begin file mem2.c ********************************************/
23112/*
23113** 2007 August 15
23114**
23115** The author disclaims copyright to this source code. In place of
23116** a legal notice, here is a blessing:
23117**
23118** May you do good and not evil.
23119** May you find forgiveness for yourself and forgive others.
23120** May you share freely, never taking more than you give.
23121**
23122*************************************************************************
23123**
23124** This file contains low-level memory allocation drivers for when
23125** SQLite will use the standard C-library malloc/realloc/free interface
23126** to obtain the memory it needs while adding lots of additional debugging
23127** information to each allocation in order to help detect and fix memory
23128** leaks and memory usage errors.
23129**
23130** This file contains implementations of the low-level memory allocation
23131** routines specified in the sqlite3_mem_methods object.
23132*/
23133/* #include "sqliteInt.h" */
23134
23135/*
23136** This version of the memory allocator is used only if the
23137** SQLITE_MEMDEBUG macro is defined
23138*/
23139#ifdef SQLITE_MEMDEBUG
23140
23141/*
23142** The backtrace functionality is only available with GLIBC
23143*/
23144#ifdef __GLIBC__2
23145 extern int backtrace(void**,int);
23146 extern void backtrace_symbols_fd(void*const*,int,int);
23147#else
23148# define backtrace(A,B) 1
23149# define backtrace_symbols_fd(A,B,C)
23150#endif
23151/* #include <stdio.h> */
23152
23153/*
23154** Each memory allocation looks like this:
23155**
23156** ------------------------------------------------------------------------
23157** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
23158** ------------------------------------------------------------------------
23159**
23160** The application code sees only a pointer to the allocation. We have
23161** to back up from the allocation pointer to find the MemBlockHdr. The
23162** MemBlockHdr tells us the size of the allocation and the number of
23163** backtrace pointers. There is also a guard word at the end of the
23164** MemBlockHdr.
23165*/
23166struct MemBlockHdr {
23167 i64 iSize; /* Size of this allocation */
23168 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
23169 char nBacktrace; /* Number of backtraces on this alloc */
23170 char nBacktraceSlots; /* Available backtrace slots */
23171 u8 nTitle; /* Bytes of title; includes '\0' */
23172 u8 eType; /* Allocation type code */
23173 int iForeGuard; /* Guard word for sanity */
23174};
23175
23176/*
23177** Guard words
23178*/
23179#define FOREGUARD 0x80F5E153
23180#define REARGUARD 0xE4676B53
23181
23182/*
23183** Number of malloc size increments to track.
23184*/
23185#define NCSIZE 1000
23186
23187/*
23188** All of the static variables used by this module are collected
23189** into a single structure named "mem". This is to keep the
23190** static variables organized and to reduce namespace pollution
23191** when this module is combined with other in the amalgamation.
23192*/
23193static struct {
23194
23195 /*
23196 ** Mutex to control access to the memory allocation subsystem.
23197 */
23198 sqlite3_mutex *mutex;
23199
23200 /*
23201 ** Head and tail of a linked list of all outstanding allocations
23202 */
23203 struct MemBlockHdr *pFirst;
23204 struct MemBlockHdr *pLast;
23205
23206 /*
23207 ** The number of levels of backtrace to save in new allocations.
23208 */
23209 int nBacktrace;
23210 void (*xBacktrace)(int, int, void **);
23211
23212 /*
23213 ** Title text to insert in front of each block
23214 */
23215 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
23216 char zTitle[100]; /* The title text */
23217
23218 /*
23219 ** sqlite3MallocDisallow() increments the following counter.
23220 ** sqlite3MallocAllow() decrements it.
23221 */
23222 int disallow; /* Do not allow memory allocation */
23223
23224 /*
23225 ** Gather statistics on the sizes of memory allocations.
23226 ** nAlloc[i] is the number of allocation attempts of i*8
23227 ** bytes. i==NCSIZE is the number of allocation attempts for
23228 ** sizes more than NCSIZE*8 bytes.
23229 */
23230 int nAlloc[NCSIZE]; /* Total number of allocations */
23231 int nCurrent[NCSIZE]; /* Current number of allocations */
23232 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
23233
23234} mem;
23235
23236
23237/*
23238** Adjust memory usage statistics
23239*/
23240static void adjustStats(int iSize, int increment){
23241 int i = ROUND8(iSize)(((iSize)+7)&~7)/8;
23242 if( i>NCSIZE-1 ){
23243 i = NCSIZE - 1;
23244 }
23245 if( increment>0 ){
23246 mem.nAlloc[i]++;
23247 mem.nCurrent[i]++;
23248 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
23249 mem.mxCurrent[i] = mem.nCurrent[i];
23250 }
23251 }else{
23252 mem.nCurrent[i]--;
23253 assert( mem.nCurrent[i]>=0 )((void) (0));
23254 }
23255}
23256
23257/*
23258** Given an allocation, find the MemBlockHdr for that allocation.
23259**
23260** This routine checks the guards at either end of the allocation and
23261** if they are incorrect it asserts.
23262*/
23263static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
23264 struct MemBlockHdr *p;
23265 int *pInt;
23266 u8 *pU8;
23267 int nReserve;
23268
23269 p = (struct MemBlockHdr*)pAllocation;
23270 p--;
23271 assert( p->iForeGuard==(int)FOREGUARD )((void) (0));
23272 nReserve = ROUND8(p->iSize)(((p->iSize)+7)&~7);
23273 pInt = (int*)pAllocation;
23274 pU8 = (u8*)pAllocation;
23275 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD )((void) (0));
23276 /* This checks any of the "extra" bytes allocated due
23277 ** to rounding up to an 8 byte boundary to ensure
23278 ** they haven't been overwritten.
23279 */
23280 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 )((void) (0));
23281 return p;
23282}
23283
23284/*
23285** Return the number of bytes currently allocated at address p.
23286*/
23287static int sqlite3MemSize(void *p){
23288 struct MemBlockHdr *pHdr;
23289 if( !p ){
23290 return 0;
23291 }
23292 pHdr = sqlite3MemsysGetHeader(p);
23293 return (int)pHdr->iSize;
23294}
23295
23296/*
23297** Initialize the memory allocation subsystem.
23298*/
23299static int sqlite3MemInit(void *NotUsed){
23300 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
23301 assert( (sizeof(struct MemBlockHdr)&7) == 0 )((void) (0));
23302 if( !sqlite3GlobalConfigsqlite3Config.bMemstat ){
23303 /* If memory status is enabled, then the malloc.c wrapper will already
23304 ** hold the STATIC_MEM mutex when the routines here are invoked. */
23305 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
23306 }
23307 return SQLITE_OK0;
23308}
23309
23310/*
23311** Deinitialize the memory allocation subsystem.
23312*/
23313static void sqlite3MemShutdown(void *NotUsed){
23314 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
23315 mem.mutex = 0;
23316}
23317
23318/*
23319** Round up a request size to the next valid allocation size.
23320*/
23321static int sqlite3MemRoundup(int n){
23322 return ROUND8(n)(((n)+7)&~7);
23323}
23324
23325/*
23326** Fill a buffer with pseudo-random bytes. This is used to preset
23327** the content of a new memory allocation to unpredictable values and
23328** to clear the content of a freed allocation to unpredictable values.
23329*/
23330static void randomFill(char *pBuf, int nByte){
23331 unsigned int x, y, r;
23332 x = SQLITE_PTR_TO_INT(pBuf)((int)(long int)(pBuf));
23333 y = nByte | 1;
23334 while( nByte >= 4 ){
23335 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
23336 y = y*1103515245 + 12345;
23337 r = x ^ y;
23338 *(int*)pBuf = r;
23339 pBuf += 4;
23340 nByte -= 4;
23341 }
23342 while( nByte-- > 0 ){
23343 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
23344 y = y*1103515245 + 12345;
23345 r = x ^ y;
23346 *(pBuf++) = r & 0xff;
23347 }
23348}
23349
23350/*
23351** Allocate nByte bytes of memory.
23352*/
23353static void *sqlite3MemMalloc(int nByte){
23354 struct MemBlockHdr *pHdr;
23355 void **pBt;
23356 char *z;
23357 int *pInt;
23358 void *p = 0;
23359 int totalSize;
23360 int nReserve;
23361 sqlite3_mutex_enter(mem.mutex);
23362 assert( mem.disallow==0 )((void) (0));
23363 nReserve = ROUND8(nByte)(((nByte)+7)&~7);
23364 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
23365 mem.nBacktrace*sizeof(void*) + mem.nTitle;
23366 p = malloc(totalSize);
23367 if( p ){
23368 z = p;
23369 pBt = (void**)&z[mem.nTitle];
23370 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
23371 pHdr->pNext = 0;
23372 pHdr->pPrev = mem.pLast;
23373 if( mem.pLast ){
23374 mem.pLast->pNext = pHdr;
23375 }else{
23376 mem.pFirst = pHdr;
23377 }
23378 mem.pLast = pHdr;
23379 pHdr->iForeGuard = FOREGUARD;
23380 pHdr->eType = MEMTYPE_HEAP0x01;
23381 pHdr->nBacktraceSlots = mem.nBacktrace;
23382 pHdr->nTitle = mem.nTitle;
23383 if( mem.nBacktrace ){
23384 void *aAddr[40];
23385 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
23386 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
23387 assert(pBt[0])((void) (0));
23388 if( mem.xBacktrace ){
23389 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
23390 }
23391 }else{
23392 pHdr->nBacktrace = 0;
23393 }
23394 if( mem.nTitle ){
23395 memcpy(z, mem.zTitle, mem.nTitle);
23396 }
23397 pHdr->iSize = nByte;
23398 adjustStats(nByte, +1);
23399 pInt = (int*)&pHdr[1];
23400 pInt[nReserve/sizeof(int)] = REARGUARD;
23401 randomFill((char*)pInt, nByte);
23402 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
23403 p = (void*)pInt;
23404 }
23405 sqlite3_mutex_leave(mem.mutex);
23406 return p;
23407}
23408
23409/*
23410** Free memory.
23411*/
23412static void sqlite3MemFree(void *pPrior){
23413 struct MemBlockHdr *pHdr;
23414 void **pBt;
23415 char *z;
23416 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0((void) (0))
23417 || mem.mutex!=0 )((void) (0));
23418 pHdr = sqlite3MemsysGetHeader(pPrior);
23419 pBt = (void**)pHdr;
23420 pBt -= pHdr->nBacktraceSlots;
23421 sqlite3_mutex_enter(mem.mutex);
23422 if( pHdr->pPrev ){
23423 assert( pHdr->pPrev->pNext==pHdr )((void) (0));
23424 pHdr->pPrev->pNext = pHdr->pNext;
23425 }else{
23426 assert( mem.pFirst==pHdr )((void) (0));
23427 mem.pFirst = pHdr->pNext;
23428 }
23429 if( pHdr->pNext ){
23430 assert( pHdr->pNext->pPrev==pHdr )((void) (0));
23431 pHdr->pNext->pPrev = pHdr->pPrev;
23432 }else{
23433 assert( mem.pLast==pHdr )((void) (0));
23434 mem.pLast = pHdr->pPrev;
23435 }
23436 z = (char*)pBt;
23437 z -= pHdr->nTitle;
23438 adjustStats((int)pHdr->iSize, -1);
23439 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
23440 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
23441 free(z);
23442 sqlite3_mutex_leave(mem.mutex);
23443}
23444
23445/*
23446** Change the size of an existing memory allocation.
23447**
23448** For this debugging implementation, we *always* make a copy of the
23449** allocation into a new place in memory. In this way, if the
23450** higher level code is using pointer to the old allocation, it is
23451** much more likely to break and we are much more liking to find
23452** the error.
23453*/
23454static void *sqlite3MemRealloc(void *pPrior, int nByte){
23455 struct MemBlockHdr *pOldHdr;
23456 void *pNew;
23457 assert( mem.disallow==0 )((void) (0));
23458 assert( (nByte & 7)==0 )((void) (0)); /* EV: R-46199-30249 */
23459 pOldHdr = sqlite3MemsysGetHeader(pPrior);
23460 pNew = sqlite3MemMalloc(nByte);
23461 if( pNew ){
23462 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
23463 if( nByte>pOldHdr->iSize ){
23464 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
23465 }
23466 sqlite3MemFree(pPrior);
23467 }
23468 return pNew;
23469}
23470
23471/*
23472** Populate the low-level memory allocation function pointers in
23473** sqlite3GlobalConfig.m with pointers to the routines in this file.
23474*/
23475SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
23476 static const sqlite3_mem_methods defaultMethods = {
23477 sqlite3MemMalloc,
23478 sqlite3MemFree,
23479 sqlite3MemRealloc,
23480 sqlite3MemSize,
23481 sqlite3MemRoundup,
23482 sqlite3MemInit,
23483 sqlite3MemShutdown,
23484 0
23485 };
23486 sqlite3_config(SQLITE_CONFIG_MALLOC4, &defaultMethods);
23487}
23488
23489/*
23490** Set the "type" of an allocation.
23491*/
23492SQLITE_PRIVATEstatic void sqlite3MemdebugSetType(void *p, u8 eType){
23493 if( p && sqlite3GlobalConfigsqlite3Config.m.xMalloc==sqlite3MemMalloc ){
23494 struct MemBlockHdr *pHdr;
23495 pHdr = sqlite3MemsysGetHeader(p);
23496 assert( pHdr->iForeGuard==FOREGUARD )((void) (0));
23497 pHdr->eType = eType;
23498 }
23499}
23500
23501/*
23502** Return TRUE if the mask of type in eType matches the type of the
23503** allocation p. Also return true if p==NULL.
23504**
23505** This routine is designed for use within an assert() statement, to
23506** verify the type of an allocation. For example:
23507**
23508** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
23509*/
23510SQLITE_PRIVATEstatic int sqlite3MemdebugHasType(void *p, u8 eType)1{
23511 int rc = 1;
23512 if( p && sqlite3GlobalConfigsqlite3Config.m.xMalloc==sqlite3MemMalloc ){
23513 struct MemBlockHdr *pHdr;
23514 pHdr = sqlite3MemsysGetHeader(p);
23515 assert( pHdr->iForeGuard==FOREGUARD )((void) (0)); /* Allocation is valid */
23516 if( (pHdr->eType&eType)==0 ){
23517 rc = 0;
23518 }
23519 }
23520 return rc;
23521}
23522
23523/*
23524** Return TRUE if the mask of type in eType matches no bits of the type of the
23525** allocation p. Also return true if p==NULL.
23526**
23527** This routine is designed for use within an assert() statement, to
23528** verify the type of an allocation. For example:
23529**
23530** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
23531*/
23532SQLITE_PRIVATEstatic int sqlite3MemdebugNoType(void *p, u8 eType)1{
23533 int rc = 1;
23534 if( p && sqlite3GlobalConfigsqlite3Config.m.xMalloc==sqlite3MemMalloc ){
23535 struct MemBlockHdr *pHdr;
23536 pHdr = sqlite3MemsysGetHeader(p);
23537 assert( pHdr->iForeGuard==FOREGUARD )((void) (0)); /* Allocation is valid */
23538 if( (pHdr->eType&eType)!=0 ){
23539 rc = 0;
23540 }
23541 }
23542 return rc;
23543}
23544
23545/*
23546** Set the number of backtrace levels kept for each allocation.
23547** A value of zero turns off backtracing. The number is always rounded
23548** up to a multiple of 2.
23549*/
23550SQLITE_PRIVATEstatic void sqlite3MemdebugBacktrace(int depth){
23551 if( depth<0 ){ depth = 0; }
23552 if( depth>20 ){ depth = 20; }
23553 depth = (depth+1)&0xfe;
23554 mem.nBacktrace = depth;
23555}
23556
23557SQLITE_PRIVATEstatic void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
23558 mem.xBacktrace = xBacktrace;
23559}
23560
23561/*
23562** Set the title string for subsequent allocations.
23563*/
23564SQLITE_PRIVATEstatic void sqlite3MemdebugSettitle(const char *zTitle){
23565 unsigned int n = sqlite3Strlen30(zTitle) + 1;
23566 sqlite3_mutex_enter(mem.mutex);
23567 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
23568 memcpy(mem.zTitle, zTitle, n);
23569 mem.zTitle[n] = 0;
23570 mem.nTitle = ROUND8(n)(((n)+7)&~7);
23571 sqlite3_mutex_leave(mem.mutex);
23572}
23573
23574SQLITE_PRIVATEstatic void sqlite3MemdebugSync(){
23575 struct MemBlockHdr *pHdr;
23576 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
23577 void **pBt = (void**)pHdr;
23578 pBt -= pHdr->nBacktraceSlots;
23579 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
23580 }
23581}
23582
23583/*
23584** Open the file indicated and write a log of all unfreed memory
23585** allocations into that log.
23586*/
23587SQLITE_PRIVATEstatic void sqlite3MemdebugDump(const char *zFilename){
23588 FILE *out;
23589 struct MemBlockHdr *pHdr;
23590 void **pBt;
23591 int i;
23592 out = fopen(zFilename, "w");
23593 if( out==0 ){
23594 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
23595 zFilename);
23596 return;
23597 }
23598 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
23599 char *z = (char*)pHdr;
23600 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
23601 fprintf(out, "**** %lld bytes at %p from %s ****\n",
23602 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
23603 if( pHdr->nBacktrace ){
23604 fflush(out);
23605 pBt = (void**)pHdr;
23606 pBt -= pHdr->nBacktraceSlots;
23607 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
23608 fprintf(out, "\n");
23609 }
23610 }
23611 fprintf(out, "COUNTS:\n");
23612 for(i=0; i<NCSIZE-1; i++){
23613 if( mem.nAlloc[i] ){
23614 fprintf(out, " %5d: %10d %10d %10d\n",
23615 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
23616 }
23617 }
23618 if( mem.nAlloc[NCSIZE-1] ){
23619 fprintf(out, " %5d: %10d %10d %10d\n",
23620 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
23621 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
23622 }
23623 fclose(out);
23624}
23625
23626/*
23627** Return the number of times sqlite3MemMalloc() has been called.
23628*/
23629SQLITE_PRIVATEstatic int sqlite3MemdebugMallocCount(){
23630 int i;
23631 int nTotal = 0;
23632 for(i=0; i<NCSIZE; i++){
23633 nTotal += mem.nAlloc[i];
23634 }
23635 return nTotal;
23636}
23637
23638
23639#endif /* SQLITE_MEMDEBUG */
23640
23641/************** End of mem2.c ************************************************/
23642/************** Begin file mem3.c ********************************************/
23643/*
23644** 2007 October 14
23645**
23646** The author disclaims copyright to this source code. In place of
23647** a legal notice, here is a blessing:
23648**
23649** May you do good and not evil.
23650** May you find forgiveness for yourself and forgive others.
23651** May you share freely, never taking more than you give.
23652**
23653*************************************************************************
23654** This file contains the C functions that implement a memory
23655** allocation subsystem for use by SQLite.
23656**
23657** This version of the memory allocation subsystem omits all
23658** use of malloc(). The SQLite user supplies a block of memory
23659** before calling sqlite3_initialize() from which allocations
23660** are made and returned by the xMalloc() and xRealloc()
23661** implementations. Once sqlite3_initialize() has been called,
23662** the amount of memory available to SQLite is fixed and cannot
23663** be changed.
23664**
23665** This version of the memory allocation subsystem is included
23666** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
23667*/
23668/* #include "sqliteInt.h" */
23669
23670/*
23671** This version of the memory allocator is only built into the library
23672** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
23673** mean that the library will use a memory-pool by default, just that
23674** it is available. The mempool allocator is activated by calling
23675** sqlite3_config().
23676*/
23677#ifdef SQLITE_ENABLE_MEMSYS3
23678
23679/*
23680** Maximum size (in Mem3Blocks) of a "small" chunk.
23681*/
23682#define MX_SMALL 10
23683
23684
23685/*
23686** Number of freelist hash slots
23687*/
23688#define N_HASH 61
23689
23690/*
23691** A memory allocation (also called a "chunk") consists of two or
23692** more blocks where each block is 8 bytes. The first 8 bytes are
23693** a header that is not returned to the user.
23694**
23695** A chunk is two or more blocks that is either checked out or
23696** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
23697** size of the allocation in blocks if the allocation is free.
23698** The u.hdr.size4x&1 bit is true if the chunk is checked out and
23699** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
23700** is true if the previous chunk is checked out and false if the
23701** previous chunk is free. The u.hdr.prevSize field is the size of
23702** the previous chunk in blocks if the previous chunk is on the
23703** freelist. If the previous chunk is checked out, then
23704** u.hdr.prevSize can be part of the data for that chunk and should
23705** not be read or written.
23706**
23707** We often identify a chunk by its index in mem3.aPool[]. When
23708** this is done, the chunk index refers to the second block of
23709** the chunk. In this way, the first chunk has an index of 1.
23710** A chunk index of 0 means "no such chunk" and is the equivalent
23711** of a NULL pointer.
23712**
23713** The second block of free chunks is of the form u.list. The
23714** two fields form a double-linked list of chunks of related sizes.
23715** Pointers to the head of the list are stored in mem3.aiSmall[]
23716** for smaller chunks and mem3.aiHash[] for larger chunks.
23717**
23718** The second block of a chunk is user data if the chunk is checked
23719** out. If a chunk is checked out, the user data may extend into
23720** the u.hdr.prevSize value of the following chunk.
23721*/
23722typedef struct Mem3Block Mem3Block;
23723struct Mem3Block {
23724 union {
23725 struct {
23726 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
23727 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
23728 } hdr;
23729 struct {
23730 u32 next; /* Index in mem3.aPool[] of next free chunk */
23731 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
23732 } list;
23733 } u;
23734};
23735
23736/*
23737** All of the static variables used by this module are collected
23738** into a single structure named "mem3". This is to keep the
23739** static variables organized and to reduce namespace pollution
23740** when this module is combined with other in the amalgamation.
23741*/
23742static SQLITE_WSD struct Mem3Global {
23743 /*
23744 ** Memory available for allocation. nPool is the size of the array
23745 ** (in Mem3Blocks) pointed to by aPool less 2.
23746 */
23747 u32 nPool;
23748 Mem3Block *aPool;
23749
23750 /*
23751 ** True if we are evaluating an out-of-memory callback.
23752 */
23753 int alarmBusy;
23754
23755 /*
23756 ** Mutex to control access to the memory allocation subsystem.
23757 */
23758 sqlite3_mutex *mutex;
23759
23760 /*
23761 ** The minimum amount of free space that we have seen.
23762 */
23763 u32 mnMaster;
23764
23765 /*
23766 ** iMaster is the index of the master chunk. Most new allocations
23767 ** occur off of this chunk. szMaster is the size (in Mem3Blocks)
23768 ** of the current master. iMaster is 0 if there is not master chunk.
23769 ** The master chunk is not in either the aiHash[] or aiSmall[].
23770 */
23771 u32 iMaster;
23772 u32 szMaster;
23773
23774 /*
23775 ** Array of lists of free blocks according to the block size
23776 ** for smaller chunks, or a hash on the block size for larger
23777 ** chunks.
23778 */
23779 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
23780 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
23781} mem3 = { 97535575 };
23782
23783#define mem3 GLOBAL(struct Mem3Global, mem3)mem3
23784
23785/*
23786** Unlink the chunk at mem3.aPool[i] from list it is currently
23787** on. *pRoot is the list that i is a member of.
23788*/
23789static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
23790 u32 next = mem3.aPool[i].u.list.next;
23791 u32 prev = mem3.aPool[i].u.list.prev;
23792 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
23793 if( prev==0 ){
23794 *pRoot = next;
23795 }else{
23796 mem3.aPool[prev].u.list.next = next;
23797 }
23798 if( next ){
23799 mem3.aPool[next].u.list.prev = prev;
23800 }
23801 mem3.aPool[i].u.list.next = 0;
23802 mem3.aPool[i].u.list.prev = 0;
23803}
23804
23805/*
23806** Unlink the chunk at index i from
23807** whatever list is currently a member of.
23808*/
23809static void memsys3Unlink(u32 i){
23810 u32 size, hash;
23811 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
23812 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 )((void) (0));
23813 assert( i>=1 )((void) (0));
23814 size = mem3.aPool[i-1].u.hdr.size4x/4;
23815 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize )((void) (0));
23816 assert( size>=2 )((void) (0));
23817 if( size <= MX_SMALL ){
23818 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
23819 }else{
23820 hash = size % N_HASH;
23821 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
23822 }
23823}
23824
23825/*
23826** Link the chunk at mem3.aPool[i] so that is on the list rooted
23827** at *pRoot.
23828*/
23829static void memsys3LinkIntoList(u32 i, u32 *pRoot){
23830 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
23831 mem3.aPool[i].u.list.next = *pRoot;
23832 mem3.aPool[i].u.list.prev = 0;
23833 if( *pRoot ){
23834 mem3.aPool[*pRoot].u.list.prev = i;
23835 }
23836 *pRoot = i;
23837}
23838
23839/*
23840** Link the chunk at index i into either the appropriate
23841** small chunk list, or into the large chunk hash table.
23842*/
23843static void memsys3Link(u32 i){
23844 u32 size, hash;
23845 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
23846 assert( i>=1 )((void) (0));
23847 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 )((void) (0));
23848 size = mem3.aPool[i-1].u.hdr.size4x/4;
23849 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize )((void) (0));
23850 assert( size>=2 )((void) (0));
23851 if( size <= MX_SMALL ){
23852 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
23853 }else{
23854 hash = size % N_HASH;
23855 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
23856 }
23857}
23858
23859/*
23860** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
23861** will already be held (obtained by code in malloc.c) if
23862** sqlite3GlobalConfig.bMemStat is true.
23863*/
23864static void memsys3Enter(void){
23865 if( sqlite3GlobalConfigsqlite3Config.bMemstat==0 && mem3.mutex==0 ){
23866 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
23867 }
23868 sqlite3_mutex_enter(mem3.mutex);
23869}
23870static void memsys3Leave(void){
23871 sqlite3_mutex_leave(mem3.mutex);
23872}
23873
23874/*
23875** Called when we are unable to satisfy an allocation of nBytes.
23876*/
23877static void memsys3OutOfMemory(int nByte){
23878 if( !mem3.alarmBusy ){
23879 mem3.alarmBusy = 1;
23880 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
23881 sqlite3_mutex_leave(mem3.mutex);
23882 sqlite3_release_memory(nByte);
23883 sqlite3_mutex_enter(mem3.mutex);
23884 mem3.alarmBusy = 0;
23885 }
23886}
23887
23888
23889/*
23890** Chunk i is a free chunk that has been unlinked. Adjust its
23891** size parameters for check-out and return a pointer to the
23892** user portion of the chunk.
23893*/
23894static void *memsys3Checkout(u32 i, u32 nBlock){
23895 u32 x;
23896 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
23897 assert( i>=1 )((void) (0));
23898 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock )((void) (0));
23899 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock )((void) (0));
23900 x = mem3.aPool[i-1].u.hdr.size4x;
23901 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
23902 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
23903 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
23904 return &mem3.aPool[i];
23905}
23906
23907/*
23908** Carve a piece off of the end of the mem3.iMaster free chunk.
23909** Return a pointer to the new allocation. Or, if the master chunk
23910** is not large enough, return 0.
23911*/
23912static void *memsys3FromMaster(u32 nBlock){
23913 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
23914 assert( mem3.szMaster>=nBlock )((void) (0));
23915 if( nBlock>=mem3.szMaster-1 ){
23916 /* Use the entire master */
23917 void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
23918 mem3.iMaster = 0;
23919 mem3.szMaster = 0;
23920 mem3.mnMaster = 0;
23921 return p;
23922 }else{
23923 /* Split the master block. Return the tail. */
23924 u32 newi, x;
23925 newi = mem3.iMaster + mem3.szMaster - nBlock;
23926 assert( newi > mem3.iMaster+1 )((void) (0));
23927 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
23928 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
23929 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
23930 mem3.szMaster -= nBlock;
23931 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
23932 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
23933 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
23934 if( mem3.szMaster < mem3.mnMaster ){
23935 mem3.mnMaster = mem3.szMaster;
23936 }
23937 return (void*)&mem3.aPool[newi];
23938 }
23939}
23940
23941/*
23942** *pRoot is the head of a list of free chunks of the same size
23943** or same size hash. In other words, *pRoot is an entry in either
23944** mem3.aiSmall[] or mem3.aiHash[].
23945**
23946** This routine examines all entries on the given list and tries
23947** to coalesce each entries with adjacent free chunks.
23948**
23949** If it sees a chunk that is larger than mem3.iMaster, it replaces
23950** the current mem3.iMaster with the new larger chunk. In order for
23951** this mem3.iMaster replacement to work, the master chunk must be
23952** linked into the hash tables. That is not the normal state of
23953** affairs, of course. The calling routine must link the master
23954** chunk before invoking this routine, then must unlink the (possibly
23955** changed) master chunk once this routine has finished.
23956*/
23957static void memsys3Merge(u32 *pRoot){
23958 u32 iNext, prev, size, i, x;
23959
23960 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
23961 for(i=*pRoot; i>0; i=iNext){
23962 iNext = mem3.aPool[i].u.list.next;
23963 size = mem3.aPool[i-1].u.hdr.size4x;
23964 assert( (size&1)==0 )((void) (0));
23965 if( (size&2)==0 ){
23966 memsys3UnlinkFromList(i, pRoot);
23967 assert( i > mem3.aPool[i-1].u.hdr.prevSize )((void) (0));
23968 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
23969 if( prev==iNext ){
23970 iNext = mem3.aPool[prev].u.list.next;
23971 }
23972 memsys3Unlink(prev);
23973 size = i + size/4 - prev;
23974 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
23975 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
23976 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
23977 memsys3Link(prev);
23978 i = prev;
23979 }else{
23980 size /= 4;
23981 }
23982 if( size>mem3.szMaster ){
23983 mem3.iMaster = i;
23984 mem3.szMaster = size;
23985 }
23986 }
23987}
23988
23989/*
23990** Return a block of memory of at least nBytes in size.
23991** Return NULL if unable.
23992**
23993** This function assumes that the necessary mutexes, if any, are
23994** already held by the caller. Hence "Unsafe".
23995*/
23996static void *memsys3MallocUnsafe(int nByte){
23997 u32 i;
23998 u32 nBlock;
23999 u32 toFree;
24000
24001 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
24002 assert( sizeof(Mem3Block)==8 )((void) (0));
24003 if( nByte<=12 ){
24004 nBlock = 2;
24005 }else{
24006 nBlock = (nByte + 11)/8;
24007 }
24008 assert( nBlock>=2 )((void) (0));
24009
24010 /* STEP 1:
24011 ** Look for an entry of the correct size in either the small
24012 ** chunk table or in the large chunk hash table. This is
24013 ** successful most of the time (about 9 times out of 10).
24014 */
24015 if( nBlock <= MX_SMALL ){
24016 i = mem3.aiSmall[nBlock-2];
24017 if( i>0 ){
24018 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
24019 return memsys3Checkout(i, nBlock);
24020 }
24021 }else{
24022 int hash = nBlock % N_HASH;
24023 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
24024 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
24025 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
24026 return memsys3Checkout(i, nBlock);
24027 }
24028 }
24029 }
24030
24031 /* STEP 2:
24032 ** Try to satisfy the allocation by carving a piece off of the end
24033 ** of the master chunk. This step usually works if step 1 fails.
24034 */
24035 if( mem3.szMaster>=nBlock ){
24036 return memsys3FromMaster(nBlock);
24037 }
24038
24039
24040 /* STEP 3:
24041 ** Loop through the entire memory pool. Coalesce adjacent free
24042 ** chunks. Recompute the master chunk as the largest free chunk.
24043 ** Then try again to satisfy the allocation by carving a piece off
24044 ** of the end of the master chunk. This step happens very
24045 ** rarely (we hope!)
24046 */
24047 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
24048 memsys3OutOfMemory(toFree);
24049 if( mem3.iMaster ){
24050 memsys3Link(mem3.iMaster);
24051 mem3.iMaster = 0;
24052 mem3.szMaster = 0;
24053 }
24054 for(i=0; i<N_HASH; i++){
24055 memsys3Merge(&mem3.aiHash[i]);
24056 }
24057 for(i=0; i<MX_SMALL-1; i++){
24058 memsys3Merge(&mem3.aiSmall[i]);
24059 }
24060 if( mem3.szMaster ){
24061 memsys3Unlink(mem3.iMaster);
24062 if( mem3.szMaster>=nBlock ){
24063 return memsys3FromMaster(nBlock);
24064 }
24065 }
24066 }
24067
24068 /* If none of the above worked, then we fail. */
24069 return 0;
24070}
24071
24072/*
24073** Free an outstanding memory allocation.
24074**
24075** This function assumes that the necessary mutexes, if any, are
24076** already held by the caller. Hence "Unsafe".
24077*/
24078static void memsys3FreeUnsafe(void *pOld){
24079 Mem3Block *p = (Mem3Block*)pOld;
24080 int i;
24081 u32 size, x;
24082 assert( sqlite3_mutex_held(mem3.mutex) )((void) (0));
24083 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] )((void) (0));
24084 i = p - mem3.aPool;
24085 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 )((void) (0));
24086 size = mem3.aPool[i-1].u.hdr.size4x/4;
24087 assert( i+size<=mem3.nPool+1 )((void) (0));
24088 mem3.aPool[i-1].u.hdr.size4x &= ~1;
24089 mem3.aPool[i+size-1].u.hdr.prevSize = size;
24090 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
24091 memsys3Link(i);
24092
24093 /* Try to expand the master using the newly freed chunk */
24094 if( mem3.iMaster ){
24095 while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
24096 size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
24097 mem3.iMaster -= size;
24098 mem3.szMaster += size;
24099 memsys3Unlink(mem3.iMaster);
24100 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
24101 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
24102 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
24103 }
24104 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
24105 while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
24106 memsys3Unlink(mem3.iMaster+mem3.szMaster);
24107 mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
24108 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
24109 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
24110 }
24111 }
24112}
24113
24114/*
24115** Return the size of an outstanding allocation, in bytes. The
24116** size returned omits the 8-byte header overhead. This only
24117** works for chunks that are currently checked out.
24118*/
24119static int memsys3Size(void *p){
24120 Mem3Block *pBlock;
24121 assert( p!=0 )((void) (0));
24122 pBlock = (Mem3Block*)p;
24123 assert( (pBlock[-1].u.hdr.size4x&1)!=0 )((void) (0));
24124 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
24125}
24126
24127/*
24128** Round up a request size to the next valid allocation size.
24129*/
24130static int memsys3Roundup(int n){
24131 if( n<=12 ){
24132 return 12;
24133 }else{
24134 return ((n+11)&~7) - 4;
24135 }
24136}
24137
24138/*
24139** Allocate nBytes of memory.
24140*/
24141static void *memsys3Malloc(int nBytes){
24142 sqlite3_int64 *p;
24143 assert( nBytes>0 )((void) (0)); /* malloc.c filters out 0 byte requests */
24144 memsys3Enter();
24145 p = memsys3MallocUnsafe(nBytes);
24146 memsys3Leave();
24147 return (void*)p;
24148}
24149
24150/*
24151** Free memory.
24152*/
24153static void memsys3Free(void *pPrior){
24154 assert( pPrior )((void) (0));
24155 memsys3Enter();
24156 memsys3FreeUnsafe(pPrior);
24157 memsys3Leave();
24158}
24159
24160/*
24161** Change the size of an existing memory allocation
24162*/
24163static void *memsys3Realloc(void *pPrior, int nBytes){
24164 int nOld;
24165 void *p;
24166 if( pPrior==0 ){
24167 return sqlite3_malloc(nBytes);
24168 }
24169 if( nBytes<=0 ){
24170 sqlite3_free(pPrior);
24171 return 0;
24172 }
24173 nOld = memsys3Size(pPrior);
24174 if( nBytes<=nOld && nBytes>=nOld-128 ){
24175 return pPrior;
24176 }
24177 memsys3Enter();
24178 p = memsys3MallocUnsafe(nBytes);
24179 if( p ){
24180 if( nOld<nBytes ){
24181 memcpy(p, pPrior, nOld);
24182 }else{
24183 memcpy(p, pPrior, nBytes);
24184 }
24185 memsys3FreeUnsafe(pPrior);
24186 }
24187 memsys3Leave();
24188 return p;
24189}
24190
24191/*
24192** Initialize this module.
24193*/
24194static int memsys3Init(void *NotUsed){
24195 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
24196 if( !sqlite3GlobalConfigsqlite3Config.pHeap ){
24197 return SQLITE_ERROR1;
24198 }
24199
24200 /* Store a pointer to the memory block in global structure mem3. */
24201 assert( sizeof(Mem3Block)==8 )((void) (0));
24202 mem3.aPool = (Mem3Block *)sqlite3GlobalConfigsqlite3Config.pHeap;
24203 mem3.nPool = (sqlite3GlobalConfigsqlite3Config.nHeap / sizeof(Mem3Block)) - 2;
24204
24205 /* Initialize the master block. */
24206 mem3.szMaster = mem3.nPool;
24207 mem3.mnMaster = mem3.szMaster;
24208 mem3.iMaster = 1;
24209 mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
24210 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
24211 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
24212
24213 return SQLITE_OK0;
24214}
24215
24216/*
24217** Deinitialize this module.
24218*/
24219static void memsys3Shutdown(void *NotUsed){
24220 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
24221 mem3.mutex = 0;
24222 return;
24223}
24224
24225
24226
24227/*
24228** Open the file indicated and write a log of all unfreed memory
24229** allocations into that log.
24230*/
24231SQLITE_PRIVATEstatic void sqlite3Memsys3Dump(const char *zFilename){
24232#ifdef SQLITE_DEBUG
24233 FILE *out;
24234 u32 i, j;
24235 u32 size;
24236 if( zFilename==0 || zFilename[0]==0 ){
24237 out = stdoutstdout;
24238 }else{
24239 out = fopen(zFilename, "w");
24240 if( out==0 ){
24241 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
24242 zFilename);
24243 return;
24244 }
24245 }
24246 memsys3Enter();
24247 fprintf(out, "CHUNKS:\n");
24248 for(i=1; i<=mem3.nPool; i+=size/4){
24249 size = mem3.aPool[i-1].u.hdr.size4x;
24250 if( size/4<=1 ){
24251 fprintf(out, "%p size error\n", &mem3.aPool[i]);
24252 assert( 0 )((void) (0));
24253 break;
24254 }
24255 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
24256 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
24257 assert( 0 )((void) (0));
24258 break;
24259 }
24260 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
24261 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
24262 assert( 0 )((void) (0));
24263 break;
24264 }
24265 if( size&1 ){
24266 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
24267 }else{
24268 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
24269 i==mem3.iMaster ? " **master**" : "");
24270 }
24271 }
24272 for(i=0; i<MX_SMALL-1; i++){
24273 if( mem3.aiSmall[i]==0 ) continue;
24274 fprintf(out, "small(%2d):", i);
24275 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
24276 fprintf(out, " %p(%d)", &mem3.aPool[j],
24277 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
24278 }
24279 fprintf(out, "\n");
24280 }
24281 for(i=0; i<N_HASH; i++){
24282 if( mem3.aiHash[i]==0 ) continue;
24283 fprintf(out, "hash(%2d):", i);
24284 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
24285 fprintf(out, " %p(%d)", &mem3.aPool[j],
24286 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
24287 }
24288 fprintf(out, "\n");
24289 }
24290 fprintf(out, "master=%d\n", mem3.iMaster);
24291 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
24292 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
24293 sqlite3_mutex_leave(mem3.mutex);
24294 if( out==stdoutstdout ){
24295 fflush(stdoutstdout);
24296 }else{
24297 fclose(out);
24298 }
24299#else
24300 UNUSED_PARAMETER(zFilename)(void)(zFilename);
24301#endif
24302}
24303
24304/*
24305** This routine is the only routine in this file with external
24306** linkage.
24307**
24308** Populate the low-level memory allocation function pointers in
24309** sqlite3GlobalConfig.m with pointers to the routines in this file. The
24310** arguments specify the block of memory to manage.
24311**
24312** This routine is only called by sqlite3_config(), and therefore
24313** is not required to be threadsafe (it is not).
24314*/
24315SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
24316 static const sqlite3_mem_methods mempoolMethods = {
24317 memsys3Malloc,
24318 memsys3Free,
24319 memsys3Realloc,
24320 memsys3Size,
24321 memsys3Roundup,
24322 memsys3Init,
24323 memsys3Shutdown,
24324 0
24325 };
24326 return &mempoolMethods;
24327}
24328
24329#endif /* SQLITE_ENABLE_MEMSYS3 */
24330
24331/************** End of mem3.c ************************************************/
24332/************** Begin file mem5.c ********************************************/
24333/*
24334** 2007 October 14
24335**
24336** The author disclaims copyright to this source code. In place of
24337** a legal notice, here is a blessing:
24338**
24339** May you do good and not evil.
24340** May you find forgiveness for yourself and forgive others.
24341** May you share freely, never taking more than you give.
24342**
24343*************************************************************************
24344** This file contains the C functions that implement a memory
24345** allocation subsystem for use by SQLite.
24346**
24347** This version of the memory allocation subsystem omits all
24348** use of malloc(). The application gives SQLite a block of memory
24349** before calling sqlite3_initialize() from which allocations
24350** are made and returned by the xMalloc() and xRealloc()
24351** implementations. Once sqlite3_initialize() has been called,
24352** the amount of memory available to SQLite is fixed and cannot
24353** be changed.
24354**
24355** This version of the memory allocation subsystem is included
24356** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
24357**
24358** This memory allocator uses the following algorithm:
24359**
24360** 1. All memory allocation sizes are rounded up to a power of 2.
24361**
24362** 2. If two adjacent free blocks are the halves of a larger block,
24363** then the two blocks are coalesced into the single larger block.
24364**
24365** 3. New memory is allocated from the first available free block.
24366**
24367** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
24368** Concerning Dynamic Storage Allocation". Journal of the Association for
24369** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
24370**
24371** Let n be the size of the largest allocation divided by the minimum
24372** allocation size (after rounding all sizes up to a power of 2.) Let M
24373** be the maximum amount of memory ever outstanding at one time. Let
24374** N be the total amount of memory available for allocation. Robson
24375** proved that this memory allocator will never breakdown due to
24376** fragmentation as long as the following constraint holds:
24377**
24378** N >= M*(1 + log2(n)/2) - n + 1
24379**
24380** The sqlite3_status() logic tracks the maximum values of n and M so
24381** that an application can, at any time, verify this constraint.
24382*/
24383/* #include "sqliteInt.h" */
24384
24385/*
24386** This version of the memory allocator is used only when
24387** SQLITE_ENABLE_MEMSYS5 is defined.
24388*/
24389#ifdef SQLITE_ENABLE_MEMSYS5
24390
24391/*
24392** A minimum allocation is an instance of the following structure.
24393** Larger allocations are an array of these structures where the
24394** size of the array is a power of 2.
24395**
24396** The size of this object must be a power of two. That fact is
24397** verified in memsys5Init().
24398*/
24399typedef struct Mem5Link Mem5Link;
24400struct Mem5Link {
24401 int next; /* Index of next free chunk */
24402 int prev; /* Index of previous free chunk */
24403};
24404
24405/*
24406** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
24407** mem5.szAtom is always at least 8 and 32-bit integers are used,
24408** it is not actually possible to reach this limit.
24409*/
24410#define LOGMAX 30
24411
24412/*
24413** Masks used for mem5.aCtrl[] elements.
24414*/
24415#define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
24416#define CTRL_FREE 0x20 /* True if not checked out */
24417
24418/*
24419** All of the static variables used by this module are collected
24420** into a single structure named "mem5". This is to keep the
24421** static variables organized and to reduce namespace pollution
24422** when this module is combined with other in the amalgamation.
24423*/
24424static SQLITE_WSD struct Mem5Global {
24425 /*
24426 ** Memory available for allocation
24427 */
24428 int szAtom; /* Smallest possible allocation in bytes */
24429 int nBlock; /* Number of szAtom sized blocks in zPool */
24430 u8 *zPool; /* Memory available to be allocated */
24431
24432 /*
24433 ** Mutex to control access to the memory allocation subsystem.
24434 */
24435 sqlite3_mutex *mutex;
24436
24437#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24438 /*
24439 ** Performance statistics
24440 */
24441 u64 nAlloc; /* Total number of calls to malloc */
24442 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
24443 u64 totalExcess; /* Total internal fragmentation */
24444 u32 currentOut; /* Current checkout, including internal fragmentation */
24445 u32 currentCount; /* Current number of distinct checkouts */
24446 u32 maxOut; /* Maximum instantaneous currentOut */
24447 u32 maxCount; /* Maximum instantaneous currentCount */
24448 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
24449#endif
24450
24451 /*
24452 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
24453 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
24454 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
24455 */
24456 int aiFreelist[LOGMAX+1];
24457
24458 /*
24459 ** Space for tracking which blocks are checked out and the size
24460 ** of each block. One byte per block.
24461 */
24462 u8 *aCtrl;
24463
24464} mem5;
24465
24466/*
24467** Access the static variable through a macro for SQLITE_OMIT_WSD.
24468*/
24469#define mem5 GLOBAL(struct Mem5Global, mem5)mem5
24470
24471/*
24472** Assuming mem5.zPool is divided up into an array of Mem5Link
24473** structures, return a pointer to the idx-th such link.
24474*/
24475#define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
24476
24477/*
24478** Unlink the chunk at mem5.aPool[i] from list it is currently
24479** on. It should be found on mem5.aiFreelist[iLogsize].
24480*/
24481static void memsys5Unlink(int i, int iLogsize){
24482 int next, prev;
24483 assert( i>=0 && i<mem5.nBlock )((void) (0));
24484 assert( iLogsize>=0 && iLogsize<=LOGMAX )((void) (0));
24485 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize )((void) (0));
24486
24487 next = MEM5LINK(i)->next;
24488 prev = MEM5LINK(i)->prev;
24489 if( prev<0 ){
24490 mem5.aiFreelist[iLogsize] = next;
24491 }else{
24492 MEM5LINK(prev)->next = next;
24493 }
24494 if( next>=0 ){
24495 MEM5LINK(next)->prev = prev;
24496 }
24497}
24498
24499/*
24500** Link the chunk at mem5.aPool[i] so that is on the iLogsize
24501** free list.
24502*/
24503static void memsys5Link(int i, int iLogsize){
24504 int x;
24505 assert( sqlite3_mutex_held(mem5.mutex) )((void) (0));
24506 assert( i>=0 && i<mem5.nBlock )((void) (0));
24507 assert( iLogsize>=0 && iLogsize<=LOGMAX )((void) (0));
24508 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize )((void) (0));
24509
24510 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
24511 MEM5LINK(i)->prev = -1;
24512 if( x>=0 ){
24513 assert( x<mem5.nBlock )((void) (0));
24514 MEM5LINK(x)->prev = i;
24515 }
24516 mem5.aiFreelist[iLogsize] = i;
24517}
24518
24519/*
24520** Obtain or release the mutex needed to access global data structures.
24521*/
24522static void memsys5Enter(void){
24523 sqlite3_mutex_enter(mem5.mutex);
24524}
24525static void memsys5Leave(void){
24526 sqlite3_mutex_leave(mem5.mutex);
24527}
24528
24529/*
24530** Return the size of an outstanding allocation, in bytes.
24531** This only works for chunks that are currently checked out.
24532*/
24533static int memsys5Size(void *p){
24534 int iSize, i;
24535 assert( p!=0 )((void) (0));
24536 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
24537 assert( i>=0 && i<mem5.nBlock )((void) (0));
24538 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
24539 return iSize;
24540}
24541
24542/*
24543** Return a block of memory of at least nBytes in size.
24544** Return NULL if unable. Return NULL if nBytes==0.
24545**
24546** The caller guarantees that nByte is positive.
24547**
24548** The caller has obtained a mutex prior to invoking this
24549** routine so there is never any chance that two or more
24550** threads can be in this routine at the same time.
24551*/
24552static void *memsys5MallocUnsafe(int nByte){
24553 int i; /* Index of a mem5.aPool[] slot */
24554 int iBin; /* Index into mem5.aiFreelist[] */
24555 int iFullSz; /* Size of allocation rounded up to power of 2 */
24556 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
24557
24558 /* nByte must be a positive */
24559 assert( nByte>0 )((void) (0));
24560
24561 /* No more than 1GiB per allocation */
24562 if( nByte > 0x40000000 ) return 0;
24563
24564#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24565 /* Keep track of the maximum allocation request. Even unfulfilled
24566 ** requests are counted */
24567 if( (u32)nByte>mem5.maxRequest ){
24568 mem5.maxRequest = nByte;
24569 }
24570#endif
24571
24572
24573 /* Round nByte up to the next valid power of two */
24574 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
24575
24576 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
24577 ** block. If not, then split a block of the next larger power of
24578 ** two in order to create a new free block of size iLogsize.
24579 */
24580 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
24581 if( iBin>LOGMAX ){
24582 testcase( sqlite3GlobalConfig.xLog!=0 );
24583 sqlite3_log(SQLITE_NOMEM7, "failed to allocate %u bytes", nByte);
24584 return 0;
24585 }
24586 i = mem5.aiFreelist[iBin];
24587 memsys5Unlink(i, iBin);
24588 while( iBin>iLogsize ){
24589 int newSize;
24590
24591 iBin--;
24592 newSize = 1 << iBin;
24593 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
24594 memsys5Link(i+newSize, iBin);
24595 }
24596 mem5.aCtrl[i] = iLogsize;
24597
24598#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24599 /* Update allocator performance statistics. */
24600 mem5.nAlloc++;
24601 mem5.totalAlloc += iFullSz;
24602 mem5.totalExcess += iFullSz - nByte;
24603 mem5.currentCount++;
24604 mem5.currentOut += iFullSz;
24605 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
24606 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
24607#endif
24608
24609#ifdef SQLITE_DEBUG
24610 /* Make sure the allocated memory does not assume that it is set to zero
24611 ** or retains a value from a previous allocation */
24612 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
24613#endif
24614
24615 /* Return a pointer to the allocated memory. */
24616 return (void*)&mem5.zPool[i*mem5.szAtom];
24617}
24618
24619/*
24620** Free an outstanding memory allocation.
24621*/
24622static void memsys5FreeUnsafe(void *pOld){
24623 u32 size, iLogsize;
24624 int iBlock;
24625
24626 /* Set iBlock to the index of the block pointed to by pOld in
24627 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
24628 */
24629 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
24630
24631 /* Check that the pointer pOld points to a valid, non-free block. */
24632 assert( iBlock>=0 && iBlock<mem5.nBlock )((void) (0));
24633 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 )((void) (0));
24634 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 )((void) (0));
24635
24636 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
24637 size = 1<<iLogsize;
24638 assert( iBlock+size-1<(u32)mem5.nBlock )((void) (0));
24639
24640 mem5.aCtrl[iBlock] |= CTRL_FREE;
24641 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
24642
24643#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
24644 assert( mem5.currentCount>0 )((void) (0));
24645 assert( mem5.currentOut>=(size*mem5.szAtom) )((void) (0));
24646 mem5.currentCount--;
24647 mem5.currentOut -= size*mem5.szAtom;
24648 assert( mem5.currentOut>0 || mem5.currentCount==0 )((void) (0));
24649 assert( mem5.currentCount>0 || mem5.currentOut==0 )((void) (0));
24650#endif
24651
24652 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
24653 while( ALWAYS(iLogsize<LOGMAX)(iLogsize<LOGMAX) ){
24654 int iBuddy;
24655 if( (iBlock>>iLogsize) & 1 ){
24656 iBuddy = iBlock - size;
24657 assert( iBuddy>=0 )((void) (0));
24658 }else{
24659 iBuddy = iBlock + size;
24660 if( iBuddy>=mem5.nBlock ) break;
24661 }
24662 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
24663 memsys5Unlink(iBuddy, iLogsize);
24664 iLogsize++;
24665 if( iBuddy<iBlock ){
24666 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
24667 mem5.aCtrl[iBlock] = 0;
24668 iBlock = iBuddy;
24669 }else{
24670 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
24671 mem5.aCtrl[iBuddy] = 0;
24672 }
24673 size *= 2;
24674 }
24675
24676#ifdef SQLITE_DEBUG
24677 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
24678 ** not used after being freed */
24679 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
24680#endif
24681
24682 memsys5Link(iBlock, iLogsize);
24683}
24684
24685/*
24686** Allocate nBytes of memory.
24687*/
24688static void *memsys5Malloc(int nBytes){
24689 sqlite3_int64 *p = 0;
24690 if( nBytes>0 ){
24691 memsys5Enter();
24692 p = memsys5MallocUnsafe(nBytes);
24693 memsys5Leave();
24694 }
24695 return (void*)p;
24696}
24697
24698/*
24699** Free memory.
24700**
24701** The outer layer memory allocator prevents this routine from
24702** being called with pPrior==0.
24703*/
24704static void memsys5Free(void *pPrior){
24705 assert( pPrior!=0 )((void) (0));
24706 memsys5Enter();
24707 memsys5FreeUnsafe(pPrior);
24708 memsys5Leave();
24709}
24710
24711/*
24712** Change the size of an existing memory allocation.
24713**
24714** The outer layer memory allocator prevents this routine from
24715** being called with pPrior==0.
24716**
24717** nBytes is always a value obtained from a prior call to
24718** memsys5Round(). Hence nBytes is always a non-negative power
24719** of two. If nBytes==0 that means that an oversize allocation
24720** (an allocation larger than 0x40000000) was requested and this
24721** routine should return 0 without freeing pPrior.
24722*/
24723static void *memsys5Realloc(void *pPrior, int nBytes){
24724 int nOld;
24725 void *p;
24726 assert( pPrior!=0 )((void) (0));
24727 assert( (nBytes&(nBytes-1))==0 )((void) (0)); /* EV: R-46199-30249 */
24728 assert( nBytes>=0 )((void) (0));
24729 if( nBytes==0 ){
24730 return 0;
24731 }
24732 nOld = memsys5Size(pPrior);
24733 if( nBytes<=nOld ){
24734 return pPrior;
24735 }
24736 p = memsys5Malloc(nBytes);
24737 if( p ){
24738 memcpy(p, pPrior, nOld);
24739 memsys5Free(pPrior);
24740 }
24741 return p;
24742}
24743
24744/*
24745** Round up a request size to the next valid allocation size. If
24746** the allocation is too large to be handled by this allocation system,
24747** return 0.
24748**
24749** All allocations must be a power of two and must be expressed by a
24750** 32-bit signed integer. Hence the largest allocation is 0x40000000
24751** or 1073741824 bytes.
24752*/
24753static int memsys5Roundup(int n){
24754 int iFullSz;
24755 if( n > 0x40000000 ) return 0;
24756 for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
24757 return iFullSz;
24758}
24759
24760/*
24761** Return the ceiling of the logarithm base 2 of iValue.
24762**
24763** Examples: memsys5Log(1) -> 0
24764** memsys5Log(2) -> 1
24765** memsys5Log(4) -> 2
24766** memsys5Log(5) -> 3
24767** memsys5Log(8) -> 3
24768** memsys5Log(9) -> 4
24769*/
24770static int memsys5Log(int iValue){
24771 int iLog;
24772 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
24773 return iLog;
24774}
24775
24776/*
24777** Initialize the memory allocator.
24778**
24779** This routine is not threadsafe. The caller must be holding a mutex
24780** to prevent multiple threads from entering at the same time.
24781*/
24782static int memsys5Init(void *NotUsed){
24783 int ii; /* Loop counter */
24784 int nByte; /* Number of bytes of memory available to this allocator */
24785 u8 *zByte; /* Memory usable by this allocator */
24786 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
24787 int iOffset; /* An offset into mem5.aCtrl[] */
24788
24789 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
24790
24791 /* For the purposes of this routine, disable the mutex */
24792 mem5.mutex = 0;
24793
24794 /* The size of a Mem5Link object must be a power of two. Verify that
24795 ** this is case.
24796 */
24797 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 )((void) (0));
24798
24799 nByte = sqlite3GlobalConfigsqlite3Config.nHeap;
24800 zByte = (u8*)sqlite3GlobalConfigsqlite3Config.pHeap;
24801 assert( zByte!=0 )((void) (0)); /* sqlite3_config() does not allow otherwise */
24802
24803 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
24804 nMinLog = memsys5Log(sqlite3GlobalConfigsqlite3Config.mnReq);
24805 mem5.szAtom = (1<<nMinLog);
24806 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
24807 mem5.szAtom = mem5.szAtom << 1;
24808 }
24809
24810 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
24811 mem5.zPool = zByte;
24812 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
24813
24814 for(ii=0; ii<=LOGMAX; ii++){
24815 mem5.aiFreelist[ii] = -1;
24816 }
24817
24818 iOffset = 0;
24819 for(ii=LOGMAX; ii>=0; ii--){
24820 int nAlloc = (1<<ii);
24821 if( (iOffset+nAlloc)<=mem5.nBlock ){
24822 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
24823 memsys5Link(iOffset, ii);
24824 iOffset += nAlloc;
24825 }
24826 assert((iOffset+nAlloc)>mem5.nBlock)((void) (0));
24827 }
24828
24829 /* If a mutex is required for normal operation, allocate one */
24830 if( sqlite3GlobalConfigsqlite3Config.bMemstat==0 ){
24831 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
24832 }
24833
24834 return SQLITE_OK0;
24835}
24836
24837/*
24838** Deinitialize this module.
24839*/
24840static void memsys5Shutdown(void *NotUsed){
24841 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
24842 mem5.mutex = 0;
24843 return;
24844}
24845
24846#ifdef SQLITE_TEST
24847/*
24848** Open the file indicated and write a log of all unfreed memory
24849** allocations into that log.
24850*/
24851SQLITE_PRIVATEstatic void sqlite3Memsys5Dump(const char *zFilename){
24852 FILE *out;
24853 int i, j, n;
24854 int nMinLog;
24855
24856 if( zFilename==0 || zFilename[0]==0 ){
24857 out = stdoutstdout;
24858 }else{
24859 out = fopen(zFilename, "w");
24860 if( out==0 ){
24861 fprintf(stderrstderr, "** Unable to output memory debug output log: %s **\n",
24862 zFilename);
24863 return;
24864 }
24865 }
24866 memsys5Enter();
24867 nMinLog = memsys5Log(mem5.szAtom);
24868 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
24869 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
24870 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
24871 }
24872 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
24873 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
24874 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
24875 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
24876 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
24877 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
24878 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
24879 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
24880 memsys5Leave();
24881 if( out==stdoutstdout ){
24882 fflush(stdoutstdout);
24883 }else{
24884 fclose(out);
24885 }
24886}
24887#endif
24888
24889/*
24890** This routine is the only routine in this file with external
24891** linkage. It returns a pointer to a static sqlite3_mem_methods
24892** struct populated with the memsys5 methods.
24893*/
24894SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
24895 static const sqlite3_mem_methods memsys5Methods = {
24896 memsys5Malloc,
24897 memsys5Free,
24898 memsys5Realloc,
24899 memsys5Size,
24900 memsys5Roundup,
24901 memsys5Init,
24902 memsys5Shutdown,
24903 0
24904 };
24905 return &memsys5Methods;
24906}
24907
24908#endif /* SQLITE_ENABLE_MEMSYS5 */
24909
24910/************** End of mem5.c ************************************************/
24911/************** Begin file mutex.c *******************************************/
24912/*
24913** 2007 August 14
24914**
24915** The author disclaims copyright to this source code. In place of
24916** a legal notice, here is a blessing:
24917**
24918** May you do good and not evil.
24919** May you find forgiveness for yourself and forgive others.
24920** May you share freely, never taking more than you give.
24921**
24922*************************************************************************
24923** This file contains the C functions that implement mutexes.
24924**
24925** This file contains code that is common across all mutex implementations.
24926*/
24927/* #include "sqliteInt.h" */
24928
24929#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
24930/*
24931** For debugging purposes, record when the mutex subsystem is initialized
24932** and uninitialized so that we can assert() if there is an attempt to
24933** allocate a mutex while the system is uninitialized.
24934*/
24935static SQLITE_WSD int mutexIsInit = 0;
24936#endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
24937
24938
24939#ifndef SQLITE_MUTEX_OMIT
24940
24941#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
24942/*
24943** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
24944** the implementation of a wrapper around the system default mutex
24945** implementation (sqlite3DefaultMutex()).
24946**
24947** Most calls are passed directly through to the underlying default
24948** mutex implementation. Except, if a mutex is configured by calling
24949** sqlite3MutexWarnOnContention() on it, then if contention is ever
24950** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
24951**
24952** This type of mutex is used as the database handle mutex when testing
24953** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
24954*/
24955
24956/*
24957** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
24958** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
24959** allocated by the system mutex implementation. Variable iType is usually set
24960** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
24961** or one of the static mutex identifiers. Or, if this is a recursive mutex
24962** that has been configured using sqlite3MutexWarnOnContention(), it is
24963** set to SQLITE_MUTEX_WARNONCONTENTION.
24964*/
24965typedef struct CheckMutex CheckMutex;
24966struct CheckMutex {
24967 int iType;
24968 sqlite3_mutex *mutex;
24969};
24970
24971#define SQLITE_MUTEX_WARNONCONTENTION (-1)
24972
24973/*
24974** Pointer to real mutex methods object used by the CheckMutex
24975** implementation. Set by checkMutexInit().
24976*/
24977static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
24978
24979#ifdef SQLITE_DEBUG
24980static int checkMutexHeld(sqlite3_mutex *p){
24981 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
24982}
24983static int checkMutexNotheld(sqlite3_mutex *p){
24984 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
24985}
24986#endif
24987
24988/*
24989** Initialize and deinitialize the mutex subsystem.
24990*/
24991static int checkMutexInit(void){
24992 pGlobalMutexMethods = sqlite3DefaultMutex();
24993 return SQLITE_OK0;
24994}
24995static int checkMutexEnd(void){
24996 pGlobalMutexMethods = 0;
24997 return SQLITE_OK0;
24998}
24999
25000/*
25001** Allocate a mutex.
25002*/
25003static sqlite3_mutex *checkMutexAlloc(int iType){
25004 static CheckMutex staticMutexes[] = {
25005 {2, 0}, {3, 0}, {4, 0}, {5, 0},
25006 {6, 0}, {7, 0}, {8, 0}, {9, 0},
25007 {10, 0}, {11, 0}, {12, 0}, {13, 0}
25008 };
25009 CheckMutex *p = 0;
25010
25011 assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 )((void) (0));
25012 if( iType<2 ){
25013 p = sqlite3MallocZero(sizeof(CheckMutex));
25014 if( p==0 ) return 0;
25015 p->iType = iType;
25016 }else{
25017#ifdef SQLITE_ENABLE_API_ARMOR
25018 if( iType-2>=ArraySize(staticMutexes)((int)(sizeof(staticMutexes)/sizeof(staticMutexes[0]))) ){
25019 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(25019);
25020 return 0;
25021 }
25022#endif
25023 p = &staticMutexes[iType-2];
25024 }
25025
25026 if( p->mutex==0 ){
25027 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
25028 if( p->mutex==0 ){
25029 if( iType<2 ){
25030 sqlite3_free(p);
25031 }
25032 p = 0;
25033 }
25034 }
25035
25036 return (sqlite3_mutex*)p;
25037}
25038
25039/*
25040** Free a mutex.
25041*/
25042static void checkMutexFree(sqlite3_mutex *p){
25043 assert( SQLITE_MUTEX_RECURSIVE<2 )((void) (0));
25044 assert( SQLITE_MUTEX_FAST<2 )((void) (0));
25045 assert( SQLITE_MUTEX_WARNONCONTENTION<2 )((void) (0));
25046
25047#if SQLITE_ENABLE_API_ARMOR
25048 if( ((CheckMutex*)p)->iType<2 )
25049#endif
25050 {
25051 CheckMutex *pCheck = (CheckMutex*)p;
25052 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
25053 sqlite3_free(pCheck);
25054 }
25055#ifdef SQLITE_ENABLE_API_ARMOR
25056 else{
25057 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(25057);
25058 }
25059#endif
25060}
25061
25062/*
25063** Enter the mutex.
25064*/
25065static void checkMutexEnter(sqlite3_mutex *p){
25066 CheckMutex *pCheck = (CheckMutex*)p;
25067 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
25068 if( SQLITE_OK0==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
25069 return;
25070 }
25071 sqlite3_log(SQLITE_MISUSE21,
25072 "illegal multi-threaded access to database connection"
25073 );
25074 }
25075 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
25076}
25077
25078/*
25079** Enter the mutex (do not block).
25080*/
25081static int checkMutexTry(sqlite3_mutex *p){
25082 CheckMutex *pCheck = (CheckMutex*)p;
25083 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
25084}
25085
25086/*
25087** Leave the mutex.
25088*/
25089static void checkMutexLeave(sqlite3_mutex *p){
25090 CheckMutex *pCheck = (CheckMutex*)p;
25091 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
25092}
25093
25094sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
25095 static const sqlite3_mutex_methods sMutex = {
25096 checkMutexInit,
25097 checkMutexEnd,
25098 checkMutexAlloc,
25099 checkMutexFree,
25100 checkMutexEnter,
25101 checkMutexTry,
25102 checkMutexLeave,
25103#ifdef SQLITE_DEBUG
25104 checkMutexHeld,
25105 checkMutexNotheld
25106#else
25107 0,
25108 0
25109#endif
25110 };
25111 return &sMutex;
25112}
25113
25114/*
25115** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
25116** one on which there should be no contention.
25117*/
25118SQLITE_PRIVATEstatic void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
25119 if( sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc==checkMutexAlloc ){
25120 CheckMutex *pCheck = (CheckMutex*)p;
25121 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE )((void) (0));
25122 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
25123 }
25124}
25125#endif /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
25126
25127/*
25128** Initialize the mutex system.
25129*/
25130SQLITE_PRIVATEstatic int sqlite3MutexInit(void){
25131 int rc = SQLITE_OK0;
25132 if( !sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc ){
25133 /* If the xMutexAlloc method has not been set, then the user did not
25134 ** install a mutex implementation via sqlite3_config() prior to
25135 ** sqlite3_initialize() being called. This block copies pointers to
25136 ** the default implementation into the sqlite3GlobalConfig structure.
25137 */
25138 sqlite3_mutex_methods const *pFrom;
25139 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfigsqlite3Config.mutex;
25140
25141 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
25142#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
25143 pFrom = multiThreadedCheckMutex();
25144#else
25145 pFrom = sqlite3DefaultMutex();
25146#endif
25147 }else{
25148 pFrom = sqlite3NoopMutex();
25149 }
25150 pTo->xMutexInit = pFrom->xMutexInit;
25151 pTo->xMutexEnd = pFrom->xMutexEnd;
25152 pTo->xMutexFree = pFrom->xMutexFree;
25153 pTo->xMutexEnter = pFrom->xMutexEnter;
25154 pTo->xMutexTry = pFrom->xMutexTry;
25155 pTo->xMutexLeave = pFrom->xMutexLeave;
25156 pTo->xMutexHeld = pFrom->xMutexHeld;
25157 pTo->xMutexNotheld = pFrom->xMutexNotheld;
25158 sqlite3MemoryBarrier();
25159 pTo->xMutexAlloc = pFrom->xMutexAlloc;
25160 }
25161 assert( sqlite3GlobalConfig.mutex.xMutexInit )((void) (0));
25162 rc = sqlite3GlobalConfigsqlite3Config.mutex.xMutexInit();
25163
25164#ifdef SQLITE_DEBUG
25165 GLOBAL(int, mutexIsInit)mutexIsInit = 1;
25166#endif
25167
25168 return rc;
25169}
25170
25171/*
25172** Shutdown the mutex system. This call frees resources allocated by
25173** sqlite3MutexInit().
25174*/
25175SQLITE_PRIVATEstatic int sqlite3MutexEnd(void){
25176 int rc = SQLITE_OK0;
25177 if( sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnd ){
25178 rc = sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnd();
25179 }
25180
25181#ifdef SQLITE_DEBUG
25182 GLOBAL(int, mutexIsInit)mutexIsInit = 0;
25183#endif
25184
25185 return rc;
25186}
25187
25188/*
25189** Retrieve a pointer to a static mutex or allocate a new dynamic one.
25190*/
25191SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
25192#ifndef SQLITE_OMIT_AUTOINIT
25193 if( id<=SQLITE_MUTEX_RECURSIVE1 && sqlite3_initialize() ) return 0;
25194 if( id>SQLITE_MUTEX_RECURSIVE1 && sqlite3MutexInit() ) return 0;
25195#endif
25196 assert( sqlite3GlobalConfig.mutex.xMutexAlloc )((void) (0));
25197 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc(id);
25198}
25199
25200SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MutexAlloc(int id){
25201 if( !sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
25202 return 0;
25203 }
25204 assert( GLOBAL(int, mutexIsInit) )((void) (0));
25205 assert( sqlite3GlobalConfig.mutex.xMutexAlloc )((void) (0));
25206 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexAlloc(id);
25207}
25208
25209/*
25210** Free a dynamic mutex.
25211*/
25212SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
25213 if( p ){
25214 assert( sqlite3GlobalConfig.mutex.xMutexFree )((void) (0));
25215 sqlite3GlobalConfigsqlite3Config.mutex.xMutexFree(p);
25216 }
25217}
25218
25219/*
25220** Obtain the mutex p. If some other thread already has the mutex, block
25221** until it can be obtained.
25222*/
25223SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
25224 if( p ){
25225 assert( sqlite3GlobalConfig.mutex.xMutexEnter )((void) (0));
25226 sqlite3GlobalConfigsqlite3Config.mutex.xMutexEnter(p);
25227 }
25228}
25229
25230/*
25231** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
25232** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
25233*/
25234SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
25235 int rc = SQLITE_OK0;
25236 if( p ){
25237 assert( sqlite3GlobalConfig.mutex.xMutexTry )((void) (0));
25238 return sqlite3GlobalConfigsqlite3Config.mutex.xMutexTry(p);
25239 }
25240 return rc;
25241}
25242
25243/*
25244** The sqlite3_mutex_leave() routine exits a mutex that was previously
25245** entered by the same thread. The behavior is undefined if the mutex
25246** is not currently entered. If a NULL pointer is passed as an argument
25247** this function is a no-op.
25248*/
25249SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
25250 if( p ){
25251 assert( sqlite3GlobalConfig.mutex.xMutexLeave )((void) (0));
25252 sqlite3GlobalConfigsqlite3Config.mutex.xMutexLeave(p);
25253 }
25254}
25255
25256#ifndef NDEBUG1
25257/*
25258** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25259** intended for use inside assert() statements.
25260*/
25261SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
25262 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld )((void) (0));
25263 return p==0 || sqlite3GlobalConfigsqlite3Config.mutex.xMutexHeld(p);
25264}
25265SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
25266 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld )((void) (0));
25267 return p==0 || sqlite3GlobalConfigsqlite3Config.mutex.xMutexNotheld(p);
25268}
25269#endif
25270
25271#endif /* !defined(SQLITE_MUTEX_OMIT) */
25272
25273/************** End of mutex.c ***********************************************/
25274/************** Begin file mutex_noop.c **************************************/
25275/*
25276** 2008 October 07
25277**
25278** The author disclaims copyright to this source code. In place of
25279** a legal notice, here is a blessing:
25280**
25281** May you do good and not evil.
25282** May you find forgiveness for yourself and forgive others.
25283** May you share freely, never taking more than you give.
25284**
25285*************************************************************************
25286** This file contains the C functions that implement mutexes.
25287**
25288** This implementation in this file does not provide any mutual
25289** exclusion and is thus suitable for use only in applications
25290** that use SQLite in a single thread. The routines defined
25291** here are place-holders. Applications can substitute working
25292** mutex routines at start-time using the
25293**
25294** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
25295**
25296** interface.
25297**
25298** If compiled with SQLITE_DEBUG, then additional logic is inserted
25299** that does error checking on mutexes to make sure they are being
25300** called correctly.
25301*/
25302/* #include "sqliteInt.h" */
25303
25304#ifndef SQLITE_MUTEX_OMIT
25305
25306#ifndef SQLITE_DEBUG
25307/*
25308** Stub routines for all mutex methods.
25309**
25310** This routines provide no mutual exclusion or error checking.
25311*/
25312static int noopMutexInit(void){ return SQLITE_OK0; }
25313static int noopMutexEnd(void){ return SQLITE_OK0; }
25314static sqlite3_mutex *noopMutexAlloc(int id){
25315 UNUSED_PARAMETER(id)(void)(id);
25316 return (sqlite3_mutex*)8;
25317}
25318static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
25319static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
25320static int noopMutexTry(sqlite3_mutex *p){
25321 UNUSED_PARAMETER(p)(void)(p);
25322 return SQLITE_OK0;
25323}
25324static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p)(void)(p); return; }
25325
25326SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void){
25327 static const sqlite3_mutex_methods sMutex = {
25328 noopMutexInit,
25329 noopMutexEnd,
25330 noopMutexAlloc,
25331 noopMutexFree,
25332 noopMutexEnter,
25333 noopMutexTry,
25334 noopMutexLeave,
25335
25336 0,
25337 0,
25338 };
25339
25340 return &sMutex;
25341}
25342#endif /* !SQLITE_DEBUG */
25343
25344#ifdef SQLITE_DEBUG
25345/*
25346** In this implementation, error checking is provided for testing
25347** and debugging purposes. The mutexes still do not provide any
25348** mutual exclusion.
25349*/
25350
25351/*
25352** The mutex object
25353*/
25354typedef struct sqlite3_debug_mutex {
25355 int id; /* The mutex type */
25356 int cnt; /* Number of entries without a matching leave */
25357} sqlite3_debug_mutex;
25358
25359/*
25360** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25361** intended for use inside assert() statements.
25362*/
25363static int debugMutexHeld(sqlite3_mutex *pX){
25364 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25365 return p==0 || p->cnt>0;
25366}
25367static int debugMutexNotheld(sqlite3_mutex *pX){
25368 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25369 return p==0 || p->cnt==0;
25370}
25371
25372/*
25373** Initialize and deinitialize the mutex subsystem.
25374*/
25375static int debugMutexInit(void){ return SQLITE_OK0; }
25376static int debugMutexEnd(void){ return SQLITE_OK0; }
25377
25378/*
25379** The sqlite3_mutex_alloc() routine allocates a new
25380** mutex and returns a pointer to it. If it returns NULL
25381** that means that a mutex could not be allocated.
25382*/
25383static sqlite3_mutex *debugMutexAlloc(int id){
25384 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS313 - 1];
25385 sqlite3_debug_mutex *pNew = 0;
25386 switch( id ){
25387 case SQLITE_MUTEX_FAST0:
25388 case SQLITE_MUTEX_RECURSIVE1: {
25389 pNew = sqlite3Malloc(sizeof(*pNew));
25390 if( pNew ){
25391 pNew->id = id;
25392 pNew->cnt = 0;
25393 }
25394 break;
25395 }
25396 default: {
25397#ifdef SQLITE_ENABLE_API_ARMOR
25398 if( id-2<0 || id-2>=ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
25399 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(25399);
25400 return 0;
25401 }
25402#endif
25403 pNew = &aStatic[id-2];
25404 pNew->id = id;
25405 break;
25406 }
25407 }
25408 return (sqlite3_mutex*)pNew;
25409}
25410
25411/*
25412** This routine deallocates a previously allocated mutex.
25413*/
25414static void debugMutexFree(sqlite3_mutex *pX){
25415 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25416 assert( p->cnt==0 )((void) (0));
25417 if( p->id==SQLITE_MUTEX_RECURSIVE1 || p->id==SQLITE_MUTEX_FAST0 ){
25418 sqlite3_free(p);
25419 }else{
25420#ifdef SQLITE_ENABLE_API_ARMOR
25421 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(25421);
25422#endif
25423 }
25424}
25425
25426/*
25427** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
25428** to enter a mutex. If another thread is already within the mutex,
25429** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
25430** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
25431** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
25432** be entered multiple times by the same thread. In such cases the,
25433** mutex must be exited an equal number of times before another thread
25434** can enter. If the same thread tries to enter any other kind of mutex
25435** more than once, the behavior is undefined.
25436*/
25437static void debugMutexEnter(sqlite3_mutex *pX){
25438 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25439 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
25440 p->cnt++;
25441}
25442static int debugMutexTry(sqlite3_mutex *pX){
25443 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25444 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
25445 p->cnt++;
25446 return SQLITE_OK0;
25447}
25448
25449/*
25450** The sqlite3_mutex_leave() routine exits a mutex that was
25451** previously entered by the same thread. The behavior
25452** is undefined if the mutex is not currently entered or
25453** is not currently allocated. SQLite will never do either.
25454*/
25455static void debugMutexLeave(sqlite3_mutex *pX){
25456 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
25457 assert( debugMutexHeld(pX) )((void) (0));
25458 p->cnt--;
25459 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) )((void) (0));
25460}
25461
25462SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3NoopMutex(void){
25463 static const sqlite3_mutex_methods sMutex = {
25464 debugMutexInit,
25465 debugMutexEnd,
25466 debugMutexAlloc,
25467 debugMutexFree,
25468 debugMutexEnter,
25469 debugMutexTry,
25470 debugMutexLeave,
25471
25472 debugMutexHeld,
25473 debugMutexNotheld
25474 };
25475
25476 return &sMutex;
25477}
25478#endif /* SQLITE_DEBUG */
25479
25480/*
25481** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
25482** is used regardless of the run-time threadsafety setting.
25483*/
25484#ifdef SQLITE_MUTEX_NOOP
25485SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
25486 return sqlite3NoopMutex();
25487}
25488#endif /* defined(SQLITE_MUTEX_NOOP) */
25489#endif /* !defined(SQLITE_MUTEX_OMIT) */
25490
25491/************** End of mutex_noop.c ******************************************/
25492/************** Begin file mutex_unix.c **************************************/
25493/*
25494** 2007 August 28
25495**
25496** The author disclaims copyright to this source code. In place of
25497** a legal notice, here is a blessing:
25498**
25499** May you do good and not evil.
25500** May you find forgiveness for yourself and forgive others.
25501** May you share freely, never taking more than you give.
25502**
25503*************************************************************************
25504** This file contains the C functions that implement mutexes for pthreads
25505*/
25506/* #include "sqliteInt.h" */
25507
25508/*
25509** The code in this file is only used if we are compiling threadsafe
25510** under unix with pthreads.
25511**
25512** Note that this implementation requires a version of pthreads that
25513** supports recursive mutexes.
25514*/
25515#ifdef SQLITE_MUTEX_PTHREADS
25516
25517#include <pthread.h>
25518
25519/*
25520** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
25521** are necessary under two condidtions: (1) Debug builds and (2) using
25522** home-grown mutexes. Encapsulate these conditions into a single #define.
25523*/
25524#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
25525# define SQLITE_MUTEX_NREF0 1
25526#else
25527# define SQLITE_MUTEX_NREF0 0
25528#endif
25529
25530/*
25531** Each recursive mutex is an instance of the following structure.
25532*/
25533struct sqlite3_mutex {
25534 pthread_mutex_t mutex; /* Mutex controlling the lock */
25535#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR)
25536 int id; /* Mutex type */
25537#endif
25538#if SQLITE_MUTEX_NREF0
25539 volatile int nRef; /* Number of entrances */
25540 volatile pthread_t owner; /* Thread that is within this mutex */
25541 int trace; /* True to trace changes */
25542#endif
25543};
25544#if SQLITE_MUTEX_NREF0
25545# define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } } \
25546 {PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } },id,0,(pthread_t)0,0}
25547#elif defined(SQLITE_ENABLE_API_ARMOR)
25548# define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } } { PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } }, id }
25549#else
25550#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } } { PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } }
25551#endif
25552
25553/*
25554** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
25555** intended for use only inside assert() statements. On some platforms,
25556** there might be race conditions that can cause these routines to
25557** deliver incorrect results. In particular, if pthread_equal() is
25558** not an atomic operation, then these routines might delivery
25559** incorrect results. On most platforms, pthread_equal() is a
25560** comparison of two integers and is therefore atomic. But we are
25561** told that HPUX is not such a platform. If so, then these routines
25562** will not always work correctly on HPUX.
25563**
25564** On those platforms where pthread_equal() is not atomic, SQLite
25565** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
25566** make sure no assert() statements are evaluated and hence these
25567** routines are never called.
25568*/
25569#if !defined(NDEBUG1) || defined(SQLITE_DEBUG)
25570static int pthreadMutexHeld(sqlite3_mutex *p){
25571 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
25572}
25573static int pthreadMutexNotheld(sqlite3_mutex *p){
25574 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
25575}
25576#endif
25577
25578/*
25579** Try to provide a memory barrier operation, needed for initialization
25580** and also for the implementation of xShmBarrier in the VFS in cases
25581** where SQLite is compiled without mutexes.
25582*/
25583SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void){
25584#if defined(SQLITE_MEMORY_BARRIER)
25585 SQLITE_MEMORY_BARRIER;
25586#elif defined(__GNUC__4) && GCC_VERSION(4*1000000+2*1000+1)>=4001000
25587 __sync_synchronize();
25588#endif
25589}
25590
25591/*
25592** Initialize and deinitialize the mutex subsystem.
25593*/
25594static int pthreadMutexInit(void){ return SQLITE_OK0; }
25595static int pthreadMutexEnd(void){ return SQLITE_OK0; }
25596
25597/*
25598** The sqlite3_mutex_alloc() routine allocates a new
25599** mutex and returns a pointer to it. If it returns NULL
25600** that means that a mutex could not be allocated. SQLite
25601** will unwind its stack and return an error. The argument
25602** to sqlite3_mutex_alloc() is one of these integer constants:
25603**
25604** <ul>
25605** <li> SQLITE_MUTEX_FAST
25606** <li> SQLITE_MUTEX_RECURSIVE
25607** <li> SQLITE_MUTEX_STATIC_MASTER
25608** <li> SQLITE_MUTEX_STATIC_MEM
25609** <li> SQLITE_MUTEX_STATIC_OPEN
25610** <li> SQLITE_MUTEX_STATIC_PRNG
25611** <li> SQLITE_MUTEX_STATIC_LRU
25612** <li> SQLITE_MUTEX_STATIC_PMEM
25613** <li> SQLITE_MUTEX_STATIC_APP1
25614** <li> SQLITE_MUTEX_STATIC_APP2
25615** <li> SQLITE_MUTEX_STATIC_APP3
25616** <li> SQLITE_MUTEX_STATIC_VFS1
25617** <li> SQLITE_MUTEX_STATIC_VFS2
25618** <li> SQLITE_MUTEX_STATIC_VFS3
25619** </ul>
25620**
25621** The first two constants cause sqlite3_mutex_alloc() to create
25622** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
25623** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
25624** The mutex implementation does not need to make a distinction
25625** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
25626** not want to. But SQLite will only request a recursive mutex in
25627** cases where it really needs one. If a faster non-recursive mutex
25628** implementation is available on the host platform, the mutex subsystem
25629** might return such a mutex in response to SQLITE_MUTEX_FAST.
25630**
25631** The other allowed parameters to sqlite3_mutex_alloc() each return
25632** a pointer to a static preexisting mutex. Six static mutexes are
25633** used by the current version of SQLite. Future versions of SQLite
25634** may add additional static mutexes. Static mutexes are for internal
25635** use by SQLite only. Applications that use SQLite mutexes should
25636** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
25637** SQLITE_MUTEX_RECURSIVE.
25638**
25639** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
25640** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
25641** returns a different mutex on every call. But for the static
25642** mutex types, the same mutex is returned on every call that has
25643** the same type number.
25644*/
25645static sqlite3_mutex *pthreadMutexAlloc(int iType){
25646 static sqlite3_mutex staticMutexes[] = {
25647 SQLITE3_MUTEX_INITIALIZER(2){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25648 SQLITE3_MUTEX_INITIALIZER(3){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25649 SQLITE3_MUTEX_INITIALIZER(4){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25650 SQLITE3_MUTEX_INITIALIZER(5){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25651 SQLITE3_MUTEX_INITIALIZER(6){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25652 SQLITE3_MUTEX_INITIALIZER(7){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25653 SQLITE3_MUTEX_INITIALIZER(8){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25654 SQLITE3_MUTEX_INITIALIZER(9){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25655 SQLITE3_MUTEX_INITIALIZER(10){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25656 SQLITE3_MUTEX_INITIALIZER(11){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25657 SQLITE3_MUTEX_INITIALIZER(12){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
25658 SQLITE3_MUTEX_INITIALIZER(13){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } }
25659 };
25660 sqlite3_mutex *p;
25661 switch( iType ){
25662 case SQLITE_MUTEX_RECURSIVE1: {
25663 p = sqlite3MallocZero( sizeof(*p) );
25664 if( p ){
25665#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25666 /* If recursive mutexes are not available, we will have to
25667 ** build our own. See below. */
25668 pthread_mutex_init(&p->mutex, 0);
25669#else
25670 /* Use a recursive mutex if it is available */
25671 pthread_mutexattr_t recursiveAttr;
25672 pthread_mutexattr_init(&recursiveAttr);
25673 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
25674 pthread_mutex_init(&p->mutex, &recursiveAttr);
25675 pthread_mutexattr_destroy(&recursiveAttr);
25676#endif
25677#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR)
25678 p->id = SQLITE_MUTEX_RECURSIVE1;
25679#endif
25680 }
25681 break;
25682 }
25683 case SQLITE_MUTEX_FAST0: {
25684 p = sqlite3MallocZero( sizeof(*p) );
25685 if( p ){
25686 pthread_mutex_init(&p->mutex, 0);
25687#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR)
25688 p->id = SQLITE_MUTEX_FAST0;
25689#endif
25690 }
25691 break;
25692 }
25693 default: {
25694#ifdef SQLITE_ENABLE_API_ARMOR
25695 if( iType-2<0 || iType-2>=ArraySize(staticMutexes)((int)(sizeof(staticMutexes)/sizeof(staticMutexes[0]))) ){
25696 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(25696);
25697 return 0;
25698 }
25699#endif
25700 p = &staticMutexes[iType-2];
25701 break;
25702 }
25703 }
25704#if SQLITE_MUTEX_NREF0 || defined(SQLITE_ENABLE_API_ARMOR)
25705 assert( p==0 || p->id==iType )((void) (0));
25706#endif
25707 return p;
25708}
25709
25710
25711/*
25712** This routine deallocates a previously
25713** allocated mutex. SQLite is careful to deallocate every
25714** mutex that it allocates.
25715*/
25716static void pthreadMutexFree(sqlite3_mutex *p){
25717 assert( p->nRef==0 )((void) (0));
25718#if SQLITE_ENABLE_API_ARMOR
25719 if( p->id==SQLITE_MUTEX_FAST0 || p->id==SQLITE_MUTEX_RECURSIVE1 )
25720#endif
25721 {
25722 pthread_mutex_destroy(&p->mutex);
25723 sqlite3_free(p);
25724 }
25725#ifdef SQLITE_ENABLE_API_ARMOR
25726 else{
25727 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(25727);
25728 }
25729#endif
25730}
25731
25732/*
25733** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
25734** to enter a mutex. If another thread is already within the mutex,
25735** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
25736** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
25737** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
25738** be entered multiple times by the same thread. In such cases the,
25739** mutex must be exited an equal number of times before another thread
25740** can enter. If the same thread tries to enter any other kind of mutex
25741** more than once, the behavior is undefined.
25742*/
25743static void pthreadMutexEnter(sqlite3_mutex *p){
25744 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) )((void) (0));
25745
25746#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25747 /* If recursive mutexes are not available, then we have to grow
25748 ** our own. This implementation assumes that pthread_equal()
25749 ** is atomic - that it cannot be deceived into thinking self
25750 ** and p->owner are equal if p->owner changes between two values
25751 ** that are not equal to self while the comparison is taking place.
25752 ** This implementation also assumes a coherent cache - that
25753 ** separate processes cannot read different values from the same
25754 ** address at the same time. If either of these two conditions
25755 ** are not met, then the mutexes will fail and problems will result.
25756 */
25757 {
25758 pthread_t self = pthread_self();
25759 if( p->nRef>0 && pthread_equal(p->owner, self) ){
25760 p->nRef++;
25761 }else{
25762 pthread_mutex_lock(&p->mutex);
25763 assert( p->nRef==0 )((void) (0));
25764 p->owner = self;
25765 p->nRef = 1;
25766 }
25767 }
25768#else
25769 /* Use the built-in recursive mutexes if they are available.
25770 */
25771 pthread_mutex_lock(&p->mutex);
25772#if SQLITE_MUTEX_NREF0
25773 assert( p->nRef>0 || p->owner==0 )((void) (0));
25774 p->owner = pthread_self();
25775 p->nRef++;
25776#endif
25777#endif
25778
25779#ifdef SQLITE_DEBUG
25780 if( p->trace ){
25781 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
25782 }
25783#endif
25784}
25785static int pthreadMutexTry(sqlite3_mutex *p){
25786 int rc;
25787 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) )((void) (0));
25788
25789#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25790 /* If recursive mutexes are not available, then we have to grow
25791 ** our own. This implementation assumes that pthread_equal()
25792 ** is atomic - that it cannot be deceived into thinking self
25793 ** and p->owner are equal if p->owner changes between two values
25794 ** that are not equal to self while the comparison is taking place.
25795 ** This implementation also assumes a coherent cache - that
25796 ** separate processes cannot read different values from the same
25797 ** address at the same time. If either of these two conditions
25798 ** are not met, then the mutexes will fail and problems will result.
25799 */
25800 {
25801 pthread_t self = pthread_self();
25802 if( p->nRef>0 && pthread_equal(p->owner, self) ){
25803 p->nRef++;
25804 rc = SQLITE_OK0;
25805 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
25806 assert( p->nRef==0 )((void) (0));
25807 p->owner = self;
25808 p->nRef = 1;
25809 rc = SQLITE_OK0;
25810 }else{
25811 rc = SQLITE_BUSY5;
25812 }
25813 }
25814#else
25815 /* Use the built-in recursive mutexes if they are available.
25816 */
25817 if( pthread_mutex_trylock(&p->mutex)==0 ){
25818#if SQLITE_MUTEX_NREF0
25819 p->owner = pthread_self();
25820 p->nRef++;
25821#endif
25822 rc = SQLITE_OK0;
25823 }else{
25824 rc = SQLITE_BUSY5;
25825 }
25826#endif
25827
25828#ifdef SQLITE_DEBUG
25829 if( rc==SQLITE_OK0 && p->trace ){
25830 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
25831 }
25832#endif
25833 return rc;
25834}
25835
25836/*
25837** The sqlite3_mutex_leave() routine exits a mutex that was
25838** previously entered by the same thread. The behavior
25839** is undefined if the mutex is not currently entered or
25840** is not currently allocated. SQLite will never do either.
25841*/
25842static void pthreadMutexLeave(sqlite3_mutex *p){
25843 assert( pthreadMutexHeld(p) )((void) (0));
25844#if SQLITE_MUTEX_NREF0
25845 p->nRef--;
25846 if( p->nRef==0 ) p->owner = 0;
25847#endif
25848 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE )((void) (0));
25849
25850#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
25851 if( p->nRef==0 ){
25852 pthread_mutex_unlock(&p->mutex);
25853 }
25854#else
25855 pthread_mutex_unlock(&p->mutex);
25856#endif
25857
25858#ifdef SQLITE_DEBUG
25859 if( p->trace ){
25860 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
25861 }
25862#endif
25863}
25864
25865SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
25866 static const sqlite3_mutex_methods sMutex = {
25867 pthreadMutexInit,
25868 pthreadMutexEnd,
25869 pthreadMutexAlloc,
25870 pthreadMutexFree,
25871 pthreadMutexEnter,
25872 pthreadMutexTry,
25873 pthreadMutexLeave,
25874#ifdef SQLITE_DEBUG
25875 pthreadMutexHeld,
25876 pthreadMutexNotheld
25877#else
25878 0,
25879 0
25880#endif
25881 };
25882
25883 return &sMutex;
25884}
25885
25886#endif /* SQLITE_MUTEX_PTHREADS */
25887
25888/************** End of mutex_unix.c ******************************************/
25889/************** Begin file mutex_w32.c ***************************************/
25890/*
25891** 2007 August 14
25892**
25893** The author disclaims copyright to this source code. In place of
25894** a legal notice, here is a blessing:
25895**
25896** May you do good and not evil.
25897** May you find forgiveness for yourself and forgive others.
25898** May you share freely, never taking more than you give.
25899**
25900*************************************************************************
25901** This file contains the C functions that implement mutexes for Win32.
25902*/
25903/* #include "sqliteInt.h" */
25904
25905#if SQLITE_OS_WIN0
25906/*
25907** Include code that is common to all os_*.c files
25908*/
25909/************** Include os_common.h in the middle of mutex_w32.c *************/
25910/************** Begin file os_common.h ***************************************/
25911/*
25912** 2004 May 22
25913**
25914** The author disclaims copyright to this source code. In place of
25915** a legal notice, here is a blessing:
25916**
25917** May you do good and not evil.
25918** May you find forgiveness for yourself and forgive others.
25919** May you share freely, never taking more than you give.
25920**
25921******************************************************************************
25922**
25923** This file contains macros and a little bit of code that is common to
25924** all of the platform-specific files (os_*.c) and is #included into those
25925** files.
25926**
25927** This file should be #included by the os_*.c files only. It is not a
25928** general purpose header file.
25929*/
25930#ifndef _OS_COMMON_H_
25931#define _OS_COMMON_H_
25932
25933/*
25934** At least two bugs have slipped in because we changed the MEMORY_DEBUG
25935** macro to SQLITE_DEBUG and some older makefiles have not yet made the
25936** switch. The following code should catch this problem at compile-time.
25937*/
25938#ifdef MEMORY_DEBUG
25939# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
25940#endif
25941
25942/*
25943** Macros for performance tracing. Normally turned off. Only works
25944** on i486 hardware.
25945*/
25946#ifdef SQLITE_PERFORMANCE_TRACE
25947
25948/*
25949** hwtime.h contains inline assembler code for implementing
25950** high-performance timing routines.
25951*/
25952/************** Include hwtime.h in the middle of os_common.h ****************/
25953/************** Begin file hwtime.h ******************************************/
25954/*
25955** 2008 May 27
25956**
25957** The author disclaims copyright to this source code. In place of
25958** a legal notice, here is a blessing:
25959**
25960** May you do good and not evil.
25961** May you find forgiveness for yourself and forgive others.
25962** May you share freely, never taking more than you give.
25963**
25964******************************************************************************
25965**
25966** This file contains inline asm code for retrieving "high-performance"
25967** counters for x86 class CPUs.
25968*/
25969#ifndef SQLITE_HWTIME_H
25970#define SQLITE_HWTIME_H
25971
25972/*
25973** The following routine only works on pentium-class (or newer) processors.
25974** It uses the RDTSC opcode to read the cycle count value out of the
25975** processor and returns that value. This can be used for high-res
25976** profiling.
25977*/
25978#if (defined(__GNUC__4) || defined(_MSC_VER)) && \
25979 (defined(i386) || defined(__i386__) || defined(_M_IX86))
25980
25981 #if defined(__GNUC__4)
25982
25983 __inline__ sqlite_uint64 sqlite3Hwtime(void){
25984 unsigned int lo, hi;
25985 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
25986 return (sqlite_uint64)hi << 32 | lo;
25987 }
25988
25989 #elif defined(_MSC_VER)
25990
25991 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
25992 __asm {
25993 rdtsc
25994 ret ; return value at EDX:EAX
25995 }
25996 }
25997
25998 #endif
25999
26000#elif (defined(__GNUC__4) && defined(__x86_64__1))
26001
26002 __inline__ sqlite_uint64 sqlite3Hwtime(void){
26003 unsigned long val;
26004 __asm__ __volatile__ ("rdtsc" : "=A" (val));
26005 return val;
26006 }
26007
26008#elif (defined(__GNUC__4) && defined(__ppc__))
26009
26010 __inline__ sqlite_uint64 sqlite3Hwtime(void){
26011 unsigned long long retval;
26012 unsigned long junk;
26013 __asm__ __volatile__ ("\n\
26014 1: mftbu %1\n\
26015 mftb %L0\n\
26016 mftbu %0\n\
26017 cmpw %0,%1\n\
26018 bne 1b"
26019 : "=r" (retval), "=r" (junk));
26020 return retval;
26021 }
26022
26023#else
26024
26025 #error Need implementation of sqlite3Hwtime() for your platform.
26026
26027 /*
26028 ** To compile without implementing sqlite3Hwtime() for your platform,
26029 ** you can remove the above #error and use the following
26030 ** stub function. You will lose timing support for many
26031 ** of the debugging and testing utilities, but it should at
26032 ** least compile and run.
26033 */
26034SQLITE_PRIVATEstatic sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
26035
26036#endif
26037
26038#endif /* !defined(SQLITE_HWTIME_H) */
26039
26040/************** End of hwtime.h **********************************************/
26041/************** Continuing where we left off in os_common.h ******************/
26042
26043static sqlite_uint64 g_start;
26044static sqlite_uint64 g_elapsed;
26045#define TIMER_START g_start=sqlite3Hwtime()
26046#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
26047#define TIMER_ELAPSED((sqlite_uint64)0) g_elapsed
26048#else
26049#define TIMER_START
26050#define TIMER_END
26051#define TIMER_ELAPSED((sqlite_uint64)0) ((sqlite_uint64)0)
26052#endif
26053
26054/*
26055** If we compile with the SQLITE_TEST macro set, then the following block
26056** of code will give us the ability to simulate a disk I/O error. This
26057** is used for testing the I/O recovery logic.
26058*/
26059#if defined(SQLITE_TEST)
26060SQLITE_API extern int sqlite3_io_error_hit;
26061SQLITE_API extern int sqlite3_io_error_hardhit;
26062SQLITE_API extern int sqlite3_io_error_pending;
26063SQLITE_API extern int sqlite3_io_error_persist;
26064SQLITE_API extern int sqlite3_io_error_benign;
26065SQLITE_API extern int sqlite3_diskfull_pending;
26066SQLITE_API extern int sqlite3_diskfull;
26067#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
26068#define SimulateIOError(CODE) \
26069 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
26070 || sqlite3_io_error_pending-- == 1 ) \
26071 { local_ioerr(); CODE; }
26072static void local_ioerr(){
26073 IOTRACE(("IOERR\n"));
26074 sqlite3_io_error_hit++;
26075 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
26076}
26077#define SimulateDiskfullError(CODE) \
26078 if( sqlite3_diskfull_pending ){ \
26079 if( sqlite3_diskfull_pending == 1 ){ \
26080 local_ioerr(); \
26081 sqlite3_diskfull = 1; \
26082 sqlite3_io_error_hit = 1; \
26083 CODE; \
26084 }else{ \
26085 sqlite3_diskfull_pending--; \
26086 } \
26087 }
26088#else
26089#define SimulateIOErrorBenign(X)
26090#define SimulateIOError(A)
26091#define SimulateDiskfullError(A)
26092#endif /* defined(SQLITE_TEST) */
26093
26094/*
26095** When testing, keep a count of the number of open files.
26096*/
26097#if defined(SQLITE_TEST)
26098SQLITE_API extern int sqlite3_open_file_count;
26099#define OpenCounter(X) sqlite3_open_file_count+=(X)
26100#else
26101#define OpenCounter(X)
26102#endif /* defined(SQLITE_TEST) */
26103
26104#endif /* !defined(_OS_COMMON_H_) */
26105
26106/************** End of os_common.h *******************************************/
26107/************** Continuing where we left off in mutex_w32.c ******************/
26108
26109/*
26110** Include the header file for the Windows VFS.
26111*/
26112/************** Include os_win.h in the middle of mutex_w32.c ****************/
26113/************** Begin file os_win.h ******************************************/
26114/*
26115** 2013 November 25
26116**
26117** The author disclaims copyright to this source code. In place of
26118** a legal notice, here is a blessing:
26119**
26120** May you do good and not evil.
26121** May you find forgiveness for yourself and forgive others.
26122** May you share freely, never taking more than you give.
26123**
26124******************************************************************************
26125**
26126** This file contains code that is specific to Windows.
26127*/
26128#ifndef SQLITE_OS_WIN_H
26129#define SQLITE_OS_WIN_H
26130
26131/*
26132** Include the primary Windows SDK header file.
26133*/
26134#include "windows.h"
26135
26136#ifdef __CYGWIN__
26137# include <sys/cygwin.h>
26138# include <errno(*__errno_location ()).h> /* amalgamator: dontcache */
26139#endif
26140
26141/*
26142** Determine if we are dealing with Windows NT.
26143**
26144** We ought to be able to determine if we are compiling for Windows 9x or
26145** Windows NT using the _WIN32_WINNT macro as follows:
26146**
26147** #if defined(_WIN32_WINNT)
26148** # define SQLITE_OS_WINNT 1
26149** #else
26150** # define SQLITE_OS_WINNT 0
26151** #endif
26152**
26153** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
26154** it ought to, so the above test does not work. We'll just assume that
26155** everything is Windows NT unless the programmer explicitly says otherwise
26156** by setting SQLITE_OS_WINNT to 0.
26157*/
26158#if SQLITE_OS_WIN0 && !defined(SQLITE_OS_WINNT)
26159# define SQLITE_OS_WINNT 1
26160#endif
26161
26162/*
26163** Determine if we are dealing with Windows CE - which has a much reduced
26164** API.
26165*/
26166#if defined(_WIN32_WCE)
26167# define SQLITE_OS_WINCE 1
26168#else
26169# define SQLITE_OS_WINCE 0
26170#endif
26171
26172/*
26173** Determine if we are dealing with WinRT, which provides only a subset of
26174** the full Win32 API.
26175*/
26176#if !defined(SQLITE_OS_WINRT)
26177# define SQLITE_OS_WINRT 0
26178#endif
26179
26180/*
26181** For WinCE, some API function parameters do not appear to be declared as
26182** volatile.
26183*/
26184#if SQLITE_OS_WINCE
26185# define SQLITE_WIN32_VOLATILE
26186#else
26187# define SQLITE_WIN32_VOLATILE volatile
26188#endif
26189
26190/*
26191** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
26192** functions are not available (e.g. those not using MSVC, Cygwin, etc).
26193*/
26194#if SQLITE_OS_WIN0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
26195 SQLITE_THREADSAFE1>0 && !defined(__CYGWIN__)
26196# define SQLITE_OS_WIN_THREADS 1
26197#else
26198# define SQLITE_OS_WIN_THREADS 0
26199#endif
26200
26201#endif /* SQLITE_OS_WIN_H */
26202
26203/************** End of os_win.h **********************************************/
26204/************** Continuing where we left off in mutex_w32.c ******************/
26205#endif
26206
26207/*
26208** The code in this file is only used if we are compiling multithreaded
26209** on a Win32 system.
26210*/
26211#ifdef SQLITE_MUTEX_W32
26212
26213/*
26214** Each recursive mutex is an instance of the following structure.
26215*/
26216struct sqlite3_mutex {
26217 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
26218 int id; /* Mutex type */
26219#ifdef SQLITE_DEBUG
26220 volatile int nRef; /* Number of enterances */
26221 volatile DWORD owner; /* Thread holding this mutex */
26222 volatile LONG trace; /* True to trace changes */
26223#endif
26224};
26225
26226/*
26227** These are the initializer values used when declaring a "static" mutex
26228** on Win32. It should be noted that all mutexes require initialization
26229** on the Win32 platform.
26230*/
26231#define SQLITE_W32_MUTEX_INITIALIZER { 0 }
26232
26233#ifdef SQLITE_DEBUG
26234#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } } { SQLITE_W32_MUTEX_INITIALIZER, id, \
26235 0L, (DWORD)0, 0 }
26236#else
26237#define SQLITE3_MUTEX_INITIALIZER(id){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } } { SQLITE_W32_MUTEX_INITIALIZER, id }
26238#endif
26239
26240#ifdef SQLITE_DEBUG
26241/*
26242** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
26243** intended for use only inside assert() statements.
26244*/
26245static int winMutexHeld(sqlite3_mutex *p){
26246 return p->nRef!=0 && p->owner==GetCurrentThreadId();
26247}
26248
26249static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
26250 return p->nRef==0 || p->owner!=tid;
26251}
26252
26253static int winMutexNotheld(sqlite3_mutex *p){
26254 DWORD tid = GetCurrentThreadId();
26255 return winMutexNotheld2(p, tid);
26256}
26257#endif
26258
26259/*
26260** Try to provide a memory barrier operation, needed for initialization
26261** and also for the xShmBarrier method of the VFS in cases when SQLite is
26262** compiled without mutexes (SQLITE_THREADSAFE=0).
26263*/
26264SQLITE_PRIVATEstatic void sqlite3MemoryBarrier(void){
26265#if defined(SQLITE_MEMORY_BARRIER)
26266 SQLITE_MEMORY_BARRIER;
26267#elif defined(__GNUC__4)
26268 __sync_synchronize();
26269#elif MSVC_VERSION0>=1300
26270 _ReadWriteBarrier();
26271#elif defined(MemoryBarrier)
26272 MemoryBarrier();
26273#endif
26274}
26275
26276/*
26277** Initialize and deinitialize the mutex subsystem.
26278*/
26279static sqlite3_mutex winMutex_staticMutexes[] = {
26280 SQLITE3_MUTEX_INITIALIZER(2){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26281 SQLITE3_MUTEX_INITIALIZER(3){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26282 SQLITE3_MUTEX_INITIALIZER(4){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26283 SQLITE3_MUTEX_INITIALIZER(5){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26284 SQLITE3_MUTEX_INITIALIZER(6){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26285 SQLITE3_MUTEX_INITIALIZER(7){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26286 SQLITE3_MUTEX_INITIALIZER(8){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26287 SQLITE3_MUTEX_INITIALIZER(9){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26288 SQLITE3_MUTEX_INITIALIZER(10){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26289 SQLITE3_MUTEX_INITIALIZER(11){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26290 SQLITE3_MUTEX_INITIALIZER(12){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } },
26291 SQLITE3_MUTEX_INITIALIZER(13){ { { 0, 0, 0, 0, PTHREAD_MUTEX_TIMED_NP, 0, 0, { 0, 0 } } } }
26292};
26293
26294static int winMutex_isInit = 0;
26295static int winMutex_isNt = -1; /* <0 means "need to query" */
26296
26297/* As the winMutexInit() and winMutexEnd() functions are called as part
26298** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
26299** "interlocked" magic used here is probably not strictly necessary.
26300*/
26301static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
26302
26303SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
26304SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
26305
26306static int winMutexInit(void){
26307 /* The first to increment to 1 does actual initialization */
26308 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
26309 int i;
26310 for(i=0; i<ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
; i++){
26311#if SQLITE_OS_WINRT
26312 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
26313#else
26314 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
26315#endif
26316 }
26317 winMutex_isInit = 1;
26318 }else{
26319 /* Another thread is (in the process of) initializing the static
26320 ** mutexes */
26321 while( !winMutex_isInit ){
26322 sqlite3_win32_sleep(1);
26323 }
26324 }
26325 return SQLITE_OK0;
26326}
26327
26328static int winMutexEnd(void){
26329 /* The first to decrement to 0 does actual shutdown
26330 ** (which should be the last to shutdown.) */
26331 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
26332 if( winMutex_isInit==1 ){
26333 int i;
26334 for(i=0; i<ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
; i++){
26335 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
26336 }
26337 winMutex_isInit = 0;
26338 }
26339 }
26340 return SQLITE_OK0;
26341}
26342
26343/*
26344** The sqlite3_mutex_alloc() routine allocates a new
26345** mutex and returns a pointer to it. If it returns NULL
26346** that means that a mutex could not be allocated. SQLite
26347** will unwind its stack and return an error. The argument
26348** to sqlite3_mutex_alloc() is one of these integer constants:
26349**
26350** <ul>
26351** <li> SQLITE_MUTEX_FAST
26352** <li> SQLITE_MUTEX_RECURSIVE
26353** <li> SQLITE_MUTEX_STATIC_MASTER
26354** <li> SQLITE_MUTEX_STATIC_MEM
26355** <li> SQLITE_MUTEX_STATIC_OPEN
26356** <li> SQLITE_MUTEX_STATIC_PRNG
26357** <li> SQLITE_MUTEX_STATIC_LRU
26358** <li> SQLITE_MUTEX_STATIC_PMEM
26359** <li> SQLITE_MUTEX_STATIC_APP1
26360** <li> SQLITE_MUTEX_STATIC_APP2
26361** <li> SQLITE_MUTEX_STATIC_APP3
26362** <li> SQLITE_MUTEX_STATIC_VFS1
26363** <li> SQLITE_MUTEX_STATIC_VFS2
26364** <li> SQLITE_MUTEX_STATIC_VFS3
26365** </ul>
26366**
26367** The first two constants cause sqlite3_mutex_alloc() to create
26368** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
26369** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
26370** The mutex implementation does not need to make a distinction
26371** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
26372** not want to. But SQLite will only request a recursive mutex in
26373** cases where it really needs one. If a faster non-recursive mutex
26374** implementation is available on the host platform, the mutex subsystem
26375** might return such a mutex in response to SQLITE_MUTEX_FAST.
26376**
26377** The other allowed parameters to sqlite3_mutex_alloc() each return
26378** a pointer to a static preexisting mutex. Six static mutexes are
26379** used by the current version of SQLite. Future versions of SQLite
26380** may add additional static mutexes. Static mutexes are for internal
26381** use by SQLite only. Applications that use SQLite mutexes should
26382** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
26383** SQLITE_MUTEX_RECURSIVE.
26384**
26385** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
26386** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
26387** returns a different mutex on every call. But for the static
26388** mutex types, the same mutex is returned on every call that has
26389** the same type number.
26390*/
26391static sqlite3_mutex *winMutexAlloc(int iType){
26392 sqlite3_mutex *p;
26393
26394 switch( iType ){
26395 case SQLITE_MUTEX_FAST0:
26396 case SQLITE_MUTEX_RECURSIVE1: {
26397 p = sqlite3MallocZero( sizeof(*p) );
26398 if( p ){
26399 p->id = iType;
26400#ifdef SQLITE_DEBUG
26401#ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
26402 p->trace = 1;
26403#endif
26404#endif
26405#if SQLITE_OS_WINRT
26406 InitializeCriticalSectionEx(&p->mutex, 0, 0);
26407#else
26408 InitializeCriticalSection(&p->mutex);
26409#endif
26410 }
26411 break;
26412 }
26413 default: {
26414#ifdef SQLITE_ENABLE_API_ARMOR
26415 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes)((int)(sizeof(winMutex_staticMutexes)/sizeof(winMutex_staticMutexes
[0])))
){
26416 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(26416);
26417 return 0;
26418 }
26419#endif
26420 p = &winMutex_staticMutexes[iType-2];
26421#ifdef SQLITE_DEBUG
26422#ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
26423 InterlockedCompareExchange(&p->trace, 1, 0);
26424#endif
26425#endif
26426 break;
26427 }
26428 }
26429 assert( p==0 || p->id==iType )((void) (0));
26430 return p;
26431}
26432
26433
26434/*
26435** This routine deallocates a previously
26436** allocated mutex. SQLite is careful to deallocate every
26437** mutex that it allocates.
26438*/
26439static void winMutexFree(sqlite3_mutex *p){
26440 assert( p )((void) (0));
26441 assert( p->nRef==0 && p->owner==0 )((void) (0));
26442 if( p->id==SQLITE_MUTEX_FAST0 || p->id==SQLITE_MUTEX_RECURSIVE1 ){
26443 DeleteCriticalSection(&p->mutex);
26444 sqlite3_free(p);
26445 }else{
26446#ifdef SQLITE_ENABLE_API_ARMOR
26447 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(26447);
26448#endif
26449 }
26450}
26451
26452/*
26453** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
26454** to enter a mutex. If another thread is already within the mutex,
26455** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
26456** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
26457** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
26458** be entered multiple times by the same thread. In such cases the,
26459** mutex must be exited an equal number of times before another thread
26460** can enter. If the same thread tries to enter any other kind of mutex
26461** more than once, the behavior is undefined.
26462*/
26463static void winMutexEnter(sqlite3_mutex *p){
26464#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26465 DWORD tid = GetCurrentThreadId();
26466#endif
26467#ifdef SQLITE_DEBUG
26468 assert( p )((void) (0));
26469 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) )((void) (0));
26470#else
26471 assert( p )((void) (0));
26472#endif
26473 assert( winMutex_isInit==1 )((void) (0));
26474 EnterCriticalSection(&p->mutex);
26475#ifdef SQLITE_DEBUG
26476 assert( p->nRef>0 || p->owner==0 )((void) (0));
26477 p->owner = tid;
26478 p->nRef++;
26479 if( p->trace ){
26480 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
26481 tid, p->id, p, p->trace, p->nRef));
26482 }
26483#endif
26484}
26485
26486static int winMutexTry(sqlite3_mutex *p){
26487#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26488 DWORD tid = GetCurrentThreadId();
26489#endif
26490 int rc = SQLITE_BUSY5;
26491 assert( p )((void) (0));
26492 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) )((void) (0));
26493 /*
26494 ** The sqlite3_mutex_try() routine is very rarely used, and when it
26495 ** is used it is merely an optimization. So it is OK for it to always
26496 ** fail.
26497 **
26498 ** The TryEnterCriticalSection() interface is only available on WinNT.
26499 ** And some windows compilers complain if you try to use it without
26500 ** first doing some #defines that prevent SQLite from building on Win98.
26501 ** For that reason, we will omit this optimization for now. See
26502 ** ticket #2685.
26503 */
26504#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
26505 assert( winMutex_isInit==1 )((void) (0));
26506 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 )((void) (0));
26507 if( winMutex_isNt<0 ){
26508 winMutex_isNt = sqlite3_win32_is_nt();
26509 }
26510 assert( winMutex_isNt==0 || winMutex_isNt==1 )((void) (0));
26511 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
26512#ifdef SQLITE_DEBUG
26513 p->owner = tid;
26514 p->nRef++;
26515#endif
26516 rc = SQLITE_OK0;
26517 }
26518#else
26519 UNUSED_PARAMETER(p)(void)(p);
26520#endif
26521#ifdef SQLITE_DEBUG
26522 if( p->trace ){
26523 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
26524 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
26525 }
26526#endif
26527 return rc;
26528}
26529
26530/*
26531** The sqlite3_mutex_leave() routine exits a mutex that was
26532** previously entered by the same thread. The behavior
26533** is undefined if the mutex is not currently entered or
26534** is not currently allocated. SQLite will never do either.
26535*/
26536static void winMutexLeave(sqlite3_mutex *p){
26537#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
26538 DWORD tid = GetCurrentThreadId();
26539#endif
26540 assert( p )((void) (0));
26541#ifdef SQLITE_DEBUG
26542 assert( p->nRef>0 )((void) (0));
26543 assert( p->owner==tid )((void) (0));
26544 p->nRef--;
26545 if( p->nRef==0 ) p->owner = 0;
26546 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE )((void) (0));
26547#endif
26548 assert( winMutex_isInit==1 )((void) (0));
26549 LeaveCriticalSection(&p->mutex);
26550#ifdef SQLITE_DEBUG
26551 if( p->trace ){
26552 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
26553 tid, p->id, p, p->trace, p->nRef));
26554 }
26555#endif
26556}
26557
26558SQLITE_PRIVATEstatic sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
26559 static const sqlite3_mutex_methods sMutex = {
26560 winMutexInit,
26561 winMutexEnd,
26562 winMutexAlloc,
26563 winMutexFree,
26564 winMutexEnter,
26565 winMutexTry,
26566 winMutexLeave,
26567#ifdef SQLITE_DEBUG
26568 winMutexHeld,
26569 winMutexNotheld
26570#else
26571 0,
26572 0
26573#endif
26574 };
26575 return &sMutex;
26576}
26577
26578#endif /* SQLITE_MUTEX_W32 */
26579
26580/************** End of mutex_w32.c *******************************************/
26581/************** Begin file malloc.c ******************************************/
26582/*
26583** 2001 September 15
26584**
26585** The author disclaims copyright to this source code. In place of
26586** a legal notice, here is a blessing:
26587**
26588** May you do good and not evil.
26589** May you find forgiveness for yourself and forgive others.
26590** May you share freely, never taking more than you give.
26591**
26592*************************************************************************
26593**
26594** Memory allocation functions used throughout sqlite.
26595*/
26596/* #include "sqliteInt.h" */
26597/* #include <stdarg.h> */
26598
26599/*
26600** Attempt to release up to n bytes of non-essential memory currently
26601** held by SQLite. An example of non-essential memory is memory used to
26602** cache database pages that are not currently in use.
26603*/
26604SQLITE_API int sqlite3_release_memory(int n){
26605#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
26606 return sqlite3PcacheReleaseMemory(n);
26607#else
26608 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
26609 ** is a no-op returning zero if SQLite is not compiled with
26610 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
26611 UNUSED_PARAMETER(n)(void)(n);
26612 return 0;
26613#endif
26614}
26615
26616/*
26617** State information local to the memory allocation subsystem.
26618*/
26619static SQLITE_WSD struct Mem0Global {
26620 sqlite3_mutex *mutex; /* Mutex to serialize access */
26621 sqlite3_int64 alarmThreshold; /* The soft heap limit */
26622
26623 /*
26624 ** True if heap is nearly "full" where "full" is defined by the
26625 ** sqlite3_soft_heap_limit() setting.
26626 */
26627 int nearlyFull;
26628} mem0mem0 = { 0, 0, 0 };
26629
26630#define mem0mem0 GLOBAL(struct Mem0Global, mem0)mem0
26631
26632/*
26633** Return the memory allocator mutex. sqlite3_status() needs it.
26634*/
26635SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3MallocMutex(void){
26636 return mem0mem0.mutex;
26637}
26638
26639#ifndef SQLITE_OMIT_DEPRECATED
26640/*
26641** Deprecated external interface. It used to set an alarm callback
26642** that was invoked when memory usage grew too large. Now it is a
26643** no-op.
26644*/
26645SQLITE_API int sqlite3_memory_alarm(
26646 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
26647 void *pArg,
26648 sqlite3_int64 iThreshold
26649){
26650 (void)xCallback;
26651 (void)pArg;
26652 (void)iThreshold;
26653 return SQLITE_OK0;
26654}
26655#endif
26656
26657/*
26658** Set the soft heap-size limit for the library. Passing a zero or
26659** negative value indicates no limit.
26660*/
26661SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
26662 sqlite3_int64 priorLimit;
26663 sqlite3_int64 excess;
26664 sqlite3_int64 nUsed;
26665#ifndef SQLITE_OMIT_AUTOINIT
26666 int rc = sqlite3_initialize();
26667 if( rc ) return -1;
26668#endif
26669 sqlite3_mutex_enter(mem0mem0.mutex);
26670 priorLimit = mem0mem0.alarmThreshold;
26671 if( n<0 ){
26672 sqlite3_mutex_leave(mem0mem0.mutex);
26673 return priorLimit;
26674 }
26675 mem0mem0.alarmThreshold = n;
26676 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
26677 mem0mem0.nearlyFull = (n>0 && n<=nUsed);
26678 sqlite3_mutex_leave(mem0mem0.mutex);
26679 excess = sqlite3_memory_used() - n;
26680 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
26681 return priorLimit;
26682}
26683SQLITE_API void sqlite3_soft_heap_limit(int n){
26684 if( n<0 ) n = 0;
26685 sqlite3_soft_heap_limit64(n);
26686}
26687
26688/*
26689** Initialize the memory allocation subsystem.
26690*/
26691SQLITE_PRIVATEstatic int sqlite3MallocInit(void){
26692 int rc;
26693 if( sqlite3GlobalConfigsqlite3Config.m.xMalloc==0 ){
26694 sqlite3MemSetDefault();
26695 }
26696 memset(&mem0mem0, 0, sizeof(mem0mem0));
26697 mem0mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM3);
26698 if( sqlite3GlobalConfigsqlite3Config.pPage==0 || sqlite3GlobalConfigsqlite3Config.szPage<512
26699 || sqlite3GlobalConfigsqlite3Config.nPage<=0 ){
26700 sqlite3GlobalConfigsqlite3Config.pPage = 0;
26701 sqlite3GlobalConfigsqlite3Config.szPage = 0;
26702 }
26703 rc = sqlite3GlobalConfigsqlite3Config.m.xInit(sqlite3GlobalConfigsqlite3Config.m.pAppData);
26704 if( rc!=SQLITE_OK0 ) memset(&mem0mem0, 0, sizeof(mem0mem0));
26705 return rc;
26706}
26707
26708/*
26709** Return true if the heap is currently under memory pressure - in other
26710** words if the amount of heap used is close to the limit set by
26711** sqlite3_soft_heap_limit().
26712*/
26713SQLITE_PRIVATEstatic int sqlite3HeapNearlyFull(void){
26714 return mem0mem0.nearlyFull;
26715}
26716
26717/*
26718** Deinitialize the memory allocation subsystem.
26719*/
26720SQLITE_PRIVATEstatic void sqlite3MallocEnd(void){
26721 if( sqlite3GlobalConfigsqlite3Config.m.xShutdown ){
26722 sqlite3GlobalConfigsqlite3Config.m.xShutdown(sqlite3GlobalConfigsqlite3Config.m.pAppData);
26723 }
26724 memset(&mem0mem0, 0, sizeof(mem0mem0));
26725}
26726
26727/*
26728** Return the amount of memory currently checked out.
26729*/
26730SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
26731 sqlite3_int64 res, mx;
26732 sqlite3_status64(SQLITE_STATUS_MEMORY_USED0, &res, &mx, 0);
26733 return res;
26734}
26735
26736/*
26737** Return the maximum amount of memory that has ever been
26738** checked out since either the beginning of this process
26739** or since the most recent reset.
26740*/
26741SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
26742 sqlite3_int64 res, mx;
26743 sqlite3_status64(SQLITE_STATUS_MEMORY_USED0, &res, &mx, resetFlag);
26744 return mx;
26745}
26746
26747/*
26748** Trigger the alarm
26749*/
26750static void sqlite3MallocAlarm(int nByte){
26751 if( mem0mem0.alarmThreshold<=0 ) return;
26752 sqlite3_mutex_leave(mem0mem0.mutex);
26753 sqlite3_release_memory(nByte);
26754 sqlite3_mutex_enter(mem0mem0.mutex);
26755}
26756
26757/*
26758** Do a memory allocation with statistics and alarms. Assume the
26759** lock is already held.
26760*/
26761static void mallocWithAlarm(int n, void **pp){
26762 void *p;
26763 int nFull;
26764 assert( sqlite3_mutex_held(mem0.mutex) )((void) (0));
26765 assert( n>0 )((void) (0));
26766
26767 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
26768 ** implementation of malloc_good_size(), which must be called in debug
26769 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
26770 ** or else a crash results. Hence, do not attempt to optimize out the
26771 ** following xRoundup() call. */
26772 nFull = sqlite3GlobalConfigsqlite3Config.m.xRoundup(n);
26773
26774#ifdef SQLITE_MAX_MEMORY
26775 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0)+nFull>SQLITE_MAX_MEMORY ){
26776 *pp = 0;
26777 return;
26778 }
26779#endif
26780
26781 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE5, n);
26782 if( mem0mem0.alarmThreshold>0 ){
26783 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0);
26784 if( nUsed >= mem0mem0.alarmThreshold - nFull ){
26785 mem0mem0.nearlyFull = 1;
26786 sqlite3MallocAlarm(nFull);
26787 }else{
26788 mem0mem0.nearlyFull = 0;
26789 }
26790 }
26791 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc(nFull);
26792#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
26793 if( p==0 && mem0mem0.alarmThreshold>0 ){
26794 sqlite3MallocAlarm(nFull);
26795 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc(nFull);
26796 }
26797#endif
26798 if( p ){
26799 nFull = sqlite3MallocSize(p);
26800 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED0, nFull);
26801 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT9, 1);
26802 }
26803 *pp = p;
26804}
26805
26806/*
26807** Allocate memory. This routine is like sqlite3_malloc() except that it
26808** assumes the memory subsystem has already been initialized.
26809*/
26810SQLITE_PRIVATEstatic void *sqlite3Malloc(u64 n){
26811 void *p;
26812 if( n==0 || n>=0x7fffff00 ){
26813 /* A memory allocation of a number of bytes which is near the maximum
26814 ** signed integer value might cause an integer overflow inside of the
26815 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
26816 ** 255 bytes of overhead. SQLite itself will never use anything near
26817 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
26818 p = 0;
26819 }else if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
26820 sqlite3_mutex_enter(mem0mem0.mutex);
26821 mallocWithAlarm((int)n, &p);
26822 sqlite3_mutex_leave(mem0mem0.mutex);
26823 }else{
26824 p = sqlite3GlobalConfigsqlite3Config.m.xMalloc((int)n);
26825 }
26826 assert( EIGHT_BYTE_ALIGNMENT(p) )((void) (0)); /* IMP: R-11148-40995 */
26827 return p;
26828}
26829
26830/*
26831** This version of the memory allocation is for use by the application.
26832** First make sure the memory subsystem is initialized, then do the
26833** allocation.
26834*/
26835SQLITE_API void *sqlite3_malloc(int n){
26836#ifndef SQLITE_OMIT_AUTOINIT
26837 if( sqlite3_initialize() ) return 0;
26838#endif
26839 return n<=0 ? 0 : sqlite3Malloc(n);
26840}
26841SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
26842#ifndef SQLITE_OMIT_AUTOINIT
26843 if( sqlite3_initialize() ) return 0;
26844#endif
26845 return sqlite3Malloc(n);
26846}
26847
26848/*
26849** TRUE if p is a lookaside memory allocation from db
26850*/
26851#ifndef SQLITE_OMIT_LOOKASIDE
26852static int isLookaside(sqlite3 *db, void *p){
26853 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd)(((uptr)(p)>=(uptr)(db->lookaside.pStart))&&((uptr
)(p)<(uptr)(db->lookaside.pEnd)))
;
26854}
26855#else
26856#define isLookaside(A,B) 0
26857#endif
26858
26859/*
26860** Return the size of a memory allocation previously obtained from
26861** sqlite3Malloc() or sqlite3_malloc().
26862*/
26863SQLITE_PRIVATEstatic int sqlite3MallocSize(void *p){
26864 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
26865 return sqlite3GlobalConfigsqlite3Config.m.xSize(p);
26866}
26867SQLITE_PRIVATEstatic int sqlite3DbMallocSize(sqlite3 *db, void *p){
26868 assert( p!=0 )((void) (0));
26869 if( db==0 || !isLookaside(db,p) ){
26870#ifdef SQLITE_DEBUG
26871 if( db==0 ){
26872 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
26873 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
26874 }else{
26875 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
26876 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
26877 }
26878#endif
26879 return sqlite3GlobalConfigsqlite3Config.m.xSize(p);
26880 }else{
26881 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
26882 return db->lookaside.sz;
26883 }
26884}
26885SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
26886 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
26887 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
26888 return p ? sqlite3GlobalConfigsqlite3Config.m.xSize(p) : 0;
26889}
26890
26891/*
26892** Free memory previously obtained from sqlite3Malloc().
26893*/
26894SQLITE_API void sqlite3_free(void *p){
26895 if( p==0 ) return; /* IMP: R-49053-54554 */
26896 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) )((void) (0));
26897 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) )((void) (0));
26898 if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
26899 sqlite3_mutex_enter(mem0mem0.mutex);
26900 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED0, sqlite3MallocSize(p));
26901 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT9, 1);
26902 sqlite3GlobalConfigsqlite3Config.m.xFree(p);
26903 sqlite3_mutex_leave(mem0mem0.mutex);
26904 }else{
26905 sqlite3GlobalConfigsqlite3Config.m.xFree(p);
26906 }
26907}
26908
26909/*
26910** Add the size of memory allocation "p" to the count in
26911** *db->pnBytesFreed.
26912*/
26913static SQLITE_NOINLINE__attribute__((noinline)) void measureAllocationSize(sqlite3 *db, void *p){
26914 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
26915}
26916
26917/*
26918** Free memory that might be associated with a particular database
26919** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
26920** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
26921*/
26922SQLITE_PRIVATEstatic void sqlite3DbFreeNN(sqlite3 *db, void *p){
26923 assert( db==0 || sqlite3_mutex_held(db->mutex) )((void) (0));
26924 assert( p!=0 )((void) (0));
26925 if( db ){
26926 if( db->pnBytesFreed ){
26927 measureAllocationSize(db, p);
26928 return;
26929 }
26930 if( isLookaside(db, p) ){
26931 LookasideSlot *pBuf = (LookasideSlot*)p;
26932#ifdef SQLITE_DEBUG
26933 /* Trash all content in the buffer being freed */
26934 memset(p, 0xaa, db->lookaside.sz);
26935#endif
26936 pBuf->pNext = db->lookaside.pFree;
26937 db->lookaside.pFree = pBuf;
26938 return;
26939 }
26940 }
26941 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
26942 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
26943 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) )((void) (0));
26944 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
26945 sqlite3_free(p);
26946}
26947SQLITE_PRIVATEstatic void sqlite3DbFree(sqlite3 *db, void *p){
26948 assert( db==0 || sqlite3_mutex_held(db->mutex) )((void) (0));
26949 if( p ) sqlite3DbFreeNN(db, p);
26950}
26951
26952/*
26953** Change the size of an existing memory allocation
26954*/
26955SQLITE_PRIVATEstatic void *sqlite3Realloc(void *pOld, u64 nBytes){
26956 int nOld, nNew, nDiff;
26957 void *pNew;
26958 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) )((void) (0));
26959 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) )((void) (0));
26960 if( pOld==0 ){
26961 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
26962 }
26963 if( nBytes==0 ){
26964 sqlite3_free(pOld); /* IMP: R-26507-47431 */
26965 return 0;
26966 }
26967 if( nBytes>=0x7fffff00 ){
26968 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
26969 return 0;
26970 }
26971 nOld = sqlite3MallocSize(pOld);
26972 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
26973 ** argument to xRealloc is always a value returned by a prior call to
26974 ** xRoundup. */
26975 nNew = sqlite3GlobalConfigsqlite3Config.m.xRoundup((int)nBytes);
26976 if( nOld==nNew ){
26977 pNew = pOld;
26978 }else if( sqlite3GlobalConfigsqlite3Config.bMemstat ){
26979 sqlite3_mutex_enter(mem0mem0.mutex);
26980 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE5, (int)nBytes);
26981 nDiff = nNew - nOld;
26982 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED0) >=
26983 mem0mem0.alarmThreshold-nDiff ){
26984 sqlite3MallocAlarm(nDiff);
26985 }
26986 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
26987 if( pNew==0 && mem0mem0.alarmThreshold>0 ){
26988 sqlite3MallocAlarm((int)nBytes);
26989 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
26990 }
26991 if( pNew ){
26992 nNew = sqlite3MallocSize(pNew);
26993 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED0, nNew-nOld);
26994 }
26995 sqlite3_mutex_leave(mem0mem0.mutex);
26996 }else{
26997 pNew = sqlite3GlobalConfigsqlite3Config.m.xRealloc(pOld, nNew);
26998 }
26999 assert( EIGHT_BYTE_ALIGNMENT(pNew) )((void) (0)); /* IMP: R-11148-40995 */
27000 return pNew;
27001}
27002
27003/*
27004** The public interface to sqlite3Realloc. Make sure that the memory
27005** subsystem is initialized prior to invoking sqliteRealloc.
27006*/
27007SQLITE_API void *sqlite3_realloc(void *pOld, int n){
27008#ifndef SQLITE_OMIT_AUTOINIT
27009 if( sqlite3_initialize() ) return 0;
27010#endif
27011 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
27012 return sqlite3Realloc(pOld, n);
27013}
27014SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
27015#ifndef SQLITE_OMIT_AUTOINIT
27016 if( sqlite3_initialize() ) return 0;
27017#endif
27018 return sqlite3Realloc(pOld, n);
27019}
27020
27021
27022/*
27023** Allocate and zero memory.
27024*/
27025SQLITE_PRIVATEstatic void *sqlite3MallocZero(u64 n){
27026 void *p = sqlite3Malloc(n);
27027 if( p ){
27028 memset(p, 0, (size_t)n);
27029 }
27030 return p;
27031}
27032
27033/*
27034** Allocate and zero memory. If the allocation fails, make
27035** the mallocFailed flag in the connection pointer.
27036*/
27037SQLITE_PRIVATEstatic void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
27038 void *p;
27039 testcase( db==0 );
27040 p = sqlite3DbMallocRaw(db, n);
27041 if( p ) memset(p, 0, (size_t)n);
27042 return p;
27043}
27044
27045
27046/* Finish the work of sqlite3DbMallocRawNN for the unusual and
27047** slower case when the allocation cannot be fulfilled using lookaside.
27048*/
27049static SQLITE_NOINLINE__attribute__((noinline)) void *dbMallocRawFinish(sqlite3 *db, u64 n){
27050 void *p;
27051 assert( db!=0 )((void) (0));
27052 p = sqlite3Malloc(n);
27053 if( !p ) sqlite3OomFault(db);
27054 sqlite3MemdebugSetType(p,
27055 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
27056 return p;
27057}
27058
27059/*
27060** Allocate memory, either lookaside (if possible) or heap.
27061** If the allocation fails, set the mallocFailed flag in
27062** the connection pointer.
27063**
27064** If db!=0 and db->mallocFailed is true (indicating a prior malloc
27065** failure on the same database connection) then always return 0.
27066** Hence for a particular database connection, once malloc starts
27067** failing, it fails consistently until mallocFailed is reset.
27068** This is an important assumption. There are many places in the
27069** code that do things like this:
27070**
27071** int *a = (int*)sqlite3DbMallocRaw(db, 100);
27072** int *b = (int*)sqlite3DbMallocRaw(db, 200);
27073** if( b ) a[10] = 9;
27074**
27075** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
27076** that all prior mallocs (ex: "a") worked too.
27077**
27078** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
27079** not a NULL pointer.
27080*/
27081SQLITE_PRIVATEstatic void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
27082 void *p;
27083 if( db ) return sqlite3DbMallocRawNN(db, n);
27084 p = sqlite3Malloc(n);
27085 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
27086 return p;
27087}
27088SQLITE_PRIVATEstatic void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
27089#ifndef SQLITE_OMIT_LOOKASIDE
27090 LookasideSlot *pBuf;
27091 assert( db!=0 )((void) (0));
27092 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
27093 assert( db->pnBytesFreed==0 )((void) (0));
27094 if( db->lookaside.bDisable==0 ){
27095 assert( db->mallocFailed==0 )((void) (0));
27096 if( n>db->lookaside.sz ){
27097 db->lookaside.anStat[1]++;
27098 }else if( (pBuf = db->lookaside.pFree)!=0 ){
27099 db->lookaside.pFree = pBuf->pNext;
27100 db->lookaside.anStat[0]++;
27101 return (void*)pBuf;
27102 }else if( (pBuf = db->lookaside.pInit)!=0 ){
27103 db->lookaside.pInit = pBuf->pNext;
27104 db->lookaside.anStat[0]++;
27105 return (void*)pBuf;
27106 }else{
27107 db->lookaside.anStat[2]++;
27108 }
27109 }else if( db->mallocFailed ){
27110 return 0;
27111 }
27112#else
27113 assert( db!=0 )((void) (0));
27114 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
27115 assert( db->pnBytesFreed==0 )((void) (0));
27116 if( db->mallocFailed ){
27117 return 0;
27118 }
27119#endif
27120 return dbMallocRawFinish(db, n);
27121}
27122
27123/* Forward declaration */
27124static SQLITE_NOINLINE__attribute__((noinline)) void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
27125
27126/*
27127** Resize the block of memory pointed to by p to n bytes. If the
27128** resize fails, set the mallocFailed flag in the connection object.
27129*/
27130SQLITE_PRIVATEstatic void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
27131 assert( db!=0 )((void) (0));
27132 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
27133 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
27134 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
27135 return dbReallocFinish(db, p, n);
27136}
27137static SQLITE_NOINLINE__attribute__((noinline)) void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
27138 void *pNew = 0;
27139 assert( db!=0 )((void) (0));
27140 assert( p!=0 )((void) (0));
27141 if( db->mallocFailed==0 ){
27142 if( isLookaside(db, p) ){
27143 pNew = sqlite3DbMallocRawNN(db, n);
27144 if( pNew ){
27145 memcpy(pNew, p, db->lookaside.sz);
27146 sqlite3DbFree(db, p);
27147 }
27148 }else{
27149 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
27150 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) )((void) (0));
27151 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
27152 pNew = sqlite3_realloc64(p, n);
27153 if( !pNew ){
27154 sqlite3OomFault(db);
27155 }
27156 sqlite3MemdebugSetType(pNew,
27157 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
27158 }
27159 }
27160 return pNew;
27161}
27162
27163/*
27164** Attempt to reallocate p. If the reallocation fails, then free p
27165** and set the mallocFailed flag in the database connection.
27166*/
27167SQLITE_PRIVATEstatic void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
27168 void *pNew;
27169 pNew = sqlite3DbRealloc(db, p, n);
27170 if( !pNew ){
27171 sqlite3DbFree(db, p);
27172 }
27173 return pNew;
27174}
27175
27176/*
27177** Make a copy of a string in memory obtained from sqliteMalloc(). These
27178** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
27179** is because when memory debugging is turned on, these two functions are
27180** called via macros that record the current file and line number in the
27181** ThreadData structure.
27182*/
27183SQLITE_PRIVATEstatic char *sqlite3DbStrDup(sqlite3 *db, const char *z){
27184 char *zNew;
27185 size_t n;
27186 if( z==0 ){
27187 return 0;
27188 }
27189 n = strlen(z) + 1;
27190 zNew = sqlite3DbMallocRaw(db, n);
27191 if( zNew ){
27192 memcpy(zNew, z, n);
27193 }
27194 return zNew;
27195}
27196SQLITE_PRIVATEstatic char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
27197 char *zNew;
27198 assert( db!=0 )((void) (0));
27199 if( z==0 ){
27200 return 0;
27201 }
27202 assert( (n&0x7fffffff)==n )((void) (0));
27203 zNew = sqlite3DbMallocRawNN(db, n+1);
27204 if( zNew ){
27205 memcpy(zNew, z, (size_t)n);
27206 zNew[n] = 0;
27207 }
27208 return zNew;
27209}
27210
27211/*
27212** The text between zStart and zEnd represents a phrase within a larger
27213** SQL statement. Make a copy of this phrase in space obtained form
27214** sqlite3DbMalloc(). Omit leading and trailing whitespace.
27215*/
27216SQLITE_PRIVATEstatic char *sqlite3DbSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
27217 int n;
27218 while( sqlite3Isspace(zStart[0])(sqlite3CtypeMap[(unsigned char)(zStart[0])]&0x01) ) zStart++;
27219 n = (int)(zEnd - zStart);
27220 while( ALWAYS(n>0)(n>0) && sqlite3Isspace(zStart[n-1])(sqlite3CtypeMap[(unsigned char)(zStart[n-1])]&0x01) ) n--;
27221 return sqlite3DbStrNDup(db, zStart, n);
27222}
27223
27224/*
27225** Free any prior content in *pz and replace it with a copy of zNew.
27226*/
27227SQLITE_PRIVATEstatic void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
27228 sqlite3DbFree(db, *pz);
27229 *pz = sqlite3DbStrDup(db, zNew);
27230}
27231
27232/*
27233** Call this routine to record the fact that an OOM (out-of-memory) error
27234** has happened. This routine will set db->mallocFailed, and also
27235** temporarily disable the lookaside memory allocator and interrupt
27236** any running VDBEs.
27237*/
27238SQLITE_PRIVATEstatic void sqlite3OomFault(sqlite3 *db){
27239 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
27240 db->mallocFailed = 1;
27241 if( db->nVdbeExec>0 ){
27242 db->u1.isInterrupted = 1;
27243 }
27244 db->lookaside.bDisable++;
27245 if( db->pParse ){
27246 db->pParse->rc = SQLITE_NOMEM_BKPT7;
27247 }
27248 }
27249}
27250
27251/*
27252** This routine reactivates the memory allocator and clears the
27253** db->mallocFailed flag as necessary.
27254**
27255** The memory allocator is not restarted if there are running
27256** VDBEs.
27257*/
27258SQLITE_PRIVATEstatic void sqlite3OomClear(sqlite3 *db){
27259 if( db->mallocFailed && db->nVdbeExec==0 ){
27260 db->mallocFailed = 0;
27261 db->u1.isInterrupted = 0;
27262 assert( db->lookaside.bDisable>0 )((void) (0));
27263 db->lookaside.bDisable--;
27264 }
27265}
27266
27267/*
27268** Take actions at the end of an API call to indicate an OOM error
27269*/
27270static SQLITE_NOINLINE__attribute__((noinline)) int apiOomError(sqlite3 *db){
27271 sqlite3OomClear(db);
27272 sqlite3Error(db, SQLITE_NOMEM7);
27273 return SQLITE_NOMEM_BKPT7;
27274}
27275
27276/*
27277** This function must be called before exiting any API function (i.e.
27278** returning control to the user) that has called sqlite3_malloc or
27279** sqlite3_realloc.
27280**
27281** The returned value is normally a copy of the second argument to this
27282** function. However, if a malloc() failure has occurred since the previous
27283** invocation SQLITE_NOMEM is returned instead.
27284**
27285** If an OOM as occurred, then the connection error-code (the value
27286** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
27287*/
27288SQLITE_PRIVATEstatic int sqlite3ApiExit(sqlite3* db, int rc){
27289 /* If the db handle must hold the connection handle mutex here.
27290 ** Otherwise the read (and possible write) of db->mallocFailed
27291 ** is unsafe, as is the call to sqlite3Error().
27292 */
27293 assert( db!=0 )((void) (0));
27294 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
27295 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
27296 return apiOomError(db);
27297 }
27298 return rc & db->errMask;
27299}
27300
27301/************** End of malloc.c **********************************************/
27302/************** Begin file printf.c ******************************************/
27303/*
27304** The "printf" code that follows dates from the 1980's. It is in
27305** the public domain.
27306**
27307**************************************************************************
27308**
27309** This file contains code for a set of "printf"-like routines. These
27310** routines format strings much like the printf() from the standard C
27311** library, though the implementation here has enhancements to support
27312** SQLite.
27313*/
27314/* #include "sqliteInt.h" */
27315
27316/*
27317** Conversion types fall into various categories as defined by the
27318** following enumeration.
27319*/
27320#define etRADIX0 0 /* non-decimal integer types. %x %o */
27321#define etFLOAT1 1 /* Floating point. %f */
27322#define etEXP2 2 /* Exponentional notation. %e and %E */
27323#define etGENERIC3 3 /* Floating or exponential, depending on exponent. %g */
27324#define etSIZE4 4 /* Return number of characters processed so far. %n */
27325#define etSTRING5 5 /* Strings. %s */
27326#define etDYNSTRING6 6 /* Dynamically allocated strings. %z */
27327#define etPERCENT7 7 /* Percent symbol. %% */
27328#define etCHARX8 8 /* Characters. %c */
27329/* The rest are extensions, not normally found in printf() */
27330#define etSQLESCAPE9 9 /* Strings with '\'' doubled. %q */
27331#define etSQLESCAPE210 10 /* Strings with '\'' doubled and enclosed in '',
27332 NULL pointers replaced by SQL NULL. %Q */
27333#define etTOKEN11 11 /* a pointer to a Token structure */
27334#define etSRCLIST12 12 /* a pointer to a SrcList */
27335#define etPOINTER13 13 /* The %p conversion */
27336#define etSQLESCAPE314 14 /* %w -> Strings with '\"' doubled */
27337#define etORDINAL15 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
27338#define etDECIMAL16 16 /* %d or %u, but not %x, %o */
27339
27340#define etINVALID17 17 /* Any unrecognized conversion type */
27341
27342
27343/*
27344** An "etByte" is an 8-bit unsigned value.
27345*/
27346typedef unsigned char etByte;
27347
27348/*
27349** Each builtin conversion character (ex: the 'd' in "%d") is described
27350** by an instance of the following structure
27351*/
27352typedef struct et_info { /* Information about each format field */
27353 char fmttype; /* The format field code letter */
27354 etByte base; /* The base for radix conversion */
27355 etByte flags; /* One or more of FLAG_ constants below */
27356 etByte type; /* Conversion paradigm */
27357 etByte charset; /* Offset into aDigits[] of the digits string */
27358 etByte prefix; /* Offset into aPrefix[] of the prefix string */
27359} et_info;
27360
27361/*
27362** Allowed values for et_info.flags
27363*/
27364#define FLAG_SIGNED1 1 /* True if the value to convert is signed */
27365#define FLAG_STRING4 4 /* Allow infinite precision */
27366
27367
27368/*
27369** The following table is searched linearly, so it is good to put the
27370** most frequently used conversion types first.
27371*/
27372static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
27373static const char aPrefix[] = "-x0\000X0";
27374static const et_info fmtinfo[] = {
27375 { 'd', 10, 1, etDECIMAL16, 0, 0 },
27376 { 's', 0, 4, etSTRING5, 0, 0 },
27377 { 'g', 0, 1, etGENERIC3, 30, 0 },
27378 { 'z', 0, 4, etDYNSTRING6, 0, 0 },
27379 { 'q', 0, 4, etSQLESCAPE9, 0, 0 },
27380 { 'Q', 0, 4, etSQLESCAPE210, 0, 0 },
27381 { 'w', 0, 4, etSQLESCAPE314, 0, 0 },
27382 { 'c', 0, 0, etCHARX8, 0, 0 },
27383 { 'o', 8, 0, etRADIX0, 0, 2 },
27384 { 'u', 10, 0, etDECIMAL16, 0, 0 },
27385 { 'x', 16, 0, etRADIX0, 16, 1 },
27386 { 'X', 16, 0, etRADIX0, 0, 4 },
27387#ifndef SQLITE_OMIT_FLOATING_POINT
27388 { 'f', 0, 1, etFLOAT1, 0, 0 },
27389 { 'e', 0, 1, etEXP2, 30, 0 },
27390 { 'E', 0, 1, etEXP2, 14, 0 },
27391 { 'G', 0, 1, etGENERIC3, 14, 0 },
27392#endif
27393 { 'i', 10, 1, etDECIMAL16, 0, 0 },
27394 { 'n', 0, 0, etSIZE4, 0, 0 },
27395 { '%', 0, 0, etPERCENT7, 0, 0 },
27396 { 'p', 16, 0, etPOINTER13, 0, 1 },
27397
27398 /* All the rest are undocumented and are for internal use only */
27399 { 'T', 0, 0, etTOKEN11, 0, 0 },
27400 { 'S', 0, 0, etSRCLIST12, 0, 0 },
27401 { 'r', 10, 1, etORDINAL15, 0, 0 },
27402};
27403
27404/* Floating point constants used for rounding */
27405static const double arRound[] = {
27406 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
27407 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
27408};
27409
27410/*
27411** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
27412** conversions will work.
27413*/
27414#ifndef SQLITE_OMIT_FLOATING_POINT
27415/*
27416** "*val" is a double such that 0.1 <= *val < 10.0
27417** Return the ascii code for the leading digit of *val, then
27418** multiply "*val" by 10.0 to renormalize.
27419**
27420** Example:
27421** input: *val = 3.14159
27422** output: *val = 1.4159 function return = '3'
27423**
27424** The counter *cnt is incremented each time. After counter exceeds
27425** 16 (the number of significant digits in a 64-bit float) '0' is
27426** always returned.
27427*/
27428static char et_getdigit(LONGDOUBLE_TYPElong double *val, int *cnt){
27429 int digit;
27430 LONGDOUBLE_TYPElong double d;
27431 if( (*cnt)<=0 ) return '0';
27432 (*cnt)--;
27433 digit = (int)*val;
27434 d = digit;
27435 digit += '0';
27436 *val = (*val - d)*10.0;
27437 return (char)digit;
27438}
27439#endif /* SQLITE_OMIT_FLOATING_POINT */
27440
27441/*
27442** Set the StrAccum object to an error mode.
27443*/
27444static void setStrAccumError(StrAccum *p, u8 eError){
27445 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG )((void) (0));
27446 p->accError = eError;
27447 if( p->mxAlloc ) sqlite3_str_reset(p);
27448 if( eError==SQLITE_TOOBIG18 ) sqlite3ErrorToParser(p->db, eError);
27449}
27450
27451/*
27452** Extra argument values from a PrintfArguments object
27453*/
27454static sqlite3_int64 getIntArg(PrintfArguments *p){
27455 if( p->nArg<=p->nUsed ) return 0;
27456 return sqlite3_value_int64(p->apArg[p->nUsed++]);
27457}
27458static double getDoubleArg(PrintfArguments *p){
27459 if( p->nArg<=p->nUsed ) return 0.0;
27460 return sqlite3_value_double(p->apArg[p->nUsed++]);
27461}
27462static char *getTextArg(PrintfArguments *p){
27463 if( p->nArg<=p->nUsed ) return 0;
27464 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
27465}
27466
27467/*
27468** Allocate memory for a temporary buffer needed for printf rendering.
27469**
27470** If the requested size of the temp buffer is larger than the size
27471** of the output buffer in pAccum, then cause an SQLITE_TOOBIG error.
27472** Do the size check before the memory allocation to prevent rogue
27473** SQL from requesting large allocations using the precision or width
27474** field of the printf() function.
27475*/
27476static char *printfTempBuf(sqlite3_str *pAccum, sqlite3_int64 n){
27477 char *z;
27478 if( pAccum->accError ) return 0;
27479 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
27480 setStrAccumError(pAccum, SQLITE_TOOBIG18);
27481 return 0;
27482 }
27483 z = sqlite3DbMallocRaw(pAccum->db, n);
27484 if( z==0 ){
27485 setStrAccumError(pAccum, SQLITE_NOMEM7);
27486 }
27487 return z;
27488}
27489
27490/*
27491** On machines with a small stack size, you can redefine the
27492** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
27493*/
27494#ifndef SQLITE_PRINT_BUF_SIZE70
27495# define SQLITE_PRINT_BUF_SIZE70 70
27496#endif
27497#define etBUFSIZE70 SQLITE_PRINT_BUF_SIZE70 /* Size of the output buffer */
27498
27499/*
27500** Render a string given by "fmt" into the StrAccum object.
27501*/
27502SQLITE_API void sqlite3_str_vappendf(
27503 sqlite3_str *pAccum, /* Accumulate results here */
27504 const char *fmt, /* Format string */
27505 va_list ap /* arguments */
27506){
27507 int c; /* Next character in the format string */
27508 char *bufpt; /* Pointer to the conversion buffer */
27509 int precision; /* Precision of the current field */
27510 int length; /* Length of the field */
27511 int idx; /* A general purpose loop counter */
27512 int width; /* Width of the current field */
27513 etByte flag_leftjustify; /* True if "-" flag is present */
27514 etByte flag_prefix; /* '+' or ' ' or 0 for prefix */
27515 etByte flag_alternateform; /* True if "#" flag is present */
27516 etByte flag_altform2; /* True if "!" flag is present */
27517 etByte flag_zeropad; /* True if field width constant starts with zero */
27518 etByte flag_long; /* 1 for the "l" flag, 2 for "ll", 0 by default */
27519 etByte done; /* Loop termination flag */
27520 etByte cThousand; /* Thousands separator for %d and %u */
27521 etByte xtype = etINVALID17; /* Conversion paradigm */
27522 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
27523 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
27524 sqlite_uint64 longvalue; /* Value for integer types */
27525 LONGDOUBLE_TYPElong double realvalue; /* Value for real types */
27526 const et_info *infop; /* Pointer to the appropriate info structure */
27527 char *zOut; /* Rendering buffer */
27528 int nOut; /* Size of the rendering buffer */
27529 char *zExtra = 0; /* Malloced memory used by some conversion */
27530#ifndef SQLITE_OMIT_FLOATING_POINT
27531 int exp, e2; /* exponent of real numbers */
27532 int nsd; /* Number of significant digits returned */
27533 double rounder; /* Used for rounding floating point values */
27534 etByte flag_dp; /* True if decimal point should be shown */
27535 etByte flag_rtz; /* True if trailing zeros should be removed */
27536#endif
27537 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
27538 char buf[etBUFSIZE70]; /* Conversion buffer */
27539
27540 /* pAccum never starts out with an empty buffer that was obtained from
27541 ** malloc(). This precondition is required by the mprintf("%z...")
27542 ** optimization. */
27543 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 )((void) (0));
27544
27545 bufpt = 0;
27546 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC0x02)!=0 ){
27547 pArgList = va_arg(ap, PrintfArguments*)__builtin_va_arg(ap, PrintfArguments*);
27548 bArgList = 1;
27549 }else{
27550 bArgList = 0;
27551 }
27552 for(; (c=(*fmt))!=0; ++fmt){
27553 if( c!='%' ){
27554 bufpt = (char *)fmt;
27555#if HAVE_STRCHRNUL
27556 fmt = strchrnul(fmt, '%');
27557#else
27558 do{ fmt++; }while( *fmt && *fmt != '%' );
27559#endif
27560 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
27561 if( *fmt==0 ) break;
27562 }
27563 if( (c=(*++fmt))==0 ){
27564 sqlite3_str_append(pAccum, "%", 1);
27565 break;
27566 }
27567 /* Find out what flags are present */
27568 flag_leftjustify = flag_prefix = cThousand =
27569 flag_alternateform = flag_altform2 = flag_zeropad = 0;
27570 done = 0;
27571 width = 0;
27572 flag_long = 0;
27573 precision = -1;
27574 do{
27575 switch( c ){
27576 case '-': flag_leftjustify = 1; break;
27577 case '+': flag_prefix = '+'; break;
27578 case ' ': flag_prefix = ' '; break;
27579 case '#': flag_alternateform = 1; break;
27580 case '!': flag_altform2 = 1; break;
27581 case '0': flag_zeropad = 1; break;
27582 case ',': cThousand = ','; break;
27583 default: done = 1; break;
27584 case 'l': {
27585 flag_long = 1;
27586 c = *++fmt;
27587 if( c=='l' ){
27588 c = *++fmt;
27589 flag_long = 2;
27590 }
27591 done = 1;
27592 break;
27593 }
27594 case '1': case '2': case '3': case '4': case '5':
27595 case '6': case '7': case '8': case '9': {
27596 unsigned wx = c - '0';
27597 while( (c = *++fmt)>='0' && c<='9' ){
27598 wx = wx*10 + c - '0';
27599 }
27600 testcase( wx>0x7fffffff );
27601 width = wx & 0x7fffffff;
27602#ifdef SQLITE_PRINTF_PRECISION_LIMIT
27603 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
27604 width = SQLITE_PRINTF_PRECISION_LIMIT;
27605 }
27606#endif
27607 if( c!='.' && c!='l' ){
27608 done = 1;
27609 }else{
27610 fmt--;
27611 }
27612 break;
27613 }
27614 case '*': {
27615 if( bArgList ){
27616 width = (int)getIntArg(pArgList);
27617 }else{
27618 width = va_arg(ap,int)__builtin_va_arg(ap, int);
27619 }
27620 if( width<0 ){
27621 flag_leftjustify = 1;
27622 width = width >= -2147483647 ? -width : 0;
27623 }
27624#ifdef SQLITE_PRINTF_PRECISION_LIMIT
27625 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
27626 width = SQLITE_PRINTF_PRECISION_LIMIT;
27627 }
27628#endif
27629 if( (c = fmt[1])!='.' && c!='l' ){
27630 c = *++fmt;
27631 done = 1;
27632 }
27633 break;
27634 }
27635 case '.': {
27636 c = *++fmt;
27637 if( c=='*' ){
27638 if( bArgList ){
27639 precision = (int)getIntArg(pArgList);
27640 }else{
27641 precision = va_arg(ap,int)__builtin_va_arg(ap, int);
27642 }
27643 if( precision<0 ){
27644 precision = precision >= -2147483647 ? -precision : -1;
27645 }
27646 c = *++fmt;
27647 }else{
27648 unsigned px = 0;
27649 while( c>='0' && c<='9' ){
27650 px = px*10 + c - '0';
27651 c = *++fmt;
27652 }
27653 testcase( px>0x7fffffff );
27654 precision = px & 0x7fffffff;
27655 }
27656#ifdef SQLITE_PRINTF_PRECISION_LIMIT
27657 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
27658 precision = SQLITE_PRINTF_PRECISION_LIMIT;
27659 }
27660#endif
27661 if( c=='l' ){
27662 --fmt;
27663 }else{
27664 done = 1;
27665 }
27666 break;
27667 }
27668 }
27669 }while( !done && (c=(*++fmt))!=0 );
27670
27671 /* Fetch the info entry for the field */
27672 infop = &fmtinfo[0];
27673 xtype = etINVALID17;
27674 for(idx=0; idx<ArraySize(fmtinfo)((int)(sizeof(fmtinfo)/sizeof(fmtinfo[0]))); idx++){
27675 if( c==fmtinfo[idx].fmttype ){
27676 infop = &fmtinfo[idx];
27677 xtype = infop->type;
27678 break;
27679 }
27680 }
27681
27682 /*
27683 ** At this point, variables are initialized as follows:
27684 **
27685 ** flag_alternateform TRUE if a '#' is present.
27686 ** flag_altform2 TRUE if a '!' is present.
27687 ** flag_prefix '+' or ' ' or zero
27688 ** flag_leftjustify TRUE if a '-' is present or if the
27689 ** field width was negative.
27690 ** flag_zeropad TRUE if the width began with 0.
27691 ** flag_long 1 for "l", 2 for "ll"
27692 ** width The specified field width. This is
27693 ** always non-negative. Zero is the default.
27694 ** precision The specified precision. The default
27695 ** is -1.
27696 ** xtype The class of the conversion.
27697 ** infop Pointer to the appropriate info struct.
27698 */
27699 switch( xtype ){
27700 case etPOINTER13:
27701 flag_long = sizeof(char*)==sizeof(i64) ? 2 :
27702 sizeof(char*)==sizeof(long int) ? 1 : 0;
27703 /* Fall through into the next case */
27704 case etORDINAL15:
27705 case etRADIX0:
27706 cThousand = 0;
27707 /* Fall through into the next case */
27708 case etDECIMAL16:
27709 if( infop->flags & FLAG_SIGNED1 ){
27710 i64 v;
27711 if( bArgList ){
27712 v = getIntArg(pArgList);
27713 }else if( flag_long ){
27714 if( flag_long==2 ){
27715 v = va_arg(ap,i64)__builtin_va_arg(ap, i64) ;
27716 }else{
27717 v = va_arg(ap,long int)__builtin_va_arg(ap, long int);
27718 }
27719 }else{
27720 v = va_arg(ap,int)__builtin_va_arg(ap, int);
27721 }
27722 if( v<0 ){
27723 if( v==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
27724 longvalue = ((u64)1)<<63;
27725 }else{
27726 longvalue = -v;
27727 }
27728 prefix = '-';
27729 }else{
27730 longvalue = v;
27731 prefix = flag_prefix;
27732 }
27733 }else{
27734 if( bArgList ){
27735 longvalue = (u64)getIntArg(pArgList);
27736 }else if( flag_long ){
27737 if( flag_long==2 ){
27738 longvalue = va_arg(ap,u64)__builtin_va_arg(ap, u64);
27739 }else{
27740 longvalue = va_arg(ap,unsigned long int)__builtin_va_arg(ap, unsigned long int);
27741 }
27742 }else{
27743 longvalue = va_arg(ap,unsigned int)__builtin_va_arg(ap, unsigned int);
27744 }
27745 prefix = 0;
27746 }
27747 if( longvalue==0 ) flag_alternateform = 0;
27748 if( flag_zeropad && precision<width-(prefix!=0) ){
27749 precision = width-(prefix!=0);
27750 }
27751 if( precision<etBUFSIZE70-10-etBUFSIZE70/3 ){
27752 nOut = etBUFSIZE70;
27753 zOut = buf;
27754 }else{
27755 u64 n;
27756 n = (u64)precision + 10;
27757 if( cThousand ) n += precision/3;
27758 zOut = zExtra = printfTempBuf(pAccum, n);
27759 if( zOut==0 ) return;
27760 nOut = (int)n;
27761 }
27762 bufpt = &zOut[nOut-1];
27763 if( xtype==etORDINAL15 ){
27764 static const char zOrd[] = "thstndrd";
27765 int x = (int)(longvalue % 10);
27766 if( x>=4 || (longvalue/10)%10==1 ){
27767 x = 0;
27768 }
27769 *(--bufpt) = zOrd[x*2+1];
27770 *(--bufpt) = zOrd[x*2];
27771 }
27772 {
27773 const char *cset = &aDigits[infop->charset];
27774 u8 base = infop->base;
27775 do{ /* Convert to ascii */
27776 *(--bufpt) = cset[longvalue%base];
27777 longvalue = longvalue/base;
27778 }while( longvalue>0 );
27779 }
27780 length = (int)(&zOut[nOut-1]-bufpt);
27781 while( precision>length ){
27782 *(--bufpt) = '0'; /* Zero pad */
27783 length++;
27784 }
27785 if( cThousand ){
27786 int nn = (length - 1)/3; /* Number of "," to insert */
27787 int ix = (length - 1)%3 + 1;
27788 bufpt -= nn;
27789 for(idx=0; nn>0; idx++){
27790 bufpt[idx] = bufpt[idx+nn];
27791 ix--;
27792 if( ix==0 ){
27793 bufpt[++idx] = cThousand;
27794 nn--;
27795 ix = 3;
27796 }
27797 }
27798 }
27799 if( prefix ) *(--bufpt) = prefix; /* Add sign */
27800 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
27801 const char *pre;
27802 char x;
27803 pre = &aPrefix[infop->prefix];
27804 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
27805 }
27806 length = (int)(&zOut[nOut-1]-bufpt);
27807 break;
27808 case etFLOAT1:
27809 case etEXP2:
27810 case etGENERIC3:
27811 if( bArgList ){
27812 realvalue = getDoubleArg(pArgList);
27813 }else{
27814 realvalue = va_arg(ap,double)__builtin_va_arg(ap, double);
27815 }
27816#ifdef SQLITE_OMIT_FLOATING_POINT
27817 length = 0;
27818#else
27819 if( precision<0 ) precision = 6; /* Set default precision */
27820 if( realvalue<0.0 ){
27821 realvalue = -realvalue;
27822 prefix = '-';
27823 }else{
27824 prefix = flag_prefix;
27825 }
27826 if( xtype==etGENERIC3 && precision>0 ) precision--;
27827 testcase( precision>0xfff );
27828 idx = precision & 0xfff;
27829 rounder = arRound[idx%10];
27830 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
27831 if( xtype==etFLOAT1 ){
27832 double rx = (double)realvalue;
27833 sqlite3_uint64 u;
27834 int ex;
27835 memcpy(&u, &rx, sizeof(u));
27836 ex = -1023 + (int)((u>>52)&0x7ff);
27837 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
27838 realvalue += rounder;
27839 }
27840 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
27841 exp = 0;
27842 if( sqlite3IsNaN((double)realvalue) ){
27843 bufpt = "NaN";
27844 length = 3;
27845 break;
27846 }
27847 if( realvalue>0.0 ){
27848 LONGDOUBLE_TYPElong double scale = 1.0;
27849 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
27850 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
27851 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
27852 realvalue /= scale;
27853 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
27854 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
27855 if( exp>350 ){
27856 bufpt = buf;
27857 buf[0] = prefix;
27858 memcpy(buf+(prefix!=0),"Inf",4);
27859 length = 3+(prefix!=0);
27860 break;
27861 }
27862 }
27863 bufpt = buf;
27864 /*
27865 ** If the field type is etGENERIC, then convert to either etEXP
27866 ** or etFLOAT, as appropriate.
27867 */
27868 if( xtype!=etFLOAT1 ){
27869 realvalue += rounder;
27870 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
27871 }
27872 if( xtype==etGENERIC3 ){
27873 flag_rtz = !flag_alternateform;
27874 if( exp<-4 || exp>precision ){
27875 xtype = etEXP2;
27876 }else{
27877 precision = precision - exp;
27878 xtype = etFLOAT1;
27879 }
27880 }else{
27881 flag_rtz = flag_altform2;
27882 }
27883 if( xtype==etEXP2 ){
27884 e2 = 0;
27885 }else{
27886 e2 = exp;
27887 }
27888 {
27889 i64 szBufNeeded; /* Size of a temporary buffer needed */
27890 szBufNeeded = MAX(e2,0)((e2)>(0)?(e2):(0))+(i64)precision+(i64)width+15;
27891 if( szBufNeeded > etBUFSIZE70 ){
27892 bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
27893 if( bufpt==0 ) return;
27894 }
27895 }
27896 zOut = bufpt;
27897 nsd = 16 + flag_altform2*10;
27898 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
27899 /* The sign in front of the number */
27900 if( prefix ){
27901 *(bufpt++) = prefix;
27902 }
27903 /* Digits prior to the decimal point */
27904 if( e2<0 ){
27905 *(bufpt++) = '0';
27906 }else{
27907 for(; e2>=0; e2--){
27908 *(bufpt++) = et_getdigit(&realvalue,&nsd);
27909 }
27910 }
27911 /* The decimal point */
27912 if( flag_dp ){
27913 *(bufpt++) = '.';
27914 }
27915 /* "0" digits after the decimal point but before the first
27916 ** significant digit of the number */
27917 for(e2++; e2<0; precision--, e2++){
27918 assert( precision>0 )((void) (0));
27919 *(bufpt++) = '0';
27920 }
27921 /* Significant digits after the decimal point */
27922 while( (precision--)>0 ){
27923 *(bufpt++) = et_getdigit(&realvalue,&nsd);
27924 }
27925 /* Remove trailing zeros and the "." if no digits follow the "." */
27926 if( flag_rtz && flag_dp ){
27927 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
27928 assert( bufpt>zOut )((void) (0));
27929 if( bufpt[-1]=='.' ){
27930 if( flag_altform2 ){
27931 *(bufpt++) = '0';
27932 }else{
27933 *(--bufpt) = 0;
27934 }
27935 }
27936 }
27937 /* Add the "eNNN" suffix */
27938 if( xtype==etEXP2 ){
27939 *(bufpt++) = aDigits[infop->charset];
27940 if( exp<0 ){
27941 *(bufpt++) = '-'; exp = -exp;
27942 }else{
27943 *(bufpt++) = '+';
27944 }
27945 if( exp>=100 ){
27946 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
27947 exp %= 100;
27948 }
27949 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
27950 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
27951 }
27952 *bufpt = 0;
27953
27954 /* The converted number is in buf[] and zero terminated. Output it.
27955 ** Note that the number is in the usual order, not reversed as with
27956 ** integer conversions. */
27957 length = (int)(bufpt-zOut);
27958 bufpt = zOut;
27959
27960 /* Special case: Add leading zeros if the flag_zeropad flag is
27961 ** set and we are not left justified */
27962 if( flag_zeropad && !flag_leftjustify && length < width){
27963 int i;
27964 int nPad = width - length;
27965 for(i=width; i>=nPad; i--){
27966 bufpt[i] = bufpt[i-nPad];
27967 }
27968 i = prefix!=0;
27969 while( nPad-- ) bufpt[i++] = '0';
27970 length = width;
27971 }
27972#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
27973 break;
27974 case etSIZE4:
27975 if( !bArgList ){
27976 *(va_arg(ap,int*)__builtin_va_arg(ap, int*)) = pAccum->nChar;
27977 }
27978 length = width = 0;
27979 break;
27980 case etPERCENT7:
27981 buf[0] = '%';
27982 bufpt = buf;
27983 length = 1;
27984 break;
27985 case etCHARX8:
27986 if( bArgList ){
27987 bufpt = getTextArg(pArgList);
27988 length = 1;
27989 if( bufpt ){
27990 buf[0] = c = *(bufpt++);
27991 if( (c&0xc0)==0xc0 ){
27992 while( length<4 && (bufpt[0]&0xc0)==0x80 ){
27993 buf[length++] = *(bufpt++);
27994 }
27995 }
27996 }else{
27997 buf[0] = 0;
27998 }
27999 }else{
28000 unsigned int ch = va_arg(ap,unsigned int)__builtin_va_arg(ap, unsigned int);
28001 if( ch<0x00080 ){
28002 buf[0] = ch & 0xff;
28003 length = 1;
28004 }else if( ch<0x00800 ){
28005 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
28006 buf[1] = 0x80 + (u8)(ch & 0x3f);
28007 length = 2;
28008 }else if( ch<0x10000 ){
28009 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
28010 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
28011 buf[2] = 0x80 + (u8)(ch & 0x3f);
28012 length = 3;
28013 }else{
28014 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
28015 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
28016 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
28017 buf[3] = 0x80 + (u8)(ch & 0x3f);
28018 length = 4;
28019 }
28020 }
28021 if( precision>1 ){
28022 width -= precision-1;
28023 if( width>1 && !flag_leftjustify ){
28024 sqlite3_str_appendchar(pAccum, width-1, ' ');
28025 width = 0;
28026 }
28027 while( precision-- > 1 ){
28028 sqlite3_str_append(pAccum, buf, length);
28029 }
28030 }
28031 bufpt = buf;
28032 flag_altform2 = 1;
28033 goto adjust_width_for_utf8;
28034 case etSTRING5:
28035 case etDYNSTRING6:
28036 if( bArgList ){
28037 bufpt = getTextArg(pArgList);
28038 xtype = etSTRING5;
28039 }else{
28040 bufpt = va_arg(ap,char*)__builtin_va_arg(ap, char*);
28041 }
28042 if( bufpt==0 ){
28043 bufpt = "";
28044 }else if( xtype==etDYNSTRING6 ){
28045 if( pAccum->nChar==0
28046 && pAccum->mxAlloc
28047 && width==0
28048 && precision<0
28049 && pAccum->accError==0
28050 ){
28051 /* Special optimization for sqlite3_mprintf("%z..."):
28052 ** Extend an existing memory allocation rather than creating
28053 ** a new one. */
28054 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 )((void) (0));
28055 pAccum->zText = bufpt;
28056 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
28057 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
28058 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
28059 length = 0;
28060 break;
28061 }
28062 zExtra = bufpt;
28063 }
28064 if( precision>=0 ){
28065 if( flag_altform2 ){
28066 /* Set length to the number of bytes needed in order to display
28067 ** precision characters */
28068 unsigned char *z = (unsigned char*)bufpt;
28069 while( precision-- > 0 && z[0] ){
28070 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
28071 }
28072 length = (int)(z - (unsigned char*)bufpt);
28073 }else{
28074 for(length=0; length<precision && bufpt[length]; length++){}
28075 }
28076 }else{
28077 length = 0x7fffffff & (int)strlen(bufpt);
28078 }
28079 adjust_width_for_utf8:
28080 if( flag_altform2 && width>0 ){
28081 /* Adjust width to account for extra bytes in UTF-8 characters */
28082 int ii = length - 1;
28083 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
28084 }
28085 break;
28086 case etSQLESCAPE9: /* %q: Escape ' characters */
28087 case etSQLESCAPE210: /* %Q: Escape ' and enclose in '...' */
28088 case etSQLESCAPE314: { /* %w: Escape " characters */
28089 int i, j, k, n, isnull;
28090 int needQuote;
28091 char ch;
28092 char q = ((xtype==etSQLESCAPE314)?'"':'\''); /* Quote character */
28093 char *escarg;
28094
28095 if( bArgList ){
28096 escarg = getTextArg(pArgList);
28097 }else{
28098 escarg = va_arg(ap,char*)__builtin_va_arg(ap, char*);
28099 }
28100 isnull = escarg==0;
28101 if( isnull ) escarg = (xtype==etSQLESCAPE210 ? "NULL" : "(NULL)");
28102 /* For %q, %Q, and %w, the precision is the number of byte (or
28103 ** characters if the ! flags is present) to use from the input.
28104 ** Because of the extra quoting characters inserted, the number
28105 ** of output characters may be larger than the precision.
28106 */
28107 k = precision;
28108 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
28109 if( ch==q ) n++;
28110 if( flag_altform2 && (ch&0xc0)==0xc0 ){
28111 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
28112 }
28113 }
28114 needQuote = !isnull && xtype==etSQLESCAPE210;
28115 n += i + 3;
28116 if( n>etBUFSIZE70 ){
28117 bufpt = zExtra = printfTempBuf(pAccum, n);
28118 if( bufpt==0 ) return;
28119 }else{
28120 bufpt = buf;
28121 }
28122 j = 0;
28123 if( needQuote ) bufpt[j++] = q;
28124 k = i;
28125 for(i=0; i<k; i++){
28126 bufpt[j++] = ch = escarg[i];
28127 if( ch==q ) bufpt[j++] = ch;
28128 }
28129 if( needQuote ) bufpt[j++] = q;
28130 bufpt[j] = 0;
28131 length = j;
28132 goto adjust_width_for_utf8;
28133 }
28134 case etTOKEN11: {
28135 Token *pToken;
28136 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL0x01)==0 ) return;
28137 pToken = va_arg(ap, Token*)__builtin_va_arg(ap, Token*);
28138 assert( bArgList==0 )((void) (0));
28139 if( pToken && pToken->n ){
28140 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
28141 }
28142 length = width = 0;
28143 break;
28144 }
28145 case etSRCLIST12: {
28146 SrcList *pSrc;
28147 int k;
28148 struct SrcList_item *pItem;
28149 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL0x01)==0 ) return;
28150 pSrc = va_arg(ap, SrcList*)__builtin_va_arg(ap, SrcList*);
28151 k = va_arg(ap, int)__builtin_va_arg(ap, int);
28152 pItem = &pSrc->a[k];
28153 assert( bArgList==0 )((void) (0));
28154 assert( k>=0 && k<pSrc->nSrc )((void) (0));
28155 if( pItem->zDatabase ){
28156 sqlite3_str_appendall(pAccum, pItem->zDatabase);
28157 sqlite3_str_append(pAccum, ".", 1);
28158 }
28159 sqlite3_str_appendall(pAccum, pItem->zName);
28160 length = width = 0;
28161 break;
28162 }
28163 default: {
28164 assert( xtype==etINVALID )((void) (0));
28165 return;
28166 }
28167 }/* End switch over the format type */
28168 /*
28169 ** The text of the conversion is pointed to by "bufpt" and is
28170 ** "length" characters long. The field width is "width". Do
28171 ** the output. Both length and width are in bytes, not characters,
28172 ** at this point. If the "!" flag was present on string conversions
28173 ** indicating that width and precision should be expressed in characters,
28174 ** then the values have been translated prior to reaching this point.
28175 */
28176 width -= length;
28177 if( width>0 ){
28178 if( !flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
28179 sqlite3_str_append(pAccum, bufpt, length);
28180 if( flag_leftjustify ) sqlite3_str_appendchar(pAccum, width, ' ');
28181 }else{
28182 sqlite3_str_append(pAccum, bufpt, length);
28183 }
28184
28185 if( zExtra ){
28186 sqlite3DbFree(pAccum->db, zExtra);
28187 zExtra = 0;
28188 }
28189 }/* End for loop over the format string */
28190} /* End of function */
28191
28192/*
28193** Enlarge the memory allocation on a StrAccum object so that it is
28194** able to accept at least N more bytes of text.
28195**
28196** Return the number of bytes of text that StrAccum is able to accept
28197** after the attempted enlargement. The value returned might be zero.
28198*/
28199static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
28200 char *zNew;
28201 assert( p->nChar+(i64)N >= p->nAlloc )((void) (0)); /* Only called if really needed */
28202 if( p->accError ){
28203 testcase(p->accError==SQLITE_TOOBIG);
28204 testcase(p->accError==SQLITE_NOMEM);
28205 return 0;
28206 }
28207 if( p->mxAlloc==0 ){
28208 setStrAccumError(p, SQLITE_TOOBIG18);
28209 return p->nAlloc - p->nChar - 1;
28210 }else{
28211 char *zOld = isMalloced(p)(((p)->printfFlags & 0x04)!=0) ? p->zText : 0;
28212 i64 szNew = p->nChar;
28213 szNew += N + 1;
28214 if( szNew+p->nChar<=p->mxAlloc ){
28215 /* Force exponential buffer size growth as long as it does not overflow,
28216 ** to avoid having to call this routine too often */
28217 szNew += p->nChar;
28218 }
28219 if( szNew > p->mxAlloc ){
28220 sqlite3_str_reset(p);
28221 setStrAccumError(p, SQLITE_TOOBIG18);
28222 return 0;
28223 }else{
28224 p->nAlloc = (int)szNew;
28225 }
28226 if( p->db ){
28227 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
28228 }else{
28229 zNew = sqlite3_realloc64(zOld, p->nAlloc);
28230 }
28231 if( zNew ){
28232 assert( p->zText!=0 || p->nChar==0 )((void) (0));
28233 if( !isMalloced(p)(((p)->printfFlags & 0x04)!=0) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
28234 p->zText = zNew;
28235 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
28236 p->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
28237 }else{
28238 sqlite3_str_reset(p);
28239 setStrAccumError(p, SQLITE_NOMEM7);
28240 return 0;
28241 }
28242 }
28243 return N;
28244}
28245
28246/*
28247** Append N copies of character c to the given string buffer.
28248*/
28249SQLITE_API void sqlite3_str_appendchar(sqlite3_str *p, int N, char c){
28250 testcase( p->nChar + (i64)N > 0x7fffffff );
28251 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
28252 return;
28253 }
28254 while( (N--)>0 ) p->zText[p->nChar++] = c;
28255}
28256
28257/*
28258** The StrAccum "p" is not large enough to accept N new bytes of z[].
28259** So enlarge if first, then do the append.
28260**
28261** This is a helper routine to sqlite3_str_append() that does special-case
28262** work (enlarging the buffer) using tail recursion, so that the
28263** sqlite3_str_append() routine can use fast calling semantics.
28264*/
28265static void SQLITE_NOINLINE__attribute__((noinline)) enlargeAndAppend(StrAccum *p, const char *z, int N){
28266 N = sqlite3StrAccumEnlarge(p, N);
28267 if( N>0 ){
28268 memcpy(&p->zText[p->nChar], z, N);
28269 p->nChar += N;
28270 }
28271}
28272
28273/*
28274** Append N bytes of text from z to the StrAccum object. Increase the
28275** size of the memory allocation for StrAccum if necessary.
28276*/
28277SQLITE_API void sqlite3_str_append(sqlite3_str *p, const char *z, int N){
28278 assert( z!=0 || N==0 )((void) (0));
28279 assert( p->zText!=0 || p->nChar==0 || p->accError )((void) (0));
28280 assert( N>=0 )((void) (0));
28281 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 )((void) (0));
28282 if( p->nChar+N >= p->nAlloc ){
28283 enlargeAndAppend(p,z,N);
28284 }else if( N ){
28285 assert( p->zText )((void) (0));
28286 p->nChar += N;
28287 memcpy(&p->zText[p->nChar-N], z, N);
28288 }
28289}
28290
28291/*
28292** Append the complete text of zero-terminated string z[] to the p string.
28293*/
28294SQLITE_API void sqlite3_str_appendall(sqlite3_str *p, const char *z){
28295 sqlite3_str_append(p, z, sqlite3Strlen30(z));
28296}
28297
28298
28299/*
28300** Finish off a string by making sure it is zero-terminated.
28301** Return a pointer to the resulting string. Return a NULL
28302** pointer if any kind of error was encountered.
28303*/
28304static SQLITE_NOINLINE__attribute__((noinline)) char *strAccumFinishRealloc(StrAccum *p){
28305 char *zText;
28306 assert( p->mxAlloc>0 && !isMalloced(p) )((void) (0));
28307 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
28308 if( zText ){
28309 memcpy(zText, p->zText, p->nChar+1);
28310 p->printfFlags |= SQLITE_PRINTF_MALLOCED0x04;
28311 }else{
28312 setStrAccumError(p, SQLITE_NOMEM7);
28313 }
28314 p->zText = zText;
28315 return zText;
28316}
28317SQLITE_PRIVATEstatic char *sqlite3StrAccumFinish(StrAccum *p){
28318 if( p->zText ){
28319 p->zText[p->nChar] = 0;
28320 if( p->mxAlloc>0 && !isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
28321 return strAccumFinishRealloc(p);
28322 }
28323 }
28324 return p->zText;
28325}
28326
28327/*
28328** This singleton is an sqlite3_str object that is returned if
28329** sqlite3_malloc() fails to provide space for a real one. This
28330** sqlite3_str object accepts no new text and always returns
28331** an SQLITE_NOMEM error.
28332*/
28333static sqlite3_str sqlite3OomStr = {
28334 0, 0, 0, 0, 0, SQLITE_NOMEM7, 0
28335};
28336
28337/* Finalize a string created using sqlite3_str_new().
28338*/
28339SQLITE_API char *sqlite3_str_finish(sqlite3_str *p){
28340 char *z;
28341 if( p!=0 && p!=&sqlite3OomStr ){
28342 z = sqlite3StrAccumFinish(p);
28343 sqlite3_free(p);
28344 }else{
28345 z = 0;
28346 }
28347 return z;
28348}
28349
28350/* Return any error code associated with p */
28351SQLITE_API int sqlite3_str_errcode(sqlite3_str *p){
28352 return p ? p->accError : SQLITE_NOMEM7;
28353}
28354
28355/* Return the current length of p in bytes */
28356SQLITE_API int sqlite3_str_length(sqlite3_str *p){
28357 return p ? p->nChar : 0;
28358}
28359
28360/* Return the current value for p */
28361SQLITE_API char *sqlite3_str_value(sqlite3_str *p){
28362 if( p==0 || p->nChar==0 ) return 0;
28363 p->zText[p->nChar] = 0;
28364 return p->zText;
28365}
28366
28367/*
28368** Reset an StrAccum string. Reclaim all malloced memory.
28369*/
28370SQLITE_API void sqlite3_str_reset(StrAccum *p){
28371 if( isMalloced(p)(((p)->printfFlags & 0x04)!=0) ){
28372 sqlite3DbFree(p->db, p->zText);
28373 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED0x04;
28374 }
28375 p->nAlloc = 0;
28376 p->nChar = 0;
28377 p->zText = 0;
28378}
28379
28380/*
28381** Initialize a string accumulator.
28382**
28383** p: The accumulator to be initialized.
28384** db: Pointer to a database connection. May be NULL. Lookaside
28385** memory is used if not NULL. db->mallocFailed is set appropriately
28386** when not NULL.
28387** zBase: An initial buffer. May be NULL in which case the initial buffer
28388** is malloced.
28389** n: Size of zBase in bytes. If total space requirements never exceed
28390** n then no memory allocations ever occur.
28391** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
28392** allocations will ever occur.
28393*/
28394SQLITE_PRIVATEstatic void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
28395 p->zText = zBase;
28396 p->db = db;
28397 p->nAlloc = n;
28398 p->mxAlloc = mx;
28399 p->nChar = 0;
28400 p->accError = 0;
28401 p->printfFlags = 0;
28402}
28403
28404/* Allocate and initialize a new dynamic string object */
28405SQLITE_API sqlite3_str *sqlite3_str_new(sqlite3 *db){
28406 sqlite3_str *p = sqlite3_malloc64(sizeof(*p));
28407 if( p ){
28408 sqlite3StrAccumInit(p, 0, 0, 0,
28409 db ? db->aLimit[SQLITE_LIMIT_LENGTH0] : SQLITE_MAX_LENGTH1000000000);
28410 }else{
28411 p = &sqlite3OomStr;
28412 }
28413 return p;
28414}
28415
28416/*
28417** Print into memory obtained from sqliteMalloc(). Use the internal
28418** %-conversion extensions.
28419*/
28420SQLITE_PRIVATEstatic char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
28421 char *z;
28422 char zBase[SQLITE_PRINT_BUF_SIZE70];
28423 StrAccum acc;
28424 assert( db!=0 )((void) (0));
28425 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
28426 db->aLimit[SQLITE_LIMIT_LENGTH0]);
28427 acc.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
28428 sqlite3_str_vappendf(&acc, zFormat, ap);
28429 z = sqlite3StrAccumFinish(&acc);
28430 if( acc.accError==SQLITE_NOMEM7 ){
28431 sqlite3OomFault(db);
28432 }
28433 return z;
28434}
28435
28436/*
28437** Print into memory obtained from sqliteMalloc(). Use the internal
28438** %-conversion extensions.
28439*/
28440SQLITE_PRIVATEstatic char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
28441 va_list ap;
28442 char *z;
28443 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
28444 z = sqlite3VMPrintf(db, zFormat, ap);
28445 va_end(ap)__builtin_va_end(ap);
28446 return z;
28447}
28448
28449/*
28450** Print into memory obtained from sqlite3_malloc(). Omit the internal
28451** %-conversion extensions.
28452*/
28453SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
28454 char *z;
28455 char zBase[SQLITE_PRINT_BUF_SIZE70];
28456 StrAccum acc;
28457
28458#ifdef SQLITE_ENABLE_API_ARMOR
28459 if( zFormat==0 ){
28460 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(28460);
28461 return 0;
28462 }
28463#endif
28464#ifndef SQLITE_OMIT_AUTOINIT
28465 if( sqlite3_initialize() ) return 0;
28466#endif
28467 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH1000000000);
28468 sqlite3_str_vappendf(&acc, zFormat, ap);
28469 z = sqlite3StrAccumFinish(&acc);
28470 return z;
28471}
28472
28473/*
28474** Print into memory obtained from sqlite3_malloc()(). Omit the internal
28475** %-conversion extensions.
28476*/
28477SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
28478 va_list ap;
28479 char *z;
28480#ifndef SQLITE_OMIT_AUTOINIT
28481 if( sqlite3_initialize() ) return 0;
28482#endif
28483 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
28484 z = sqlite3_vmprintf(zFormat, ap);
28485 va_end(ap)__builtin_va_end(ap);
28486 return z;
28487}
28488
28489/*
28490** sqlite3_snprintf() works like snprintf() except that it ignores the
28491** current locale settings. This is important for SQLite because we
28492** are not able to use a "," as the decimal point in place of "." as
28493** specified by some locales.
28494**
28495** Oops: The first two arguments of sqlite3_snprintf() are backwards
28496** from the snprintf() standard. Unfortunately, it is too late to change
28497** this without breaking compatibility, so we just have to live with the
28498** mistake.
28499**
28500** sqlite3_vsnprintf() is the varargs version.
28501*/
28502SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
28503 StrAccum acc;
28504 if( n<=0 ) return zBuf;
28505#ifdef SQLITE_ENABLE_API_ARMOR
28506 if( zBuf==0 || zFormat==0 ) {
28507 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(28507);
28508 if( zBuf ) zBuf[0] = 0;
28509 return zBuf;
28510 }
28511#endif
28512 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
28513 sqlite3_str_vappendf(&acc, zFormat, ap);
28514 zBuf[acc.nChar] = 0;
28515 return zBuf;
28516}
28517SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
28518 char *z;
28519 va_list ap;
28520 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
28521 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
28522 va_end(ap)__builtin_va_end(ap);
28523 return z;
28524}
28525
28526/*
28527** This is the routine that actually formats the sqlite3_log() message.
28528** We house it in a separate routine from sqlite3_log() to avoid using
28529** stack space on small-stack systems when logging is disabled.
28530**
28531** sqlite3_log() must render into a static buffer. It cannot dynamically
28532** allocate memory because it might be called while the memory allocator
28533** mutex is held.
28534**
28535** sqlite3_str_vappendf() might ask for *temporary* memory allocations for
28536** certain format characters (%q) or for very large precisions or widths.
28537** Care must be taken that any sqlite3_log() calls that occur while the
28538** memory mutex is held do not use these mechanisms.
28539*/
28540static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
28541 StrAccum acc; /* String accumulator */
28542 char zMsg[SQLITE_PRINT_BUF_SIZE70*3]; /* Complete log message */
28543
28544 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
28545 sqlite3_str_vappendf(&acc, zFormat, ap);
28546 sqlite3GlobalConfigsqlite3Config.xLog(sqlite3GlobalConfigsqlite3Config.pLogArg, iErrCode,
28547 sqlite3StrAccumFinish(&acc));
28548}
28549
28550/*
28551** Format and write a message to the log if logging is enabled.
28552*/
28553SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
28554 va_list ap; /* Vararg list */
28555 if( sqlite3GlobalConfigsqlite3Config.xLog ){
28556 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
28557 renderLogMsg(iErrCode, zFormat, ap);
28558 va_end(ap)__builtin_va_end(ap);
28559 }
28560}
28561
28562#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
28563/*
28564** A version of printf() that understands %lld. Used for debugging.
28565** The printf() built into some versions of windows does not understand %lld
28566** and segfaults if you give it a long long int.
28567*/
28568SQLITE_PRIVATEstatic void sqlite3DebugPrintf(const char *zFormat, ...){
28569 va_list ap;
28570 StrAccum acc;
28571 char zBuf[500];
28572 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
28573 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
28574 sqlite3_str_vappendf(&acc, zFormat, ap);
28575 va_end(ap)__builtin_va_end(ap);
28576 sqlite3StrAccumFinish(&acc);
28577#ifdef SQLITE_OS_TRACE_PROC
28578 {
28579 extern void SQLITE_OS_TRACE_PROC(const char *zBuf, int nBuf);
28580 SQLITE_OS_TRACE_PROC(zBuf, sizeof(zBuf));
28581 }
28582#else
28583 fprintf(stdoutstdout,"%s", zBuf);
28584 fflush(stdoutstdout);
28585#endif
28586}
28587#endif
28588
28589
28590/*
28591** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
28592** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
28593*/
28594SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
28595 va_list ap;
28596 va_start(ap,zFormat)__builtin_va_start(ap, zFormat);
28597 sqlite3_str_vappendf(p, zFormat, ap);
28598 va_end(ap)__builtin_va_end(ap);
28599}
28600
28601/************** End of printf.c **********************************************/
28602/************** Begin file treeview.c ****************************************/
28603/*
28604** 2015-06-08
28605**
28606** The author disclaims copyright to this source code. In place of
28607** a legal notice, here is a blessing:
28608**
28609** May you do good and not evil.
28610** May you find forgiveness for yourself and forgive others.
28611** May you share freely, never taking more than you give.
28612**
28613*************************************************************************
28614**
28615** This file contains C code to implement the TreeView debugging routines.
28616** These routines print a parse tree to standard output for debugging and
28617** analysis.
28618**
28619** The interfaces in this file is only available when compiling
28620** with SQLITE_DEBUG.
28621*/
28622/* #include "sqliteInt.h" */
28623#ifdef SQLITE_DEBUG
28624
28625/*
28626** Add a new subitem to the tree. The moreToFollow flag indicates that this
28627** is not the last item in the tree.
28628*/
28629static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
28630 if( p==0 ){
28631 p = sqlite3_malloc64( sizeof(*p) );
28632 if( p==0 ) return 0;
28633 memset(p, 0, sizeof(*p));
28634 }else{
28635 p->iLevel++;
28636 }
28637 assert( moreToFollow==0 || moreToFollow==1 )((void) (0));
28638 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
28639 return p;
28640}
28641
28642/*
28643** Finished with one layer of the tree
28644*/
28645static void sqlite3TreeViewPop(TreeView *p){
28646 if( p==0 ) return;
28647 p->iLevel--;
28648 if( p->iLevel<0 ) sqlite3_free(p);
28649}
28650
28651/*
28652** Generate a single line of output for the tree, with a prefix that contains
28653** all the appropriate tree lines
28654*/
28655static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
28656 va_list ap;
28657 int i;
28658 StrAccum acc;
28659 char zBuf[500];
28660 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
28661 if( p ){
28662 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
28663 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
28664 }
28665 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
28666 }
28667 if( zFormat!=0 ){
28668 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
28669 sqlite3_str_vappendf(&acc, zFormat, ap);
28670 va_end(ap)__builtin_va_end(ap);
28671 assert( acc.nChar>0 )((void) (0));
28672 sqlite3_str_append(&acc, "\n", 1);
28673 }
28674 sqlite3StrAccumFinish(&acc);
28675 fprintf(stdoutstdout,"%s", zBuf);
28676 fflush(stdoutstdout);
28677}
28678
28679/*
28680** Shorthand for starting a new tree item that consists of a single label
28681*/
28682static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
28683 p = sqlite3TreeViewPush(p, moreFollows);
28684 sqlite3TreeViewLine(p, "%s", zLabel);
28685}
28686
28687/*
28688** Generate a human-readable description of a WITH clause.
28689*/
28690SQLITE_PRIVATEstatic void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 moreToFollow){
28691 int i;
28692 if( pWith==0 ) return;
28693 if( pWith->nCte==0 ) return;
28694 if( pWith->pOuter ){
28695 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
28696 }else{
28697 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
28698 }
28699 if( pWith->nCte>0 ){
28700 pView = sqlite3TreeViewPush(pView, 1);
28701 for(i=0; i<pWith->nCte; i++){
28702 StrAccum x;
28703 char zLine[1000];
28704 const struct Cte *pCte = &pWith->a[i];
28705 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
28706 sqlite3_str_appendf(&x, "%s", pCte->zName);
28707 if( pCte->pCols && pCte->pCols->nExpr>0 ){
28708 char cSep = '(';
28709 int j;
28710 for(j=0; j<pCte->pCols->nExpr; j++){
28711 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
28712 cSep = ',';
28713 }
28714 sqlite3_str_appendf(&x, ")");
28715 }
28716 sqlite3_str_appendf(&x, " AS");
28717 sqlite3StrAccumFinish(&x);
28718 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
28719 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
28720 sqlite3TreeViewPop(pView);
28721 }
28722 sqlite3TreeViewPop(pView);
28723 }
28724}
28725
28726/*
28727** Generate a human-readable description of a SrcList object.
28728*/
28729SQLITE_PRIVATEstatic void sqlite3TreeViewSrcList(TreeView *pView, const SrcList *pSrc){
28730 int i;
28731 for(i=0; i<pSrc->nSrc; i++){
28732 const struct SrcList_item *pItem = &pSrc->a[i];
28733 StrAccum x;
28734 char zLine[100];
28735 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
28736 sqlite3_str_appendf(&x, "{%d,*}", pItem->iCursor);
28737 if( pItem->zDatabase ){
28738 sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
28739 }else if( pItem->zName ){
28740 sqlite3_str_appendf(&x, " %s", pItem->zName);
28741 }
28742 if( pItem->pTab ){
28743 sqlite3_str_appendf(&x, " tab=%Q nCol=%d ptr=%p",
28744 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab);
28745 }
28746 if( pItem->zAlias ){
28747 sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
28748 }
28749 if( pItem->fg.jointype & JT_LEFT0x0008 ){
28750 sqlite3_str_appendf(&x, " LEFT-JOIN");
28751 }
28752 sqlite3StrAccumFinish(&x);
28753 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
28754 if( pItem->pSelect ){
28755 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
28756 }
28757 if( pItem->fg.isTabFunc ){
28758 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
28759 }
28760 sqlite3TreeViewPop(pView);
28761 }
28762}
28763
28764/*
28765** Generate a human-readable description of a Select object.
28766*/
28767SQLITE_PRIVATEstatic void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
28768 int n = 0;
28769 int cnt = 0;
28770 if( p==0 ){
28771 sqlite3TreeViewLine(pView, "nil-SELECT");
28772 return;
28773 }
28774 pView = sqlite3TreeViewPush(pView, moreToFollow);
28775 if( p->pWith ){
28776 sqlite3TreeViewWith(pView, p->pWith, 1);
28777 cnt = 1;
28778 sqlite3TreeViewPush(pView, 1);
28779 }
28780 do{
28781 sqlite3TreeViewLine(pView,
28782 "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
28783 ((p->selFlags & SF_Distinct0x00001) ? " DISTINCT" : ""),
28784 ((p->selFlags & SF_Aggregate0x00008) ? " agg_flag" : ""),
28785 p->selId, p, p->selFlags,
28786 (int)p->nSelectRow
28787 );
28788 if( cnt++ ) sqlite3TreeViewPop(pView);
28789 if( p->pPrior ){
28790 n = 1000;
28791 }else{
28792 n = 0;
28793 if( p->pSrc && p->pSrc->nSrc ) n++;
28794 if( p->pWhere ) n++;
28795 if( p->pGroupBy ) n++;
28796 if( p->pHaving ) n++;
28797 if( p->pOrderBy ) n++;
28798 if( p->pLimit ) n++;
28799#ifndef SQLITE_OMIT_WINDOWFUNC
28800 if( p->pWin ) n++;
28801 if( p->pWinDefn ) n++;
28802#endif
28803 }
28804 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
28805#ifndef SQLITE_OMIT_WINDOWFUNC
28806 if( p->pWin ){
28807 Window *pX;
28808 pView = sqlite3TreeViewPush(pView, (n--)>0);
28809 sqlite3TreeViewLine(pView, "window-functions");
28810 for(pX=p->pWin; pX; pX=pX->pNextWin){
28811 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
28812 }
28813 sqlite3TreeViewPop(pView);
28814 }
28815#endif
28816 if( p->pSrc && p->pSrc->nSrc ){
28817 pView = sqlite3TreeViewPush(pView, (n--)>0);
28818 sqlite3TreeViewLine(pView, "FROM");
28819 sqlite3TreeViewSrcList(pView, p->pSrc);
28820 sqlite3TreeViewPop(pView);
28821 }
28822 if( p->pWhere ){
28823 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
28824 sqlite3TreeViewExpr(pView, p->pWhere, 0);
28825 sqlite3TreeViewPop(pView);
28826 }
28827 if( p->pGroupBy ){
28828 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
28829 }
28830 if( p->pHaving ){
28831 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
28832 sqlite3TreeViewExpr(pView, p->pHaving, 0);
28833 sqlite3TreeViewPop(pView);
28834 }
28835#ifndef SQLITE_OMIT_WINDOWFUNC
28836 if( p->pWinDefn ){
28837 Window *pX;
28838 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
28839 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
28840 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
28841 }
28842 sqlite3TreeViewPop(pView);
28843 }
28844#endif
28845 if( p->pOrderBy ){
28846 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
28847 }
28848 if( p->pLimit ){
28849 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
28850 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
28851 if( p->pLimit->pRight ){
28852 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
28853 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
28854 sqlite3TreeViewPop(pView);
28855 }
28856 sqlite3TreeViewPop(pView);
28857 }
28858 if( p->pPrior ){
28859 const char *zOp = "UNION";
28860 switch( p->op ){
28861 case TK_ALL128: zOp = "UNION ALL"; break;
28862 case TK_INTERSECT130: zOp = "INTERSECT"; break;
28863 case TK_EXCEPT129: zOp = "EXCEPT"; break;
28864 }
28865 sqlite3TreeViewItem(pView, zOp, 1);
28866 }
28867 p = p->pPrior;
28868 }while( p!=0 );
28869 sqlite3TreeViewPop(pView);
28870}
28871
28872#ifndef SQLITE_OMIT_WINDOWFUNC
28873/*
28874** Generate a description of starting or stopping bounds
28875*/
28876SQLITE_PRIVATEstatic void sqlite3TreeViewBound(
28877 TreeView *pView, /* View context */
28878 u8 eBound, /* UNBOUNDED, CURRENT, PRECEDING, FOLLOWING */
28879 Expr *pExpr, /* Value for PRECEDING or FOLLOWING */
28880 u8 moreToFollow /* True if more to follow */
28881){
28882 switch( eBound ){
28883 case TK_UNBOUNDED87: {
28884 sqlite3TreeViewItem(pView, "UNBOUNDED", moreToFollow);
28885 sqlite3TreeViewPop(pView);
28886 break;
28887 }
28888 case TK_CURRENT82: {
28889 sqlite3TreeViewItem(pView, "CURRENT", moreToFollow);
28890 sqlite3TreeViewPop(pView);
28891 break;
28892 }
28893 case TK_PRECEDING85: {
28894 sqlite3TreeViewItem(pView, "PRECEDING", moreToFollow);
28895 sqlite3TreeViewExpr(pView, pExpr, 0);
28896 sqlite3TreeViewPop(pView);
28897 break;
28898 }
28899 case TK_FOLLOWING83: {
28900 sqlite3TreeViewItem(pView, "FOLLOWING", moreToFollow);
28901 sqlite3TreeViewExpr(pView, pExpr, 0);
28902 sqlite3TreeViewPop(pView);
28903 break;
28904 }
28905 }
28906}
28907#endif /* SQLITE_OMIT_WINDOWFUNC */
28908
28909#ifndef SQLITE_OMIT_WINDOWFUNC
28910/*
28911** Generate a human-readable explanation for a Window object
28912*/
28913SQLITE_PRIVATEstatic void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u8 more){
28914 int nElement = 0;
28915 if( pWin->pFilter ){
28916 sqlite3TreeViewItem(pView, "FILTER", 1);
28917 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
28918 sqlite3TreeViewPop(pView);
28919 }
28920 pView = sqlite3TreeViewPush(pView, more);
28921 if( pWin->zName ){
28922 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
28923 }else{
28924 sqlite3TreeViewLine(pView, "OVER (%p)", pWin);
28925 }
28926 if( pWin->zBase ) nElement++;
28927 if( pWin->pOrderBy ) nElement++;
28928 if( pWin->eFrmType ) nElement++;
28929 if( pWin->eExclude ) nElement++;
28930 if( pWin->zBase ){
28931 sqlite3TreeViewPush(pView, (--nElement)>0);
28932 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
28933 sqlite3TreeViewPop(pView);
28934 }
28935 if( pWin->pPartition ){
28936 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
28937 }
28938 if( pWin->pOrderBy ){
28939 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
28940 }
28941 if( pWin->eFrmType ){
28942 char zBuf[30];
28943 const char *zFrmType = "ROWS";
28944 if( pWin->eFrmType==TK_RANGE86 ) zFrmType = "RANGE";
28945 if( pWin->eFrmType==TK_GROUPS89 ) zFrmType = "GROUPS";
28946 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s%s",zFrmType,
28947 pWin->bImplicitFrame ? " (implied)" : "");
28948 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
28949 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
28950 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
28951 sqlite3TreeViewPop(pView);
28952 }
28953 if( pWin->eExclude ){
28954 char zBuf[30];
28955 const char *zExclude;
28956 switch( pWin->eExclude ){
28957 case TK_NO66: zExclude = "NO OTHERS"; break;
28958 case TK_CURRENT82: zExclude = "CURRENT ROW"; break;
28959 case TK_GROUP139: zExclude = "GROUP"; break;
28960 case TK_TIES91: zExclude = "TIES"; break;
28961 default:
28962 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
28963 zExclude = zBuf;
28964 break;
28965 }
28966 sqlite3TreeViewPush(pView, 0);
28967 sqlite3TreeViewLine(pView, "EXCLUDE %s", zExclude);
28968 sqlite3TreeViewPop(pView);
28969 }
28970 sqlite3TreeViewPop(pView);
28971}
28972#endif /* SQLITE_OMIT_WINDOWFUNC */
28973
28974#ifndef SQLITE_OMIT_WINDOWFUNC
28975/*
28976** Generate a human-readable explanation for a Window Function object
28977*/
28978SQLITE_PRIVATEstatic void sqlite3TreeViewWinFunc(TreeView *pView, const Window *pWin, u8 more){
28979 pView = sqlite3TreeViewPush(pView, more);
28980 sqlite3TreeViewLine(pView, "WINFUNC %s(%d)",
28981 pWin->pFunc->zName, pWin->pFunc->nArg);
28982 sqlite3TreeViewWindow(pView, pWin, 0);
28983 sqlite3TreeViewPop(pView);
28984}
28985#endif /* SQLITE_OMIT_WINDOWFUNC */
28986
28987/*
28988** Generate a human-readable explanation of an expression tree.
28989*/
28990SQLITE_PRIVATEstatic void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
28991 const char *zBinOp = 0; /* Binary operator */
28992 const char *zUniOp = 0; /* Unary operator */
28993 char zFlgs[60];
28994 pView = sqlite3TreeViewPush(pView, moreToFollow);
28995 if( pExpr==0 ){
28996 sqlite3TreeViewLine(pView, "nil");
28997 sqlite3TreeViewPop(pView);
28998 return;
28999 }
29000 if( pExpr->flags ){
29001 if( ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0) ){
29002 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x iRJT=%d",
29003 pExpr->flags, pExpr->iRightJoinTable);
29004 }else{
29005 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags);
29006 }
29007 }else{
29008 zFlgs[0] = 0;
29009 }
29010 switch( pExpr->op ){
29011 case TK_AGG_COLUMN164: {
29012 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
29013 pExpr->iTable, pExpr->iColumn, zFlgs);
29014 break;
29015 }
29016 case TK_COLUMN162: {
29017 if( pExpr->iTable<0 ){
29018 /* This only happens when coding check constraints */
29019 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
29020 }else{
29021 sqlite3TreeViewLine(pView, "{%d:%d}%s",
29022 pExpr->iTable, pExpr->iColumn, zFlgs);
29023 }
29024 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000008))!=0) ){
29025 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29026 }
29027 break;
29028 }
29029 case TK_INTEGER147: {
29030 if( pExpr->flags & EP_IntValue0x000400 ){
29031 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
29032 }else{
29033 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
29034 }
29035 break;
29036 }
29037#ifndef SQLITE_OMIT_FLOATING_POINT
29038 case TK_FLOAT145: {
29039 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
29040 break;
29041 }
29042#endif
29043 case TK_STRING110: {
29044 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
29045 break;
29046 }
29047 case TK_NULL114: {
29048 sqlite3TreeViewLine(pView,"NULL");
29049 break;
29050 }
29051 case TK_TRUEFALSE159: {
29052 sqlite3TreeViewLine(pView,
29053 sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
29054 break;
29055 }
29056#ifndef SQLITE_OMIT_BLOB_LITERAL
29057 case TK_BLOB146: {
29058 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
29059 break;
29060 }
29061#endif
29062 case TK_VARIABLE148: {
29063 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
29064 pExpr->u.zToken, pExpr->iColumn);
29065 break;
29066 }
29067 case TK_REGISTER168: {
29068 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
29069 break;
29070 }
29071 case TK_ID59: {
29072 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
29073 break;
29074 }
29075#ifndef SQLITE_OMIT_CAST
29076 case TK_CAST36: {
29077 /* Expressions of the form: CAST(pLeft AS token) */
29078 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
29079 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29080 break;
29081 }
29082#endif /* SQLITE_OMIT_CAST */
29083 case TK_LT56: zBinOp = "LT"; break;
29084 case TK_LE55: zBinOp = "LE"; break;
29085 case TK_GT54: zBinOp = "GT"; break;
29086 case TK_GE57: zBinOp = "GE"; break;
29087 case TK_NE52: zBinOp = "NE"; break;
29088 case TK_EQ53: zBinOp = "EQ"; break;
29089 case TK_IS45: zBinOp = "IS"; break;
29090 case TK_ISNOT160: zBinOp = "ISNOT"; break;
29091 case TK_AND44: zBinOp = "AND"; break;
29092 case TK_OR43: zBinOp = "OR"; break;
29093 case TK_PLUS100: zBinOp = "ADD"; break;
29094 case TK_STAR102: zBinOp = "MUL"; break;
29095 case TK_MINUS101: zBinOp = "SUB"; break;
29096 case TK_REM104: zBinOp = "REM"; break;
29097 case TK_BITAND96: zBinOp = "BITAND"; break;
29098 case TK_BITOR97: zBinOp = "BITOR"; break;
29099 case TK_SLASH103: zBinOp = "DIV"; break;
29100 case TK_LSHIFT98: zBinOp = "LSHIFT"; break;
29101 case TK_RSHIFT99: zBinOp = "RSHIFT"; break;
29102 case TK_CONCAT105: zBinOp = "CONCAT"; break;
29103 case TK_DOT134: zBinOp = "DOT"; break;
29104
29105 case TK_UMINUS165: zUniOp = "UMINUS"; break;
29106 case TK_UPLUS166: zUniOp = "UPLUS"; break;
29107 case TK_BITNOT107: zUniOp = "BITNOT"; break;
29108 case TK_NOT19: zUniOp = "NOT"; break;
29109 case TK_ISNULL50: zUniOp = "ISNULL"; break;
29110 case TK_NOTNULL51: zUniOp = "NOTNULL"; break;
29111
29112 case TK_TRUTH167: {
29113 int x;
29114 const char *azOp[] = {
29115 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
29116 };
29117 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT )((void) (0));
29118 assert( pExpr->pRight )((void) (0));
29119 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE )((void) (0));
29120 x = (pExpr->op2==TK_ISNOT160)*2 + sqlite3ExprTruthValue(pExpr->pRight);
29121 zUniOp = azOp[x];
29122 break;
29123 }
29124
29125 case TK_SPAN173: {
29126 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
29127 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29128 break;
29129 }
29130
29131 case TK_COLLATE106: {
29132 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
29133 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29134 break;
29135 }
29136
29137 case TK_AGG_FUNCTION163:
29138 case TK_FUNCTION161: {
29139 ExprList *pFarg; /* List of function arguments */
29140 Window *pWin;
29141 if( ExprHasProperty(pExpr, EP_TokenOnly)(((pExpr)->flags&(0x004000))!=0) ){
29142 pFarg = 0;
29143 pWin = 0;
29144 }else{
29145 pFarg = pExpr->x.pList;
29146#ifndef SQLITE_OMIT_WINDOWFUNC
29147 pWin = pExpr->y.pWin;
29148#else
29149 pWin = 0;
29150#endif
29151 }
29152 if( pExpr->op==TK_AGG_FUNCTION163 ){
29153 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
29154 pExpr->op2, pExpr->u.zToken);
29155 }else{
29156 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
29157 }
29158 if( pFarg ){
29159 sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
29160 }
29161#ifndef SQLITE_OMIT_WINDOWFUNC
29162 if( pWin ){
29163 sqlite3TreeViewWindow(pView, pWin, 0);
29164 }
29165#endif
29166 break;
29167 }
29168#ifndef SQLITE_OMIT_SUBQUERY
29169 case TK_EXISTS20: {
29170 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
29171 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
29172 break;
29173 }
29174 case TK_SELECT131: {
29175 sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
29176 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
29177 break;
29178 }
29179 case TK_IN49: {
29180 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
29181 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
29182 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
29183 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
29184 }else{
29185 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
29186 }
29187 break;
29188 }
29189#endif /* SQLITE_OMIT_SUBQUERY */
29190
29191 /*
29192 ** x BETWEEN y AND z
29193 **
29194 ** This is equivalent to
29195 **
29196 ** x>=y AND x<=z
29197 **
29198 ** X is stored in pExpr->pLeft.
29199 ** Y is stored in pExpr->pList->a[0].pExpr.
29200 ** Z is stored in pExpr->pList->a[1].pExpr.
29201 */
29202 case TK_BETWEEN48: {
29203 Expr *pX = pExpr->pLeft;
29204 Expr *pY = pExpr->x.pList->a[0].pExpr;
29205 Expr *pZ = pExpr->x.pList->a[1].pExpr;
29206 sqlite3TreeViewLine(pView, "BETWEEN");
29207 sqlite3TreeViewExpr(pView, pX, 1);
29208 sqlite3TreeViewExpr(pView, pY, 1);
29209 sqlite3TreeViewExpr(pView, pZ, 0);
29210 break;
29211 }
29212 case TK_TRIGGER77: {
29213 /* If the opcode is TK_TRIGGER, then the expression is a reference
29214 ** to a column in the new.* or old.* pseudo-tables available to
29215 ** trigger programs. In this case Expr.iTable is set to 1 for the
29216 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
29217 ** is set to the column of the pseudo-table to read, or to -1 to
29218 ** read the rowid field.
29219 */
29220 sqlite3TreeViewLine(pView, "%s(%d)",
29221 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
29222 break;
29223 }
29224 case TK_CASE149: {
29225 sqlite3TreeViewLine(pView, "CASE");
29226 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
29227 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
29228 break;
29229 }
29230#ifndef SQLITE_OMIT_TRIGGER
29231 case TK_RAISE71: {
29232 const char *zType = "unk";
29233 switch( pExpr->affinity ){
29234 case OE_Rollback1: zType = "rollback"; break;
29235 case OE_Abort2: zType = "abort"; break;
29236 case OE_Fail3: zType = "fail"; break;
29237 case OE_Ignore4: zType = "ignore"; break;
29238 }
29239 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
29240 break;
29241 }
29242#endif
29243 case TK_MATCH46: {
29244 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
29245 pExpr->iTable, pExpr->iColumn, zFlgs);
29246 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
29247 break;
29248 }
29249 case TK_VECTOR169: {
29250 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
29251 break;
29252 }
29253 case TK_SELECT_COLUMN170: {
29254 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
29255 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
29256 break;
29257 }
29258 case TK_IF_NULL_ROW171: {
29259 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
29260 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29261 break;
29262 }
29263 default: {
29264 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
29265 break;
29266 }
29267 }
29268 if( zBinOp ){
29269 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
29270 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
29271 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
29272 }else if( zUniOp ){
29273 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
29274 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29275 }
29276 sqlite3TreeViewPop(pView);
29277}
29278
29279
29280/*
29281** Generate a human-readable explanation of an expression list.
29282*/
29283SQLITE_PRIVATEstatic void sqlite3TreeViewBareExprList(
29284 TreeView *pView,
29285 const ExprList *pList,
29286 const char *zLabel
29287){
29288 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
29289 if( pList==0 ){
29290 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
29291 }else{
29292 int i;
29293 sqlite3TreeViewLine(pView, "%s", zLabel);
29294 for(i=0; i<pList->nExpr; i++){
29295 int j = pList->a[i].u.x.iOrderByCol;
29296 char *zName = pList->a[i].zName;
29297 int moreToFollow = i<pList->nExpr - 1;
29298 if( j || zName ){
29299 sqlite3TreeViewPush(pView, moreToFollow);
29300 moreToFollow = 0;
29301 sqlite3TreeViewLine(pView, 0);
29302 if( zName ){
29303 fprintf(stdoutstdout, "AS %s ", zName);
29304 }
29305 if( j ){
29306 fprintf(stdoutstdout, "iOrderByCol=%d", j);
29307 }
29308 fprintf(stdoutstdout, "\n");
29309 fflush(stdoutstdout);
29310 }
29311 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
29312 if( j || zName ){
29313 sqlite3TreeViewPop(pView);
29314 }
29315 }
29316 }
29317}
29318SQLITE_PRIVATEstatic void sqlite3TreeViewExprList(
29319 TreeView *pView,
29320 const ExprList *pList,
29321 u8 moreToFollow,
29322 const char *zLabel
29323){
29324 pView = sqlite3TreeViewPush(pView, moreToFollow);
29325 sqlite3TreeViewBareExprList(pView, pList, zLabel);
29326 sqlite3TreeViewPop(pView);
29327}
29328
29329#endif /* SQLITE_DEBUG */
29330
29331/************** End of treeview.c ********************************************/
29332/************** Begin file random.c ******************************************/
29333/*
29334** 2001 September 15
29335**
29336** The author disclaims copyright to this source code. In place of
29337** a legal notice, here is a blessing:
29338**
29339** May you do good and not evil.
29340** May you find forgiveness for yourself and forgive others.
29341** May you share freely, never taking more than you give.
29342**
29343*************************************************************************
29344** This file contains code to implement a pseudo-random number
29345** generator (PRNG) for SQLite.
29346**
29347** Random numbers are used by some of the database backends in order
29348** to generate random integer keys for tables or random filenames.
29349*/
29350/* #include "sqliteInt.h" */
29351
29352
29353/* All threads share a single random number generator.
29354** This structure is the current state of the generator.
29355*/
29356static SQLITE_WSD struct sqlite3PrngType {
29357 unsigned char isInit; /* True if initialized */
29358 unsigned char i, j; /* State variables */
29359 unsigned char s[256]; /* State variables */
29360} sqlite3Prng;
29361
29362/*
29363** Return N random bytes.
29364*/
29365SQLITE_API void sqlite3_randomness(int N, void *pBuf){
29366 unsigned char t;
29367 unsigned char *zBuf = pBuf;
29368
29369 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
29370 ** state vector. If writable static data is unsupported on the target,
29371 ** we have to locate the state vector at run-time. In the more common
29372 ** case where writable static data is supported, wsdPrng can refer directly
29373 ** to the "sqlite3Prng" state vector declared above.
29374 */
29375#ifdef SQLITE_OMIT_WSD
29376 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng;
29377# define wsdPrngsqlite3Prng p[0]
29378#else
29379# define wsdPrngsqlite3Prng sqlite3Prng
29380#endif
29381
29382#if SQLITE_THREADSAFE1
29383 sqlite3_mutex *mutex;
29384#endif
29385
29386#ifndef SQLITE_OMIT_AUTOINIT
29387 if( sqlite3_initialize() ) return;
29388#endif
29389
29390#if SQLITE_THREADSAFE1
29391 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG5);
29392#endif
29393
29394 sqlite3_mutex_enter(mutex);
29395 if( N<=0 || pBuf==0 ){
29396 wsdPrngsqlite3Prng.isInit = 0;
29397 sqlite3_mutex_leave(mutex);
29398 return;
29399 }
29400
29401 /* Initialize the state of the random number generator once,
29402 ** the first time this routine is called. The seed value does
29403 ** not need to contain a lot of randomness since we are not
29404 ** trying to do secure encryption or anything like that...
29405 **
29406 ** Nothing in this file or anywhere else in SQLite does any kind of
29407 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
29408 ** number generator) not as an encryption device.
29409 */
29410 if( !wsdPrngsqlite3Prng.isInit ){
29411 int i;
29412 char k[256];
29413 wsdPrngsqlite3Prng.j = 0;
29414 wsdPrngsqlite3Prng.i = 0;
29415 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
29416 for(i=0; i<256; i++){
29417 wsdPrngsqlite3Prng.s[i] = (u8)i;
29418 }
29419 for(i=0; i<256; i++){
29420 wsdPrngsqlite3Prng.j += wsdPrngsqlite3Prng.s[i] + k[i];
29421 t = wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.j];
29422 wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.j] = wsdPrngsqlite3Prng.s[i];
29423 wsdPrngsqlite3Prng.s[i] = t;
29424 }
29425 wsdPrngsqlite3Prng.isInit = 1;
29426 }
29427
29428 assert( N>0 )((void) (0));
29429 do{
29430 wsdPrngsqlite3Prng.i++;
29431 t = wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.i];
29432 wsdPrngsqlite3Prng.j += t;
29433 wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.i] = wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.j];
29434 wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.j] = t;
29435 t += wsdPrngsqlite3Prng.s[wsdPrngsqlite3Prng.i];
29436 *(zBuf++) = wsdPrngsqlite3Prng.s[t];
29437 }while( --N );
29438 sqlite3_mutex_leave(mutex);
29439}
29440
29441#ifndef SQLITE_UNTESTABLE
29442/*
29443** For testing purposes, we sometimes want to preserve the state of
29444** PRNG and restore the PRNG to its saved state at a later time, or
29445** to reset the PRNG to its initial state. These routines accomplish
29446** those tasks.
29447**
29448** The sqlite3_test_control() interface calls these routines to
29449** control the PRNG.
29450*/
29451static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
29452SQLITE_PRIVATEstatic void sqlite3PrngSaveState(void){
29453 memcpy(
29454 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng)sqlite3SavedPrng,
29455 &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng,
29456 sizeof(sqlite3Prng)
29457 );
29458}
29459SQLITE_PRIVATEstatic void sqlite3PrngRestoreState(void){
29460 memcpy(
29461 &GLOBAL(struct sqlite3PrngType, sqlite3Prng)sqlite3Prng,
29462 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng)sqlite3SavedPrng,
29463 sizeof(sqlite3Prng)
29464 );
29465}
29466#endif /* SQLITE_UNTESTABLE */
29467
29468/************** End of random.c **********************************************/
29469/************** Begin file threads.c *****************************************/
29470/*
29471** 2012 July 21
29472**
29473** The author disclaims copyright to this source code. In place of
29474** a legal notice, here is a blessing:
29475**
29476** May you do good and not evil.
29477** May you find forgiveness for yourself and forgive others.
29478** May you share freely, never taking more than you give.
29479**
29480******************************************************************************
29481**
29482** This file presents a simple cross-platform threading interface for
29483** use internally by SQLite.
29484**
29485** A "thread" can be created using sqlite3ThreadCreate(). This thread
29486** runs independently of its creator until it is joined using
29487** sqlite3ThreadJoin(), at which point it terminates.
29488**
29489** Threads do not have to be real. It could be that the work of the
29490** "thread" is done by the main thread at either the sqlite3ThreadCreate()
29491** or sqlite3ThreadJoin() call. This is, in fact, what happens in
29492** single threaded systems. Nothing in SQLite requires multiple threads.
29493** This interface exists so that applications that want to take advantage
29494** of multiple cores can do so, while also allowing applications to stay
29495** single-threaded if desired.
29496*/
29497/* #include "sqliteInt.h" */
29498#if SQLITE_OS_WIN0
29499/* # include "os_win.h" */
29500#endif
29501
29502#if SQLITE_MAX_WORKER_THREADS8>0
29503
29504/********************************* Unix Pthreads ****************************/
29505#if SQLITE_OS_UNIX1 && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE1>0
29506
29507#define SQLITE_THREADS_IMPLEMENTED1 1 /* Prevent the single-thread code below */
29508/* #include <pthread.h> */
29509
29510/* A running thread */
29511struct SQLiteThread {
29512 pthread_t tid; /* Thread ID */
29513 int done; /* Set to true when thread finishes */
29514 void *pOut; /* Result returned by the thread */
29515 void *(*xTask)(void*); /* The thread routine */
29516 void *pIn; /* Argument to the thread */
29517};
29518
29519/* Create a new thread */
29520SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
29521 SQLiteThread **ppThread, /* OUT: Write the thread object here */
29522 void *(*xTask)(void*), /* Routine to run in a separate thread */
29523 void *pIn /* Argument passed into xTask() */
29524){
29525 SQLiteThread *p;
29526 int rc;
29527
29528 assert( ppThread!=0 )((void) (0));
29529 assert( xTask!=0 )((void) (0));
29530 /* This routine is never used in single-threaded mode */
29531 assert( sqlite3GlobalConfig.bCoreMutex!=0 )((void) (0));
29532
29533 *ppThread = 0;
29534 p = sqlite3Malloc(sizeof(*p));
29535 if( p==0 ) return SQLITE_NOMEM_BKPT7;
29536 memset(p, 0, sizeof(*p));
29537 p->xTask = xTask;
29538 p->pIn = pIn;
29539 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
29540 ** function that returns SQLITE_ERROR when passed the argument 200, that
29541 ** forces worker threads to run sequentially and deterministically
29542 ** for testing purposes. */
29543 if( sqlite3FaultSim(200) ){
29544 rc = 1;
29545 }else{
29546 rc = pthread_create(&p->tid, 0, xTask, pIn);
29547 }
29548 if( rc ){
29549 p->done = 1;
29550 p->pOut = xTask(pIn);
29551 }
29552 *ppThread = p;
29553 return SQLITE_OK0;
29554}
29555
29556/* Get the results of the thread */
29557SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
29558 int rc;
29559
29560 assert( ppOut!=0 )((void) (0));
29561 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
29562 if( p->done ){
29563 *ppOut = p->pOut;
29564 rc = SQLITE_OK0;
29565 }else{
29566 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR1 : SQLITE_OK0;
29567 }
29568 sqlite3_free(p);
29569 return rc;
29570}
29571
29572#endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
29573/******************************** End Unix Pthreads *************************/
29574
29575
29576/********************************* Win32 Threads ****************************/
29577#if SQLITE_OS_WIN_THREADS
29578
29579#define SQLITE_THREADS_IMPLEMENTED1 1 /* Prevent the single-thread code below */
29580#include <process.h>
29581
29582/* A running thread */
29583struct SQLiteThread {
29584 void *tid; /* The thread handle */
29585 unsigned id; /* The thread identifier */
29586 void *(*xTask)(void*); /* The routine to run as a thread */
29587 void *pIn; /* Argument to xTask */
29588 void *pResult; /* Result of xTask */
29589};
29590
29591/* Thread procedure Win32 compatibility shim */
29592static unsigned __stdcall sqlite3ThreadProc(
29593 void *pArg /* IN: Pointer to the SQLiteThread structure */
29594){
29595 SQLiteThread *p = (SQLiteThread *)pArg;
29596
29597 assert( p!=0 )((void) (0));
29598#if 0
29599 /*
29600 ** This assert appears to trigger spuriously on certain
29601 ** versions of Windows, possibly due to _beginthreadex()
29602 ** and/or CreateThread() not fully setting their thread
29603 ** ID parameter before starting the thread.
29604 */
29605 assert( p->id==GetCurrentThreadId() )((void) (0));
29606#endif
29607 assert( p->xTask!=0 )((void) (0));
29608 p->pResult = p->xTask(p->pIn);
29609
29610 _endthreadex(0);
29611 return 0; /* NOT REACHED */
29612}
29613
29614/* Create a new thread */
29615SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
29616 SQLiteThread **ppThread, /* OUT: Write the thread object here */
29617 void *(*xTask)(void*), /* Routine to run in a separate thread */
29618 void *pIn /* Argument passed into xTask() */
29619){
29620 SQLiteThread *p;
29621
29622 assert( ppThread!=0 )((void) (0));
29623 assert( xTask!=0 )((void) (0));
29624 *ppThread = 0;
29625 p = sqlite3Malloc(sizeof(*p));
29626 if( p==0 ) return SQLITE_NOMEM_BKPT7;
29627 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
29628 ** function that returns SQLITE_ERROR when passed the argument 200, that
29629 ** forces worker threads to run sequentially and deterministically
29630 ** (via the sqlite3FaultSim() term of the conditional) for testing
29631 ** purposes. */
29632 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 || sqlite3FaultSim(200) ){
29633 memset(p, 0, sizeof(*p));
29634 }else{
29635 p->xTask = xTask;
29636 p->pIn = pIn;
29637 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
29638 if( p->tid==0 ){
29639 memset(p, 0, sizeof(*p));
29640 }
29641 }
29642 if( p->xTask==0 ){
29643 p->id = GetCurrentThreadId();
29644 p->pResult = xTask(pIn);
29645 }
29646 *ppThread = p;
29647 return SQLITE_OK0;
29648}
29649
29650SQLITE_PRIVATEstatic DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
29651
29652/* Get the results of the thread */
29653SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
29654 DWORD rc;
29655 BOOL bRc;
29656
29657 assert( ppOut!=0 )((void) (0));
29658 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
29659 if( p->xTask==0 ){
29660 /* assert( p->id==GetCurrentThreadId() ); */
29661 rc = WAIT_OBJECT_0;
29662 assert( p->tid==0 )((void) (0));
29663 }else{
29664 assert( p->id!=0 && p->id!=GetCurrentThreadId() )((void) (0));
29665 rc = sqlite3Win32Wait((HANDLE)p->tid);
29666 assert( rc!=WAIT_IO_COMPLETION )((void) (0));
29667 bRc = CloseHandle((HANDLE)p->tid);
29668 assert( bRc )((void) (0));
29669 }
29670 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
29671 sqlite3_free(p);
29672 return (rc==WAIT_OBJECT_0) ? SQLITE_OK0 : SQLITE_ERROR1;
29673}
29674
29675#endif /* SQLITE_OS_WIN_THREADS */
29676/******************************** End Win32 Threads *************************/
29677
29678
29679/********************************* Single-Threaded **************************/
29680#ifndef SQLITE_THREADS_IMPLEMENTED1
29681/*
29682** This implementation does not actually create a new thread. It does the
29683** work of the thread in the main thread, when either the thread is created
29684** or when it is joined
29685*/
29686
29687/* A running thread */
29688struct SQLiteThread {
29689 void *(*xTask)(void*); /* The routine to run as a thread */
29690 void *pIn; /* Argument to xTask */
29691 void *pResult; /* Result of xTask */
29692};
29693
29694/* Create a new thread */
29695SQLITE_PRIVATEstatic int sqlite3ThreadCreate(
29696 SQLiteThread **ppThread, /* OUT: Write the thread object here */
29697 void *(*xTask)(void*), /* Routine to run in a separate thread */
29698 void *pIn /* Argument passed into xTask() */
29699){
29700 SQLiteThread *p;
29701
29702 assert( ppThread!=0 )((void) (0));
29703 assert( xTask!=0 )((void) (0));
29704 *ppThread = 0;
29705 p = sqlite3Malloc(sizeof(*p));
29706 if( p==0 ) return SQLITE_NOMEM_BKPT7;
29707 if( (SQLITE_PTR_TO_INT(p)((int)(long int)(p))/17)&1 ){
29708 p->xTask = xTask;
29709 p->pIn = pIn;
29710 }else{
29711 p->xTask = 0;
29712 p->pResult = xTask(pIn);
29713 }
29714 *ppThread = p;
29715 return SQLITE_OK0;
29716}
29717
29718/* Get the results of the thread */
29719SQLITE_PRIVATEstatic int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
29720
29721 assert( ppOut!=0 )((void) (0));
29722 if( NEVER(p==0)(p==0) ) return SQLITE_NOMEM_BKPT7;
29723 if( p->xTask ){
29724 *ppOut = p->xTask(p->pIn);
29725 }else{
29726 *ppOut = p->pResult;
29727 }
29728 sqlite3_free(p);
29729
29730#if defined(SQLITE_TEST)
29731 {
29732 void *pTstAlloc = sqlite3Malloc(10);
29733 if (!pTstAlloc) return SQLITE_NOMEM_BKPT7;
29734 sqlite3_free(pTstAlloc);
29735 }
29736#endif
29737
29738 return SQLITE_OK0;
29739}
29740
29741#endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
29742/****************************** End Single-Threaded *************************/
29743#endif /* SQLITE_MAX_WORKER_THREADS>0 */
29744
29745/************** End of threads.c *********************************************/
29746/************** Begin file utf.c *********************************************/
29747/*
29748** 2004 April 13
29749**
29750** The author disclaims copyright to this source code. In place of
29751** a legal notice, here is a blessing:
29752**
29753** May you do good and not evil.
29754** May you find forgiveness for yourself and forgive others.
29755** May you share freely, never taking more than you give.
29756**
29757*************************************************************************
29758** This file contains routines used to translate between UTF-8,
29759** UTF-16, UTF-16BE, and UTF-16LE.
29760**
29761** Notes on UTF-8:
29762**
29763** Byte-0 Byte-1 Byte-2 Byte-3 Value
29764** 0xxxxxxx 00000000 00000000 0xxxxxxx
29765** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
29766** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
29767** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
29768**
29769**
29770** Notes on UTF-16: (with wwww+1==uuuuu)
29771**
29772** Word-0 Word-1 Value
29773** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
29774** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
29775**
29776**
29777** BOM or Byte Order Mark:
29778** 0xff 0xfe little-endian utf-16 follows
29779** 0xfe 0xff big-endian utf-16 follows
29780**
29781*/
29782/* #include "sqliteInt.h" */
29783/* #include <assert.h> */
29784/* #include "vdbeInt.h" */
29785
29786#if !defined(SQLITE_AMALGAMATION1) && SQLITE_BYTEORDER1234==0
29787/*
29788** The following constant value is used by the SQLITE_BIGENDIAN and
29789** SQLITE_LITTLEENDIAN macros.
29790*/
29791SQLITE_PRIVATEstatic const int sqlite3one = 1;
29792#endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
29793
29794/*
29795** This lookup table is used to help decode the first byte of
29796** a multi-byte UTF8 character.
29797*/
29798static const unsigned char sqlite3Utf8Trans1[] = {
29799 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29800 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
29801 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
29802 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
29803 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29804 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
29805 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
29806 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
29807};
29808
29809
29810#define WRITE_UTF8(zOut, c){ if( c<0x00080 ){ *zOut++ = (u8)(c&0xFF); } else if( c
<0x00800 ){ *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); *
zOut++ = 0x80 + (u8)(c & 0x3F); } else if( c<0x10000 )
{ *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); *zOut++ = 0x80
+ (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(c &
0x3F); }else{ *zOut++ = 0xF0 + (u8)((c>>18) & 0x07
); *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); *zOut++ =
0x80 + (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(
c & 0x3F); } }
{ \
29811 if( c<0x00080 ){ \
29812 *zOut++ = (u8)(c&0xFF); \
29813 } \
29814 else if( c<0x00800 ){ \
29815 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
29816 *zOut++ = 0x80 + (u8)(c & 0x3F); \
29817 } \
29818 else if( c<0x10000 ){ \
29819 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
29820 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
29821 *zOut++ = 0x80 + (u8)(c & 0x3F); \
29822 }else{ \
29823 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
29824 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
29825 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
29826 *zOut++ = 0x80 + (u8)(c & 0x3F); \
29827 } \
29828}
29829
29830#define WRITE_UTF16LE(zOut, c){ if( c<=0xFFFF ){ *zOut++ = (u8)(c&0x00FF); *zOut++ =
(u8)((c>>8)&0x00FF); }else{ *zOut++ = (u8)(((c>>
10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); *zOut
++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); *zOut
++ = (u8)(c&0x00FF); *zOut++ = (u8)(0x00DC + ((c>>8
)&0x03)); } }
{ \
29831 if( c<=0xFFFF ){ \
29832 *zOut++ = (u8)(c&0x00FF); \
29833 *zOut++ = (u8)((c>>8)&0x00FF); \
29834 }else{ \
29835 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
29836 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
29837 *zOut++ = (u8)(c&0x00FF); \
29838 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
29839 } \
29840}
29841
29842#define WRITE_UTF16BE(zOut, c){ if( c<=0xFFFF ){ *zOut++ = (u8)((c>>8)&0x00FF)
; *zOut++ = (u8)(c&0x00FF); }else{ *zOut++ = (u8)(0x00D8 +
(((c-0x10000)>>18)&0x03)); *zOut++ = (u8)(((c>>
10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); *zOut
++ = (u8)(0x00DC + ((c>>8)&0x03)); *zOut++ = (u8)(c
&0x00FF); } }
{ \
29843 if( c<=0xFFFF ){ \
29844 *zOut++ = (u8)((c>>8)&0x00FF); \
29845 *zOut++ = (u8)(c&0x00FF); \
29846 }else{ \
29847 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
29848 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
29849 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
29850 *zOut++ = (u8)(c&0x00FF); \
29851 } \
29852}
29853
29854#define READ_UTF16LE(zIn, TERM, c){ c = (*zIn++); c += ((*zIn++)<<8); if( c>=0xD800 &&
c<0xE000 && TERM ){ int c2 = (*zIn++); c2 += ((*zIn
++)<<8); c = (c2&0x03FF) + ((c&0x003F)<<10
) + (((c&0x03C0)+0x0040)<<10); } }
{ \
29855 c = (*zIn++); \
29856 c += ((*zIn++)<<8); \
29857 if( c>=0xD800 && c<0xE000 && TERM ){ \
29858 int c2 = (*zIn++); \
29859 c2 += ((*zIn++)<<8); \
29860 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
29861 } \
29862}
29863
29864#define READ_UTF16BE(zIn, TERM, c){ c = ((*zIn++)<<8); c += (*zIn++); if( c>=0xD800 &&
c<0xE000 && TERM ){ int c2 = ((*zIn++)<<8);
c2 += (*zIn++); c = (c2&0x03FF) + ((c&0x003F)<<
10) + (((c&0x03C0)+0x0040)<<10); } }
{ \
29865 c = ((*zIn++)<<8); \
29866 c += (*zIn++); \
29867 if( c>=0xD800 && c<0xE000 && TERM ){ \
29868 int c2 = ((*zIn++)<<8); \
29869 c2 += (*zIn++); \
29870 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
29871 } \
29872}
29873
29874/*
29875** Translate a single UTF-8 character. Return the unicode value.
29876**
29877** During translation, assume that the byte that zTerm points
29878** is a 0x00.
29879**
29880** Write a pointer to the next unread byte back into *pzNext.
29881**
29882** Notes On Invalid UTF-8:
29883**
29884** * This routine never allows a 7-bit character (0x00 through 0x7f) to
29885** be encoded as a multi-byte character. Any multi-byte character that
29886** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
29887**
29888** * This routine never allows a UTF16 surrogate value to be encoded.
29889** If a multi-byte character attempts to encode a value between
29890** 0xd800 and 0xe000 then it is rendered as 0xfffd.
29891**
29892** * Bytes in the range of 0x80 through 0xbf which occur as the first
29893** byte of a character are interpreted as single-byte characters
29894** and rendered as themselves even though they are technically
29895** invalid characters.
29896**
29897** * This routine accepts over-length UTF8 encodings
29898** for unicode values 0x80 and greater. It does not change over-length
29899** encodings to 0xfffd as some systems recommend.
29900*/
29901#define READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ c =
(c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (c&
0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD
; } }
\
29902 c = *(zIn++); \
29903 if( c>=0xc0 ){ \
29904 c = sqlite3Utf8Trans1[c-0xc0]; \
29905 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
29906 c = (c<<6) + (0x3f & *(zIn++)); \
29907 } \
29908 if( c<0x80 \
29909 || (c&0xFFFFF800)==0xD800 \
29910 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
29911 }
29912SQLITE_PRIVATEstatic u32 sqlite3Utf8Read(
29913 const unsigned char **pz /* Pointer to string from which to read char */
29914){
29915 unsigned int c;
29916
29917 /* Same as READ_UTF8() above but without the zTerm parameter.
29918 ** For this routine, we assume the UTF8 string is always zero-terminated.
29919 */
29920 c = *((*pz)++);
29921 if( c>=0xc0 ){
29922 c = sqlite3Utf8Trans1[c-0xc0];
29923 while( (*(*pz) & 0xc0)==0x80 ){
29924 c = (c<<6) + (0x3f & *((*pz)++));
29925 }
29926 if( c<0x80
29927 || (c&0xFFFFF800)==0xD800
29928 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
29929 }
29930 return c;
29931}
29932
29933
29934
29935
29936/*
29937** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
29938** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
29939*/
29940/* #define TRANSLATE_TRACE 1 */
29941
29942#ifndef SQLITE_OMIT_UTF16
29943/*
29944** This routine transforms the internal text encoding used by pMem to
29945** desiredEnc. It is an error if the string is already of the desired
29946** encoding, or if *pMem does not contain a string value.
29947*/
29948SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
29949 sqlite3_int64 len; /* Maximum length of output string in bytes */
29950 unsigned char *zOut; /* Output buffer */
29951 unsigned char *zIn; /* Input iterator */
29952 unsigned char *zTerm; /* End of input */
29953 unsigned char *z; /* Output iterator */
29954 unsigned int c;
29955
29956 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
29957 assert( pMem->flags&MEM_Str )((void) (0));
29958 assert( pMem->enc!=desiredEnc )((void) (0));
29959 assert( pMem->enc!=0 )((void) (0));
29960 assert( pMem->n>=0 )((void) (0));
29961
29962#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
29963 {
29964 char zBuf[100];
29965 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
29966 fprintf(stderrstderr, "INPUT: %s\n", zBuf);
29967 }
29968#endif
29969
29970 /* If the translation is between UTF-16 little and big endian, then
29971 ** all that is required is to swap the byte order. This case is handled
29972 ** differently from the others.
29973 */
29974 if( pMem->enc!=SQLITE_UTF81 && desiredEnc!=SQLITE_UTF81 ){
29975 u8 temp;
29976 int rc;
29977 rc = sqlite3VdbeMemMakeWriteable(pMem);
29978 if( rc!=SQLITE_OK0 ){
29979 assert( rc==SQLITE_NOMEM )((void) (0));
29980 return SQLITE_NOMEM_BKPT7;
29981 }
29982 zIn = (u8*)pMem->z;
29983 zTerm = &zIn[pMem->n&~1];
29984 while( zIn<zTerm ){
29985 temp = *zIn;
29986 *zIn = *(zIn+1);
29987 zIn++;
29988 *zIn++ = temp;
29989 }
29990 pMem->enc = desiredEnc;
29991 goto translate_out;
29992 }
29993
29994 /* Set len to the maximum number of bytes required in the output buffer. */
29995 if( desiredEnc==SQLITE_UTF81 ){
29996 /* When converting from UTF-16, the maximum growth results from
29997 ** translating a 2-byte character to a 4-byte UTF-8 character.
29998 ** A single byte is required for the output string
29999 ** nul-terminator.
30000 */
30001 pMem->n &= ~1;
30002 len = 2 * (sqlite3_int64)pMem->n + 1;
30003 }else{
30004 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
30005 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
30006 ** character. Two bytes are required in the output buffer for the
30007 ** nul-terminator.
30008 */
30009 len = 2 * (sqlite3_int64)pMem->n + 2;
30010 }
30011
30012 /* Set zIn to point at the start of the input buffer and zTerm to point 1
30013 ** byte past the end.
30014 **
30015 ** Variable zOut is set to point at the output buffer, space obtained
30016 ** from sqlite3_malloc().
30017 */
30018 zIn = (u8*)pMem->z;
30019 zTerm = &zIn[pMem->n];
30020 zOut = sqlite3DbMallocRaw(pMem->db, len);
30021 if( !zOut ){
30022 return SQLITE_NOMEM_BKPT7;
30023 }
30024 z = zOut;
30025
30026 if( pMem->enc==SQLITE_UTF81 ){
30027 if( desiredEnc==SQLITE_UTF16LE2 ){
30028 /* UTF-8 -> UTF-16 Little-endian */
30029 while( zIn<zTerm ){
30030 READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ c =
(c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (c&
0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD
; } }
;
30031 WRITE_UTF16LE(z, c){ if( c<=0xFFFF ){ *z++ = (u8)(c&0x00FF); *z++ = (u8)(
(c>>8)&0x00FF); }else{ *z++ = (u8)(((c>>10)&
0x003F) + (((c-0x10000)>>10)&0x00C0)); *z++ = (u8)(
0x00D8 + (((c-0x10000)>>18)&0x03)); *z++ = (u8)(c&
0x00FF); *z++ = (u8)(0x00DC + ((c>>8)&0x03)); } }
;
30032 }
30033 }else{
30034 assert( desiredEnc==SQLITE_UTF16BE )((void) (0));
30035 /* UTF-8 -> UTF-16 Big-endian */
30036 while( zIn<zTerm ){
30037 READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ c =
(c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (c&
0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD
; } }
;
30038 WRITE_UTF16BE(z, c){ if( c<=0xFFFF ){ *z++ = (u8)((c>>8)&0x00FF); *
z++ = (u8)(c&0x00FF); }else{ *z++ = (u8)(0x00D8 + (((c-0x10000
)>>18)&0x03)); *z++ = (u8)(((c>>10)&0x003F
) + (((c-0x10000)>>10)&0x00C0)); *z++ = (u8)(0x00DC
+ ((c>>8)&0x03)); *z++ = (u8)(c&0x00FF); } }
;
30039 }
30040 }
30041 pMem->n = (int)(z - zOut);
30042 *z++ = 0;
30043 }else{
30044 assert( desiredEnc==SQLITE_UTF8 )((void) (0));
30045 if( pMem->enc==SQLITE_UTF16LE2 ){
30046 /* UTF-16 Little-endian -> UTF-8 */
30047 while( zIn<zTerm ){
30048 READ_UTF16LE(zIn, zIn<zTerm, c){ c = (*zIn++); c += ((*zIn++)<<8); if( c>=0xD800 &&
c<0xE000 && zIn<zTerm ){ int c2 = (*zIn++); c2
+= ((*zIn++)<<8); c = (c2&0x03FF) + ((c&0x003F
)<<10) + (((c&0x03C0)+0x0040)<<10); } }
;
30049 WRITE_UTF8(z, c){ if( c<0x00080 ){ *z++ = (u8)(c&0xFF); } else if( c<
0x00800 ){ *z++ = 0xC0 + (u8)((c>>6)&0x1F); *z++ = 0x80
+ (u8)(c & 0x3F); } else if( c<0x10000 ){ *z++ = 0xE0
+ (u8)((c>>12)&0x0F); *z++ = 0x80 + (u8)((c>>
6) & 0x3F); *z++ = 0x80 + (u8)(c & 0x3F); }else{ *z++
= 0xF0 + (u8)((c>>18) & 0x07); *z++ = 0x80 + (u8)(
(c>>12) & 0x3F); *z++ = 0x80 + (u8)((c>>6) &
0x3F); *z++ = 0x80 + (u8)(c & 0x3F); } }
;
30050 }
30051 }else{
30052 /* UTF-16 Big-endian -> UTF-8 */
30053 while( zIn<zTerm ){
30054 READ_UTF16BE(zIn, zIn<zTerm, c){ c = ((*zIn++)<<8); c += (*zIn++); if( c>=0xD800 &&
c<0xE000 && zIn<zTerm ){ int c2 = ((*zIn++)<<
8); c2 += (*zIn++); c = (c2&0x03FF) + ((c&0x003F)<<
10) + (((c&0x03C0)+0x0040)<<10); } }
;
30055 WRITE_UTF8(z, c){ if( c<0x00080 ){ *z++ = (u8)(c&0xFF); } else if( c<
0x00800 ){ *z++ = 0xC0 + (u8)((c>>6)&0x1F); *z++ = 0x80
+ (u8)(c & 0x3F); } else if( c<0x10000 ){ *z++ = 0xE0
+ (u8)((c>>12)&0x0F); *z++ = 0x80 + (u8)((c>>
6) & 0x3F); *z++ = 0x80 + (u8)(c & 0x3F); }else{ *z++
= 0xF0 + (u8)((c>>18) & 0x07); *z++ = 0x80 + (u8)(
(c>>12) & 0x3F); *z++ = 0x80 + (u8)((c>>6) &
0x3F); *z++ = 0x80 + (u8)(c & 0x3F); } }
;
30056 }
30057 }
30058 pMem->n = (int)(z - zOut);
30059 }
30060 *z = 0;
30061 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len )((void) (0));
30062
30063 c = pMem->flags;
30064 sqlite3VdbeMemRelease(pMem);
30065 pMem->flags = MEM_Str0x0002|MEM_Term0x0200|(c&(MEM_AffMask0x003f|MEM_Subtype0x8000));
30066 pMem->enc = desiredEnc;
30067 pMem->z = (char*)zOut;
30068 pMem->zMalloc = pMem->z;
30069 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
30070
30071translate_out:
30072#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
30073 {
30074 char zBuf[100];
30075 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
30076 fprintf(stderrstderr, "OUTPUT: %s\n", zBuf);
30077 }
30078#endif
30079 return SQLITE_OK0;
30080}
30081#endif /* SQLITE_OMIT_UTF16 */
30082
30083#ifndef SQLITE_OMIT_UTF16
30084/*
30085** This routine checks for a byte-order mark at the beginning of the
30086** UTF-16 string stored in *pMem. If one is present, it is removed and
30087** the encoding of the Mem adjusted. This routine does not do any
30088** byte-swapping, it just sets Mem.enc appropriately.
30089**
30090** The allocation (static, dynamic etc.) and encoding of the Mem may be
30091** changed by this function.
30092*/
30093SQLITE_PRIVATEstatic int sqlite3VdbeMemHandleBom(Mem *pMem){
30094 int rc = SQLITE_OK0;
30095 u8 bom = 0;
30096
30097 assert( pMem->n>=0 )((void) (0));
30098 if( pMem->n>1 ){
30099 u8 b1 = *(u8 *)pMem->z;
30100 u8 b2 = *(((u8 *)pMem->z) + 1);
30101 if( b1==0xFE && b2==0xFF ){
30102 bom = SQLITE_UTF16BE3;
30103 }
30104 if( b1==0xFF && b2==0xFE ){
30105 bom = SQLITE_UTF16LE2;
30106 }
30107 }
30108
30109 if( bom ){
30110 rc = sqlite3VdbeMemMakeWriteable(pMem);
30111 if( rc==SQLITE_OK0 ){
30112 pMem->n -= 2;
30113 memmove(pMem->z, &pMem->z[2], pMem->n);
30114 pMem->z[pMem->n] = '\0';
30115 pMem->z[pMem->n+1] = '\0';
30116 pMem->flags |= MEM_Term0x0200;
30117 pMem->enc = bom;
30118 }
30119 }
30120 return rc;
30121}
30122#endif /* SQLITE_OMIT_UTF16 */
30123
30124/*
30125** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
30126** return the number of unicode characters in pZ up to (but not including)
30127** the first 0x00 byte. If nByte is not less than zero, return the
30128** number of unicode characters in the first nByte of pZ (or up to
30129** the first 0x00, whichever comes first).
30130*/
30131SQLITE_PRIVATEstatic int sqlite3Utf8CharLen(const char *zIn, int nByte){
30132 int r = 0;
30133 const u8 *z = (const u8*)zIn;
30134 const u8 *zTerm;
30135 if( nByte>=0 ){
30136 zTerm = &z[nByte];
30137 }else{
30138 zTerm = (const u8*)(-1);
30139 }
30140 assert( z<=zTerm )((void) (0));
30141 while( *z!=0 && z<zTerm ){
30142 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
30143 r++;
30144 }
30145 return r;
30146}
30147
30148/* This test function is not currently used by the automated test-suite.
30149** Hence it is only available in debug builds.
30150*/
30151#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
30152/*
30153** Translate UTF-8 to UTF-8.
30154**
30155** This has the effect of making sure that the string is well-formed
30156** UTF-8. Miscoded characters are removed.
30157**
30158** The translation is done in-place and aborted if the output
30159** overruns the input.
30160*/
30161SQLITE_PRIVATEstatic int sqlite3Utf8To8(unsigned char *zIn){
30162 unsigned char *zOut = zIn;
30163 unsigned char *zStart = zIn;
30164 u32 c;
30165
30166 while( zIn[0] && zOut<=zIn ){
30167 c = sqlite3Utf8Read((const u8**)&zIn);
30168 if( c!=0xfffd ){
30169 WRITE_UTF8(zOut, c){ if( c<0x00080 ){ *zOut++ = (u8)(c&0xFF); } else if( c
<0x00800 ){ *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); *
zOut++ = 0x80 + (u8)(c & 0x3F); } else if( c<0x10000 )
{ *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); *zOut++ = 0x80
+ (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(c &
0x3F); }else{ *zOut++ = 0xF0 + (u8)((c>>18) & 0x07
); *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); *zOut++ =
0x80 + (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(
c & 0x3F); } }
;
30170 }
30171 }
30172 *zOut = 0;
30173 return (int)(zOut - zStart);
30174}
30175#endif
30176
30177#ifndef SQLITE_OMIT_UTF16
30178/*
30179** Convert a UTF-16 string in the native encoding into a UTF-8 string.
30180** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
30181** be freed by the calling function.
30182**
30183** NULL is returned if there is an allocation error.
30184*/
30185SQLITE_PRIVATEstatic char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
30186 Mem m;
30187 memset(&m, 0, sizeof(m));
30188 m.db = db;
30189 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC((sqlite3_destructor_type)0));
30190 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF81);
30191 if( db->mallocFailed ){
30192 sqlite3VdbeMemRelease(&m);
30193 m.z = 0;
30194 }
30195 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed )((void) (0));
30196 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed )((void) (0));
30197 assert( m.z || db->mallocFailed )((void) (0));
30198 return m.z;
30199}
30200
30201/*
30202** zIn is a UTF-16 encoded unicode string at least nChar characters long.
30203** Return the number of bytes in the first nChar unicode characters
30204** in pZ. nChar must be non-negative.
30205*/
30206SQLITE_PRIVATEstatic int sqlite3Utf16ByteLen(const void *zIn, int nChar){
30207 int c;
30208 unsigned char const *z = zIn;
30209 int n = 0;
30210
30211 if( SQLITE_UTF16NATIVE2==SQLITE_UTF16BE3 ){
30212 while( n<nChar ){
30213 READ_UTF16BE(z, 1, c){ c = ((*z++)<<8); c += (*z++); if( c>=0xD800 &&
c<0xE000 && 1 ){ int c2 = ((*z++)<<8); c2 +=
(*z++); c = (c2&0x03FF) + ((c&0x003F)<<10) + (
((c&0x03C0)+0x0040)<<10); } }
;
30214 n++;
30215 }
30216 }else{
30217 while( n<nChar ){
30218 READ_UTF16LE(z, 1, c){ c = (*z++); c += ((*z++)<<8); if( c>=0xD800 &&
c<0xE000 && 1 ){ int c2 = (*z++); c2 += ((*z++)<<
8); c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&
0x03C0)+0x0040)<<10); } }
;
30219 n++;
30220 }
30221 }
30222 return (int)(z-(unsigned char const *)zIn);
30223}
30224
30225#if defined(SQLITE_TEST)
30226/*
30227** This routine is called from the TCL test function "translate_selftest".
30228** It checks that the primitives for serializing and deserializing
30229** characters in each encoding are inverses of each other.
30230*/
30231SQLITE_PRIVATEstatic void sqlite3UtfSelfTest(void){
30232 unsigned int i, t;
30233 unsigned char zBuf[20];
30234 unsigned char *z;
30235 int n;
30236 unsigned int c;
30237
30238 for(i=0; i<0x00110000; i++){
30239 z = zBuf;
30240 WRITE_UTF8(z, i){ if( i<0x00080 ){ *z++ = (u8)(i&0xFF); } else if( i<
0x00800 ){ *z++ = 0xC0 + (u8)((i>>6)&0x1F); *z++ = 0x80
+ (u8)(i & 0x3F); } else if( i<0x10000 ){ *z++ = 0xE0
+ (u8)((i>>12)&0x0F); *z++ = 0x80 + (u8)((i>>
6) & 0x3F); *z++ = 0x80 + (u8)(i & 0x3F); }else{ *z++
= 0xF0 + (u8)((i>>18) & 0x07); *z++ = 0x80 + (u8)(
(i>>12) & 0x3F); *z++ = 0x80 + (u8)((i>>6) &
0x3F); *z++ = 0x80 + (u8)(i & 0x3F); } }
;
30241 n = (int)(z-zBuf);
30242 assert( n>0 && n<=4 )((void) (0));
30243 z[0] = 0;
30244 z = zBuf;
30245 c = sqlite3Utf8Read((const u8**)&z);
30246 t = i;
30247 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
30248 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
30249 assert( c==t )((void) (0));
30250 assert( (z-zBuf)==n )((void) (0));
30251 }
30252 for(i=0; i<0x00110000; i++){
30253 if( i>=0xD800 && i<0xE000 ) continue;
30254 z = zBuf;
30255 WRITE_UTF16LE(z, i){ if( i<=0xFFFF ){ *z++ = (u8)(i&0x00FF); *z++ = (u8)(
(i>>8)&0x00FF); }else{ *z++ = (u8)(((i>>10)&
0x003F) + (((i-0x10000)>>10)&0x00C0)); *z++ = (u8)(
0x00D8 + (((i-0x10000)>>18)&0x03)); *z++ = (u8)(i&
0x00FF); *z++ = (u8)(0x00DC + ((i>>8)&0x03)); } }
;
30256 n = (int)(z-zBuf);
30257 assert( n>0 && n<=4 )((void) (0));
30258 z[0] = 0;
30259 z = zBuf;
30260 READ_UTF16LE(z, 1, c){ c = (*z++); c += ((*z++)<<8); if( c>=0xD800 &&
c<0xE000 && 1 ){ int c2 = (*z++); c2 += ((*z++)<<
8); c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&
0x03C0)+0x0040)<<10); } }
;
30261 assert( c==i )((void) (0));
30262 assert( (z-zBuf)==n )((void) (0));
30263 }
30264 for(i=0; i<0x00110000; i++){
30265 if( i>=0xD800 && i<0xE000 ) continue;
30266 z = zBuf;
30267 WRITE_UTF16BE(z, i){ if( i<=0xFFFF ){ *z++ = (u8)((i>>8)&0x00FF); *
z++ = (u8)(i&0x00FF); }else{ *z++ = (u8)(0x00D8 + (((i-0x10000
)>>18)&0x03)); *z++ = (u8)(((i>>10)&0x003F
) + (((i-0x10000)>>10)&0x00C0)); *z++ = (u8)(0x00DC
+ ((i>>8)&0x03)); *z++ = (u8)(i&0x00FF); } }
;
30268 n = (int)(z-zBuf);
30269 assert( n>0 && n<=4 )((void) (0));
30270 z[0] = 0;
30271 z = zBuf;
30272 READ_UTF16BE(z, 1, c){ c = ((*z++)<<8); c += (*z++); if( c>=0xD800 &&
c<0xE000 && 1 ){ int c2 = ((*z++)<<8); c2 +=
(*z++); c = (c2&0x03FF) + ((c&0x003F)<<10) + (
((c&0x03C0)+0x0040)<<10); } }
;
30273 assert( c==i )((void) (0));
30274 assert( (z-zBuf)==n )((void) (0));
30275 }
30276}
30277#endif /* SQLITE_TEST */
30278#endif /* SQLITE_OMIT_UTF16 */
30279
30280/************** End of utf.c *************************************************/
30281/************** Begin file util.c ********************************************/
30282/*
30283** 2001 September 15
30284**
30285** The author disclaims copyright to this source code. In place of
30286** a legal notice, here is a blessing:
30287**
30288** May you do good and not evil.
30289** May you find forgiveness for yourself and forgive others.
30290** May you share freely, never taking more than you give.
30291**
30292*************************************************************************
30293** Utility functions used throughout sqlite.
30294**
30295** This file contains functions for allocating memory, comparing
30296** strings, and stuff like that.
30297**
30298*/
30299/* #include "sqliteInt.h" */
30300/* #include <stdarg.h> */
30301#include <math.h>
30302
30303/*
30304** Routine needed to support the testcase() macro.
30305*/
30306#ifdef SQLITE_COVERAGE_TEST
30307SQLITE_PRIVATEstatic void sqlite3Coverage(int x){
30308 static unsigned dummy = 0;
30309 dummy += (unsigned)x;
30310}
30311#endif
30312
30313/*
30314** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
30315** or to bypass normal error detection during testing in order to let
30316** execute proceed futher downstream.
30317**
30318** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
30319** sqlite3FaultSim() function only returns non-zero during testing.
30320**
30321** During testing, if the test harness has set a fault-sim callback using
30322** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
30323** each call to sqlite3FaultSim() is relayed to that application-supplied
30324** callback and the integer return value form the application-supplied
30325** callback is returned by sqlite3FaultSim().
30326**
30327** The integer argument to sqlite3FaultSim() is a code to identify which
30328** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
30329** should have a unique code. To prevent legacy testing applications from
30330** breaking, the codes should not be changed or reused.
30331*/
30332#ifndef SQLITE_UNTESTABLE
30333SQLITE_PRIVATEstatic int sqlite3FaultSim(int iTest){
30334 int (*xCallback)(int) = sqlite3GlobalConfigsqlite3Config.xTestCallback;
30335 return xCallback ? xCallback(iTest) : SQLITE_OK0;
30336}
30337#endif
30338
30339#ifndef SQLITE_OMIT_FLOATING_POINT
30340/*
30341** Return true if the floating point value is Not a Number (NaN).
30342*/
30343SQLITE_PRIVATEstatic int sqlite3IsNaN(double x){
30344 u64 y;
30345 memcpy(&y,&x,sizeof(y));
30346 return IsNaN(y)(((y)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((y)&((((u64)1)<<52)-1))!=0)
;
30347}
30348#endif /* SQLITE_OMIT_FLOATING_POINT */
30349
30350/*
30351** Compute a string length that is limited to what can be stored in
30352** lower 30 bits of a 32-bit signed integer.
30353**
30354** The value returned will never be negative. Nor will it ever be greater
30355** than the actual length of the string. For very long strings (greater
30356** than 1GiB) the value returned might be less than the true string length.
30357*/
30358SQLITE_PRIVATEstatic int sqlite3Strlen30(const char *z){
30359 if( z==0 ) return 0;
30360 return 0x3fffffff & (int)strlen(z);
30361}
30362
30363/*
30364** Return the declared type of a column. Or return zDflt if the column
30365** has no declared type.
30366**
30367** The column type is an extra string stored after the zero-terminator on
30368** the column name if and only if the COLFLAG_HASTYPE flag is set.
30369*/
30370SQLITE_PRIVATEstatic char *sqlite3ColumnType(Column *pCol, char *zDflt){
30371 if( (pCol->colFlags & COLFLAG_HASTYPE0x0004)==0 ) return zDflt;
30372 return pCol->zName + strlen(pCol->zName) + 1;
30373}
30374
30375/*
30376** Helper function for sqlite3Error() - called rarely. Broken out into
30377** a separate routine to avoid unnecessary register saves on entry to
30378** sqlite3Error().
30379*/
30380static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ErrorFinish(sqlite3 *db, int err_code){
30381 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
30382 sqlite3SystemError(db, err_code);
30383}
30384
30385/*
30386** Set the current error code to err_code and clear any prior error message.
30387** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
30388** that would be appropriate.
30389*/
30390SQLITE_PRIVATEstatic void sqlite3Error(sqlite3 *db, int err_code){
30391 assert( db!=0 )((void) (0));
30392 db->errCode = err_code;
30393 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
30394}
30395
30396/*
30397** Load the sqlite3.iSysErrno field if that is an appropriate thing
30398** to do based on the SQLite error code in rc.
30399*/
30400SQLITE_PRIVATEstatic void sqlite3SystemError(sqlite3 *db, int rc){
30401 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) return;
30402 rc &= 0xff;
30403 if( rc==SQLITE_CANTOPEN14 || rc==SQLITE_IOERR10 ){
30404 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
30405 }
30406}
30407
30408/*
30409** Set the most recent error code and error string for the sqlite
30410** handle "db". The error code is set to "err_code".
30411**
30412** If it is not NULL, string zFormat specifies the format of the
30413** error string in the style of the printf functions: The following
30414** format characters are allowed:
30415**
30416** %s Insert a string
30417** %z A string that should be freed after use
30418** %d Insert an integer
30419** %T Insert a token
30420** %S Insert the first element of a SrcList
30421**
30422** zFormat and any string tokens that follow it are assumed to be
30423** encoded in UTF-8.
30424**
30425** To clear the most recent error for sqlite handle "db", sqlite3Error
30426** should be called with err_code set to SQLITE_OK and zFormat set
30427** to NULL.
30428*/
30429SQLITE_PRIVATEstatic void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
30430 assert( db!=0 )((void) (0));
30431 db->errCode = err_code;
30432 sqlite3SystemError(db, err_code);
30433 if( zFormat==0 ){
30434 sqlite3Error(db, err_code);
30435 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
30436 char *z;
30437 va_list ap;
30438 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
30439 z = sqlite3VMPrintf(db, zFormat, ap);
30440 va_end(ap)__builtin_va_end(ap);
30441 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF81, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3MallocSize));
30442 }
30443}
30444
30445/*
30446** Add an error message to pParse->zErrMsg and increment pParse->nErr.
30447** The following formatting characters are allowed:
30448**
30449** %s Insert a string
30450** %z A string that should be freed after use
30451** %d Insert an integer
30452** %T Insert a token
30453** %S Insert the first element of a SrcList
30454**
30455** This function should be used to report any error that occurs while
30456** compiling an SQL statement (i.e. within sqlite3_prepare()). The
30457** last thing the sqlite3_prepare() function does is copy the error
30458** stored by this function into the database handle using sqlite3Error().
30459** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
30460** during statement execution (sqlite3_step() etc.).
30461*/
30462SQLITE_PRIVATEstatic void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
30463 char *zMsg;
30464 va_list ap;
30465 sqlite3 *db = pParse->db;
30466 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
30467 zMsg = sqlite3VMPrintf(db, zFormat, ap);
30468 va_end(ap)__builtin_va_end(ap);
30469 if( db->suppressErr ){
30470 sqlite3DbFree(db, zMsg);
30471 }else{
30472 pParse->nErr++;
30473 sqlite3DbFree(db, pParse->zErrMsg);
30474 pParse->zErrMsg = zMsg;
30475 pParse->rc = SQLITE_ERROR1;
30476 }
30477}
30478
30479/*
30480** If database connection db is currently parsing SQL, then transfer
30481** error code errCode to that parser if the parser has not already
30482** encountered some other kind of error.
30483*/
30484SQLITE_PRIVATEstatic int sqlite3ErrorToParser(sqlite3 *db, int errCode){
30485 Parse *pParse;
30486 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
30487 pParse->rc = errCode;
30488 pParse->nErr++;
30489 return errCode;
30490}
30491
30492/*
30493** Convert an SQL-style quoted string into a normal string by removing
30494** the quote characters. The conversion is done in-place. If the
30495** input does not begin with a quote character, then this routine
30496** is a no-op.
30497**
30498** The input string must be zero-terminated. A new zero-terminator
30499** is added to the dequoted string.
30500**
30501** The return value is -1 if no dequoting occurs or the length of the
30502** dequoted string, exclusive of the zero terminator, if dequoting does
30503** occur.
30504**
30505** 2002-02-14: This routine is extended to remove MS-Access style
30506** brackets from around identifiers. For example: "[a-b-c]" becomes
30507** "a-b-c".
30508*/
30509SQLITE_PRIVATEstatic void sqlite3Dequote(char *z){
30510 char quote;
30511 int i, j;
30512 if( z==0 ) return;
30513 quote = z[0];
30514 if( !sqlite3Isquote(quote)(sqlite3CtypeMap[(unsigned char)(quote)]&0x80) ) return;
30515 if( quote=='[' ) quote = ']';
30516 for(i=1, j=0;; i++){
30517 assert( z[i] )((void) (0));
30518 if( z[i]==quote ){
30519 if( z[i+1]==quote ){
30520 z[j++] = quote;
30521 i++;
30522 }else{
30523 break;
30524 }
30525 }else{
30526 z[j++] = z[i];
30527 }
30528 }
30529 z[j] = 0;
30530}
30531SQLITE_PRIVATEstatic void sqlite3DequoteExpr(Expr *p){
30532 assert( sqlite3Isquote(p->u.zToken[0]) )((void) (0));
30533 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted0x4000000|EP_DblQuoted0x000040 : EP_Quoted0x4000000;
30534 sqlite3Dequote(p->u.zToken);
30535}
30536
30537/*
30538** Generate a Token object from a string
30539*/
30540SQLITE_PRIVATEstatic void sqlite3TokenInit(Token *p, char *z){
30541 p->z = z;
30542 p->n = sqlite3Strlen30(z);
30543}
30544
30545/* Convenient short-hand */
30546#define UpperToLowersqlite3UpperToLower sqlite3UpperToLower
30547
30548/*
30549** Some systems have stricmp(). Others have strcasecmp(). Because
30550** there is no consistency, we will define our own.
30551**
30552** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
30553** sqlite3_strnicmp() APIs allow applications and extensions to compare
30554** the contents of two buffers containing UTF-8 strings in a
30555** case-independent fashion, using the same definition of "case
30556** independence" that SQLite uses internally when comparing identifiers.
30557*/
30558SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
30559 if( zLeft==0 ){
30560 return zRight ? -1 : 0;
30561 }else if( zRight==0 ){
30562 return 1;
30563 }
30564 return sqlite3StrICmp(zLeft, zRight);
30565}
30566SQLITE_PRIVATEstatic int sqlite3StrICmp(const char *zLeft, const char *zRight){
30567 unsigned char *a, *b;
30568 int c, x;
30569 a = (unsigned char *)zLeft;
30570 b = (unsigned char *)zRight;
30571 for(;;){
30572 c = *a;
30573 x = *b;
30574 if( c==x ){
30575 if( c==0 ) break;
30576 }else{
30577 c = (int)UpperToLowersqlite3UpperToLower[c] - (int)UpperToLowersqlite3UpperToLower[x];
30578 if( c ) break;
30579 }
30580 a++;
30581 b++;
30582 }
30583 return c;
30584}
30585SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
30586 register unsigned char *a, *b;
30587 if( zLeft==0 ){
30588 return zRight ? -1 : 0;
30589 }else if( zRight==0 ){
30590 return 1;
30591 }
30592 a = (unsigned char *)zLeft;
30593 b = (unsigned char *)zRight;
30594 while( N-- > 0 && *a!=0 && UpperToLowersqlite3UpperToLower[*a]==UpperToLowersqlite3UpperToLower[*b]){ a++; b++; }
30595 return N<0 ? 0 : UpperToLowersqlite3UpperToLower[*a] - UpperToLowersqlite3UpperToLower[*b];
30596}
30597
30598/*
30599** Compute 10 to the E-th power. Examples: E==1 results in 10.
30600** E==2 results in 100. E==50 results in 1.0e50.
30601**
30602** This routine only works for values of E between 1 and 341.
30603*/
30604static LONGDOUBLE_TYPElong double sqlite3Pow10(int E){
30605#if defined(_MSC_VER)
30606 static const LONGDOUBLE_TYPElong double x[] = {
30607 1.0e+001L,
30608 1.0e+002L,
30609 1.0e+004L,
30610 1.0e+008L,
30611 1.0e+016L,
30612 1.0e+032L,
30613 1.0e+064L,
30614 1.0e+128L,
30615 1.0e+256L
30616 };
30617 LONGDOUBLE_TYPElong double r = 1.0;
30618 int i;
30619 assert( E>=0 && E<=307 )((void) (0));
30620 for(i=0; E!=0; i++, E >>=1){
30621 if( E & 1 ) r *= x[i];
30622 }
30623 return r;
30624#else
30625 LONGDOUBLE_TYPElong double x = 10.0;
30626 LONGDOUBLE_TYPElong double r = 1.0;
30627 while(1){
30628 if( E & 1 ) r *= x;
30629 E >>= 1;
30630 if( E==0 ) break;
30631 x *= x;
30632 }
30633 return r;
30634#endif
30635}
30636
30637/*
30638** The string z[] is an text representation of a real number.
30639** Convert this string to a double and write it into *pResult.
30640**
30641** The string z[] is length bytes in length (bytes, not characters) and
30642** uses the encoding enc. The string is not necessarily zero-terminated.
30643**
30644** Return TRUE if the result is a valid real number (or integer) and FALSE
30645** if the string is empty or contains extraneous text. More specifically
30646** return
30647** 1 => The input string is a pure integer
30648** 2 or more => The input has a decimal point or eNNN clause
30649** 0 or less => The input string is not a valid number
30650** -1 => Not a valid number, but has a valid prefix which
30651** includes a decimal point and/or an eNNN clause
30652**
30653** Valid numbers are in one of these formats:
30654**
30655** [+-]digits[E[+-]digits]
30656** [+-]digits.[digits][E[+-]digits]
30657** [+-].digits[E[+-]digits]
30658**
30659** Leading and trailing whitespace is ignored for the purpose of determining
30660** validity.
30661**
30662** If some prefix of the input string is a valid number, this routine
30663** returns FALSE but it still converts the prefix and writes the result
30664** into *pResult.
30665*/
30666SQLITE_PRIVATEstatic int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
30667#ifndef SQLITE_OMIT_FLOATING_POINT
30668 int incr;
30669 const char *zEnd = z + length;
30670 /* sign * significand * (10 ^ (esign * exponent)) */
30671 int sign = 1; /* sign of significand */
30672 i64 s = 0; /* significand */
30673 int d = 0; /* adjust exponent for shifting decimal point */
30674 int esign = 1; /* sign of exponent */
30675 int e = 0; /* exponent */
30676 int eValid = 1; /* True exponent is either not used or is well-formed */
30677 double result;
30678 int nDigit = 0; /* Number of digits processed */
30679 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
30680
30681 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
30682 *pResult = 0.0; /* Default return value, in case of an error */
30683
30684 if( enc==SQLITE_UTF81 ){
30685 incr = 1;
30686 }else{
30687 int i;
30688 incr = 2;
30689 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
30690 testcase( enc==SQLITE_UTF16LE );
30691 testcase( enc==SQLITE_UTF16BE );
30692 for(i=3-enc; i<length && z[i]==0; i+=2){}
30693 if( i<length ) eType = -100;
30694 zEnd = &z[i^1];
30695 z += (enc&1);
30696 }
30697
30698 /* skip leading spaces */
30699 while( z<zEnd && sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z+=incr;
30700 if( z>=zEnd ) return 0;
30701
30702 /* get sign of significand */
30703 if( *z=='-' ){
30704 sign = -1;
30705 z+=incr;
30706 }else if( *z=='+' ){
30707 z+=incr;
30708 }
30709
30710 /* copy max significant digits to significand */
30711 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
30712 s = s*10 + (*z - '0');
30713 z+=incr; nDigit++;
30714 if( s>=((LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))-9)/10) ){
30715 /* skip non-significant significand digits
30716 ** (increase exponent by d to shift decimal left) */
30717 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){ z+=incr; d++; }
30718 }
30719 }
30720 if( z>=zEnd ) goto do_atof_calc;
30721
30722 /* if decimal point is present */
30723 if( *z=='.' ){
30724 z+=incr;
30725 eType++;
30726 /* copy digits from after decimal to significand
30727 ** (decrease exponent by d to shift decimal right) */
30728 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
30729 if( s<((LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))-9)/10) ){
30730 s = s*10 + (*z - '0');
30731 d--;
30732 nDigit++;
30733 }
30734 z+=incr;
30735 }
30736 }
30737 if( z>=zEnd ) goto do_atof_calc;
30738
30739 /* if exponent is present */
30740 if( *z=='e' || *z=='E' ){
30741 z+=incr;
30742 eValid = 0;
30743 eType++;
30744
30745 /* This branch is needed to avoid a (harmless) buffer overread. The
30746 ** special comment alerts the mutation tester that the correct answer
30747 ** is obtained even if the branch is omitted */
30748 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
30749
30750 /* get sign of exponent */
30751 if( *z=='-' ){
30752 esign = -1;
30753 z+=incr;
30754 }else if( *z=='+' ){
30755 z+=incr;
30756 }
30757 /* copy digits to exponent */
30758 while( z<zEnd && sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
30759 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
30760 z+=incr;
30761 eValid = 1;
30762 }
30763 }
30764
30765 /* skip trailing spaces */
30766 while( z<zEnd && sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z+=incr;
30767
30768do_atof_calc:
30769 /* adjust exponent by d, and update sign */
30770 e = (e*esign) + d;
30771 if( e<0 ) {
30772 esign = -1;
30773 e *= -1;
30774 } else {
30775 esign = 1;
30776 }
30777
30778 if( s==0 ) {
30779 /* In the IEEE 754 standard, zero is signed. */
30780 result = sign<0 ? -(double)0 : (double)0;
30781 } else {
30782 /* Attempt to reduce exponent.
30783 **
30784 ** Branches that are not required for the correct answer but which only
30785 ** help to obtain the correct answer faster are marked with special
30786 ** comments, as a hint to the mutation tester.
30787 */
30788 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
30789 if( esign>0 ){
30790 if( s>=(LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/10) ) break; /*OPTIMIZATION-IF-FALSE*/
30791 s *= 10;
30792 }else{
30793 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
30794 s /= 10;
30795 }
30796 e--;
30797 }
30798
30799 /* adjust the sign of significand */
30800 s = sign<0 ? -s : s;
30801
30802 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
30803 result = (double)s;
30804 }else{
30805 /* attempt to handle extremely small/large numbers better */
30806 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
30807 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
30808 LONGDOUBLE_TYPElong double scale = sqlite3Pow10(e-308);
30809 if( esign<0 ){
30810 result = s / scale;
30811 result /= 1.0e+308;
30812 }else{
30813 result = s * scale;
30814 result *= 1.0e+308;
30815 }
30816 }else{ assert( e>=342 )((void) (0));
30817 if( esign<0 ){
30818 result = 0.0*s;
30819 }else{
30820#ifdef INFINITY(__builtin_inff ())
30821 result = INFINITY(__builtin_inff ())*s;
30822#else
30823 result = 1e308*1e308*s; /* Infinity */
30824#endif
30825 }
30826 }
30827 }else{
30828 LONGDOUBLE_TYPElong double scale = sqlite3Pow10(e);
30829 if( esign<0 ){
30830 result = s / scale;
30831 }else{
30832 result = s * scale;
30833 }
30834 }
30835 }
30836 }
30837
30838 /* store the result */
30839 *pResult = result;
30840
30841 /* return true if number and no extra non-whitespace chracters after */
30842 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
30843 return eType;
30844 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
30845 return -1;
30846 }else{
30847 return 0;
30848 }
30849#else
30850 return !sqlite3Atoi64(z, pResult, length, enc);
30851#endif /* SQLITE_OMIT_FLOATING_POINT */
30852}
30853
30854/*
30855** Compare the 19-character string zNum against the text representation
30856** value 2^63: 9223372036854775808. Return negative, zero, or positive
30857** if zNum is less than, equal to, or greater than the string.
30858** Note that zNum must contain exactly 19 characters.
30859**
30860** Unlike memcmp() this routine is guaranteed to return the difference
30861** in the values of the last digit if the only difference is in the
30862** last digit. So, for example,
30863**
30864** compare2pow63("9223372036854775800", 1)
30865**
30866** will return -8.
30867*/
30868static int compare2pow63(const char *zNum, int incr){
30869 int c = 0;
30870 int i;
30871 /* 012345678901234567 */
30872 const char *pow63 = "922337203685477580";
30873 for(i=0; c==0 && i<18; i++){
30874 c = (zNum[i*incr]-pow63[i])*10;
30875 }
30876 if( c==0 ){
30877 c = zNum[18*incr] - '8';
30878 testcase( c==(-1) );
30879 testcase( c==0 );
30880 testcase( c==(+1) );
30881 }
30882 return c;
30883}
30884
30885/*
30886** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
30887** routine does *not* accept hexadecimal notation.
30888**
30889** Returns:
30890**
30891** -1 Not even a prefix of the input text looks like an integer
30892** 0 Successful transformation. Fits in a 64-bit signed integer.
30893** 1 Excess non-space text after the integer value
30894** 2 Integer too large for a 64-bit signed integer or is malformed
30895** 3 Special case of 9223372036854775808
30896**
30897** length is the number of bytes in the string (bytes, not characters).
30898** The string is not necessarily zero-terminated. The encoding is
30899** given by enc.
30900*/
30901SQLITE_PRIVATEstatic int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
30902 int incr;
30903 u64 u = 0;
30904 int neg = 0; /* assume positive */
30905 int i;
30906 int c = 0;
30907 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
30908 int rc; /* Baseline return code */
30909 const char *zStart;
30910 const char *zEnd = zNum + length;
30911 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE )((void) (0));
30912 if( enc==SQLITE_UTF81 ){
30913 incr = 1;
30914 }else{
30915 incr = 2;
30916 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
30917 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
30918 nonNum = i<length;
30919 zEnd = &zNum[i^1];
30920 zNum += (enc&1);
30921 }
30922 while( zNum<zEnd && sqlite3Isspace(*zNum)(sqlite3CtypeMap[(unsigned char)(*zNum)]&0x01) ) zNum+=incr;
30923 if( zNum<zEnd ){
30924 if( *zNum=='-' ){
30925 neg = 1;
30926 zNum+=incr;
30927 }else if( *zNum=='+' ){
30928 zNum+=incr;
30929 }
30930 }
30931 zStart = zNum;
30932 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
30933 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
30934 u = u*10 + c - '0';
30935 }
30936 testcase( i==18*incr );
30937 testcase( i==19*incr );
30938 testcase( i==20*incr );
30939 if( u>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ){
30940 /* This test and assignment is needed only to suppress UB warnings
30941 ** from clang and -fsanitize=undefined. This test and assignment make
30942 ** the code a little larger and slower, and no harm comes from omitting
30943 ** them, but we must appaise the undefined-behavior pharisees. */
30944 *pNum = neg ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
30945 }else if( neg ){
30946 *pNum = -(i64)u;
30947 }else{
30948 *pNum = (i64)u;
30949 }
30950 rc = 0;
30951 if( i==0 && zStart==zNum ){ /* No digits */
30952 rc = -1;
30953 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
30954 rc = 1;
30955 }else if( &zNum[i]<zEnd ){ /* Extra bytes at the end */
30956 int jj = i;
30957 do{
30958 if( !sqlite3Isspace(zNum[jj])(sqlite3CtypeMap[(unsigned char)(zNum[jj])]&0x01) ){
30959 rc = 1; /* Extra non-space text after the integer */
30960 break;
30961 }
30962 jj += incr;
30963 }while( &zNum[jj]<zEnd );
30964 }
30965 if( i<19*incr ){
30966 /* Less than 19 digits, so we know that it fits in 64 bits */
30967 assert( u<=LARGEST_INT64 )((void) (0));
30968 return rc;
30969 }else{
30970 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
30971 c = i>19*incr ? 1 : compare2pow63(zNum, incr);
30972 if( c<0 ){
30973 /* zNum is less than 9223372036854775808 so it fits */
30974 assert( u<=LARGEST_INT64 )((void) (0));
30975 return rc;
30976 }else{
30977 *pNum = neg ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
30978 if( c>0 ){
30979 /* zNum is greater than 9223372036854775808 so it overflows */
30980 return 2;
30981 }else{
30982 /* zNum is exactly 9223372036854775808. Fits if negative. The
30983 ** special case 2 overflow if positive */
30984 assert( u-1==LARGEST_INT64 )((void) (0));
30985 return neg ? rc : 3;
30986 }
30987 }
30988 }
30989}
30990
30991/*
30992** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
30993** into a 64-bit signed integer. This routine accepts hexadecimal literals,
30994** whereas sqlite3Atoi64() does not.
30995**
30996** Returns:
30997**
30998** 0 Successful transformation. Fits in a 64-bit signed integer.
30999** 1 Excess text after the integer value
31000** 2 Integer too large for a 64-bit signed integer or is malformed
31001** 3 Special case of 9223372036854775808
31002*/
31003SQLITE_PRIVATEstatic int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
31004#ifndef SQLITE_OMIT_HEX_INTEGER
31005 if( z[0]=='0'
31006 && (z[1]=='x' || z[1]=='X')
31007 ){
31008 u64 u = 0;
31009 int i, k;
31010 for(i=2; z[i]=='0'; i++){}
31011 for(k=i; sqlite3Isxdigit(z[k])(sqlite3CtypeMap[(unsigned char)(z[k])]&0x08); k++){
31012 u = u*16 + sqlite3HexToInt(z[k]);
31013 }
31014 memcpy(pOut, &u, 8);
31015 return (z[k]==0 && k-i<=16) ? 0 : 2;
31016 }else
31017#endif /* SQLITE_OMIT_HEX_INTEGER */
31018 {
31019 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF81);
31020 }
31021}
31022
31023/*
31024** If zNum represents an integer that will fit in 32-bits, then set
31025** *pValue to that integer and return true. Otherwise return false.
31026**
31027** This routine accepts both decimal and hexadecimal notation for integers.
31028**
31029** Any non-numeric characters that following zNum are ignored.
31030** This is different from sqlite3Atoi64() which requires the
31031** input number to be zero-terminated.
31032*/
31033SQLITE_PRIVATEstatic int sqlite3GetInt32(const char *zNum, int *pValue){
31034 sqlite_int64 v = 0;
31035 int i, c;
31036 int neg = 0;
31037 if( zNum[0]=='-' ){
31038 neg = 1;
31039 zNum++;
31040 }else if( zNum[0]=='+' ){
31041 zNum++;
31042 }
31043#ifndef SQLITE_OMIT_HEX_INTEGER
31044 else if( zNum[0]=='0'
31045 && (zNum[1]=='x' || zNum[1]=='X')
31046 && sqlite3Isxdigit(zNum[2])(sqlite3CtypeMap[(unsigned char)(zNum[2])]&0x08)
31047 ){
31048 u32 u = 0;
31049 zNum += 2;
31050 while( zNum[0]=='0' ) zNum++;
31051 for(i=0; sqlite3Isxdigit(zNum[i])(sqlite3CtypeMap[(unsigned char)(zNum[i])]&0x08) && i<8; i++){
31052 u = u*16 + sqlite3HexToInt(zNum[i]);
31053 }
31054 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])(sqlite3CtypeMap[(unsigned char)(zNum[i])]&0x08)==0 ){
31055 memcpy(pValue, &u, 4);
31056 return 1;
31057 }else{
31058 return 0;
31059 }
31060 }
31061#endif
31062 if( !sqlite3Isdigit(zNum[0])(sqlite3CtypeMap[(unsigned char)(zNum[0])]&0x04) ) return 0;
31063 while( zNum[0]=='0' ) zNum++;
31064 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
31065 v = v*10 + c;
31066 }
31067
31068 /* The longest decimal representation of a 32 bit integer is 10 digits:
31069 **
31070 ** 1234567890
31071 ** 2^31 -> 2147483648
31072 */
31073 testcase( i==10 );
31074 if( i>10 ){
31075 return 0;
31076 }
31077 testcase( v-neg==2147483647 );
31078 if( v-neg>2147483647 ){
31079 return 0;
31080 }
31081 if( neg ){
31082 v = -v;
31083 }
31084 *pValue = (int)v;
31085 return 1;
31086}
31087
31088/*
31089** Return a 32-bit integer value extracted from a string. If the
31090** string is not an integer, just return 0.
31091*/
31092SQLITE_PRIVATEstatic int sqlite3Atoi(const char *z){
31093 int x = 0;
31094 if( z ) sqlite3GetInt32(z, &x);
31095 return x;
31096}
31097
31098/*
31099** The variable-length integer encoding is as follows:
31100**
31101** KEY:
31102** A = 0xxxxxxx 7 bits of data and one flag bit
31103** B = 1xxxxxxx 7 bits of data and one flag bit
31104** C = xxxxxxxx 8 bits of data
31105**
31106** 7 bits - A
31107** 14 bits - BA
31108** 21 bits - BBA
31109** 28 bits - BBBA
31110** 35 bits - BBBBA
31111** 42 bits - BBBBBA
31112** 49 bits - BBBBBBA
31113** 56 bits - BBBBBBBA
31114** 64 bits - BBBBBBBBC
31115*/
31116
31117/*
31118** Write a 64-bit variable-length integer to memory starting at p[0].
31119** The length of data write will be between 1 and 9 bytes. The number
31120** of bytes written is returned.
31121**
31122** A variable-length integer consists of the lower 7 bits of each byte
31123** for all bytes that have the 8th bit set and one byte with the 8th
31124** bit clear. Except, if we get to the 9th byte, it stores the full
31125** 8 bits and is the last byte.
31126*/
31127static int SQLITE_NOINLINE__attribute__((noinline)) putVarint64(unsigned char *p, u64 v){
31128 int i, j, n;
31129 u8 buf[10];
31130 if( v & (((u64)0xff000000)<<32) ){
31131 p[8] = (u8)v;
31132 v >>= 8;
31133 for(i=7; i>=0; i--){
31134 p[i] = (u8)((v & 0x7f) | 0x80);
31135 v >>= 7;
31136 }
31137 return 9;
31138 }
31139 n = 0;
31140 do{
31141 buf[n++] = (u8)((v & 0x7f) | 0x80);
31142 v >>= 7;
31143 }while( v!=0 );
31144 buf[0] &= 0x7f;
31145 assert( n<=9 )((void) (0));
31146 for(i=0, j=n-1; j>=0; j--, i++){
31147 p[i] = buf[j];
31148 }
31149 return n;
31150}
31151SQLITE_PRIVATEstatic int sqlite3PutVarint(unsigned char *p, u64 v){
31152 if( v<=0x7f ){
31153 p[0] = v&0x7f;
31154 return 1;
31155 }
31156 if( v<=0x3fff ){
31157 p[0] = ((v>>7)&0x7f)|0x80;
31158 p[1] = v&0x7f;
31159 return 2;
31160 }
31161 return putVarint64(p,v);
31162}
31163
31164/*
31165** Bitmasks used by sqlite3GetVarint(). These precomputed constants
31166** are defined here rather than simply putting the constant expressions
31167** inline in order to work around bugs in the RVT compiler.
31168**
31169** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
31170**
31171** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
31172*/
31173#define SLOT_2_00x001fc07f 0x001fc07f
31174#define SLOT_4_2_00xf01fc07f 0xf01fc07f
31175
31176
31177/*
31178** Read a 64-bit variable-length integer from memory starting at p[0].
31179** Return the number of bytes read. The value is stored in *v.
31180*/
31181SQLITE_PRIVATEstatic u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
31182 u32 a,b,s;
31183
31184 if( ((signed char*)p)[0]>=0 ){
31185 *v = *p;
31186 return 1;
31187 }
31188 if( ((signed char*)p)[1]>=0 ){
31189 *v = ((u32)(p[0]&0x7f)<<7) | p[1];
31190 return 2;
31191 }
31192
31193 /* Verify that constants are precomputed correctly */
31194 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) )((void) (0));
31195 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) )((void) (0));
31196
31197 a = ((u32)p[0])<<14;
31198 b = p[1];
31199 p += 2;
31200 a |= *p;
31201 /* a: p0<<14 | p2 (unmasked) */
31202 if (!(a&0x80))
31203 {
31204 a &= SLOT_2_00x001fc07f;
31205 b &= 0x7f;
31206 b = b<<7;
31207 a |= b;
31208 *v = a;
31209 return 3;
31210 }
31211
31212 /* CSE1 from below */
31213 a &= SLOT_2_00x001fc07f;
31214 p++;
31215 b = b<<14;
31216 b |= *p;
31217 /* b: p1<<14 | p3 (unmasked) */
31218 if (!(b&0x80))
31219 {
31220 b &= SLOT_2_00x001fc07f;
31221 /* moved CSE1 up */
31222 /* a &= (0x7f<<14)|(0x7f); */
31223 a = a<<7;
31224 a |= b;
31225 *v = a;
31226 return 4;
31227 }
31228
31229 /* a: p0<<14 | p2 (masked) */
31230 /* b: p1<<14 | p3 (unmasked) */
31231 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
31232 /* moved CSE1 up */
31233 /* a &= (0x7f<<14)|(0x7f); */
31234 b &= SLOT_2_00x001fc07f;
31235 s = a;
31236 /* s: p0<<14 | p2 (masked) */
31237
31238 p++;
31239 a = a<<14;
31240 a |= *p;
31241 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
31242 if (!(a&0x80))
31243 {
31244 /* we can skip these cause they were (effectively) done above
31245 ** while calculating s */
31246 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
31247 /* b &= (0x7f<<14)|(0x7f); */
31248 b = b<<7;
31249 a |= b;
31250 s = s>>18;
31251 *v = ((u64)s)<<32 | a;
31252 return 5;
31253 }
31254
31255 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
31256 s = s<<7;
31257 s |= b;
31258 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
31259
31260 p++;
31261 b = b<<14;
31262 b |= *p;
31263 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
31264 if (!(b&0x80))
31265 {
31266 /* we can skip this cause it was (effectively) done above in calc'ing s */
31267 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
31268 a &= SLOT_2_00x001fc07f;
31269 a = a<<7;
31270 a |= b;
31271 s = s>>18;
31272 *v = ((u64)s)<<32 | a;
31273 return 6;
31274 }
31275
31276 p++;
31277 a = a<<14;
31278 a |= *p;
31279 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
31280 if (!(a&0x80))
31281 {
31282 a &= SLOT_4_2_00xf01fc07f;
31283 b &= SLOT_2_00x001fc07f;
31284 b = b<<7;
31285 a |= b;
31286 s = s>>11;
31287 *v = ((u64)s)<<32 | a;
31288 return 7;
31289 }
31290
31291 /* CSE2 from below */
31292 a &= SLOT_2_00x001fc07f;
31293 p++;
31294 b = b<<14;
31295 b |= *p;
31296 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
31297 if (!(b&0x80))
31298 {
31299 b &= SLOT_4_2_00xf01fc07f;
31300 /* moved CSE2 up */
31301 /* a &= (0x7f<<14)|(0x7f); */
31302 a = a<<7;
31303 a |= b;
31304 s = s>>4;
31305 *v = ((u64)s)<<32 | a;
31306 return 8;
31307 }
31308
31309 p++;
31310 a = a<<15;
31311 a |= *p;
31312 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
31313
31314 /* moved CSE2 up */
31315 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
31316 b &= SLOT_2_00x001fc07f;
31317 b = b<<8;
31318 a |= b;
31319
31320 s = s<<4;
31321 b = p[-4];
31322 b &= 0x7f;
31323 b = b>>3;
31324 s |= b;
31325
31326 *v = ((u64)s)<<32 | a;
31327
31328 return 9;
31329}
31330
31331/*
31332** Read a 32-bit variable-length integer from memory starting at p[0].
31333** Return the number of bytes read. The value is stored in *v.
31334**
31335** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
31336** integer, then set *v to 0xffffffff.
31337**
31338** A MACRO version, getVarint32, is provided which inlines the
31339** single-byte case. All code should use the MACRO version as
31340** this function assumes the single-byte case has already been handled.
31341*/
31342SQLITE_PRIVATEstatic u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
31343 u32 a,b;
31344
31345 /* The 1-byte case. Overwhelmingly the most common. Handled inline
31346 ** by the getVarin32() macro */
31347 a = *p;
31348 /* a: p0 (unmasked) */
31349#ifndef getVarint32
31350 if (!(a&0x80))
31351 {
31352 /* Values between 0 and 127 */
31353 *v = a;
31354 return 1;
31355 }
31356#endif
31357
31358 /* The 2-byte case */
31359 p++;
31360 b = *p;
31361 /* b: p1 (unmasked) */
31362 if (!(b&0x80))
31363 {
31364 /* Values between 128 and 16383 */
31365 a &= 0x7f;
31366 a = a<<7;
31367 *v = a | b;
31368 return 2;
31369 }
31370
31371 /* The 3-byte case */
31372 p++;
31373 a = a<<14;
31374 a |= *p;
31375 /* a: p0<<14 | p2 (unmasked) */
31376 if (!(a&0x80))
31377 {
31378 /* Values between 16384 and 2097151 */
31379 a &= (0x7f<<14)|(0x7f);
31380 b &= 0x7f;
31381 b = b<<7;
31382 *v = a | b;
31383 return 3;
31384 }
31385
31386 /* A 32-bit varint is used to store size information in btrees.
31387 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
31388 ** A 3-byte varint is sufficient, for example, to record the size
31389 ** of a 1048569-byte BLOB or string.
31390 **
31391 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
31392 ** rare larger cases can be handled by the slower 64-bit varint
31393 ** routine.
31394 */
31395#if 1
31396 {
31397 u64 v64;
31398 u8 n;
31399
31400 p -= 2;
31401 n = sqlite3GetVarint(p, &v64);
31402 assert( n>3 && n<=9 )((void) (0));
31403 if( (v64 & SQLITE_MAX_U32((((u64)1)<<32)-1))!=v64 ){
31404 *v = 0xffffffff;
31405 }else{
31406 *v = (u32)v64;
31407 }
31408 return n;
31409 }
31410
31411#else
31412 /* For following code (kept for historical record only) shows an
31413 ** unrolling for the 3- and 4-byte varint cases. This code is
31414 ** slightly faster, but it is also larger and much harder to test.
31415 */
31416 p++;
31417 b = b<<14;
31418 b |= *p;
31419 /* b: p1<<14 | p3 (unmasked) */
31420 if (!(b&0x80))
31421 {
31422 /* Values between 2097152 and 268435455 */
31423 b &= (0x7f<<14)|(0x7f);
31424 a &= (0x7f<<14)|(0x7f);
31425 a = a<<7;
31426 *v = a | b;
31427 return 4;
31428 }
31429
31430 p++;
31431 a = a<<14;
31432 a |= *p;
31433 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
31434 if (!(a&0x80))
31435 {
31436 /* Values between 268435456 and 34359738367 */
31437 a &= SLOT_4_2_00xf01fc07f;
31438 b &= SLOT_4_2_00xf01fc07f;
31439 b = b<<7;
31440 *v = a | b;
31441 return 5;
31442 }
31443
31444 /* We can only reach this point when reading a corrupt database
31445 ** file. In that case we are not in any hurry. Use the (relatively
31446 ** slow) general-purpose sqlite3GetVarint() routine to extract the
31447 ** value. */
31448 {
31449 u64 v64;
31450 u8 n;
31451
31452 p -= 4;
31453 n = sqlite3GetVarint(p, &v64);
31454 assert( n>5 && n<=9 )((void) (0));
31455 *v = (u32)v64;
31456 return n;
31457 }
31458#endif
31459}
31460
31461/*
31462** Return the number of bytes that will be needed to store the given
31463** 64-bit integer.
31464*/
31465SQLITE_PRIVATEstatic int sqlite3VarintLen(u64 v){
31466 int i;
31467 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 )((void) (0)); }
31468 return i;
31469}
31470
31471
31472/*
31473** Read or write a four-byte big-endian integer value.
31474*/
31475SQLITE_PRIVATEstatic u32 sqlite3Get4byte(const u8 *p){
31476#if SQLITE_BYTEORDER1234==4321
31477 u32 x;
31478 memcpy(&x,p,4);
31479 return x;
31480#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
31481 u32 x;
31482 memcpy(&x,p,4);
31483 return __builtin_bswap32(x);
31484#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
31485 u32 x;
31486 memcpy(&x,p,4);
31487 return _byteswap_ulong(x);
31488#else
31489 testcase( p[0]&0x80 );
31490 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
31491#endif
31492}
31493SQLITE_PRIVATEstatic void sqlite3Put4byte(unsigned char *p, u32 v){
31494#if SQLITE_BYTEORDER1234==4321
31495 memcpy(p,&v,4);
31496#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
31497 u32 x = __builtin_bswap32(v);
31498 memcpy(p,&x,4);
31499#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
31500 u32 x = _byteswap_ulong(v);
31501 memcpy(p,&x,4);
31502#else
31503 p[0] = (u8)(v>>24);
31504 p[1] = (u8)(v>>16);
31505 p[2] = (u8)(v>>8);
31506 p[3] = (u8)v;
31507#endif
31508}
31509
31510
31511
31512/*
31513** Translate a single byte of Hex into an integer.
31514** This routine only works if h really is a valid hexadecimal
31515** character: 0..9a..fA..F
31516*/
31517SQLITE_PRIVATEstatic u8 sqlite3HexToInt(int h){
31518 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') )((void) (0));
31519#ifdef SQLITE_ASCII1
31520 h += 9*(1&(h>>6));
31521#endif
31522#ifdef SQLITE_EBCDIC
31523 h += 9*(1&~(h>>4));
31524#endif
31525 return (u8)(h & 0xf);
31526}
31527
31528#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
31529/*
31530** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
31531** value. Return a pointer to its binary value. Space to hold the
31532** binary value has been obtained from malloc and must be freed by
31533** the calling routine.
31534*/
31535SQLITE_PRIVATEstatic void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
31536 char *zBlob;
31537 int i;
31538
31539 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
31540 n--;
31541 if( zBlob ){
31542 for(i=0; i<n; i+=2){
31543 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
31544 }
31545 zBlob[i/2] = 0;
31546 }
31547 return zBlob;
31548}
31549#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
31550
31551/*
31552** Log an error that is an API call on a connection pointer that should
31553** not have been used. The "type" of connection pointer is given as the
31554** argument. The zType is a word like "NULL" or "closed" or "invalid".
31555*/
31556static void logBadConnection(const char *zType){
31557 sqlite3_log(SQLITE_MISUSE21,
31558 "API call with %s database connection pointer",
31559 zType
31560 );
31561}
31562
31563/*
31564** Check to make sure we have a valid db pointer. This test is not
31565** foolproof but it does provide some measure of protection against
31566** misuse of the interface such as passing in db pointers that are
31567** NULL or which have been previously closed. If this routine returns
31568** 1 it means that the db pointer is valid and 0 if it should not be
31569** dereferenced for any reason. The calling function should invoke
31570** SQLITE_MISUSE immediately.
31571**
31572** sqlite3SafetyCheckOk() requires that the db pointer be valid for
31573** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
31574** open properly and is not fit for general use but which can be
31575** used as an argument to sqlite3_errmsg() or sqlite3_close().
31576*/
31577SQLITE_PRIVATEstatic int sqlite3SafetyCheckOk(sqlite3 *db){
31578 u32 magic;
31579 if( db==0 ){
31580 logBadConnection("NULL");
31581 return 0;
31582 }
31583 magic = db->magic;
31584 if( magic!=SQLITE_MAGIC_OPEN0xa029a697 ){
31585 if( sqlite3SafetyCheckSickOrOk(db) ){
31586 testcase( sqlite3GlobalConfig.xLog!=0 );
31587 logBadConnection("unopened");
31588 }
31589 return 0;
31590 }else{
31591 return 1;
31592 }
31593}
31594SQLITE_PRIVATEstatic int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
31595 u32 magic;
31596 magic = db->magic;
31597 if( magic!=SQLITE_MAGIC_SICK0x4b771290 &&
31598 magic!=SQLITE_MAGIC_OPEN0xa029a697 &&
31599 magic!=SQLITE_MAGIC_BUSY0xf03b7906 ){
31600 testcase( sqlite3GlobalConfig.xLog!=0 );
31601 logBadConnection("invalid");
31602 return 0;
31603 }else{
31604 return 1;
31605 }
31606}
31607
31608/*
31609** Attempt to add, substract, or multiply the 64-bit signed value iB against
31610** the other 64-bit signed integer at *pA and store the result in *pA.
31611** Return 0 on success. Or if the operation would have resulted in an
31612** overflow, leave *pA unchanged and return 1.
31613*/
31614SQLITE_PRIVATEstatic int sqlite3AddInt64(i64 *pA, i64 iB){
31615#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
31616 return __builtin_add_overflow(*pA, iB, pA);
31617#else
31618 i64 iA = *pA;
31619 testcase( iA==0 ); testcase( iA==1 );
31620 testcase( iB==-1 ); testcase( iB==0 );
31621 if( iB>=0 ){
31622 testcase( iA>0 && LARGEST_INT64 - iA == iB );
31623 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
31624 if( iA>0 && LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) - iA < iB ) return 1;
31625 }else{
31626 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
31627 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
31628 if( iA<0 && -(iA + LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))) > iB + 1 ) return 1;
31629 }
31630 *pA += iB;
31631 return 0;
31632#endif
31633}
31634SQLITE_PRIVATEstatic int sqlite3SubInt64(i64 *pA, i64 iB){
31635#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
31636 return __builtin_sub_overflow(*pA, iB, pA);
31637#else
31638 testcase( iB==SMALLEST_INT64+1 );
31639 if( iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
31640 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
31641 if( (*pA)>=0 ) return 1;
31642 *pA -= iB;
31643 return 0;
31644 }else{
31645 return sqlite3AddInt64(pA, -iB);
31646 }
31647#endif
31648}
31649SQLITE_PRIVATEstatic int sqlite3MulInt64(i64 *pA, i64 iB){
31650#if GCC_VERSION(4*1000000+2*1000+1)>=5004000 && !defined(__INTEL_COMPILER)
31651 return __builtin_mul_overflow(*pA, iB, pA);
31652#else
31653 i64 iA = *pA;
31654 if( iB>0 ){
31655 if( iA>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/iB ) return 1;
31656 if( iA<SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))/iB ) return 1;
31657 }else if( iB<0 ){
31658 if( iA>0 ){
31659 if( iB<SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)))/iA ) return 1;
31660 }else if( iA<0 ){
31661 if( iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) return 1;
31662 if( iA==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) return 1;
31663 if( -iA>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/-iB ) return 1;
31664 }
31665 }
31666 *pA = iA*iB;
31667 return 0;
31668#endif
31669}
31670
31671/*
31672** Compute the absolute value of a 32-bit signed integer, of possible. Or
31673** if the integer has a value of -2147483648, return +2147483647
31674*/
31675SQLITE_PRIVATEstatic int sqlite3AbsInt32(int x){
31676 if( x>=0 ) return x;
31677 if( x==(int)0x80000000 ) return 0x7fffffff;
31678 return -x;
31679}
31680
31681#ifdef SQLITE_ENABLE_8_3_NAMES
31682/*
31683** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
31684** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
31685** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
31686** three characters, then shorten the suffix on z[] to be the last three
31687** characters of the original suffix.
31688**
31689** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
31690** do the suffix shortening regardless of URI parameter.
31691**
31692** Examples:
31693**
31694** test.db-journal => test.nal
31695** test.db-wal => test.wal
31696** test.db-shm => test.shm
31697** test.db-mj7f3319fa => test.9fa
31698*/
31699SQLITE_PRIVATEstatic void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
31700#if SQLITE_ENABLE_8_3_NAMES<2
31701 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
31702#endif
31703 {
31704 int i, sz;
31705 sz = sqlite3Strlen30(z);
31706 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
31707 if( z[i]=='.' && ALWAYS(sz>i+4)(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
31708 }
31709}
31710#endif
31711
31712/*
31713** Find (an approximate) sum of two LogEst values. This computation is
31714** not a simple "+" operator because LogEst is stored as a logarithmic
31715** value.
31716**
31717*/
31718SQLITE_PRIVATEstatic LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
31719 static const unsigned char x[] = {
31720 10, 10, /* 0,1 */
31721 9, 9, /* 2,3 */
31722 8, 8, /* 4,5 */
31723 7, 7, 7, /* 6,7,8 */
31724 6, 6, 6, /* 9,10,11 */
31725 5, 5, 5, /* 12-14 */
31726 4, 4, 4, 4, /* 15-18 */
31727 3, 3, 3, 3, 3, 3, /* 19-24 */
31728 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
31729 };
31730 if( a>=b ){
31731 if( a>b+49 ) return a;
31732 if( a>b+31 ) return a+1;
31733 return a+x[a-b];
31734 }else{
31735 if( b>a+49 ) return b;
31736 if( b>a+31 ) return b+1;
31737 return b+x[b-a];
31738 }
31739}
31740
31741/*
31742** Convert an integer into a LogEst. In other words, compute an
31743** approximation for 10*log2(x).
31744*/
31745SQLITE_PRIVATEstatic LogEst sqlite3LogEst(u64 x){
31746 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
31747 LogEst y = 40;
31748 if( x<8 ){
31749 if( x<2 ) return 0;
31750 while( x<8 ){ y -= 10; x <<= 1; }
31751 }else{
31752#if GCC_VERSION(4*1000000+2*1000+1)>=5004000
31753 int i = 60 - __builtin_clzll(x);
31754 y += i*10;
31755 x >>= i;
31756#else
31757 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
31758 while( x>15 ){ y += 10; x >>= 1; }
31759#endif
31760 }
31761 return a[x&7] + y - 10;
31762}
31763
31764#ifndef SQLITE_OMIT_VIRTUALTABLE
31765/*
31766** Convert a double into a LogEst
31767** In other words, compute an approximation for 10*log2(x).
31768*/
31769SQLITE_PRIVATEstatic LogEst sqlite3LogEstFromDouble(double x){
31770 u64 a;
31771 LogEst e;
31772 assert( sizeof(x)==8 && sizeof(a)==8 )((void) (0));
31773 if( x<=1 ) return 0;
31774 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
31775 memcpy(&a, &x, 8);
31776 e = (a>>52) - 1022;
31777 return e*10;
31778}
31779#endif /* SQLITE_OMIT_VIRTUALTABLE */
31780
31781#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
31782 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
31783 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
31784/*
31785** Convert a LogEst into an integer.
31786**
31787** Note that this routine is only used when one or more of various
31788** non-standard compile-time options is enabled.
31789*/
31790SQLITE_PRIVATEstatic u64 sqlite3LogEstToInt(LogEst x){
31791 u64 n;
31792 n = x%10;
31793 x /= 10;
31794 if( n>=5 ) n -= 2;
31795 else if( n>=1 ) n -= 1;
31796#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
31797 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
31798 if( x>60 ) return (u64)LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
31799#else
31800 /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input
31801 ** possible to this routine is 310, resulting in a maximum x of 31 */
31802 assert( x<=60 )((void) (0));
31803#endif
31804 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
31805}
31806#endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
31807
31808/*
31809** Add a new name/number pair to a VList. This might require that the
31810** VList object be reallocated, so return the new VList. If an OOM
31811** error occurs, the original VList returned and the
31812** db->mallocFailed flag is set.
31813**
31814** A VList is really just an array of integers. To destroy a VList,
31815** simply pass it to sqlite3DbFree().
31816**
31817** The first integer is the number of integers allocated for the whole
31818** VList. The second integer is the number of integers actually used.
31819** Each name/number pair is encoded by subsequent groups of 3 or more
31820** integers.
31821**
31822** Each name/number pair starts with two integers which are the numeric
31823** value for the pair and the size of the name/number pair, respectively.
31824** The text name overlays one or more following integers. The text name
31825** is always zero-terminated.
31826**
31827** Conceptually:
31828**
31829** struct VList {
31830** int nAlloc; // Number of allocated slots
31831** int nUsed; // Number of used slots
31832** struct VListEntry {
31833** int iValue; // Value for this entry
31834** int nSlot; // Slots used by this entry
31835** // ... variable name goes here
31836** } a[0];
31837** }
31838**
31839** During code generation, pointers to the variable names within the
31840** VList are taken. When that happens, nAlloc is set to zero as an
31841** indication that the VList may never again be enlarged, since the
31842** accompanying realloc() would invalidate the pointers.
31843*/
31844SQLITE_PRIVATEstatic VList *sqlite3VListAdd(
31845 sqlite3 *db, /* The database connection used for malloc() */
31846 VList *pIn, /* The input VList. Might be NULL */
31847 const char *zName, /* Name of symbol to add */
31848 int nName, /* Bytes of text in zName */
31849 int iVal /* Value to associate with zName */
31850){
31851 int nInt; /* number of sizeof(int) objects needed for zName */
31852 char *z; /* Pointer to where zName will be stored */
31853 int i; /* Index in pIn[] where zName is stored */
31854
31855 nInt = nName/4 + 3;
31856 assert( pIn==0 || pIn[0]>=3 )((void) (0)); /* Verify ok to add new elements */
31857 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
31858 /* Enlarge the allocation */
31859 sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
31860 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
31861 if( pOut==0 ) return pIn;
31862 if( pIn==0 ) pOut[1] = 2;
31863 pIn = pOut;
31864 pIn[0] = nAlloc;
31865 }
31866 i = pIn[1];
31867 pIn[i] = iVal;
31868 pIn[i+1] = nInt;
31869 z = (char*)&pIn[i+2];
31870 pIn[1] = i+nInt;
31871 assert( pIn[1]<=pIn[0] )((void) (0));
31872 memcpy(z, zName, nName);
31873 z[nName] = 0;
31874 return pIn;
31875}
31876
31877/*
31878** Return a pointer to the name of a variable in the given VList that
31879** has the value iVal. Or return a NULL if there is no such variable in
31880** the list
31881*/
31882SQLITE_PRIVATEstatic const char *sqlite3VListNumToName(VList *pIn, int iVal){
31883 int i, mx;
31884 if( pIn==0 ) return 0;
31885 mx = pIn[1];
31886 i = 2;
31887 do{
31888 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
31889 i += pIn[i+1];
31890 }while( i<mx );
31891 return 0;
31892}
31893
31894/*
31895** Return the number of the variable named zName, if it is in VList.
31896** or return 0 if there is no such variable.
31897*/
31898SQLITE_PRIVATEstatic int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
31899 int i, mx;
31900 if( pIn==0 ) return 0;
31901 mx = pIn[1];
31902 i = 2;
31903 do{
31904 const char *z = (const char*)&pIn[i+2];
31905 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
31906 i += pIn[i+1];
31907 }while( i<mx );
31908 return 0;
31909}
31910
31911/************** End of util.c ************************************************/
31912/************** Begin file hash.c ********************************************/
31913/*
31914** 2001 September 22
31915**
31916** The author disclaims copyright to this source code. In place of
31917** a legal notice, here is a blessing:
31918**
31919** May you do good and not evil.
31920** May you find forgiveness for yourself and forgive others.
31921** May you share freely, never taking more than you give.
31922**
31923*************************************************************************
31924** This is the implementation of generic hash-tables
31925** used in SQLite.
31926*/
31927/* #include "sqliteInt.h" */
31928/* #include <assert.h> */
31929
31930/* Turn bulk memory into a hash table object by initializing the
31931** fields of the Hash structure.
31932**
31933** "pNew" is a pointer to the hash table that is to be initialized.
31934*/
31935SQLITE_PRIVATEstatic void sqlite3HashInit(Hash *pNew){
31936 assert( pNew!=0 )((void) (0));
31937 pNew->first = 0;
31938 pNew->count = 0;
31939 pNew->htsize = 0;
31940 pNew->ht = 0;
31941}
31942
31943/* Remove all entries from a hash table. Reclaim all memory.
31944** Call this routine to delete a hash table or to reset a hash table
31945** to the empty state.
31946*/
31947SQLITE_PRIVATEstatic void sqlite3HashClear(Hash *pH){
31948 HashElem *elem; /* For looping over all elements of the table */
31949
31950 assert( pH!=0 )((void) (0));
31951 elem = pH->first;
31952 pH->first = 0;
31953 sqlite3_free(pH->ht);
31954 pH->ht = 0;
31955 pH->htsize = 0;
31956 while( elem ){
31957 HashElem *next_elem = elem->next;
31958 sqlite3_free(elem);
31959 elem = next_elem;
31960 }
31961 pH->count = 0;
31962}
31963
31964/*
31965** The hashing function.
31966*/
31967static unsigned int strHash(const char *z){
31968 unsigned int h = 0;
31969 unsigned char c;
31970 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
31971 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
31972 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
31973 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
31974 h += sqlite3UpperToLower[c];
31975 h *= 0x9e3779b1;
31976 }
31977 return h;
31978}
31979
31980
31981/* Link pNew element into the hash table pH. If pEntry!=0 then also
31982** insert pNew into the pEntry hash bucket.
31983*/
31984static void insertElement(
31985 Hash *pH, /* The complete hash table */
31986 struct _ht *pEntry, /* The entry into which pNew is inserted */
31987 HashElem *pNew /* The element to be inserted */
31988){
31989 HashElem *pHead; /* First element already in pEntry */
31990 if( pEntry ){
31991 pHead = pEntry->count ? pEntry->chain : 0;
31992 pEntry->count++;
31993 pEntry->chain = pNew;
31994 }else{
31995 pHead = 0;
31996 }
31997 if( pHead ){
31998 pNew->next = pHead;
31999 pNew->prev = pHead->prev;
32000 if( pHead->prev ){ pHead->prev->next = pNew; }
32001 else { pH->first = pNew; }
32002 pHead->prev = pNew;
32003 }else{
32004 pNew->next = pH->first;
32005 if( pH->first ){ pH->first->prev = pNew; }
32006 pNew->prev = 0;
32007 pH->first = pNew;
32008 }
32009}
32010
32011
32012/* Resize the hash table so that it cantains "new_size" buckets.
32013**
32014** The hash table might fail to resize if sqlite3_malloc() fails or
32015** if the new size is the same as the prior size.
32016** Return TRUE if the resize occurs and false if not.
32017*/
32018static int rehash(Hash *pH, unsigned int new_size){
32019 struct _ht *new_ht; /* The new hash table */
32020 HashElem *elem, *next_elem; /* For looping over existing elements */
32021
32022#if SQLITE_MALLOC_SOFT_LIMIT1024>0
32023 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT1024 ){
32024 new_size = SQLITE_MALLOC_SOFT_LIMIT1024/sizeof(struct _ht);
32025 }
32026 if( new_size==pH->htsize ) return 0;
32027#endif
32028
32029 /* The inability to allocates space for a larger hash table is
32030 ** a performance hit but it is not a fatal error. So mark the
32031 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
32032 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
32033 ** only zeroes the requested number of bytes whereas this module will
32034 ** use the actual amount of space allocated for the hash table (which
32035 ** may be larger than the requested amount).
32036 */
32037 sqlite3BeginBenignMalloc();
32038 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
32039 sqlite3EndBenignMalloc();
32040
32041 if( new_ht==0 ) return 0;
32042 sqlite3_free(pH->ht);
32043 pH->ht = new_ht;
32044 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
32045 memset(new_ht, 0, new_size*sizeof(struct _ht));
32046 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
32047 unsigned int h = strHash(elem->pKey) % new_size;
32048 next_elem = elem->next;
32049 insertElement(pH, &new_ht[h], elem);
32050 }
32051 return 1;
32052}
32053
32054/* This function (for internal use only) locates an element in an
32055** hash table that matches the given key. If no element is found,
32056** a pointer to a static null element with HashElem.data==0 is returned.
32057** If pH is not NULL, then the hash for this key is written to *pH.
32058*/
32059static HashElem *findElementWithHash(
32060 const Hash *pH, /* The pH to be searched */
32061 const char *pKey, /* The key we are searching for */
32062 unsigned int *pHash /* Write the hash value here */
32063){
32064 HashElem *elem; /* Used to loop thru the element list */
32065 unsigned int count; /* Number of elements left to test */
32066 unsigned int h; /* The computed hash */
32067 static HashElem nullElement = { 0, 0, 0, 0 };
32068
32069 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
32070 struct _ht *pEntry;
32071 h = strHash(pKey) % pH->htsize;
32072 pEntry = &pH->ht[h];
32073 elem = pEntry->chain;
32074 count = pEntry->count;
32075 }else{
32076 h = 0;
32077 elem = pH->first;
32078 count = pH->count;
32079 }
32080 if( pHash ) *pHash = h;
32081 while( count-- ){
32082 assert( elem!=0 )((void) (0));
32083 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
32084 return elem;
32085 }
32086 elem = elem->next;
32087 }
32088 return &nullElement;
32089}
32090
32091/* Remove a single entry from the hash table given a pointer to that
32092** element and a hash on the element's key.
32093*/
32094static void removeElementGivenHash(
32095 Hash *pH, /* The pH containing "elem" */
32096 HashElem* elem, /* The element to be removed from the pH */
32097 unsigned int h /* Hash value for the element */
32098){
32099 struct _ht *pEntry;
32100 if( elem->prev ){
32101 elem->prev->next = elem->next;
32102 }else{
32103 pH->first = elem->next;
32104 }
32105 if( elem->next ){
32106 elem->next->prev = elem->prev;
32107 }
32108 if( pH->ht ){
32109 pEntry = &pH->ht[h];
32110 if( pEntry->chain==elem ){
32111 pEntry->chain = elem->next;
32112 }
32113 assert( pEntry->count>0 )((void) (0));
32114 pEntry->count--;
32115 }
32116 sqlite3_free( elem );
32117 pH->count--;
32118 if( pH->count==0 ){
32119 assert( pH->first==0 )((void) (0));
32120 assert( pH->count==0 )((void) (0));
32121 sqlite3HashClear(pH);
32122 }
32123}
32124
32125/* Attempt to locate an element of the hash table pH with a key
32126** that matches pKey. Return the data for this element if it is
32127** found, or NULL if there is no match.
32128*/
32129SQLITE_PRIVATEstatic void *sqlite3HashFind(const Hash *pH, const char *pKey){
32130 assert( pH!=0 )((void) (0));
32131 assert( pKey!=0 )((void) (0));
32132 return findElementWithHash(pH, pKey, 0)->data;
32133}
32134
32135/* Insert an element into the hash table pH. The key is pKey
32136** and the data is "data".
32137**
32138** If no element exists with a matching key, then a new
32139** element is created and NULL is returned.
32140**
32141** If another element already exists with the same key, then the
32142** new data replaces the old data and the old data is returned.
32143** The key is not copied in this instance. If a malloc fails, then
32144** the new data is returned and the hash table is unchanged.
32145**
32146** If the "data" parameter to this function is NULL, then the
32147** element corresponding to "key" is removed from the hash table.
32148*/
32149SQLITE_PRIVATEstatic void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
32150 unsigned int h; /* the hash of the key modulo hash table size */
32151 HashElem *elem; /* Used to loop thru the element list */
32152 HashElem *new_elem; /* New element added to the pH */
32153
32154 assert( pH!=0 )((void) (0));
32155 assert( pKey!=0 )((void) (0));
32156 elem = findElementWithHash(pH,pKey,&h);
32157 if( elem->data ){
32158 void *old_data = elem->data;
32159 if( data==0 ){
32160 removeElementGivenHash(pH,elem,h);
32161 }else{
32162 elem->data = data;
32163 elem->pKey = pKey;
32164 }
32165 return old_data;
32166 }
32167 if( data==0 ) return 0;
32168 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
32169 if( new_elem==0 ) return data;
32170 new_elem->pKey = pKey;
32171 new_elem->data = data;
32172 pH->count++;
32173 if( pH->count>=10 && pH->count > 2*pH->htsize ){
32174 if( rehash(pH, pH->count*2) ){
32175 assert( pH->htsize>0 )((void) (0));
32176 h = strHash(pKey) % pH->htsize;
32177 }
32178 }
32179 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
32180 return 0;
32181}
32182
32183/************** End of hash.c ************************************************/
32184/************** Begin file opcodes.c *****************************************/
32185/* Automatically generated. Do not edit */
32186/* See the tool/mkopcodec.tcl script for details. */
32187#if !defined(SQLITE_OMIT_EXPLAIN) \
32188 || defined(VDBE_PROFILE) \
32189 || defined(SQLITE_DEBUG)
32190#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
32191# define OpHelp(X) "\0" X
32192#else
32193# define OpHelp(X)
32194#endif
32195SQLITE_PRIVATEstatic const char *sqlite3OpcodeName(int i){
32196 static const char *const azName[] = {
32197 /* 0 */ "Savepoint" OpHelp(""),
32198 /* 1 */ "AutoCommit" OpHelp(""),
32199 /* 2 */ "Transaction" OpHelp(""),
32200 /* 3 */ "SorterNext" OpHelp(""),
32201 /* 4 */ "Prev" OpHelp(""),
32202 /* 5 */ "Next" OpHelp(""),
32203 /* 6 */ "Checkpoint" OpHelp(""),
32204 /* 7 */ "JournalMode" OpHelp(""),
32205 /* 8 */ "Vacuum" OpHelp(""),
32206 /* 9 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
32207 /* 10 */ "VUpdate" OpHelp("data=r[P3@P2]"),
32208 /* 11 */ "Goto" OpHelp(""),
32209 /* 12 */ "Gosub" OpHelp(""),
32210 /* 13 */ "InitCoroutine" OpHelp(""),
32211 /* 14 */ "Yield" OpHelp(""),
32212 /* 15 */ "MustBeInt" OpHelp(""),
32213 /* 16 */ "Jump" OpHelp(""),
32214 /* 17 */ "Once" OpHelp(""),
32215 /* 18 */ "If" OpHelp(""),
32216 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
32217 /* 20 */ "IfNot" OpHelp(""),
32218 /* 21 */ "IfNullRow" OpHelp("if P1.nullRow then r[P3]=NULL, goto P2"),
32219 /* 22 */ "SeekLT" OpHelp("key=r[P3@P4]"),
32220 /* 23 */ "SeekLE" OpHelp("key=r[P3@P4]"),
32221 /* 24 */ "SeekGE" OpHelp("key=r[P3@P4]"),
32222 /* 25 */ "SeekGT" OpHelp("key=r[P3@P4]"),
32223 /* 26 */ "IfNoHope" OpHelp("key=r[P3@P4]"),
32224 /* 27 */ "NoConflict" OpHelp("key=r[P3@P4]"),
32225 /* 28 */ "NotFound" OpHelp("key=r[P3@P4]"),
32226 /* 29 */ "Found" OpHelp("key=r[P3@P4]"),
32227 /* 30 */ "SeekRowid" OpHelp("intkey=r[P3]"),
32228 /* 31 */ "NotExists" OpHelp("intkey=r[P3]"),
32229 /* 32 */ "Last" OpHelp(""),
32230 /* 33 */ "IfSmaller" OpHelp(""),
32231 /* 34 */ "SorterSort" OpHelp(""),
32232 /* 35 */ "Sort" OpHelp(""),
32233 /* 36 */ "Rewind" OpHelp(""),
32234 /* 37 */ "IdxLE" OpHelp("key=r[P3@P4]"),
32235 /* 38 */ "IdxGT" OpHelp("key=r[P3@P4]"),
32236 /* 39 */ "IdxLT" OpHelp("key=r[P3@P4]"),
32237 /* 40 */ "IdxGE" OpHelp("key=r[P3@P4]"),
32238 /* 41 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
32239 /* 42 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
32240 /* 43 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
32241 /* 44 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
32242 /* 45 */ "Program" OpHelp(""),
32243 /* 46 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
32244 /* 47 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
32245 /* 48 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
32246 /* 49 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
32247 /* 50 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
32248 /* 51 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
32249 /* 52 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
32250 /* 53 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
32251 /* 54 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
32252 /* 55 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
32253 /* 56 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
32254 /* 57 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
32255 /* 58 */ "ElseNotEq" OpHelp(""),
32256 /* 59 */ "IncrVacuum" OpHelp(""),
32257 /* 60 */ "VNext" OpHelp(""),
32258 /* 61 */ "Init" OpHelp("Start at P2"),
32259 /* 62 */ "PureFunc0" OpHelp(""),
32260 /* 63 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
32261 /* 64 */ "PureFunc" OpHelp(""),
32262 /* 65 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
32263 /* 66 */ "Return" OpHelp(""),
32264 /* 67 */ "EndCoroutine" OpHelp(""),
32265 /* 68 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
32266 /* 69 */ "Halt" OpHelp(""),
32267 /* 70 */ "Integer" OpHelp("r[P2]=P1"),
32268 /* 71 */ "Int64" OpHelp("r[P2]=P4"),
32269 /* 72 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
32270 /* 73 */ "Null" OpHelp("r[P2..P3]=NULL"),
32271 /* 74 */ "SoftNull" OpHelp("r[P1]=NULL"),
32272 /* 75 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
32273 /* 76 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
32274 /* 77 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
32275 /* 78 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
32276 /* 79 */ "SCopy" OpHelp("r[P2]=r[P1]"),
32277 /* 80 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
32278 /* 81 */ "ResultRow" OpHelp("output=r[P1@P2]"),
32279 /* 82 */ "CollSeq" OpHelp(""),
32280 /* 83 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
32281 /* 84 */ "RealAffinity" OpHelp(""),
32282 /* 85 */ "Cast" OpHelp("affinity(r[P1])"),
32283 /* 86 */ "Permutation" OpHelp(""),
32284 /* 87 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
32285 /* 88 */ "IsTrue" OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
32286 /* 89 */ "Offset" OpHelp("r[P3] = sqlite_offset(P1)"),
32287 /* 90 */ "Column" OpHelp("r[P3]=PX"),
32288 /* 91 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
32289 /* 92 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
32290 /* 93 */ "Count" OpHelp("r[P2]=count()"),
32291 /* 94 */ "ReadCookie" OpHelp(""),
32292 /* 95 */ "SetCookie" OpHelp(""),
32293 /* 96 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
32294 /* 97 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
32295 /* 98 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
32296 /* 99 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
32297 /* 100 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
32298 /* 101 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
32299 /* 102 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
32300 /* 103 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
32301 /* 104 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
32302 /* 105 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
32303 /* 106 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
32304 /* 107 */ "BitNot" OpHelp("r[P2]= ~r[P1]"),
32305 /* 108 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
32306 /* 109 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
32307 /* 110 */ "String8" OpHelp("r[P2]='P4'"),
32308 /* 111 */ "OpenDup" OpHelp(""),
32309 /* 112 */ "OpenAutoindex" OpHelp("nColumn=P2"),
32310 /* 113 */ "OpenEphemeral" OpHelp("nColumn=P2"),
32311 /* 114 */ "SorterOpen" OpHelp(""),
32312 /* 115 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
32313 /* 116 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
32314 /* 117 */ "Close" OpHelp(""),
32315 /* 118 */ "ColumnsUsed" OpHelp(""),
32316 /* 119 */ "SeekHit" OpHelp("seekHit=P2"),
32317 /* 120 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
32318 /* 121 */ "NewRowid" OpHelp("r[P2]=rowid"),
32319 /* 122 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
32320 /* 123 */ "Delete" OpHelp(""),
32321 /* 124 */ "ResetCount" OpHelp(""),
32322 /* 125 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
32323 /* 126 */ "SorterData" OpHelp("r[P2]=data"),
32324 /* 127 */ "RowData" OpHelp("r[P2]=data"),
32325 /* 128 */ "Rowid" OpHelp("r[P2]=rowid"),
32326 /* 129 */ "NullRow" OpHelp(""),
32327 /* 130 */ "SeekEnd" OpHelp(""),
32328 /* 131 */ "SorterInsert" OpHelp("key=r[P2]"),
32329 /* 132 */ "IdxInsert" OpHelp("key=r[P2]"),
32330 /* 133 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
32331 /* 134 */ "DeferredSeek" OpHelp("Move P3 to P1.rowid if needed"),
32332 /* 135 */ "IdxRowid" OpHelp("r[P2]=rowid"),
32333 /* 136 */ "Destroy" OpHelp(""),
32334 /* 137 */ "Clear" OpHelp(""),
32335 /* 138 */ "ResetSorter" OpHelp(""),
32336 /* 139 */ "CreateBtree" OpHelp("r[P2]=root iDb=P1 flags=P3"),
32337 /* 140 */ "SqlExec" OpHelp(""),
32338 /* 141 */ "ParseSchema" OpHelp(""),
32339 /* 142 */ "LoadAnalysis" OpHelp(""),
32340 /* 143 */ "DropTable" OpHelp(""),
32341 /* 144 */ "DropIndex" OpHelp(""),
32342 /* 145 */ "Real" OpHelp("r[P2]=P4"),
32343 /* 146 */ "DropTrigger" OpHelp(""),
32344 /* 147 */ "IntegrityCk" OpHelp(""),
32345 /* 148 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
32346 /* 149 */ "Param" OpHelp(""),
32347 /* 150 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
32348 /* 151 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
32349 /* 152 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
32350 /* 153 */ "AggInverse" OpHelp("accum=r[P3] inverse(r[P2@P5])"),
32351 /* 154 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
32352 /* 155 */ "AggStep1" OpHelp("accum=r[P3] step(r[P2@P5])"),
32353 /* 156 */ "AggValue" OpHelp("r[P3]=value N=P2"),
32354 /* 157 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
32355 /* 158 */ "Expire" OpHelp(""),
32356 /* 159 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
32357 /* 160 */ "VBegin" OpHelp(""),
32358 /* 161 */ "VCreate" OpHelp(""),
32359 /* 162 */ "VDestroy" OpHelp(""),
32360 /* 163 */ "VOpen" OpHelp(""),
32361 /* 164 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
32362 /* 165 */ "VRename" OpHelp(""),
32363 /* 166 */ "Pagecount" OpHelp(""),
32364 /* 167 */ "MaxPgcnt" OpHelp(""),
32365 /* 168 */ "Trace" OpHelp(""),
32366 /* 169 */ "CursorHint" OpHelp(""),
32367 /* 170 */ "Noop" OpHelp(""),
32368 /* 171 */ "Explain" OpHelp(""),
32369 /* 172 */ "Abortable" OpHelp(""),
32370 };
32371 return azName[i];
32372}
32373#endif
32374
32375/************** End of opcodes.c *********************************************/
32376/************** Begin file os_unix.c *****************************************/
32377/*
32378** 2004 May 22
32379**
32380** The author disclaims copyright to this source code. In place of
32381** a legal notice, here is a blessing:
32382**
32383** May you do good and not evil.
32384** May you find forgiveness for yourself and forgive others.
32385** May you share freely, never taking more than you give.
32386**
32387******************************************************************************
32388**
32389** This file contains the VFS implementation for unix-like operating systems
32390** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
32391**
32392** There are actually several different VFS implementations in this file.
32393** The differences are in the way that file locking is done. The default
32394** implementation uses Posix Advisory Locks. Alternative implementations
32395** use flock(), dot-files, various proprietary locking schemas, or simply
32396** skip locking all together.
32397**
32398** This source file is organized into divisions where the logic for various
32399** subfunctions is contained within the appropriate division. PLEASE
32400** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
32401** in the correct division and should be clearly labeled.
32402**
32403** The layout of divisions is as follows:
32404**
32405** * General-purpose declarations and utility functions.
32406** * Unique file ID logic used by VxWorks.
32407** * Various locking primitive implementations (all except proxy locking):
32408** + for Posix Advisory Locks
32409** + for no-op locks
32410** + for dot-file locks
32411** + for flock() locking
32412** + for named semaphore locks (VxWorks only)
32413** + for AFP filesystem locks (MacOSX only)
32414** * sqlite3_file methods not associated with locking.
32415** * Definitions of sqlite3_io_methods objects for all locking
32416** methods plus "finder" functions for each locking method.
32417** * sqlite3_vfs method implementations.
32418** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
32419** * Definitions of sqlite3_vfs objects for all locking methods
32420** plus implementations of sqlite3_os_init() and sqlite3_os_end().
32421*/
32422/* #include "sqliteInt.h" */
32423#if SQLITE_OS_UNIX1 /* This file is used on unix only */
32424
32425/*
32426** There are various methods for file locking used for concurrency
32427** control:
32428**
32429** 1. POSIX locking (the default),
32430** 2. No locking,
32431** 3. Dot-file locking,
32432** 4. flock() locking,
32433** 5. AFP locking (OSX only),
32434** 6. Named POSIX semaphores (VXWorks only),
32435** 7. proxy locking. (OSX only)
32436**
32437** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
32438** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
32439** selection of the appropriate locking style based on the filesystem
32440** where the database is located.
32441*/
32442#if !defined(SQLITE_ENABLE_LOCKING_STYLE0)
32443# if defined(__APPLE__)
32444# define SQLITE_ENABLE_LOCKING_STYLE0 1
32445# else
32446# define SQLITE_ENABLE_LOCKING_STYLE0 0
32447# endif
32448#endif
32449
32450/* Use pread() and pwrite() if they are available */
32451#if defined(__APPLE__)
32452# define HAVE_PREAD 1
32453# define HAVE_PWRITE 1
32454#endif
32455#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
32456# undef USE_PREAD
32457# define USE_PREAD64 1
32458#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
32459# undef USE_PREAD64
32460# define USE_PREAD 1
32461#endif
32462
32463/*
32464** standard include files.
32465*/
32466#include <sys/types.h>
32467#include <sys/stat.h>
32468#include <fcntl.h>
32469#include <sys/ioctl.h>
32470#include <unistd.h>
32471/* #include <time.h> */
32472#include <sys/time.h>
32473#include <errno(*__errno_location ()).h>
32474#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0
32475# include <sys/mman.h>
32476#endif
32477
32478#if SQLITE_ENABLE_LOCKING_STYLE0
32479/* # include <sys/ioctl.h> */
32480# include <sys/file.h>
32481# include <sys/param.h>
32482#endif /* SQLITE_ENABLE_LOCKING_STYLE */
32483
32484#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
32485 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
32486# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
32487 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
32488# define HAVE_GETHOSTUUID 1
32489# else
32490# warning "gethostuuid() is disabled."
32491# endif
32492#endif
32493
32494
32495#if OS_VXWORKS0
32496/* # include <sys/ioctl.h> */
32497# include <semaphore.h>
32498# include <limits.h>
32499#endif /* OS_VXWORKS */
32500
32501#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
32502# include <sys/mount.h>
32503#endif
32504
32505#ifdef HAVE_UTIME
32506# include <utime.h>
32507#endif
32508
32509/*
32510** Allowed values of unixFile.fsFlags
32511*/
32512#define SQLITE_FSFLAGS_IS_MSDOS0x1 0x1
32513
32514/*
32515** If we are to be thread-safe, include the pthreads header.
32516*/
32517#if SQLITE_THREADSAFE1
32518/* # include <pthread.h> */
32519#endif
32520
32521/*
32522** Default permissions when creating a new file
32523*/
32524#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS0644
32525# define SQLITE_DEFAULT_FILE_PERMISSIONS0644 0644
32526#endif
32527
32528/*
32529** Default permissions when creating auto proxy dir
32530*/
32531#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755
32532# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755 0755
32533#endif
32534
32535/*
32536** Maximum supported path-length.
32537*/
32538#define MAX_PATHNAME512 512
32539
32540/*
32541** Maximum supported symbolic links
32542*/
32543#define SQLITE_MAX_SYMLINKS100 100
32544
32545/* Always cast the getpid() return type for compatibility with
32546** kernel modules in VxWorks. */
32547#define osGetpid(X)(pid_t)getpid() (pid_t)getpid()
32548
32549/*
32550** Only set the lastErrno if the error code is a real error and not
32551** a normal expected return code of SQLITE_BUSY or SQLITE_OK
32552*/
32553#define IS_LOCK_ERROR(x)((x != 0) && (x != 5)) ((x != SQLITE_OK0) && (x != SQLITE_BUSY5))
32554
32555/* Forward references */
32556typedef struct unixShm unixShm; /* Connection shared memory */
32557typedef struct unixShmNode unixShmNode; /* Shared memory instance */
32558typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
32559typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
32560
32561/*
32562** Sometimes, after a file handle is closed by SQLite, the file descriptor
32563** cannot be closed immediately. In these cases, instances of the following
32564** structure are used to store the file descriptor while waiting for an
32565** opportunity to either close or reuse it.
32566*/
32567struct UnixUnusedFd {
32568 int fd; /* File descriptor to close */
32569 int flags; /* Flags this file descriptor was opened with */
32570 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
32571};
32572
32573/*
32574** The unixFile structure is subclass of sqlite3_file specific to the unix
32575** VFS implementations.
32576*/
32577typedef struct unixFile unixFile;
32578struct unixFile {
32579 sqlite3_io_methods const *pMethod; /* Always the first entry */
32580 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
32581 unixInodeInfo *pInode; /* Info about locks on this inode */
32582 int h; /* The file descriptor */
32583 unsigned char eFileLock; /* The type of lock held on this fd */
32584 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
32585 int lastErrno; /* The unix errno from last I/O error */
32586 void *lockingContext; /* Locking style specific state */
32587 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
32588 const char *zPath; /* Name of the file */
32589 unixShm *pShm; /* Shared memory segment information */
32590 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
32591#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
32592 int nFetchOut; /* Number of outstanding xFetch refs */
32593 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
32594 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
32595 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
32596 void *pMapRegion; /* Memory mapped region */
32597#endif
32598 int sectorSize; /* Device sector size */
32599 int deviceCharacteristics; /* Precomputed device characteristics */
32600#if SQLITE_ENABLE_LOCKING_STYLE0
32601 int openFlags; /* The flags specified at open() */
32602#endif
32603#if SQLITE_ENABLE_LOCKING_STYLE0 || defined(__APPLE__)
32604 unsigned fsFlags; /* cached details from statfs() */
32605#endif
32606#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
32607 unsigned iBusyTimeout; /* Wait this many millisec on locks */
32608#endif
32609#if OS_VXWORKS0
32610 struct vxworksFileId *pId; /* Unique file ID */
32611#endif
32612#ifdef SQLITE_DEBUG
32613 /* The next group of variables are used to track whether or not the
32614 ** transaction counter in bytes 24-27 of database files are updated
32615 ** whenever any part of the database changes. An assertion fault will
32616 ** occur if a file is updated without also updating the transaction
32617 ** counter. This test is made to avoid new problems similar to the
32618 ** one described by ticket #3584.
32619 */
32620 unsigned char transCntrChng; /* True if the transaction counter changed */
32621 unsigned char dbUpdate; /* True if any part of database file changed */
32622 unsigned char inNormalWrite; /* True if in a normal write operation */
32623
32624#endif
32625
32626#ifdef SQLITE_TEST
32627 /* In test mode, increase the size of this structure a bit so that
32628 ** it is larger than the struct CrashFile defined in test6.c.
32629 */
32630 char aPadding[32];
32631#endif
32632};
32633
32634/* This variable holds the process id (pid) from when the xRandomness()
32635** method was called. If xOpen() is called from a different process id,
32636** indicating that a fork() has occurred, the PRNG will be reset.
32637*/
32638static pid_t randomnessPid = 0;
32639
32640/*
32641** Allowed values for the unixFile.ctrlFlags bitmask:
32642*/
32643#define UNIXFILE_EXCL0x01 0x01 /* Connections from one process only */
32644#define UNIXFILE_RDONLY0x02 0x02 /* Connection is read only */
32645#define UNIXFILE_PERSIST_WAL0x04 0x04 /* Persistent WAL mode */
32646#ifndef SQLITE_DISABLE_DIRSYNC
32647# define UNIXFILE_DIRSYNC0x08 0x08 /* Directory sync needed */
32648#else
32649# define UNIXFILE_DIRSYNC0x08 0x00
32650#endif
32651#define UNIXFILE_PSOW0x10 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
32652#define UNIXFILE_DELETE0x20 0x20 /* Delete on close */
32653#define UNIXFILE_URI0x40 0x40 /* Filename might have query parameters */
32654#define UNIXFILE_NOLOCK0x80 0x80 /* Do no file locking */
32655
32656/*
32657** Include code that is common to all os_*.c files
32658*/
32659/************** Include os_common.h in the middle of os_unix.c ***************/
32660/************** Begin file os_common.h ***************************************/
32661/*
32662** 2004 May 22
32663**
32664** The author disclaims copyright to this source code. In place of
32665** a legal notice, here is a blessing:
32666**
32667** May you do good and not evil.
32668** May you find forgiveness for yourself and forgive others.
32669** May you share freely, never taking more than you give.
32670**
32671******************************************************************************
32672**
32673** This file contains macros and a little bit of code that is common to
32674** all of the platform-specific files (os_*.c) and is #included into those
32675** files.
32676**
32677** This file should be #included by the os_*.c files only. It is not a
32678** general purpose header file.
32679*/
32680#ifndef _OS_COMMON_H_
32681#define _OS_COMMON_H_
32682
32683/*
32684** At least two bugs have slipped in because we changed the MEMORY_DEBUG
32685** macro to SQLITE_DEBUG and some older makefiles have not yet made the
32686** switch. The following code should catch this problem at compile-time.
32687*/
32688#ifdef MEMORY_DEBUG
32689# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
32690#endif
32691
32692/*
32693** Macros for performance tracing. Normally turned off. Only works
32694** on i486 hardware.
32695*/
32696#ifdef SQLITE_PERFORMANCE_TRACE
32697
32698/*
32699** hwtime.h contains inline assembler code for implementing
32700** high-performance timing routines.
32701*/
32702/************** Include hwtime.h in the middle of os_common.h ****************/
32703/************** Begin file hwtime.h ******************************************/
32704/*
32705** 2008 May 27
32706**
32707** The author disclaims copyright to this source code. In place of
32708** a legal notice, here is a blessing:
32709**
32710** May you do good and not evil.
32711** May you find forgiveness for yourself and forgive others.
32712** May you share freely, never taking more than you give.
32713**
32714******************************************************************************
32715**
32716** This file contains inline asm code for retrieving "high-performance"
32717** counters for x86 class CPUs.
32718*/
32719#ifndef SQLITE_HWTIME_H
32720#define SQLITE_HWTIME_H
32721
32722/*
32723** The following routine only works on pentium-class (or newer) processors.
32724** It uses the RDTSC opcode to read the cycle count value out of the
32725** processor and returns that value. This can be used for high-res
32726** profiling.
32727*/
32728#if (defined(__GNUC__4) || defined(_MSC_VER)) && \
32729 (defined(i386) || defined(__i386__) || defined(_M_IX86))
32730
32731 #if defined(__GNUC__4)
32732
32733 __inline__ sqlite_uint64 sqlite3Hwtime(void){
32734 unsigned int lo, hi;
32735 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
32736 return (sqlite_uint64)hi << 32 | lo;
32737 }
32738
32739 #elif defined(_MSC_VER)
32740
32741 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
32742 __asm {
32743 rdtsc
32744 ret ; return value at EDX:EAX
32745 }
32746 }
32747
32748 #endif
32749
32750#elif (defined(__GNUC__4) && defined(__x86_64__1))
32751
32752 __inline__ sqlite_uint64 sqlite3Hwtime(void){
32753 unsigned long val;
32754 __asm__ __volatile__ ("rdtsc" : "=A" (val));
32755 return val;
32756 }
32757
32758#elif (defined(__GNUC__4) && defined(__ppc__))
32759
32760 __inline__ sqlite_uint64 sqlite3Hwtime(void){
32761 unsigned long long retval;
32762 unsigned long junk;
32763 __asm__ __volatile__ ("\n\
32764 1: mftbu %1\n\
32765 mftb %L0\n\
32766 mftbu %0\n\
32767 cmpw %0,%1\n\
32768 bne 1b"
32769 : "=r" (retval), "=r" (junk));
32770 return retval;
32771 }
32772
32773#else
32774
32775 #error Need implementation of sqlite3Hwtime() for your platform.
32776
32777 /*
32778 ** To compile without implementing sqlite3Hwtime() for your platform,
32779 ** you can remove the above #error and use the following
32780 ** stub function. You will lose timing support for many
32781 ** of the debugging and testing utilities, but it should at
32782 ** least compile and run.
32783 */
32784SQLITE_PRIVATEstatic sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
32785
32786#endif
32787
32788#endif /* !defined(SQLITE_HWTIME_H) */
32789
32790/************** End of hwtime.h **********************************************/
32791/************** Continuing where we left off in os_common.h ******************/
32792
32793static sqlite_uint64 g_start;
32794static sqlite_uint64 g_elapsed;
32795#define TIMER_START g_start=sqlite3Hwtime()
32796#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
32797#define TIMER_ELAPSED((sqlite_uint64)0) g_elapsed
32798#else
32799#define TIMER_START
32800#define TIMER_END
32801#define TIMER_ELAPSED((sqlite_uint64)0) ((sqlite_uint64)0)
32802#endif
32803
32804/*
32805** If we compile with the SQLITE_TEST macro set, then the following block
32806** of code will give us the ability to simulate a disk I/O error. This
32807** is used for testing the I/O recovery logic.
32808*/
32809#if defined(SQLITE_TEST)
32810SQLITE_API extern int sqlite3_io_error_hit;
32811SQLITE_API extern int sqlite3_io_error_hardhit;
32812SQLITE_API extern int sqlite3_io_error_pending;
32813SQLITE_API extern int sqlite3_io_error_persist;
32814SQLITE_API extern int sqlite3_io_error_benign;
32815SQLITE_API extern int sqlite3_diskfull_pending;
32816SQLITE_API extern int sqlite3_diskfull;
32817#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
32818#define SimulateIOError(CODE) \
32819 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
32820 || sqlite3_io_error_pending-- == 1 ) \
32821 { local_ioerr(); CODE; }
32822static void local_ioerr(){
32823 IOTRACE(("IOERR\n"));
32824 sqlite3_io_error_hit++;
32825 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
32826}
32827#define SimulateDiskfullError(CODE) \
32828 if( sqlite3_diskfull_pending ){ \
32829 if( sqlite3_diskfull_pending == 1 ){ \
32830 local_ioerr(); \
32831 sqlite3_diskfull = 1; \
32832 sqlite3_io_error_hit = 1; \
32833 CODE; \
32834 }else{ \
32835 sqlite3_diskfull_pending--; \
32836 } \
32837 }
32838#else
32839#define SimulateIOErrorBenign(X)
32840#define SimulateIOError(A)
32841#define SimulateDiskfullError(A)
32842#endif /* defined(SQLITE_TEST) */
32843
32844/*
32845** When testing, keep a count of the number of open files.
32846*/
32847#if defined(SQLITE_TEST)
32848SQLITE_API extern int sqlite3_open_file_count;
32849#define OpenCounter(X) sqlite3_open_file_count+=(X)
32850#else
32851#define OpenCounter(X)
32852#endif /* defined(SQLITE_TEST) */
32853
32854#endif /* !defined(_OS_COMMON_H_) */
32855
32856/************** End of os_common.h *******************************************/
32857/************** Continuing where we left off in os_unix.c ********************/
32858
32859/*
32860** Define various macros that are missing from some systems.
32861*/
32862#ifndef O_LARGEFILE0
32863# define O_LARGEFILE0 0
32864#endif
32865#ifdef SQLITE_DISABLE_LFS
32866# undef O_LARGEFILE0
32867# define O_LARGEFILE0 0
32868#endif
32869#ifndef O_NOFOLLOW0400000
32870# define O_NOFOLLOW0400000 0
32871#endif
32872#ifndef O_BINARY0
32873# define O_BINARY0 0
32874#endif
32875
32876/*
32877** The threadid macro resolves to the thread-id or to 0. Used for
32878** testing and debugging only.
32879*/
32880#if SQLITE_THREADSAFE1
32881#define threadidpthread_self() pthread_self()
32882#else
32883#define threadidpthread_self() 0
32884#endif
32885
32886/*
32887** HAVE_MREMAP defaults to true on Linux and false everywhere else.
32888*/
32889#if !defined(HAVE_MREMAP1)
32890# if defined(__linux__1) && defined(_GNU_SOURCE)
32891# define HAVE_MREMAP1 1
32892# else
32893# define HAVE_MREMAP1 0
32894# endif
32895#endif
32896
32897/*
32898** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
32899** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
32900*/
32901#ifdef __ANDROID__
32902# define lseek lseek64
32903#endif
32904
32905#ifdef __linux__1
32906/*
32907** Linux-specific IOCTL magic numbers used for controlling F2FS
32908*/
32909#define F2FS_IOCTL_MAGIC0xf5 0xf5
32910#define F2FS_IOC_START_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((1)) << 0) | ((0) << ((0 +8)+8)))
_IO(F2FS_IOCTL_MAGIC, 1)(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((1)) << 0) | ((0) << ((0 +8)+8)))
32911#define F2FS_IOC_COMMIT_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((2)) << 0) | ((0) << ((0 +8)+8)))
_IO(F2FS_IOCTL_MAGIC, 2)(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((2)) << 0) | ((0) << ((0 +8)+8)))
32912#define F2FS_IOC_START_VOLATILE_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((3)) << 0) | ((0) << ((0 +8)+8)))
_IO(F2FS_IOCTL_MAGIC, 3)(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((3)) << 0) | ((0) << ((0 +8)+8)))
32913#define F2FS_IOC_ABORT_VOLATILE_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((5)) << 0) | ((0) << ((0 +8)+8)))
_IO(F2FS_IOCTL_MAGIC, 5)(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((5)) << 0) | ((0) << ((0 +8)+8)))
32914#define F2FS_IOC_GET_FEATURES(((2U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((12)) << 0) | ((((sizeof(u32)))) << ((0 +8)+
8)))
_IOR(F2FS_IOCTL_MAGIC, 12, u32)(((2U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((12)) << 0) | ((((sizeof(u32)))) << ((0 +8)+
8)))
32915#define F2FS_FEATURE_ATOMIC_WRITE0x0004 0x0004
32916#endif /* __linux__ */
32917
32918
32919/*
32920** Different Unix systems declare open() in different ways. Same use
32921** open(const char*,int,mode_t). Others use open(const char*,int,...).
32922** The difference is important when using a pointer to the function.
32923**
32924** The safest way to deal with the problem is to always use this wrapper
32925** which always has the same well-defined interface.
32926*/
32927static int posixOpen(const char *zFile, int flags, int mode){
32928 return open(zFile, flags, mode);
32929}
32930
32931/* Forward reference */
32932static int openDirectory(const char*, int*);
32933static int unixGetpagesize(void);
32934
32935/*
32936** Many system calls are accessed through pointer-to-functions so that
32937** they may be overridden at runtime to facilitate fault injection during
32938** testing and sandboxing. The following array holds the names and pointers
32939** to all overrideable system calls.
32940*/
32941static struct unix_syscall {
32942 const char *zName; /* Name of the system call */
32943 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
32944 sqlite3_syscall_ptr pDefault; /* Default value */
32945} aSyscall[] = {
32946 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
32947#define osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent) ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
32948
32949 { "close", (sqlite3_syscall_ptr)close, 0 },
32950#define osClose((int(*)(int))aSyscall[1].pCurrent) ((int(*)(int))aSyscall[1].pCurrent)
32951
32952 { "access", (sqlite3_syscall_ptr)access, 0 },
32953#define osAccess((int(*)(const char*,int))aSyscall[2].pCurrent) ((int(*)(const char*,int))aSyscall[2].pCurrent)
32954
32955 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
32956#define osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent) ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
32957
32958 { "stat", (sqlite3_syscall_ptr)stat, 0 },
32959#define osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
32960
32961/*
32962** The DJGPP compiler environment looks mostly like Unix, but it
32963** lacks the fcntl() system call. So redefine fcntl() to be something
32964** that always succeeds. This means that locking does not occur under
32965** DJGPP. But it is DOS - what did you expect?
32966*/
32967#ifdef __DJGPP__
32968 { "fstat", 0, 0 },
32969#define osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(a,b,c) 0
32970#else
32971 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
32972#define osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent) ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
32973#endif
32974
32975 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
32976#define osFtruncate((int(*)(int,off_t))aSyscall[6].pCurrent) ((int(*)(int,off_t))aSyscall[6].pCurrent)
32977
32978 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
32979#define osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent) ((int(*)(int,int,...))aSyscall[7].pCurrent)
32980
32981 { "read", (sqlite3_syscall_ptr)read, 0 },
32982#define osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
32983
32984#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE0
32985 { "pread", (sqlite3_syscall_ptr)pread, 0 },
32986#else
32987 { "pread", (sqlite3_syscall_ptr)0, 0 },
32988#endif
32989#define osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
32990
32991#if defined(USE_PREAD64)
32992 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
32993#else
32994 { "pread64", (sqlite3_syscall_ptr)0, 0 },
32995#endif
32996#define osPread64((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
32997
32998 { "write", (sqlite3_syscall_ptr)write, 0 },
32999#define osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
33000
33001#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE0
33002 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
33003#else
33004 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
33005#endif
33006#define osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
((ssize_t(*)(int,const void*,size_t,off_t))\
33007 aSyscall[12].pCurrent)
33008
33009#if defined(USE_PREAD64)
33010 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
33011#else
33012 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
33013#endif
33014#define osPwrite64((ssize_t(*)(int,const void*,size_t,off64_t)) aSyscall[13].pCurrent
)
((ssize_t(*)(int,const void*,size_t,off64_t))\
33015 aSyscall[13].pCurrent)
33016
33017 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
33018#define osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent) ((int(*)(int,mode_t))aSyscall[14].pCurrent)
33019
33020#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
33021 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
33022#else
33023 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
33024#endif
33025#define osFallocate((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
33026
33027 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
33028#define osUnlink((int(*)(const char*))aSyscall[16].pCurrent) ((int(*)(const char*))aSyscall[16].pCurrent)
33029
33030 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
33031#define osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent) ((int(*)(const char*,int*))aSyscall[17].pCurrent)
33032
33033 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
33034#define osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent) ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
33035
33036 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
33037#define osRmdir((int(*)(const char*))aSyscall[19].pCurrent) ((int(*)(const char*))aSyscall[19].pCurrent)
33038
33039#if defined(HAVE_FCHOWN1)
33040 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
33041#else
33042 { "fchown", (sqlite3_syscall_ptr)0, 0 },
33043#endif
33044#define osFchown((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
33045
33046#if defined(HAVE_FCHOWN1)
33047 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
33048#else
33049 { "geteuid", (sqlite3_syscall_ptr)0, 0 },
33050#endif
33051#define osGeteuid((uid_t(*)(void))aSyscall[21].pCurrent) ((uid_t(*)(void))aSyscall[21].pCurrent)
33052
33053#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0
33054 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
33055#else
33056 { "mmap", (sqlite3_syscall_ptr)0, 0 },
33057#endif
33058#define osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
33059
33060#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0
33061 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
33062#else
33063 { "munmap", (sqlite3_syscall_ptr)0, 0 },
33064#endif
33065#define osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent) ((int(*)(void*,size_t))aSyscall[23].pCurrent)
33066
33067#if HAVE_MREMAP1 && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0)
33068 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
33069#else
33070 { "mremap", (sqlite3_syscall_ptr)0, 0 },
33071#endif
33072#define osMremap((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent
)
((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
33073
33074#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0
33075 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
33076#else
33077 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
33078#endif
33079#define osGetpagesize((int(*)(void))aSyscall[25].pCurrent) ((int(*)(void))aSyscall[25].pCurrent)
33080
33081#if defined(HAVE_READLINK1)
33082 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
33083#else
33084 { "readlink", (sqlite3_syscall_ptr)0, 0 },
33085#endif
33086#define osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
33087
33088#if defined(HAVE_LSTAT1)
33089 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
33090#else
33091 { "lstat", (sqlite3_syscall_ptr)0, 0 },
33092#endif
33093#define osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
33094
33095#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
33096# ifdef __ANDROID__
33097 { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
33098# else
33099 { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 },
33100# endif
33101#else
33102 { "ioctl", (sqlite3_syscall_ptr)0, 0 },
33103#endif
33104#define osIoctl((int(*)(int,int,...))aSyscall[28].pCurrent) ((int(*)(int,int,...))aSyscall[28].pCurrent)
33105
33106}; /* End of the overrideable system calls */
33107
33108
33109/*
33110** On some systems, calls to fchown() will trigger a message in a security
33111** log if they come from non-root processes. So avoid calling fchown() if
33112** we are not running as root.
33113*/
33114static int robustFchown(int fd, uid_t uid, gid_t gid){
33115#if defined(HAVE_FCHOWN1)
33116 return osGeteuid((uid_t(*)(void))aSyscall[21].pCurrent)() ? 0 : osFchown((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)(fd,uid,gid);
33117#else
33118 return 0;
33119#endif
33120}
33121
33122/*
33123** This is the xSetSystemCall() method of sqlite3_vfs for all of the
33124** "unix" VFSes. Return SQLITE_OK opon successfully updating the
33125** system call pointer, or SQLITE_NOTFOUND if there is no configurable
33126** system call named zName.
33127*/
33128static int unixSetSystemCall(
33129 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
33130 const char *zName, /* Name of system call to override */
33131 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
33132){
33133 unsigned int i;
33134 int rc = SQLITE_NOTFOUND12;
33135
33136 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
33137 if( zName==0 ){
33138 /* If no zName is given, restore all system calls to their default
33139 ** settings and return NULL
33140 */
33141 rc = SQLITE_OK0;
33142 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
33143 if( aSyscall[i].pDefault ){
33144 aSyscall[i].pCurrent = aSyscall[i].pDefault;
33145 }
33146 }
33147 }else{
33148 /* If zName is specified, operate on only the one system call
33149 ** specified.
33150 */
33151 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
33152 if( strcmp(zName, aSyscall[i].zName)==0 ){
33153 if( aSyscall[i].pDefault==0 ){
33154 aSyscall[i].pDefault = aSyscall[i].pCurrent;
33155 }
33156 rc = SQLITE_OK0;
33157 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
33158 aSyscall[i].pCurrent = pNewFunc;
33159 break;
33160 }
33161 }
33162 }
33163 return rc;
33164}
33165
33166/*
33167** Return the value of a system call. Return NULL if zName is not a
33168** recognized system call name. NULL is also returned if the system call
33169** is currently undefined.
33170*/
33171static sqlite3_syscall_ptr unixGetSystemCall(
33172 sqlite3_vfs *pNotUsed,
33173 const char *zName
33174){
33175 unsigned int i;
33176
33177 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
33178 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
33179 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
33180 }
33181 return 0;
33182}
33183
33184/*
33185** Return the name of the first system call after zName. If zName==NULL
33186** then return the name of the first system call. Return NULL if zName
33187** is the last system call or if zName is not the name of a valid
33188** system call.
33189*/
33190static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
33191 int i = -1;
33192
33193 UNUSED_PARAMETER(p)(void)(p);
33194 if( zName ){
33195 for(i=0; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0])))-1; i++){
33196 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
33197 }
33198 }
33199 for(i++; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0]))); i++){
33200 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
33201 }
33202 return 0;
33203}
33204
33205/*
33206** Do not accept any file descriptor less than this value, in order to avoid
33207** opening database file using file descriptors that are commonly used for
33208** standard input, output, and error.
33209*/
33210#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR3
33211# define SQLITE_MINIMUM_FILE_DESCRIPTOR3 3
33212#endif
33213
33214/*
33215** Invoke open(). Do so multiple times, until it either succeeds or
33216** fails for some reason other than EINTR.
33217**
33218** If the file creation mode "m" is 0 then set it to the default for
33219** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
33220** 0644) as modified by the system umask. If m is not 0, then
33221** make the file creation mode be exactly m ignoring the umask.
33222**
33223** The m parameter will be non-zero only when creating -wal, -journal,
33224** and -shm files. We want those files to have *exactly* the same
33225** permissions as their original database, unadulterated by the umask.
33226** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
33227** transaction crashes and leaves behind hot journals, then any
33228** process that is able to write to the database will also be able to
33229** recover the hot journals.
33230*/
33231static int robust_open(const char *z, int f, mode_t m){
33232 int fd;
33233 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS0644;
33234 while(1){
33235#if defined(O_CLOEXEC02000000)
33236 fd = osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)(z,f|O_CLOEXEC02000000,m2);
33237#else
33238 fd = osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)(z,f,m2);
33239#endif
33240 if( fd<0 ){
33241 if( errno(*__errno_location ())==EINTR4 ) continue;
33242 break;
33243 }
33244 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR3 ) break;
33245 osClose((int(*)(int))aSyscall[1].pCurrent)(fd);
33246 sqlite3_log(SQLITE_WARNING28,
33247 "attempt to open \"%s\" as file descriptor %d", z, fd);
33248 fd = -1;
33249 if( osOpen((int(*)(const char*,int,int))aSyscall[0].pCurrent)("/dev/null", f, m)<0 ) break;
33250 }
33251 if( fd>=0 ){
33252 if( m!=0 ){
33253 struct stat statbuf;
33254 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf)==0
33255 && statbuf.st_size==0
33256 && (statbuf.st_mode&0777)!=m
33257 ){
33258 osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(fd, m);
33259 }
33260 }
33261#if defined(FD_CLOEXEC1) && (!defined(O_CLOEXEC02000000) || O_CLOEXEC02000000==0)
33262 osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_SETFD2, osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_GETFD1, 0) | FD_CLOEXEC1);
33263#endif
33264 }
33265 return fd;
33266}
33267
33268/*
33269** Helper functions to obtain and relinquish the global mutex. The
33270** global mutex is used to protect the unixInodeInfo and
33271** vxworksFileId objects used by this file, all of which may be
33272** shared by multiple threads.
33273**
33274** Function unixMutexHeld() is used to assert() that the global mutex
33275** is held when required. This function is only used as part of assert()
33276** statements. e.g.
33277**
33278** unixEnterMutex()
33279** assert( unixMutexHeld() );
33280** unixEnterLeave()
33281**
33282** To prevent deadlock, the global unixBigLock must must be acquired
33283** before the unixInodeInfo.pLockMutex mutex, if both are held. It is
33284** OK to get the pLockMutex without holding unixBigLock first, but if
33285** that happens, the unixBigLock mutex must not be acquired until after
33286** pLockMutex is released.
33287**
33288** OK: enter(unixBigLock), enter(pLockInfo)
33289** OK: enter(unixBigLock)
33290** OK: enter(pLockInfo)
33291** ERROR: enter(pLockInfo), enter(unixBigLock)
33292*/
33293static sqlite3_mutex *unixBigLock = 0;
33294static void unixEnterMutex(void){
33295 assert( sqlite3_mutex_notheld(unixBigLock) )((void) (0)); /* Not a recursive mutex */
33296 sqlite3_mutex_enter(unixBigLock);
33297}
33298static void unixLeaveMutex(void){
33299 assert( sqlite3_mutex_held(unixBigLock) )((void) (0));
33300 sqlite3_mutex_leave(unixBigLock);
33301}
33302#ifdef SQLITE_DEBUG
33303static int unixMutexHeld(void) {
33304 return sqlite3_mutex_held(unixBigLock);
33305}
33306#endif
33307
33308
33309#ifdef SQLITE_HAVE_OS_TRACE
33310/*
33311** Helper function for printing out trace information from debugging
33312** binaries. This returns the string representation of the supplied
33313** integer lock-type.
33314*/
33315static const char *azFileLock(int eFileLock){
33316 switch( eFileLock ){
33317 case NO_LOCK0: return "NONE";
33318 case SHARED_LOCK1: return "SHARED";
33319 case RESERVED_LOCK2: return "RESERVED";
33320 case PENDING_LOCK3: return "PENDING";
33321 case EXCLUSIVE_LOCK4: return "EXCLUSIVE";
33322 }
33323 return "ERROR";
33324}
33325#endif
33326
33327#ifdef SQLITE_LOCK_TRACE
33328/*
33329** Print out information about all locking operations.
33330**
33331** This routine is used for troubleshooting locks on multithreaded
33332** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
33333** command-line option on the compiler. This code is normally
33334** turned off.
33335*/
33336static int lockTrace(int fd, int op, struct flock *p){
33337 char *zOpName, *zType;
33338 int s;
33339 int savedErrno;
33340 if( op==F_GETLK5 ){
33341 zOpName = "GETLK";
33342 }else if( op==F_SETLK6 ){
33343 zOpName = "SETLK";
33344 }else{
33345 s = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, op, p);
33346 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
33347 return s;
33348 }
33349 if( p->l_type==F_RDLCK0 ){
33350 zType = "RDLCK";
33351 }else if( p->l_type==F_WRLCK1 ){
33352 zType = "WRLCK";
33353 }else if( p->l_type==F_UNLCK2 ){
33354 zType = "UNLCK";
33355 }else{
33356 assert( 0 )((void) (0));
33357 }
33358 assert( p->l_whence==SEEK_SET )((void) (0));
33359 s = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, op, p);
33360 savedErrno = errno(*__errno_location ());
33361 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
33362 threadidpthread_self(), fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
33363 (int)p->l_pid, s);
33364 if( s==(-1) && op==F_SETLK6 && (p->l_type==F_RDLCK0 || p->l_type==F_WRLCK1) ){
33365 struct flock l2;
33366 l2 = *p;
33367 osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_GETLK5, &l2);
33368 if( l2.l_type==F_RDLCK0 ){
33369 zType = "RDLCK";
33370 }else if( l2.l_type==F_WRLCK1 ){
33371 zType = "WRLCK";
33372 }else if( l2.l_type==F_UNLCK2 ){
33373 zType = "UNLCK";
33374 }else{
33375 assert( 0 )((void) (0));
33376 }
33377 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
33378 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
33379 }
33380 errno(*__errno_location ()) = savedErrno;
33381 return s;
33382}
33383#undef osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)
33384#define osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent) lockTrace
33385#endif /* SQLITE_LOCK_TRACE */
33386
33387/*
33388** Retry ftruncate() calls that fail due to EINTR
33389**
33390** All calls to ftruncate() within this file should be made through
33391** this wrapper. On the Android platform, bypassing the logic below
33392** could lead to a corrupt database.
33393*/
33394static int robust_ftruncate(int h, sqlite3_int64 sz){
33395 int rc;
33396#ifdef __ANDROID__
33397 /* On Android, ftruncate() always uses 32-bit offsets, even if
33398 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
33399 ** truncate a file to any size larger than 2GiB. Silently ignore any
33400 ** such attempts. */
33401 if( sz>(sqlite3_int64)0x7FFFFFFF ){
33402 rc = SQLITE_OK0;
33403 }else
33404#endif
33405 do{ rc = osFtruncate((int(*)(int,off_t))aSyscall[6].pCurrent)(h,sz); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
33406 return rc;
33407}
33408
33409/*
33410** This routine translates a standard POSIX errno code into something
33411** useful to the clients of the sqlite3 functions. Specifically, it is
33412** intended to translate a variety of "try again" errors into SQLITE_BUSY
33413** and a variety of "please close the file descriptor NOW" errors into
33414** SQLITE_IOERR
33415**
33416** Errors during initialization of locks, or file system support for locks,
33417** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
33418*/
33419static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
33420 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||((void) (0))
33421 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||((void) (0))
33422 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||((void) (0))
33423 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) )((void) (0));
33424 switch (posixError) {
33425 case EACCES13:
33426 case EAGAIN11:
33427 case ETIMEDOUT110:
33428 case EBUSY16:
33429 case EINTR4:
33430 case ENOLCK37:
33431 /* random NFS retry error, unless during file system support
33432 * introspection, in which it actually means what it says */
33433 return SQLITE_BUSY5;
33434
33435 case EPERM1:
33436 return SQLITE_PERM3;
33437
33438 default:
33439 return sqliteIOErr;
33440 }
33441}
33442
33443
33444/******************************************************************************
33445****************** Begin Unique File ID Utility Used By VxWorks ***************
33446**
33447** On most versions of unix, we can get a unique ID for a file by concatenating
33448** the device number and the inode number. But this does not work on VxWorks.
33449** On VxWorks, a unique file id must be based on the canonical filename.
33450**
33451** A pointer to an instance of the following structure can be used as a
33452** unique file ID in VxWorks. Each instance of this structure contains
33453** a copy of the canonical filename. There is also a reference count.
33454** The structure is reclaimed when the number of pointers to it drops to
33455** zero.
33456**
33457** There are never very many files open at one time and lookups are not
33458** a performance-critical path, so it is sufficient to put these
33459** structures on a linked list.
33460*/
33461struct vxworksFileId {
33462 struct vxworksFileId *pNext; /* Next in a list of them all */
33463 int nRef; /* Number of references to this one */
33464 int nName; /* Length of the zCanonicalName[] string */
33465 char *zCanonicalName; /* Canonical filename */
33466};
33467
33468#if OS_VXWORKS0
33469/*
33470** All unique filenames are held on a linked list headed by this
33471** variable:
33472*/
33473static struct vxworksFileId *vxworksFileList = 0;
33474
33475/*
33476** Simplify a filename into its canonical form
33477** by making the following changes:
33478**
33479** * removing any trailing and duplicate /
33480** * convert /./ into just /
33481** * convert /A/../ where A is any simple name into just /
33482**
33483** Changes are made in-place. Return the new name length.
33484**
33485** The original filename is in z[0..n-1]. Return the number of
33486** characters in the simplified name.
33487*/
33488static int vxworksSimplifyName(char *z, int n){
33489 int i, j;
33490 while( n>1 && z[n-1]=='/' ){ n--; }
33491 for(i=j=0; i<n; i++){
33492 if( z[i]=='/' ){
33493 if( z[i+1]=='/' ) continue;
33494 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
33495 i += 1;
33496 continue;
33497 }
33498 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
33499 while( j>0 && z[j-1]!='/' ){ j--; }
33500 if( j>0 ){ j--; }
33501 i += 2;
33502 continue;
33503 }
33504 }
33505 z[j++] = z[i];
33506 }
33507 z[j] = 0;
33508 return j;
33509}
33510
33511/*
33512** Find a unique file ID for the given absolute pathname. Return
33513** a pointer to the vxworksFileId object. This pointer is the unique
33514** file ID.
33515**
33516** The nRef field of the vxworksFileId object is incremented before
33517** the object is returned. A new vxworksFileId object is created
33518** and added to the global list if necessary.
33519**
33520** If a memory allocation error occurs, return NULL.
33521*/
33522static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
33523 struct vxworksFileId *pNew; /* search key and new file ID */
33524 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
33525 int n; /* Length of zAbsoluteName string */
33526
33527 assert( zAbsoluteName[0]=='/' )((void) (0));
33528 n = (int)strlen(zAbsoluteName);
33529 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
33530 if( pNew==0 ) return 0;
33531 pNew->zCanonicalName = (char*)&pNew[1];
33532 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
33533 n = vxworksSimplifyName(pNew->zCanonicalName, n);
33534
33535 /* Search for an existing entry that matching the canonical name.
33536 ** If found, increment the reference count and return a pointer to
33537 ** the existing file ID.
33538 */
33539 unixEnterMutex();
33540 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
33541 if( pCandidate->nName==n
33542 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
33543 ){
33544 sqlite3_free(pNew);
33545 pCandidate->nRef++;
33546 unixLeaveMutex();
33547 return pCandidate;
33548 }
33549 }
33550
33551 /* No match was found. We will make a new file ID */
33552 pNew->nRef = 1;
33553 pNew->nName = n;
33554 pNew->pNext = vxworksFileList;
33555 vxworksFileList = pNew;
33556 unixLeaveMutex();
33557 return pNew;
33558}
33559
33560/*
33561** Decrement the reference count on a vxworksFileId object. Free
33562** the object when the reference count reaches zero.
33563*/
33564static void vxworksReleaseFileId(struct vxworksFileId *pId){
33565 unixEnterMutex();
33566 assert( pId->nRef>0 )((void) (0));
33567 pId->nRef--;
33568 if( pId->nRef==0 ){
33569 struct vxworksFileId **pp;
33570 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
33571 assert( *pp==pId )((void) (0));
33572 *pp = pId->pNext;
33573 sqlite3_free(pId);
33574 }
33575 unixLeaveMutex();
33576}
33577#endif /* OS_VXWORKS */
33578/*************** End of Unique File ID Utility Used By VxWorks ****************
33579******************************************************************************/
33580
33581
33582/******************************************************************************
33583*************************** Posix Advisory Locking ****************************
33584**
33585** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
33586** section 6.5.2.2 lines 483 through 490 specify that when a process
33587** sets or clears a lock, that operation overrides any prior locks set
33588** by the same process. It does not explicitly say so, but this implies
33589** that it overrides locks set by the same process using a different
33590** file descriptor. Consider this test case:
33591**
33592** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
33593** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
33594**
33595** Suppose ./file1 and ./file2 are really the same file (because
33596** one is a hard or symbolic link to the other) then if you set
33597** an exclusive lock on fd1, then try to get an exclusive lock
33598** on fd2, it works. I would have expected the second lock to
33599** fail since there was already a lock on the file due to fd1.
33600** But not so. Since both locks came from the same process, the
33601** second overrides the first, even though they were on different
33602** file descriptors opened on different file names.
33603**
33604** This means that we cannot use POSIX locks to synchronize file access
33605** among competing threads of the same process. POSIX locks will work fine
33606** to synchronize access for threads in separate processes, but not
33607** threads within the same process.
33608**
33609** To work around the problem, SQLite has to manage file locks internally
33610** on its own. Whenever a new database is opened, we have to find the
33611** specific inode of the database file (the inode is determined by the
33612** st_dev and st_ino fields of the stat structure that fstat() fills in)
33613** and check for locks already existing on that inode. When locks are
33614** created or removed, we have to look at our own internal record of the
33615** locks to see if another thread has previously set a lock on that same
33616** inode.
33617**
33618** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
33619** For VxWorks, we have to use the alternative unique ID system based on
33620** canonical filename and implemented in the previous division.)
33621**
33622** The sqlite3_file structure for POSIX is no longer just an integer file
33623** descriptor. It is now a structure that holds the integer file
33624** descriptor and a pointer to a structure that describes the internal
33625** locks on the corresponding inode. There is one locking structure
33626** per inode, so if the same inode is opened twice, both unixFile structures
33627** point to the same locking structure. The locking structure keeps
33628** a reference count (so we will know when to delete it) and a "cnt"
33629** field that tells us its internal lock status. cnt==0 means the
33630** file is unlocked. cnt==-1 means the file has an exclusive lock.
33631** cnt>0 means there are cnt shared locks on the file.
33632**
33633** Any attempt to lock or unlock a file first checks the locking
33634** structure. The fcntl() system call is only invoked to set a
33635** POSIX lock if the internal lock structure transitions between
33636** a locked and an unlocked state.
33637**
33638** But wait: there are yet more problems with POSIX advisory locks.
33639**
33640** If you close a file descriptor that points to a file that has locks,
33641** all locks on that file that are owned by the current process are
33642** released. To work around this problem, each unixInodeInfo object
33643** maintains a count of the number of pending locks on tha inode.
33644** When an attempt is made to close an unixFile, if there are
33645** other unixFile open on the same inode that are holding locks, the call
33646** to close() the file descriptor is deferred until all of the locks clear.
33647** The unixInodeInfo structure keeps a list of file descriptors that need to
33648** be closed and that list is walked (and cleared) when the last lock
33649** clears.
33650**
33651** Yet another problem: LinuxThreads do not play well with posix locks.
33652**
33653** Many older versions of linux use the LinuxThreads library which is
33654** not posix compliant. Under LinuxThreads, a lock created by thread
33655** A cannot be modified or overridden by a different thread B.
33656** Only thread A can modify the lock. Locking behavior is correct
33657** if the appliation uses the newer Native Posix Thread Library (NPTL)
33658** on linux - with NPTL a lock created by thread A can override locks
33659** in thread B. But there is no way to know at compile-time which
33660** threading library is being used. So there is no way to know at
33661** compile-time whether or not thread A can override locks on thread B.
33662** One has to do a run-time check to discover the behavior of the
33663** current process.
33664**
33665** SQLite used to support LinuxThreads. But support for LinuxThreads
33666** was dropped beginning with version 3.7.0. SQLite will still work with
33667** LinuxThreads provided that (1) there is no more than one connection
33668** per database file in the same process and (2) database connections
33669** do not move across threads.
33670*/
33671
33672/*
33673** An instance of the following structure serves as the key used
33674** to locate a particular unixInodeInfo object.
33675*/
33676struct unixFileId {
33677 dev_t dev; /* Device number */
33678#if OS_VXWORKS0
33679 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
33680#else
33681 /* We are told that some versions of Android contain a bug that
33682 ** sizes ino_t at only 32-bits instead of 64-bits. (See
33683 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
33684 ** To work around this, always allocate 64-bits for the inode number.
33685 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
33686 ** but that should not be a big deal. */
33687 /* WAS: ino_t ino; */
33688 u64 ino; /* Inode number */
33689#endif
33690};
33691
33692/*
33693** An instance of the following structure is allocated for each open
33694** inode.
33695**
33696** A single inode can have multiple file descriptors, so each unixFile
33697** structure contains a pointer to an instance of this object and this
33698** object keeps a count of the number of unixFile pointing to it.
33699**
33700** Mutex rules:
33701**
33702** (1) Only the pLockMutex mutex must be held in order to read or write
33703** any of the locking fields:
33704** nShared, nLock, eFileLock, bProcessLock, pUnused
33705**
33706** (2) When nRef>0, then the following fields are unchanging and can
33707** be read (but not written) without holding any mutex:
33708** fileId, pLockMutex
33709**
33710** (3) With the exceptions above, all the fields may only be read
33711** or written while holding the global unixBigLock mutex.
33712**
33713** Deadlock prevention: The global unixBigLock mutex may not
33714** be acquired while holding the pLockMutex mutex. If both unixBigLock
33715** and pLockMutex are needed, then unixBigLock must be acquired first.
33716*/
33717struct unixInodeInfo {
33718 struct unixFileId fileId; /* The lookup key */
33719 sqlite3_mutex *pLockMutex; /* Hold this mutex for... */
33720 int nShared; /* Number of SHARED locks held */
33721 int nLock; /* Number of outstanding file locks */
33722 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
33723 unsigned char bProcessLock; /* An exclusive process lock is held */
33724 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
33725 int nRef; /* Number of pointers to this structure */
33726 unixShmNode *pShmNode; /* Shared memory associated with this inode */
33727 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
33728 unixInodeInfo *pPrev; /* .... doubly linked */
33729#if SQLITE_ENABLE_LOCKING_STYLE0
33730 unsigned long long sharedByte; /* for AFP simulated shared lock */
33731#endif
33732#if OS_VXWORKS0
33733 sem_t *pSem; /* Named POSIX semaphore */
33734 char aSemName[MAX_PATHNAME512+2]; /* Name of that semaphore */
33735#endif
33736};
33737
33738/*
33739** A lists of all unixInodeInfo objects.
33740**
33741** Must hold unixBigLock in order to read or write this variable.
33742*/
33743static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */
33744
33745#ifdef SQLITE_DEBUG
33746/*
33747** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not.
33748** This routine is used only within assert() to help verify correct mutex
33749** usage.
33750*/
33751int unixFileMutexHeld(unixFile *pFile){
33752 assert( pFile->pInode )((void) (0));
33753 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
33754}
33755int unixFileMutexNotheld(unixFile *pFile){
33756 assert( pFile->pInode )((void) (0));
33757 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
33758}
33759#endif
33760
33761/*
33762**
33763** This function - unixLogErrorAtLine(), is only ever called via the macro
33764** unixLogError().
33765**
33766** It is invoked after an error occurs in an OS function and errno has been
33767** set. It logs a message using sqlite3_log() containing the current value of
33768** errno and, if possible, the human-readable equivalent from strerror() or
33769** strerror_r().
33770**
33771** The first argument passed to the macro should be the error code that
33772** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
33773** The two subsequent arguments should be the name of the OS function that
33774** failed (e.g. "unlink", "open") and the associated file-system path,
33775** if any.
33776*/
33777#define unixLogError(a,b,c)unixLogErrorAtLine(a,b,c,33777) unixLogErrorAtLine(a,b,c,__LINE__33777)
33778static int unixLogErrorAtLine(
33779 int errcode, /* SQLite error code */
33780 const char *zFunc, /* Name of OS function that failed */
33781 const char *zPath, /* File path associated with error */
33782 int iLine /* Source line number where error occurred */
33783){
33784 char *zErr; /* Message from strerror() or equivalent */
33785 int iErrno = errno(*__errno_location ()); /* Saved syscall error number */
33786
33787 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
33788 ** the strerror() function to obtain the human-readable error message
33789 ** equivalent to errno. Otherwise, use strerror_r().
33790 */
33791#if SQLITE_THREADSAFE1 && defined(HAVE_STRERROR_R)
33792 char aErr[80];
33793 memset(aErr, 0, sizeof(aErr));
33794 zErr = aErr;
33795
33796 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
33797 ** assume that the system provides the GNU version of strerror_r() that
33798 ** returns a pointer to a buffer containing the error message. That pointer
33799 ** may point to aErr[], or it may point to some static storage somewhere.
33800 ** Otherwise, assume that the system provides the POSIX version of
33801 ** strerror_r(), which always writes an error message into aErr[].
33802 **
33803 ** If the code incorrectly assumes that it is the POSIX version that is
33804 ** available, the error message will often be an empty string. Not a
33805 ** huge problem. Incorrectly concluding that the GNU version is available
33806 ** could lead to a segfault though.
33807 */
33808#if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU1)
33809 zErr =
33810# endif
33811 strerror_r(iErrno, aErr, sizeof(aErr)-1);
33812
33813#elif SQLITE_THREADSAFE1
33814 /* This is a threadsafe build, but strerror_r() is not available. */
33815 zErr = "";
33816#else
33817 /* Non-threadsafe build, use strerror(). */
33818 zErr = strerror(iErrno);
33819#endif
33820
33821 if( zPath==0 ) zPath = "";
33822 sqlite3_log(errcode,
33823 "os_unix.c:%d: (%d) %s(%s) - %s",
33824 iLine, iErrno, zFunc, zPath, zErr
33825 );
33826
33827 return errcode;
33828}
33829
33830/*
33831** Close a file descriptor.
33832**
33833** We assume that close() almost always works, since it is only in a
33834** very sick application or on a very sick platform that it might fail.
33835** If it does fail, simply leak the file descriptor, but do log the
33836** error.
33837**
33838** Note that it is not safe to retry close() after EINTR since the
33839** file descriptor might have already been reused by another thread.
33840** So we don't even try to recover from an EINTR. Just log the error
33841** and move on.
33842*/
33843static void robust_close(unixFile *pFile, int h, int lineno){
33844 if( osClose((int(*)(int))aSyscall[1].pCurrent)(h) ){
33845 unixLogErrorAtLine(SQLITE_IOERR_CLOSE(10 | (16<<8)), "close",
33846 pFile ? pFile->zPath : 0, lineno);
33847 }
33848}
33849
33850/*
33851** Set the pFile->lastErrno. Do this in a subroutine as that provides
33852** a convenient place to set a breakpoint.
33853*/
33854static void storeLastErrno(unixFile *pFile, int error){
33855 pFile->lastErrno = error;
33856}
33857
33858/*
33859** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
33860*/
33861static void closePendingFds(unixFile *pFile){
33862 unixInodeInfo *pInode = pFile->pInode;
33863 UnixUnusedFd *p;
33864 UnixUnusedFd *pNext;
33865 assert( unixFileMutexHeld(pFile) )((void) (0));
33866 for(p=pInode->pUnused; p; p=pNext){
33867 pNext = p->pNext;
33868 robust_close(pFile, p->fd, __LINE__33868);
33869 sqlite3_free(p);
33870 }
33871 pInode->pUnused = 0;
33872}
33873
33874/*
33875** Release a unixInodeInfo structure previously allocated by findInodeInfo().
33876**
33877** The global mutex must be held when this routine is called, but the mutex
33878** on the inode being deleted must NOT be held.
33879*/
33880static void releaseInodeInfo(unixFile *pFile){
33881 unixInodeInfo *pInode = pFile->pInode;
33882 assert( unixMutexHeld() )((void) (0));
33883 assert( unixFileMutexNotheld(pFile) )((void) (0));
33884 if( ALWAYS(pInode)(pInode) ){
33885 pInode->nRef--;
33886 if( pInode->nRef==0 ){
33887 assert( pInode->pShmNode==0 )((void) (0));
33888 sqlite3_mutex_enter(pInode->pLockMutex);
33889 closePendingFds(pFile);
33890 sqlite3_mutex_leave(pInode->pLockMutex);
33891 if( pInode->pPrev ){
33892 assert( pInode->pPrev->pNext==pInode )((void) (0));
33893 pInode->pPrev->pNext = pInode->pNext;
33894 }else{
33895 assert( inodeList==pInode )((void) (0));
33896 inodeList = pInode->pNext;
33897 }
33898 if( pInode->pNext ){
33899 assert( pInode->pNext->pPrev==pInode )((void) (0));
33900 pInode->pNext->pPrev = pInode->pPrev;
33901 }
33902 sqlite3_mutex_free(pInode->pLockMutex);
33903 sqlite3_free(pInode);
33904 }
33905 }
33906}
33907
33908/*
33909** Given a file descriptor, locate the unixInodeInfo object that
33910** describes that file descriptor. Create a new one if necessary. The
33911** return value might be uninitialized if an error occurs.
33912**
33913** The global mutex must held when calling this routine.
33914**
33915** Return an appropriate error code.
33916*/
33917static int findInodeInfo(
33918 unixFile *pFile, /* Unix file with file desc used in the key */
33919 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
33920){
33921 int rc; /* System call return code */
33922 int fd; /* The file descriptor for pFile */
33923 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
33924 struct stat statbuf; /* Low-level file information */
33925 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
33926
33927 assert( unixMutexHeld() )((void) (0));
33928
33929 /* Get low-level information about the file that we can used to
33930 ** create a unique name for the file.
33931 */
33932 fd = pFile->h;
33933 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf);
33934 if( rc!=0 ){
33935 storeLastErrno(pFile, errno(*__errno_location ()));
33936#if defined(EOVERFLOW75) && defined(SQLITE_DISABLE_LFS)
33937 if( pFile->lastErrno==EOVERFLOW75 ) return SQLITE_NOLFS22;
33938#endif
33939 return SQLITE_IOERR10;
33940 }
33941
33942#ifdef __APPLE__
33943 /* On OS X on an msdos filesystem, the inode number is reported
33944 ** incorrectly for zero-size files. See ticket #3260. To work
33945 ** around this problem (we consider it a bug in OS X, not SQLite)
33946 ** we always increase the file size to 1 by writing a single byte
33947 ** prior to accessing the inode number. The one byte written is
33948 ** an ASCII 'S' character which also happens to be the first byte
33949 ** in the header of every SQLite database. In this way, if there
33950 ** is a race condition such that another thread has already populated
33951 ** the first page of the database, no damage is done.
33952 */
33953 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS0x1)!=0 ){
33954 do{ rc = osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)(fd, "S", 1); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
33955 if( rc!=1 ){
33956 storeLastErrno(pFile, errno(*__errno_location ()));
33957 return SQLITE_IOERR10;
33958 }
33959 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &statbuf);
33960 if( rc!=0 ){
33961 storeLastErrno(pFile, errno(*__errno_location ()));
33962 return SQLITE_IOERR10;
33963 }
33964 }
33965#endif
33966
33967 memset(&fileId, 0, sizeof(fileId));
33968 fileId.dev = statbuf.st_dev;
33969#if OS_VXWORKS0
33970 fileId.pId = pFile->pId;
33971#else
33972 fileId.ino = (u64)statbuf.st_ino;
33973#endif
33974 assert( unixMutexHeld() )((void) (0));
33975 pInode = inodeList;
33976 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
33977 pInode = pInode->pNext;
33978 }
33979 if( pInode==0 ){
33980 pInode = sqlite3_malloc64( sizeof(*pInode) );
33981 if( pInode==0 ){
33982 return SQLITE_NOMEM_BKPT7;
33983 }
33984 memset(pInode, 0, sizeof(*pInode));
33985 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
33986 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
33987 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
33988 if( pInode->pLockMutex==0 ){
33989 sqlite3_free(pInode);
33990 return SQLITE_NOMEM_BKPT7;
33991 }
33992 }
33993 pInode->nRef = 1;
33994 assert( unixMutexHeld() )((void) (0));
33995 pInode->pNext = inodeList;
33996 pInode->pPrev = 0;
33997 if( inodeList ) inodeList->pPrev = pInode;
33998 inodeList = pInode;
33999 }else{
34000 pInode->nRef++;
34001 }
34002 *ppInode = pInode;
34003 return SQLITE_OK0;
34004}
34005
34006/*
34007** Return TRUE if pFile has been renamed or unlinked since it was first opened.
34008*/
34009static int fileHasMoved(unixFile *pFile){
34010#if OS_VXWORKS0
34011 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
34012#else
34013 struct stat buf;
34014 return pFile->pInode!=0 &&
34015 (osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(pFile->zPath, &buf)!=0
34016 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
34017#endif
34018}
34019
34020
34021/*
34022** Check a unixFile that is a database. Verify the following:
34023**
34024** (1) There is exactly one hard link on the file
34025** (2) The file is not a symbolic link
34026** (3) The file has not been renamed or unlinked
34027**
34028** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
34029*/
34030static void verifyDbFile(unixFile *pFile){
34031 struct stat buf;
34032 int rc;
34033
34034 /* These verifications occurs for the main database only */
34035 if( pFile->ctrlFlags & UNIXFILE_NOLOCK0x80 ) return;
34036
34037 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf);
34038 if( rc!=0 ){
34039 sqlite3_log(SQLITE_WARNING28, "cannot fstat db file %s", pFile->zPath);
34040 return;
34041 }
34042 if( buf.st_nlink==0 ){
34043 sqlite3_log(SQLITE_WARNING28, "file unlinked while open: %s", pFile->zPath);
34044 return;
34045 }
34046 if( buf.st_nlink>1 ){
34047 sqlite3_log(SQLITE_WARNING28, "multiple links to file: %s", pFile->zPath);
34048 return;
34049 }
34050 if( fileHasMoved(pFile) ){
34051 sqlite3_log(SQLITE_WARNING28, "file renamed while open: %s", pFile->zPath);
34052 return;
34053 }
34054}
34055
34056
34057/*
34058** This routine checks if there is a RESERVED lock held on the specified
34059** file by this or any other process. If such a lock is held, set *pResOut
34060** to a non-zero value otherwise *pResOut is set to zero. The return value
34061** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34062*/
34063static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
34064 int rc = SQLITE_OK0;
34065 int reserved = 0;
34066 unixFile *pFile = (unixFile*)id;
34067
34068 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34069
34070 assert( pFile )((void) (0));
34071 assert( pFile->eFileLock<=SHARED_LOCK )((void) (0));
34072 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
34073
34074 /* Check if a thread in this process holds such a lock */
34075 if( pFile->pInode->eFileLock>SHARED_LOCK1 ){
34076 reserved = 1;
34077 }
34078
34079 /* Otherwise see if some other process holds it.
34080 */
34081#ifndef __DJGPP__
34082 if( !reserved && !pFile->pInode->bProcessLock ){
34083 struct flock lock;
34084 lock.l_whence = SEEK_SET0;
34085 lock.l_start = RESERVED_BYTE(sqlite3PendingByte+1);
34086 lock.l_len = 1;
34087 lock.l_type = F_WRLCK1;
34088 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h, F_GETLK5, &lock) ){
34089 rc = SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8));
34090 storeLastErrno(pFile, errno(*__errno_location ()));
34091 } else if( lock.l_type!=F_UNLCK2 ){
34092 reserved = 1;
34093 }
34094 }
34095#endif
34096
34097 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
34098 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
34099
34100 *pResOut = reserved;
34101 return rc;
34102}
34103
34104/*
34105** Set a posix-advisory-lock.
34106**
34107** There are two versions of this routine. If compiled with
34108** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
34109** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
34110** value is set, then it is the number of milliseconds to wait before
34111** failing the lock. The iBusyTimeout value is always reset back to
34112** zero on each call.
34113**
34114** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
34115** attempt to set the lock.
34116*/
34117#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
34118# define osSetPosixAdvisoryLock(h,x,t)((int(*)(int,int,...))aSyscall[7].pCurrent)(h,6,x) osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,x)
34119#else
34120static int osSetPosixAdvisoryLock(((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
34121 int h, /* The file descriptor on which to take the lock */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
34122 struct flock *pLock, /* The description of the lock */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
34123 unixFile *pFile /* Structure holding timeout value */((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
34124)((int(*)(int,int,...))aSyscall[7].pCurrent)(int h,6,struct flock
*pLock)
{
34125 int rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,pLock);
34126 while( rc<0 && pFile->iBusyTimeout>0 ){
34127 /* On systems that support some kind of blocking file lock with a timeout,
34128 ** make appropriate changes here to invoke that blocking file lock. On
34129 ** generic posix, however, there is no such API. So we simply try the
34130 ** lock once every millisecond until either the timeout expires, or until
34131 ** the lock is obtained. */
34132 usleep(1000);
34133 rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(h,F_SETLK6,pLock);
34134 pFile->iBusyTimeout--;
34135 }
34136 return rc;
34137}
34138#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
34139
34140
34141/*
34142** Attempt to set a system-lock on the file pFile. The lock is
34143** described by pLock.
34144**
34145** If the pFile was opened read/write from unix-excl, then the only lock
34146** ever obtained is an exclusive lock, and it is obtained exactly once
34147** the first time any lock is attempted. All subsequent system locking
34148** operations become no-ops. Locking operations still happen internally,
34149** in order to coordinate access between separate database connections
34150** within this process, but all of that is handled in memory and the
34151** operating system does not participate.
34152**
34153** This function is a pass-through to fcntl(F_SETLK) if pFile is using
34154** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
34155** and is read-only.
34156**
34157** Zero is returned if the call completes successfully, or -1 if a call
34158** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
34159*/
34160static int unixFileLock(unixFile *pFile, struct flock *pLock){
34161 int rc;
34162 unixInodeInfo *pInode = pFile->pInode;
34163 assert( pInode!=0 )((void) (0));
34164 assert( sqlite3_mutex_held(pInode->pLockMutex) )((void) (0));
34165 if( (pFile->ctrlFlags & (UNIXFILE_EXCL0x01|UNIXFILE_RDONLY0x02))==UNIXFILE_EXCL0x01 ){
34166 if( pInode->bProcessLock==0 ){
34167 struct flock lock;
34168 assert( pInode->nLock==0 )((void) (0));
34169 lock.l_whence = SEEK_SET0;
34170 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
34171 lock.l_len = SHARED_SIZE510;
34172 lock.l_type = F_WRLCK1;
34173 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h,6,&
lock)
;
34174 if( rc<0 ) return rc;
34175 pInode->bProcessLock = 1;
34176 pInode->nLock++;
34177 }else{
34178 rc = 0;
34179 }
34180 }else{
34181 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pFile->h,6,pLock
)
;
34182 }
34183 return rc;
34184}
34185
34186/*
34187** Lock the file with the lock specified by parameter eFileLock - one
34188** of the following:
34189**
34190** (1) SHARED_LOCK
34191** (2) RESERVED_LOCK
34192** (3) PENDING_LOCK
34193** (4) EXCLUSIVE_LOCK
34194**
34195** Sometimes when requesting one lock state, additional lock states
34196** are inserted in between. The locking might fail on one of the later
34197** transitions leaving the lock state different from what it started but
34198** still short of its goal. The following chart shows the allowed
34199** transitions and the inserted intermediate states:
34200**
34201** UNLOCKED -> SHARED
34202** SHARED -> RESERVED
34203** SHARED -> (PENDING) -> EXCLUSIVE
34204** RESERVED -> (PENDING) -> EXCLUSIVE
34205** PENDING -> EXCLUSIVE
34206**
34207** This routine will only increase a lock. Use the sqlite3OsUnlock()
34208** routine to lower a locking level.
34209*/
34210static int unixLock(sqlite3_file *id, int eFileLock){
34211 /* The following describes the implementation of the various locks and
34212 ** lock transitions in terms of the POSIX advisory shared and exclusive
34213 ** lock primitives (called read-locks and write-locks below, to avoid
34214 ** confusion with SQLite lock names). The algorithms are complicated
34215 ** slightly in order to be compatible with Windows95 systems simultaneously
34216 ** accessing the same database file, in case that is ever required.
34217 **
34218 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
34219 ** byte', each single bytes at well known offsets, and the 'shared byte
34220 ** range', a range of 510 bytes at a well known offset.
34221 **
34222 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
34223 ** byte'. If this is successful, 'shared byte range' is read-locked
34224 ** and the lock on the 'pending byte' released. (Legacy note: When
34225 ** SQLite was first developed, Windows95 systems were still very common,
34226 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
34227 ** single randomly selected by from the 'shared byte range' is locked.
34228 ** Windows95 is now pretty much extinct, but this work-around for the
34229 ** lack of shared-locks on Windows95 lives on, for backwards
34230 ** compatibility.)
34231 **
34232 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
34233 ** A RESERVED lock is implemented by grabbing a write-lock on the
34234 ** 'reserved byte'.
34235 **
34236 ** A process may only obtain a PENDING lock after it has obtained a
34237 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
34238 ** on the 'pending byte'. This ensures that no new SHARED locks can be
34239 ** obtained, but existing SHARED locks are allowed to persist. A process
34240 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
34241 ** This property is used by the algorithm for rolling back a journal file
34242 ** after a crash.
34243 **
34244 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
34245 ** implemented by obtaining a write-lock on the entire 'shared byte
34246 ** range'. Since all other locks require a read-lock on one of the bytes
34247 ** within this range, this ensures that no other locks are held on the
34248 ** database.
34249 */
34250 int rc = SQLITE_OK0;
34251 unixFile *pFile = (unixFile*)id;
34252 unixInodeInfo *pInode;
34253 struct flock lock;
34254 int tErrno = 0;
34255
34256 assert( pFile )((void) (0));
34257 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
34258 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
34259 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
34260 osGetpid(0)));
34261
34262 /* If there is already a lock of this type or more restrictive on the
34263 ** unixFile, do nothing. Don't use the end_lock: exit path, as
34264 ** unixEnterMutex() hasn't been called yet.
34265 */
34266 if( pFile->eFileLock>=eFileLock ){
34267 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
34268 azFileLock(eFileLock)));
34269 return SQLITE_OK0;
34270 }
34271
34272 /* Make sure the locking sequence is correct.
34273 ** (1) We never move from unlocked to anything higher than shared lock.
34274 ** (2) SQLite never explicitly requests a pendig lock.
34275 ** (3) A shared lock is always held when a reserve lock is requested.
34276 */
34277 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK )((void) (0));
34278 assert( eFileLock!=PENDING_LOCK )((void) (0));
34279 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK )((void) (0));
34280
34281 /* This mutex is needed because pFile->pInode is shared across threads
34282 */
34283 pInode = pFile->pInode;
34284 sqlite3_mutex_enter(pInode->pLockMutex);
34285
34286 /* If some thread using this PID has a lock via a different unixFile*
34287 ** handle that precludes the requested lock, return BUSY.
34288 */
34289 if( (pFile->eFileLock!=pInode->eFileLock &&
34290 (pInode->eFileLock>=PENDING_LOCK3 || eFileLock>SHARED_LOCK1))
34291 ){
34292 rc = SQLITE_BUSY5;
34293 goto end_lock;
34294 }
34295
34296 /* If a SHARED lock is requested, and some thread using this PID already
34297 ** has a SHARED or RESERVED lock, then increment reference counts and
34298 ** return SQLITE_OK.
34299 */
34300 if( eFileLock==SHARED_LOCK1 &&
34301 (pInode->eFileLock==SHARED_LOCK1 || pInode->eFileLock==RESERVED_LOCK2) ){
34302 assert( eFileLock==SHARED_LOCK )((void) (0));
34303 assert( pFile->eFileLock==0 )((void) (0));
34304 assert( pInode->nShared>0 )((void) (0));
34305 pFile->eFileLock = SHARED_LOCK1;
34306 pInode->nShared++;
34307 pInode->nLock++;
34308 goto end_lock;
34309 }
34310
34311
34312 /* A PENDING lock is needed before acquiring a SHARED lock and before
34313 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
34314 ** be released.
34315 */
34316 lock.l_len = 1L;
34317 lock.l_whence = SEEK_SET0;
34318 if( eFileLock==SHARED_LOCK1
34319 || (eFileLock==EXCLUSIVE_LOCK4 && pFile->eFileLock<PENDING_LOCK3)
34320 ){
34321 lock.l_type = (eFileLock==SHARED_LOCK1?F_RDLCK0:F_WRLCK1);
34322 lock.l_start = PENDING_BYTEsqlite3PendingByte;
34323 if( unixFileLock(pFile, &lock) ){
34324 tErrno = errno(*__errno_location ());
34325 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
34326 if( rc!=SQLITE_BUSY5 ){
34327 storeLastErrno(pFile, tErrno);
34328 }
34329 goto end_lock;
34330 }
34331 }
34332
34333
34334 /* If control gets to this point, then actually go ahead and make
34335 ** operating system calls for the specified lock.
34336 */
34337 if( eFileLock==SHARED_LOCK1 ){
34338 assert( pInode->nShared==0 )((void) (0));
34339 assert( pInode->eFileLock==0 )((void) (0));
34340 assert( rc==SQLITE_OK )((void) (0));
34341
34342 /* Now get the read-lock */
34343 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
34344 lock.l_len = SHARED_SIZE510;
34345 if( unixFileLock(pFile, &lock) ){
34346 tErrno = errno(*__errno_location ());
34347 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
34348 }
34349
34350 /* Drop the temporary PENDING lock */
34351 lock.l_start = PENDING_BYTEsqlite3PendingByte;
34352 lock.l_len = 1L;
34353 lock.l_type = F_UNLCK2;
34354 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK0 ){
34355 /* This could happen with a network mount */
34356 tErrno = errno(*__errno_location ());
34357 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
34358 }
34359
34360 if( rc ){
34361 if( rc!=SQLITE_BUSY5 ){
34362 storeLastErrno(pFile, tErrno);
34363 }
34364 goto end_lock;
34365 }else{
34366 pFile->eFileLock = SHARED_LOCK1;
34367 pInode->nLock++;
34368 pInode->nShared = 1;
34369 }
34370 }else if( eFileLock==EXCLUSIVE_LOCK4 && pInode->nShared>1 ){
34371 /* We are trying for an exclusive lock but another thread in this
34372 ** same process is still holding a shared lock. */
34373 rc = SQLITE_BUSY5;
34374 }else{
34375 /* The request was for a RESERVED or EXCLUSIVE lock. It is
34376 ** assumed that there is a SHARED or greater lock on the file
34377 ** already.
34378 */
34379 assert( 0!=pFile->eFileLock )((void) (0));
34380 lock.l_type = F_WRLCK1;
34381
34382 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK )((void) (0));
34383 if( eFileLock==RESERVED_LOCK2 ){
34384 lock.l_start = RESERVED_BYTE(sqlite3PendingByte+1);
34385 lock.l_len = 1L;
34386 }else{
34387 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
34388 lock.l_len = SHARED_SIZE510;
34389 }
34390
34391 if( unixFileLock(pFile, &lock) ){
34392 tErrno = errno(*__errno_location ());
34393 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
34394 if( rc!=SQLITE_BUSY5 ){
34395 storeLastErrno(pFile, tErrno);
34396 }
34397 }
34398 }
34399
34400
34401#ifdef SQLITE_DEBUG
34402 /* Set up the transaction-counter change checking flags when
34403 ** transitioning from a SHARED to a RESERVED lock. The change
34404 ** from SHARED to RESERVED marks the beginning of a normal
34405 ** write operation (not a hot journal rollback).
34406 */
34407 if( rc==SQLITE_OK0
34408 && pFile->eFileLock<=SHARED_LOCK1
34409 && eFileLock==RESERVED_LOCK2
34410 ){
34411 pFile->transCntrChng = 0;
34412 pFile->dbUpdate = 0;
34413 pFile->inNormalWrite = 1;
34414 }
34415#endif
34416
34417
34418 if( rc==SQLITE_OK0 ){
34419 pFile->eFileLock = eFileLock;
34420 pInode->eFileLock = eFileLock;
34421 }else if( eFileLock==EXCLUSIVE_LOCK4 ){
34422 pFile->eFileLock = PENDING_LOCK3;
34423 pInode->eFileLock = PENDING_LOCK3;
34424 }
34425
34426end_lock:
34427 sqlite3_mutex_leave(pInode->pLockMutex);
34428 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
34429 rc==SQLITE_OK ? "ok" : "failed"));
34430 return rc;
34431}
34432
34433/*
34434** Add the file descriptor used by file handle pFile to the corresponding
34435** pUnused list.
34436*/
34437static void setPendingFd(unixFile *pFile){
34438 unixInodeInfo *pInode = pFile->pInode;
34439 UnixUnusedFd *p = pFile->pPreallocatedUnused;
34440 assert( unixFileMutexHeld(pFile) )((void) (0));
34441 p->pNext = pInode->pUnused;
34442 pInode->pUnused = p;
34443 pFile->h = -1;
34444 pFile->pPreallocatedUnused = 0;
34445}
34446
34447/*
34448** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34449** must be either NO_LOCK or SHARED_LOCK.
34450**
34451** If the locking level of the file descriptor is already at or below
34452** the requested locking level, this routine is a no-op.
34453**
34454** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
34455** the byte range is divided into 2 parts and the first part is unlocked then
34456** set to a read lock, then the other part is simply unlocked. This works
34457** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
34458** remove the write lock on a region when a read lock is set.
34459*/
34460static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
34461 unixFile *pFile = (unixFile*)id;
34462 unixInodeInfo *pInode;
34463 struct flock lock;
34464 int rc = SQLITE_OK0;
34465
34466 assert( pFile )((void) (0));
34467 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
34468 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
34469 osGetpid(0)));
34470
34471 assert( eFileLock<=SHARED_LOCK )((void) (0));
34472 if( pFile->eFileLock<=eFileLock ){
34473 return SQLITE_OK0;
34474 }
34475 pInode = pFile->pInode;
34476 sqlite3_mutex_enter(pInode->pLockMutex);
34477 assert( pInode->nShared!=0 )((void) (0));
34478 if( pFile->eFileLock>SHARED_LOCK1 ){
34479 assert( pInode->eFileLock==pFile->eFileLock )((void) (0));
34480
34481#ifdef SQLITE_DEBUG
34482 /* When reducing a lock such that other processes can start
34483 ** reading the database file again, make sure that the
34484 ** transaction counter was updated if any part of the database
34485 ** file changed. If the transaction counter is not updated,
34486 ** other connections to the same file might not realize that
34487 ** the file has changed and hence might not know to flush their
34488 ** cache. The use of a stale cache can lead to database corruption.
34489 */
34490 pFile->inNormalWrite = 0;
34491#endif
34492
34493 /* downgrading to a shared lock on NFS involves clearing the write lock
34494 ** before establishing the readlock - to avoid a race condition we downgrade
34495 ** the lock in 2 blocks, so that part of the range will be covered by a
34496 ** write lock until the rest is covered by a read lock:
34497 ** 1: [WWWWW]
34498 ** 2: [....W]
34499 ** 3: [RRRRW]
34500 ** 4: [RRRR.]
34501 */
34502 if( eFileLock==SHARED_LOCK1 ){
34503#if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE0
34504 (void)handleNFSUnlock;
34505 assert( handleNFSUnlock==0 )((void) (0));
34506#endif
34507#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
34508 if( handleNFSUnlock ){
34509 int tErrno; /* Error code from system call errors */
34510 off_t divSize = SHARED_SIZE510 - 1;
34511
34512 lock.l_type = F_UNLCK2;
34513 lock.l_whence = SEEK_SET0;
34514 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
34515 lock.l_len = divSize;
34516 if( unixFileLock(pFile, &lock)==(-1) ){
34517 tErrno = errno(*__errno_location ());
34518 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
34519 storeLastErrno(pFile, tErrno);
34520 goto end_unlock;
34521 }
34522 lock.l_type = F_RDLCK0;
34523 lock.l_whence = SEEK_SET0;
34524 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
34525 lock.l_len = divSize;
34526 if( unixFileLock(pFile, &lock)==(-1) ){
34527 tErrno = errno(*__errno_location ());
34528 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK(10 | (9<<8)));
34529 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
34530 storeLastErrno(pFile, tErrno);
34531 }
34532 goto end_unlock;
34533 }
34534 lock.l_type = F_UNLCK2;
34535 lock.l_whence = SEEK_SET0;
34536 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2)+divSize;
34537 lock.l_len = SHARED_SIZE510-divSize;
34538 if( unixFileLock(pFile, &lock)==(-1) ){
34539 tErrno = errno(*__errno_location ());
34540 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
34541 storeLastErrno(pFile, tErrno);
34542 goto end_unlock;
34543 }
34544 }else
34545#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
34546 {
34547 lock.l_type = F_RDLCK0;
34548 lock.l_whence = SEEK_SET0;
34549 lock.l_start = SHARED_FIRST(sqlite3PendingByte+2);
34550 lock.l_len = SHARED_SIZE510;
34551 if( unixFileLock(pFile, &lock) ){
34552 /* In theory, the call to unixFileLock() cannot fail because another
34553 ** process is holding an incompatible lock. If it does, this
34554 ** indicates that the other process is not following the locking
34555 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
34556 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
34557 ** an assert to fail). */
34558 rc = SQLITE_IOERR_RDLOCK(10 | (9<<8));
34559 storeLastErrno(pFile, errno(*__errno_location ()));
34560 goto end_unlock;
34561 }
34562 }
34563 }
34564 lock.l_type = F_UNLCK2;
34565 lock.l_whence = SEEK_SET0;
34566 lock.l_start = PENDING_BYTEsqlite3PendingByte;
34567 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE )((void) (0));
34568 if( unixFileLock(pFile, &lock)==0 ){
34569 pInode->eFileLock = SHARED_LOCK1;
34570 }else{
34571 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
34572 storeLastErrno(pFile, errno(*__errno_location ()));
34573 goto end_unlock;
34574 }
34575 }
34576 if( eFileLock==NO_LOCK0 ){
34577 /* Decrement the shared lock counter. Release the lock using an
34578 ** OS call only when all threads in this same process have released
34579 ** the lock.
34580 */
34581 pInode->nShared--;
34582 if( pInode->nShared==0 ){
34583 lock.l_type = F_UNLCK2;
34584 lock.l_whence = SEEK_SET0;
34585 lock.l_start = lock.l_len = 0L;
34586 if( unixFileLock(pFile, &lock)==0 ){
34587 pInode->eFileLock = NO_LOCK0;
34588 }else{
34589 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
34590 storeLastErrno(pFile, errno(*__errno_location ()));
34591 pInode->eFileLock = NO_LOCK0;
34592 pFile->eFileLock = NO_LOCK0;
34593 }
34594 }
34595
34596 /* Decrement the count of locks against this same file. When the
34597 ** count reaches zero, close any other file descriptors whose close
34598 ** was deferred because of outstanding locks.
34599 */
34600 pInode->nLock--;
34601 assert( pInode->nLock>=0 )((void) (0));
34602 if( pInode->nLock==0 ) closePendingFds(pFile);
34603 }
34604
34605end_unlock:
34606 sqlite3_mutex_leave(pInode->pLockMutex);
34607 if( rc==SQLITE_OK0 ){
34608 pFile->eFileLock = eFileLock;
34609 }
34610 return rc;
34611}
34612
34613/*
34614** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34615** must be either NO_LOCK or SHARED_LOCK.
34616**
34617** If the locking level of the file descriptor is already at or below
34618** the requested locking level, this routine is a no-op.
34619*/
34620static int unixUnlock(sqlite3_file *id, int eFileLock){
34621#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
34622 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 )((void) (0));
34623#endif
34624 return posixUnlock(id, eFileLock, 0);
34625}
34626
34627#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
34628static int unixMapfile(unixFile *pFd, i64 nByte);
34629static void unixUnmapfile(unixFile *pFd);
34630#endif
34631
34632/*
34633** This function performs the parts of the "close file" operation
34634** common to all locking schemes. It closes the directory and file
34635** handles, if they are valid, and sets all fields of the unixFile
34636** structure to 0.
34637**
34638** It is *not* necessary to hold the mutex when this routine is called,
34639** even on VxWorks. A mutex will be acquired on VxWorks by the
34640** vxworksReleaseFileId() routine.
34641*/
34642static int closeUnixFile(sqlite3_file *id){
34643 unixFile *pFile = (unixFile*)id;
34644#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
34645 unixUnmapfile(pFile);
34646#endif
34647 if( pFile->h>=0 ){
34648 robust_close(pFile, pFile->h, __LINE__34648);
34649 pFile->h = -1;
34650 }
34651#if OS_VXWORKS0
34652 if( pFile->pId ){
34653 if( pFile->ctrlFlags & UNIXFILE_DELETE0x20 ){
34654 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pFile->pId->zCanonicalName);
34655 }
34656 vxworksReleaseFileId(pFile->pId);
34657 pFile->pId = 0;
34658 }
34659#endif
34660#ifdef SQLITE_UNLINK_AFTER_CLOSE
34661 if( pFile->ctrlFlags & UNIXFILE_DELETE0x20 ){
34662 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pFile->zPath);
34663 sqlite3_free(*(char**)&pFile->zPath);
34664 pFile->zPath = 0;
34665 }
34666#endif
34667 OSTRACE(("CLOSE %-3d\n", pFile->h));
34668 OpenCounter(-1);
34669 sqlite3_free(pFile->pPreallocatedUnused);
34670 memset(pFile, 0, sizeof(unixFile));
34671 return SQLITE_OK0;
34672}
34673
34674/*
34675** Close a file.
34676*/
34677static int unixClose(sqlite3_file *id){
34678 int rc = SQLITE_OK0;
34679 unixFile *pFile = (unixFile *)id;
34680 unixInodeInfo *pInode = pFile->pInode;
34681
34682 assert( pInode!=0 )((void) (0));
34683 verifyDbFile(pFile);
34684 unixUnlock(id, NO_LOCK0);
34685 assert( unixFileMutexNotheld(pFile) )((void) (0));
34686 unixEnterMutex();
34687
34688 /* unixFile.pInode is always valid here. Otherwise, a different close
34689 ** routine (e.g. nolockClose()) would be called instead.
34690 */
34691 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 )((void) (0));
34692 sqlite3_mutex_enter(pInode->pLockMutex);
34693 if( pInode->nLock ){
34694 /* If there are outstanding locks, do not actually close the file just
34695 ** yet because that would clear those locks. Instead, add the file
34696 ** descriptor to pInode->pUnused list. It will be automatically closed
34697 ** when the last lock is cleared.
34698 */
34699 setPendingFd(pFile);
34700 }
34701 sqlite3_mutex_leave(pInode->pLockMutex);
34702 releaseInodeInfo(pFile);
34703 rc = closeUnixFile(id);
34704 unixLeaveMutex();
34705 return rc;
34706}
34707
34708/************** End of the posix advisory lock implementation *****************
34709******************************************************************************/
34710
34711/******************************************************************************
34712****************************** No-op Locking **********************************
34713**
34714** Of the various locking implementations available, this is by far the
34715** simplest: locking is ignored. No attempt is made to lock the database
34716** file for reading or writing.
34717**
34718** This locking mode is appropriate for use on read-only databases
34719** (ex: databases that are burned into CD-ROM, for example.) It can
34720** also be used if the application employs some external mechanism to
34721** prevent simultaneous access of the same database by two or more
34722** database connections. But there is a serious risk of database
34723** corruption if this locking mode is used in situations where multiple
34724** database connections are accessing the same database file at the same
34725** time and one or more of those connections are writing.
34726*/
34727
34728static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
34729 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
34730 *pResOut = 0;
34731 return SQLITE_OK0;
34732}
34733static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
34734 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
34735 return SQLITE_OK0;
34736}
34737static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
34738 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
34739 return SQLITE_OK0;
34740}
34741
34742/*
34743** Close the file.
34744*/
34745static int nolockClose(sqlite3_file *id) {
34746 return closeUnixFile(id);
34747}
34748
34749/******************* End of the no-op lock implementation *********************
34750******************************************************************************/
34751
34752/******************************************************************************
34753************************* Begin dot-file Locking ******************************
34754**
34755** The dotfile locking implementation uses the existence of separate lock
34756** files (really a directory) to control access to the database. This works
34757** on just about every filesystem imaginable. But there are serious downsides:
34758**
34759** (1) There is zero concurrency. A single reader blocks all other
34760** connections from reading or writing the database.
34761**
34762** (2) An application crash or power loss can leave stale lock files
34763** sitting around that need to be cleared manually.
34764**
34765** Nevertheless, a dotlock is an appropriate locking mode for use if no
34766** other locking strategy is available.
34767**
34768** Dotfile locking works by creating a subdirectory in the same directory as
34769** the database and with the same name but with a ".lock" extension added.
34770** The existence of a lock directory implies an EXCLUSIVE lock. All other
34771** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
34772*/
34773
34774/*
34775** The file suffix added to the data base filename in order to create the
34776** lock directory.
34777*/
34778#define DOTLOCK_SUFFIX".lock" ".lock"
34779
34780/*
34781** This routine checks if there is a RESERVED lock held on the specified
34782** file by this or any other process. If such a lock is held, set *pResOut
34783** to a non-zero value otherwise *pResOut is set to zero. The return value
34784** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34785**
34786** In dotfile locking, either a lock exists or it does not. So in this
34787** variation of CheckReservedLock(), *pResOut is set to true if any lock
34788** is held on the file and false if the file is unlocked.
34789*/
34790static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
34791 int rc = SQLITE_OK0;
34792 int reserved = 0;
34793 unixFile *pFile = (unixFile*)id;
34794
34795 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34796
34797 assert( pFile )((void) (0));
34798 reserved = osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)((const char*)pFile->lockingContext, 0)==0;
34799 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
34800 *pResOut = reserved;
34801 return rc;
34802}
34803
34804/*
34805** Lock the file with the lock specified by parameter eFileLock - one
34806** of the following:
34807**
34808** (1) SHARED_LOCK
34809** (2) RESERVED_LOCK
34810** (3) PENDING_LOCK
34811** (4) EXCLUSIVE_LOCK
34812**
34813** Sometimes when requesting one lock state, additional lock states
34814** are inserted in between. The locking might fail on one of the later
34815** transitions leaving the lock state different from what it started but
34816** still short of its goal. The following chart shows the allowed
34817** transitions and the inserted intermediate states:
34818**
34819** UNLOCKED -> SHARED
34820** SHARED -> RESERVED
34821** SHARED -> (PENDING) -> EXCLUSIVE
34822** RESERVED -> (PENDING) -> EXCLUSIVE
34823** PENDING -> EXCLUSIVE
34824**
34825** This routine will only increase a lock. Use the sqlite3OsUnlock()
34826** routine to lower a locking level.
34827**
34828** With dotfile locking, we really only support state (4): EXCLUSIVE.
34829** But we track the other locking levels internally.
34830*/
34831static int dotlockLock(sqlite3_file *id, int eFileLock) {
34832 unixFile *pFile = (unixFile*)id;
34833 char *zLockFile = (char *)pFile->lockingContext;
34834 int rc = SQLITE_OK0;
34835
34836
34837 /* If we have any lock, then the lock file already exists. All we have
34838 ** to do is adjust our internal record of the lock level.
34839 */
34840 if( pFile->eFileLock > NO_LOCK0 ){
34841 pFile->eFileLock = eFileLock;
34842 /* Always update the timestamp on the old file */
34843#ifdef HAVE_UTIME
34844 utime(zLockFile, NULL((void*)0));
34845#else
34846 utimes(zLockFile, NULL((void*)0));
34847#endif
34848 return SQLITE_OK0;
34849 }
34850
34851 /* grab an exclusive lock */
34852 rc = osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent)(zLockFile, 0777);
34853 if( rc<0 ){
34854 /* failed to open/create the lock directory */
34855 int tErrno = errno(*__errno_location ());
34856 if( EEXIST17 == tErrno ){
34857 rc = SQLITE_BUSY5;
34858 } else {
34859 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
34860 if( rc!=SQLITE_BUSY5 ){
34861 storeLastErrno(pFile, tErrno);
34862 }
34863 }
34864 return rc;
34865 }
34866
34867 /* got it, set the type and return ok */
34868 pFile->eFileLock = eFileLock;
34869 return rc;
34870}
34871
34872/*
34873** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
34874** must be either NO_LOCK or SHARED_LOCK.
34875**
34876** If the locking level of the file descriptor is already at or below
34877** the requested locking level, this routine is a no-op.
34878**
34879** When the locking level reaches NO_LOCK, delete the lock file.
34880*/
34881static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
34882 unixFile *pFile = (unixFile*)id;
34883 char *zLockFile = (char *)pFile->lockingContext;
34884 int rc;
34885
34886 assert( pFile )((void) (0));
34887 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
34888 pFile->eFileLock, osGetpid(0)));
34889 assert( eFileLock<=SHARED_LOCK )((void) (0));
34890
34891 /* no-op if possible */
34892 if( pFile->eFileLock==eFileLock ){
34893 return SQLITE_OK0;
34894 }
34895
34896 /* To downgrade to shared, simply update our internal notion of the
34897 ** lock state. No need to mess with the file on disk.
34898 */
34899 if( eFileLock==SHARED_LOCK1 ){
34900 pFile->eFileLock = SHARED_LOCK1;
34901 return SQLITE_OK0;
34902 }
34903
34904 /* To fully unlock the database, delete the lock file */
34905 assert( eFileLock==NO_LOCK )((void) (0));
34906 rc = osRmdir((int(*)(const char*))aSyscall[19].pCurrent)(zLockFile);
34907 if( rc<0 ){
34908 int tErrno = errno(*__errno_location ());
34909 if( tErrno==ENOENT2 ){
34910 rc = SQLITE_OK0;
34911 }else{
34912 rc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
34913 storeLastErrno(pFile, tErrno);
34914 }
34915 return rc;
34916 }
34917 pFile->eFileLock = NO_LOCK0;
34918 return SQLITE_OK0;
34919}
34920
34921/*
34922** Close a file. Make sure the lock has been released before closing.
34923*/
34924static int dotlockClose(sqlite3_file *id) {
34925 unixFile *pFile = (unixFile*)id;
34926 assert( id!=0 )((void) (0));
34927 dotlockUnlock(id, NO_LOCK0);
34928 sqlite3_free(pFile->lockingContext);
34929 return closeUnixFile(id);
34930}
34931/****************** End of the dot-file lock implementation *******************
34932******************************************************************************/
34933
34934/******************************************************************************
34935************************** Begin flock Locking ********************************
34936**
34937** Use the flock() system call to do file locking.
34938**
34939** flock() locking is like dot-file locking in that the various
34940** fine-grain locking levels supported by SQLite are collapsed into
34941** a single exclusive lock. In other words, SHARED, RESERVED, and
34942** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
34943** still works when you do this, but concurrency is reduced since
34944** only a single process can be reading the database at a time.
34945**
34946** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
34947*/
34948#if SQLITE_ENABLE_LOCKING_STYLE0
34949
34950/*
34951** Retry flock() calls that fail with EINTR
34952*/
34953#ifdef EINTR4
34954static int robust_flock(int fd, int op){
34955 int rc;
34956 do{ rc = flock(fd,op); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
34957 return rc;
34958}
34959#else
34960# define robust_flock(a,b) flock(a,b)
34961#endif
34962
34963
34964/*
34965** This routine checks if there is a RESERVED lock held on the specified
34966** file by this or any other process. If such a lock is held, set *pResOut
34967** to a non-zero value otherwise *pResOut is set to zero. The return value
34968** is set to SQLITE_OK unless an I/O error occurs during lock checking.
34969*/
34970static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
34971 int rc = SQLITE_OK0;
34972 int reserved = 0;
34973 unixFile *pFile = (unixFile*)id;
34974
34975 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34976
34977 assert( pFile )((void) (0));
34978
34979 /* Check if a thread in this process holds such a lock */
34980 if( pFile->eFileLock>SHARED_LOCK1 ){
34981 reserved = 1;
34982 }
34983
34984 /* Otherwise see if some other process holds it. */
34985 if( !reserved ){
34986 /* attempt to get the lock */
34987 int lrc = robust_flock(pFile->h, LOCK_EX2 | LOCK_NB4);
34988 if( !lrc ){
34989 /* got the lock, unlock it */
34990 lrc = robust_flock(pFile->h, LOCK_UN8);
34991 if ( lrc ) {
34992 int tErrno = errno(*__errno_location ());
34993 /* unlock failed with an error */
34994 lrc = SQLITE_IOERR_UNLOCK(10 | (8<<8));
34995 storeLastErrno(pFile, tErrno);
34996 rc = lrc;
34997 }
34998 } else {
34999 int tErrno = errno(*__errno_location ());
35000 reserved = 1;
35001 /* someone else might have it reserved */
35002 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
35003 if( IS_LOCK_ERROR(lrc)((lrc != 0) && (lrc != 5)) ){
35004 storeLastErrno(pFile, tErrno);
35005 rc = lrc;
35006 }
35007 }
35008 }
35009 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
35010
35011#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
35012 if( (rc & 0xff) == SQLITE_IOERR10 ){
35013 rc = SQLITE_OK0;
35014 reserved=1;
35015 }
35016#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
35017 *pResOut = reserved;
35018 return rc;
35019}
35020
35021/*
35022** Lock the file with the lock specified by parameter eFileLock - one
35023** of the following:
35024**
35025** (1) SHARED_LOCK
35026** (2) RESERVED_LOCK
35027** (3) PENDING_LOCK
35028** (4) EXCLUSIVE_LOCK
35029**
35030** Sometimes when requesting one lock state, additional lock states
35031** are inserted in between. The locking might fail on one of the later
35032** transitions leaving the lock state different from what it started but
35033** still short of its goal. The following chart shows the allowed
35034** transitions and the inserted intermediate states:
35035**
35036** UNLOCKED -> SHARED
35037** SHARED -> RESERVED
35038** SHARED -> (PENDING) -> EXCLUSIVE
35039** RESERVED -> (PENDING) -> EXCLUSIVE
35040** PENDING -> EXCLUSIVE
35041**
35042** flock() only really support EXCLUSIVE locks. We track intermediate
35043** lock states in the sqlite3_file structure, but all locks SHARED or
35044** above are really EXCLUSIVE locks and exclude all other processes from
35045** access the file.
35046**
35047** This routine will only increase a lock. Use the sqlite3OsUnlock()
35048** routine to lower a locking level.
35049*/
35050static int flockLock(sqlite3_file *id, int eFileLock) {
35051 int rc = SQLITE_OK0;
35052 unixFile *pFile = (unixFile*)id;
35053
35054 assert( pFile )((void) (0));
35055
35056 /* if we already have a lock, it is exclusive.
35057 ** Just adjust level and punt on outta here. */
35058 if (pFile->eFileLock > NO_LOCK0) {
35059 pFile->eFileLock = eFileLock;
35060 return SQLITE_OK0;
35061 }
35062
35063 /* grab an exclusive lock */
35064
35065 if (robust_flock(pFile->h, LOCK_EX2 | LOCK_NB4)) {
35066 int tErrno = errno(*__errno_location ());
35067 /* didn't get, must be busy */
35068 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK(10 | (15<<8)));
35069 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
35070 storeLastErrno(pFile, tErrno);
35071 }
35072 } else {
35073 /* got it, set the type and return ok */
35074 pFile->eFileLock = eFileLock;
35075 }
35076 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
35077 rc==SQLITE_OK ? "ok" : "failed"));
35078#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
35079 if( (rc & 0xff) == SQLITE_IOERR10 ){
35080 rc = SQLITE_BUSY5;
35081 }
35082#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
35083 return rc;
35084}
35085
35086
35087/*
35088** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35089** must be either NO_LOCK or SHARED_LOCK.
35090**
35091** If the locking level of the file descriptor is already at or below
35092** the requested locking level, this routine is a no-op.
35093*/
35094static int flockUnlock(sqlite3_file *id, int eFileLock) {
35095 unixFile *pFile = (unixFile*)id;
35096
35097 assert( pFile )((void) (0));
35098 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
35099 pFile->eFileLock, osGetpid(0)));
35100 assert( eFileLock<=SHARED_LOCK )((void) (0));
35101
35102 /* no-op if possible */
35103 if( pFile->eFileLock==eFileLock ){
35104 return SQLITE_OK0;
35105 }
35106
35107 /* shared can just be set because we always have an exclusive */
35108 if (eFileLock==SHARED_LOCK1) {
35109 pFile->eFileLock = eFileLock;
35110 return SQLITE_OK0;
35111 }
35112
35113 /* no, really, unlock. */
35114 if( robust_flock(pFile->h, LOCK_UN8) ){
35115#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
35116 return SQLITE_OK0;
35117#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
35118 return SQLITE_IOERR_UNLOCK(10 | (8<<8));
35119 }else{
35120 pFile->eFileLock = NO_LOCK0;
35121 return SQLITE_OK0;
35122 }
35123}
35124
35125/*
35126** Close a file.
35127*/
35128static int flockClose(sqlite3_file *id) {
35129 assert( id!=0 )((void) (0));
35130 flockUnlock(id, NO_LOCK0);
35131 return closeUnixFile(id);
35132}
35133
35134#endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
35135
35136/******************* End of the flock lock implementation *********************
35137******************************************************************************/
35138
35139/******************************************************************************
35140************************ Begin Named Semaphore Locking ************************
35141**
35142** Named semaphore locking is only supported on VxWorks.
35143**
35144** Semaphore locking is like dot-lock and flock in that it really only
35145** supports EXCLUSIVE locking. Only a single process can read or write
35146** the database file at a time. This reduces potential concurrency, but
35147** makes the lock implementation much easier.
35148*/
35149#if OS_VXWORKS0
35150
35151/*
35152** This routine checks if there is a RESERVED lock held on the specified
35153** file by this or any other process. If such a lock is held, set *pResOut
35154** to a non-zero value otherwise *pResOut is set to zero. The return value
35155** is set to SQLITE_OK unless an I/O error occurs during lock checking.
35156*/
35157static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
35158 int rc = SQLITE_OK0;
35159 int reserved = 0;
35160 unixFile *pFile = (unixFile*)id;
35161
35162 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
35163
35164 assert( pFile )((void) (0));
35165
35166 /* Check if a thread in this process holds such a lock */
35167 if( pFile->eFileLock>SHARED_LOCK1 ){
35168 reserved = 1;
35169 }
35170
35171 /* Otherwise see if some other process holds it. */
35172 if( !reserved ){
35173 sem_t *pSem = pFile->pInode->pSem;
35174
35175 if( sem_trywait(pSem)==-1 ){
35176 int tErrno = errno(*__errno_location ());
35177 if( EAGAIN11 != tErrno ){
35178 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)));
35179 storeLastErrno(pFile, tErrno);
35180 } else {
35181 /* someone else has the lock when we are in NO_LOCK */
35182 reserved = (pFile->eFileLock < SHARED_LOCK1);
35183 }
35184 }else{
35185 /* we could have it if we want it */
35186 sem_post(pSem);
35187 }
35188 }
35189 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
35190
35191 *pResOut = reserved;
35192 return rc;
35193}
35194
35195/*
35196** Lock the file with the lock specified by parameter eFileLock - one
35197** of the following:
35198**
35199** (1) SHARED_LOCK
35200** (2) RESERVED_LOCK
35201** (3) PENDING_LOCK
35202** (4) EXCLUSIVE_LOCK
35203**
35204** Sometimes when requesting one lock state, additional lock states
35205** are inserted in between. The locking might fail on one of the later
35206** transitions leaving the lock state different from what it started but
35207** still short of its goal. The following chart shows the allowed
35208** transitions and the inserted intermediate states:
35209**
35210** UNLOCKED -> SHARED
35211** SHARED -> RESERVED
35212** SHARED -> (PENDING) -> EXCLUSIVE
35213** RESERVED -> (PENDING) -> EXCLUSIVE
35214** PENDING -> EXCLUSIVE
35215**
35216** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
35217** lock states in the sqlite3_file structure, but all locks SHARED or
35218** above are really EXCLUSIVE locks and exclude all other processes from
35219** access the file.
35220**
35221** This routine will only increase a lock. Use the sqlite3OsUnlock()
35222** routine to lower a locking level.
35223*/
35224static int semXLock(sqlite3_file *id, int eFileLock) {
35225 unixFile *pFile = (unixFile*)id;
35226 sem_t *pSem = pFile->pInode->pSem;
35227 int rc = SQLITE_OK0;
35228
35229 /* if we already have a lock, it is exclusive.
35230 ** Just adjust level and punt on outta here. */
35231 if (pFile->eFileLock > NO_LOCK0) {
35232 pFile->eFileLock = eFileLock;
35233 rc = SQLITE_OK0;
35234 goto sem_end_lock;
35235 }
35236
35237 /* lock semaphore now but bail out when already locked. */
35238 if( sem_trywait(pSem)==-1 ){
35239 rc = SQLITE_BUSY5;
35240 goto sem_end_lock;
35241 }
35242
35243 /* got it, set the type and return ok */
35244 pFile->eFileLock = eFileLock;
35245
35246 sem_end_lock:
35247 return rc;
35248}
35249
35250/*
35251** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35252** must be either NO_LOCK or SHARED_LOCK.
35253**
35254** If the locking level of the file descriptor is already at or below
35255** the requested locking level, this routine is a no-op.
35256*/
35257static int semXUnlock(sqlite3_file *id, int eFileLock) {
35258 unixFile *pFile = (unixFile*)id;
35259 sem_t *pSem = pFile->pInode->pSem;
35260
35261 assert( pFile )((void) (0));
35262 assert( pSem )((void) (0));
35263 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
35264 pFile->eFileLock, osGetpid(0)));
35265 assert( eFileLock<=SHARED_LOCK )((void) (0));
35266
35267 /* no-op if possible */
35268 if( pFile->eFileLock==eFileLock ){
35269 return SQLITE_OK0;
35270 }
35271
35272 /* shared can just be set because we always have an exclusive */
35273 if (eFileLock==SHARED_LOCK1) {
35274 pFile->eFileLock = eFileLock;
35275 return SQLITE_OK0;
35276 }
35277
35278 /* no, really unlock. */
35279 if ( sem_post(pSem)==-1 ) {
35280 int rc, tErrno = errno(*__errno_location ());
35281 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK(10 | (8<<8)));
35282 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
35283 storeLastErrno(pFile, tErrno);
35284 }
35285 return rc;
35286 }
35287 pFile->eFileLock = NO_LOCK0;
35288 return SQLITE_OK0;
35289}
35290
35291/*
35292 ** Close a file.
35293 */
35294static int semXClose(sqlite3_file *id) {
35295 if( id ){
35296 unixFile *pFile = (unixFile*)id;
35297 semXUnlock(id, NO_LOCK0);
35298 assert( pFile )((void) (0));
35299 assert( unixFileMutexNotheld(pFile) )((void) (0));
35300 unixEnterMutex();
35301 releaseInodeInfo(pFile);
35302 unixLeaveMutex();
35303 closeUnixFile(id);
35304 }
35305 return SQLITE_OK0;
35306}
35307
35308#endif /* OS_VXWORKS */
35309/*
35310** Named semaphore locking is only available on VxWorks.
35311**
35312*************** End of the named semaphore lock implementation ****************
35313******************************************************************************/
35314
35315
35316/******************************************************************************
35317*************************** Begin AFP Locking *********************************
35318**
35319** AFP is the Apple Filing Protocol. AFP is a network filesystem found
35320** on Apple Macintosh computers - both OS9 and OSX.
35321**
35322** Third-party implementations of AFP are available. But this code here
35323** only works on OSX.
35324*/
35325
35326#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
35327/*
35328** The afpLockingContext structure contains all afp lock specific state
35329*/
35330typedef struct afpLockingContext afpLockingContext;
35331struct afpLockingContext {
35332 int reserved;
35333 const char *dbPath; /* Name of the open file */
35334};
35335
35336struct ByteRangeLockPB2
35337{
35338 unsigned long long offset; /* offset to first byte to lock */
35339 unsigned long long length; /* nbr of bytes to lock */
35340 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
35341 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
35342 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
35343 int fd; /* file desc to assoc this lock with */
35344};
35345
35346#define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)(((2U|1U) << (((0 +8)+8)+14)) | ((('z')) << (0 +8
)) | (((23)) << 0) | ((((sizeof(struct ByteRangeLockPB2
)))) << ((0 +8)+8)))
35347
35348/*
35349** This is a utility for setting or clearing a bit-range lock on an
35350** AFP filesystem.
35351**
35352** Return SQLITE_OK on success, SQLITE_BUSY on failure.
35353*/
35354static int afpSetLock(
35355 const char *path, /* Name of the file to be locked or unlocked */
35356 unixFile *pFile, /* Open file descriptor on path */
35357 unsigned long long offset, /* First byte to be locked */
35358 unsigned long long length, /* Number of bytes to lock */
35359 int setLockFlag /* True to set lock. False to clear lock */
35360){
35361 struct ByteRangeLockPB2 pb;
35362 int err;
35363
35364 pb.unLockFlag = setLockFlag ? 0 : 1;
35365 pb.startEndFlag = 0;
35366 pb.offset = offset;
35367 pb.length = length;
35368 pb.fd = pFile->h;
35369
35370 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
35371 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
35372 offset, length));
35373 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
35374 if ( err==-1 ) {
35375 int rc;
35376 int tErrno = errno(*__errno_location ());
35377 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
35378 path, tErrno, strerror(tErrno)));
35379#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
35380 rc = SQLITE_BUSY5;
35381#else
35382 rc = sqliteErrorFromPosixError(tErrno,
35383 setLockFlag ? SQLITE_IOERR_LOCK(10 | (15<<8)) : SQLITE_IOERR_UNLOCK(10 | (8<<8)));
35384#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
35385 if( IS_LOCK_ERROR(rc)((rc != 0) && (rc != 5)) ){
35386 storeLastErrno(pFile, tErrno);
35387 }
35388 return rc;
35389 } else {
35390 return SQLITE_OK0;
35391 }
35392}
35393
35394/*
35395** This routine checks if there is a RESERVED lock held on the specified
35396** file by this or any other process. If such a lock is held, set *pResOut
35397** to a non-zero value otherwise *pResOut is set to zero. The return value
35398** is set to SQLITE_OK unless an I/O error occurs during lock checking.
35399*/
35400static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
35401 int rc = SQLITE_OK0;
35402 int reserved = 0;
35403 unixFile *pFile = (unixFile*)id;
35404 afpLockingContext *context;
35405
35406 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
35407
35408 assert( pFile )((void) (0));
35409 context = (afpLockingContext *) pFile->lockingContext;
35410 if( context->reserved ){
35411 *pResOut = 1;
35412 return SQLITE_OK0;
35413 }
35414 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
35415 /* Check if a thread in this process holds such a lock */
35416 if( pFile->pInode->eFileLock>SHARED_LOCK1 ){
35417 reserved = 1;
35418 }
35419
35420 /* Otherwise see if some other process holds it.
35421 */
35422 if( !reserved ){
35423 /* lock the RESERVED byte */
35424 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1,1);
35425 if( SQLITE_OK0==lrc ){
35426 /* if we succeeded in taking the reserved lock, unlock it to restore
35427 ** the original state */
35428 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1, 0);
35429 } else {
35430 /* if we failed to get the lock then someone else must have it */
35431 reserved = 1;
35432 }
35433 if( IS_LOCK_ERROR(lrc)((lrc != 0) && (lrc != 5)) ){
35434 rc=lrc;
35435 }
35436 }
35437
35438 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
35439 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
35440
35441 *pResOut = reserved;
35442 return rc;
35443}
35444
35445/*
35446** Lock the file with the lock specified by parameter eFileLock - one
35447** of the following:
35448**
35449** (1) SHARED_LOCK
35450** (2) RESERVED_LOCK
35451** (3) PENDING_LOCK
35452** (4) EXCLUSIVE_LOCK
35453**
35454** Sometimes when requesting one lock state, additional lock states
35455** are inserted in between. The locking might fail on one of the later
35456** transitions leaving the lock state different from what it started but
35457** still short of its goal. The following chart shows the allowed
35458** transitions and the inserted intermediate states:
35459**
35460** UNLOCKED -> SHARED
35461** SHARED -> RESERVED
35462** SHARED -> (PENDING) -> EXCLUSIVE
35463** RESERVED -> (PENDING) -> EXCLUSIVE
35464** PENDING -> EXCLUSIVE
35465**
35466** This routine will only increase a lock. Use the sqlite3OsUnlock()
35467** routine to lower a locking level.
35468*/
35469static int afpLock(sqlite3_file *id, int eFileLock){
35470 int rc = SQLITE_OK0;
35471 unixFile *pFile = (unixFile*)id;
35472 unixInodeInfo *pInode = pFile->pInode;
35473 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
35474
35475 assert( pFile )((void) (0));
35476 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
35477 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
35478 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
35479
35480 /* If there is already a lock of this type or more restrictive on the
35481 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
35482 ** unixEnterMutex() hasn't been called yet.
35483 */
35484 if( pFile->eFileLock>=eFileLock ){
35485 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
35486 azFileLock(eFileLock)));
35487 return SQLITE_OK0;
35488 }
35489
35490 /* Make sure the locking sequence is correct
35491 ** (1) We never move from unlocked to anything higher than shared lock.
35492 ** (2) SQLite never explicitly requests a pendig lock.
35493 ** (3) A shared lock is always held when a reserve lock is requested.
35494 */
35495 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK )((void) (0));
35496 assert( eFileLock!=PENDING_LOCK )((void) (0));
35497 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK )((void) (0));
35498
35499 /* This mutex is needed because pFile->pInode is shared across threads
35500 */
35501 pInode = pFile->pInode;
35502 sqlite3_mutex_enter(pInode->pLockMutex);
35503
35504 /* If some thread using this PID has a lock via a different unixFile*
35505 ** handle that precludes the requested lock, return BUSY.
35506 */
35507 if( (pFile->eFileLock!=pInode->eFileLock &&
35508 (pInode->eFileLock>=PENDING_LOCK3 || eFileLock>SHARED_LOCK1))
35509 ){
35510 rc = SQLITE_BUSY5;
35511 goto afp_end_lock;
35512 }
35513
35514 /* If a SHARED lock is requested, and some thread using this PID already
35515 ** has a SHARED or RESERVED lock, then increment reference counts and
35516 ** return SQLITE_OK.
35517 */
35518 if( eFileLock==SHARED_LOCK1 &&
35519 (pInode->eFileLock==SHARED_LOCK1 || pInode->eFileLock==RESERVED_LOCK2) ){
35520 assert( eFileLock==SHARED_LOCK )((void) (0));
35521 assert( pFile->eFileLock==0 )((void) (0));
35522 assert( pInode->nShared>0 )((void) (0));
35523 pFile->eFileLock = SHARED_LOCK1;
35524 pInode->nShared++;
35525 pInode->nLock++;
35526 goto afp_end_lock;
35527 }
35528
35529 /* A PENDING lock is needed before acquiring a SHARED lock and before
35530 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
35531 ** be released.
35532 */
35533 if( eFileLock==SHARED_LOCK1
35534 || (eFileLock==EXCLUSIVE_LOCK4 && pFile->eFileLock<PENDING_LOCK3)
35535 ){
35536 int failed;
35537 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 1);
35538 if (failed) {
35539 rc = failed;
35540 goto afp_end_lock;
35541 }
35542 }
35543
35544 /* If control gets to this point, then actually go ahead and make
35545 ** operating system calls for the specified lock.
35546 */
35547 if( eFileLock==SHARED_LOCK1 ){
35548 int lrc1, lrc2, lrc1Errno = 0;
35549 long lk, mask;
35550
35551 assert( pInode->nShared==0 )((void) (0));
35552 assert( pInode->eFileLock==0 )((void) (0));
35553
35554 mask = (sizeof(long)==8) ? LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) : 0x7fffffff;
35555 /* Now get the read-lock SHARED_LOCK */
35556 /* note that the quality of the randomness doesn't matter that much */
35557 lk = random();
35558 pInode->sharedByte = (lk & mask)%(SHARED_SIZE510 - 1);
35559 lrc1 = afpSetLock(context->dbPath, pFile,
35560 SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte, 1, 1);
35561 if( IS_LOCK_ERROR(lrc1)((lrc1 != 0) && (lrc1 != 5)) ){
35562 lrc1Errno = pFile->lastErrno;
35563 }
35564 /* Drop the temporary PENDING lock */
35565 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 0);
35566
35567 if( IS_LOCK_ERROR(lrc1)((lrc1 != 0) && (lrc1 != 5)) ) {
35568 storeLastErrno(pFile, lrc1Errno);
35569 rc = lrc1;
35570 goto afp_end_lock;
35571 } else if( IS_LOCK_ERROR(lrc2)((lrc2 != 0) && (lrc2 != 5)) ){
35572 rc = lrc2;
35573 goto afp_end_lock;
35574 } else if( lrc1 != SQLITE_OK0 ) {
35575 rc = lrc1;
35576 } else {
35577 pFile->eFileLock = SHARED_LOCK1;
35578 pInode->nLock++;
35579 pInode->nShared = 1;
35580 }
35581 }else if( eFileLock==EXCLUSIVE_LOCK4 && pInode->nShared>1 ){
35582 /* We are trying for an exclusive lock but another thread in this
35583 ** same process is still holding a shared lock. */
35584 rc = SQLITE_BUSY5;
35585 }else{
35586 /* The request was for a RESERVED or EXCLUSIVE lock. It is
35587 ** assumed that there is a SHARED or greater lock on the file
35588 ** already.
35589 */
35590 int failed = 0;
35591 assert( 0!=pFile->eFileLock )((void) (0));
35592 if (eFileLock >= RESERVED_LOCK2 && pFile->eFileLock < RESERVED_LOCK2) {
35593 /* Acquire a RESERVED lock */
35594 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1,1);
35595 if( !failed ){
35596 context->reserved = 1;
35597 }
35598 }
35599 if (!failed && eFileLock == EXCLUSIVE_LOCK4) {
35600 /* Acquire an EXCLUSIVE lock */
35601
35602 /* Remove the shared lock before trying the range. we'll need to
35603 ** reestablish the shared lock if we can't get the afpUnlock
35604 */
35605 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2) +
35606 pInode->sharedByte, 1, 0)) ){
35607 int failed2 = SQLITE_OK0;
35608 /* now attemmpt to get the exclusive lock range */
35609 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2),
35610 SHARED_SIZE510, 1);
35611 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
35612 SHARED_FIRST(sqlite3PendingByte+2) + pInode->sharedByte, 1, 1)) ){
35613 /* Can't reestablish the shared lock. Sqlite can't deal, this is
35614 ** a critical I/O error
35615 */
35616 rc = ((failed & 0xff) == SQLITE_IOERR10) ? failed2 :
35617 SQLITE_IOERR_LOCK(10 | (15<<8));
35618 goto afp_end_lock;
35619 }
35620 }else{
35621 rc = failed;
35622 }
35623 }
35624 if( failed ){
35625 rc = failed;
35626 }
35627 }
35628
35629 if( rc==SQLITE_OK0 ){
35630 pFile->eFileLock = eFileLock;
35631 pInode->eFileLock = eFileLock;
35632 }else if( eFileLock==EXCLUSIVE_LOCK4 ){
35633 pFile->eFileLock = PENDING_LOCK3;
35634 pInode->eFileLock = PENDING_LOCK3;
35635 }
35636
35637afp_end_lock:
35638 sqlite3_mutex_leave(pInode->pLockMutex);
35639 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
35640 rc==SQLITE_OK ? "ok" : "failed"));
35641 return rc;
35642}
35643
35644/*
35645** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35646** must be either NO_LOCK or SHARED_LOCK.
35647**
35648** If the locking level of the file descriptor is already at or below
35649** the requested locking level, this routine is a no-op.
35650*/
35651static int afpUnlock(sqlite3_file *id, int eFileLock) {
35652 int rc = SQLITE_OK0;
35653 unixFile *pFile = (unixFile*)id;
35654 unixInodeInfo *pInode;
35655 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
35656 int skipShared = 0;
35657#ifdef SQLITE_TEST
35658 int h = pFile->h;
35659#endif
35660
35661 assert( pFile )((void) (0));
35662 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
35663 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
35664 osGetpid(0)));
35665
35666 assert( eFileLock<=SHARED_LOCK )((void) (0));
35667 if( pFile->eFileLock<=eFileLock ){
35668 return SQLITE_OK0;
35669 }
35670 pInode = pFile->pInode;
35671 sqlite3_mutex_enter(pInode->pLockMutex);
35672 assert( pInode->nShared!=0 )((void) (0));
35673 if( pFile->eFileLock>SHARED_LOCK1 ){
35674 assert( pInode->eFileLock==pFile->eFileLock )((void) (0));
35675 SimulateIOErrorBenign(1);
35676 SimulateIOError( h=(-1) )
35677 SimulateIOErrorBenign(0);
35678
35679#ifdef SQLITE_DEBUG
35680 /* When reducing a lock such that other processes can start
35681 ** reading the database file again, make sure that the
35682 ** transaction counter was updated if any part of the database
35683 ** file changed. If the transaction counter is not updated,
35684 ** other connections to the same file might not realize that
35685 ** the file has changed and hence might not know to flush their
35686 ** cache. The use of a stale cache can lead to database corruption.
35687 */
35688 assert( pFile->inNormalWrite==0((void) (0))
35689 || pFile->dbUpdate==0((void) (0))
35690 || pFile->transCntrChng==1 )((void) (0));
35691 pFile->inNormalWrite = 0;
35692#endif
35693
35694 if( pFile->eFileLock==EXCLUSIVE_LOCK4 ){
35695 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST(sqlite3PendingByte+2), SHARED_SIZE510, 0);
35696 if( rc==SQLITE_OK0 && (eFileLock==SHARED_LOCK1 || pInode->nShared>1) ){
35697 /* only re-establish the shared lock if necessary */
35698 int sharedLockByte = SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte;
35699 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
35700 } else {
35701 skipShared = 1;
35702 }
35703 }
35704 if( rc==SQLITE_OK0 && pFile->eFileLock>=PENDING_LOCK3 ){
35705 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTEsqlite3PendingByte, 1, 0);
35706 }
35707 if( rc==SQLITE_OK0 && pFile->eFileLock>=RESERVED_LOCK2 && context->reserved ){
35708 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE(sqlite3PendingByte+1), 1, 0);
35709 if( !rc ){
35710 context->reserved = 0;
35711 }
35712 }
35713 if( rc==SQLITE_OK0 && (eFileLock==SHARED_LOCK1 || pInode->nShared>1)){
35714 pInode->eFileLock = SHARED_LOCK1;
35715 }
35716 }
35717 if( rc==SQLITE_OK0 && eFileLock==NO_LOCK0 ){
35718
35719 /* Decrement the shared lock counter. Release the lock using an
35720 ** OS call only when all threads in this same process have released
35721 ** the lock.
35722 */
35723 unsigned long long sharedLockByte = SHARED_FIRST(sqlite3PendingByte+2)+pInode->sharedByte;
35724 pInode->nShared--;
35725 if( pInode->nShared==0 ){
35726 SimulateIOErrorBenign(1);
35727 SimulateIOError( h=(-1) )
35728 SimulateIOErrorBenign(0);
35729 if( !skipShared ){
35730 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
35731 }
35732 if( !rc ){
35733 pInode->eFileLock = NO_LOCK0;
35734 pFile->eFileLock = NO_LOCK0;
35735 }
35736 }
35737 if( rc==SQLITE_OK0 ){
35738 pInode->nLock--;
35739 assert( pInode->nLock>=0 )((void) (0));
35740 if( pInode->nLock==0 ) closePendingFds(pFile);
35741 }
35742 }
35743
35744 sqlite3_mutex_leave(pInode->pLockMutex);
35745 if( rc==SQLITE_OK0 ){
35746 pFile->eFileLock = eFileLock;
35747 }
35748 return rc;
35749}
35750
35751/*
35752** Close a file & cleanup AFP specific locking context
35753*/
35754static int afpClose(sqlite3_file *id) {
35755 int rc = SQLITE_OK0;
35756 unixFile *pFile = (unixFile*)id;
35757 assert( id!=0 )((void) (0));
35758 afpUnlock(id, NO_LOCK0);
35759 assert( unixFileMutexNotheld(pFile) )((void) (0));
35760 unixEnterMutex();
35761 if( pFile->pInode ){
35762 unixInodeInfo *pInode = pFile->pInode;
35763 sqlite3_mutex_enter(pInode->pLockMutex);
35764 if( pInode->nLock ){
35765 /* If there are outstanding locks, do not actually close the file just
35766 ** yet because that would clear those locks. Instead, add the file
35767 ** descriptor to pInode->aPending. It will be automatically closed when
35768 ** the last lock is cleared.
35769 */
35770 setPendingFd(pFile);
35771 }
35772 sqlite3_mutex_leave(pInode->pLockMutex);
35773 }
35774 releaseInodeInfo(pFile);
35775 sqlite3_free(pFile->lockingContext);
35776 rc = closeUnixFile(id);
35777 unixLeaveMutex();
35778 return rc;
35779}
35780
35781#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
35782/*
35783** The code above is the AFP lock implementation. The code is specific
35784** to MacOSX and does not work on other unix platforms. No alternative
35785** is available. If you don't compile for a mac, then the "unix-afp"
35786** VFS is not available.
35787**
35788********************* End of the AFP lock implementation **********************
35789******************************************************************************/
35790
35791/******************************************************************************
35792*************************** Begin NFS Locking ********************************/
35793
35794#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
35795/*
35796 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
35797 ** must be either NO_LOCK or SHARED_LOCK.
35798 **
35799 ** If the locking level of the file descriptor is already at or below
35800 ** the requested locking level, this routine is a no-op.
35801 */
35802static int nfsUnlock(sqlite3_file *id, int eFileLock){
35803 return posixUnlock(id, eFileLock, 1);
35804}
35805
35806#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
35807/*
35808** The code above is the NFS lock implementation. The code is specific
35809** to MacOSX and does not work on other unix platforms. No alternative
35810** is available.
35811**
35812********************* End of the NFS lock implementation **********************
35813******************************************************************************/
35814
35815/******************************************************************************
35816**************** Non-locking sqlite3_file methods *****************************
35817**
35818** The next division contains implementations for all methods of the
35819** sqlite3_file object other than the locking methods. The locking
35820** methods were defined in divisions above (one locking method per
35821** division). Those methods that are common to all locking modes
35822** are gather together into this division.
35823*/
35824
35825/*
35826** Seek to the offset passed as the second argument, then read cnt
35827** bytes into pBuf. Return the number of bytes actually read.
35828**
35829** NB: If you define USE_PREAD or USE_PREAD64, then it might also
35830** be necessary to define _XOPEN_SOURCE to be 500. This varies from
35831** one system to another. Since SQLite does not define USE_PREAD
35832** in any form by default, we will not attempt to define _XOPEN_SOURCE.
35833** See tickets #2741 and #2681.
35834**
35835** To avoid stomping the errno value on a failed read the lastErrno value
35836** is set before returning.
35837*/
35838static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
35839 int got;
35840 int prior = 0;
35841#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
35842 i64 newOffset;
35843#endif
35844 TIMER_START;
35845 assert( cnt==(cnt&0x1ffff) )((void) (0));
35846 assert( id->h>2 )((void) (0));
35847 do{
35848#if defined(USE_PREAD)
35849 got = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(id->h, pBuf, cnt, offset);
35850 SimulateIOError( got = -1 );
35851#elif defined(USE_PREAD64)
35852 got = osPread64((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)(id->h, pBuf, cnt, offset);
35853 SimulateIOError( got = -1 );
35854#else
35855 newOffset = lseek(id->h, offset, SEEK_SET0);
35856 SimulateIOError( newOffset = -1 );
35857 if( newOffset<0 ){
35858 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
35859 return -1;
35860 }
35861 got = osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)(id->h, pBuf, cnt);
35862#endif
35863 if( got==cnt ) break;
35864 if( got<0 ){
35865 if( errno(*__errno_location ())==EINTR4 ){ got = 1; continue; }
35866 prior = 0;
35867 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
35868 break;
35869 }else if( got>0 ){
35870 cnt -= got;
35871 offset += got;
35872 prior += got;
35873 pBuf = (void*)(got + (char*)pBuf);
35874 }
35875 }while( got>0 );
35876 TIMER_END;
35877 OSTRACE(("READ %-3d %5d %7lld %llu\n",
35878 id->h, got+prior, offset-prior, TIMER_ELAPSED));
35879 return got+prior;
35880}
35881
35882/*
35883** Read data from a file into a buffer. Return SQLITE_OK if all
35884** bytes were read successfully and SQLITE_IOERR if anything goes
35885** wrong.
35886*/
35887static int unixRead(
35888 sqlite3_file *id,
35889 void *pBuf,
35890 int amt,
35891 sqlite3_int64 offset
35892){
35893 unixFile *pFile = (unixFile *)id;
35894 int got;
35895 assert( id )((void) (0));
35896 assert( offset>=0 )((void) (0));
35897 assert( amt>0 )((void) (0));
35898
35899 /* If this is a database file (not a journal, master-journal or temp
35900 ** file), the bytes in the locking range should never be read or written. */
35901#if 0
35902 assert( pFile->pPreallocatedUnused==0((void) (0))
35903 || offset>=PENDING_BYTE+512((void) (0))
35904 || offset+amt<=PENDING_BYTE((void) (0))
35905 )((void) (0));
35906#endif
35907
35908#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
35909 /* Deal with as much of this read request as possible by transfering
35910 ** data from the memory mapping using memcpy(). */
35911 if( offset<pFile->mmapSize ){
35912 if( offset+amt <= pFile->mmapSize ){
35913 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
35914 return SQLITE_OK0;
35915 }else{
35916 int nCopy = pFile->mmapSize - offset;
35917 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
35918 pBuf = &((u8 *)pBuf)[nCopy];
35919 amt -= nCopy;
35920 offset += nCopy;
35921 }
35922 }
35923#endif
35924
35925 got = seekAndRead(pFile, offset, pBuf, amt);
35926 if( got==amt ){
35927 return SQLITE_OK0;
35928 }else if( got<0 ){
35929 /* lastErrno set by seekAndRead */
35930 return SQLITE_IOERR_READ(10 | (1<<8));
35931 }else{
35932 storeLastErrno(pFile, 0); /* not a system error */
35933 /* Unread parts of the buffer must be zero-filled */
35934 memset(&((char*)pBuf)[got], 0, amt-got);
35935 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
35936 }
35937}
35938
35939/*
35940** Attempt to seek the file-descriptor passed as the first argument to
35941** absolute offset iOff, then attempt to write nBuf bytes of data from
35942** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
35943** return the actual number of bytes written (which may be less than
35944** nBuf).
35945*/
35946static int seekAndWriteFd(
35947 int fd, /* File descriptor to write to */
35948 i64 iOff, /* File offset to begin writing at */
35949 const void *pBuf, /* Copy data from this buffer to the file */
35950 int nBuf, /* Size of buffer pBuf in bytes */
35951 int *piErrno /* OUT: Error number if error occurs */
35952){
35953 int rc = 0; /* Value returned by system call */
35954
35955 assert( nBuf==(nBuf&0x1ffff) )((void) (0));
35956 assert( fd>2 )((void) (0));
35957 assert( piErrno!=0 )((void) (0));
35958 nBuf &= 0x1ffff;
35959 TIMER_START;
35960
35961#if defined(USE_PREAD)
35962 do{ rc = (int)osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
(fd, pBuf, nBuf, iOff); }while( rc<0 && errno(*__errno_location ())==EINTR4 );
35963#elif defined(USE_PREAD64)
35964 do{ rc = (int)osPwrite64((ssize_t(*)(int,const void*,size_t,off64_t)) aSyscall[13].pCurrent
)
(fd, pBuf, nBuf, iOff);}while( rc<0 && errno(*__errno_location ())==EINTR4);
35965#else
35966 do{
35967 i64 iSeek = lseek(fd, iOff, SEEK_SET0);
35968 SimulateIOError( iSeek = -1 );
35969 if( iSeek<0 ){
35970 rc = -1;
35971 break;
35972 }
35973 rc = osWrite((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)(fd, pBuf, nBuf);
35974 }while( rc<0 && errno(*__errno_location ())==EINTR4 );
35975#endif
35976
35977 TIMER_END;
35978 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
35979
35980 if( rc<0 ) *piErrno = errno(*__errno_location ());
35981 return rc;
35982}
35983
35984
35985/*
35986** Seek to the offset in id->offset then read cnt bytes into pBuf.
35987** Return the number of bytes actually read. Update the offset.
35988**
35989** To avoid stomping the errno value on a failed write the lastErrno value
35990** is set before returning.
35991*/
35992static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
35993 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
35994}
35995
35996
35997/*
35998** Write data from a buffer into a file. Return SQLITE_OK on success
35999** or some other error code on failure.
36000*/
36001static int unixWrite(
36002 sqlite3_file *id,
36003 const void *pBuf,
36004 int amt,
36005 sqlite3_int64 offset
36006){
36007 unixFile *pFile = (unixFile*)id;
36008 int wrote = 0;
36009 assert( id )((void) (0));
36010 assert( amt>0 )((void) (0));
36011
36012 /* If this is a database file (not a journal, master-journal or temp
36013 ** file), the bytes in the locking range should never be read or written. */
36014#if 0
36015 assert( pFile->pPreallocatedUnused==0((void) (0))
36016 || offset>=PENDING_BYTE+512((void) (0))
36017 || offset+amt<=PENDING_BYTE((void) (0))
36018 )((void) (0));
36019#endif
36020
36021#ifdef SQLITE_DEBUG
36022 /* If we are doing a normal write to a database file (as opposed to
36023 ** doing a hot-journal rollback or a write to some file other than a
36024 ** normal database file) then record the fact that the database
36025 ** has changed. If the transaction counter is modified, record that
36026 ** fact too.
36027 */
36028 if( pFile->inNormalWrite ){
36029 pFile->dbUpdate = 1; /* The database has been modified */
36030 if( offset<=24 && offset+amt>=27 ){
36031 int rc;
36032 char oldCntr[4];
36033 SimulateIOErrorBenign(1);
36034 rc = seekAndRead(pFile, 24, oldCntr, 4);
36035 SimulateIOErrorBenign(0);
36036 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
36037 pFile->transCntrChng = 1; /* The transaction counter has changed */
36038 }
36039 }
36040 }
36041#endif
36042
36043#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE0x7fff0000>0
36044 /* Deal with as much of this write request as possible by transfering
36045 ** data from the memory mapping using memcpy(). */
36046 if( offset<pFile->mmapSize ){
36047 if( offset+amt <= pFile->mmapSize ){
36048 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
36049 return SQLITE_OK0;
36050 }else{
36051 int nCopy = pFile->mmapSize - offset;
36052 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
36053 pBuf = &((u8 *)pBuf)[nCopy];
36054 amt -= nCopy;
36055 offset += nCopy;
36056 }
36057 }
36058#endif
36059
36060 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
36061 amt -= wrote;
36062 offset += wrote;
36063 pBuf = &((char*)pBuf)[wrote];
36064 }
36065 SimulateIOError(( wrote=(-1), amt=1 ));
36066 SimulateDiskfullError(( wrote=0, amt=1 ));
36067
36068 if( amt>wrote ){
36069 if( wrote<0 && pFile->lastErrno!=ENOSPC28 ){
36070 /* lastErrno set by seekAndWrite */
36071 return SQLITE_IOERR_WRITE(10 | (3<<8));
36072 }else{
36073 storeLastErrno(pFile, 0); /* not a system error */
36074 return SQLITE_FULL13;
36075 }
36076 }
36077
36078 return SQLITE_OK0;
36079}
36080
36081#ifdef SQLITE_TEST
36082/*
36083** Count the number of fullsyncs and normal syncs. This is used to test
36084** that syncs and fullsyncs are occurring at the right times.
36085*/
36086SQLITE_API int sqlite3_sync_count = 0;
36087SQLITE_API int sqlite3_fullsync_count = 0;
36088#endif
36089
36090/*
36091** We do not trust systems to provide a working fdatasync(). Some do.
36092** Others do no. To be safe, we will stick with the (slightly slower)
36093** fsync(). If you know that your system does support fdatasync() correctly,
36094** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
36095*/
36096#if !defined(fdatasyncfsync) && !HAVE_FDATASYNC
36097# define fdatasyncfsync fsync
36098#endif
36099
36100/*
36101** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
36102** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
36103** only available on Mac OS X. But that could change.
36104*/
36105#ifdef F_FULLFSYNC
36106# define HAVE_FULLFSYNC0 1
36107#else
36108# define HAVE_FULLFSYNC0 0
36109#endif
36110
36111
36112/*
36113** The fsync() system call does not work as advertised on many
36114** unix systems. The following procedure is an attempt to make
36115** it work better.
36116**
36117** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
36118** for testing when we want to run through the test suite quickly.
36119** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
36120** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
36121** or power failure will likely corrupt the database file.
36122**
36123** SQLite sets the dataOnly flag if the size of the file is unchanged.
36124** The idea behind dataOnly is that it should only write the file content
36125** to disk, not the inode. We only set dataOnly if the file size is
36126** unchanged since the file size is part of the inode. However,
36127** Ted Ts'o tells us that fdatasync() will also write the inode if the
36128** file size has changed. The only real difference between fdatasync()
36129** and fsync(), Ted tells us, is that fdatasync() will not flush the
36130** inode if the mtime or owner or other inode attributes have changed.
36131** We only care about the file size, not the other file attributes, so
36132** as far as SQLite is concerned, an fdatasync() is always adequate.
36133** So, we always use fdatasync() if it is available, regardless of
36134** the value of the dataOnly flag.
36135*/
36136static int full_fsync(int fd, int fullSync, int dataOnly){
36137 int rc;
36138
36139 /* The following "ifdef/elif/else/" block has the same structure as
36140 ** the one below. It is replicated here solely to avoid cluttering
36141 ** up the real code with the UNUSED_PARAMETER() macros.
36142 */
36143#ifdef SQLITE_NO_SYNC
36144 UNUSED_PARAMETER(fd)(void)(fd);
36145 UNUSED_PARAMETER(fullSync)(void)(fullSync);
36146 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
36147#elif HAVE_FULLFSYNC0
36148 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
36149#else
36150 UNUSED_PARAMETER(fullSync)(void)(fullSync);
36151 UNUSED_PARAMETER(dataOnly)(void)(dataOnly);
36152#endif
36153
36154 /* Record the number of times that we do a normal fsync() and
36155 ** FULLSYNC. This is used during testing to verify that this procedure
36156 ** gets called with the correct arguments.
36157 */
36158#ifdef SQLITE_TEST
36159 if( fullSync ) sqlite3_fullsync_count++;
36160 sqlite3_sync_count++;
36161#endif
36162
36163 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
36164 ** no-op. But go ahead and call fstat() to validate the file
36165 ** descriptor as we need a method to provoke a failure during
36166 ** coverate testing.
36167 */
36168#ifdef SQLITE_NO_SYNC
36169 {
36170 struct stat buf;
36171 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(fd, &buf);
36172 }
36173#elif HAVE_FULLFSYNC0
36174 if( fullSync ){
36175 rc = osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(fd, F_FULLFSYNC, 0);
36176 }else{
36177 rc = 1;
36178 }
36179 /* If the FULLFSYNC failed, fall back to attempting an fsync().
36180 ** It shouldn't be possible for fullfsync to fail on the local
36181 ** file system (on OSX), so failure indicates that FULLFSYNC
36182 ** isn't supported for this file system. So, attempt an fsync
36183 ** and (for now) ignore the overhead of a superfluous fcntl call.
36184 ** It'd be better to detect fullfsync support once and avoid
36185 ** the fcntl call every time sync is called.
36186 */
36187 if( rc ) rc = fsync(fd);
36188
36189#elif defined(__APPLE__)
36190 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
36191 ** so currently we default to the macro that redefines fdatasync to fsync
36192 */
36193 rc = fsync(fd);
36194#else
36195 rc = fdatasyncfsync(fd);
36196#if OS_VXWORKS0
36197 if( rc==-1 && errno(*__errno_location ())==ENOTSUP95 ){
36198 rc = fsync(fd);
36199 }
36200#endif /* OS_VXWORKS */
36201#endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
36202
36203 if( OS_VXWORKS0 && rc!= -1 ){
36204 rc = 0;
36205 }
36206 return rc;
36207}
36208
36209/*
36210** Open a file descriptor to the directory containing file zFilename.
36211** If successful, *pFd is set to the opened file descriptor and
36212** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
36213** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
36214** value.
36215**
36216** The directory file descriptor is used for only one thing - to
36217** fsync() a directory to make sure file creation and deletion events
36218** are flushed to disk. Such fsyncs are not needed on newer
36219** journaling filesystems, but are required on older filesystems.
36220**
36221** This routine can be overridden using the xSetSysCall interface.
36222** The ability to override this routine was added in support of the
36223** chromium sandbox. Opening a directory is a security risk (we are
36224** told) so making it overrideable allows the chromium sandbox to
36225** replace this routine with a harmless no-op. To make this routine
36226** a no-op, replace it with a stub that returns SQLITE_OK but leaves
36227** *pFd set to a negative number.
36228**
36229** If SQLITE_OK is returned, the caller is responsible for closing
36230** the file descriptor *pFd using close().
36231*/
36232static int openDirectory(const char *zFilename, int *pFd){
36233 int ii;
36234 int fd = -1;
36235 char zDirname[MAX_PATHNAME512+1];
36236
36237 sqlite3_snprintf(MAX_PATHNAME512, zDirname, "%s", zFilename);
36238 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
36239 if( ii>0 ){
36240 zDirname[ii] = '\0';
36241 }else{
36242 if( zDirname[0]!='/' ) zDirname[0] = '.';
36243 zDirname[1] = 0;
36244 }
36245 fd = robust_open(zDirname, O_RDONLY00|O_BINARY0, 0);
36246 if( fd>=0 ){
36247 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
36248 }
36249 *pFd = fd;
36250 if( fd>=0 ) return SQLITE_OK0;
36251 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname)unixLogErrorAtLine(sqlite3CantopenError(36251),"openDirectory"
,zDirname,36251)
;
36252}
36253
36254/*
36255** Make sure all writes to a particular file are committed to disk.
36256**
36257** If dataOnly==0 then both the file itself and its metadata (file
36258** size, access time, etc) are synced. If dataOnly!=0 then only the
36259** file data is synced.
36260**
36261** Under Unix, also make sure that the directory entry for the file
36262** has been created by fsync-ing the directory that contains the file.
36263** If we do not do this and we encounter a power failure, the directory
36264** entry for the journal might not exist after we reboot. The next
36265** SQLite to access the file will not know that the journal exists (because
36266** the directory entry for the journal was never created) and the transaction
36267** will not roll back - possibly leading to database corruption.
36268*/
36269static int unixSync(sqlite3_file *id, int flags){
36270 int rc;
36271 unixFile *pFile = (unixFile*)id;
36272
36273 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY0x00010);
36274 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL0x00003;
36275
36276 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
36277 assert((flags&0x0F)==SQLITE_SYNC_NORMAL((void) (0))
36278 || (flags&0x0F)==SQLITE_SYNC_FULL((void) (0))
36279 )((void) (0));
36280
36281 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
36282 ** line is to test that doing so does not cause any problems.
36283 */
36284 SimulateDiskfullError( return SQLITE_FULL );
36285
36286 assert( pFile )((void) (0));
36287 OSTRACE(("SYNC %-3d\n", pFile->h));
36288 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
36289 SimulateIOError( rc=1 );
36290 if( rc ){
36291 storeLastErrno(pFile, errno(*__errno_location ()));
36292 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath)unixLogErrorAtLine((10 | (4<<8)),"full_fsync",pFile->
zPath,36292)
;
36293 }
36294
36295 /* Also fsync the directory containing the file if the DIRSYNC flag
36296 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
36297 ** are unable to fsync a directory, so ignore errors on the fsync.
36298 */
36299 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC0x08 ){
36300 int dirfd;
36301 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
36302 HAVE_FULLFSYNC, isFullsync));
36303 rc = osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent)(pFile->zPath, &dirfd);
36304 if( rc==SQLITE_OK0 ){
36305 full_fsync(dirfd, 0, 0);
36306 robust_close(pFile, dirfd, __LINE__36306);
36307 }else{
36308 assert( rc==SQLITE_CANTOPEN )((void) (0));
36309 rc = SQLITE_OK0;
36310 }
36311 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC0x08;
36312 }
36313 return rc;
36314}
36315
36316/*
36317** Truncate an open file to a specified size
36318*/
36319static int unixTruncate(sqlite3_file *id, i64 nByte){
36320 unixFile *pFile = (unixFile *)id;
36321 int rc;
36322 assert( pFile )((void) (0));
36323 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
36324
36325 /* If the user has configured a chunk-size for this file, truncate the
36326 ** file so that it consists of an integer number of chunks (i.e. the
36327 ** actual file size after the operation may be larger than the requested
36328 ** size).
36329 */
36330 if( pFile->szChunk>0 ){
36331 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
36332 }
36333
36334 rc = robust_ftruncate(pFile->h, nByte);
36335 if( rc ){
36336 storeLastErrno(pFile, errno(*__errno_location ()));
36337 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath)unixLogErrorAtLine((10 | (6<<8)),"ftruncate",pFile->
zPath,36337)
;
36338 }else{
36339#ifdef SQLITE_DEBUG
36340 /* If we are doing a normal write to a database file (as opposed to
36341 ** doing a hot-journal rollback or a write to some file other than a
36342 ** normal database file) and we truncate the file to zero length,
36343 ** that effectively updates the change counter. This might happen
36344 ** when restoring a database using the backup API from a zero-length
36345 ** source.
36346 */
36347 if( pFile->inNormalWrite && nByte==0 ){
36348 pFile->transCntrChng = 1;
36349 }
36350#endif
36351
36352#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
36353 /* If the file was just truncated to a size smaller than the currently
36354 ** mapped region, reduce the effective mapping size as well. SQLite will
36355 ** use read() and write() to access data beyond this point from now on.
36356 */
36357 if( nByte<pFile->mmapSize ){
36358 pFile->mmapSize = nByte;
36359 }
36360#endif
36361
36362 return SQLITE_OK0;
36363 }
36364}
36365
36366/*
36367** Determine the current size of a file in bytes
36368*/
36369static int unixFileSize(sqlite3_file *id, i64 *pSize){
36370 int rc;
36371 struct stat buf;
36372 assert( id )((void) (0));
36373 rc = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(((unixFile*)id)->h, &buf);
36374 SimulateIOError( rc=1 );
36375 if( rc!=0 ){
36376 storeLastErrno((unixFile*)id, errno(*__errno_location ()));
36377 return SQLITE_IOERR_FSTAT(10 | (7<<8));
36378 }
36379 *pSize = buf.st_size;
36380
36381 /* When opening a zero-size database, the findInodeInfo() procedure
36382 ** writes a single byte into that file in order to work around a bug
36383 ** in the OS-X msdos filesystem. In order to avoid problems with upper
36384 ** layers, we need to report this file size as zero even though it is
36385 ** really 1. Ticket #3260.
36386 */
36387 if( *pSize==1 ) *pSize = 0;
36388
36389
36390 return SQLITE_OK0;
36391}
36392
36393#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
36394/*
36395** Handler for proxy-locking file-control verbs. Defined below in the
36396** proxying locking division.
36397*/
36398static int proxyFileControl(sqlite3_file*,int,void*);
36399#endif
36400
36401/*
36402** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
36403** file-control operation. Enlarge the database to nBytes in size
36404** (rounded up to the next chunk-size). If the database is already
36405** nBytes or larger, this routine is a no-op.
36406*/
36407static int fcntlSizeHint(unixFile *pFile, i64 nByte){
36408 if( pFile->szChunk>0 ){
36409 i64 nSize; /* Required file size */
36410 struct stat buf; /* Used to hold return values of fstat() */
36411
36412 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf) ){
36413 return SQLITE_IOERR_FSTAT(10 | (7<<8));
36414 }
36415
36416 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
36417 if( nSize>(i64)buf.st_size ){
36418
36419#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
36420 /* The code below is handling the return value of osFallocate()
36421 ** correctly. posix_fallocate() is defined to "returns zero on success,
36422 ** or an error number on failure". See the manpage for details. */
36423 int err;
36424 do{
36425 err = osFallocate((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)(pFile->h, buf.st_size, nSize-buf.st_size);
36426 }while( err==EINTR4 );
36427 if( err && err!=EINVAL22 ) return SQLITE_IOERR_WRITE(10 | (3<<8));
36428#else
36429 /* If the OS does not have posix_fallocate(), fake it. Write a
36430 ** single byte to the last byte in each block that falls entirely
36431 ** within the extended region. Then, if required, a single byte
36432 ** at offset (nSize-1), to set the size of the file correctly.
36433 ** This is a similar technique to that used by glibc on systems
36434 ** that do not have a real fallocate() call.
36435 */
36436 int nBlk = buf.st_blksize; /* File-system block size */
36437 int nWrite = 0; /* Number of bytes written by seekAndWrite */
36438 i64 iWrite; /* Next offset to write to */
36439
36440 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
36441 assert( iWrite>=buf.st_size )((void) (0));
36442 assert( ((iWrite+1)%nBlk)==0 )((void) (0));
36443 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
36444 if( iWrite>=nSize ) iWrite = nSize - 1;
36445 nWrite = seekAndWrite(pFile, iWrite, "", 1);
36446 if( nWrite!=1 ) return SQLITE_IOERR_WRITE(10 | (3<<8));
36447 }
36448#endif
36449 }
36450 }
36451
36452#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
36453 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
36454 int rc;
36455 if( pFile->szChunk<=0 ){
36456 if( robust_ftruncate(pFile->h, nByte) ){
36457 storeLastErrno(pFile, errno(*__errno_location ()));
36458 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath)unixLogErrorAtLine((10 | (6<<8)),"ftruncate",pFile->
zPath,36458)
;
36459 }
36460 }
36461
36462 rc = unixMapfile(pFile, nByte);
36463 return rc;
36464 }
36465#endif
36466
36467 return SQLITE_OK0;
36468}
36469
36470/*
36471** If *pArg is initially negative then this is a query. Set *pArg to
36472** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
36473**
36474** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
36475*/
36476static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
36477 if( *pArg<0 ){
36478 *pArg = (pFile->ctrlFlags & mask)!=0;
36479 }else if( (*pArg)==0 ){
36480 pFile->ctrlFlags &= ~mask;
36481 }else{
36482 pFile->ctrlFlags |= mask;
36483 }
36484}
36485
36486/* Forward declaration */
36487static int unixGetTempname(int nBuf, char *zBuf);
36488
36489/*
36490** Information and control of an open file handle.
36491*/
36492static int unixFileControl(sqlite3_file *id, int op, void *pArg){
36493 unixFile *pFile = (unixFile*)id;
36494 switch( op ){
36495#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
36496 case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31: {
36497 int rc = osIoctl((int(*)(int,int,...))aSyscall[28].pCurrent)(pFile->h, F2FS_IOC_START_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((1)) << 0) | ((0) << ((0 +8)+8)))
);
36498 return rc ? SQLITE_IOERR_BEGIN_ATOMIC(10 | (29<<8)) : SQLITE_OK0;
36499 }
36500 case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32: {
36501 int rc = osIoctl((int(*)(int,int,...))aSyscall[28].pCurrent)(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((2)) << 0) | ((0) << ((0 +8)+8)))
);
36502 return rc ? SQLITE_IOERR_COMMIT_ATOMIC(10 | (30<<8)) : SQLITE_OK0;
36503 }
36504 case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33: {
36505 int rc = osIoctl((int(*)(int,int,...))aSyscall[28].pCurrent)(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE(((0U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((5)) << 0) | ((0) << ((0 +8)+8)))
);
36506 return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC(10 | (31<<8)) : SQLITE_OK0;
36507 }
36508#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
36509
36510 case SQLITE_FCNTL_LOCKSTATE1: {
36511 *(int*)pArg = pFile->eFileLock;
36512 return SQLITE_OK0;
36513 }
36514 case SQLITE_FCNTL_LAST_ERRNO4: {
36515 *(int*)pArg = pFile->lastErrno;
36516 return SQLITE_OK0;
36517 }
36518 case SQLITE_FCNTL_CHUNK_SIZE6: {
36519 pFile->szChunk = *(int *)pArg;
36520 return SQLITE_OK0;
36521 }
36522 case SQLITE_FCNTL_SIZE_HINT5: {
36523 int rc;
36524 SimulateIOErrorBenign(1);
36525 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
36526 SimulateIOErrorBenign(0);
36527 return rc;
36528 }
36529 case SQLITE_FCNTL_PERSIST_WAL10: {
36530 unixModeBit(pFile, UNIXFILE_PERSIST_WAL0x04, (int*)pArg);
36531 return SQLITE_OK0;
36532 }
36533 case SQLITE_FCNTL_POWERSAFE_OVERWRITE13: {
36534 unixModeBit(pFile, UNIXFILE_PSOW0x10, (int*)pArg);
36535 return SQLITE_OK0;
36536 }
36537 case SQLITE_FCNTL_VFSNAME12: {
36538 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
36539 return SQLITE_OK0;
36540 }
36541 case SQLITE_FCNTL_TEMPFILENAME16: {
36542 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
36543 if( zTFile ){
36544 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
36545 *(char**)pArg = zTFile;
36546 }
36547 return SQLITE_OK0;
36548 }
36549 case SQLITE_FCNTL_HAS_MOVED20: {
36550 *(int*)pArg = fileHasMoved(pFile);
36551 return SQLITE_OK0;
36552 }
36553#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
36554 case SQLITE_FCNTL_LOCK_TIMEOUT34: {
36555 pFile->iBusyTimeout = *(int*)pArg;
36556 return SQLITE_OK0;
36557 }
36558#endif
36559#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
36560 case SQLITE_FCNTL_MMAP_SIZE18: {
36561 i64 newLimit = *(i64*)pArg;
36562 int rc = SQLITE_OK0;
36563 if( newLimit>sqlite3GlobalConfigsqlite3Config.mxMmap ){
36564 newLimit = sqlite3GlobalConfigsqlite3Config.mxMmap;
36565 }
36566
36567 /* The value of newLimit may be eventually cast to (size_t) and passed
36568 ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a
36569 ** 64-bit type. */
36570 if( newLimit>0 && sizeof(size_t)<8 ){
36571 newLimit = (newLimit & 0x7FFFFFFF);
36572 }
36573
36574 *(i64*)pArg = pFile->mmapSizeMax;
36575 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
36576 pFile->mmapSizeMax = newLimit;
36577 if( pFile->mmapSize>0 ){
36578 unixUnmapfile(pFile);
36579 rc = unixMapfile(pFile, -1);
36580 }
36581 }
36582 return rc;
36583 }
36584#endif
36585#ifdef SQLITE_DEBUG
36586 /* The pager calls this method to signal that it has done
36587 ** a rollback and that the database is therefore unchanged and
36588 ** it hence it is OK for the transaction change counter to be
36589 ** unchanged.
36590 */
36591 case SQLITE_FCNTL_DB_UNCHANGED0xca093fa0: {
36592 ((unixFile*)id)->dbUpdate = 0;
36593 return SQLITE_OK0;
36594 }
36595#endif
36596#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
36597 case SQLITE_FCNTL_SET_LOCKPROXYFILE3:
36598 case SQLITE_FCNTL_GET_LOCKPROXYFILE2: {
36599 return proxyFileControl(id,op,pArg);
36600 }
36601#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
36602 }
36603 return SQLITE_NOTFOUND12;
36604}
36605
36606/*
36607** If pFd->sectorSize is non-zero when this function is called, it is a
36608** no-op. Otherwise, the values of pFd->sectorSize and
36609** pFd->deviceCharacteristics are set according to the file-system
36610** characteristics.
36611**
36612** There are two versions of this function. One for QNX and one for all
36613** other systems.
36614*/
36615#ifndef __QNXNTO__
36616static void setDeviceCharacteristics(unixFile *pFd){
36617 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 )((void) (0));
36618 if( pFd->sectorSize==0 ){
36619#if defined(__linux__1) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
36620 int res;
36621 u32 f = 0;
36622
36623 /* Check for support for F2FS atomic batch writes. */
36624 res = osIoctl((int(*)(int,int,...))aSyscall[28].pCurrent)(pFd->h, F2FS_IOC_GET_FEATURES(((2U) << (((0 +8)+8)+14)) | (((0xf5)) << (0 +8))
| (((12)) << 0) | ((((sizeof(u32)))) << ((0 +8)+
8)))
, &f);
36625 if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE0x0004) ){
36626 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC0x00004000;
36627 }
36628#endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
36629
36630 /* Set the POWERSAFE_OVERWRITE flag if requested. */
36631 if( pFd->ctrlFlags & UNIXFILE_PSOW0x10 ){
36632 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000;
36633 }
36634
36635 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
36636 }
36637}
36638#else
36639#include <sys/dcmd_blk.h>
36640#include <sys/statvfs.h>
36641static void setDeviceCharacteristics(unixFile *pFile){
36642 if( pFile->sectorSize == 0 ){
36643 struct statvfs fsInfo;
36644
36645 /* Set defaults for non-supported filesystems */
36646 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
36647 pFile->deviceCharacteristics = 0;
36648 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
36649 return;
36650 }
36651
36652 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
36653 pFile->sectorSize = fsInfo.f_bsize;
36654 pFile->deviceCharacteristics =
36655 SQLITE_IOCAP_ATOMIC4K0x00000010 | /* All ram filesystem writes are atomic */
36656 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
36657 ** the write succeeds */
36658 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
36659 ** so it is ordered */
36660 0;
36661 }else if( strstr(fsInfo.f_basetype, "etfs") ){
36662 pFile->sectorSize = fsInfo.f_bsize;
36663 pFile->deviceCharacteristics =
36664 /* etfs cluster size writes are atomic */
36665 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) |
36666 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
36667 ** the write succeeds */
36668 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
36669 ** so it is ordered */
36670 0;
36671 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
36672 pFile->sectorSize = fsInfo.f_bsize;
36673 pFile->deviceCharacteristics =
36674 SQLITE_IOCAP_ATOMIC0x00000001 | /* All filesystem writes are atomic */
36675 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
36676 ** the write succeeds */
36677 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
36678 ** so it is ordered */
36679 0;
36680 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
36681 pFile->sectorSize = fsInfo.f_bsize;
36682 pFile->deviceCharacteristics =
36683 /* full bitset of atomics from max sector size and smaller */
36684 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) << 1) - 2 |
36685 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
36686 ** so it is ordered */
36687 0;
36688 }else if( strstr(fsInfo.f_basetype, "dos") ){
36689 pFile->sectorSize = fsInfo.f_bsize;
36690 pFile->deviceCharacteristics =
36691 /* full bitset of atomics from max sector size and smaller */
36692 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC5120x00000002) << 1) - 2 |
36693 SQLITE_IOCAP_SEQUENTIAL0x00000400 | /* The ram filesystem has no write behind
36694 ** so it is ordered */
36695 0;
36696 }else{
36697 pFile->deviceCharacteristics =
36698 SQLITE_IOCAP_ATOMIC5120x00000002 | /* blocks are atomic */
36699 SQLITE_IOCAP_SAFE_APPEND0x00000200 | /* growing the file does not occur until
36700 ** the write succeeds */
36701 0;
36702 }
36703 }
36704 /* Last chance verification. If the sector size isn't a multiple of 512
36705 ** then it isn't valid.*/
36706 if( pFile->sectorSize % 512 != 0 ){
36707 pFile->deviceCharacteristics = 0;
36708 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE4096;
36709 }
36710}
36711#endif
36712
36713/*
36714** Return the sector size in bytes of the underlying block device for
36715** the specified file. This is almost always 512 bytes, but may be
36716** larger for some devices.
36717**
36718** SQLite code assumes this function cannot fail. It also assumes that
36719** if two files are created in the same file-system directory (i.e.
36720** a database and its journal file) that the sector size will be the
36721** same for both.
36722*/
36723static int unixSectorSize(sqlite3_file *id){
36724 unixFile *pFd = (unixFile*)id;
36725 setDeviceCharacteristics(pFd);
36726 return pFd->sectorSize;
36727}
36728
36729/*
36730** Return the device characteristics for the file.
36731**
36732** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
36733** However, that choice is controversial since technically the underlying
36734** file system does not always provide powersafe overwrites. (In other
36735** words, after a power-loss event, parts of the file that were never
36736** written might end up being altered.) However, non-PSOW behavior is very,
36737** very rare. And asserting PSOW makes a large reduction in the amount
36738** of required I/O for journaling, since a lot of padding is eliminated.
36739** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
36740** available to turn it off and URI query parameter available to turn it off.
36741*/
36742static int unixDeviceCharacteristics(sqlite3_file *id){
36743 unixFile *pFd = (unixFile*)id;
36744 setDeviceCharacteristics(pFd);
36745 return pFd->deviceCharacteristics;
36746}
36747
36748#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0
36749
36750/*
36751** Return the system page size.
36752**
36753** This function should not be called directly by other code in this file.
36754** Instead, it should be called via macro osGetpagesize().
36755*/
36756static int unixGetpagesize(void){
36757#if OS_VXWORKS0
36758 return 1024;
36759#elif defined(_BSD_SOURCE1)
36760 return getpagesize();
36761#else
36762 return (int)sysconf(_SC_PAGESIZE_SC_PAGESIZE);
36763#endif
36764}
36765
36766#endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
36767
36768#ifndef SQLITE_OMIT_WAL
36769
36770/*
36771** Object used to represent an shared memory buffer.
36772**
36773** When multiple threads all reference the same wal-index, each thread
36774** has its own unixShm object, but they all point to a single instance
36775** of this unixShmNode object. In other words, each wal-index is opened
36776** only once per process.
36777**
36778** Each unixShmNode object is connected to a single unixInodeInfo object.
36779** We could coalesce this object into unixInodeInfo, but that would mean
36780** every open file that does not use shared memory (in other words, most
36781** open files) would have to carry around this extra information. So
36782** the unixInodeInfo object contains a pointer to this unixShmNode object
36783** and the unixShmNode object is created only when needed.
36784**
36785** unixMutexHeld() must be true when creating or destroying
36786** this object or while reading or writing the following fields:
36787**
36788** nRef
36789**
36790** The following fields are read-only after the object is created:
36791**
36792** hShm
36793** zFilename
36794**
36795** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
36796** unixMutexHeld() is true when reading or writing any other field
36797** in this structure.
36798*/
36799struct unixShmNode {
36800 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
36801 sqlite3_mutex *pShmMutex; /* Mutex to access this object */
36802 char *zFilename; /* Name of the mmapped file */
36803 int hShm; /* Open file descriptor */
36804 int szRegion; /* Size of shared-memory regions */
36805 u16 nRegion; /* Size of array apRegion */
36806 u8 isReadonly; /* True if read-only */
36807 u8 isUnlocked; /* True if no DMS lock held */
36808 char **apRegion; /* Array of mapped shared-memory regions */
36809 int nRef; /* Number of unixShm objects pointing to this */
36810 unixShm *pFirst; /* All unixShm objects pointing to this */
36811#ifdef SQLITE_DEBUG
36812 u8 exclMask; /* Mask of exclusive locks held */
36813 u8 sharedMask; /* Mask of shared locks held */
36814 u8 nextShmId; /* Next available unixShm.id value */
36815#endif
36816};
36817
36818/*
36819** Structure used internally by this VFS to record the state of an
36820** open shared memory connection.
36821**
36822** The following fields are initialized when this object is created and
36823** are read-only thereafter:
36824**
36825** unixShm.pShmNode
36826** unixShm.id
36827**
36828** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
36829** be held while accessing any read/write fields.
36830*/
36831struct unixShm {
36832 unixShmNode *pShmNode; /* The underlying unixShmNode object */
36833 unixShm *pNext; /* Next unixShm with the same unixShmNode */
36834 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
36835 u8 id; /* Id of this connection within its unixShmNode */
36836 u16 sharedMask; /* Mask of shared locks held */
36837 u16 exclMask; /* Mask of exclusive locks held */
36838};
36839
36840/*
36841** Constants used for locking
36842*/
36843#define UNIX_SHM_BASE((22+8)*4) ((22+SQLITE_SHM_NLOCK8)*4) /* first lock byte */
36844#define UNIX_SHM_DMS(((22+8)*4)+8) (UNIX_SHM_BASE((22+8)*4)+SQLITE_SHM_NLOCK8) /* deadman switch */
36845
36846/*
36847** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
36848**
36849** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
36850** otherwise.
36851*/
36852static int unixShmSystemLock(
36853 unixFile *pFile, /* Open connection to the WAL file */
36854 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
36855 int ofst, /* First byte of the locking range */
36856 int n /* Number of bytes to lock */
36857){
36858 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
36859 struct flock f; /* The posix advisory locking structure */
36860 int rc = SQLITE_OK0; /* Result code form fcntl() */
36861
36862 /* Access to the unixShmNode object is serialized by the caller */
36863 pShmNode = pFile->pInode->pShmNode;
36864 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) )((void) (0));
36865 assert( pShmNode->nRef>0 || unixMutexHeld() )((void) (0));
36866
36867 /* Shared locks never span more than one byte */
36868 assert( n==1 || lockType!=F_RDLCK )((void) (0));
36869
36870 /* Locks are within range */
36871 assert( n>=1 && n<=SQLITE_SHM_NLOCK )((void) (0));
36872
36873 if( pShmNode->hShm>=0 ){
36874 /* Initialize the locking parameters */
36875 f.l_type = lockType;
36876 f.l_whence = SEEK_SET0;
36877 f.l_start = ofst;
36878 f.l_len = n;
36879 rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile)((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm
,6,&f)
;
36880 rc = (rc!=(-1)) ? SQLITE_OK0 : SQLITE_BUSY5;
36881 }
36882
36883 /* Update the global lock state and do debug tracing */
36884#ifdef SQLITE_DEBUG
36885 { u16 mask;
36886 OSTRACE(("SHM-LOCK "));
36887 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
36888 if( rc==SQLITE_OK0 ){
36889 if( lockType==F_UNLCK2 ){
36890 OSTRACE(("unlock %d ok", ofst));
36891 pShmNode->exclMask &= ~mask;
36892 pShmNode->sharedMask &= ~mask;
36893 }else if( lockType==F_RDLCK0 ){
36894 OSTRACE(("read-lock %d ok", ofst));
36895 pShmNode->exclMask &= ~mask;
36896 pShmNode->sharedMask |= mask;
36897 }else{
36898 assert( lockType==F_WRLCK )((void) (0));
36899 OSTRACE(("write-lock %d ok", ofst));
36900 pShmNode->exclMask |= mask;
36901 pShmNode->sharedMask &= ~mask;
36902 }
36903 }else{
36904 if( lockType==F_UNLCK2 ){
36905 OSTRACE(("unlock %d failed", ofst));
36906 }else if( lockType==F_RDLCK0 ){
36907 OSTRACE(("read-lock failed"));
36908 }else{
36909 assert( lockType==F_WRLCK )((void) (0));
36910 OSTRACE(("write-lock %d failed", ofst));
36911 }
36912 }
36913 OSTRACE((" - afterwards %03x,%03x\n",
36914 pShmNode->sharedMask, pShmNode->exclMask));
36915 }
36916#endif
36917
36918 return rc;
36919}
36920
36921/*
36922** Return the minimum number of 32KB shm regions that should be mapped at
36923** a time, assuming that each mapping must be an integer multiple of the
36924** current system page-size.
36925**
36926** Usually, this is 1. The exception seems to be systems that are configured
36927** to use 64KB pages - in this case each mapping must cover at least two
36928** shm regions.
36929*/
36930static int unixShmRegionPerMap(void){
36931 int shmsz = 32*1024; /* SHM region size */
36932 int pgsz = osGetpagesize((int(*)(void))aSyscall[25].pCurrent)(); /* System page size */
36933 assert( ((pgsz-1)&pgsz)==0 )((void) (0)); /* Page size must be a power of 2 */
36934 if( pgsz<shmsz ) return 1;
36935 return pgsz/shmsz;
36936}
36937
36938/*
36939** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
36940**
36941** This is not a VFS shared-memory method; it is a utility function called
36942** by VFS shared-memory methods.
36943*/
36944static void unixShmPurge(unixFile *pFd){
36945 unixShmNode *p = pFd->pInode->pShmNode;
36946 assert( unixMutexHeld() )((void) (0));
36947 if( p && ALWAYS(p->nRef==0)(p->nRef==0) ){
36948 int nShmPerMap = unixShmRegionPerMap();
36949 int i;
36950 assert( p->pInode==pFd->pInode )((void) (0));
36951 sqlite3_mutex_free(p->pShmMutex);
36952 for(i=0; i<p->nRegion; i+=nShmPerMap){
36953 if( p->hShm>=0 ){
36954 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(p->apRegion[i], p->szRegion);
36955 }else{
36956 sqlite3_free(p->apRegion[i]);
36957 }
36958 }
36959 sqlite3_free(p->apRegion);
36960 if( p->hShm>=0 ){
36961 robust_close(pFd, p->hShm, __LINE__36961);
36962 p->hShm = -1;
36963 }
36964 p->pInode->pShmNode = 0;
36965 sqlite3_free(p);
36966 }
36967}
36968
36969/*
36970** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
36971** take it now. Return SQLITE_OK if successful, or an SQLite error
36972** code otherwise.
36973**
36974** If the DMS cannot be locked because this is a readonly_shm=1
36975** connection and no other process already holds a lock, return
36976** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
36977*/
36978static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
36979 struct flock lock;
36980 int rc = SQLITE_OK0;
36981
36982 /* Use F_GETLK to determine the locks other processes are holding
36983 ** on the DMS byte. If it indicates that another process is holding
36984 ** a SHARED lock, then this process may also take a SHARED lock
36985 ** and proceed with opening the *-shm file.
36986 **
36987 ** Or, if no other process is holding any lock, then this process
36988 ** is the first to open it. In this case take an EXCLUSIVE lock on the
36989 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
36990 ** downgrade to a SHARED lock on the DMS byte.
36991 **
36992 ** If another process is holding an EXCLUSIVE lock on the DMS byte,
36993 ** return SQLITE_BUSY to the caller (it will try again). An earlier
36994 ** version of this code attempted the SHARED lock at this point. But
36995 ** this introduced a subtle race condition: if the process holding
36996 ** EXCLUSIVE failed just before truncating the *-shm file, then this
36997 ** process might open and use the *-shm file without truncating it.
36998 ** And if the *-shm file has been corrupted by a power failure or
36999 ** system crash, the database itself may also become corrupt. */
37000 lock.l_whence = SEEK_SET0;
37001 lock.l_start = UNIX_SHM_DMS(((22+8)*4)+8);
37002 lock.l_len = 1;
37003 lock.l_type = F_WRLCK1;
37004 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pShmNode->hShm, F_GETLK5, &lock)!=0 ) {
37005 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
37006 }else if( lock.l_type==F_UNLCK2 ){
37007 if( pShmNode->isReadonly ){
37008 pShmNode->isUnlocked = 1;
37009 rc = SQLITE_READONLY_CANTINIT(8 | (5<<8));
37010 }else{
37011 rc = unixShmSystemLock(pDbFd, F_WRLCK1, UNIX_SHM_DMS(((22+8)*4)+8), 1);
37012 /* The first connection to attach must truncate the -shm file. We
37013 ** truncate to 3 bytes (an arbitrary small number, less than the
37014 ** -shm header size) rather than 0 as a system debugging aid, to
37015 ** help detect if a -shm file truncation is legitimate or is the work
37016 ** or a rogue process. */
37017 if( rc==SQLITE_OK0 && robust_ftruncate(pShmNode->hShm, 3) ){
37018 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename)unixLogErrorAtLine((10 | (18<<8)),"ftruncate",pShmNode->
zFilename,37018)
;
37019 }
37020 }
37021 }else if( lock.l_type==F_WRLCK1 ){
37022 rc = SQLITE_BUSY5;
37023 }
37024
37025 if( rc==SQLITE_OK0 ){
37026 assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK )((void) (0));
37027 rc = unixShmSystemLock(pDbFd, F_RDLCK0, UNIX_SHM_DMS(((22+8)*4)+8), 1);
37028 }
37029 return rc;
37030}
37031
37032/*
37033** Open a shared-memory area associated with open database file pDbFd.
37034** This particular implementation uses mmapped files.
37035**
37036** The file used to implement shared-memory is in the same directory
37037** as the open database file and has the same name as the open database
37038** file with the "-shm" suffix added. For example, if the database file
37039** is "/home/user1/config.db" then the file that is created and mmapped
37040** for shared memory will be called "/home/user1/config.db-shm".
37041**
37042** Another approach to is to use files in /dev/shm or /dev/tmp or an
37043** some other tmpfs mount. But if a file in a different directory
37044** from the database file is used, then differing access permissions
37045** or a chroot() might cause two different processes on the same
37046** database to end up using different files for shared memory -
37047** meaning that their memory would not really be shared - resulting
37048** in database corruption. Nevertheless, this tmpfs file usage
37049** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
37050** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
37051** option results in an incompatible build of SQLite; builds of SQLite
37052** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
37053** same database file at the same time, database corruption will likely
37054** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
37055** "unsupported" and may go away in a future SQLite release.
37056**
37057** When opening a new shared-memory file, if no other instances of that
37058** file are currently open, in this process or in other processes, then
37059** the file must be truncated to zero length or have its header cleared.
37060**
37061** If the original database file (pDbFd) is using the "unix-excl" VFS
37062** that means that an exclusive lock is held on the database file and
37063** that no other processes are able to read or write the database. In
37064** that case, we do not really need shared memory. No shared memory
37065** file is created. The shared memory will be simulated with heap memory.
37066*/
37067static int unixOpenSharedMemory(unixFile *pDbFd){
37068 struct unixShm *p = 0; /* The connection to be opened */
37069 struct unixShmNode *pShmNode; /* The underlying mmapped file */
37070 int rc = SQLITE_OK0; /* Result code */
37071 unixInodeInfo *pInode; /* The inode of fd */
37072 char *zShm; /* Name of the file used for SHM */
37073 int nShmFilename; /* Size of the SHM filename in bytes */
37074
37075 /* Allocate space for the new unixShm object. */
37076 p = sqlite3_malloc64( sizeof(*p) );
37077 if( p==0 ) return SQLITE_NOMEM_BKPT7;
37078 memset(p, 0, sizeof(*p));
37079 assert( pDbFd->pShm==0 )((void) (0));
37080
37081 /* Check to see if a unixShmNode object already exists. Reuse an existing
37082 ** one if present. Create a new one if necessary.
37083 */
37084 assert( unixFileMutexNotheld(pDbFd) )((void) (0));
37085 unixEnterMutex();
37086 pInode = pDbFd->pInode;
37087 pShmNode = pInode->pShmNode;
37088 if( pShmNode==0 ){
37089 struct stat sStat; /* fstat() info for database file */
37090#ifndef SQLITE_SHM_DIRECTORY
37091 const char *zBasePath = pDbFd->zPath;
37092#endif
37093
37094 /* Call fstat() to figure out the permissions on the database file. If
37095 ** a new *-shm file is created, an attempt will be made to create it
37096 ** with the same permissions.
37097 */
37098 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pDbFd->h, &sStat) ){
37099 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
37100 goto shm_open_err;
37101 }
37102
37103#ifdef SQLITE_SHM_DIRECTORY
37104 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
37105#else
37106 nShmFilename = 6 + (int)strlen(zBasePath);
37107#endif
37108 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
37109 if( pShmNode==0 ){
37110 rc = SQLITE_NOMEM_BKPT7;
37111 goto shm_open_err;
37112 }
37113 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
37114 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
37115#ifdef SQLITE_SHM_DIRECTORY
37116 sqlite3_snprintf(nShmFilename, zShm,
37117 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
37118 (u32)sStat.st_ino, (u32)sStat.st_dev);
37119#else
37120 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
37121 sqlite3FileSuffix3(pDbFd->zPath, zShm);
37122#endif
37123 pShmNode->hShm = -1;
37124 pDbFd->pInode->pShmNode = pShmNode;
37125 pShmNode->pInode = pDbFd->pInode;
37126 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
37127 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
37128 if( pShmNode->pShmMutex==0 ){
37129 rc = SQLITE_NOMEM_BKPT7;
37130 goto shm_open_err;
37131 }
37132 }
37133
37134 if( pInode->bProcessLock==0 ){
37135 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
37136 pShmNode->hShm = robust_open(zShm, O_RDWR02|O_CREAT0100,(sStat.st_mode&0777));
37137 }
37138 if( pShmNode->hShm<0 ){
37139 pShmNode->hShm = robust_open(zShm, O_RDONLY00, (sStat.st_mode&0777));
37140 if( pShmNode->hShm<0 ){
37141 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm)unixLogErrorAtLine(sqlite3CantopenError(37141),"open",zShm,37141
)
;
37142 goto shm_open_err;
37143 }
37144 pShmNode->isReadonly = 1;
37145 }
37146
37147 /* If this process is running as root, make sure that the SHM file
37148 ** is owned by the same user that owns the original database. Otherwise,
37149 ** the original owner will not be able to connect.
37150 */
37151 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
37152
37153 rc = unixLockSharedMemory(pDbFd, pShmNode);
37154 if( rc!=SQLITE_OK0 && rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ) goto shm_open_err;
37155 }
37156 }
37157
37158 /* Make the new connection a child of the unixShmNode */
37159 p->pShmNode = pShmNode;
37160#ifdef SQLITE_DEBUG
37161 p->id = pShmNode->nextShmId++;
37162#endif
37163 pShmNode->nRef++;
37164 pDbFd->pShm = p;
37165 unixLeaveMutex();
37166
37167 /* The reference count on pShmNode has already been incremented under
37168 ** the cover of the unixEnterMutex() mutex and the pointer from the
37169 ** new (struct unixShm) object to the pShmNode has been set. All that is
37170 ** left to do is to link the new object into the linked list starting
37171 ** at pShmNode->pFirst. This must be done while holding the
37172 ** pShmNode->pShmMutex.
37173 */
37174 sqlite3_mutex_enter(pShmNode->pShmMutex);
37175 p->pNext = pShmNode->pFirst;
37176 pShmNode->pFirst = p;
37177 sqlite3_mutex_leave(pShmNode->pShmMutex);
37178 return rc;
37179
37180 /* Jump here on any error */
37181shm_open_err:
37182 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
37183 sqlite3_free(p);
37184 unixLeaveMutex();
37185 return rc;
37186}
37187
37188/*
37189** This function is called to obtain a pointer to region iRegion of the
37190** shared-memory associated with the database file fd. Shared-memory regions
37191** are numbered starting from zero. Each shared-memory region is szRegion
37192** bytes in size.
37193**
37194** If an error occurs, an error code is returned and *pp is set to NULL.
37195**
37196** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
37197** region has not been allocated (by any client, including one running in a
37198** separate process), then *pp is set to NULL and SQLITE_OK returned. If
37199** bExtend is non-zero and the requested shared-memory region has not yet
37200** been allocated, it is allocated by this function.
37201**
37202** If the shared-memory region has already been allocated or is allocated by
37203** this call as described above, then it is mapped into this processes
37204** address space (if it is not already), *pp is set to point to the mapped
37205** memory and SQLITE_OK returned.
37206*/
37207static int unixShmMap(
37208 sqlite3_file *fd, /* Handle open on database file */
37209 int iRegion, /* Region to retrieve */
37210 int szRegion, /* Size of regions */
37211 int bExtend, /* True to extend file if necessary */
37212 void volatile **pp /* OUT: Mapped memory */
37213){
37214 unixFile *pDbFd = (unixFile*)fd;
37215 unixShm *p;
37216 unixShmNode *pShmNode;
37217 int rc = SQLITE_OK0;
37218 int nShmPerMap = unixShmRegionPerMap();
37219 int nReqRegion;
37220
37221 /* If the shared-memory file has not yet been opened, open it now. */
37222 if( pDbFd->pShm==0 ){
37223 rc = unixOpenSharedMemory(pDbFd);
37224 if( rc!=SQLITE_OK0 ) return rc;
37225 }
37226
37227 p = pDbFd->pShm;
37228 pShmNode = p->pShmNode;
37229 sqlite3_mutex_enter(pShmNode->pShmMutex);
37230 if( pShmNode->isUnlocked ){
37231 rc = unixLockSharedMemory(pDbFd, pShmNode);
37232 if( rc!=SQLITE_OK0 ) goto shmpage_out;
37233 pShmNode->isUnlocked = 0;
37234 }
37235 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 )((void) (0));
37236 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
37237 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 )((void) (0));
37238 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 )((void) (0));
37239
37240 /* Minimum number of regions required to be mapped. */
37241 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
37242
37243 if( pShmNode->nRegion<nReqRegion ){
37244 char **apNew; /* New apRegion[] array */
37245 int nByte = nReqRegion*szRegion; /* Minimum required file size */
37246 struct stat sStat; /* Used by fstat() */
37247
37248 pShmNode->szRegion = szRegion;
37249
37250 if( pShmNode->hShm>=0 ){
37251 /* The requested region is not mapped into this processes address space.
37252 ** Check to see if it has been allocated (i.e. if the wal-index file is
37253 ** large enough to contain the requested region).
37254 */
37255 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pShmNode->hShm, &sStat) ){
37256 rc = SQLITE_IOERR_SHMSIZE(10 | (19<<8));
37257 goto shmpage_out;
37258 }
37259
37260 if( sStat.st_size<nByte ){
37261 /* The requested memory region does not exist. If bExtend is set to
37262 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
37263 */
37264 if( !bExtend ){
37265 goto shmpage_out;
37266 }
37267
37268 /* Alternatively, if bExtend is true, extend the file. Do this by
37269 ** writing a single byte to the end of each (OS) page being
37270 ** allocated or extended. Technically, we need only write to the
37271 ** last page in order to extend the file. But writing to all new
37272 ** pages forces the OS to allocate them immediately, which reduces
37273 ** the chances of SIGBUS while accessing the mapped region later on.
37274 */
37275 else{
37276 static const int pgsz = 4096;
37277 int iPg;
37278
37279 /* Write to the last byte of each newly allocated or extended page */
37280 assert( (nByte % pgsz)==0 )((void) (0));
37281 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
37282 int x = 0;
37283 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
37284 const char *zFile = pShmNode->zFilename;
37285 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile)unixLogErrorAtLine((10 | (19<<8)),"write",zFile,37285);
37286 goto shmpage_out;
37287 }
37288 }
37289 }
37290 }
37291 }
37292
37293 /* Map the requested memory region into this processes address space. */
37294 apNew = (char **)sqlite3_realloc(
37295 pShmNode->apRegion, nReqRegion*sizeof(char *)
37296 );
37297 if( !apNew ){
37298 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
37299 goto shmpage_out;
37300 }
37301 pShmNode->apRegion = apNew;
37302 while( pShmNode->nRegion<nReqRegion ){
37303 int nMap = szRegion*nShmPerMap;
37304 int i;
37305 void *pMem;
37306 if( pShmNode->hShm>=0 ){
37307 pMem = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(0, nMap,
37308 pShmNode->isReadonly ? PROT_READ0x1 : PROT_READ0x1|PROT_WRITE0x2,
37309 MAP_SHARED0x01, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
37310 );
37311 if( pMem==MAP_FAILED((void *) -1) ){
37312 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename)unixLogErrorAtLine((10 | (21<<8)),"mmap",pShmNode->zFilename
,37312)
;
37313 goto shmpage_out;
37314 }
37315 }else{
37316 pMem = sqlite3_malloc64(nMap);
37317 if( pMem==0 ){
37318 rc = SQLITE_NOMEM_BKPT7;
37319 goto shmpage_out;
37320 }
37321 memset(pMem, 0, nMap);
37322 }
37323
37324 for(i=0; i<nShmPerMap; i++){
37325 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
37326 }
37327 pShmNode->nRegion += nShmPerMap;
37328 }
37329 }
37330
37331shmpage_out:
37332 if( pShmNode->nRegion>iRegion ){
37333 *pp = pShmNode->apRegion[iRegion];
37334 }else{
37335 *pp = 0;
37336 }
37337 if( pShmNode->isReadonly && rc==SQLITE_OK0 ) rc = SQLITE_READONLY8;
37338 sqlite3_mutex_leave(pShmNode->pShmMutex);
37339 return rc;
37340}
37341
37342/*
37343** Change the lock state for a shared-memory segment.
37344**
37345** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
37346** different here than in posix. In xShmLock(), one can go from unlocked
37347** to shared and back or from unlocked to exclusive and back. But one may
37348** not go from shared to exclusive or from exclusive to shared.
37349*/
37350static int unixShmLock(
37351 sqlite3_file *fd, /* Database file holding the shared memory */
37352 int ofst, /* First lock to acquire or release */
37353 int n, /* Number of locks to acquire or release */
37354 int flags /* What to do with the lock */
37355){
37356 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
37357 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
37358 unixShm *pX; /* For looping over all siblings */
37359 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
37360 int rc = SQLITE_OK0; /* Result code */
37361 u16 mask; /* Mask of locks to take or release */
37362
37363 assert( pShmNode==pDbFd->pInode->pShmNode )((void) (0));
37364 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
37365 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK )((void) (0));
37366 assert( n>=1 )((void) (0));
37367 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)((void) (0))
37368 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)((void) (0))
37369 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)((void) (0))
37370 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) )((void) (0));
37371 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 )((void) (0));
37372 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 )((void) (0));
37373 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 )((void) (0));
37374
37375 mask = (1<<(ofst+n)) - (1<<ofst);
37376 assert( n>1 || mask==(1<<ofst) )((void) (0));
37377 sqlite3_mutex_enter(pShmNode->pShmMutex);
37378 if( flags & SQLITE_SHM_UNLOCK1 ){
37379 u16 allMask = 0; /* Mask of locks held by siblings */
37380
37381 /* See if any siblings hold this same lock */
37382 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
37383 if( pX==p ) continue;
37384 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 )((void) (0));
37385 allMask |= pX->sharedMask;
37386 }
37387
37388 /* Unlock the system-level locks */
37389 if( (mask & allMask)==0 ){
37390 rc = unixShmSystemLock(pDbFd, F_UNLCK2, ofst+UNIX_SHM_BASE((22+8)*4), n);
37391 }else{
37392 rc = SQLITE_OK0;
37393 }
37394
37395 /* Undo the local locks */
37396 if( rc==SQLITE_OK0 ){
37397 p->exclMask &= ~mask;
37398 p->sharedMask &= ~mask;
37399 }
37400 }else if( flags & SQLITE_SHM_SHARED4 ){
37401 u16 allShared = 0; /* Union of locks held by connections other than "p" */
37402
37403 /* Find out which shared locks are already held by sibling connections.
37404 ** If any sibling already holds an exclusive lock, go ahead and return
37405 ** SQLITE_BUSY.
37406 */
37407 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
37408 if( (pX->exclMask & mask)!=0 ){
37409 rc = SQLITE_BUSY5;
37410 break;
37411 }
37412 allShared |= pX->sharedMask;
37413 }
37414
37415 /* Get shared locks at the system level, if necessary */
37416 if( rc==SQLITE_OK0 ){
37417 if( (allShared & mask)==0 ){
37418 rc = unixShmSystemLock(pDbFd, F_RDLCK0, ofst+UNIX_SHM_BASE((22+8)*4), n);
37419 }else{
37420 rc = SQLITE_OK0;
37421 }
37422 }
37423
37424 /* Get the local shared locks */
37425 if( rc==SQLITE_OK0 ){
37426 p->sharedMask |= mask;
37427 }
37428 }else{
37429 /* Make sure no sibling connections hold locks that will block this
37430 ** lock. If any do, return SQLITE_BUSY right away.
37431 */
37432 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
37433 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
37434 rc = SQLITE_BUSY5;
37435 break;
37436 }
37437 }
37438
37439 /* Get the exclusive locks at the system level. Then if successful
37440 ** also mark the local connection as being locked.
37441 */
37442 if( rc==SQLITE_OK0 ){
37443 rc = unixShmSystemLock(pDbFd, F_WRLCK1, ofst+UNIX_SHM_BASE((22+8)*4), n);
37444 if( rc==SQLITE_OK0 ){
37445 assert( (p->sharedMask & mask)==0 )((void) (0));
37446 p->exclMask |= mask;
37447 }
37448 }
37449 }
37450 sqlite3_mutex_leave(pShmNode->pShmMutex);
37451 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
37452 p->id, osGetpid(0), p->sharedMask, p->exclMask));
37453 return rc;
37454}
37455
37456/*
37457** Implement a memory barrier or memory fence on shared memory.
37458**
37459** All loads and stores begun before the barrier must complete before
37460** any load or store begun after the barrier.
37461*/
37462static void unixShmBarrier(
37463 sqlite3_file *fd /* Database file holding the shared memory */
37464){
37465 UNUSED_PARAMETER(fd)(void)(fd);
37466 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
37467 assert( fd->pMethods->xLock==nolockLock((void) (0))
37468 || unixFileMutexNotheld((unixFile*)fd)((void) (0))
37469 )((void) (0));
37470 unixEnterMutex(); /* Also mutex, for redundancy */
37471 unixLeaveMutex();
37472}
37473
37474/*
37475** Close a connection to shared-memory. Delete the underlying
37476** storage if deleteFlag is true.
37477**
37478** If there is no shared memory associated with the connection then this
37479** routine is a harmless no-op.
37480*/
37481static int unixShmUnmap(
37482 sqlite3_file *fd, /* The underlying database file */
37483 int deleteFlag /* Delete shared-memory if true */
37484){
37485 unixShm *p; /* The connection to be closed */
37486 unixShmNode *pShmNode; /* The underlying shared-memory file */
37487 unixShm **pp; /* For looping over sibling connections */
37488 unixFile *pDbFd; /* The underlying database file */
37489
37490 pDbFd = (unixFile*)fd;
37491 p = pDbFd->pShm;
37492 if( p==0 ) return SQLITE_OK0;
37493 pShmNode = p->pShmNode;
37494
37495 assert( pShmNode==pDbFd->pInode->pShmNode )((void) (0));
37496 assert( pShmNode->pInode==pDbFd->pInode )((void) (0));
37497
37498 /* Remove connection p from the set of connections associated
37499 ** with pShmNode */
37500 sqlite3_mutex_enter(pShmNode->pShmMutex);
37501 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
37502 *pp = p->pNext;
37503
37504 /* Free the connection p */
37505 sqlite3_free(p);
37506 pDbFd->pShm = 0;
37507 sqlite3_mutex_leave(pShmNode->pShmMutex);
37508
37509 /* If pShmNode->nRef has reached 0, then close the underlying
37510 ** shared-memory file, too */
37511 assert( unixFileMutexNotheld(pDbFd) )((void) (0));
37512 unixEnterMutex();
37513 assert( pShmNode->nRef>0 )((void) (0));
37514 pShmNode->nRef--;
37515 if( pShmNode->nRef==0 ){
37516 if( deleteFlag && pShmNode->hShm>=0 ){
37517 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(pShmNode->zFilename);
37518 }
37519 unixShmPurge(pDbFd);
37520 }
37521 unixLeaveMutex();
37522
37523 return SQLITE_OK0;
37524}
37525
37526
37527#else
37528# define unixShmMap 0
37529# define unixShmLock 0
37530# define unixShmBarrier 0
37531# define unixShmUnmap 0
37532#endif /* #ifndef SQLITE_OMIT_WAL */
37533
37534#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
37535/*
37536** If it is currently memory mapped, unmap file pFd.
37537*/
37538static void unixUnmapfile(unixFile *pFd){
37539 assert( pFd->nFetchOut==0 )((void) (0));
37540 if( pFd->pMapRegion ){
37541 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pFd->pMapRegion, pFd->mmapSizeActual);
37542 pFd->pMapRegion = 0;
37543 pFd->mmapSize = 0;
37544 pFd->mmapSizeActual = 0;
37545 }
37546}
37547
37548/*
37549** Attempt to set the size of the memory mapping maintained by file
37550** descriptor pFd to nNew bytes. Any existing mapping is discarded.
37551**
37552** If successful, this function sets the following variables:
37553**
37554** unixFile.pMapRegion
37555** unixFile.mmapSize
37556** unixFile.mmapSizeActual
37557**
37558** If unsuccessful, an error message is logged via sqlite3_log() and
37559** the three variables above are zeroed. In this case SQLite should
37560** continue accessing the database using the xRead() and xWrite()
37561** methods.
37562*/
37563static void unixRemapfile(
37564 unixFile *pFd, /* File descriptor object */
37565 i64 nNew /* Required mapping size */
37566){
37567 const char *zErr = "mmap";
37568 int h = pFd->h; /* File descriptor open on db file */
37569 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
37570 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
37571 u8 *pNew = 0; /* Location of new mapping */
37572 int flags = PROT_READ0x1; /* Flags to pass to mmap() */
37573
37574 assert( pFd->nFetchOut==0 )((void) (0));
37575 assert( nNew>pFd->mmapSize )((void) (0));
37576 assert( nNew<=pFd->mmapSizeMax )((void) (0));
37577 assert( nNew>0 )((void) (0));
37578 assert( pFd->mmapSizeActual>=pFd->mmapSize )((void) (0));
37579 assert( MAP_FAILED!=0 )((void) (0));
37580
37581#ifdef SQLITE_MMAP_READWRITE
37582 if( (pFd->ctrlFlags & UNIXFILE_RDONLY0x02)==0 ) flags |= PROT_WRITE0x2;
37583#endif
37584
37585 if( pOrig ){
37586#if HAVE_MREMAP1
37587 i64 nReuse = pFd->mmapSize;
37588#else
37589 const int szSyspage = osGetpagesize((int(*)(void))aSyscall[25].pCurrent)();
37590 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
37591#endif
37592 u8 *pReq = &pOrig[nReuse];
37593
37594 /* Unmap any pages of the existing mapping that cannot be reused. */
37595 if( nReuse!=nOrig ){
37596 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pReq, nOrig-nReuse);
37597 }
37598
37599#if HAVE_MREMAP1
37600 pNew = osMremap((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent
)
(pOrig, nReuse, nNew, MREMAP_MAYMOVE1);
37601 zErr = "mremap";
37602#else
37603 pNew = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(pReq, nNew-nReuse, flags, MAP_SHARED0x01, h, nReuse);
37604 if( pNew!=MAP_FAILED((void *) -1) ){
37605 if( pNew!=pReq ){
37606 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pNew, nNew - nReuse);
37607 pNew = 0;
37608 }else{
37609 pNew = pOrig;
37610 }
37611 }
37612#endif
37613
37614 /* The attempt to extend the existing mapping failed. Free it. */
37615 if( pNew==MAP_FAILED((void *) -1) || pNew==0 ){
37616 osMunmap((int(*)(void*,size_t))aSyscall[23].pCurrent)(pOrig, nReuse);
37617 }
37618 }
37619
37620 /* If pNew is still NULL, try to create an entirely new mapping. */
37621 if( pNew==0 ){
37622 pNew = osMmap((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent
)
(0, nNew, flags, MAP_SHARED0x01, h, 0);
37623 }
37624
37625 if( pNew==MAP_FAILED((void *) -1) ){
37626 pNew = 0;
37627 nNew = 0;
37628 unixLogError(SQLITE_OK, zErr, pFd->zPath)unixLogErrorAtLine(0,zErr,pFd->zPath,37628);
37629
37630 /* If the mmap() above failed, assume that all subsequent mmap() calls
37631 ** will probably fail too. Fall back to using xRead/xWrite exclusively
37632 ** in this case. */
37633 pFd->mmapSizeMax = 0;
37634 }
37635 pFd->pMapRegion = (void *)pNew;
37636 pFd->mmapSize = pFd->mmapSizeActual = nNew;
37637}
37638
37639/*
37640** Memory map or remap the file opened by file-descriptor pFd (if the file
37641** is already mapped, the existing mapping is replaced by the new). Or, if
37642** there already exists a mapping for this file, and there are still
37643** outstanding xFetch() references to it, this function is a no-op.
37644**
37645** If parameter nByte is non-negative, then it is the requested size of
37646** the mapping to create. Otherwise, if nByte is less than zero, then the
37647** requested size is the size of the file on disk. The actual size of the
37648** created mapping is either the requested size or the value configured
37649** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
37650**
37651** SQLITE_OK is returned if no error occurs (even if the mapping is not
37652** recreated as a result of outstanding references) or an SQLite error
37653** code otherwise.
37654*/
37655static int unixMapfile(unixFile *pFd, i64 nMap){
37656 assert( nMap>=0 || pFd->nFetchOut==0 )((void) (0));
37657 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) )((void) (0));
37658 if( pFd->nFetchOut>0 ) return SQLITE_OK0;
37659
37660 if( nMap<0 ){
37661 struct stat statbuf; /* Low-level file information */
37662 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFd->h, &statbuf) ){
37663 return SQLITE_IOERR_FSTAT(10 | (7<<8));
37664 }
37665 nMap = statbuf.st_size;
37666 }
37667 if( nMap>pFd->mmapSizeMax ){
37668 nMap = pFd->mmapSizeMax;
37669 }
37670
37671 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) )((void) (0));
37672 if( nMap!=pFd->mmapSize ){
37673 unixRemapfile(pFd, nMap);
37674 }
37675
37676 return SQLITE_OK0;
37677}
37678#endif /* SQLITE_MAX_MMAP_SIZE>0 */
37679
37680/*
37681** If possible, return a pointer to a mapping of file fd starting at offset
37682** iOff. The mapping must be valid for at least nAmt bytes.
37683**
37684** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
37685** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
37686** Finally, if an error does occur, return an SQLite error code. The final
37687** value of *pp is undefined in this case.
37688**
37689** If this function does return a pointer, the caller must eventually
37690** release the reference by calling unixUnfetch().
37691*/
37692static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
37693#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
37694 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
37695#endif
37696 *pp = 0;
37697
37698#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
37699 if( pFd->mmapSizeMax>0 ){
37700 if( pFd->pMapRegion==0 ){
37701 int rc = unixMapfile(pFd, -1);
37702 if( rc!=SQLITE_OK0 ) return rc;
37703 }
37704 if( pFd->mmapSize >= iOff+nAmt ){
37705 *pp = &((u8 *)pFd->pMapRegion)[iOff];
37706 pFd->nFetchOut++;
37707 }
37708 }
37709#endif
37710 return SQLITE_OK0;
37711}
37712
37713/*
37714** If the third argument is non-NULL, then this function releases a
37715** reference obtained by an earlier call to unixFetch(). The second
37716** argument passed to this function must be the same as the corresponding
37717** argument that was passed to the unixFetch() invocation.
37718**
37719** Or, if the third argument is NULL, then this function is being called
37720** to inform the VFS layer that, according to POSIX, any existing mapping
37721** may now be invalid and should be unmapped.
37722*/
37723static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
37724#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
37725 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
37726 UNUSED_PARAMETER(iOff)(void)(iOff);
37727
37728 /* If p==0 (unmap the entire file) then there must be no outstanding
37729 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
37730 ** then there must be at least one outstanding. */
37731 assert( (p==0)==(pFd->nFetchOut==0) )((void) (0));
37732
37733 /* If p!=0, it must match the iOff value. */
37734 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] )((void) (0));
37735
37736 if( p ){
37737 pFd->nFetchOut--;
37738 }else{
37739 unixUnmapfile(pFd);
37740 }
37741
37742 assert( pFd->nFetchOut>=0 )((void) (0));
37743#else
37744 UNUSED_PARAMETER(fd)(void)(fd);
37745 UNUSED_PARAMETER(p)(void)(p);
37746 UNUSED_PARAMETER(iOff)(void)(iOff);
37747#endif
37748 return SQLITE_OK0;
37749}
37750
37751/*
37752** Here ends the implementation of all sqlite3_file methods.
37753**
37754********************** End sqlite3_file Methods *******************************
37755******************************************************************************/
37756
37757/*
37758** This division contains definitions of sqlite3_io_methods objects that
37759** implement various file locking strategies. It also contains definitions
37760** of "finder" functions. A finder-function is used to locate the appropriate
37761** sqlite3_io_methods object for a particular database file. The pAppData
37762** field of the sqlite3_vfs VFS objects are initialized to be pointers to
37763** the correct finder-function for that VFS.
37764**
37765** Most finder functions return a pointer to a fixed sqlite3_io_methods
37766** object. The only interesting finder-function is autolockIoFinder, which
37767** looks at the filesystem type and tries to guess the best locking
37768** strategy from that.
37769**
37770** For finder-function F, two objects are created:
37771**
37772** (1) The real finder-function named "FImpt()".
37773**
37774** (2) A constant pointer to this function named just "F".
37775**
37776**
37777** A pointer to the F pointer is used as the pAppData value for VFS
37778** objects. We have to do this instead of letting pAppData point
37779** directly at the finder-function since C90 rules prevent a void*
37780** from be cast into a function pointer.
37781**
37782**
37783** Each instance of this macro generates two objects:
37784**
37785** * A constant sqlite3_io_methods object call METHOD that has locking
37786** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
37787**
37788** * An I/O method finder function called FINDER that returns a pointer
37789** to the METHOD object in the previous bullet.
37790*/
37791#define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP)static const sqlite3_io_methods METHOD = { VERSION, CLOSE, unixRead
, unixWrite, unixTruncate, unixSync, unixFileSize, LOCK, UNLOCK
, CKLOCK, unixFileControl, unixSectorSize, unixDeviceCharacteristics
, SHMMAP, unixShmLock, unixShmBarrier, unixShmUnmap, unixFetch
, unixUnfetch, }; static const sqlite3_io_methods *FINDERImpl
(const char *z, unixFile *p){ (void)(z); (void)(p); return &
METHOD; } static const sqlite3_io_methods *(*const FINDER)(const
char*,unixFile *p) = FINDERImpl;
\
37792static const sqlite3_io_methods METHOD = { \
37793 VERSION, /* iVersion */ \
37794 CLOSE, /* xClose */ \
37795 unixRead, /* xRead */ \
37796 unixWrite, /* xWrite */ \
37797 unixTruncate, /* xTruncate */ \
37798 unixSync, /* xSync */ \
37799 unixFileSize, /* xFileSize */ \
37800 LOCK, /* xLock */ \
37801 UNLOCK, /* xUnlock */ \
37802 CKLOCK, /* xCheckReservedLock */ \
37803 unixFileControl, /* xFileControl */ \
37804 unixSectorSize, /* xSectorSize */ \
37805 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
37806 SHMMAP, /* xShmMap */ \
37807 unixShmLock, /* xShmLock */ \
37808 unixShmBarrier, /* xShmBarrier */ \
37809 unixShmUnmap, /* xShmUnmap */ \
37810 unixFetch, /* xFetch */ \
37811 unixUnfetch, /* xUnfetch */ \
37812}; \
37813static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
37814 UNUSED_PARAMETER(z)(void)(z); UNUSED_PARAMETER(p)(void)(p); \
37815 return &METHOD; \
37816} \
37817static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
37818 = FINDER##Impl;
37819
37820/*
37821** Here are all of the sqlite3_io_methods objects for each of the
37822** locking strategies. Functions that return pointers to these methods
37823** are also created.
37824*/
37825IOMETHODS(static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37826 posixIoFinder, /* Finder function name */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37827 posixIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37828 3, /* shared memory and mmap are enabled */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37829 unixClose, /* xClose method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37830 unixLock, /* xLock method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37831 unixUnlock, /* xUnlock method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37832 unixCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37833 unixShmMap /* xShmMap method */static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37834)static const sqlite3_io_methods posixIoMethods = { 3, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, unixUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, unixShmMap, unixShmLock, unixShmBarrier
, unixShmUnmap, unixFetch, unixUnfetch, }; static const sqlite3_io_methods
*posixIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &posixIoMethods; } static const sqlite3_io_methods
*(*const posixIoFinder)(const char*,unixFile *p) = posixIoFinderImpl
;
37835IOMETHODS(static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37836 nolockIoFinder, /* Finder function name */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37837 nolockIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37838 3, /* shared memory and mmap are enabled */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37839 nolockClose, /* xClose method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37840 nolockLock, /* xLock method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37841 nolockUnlock, /* xUnlock method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37842 nolockCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37843 0 /* xShmMap method */static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37844)static const sqlite3_io_methods nolockIoMethods = { 3, nolockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, nolockLock
, nolockUnlock, nolockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nolockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nolockIoMethods; } static const sqlite3_io_methods
*(*const nolockIoFinder)(const char*,unixFile *p) = nolockIoFinderImpl
;
37845IOMETHODS(static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37846 dotlockIoFinder, /* Finder function name */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37847 dotlockIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37848 1, /* shared memory is disabled */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37849 dotlockClose, /* xClose method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37850 dotlockLock, /* xLock method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37851 dotlockUnlock, /* xUnlock method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37852 dotlockCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37853 0 /* xShmMap method */static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37854)static const sqlite3_io_methods dotlockIoMethods = { 1, dotlockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, dotlockLock
, dotlockUnlock, dotlockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
dotlockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (
void)(p); return &dotlockIoMethods; } static const sqlite3_io_methods
*(*const dotlockIoFinder)(const char*,unixFile *p) = dotlockIoFinderImpl
;
37855
37856#if SQLITE_ENABLE_LOCKING_STYLE0
37857IOMETHODS(static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37858 flockIoFinder, /* Finder function name */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37859 flockIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37860 1, /* shared memory is disabled */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37861 flockClose, /* xClose method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37862 flockLock, /* xLock method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37863 flockUnlock, /* xUnlock method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37864 flockCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37865 0 /* xShmMap method */static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37866)static const sqlite3_io_methods flockIoMethods = { 1, flockClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, flockLock
, flockUnlock, flockCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
flockIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &flockIoMethods; } static const sqlite3_io_methods
*(*const flockIoFinder)(const char*,unixFile *p) = flockIoFinderImpl
;
37867#endif
37868
37869#if OS_VXWORKS0
37870IOMETHODS(static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37871 semIoFinder, /* Finder function name */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37872 semIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37873 1, /* shared memory is disabled */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37874 semXClose, /* xClose method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37875 semXLock, /* xLock method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37876 semXUnlock, /* xUnlock method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37877 semXCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37878 0 /* xShmMap method */static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37879)static const sqlite3_io_methods semIoMethods = { 1, semXClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, semXLock
, semXUnlock, semXCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
semIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &semIoMethods; } static const sqlite3_io_methods
*(*const semIoFinder)(const char*,unixFile *p) = semIoFinderImpl
;
37880#endif
37881
37882#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
37883IOMETHODS(static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37884 afpIoFinder, /* Finder function name */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37885 afpIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37886 1, /* shared memory is disabled */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37887 afpClose, /* xClose method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37888 afpLock, /* xLock method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37889 afpUnlock, /* xUnlock method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37890 afpCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37891 0 /* xShmMap method */static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37892)static const sqlite3_io_methods afpIoMethods = { 1, afpClose,
unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, afpLock
, afpUnlock, afpCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
afpIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &afpIoMethods; } static const sqlite3_io_methods
*(*const afpIoFinder)(const char*,unixFile *p) = afpIoFinderImpl
;
37893#endif
37894
37895/*
37896** The proxy locking method is a "super-method" in the sense that it
37897** opens secondary file descriptors for the conch and lock files and
37898** it uses proxy, dot-file, AFP, and flock() locking methods on those
37899** secondary files. For this reason, the division that implements
37900** proxy locking is located much further down in the file. But we need
37901** to go ahead and define the sqlite3_io_methods and finder function
37902** for proxy locking here. So we forward declare the I/O methods.
37903*/
37904#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
37905static int proxyClose(sqlite3_file*);
37906static int proxyLock(sqlite3_file*, int);
37907static int proxyUnlock(sqlite3_file*, int);
37908static int proxyCheckReservedLock(sqlite3_file*, int*);
37909IOMETHODS(static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37910 proxyIoFinder, /* Finder function name */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37911 proxyIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37912 1, /* shared memory is disabled */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37913 proxyClose, /* xClose method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37914 proxyLock, /* xLock method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37915 proxyUnlock, /* xUnlock method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37916 proxyCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37917 0 /* xShmMap method */static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37918)static const sqlite3_io_methods proxyIoMethods = { 1, proxyClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, proxyLock
, proxyUnlock, proxyCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
proxyIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &proxyIoMethods; } static const sqlite3_io_methods
*(*const proxyIoFinder)(const char*,unixFile *p) = proxyIoFinderImpl
;
37919#endif
37920
37921/* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
37922#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
37923IOMETHODS(static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37924 nfsIoFinder, /* Finder function name */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37925 nfsIoMethods, /* sqlite3_io_methods object name */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37926 1, /* shared memory is disabled */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37927 unixClose, /* xClose method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37928 unixLock, /* xLock method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37929 nfsUnlock, /* xUnlock method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37930 unixCheckReservedLock, /* xCheckReservedLock method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37931 0 /* xShmMap method */static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37932)static const sqlite3_io_methods nfsIoMethods = { 1, unixClose
, unixRead, unixWrite, unixTruncate, unixSync, unixFileSize, unixLock
, nfsUnlock, unixCheckReservedLock, unixFileControl, unixSectorSize
, unixDeviceCharacteristics, 0, unixShmLock, unixShmBarrier, unixShmUnmap
, unixFetch, unixUnfetch, }; static const sqlite3_io_methods *
nfsIoFinderImpl(const char *z, unixFile *p){ (void)(z); (void
)(p); return &nfsIoMethods; } static const sqlite3_io_methods
*(*const nfsIoFinder)(const char*,unixFile *p) = nfsIoFinderImpl
;
37933#endif
37934
37935#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
37936/*
37937** This "finder" function attempts to determine the best locking strategy
37938** for the database file "filePath". It then returns the sqlite3_io_methods
37939** object that implements that strategy.
37940**
37941** This is for MacOSX only.
37942*/
37943static const sqlite3_io_methods *autolockIoFinderImpl(
37944 const char *filePath, /* name of the database file */
37945 unixFile *pNew /* open file object for the database file */
37946){
37947 static const struct Mapping {
37948 const char *zFilesystem; /* Filesystem type name */
37949 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
37950 } aMap[] = {
37951 { "hfs", &posixIoMethods },
37952 { "ufs", &posixIoMethods },
37953 { "afpfs", &afpIoMethods },
37954 { "smbfs", &afpIoMethods },
37955 { "webdav", &nolockIoMethods },
37956 { 0, 0 }
37957 };
37958 int i;
37959 struct statfs fsInfo;
37960 struct flock lockInfo;
37961
37962 if( !filePath ){
37963 /* If filePath==NULL that means we are dealing with a transient file
37964 ** that does not need to be locked. */
37965 return &nolockIoMethods;
37966 }
37967 if( statfs(filePath, &fsInfo) != -1 ){
37968 if( fsInfo.f_flags & MNT_RDONLY ){
37969 return &nolockIoMethods;
37970 }
37971 for(i=0; aMap[i].zFilesystem; i++){
37972 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
37973 return aMap[i].pMethods;
37974 }
37975 }
37976 }
37977
37978 /* Default case. Handles, amongst others, "nfs".
37979 ** Test byte-range lock using fcntl(). If the call succeeds,
37980 ** assume that the file-system supports POSIX style locks.
37981 */
37982 lockInfo.l_len = 1;
37983 lockInfo.l_start = 0;
37984 lockInfo.l_whence = SEEK_SET0;
37985 lockInfo.l_type = F_RDLCK0;
37986 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pNew->h, F_GETLK5, &lockInfo)!=-1 ) {
37987 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
37988 return &nfsIoMethods;
37989 } else {
37990 return &posixIoMethods;
37991 }
37992 }else{
37993 return &dotlockIoMethods;
37994 }
37995}
37996static const sqlite3_io_methods
37997 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
37998
37999#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
38000
38001#if OS_VXWORKS0
38002/*
38003** This "finder" function for VxWorks checks to see if posix advisory
38004** locking works. If it does, then that is what is used. If it does not
38005** work, then fallback to named semaphore locking.
38006*/
38007static const sqlite3_io_methods *vxworksIoFinderImpl(
38008 const char *filePath, /* name of the database file */
38009 unixFile *pNew /* the open file object */
38010){
38011 struct flock lockInfo;
38012
38013 if( !filePath ){
38014 /* If filePath==NULL that means we are dealing with a transient file
38015 ** that does not need to be locked. */
38016 return &nolockIoMethods;
38017 }
38018
38019 /* Test if fcntl() is supported and use POSIX style locks.
38020 ** Otherwise fall back to the named semaphore method.
38021 */
38022 lockInfo.l_len = 1;
38023 lockInfo.l_start = 0;
38024 lockInfo.l_whence = SEEK_SET0;
38025 lockInfo.l_type = F_RDLCK0;
38026 if( osFcntl((int(*)(int,int,...))aSyscall[7].pCurrent)(pNew->h, F_GETLK5, &lockInfo)!=-1 ) {
38027 return &posixIoMethods;
38028 }else{
38029 return &semIoMethods;
38030 }
38031}
38032static const sqlite3_io_methods
38033 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
38034
38035#endif /* OS_VXWORKS */
38036
38037/*
38038** An abstract type for a pointer to an IO method finder function:
38039*/
38040typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
38041
38042
38043/****************************************************************************
38044**************************** sqlite3_vfs methods ****************************
38045**
38046** This division contains the implementation of methods on the
38047** sqlite3_vfs object.
38048*/
38049
38050/*
38051** Initialize the contents of the unixFile structure pointed to by pId.
38052*/
38053static int fillInUnixFile(
38054 sqlite3_vfs *pVfs, /* Pointer to vfs object */
38055 int h, /* Open file descriptor of file being opened */
38056 sqlite3_file *pId, /* Write to the unixFile structure here */
38057 const char *zFilename, /* Name of the file being opened */
38058 int ctrlFlags /* Zero or more UNIXFILE_* values */
38059){
38060 const sqlite3_io_methods *pLockingStyle;
38061 unixFile *pNew = (unixFile *)pId;
38062 int rc = SQLITE_OK0;
38063
38064 assert( pNew->pInode==NULL )((void) (0));
38065
38066 /* No locking occurs in temporary files */
38067 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 )((void) (0));
38068
38069 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
38070 pNew->h = h;
38071 pNew->pVfs = pVfs;
38072 pNew->zPath = zFilename;
38073 pNew->ctrlFlags = (u8)ctrlFlags;
38074#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
38075 pNew->mmapSizeMax = sqlite3GlobalConfigsqlite3Config.szMmap;
38076#endif
38077 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI0x40) ? zFilename : 0),
38078 "psow", SQLITE_POWERSAFE_OVERWRITE1) ){
38079 pNew->ctrlFlags |= UNIXFILE_PSOW0x10;
38080 }
38081 if( strcmp(pVfs->zName,"unix-excl")==0 ){
38082 pNew->ctrlFlags |= UNIXFILE_EXCL0x01;
38083 }
38084
38085#if OS_VXWORKS0
38086 pNew->pId = vxworksFindFileId(zFilename);
38087 if( pNew->pId==0 ){
38088 ctrlFlags |= UNIXFILE_NOLOCK0x80;
38089 rc = SQLITE_NOMEM_BKPT7;
38090 }
38091#endif
38092
38093 if( ctrlFlags & UNIXFILE_NOLOCK0x80 ){
38094 pLockingStyle = &nolockIoMethods;
38095 }else{
38096 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
38097#if SQLITE_ENABLE_LOCKING_STYLE0
38098 /* Cache zFilename in the locking context (AFP and dotlock override) for
38099 ** proxyLock activation is possible (remote proxy is based on db name)
38100 ** zFilename remains valid until file is closed, to support */
38101 pNew->lockingContext = (void*)zFilename;
38102#endif
38103 }
38104
38105 if( pLockingStyle == &posixIoMethods
38106#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
38107 || pLockingStyle == &nfsIoMethods
38108#endif
38109 ){
38110 unixEnterMutex();
38111 rc = findInodeInfo(pNew, &pNew->pInode);
38112 if( rc!=SQLITE_OK0 ){
38113 /* If an error occurred in findInodeInfo(), close the file descriptor
38114 ** immediately, before releasing the mutex. findInodeInfo() may fail
38115 ** in two scenarios:
38116 **
38117 ** (a) A call to fstat() failed.
38118 ** (b) A malloc failed.
38119 **
38120 ** Scenario (b) may only occur if the process is holding no other
38121 ** file descriptors open on the same file. If there were other file
38122 ** descriptors on this file, then no malloc would be required by
38123 ** findInodeInfo(). If this is the case, it is quite safe to close
38124 ** handle h - as it is guaranteed that no posix locks will be released
38125 ** by doing so.
38126 **
38127 ** If scenario (a) caused the error then things are not so safe. The
38128 ** implicit assumption here is that if fstat() fails, things are in
38129 ** such bad shape that dropping a lock or two doesn't matter much.
38130 */
38131 robust_close(pNew, h, __LINE__38131);
38132 h = -1;
38133 }
38134 unixLeaveMutex();
38135 }
38136
38137#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
38138 else if( pLockingStyle == &afpIoMethods ){
38139 /* AFP locking uses the file path so it needs to be included in
38140 ** the afpLockingContext.
38141 */
38142 afpLockingContext *pCtx;
38143 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
38144 if( pCtx==0 ){
38145 rc = SQLITE_NOMEM_BKPT7;
38146 }else{
38147 /* NB: zFilename exists and remains valid until the file is closed
38148 ** according to requirement F11141. So we do not need to make a
38149 ** copy of the filename. */
38150 pCtx->dbPath = zFilename;
38151 pCtx->reserved = 0;
38152 srandomdev();
38153 unixEnterMutex();
38154 rc = findInodeInfo(pNew, &pNew->pInode);
38155 if( rc!=SQLITE_OK0 ){
38156 sqlite3_free(pNew->lockingContext);
38157 robust_close(pNew, h, __LINE__38157);
38158 h = -1;
38159 }
38160 unixLeaveMutex();
38161 }
38162 }
38163#endif
38164
38165 else if( pLockingStyle == &dotlockIoMethods ){
38166 /* Dotfile locking uses the file path so it needs to be included in
38167 ** the dotlockLockingContext
38168 */
38169 char *zLockFile;
38170 int nFilename;
38171 assert( zFilename!=0 )((void) (0));
38172 nFilename = (int)strlen(zFilename) + 6;
38173 zLockFile = (char *)sqlite3_malloc64(nFilename);
38174 if( zLockFile==0 ){
38175 rc = SQLITE_NOMEM_BKPT7;
38176 }else{
38177 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX".lock", zFilename);
38178 }
38179 pNew->lockingContext = zLockFile;
38180 }
38181
38182#if OS_VXWORKS0
38183 else if( pLockingStyle == &semIoMethods ){
38184 /* Named semaphore locking uses the file path so it needs to be
38185 ** included in the semLockingContext
38186 */
38187 unixEnterMutex();
38188 rc = findInodeInfo(pNew, &pNew->pInode);
38189 if( (rc==SQLITE_OK0) && (pNew->pInode->pSem==NULL((void*)0)) ){
38190 char *zSemName = pNew->pInode->aSemName;
38191 int n;
38192 sqlite3_snprintf(MAX_PATHNAME512, zSemName, "/%s.sem",
38193 pNew->pId->zCanonicalName);
38194 for( n=1; zSemName[n]; n++ )
38195 if( zSemName[n]=='/' ) zSemName[n] = '_';
38196 pNew->pInode->pSem = sem_open(zSemName, O_CREAT0100, 0666, 1);
38197 if( pNew->pInode->pSem == SEM_FAILED ){
38198 rc = SQLITE_NOMEM_BKPT7;
38199 pNew->pInode->aSemName[0] = '\0';
38200 }
38201 }
38202 unixLeaveMutex();
38203 }
38204#endif
38205
38206 storeLastErrno(pNew, 0);
38207#if OS_VXWORKS0
38208 if( rc!=SQLITE_OK0 ){
38209 if( h>=0 ) robust_close(pNew, h, __LINE__38209);
38210 h = -1;
38211 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zFilename);
38212 pNew->ctrlFlags |= UNIXFILE_DELETE0x20;
38213 }
38214#endif
38215 if( rc!=SQLITE_OK0 ){
38216 if( h>=0 ) robust_close(pNew, h, __LINE__38216);
38217 }else{
38218 pNew->pMethod = pLockingStyle;
38219 OpenCounter(+1);
38220 verifyDbFile(pNew);
38221 }
38222 return rc;
38223}
38224
38225/*
38226** Return the name of a directory in which to put temporary files.
38227** If no suitable temporary file directory can be found, return NULL.
38228*/
38229static const char *unixTempFileDir(void){
38230 static const char *azDirs[] = {
38231 0,
38232 0,
38233 "/var/tmp",
38234 "/usr/tmp",
38235 "/tmp",
38236 "."
38237 };
38238 unsigned int i = 0;
38239 struct stat buf;
38240 const char *zDir = sqlite3_temp_directory;
38241
38242 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
38243 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
38244 while(1){
38245 if( zDir!=0
38246 && osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zDir, &buf)==0
38247 && S_ISDIR(buf.st_mode)((((buf.st_mode)) & 0170000) == (0040000))
38248 && osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zDir, 03)==0
38249 ){
38250 return zDir;
38251 }
38252 if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
38253 zDir = azDirs[i++];
38254 }
38255 return 0;
38256}
38257
38258/*
38259** Create a temporary file name in zBuf. zBuf must be allocated
38260** by the calling process and must be big enough to hold at least
38261** pVfs->mxPathname bytes.
38262*/
38263static int unixGetTempname(int nBuf, char *zBuf){
38264 const char *zDir;
38265 int iLimit = 0;
38266
38267 /* It's odd to simulate an io-error here, but really this is just
38268 ** using the io-error infrastructure to test that SQLite handles this
38269 ** function failing.
38270 */
38271 zBuf[0] = 0;
38272 SimulateIOError( return SQLITE_IOERR );
38273
38274 zDir = unixTempFileDir();
38275 if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH(10 | (25<<8));
38276 do{
38277 u64 r;
38278 sqlite3_randomness(sizeof(r), &r);
38279 assert( nBuf>2 )((void) (0));
38280 zBuf[nBuf-2] = 0;
38281 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"etilqs_""%llx%c",
38282 zDir, r, 0);
38283 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR1;
38284 }while( osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zBuf,0)==0 );
38285 return SQLITE_OK0;
38286}
38287
38288#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
38289/*
38290** Routine to transform a unixFile into a proxy-locking unixFile.
38291** Implementation in the proxy-lock division, but used by unixOpen()
38292** if SQLITE_PREFER_PROXY_LOCKING is defined.
38293*/
38294static int proxyTransformUnixFile(unixFile*, const char*);
38295#endif
38296
38297/*
38298** Search for an unused file descriptor that was opened on the database
38299** file (not a journal or master-journal file) identified by pathname
38300** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
38301** argument to this function.
38302**
38303** Such a file descriptor may exist if a database connection was closed
38304** but the associated file descriptor could not be closed because some
38305** other file descriptor open on the same file is holding a file-lock.
38306** Refer to comments in the unixClose() function and the lengthy comment
38307** describing "Posix Advisory Locking" at the start of this file for
38308** further details. Also, ticket #4018.
38309**
38310** If a suitable file descriptor is found, then it is returned. If no
38311** such file descriptor is located, -1 is returned.
38312*/
38313static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
38314 UnixUnusedFd *pUnused = 0;
38315
38316 /* Do not search for an unused file descriptor on vxworks. Not because
38317 ** vxworks would not benefit from the change (it might, we're not sure),
38318 ** but because no way to test it is currently available. It is better
38319 ** not to risk breaking vxworks support for the sake of such an obscure
38320 ** feature. */
38321#if !OS_VXWORKS0
38322 struct stat sStat; /* Results of stat() call */
38323
38324 unixEnterMutex();
38325
38326 /* A stat() call may fail for various reasons. If this happens, it is
38327 ** almost certain that an open() call on the same path will also fail.
38328 ** For this reason, if an error occurs in the stat() call here, it is
38329 ** ignored and -1 is returned. The caller will try to open a new file
38330 ** descriptor on the same path, fail, and return an error to SQLite.
38331 **
38332 ** Even if a subsequent open() call does succeed, the consequences of
38333 ** not searching for a reusable file descriptor are not dire. */
38334 if( inodeList!=0 && 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zPath, &sStat) ){
38335 unixInodeInfo *pInode;
38336
38337 pInode = inodeList;
38338 while( pInode && (pInode->fileId.dev!=sStat.st_dev
38339 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
38340 pInode = pInode->pNext;
38341 }
38342 if( pInode ){
38343 UnixUnusedFd **pp;
38344 assert( sqlite3_mutex_notheld(pInode->pLockMutex) )((void) (0));
38345 sqlite3_mutex_enter(pInode->pLockMutex);
38346 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
38347 pUnused = *pp;
38348 if( pUnused ){
38349 *pp = pUnused->pNext;
38350 }
38351 sqlite3_mutex_leave(pInode->pLockMutex);
38352 }
38353 }
38354 unixLeaveMutex();
38355#endif /* if !OS_VXWORKS */
38356 return pUnused;
38357}
38358
38359/*
38360** Find the mode, uid and gid of file zFile.
38361*/
38362static int getFileMode(
38363 const char *zFile, /* File name */
38364 mode_t *pMode, /* OUT: Permissions of zFile */
38365 uid_t *pUid, /* OUT: uid of zFile. */
38366 gid_t *pGid /* OUT: gid of zFile. */
38367){
38368 struct stat sStat; /* Output of stat() on database file */
38369 int rc = SQLITE_OK0;
38370 if( 0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zFile, &sStat) ){
38371 *pMode = sStat.st_mode & 0777;
38372 *pUid = sStat.st_uid;
38373 *pGid = sStat.st_gid;
38374 }else{
38375 rc = SQLITE_IOERR_FSTAT(10 | (7<<8));
38376 }
38377 return rc;
38378}
38379
38380/*
38381** This function is called by unixOpen() to determine the unix permissions
38382** to create new files with. If no error occurs, then SQLITE_OK is returned
38383** and a value suitable for passing as the third argument to open(2) is
38384** written to *pMode. If an IO error occurs, an SQLite error code is
38385** returned and the value of *pMode is not modified.
38386**
38387** In most cases, this routine sets *pMode to 0, which will become
38388** an indication to robust_open() to create the file using
38389** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
38390** But if the file being opened is a WAL or regular journal file, then
38391** this function queries the file-system for the permissions on the
38392** corresponding database file and sets *pMode to this value. Whenever
38393** possible, WAL and journal files are created using the same permissions
38394** as the associated database file.
38395**
38396** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
38397** original filename is unavailable. But 8_3_NAMES is only used for
38398** FAT filesystems and permissions do not matter there, so just use
38399** the default permissions.
38400*/
38401static int findCreateFileMode(
38402 const char *zPath, /* Path of file (possibly) being created */
38403 int flags, /* Flags passed as 4th argument to xOpen() */
38404 mode_t *pMode, /* OUT: Permissions to open file with */
38405 uid_t *pUid, /* OUT: uid to set on the file */
38406 gid_t *pGid /* OUT: gid to set on the file */
38407){
38408 int rc = SQLITE_OK0; /* Return Code */
38409 *pMode = 0;
38410 *pUid = 0;
38411 *pGid = 0;
38412 if( flags & (SQLITE_OPEN_WAL0x00080000|SQLITE_OPEN_MAIN_JOURNAL0x00000800) ){
38413 char zDb[MAX_PATHNAME512+1]; /* Database file path */
38414 int nDb; /* Number of valid bytes in zDb */
38415
38416 /* zPath is a path to a WAL or journal file. The following block derives
38417 ** the path to the associated database file from zPath. This block handles
38418 ** the following naming conventions:
38419 **
38420 ** "<path to db>-journal"
38421 ** "<path to db>-wal"
38422 ** "<path to db>-journalNN"
38423 ** "<path to db>-walNN"
38424 **
38425 ** where NN is a decimal number. The NN naming schemes are
38426 ** used by the test_multiplex.c module.
38427 */
38428 nDb = sqlite3Strlen30(zPath) - 1;
38429 while( zPath[nDb]!='-' ){
38430 /* In normal operation, the journal file name will always contain
38431 ** a '-' character. However in 8+3 filename mode, or if a corrupt
38432 ** rollback journal specifies a master journal with a goofy name, then
38433 ** the '-' might be missing. */
38434 if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK0;
38435 nDb--;
38436 }
38437 memcpy(zDb, zPath, nDb);
38438 zDb[nDb] = '\0';
38439
38440 rc = getFileMode(zDb, pMode, pUid, pGid);
38441 }else if( flags & SQLITE_OPEN_DELETEONCLOSE0x00000008 ){
38442 *pMode = 0600;
38443 }else if( flags & SQLITE_OPEN_URI0x00000040 ){
38444 /* If this is a main database file and the file was opened using a URI
38445 ** filename, check for the "modeof" parameter. If present, interpret
38446 ** its value as a filename and try to copy the mode, uid and gid from
38447 ** that file. */
38448 const char *z = sqlite3_uri_parameter(zPath, "modeof");
38449 if( z ){
38450 rc = getFileMode(z, pMode, pUid, pGid);
38451 }
38452 }
38453 return rc;
38454}
38455
38456/*
38457** Open the file zPath.
38458**
38459** Previously, the SQLite OS layer used three functions in place of this
38460** one:
38461**
38462** sqlite3OsOpenReadWrite();
38463** sqlite3OsOpenReadOnly();
38464** sqlite3OsOpenExclusive();
38465**
38466** These calls correspond to the following combinations of flags:
38467**
38468** ReadWrite() -> (READWRITE | CREATE)
38469** ReadOnly() -> (READONLY)
38470** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
38471**
38472** The old OpenExclusive() accepted a boolean argument - "delFlag". If
38473** true, the file was configured to be automatically deleted when the
38474** file handle closed. To achieve the same effect using this new
38475** interface, add the DELETEONCLOSE flag to those specified above for
38476** OpenExclusive().
38477*/
38478static int unixOpen(
38479 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
38480 const char *zPath, /* Pathname of file to be opened */
38481 sqlite3_file *pFile, /* The file descriptor to be filled in */
38482 int flags, /* Input flags to control the opening */
38483 int *pOutFlags /* Output flags returned to SQLite core */
38484){
38485 unixFile *p = (unixFile *)pFile;
38486 int fd = -1; /* File descriptor returned by open() */
38487 int openFlags = 0; /* Flags to pass to open() */
38488 int eType = flags&0xFFFFFF00; /* Type of file to open */
38489 int noLock; /* True to omit locking primitives */
38490 int rc = SQLITE_OK0; /* Function Return Code */
38491 int ctrlFlags = 0; /* UNIXFILE_* flags */
38492
38493 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE0x00000010);
38494 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE0x00000008);
38495 int isCreate = (flags & SQLITE_OPEN_CREATE0x00000004);
38496 int isReadonly = (flags & SQLITE_OPEN_READONLY0x00000001);
38497 int isReadWrite = (flags & SQLITE_OPEN_READWRITE0x00000002);
38498#if SQLITE_ENABLE_LOCKING_STYLE0
38499 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY0x00000020);
38500#endif
38501#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
38502 struct statfs fsInfo;
38503#endif
38504
38505 /* If creating a master or main-file journal, this function will open
38506 ** a file-descriptor on the directory too. The first time unixSync()
38507 ** is called the directory file descriptor will be fsync()ed and close()d.
38508 */
38509 int isNewJrnl = (isCreate && (
38510 eType==SQLITE_OPEN_MASTER_JOURNAL0x00004000
38511 || eType==SQLITE_OPEN_MAIN_JOURNAL0x00000800
38512 || eType==SQLITE_OPEN_WAL0x00080000
38513 ));
38514
38515 /* If argument zPath is a NULL pointer, this function is required to open
38516 ** a temporary file. Use this buffer to store the file name in.
38517 */
38518 char zTmpname[MAX_PATHNAME512+2];
38519 const char *zName = zPath;
38520
38521 /* Check the following statements are true:
38522 **
38523 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
38524 ** (b) if CREATE is set, then READWRITE must also be set, and
38525 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
38526 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
38527 */
38528 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly))((void) (0));
38529 assert(isCreate==0 || isReadWrite)((void) (0));
38530 assert(isExclusive==0 || isCreate)((void) (0));
38531 assert(isDelete==0 || isCreate)((void) (0));
38532
38533 /* The main DB, main journal, WAL file and master journal are never
38534 ** automatically deleted. Nor are they ever temporary files. */
38535 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB )((void) (0));
38536 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
38537 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL )((void) (0));
38538 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL )((void) (0));
38539
38540 /* Assert that the upper layer has set one of the "file-type" flags. */
38541 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB((void) (0))
38542 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL((void) (0))
38543 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL((void) (0))
38544 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL((void) (0))
38545 )((void) (0));
38546
38547 /* Detect a pid change and reset the PRNG. There is a race condition
38548 ** here such that two or more threads all trying to open databases at
38549 ** the same instant might all reset the PRNG. But multiple resets
38550 ** are harmless.
38551 */
38552 if( randomnessPid!=osGetpid(0)(pid_t)getpid() ){
38553 randomnessPid = osGetpid(0)(pid_t)getpid();
38554 sqlite3_randomness(0,0);
38555 }
38556 memset(p, 0, sizeof(unixFile));
38557
38558 if( eType==SQLITE_OPEN_MAIN_DB0x00000100 ){
38559 UnixUnusedFd *pUnused;
38560 pUnused = findReusableFd(zName, flags);
38561 if( pUnused ){
38562 fd = pUnused->fd;
38563 }else{
38564 pUnused = sqlite3_malloc64(sizeof(*pUnused));
38565 if( !pUnused ){
38566 return SQLITE_NOMEM_BKPT7;
38567 }
38568 }
38569 p->pPreallocatedUnused = pUnused;
38570
38571 /* Database filenames are double-zero terminated if they are not
38572 ** URIs with parameters. Hence, they can always be passed into
38573 ** sqlite3_uri_parameter(). */
38574 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 )((void) (0));
38575
38576 }else if( !zName ){
38577 /* If zName is NULL, the upper layer is requesting a temp file. */
38578 assert(isDelete && !isNewJrnl)((void) (0));
38579 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
38580 if( rc!=SQLITE_OK0 ){
38581 return rc;
38582 }
38583 zName = zTmpname;
38584
38585 /* Generated temporary filenames are always double-zero terminated
38586 ** for use by sqlite3_uri_parameter(). */
38587 assert( zName[strlen(zName)+1]==0 )((void) (0));
38588 }
38589
38590 /* Determine the value of the flags parameter passed to POSIX function
38591 ** open(). These must be calculated even if open() is not called, as
38592 ** they may be stored as part of the file handle and used by the
38593 ** 'conch file' locking functions later on. */
38594 if( isReadonly ) openFlags |= O_RDONLY00;
38595 if( isReadWrite ) openFlags |= O_RDWR02;
38596 if( isCreate ) openFlags |= O_CREAT0100;
38597 if( isExclusive ) openFlags |= (O_EXCL0200|O_NOFOLLOW0400000);
38598 openFlags |= (O_LARGEFILE0|O_BINARY0);
38599
38600 if( fd<0 ){
38601 mode_t openMode; /* Permissions to create file with */
38602 uid_t uid; /* Userid for the file */
38603 gid_t gid; /* Groupid for the file */
38604 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
38605 if( rc!=SQLITE_OK0 ){
38606 assert( !p->pPreallocatedUnused )((void) (0));
38607 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
38608 return rc;
38609 }
38610 fd = robust_open(zName, openFlags, openMode);
38611 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
38612 assert( !isExclusive || (openFlags & O_CREAT)!=0 )((void) (0));
38613 if( fd<0 ){
38614 if( isNewJrnl && errno(*__errno_location ())==EACCES13 && osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zName, F_OK0) ){
38615 /* If unable to create a journal because the directory is not
38616 ** writable, change the error code to indicate that. */
38617 rc = SQLITE_READONLY_DIRECTORY(8 | (6<<8));
38618 }else if( errno(*__errno_location ())!=EISDIR21 && isReadWrite ){
38619 /* Failed to open the file for read/write access. Try read-only. */
38620 flags &= ~(SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004);
38621 openFlags &= ~(O_RDWR02|O_CREAT0100);
38622 flags |= SQLITE_OPEN_READONLY0x00000001;
38623 openFlags |= O_RDONLY00;
38624 isReadonly = 1;
38625 fd = robust_open(zName, openFlags, openMode);
38626 }
38627 }
38628 if( fd<0 ){
38629 int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName)unixLogErrorAtLine(sqlite3CantopenError(38629),"open",zName,38629
)
;
38630 if( rc==SQLITE_OK0 ) rc = rc2;
38631 goto open_finished;
38632 }
38633
38634 /* If this process is running as root and if creating a new rollback
38635 ** journal or WAL file, set the ownership of the journal or WAL to be
38636 ** the same as the original database.
38637 */
38638 if( flags & (SQLITE_OPEN_WAL0x00080000|SQLITE_OPEN_MAIN_JOURNAL0x00000800) ){
38639 robustFchown(fd, uid, gid);
38640 }
38641 }
38642 assert( fd>=0 )((void) (0));
38643 if( pOutFlags ){
38644 *pOutFlags = flags;
38645 }
38646
38647 if( p->pPreallocatedUnused ){
38648 p->pPreallocatedUnused->fd = fd;
38649 p->pPreallocatedUnused->flags = flags;
38650 }
38651
38652 if( isDelete ){
38653#if OS_VXWORKS0
38654 zPath = zName;
38655#elif defined(SQLITE_UNLINK_AFTER_CLOSE)
38656 zPath = sqlite3_mprintf("%s", zName);
38657 if( zPath==0 ){
38658 robust_close(p, fd, __LINE__38658);
38659 return SQLITE_NOMEM_BKPT7;
38660 }
38661#else
38662 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zName);
38663#endif
38664 }
38665#if SQLITE_ENABLE_LOCKING_STYLE0
38666 else{
38667 p->openFlags = openFlags;
38668 }
38669#endif
38670
38671#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE0
38672 if( fstatfs(fd, &fsInfo) == -1 ){
38673 storeLastErrno(p, errno(*__errno_location ()));
38674 robust_close(p, fd, __LINE__38674);
38675 return SQLITE_IOERR_ACCESS(10 | (13<<8));
38676 }
38677 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
38678 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS0x1;
38679 }
38680 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
38681 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS0x1;
38682 }
38683#endif
38684
38685 /* Set up appropriate ctrlFlags */
38686 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE0x20;
38687 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY0x02;
38688 noLock = eType!=SQLITE_OPEN_MAIN_DB0x00000100;
38689 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK0x80;
38690 if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC0x08;
38691 if( flags & SQLITE_OPEN_URI0x00000040 ) ctrlFlags |= UNIXFILE_URI0x40;
38692
38693#if SQLITE_ENABLE_LOCKING_STYLE0
38694#if SQLITE_PREFER_PROXY_LOCKING
38695 isAutoProxy = 1;
38696#endif
38697 if( isAutoProxy && (zPath!=NULL((void*)0)) && (!noLock) && pVfs->xOpen ){
38698 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
38699 int useProxy = 0;
38700
38701 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
38702 ** never use proxy, NULL means use proxy for non-local files only. */
38703 if( envforce!=NULL((void*)0) ){
38704 useProxy = atoi(envforce)>0;
38705 }else{
38706 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
38707 }
38708 if( useProxy ){
38709 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
38710 if( rc==SQLITE_OK0 ){
38711 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
38712 if( rc!=SQLITE_OK0 ){
38713 /* Use unixClose to clean up the resources added in fillInUnixFile
38714 ** and clear all the structure's references. Specifically,
38715 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
38716 */
38717 unixClose(pFile);
38718 return rc;
38719 }
38720 }
38721 goto open_finished;
38722 }
38723 }
38724#endif
38725
38726 assert( zPath==0 || zPath[0]=='/'((void) (0))
38727 || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL((void) (0))
38728 )((void) (0));
38729 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
38730
38731open_finished:
38732 if( rc!=SQLITE_OK0 ){
38733 sqlite3_free(p->pPreallocatedUnused);
38734 }
38735 return rc;
38736}
38737
38738
38739/*
38740** Delete the file at zPath. If the dirSync argument is true, fsync()
38741** the directory after deleting the file.
38742*/
38743static int unixDelete(
38744 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
38745 const char *zPath, /* Name of file to be deleted */
38746 int dirSync /* If true, fsync() directory after deleting file */
38747){
38748 int rc = SQLITE_OK0;
38749 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
38750 SimulateIOError(return SQLITE_IOERR_DELETE);
38751 if( osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(zPath)==(-1) ){
38752 if( errno(*__errno_location ())==ENOENT2
38753#if OS_VXWORKS0
38754 || osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zPath,0)!=0
38755#endif
38756 ){
38757 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8));
38758 }else{
38759 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath)unixLogErrorAtLine((10 | (10<<8)),"unlink",zPath,38759);
38760 }
38761 return rc;
38762 }
38763#ifndef SQLITE_DISABLE_DIRSYNC
38764 if( (dirSync & 1)!=0 ){
38765 int fd;
38766 rc = osOpenDirectory((int(*)(const char*,int*))aSyscall[17].pCurrent)(zPath, &fd);
38767 if( rc==SQLITE_OK0 ){
38768 if( full_fsync(fd,0,0) ){
38769 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath)unixLogErrorAtLine((10 | (5<<8)),"fsync",zPath,38769);
38770 }
38771 robust_close(0, fd, __LINE__38771);
38772 }else{
38773 assert( rc==SQLITE_CANTOPEN )((void) (0));
38774 rc = SQLITE_OK0;
38775 }
38776 }
38777#endif
38778 return rc;
38779}
38780
38781/*
38782** Test the existence of or access permissions of file zPath. The
38783** test performed depends on the value of flags:
38784**
38785** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
38786** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
38787** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
38788**
38789** Otherwise return 0.
38790*/
38791static int unixAccess(
38792 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
38793 const char *zPath, /* Path of the file to examine */
38794 int flags, /* What do we want to learn about the zPath file? */
38795 int *pResOut /* Write result boolean here */
38796){
38797 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
38798 SimulateIOError( return SQLITE_IOERR_ACCESS; );
38799 assert( pResOut!=0 )((void) (0));
38800
38801 /* The spec says there are three possible values for flags. But only
38802 ** two of them are actually used */
38803 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE )((void) (0));
38804
38805 if( flags==SQLITE_ACCESS_EXISTS0 ){
38806 struct stat buf;
38807 *pResOut = (0==osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(zPath, &buf) && buf.st_size>0);
38808 }else{
38809 *pResOut = osAccess((int(*)(const char*,int))aSyscall[2].pCurrent)(zPath, W_OK2|R_OK4)==0;
38810 }
38811 return SQLITE_OK0;
38812}
38813
38814/*
38815**
38816*/
38817static int mkFullPathname(
38818 const char *zPath, /* Input path */
38819 char *zOut, /* Output buffer */
38820 int nOut /* Allocated size of buffer zOut */
38821){
38822 int nPath = sqlite3Strlen30(zPath);
38823 int iOff = 0;
38824 if( zPath[0]!='/' ){
38825 if( osGetcwd((char*(*)(char*,size_t))aSyscall[3].pCurrent)(zOut, nOut-2)==0 ){
38826 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath)unixLogErrorAtLine(sqlite3CantopenError(38826),"getcwd",zPath
,38826)
;
38827 }
38828 iOff = sqlite3Strlen30(zOut);
38829 zOut[iOff++] = '/';
38830 }
38831 if( (iOff+nPath+1)>nOut ){
38832 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
38833 ** even if it returns an error. */
38834 zOut[iOff] = '\0';
38835 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(38835);
38836 }
38837 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
38838 return SQLITE_OK0;
38839}
38840
38841/*
38842** Turn a relative pathname into a full pathname. The relative path
38843** is stored as a nul-terminated string in the buffer pointed to by
38844** zPath.
38845**
38846** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
38847** (in this case, MAX_PATHNAME bytes). The full-path is written to
38848** this buffer before returning.
38849*/
38850static int unixFullPathname(
38851 sqlite3_vfs *pVfs, /* Pointer to vfs object */
38852 const char *zPath, /* Possibly relative input path */
38853 int nOut, /* Size of output buffer in bytes */
38854 char *zOut /* Output buffer */
38855){
38856#if !defined(HAVE_READLINK1) || !defined(HAVE_LSTAT1)
38857 return mkFullPathname(zPath, zOut, nOut);
38858#else
38859 int rc = SQLITE_OK0;
38860 int nByte;
38861 int nLink = 1; /* Number of symbolic links followed so far */
38862 const char *zIn = zPath; /* Input path for each iteration of loop */
38863 char *zDel = 0;
38864
38865 assert( pVfs->mxPathname==MAX_PATHNAME )((void) (0));
38866 UNUSED_PARAMETER(pVfs)(void)(pVfs);
38867
38868 /* It's odd to simulate an io-error here, but really this is just
38869 ** using the io-error infrastructure to test that SQLite handles this
38870 ** function failing. This function could fail if, for example, the
38871 ** current working directory has been unlinked.
38872 */
38873 SimulateIOError( return SQLITE_ERROR );
38874
38875 do {
38876
38877 /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
38878 ** link, or false otherwise. */
38879 int bLink = 0;
38880 struct stat buf;
38881 if( osLstat((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)(zIn, &buf)!=0 ){
38882 if( errno(*__errno_location ())!=ENOENT2 ){
38883 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn)unixLogErrorAtLine(sqlite3CantopenError(38883),"lstat",zIn,38883
)
;
38884 }
38885 }else{
38886 bLink = S_ISLNK(buf.st_mode)((((buf.st_mode)) & 0170000) == (0120000));
38887 }
38888
38889 if( bLink ){
38890 if( zDel==0 ){
38891 zDel = sqlite3_malloc(nOut);
38892 if( zDel==0 ) rc = SQLITE_NOMEM_BKPT7;
38893 }else if( ++nLink>SQLITE_MAX_SYMLINKS100 ){
38894 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(38894);
38895 }
38896
38897 if( rc==SQLITE_OK0 ){
38898 nByte = osReadlink((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)(zIn, zDel, nOut-1);
38899 if( nByte<0 ){
38900 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn)unixLogErrorAtLine(sqlite3CantopenError(38900),"readlink",zIn
,38900)
;
38901 }else{
38902 if( zDel[0]!='/' ){
38903 int n;
38904 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
38905 if( nByte+n+1>nOut ){
38906 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(38906);
38907 }else{
38908 memmove(&zDel[n], zDel, nByte+1);
38909 memcpy(zDel, zIn, n);
38910 nByte += n;
38911 }
38912 }
38913 zDel[nByte] = '\0';
38914 }
38915 }
38916
38917 zIn = zDel;
38918 }
38919
38920 assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' )((void) (0));
38921 if( rc==SQLITE_OK0 && zIn!=zOut ){
38922 rc = mkFullPathname(zIn, zOut, nOut);
38923 }
38924 if( bLink==0 ) break;
38925 zIn = zOut;
38926 }while( rc==SQLITE_OK0 );
38927
38928 sqlite3_free(zDel);
38929 return rc;
38930#endif /* HAVE_READLINK && HAVE_LSTAT */
38931}
38932
38933
38934#ifndef SQLITE_OMIT_LOAD_EXTENSION
38935/*
38936** Interfaces for opening a shared library, finding entry points
38937** within the shared library, and closing the shared library.
38938*/
38939#include <dlfcn.h>
38940static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
38941 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
38942 return dlopen(zFilename, RTLD_NOW0x00002 | RTLD_GLOBAL0x00100);
38943}
38944
38945/*
38946** SQLite calls this function immediately after a call to unixDlSym() or
38947** unixDlOpen() fails (returns a null pointer). If a more detailed error
38948** message is available, it is written to zBufOut. If no error message
38949** is available, zBufOut is left unmodified and SQLite uses a default
38950** error message.
38951*/
38952static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
38953 const char *zErr;
38954 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
38955 unixEnterMutex();
38956 zErr = dlerror();
38957 if( zErr ){
38958 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
38959 }
38960 unixLeaveMutex();
38961}
38962static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
38963 /*
38964 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
38965 ** cast into a pointer to a function. And yet the library dlsym() routine
38966 ** returns a void* which is really a pointer to a function. So how do we
38967 ** use dlsym() with -pedantic-errors?
38968 **
38969 ** Variable x below is defined to be a pointer to a function taking
38970 ** parameters void* and const char* and returning a pointer to a function.
38971 ** We initialize x by assigning it a pointer to the dlsym() function.
38972 ** (That assignment requires a cast.) Then we call the function that
38973 ** x points to.
38974 **
38975 ** This work-around is unlikely to work correctly on any system where
38976 ** you really cannot cast a function pointer into void*. But then, on the
38977 ** other hand, dlsym() will not work on such a system either, so we have
38978 ** not really lost anything.
38979 */
38980 void (*(*x)(void*,const char*))(void);
38981 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
38982 x = (void(*(*)(void*,const char*))(void))dlsym;
38983 return (*x)(p, zSym);
38984}
38985static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
38986 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
38987 dlclose(pHandle);
38988}
38989#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
38990 #define unixDlOpen 0
38991 #define unixDlError 0
38992 #define unixDlSym 0
38993 #define unixDlClose 0
38994#endif
38995
38996/*
38997** Write nBuf bytes of random data to the supplied buffer zBuf.
38998*/
38999static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
39000 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
39001 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)))((void) (0));
39002
39003 /* We have to initialize zBuf to prevent valgrind from reporting
39004 ** errors. The reports issued by valgrind are incorrect - we would
39005 ** prefer that the randomness be increased by making use of the
39006 ** uninitialized space in zBuf - but valgrind errors tend to worry
39007 ** some users. Rather than argue, it seems easier just to initialize
39008 ** the whole array and silence valgrind, even if that means less randomness
39009 ** in the random seed.
39010 **
39011 ** When testing, initializing zBuf[] to zero is all we do. That means
39012 ** that we always use the same random number sequence. This makes the
39013 ** tests repeatable.
39014 */
39015 memset(zBuf, 0, nBuf);
39016 randomnessPid = osGetpid(0)(pid_t)getpid();
39017#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
39018 {
39019 int fd, got;
39020 fd = robust_open("/dev/urandom", O_RDONLY00, 0);
39021 if( fd<0 ){
39022 time_t t;
39023 time(&t);
39024 memcpy(zBuf, &t, sizeof(t));
39025 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
39026 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf )((void) (0));
39027 nBuf = sizeof(t) + sizeof(randomnessPid);
39028 }else{
39029 do{ got = osRead((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)(fd, zBuf, nBuf); }while( got<0 && errno(*__errno_location ())==EINTR4 );
39030 robust_close(0, fd, __LINE__39030);
39031 }
39032 }
39033#endif
39034 return nBuf;
39035}
39036
39037
39038/*
39039** Sleep for a little while. Return the amount of time slept.
39040** The argument is the number of microseconds we want to sleep.
39041** The return value is the number of microseconds of sleep actually
39042** requested from the underlying operating system, a number which
39043** might be greater than or equal to the argument, but not less
39044** than the argument.
39045*/
39046static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
39047#if OS_VXWORKS0
39048 struct timespec sp;
39049
39050 sp.tv_sec = microseconds / 1000000;
39051 sp.tv_nsec = (microseconds % 1000000) * 1000;
39052 nanosleep(&sp, NULL((void*)0));
39053 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
39054 return microseconds;
39055#elif defined(HAVE_USLEEP) && HAVE_USLEEP
39056 usleep(microseconds);
39057 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
39058 return microseconds;
39059#else
39060 int seconds = (microseconds+999999)/1000000;
39061 sleep(seconds);
39062 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
39063 return seconds*1000000;
39064#endif
39065}
39066
39067/*
39068** The following variable, if set to a non-zero value, is interpreted as
39069** the number of seconds since 1970 and is used to set the result of
39070** sqlite3OsCurrentTime() during testing.
39071*/
39072#ifdef SQLITE_TEST
39073SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
39074#endif
39075
39076/*
39077** Find the current time (in Universal Coordinated Time). Write into *piNow
39078** the current time and date as a Julian Day number times 86_400_000. In
39079** other words, write into *piNow the number of milliseconds since the Julian
39080** epoch of noon in Greenwich on November 24, 4714 B.C according to the
39081** proleptic Gregorian calendar.
39082**
39083** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
39084** cannot be found.
39085*/
39086static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
39087 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
39088 int rc = SQLITE_OK0;
39089#if defined(NO_GETTOD)
39090 time_t t;
39091 time(&t);
39092 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
39093#elif OS_VXWORKS0
39094 struct timespec sNow;
39095 clock_gettime(CLOCK_REALTIME0, &sNow);
39096 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
39097#else
39098 struct timeval sNow;
39099 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
39100 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
39101#endif
39102
39103#ifdef SQLITE_TEST
39104 if( sqlite3_current_time ){
39105 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
39106 }
39107#endif
39108 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
39109 return rc;
39110}
39111
39112#ifndef SQLITE_OMIT_DEPRECATED
39113/*
39114** Find the current time (in Universal Coordinated Time). Write the
39115** current time and date as a Julian Day number into *prNow and
39116** return 0. Return 1 if the time and date cannot be found.
39117*/
39118static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
39119 sqlite3_int64 i = 0;
39120 int rc;
39121 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
39122 rc = unixCurrentTimeInt64(0, &i);
39123 *prNow = i/86400000.0;
39124 return rc;
39125}
39126#else
39127# define unixCurrentTime 0
39128#endif
39129
39130/*
39131** The xGetLastError() method is designed to return a better
39132** low-level error message when operating-system problems come up
39133** during SQLite operation. Only the integer return code is currently
39134** used.
39135*/
39136static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
39137 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
39138 UNUSED_PARAMETER(NotUsed2)(void)(NotUsed2);
39139 UNUSED_PARAMETER(NotUsed3)(void)(NotUsed3);
39140 return errno(*__errno_location ());
39141}
39142
39143
39144/*
39145************************ End of sqlite3_vfs methods ***************************
39146******************************************************************************/
39147
39148/******************************************************************************
39149************************** Begin Proxy Locking ********************************
39150**
39151** Proxy locking is a "uber-locking-method" in this sense: It uses the
39152** other locking methods on secondary lock files. Proxy locking is a
39153** meta-layer over top of the primitive locking implemented above. For
39154** this reason, the division that implements of proxy locking is deferred
39155** until late in the file (here) after all of the other I/O methods have
39156** been defined - so that the primitive locking methods are available
39157** as services to help with the implementation of proxy locking.
39158**
39159****
39160**
39161** The default locking schemes in SQLite use byte-range locks on the
39162** database file to coordinate safe, concurrent access by multiple readers
39163** and writers [http://sqlite.org/lockingv3.html]. The five file locking
39164** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
39165** as POSIX read & write locks over fixed set of locations (via fsctl),
39166** on AFP and SMB only exclusive byte-range locks are available via fsctl
39167** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
39168** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
39169** address in the shared range is taken for a SHARED lock, the entire
39170** shared range is taken for an EXCLUSIVE lock):
39171**
39172** PENDING_BYTE 0x40000000
39173** RESERVED_BYTE 0x40000001
39174** SHARED_RANGE 0x40000002 -> 0x40000200
39175**
39176** This works well on the local file system, but shows a nearly 100x
39177** slowdown in read performance on AFP because the AFP client disables
39178** the read cache when byte-range locks are present. Enabling the read
39179** cache exposes a cache coherency problem that is present on all OS X
39180** supported network file systems. NFS and AFP both observe the
39181** close-to-open semantics for ensuring cache coherency
39182** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
39183** address the requirements for concurrent database access by multiple
39184** readers and writers
39185** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
39186**
39187** To address the performance and cache coherency issues, proxy file locking
39188** changes the way database access is controlled by limiting access to a
39189** single host at a time and moving file locks off of the database file
39190** and onto a proxy file on the local file system.
39191**
39192**
39193** Using proxy locks
39194** -----------------
39195**
39196** C APIs
39197**
39198** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
39199** <proxy_path> | ":auto:");
39200** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
39201** &<proxy_path>);
39202**
39203**
39204** SQL pragmas
39205**
39206** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
39207** PRAGMA [database.]lock_proxy_file
39208**
39209** Specifying ":auto:" means that if there is a conch file with a matching
39210** host ID in it, the proxy path in the conch file will be used, otherwise
39211** a proxy path based on the user's temp dir
39212** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
39213** actual proxy file name is generated from the name and path of the
39214** database file. For example:
39215**
39216** For database path "/Users/me/foo.db"
39217** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
39218**
39219** Once a lock proxy is configured for a database connection, it can not
39220** be removed, however it may be switched to a different proxy path via
39221** the above APIs (assuming the conch file is not being held by another
39222** connection or process).
39223**
39224**
39225** How proxy locking works
39226** -----------------------
39227**
39228** Proxy file locking relies primarily on two new supporting files:
39229**
39230** * conch file to limit access to the database file to a single host
39231** at a time
39232**
39233** * proxy file to act as a proxy for the advisory locks normally
39234** taken on the database
39235**
39236** The conch file - to use a proxy file, sqlite must first "hold the conch"
39237** by taking an sqlite-style shared lock on the conch file, reading the
39238** contents and comparing the host's unique host ID (see below) and lock
39239** proxy path against the values stored in the conch. The conch file is
39240** stored in the same directory as the database file and the file name
39241** is patterned after the database file name as ".<databasename>-conch".
39242** If the conch file does not exist, or its contents do not match the
39243** host ID and/or proxy path, then the lock is escalated to an exclusive
39244** lock and the conch file contents is updated with the host ID and proxy
39245** path and the lock is downgraded to a shared lock again. If the conch
39246** is held by another process (with a shared lock), the exclusive lock
39247** will fail and SQLITE_BUSY is returned.
39248**
39249** The proxy file - a single-byte file used for all advisory file locks
39250** normally taken on the database file. This allows for safe sharing
39251** of the database file for multiple readers and writers on the same
39252** host (the conch ensures that they all use the same local lock file).
39253**
39254** Requesting the lock proxy does not immediately take the conch, it is
39255** only taken when the first request to lock database file is made.
39256** This matches the semantics of the traditional locking behavior, where
39257** opening a connection to a database file does not take a lock on it.
39258** The shared lock and an open file descriptor are maintained until
39259** the connection to the database is closed.
39260**
39261** The proxy file and the lock file are never deleted so they only need
39262** to be created the first time they are used.
39263**
39264** Configuration options
39265** ---------------------
39266**
39267** SQLITE_PREFER_PROXY_LOCKING
39268**
39269** Database files accessed on non-local file systems are
39270** automatically configured for proxy locking, lock files are
39271** named automatically using the same logic as
39272** PRAGMA lock_proxy_file=":auto:"
39273**
39274** SQLITE_PROXY_DEBUG
39275**
39276** Enables the logging of error messages during host id file
39277** retrieval and creation
39278**
39279** LOCKPROXYDIR
39280**
39281** Overrides the default directory used for lock proxy files that
39282** are named automatically via the ":auto:" setting
39283**
39284** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
39285**
39286** Permissions to use when creating a directory for storing the
39287** lock proxy files, only used when LOCKPROXYDIR is not set.
39288**
39289**
39290** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
39291** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
39292** force proxy locking to be used for every database file opened, and 0
39293** will force automatic proxy locking to be disabled for all database
39294** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
39295** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
39296*/
39297
39298/*
39299** Proxy locking is only available on MacOSX
39300*/
39301#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE0
39302
39303/*
39304** The proxyLockingContext has the path and file structures for the remote
39305** and local proxy files in it
39306*/
39307typedef struct proxyLockingContext proxyLockingContext;
39308struct proxyLockingContext {
39309 unixFile *conchFile; /* Open conch file */
39310 char *conchFilePath; /* Name of the conch file */
39311 unixFile *lockProxy; /* Open proxy lock file */
39312 char *lockProxyPath; /* Name of the proxy lock file */
39313 char *dbPath; /* Name of the open file */
39314 int conchHeld; /* 1 if the conch is held, -1 if lockless */
39315 int nFails; /* Number of conch taking failures */
39316 void *oldLockingContext; /* Original lockingcontext to restore on close */
39317 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
39318};
39319
39320/*
39321** The proxy lock file path for the database at dbPath is written into lPath,
39322** which must point to valid, writable memory large enough for a maxLen length
39323** file path.
39324*/
39325static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
39326 int len;
39327 int dbLen;
39328 int i;
39329
39330#ifdef LOCKPROXYDIR
39331 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
39332#else
39333# ifdef _CS_DARWIN_USER_TEMP_DIR
39334 {
39335 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
39336 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
39337 lPath, errno, osGetpid(0)));
39338 return SQLITE_IOERR_LOCK(10 | (15<<8));
39339 }
39340 len = strlcat(lPath, "sqliteplocks", maxLen);
39341 }
39342# else
39343 len = strlcpy(lPath, "/tmp/", maxLen);
39344# endif
39345#endif
39346
39347 if( lPath[len-1]!='/' ){
39348 len = strlcat(lPath, "/", maxLen);
39349 }
39350
39351 /* transform the db path to a unique cache name */
39352 dbLen = (int)strlen(dbPath);
39353 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
39354 char c = dbPath[i];
39355 lPath[i+len] = (c=='/')?'_':c;
39356 }
39357 lPath[i+len]='\0';
39358 strlcat(lPath, ":auto:", maxLen);
39359 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
39360 return SQLITE_OK0;
39361}
39362
39363/*
39364 ** Creates the lock file and any missing directories in lockPath
39365 */
39366static int proxyCreateLockPath(const char *lockPath){
39367 int i, len;
39368 char buf[MAXPATHLEN];
39369 int start = 0;
39370
39371 assert(lockPath!=NULL)((void) (0));
39372 /* try to create all the intermediate directories */
39373 len = (int)strlen(lockPath);
39374 buf[0] = lockPath[0];
39375 for( i=1; i<len; i++ ){
39376 if( lockPath[i] == '/' && (i - start > 0) ){
39377 /* only mkdir if leaf dir != "." or "/" or ".." */
39378 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
39379 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
39380 buf[i]='\0';
39381 if( osMkdir((int(*)(const char*,mode_t))aSyscall[18].pCurrent)(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS0755) ){
39382 int err=errno(*__errno_location ());
39383 if( err!=EEXIST17 ) {
39384 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
39385 "'%s' proxy lock path=%s pid=%d\n",
39386 buf, strerror(err), lockPath, osGetpid(0)));
39387 return err;
39388 }
39389 }
39390 }
39391 start=i+1;
39392 }
39393 buf[i] = lockPath[i];
39394 }
39395 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
39396 return 0;
39397}
39398
39399/*
39400** Create a new VFS file descriptor (stored in memory obtained from
39401** sqlite3_malloc) and open the file named "path" in the file descriptor.
39402**
39403** The caller is responsible not only for closing the file descriptor
39404** but also for freeing the memory associated with the file descriptor.
39405*/
39406static int proxyCreateUnixFile(
39407 const char *path, /* path for the new unixFile */
39408 unixFile **ppFile, /* unixFile created and returned by ref */
39409 int islockfile /* if non zero missing dirs will be created */
39410) {
39411 int fd = -1;
39412 unixFile *pNew;
39413 int rc = SQLITE_OK0;
39414 int openFlags = O_RDWR02 | O_CREAT0100;
39415 sqlite3_vfs dummyVfs;
39416 int terrno = 0;
39417 UnixUnusedFd *pUnused = NULL((void*)0);
39418
39419 /* 1. first try to open/create the file
39420 ** 2. if that fails, and this is a lock file (not-conch), try creating
39421 ** the parent directories and then try again.
39422 ** 3. if that fails, try to open the file read-only
39423 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
39424 */
39425 pUnused = findReusableFd(path, openFlags);
39426 if( pUnused ){
39427 fd = pUnused->fd;
39428 }else{
39429 pUnused = sqlite3_malloc64(sizeof(*pUnused));
39430 if( !pUnused ){
39431 return SQLITE_NOMEM_BKPT7;
39432 }
39433 }
39434 if( fd<0 ){
39435 fd = robust_open(path, openFlags, 0);
39436 terrno = errno(*__errno_location ());
39437 if( fd<0 && errno(*__errno_location ())==ENOENT2 && islockfile ){
39438 if( proxyCreateLockPath(path) == SQLITE_OK0 ){
39439 fd = robust_open(path, openFlags, 0);
39440 }
39441 }
39442 }
39443 if( fd<0 ){
39444 openFlags = O_RDONLY00;
39445 fd = robust_open(path, openFlags, 0);
39446 terrno = errno(*__errno_location ());
39447 }
39448 if( fd<0 ){
39449 if( islockfile ){
39450 return SQLITE_BUSY5;
39451 }
39452 switch (terrno) {
39453 case EACCES13:
39454 return SQLITE_PERM3;
39455 case EIO5:
39456 return SQLITE_IOERR_LOCK(10 | (15<<8)); /* even though it is the conch */
39457 default:
39458 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(39458);
39459 }
39460 }
39461
39462 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
39463 if( pNew==NULL((void*)0) ){
39464 rc = SQLITE_NOMEM_BKPT7;
39465 goto end_create_proxy;
39466 }
39467 memset(pNew, 0, sizeof(unixFile));
39468 pNew->openFlags = openFlags;
39469 memset(&dummyVfs, 0, sizeof(dummyVfs));
39470 dummyVfs.pAppData = (void*)&autolockIoFinder;
39471 dummyVfs.zName = "dummy";
39472 pUnused->fd = fd;
39473 pUnused->flags = openFlags;
39474 pNew->pPreallocatedUnused = pUnused;
39475
39476 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
39477 if( rc==SQLITE_OK0 ){
39478 *ppFile = pNew;
39479 return SQLITE_OK0;
39480 }
39481end_create_proxy:
39482 robust_close(pNew, fd, __LINE__39482);
39483 sqlite3_free(pNew);
39484 sqlite3_free(pUnused);
39485 return rc;
39486}
39487
39488#ifdef SQLITE_TEST
39489/* simulate multiple hosts by creating unique hostid file paths */
39490SQLITE_API int sqlite3_hostid_num = 0;
39491#endif
39492
39493#define PROXY_HOSTIDLEN 16 /* conch file host id length */
39494
39495#ifdef HAVE_GETHOSTUUID
39496/* Not always defined in the headers as it ought to be */
39497extern int gethostuuid(uuid_t id, const struct timespec *wait);
39498#endif
39499
39500/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
39501** bytes of writable memory.
39502*/
39503static int proxyGetHostID(unsigned char *pHostID, int *pError){
39504 assert(PROXY_HOSTIDLEN == sizeof(uuid_t))((void) (0));
39505 memset(pHostID, 0, PROXY_HOSTIDLEN);
39506#ifdef HAVE_GETHOSTUUID
39507 {
39508 struct timespec timeout = {1, 0}; /* 1 sec timeout */
39509 if( gethostuuid(pHostID, &timeout) ){
39510 int err = errno(*__errno_location ());
39511 if( pError ){
39512 *pError = err;
39513 }
39514 return SQLITE_IOERR10;
39515 }
39516 }
39517#else
39518 UNUSED_PARAMETER(pError)(void)(pError);
39519#endif
39520#ifdef SQLITE_TEST
39521 /* simulate multiple hosts by creating unique hostid file paths */
39522 if( sqlite3_hostid_num != 0){
39523 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
39524 }
39525#endif
39526
39527 return SQLITE_OK0;
39528}
39529
39530/* The conch file contains the header, host id and lock file path
39531 */
39532#define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
39533#define PROXY_HEADERLEN 1 /* conch file header length */
39534#define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
39535#define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
39536
39537/*
39538** Takes an open conch file, copies the contents to a new path and then moves
39539** it back. The newly created file's file descriptor is assigned to the
39540** conch file structure and finally the original conch file descriptor is
39541** closed. Returns zero if successful.
39542*/
39543static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
39544 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39545 unixFile *conchFile = pCtx->conchFile;
39546 char tPath[MAXPATHLEN];
39547 char buf[PROXY_MAXCONCHLEN];
39548 char *cPath = pCtx->conchFilePath;
39549 size_t readLen = 0;
39550 size_t pathLen = 0;
39551 char errmsg[64] = "";
39552 int fd = -1;
39553 int rc = -1;
39554 UNUSED_PARAMETER(myHostID)(void)(myHostID);
39555
39556 /* create a new path by replace the trailing '-conch' with '-break' */
39557 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
39558 if( pathLen>MAXPATHLEN || pathLen<6 ||
39559 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
39560 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
39561 goto end_breaklock;
39562 }
39563 /* read the conch content */
39564 readLen = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
39565 if( readLen<PROXY_PATHINDEX ){
39566 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
39567 goto end_breaklock;
39568 }
39569 /* write it out to the temporary break file */
39570 fd = robust_open(tPath, (O_RDWR02|O_CREAT0100|O_EXCL0200), 0);
39571 if( fd<0 ){
39572 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno(*__errno_location ()));
39573 goto end_breaklock;
39574 }
39575 if( osPwrite((ssize_t(*)(int,const void*,size_t,off_t)) aSyscall[12].pCurrent
)
(fd, buf, readLen, 0) != (ssize_t)readLen ){
39576 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno(*__errno_location ()));
39577 goto end_breaklock;
39578 }
39579 if( rename(tPath, cPath) ){
39580 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno(*__errno_location ()));
39581 goto end_breaklock;
39582 }
39583 rc = 0;
39584 fprintf(stderrstderr, "broke stale lock on %s\n", cPath);
39585 robust_close(pFile, conchFile->h, __LINE__39585);
39586 conchFile->h = fd;
39587 conchFile->openFlags = O_RDWR02 | O_CREAT0100;
39588
39589end_breaklock:
39590 if( rc ){
39591 if( fd>=0 ){
39592 osUnlink((int(*)(const char*))aSyscall[16].pCurrent)(tPath);
39593 robust_close(pFile, fd, __LINE__39593);
39594 }
39595 fprintf(stderrstderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
39596 }
39597 return rc;
39598}
39599
39600/* Take the requested lock on the conch file and break a stale lock if the
39601** host id matches.
39602*/
39603static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
39604 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39605 unixFile *conchFile = pCtx->conchFile;
39606 int rc = SQLITE_OK0;
39607 int nTries = 0;
39608 struct timespec conchModTime;
39609
39610 memset(&conchModTime, 0, sizeof(conchModTime));
39611 do {
39612 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
39613 nTries ++;
39614 if( rc==SQLITE_BUSY5 ){
39615 /* If the lock failed (busy):
39616 * 1st try: get the mod time of the conch, wait 0.5s and try again.
39617 * 2nd try: fail if the mod time changed or host id is different, wait
39618 * 10 sec and try again
39619 * 3rd try: break the lock unless the mod time has changed.
39620 */
39621 struct stat buf;
39622 if( osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(conchFile->h, &buf) ){
39623 storeLastErrno(pFile, errno(*__errno_location ()));
39624 return SQLITE_IOERR_LOCK(10 | (15<<8));
39625 }
39626
39627 if( nTries==1 ){
39628 conchModTime = buf.st_mtimespec;
39629 usleep(500000); /* wait 0.5 sec and try the lock again*/
39630 continue;
39631 }
39632
39633 assert( nTries>1 )((void) (0));
39634 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
39635 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
39636 return SQLITE_BUSY5;
39637 }
39638
39639 if( nTries==2 ){
39640 char tBuf[PROXY_MAXCONCHLEN];
39641 int len = osPread((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
39642 if( len<0 ){
39643 storeLastErrno(pFile, errno(*__errno_location ()));
39644 return SQLITE_IOERR_LOCK(10 | (15<<8));
39645 }
39646 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
39647 /* don't break the lock if the host id doesn't match */
39648 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
39649 return SQLITE_BUSY5;
39650 }
39651 }else{
39652 /* don't break the lock on short read or a version mismatch */
39653 return SQLITE_BUSY5;
39654 }
39655 usleep(10000000); /* wait 10 sec and try the lock again */
39656 continue;
39657 }
39658
39659 assert( nTries==3 )((void) (0));
39660 if( 0==proxyBreakConchLock(pFile, myHostID) ){
39661 rc = SQLITE_OK0;
39662 if( lockType==EXCLUSIVE_LOCK4 ){
39663 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK1);
39664 }
39665 if( !rc ){
39666 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
39667 }
39668 }
39669 }
39670 } while( rc==SQLITE_BUSY5 && nTries<3 );
39671
39672 return rc;
39673}
39674
39675/* Takes the conch by taking a shared lock and read the contents conch, if
39676** lockPath is non-NULL, the host ID and lock file path must match. A NULL
39677** lockPath means that the lockPath in the conch file will be used if the
39678** host IDs match, or a new lock path will be generated automatically
39679** and written to the conch file.
39680*/
39681static int proxyTakeConch(unixFile *pFile){
39682 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
39683
39684 if( pCtx->conchHeld!=0 ){
39685 return SQLITE_OK0;
39686 }else{
39687 unixFile *conchFile = pCtx->conchFile;
39688 uuid_t myHostID;
39689 int pError = 0;
39690 char readBuf[PROXY_MAXCONCHLEN];
39691 char lockPath[MAXPATHLEN];
39692 char *tempLockPath = NULL((void*)0);
39693 int rc = SQLITE_OK0;
39694 int createConch = 0;
39695 int hostIdMatch = 0;
39696 int readLen = 0;
39697 int tryOldLockPath = 0;
39698 int forceNewLockPath = 0;
39699
39700 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
39701 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
39702 osGetpid(0)));
39703
39704 rc = proxyGetHostID(myHostID, &pError);
39705 if( (rc&0xff)==SQLITE_IOERR10 ){
39706 storeLastErrno(pFile, pError);
39707 goto end_takeconch;
39708 }
39709 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK1);
39710 if( rc!=SQLITE_OK0 ){
39711 goto end_takeconch;
39712 }
39713 /* read the existing conch file */
39714 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
39715 if( readLen<0 ){
39716 /* I/O error: lastErrno set by seekAndRead */
39717 storeLastErrno(pFile, conchFile->lastErrno);
39718 rc = SQLITE_IOERR_READ(10 | (1<<8));
39719 goto end_takeconch;
39720 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
39721 readBuf[0]!=(char)PROXY_CONCHVERSION ){
39722 /* a short read or version format mismatch means we need to create a new
39723 ** conch file.
39724 */
39725 createConch = 1;
39726 }
39727 /* if the host id matches and the lock path already exists in the conch
39728 ** we'll try to use the path there, if we can't open that path, we'll
39729 ** retry with a new auto-generated path
39730 */
39731 do { /* in case we need to try again for an :auto: named lock file */
39732
39733 if( !createConch && !forceNewLockPath ){
39734 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
39735 PROXY_HOSTIDLEN);
39736 /* if the conch has data compare the contents */
39737 if( !pCtx->lockProxyPath ){
39738 /* for auto-named local lock file, just check the host ID and we'll
39739 ** use the local lock file path that's already in there
39740 */
39741 if( hostIdMatch ){
39742 size_t pathLen = (readLen - PROXY_PATHINDEX);
39743
39744 if( pathLen>=MAXPATHLEN ){
39745 pathLen=MAXPATHLEN-1;
39746 }
39747 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
39748 lockPath[pathLen] = 0;
39749 tempLockPath = lockPath;
39750 tryOldLockPath = 1;
39751 /* create a copy of the lock path if the conch is taken */
39752 goto end_takeconch;
39753 }
39754 }else if( hostIdMatch
39755 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
39756 readLen-PROXY_PATHINDEX)
39757 ){
39758 /* conch host and lock path match */
39759 goto end_takeconch;
39760 }
39761 }
39762
39763 /* if the conch isn't writable and doesn't match, we can't take it */
39764 if( (conchFile->openFlags&O_RDWR02) == 0 ){
39765 rc = SQLITE_BUSY5;
39766 goto end_takeconch;
39767 }
39768
39769 /* either the conch didn't match or we need to create a new one */
39770 if( !pCtx->lockProxyPath ){
39771 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
39772 tempLockPath = lockPath;
39773 /* create a copy of the lock path _only_ if the conch is taken */
39774 }
39775
39776 /* update conch with host and path (this will fail if other process
39777 ** has a shared lock already), if the host id matches, use the big
39778 ** stick.
39779 */
39780 futimes(conchFile->h, NULL((void*)0));
39781 if( hostIdMatch && !createConch ){
39782 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
39783 /* We are trying for an exclusive lock but another thread in this
39784 ** same process is still holding a shared lock. */
39785 rc = SQLITE_BUSY5;
39786 } else {
39787 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK4);
39788 }
39789 }else{
39790 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK4);
39791 }
39792 if( rc==SQLITE_OK0 ){
39793 char writeBuffer[PROXY_MAXCONCHLEN];
39794 int writeSize = 0;
39795
39796 writeBuffer[0] = (char)PROXY_CONCHVERSION;
39797 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
39798 if( pCtx->lockProxyPath!=NULL((void*)0) ){
39799 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
39800 MAXPATHLEN);
39801 }else{
39802 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
39803 }
39804 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
39805 robust_ftruncate(conchFile->h, writeSize);
39806 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
39807 full_fsync(conchFile->h,0,0);
39808 /* If we created a new conch file (not just updated the contents of a
39809 ** valid conch file), try to match the permissions of the database
39810 */
39811 if( rc==SQLITE_OK0 && createConch ){
39812 struct stat buf;
39813 int err = osFstat((int(*)(int,struct stat*))aSyscall[5].pCurrent)(pFile->h, &buf);
39814 if( err==0 ){
39815 mode_t cmode = buf.st_mode&(S_IRUSR0400|S_IWUSR0200 | S_IRGRP(0400 >> 3)|S_IWGRP(0200 >> 3) |
39816 S_IROTH((0400 >> 3) >> 3)|S_IWOTH((0200 >> 3) >> 3));
39817 /* try to match the database file R/W permissions, ignore failure */
39818#ifndef SQLITE_PROXY_DEBUG
39819 osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(conchFile->h, cmode);
39820#else
39821 do{
39822 rc = osFchmod((int(*)(int,mode_t))aSyscall[14].pCurrent)(conchFile->h, cmode);
39823 }while( rc==(-1) && errno(*__errno_location ())==EINTR4 );
39824 if( rc!=0 ){
39825 int code = errno(*__errno_location ());
39826 fprintf(stderrstderr, "fchmod %o FAILED with %d %s\n",
39827 cmode, code, strerror(code));
39828 } else {
39829 fprintf(stderrstderr, "fchmod %o SUCCEDED\n",cmode);
39830 }
39831 }else{
39832 int code = errno(*__errno_location ());
39833 fprintf(stderrstderr, "STAT FAILED[%d] with %d %s\n",
39834 err, code, strerror(code));
39835#endif
39836 }
39837 }
39838 }
39839 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK1);
39840
39841 end_takeconch:
39842 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
39843 if( rc==SQLITE_OK0 && pFile->openFlags ){
39844 int fd;
39845 if( pFile->h>=0 ){
39846 robust_close(pFile, pFile->h, __LINE__39846);
39847 }
39848 pFile->h = -1;
39849 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
39850 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
39851 if( fd>=0 ){
39852 pFile->h = fd;
39853 }else{
39854 rc=SQLITE_CANTOPEN_BKPTsqlite3CantopenError(39854); /* SQLITE_BUSY? proxyTakeConch called
39855 during locking */
39856 }
39857 }
39858 if( rc==SQLITE_OK0 && !pCtx->lockProxy ){
39859 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
39860 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
39861 if( rc!=SQLITE_OK0 && rc!=SQLITE_NOMEM7 && tryOldLockPath ){
39862 /* we couldn't create the proxy lock file with the old lock file path
39863 ** so try again via auto-naming
39864 */
39865 forceNewLockPath = 1;
39866 tryOldLockPath = 0;
39867 continue; /* go back to the do {} while start point, try again */
39868 }
39869 }
39870 if( rc==SQLITE_OK0 ){
39871 /* Need to make a copy of path if we extracted the value
39872 ** from the conch file or the path was allocated on the stack
39873 */
39874 if( tempLockPath ){
39875 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
39876 if( !pCtx->lockProxyPath ){
39877 rc = SQLITE_NOMEM_BKPT7;
39878 }
39879 }
39880 }
39881 if( rc==SQLITE_OK0 ){
39882 pCtx->conchHeld = 1;
39883
39884 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
39885 afpLockingContext *afpCtx;
39886 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
39887 afpCtx->dbPath = pCtx->lockProxyPath;
39888 }
39889 } else {
39890 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK0);
39891 }
39892 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
39893 rc==SQLITE_OK?"ok":"failed"));
39894 return rc;
39895 } while (1); /* in case we need to retry the :auto: lock file -
39896 ** we should never get here except via the 'continue' call. */
39897 }
39898}
39899
39900/*
39901** If pFile holds a lock on a conch file, then release that lock.
39902*/
39903static int proxyReleaseConch(unixFile *pFile){
39904 int rc = SQLITE_OK0; /* Subroutine return code */
39905 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
39906 unixFile *conchFile; /* Name of the conch file */
39907
39908 pCtx = (proxyLockingContext *)pFile->lockingContext;
39909 conchFile = pCtx->conchFile;
39910 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
39911 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
39912 osGetpid(0)));
39913 if( pCtx->conchHeld>0 ){
39914 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK0);
39915 }
39916 pCtx->conchHeld = 0;
39917 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
39918 (rc==SQLITE_OK ? "ok" : "failed")));
39919 return rc;
39920}
39921
39922/*
39923** Given the name of a database file, compute the name of its conch file.
39924** Store the conch filename in memory obtained from sqlite3_malloc64().
39925** Make *pConchPath point to the new name. Return SQLITE_OK on success
39926** or SQLITE_NOMEM if unable to obtain memory.
39927**
39928** The caller is responsible for ensuring that the allocated memory
39929** space is eventually freed.
39930**
39931** *pConchPath is set to NULL if a memory allocation error occurs.
39932*/
39933static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
39934 int i; /* Loop counter */
39935 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
39936 char *conchPath; /* buffer in which to construct conch name */
39937
39938 /* Allocate space for the conch filename and initialize the name to
39939 ** the name of the original database file. */
39940 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
39941 if( conchPath==0 ){
39942 return SQLITE_NOMEM_BKPT7;
39943 }
39944 memcpy(conchPath, dbPath, len+1);
39945
39946 /* now insert a "." before the last / character */
39947 for( i=(len-1); i>=0; i-- ){
39948 if( conchPath[i]=='/' ){
39949 i++;
39950 break;
39951 }
39952 }
39953 conchPath[i]='.';
39954 while ( i<len ){
39955 conchPath[i+1]=dbPath[i];
39956 i++;
39957 }
39958
39959 /* append the "-conch" suffix to the file */
39960 memcpy(&conchPath[i+1], "-conch", 7);
39961 assert( (int)strlen(conchPath) == len+7 )((void) (0));
39962
39963 return SQLITE_OK0;
39964}
39965
39966
39967/* Takes a fully configured proxy locking-style unix file and switches
39968** the local lock file path
39969*/
39970static int switchLockProxyPath(unixFile *pFile, const char *path) {
39971 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
39972 char *oldPath = pCtx->lockProxyPath;
39973 int rc = SQLITE_OK0;
39974
39975 if( pFile->eFileLock!=NO_LOCK0 ){
39976 return SQLITE_BUSY5;
39977 }
39978
39979 /* nothing to do if the path is NULL, :auto: or matches the existing path */
39980 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
39981 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
39982 return SQLITE_OK0;
39983 }else{
39984 unixFile *lockProxy = pCtx->lockProxy;
39985 pCtx->lockProxy=NULL((void*)0);
39986 pCtx->conchHeld = 0;
39987 if( lockProxy!=NULL((void*)0) ){
39988 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
39989 if( rc ) return rc;
39990 sqlite3_free(lockProxy);
39991 }
39992 sqlite3_free(oldPath);
39993 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
39994 }
39995
39996 return rc;
39997}
39998
39999/*
40000** pFile is a file that has been opened by a prior xOpen call. dbPath
40001** is a string buffer at least MAXPATHLEN+1 characters in size.
40002**
40003** This routine find the filename associated with pFile and writes it
40004** int dbPath.
40005*/
40006static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
40007#if defined(__APPLE__)
40008 if( pFile->pMethod == &afpIoMethods ){
40009 /* afp style keeps a reference to the db path in the filePath field
40010 ** of the struct */
40011 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN )((void) (0));
40012 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
40013 MAXPATHLEN);
40014 } else
40015#endif
40016 if( pFile->pMethod == &dotlockIoMethods ){
40017 /* dot lock style uses the locking context to store the dot lock
40018 ** file path */
40019 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX".lock");
40020 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
40021 }else{
40022 /* all other styles use the locking context to store the db file path */
40023 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN )((void) (0));
40024 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
40025 }
40026 return SQLITE_OK0;
40027}
40028
40029/*
40030** Takes an already filled in unix file and alters it so all file locking
40031** will be performed on the local proxy lock file. The following fields
40032** are preserved in the locking context so that they can be restored and
40033** the unix structure properly cleaned up at close time:
40034** ->lockingContext
40035** ->pMethod
40036*/
40037static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
40038 proxyLockingContext *pCtx;
40039 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
40040 char *lockPath=NULL((void*)0);
40041 int rc = SQLITE_OK0;
40042
40043 if( pFile->eFileLock!=NO_LOCK0 ){
40044 return SQLITE_BUSY5;
40045 }
40046 proxyGetDbPathForUnixFile(pFile, dbPath);
40047 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
40048 lockPath=NULL((void*)0);
40049 }else{
40050 lockPath=(char *)path;
40051 }
40052
40053 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
40054 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
40055
40056 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
40057 if( pCtx==0 ){
40058 return SQLITE_NOMEM_BKPT7;
40059 }
40060 memset(pCtx, 0, sizeof(*pCtx));
40061
40062 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
40063 if( rc==SQLITE_OK0 ){
40064 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
40065 if( rc==SQLITE_CANTOPEN14 && ((pFile->openFlags&O_RDWR02) == 0) ){
40066 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
40067 ** (c) the file system is read-only, then enable no-locking access.
40068 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
40069 ** that openFlags will have only one of O_RDONLY or O_RDWR.
40070 */
40071 struct statfs fsInfo;
40072 struct stat conchInfo;
40073 int goLockless = 0;
40074
40075 if( osStat((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)(pCtx->conchFilePath, &conchInfo) == -1 ) {
40076 int err = errno(*__errno_location ());
40077 if( (err==ENOENT2) && (statfs(dbPath, &fsInfo) != -1) ){
40078 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
40079 }
40080 }
40081 if( goLockless ){
40082 pCtx->conchHeld = -1; /* read only FS/ lockless */
40083 rc = SQLITE_OK0;
40084 }
40085 }
40086 }
40087 if( rc==SQLITE_OK0 && lockPath ){
40088 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
40089 }
40090
40091 if( rc==SQLITE_OK0 ){
40092 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
40093 if( pCtx->dbPath==NULL((void*)0) ){
40094 rc = SQLITE_NOMEM_BKPT7;
40095 }
40096 }
40097 if( rc==SQLITE_OK0 ){
40098 /* all memory is allocated, proxys are created and assigned,
40099 ** switch the locking context and pMethod then return.
40100 */
40101 pCtx->oldLockingContext = pFile->lockingContext;
40102 pFile->lockingContext = pCtx;
40103 pCtx->pOldMethod = pFile->pMethod;
40104 pFile->pMethod = &proxyIoMethods;
40105 }else{
40106 if( pCtx->conchFile ){
40107 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
40108 sqlite3_free(pCtx->conchFile);
40109 }
40110 sqlite3DbFree(0, pCtx->lockProxyPath);
40111 sqlite3_free(pCtx->conchFilePath);
40112 sqlite3_free(pCtx);
40113 }
40114 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
40115 (rc==SQLITE_OK ? "ok" : "failed")));
40116 return rc;
40117}
40118
40119
40120/*
40121** This routine handles sqlite3_file_control() calls that are specific
40122** to proxy locking.
40123*/
40124static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
40125 switch( op ){
40126 case SQLITE_FCNTL_GET_LOCKPROXYFILE2: {
40127 unixFile *pFile = (unixFile*)id;
40128 if( pFile->pMethod == &proxyIoMethods ){
40129 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
40130 proxyTakeConch(pFile);
40131 if( pCtx->lockProxyPath ){
40132 *(const char **)pArg = pCtx->lockProxyPath;
40133 }else{
40134 *(const char **)pArg = ":auto: (not held)";
40135 }
40136 } else {
40137 *(const char **)pArg = NULL((void*)0);
40138 }
40139 return SQLITE_OK0;
40140 }
40141 case SQLITE_FCNTL_SET_LOCKPROXYFILE3: {
40142 unixFile *pFile = (unixFile*)id;
40143 int rc = SQLITE_OK0;
40144 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
40145 if( pArg==NULL((void*)0) || (const char *)pArg==0 ){
40146 if( isProxyStyle ){
40147 /* turn off proxy locking - not supported. If support is added for
40148 ** switching proxy locking mode off then it will need to fail if
40149 ** the journal mode is WAL mode.
40150 */
40151 rc = SQLITE_ERROR1 /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
40152 }else{
40153 /* turn off proxy locking - already off - NOOP */
40154 rc = SQLITE_OK0;
40155 }
40156 }else{
40157 const char *proxyPath = (const char *)pArg;
40158 if( isProxyStyle ){
40159 proxyLockingContext *pCtx =
40160 (proxyLockingContext*)pFile->lockingContext;
40161 if( !strcmp(pArg, ":auto:")
40162 || (pCtx->lockProxyPath &&
40163 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
40164 ){
40165 rc = SQLITE_OK0;
40166 }else{
40167 rc = switchLockProxyPath(pFile, proxyPath);
40168 }
40169 }else{
40170 /* turn on proxy file locking */
40171 rc = proxyTransformUnixFile(pFile, proxyPath);
40172 }
40173 }
40174 return rc;
40175 }
40176 default: {
40177 assert( 0 )((void) (0)); /* The call assures that only valid opcodes are sent */
40178 }
40179 }
40180 /*NOTREACHED*/
40181 return SQLITE_ERROR1;
40182}
40183
40184/*
40185** Within this division (the proxying locking implementation) the procedures
40186** above this point are all utilities. The lock-related methods of the
40187** proxy-locking sqlite3_io_method object follow.
40188*/
40189
40190
40191/*
40192** This routine checks if there is a RESERVED lock held on the specified
40193** file by this or any other process. If such a lock is held, set *pResOut
40194** to a non-zero value otherwise *pResOut is set to zero. The return value
40195** is set to SQLITE_OK unless an I/O error occurs during lock checking.
40196*/
40197static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
40198 unixFile *pFile = (unixFile*)id;
40199 int rc = proxyTakeConch(pFile);
40200 if( rc==SQLITE_OK0 ){
40201 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40202 if( pCtx->conchHeld>0 ){
40203 unixFile *proxy = pCtx->lockProxy;
40204 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
40205 }else{ /* conchHeld < 0 is lockless */
40206 pResOut=0;
40207 }
40208 }
40209 return rc;
40210}
40211
40212/*
40213** Lock the file with the lock specified by parameter eFileLock - one
40214** of the following:
40215**
40216** (1) SHARED_LOCK
40217** (2) RESERVED_LOCK
40218** (3) PENDING_LOCK
40219** (4) EXCLUSIVE_LOCK
40220**
40221** Sometimes when requesting one lock state, additional lock states
40222** are inserted in between. The locking might fail on one of the later
40223** transitions leaving the lock state different from what it started but
40224** still short of its goal. The following chart shows the allowed
40225** transitions and the inserted intermediate states:
40226**
40227** UNLOCKED -> SHARED
40228** SHARED -> RESERVED
40229** SHARED -> (PENDING) -> EXCLUSIVE
40230** RESERVED -> (PENDING) -> EXCLUSIVE
40231** PENDING -> EXCLUSIVE
40232**
40233** This routine will only increase a lock. Use the sqlite3OsUnlock()
40234** routine to lower a locking level.
40235*/
40236static int proxyLock(sqlite3_file *id, int eFileLock) {
40237 unixFile *pFile = (unixFile*)id;
40238 int rc = proxyTakeConch(pFile);
40239 if( rc==SQLITE_OK0 ){
40240 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40241 if( pCtx->conchHeld>0 ){
40242 unixFile *proxy = pCtx->lockProxy;
40243 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
40244 pFile->eFileLock = proxy->eFileLock;
40245 }else{
40246 /* conchHeld < 0 is lockless */
40247 }
40248 }
40249 return rc;
40250}
40251
40252
40253/*
40254** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
40255** must be either NO_LOCK or SHARED_LOCK.
40256**
40257** If the locking level of the file descriptor is already at or below
40258** the requested locking level, this routine is a no-op.
40259*/
40260static int proxyUnlock(sqlite3_file *id, int eFileLock) {
40261 unixFile *pFile = (unixFile*)id;
40262 int rc = proxyTakeConch(pFile);
40263 if( rc==SQLITE_OK0 ){
40264 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40265 if( pCtx->conchHeld>0 ){
40266 unixFile *proxy = pCtx->lockProxy;
40267 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
40268 pFile->eFileLock = proxy->eFileLock;
40269 }else{
40270 /* conchHeld < 0 is lockless */
40271 }
40272 }
40273 return rc;
40274}
40275
40276/*
40277** Close a file that uses proxy locks.
40278*/
40279static int proxyClose(sqlite3_file *id) {
40280 if( ALWAYS(id)(id) ){
40281 unixFile *pFile = (unixFile*)id;
40282 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
40283 unixFile *lockProxy = pCtx->lockProxy;
40284 unixFile *conchFile = pCtx->conchFile;
40285 int rc = SQLITE_OK0;
40286
40287 if( lockProxy ){
40288 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK0);
40289 if( rc ) return rc;
40290 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
40291 if( rc ) return rc;
40292 sqlite3_free(lockProxy);
40293 pCtx->lockProxy = 0;
40294 }
40295 if( conchFile ){
40296 if( pCtx->conchHeld ){
40297 rc = proxyReleaseConch(pFile);
40298 if( rc ) return rc;
40299 }
40300 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
40301 if( rc ) return rc;
40302 sqlite3_free(conchFile);
40303 }
40304 sqlite3DbFree(0, pCtx->lockProxyPath);
40305 sqlite3_free(pCtx->conchFilePath);
40306 sqlite3DbFree(0, pCtx->dbPath);
40307 /* restore the original locking context and pMethod then close it */
40308 pFile->lockingContext = pCtx->oldLockingContext;
40309 pFile->pMethod = pCtx->pOldMethod;
40310 sqlite3_free(pCtx);
40311 return pFile->pMethod->xClose(id);
40312 }
40313 return SQLITE_OK0;
40314}
40315
40316
40317
40318#endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
40319/*
40320** The proxy locking style is intended for use with AFP filesystems.
40321** And since AFP is only supported on MacOSX, the proxy locking is also
40322** restricted to MacOSX.
40323**
40324**
40325******************* End of the proxy lock implementation **********************
40326******************************************************************************/
40327
40328/*
40329** Initialize the operating system interface.
40330**
40331** This routine registers all VFS implementations for unix-like operating
40332** systems. This routine, and the sqlite3_os_end() routine that follows,
40333** should be the only routines in this file that are visible from other
40334** files.
40335**
40336** This routine is called once during SQLite initialization and by a
40337** single thread. The memory allocation and mutex subsystems have not
40338** necessarily been initialized when this routine is called, and so they
40339** should not be used.
40340*/
40341SQLITE_API int sqlite3_os_init(void){
40342 /*
40343 ** The following macro defines an initializer for an sqlite3_vfs object.
40344 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
40345 ** to the "finder" function. (pAppData is a pointer to a pointer because
40346 ** silly C90 rules prohibit a void* from being cast to a function pointer
40347 ** and so we have to go through the intermediate pointer to avoid problems
40348 ** when compiling with -pedantic-errors on GCC.)
40349 **
40350 ** The FINDER parameter to this macro is the name of the pointer to the
40351 ** finder-function. The finder-function returns a pointer to the
40352 ** sqlite_io_methods object that implements the desired locking
40353 ** behaviors. See the division above that contains the IOMETHODS
40354 ** macro for addition information on finder-functions.
40355 **
40356 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
40357 ** object. But the "autolockIoFinder" available on MacOSX does a little
40358 ** more than that; it looks at the filesystem type that hosts the
40359 ** database file and tries to choose an locking method appropriate for
40360 ** that filesystem time.
40361 */
40362 #define UNIXVFS(VFSNAME, FINDER){ 3, sizeof(unixFile), 512, 0, VFSNAME, (void*)&FINDER, unixOpen
, unixDelete, unixAccess, unixFullPathname, unixDlOpen, unixDlError
, unixDlSym, unixDlClose, unixRandomness, unixSleep, unixCurrentTime
, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall, unixGetSystemCall
, unixNextSystemCall, }
{ \
40363 3, /* iVersion */ \
40364 sizeof(unixFile), /* szOsFile */ \
40365 MAX_PATHNAME512, /* mxPathname */ \
40366 0, /* pNext */ \
40367 VFSNAME, /* zName */ \
40368 (void*)&FINDER, /* pAppData */ \
40369 unixOpen, /* xOpen */ \
40370 unixDelete, /* xDelete */ \
40371 unixAccess, /* xAccess */ \
40372 unixFullPathname, /* xFullPathname */ \
40373 unixDlOpen, /* xDlOpen */ \
40374 unixDlError, /* xDlError */ \
40375 unixDlSym, /* xDlSym */ \
40376 unixDlClose, /* xDlClose */ \
40377 unixRandomness, /* xRandomness */ \
40378 unixSleep, /* xSleep */ \
40379 unixCurrentTime, /* xCurrentTime */ \
40380 unixGetLastError, /* xGetLastError */ \
40381 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
40382 unixSetSystemCall, /* xSetSystemCall */ \
40383 unixGetSystemCall, /* xGetSystemCall */ \
40384 unixNextSystemCall, /* xNextSystemCall */ \
40385 }
40386
40387 /*
40388 ** All default VFSes for unix are contained in the following array.
40389 **
40390 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
40391 ** by the SQLite core when the VFS is registered. So the following
40392 ** array cannot be const.
40393 */
40394 static sqlite3_vfs aVfs[] = {
40395#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
40396 UNIXVFS("unix", autolockIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix", (void*)&autolockIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40397#elif OS_VXWORKS0
40398 UNIXVFS("unix", vxworksIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix", (void*)&vxworksIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40399#else
40400 UNIXVFS("unix", posixIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix", (void*)&posixIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40401#endif
40402 UNIXVFS("unix-none", nolockIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-none", (void*)&nolockIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40403 UNIXVFS("unix-dotfile", dotlockIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-dotfile", (void*)&dotlockIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40404 UNIXVFS("unix-excl", posixIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-excl", (void*)&posixIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40405#if OS_VXWORKS0
40406 UNIXVFS("unix-namedsem", semIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-namedsem", (void*)&semIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40407#endif
40408#if SQLITE_ENABLE_LOCKING_STYLE0 || OS_VXWORKS0
40409 UNIXVFS("unix-posix", posixIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-posix", (void*)&posixIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40410#endif
40411#if SQLITE_ENABLE_LOCKING_STYLE0
40412 UNIXVFS("unix-flock", flockIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-flock", (void*)&flockIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40413#endif
40414#if SQLITE_ENABLE_LOCKING_STYLE0 && defined(__APPLE__)
40415 UNIXVFS("unix-afp", afpIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-afp", (void*)&afpIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40416 UNIXVFS("unix-nfs", nfsIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-nfs", (void*)&nfsIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40417 UNIXVFS("unix-proxy", proxyIoFinder ){ 3, sizeof(unixFile), 512, 0, "unix-proxy", (void*)&proxyIoFinder
, unixOpen, unixDelete, unixAccess, unixFullPathname, unixDlOpen
, unixDlError, unixDlSym, unixDlClose, unixRandomness, unixSleep
, unixCurrentTime, unixGetLastError, unixCurrentTimeInt64, unixSetSystemCall
, unixGetSystemCall, unixNextSystemCall, }
,
40418#endif
40419 };
40420 unsigned int i; /* Loop counter */
40421
40422 /* Double-check that the aSyscall[] array has been constructed
40423 ** correctly. See ticket [bb3a86e890c8e96ab] */
40424 assert( ArraySize(aSyscall)==29 )((void) (0));
40425
40426 /* Register all VFSes defined in the aVfs[] array */
40427 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
40428 sqlite3_vfs_register(&aVfs[i], i==0);
40429 }
40430 unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
40431 return SQLITE_OK0;
40432}
40433
40434/*
40435** Shutdown the operating system interface.
40436**
40437** Some operating systems might need to do some cleanup in this routine,
40438** to release dynamically allocated objects. But not on unix.
40439** This routine is a no-op for unix.
40440*/
40441SQLITE_API int sqlite3_os_end(void){
40442 unixBigLock = 0;
40443 return SQLITE_OK0;
40444}
40445
40446#endif /* SQLITE_OS_UNIX */
40447
40448/************** End of os_unix.c *********************************************/
40449/************** Begin file os_win.c ******************************************/
40450/*
40451** 2004 May 22
40452**
40453** The author disclaims copyright to this source code. In place of
40454** a legal notice, here is a blessing:
40455**
40456** May you do good and not evil.
40457** May you find forgiveness for yourself and forgive others.
40458** May you share freely, never taking more than you give.
40459**
40460******************************************************************************
40461**
40462** This file contains code that is specific to Windows.
40463*/
40464/* #include "sqliteInt.h" */
40465#if SQLITE_OS_WIN0 /* This file is used for Windows only */
40466
40467/*
40468** Include code that is common to all os_*.c files
40469*/
40470/************** Include os_common.h in the middle of os_win.c ****************/
40471/************** Begin file os_common.h ***************************************/
40472/*
40473** 2004 May 22
40474**
40475** The author disclaims copyright to this source code. In place of
40476** a legal notice, here is a blessing:
40477**
40478** May you do good and not evil.
40479** May you find forgiveness for yourself and forgive others.
40480** May you share freely, never taking more than you give.
40481**
40482******************************************************************************
40483**
40484** This file contains macros and a little bit of code that is common to
40485** all of the platform-specific files (os_*.c) and is #included into those
40486** files.
40487**
40488** This file should be #included by the os_*.c files only. It is not a
40489** general purpose header file.
40490*/
40491#ifndef _OS_COMMON_H_
40492#define _OS_COMMON_H_
40493
40494/*
40495** At least two bugs have slipped in because we changed the MEMORY_DEBUG
40496** macro to SQLITE_DEBUG and some older makefiles have not yet made the
40497** switch. The following code should catch this problem at compile-time.
40498*/
40499#ifdef MEMORY_DEBUG
40500# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
40501#endif
40502
40503/*
40504** Macros for performance tracing. Normally turned off. Only works
40505** on i486 hardware.
40506*/
40507#ifdef SQLITE_PERFORMANCE_TRACE
40508
40509/*
40510** hwtime.h contains inline assembler code for implementing
40511** high-performance timing routines.
40512*/
40513/************** Include hwtime.h in the middle of os_common.h ****************/
40514/************** Begin file hwtime.h ******************************************/
40515/*
40516** 2008 May 27
40517**
40518** The author disclaims copyright to this source code. In place of
40519** a legal notice, here is a blessing:
40520**
40521** May you do good and not evil.
40522** May you find forgiveness for yourself and forgive others.
40523** May you share freely, never taking more than you give.
40524**
40525******************************************************************************
40526**
40527** This file contains inline asm code for retrieving "high-performance"
40528** counters for x86 class CPUs.
40529*/
40530#ifndef SQLITE_HWTIME_H
40531#define SQLITE_HWTIME_H
40532
40533/*
40534** The following routine only works on pentium-class (or newer) processors.
40535** It uses the RDTSC opcode to read the cycle count value out of the
40536** processor and returns that value. This can be used for high-res
40537** profiling.
40538*/
40539#if (defined(__GNUC__4) || defined(_MSC_VER)) && \
40540 (defined(i386) || defined(__i386__) || defined(_M_IX86))
40541
40542 #if defined(__GNUC__4)
40543
40544 __inline__ sqlite_uint64 sqlite3Hwtime(void){
40545 unsigned int lo, hi;
40546 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
40547 return (sqlite_uint64)hi << 32 | lo;
40548 }
40549
40550 #elif defined(_MSC_VER)
40551
40552 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
40553 __asm {
40554 rdtsc
40555 ret ; return value at EDX:EAX
40556 }
40557 }
40558
40559 #endif
40560
40561#elif (defined(__GNUC__4) && defined(__x86_64__1))
40562
40563 __inline__ sqlite_uint64 sqlite3Hwtime(void){
40564 unsigned long val;
40565 __asm__ __volatile__ ("rdtsc" : "=A" (val));
40566 return val;
40567 }
40568
40569#elif (defined(__GNUC__4) && defined(__ppc__))
40570
40571 __inline__ sqlite_uint64 sqlite3Hwtime(void){
40572 unsigned long long retval;
40573 unsigned long junk;
40574 __asm__ __volatile__ ("\n\
40575 1: mftbu %1\n\
40576 mftb %L0\n\
40577 mftbu %0\n\
40578 cmpw %0,%1\n\
40579 bne 1b"
40580 : "=r" (retval), "=r" (junk));
40581 return retval;
40582 }
40583
40584#else
40585
40586 #error Need implementation of sqlite3Hwtime() for your platform.
40587
40588 /*
40589 ** To compile without implementing sqlite3Hwtime() for your platform,
40590 ** you can remove the above #error and use the following
40591 ** stub function. You will lose timing support for many
40592 ** of the debugging and testing utilities, but it should at
40593 ** least compile and run.
40594 */
40595SQLITE_PRIVATEstatic sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
40596
40597#endif
40598
40599#endif /* !defined(SQLITE_HWTIME_H) */
40600
40601/************** End of hwtime.h **********************************************/
40602/************** Continuing where we left off in os_common.h ******************/
40603
40604static sqlite_uint64 g_start;
40605static sqlite_uint64 g_elapsed;
40606#define TIMER_START g_start=sqlite3Hwtime()
40607#define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
40608#define TIMER_ELAPSED((sqlite_uint64)0) g_elapsed
40609#else
40610#define TIMER_START
40611#define TIMER_END
40612#define TIMER_ELAPSED((sqlite_uint64)0) ((sqlite_uint64)0)
40613#endif
40614
40615/*
40616** If we compile with the SQLITE_TEST macro set, then the following block
40617** of code will give us the ability to simulate a disk I/O error. This
40618** is used for testing the I/O recovery logic.
40619*/
40620#if defined(SQLITE_TEST)
40621SQLITE_API extern int sqlite3_io_error_hit;
40622SQLITE_API extern int sqlite3_io_error_hardhit;
40623SQLITE_API extern int sqlite3_io_error_pending;
40624SQLITE_API extern int sqlite3_io_error_persist;
40625SQLITE_API extern int sqlite3_io_error_benign;
40626SQLITE_API extern int sqlite3_diskfull_pending;
40627SQLITE_API extern int sqlite3_diskfull;
40628#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
40629#define SimulateIOError(CODE) \
40630 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
40631 || sqlite3_io_error_pending-- == 1 ) \
40632 { local_ioerr(); CODE; }
40633static void local_ioerr(){
40634 IOTRACE(("IOERR\n"));
40635 sqlite3_io_error_hit++;
40636 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
40637}
40638#define SimulateDiskfullError(CODE) \
40639 if( sqlite3_diskfull_pending ){ \
40640 if( sqlite3_diskfull_pending == 1 ){ \
40641 local_ioerr(); \
40642 sqlite3_diskfull = 1; \
40643 sqlite3_io_error_hit = 1; \
40644 CODE; \
40645 }else{ \
40646 sqlite3_diskfull_pending--; \
40647 } \
40648 }
40649#else
40650#define SimulateIOErrorBenign(X)
40651#define SimulateIOError(A)
40652#define SimulateDiskfullError(A)
40653#endif /* defined(SQLITE_TEST) */
40654
40655/*
40656** When testing, keep a count of the number of open files.
40657*/
40658#if defined(SQLITE_TEST)
40659SQLITE_API extern int sqlite3_open_file_count;
40660#define OpenCounter(X) sqlite3_open_file_count+=(X)
40661#else
40662#define OpenCounter(X)
40663#endif /* defined(SQLITE_TEST) */
40664
40665#endif /* !defined(_OS_COMMON_H_) */
40666
40667/************** End of os_common.h *******************************************/
40668/************** Continuing where we left off in os_win.c *********************/
40669
40670/*
40671** Include the header file for the Windows VFS.
40672*/
40673/* #include "os_win.h" */
40674
40675/*
40676** Compiling and using WAL mode requires several APIs that are only
40677** available in Windows platforms based on the NT kernel.
40678*/
40679#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
40680# error "WAL mode requires support from the Windows NT kernel, compile\
40681 with SQLITE_OMIT_WAL."
40682#endif
40683
40684#if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE0x7fff0000>0
40685# error "Memory mapped files require support from the Windows NT kernel,\
40686 compile with SQLITE_MAX_MMAP_SIZE=0."
40687#endif
40688
40689/*
40690** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
40691** based on the sub-platform)?
40692*/
40693#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
40694# define SQLITE_WIN32_HAS_ANSI
40695#endif
40696
40697/*
40698** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
40699** based on the sub-platform)?
40700*/
40701#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
40702 !defined(SQLITE_WIN32_NO_WIDE)
40703# define SQLITE_WIN32_HAS_WIDE
40704#endif
40705
40706/*
40707** Make sure at least one set of Win32 APIs is available.
40708*/
40709#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
40710# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
40711 must be defined."
40712#endif
40713
40714/*
40715** Define the required Windows SDK version constants if they are not
40716** already available.
40717*/
40718#ifndef NTDDI_WIN8
40719# define NTDDI_WIN8 0x06020000
40720#endif
40721
40722#ifndef NTDDI_WINBLUE
40723# define NTDDI_WINBLUE 0x06030000
40724#endif
40725
40726#ifndef NTDDI_WINTHRESHOLD
40727# define NTDDI_WINTHRESHOLD 0x06040000
40728#endif
40729
40730/*
40731** Check to see if the GetVersionEx[AW] functions are deprecated on the
40732** target system. GetVersionEx was first deprecated in Win8.1.
40733*/
40734#ifndef SQLITE_WIN32_GETVERSIONEX
40735# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
40736# define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
40737# else
40738# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
40739# endif
40740#endif
40741
40742/*
40743** Check to see if the CreateFileMappingA function is supported on the
40744** target system. It is unavailable when using "mincore.lib" on Win10.
40745** When compiling for Windows 10, always assume "mincore.lib" is in use.
40746*/
40747#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
40748# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
40749# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
40750# else
40751# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
40752# endif
40753#endif
40754
40755/*
40756** This constant should already be defined (in the "WinDef.h" SDK file).
40757*/
40758#ifndef MAX_PATH
40759# define MAX_PATH (260)
40760#endif
40761
40762/*
40763** Maximum pathname length (in chars) for Win32. This should normally be
40764** MAX_PATH.
40765*/
40766#ifndef SQLITE_WIN32_MAX_PATH_CHARS
40767# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
40768#endif
40769
40770/*
40771** This constant should already be defined (in the "WinNT.h" SDK file).
40772*/
40773#ifndef UNICODE_STRING_MAX_CHARS
40774# define UNICODE_STRING_MAX_CHARS (32767)
40775#endif
40776
40777/*
40778** Maximum pathname length (in chars) for WinNT. This should normally be
40779** UNICODE_STRING_MAX_CHARS.
40780*/
40781#ifndef SQLITE_WINNT_MAX_PATH_CHARS
40782# define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
40783#endif
40784
40785/*
40786** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
40787** characters, so we allocate 4 bytes per character assuming worst-case of
40788** 4-bytes-per-character for UTF8.
40789*/
40790#ifndef SQLITE_WIN32_MAX_PATH_BYTES
40791# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
40792#endif
40793
40794/*
40795** Maximum pathname length (in bytes) for WinNT. This should normally be
40796** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
40797*/
40798#ifndef SQLITE_WINNT_MAX_PATH_BYTES
40799# define SQLITE_WINNT_MAX_PATH_BYTES \
40800 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
40801#endif
40802
40803/*
40804** Maximum error message length (in chars) for WinRT.
40805*/
40806#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
40807# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
40808#endif
40809
40810/*
40811** Returns non-zero if the character should be treated as a directory
40812** separator.
40813*/
40814#ifndef winIsDirSep
40815# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
40816#endif
40817
40818/*
40819** This macro is used when a local variable is set to a value that is
40820** [sometimes] not used by the code (e.g. via conditional compilation).
40821*/
40822#ifndef UNUSED_VARIABLE_VALUE
40823# define UNUSED_VARIABLE_VALUE(x) (void)(x)
40824#endif
40825
40826/*
40827** Returns the character that should be used as the directory separator.
40828*/
40829#ifndef winGetDirSep
40830# define winGetDirSep() '\\'
40831#endif
40832
40833/*
40834** Do we need to manually define the Win32 file mapping APIs for use with WAL
40835** mode or memory mapped files (e.g. these APIs are available in the Windows
40836** CE SDK; however, they are not present in the header file)?
40837*/
40838#if SQLITE_WIN32_FILEMAPPING_API && \
40839 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0)
40840/*
40841** Two of the file mapping APIs are different under WinRT. Figure out which
40842** set we need.
40843*/
40844#if SQLITE_OS_WINRT
40845WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
40846 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
40847
40848WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
40849#else
40850#if defined(SQLITE_WIN32_HAS_ANSI)
40851WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
40852 DWORD, DWORD, DWORD, LPCSTR);
40853#endif /* defined(SQLITE_WIN32_HAS_ANSI) */
40854
40855#if defined(SQLITE_WIN32_HAS_WIDE)
40856WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
40857 DWORD, DWORD, DWORD, LPCWSTR);
40858#endif /* defined(SQLITE_WIN32_HAS_WIDE) */
40859
40860WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
40861#endif /* SQLITE_OS_WINRT */
40862
40863/*
40864** These file mapping APIs are common to both Win32 and WinRT.
40865*/
40866
40867WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
40868WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
40869#endif /* SQLITE_WIN32_FILEMAPPING_API */
40870
40871/*
40872** Some Microsoft compilers lack this definition.
40873*/
40874#ifndef INVALID_FILE_ATTRIBUTES
40875# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
40876#endif
40877
40878#ifndef FILE_FLAG_MASK
40879# define FILE_FLAG_MASK (0xFF3C0000)
40880#endif
40881
40882#ifndef FILE_ATTRIBUTE_MASK
40883# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
40884#endif
40885
40886#ifndef SQLITE_OMIT_WAL
40887/* Forward references to structures used for WAL */
40888typedef struct winShm winShm; /* A connection to shared-memory */
40889typedef struct winShmNode winShmNode; /* A region of shared-memory */
40890#endif
40891
40892/*
40893** WinCE lacks native support for file locking so we have to fake it
40894** with some code of our own.
40895*/
40896#if SQLITE_OS_WINCE
40897typedef struct winceLock {
40898 int nReaders; /* Number of reader locks obtained */
40899 BOOL bPending; /* Indicates a pending lock has been obtained */
40900 BOOL bReserved; /* Indicates a reserved lock has been obtained */
40901 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
40902} winceLock;
40903#endif
40904
40905/*
40906** The winFile structure is a subclass of sqlite3_file* specific to the win32
40907** portability layer.
40908*/
40909typedef struct winFile winFile;
40910struct winFile {
40911 const sqlite3_io_methods *pMethod; /*** Must be first ***/
40912 sqlite3_vfs *pVfs; /* The VFS used to open this file */
40913 HANDLE h; /* Handle for accessing the file */
40914 u8 locktype; /* Type of lock currently held on this file */
40915 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
40916 u8 ctrlFlags; /* Flags. See WINFILE_* below */
40917 DWORD lastErrno; /* The Windows errno from the last I/O error */
40918#ifndef SQLITE_OMIT_WAL
40919 winShm *pShm; /* Instance of shared memory on this file */
40920#endif
40921 const char *zPath; /* Full pathname of this file */
40922 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
40923#if SQLITE_OS_WINCE
40924 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
40925 HANDLE hMutex; /* Mutex used to control access to shared lock */
40926 HANDLE hShared; /* Shared memory segment used for locking */
40927 winceLock local; /* Locks obtained by this instance of winFile */
40928 winceLock *shared; /* Global shared lock memory for the file */
40929#endif
40930#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
40931 int nFetchOut; /* Number of outstanding xFetch references */
40932 HANDLE hMap; /* Handle for accessing memory mapping */
40933 void *pMapRegion; /* Area memory mapped */
40934 sqlite3_int64 mmapSize; /* Size of mapped region */
40935 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
40936#endif
40937};
40938
40939/*
40940** The winVfsAppData structure is used for the pAppData member for all of the
40941** Win32 VFS variants.
40942*/
40943typedef struct winVfsAppData winVfsAppData;
40944struct winVfsAppData {
40945 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
40946 void *pAppData; /* The extra pAppData, if any. */
40947 BOOL bNoLock; /* Non-zero if locking is disabled. */
40948};
40949
40950/*
40951** Allowed values for winFile.ctrlFlags
40952*/
40953#define WINFILE_RDONLY 0x02 /* Connection is read only */
40954#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
40955#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
40956
40957/*
40958 * The size of the buffer used by sqlite3_win32_write_debug().
40959 */
40960#ifndef SQLITE_WIN32_DBG_BUF_SIZE
40961# define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
40962#endif
40963
40964/*
40965 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
40966 * various Win32 API heap functions instead of our own.
40967 */
40968#ifdef SQLITE_WIN32_MALLOC
40969
40970/*
40971 * If this is non-zero, an isolated heap will be created by the native Win32
40972 * allocator subsystem; otherwise, the default process heap will be used. This
40973 * setting has no effect when compiling for WinRT. By default, this is enabled
40974 * and an isolated heap will be created to store all allocated data.
40975 *
40976 ******************************************************************************
40977 * WARNING: It is important to note that when this setting is non-zero and the
40978 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
40979 * function), all data that was allocated using the isolated heap will
40980 * be freed immediately and any attempt to access any of that freed
40981 * data will almost certainly result in an immediate access violation.
40982 ******************************************************************************
40983 */
40984#ifndef SQLITE_WIN32_HEAP_CREATE
40985# define SQLITE_WIN32_HEAP_CREATE (TRUE)
40986#endif
40987
40988/*
40989 * This is the maximum possible initial size of the Win32-specific heap, in
40990 * bytes.
40991 */
40992#ifndef SQLITE_WIN32_HEAP_MAX_INIT_SIZE
40993# define SQLITE_WIN32_HEAP_MAX_INIT_SIZE (4294967295U)
40994#endif
40995
40996/*
40997 * This is the extra space for the initial size of the Win32-specific heap,
40998 * in bytes. This value may be zero.
40999 */
41000#ifndef SQLITE_WIN32_HEAP_INIT_EXTRA
41001# define SQLITE_WIN32_HEAP_INIT_EXTRA (4194304)
41002#endif
41003
41004/*
41005 * Calculate the maximum legal cache size, in pages, based on the maximum
41006 * possible initial heap size and the default page size, setting aside the
41007 * needed extra space.
41008 */
41009#ifndef SQLITE_WIN32_MAX_CACHE_SIZE
41010# define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
41011 (SQLITE_WIN32_HEAP_INIT_EXTRA)) / \
41012 (SQLITE_DEFAULT_PAGE_SIZE4096))
41013#endif
41014
41015/*
41016 * This is cache size used in the calculation of the initial size of the
41017 * Win32-specific heap. It cannot be negative.
41018 */
41019#ifndef SQLITE_WIN32_CACHE_SIZE
41020# if SQLITE_DEFAULT_CACHE_SIZE-2000>=0
41021# define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE-2000)
41022# else
41023# define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE-2000))
41024# endif
41025#endif
41026
41027/*
41028 * Make sure that the calculated cache size, in pages, cannot cause the
41029 * initial size of the Win32-specific heap to exceed the maximum amount
41030 * of memory that can be specified in the call to HeapCreate.
41031 */
41032#if SQLITE_WIN32_CACHE_SIZE>SQLITE_WIN32_MAX_CACHE_SIZE
41033# undef SQLITE_WIN32_CACHE_SIZE
41034# define SQLITE_WIN32_CACHE_SIZE (2000)
41035#endif
41036
41037/*
41038 * The initial size of the Win32-specific heap. This value may be zero.
41039 */
41040#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
41041# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
41042 (SQLITE_DEFAULT_PAGE_SIZE4096) + \
41043 (SQLITE_WIN32_HEAP_INIT_EXTRA))
41044#endif
41045
41046/*
41047 * The maximum size of the Win32-specific heap. This value may be zero.
41048 */
41049#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
41050# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
41051#endif
41052
41053/*
41054 * The extra flags to use in calls to the Win32 heap APIs. This value may be
41055 * zero for the default behavior.
41056 */
41057#ifndef SQLITE_WIN32_HEAP_FLAGS
41058# define SQLITE_WIN32_HEAP_FLAGS (0)
41059#endif
41060
41061
41062/*
41063** The winMemData structure stores information required by the Win32-specific
41064** sqlite3_mem_methods implementation.
41065*/
41066typedef struct winMemData winMemData;
41067struct winMemData {
41068#ifndef NDEBUG1
41069 u32 magic1; /* Magic number to detect structure corruption. */
41070#endif
41071 HANDLE hHeap; /* The handle to our heap. */
41072 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
41073#ifndef NDEBUG1
41074 u32 magic2; /* Magic number to detect structure corruption. */
41075#endif
41076};
41077
41078#ifndef NDEBUG1
41079#define WINMEM_MAGIC1 0x42b2830b
41080#define WINMEM_MAGIC2 0xbd4d7cf4
41081#endif
41082
41083static struct winMemData win_mem_data = {
41084#ifndef NDEBUG1
41085 WINMEM_MAGIC1,
41086#endif
41087 NULL((void*)0), FALSE
41088#ifndef NDEBUG1
41089 ,WINMEM_MAGIC2
41090#endif
41091};
41092
41093#ifndef NDEBUG1
41094#define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )((void) (0))
41095#define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )((void) (0))
41096#define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
41097#else
41098#define winMemAssertMagic()
41099#endif
41100
41101#define winMemGetDataPtr() &win_mem_data
41102#define winMemGetHeap() win_mem_data.hHeap
41103#define winMemGetOwned() win_mem_data.bOwned
41104
41105static void *winMemMalloc(int nBytes);
41106static void winMemFree(void *pPrior);
41107static void *winMemRealloc(void *pPrior, int nBytes);
41108static int winMemSize(void *p);
41109static int winMemRoundup(int n);
41110static int winMemInit(void *pAppData);
41111static void winMemShutdown(void *pAppData);
41112
41113SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetWin32(void);
41114#endif /* SQLITE_WIN32_MALLOC */
41115
41116/*
41117** The following variable is (normally) set once and never changes
41118** thereafter. It records whether the operating system is Win9x
41119** or WinNT.
41120**
41121** 0: Operating system unknown.
41122** 1: Operating system is Win9x.
41123** 2: Operating system is WinNT.
41124**
41125** In order to facilitate testing on a WinNT system, the test fixture
41126** can manually set this value to 1 to emulate Win98 behavior.
41127*/
41128#ifdef SQLITE_TEST
41129SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
41130#else
41131static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
41132#endif
41133
41134#ifndef SYSCALL
41135# define SYSCALL sqlite3_syscall_ptr
41136#endif
41137
41138/*
41139** This function is not available on Windows CE or WinRT.
41140 */
41141
41142#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
41143# define osAreFileApisANSI() 1
41144#endif
41145
41146/*
41147** Many system calls are accessed through pointer-to-functions so that
41148** they may be overridden at runtime to facilitate fault injection during
41149** testing and sandboxing. The following array holds the names and pointers
41150** to all overrideable system calls.
41151*/
41152static struct win_syscall {
41153 const char *zName; /* Name of the system call */
41154 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
41155 sqlite3_syscall_ptr pDefault; /* Default value */
41156} aSyscall[] = {
41157#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41158 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
41159#else
41160 { "AreFileApisANSI", (SYSCALL)0, 0 },
41161#endif
41162
41163#ifndef osAreFileApisANSI
41164#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
41165#endif
41166
41167#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
41168 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
41169#else
41170 { "CharLowerW", (SYSCALL)0, 0 },
41171#endif
41172
41173#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
41174
41175#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
41176 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
41177#else
41178 { "CharUpperW", (SYSCALL)0, 0 },
41179#endif
41180
41181#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
41182
41183 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
41184
41185#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
41186
41187#if defined(SQLITE_WIN32_HAS_ANSI)
41188 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
41189#else
41190 { "CreateFileA", (SYSCALL)0, 0 },
41191#endif
41192
41193#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
41194 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
41195
41196#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41197 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
41198#else
41199 { "CreateFileW", (SYSCALL)0, 0 },
41200#endif
41201
41202#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
41203 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
41204
41205#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
41206 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0) && \
41207 SQLITE_WIN32_CREATEFILEMAPPINGA
41208 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
41209#else
41210 { "CreateFileMappingA", (SYSCALL)0, 0 },
41211#endif
41212
41213#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
41214 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
41215
41216#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
41217 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0))
41218 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
41219#else
41220 { "CreateFileMappingW", (SYSCALL)0, 0 },
41221#endif
41222
41223#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
41224 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
41225
41226#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41227 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
41228#else
41229 { "CreateMutexW", (SYSCALL)0, 0 },
41230#endif
41231
41232#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
41233 LPCWSTR))aSyscall[8].pCurrent)
41234
41235#if defined(SQLITE_WIN32_HAS_ANSI)
41236 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
41237#else
41238 { "DeleteFileA", (SYSCALL)0, 0 },
41239#endif
41240
41241#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
41242
41243#if defined(SQLITE_WIN32_HAS_WIDE)
41244 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
41245#else
41246 { "DeleteFileW", (SYSCALL)0, 0 },
41247#endif
41248
41249#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
41250
41251#if SQLITE_OS_WINCE
41252 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
41253#else
41254 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
41255#endif
41256
41257#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
41258 LPFILETIME))aSyscall[11].pCurrent)
41259
41260#if SQLITE_OS_WINCE
41261 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
41262#else
41263 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
41264#endif
41265
41266#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
41267 LPSYSTEMTIME))aSyscall[12].pCurrent)
41268
41269 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
41270
41271#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
41272
41273#if defined(SQLITE_WIN32_HAS_ANSI)
41274 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
41275#else
41276 { "FormatMessageA", (SYSCALL)0, 0 },
41277#endif
41278
41279#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
41280 DWORD,va_list*))aSyscall[14].pCurrent)
41281
41282#if defined(SQLITE_WIN32_HAS_WIDE)
41283 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
41284#else
41285 { "FormatMessageW", (SYSCALL)0, 0 },
41286#endif
41287
41288#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
41289 DWORD,va_list*))aSyscall[15].pCurrent)
41290
41291#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
41292 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
41293#else
41294 { "FreeLibrary", (SYSCALL)0, 0 },
41295#endif
41296
41297#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
41298
41299 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
41300
41301#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
41302
41303#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
41304 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
41305#else
41306 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
41307#endif
41308
41309#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
41310 LPDWORD))aSyscall[18].pCurrent)
41311
41312#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41313 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
41314#else
41315 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
41316#endif
41317
41318#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
41319 LPDWORD))aSyscall[19].pCurrent)
41320
41321#if defined(SQLITE_WIN32_HAS_ANSI)
41322 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
41323#else
41324 { "GetFileAttributesA", (SYSCALL)0, 0 },
41325#endif
41326
41327#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
41328
41329#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41330 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
41331#else
41332 { "GetFileAttributesW", (SYSCALL)0, 0 },
41333#endif
41334
41335#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
41336
41337#if defined(SQLITE_WIN32_HAS_WIDE)
41338 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
41339#else
41340 { "GetFileAttributesExW", (SYSCALL)0, 0 },
41341#endif
41342
41343#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
41344 LPVOID))aSyscall[22].pCurrent)
41345
41346#if !SQLITE_OS_WINRT
41347 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
41348#else
41349 { "GetFileSize", (SYSCALL)0, 0 },
41350#endif
41351
41352#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
41353
41354#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
41355 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
41356#else
41357 { "GetFullPathNameA", (SYSCALL)0, 0 },
41358#endif
41359
41360#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
41361 LPSTR*))aSyscall[24].pCurrent)
41362
41363#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41364 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
41365#else
41366 { "GetFullPathNameW", (SYSCALL)0, 0 },
41367#endif
41368
41369#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
41370 LPWSTR*))aSyscall[25].pCurrent)
41371
41372 { "GetLastError", (SYSCALL)GetLastError, 0 },
41373
41374#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
41375
41376#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
41377#if SQLITE_OS_WINCE
41378 /* The GetProcAddressA() routine is only available on Windows CE. */
41379 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
41380#else
41381 /* All other Windows platforms expect GetProcAddress() to take
41382 ** an ANSI string regardless of the _UNICODE setting */
41383 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
41384#endif
41385#else
41386 { "GetProcAddressA", (SYSCALL)0, 0 },
41387#endif
41388
41389#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
41390 LPCSTR))aSyscall[27].pCurrent)
41391
41392#if !SQLITE_OS_WINRT
41393 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
41394#else
41395 { "GetSystemInfo", (SYSCALL)0, 0 },
41396#endif
41397
41398#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
41399
41400 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
41401
41402#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
41403
41404#if !SQLITE_OS_WINCE
41405 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
41406#else
41407 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
41408#endif
41409
41410#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
41411 LPFILETIME))aSyscall[30].pCurrent)
41412
41413#if defined(SQLITE_WIN32_HAS_ANSI)
41414 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
41415#else
41416 { "GetTempPathA", (SYSCALL)0, 0 },
41417#endif
41418
41419#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
41420
41421#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
41422 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
41423#else
41424 { "GetTempPathW", (SYSCALL)0, 0 },
41425#endif
41426
41427#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
41428
41429#if !SQLITE_OS_WINRT
41430 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
41431#else
41432 { "GetTickCount", (SYSCALL)0, 0 },
41433#endif
41434
41435#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
41436
41437#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
41438 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
41439#else
41440 { "GetVersionExA", (SYSCALL)0, 0 },
41441#endif
41442
41443#define osGetVersionExA ((BOOL(WINAPI*)( \
41444 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
41445
41446#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
41447 SQLITE_WIN32_GETVERSIONEX
41448 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
41449#else
41450 { "GetVersionExW", (SYSCALL)0, 0 },
41451#endif
41452
41453#define osGetVersionExW ((BOOL(WINAPI*)( \
41454 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
41455
41456 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
41457
41458#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
41459 SIZE_T))aSyscall[36].pCurrent)
41460
41461#if !SQLITE_OS_WINRT
41462 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
41463#else
41464 { "HeapCreate", (SYSCALL)0, 0 },
41465#endif
41466
41467#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
41468 SIZE_T))aSyscall[37].pCurrent)
41469
41470#if !SQLITE_OS_WINRT
41471 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
41472#else
41473 { "HeapDestroy", (SYSCALL)0, 0 },
41474#endif
41475
41476#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
41477
41478 { "HeapFree", (SYSCALL)HeapFree, 0 },
41479
41480#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
41481
41482 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
41483
41484#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
41485 SIZE_T))aSyscall[40].pCurrent)
41486
41487 { "HeapSize", (SYSCALL)HeapSize, 0 },
41488
41489#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
41490 LPCVOID))aSyscall[41].pCurrent)
41491
41492#if !SQLITE_OS_WINRT
41493 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
41494#else
41495 { "HeapValidate", (SYSCALL)0, 0 },
41496#endif
41497
41498#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
41499 LPCVOID))aSyscall[42].pCurrent)
41500
41501#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41502 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
41503#else
41504 { "HeapCompact", (SYSCALL)0, 0 },
41505#endif
41506
41507#define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
41508
41509#if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
41510 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
41511#else
41512 { "LoadLibraryA", (SYSCALL)0, 0 },
41513#endif
41514
41515#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
41516
41517#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
41518 !defined(SQLITE_OMIT_LOAD_EXTENSION)
41519 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
41520#else
41521 { "LoadLibraryW", (SYSCALL)0, 0 },
41522#endif
41523
41524#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
41525
41526#if !SQLITE_OS_WINRT
41527 { "LocalFree", (SYSCALL)LocalFree, 0 },
41528#else
41529 { "LocalFree", (SYSCALL)0, 0 },
41530#endif
41531
41532#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
41533
41534#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41535 { "LockFile", (SYSCALL)LockFile, 0 },
41536#else
41537 { "LockFile", (SYSCALL)0, 0 },
41538#endif
41539
41540#ifndef osLockFile
41541#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41542 DWORD))aSyscall[47].pCurrent)
41543#endif
41544
41545#if !SQLITE_OS_WINCE
41546 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
41547#else
41548 { "LockFileEx", (SYSCALL)0, 0 },
41549#endif
41550
41551#ifndef osLockFileEx
41552#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
41553 LPOVERLAPPED))aSyscall[48].pCurrent)
41554#endif
41555
41556#if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
41557 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0))
41558 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
41559#else
41560 { "MapViewOfFile", (SYSCALL)0, 0 },
41561#endif
41562
41563#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41564 SIZE_T))aSyscall[49].pCurrent)
41565
41566 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
41567
41568#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
41569 int))aSyscall[50].pCurrent)
41570
41571 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
41572
41573#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
41574 LARGE_INTEGER*))aSyscall[51].pCurrent)
41575
41576 { "ReadFile", (SYSCALL)ReadFile, 0 },
41577
41578#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
41579 LPOVERLAPPED))aSyscall[52].pCurrent)
41580
41581 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
41582
41583#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
41584
41585#if !SQLITE_OS_WINRT
41586 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
41587#else
41588 { "SetFilePointer", (SYSCALL)0, 0 },
41589#endif
41590
41591#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
41592 DWORD))aSyscall[54].pCurrent)
41593
41594#if !SQLITE_OS_WINRT
41595 { "Sleep", (SYSCALL)Sleep, 0 },
41596#else
41597 { "Sleep", (SYSCALL)0, 0 },
41598#endif
41599
41600#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
41601
41602 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
41603
41604#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
41605 LPFILETIME))aSyscall[56].pCurrent)
41606
41607#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41608 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
41609#else
41610 { "UnlockFile", (SYSCALL)0, 0 },
41611#endif
41612
41613#ifndef osUnlockFile
41614#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41615 DWORD))aSyscall[57].pCurrent)
41616#endif
41617
41618#if !SQLITE_OS_WINCE
41619 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
41620#else
41621 { "UnlockFileEx", (SYSCALL)0, 0 },
41622#endif
41623
41624#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
41625 LPOVERLAPPED))aSyscall[58].pCurrent)
41626
41627#if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0
41628 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
41629#else
41630 { "UnmapViewOfFile", (SYSCALL)0, 0 },
41631#endif
41632
41633#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
41634
41635 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
41636
41637#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
41638 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
41639
41640 { "WriteFile", (SYSCALL)WriteFile, 0 },
41641
41642#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
41643 LPOVERLAPPED))aSyscall[61].pCurrent)
41644
41645#if SQLITE_OS_WINRT
41646 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
41647#else
41648 { "CreateEventExW", (SYSCALL)0, 0 },
41649#endif
41650
41651#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
41652 DWORD,DWORD))aSyscall[62].pCurrent)
41653
41654#if !SQLITE_OS_WINRT
41655 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
41656#else
41657 { "WaitForSingleObject", (SYSCALL)0, 0 },
41658#endif
41659
41660#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
41661 DWORD))aSyscall[63].pCurrent)
41662
41663#if !SQLITE_OS_WINCE
41664 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
41665#else
41666 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
41667#endif
41668
41669#define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
41670 BOOL))aSyscall[64].pCurrent)
41671
41672#if SQLITE_OS_WINRT
41673 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
41674#else
41675 { "SetFilePointerEx", (SYSCALL)0, 0 },
41676#endif
41677
41678#define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
41679 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
41680
41681#if SQLITE_OS_WINRT
41682 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
41683#else
41684 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
41685#endif
41686
41687#define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
41688 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
41689
41690#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0)
41691 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
41692#else
41693 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
41694#endif
41695
41696#define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
41697 SIZE_T))aSyscall[67].pCurrent)
41698
41699#if SQLITE_OS_WINRT
41700 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
41701#else
41702 { "CreateFile2", (SYSCALL)0, 0 },
41703#endif
41704
41705#define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
41706 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
41707
41708#if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
41709 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
41710#else
41711 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
41712#endif
41713
41714#define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
41715 DWORD))aSyscall[69].pCurrent)
41716
41717#if SQLITE_OS_WINRT
41718 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
41719#else
41720 { "GetTickCount64", (SYSCALL)0, 0 },
41721#endif
41722
41723#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
41724
41725#if SQLITE_OS_WINRT
41726 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
41727#else
41728 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
41729#endif
41730
41731#define osGetNativeSystemInfo ((VOID(WINAPI*)( \
41732 LPSYSTEM_INFO))aSyscall[71].pCurrent)
41733
41734#if defined(SQLITE_WIN32_HAS_ANSI)
41735 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
41736#else
41737 { "OutputDebugStringA", (SYSCALL)0, 0 },
41738#endif
41739
41740#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
41741
41742#if defined(SQLITE_WIN32_HAS_WIDE)
41743 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
41744#else
41745 { "OutputDebugStringW", (SYSCALL)0, 0 },
41746#endif
41747
41748#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
41749
41750 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
41751
41752#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
41753
41754#if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE0x7fff0000>0)
41755 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
41756#else
41757 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
41758#endif
41759
41760#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
41761 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
41762
41763/*
41764** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
41765** is really just a macro that uses a compiler intrinsic (e.g. x64).
41766** So do not try to make this is into a redefinable interface.
41767*/
41768#if defined(InterlockedCompareExchange)
41769 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
41770
41771#define osInterlockedCompareExchange InterlockedCompareExchange
41772#else
41773 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
41774
41775#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
41776 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
41777#endif /* defined(InterlockedCompareExchange) */
41778
41779#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
41780 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
41781#else
41782 { "UuidCreate", (SYSCALL)0, 0 },
41783#endif
41784
41785#define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
41786
41787#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
41788 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
41789#else
41790 { "UuidCreateSequential", (SYSCALL)0, 0 },
41791#endif
41792
41793#define osUuidCreateSequential \
41794 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
41795
41796#if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE0x7fff0000>0
41797 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
41798#else
41799 { "FlushViewOfFile", (SYSCALL)0, 0 },
41800#endif
41801
41802#define osFlushViewOfFile \
41803 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
41804
41805}; /* End of the overrideable system calls */
41806
41807/*
41808** This is the xSetSystemCall() method of sqlite3_vfs for all of the
41809** "win32" VFSes. Return SQLITE_OK opon successfully updating the
41810** system call pointer, or SQLITE_NOTFOUND if there is no configurable
41811** system call named zName.
41812*/
41813static int winSetSystemCall(
41814 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
41815 const char *zName, /* Name of system call to override */
41816 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
41817){
41818 unsigned int i;
41819 int rc = SQLITE_NOTFOUND12;
41820
41821 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
41822 if( zName==0 ){
41823 /* If no zName is given, restore all system calls to their default
41824 ** settings and return NULL
41825 */
41826 rc = SQLITE_OK0;
41827 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
41828 if( aSyscall[i].pDefault ){
41829 aSyscall[i].pCurrent = aSyscall[i].pDefault;
41830 }
41831 }
41832 }else{
41833 /* If zName is specified, operate on only the one system call
41834 ** specified.
41835 */
41836 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
41837 if( strcmp(zName, aSyscall[i].zName)==0 ){
41838 if( aSyscall[i].pDefault==0 ){
41839 aSyscall[i].pDefault = aSyscall[i].pCurrent;
41840 }
41841 rc = SQLITE_OK0;
41842 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
41843 aSyscall[i].pCurrent = pNewFunc;
41844 break;
41845 }
41846 }
41847 }
41848 return rc;
41849}
41850
41851/*
41852** Return the value of a system call. Return NULL if zName is not a
41853** recognized system call name. NULL is also returned if the system call
41854** is currently undefined.
41855*/
41856static sqlite3_syscall_ptr winGetSystemCall(
41857 sqlite3_vfs *pNotUsed,
41858 const char *zName
41859){
41860 unsigned int i;
41861
41862 UNUSED_PARAMETER(pNotUsed)(void)(pNotUsed);
41863 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
41864 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
41865 }
41866 return 0;
41867}
41868
41869/*
41870** Return the name of the first system call after zName. If zName==NULL
41871** then return the name of the first system call. Return NULL if zName
41872** is the last system call or if zName is not the name of a valid
41873** system call.
41874*/
41875static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
41876 int i = -1;
41877
41878 UNUSED_PARAMETER(p)(void)(p);
41879 if( zName ){
41880 for(i=0; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0])))-1; i++){
41881 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
41882 }
41883 }
41884 for(i++; i<ArraySize(aSyscall)((int)(sizeof(aSyscall)/sizeof(aSyscall[0]))); i++){
41885 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
41886 }
41887 return 0;
41888}
41889
41890#ifdef SQLITE_WIN32_MALLOC
41891/*
41892** If a Win32 native heap has been configured, this function will attempt to
41893** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
41894** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
41895** "pnLargest" argument, if non-zero, will be used to return the size of the
41896** largest committed free block in the heap, in bytes.
41897*/
41898SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
41899 int rc = SQLITE_OK0;
41900 UINT nLargest = 0;
41901 HANDLE hHeap;
41902
41903 winMemAssertMagic();
41904 hHeap = winMemGetHeap();
41905 assert( hHeap!=0 )((void) (0));
41906 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
41907#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
41908 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
41909#endif
41910#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
41911 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
41912 DWORD lastErrno = osGetLastError();
41913 if( lastErrno==NO_ERROR ){
41914 sqlite3_log(SQLITE_NOMEM7, "failed to HeapCompact (no space), heap=%p",
41915 (void*)hHeap);
41916 rc = SQLITE_NOMEM_BKPT7;
41917 }else{
41918 sqlite3_log(SQLITE_ERROR1, "failed to HeapCompact (%lu), heap=%p",
41919 osGetLastError(), (void*)hHeap);
41920 rc = SQLITE_ERROR1;
41921 }
41922 }
41923#else
41924 sqlite3_log(SQLITE_NOTFOUND12, "failed to HeapCompact, heap=%p",
41925 (void*)hHeap);
41926 rc = SQLITE_NOTFOUND12;
41927#endif
41928 if( pnLargest ) *pnLargest = nLargest;
41929 return rc;
41930}
41931
41932/*
41933** If a Win32 native heap has been configured, this function will attempt to
41934** destroy and recreate it. If the Win32 native heap is not isolated and/or
41935** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
41936** be returned and no changes will be made to the Win32 native heap.
41937*/
41938SQLITE_API int sqlite3_win32_reset_heap(){
41939 int rc;
41940 MUTEX_LOGIC( sqlite3_mutex *pMaster; )sqlite3_mutex *pMaster; /* The main static mutex */
41941 MUTEX_LOGIC( sqlite3_mutex *pMem; )sqlite3_mutex *pMem; /* The memsys static mutex */
41942 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )pMaster = sqlite3MutexAlloc(2);
41943 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )pMem = sqlite3MutexAlloc(3);
41944 sqlite3_mutex_enter(pMaster);
41945 sqlite3_mutex_enter(pMem);
41946 winMemAssertMagic();
41947 if( winMemGetHeap()!=NULL((void*)0) && winMemGetOwned() && sqlite3_memory_used()==0 ){
41948 /*
41949 ** At this point, there should be no outstanding memory allocations on
41950 ** the heap. Also, since both the master and memsys locks are currently
41951 ** being held by us, no other function (i.e. from another thread) should
41952 ** be able to even access the heap. Attempt to destroy and recreate our
41953 ** isolated Win32 native heap now.
41954 */
41955 assert( winMemGetHeap()!=NULL )((void) (0));
41956 assert( winMemGetOwned() )((void) (0));
41957 assert( sqlite3_memory_used()==0 )((void) (0));
41958 winMemShutdown(winMemGetDataPtr());
41959 assert( winMemGetHeap()==NULL )((void) (0));
41960 assert( !winMemGetOwned() )((void) (0));
41961 assert( sqlite3_memory_used()==0 )((void) (0));
41962 rc = winMemInit(winMemGetDataPtr());
41963 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL )((void) (0));
41964 assert( rc!=SQLITE_OK || winMemGetOwned() )((void) (0));
41965 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 )((void) (0));
41966 }else{
41967 /*
41968 ** The Win32 native heap cannot be modified because it may be in use.
41969 */
41970 rc = SQLITE_BUSY5;
41971 }
41972 sqlite3_mutex_leave(pMem);
41973 sqlite3_mutex_leave(pMaster);
41974 return rc;
41975}
41976#endif /* SQLITE_WIN32_MALLOC */
41977
41978/*
41979** This function outputs the specified (ANSI) string to the Win32 debugger
41980** (if available).
41981*/
41982
41983SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
41984 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
41985 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1))((nBuf)<((SQLITE_WIN32_DBG_BUF_SIZE - 1))?(nBuf):((SQLITE_WIN32_DBG_BUF_SIZE
- 1)))
; /* may be negative. */
41986 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
41987 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE )((void) (0));
41988#ifdef SQLITE_ENABLE_API_ARMOR
41989 if( !zBuf ){
41990 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(41990);
41991 return;
41992 }
41993#endif
41994#if defined(SQLITE_WIN32_HAS_ANSI)
41995 if( nMin>0 ){
41996 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
41997 memcpy(zDbgBuf, zBuf, nMin);
41998 osOutputDebugStringA(zDbgBuf);
41999 }else{
42000 osOutputDebugStringA(zBuf);
42001 }
42002#elif defined(SQLITE_WIN32_HAS_WIDE)
42003 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
42004 if ( osMultiByteToWideChar(
42005 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
42006 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
42007 return;
42008 }
42009 osOutputDebugStringW((LPCWSTR)zDbgBuf);
42010#else
42011 if( nMin>0 ){
42012 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
42013 memcpy(zDbgBuf, zBuf, nMin);
42014 fprintf(stderrstderr, "%s", zDbgBuf);
42015 }else{
42016 fprintf(stderrstderr, "%s", zBuf);
42017 }
42018#endif
42019}
42020
42021/*
42022** The following routine suspends the current thread for at least ms
42023** milliseconds. This is equivalent to the Win32 Sleep() interface.
42024*/
42025#if SQLITE_OS_WINRT
42026static HANDLE sleepObj = NULL((void*)0);
42027#endif
42028
42029SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
42030#if SQLITE_OS_WINRT
42031 if ( sleepObj==NULL((void*)0) ){
42032 sleepObj = osCreateEventExW(NULL((void*)0), NULL((void*)0), CREATE_EVENT_MANUAL_RESET,
42033 SYNCHRONIZE);
42034 }
42035 assert( sleepObj!=NULL )((void) (0));
42036 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
42037#else
42038 osSleep(milliseconds);
42039#endif
42040}
42041
42042#if SQLITE_MAX_WORKER_THREADS8>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
42043 SQLITE_THREADSAFE1>0
42044SQLITE_PRIVATEstatic DWORD sqlite3Win32Wait(HANDLE hObject){
42045 DWORD rc;
42046 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
42047 TRUE))==WAIT_IO_COMPLETION ){}
42048 return rc;
42049}
42050#endif
42051
42052/*
42053** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
42054** or WinCE. Return false (zero) for Win95, Win98, or WinME.
42055**
42056** Here is an interesting observation: Win95, Win98, and WinME lack
42057** the LockFileEx() API. But we can still statically link against that
42058** API as long as we don't call it when running Win95/98/ME. A call to
42059** this routine is used to determine if the host is Win95/98/ME or
42060** WinNT/2K/XP so that we will know whether or not we can safely call
42061** the LockFileEx() API.
42062*/
42063
42064#if !SQLITE_WIN32_GETVERSIONEX
42065# define osIsNT() (1)
42066#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
42067# define osIsNT() (1)
42068#elif !defined(SQLITE_WIN32_HAS_WIDE)
42069# define osIsNT() (0)
42070#else
42071# define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
42072#endif
42073
42074/*
42075** This function determines if the machine is running a version of Windows
42076** based on the NT kernel.
42077*/
42078SQLITE_API int sqlite3_win32_is_nt(void){
42079#if SQLITE_OS_WINRT
42080 /*
42081 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
42082 ** kernel.
42083 */
42084 return 1;
42085#elif SQLITE_WIN32_GETVERSIONEX
42086 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
42087#if defined(SQLITE_WIN32_HAS_ANSI)
42088 OSVERSIONINFOA sInfo;
42089 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
42090 osGetVersionExA(&sInfo);
42091 osInterlockedCompareExchange(&sqlite3_os_type,
42092 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
42093#elif defined(SQLITE_WIN32_HAS_WIDE)
42094 OSVERSIONINFOW sInfo;
42095 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
42096 osGetVersionExW(&sInfo);
42097 osInterlockedCompareExchange(&sqlite3_os_type,
42098 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
42099#endif
42100 }
42101 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
42102#elif SQLITE_TEST
42103 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
42104#else
42105 /*
42106 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
42107 ** deprecated are always assumed to be based on the NT kernel.
42108 */
42109 return 1;
42110#endif
42111}
42112
42113#ifdef SQLITE_WIN32_MALLOC
42114/*
42115** Allocate nBytes of memory.
42116*/
42117static void *winMemMalloc(int nBytes){
42118 HANDLE hHeap;
42119 void *p;
42120
42121 winMemAssertMagic();
42122 hHeap = winMemGetHeap();
42123 assert( hHeap!=0 )((void) (0));
42124 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
42125#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42126 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
42127#endif
42128 assert( nBytes>=0 )((void) (0));
42129 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
42130 if( !p ){
42131 sqlite3_log(SQLITE_NOMEM7, "failed to HeapAlloc %u bytes (%lu), heap=%p",
42132 nBytes, osGetLastError(), (void*)hHeap);
42133 }
42134 return p;
42135}
42136
42137/*
42138** Free memory.
42139*/
42140static void winMemFree(void *pPrior){
42141 HANDLE hHeap;
42142
42143 winMemAssertMagic();
42144 hHeap = winMemGetHeap();
42145 assert( hHeap!=0 )((void) (0));
42146 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
42147#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42148 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) )((void) (0));
42149#endif
42150 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
42151 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
42152 sqlite3_log(SQLITE_NOMEM7, "failed to HeapFree block %p (%lu), heap=%p",
42153 pPrior, osGetLastError(), (void*)hHeap);
42154 }
42155}
42156
42157/*
42158** Change the size of an existing memory allocation
42159*/
42160static void *winMemRealloc(void *pPrior, int nBytes){
42161 HANDLE hHeap;
42162 void *p;
42163
42164 winMemAssertMagic();
42165 hHeap = winMemGetHeap();
42166 assert( hHeap!=0 )((void) (0));
42167 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
42168#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42169 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) )((void) (0));
42170#endif
42171 assert( nBytes>=0 )((void) (0));
42172 if( !pPrior ){
42173 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
42174 }else{
42175 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
42176 }
42177 if( !p ){
42178 sqlite3_log(SQLITE_NOMEM7, "failed to %s %u bytes (%lu), heap=%p",
42179 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
42180 (void*)hHeap);
42181 }
42182 return p;
42183}
42184
42185/*
42186** Return the size of an outstanding allocation, in bytes.
42187*/
42188static int winMemSize(void *p){
42189 HANDLE hHeap;
42190 SIZE_T n;
42191
42192 winMemAssertMagic();
42193 hHeap = winMemGetHeap();
42194 assert( hHeap!=0 )((void) (0));
42195 assert( hHeap!=INVALID_HANDLE_VALUE )((void) (0));
42196#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42197 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) )((void) (0));
42198#endif
42199 if( !p ) return 0;
42200 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
42201 if( n==(SIZE_T)-1 ){
42202 sqlite3_log(SQLITE_NOMEM7, "failed to HeapSize block %p (%lu), heap=%p",
42203 p, osGetLastError(), (void*)hHeap);
42204 return 0;
42205 }
42206 return (int)n;
42207}
42208
42209/*
42210** Round up a request size to the next valid allocation size.
42211*/
42212static int winMemRoundup(int n){
42213 return n;
42214}
42215
42216/*
42217** Initialize this module.
42218*/
42219static int winMemInit(void *pAppData){
42220 winMemData *pWinMemData = (winMemData *)pAppData;
42221
42222 if( !pWinMemData ) return SQLITE_ERROR1;
42223 assert( pWinMemData->magic1==WINMEM_MAGIC1 )((void) (0));
42224 assert( pWinMemData->magic2==WINMEM_MAGIC2 )((void) (0));
42225
42226#if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
42227 if( !pWinMemData->hHeap ){
42228 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
42229 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfigsqlite3Config.nHeap;
42230 if( dwMaximumSize==0 ){
42231 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
42232 }else if( dwInitialSize>dwMaximumSize ){
42233 dwInitialSize = dwMaximumSize;
42234 }
42235 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
42236 dwInitialSize, dwMaximumSize);
42237 if( !pWinMemData->hHeap ){
42238 sqlite3_log(SQLITE_NOMEM7,
42239 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
42240 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
42241 dwMaximumSize);
42242 return SQLITE_NOMEM_BKPT7;
42243 }
42244 pWinMemData->bOwned = TRUE;
42245 assert( pWinMemData->bOwned )((void) (0));
42246 }
42247#else
42248 pWinMemData->hHeap = osGetProcessHeap();
42249 if( !pWinMemData->hHeap ){
42250 sqlite3_log(SQLITE_NOMEM7,
42251 "failed to GetProcessHeap (%lu)", osGetLastError());
42252 return SQLITE_NOMEM_BKPT7;
42253 }
42254 pWinMemData->bOwned = FALSE;
42255 assert( !pWinMemData->bOwned )((void) (0));
42256#endif
42257 assert( pWinMemData->hHeap!=0 )((void) (0));
42258 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE )((void) (0));
42259#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42260 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
42261#endif
42262 return SQLITE_OK0;
42263}
42264
42265/*
42266** Deinitialize this module.
42267*/
42268static void winMemShutdown(void *pAppData){
42269 winMemData *pWinMemData = (winMemData *)pAppData;
42270
42271 if( !pWinMemData ) return;
42272 assert( pWinMemData->magic1==WINMEM_MAGIC1 )((void) (0));
42273 assert( pWinMemData->magic2==WINMEM_MAGIC2 )((void) (0));
42274
42275 if( pWinMemData->hHeap ){
42276 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE )((void) (0));
42277#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
42278 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) )((void) (0));
42279#endif
42280 if( pWinMemData->bOwned ){
42281 if( !osHeapDestroy(pWinMemData->hHeap) ){
42282 sqlite3_log(SQLITE_NOMEM7, "failed to HeapDestroy (%lu), heap=%p",
42283 osGetLastError(), (void*)pWinMemData->hHeap);
42284 }
42285 pWinMemData->bOwned = FALSE;
42286 }
42287 pWinMemData->hHeap = NULL((void*)0);
42288 }
42289}
42290
42291/*
42292** Populate the low-level memory allocation function pointers in
42293** sqlite3GlobalConfig.m with pointers to the routines in this file. The
42294** arguments specify the block of memory to manage.
42295**
42296** This routine is only called by sqlite3_config(), and therefore
42297** is not required to be threadsafe (it is not).
42298*/
42299SQLITE_PRIVATEstatic const sqlite3_mem_methods *sqlite3MemGetWin32(void){
42300 static const sqlite3_mem_methods winMemMethods = {
42301 winMemMalloc,
42302 winMemFree,
42303 winMemRealloc,
42304 winMemSize,
42305 winMemRoundup,
42306 winMemInit,
42307 winMemShutdown,
42308 &win_mem_data
42309 };
42310 return &winMemMethods;
42311}
42312
42313SQLITE_PRIVATEstatic void sqlite3MemSetDefault(void){
42314 sqlite3_config(SQLITE_CONFIG_MALLOC4, sqlite3MemGetWin32());
42315}
42316#endif /* SQLITE_WIN32_MALLOC */
42317
42318/*
42319** Convert a UTF-8 string to Microsoft Unicode.
42320**
42321** Space to hold the returned string is obtained from sqlite3_malloc().
42322*/
42323static LPWSTR winUtf8ToUnicode(const char *zText){
42324 int nChar;
42325 LPWSTR zWideText;
42326
42327 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL((void*)0), 0);
42328 if( nChar==0 ){
42329 return 0;
42330 }
42331 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
42332 if( zWideText==0 ){
42333 return 0;
42334 }
42335 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
42336 nChar);
42337 if( nChar==0 ){
42338 sqlite3_free(zWideText);
42339 zWideText = 0;
42340 }
42341 return zWideText;
42342}
42343
42344/*
42345** Convert a Microsoft Unicode string to UTF-8.
42346**
42347** Space to hold the returned string is obtained from sqlite3_malloc().
42348*/
42349static char *winUnicodeToUtf8(LPCWSTR zWideText){
42350 int nByte;
42351 char *zText;
42352
42353 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
42354 if( nByte == 0 ){
42355 return 0;
42356 }
42357 zText = sqlite3MallocZero( nByte );
42358 if( zText==0 ){
42359 return 0;
42360 }
42361 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
42362 0, 0);
42363 if( nByte == 0 ){
42364 sqlite3_free(zText);
42365 zText = 0;
42366 }
42367 return zText;
42368}
42369
42370/*
42371** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
42372** code page.
42373**
42374** Space to hold the returned string is obtained from sqlite3_malloc().
42375*/
42376static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
42377 int nByte;
42378 LPWSTR zMbcsText;
42379 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
42380
42381 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL((void*)0),
42382 0)*sizeof(WCHAR);
42383 if( nByte==0 ){
42384 return 0;
42385 }
42386 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
42387 if( zMbcsText==0 ){
42388 return 0;
42389 }
42390 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
42391 nByte);
42392 if( nByte==0 ){
42393 sqlite3_free(zMbcsText);
42394 zMbcsText = 0;
42395 }
42396 return zMbcsText;
42397}
42398
42399/*
42400** Convert a Microsoft Unicode string to a multi-byte character string,
42401** using the ANSI or OEM code page.
42402**
42403** Space to hold the returned string is obtained from sqlite3_malloc().
42404*/
42405static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
42406 int nByte;
42407 char *zText;
42408 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
42409
42410 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
42411 if( nByte == 0 ){
42412 return 0;
42413 }
42414 zText = sqlite3MallocZero( nByte );
42415 if( zText==0 ){
42416 return 0;
42417 }
42418 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
42419 nByte, 0, 0);
42420 if( nByte == 0 ){
42421 sqlite3_free(zText);
42422 zText = 0;
42423 }
42424 return zText;
42425}
42426
42427/*
42428** Convert a multi-byte character string to UTF-8.
42429**
42430** Space to hold the returned string is obtained from sqlite3_malloc().
42431*/
42432static char *winMbcsToUtf8(const char *zText, int useAnsi){
42433 char *zTextUtf8;
42434 LPWSTR zTmpWide;
42435
42436 zTmpWide = winMbcsToUnicode(zText, useAnsi);
42437 if( zTmpWide==0 ){
42438 return 0;
42439 }
42440 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
42441 sqlite3_free(zTmpWide);
42442 return zTextUtf8;
42443}
42444
42445/*
42446** Convert a UTF-8 string to a multi-byte character string.
42447**
42448** Space to hold the returned string is obtained from sqlite3_malloc().
42449*/
42450static char *winUtf8ToMbcs(const char *zText, int useAnsi){
42451 char *zTextMbcs;
42452 LPWSTR zTmpWide;
42453
42454 zTmpWide = winUtf8ToUnicode(zText);
42455 if( zTmpWide==0 ){
42456 return 0;
42457 }
42458 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
42459 sqlite3_free(zTmpWide);
42460 return zTextMbcs;
42461}
42462
42463/*
42464** This is a public wrapper for the winUtf8ToUnicode() function.
42465*/
42466SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
42467#ifdef SQLITE_ENABLE_API_ARMOR
42468 if( !zText ){
42469 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(42469);
42470 return 0;
42471 }
42472#endif
42473#ifndef SQLITE_OMIT_AUTOINIT
42474 if( sqlite3_initialize() ) return 0;
42475#endif
42476 return winUtf8ToUnicode(zText);
42477}
42478
42479/*
42480** This is a public wrapper for the winUnicodeToUtf8() function.
42481*/
42482SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
42483#ifdef SQLITE_ENABLE_API_ARMOR
42484 if( !zWideText ){
42485 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(42485);
42486 return 0;
42487 }
42488#endif
42489#ifndef SQLITE_OMIT_AUTOINIT
42490 if( sqlite3_initialize() ) return 0;
42491#endif
42492 return winUnicodeToUtf8(zWideText);
42493}
42494
42495/*
42496** This is a public wrapper for the winMbcsToUtf8() function.
42497*/
42498SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
42499#ifdef SQLITE_ENABLE_API_ARMOR
42500 if( !zText ){
42501 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(42501);
42502 return 0;
42503 }
42504#endif
42505#ifndef SQLITE_OMIT_AUTOINIT
42506 if( sqlite3_initialize() ) return 0;
42507#endif
42508 return winMbcsToUtf8(zText, osAreFileApisANSI());
42509}
42510
42511/*
42512** This is a public wrapper for the winMbcsToUtf8() function.
42513*/
42514SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
42515#ifdef SQLITE_ENABLE_API_ARMOR
42516 if( !zText ){
42517 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(42517);
42518 return 0;
42519 }
42520#endif
42521#ifndef SQLITE_OMIT_AUTOINIT
42522 if( sqlite3_initialize() ) return 0;
42523#endif
42524 return winMbcsToUtf8(zText, useAnsi);
42525}
42526
42527/*
42528** This is a public wrapper for the winUtf8ToMbcs() function.
42529*/
42530SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
42531#ifdef SQLITE_ENABLE_API_ARMOR
42532 if( !zText ){
42533 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(42533);
42534 return 0;
42535 }
42536#endif
42537#ifndef SQLITE_OMIT_AUTOINIT
42538 if( sqlite3_initialize() ) return 0;
42539#endif
42540 return winUtf8ToMbcs(zText, osAreFileApisANSI());
42541}
42542
42543/*
42544** This is a public wrapper for the winUtf8ToMbcs() function.
42545*/
42546SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
42547#ifdef SQLITE_ENABLE_API_ARMOR
42548 if( !zText ){
42549 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(42549);
42550 return 0;
42551 }
42552#endif
42553#ifndef SQLITE_OMIT_AUTOINIT
42554 if( sqlite3_initialize() ) return 0;
42555#endif
42556 return winUtf8ToMbcs(zText, useAnsi);
42557}
42558
42559/*
42560** This function is the same as sqlite3_win32_set_directory (below); however,
42561** it accepts a UTF-8 string.
42562*/
42563SQLITE_API int sqlite3_win32_set_directory8(
42564 unsigned long type, /* Identifier for directory being set or reset */
42565 const char *zValue /* New value for directory being set or reset */
42566){
42567 char **ppDirectory = 0;
42568#ifndef SQLITE_OMIT_AUTOINIT
42569 int rc = sqlite3_initialize();
42570 if( rc ) return rc;
42571#endif
42572 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE1 ){
42573 ppDirectory = &sqlite3_data_directory;
42574 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE2 ){
42575 ppDirectory = &sqlite3_temp_directory;
42576 }
42577 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE((void) (0))
42578 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE((void) (0))
42579 )((void) (0));
42580 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) )((void) (0));
42581 if( ppDirectory ){
42582 char *zCopy = 0;
42583 if( zValue && zValue[0] ){
42584 zCopy = sqlite3_mprintf("%s", zValue);
42585 if ( zCopy==0 ){
42586 return SQLITE_NOMEM_BKPT7;
42587 }
42588 }
42589 sqlite3_free(*ppDirectory);
42590 *ppDirectory = zCopy;
42591 return SQLITE_OK0;
42592 }
42593 return SQLITE_ERROR1;
42594}
42595
42596/*
42597** This function is the same as sqlite3_win32_set_directory (below); however,
42598** it accepts a UTF-16 string.
42599*/
42600SQLITE_API int sqlite3_win32_set_directory16(
42601 unsigned long type, /* Identifier for directory being set or reset */
42602 const void *zValue /* New value for directory being set or reset */
42603){
42604 int rc;
42605 char *zUtf8 = 0;
42606 if( zValue ){
42607 zUtf8 = sqlite3_win32_unicode_to_utf8(zValue);
42608 if( zUtf8==0 ) return SQLITE_NOMEM_BKPT7;
42609 }
42610 rc = sqlite3_win32_set_directory8(type, zUtf8);
42611 if( zUtf8 ) sqlite3_free(zUtf8);
42612 return rc;
42613}
42614
42615/*
42616** This function sets the data directory or the temporary directory based on
42617** the provided arguments. The type argument must be 1 in order to set the
42618** data directory or 2 in order to set the temporary directory. The zValue
42619** argument is the name of the directory to use. The return value will be
42620** SQLITE_OK if successful.
42621*/
42622SQLITE_API int sqlite3_win32_set_directory(
42623 unsigned long type, /* Identifier for directory being set or reset */
42624 void *zValue /* New value for directory being set or reset */
42625){
42626 return sqlite3_win32_set_directory16(type, zValue);
42627}
42628
42629/*
42630** The return value of winGetLastErrorMsg
42631** is zero if the error message fits in the buffer, or non-zero
42632** otherwise (if the message was truncated).
42633*/
42634static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
42635 /* FormatMessage returns 0 on failure. Otherwise it
42636 ** returns the number of TCHARs written to the output
42637 ** buffer, excluding the terminating null char.
42638 */
42639 DWORD dwLen = 0;
42640 char *zOut = 0;
42641
42642 if( osIsNT() ){
42643#if SQLITE_OS_WINRT
42644 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
42645 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
42646 FORMAT_MESSAGE_IGNORE_INSERTS,
42647 NULL((void*)0),
42648 lastErrno,
42649 0,
42650 zTempWide,
42651 SQLITE_WIN32_MAX_ERRMSG_CHARS,
42652 0);
42653#else
42654 LPWSTR zTempWide = NULL((void*)0);
42655 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
42656 FORMAT_MESSAGE_FROM_SYSTEM |
42657 FORMAT_MESSAGE_IGNORE_INSERTS,
42658 NULL((void*)0),
42659 lastErrno,
42660 0,
42661 (LPWSTR) &zTempWide,
42662 0,
42663 0);
42664#endif
42665 if( dwLen > 0 ){
42666 /* allocate a buffer and convert to UTF8 */
42667 sqlite3BeginBenignMalloc();
42668 zOut = winUnicodeToUtf8(zTempWide);
42669 sqlite3EndBenignMalloc();
42670#if !SQLITE_OS_WINRT
42671 /* free the system buffer allocated by FormatMessage */
42672 osLocalFree(zTempWide);
42673#endif
42674 }
42675 }
42676#ifdef SQLITE_WIN32_HAS_ANSI
42677 else{
42678 char *zTemp = NULL((void*)0);
42679 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
42680 FORMAT_MESSAGE_FROM_SYSTEM |
42681 FORMAT_MESSAGE_IGNORE_INSERTS,
42682 NULL((void*)0),
42683 lastErrno,
42684 0,
42685 (LPSTR) &zTemp,
42686 0,
42687 0);
42688 if( dwLen > 0 ){
42689 /* allocate a buffer and convert to UTF8 */
42690 sqlite3BeginBenignMalloc();
42691 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
42692 sqlite3EndBenignMalloc();
42693 /* free the system buffer allocated by FormatMessage */
42694 osLocalFree(zTemp);
42695 }
42696 }
42697#endif
42698 if( 0 == dwLen ){
42699 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
42700 }else{
42701 /* copy a maximum of nBuf chars to output buffer */
42702 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
42703 /* free the UTF8 buffer */
42704 sqlite3_free(zOut);
42705 }
42706 return 0;
42707}
42708
42709/*
42710**
42711** This function - winLogErrorAtLine() - is only ever called via the macro
42712** winLogError().
42713**
42714** This routine is invoked after an error occurs in an OS function.
42715** It logs a message using sqlite3_log() containing the current value of
42716** error code and, if possible, the human-readable equivalent from
42717** FormatMessage.
42718**
42719** The first argument passed to the macro should be the error code that
42720** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
42721** The two subsequent arguments should be the name of the OS function that
42722** failed and the associated file-system path, if any.
42723*/
42724#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__42724)
42725static int winLogErrorAtLine(
42726 int errcode, /* SQLite error code */
42727 DWORD lastErrno, /* Win32 last error */
42728 const char *zFunc, /* Name of OS function that failed */
42729 const char *zPath, /* File path associated with error */
42730 int iLine /* Source line number where error occurred */
42731){
42732 char zMsg[500]; /* Human readable error text */
42733 int i; /* Loop counter */
42734
42735 zMsg[0] = 0;
42736 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
42737 assert( errcode!=SQLITE_OK )((void) (0));
42738 if( zPath==0 ) zPath = "";
42739 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
42740 zMsg[i] = 0;
42741 sqlite3_log(errcode,
42742 "os_win.c:%d: (%lu) %s(%s) - %s",
42743 iLine, lastErrno, zFunc, zPath, zMsg
42744 );
42745
42746 return errcode;
42747}
42748
42749/*
42750** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
42751** will be retried following a locking error - probably caused by
42752** antivirus software. Also the initial delay before the first retry.
42753** The delay increases linearly with each retry.
42754*/
42755#ifndef SQLITE_WIN32_IOERR_RETRY
42756# define SQLITE_WIN32_IOERR_RETRY 10
42757#endif
42758#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
42759# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
42760#endif
42761static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
42762static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
42763
42764/*
42765** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
42766** error code obtained via GetLastError() is eligible to be retried. It
42767** must accept the error code DWORD as its only argument and should return
42768** non-zero if the error code is transient in nature and the operation
42769** responsible for generating the original error might succeed upon being
42770** retried. The argument to this macro should be a variable.
42771**
42772** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
42773** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
42774** returns zero. The "winIoerrCanRetry2" macro is completely optional and
42775** may be used to include additional error codes in the set that should
42776** result in the failing I/O operation being retried by the caller. If
42777** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
42778** identical to those of the "winIoerrCanRetry1" macro.
42779*/
42780#if !defined(winIoerrCanRetry1)
42781#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
42782 ((a)==ERROR_SHARING_VIOLATION) || \
42783 ((a)==ERROR_LOCK_VIOLATION) || \
42784 ((a)==ERROR_DEV_NOT_EXIST) || \
42785 ((a)==ERROR_NETNAME_DELETED) || \
42786 ((a)==ERROR_SEM_TIMEOUT) || \
42787 ((a)==ERROR_NETWORK_UNREACHABLE))
42788#endif
42789
42790/*
42791** If a ReadFile() or WriteFile() error occurs, invoke this routine
42792** to see if it should be retried. Return TRUE to retry. Return FALSE
42793** to give up with an error.
42794*/
42795static int winRetryIoerr(int *pnRetry, DWORD *pError){
42796 DWORD e = osGetLastError();
42797 if( *pnRetry>=winIoerrRetry ){
42798 if( pError ){
42799 *pError = e;
42800 }
42801 return 0;
42802 }
42803 if( winIoerrCanRetry1(e) ){
42804 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
42805 ++*pnRetry;
42806 return 1;
42807 }
42808#if defined(winIoerrCanRetry2)
42809 else if( winIoerrCanRetry2(e) ){
42810 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
42811 ++*pnRetry;
42812 return 1;
42813 }
42814#endif
42815 if( pError ){
42816 *pError = e;
42817 }
42818 return 0;
42819}
42820
42821/*
42822** Log a I/O error retry episode.
42823*/
42824static void winLogIoerr(int nRetry, int lineno){
42825 if( nRetry ){
42826 sqlite3_log(SQLITE_NOTICE27,
42827 "delayed %dms for lock/sharing conflict at line %d",
42828 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
42829 );
42830 }
42831}
42832
42833/*
42834** This #if does not rely on the SQLITE_OS_WINCE define because the
42835** corresponding section in "date.c" cannot use it.
42836*/
42837#if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
42838 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
42839/*
42840** The MSVC CRT on Windows CE may not have a localtime() function.
42841** So define a substitute.
42842*/
42843/* # include <time.h> */
42844struct tm *__cdecl localtime(const time_t *t)
42845{
42846 static struct tm y;
42847 FILETIME uTm, lTm;
42848 SYSTEMTIME pTm;
42849 sqlite3_int64 t64;
42850 t64 = *t;
42851 t64 = (t64 + 11644473600)*10000000;
42852 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
42853 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
42854 osFileTimeToLocalFileTime(&uTm,&lTm);
42855 osFileTimeToSystemTime(&lTm,&pTm);
42856 y.tm_year = pTm.wYear - 1900;
42857 y.tm_mon = pTm.wMonth - 1;
42858 y.tm_wday = pTm.wDayOfWeek;
42859 y.tm_mday = pTm.wDay;
42860 y.tm_hour = pTm.wHour;
42861 y.tm_min = pTm.wMinute;
42862 y.tm_sec = pTm.wSecond;
42863 return &y;
42864}
42865#endif
42866
42867#if SQLITE_OS_WINCE
42868/*************************************************************************
42869** This section contains code for WinCE only.
42870*/
42871#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)__builtin_offsetof(winFile, h)]
42872
42873/*
42874** Acquire a lock on the handle h
42875*/
42876static void winceMutexAcquire(HANDLE h){
42877 DWORD dwErr;
42878 do {
42879 dwErr = osWaitForSingleObject(h, INFINITE);
42880 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
42881}
42882/*
42883** Release a lock acquired by winceMutexAcquire()
42884*/
42885#define winceMutexRelease(h) ReleaseMutex(h)
42886
42887/*
42888** Create the mutex and shared memory used for locking in the file
42889** descriptor pFile
42890*/
42891static int winceCreateLock(const char *zFilename, winFile *pFile){
42892 LPWSTR zTok;
42893 LPWSTR zName;
42894 DWORD lastErrno;
42895 BOOL bLogged = FALSE;
42896 BOOL bInit = TRUE;
42897
42898 zName = winUtf8ToUnicode(zFilename);
42899 if( zName==0 ){
42900 /* out of memory */
42901 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
42902 }
42903
42904 /* Initialize the local lockdata */
42905 memset(&pFile->local, 0, sizeof(pFile->local));
42906
42907 /* Replace the backslashes from the filename and lowercase it
42908 ** to derive a mutex name. */
42909 zTok = osCharLowerW(zName);
42910 for (;*zTok;zTok++){
42911 if (*zTok == '\\') *zTok = '_';
42912 }
42913
42914 /* Create/open the named mutex */
42915 pFile->hMutex = osCreateMutexW(NULL((void*)0), FALSE, zName);
42916 if (!pFile->hMutex){
42917 pFile->lastErrno = osGetLastError();
42918 sqlite3_free(zName);
42919 return winLogError(SQLITE_IOERR10, pFile->lastErrno,
42920 "winceCreateLock1", zFilename);
42921 }
42922
42923 /* Acquire the mutex before continuing */
42924 winceMutexAcquire(pFile->hMutex);
42925
42926 /* Since the names of named mutexes, semaphores, file mappings etc are
42927 ** case-sensitive, take advantage of that by uppercasing the mutex name
42928 ** and using that as the shared filemapping name.
42929 */
42930 osCharUpperW(zName);
42931 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL((void*)0),
42932 PAGE_READWRITE, 0, sizeof(winceLock),
42933 zName);
42934
42935 /* Set a flag that indicates we're the first to create the memory so it
42936 ** must be zero-initialized */
42937 lastErrno = osGetLastError();
42938 if (lastErrno == ERROR_ALREADY_EXISTS){
42939 bInit = FALSE;
42940 }
42941
42942 sqlite3_free(zName);
42943
42944 /* If we succeeded in making the shared memory handle, map it. */
42945 if( pFile->hShared ){
42946 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
42947 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
42948 /* If mapping failed, close the shared memory handle and erase it */
42949 if( !pFile->shared ){
42950 pFile->lastErrno = osGetLastError();
42951 winLogError(SQLITE_IOERR10, pFile->lastErrno,
42952 "winceCreateLock2", zFilename);
42953 bLogged = TRUE;
42954 osCloseHandle(pFile->hShared);
42955 pFile->hShared = NULL((void*)0);
42956 }
42957 }
42958
42959 /* If shared memory could not be created, then close the mutex and fail */
42960 if( pFile->hShared==NULL((void*)0) ){
42961 if( !bLogged ){
42962 pFile->lastErrno = lastErrno;
42963 winLogError(SQLITE_IOERR10, pFile->lastErrno,
42964 "winceCreateLock3", zFilename);
42965 bLogged = TRUE;
42966 }
42967 winceMutexRelease(pFile->hMutex);
42968 osCloseHandle(pFile->hMutex);
42969 pFile->hMutex = NULL((void*)0);
42970 return SQLITE_IOERR10;
42971 }
42972
42973 /* Initialize the shared memory if we're supposed to */
42974 if( bInit ){
42975 memset(pFile->shared, 0, sizeof(winceLock));
42976 }
42977
42978 winceMutexRelease(pFile->hMutex);
42979 return SQLITE_OK0;
42980}
42981
42982/*
42983** Destroy the part of winFile that deals with wince locks
42984*/
42985static void winceDestroyLock(winFile *pFile){
42986 if (pFile->hMutex){
42987 /* Acquire the mutex */
42988 winceMutexAcquire(pFile->hMutex);
42989
42990 /* The following blocks should probably assert in debug mode, but they
42991 are to cleanup in case any locks remained open */
42992 if (pFile->local.nReaders){
42993 pFile->shared->nReaders --;
42994 }
42995 if (pFile->local.bReserved){
42996 pFile->shared->bReserved = FALSE;
42997 }
42998 if (pFile->local.bPending){
42999 pFile->shared->bPending = FALSE;
43000 }
43001 if (pFile->local.bExclusive){
43002 pFile->shared->bExclusive = FALSE;
43003 }
43004
43005 /* De-reference and close our copy of the shared memory handle */
43006 osUnmapViewOfFile(pFile->shared);
43007 osCloseHandle(pFile->hShared);
43008
43009 /* Done with the mutex */
43010 winceMutexRelease(pFile->hMutex);
43011 osCloseHandle(pFile->hMutex);
43012 pFile->hMutex = NULL((void*)0);
43013 }
43014}
43015
43016/*
43017** An implementation of the LockFile() API of Windows for CE
43018*/
43019static BOOL winceLockFile(
43020 LPHANDLE phFile,
43021 DWORD dwFileOffsetLow,
43022 DWORD dwFileOffsetHigh,
43023 DWORD nNumberOfBytesToLockLow,
43024 DWORD nNumberOfBytesToLockHigh
43025){
43026 winFile *pFile = HANDLE_TO_WINFILE(phFile);
43027 BOOL bReturn = FALSE;
43028
43029 UNUSED_PARAMETER(dwFileOffsetHigh)(void)(dwFileOffsetHigh);
43030 UNUSED_PARAMETER(nNumberOfBytesToLockHigh)(void)(nNumberOfBytesToLockHigh);
43031
43032 if (!pFile->hMutex) return TRUE;
43033 winceMutexAcquire(pFile->hMutex);
43034
43035 /* Wanting an exclusive lock? */
43036 if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2)
43037 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE510){
43038 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
43039 pFile->shared->bExclusive = TRUE;
43040 pFile->local.bExclusive = TRUE;
43041 bReturn = TRUE;
43042 }
43043 }
43044
43045 /* Want a read-only lock? */
43046 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2) &&
43047 nNumberOfBytesToLockLow == 1){
43048 if (pFile->shared->bExclusive == 0){
43049 pFile->local.nReaders ++;
43050 if (pFile->local.nReaders == 1){
43051 pFile->shared->nReaders ++;
43052 }
43053 bReturn = TRUE;
43054 }
43055 }
43056
43057 /* Want a pending lock? */
43058 else if (dwFileOffsetLow == (DWORD)PENDING_BYTEsqlite3PendingByte
43059 && nNumberOfBytesToLockLow == 1){
43060 /* If no pending lock has been acquired, then acquire it */
43061 if (pFile->shared->bPending == 0) {
43062 pFile->shared->bPending = TRUE;
43063 pFile->local.bPending = TRUE;
43064 bReturn = TRUE;
43065 }
43066 }
43067
43068 /* Want a reserved lock? */
43069 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE(sqlite3PendingByte+1)
43070 && nNumberOfBytesToLockLow == 1){
43071 if (pFile->shared->bReserved == 0) {
43072 pFile->shared->bReserved = TRUE;
43073 pFile->local.bReserved = TRUE;
43074 bReturn = TRUE;
43075 }
43076 }
43077
43078 winceMutexRelease(pFile->hMutex);
43079 return bReturn;
43080}
43081
43082/*
43083** An implementation of the UnlockFile API of Windows for CE
43084*/
43085static BOOL winceUnlockFile(
43086 LPHANDLE phFile,
43087 DWORD dwFileOffsetLow,
43088 DWORD dwFileOffsetHigh,
43089 DWORD nNumberOfBytesToUnlockLow,
43090 DWORD nNumberOfBytesToUnlockHigh
43091){
43092 winFile *pFile = HANDLE_TO_WINFILE(phFile);
43093 BOOL bReturn = FALSE;
43094
43095 UNUSED_PARAMETER(dwFileOffsetHigh)(void)(dwFileOffsetHigh);
43096 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh)(void)(nNumberOfBytesToUnlockHigh);
43097
43098 if (!pFile->hMutex) return TRUE;
43099 winceMutexAcquire(pFile->hMutex);
43100
43101 /* Releasing a reader lock or an exclusive lock */
43102 if (dwFileOffsetLow == (DWORD)SHARED_FIRST(sqlite3PendingByte+2)){
43103 /* Did we have an exclusive lock? */
43104 if (pFile->local.bExclusive){
43105 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE)((void) (0));
43106 pFile->local.bExclusive = FALSE;
43107 pFile->shared->bExclusive = FALSE;
43108 bReturn = TRUE;
43109 }
43110
43111 /* Did we just have a reader lock? */
43112 else if (pFile->local.nReaders){
43113 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE((void) (0))
43114 || nNumberOfBytesToUnlockLow == 1)((void) (0));
43115 pFile->local.nReaders --;
43116 if (pFile->local.nReaders == 0)
43117 {
43118 pFile->shared->nReaders --;
43119 }
43120 bReturn = TRUE;
43121 }
43122 }
43123
43124 /* Releasing a pending lock */
43125 else if (dwFileOffsetLow == (DWORD)PENDING_BYTEsqlite3PendingByte
43126 && nNumberOfBytesToUnlockLow == 1){
43127 if (pFile->local.bPending){
43128 pFile->local.bPending = FALSE;
43129 pFile->shared->bPending = FALSE;
43130 bReturn = TRUE;
43131 }
43132 }
43133 /* Releasing a reserved lock */
43134 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE(sqlite3PendingByte+1)
43135 && nNumberOfBytesToUnlockLow == 1){
43136 if (pFile->local.bReserved) {
43137 pFile->local.bReserved = FALSE;
43138 pFile->shared->bReserved = FALSE;
43139 bReturn = TRUE;
43140 }
43141 }
43142
43143 winceMutexRelease(pFile->hMutex);
43144 return bReturn;
43145}
43146/*
43147** End of the special code for wince
43148*****************************************************************************/
43149#endif /* SQLITE_OS_WINCE */
43150
43151/*
43152** Lock a file region.
43153*/
43154static BOOL winLockFile(
43155 LPHANDLE phFile,
43156 DWORD flags,
43157 DWORD offsetLow,
43158 DWORD offsetHigh,
43159 DWORD numBytesLow,
43160 DWORD numBytesHigh
43161){
43162#if SQLITE_OS_WINCE
43163 /*
43164 ** NOTE: Windows CE is handled differently here due its lack of the Win32
43165 ** API LockFile.
43166 */
43167 return winceLockFile(phFile, offsetLow, offsetHigh,
43168 numBytesLow, numBytesHigh);
43169#else
43170 if( osIsNT() ){
43171 OVERLAPPED ovlp;
43172 memset(&ovlp, 0, sizeof(OVERLAPPED));
43173 ovlp.Offset = offsetLow;
43174 ovlp.OffsetHigh = offsetHigh;
43175 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
43176 }else{
43177 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
43178 numBytesHigh);
43179 }
43180#endif
43181}
43182
43183/*
43184** Unlock a file region.
43185 */
43186static BOOL winUnlockFile(
43187 LPHANDLE phFile,
43188 DWORD offsetLow,
43189 DWORD offsetHigh,
43190 DWORD numBytesLow,
43191 DWORD numBytesHigh
43192){
43193#if SQLITE_OS_WINCE
43194 /*
43195 ** NOTE: Windows CE is handled differently here due its lack of the Win32
43196 ** API UnlockFile.
43197 */
43198 return winceUnlockFile(phFile, offsetLow, offsetHigh,
43199 numBytesLow, numBytesHigh);
43200#else
43201 if( osIsNT() ){
43202 OVERLAPPED ovlp;
43203 memset(&ovlp, 0, sizeof(OVERLAPPED));
43204 ovlp.Offset = offsetLow;
43205 ovlp.OffsetHigh = offsetHigh;
43206 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
43207 }else{
43208 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
43209 numBytesHigh);
43210 }
43211#endif
43212}
43213
43214/*****************************************************************************
43215** The next group of routines implement the I/O methods specified
43216** by the sqlite3_io_methods object.
43217******************************************************************************/
43218
43219/*
43220** Some Microsoft compilers lack this definition.
43221*/
43222#ifndef INVALID_SET_FILE_POINTER
43223# define INVALID_SET_FILE_POINTER ((DWORD)-1)
43224#endif
43225
43226/*
43227** Move the current position of the file handle passed as the first
43228** argument to offset iOffset within the file. If successful, return 0.
43229** Otherwise, set pFile->lastErrno and return non-zero.
43230*/
43231static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
43232#if !SQLITE_OS_WINRT
43233 LONG upperBits; /* Most sig. 32 bits of new offset */
43234 LONG lowerBits; /* Least sig. 32 bits of new offset */
43235 DWORD dwRet; /* Value returned by SetFilePointer() */
43236 DWORD lastErrno; /* Value returned by GetLastError() */
43237
43238 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
43239
43240 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
43241 lowerBits = (LONG)(iOffset & 0xffffffff);
43242
43243 /* API oddity: If successful, SetFilePointer() returns a dword
43244 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
43245 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
43246 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
43247 ** whether an error has actually occurred, it is also necessary to call
43248 ** GetLastError().
43249 */
43250 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
43251
43252 if( (dwRet==INVALID_SET_FILE_POINTER
43253 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
43254 pFile->lastErrno = lastErrno;
43255 winLogError(SQLITE_IOERR_SEEK(10 | (22<<8)), pFile->lastErrno,
43256 "winSeekFile", pFile->zPath);
43257 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
43258 return 1;
43259 }
43260
43261 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
43262 return 0;
43263#else
43264 /*
43265 ** Same as above, except that this implementation works for WinRT.
43266 */
43267
43268 LARGE_INTEGER x; /* The new offset */
43269 BOOL bRet; /* Value returned by SetFilePointerEx() */
43270
43271 x.QuadPart = iOffset;
43272 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
43273
43274 if(!bRet){
43275 pFile->lastErrno = osGetLastError();
43276 winLogError(SQLITE_IOERR_SEEK(10 | (22<<8)), pFile->lastErrno,
43277 "winSeekFile", pFile->zPath);
43278 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
43279 return 1;
43280 }
43281
43282 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
43283 return 0;
43284#endif
43285}
43286
43287#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
43288/* Forward references to VFS helper methods used for memory mapped files */
43289static int winMapfile(winFile*, sqlite3_int64);
43290static int winUnmapfile(winFile*);
43291#endif
43292
43293/*
43294** Close a file.
43295**
43296** It is reported that an attempt to close a handle might sometimes
43297** fail. This is a very unreasonable result, but Windows is notorious
43298** for being unreasonable so I do not doubt that it might happen. If
43299** the close fails, we pause for 100 milliseconds and try again. As
43300** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
43301** giving up and returning an error.
43302*/
43303#define MX_CLOSE_ATTEMPT 3
43304static int winClose(sqlite3_file *id){
43305 int rc, cnt = 0;
43306 winFile *pFile = (winFile*)id;
43307
43308 assert( id!=0 )((void) (0));
43309#ifndef SQLITE_OMIT_WAL
43310 assert( pFile->pShm==0 )((void) (0));
43311#endif
43312 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE )((void) (0));
43313 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
43314 osGetCurrentProcessId(), pFile, pFile->h));
43315
43316#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
43317 winUnmapfile(pFile);
43318#endif
43319
43320 do{
43321 rc = osCloseHandle(pFile->h);
43322 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
43323 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
43324#if SQLITE_OS_WINCE
43325#define WINCE_DELETION_ATTEMPTS 3
43326 {
43327 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
43328 if( pAppData==NULL((void*)0) || !pAppData->bNoLock ){
43329 winceDestroyLock(pFile);
43330 }
43331 }
43332 if( pFile->zDeleteOnClose ){
43333 int cnt = 0;
43334 while(
43335 osDeleteFileW(pFile->zDeleteOnClose)==0
43336 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
43337 && cnt++ < WINCE_DELETION_ATTEMPTS
43338 ){
43339 sqlite3_win32_sleep(100); /* Wait a little before trying again */
43340 }
43341 sqlite3_free(pFile->zDeleteOnClose);
43342 }
43343#endif
43344 if( rc ){
43345 pFile->h = NULL((void*)0);
43346 }
43347 OpenCounter(-1);
43348 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
43349 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
43350 return rc ? SQLITE_OK0
43351 : winLogError(SQLITE_IOERR_CLOSE(10 | (16<<8)), osGetLastError(),
43352 "winClose", pFile->zPath);
43353}
43354
43355/*
43356** Read data from a file into a buffer. Return SQLITE_OK if all
43357** bytes were read successfully and SQLITE_IOERR if anything goes
43358** wrong.
43359*/
43360static int winRead(
43361 sqlite3_file *id, /* File to read from */
43362 void *pBuf, /* Write content into this buffer */
43363 int amt, /* Number of bytes to read */
43364 sqlite3_int64 offset /* Begin reading at this offset */
43365){
43366#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43367 OVERLAPPED overlapped; /* The offset for ReadFile. */
43368#endif
43369 winFile *pFile = (winFile*)id; /* file handle */
43370 DWORD nRead; /* Number of bytes actually read from file */
43371 int nRetry = 0; /* Number of retrys */
43372
43373 assert( id!=0 )((void) (0));
43374 assert( amt>0 )((void) (0));
43375 assert( offset>=0 )((void) (0));
43376 SimulateIOError(return SQLITE_IOERR_READ);
43377 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
43378 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
43379 pFile->h, pBuf, amt, offset, pFile->locktype));
43380
43381#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
43382 /* Deal with as much of this read request as possible by transfering
43383 ** data from the memory mapping using memcpy(). */
43384 if( offset<pFile->mmapSize ){
43385 if( offset+amt <= pFile->mmapSize ){
43386 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
43387 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43388 osGetCurrentProcessId(), pFile, pFile->h));
43389 return SQLITE_OK0;
43390 }else{
43391 int nCopy = (int)(pFile->mmapSize - offset);
43392 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
43393 pBuf = &((u8 *)pBuf)[nCopy];
43394 amt -= nCopy;
43395 offset += nCopy;
43396 }
43397 }
43398#endif
43399
43400#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
43401 if( winSeekFile(pFile, offset) ){
43402 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
43403 osGetCurrentProcessId(), pFile, pFile->h));
43404 return SQLITE_FULL13;
43405 }
43406 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
43407#else
43408 memset(&overlapped, 0, sizeof(OVERLAPPED));
43409 overlapped.Offset = (LONG)(offset & 0xffffffff);
43410 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
43411 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
43412 osGetLastError()!=ERROR_HANDLE_EOF ){
43413#endif
43414 DWORD lastErrno;
43415 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
43416 pFile->lastErrno = lastErrno;
43417 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
43418 osGetCurrentProcessId(), pFile, pFile->h));
43419 return winLogError(SQLITE_IOERR_READ(10 | (1<<8)), pFile->lastErrno,
43420 "winRead", pFile->zPath);
43421 }
43422 winLogIoerr(nRetry, __LINE__43422);
43423 if( nRead<(DWORD)amt ){
43424 /* Unread parts of the buffer must be zero-filled */
43425 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
43426 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
43427 osGetCurrentProcessId(), pFile, pFile->h));
43428 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
43429 }
43430
43431 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43432 osGetCurrentProcessId(), pFile, pFile->h));
43433 return SQLITE_OK0;
43434}
43435
43436/*
43437** Write data from a buffer into a file. Return SQLITE_OK on success
43438** or some other error code on failure.
43439*/
43440static int winWrite(
43441 sqlite3_file *id, /* File to write into */
43442 const void *pBuf, /* The bytes to be written */
43443 int amt, /* Number of bytes to write */
43444 sqlite3_int64 offset /* Offset into the file to begin writing at */
43445){
43446 int rc = 0; /* True if error has occurred, else false */
43447 winFile *pFile = (winFile*)id; /* File handle */
43448 int nRetry = 0; /* Number of retries */
43449
43450 assert( amt>0 )((void) (0));
43451 assert( pFile )((void) (0));
43452 SimulateIOError(return SQLITE_IOERR_WRITE);
43453 SimulateDiskfullError(return SQLITE_FULL);
43454
43455 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
43456 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
43457 pFile->h, pBuf, amt, offset, pFile->locktype));
43458
43459#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE0x7fff0000>0
43460 /* Deal with as much of this write request as possible by transfering
43461 ** data from the memory mapping using memcpy(). */
43462 if( offset<pFile->mmapSize ){
43463 if( offset+amt <= pFile->mmapSize ){
43464 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
43465 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43466 osGetCurrentProcessId(), pFile, pFile->h));
43467 return SQLITE_OK0;
43468 }else{
43469 int nCopy = (int)(pFile->mmapSize - offset);
43470 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
43471 pBuf = &((u8 *)pBuf)[nCopy];
43472 amt -= nCopy;
43473 offset += nCopy;
43474 }
43475 }
43476#endif
43477
43478#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
43479 rc = winSeekFile(pFile, offset);
43480 if( rc==0 ){
43481#else
43482 {
43483#endif
43484#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43485 OVERLAPPED overlapped; /* The offset for WriteFile. */
43486#endif
43487 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
43488 int nRem = amt; /* Number of bytes yet to be written */
43489 DWORD nWrite; /* Bytes written by each WriteFile() call */
43490 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
43491
43492#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43493 memset(&overlapped, 0, sizeof(OVERLAPPED));
43494 overlapped.Offset = (LONG)(offset & 0xffffffff);
43495 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
43496#endif
43497
43498 while( nRem>0 ){
43499#if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
43500 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
43501#else
43502 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
43503#endif
43504 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
43505 break;
43506 }
43507 assert( nWrite==0 || nWrite<=(DWORD)nRem )((void) (0));
43508 if( nWrite==0 || nWrite>(DWORD)nRem ){
43509 lastErrno = osGetLastError();
43510 break;
43511 }
43512#if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
43513 offset += nWrite;
43514 overlapped.Offset = (LONG)(offset & 0xffffffff);
43515 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
43516#endif
43517 aRem += nWrite;
43518 nRem -= nWrite;
43519 }
43520 if( nRem>0 ){
43521 pFile->lastErrno = lastErrno;
43522 rc = 1;
43523 }
43524 }
43525
43526 if( rc ){
43527 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
43528 || ( pFile->lastErrno==ERROR_DISK_FULL )){
43529 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
43530 osGetCurrentProcessId(), pFile, pFile->h));
43531 return winLogError(SQLITE_FULL13, pFile->lastErrno,
43532 "winWrite1", pFile->zPath);
43533 }
43534 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
43535 osGetCurrentProcessId(), pFile, pFile->h));
43536 return winLogError(SQLITE_IOERR_WRITE(10 | (3<<8)), pFile->lastErrno,
43537 "winWrite2", pFile->zPath);
43538 }else{
43539 winLogIoerr(nRetry, __LINE__43539);
43540 }
43541 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43542 osGetCurrentProcessId(), pFile, pFile->h));
43543 return SQLITE_OK0;
43544}
43545
43546/*
43547** Truncate an open file to a specified size
43548*/
43549static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
43550 winFile *pFile = (winFile*)id; /* File handle object */
43551 int rc = SQLITE_OK0; /* Return code for this function */
43552 DWORD lastErrno;
43553#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
43554 sqlite3_int64 oldMmapSize;
43555 if( pFile->nFetchOut>0 ){
43556 /* File truncation is a no-op if there are outstanding memory mapped
43557 ** pages. This is because truncating the file means temporarily unmapping
43558 ** the file, and that might delete memory out from under existing cursors.
43559 **
43560 ** This can result in incremental vacuum not truncating the file,
43561 ** if there is an active read cursor when the incremental vacuum occurs.
43562 ** No real harm comes of this - the database file is not corrupted,
43563 ** though some folks might complain that the file is bigger than it
43564 ** needs to be.
43565 **
43566 ** The only feasible work-around is to defer the truncation until after
43567 ** all references to memory-mapped content are closed. That is doable,
43568 ** but involves adding a few branches in the common write code path which
43569 ** could slow down normal operations slightly. Hence, we have decided for
43570 ** now to simply make trancations a no-op if there are pending reads. We
43571 ** can maybe revisit this decision in the future.
43572 */
43573 return SQLITE_OK0;
43574 }
43575#endif
43576
43577 assert( pFile )((void) (0));
43578 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
43579 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
43580 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
43581
43582 /* If the user has configured a chunk-size for this file, truncate the
43583 ** file so that it consists of an integer number of chunks (i.e. the
43584 ** actual file size after the operation may be larger than the requested
43585 ** size).
43586 */
43587 if( pFile->szChunk>0 ){
43588 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
43589 }
43590
43591#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
43592 if( pFile->pMapRegion ){
43593 oldMmapSize = pFile->mmapSize;
43594 }else{
43595 oldMmapSize = 0;
43596 }
43597 winUnmapfile(pFile);
43598#endif
43599
43600 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
43601 if( winSeekFile(pFile, nByte) ){
43602 rc = winLogError(SQLITE_IOERR_TRUNCATE(10 | (6<<8)), pFile->lastErrno,
43603 "winTruncate1", pFile->zPath);
43604 }else if( 0==osSetEndOfFile(pFile->h) &&
43605 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
43606 pFile->lastErrno = lastErrno;
43607 rc = winLogError(SQLITE_IOERR_TRUNCATE(10 | (6<<8)), pFile->lastErrno,
43608 "winTruncate2", pFile->zPath);
43609 }
43610
43611#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
43612 if( rc==SQLITE_OK0 && oldMmapSize>0 ){
43613 if( oldMmapSize>nByte ){
43614 winMapfile(pFile, -1);
43615 }else{
43616 winMapfile(pFile, oldMmapSize);
43617 }
43618 }
43619#endif
43620
43621 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
43622 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
43623 return rc;
43624}
43625
43626#ifdef SQLITE_TEST
43627/*
43628** Count the number of fullsyncs and normal syncs. This is used to test
43629** that syncs and fullsyncs are occuring at the right times.
43630*/
43631SQLITE_API int sqlite3_sync_count = 0;
43632SQLITE_API int sqlite3_fullsync_count = 0;
43633#endif
43634
43635/*
43636** Make sure all writes to a particular file are committed to disk.
43637*/
43638static int winSync(sqlite3_file *id, int flags){
43639#ifndef SQLITE_NO_SYNC
43640 /*
43641 ** Used only when SQLITE_NO_SYNC is not defined.
43642 */
43643 BOOL rc;
43644#endif
43645#if !defined(NDEBUG1) || !defined(SQLITE_NO_SYNC) || \
43646 defined(SQLITE_HAVE_OS_TRACE)
43647 /*
43648 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
43649 ** OSTRACE() macros.
43650 */
43651 winFile *pFile = (winFile*)id;
43652#else
43653 UNUSED_PARAMETER(id)(void)(id);
43654#endif
43655
43656 assert( pFile )((void) (0));
43657 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
43658 assert((flags&0x0F)==SQLITE_SYNC_NORMAL((void) (0))
43659 || (flags&0x0F)==SQLITE_SYNC_FULL((void) (0))
43660 )((void) (0));
43661
43662 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
43663 ** line is to test that doing so does not cause any problems.
43664 */
43665 SimulateDiskfullError( return SQLITE_FULL );
43666
43667 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
43668 osGetCurrentProcessId(), pFile, pFile->h, flags,
43669 pFile->locktype));
43670
43671#ifndef SQLITE_TEST
43672 UNUSED_PARAMETER(flags)(void)(flags);
43673#else
43674 if( (flags&0x0F)==SQLITE_SYNC_FULL0x00003 ){
43675 sqlite3_fullsync_count++;
43676 }
43677 sqlite3_sync_count++;
43678#endif
43679
43680 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
43681 ** no-op
43682 */
43683#ifdef SQLITE_NO_SYNC
43684 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43685 osGetCurrentProcessId(), pFile, pFile->h));
43686 return SQLITE_OK0;
43687#else
43688#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
43689 if( pFile->pMapRegion ){
43690 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
43691 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
43692 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
43693 pFile, pFile->pMapRegion));
43694 }else{
43695 pFile->lastErrno = osGetLastError();
43696 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
43697 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
43698 pFile, pFile->pMapRegion));
43699 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
43700 "winSync1", pFile->zPath);
43701 }
43702 }
43703#endif
43704 rc = osFlushFileBuffers(pFile->h);
43705 SimulateIOError( rc=FALSE );
43706 if( rc ){
43707 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
43708 osGetCurrentProcessId(), pFile, pFile->h));
43709 return SQLITE_OK0;
43710 }else{
43711 pFile->lastErrno = osGetLastError();
43712 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
43713 osGetCurrentProcessId(), pFile, pFile->h));
43714 return winLogError(SQLITE_IOERR_FSYNC(10 | (4<<8)), pFile->lastErrno,
43715 "winSync2", pFile->zPath);
43716 }
43717#endif
43718}
43719
43720/*
43721** Determine the current size of a file in bytes
43722*/
43723static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
43724 winFile *pFile = (winFile*)id;
43725 int rc = SQLITE_OK0;
43726
43727 assert( id!=0 )((void) (0));
43728 assert( pSize!=0 )((void) (0));
43729 SimulateIOError(return SQLITE_IOERR_FSTAT);
43730 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
43731
43732#if SQLITE_OS_WINRT
43733 {
43734 FILE_STANDARD_INFO info;
43735 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
43736 &info, sizeof(info)) ){
43737 *pSize = info.EndOfFile.QuadPart;
43738 }else{
43739 pFile->lastErrno = osGetLastError();
43740 rc = winLogError(SQLITE_IOERR_FSTAT(10 | (7<<8)), pFile->lastErrno,
43741 "winFileSize", pFile->zPath);
43742 }
43743 }
43744#else
43745 {
43746 DWORD upperBits;
43747 DWORD lowerBits;
43748 DWORD lastErrno;
43749
43750 lowerBits = osGetFileSize(pFile->h, &upperBits);
43751 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
43752 if( (lowerBits == INVALID_FILE_SIZE)
43753 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
43754 pFile->lastErrno = lastErrno;
43755 rc = winLogError(SQLITE_IOERR_FSTAT(10 | (7<<8)), pFile->lastErrno,
43756 "winFileSize", pFile->zPath);
43757 }
43758 }
43759#endif
43760 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
43761 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
43762 return rc;
43763}
43764
43765/*
43766** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
43767*/
43768#ifndef LOCKFILE_FAIL_IMMEDIATELY
43769# define LOCKFILE_FAIL_IMMEDIATELY 1
43770#endif
43771
43772#ifndef LOCKFILE_EXCLUSIVE_LOCK
43773# define LOCKFILE_EXCLUSIVE_LOCK 2
43774#endif
43775
43776/*
43777** Historically, SQLite has used both the LockFile and LockFileEx functions.
43778** When the LockFile function was used, it was always expected to fail
43779** immediately if the lock could not be obtained. Also, it always expected to
43780** obtain an exclusive lock. These flags are used with the LockFileEx function
43781** and reflect those expectations; therefore, they should not be changed.
43782*/
43783#ifndef SQLITE_LOCKFILE_FLAGS
43784# define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
43785 LOCKFILE_EXCLUSIVE_LOCK)
43786#endif
43787
43788/*
43789** Currently, SQLite never calls the LockFileEx function without wanting the
43790** call to fail immediately if the lock cannot be obtained.
43791*/
43792#ifndef SQLITE_LOCKFILEEX_FLAGS
43793# define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
43794#endif
43795
43796/*
43797** Acquire a reader lock.
43798** Different API routines are called depending on whether or not this
43799** is Win9x or WinNT.
43800*/
43801static int winGetReadLock(winFile *pFile){
43802 int res;
43803 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
43804 if( osIsNT() ){
43805#if SQLITE_OS_WINCE
43806 /*
43807 ** NOTE: Windows CE is handled differently here due its lack of the Win32
43808 ** API LockFileEx.
43809 */
43810 res = winceLockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, 1, 0);
43811#else
43812 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST(sqlite3PendingByte+2), 0,
43813 SHARED_SIZE510, 0);
43814#endif
43815 }
43816#ifdef SQLITE_WIN32_HAS_ANSI
43817 else{
43818 int lk;
43819 sqlite3_randomness(sizeof(lk), &lk);
43820 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE510 - 1));
43821 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
43822 SHARED_FIRST(sqlite3PendingByte+2)+pFile->sharedLockByte, 0, 1, 0);
43823 }
43824#endif
43825 if( res == 0 ){
43826 pFile->lastErrno = osGetLastError();
43827 /* No need to log a failure to lock */
43828 }
43829 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
43830 return res;
43831}
43832
43833/*
43834** Undo a readlock
43835*/
43836static int winUnlockReadLock(winFile *pFile){
43837 int res;
43838 DWORD lastErrno;
43839 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
43840 if( osIsNT() ){
43841 res = winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, SHARED_SIZE510, 0);
43842 }
43843#ifdef SQLITE_WIN32_HAS_ANSI
43844 else{
43845 res = winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2)+pFile->sharedLockByte, 0, 1, 0);
43846 }
43847#endif
43848 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
43849 pFile->lastErrno = lastErrno;
43850 winLogError(SQLITE_IOERR_UNLOCK(10 | (8<<8)), pFile->lastErrno,
43851 "winUnlockReadLock", pFile->zPath);
43852 }
43853 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
43854 return res;
43855}
43856
43857/*
43858** Lock the file with the lock specified by parameter locktype - one
43859** of the following:
43860**
43861** (1) SHARED_LOCK
43862** (2) RESERVED_LOCK
43863** (3) PENDING_LOCK
43864** (4) EXCLUSIVE_LOCK
43865**
43866** Sometimes when requesting one lock state, additional lock states
43867** are inserted in between. The locking might fail on one of the later
43868** transitions leaving the lock state different from what it started but
43869** still short of its goal. The following chart shows the allowed
43870** transitions and the inserted intermediate states:
43871**
43872** UNLOCKED -> SHARED
43873** SHARED -> RESERVED
43874** SHARED -> (PENDING) -> EXCLUSIVE
43875** RESERVED -> (PENDING) -> EXCLUSIVE
43876** PENDING -> EXCLUSIVE
43877**
43878** This routine will only increase a lock. The winUnlock() routine
43879** erases all locks at once and returns us immediately to locking level 0.
43880** It is not possible to lower the locking level one step at a time. You
43881** must go straight to locking level 0.
43882*/
43883static int winLock(sqlite3_file *id, int locktype){
43884 int rc = SQLITE_OK0; /* Return code from subroutines */
43885 int res = 1; /* Result of a Windows lock call */
43886 int newLocktype; /* Set pFile->locktype to this value before exiting */
43887 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
43888 winFile *pFile = (winFile*)id;
43889 DWORD lastErrno = NO_ERROR;
43890
43891 assert( id!=0 )((void) (0));
43892 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
43893 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
43894
43895 /* If there is already a lock of this type or more restrictive on the
43896 ** OsFile, do nothing. Don't use the end_lock: exit path, as
43897 ** sqlite3OsEnterMutex() hasn't been called yet.
43898 */
43899 if( pFile->locktype>=locktype ){
43900 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
43901 return SQLITE_OK0;
43902 }
43903
43904 /* Do not allow any kind of write-lock on a read-only database
43905 */
43906 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK2 ){
43907 return SQLITE_IOERR_LOCK(10 | (15<<8));
43908 }
43909
43910 /* Make sure the locking sequence is correct
43911 */
43912 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK )((void) (0));
43913 assert( locktype!=PENDING_LOCK )((void) (0));
43914 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK )((void) (0));
43915
43916 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
43917 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
43918 ** the PENDING_LOCK byte is temporary.
43919 */
43920 newLocktype = pFile->locktype;
43921 if( pFile->locktype==NO_LOCK0
43922 || (locktype==EXCLUSIVE_LOCK4 && pFile->locktype<=RESERVED_LOCK2)
43923 ){
43924 int cnt = 3;
43925 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
43926 PENDING_BYTEsqlite3PendingByte, 0, 1, 0))==0 ){
43927 /* Try 3 times to get the pending lock. This is needed to work
43928 ** around problems caused by indexing and/or anti-virus software on
43929 ** Windows systems.
43930 ** If you are using this code as a model for alternative VFSes, do not
43931 ** copy this retry logic. It is a hack intended for Windows only.
43932 */
43933 lastErrno = osGetLastError();
43934 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
43935 pFile->h, cnt, res));
43936 if( lastErrno==ERROR_INVALID_HANDLE ){
43937 pFile->lastErrno = lastErrno;
43938 rc = SQLITE_IOERR_LOCK(10 | (15<<8));
43939 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
43940 pFile->h, cnt, sqlite3ErrName(rc)));
43941 return rc;
43942 }
43943 if( cnt ) sqlite3_win32_sleep(1);
43944 }
43945 gotPendingLock = res;
43946 if( !res ){
43947 lastErrno = osGetLastError();
43948 }
43949 }
43950
43951 /* Acquire a shared lock
43952 */
43953 if( locktype==SHARED_LOCK1 && res ){
43954 assert( pFile->locktype==NO_LOCK )((void) (0));
43955 res = winGetReadLock(pFile);
43956 if( res ){
43957 newLocktype = SHARED_LOCK1;
43958 }else{
43959 lastErrno = osGetLastError();
43960 }
43961 }
43962
43963 /* Acquire a RESERVED lock
43964 */
43965 if( locktype==RESERVED_LOCK2 && res ){
43966 assert( pFile->locktype==SHARED_LOCK )((void) (0));
43967 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
43968 if( res ){
43969 newLocktype = RESERVED_LOCK2;
43970 }else{
43971 lastErrno = osGetLastError();
43972 }
43973 }
43974
43975 /* Acquire a PENDING lock
43976 */
43977 if( locktype==EXCLUSIVE_LOCK4 && res ){
43978 newLocktype = PENDING_LOCK3;
43979 gotPendingLock = 0;
43980 }
43981
43982 /* Acquire an EXCLUSIVE lock
43983 */
43984 if( locktype==EXCLUSIVE_LOCK4 && res ){
43985 assert( pFile->locktype>=SHARED_LOCK )((void) (0));
43986 res = winUnlockReadLock(pFile);
43987 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST(sqlite3PendingByte+2), 0,
43988 SHARED_SIZE510, 0);
43989 if( res ){
43990 newLocktype = EXCLUSIVE_LOCK4;
43991 }else{
43992 lastErrno = osGetLastError();
43993 winGetReadLock(pFile);
43994 }
43995 }
43996
43997 /* If we are holding a PENDING lock that ought to be released, then
43998 ** release it now.
43999 */
44000 if( gotPendingLock && locktype==SHARED_LOCK1 ){
44001 winUnlockFile(&pFile->h, PENDING_BYTEsqlite3PendingByte, 0, 1, 0);
44002 }
44003
44004 /* Update the state of the lock has held in the file descriptor then
44005 ** return the appropriate result code.
44006 */
44007 if( res ){
44008 rc = SQLITE_OK0;
44009 }else{
44010 pFile->lastErrno = lastErrno;
44011 rc = SQLITE_BUSY5;
44012 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
44013 pFile->h, locktype, newLocktype));
44014 }
44015 pFile->locktype = (u8)newLocktype;
44016 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
44017 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
44018 return rc;
44019}
44020
44021/*
44022** This routine checks if there is a RESERVED lock held on the specified
44023** file by this or any other process. If such a lock is held, return
44024** non-zero, otherwise zero.
44025*/
44026static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
44027 int res;
44028 winFile *pFile = (winFile*)id;
44029
44030 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
44031 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
44032
44033 assert( id!=0 )((void) (0));
44034 if( pFile->locktype>=RESERVED_LOCK2 ){
44035 res = 1;
44036 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
44037 }else{
44038 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE(sqlite3PendingByte+1),0,1,0);
44039 if( res ){
44040 winUnlockFile(&pFile->h, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
44041 }
44042 res = !res;
44043 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
44044 }
44045 *pResOut = res;
44046 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
44047 pFile->h, pResOut, *pResOut));
44048 return SQLITE_OK0;
44049}
44050
44051/*
44052** Lower the locking level on file descriptor id to locktype. locktype
44053** must be either NO_LOCK or SHARED_LOCK.
44054**
44055** If the locking level of the file descriptor is already at or below
44056** the requested locking level, this routine is a no-op.
44057**
44058** It is not possible for this routine to fail if the second argument
44059** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
44060** might return SQLITE_IOERR;
44061*/
44062static int winUnlock(sqlite3_file *id, int locktype){
44063 int type;
44064 winFile *pFile = (winFile*)id;
44065 int rc = SQLITE_OK0;
44066 assert( pFile!=0 )((void) (0));
44067 assert( locktype<=SHARED_LOCK )((void) (0));
44068 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
44069 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
44070 type = pFile->locktype;
44071 if( type>=EXCLUSIVE_LOCK4 ){
44072 winUnlockFile(&pFile->h, SHARED_FIRST(sqlite3PendingByte+2), 0, SHARED_SIZE510, 0);
44073 if( locktype==SHARED_LOCK1 && !winGetReadLock(pFile) ){
44074 /* This should never happen. We should always be able to
44075 ** reacquire the read lock */
44076 rc = winLogError(SQLITE_IOERR_UNLOCK(10 | (8<<8)), osGetLastError(),
44077 "winUnlock", pFile->zPath);
44078 }
44079 }
44080 if( type>=RESERVED_LOCK2 ){
44081 winUnlockFile(&pFile->h, RESERVED_BYTE(sqlite3PendingByte+1), 0, 1, 0);
44082 }
44083 if( locktype==NO_LOCK0 && type>=SHARED_LOCK1 ){
44084 winUnlockReadLock(pFile);
44085 }
44086 if( type>=PENDING_LOCK3 ){
44087 winUnlockFile(&pFile->h, PENDING_BYTEsqlite3PendingByte, 0, 1, 0);
44088 }
44089 pFile->locktype = (u8)locktype;
44090 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
44091 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
44092 return rc;
44093}
44094
44095/******************************************************************************
44096****************************** No-op Locking **********************************
44097**
44098** Of the various locking implementations available, this is by far the
44099** simplest: locking is ignored. No attempt is made to lock the database
44100** file for reading or writing.
44101**
44102** This locking mode is appropriate for use on read-only databases
44103** (ex: databases that are burned into CD-ROM, for example.) It can
44104** also be used if the application employs some external mechanism to
44105** prevent simultaneous access of the same database by two or more
44106** database connections. But there is a serious risk of database
44107** corruption if this locking mode is used in situations where multiple
44108** database connections are accessing the same database file at the same
44109** time and one or more of those connections are writing.
44110*/
44111
44112static int winNolockLock(sqlite3_file *id, int locktype){
44113 UNUSED_PARAMETER(id)(void)(id);
44114 UNUSED_PARAMETER(locktype)(void)(locktype);
44115 return SQLITE_OK0;
44116}
44117
44118static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
44119 UNUSED_PARAMETER(id)(void)(id);
44120 UNUSED_PARAMETER(pResOut)(void)(pResOut);
44121 return SQLITE_OK0;
44122}
44123
44124static int winNolockUnlock(sqlite3_file *id, int locktype){
44125 UNUSED_PARAMETER(id)(void)(id);
44126 UNUSED_PARAMETER(locktype)(void)(locktype);
44127 return SQLITE_OK0;
44128}
44129
44130/******************* End of the no-op lock implementation *********************
44131******************************************************************************/
44132
44133/*
44134** If *pArg is initially negative then this is a query. Set *pArg to
44135** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
44136**
44137** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
44138*/
44139static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
44140 if( *pArg<0 ){
44141 *pArg = (pFile->ctrlFlags & mask)!=0;
44142 }else if( (*pArg)==0 ){
44143 pFile->ctrlFlags &= ~mask;
44144 }else{
44145 pFile->ctrlFlags |= mask;
44146 }
44147}
44148
44149/* Forward references to VFS helper methods used for temporary files */
44150static int winGetTempname(sqlite3_vfs *, char **);
44151static int winIsDir(const void *);
44152static BOOL winIsDriveLetterAndColon(const char *);
44153
44154/*
44155** Control and query of the open file handle.
44156*/
44157static int winFileControl(sqlite3_file *id, int op, void *pArg){
44158 winFile *pFile = (winFile*)id;
44159 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
44160 switch( op ){
44161 case SQLITE_FCNTL_LOCKSTATE1: {
44162 *(int*)pArg = pFile->locktype;
44163 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
44164 return SQLITE_OK0;
44165 }
44166 case SQLITE_FCNTL_LAST_ERRNO4: {
44167 *(int*)pArg = (int)pFile->lastErrno;
44168 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
44169 return SQLITE_OK0;
44170 }
44171 case SQLITE_FCNTL_CHUNK_SIZE6: {
44172 pFile->szChunk = *(int *)pArg;
44173 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
44174 return SQLITE_OK0;
44175 }
44176 case SQLITE_FCNTL_SIZE_HINT5: {
44177 if( pFile->szChunk>0 ){
44178 sqlite3_int64 oldSz;
44179 int rc = winFileSize(id, &oldSz);
44180 if( rc==SQLITE_OK0 ){
44181 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
44182 if( newSz>oldSz ){
44183 SimulateIOErrorBenign(1);
44184 rc = winTruncate(id, newSz);
44185 SimulateIOErrorBenign(0);
44186 }
44187 }
44188 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
44189 return rc;
44190 }
44191 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
44192 return SQLITE_OK0;
44193 }
44194 case SQLITE_FCNTL_PERSIST_WAL10: {
44195 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
44196 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
44197 return SQLITE_OK0;
44198 }
44199 case SQLITE_FCNTL_POWERSAFE_OVERWRITE13: {
44200 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
44201 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
44202 return SQLITE_OK0;
44203 }
44204 case SQLITE_FCNTL_VFSNAME12: {
44205 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
44206 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
44207 return SQLITE_OK0;
44208 }
44209 case SQLITE_FCNTL_WIN32_AV_RETRY9: {
44210 int *a = (int*)pArg;
44211 if( a[0]>0 ){
44212 winIoerrRetry = a[0];
44213 }else{
44214 a[0] = winIoerrRetry;
44215 }
44216 if( a[1]>0 ){
44217 winIoerrRetryDelay = a[1];
44218 }else{
44219 a[1] = winIoerrRetryDelay;
44220 }
44221 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
44222 return SQLITE_OK0;
44223 }
44224 case SQLITE_FCNTL_WIN32_GET_HANDLE29: {
44225 LPHANDLE phFile = (LPHANDLE)pArg;
44226 *phFile = pFile->h;
44227 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
44228 return SQLITE_OK0;
44229 }
44230#ifdef SQLITE_TEST
44231 case SQLITE_FCNTL_WIN32_SET_HANDLE23: {
44232 LPHANDLE phFile = (LPHANDLE)pArg;
44233 HANDLE hOldFile = pFile->h;
44234 pFile->h = *phFile;
44235 *phFile = hOldFile;
44236 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
44237 hOldFile, pFile->h));
44238 return SQLITE_OK0;
44239 }
44240#endif
44241 case SQLITE_FCNTL_TEMPFILENAME16: {
44242 char *zTFile = 0;
44243 int rc = winGetTempname(pFile->pVfs, &zTFile);
44244 if( rc==SQLITE_OK0 ){
44245 *(char**)pArg = zTFile;
44246 }
44247 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
44248 return rc;
44249 }
44250#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
44251 case SQLITE_FCNTL_MMAP_SIZE18: {
44252 i64 newLimit = *(i64*)pArg;
44253 int rc = SQLITE_OK0;
44254 if( newLimit>sqlite3GlobalConfigsqlite3Config.mxMmap ){
44255 newLimit = sqlite3GlobalConfigsqlite3Config.mxMmap;
44256 }
44257
44258 /* The value of newLimit may be eventually cast to (SIZE_T) and passed
44259 ** to MapViewOfFile(). Restrict its value to 2GB if (SIZE_T) is not at
44260 ** least a 64-bit type. */
44261 if( newLimit>0 && sizeof(SIZE_T)<8 ){
44262 newLimit = (newLimit & 0x7FFFFFFF);
44263 }
44264
44265 *(i64*)pArg = pFile->mmapSizeMax;
44266 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
44267 pFile->mmapSizeMax = newLimit;
44268 if( pFile->mmapSize>0 ){
44269 winUnmapfile(pFile);
44270 rc = winMapfile(pFile, -1);
44271 }
44272 }
44273 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
44274 return rc;
44275 }
44276#endif
44277 }
44278 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
44279 return SQLITE_NOTFOUND12;
44280}
44281
44282/*
44283** Return the sector size in bytes of the underlying block device for
44284** the specified file. This is almost always 512 bytes, but may be
44285** larger for some devices.
44286**
44287** SQLite code assumes this function cannot fail. It also assumes that
44288** if two files are created in the same file-system directory (i.e.
44289** a database and its journal file) that the sector size will be the
44290** same for both.
44291*/
44292static int winSectorSize(sqlite3_file *id){
44293 (void)id;
44294 return SQLITE_DEFAULT_SECTOR_SIZE4096;
44295}
44296
44297/*
44298** Return a vector of device characteristics.
44299*/
44300static int winDeviceCharacteristics(sqlite3_file *id){
44301 winFile *p = (winFile*)id;
44302 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800 |
44303 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000:0);
44304}
44305
44306/*
44307** Windows will only let you create file view mappings
44308** on allocation size granularity boundaries.
44309** During sqlite3_os_init() we do a GetSystemInfo()
44310** to get the granularity size.
44311*/
44312static SYSTEM_INFO winSysInfo;
44313
44314#ifndef SQLITE_OMIT_WAL
44315
44316/*
44317** Helper functions to obtain and relinquish the global mutex. The
44318** global mutex is used to protect the winLockInfo objects used by
44319** this file, all of which may be shared by multiple threads.
44320**
44321** Function winShmMutexHeld() is used to assert() that the global mutex
44322** is held when required. This function is only used as part of assert()
44323** statements. e.g.
44324**
44325** winShmEnterMutex()
44326** assert( winShmMutexHeld() );
44327** winShmLeaveMutex()
44328*/
44329static sqlite3_mutex *winBigLock = 0;
44330static void winShmEnterMutex(void){
44331 sqlite3_mutex_enter(winBigLock);
44332}
44333static void winShmLeaveMutex(void){
44334 sqlite3_mutex_leave(winBigLock);
44335}
44336#ifndef NDEBUG1
44337static int winShmMutexHeld(void) {
44338 return sqlite3_mutex_held(winBigLock);
44339}
44340#endif
44341
44342/*
44343** Object used to represent a single file opened and mmapped to provide
44344** shared memory. When multiple threads all reference the same
44345** log-summary, each thread has its own winFile object, but they all
44346** point to a single instance of this object. In other words, each
44347** log-summary is opened only once per process.
44348**
44349** winShmMutexHeld() must be true when creating or destroying
44350** this object or while reading or writing the following fields:
44351**
44352** nRef
44353** pNext
44354**
44355** The following fields are read-only after the object is created:
44356**
44357** fid
44358** zFilename
44359**
44360** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
44361** winShmMutexHeld() is true when reading or writing any other field
44362** in this structure.
44363**
44364*/
44365struct winShmNode {
44366 sqlite3_mutex *mutex; /* Mutex to access this object */
44367 char *zFilename; /* Name of the file */
44368 winFile hFile; /* File handle from winOpen */
44369
44370 int szRegion; /* Size of shared-memory regions */
44371 int nRegion; /* Size of array apRegion */
44372 u8 isReadonly; /* True if read-only */
44373 u8 isUnlocked; /* True if no DMS lock held */
44374
44375 struct ShmRegion {
44376 HANDLE hMap; /* File handle from CreateFileMapping */
44377 void *pMap;
44378 } *aRegion;
44379 DWORD lastErrno; /* The Windows errno from the last I/O error */
44380
44381 int nRef; /* Number of winShm objects pointing to this */
44382 winShm *pFirst; /* All winShm objects pointing to this */
44383 winShmNode *pNext; /* Next in list of all winShmNode objects */
44384#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
44385 u8 nextShmId; /* Next available winShm.id value */
44386#endif
44387};
44388
44389/*
44390** A global array of all winShmNode objects.
44391**
44392** The winShmMutexHeld() must be true while reading or writing this list.
44393*/
44394static winShmNode *winShmNodeList = 0;
44395
44396/*
44397** Structure used internally by this VFS to record the state of an
44398** open shared memory connection.
44399**
44400** The following fields are initialized when this object is created and
44401** are read-only thereafter:
44402**
44403** winShm.pShmNode
44404** winShm.id
44405**
44406** All other fields are read/write. The winShm.pShmNode->mutex must be held
44407** while accessing any read/write fields.
44408*/
44409struct winShm {
44410 winShmNode *pShmNode; /* The underlying winShmNode object */
44411 winShm *pNext; /* Next winShm with the same winShmNode */
44412 u8 hasMutex; /* True if holding the winShmNode mutex */
44413 u16 sharedMask; /* Mask of shared locks held */
44414 u16 exclMask; /* Mask of exclusive locks held */
44415#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
44416 u8 id; /* Id of this connection with its winShmNode */
44417#endif
44418};
44419
44420/*
44421** Constants used for locking
44422*/
44423#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK8)*4) /* first lock byte */
44424#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK8) /* deadman switch */
44425
44426/*
44427** Apply advisory locks for all n bytes beginning at ofst.
44428*/
44429#define WINSHM_UNLCK 1
44430#define WINSHM_RDLCK 2
44431#define WINSHM_WRLCK 3
44432static int winShmSystemLock(
44433 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
44434 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
44435 int ofst, /* Offset to first byte to be locked/unlocked */
44436 int nByte /* Number of bytes to lock or unlock */
44437){
44438 int rc = 0; /* Result code form Lock/UnlockFileEx() */
44439
44440 /* Access to the winShmNode object is serialized by the caller */
44441 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) )((void) (0));
44442
44443 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
44444 pFile->hFile.h, lockType, ofst, nByte));
44445
44446 /* Release/Acquire the system-level lock */
44447 if( lockType==WINSHM_UNLCK ){
44448 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
44449 }else{
44450 /* Initialize the locking parameters */
44451 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
44452 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
44453 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
44454 }
44455
44456 if( rc!= 0 ){
44457 rc = SQLITE_OK0;
44458 }else{
44459 pFile->lastErrno = osGetLastError();
44460 rc = SQLITE_BUSY5;
44461 }
44462
44463 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
44464 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
44465 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
44466
44467 return rc;
44468}
44469
44470/* Forward references to VFS methods */
44471static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
44472static int winDelete(sqlite3_vfs *,const char*,int);
44473
44474/*
44475** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
44476**
44477** This is not a VFS shared-memory method; it is a utility function called
44478** by VFS shared-memory methods.
44479*/
44480static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
44481 winShmNode **pp;
44482 winShmNode *p;
44483 assert( winShmMutexHeld() )((void) (0));
44484 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
44485 osGetCurrentProcessId(), deleteFlag));
44486 pp = &winShmNodeList;
44487 while( (p = *pp)!=0 ){
44488 if( p->nRef==0 ){
44489 int i;
44490 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
44491 for(i=0; i<p->nRegion; i++){
44492 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
44493 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
44494 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
44495 UNUSED_VARIABLE_VALUE(bRc);
44496 bRc = osCloseHandle(p->aRegion[i].hMap);
44497 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
44498 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
44499 UNUSED_VARIABLE_VALUE(bRc);
44500 }
44501 if( p->hFile.h!=NULL((void*)0) && p->hFile.h!=INVALID_HANDLE_VALUE ){
44502 SimulateIOErrorBenign(1);
44503 winClose((sqlite3_file *)&p->hFile);
44504 SimulateIOErrorBenign(0);
44505 }
44506 if( deleteFlag ){
44507 SimulateIOErrorBenign(1);
44508 sqlite3BeginBenignMalloc();
44509 winDelete(pVfs, p->zFilename, 0);
44510 sqlite3EndBenignMalloc();
44511 SimulateIOErrorBenign(0);
44512 }
44513 *pp = p->pNext;
44514 sqlite3_free(p->aRegion);
44515 sqlite3_free(p);
44516 }else{
44517 pp = &p->pNext;
44518 }
44519 }
44520}
44521
44522/*
44523** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
44524** take it now. Return SQLITE_OK if successful, or an SQLite error
44525** code otherwise.
44526**
44527** If the DMS cannot be locked because this is a readonly_shm=1
44528** connection and no other process already holds a lock, return
44529** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
44530*/
44531static int winLockSharedMemory(winShmNode *pShmNode){
44532 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
44533
44534 if( rc==SQLITE_OK0 ){
44535 if( pShmNode->isReadonly ){
44536 pShmNode->isUnlocked = 1;
44537 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44538 return SQLITE_READONLY_CANTINIT(8 | (5<<8));
44539 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
44540 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44541 return winLogError(SQLITE_IOERR_SHMOPEN(10 | (18<<8)), osGetLastError(),
44542 "winLockSharedMemory", pShmNode->zFilename);
44543 }
44544 }
44545
44546 if( rc==SQLITE_OK0 ){
44547 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44548 }
44549
44550 return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
44551}
44552
44553/*
44554** Open the shared-memory area associated with database file pDbFd.
44555**
44556** When opening a new shared-memory file, if no other instances of that
44557** file are currently open, in this process or in other processes, then
44558** the file must be truncated to zero length or have its header cleared.
44559*/
44560static int winOpenSharedMemory(winFile *pDbFd){
44561 struct winShm *p; /* The connection to be opened */
44562 winShmNode *pShmNode = 0; /* The underlying mmapped file */
44563 int rc = SQLITE_OK0; /* Result code */
44564 winShmNode *pNew; /* Newly allocated winShmNode */
44565 int nName; /* Size of zName in bytes */
44566
44567 assert( pDbFd->pShm==0 )((void) (0)); /* Not previously opened */
44568
44569 /* Allocate space for the new sqlite3_shm object. Also speculatively
44570 ** allocate space for a new winShmNode and filename.
44571 */
44572 p = sqlite3MallocZero( sizeof(*p) );
44573 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
44574 nName = sqlite3Strlen30(pDbFd->zPath);
44575 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
44576 if( pNew==0 ){
44577 sqlite3_free(p);
44578 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
44579 }
44580 pNew->zFilename = (char*)&pNew[1];
44581 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
44582 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
44583
44584 /* Look to see if there is an existing winShmNode that can be used.
44585 ** If no matching winShmNode currently exists, create a new one.
44586 */
44587 winShmEnterMutex();
44588 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
44589 /* TBD need to come up with better match here. Perhaps
44590 ** use FILE_ID_BOTH_DIR_INFO Structure.
44591 */
44592 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
44593 }
44594 if( pShmNode ){
44595 sqlite3_free(pNew);
44596 }else{
44597 int inFlags = SQLITE_OPEN_WAL0x00080000;
44598 int outFlags = 0;
44599
44600 pShmNode = pNew;
44601 pNew = 0;
44602 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
44603 pShmNode->pNext = winShmNodeList;
44604 winShmNodeList = pShmNode;
44605
44606 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
44607 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST0);
44608 if( pShmNode->mutex==0 ){
44609 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
44610 goto shm_open_err;
44611 }
44612 }
44613
44614 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
44615 inFlags |= SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004;
44616 }else{
44617 inFlags |= SQLITE_OPEN_READONLY0x00000001;
44618 }
44619 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
44620 (sqlite3_file*)&pShmNode->hFile,
44621 inFlags, &outFlags);
44622 if( rc!=SQLITE_OK0 ){
44623 rc = winLogError(rc, osGetLastError(), "winOpenShm",
44624 pShmNode->zFilename);
44625 goto shm_open_err;
44626 }
44627 if( outFlags==SQLITE_OPEN_READONLY0x00000001 ) pShmNode->isReadonly = 1;
44628
44629 rc = winLockSharedMemory(pShmNode);
44630 if( rc!=SQLITE_OK0 && rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ) goto shm_open_err;
44631 }
44632
44633 /* Make the new connection a child of the winShmNode */
44634 p->pShmNode = pShmNode;
44635#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
44636 p->id = pShmNode->nextShmId++;
44637#endif
44638 pShmNode->nRef++;
44639 pDbFd->pShm = p;
44640 winShmLeaveMutex();
44641
44642 /* The reference count on pShmNode has already been incremented under
44643 ** the cover of the winShmEnterMutex() mutex and the pointer from the
44644 ** new (struct winShm) object to the pShmNode has been set. All that is
44645 ** left to do is to link the new object into the linked list starting
44646 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
44647 ** mutex.
44648 */
44649 sqlite3_mutex_enter(pShmNode->mutex);
44650 p->pNext = pShmNode->pFirst;
44651 pShmNode->pFirst = p;
44652 sqlite3_mutex_leave(pShmNode->mutex);
44653 return rc;
44654
44655 /* Jump here on any error */
44656shm_open_err:
44657 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
44658 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
44659 sqlite3_free(p);
44660 sqlite3_free(pNew);
44661 winShmLeaveMutex();
44662 return rc;
44663}
44664
44665/*
44666** Close a connection to shared-memory. Delete the underlying
44667** storage if deleteFlag is true.
44668*/
44669static int winShmUnmap(
44670 sqlite3_file *fd, /* Database holding shared memory */
44671 int deleteFlag /* Delete after closing if true */
44672){
44673 winFile *pDbFd; /* Database holding shared-memory */
44674 winShm *p; /* The connection to be closed */
44675 winShmNode *pShmNode; /* The underlying shared-memory file */
44676 winShm **pp; /* For looping over sibling connections */
44677
44678 pDbFd = (winFile*)fd;
44679 p = pDbFd->pShm;
44680 if( p==0 ) return SQLITE_OK0;
44681 pShmNode = p->pShmNode;
44682
44683 /* Remove connection p from the set of connections associated
44684 ** with pShmNode */
44685 sqlite3_mutex_enter(pShmNode->mutex);
44686 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
44687 *pp = p->pNext;
44688
44689 /* Free the connection p */
44690 sqlite3_free(p);
44691 pDbFd->pShm = 0;
44692 sqlite3_mutex_leave(pShmNode->mutex);
44693
44694 /* If pShmNode->nRef has reached 0, then close the underlying
44695 ** shared-memory file, too */
44696 winShmEnterMutex();
44697 assert( pShmNode->nRef>0 )((void) (0));
44698 pShmNode->nRef--;
44699 if( pShmNode->nRef==0 ){
44700 winShmPurge(pDbFd->pVfs, deleteFlag);
44701 }
44702 winShmLeaveMutex();
44703
44704 return SQLITE_OK0;
44705}
44706
44707/*
44708** Change the lock state for a shared-memory segment.
44709*/
44710static int winShmLock(
44711 sqlite3_file *fd, /* Database file holding the shared memory */
44712 int ofst, /* First lock to acquire or release */
44713 int n, /* Number of locks to acquire or release */
44714 int flags /* What to do with the lock */
44715){
44716 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
44717 winShm *p = pDbFd->pShm; /* The shared memory being locked */
44718 winShm *pX; /* For looping over all siblings */
44719 winShmNode *pShmNode = p->pShmNode;
44720 int rc = SQLITE_OK0; /* Result code */
44721 u16 mask; /* Mask of locks to take or release */
44722
44723 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK )((void) (0));
44724 assert( n>=1 )((void) (0));
44725 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)((void) (0))
44726 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)((void) (0))
44727 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)((void) (0))
44728 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) )((void) (0));
44729 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 )((void) (0));
44730
44731 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
44732 assert( n>1 || mask==(1<<ofst) )((void) (0));
44733 sqlite3_mutex_enter(pShmNode->mutex);
44734 if( flags & SQLITE_SHM_UNLOCK1 ){
44735 u16 allMask = 0; /* Mask of locks held by siblings */
44736
44737 /* See if any siblings hold this same lock */
44738 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
44739 if( pX==p ) continue;
44740 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 )((void) (0));
44741 allMask |= pX->sharedMask;
44742 }
44743
44744 /* Unlock the system-level locks */
44745 if( (mask & allMask)==0 ){
44746 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
44747 }else{
44748 rc = SQLITE_OK0;
44749 }
44750
44751 /* Undo the local locks */
44752 if( rc==SQLITE_OK0 ){
44753 p->exclMask &= ~mask;
44754 p->sharedMask &= ~mask;
44755 }
44756 }else if( flags & SQLITE_SHM_SHARED4 ){
44757 u16 allShared = 0; /* Union of locks held by connections other than "p" */
44758
44759 /* Find out which shared locks are already held by sibling connections.
44760 ** If any sibling already holds an exclusive lock, go ahead and return
44761 ** SQLITE_BUSY.
44762 */
44763 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
44764 if( (pX->exclMask & mask)!=0 ){
44765 rc = SQLITE_BUSY5;
44766 break;
44767 }
44768 allShared |= pX->sharedMask;
44769 }
44770
44771 /* Get shared locks at the system level, if necessary */
44772 if( rc==SQLITE_OK0 ){
44773 if( (allShared & mask)==0 ){
44774 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
44775 }else{
44776 rc = SQLITE_OK0;
44777 }
44778 }
44779
44780 /* Get the local shared locks */
44781 if( rc==SQLITE_OK0 ){
44782 p->sharedMask |= mask;
44783 }
44784 }else{
44785 /* Make sure no sibling connections hold locks that will block this
44786 ** lock. If any do, return SQLITE_BUSY right away.
44787 */
44788 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
44789 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
44790 rc = SQLITE_BUSY5;
44791 break;
44792 }
44793 }
44794
44795 /* Get the exclusive locks at the system level. Then if successful
44796 ** also mark the local connection as being locked.
44797 */
44798 if( rc==SQLITE_OK0 ){
44799 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
44800 if( rc==SQLITE_OK0 ){
44801 assert( (p->sharedMask & mask)==0 )((void) (0));
44802 p->exclMask |= mask;
44803 }
44804 }
44805 }
44806 sqlite3_mutex_leave(pShmNode->mutex);
44807 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
44808 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
44809 sqlite3ErrName(rc)));
44810 return rc;
44811}
44812
44813/*
44814** Implement a memory barrier or memory fence on shared memory.
44815**
44816** All loads and stores begun before the barrier must complete before
44817** any load or store begun after the barrier.
44818*/
44819static void winShmBarrier(
44820 sqlite3_file *fd /* Database holding the shared memory */
44821){
44822 UNUSED_PARAMETER(fd)(void)(fd);
44823 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
44824 winShmEnterMutex(); /* Also mutex, for redundancy */
44825 winShmLeaveMutex();
44826}
44827
44828/*
44829** This function is called to obtain a pointer to region iRegion of the
44830** shared-memory associated with the database file fd. Shared-memory regions
44831** are numbered starting from zero. Each shared-memory region is szRegion
44832** bytes in size.
44833**
44834** If an error occurs, an error code is returned and *pp is set to NULL.
44835**
44836** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
44837** region has not been allocated (by any client, including one running in a
44838** separate process), then *pp is set to NULL and SQLITE_OK returned. If
44839** isWrite is non-zero and the requested shared-memory region has not yet
44840** been allocated, it is allocated by this function.
44841**
44842** If the shared-memory region has already been allocated or is allocated by
44843** this call as described above, then it is mapped into this processes
44844** address space (if it is not already), *pp is set to point to the mapped
44845** memory and SQLITE_OK returned.
44846*/
44847static int winShmMap(
44848 sqlite3_file *fd, /* Handle open on database file */
44849 int iRegion, /* Region to retrieve */
44850 int szRegion, /* Size of regions */
44851 int isWrite, /* True to extend file if necessary */
44852 void volatile **pp /* OUT: Mapped memory */
44853){
44854 winFile *pDbFd = (winFile*)fd;
44855 winShm *pShm = pDbFd->pShm;
44856 winShmNode *pShmNode;
44857 DWORD protect = PAGE_READWRITE;
44858 DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
44859 int rc = SQLITE_OK0;
44860
44861 if( !pShm ){
44862 rc = winOpenSharedMemory(pDbFd);
44863 if( rc!=SQLITE_OK0 ) return rc;
44864 pShm = pDbFd->pShm;
44865 }
44866 pShmNode = pShm->pShmNode;
44867
44868 sqlite3_mutex_enter(pShmNode->mutex);
44869 if( pShmNode->isUnlocked ){
44870 rc = winLockSharedMemory(pShmNode);
44871 if( rc!=SQLITE_OK0 ) goto shmpage_out;
44872 pShmNode->isUnlocked = 0;
44873 }
44874 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 )((void) (0));
44875
44876 if( pShmNode->nRegion<=iRegion ){
44877 struct ShmRegion *apNew; /* New aRegion[] array */
44878 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
44879 sqlite3_int64 sz; /* Current size of wal-index file */
44880
44881 pShmNode->szRegion = szRegion;
44882
44883 /* The requested region is not mapped into this processes address space.
44884 ** Check to see if it has been allocated (i.e. if the wal-index file is
44885 ** large enough to contain the requested region).
44886 */
44887 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
44888 if( rc!=SQLITE_OK0 ){
44889 rc = winLogError(SQLITE_IOERR_SHMSIZE(10 | (19<<8)), osGetLastError(),
44890 "winShmMap1", pDbFd->zPath);
44891 goto shmpage_out;
44892 }
44893
44894 if( sz<nByte ){
44895 /* The requested memory region does not exist. If isWrite is set to
44896 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
44897 **
44898 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
44899 ** the requested memory region.
44900 */
44901 if( !isWrite ) goto shmpage_out;
44902 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
44903 if( rc!=SQLITE_OK0 ){
44904 rc = winLogError(SQLITE_IOERR_SHMSIZE(10 | (19<<8)), osGetLastError(),
44905 "winShmMap2", pDbFd->zPath);
44906 goto shmpage_out;
44907 }
44908 }
44909
44910 /* Map the requested memory region into this processes address space. */
44911 apNew = (struct ShmRegion *)sqlite3_realloc64(
44912 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
44913 );
44914 if( !apNew ){
44915 rc = SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
44916 goto shmpage_out;
44917 }
44918 pShmNode->aRegion = apNew;
44919
44920 if( pShmNode->isReadonly ){
44921 protect = PAGE_READONLY;
44922 flags = FILE_MAP_READ;
44923 }
44924
44925 while( pShmNode->nRegion<=iRegion ){
44926 HANDLE hMap = NULL((void*)0); /* file-mapping handle */
44927 void *pMap = 0; /* Mapped memory region */
44928
44929#if SQLITE_OS_WINRT
44930 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
44931 NULL((void*)0), protect, nByte, NULL((void*)0)
44932 );
44933#elif defined(SQLITE_WIN32_HAS_WIDE)
44934 hMap = osCreateFileMappingW(pShmNode->hFile.h,
44935 NULL((void*)0), protect, 0, nByte, NULL((void*)0)
44936 );
44937#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
44938 hMap = osCreateFileMappingA(pShmNode->hFile.h,
44939 NULL((void*)0), protect, 0, nByte, NULL((void*)0)
44940 );
44941#endif
44942 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
44943 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
44944 hMap ? "ok" : "failed"));
44945 if( hMap ){
44946 int iOffset = pShmNode->nRegion*szRegion;
44947 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
44948#if SQLITE_OS_WINRT
44949 pMap = osMapViewOfFileFromApp(hMap, flags,
44950 iOffset - iOffsetShift, szRegion + iOffsetShift
44951 );
44952#else
44953 pMap = osMapViewOfFile(hMap, flags,
44954 0, iOffset - iOffsetShift, szRegion + iOffsetShift
44955 );
44956#endif
44957 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
44958 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
44959 szRegion, pMap ? "ok" : "failed"));
44960 }
44961 if( !pMap ){
44962 pShmNode->lastErrno = osGetLastError();
44963 rc = winLogError(SQLITE_IOERR_SHMMAP(10 | (21<<8)), pShmNode->lastErrno,
44964 "winShmMap3", pDbFd->zPath);
44965 if( hMap ) osCloseHandle(hMap);
44966 goto shmpage_out;
44967 }
44968
44969 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
44970 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
44971 pShmNode->nRegion++;
44972 }
44973 }
44974
44975shmpage_out:
44976 if( pShmNode->nRegion>iRegion ){
44977 int iOffset = iRegion*szRegion;
44978 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
44979 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
44980 *pp = (void *)&p[iOffsetShift];
44981 }else{
44982 *pp = 0;
44983 }
44984 if( pShmNode->isReadonly && rc==SQLITE_OK0 ) rc = SQLITE_READONLY8;
44985 sqlite3_mutex_leave(pShmNode->mutex);
44986 return rc;
44987}
44988
44989#else
44990# define winShmMap 0
44991# define winShmLock 0
44992# define winShmBarrier 0
44993# define winShmUnmap 0
44994#endif /* #ifndef SQLITE_OMIT_WAL */
44995
44996/*
44997** Cleans up the mapped region of the specified file, if any.
44998*/
44999#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
45000static int winUnmapfile(winFile *pFile){
45001 assert( pFile!=0 )((void) (0));
45002 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
45003 "mmapSize=%lld, mmapSizeMax=%lld\n",
45004 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
45005 pFile->mmapSize, pFile->mmapSizeMax));
45006 if( pFile->pMapRegion ){
45007 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
45008 pFile->lastErrno = osGetLastError();
45009 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
45010 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
45011 pFile->pMapRegion));
45012 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
45013 "winUnmapfile1", pFile->zPath);
45014 }
45015 pFile->pMapRegion = 0;
45016 pFile->mmapSize = 0;
45017 }
45018 if( pFile->hMap!=NULL((void*)0) ){
45019 if( !osCloseHandle(pFile->hMap) ){
45020 pFile->lastErrno = osGetLastError();
45021 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
45022 osGetCurrentProcessId(), pFile, pFile->hMap));
45023 return winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFile->lastErrno,
45024 "winUnmapfile2", pFile->zPath);
45025 }
45026 pFile->hMap = NULL((void*)0);
45027 }
45028 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
45029 osGetCurrentProcessId(), pFile));
45030 return SQLITE_OK0;
45031}
45032
45033/*
45034** Memory map or remap the file opened by file-descriptor pFd (if the file
45035** is already mapped, the existing mapping is replaced by the new). Or, if
45036** there already exists a mapping for this file, and there are still
45037** outstanding xFetch() references to it, this function is a no-op.
45038**
45039** If parameter nByte is non-negative, then it is the requested size of
45040** the mapping to create. Otherwise, if nByte is less than zero, then the
45041** requested size is the size of the file on disk. The actual size of the
45042** created mapping is either the requested size or the value configured
45043** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
45044**
45045** SQLITE_OK is returned if no error occurs (even if the mapping is not
45046** recreated as a result of outstanding references) or an SQLite error
45047** code otherwise.
45048*/
45049static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
45050 sqlite3_int64 nMap = nByte;
45051 int rc;
45052
45053 assert( nMap>=0 || pFd->nFetchOut==0 )((void) (0));
45054 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
45055 osGetCurrentProcessId(), pFd, nByte));
45056
45057 if( pFd->nFetchOut>0 ) return SQLITE_OK0;
45058
45059 if( nMap<0 ){
45060 rc = winFileSize((sqlite3_file*)pFd, &nMap);
45061 if( rc ){
45062 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
45063 osGetCurrentProcessId(), pFd));
45064 return SQLITE_IOERR_FSTAT(10 | (7<<8));
45065 }
45066 }
45067 if( nMap>pFd->mmapSizeMax ){
45068 nMap = pFd->mmapSizeMax;
45069 }
45070 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
45071
45072 if( nMap==0 && pFd->mmapSize>0 ){
45073 winUnmapfile(pFd);
45074 }
45075 if( nMap!=pFd->mmapSize ){
45076 void *pNew = 0;
45077 DWORD protect = PAGE_READONLY;
45078 DWORD flags = FILE_MAP_READ;
45079
45080 winUnmapfile(pFd);
45081#ifdef SQLITE_MMAP_READWRITE
45082 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
45083 protect = PAGE_READWRITE;
45084 flags |= FILE_MAP_WRITE;
45085 }
45086#endif
45087#if SQLITE_OS_WINRT
45088 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL((void*)0), protect, nMap, NULL((void*)0));
45089#elif defined(SQLITE_WIN32_HAS_WIDE)
45090 pFd->hMap = osCreateFileMappingW(pFd->h, NULL((void*)0), protect,
45091 (DWORD)((nMap>>32) & 0xffffffff),
45092 (DWORD)(nMap & 0xffffffff), NULL((void*)0));
45093#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
45094 pFd->hMap = osCreateFileMappingA(pFd->h, NULL((void*)0), protect,
45095 (DWORD)((nMap>>32) & 0xffffffff),
45096 (DWORD)(nMap & 0xffffffff), NULL((void*)0));
45097#endif
45098 if( pFd->hMap==NULL((void*)0) ){
45099 pFd->lastErrno = osGetLastError();
45100 rc = winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFd->lastErrno,
45101 "winMapfile1", pFd->zPath);
45102 /* Log the error, but continue normal operation using xRead/xWrite */
45103 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
45104 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
45105 return SQLITE_OK0;
45106 }
45107 assert( (nMap % winSysInfo.dwPageSize)==0 )((void) (0));
45108 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff )((void) (0));
45109#if SQLITE_OS_WINRT
45110 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
45111#else
45112 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
45113#endif
45114 if( pNew==NULL((void*)0) ){
45115 osCloseHandle(pFd->hMap);
45116 pFd->hMap = NULL((void*)0);
45117 pFd->lastErrno = osGetLastError();
45118 rc = winLogError(SQLITE_IOERR_MMAP(10 | (24<<8)), pFd->lastErrno,
45119 "winMapfile2", pFd->zPath);
45120 /* Log the error, but continue normal operation using xRead/xWrite */
45121 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
45122 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
45123 return SQLITE_OK0;
45124 }
45125 pFd->pMapRegion = pNew;
45126 pFd->mmapSize = nMap;
45127 }
45128
45129 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
45130 osGetCurrentProcessId(), pFd));
45131 return SQLITE_OK0;
45132}
45133#endif /* SQLITE_MAX_MMAP_SIZE>0 */
45134
45135/*
45136** If possible, return a pointer to a mapping of file fd starting at offset
45137** iOff. The mapping must be valid for at least nAmt bytes.
45138**
45139** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
45140** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
45141** Finally, if an error does occur, return an SQLite error code. The final
45142** value of *pp is undefined in this case.
45143**
45144** If this function does return a pointer, the caller must eventually
45145** release the reference by calling winUnfetch().
45146*/
45147static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
45148#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
45149 winFile *pFd = (winFile*)fd; /* The underlying database file */
45150#endif
45151 *pp = 0;
45152
45153 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
45154 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
45155
45156#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
45157 if( pFd->mmapSizeMax>0 ){
45158 if( pFd->pMapRegion==0 ){
45159 int rc = winMapfile(pFd, -1);
45160 if( rc!=SQLITE_OK0 ){
45161 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
45162 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
45163 return rc;
45164 }
45165 }
45166 if( pFd->mmapSize >= iOff+nAmt ){
45167 *pp = &((u8 *)pFd->pMapRegion)[iOff];
45168 pFd->nFetchOut++;
45169 }
45170 }
45171#endif
45172
45173 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
45174 osGetCurrentProcessId(), fd, pp, *pp));
45175 return SQLITE_OK0;
45176}
45177
45178/*
45179** If the third argument is non-NULL, then this function releases a
45180** reference obtained by an earlier call to winFetch(). The second
45181** argument passed to this function must be the same as the corresponding
45182** argument that was passed to the winFetch() invocation.
45183**
45184** Or, if the third argument is NULL, then this function is being called
45185** to inform the VFS layer that, according to POSIX, any existing mapping
45186** may now be invalid and should be unmapped.
45187*/
45188static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
45189#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
45190 winFile *pFd = (winFile*)fd; /* The underlying database file */
45191
45192 /* If p==0 (unmap the entire file) then there must be no outstanding
45193 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
45194 ** then there must be at least one outstanding. */
45195 assert( (p==0)==(pFd->nFetchOut==0) )((void) (0));
45196
45197 /* If p!=0, it must match the iOff value. */
45198 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] )((void) (0));
45199
45200 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
45201 osGetCurrentProcessId(), pFd, iOff, p));
45202
45203 if( p ){
45204 pFd->nFetchOut--;
45205 }else{
45206 /* FIXME: If Windows truly always prevents truncating or deleting a
45207 ** file while a mapping is held, then the following winUnmapfile() call
45208 ** is unnecessary can be omitted - potentially improving
45209 ** performance. */
45210 winUnmapfile(pFd);
45211 }
45212
45213 assert( pFd->nFetchOut>=0 )((void) (0));
45214#endif
45215
45216 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
45217 osGetCurrentProcessId(), fd));
45218 return SQLITE_OK0;
45219}
45220
45221/*
45222** Here ends the implementation of all sqlite3_file methods.
45223**
45224********************** End sqlite3_file Methods *******************************
45225******************************************************************************/
45226
45227/*
45228** This vector defines all the methods that can operate on an
45229** sqlite3_file for win32.
45230*/
45231static const sqlite3_io_methods winIoMethod = {
45232 3, /* iVersion */
45233 winClose, /* xClose */
45234 winRead, /* xRead */
45235 winWrite, /* xWrite */
45236 winTruncate, /* xTruncate */
45237 winSync, /* xSync */
45238 winFileSize, /* xFileSize */
45239 winLock, /* xLock */
45240 winUnlock, /* xUnlock */
45241 winCheckReservedLock, /* xCheckReservedLock */
45242 winFileControl, /* xFileControl */
45243 winSectorSize, /* xSectorSize */
45244 winDeviceCharacteristics, /* xDeviceCharacteristics */
45245 winShmMap, /* xShmMap */
45246 winShmLock, /* xShmLock */
45247 winShmBarrier, /* xShmBarrier */
45248 winShmUnmap, /* xShmUnmap */
45249 winFetch, /* xFetch */
45250 winUnfetch /* xUnfetch */
45251};
45252
45253/*
45254** This vector defines all the methods that can operate on an
45255** sqlite3_file for win32 without performing any locking.
45256*/
45257static const sqlite3_io_methods winIoNolockMethod = {
45258 3, /* iVersion */
45259 winClose, /* xClose */
45260 winRead, /* xRead */
45261 winWrite, /* xWrite */
45262 winTruncate, /* xTruncate */
45263 winSync, /* xSync */
45264 winFileSize, /* xFileSize */
45265 winNolockLock, /* xLock */
45266 winNolockUnlock, /* xUnlock */
45267 winNolockCheckReservedLock, /* xCheckReservedLock */
45268 winFileControl, /* xFileControl */
45269 winSectorSize, /* xSectorSize */
45270 winDeviceCharacteristics, /* xDeviceCharacteristics */
45271 winShmMap, /* xShmMap */
45272 winShmLock, /* xShmLock */
45273 winShmBarrier, /* xShmBarrier */
45274 winShmUnmap, /* xShmUnmap */
45275 winFetch, /* xFetch */
45276 winUnfetch /* xUnfetch */
45277};
45278
45279static winVfsAppData winAppData = {
45280 &winIoMethod, /* pMethod */
45281 0, /* pAppData */
45282 0 /* bNoLock */
45283};
45284
45285static winVfsAppData winNolockAppData = {
45286 &winIoNolockMethod, /* pMethod */
45287 0, /* pAppData */
45288 1 /* bNoLock */
45289};
45290
45291/****************************************************************************
45292**************************** sqlite3_vfs methods ****************************
45293**
45294** This division contains the implementation of methods on the
45295** sqlite3_vfs object.
45296*/
45297
45298#if defined(__CYGWIN__)
45299/*
45300** Convert a filename from whatever the underlying operating system
45301** supports for filenames into UTF-8. Space to hold the result is
45302** obtained from malloc and must be freed by the calling function.
45303*/
45304static char *winConvertToUtf8Filename(const void *zFilename){
45305 char *zConverted = 0;
45306 if( osIsNT() ){
45307 zConverted = winUnicodeToUtf8(zFilename);
45308 }
45309#ifdef SQLITE_WIN32_HAS_ANSI
45310 else{
45311 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
45312 }
45313#endif
45314 /* caller will handle out of memory */
45315 return zConverted;
45316}
45317#endif
45318
45319/*
45320** Convert a UTF-8 filename into whatever form the underlying
45321** operating system wants filenames in. Space to hold the result
45322** is obtained from malloc and must be freed by the calling
45323** function.
45324*/
45325static void *winConvertFromUtf8Filename(const char *zFilename){
45326 void *zConverted = 0;
45327 if( osIsNT() ){
45328 zConverted = winUtf8ToUnicode(zFilename);
45329 }
45330#ifdef SQLITE_WIN32_HAS_ANSI
45331 else{
45332 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
45333 }
45334#endif
45335 /* caller will handle out of memory */
45336 return zConverted;
45337}
45338
45339/*
45340** This function returns non-zero if the specified UTF-8 string buffer
45341** ends with a directory separator character or one was successfully
45342** added to it.
45343*/
45344static int winMakeEndInDirSep(int nBuf, char *zBuf){
45345 if( zBuf ){
45346 int nLen = sqlite3Strlen30(zBuf);
45347 if( nLen>0 ){
45348 if( winIsDirSep(zBuf[nLen-1]) ){
45349 return 1;
45350 }else if( nLen+1<nBuf ){
45351 zBuf[nLen] = winGetDirSep();
45352 zBuf[nLen+1] = '\0';
45353 return 1;
45354 }
45355 }
45356 }
45357 return 0;
45358}
45359
45360/*
45361** Create a temporary file name and store the resulting pointer into pzBuf.
45362** The pointer returned in pzBuf must be freed via sqlite3_free().
45363*/
45364static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
45365 static char zChars[] =
45366 "abcdefghijklmnopqrstuvwxyz"
45367 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45368 "0123456789";
45369 size_t i, j;
45370 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX"etilqs_");
45371 int nMax, nBuf, nDir, nLen;
45372 char *zBuf;
45373
45374 /* It's odd to simulate an io-error here, but really this is just
45375 ** using the io-error infrastructure to test that SQLite handles this
45376 ** function failing.
45377 */
45378 SimulateIOError( return SQLITE_IOERR );
45379
45380 /* Allocate a temporary buffer to store the fully qualified file
45381 ** name for the temporary file. If this fails, we cannot continue.
45382 */
45383 nMax = pVfs->mxPathname; nBuf = nMax + 2;
45384 zBuf = sqlite3MallocZero( nBuf );
45385 if( !zBuf ){
45386 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45387 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45388 }
45389
45390 /* Figure out the effective temporary directory. First, check if one
45391 ** has been explicitly set by the application; otherwise, use the one
45392 ** configured by the operating system.
45393 */
45394 nDir = nMax - (nPre + 15);
45395 assert( nDir>0 )((void) (0));
45396 if( sqlite3_temp_directory ){
45397 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
45398 if( nDirLen>0 ){
45399 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
45400 nDirLen++;
45401 }
45402 if( nDirLen>nDir ){
45403 sqlite3_free(zBuf);
45404 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
45405 return winLogError(SQLITE_ERROR1, 0, "winGetTempname1", 0);
45406 }
45407 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
45408 }
45409 }
45410#if defined(__CYGWIN__)
45411 else{
45412 static const char *azDirs[] = {
45413 0, /* getenv("SQLITE_TMPDIR") */
45414 0, /* getenv("TMPDIR") */
45415 0, /* getenv("TMP") */
45416 0, /* getenv("TEMP") */
45417 0, /* getenv("USERPROFILE") */
45418 "/var/tmp",
45419 "/usr/tmp",
45420 "/tmp",
45421 ".",
45422 0 /* List terminator */
45423 };
45424 unsigned int i;
45425 const char *zDir = 0;
45426
45427 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
45428 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
45429 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
45430 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
45431 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
45432 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
45433 void *zConverted;
45434 if( zDir==0 ) continue;
45435 /* If the path starts with a drive letter followed by the colon
45436 ** character, assume it is already a native Win32 path; otherwise,
45437 ** it must be converted to a native Win32 path via the Cygwin API
45438 ** prior to using it.
45439 */
45440 if( winIsDriveLetterAndColon(zDir) ){
45441 zConverted = winConvertFromUtf8Filename(zDir);
45442 if( !zConverted ){
45443 sqlite3_free(zBuf);
45444 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45445 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45446 }
45447 if( winIsDir(zConverted) ){
45448 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
45449 sqlite3_free(zConverted);
45450 break;
45451 }
45452 sqlite3_free(zConverted);
45453 }else{
45454 zConverted = sqlite3MallocZero( nMax+1 );
45455 if( !zConverted ){
45456 sqlite3_free(zBuf);
45457 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45458 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45459 }
45460 if( cygwin_conv_path(
45461 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
45462 zConverted, nMax+1)<0 ){
45463 sqlite3_free(zConverted);
45464 sqlite3_free(zBuf);
45465 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
45466 return winLogError(SQLITE_IOERR_CONVPATH(10 | (26<<8)), (DWORD)errno(*__errno_location ()),
45467 "winGetTempname2", zDir);
45468 }
45469 if( winIsDir(zConverted) ){
45470 /* At this point, we know the candidate directory exists and should
45471 ** be used. However, we may need to convert the string containing
45472 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
45473 */
45474 char *zUtf8 = winConvertToUtf8Filename(zConverted);
45475 if( !zUtf8 ){
45476 sqlite3_free(zConverted);
45477 sqlite3_free(zBuf);
45478 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45479 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45480 }
45481 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
45482 sqlite3_free(zUtf8);
45483 sqlite3_free(zConverted);
45484 break;
45485 }
45486 sqlite3_free(zConverted);
45487 }
45488 }
45489 }
45490#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
45491 else if( osIsNT() ){
45492 char *zMulti;
45493 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
45494 if( !zWidePath ){
45495 sqlite3_free(zBuf);
45496 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45497 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45498 }
45499 if( osGetTempPathW(nMax, zWidePath)==0 ){
45500 sqlite3_free(zWidePath);
45501 sqlite3_free(zBuf);
45502 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
45503 return winLogError(SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)), osGetLastError(),
45504 "winGetTempname2", 0);
45505 }
45506 zMulti = winUnicodeToUtf8(zWidePath);
45507 if( zMulti ){
45508 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
45509 sqlite3_free(zMulti);
45510 sqlite3_free(zWidePath);
45511 }else{
45512 sqlite3_free(zWidePath);
45513 sqlite3_free(zBuf);
45514 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45515 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45516 }
45517 }
45518#ifdef SQLITE_WIN32_HAS_ANSI
45519 else{
45520 char *zUtf8;
45521 char *zMbcsPath = sqlite3MallocZero( nMax );
45522 if( !zMbcsPath ){
45523 sqlite3_free(zBuf);
45524 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45525 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45526 }
45527 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
45528 sqlite3_free(zBuf);
45529 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
45530 return winLogError(SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)), osGetLastError(),
45531 "winGetTempname3", 0);
45532 }
45533 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
45534 if( zUtf8 ){
45535 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
45536 sqlite3_free(zUtf8);
45537 }else{
45538 sqlite3_free(zBuf);
45539 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
45540 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45541 }
45542 }
45543#endif /* SQLITE_WIN32_HAS_ANSI */
45544#endif /* !SQLITE_OS_WINRT */
45545
45546 /*
45547 ** Check to make sure the temporary directory ends with an appropriate
45548 ** separator. If it does not and there is not enough space left to add
45549 ** one, fail.
45550 */
45551 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
45552 sqlite3_free(zBuf);
45553 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
45554 return winLogError(SQLITE_ERROR1, 0, "winGetTempname4", 0);
45555 }
45556
45557 /*
45558 ** Check that the output buffer is large enough for the temporary file
45559 ** name in the following format:
45560 **
45561 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
45562 **
45563 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
45564 ** account for the space used by the 15 character random suffix and the
45565 ** two trailing NUL characters. The final directory separator character
45566 ** has already added if it was not already present.
45567 */
45568 nLen = sqlite3Strlen30(zBuf);
45569 if( (nLen + nPre + 17) > nBuf ){
45570 sqlite3_free(zBuf);
45571 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
45572 return winLogError(SQLITE_ERROR1, 0, "winGetTempname5", 0);
45573 }
45574
45575 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX"etilqs_");
45576
45577 j = sqlite3Strlen30(zBuf);
45578 sqlite3_randomness(15, &zBuf[j]);
45579 for(i=0; i<15; i++, j++){
45580 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
45581 }
45582 zBuf[j] = 0;
45583 zBuf[j+1] = 0;
45584 *pzBuf = zBuf;
45585
45586 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
45587 return SQLITE_OK0;
45588}
45589
45590/*
45591** Return TRUE if the named file is really a directory. Return false if
45592** it is something other than a directory, or if there is any kind of memory
45593** allocation failure.
45594*/
45595static int winIsDir(const void *zConverted){
45596 DWORD attr;
45597 int rc = 0;
45598 DWORD lastErrno;
45599
45600 if( osIsNT() ){
45601 int cnt = 0;
45602 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
45603 memset(&sAttrData, 0, sizeof(sAttrData));
45604 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
45605 GetFileExInfoStandard,
45606 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
45607 if( !rc ){
45608 return 0; /* Invalid name? */
45609 }
45610 attr = sAttrData.dwFileAttributes;
45611#if SQLITE_OS_WINCE==0
45612 }else{
45613 attr = osGetFileAttributesA((char*)zConverted);
45614#endif
45615 }
45616 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
45617}
45618
45619/* forward reference */
45620static int winAccess(
45621 sqlite3_vfs *pVfs, /* Not used on win32 */
45622 const char *zFilename, /* Name of file to check */
45623 int flags, /* Type of test to make on this file */
45624 int *pResOut /* OUT: Result */
45625);
45626
45627/*
45628** Open a file.
45629*/
45630static int winOpen(
45631 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
45632 const char *zName, /* Name of the file (UTF-8) */
45633 sqlite3_file *id, /* Write the SQLite file handle here */
45634 int flags, /* Open mode flags */
45635 int *pOutFlags /* Status return flags */
45636){
45637 HANDLE h;
45638 DWORD lastErrno = 0;
45639 DWORD dwDesiredAccess;
45640 DWORD dwShareMode;
45641 DWORD dwCreationDisposition;
45642 DWORD dwFlagsAndAttributes = 0;
45643#if SQLITE_OS_WINCE
45644 int isTemp = 0;
45645#endif
45646 winVfsAppData *pAppData;
45647 winFile *pFile = (winFile*)id;
45648 void *zConverted; /* Filename in OS encoding */
45649 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
45650 int cnt = 0;
45651
45652 /* If argument zPath is a NULL pointer, this function is required to open
45653 ** a temporary file. Use this buffer to store the file name in.
45654 */
45655 char *zTmpname = 0; /* For temporary filename, if necessary. */
45656
45657 int rc = SQLITE_OK0; /* Function Return Code */
45658#if !defined(NDEBUG1) || SQLITE_OS_WINCE
45659 int eType = flags&0xFFFFFF00; /* Type of file to open */
45660#endif
45661
45662 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE0x00000010);
45663 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE0x00000008);
45664 int isCreate = (flags & SQLITE_OPEN_CREATE0x00000004);
45665 int isReadonly = (flags & SQLITE_OPEN_READONLY0x00000001);
45666 int isReadWrite = (flags & SQLITE_OPEN_READWRITE0x00000002);
45667
45668#ifndef NDEBUG1
45669 int isOpenJournal = (isCreate && (
45670 eType==SQLITE_OPEN_MASTER_JOURNAL0x00004000
45671 || eType==SQLITE_OPEN_MAIN_JOURNAL0x00000800
45672 || eType==SQLITE_OPEN_WAL0x00080000
45673 ));
45674#endif
45675
45676 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
45677 zUtf8Name, id, flags, pOutFlags));
45678
45679 /* Check the following statements are true:
45680 **
45681 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
45682 ** (b) if CREATE is set, then READWRITE must also be set, and
45683 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
45684 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
45685 */
45686 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly))((void) (0));
45687 assert(isCreate==0 || isReadWrite)((void) (0));
45688 assert(isExclusive==0 || isCreate)((void) (0));
45689 assert(isDelete==0 || isCreate)((void) (0));
45690
45691 /* The main DB, main journal, WAL file and master journal are never
45692 ** automatically deleted. Nor are they ever temporary files. */
45693 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB )((void) (0));
45694 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL )((void) (0));
45695 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL )((void) (0));
45696 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL )((void) (0));
45697
45698 /* Assert that the upper layer has set one of the "file-type" flags. */
45699 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB((void) (0))
45700 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL((void) (0))
45701 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL((void) (0))
45702 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL((void) (0))
45703 )((void) (0));
45704
45705 assert( pFile!=0 )((void) (0));
45706 memset(pFile, 0, sizeof(winFile));
45707 pFile->h = INVALID_HANDLE_VALUE;
45708
45709#if SQLITE_OS_WINRT
45710 if( !zUtf8Name && !sqlite3_temp_directory ){
45711 sqlite3_log(SQLITE_ERROR1,
45712 "sqlite3_temp_directory variable should be set for WinRT");
45713 }
45714#endif
45715
45716 /* If the second argument to this function is NULL, generate a
45717 ** temporary file name to use
45718 */
45719 if( !zUtf8Name ){
45720 assert( isDelete && !isOpenJournal )((void) (0));
45721 rc = winGetTempname(pVfs, &zTmpname);
45722 if( rc!=SQLITE_OK0 ){
45723 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
45724 return rc;
45725 }
45726 zUtf8Name = zTmpname;
45727 }
45728
45729 /* Database filenames are double-zero terminated if they are not
45730 ** URIs with parameters. Hence, they can always be passed into
45731 ** sqlite3_uri_parameter().
45732 */
45733 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||((void) (0))
45734 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 )((void) (0));
45735
45736 /* Convert the filename to the system encoding. */
45737 zConverted = winConvertFromUtf8Filename(zUtf8Name);
45738 if( zConverted==0 ){
45739 sqlite3_free(zTmpname);
45740 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
45741 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45742 }
45743
45744 if( winIsDir(zConverted) ){
45745 sqlite3_free(zConverted);
45746 sqlite3_free(zTmpname);
45747 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
45748 return SQLITE_CANTOPEN_ISDIR(14 | (2<<8));
45749 }
45750
45751 if( isReadWrite ){
45752 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
45753 }else{
45754 dwDesiredAccess = GENERIC_READ;
45755 }
45756
45757 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
45758 ** created. SQLite doesn't use it to indicate "exclusive access"
45759 ** as it is usually understood.
45760 */
45761 if( isExclusive ){
45762 /* Creates a new file, only if it does not already exist. */
45763 /* If the file exists, it fails. */
45764 dwCreationDisposition = CREATE_NEW;
45765 }else if( isCreate ){
45766 /* Open existing file, or create if it doesn't exist */
45767 dwCreationDisposition = OPEN_ALWAYS;
45768 }else{
45769 /* Opens a file, only if it exists. */
45770 dwCreationDisposition = OPEN_EXISTING;
45771 }
45772
45773 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
45774
45775 if( isDelete ){
45776#if SQLITE_OS_WINCE
45777 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
45778 isTemp = 1;
45779#else
45780 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
45781 | FILE_ATTRIBUTE_HIDDEN
45782 | FILE_FLAG_DELETE_ON_CLOSE;
45783#endif
45784 }else{
45785 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
45786 }
45787 /* Reports from the internet are that performance is always
45788 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
45789#if SQLITE_OS_WINCE
45790 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
45791#endif
45792
45793 if( osIsNT() ){
45794#if SQLITE_OS_WINRT
45795 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
45796 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
45797 extendedParameters.dwFileAttributes =
45798 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
45799 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
45800 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
45801 extendedParameters.lpSecurityAttributes = NULL((void*)0);
45802 extendedParameters.hTemplateFile = NULL((void*)0);
45803 do{
45804 h = osCreateFile2((LPCWSTR)zConverted,
45805 dwDesiredAccess,
45806 dwShareMode,
45807 dwCreationDisposition,
45808 &extendedParameters);
45809 if( h!=INVALID_HANDLE_VALUE ) break;
45810 if( isReadWrite ){
45811 int rc2, isRO = 0;
45812 sqlite3BeginBenignMalloc();
45813 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ2, &isRO);
45814 sqlite3EndBenignMalloc();
45815 if( rc2==SQLITE_OK0 && isRO ) break;
45816 }
45817 }while( winRetryIoerr(&cnt, &lastErrno) );
45818#else
45819 do{
45820 h = osCreateFileW((LPCWSTR)zConverted,
45821 dwDesiredAccess,
45822 dwShareMode, NULL((void*)0),
45823 dwCreationDisposition,
45824 dwFlagsAndAttributes,
45825 NULL((void*)0));
45826 if( h!=INVALID_HANDLE_VALUE ) break;
45827 if( isReadWrite ){
45828 int rc2, isRO = 0;
45829 sqlite3BeginBenignMalloc();
45830 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ2, &isRO);
45831 sqlite3EndBenignMalloc();
45832 if( rc2==SQLITE_OK0 && isRO ) break;
45833 }
45834 }while( winRetryIoerr(&cnt, &lastErrno) );
45835#endif
45836 }
45837#ifdef SQLITE_WIN32_HAS_ANSI
45838 else{
45839 do{
45840 h = osCreateFileA((LPCSTR)zConverted,
45841 dwDesiredAccess,
45842 dwShareMode, NULL((void*)0),
45843 dwCreationDisposition,
45844 dwFlagsAndAttributes,
45845 NULL((void*)0));
45846 if( h!=INVALID_HANDLE_VALUE ) break;
45847 if( isReadWrite ){
45848 int rc2, isRO = 0;
45849 sqlite3BeginBenignMalloc();
45850 rc2 = winAccess(pVfs, zName, SQLITE_ACCESS_READ2, &isRO);
45851 sqlite3EndBenignMalloc();
45852 if( rc2==SQLITE_OK0 && isRO ) break;
45853 }
45854 }while( winRetryIoerr(&cnt, &lastErrno) );
45855 }
45856#endif
45857 winLogIoerr(cnt, __LINE__45857);
45858
45859 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
45860 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
45861
45862 if( h==INVALID_HANDLE_VALUE ){
45863 sqlite3_free(zConverted);
45864 sqlite3_free(zTmpname);
45865 if( isReadWrite && !isExclusive ){
45866 return winOpen(pVfs, zName, id,
45867 ((flags|SQLITE_OPEN_READONLY0x00000001) &
45868 ~(SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_READWRITE0x00000002)),
45869 pOutFlags);
45870 }else{
45871 pFile->lastErrno = lastErrno;
45872 winLogError(SQLITE_CANTOPEN14, pFile->lastErrno, "winOpen", zUtf8Name);
45873 return SQLITE_CANTOPEN_BKPTsqlite3CantopenError(45873);
45874 }
45875 }
45876
45877 if( pOutFlags ){
45878 if( isReadWrite ){
45879 *pOutFlags = SQLITE_OPEN_READWRITE0x00000002;
45880 }else{
45881 *pOutFlags = SQLITE_OPEN_READONLY0x00000001;
45882 }
45883 }
45884
45885 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
45886 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
45887 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
45888
45889 pAppData = (winVfsAppData*)pVfs->pAppData;
45890
45891#if SQLITE_OS_WINCE
45892 {
45893 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB0x00000100
45894 && ((pAppData==NULL((void*)0)) || !pAppData->bNoLock)
45895 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK0
45896 ){
45897 osCloseHandle(h);
45898 sqlite3_free(zConverted);
45899 sqlite3_free(zTmpname);
45900 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
45901 return rc;
45902 }
45903 }
45904 if( isTemp ){
45905 pFile->zDeleteOnClose = zConverted;
45906 }else
45907#endif
45908 {
45909 sqlite3_free(zConverted);
45910 }
45911
45912 sqlite3_free(zTmpname);
45913 pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
45914 pFile->pVfs = pVfs;
45915 pFile->h = h;
45916 if( isReadonly ){
45917 pFile->ctrlFlags |= WINFILE_RDONLY;
45918 }
45919 if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE1) ){
45920 pFile->ctrlFlags |= WINFILE_PSOW;
45921 }
45922 pFile->lastErrno = NO_ERROR;
45923 pFile->zPath = zName;
45924#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
45925 pFile->hMap = NULL((void*)0);
45926 pFile->pMapRegion = 0;
45927 pFile->mmapSize = 0;
45928 pFile->mmapSizeMax = sqlite3GlobalConfigsqlite3Config.szMmap;
45929#endif
45930
45931 OpenCounter(+1);
45932 return rc;
45933}
45934
45935/*
45936** Delete the named file.
45937**
45938** Note that Windows does not allow a file to be deleted if some other
45939** process has it open. Sometimes a virus scanner or indexing program
45940** will open a journal file shortly after it is created in order to do
45941** whatever it does. While this other process is holding the
45942** file open, we will be unable to delete it. To work around this
45943** problem, we delay 100 milliseconds and try to delete again. Up
45944** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
45945** up and returning an error.
45946*/
45947static int winDelete(
45948 sqlite3_vfs *pVfs, /* Not used on win32 */
45949 const char *zFilename, /* Name of file to delete */
45950 int syncDir /* Not used on win32 */
45951){
45952 int cnt = 0;
45953 int rc;
45954 DWORD attr;
45955 DWORD lastErrno = 0;
45956 void *zConverted;
45957 UNUSED_PARAMETER(pVfs)(void)(pVfs);
45958 UNUSED_PARAMETER(syncDir)(void)(syncDir);
45959
45960 SimulateIOError(return SQLITE_IOERR_DELETE);
45961 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
45962
45963 zConverted = winConvertFromUtf8Filename(zFilename);
45964 if( zConverted==0 ){
45965 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
45966 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
45967 }
45968 if( osIsNT() ){
45969 do {
45970#if SQLITE_OS_WINRT
45971 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
45972 memset(&sAttrData, 0, sizeof(sAttrData));
45973 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
45974 &sAttrData) ){
45975 attr = sAttrData.dwFileAttributes;
45976 }else{
45977 lastErrno = osGetLastError();
45978 if( lastErrno==ERROR_FILE_NOT_FOUND
45979 || lastErrno==ERROR_PATH_NOT_FOUND ){
45980 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
45981 }else{
45982 rc = SQLITE_ERROR1;
45983 }
45984 break;
45985 }
45986#else
45987 attr = osGetFileAttributesW(zConverted);
45988#endif
45989 if ( attr==INVALID_FILE_ATTRIBUTES ){
45990 lastErrno = osGetLastError();
45991 if( lastErrno==ERROR_FILE_NOT_FOUND
45992 || lastErrno==ERROR_PATH_NOT_FOUND ){
45993 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
45994 }else{
45995 rc = SQLITE_ERROR1;
45996 }
45997 break;
45998 }
45999 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
46000 rc = SQLITE_ERROR1; /* Files only. */
46001 break;
46002 }
46003 if ( osDeleteFileW(zConverted) ){
46004 rc = SQLITE_OK0; /* Deleted OK. */
46005 break;
46006 }
46007 if ( !winRetryIoerr(&cnt, &lastErrno) ){
46008 rc = SQLITE_ERROR1; /* No more retries. */
46009 break;
46010 }
46011 } while(1);
46012 }
46013#ifdef SQLITE_WIN32_HAS_ANSI
46014 else{
46015 do {
46016 attr = osGetFileAttributesA(zConverted);
46017 if ( attr==INVALID_FILE_ATTRIBUTES ){
46018 lastErrno = osGetLastError();
46019 if( lastErrno==ERROR_FILE_NOT_FOUND
46020 || lastErrno==ERROR_PATH_NOT_FOUND ){
46021 rc = SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)); /* Already gone? */
46022 }else{
46023 rc = SQLITE_ERROR1;
46024 }
46025 break;
46026 }
46027 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
46028 rc = SQLITE_ERROR1; /* Files only. */
46029 break;
46030 }
46031 if ( osDeleteFileA(zConverted) ){
46032 rc = SQLITE_OK0; /* Deleted OK. */
46033 break;
46034 }
46035 if ( !winRetryIoerr(&cnt, &lastErrno) ){
46036 rc = SQLITE_ERROR1; /* No more retries. */
46037 break;
46038 }
46039 } while(1);
46040 }
46041#endif
46042 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)) ){
46043 rc = winLogError(SQLITE_IOERR_DELETE(10 | (10<<8)), lastErrno, "winDelete", zFilename);
46044 }else{
46045 winLogIoerr(cnt, __LINE__46045);
46046 }
46047 sqlite3_free(zConverted);
46048 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
46049 return rc;
46050}
46051
46052/*
46053** Check the existence and status of a file.
46054*/
46055static int winAccess(
46056 sqlite3_vfs *pVfs, /* Not used on win32 */
46057 const char *zFilename, /* Name of file to check */
46058 int flags, /* Type of test to make on this file */
46059 int *pResOut /* OUT: Result */
46060){
46061 DWORD attr;
46062 int rc = 0;
46063 DWORD lastErrno = 0;
46064 void *zConverted;
46065 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46066
46067 SimulateIOError( return SQLITE_IOERR_ACCESS; );
46068 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
46069 zFilename, flags, pResOut));
46070
46071 zConverted = winConvertFromUtf8Filename(zFilename);
46072 if( zConverted==0 ){
46073 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
46074 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
46075 }
46076 if( osIsNT() ){
46077 int cnt = 0;
46078 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
46079 memset(&sAttrData, 0, sizeof(sAttrData));
46080 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
46081 GetFileExInfoStandard,
46082 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
46083 if( rc ){
46084 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
46085 ** as if it does not exist.
46086 */
46087 if( flags==SQLITE_ACCESS_EXISTS0
46088 && sAttrData.nFileSizeHigh==0
46089 && sAttrData.nFileSizeLow==0 ){
46090 attr = INVALID_FILE_ATTRIBUTES;
46091 }else{
46092 attr = sAttrData.dwFileAttributes;
46093 }
46094 }else{
46095 winLogIoerr(cnt, __LINE__46095);
46096 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
46097 sqlite3_free(zConverted);
46098 return winLogError(SQLITE_IOERR_ACCESS(10 | (13<<8)), lastErrno, "winAccess",
46099 zFilename);
46100 }else{
46101 attr = INVALID_FILE_ATTRIBUTES;
46102 }
46103 }
46104 }
46105#ifdef SQLITE_WIN32_HAS_ANSI
46106 else{
46107 attr = osGetFileAttributesA((char*)zConverted);
46108 }
46109#endif
46110 sqlite3_free(zConverted);
46111 switch( flags ){
46112 case SQLITE_ACCESS_READ2:
46113 case SQLITE_ACCESS_EXISTS0:
46114 rc = attr!=INVALID_FILE_ATTRIBUTES;
46115 break;
46116 case SQLITE_ACCESS_READWRITE1:
46117 rc = attr!=INVALID_FILE_ATTRIBUTES &&
46118 (attr & FILE_ATTRIBUTE_READONLY)==0;
46119 break;
46120 default:
46121 assert(!"Invalid flags argument")((void) (0));
46122 }
46123 *pResOut = rc;
46124 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
46125 zFilename, pResOut, *pResOut));
46126 return SQLITE_OK0;
46127}
46128
46129/*
46130** Returns non-zero if the specified path name starts with a drive letter
46131** followed by a colon character.
46132*/
46133static BOOL winIsDriveLetterAndColon(
46134 const char *zPathname
46135){
46136 return ( sqlite3Isalpha(zPathname[0])(sqlite3CtypeMap[(unsigned char)(zPathname[0])]&0x02) && zPathname[1]==':' );
46137}
46138
46139/*
46140** Returns non-zero if the specified path name should be used verbatim. If
46141** non-zero is returned from this function, the calling function must simply
46142** use the provided path name verbatim -OR- resolve it into a full path name
46143** using the GetFullPathName Win32 API function (if available).
46144*/
46145static BOOL winIsVerbatimPathname(
46146 const char *zPathname
46147){
46148 /*
46149 ** If the path name starts with a forward slash or a backslash, it is either
46150 ** a legal UNC name, a volume relative path, or an absolute path name in the
46151 ** "Unix" format on Windows. There is no easy way to differentiate between
46152 ** the final two cases; therefore, we return the safer return value of TRUE
46153 ** so that callers of this function will simply use it verbatim.
46154 */
46155 if ( winIsDirSep(zPathname[0]) ){
46156 return TRUE;
46157 }
46158
46159 /*
46160 ** If the path name starts with a letter and a colon it is either a volume
46161 ** relative path or an absolute path. Callers of this function must not
46162 ** attempt to treat it as a relative path name (i.e. they should simply use
46163 ** it verbatim).
46164 */
46165 if ( winIsDriveLetterAndColon(zPathname) ){
46166 return TRUE;
46167 }
46168
46169 /*
46170 ** If we get to this point, the path name should almost certainly be a purely
46171 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
46172 */
46173 return FALSE;
46174}
46175
46176/*
46177** Turn a relative pathname into a full pathname. Write the full
46178** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
46179** bytes in size.
46180*/
46181static int winFullPathname(
46182 sqlite3_vfs *pVfs, /* Pointer to vfs object */
46183 const char *zRelative, /* Possibly relative input path */
46184 int nFull, /* Size of output buffer in bytes */
46185 char *zFull /* Output buffer */
46186){
46187#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
46188 DWORD nByte;
46189 void *zConverted;
46190 char *zOut;
46191#endif
46192
46193 /* If this path name begins with "/X:", where "X" is any alphabetic
46194 ** character, discard the initial "/" from the pathname.
46195 */
46196 if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
46197 zRelative++;
46198 }
46199
46200#if defined(__CYGWIN__)
46201 SimulateIOError( return SQLITE_ERROR );
46202 UNUSED_PARAMETER(nFull)(void)(nFull);
46203 assert( nFull>=pVfs->mxPathname )((void) (0));
46204 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
46205 /*
46206 ** NOTE: We are dealing with a relative path name and the data
46207 ** directory has been set. Therefore, use it as the basis
46208 ** for converting the relative path name to an absolute
46209 ** one by prepending the data directory and a slash.
46210 */
46211 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
46212 if( !zOut ){
46213 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
46214 }
46215 if( cygwin_conv_path(
46216 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
46217 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
46218 sqlite3_free(zOut);
46219 return winLogError(SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)), (DWORD)errno(*__errno_location ()),
46220 "winFullPathname1", zRelative);
46221 }else{
46222 char *zUtf8 = winConvertToUtf8Filename(zOut);
46223 if( !zUtf8 ){
46224 sqlite3_free(zOut);
46225 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
46226 }
46227 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
46228 sqlite3_data_directory, winGetDirSep(), zUtf8);
46229 sqlite3_free(zUtf8);
46230 sqlite3_free(zOut);
46231 }
46232 }else{
46233 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
46234 if( !zOut ){
46235 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
46236 }
46237 if( cygwin_conv_path(
46238 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
46239 zRelative, zOut, pVfs->mxPathname+1)<0 ){
46240 sqlite3_free(zOut);
46241 return winLogError(SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)), (DWORD)errno(*__errno_location ()),
46242 "winFullPathname2", zRelative);
46243 }else{
46244 char *zUtf8 = winConvertToUtf8Filename(zOut);
46245 if( !zUtf8 ){
46246 sqlite3_free(zOut);
46247 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
46248 }
46249 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zUtf8);
46250 sqlite3_free(zUtf8);
46251 sqlite3_free(zOut);
46252 }
46253 }
46254 return SQLITE_OK0;
46255#endif
46256
46257#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
46258 SimulateIOError( return SQLITE_ERROR );
46259 /* WinCE has no concept of a relative pathname, or so I am told. */
46260 /* WinRT has no way to convert a relative path to an absolute one. */
46261 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
46262 /*
46263 ** NOTE: We are dealing with a relative path name and the data
46264 ** directory has been set. Therefore, use it as the basis
46265 ** for converting the relative path name to an absolute
46266 ** one by prepending the data directory and a backslash.
46267 */
46268 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
46269 sqlite3_data_directory, winGetDirSep(), zRelative);
46270 }else{
46271 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zRelative);
46272 }
46273 return SQLITE_OK0;
46274#endif
46275
46276#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
46277 /* It's odd to simulate an io-error here, but really this is just
46278 ** using the io-error infrastructure to test that SQLite handles this
46279 ** function failing. This function could fail if, for example, the
46280 ** current working directory has been unlinked.
46281 */
46282 SimulateIOError( return SQLITE_ERROR );
46283 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
46284 /*
46285 ** NOTE: We are dealing with a relative path name and the data
46286 ** directory has been set. Therefore, use it as the basis
46287 ** for converting the relative path name to an absolute
46288 ** one by prepending the data directory and a backslash.
46289 */
46290 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s%c%s",
46291 sqlite3_data_directory, winGetDirSep(), zRelative);
46292 return SQLITE_OK0;
46293 }
46294 zConverted = winConvertFromUtf8Filename(zRelative);
46295 if( zConverted==0 ){
46296 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
46297 }
46298 if( osIsNT() ){
46299 LPWSTR zTemp;
46300 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
46301 if( nByte==0 ){
46302 sqlite3_free(zConverted);
46303 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
46304 "winFullPathname1", zRelative);
46305 }
46306 nByte += 3;
46307 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
46308 if( zTemp==0 ){
46309 sqlite3_free(zConverted);
46310 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
46311 }
46312 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
46313 if( nByte==0 ){
46314 sqlite3_free(zConverted);
46315 sqlite3_free(zTemp);
46316 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
46317 "winFullPathname2", zRelative);
46318 }
46319 sqlite3_free(zConverted);
46320 zOut = winUnicodeToUtf8(zTemp);
46321 sqlite3_free(zTemp);
46322 }
46323#ifdef SQLITE_WIN32_HAS_ANSI
46324 else{
46325 char *zTemp;
46326 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
46327 if( nByte==0 ){
46328 sqlite3_free(zConverted);
46329 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
46330 "winFullPathname3", zRelative);
46331 }
46332 nByte += 3;
46333 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
46334 if( zTemp==0 ){
46335 sqlite3_free(zConverted);
46336 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
46337 }
46338 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
46339 if( nByte==0 ){
46340 sqlite3_free(zConverted);
46341 sqlite3_free(zTemp);
46342 return winLogError(SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)), osGetLastError(),
46343 "winFullPathname4", zRelative);
46344 }
46345 sqlite3_free(zConverted);
46346 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
46347 sqlite3_free(zTemp);
46348 }
46349#endif
46350 if( zOut ){
46351 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname)((nFull)<(pVfs->mxPathname)?(nFull):(pVfs->mxPathname
))
, zFull, "%s", zOut);
46352 sqlite3_free(zOut);
46353 return SQLITE_OK0;
46354 }else{
46355 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
46356 }
46357#endif
46358}
46359
46360#ifndef SQLITE_OMIT_LOAD_EXTENSION
46361/*
46362** Interfaces for opening a shared library, finding entry points
46363** within the shared library, and closing the shared library.
46364*/
46365static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
46366 HANDLE h;
46367#if defined(__CYGWIN__)
46368 int nFull = pVfs->mxPathname+1;
46369 char *zFull = sqlite3MallocZero( nFull );
46370 void *zConverted = 0;
46371 if( zFull==0 ){
46372 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
46373 return 0;
46374 }
46375 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK0 ){
46376 sqlite3_free(zFull);
46377 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
46378 return 0;
46379 }
46380 zConverted = winConvertFromUtf8Filename(zFull);
46381 sqlite3_free(zFull);
46382#else
46383 void *zConverted = winConvertFromUtf8Filename(zFilename);
46384 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46385#endif
46386 if( zConverted==0 ){
46387 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
46388 return 0;
46389 }
46390 if( osIsNT() ){
46391#if SQLITE_OS_WINRT
46392 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
46393#else
46394 h = osLoadLibraryW((LPCWSTR)zConverted);
46395#endif
46396 }
46397#ifdef SQLITE_WIN32_HAS_ANSI
46398 else{
46399 h = osLoadLibraryA((char*)zConverted);
46400 }
46401#endif
46402 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
46403 sqlite3_free(zConverted);
46404 return (void*)h;
46405}
46406static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
46407 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46408 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
46409}
46410static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
46411 FARPROC proc;
46412 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46413 proc = osGetProcAddressA((HANDLE)pH, zSym);
46414 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
46415 (void*)pH, zSym, (void*)proc));
46416 return (void(*)(void))proc;
46417}
46418static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
46419 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46420 osFreeLibrary((HANDLE)pHandle);
46421 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
46422}
46423#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
46424 #define winDlOpen 0
46425 #define winDlError 0
46426 #define winDlSym 0
46427 #define winDlClose 0
46428#endif
46429
46430/* State information for the randomness gatherer. */
46431typedef struct EntropyGatherer EntropyGatherer;
46432struct EntropyGatherer {
46433 unsigned char *a; /* Gather entropy into this buffer */
46434 int na; /* Size of a[] in bytes */
46435 int i; /* XOR next input into a[i] */
46436 int nXor; /* Number of XOR operations done */
46437};
46438
46439#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
46440/* Mix sz bytes of entropy into p. */
46441static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
46442 int j, k;
46443 for(j=0, k=p->i; j<sz; j++){
46444 p->a[k++] ^= x[j];
46445 if( k>=p->na ) k = 0;
46446 }
46447 p->i = k;
46448 p->nXor += sz;
46449}
46450#endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
46451
46452/*
46453** Write up to nBuf bytes of randomness into zBuf.
46454*/
46455static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
46456#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
46457 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46458 memset(zBuf, 0, nBuf);
46459 return nBuf;
46460#else
46461 EntropyGatherer e;
46462 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46463 memset(zBuf, 0, nBuf);
46464 e.a = (unsigned char*)zBuf;
46465 e.na = nBuf;
46466 e.nXor = 0;
46467 e.i = 0;
46468 {
46469 SYSTEMTIME x;
46470 osGetSystemTime(&x);
46471 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
46472 }
46473 {
46474 DWORD pid = osGetCurrentProcessId();
46475 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
46476 }
46477#if SQLITE_OS_WINRT
46478 {
46479 ULONGLONG cnt = osGetTickCount64();
46480 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
46481 }
46482#else
46483 {
46484 DWORD cnt = osGetTickCount();
46485 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
46486 }
46487#endif /* SQLITE_OS_WINRT */
46488 {
46489 LARGE_INTEGER i;
46490 osQueryPerformanceCounter(&i);
46491 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
46492 }
46493#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
46494 {
46495 UUID id;
46496 memset(&id, 0, sizeof(UUID));
46497 osUuidCreate(&id);
46498 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
46499 memset(&id, 0, sizeof(UUID));
46500 osUuidCreateSequential(&id);
46501 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
46502 }
46503#endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
46504 return e.nXor>nBuf ? nBuf : e.nXor;
46505#endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
46506}
46507
46508
46509/*
46510** Sleep for a little while. Return the amount of time slept.
46511*/
46512static int winSleep(sqlite3_vfs *pVfs, int microsec){
46513 sqlite3_win32_sleep((microsec+999)/1000);
46514 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46515 return ((microsec+999)/1000)*1000;
46516}
46517
46518/*
46519** The following variable, if set to a non-zero value, is interpreted as
46520** the number of seconds since 1970 and is used to set the result of
46521** sqlite3OsCurrentTime() during testing.
46522*/
46523#ifdef SQLITE_TEST
46524SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */
46525#endif
46526
46527/*
46528** Find the current time (in Universal Coordinated Time). Write into *piNow
46529** the current time and date as a Julian Day number times 86_400_000. In
46530** other words, write into *piNow the number of milliseconds since the Julian
46531** epoch of noon in Greenwich on November 24, 4714 B.C according to the
46532** proleptic Gregorian calendar.
46533**
46534** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
46535** cannot be found.
46536*/
46537static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
46538 /* FILETIME structure is a 64-bit value representing the number of
46539 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
46540 */
46541 FILETIME ft;
46542 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
46543#ifdef SQLITE_TEST
46544 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
46545#endif
46546 /* 2^32 - to avoid use of LL and warnings in gcc */
46547 static const sqlite3_int64 max32BitValue =
46548 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
46549 (sqlite3_int64)294967296;
46550
46551#if SQLITE_OS_WINCE
46552 SYSTEMTIME time;
46553 osGetSystemTime(&time);
46554 /* if SystemTimeToFileTime() fails, it returns zero. */
46555 if (!osSystemTimeToFileTime(&time,&ft)){
46556 return SQLITE_ERROR1;
46557 }
46558#else
46559 osGetSystemTimeAsFileTime( &ft );
46560#endif
46561
46562 *piNow = winFiletimeEpoch +
46563 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
46564 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
46565
46566#ifdef SQLITE_TEST
46567 if( sqlite3_current_time ){
46568 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
46569 }
46570#endif
46571 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46572 return SQLITE_OK0;
46573}
46574
46575/*
46576** Find the current time (in Universal Coordinated Time). Write the
46577** current time and date as a Julian Day number into *prNow and
46578** return 0. Return 1 if the time and date cannot be found.
46579*/
46580static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
46581 int rc;
46582 sqlite3_int64 i;
46583 rc = winCurrentTimeInt64(pVfs, &i);
46584 if( !rc ){
46585 *prNow = i/86400000.0;
46586 }
46587 return rc;
46588}
46589
46590/*
46591** The idea is that this function works like a combination of
46592** GetLastError() and FormatMessage() on Windows (or errno and
46593** strerror_r() on Unix). After an error is returned by an OS
46594** function, SQLite calls this function with zBuf pointing to
46595** a buffer of nBuf bytes. The OS layer should populate the
46596** buffer with a nul-terminated UTF-8 encoded error message
46597** describing the last IO error to have occurred within the calling
46598** thread.
46599**
46600** If the error message is too large for the supplied buffer,
46601** it should be truncated. The return value of xGetLastError
46602** is zero if the error message fits in the buffer, or non-zero
46603** otherwise (if the message was truncated). If non-zero is returned,
46604** then it is not necessary to include the nul-terminator character
46605** in the output buffer.
46606**
46607** Not supplying an error message will have no adverse effect
46608** on SQLite. It is fine to have an implementation that never
46609** returns an error message:
46610**
46611** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
46612** assert(zBuf[0]=='\0');
46613** return 0;
46614** }
46615**
46616** However if an error message is supplied, it will be incorporated
46617** by sqlite into the error message available to the user using
46618** sqlite3_errmsg(), possibly making IO errors easier to debug.
46619*/
46620static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
46621 DWORD e = osGetLastError();
46622 UNUSED_PARAMETER(pVfs)(void)(pVfs);
46623 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
46624 return e;
46625}
46626
46627/*
46628** Initialize and deinitialize the operating system interface.
46629*/
46630SQLITE_API int sqlite3_os_init(void){
46631 static sqlite3_vfs winVfs = {
46632 3, /* iVersion */
46633 sizeof(winFile), /* szOsFile */
46634 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
46635 0, /* pNext */
46636 "win32", /* zName */
46637 &winAppData, /* pAppData */
46638 winOpen, /* xOpen */
46639 winDelete, /* xDelete */
46640 winAccess, /* xAccess */
46641 winFullPathname, /* xFullPathname */
46642 winDlOpen, /* xDlOpen */
46643 winDlError, /* xDlError */
46644 winDlSym, /* xDlSym */
46645 winDlClose, /* xDlClose */
46646 winRandomness, /* xRandomness */
46647 winSleep, /* xSleep */
46648 winCurrentTime, /* xCurrentTime */
46649 winGetLastError, /* xGetLastError */
46650 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46651 winSetSystemCall, /* xSetSystemCall */
46652 winGetSystemCall, /* xGetSystemCall */
46653 winNextSystemCall, /* xNextSystemCall */
46654 };
46655#if defined(SQLITE_WIN32_HAS_WIDE)
46656 static sqlite3_vfs winLongPathVfs = {
46657 3, /* iVersion */
46658 sizeof(winFile), /* szOsFile */
46659 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
46660 0, /* pNext */
46661 "win32-longpath", /* zName */
46662 &winAppData, /* pAppData */
46663 winOpen, /* xOpen */
46664 winDelete, /* xDelete */
46665 winAccess, /* xAccess */
46666 winFullPathname, /* xFullPathname */
46667 winDlOpen, /* xDlOpen */
46668 winDlError, /* xDlError */
46669 winDlSym, /* xDlSym */
46670 winDlClose, /* xDlClose */
46671 winRandomness, /* xRandomness */
46672 winSleep, /* xSleep */
46673 winCurrentTime, /* xCurrentTime */
46674 winGetLastError, /* xGetLastError */
46675 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46676 winSetSystemCall, /* xSetSystemCall */
46677 winGetSystemCall, /* xGetSystemCall */
46678 winNextSystemCall, /* xNextSystemCall */
46679 };
46680#endif
46681 static sqlite3_vfs winNolockVfs = {
46682 3, /* iVersion */
46683 sizeof(winFile), /* szOsFile */
46684 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
46685 0, /* pNext */
46686 "win32-none", /* zName */
46687 &winNolockAppData, /* pAppData */
46688 winOpen, /* xOpen */
46689 winDelete, /* xDelete */
46690 winAccess, /* xAccess */
46691 winFullPathname, /* xFullPathname */
46692 winDlOpen, /* xDlOpen */
46693 winDlError, /* xDlError */
46694 winDlSym, /* xDlSym */
46695 winDlClose, /* xDlClose */
46696 winRandomness, /* xRandomness */
46697 winSleep, /* xSleep */
46698 winCurrentTime, /* xCurrentTime */
46699 winGetLastError, /* xGetLastError */
46700 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46701 winSetSystemCall, /* xSetSystemCall */
46702 winGetSystemCall, /* xGetSystemCall */
46703 winNextSystemCall, /* xNextSystemCall */
46704 };
46705#if defined(SQLITE_WIN32_HAS_WIDE)
46706 static sqlite3_vfs winLongPathNolockVfs = {
46707 3, /* iVersion */
46708 sizeof(winFile), /* szOsFile */
46709 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
46710 0, /* pNext */
46711 "win32-longpath-none", /* zName */
46712 &winNolockAppData, /* pAppData */
46713 winOpen, /* xOpen */
46714 winDelete, /* xDelete */
46715 winAccess, /* xAccess */
46716 winFullPathname, /* xFullPathname */
46717 winDlOpen, /* xDlOpen */
46718 winDlError, /* xDlError */
46719 winDlSym, /* xDlSym */
46720 winDlClose, /* xDlClose */
46721 winRandomness, /* xRandomness */
46722 winSleep, /* xSleep */
46723 winCurrentTime, /* xCurrentTime */
46724 winGetLastError, /* xGetLastError */
46725 winCurrentTimeInt64, /* xCurrentTimeInt64 */
46726 winSetSystemCall, /* xSetSystemCall */
46727 winGetSystemCall, /* xGetSystemCall */
46728 winNextSystemCall, /* xNextSystemCall */
46729 };
46730#endif
46731
46732 /* Double-check that the aSyscall[] array has been constructed
46733 ** correctly. See ticket [bb3a86e890c8e96ab] */
46734 assert( ArraySize(aSyscall)==80 )((void) (0));
46735
46736 /* get memory map allocation granularity */
46737 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
46738#if SQLITE_OS_WINRT
46739 osGetNativeSystemInfo(&winSysInfo);
46740#else
46741 osGetSystemInfo(&winSysInfo);
46742#endif
46743 assert( winSysInfo.dwAllocationGranularity>0 )((void) (0));
46744 assert( winSysInfo.dwPageSize>0 )((void) (0));
46745
46746 sqlite3_vfs_register(&winVfs, 1);
46747
46748#if defined(SQLITE_WIN32_HAS_WIDE)
46749 sqlite3_vfs_register(&winLongPathVfs, 0);
46750#endif
46751
46752 sqlite3_vfs_register(&winNolockVfs, 0);
46753
46754#if defined(SQLITE_WIN32_HAS_WIDE)
46755 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
46756#endif
46757
46758#ifndef SQLITE_OMIT_WAL
46759 winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS111);
46760#endif
46761
46762 return SQLITE_OK0;
46763}
46764
46765SQLITE_API int sqlite3_os_end(void){
46766#if SQLITE_OS_WINRT
46767 if( sleepObj!=NULL((void*)0) ){
46768 osCloseHandle(sleepObj);
46769 sleepObj = NULL((void*)0);
46770 }
46771#endif
46772
46773#ifndef SQLITE_OMIT_WAL
46774 winBigLock = 0;
46775#endif
46776
46777 return SQLITE_OK0;
46778}
46779
46780#endif /* SQLITE_OS_WIN */
46781
46782/************** End of os_win.c **********************************************/
46783/************** Begin file memdb.c *******************************************/
46784/*
46785** 2016-09-07
46786**
46787** The author disclaims copyright to this source code. In place of
46788** a legal notice, here is a blessing:
46789**
46790** May you do good and not evil.
46791** May you find forgiveness for yourself and forgive others.
46792** May you share freely, never taking more than you give.
46793**
46794******************************************************************************
46795**
46796** This file implements an in-memory VFS. A database is held as a contiguous
46797** block of memory.
46798**
46799** This file also implements interface sqlite3_serialize() and
46800** sqlite3_deserialize().
46801*/
46802/* #include "sqliteInt.h" */
46803#ifdef SQLITE_ENABLE_DESERIALIZE
46804
46805/*
46806** Forward declaration of objects used by this utility
46807*/
46808typedef struct sqlite3_vfs MemVfs;
46809typedef struct MemFile MemFile;
46810
46811/* Access to a lower-level VFS that (might) implement dynamic loading,
46812** access to randomness, etc.
46813*/
46814#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
46815
46816/* An open file */
46817struct MemFile {
46818 sqlite3_file base; /* IO methods */
46819 sqlite3_int64 sz; /* Size of the file */
46820 sqlite3_int64 szAlloc; /* Space allocated to aData */
46821 sqlite3_int64 szMax; /* Maximum allowed size of the file */
46822 unsigned char *aData; /* content of the file */
46823 int nMmap; /* Number of memory mapped pages */
46824 unsigned mFlags; /* Flags */
46825 int eLock; /* Most recent lock against this file */
46826};
46827
46828/*
46829** Methods for MemFile
46830*/
46831static int memdbClose(sqlite3_file*);
46832static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
46833static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
46834static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
46835static int memdbSync(sqlite3_file*, int flags);
46836static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
46837static int memdbLock(sqlite3_file*, int);
46838/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
46839static int memdbFileControl(sqlite3_file*, int op, void *pArg);
46840/* static int memdbSectorSize(sqlite3_file*); // not used */
46841static int memdbDeviceCharacteristics(sqlite3_file*);
46842static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
46843static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
46844
46845/*
46846** Methods for MemVfs
46847*/
46848static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
46849/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
46850static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
46851static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
46852static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
46853static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
46854static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
46855static void memdbDlClose(sqlite3_vfs*, void*);
46856static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
46857static int memdbSleep(sqlite3_vfs*, int microseconds);
46858/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
46859static int memdbGetLastError(sqlite3_vfs*, int, char *);
46860static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
46861
46862static sqlite3_vfs memdb_vfs = {
46863 2, /* iVersion */
46864 0, /* szOsFile (set when registered) */
46865 1024, /* mxPathname */
46866 0, /* pNext */
46867 "memdb", /* zName */
46868 0, /* pAppData (set when registered) */
46869 memdbOpen, /* xOpen */
46870 0, /* memdbDelete, */ /* xDelete */
46871 memdbAccess, /* xAccess */
46872 memdbFullPathname, /* xFullPathname */
46873 memdbDlOpen, /* xDlOpen */
46874 memdbDlError, /* xDlError */
46875 memdbDlSym, /* xDlSym */
46876 memdbDlClose, /* xDlClose */
46877 memdbRandomness, /* xRandomness */
46878 memdbSleep, /* xSleep */
46879 0, /* memdbCurrentTime, */ /* xCurrentTime */
46880 memdbGetLastError, /* xGetLastError */
46881 memdbCurrentTimeInt64 /* xCurrentTimeInt64 */
46882};
46883
46884static const sqlite3_io_methods memdb_io_methods = {
46885 3, /* iVersion */
46886 memdbClose, /* xClose */
46887 memdbRead, /* xRead */
46888 memdbWrite, /* xWrite */
46889 memdbTruncate, /* xTruncate */
46890 memdbSync, /* xSync */
46891 memdbFileSize, /* xFileSize */
46892 memdbLock, /* xLock */
46893 memdbLock, /* xUnlock - same as xLock in this case */
46894 0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
46895 memdbFileControl, /* xFileControl */
46896 0, /* memdbSectorSize,*/ /* xSectorSize */
46897 memdbDeviceCharacteristics, /* xDeviceCharacteristics */
46898 0, /* xShmMap */
46899 0, /* xShmLock */
46900 0, /* xShmBarrier */
46901 0, /* xShmUnmap */
46902 memdbFetch, /* xFetch */
46903 memdbUnfetch /* xUnfetch */
46904};
46905
46906
46907
46908/*
46909** Close an memdb-file.
46910**
46911** The pData pointer is owned by the application, so there is nothing
46912** to free.
46913*/
46914static int memdbClose(sqlite3_file *pFile){
46915 MemFile *p = (MemFile *)pFile;
46916 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE1 ) sqlite3_free(p->aData);
46917 return SQLITE_OK0;
46918}
46919
46920/*
46921** Read data from an memdb-file.
46922*/
46923static int memdbRead(
46924 sqlite3_file *pFile,
46925 void *zBuf,
46926 int iAmt,
46927 sqlite_int64 iOfst
46928){
46929 MemFile *p = (MemFile *)pFile;
46930 if( iOfst+iAmt>p->sz ){
46931 memset(zBuf, 0, iAmt);
46932 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
46933 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
46934 }
46935 memcpy(zBuf, p->aData+iOfst, iAmt);
46936 return SQLITE_OK0;
46937}
46938
46939/*
46940** Try to enlarge the memory allocation to hold at least sz bytes
46941*/
46942static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
46943 unsigned char *pNew;
46944 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE2)==0 || p->nMmap>0 ){
46945 return SQLITE_FULL13;
46946 }
46947 if( newSz>p->szMax ){
46948 return SQLITE_FULL13;
46949 }
46950 newSz *= 2;
46951 if( newSz>p->szMax ) newSz = p->szMax;
46952 pNew = sqlite3_realloc64(p->aData, newSz);
46953 if( pNew==0 ) return SQLITE_NOMEM7;
46954 p->aData = pNew;
46955 p->szAlloc = newSz;
46956 return SQLITE_OK0;
46957}
46958
46959/*
46960** Write data to an memdb-file.
46961*/
46962static int memdbWrite(
46963 sqlite3_file *pFile,
46964 const void *z,
46965 int iAmt,
46966 sqlite_int64 iOfst
46967){
46968 MemFile *p = (MemFile *)pFile;
46969 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY)(p->mFlags & 4) ) return SQLITE_READONLY8;
46970 if( iOfst+iAmt>p->sz ){
46971 int rc;
46972 if( iOfst+iAmt>p->szAlloc
46973 && (rc = memdbEnlarge(p, iOfst+iAmt))!=SQLITE_OK0
46974 ){
46975 return rc;
46976 }
46977 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
46978 p->sz = iOfst+iAmt;
46979 }
46980 memcpy(p->aData+iOfst, z, iAmt);
46981 return SQLITE_OK0;
46982}
46983
46984/*
46985** Truncate an memdb-file.
46986**
46987** In rollback mode (which is always the case for memdb, as it does not
46988** support WAL mode) the truncate() method is only used to reduce
46989** the size of a file, never to increase the size.
46990*/
46991static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
46992 MemFile *p = (MemFile *)pFile;
46993 if( NEVER(size>p->sz)(size>p->sz) ) return SQLITE_FULL13;
46994 p->sz = size;
46995 return SQLITE_OK0;
46996}
46997
46998/*
46999** Sync an memdb-file.
47000*/
47001static int memdbSync(sqlite3_file *pFile, int flags){
47002 return SQLITE_OK0;
47003}
47004
47005/*
47006** Return the current file-size of an memdb-file.
47007*/
47008static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
47009 MemFile *p = (MemFile *)pFile;
47010 *pSize = p->sz;
47011 return SQLITE_OK0;
47012}
47013
47014/*
47015** Lock an memdb-file.
47016*/
47017static int memdbLock(sqlite3_file *pFile, int eLock){
47018 MemFile *p = (MemFile *)pFile;
47019 if( eLock>SQLITE_LOCK_SHARED1
47020 && (p->mFlags & SQLITE_DESERIALIZE_READONLY4)!=0
47021 ){
47022 return SQLITE_READONLY8;
47023 }
47024 p->eLock = eLock;
47025 return SQLITE_OK0;
47026}
47027
47028#if 0 /* Never used because memdbAccess() always returns false */
47029/*
47030** Check if another file-handle holds a RESERVED lock on an memdb-file.
47031*/
47032static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
47033 *pResOut = 0;
47034 return SQLITE_OK0;
47035}
47036#endif
47037
47038/*
47039** File control method. For custom operations on an memdb-file.
47040*/
47041static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
47042 MemFile *p = (MemFile *)pFile;
47043 int rc = SQLITE_NOTFOUND12;
47044 if( op==SQLITE_FCNTL_VFSNAME12 ){
47045 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
47046 rc = SQLITE_OK0;
47047 }
47048 if( op==SQLITE_FCNTL_SIZE_LIMIT36 ){
47049 sqlite3_int64 iLimit = *(sqlite3_int64*)pArg;
47050 if( iLimit<p->sz ){
47051 if( iLimit<0 ){
47052 iLimit = p->szMax;
47053 }else{
47054 iLimit = p->sz;
47055 }
47056 }
47057 p->szMax = iLimit;
47058 *(sqlite3_int64*)pArg = iLimit;
47059 rc = SQLITE_OK0;
47060 }
47061 return rc;
47062}
47063
47064#if 0 /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
47065/*
47066** Return the sector-size in bytes for an memdb-file.
47067*/
47068static int memdbSectorSize(sqlite3_file *pFile){
47069 return 1024;
47070}
47071#endif
47072
47073/*
47074** Return the device characteristic flags supported by an memdb-file.
47075*/
47076static int memdbDeviceCharacteristics(sqlite3_file *pFile){
47077 return SQLITE_IOCAP_ATOMIC0x00000001 |
47078 SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 |
47079 SQLITE_IOCAP_SAFE_APPEND0x00000200 |
47080 SQLITE_IOCAP_SEQUENTIAL0x00000400;
47081}
47082
47083/* Fetch a page of a memory-mapped file */
47084static int memdbFetch(
47085 sqlite3_file *pFile,
47086 sqlite3_int64 iOfst,
47087 int iAmt,
47088 void **pp
47089){
47090 MemFile *p = (MemFile *)pFile;
47091 if( iOfst+iAmt>p->sz ){
47092 *pp = 0;
47093 }else{
47094 p->nMmap++;
47095 *pp = (void*)(p->aData + iOfst);
47096 }
47097 return SQLITE_OK0;
47098}
47099
47100/* Release a memory-mapped page */
47101static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
47102 MemFile *p = (MemFile *)pFile;
47103 p->nMmap--;
47104 return SQLITE_OK0;
47105}
47106
47107/*
47108** Open an mem file handle.
47109*/
47110static int memdbOpen(
47111 sqlite3_vfs *pVfs,
47112 const char *zName,
47113 sqlite3_file *pFile,
47114 int flags,
47115 int *pOutFlags
47116){
47117 MemFile *p = (MemFile*)pFile;
47118 if( (flags & SQLITE_OPEN_MAIN_DB0x00000100)==0 ){
47119 return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
47120 }
47121 memset(p, 0, sizeof(*p));
47122 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE2 | SQLITE_DESERIALIZE_FREEONCLOSE1;
47123 assert( pOutFlags!=0 )((void) (0)); /* True because flags==SQLITE_OPEN_MAIN_DB */
47124 *pOutFlags = flags | SQLITE_OPEN_MEMORY0x00000080;
47125 p->base.pMethods = &memdb_io_methods;
47126 p->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
47127 return SQLITE_OK0;
47128}
47129
47130#if 0 /* Only used to delete rollback journals, master journals, and WAL
47131 ** files, none of which exist in memdb. So this routine is never used */
47132/*
47133** Delete the file located at zPath. If the dirSync argument is true,
47134** ensure the file-system modifications are synced to disk before
47135** returning.
47136*/
47137static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
47138 return SQLITE_IOERR_DELETE(10 | (10<<8));
47139}
47140#endif
47141
47142/*
47143** Test for access permissions. Return true if the requested permission
47144** is available, or false otherwise.
47145**
47146** With memdb, no files ever exist on disk. So always return false.
47147*/
47148static int memdbAccess(
47149 sqlite3_vfs *pVfs,
47150 const char *zPath,
47151 int flags,
47152 int *pResOut
47153){
47154 *pResOut = 0;
47155 return SQLITE_OK0;
47156}
47157
47158/*
47159** Populate buffer zOut with the full canonical pathname corresponding
47160** to the pathname in zPath. zOut is guaranteed to point to a buffer
47161** of at least (INST_MAX_PATHNAME+1) bytes.
47162*/
47163static int memdbFullPathname(
47164 sqlite3_vfs *pVfs,
47165 const char *zPath,
47166 int nOut,
47167 char *zOut
47168){
47169 sqlite3_snprintf(nOut, zOut, "%s", zPath);
47170 return SQLITE_OK0;
47171}
47172
47173/*
47174** Open the dynamic library located at zPath and return a handle.
47175*/
47176static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
47177 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
47178}
47179
47180/*
47181** Populate the buffer zErrMsg (size nByte bytes) with a human readable
47182** utf-8 string describing the most recent error encountered associated
47183** with dynamic libraries.
47184*/
47185static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
47186 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
47187}
47188
47189/*
47190** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
47191*/
47192static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
47193 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
47194}
47195
47196/*
47197** Close the dynamic library handle pHandle.
47198*/
47199static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
47200 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
47201}
47202
47203/*
47204** Populate the buffer pointed to by zBufOut with nByte bytes of
47205** random data.
47206*/
47207static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
47208 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
47209}
47210
47211/*
47212** Sleep for nMicro microseconds. Return the number of microseconds
47213** actually slept.
47214*/
47215static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
47216 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
47217}
47218
47219#if 0 /* Never used. Modern cores only call xCurrentTimeInt64() */
47220/*
47221** Return the current time as a Julian Day number in *pTimeOut.
47222*/
47223static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
47224 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
47225}
47226#endif
47227
47228static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
47229 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
47230}
47231static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
47232 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
47233}
47234
47235/*
47236** Translate a database connection pointer and schema name into a
47237** MemFile pointer.
47238*/
47239static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
47240 MemFile *p = 0;
47241 int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER7, &p);
47242 if( rc ) return 0;
47243 if( p->base.pMethods!=&memdb_io_methods ) return 0;
47244 return p;
47245}
47246
47247/*
47248** Return the serialization of a database
47249*/
47250SQLITE_API unsigned char *sqlite3_serialize(
47251 sqlite3 *db, /* The database connection */
47252 const char *zSchema, /* Which database within the connection */
47253 sqlite3_int64 *piSize, /* Write size here, if not NULL */
47254 unsigned int mFlags /* Maybe SQLITE_SERIALIZE_NOCOPY */
47255){
47256 MemFile *p;
47257 int iDb;
47258 Btree *pBt;
47259 sqlite3_int64 sz;
47260 int szPage = 0;
47261 sqlite3_stmt *pStmt = 0;
47262 unsigned char *pOut;
47263 char *zSql;
47264 int rc;
47265
47266#ifdef SQLITE_ENABLE_API_ARMOR
47267 if( !sqlite3SafetyCheckOk(db) ){
47268 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(47268);
47269 return 0;
47270 }
47271#endif
47272
47273 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
47274 p = memdbFromDbSchema(db, zSchema);
47275 iDb = sqlite3FindDbName(db, zSchema);
47276 if( piSize ) *piSize = -1;
47277 if( iDb<0 ) return 0;
47278 if( p ){
47279 if( piSize ) *piSize = p->sz;
47280 if( mFlags & SQLITE_SERIALIZE_NOCOPY0x001 ){
47281 pOut = p->aData;
47282 }else{
47283 pOut = sqlite3_malloc64( p->sz );
47284 if( pOut ) memcpy(pOut, p->aData, p->sz);
47285 }
47286 return pOut;
47287 }
47288 pBt = db->aDb[iDb].pBt;
47289 if( pBt==0 ) return 0;
47290 szPage = sqlite3BtreeGetPageSize(pBt);
47291 zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
47292 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM7;
47293 sqlite3_free(zSql);
47294 if( rc ) return 0;
47295 rc = sqlite3_step(pStmt);
47296 if( rc!=SQLITE_ROW100 ){
47297 pOut = 0;
47298 }else{
47299 sz = sqlite3_column_int64(pStmt, 0)*szPage;
47300 if( piSize ) *piSize = sz;
47301 if( mFlags & SQLITE_SERIALIZE_NOCOPY0x001 ){
47302 pOut = 0;
47303 }else{
47304 pOut = sqlite3_malloc64( sz );
47305 if( pOut ){
47306 int nPage = sqlite3_column_int(pStmt, 0);
47307 Pager *pPager = sqlite3BtreePager(pBt);
47308 int pgno;
47309 for(pgno=1; pgno<=nPage; pgno++){
47310 DbPage *pPage = 0;
47311 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
47312 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
47313 if( rc==SQLITE_OK0 ){
47314 memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
47315 }else{
47316 memset(pTo, 0, szPage);
47317 }
47318 sqlite3PagerUnref(pPage);
47319 }
47320 }
47321 }
47322 }
47323 sqlite3_finalize(pStmt);
47324 return pOut;
47325}
47326
47327/* Convert zSchema to a MemDB and initialize its content.
47328*/
47329SQLITE_API int sqlite3_deserialize(
47330 sqlite3 *db, /* The database connection */
47331 const char *zSchema, /* Which DB to reopen with the deserialization */
47332 unsigned char *pData, /* The serialized database content */
47333 sqlite3_int64 szDb, /* Number bytes in the deserialization */
47334 sqlite3_int64 szBuf, /* Total size of buffer pData[] */
47335 unsigned mFlags /* Zero or more SQLITE_DESERIALIZE_* flags */
47336){
47337 MemFile *p;
47338 char *zSql;
47339 sqlite3_stmt *pStmt = 0;
47340 int rc;
47341 int iDb;
47342
47343#ifdef SQLITE_ENABLE_API_ARMOR
47344 if( !sqlite3SafetyCheckOk(db) ){
47345 return SQLITE_MISUSE_BKPTsqlite3MisuseError(47345);
47346 }
47347 if( szDb<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(47347);
47348 if( szBuf<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(47348);
47349#endif
47350
47351 sqlite3_mutex_enter(db->mutex);
47352 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
47353 iDb = sqlite3FindDbName(db, zSchema);
47354 if( iDb<0 ){
47355 rc = SQLITE_ERROR1;
47356 goto end_deserialize;
47357 }
47358 zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
47359 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
47360 sqlite3_free(zSql);
47361 if( rc ) goto end_deserialize;
47362 db->init.iDb = (u8)iDb;
47363 db->init.reopenMemdb = 1;
47364 rc = sqlite3_step(pStmt);
47365 db->init.reopenMemdb = 0;
47366 if( rc!=SQLITE_DONE101 ){
47367 rc = SQLITE_ERROR1;
47368 goto end_deserialize;
47369 }
47370 p = memdbFromDbSchema(db, zSchema);
47371 if( p==0 ){
47372 rc = SQLITE_ERROR1;
47373 }else{
47374 p->aData = pData;
47375 p->sz = szDb;
47376 p->szAlloc = szBuf;
47377 p->szMax = szBuf;
47378 if( p->szMax<sqlite3GlobalConfigsqlite3Config.mxMemdbSize ){
47379 p->szMax = sqlite3GlobalConfigsqlite3Config.mxMemdbSize;
47380 }
47381 p->mFlags = mFlags;
47382 rc = SQLITE_OK0;
47383 }
47384
47385end_deserialize:
47386 sqlite3_finalize(pStmt);
47387 sqlite3_mutex_leave(db->mutex);
47388 return rc;
47389}
47390
47391/*
47392** This routine is called when the extension is loaded.
47393** Register the new VFS.
47394*/
47395SQLITE_PRIVATEstatic int sqlite3MemdbInit(void){
47396 sqlite3_vfs *pLower = sqlite3_vfs_find(0);
47397 int sz = pLower->szOsFile;
47398 memdb_vfs.pAppData = pLower;
47399 /* In all known configurations of SQLite, the size of a default
47400 ** sqlite3_file is greater than the size of a memdb sqlite3_file.
47401 ** Should that ever change, remove the following NEVER() */
47402 if( NEVER(sz<sizeof(MemFile))(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
47403 memdb_vfs.szOsFile = sz;
47404 return sqlite3_vfs_register(&memdb_vfs, 0);
47405}
47406#endif /* SQLITE_ENABLE_DESERIALIZE */
47407
47408/************** End of memdb.c ***********************************************/
47409/************** Begin file bitvec.c ******************************************/
47410/*
47411** 2008 February 16
47412**
47413** The author disclaims copyright to this source code. In place of
47414** a legal notice, here is a blessing:
47415**
47416** May you do good and not evil.
47417** May you find forgiveness for yourself and forgive others.
47418** May you share freely, never taking more than you give.
47419**
47420*************************************************************************
47421** This file implements an object that represents a fixed-length
47422** bitmap. Bits are numbered starting with 1.
47423**
47424** A bitmap is used to record which pages of a database file have been
47425** journalled during a transaction, or which pages have the "dont-write"
47426** property. Usually only a few pages are meet either condition.
47427** So the bitmap is usually sparse and has low cardinality.
47428** But sometimes (for example when during a DROP of a large table) most
47429** or all of the pages in a database can get journalled. In those cases,
47430** the bitmap becomes dense with high cardinality. The algorithm needs
47431** to handle both cases well.
47432**
47433** The size of the bitmap is fixed when the object is created.
47434**
47435** All bits are clear when the bitmap is created. Individual bits
47436** may be set or cleared one at a time.
47437**
47438** Test operations are about 100 times more common that set operations.
47439** Clear operations are exceedingly rare. There are usually between
47440** 5 and 500 set operations per Bitvec object, though the number of sets can
47441** sometimes grow into tens of thousands or larger. The size of the
47442** Bitvec object is the number of pages in the database file at the
47443** start of a transaction, and is thus usually less than a few thousand,
47444** but can be as large as 2 billion for a really big database.
47445*/
47446/* #include "sqliteInt.h" */
47447
47448/* Size of the Bitvec structure in bytes. */
47449#define BITVEC_SZ512 512
47450
47451/* Round the union size down to the nearest pointer boundary, since that's how
47452** it will be aligned within the Bitvec struct. */
47453#define BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) \
47454 (((BITVEC_SZ512-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
47455
47456/* Type of the array "element" for the bitmap representation.
47457** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
47458** Setting this to the "natural word" size of your CPU may improve
47459** performance. */
47460#define BITVEC_TELEMu8 u8
47461/* Size, in bits, of the bitmap element. */
47462#define BITVEC_SZELEM8 8
47463/* Number of elements in a bitmap array. */
47464#define BITVEC_NELEM((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u8))
(BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof(BITVEC_TELEMu8))
47465/* Number of bits in the bitmap array. */
47466#define BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
(BITVEC_NELEM((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u8))
*BITVEC_SZELEM8)
47467
47468/* Number of u32 values in hash table. */
47469#define BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
(BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof(u32))
47470/* Maximum number of entries in hash table before
47471** sub-dividing and re-hashing. */
47472#define BITVEC_MXHASH(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u32))/2)
(BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
/2)
47473/* Hashing function for the aHash representation.
47474** Empirical testing showed that the *37 multiplier
47475** (an arbitrary prime)in the hash function provided
47476** no fewer collisions than the no-op *1. */
47477#define BITVEC_HASH(X)(((X)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec
*))/sizeof(u32)))
(((X)*1)%BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
)
47478
47479#define BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
(BITVEC_USIZE(((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof(Bitvec *))
47480
47481
47482/*
47483** A bitmap is an instance of the following structure.
47484**
47485** This bitmap records the existence of zero or more bits
47486** with values between 1 and iSize, inclusive.
47487**
47488** There are three possible representations of the bitmap.
47489** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
47490** bitmap. The least significant bit is bit 1.
47491**
47492** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
47493** a hash table that will hold up to BITVEC_MXHASH distinct values.
47494**
47495** Otherwise, the value i is redirected into one of BITVEC_NPTR
47496** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
47497** handles up to iDivisor separate values of i. apSub[0] holds
47498** values between 1 and iDivisor. apSub[1] holds values between
47499** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
47500** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
47501** to hold deal with values between 1 and iDivisor.
47502*/
47503struct Bitvec {
47504 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
47505 u32 nSet; /* Number of bits that are set - only valid for aHash
47506 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
47507 ** this would be 125. */
47508 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
47509 /* Should >=0 for apSub element. */
47510 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
47511 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
47512 union {
47513 BITVEC_TELEMu8 aBitmap[BITVEC_NELEM((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u8))
]; /* Bitmap representation */
47514 u32 aHash[BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
]; /* Hash table representation */
47515 Bitvec *apSub[BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
]; /* Recursive representation */
47516 } u;
47517};
47518
47519/*
47520** Create a new bitmap object able to handle bits between 0 and iSize,
47521** inclusive. Return a pointer to the new object. Return NULL if
47522** malloc fails.
47523*/
47524SQLITE_PRIVATEstatic Bitvec *sqlite3BitvecCreate(u32 iSize){
47525 Bitvec *p;
47526 assert( sizeof(*p)==BITVEC_SZ )((void) (0));
47527 p = sqlite3MallocZero( sizeof(*p) );
47528 if( p ){
47529 p->iSize = iSize;
47530 }
47531 return p;
47532}
47533
47534/*
47535** Check to see if the i-th bit is set. Return true or false.
47536** If p is NULL (if the bitmap has not been created) or if
47537** i is out of range, then return false.
47538*/
47539SQLITE_PRIVATEstatic int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
47540 assert( p!=0 )((void) (0));
47541 i--;
47542 if( i>=p->iSize ) return 0;
47543 while( p->iDivisor ){
47544 u32 bin = i/p->iDivisor;
47545 i = i%p->iDivisor;
47546 p = p->u.apSub[bin];
47547 if (!p) {
47548 return 0;
47549 }
47550 }
47551 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
47552 return (p->u.aBitmap[i/BITVEC_SZELEM8] & (1<<(i&(BITVEC_SZELEM8-1))))!=0;
47553 } else{
47554 u32 h = BITVEC_HASH(i++)(((i++)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(
Bitvec*))/sizeof(u32)))
;
47555 while( p->u.aHash[h] ){
47556 if( p->u.aHash[h]==i ) return 1;
47557 h = (h+1) % BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
;
47558 }
47559 return 0;
47560 }
47561}
47562SQLITE_PRIVATEstatic int sqlite3BitvecTest(Bitvec *p, u32 i){
47563 return p!=0 && sqlite3BitvecTestNotNull(p,i);
47564}
47565
47566/*
47567** Set the i-th bit. Return 0 on success and an error code if
47568** anything goes wrong.
47569**
47570** This routine might cause sub-bitmaps to be allocated. Failing
47571** to get the memory needed to hold the sub-bitmap is the only
47572** that can go wrong with an insert, assuming p and i are valid.
47573**
47574** The calling function must ensure that p is a valid Bitvec object
47575** and that the value for "i" is within range of the Bitvec object.
47576** Otherwise the behavior is undefined.
47577*/
47578SQLITE_PRIVATEstatic int sqlite3BitvecSet(Bitvec *p, u32 i){
47579 u32 h;
47580 if( p==0 ) return SQLITE_OK0;
47581 assert( i>0 )((void) (0));
47582 assert( i<=p->iSize )((void) (0));
47583 i--;
47584 while((p->iSize > BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
) && p->iDivisor) {
47585 u32 bin = i/p->iDivisor;
47586 i = i%p->iDivisor;
47587 if( p->u.apSub[bin]==0 ){
47588 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
47589 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT7;
47590 }
47591 p = p->u.apSub[bin];
47592 }
47593 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
47594 p->u.aBitmap[i/BITVEC_SZELEM8] |= 1 << (i&(BITVEC_SZELEM8-1));
47595 return SQLITE_OK0;
47596 }
47597 h = BITVEC_HASH(i++)(((i++)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(
Bitvec*))/sizeof(u32)))
;
47598 /* if there wasn't a hash collision, and this doesn't */
47599 /* completely fill the hash, then just add it without */
47600 /* worring about sub-dividing and re-hashing. */
47601 if( !p->u.aHash[h] ){
47602 if (p->nSet<(BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
-1)) {
47603 goto bitvec_set_end;
47604 } else {
47605 goto bitvec_set_rehash;
47606 }
47607 }
47608 /* there was a collision, check to see if it's already */
47609 /* in hash, if not, try to find a spot for it */
47610 do {
47611 if( p->u.aHash[h]==i ) return SQLITE_OK0;
47612 h++;
47613 if( h>=BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
) h = 0;
47614 } while( p->u.aHash[h] );
47615 /* we didn't find it in the hash. h points to the first */
47616 /* available free spot. check to see if this is going to */
47617 /* make our hash too "full". */
47618bitvec_set_rehash:
47619 if( p->nSet>=BITVEC_MXHASH(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u32))/2)
){
47620 unsigned int j;
47621 int rc;
47622 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash))sqlite3DbMallocRaw(0,sizeof(p->u.aHash));
47623 if( aiValues==0 ){
47624 return SQLITE_NOMEM_BKPT7;
47625 }else{
47626 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
47627 memset(p->u.apSub, 0, sizeof(p->u.apSub));
47628 p->iDivisor = (p->iSize + BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
- 1)/BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
;
47629 rc = sqlite3BitvecSet(p, i);
47630 for(j=0; j<BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
; j++){
47631 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
47632 }
47633 sqlite3StackFree(0, aiValues)sqlite3DbFree(0,aiValues);
47634 return rc;
47635 }
47636 }
47637bitvec_set_end:
47638 p->nSet++;
47639 p->u.aHash[h] = i;
47640 return SQLITE_OK0;
47641}
47642
47643/*
47644** Clear the i-th bit.
47645**
47646** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
47647** that BitvecClear can use to rebuilt its hash table.
47648*/
47649SQLITE_PRIVATEstatic void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
47650 if( p==0 ) return;
47651 assert( i>0 )((void) (0));
47652 i--;
47653 while( p->iDivisor ){
47654 u32 bin = i/p->iDivisor;
47655 i = i%p->iDivisor;
47656 p = p->u.apSub[bin];
47657 if (!p) {
47658 return;
47659 }
47660 }
47661 if( p->iSize<=BITVEC_NBIT(((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/
sizeof(u8))*8)
){
47662 p->u.aBitmap[i/BITVEC_SZELEM8] &= ~(1 << (i&(BITVEC_SZELEM8-1)));
47663 }else{
47664 unsigned int j;
47665 u32 *aiValues = pBuf;
47666 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
47667 memset(p->u.aHash, 0, sizeof(p->u.aHash));
47668 p->nSet = 0;
47669 for(j=0; j<BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
; j++){
47670 if( aiValues[j] && aiValues[j]!=(i+1) ){
47671 u32 h = BITVEC_HASH(aiValues[j]-1)(((aiValues[j]-1)*1)%((((512 -(3*sizeof(u32)))/sizeof(Bitvec*
))*sizeof(Bitvec*))/sizeof(u32)))
;
47672 p->nSet++;
47673 while( p->u.aHash[h] ){
47674 h++;
47675 if( h>=BITVEC_NINT((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(u32))
) h = 0;
47676 }
47677 p->u.aHash[h] = aiValues[j];
47678 }
47679 }
47680 }
47681}
47682
47683/*
47684** Destroy a bitmap object. Reclaim all memory used.
47685*/
47686SQLITE_PRIVATEstatic void sqlite3BitvecDestroy(Bitvec *p){
47687 if( p==0 ) return;
47688 if( p->iDivisor ){
47689 unsigned int i;
47690 for(i=0; i<BITVEC_NPTR((((512 -(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))/sizeof
(Bitvec *))
; i++){
47691 sqlite3BitvecDestroy(p->u.apSub[i]);
47692 }
47693 }
47694 sqlite3_free(p);
47695}
47696
47697/*
47698** Return the value of the iSize parameter specified when Bitvec *p
47699** was created.
47700*/
47701SQLITE_PRIVATEstatic u32 sqlite3BitvecSize(Bitvec *p){
47702 return p->iSize;
47703}
47704
47705#ifndef SQLITE_UNTESTABLE
47706/*
47707** Let V[] be an array of unsigned characters sufficient to hold
47708** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
47709** Then the following macros can be used to set, clear, or test
47710** individual bits within V.
47711*/
47712#define SETBIT(V,I)V[I>>3] |= (1<<(I&7)) V[I>>3] |= (1<<(I&7))
47713#define CLEARBIT(V,I)V[I>>3] &= ~(1<<(I&7)) V[I>>3] &= ~(1<<(I&7))
47714#define TESTBIT(V,I)(V[I>>3]&(1<<(I&7)))!=0 (V[I>>3]&(1<<(I&7)))!=0
47715
47716/*
47717** This routine runs an extensive test of the Bitvec code.
47718**
47719** The input is an array of integers that acts as a program
47720** to test the Bitvec. The integers are opcodes followed
47721** by 0, 1, or 3 operands, depending on the opcode. Another
47722** opcode follows immediately after the last operand.
47723**
47724** There are 6 opcodes numbered from 0 through 5. 0 is the
47725** "halt" opcode and causes the test to end.
47726**
47727** 0 Halt and return the number of errors
47728** 1 N S X Set N bits beginning with S and incrementing by X
47729** 2 N S X Clear N bits beginning with S and incrementing by X
47730** 3 N Set N randomly chosen bits
47731** 4 N Clear N randomly chosen bits
47732** 5 N S X Set N bits from S increment X in array only, not in bitvec
47733**
47734** The opcodes 1 through 4 perform set and clear operations are performed
47735** on both a Bitvec object and on a linear array of bits obtained from malloc.
47736** Opcode 5 works on the linear array only, not on the Bitvec.
47737** Opcode 5 is used to deliberately induce a fault in order to
47738** confirm that error detection works.
47739**
47740** At the conclusion of the test the linear array is compared
47741** against the Bitvec object. If there are any differences,
47742** an error is returned. If they are the same, zero is returned.
47743**
47744** If a memory allocation error occurs, return -1.
47745*/
47746SQLITE_PRIVATEstatic int sqlite3BitvecBuiltinTest(int sz, int *aOp){
47747 Bitvec *pBitvec = 0;
47748 unsigned char *pV = 0;
47749 int rc = -1;
47750 int i, nx, pc, op;
47751 void *pTmpSpace;
47752
47753 /* Allocate the Bitvec to be tested and a linear array of
47754 ** bits to act as the reference */
47755 pBitvec = sqlite3BitvecCreate( sz );
47756 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
47757 pTmpSpace = sqlite3_malloc64(BITVEC_SZ512);
47758 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
47759
47760 /* NULL pBitvec tests */
47761 sqlite3BitvecSet(0, 1);
47762 sqlite3BitvecClear(0, 1, pTmpSpace);
47763
47764 /* Run the program */
47765 pc = 0;
47766 while( (op = aOp[pc])!=0 ){
47767 switch( op ){
47768 case 1:
47769 case 2:
47770 case 5: {
47771 nx = 4;
47772 i = aOp[pc+2] - 1;
47773 aOp[pc+2] += aOp[pc+3];
47774 break;
47775 }
47776 case 3:
47777 case 4:
47778 default: {
47779 nx = 2;
47780 sqlite3_randomness(sizeof(i), &i);
47781 break;
47782 }
47783 }
47784 if( (--aOp[pc+1]) > 0 ) nx = 0;
47785 pc += nx;
47786 i = (i & 0x7fffffff)%sz;
47787 if( (op & 1)!=0 ){
47788 SETBIT(pV, (i+1))pV[(i+1)>>3] |= (1<<((i+1)&7));
47789 if( op!=5 ){
47790 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
47791 }
47792 }else{
47793 CLEARBIT(pV, (i+1))pV[(i+1)>>3] &= ~(1<<((i+1)&7));
47794 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
47795 }
47796 }
47797
47798 /* Test to make sure the linear array exactly matches the
47799 ** Bitvec object. Start with the assumption that they do
47800 ** match (rc==0). Change rc to non-zero if a discrepancy
47801 ** is found.
47802 */
47803 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
47804 + sqlite3BitvecTest(pBitvec, 0)
47805 + (sqlite3BitvecSize(pBitvec) - sz);
47806 for(i=1; i<=sz; i++){
47807 if( (TESTBIT(pV,i)(pV[i>>3]&(1<<(i&7)))!=0)!=sqlite3BitvecTest(pBitvec,i) ){
47808 rc = i;
47809 break;
47810 }
47811 }
47812
47813 /* Free allocated structure */
47814bitvec_end:
47815 sqlite3_free(pTmpSpace);
47816 sqlite3_free(pV);
47817 sqlite3BitvecDestroy(pBitvec);
47818 return rc;
47819}
47820#endif /* SQLITE_UNTESTABLE */
47821
47822/************** End of bitvec.c **********************************************/
47823/************** Begin file pcache.c ******************************************/
47824/*
47825** 2008 August 05
47826**
47827** The author disclaims copyright to this source code. In place of
47828** a legal notice, here is a blessing:
47829**
47830** May you do good and not evil.
47831** May you find forgiveness for yourself and forgive others.
47832** May you share freely, never taking more than you give.
47833**
47834*************************************************************************
47835** This file implements that page cache.
47836*/
47837/* #include "sqliteInt.h" */
47838
47839/*
47840** A complete page cache is an instance of this structure. Every
47841** entry in the cache holds a single page of the database file. The
47842** btree layer only operates on the cached copy of the database pages.
47843**
47844** A page cache entry is "clean" if it exactly matches what is currently
47845** on disk. A page is "dirty" if it has been modified and needs to be
47846** persisted to disk.
47847**
47848** pDirty, pDirtyTail, pSynced:
47849** All dirty pages are linked into the doubly linked list using
47850** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
47851** such that p was added to the list more recently than p->pDirtyNext.
47852** PCache.pDirty points to the first (newest) element in the list and
47853** pDirtyTail to the last (oldest).
47854**
47855** The PCache.pSynced variable is used to optimize searching for a dirty
47856** page to eject from the cache mid-transaction. It is better to eject
47857** a page that does not require a journal sync than one that does.
47858** Therefore, pSynced is maintained so that it *almost* always points
47859** to either the oldest page in the pDirty/pDirtyTail list that has a
47860** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
47861** (so that the right page to eject can be found by following pDirtyPrev
47862** pointers).
47863*/
47864struct PCache {
47865 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
47866 PgHdr *pSynced; /* Last synced page in dirty page list */
47867 int nRefSum; /* Sum of ref counts over all pages */
47868 int szCache; /* Configured cache size */
47869 int szSpill; /* Size before spilling occurs */
47870 int szPage; /* Size of every page in this cache */
47871 int szExtra; /* Size of extra space for each page */
47872 u8 bPurgeable; /* True if pages are on backing store */
47873 u8 eCreate; /* eCreate value for for xFetch() */
47874 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
47875 void *pStress; /* Argument to xStress */
47876 sqlite3_pcache *pCache; /* Pluggable cache module */
47877};
47878
47879/********************************** Test and Debug Logic **********************/
47880/*
47881** Debug tracing macros. Enable by by changing the "0" to "1" and
47882** recompiling.
47883**
47884** When sqlite3PcacheTrace is 1, single line trace messages are issued.
47885** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
47886** is displayed for many operations, resulting in a lot of output.
47887*/
47888#if defined(SQLITE_DEBUG) && 0
47889 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
47890 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
47891# define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
47892 void pcacheDump(PCache *pCache){
47893 int N;
47894 int i, j;
47895 sqlite3_pcache_page *pLower;
47896 PgHdr *pPg;
47897 unsigned char *a;
47898
47899 if( sqlite3PcacheTrace<2 ) return;
47900 if( pCache->pCache==0 ) return;
47901 N = sqlite3PcachePagecount(pCache);
47902 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
47903 for(i=1; i<=N; i++){
47904 pLower = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, i, 0);
47905 if( pLower==0 ) continue;
47906 pPg = (PgHdr*)pLower->pExtra;
47907 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
47908 a = (unsigned char *)pLower->pBuf;
47909 for(j=0; j<12; j++) printf("%02x", a[j]);
47910 printf("\n");
47911 if( pPg->pPage==0 ){
47912 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(pCache->pCache, pLower, 0);
47913 }
47914 }
47915 }
47916 #else
47917# define pcacheTrace(X)
47918# define pcacheDump(X)
47919#endif
47920
47921/*
47922** Check invariants on a PgHdr entry. Return true if everything is OK.
47923** Return false if any invariant is violated.
47924**
47925** This routine is for use inside of assert() statements only. For
47926** example:
47927**
47928** assert( sqlite3PcachePageSanity(pPg) );
47929*/
47930#ifdef SQLITE_DEBUG
47931SQLITE_PRIVATEstatic int sqlite3PcachePageSanity(PgHdr *pPg){
47932 PCache *pCache;
47933 assert( pPg!=0 )((void) (0));
47934 assert( pPg->pgno>0 || pPg->pPager==0 )((void) (0)); /* Page number is 1 or more */
47935 pCache = pPg->pCache;
47936 assert( pCache!=0 )((void) (0)); /* Every page has an associated PCache */
47937 if( pPg->flags & PGHDR_CLEAN0x001 ){
47938 assert( (pPg->flags & PGHDR_DIRTY)==0 )((void) (0));/* Cannot be both CLEAN and DIRTY */
47939 assert( pCache->pDirty!=pPg )((void) (0)); /* CLEAN pages not on dirty list */
47940 assert( pCache->pDirtyTail!=pPg )((void) (0));
47941 }
47942 /* WRITEABLE pages must also be DIRTY */
47943 if( pPg->flags & PGHDR_WRITEABLE0x004 ){
47944 assert( pPg->flags & PGHDR_DIRTY )((void) (0)); /* WRITEABLE implies DIRTY */
47945 }
47946 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
47947 ** for example, when using the sqlite3PagerDontWrite() optimization:
47948 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
47949 ** (2) Page X moved to freelist, WRITEABLE is cleared
47950 ** (3) Page X reused, WRITEABLE is set again
47951 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
47952 ** in step 3, and page might be written into the database without first
47953 ** syncing the rollback journal, which might cause corruption on a power
47954 ** loss.
47955 **
47956 ** Another example is when the database page size is smaller than the
47957 ** disk sector size. When any page of a sector is journalled, all pages
47958 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
47959 ** in case they are later modified, since all pages in the same sector
47960 ** must be journalled and synced before any of those pages can be safely
47961 ** written.
47962 */
47963 return 1;
47964}
47965#endif /* SQLITE_DEBUG */
47966
47967
47968/********************************** Linked List Management ********************/
47969
47970/* Allowed values for second argument to pcacheManageDirtyList() */
47971#define PCACHE_DIRTYLIST_REMOVE1 1 /* Remove pPage from dirty list */
47972#define PCACHE_DIRTYLIST_ADD2 2 /* Add pPage to the dirty list */
47973#define PCACHE_DIRTYLIST_FRONT3 3 /* Move pPage to the front of the list */
47974
47975/*
47976** Manage pPage's participation on the dirty list. Bits of the addRemove
47977** argument determines what operation to do. The 0x01 bit means first
47978** remove pPage from the dirty list. The 0x02 means add pPage back to
47979** the dirty list. Doing both moves pPage to the front of the dirty list.
47980*/
47981static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
47982 PCache *p = pPage->pCache;
47983
47984 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
47985 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
47986 pPage->pgno));
47987 if( addRemove & PCACHE_DIRTYLIST_REMOVE1 ){
47988 assert( pPage->pDirtyNext || pPage==p->pDirtyTail )((void) (0));
47989 assert( pPage->pDirtyPrev || pPage==p->pDirty )((void) (0));
47990
47991 /* Update the PCache1.pSynced variable if necessary. */
47992 if( p->pSynced==pPage ){
47993 p->pSynced = pPage->pDirtyPrev;
47994 }
47995
47996 if( pPage->pDirtyNext ){
47997 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
47998 }else{
47999 assert( pPage==p->pDirtyTail )((void) (0));
48000 p->pDirtyTail = pPage->pDirtyPrev;
48001 }
48002 if( pPage->pDirtyPrev ){
48003 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
48004 }else{
48005 /* If there are now no dirty pages in the cache, set eCreate to 2.
48006 ** This is an optimization that allows sqlite3PcacheFetch() to skip
48007 ** searching for a dirty page to eject from the cache when it might
48008 ** otherwise have to. */
48009 assert( pPage==p->pDirty )((void) (0));
48010 p->pDirty = pPage->pDirtyNext;
48011 assert( p->bPurgeable || p->eCreate==2 )((void) (0));
48012 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
48013 assert( p->bPurgeable==0 || p->eCreate==1 )((void) (0));
48014 p->eCreate = 2;
48015 }
48016 }
48017 }
48018 if( addRemove & PCACHE_DIRTYLIST_ADD2 ){
48019 pPage->pDirtyPrev = 0;
48020 pPage->pDirtyNext = p->pDirty;
48021 if( pPage->pDirtyNext ){
48022 assert( pPage->pDirtyNext->pDirtyPrev==0 )((void) (0));
48023 pPage->pDirtyNext->pDirtyPrev = pPage;
48024 }else{
48025 p->pDirtyTail = pPage;
48026 if( p->bPurgeable ){
48027 assert( p->eCreate==2 )((void) (0));
48028 p->eCreate = 1;
48029 }
48030 }
48031 p->pDirty = pPage;
48032
48033 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
48034 ** pSynced to point to it. Checking the NEED_SYNC flag is an
48035 ** optimization, as if pSynced points to a page with the NEED_SYNC
48036 ** flag set sqlite3PcacheFetchStress() searches through all newer
48037 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
48038 if( !p->pSynced
48039 && 0==(pPage->flags&PGHDR_NEED_SYNC0x008) /*OPTIMIZATION-IF-FALSE*/
48040 ){
48041 p->pSynced = pPage;
48042 }
48043 }
48044 pcacheDump(p);
48045}
48046
48047/*
48048** Wrapper around the pluggable caches xUnpin method. If the cache is
48049** being used for an in-memory database, this function is a no-op.
48050*/
48051static void pcacheUnpin(PgHdr *p){
48052 if( p->pCache->bPurgeable ){
48053 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
48054 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
48055 pcacheDump(p->pCache);
48056 }
48057}
48058
48059/*
48060** Compute the number of pages of cache requested. p->szCache is the
48061** cache size requested by the "PRAGMA cache_size" statement.
48062*/
48063static int numberOfCachePages(PCache *p){
48064 if( p->szCache>=0 ){
48065 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
48066 ** suggested cache size is set to N. */
48067 return p->szCache;
48068 }else{
48069 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
48070 ** number of cache pages is adjusted to be a number of pages that would
48071 ** use approximately abs(N*1024) bytes of memory based on the current
48072 ** page size. */
48073 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
48074 }
48075}
48076
48077/*************************************************** General Interfaces ******
48078**
48079** Initialize and shutdown the page cache subsystem. Neither of these
48080** functions are threadsafe.
48081*/
48082SQLITE_PRIVATEstatic int sqlite3PcacheInitialize(void){
48083 if( sqlite3GlobalConfigsqlite3Config.pcache2.xInit==0 ){
48084 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
48085 ** built-in default page cache is used instead of the application defined
48086 ** page cache. */
48087 sqlite3PCacheSetDefault();
48088 }
48089 return sqlite3GlobalConfigsqlite3Config.pcache2.xInit(sqlite3GlobalConfigsqlite3Config.pcache2.pArg);
48090}
48091SQLITE_PRIVATEstatic void sqlite3PcacheShutdown(void){
48092 if( sqlite3GlobalConfigsqlite3Config.pcache2.xShutdown ){
48093 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
48094 sqlite3GlobalConfigsqlite3Config.pcache2.xShutdown(sqlite3GlobalConfigsqlite3Config.pcache2.pArg);
48095 }
48096}
48097
48098/*
48099** Return the size in bytes of a PCache object.
48100*/
48101SQLITE_PRIVATEstatic int sqlite3PcacheSize(void){ return sizeof(PCache); }
48102
48103/*
48104** Create a new PCache object. Storage space to hold the object
48105** has already been allocated and is passed in as the p pointer.
48106** The caller discovers how much space needs to be allocated by
48107** calling sqlite3PcacheSize().
48108**
48109** szExtra is some extra space allocated for each page. The first
48110** 8 bytes of the extra space will be zeroed as the page is allocated,
48111** but remaining content will be uninitialized. Though it is opaque
48112** to this module, the extra space really ends up being the MemPage
48113** structure in the pager.
48114*/
48115SQLITE_PRIVATEstatic int sqlite3PcacheOpen(
48116 int szPage, /* Size of every page */
48117 int szExtra, /* Extra space associated with each page */
48118 int bPurgeable, /* True if pages are on backing store */
48119 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
48120 void *pStress, /* Argument to xStress */
48121 PCache *p /* Preallocated space for the PCache */
48122){
48123 memset(p, 0, sizeof(PCache));
48124 p->szPage = 1;
48125 p->szExtra = szExtra;
48126 assert( szExtra>=8 )((void) (0)); /* First 8 bytes will be zeroed */
48127 p->bPurgeable = bPurgeable;
48128 p->eCreate = 2;
48129 p->xStress = xStress;
48130 p->pStress = pStress;
48131 p->szCache = 100;
48132 p->szSpill = 1;
48133 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
48134 return sqlite3PcacheSetPageSize(p, szPage);
48135}
48136
48137/*
48138** Change the page size for PCache object. The caller must ensure that there
48139** are no outstanding page references when this function is called.
48140*/
48141SQLITE_PRIVATEstatic int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
48142 assert( pCache->nRefSum==0 && pCache->pDirty==0 )((void) (0));
48143 if( pCache->szPage ){
48144 sqlite3_pcache *pNew;
48145 pNew = sqlite3GlobalConfigsqlite3Config.pcache2.xCreate(
48146 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr))(((sizeof(PgHdr))+7)&~7),
48147 pCache->bPurgeable
48148 );
48149 if( pNew==0 ) return SQLITE_NOMEM_BKPT7;
48150 sqlite3GlobalConfigsqlite3Config.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
48151 if( pCache->pCache ){
48152 sqlite3GlobalConfigsqlite3Config.pcache2.xDestroy(pCache->pCache);
48153 }
48154 pCache->pCache = pNew;
48155 pCache->szPage = szPage;
48156 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
48157 }
48158 return SQLITE_OK0;
48159}
48160
48161/*
48162** Try to obtain a page from the cache.
48163**
48164** This routine returns a pointer to an sqlite3_pcache_page object if
48165** such an object is already in cache, or if a new one is created.
48166** This routine returns a NULL pointer if the object was not in cache
48167** and could not be created.
48168**
48169** The createFlags should be 0 to check for existing pages and should
48170** be 3 (not 1, but 3) to try to create a new page.
48171**
48172** If the createFlag is 0, then NULL is always returned if the page
48173** is not already in the cache. If createFlag is 1, then a new page
48174** is created only if that can be done without spilling dirty pages
48175** and without exceeding the cache size limit.
48176**
48177** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
48178** initialize the sqlite3_pcache_page object and convert it into a
48179** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
48180** routines are split this way for performance reasons. When separated
48181** they can both (usually) operate without having to push values to
48182** the stack on entry and pop them back off on exit, which saves a
48183** lot of pushing and popping.
48184*/
48185SQLITE_PRIVATEstatic sqlite3_pcache_page *sqlite3PcacheFetch(
48186 PCache *pCache, /* Obtain the page from this cache */
48187 Pgno pgno, /* Page number to obtain */
48188 int createFlag /* If true, create page if it does not exist already */
48189){
48190 int eCreate;
48191 sqlite3_pcache_page *pRes;
48192
48193 assert( pCache!=0 )((void) (0));
48194 assert( pCache->pCache!=0 )((void) (0));
48195 assert( createFlag==3 || createFlag==0 )((void) (0));
48196 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) )((void) (0));
48197
48198 /* eCreate defines what to do if the page does not exist.
48199 ** 0 Do not allocate a new page. (createFlag==0)
48200 ** 1 Allocate a new page if doing so is inexpensive.
48201 ** (createFlag==1 AND bPurgeable AND pDirty)
48202 ** 2 Allocate a new page even it doing so is difficult.
48203 ** (createFlag==1 AND !(bPurgeable AND pDirty)
48204 */
48205 eCreate = createFlag & pCache->eCreate;
48206 assert( eCreate==0 || eCreate==1 || eCreate==2 )((void) (0));
48207 assert( createFlag==0 || pCache->eCreate==eCreate )((void) (0));
48208 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) )((void) (0));
48209 pRes = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, pgno, eCreate);
48210 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
48211 createFlag?" create":"",pRes));
48212 return pRes;
48213}
48214
48215/*
48216** If the sqlite3PcacheFetch() routine is unable to allocate a new
48217** page because no clean pages are available for reuse and the cache
48218** size limit has been reached, then this routine can be invoked to
48219** try harder to allocate a page. This routine might invoke the stress
48220** callback to spill dirty pages to the journal. It will then try to
48221** allocate the new page and will only fail to allocate a new page on
48222** an OOM error.
48223**
48224** This routine should be invoked only after sqlite3PcacheFetch() fails.
48225*/
48226SQLITE_PRIVATEstatic int sqlite3PcacheFetchStress(
48227 PCache *pCache, /* Obtain the page from this cache */
48228 Pgno pgno, /* Page number to obtain */
48229 sqlite3_pcache_page **ppPage /* Write result here */
48230){
48231 PgHdr *pPg;
48232 if( pCache->eCreate==2 ) return 0;
48233
48234 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
48235 /* Find a dirty page to write-out and recycle. First try to find a
48236 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
48237 ** cleared), but if that is not possible settle for any other
48238 ** unreferenced dirty page.
48239 **
48240 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
48241 ** flag is currently referenced, then the following may leave pSynced
48242 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
48243 ** cleared). This is Ok, as pSynced is just an optimization. */
48244 for(pPg=pCache->pSynced;
48245 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC0x008));
48246 pPg=pPg->pDirtyPrev
48247 );
48248 pCache->pSynced = pPg;
48249 if( !pPg ){
48250 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
48251 }
48252 if( pPg ){
48253 int rc;
48254#ifdef SQLITE_LOG_CACHE_SPILL
48255 sqlite3_log(SQLITE_FULL13,
48256 "spill page %d making room for %d - cache used: %d/%d",
48257 pPg->pgno, pgno,
48258 sqlite3GlobalConfigsqlite3Config.pcache2.xPagecount(pCache->pCache),
48259 numberOfCachePages(pCache));
48260#endif
48261 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
48262 rc = pCache->xStress(pCache->pStress, pPg);
48263 pcacheDump(pCache);
48264 if( rc!=SQLITE_OK0 && rc!=SQLITE_BUSY5 ){
48265 return rc;
48266 }
48267 }
48268 }
48269 *ppPage = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache, pgno, 2);
48270 return *ppPage==0 ? SQLITE_NOMEM_BKPT7 : SQLITE_OK0;
48271}
48272
48273/*
48274** This is a helper routine for sqlite3PcacheFetchFinish()
48275**
48276** In the uncommon case where the page being fetched has not been
48277** initialized, this routine is invoked to do the initialization.
48278** This routine is broken out into a separate function since it
48279** requires extra stack manipulation that can be avoided in the common
48280** case.
48281*/
48282static SQLITE_NOINLINE__attribute__((noinline)) PgHdr *pcacheFetchFinishWithInit(
48283 PCache *pCache, /* Obtain the page from this cache */
48284 Pgno pgno, /* Page number obtained */
48285 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
48286){
48287 PgHdr *pPgHdr;
48288 assert( pPage!=0 )((void) (0));
48289 pPgHdr = (PgHdr*)pPage->pExtra;
48290 assert( pPgHdr->pPage==0 )((void) (0));
48291 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty)__builtin_offsetof(PgHdr, pDirty));
48292 pPgHdr->pPage = pPage;
48293 pPgHdr->pData = pPage->pBuf;
48294 pPgHdr->pExtra = (void *)&pPgHdr[1];
48295 memset(pPgHdr->pExtra, 0, 8);
48296 pPgHdr->pCache = pCache;
48297 pPgHdr->pgno = pgno;
48298 pPgHdr->flags = PGHDR_CLEAN0x001;
48299 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
48300}
48301
48302/*
48303** This routine converts the sqlite3_pcache_page object returned by
48304** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
48305** must be called after sqlite3PcacheFetch() in order to get a usable
48306** result.
48307*/
48308SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheFetchFinish(
48309 PCache *pCache, /* Obtain the page from this cache */
48310 Pgno pgno, /* Page number obtained */
48311 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
48312){
48313 PgHdr *pPgHdr;
48314
48315 assert( pPage!=0 )((void) (0));
48316 pPgHdr = (PgHdr *)pPage->pExtra;
48317
48318 if( !pPgHdr->pPage ){
48319 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
48320 }
48321 pCache->nRefSum++;
48322 pPgHdr->nRef++;
48323 assert( sqlite3PcachePageSanity(pPgHdr) )((void) (0));
48324 return pPgHdr;
48325}
48326
48327/*
48328** Decrement the reference count on a page. If the page is clean and the
48329** reference count drops to 0, then it is made eligible for recycling.
48330*/
48331SQLITE_PRIVATEstatic void SQLITE_NOINLINE__attribute__((noinline)) sqlite3PcacheRelease(PgHdr *p){
48332 assert( p->nRef>0 )((void) (0));
48333 p->pCache->nRefSum--;
48334 if( (--p->nRef)==0 ){
48335 if( p->flags&PGHDR_CLEAN0x001 ){
48336 pcacheUnpin(p);
48337 }else{
48338 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT3);
48339 }
48340 }
48341}
48342
48343/*
48344** Increase the reference count of a supplied page by 1.
48345*/
48346SQLITE_PRIVATEstatic void sqlite3PcacheRef(PgHdr *p){
48347 assert(p->nRef>0)((void) (0));
48348 assert( sqlite3PcachePageSanity(p) )((void) (0));
48349 p->nRef++;
48350 p->pCache->nRefSum++;
48351}
48352
48353/*
48354** Drop a page from the cache. There must be exactly one reference to the
48355** page. This function deletes that reference, so after it returns the
48356** page pointed to by p is invalid.
48357*/
48358SQLITE_PRIVATEstatic void sqlite3PcacheDrop(PgHdr *p){
48359 assert( p->nRef==1 )((void) (0));
48360 assert( sqlite3PcachePageSanity(p) )((void) (0));
48361 if( p->flags&PGHDR_DIRTY0x002 ){
48362 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE1);
48363 }
48364 p->pCache->nRefSum--;
48365 sqlite3GlobalConfigsqlite3Config.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
48366}
48367
48368/*
48369** Make sure the page is marked as dirty. If it isn't dirty already,
48370** make it so.
48371*/
48372SQLITE_PRIVATEstatic void sqlite3PcacheMakeDirty(PgHdr *p){
48373 assert( p->nRef>0 )((void) (0));
48374 assert( sqlite3PcachePageSanity(p) )((void) (0));
48375 if( p->flags & (PGHDR_CLEAN0x001|PGHDR_DONT_WRITE0x010) ){ /*OPTIMIZATION-IF-FALSE*/
48376 p->flags &= ~PGHDR_DONT_WRITE0x010;
48377 if( p->flags & PGHDR_CLEAN0x001 ){
48378 p->flags ^= (PGHDR_DIRTY0x002|PGHDR_CLEAN0x001);
48379 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
48380 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY )((void) (0));
48381 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD2);
48382 }
48383 assert( sqlite3PcachePageSanity(p) )((void) (0));
48384 }
48385}
48386
48387/*
48388** Make sure the page is marked as clean. If it isn't clean already,
48389** make it so.
48390*/
48391SQLITE_PRIVATEstatic void sqlite3PcacheMakeClean(PgHdr *p){
48392 assert( sqlite3PcachePageSanity(p) )((void) (0));
48393 assert( (p->flags & PGHDR_DIRTY)!=0 )((void) (0));
48394 assert( (p->flags & PGHDR_CLEAN)==0 )((void) (0));
48395 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE1);
48396 p->flags &= ~(PGHDR_DIRTY0x002|PGHDR_NEED_SYNC0x008|PGHDR_WRITEABLE0x004);
48397 p->flags |= PGHDR_CLEAN0x001;
48398 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
48399 assert( sqlite3PcachePageSanity(p) )((void) (0));
48400 if( p->nRef==0 ){
48401 pcacheUnpin(p);
48402 }
48403}
48404
48405/*
48406** Make every page in the cache clean.
48407*/
48408SQLITE_PRIVATEstatic void sqlite3PcacheCleanAll(PCache *pCache){
48409 PgHdr *p;
48410 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
48411 while( (p = pCache->pDirty)!=0 ){
48412 sqlite3PcacheMakeClean(p);
48413 }
48414}
48415
48416/*
48417** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
48418*/
48419SQLITE_PRIVATEstatic void sqlite3PcacheClearWritable(PCache *pCache){
48420 PgHdr *p;
48421 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
48422 for(p=pCache->pDirty; p; p=p->pDirtyNext){
48423 p->flags &= ~(PGHDR_NEED_SYNC0x008|PGHDR_WRITEABLE0x004);
48424 }
48425 pCache->pSynced = pCache->pDirtyTail;
48426}
48427
48428/*
48429** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
48430*/
48431SQLITE_PRIVATEstatic void sqlite3PcacheClearSyncFlags(PCache *pCache){
48432 PgHdr *p;
48433 for(p=pCache->pDirty; p; p=p->pDirtyNext){
48434 p->flags &= ~PGHDR_NEED_SYNC0x008;
48435 }
48436 pCache->pSynced = pCache->pDirtyTail;
48437}
48438
48439/*
48440** Change the page number of page p to newPgno.
48441*/
48442SQLITE_PRIVATEstatic void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
48443 PCache *pCache = p->pCache;
48444 assert( p->nRef>0 )((void) (0));
48445 assert( newPgno>0 )((void) (0));
48446 assert( sqlite3PcachePageSanity(p) )((void) (0));
48447 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
48448 sqlite3GlobalConfigsqlite3Config.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
48449 p->pgno = newPgno;
48450 if( (p->flags&PGHDR_DIRTY0x002) && (p->flags&PGHDR_NEED_SYNC0x008) ){
48451 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT3);
48452 }
48453}
48454
48455/*
48456** Drop every cache entry whose page number is greater than "pgno". The
48457** caller must ensure that there are no outstanding references to any pages
48458** other than page 1 with a page number greater than pgno.
48459**
48460** If there is a reference to page 1 and the pgno parameter passed to this
48461** function is 0, then the data area associated with page 1 is zeroed, but
48462** the page object is not dropped.
48463*/
48464SQLITE_PRIVATEstatic void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
48465 if( pCache->pCache ){
48466 PgHdr *p;
48467 PgHdr *pNext;
48468 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
48469 for(p=pCache->pDirty; p; p=pNext){
48470 pNext = p->pDirtyNext;
48471 /* This routine never gets call with a positive pgno except right
48472 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
48473 ** it must be that pgno==0.
48474 */
48475 assert( p->pgno>0 )((void) (0));
48476 if( p->pgno>pgno ){
48477 assert( p->flags&PGHDR_DIRTY )((void) (0));
48478 sqlite3PcacheMakeClean(p);
48479 }
48480 }
48481 if( pgno==0 && pCache->nRefSum ){
48482 sqlite3_pcache_page *pPage1;
48483 pPage1 = sqlite3GlobalConfigsqlite3Config.pcache2.xFetch(pCache->pCache,1,0);
48484 if( ALWAYS(pPage1)(pPage1) ){ /* Page 1 is always available in cache, because
48485 ** pCache->nRefSum>0 */
48486 memset(pPage1->pBuf, 0, pCache->szPage);
48487 pgno = 1;
48488 }
48489 }
48490 sqlite3GlobalConfigsqlite3Config.pcache2.xTruncate(pCache->pCache, pgno+1);
48491 }
48492}
48493
48494/*
48495** Close a cache.
48496*/
48497SQLITE_PRIVATEstatic void sqlite3PcacheClose(PCache *pCache){
48498 assert( pCache->pCache!=0 )((void) (0));
48499 pcacheTrace(("%p.CLOSE\n",pCache));
48500 sqlite3GlobalConfigsqlite3Config.pcache2.xDestroy(pCache->pCache);
48501}
48502
48503/*
48504** Discard the contents of the cache.
48505*/
48506SQLITE_PRIVATEstatic void sqlite3PcacheClear(PCache *pCache){
48507 sqlite3PcacheTruncate(pCache, 0);
48508}
48509
48510/*
48511** Merge two lists of pages connected by pDirty and in pgno order.
48512** Do not bother fixing the pDirtyPrev pointers.
48513*/
48514static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
48515 PgHdr result, *pTail;
48516 pTail = &result;
48517 assert( pA!=0 && pB!=0 )((void) (0));
48518 for(;;){
48519 if( pA->pgno<pB->pgno ){
48520 pTail->pDirty = pA;
48521 pTail = pA;
48522 pA = pA->pDirty;
48523 if( pA==0 ){
48524 pTail->pDirty = pB;
48525 break;
48526 }
48527 }else{
48528 pTail->pDirty = pB;
48529 pTail = pB;
48530 pB = pB->pDirty;
48531 if( pB==0 ){
48532 pTail->pDirty = pA;
48533 break;
48534 }
48535 }
48536 }
48537 return result.pDirty;
48538}
48539
48540/*
48541** Sort the list of pages in accending order by pgno. Pages are
48542** connected by pDirty pointers. The pDirtyPrev pointers are
48543** corrupted by this sort.
48544**
48545** Since there cannot be more than 2^31 distinct pages in a database,
48546** there cannot be more than 31 buckets required by the merge sorter.
48547** One extra bucket is added to catch overflow in case something
48548** ever changes to make the previous sentence incorrect.
48549*/
48550#define N_SORT_BUCKET32 32
48551static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
48552 PgHdr *a[N_SORT_BUCKET32], *p;
48553 int i;
48554 memset(a, 0, sizeof(a));
48555 while( pIn ){
48556 p = pIn;
48557 pIn = p->pDirty;
48558 p->pDirty = 0;
48559 for(i=0; ALWAYS(i<N_SORT_BUCKET-1)(i<32 -1); i++){
48560 if( a[i]==0 ){
48561 a[i] = p;
48562 break;
48563 }else{
48564 p = pcacheMergeDirtyList(a[i], p);
48565 a[i] = 0;
48566 }
48567 }
48568 if( NEVER(i==N_SORT_BUCKET-1)(i==32 -1) ){
48569 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
48570 ** the input list. But that is impossible.
48571 */
48572 a[i] = pcacheMergeDirtyList(a[i], p);
48573 }
48574 }
48575 p = a[0];
48576 for(i=1; i<N_SORT_BUCKET32; i++){
48577 if( a[i]==0 ) continue;
48578 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
48579 }
48580 return p;
48581}
48582
48583/*
48584** Return a list of all dirty pages in the cache, sorted by page number.
48585*/
48586SQLITE_PRIVATEstatic PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
48587 PgHdr *p;
48588 for(p=pCache->pDirty; p; p=p->pDirtyNext){
48589 p->pDirty = p->pDirtyNext;
48590 }
48591 return pcacheSortDirtyList(pCache->pDirty);
48592}
48593
48594/*
48595** Return the total number of references to all pages held by the cache.
48596**
48597** This is not the total number of pages referenced, but the sum of the
48598** reference count for all pages.
48599*/
48600SQLITE_PRIVATEstatic int sqlite3PcacheRefCount(PCache *pCache){
48601 return pCache->nRefSum;
48602}
48603
48604/*
48605** Return the number of references to the page supplied as an argument.
48606*/
48607SQLITE_PRIVATEstatic int sqlite3PcachePageRefcount(PgHdr *p){
48608 return p->nRef;
48609}
48610
48611/*
48612** Return the total number of pages in the cache.
48613*/
48614SQLITE_PRIVATEstatic int sqlite3PcachePagecount(PCache *pCache){
48615 assert( pCache->pCache!=0 )((void) (0));
48616 return sqlite3GlobalConfigsqlite3Config.pcache2.xPagecount(pCache->pCache);
48617}
48618
48619#ifdef SQLITE_TEST
48620/*
48621** Get the suggested cache-size value.
48622*/
48623SQLITE_PRIVATEstatic int sqlite3PcacheGetCachesize(PCache *pCache){
48624 return numberOfCachePages(pCache);
48625}
48626#endif
48627
48628/*
48629** Set the suggested cache-size value.
48630*/
48631SQLITE_PRIVATEstatic void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
48632 assert( pCache->pCache!=0 )((void) (0));
48633 pCache->szCache = mxPage;
48634 sqlite3GlobalConfigsqlite3Config.pcache2.xCachesize(pCache->pCache,
48635 numberOfCachePages(pCache));
48636}
48637
48638/*
48639** Set the suggested cache-spill value. Make no changes if if the
48640** argument is zero. Return the effective cache-spill size, which will
48641** be the larger of the szSpill and szCache.
48642*/
48643SQLITE_PRIVATEstatic int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
48644 int res;
48645 assert( p->pCache!=0 )((void) (0));
48646 if( mxPage ){
48647 if( mxPage<0 ){
48648 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
48649 }
48650 p->szSpill = mxPage;
48651 }
48652 res = numberOfCachePages(p);
48653 if( res<p->szSpill ) res = p->szSpill;
48654 return res;
48655}
48656
48657/*
48658** Free up as much memory as possible from the page cache.
48659*/
48660SQLITE_PRIVATEstatic void sqlite3PcacheShrink(PCache *pCache){
48661 assert( pCache->pCache!=0 )((void) (0));
48662 sqlite3GlobalConfigsqlite3Config.pcache2.xShrink(pCache->pCache);
48663}
48664
48665/*
48666** Return the size of the header added by this middleware layer
48667** in the page-cache hierarchy.
48668*/
48669SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr))(((sizeof(PgHdr))+7)&~7); }
48670
48671/*
48672** Return the number of dirty pages currently in the cache, as a percentage
48673** of the configured cache size.
48674*/
48675SQLITE_PRIVATEstatic int sqlite3PCachePercentDirty(PCache *pCache){
48676 PgHdr *pDirty;
48677 int nDirty = 0;
48678 int nCache = numberOfCachePages(pCache);
48679 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
48680 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
48681}
48682
48683#ifdef SQLITE_DIRECT_OVERFLOW_READ
48684/*
48685** Return true if there are one or more dirty pages in the cache. Else false.
48686*/
48687SQLITE_PRIVATEstatic int sqlite3PCacheIsDirty(PCache *pCache){
48688 return (pCache->pDirty!=0);
48689}
48690#endif
48691
48692#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
48693/*
48694** For all dirty pages currently in the cache, invoke the specified
48695** callback. This is only used if the SQLITE_CHECK_PAGES macro is
48696** defined.
48697*/
48698SQLITE_PRIVATEstatic void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
48699 PgHdr *pDirty;
48700 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
48701 xIter(pDirty);
48702 }
48703}
48704#endif
48705
48706/************** End of pcache.c **********************************************/
48707/************** Begin file pcache1.c *****************************************/
48708/*
48709** 2008 November 05
48710**
48711** The author disclaims copyright to this source code. In place of
48712** a legal notice, here is a blessing:
48713**
48714** May you do good and not evil.
48715** May you find forgiveness for yourself and forgive others.
48716** May you share freely, never taking more than you give.
48717**
48718*************************************************************************
48719**
48720** This file implements the default page cache implementation (the
48721** sqlite3_pcache interface). It also contains part of the implementation
48722** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
48723** If the default page cache implementation is overridden, then neither of
48724** these two features are available.
48725**
48726** A Page cache line looks like this:
48727**
48728** -------------------------------------------------------------
48729** | database page content | PgHdr1 | MemPage | PgHdr |
48730** -------------------------------------------------------------
48731**
48732** The database page content is up front (so that buffer overreads tend to
48733** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage
48734** is the extension added by the btree.c module containing information such
48735** as the database page number and how that database page is used. PgHdr
48736** is added by the pcache.c layer and contains information used to keep track
48737** of which pages are "dirty". PgHdr1 is an extension added by this
48738** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page.
48739** PgHdr1 contains information needed to look up a page by its page number.
48740** The superclass sqlite3_pcache_page.pBuf points to the start of the
48741** database page content and sqlite3_pcache_page.pExtra points to PgHdr.
48742**
48743** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at
48744** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The
48745** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this
48746** size can vary according to architecture, compile-time options, and
48747** SQLite library version number.
48748**
48749** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained
48750** using a separate memory allocation from the database page content. This
48751** seeks to overcome the "clownshoe" problem (also called "internal
48752** fragmentation" in academic literature) of allocating a few bytes more
48753** than a power of two with the memory allocator rounding up to the next
48754** power of two, and leaving the rounded-up space unused.
48755**
48756** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates
48757** with this module. Information is passed back and forth as PgHdr1 pointers.
48758**
48759** The pcache.c and pager.c modules deal pointers to PgHdr objects.
48760** The btree.c module deals with pointers to MemPage objects.
48761**
48762** SOURCE OF PAGE CACHE MEMORY:
48763**
48764** Memory for a page might come from any of three sources:
48765**
48766** (1) The general-purpose memory allocator - sqlite3Malloc()
48767** (2) Global page-cache memory provided using sqlite3_config() with
48768** SQLITE_CONFIG_PAGECACHE.
48769** (3) PCache-local bulk allocation.
48770**
48771** The third case is a chunk of heap memory (defaulting to 100 pages worth)
48772** that is allocated when the page cache is created. The size of the local
48773** bulk allocation can be adjusted using
48774**
48775** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N).
48776**
48777** If N is positive, then N pages worth of memory are allocated using a single
48778** sqlite3Malloc() call and that memory is used for the first N pages allocated.
48779** Or if N is negative, then -1024*N bytes of memory are allocated and used
48780** for as many pages as can be accomodated.
48781**
48782** Only one of (2) or (3) can be used. Once the memory available to (2) or
48783** (3) is exhausted, subsequent allocations fail over to the general-purpose
48784** memory allocator (1).
48785**
48786** Earlier versions of SQLite used only methods (1) and (2). But experiments
48787** show that method (3) with N==100 provides about a 5% performance boost for
48788** common workloads.
48789*/
48790/* #include "sqliteInt.h" */
48791
48792typedef struct PCache1 PCache1;
48793typedef struct PgHdr1 PgHdr1;
48794typedef struct PgFreeslot PgFreeslot;
48795typedef struct PGroup PGroup;
48796
48797/*
48798** Each cache entry is represented by an instance of the following
48799** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
48800** PgHdr1.pCache->szPage bytes is allocated directly before this structure
48801** in memory.
48802**
48803** Note: Variables isBulkLocal and isAnchor were once type "u8". That works,
48804** but causes a 2-byte gap in the structure for most architectures (since
48805** pointers must be either 4 or 8-byte aligned). As this structure is located
48806** in memory directly after the associated page data, if the database is
48807** corrupt, code at the b-tree layer may overread the page buffer and
48808** read part of this structure before the corruption is detected. This
48809** can cause a valgrind error if the unitialized gap is accessed. Using u16
48810** ensures there is no such gap, and therefore no bytes of unitialized memory
48811** in the structure.
48812*/
48813struct PgHdr1 {
48814 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */
48815 unsigned int iKey; /* Key value (page number) */
48816 u16 isBulkLocal; /* This page from bulk local storage */
48817 u16 isAnchor; /* This is the PGroup.lru element */
48818 PgHdr1 *pNext; /* Next in hash table chain */
48819 PCache1 *pCache; /* Cache that currently owns this page */
48820 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
48821 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
48822 /* NB: pLruPrev is only valid if pLruNext!=0 */
48823};
48824
48825/*
48826** A page is pinned if it is not on the LRU list. To be "pinned" means
48827** that the page is in active use and must not be deallocated.
48828*/
48829#define PAGE_IS_PINNED(p)((p)->pLruNext==0) ((p)->pLruNext==0)
48830#define PAGE_IS_UNPINNED(p)((p)->pLruNext!=0) ((p)->pLruNext!=0)
48831
48832/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
48833** of one or more PCaches that are able to recycle each other's unpinned
48834** pages when they are under memory pressure. A PGroup is an instance of
48835** the following object.
48836**
48837** This page cache implementation works in one of two modes:
48838**
48839** (1) Every PCache is the sole member of its own PGroup. There is
48840** one PGroup per PCache.
48841**
48842** (2) There is a single global PGroup that all PCaches are a member
48843** of.
48844**
48845** Mode 1 uses more memory (since PCache instances are not able to rob
48846** unused pages from other PCaches) but it also operates without a mutex,
48847** and is therefore often faster. Mode 2 requires a mutex in order to be
48848** threadsafe, but recycles pages more efficiently.
48849**
48850** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
48851** PGroup which is the pcache1.grp global variable and its mutex is
48852** SQLITE_MUTEX_STATIC_LRU.
48853*/
48854struct PGroup {
48855 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
48856 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
48857 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
48858 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
48859 unsigned int nPurgeable; /* Number of purgeable pages allocated */
48860 PgHdr1 lru; /* The beginning and end of the LRU list */
48861};
48862
48863/* Each page cache is an instance of the following object. Every
48864** open database file (including each in-memory database and each
48865** temporary or transient database) has a single page cache which
48866** is an instance of this object.
48867**
48868** Pointers to structures of this type are cast and returned as
48869** opaque sqlite3_pcache* handles.
48870*/
48871struct PCache1 {
48872 /* Cache configuration parameters. Page size (szPage) and the purgeable
48873 ** flag (bPurgeable) and the pnPurgeable pointer are all set when the
48874 ** cache is created and are never changed thereafter. nMax may be
48875 ** modified at any time by a call to the pcache1Cachesize() method.
48876 ** The PGroup mutex must be held when accessing nMax.
48877 */
48878 PGroup *pGroup; /* PGroup this cache belongs to */
48879 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
48880 int szPage; /* Size of database content section */
48881 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */
48882 int szAlloc; /* Total size of one pcache line */
48883 int bPurgeable; /* True if cache is purgeable */
48884 unsigned int nMin; /* Minimum number of pages reserved */
48885 unsigned int nMax; /* Configured "cache_size" value */
48886 unsigned int n90pct; /* nMax*9/10 */
48887 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
48888 unsigned int nPurgeableDummy; /* pnPurgeable points here when not used*/
48889
48890 /* Hash table of all pages. The following variables may only be accessed
48891 ** when the accessor is holding the PGroup mutex.
48892 */
48893 unsigned int nRecyclable; /* Number of pages in the LRU list */
48894 unsigned int nPage; /* Total number of pages in apHash */
48895 unsigned int nHash; /* Number of slots in apHash[] */
48896 PgHdr1 **apHash; /* Hash table for fast lookup by key */
48897 PgHdr1 *pFree; /* List of unused pcache-local pages */
48898 void *pBulk; /* Bulk memory used by pcache-local */
48899};
48900
48901/*
48902** Free slots in the allocator used to divide up the global page cache
48903** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism.
48904*/
48905struct PgFreeslot {
48906 PgFreeslot *pNext; /* Next free slot */
48907};
48908
48909/*
48910** Global data used by this cache.
48911*/
48912static SQLITE_WSD struct PCacheGlobal {
48913 PGroup grp; /* The global PGroup for mode (2) */
48914
48915 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The
48916 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
48917 ** fixed at sqlite3_initialize() time and do not require mutex protection.
48918 ** The nFreeSlot and pFree values do require mutex protection.
48919 */
48920 int isInit; /* True if initialized */
48921 int separateCache; /* Use a new PGroup for each PCache */
48922 int nInitPage; /* Initial bulk allocation size */
48923 int szSlot; /* Size of each free slot */
48924 int nSlot; /* The number of pcache slots */
48925 int nReserve; /* Try to keep nFreeSlot above this */
48926 void *pStart, *pEnd; /* Bounds of global page cache memory */
48927 /* Above requires no mutex. Use mutex below for variable that follow. */
48928 sqlite3_mutex *mutex; /* Mutex for accessing the following: */
48929 PgFreeslot *pFree; /* Free page blocks */
48930 int nFreeSlot; /* Number of unused pcache slots */
48931 /* The following value requires a mutex to change. We skip the mutex on
48932 ** reading because (1) most platforms read a 32-bit integer atomically and
48933 ** (2) even if an incorrect value is read, no great harm is done since this
48934 ** is really just an optimization. */
48935 int bUnderPressure; /* True if low on PAGECACHE memory */
48936} pcache1_g;
48937
48938/*
48939** All code in this file should access the global structure above via the
48940** alias "pcache1". This ensures that the WSD emulation is used when
48941** compiling for systems that do not support real WSD.
48942*/
48943#define pcache1(pcache1_g) (GLOBAL(struct PCacheGlobal, pcache1_g)pcache1_g)
48944
48945/*
48946** Macros to enter and leave the PCache LRU mutex.
48947*/
48948#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE1==0
48949# define pcache1EnterMutex(X)((void) (0)) assert((X)->mutex==0)((void) (0))
48950# define pcache1LeaveMutex(X)((void) (0)) assert((X)->mutex==0)((void) (0))
48951# define PCACHE1_MIGHT_USE_GROUP_MUTEX0 0
48952#else
48953# define pcache1EnterMutex(X)((void) (0)) sqlite3_mutex_enter((X)->mutex)
48954# define pcache1LeaveMutex(X)((void) (0)) sqlite3_mutex_leave((X)->mutex)
48955# define PCACHE1_MIGHT_USE_GROUP_MUTEX0 1
48956#endif
48957
48958/******************************************************************************/
48959/******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
48960
48961
48962/*
48963** This function is called during initialization if a static buffer is
48964** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
48965** verb to sqlite3_config(). Parameter pBuf points to an allocation large
48966** enough to contain 'n' buffers of 'sz' bytes each.
48967**
48968** This routine is called from sqlite3_initialize() and so it is guaranteed
48969** to be serialized already. There is no need for further mutexing.
48970*/
48971SQLITE_PRIVATEstatic void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
48972 if( pcache1(pcache1_g).isInit ){
48973 PgFreeslot *p;
48974 if( pBuf==0 ) sz = n = 0;
48975 if( n==0 ) sz = 0;
48976 sz = ROUNDDOWN8(sz)((sz)&~7);
48977 pcache1(pcache1_g).szSlot = sz;
48978 pcache1(pcache1_g).nSlot = pcache1(pcache1_g).nFreeSlot = n;
48979 pcache1(pcache1_g).nReserve = n>90 ? 10 : (n/10 + 1);
48980 pcache1(pcache1_g).pStart = pBuf;
48981 pcache1(pcache1_g).pFree = 0;
48982 pcache1(pcache1_g).bUnderPressure = 0;
48983 while( n-- ){
48984 p = (PgFreeslot*)pBuf;
48985 p->pNext = pcache1(pcache1_g).pFree;
48986 pcache1(pcache1_g).pFree = p;
48987 pBuf = (void*)&((char*)pBuf)[sz];
48988 }
48989 pcache1(pcache1_g).pEnd = pBuf;
48990 }
48991}
48992
48993/*
48994** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
48995** true if pCache->pFree ends up containing one or more free pages.
48996*/
48997static int pcache1InitBulk(PCache1 *pCache){
48998 i64 szBulk;
48999 char *zBulk;
49000 if( pcache1(pcache1_g).nInitPage==0 ) return 0;
49001 /* Do not bother with a bulk allocation if the cache size very small */
49002 if( pCache->nMax<3 ) return 0;
49003 sqlite3BeginBenignMalloc();
49004 if( pcache1(pcache1_g).nInitPage>0 ){
49005 szBulk = pCache->szAlloc * (i64)pcache1(pcache1_g).nInitPage;
49006 }else{
49007 szBulk = -1024 * (i64)pcache1(pcache1_g).nInitPage;
49008 }
49009 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
49010 szBulk = pCache->szAlloc*(i64)pCache->nMax;
49011 }
49012 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
49013 sqlite3EndBenignMalloc();
49014 if( zBulk ){
49015 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
49016 do{
49017 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
49018 pX->page.pBuf = zBulk;
49019 pX->page.pExtra = &pX[1];
49020 pX->isBulkLocal = 1;
49021 pX->isAnchor = 0;
49022 pX->pNext = pCache->pFree;
49023 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
49024 pCache->pFree = pX;
49025 zBulk += pCache->szAlloc;
49026 }while( --nBulk );
49027 }
49028 return pCache->pFree!=0;
49029}
49030
49031/*
49032** Malloc function used within this file to allocate space from the buffer
49033** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
49034** such buffer exists or there is no space left in it, this function falls
49035** back to sqlite3Malloc().
49036**
49037** Multiple threads can run this routine at the same time. Global variables
49038** in pcache1 need to be protected via mutex.
49039*/
49040static void *pcache1Alloc(int nByte){
49041 void *p = 0;
49042 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) )((void) (0));
49043 if( nByte<=pcache1(pcache1_g).szSlot ){
49044 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
49045 p = (PgHdr1 *)pcache1(pcache1_g).pFree;
49046 if( p ){
49047 pcache1(pcache1_g).pFree = pcache1(pcache1_g).pFree->pNext;
49048 pcache1(pcache1_g).nFreeSlot--;
49049 pcache1(pcache1_g).bUnderPressure = pcache1(pcache1_g).nFreeSlot<pcache1(pcache1_g).nReserve;
49050 assert( pcache1.nFreeSlot>=0 )((void) (0));
49051 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE7, nByte);
49052 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED1, 1);
49053 }
49054 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
49055 }
49056 if( p==0 ){
49057 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get
49058 ** it from sqlite3Malloc instead.
49059 */
49060 p = sqlite3Malloc(nByte);
49061#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
49062 if( p ){
49063 int sz = sqlite3MallocSize(p);
49064 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
49065 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE7, nByte);
49066 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW2, sz);
49067 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
49068 }
49069#endif
49070 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
49071 }
49072 return p;
49073}
49074
49075/*
49076** Free an allocated buffer obtained from pcache1Alloc().
49077*/
49078static void pcache1Free(void *p){
49079 if( p==0 ) return;
49080 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd)(((uptr)(p)>=(uptr)((pcache1_g).pStart))&&((uptr)(
p)<(uptr)((pcache1_g).pEnd)))
){
49081 PgFreeslot *pSlot;
49082 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
49083 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED1, 1);
49084 pSlot = (PgFreeslot*)p;
49085 pSlot->pNext = pcache1(pcache1_g).pFree;
49086 pcache1(pcache1_g).pFree = pSlot;
49087 pcache1(pcache1_g).nFreeSlot++;
49088 pcache1(pcache1_g).bUnderPressure = pcache1(pcache1_g).nFreeSlot<pcache1(pcache1_g).nReserve;
49089 assert( pcache1.nFreeSlot<=pcache1.nSlot )((void) (0));
49090 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
49091 }else{
49092 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) )((void) (0));
49093 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
49094#ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
49095 {
49096 int nFreed = 0;
49097 nFreed = sqlite3MallocSize(p);
49098 sqlite3_mutex_enter(pcache1(pcache1_g).mutex);
49099 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW2, nFreed);
49100 sqlite3_mutex_leave(pcache1(pcache1_g).mutex);
49101 }
49102#endif
49103 sqlite3_free(p);
49104 }
49105}
49106
49107#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
49108/*
49109** Return the size of a pcache allocation
49110*/
49111static int pcache1MemSize(void *p){
49112 if( p>=pcache1(pcache1_g).pStart && p<pcache1(pcache1_g).pEnd ){
49113 return pcache1(pcache1_g).szSlot;
49114 }else{
49115 int iSize;
49116 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) )((void) (0));
49117 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
49118 iSize = sqlite3MallocSize(p);
49119 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
49120 return iSize;
49121 }
49122}
49123#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
49124
49125/*
49126** Allocate a new page object initially associated with cache pCache.
49127*/
49128static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
49129 PgHdr1 *p = 0;
49130 void *pPg;
49131
49132 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
49133 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
49134 p = pCache->pFree;
49135 pCache->pFree = p->pNext;
49136 p->pNext = 0;
49137 }else{
49138#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
49139 /* The group mutex must be released before pcache1Alloc() is called. This
49140 ** is because it might call sqlite3_release_memory(), which assumes that
49141 ** this mutex is not held. */
49142 assert( pcache1.separateCache==0 )((void) (0));
49143 assert( pCache->pGroup==&pcache1.grp )((void) (0));
49144 pcache1LeaveMutex(pCache->pGroup)((void) (0));
49145#endif
49146 if( benignMalloc ){ sqlite3BeginBenignMalloc(); }
49147#ifdef SQLITE_PCACHE_SEPARATE_HEADER
49148 pPg = pcache1Alloc(pCache->szPage);
49149 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
49150 if( !pPg || !p ){
49151 pcache1Free(pPg);
49152 sqlite3_free(p);
49153 pPg = 0;
49154 }
49155#else
49156 pPg = pcache1Alloc(pCache->szAlloc);
49157 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
49158#endif
49159 if( benignMalloc ){ sqlite3EndBenignMalloc(); }
49160#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
49161 pcache1EnterMutex(pCache->pGroup)((void) (0));
49162#endif
49163 if( pPg==0 ) return 0;
49164 p->page.pBuf = pPg;
49165 p->page.pExtra = &p[1];
49166 p->isBulkLocal = 0;
49167 p->isAnchor = 0;
49168 }
49169 (*pCache->pnPurgeable)++;
49170 return p;
49171}
49172
49173/*
49174** Free a page object allocated by pcache1AllocPage().
49175*/
49176static void pcache1FreePage(PgHdr1 *p){
49177 PCache1 *pCache;
49178 assert( p!=0 )((void) (0));
49179 pCache = p->pCache;
49180 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) )((void) (0));
49181 if( p->isBulkLocal ){
49182 p->pNext = pCache->pFree;
49183 pCache->pFree = p;
49184 }else{
49185 pcache1Free(p->page.pBuf);
49186#ifdef SQLITE_PCACHE_SEPARATE_HEADER
49187 sqlite3_free(p);
49188#endif
49189 }
49190 (*pCache->pnPurgeable)--;
49191}
49192
49193/*
49194** Malloc function used by SQLite to obtain space from the buffer configured
49195** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
49196** exists, this function falls back to sqlite3Malloc().
49197*/
49198SQLITE_PRIVATEstatic void *sqlite3PageMalloc(int sz){
49199 assert( sz<=65536+8 )((void) (0)); /* These allocations are never very large */
49200 return pcache1Alloc(sz);
49201}
49202
49203/*
49204** Free an allocated buffer obtained from sqlite3PageMalloc().
49205*/
49206SQLITE_PRIVATEstatic void sqlite3PageFree(void *p){
49207 pcache1Free(p);
49208}
49209
49210
49211/*
49212** Return true if it desirable to avoid allocating a new page cache
49213** entry.
49214**
49215** If memory was allocated specifically to the page cache using
49216** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
49217** it is desirable to avoid allocating a new page cache entry because
49218** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
49219** for all page cache needs and we should not need to spill the
49220** allocation onto the heap.
49221**
49222** Or, the heap is used for all page cache memory but the heap is
49223** under memory pressure, then again it is desirable to avoid
49224** allocating a new page cache entry in order to avoid stressing
49225** the heap even further.
49226*/
49227static int pcache1UnderMemoryPressure(PCache1 *pCache){
49228 if( pcache1(pcache1_g).nSlot && (pCache->szPage+pCache->szExtra)<=pcache1(pcache1_g).szSlot ){
49229 return pcache1(pcache1_g).bUnderPressure;
49230 }else{
49231 return sqlite3HeapNearlyFull();
49232 }
49233}
49234
49235/******************************************************************************/
49236/******** General Implementation Functions ************************************/
49237
49238/*
49239** This function is used to resize the hash table used by the cache passed
49240** as the first argument.
49241**
49242** The PCache mutex must be held when this function is called.
49243*/
49244static void pcache1ResizeHash(PCache1 *p){
49245 PgHdr1 **apNew;
49246 unsigned int nNew;
49247 unsigned int i;
49248
49249 assert( sqlite3_mutex_held(p->pGroup->mutex) )((void) (0));
49250
49251 nNew = p->nHash*2;
49252 if( nNew<256 ){
49253 nNew = 256;
49254 }
49255
49256 pcache1LeaveMutex(p->pGroup)((void) (0));
49257 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
49258 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
49259 if( p->nHash ){ sqlite3EndBenignMalloc(); }
49260 pcache1EnterMutex(p->pGroup)((void) (0));
49261 if( apNew ){
49262 for(i=0; i<p->nHash; i++){
49263 PgHdr1 *pPage;
49264 PgHdr1 *pNext = p->apHash[i];
49265 while( (pPage = pNext)!=0 ){
49266 unsigned int h = pPage->iKey % nNew;
49267 pNext = pPage->pNext;
49268 pPage->pNext = apNew[h];
49269 apNew[h] = pPage;
49270 }
49271 }
49272 sqlite3_free(p->apHash);
49273 p->apHash = apNew;
49274 p->nHash = nNew;
49275 }
49276}
49277
49278/*
49279** This function is used internally to remove the page pPage from the
49280** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
49281** LRU list, then this function is a no-op.
49282**
49283** The PGroup mutex must be held when this function is called.
49284*/
49285static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){
49286 assert( pPage!=0 )((void) (0));
49287 assert( PAGE_IS_UNPINNED(pPage) )((void) (0));
49288 assert( pPage->pLruNext )((void) (0));
49289 assert( pPage->pLruPrev )((void) (0));
49290 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) )((void) (0));
49291 pPage->pLruPrev->pLruNext = pPage->pLruNext;
49292 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
49293 pPage->pLruNext = 0;
49294 /* pPage->pLruPrev = 0;
49295 ** No need to clear pLruPrev as it is never accessed if pLruNext is 0 */
49296 assert( pPage->isAnchor==0 )((void) (0));
49297 assert( pPage->pCache->pGroup->lru.isAnchor==1 )((void) (0));
49298 pPage->pCache->nRecyclable--;
49299 return pPage;
49300}
49301
49302
49303/*
49304** Remove the page supplied as an argument from the hash table
49305** (PCache1.apHash structure) that it is currently stored in.
49306** Also free the page if freePage is true.
49307**
49308** The PGroup mutex must be held when this function is called.
49309*/
49310static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){
49311 unsigned int h;
49312 PCache1 *pCache = pPage->pCache;
49313 PgHdr1 **pp;
49314
49315 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
49316 h = pPage->iKey % pCache->nHash;
49317 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
49318 *pp = (*pp)->pNext;
49319
49320 pCache->nPage--;
49321 if( freeFlag ) pcache1FreePage(pPage);
49322}
49323
49324/*
49325** If there are currently more than nMaxPage pages allocated, try
49326** to recycle pages to reduce the number allocated to nMaxPage.
49327*/
49328static void pcache1EnforceMaxPage(PCache1 *pCache){
49329 PGroup *pGroup = pCache->pGroup;
49330 PgHdr1 *p;
49331 assert( sqlite3_mutex_held(pGroup->mutex) )((void) (0));
49332 while( pGroup->nPurgeable>pGroup->nMaxPage
49333 && (p=pGroup->lru.pLruPrev)->isAnchor==0
49334 ){
49335 assert( p->pCache->pGroup==pGroup )((void) (0));
49336 assert( PAGE_IS_UNPINNED(p) )((void) (0));
49337 pcache1PinPage(p);
49338 pcache1RemoveFromHash(p, 1);
49339 }
49340 if( pCache->nPage==0 && pCache->pBulk ){
49341 sqlite3_free(pCache->pBulk);
49342 pCache->pBulk = pCache->pFree = 0;
49343 }
49344}
49345
49346/*
49347** Discard all pages from cache pCache with a page number (key value)
49348** greater than or equal to iLimit. Any pinned pages that meet this
49349** criteria are unpinned before they are discarded.
49350**
49351** The PCache mutex must be held when this function is called.
49352*/
49353static void pcache1TruncateUnsafe(
49354 PCache1 *pCache, /* The cache to truncate */
49355 unsigned int iLimit /* Drop pages with this pgno or larger */
49356){
49357 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
49358 unsigned int h, iStop;
49359 assert( sqlite3_mutex_held(pCache->pGroup->mutex) )((void) (0));
49360 assert( pCache->iMaxKey >= iLimit )((void) (0));
49361 assert( pCache->nHash > 0 )((void) (0));
49362 if( pCache->iMaxKey - iLimit < pCache->nHash ){
49363 /* If we are just shaving the last few pages off the end of the
49364 ** cache, then there is no point in scanning the entire hash table.
49365 ** Only scan those hash slots that might contain pages that need to
49366 ** be removed. */
49367 h = iLimit % pCache->nHash;
49368 iStop = pCache->iMaxKey % pCache->nHash;
49369 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
49370 }else{
49371 /* This is the general case where many pages are being removed.
49372 ** It is necessary to scan the entire hash table */
49373 h = pCache->nHash/2;
49374 iStop = h - 1;
49375 }
49376 for(;;){
49377 PgHdr1 **pp;
49378 PgHdr1 *pPage;
49379 assert( h<pCache->nHash )((void) (0));
49380 pp = &pCache->apHash[h];
49381 while( (pPage = *pp)!=0 ){
49382 if( pPage->iKey>=iLimit ){
49383 pCache->nPage--;
49384 *pp = pPage->pNext;
49385 if( PAGE_IS_UNPINNED(pPage)((pPage)->pLruNext!=0) ) pcache1PinPage(pPage);
49386 pcache1FreePage(pPage);
49387 }else{
49388 pp = &pPage->pNext;
49389 TESTONLY( if( nPage>=0 ) nPage++; )
49390 }
49391 }
49392 if( h==iStop ) break;
49393 h = (h+1) % pCache->nHash;
49394 }
49395 assert( nPage<0 || pCache->nPage==(unsigned)nPage )((void) (0));
49396}
49397
49398/******************************************************************************/
49399/******** sqlite3_pcache Methods **********************************************/
49400
49401/*
49402** Implementation of the sqlite3_pcache.xInit method.
49403*/
49404static int pcache1Init(void *NotUsed){
49405 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
49406 assert( pcache1.isInit==0 )((void) (0));
49407 memset(&pcache1(pcache1_g), 0, sizeof(pcache1(pcache1_g)));
49408
49409
49410 /*
49411 ** The pcache1.separateCache variable is true if each PCache has its own
49412 ** private PGroup (mode-1). pcache1.separateCache is false if the single
49413 ** PGroup in pcache1.grp is used for all page caches (mode-2).
49414 **
49415 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
49416 **
49417 ** * Use a unified cache in single-threaded applications that have
49418 ** configured a start-time buffer for use as page-cache memory using
49419 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
49420 ** pBuf argument.
49421 **
49422 ** * Otherwise use separate caches (mode-1)
49423 */
49424#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT)
49425 pcache1(pcache1_g).separateCache = 0;
49426#elif SQLITE_THREADSAFE1
49427 pcache1(pcache1_g).separateCache = sqlite3GlobalConfigsqlite3Config.pPage==0
49428 || sqlite3GlobalConfigsqlite3Config.bCoreMutex>0;
49429#else
49430 pcache1(pcache1_g).separateCache = sqlite3GlobalConfigsqlite3Config.pPage==0;
49431#endif
49432
49433#if SQLITE_THREADSAFE1
49434 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
49435 pcache1(pcache1_g).grp.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_LRU6);
49436 pcache1(pcache1_g).mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PMEM7);
49437 }
49438#endif
49439 if( pcache1(pcache1_g).separateCache
49440 && sqlite3GlobalConfigsqlite3Config.nPage!=0
49441 && sqlite3GlobalConfigsqlite3Config.pPage==0
49442 ){
49443 pcache1(pcache1_g).nInitPage = sqlite3GlobalConfigsqlite3Config.nPage;
49444 }else{
49445 pcache1(pcache1_g).nInitPage = 0;
49446 }
49447 pcache1(pcache1_g).grp.mxPinned = 10;
49448 pcache1(pcache1_g).isInit = 1;
49449 return SQLITE_OK0;
49450}
49451
49452/*
49453** Implementation of the sqlite3_pcache.xShutdown method.
49454** Note that the static mutex allocated in xInit does
49455** not need to be freed.
49456*/
49457static void pcache1Shutdown(void *NotUsed){
49458 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
49459 assert( pcache1.isInit!=0 )((void) (0));
49460 memset(&pcache1(pcache1_g), 0, sizeof(pcache1(pcache1_g)));
49461}
49462
49463/* forward declaration */
49464static void pcache1Destroy(sqlite3_pcache *p);
49465
49466/*
49467** Implementation of the sqlite3_pcache.xCreate method.
49468**
49469** Allocate a new cache.
49470*/
49471static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
49472 PCache1 *pCache; /* The newly created page cache */
49473 PGroup *pGroup; /* The group the new page cache will belong to */
49474 int sz; /* Bytes of memory required to allocate the new cache */
49475
49476 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 )((void) (0));
49477 assert( szExtra < 300 )((void) (0));
49478
49479 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1(pcache1_g).separateCache;
49480 pCache = (PCache1 *)sqlite3MallocZero(sz);
49481 if( pCache ){
49482 if( pcache1(pcache1_g).separateCache ){
49483 pGroup = (PGroup*)&pCache[1];
49484 pGroup->mxPinned = 10;
49485 }else{
49486 pGroup = &pcache1(pcache1_g).grp;
49487 }
49488 pcache1EnterMutex(pGroup)((void) (0));
49489 if( pGroup->lru.isAnchor==0 ){
49490 pGroup->lru.isAnchor = 1;
49491 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
49492 }
49493 pCache->pGroup = pGroup;
49494 pCache->szPage = szPage;
49495 pCache->szExtra = szExtra;
49496 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1))(((sizeof(PgHdr1))+7)&~7);
49497 pCache->bPurgeable = (bPurgeable ? 1 : 0);
49498 pcache1ResizeHash(pCache);
49499 if( bPurgeable ){
49500 pCache->nMin = 10;
49501 pGroup->nMinPage += pCache->nMin;
49502 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
49503 pCache->pnPurgeable = &pGroup->nPurgeable;
49504 }else{
49505 pCache->pnPurgeable = &pCache->nPurgeableDummy;
49506 }
49507 pcache1LeaveMutex(pGroup)((void) (0));
49508 if( pCache->nHash==0 ){
49509 pcache1Destroy((sqlite3_pcache*)pCache);
49510 pCache = 0;
49511 }
49512 }
49513 return (sqlite3_pcache *)pCache;
49514}
49515
49516/*
49517** Implementation of the sqlite3_pcache.xCachesize method.
49518**
49519** Configure the cache_size limit for a cache.
49520*/
49521static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
49522 PCache1 *pCache = (PCache1 *)p;
49523 if( pCache->bPurgeable ){
49524 PGroup *pGroup = pCache->pGroup;
49525 pcache1EnterMutex(pGroup)((void) (0));
49526 pGroup->nMaxPage += (nMax - pCache->nMax);
49527 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
49528 pCache->nMax = nMax;
49529 pCache->n90pct = pCache->nMax*9/10;
49530 pcache1EnforceMaxPage(pCache);
49531 pcache1LeaveMutex(pGroup)((void) (0));
49532 }
49533}
49534
49535/*
49536** Implementation of the sqlite3_pcache.xShrink method.
49537**
49538** Free up as much memory as possible.
49539*/
49540static void pcache1Shrink(sqlite3_pcache *p){
49541 PCache1 *pCache = (PCache1*)p;
49542 if( pCache->bPurgeable ){
49543 PGroup *pGroup = pCache->pGroup;
49544 int savedMaxPage;
49545 pcache1EnterMutex(pGroup)((void) (0));
49546 savedMaxPage = pGroup->nMaxPage;
49547 pGroup->nMaxPage = 0;
49548 pcache1EnforceMaxPage(pCache);
49549 pGroup->nMaxPage = savedMaxPage;
49550 pcache1LeaveMutex(pGroup)((void) (0));
49551 }
49552}
49553
49554/*
49555** Implementation of the sqlite3_pcache.xPagecount method.
49556*/
49557static int pcache1Pagecount(sqlite3_pcache *p){
49558 int n;
49559 PCache1 *pCache = (PCache1*)p;
49560 pcache1EnterMutex(pCache->pGroup)((void) (0));
49561 n = pCache->nPage;
49562 pcache1LeaveMutex(pCache->pGroup)((void) (0));
49563 return n;
49564}
49565
49566
49567/*
49568** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described
49569** in the header of the pcache1Fetch() procedure.
49570**
49571** This steps are broken out into a separate procedure because they are
49572** usually not needed, and by avoiding the stack initialization required
49573** for these steps, the main pcache1Fetch() procedure can run faster.
49574*/
49575static SQLITE_NOINLINE__attribute__((noinline)) PgHdr1 *pcache1FetchStage2(
49576 PCache1 *pCache,
49577 unsigned int iKey,
49578 int createFlag
49579){
49580 unsigned int nPinned;
49581 PGroup *pGroup = pCache->pGroup;
49582 PgHdr1 *pPage = 0;
49583
49584 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
49585 assert( pCache->nPage >= pCache->nRecyclable )((void) (0));
49586 nPinned = pCache->nPage - pCache->nRecyclable;
49587 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage )((void) (0));
49588 assert( pCache->n90pct == pCache->nMax*9/10 )((void) (0));
49589 if( createFlag==1 && (
49590 nPinned>=pGroup->mxPinned
49591 || nPinned>=pCache->n90pct
49592 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
49593 )){
49594 return 0;
49595 }
49596
49597 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
49598 assert( pCache->nHash>0 && pCache->apHash )((void) (0));
49599
49600 /* Step 4. Try to recycle a page. */
49601 if( pCache->bPurgeable
49602 && !pGroup->lru.pLruPrev->isAnchor
49603 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
49604 ){
49605 PCache1 *pOther;
49606 pPage = pGroup->lru.pLruPrev;
49607 assert( PAGE_IS_UNPINNED(pPage) )((void) (0));
49608 pcache1RemoveFromHash(pPage, 0);
49609 pcache1PinPage(pPage);
49610 pOther = pPage->pCache;
49611 if( pOther->szAlloc != pCache->szAlloc ){
49612 pcache1FreePage(pPage);
49613 pPage = 0;
49614 }else{
49615 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
49616 }
49617 }
49618
49619 /* Step 5. If a usable page buffer has still not been found,
49620 ** attempt to allocate a new one.
49621 */
49622 if( !pPage ){
49623 pPage = pcache1AllocPage(pCache, createFlag==1);
49624 }
49625
49626 if( pPage ){
49627 unsigned int h = iKey % pCache->nHash;
49628 pCache->nPage++;
49629 pPage->iKey = iKey;
49630 pPage->pNext = pCache->apHash[h];
49631 pPage->pCache = pCache;
49632 pPage->pLruNext = 0;
49633 /* pPage->pLruPrev = 0;
49634 ** No need to clear pLruPrev since it is not accessed when pLruNext==0 */
49635 *(void **)pPage->page.pExtra = 0;
49636 pCache->apHash[h] = pPage;
49637 if( iKey>pCache->iMaxKey ){
49638 pCache->iMaxKey = iKey;
49639 }
49640 }
49641 return pPage;
49642}
49643
49644/*
49645** Implementation of the sqlite3_pcache.xFetch method.
49646**
49647** Fetch a page by key value.
49648**
49649** Whether or not a new page may be allocated by this function depends on
49650** the value of the createFlag argument. 0 means do not allocate a new
49651** page. 1 means allocate a new page if space is easily available. 2
49652** means to try really hard to allocate a new page.
49653**
49654** For a non-purgeable cache (a cache used as the storage for an in-memory
49655** database) there is really no difference between createFlag 1 and 2. So
49656** the calling function (pcache.c) will never have a createFlag of 1 on
49657** a non-purgeable cache.
49658**
49659** There are three different approaches to obtaining space for a page,
49660** depending on the value of parameter createFlag (which may be 0, 1 or 2).
49661**
49662** 1. Regardless of the value of createFlag, the cache is searched for a
49663** copy of the requested page. If one is found, it is returned.
49664**
49665** 2. If createFlag==0 and the page is not already in the cache, NULL is
49666** returned.
49667**
49668** 3. If createFlag is 1, and the page is not already in the cache, then
49669** return NULL (do not allocate a new page) if any of the following
49670** conditions are true:
49671**
49672** (a) the number of pages pinned by the cache is greater than
49673** PCache1.nMax, or
49674**
49675** (b) the number of pages pinned by the cache is greater than
49676** the sum of nMax for all purgeable caches, less the sum of
49677** nMin for all other purgeable caches, or
49678**
49679** 4. If none of the first three conditions apply and the cache is marked
49680** as purgeable, and if one of the following is true:
49681**
49682** (a) The number of pages allocated for the cache is already
49683** PCache1.nMax, or
49684**
49685** (b) The number of pages allocated for all purgeable caches is
49686** already equal to or greater than the sum of nMax for all
49687** purgeable caches,
49688**
49689** (c) The system is under memory pressure and wants to avoid
49690** unnecessary pages cache entry allocations
49691**
49692** then attempt to recycle a page from the LRU list. If it is the right
49693** size, return the recycled buffer. Otherwise, free the buffer and
49694** proceed to step 5.
49695**
49696** 5. Otherwise, allocate and return a new page buffer.
49697**
49698** There are two versions of this routine. pcache1FetchWithMutex() is
49699** the general case. pcache1FetchNoMutex() is a faster implementation for
49700** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
49701** invokes the appropriate routine.
49702*/
49703static PgHdr1 *pcache1FetchNoMutex(
49704 sqlite3_pcache *p,
49705 unsigned int iKey,
49706 int createFlag
49707){
49708 PCache1 *pCache = (PCache1 *)p;
49709 PgHdr1 *pPage = 0;
49710
49711 /* Step 1: Search the hash table for an existing entry. */
49712 pPage = pCache->apHash[iKey % pCache->nHash];
49713 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
49714
49715 /* Step 2: If the page was found in the hash table, then return it.
49716 ** If the page was not in the hash table and createFlag is 0, abort.
49717 ** Otherwise (page not in hash and createFlag!=0) continue with
49718 ** subsequent steps to try to create the page. */
49719 if( pPage ){
49720 if( PAGE_IS_UNPINNED(pPage)((pPage)->pLruNext!=0) ){
49721 return pcache1PinPage(pPage);
49722 }else{
49723 return pPage;
49724 }
49725 }else if( createFlag ){
49726 /* Steps 3, 4, and 5 implemented by this subroutine */
49727 return pcache1FetchStage2(pCache, iKey, createFlag);
49728 }else{
49729 return 0;
49730 }
49731}
49732#if PCACHE1_MIGHT_USE_GROUP_MUTEX0
49733static PgHdr1 *pcache1FetchWithMutex(
49734 sqlite3_pcache *p,
49735 unsigned int iKey,
49736 int createFlag
49737){
49738 PCache1 *pCache = (PCache1 *)p;
49739 PgHdr1 *pPage;
49740
49741 pcache1EnterMutex(pCache->pGroup)((void) (0));
49742 pPage = pcache1FetchNoMutex(p, iKey, createFlag);
49743 assert( pPage==0 || pCache->iMaxKey>=iKey )((void) (0));
49744 pcache1LeaveMutex(pCache->pGroup)((void) (0));
49745 return pPage;
49746}
49747#endif
49748static sqlite3_pcache_page *pcache1Fetch(
49749 sqlite3_pcache *p,
49750 unsigned int iKey,
49751 int createFlag
49752){
49753#if PCACHE1_MIGHT_USE_GROUP_MUTEX0 || defined(SQLITE_DEBUG)
49754 PCache1 *pCache = (PCache1 *)p;
49755#endif
49756
49757 assert( offsetof(PgHdr1,page)==0 )((void) (0));
49758 assert( pCache->bPurgeable || createFlag!=1 )((void) (0));
49759 assert( pCache->bPurgeable || pCache->nMin==0 )((void) (0));
49760 assert( pCache->bPurgeable==0 || pCache->nMin==10 )((void) (0));
49761 assert( pCache->nMin==0 || pCache->bPurgeable )((void) (0));
49762 assert( pCache->nHash>0 )((void) (0));
49763#if PCACHE1_MIGHT_USE_GROUP_MUTEX0
49764 if( pCache->pGroup->mutex ){
49765 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
49766 }else
49767#endif
49768 {
49769 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
49770 }
49771}
49772
49773
49774/*
49775** Implementation of the sqlite3_pcache.xUnpin method.
49776**
49777** Mark a page as unpinned (eligible for asynchronous recycling).
49778*/
49779static void pcache1Unpin(
49780 sqlite3_pcache *p,
49781 sqlite3_pcache_page *pPg,
49782 int reuseUnlikely
49783){
49784 PCache1 *pCache = (PCache1 *)p;
49785 PgHdr1 *pPage = (PgHdr1 *)pPg;
49786 PGroup *pGroup = pCache->pGroup;
49787
49788 assert( pPage->pCache==pCache )((void) (0));
49789 pcache1EnterMutex(pGroup)((void) (0));
49790
49791 /* It is an error to call this function if the page is already
49792 ** part of the PGroup LRU list.
49793 */
49794 assert( pPage->pLruNext==0 )((void) (0));
49795 assert( PAGE_IS_PINNED(pPage) )((void) (0));
49796
49797 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
49798 pcache1RemoveFromHash(pPage, 1);
49799 }else{
49800 /* Add the page to the PGroup LRU list. */
49801 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
49802 pPage->pLruPrev = &pGroup->lru;
49803 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
49804 *ppFirst = pPage;
49805 pCache->nRecyclable++;
49806 }
49807
49808 pcache1LeaveMutex(pCache->pGroup)((void) (0));
49809}
49810
49811/*
49812** Implementation of the sqlite3_pcache.xRekey method.
49813*/
49814static void pcache1Rekey(
49815 sqlite3_pcache *p,
49816 sqlite3_pcache_page *pPg,
49817 unsigned int iOld,
49818 unsigned int iNew
49819){
49820 PCache1 *pCache = (PCache1 *)p;
49821 PgHdr1 *pPage = (PgHdr1 *)pPg;
49822 PgHdr1 **pp;
49823 unsigned int h;
49824 assert( pPage->iKey==iOld )((void) (0));
49825 assert( pPage->pCache==pCache )((void) (0));
49826
49827 pcache1EnterMutex(pCache->pGroup)((void) (0));
49828
49829 h = iOld%pCache->nHash;
49830 pp = &pCache->apHash[h];
49831 while( (*pp)!=pPage ){
49832 pp = &(*pp)->pNext;
49833 }
49834 *pp = pPage->pNext;
49835
49836 h = iNew%pCache->nHash;
49837 pPage->iKey = iNew;
49838 pPage->pNext = pCache->apHash[h];
49839 pCache->apHash[h] = pPage;
49840 if( iNew>pCache->iMaxKey ){
49841 pCache->iMaxKey = iNew;
49842 }
49843
49844 pcache1LeaveMutex(pCache->pGroup)((void) (0));
49845}
49846
49847/*
49848** Implementation of the sqlite3_pcache.xTruncate method.
49849**
49850** Discard all unpinned pages in the cache with a page number equal to
49851** or greater than parameter iLimit. Any pinned pages with a page number
49852** equal to or greater than iLimit are implicitly unpinned.
49853*/
49854static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
49855 PCache1 *pCache = (PCache1 *)p;
49856 pcache1EnterMutex(pCache->pGroup)((void) (0));
49857 if( iLimit<=pCache->iMaxKey ){
49858 pcache1TruncateUnsafe(pCache, iLimit);
49859 pCache->iMaxKey = iLimit-1;
49860 }
49861 pcache1LeaveMutex(pCache->pGroup)((void) (0));
49862}
49863
49864/*
49865** Implementation of the sqlite3_pcache.xDestroy method.
49866**
49867** Destroy a cache allocated using pcache1Create().
49868*/
49869static void pcache1Destroy(sqlite3_pcache *p){
49870 PCache1 *pCache = (PCache1 *)p;
49871 PGroup *pGroup = pCache->pGroup;
49872 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) )((void) (0));
49873 pcache1EnterMutex(pGroup)((void) (0));
49874 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
49875 assert( pGroup->nMaxPage >= pCache->nMax )((void) (0));
49876 pGroup->nMaxPage -= pCache->nMax;
49877 assert( pGroup->nMinPage >= pCache->nMin )((void) (0));
49878 pGroup->nMinPage -= pCache->nMin;
49879 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
49880 pcache1EnforceMaxPage(pCache);
49881 pcache1LeaveMutex(pGroup)((void) (0));
49882 sqlite3_free(pCache->pBulk);
49883 sqlite3_free(pCache->apHash);
49884 sqlite3_free(pCache);
49885}
49886
49887/*
49888** This function is called during initialization (sqlite3_initialize()) to
49889** install the default pluggable cache module, assuming the user has not
49890** already provided an alternative.
49891*/
49892SQLITE_PRIVATEstatic void sqlite3PCacheSetDefault(void){
49893 static const sqlite3_pcache_methods2 defaultMethods = {
49894 1, /* iVersion */
49895 0, /* pArg */
49896 pcache1Init, /* xInit */
49897 pcache1Shutdown, /* xShutdown */
49898 pcache1Create, /* xCreate */
49899 pcache1Cachesize, /* xCachesize */
49900 pcache1Pagecount, /* xPagecount */
49901 pcache1Fetch, /* xFetch */
49902 pcache1Unpin, /* xUnpin */
49903 pcache1Rekey, /* xRekey */
49904 pcache1Truncate, /* xTruncate */
49905 pcache1Destroy, /* xDestroy */
49906 pcache1Shrink /* xShrink */
49907 };
49908 sqlite3_config(SQLITE_CONFIG_PCACHE218, &defaultMethods);
49909}
49910
49911/*
49912** Return the size of the header on each page of this PCACHE implementation.
49913*/
49914SQLITE_PRIVATEstatic int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1))(((sizeof(PgHdr1))+7)&~7); }
49915
49916/*
49917** Return the global mutex used by this PCACHE implementation. The
49918** sqlite3_status() routine needs access to this mutex.
49919*/
49920SQLITE_PRIVATEstatic sqlite3_mutex *sqlite3Pcache1Mutex(void){
49921 return pcache1(pcache1_g).mutex;
49922}
49923
49924#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
49925/*
49926** This function is called to free superfluous dynamically allocated memory
49927** held by the pager system. Memory in use by any SQLite pager allocated
49928** by the current thread may be sqlite3_free()ed.
49929**
49930** nReq is the number of bytes of memory required. Once this much has
49931** been released, the function returns. The return value is the total number
49932** of bytes of memory released.
49933*/
49934SQLITE_PRIVATEstatic int sqlite3PcacheReleaseMemory(int nReq){
49935 int nFree = 0;
49936 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) )((void) (0));
49937 assert( sqlite3_mutex_notheld(pcache1.mutex) )((void) (0));
49938 if( sqlite3GlobalConfigsqlite3Config.pPage==0 ){
49939 PgHdr1 *p;
49940 pcache1EnterMutex(&pcache1.grp)((void) (0));
49941 while( (nReq<0 || nFree<nReq)
49942 && (p=pcache1(pcache1_g).grp.lru.pLruPrev)!=0
49943 && p->isAnchor==0
49944 ){
49945 nFree += pcache1MemSize(p->page.pBuf);
49946#ifdef SQLITE_PCACHE_SEPARATE_HEADER
49947 nFree += sqlite3MemSize(p);
49948#endif
49949 assert( PAGE_IS_UNPINNED(p) )((void) (0));
49950 pcache1PinPage(p);
49951 pcache1RemoveFromHash(p, 1);
49952 }
49953 pcache1LeaveMutex(&pcache1.grp)((void) (0));
49954 }
49955 return nFree;
49956}
49957#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
49958
49959#ifdef SQLITE_TEST
49960/*
49961** This function is used by test procedures to inspect the internal state
49962** of the global cache.
49963*/
49964SQLITE_PRIVATEstatic void sqlite3PcacheStats(
49965 int *pnCurrent, /* OUT: Total number of pages cached */
49966 int *pnMax, /* OUT: Global maximum cache size */
49967 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */
49968 int *pnRecyclable /* OUT: Total number of pages available for recycling */
49969){
49970 PgHdr1 *p;
49971 int nRecyclable = 0;
49972 for(p=pcache1(pcache1_g).grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
49973 assert( PAGE_IS_UNPINNED(p) )((void) (0));
49974 nRecyclable++;
49975 }
49976 *pnCurrent = pcache1(pcache1_g).grp.nPurgeable;
49977 *pnMax = (int)pcache1(pcache1_g).grp.nMaxPage;
49978 *pnMin = (int)pcache1(pcache1_g).grp.nMinPage;
49979 *pnRecyclable = nRecyclable;
49980}
49981#endif
49982
49983/************** End of pcache1.c *********************************************/
49984/************** Begin file rowset.c ******************************************/
49985/*
49986** 2008 December 3
49987**
49988** The author disclaims copyright to this source code. In place of
49989** a legal notice, here is a blessing:
49990**
49991** May you do good and not evil.
49992** May you find forgiveness for yourself and forgive others.
49993** May you share freely, never taking more than you give.
49994**
49995*************************************************************************
49996**
49997** This module implements an object we call a "RowSet".
49998**
49999** The RowSet object is a collection of rowids. Rowids
50000** are inserted into the RowSet in an arbitrary order. Inserts
50001** can be intermixed with tests to see if a given rowid has been
50002** previously inserted into the RowSet.
50003**
50004** After all inserts are finished, it is possible to extract the
50005** elements of the RowSet in sorted order. Once this extraction
50006** process has started, no new elements may be inserted.
50007**
50008** Hence, the primitive operations for a RowSet are:
50009**
50010** CREATE
50011** INSERT
50012** TEST
50013** SMALLEST
50014** DESTROY
50015**
50016** The CREATE and DESTROY primitives are the constructor and destructor,
50017** obviously. The INSERT primitive adds a new element to the RowSet.
50018** TEST checks to see if an element is already in the RowSet. SMALLEST
50019** extracts the least value from the RowSet.
50020**
50021** The INSERT primitive might allocate additional memory. Memory is
50022** allocated in chunks so most INSERTs do no allocation. There is an
50023** upper bound on the size of allocated memory. No memory is freed
50024** until DESTROY.
50025**
50026** The TEST primitive includes a "batch" number. The TEST primitive
50027** will only see elements that were inserted before the last change
50028** in the batch number. In other words, if an INSERT occurs between
50029** two TESTs where the TESTs have the same batch nubmer, then the
50030** value added by the INSERT will not be visible to the second TEST.
50031** The initial batch number is zero, so if the very first TEST contains
50032** a non-zero batch number, it will see all prior INSERTs.
50033**
50034** No INSERTs may occurs after a SMALLEST. An assertion will fail if
50035** that is attempted.
50036**
50037** The cost of an INSERT is roughly constant. (Sometimes new memory
50038** has to be allocated on an INSERT.) The cost of a TEST with a new
50039** batch number is O(NlogN) where N is the number of elements in the RowSet.
50040** The cost of a TEST using the same batch number is O(logN). The cost
50041** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST
50042** primitives are constant time. The cost of DESTROY is O(N).
50043**
50044** TEST and SMALLEST may not be used by the same RowSet. This used to
50045** be possible, but the feature was not used, so it was removed in order
50046** to simplify the code.
50047*/
50048/* #include "sqliteInt.h" */
50049
50050
50051/*
50052** Target size for allocation chunks.
50053*/
50054#define ROWSET_ALLOCATION_SIZE1024 1024
50055
50056/*
50057** The number of rowset entries per allocation chunk.
50058*/
50059#define ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry)) \
50060 ((ROWSET_ALLOCATION_SIZE1024-8)/sizeof(struct RowSetEntry))
50061
50062/*
50063** Each entry in a RowSet is an instance of the following object.
50064**
50065** This same object is reused to store a linked list of trees of RowSetEntry
50066** objects. In that alternative use, pRight points to the next entry
50067** in the list, pLeft points to the tree, and v is unused. The
50068** RowSet.pForest value points to the head of this forest list.
50069*/
50070struct RowSetEntry {
50071 i64 v; /* ROWID value for this entry */
50072 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */
50073 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */
50074};
50075
50076/*
50077** RowSetEntry objects are allocated in large chunks (instances of the
50078** following structure) to reduce memory allocation overhead. The
50079** chunks are kept on a linked list so that they can be deallocated
50080** when the RowSet is destroyed.
50081*/
50082struct RowSetChunk {
50083 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */
50084 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry))]; /* Allocated entries */
50085};
50086
50087/*
50088** A RowSet in an instance of the following structure.
50089**
50090** A typedef of this structure if found in sqliteInt.h.
50091*/
50092struct RowSet {
50093 struct RowSetChunk *pChunk; /* List of all chunk allocations */
50094 sqlite3 *db; /* The database connection */
50095 struct RowSetEntry *pEntry; /* List of entries using pRight */
50096 struct RowSetEntry *pLast; /* Last entry on the pEntry list */
50097 struct RowSetEntry *pFresh; /* Source of new entry objects */
50098 struct RowSetEntry *pForest; /* List of binary trees of entries */
50099 u16 nFresh; /* Number of objects on pFresh */
50100 u16 rsFlags; /* Various flags */
50101 int iBatch; /* Current insert batch */
50102};
50103
50104/*
50105** Allowed values for RowSet.rsFlags
50106*/
50107#define ROWSET_SORTED0x01 0x01 /* True if RowSet.pEntry is sorted */
50108#define ROWSET_NEXT0x02 0x02 /* True if sqlite3RowSetNext() has been called */
50109
50110/*
50111** Allocate a RowSet object. Return NULL if a memory allocation
50112** error occurs.
50113*/
50114SQLITE_PRIVATEstatic RowSet *sqlite3RowSetInit(sqlite3 *db){
50115 RowSet *p = sqlite3DbMallocRawNN(db, sizeof(*p));
50116 if( p ){
50117 int N = sqlite3DbMallocSize(db, p);
50118 p->pChunk = 0;
50119 p->db = db;
50120 p->pEntry = 0;
50121 p->pLast = 0;
50122 p->pForest = 0;
50123 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7) + (char*)p);
50124 p->nFresh = (u16)((N - ROUND8(sizeof(*p))(((sizeof(*p))+7)&~7))/sizeof(struct RowSetEntry));
50125 p->rsFlags = ROWSET_SORTED0x01;
50126 p->iBatch = 0;
50127 }
50128 return p;
50129}
50130
50131/*
50132** Deallocate all chunks from a RowSet. This frees all memory that
50133** the RowSet has allocated over its lifetime. This routine is
50134** the destructor for the RowSet.
50135*/
50136SQLITE_PRIVATEstatic void sqlite3RowSetClear(void *pArg){
50137 RowSet *p = (RowSet*)pArg;
50138 struct RowSetChunk *pChunk, *pNextChunk;
50139 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
50140 pNextChunk = pChunk->pNextChunk;
50141 sqlite3DbFree(p->db, pChunk);
50142 }
50143 p->pChunk = 0;
50144 p->nFresh = 0;
50145 p->pEntry = 0;
50146 p->pLast = 0;
50147 p->pForest = 0;
50148 p->rsFlags = ROWSET_SORTED0x01;
50149}
50150
50151/*
50152** Deallocate all chunks from a RowSet. This frees all memory that
50153** the RowSet has allocated over its lifetime. This routine is
50154** the destructor for the RowSet.
50155*/
50156SQLITE_PRIVATEstatic void sqlite3RowSetDelete(void *pArg){
50157 sqlite3RowSetClear(pArg);
50158 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
50159}
50160
50161/*
50162** Allocate a new RowSetEntry object that is associated with the
50163** given RowSet. Return a pointer to the new and completely uninitialized
50164** objected.
50165**
50166** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
50167** routine returns NULL.
50168*/
50169static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
50170 assert( p!=0 )((void) (0));
50171 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
50172 /* We could allocate a fresh RowSetEntry each time one is needed, but it
50173 ** is more efficient to pull a preallocated entry from the pool */
50174 struct RowSetChunk *pNew;
50175 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
50176 if( pNew==0 ){
50177 return 0;
50178 }
50179 pNew->pNextChunk = p->pChunk;
50180 p->pChunk = pNew;
50181 p->pFresh = pNew->aEntry;
50182 p->nFresh = ROWSET_ENTRY_PER_CHUNK((1024 -8)/sizeof(struct RowSetEntry));
50183 }
50184 p->nFresh--;
50185 return p->pFresh++;
50186}
50187
50188/*
50189** Insert a new value into a RowSet.
50190**
50191** The mallocFailed flag of the database connection is set if a
50192** memory allocation fails.
50193*/
50194SQLITE_PRIVATEstatic void sqlite3RowSetInsert(RowSet *p, i64 rowid){
50195 struct RowSetEntry *pEntry; /* The new entry */
50196 struct RowSetEntry *pLast; /* The last prior entry */
50197
50198 /* This routine is never called after sqlite3RowSetNext() */
50199 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 )((void) (0));
50200
50201 pEntry = rowSetEntryAlloc(p);
50202 if( pEntry==0 ) return;
50203 pEntry->v = rowid;
50204 pEntry->pRight = 0;
50205 pLast = p->pLast;
50206 if( pLast ){
50207 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
50208 /* Avoid unnecessary sorts by preserving the ROWSET_SORTED flags
50209 ** where possible */
50210 p->rsFlags &= ~ROWSET_SORTED0x01;
50211 }
50212 pLast->pRight = pEntry;
50213 }else{
50214 p->pEntry = pEntry;
50215 }
50216 p->pLast = pEntry;
50217}
50218
50219/*
50220** Merge two lists of RowSetEntry objects. Remove duplicates.
50221**
50222** The input lists are connected via pRight pointers and are
50223** assumed to each already be in sorted order.
50224*/
50225static struct RowSetEntry *rowSetEntryMerge(
50226 struct RowSetEntry *pA, /* First sorted list to be merged */
50227 struct RowSetEntry *pB /* Second sorted list to be merged */
50228){
50229 struct RowSetEntry head;
50230 struct RowSetEntry *pTail;
50231
50232 pTail = &head;
50233 assert( pA!=0 && pB!=0 )((void) (0));
50234 for(;;){
50235 assert( pA->pRight==0 || pA->v<=pA->pRight->v )((void) (0));
50236 assert( pB->pRight==0 || pB->v<=pB->pRight->v )((void) (0));
50237 if( pA->v<=pB->v ){
50238 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
50239 pA = pA->pRight;
50240 if( pA==0 ){
50241 pTail->pRight = pB;
50242 break;
50243 }
50244 }else{
50245 pTail = pTail->pRight = pB;
50246 pB = pB->pRight;
50247 if( pB==0 ){
50248 pTail->pRight = pA;
50249 break;
50250 }
50251 }
50252 }
50253 return head.pRight;
50254}
50255
50256/*
50257** Sort all elements on the list of RowSetEntry objects into order of
50258** increasing v.
50259*/
50260static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
50261 unsigned int i;
50262 struct RowSetEntry *pNext, *aBucket[40];
50263
50264 memset(aBucket, 0, sizeof(aBucket));
50265 while( pIn ){
50266 pNext = pIn->pRight;
50267 pIn->pRight = 0;
50268 for(i=0; aBucket[i]; i++){
50269 pIn = rowSetEntryMerge(aBucket[i], pIn);
50270 aBucket[i] = 0;
50271 }
50272 aBucket[i] = pIn;
50273 pIn = pNext;
50274 }
50275 pIn = aBucket[0];
50276 for(i=1; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
50277 if( aBucket[i]==0 ) continue;
50278 pIn = pIn ? rowSetEntryMerge(pIn, aBucket[i]) : aBucket[i];
50279 }
50280 return pIn;
50281}
50282
50283
50284/*
50285** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
50286** Convert this tree into a linked list connected by the pRight pointers
50287** and return pointers to the first and last elements of the new list.
50288*/
50289static void rowSetTreeToList(
50290 struct RowSetEntry *pIn, /* Root of the input tree */
50291 struct RowSetEntry **ppFirst, /* Write head of the output list here */
50292 struct RowSetEntry **ppLast /* Write tail of the output list here */
50293){
50294 assert( pIn!=0 )((void) (0));
50295 if( pIn->pLeft ){
50296 struct RowSetEntry *p;
50297 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
50298 p->pRight = pIn;
50299 }else{
50300 *ppFirst = pIn;
50301 }
50302 if( pIn->pRight ){
50303 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
50304 }else{
50305 *ppLast = pIn;
50306 }
50307 assert( (*ppLast)->pRight==0 )((void) (0));
50308}
50309
50310
50311/*
50312** Convert a sorted list of elements (connected by pRight) into a binary
50313** tree with depth of iDepth. A depth of 1 means the tree contains a single
50314** node taken from the head of *ppList. A depth of 2 means a tree with
50315** three nodes. And so forth.
50316**
50317** Use as many entries from the input list as required and update the
50318** *ppList to point to the unused elements of the list. If the input
50319** list contains too few elements, then construct an incomplete tree
50320** and leave *ppList set to NULL.
50321**
50322** Return a pointer to the root of the constructed binary tree.
50323*/
50324static struct RowSetEntry *rowSetNDeepTree(
50325 struct RowSetEntry **ppList,
50326 int iDepth
50327){
50328 struct RowSetEntry *p; /* Root of the new tree */
50329 struct RowSetEntry *pLeft; /* Left subtree */
50330 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
50331 /* Prevent unnecessary deep recursion when we run out of entries */
50332 return 0;
50333 }
50334 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
50335 /* This branch causes a *balanced* tree to be generated. A valid tree
50336 ** is still generated without this branch, but the tree is wildly
50337 ** unbalanced and inefficient. */
50338 pLeft = rowSetNDeepTree(ppList, iDepth-1);
50339 p = *ppList;
50340 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
50341 /* It is safe to always return here, but the resulting tree
50342 ** would be unbalanced */
50343 return pLeft;
50344 }
50345 p->pLeft = pLeft;
50346 *ppList = p->pRight;
50347 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
50348 }else{
50349 p = *ppList;
50350 *ppList = p->pRight;
50351 p->pLeft = p->pRight = 0;
50352 }
50353 return p;
50354}
50355
50356/*
50357** Convert a sorted list of elements into a binary tree. Make the tree
50358** as deep as it needs to be in order to contain the entire list.
50359*/
50360static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
50361 int iDepth; /* Depth of the tree so far */
50362 struct RowSetEntry *p; /* Current tree root */
50363 struct RowSetEntry *pLeft; /* Left subtree */
50364
50365 assert( pList!=0 )((void) (0));
50366 p = pList;
50367 pList = p->pRight;
50368 p->pLeft = p->pRight = 0;
50369 for(iDepth=1; pList; iDepth++){
50370 pLeft = p;
50371 p = pList;
50372 pList = p->pRight;
50373 p->pLeft = pLeft;
50374 p->pRight = rowSetNDeepTree(&pList, iDepth);
50375 }
50376 return p;
50377}
50378
50379/*
50380** Extract the smallest element from the RowSet.
50381** Write the element into *pRowid. Return 1 on success. Return
50382** 0 if the RowSet is already empty.
50383**
50384** After this routine has been called, the sqlite3RowSetInsert()
50385** routine may not be called again.
50386**
50387** This routine may not be called after sqlite3RowSetTest() has
50388** been used. Older versions of RowSet allowed that, but as the
50389** capability was not used by the code generator, it was removed
50390** for code economy.
50391*/
50392SQLITE_PRIVATEstatic int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
50393 assert( p!=0 )((void) (0));
50394 assert( p->pForest==0 )((void) (0)); /* Cannot be used with sqlite3RowSetText() */
50395
50396 /* Merge the forest into a single sorted list on first call */
50397 if( (p->rsFlags & ROWSET_NEXT0x02)==0 ){ /*OPTIMIZATION-IF-FALSE*/
50398 if( (p->rsFlags & ROWSET_SORTED0x01)==0 ){ /*OPTIMIZATION-IF-FALSE*/
50399 p->pEntry = rowSetEntrySort(p->pEntry);
50400 }
50401 p->rsFlags |= ROWSET_SORTED0x01|ROWSET_NEXT0x02;
50402 }
50403
50404 /* Return the next entry on the list */
50405 if( p->pEntry ){
50406 *pRowid = p->pEntry->v;
50407 p->pEntry = p->pEntry->pRight;
50408 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
50409 /* Free memory immediately, rather than waiting on sqlite3_finalize() */
50410 sqlite3RowSetClear(p);
50411 }
50412 return 1;
50413 }else{
50414 return 0;
50415 }
50416}
50417
50418/*
50419** Check to see if element iRowid was inserted into the rowset as
50420** part of any insert batch prior to iBatch. Return 1 or 0.
50421**
50422** If this is the first test of a new batch and if there exist entries
50423** on pRowSet->pEntry, then sort those entries into the forest at
50424** pRowSet->pForest so that they can be tested.
50425*/
50426SQLITE_PRIVATEstatic int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64 iRowid){
50427 struct RowSetEntry *p, *pTree;
50428
50429 /* This routine is never called after sqlite3RowSetNext() */
50430 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 )((void) (0));
50431
50432 /* Sort entries into the forest on the first test of a new batch.
50433 ** To save unnecessary work, only do this when the batch number changes.
50434 */
50435 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
50436 p = pRowSet->pEntry;
50437 if( p ){
50438 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
50439 if( (pRowSet->rsFlags & ROWSET_SORTED0x01)==0 ){ /*OPTIMIZATION-IF-FALSE*/
50440 /* Only sort the current set of entiries if they need it */
50441 p = rowSetEntrySort(p);
50442 }
50443 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
50444 ppPrevTree = &pTree->pRight;
50445 if( pTree->pLeft==0 ){
50446 pTree->pLeft = rowSetListToTree(p);
50447 break;
50448 }else{
50449 struct RowSetEntry *pAux, *pTail;
50450 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
50451 pTree->pLeft = 0;
50452 p = rowSetEntryMerge(pAux, p);
50453 }
50454 }
50455 if( pTree==0 ){
50456 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
50457 if( pTree ){
50458 pTree->v = 0;
50459 pTree->pRight = 0;
50460 pTree->pLeft = rowSetListToTree(p);
50461 }
50462 }
50463 pRowSet->pEntry = 0;
50464 pRowSet->pLast = 0;
50465 pRowSet->rsFlags |= ROWSET_SORTED0x01;
50466 }
50467 pRowSet->iBatch = iBatch;
50468 }
50469
50470 /* Test to see if the iRowid value appears anywhere in the forest.
50471 ** Return 1 if it does and 0 if not.
50472 */
50473 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
50474 p = pTree->pLeft;
50475 while( p ){
50476 if( p->v<iRowid ){
50477 p = p->pRight;
50478 }else if( p->v>iRowid ){
50479 p = p->pLeft;
50480 }else{
50481 return 1;
50482 }
50483 }
50484 }
50485 return 0;
50486}
50487
50488/************** End of rowset.c **********************************************/
50489/************** Begin file pager.c *******************************************/
50490/*
50491** 2001 September 15
50492**
50493** The author disclaims copyright to this source code. In place of
50494** a legal notice, here is a blessing:
50495**
50496** May you do good and not evil.
50497** May you find forgiveness for yourself and forgive others.
50498** May you share freely, never taking more than you give.
50499**
50500*************************************************************************
50501** This is the implementation of the page cache subsystem or "pager".
50502**
50503** The pager is used to access a database disk file. It implements
50504** atomic commit and rollback through the use of a journal file that
50505** is separate from the database file. The pager also implements file
50506** locking to prevent two processes from writing the same database
50507** file simultaneously, or one process from reading the database while
50508** another is writing.
50509*/
50510#ifndef SQLITE_OMIT_DISKIO
50511/* #include "sqliteInt.h" */
50512/************** Include wal.h in the middle of pager.c ***********************/
50513/************** Begin file wal.h *********************************************/
50514/*
50515** 2010 February 1
50516**
50517** The author disclaims copyright to this source code. In place of
50518** a legal notice, here is a blessing:
50519**
50520** May you do good and not evil.
50521** May you find forgiveness for yourself and forgive others.
50522** May you share freely, never taking more than you give.
50523**
50524*************************************************************************
50525** This header file defines the interface to the write-ahead logging
50526** system. Refer to the comments below and the header comment attached to
50527** the implementation of each function in log.c for further details.
50528*/
50529
50530#ifndef SQLITE_WAL_H
50531#define SQLITE_WAL_H
50532
50533/* #include "sqliteInt.h" */
50534
50535/* Macros for extracting appropriate sync flags for either transaction
50536** commits (WAL_SYNC_FLAGS(X)) or for checkpoint ops (CKPT_SYNC_FLAGS(X)):
50537*/
50538#define WAL_SYNC_FLAGS(X)((X)&0x03) ((X)&0x03)
50539#define CKPT_SYNC_FLAGS(X)(((X)>>2)&0x03) (((X)>>2)&0x03)
50540
50541#ifdef SQLITE_OMIT_WAL
50542# define sqlite3WalOpen(x,y,z) 0
50543# define sqlite3WalLimit(x,y)
50544# define sqlite3WalClose(v,w,x,y,z) 0
50545# define sqlite3WalBeginReadTransaction(y,z) 0
50546# define sqlite3WalEndReadTransaction(z)
50547# define sqlite3WalDbsize(y) 0
50548# define sqlite3WalBeginWriteTransaction(y) 0
50549# define sqlite3WalEndWriteTransaction(x) 0
50550# define sqlite3WalUndo(x,y,z) 0
50551# define sqlite3WalSavepoint(y,z)
50552# define sqlite3WalSavepointUndo(y,z) 0
50553# define sqlite3WalFrames(u,v,w,x,y,z) 0
50554# define sqlite3WalCheckpoint(q,r,s,t,u,v,w,x,y,z) 0
50555# define sqlite3WalCallback(z) 0
50556# define sqlite3WalExclusiveMode(y,z) 0
50557# define sqlite3WalHeapMemory(z) 0
50558# define sqlite3WalFramesize(z) 0
50559# define sqlite3WalFindFrame(x,y,z) 0
50560# define sqlite3WalFile(x) 0
50561#else
50562
50563#define WAL_SAVEPOINT_NDATA4 4
50564
50565/* Connection to a write-ahead log (WAL) file.
50566** There is one object of this type for each pager.
50567*/
50568typedef struct Wal Wal;
50569
50570/* Open and close a connection to a write-ahead log. */
50571SQLITE_PRIVATEstatic int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
50572SQLITE_PRIVATEstatic int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *);
50573
50574/* Set the limiting size of a WAL file. */
50575SQLITE_PRIVATEstatic void sqlite3WalLimit(Wal*, i64);
50576
50577/* Used by readers to open (lock) and close (unlock) a snapshot. A
50578** snapshot is like a read-transaction. It is the state of the database
50579** at an instant in time. sqlite3WalOpenSnapshot gets a read lock and
50580** preserves the current state even if the other threads or processes
50581** write to or checkpoint the WAL. sqlite3WalCloseSnapshot() closes the
50582** transaction and releases the lock.
50583*/
50584SQLITE_PRIVATEstatic int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
50585SQLITE_PRIVATEstatic void sqlite3WalEndReadTransaction(Wal *pWal);
50586
50587/* Read a page from the write-ahead log, if it is present. */
50588SQLITE_PRIVATEstatic int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
50589SQLITE_PRIVATEstatic int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
50590
50591/* If the WAL is not empty, return the size of the database. */
50592SQLITE_PRIVATEstatic Pgno sqlite3WalDbsize(Wal *pWal);
50593
50594/* Obtain or release the WRITER lock. */
50595SQLITE_PRIVATEstatic int sqlite3WalBeginWriteTransaction(Wal *pWal);
50596SQLITE_PRIVATEstatic int sqlite3WalEndWriteTransaction(Wal *pWal);
50597
50598/* Undo any frames written (but not committed) to the log */
50599SQLITE_PRIVATEstatic int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
50600
50601/* Return an integer that records the current (uncommitted) write
50602** position in the WAL */
50603SQLITE_PRIVATEstatic void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
50604
50605/* Move the write position of the WAL back to iFrame. Called in
50606** response to a ROLLBACK TO command. */
50607SQLITE_PRIVATEstatic int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
50608
50609/* Write a frame or frames to the log. */
50610SQLITE_PRIVATEstatic int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
50611
50612/* Copy pages from the log to the database file */
50613SQLITE_PRIVATEstatic int sqlite3WalCheckpoint(
50614 Wal *pWal, /* Write-ahead log connection */
50615 sqlite3 *db, /* Check this handle's interrupt flag */
50616 int eMode, /* One of PASSIVE, FULL and RESTART */
50617 int (*xBusy)(void*), /* Function to call when busy */
50618 void *pBusyArg, /* Context argument for xBusyHandler */
50619 int sync_flags, /* Flags to sync db file with (or 0) */
50620 int nBuf, /* Size of buffer nBuf */
50621 u8 *zBuf, /* Temporary buffer to use */
50622 int *pnLog, /* OUT: Number of frames in WAL */
50623 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
50624);
50625
50626/* Return the value to pass to a sqlite3_wal_hook callback, the
50627** number of frames in the WAL at the point of the last commit since
50628** sqlite3WalCallback() was called. If no commits have occurred since
50629** the last call, then return 0.
50630*/
50631SQLITE_PRIVATEstatic int sqlite3WalCallback(Wal *pWal);
50632
50633/* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
50634** by the pager layer on the database file.
50635*/
50636SQLITE_PRIVATEstatic int sqlite3WalExclusiveMode(Wal *pWal, int op);
50637
50638/* Return true if the argument is non-NULL and the WAL module is using
50639** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
50640** WAL module is using shared-memory, return false.
50641*/
50642SQLITE_PRIVATEstatic int sqlite3WalHeapMemory(Wal *pWal);
50643
50644#ifdef SQLITE_ENABLE_SNAPSHOT
50645SQLITE_PRIVATEstatic int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot);
50646SQLITE_PRIVATEstatic void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot);
50647SQLITE_PRIVATEstatic int sqlite3WalSnapshotRecover(Wal *pWal);
50648SQLITE_PRIVATEstatic int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot);
50649SQLITE_PRIVATEstatic void sqlite3WalSnapshotUnlock(Wal *pWal);
50650#endif
50651
50652#ifdef SQLITE_ENABLE_ZIPVFS
50653/* If the WAL file is not empty, return the number of bytes of content
50654** stored in each frame (i.e. the db page-size when the WAL was created).
50655*/
50656SQLITE_PRIVATEstatic int sqlite3WalFramesize(Wal *pWal);
50657#endif
50658
50659/* Return the sqlite3_file object for the WAL file */
50660SQLITE_PRIVATEstatic sqlite3_file *sqlite3WalFile(Wal *pWal);
50661
50662#endif /* ifndef SQLITE_OMIT_WAL */
50663#endif /* SQLITE_WAL_H */
50664
50665/************** End of wal.h *************************************************/
50666/************** Continuing where we left off in pager.c **********************/
50667
50668
50669/******************* NOTES ON THE DESIGN OF THE PAGER ************************
50670**
50671** This comment block describes invariants that hold when using a rollback
50672** journal. These invariants do not apply for journal_mode=WAL,
50673** journal_mode=MEMORY, or journal_mode=OFF.
50674**
50675** Within this comment block, a page is deemed to have been synced
50676** automatically as soon as it is written when PRAGMA synchronous=OFF.
50677** Otherwise, the page is not synced until the xSync method of the VFS
50678** is called successfully on the file containing the page.
50679**
50680** Definition: A page of the database file is said to be "overwriteable" if
50681** one or more of the following are true about the page:
50682**
50683** (a) The original content of the page as it was at the beginning of
50684** the transaction has been written into the rollback journal and
50685** synced.
50686**
50687** (b) The page was a freelist leaf page at the start of the transaction.
50688**
50689** (c) The page number is greater than the largest page that existed in
50690** the database file at the start of the transaction.
50691**
50692** (1) A page of the database file is never overwritten unless one of the
50693** following are true:
50694**
50695** (a) The page and all other pages on the same sector are overwriteable.
50696**
50697** (b) The atomic page write optimization is enabled, and the entire
50698** transaction other than the update of the transaction sequence
50699** number consists of a single page change.
50700**
50701** (2) The content of a page written into the rollback journal exactly matches
50702** both the content in the database when the rollback journal was written
50703** and the content in the database at the beginning of the current
50704** transaction.
50705**
50706** (3) Writes to the database file are an integer multiple of the page size
50707** in length and are aligned on a page boundary.
50708**
50709** (4) Reads from the database file are either aligned on a page boundary and
50710** an integer multiple of the page size in length or are taken from the
50711** first 100 bytes of the database file.
50712**
50713** (5) All writes to the database file are synced prior to the rollback journal
50714** being deleted, truncated, or zeroed.
50715**
50716** (6) If a master journal file is used, then all writes to the database file
50717** are synced prior to the master journal being deleted.
50718**
50719** Definition: Two databases (or the same database at two points it time)
50720** are said to be "logically equivalent" if they give the same answer to
50721** all queries. Note in particular the content of freelist leaf
50722** pages can be changed arbitrarily without affecting the logical equivalence
50723** of the database.
50724**
50725** (7) At any time, if any subset, including the empty set and the total set,
50726** of the unsynced changes to a rollback journal are removed and the
50727** journal is rolled back, the resulting database file will be logically
50728** equivalent to the database file at the beginning of the transaction.
50729**
50730** (8) When a transaction is rolled back, the xTruncate method of the VFS
50731** is called to restore the database file to the same size it was at
50732** the beginning of the transaction. (In some VFSes, the xTruncate
50733** method is a no-op, but that does not change the fact the SQLite will
50734** invoke it.)
50735**
50736** (9) Whenever the database file is modified, at least one bit in the range
50737** of bytes from 24 through 39 inclusive will be changed prior to releasing
50738** the EXCLUSIVE lock, thus signaling other connections on the same
50739** database to flush their caches.
50740**
50741** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
50742** than one billion transactions.
50743**
50744** (11) A database file is well-formed at the beginning and at the conclusion
50745** of every transaction.
50746**
50747** (12) An EXCLUSIVE lock is held on the database file when writing to
50748** the database file.
50749**
50750** (13) A SHARED lock is held on the database file while reading any
50751** content out of the database file.
50752**
50753******************************************************************************/
50754
50755/*
50756** Macros for troubleshooting. Normally turned off
50757*/
50758#if 0
50759int sqlite3PagerTrace=1; /* True to enable tracing */
50760#define sqlite3DebugPrintf printf
50761#define PAGERTRACE(X) if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
50762#else
50763#define PAGERTRACE(X)
50764#endif
50765
50766/*
50767** The following two macros are used within the PAGERTRACE() macros above
50768** to print out file-descriptors.
50769**
50770** PAGERID() takes a pointer to a Pager struct as its argument. The
50771** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
50772** struct as its argument.
50773*/
50774#define PAGERID(p)(((int)(long int)(p->fd))) (SQLITE_PTR_TO_INT(p->fd)((int)(long int)(p->fd)))
50775#define FILEHANDLEID(fd)(((int)(long int)(fd))) (SQLITE_PTR_TO_INT(fd)((int)(long int)(fd)))
50776
50777/*
50778** The Pager.eState variable stores the current 'state' of a pager. A
50779** pager may be in any one of the seven states shown in the following
50780** state diagram.
50781**
50782** OPEN <------+------+
50783** | | |
50784** V | |
50785** +---------> READER-------+ |
50786** | | |
50787** | V |
50788** |<-------WRITER_LOCKED------> ERROR
50789** | | ^
50790** | V |
50791** |<------WRITER_CACHEMOD-------->|
50792** | | |
50793** | V |
50794** |<-------WRITER_DBMOD---------->|
50795** | | |
50796** | V |
50797** +<------WRITER_FINISHED-------->+
50798**
50799**
50800** List of state transitions and the C [function] that performs each:
50801**
50802** OPEN -> READER [sqlite3PagerSharedLock]
50803** READER -> OPEN [pager_unlock]
50804**
50805** READER -> WRITER_LOCKED [sqlite3PagerBegin]
50806** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
50807** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
50808** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
50809** WRITER_*** -> READER [pager_end_transaction]
50810**
50811** WRITER_*** -> ERROR [pager_error]
50812** ERROR -> OPEN [pager_unlock]
50813**
50814**
50815** OPEN:
50816**
50817** The pager starts up in this state. Nothing is guaranteed in this
50818** state - the file may or may not be locked and the database size is
50819** unknown. The database may not be read or written.
50820**
50821** * No read or write transaction is active.
50822** * Any lock, or no lock at all, may be held on the database file.
50823** * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
50824**
50825** READER:
50826**
50827** In this state all the requirements for reading the database in
50828** rollback (non-WAL) mode are met. Unless the pager is (or recently
50829** was) in exclusive-locking mode, a user-level read transaction is
50830** open. The database size is known in this state.
50831**
50832** A connection running with locking_mode=normal enters this state when
50833** it opens a read-transaction on the database and returns to state
50834** OPEN after the read-transaction is completed. However a connection
50835** running in locking_mode=exclusive (including temp databases) remains in
50836** this state even after the read-transaction is closed. The only way
50837** a locking_mode=exclusive connection can transition from READER to OPEN
50838** is via the ERROR state (see below).
50839**
50840** * A read transaction may be active (but a write-transaction cannot).
50841** * A SHARED or greater lock is held on the database file.
50842** * The dbSize variable may be trusted (even if a user-level read
50843** transaction is not active). The dbOrigSize and dbFileSize variables
50844** may not be trusted at this point.
50845** * If the database is a WAL database, then the WAL connection is open.
50846** * Even if a read-transaction is not open, it is guaranteed that
50847** there is no hot-journal in the file-system.
50848**
50849** WRITER_LOCKED:
50850**
50851** The pager moves to this state from READER when a write-transaction
50852** is first opened on the database. In WRITER_LOCKED state, all locks
50853** required to start a write-transaction are held, but no actual
50854** modifications to the cache or database have taken place.
50855**
50856** In rollback mode, a RESERVED or (if the transaction was opened with
50857** BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
50858** moving to this state, but the journal file is not written to or opened
50859** to in this state. If the transaction is committed or rolled back while
50860** in WRITER_LOCKED state, all that is required is to unlock the database
50861** file.
50862**
50863** IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
50864** If the connection is running with locking_mode=exclusive, an attempt
50865** is made to obtain an EXCLUSIVE lock on the database file.
50866**
50867** * A write transaction is active.
50868** * If the connection is open in rollback-mode, a RESERVED or greater
50869** lock is held on the database file.
50870** * If the connection is open in WAL-mode, a WAL write transaction
50871** is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
50872** called).
50873** * The dbSize, dbOrigSize and dbFileSize variables are all valid.
50874** * The contents of the pager cache have not been modified.
50875** * The journal file may or may not be open.
50876** * Nothing (not even the first header) has been written to the journal.
50877**
50878** WRITER_CACHEMOD:
50879**
50880** A pager moves from WRITER_LOCKED state to this state when a page is
50881** first modified by the upper layer. In rollback mode the journal file
50882** is opened (if it is not already open) and a header written to the
50883** start of it. The database file on disk has not been modified.
50884**
50885** * A write transaction is active.
50886** * A RESERVED or greater lock is held on the database file.
50887** * The journal file is open and the first header has been written
50888** to it, but the header has not been synced to disk.
50889** * The contents of the page cache have been modified.
50890**
50891** WRITER_DBMOD:
50892**
50893** The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
50894** when it modifies the contents of the database file. WAL connections
50895** never enter this state (since they do not modify the database file,
50896** just the log file).
50897**
50898** * A write transaction is active.
50899** * An EXCLUSIVE or greater lock is held on the database file.
50900** * The journal file is open and the first header has been written
50901** and synced to disk.
50902** * The contents of the page cache have been modified (and possibly
50903** written to disk).
50904**
50905** WRITER_FINISHED:
50906**
50907** It is not possible for a WAL connection to enter this state.
50908**
50909** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
50910** state after the entire transaction has been successfully written into the
50911** database file. In this state the transaction may be committed simply
50912** by finalizing the journal file. Once in WRITER_FINISHED state, it is
50913** not possible to modify the database further. At this point, the upper
50914** layer must either commit or rollback the transaction.
50915**
50916** * A write transaction is active.
50917** * An EXCLUSIVE or greater lock is held on the database file.
50918** * All writing and syncing of journal and database data has finished.
50919** If no error occurred, all that remains is to finalize the journal to
50920** commit the transaction. If an error did occur, the caller will need
50921** to rollback the transaction.
50922**
50923** ERROR:
50924**
50925** The ERROR state is entered when an IO or disk-full error (including
50926** SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
50927** difficult to be sure that the in-memory pager state (cache contents,
50928** db size etc.) are consistent with the contents of the file-system.
50929**
50930** Temporary pager files may enter the ERROR state, but in-memory pagers
50931** cannot.
50932**
50933** For example, if an IO error occurs while performing a rollback,
50934** the contents of the page-cache may be left in an inconsistent state.
50935** At this point it would be dangerous to change back to READER state
50936** (as usually happens after a rollback). Any subsequent readers might
50937** report database corruption (due to the inconsistent cache), and if
50938** they upgrade to writers, they may inadvertently corrupt the database
50939** file. To avoid this hazard, the pager switches into the ERROR state
50940** instead of READER following such an error.
50941**
50942** Once it has entered the ERROR state, any attempt to use the pager
50943** to read or write data returns an error. Eventually, once all
50944** outstanding transactions have been abandoned, the pager is able to
50945** transition back to OPEN state, discarding the contents of the
50946** page-cache and any other in-memory state at the same time. Everything
50947** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
50948** when a read-transaction is next opened on the pager (transitioning
50949** the pager into READER state). At that point the system has recovered
50950** from the error.
50951**
50952** Specifically, the pager jumps into the ERROR state if:
50953**
50954** 1. An error occurs while attempting a rollback. This happens in
50955** function sqlite3PagerRollback().
50956**
50957** 2. An error occurs while attempting to finalize a journal file
50958** following a commit in function sqlite3PagerCommitPhaseTwo().
50959**
50960** 3. An error occurs while attempting to write to the journal or
50961** database file in function pagerStress() in order to free up
50962** memory.
50963**
50964** In other cases, the error is returned to the b-tree layer. The b-tree
50965** layer then attempts a rollback operation. If the error condition
50966** persists, the pager enters the ERROR state via condition (1) above.
50967**
50968** Condition (3) is necessary because it can be triggered by a read-only
50969** statement executed within a transaction. In this case, if the error
50970** code were simply returned to the user, the b-tree layer would not
50971** automatically attempt a rollback, as it assumes that an error in a
50972** read-only statement cannot leave the pager in an internally inconsistent
50973** state.
50974**
50975** * The Pager.errCode variable is set to something other than SQLITE_OK.
50976** * There are one or more outstanding references to pages (after the
50977** last reference is dropped the pager should move back to OPEN state).
50978** * The pager is not an in-memory pager.
50979**
50980**
50981** Notes:
50982**
50983** * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
50984** connection is open in WAL mode. A WAL connection is always in one
50985** of the first four states.
50986**
50987** * Normally, a connection open in exclusive mode is never in PAGER_OPEN
50988** state. There are two exceptions: immediately after exclusive-mode has
50989** been turned on (and before any read or write transactions are
50990** executed), and when the pager is leaving the "error state".
50991**
50992** * See also: assert_pager_state().
50993*/
50994#define PAGER_OPEN0 0
50995#define PAGER_READER1 1
50996#define PAGER_WRITER_LOCKED2 2
50997#define PAGER_WRITER_CACHEMOD3 3
50998#define PAGER_WRITER_DBMOD4 4
50999#define PAGER_WRITER_FINISHED5 5
51000#define PAGER_ERROR6 6
51001
51002/*
51003** The Pager.eLock variable is almost always set to one of the
51004** following locking-states, according to the lock currently held on
51005** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
51006** This variable is kept up to date as locks are taken and released by
51007** the pagerLockDb() and pagerUnlockDb() wrappers.
51008**
51009** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
51010** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
51011** the operation was successful. In these circumstances pagerLockDb() and
51012** pagerUnlockDb() take a conservative approach - eLock is always updated
51013** when unlocking the file, and only updated when locking the file if the
51014** VFS call is successful. This way, the Pager.eLock variable may be set
51015** to a less exclusive (lower) value than the lock that is actually held
51016** at the system level, but it is never set to a more exclusive value.
51017**
51018** This is usually safe. If an xUnlock fails or appears to fail, there may
51019** be a few redundant xLock() calls or a lock may be held for longer than
51020** required, but nothing really goes wrong.
51021**
51022** The exception is when the database file is unlocked as the pager moves
51023** from ERROR to OPEN state. At this point there may be a hot-journal file
51024** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
51025** transition, by the same pager or any other). If the call to xUnlock()
51026** fails at this point and the pager is left holding an EXCLUSIVE lock, this
51027** can confuse the call to xCheckReservedLock() call made later as part
51028** of hot-journal detection.
51029**
51030** xCheckReservedLock() is defined as returning true "if there is a RESERVED
51031** lock held by this process or any others". So xCheckReservedLock may
51032** return true because the caller itself is holding an EXCLUSIVE lock (but
51033** doesn't know it because of a previous error in xUnlock). If this happens
51034** a hot-journal may be mistaken for a journal being created by an active
51035** transaction in another process, causing SQLite to read from the database
51036** without rolling it back.
51037**
51038** To work around this, if a call to xUnlock() fails when unlocking the
51039** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
51040** is only changed back to a real locking state after a successful call
51041** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
51042** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
51043** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
51044** lock on the database file before attempting to roll it back. See function
51045** PagerSharedLock() for more detail.
51046**
51047** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
51048** PAGER_OPEN state.
51049*/
51050#define UNKNOWN_LOCK(4 +1) (EXCLUSIVE_LOCK4+1)
51051
51052/*
51053** A macro used for invoking the codec if there is one
51054*/
51055#ifdef SQLITE_HAS_CODEC
51056# define CODEC1(P,D,N,X,E) \
51057 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
51058# define CODEC2(P,D,N,X,E,O)O=(char*)D \
51059 if( P->xCodec==0 ){ O=(char*)D; }else \
51060 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
51061#else
51062# define CODEC1(P,D,N,X,E) /* NO-OP */
51063# define CODEC2(P,D,N,X,E,O)O=(char*)D O=(char*)D
51064#endif
51065
51066/*
51067** The maximum allowed sector size. 64KiB. If the xSectorsize() method
51068** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
51069** This could conceivably cause corruption following a power failure on
51070** such a system. This is currently an undocumented limit.
51071*/
51072#define MAX_SECTOR_SIZE0x10000 0x10000
51073
51074
51075/*
51076** An instance of the following structure is allocated for each active
51077** savepoint and statement transaction in the system. All such structures
51078** are stored in the Pager.aSavepoint[] array, which is allocated and
51079** resized using sqlite3Realloc().
51080**
51081** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
51082** set to 0. If a journal-header is written into the main journal while
51083** the savepoint is active, then iHdrOffset is set to the byte offset
51084** immediately following the last journal record written into the main
51085** journal before the journal-header. This is required during savepoint
51086** rollback (see pagerPlaybackSavepoint()).
51087*/
51088typedef struct PagerSavepoint PagerSavepoint;
51089struct PagerSavepoint {
51090 i64 iOffset; /* Starting offset in main journal */
51091 i64 iHdrOffset; /* See above */
51092 Bitvec *pInSavepoint; /* Set of pages in this savepoint */
51093 Pgno nOrig; /* Original number of pages in file */
51094 Pgno iSubRec; /* Index of first record in sub-journal */
51095#ifndef SQLITE_OMIT_WAL
51096 u32 aWalData[WAL_SAVEPOINT_NDATA4]; /* WAL savepoint context */
51097#endif
51098};
51099
51100/*
51101** Bits of the Pager.doNotSpill flag. See further description below.
51102*/
51103#define SPILLFLAG_OFF0x01 0x01 /* Never spill cache. Set via pragma */
51104#define SPILLFLAG_ROLLBACK0x02 0x02 /* Current rolling back, so do not spill */
51105#define SPILLFLAG_NOSYNC0x04 0x04 /* Spill is ok, but do not sync */
51106
51107/*
51108** An open page cache is an instance of struct Pager. A description of
51109** some of the more important member variables follows:
51110**
51111** eState
51112**
51113** The current 'state' of the pager object. See the comment and state
51114** diagram above for a description of the pager state.
51115**
51116** eLock
51117**
51118** For a real on-disk database, the current lock held on the database file -
51119** NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
51120**
51121** For a temporary or in-memory database (neither of which require any
51122** locks), this variable is always set to EXCLUSIVE_LOCK. Since such
51123** databases always have Pager.exclusiveMode==1, this tricks the pager
51124** logic into thinking that it already has all the locks it will ever
51125** need (and no reason to release them).
51126**
51127** In some (obscure) circumstances, this variable may also be set to
51128** UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
51129** details.
51130**
51131** changeCountDone
51132**
51133** This boolean variable is used to make sure that the change-counter
51134** (the 4-byte header field at byte offset 24 of the database file) is
51135** not updated more often than necessary.
51136**
51137** It is set to true when the change-counter field is updated, which
51138** can only happen if an exclusive lock is held on the database file.
51139** It is cleared (set to false) whenever an exclusive lock is
51140** relinquished on the database file. Each time a transaction is committed,
51141** The changeCountDone flag is inspected. If it is true, the work of
51142** updating the change-counter is omitted for the current transaction.
51143**
51144** This mechanism means that when running in exclusive mode, a connection
51145** need only update the change-counter once, for the first transaction
51146** committed.
51147**
51148** setMaster
51149**
51150** When PagerCommitPhaseOne() is called to commit a transaction, it may
51151** (or may not) specify a master-journal name to be written into the
51152** journal file before it is synced to disk.
51153**
51154** Whether or not a journal file contains a master-journal pointer affects
51155** the way in which the journal file is finalized after the transaction is
51156** committed or rolled back when running in "journal_mode=PERSIST" mode.
51157** If a journal file does not contain a master-journal pointer, it is
51158** finalized by overwriting the first journal header with zeroes. If
51159** it does contain a master-journal pointer the journal file is finalized
51160** by truncating it to zero bytes, just as if the connection were
51161** running in "journal_mode=truncate" mode.
51162**
51163** Journal files that contain master journal pointers cannot be finalized
51164** simply by overwriting the first journal-header with zeroes, as the
51165** master journal pointer could interfere with hot-journal rollback of any
51166** subsequently interrupted transaction that reuses the journal file.
51167**
51168** The flag is cleared as soon as the journal file is finalized (either
51169** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
51170** journal file from being successfully finalized, the setMaster flag
51171** is cleared anyway (and the pager will move to ERROR state).
51172**
51173** doNotSpill
51174**
51175** This variables control the behavior of cache-spills (calls made by
51176** the pcache module to the pagerStress() routine to write cached data
51177** to the file-system in order to free up memory).
51178**
51179** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
51180** writing to the database from pagerStress() is disabled altogether.
51181** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
51182** comes up during savepoint rollback that requires the pcache module
51183** to allocate a new page to prevent the journal file from being written
51184** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
51185** case is a user preference.
51186**
51187** If the SPILLFLAG_NOSYNC bit is set, writing to the database from
51188** pagerStress() is permitted, but syncing the journal file is not.
51189** This flag is set by sqlite3PagerWrite() when the file-system sector-size
51190** is larger than the database page-size in order to prevent a journal sync
51191** from happening in between the journalling of two pages on the same sector.
51192**
51193** subjInMemory
51194**
51195** This is a boolean variable. If true, then any required sub-journal
51196** is opened as an in-memory journal file. If false, then in-memory
51197** sub-journals are only used for in-memory pager files.
51198**
51199** This variable is updated by the upper layer each time a new
51200** write-transaction is opened.
51201**
51202** dbSize, dbOrigSize, dbFileSize
51203**
51204** Variable dbSize is set to the number of pages in the database file.
51205** It is valid in PAGER_READER and higher states (all states except for
51206** OPEN and ERROR).
51207**
51208** dbSize is set based on the size of the database file, which may be
51209** larger than the size of the database (the value stored at offset
51210** 28 of the database header by the btree). If the size of the file
51211** is not an integer multiple of the page-size, the value stored in
51212** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
51213** Except, any file that is greater than 0 bytes in size is considered
51214** to have at least one page. (i.e. a 1KB file with 2K page-size leads
51215** to dbSize==1).
51216**
51217** During a write-transaction, if pages with page-numbers greater than
51218** dbSize are modified in the cache, dbSize is updated accordingly.
51219** Similarly, if the database is truncated using PagerTruncateImage(),
51220** dbSize is updated.
51221**
51222** Variables dbOrigSize and dbFileSize are valid in states
51223** PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
51224** variable at the start of the transaction. It is used during rollback,
51225** and to determine whether or not pages need to be journalled before
51226** being modified.
51227**
51228** Throughout a write-transaction, dbFileSize contains the size of
51229** the file on disk in pages. It is set to a copy of dbSize when the
51230** write-transaction is first opened, and updated when VFS calls are made
51231** to write or truncate the database file on disk.
51232**
51233** The only reason the dbFileSize variable is required is to suppress
51234** unnecessary calls to xTruncate() after committing a transaction. If,
51235** when a transaction is committed, the dbFileSize variable indicates
51236** that the database file is larger than the database image (Pager.dbSize),
51237** pager_truncate() is called. The pager_truncate() call uses xFilesize()
51238** to measure the database file on disk, and then truncates it if required.
51239** dbFileSize is not used when rolling back a transaction. In this case
51240** pager_truncate() is called unconditionally (which means there may be
51241** a call to xFilesize() that is not strictly required). In either case,
51242** pager_truncate() may cause the file to become smaller or larger.
51243**
51244** dbHintSize
51245**
51246** The dbHintSize variable is used to limit the number of calls made to
51247** the VFS xFileControl(FCNTL_SIZE_HINT) method.
51248**
51249** dbHintSize is set to a copy of the dbSize variable when a
51250** write-transaction is opened (at the same time as dbFileSize and
51251** dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
51252** dbHintSize is increased to the number of pages that correspond to the
51253** size-hint passed to the method call. See pager_write_pagelist() for
51254** details.
51255**
51256** errCode
51257**
51258** The Pager.errCode variable is only ever used in PAGER_ERROR state. It
51259** is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
51260** is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
51261** sub-codes.
51262**
51263** syncFlags, walSyncFlags
51264**
51265** syncFlags is either SQLITE_SYNC_NORMAL (0x02) or SQLITE_SYNC_FULL (0x03).
51266** syncFlags is used for rollback mode. walSyncFlags is used for WAL mode
51267** and contains the flags used to sync the checkpoint operations in the
51268** lower two bits, and sync flags used for transaction commits in the WAL
51269** file in bits 0x04 and 0x08. In other words, to get the correct sync flags
51270** for checkpoint operations, use (walSyncFlags&0x03) and to get the correct
51271** sync flags for transaction commit, use ((walSyncFlags>>2)&0x03). Note
51272** that with synchronous=NORMAL in WAL mode, transaction commit is not synced
51273** meaning that the 0x04 and 0x08 bits are both zero.
51274*/
51275struct Pager {
51276 sqlite3_vfs *pVfs; /* OS functions to use for IO */
51277 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
51278 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
51279 u8 useJournal; /* Use a rollback journal on this file */
51280 u8 noSync; /* Do not sync the journal if true */
51281 u8 fullSync; /* Do extra syncs of the journal for robustness */
51282 u8 extraSync; /* sync directory after journal delete */
51283 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
51284 u8 walSyncFlags; /* See description above */
51285 u8 tempFile; /* zFilename is a temporary or immutable file */
51286 u8 noLock; /* Do not lock (except in WAL mode) */
51287 u8 readOnly; /* True for a read-only database */
51288 u8 memDb; /* True to inhibit all file I/O */
51289
51290 /**************************************************************************
51291 ** The following block contains those class members that change during
51292 ** routine operation. Class members not in this block are either fixed
51293 ** when the pager is first created or else only change when there is a
51294 ** significant mode change (such as changing the page_size, locking_mode,
51295 ** or the journal_mode). From another view, these class members describe
51296 ** the "state" of the pager, while other class members describe the
51297 ** "configuration" of the pager.
51298 */
51299 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
51300 u8 eLock; /* Current lock held on database file */
51301 u8 changeCountDone; /* Set after incrementing the change-counter */
51302 u8 setMaster; /* True if a m-j name has been written to jrnl */
51303 u8 doNotSpill; /* Do not spill the cache when non-zero */
51304 u8 subjInMemory; /* True to use in-memory sub-journals */
51305 u8 bUseFetch; /* True to use xFetch() */
51306 u8 hasHeldSharedLock; /* True if a shared lock has ever been held */
51307 Pgno dbSize; /* Number of pages in the database */
51308 Pgno dbOrigSize; /* dbSize before the current transaction */
51309 Pgno dbFileSize; /* Number of pages in the database file */
51310 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
51311 int errCode; /* One of several kinds of errors */
51312 int nRec; /* Pages journalled since last j-header written */
51313 u32 cksumInit; /* Quasi-random value added to every checksum */
51314 u32 nSubRec; /* Number of records written to sub-journal */
51315 Bitvec *pInJournal; /* One bit for each page in the database file */
51316 sqlite3_file *fd; /* File descriptor for database */
51317 sqlite3_file *jfd; /* File descriptor for main journal */
51318 sqlite3_file *sjfd; /* File descriptor for sub-journal */
51319 i64 journalOff; /* Current write offset in the journal file */
51320 i64 journalHdr; /* Byte offset to previous journal header */
51321 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
51322 PagerSavepoint *aSavepoint; /* Array of active savepoints */
51323 int nSavepoint; /* Number of elements in aSavepoint[] */
51324 u32 iDataVersion; /* Changes whenever database content changes */
51325 char dbFileVers[16]; /* Changes whenever database file changes */
51326
51327 int nMmapOut; /* Number of mmap pages currently outstanding */
51328 sqlite3_int64 szMmap; /* Desired maximum mmap size */
51329 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
51330 /*
51331 ** End of the routinely-changing class members
51332 ***************************************************************************/
51333
51334 u16 nExtra; /* Add this many bytes to each in-memory page */
51335 i16 nReserve; /* Number of unused bytes at end of each page */
51336 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
51337 u32 sectorSize; /* Assumed sector size during rollback */
51338 int pageSize; /* Number of bytes in a page */
51339 Pgno mxPgno; /* Maximum allowed size of the database */
51340 i64 journalSizeLimit; /* Size limit for persistent journal files */
51341 char *zFilename; /* Name of the database file */
51342 char *zJournal; /* Name of the journal file */
51343 int (*xBusyHandler)(void*); /* Function to call when busy */
51344 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
51345 int aStat[4]; /* Total cache hits, misses, writes, spills */
51346#ifdef SQLITE_TEST
51347 int nRead; /* Database pages read */
51348#endif
51349 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
51350 int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
51351#ifdef SQLITE_HAS_CODEC
51352 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
51353 void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
51354 void (*xCodecFree)(void*); /* Destructor for the codec */
51355 void *pCodec; /* First argument to xCodec... methods */
51356#endif
51357 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
51358 PCache *pPCache; /* Pointer to page cache object */
51359#ifndef SQLITE_OMIT_WAL
51360 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
51361 char *zWal; /* File name for write-ahead log */
51362#endif
51363};
51364
51365/*
51366** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
51367** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
51368** or CACHE_WRITE to sqlite3_db_status().
51369*/
51370#define PAGER_STAT_HIT0 0
51371#define PAGER_STAT_MISS1 1
51372#define PAGER_STAT_WRITE2 2
51373#define PAGER_STAT_SPILL3 3
51374
51375/*
51376** The following global variables hold counters used for
51377** testing purposes only. These variables do not exist in
51378** a non-testing build. These variables are not thread-safe.
51379*/
51380#ifdef SQLITE_TEST
51381SQLITE_API int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
51382SQLITE_API int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
51383SQLITE_API int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
51384# define PAGER_INCR(v) v++
51385#else
51386# define PAGER_INCR(v)
51387#endif
51388
51389
51390
51391/*
51392** Journal files begin with the following magic string. The data
51393** was obtained from /dev/random. It is used only as a sanity check.
51394**
51395** Since version 2.8.0, the journal format contains additional sanity
51396** checking information. If the power fails while the journal is being
51397** written, semi-random garbage data might appear in the journal
51398** file after power is restored. If an attempt is then made
51399** to roll the journal back, the database could be corrupted. The additional
51400** sanity checking data is an attempt to discover the garbage in the
51401** journal and ignore it.
51402**
51403** The sanity checking information for the new journal format consists
51404** of a 32-bit checksum on each page of data. The checksum covers both
51405** the page number and the pPager->pageSize bytes of data for the page.
51406** This cksum is initialized to a 32-bit random value that appears in the
51407** journal file right after the header. The random initializer is important,
51408** because garbage data that appears at the end of a journal is likely
51409** data that was once in other files that have now been deleted. If the
51410** garbage data came from an obsolete journal file, the checksums might
51411** be correct. But by initializing the checksum to random value which
51412** is different for every journal, we minimize that risk.
51413*/
51414static const unsigned char aJournalMagic[] = {
51415 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
51416};
51417
51418/*
51419** The size of the of each page record in the journal is given by
51420** the following macro.
51421*/
51422#define JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8) ((pPager->pageSize) + 8)
51423
51424/*
51425** The journal header size for this pager. This is usually the same
51426** size as a single disk sector. See also setSectorSize().
51427*/
51428#define JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) (pPager->sectorSize)
51429
51430/*
51431** The macro MEMDB is true if we are dealing with an in-memory database.
51432** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
51433** the value of MEMDB will be a constant and the compiler will optimize
51434** out code that would never execute.
51435*/
51436#ifdef SQLITE_OMIT_MEMORYDB
51437# define MEMDBpPager->memDb 0
51438#else
51439# define MEMDBpPager->memDb pPager->memDb
51440#endif
51441
51442/*
51443** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
51444** interfaces to access the database using memory-mapped I/O.
51445*/
51446#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
51447# define USEFETCH(x)((x)->bUseFetch) ((x)->bUseFetch)
51448#else
51449# define USEFETCH(x)((x)->bUseFetch) 0
51450#endif
51451
51452/*
51453** The maximum legal page number is (2^31 - 1).
51454*/
51455#define PAGER_MAX_PGNO2147483647 2147483647
51456
51457/*
51458** The argument to this macro is a file descriptor (type sqlite3_file*).
51459** Return 0 if it is not open, or non-zero (but not 1) if it is.
51460**
51461** This is so that expressions can be written as:
51462**
51463** if( isOpen(pPager->jfd) ){ ...
51464**
51465** instead of
51466**
51467** if( pPager->jfd->pMethods ){ ...
51468*/
51469#define isOpen(pFd)((pFd)->pMethods!=0) ((pFd)->pMethods!=0)
51470
51471#ifdef SQLITE_DIRECT_OVERFLOW_READ
51472/*
51473** Return true if page pgno can be read directly from the database file
51474** by the b-tree layer. This is the case if:
51475**
51476** * the database file is open,
51477** * there are no dirty pages in the cache, and
51478** * the desired page is not currently in the wal file.
51479*/
51480SQLITE_PRIVATEstatic int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
51481 if( pPager->fd->pMethods==0 ) return 0;
51482 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
51483#ifdef SQLITE_HAS_CODEC
51484 if( pPager->xCodec!=0 ) return 0;
51485#endif
51486#ifndef SQLITE_OMIT_WAL
51487 if( pPager->pWal ){
51488 u32 iRead = 0;
51489 int rc;
51490 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
51491 return (rc==SQLITE_OK0 && iRead==0);
51492 }
51493#endif
51494 return 1;
51495}
51496#endif
51497
51498#ifndef SQLITE_OMIT_WAL
51499# define pagerUseWal(x)((x)->pWal!=0) ((x)->pWal!=0)
51500#else
51501# define pagerUseWal(x)((x)->pWal!=0) 0
51502# define pagerRollbackWal(x) 0
51503# define pagerWalFrames(v,w,x,y) 0
51504# define pagerOpenWalIfPresent(z) SQLITE_OK0
51505# define pagerBeginReadTransaction(z) SQLITE_OK0
51506#endif
51507
51508#ifndef NDEBUG1
51509/*
51510** Usage:
51511**
51512** assert( assert_pager_state(pPager) );
51513**
51514** This function runs many asserts to try to find inconsistencies in
51515** the internal state of the Pager object.
51516*/
51517static int assert_pager_state(Pager *p){
51518 Pager *pPager = p;
51519
51520 /* State must be valid. */
51521 assert( p->eState==PAGER_OPEN((void) (0))
51522 || p->eState==PAGER_READER((void) (0))
51523 || p->eState==PAGER_WRITER_LOCKED((void) (0))
51524 || p->eState==PAGER_WRITER_CACHEMOD((void) (0))
51525 || p->eState==PAGER_WRITER_DBMOD((void) (0))
51526 || p->eState==PAGER_WRITER_FINISHED((void) (0))
51527 || p->eState==PAGER_ERROR((void) (0))
51528 )((void) (0));
51529
51530 /* Regardless of the current state, a temp-file connection always behaves
51531 ** as if it has an exclusive lock on the database file. It never updates
51532 ** the change-counter field, so the changeCountDone flag is always set.
51533 */
51534 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK )((void) (0));
51535 assert( p->tempFile==0 || pPager->changeCountDone )((void) (0));
51536
51537 /* If the useJournal flag is clear, the journal-mode must be "OFF".
51538 ** And if the journal-mode is "OFF", the journal file must not be open.
51539 */
51540 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal )((void) (0));
51541 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) )((void) (0));
51542
51543 /* Check that MEMDB implies noSync. And an in-memory journal. Since
51544 ** this means an in-memory pager performs no IO at all, it cannot encounter
51545 ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
51546 ** a journal file. (although the in-memory journal implementation may
51547 ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
51548 ** is therefore not possible for an in-memory pager to enter the ERROR
51549 ** state.
51550 */
51551 if( MEMDBpPager->memDb ){
51552 assert( !isOpen(p->fd) )((void) (0));
51553 assert( p->noSync )((void) (0));
51554 assert( p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
51555 || p->journalMode==PAGER_JOURNALMODE_MEMORY((void) (0))
51556 )((void) (0));
51557 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN )((void) (0));
51558 assert( pagerUseWal(p)==0 )((void) (0));
51559 }
51560
51561 /* If changeCountDone is set, a RESERVED lock or greater must be held
51562 ** on the file.
51563 */
51564 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK )((void) (0));
51565 assert( p->eLock!=PENDING_LOCK )((void) (0));
51566
51567 switch( p->eState ){
51568 case PAGER_OPEN0:
51569 assert( !MEMDB )((void) (0));
51570 assert( pPager->errCode==SQLITE_OK )((void) (0));
51571 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile )((void) (0));
51572 break;
51573
51574 case PAGER_READER1:
51575 assert( pPager->errCode==SQLITE_OK )((void) (0));
51576 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
51577 assert( p->eLock>=SHARED_LOCK )((void) (0));
51578 break;
51579
51580 case PAGER_WRITER_LOCKED2:
51581 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
51582 assert( pPager->errCode==SQLITE_OK )((void) (0));
51583 if( !pagerUseWal(pPager)((pPager)->pWal!=0) ){
51584 assert( p->eLock>=RESERVED_LOCK )((void) (0));
51585 }
51586 assert( pPager->dbSize==pPager->dbOrigSize )((void) (0));
51587 assert( pPager->dbOrigSize==pPager->dbFileSize )((void) (0));
51588 assert( pPager->dbOrigSize==pPager->dbHintSize )((void) (0));
51589 assert( pPager->setMaster==0 )((void) (0));
51590 break;
51591
51592 case PAGER_WRITER_CACHEMOD3:
51593 assert( p->eLock!=UNKNOWN_LOCK )((void) (0));
51594 assert( pPager->errCode==SQLITE_OK )((void) (0));
51595 if( !pagerUseWal(pPager)((pPager)->pWal!=0) ){
51596 /* It is possible that if journal_mode=wal here that neither the
51597 ** journal file nor the WAL file are open. This happens during
51598 ** a rollback transaction that switches from journal_mode=off
51599 ** to journal_mode=wal.
51600 */
51601 assert( p->eLock>=RESERVED_LOCK )((void) (0));
51602 assert( isOpen(p->jfd)((void) (0))
51603 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
51604 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
51605 )((void) (0));
51606 }
51607 assert( pPager->dbOrigSize==pPager->dbFileSize )((void) (0));
51608 assert( pPager->dbOrigSize==pPager->dbHintSize )((void) (0));
51609 break;
51610
51611 case PAGER_WRITER_DBMOD4:
51612 assert( p->eLock==EXCLUSIVE_LOCK )((void) (0));
51613 assert( pPager->errCode==SQLITE_OK )((void) (0));
51614 assert( !pagerUseWal(pPager) )((void) (0));
51615 assert( p->eLock>=EXCLUSIVE_LOCK )((void) (0));
51616 assert( isOpen(p->jfd)((void) (0))
51617 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
51618 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
51619 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
51620 )((void) (0));
51621 assert( pPager->dbOrigSize<=pPager->dbHintSize )((void) (0));
51622 break;
51623
51624 case PAGER_WRITER_FINISHED5:
51625 assert( p->eLock==EXCLUSIVE_LOCK )((void) (0));
51626 assert( pPager->errCode==SQLITE_OK )((void) (0));
51627 assert( !pagerUseWal(pPager) )((void) (0));
51628 assert( isOpen(p->jfd)((void) (0))
51629 || p->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
51630 || p->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
51631 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
51632 )((void) (0));
51633 break;
51634
51635 case PAGER_ERROR6:
51636 /* There must be at least one outstanding reference to the pager if
51637 ** in ERROR state. Otherwise the pager should have already dropped
51638 ** back to OPEN state.
51639 */
51640 assert( pPager->errCode!=SQLITE_OK )((void) (0));
51641 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile )((void) (0));
51642 break;
51643 }
51644
51645 return 1;
51646}
51647#endif /* ifndef NDEBUG */
51648
51649#ifdef SQLITE_DEBUG
51650/*
51651** Return a pointer to a human readable string in a static buffer
51652** containing the state of the Pager object passed as an argument. This
51653** is intended to be used within debuggers. For example, as an alternative
51654** to "print *pPager" in gdb:
51655**
51656** (gdb) printf "%s", print_pager_state(pPager)
51657**
51658** This routine has external linkage in order to suppress compiler warnings
51659** about an unused function. It is enclosed within SQLITE_DEBUG and so does
51660** not appear in normal builds.
51661*/
51662char *print_pager_state(Pager *p){
51663 static char zRet[1024];
51664
51665 sqlite3_snprintf(1024, zRet,
51666 "Filename: %s\n"
51667 "State: %s errCode=%d\n"
51668 "Lock: %s\n"
51669 "Locking mode: locking_mode=%s\n"
51670 "Journal mode: journal_mode=%s\n"
51671 "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
51672 "Journal: journalOff=%lld journalHdr=%lld\n"
51673 "Size: dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
51674 , p->zFilename
51675 , p->eState==PAGER_OPEN0 ? "OPEN" :
51676 p->eState==PAGER_READER1 ? "READER" :
51677 p->eState==PAGER_WRITER_LOCKED2 ? "WRITER_LOCKED" :
51678 p->eState==PAGER_WRITER_CACHEMOD3 ? "WRITER_CACHEMOD" :
51679 p->eState==PAGER_WRITER_DBMOD4 ? "WRITER_DBMOD" :
51680 p->eState==PAGER_WRITER_FINISHED5 ? "WRITER_FINISHED" :
51681 p->eState==PAGER_ERROR6 ? "ERROR" : "?error?"
51682 , (int)p->errCode
51683 , p->eLock==NO_LOCK0 ? "NO_LOCK" :
51684 p->eLock==RESERVED_LOCK2 ? "RESERVED" :
51685 p->eLock==EXCLUSIVE_LOCK4 ? "EXCLUSIVE" :
51686 p->eLock==SHARED_LOCK1 ? "SHARED" :
51687 p->eLock==UNKNOWN_LOCK(4 +1) ? "UNKNOWN" : "?error?"
51688 , p->exclusiveMode ? "exclusive" : "normal"
51689 , p->journalMode==PAGER_JOURNALMODE_MEMORY4 ? "memory" :
51690 p->journalMode==PAGER_JOURNALMODE_OFF2 ? "off" :
51691 p->journalMode==PAGER_JOURNALMODE_DELETE0 ? "delete" :
51692 p->journalMode==PAGER_JOURNALMODE_PERSIST1 ? "persist" :
51693 p->journalMode==PAGER_JOURNALMODE_TRUNCATE3 ? "truncate" :
51694 p->journalMode==PAGER_JOURNALMODE_WAL5 ? "wal" : "?error?"
51695 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
51696 , p->journalOff, p->journalHdr
51697 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
51698 );
51699
51700 return zRet;
51701}
51702#endif
51703
51704/* Forward references to the various page getters */
51705static int getPageNormal(Pager*,Pgno,DbPage**,int);
51706static int getPageError(Pager*,Pgno,DbPage**,int);
51707#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
51708static int getPageMMap(Pager*,Pgno,DbPage**,int);
51709#endif
51710
51711/*
51712** Set the Pager.xGet method for the appropriate routine used to fetch
51713** content from the pager.
51714*/
51715static void setGetterMethod(Pager *pPager){
51716 if( pPager->errCode ){
51717 pPager->xGet = getPageError;
51718#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
51719 }else if( USEFETCH(pPager)((pPager)->bUseFetch)
51720#ifdef SQLITE_HAS_CODEC
51721 && pPager->xCodec==0
51722#endif
51723 ){
51724 pPager->xGet = getPageMMap;
51725#endif /* SQLITE_MAX_MMAP_SIZE>0 */
51726 }else{
51727 pPager->xGet = getPageNormal;
51728 }
51729}
51730
51731/*
51732** Return true if it is necessary to write page *pPg into the sub-journal.
51733** A page needs to be written into the sub-journal if there exists one
51734** or more open savepoints for which:
51735**
51736** * The page-number is less than or equal to PagerSavepoint.nOrig, and
51737** * The bit corresponding to the page-number is not set in
51738** PagerSavepoint.pInSavepoint.
51739*/
51740static int subjRequiresPage(PgHdr *pPg){
51741 Pager *pPager = pPg->pPager;
51742 PagerSavepoint *p;
51743 Pgno pgno = pPg->pgno;
51744 int i;
51745 for(i=0; i<pPager->nSavepoint; i++){
51746 p = &pPager->aSavepoint[i];
51747 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
51748 return 1;
51749 }
51750 }
51751 return 0;
51752}
51753
51754#ifdef SQLITE_DEBUG
51755/*
51756** Return true if the page is already in the journal file.
51757*/
51758static int pageInJournal(Pager *pPager, PgHdr *pPg){
51759 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
51760}
51761#endif
51762
51763/*
51764** Read a 32-bit integer from the given file descriptor. Store the integer
51765** that is read in *pRes. Return SQLITE_OK if everything worked, or an
51766** error code is something goes wrong.
51767**
51768** All values are stored on disk as big-endian.
51769*/
51770static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
51771 unsigned char ac[4];
51772 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
51773 if( rc==SQLITE_OK0 ){
51774 *pRes = sqlite3Get4byte(ac);
51775 }
51776 return rc;
51777}
51778
51779/*
51780** Write a 32-bit integer into a string buffer in big-endian byte order.
51781*/
51782#define put32bits(A,B)sqlite3Put4byte((u8*)A,B) sqlite3Put4byte((u8*)A,B)
51783
51784
51785/*
51786** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
51787** on success or an error code is something goes wrong.
51788*/
51789static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
51790 char ac[4];
51791 put32bits(ac, val)sqlite3Put4byte((u8*)ac,val);
51792 return sqlite3OsWrite(fd, ac, 4, offset);
51793}
51794
51795/*
51796** Unlock the database file to level eLock, which must be either NO_LOCK
51797** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
51798** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
51799**
51800** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
51801** called, do not modify it. See the comment above the #define of
51802** UNKNOWN_LOCK for an explanation of this.
51803*/
51804static int pagerUnlockDb(Pager *pPager, int eLock){
51805 int rc = SQLITE_OK0;
51806
51807 assert( !pPager->exclusiveMode || pPager->eLock==eLock )((void) (0));
51808 assert( eLock==NO_LOCK || eLock==SHARED_LOCK )((void) (0));
51809 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 )((void) (0));
51810 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
51811 assert( pPager->eLock>=eLock )((void) (0));
51812 rc = pPager->noLock ? SQLITE_OK0 : sqlite3OsUnlock(pPager->fd, eLock);
51813 if( pPager->eLock!=UNKNOWN_LOCK(4 +1) ){
51814 pPager->eLock = (u8)eLock;
51815 }
51816 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
51817 }
51818 return rc;
51819}
51820
51821/*
51822** Lock the database file to level eLock, which must be either SHARED_LOCK,
51823** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
51824** Pager.eLock variable to the new locking state.
51825**
51826** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
51827** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
51828** See the comment above the #define of UNKNOWN_LOCK for an explanation
51829** of this.
51830*/
51831static int pagerLockDb(Pager *pPager, int eLock){
51832 int rc = SQLITE_OK0;
51833
51834 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK )((void) (0));
51835 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK(4 +1) ){
51836 rc = pPager->noLock ? SQLITE_OK0 : sqlite3OsLock(pPager->fd, eLock);
51837 if( rc==SQLITE_OK0 && (pPager->eLock!=UNKNOWN_LOCK(4 +1)||eLock==EXCLUSIVE_LOCK4) ){
51838 pPager->eLock = (u8)eLock;
51839 IOTRACE(("LOCK %p %d\n", pPager, eLock))
51840 }
51841 }
51842 return rc;
51843}
51844
51845/*
51846** This function determines whether or not the atomic-write or
51847** atomic-batch-write optimizations can be used with this pager. The
51848** atomic-write optimization can be used if:
51849**
51850** (a) the value returned by OsDeviceCharacteristics() indicates that
51851** a database page may be written atomically, and
51852** (b) the value returned by OsSectorSize() is less than or equal
51853** to the page size.
51854**
51855** If it can be used, then the value returned is the size of the journal
51856** file when it contains rollback data for exactly one page.
51857**
51858** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
51859** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
51860** returned in this case.
51861**
51862** If neither optimization can be used, 0 is returned.
51863*/
51864static int jrnlBufferSize(Pager *pPager){
51865 assert( !MEMDB )((void) (0));
51866
51867#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
51868 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
51869 int dc; /* Device characteristics */
51870
51871 assert( isOpen(pPager->fd) )((void) (0));
51872 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
51873#else
51874 UNUSED_PARAMETER(pPager)(void)(pPager);
51875#endif
51876
51877#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
51878 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC0x00004000) ){
51879 return -1;
51880 }
51881#endif
51882
51883#ifdef SQLITE_ENABLE_ATOMIC_WRITE
51884 {
51885 int nSector = pPager->sectorSize;
51886 int szPage = pPager->pageSize;
51887
51888 assert(SQLITE_IOCAP_ATOMIC512==(512>>8))((void) (0));
51889 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8))((void) (0));
51890 if( 0==(dc&(SQLITE_IOCAP_ATOMIC0x00000001|(szPage>>8)) || nSector>szPage) ){
51891 return 0;
51892 }
51893 }
51894
51895 return JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) + JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8);
51896#endif
51897
51898 return 0;
51899}
51900
51901/*
51902** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
51903** on the cache using a hash function. This is used for testing
51904** and debugging only.
51905*/
51906#ifdef SQLITE_CHECK_PAGES
51907/*
51908** Return a 32-bit hash of the page data for pPage.
51909*/
51910static u32 pager_datahash(int nByte, unsigned char *pData)0{
51911 u32 hash = 0;
51912 int i;
51913 for(i=0; i<nByte; i++){
51914 hash = (hash*1039) + pData[i];
51915 }
51916 return hash;
51917}
51918static u32 pager_pagehash(PgHdr *pPage)0{
51919 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData)0;
51920}
51921static void pager_set_pagehash(PgHdr *pPage){
51922 pPage->pageHash = pager_pagehash(pPage)0;
51923}
51924
51925/*
51926** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
51927** is defined, and NDEBUG is not defined, an assert() statement checks
51928** that the page is either dirty or still matches the calculated page-hash.
51929*/
51930#define CHECK_PAGE(x) checkPage(x)
51931static void checkPage(PgHdr *pPg){
51932 Pager *pPager = pPg->pPager;
51933 assert( pPager->eState!=PAGER_ERROR )((void) (0));
51934 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) )((void) (0));
51935}
51936
51937#else
51938#define pager_datahash(X,Y)0 0
51939#define pager_pagehash(X)0 0
51940#define pager_set_pagehash(X)
51941#define CHECK_PAGE(x)
51942#endif /* SQLITE_CHECK_PAGES */
51943
51944/*
51945** When this is called the journal file for pager pPager must be open.
51946** This function attempts to read a master journal file name from the
51947** end of the file and, if successful, copies it into memory supplied
51948** by the caller. See comments above writeMasterJournal() for the format
51949** used to store a master journal file name at the end of a journal file.
51950**
51951** zMaster must point to a buffer of at least nMaster bytes allocated by
51952** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
51953** enough space to write the master journal name). If the master journal
51954** name in the journal is longer than nMaster bytes (including a
51955** nul-terminator), then this is handled as if no master journal name
51956** were present in the journal.
51957**
51958** If a master journal file name is present at the end of the journal
51959** file, then it is copied into the buffer pointed to by zMaster. A
51960** nul-terminator byte is appended to the buffer following the master
51961** journal file name.
51962**
51963** If it is determined that no master journal file name is present
51964** zMaster[0] is set to 0 and SQLITE_OK returned.
51965**
51966** If an error occurs while reading from the journal file, an SQLite
51967** error code is returned.
51968*/
51969static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
51970 int rc; /* Return code */
51971 u32 len; /* Length in bytes of master journal name */
51972 i64 szJ; /* Total size in bytes of journal file pJrnl */
51973 u32 cksum; /* MJ checksum value read from journal */
51974 u32 u; /* Unsigned loop counter */
51975 unsigned char aMagic[8]; /* A buffer to hold the magic header */
51976 zMaster[0] = '\0';
51977
51978 if( SQLITE_OK0!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
51979 || szJ<16
51980 || SQLITE_OK0!=(rc = read32bits(pJrnl, szJ-16, &len))
51981 || len>=nMaster
51982 || len>szJ-16
51983 || len==0
51984 || SQLITE_OK0!=(rc = read32bits(pJrnl, szJ-12, &cksum))
51985 || SQLITE_OK0!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
51986 || memcmp(aMagic, aJournalMagic, 8)
51987 || SQLITE_OK0!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
51988 ){
51989 return rc;
51990 }
51991
51992 /* See if the checksum matches the master journal name */
51993 for(u=0; u<len; u++){
51994 cksum -= zMaster[u];
51995 }
51996 if( cksum ){
51997 /* If the checksum doesn't add up, then one or more of the disk sectors
51998 ** containing the master journal filename is corrupted. This means
51999 ** definitely roll back, so just return SQLITE_OK and report a (nul)
52000 ** master-journal filename.
52001 */
52002 len = 0;
52003 }
52004 zMaster[len] = '\0';
52005
52006 return SQLITE_OK0;
52007}
52008
52009/*
52010** Return the offset of the sector boundary at or immediately
52011** following the value in pPager->journalOff, assuming a sector
52012** size of pPager->sectorSize bytes.
52013**
52014** i.e for a sector size of 512:
52015**
52016** Pager.journalOff Return value
52017** ---------------------------------------
52018** 0 0
52019** 512 512
52020** 100 512
52021** 2000 2048
52022**
52023*/
52024static i64 journalHdrOffset(Pager *pPager){
52025 i64 offset = 0;
52026 i64 c = pPager->journalOff;
52027 if( c ){
52028 offset = ((c-1)/JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) + 1) * JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
52029 }
52030 assert( offset%JOURNAL_HDR_SZ(pPager)==0 )((void) (0));
52031 assert( offset>=c )((void) (0));
52032 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) )((void) (0));
52033 return offset;
52034}
52035
52036/*
52037** The journal file must be open when this function is called.
52038**
52039** This function is a no-op if the journal file has not been written to
52040** within the current transaction (i.e. if Pager.journalOff==0).
52041**
52042** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
52043** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
52044** zero the 28-byte header at the start of the journal file. In either case,
52045** if the pager is not in no-sync mode, sync the journal file immediately
52046** after writing or truncating it.
52047**
52048** If Pager.journalSizeLimit is set to a positive, non-zero value, and
52049** following the truncation or zeroing described above the size of the
52050** journal file in bytes is larger than this value, then truncate the
52051** journal file to Pager.journalSizeLimit bytes. The journal file does
52052** not need to be synced following this operation.
52053**
52054** If an IO error occurs, abandon processing and return the IO error code.
52055** Otherwise, return SQLITE_OK.
52056*/
52057static int zeroJournalHdr(Pager *pPager, int doTruncate){
52058 int rc = SQLITE_OK0; /* Return code */
52059 assert( isOpen(pPager->jfd) )((void) (0));
52060 assert( !sqlite3JournalIsInMemory(pPager->jfd) )((void) (0));
52061 if( pPager->journalOff ){
52062 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
52063
52064 IOTRACE(("JZEROHDR %p\n", pPager))
52065 if( doTruncate || iLimit==0 ){
52066 rc = sqlite3OsTruncate(pPager->jfd, 0);
52067 }else{
52068 static const char zeroHdr[28] = {0};
52069 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
52070 }
52071 if( rc==SQLITE_OK0 && !pPager->noSync ){
52072 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY0x00010|pPager->syncFlags);
52073 }
52074
52075 /* At this point the transaction is committed but the write lock
52076 ** is still held on the file. If there is a size limit configured for
52077 ** the persistent journal and the journal file currently consumes more
52078 ** space than that limit allows for, truncate it now. There is no need
52079 ** to sync the file following this operation.
52080 */
52081 if( rc==SQLITE_OK0 && iLimit>0 ){
52082 i64 sz;
52083 rc = sqlite3OsFileSize(pPager->jfd, &sz);
52084 if( rc==SQLITE_OK0 && sz>iLimit ){
52085 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
52086 }
52087 }
52088 }
52089 return rc;
52090}
52091
52092/*
52093** The journal file must be open when this routine is called. A journal
52094** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
52095** current location.
52096**
52097** The format for the journal header is as follows:
52098** - 8 bytes: Magic identifying journal format.
52099** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
52100** - 4 bytes: Random number used for page hash.
52101** - 4 bytes: Initial database page count.
52102** - 4 bytes: Sector size used by the process that wrote this journal.
52103** - 4 bytes: Database page size.
52104**
52105** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
52106*/
52107static int writeJournalHdr(Pager *pPager){
52108 int rc = SQLITE_OK0; /* Return code */
52109 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
52110 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
52111 u32 nWrite; /* Bytes of header sector written */
52112 int ii; /* Loop counter */
52113
52114 assert( isOpen(pPager->jfd) )((void) (0)); /* Journal file must be open. */
52115
52116 if( nHeader>JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) ){
52117 nHeader = JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
52118 }
52119
52120 /* If there are active savepoints and any of them were created
52121 ** since the most recent journal header was written, update the
52122 ** PagerSavepoint.iHdrOffset fields now.
52123 */
52124 for(ii=0; ii<pPager->nSavepoint; ii++){
52125 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
52126 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
52127 }
52128 }
52129
52130 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
52131
52132 /*
52133 ** Write the nRec Field - the number of page records that follow this
52134 ** journal header. Normally, zero is written to this value at this time.
52135 ** After the records are added to the journal (and the journal synced,
52136 ** if in full-sync mode), the zero is overwritten with the true number
52137 ** of records (see syncJournal()).
52138 **
52139 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
52140 ** reading the journal this value tells SQLite to assume that the
52141 ** rest of the journal file contains valid page records. This assumption
52142 ** is dangerous, as if a failure occurred whilst writing to the journal
52143 ** file it may contain some garbage data. There are two scenarios
52144 ** where this risk can be ignored:
52145 **
52146 ** * When the pager is in no-sync mode. Corruption can follow a
52147 ** power failure in this case anyway.
52148 **
52149 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
52150 ** that garbage data is never appended to the journal file.
52151 */
52152 assert( isOpen(pPager->fd) || pPager->noSync )((void) (0));
52153 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY4)
52154 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND0x00000200)
52155 ){
52156 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
52157 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)],0xffffffff
)
;
52158 }else{
52159 memset(zHeader, 0, sizeof(aJournalMagic)+4);
52160 }
52161
52162 /* The random check-hash initializer */
52163 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
52164 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+4],pPager
->cksumInit)
;
52165 /* The initial database size */
52166 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+8],pPager
->dbOrigSize)
;
52167 /* The assumed sector size for this process */
52168 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+12],pPager
->sectorSize)
;
52169
52170 /* The page size */
52171 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)+16],pPager
->pageSize)
;
52172
52173 /* Initializing the tail of the buffer is not necessary. Everything
52174 ** works find if the following memset() is omitted. But initializing
52175 ** the memory prevents valgrind from complaining, so we are willing to
52176 ** take the performance hit.
52177 */
52178 memset(&zHeader[sizeof(aJournalMagic)+20], 0,
52179 nHeader-(sizeof(aJournalMagic)+20));
52180
52181 /* In theory, it is only necessary to write the 28 bytes that the
52182 ** journal header consumes to the journal file here. Then increment the
52183 ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
52184 ** record is written to the following sector (leaving a gap in the file
52185 ** that will be implicitly filled in by the OS).
52186 **
52187 ** However it has been discovered that on some systems this pattern can
52188 ** be significantly slower than contiguously writing data to the file,
52189 ** even if that means explicitly writing data to the block of
52190 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
52191 ** is done.
52192 **
52193 ** The loop is required here in case the sector-size is larger than the
52194 ** database page size. Since the zHeader buffer is only Pager.pageSize
52195 ** bytes in size, more than one call to sqlite3OsWrite() may be required
52196 ** to populate the entire journal header sector.
52197 */
52198 for(nWrite=0; rc==SQLITE_OK0&&nWrite<JOURNAL_HDR_SZ(pPager)(pPager->sectorSize); nWrite+=nHeader){
52199 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
52200 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
52201 assert( pPager->journalHdr <= pPager->journalOff )((void) (0));
52202 pPager->journalOff += nHeader;
52203 }
52204
52205 return rc;
52206}
52207
52208/*
52209** The journal file must be open when this is called. A journal header file
52210** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
52211** file. The current location in the journal file is given by
52212** pPager->journalOff. See comments above function writeJournalHdr() for
52213** a description of the journal header format.
52214**
52215** If the header is read successfully, *pNRec is set to the number of
52216** page records following this header and *pDbSize is set to the size of the
52217** database before the transaction began, in pages. Also, pPager->cksumInit
52218** is set to the value read from the journal header. SQLITE_OK is returned
52219** in this case.
52220**
52221** If the journal header file appears to be corrupted, SQLITE_DONE is
52222** returned and *pNRec and *PDbSize are undefined. If JOURNAL_HDR_SZ bytes
52223** cannot be read from the journal file an error code is returned.
52224*/
52225static int readJournalHdr(
52226 Pager *pPager, /* Pager object */
52227 int isHot,
52228 i64 journalSize, /* Size of the open journal file in bytes */
52229 u32 *pNRec, /* OUT: Value read from the nRec field */
52230 u32 *pDbSize /* OUT: Value of original database size field */
52231){
52232 int rc; /* Return code */
52233 unsigned char aMagic[8]; /* A buffer to hold the magic header */
52234 i64 iHdrOff; /* Offset of journal header being read */
52235
52236 assert( isOpen(pPager->jfd) )((void) (0)); /* Journal file must be open. */
52237
52238 /* Advance Pager.journalOff to the start of the next sector. If the
52239 ** journal file is too small for there to be a header stored at this
52240 ** point, return SQLITE_DONE.
52241 */
52242 pPager->journalOff = journalHdrOffset(pPager);
52243 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) > journalSize ){
52244 return SQLITE_DONE101;
52245 }
52246 iHdrOff = pPager->journalOff;
52247
52248 /* Read in the first 8 bytes of the journal header. If they do not match
52249 ** the magic string found at the start of each journal header, return
52250 ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
52251 ** proceed.
52252 */
52253 if( isHot || iHdrOff!=pPager->journalHdr ){
52254 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
52255 if( rc ){
52256 return rc;
52257 }
52258 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
52259 return SQLITE_DONE101;
52260 }
52261 }
52262
52263 /* Read the first three 32-bit fields of the journal header: The nRec
52264 ** field, the checksum-initializer and the database size at the start
52265 ** of the transaction. Return an error code if anything goes wrong.
52266 */
52267 if( SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
52268 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
52269 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
52270 ){
52271 return rc;
52272 }
52273
52274 if( pPager->journalOff==0 ){
52275 u32 iPageSize; /* Page-size field of journal header */
52276 u32 iSectorSize; /* Sector-size field of journal header */
52277
52278 /* Read the page-size and sector-size journal header fields. */
52279 if( SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
52280 || SQLITE_OK0!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
52281 ){
52282 return rc;
52283 }
52284
52285 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
52286 ** journal header to zero. In this case, assume that the Pager.pageSize
52287 ** variable is already set to the correct page size.
52288 */
52289 if( iPageSize==0 ){
52290 iPageSize = pPager->pageSize;
52291 }
52292
52293 /* Check that the values read from the page-size and sector-size fields
52294 ** are within range. To be 'in range', both values need to be a power
52295 ** of two greater than or equal to 512 or 32, and not greater than their
52296 ** respective compile time maximum limits.
52297 */
52298 if( iPageSize<512 || iSectorSize<32
52299 || iPageSize>SQLITE_MAX_PAGE_SIZE65536 || iSectorSize>MAX_SECTOR_SIZE0x10000
52300 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
52301 ){
52302 /* If the either the page-size or sector-size in the journal-header is
52303 ** invalid, then the process that wrote the journal-header must have
52304 ** crashed before the header was synced. In this case stop reading
52305 ** the journal file here.
52306 */
52307 return SQLITE_DONE101;
52308 }
52309
52310 /* Update the page-size to match the value read from the journal.
52311 ** Use a testcase() macro to make sure that malloc failure within
52312 ** PagerSetPagesize() is tested.
52313 */
52314 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
52315 testcase( rc!=SQLITE_OK );
52316
52317 /* Update the assumed sector-size to match the value used by
52318 ** the process that created this journal. If this journal was
52319 ** created by a process other than this one, then this routine
52320 ** is being called from within pager_playback(). The local value
52321 ** of Pager.sectorSize is restored at the end of that routine.
52322 */
52323 pPager->sectorSize = iSectorSize;
52324 }
52325
52326 pPager->journalOff += JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
52327 return rc;
52328}
52329
52330
52331/*
52332** Write the supplied master journal name into the journal file for pager
52333** pPager at the current location. The master journal name must be the last
52334** thing written to a journal file. If the pager is in full-sync mode, the
52335** journal file descriptor is advanced to the next sector boundary before
52336** anything is written. The format is:
52337**
52338** + 4 bytes: PAGER_MJ_PGNO.
52339** + N bytes: Master journal filename in utf-8.
52340** + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
52341** + 4 bytes: Master journal name checksum.
52342** + 8 bytes: aJournalMagic[].
52343**
52344** The master journal page checksum is the sum of the bytes in the master
52345** journal name, where each byte is interpreted as a signed 8-bit integer.
52346**
52347** If zMaster is a NULL pointer (occurs for a single database transaction),
52348** this call is a no-op.
52349*/
52350static int writeMasterJournal(Pager *pPager, const char *zMaster){
52351 int rc; /* Return code */
52352 int nMaster; /* Length of string zMaster */
52353 i64 iHdrOff; /* Offset of header in journal file */
52354 i64 jrnlSize; /* Size of journal file on disk */
52355 u32 cksum = 0; /* Checksum of string zMaster */
52356
52357 assert( pPager->setMaster==0 )((void) (0));
52358 assert( !pagerUseWal(pPager) )((void) (0));
52359
52360 if( !zMaster
52361 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY4
52362 || !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
52363 ){
52364 return SQLITE_OK0;
52365 }
52366 pPager->setMaster = 1;
52367 assert( pPager->journalHdr <= pPager->journalOff )((void) (0));
52368
52369 /* Calculate the length in bytes and the checksum of zMaster */
52370 for(nMaster=0; zMaster[nMaster]; nMaster++){
52371 cksum += zMaster[nMaster];
52372 }
52373
52374 /* If in full-sync mode, advance to the next disk sector before writing
52375 ** the master journal name. This is in case the previous page written to
52376 ** the journal has already been synced.
52377 */
52378 if( pPager->fullSync ){
52379 pPager->journalOff = journalHdrOffset(pPager);
52380 }
52381 iHdrOff = pPager->journalOff;
52382
52383 /* Write the master journal data to the end of the journal file. If
52384 ** an error occurs, return the error code to the caller.
52385 */
52386 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager)((Pgno)((sqlite3PendingByte/((pPager)->pageSize))+1)))))
52387 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
52388 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
52389 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
52390 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
52391 iHdrOff+4+nMaster+8)))
52392 ){
52393 return rc;
52394 }
52395 pPager->journalOff += (nMaster+20);
52396
52397 /* If the pager is in peristent-journal mode, then the physical
52398 ** journal-file may extend past the end of the master-journal name
52399 ** and 8 bytes of magic data just written to the file. This is
52400 ** dangerous because the code to rollback a hot-journal file
52401 ** will not be able to find the master-journal name to determine
52402 ** whether or not the journal is hot.
52403 **
52404 ** Easiest thing to do in this scenario is to truncate the journal
52405 ** file to the required size.
52406 */
52407 if( SQLITE_OK0==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
52408 && jrnlSize>pPager->journalOff
52409 ){
52410 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
52411 }
52412 return rc;
52413}
52414
52415/*
52416** Discard the entire contents of the in-memory page-cache.
52417*/
52418static void pager_reset(Pager *pPager){
52419 pPager->iDataVersion++;
52420 sqlite3BackupRestart(pPager->pBackup);
52421 sqlite3PcacheClear(pPager->pPCache);
52422}
52423
52424/*
52425** Return the pPager->iDataVersion value
52426*/
52427SQLITE_PRIVATEstatic u32 sqlite3PagerDataVersion(Pager *pPager){
52428 return pPager->iDataVersion;
52429}
52430
52431/*
52432** Free all structures in the Pager.aSavepoint[] array and set both
52433** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
52434** if it is open and the pager is not in exclusive mode.
52435*/
52436static void releaseAllSavepoints(Pager *pPager){
52437 int ii; /* Iterator for looping through Pager.aSavepoint */
52438 for(ii=0; ii<pPager->nSavepoint; ii++){
52439 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
52440 }
52441 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
52442 sqlite3OsClose(pPager->sjfd);
52443 }
52444 sqlite3_free(pPager->aSavepoint);
52445 pPager->aSavepoint = 0;
52446 pPager->nSavepoint = 0;
52447 pPager->nSubRec = 0;
52448}
52449
52450/*
52451** Set the bit number pgno in the PagerSavepoint.pInSavepoint
52452** bitvecs of all open savepoints. Return SQLITE_OK if successful
52453** or SQLITE_NOMEM if a malloc failure occurs.
52454*/
52455static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
52456 int ii; /* Loop counter */
52457 int rc = SQLITE_OK0; /* Result code */
52458
52459 for(ii=0; ii<pPager->nSavepoint; ii++){
52460 PagerSavepoint *p = &pPager->aSavepoint[ii];
52461 if( pgno<=p->nOrig ){
52462 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
52463 testcase( rc==SQLITE_NOMEM );
52464 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
52465 }
52466 }
52467 return rc;
52468}
52469
52470/*
52471** This function is a no-op if the pager is in exclusive mode and not
52472** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
52473** state.
52474**
52475** If the pager is not in exclusive-access mode, the database file is
52476** completely unlocked. If the file is unlocked and the file-system does
52477** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
52478** closed (if it is open).
52479**
52480** If the pager is in ERROR state when this function is called, the
52481** contents of the pager cache are discarded before switching back to
52482** the OPEN state. Regardless of whether the pager is in exclusive-mode
52483** or not, any journal file left in the file-system will be treated
52484** as a hot-journal and rolled back the next time a read-transaction
52485** is opened (by this or by any other connection).
52486*/
52487static void pager_unlock(Pager *pPager){
52488
52489 assert( pPager->eState==PAGER_READER((void) (0))
52490 || pPager->eState==PAGER_OPEN((void) (0))
52491 || pPager->eState==PAGER_ERROR((void) (0))
52492 )((void) (0));
52493
52494 sqlite3BitvecDestroy(pPager->pInJournal);
52495 pPager->pInJournal = 0;
52496 releaseAllSavepoints(pPager);
52497
52498 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
52499 assert( !isOpen(pPager->jfd) )((void) (0));
52500 sqlite3WalEndReadTransaction(pPager->pWal);
52501 pPager->eState = PAGER_OPEN0;
52502 }else if( !pPager->exclusiveMode ){
52503 int rc; /* Error code returned by pagerUnlockDb() */
52504 int iDc = isOpen(pPager->fd)((pPager->fd)->pMethods!=0)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
52505
52506 /* If the operating system support deletion of open files, then
52507 ** close the journal file when dropping the database lock. Otherwise
52508 ** another connection with journal_mode=delete might delete the file
52509 ** out from under us.
52510 */
52511 assert( (PAGER_JOURNALMODE_MEMORY & 5)!=1 )((void) (0));
52512 assert( (PAGER_JOURNALMODE_OFF & 5)!=1 )((void) (0));
52513 assert( (PAGER_JOURNALMODE_WAL & 5)!=1 )((void) (0));
52514 assert( (PAGER_JOURNALMODE_DELETE & 5)!=1 )((void) (0));
52515 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 )((void) (0));
52516 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 )((void) (0));
52517 if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN0x00000800)
52518 || 1!=(pPager->journalMode & 5)
52519 ){
52520 sqlite3OsClose(pPager->jfd);
52521 }
52522
52523 /* If the pager is in the ERROR state and the call to unlock the database
52524 ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
52525 ** above the #define for UNKNOWN_LOCK for an explanation of why this
52526 ** is necessary.
52527 */
52528 rc = pagerUnlockDb(pPager, NO_LOCK0);
52529 if( rc!=SQLITE_OK0 && pPager->eState==PAGER_ERROR6 ){
52530 pPager->eLock = UNKNOWN_LOCK(4 +1);
52531 }
52532
52533 /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
52534 ** without clearing the error code. This is intentional - the error
52535 ** code is cleared and the cache reset in the block below.
52536 */
52537 assert( pPager->errCode || pPager->eState!=PAGER_ERROR )((void) (0));
52538 pPager->changeCountDone = 0;
52539 pPager->eState = PAGER_OPEN0;
52540 }
52541
52542 /* If Pager.errCode is set, the contents of the pager cache cannot be
52543 ** trusted. Now that there are no outstanding references to the pager,
52544 ** it can safely move back to PAGER_OPEN state. This happens in both
52545 ** normal and exclusive-locking mode.
52546 */
52547 assert( pPager->errCode==SQLITE_OK || !MEMDB )((void) (0));
52548 if( pPager->errCode ){
52549 if( pPager->tempFile==0 ){
52550 pager_reset(pPager);
52551 pPager->changeCountDone = 0;
52552 pPager->eState = PAGER_OPEN0;
52553 }else{
52554 pPager->eState = (isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ? PAGER_OPEN0 : PAGER_READER1);
52555 }
52556 if( USEFETCH(pPager)((pPager)->bUseFetch) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
52557 pPager->errCode = SQLITE_OK0;
52558 setGetterMethod(pPager);
52559 }
52560
52561 pPager->journalOff = 0;
52562 pPager->journalHdr = 0;
52563 pPager->setMaster = 0;
52564}
52565
52566/*
52567** This function is called whenever an IOERR or FULL error that requires
52568** the pager to transition into the ERROR state may ahve occurred.
52569** The first argument is a pointer to the pager structure, the second
52570** the error-code about to be returned by a pager API function. The
52571** value returned is a copy of the second argument to this function.
52572**
52573** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
52574** IOERR sub-codes, the pager enters the ERROR state and the error code
52575** is stored in Pager.errCode. While the pager remains in the ERROR state,
52576** all major API calls on the Pager will immediately return Pager.errCode.
52577**
52578** The ERROR state indicates that the contents of the pager-cache
52579** cannot be trusted. This state can be cleared by completely discarding
52580** the contents of the pager-cache. If a transaction was active when
52581** the persistent error occurred, then the rollback journal may need
52582** to be replayed to restore the contents of the database file (as if
52583** it were a hot-journal).
52584*/
52585static int pager_error(Pager *pPager, int rc){
52586 int rc2 = rc & 0xff;
52587 assert( rc==SQLITE_OK || !MEMDB )((void) (0));
52588 assert(((void) (0))
52589 pPager->errCode==SQLITE_FULL ||((void) (0))
52590 pPager->errCode==SQLITE_OK ||((void) (0))
52591 (pPager->errCode & 0xff)==SQLITE_IOERR((void) (0))
52592 )((void) (0));
52593 if( rc2==SQLITE_FULL13 || rc2==SQLITE_IOERR10 ){
52594 pPager->errCode = rc;
52595 pPager->eState = PAGER_ERROR6;
52596 setGetterMethod(pPager);
52597 }
52598 return rc;
52599}
52600
52601static int pager_truncate(Pager *pPager, Pgno nPage);
52602
52603/*
52604** The write transaction open on pPager is being committed (bCommit==1)
52605** or rolled back (bCommit==0).
52606**
52607** Return TRUE if and only if all dirty pages should be flushed to disk.
52608**
52609** Rules:
52610**
52611** * For non-TEMP databases, always sync to disk. This is necessary
52612** for transactions to be durable.
52613**
52614** * Sync TEMP database only on a COMMIT (not a ROLLBACK) when the backing
52615** file has been created already (via a spill on pagerStress()) and
52616** when the number of dirty pages in memory exceeds 25% of the total
52617** cache size.
52618*/
52619static int pagerFlushOnCommit(Pager *pPager, int bCommit){
52620 if( pPager->tempFile==0 ) return 1;
52621 if( !bCommit ) return 0;
52622 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ) return 0;
52623 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
52624}
52625
52626/*
52627** This routine ends a transaction. A transaction is usually ended by
52628** either a COMMIT or a ROLLBACK operation. This routine may be called
52629** after rollback of a hot-journal, or if an error occurs while opening
52630** the journal file or writing the very first journal-header of a
52631** database transaction.
52632**
52633** This routine is never called in PAGER_ERROR state. If it is called
52634** in PAGER_NONE or PAGER_SHARED state and the lock held is less
52635** exclusive than a RESERVED lock, it is a no-op.
52636**
52637** Otherwise, any active savepoints are released.
52638**
52639** If the journal file is open, then it is "finalized". Once a journal
52640** file has been finalized it is not possible to use it to roll back a
52641** transaction. Nor will it be considered to be a hot-journal by this
52642** or any other database connection. Exactly how a journal is finalized
52643** depends on whether or not the pager is running in exclusive mode and
52644** the current journal-mode (Pager.journalMode value), as follows:
52645**
52646** journalMode==MEMORY
52647** Journal file descriptor is simply closed. This destroys an
52648** in-memory journal.
52649**
52650** journalMode==TRUNCATE
52651** Journal file is truncated to zero bytes in size.
52652**
52653** journalMode==PERSIST
52654** The first 28 bytes of the journal file are zeroed. This invalidates
52655** the first journal header in the file, and hence the entire journal
52656** file. An invalid journal file cannot be rolled back.
52657**
52658** journalMode==DELETE
52659** The journal file is closed and deleted using sqlite3OsDelete().
52660**
52661** If the pager is running in exclusive mode, this method of finalizing
52662** the journal file is never used. Instead, if the journalMode is
52663** DELETE and the pager is in exclusive mode, the method described under
52664** journalMode==PERSIST is used instead.
52665**
52666** After the journal is finalized, the pager moves to PAGER_READER state.
52667** If running in non-exclusive rollback mode, the lock on the file is
52668** downgraded to a SHARED_LOCK.
52669**
52670** SQLITE_OK is returned if no error occurs. If an error occurs during
52671** any of the IO operations to finalize the journal file or unlock the
52672** database then the IO error code is returned to the user. If the
52673** operation to finalize the journal file fails, then the code still
52674** tries to unlock the database file if not in exclusive mode. If the
52675** unlock operation fails as well, then the first error code related
52676** to the first error encountered (the journal finalization one) is
52677** returned.
52678*/
52679static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
52680 int rc = SQLITE_OK0; /* Error code from journal finalization operation */
52681 int rc2 = SQLITE_OK0; /* Error code from db file unlock operation */
52682
52683 /* Do nothing if the pager does not have an open write transaction
52684 ** or at least a RESERVED lock. This function may be called when there
52685 ** is no write-transaction active but a RESERVED or greater lock is
52686 ** held under two circumstances:
52687 **
52688 ** 1. After a successful hot-journal rollback, it is called with
52689 ** eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
52690 **
52691 ** 2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
52692 ** lock switches back to locking_mode=normal and then executes a
52693 ** read-transaction, this function is called with eState==PAGER_READER
52694 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
52695 */
52696 assert( assert_pager_state(pPager) )((void) (0));
52697 assert( pPager->eState!=PAGER_ERROR )((void) (0));
52698 if( pPager->eState<PAGER_WRITER_LOCKED2 && pPager->eLock<RESERVED_LOCK2 ){
52699 return SQLITE_OK0;
52700 }
52701
52702 releaseAllSavepoints(pPager);
52703 assert( isOpen(pPager->jfd) || pPager->pInJournal==0((void) (0))
52704 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)((void) (0))
52705 )((void) (0));
52706 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
52707 assert( !pagerUseWal(pPager) )((void) (0));
52708
52709 /* Finalize the journal file. */
52710 if( sqlite3JournalIsInMemory(pPager->jfd) ){
52711 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
52712 sqlite3OsClose(pPager->jfd);
52713 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE3 ){
52714 if( pPager->journalOff==0 ){
52715 rc = SQLITE_OK0;
52716 }else{
52717 rc = sqlite3OsTruncate(pPager->jfd, 0);
52718 if( rc==SQLITE_OK0 && pPager->fullSync ){
52719 /* Make sure the new file size is written into the inode right away.
52720 ** Otherwise the journal might resurrect following a power loss and
52721 ** cause the last transaction to roll back. See
52722 ** https://bugzilla.mozilla.org/show_bug.cgi?id=1072773
52723 */
52724 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
52725 }
52726 }
52727 pPager->journalOff = 0;
52728 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST1
52729 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL5)
52730 ){
52731 rc = zeroJournalHdr(pPager, hasMaster||pPager->tempFile);
52732 pPager->journalOff = 0;
52733 }else{
52734 /* This branch may be executed with Pager.journalMode==MEMORY if
52735 ** a hot-journal was just rolled back. In this case the journal
52736 ** file should be closed and deleted. If this connection writes to
52737 ** the database file, it will do so using an in-memory journal.
52738 */
52739 int bDelete = !pPager->tempFile;
52740 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 )((void) (0));
52741 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE((void) (0))
52742 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY((void) (0))
52743 || pPager->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
52744 )((void) (0));
52745 sqlite3OsClose(pPager->jfd);
52746 if( bDelete ){
52747 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
52748 }
52749 }
52750 }
52751
52752#ifdef SQLITE_CHECK_PAGES
52753 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
52754 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
52755 PgHdr *p = sqlite3PagerLookup(pPager, 1);
52756 if( p ){
52757 p->pageHash = 0;
52758 sqlite3PagerUnrefNotNull(p);
52759 }
52760 }
52761#endif
52762
52763 sqlite3BitvecDestroy(pPager->pInJournal);
52764 pPager->pInJournal = 0;
52765 pPager->nRec = 0;
52766 if( rc==SQLITE_OK0 ){
52767 if( MEMDBpPager->memDb || pagerFlushOnCommit(pPager, bCommit) ){
52768 sqlite3PcacheCleanAll(pPager->pPCache);
52769 }else{
52770 sqlite3PcacheClearWritable(pPager->pPCache);
52771 }
52772 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
52773 }
52774
52775 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
52776 /* Drop the WAL write-lock, if any. Also, if the connection was in
52777 ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
52778 ** lock held on the database file.
52779 */
52780 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
52781 assert( rc2==SQLITE_OK )((void) (0));
52782 }else if( rc==SQLITE_OK0 && bCommit && pPager->dbFileSize>pPager->dbSize ){
52783 /* This branch is taken when committing a transaction in rollback-journal
52784 ** mode if the database file on disk is larger than the database image.
52785 ** At this point the journal has been finalized and the transaction
52786 ** successfully committed, but the EXCLUSIVE lock is still held on the
52787 ** file. So it is safe to truncate the database file to its minimum
52788 ** required size. */
52789 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
52790 rc = pager_truncate(pPager, pPager->dbSize);
52791 }
52792
52793 if( rc==SQLITE_OK0 && bCommit ){
52794 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO22, 0);
52795 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
52796 }
52797
52798 if( !pPager->exclusiveMode
52799 && (!pagerUseWal(pPager)((pPager)->pWal!=0) || sqlite3WalExclusiveMode(pPager->pWal, 0))
52800 ){
52801 rc2 = pagerUnlockDb(pPager, SHARED_LOCK1);
52802 pPager->changeCountDone = 0;
52803 }
52804 pPager->eState = PAGER_READER1;
52805 pPager->setMaster = 0;
52806
52807 return (rc==SQLITE_OK0?rc2:rc);
52808}
52809
52810/*
52811** Execute a rollback if a transaction is active and unlock the
52812** database file.
52813**
52814** If the pager has already entered the ERROR state, do not attempt
52815** the rollback at this time. Instead, pager_unlock() is called. The
52816** call to pager_unlock() will discard all in-memory pages, unlock
52817** the database file and move the pager back to OPEN state. If this
52818** means that there is a hot-journal left in the file-system, the next
52819** connection to obtain a shared lock on the pager (which may be this one)
52820** will roll it back.
52821**
52822** If the pager has not already entered the ERROR state, but an IO or
52823** malloc error occurs during a rollback, then this will itself cause
52824** the pager to enter the ERROR state. Which will be cleared by the
52825** call to pager_unlock(), as described above.
52826*/
52827static void pagerUnlockAndRollback(Pager *pPager){
52828 if( pPager->eState!=PAGER_ERROR6 && pPager->eState!=PAGER_OPEN0 ){
52829 assert( assert_pager_state(pPager) )((void) (0));
52830 if( pPager->eState>=PAGER_WRITER_LOCKED2 ){
52831 sqlite3BeginBenignMalloc();
52832 sqlite3PagerRollback(pPager);
52833 sqlite3EndBenignMalloc();
52834 }else if( !pPager->exclusiveMode ){
52835 assert( pPager->eState==PAGER_READER )((void) (0));
52836 pager_end_transaction(pPager, 0, 0);
52837 }
52838 }
52839 pager_unlock(pPager);
52840}
52841
52842/*
52843** Parameter aData must point to a buffer of pPager->pageSize bytes
52844** of data. Compute and return a checksum based ont the contents of the
52845** page of data and the current value of pPager->cksumInit.
52846**
52847** This is not a real checksum. It is really just the sum of the
52848** random initial value (pPager->cksumInit) and every 200th byte
52849** of the page data, starting with byte offset (pPager->pageSize%200).
52850** Each byte is interpreted as an 8-bit unsigned integer.
52851**
52852** Changing the formula used to compute this checksum results in an
52853** incompatible journal file format.
52854**
52855** If journal corruption occurs due to a power failure, the most likely
52856** scenario is that one end or the other of the record will be changed.
52857** It is much less likely that the two ends of the journal record will be
52858** correct and the middle be corrupt. Thus, this "checksum" scheme,
52859** though fast and simple, catches the mostly likely kind of corruption.
52860*/
52861static u32 pager_cksum(Pager *pPager, const u8 *aData){
52862 u32 cksum = pPager->cksumInit; /* Checksum value to return */
52863 int i = pPager->pageSize-200; /* Loop counter */
52864 while( i>0 ){
52865 cksum += aData[i];
52866 i -= 200;
52867 }
52868 return cksum;
52869}
52870
52871/*
52872** Report the current page size and number of reserved bytes back
52873** to the codec.
52874*/
52875#ifdef SQLITE_HAS_CODEC
52876static void pagerReportSize(Pager *pPager){
52877 if( pPager->xCodecSizeChng ){
52878 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
52879 (int)pPager->nReserve);
52880 }
52881}
52882#else
52883# define pagerReportSize(X) /* No-op if we do not support a codec */
52884#endif
52885
52886#ifdef SQLITE_HAS_CODEC
52887/*
52888** Make sure the number of reserved bits is the same in the destination
52889** pager as it is in the source. This comes up when a VACUUM changes the
52890** number of reserved bits to the "optimal" amount.
52891*/
52892SQLITE_PRIVATEstatic void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){
52893 if( pDest->nReserve!=pSrc->nReserve ){
52894 pDest->nReserve = pSrc->nReserve;
52895 pagerReportSize(pDest);
52896 }
52897}
52898#endif
52899
52900/*
52901** Read a single page from either the journal file (if isMainJrnl==1) or
52902** from the sub-journal (if isMainJrnl==0) and playback that page.
52903** The page begins at offset *pOffset into the file. The *pOffset
52904** value is increased to the start of the next page in the journal.
52905**
52906** The main rollback journal uses checksums - the statement journal does
52907** not.
52908**
52909** If the page number of the page record read from the (sub-)journal file
52910** is greater than the current value of Pager.dbSize, then playback is
52911** skipped and SQLITE_OK is returned.
52912**
52913** If pDone is not NULL, then it is a record of pages that have already
52914** been played back. If the page at *pOffset has already been played back
52915** (if the corresponding pDone bit is set) then skip the playback.
52916** Make sure the pDone bit corresponding to the *pOffset page is set
52917** prior to returning.
52918**
52919** If the page record is successfully read from the (sub-)journal file
52920** and played back, then SQLITE_OK is returned. If an IO error occurs
52921** while reading the record from the (sub-)journal file or while writing
52922** to the database file, then the IO error code is returned. If data
52923** is successfully read from the (sub-)journal file but appears to be
52924** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
52925** two circumstances:
52926**
52927** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
52928** * If the record is being rolled back from the main journal file
52929** and the checksum field does not match the record content.
52930**
52931** Neither of these two scenarios are possible during a savepoint rollback.
52932**
52933** If this is a savepoint rollback, then memory may have to be dynamically
52934** allocated by this function. If this is the case and an allocation fails,
52935** SQLITE_NOMEM is returned.
52936*/
52937static int pager_playback_one_page(
52938 Pager *pPager, /* The pager being played back */
52939 i64 *pOffset, /* Offset of record to playback */
52940 Bitvec *pDone, /* Bitvec of pages already played back */
52941 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
52942 int isSavepnt /* True for a savepoint rollback */
52943){
52944 int rc;
52945 PgHdr *pPg; /* An existing page in the cache */
52946 Pgno pgno; /* The page number of a page in journal */
52947 u32 cksum; /* Checksum used for sanity checking */
52948 char *aData; /* Temporary storage for the page */
52949 sqlite3_file *jfd; /* The file descriptor for the journal file */
52950 int isSynced; /* True if journal page is synced */
52951#ifdef SQLITE_HAS_CODEC
52952 /* The jrnlEnc flag is true if Journal pages should be passed through
52953 ** the codec. It is false for pure in-memory journals. */
52954 const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0);
52955#endif
52956
52957 assert( (isMainJrnl&~1)==0 )((void) (0)); /* isMainJrnl is 0 or 1 */
52958 assert( (isSavepnt&~1)==0 )((void) (0)); /* isSavepnt is 0 or 1 */
52959 assert( isMainJrnl || pDone )((void) (0)); /* pDone always used on sub-journals */
52960 assert( isSavepnt || pDone==0 )((void) (0)); /* pDone never used on non-savepoint */
52961
52962 aData = pPager->pTmpSpace;
52963 assert( aData )((void) (0)); /* Temp storage must have already been allocated */
52964 assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) )((void) (0));
52965
52966 /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
52967 ** or savepoint rollback done at the request of the caller) or this is
52968 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
52969 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
52970 ** only reads from the main journal, not the sub-journal.
52971 */
52972 assert( pPager->eState>=PAGER_WRITER_CACHEMOD((void) (0))
52973 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)((void) (0))
52974 )((void) (0));
52975 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl )((void) (0));
52976
52977 /* Read the page number and page data from the journal or sub-journal
52978 ** file. Return an error code to the caller if an IO error occurs.
52979 */
52980 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
52981 rc = read32bits(jfd, *pOffset, &pgno);
52982 if( rc!=SQLITE_OK0 ) return rc;
52983 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
52984 if( rc!=SQLITE_OK0 ) return rc;
52985 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
52986
52987 /* Sanity checking on the page. This is more important that I originally
52988 ** thought. If a power failure occurs while the journal is being written,
52989 ** it could cause invalid data to be written into the journal. We need to
52990 ** detect this invalid data (with high probability) and ignore it.
52991 */
52992 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager)((Pgno)((sqlite3PendingByte/((pPager)->pageSize))+1)) ){
52993 assert( !isSavepnt )((void) (0));
52994 return SQLITE_DONE101;
52995 }
52996 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
52997 return SQLITE_OK0;
52998 }
52999 if( isMainJrnl ){
53000 rc = read32bits(jfd, (*pOffset)-4, &cksum);
53001 if( rc ) return rc;
53002 if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
53003 return SQLITE_DONE101;
53004 }
53005 }
53006
53007 /* If this page has already been played back before during the current
53008 ** rollback, then don't bother to play it back again.
53009 */
53010 if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK0 ){
53011 return rc;
53012 }
53013
53014 /* When playing back page 1, restore the nReserve setting
53015 */
53016 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
53017 pPager->nReserve = ((u8*)aData)[20];
53018 pagerReportSize(pPager);
53019 }
53020
53021 /* If the pager is in CACHEMOD state, then there must be a copy of this
53022 ** page in the pager cache. In this case just update the pager cache,
53023 ** not the database file. The page is left marked dirty in this case.
53024 **
53025 ** An exception to the above rule: If the database is in no-sync mode
53026 ** and a page is moved during an incremental vacuum then the page may
53027 ** not be in the pager cache. Later: if a malloc() or IO error occurs
53028 ** during a Movepage() call, then the page may not be in the cache
53029 ** either. So the condition described in the above paragraph is not
53030 ** assert()able.
53031 **
53032 ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
53033 ** pager cache if it exists and the main file. The page is then marked
53034 ** not dirty. Since this code is only executed in PAGER_OPEN state for
53035 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
53036 ** if the pager is in OPEN state.
53037 **
53038 ** Ticket #1171: The statement journal might contain page content that is
53039 ** different from the page content at the start of the transaction.
53040 ** This occurs when a page is changed prior to the start of a statement
53041 ** then changed again within the statement. When rolling back such a
53042 ** statement we must not write to the original database unless we know
53043 ** for certain that original page contents are synced into the main rollback
53044 ** journal. Otherwise, a power loss might leave modified data in the
53045 ** database file without an entry in the rollback journal that can
53046 ** restore the database to its original form. Two conditions must be
53047 ** met before writing to the database files. (1) the database must be
53048 ** locked. (2) we know that the original page content is fully synced
53049 ** in the main journal either because the page is not in cache or else
53050 ** the page is marked as needSync==0.
53051 **
53052 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
53053 ** is possible to fail a statement on a database that does not yet exist.
53054 ** Do not attempt to write if database file has never been opened.
53055 */
53056 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
53057 pPg = 0;
53058 }else{
53059 pPg = sqlite3PagerLookup(pPager, pgno);
53060 }
53061 assert( pPg || !MEMDB )((void) (0));
53062 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile )((void) (0));
53063 PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
53064 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
53065 (isMainJrnl?"main-journal":"sub-journal")
53066 ));
53067 if( isMainJrnl ){
53068 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
53069 }else{
53070 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC0x008));
53071 }
53072 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0)
53073 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
53074 && isSynced
53075 ){
53076 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
53077 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
53078 assert( !pagerUseWal(pPager) )((void) (0));
53079
53080 /* Write the data read from the journal back into the database file.
53081 ** This is usually safe even for an encrypted database - as the data
53082 ** was encrypted before it was written to the journal file. The exception
53083 ** is if the data was just read from an in-memory sub-journal. In that
53084 ** case it must be encrypted here before it is copied into the database
53085 ** file. */
53086#ifdef SQLITE_HAS_CODEC
53087 if( !jrnlEnc ){
53088 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData)aData=(char*)aData;
53089 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
53090 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
53091 }else
53092#endif
53093 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
53094
53095 if( pgno>pPager->dbFileSize ){
53096 pPager->dbFileSize = pgno;
53097 }
53098 if( pPager->pBackup ){
53099#ifdef SQLITE_HAS_CODEC
53100 if( jrnlEnc ){
53101 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT);
53102 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
53103 CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT,aData)aData=(char*)aData;
53104 }else
53105#endif
53106 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
53107 }
53108 }else if( !isMainJrnl && pPg==0 ){
53109 /* If this is a rollback of a savepoint and data was not written to
53110 ** the database and the page is not in-memory, there is a potential
53111 ** problem. When the page is next fetched by the b-tree layer, it
53112 ** will be read from the database file, which may or may not be
53113 ** current.
53114 **
53115 ** There are a couple of different ways this can happen. All are quite
53116 ** obscure. When running in synchronous mode, this can only happen
53117 ** if the page is on the free-list at the start of the transaction, then
53118 ** populated, then moved using sqlite3PagerMovepage().
53119 **
53120 ** The solution is to add an in-memory page to the cache containing
53121 ** the data just read from the sub-journal. Mark the page as dirty
53122 ** and if the pager requires a journal-sync, then mark the page as
53123 ** requiring a journal-sync before it is written.
53124 */
53125 assert( isSavepnt )((void) (0));
53126 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 )((void) (0));
53127 pPager->doNotSpill |= SPILLFLAG_ROLLBACK0x02;
53128 rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
53129 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 )((void) (0));
53130 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK0x02;
53131 if( rc!=SQLITE_OK0 ) return rc;
53132 sqlite3PcacheMakeDirty(pPg);
53133 }
53134 if( pPg ){
53135 /* No page should ever be explicitly rolled back that is in use, except
53136 ** for page 1 which is held in use in order to keep the lock on the
53137 ** database active. However such a page may be rolled back as a result
53138 ** of an internal error resulting in an automatic call to
53139 ** sqlite3PagerRollback().
53140 */
53141 void *pData;
53142 pData = pPg->pData;
53143 memcpy(pData, (u8*)aData, pPager->pageSize);
53144 pPager->xReiniter(pPg);
53145 /* It used to be that sqlite3PcacheMakeClean(pPg) was called here. But
53146 ** that call was dangerous and had no detectable benefit since the cache
53147 ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
53148 ** has been removed. */
53149 pager_set_pagehash(pPg);
53150
53151 /* If this was page 1, then restore the value of Pager.dbFileVers.
53152 ** Do this before any decoding. */
53153 if( pgno==1 ){
53154 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
53155 }
53156
53157 /* Decode the page just read from disk */
53158#if SQLITE_HAS_CODEC
53159 if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); }
53160#endif
53161 sqlite3PcacheRelease(pPg);
53162 }
53163 return rc;
53164}
53165
53166/*
53167** Parameter zMaster is the name of a master journal file. A single journal
53168** file that referred to the master journal file has just been rolled back.
53169** This routine checks if it is possible to delete the master journal file,
53170** and does so if it is.
53171**
53172** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
53173** available for use within this function.
53174**
53175** When a master journal file is created, it is populated with the names
53176** of all of its child journals, one after another, formatted as utf-8
53177** encoded text. The end of each child journal file is marked with a
53178** nul-terminator byte (0x00). i.e. the entire contents of a master journal
53179** file for a transaction involving two databases might be:
53180**
53181** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
53182**
53183** A master journal file may only be deleted once all of its child
53184** journals have been rolled back.
53185**
53186** This function reads the contents of the master-journal file into
53187** memory and loops through each of the child journal names. For
53188** each child journal, it checks if:
53189**
53190** * if the child journal exists, and if so
53191** * if the child journal contains a reference to master journal
53192** file zMaster
53193**
53194** If a child journal can be found that matches both of the criteria
53195** above, this function returns without doing anything. Otherwise, if
53196** no such child journal can be found, file zMaster is deleted from
53197** the file-system using sqlite3OsDelete().
53198**
53199** If an IO error within this function, an error code is returned. This
53200** function allocates memory by calling sqlite3Malloc(). If an allocation
53201** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
53202** occur, SQLITE_OK is returned.
53203**
53204** TODO: This function allocates a single block of memory to load
53205** the entire contents of the master journal file. This could be
53206** a couple of kilobytes or so - potentially larger than the page
53207** size.
53208*/
53209static int pager_delmaster(Pager *pPager, const char *zMaster){
53210 sqlite3_vfs *pVfs = pPager->pVfs;
53211 int rc; /* Return code */
53212 sqlite3_file *pMaster; /* Malloc'd master-journal file descriptor */
53213 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
53214 char *zMasterJournal = 0; /* Contents of master journal file */
53215 i64 nMasterJournal; /* Size of master journal file */
53216 char *zJournal; /* Pointer to one journal within MJ file */
53217 char *zMasterPtr; /* Space to hold MJ filename from a journal file */
53218 int nMasterPtr; /* Amount of space allocated to zMasterPtr[] */
53219
53220 /* Allocate space for both the pJournal and pMaster file descriptors.
53221 ** If successful, open the master journal file for reading.
53222 */
53223 pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
53224 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
53225 if( !pMaster ){
53226 rc = SQLITE_NOMEM_BKPT7;
53227 }else{
53228 const int flags = (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_MASTER_JOURNAL0x00004000);
53229 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
53230 }
53231 if( rc!=SQLITE_OK0 ) goto delmaster_out;
53232
53233 /* Load the entire master journal file into space obtained from
53234 ** sqlite3_malloc() and pointed to by zMasterJournal. Also obtain
53235 ** sufficient space (in zMasterPtr) to hold the names of master
53236 ** journal files extracted from regular rollback-journals.
53237 */
53238 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
53239 if( rc!=SQLITE_OK0 ) goto delmaster_out;
53240 nMasterPtr = pVfs->mxPathname+1;
53241 zMasterJournal = sqlite3Malloc(nMasterJournal + nMasterPtr + 1);
53242 if( !zMasterJournal ){
53243 rc = SQLITE_NOMEM_BKPT7;
53244 goto delmaster_out;
53245 }
53246 zMasterPtr = &zMasterJournal[nMasterJournal+1];
53247 rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
53248 if( rc!=SQLITE_OK0 ) goto delmaster_out;
53249 zMasterJournal[nMasterJournal] = 0;
53250
53251 zJournal = zMasterJournal;
53252 while( (zJournal-zMasterJournal)<nMasterJournal ){
53253 int exists;
53254 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS0, &exists);
53255 if( rc!=SQLITE_OK0 ){
53256 goto delmaster_out;
53257 }
53258 if( exists ){
53259 /* One of the journals pointed to by the master journal exists.
53260 ** Open it and check if it points at the master journal. If
53261 ** so, return without deleting the master journal file.
53262 */
53263 int c;
53264 int flags = (SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_MAIN_JOURNAL0x00000800);
53265 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
53266 if( rc!=SQLITE_OK0 ){
53267 goto delmaster_out;
53268 }
53269
53270 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
53271 sqlite3OsClose(pJournal);
53272 if( rc!=SQLITE_OK0 ){
53273 goto delmaster_out;
53274 }
53275
53276 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
53277 if( c ){
53278 /* We have a match. Do not delete the master journal file. */
53279 goto delmaster_out;
53280 }
53281 }
53282 zJournal += (sqlite3Strlen30(zJournal)+1);
53283 }
53284
53285 sqlite3OsClose(pMaster);
53286 rc = sqlite3OsDelete(pVfs, zMaster, 0);
53287
53288delmaster_out:
53289 sqlite3_free(zMasterJournal);
53290 if( pMaster ){
53291 sqlite3OsClose(pMaster);
53292 assert( !isOpen(pJournal) )((void) (0));
53293 sqlite3_free(pMaster);
53294 }
53295 return rc;
53296}
53297
53298
53299/*
53300** This function is used to change the actual size of the database
53301** file in the file-system. This only happens when committing a transaction,
53302** or rolling back a transaction (including rolling back a hot-journal).
53303**
53304** If the main database file is not open, or the pager is not in either
53305** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
53306** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
53307** If the file on disk is currently larger than nPage pages, then use the VFS
53308** xTruncate() method to truncate it.
53309**
53310** Or, it might be the case that the file on disk is smaller than
53311** nPage pages. Some operating system implementations can get confused if
53312** you try to truncate a file to some size that is larger than it
53313** currently is, so detect this case and write a single zero byte to
53314** the end of the new file instead.
53315**
53316** If successful, return SQLITE_OK. If an IO error occurs while modifying
53317** the database file, return the error code to the caller.
53318*/
53319static int pager_truncate(Pager *pPager, Pgno nPage){
53320 int rc = SQLITE_OK0;
53321 assert( pPager->eState!=PAGER_ERROR )((void) (0));
53322 assert( pPager->eState!=PAGER_READER )((void) (0));
53323
53324 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0)
53325 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
53326 ){
53327 i64 currentSize, newSize;
53328 int szPage = pPager->pageSize;
53329 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
53330 /* TODO: Is it safe to use Pager.dbFileSize here? */
53331 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
53332 newSize = szPage*(i64)nPage;
53333 if( rc==SQLITE_OK0 && currentSize!=newSize ){
53334 if( currentSize>newSize ){
53335 rc = sqlite3OsTruncate(pPager->fd, newSize);
53336 }else if( (currentSize+szPage)<=newSize ){
53337 char *pTmp = pPager->pTmpSpace;
53338 memset(pTmp, 0, szPage);
53339 testcase( (newSize-szPage) == currentSize );
53340 testcase( (newSize-szPage) > currentSize );
53341 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
53342 }
53343 if( rc==SQLITE_OK0 ){
53344 pPager->dbFileSize = nPage;
53345 }
53346 }
53347 }
53348 return rc;
53349}
53350
53351/*
53352** Return a sanitized version of the sector-size of OS file pFile. The
53353** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
53354*/
53355SQLITE_PRIVATEstatic int sqlite3SectorSize(sqlite3_file *pFile){
53356 int iRet = sqlite3OsSectorSize(pFile);
53357 if( iRet<32 ){
53358 iRet = 512;
53359 }else if( iRet>MAX_SECTOR_SIZE0x10000 ){
53360 assert( MAX_SECTOR_SIZE>=512 )((void) (0));
53361 iRet = MAX_SECTOR_SIZE0x10000;
53362 }
53363 return iRet;
53364}
53365
53366/*
53367** Set the value of the Pager.sectorSize variable for the given
53368** pager based on the value returned by the xSectorSize method
53369** of the open database file. The sector size will be used
53370** to determine the size and alignment of journal header and
53371** master journal pointers within created journal files.
53372**
53373** For temporary files the effective sector size is always 512 bytes.
53374**
53375** Otherwise, for non-temporary files, the effective sector size is
53376** the value returned by the xSectorSize() method rounded up to 32 if
53377** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
53378** is greater than MAX_SECTOR_SIZE.
53379**
53380** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
53381** the effective sector size to its minimum value (512). The purpose of
53382** pPager->sectorSize is to define the "blast radius" of bytes that
53383** might change if a crash occurs while writing to a single byte in
53384** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
53385** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
53386** size. For backwards compatibility of the rollback journal file format,
53387** we cannot reduce the effective sector size below 512.
53388*/
53389static void setSectorSize(Pager *pPager){
53390 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
53391
53392 if( pPager->tempFile
53393 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
53394 SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000)!=0
53395 ){
53396 /* Sector size doesn't matter for temporary files. Also, the file
53397 ** may not have been opened yet, in which case the OsSectorSize()
53398 ** call will segfault. */
53399 pPager->sectorSize = 512;
53400 }else{
53401 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
53402 }
53403}
53404
53405/*
53406** Playback the journal and thus restore the database file to
53407** the state it was in before we started making changes.
53408**
53409** The journal file format is as follows:
53410**
53411** (1) 8 byte prefix. A copy of aJournalMagic[].
53412** (2) 4 byte big-endian integer which is the number of valid page records
53413** in the journal. If this value is 0xffffffff, then compute the
53414** number of page records from the journal size.
53415** (3) 4 byte big-endian integer which is the initial value for the
53416** sanity checksum.
53417** (4) 4 byte integer which is the number of pages to truncate the
53418** database to during a rollback.
53419** (5) 4 byte big-endian integer which is the sector size. The header
53420** is this many bytes in size.
53421** (6) 4 byte big-endian integer which is the page size.
53422** (7) zero padding out to the next sector size.
53423** (8) Zero or more pages instances, each as follows:
53424** + 4 byte page number.
53425** + pPager->pageSize bytes of data.
53426** + 4 byte checksum
53427**
53428** When we speak of the journal header, we mean the first 7 items above.
53429** Each entry in the journal is an instance of the 8th item.
53430**
53431** Call the value from the second bullet "nRec". nRec is the number of
53432** valid page entries in the journal. In most cases, you can compute the
53433** value of nRec from the size of the journal file. But if a power
53434** failure occurred while the journal was being written, it could be the
53435** case that the size of the journal file had already been increased but
53436** the extra entries had not yet made it safely to disk. In such a case,
53437** the value of nRec computed from the file size would be too large. For
53438** that reason, we always use the nRec value in the header.
53439**
53440** If the nRec value is 0xffffffff it means that nRec should be computed
53441** from the file size. This value is used when the user selects the
53442** no-sync option for the journal. A power failure could lead to corruption
53443** in this case. But for things like temporary table (which will be
53444** deleted when the power is restored) we don't care.
53445**
53446** If the file opened as the journal file is not a well-formed
53447** journal file then all pages up to the first corrupted page are rolled
53448** back (or no pages if the journal header is corrupted). The journal file
53449** is then deleted and SQLITE_OK returned, just as if no corruption had
53450** been encountered.
53451**
53452** If an I/O or malloc() error occurs, the journal-file is not deleted
53453** and an error code is returned.
53454**
53455** The isHot parameter indicates that we are trying to rollback a journal
53456** that might be a hot journal. Or, it could be that the journal is
53457** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
53458** If the journal really is hot, reset the pager cache prior rolling
53459** back any content. If the journal is merely persistent, no reset is
53460** needed.
53461*/
53462static int pager_playback(Pager *pPager, int isHot){
53463 sqlite3_vfs *pVfs = pPager->pVfs;
53464 i64 szJ; /* Size of the journal file in bytes */
53465 u32 nRec; /* Number of Records in the journal */
53466 u32 u; /* Unsigned loop counter */
53467 Pgno mxPg = 0; /* Size of the original file in pages */
53468 int rc; /* Result code of a subroutine */
53469 int res = 1; /* Value returned by sqlite3OsAccess() */
53470 char *zMaster = 0; /* Name of master journal file if any */
53471 int needPagerReset; /* True to reset page prior to first page rollback */
53472 int nPlayback = 0; /* Total number of pages restored from journal */
53473 u32 savedPageSize = pPager->pageSize;
53474
53475 /* Figure out how many records are in the journal. Abort early if
53476 ** the journal is empty.
53477 */
53478 assert( isOpen(pPager->jfd) )((void) (0));
53479 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
53480 if( rc!=SQLITE_OK0 ){
53481 goto end_playback;
53482 }
53483
53484 /* Read the master journal name from the journal, if it is present.
53485 ** If a master journal file name is specified, but the file is not
53486 ** present on disk, then the journal is not hot and does not need to be
53487 ** played back.
53488 **
53489 ** TODO: Technically the following is an error because it assumes that
53490 ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
53491 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
53492 ** mxPathname is 512, which is the same as the minimum allowable value
53493 ** for pageSize.
53494 */
53495 zMaster = pPager->pTmpSpace;
53496 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
53497 if( rc==SQLITE_OK0 && zMaster[0] ){
53498 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS0, &res);
53499 }
53500 zMaster = 0;
53501 if( rc!=SQLITE_OK0 || !res ){
53502 goto end_playback;
53503 }
53504 pPager->journalOff = 0;
53505 needPagerReset = isHot;
53506
53507 /* This loop terminates either when a readJournalHdr() or
53508 ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
53509 ** occurs.
53510 */
53511 while( 1 ){
53512 /* Read the next journal header from the journal file. If there are
53513 ** not enough bytes left in the journal file for a complete header, or
53514 ** it is corrupted, then a process must have failed while writing it.
53515 ** This indicates nothing more needs to be rolled back.
53516 */
53517 rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
53518 if( rc!=SQLITE_OK0 ){
53519 if( rc==SQLITE_DONE101 ){
53520 rc = SQLITE_OK0;
53521 }
53522 goto end_playback;
53523 }
53524
53525 /* If nRec is 0xffffffff, then this journal was created by a process
53526 ** working in no-sync mode. This means that the rest of the journal
53527 ** file consists of pages, there are no more journal headers. Compute
53528 ** the value of nRec based on this assumption.
53529 */
53530 if( nRec==0xffffffff ){
53531 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) )((void) (0));
53532 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager)(pPager->sectorSize))/JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
53533 }
53534
53535 /* If nRec is 0 and this rollback is of a transaction created by this
53536 ** process and if this is the final header in the journal, then it means
53537 ** that this part of the journal was being filled but has not yet been
53538 ** synced to disk. Compute the number of pages based on the remaining
53539 ** size of the file.
53540 **
53541 ** The third term of the test was added to fix ticket #2565.
53542 ** When rolling back a hot journal, nRec==0 always means that the next
53543 ** chunk of the journal contains zero pages to be rolled back. But
53544 ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
53545 ** the journal, it means that the journal might contain additional
53546 ** pages that need to be rolled back and that the number of pages
53547 ** should be computed based on the journal file size.
53548 */
53549 if( nRec==0 && !isHot &&
53550 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize)==pPager->journalOff ){
53551 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
53552 }
53553
53554 /* If this is the first header read from the journal, truncate the
53555 ** database file back to its original size.
53556 */
53557 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager)(pPager->sectorSize) ){
53558 rc = pager_truncate(pPager, mxPg);
53559 if( rc!=SQLITE_OK0 ){
53560 goto end_playback;
53561 }
53562 pPager->dbSize = mxPg;
53563 }
53564
53565 /* Copy original pages out of the journal and back into the
53566 ** database file and/or page cache.
53567 */
53568 for(u=0; u<nRec; u++){
53569 if( needPagerReset ){
53570 pager_reset(pPager);
53571 needPagerReset = 0;
53572 }
53573 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
53574 if( rc==SQLITE_OK0 ){
53575 nPlayback++;
53576 }else{
53577 if( rc==SQLITE_DONE101 ){
53578 pPager->journalOff = szJ;
53579 break;
53580 }else if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
53581 /* If the journal has been truncated, simply stop reading and
53582 ** processing the journal. This might happen if the journal was
53583 ** not completely written and synced prior to a crash. In that
53584 ** case, the database should have never been written in the
53585 ** first place so it is OK to simply abandon the rollback. */
53586 rc = SQLITE_OK0;
53587 goto end_playback;
53588 }else{
53589 /* If we are unable to rollback, quit and return the error
53590 ** code. This will cause the pager to enter the error state
53591 ** so that no further harm will be done. Perhaps the next
53592 ** process to come along will be able to rollback the database.
53593 */
53594 goto end_playback;
53595 }
53596 }
53597 }
53598 }
53599 /*NOTREACHED*/
53600 assert( 0 )((void) (0));
53601
53602end_playback:
53603 if( rc==SQLITE_OK0 ){
53604 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
53605 }
53606 /* Following a rollback, the database file should be back in its original
53607 ** state prior to the start of the transaction, so invoke the
53608 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
53609 ** assertion that the transaction counter was modified.
53610 */
53611#ifdef SQLITE_DEBUG
53612 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED0xca093fa0,0);
53613#endif
53614
53615 /* If this playback is happening automatically as a result of an IO or
53616 ** malloc error that occurred after the change-counter was updated but
53617 ** before the transaction was committed, then the change-counter
53618 ** modification may just have been reverted. If this happens in exclusive
53619 ** mode, then subsequent transactions performed by the connection will not
53620 ** update the change-counter at all. This may lead to cache inconsistency
53621 ** problems for other processes at some point in the future. So, just
53622 ** in case this has happened, clear the changeCountDone flag now.
53623 */
53624 pPager->changeCountDone = pPager->tempFile;
53625
53626 if( rc==SQLITE_OK0 ){
53627 zMaster = pPager->pTmpSpace;
53628 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
53629 testcase( rc!=SQLITE_OK );
53630 }
53631 if( rc==SQLITE_OK0
53632 && (pPager->eState>=PAGER_WRITER_DBMOD4 || pPager->eState==PAGER_OPEN0)
53633 ){
53634 rc = sqlite3PagerSync(pPager, 0);
53635 }
53636 if( rc==SQLITE_OK0 ){
53637 rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
53638 testcase( rc!=SQLITE_OK );
53639 }
53640 if( rc==SQLITE_OK0 && zMaster[0] && res ){
53641 /* If there was a master journal and this routine will return success,
53642 ** see if it is possible to delete the master journal.
53643 */
53644 rc = pager_delmaster(pPager, zMaster);
53645 testcase( rc!=SQLITE_OK );
53646 }
53647 if( isHot && nPlayback ){
53648 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)), "recovered %d pages from %s",
53649 nPlayback, pPager->zJournal);
53650 }
53651
53652 /* The Pager.sectorSize variable may have been updated while rolling
53653 ** back a journal created by a process with a different sector size
53654 ** value. Reset it to the correct value for this process.
53655 */
53656 setSectorSize(pPager);
53657 return rc;
53658}
53659
53660
53661/*
53662** Read the content for page pPg out of the database file (or out of
53663** the WAL if that is where the most recent copy if found) into
53664** pPg->pData. A shared lock or greater must be held on the database
53665** file before this function is called.
53666**
53667** If page 1 is read, then the value of Pager.dbFileVers[] is set to
53668** the value read from the database file.
53669**
53670** If an IO error occurs, then the IO error is returned to the caller.
53671** Otherwise, SQLITE_OK is returned.
53672*/
53673static int readDbPage(PgHdr *pPg){
53674 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
53675 int rc = SQLITE_OK0; /* Return code */
53676
53677#ifndef SQLITE_OMIT_WAL
53678 u32 iFrame = 0; /* Frame of WAL containing pgno */
53679
53680 assert( pPager->eState>=PAGER_READER && !MEMDB )((void) (0));
53681 assert( isOpen(pPager->fd) )((void) (0));
53682
53683 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
53684 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
53685 if( rc ) return rc;
53686 }
53687 if( iFrame ){
53688 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
53689 }else
53690#endif
53691 {
53692 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
53693 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
53694 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
53695 rc = SQLITE_OK0;
53696 }
53697 }
53698
53699 if( pPg->pgno==1 ){
53700 if( rc ){
53701 /* If the read is unsuccessful, set the dbFileVers[] to something
53702 ** that will never be a valid file version. dbFileVers[] is a copy
53703 ** of bytes 24..39 of the database. Bytes 28..31 should always be
53704 ** zero or the size of the database in page. Bytes 32..35 and 35..39
53705 ** should be page numbers which are never 0xffffffff. So filling
53706 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
53707 **
53708 ** For an encrypted database, the situation is more complex: bytes
53709 ** 24..39 of the database are white noise. But the probability of
53710 ** white noise equaling 16 bytes of 0xff is vanishingly small so
53711 ** we should still be ok.
53712 */
53713 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
53714 }else{
53715 u8 *dbFileVers = &((u8*)pPg->pData)[24];
53716 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
53717 }
53718 }
53719 CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT);
53720
53721 PAGER_INCR(sqlite3_pager_readdb_count);
53722 PAGER_INCR(pPager->nRead);
53723 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
53724 PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
53725 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
53726
53727 return rc;
53728}
53729
53730/*
53731** Update the value of the change-counter at offsets 24 and 92 in
53732** the header and the sqlite version number at offset 96.
53733**
53734** This is an unconditional update. See also the pager_incr_changecounter()
53735** routine which only updates the change-counter if the update is actually
53736** needed, as determined by the pPager->changeCountDone state variable.
53737*/
53738static void pager_write_changecounter(PgHdr *pPg){
53739 u32 change_counter;
53740
53741 /* Increment the value just read and write it back to byte 24. */
53742 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
53743 put32bits(((char*)pPg->pData)+24, change_counter)sqlite3Put4byte((u8*)((char*)pPg->pData)+24,change_counter
)
;
53744
53745 /* Also store the SQLite version number in bytes 96..99 and in
53746 ** bytes 92..95 store the change counter for which the version number
53747 ** is valid. */
53748 put32bits(((char*)pPg->pData)+92, change_counter)sqlite3Put4byte((u8*)((char*)pPg->pData)+92,change_counter
)
;
53749 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER)sqlite3Put4byte((u8*)((char*)pPg->pData)+96,3029000);
53750}
53751
53752#ifndef SQLITE_OMIT_WAL
53753/*
53754** This function is invoked once for each page that has already been
53755** written into the log file when a WAL transaction is rolled back.
53756** Parameter iPg is the page number of said page. The pCtx argument
53757** is actually a pointer to the Pager structure.
53758**
53759** If page iPg is present in the cache, and has no outstanding references,
53760** it is discarded. Otherwise, if there are one or more outstanding
53761** references, the page content is reloaded from the database. If the
53762** attempt to reload content from the database is required and fails,
53763** return an SQLite error code. Otherwise, SQLITE_OK.
53764*/
53765static int pagerUndoCallback(void *pCtx, Pgno iPg){
53766 int rc = SQLITE_OK0;
53767 Pager *pPager = (Pager *)pCtx;
53768 PgHdr *pPg;
53769
53770 assert( pagerUseWal(pPager) )((void) (0));
53771 pPg = sqlite3PagerLookup(pPager, iPg);
53772 if( pPg ){
53773 if( sqlite3PcachePageRefcount(pPg)==1 ){
53774 sqlite3PcacheDrop(pPg);
53775 }else{
53776 rc = readDbPage(pPg);
53777 if( rc==SQLITE_OK0 ){
53778 pPager->xReiniter(pPg);
53779 }
53780 sqlite3PagerUnrefNotNull(pPg);
53781 }
53782 }
53783
53784 /* Normally, if a transaction is rolled back, any backup processes are
53785 ** updated as data is copied out of the rollback journal and into the
53786 ** database. This is not generally possible with a WAL database, as
53787 ** rollback involves simply truncating the log file. Therefore, if one
53788 ** or more frames have already been written to the log (and therefore
53789 ** also copied into the backup databases) as part of this transaction,
53790 ** the backups must be restarted.
53791 */
53792 sqlite3BackupRestart(pPager->pBackup);
53793
53794 return rc;
53795}
53796
53797/*
53798** This function is called to rollback a transaction on a WAL database.
53799*/
53800static int pagerRollbackWal(Pager *pPager){
53801 int rc; /* Return Code */
53802 PgHdr *pList; /* List of dirty pages to revert */
53803
53804 /* For all pages in the cache that are currently dirty or have already
53805 ** been written (but not committed) to the log file, do one of the
53806 ** following:
53807 **
53808 ** + Discard the cached page (if refcount==0), or
53809 ** + Reload page content from the database (if refcount>0).
53810 */
53811 pPager->dbSize = pPager->dbOrigSize;
53812 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
53813 pList = sqlite3PcacheDirtyList(pPager->pPCache);
53814 while( pList && rc==SQLITE_OK0 ){
53815 PgHdr *pNext = pList->pDirty;
53816 rc = pagerUndoCallback((void *)pPager, pList->pgno);
53817 pList = pNext;
53818 }
53819
53820 return rc;
53821}
53822
53823/*
53824** This function is a wrapper around sqlite3WalFrames(). As well as logging
53825** the contents of the list of pages headed by pList (connected by pDirty),
53826** this function notifies any active backup processes that the pages have
53827** changed.
53828**
53829** The list of pages passed into this routine is always sorted by page number.
53830** Hence, if page 1 appears anywhere on the list, it will be the first page.
53831*/
53832static int pagerWalFrames(
53833 Pager *pPager, /* Pager object */
53834 PgHdr *pList, /* List of frames to log */
53835 Pgno nTruncate, /* Database size after this commit */
53836 int isCommit /* True if this is a commit */
53837){
53838 int rc; /* Return code */
53839 int nList; /* Number of pages in pList */
53840 PgHdr *p; /* For looping over pages */
53841
53842 assert( pPager->pWal )((void) (0));
53843 assert( pList )((void) (0));
53844#ifdef SQLITE_DEBUG
53845 /* Verify that the page list is in accending order */
53846 for(p=pList; p && p->pDirty; p=p->pDirty){
53847 assert( p->pgno < p->pDirty->pgno )((void) (0));
53848 }
53849#endif
53850
53851 assert( pList->pDirty==0 || isCommit )((void) (0));
53852 if( isCommit ){
53853 /* If a WAL transaction is being committed, there is no point in writing
53854 ** any pages with page numbers greater than nTruncate into the WAL file.
53855 ** They will never be read by any client. So remove them from the pDirty
53856 ** list here. */
53857 PgHdr **ppNext = &pList;
53858 nList = 0;
53859 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
53860 if( p->pgno<=nTruncate ){
53861 ppNext = &p->pDirty;
53862 nList++;
53863 }
53864 }
53865 assert( pList )((void) (0));
53866 }else{
53867 nList = 1;
53868 }
53869 pPager->aStat[PAGER_STAT_WRITE2] += nList;
53870
53871 if( pList->pgno==1 ) pager_write_changecounter(pList);
53872 rc = sqlite3WalFrames(pPager->pWal,
53873 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
53874 );
53875 if( rc==SQLITE_OK0 && pPager->pBackup ){
53876 for(p=pList; p; p=p->pDirty){
53877 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
53878 }
53879 }
53880
53881#ifdef SQLITE_CHECK_PAGES
53882 pList = sqlite3PcacheDirtyList(pPager->pPCache);
53883 for(p=pList; p; p=p->pDirty){
53884 pager_set_pagehash(p);
53885 }
53886#endif
53887
53888 return rc;
53889}
53890
53891/*
53892** Begin a read transaction on the WAL.
53893**
53894** This routine used to be called "pagerOpenSnapshot()" because it essentially
53895** makes a snapshot of the database at the current point in time and preserves
53896** that snapshot for use by the reader in spite of concurrently changes by
53897** other writers or checkpointers.
53898*/
53899static int pagerBeginReadTransaction(Pager *pPager){
53900 int rc; /* Return code */
53901 int changed = 0; /* True if cache must be reset */
53902
53903 assert( pagerUseWal(pPager) )((void) (0));
53904 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER )((void) (0));
53905
53906 /* sqlite3WalEndReadTransaction() was not called for the previous
53907 ** transaction in locking_mode=EXCLUSIVE. So call it now. If we
53908 ** are in locking_mode=NORMAL and EndRead() was previously called,
53909 ** the duplicate call is harmless.
53910 */
53911 sqlite3WalEndReadTransaction(pPager->pWal);
53912
53913 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
53914 if( rc!=SQLITE_OK0 || changed ){
53915 pager_reset(pPager);
53916 if( USEFETCH(pPager)((pPager)->bUseFetch) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
53917 }
53918
53919 return rc;
53920}
53921#endif
53922
53923/*
53924** This function is called as part of the transition from PAGER_OPEN
53925** to PAGER_READER state to determine the size of the database file
53926** in pages (assuming the page size currently stored in Pager.pageSize).
53927**
53928** If no error occurs, SQLITE_OK is returned and the size of the database
53929** in pages is stored in *pnPage. Otherwise, an error code (perhaps
53930** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
53931*/
53932static int pagerPagecount(Pager *pPager, Pgno *pnPage){
53933 Pgno nPage; /* Value to return via *pnPage */
53934
53935 /* Query the WAL sub-system for the database size. The WalDbsize()
53936 ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
53937 ** if the database size is not available. The database size is not
53938 ** available from the WAL sub-system if the log file is empty or
53939 ** contains no valid committed transactions.
53940 */
53941 assert( pPager->eState==PAGER_OPEN )((void) (0));
53942 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
53943 assert( isOpen(pPager->fd) )((void) (0));
53944 assert( pPager->tempFile==0 )((void) (0));
53945 nPage = sqlite3WalDbsize(pPager->pWal);
53946
53947 /* If the number of pages in the database is not available from the
53948 ** WAL sub-system, determine the page count based on the size of
53949 ** the database file. If the size of the database file is not an
53950 ** integer multiple of the page-size, round up the result.
53951 */
53952 if( nPage==0 && ALWAYS(isOpen(pPager->fd))(((pPager->fd)->pMethods!=0)) ){
53953 i64 n = 0; /* Size of db file in bytes */
53954 int rc = sqlite3OsFileSize(pPager->fd, &n);
53955 if( rc!=SQLITE_OK0 ){
53956 return rc;
53957 }
53958 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
53959 }
53960
53961 /* If the current number of pages in the file is greater than the
53962 ** configured maximum pager number, increase the allowed limit so
53963 ** that the file can be read.
53964 */
53965 if( nPage>pPager->mxPgno ){
53966 pPager->mxPgno = (Pgno)nPage;
53967 }
53968
53969 *pnPage = nPage;
53970 return SQLITE_OK0;
53971}
53972
53973#ifndef SQLITE_OMIT_WAL
53974/*
53975** Check if the *-wal file that corresponds to the database opened by pPager
53976** exists if the database is not empy, or verify that the *-wal file does
53977** not exist (by deleting it) if the database file is empty.
53978**
53979** If the database is not empty and the *-wal file exists, open the pager
53980** in WAL mode. If the database is empty or if no *-wal file exists and
53981** if no error occurs, make sure Pager.journalMode is not set to
53982** PAGER_JOURNALMODE_WAL.
53983**
53984** Return SQLITE_OK or an error code.
53985**
53986** The caller must hold a SHARED lock on the database file to call this
53987** function. Because an EXCLUSIVE lock on the db file is required to delete
53988** a WAL on a none-empty database, this ensures there is no race condition
53989** between the xAccess() below and an xDelete() being executed by some
53990** other connection.
53991*/
53992static int pagerOpenWalIfPresent(Pager *pPager){
53993 int rc = SQLITE_OK0;
53994 assert( pPager->eState==PAGER_OPEN )((void) (0));
53995 assert( pPager->eLock>=SHARED_LOCK )((void) (0));
53996
53997 if( !pPager->tempFile ){
53998 int isWal; /* True if WAL file exists */
53999 rc = sqlite3OsAccess(
54000 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS0, &isWal
54001 );
54002 if( rc==SQLITE_OK0 ){
54003 if( isWal ){
54004 Pgno nPage; /* Size of the database file */
54005
54006 rc = pagerPagecount(pPager, &nPage);
54007 if( rc ) return rc;
54008 if( nPage==0 ){
54009 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
54010 }else{
54011 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
54012 rc = sqlite3PagerOpenWal(pPager, 0);
54013 }
54014 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL5 ){
54015 pPager->journalMode = PAGER_JOURNALMODE_DELETE0;
54016 }
54017 }
54018 }
54019 return rc;
54020}
54021#endif
54022
54023/*
54024** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
54025** the entire master journal file. The case pSavepoint==NULL occurs when
54026** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
54027** savepoint.
54028**
54029** When pSavepoint is not NULL (meaning a non-transaction savepoint is
54030** being rolled back), then the rollback consists of up to three stages,
54031** performed in the order specified:
54032**
54033** * Pages are played back from the main journal starting at byte
54034** offset PagerSavepoint.iOffset and continuing to
54035** PagerSavepoint.iHdrOffset, or to the end of the main journal
54036** file if PagerSavepoint.iHdrOffset is zero.
54037**
54038** * If PagerSavepoint.iHdrOffset is not zero, then pages are played
54039** back starting from the journal header immediately following
54040** PagerSavepoint.iHdrOffset to the end of the main journal file.
54041**
54042** * Pages are then played back from the sub-journal file, starting
54043** with the PagerSavepoint.iSubRec and continuing to the end of
54044** the journal file.
54045**
54046** Throughout the rollback process, each time a page is rolled back, the
54047** corresponding bit is set in a bitvec structure (variable pDone in the
54048** implementation below). This is used to ensure that a page is only
54049** rolled back the first time it is encountered in either journal.
54050**
54051** If pSavepoint is NULL, then pages are only played back from the main
54052** journal file. There is no need for a bitvec in this case.
54053**
54054** In either case, before playback commences the Pager.dbSize variable
54055** is reset to the value that it held at the start of the savepoint
54056** (or transaction). No page with a page-number greater than this value
54057** is played back. If one is encountered it is simply skipped.
54058*/
54059static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
54060 i64 szJ; /* Effective size of the main journal */
54061 i64 iHdrOff; /* End of first segment of main-journal records */
54062 int rc = SQLITE_OK0; /* Return code */
54063 Bitvec *pDone = 0; /* Bitvec to ensure pages played back only once */
54064
54065 assert( pPager->eState!=PAGER_ERROR )((void) (0));
54066 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
54067
54068 /* Allocate a bitvec to use to store the set of pages rolled back */
54069 if( pSavepoint ){
54070 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
54071 if( !pDone ){
54072 return SQLITE_NOMEM_BKPT7;
54073 }
54074 }
54075
54076 /* Set the database size back to the value it was before the savepoint
54077 ** being reverted was opened.
54078 */
54079 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
54080 pPager->changeCountDone = pPager->tempFile;
54081
54082 if( !pSavepoint && pagerUseWal(pPager)((pPager)->pWal!=0) ){
54083 return pagerRollbackWal(pPager);
54084 }
54085
54086 /* Use pPager->journalOff as the effective size of the main rollback
54087 ** journal. The actual file might be larger than this in
54088 ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST. But anything
54089 ** past pPager->journalOff is off-limits to us.
54090 */
54091 szJ = pPager->journalOff;
54092 assert( pagerUseWal(pPager)==0 || szJ==0 )((void) (0));
54093
54094 /* Begin by rolling back records from the main journal starting at
54095 ** PagerSavepoint.iOffset and continuing to the next journal header.
54096 ** There might be records in the main journal that have a page number
54097 ** greater than the current database size (pPager->dbSize) but those
54098 ** will be skipped automatically. Pages are added to pDone as they
54099 ** are played back.
54100 */
54101 if( pSavepoint && !pagerUseWal(pPager)((pPager)->pWal!=0) ){
54102 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
54103 pPager->journalOff = pSavepoint->iOffset;
54104 while( rc==SQLITE_OK0 && pPager->journalOff<iHdrOff ){
54105 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
54106 }
54107 assert( rc!=SQLITE_DONE )((void) (0));
54108 }else{
54109 pPager->journalOff = 0;
54110 }
54111
54112 /* Continue rolling back records out of the main journal starting at
54113 ** the first journal header seen and continuing until the effective end
54114 ** of the main journal file. Continue to skip out-of-range pages and
54115 ** continue adding pages rolled back to pDone.
54116 */
54117 while( rc==SQLITE_OK0 && pPager->journalOff<szJ ){
54118 u32 ii; /* Loop counter */
54119 u32 nJRec = 0; /* Number of Journal Records */
54120 u32 dummy;
54121 rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
54122 assert( rc!=SQLITE_DONE )((void) (0));
54123
54124 /*
54125 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
54126 ** test is related to ticket #2565. See the discussion in the
54127 ** pager_playback() function for additional information.
54128 */
54129 if( nJRec==0
54130 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)(pPager->sectorSize)==pPager->journalOff
54131 ){
54132 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager)((pPager->pageSize) + 8));
54133 }
54134 for(ii=0; rc==SQLITE_OK0 && ii<nJRec && pPager->journalOff<szJ; ii++){
54135 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
54136 }
54137 assert( rc!=SQLITE_DONE )((void) (0));
54138 }
54139 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ )((void) (0));
54140
54141 /* Finally, rollback pages from the sub-journal. Page that were
54142 ** previously rolled back out of the main journal (and are hence in pDone)
54143 ** will be skipped. Out-of-range pages are also skipped.
54144 */
54145 if( pSavepoint ){
54146 u32 ii; /* Loop counter */
54147 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
54148
54149 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
54150 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
54151 }
54152 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK0 && ii<pPager->nSubRec; ii++){
54153 assert( offset==(i64)ii*(4+pPager->pageSize) )((void) (0));
54154 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
54155 }
54156 assert( rc!=SQLITE_DONE )((void) (0));
54157 }
54158
54159 sqlite3BitvecDestroy(pDone);
54160 if( rc==SQLITE_OK0 ){
54161 pPager->journalOff = szJ;
54162 }
54163
54164 return rc;
54165}
54166
54167/*
54168** Change the maximum number of in-memory pages that are allowed
54169** before attempting to recycle clean and unused pages.
54170*/
54171SQLITE_PRIVATEstatic void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
54172 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
54173}
54174
54175/*
54176** Change the maximum number of in-memory pages that are allowed
54177** before attempting to spill pages to journal.
54178*/
54179SQLITE_PRIVATEstatic int sqlite3PagerSetSpillsize(Pager *pPager, int mxPage){
54180 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
54181}
54182
54183/*
54184** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
54185*/
54186static void pagerFixMaplimit(Pager *pPager){
54187#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
54188 sqlite3_file *fd = pPager->fd;
54189 if( isOpen(fd)((fd)->pMethods!=0) && fd->pMethods->iVersion>=3 ){
54190 sqlite3_int64 sz;
54191 sz = pPager->szMmap;
54192 pPager->bUseFetch = (sz>0);
54193 setGetterMethod(pPager);
54194 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE18, &sz);
54195 }
54196#endif
54197}
54198
54199/*
54200** Change the maximum size of any memory mapping made of the database file.
54201*/
54202SQLITE_PRIVATEstatic void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
54203 pPager->szMmap = szMmap;
54204 pagerFixMaplimit(pPager);
54205}
54206
54207/*
54208** Free as much memory as possible from the pager.
54209*/
54210SQLITE_PRIVATEstatic void sqlite3PagerShrink(Pager *pPager){
54211 sqlite3PcacheShrink(pPager->pPCache);
54212}
54213
54214/*
54215** Adjust settings of the pager to those specified in the pgFlags parameter.
54216**
54217** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
54218** of the database to damage due to OS crashes or power failures by
54219** changing the number of syncs()s when writing the journals.
54220** There are four levels:
54221**
54222** OFF sqlite3OsSync() is never called. This is the default
54223** for temporary and transient files.
54224**
54225** NORMAL The journal is synced once before writes begin on the
54226** database. This is normally adequate protection, but
54227** it is theoretically possible, though very unlikely,
54228** that an inopertune power failure could leave the journal
54229** in a state which would cause damage to the database
54230** when it is rolled back.
54231**
54232** FULL The journal is synced twice before writes begin on the
54233** database (with some additional information - the nRec field
54234** of the journal header - being written in between the two
54235** syncs). If we assume that writing a
54236** single disk sector is atomic, then this mode provides
54237** assurance that the journal will not be corrupted to the
54238** point of causing damage to the database during rollback.
54239**
54240** EXTRA This is like FULL except that is also syncs the directory
54241** that contains the rollback journal after the rollback
54242** journal is unlinked.
54243**
54244** The above is for a rollback-journal mode. For WAL mode, OFF continues
54245** to mean that no syncs ever occur. NORMAL means that the WAL is synced
54246** prior to the start of checkpoint and that the database file is synced
54247** at the conclusion of the checkpoint if the entire content of the WAL
54248** was written back into the database. But no sync operations occur for
54249** an ordinary commit in NORMAL mode with WAL. FULL means that the WAL
54250** file is synced following each commit operation, in addition to the
54251** syncs associated with NORMAL. There is no difference between FULL
54252** and EXTRA for WAL mode.
54253**
54254** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL. The
54255** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
54256** using fcntl(F_FULLFSYNC). SQLITE_SYNC_NORMAL means to do an
54257** ordinary fsync() call. There is no difference between SQLITE_SYNC_FULL
54258** and SQLITE_SYNC_NORMAL on platforms other than MacOSX. But the
54259** synchronous=FULL versus synchronous=NORMAL setting determines when
54260** the xSync primitive is called and is relevant to all platforms.
54261**
54262** Numeric values associated with these states are OFF==1, NORMAL=2,
54263** and FULL=3.
54264*/
54265#ifndef SQLITE_OMIT_PAGER_PRAGMAS
54266SQLITE_PRIVATEstatic void sqlite3PagerSetFlags(
54267 Pager *pPager, /* The pager to set safety level for */
54268 unsigned pgFlags /* Various flags */
54269){
54270 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK0x07;
54271 if( pPager->tempFile ){
54272 pPager->noSync = 1;
54273 pPager->fullSync = 0;
54274 pPager->extraSync = 0;
54275 }else{
54276 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF0x01 ?1:0;
54277 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL0x03 ?1:0;
54278 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA0x04 ?1:0;
54279 }
54280 if( pPager->noSync ){
54281 pPager->syncFlags = 0;
54282 }else if( pgFlags & PAGER_FULLFSYNC0x08 ){
54283 pPager->syncFlags = SQLITE_SYNC_FULL0x00003;
54284 }else{
54285 pPager->syncFlags = SQLITE_SYNC_NORMAL0x00002;
54286 }
54287 pPager->walSyncFlags = (pPager->syncFlags<<2);
54288 if( pPager->fullSync ){
54289 pPager->walSyncFlags |= pPager->syncFlags;
54290 }
54291 if( (pgFlags & PAGER_CKPT_FULLFSYNC0x10) && !pPager->noSync ){
54292 pPager->walSyncFlags |= (SQLITE_SYNC_FULL0x00003<<2);
54293 }
54294 if( pgFlags & PAGER_CACHESPILL0x20 ){
54295 pPager->doNotSpill &= ~SPILLFLAG_OFF0x01;
54296 }else{
54297 pPager->doNotSpill |= SPILLFLAG_OFF0x01;
54298 }
54299}
54300#endif
54301
54302/*
54303** The following global variable is incremented whenever the library
54304** attempts to open a temporary file. This information is used for
54305** testing and analysis only.
54306*/
54307#ifdef SQLITE_TEST
54308SQLITE_API int sqlite3_opentemp_count = 0;
54309#endif
54310
54311/*
54312** Open a temporary file.
54313**
54314** Write the file descriptor into *pFile. Return SQLITE_OK on success
54315** or some other error code if we fail. The OS will automatically
54316** delete the temporary file when it is closed.
54317**
54318** The flags passed to the VFS layer xOpen() call are those specified
54319** by parameter vfsFlags ORed with the following:
54320**
54321** SQLITE_OPEN_READWRITE
54322** SQLITE_OPEN_CREATE
54323** SQLITE_OPEN_EXCLUSIVE
54324** SQLITE_OPEN_DELETEONCLOSE
54325*/
54326static int pagerOpentemp(
54327 Pager *pPager, /* The pager object */
54328 sqlite3_file *pFile, /* Write the file descriptor here */
54329 int vfsFlags /* Flags passed through to the VFS */
54330){
54331 int rc; /* Return code */
54332
54333#ifdef SQLITE_TEST
54334 sqlite3_opentemp_count++; /* Used for testing and analysis only */
54335#endif
54336
54337 vfsFlags |= SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
54338 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
54339 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
54340 assert( rc!=SQLITE_OK || isOpen(pFile) )((void) (0));
54341 return rc;
54342}
54343
54344/*
54345** Set the busy handler function.
54346**
54347** The pager invokes the busy-handler if sqlite3OsLock() returns
54348** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
54349** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
54350** lock. It does *not* invoke the busy handler when upgrading from
54351** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
54352** (which occurs during hot-journal rollback). Summary:
54353**
54354** Transition | Invokes xBusyHandler
54355** --------------------------------------------------------
54356** NO_LOCK -> SHARED_LOCK | Yes
54357** SHARED_LOCK -> RESERVED_LOCK | No
54358** SHARED_LOCK -> EXCLUSIVE_LOCK | No
54359** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
54360**
54361** If the busy-handler callback returns non-zero, the lock is
54362** retried. If it returns zero, then the SQLITE_BUSY error is
54363** returned to the caller of the pager API function.
54364*/
54365SQLITE_PRIVATEstatic void sqlite3PagerSetBusyHandler(
54366 Pager *pPager, /* Pager object */
54367 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
54368 void *pBusyHandlerArg /* Argument to pass to xBusyHandler */
54369){
54370 void **ap;
54371 pPager->xBusyHandler = xBusyHandler;
54372 pPager->pBusyHandlerArg = pBusyHandlerArg;
54373 ap = (void **)&pPager->xBusyHandler;
54374 assert( ((int(*)(void *))(ap[0]))==xBusyHandler )((void) (0));
54375 assert( ap[1]==pBusyHandlerArg )((void) (0));
54376 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER15, (void *)ap);
54377}
54378
54379/*
54380** Change the page size used by the Pager object. The new page size
54381** is passed in *pPageSize.
54382**
54383** If the pager is in the error state when this function is called, it
54384** is a no-op. The value returned is the error state error code (i.e.
54385** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
54386**
54387** Otherwise, if all of the following are true:
54388**
54389** * the new page size (value of *pPageSize) is valid (a power
54390** of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
54391**
54392** * there are no outstanding page references, and
54393**
54394** * the database is either not an in-memory database or it is
54395** an in-memory database that currently consists of zero pages.
54396**
54397** then the pager object page size is set to *pPageSize.
54398**
54399** If the page size is changed, then this function uses sqlite3PagerMalloc()
54400** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
54401** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
54402** In all other cases, SQLITE_OK is returned.
54403**
54404** If the page size is not changed, either because one of the enumerated
54405** conditions above is not true, the pager was in error state when this
54406** function was called, or because the memory allocation attempt failed,
54407** then *pPageSize is set to the old, retained page size before returning.
54408*/
54409SQLITE_PRIVATEstatic int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
54410 int rc = SQLITE_OK0;
54411
54412 /* It is not possible to do a full assert_pager_state() here, as this
54413 ** function may be called from within PagerOpen(), before the state
54414 ** of the Pager object is internally consistent.
54415 **
54416 ** At one point this function returned an error if the pager was in
54417 ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
54418 ** there is at least one outstanding page reference, this function
54419 ** is a no-op for that case anyhow.
54420 */
54421
54422 u32 pageSize = *pPageSize;
54423 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) )((void) (0));
54424 if( (pPager->memDb==0 || pPager->dbSize==0)
54425 && sqlite3PcacheRefCount(pPager->pPCache)==0
54426 && pageSize && pageSize!=(u32)pPager->pageSize
54427 ){
54428 char *pNew = NULL((void*)0); /* New temp space */
54429 i64 nByte = 0;
54430
54431 if( pPager->eState>PAGER_OPEN0 && isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
54432 rc = sqlite3OsFileSize(pPager->fd, &nByte);
54433 }
54434 if( rc==SQLITE_OK0 ){
54435 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
54436 * cell header parser will never run off the end of the allocation */
54437 pNew = (char *)sqlite3PageMalloc(pageSize+8);
54438 if( !pNew ){
54439 rc = SQLITE_NOMEM_BKPT7;
54440 }else{
54441 memset(pNew+pageSize, 0, 8);
54442 }
54443 }
54444
54445 if( rc==SQLITE_OK0 ){
54446 pager_reset(pPager);
54447 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
54448 }
54449 if( rc==SQLITE_OK0 ){
54450 sqlite3PageFree(pPager->pTmpSpace);
54451 pPager->pTmpSpace = pNew;
54452 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
54453 pPager->pageSize = pageSize;
54454 }else{
54455 sqlite3PageFree(pNew);
54456 }
54457 }
54458
54459 *pPageSize = pPager->pageSize;
54460 if( rc==SQLITE_OK0 ){
54461 if( nReserve<0 ) nReserve = pPager->nReserve;
54462 assert( nReserve>=0 && nReserve<1000 )((void) (0));
54463 pPager->nReserve = (i16)nReserve;
54464 pagerReportSize(pPager);
54465 pagerFixMaplimit(pPager);
54466 }
54467 return rc;
54468}
54469
54470/*
54471** Return a pointer to the "temporary page" buffer held internally
54472** by the pager. This is a buffer that is big enough to hold the
54473** entire content of a database page. This buffer is used internally
54474** during rollback and will be overwritten whenever a rollback
54475** occurs. But other modules are free to use it too, as long as
54476** no rollbacks are happening.
54477*/
54478SQLITE_PRIVATEstatic void *sqlite3PagerTempSpace(Pager *pPager){
54479 return pPager->pTmpSpace;
54480}
54481
54482/*
54483** Attempt to set the maximum database page count if mxPage is positive.
54484** Make no changes if mxPage is zero or negative. And never reduce the
54485** maximum page count below the current size of the database.
54486**
54487** Regardless of mxPage, return the current maximum page count.
54488*/
54489SQLITE_PRIVATEstatic int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
54490 if( mxPage>0 ){
54491 pPager->mxPgno = mxPage;
54492 }
54493 assert( pPager->eState!=PAGER_OPEN )((void) (0)); /* Called only by OP_MaxPgcnt */
54494 /* assert( pPager->mxPgno>=pPager->dbSize ); */
54495 /* OP_MaxPgcnt ensures that the parameter passed to this function is not
54496 ** less than the total number of valid pages in the database. But this
54497 ** may be less than Pager.dbSize, and so the assert() above is not valid */
54498 return pPager->mxPgno;
54499}
54500
54501/*
54502** The following set of routines are used to disable the simulated
54503** I/O error mechanism. These routines are used to avoid simulated
54504** errors in places where we do not care about errors.
54505**
54506** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
54507** and generate no code.
54508*/
54509#ifdef SQLITE_TEST
54510SQLITE_API extern int sqlite3_io_error_pending;
54511SQLITE_API extern int sqlite3_io_error_hit;
54512static int saved_cnt;
54513void disable_simulated_io_errors(void){
54514 saved_cnt = sqlite3_io_error_pending;
54515 sqlite3_io_error_pending = -1;
54516}
54517void enable_simulated_io_errors(void){
54518 sqlite3_io_error_pending = saved_cnt;
54519}
54520#else
54521# define disable_simulated_io_errors()
54522# define enable_simulated_io_errors()
54523#endif
54524
54525/*
54526** Read the first N bytes from the beginning of the file into memory
54527** that pDest points to.
54528**
54529** If the pager was opened on a transient file (zFilename==""), or
54530** opened on a file less than N bytes in size, the output buffer is
54531** zeroed and SQLITE_OK returned. The rationale for this is that this
54532** function is used to read database headers, and a new transient or
54533** zero sized database has a header than consists entirely of zeroes.
54534**
54535** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
54536** the error code is returned to the caller and the contents of the
54537** output buffer undefined.
54538*/
54539SQLITE_PRIVATEstatic int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
54540 int rc = SQLITE_OK0;
54541 memset(pDest, 0, N);
54542 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
54543
54544 /* This routine is only called by btree immediately after creating
54545 ** the Pager object. There has not been an opportunity to transition
54546 ** to WAL mode yet.
54547 */
54548 assert( !pagerUseWal(pPager) )((void) (0));
54549
54550 if( isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
54551 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
54552 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
54553 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
54554 rc = SQLITE_OK0;
54555 }
54556 }
54557 return rc;
54558}
54559
54560/*
54561** This function may only be called when a read-transaction is open on
54562** the pager. It returns the total number of pages in the database.
54563**
54564** However, if the file is between 1 and <page-size> bytes in size, then
54565** this is considered a 1 page file.
54566*/
54567SQLITE_PRIVATEstatic void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
54568 assert( pPager->eState>=PAGER_READER )((void) (0));
54569 assert( pPager->eState!=PAGER_WRITER_FINISHED )((void) (0));
54570 *pnPage = (int)pPager->dbSize;
54571}
54572
54573
54574/*
54575** Try to obtain a lock of type locktype on the database file. If
54576** a similar or greater lock is already held, this function is a no-op
54577** (returning SQLITE_OK immediately).
54578**
54579** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
54580** the busy callback if the lock is currently not available. Repeat
54581** until the busy callback returns false or until the attempt to
54582** obtain the lock succeeds.
54583**
54584** Return SQLITE_OK on success and an error code if we cannot obtain
54585** the lock. If the lock is obtained successfully, set the Pager.state
54586** variable to locktype before returning.
54587*/
54588static int pager_wait_on_lock(Pager *pPager, int locktype){
54589 int rc; /* Return code */
54590
54591 /* Check that this is either a no-op (because the requested lock is
54592 ** already held), or one of the transitions that the busy-handler
54593 ** may be invoked during, according to the comment above
54594 ** sqlite3PagerSetBusyhandler().
54595 */
54596 assert( (pPager->eLock>=locktype)((void) (0))
54597 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)((void) (0))
54598 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)((void) (0))
54599 )((void) (0));
54600
54601 do {
54602 rc = pagerLockDb(pPager, locktype);
54603 }while( rc==SQLITE_BUSY5 && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
54604 return rc;
54605}
54606
54607/*
54608** Function assertTruncateConstraint(pPager) checks that one of the
54609** following is true for all dirty pages currently in the page-cache:
54610**
54611** a) The page number is less than or equal to the size of the
54612** current database image, in pages, OR
54613**
54614** b) if the page content were written at this time, it would not
54615** be necessary to write the current content out to the sub-journal
54616** (as determined by function subjRequiresPage()).
54617**
54618** If the condition asserted by this function were not true, and the
54619** dirty page were to be discarded from the cache via the pagerStress()
54620** routine, pagerStress() would not write the current page content to
54621** the database file. If a savepoint transaction were rolled back after
54622** this happened, the correct behavior would be to restore the current
54623** content of the page. However, since this content is not present in either
54624** the database file or the portion of the rollback journal and
54625** sub-journal rolled back the content could not be restored and the
54626** database image would become corrupt. It is therefore fortunate that
54627** this circumstance cannot arise.
54628*/
54629#if defined(SQLITE_DEBUG)
54630static void assertTruncateConstraintCb(PgHdr *pPg){
54631 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
54632 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize )((void) (0));
54633}
54634static void assertTruncateConstraint(Pager *pPager){
54635 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
54636}
54637#else
54638# define assertTruncateConstraint(pPager)
54639#endif
54640
54641/*
54642** Truncate the in-memory database file image to nPage pages. This
54643** function does not actually modify the database file on disk. It
54644** just sets the internal state of the pager object so that the
54645** truncation will be done when the current transaction is committed.
54646**
54647** This function is only called right before committing a transaction.
54648** Once this function has been called, the transaction must either be
54649** rolled back or committed. It is not safe to call this function and
54650** then continue writing to the database.
54651*/
54652SQLITE_PRIVATEstatic void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
54653 assert( pPager->dbSize>=nPage )((void) (0));
54654 assert( pPager->eState>=PAGER_WRITER_CACHEMOD )((void) (0));
54655 pPager->dbSize = nPage;
54656
54657 /* At one point the code here called assertTruncateConstraint() to
54658 ** ensure that all pages being truncated away by this operation are,
54659 ** if one or more savepoints are open, present in the savepoint
54660 ** journal so that they can be restored if the savepoint is rolled
54661 ** back. This is no longer necessary as this function is now only
54662 ** called right before committing a transaction. So although the
54663 ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
54664 ** they cannot be rolled back. So the assertTruncateConstraint() call
54665 ** is no longer correct. */
54666}
54667
54668
54669/*
54670** This function is called before attempting a hot-journal rollback. It
54671** syncs the journal file to disk, then sets pPager->journalHdr to the
54672** size of the journal file so that the pager_playback() routine knows
54673** that the entire journal file has been synced.
54674**
54675** Syncing a hot-journal to disk before attempting to roll it back ensures
54676** that if a power-failure occurs during the rollback, the process that
54677** attempts rollback following system recovery sees the same journal
54678** content as this process.
54679**
54680** If everything goes as planned, SQLITE_OK is returned. Otherwise,
54681** an SQLite error code.
54682*/
54683static int pagerSyncHotJournal(Pager *pPager){
54684 int rc = SQLITE_OK0;
54685 if( !pPager->noSync ){
54686 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL0x00002);
54687 }
54688 if( rc==SQLITE_OK0 ){
54689 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
54690 }
54691 return rc;
54692}
54693
54694#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
54695/*
54696** Obtain a reference to a memory mapped page object for page number pgno.
54697** The new object will use the pointer pData, obtained from xFetch().
54698** If successful, set *ppPage to point to the new page reference
54699** and return SQLITE_OK. Otherwise, return an SQLite error code and set
54700** *ppPage to zero.
54701**
54702** Page references obtained by calling this function should be released
54703** by calling pagerReleaseMapPage().
54704*/
54705static int pagerAcquireMapPage(
54706 Pager *pPager, /* Pager object */
54707 Pgno pgno, /* Page number */
54708 void *pData, /* xFetch()'d data for this page */
54709 PgHdr **ppPage /* OUT: Acquired page object */
54710){
54711 PgHdr *p; /* Memory mapped page to return */
54712
54713 if( pPager->pMmapFreelist ){
54714 *ppPage = p = pPager->pMmapFreelist;
54715 pPager->pMmapFreelist = p->pDirty;
54716 p->pDirty = 0;
54717 assert( pPager->nExtra>=8 )((void) (0));
54718 memset(p->pExtra, 0, 8);
54719 }else{
54720 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
54721 if( p==0 ){
54722 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
54723 return SQLITE_NOMEM_BKPT7;
54724 }
54725 p->pExtra = (void *)&p[1];
54726 p->flags = PGHDR_MMAP0x020;
54727 p->nRef = 1;
54728 p->pPager = pPager;
54729 }
54730
54731 assert( p->pExtra==(void *)&p[1] )((void) (0));
54732 assert( p->pPage==0 )((void) (0));
54733 assert( p->flags==PGHDR_MMAP )((void) (0));
54734 assert( p->pPager==pPager )((void) (0));
54735 assert( p->nRef==1 )((void) (0));
54736
54737 p->pgno = pgno;
54738 p->pData = pData;
54739 pPager->nMmapOut++;
54740
54741 return SQLITE_OK0;
54742}
54743#endif
54744
54745/*
54746** Release a reference to page pPg. pPg must have been returned by an
54747** earlier call to pagerAcquireMapPage().
54748*/
54749static void pagerReleaseMapPage(PgHdr *pPg){
54750 Pager *pPager = pPg->pPager;
54751 pPager->nMmapOut--;
54752 pPg->pDirty = pPager->pMmapFreelist;
54753 pPager->pMmapFreelist = pPg;
54754
54755 assert( pPager->fd->pMethods->iVersion>=3 )((void) (0));
54756 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
54757}
54758
54759/*
54760** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
54761*/
54762static void pagerFreeMapHdrs(Pager *pPager){
54763 PgHdr *p;
54764 PgHdr *pNext;
54765 for(p=pPager->pMmapFreelist; p; p=pNext){
54766 pNext = p->pDirty;
54767 sqlite3_free(p);
54768 }
54769}
54770
54771/* Verify that the database file has not be deleted or renamed out from
54772** under the pager. Return SQLITE_OK if the database is still where it ought
54773** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
54774** code from sqlite3OsAccess()) if the database has gone missing.
54775*/
54776static int databaseIsUnmoved(Pager *pPager){
54777 int bHasMoved = 0;
54778 int rc;
54779
54780 if( pPager->tempFile ) return SQLITE_OK0;
54781 if( pPager->dbSize==0 ) return SQLITE_OK0;
54782 assert( pPager->zFilename && pPager->zFilename[0] )((void) (0));
54783 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED20, &bHasMoved);
54784 if( rc==SQLITE_NOTFOUND12 ){
54785 /* If the HAS_MOVED file-control is unimplemented, assume that the file
54786 ** has not been moved. That is the historical behavior of SQLite: prior to
54787 ** version 3.8.3, it never checked */
54788 rc = SQLITE_OK0;
54789 }else if( rc==SQLITE_OK0 && bHasMoved ){
54790 rc = SQLITE_READONLY_DBMOVED(8 | (4<<8));
54791 }
54792 return rc;
54793}
54794
54795
54796/*
54797** Shutdown the page cache. Free all memory and close all files.
54798**
54799** If a transaction was in progress when this routine is called, that
54800** transaction is rolled back. All outstanding pages are invalidated
54801** and their memory is freed. Any attempt to use a page associated
54802** with this page cache after this function returns will likely
54803** result in a coredump.
54804**
54805** This function always succeeds. If a transaction is active an attempt
54806** is made to roll it back. If an error occurs during the rollback
54807** a hot journal may be left in the filesystem but no error is returned
54808** to the caller.
54809*/
54810SQLITE_PRIVATEstatic int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
54811 u8 *pTmp = (u8*)pPager->pTmpSpace;
54812 assert( db || pagerUseWal(pPager)==0 )((void) (0));
54813 assert( assert_pager_state(pPager) )((void) (0));
54814 disable_simulated_io_errors();
54815 sqlite3BeginBenignMalloc();
54816 pagerFreeMapHdrs(pPager);
54817 /* pPager->errCode = 0; */
54818 pPager->exclusiveMode = 0;
54819#ifndef SQLITE_OMIT_WAL
54820 {
54821 u8 *a = 0;
54822 assert( db || pPager->pWal==0 )((void) (0));
54823 if( db && 0==(db->flags & SQLITE_NoCkptOnClose0x00000800)
54824 && SQLITE_OK0==databaseIsUnmoved(pPager)
54825 ){
54826 a = pTmp;
54827 }
54828 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
54829 pPager->pWal = 0;
54830 }
54831#endif
54832 pager_reset(pPager);
54833 if( MEMDBpPager->memDb ){
54834 pager_unlock(pPager);
54835 }else{
54836 /* If it is open, sync the journal file before calling UnlockAndRollback.
54837 ** If this is not done, then an unsynced portion of the open journal
54838 ** file may be played back into the database. If a power failure occurs
54839 ** while this is happening, the database could become corrupt.
54840 **
54841 ** If an error occurs while trying to sync the journal, shift the pager
54842 ** into the ERROR state. This causes UnlockAndRollback to unlock the
54843 ** database and close the journal file without attempting to roll it
54844 ** back or finalize it. The next database user will have to do hot-journal
54845 ** rollback before accessing the database file.
54846 */
54847 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
54848 pager_error(pPager, pagerSyncHotJournal(pPager));
54849 }
54850 pagerUnlockAndRollback(pPager);
54851 }
54852 sqlite3EndBenignMalloc();
54853 enable_simulated_io_errors();
54854 PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
54855 IOTRACE(("CLOSE %p\n", pPager))
54856 sqlite3OsClose(pPager->jfd);
54857 sqlite3OsClose(pPager->fd);
54858 sqlite3PageFree(pTmp);
54859 sqlite3PcacheClose(pPager->pPCache);
54860
54861#ifdef SQLITE_HAS_CODEC
54862 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
54863#endif
54864
54865 assert( !pPager->aSavepoint && !pPager->pInJournal )((void) (0));
54866 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) )((void) (0));
54867
54868 sqlite3_free(pPager);
54869 return SQLITE_OK0;
54870}
54871
54872#if !defined(NDEBUG1) || defined(SQLITE_TEST)
54873/*
54874** Return the page number for page pPg.
54875*/
54876SQLITE_PRIVATEstatic Pgno sqlite3PagerPagenumber(DbPage *pPg){
54877 return pPg->pgno;
54878}
54879#endif
54880
54881/*
54882** Increment the reference count for page pPg.
54883*/
54884SQLITE_PRIVATEstatic void sqlite3PagerRef(DbPage *pPg){
54885 sqlite3PcacheRef(pPg);
54886}
54887
54888/*
54889** Sync the journal. In other words, make sure all the pages that have
54890** been written to the journal have actually reached the surface of the
54891** disk and can be restored in the event of a hot-journal rollback.
54892**
54893** If the Pager.noSync flag is set, then this function is a no-op.
54894** Otherwise, the actions required depend on the journal-mode and the
54895** device characteristics of the file-system, as follows:
54896**
54897** * If the journal file is an in-memory journal file, no action need
54898** be taken.
54899**
54900** * Otherwise, if the device does not support the SAFE_APPEND property,
54901** then the nRec field of the most recently written journal header
54902** is updated to contain the number of journal records that have
54903** been written following it. If the pager is operating in full-sync
54904** mode, then the journal file is synced before this field is updated.
54905**
54906** * If the device does not support the SEQUENTIAL property, then
54907** journal file is synced.
54908**
54909** Or, in pseudo-code:
54910**
54911** if( NOT <in-memory journal> ){
54912** if( NOT SAFE_APPEND ){
54913** if( <full-sync mode> ) xSync(<journal file>);
54914** <update nRec field>
54915** }
54916** if( NOT SEQUENTIAL ) xSync(<journal file>);
54917** }
54918**
54919** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
54920** page currently held in memory before returning SQLITE_OK. If an IO
54921** error is encountered, then the IO error code is returned to the caller.
54922*/
54923static int syncJournal(Pager *pPager, int newHdr){
54924 int rc; /* Return code */
54925
54926 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
54927 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
54928 )((void) (0));
54929 assert( assert_pager_state(pPager) )((void) (0));
54930 assert( !pagerUseWal(pPager) )((void) (0));
54931
54932 rc = sqlite3PagerExclusiveLock(pPager);
54933 if( rc!=SQLITE_OK0 ) return rc;
54934
54935 if( !pPager->noSync ){
54936 assert( !pPager->tempFile )((void) (0));
54937 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY4 ){
54938 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
54939 assert( isOpen(pPager->jfd) )((void) (0));
54940
54941 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND0x00000200) ){
54942 /* This block deals with an obscure problem. If the last connection
54943 ** that wrote to this database was operating in persistent-journal
54944 ** mode, then the journal file may at this point actually be larger
54945 ** than Pager.journalOff bytes. If the next thing in the journal
54946 ** file happens to be a journal-header (written as part of the
54947 ** previous connection's transaction), and a crash or power-failure
54948 ** occurs after nRec is updated but before this connection writes
54949 ** anything else to the journal file (or commits/rolls back its
54950 ** transaction), then SQLite may become confused when doing the
54951 ** hot-journal rollback following recovery. It may roll back all
54952 ** of this connections data, then proceed to rolling back the old,
54953 ** out-of-date data that follows it. Database corruption.
54954 **
54955 ** To work around this, if the journal file does appear to contain
54956 ** a valid header following Pager.journalOff, then write a 0x00
54957 ** byte to the start of it to prevent it from being recognized.
54958 **
54959 ** Variable iNextHdrOffset is set to the offset at which this
54960 ** problematic header will occur, if it exists. aMagic is used
54961 ** as a temporary buffer to inspect the first couple of bytes of
54962 ** the potential journal header.
54963 */
54964 i64 iNextHdrOffset;
54965 u8 aMagic[8];
54966 u8 zHeader[sizeof(aJournalMagic)+4];
54967
54968 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
54969 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec)sqlite3Put4byte((u8*)&zHeader[sizeof(aJournalMagic)],pPager
->nRec)
;
54970
54971 iNextHdrOffset = journalHdrOffset(pPager);
54972 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
54973 if( rc==SQLITE_OK0 && 0==memcmp(aMagic, aJournalMagic, 8) ){
54974 static const u8 zerobyte = 0;
54975 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
54976 }
54977 if( rc!=SQLITE_OK0 && rc!=SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
54978 return rc;
54979 }
54980
54981 /* Write the nRec value into the journal file header. If in
54982 ** full-synchronous mode, sync the journal first. This ensures that
54983 ** all data has really hit the disk before nRec is updated to mark
54984 ** it as a candidate for rollback.
54985 **
54986 ** This is not required if the persistent media supports the
54987 ** SAFE_APPEND property. Because in this case it is not possible
54988 ** for garbage data to be appended to the file, the nRec field
54989 ** is populated with 0xFFFFFFFF when the journal header is written
54990 ** and never needs to be updated.
54991 */
54992 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL0x00000400) ){
54993 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
54994 IOTRACE(("JSYNC %p\n", pPager))
54995 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
54996 if( rc!=SQLITE_OK0 ) return rc;
54997 }
54998 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
54999 rc = sqlite3OsWrite(
55000 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
55001 );
55002 if( rc!=SQLITE_OK0 ) return rc;
55003 }
55004 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL0x00000400) ){
55005 PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
55006 IOTRACE(("JSYNC %p\n", pPager))
55007 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
55008 (pPager->syncFlags==SQLITE_SYNC_FULL0x00003?SQLITE_SYNC_DATAONLY0x00010:0)
55009 );
55010 if( rc!=SQLITE_OK0 ) return rc;
55011 }
55012
55013 pPager->journalHdr = pPager->journalOff;
55014 if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND0x00000200) ){
55015 pPager->nRec = 0;
55016 rc = writeJournalHdr(pPager);
55017 if( rc!=SQLITE_OK0 ) return rc;
55018 }
55019 }else{
55020 pPager->journalHdr = pPager->journalOff;
55021 }
55022 }
55023
55024 /* Unless the pager is in noSync mode, the journal file was just
55025 ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
55026 ** all pages.
55027 */
55028 sqlite3PcacheClearSyncFlags(pPager->pPCache);
55029 pPager->eState = PAGER_WRITER_DBMOD4;
55030 assert( assert_pager_state(pPager) )((void) (0));
55031 return SQLITE_OK0;
55032}
55033
55034/*
55035** The argument is the first in a linked list of dirty pages connected
55036** by the PgHdr.pDirty pointer. This function writes each one of the
55037** in-memory pages in the list to the database file. The argument may
55038** be NULL, representing an empty list. In this case this function is
55039** a no-op.
55040**
55041** The pager must hold at least a RESERVED lock when this function
55042** is called. Before writing anything to the database file, this lock
55043** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
55044** SQLITE_BUSY is returned and no data is written to the database file.
55045**
55046** If the pager is a temp-file pager and the actual file-system file
55047** is not yet open, it is created and opened before any data is
55048** written out.
55049**
55050** Once the lock has been upgraded and, if necessary, the file opened,
55051** the pages are written out to the database file in list order. Writing
55052** a page is skipped if it meets either of the following criteria:
55053**
55054** * The page number is greater than Pager.dbSize, or
55055** * The PGHDR_DONT_WRITE flag is set on the page.
55056**
55057** If writing out a page causes the database file to grow, Pager.dbFileSize
55058** is updated accordingly. If page 1 is written out, then the value cached
55059** in Pager.dbFileVers[] is updated to match the new value stored in
55060** the database file.
55061**
55062** If everything is successful, SQLITE_OK is returned. If an IO error
55063** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
55064** be obtained, SQLITE_BUSY is returned.
55065*/
55066static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
55067 int rc = SQLITE_OK0; /* Return code */
55068
55069 /* This function is only called for rollback pagers in WRITER_DBMOD state. */
55070 assert( !pagerUseWal(pPager) )((void) (0));
55071 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD )((void) (0));
55072 assert( pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
55073 assert( isOpen(pPager->fd) || pList->pDirty==0 )((void) (0));
55074
55075 /* If the file is a temp-file has not yet been opened, open it now. It
55076 ** is not possible for rc to be other than SQLITE_OK if this branch
55077 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
55078 */
55079 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) ){
55080 assert( pPager->tempFile && rc==SQLITE_OK )((void) (0));
55081 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
55082 }
55083
55084 /* Before the first write, give the VFS a hint of what the final
55085 ** file size will be.
55086 */
55087 assert( rc!=SQLITE_OK || isOpen(pPager->fd) )((void) (0));
55088 if( rc==SQLITE_OK0
55089 && pPager->dbHintSize<pPager->dbSize
55090 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
55091 ){
55092 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
55093 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT5, &szFile);
55094 pPager->dbHintSize = pPager->dbSize;
55095 }
55096
55097 while( rc==SQLITE_OK0 && pList ){
55098 Pgno pgno = pList->pgno;
55099
55100 /* If there are dirty pages in the page cache with page numbers greater
55101 ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
55102 ** make the file smaller (presumably by auto-vacuum code). Do not write
55103 ** any such pages to the file.
55104 **
55105 ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
55106 ** set (set by sqlite3PagerDontWrite()).
55107 */
55108 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE0x010) ){
55109 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
55110 char *pData; /* Data to write */
55111
55112 assert( (pList->flags&PGHDR_NEED_SYNC)==0 )((void) (0));
55113 if( pList->pgno==1 ) pager_write_changecounter(pList);
55114
55115 /* Encode the database */
55116 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData)pData=(char*)pList->pData;
55117
55118 /* Write out the page data. */
55119 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
55120
55121 /* If page 1 was just written, update Pager.dbFileVers to match
55122 ** the value now stored in the database file. If writing this
55123 ** page caused the database file to grow, update dbFileSize.
55124 */
55125 if( pgno==1 ){
55126 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
55127 }
55128 if( pgno>pPager->dbFileSize ){
55129 pPager->dbFileSize = pgno;
55130 }
55131 pPager->aStat[PAGER_STAT_WRITE2]++;
55132
55133 /* Update any backup objects copying the contents of this pager. */
55134 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
55135
55136 PAGERTRACE(("STORE %d page %d hash(%08x)\n",
55137 PAGERID(pPager), pgno, pager_pagehash(pList)));
55138 IOTRACE(("PGOUT %p %d\n", pPager, pgno));
55139 PAGER_INCR(sqlite3_pager_writedb_count);
55140 }else{
55141 PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
55142 }
55143 pager_set_pagehash(pList);
55144 pList = pList->pDirty;
55145 }
55146
55147 return rc;
55148}
55149
55150/*
55151** Ensure that the sub-journal file is open. If it is already open, this
55152** function is a no-op.
55153**
55154** SQLITE_OK is returned if everything goes according to plan. An
55155** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
55156** fails.
55157*/
55158static int openSubJournal(Pager *pPager){
55159 int rc = SQLITE_OK0;
55160 if( !isOpen(pPager->sjfd)((pPager->sjfd)->pMethods!=0) ){
55161 const int flags = SQLITE_OPEN_SUBJOURNAL0x00002000 | SQLITE_OPEN_READWRITE0x00000002
55162 | SQLITE_OPEN_CREATE0x00000004 | SQLITE_OPEN_EXCLUSIVE0x00000010
55163 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
55164 int nStmtSpill = sqlite3Config.nStmtSpill;
55165 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY4 || pPager->subjInMemory ){
55166 nStmtSpill = -1;
55167 }
55168 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
55169 }
55170 return rc;
55171}
55172
55173/*
55174** Append a record of the current state of page pPg to the sub-journal.
55175**
55176** If successful, set the bit corresponding to pPg->pgno in the bitvecs
55177** for all open savepoints before returning.
55178**
55179** This function returns SQLITE_OK if everything is successful, an IO
55180** error code if the attempt to write to the sub-journal fails, or
55181** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
55182** bitvec.
55183*/
55184static int subjournalPage(PgHdr *pPg){
55185 int rc = SQLITE_OK0;
55186 Pager *pPager = pPg->pPager;
55187 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
55188
55189 /* Open the sub-journal, if it has not already been opened */
55190 assert( pPager->useJournal )((void) (0));
55191 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) )((void) (0));
55192 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 )((void) (0));
55193 assert( pagerUseWal(pPager)((void) (0))
55194 || pageInJournal(pPager, pPg)((void) (0))
55195 || pPg->pgno>pPager->dbOrigSize((void) (0))
55196 )((void) (0));
55197 rc = openSubJournal(pPager);
55198
55199 /* If the sub-journal was opened successfully (or was already open),
55200 ** write the journal record into the file. */
55201 if( rc==SQLITE_OK0 ){
55202 void *pData = pPg->pData;
55203 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
55204 char *pData2;
55205
55206#if SQLITE_HAS_CODEC
55207 if( !pPager->subjInMemory ){
55208 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2)pData2=(char*)pData;
55209 }else
55210#endif
55211 pData2 = pData;
55212 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
55213 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
55214 if( rc==SQLITE_OK0 ){
55215 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
55216 }
55217 }
55218 }
55219 if( rc==SQLITE_OK0 ){
55220 pPager->nSubRec++;
55221 assert( pPager->nSavepoint>0 )((void) (0));
55222 rc = addToSavepointBitvecs(pPager, pPg->pgno);
55223 }
55224 return rc;
55225}
55226static int subjournalPageIfRequired(PgHdr *pPg){
55227 if( subjRequiresPage(pPg) ){
55228 return subjournalPage(pPg);
55229 }else{
55230 return SQLITE_OK0;
55231 }
55232}
55233
55234/*
55235** This function is called by the pcache layer when it has reached some
55236** soft memory limit. The first argument is a pointer to a Pager object
55237** (cast as a void*). The pager is always 'purgeable' (not an in-memory
55238** database). The second argument is a reference to a page that is
55239** currently dirty but has no outstanding references. The page
55240** is always associated with the Pager object passed as the first
55241** argument.
55242**
55243** The job of this function is to make pPg clean by writing its contents
55244** out to the database file, if possible. This may involve syncing the
55245** journal file.
55246**
55247** If successful, sqlite3PcacheMakeClean() is called on the page and
55248** SQLITE_OK returned. If an IO error occurs while trying to make the
55249** page clean, the IO error code is returned. If the page cannot be
55250** made clean for some other reason, but no error occurs, then SQLITE_OK
55251** is returned by sqlite3PcacheMakeClean() is not called.
55252*/
55253static int pagerStress(void *p, PgHdr *pPg){
55254 Pager *pPager = (Pager *)p;
55255 int rc = SQLITE_OK0;
55256
55257 assert( pPg->pPager==pPager )((void) (0));
55258 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
55259
55260 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
55261 ** journal (and adding a new header) is not allowed. This occurs
55262 ** during calls to sqlite3PagerWrite() while trying to journal multiple
55263 ** pages belonging to the same sector.
55264 **
55265 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
55266 ** regardless of whether or not a sync is required. This is set during
55267 ** a rollback or by user request, respectively.
55268 **
55269 ** Spilling is also prohibited when in an error state since that could
55270 ** lead to database corruption. In the current implementation it
55271 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
55272 ** while in the error state, hence it is impossible for this routine to
55273 ** be called in the error state. Nevertheless, we include a NEVER()
55274 ** test for the error state as a safeguard against future changes.
55275 */
55276 if( NEVER(pPager->errCode)(pPager->errCode) ) return SQLITE_OK0;
55277 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
55278 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
55279 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
55280 if( pPager->doNotSpill
55281 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK0x02|SPILLFLAG_OFF0x01))!=0
55282 || (pPg->flags & PGHDR_NEED_SYNC0x008)!=0)
55283 ){
55284 return SQLITE_OK0;
55285 }
55286
55287 pPager->aStat[PAGER_STAT_SPILL3]++;
55288 pPg->pDirty = 0;
55289 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
55290 /* Write a single frame for this page to the log. */
55291 rc = subjournalPageIfRequired(pPg);
55292 if( rc==SQLITE_OK0 ){
55293 rc = pagerWalFrames(pPager, pPg, 0, 0);
55294 }
55295 }else{
55296
55297#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
55298 if( pPager->tempFile==0 ){
55299 rc = sqlite3JournalCreate(pPager->jfd);
55300 if( rc!=SQLITE_OK0 ) return pager_error(pPager, rc);
55301 }
55302#endif
55303
55304 /* Sync the journal file if required. */
55305 if( pPg->flags&PGHDR_NEED_SYNC0x008
55306 || pPager->eState==PAGER_WRITER_CACHEMOD3
55307 ){
55308 rc = syncJournal(pPager, 1);
55309 }
55310
55311 /* Write the contents of the page out to the database file. */
55312 if( rc==SQLITE_OK0 ){
55313 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 )((void) (0));
55314 rc = pager_write_pagelist(pPager, pPg);
55315 }
55316 }
55317
55318 /* Mark the page as clean. */
55319 if( rc==SQLITE_OK0 ){
55320 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
55321 sqlite3PcacheMakeClean(pPg);
55322 }
55323
55324 return pager_error(pPager, rc);
55325}
55326
55327/*
55328** Flush all unreferenced dirty pages to disk.
55329*/
55330SQLITE_PRIVATEstatic int sqlite3PagerFlush(Pager *pPager){
55331 int rc = pPager->errCode;
55332 if( !MEMDBpPager->memDb ){
55333 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
55334 assert( assert_pager_state(pPager) )((void) (0));
55335 while( rc==SQLITE_OK0 && pList ){
55336 PgHdr *pNext = pList->pDirty;
55337 if( pList->nRef==0 ){
55338 rc = pagerStress((void*)pPager, pList);
55339 }
55340 pList = pNext;
55341 }
55342 }
55343
55344 return rc;
55345}
55346
55347/*
55348** Allocate and initialize a new Pager object and put a pointer to it
55349** in *ppPager. The pager should eventually be freed by passing it
55350** to sqlite3PagerClose().
55351**
55352** The zFilename argument is the path to the database file to open.
55353** If zFilename is NULL then a randomly-named temporary file is created
55354** and used as the file to be cached. Temporary files are be deleted
55355** automatically when they are closed. If zFilename is ":memory:" then
55356** all information is held in cache. It is never written to disk.
55357** This can be used to implement an in-memory database.
55358**
55359** The nExtra parameter specifies the number of bytes of space allocated
55360** along with each page reference. This space is available to the user
55361** via the sqlite3PagerGetExtra() API. When a new page is allocated, the
55362** first 8 bytes of this space are zeroed but the remainder is uninitialized.
55363** (The extra space is used by btree as the MemPage object.)
55364**
55365** The flags argument is used to specify properties that affect the
55366** operation of the pager. It should be passed some bitwise combination
55367** of the PAGER_* flags.
55368**
55369** The vfsFlags parameter is a bitmask to pass to the flags parameter
55370** of the xOpen() method of the supplied VFS when opening files.
55371**
55372** If the pager object is allocated and the specified file opened
55373** successfully, SQLITE_OK is returned and *ppPager set to point to
55374** the new pager object. If an error occurs, *ppPager is set to NULL
55375** and error code returned. This function may return SQLITE_NOMEM
55376** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
55377** various SQLITE_IO_XXX errors.
55378*/
55379SQLITE_PRIVATEstatic int sqlite3PagerOpen(
55380 sqlite3_vfs *pVfs, /* The virtual file system to use */
55381 Pager **ppPager, /* OUT: Return the Pager structure here */
55382 const char *zFilename, /* Name of the database file to open */
55383 int nExtra, /* Extra bytes append to each in-memory page */
55384 int flags, /* flags controlling this file */
55385 int vfsFlags, /* flags passed through to sqlite3_vfs.xOpen() */
55386 void (*xReinit)(DbPage*) /* Function to reinitialize pages */
55387){
55388 u8 *pPtr;
55389 Pager *pPager = 0; /* Pager object to allocate and return */
55390 int rc = SQLITE_OK0; /* Return code */
55391 int tempFile = 0; /* True for temp files (incl. in-memory files) */
55392 int memDb = 0; /* True if this is an in-memory file */
55393#ifdef SQLITE_ENABLE_DESERIALIZE
55394 int memJM0 = 0; /* Memory journal mode */
55395#else
55396# define memJM0 0
55397#endif
55398 int readOnly = 0; /* True if this is a read-only file */
55399 int journalFileSize; /* Bytes to allocate for each journal fd */
55400 char *zPathname = 0; /* Full path to database file */
55401 int nPathname = 0; /* Number of bytes in zPathname */
55402 int useJournal = (flags & PAGER_OMIT_JOURNAL0x0001)==0; /* False to omit journal */
55403 int pcacheSize = sqlite3PcacheSize(); /* Bytes to allocate for PCache */
55404 u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE4096; /* Default page size */
55405 const char *zUri = 0; /* URI args to copy */
55406 int nUri = 0; /* Number of bytes of URI args at *zUri */
55407
55408 /* Figure out how much space is required for each journal file-handle
55409 ** (there are two of them, the main journal and the sub-journal). */
55410 journalFileSize = ROUND8(sqlite3JournalSize(pVfs))(((sqlite3JournalSize(pVfs))+7)&~7);
55411
55412 /* Set the output variable to NULL in case an error occurs. */
55413 *ppPager = 0;
55414
55415#ifndef SQLITE_OMIT_MEMORYDB
55416 if( flags & PAGER_MEMORY0x0002 ){
55417 memDb = 1;
55418 if( zFilename && zFilename[0] ){
55419 zPathname = sqlite3DbStrDup(0, zFilename);
55420 if( zPathname==0 ) return SQLITE_NOMEM_BKPT7;
55421 nPathname = sqlite3Strlen30(zPathname);
55422 zFilename = 0;
55423 }
55424 }
55425#endif
55426
55427 /* Compute and store the full pathname in an allocated buffer pointed
55428 ** to by zPathname, length nPathname. Or, if this is a temporary file,
55429 ** leave both nPathname and zPathname set to 0.
55430 */
55431 if( zFilename && zFilename[0] ){
55432 const char *z;
55433 nPathname = pVfs->mxPathname+1;
55434 zPathname = sqlite3DbMallocRaw(0, nPathname*2);
55435 if( zPathname==0 ){
55436 return SQLITE_NOMEM_BKPT7;
55437 }
55438 zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
55439 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
55440 nPathname = sqlite3Strlen30(zPathname);
55441 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
55442 while( *z ){
55443 z += sqlite3Strlen30(z)+1;
55444 z += sqlite3Strlen30(z)+1;
55445 }
55446 nUri = (int)(&z[1] - zUri);
55447 assert( nUri>=0 )((void) (0));
55448 if( rc==SQLITE_OK0 && nPathname+8>pVfs->mxPathname ){
55449 /* This branch is taken when the journal path required by
55450 ** the database being opened will be more than pVfs->mxPathname
55451 ** bytes in length. This means the database cannot be opened,
55452 ** as it will not be possible to open the journal file or even
55453 ** check for a hot-journal before reading.
55454 */
55455 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(55455);
55456 }
55457 if( rc!=SQLITE_OK0 ){
55458 sqlite3DbFree(0, zPathname);
55459 return rc;
55460 }
55461 }
55462
55463 /* Allocate memory for the Pager structure, PCache object, the
55464 ** three file descriptors, the database file name and the journal
55465 ** file name. The layout in memory is as follows:
55466 **
55467 ** Pager object (sizeof(Pager) bytes)
55468 ** PCache object (sqlite3PcacheSize() bytes)
55469 ** Database file handle (pVfs->szOsFile bytes)
55470 ** Sub-journal file handle (journalFileSize bytes)
55471 ** Main journal file handle (journalFileSize bytes)
55472 ** Database file name (nPathname+1 bytes)
55473 ** Journal file name (nPathname+8+1 bytes)
55474 */
55475 pPtr = (u8 *)sqlite3MallocZero(
55476 ROUND8(sizeof(*pPager))(((sizeof(*pPager))+7)&~7) + /* Pager structure */
55477 ROUND8(pcacheSize)(((pcacheSize)+7)&~7) + /* PCache object */
55478 ROUND8(pVfs->szOsFile)(((pVfs->szOsFile)+7)&~7) + /* The main db file */
55479 journalFileSize * 2 + /* The two journal files */
55480 nPathname + 1 + nUri + /* zFilename */
55481 nPathname + 8 + 2 /* zJournal */
55482#ifndef SQLITE_OMIT_WAL
55483 + nPathname + 4 + 2 /* zWal */
55484#endif
55485 );
55486 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) )((void) (0));
55487 if( !pPtr ){
55488 sqlite3DbFree(0, zPathname);
55489 return SQLITE_NOMEM_BKPT7;
55490 }
55491 pPager = (Pager*)(pPtr);
55492 pPager->pPCache = (PCache*)(pPtr += ROUND8(sizeof(*pPager))(((sizeof(*pPager))+7)&~7));
55493 pPager->fd = (sqlite3_file*)(pPtr += ROUND8(pcacheSize)(((pcacheSize)+7)&~7));
55494 pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile)(((pVfs->szOsFile)+7)&~7));
55495 pPager->jfd = (sqlite3_file*)(pPtr += journalFileSize);
55496 pPager->zFilename = (char*)(pPtr += journalFileSize);
55497 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) )((void) (0));
55498
55499 /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
55500 if( zPathname ){
55501 assert( nPathname>0 )((void) (0));
55502 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
55503 memcpy(pPager->zFilename, zPathname, nPathname);
55504 if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
55505 memcpy(pPager->zJournal, zPathname, nPathname);
55506 memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
55507 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
55508#ifndef SQLITE_OMIT_WAL
55509 pPager->zWal = &pPager->zJournal[nPathname+8+1];
55510 memcpy(pPager->zWal, zPathname, nPathname);
55511 memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
55512 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
55513#endif
55514 sqlite3DbFree(0, zPathname);
55515 }
55516 pPager->pVfs = pVfs;
55517 pPager->vfsFlags = vfsFlags;
55518
55519 /* Open the pager file.
55520 */
55521 if( zFilename && zFilename[0] ){
55522 int fout = 0; /* VFS flags returned by xOpen() */
55523 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
55524 assert( !memDb )((void) (0));
55525#ifdef SQLITE_ENABLE_DESERIALIZE
55526 memJM0 = (fout&SQLITE_OPEN_MEMORY0x00000080)!=0;
55527#endif
55528 readOnly = (fout&SQLITE_OPEN_READONLY0x00000001)!=0;
55529
55530 /* If the file was successfully opened for read/write access,
55531 ** choose a default page size in case we have to create the
55532 ** database file. The default page size is the maximum of:
55533 **
55534 ** + SQLITE_DEFAULT_PAGE_SIZE,
55535 ** + The value returned by sqlite3OsSectorSize()
55536 ** + The largest page size that can be written atomically.
55537 */
55538 if( rc==SQLITE_OK0 ){
55539 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
55540 if( !readOnly ){
55541 setSectorSize(pPager);
55542 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE)((void) (0));
55543 if( szPageDflt<pPager->sectorSize ){
55544 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE8192 ){
55545 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE8192;
55546 }else{
55547 szPageDflt = (u32)pPager->sectorSize;
55548 }
55549 }
55550#ifdef SQLITE_ENABLE_ATOMIC_WRITE
55551 {
55552 int ii;
55553 assert(SQLITE_IOCAP_ATOMIC512==(512>>8))((void) (0));
55554 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8))((void) (0));
55555 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536)((void) (0));
55556 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE8192; ii=ii*2){
55557 if( iDc&(SQLITE_IOCAP_ATOMIC0x00000001|(ii>>8)) ){
55558 szPageDflt = ii;
55559 }
55560 }
55561 }
55562#endif
55563 }
55564 pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
55565 if( (iDc & SQLITE_IOCAP_IMMUTABLE0x00002000)!=0
55566 || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
55567 vfsFlags |= SQLITE_OPEN_READONLY0x00000001;
55568 goto act_like_temp_file;
55569 }
55570 }
55571 }else{
55572 /* If a temporary file is requested, it is not opened immediately.
55573 ** In this case we accept the default page size and delay actually
55574 ** opening the file until the first call to OsWrite().
55575 **
55576 ** This branch is also run for an in-memory database. An in-memory
55577 ** database is the same as a temp-file that is never written out to
55578 ** disk and uses an in-memory rollback journal.
55579 **
55580 ** This branch also runs for files marked as immutable.
55581 */
55582act_like_temp_file:
55583 tempFile = 1;
55584 pPager->eState = PAGER_READER1; /* Pretend we already have a lock */
55585 pPager->eLock = EXCLUSIVE_LOCK4; /* Pretend we are in EXCLUSIVE mode */
55586 pPager->noLock = 1; /* Do no locking */
55587 readOnly = (vfsFlags&SQLITE_OPEN_READONLY0x00000001);
55588 }
55589
55590 /* The following call to PagerSetPagesize() serves to set the value of
55591 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
55592 */
55593 if( rc==SQLITE_OK0 ){
55594 assert( pPager->memDb==0 )((void) (0));
55595 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
55596 testcase( rc!=SQLITE_OK );
55597 }
55598
55599 /* Initialize the PCache object. */
55600 if( rc==SQLITE_OK0 ){
55601 nExtra = ROUND8(nExtra)(((nExtra)+7)&~7);
55602 assert( nExtra>=8 && nExtra<1000 )((void) (0));
55603 rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
55604 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
55605 }
55606
55607 /* If an error occurred above, free the Pager structure and close the file.
55608 */
55609 if( rc!=SQLITE_OK0 ){
55610 sqlite3OsClose(pPager->fd);
55611 sqlite3PageFree(pPager->pTmpSpace);
55612 sqlite3_free(pPager);
55613 return rc;
55614 }
55615
55616 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
55617 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
55618
55619 pPager->useJournal = (u8)useJournal;
55620 /* pPager->stmtOpen = 0; */
55621 /* pPager->stmtInUse = 0; */
55622 /* pPager->nRef = 0; */
55623 /* pPager->stmtSize = 0; */
55624 /* pPager->stmtJSize = 0; */
55625 /* pPager->nPage = 0; */
55626 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT1073741823;
55627 /* pPager->state = PAGER_UNLOCK; */
55628 /* pPager->errMask = 0; */
55629 pPager->tempFile = (u8)tempFile;
55630 assert( tempFile==PAGER_LOCKINGMODE_NORMAL((void) (0))
55631 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
55632 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 )((void) (0));
55633 pPager->exclusiveMode = (u8)tempFile;
55634 pPager->changeCountDone = pPager->tempFile;
55635 pPager->memDb = (u8)memDb;
55636 pPager->readOnly = (u8)readOnly;
55637 assert( useJournal || pPager->tempFile )((void) (0));
55638 pPager->noSync = pPager->tempFile;
55639 if( pPager->noSync ){
55640 assert( pPager->fullSync==0 )((void) (0));
55641 assert( pPager->extraSync==0 )((void) (0));
55642 assert( pPager->syncFlags==0 )((void) (0));
55643 assert( pPager->walSyncFlags==0 )((void) (0));
55644 }else{
55645 pPager->fullSync = 1;
55646 pPager->extraSync = 0;
55647 pPager->syncFlags = SQLITE_SYNC_NORMAL0x00002;
55648 pPager->walSyncFlags = SQLITE_SYNC_NORMAL0x00002 | (SQLITE_SYNC_NORMAL0x00002<<2);
55649 }
55650 /* pPager->pFirst = 0; */
55651 /* pPager->pFirstSynced = 0; */
55652 /* pPager->pLast = 0; */
55653 pPager->nExtra = (u16)nExtra;
55654 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT-1;
55655 assert( isOpen(pPager->fd) || tempFile )((void) (0));
55656 setSectorSize(pPager);
55657 if( !useJournal ){
55658 pPager->journalMode = PAGER_JOURNALMODE_OFF2;
55659 }else if( memDb || memJM0 ){
55660 pPager->journalMode = PAGER_JOURNALMODE_MEMORY4;
55661 }
55662 /* pPager->xBusyHandler = 0; */
55663 /* pPager->pBusyHandlerArg = 0; */
55664 pPager->xReiniter = xReinit;
55665 setGetterMethod(pPager);
55666 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
55667 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
55668
55669 *ppPager = pPager;
55670 return SQLITE_OK0;
55671}
55672
55673
55674
55675/*
55676** This function is called after transitioning from PAGER_UNLOCK to
55677** PAGER_SHARED state. It tests if there is a hot journal present in
55678** the file-system for the given pager. A hot journal is one that
55679** needs to be played back. According to this function, a hot-journal
55680** file exists if the following criteria are met:
55681**
55682** * The journal file exists in the file system, and
55683** * No process holds a RESERVED or greater lock on the database file, and
55684** * The database file itself is greater than 0 bytes in size, and
55685** * The first byte of the journal file exists and is not 0x00.
55686**
55687** If the current size of the database file is 0 but a journal file
55688** exists, that is probably an old journal left over from a prior
55689** database with the same name. In this case the journal file is
55690** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
55691** is returned.
55692**
55693** This routine does not check if there is a master journal filename
55694** at the end of the file. If there is, and that master journal file
55695** does not exist, then the journal file is not really hot. In this
55696** case this routine will return a false-positive. The pager_playback()
55697** routine will discover that the journal file is not really hot and
55698** will not roll it back.
55699**
55700** If a hot-journal file is found to exist, *pExists is set to 1 and
55701** SQLITE_OK returned. If no hot-journal file is present, *pExists is
55702** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
55703** to determine whether or not a hot-journal file exists, the IO error
55704** code is returned and the value of *pExists is undefined.
55705*/
55706static int hasHotJournal(Pager *pPager, int *pExists){
55707 sqlite3_vfs * const pVfs = pPager->pVfs;
55708 int rc = SQLITE_OK0; /* Return code */
55709 int exists = 1; /* True if a journal file is present */
55710 int jrnlOpen = !!isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0);
55711
55712 assert( pPager->useJournal )((void) (0));
55713 assert( isOpen(pPager->fd) )((void) (0));
55714 assert( pPager->eState==PAGER_OPEN )((void) (0));
55715
55716 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &((void) (0))
55717 SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN((void) (0))
55718 ))((void) (0));
55719
55720 *pExists = 0;
55721 if( !jrnlOpen ){
55722 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS0, &exists);
55723 }
55724 if( rc==SQLITE_OK0 && exists ){
55725 int locked = 0; /* True if some process holds a RESERVED lock */
55726
55727 /* Race condition here: Another process might have been holding the
55728 ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
55729 ** call above, but then delete the journal and drop the lock before
55730 ** we get to the following sqlite3OsCheckReservedLock() call. If that
55731 ** is the case, this routine might think there is a hot journal when
55732 ** in fact there is none. This results in a false-positive which will
55733 ** be dealt with by the playback routine. Ticket #3883.
55734 */
55735 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
55736 if( rc==SQLITE_OK0 && !locked ){
55737 Pgno nPage; /* Number of pages in database file */
55738
55739 assert( pPager->tempFile==0 )((void) (0));
55740 rc = pagerPagecount(pPager, &nPage);
55741 if( rc==SQLITE_OK0 ){
55742 /* If the database is zero pages in size, that means that either (1) the
55743 ** journal is a remnant from a prior database with the same name where
55744 ** the database file but not the journal was deleted, or (2) the initial
55745 ** transaction that populates a new database is being rolled back.
55746 ** In either case, the journal file can be deleted. However, take care
55747 ** not to delete the journal file if it is already open due to
55748 ** journal_mode=PERSIST.
55749 */
55750 if( nPage==0 && !jrnlOpen ){
55751 sqlite3BeginBenignMalloc();
55752 if( pagerLockDb(pPager, RESERVED_LOCK2)==SQLITE_OK0 ){
55753 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
55754 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK1);
55755 }
55756 sqlite3EndBenignMalloc();
55757 }else{
55758 /* The journal file exists and no other connection has a reserved
55759 ** or greater lock on the database file. Now check that there is
55760 ** at least one non-zero bytes at the start of the journal file.
55761 ** If there is, then we consider this journal to be hot. If not,
55762 ** it can be ignored.
55763 */
55764 if( !jrnlOpen ){
55765 int f = SQLITE_OPEN_READONLY0x00000001|SQLITE_OPEN_MAIN_JOURNAL0x00000800;
55766 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
55767 }
55768 if( rc==SQLITE_OK0 ){
55769 u8 first = 0;
55770 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
55771 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
55772 rc = SQLITE_OK0;
55773 }
55774 if( !jrnlOpen ){
55775 sqlite3OsClose(pPager->jfd);
55776 }
55777 *pExists = (first!=0);
55778 }else if( rc==SQLITE_CANTOPEN14 ){
55779 /* If we cannot open the rollback journal file in order to see if
55780 ** it has a zero header, that might be due to an I/O error, or
55781 ** it might be due to the race condition described above and in
55782 ** ticket #3883. Either way, assume that the journal is hot.
55783 ** This might be a false positive. But if it is, then the
55784 ** automatic journal playback and recovery mechanism will deal
55785 ** with it under an EXCLUSIVE lock where we do not need to
55786 ** worry so much with race conditions.
55787 */
55788 *pExists = 1;
55789 rc = SQLITE_OK0;
55790 }
55791 }
55792 }
55793 }
55794 }
55795
55796 return rc;
55797}
55798
55799/*
55800** This function is called to obtain a shared lock on the database file.
55801** It is illegal to call sqlite3PagerGet() until after this function
55802** has been successfully called. If a shared-lock is already held when
55803** this function is called, it is a no-op.
55804**
55805** The following operations are also performed by this function.
55806**
55807** 1) If the pager is currently in PAGER_OPEN state (no lock held
55808** on the database file), then an attempt is made to obtain a
55809** SHARED lock on the database file. Immediately after obtaining
55810** the SHARED lock, the file-system is checked for a hot-journal,
55811** which is played back if present. Following any hot-journal
55812** rollback, the contents of the cache are validated by checking
55813** the 'change-counter' field of the database file header and
55814** discarded if they are found to be invalid.
55815**
55816** 2) If the pager is running in exclusive-mode, and there are currently
55817** no outstanding references to any pages, and is in the error state,
55818** then an attempt is made to clear the error state by discarding
55819** the contents of the page cache and rolling back any open journal
55820** file.
55821**
55822** If everything is successful, SQLITE_OK is returned. If an IO error
55823** occurs while locking the database, checking for a hot-journal file or
55824** rolling back a journal file, the IO error code is returned.
55825*/
55826SQLITE_PRIVATEstatic int sqlite3PagerSharedLock(Pager *pPager){
55827 int rc = SQLITE_OK0; /* Return code */
55828
55829 /* This routine is only called from b-tree and only when there are no
55830 ** outstanding pages. This implies that the pager state should either
55831 ** be OPEN or READER. READER is only possible if the pager is or was in
55832 ** exclusive access mode. */
55833 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 )((void) (0));
55834 assert( assert_pager_state(pPager) )((void) (0));
55835 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER )((void) (0));
55836 assert( pPager->errCode==SQLITE_OK )((void) (0));
55837
55838 if( !pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->eState==PAGER_OPEN0 ){
55839 int bHotJournal = 1; /* True if there exists a hot journal-file */
55840
55841 assert( !MEMDB )((void) (0));
55842 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
55843
55844 rc = pager_wait_on_lock(pPager, SHARED_LOCK1);
55845 if( rc!=SQLITE_OK0 ){
55846 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK )((void) (0));
55847 goto failed;
55848 }
55849
55850 /* If a journal file exists, and there is no RESERVED lock on the
55851 ** database file, then it either needs to be played back or deleted.
55852 */
55853 if( pPager->eLock<=SHARED_LOCK1 ){
55854 rc = hasHotJournal(pPager, &bHotJournal);
55855 }
55856 if( rc!=SQLITE_OK0 ){
55857 goto failed;
55858 }
55859 if( bHotJournal ){
55860 if( pPager->readOnly ){
55861 rc = SQLITE_READONLY_ROLLBACK(8 | (3<<8));
55862 goto failed;
55863 }
55864
55865 /* Get an EXCLUSIVE lock on the database file. At this point it is
55866 ** important that a RESERVED lock is not obtained on the way to the
55867 ** EXCLUSIVE lock. If it were, another process might open the
55868 ** database file, detect the RESERVED lock, and conclude that the
55869 ** database is safe to read while this process is still rolling the
55870 ** hot-journal back.
55871 **
55872 ** Because the intermediate RESERVED lock is not requested, any
55873 ** other process attempting to access the database file will get to
55874 ** this point in the code and fail to obtain its own EXCLUSIVE lock
55875 ** on the database file.
55876 **
55877 ** Unless the pager is in locking_mode=exclusive mode, the lock is
55878 ** downgraded to SHARED_LOCK before this function returns.
55879 */
55880 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
55881 if( rc!=SQLITE_OK0 ){
55882 goto failed;
55883 }
55884
55885 /* If it is not already open and the file exists on disk, open the
55886 ** journal for read/write access. Write access is required because
55887 ** in exclusive-access mode the file descriptor will be kept open
55888 ** and possibly used for a transaction later on. Also, write-access
55889 ** is usually required to finalize the journal in journal_mode=persist
55890 ** mode (and also for journal_mode=truncate on some systems).
55891 **
55892 ** If the journal does not exist, it usually means that some
55893 ** other connection managed to get in and roll it back before
55894 ** this connection obtained the exclusive lock above. Or, it
55895 ** may mean that the pager was in the error-state when this
55896 ** function was called and the journal file does not exist.
55897 */
55898 if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
55899 sqlite3_vfs * const pVfs = pPager->pVfs;
55900 int bExists; /* True if journal file exists */
55901 rc = sqlite3OsAccess(
55902 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS0, &bExists);
55903 if( rc==SQLITE_OK0 && bExists ){
55904 int fout = 0;
55905 int f = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_MAIN_JOURNAL0x00000800;
55906 assert( !pPager->tempFile )((void) (0));
55907 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
55908 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) )((void) (0));
55909 if( rc==SQLITE_OK0 && fout&SQLITE_OPEN_READONLY0x00000001 ){
55910 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(55910);
55911 sqlite3OsClose(pPager->jfd);
55912 }
55913 }
55914 }
55915
55916 /* Playback and delete the journal. Drop the database write
55917 ** lock and reacquire the read lock. Purge the cache before
55918 ** playing back the hot-journal so that we don't end up with
55919 ** an inconsistent cache. Sync the hot journal before playing
55920 ** it back since the process that crashed and left the hot journal
55921 ** probably did not sync it and we are required to always sync
55922 ** the journal before playing it back.
55923 */
55924 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
55925 assert( rc==SQLITE_OK )((void) (0));
55926 rc = pagerSyncHotJournal(pPager);
55927 if( rc==SQLITE_OK0 ){
55928 rc = pager_playback(pPager, !pPager->tempFile);
55929 pPager->eState = PAGER_OPEN0;
55930 }
55931 }else if( !pPager->exclusiveMode ){
55932 pagerUnlockDb(pPager, SHARED_LOCK1);
55933 }
55934
55935 if( rc!=SQLITE_OK0 ){
55936 /* This branch is taken if an error occurs while trying to open
55937 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
55938 ** pager_unlock() routine will be called before returning to unlock
55939 ** the file. If the unlock attempt fails, then Pager.eLock must be
55940 ** set to UNKNOWN_LOCK (see the comment above the #define for
55941 ** UNKNOWN_LOCK above for an explanation).
55942 **
55943 ** In order to get pager_unlock() to do this, set Pager.eState to
55944 ** PAGER_ERROR now. This is not actually counted as a transition
55945 ** to ERROR state in the state diagram at the top of this file,
55946 ** since we know that the same call to pager_unlock() will very
55947 ** shortly transition the pager object to the OPEN state. Calling
55948 ** assert_pager_state() would fail now, as it should not be possible
55949 ** to be in ERROR state when there are zero outstanding page
55950 ** references.
55951 */
55952 pager_error(pPager, rc);
55953 goto failed;
55954 }
55955
55956 assert( pPager->eState==PAGER_OPEN )((void) (0));
55957 assert( (pPager->eLock==SHARED_LOCK)((void) (0))
55958 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)((void) (0))
55959 )((void) (0));
55960 }
55961
55962 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
55963 /* The shared-lock has just been acquired then check to
55964 ** see if the database has been modified. If the database has changed,
55965 ** flush the cache. The hasHeldSharedLock flag prevents this from
55966 ** occurring on the very first access to a file, in order to save a
55967 ** single unnecessary sqlite3OsRead() call at the start-up.
55968 **
55969 ** Database changes are detected by looking at 15 bytes beginning
55970 ** at offset 24 into the file. The first 4 of these 16 bytes are
55971 ** a 32-bit counter that is incremented with each change. The
55972 ** other bytes change randomly with each file change when
55973 ** a codec is in use.
55974 **
55975 ** There is a vanishingly small chance that a change will not be
55976 ** detected. The chance of an undetected change is so small that
55977 ** it can be neglected.
55978 */
55979 char dbFileVers[sizeof(pPager->dbFileVers)];
55980
55981 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
55982 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
55983 if( rc!=SQLITE_OK0 ){
55984 if( rc!=SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ){
55985 goto failed;
55986 }
55987 memset(dbFileVers, 0, sizeof(dbFileVers));
55988 }
55989
55990 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
55991 pager_reset(pPager);
55992
55993 /* Unmap the database file. It is possible that external processes
55994 ** may have truncated the database file and then extended it back
55995 ** to its original size while this process was not holding a lock.
55996 ** In this case there may exist a Pager.pMap mapping that appears
55997 ** to be the right size but is not actually valid. Avoid this
55998 ** possibility by unmapping the db here. */
55999 if( USEFETCH(pPager)((pPager)->bUseFetch) ){
56000 sqlite3OsUnfetch(pPager->fd, 0, 0);
56001 }
56002 }
56003 }
56004
56005 /* If there is a WAL file in the file-system, open this database in WAL
56006 ** mode. Otherwise, the following function call is a no-op.
56007 */
56008 rc = pagerOpenWalIfPresent(pPager);
56009#ifndef SQLITE_OMIT_WAL
56010 assert( pPager->pWal==0 || rc==SQLITE_OK )((void) (0));
56011#endif
56012 }
56013
56014 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
56015 assert( rc==SQLITE_OK )((void) (0));
56016 rc = pagerBeginReadTransaction(pPager);
56017 }
56018
56019 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN0 && rc==SQLITE_OK0 ){
56020 rc = pagerPagecount(pPager, &pPager->dbSize);
56021 }
56022
56023 failed:
56024 if( rc!=SQLITE_OK0 ){
56025 assert( !MEMDB )((void) (0));
56026 pager_unlock(pPager);
56027 assert( pPager->eState==PAGER_OPEN )((void) (0));
56028 }else{
56029 pPager->eState = PAGER_READER1;
56030 pPager->hasHeldSharedLock = 1;
56031 }
56032 return rc;
56033}
56034
56035/*
56036** If the reference count has reached zero, rollback any active
56037** transaction and unlock the pager.
56038**
56039** Except, in locking_mode=EXCLUSIVE when there is nothing to in
56040** the rollback journal, the unlock is not performed and there is
56041** nothing to rollback, so this routine is a no-op.
56042*/
56043static void pagerUnlockIfUnused(Pager *pPager){
56044 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
56045 assert( pPager->nMmapOut==0 )((void) (0)); /* because page1 is never memory mapped */
56046 pagerUnlockAndRollback(pPager);
56047 }
56048}
56049
56050/*
56051** The page getter methods each try to acquire a reference to a
56052** page with page number pgno. If the requested reference is
56053** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
56054**
56055** There are different implementations of the getter method depending
56056** on the current state of the pager.
56057**
56058** getPageNormal() -- The normal getter
56059** getPageError() -- Used if the pager is in an error state
56060** getPageMmap() -- Used if memory-mapped I/O is enabled
56061**
56062** If the requested page is already in the cache, it is returned.
56063** Otherwise, a new page object is allocated and populated with data
56064** read from the database file. In some cases, the pcache module may
56065** choose not to allocate a new page object and may reuse an existing
56066** object with no outstanding references.
56067**
56068** The extra data appended to a page is always initialized to zeros the
56069** first time a page is loaded into memory. If the page requested is
56070** already in the cache when this function is called, then the extra
56071** data is left as it was when the page object was last used.
56072**
56073** If the database image is smaller than the requested page or if
56074** the flags parameter contains the PAGER_GET_NOCONTENT bit and the
56075** requested page is not already stored in the cache, then no
56076** actual disk read occurs. In this case the memory image of the
56077** page is initialized to all zeros.
56078**
56079** If PAGER_GET_NOCONTENT is true, it means that we do not care about
56080** the contents of the page. This occurs in two scenarios:
56081**
56082** a) When reading a free-list leaf page from the database, and
56083**
56084** b) When a savepoint is being rolled back and we need to load
56085** a new page into the cache to be filled with the data read
56086** from the savepoint journal.
56087**
56088** If PAGER_GET_NOCONTENT is true, then the data returned is zeroed instead
56089** of being read from the database. Additionally, the bits corresponding
56090** to pgno in Pager.pInJournal (bitvec of pages already written to the
56091** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
56092** savepoints are set. This means if the page is made writable at any
56093** point in the future, using a call to sqlite3PagerWrite(), its contents
56094** will not be journaled. This saves IO.
56095**
56096** The acquisition might fail for several reasons. In all cases,
56097** an appropriate error code is returned and *ppPage is set to NULL.
56098**
56099** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
56100** to find a page in the in-memory cache first. If the page is not already
56101** in memory, this routine goes to disk to read it in whereas Lookup()
56102** just returns 0. This routine acquires a read-lock the first time it
56103** has to go to disk, and could also playback an old journal if necessary.
56104** Since Lookup() never goes to disk, it never has to deal with locks
56105** or journal files.
56106*/
56107static int getPageNormal(
56108 Pager *pPager, /* The pager open on the database file */
56109 Pgno pgno, /* Page number to fetch */
56110 DbPage **ppPage, /* Write a pointer to the page here */
56111 int flags /* PAGER_GET_XXX flags */
56112){
56113 int rc = SQLITE_OK0;
56114 PgHdr *pPg;
56115 u8 noContent; /* True if PAGER_GET_NOCONTENT is set */
56116 sqlite3_pcache_page *pBase;
56117
56118 assert( pPager->errCode==SQLITE_OK )((void) (0));
56119 assert( pPager->eState>=PAGER_READER )((void) (0));
56120 assert( assert_pager_state(pPager) )((void) (0));
56121 assert( pPager->hasHeldSharedLock==1 )((void) (0));
56122
56123 if( pgno==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(56123);
56124 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
56125 if( pBase==0 ){
56126 pPg = 0;
56127 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
56128 if( rc!=SQLITE_OK0 ) goto pager_acquire_err;
56129 if( pBase==0 ){
56130 rc = SQLITE_NOMEM_BKPT7;
56131 goto pager_acquire_err;
56132 }
56133 }
56134 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
56135 assert( pPg==(*ppPage) )((void) (0));
56136 assert( pPg->pgno==pgno )((void) (0));
56137 assert( pPg->pPager==pPager || pPg->pPager==0 )((void) (0));
56138
56139 noContent = (flags & PAGER_GET_NOCONTENT0x01)!=0;
56140 if( pPg->pPager && !noContent ){
56141 /* In this case the pcache already contains an initialized copy of
56142 ** the page. Return without further ado. */
56143 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) )((void) (0));
56144 pPager->aStat[PAGER_STAT_HIT0]++;
56145 return SQLITE_OK0;
56146
56147 }else{
56148 /* The pager cache has created a new page. Its content needs to
56149 ** be initialized. But first some error checks:
56150 **
56151 ** (1) The maximum page number is 2^31
56152 ** (2) Never try to fetch the locking page
56153 */
56154 if( pgno>PAGER_MAX_PGNO2147483647 || pgno==PAGER_MJ_PGNO(pPager)((Pgno)((sqlite3PendingByte/((pPager)->pageSize))+1)) ){
56155 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(56155);
56156 goto pager_acquire_err;
56157 }
56158
56159 pPg->pPager = pPager;
56160
56161 assert( !isOpen(pPager->fd) || !MEMDB )((void) (0));
56162 if( !isOpen(pPager->fd)((pPager->fd)->pMethods!=0) || pPager->dbSize<pgno || noContent ){
56163 if( pgno>pPager->mxPgno ){
56164 rc = SQLITE_FULL13;
56165 goto pager_acquire_err;
56166 }
56167 if( noContent ){
56168 /* Failure to set the bits in the InJournal bit-vectors is benign.
56169 ** It merely means that we might do some extra work to journal a
56170 ** page that does not need to be journaled. Nevertheless, be sure
56171 ** to test the case where a malloc error occurs while trying to set
56172 ** a bit in a bit vector.
56173 */
56174 sqlite3BeginBenignMalloc();
56175 if( pgno<=pPager->dbOrigSize ){
56176 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
56177 testcase( rc==SQLITE_NOMEM );
56178 }
56179 TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
56180 testcase( rc==SQLITE_NOMEM );
56181 sqlite3EndBenignMalloc();
56182 }
56183 memset(pPg->pData, 0, pPager->pageSize);
56184 IOTRACE(("ZERO %p %d\n", pPager, pgno));
56185 }else{
56186 assert( pPg->pPager==pPager )((void) (0));
56187 pPager->aStat[PAGER_STAT_MISS1]++;
56188 rc = readDbPage(pPg);
56189 if( rc!=SQLITE_OK0 ){
56190 goto pager_acquire_err;
56191 }
56192 }
56193 pager_set_pagehash(pPg);
56194 }
56195 return SQLITE_OK0;
56196
56197pager_acquire_err:
56198 assert( rc!=SQLITE_OK )((void) (0));
56199 if( pPg ){
56200 sqlite3PcacheDrop(pPg);
56201 }
56202 pagerUnlockIfUnused(pPager);
56203 *ppPage = 0;
56204 return rc;
56205}
56206
56207#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
56208/* The page getter for when memory-mapped I/O is enabled */
56209static int getPageMMap(
56210 Pager *pPager, /* The pager open on the database file */
56211 Pgno pgno, /* Page number to fetch */
56212 DbPage **ppPage, /* Write a pointer to the page here */
56213 int flags /* PAGER_GET_XXX flags */
56214){
56215 int rc = SQLITE_OK0;
56216 PgHdr *pPg = 0;
56217 u32 iFrame = 0; /* Frame to read from WAL file */
56218
56219 /* It is acceptable to use a read-only (mmap) page for any page except
56220 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
56221 ** flag was specified by the caller. And so long as the db is not a
56222 ** temporary or in-memory database. */
56223 const int bMmapOk = (pgno>1
56224 && (pPager->eState==PAGER_READER1 || (flags & PAGER_GET_READONLY0x02))
56225 );
56226
56227 assert( USEFETCH(pPager) )((void) (0));
56228#ifdef SQLITE_HAS_CODEC
56229 assert( pPager->xCodec==0 )((void) (0));
56230#endif
56231
56232 /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here
56233 ** allows the compiler optimizer to reuse the results of the "pgno>1"
56234 ** test in the previous statement, and avoid testing pgno==0 in the
56235 ** common case where pgno is large. */
56236 if( pgno<=1 && pgno==0 ){
56237 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(56237);
56238 }
56239 assert( pPager->eState>=PAGER_READER )((void) (0));
56240 assert( assert_pager_state(pPager) )((void) (0));
56241 assert( pPager->hasHeldSharedLock==1 )((void) (0));
56242 assert( pPager->errCode==SQLITE_OK )((void) (0));
56243
56244 if( bMmapOk && pagerUseWal(pPager)((pPager)->pWal!=0) ){
56245 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
56246 if( rc!=SQLITE_OK0 ){
56247 *ppPage = 0;
56248 return rc;
56249 }
56250 }
56251 if( bMmapOk && iFrame==0 ){
56252 void *pData = 0;
56253 rc = sqlite3OsFetch(pPager->fd,
56254 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
56255 );
56256 if( rc==SQLITE_OK0 && pData ){
56257 if( pPager->eState>PAGER_READER1 || pPager->tempFile ){
56258 pPg = sqlite3PagerLookup(pPager, pgno);
56259 }
56260 if( pPg==0 ){
56261 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
56262 }else{
56263 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
56264 }
56265 if( pPg ){
56266 assert( rc==SQLITE_OK )((void) (0));
56267 *ppPage = pPg;
56268 return SQLITE_OK0;
56269 }
56270 }
56271 if( rc!=SQLITE_OK0 ){
56272 *ppPage = 0;
56273 return rc;
56274 }
56275 }
56276 return getPageNormal(pPager, pgno, ppPage, flags);
56277}
56278#endif /* SQLITE_MAX_MMAP_SIZE>0 */
56279
56280/* The page getter method for when the pager is an error state */
56281static int getPageError(
56282 Pager *pPager, /* The pager open on the database file */
56283 Pgno pgno, /* Page number to fetch */
56284 DbPage **ppPage, /* Write a pointer to the page here */
56285 int flags /* PAGER_GET_XXX flags */
56286){
56287 UNUSED_PARAMETER(pgno)(void)(pgno);
56288 UNUSED_PARAMETER(flags)(void)(flags);
56289 assert( pPager->errCode!=SQLITE_OK )((void) (0));
56290 *ppPage = 0;
56291 return pPager->errCode;
56292}
56293
56294
56295/* Dispatch all page fetch requests to the appropriate getter method.
56296*/
56297SQLITE_PRIVATEstatic int sqlite3PagerGet(
56298 Pager *pPager, /* The pager open on the database file */
56299 Pgno pgno, /* Page number to fetch */
56300 DbPage **ppPage, /* Write a pointer to the page here */
56301 int flags /* PAGER_GET_XXX flags */
56302){
56303 return pPager->xGet(pPager, pgno, ppPage, flags);
56304}
56305
56306/*
56307** Acquire a page if it is already in the in-memory cache. Do
56308** not read the page from disk. Return a pointer to the page,
56309** or 0 if the page is not in cache.
56310**
56311** See also sqlite3PagerGet(). The difference between this routine
56312** and sqlite3PagerGet() is that _get() will go to the disk and read
56313** in the page if the page is not already in cache. This routine
56314** returns NULL if the page is not in cache or if a disk I/O error
56315** has ever happened.
56316*/
56317SQLITE_PRIVATEstatic DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
56318 sqlite3_pcache_page *pPage;
56319 assert( pPager!=0 )((void) (0));
56320 assert( pgno!=0 )((void) (0));
56321 assert( pPager->pPCache!=0 )((void) (0));
56322 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
56323 assert( pPage==0 || pPager->hasHeldSharedLock )((void) (0));
56324 if( pPage==0 ) return 0;
56325 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
56326}
56327
56328/*
56329** Release a page reference.
56330**
56331** The sqlite3PagerUnref() and sqlite3PagerUnrefNotNull() may only be
56332** used if we know that the page being released is not the last page.
56333** The btree layer always holds page1 open until the end, so these first
56334** to routines can be used to release any page other than BtShared.pPage1.
56335**
56336** Use sqlite3PagerUnrefPageOne() to release page1. This latter routine
56337** checks the total number of outstanding pages and if the number of
56338** pages reaches zero it drops the database lock.
56339*/
56340SQLITE_PRIVATEstatic void sqlite3PagerUnrefNotNull(DbPage *pPg){
56341 TESTONLY( Pager *pPager = pPg->pPager; )
56342 assert( pPg!=0 )((void) (0));
56343 if( pPg->flags & PGHDR_MMAP0x020 ){
56344 assert( pPg->pgno!=1 )((void) (0)); /* Page1 is never memory mapped */
56345 pagerReleaseMapPage(pPg);
56346 }else{
56347 sqlite3PcacheRelease(pPg);
56348 }
56349 /* Do not use this routine to release the last reference to page1 */
56350 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 )((void) (0));
56351}
56352SQLITE_PRIVATEstatic void sqlite3PagerUnref(DbPage *pPg){
56353 if( pPg ) sqlite3PagerUnrefNotNull(pPg);
56354}
56355SQLITE_PRIVATEstatic void sqlite3PagerUnrefPageOne(DbPage *pPg){
56356 Pager *pPager;
56357 assert( pPg!=0 )((void) (0));
56358 assert( pPg->pgno==1 )((void) (0));
56359 assert( (pPg->flags & PGHDR_MMAP)==0 )((void) (0)); /* Page1 is never memory mapped */
56360 pPager = pPg->pPager;
56361 sqlite3PagerResetLockTimeout(pPager);
56362 sqlite3PcacheRelease(pPg);
56363 pagerUnlockIfUnused(pPager);
56364}
56365
56366/*
56367** This function is called at the start of every write transaction.
56368** There must already be a RESERVED or EXCLUSIVE lock on the database
56369** file when this routine is called.
56370**
56371** Open the journal file for pager pPager and write a journal header
56372** to the start of it. If there are active savepoints, open the sub-journal
56373** as well. This function is only used when the journal file is being
56374** opened to write a rollback log for a transaction. It is not used
56375** when opening a hot journal file to roll it back.
56376**
56377** If the journal file is already open (as it may be in exclusive mode),
56378** then this function just writes a journal header to the start of the
56379** already open file.
56380**
56381** Whether or not the journal file is opened by this function, the
56382** Pager.pInJournal bitvec structure is allocated.
56383**
56384** Return SQLITE_OK if everything is successful. Otherwise, return
56385** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
56386** an IO error code if opening or writing the journal file fails.
56387*/
56388static int pager_open_journal(Pager *pPager){
56389 int rc = SQLITE_OK0; /* Return code */
56390 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
56391
56392 assert( pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
56393 assert( assert_pager_state(pPager) )((void) (0));
56394 assert( pPager->pInJournal==0 )((void) (0));
56395
56396 /* If already in the error state, this function is a no-op. But on
56397 ** the other hand, this routine is never called if we are already in
56398 ** an error state. */
56399 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
56400
56401 if( !pagerUseWal(pPager)((pPager)->pWal!=0) && pPager->journalMode!=PAGER_JOURNALMODE_OFF2 ){
56402 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
56403 if( pPager->pInJournal==0 ){
56404 return SQLITE_NOMEM_BKPT7;
56405 }
56406
56407 /* Open the journal file if it is not already open. */
56408 if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
56409 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY4 ){
56410 sqlite3MemJournalOpen(pPager->jfd);
56411 }else{
56412 int flags = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004;
56413 int nSpill;
56414
56415 if( pPager->tempFile ){
56416 flags |= (SQLITE_OPEN_DELETEONCLOSE0x00000008|SQLITE_OPEN_TEMP_JOURNAL0x00001000);
56417 nSpill = sqlite3Config.nStmtSpill;
56418 }else{
56419 flags |= SQLITE_OPEN_MAIN_JOURNAL0x00000800;
56420 nSpill = jrnlBufferSize(pPager);
56421 }
56422
56423 /* Verify that the database still has the same name as it did when
56424 ** it was originally opened. */
56425 rc = databaseIsUnmoved(pPager);
56426 if( rc==SQLITE_OK0 ){
56427 rc = sqlite3JournalOpen (
56428 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
56429 );
56430 }
56431 }
56432 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) )((void) (0));
56433 }
56434
56435
56436 /* Write the first journal header to the journal file and open
56437 ** the sub-journal if necessary.
56438 */
56439 if( rc==SQLITE_OK0 ){
56440 /* TODO: Check if all of these are really required. */
56441 pPager->nRec = 0;
56442 pPager->journalOff = 0;
56443 pPager->setMaster = 0;
56444 pPager->journalHdr = 0;
56445 rc = writeJournalHdr(pPager);
56446 }
56447 }
56448
56449 if( rc!=SQLITE_OK0 ){
56450 sqlite3BitvecDestroy(pPager->pInJournal);
56451 pPager->pInJournal = 0;
56452 }else{
56453 assert( pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
56454 pPager->eState = PAGER_WRITER_CACHEMOD3;
56455 }
56456
56457 return rc;
56458}
56459
56460/*
56461** Begin a write-transaction on the specified pager object. If a
56462** write-transaction has already been opened, this function is a no-op.
56463**
56464** If the exFlag argument is false, then acquire at least a RESERVED
56465** lock on the database file. If exFlag is true, then acquire at least
56466** an EXCLUSIVE lock. If such a lock is already held, no locking
56467** functions need be called.
56468**
56469** If the subjInMemory argument is non-zero, then any sub-journal opened
56470** within this transaction will be opened as an in-memory file. This
56471** has no effect if the sub-journal is already opened (as it may be when
56472** running in exclusive mode) or if the transaction does not require a
56473** sub-journal. If the subjInMemory argument is zero, then any required
56474** sub-journal is implemented in-memory if pPager is an in-memory database,
56475** or using a temporary file otherwise.
56476*/
56477SQLITE_PRIVATEstatic int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
56478 int rc = SQLITE_OK0;
56479
56480 if( pPager->errCode ) return pPager->errCode;
56481 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR )((void) (0));
56482 pPager->subjInMemory = (u8)subjInMemory;
56483
56484 if( ALWAYS(pPager->eState==PAGER_READER)(pPager->eState==1) ){
56485 assert( pPager->pInJournal==0 )((void) (0));
56486
56487 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
56488 /* If the pager is configured to use locking_mode=exclusive, and an
56489 ** exclusive lock on the database is not already held, obtain it now.
56490 */
56491 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
56492 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
56493 if( rc!=SQLITE_OK0 ){
56494 return rc;
56495 }
56496 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
56497 }
56498
56499 /* Grab the write lock on the log file. If successful, upgrade to
56500 ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
56501 ** The busy-handler is not invoked if another connection already
56502 ** holds the write-lock. If possible, the upper layer will call it.
56503 */
56504 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
56505 }else{
56506 /* Obtain a RESERVED lock on the database file. If the exFlag parameter
56507 ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
56508 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
56509 ** lock, but not when obtaining the RESERVED lock.
56510 */
56511 rc = pagerLockDb(pPager, RESERVED_LOCK2);
56512 if( rc==SQLITE_OK0 && exFlag ){
56513 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK4);
56514 }
56515 }
56516
56517 if( rc==SQLITE_OK0 ){
56518 /* Change to WRITER_LOCKED state.
56519 **
56520 ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
56521 ** when it has an open transaction, but never to DBMOD or FINISHED.
56522 ** This is because in those states the code to roll back savepoint
56523 ** transactions may copy data from the sub-journal into the database
56524 ** file as well as into the page cache. Which would be incorrect in
56525 ** WAL mode.
56526 */
56527 pPager->eState = PAGER_WRITER_LOCKED2;
56528 pPager->dbHintSize = pPager->dbSize;
56529 pPager->dbFileSize = pPager->dbSize;
56530 pPager->dbOrigSize = pPager->dbSize;
56531 pPager->journalOff = 0;
56532 }
56533
56534 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER )((void) (0));
56535 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED )((void) (0));
56536 assert( assert_pager_state(pPager) )((void) (0));
56537 }
56538
56539 PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
56540 return rc;
56541}
56542
56543/*
56544** Write page pPg onto the end of the rollback journal.
56545*/
56546static SQLITE_NOINLINE__attribute__((noinline)) int pagerAddPageToRollbackJournal(PgHdr *pPg){
56547 Pager *pPager = pPg->pPager;
56548 int rc;
56549 u32 cksum;
56550 char *pData2;
56551 i64 iOff = pPager->journalOff;
56552
56553 /* We should never write to the journal file the page that
56554 ** contains the database locks. The following assert verifies
56555 ** that we do not. */
56556 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) )((void) (0));
56557
56558 assert( pPager->journalHdr<=pPager->journalOff )((void) (0));
56559 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2)pData2=(char*)pPg->pData;
56560 cksum = pager_cksum(pPager, (u8*)pData2);
56561
56562 /* Even if an IO or diskfull error occurs while journalling the
56563 ** page in the block above, set the need-sync flag for the page.
56564 ** Otherwise, when the transaction is rolled back, the logic in
56565 ** playback_one_page() will think that the page needs to be restored
56566 ** in the database file. And if an IO error occurs while doing so,
56567 ** then corruption may follow.
56568 */
56569 pPg->flags |= PGHDR_NEED_SYNC0x008;
56570
56571 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
56572 if( rc!=SQLITE_OK0 ) return rc;
56573 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
56574 if( rc!=SQLITE_OK0 ) return rc;
56575 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
56576 if( rc!=SQLITE_OK0 ) return rc;
56577
56578 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
56579 pPager->journalOff, pPager->pageSize));
56580 PAGER_INCR(sqlite3_pager_writej_count);
56581 PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
56582 PAGERID(pPager), pPg->pgno,
56583 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
56584
56585 pPager->journalOff += 8 + pPager->pageSize;
56586 pPager->nRec++;
56587 assert( pPager->pInJournal!=0 )((void) (0));
56588 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
56589 testcase( rc==SQLITE_NOMEM );
56590 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
56591 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
56592 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
56593 return rc;
56594}
56595
56596/*
56597** Mark a single data page as writeable. The page is written into the
56598** main journal or sub-journal as required. If the page is written into
56599** one of the journals, the corresponding bit is set in the
56600** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
56601** of any open savepoints as appropriate.
56602*/
56603static int pager_write(PgHdr *pPg){
56604 Pager *pPager = pPg->pPager;
56605 int rc = SQLITE_OK0;
56606
56607 /* This routine is not called unless a write-transaction has already
56608 ** been started. The journal file may or may not be open at this point.
56609 ** It is never called in the ERROR state.
56610 */
56611 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
56612 || pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
56613 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
56614 )((void) (0));
56615 assert( assert_pager_state(pPager) )((void) (0));
56616 assert( pPager->errCode==0 )((void) (0));
56617 assert( pPager->readOnly==0 )((void) (0));
56618 CHECK_PAGE(pPg);
56619
56620 /* The journal file needs to be opened. Higher level routines have already
56621 ** obtained the necessary locks to begin the write-transaction, but the
56622 ** rollback journal might not yet be open. Open it now if this is the case.
56623 **
56624 ** This is done before calling sqlite3PcacheMakeDirty() on the page.
56625 ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
56626 ** an error might occur and the pager would end up in WRITER_LOCKED state
56627 ** with pages marked as dirty in the cache.
56628 */
56629 if( pPager->eState==PAGER_WRITER_LOCKED2 ){
56630 rc = pager_open_journal(pPager);
56631 if( rc!=SQLITE_OK0 ) return rc;
56632 }
56633 assert( pPager->eState>=PAGER_WRITER_CACHEMOD )((void) (0));
56634 assert( assert_pager_state(pPager) )((void) (0));
56635
56636 /* Mark the page that is about to be modified as dirty. */
56637 sqlite3PcacheMakeDirty(pPg);
56638
56639 /* If a rollback journal is in use, them make sure the page that is about
56640 ** to change is in the rollback journal, or if the page is a new page off
56641 ** then end of the file, make sure it is marked as PGHDR_NEED_SYNC.
56642 */
56643 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) )((void) (0));
56644 if( pPager->pInJournal!=0
56645 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
56646 ){
56647 assert( pagerUseWal(pPager)==0 )((void) (0));
56648 if( pPg->pgno<=pPager->dbOrigSize ){
56649 rc = pagerAddPageToRollbackJournal(pPg);
56650 if( rc!=SQLITE_OK0 ){
56651 return rc;
56652 }
56653 }else{
56654 if( pPager->eState!=PAGER_WRITER_DBMOD4 ){
56655 pPg->flags |= PGHDR_NEED_SYNC0x008;
56656 }
56657 PAGERTRACE(("APPEND %d page %d needSync=%d\n",
56658 PAGERID(pPager), pPg->pgno,
56659 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
56660 }
56661 }
56662
56663 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
56664 ** and before writing the page into the rollback journal. Wait until now,
56665 ** after the page has been successfully journalled, before setting the
56666 ** PGHDR_WRITEABLE bit that indicates that the page can be safely modified.
56667 */
56668 pPg->flags |= PGHDR_WRITEABLE0x004;
56669
56670 /* If the statement journal is open and the page is not in it,
56671 ** then write the page into the statement journal.
56672 */
56673 if( pPager->nSavepoint>0 ){
56674 rc = subjournalPageIfRequired(pPg);
56675 }
56676
56677 /* Update the database size and return. */
56678 if( pPager->dbSize<pPg->pgno ){
56679 pPager->dbSize = pPg->pgno;
56680 }
56681 return rc;
56682}
56683
56684/*
56685** This is a variant of sqlite3PagerWrite() that runs when the sector size
56686** is larger than the page size. SQLite makes the (reasonable) assumption that
56687** all bytes of a sector are written together by hardware. Hence, all bytes of
56688** a sector need to be journalled in case of a power loss in the middle of
56689** a write.
56690**
56691** Usually, the sector size is less than or equal to the page size, in which
56692** case pages can be individually written. This routine only runs in the
56693** exceptional case where the page size is smaller than the sector size.
56694*/
56695static SQLITE_NOINLINE__attribute__((noinline)) int pagerWriteLargeSector(PgHdr *pPg){
56696 int rc = SQLITE_OK0; /* Return code */
56697 Pgno nPageCount; /* Total number of pages in database file */
56698 Pgno pg1; /* First page of the sector pPg is located on. */
56699 int nPage = 0; /* Number of pages starting at pg1 to journal */
56700 int ii; /* Loop counter */
56701 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
56702 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
56703 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
56704
56705 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
56706 ** a journal header to be written between the pages journaled by
56707 ** this function.
56708 */
56709 assert( !MEMDB )((void) (0));
56710 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 )((void) (0));
56711 pPager->doNotSpill |= SPILLFLAG_NOSYNC0x04;
56712
56713 /* This trick assumes that both the page-size and sector-size are
56714 ** an integer power of 2. It sets variable pg1 to the identifier
56715 ** of the first page of the sector pPg is located on.
56716 */
56717 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
56718
56719 nPageCount = pPager->dbSize;
56720 if( pPg->pgno>nPageCount ){
56721 nPage = (pPg->pgno - pg1)+1;
56722 }else if( (pg1+nPagePerSector-1)>nPageCount ){
56723 nPage = nPageCount+1-pg1;
56724 }else{
56725 nPage = nPagePerSector;
56726 }
56727 assert(nPage>0)((void) (0));
56728 assert(pg1<=pPg->pgno)((void) (0));
56729 assert((pg1+nPage)>pPg->pgno)((void) (0));
56730
56731 for(ii=0; ii<nPage && rc==SQLITE_OK0; ii++){
56732 Pgno pg = pg1+ii;
56733 PgHdr *pPage;
56734 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
56735 if( pg!=PAGER_MJ_PGNO(pPager)((Pgno)((sqlite3PendingByte/((pPager)->pageSize))+1)) ){
56736 rc = sqlite3PagerGet(pPager, pg, &pPage, 0);
56737 if( rc==SQLITE_OK0 ){
56738 rc = pager_write(pPage);
56739 if( pPage->flags&PGHDR_NEED_SYNC0x008 ){
56740 needSync = 1;
56741 }
56742 sqlite3PagerUnrefNotNull(pPage);
56743 }
56744 }
56745 }else if( (pPage = sqlite3PagerLookup(pPager, pg))!=0 ){
56746 if( pPage->flags&PGHDR_NEED_SYNC0x008 ){
56747 needSync = 1;
56748 }
56749 sqlite3PagerUnrefNotNull(pPage);
56750 }
56751 }
56752
56753 /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
56754 ** starting at pg1, then it needs to be set for all of them. Because
56755 ** writing to any of these nPage pages may damage the others, the
56756 ** journal file must contain sync()ed copies of all of them
56757 ** before any of them can be written out to the database file.
56758 */
56759 if( rc==SQLITE_OK0 && needSync ){
56760 assert( !MEMDB )((void) (0));
56761 for(ii=0; ii<nPage; ii++){
56762 PgHdr *pPage = sqlite3PagerLookup(pPager, pg1+ii);
56763 if( pPage ){
56764 pPage->flags |= PGHDR_NEED_SYNC0x008;
56765 sqlite3PagerUnrefNotNull(pPage);
56766 }
56767 }
56768 }
56769
56770 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 )((void) (0));
56771 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC0x04;
56772 return rc;
56773}
56774
56775/*
56776** Mark a data page as writeable. This routine must be called before
56777** making changes to a page. The caller must check the return value
56778** of this function and be careful not to change any page data unless
56779** this routine returns SQLITE_OK.
56780**
56781** The difference between this function and pager_write() is that this
56782** function also deals with the special case where 2 or more pages
56783** fit on a single disk sector. In this case all co-resident pages
56784** must have been written to the journal file before returning.
56785**
56786** If an error occurs, SQLITE_NOMEM or an IO error code is returned
56787** as appropriate. Otherwise, SQLITE_OK.
56788*/
56789SQLITE_PRIVATEstatic int sqlite3PagerWrite(PgHdr *pPg){
56790 Pager *pPager = pPg->pPager;
56791 assert( (pPg->flags & PGHDR_MMAP)==0 )((void) (0));
56792 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
56793 assert( assert_pager_state(pPager) )((void) (0));
56794 if( (pPg->flags & PGHDR_WRITEABLE0x004)!=0 && pPager->dbSize>=pPg->pgno ){
56795 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
56796 return SQLITE_OK0;
56797 }else if( pPager->errCode ){
56798 return pPager->errCode;
56799 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
56800 assert( pPager->tempFile==0 )((void) (0));
56801 return pagerWriteLargeSector(pPg);
56802 }else{
56803 return pager_write(pPg);
56804 }
56805}
56806
56807/*
56808** Return TRUE if the page given in the argument was previously passed
56809** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
56810** to change the content of the page.
56811*/
56812#ifndef NDEBUG1
56813SQLITE_PRIVATEstatic int sqlite3PagerIswriteable(DbPage *pPg){
56814 return pPg->flags & PGHDR_WRITEABLE0x004;
56815}
56816#endif
56817
56818/*
56819** A call to this routine tells the pager that it is not necessary to
56820** write the information on page pPg back to the disk, even though
56821** that page might be marked as dirty. This happens, for example, when
56822** the page has been added as a leaf of the freelist and so its
56823** content no longer matters.
56824**
56825** The overlying software layer calls this routine when all of the data
56826** on the given page is unused. The pager marks the page as clean so
56827** that it does not get written to disk.
56828**
56829** Tests show that this optimization can quadruple the speed of large
56830** DELETE operations.
56831**
56832** This optimization cannot be used with a temp-file, as the page may
56833** have been dirty at the start of the transaction. In that case, if
56834** memory pressure forces page pPg out of the cache, the data does need
56835** to be written out to disk so that it may be read back in if the
56836** current transaction is rolled back.
56837*/
56838SQLITE_PRIVATEstatic void sqlite3PagerDontWrite(PgHdr *pPg){
56839 Pager *pPager = pPg->pPager;
56840 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY0x002) && pPager->nSavepoint==0 ){
56841 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
56842 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
56843 pPg->flags |= PGHDR_DONT_WRITE0x010;
56844 pPg->flags &= ~PGHDR_WRITEABLE0x004;
56845 testcase( pPg->flags & PGHDR_NEED_SYNC );
56846 pager_set_pagehash(pPg);
56847 }
56848}
56849
56850/*
56851** This routine is called to increment the value of the database file
56852** change-counter, stored as a 4-byte big-endian integer starting at
56853** byte offset 24 of the pager file. The secondary change counter at
56854** 92 is also updated, as is the SQLite version number at offset 96.
56855**
56856** But this only happens if the pPager->changeCountDone flag is false.
56857** To avoid excess churning of page 1, the update only happens once.
56858** See also the pager_write_changecounter() routine that does an
56859** unconditional update of the change counters.
56860**
56861** If the isDirectMode flag is zero, then this is done by calling
56862** sqlite3PagerWrite() on page 1, then modifying the contents of the
56863** page data. In this case the file will be updated when the current
56864** transaction is committed.
56865**
56866** The isDirectMode flag may only be non-zero if the library was compiled
56867** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
56868** if isDirect is non-zero, then the database file is updated directly
56869** by writing an updated version of page 1 using a call to the
56870** sqlite3OsWrite() function.
56871*/
56872static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
56873 int rc = SQLITE_OK0;
56874
56875 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
56876 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
56877 )((void) (0));
56878 assert( assert_pager_state(pPager) )((void) (0));
56879
56880 /* Declare and initialize constant integer 'isDirect'. If the
56881 ** atomic-write optimization is enabled in this build, then isDirect
56882 ** is initialized to the value passed as the isDirectMode parameter
56883 ** to this function. Otherwise, it is always set to zero.
56884 **
56885 ** The idea is that if the atomic-write optimization is not
56886 ** enabled at compile time, the compiler can omit the tests of
56887 ** 'isDirect' below, as well as the block enclosed in the
56888 ** "if( isDirect )" condition.
56889 */
56890#ifndef SQLITE_ENABLE_ATOMIC_WRITE
56891# define DIRECT_MODE0 0
56892 assert( isDirectMode==0 )((void) (0));
56893 UNUSED_PARAMETER(isDirectMode)(void)(isDirectMode);
56894#else
56895# define DIRECT_MODE0 isDirectMode
56896#endif
56897
56898 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0)(pPager->dbSize>0) ){
56899 PgHdr *pPgHdr; /* Reference to page 1 */
56900
56901 assert( !pPager->tempFile && isOpen(pPager->fd) )((void) (0));
56902
56903 /* Open page 1 of the file for writing. */
56904 rc = sqlite3PagerGet(pPager, 1, &pPgHdr, 0);
56905 assert( pPgHdr==0 || rc==SQLITE_OK )((void) (0));
56906
56907 /* If page one was fetched successfully, and this function is not
56908 ** operating in direct-mode, make page 1 writable. When not in
56909 ** direct mode, page 1 is always held in cache and hence the PagerGet()
56910 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
56911 */
56912 if( !DIRECT_MODE0 && ALWAYS(rc==SQLITE_OK)(rc==0) ){
56913 rc = sqlite3PagerWrite(pPgHdr);
56914 }
56915
56916 if( rc==SQLITE_OK0 ){
56917 /* Actually do the update of the change counter */
56918 pager_write_changecounter(pPgHdr);
56919
56920 /* If running in direct mode, write the contents of page 1 to the file. */
56921 if( DIRECT_MODE0 ){
56922 const void *zBuf;
56923 assert( pPager->dbFileSize>0 )((void) (0));
56924 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf)zBuf=(char*)pPgHdr->pData;
56925 if( rc==SQLITE_OK0 ){
56926 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
56927 pPager->aStat[PAGER_STAT_WRITE2]++;
56928 }
56929 if( rc==SQLITE_OK0 ){
56930 /* Update the pager's copy of the change-counter. Otherwise, the
56931 ** next time a read transaction is opened the cache will be
56932 ** flushed (as the change-counter values will not match). */
56933 const void *pCopy = (const void *)&((const char *)zBuf)[24];
56934 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
56935 pPager->changeCountDone = 1;
56936 }
56937 }else{
56938 pPager->changeCountDone = 1;
56939 }
56940 }
56941
56942 /* Release the page reference. */
56943 sqlite3PagerUnref(pPgHdr);
56944 }
56945 return rc;
56946}
56947
56948/*
56949** Sync the database file to disk. This is a no-op for in-memory databases
56950** or pages with the Pager.noSync flag set.
56951**
56952** If successful, or if called on a pager for which it is a no-op, this
56953** function returns SQLITE_OK. Otherwise, an IO error code is returned.
56954*/
56955SQLITE_PRIVATEstatic int sqlite3PagerSync(Pager *pPager, const char *zMaster){
56956 int rc = SQLITE_OK0;
56957 void *pArg = (void*)zMaster;
56958 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC21, pArg);
56959 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
56960 if( rc==SQLITE_OK0 && !pPager->noSync ){
56961 assert( !MEMDB )((void) (0));
56962 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
56963 }
56964 return rc;
56965}
56966
56967/*
56968** This function may only be called while a write-transaction is active in
56969** rollback. If the connection is in WAL mode, this call is a no-op.
56970** Otherwise, if the connection does not already have an EXCLUSIVE lock on
56971** the database file, an attempt is made to obtain one.
56972**
56973** If the EXCLUSIVE lock is already held or the attempt to obtain it is
56974** successful, or the connection is in WAL mode, SQLITE_OK is returned.
56975** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
56976** returned.
56977*/
56978SQLITE_PRIVATEstatic int sqlite3PagerExclusiveLock(Pager *pPager){
56979 int rc = pPager->errCode;
56980 assert( assert_pager_state(pPager) )((void) (0));
56981 if( rc==SQLITE_OK0 ){
56982 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
56983 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
56984 || pPager->eState==PAGER_WRITER_LOCKED((void) (0))
56985 )((void) (0));
56986 assert( assert_pager_state(pPager) )((void) (0));
56987 if( 0==pagerUseWal(pPager)((pPager)->pWal!=0) ){
56988 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK4);
56989 }
56990 }
56991 return rc;
56992}
56993
56994/*
56995** Sync the database file for the pager pPager. zMaster points to the name
56996** of a master journal file that should be written into the individual
56997** journal file. zMaster may be NULL, which is interpreted as no master
56998** journal (a single database transaction).
56999**
57000** This routine ensures that:
57001**
57002** * The database file change-counter is updated,
57003** * the journal is synced (unless the atomic-write optimization is used),
57004** * all dirty pages are written to the database file,
57005** * the database file is truncated (if required), and
57006** * the database file synced.
57007**
57008** The only thing that remains to commit the transaction is to finalize
57009** (delete, truncate or zero the first part of) the journal file (or
57010** delete the master journal file if specified).
57011**
57012** Note that if zMaster==NULL, this does not overwrite a previous value
57013** passed to an sqlite3PagerCommitPhaseOne() call.
57014**
57015** If the final parameter - noSync - is true, then the database file itself
57016** is not synced. The caller must call sqlite3PagerSync() directly to
57017** sync the database file before calling CommitPhaseTwo() to delete the
57018** journal file in this case.
57019*/
57020SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseOne(
57021 Pager *pPager, /* Pager object */
57022 const char *zMaster, /* If not NULL, the master journal name */
57023 int noSync /* True to omit the xSync on the db file */
57024){
57025 int rc = SQLITE_OK0; /* Return code */
57026
57027 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
57028 || pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
57029 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
57030 || pPager->eState==PAGER_ERROR((void) (0))
57031 )((void) (0));
57032 assert( assert_pager_state(pPager) )((void) (0));
57033
57034 /* If a prior error occurred, report that error again. */
57035 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
57036
57037 /* Provide the ability to easily simulate an I/O error during testing */
57038 if( sqlite3FaultSim(400) ) return SQLITE_IOERR10;
57039
57040 PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
57041 pPager->zFilename, zMaster, pPager->dbSize));
57042
57043 /* If no database changes have been made, return early. */
57044 if( pPager->eState<PAGER_WRITER_CACHEMOD3 ) return SQLITE_OK0;
57045
57046 assert( MEMDB==0 || pPager->tempFile )((void) (0));
57047 assert( isOpen(pPager->fd) || pPager->tempFile )((void) (0));
57048 if( 0==pagerFlushOnCommit(pPager, 1) ){
57049 /* If this is an in-memory db, or no pages have been written to, or this
57050 ** function has already been called, it is mostly a no-op. However, any
57051 ** backup in progress needs to be restarted. */
57052 sqlite3BackupRestart(pPager->pBackup);
57053 }else{
57054 PgHdr *pList;
57055 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
57056 PgHdr *pPageOne = 0;
57057 pList = sqlite3PcacheDirtyList(pPager->pPCache);
57058 if( pList==0 ){
57059 /* Must have at least one page for the WAL commit flag.
57060 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
57061 rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
57062 pList = pPageOne;
57063 pList->pDirty = 0;
57064 }
57065 assert( rc==SQLITE_OK )((void) (0));
57066 if( ALWAYS(pList)(pList) ){
57067 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
57068 }
57069 sqlite3PagerUnref(pPageOne);
57070 if( rc==SQLITE_OK0 ){
57071 sqlite3PcacheCleanAll(pPager->pPCache);
57072 }
57073 }else{
57074 /* The bBatch boolean is true if the batch-atomic-write commit method
57075 ** should be used. No rollback journal is created if batch-atomic-write
57076 ** is enabled.
57077 */
57078#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
57079 sqlite3_file *fd = pPager->fd;
57080 int bBatch0 = zMaster==0 /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
57081 && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC0x00004000)
57082 && !pPager->noSync
57083 && sqlite3JournalIsInMemory(pPager->jfd);
57084#else
57085# define bBatch0 0
57086#endif
57087
57088#ifdef SQLITE_ENABLE_ATOMIC_WRITE
57089 /* The following block updates the change-counter. Exactly how it
57090 ** does this depends on whether or not the atomic-update optimization
57091 ** was enabled at compile time, and if this transaction meets the
57092 ** runtime criteria to use the operation:
57093 **
57094 ** * The file-system supports the atomic-write property for
57095 ** blocks of size page-size, and
57096 ** * This commit is not part of a multi-file transaction, and
57097 ** * Exactly one page has been modified and store in the journal file.
57098 **
57099 ** If the optimization was not enabled at compile time, then the
57100 ** pager_incr_changecounter() function is called to update the change
57101 ** counter in 'indirect-mode'. If the optimization is compiled in but
57102 ** is not applicable to this transaction, call sqlite3JournalCreate()
57103 ** to make sure the journal file has actually been created, then call
57104 ** pager_incr_changecounter() to update the change-counter in indirect
57105 ** mode.
57106 **
57107 ** Otherwise, if the optimization is both enabled and applicable,
57108 ** then call pager_incr_changecounter() to update the change-counter
57109 ** in 'direct' mode. In this case the journal file will never be
57110 ** created for this transaction.
57111 */
57112 if( bBatch0==0 ){
57113 PgHdr *pPg;
57114 assert( isOpen(pPager->jfd)((void) (0))
57115 || pPager->journalMode==PAGER_JOURNALMODE_OFF((void) (0))
57116 || pPager->journalMode==PAGER_JOURNALMODE_WAL((void) (0))
57117 )((void) (0));
57118 if( !zMaster && isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0)
57119 && pPager->journalOff==jrnlBufferSize(pPager)
57120 && pPager->dbSize>=pPager->dbOrigSize
57121 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
57122 ){
57123 /* Update the db file change counter via the direct-write method. The
57124 ** following call will modify the in-memory representation of page 1
57125 ** to include the updated change counter and then write page 1
57126 ** directly to the database file. Because of the atomic-write
57127 ** property of the host file-system, this is safe.
57128 */
57129 rc = pager_incr_changecounter(pPager, 1);
57130 }else{
57131 rc = sqlite3JournalCreate(pPager->jfd);
57132 if( rc==SQLITE_OK0 ){
57133 rc = pager_incr_changecounter(pPager, 0);
57134 }
57135 }
57136 }
57137#else /* SQLITE_ENABLE_ATOMIC_WRITE */
57138#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
57139 if( zMaster ){
57140 rc = sqlite3JournalCreate(pPager->jfd);
57141 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
57142 assert( bBatch==0 )((void) (0));
57143 }
57144#endif
57145 rc = pager_incr_changecounter(pPager, 0);
57146#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
57147 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
57148
57149 /* Write the master journal name into the journal file. If a master
57150 ** journal file name has already been written to the journal file,
57151 ** or if zMaster is NULL (no master journal), then this call is a no-op.
57152 */
57153 rc = writeMasterJournal(pPager, zMaster);
57154 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
57155
57156 /* Sync the journal file and write all dirty pages to the database.
57157 ** If the atomic-update optimization is being used, this sync will not
57158 ** create the journal file or perform any real IO.
57159 **
57160 ** Because the change-counter page was just modified, unless the
57161 ** atomic-update optimization is used it is almost certain that the
57162 ** journal requires a sync here. However, in locking_mode=exclusive
57163 ** on a system under memory pressure it is just possible that this is
57164 ** not the case. In this case it is likely enough that the redundant
57165 ** xSync() call will be changed to a no-op by the OS anyhow.
57166 */
57167 rc = syncJournal(pPager, 0);
57168 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
57169
57170 pList = sqlite3PcacheDirtyList(pPager->pPCache);
57171#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
57172 if( bBatch0 ){
57173 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE31, 0);
57174 if( rc==SQLITE_OK0 ){
57175 rc = pager_write_pagelist(pPager, pList);
57176 if( rc==SQLITE_OK0 ){
57177 rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE32, 0);
57178 }
57179 if( rc!=SQLITE_OK0 ){
57180 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE33, 0);
57181 }
57182 }
57183
57184 if( (rc&0xFF)==SQLITE_IOERR10 && rc!=SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
57185 rc = sqlite3JournalCreate(pPager->jfd);
57186 if( rc!=SQLITE_OK0 ){
57187 sqlite3OsClose(pPager->jfd);
57188 goto commit_phase_one_exit;
57189 }
57190 bBatch0 = 0;
57191 }else{
57192 sqlite3OsClose(pPager->jfd);
57193 }
57194 }
57195#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */
57196
57197 if( bBatch0==0 ){
57198 rc = pager_write_pagelist(pPager, pList);
57199 }
57200 if( rc!=SQLITE_OK0 ){
57201 assert( rc!=SQLITE_IOERR_BLOCKED )((void) (0));
57202 goto commit_phase_one_exit;
57203 }
57204 sqlite3PcacheCleanAll(pPager->pPCache);
57205
57206 /* If the file on disk is smaller than the database image, use
57207 ** pager_truncate to grow the file here. This can happen if the database
57208 ** image was extended as part of the current transaction and then the
57209 ** last page in the db image moved to the free-list. In this case the
57210 ** last page is never written out to disk, leaving the database file
57211 ** undersized. Fix this now if it is the case. */
57212 if( pPager->dbSize>pPager->dbFileSize ){
57213 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager)((Pgno)((sqlite3PendingByte/((pPager)->pageSize))+1)));
57214 assert( pPager->eState==PAGER_WRITER_DBMOD )((void) (0));
57215 rc = pager_truncate(pPager, nNew);
57216 if( rc!=SQLITE_OK0 ) goto commit_phase_one_exit;
57217 }
57218
57219 /* Finally, sync the database file. */
57220 if( !noSync ){
57221 rc = sqlite3PagerSync(pPager, zMaster);
57222 }
57223 IOTRACE(("DBSYNC %p\n", pPager))
57224 }
57225 }
57226
57227commit_phase_one_exit:
57228 if( rc==SQLITE_OK0 && !pagerUseWal(pPager)((pPager)->pWal!=0) ){
57229 pPager->eState = PAGER_WRITER_FINISHED5;
57230 }
57231 return rc;
57232}
57233
57234
57235/*
57236** When this function is called, the database file has been completely
57237** updated to reflect the changes made by the current transaction and
57238** synced to disk. The journal file still exists in the file-system
57239** though, and if a failure occurs at this point it will eventually
57240** be used as a hot-journal and the current transaction rolled back.
57241**
57242** This function finalizes the journal file, either by deleting,
57243** truncating or partially zeroing it, so that it cannot be used
57244** for hot-journal rollback. Once this is done the transaction is
57245** irrevocably committed.
57246**
57247** If an error occurs, an IO error code is returned and the pager
57248** moves into the error state. Otherwise, SQLITE_OK is returned.
57249*/
57250SQLITE_PRIVATEstatic int sqlite3PagerCommitPhaseTwo(Pager *pPager){
57251 int rc = SQLITE_OK0; /* Return code */
57252
57253 /* This routine should not be called if a prior error has occurred.
57254 ** But if (due to a coding error elsewhere in the system) it does get
57255 ** called, just return the same error code without doing anything. */
57256 if( NEVER(pPager->errCode)(pPager->errCode) ) return pPager->errCode;
57257
57258 assert( pPager->eState==PAGER_WRITER_LOCKED((void) (0))
57259 || pPager->eState==PAGER_WRITER_FINISHED((void) (0))
57260 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)((void) (0))
57261 )((void) (0));
57262 assert( assert_pager_state(pPager) )((void) (0));
57263
57264 /* An optimization. If the database was not actually modified during
57265 ** this transaction, the pager is running in exclusive-mode and is
57266 ** using persistent journals, then this function is a no-op.
57267 **
57268 ** The start of the journal file currently contains a single journal
57269 ** header with the nRec field set to 0. If such a journal is used as
57270 ** a hot-journal during hot-journal rollback, 0 changes will be made
57271 ** to the database file. So there is no need to zero the journal
57272 ** header. Since the pager is in exclusive mode, there is no need
57273 ** to drop any locks either.
57274 */
57275 if( pPager->eState==PAGER_WRITER_LOCKED2
57276 && pPager->exclusiveMode
57277 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST1
57278 ){
57279 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff )((void) (0));
57280 pPager->eState = PAGER_READER1;
57281 return SQLITE_OK0;
57282 }
57283
57284 PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
57285 pPager->iDataVersion++;
57286 rc = pager_end_transaction(pPager, pPager->setMaster, 1);
57287 return pager_error(pPager, rc);
57288}
57289
57290/*
57291** If a write transaction is open, then all changes made within the
57292** transaction are reverted and the current write-transaction is closed.
57293** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
57294** state if an error occurs.
57295**
57296** If the pager is already in PAGER_ERROR state when this function is called,
57297** it returns Pager.errCode immediately. No work is performed in this case.
57298**
57299** Otherwise, in rollback mode, this function performs two functions:
57300**
57301** 1) It rolls back the journal file, restoring all database file and
57302** in-memory cache pages to the state they were in when the transaction
57303** was opened, and
57304**
57305** 2) It finalizes the journal file, so that it is not used for hot
57306** rollback at any point in the future.
57307**
57308** Finalization of the journal file (task 2) is only performed if the
57309** rollback is successful.
57310**
57311** In WAL mode, all cache-entries containing data modified within the
57312** current transaction are either expelled from the cache or reverted to
57313** their pre-transaction state by re-reading data from the database or
57314** WAL files. The WAL transaction is then closed.
57315*/
57316SQLITE_PRIVATEstatic int sqlite3PagerRollback(Pager *pPager){
57317 int rc = SQLITE_OK0; /* Return code */
57318 PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
57319
57320 /* PagerRollback() is a no-op if called in READER or OPEN state. If
57321 ** the pager is already in the ERROR state, the rollback is not
57322 ** attempted here. Instead, the error code is returned to the caller.
57323 */
57324 assert( assert_pager_state(pPager) )((void) (0));
57325 if( pPager->eState==PAGER_ERROR6 ) return pPager->errCode;
57326 if( pPager->eState<=PAGER_READER1 ) return SQLITE_OK0;
57327
57328 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
57329 int rc2;
57330 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK2, -1);
57331 rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
57332 if( rc==SQLITE_OK0 ) rc = rc2;
57333 }else if( !isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) || pPager->eState==PAGER_WRITER_LOCKED2 ){
57334 int eState = pPager->eState;
57335 rc = pager_end_transaction(pPager, 0, 0);
57336 if( !MEMDBpPager->memDb && eState>PAGER_WRITER_LOCKED2 ){
57337 /* This can happen using journal_mode=off. Move the pager to the error
57338 ** state to indicate that the contents of the cache may not be trusted.
57339 ** Any active readers will get SQLITE_ABORT.
57340 */
57341 pPager->errCode = SQLITE_ABORT4;
57342 pPager->eState = PAGER_ERROR6;
57343 setGetterMethod(pPager);
57344 return rc;
57345 }
57346 }else{
57347 rc = pager_playback(pPager, 0);
57348 }
57349
57350 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK )((void) (0));
57351 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT((void) (0))
57352 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR((void) (0))
57353 || rc==SQLITE_CANTOPEN((void) (0))
57354 )((void) (0));
57355
57356 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
57357 ** cache. So call pager_error() on the way out to make any error persistent.
57358 */
57359 return pager_error(pPager, rc);
57360}
57361
57362/*
57363** Return TRUE if the database file is opened read-only. Return FALSE
57364** if the database is (in theory) writable.
57365*/
57366SQLITE_PRIVATEstatic u8 sqlite3PagerIsreadonly(Pager *pPager){
57367 return pPager->readOnly;
57368}
57369
57370#ifdef SQLITE_DEBUG
57371/*
57372** Return the sum of the reference counts for all pages held by pPager.
57373*/
57374SQLITE_PRIVATEstatic int sqlite3PagerRefcount(Pager *pPager){
57375 return sqlite3PcacheRefCount(pPager->pPCache);
57376}
57377#endif
57378
57379/*
57380** Return the approximate number of bytes of memory currently
57381** used by the pager and its associated cache.
57382*/
57383SQLITE_PRIVATEstatic int sqlite3PagerMemUsed(Pager *pPager){
57384 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
57385 + 5*sizeof(void*);
57386 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
57387 + sqlite3MallocSize(pPager)
57388 + pPager->pageSize;
57389}
57390
57391/*
57392** Return the number of references to the specified page.
57393*/
57394SQLITE_PRIVATEstatic int sqlite3PagerPageRefcount(DbPage *pPage){
57395 return sqlite3PcachePageRefcount(pPage);
57396}
57397
57398#ifdef SQLITE_TEST
57399/*
57400** This routine is used for testing and analysis only.
57401*/
57402SQLITE_PRIVATEstatic int *sqlite3PagerStats(Pager *pPager){
57403 static int a[11];
57404 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
57405 a[1] = sqlite3PcachePagecount(pPager->pPCache);
57406 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
57407 a[3] = pPager->eState==PAGER_OPEN0 ? -1 : (int) pPager->dbSize;
57408 a[4] = pPager->eState;
57409 a[5] = pPager->errCode;
57410 a[6] = pPager->aStat[PAGER_STAT_HIT0];
57411 a[7] = pPager->aStat[PAGER_STAT_MISS1];
57412 a[8] = 0; /* Used to be pPager->nOvfl */
57413 a[9] = pPager->nRead;
57414 a[10] = pPager->aStat[PAGER_STAT_WRITE2];
57415 return a;
57416}
57417#endif
57418
57419/*
57420** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
57421** or _WRITE+1. The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
57422** of SQLITE_DBSTATUS_CACHE_SPILL. The _SPILL case is not contiguous because
57423** it was added later.
57424**
57425** Before returning, *pnVal is incremented by the
57426** current cache hit or miss count, according to the value of eStat. If the
57427** reset parameter is non-zero, the cache hit or miss count is zeroed before
57428** returning.
57429*/
57430SQLITE_PRIVATEstatic void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
57431
57432 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT((void) (0))
57433 || eStat==SQLITE_DBSTATUS_CACHE_MISS((void) (0))
57434 || eStat==SQLITE_DBSTATUS_CACHE_WRITE((void) (0))
57435 || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1((void) (0))
57436 )((void) (0));
57437
57438 assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS )((void) (0));
57439 assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE )((void) (0));
57440 assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1((void) (0))
57441 && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 )((void) (0));
57442
57443 eStat -= SQLITE_DBSTATUS_CACHE_HIT7;
57444 *pnVal += pPager->aStat[eStat];
57445 if( reset ){
57446 pPager->aStat[eStat] = 0;
57447 }
57448}
57449
57450/*
57451** Return true if this is an in-memory or temp-file backed pager.
57452*/
57453SQLITE_PRIVATEstatic int sqlite3PagerIsMemdb(Pager *pPager){
57454 return pPager->tempFile;
57455}
57456
57457/*
57458** Check that there are at least nSavepoint savepoints open. If there are
57459** currently less than nSavepoints open, then open one or more savepoints
57460** to make up the difference. If the number of savepoints is already
57461** equal to nSavepoint, then this function is a no-op.
57462**
57463** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
57464** occurs while opening the sub-journal file, then an IO error code is
57465** returned. Otherwise, SQLITE_OK.
57466*/
57467static SQLITE_NOINLINE__attribute__((noinline)) int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
57468 int rc = SQLITE_OK0; /* Return code */
57469 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
57470 int ii; /* Iterator variable */
57471 PagerSavepoint *aNew; /* New Pager.aSavepoint array */
57472
57473 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
57474 assert( assert_pager_state(pPager) )((void) (0));
57475 assert( nSavepoint>nCurrent && pPager->useJournal )((void) (0));
57476
57477 /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
57478 ** if the allocation fails. Otherwise, zero the new portion in case a
57479 ** malloc failure occurs while populating it in the for(...) loop below.
57480 */
57481 aNew = (PagerSavepoint *)sqlite3Realloc(
57482 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
57483 );
57484 if( !aNew ){
57485 return SQLITE_NOMEM_BKPT7;
57486 }
57487 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
57488 pPager->aSavepoint = aNew;
57489
57490 /* Populate the PagerSavepoint structures just allocated. */
57491 for(ii=nCurrent; ii<nSavepoint; ii++){
57492 aNew[ii].nOrig = pPager->dbSize;
57493 if( isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) && pPager->journalOff>0 ){
57494 aNew[ii].iOffset = pPager->journalOff;
57495 }else{
57496 aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager)(pPager->sectorSize);
57497 }
57498 aNew[ii].iSubRec = pPager->nSubRec;
57499 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
57500 if( !aNew[ii].pInSavepoint ){
57501 return SQLITE_NOMEM_BKPT7;
57502 }
57503 if( pagerUseWal(pPager)((pPager)->pWal!=0) ){
57504 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
57505 }
57506 pPager->nSavepoint = ii+1;
57507 }
57508 assert( pPager->nSavepoint==nSavepoint )((void) (0));
57509 assertTruncateConstraint(pPager);
57510 return rc;
57511}
57512SQLITE_PRIVATEstatic int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
57513 assert( pPager->eState>=PAGER_WRITER_LOCKED )((void) (0));
57514 assert( assert_pager_state(pPager) )((void) (0));
57515
57516 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
57517 return pagerOpenSavepoint(pPager, nSavepoint);
57518 }else{
57519 return SQLITE_OK0;
57520 }
57521}
57522
57523
57524/*
57525** This function is called to rollback or release (commit) a savepoint.
57526** The savepoint to release or rollback need not be the most recently
57527** created savepoint.
57528**
57529** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
57530** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
57531** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
57532** that have occurred since the specified savepoint was created.
57533**
57534** The savepoint to rollback or release is identified by parameter
57535** iSavepoint. A value of 0 means to operate on the outermost savepoint
57536** (the first created). A value of (Pager.nSavepoint-1) means operate
57537** on the most recently created savepoint. If iSavepoint is greater than
57538** (Pager.nSavepoint-1), then this function is a no-op.
57539**
57540** If a negative value is passed to this function, then the current
57541** transaction is rolled back. This is different to calling
57542** sqlite3PagerRollback() because this function does not terminate
57543** the transaction or unlock the database, it just restores the
57544** contents of the database to its original state.
57545**
57546** In any case, all savepoints with an index greater than iSavepoint
57547** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
57548** then savepoint iSavepoint is also destroyed.
57549**
57550** This function may return SQLITE_NOMEM if a memory allocation fails,
57551** or an IO error code if an IO error occurs while rolling back a
57552** savepoint. If no errors occur, SQLITE_OK is returned.
57553*/
57554SQLITE_PRIVATEstatic int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
57555 int rc = pPager->errCode;
57556
57557#ifdef SQLITE_ENABLE_ZIPVFS
57558 if( op==SAVEPOINT_RELEASE1 ) rc = SQLITE_OK0;
57559#endif
57560
57561 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK )((void) (0));
57562 assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK )((void) (0));
57563
57564 if( rc==SQLITE_OK0 && iSavepoint<pPager->nSavepoint ){
57565 int ii; /* Iterator variable */
57566 int nNew; /* Number of remaining savepoints after this op. */
57567
57568 /* Figure out how many savepoints will still be active after this
57569 ** operation. Store this value in nNew. Then free resources associated
57570 ** with any savepoints that are destroyed by this operation.
57571 */
57572 nNew = iSavepoint + (( op==SAVEPOINT_RELEASE1 ) ? 0 : 1);
57573 for(ii=nNew; ii<pPager->nSavepoint; ii++){
57574 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
57575 }
57576 pPager->nSavepoint = nNew;
57577
57578 /* If this is a release of the outermost savepoint, truncate
57579 ** the sub-journal to zero bytes in size. */
57580 if( op==SAVEPOINT_RELEASE1 ){
57581 if( nNew==0 && isOpen(pPager->sjfd)((pPager->sjfd)->pMethods!=0) ){
57582 /* Only truncate if it is an in-memory sub-journal. */
57583 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
57584 rc = sqlite3OsTruncate(pPager->sjfd, 0);
57585 assert( rc==SQLITE_OK )((void) (0));
57586 }
57587 pPager->nSubRec = 0;
57588 }
57589 }
57590 /* Else this is a rollback operation, playback the specified savepoint.
57591 ** If this is a temp-file, it is possible that the journal file has
57592 ** not yet been opened. In this case there have been no changes to
57593 ** the database file, so the playback operation can be skipped.
57594 */
57595 else if( pagerUseWal(pPager)((pPager)->pWal!=0) || isOpen(pPager->jfd)((pPager->jfd)->pMethods!=0) ){
57596 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
57597 rc = pagerPlaybackSavepoint(pPager, pSavepoint);
57598 assert(rc!=SQLITE_DONE)((void) (0));
57599 }
57600
57601#ifdef SQLITE_ENABLE_ZIPVFS
57602 /* If the cache has been modified but the savepoint cannot be rolled
57603 ** back journal_mode=off, put the pager in the error state. This way,
57604 ** if the VFS used by this pager includes ZipVFS, the entire transaction
57605 ** can be rolled back at the ZipVFS level. */
57606 else if(
57607 pPager->journalMode==PAGER_JOURNALMODE_OFF2
57608 && pPager->eState>=PAGER_WRITER_CACHEMOD3
57609 ){
57610 pPager->errCode = SQLITE_ABORT4;
57611 pPager->eState = PAGER_ERROR6;
57612 setGetterMethod(pPager);
57613 }
57614#endif
57615 }
57616
57617 return rc;
57618}
57619
57620/*
57621** Return the full pathname of the database file.
57622**
57623** Except, if the pager is in-memory only, then return an empty string if
57624** nullIfMemDb is true. This routine is called with nullIfMemDb==1 when
57625** used to report the filename to the user, for compatibility with legacy
57626** behavior. But when the Btree needs to know the filename for matching to
57627** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
57628** participate in shared-cache.
57629*/
57630SQLITE_PRIVATEstatic const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
57631 return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
57632}
57633
57634/*
57635** Return the VFS structure for the pager.
57636*/
57637SQLITE_PRIVATEstatic sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
57638 return pPager->pVfs;
57639}
57640
57641/*
57642** Return the file handle for the database file associated
57643** with the pager. This might return NULL if the file has
57644** not yet been opened.
57645*/
57646SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerFile(Pager *pPager){
57647 return pPager->fd;
57648}
57649
57650#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
57651/*
57652** Reset the lock timeout for pager.
57653*/
57654SQLITE_PRIVATEstatic void sqlite3PagerResetLockTimeout(Pager *pPager){
57655 int x = 0;
57656 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT34, &x);
57657}
57658#endif
57659
57660/*
57661** Return the file handle for the journal file (if it exists).
57662** This will be either the rollback journal or the WAL file.
57663*/
57664SQLITE_PRIVATEstatic sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
57665#if SQLITE_OMIT_WAL
57666 return pPager->jfd;
57667#else
57668 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
57669#endif
57670}
57671
57672/*
57673** Return the full pathname of the journal file.
57674*/
57675SQLITE_PRIVATEstatic const char *sqlite3PagerJournalname(Pager *pPager){
57676 return pPager->zJournal;
57677}
57678
57679#ifdef SQLITE_HAS_CODEC
57680/*
57681** Set or retrieve the codec for this pager
57682*/
57683SQLITE_PRIVATEstatic void sqlite3PagerSetCodec(
57684 Pager *pPager,
57685 void *(*xCodec)(void*,void*,Pgno,int),
57686 void (*xCodecSizeChng)(void*,int,int),
57687 void (*xCodecFree)(void*),
57688 void *pCodec
57689){
57690 if( pPager->xCodecFree ){
57691 pPager->xCodecFree(pPager->pCodec);
57692 }else{
57693 pager_reset(pPager);
57694 }
57695 pPager->xCodec = pPager->memDb ? 0 : xCodec;
57696 pPager->xCodecSizeChng = xCodecSizeChng;
57697 pPager->xCodecFree = xCodecFree;
57698 pPager->pCodec = pCodec;
57699 setGetterMethod(pPager);
57700 pagerReportSize(pPager);
57701}
57702SQLITE_PRIVATEstatic void *sqlite3PagerGetCodec(Pager *pPager){
57703 return pPager->pCodec;
57704}
57705
57706/*
57707** This function is called by the wal module when writing page content
57708** into the log file.
57709**
57710** This function returns a pointer to a buffer containing the encrypted
57711** page content. If a malloc fails, this function may return NULL.
57712*/
57713SQLITE_PRIVATEstatic void *sqlite3PagerCodec(PgHdr *pPg){
57714 void *aData = 0;
57715 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData)aData=(char*)pPg->pData;
57716 return aData;
57717}
57718
57719/*
57720** Return the current pager state
57721*/
57722SQLITE_PRIVATEstatic int sqlite3PagerState(Pager *pPager){
57723 return pPager->eState;
57724}
57725#endif /* SQLITE_HAS_CODEC */
57726
57727#ifndef SQLITE_OMIT_AUTOVACUUM
57728/*
57729** Move the page pPg to location pgno in the file.
57730**
57731** There must be no references to the page previously located at
57732** pgno (which we call pPgOld) though that page is allowed to be
57733** in cache. If the page previously located at pgno is not already
57734** in the rollback journal, it is not put there by by this routine.
57735**
57736** References to the page pPg remain valid. Updating any
57737** meta-data associated with pPg (i.e. data stored in the nExtra bytes
57738** allocated along with the page) is the responsibility of the caller.
57739**
57740** A transaction must be active when this routine is called. It used to be
57741** required that a statement transaction was not active, but this restriction
57742** has been removed (CREATE INDEX needs to move a page when a statement
57743** transaction is active).
57744**
57745** If the fourth argument, isCommit, is non-zero, then this page is being
57746** moved as part of a database reorganization just before the transaction
57747** is being committed. In this case, it is guaranteed that the database page
57748** pPg refers to will not be written to again within this transaction.
57749**
57750** This function may return SQLITE_NOMEM or an IO error code if an error
57751** occurs. Otherwise, it returns SQLITE_OK.
57752*/
57753SQLITE_PRIVATEstatic int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
57754 PgHdr *pPgOld; /* The page being overwritten. */
57755 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
57756 int rc; /* Return code */
57757 Pgno origPgno; /* The original page number */
57758
57759 assert( pPg->nRef>0 )((void) (0));
57760 assert( pPager->eState==PAGER_WRITER_CACHEMOD((void) (0))
57761 || pPager->eState==PAGER_WRITER_DBMOD((void) (0))
57762 )((void) (0));
57763 assert( assert_pager_state(pPager) )((void) (0));
57764
57765 /* In order to be able to rollback, an in-memory database must journal
57766 ** the page we are moving from.
57767 */
57768 assert( pPager->tempFile || !MEMDB )((void) (0));
57769 if( pPager->tempFile ){
57770 rc = sqlite3PagerWrite(pPg);
57771 if( rc ) return rc;
57772 }
57773
57774 /* If the page being moved is dirty and has not been saved by the latest
57775 ** savepoint, then save the current contents of the page into the
57776 ** sub-journal now. This is required to handle the following scenario:
57777 **
57778 ** BEGIN;
57779 ** <journal page X, then modify it in memory>
57780 ** SAVEPOINT one;
57781 ** <Move page X to location Y>
57782 ** ROLLBACK TO one;
57783 **
57784 ** If page X were not written to the sub-journal here, it would not
57785 ** be possible to restore its contents when the "ROLLBACK TO one"
57786 ** statement were is processed.
57787 **
57788 ** subjournalPage() may need to allocate space to store pPg->pgno into
57789 ** one or more savepoint bitvecs. This is the reason this function
57790 ** may return SQLITE_NOMEM.
57791 */
57792 if( (pPg->flags & PGHDR_DIRTY0x002)!=0
57793 && SQLITE_OK0!=(rc = subjournalPageIfRequired(pPg))
57794 ){
57795 return rc;
57796 }
57797
57798 PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
57799 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
57800 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
57801
57802 /* If the journal needs to be sync()ed before page pPg->pgno can
57803 ** be written to, store pPg->pgno in local variable needSyncPgno.
57804 **
57805 ** If the isCommit flag is set, there is no need to remember that
57806 ** the journal needs to be sync()ed before database page pPg->pgno
57807 ** can be written to. The caller has already promised not to write to it.
57808 */
57809 if( (pPg->flags&PGHDR_NEED_SYNC0x008) && !isCommit ){
57810 needSyncPgno = pPg->pgno;
57811 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||((void) (0))
57812 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize )((void) (0));
57813 assert( pPg->flags&PGHDR_DIRTY )((void) (0));
57814 }
57815
57816 /* If the cache contains a page with page-number pgno, remove it
57817 ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
57818 ** page pgno before the 'move' operation, it needs to be retained
57819 ** for the page moved there.
57820 */
57821 pPg->flags &= ~PGHDR_NEED_SYNC0x008;
57822 pPgOld = sqlite3PagerLookup(pPager, pgno);
57823 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB )((void) (0));
57824 if( pPgOld ){
57825 if( pPgOld->nRef>1 ){
57826 sqlite3PagerUnrefNotNull(pPgOld);
57827 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(57827);
57828 }
57829 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC0x008);
57830 if( pPager->tempFile ){
57831 /* Do not discard pages from an in-memory database since we might
57832 ** need to rollback later. Just move the page out of the way. */
57833 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
57834 }else{
57835 sqlite3PcacheDrop(pPgOld);
57836 }
57837 }
57838
57839 origPgno = pPg->pgno;
57840 sqlite3PcacheMove(pPg, pgno);
57841 sqlite3PcacheMakeDirty(pPg);
57842
57843 /* For an in-memory database, make sure the original page continues
57844 ** to exist, in case the transaction needs to roll back. Use pPgOld
57845 ** as the original page since it has already been allocated.
57846 */
57847 if( pPager->tempFile && pPgOld ){
57848 sqlite3PcacheMove(pPgOld, origPgno);
57849 sqlite3PagerUnrefNotNull(pPgOld);
57850 }
57851
57852 if( needSyncPgno ){
57853 /* If needSyncPgno is non-zero, then the journal file needs to be
57854 ** sync()ed before any data is written to database file page needSyncPgno.
57855 ** Currently, no such page exists in the page-cache and the
57856 ** "is journaled" bitvec flag has been set. This needs to be remedied by
57857 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
57858 ** flag.
57859 **
57860 ** If the attempt to load the page into the page-cache fails, (due
57861 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
57862 ** array. Otherwise, if the page is loaded and written again in
57863 ** this transaction, it may be written to the database file before
57864 ** it is synced into the journal file. This way, it may end up in
57865 ** the journal file twice, but that is not a problem.
57866 */
57867 PgHdr *pPgHdr;
57868 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr, 0);
57869 if( rc!=SQLITE_OK0 ){
57870 if( needSyncPgno<=pPager->dbOrigSize ){
57871 assert( pPager->pTmpSpace!=0 )((void) (0));
57872 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
57873 }
57874 return rc;
57875 }
57876 pPgHdr->flags |= PGHDR_NEED_SYNC0x008;
57877 sqlite3PcacheMakeDirty(pPgHdr);
57878 sqlite3PagerUnrefNotNull(pPgHdr);
57879 }
57880
57881 return SQLITE_OK0;
57882}
57883#endif
57884
57885/*
57886** The page handle passed as the first argument refers to a dirty page
57887** with a page number other than iNew. This function changes the page's
57888** page number to iNew and sets the value of the PgHdr.flags field to
57889** the value passed as the third parameter.
57890*/
57891SQLITE_PRIVATEstatic void sqlite3PagerRekey(DbPage *pPg, Pgno iNew, u16 flags){
57892 assert( pPg->pgno!=iNew )((void) (0));
57893 pPg->flags = flags;
57894 sqlite3PcacheMove(pPg, iNew);
57895}
57896
57897/*
57898** Return a pointer to the data for the specified page.
57899*/
57900SQLITE_PRIVATEstatic void *sqlite3PagerGetData(DbPage *pPg){
57901 assert( pPg->nRef>0 || pPg->pPager->memDb )((void) (0));
57902 return pPg->pData;
57903}
57904
57905/*
57906** Return a pointer to the Pager.nExtra bytes of "extra" space
57907** allocated along with the specified page.
57908*/
57909SQLITE_PRIVATEstatic void *sqlite3PagerGetExtra(DbPage *pPg){
57910 return pPg->pExtra;
57911}
57912
57913/*
57914** Get/set the locking-mode for this pager. Parameter eMode must be one
57915** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
57916** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
57917** the locking-mode is set to the value specified.
57918**
57919** The returned value is either PAGER_LOCKINGMODE_NORMAL or
57920** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
57921** locking-mode.
57922*/
57923SQLITE_PRIVATEstatic int sqlite3PagerLockingMode(Pager *pPager, int eMode){
57924 assert( eMode==PAGER_LOCKINGMODE_QUERY((void) (0))
57925 || eMode==PAGER_LOCKINGMODE_NORMAL((void) (0))
57926 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
57927 assert( PAGER_LOCKINGMODE_QUERY<0 )((void) (0));
57928 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 )((void) (0));
57929 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) )((void) (0));
57930 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
57931 pPager->exclusiveMode = (u8)eMode;
57932 }
57933 return (int)pPager->exclusiveMode;
57934}
57935
57936/*
57937** Set the journal-mode for this pager. Parameter eMode must be one of:
57938**
57939** PAGER_JOURNALMODE_DELETE
57940** PAGER_JOURNALMODE_TRUNCATE
57941** PAGER_JOURNALMODE_PERSIST
57942** PAGER_JOURNALMODE_OFF
57943** PAGER_JOURNALMODE_MEMORY
57944** PAGER_JOURNALMODE_WAL
57945**
57946** The journalmode is set to the value specified if the change is allowed.
57947** The change may be disallowed for the following reasons:
57948**
57949** * An in-memory database can only have its journal_mode set to _OFF
57950** or _MEMORY.
57951**
57952** * Temporary databases cannot have _WAL journalmode.
57953**
57954** The returned indicate the current (possibly updated) journal-mode.
57955*/
57956SQLITE_PRIVATEstatic int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
57957 u8 eOld = pPager->journalMode; /* Prior journalmode */
57958
57959 /* The eMode parameter is always valid */
57960 assert( eMode==PAGER_JOURNALMODE_DELETE((void) (0))
57961 || eMode==PAGER_JOURNALMODE_TRUNCATE((void) (0))
57962 || eMode==PAGER_JOURNALMODE_PERSIST((void) (0))
57963 || eMode==PAGER_JOURNALMODE_OFF((void) (0))
57964 || eMode==PAGER_JOURNALMODE_WAL((void) (0))
57965 || eMode==PAGER_JOURNALMODE_MEMORY )((void) (0));
57966
57967 /* This routine is only called from the OP_JournalMode opcode, and
57968 ** the logic there will never allow a temporary file to be changed
57969 ** to WAL mode.
57970 */
57971 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL )((void) (0));
57972
57973 /* Do allow the journalmode of an in-memory database to be set to
57974 ** anything other than MEMORY or OFF
57975 */
57976 if( MEMDBpPager->memDb ){
57977 assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF )((void) (0));
57978 if( eMode!=PAGER_JOURNALMODE_MEMORY4 && eMode!=PAGER_JOURNALMODE_OFF2 ){
57979 eMode = eOld;
57980 }
57981 }
57982
57983 if( eMode!=eOld ){
57984
57985 /* Change the journal mode. */
57986 assert( pPager->eState!=PAGER_ERROR )((void) (0));
57987 pPager->journalMode = (u8)eMode;
57988
57989 /* When transistioning from TRUNCATE or PERSIST to any other journal
57990 ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
57991 ** delete the journal file.
57992 */
57993 assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 )((void) (0));
57994 assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 )((void) (0));
57995 assert( (PAGER_JOURNALMODE_DELETE & 5)==0 )((void) (0));
57996 assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 )((void) (0));
57997 assert( (PAGER_JOURNALMODE_OFF & 5)==0 )((void) (0));
57998 assert( (PAGER_JOURNALMODE_WAL & 5)==5 )((void) (0));
57999
58000 assert( isOpen(pPager->fd) || pPager->exclusiveMode )((void) (0));
58001 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
58002
58003 /* In this case we would like to delete the journal file. If it is
58004 ** not possible, then that is not a problem. Deleting the journal file
58005 ** here is an optimization only.
58006 **
58007 ** Before deleting the journal file, obtain a RESERVED lock on the
58008 ** database file. This ensures that the journal file is not deleted
58009 ** while it is in use by some other client.
58010 */
58011 sqlite3OsClose(pPager->jfd);
58012 if( pPager->eLock>=RESERVED_LOCK2 ){
58013 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
58014 }else{
58015 int rc = SQLITE_OK0;
58016 int state = pPager->eState;
58017 assert( state==PAGER_OPEN || state==PAGER_READER )((void) (0));
58018 if( state==PAGER_OPEN0 ){
58019 rc = sqlite3PagerSharedLock(pPager);
58020 }
58021 if( pPager->eState==PAGER_READER1 ){
58022 assert( rc==SQLITE_OK )((void) (0));
58023 rc = pagerLockDb(pPager, RESERVED_LOCK2);
58024 }
58025 if( rc==SQLITE_OK0 ){
58026 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
58027 }
58028 if( rc==SQLITE_OK0 && state==PAGER_READER1 ){
58029 pagerUnlockDb(pPager, SHARED_LOCK1);
58030 }else if( state==PAGER_OPEN0 ){
58031 pager_unlock(pPager);
58032 }
58033 assert( state==pPager->eState )((void) (0));
58034 }
58035 }else if( eMode==PAGER_JOURNALMODE_OFF2 ){
58036 sqlite3OsClose(pPager->jfd);
58037 }
58038 }
58039
58040 /* Return the new journal mode */
58041 return (int)pPager->journalMode;
58042}
58043
58044/*
58045** Return the current journal mode.
58046*/
58047SQLITE_PRIVATEstatic int sqlite3PagerGetJournalMode(Pager *pPager){
58048 return (int)pPager->journalMode;
58049}
58050
58051/*
58052** Return TRUE if the pager is in a state where it is OK to change the
58053** journalmode. Journalmode changes can only happen when the database
58054** is unmodified.
58055*/
58056SQLITE_PRIVATEstatic int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
58057 assert( assert_pager_state(pPager) )((void) (0));
58058 if( pPager->eState>=PAGER_WRITER_CACHEMOD3 ) return 0;
58059 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0)(((pPager->jfd)->pMethods!=0) && pPager->journalOff
>0)
) return 0;
58060 return 1;
58061}
58062
58063/*
58064** Get/set the size-limit used for persistent journal files.
58065**
58066** Setting the size limit to -1 means no limit is enforced.
58067** An attempt to set a limit smaller than -1 is a no-op.
58068*/
58069SQLITE_PRIVATEstatic i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
58070 if( iLimit>=-1 ){
58071 pPager->journalSizeLimit = iLimit;
58072 sqlite3WalLimit(pPager->pWal, iLimit);
58073 }
58074 return pPager->journalSizeLimit;
58075}
58076
58077/*
58078** Return a pointer to the pPager->pBackup variable. The backup module
58079** in backup.c maintains the content of this variable. This module
58080** uses it opaquely as an argument to sqlite3BackupRestart() and
58081** sqlite3BackupUpdate() only.
58082*/
58083SQLITE_PRIVATEstatic sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
58084 return &pPager->pBackup;
58085}
58086
58087#ifndef SQLITE_OMIT_VACUUM
58088/*
58089** Unless this is an in-memory or temporary database, clear the pager cache.
58090*/
58091SQLITE_PRIVATEstatic void sqlite3PagerClearCache(Pager *pPager){
58092 assert( MEMDB==0 || pPager->tempFile )((void) (0));
58093 if( pPager->tempFile==0 ) pager_reset(pPager);
58094}
58095#endif
58096
58097
58098#ifndef SQLITE_OMIT_WAL
58099/*
58100** This function is called when the user invokes "PRAGMA wal_checkpoint",
58101** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
58102** or wal_blocking_checkpoint() API functions.
58103**
58104** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
58105*/
58106SQLITE_PRIVATEstatic int sqlite3PagerCheckpoint(
58107 Pager *pPager, /* Checkpoint on this pager */
58108 sqlite3 *db, /* Db handle used to check for interrupts */
58109 int eMode, /* Type of checkpoint */
58110 int *pnLog, /* OUT: Final number of frames in log */
58111 int *pnCkpt /* OUT: Final number of checkpointed frames */
58112){
58113 int rc = SQLITE_OK0;
58114 if( pPager->pWal ){
58115 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
58116 (eMode==SQLITE_CHECKPOINT_PASSIVE0 ? 0 : pPager->xBusyHandler),
58117 pPager->pBusyHandlerArg,
58118 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
58119 pnLog, pnCkpt
58120 );
58121 sqlite3PagerResetLockTimeout(pPager);
58122 }
58123 return rc;
58124}
58125
58126SQLITE_PRIVATEstatic int sqlite3PagerWalCallback(Pager *pPager){
58127 return sqlite3WalCallback(pPager->pWal);
58128}
58129
58130/*
58131** Return true if the underlying VFS for the given pager supports the
58132** primitives necessary for write-ahead logging.
58133*/
58134SQLITE_PRIVATEstatic int sqlite3PagerWalSupported(Pager *pPager){
58135 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
58136 if( pPager->noLock ) return 0;
58137 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
58138}
58139
58140/*
58141** Attempt to take an exclusive lock on the database file. If a PENDING lock
58142** is obtained instead, immediately release it.
58143*/
58144static int pagerExclusiveLock(Pager *pPager){
58145 int rc; /* Return code */
58146
58147 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
58148 rc = pagerLockDb(pPager, EXCLUSIVE_LOCK4);
58149 if( rc!=SQLITE_OK0 ){
58150 /* If the attempt to grab the exclusive lock failed, release the
58151 ** pending lock that may have been obtained instead. */
58152 pagerUnlockDb(pPager, SHARED_LOCK1);
58153 }
58154
58155 return rc;
58156}
58157
58158/*
58159** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
58160** exclusive-locking mode when this function is called, take an EXCLUSIVE
58161** lock on the database file and use heap-memory to store the wal-index
58162** in. Otherwise, use the normal shared-memory.
58163*/
58164static int pagerOpenWal(Pager *pPager){
58165 int rc = SQLITE_OK0;
58166
58167 assert( pPager->pWal==0 && pPager->tempFile==0 )((void) (0));
58168 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK )((void) (0));
58169
58170 /* If the pager is already in exclusive-mode, the WAL module will use
58171 ** heap-memory for the wal-index instead of the VFS shared-memory
58172 ** implementation. Take the exclusive lock now, before opening the WAL
58173 ** file, to make sure this is safe.
58174 */
58175 if( pPager->exclusiveMode ){
58176 rc = pagerExclusiveLock(pPager);
58177 }
58178
58179 /* Open the connection to the log file. If this operation fails,
58180 ** (e.g. due to malloc() failure), return an error code.
58181 */
58182 if( rc==SQLITE_OK0 ){
58183 rc = sqlite3WalOpen(pPager->pVfs,
58184 pPager->fd, pPager->zWal, pPager->exclusiveMode,
58185 pPager->journalSizeLimit, &pPager->pWal
58186 );
58187 }
58188 pagerFixMaplimit(pPager);
58189
58190 return rc;
58191}
58192
58193
58194/*
58195** The caller must be holding a SHARED lock on the database file to call
58196** this function.
58197**
58198** If the pager passed as the first argument is open on a real database
58199** file (not a temp file or an in-memory database), and the WAL file
58200** is not already open, make an attempt to open it now. If successful,
58201** return SQLITE_OK. If an error occurs or the VFS used by the pager does
58202** not support the xShmXXX() methods, return an error code. *pbOpen is
58203** not modified in either case.
58204**
58205** If the pager is open on a temp-file (or in-memory database), or if
58206** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
58207** without doing anything.
58208*/
58209SQLITE_PRIVATEstatic int sqlite3PagerOpenWal(
58210 Pager *pPager, /* Pager object */
58211 int *pbOpen /* OUT: Set to true if call is a no-op */
58212){
58213 int rc = SQLITE_OK0; /* Return code */
58214
58215 assert( assert_pager_state(pPager) )((void) (0));
58216 assert( pPager->eState==PAGER_OPEN || pbOpen )((void) (0));
58217 assert( pPager->eState==PAGER_READER || !pbOpen )((void) (0));
58218 assert( pbOpen==0 || *pbOpen==0 )((void) (0));
58219 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) )((void) (0));
58220
58221 if( !pPager->tempFile && !pPager->pWal ){
58222 if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN14;
58223
58224 /* Close any rollback journal previously open */
58225 sqlite3OsClose(pPager->jfd);
58226
58227 rc = pagerOpenWal(pPager);
58228 if( rc==SQLITE_OK0 ){
58229 pPager->journalMode = PAGER_JOURNALMODE_WAL5;
58230 pPager->eState = PAGER_OPEN0;
58231 }
58232 }else{
58233 *pbOpen = 1;
58234 }
58235
58236 return rc;
58237}
58238
58239/*
58240** This function is called to close the connection to the log file prior
58241** to switching from WAL to rollback mode.
58242**
58243** Before closing the log file, this function attempts to take an
58244** EXCLUSIVE lock on the database file. If this cannot be obtained, an
58245** error (SQLITE_BUSY) is returned and the log connection is not closed.
58246** If successful, the EXCLUSIVE lock is not released before returning.
58247*/
58248SQLITE_PRIVATEstatic int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){
58249 int rc = SQLITE_OK0;
58250
58251 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL )((void) (0));
58252
58253 /* If the log file is not already open, but does exist in the file-system,
58254 ** it may need to be checkpointed before the connection can switch to
58255 ** rollback mode. Open it now so this can happen.
58256 */
58257 if( !pPager->pWal ){
58258 int logexists = 0;
58259 rc = pagerLockDb(pPager, SHARED_LOCK1);
58260 if( rc==SQLITE_OK0 ){
58261 rc = sqlite3OsAccess(
58262 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS0, &logexists
58263 );
58264 }
58265 if( rc==SQLITE_OK0 && logexists ){
58266 rc = pagerOpenWal(pPager);
58267 }
58268 }
58269
58270 /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
58271 ** the database file, the log and log-summary files will be deleted.
58272 */
58273 if( rc==SQLITE_OK0 && pPager->pWal ){
58274 rc = pagerExclusiveLock(pPager);
58275 if( rc==SQLITE_OK0 ){
58276 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
58277 pPager->pageSize, (u8*)pPager->pTmpSpace);
58278 pPager->pWal = 0;
58279 pagerFixMaplimit(pPager);
58280 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK1);
58281 }
58282 }
58283 return rc;
58284}
58285
58286#ifdef SQLITE_ENABLE_SNAPSHOT
58287/*
58288** If this is a WAL database, obtain a snapshot handle for the snapshot
58289** currently open. Otherwise, return an error.
58290*/
58291SQLITE_PRIVATEstatic int sqlite3PagerSnapshotGet(Pager *pPager, sqlite3_snapshot **ppSnapshot){
58292 int rc = SQLITE_ERROR1;
58293 if( pPager->pWal ){
58294 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
58295 }
58296 return rc;
58297}
58298
58299/*
58300** If this is a WAL database, store a pointer to pSnapshot. Next time a
58301** read transaction is opened, attempt to read from the snapshot it
58302** identifies. If this is not a WAL database, return an error.
58303*/
58304SQLITE_PRIVATEstatic int sqlite3PagerSnapshotOpen(Pager *pPager, sqlite3_snapshot *pSnapshot){
58305 int rc = SQLITE_OK0;
58306 if( pPager->pWal ){
58307 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
58308 }else{
58309 rc = SQLITE_ERROR1;
58310 }
58311 return rc;
58312}
58313
58314/*
58315** If this is a WAL database, call sqlite3WalSnapshotRecover(). If this
58316** is not a WAL database, return an error.
58317*/
58318SQLITE_PRIVATEstatic int sqlite3PagerSnapshotRecover(Pager *pPager){
58319 int rc;
58320 if( pPager->pWal ){
58321 rc = sqlite3WalSnapshotRecover(pPager->pWal);
58322 }else{
58323 rc = SQLITE_ERROR1;
58324 }
58325 return rc;
58326}
58327
58328/*
58329** The caller currently has a read transaction open on the database.
58330** If this is not a WAL database, SQLITE_ERROR is returned. Otherwise,
58331** this function takes a SHARED lock on the CHECKPOINTER slot and then
58332** checks if the snapshot passed as the second argument is still
58333** available. If so, SQLITE_OK is returned.
58334**
58335** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
58336** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
58337** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
58338** lock is released before returning.
58339*/
58340SQLITE_PRIVATEstatic int sqlite3PagerSnapshotCheck(Pager *pPager, sqlite3_snapshot *pSnapshot){
58341 int rc;
58342 if( pPager->pWal ){
58343 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
58344 }else{
58345 rc = SQLITE_ERROR1;
58346 }
58347 return rc;
58348}
58349
58350/*
58351** Release a lock obtained by an earlier successful call to
58352** sqlite3PagerSnapshotCheck().
58353*/
58354SQLITE_PRIVATEstatic void sqlite3PagerSnapshotUnlock(Pager *pPager){
58355 assert( pPager->pWal )((void) (0));
58356 sqlite3WalSnapshotUnlock(pPager->pWal);
58357}
58358
58359#endif /* SQLITE_ENABLE_SNAPSHOT */
58360#endif /* !SQLITE_OMIT_WAL */
58361
58362#ifdef SQLITE_ENABLE_ZIPVFS
58363/*
58364** A read-lock must be held on the pager when this function is called. If
58365** the pager is in WAL mode and the WAL file currently contains one or more
58366** frames, return the size in bytes of the page images stored within the
58367** WAL frames. Otherwise, if this is not a WAL database or the WAL file
58368** is empty, return 0.
58369*/
58370SQLITE_PRIVATEstatic int sqlite3PagerWalFramesize(Pager *pPager){
58371 assert( pPager->eState>=PAGER_READER )((void) (0));
58372 return sqlite3WalFramesize(pPager->pWal);
58373}
58374#endif
58375
58376#endif /* SQLITE_OMIT_DISKIO */
58377
58378/************** End of pager.c ***********************************************/
58379/************** Begin file wal.c *********************************************/
58380/*
58381** 2010 February 1
58382**
58383** The author disclaims copyright to this source code. In place of
58384** a legal notice, here is a blessing:
58385**
58386** May you do good and not evil.
58387** May you find forgiveness for yourself and forgive others.
58388** May you share freely, never taking more than you give.
58389**
58390*************************************************************************
58391**
58392** This file contains the implementation of a write-ahead log (WAL) used in
58393** "journal_mode=WAL" mode.
58394**
58395** WRITE-AHEAD LOG (WAL) FILE FORMAT
58396**
58397** A WAL file consists of a header followed by zero or more "frames".
58398** Each frame records the revised content of a single page from the
58399** database file. All changes to the database are recorded by writing
58400** frames into the WAL. Transactions commit when a frame is written that
58401** contains a commit marker. A single WAL can and usually does record
58402** multiple transactions. Periodically, the content of the WAL is
58403** transferred back into the database file in an operation called a
58404** "checkpoint".
58405**
58406** A single WAL file can be used multiple times. In other words, the
58407** WAL can fill up with frames and then be checkpointed and then new
58408** frames can overwrite the old ones. A WAL always grows from beginning
58409** toward the end. Checksums and counters attached to each frame are
58410** used to determine which frames within the WAL are valid and which
58411** are leftovers from prior checkpoints.
58412**
58413** The WAL header is 32 bytes in size and consists of the following eight
58414** big-endian 32-bit unsigned integer values:
58415**
58416** 0: Magic number. 0x377f0682 or 0x377f0683
58417** 4: File format version. Currently 3007000
58418** 8: Database page size. Example: 1024
58419** 12: Checkpoint sequence number
58420** 16: Salt-1, random integer incremented with each checkpoint
58421** 20: Salt-2, a different random integer changing with each ckpt
58422** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
58423** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
58424**
58425** Immediately following the wal-header are zero or more frames. Each
58426** frame consists of a 24-byte frame-header followed by a <page-size> bytes
58427** of page data. The frame-header is six big-endian 32-bit unsigned
58428** integer values, as follows:
58429**
58430** 0: Page number.
58431** 4: For commit records, the size of the database image in pages
58432** after the commit. For all other records, zero.
58433** 8: Salt-1 (copied from the header)
58434** 12: Salt-2 (copied from the header)
58435** 16: Checksum-1.
58436** 20: Checksum-2.
58437**
58438** A frame is considered valid if and only if the following conditions are
58439** true:
58440**
58441** (1) The salt-1 and salt-2 values in the frame-header match
58442** salt values in the wal-header
58443**
58444** (2) The checksum values in the final 8 bytes of the frame-header
58445** exactly match the checksum computed consecutively on the
58446** WAL header and the first 8 bytes and the content of all frames
58447** up to and including the current frame.
58448**
58449** The checksum is computed using 32-bit big-endian integers if the
58450** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
58451** is computed using little-endian if the magic number is 0x377f0682.
58452** The checksum values are always stored in the frame header in a
58453** big-endian format regardless of which byte order is used to compute
58454** the checksum. The checksum is computed by interpreting the input as
58455** an even number of unsigned 32-bit integers: x[0] through x[N]. The
58456** algorithm used for the checksum is as follows:
58457**
58458** for i from 0 to n-1 step 2:
58459** s0 += x[i] + s1;
58460** s1 += x[i+1] + s0;
58461** endfor
58462**
58463** Note that s0 and s1 are both weighted checksums using fibonacci weights
58464** in reverse order (the largest fibonacci weight occurs on the first element
58465** of the sequence being summed.) The s1 value spans all 32-bit
58466** terms of the sequence whereas s0 omits the final term.
58467**
58468** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
58469** WAL is transferred into the database, then the database is VFS.xSync-ed.
58470** The VFS.xSync operations serve as write barriers - all writes launched
58471** before the xSync must complete before any write that launches after the
58472** xSync begins.
58473**
58474** After each checkpoint, the salt-1 value is incremented and the salt-2
58475** value is randomized. This prevents old and new frames in the WAL from
58476** being considered valid at the same time and being checkpointing together
58477** following a crash.
58478**
58479** READER ALGORITHM
58480**
58481** To read a page from the database (call it page number P), a reader
58482** first checks the WAL to see if it contains page P. If so, then the
58483** last valid instance of page P that is a followed by a commit frame
58484** or is a commit frame itself becomes the value read. If the WAL
58485** contains no copies of page P that are valid and which are a commit
58486** frame or are followed by a commit frame, then page P is read from
58487** the database file.
58488**
58489** To start a read transaction, the reader records the index of the last
58490** valid frame in the WAL. The reader uses this recorded "mxFrame" value
58491** for all subsequent read operations. New transactions can be appended
58492** to the WAL, but as long as the reader uses its original mxFrame value
58493** and ignores the newly appended content, it will see a consistent snapshot
58494** of the database from a single point in time. This technique allows
58495** multiple concurrent readers to view different versions of the database
58496** content simultaneously.
58497**
58498** The reader algorithm in the previous paragraphs works correctly, but
58499** because frames for page P can appear anywhere within the WAL, the
58500** reader has to scan the entire WAL looking for page P frames. If the
58501** WAL is large (multiple megabytes is typical) that scan can be slow,
58502** and read performance suffers. To overcome this problem, a separate
58503** data structure called the wal-index is maintained to expedite the
58504** search for frames of a particular page.
58505**
58506** WAL-INDEX FORMAT
58507**
58508** Conceptually, the wal-index is shared memory, though VFS implementations
58509** might choose to implement the wal-index using a mmapped file. Because
58510** the wal-index is shared memory, SQLite does not support journal_mode=WAL
58511** on a network filesystem. All users of the database must be able to
58512** share memory.
58513**
58514** In the default unix and windows implementation, the wal-index is a mmapped
58515** file whose name is the database name with a "-shm" suffix added. For that
58516** reason, the wal-index is sometimes called the "shm" file.
58517**
58518** The wal-index is transient. After a crash, the wal-index can (and should
58519** be) reconstructed from the original WAL file. In fact, the VFS is required
58520** to either truncate or zero the header of the wal-index when the last
58521** connection to it closes. Because the wal-index is transient, it can
58522** use an architecture-specific format; it does not have to be cross-platform.
58523** Hence, unlike the database and WAL file formats which store all values
58524** as big endian, the wal-index can store multi-byte values in the native
58525** byte order of the host computer.
58526**
58527** The purpose of the wal-index is to answer this question quickly: Given
58528** a page number P and a maximum frame index M, return the index of the
58529** last frame in the wal before frame M for page P in the WAL, or return
58530** NULL if there are no frames for page P in the WAL prior to M.
58531**
58532** The wal-index consists of a header region, followed by an one or
58533** more index blocks.
58534**
58535** The wal-index header contains the total number of frames within the WAL
58536** in the mxFrame field.
58537**
58538** Each index block except for the first contains information on
58539** HASHTABLE_NPAGE frames. The first index block contains information on
58540** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
58541** HASHTABLE_NPAGE are selected so that together the wal-index header and
58542** first index block are the same size as all other index blocks in the
58543** wal-index.
58544**
58545** Each index block contains two sections, a page-mapping that contains the
58546** database page number associated with each wal frame, and a hash-table
58547** that allows readers to query an index block for a specific page number.
58548** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
58549** for the first index block) 32-bit page numbers. The first entry in the
58550** first index-block contains the database page number corresponding to the
58551** first frame in the WAL file. The first entry in the second index block
58552** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
58553** the log, and so on.
58554**
58555** The last index block in a wal-index usually contains less than the full
58556** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
58557** depending on the contents of the WAL file. This does not change the
58558** allocated size of the page-mapping array - the page-mapping array merely
58559** contains unused entries.
58560**
58561** Even without using the hash table, the last frame for page P
58562** can be found by scanning the page-mapping sections of each index block
58563** starting with the last index block and moving toward the first, and
58564** within each index block, starting at the end and moving toward the
58565** beginning. The first entry that equals P corresponds to the frame
58566** holding the content for that page.
58567**
58568** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
58569** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
58570** hash table for each page number in the mapping section, so the hash
58571** table is never more than half full. The expected number of collisions
58572** prior to finding a match is 1. Each entry of the hash table is an
58573** 1-based index of an entry in the mapping section of the same
58574** index block. Let K be the 1-based index of the largest entry in
58575** the mapping section. (For index blocks other than the last, K will
58576** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
58577** K will be (mxFrame%HASHTABLE_NPAGE).) Unused slots of the hash table
58578** contain a value of 0.
58579**
58580** To look for page P in the hash table, first compute a hash iKey on
58581** P as follows:
58582**
58583** iKey = (P * 383) % HASHTABLE_NSLOT
58584**
58585** Then start scanning entries of the hash table, starting with iKey
58586** (wrapping around to the beginning when the end of the hash table is
58587** reached) until an unused hash slot is found. Let the first unused slot
58588** be at index iUnused. (iUnused might be less than iKey if there was
58589** wrap-around.) Because the hash table is never more than half full,
58590** the search is guaranteed to eventually hit an unused entry. Let
58591** iMax be the value between iKey and iUnused, closest to iUnused,
58592** where aHash[iMax]==P. If there is no iMax entry (if there exists
58593** no hash slot such that aHash[i]==p) then page P is not in the
58594** current index block. Otherwise the iMax-th mapping entry of the
58595** current index block corresponds to the last entry that references
58596** page P.
58597**
58598** A hash search begins with the last index block and moves toward the
58599** first index block, looking for entries corresponding to page P. On
58600** average, only two or three slots in each index block need to be
58601** examined in order to either find the last entry for page P, or to
58602** establish that no such entry exists in the block. Each index block
58603** holds over 4000 entries. So two or three index blocks are sufficient
58604** to cover a typical 10 megabyte WAL file, assuming 1K pages. 8 or 10
58605** comparisons (on average) suffice to either locate a frame in the
58606** WAL or to establish that the frame does not exist in the WAL. This
58607** is much faster than scanning the entire 10MB WAL.
58608**
58609** Note that entries are added in order of increasing K. Hence, one
58610** reader might be using some value K0 and a second reader that started
58611** at a later time (after additional transactions were added to the WAL
58612** and to the wal-index) might be using a different value K1, where K1>K0.
58613** Both readers can use the same hash table and mapping section to get
58614** the correct result. There may be entries in the hash table with
58615** K>K0 but to the first reader, those entries will appear to be unused
58616** slots in the hash table and so the first reader will get an answer as
58617** if no values greater than K0 had ever been inserted into the hash table
58618** in the first place - which is what reader one wants. Meanwhile, the
58619** second reader using K1 will see additional values that were inserted
58620** later, which is exactly what reader two wants.
58621**
58622** When a rollback occurs, the value of K is decreased. Hash table entries
58623** that correspond to frames greater than the new K value are removed
58624** from the hash table at this point.
58625*/
58626#ifndef SQLITE_OMIT_WAL
58627
58628/* #include "wal.h" */
58629
58630/*
58631** Trace output macros
58632*/
58633#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
58634SQLITE_PRIVATEstatic int sqlite3WalTrace = 0;
58635# define WALTRACE(X) if(sqlite3WalTrace) sqlite3DebugPrintf X
58636#else
58637# define WALTRACE(X)
58638#endif
58639
58640/*
58641** WAL mode depends on atomic aligned 32-bit loads and stores in a few
58642** places. The following macros try to make this explicit.
58643*/
58644#if GCC_VESRION>=5004000
58645# define AtomicLoad(PTR)(*(PTR)) __atomic_load_n((PTR),__ATOMIC_RELAXED0)
58646# define AtomicStore(PTR,VAL)(*(PTR) = (VAL)) __atomic_store_n((PTR),(VAL),__ATOMIC_RELAXED0)
58647#else
58648# define AtomicLoad(PTR)(*(PTR)) (*(PTR))
58649# define AtomicStore(PTR,VAL)(*(PTR) = (VAL)) (*(PTR) = (VAL))
58650#endif
58651
58652/*
58653** The maximum (and only) versions of the wal and wal-index formats
58654** that may be interpreted by this version of SQLite.
58655**
58656** If a client begins recovering a WAL file and finds that (a) the checksum
58657** values in the wal-header are correct and (b) the version field is not
58658** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
58659**
58660** Similarly, if a client successfully reads a wal-index header (i.e. the
58661** checksum test is successful) and finds that the version field is not
58662** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
58663** returns SQLITE_CANTOPEN.
58664*/
58665#define WAL_MAX_VERSION3007000 3007000
58666#define WALINDEX_MAX_VERSION3007000 3007000
58667
58668/*
58669** Index numbers for various locking bytes. WAL_NREADER is the number
58670** of available reader locks and should be at least 3. The default
58671** is SQLITE_SHM_NLOCK==8 and WAL_NREADER==5.
58672**
58673** Technically, the various VFSes are free to implement these locks however
58674** they see fit. However, compatibility is encouraged so that VFSes can
58675** interoperate. The standard implemention used on both unix and windows
58676** is for the index number to indicate a byte offset into the
58677** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
58678** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
58679** should be 120) is the location in the shm file for the first locking
58680** byte.
58681*/
58682#define WAL_WRITE_LOCK0 0
58683#define WAL_ALL_BUT_WRITE1 1
58684#define WAL_CKPT_LOCK1 1
58685#define WAL_RECOVER_LOCK2 2
58686#define WAL_READ_LOCK(I)(3+(I)) (3+(I))
58687#define WAL_NREADER(8 -3) (SQLITE_SHM_NLOCK8-3)
58688
58689
58690/* Object declarations */
58691typedef struct WalIndexHdr WalIndexHdr;
58692typedef struct WalIterator WalIterator;
58693typedef struct WalCkptInfo WalCkptInfo;
58694
58695
58696/*
58697** The following object holds a copy of the wal-index header content.
58698**
58699** The actual header in the wal-index consists of two copies of this
58700** object followed by one instance of the WalCkptInfo object.
58701** For all versions of SQLite through 3.10.0 and probably beyond,
58702** the locking bytes (WalCkptInfo.aLock) start at offset 120 and
58703** the total header size is 136 bytes.
58704**
58705** The szPage value can be any power of 2 between 512 and 32768, inclusive.
58706** Or it can be 1 to represent a 65536-byte page. The latter case was
58707** added in 3.7.1 when support for 64K pages was added.
58708*/
58709struct WalIndexHdr {
58710 u32 iVersion; /* Wal-index version */
58711 u32 unused; /* Unused (padding) field */
58712 u32 iChange; /* Counter incremented each transaction */
58713 u8 isInit; /* 1 when initialized */
58714 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
58715 u16 szPage; /* Database page size in bytes. 1==64K */
58716 u32 mxFrame; /* Index of last valid frame in the WAL */
58717 u32 nPage; /* Size of database in pages */
58718 u32 aFrameCksum[2]; /* Checksum of last frame in log */
58719 u32 aSalt[2]; /* Two salt values copied from WAL header */
58720 u32 aCksum[2]; /* Checksum over all prior fields */
58721};
58722
58723/*
58724** A copy of the following object occurs in the wal-index immediately
58725** following the second copy of the WalIndexHdr. This object stores
58726** information used by checkpoint.
58727**
58728** nBackfill is the number of frames in the WAL that have been written
58729** back into the database. (We call the act of moving content from WAL to
58730** database "backfilling".) The nBackfill number is never greater than
58731** WalIndexHdr.mxFrame. nBackfill can only be increased by threads
58732** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
58733** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
58734** mxFrame back to zero when the WAL is reset.
58735**
58736** nBackfillAttempted is the largest value of nBackfill that a checkpoint
58737** has attempted to achieve. Normally nBackfill==nBackfillAtempted, however
58738** the nBackfillAttempted is set before any backfilling is done and the
58739** nBackfill is only set after all backfilling completes. So if a checkpoint
58740** crashes, nBackfillAttempted might be larger than nBackfill. The
58741** WalIndexHdr.mxFrame must never be less than nBackfillAttempted.
58742**
58743** The aLock[] field is a set of bytes used for locking. These bytes should
58744** never be read or written.
58745**
58746** There is one entry in aReadMark[] for each reader lock. If a reader
58747** holds read-lock K, then the value in aReadMark[K] is no greater than
58748** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
58749** for any aReadMark[] means that entry is unused. aReadMark[0] is
58750** a special case; its value is never used and it exists as a place-holder
58751** to avoid having to offset aReadMark[] indexs by one. Readers holding
58752** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
58753** directly from the database.
58754**
58755** The value of aReadMark[K] may only be changed by a thread that
58756** is holding an exclusive lock on WAL_READ_LOCK(K). Thus, the value of
58757** aReadMark[K] cannot changed while there is a reader is using that mark
58758** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
58759**
58760** The checkpointer may only transfer frames from WAL to database where
58761** the frame numbers are less than or equal to every aReadMark[] that is
58762** in use (that is, every aReadMark[j] for which there is a corresponding
58763** WAL_READ_LOCK(j)). New readers (usually) pick the aReadMark[] with the
58764** largest value and will increase an unused aReadMark[] to mxFrame if there
58765** is not already an aReadMark[] equal to mxFrame. The exception to the
58766** previous sentence is when nBackfill equals mxFrame (meaning that everything
58767** in the WAL has been backfilled into the database) then new readers
58768** will choose aReadMark[0] which has value 0 and hence such reader will
58769** get all their all content directly from the database file and ignore
58770** the WAL.
58771**
58772** Writers normally append new frames to the end of the WAL. However,
58773** if nBackfill equals mxFrame (meaning that all WAL content has been
58774** written back into the database) and if no readers are using the WAL
58775** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
58776** the writer will first "reset" the WAL back to the beginning and start
58777** writing new content beginning at frame 1.
58778**
58779** We assume that 32-bit loads are atomic and so no locks are needed in
58780** order to read from any aReadMark[] entries.
58781*/
58782struct WalCkptInfo {
58783 u32 nBackfill; /* Number of WAL frames backfilled into DB */
58784 u32 aReadMark[WAL_NREADER(8 -3)]; /* Reader marks */
58785 u8 aLock[SQLITE_SHM_NLOCK8]; /* Reserved space for locks */
58786 u32 nBackfillAttempted; /* WAL frames perhaps written, or maybe not */
58787 u32 notUsed0; /* Available for future enhancements */
58788};
58789#define READMARK_NOT_USED0xffffffff 0xffffffff
58790
58791
58792/* A block of WALINDEX_LOCK_RESERVED bytes beginning at
58793** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
58794** only support mandatory file-locks, we do not read or write data
58795** from the region of the file on which locks are applied.
58796*/
58797#define WALINDEX_LOCK_OFFSET(sizeof(WalIndexHdr)*2+__builtin_offsetof(WalCkptInfo, aLock)
)
(sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock)__builtin_offsetof(WalCkptInfo, aLock))
58798#define WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo)) (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
58799
58800/* Size of header before each frame in wal */
58801#define WAL_FRAME_HDRSIZE24 24
58802
58803/* Size of write ahead log header, including checksum. */
58804#define WAL_HDRSIZE32 32
58805
58806/* WAL magic value. Either this value, or the same value with the least
58807** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
58808** big-endian format in the first 4 bytes of a WAL file.
58809**
58810** If the LSB is set, then the checksums for each frame within the WAL
58811** file are calculated by treating all data as an array of 32-bit
58812** big-endian words. Otherwise, they are calculated by interpreting
58813** all data as 32-bit little-endian words.
58814*/
58815#define WAL_MAGIC0x377f0682 0x377f0682
58816
58817/*
58818** Return the offset of frame iFrame in the write-ahead log file,
58819** assuming a database page size of szPage bytes. The offset returned
58820** is to the start of the write-ahead log frame-header.
58821*/
58822#define walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) ) ( \
58823 WAL_HDRSIZE32 + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE24) \
58824)
58825
58826/*
58827** An open write-ahead log file is represented by an instance of the
58828** following object.
58829*/
58830struct Wal {
58831 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
58832 sqlite3_file *pDbFd; /* File handle for the database file */
58833 sqlite3_file *pWalFd; /* File handle for WAL file */
58834 u32 iCallback; /* Value to pass to log callback (or 0) */
58835 i64 mxWalSize; /* Truncate WAL to this size upon reset */
58836 int nWiData; /* Size of array apWiData */
58837 int szFirstBlock; /* Size of first block written to WAL file */
58838 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
58839 u32 szPage; /* Database page size */
58840 i16 readLock; /* Which read lock is being held. -1 for none */
58841 u8 syncFlags; /* Flags to use to sync header writes */
58842 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
58843 u8 writeLock; /* True if in a write transaction */
58844 u8 ckptLock; /* True if holding a checkpoint lock */
58845 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
58846 u8 truncateOnCommit; /* True to truncate WAL file on commit */
58847 u8 syncHeader; /* Fsync the WAL header if true */
58848 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
58849 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
58850 WalIndexHdr hdr; /* Wal-index header for current transaction */
58851 u32 minFrame; /* Ignore wal frames before this one */
58852 u32 iReCksum; /* On commit, recalculate checksums from here */
58853 const char *zWalName; /* Name of WAL file */
58854 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
58855#ifdef SQLITE_DEBUG
58856 u8 lockError; /* True if a locking error has occurred */
58857#endif
58858#ifdef SQLITE_ENABLE_SNAPSHOT
58859 WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */
58860#endif
58861};
58862
58863/*
58864** Candidate values for Wal.exclusiveMode.
58865*/
58866#define WAL_NORMAL_MODE0 0
58867#define WAL_EXCLUSIVE_MODE1 1
58868#define WAL_HEAPMEMORY_MODE2 2
58869
58870/*
58871** Possible values for WAL.readOnly
58872*/
58873#define WAL_RDWR0 0 /* Normal read/write connection */
58874#define WAL_RDONLY1 1 /* The WAL file is readonly */
58875#define WAL_SHM_RDONLY2 2 /* The SHM file is readonly */
58876
58877/*
58878** Each page of the wal-index mapping contains a hash-table made up of
58879** an array of HASHTABLE_NSLOT elements of the following type.
58880*/
58881typedef u16 ht_slot;
58882
58883/*
58884** This structure is used to implement an iterator that loops through
58885** all frames in the WAL in database page order. Where two or more frames
58886** correspond to the same database page, the iterator visits only the
58887** frame most recently written to the WAL (in other words, the frame with
58888** the largest index).
58889**
58890** The internals of this structure are only accessed by:
58891**
58892** walIteratorInit() - Create a new iterator,
58893** walIteratorNext() - Step an iterator,
58894** walIteratorFree() - Free an iterator.
58895**
58896** This functionality is used by the checkpoint code (see walCheckpoint()).
58897*/
58898struct WalIterator {
58899 int iPrior; /* Last result returned from the iterator */
58900 int nSegment; /* Number of entries in aSegment[] */
58901 struct WalSegment {
58902 int iNext; /* Next slot in aIndex[] not yet returned */
58903 ht_slot *aIndex; /* i0, i1, i2... such that aPgno[iN] ascend */
58904 u32 *aPgno; /* Array of page numbers. */
58905 int nEntry; /* Nr. of entries in aPgno[] and aIndex[] */
58906 int iZero; /* Frame number associated with aPgno[0] */
58907 } aSegment[1]; /* One for every 32KB page in the wal-index */
58908};
58909
58910/*
58911** Define the parameters of the hash tables in the wal-index file. There
58912** is a hash-table following every HASHTABLE_NPAGE page numbers in the
58913** wal-index.
58914**
58915** Changing any of these constants will alter the wal-index format and
58916** create incompatibilities.
58917*/
58918#define HASHTABLE_NPAGE4096 4096 /* Must be power of 2 */
58919#define HASHTABLE_HASH_1383 383 /* Should be prime */
58920#define HASHTABLE_NSLOT(4096*2) (HASHTABLE_NPAGE4096*2) /* Must be a power of 2 */
58921
58922/*
58923** The block of page numbers associated with the first hash-table in a
58924** wal-index is smaller than usual. This is so that there is a complete
58925** hash-table on each aligned 32KB page of the wal-index.
58926*/
58927#define HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
(HASHTABLE_NPAGE4096 - (WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32)))
58928
58929/* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
58930#define WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ) ( \
58931 sizeof(ht_slot)*HASHTABLE_NSLOT(4096*2) + HASHTABLE_NPAGE4096*sizeof(u32) \
58932)
58933
58934/*
58935** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
58936** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
58937** numbered from zero.
58938**
58939** If the wal-index is currently smaller the iPage pages then the size
58940** of the wal-index might be increased, but only if it is safe to do
58941** so. It is safe to enlarge the wal-index if pWal->writeLock is true
58942** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
58943**
58944** If this call is successful, *ppPage is set to point to the wal-index
58945** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
58946** then an SQLite error code is returned and *ppPage is set to 0.
58947*/
58948static SQLITE_NOINLINE__attribute__((noinline)) int walIndexPageRealloc(
58949 Wal *pWal, /* The WAL context */
58950 int iPage, /* The page we seek */
58951 volatile u32 **ppPage /* Write the page pointer here */
58952){
58953 int rc = SQLITE_OK0;
58954
58955 /* Enlarge the pWal->apWiData[] array if required */
58956 if( pWal->nWiData<=iPage ){
58957 sqlite3_int64 nByte = sizeof(u32*)*(iPage+1);
58958 volatile u32 **apNew;
58959 apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
58960 if( !apNew ){
58961 *ppPage = 0;
58962 return SQLITE_NOMEM_BKPT7;
58963 }
58964 memset((void*)&apNew[pWal->nWiData], 0,
58965 sizeof(u32*)*(iPage+1-pWal->nWiData));
58966 pWal->apWiData = apNew;
58967 pWal->nWiData = iPage+1;
58968 }
58969
58970 /* Request a pointer to the required page from the VFS */
58971 assert( pWal->apWiData[iPage]==0 )((void) (0));
58972 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 ){
58973 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ));
58974 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT7;
58975 }else{
58976 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ),
58977 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
58978 );
58979 assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 )((void) (0));
58980 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
58981 if( (rc&0xff)==SQLITE_READONLY8 ){
58982 pWal->readOnly |= WAL_SHM_RDONLY2;
58983 if( rc==SQLITE_READONLY8 ){
58984 rc = SQLITE_OK0;
58985 }
58986 }
58987 }
58988
58989 *ppPage = pWal->apWiData[iPage];
58990 assert( iPage==0 || *ppPage || rc!=SQLITE_OK )((void) (0));
58991 return rc;
58992}
58993static int walIndexPage(
58994 Wal *pWal, /* The WAL context */
58995 int iPage, /* The page we seek */
58996 volatile u32 **ppPage /* Write the page pointer here */
58997){
58998 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
58999 return walIndexPageRealloc(pWal, iPage, ppPage);
59000 }
59001 return SQLITE_OK0;
59002}
59003
59004/*
59005** Return a pointer to the WalCkptInfo structure in the wal-index.
59006*/
59007static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
59008 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
59009 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
59010}
59011
59012/*
59013** Return a pointer to the WalIndexHdr structure in the wal-index.
59014*/
59015static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
59016 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
59017 return (volatile WalIndexHdr*)pWal->apWiData[0];
59018}
59019
59020/*
59021** The argument to this macro must be of type u32. On a little-endian
59022** architecture, it returns the u32 value that results from interpreting
59023** the 4 bytes as a big-endian value. On a big-endian architecture, it
59024** returns the value that would be produced by interpreting the 4 bytes
59025** of the input value as a little-endian integer.
59026*/
59027#define BYTESWAP32(x)( (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<
8) + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>
24) )
( \
59028 (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) \
59029 + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24) \
59030)
59031
59032/*
59033** Generate or extend an 8 byte checksum based on the data in
59034** array aByte[] and the initial values of aIn[0] and aIn[1] (or
59035** initial values of 0 and 0 if aIn==NULL).
59036**
59037** The checksum is written back into aOut[] before returning.
59038**
59039** nByte must be a positive multiple of 8.
59040*/
59041static void walChecksumBytes(
59042 int nativeCksum, /* True for native byte-order, false for non-native */
59043 u8 *a, /* Content to be checksummed */
59044 int nByte, /* Bytes of content in a[]. Must be a multiple of 8. */
59045 const u32 *aIn, /* Initial checksum value input */
59046 u32 *aOut /* OUT: Final checksum value output */
59047){
59048 u32 s1, s2;
59049 u32 *aData = (u32 *)a;
59050 u32 *aEnd = (u32 *)&a[nByte];
59051
59052 if( aIn ){
59053 s1 = aIn[0];
59054 s2 = aIn[1];
59055 }else{
59056 s1 = s2 = 0;
59057 }
59058
59059 assert( nByte>=8 )((void) (0));
59060 assert( (nByte&0x00000007)==0 )((void) (0));
59061 assert( nByte<=65536 )((void) (0));
59062
59063 if( nativeCksum ){
59064 do {
59065 s1 += *aData++ + s2;
59066 s2 += *aData++ + s1;
59067 }while( aData<aEnd );
59068 }else{
59069 do {
59070 s1 += BYTESWAP32(aData[0])( (((aData[0])&0x000000FF)<<24) + (((aData[0])&
0x0000FF00)<<8) + (((aData[0])&0x00FF0000)>>8
) + (((aData[0])&0xFF000000)>>24) )
+ s2;
59071 s2 += BYTESWAP32(aData[1])( (((aData[1])&0x000000FF)<<24) + (((aData[1])&
0x0000FF00)<<8) + (((aData[1])&0x00FF0000)>>8
) + (((aData[1])&0xFF000000)>>24) )
+ s1;
59072 aData += 2;
59073 }while( aData<aEnd );
59074 }
59075
59076 aOut[0] = s1;
59077 aOut[1] = s2;
59078}
59079
59080static void walShmBarrier(Wal *pWal){
59081 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE2 ){
59082 sqlite3OsShmBarrier(pWal->pDbFd);
59083 }
59084}
59085
59086/*
59087** Write the header information in pWal->hdr into the wal-index.
59088**
59089** The checksum on pWal->hdr is updated before it is written.
59090*/
59091static void walIndexWriteHdr(Wal *pWal){
59092 volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
59093 const int nCksum = offsetof(WalIndexHdr, aCksum)__builtin_offsetof(WalIndexHdr, aCksum);
59094
59095 assert( pWal->writeLock )((void) (0));
59096 pWal->hdr.isInit = 1;
59097 pWal->hdr.iVersion = WALINDEX_MAX_VERSION3007000;
59098 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
59099 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
59100 walShmBarrier(pWal);
59101 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
59102}
59103
59104/*
59105** This function encodes a single frame header and writes it to a buffer
59106** supplied by the caller. A frame-header is made up of a series of
59107** 4-byte big-endian integers, as follows:
59108**
59109** 0: Page number.
59110** 4: For commit records, the size of the database image in pages
59111** after the commit. For all other records, zero.
59112** 8: Salt-1 (copied from the wal-header)
59113** 12: Salt-2 (copied from the wal-header)
59114** 16: Checksum-1.
59115** 20: Checksum-2.
59116*/
59117static void walEncodeFrame(
59118 Wal *pWal, /* The write-ahead log */
59119 u32 iPage, /* Database page number for frame */
59120 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
59121 u8 *aData, /* Pointer to page data */
59122 u8 *aFrame /* OUT: Write encoded frame here */
59123){
59124 int nativeCksum; /* True for native byte-order checksums */
59125 u32 *aCksum = pWal->hdr.aFrameCksum;
59126 assert( WAL_FRAME_HDRSIZE==24 )((void) (0));
59127 sqlite3Put4byte(&aFrame[0], iPage);
59128 sqlite3Put4byte(&aFrame[4], nTruncate);
59129 if( pWal->iReCksum==0 ){
59130 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
59131
59132 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0);
59133 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
59134 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
59135
59136 sqlite3Put4byte(&aFrame[16], aCksum[0]);
59137 sqlite3Put4byte(&aFrame[20], aCksum[1]);
59138 }else{
59139 memset(&aFrame[8], 0, 16);
59140 }
59141}
59142
59143/*
59144** Check to see if the frame with header in aFrame[] and content
59145** in aData[] is valid. If it is a valid frame, fill *piPage and
59146** *pnTruncate and return true. Return if the frame is not valid.
59147*/
59148static int walDecodeFrame(
59149 Wal *pWal, /* The write-ahead log */
59150 u32 *piPage, /* OUT: Database page number for frame */
59151 u32 *pnTruncate, /* OUT: New db size (or 0 if not commit) */
59152 u8 *aData, /* Pointer to page data (for checksum) */
59153 u8 *aFrame /* Frame data */
59154){
59155 int nativeCksum; /* True for native byte-order checksums */
59156 u32 *aCksum = pWal->hdr.aFrameCksum;
59157 u32 pgno; /* Page number of the frame */
59158 assert( WAL_FRAME_HDRSIZE==24 )((void) (0));
59159
59160 /* A frame is only valid if the salt values in the frame-header
59161 ** match the salt values in the wal-header.
59162 */
59163 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
59164 return 0;
59165 }
59166
59167 /* A frame is only valid if the page number is creater than zero.
59168 */
59169 pgno = sqlite3Get4byte(&aFrame[0]);
59170 if( pgno==0 ){
59171 return 0;
59172 }
59173
59174 /* A frame is only valid if a checksum of the WAL header,
59175 ** all prior frams, the first 16 bytes of this frame-header,
59176 ** and the frame-data matches the checksum in the last 8
59177 ** bytes of this frame-header.
59178 */
59179 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0);
59180 walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
59181 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
59182 if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
59183 || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
59184 ){
59185 /* Checksum failed. */
59186 return 0;
59187 }
59188
59189 /* If we reach this point, the frame is valid. Return the page number
59190 ** and the new database size.
59191 */
59192 *piPage = pgno;
59193 *pnTruncate = sqlite3Get4byte(&aFrame[4]);
59194 return 1;
59195}
59196
59197
59198#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
59199/*
59200** Names of locks. This routine is used to provide debugging output and is not
59201** a part of an ordinary build.
59202*/
59203static const char *walLockName(int lockIdx){
59204 if( lockIdx==WAL_WRITE_LOCK0 ){
59205 return "WRITE-LOCK";
59206 }else if( lockIdx==WAL_CKPT_LOCK1 ){
59207 return "CKPT-LOCK";
59208 }else if( lockIdx==WAL_RECOVER_LOCK2 ){
59209 return "RECOVER-LOCK";
59210 }else{
59211 static char zName[15];
59212 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
59213 lockIdx-WAL_READ_LOCK(0)(3+(0)));
59214 return zName;
59215 }
59216}
59217#endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
59218
59219
59220/*
59221** Set or release locks on the WAL. Locks are either shared or exclusive.
59222** A lock cannot be moved directly between shared and exclusive - it must go
59223** through the unlocked state first.
59224**
59225** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
59226*/
59227static int walLockShared(Wal *pWal, int lockIdx){
59228 int rc;
59229 if( pWal->exclusiveMode ) return SQLITE_OK0;
59230 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
59231 SQLITE_SHM_LOCK2 | SQLITE_SHM_SHARED4);
59232 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
59233 walLockName(lockIdx), rc ? "failed" : "ok"));
59234 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
59235 return rc;
59236}
59237static void walUnlockShared(Wal *pWal, int lockIdx){
59238 if( pWal->exclusiveMode ) return;
59239 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
59240 SQLITE_SHM_UNLOCK1 | SQLITE_SHM_SHARED4);
59241 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
59242}
59243static int walLockExclusive(Wal *pWal, int lockIdx, int n){
59244 int rc;
59245 if( pWal->exclusiveMode ) return SQLITE_OK0;
59246 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
59247 SQLITE_SHM_LOCK2 | SQLITE_SHM_EXCLUSIVE8);
59248 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
59249 walLockName(lockIdx), n, rc ? "failed" : "ok"));
59250 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
59251 return rc;
59252}
59253static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
59254 if( pWal->exclusiveMode ) return;
59255 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
59256 SQLITE_SHM_UNLOCK1 | SQLITE_SHM_EXCLUSIVE8);
59257 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
59258 walLockName(lockIdx), n));
59259}
59260
59261/*
59262** Compute a hash on a page number. The resulting hash value must land
59263** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
59264** the hash to the next value in the event of a collision.
59265*/
59266static int walHash(u32 iPage){
59267 assert( iPage>0 )((void) (0));
59268 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 )((void) (0));
59269 return (iPage*HASHTABLE_HASH_1383) & (HASHTABLE_NSLOT(4096*2)-1);
59270}
59271static int walNextHash(int iPriorHash){
59272 return (iPriorHash+1)&(HASHTABLE_NSLOT(4096*2)-1);
59273}
59274
59275/*
59276** An instance of the WalHashLoc object is used to describe the location
59277** of a page hash table in the wal-index. This becomes the return value
59278** from walHashGet().
59279*/
59280typedef struct WalHashLoc WalHashLoc;
59281struct WalHashLoc {
59282 volatile ht_slot *aHash; /* Start of the wal-index hash table */
59283 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
59284 u32 iZero; /* One less than the frame number of first indexed*/
59285};
59286
59287/*
59288** Return pointers to the hash table and page number array stored on
59289** page iHash of the wal-index. The wal-index is broken into 32KB pages
59290** numbered starting from 0.
59291**
59292** Set output variable pLoc->aHash to point to the start of the hash table
59293** in the wal-index file. Set pLoc->iZero to one less than the frame
59294** number of the first frame indexed by this hash table. If a
59295** slot in the hash table is set to N, it refers to frame number
59296** (pLoc->iZero+N) in the log.
59297**
59298** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the
59299** first frame indexed by the hash table, frame (pLoc->iZero+1).
59300*/
59301static int walHashGet(
59302 Wal *pWal, /* WAL handle */
59303 int iHash, /* Find the iHash'th table */
59304 WalHashLoc *pLoc /* OUT: Hash table location */
59305){
59306 int rc; /* Return code */
59307
59308 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
59309 assert( rc==SQLITE_OK || iHash>0 )((void) (0));
59310
59311 if( rc==SQLITE_OK0 ){
59312 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE4096];
59313 if( iHash==0 ){
59314 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32)];
59315 pLoc->iZero = 0;
59316 }else{
59317 pLoc->iZero = HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
+ (iHash-1)*HASHTABLE_NPAGE4096;
59318 }
59319 pLoc->aPgno = &pLoc->aPgno[-1];
59320 }
59321 return rc;
59322}
59323
59324/*
59325** Return the number of the wal-index page that contains the hash-table
59326** and page-number array that contain entries corresponding to WAL frame
59327** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
59328** are numbered starting from 0.
59329*/
59330static int walFramePage(u32 iFrame){
59331 int iHash = (iFrame+HASHTABLE_NPAGE4096-HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
-1) / HASHTABLE_NPAGE4096;
59332 assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)((void) (0))
59333 && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)((void) (0))
59334 && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))((void) (0))
59335 && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)((void) (0))
59336 && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))((void) (0))
59337 )((void) (0));
59338 return iHash;
59339}
59340
59341/*
59342** Return the page number associated with frame iFrame in this WAL.
59343*/
59344static u32 walFramePgno(Wal *pWal, u32 iFrame){
59345 int iHash = walFramePage(iFrame);
59346 if( iHash==0 ){
59347 return pWal->apWiData[0][WALINDEX_HDR_SIZE(sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32) + iFrame - 1];
59348 }
59349 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE(4096 - ((sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))/sizeof(u32
)))
)%HASHTABLE_NPAGE4096];
59350}
59351
59352/*
59353** Remove entries from the hash table that point to WAL slots greater
59354** than pWal->hdr.mxFrame.
59355**
59356** This function is called whenever pWal->hdr.mxFrame is decreased due
59357** to a rollback or savepoint.
59358**
59359** At most only the hash table containing pWal->hdr.mxFrame needs to be
59360** updated. Any later hash tables will be automatically cleared when
59361** pWal->hdr.mxFrame advances to the point where those hash tables are
59362** actually needed.
59363*/
59364static void walCleanupHash(Wal *pWal){
59365 WalHashLoc sLoc; /* Hash table location */
59366 int iLimit = 0; /* Zero values greater than this */
59367 int nByte; /* Number of bytes to zero in aPgno[] */
59368 int i; /* Used to iterate through aHash[] */
59369 int rc; /* Return code form walHashGet() */
59370
59371 assert( pWal->writeLock )((void) (0));
59372 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
59373 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
59374 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
59375
59376 if( pWal->hdr.mxFrame==0 ) return;
59377
59378 /* Obtain pointers to the hash-table and page-number array containing
59379 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
59380 ** that the page said hash-table and array reside on is already mapped.(1)
59381 */
59382 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) )((void) (0));
59383 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] )((void) (0));
59384 rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
59385 if( NEVER(rc)(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */
59386
59387 /* Zero all hash-table entries that correspond to frame numbers greater
59388 ** than pWal->hdr.mxFrame.
59389 */
59390 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
59391 assert( iLimit>0 )((void) (0));
59392 for(i=0; i<HASHTABLE_NSLOT(4096*2); i++){
59393 if( sLoc.aHash[i]>iLimit ){
59394 sLoc.aHash[i] = 0;
59395 }
59396 }
59397
59398 /* Zero the entries in the aPgno array that correspond to frames with
59399 ** frame numbers greater than pWal->hdr.mxFrame.
59400 */
59401 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]);
59402 memset((void *)&sLoc.aPgno[iLimit+1], 0, nByte);
59403
59404#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
59405 /* Verify that the every entry in the mapping region is still reachable
59406 ** via the hash table even after the cleanup.
59407 */
59408 if( iLimit ){
59409 int j; /* Loop counter */
59410 int iKey; /* Hash key */
59411 for(j=1; j<=iLimit; j++){
59412 for(iKey=walHash(sLoc.aPgno[j]);sLoc.aHash[iKey];iKey=walNextHash(iKey)){
59413 if( sLoc.aHash[iKey]==j ) break;
59414 }
59415 assert( sLoc.aHash[iKey]==j )((void) (0));
59416 }
59417 }
59418#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
59419}
59420
59421
59422/*
59423** Set an entry in the wal-index that will map database page number
59424** pPage into WAL frame iFrame.
59425*/
59426static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
59427 int rc; /* Return code */
59428 WalHashLoc sLoc; /* Wal-index hash table location */
59429
59430 rc = walHashGet(pWal, walFramePage(iFrame), &sLoc);
59431
59432 /* Assuming the wal-index file was successfully mapped, populate the
59433 ** page number array and hash table entry.
59434 */
59435 if( rc==SQLITE_OK0 ){
59436 int iKey; /* Hash table key */
59437 int idx; /* Value to write to hash-table slot */
59438 int nCollide; /* Number of hash collisions */
59439
59440 idx = iFrame - sLoc.iZero;
59441 assert( idx <= HASHTABLE_NSLOT/2 + 1 )((void) (0));
59442
59443 /* If this is the first entry to be added to this hash-table, zero the
59444 ** entire hash table and aPgno[] array before proceeding.
59445 */
59446 if( idx==1 ){
59447 int nByte = (int)((u8 *)&sLoc.aHash[HASHTABLE_NSLOT(4096*2)]
59448 - (u8 *)&sLoc.aPgno[1]);
59449 memset((void*)&sLoc.aPgno[1], 0, nByte);
59450 }
59451
59452 /* If the entry in aPgno[] is already set, then the previous writer
59453 ** must have exited unexpectedly in the middle of a transaction (after
59454 ** writing one or more dirty pages to the WAL to free up memory).
59455 ** Remove the remnants of that writers uncommitted transaction from
59456 ** the hash-table before writing any new entries.
59457 */
59458 if( sLoc.aPgno[idx] ){
59459 walCleanupHash(pWal);
59460 assert( !sLoc.aPgno[idx] )((void) (0));
59461 }
59462
59463 /* Write the aPgno[] array entry and the hash-table slot. */
59464 nCollide = idx;
59465 for(iKey=walHash(iPage); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
59466 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(59466);
59467 }
59468 sLoc.aPgno[idx] = iPage;
59469 sLoc.aHash[iKey] = (ht_slot)idx;
59470
59471#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
59472 /* Verify that the number of entries in the hash table exactly equals
59473 ** the number of entries in the mapping region.
59474 */
59475 {
59476 int i; /* Loop counter */
59477 int nEntry = 0; /* Number of entries in the hash table */
59478 for(i=0; i<HASHTABLE_NSLOT(4096*2); i++){ if( sLoc.aHash[i] ) nEntry++; }
59479 assert( nEntry==idx )((void) (0));
59480 }
59481
59482 /* Verify that the every entry in the mapping region is reachable
59483 ** via the hash table. This turns out to be a really, really expensive
59484 ** thing to check, so only do this occasionally - not on every
59485 ** iteration.
59486 */
59487 if( (idx&0x3ff)==0 ){
59488 int i; /* Loop counter */
59489 for(i=1; i<=idx; i++){
59490 for(iKey=walHash(sLoc.aPgno[i]);
59491 sLoc.aHash[iKey];
59492 iKey=walNextHash(iKey)){
59493 if( sLoc.aHash[iKey]==i ) break;
59494 }
59495 assert( sLoc.aHash[iKey]==i )((void) (0));
59496 }
59497 }
59498#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
59499 }
59500
59501
59502 return rc;
59503}
59504
59505
59506/*
59507** Recover the wal-index by reading the write-ahead log file.
59508**
59509** This routine first tries to establish an exclusive lock on the
59510** wal-index to prevent other threads/processes from doing anything
59511** with the WAL or wal-index while recovery is running. The
59512** WAL_RECOVER_LOCK is also held so that other threads will know
59513** that this thread is running recovery. If unable to establish
59514** the necessary locks, this routine returns SQLITE_BUSY.
59515*/
59516static int walIndexRecover(Wal *pWal){
59517 int rc; /* Return Code */
59518 i64 nSize; /* Size of log file */
59519 u32 aFrameCksum[2] = {0, 0};
59520 int iLock; /* Lock offset to lock for checkpoint */
59521
59522 /* Obtain an exclusive lock on all byte in the locking range not already
59523 ** locked by the caller. The caller is guaranteed to have locked the
59524 ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
59525 ** If successful, the same bytes that are locked here are unlocked before
59526 ** this function returns.
59527 */
59528 assert( pWal->ckptLock==1 || pWal->ckptLock==0 )((void) (0));
59529 assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 )((void) (0));
59530 assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE )((void) (0));
59531 assert( pWal->writeLock )((void) (0));
59532 iLock = WAL_ALL_BUT_WRITE1 + pWal->ckptLock;
59533 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)(3+(0))-iLock);
59534 if( rc==SQLITE_OK0 ){
59535 rc = walLockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
59536 if( rc!=SQLITE_OK0 ){
59537 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)(3+(0))-iLock);
59538 }
59539 }
59540 if( rc ){
59541 return rc;
59542 }
59543
59544 WALTRACE(("WAL%p: recovery begin...\n", pWal));
59545
59546 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
59547
59548 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
59549 if( rc!=SQLITE_OK0 ){
59550 goto recovery_error;
59551 }
59552
59553 if( nSize>WAL_HDRSIZE32 ){
59554 u8 aBuf[WAL_HDRSIZE32]; /* Buffer to load WAL header into */
59555 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
59556 int szFrame; /* Number of bytes in buffer aFrame[] */
59557 u8 *aData; /* Pointer to data part of aFrame buffer */
59558 int iFrame; /* Index of last frame read */
59559 i64 iOffset; /* Next offset to read from log file */
59560 int szPage; /* Page size according to the log */
59561 u32 magic; /* Magic value read from WAL header */
59562 u32 version; /* Magic value read from WAL header */
59563 int isValid; /* True if this frame is valid */
59564
59565 /* Read in the WAL header. */
59566 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE32, 0);
59567 if( rc!=SQLITE_OK0 ){
59568 goto recovery_error;
59569 }
59570
59571 /* If the database page size is not a power of two, or is greater than
59572 ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
59573 ** data. Similarly, if the 'magic' value is invalid, ignore the whole
59574 ** WAL file.
59575 */
59576 magic = sqlite3Get4byte(&aBuf[0]);
59577 szPage = sqlite3Get4byte(&aBuf[8]);
59578 if( (magic&0xFFFFFFFE)!=WAL_MAGIC0x377f0682
59579 || szPage&(szPage-1)
59580 || szPage>SQLITE_MAX_PAGE_SIZE65536
59581 || szPage<512
59582 ){
59583 goto finished;
59584 }
59585 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
59586 pWal->szPage = szPage;
59587 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
59588 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
59589
59590 /* Verify that the WAL header checksum is correct */
59591 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN0,
59592 aBuf, WAL_HDRSIZE32-2*4, 0, pWal->hdr.aFrameCksum
59593 );
59594 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
59595 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
59596 ){
59597 goto finished;
59598 }
59599
59600 /* Verify that the version number on the WAL format is one that
59601 ** are able to understand */
59602 version = sqlite3Get4byte(&aBuf[4]);
59603 if( version!=WAL_MAX_VERSION3007000 ){
59604 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(59604);
59605 goto finished;
59606 }
59607
59608 /* Malloc a buffer to read frames into. */
59609 szFrame = szPage + WAL_FRAME_HDRSIZE24;
59610 aFrame = (u8 *)sqlite3_malloc64(szFrame);
59611 if( !aFrame ){
59612 rc = SQLITE_NOMEM_BKPT7;
59613 goto recovery_error;
59614 }
59615 aData = &aFrame[WAL_FRAME_HDRSIZE24];
59616
59617 /* Read all frames from the log file. */
59618 iFrame = 0;
59619 for(iOffset=WAL_HDRSIZE32; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
59620 u32 pgno; /* Database page number for frame */
59621 u32 nTruncate; /* dbsize field from frame header */
59622
59623 /* Read and decode the next log frame. */
59624 iFrame++;
59625 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
59626 if( rc!=SQLITE_OK0 ) break;
59627 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
59628 if( !isValid ) break;
59629 rc = walIndexAppend(pWal, iFrame, pgno);
59630 if( rc!=SQLITE_OK0 ) break;
59631
59632 /* If nTruncate is non-zero, this is a commit record. */
59633 if( nTruncate ){
59634 pWal->hdr.mxFrame = iFrame;
59635 pWal->hdr.nPage = nTruncate;
59636 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
59637 testcase( szPage<=32768 );
59638 testcase( szPage>=65536 );
59639 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
59640 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
59641 }
59642 }
59643
59644 sqlite3_free(aFrame);
59645 }
59646
59647finished:
59648 if( rc==SQLITE_OK0 ){
59649 volatile WalCkptInfo *pInfo;
59650 int i;
59651 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
59652 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
59653 walIndexWriteHdr(pWal);
59654
59655 /* Reset the checkpoint-header. This is safe because this thread is
59656 ** currently holding locks that exclude all other readers, writers and
59657 ** checkpointers.
59658 */
59659 pInfo = walCkptInfo(pWal);
59660 pInfo->nBackfill = 0;
59661 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
59662 pInfo->aReadMark[0] = 0;
59663 for(i=1; i<WAL_NREADER(8 -3); i++) pInfo->aReadMark[i] = READMARK_NOT_USED0xffffffff;
59664 if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
59665
59666 /* If more than one frame was recovered from the log file, report an
59667 ** event via sqlite3_log(). This is to help with identifying performance
59668 ** problems caused by applications routinely shutting down without
59669 ** checkpointing the log file.
59670 */
59671 if( pWal->hdr.nPage ){
59672 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)),
59673 "recovered %d frames from WAL file %s",
59674 pWal->hdr.mxFrame, pWal->zWalName
59675 );
59676 }
59677 }
59678
59679recovery_error:
59680 WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
59681 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)(3+(0))-iLock);
59682 walUnlockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
59683 return rc;
59684}
59685
59686/*
59687** Close an open wal-index.
59688*/
59689static void walIndexClose(Wal *pWal, int isDelete){
59690 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 || pWal->bShmUnreliable ){
59691 int i;
59692 for(i=0; i<pWal->nWiData; i++){
59693 sqlite3_free((void *)pWal->apWiData[i]);
59694 pWal->apWiData[i] = 0;
59695 }
59696 }
59697 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE2 ){
59698 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
59699 }
59700}
59701
59702/*
59703** Open a connection to the WAL file zWalName. The database file must
59704** already be opened on connection pDbFd. The buffer that zWalName points
59705** to must remain valid for the lifetime of the returned Wal* handle.
59706**
59707** A SHARED lock should be held on the database file when this function
59708** is called. The purpose of this SHARED lock is to prevent any other
59709** client from unlinking the WAL or wal-index file. If another process
59710** were to do this just after this client opened one of these files, the
59711** system would be badly broken.
59712**
59713** If the log file is successfully opened, SQLITE_OK is returned and
59714** *ppWal is set to point to a new WAL handle. If an error occurs,
59715** an SQLite error code is returned and *ppWal is left unmodified.
59716*/
59717SQLITE_PRIVATEstatic int sqlite3WalOpen(
59718 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
59719 sqlite3_file *pDbFd, /* The open database file */
59720 const char *zWalName, /* Name of the WAL file */
59721 int bNoShm, /* True to run in heap-memory mode */
59722 i64 mxWalSize, /* Truncate WAL to this size on reset */
59723 Wal **ppWal /* OUT: Allocated Wal handle */
59724){
59725 int rc; /* Return Code */
59726 Wal *pRet; /* Object to allocate and return */
59727 int flags; /* Flags passed to OsOpen() */
59728
59729 assert( zWalName && zWalName[0] )((void) (0));
59730 assert( pDbFd )((void) (0));
59731
59732 /* In the amalgamation, the os_unix.c and os_win.c source files come before
59733 ** this source file. Verify that the #defines of the locking byte offsets
59734 ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
59735 ** For that matter, if the lock offset ever changes from its initial design
59736 ** value of 120, we need to know that so there is an assert() to check it.
59737 */
59738 assert( 120==WALINDEX_LOCK_OFFSET )((void) (0));
59739 assert( 136==WALINDEX_HDR_SIZE )((void) (0));
59740#ifdef WIN_SHM_BASE
59741 assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET )((void) (0));
59742#endif
59743#ifdef UNIX_SHM_BASE((22+8)*4)
59744 assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET )((void) (0));
59745#endif
59746
59747
59748 /* Allocate an instance of struct Wal to return. */
59749 *ppWal = 0;
59750 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
59751 if( !pRet ){
59752 return SQLITE_NOMEM_BKPT7;
59753 }
59754
59755 pRet->pVfs = pVfs;
59756 pRet->pWalFd = (sqlite3_file *)&pRet[1];
59757 pRet->pDbFd = pDbFd;
59758 pRet->readLock = -1;
59759 pRet->mxWalSize = mxWalSize;
59760 pRet->zWalName = zWalName;
59761 pRet->syncHeader = 1;
59762 pRet->padToSectorBoundary = 1;
59763 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE2: WAL_NORMAL_MODE0);
59764
59765 /* Open file handle on the write-ahead log file. */
59766 flags = (SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_WAL0x00080000);
59767 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
59768 if( rc==SQLITE_OK0 && flags&SQLITE_OPEN_READONLY0x00000001 ){
59769 pRet->readOnly = WAL_RDONLY1;
59770 }
59771
59772 if( rc!=SQLITE_OK0 ){
59773 walIndexClose(pRet, 0);
59774 sqlite3OsClose(pRet->pWalFd);
59775 sqlite3_free(pRet);
59776 }else{
59777 int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
59778 if( iDC & SQLITE_IOCAP_SEQUENTIAL0x00000400 ){ pRet->syncHeader = 0; }
59779 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE0x00001000 ){
59780 pRet->padToSectorBoundary = 0;
59781 }
59782 *ppWal = pRet;
59783 WALTRACE(("WAL%d: opened\n", pRet));
59784 }
59785 return rc;
59786}
59787
59788/*
59789** Change the size to which the WAL file is trucated on each reset.
59790*/
59791SQLITE_PRIVATEstatic void sqlite3WalLimit(Wal *pWal, i64 iLimit){
59792 if( pWal ) pWal->mxWalSize = iLimit;
59793}
59794
59795/*
59796** Find the smallest page number out of all pages held in the WAL that
59797** has not been returned by any prior invocation of this method on the
59798** same WalIterator object. Write into *piFrame the frame index where
59799** that page was last written into the WAL. Write into *piPage the page
59800** number.
59801**
59802** Return 0 on success. If there are no pages in the WAL with a page
59803** number larger than *piPage, then return 1.
59804*/
59805static int walIteratorNext(
59806 WalIterator *p, /* Iterator */
59807 u32 *piPage, /* OUT: The page number of the next page */
59808 u32 *piFrame /* OUT: Wal frame index of next page */
59809){
59810 u32 iMin; /* Result pgno must be greater than iMin */
59811 u32 iRet = 0xFFFFFFFF; /* 0xffffffff is never a valid page number */
59812 int i; /* For looping through segments */
59813
59814 iMin = p->iPrior;
59815 assert( iMin<0xffffffff )((void) (0));
59816 for(i=p->nSegment-1; i>=0; i--){
59817 struct WalSegment *pSegment = &p->aSegment[i];
59818 while( pSegment->iNext<pSegment->nEntry ){
59819 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
59820 if( iPg>iMin ){
59821 if( iPg<iRet ){
59822 iRet = iPg;
59823 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
59824 }
59825 break;
59826 }
59827 pSegment->iNext++;
59828 }
59829 }
59830
59831 *piPage = p->iPrior = iRet;
59832 return (iRet==0xFFFFFFFF);
59833}
59834
59835/*
59836** This function merges two sorted lists into a single sorted list.
59837**
59838** aLeft[] and aRight[] are arrays of indices. The sort key is
59839** aContent[aLeft[]] and aContent[aRight[]]. Upon entry, the following
59840** is guaranteed for all J<K:
59841**
59842** aContent[aLeft[J]] < aContent[aLeft[K]]
59843** aContent[aRight[J]] < aContent[aRight[K]]
59844**
59845** This routine overwrites aRight[] with a new (probably longer) sequence
59846** of indices such that the aRight[] contains every index that appears in
59847** either aLeft[] or the old aRight[] and such that the second condition
59848** above is still met.
59849**
59850** The aContent[aLeft[X]] values will be unique for all X. And the
59851** aContent[aRight[X]] values will be unique too. But there might be
59852** one or more combinations of X and Y such that
59853**
59854** aLeft[X]!=aRight[Y] && aContent[aLeft[X]] == aContent[aRight[Y]]
59855**
59856** When that happens, omit the aLeft[X] and use the aRight[Y] index.
59857*/
59858static void walMerge(
59859 const u32 *aContent, /* Pages in wal - keys for the sort */
59860 ht_slot *aLeft, /* IN: Left hand input list */
59861 int nLeft, /* IN: Elements in array *paLeft */
59862 ht_slot **paRight, /* IN/OUT: Right hand input list */
59863 int *pnRight, /* IN/OUT: Elements in *paRight */
59864 ht_slot *aTmp /* Temporary buffer */
59865){
59866 int iLeft = 0; /* Current index in aLeft */
59867 int iRight = 0; /* Current index in aRight */
59868 int iOut = 0; /* Current index in output buffer */
59869 int nRight = *pnRight;
59870 ht_slot *aRight = *paRight;
59871
59872 assert( nLeft>0 && nRight>0 )((void) (0));
59873 while( iRight<nRight || iLeft<nLeft ){
59874 ht_slot logpage;
59875 Pgno dbpage;
59876
59877 if( (iLeft<nLeft)
59878 && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
59879 ){
59880 logpage = aLeft[iLeft++];
59881 }else{
59882 logpage = aRight[iRight++];
59883 }
59884 dbpage = aContent[logpage];
59885
59886 aTmp[iOut++] = logpage;
59887 if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
59888
59889 assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage )((void) (0));
59890 assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage )((void) (0));
59891 }
59892
59893 *paRight = aLeft;
59894 *pnRight = iOut;
59895 memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
59896}
59897
59898/*
59899** Sort the elements in list aList using aContent[] as the sort key.
59900** Remove elements with duplicate keys, preferring to keep the
59901** larger aList[] values.
59902**
59903** The aList[] entries are indices into aContent[]. The values in
59904** aList[] are to be sorted so that for all J<K:
59905**
59906** aContent[aList[J]] < aContent[aList[K]]
59907**
59908** For any X and Y such that
59909**
59910** aContent[aList[X]] == aContent[aList[Y]]
59911**
59912** Keep the larger of the two values aList[X] and aList[Y] and discard
59913** the smaller.
59914*/
59915static void walMergesort(
59916 const u32 *aContent, /* Pages in wal */
59917 ht_slot *aBuffer, /* Buffer of at least *pnList items to use */
59918 ht_slot *aList, /* IN/OUT: List to sort */
59919 int *pnList /* IN/OUT: Number of elements in aList[] */
59920){
59921 struct Sublist {
59922 int nList; /* Number of elements in aList */
59923 ht_slot *aList; /* Pointer to sub-list content */
59924 };
59925
59926 const int nList = *pnList; /* Size of input list */
59927 int nMerge = 0; /* Number of elements in list aMerge */
59928 ht_slot *aMerge = 0; /* List to be merged */
59929 int iList; /* Index into input list */
59930 u32 iSub = 0; /* Index into aSub array */
59931 struct Sublist aSub[13]; /* Array of sub-lists */
59932
59933 memset(aSub, 0, sizeof(aSub));
59934 assert( nList<=HASHTABLE_NPAGE && nList>0 )((void) (0));
59935 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) )((void) (0));
59936
59937 for(iList=0; iList<nList; iList++){
59938 nMerge = 1;
59939 aMerge = &aList[iList];
59940 for(iSub=0; iList & (1<<iSub); iSub++){
59941 struct Sublist *p;
59942 assert( iSub<ArraySize(aSub) )((void) (0));
59943 p = &aSub[iSub];
59944 assert( p->aList && p->nList<=(1<<iSub) )((void) (0));
59945 assert( p->aList==&aList[iList&~((2<<iSub)-1)] )((void) (0));
59946 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
59947 }
59948 aSub[iSub].aList = aMerge;
59949 aSub[iSub].nList = nMerge;
59950 }
59951
59952 for(iSub++; iSub<ArraySize(aSub)((int)(sizeof(aSub)/sizeof(aSub[0]))); iSub++){
59953 if( nList & (1<<iSub) ){
59954 struct Sublist *p;
59955 assert( iSub<ArraySize(aSub) )((void) (0));
59956 p = &aSub[iSub];
59957 assert( p->nList<=(1<<iSub) )((void) (0));
59958 assert( p->aList==&aList[nList&~((2<<iSub)-1)] )((void) (0));
59959 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
59960 }
59961 }
59962 assert( aMerge==aList )((void) (0));
59963 *pnList = nMerge;
59964
59965#ifdef SQLITE_DEBUG
59966 {
59967 int i;
59968 for(i=1; i<*pnList; i++){
59969 assert( aContent[aList[i]] > aContent[aList[i-1]] )((void) (0));
59970 }
59971 }
59972#endif
59973}
59974
59975/*
59976** Free an iterator allocated by walIteratorInit().
59977*/
59978static void walIteratorFree(WalIterator *p){
59979 sqlite3_free(p);
59980}
59981
59982/*
59983** Construct a WalInterator object that can be used to loop over all
59984** pages in the WAL following frame nBackfill in ascending order. Frames
59985** nBackfill or earlier may be included - excluding them is an optimization
59986** only. The caller must hold the checkpoint lock.
59987**
59988** On success, make *pp point to the newly allocated WalInterator object
59989** return SQLITE_OK. Otherwise, return an error code. If this routine
59990** returns an error, the value of *pp is undefined.
59991**
59992** The calling routine should invoke walIteratorFree() to destroy the
59993** WalIterator object when it has finished with it.
59994*/
59995static int walIteratorInit(Wal *pWal, u32 nBackfill, WalIterator **pp){
59996 WalIterator *p; /* Return value */
59997 int nSegment; /* Number of segments to merge */
59998 u32 iLast; /* Last frame in log */
59999 sqlite3_int64 nByte; /* Number of bytes to allocate */
60000 int i; /* Iterator variable */
60001 ht_slot *aTmp; /* Temp space used by merge-sort */
60002 int rc = SQLITE_OK0; /* Return Code */
60003
60004 /* This routine only runs while holding the checkpoint lock. And
60005 ** it only runs if there is actually content in the log (mxFrame>0).
60006 */
60007 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 )((void) (0));
60008 iLast = pWal->hdr.mxFrame;
60009
60010 /* Allocate space for the WalIterator object. */
60011 nSegment = walFramePage(iLast) + 1;
60012 nByte = sizeof(WalIterator)
60013 + (nSegment-1)*sizeof(struct WalSegment)
60014 + iLast*sizeof(ht_slot);
60015 p = (WalIterator *)sqlite3_malloc64(nByte);
60016 if( !p ){
60017 return SQLITE_NOMEM_BKPT7;
60018 }
60019 memset(p, 0, nByte);
60020 p->nSegment = nSegment;
60021
60022 /* Allocate temporary space used by the merge-sort routine. This block
60023 ** of memory will be freed before this function returns.
60024 */
60025 aTmp = (ht_slot *)sqlite3_malloc64(
60026 sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE4096?HASHTABLE_NPAGE4096:iLast)
60027 );
60028 if( !aTmp ){
60029 rc = SQLITE_NOMEM_BKPT7;
60030 }
60031
60032 for(i=walFramePage(nBackfill+1); rc==SQLITE_OK0 && i<nSegment; i++){
60033 WalHashLoc sLoc;
60034
60035 rc = walHashGet(pWal, i, &sLoc);
60036 if( rc==SQLITE_OK0 ){
60037 int j; /* Counter variable */
60038 int nEntry; /* Number of entries in this segment */
60039 ht_slot *aIndex; /* Sorted index for this segment */
60040
60041 sLoc.aPgno++;
60042 if( (i+1)==nSegment ){
60043 nEntry = (int)(iLast - sLoc.iZero);
60044 }else{
60045 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
60046 }
60047 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
60048 sLoc.iZero++;
60049
60050 for(j=0; j<nEntry; j++){
60051 aIndex[j] = (ht_slot)j;
60052 }
60053 walMergesort((u32 *)sLoc.aPgno, aTmp, aIndex, &nEntry);
60054 p->aSegment[i].iZero = sLoc.iZero;
60055 p->aSegment[i].nEntry = nEntry;
60056 p->aSegment[i].aIndex = aIndex;
60057 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
60058 }
60059 }
60060 sqlite3_free(aTmp);
60061
60062 if( rc!=SQLITE_OK0 ){
60063 walIteratorFree(p);
60064 p = 0;
60065 }
60066 *pp = p;
60067 return rc;
60068}
60069
60070/*
60071** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
60072** n. If the attempt fails and parameter xBusy is not NULL, then it is a
60073** busy-handler function. Invoke it and retry the lock until either the
60074** lock is successfully obtained or the busy-handler returns 0.
60075*/
60076static int walBusyLock(
60077 Wal *pWal, /* WAL connection */
60078 int (*xBusy)(void*), /* Function to call when busy */
60079 void *pBusyArg, /* Context argument for xBusyHandler */
60080 int lockIdx, /* Offset of first byte to lock */
60081 int n /* Number of bytes to lock */
60082){
60083 int rc;
60084 do {
60085 rc = walLockExclusive(pWal, lockIdx, n);
60086 }while( xBusy && rc==SQLITE_BUSY5 && xBusy(pBusyArg) );
60087 return rc;
60088}
60089
60090/*
60091** The cache of the wal-index header must be valid to call this function.
60092** Return the page-size in bytes used by the database.
60093*/
60094static int walPagesize(Wal *pWal){
60095 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
60096}
60097
60098/*
60099** The following is guaranteed when this function is called:
60100**
60101** a) the WRITER lock is held,
60102** b) the entire log file has been checkpointed, and
60103** c) any existing readers are reading exclusively from the database
60104** file - there are no readers that may attempt to read a frame from
60105** the log file.
60106**
60107** This function updates the shared-memory structures so that the next
60108** client to write to the database (which may be this one) does so by
60109** writing frames into the start of the log file.
60110**
60111** The value of parameter salt1 is used as the aSalt[1] value in the
60112** new wal-index header. It should be passed a pseudo-random value (i.e.
60113** one obtained from sqlite3_randomness()).
60114*/
60115static void walRestartHdr(Wal *pWal, u32 salt1){
60116 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
60117 int i; /* Loop counter */
60118 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
60119 pWal->nCkpt++;
60120 pWal->hdr.mxFrame = 0;
60121 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
60122 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
60123 walIndexWriteHdr(pWal);
60124 pInfo->nBackfill = 0;
60125 pInfo->nBackfillAttempted = 0;
60126 pInfo->aReadMark[1] = 0;
60127 for(i=2; i<WAL_NREADER(8 -3); i++) pInfo->aReadMark[i] = READMARK_NOT_USED0xffffffff;
60128 assert( pInfo->aReadMark[0]==0 )((void) (0));
60129}
60130
60131/*
60132** Copy as much content as we can from the WAL back into the database file
60133** in response to an sqlite3_wal_checkpoint() request or the equivalent.
60134**
60135** The amount of information copies from WAL to database might be limited
60136** by active readers. This routine will never overwrite a database page
60137** that a concurrent reader might be using.
60138**
60139** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
60140** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
60141** checkpoints are always run by a background thread or background
60142** process, foreground threads will never block on a lengthy fsync call.
60143**
60144** Fsync is called on the WAL before writing content out of the WAL and
60145** into the database. This ensures that if the new content is persistent
60146** in the WAL and can be recovered following a power-loss or hard reset.
60147**
60148** Fsync is also called on the database file if (and only if) the entire
60149** WAL content is copied into the database file. This second fsync makes
60150** it safe to delete the WAL since the new content will persist in the
60151** database file.
60152**
60153** This routine uses and updates the nBackfill field of the wal-index header.
60154** This is the only routine that will increase the value of nBackfill.
60155** (A WAL reset or recovery will revert nBackfill to zero, but not increase
60156** its value.)
60157**
60158** The caller must be holding sufficient locks to ensure that no other
60159** checkpoint is running (in any other thread or process) at the same
60160** time.
60161*/
60162static int walCheckpoint(
60163 Wal *pWal, /* Wal connection */
60164 sqlite3 *db, /* Check for interrupts on this handle */
60165 int eMode, /* One of PASSIVE, FULL or RESTART */
60166 int (*xBusy)(void*), /* Function to call when busy */
60167 void *pBusyArg, /* Context argument for xBusyHandler */
60168 int sync_flags, /* Flags for OsSync() (or 0) */
60169 u8 *zBuf /* Temporary buffer to use */
60170){
60171 int rc = SQLITE_OK0; /* Return code */
60172 int szPage; /* Database page-size */
60173 WalIterator *pIter = 0; /* Wal iterator context */
60174 u32 iDbpage = 0; /* Next database page to write */
60175 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
60176 u32 mxSafeFrame; /* Max frame that can be backfilled */
60177 u32 mxPage; /* Max database page to write */
60178 int i; /* Loop counter */
60179 volatile WalCkptInfo *pInfo; /* The checkpoint status information */
60180
60181 szPage = walPagesize(pWal);
60182 testcase( szPage<=32768 );
60183 testcase( szPage>=65536 );
60184 pInfo = walCkptInfo(pWal);
60185 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
60186
60187 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
60188 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
60189 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 )((void) (0));
60190
60191 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
60192 ** safe to write into the database. Frames beyond mxSafeFrame might
60193 ** overwrite database pages that are in use by active readers and thus
60194 ** cannot be backfilled from the WAL.
60195 */
60196 mxSafeFrame = pWal->hdr.mxFrame;
60197 mxPage = pWal->hdr.nPage;
60198 for(i=1; i<WAL_NREADER(8 -3); i++){
60199 /* Thread-sanitizer reports that the following is an unsafe read,
60200 ** as some other thread may be in the process of updating the value
60201 ** of the aReadMark[] slot. The assumption here is that if that is
60202 ** happening, the other client may only be increasing the value,
60203 ** not decreasing it. So assuming either that either the "old" or
60204 ** "new" version of the value is read, and not some arbitrary value
60205 ** that would never be written by a real client, things are still
60206 ** safe. */
60207 u32 y = pInfo->aReadMark[i];
60208 if( mxSafeFrame>y ){
60209 assert( y<=pWal->hdr.mxFrame )((void) (0));
60210 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i)(3+(i)), 1);
60211 if( rc==SQLITE_OK0 ){
60212 pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED0xffffffff);
60213 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
60214 }else if( rc==SQLITE_BUSY5 ){
60215 mxSafeFrame = y;
60216 xBusy = 0;
60217 }else{
60218 goto walcheckpoint_out;
60219 }
60220 }
60221 }
60222
60223 /* Allocate the iterator */
60224 if( pInfo->nBackfill<mxSafeFrame ){
60225 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
60226 assert( rc==SQLITE_OK || pIter==0 )((void) (0));
60227 }
60228
60229 if( pIter
60230 && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0)(3+(0)),1))==SQLITE_OK0
60231 ){
60232 u32 nBackfill = pInfo->nBackfill;
60233
60234 pInfo->nBackfillAttempted = mxSafeFrame;
60235
60236 /* Sync the WAL to disk */
60237 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
60238
60239 /* If the database may grow as a result of this checkpoint, hint
60240 ** about the eventual size of the db file to the VFS layer.
60241 */
60242 if( rc==SQLITE_OK0 ){
60243 i64 nReq = ((i64)mxPage * szPage);
60244 i64 nSize; /* Current size of database file */
60245 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
60246 if( rc==SQLITE_OK0 && nSize<nReq ){
60247 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT5, &nReq);
60248 }
60249 }
60250
60251
60252 /* Iterate through the contents of the WAL, copying data to the db file */
60253 while( rc==SQLITE_OK0 && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
60254 i64 iOffset;
60255 assert( walFramePgno(pWal, iFrame)==iDbpage )((void) (0));
60256 if( db->u1.isInterrupted ){
60257 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT7 : SQLITE_INTERRUPT9;
60258 break;
60259 }
60260 if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
60261 continue;
60262 }
60263 iOffset = walFrameOffset(iFrame, szPage)( 32 + ((iFrame)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
60264 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
60265 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
60266 if( rc!=SQLITE_OK0 ) break;
60267 iOffset = (iDbpage-1)*(i64)szPage;
60268 testcase( IS_BIG_INT(iOffset) );
60269 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
60270 if( rc!=SQLITE_OK0 ) break;
60271 }
60272
60273 /* If work was actually accomplished... */
60274 if( rc==SQLITE_OK0 ){
60275 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
60276 i64 szDb = pWal->hdr.nPage*(i64)szPage;
60277 testcase( IS_BIG_INT(szDb) );
60278 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
60279 if( rc==SQLITE_OK0 ){
60280 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
60281 }
60282 }
60283 if( rc==SQLITE_OK0 ){
60284 pInfo->nBackfill = mxSafeFrame;
60285 }
60286 }
60287
60288 /* Release the reader lock held while backfilling */
60289 walUnlockExclusive(pWal, WAL_READ_LOCK(0)(3+(0)), 1);
60290 }
60291
60292 if( rc==SQLITE_BUSY5 ){
60293 /* Reset the return code so as not to report a checkpoint failure
60294 ** just because there are active readers. */
60295 rc = SQLITE_OK0;
60296 }
60297 }
60298
60299 /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
60300 ** entire wal file has been copied into the database file, then block
60301 ** until all readers have finished using the wal file. This ensures that
60302 ** the next process to write to the database restarts the wal file.
60303 */
60304 if( rc==SQLITE_OK0 && eMode!=SQLITE_CHECKPOINT_PASSIVE0 ){
60305 assert( pWal->writeLock )((void) (0));
60306 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
60307 rc = SQLITE_BUSY5;
60308 }else if( eMode>=SQLITE_CHECKPOINT_RESTART2 ){
60309 u32 salt1;
60310 sqlite3_randomness(4, &salt1);
60311 assert( pInfo->nBackfill==pWal->hdr.mxFrame )((void) (0));
60312 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
60313 if( rc==SQLITE_OK0 ){
60314 if( eMode==SQLITE_CHECKPOINT_TRUNCATE3 ){
60315 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
60316 ** SQLITE_CHECKPOINT_RESTART with the addition that it also
60317 ** truncates the log file to zero bytes just prior to a
60318 ** successful return.
60319 **
60320 ** In theory, it might be safe to do this without updating the
60321 ** wal-index header in shared memory, as all subsequent reader or
60322 ** writer clients should see that the entire log file has been
60323 ** checkpointed and behave accordingly. This seems unsafe though,
60324 ** as it would leave the system in a state where the contents of
60325 ** the wal-index header do not match the contents of the
60326 ** file-system. To avoid this, update the wal-index header to
60327 ** indicate that the log file contains zero valid frames. */
60328 walRestartHdr(pWal, salt1);
60329 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
60330 }
60331 walUnlockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
60332 }
60333 }
60334 }
60335
60336 walcheckpoint_out:
60337 walIteratorFree(pIter);
60338 return rc;
60339}
60340
60341/*
60342** If the WAL file is currently larger than nMax bytes in size, truncate
60343** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
60344*/
60345static void walLimitSize(Wal *pWal, i64 nMax){
60346 i64 sz;
60347 int rx;
60348 sqlite3BeginBenignMalloc();
60349 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
60350 if( rx==SQLITE_OK0 && (sz > nMax ) ){
60351 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
60352 }
60353 sqlite3EndBenignMalloc();
60354 if( rx ){
60355 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
60356 }
60357}
60358
60359/*
60360** Close a connection to a log file.
60361*/
60362SQLITE_PRIVATEstatic int sqlite3WalClose(
60363 Wal *pWal, /* Wal to close */
60364 sqlite3 *db, /* For interrupt flag */
60365 int sync_flags, /* Flags to pass to OsSync() (or 0) */
60366 int nBuf,
60367 u8 *zBuf /* Buffer of at least nBuf bytes */
60368){
60369 int rc = SQLITE_OK0;
60370 if( pWal ){
60371 int isDelete = 0; /* True to unlink wal and wal-index files */
60372
60373 /* If an EXCLUSIVE lock can be obtained on the database file (using the
60374 ** ordinary, rollback-mode locking methods, this guarantees that the
60375 ** connection associated with this log file is the only connection to
60376 ** the database. In this case checkpoint the database and unlink both
60377 ** the wal and wal-index files.
60378 **
60379 ** The EXCLUSIVE lock is not released before returning.
60380 */
60381 if( zBuf!=0
60382 && SQLITE_OK0==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE4))
60383 ){
60384 if( pWal->exclusiveMode==WAL_NORMAL_MODE0 ){
60385 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
60386 }
60387 rc = sqlite3WalCheckpoint(pWal, db,
60388 SQLITE_CHECKPOINT_PASSIVE0, 0, 0, sync_flags, nBuf, zBuf, 0, 0
60389 );
60390 if( rc==SQLITE_OK0 ){
60391 int bPersist = -1;
60392 sqlite3OsFileControlHint(
60393 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL10, &bPersist
60394 );
60395 if( bPersist!=1 ){
60396 /* Try to delete the WAL file if the checkpoint completed and
60397 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
60398 ** mode (!bPersist) */
60399 isDelete = 1;
60400 }else if( pWal->mxWalSize>=0 ){
60401 /* Try to truncate the WAL file to zero bytes if the checkpoint
60402 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
60403 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
60404 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
60405 ** to zero bytes as truncating to the journal_size_limit might
60406 ** leave a corrupt WAL file on disk. */
60407 walLimitSize(pWal, 0);
60408 }
60409 }
60410 }
60411
60412 walIndexClose(pWal, isDelete);
60413 sqlite3OsClose(pWal->pWalFd);
60414 if( isDelete ){
60415 sqlite3BeginBenignMalloc();
60416 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
60417 sqlite3EndBenignMalloc();
60418 }
60419 WALTRACE(("WAL%p: closed\n", pWal));
60420 sqlite3_free((void *)pWal->apWiData);
60421 sqlite3_free(pWal);
60422 }
60423 return rc;
60424}
60425
60426/*
60427** Try to read the wal-index header. Return 0 on success and 1 if
60428** there is a problem.
60429**
60430** The wal-index is in shared memory. Another thread or process might
60431** be writing the header at the same time this procedure is trying to
60432** read it, which might result in inconsistency. A dirty read is detected
60433** by verifying that both copies of the header are the same and also by
60434** a checksum on the header.
60435**
60436** If and only if the read is consistent and the header is different from
60437** pWal->hdr, then pWal->hdr is updated to the content of the new header
60438** and *pChanged is set to 1.
60439**
60440** If the checksum cannot be verified return non-zero. If the header
60441** is read successfully and the checksum verified, return zero.
60442*/
60443static int walIndexTryHdr(Wal *pWal, int *pChanged){
60444 u32 aCksum[2]; /* Checksum on the header content */
60445 WalIndexHdr h1, h2; /* Two copies of the header content */
60446 WalIndexHdr volatile *aHdr; /* Header in shared memory */
60447
60448 /* The first page of the wal-index must be mapped at this point. */
60449 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
60450
60451 /* Read the header. This might happen concurrently with a write to the
60452 ** same area of shared memory on a different CPU in a SMP,
60453 ** meaning it is possible that an inconsistent snapshot is read
60454 ** from the file. If this happens, return non-zero.
60455 **
60456 ** There are two copies of the header at the beginning of the wal-index.
60457 ** When reading, read [0] first then [1]. Writes are in the reverse order.
60458 ** Memory barriers are used to prevent the compiler or the hardware from
60459 ** reordering the reads and writes.
60460 */
60461 aHdr = walIndexHdr(pWal);
60462 memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
60463 walShmBarrier(pWal);
60464 memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
60465
60466 if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
60467 return 1; /* Dirty read */
60468 }
60469 if( h1.isInit==0 ){
60470 return 1; /* Malformed header - probably all zeros */
60471 }
60472 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
60473 if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
60474 return 1; /* Checksum does not match */
60475 }
60476
60477 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
60478 *pChanged = 1;
60479 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
60480 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
60481 testcase( pWal->szPage<=32768 );
60482 testcase( pWal->szPage>=65536 );
60483 }
60484
60485 /* The header was successfully read. Return zero. */
60486 return 0;
60487}
60488
60489/*
60490** This is the value that walTryBeginRead returns when it needs to
60491** be retried.
60492*/
60493#define WAL_RETRY(-1) (-1)
60494
60495/*
60496** Read the wal-index header from the wal-index and into pWal->hdr.
60497** If the wal-header appears to be corrupt, try to reconstruct the
60498** wal-index from the WAL before returning.
60499**
60500** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
60501** changed by this operation. If pWal->hdr is unchanged, set *pChanged
60502** to 0.
60503**
60504** If the wal-index header is successfully read, return SQLITE_OK.
60505** Otherwise an SQLite error code.
60506*/
60507static int walIndexReadHdr(Wal *pWal, int *pChanged){
60508 int rc; /* Return code */
60509 int badHdr; /* True if a header read failed */
60510 volatile u32 *page0; /* Chunk of wal-index containing header */
60511
60512 /* Ensure that page 0 of the wal-index (the page that contains the
60513 ** wal-index header) is mapped. Return early if an error occurs here.
60514 */
60515 assert( pChanged )((void) (0));
60516 rc = walIndexPage(pWal, 0, &page0);
60517 if( rc!=SQLITE_OK0 ){
60518 assert( rc!=SQLITE_READONLY )((void) (0)); /* READONLY changed to OK in walIndexPage */
60519 if( rc==SQLITE_READONLY_CANTINIT(8 | (5<<8)) ){
60520 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
60521 ** was openable but is not writable, and this thread is unable to
60522 ** confirm that another write-capable connection has the shared-memory
60523 ** open, and hence the content of the shared-memory is unreliable,
60524 ** since the shared-memory might be inconsistent with the WAL file
60525 ** and there is no writer on hand to fix it. */
60526 assert( page0==0 )((void) (0));
60527 assert( pWal->writeLock==0 )((void) (0));
60528 assert( pWal->readOnly & WAL_SHM_RDONLY )((void) (0));
60529 pWal->bShmUnreliable = 1;
60530 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE2;
60531 *pChanged = 1;
60532 }else{
60533 return rc; /* Any other non-OK return is just an error */
60534 }
60535 }else{
60536 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
60537 ** is zero, which prevents the SHM from growing */
60538 testcase( page0!=0 );
60539 }
60540 assert( page0!=0 || pWal->writeLock==0 )((void) (0));
60541
60542 /* If the first page of the wal-index has been mapped, try to read the
60543 ** wal-index header immediately, without holding any lock. This usually
60544 ** works, but may fail if the wal-index header is corrupt or currently
60545 ** being modified by another thread or process.
60546 */
60547 badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
60548
60549 /* If the first attempt failed, it might have been due to a race
60550 ** with a writer. So get a WRITE lock and try again.
60551 */
60552 assert( badHdr==0 || pWal->writeLock==0 )((void) (0));
60553 if( badHdr ){
60554 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY2) ){
60555 if( SQLITE_OK0==(rc = walLockShared(pWal, WAL_WRITE_LOCK0)) ){
60556 walUnlockShared(pWal, WAL_WRITE_LOCK0);
60557 rc = SQLITE_READONLY_RECOVERY(8 | (1<<8));
60558 }
60559 }else if( SQLITE_OK0==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1)) ){
60560 pWal->writeLock = 1;
60561 if( SQLITE_OK0==(rc = walIndexPage(pWal, 0, &page0)) ){
60562 badHdr = walIndexTryHdr(pWal, pChanged);
60563 if( badHdr ){
60564 /* If the wal-index header is still malformed even while holding
60565 ** a WRITE lock, it can only mean that the header is corrupted and
60566 ** needs to be reconstructed. So run recovery to do exactly that.
60567 */
60568 rc = walIndexRecover(pWal);
60569 *pChanged = 1;
60570 }
60571 }
60572 pWal->writeLock = 0;
60573 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
60574 }
60575 }
60576
60577 /* If the header is read successfully, check the version number to make
60578 ** sure the wal-index was not constructed with some future format that
60579 ** this version of SQLite cannot understand.
60580 */
60581 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION3007000 ){
60582 rc = SQLITE_CANTOPEN_BKPTsqlite3CantopenError(60582);
60583 }
60584 if( pWal->bShmUnreliable ){
60585 if( rc!=SQLITE_OK0 ){
60586 walIndexClose(pWal, 0);
60587 pWal->bShmUnreliable = 0;
60588 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 )((void) (0));
60589 /* walIndexRecover() might have returned SHORT_READ if a concurrent
60590 ** writer truncated the WAL out from under it. If that happens, it
60591 ** indicates that a writer has fixed the SHM file for us, so retry */
60592 if( rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) ) rc = WAL_RETRY(-1);
60593 }
60594 pWal->exclusiveMode = WAL_NORMAL_MODE0;
60595 }
60596
60597 return rc;
60598}
60599
60600/*
60601** Open a transaction in a connection where the shared-memory is read-only
60602** and where we cannot verify that there is a separate write-capable connection
60603** on hand to keep the shared-memory up-to-date with the WAL file.
60604**
60605** This can happen, for example, when the shared-memory is implemented by
60606** memory-mapping a *-shm file, where a prior writer has shut down and
60607** left the *-shm file on disk, and now the present connection is trying
60608** to use that database but lacks write permission on the *-shm file.
60609** Other scenarios are also possible, depending on the VFS implementation.
60610**
60611** Precondition:
60612**
60613** The *-wal file has been read and an appropriate wal-index has been
60614** constructed in pWal->apWiData[] using heap memory instead of shared
60615** memory.
60616**
60617** If this function returns SQLITE_OK, then the read transaction has
60618** been successfully opened. In this case output variable (*pChanged)
60619** is set to true before returning if the caller should discard the
60620** contents of the page cache before proceeding. Or, if it returns
60621** WAL_RETRY, then the heap memory wal-index has been discarded and
60622** the caller should retry opening the read transaction from the
60623** beginning (including attempting to map the *-shm file).
60624**
60625** If an error occurs, an SQLite error code is returned.
60626*/
60627static int walBeginShmUnreliable(Wal *pWal, int *pChanged){
60628 i64 szWal; /* Size of wal file on disk in bytes */
60629 i64 iOffset; /* Current offset when reading wal file */
60630 u8 aBuf[WAL_HDRSIZE32]; /* Buffer to load WAL header into */
60631 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
60632 int szFrame; /* Number of bytes in buffer aFrame[] */
60633 u8 *aData; /* Pointer to data part of aFrame buffer */
60634 volatile void *pDummy; /* Dummy argument for xShmMap */
60635 int rc; /* Return code */
60636 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
60637
60638 assert( pWal->bShmUnreliable )((void) (0));
60639 assert( pWal->readOnly & WAL_SHM_RDONLY )((void) (0));
60640 assert( pWal->nWiData>0 && pWal->apWiData[0] )((void) (0));
60641
60642 /* Take WAL_READ_LOCK(0). This has the effect of preventing any
60643 ** writers from running a checkpoint, but does not stop them
60644 ** from running recovery. */
60645 rc = walLockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
60646 if( rc!=SQLITE_OK0 ){
60647 if( rc==SQLITE_BUSY5 ) rc = WAL_RETRY(-1);
60648 goto begin_unreliable_shm_out;
60649 }
60650 pWal->readLock = 0;
60651
60652 /* Check to see if a separate writer has attached to the shared-memory area,
60653 ** thus making the shared-memory "reliable" again. Do this by invoking
60654 ** the xShmMap() routine of the VFS and looking to see if the return
60655 ** is SQLITE_READONLY instead of SQLITE_READONLY_CANTINIT.
60656 **
60657 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
60658 ** cause the heap-memory WAL-index to be discarded and the actual
60659 ** shared memory to be used in its place.
60660 **
60661 ** This step is important because, even though this connection is holding
60662 ** the WAL_READ_LOCK(0) which prevents a checkpoint, a writer might
60663 ** have already checkpointed the WAL file and, while the current
60664 ** is active, wrap the WAL and start overwriting frames that this
60665 ** process wants to use.
60666 **
60667 ** Once sqlite3OsShmMap() has been called for an sqlite3_file and has
60668 ** returned any SQLITE_READONLY value, it must return only SQLITE_READONLY
60669 ** or SQLITE_READONLY_CANTINIT or some error for all subsequent invocations,
60670 ** even if some external agent does a "chmod" to make the shared-memory
60671 ** writable by us, until sqlite3OsShmUnmap() has been called.
60672 ** This is a requirement on the VFS implementation.
60673 */
60674 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ( sizeof(ht_slot)*(4096*2) + 4096*sizeof(u32) ), 0, &pDummy);
60675 assert( rc!=SQLITE_OK )((void) (0)); /* SQLITE_OK not possible for read-only connection */
60676 if( rc!=SQLITE_READONLY_CANTINIT(8 | (5<<8)) ){
60677 rc = (rc==SQLITE_READONLY8 ? WAL_RETRY(-1) : rc);
60678 goto begin_unreliable_shm_out;
60679 }
60680
60681 /* We reach this point only if the real shared-memory is still unreliable.
60682 ** Assume the in-memory WAL-index substitute is correct and load it
60683 ** into pWal->hdr.
60684 */
60685 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
60686
60687 /* Make sure some writer hasn't come in and changed the WAL file out
60688 ** from under us, then disconnected, while we were not looking.
60689 */
60690 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
60691 if( rc!=SQLITE_OK0 ){
60692 goto begin_unreliable_shm_out;
60693 }
60694 if( szWal<WAL_HDRSIZE32 ){
60695 /* If the wal file is too small to contain a wal-header and the
60696 ** wal-index header has mxFrame==0, then it must be safe to proceed
60697 ** reading the database file only. However, the page cache cannot
60698 ** be trusted, as a read/write connection may have connected, written
60699 ** the db, run a checkpoint, truncated the wal file and disconnected
60700 ** since this client's last read transaction. */
60701 *pChanged = 1;
60702 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK0 : WAL_RETRY(-1));
60703 goto begin_unreliable_shm_out;
60704 }
60705
60706 /* Check the salt keys at the start of the wal file still match. */
60707 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE32, 0);
60708 if( rc!=SQLITE_OK0 ){
60709 goto begin_unreliable_shm_out;
60710 }
60711 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
60712 /* Some writer has wrapped the WAL file while we were not looking.
60713 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
60714 ** rebuilt. */
60715 rc = WAL_RETRY(-1);
60716 goto begin_unreliable_shm_out;
60717 }
60718
60719 /* Allocate a buffer to read frames into */
60720 szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE24;
60721 aFrame = (u8 *)sqlite3_malloc64(szFrame);
60722 if( aFrame==0 ){
60723 rc = SQLITE_NOMEM_BKPT7;
60724 goto begin_unreliable_shm_out;
60725 }
60726 aData = &aFrame[WAL_FRAME_HDRSIZE24];
60727
60728 /* Check to see if a complete transaction has been appended to the
60729 ** wal file since the heap-memory wal-index was created. If so, the
60730 ** heap-memory wal-index is discarded and WAL_RETRY returned to
60731 ** the caller. */
60732 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
60733 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
60734 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage)( 32 + ((pWal->hdr.mxFrame+1)-1)*(i64)((pWal->hdr.szPage
)+24) )
;
60735 iOffset+szFrame<=szWal;
60736 iOffset+=szFrame
60737 ){
60738 u32 pgno; /* Database page number for frame */
60739 u32 nTruncate; /* dbsize field from frame header */
60740
60741 /* Read and decode the next log frame. */
60742 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
60743 if( rc!=SQLITE_OK0 ) break;
60744 if( !walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame) ) break;
60745
60746 /* If nTruncate is non-zero, then a complete transaction has been
60747 ** appended to this wal file. Set rc to WAL_RETRY and break out of
60748 ** the loop. */
60749 if( nTruncate ){
60750 rc = WAL_RETRY(-1);
60751 break;
60752 }
60753 }
60754 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
60755 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
60756
60757 begin_unreliable_shm_out:
60758 sqlite3_free(aFrame);
60759 if( rc!=SQLITE_OK0 ){
60760 int i;
60761 for(i=0; i<pWal->nWiData; i++){
60762 sqlite3_free((void*)pWal->apWiData[i]);
60763 pWal->apWiData[i] = 0;
60764 }
60765 pWal->bShmUnreliable = 0;
60766 sqlite3WalEndReadTransaction(pWal);
60767 *pChanged = 1;
60768 }
60769 return rc;
60770}
60771
60772/*
60773** Attempt to start a read transaction. This might fail due to a race or
60774** other transient condition. When that happens, it returns WAL_RETRY to
60775** indicate to the caller that it is safe to retry immediately.
60776**
60777** On success return SQLITE_OK. On a permanent failure (such an
60778** I/O error or an SQLITE_BUSY because another process is running
60779** recovery) return a positive error code.
60780**
60781** The useWal parameter is true to force the use of the WAL and disable
60782** the case where the WAL is bypassed because it has been completely
60783** checkpointed. If useWal==0 then this routine calls walIndexReadHdr()
60784** to make a copy of the wal-index header into pWal->hdr. If the
60785** wal-index header has changed, *pChanged is set to 1 (as an indication
60786** to the caller that the local page cache is obsolete and needs to be
60787** flushed.) When useWal==1, the wal-index header is assumed to already
60788** be loaded and the pChanged parameter is unused.
60789**
60790** The caller must set the cnt parameter to the number of prior calls to
60791** this routine during the current read attempt that returned WAL_RETRY.
60792** This routine will start taking more aggressive measures to clear the
60793** race conditions after multiple WAL_RETRY returns, and after an excessive
60794** number of errors will ultimately return SQLITE_PROTOCOL. The
60795** SQLITE_PROTOCOL return indicates that some other process has gone rogue
60796** and is not honoring the locking protocol. There is a vanishingly small
60797** chance that SQLITE_PROTOCOL could be returned because of a run of really
60798** bad luck when there is lots of contention for the wal-index, but that
60799** possibility is so small that it can be safely neglected, we believe.
60800**
60801** On success, this routine obtains a read lock on
60802** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
60803** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
60804** that means the Wal does not hold any read lock. The reader must not
60805** access any database page that is modified by a WAL frame up to and
60806** including frame number aReadMark[pWal->readLock]. The reader will
60807** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
60808** Or if pWal->readLock==0, then the reader will ignore the WAL
60809** completely and get all content directly from the database file.
60810** If the useWal parameter is 1 then the WAL will never be ignored and
60811** this routine will always set pWal->readLock>0 on success.
60812** When the read transaction is completed, the caller must release the
60813** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
60814**
60815** This routine uses the nBackfill and aReadMark[] fields of the header
60816** to select a particular WAL_READ_LOCK() that strives to let the
60817** checkpoint process do as much work as possible. This routine might
60818** update values of the aReadMark[] array in the header, but if it does
60819** so it takes care to hold an exclusive lock on the corresponding
60820** WAL_READ_LOCK() while changing values.
60821*/
60822static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
60823 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
60824 u32 mxReadMark; /* Largest aReadMark[] value */
60825 int mxI; /* Index of largest aReadMark[] value */
60826 int i; /* Loop counter */
60827 int rc = SQLITE_OK0; /* Return code */
60828 u32 mxFrame; /* Wal frame to lock to */
60829
60830 assert( pWal->readLock<0 )((void) (0)); /* Not currently locked */
60831
60832 /* useWal may only be set for read/write connections */
60833 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 )((void) (0));
60834
60835 /* Take steps to avoid spinning forever if there is a protocol error.
60836 **
60837 ** Circumstances that cause a RETRY should only last for the briefest
60838 ** instances of time. No I/O or other system calls are done while the
60839 ** locks are held, so the locks should not be held for very long. But
60840 ** if we are unlucky, another process that is holding a lock might get
60841 ** paged out or take a page-fault that is time-consuming to resolve,
60842 ** during the few nanoseconds that it is holding the lock. In that case,
60843 ** it might take longer than normal for the lock to free.
60844 **
60845 ** After 5 RETRYs, we begin calling sqlite3OsSleep(). The first few
60846 ** calls to sqlite3OsSleep() have a delay of 1 microsecond. Really this
60847 ** is more of a scheduler yield than an actual delay. But on the 10th
60848 ** an subsequent retries, the delays start becoming longer and longer,
60849 ** so that on the 100th (and last) RETRY we delay for 323 milliseconds.
60850 ** The total delay time before giving up is less than 10 seconds.
60851 */
60852 if( cnt>5 ){
60853 int nDelay = 1; /* Pause time in microseconds */
60854 if( cnt>100 ){
60855 VVA_ONLY( pWal->lockError = 1; )
60856 return SQLITE_PROTOCOL15;
60857 }
60858 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
60859 sqlite3OsSleep(pWal->pVfs, nDelay);
60860 }
60861
60862 if( !useWal ){
60863 assert( rc==SQLITE_OK )((void) (0));
60864 if( pWal->bShmUnreliable==0 ){
60865 rc = walIndexReadHdr(pWal, pChanged);
60866 }
60867 if( rc==SQLITE_BUSY5 ){
60868 /* If there is not a recovery running in another thread or process
60869 ** then convert BUSY errors to WAL_RETRY. If recovery is known to
60870 ** be running, convert BUSY to BUSY_RECOVERY. There is a race here
60871 ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
60872 ** would be technically correct. But the race is benign since with
60873 ** WAL_RETRY this routine will be called again and will probably be
60874 ** right on the second iteration.
60875 */
60876 if( pWal->apWiData[0]==0 ){
60877 /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
60878 ** We assume this is a transient condition, so return WAL_RETRY. The
60879 ** xShmMap() implementation used by the default unix and win32 VFS
60880 ** modules may return SQLITE_BUSY due to a race condition in the
60881 ** code that determines whether or not the shared-memory region
60882 ** must be zeroed before the requested page is returned.
60883 */
60884 rc = WAL_RETRY(-1);
60885 }else if( SQLITE_OK0==(rc = walLockShared(pWal, WAL_RECOVER_LOCK2)) ){
60886 walUnlockShared(pWal, WAL_RECOVER_LOCK2);
60887 rc = WAL_RETRY(-1);
60888 }else if( rc==SQLITE_BUSY5 ){
60889 rc = SQLITE_BUSY_RECOVERY(5 | (1<<8));
60890 }
60891 }
60892 if( rc!=SQLITE_OK0 ){
60893 return rc;
60894 }
60895 else if( pWal->bShmUnreliable ){
60896 return walBeginShmUnreliable(pWal, pChanged);
60897 }
60898 }
60899
60900 assert( pWal->nWiData>0 )((void) (0));
60901 assert( pWal->apWiData[0]!=0 )((void) (0));
60902 pInfo = walCkptInfo(pWal);
60903 if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame
60904#ifdef SQLITE_ENABLE_SNAPSHOT
60905 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
60906#endif
60907 ){
60908 /* The WAL has been completely backfilled (or it is empty).
60909 ** and can be safely ignored.
60910 */
60911 rc = walLockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
60912 walShmBarrier(pWal);
60913 if( rc==SQLITE_OK0 ){
60914 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
60915 /* It is not safe to allow the reader to continue here if frames
60916 ** may have been appended to the log before READ_LOCK(0) was obtained.
60917 ** When holding READ_LOCK(0), the reader ignores the entire log file,
60918 ** which implies that the database file contains a trustworthy
60919 ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from
60920 ** happening, this is usually correct.
60921 **
60922 ** However, if frames have been appended to the log (or if the log
60923 ** is wrapped and written for that matter) before the READ_LOCK(0)
60924 ** is obtained, that is not necessarily true. A checkpointer may
60925 ** have started to backfill the appended frames but crashed before
60926 ** it finished. Leaving a corrupt image in the database file.
60927 */
60928 walUnlockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
60929 return WAL_RETRY(-1);
60930 }
60931 pWal->readLock = 0;
60932 return SQLITE_OK0;
60933 }else if( rc!=SQLITE_BUSY5 ){
60934 return rc;
60935 }
60936 }
60937
60938 /* If we get this far, it means that the reader will want to use
60939 ** the WAL to get at content from recent commits. The job now is
60940 ** to select one of the aReadMark[] entries that is closest to
60941 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
60942 */
60943 mxReadMark = 0;
60944 mxI = 0;
60945 mxFrame = pWal->hdr.mxFrame;
60946#ifdef SQLITE_ENABLE_SNAPSHOT
60947 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
60948 mxFrame = pWal->pSnapshot->mxFrame;
60949 }
60950#endif
60951 for(i=1; i<WAL_NREADER(8 -3); i++){
60952 u32 thisMark = AtomicLoad(pInfo->aReadMark+i)(*(pInfo->aReadMark+i));
60953 if( mxReadMark<=thisMark && thisMark<=mxFrame ){
60954 assert( thisMark!=READMARK_NOT_USED )((void) (0));
60955 mxReadMark = thisMark;
60956 mxI = i;
60957 }
60958 }
60959 if( (pWal->readOnly & WAL_SHM_RDONLY2)==0
60960 && (mxReadMark<mxFrame || mxI==0)
60961 ){
60962 for(i=1; i<WAL_NREADER(8 -3); i++){
60963 rc = walLockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
60964 if( rc==SQLITE_OK0 ){
60965 mxReadMark = AtomicStore(pInfo->aReadMark+i,mxFrame)(*(pInfo->aReadMark+i) = (mxFrame));
60966 mxI = i;
60967 walUnlockExclusive(pWal, WAL_READ_LOCK(i)(3+(i)), 1);
60968 break;
60969 }else if( rc!=SQLITE_BUSY5 ){
60970 return rc;
60971 }
60972 }
60973 }
60974 if( mxI==0 ){
60975 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 )((void) (0));
60976 return rc==SQLITE_BUSY5 ? WAL_RETRY(-1) : SQLITE_READONLY_CANTINIT(8 | (5<<8));
60977 }
60978
60979 rc = walLockShared(pWal, WAL_READ_LOCK(mxI)(3+(mxI)));
60980 if( rc ){
60981 return rc==SQLITE_BUSY5 ? WAL_RETRY(-1) : rc;
60982 }
60983 /* Now that the read-lock has been obtained, check that neither the
60984 ** value in the aReadMark[] array or the contents of the wal-index
60985 ** header have changed.
60986 **
60987 ** It is necessary to check that the wal-index header did not change
60988 ** between the time it was read and when the shared-lock was obtained
60989 ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
60990 ** that the log file may have been wrapped by a writer, or that frames
60991 ** that occur later in the log than pWal->hdr.mxFrame may have been
60992 ** copied into the database by a checkpointer. If either of these things
60993 ** happened, then reading the database with the current value of
60994 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
60995 ** instead.
60996 **
60997 ** Before checking that the live wal-index header has not changed
60998 ** since it was read, set Wal.minFrame to the first frame in the wal
60999 ** file that has not yet been checkpointed. This client will not need
61000 ** to read any frames earlier than minFrame from the wal file - they
61001 ** can be safely read directly from the database file.
61002 **
61003 ** Because a ShmBarrier() call is made between taking the copy of
61004 ** nBackfill and checking that the wal-header in shared-memory still
61005 ** matches the one cached in pWal->hdr, it is guaranteed that the
61006 ** checkpointer that set nBackfill was not working with a wal-index
61007 ** header newer than that cached in pWal->hdr. If it were, that could
61008 ** cause a problem. The checkpointer could omit to checkpoint
61009 ** a version of page X that lies before pWal->minFrame (call that version
61010 ** A) on the basis that there is a newer version (version B) of the same
61011 ** page later in the wal file. But if version B happens to like past
61012 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
61013 ** that it can read version A from the database file. However, since
61014 ** we can guarantee that the checkpointer that set nBackfill could not
61015 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
61016 */
61017 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)(*(&pInfo->nBackfill))+1;
61018 walShmBarrier(pWal);
61019 if( AtomicLoad(pInfo->aReadMark+mxI)(*(pInfo->aReadMark+mxI))!=mxReadMark
61020 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
61021 ){
61022 walUnlockShared(pWal, WAL_READ_LOCK(mxI)(3+(mxI)));
61023 return WAL_RETRY(-1);
61024 }else{
61025 assert( mxReadMark<=pWal->hdr.mxFrame )((void) (0));
61026 pWal->readLock = (i16)mxI;
61027 }
61028 return rc;
61029}
61030
61031#ifdef SQLITE_ENABLE_SNAPSHOT
61032/*
61033** Attempt to reduce the value of the WalCkptInfo.nBackfillAttempted
61034** variable so that older snapshots can be accessed. To do this, loop
61035** through all wal frames from nBackfillAttempted to (nBackfill+1),
61036** comparing their content to the corresponding page with the database
61037** file, if any. Set nBackfillAttempted to the frame number of the
61038** first frame for which the wal file content matches the db file.
61039**
61040** This is only really safe if the file-system is such that any page
61041** writes made by earlier checkpointers were atomic operations, which
61042** is not always true. It is also possible that nBackfillAttempted
61043** may be left set to a value larger than expected, if a wal frame
61044** contains content that duplicate of an earlier version of the same
61045** page.
61046**
61047** SQLITE_OK is returned if successful, or an SQLite error code if an
61048** error occurs. It is not an error if nBackfillAttempted cannot be
61049** decreased at all.
61050*/
61051SQLITE_PRIVATEstatic int sqlite3WalSnapshotRecover(Wal *pWal){
61052 int rc;
61053
61054 assert( pWal->readLock>=0 )((void) (0));
61055 rc = walLockExclusive(pWal, WAL_CKPT_LOCK1, 1);
61056 if( rc==SQLITE_OK0 ){
61057 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
61058 int szPage = (int)pWal->szPage;
61059 i64 szDb; /* Size of db file in bytes */
61060
61061 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
61062 if( rc==SQLITE_OK0 ){
61063 void *pBuf1 = sqlite3_malloc(szPage);
61064 void *pBuf2 = sqlite3_malloc(szPage);
61065 if( pBuf1==0 || pBuf2==0 ){
61066 rc = SQLITE_NOMEM7;
61067 }else{
61068 u32 i = pInfo->nBackfillAttempted;
61069 for(i=pInfo->nBackfillAttempted; i>pInfo->nBackfill; i--){
61070 WalHashLoc sLoc; /* Hash table location */
61071 u32 pgno; /* Page number in db file */
61072 i64 iDbOff; /* Offset of db file entry */
61073 i64 iWalOff; /* Offset of wal file entry */
61074
61075 rc = walHashGet(pWal, walFramePage(i), &sLoc);
61076 if( rc!=SQLITE_OK0 ) break;
61077 pgno = sLoc.aPgno[i-sLoc.iZero];
61078 iDbOff = (i64)(pgno-1) * szPage;
61079
61080 if( iDbOff+szPage<=szDb ){
61081 iWalOff = walFrameOffset(i, szPage)( 32 + ((i)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
61082 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
61083
61084 if( rc==SQLITE_OK0 ){
61085 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
61086 }
61087
61088 if( rc!=SQLITE_OK0 || 0==memcmp(pBuf1, pBuf2, szPage) ){
61089 break;
61090 }
61091 }
61092
61093 pInfo->nBackfillAttempted = i-1;
61094 }
61095 }
61096
61097 sqlite3_free(pBuf1);
61098 sqlite3_free(pBuf2);
61099 }
61100 walUnlockExclusive(pWal, WAL_CKPT_LOCK1, 1);
61101 }
61102
61103 return rc;
61104}
61105#endif /* SQLITE_ENABLE_SNAPSHOT */
61106
61107/*
61108** Begin a read transaction on the database.
61109**
61110** This routine used to be called sqlite3OpenSnapshot() and with good reason:
61111** it takes a snapshot of the state of the WAL and wal-index for the current
61112** instant in time. The current thread will continue to use this snapshot.
61113** Other threads might append new content to the WAL and wal-index but
61114** that extra content is ignored by the current thread.
61115**
61116** If the database contents have changes since the previous read
61117** transaction, then *pChanged is set to 1 before returning. The
61118** Pager layer will use this to know that its cache is stale and
61119** needs to be flushed.
61120*/
61121SQLITE_PRIVATEstatic int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
61122 int rc; /* Return code */
61123 int cnt = 0; /* Number of TryBeginRead attempts */
61124
61125#ifdef SQLITE_ENABLE_SNAPSHOT
61126 int bChanged = 0;
61127 WalIndexHdr *pSnapshot = pWal->pSnapshot;
61128 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
61129 bChanged = 1;
61130 }
61131#endif
61132
61133 do{
61134 rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
61135 }while( rc==WAL_RETRY(-1) );
61136 testcase( (rc&0xff)==SQLITE_BUSY );
61137 testcase( (rc&0xff)==SQLITE_IOERR );
61138 testcase( rc==SQLITE_PROTOCOL );
61139 testcase( rc==SQLITE_OK );
61140
61141#ifdef SQLITE_ENABLE_SNAPSHOT
61142 if( rc==SQLITE_OK0 ){
61143 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
61144 /* At this point the client has a lock on an aReadMark[] slot holding
61145 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
61146 ** is populated with the wal-index header corresponding to the head
61147 ** of the wal file. Verify that pSnapshot is still valid before
61148 ** continuing. Reasons why pSnapshot might no longer be valid:
61149 **
61150 ** (1) The WAL file has been reset since the snapshot was taken.
61151 ** In this case, the salt will have changed.
61152 **
61153 ** (2) A checkpoint as been attempted that wrote frames past
61154 ** pSnapshot->mxFrame into the database file. Note that the
61155 ** checkpoint need not have completed for this to cause problems.
61156 */
61157 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
61158
61159 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 )((void) (0));
61160 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame )((void) (0));
61161
61162 /* It is possible that there is a checkpointer thread running
61163 ** concurrent with this code. If this is the case, it may be that the
61164 ** checkpointer has already determined that it will checkpoint
61165 ** snapshot X, where X is later in the wal file than pSnapshot, but
61166 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
61167 ** its intent. To avoid the race condition this leads to, ensure that
61168 ** there is no checkpointer process by taking a shared CKPT lock
61169 ** before checking pInfo->nBackfillAttempted.
61170 **
61171 ** TODO: Does the aReadMark[] lock prevent a checkpointer from doing
61172 ** this already?
61173 */
61174 rc = walLockShared(pWal, WAL_CKPT_LOCK1);
61175
61176 if( rc==SQLITE_OK0 ){
61177 /* Check that the wal file has not been wrapped. Assuming that it has
61178 ** not, also check that no checkpointer has attempted to checkpoint any
61179 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
61180 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
61181 ** with *pSnapshot and set *pChanged as appropriate for opening the
61182 ** snapshot. */
61183 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
61184 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
61185 ){
61186 assert( pWal->readLock>0 )((void) (0));
61187 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
61188 *pChanged = bChanged;
61189 }else{
61190 rc = SQLITE_ERROR_SNAPSHOT(1 | (3<<8));
61191 }
61192
61193 /* Release the shared CKPT lock obtained above. */
61194 walUnlockShared(pWal, WAL_CKPT_LOCK1);
61195 pWal->minFrame = 1;
61196 }
61197
61198
61199 if( rc!=SQLITE_OK0 ){
61200 sqlite3WalEndReadTransaction(pWal);
61201 }
61202 }
61203 }
61204#endif
61205 return rc;
61206}
61207
61208/*
61209** Finish with a read transaction. All this does is release the
61210** read-lock.
61211*/
61212SQLITE_PRIVATEstatic void sqlite3WalEndReadTransaction(Wal *pWal){
61213 sqlite3WalEndWriteTransaction(pWal);
61214 if( pWal->readLock>=0 ){
61215 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)));
61216 pWal->readLock = -1;
61217 }
61218}
61219
61220/*
61221** Search the wal file for page pgno. If found, set *piRead to the frame that
61222** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
61223** to zero.
61224**
61225** Return SQLITE_OK if successful, or an error code if an error occurs. If an
61226** error does occur, the final value of *piRead is undefined.
61227*/
61228SQLITE_PRIVATEstatic int sqlite3WalFindFrame(
61229 Wal *pWal, /* WAL handle */
61230 Pgno pgno, /* Database page number to read data for */
61231 u32 *piRead /* OUT: Frame number (or zero) */
61232){
61233 u32 iRead = 0; /* If !=0, WAL frame to return data from */
61234 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
61235 int iHash; /* Used to loop through N hash tables */
61236 int iMinHash;
61237
61238 /* This routine is only be called from within a read transaction. */
61239 assert( pWal->readLock>=0 || pWal->lockError )((void) (0));
61240
61241 /* If the "last page" field of the wal-index header snapshot is 0, then
61242 ** no data will be read from the wal under any circumstances. Return early
61243 ** in this case as an optimization. Likewise, if pWal->readLock==0,
61244 ** then the WAL is ignored by the reader so return early, as if the
61245 ** WAL were empty.
61246 */
61247 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
61248 *piRead = 0;
61249 return SQLITE_OK0;
61250 }
61251
61252 /* Search the hash table or tables for an entry matching page number
61253 ** pgno. Each iteration of the following for() loop searches one
61254 ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
61255 **
61256 ** This code might run concurrently to the code in walIndexAppend()
61257 ** that adds entries to the wal-index (and possibly to this hash
61258 ** table). This means the value just read from the hash
61259 ** slot (aHash[iKey]) may have been added before or after the
61260 ** current read transaction was opened. Values added after the
61261 ** read transaction was opened may have been written incorrectly -
61262 ** i.e. these slots may contain garbage data. However, we assume
61263 ** that any slots written before the current read transaction was
61264 ** opened remain unmodified.
61265 **
61266 ** For the reasons above, the if(...) condition featured in the inner
61267 ** loop of the following block is more stringent that would be required
61268 ** if we had exclusive access to the hash-table:
61269 **
61270 ** (aPgno[iFrame]==pgno):
61271 ** This condition filters out normal hash-table collisions.
61272 **
61273 ** (iFrame<=iLast):
61274 ** This condition filters out entries that were added to the hash
61275 ** table after the current read-transaction had started.
61276 */
61277 iMinHash = walFramePage(pWal->minFrame);
61278 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
61279 WalHashLoc sLoc; /* Hash table location */
61280 int iKey; /* Hash slot index */
61281 int nCollide; /* Number of hash collisions remaining */
61282 int rc; /* Error code */
61283
61284 rc = walHashGet(pWal, iHash, &sLoc);
61285 if( rc!=SQLITE_OK0 ){
61286 return rc;
61287 }
61288 nCollide = HASHTABLE_NSLOT(4096*2);
61289 for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
61290 u32 iH = sLoc.aHash[iKey];
61291 u32 iFrame = iH + sLoc.iZero;
61292 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){
61293 assert( iFrame>iRead || CORRUPT_DB )((void) (0));
61294 iRead = iFrame;
61295 }
61296 if( (nCollide--)==0 ){
61297 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(61297);
61298 }
61299 }
61300 if( iRead ) break;
61301 }
61302
61303#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
61304 /* If expensive assert() statements are available, do a linear search
61305 ** of the wal-index file content. Make sure the results agree with the
61306 ** result obtained using the hash indexes above. */
61307 {
61308 u32 iRead2 = 0;
61309 u32 iTest;
61310 assert( pWal->bShmUnreliable || pWal->minFrame>0 )((void) (0));
61311 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
61312 if( walFramePgno(pWal, iTest)==pgno ){
61313 iRead2 = iTest;
61314 break;
61315 }
61316 }
61317 assert( iRead==iRead2 )((void) (0));
61318 }
61319#endif
61320
61321 *piRead = iRead;
61322 return SQLITE_OK0;
61323}
61324
61325/*
61326** Read the contents of frame iRead from the wal file into buffer pOut
61327** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
61328** error code otherwise.
61329*/
61330SQLITE_PRIVATEstatic int sqlite3WalReadFrame(
61331 Wal *pWal, /* WAL handle */
61332 u32 iRead, /* Frame to read */
61333 int nOut, /* Size of buffer pOut in bytes */
61334 u8 *pOut /* Buffer to write page data to */
61335){
61336 int sz;
61337 i64 iOffset;
61338 sz = pWal->hdr.szPage;
61339 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
61340 testcase( sz<=32768 );
61341 testcase( sz>=65536 );
61342 iOffset = walFrameOffset(iRead, sz)( 32 + ((iRead)-1)*(i64)((sz)+24) ) + WAL_FRAME_HDRSIZE24;
61343 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
61344 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
61345}
61346
61347/*
61348** Return the size of the database in pages (or zero, if unknown).
61349*/
61350SQLITE_PRIVATEstatic Pgno sqlite3WalDbsize(Wal *pWal){
61351 if( pWal && ALWAYS(pWal->readLock>=0)(pWal->readLock>=0) ){
61352 return pWal->hdr.nPage;
61353 }
61354 return 0;
61355}
61356
61357
61358/*
61359** This function starts a write transaction on the WAL.
61360**
61361** A read transaction must have already been started by a prior call
61362** to sqlite3WalBeginReadTransaction().
61363**
61364** If another thread or process has written into the database since
61365** the read transaction was started, then it is not possible for this
61366** thread to write as doing so would cause a fork. So this routine
61367** returns SQLITE_BUSY in that case and no write transaction is started.
61368**
61369** There can only be a single writer active at a time.
61370*/
61371SQLITE_PRIVATEstatic int sqlite3WalBeginWriteTransaction(Wal *pWal){
61372 int rc;
61373
61374 /* Cannot start a write transaction without first holding a read
61375 ** transaction. */
61376 assert( pWal->readLock>=0 )((void) (0));
61377 assert( pWal->writeLock==0 && pWal->iReCksum==0 )((void) (0));
61378
61379 if( pWal->readOnly ){
61380 return SQLITE_READONLY8;
61381 }
61382
61383 /* Only one writer allowed at a time. Get the write lock. Return
61384 ** SQLITE_BUSY if unable.
61385 */
61386 rc = walLockExclusive(pWal, WAL_WRITE_LOCK0, 1);
61387 if( rc ){
61388 return rc;
61389 }
61390 pWal->writeLock = 1;
61391
61392 /* If another connection has written to the database file since the
61393 ** time the read transaction on this connection was started, then
61394 ** the write is disallowed.
61395 */
61396 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
61397 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
61398 pWal->writeLock = 0;
61399 rc = SQLITE_BUSY_SNAPSHOT(5 | (2<<8));
61400 }
61401
61402 return rc;
61403}
61404
61405/*
61406** End a write transaction. The commit has already been done. This
61407** routine merely releases the lock.
61408*/
61409SQLITE_PRIVATEstatic int sqlite3WalEndWriteTransaction(Wal *pWal){
61410 if( pWal->writeLock ){
61411 walUnlockExclusive(pWal, WAL_WRITE_LOCK0, 1);
61412 pWal->writeLock = 0;
61413 pWal->iReCksum = 0;
61414 pWal->truncateOnCommit = 0;
61415 }
61416 return SQLITE_OK0;
61417}
61418
61419/*
61420** If any data has been written (but not committed) to the log file, this
61421** function moves the write-pointer back to the start of the transaction.
61422**
61423** Additionally, the callback function is invoked for each frame written
61424** to the WAL since the start of the transaction. If the callback returns
61425** other than SQLITE_OK, it is not invoked again and the error code is
61426** returned to the caller.
61427**
61428** Otherwise, if the callback function does not return an error, this
61429** function returns SQLITE_OK.
61430*/
61431SQLITE_PRIVATEstatic int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
61432 int rc = SQLITE_OK0;
61433 if( ALWAYS(pWal->writeLock)(pWal->writeLock) ){
61434 Pgno iMax = pWal->hdr.mxFrame;
61435 Pgno iFrame;
61436
61437 /* Restore the clients cache of the wal-index header to the state it
61438 ** was in before the client began writing to the database.
61439 */
61440 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
61441
61442 for(iFrame=pWal->hdr.mxFrame+1;
61443 ALWAYS(rc==SQLITE_OK)(rc==0) && iFrame<=iMax;
61444 iFrame++
61445 ){
61446 /* This call cannot fail. Unless the page for which the page number
61447 ** is passed as the second argument is (a) in the cache and
61448 ** (b) has an outstanding reference, then xUndo is either a no-op
61449 ** (if (a) is false) or simply expels the page from the cache (if (b)
61450 ** is false).
61451 **
61452 ** If the upper layer is doing a rollback, it is guaranteed that there
61453 ** are no outstanding references to any page other than page 1. And
61454 ** page 1 is never written to the log until the transaction is
61455 ** committed. As a result, the call to xUndo may not fail.
61456 */
61457 assert( walFramePgno(pWal, iFrame)!=1 )((void) (0));
61458 rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
61459 }
61460 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
61461 }
61462 return rc;
61463}
61464
61465/*
61466** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
61467** values. This function populates the array with values required to
61468** "rollback" the write position of the WAL handle back to the current
61469** point in the event of a savepoint rollback (via WalSavepointUndo()).
61470*/
61471SQLITE_PRIVATEstatic void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
61472 assert( pWal->writeLock )((void) (0));
61473 aWalData[0] = pWal->hdr.mxFrame;
61474 aWalData[1] = pWal->hdr.aFrameCksum[0];
61475 aWalData[2] = pWal->hdr.aFrameCksum[1];
61476 aWalData[3] = pWal->nCkpt;
61477}
61478
61479/*
61480** Move the write position of the WAL back to the point identified by
61481** the values in the aWalData[] array. aWalData must point to an array
61482** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
61483** by a call to WalSavepoint().
61484*/
61485SQLITE_PRIVATEstatic int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
61486 int rc = SQLITE_OK0;
61487
61488 assert( pWal->writeLock )((void) (0));
61489 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame )((void) (0));
61490
61491 if( aWalData[3]!=pWal->nCkpt ){
61492 /* This savepoint was opened immediately after the write-transaction
61493 ** was started. Right after that, the writer decided to wrap around
61494 ** to the start of the log. Update the savepoint values to match.
61495 */
61496 aWalData[0] = 0;
61497 aWalData[3] = pWal->nCkpt;
61498 }
61499
61500 if( aWalData[0]<pWal->hdr.mxFrame ){
61501 pWal->hdr.mxFrame = aWalData[0];
61502 pWal->hdr.aFrameCksum[0] = aWalData[1];
61503 pWal->hdr.aFrameCksum[1] = aWalData[2];
61504 walCleanupHash(pWal);
61505 }
61506
61507 return rc;
61508}
61509
61510/*
61511** This function is called just before writing a set of frames to the log
61512** file (see sqlite3WalFrames()). It checks to see if, instead of appending
61513** to the current log file, it is possible to overwrite the start of the
61514** existing log file with the new frames (i.e. "reset" the log). If so,
61515** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
61516** unchanged.
61517**
61518** SQLITE_OK is returned if no error is encountered (regardless of whether
61519** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
61520** if an error occurs.
61521*/
61522static int walRestartLog(Wal *pWal){
61523 int rc = SQLITE_OK0;
61524 int cnt;
61525
61526 if( pWal->readLock==0 ){
61527 volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
61528 assert( pInfo->nBackfill==pWal->hdr.mxFrame )((void) (0));
61529 if( pInfo->nBackfill>0 ){
61530 u32 salt1;
61531 sqlite3_randomness(4, &salt1);
61532 rc = walLockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
61533 if( rc==SQLITE_OK0 ){
61534 /* If all readers are using WAL_READ_LOCK(0) (in other words if no
61535 ** readers are currently using the WAL), then the transactions
61536 ** frames will overwrite the start of the existing log. Update the
61537 ** wal-index header to reflect this.
61538 **
61539 ** In theory it would be Ok to update the cache of the header only
61540 ** at this point. But updating the actual wal-index header is also
61541 ** safe and means there is no special case for sqlite3WalUndo()
61542 ** to handle if this transaction is rolled back. */
61543 walRestartHdr(pWal, salt1);
61544 walUnlockExclusive(pWal, WAL_READ_LOCK(1)(3+(1)), WAL_NREADER(8 -3)-1);
61545 }else if( rc!=SQLITE_BUSY5 ){
61546 return rc;
61547 }
61548 }
61549 walUnlockShared(pWal, WAL_READ_LOCK(0)(3+(0)));
61550 pWal->readLock = -1;
61551 cnt = 0;
61552 do{
61553 int notUsed;
61554 rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
61555 }while( rc==WAL_RETRY(-1) );
61556 assert( (rc&0xff)!=SQLITE_BUSY )((void) (0)); /* BUSY not possible when useWal==1 */
61557 testcase( (rc&0xff)==SQLITE_IOERR );
61558 testcase( rc==SQLITE_PROTOCOL );
61559 testcase( rc==SQLITE_OK );
61560 }
61561 return rc;
61562}
61563
61564/*
61565** Information about the current state of the WAL file and where
61566** the next fsync should occur - passed from sqlite3WalFrames() into
61567** walWriteToLog().
61568*/
61569typedef struct WalWriter {
61570 Wal *pWal; /* The complete WAL information */
61571 sqlite3_file *pFd; /* The WAL file to which we write */
61572 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
61573 int syncFlags; /* Flags for the fsync */
61574 int szPage; /* Size of one page */
61575} WalWriter;
61576
61577/*
61578** Write iAmt bytes of content into the WAL file beginning at iOffset.
61579** Do a sync when crossing the p->iSyncPoint boundary.
61580**
61581** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
61582** first write the part before iSyncPoint, then sync, then write the
61583** rest.
61584*/
61585static int walWriteToLog(
61586 WalWriter *p, /* WAL to write to */
61587 void *pContent, /* Content to be written */
61588 int iAmt, /* Number of bytes to write */
61589 sqlite3_int64 iOffset /* Start writing at this offset */
61590){
61591 int rc;
61592 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
61593 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
61594 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
61595 if( rc ) return rc;
61596 iOffset += iFirstAmt;
61597 iAmt -= iFirstAmt;
61598 pContent = (void*)(iFirstAmt + (char*)pContent);
61599 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 )((void) (0));
61600 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags)((p->syncFlags)&0x03));
61601 if( iAmt==0 || rc ) return rc;
61602 }
61603 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
61604 return rc;
61605}
61606
61607/*
61608** Write out a single frame of the WAL
61609*/
61610static int walWriteOneFrame(
61611 WalWriter *p, /* Where to write the frame */
61612 PgHdr *pPage, /* The page of the frame to be written */
61613 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
61614 sqlite3_int64 iOffset /* Byte offset at which to write */
61615){
61616 int rc; /* Result code from subfunctions */
61617 void *pData; /* Data actually written */
61618 u8 aFrame[WAL_FRAME_HDRSIZE24]; /* Buffer to assemble frame-header in */
61619#if defined(SQLITE_HAS_CODEC)
61620 if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT7;
61621#else
61622 pData = pPage->pData;
61623#endif
61624 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
61625 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
61626 if( rc ) return rc;
61627 /* Write the page data */
61628 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
61629 return rc;
61630}
61631
61632/*
61633** This function is called as part of committing a transaction within which
61634** one or more frames have been overwritten. It updates the checksums for
61635** all frames written to the wal file by the current transaction starting
61636** with the earliest to have been overwritten.
61637**
61638** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
61639*/
61640static int walRewriteChecksums(Wal *pWal, u32 iLast){
61641 const int szPage = pWal->szPage;/* Database page size */
61642 int rc = SQLITE_OK0; /* Return code */
61643 u8 *aBuf; /* Buffer to load data from wal file into */
61644 u8 aFrame[WAL_FRAME_HDRSIZE24]; /* Buffer to assemble frame-headers in */
61645 u32 iRead; /* Next frame to read from wal file */
61646 i64 iCksumOff;
61647
61648 aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE24);
61649 if( aBuf==0 ) return SQLITE_NOMEM_BKPT7;
61650
61651 /* Find the checksum values to use as input for the recalculating the
61652 ** first checksum. If the first frame is frame 1 (implying that the current
61653 ** transaction restarted the wal file), these values must be read from the
61654 ** wal-file header. Otherwise, read them from the frame header of the
61655 ** previous frame. */
61656 assert( pWal->iReCksum>0 )((void) (0));
61657 if( pWal->iReCksum==1 ){
61658 iCksumOff = 24;
61659 }else{
61660 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage)( 32 + ((pWal->iReCksum-1)-1)*(i64)((szPage)+24) ) + 16;
61661 }
61662 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
61663 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
61664 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
61665
61666 iRead = pWal->iReCksum;
61667 pWal->iReCksum = 0;
61668 for(; rc==SQLITE_OK0 && iRead<=iLast; iRead++){
61669 i64 iOff = walFrameOffset(iRead, szPage)( 32 + ((iRead)-1)*(i64)((szPage)+24) );
61670 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE24, iOff);
61671 if( rc==SQLITE_OK0 ){
61672 u32 iPgno, nDbSize;
61673 iPgno = sqlite3Get4byte(aBuf);
61674 nDbSize = sqlite3Get4byte(&aBuf[4]);
61675
61676 walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE24], aFrame);
61677 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
61678 }
61679 }
61680
61681 sqlite3_free(aBuf);
61682 return rc;
61683}
61684
61685/*
61686** Write a set of frames to the log. The caller must hold the write-lock
61687** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
61688*/
61689SQLITE_PRIVATEstatic int sqlite3WalFrames(
61690 Wal *pWal, /* Wal handle to write to */
61691 int szPage, /* Database page-size in bytes */
61692 PgHdr *pList, /* List of dirty pages to write */
61693 Pgno nTruncate, /* Database size after this commit */
61694 int isCommit, /* True if this is a commit */
61695 int sync_flags /* Flags to pass to OsSync() (or 0) */
61696){
61697 int rc; /* Used to catch return codes */
61698 u32 iFrame; /* Next frame address */
61699 PgHdr *p; /* Iterator to run through pList with. */
61700 PgHdr *pLast = 0; /* Last frame in list */
61701 int nExtra = 0; /* Number of extra copies of last page */
61702 int szFrame; /* The size of a single frame */
61703 i64 iOffset; /* Next byte to write in WAL file */
61704 WalWriter w; /* The writer */
61705 u32 iFirst = 0; /* First frame that may be overwritten */
61706 WalIndexHdr *pLive; /* Pointer to shared header */
61707
61708 assert( pList )((void) (0));
61709 assert( pWal->writeLock )((void) (0));
61710
61711 /* If this frame set completes a transaction, then nTruncate>0. If
61712 ** nTruncate==0 then this frame set does not complete the transaction. */
61713 assert( (isCommit!=0)==(nTruncate!=0) )((void) (0));
61714
61715#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
61716 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
61717 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
61718 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
61719 }
61720#endif
61721
61722 pLive = (WalIndexHdr*)walIndexHdr(pWal);
61723 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
61724 iFirst = pLive->mxFrame+1;
61725 }
61726
61727 /* See if it is possible to write these frames into the start of the
61728 ** log file, instead of appending to it at pWal->hdr.mxFrame.
61729 */
61730 if( SQLITE_OK0!=(rc = walRestartLog(pWal)) ){
61731 return rc;
61732 }
61733
61734 /* If this is the first frame written into the log, write the WAL
61735 ** header to the start of the WAL file. See comments at the top of
61736 ** this source file for a description of the WAL header format.
61737 */
61738 iFrame = pWal->hdr.mxFrame;
61739 if( iFrame==0 ){
61740 u8 aWalHdr[WAL_HDRSIZE32]; /* Buffer to assemble wal-header in */
61741 u32 aCksum[2]; /* Checksum for wal-header */
61742
61743 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC0x377f0682 | SQLITE_BIGENDIAN0));
61744 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION3007000);
61745 sqlite3Put4byte(&aWalHdr[8], szPage);
61746 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
61747 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
61748 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
61749 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE32-2*4, 0, aCksum);
61750 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
61751 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
61752
61753 pWal->szPage = szPage;
61754 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN0;
61755 pWal->hdr.aFrameCksum[0] = aCksum[0];
61756 pWal->hdr.aFrameCksum[1] = aCksum[1];
61757 pWal->truncateOnCommit = 1;
61758
61759 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
61760 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
61761 if( rc!=SQLITE_OK0 ){
61762 return rc;
61763 }
61764
61765 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
61766 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
61767 ** an out-of-order write following a WAL restart could result in
61768 ** database corruption. See the ticket:
61769 **
61770 ** https://sqlite.org/src/info/ff5be73dee
61771 */
61772 if( pWal->syncHeader ){
61773 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)(((sync_flags)>>2)&0x03));
61774 if( rc ) return rc;
61775 }
61776 }
61777 assert( (int)pWal->szPage==szPage )((void) (0));
61778
61779 /* Setup information needed to write frames into the WAL */
61780 w.pWal = pWal;
61781 w.pFd = pWal->pWalFd;
61782 w.iSyncPoint = 0;
61783 w.syncFlags = sync_flags;
61784 w.szPage = szPage;
61785 iOffset = walFrameOffset(iFrame+1, szPage)( 32 + ((iFrame+1)-1)*(i64)((szPage)+24) );
61786 szFrame = szPage + WAL_FRAME_HDRSIZE24;
61787
61788 /* Write all frames into the log file exactly once */
61789 for(p=pList; p; p=p->pDirty){
61790 int nDbSize; /* 0 normally. Positive == commit flag */
61791
61792 /* Check if this page has already been written into the wal file by
61793 ** the current transaction. If so, overwrite the existing frame and
61794 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
61795 ** checksums must be recomputed when the transaction is committed. */
61796 if( iFirst && (p->pDirty || isCommit==0) ){
61797 u32 iWrite = 0;
61798 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
61799 assert( rc==SQLITE_OK || iWrite==0 )((void) (0));
61800 if( iWrite>=iFirst ){
61801 i64 iOff = walFrameOffset(iWrite, szPage)( 32 + ((iWrite)-1)*(i64)((szPage)+24) ) + WAL_FRAME_HDRSIZE24;
61802 void *pData;
61803 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
61804 pWal->iReCksum = iWrite;
61805 }
61806#if defined(SQLITE_HAS_CODEC)
61807 if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM7;
61808#else
61809 pData = p->pData;
61810#endif
61811 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
61812 if( rc ) return rc;
61813 p->flags &= ~PGHDR_WAL_APPEND0x040;
61814 continue;
61815 }
61816 }
61817
61818 iFrame++;
61819 assert( iOffset==walFrameOffset(iFrame, szPage) )((void) (0));
61820 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
61821 rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
61822 if( rc ) return rc;
61823 pLast = p;
61824 iOffset += szFrame;
61825 p->flags |= PGHDR_WAL_APPEND0x040;
61826 }
61827
61828 /* Recalculate checksums within the wal file if required. */
61829 if( isCommit && pWal->iReCksum ){
61830 rc = walRewriteChecksums(pWal, iFrame);
61831 if( rc ) return rc;
61832 }
61833
61834 /* If this is the end of a transaction, then we might need to pad
61835 ** the transaction and/or sync the WAL file.
61836 **
61837 ** Padding and syncing only occur if this set of frames complete a
61838 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
61839 ** or synchronous==OFF, then no padding or syncing are needed.
61840 **
61841 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
61842 ** needed and only the sync is done. If padding is needed, then the
61843 ** final frame is repeated (with its commit mark) until the next sector
61844 ** boundary is crossed. Only the part of the WAL prior to the last
61845 ** sector boundary is synced; the part of the last frame that extends
61846 ** past the sector boundary is written after the sync.
61847 */
61848 if( isCommit && WAL_SYNC_FLAGS(sync_flags)((sync_flags)&0x03)!=0 ){
61849 int bSync = 1;
61850 if( pWal->padToSectorBoundary ){
61851 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
61852 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
61853 bSync = (w.iSyncPoint==iOffset);
61854 testcase( bSync );
61855 while( iOffset<w.iSyncPoint ){
61856 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
61857 if( rc ) return rc;
61858 iOffset += szFrame;
61859 nExtra++;
61860 }
61861 }
61862 if( bSync ){
61863 assert( rc==SQLITE_OK )((void) (0));
61864 rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags)((sync_flags)&0x03));
61865 }
61866 }
61867
61868 /* If this frame set completes the first transaction in the WAL and
61869 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
61870 ** journal size limit, if possible.
61871 */
61872 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
61873 i64 sz = pWal->mxWalSize;
61874 if( walFrameOffset(iFrame+nExtra+1, szPage)( 32 + ((iFrame+nExtra+1)-1)*(i64)((szPage)+24) )>pWal->mxWalSize ){
61875 sz = walFrameOffset(iFrame+nExtra+1, szPage)( 32 + ((iFrame+nExtra+1)-1)*(i64)((szPage)+24) );
61876 }
61877 walLimitSize(pWal, sz);
61878 pWal->truncateOnCommit = 0;
61879 }
61880
61881 /* Append data to the wal-index. It is not necessary to lock the
61882 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
61883 ** guarantees that there are no other writers, and no data that may
61884 ** be in use by existing readers is being overwritten.
61885 */
61886 iFrame = pWal->hdr.mxFrame;
61887 for(p=pList; p && rc==SQLITE_OK0; p=p->pDirty){
61888 if( (p->flags & PGHDR_WAL_APPEND0x040)==0 ) continue;
61889 iFrame++;
61890 rc = walIndexAppend(pWal, iFrame, p->pgno);
61891 }
61892 while( rc==SQLITE_OK0 && nExtra>0 ){
61893 iFrame++;
61894 nExtra--;
61895 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
61896 }
61897
61898 if( rc==SQLITE_OK0 ){
61899 /* Update the private copy of the header. */
61900 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
61901 testcase( szPage<=32768 );
61902 testcase( szPage>=65536 );
61903 pWal->hdr.mxFrame = iFrame;
61904 if( isCommit ){
61905 pWal->hdr.iChange++;
61906 pWal->hdr.nPage = nTruncate;
61907 }
61908 /* If this is a commit, update the wal-index header too. */
61909 if( isCommit ){
61910 walIndexWriteHdr(pWal);
61911 pWal->iCallback = iFrame;
61912 }
61913 }
61914
61915 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
61916 return rc;
61917}
61918
61919/*
61920** This routine is called to implement sqlite3_wal_checkpoint() and
61921** related interfaces.
61922**
61923** Obtain a CHECKPOINT lock and then backfill as much information as
61924** we can from WAL into the database.
61925**
61926** If parameter xBusy is not NULL, it is a pointer to a busy-handler
61927** callback. In this case this function runs a blocking checkpoint.
61928*/
61929SQLITE_PRIVATEstatic int sqlite3WalCheckpoint(
61930 Wal *pWal, /* Wal connection */
61931 sqlite3 *db, /* Check this handle's interrupt flag */
61932 int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
61933 int (*xBusy)(void*), /* Function to call when busy */
61934 void *pBusyArg, /* Context argument for xBusyHandler */
61935 int sync_flags, /* Flags to sync db file with (or 0) */
61936 int nBuf, /* Size of temporary buffer */
61937 u8 *zBuf, /* Temporary buffer to use */
61938 int *pnLog, /* OUT: Number of frames in WAL */
61939 int *pnCkpt /* OUT: Number of backfilled frames in WAL */
61940){
61941 int rc; /* Return code */
61942 int isChanged = 0; /* True if a new wal-index header is loaded */
61943 int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
61944 int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
61945
61946 assert( pWal->ckptLock==0 )((void) (0));
61947 assert( pWal->writeLock==0 )((void) (0));
61948
61949 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
61950 ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
61951 assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 )((void) (0));
61952
61953 if( pWal->readOnly ) return SQLITE_READONLY8;
61954 WALTRACE(("WAL%p: checkpoint begins\n", pWal));
61955
61956 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
61957 ** "checkpoint" lock on the database file. */
61958 rc = walLockExclusive(pWal, WAL_CKPT_LOCK1, 1);
61959 if( rc ){
61960 /* EVIDENCE-OF: R-10421-19736 If any other process is running a
61961 ** checkpoint operation at the same time, the lock cannot be obtained and
61962 ** SQLITE_BUSY is returned.
61963 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
61964 ** it will not be invoked in this case.
61965 */
61966 testcase( rc==SQLITE_BUSY );
61967 testcase( xBusy!=0 );
61968 return rc;
61969 }
61970 pWal->ckptLock = 1;
61971
61972 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
61973 ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
61974 ** file.
61975 **
61976 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
61977 ** immediately, and a busy-handler is configured, it is invoked and the
61978 ** writer lock retried until either the busy-handler returns 0 or the
61979 ** lock is successfully obtained.
61980 */
61981 if( eMode!=SQLITE_CHECKPOINT_PASSIVE0 ){
61982 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK0, 1);
61983 if( rc==SQLITE_OK0 ){
61984 pWal->writeLock = 1;
61985 }else if( rc==SQLITE_BUSY5 ){
61986 eMode2 = SQLITE_CHECKPOINT_PASSIVE0;
61987 xBusy2 = 0;
61988 rc = SQLITE_OK0;
61989 }
61990 }
61991
61992 /* Read the wal-index header. */
61993 if( rc==SQLITE_OK0 ){
61994 rc = walIndexReadHdr(pWal, &isChanged);
61995 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
61996 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
61997 }
61998 }
61999
62000 /* Copy data from the log to the database file. */
62001 if( rc==SQLITE_OK0 ){
62002
62003 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
62004 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(62004);
62005 }else{
62006 rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
62007 }
62008
62009 /* If no error occurred, set the output variables. */
62010 if( rc==SQLITE_OK0 || rc==SQLITE_BUSY5 ){
62011 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
62012 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
62013 }
62014 }
62015
62016 if( isChanged ){
62017 /* If a new wal-index header was loaded before the checkpoint was
62018 ** performed, then the pager-cache associated with pWal is now
62019 ** out of date. So zero the cached wal-index header to ensure that
62020 ** next time the pager opens a snapshot on this database it knows that
62021 ** the cache needs to be reset.
62022 */
62023 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
62024 }
62025
62026 /* Release the locks. */
62027 sqlite3WalEndWriteTransaction(pWal);
62028 walUnlockExclusive(pWal, WAL_CKPT_LOCK1, 1);
62029 pWal->ckptLock = 0;
62030 WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
62031 return (rc==SQLITE_OK0 && eMode!=eMode2 ? SQLITE_BUSY5 : rc);
62032}
62033
62034/* Return the value to pass to a sqlite3_wal_hook callback, the
62035** number of frames in the WAL at the point of the last commit since
62036** sqlite3WalCallback() was called. If no commits have occurred since
62037** the last call, then return 0.
62038*/
62039SQLITE_PRIVATEstatic int sqlite3WalCallback(Wal *pWal){
62040 u32 ret = 0;
62041 if( pWal ){
62042 ret = pWal->iCallback;
62043 pWal->iCallback = 0;
62044 }
62045 return (int)ret;
62046}
62047
62048/*
62049** This function is called to change the WAL subsystem into or out
62050** of locking_mode=EXCLUSIVE.
62051**
62052** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
62053** into locking_mode=NORMAL. This means that we must acquire a lock
62054** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
62055** or if the acquisition of the lock fails, then return 0. If the
62056** transition out of exclusive-mode is successful, return 1. This
62057** operation must occur while the pager is still holding the exclusive
62058** lock on the main database file.
62059**
62060** If op is one, then change from locking_mode=NORMAL into
62061** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
62062** be released. Return 1 if the transition is made and 0 if the
62063** WAL is already in exclusive-locking mode - meaning that this
62064** routine is a no-op. The pager must already hold the exclusive lock
62065** on the main database file before invoking this operation.
62066**
62067** If op is negative, then do a dry-run of the op==1 case but do
62068** not actually change anything. The pager uses this to see if it
62069** should acquire the database exclusive lock prior to invoking
62070** the op==1 case.
62071*/
62072SQLITE_PRIVATEstatic int sqlite3WalExclusiveMode(Wal *pWal, int op){
62073 int rc;
62074 assert( pWal->writeLock==0 )((void) (0));
62075 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 )((void) (0));
62076
62077 /* pWal->readLock is usually set, but might be -1 if there was a
62078 ** prior error while attempting to acquire are read-lock. This cannot
62079 ** happen if the connection is actually in exclusive mode (as no xShmLock
62080 ** locks are taken in this case). Nor should the pager attempt to
62081 ** upgrade to exclusive-mode following such an error.
62082 */
62083 assert( pWal->readLock>=0 || pWal->lockError )((void) (0));
62084 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) )((void) (0));
62085
62086 if( op==0 ){
62087 if( pWal->exclusiveMode!=WAL_NORMAL_MODE0 ){
62088 pWal->exclusiveMode = WAL_NORMAL_MODE0;
62089 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)))!=SQLITE_OK0 ){
62090 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
62091 }
62092 rc = pWal->exclusiveMode==WAL_NORMAL_MODE0;
62093 }else{
62094 /* Already in locking_mode=NORMAL */
62095 rc = 0;
62096 }
62097 }else if( op>0 ){
62098 assert( pWal->exclusiveMode==WAL_NORMAL_MODE )((void) (0));
62099 assert( pWal->readLock>=0 )((void) (0));
62100 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)(3+(pWal->readLock)));
62101 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE1;
62102 rc = 1;
62103 }else{
62104 rc = pWal->exclusiveMode==WAL_NORMAL_MODE0;
62105 }
62106 return rc;
62107}
62108
62109/*
62110** Return true if the argument is non-NULL and the WAL module is using
62111** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
62112** WAL module is using shared-memory, return false.
62113*/
62114SQLITE_PRIVATEstatic int sqlite3WalHeapMemory(Wal *pWal){
62115 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE2 );
62116}
62117
62118#ifdef SQLITE_ENABLE_SNAPSHOT
62119/* Create a snapshot object. The content of a snapshot is opaque to
62120** every other subsystem, so the WAL module can put whatever it needs
62121** in the object.
62122*/
62123SQLITE_PRIVATEstatic int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){
62124 int rc = SQLITE_OK0;
62125 WalIndexHdr *pRet;
62126 static const u32 aZero[4] = { 0, 0, 0, 0 };
62127
62128 assert( pWal->readLock>=0 && pWal->writeLock==0 )((void) (0));
62129
62130 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
62131 *ppSnapshot = 0;
62132 return SQLITE_ERROR1;
62133 }
62134 pRet = (WalIndexHdr*)sqlite3_malloc(sizeof(WalIndexHdr));
62135 if( pRet==0 ){
62136 rc = SQLITE_NOMEM_BKPT7;
62137 }else{
62138 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
62139 *ppSnapshot = (sqlite3_snapshot*)pRet;
62140 }
62141
62142 return rc;
62143}
62144
62145/* Try to open on pSnapshot when the next read-transaction starts
62146*/
62147SQLITE_PRIVATEstatic void sqlite3WalSnapshotOpen(Wal *pWal, sqlite3_snapshot *pSnapshot){
62148 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
62149}
62150
62151/*
62152** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
62153** p1 is older than p2 and zero if p1 and p2 are the same snapshot.
62154*/
62155SQLITE_API int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){
62156 WalIndexHdr *pHdr1 = (WalIndexHdr*)p1;
62157 WalIndexHdr *pHdr2 = (WalIndexHdr*)p2;
62158
62159 /* aSalt[0] is a copy of the value stored in the wal file header. It
62160 ** is incremented each time the wal file is restarted. */
62161 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
62162 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
62163 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
62164 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
62165 return 0;
62166}
62167
62168/*
62169** The caller currently has a read transaction open on the database.
62170** This function takes a SHARED lock on the CHECKPOINTER slot and then
62171** checks if the snapshot passed as the second argument is still
62172** available. If so, SQLITE_OK is returned.
62173**
62174** If the snapshot is not available, SQLITE_ERROR is returned. Or, if
62175** the CHECKPOINTER lock cannot be obtained, SQLITE_BUSY. If any error
62176** occurs (any value other than SQLITE_OK is returned), the CHECKPOINTER
62177** lock is released before returning.
62178*/
62179SQLITE_PRIVATEstatic int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){
62180 int rc;
62181 rc = walLockShared(pWal, WAL_CKPT_LOCK1);
62182 if( rc==SQLITE_OK0 ){
62183 WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot;
62184 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
62185 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
62186 ){
62187 rc = SQLITE_ERROR_SNAPSHOT(1 | (3<<8));
62188 walUnlockShared(pWal, WAL_CKPT_LOCK1);
62189 }
62190 }
62191 return rc;
62192}
62193
62194/*
62195** Release a lock obtained by an earlier successful call to
62196** sqlite3WalSnapshotCheck().
62197*/
62198SQLITE_PRIVATEstatic void sqlite3WalSnapshotUnlock(Wal *pWal){
62199 assert( pWal )((void) (0));
62200 walUnlockShared(pWal, WAL_CKPT_LOCK1);
62201}
62202
62203
62204#endif /* SQLITE_ENABLE_SNAPSHOT */
62205
62206#ifdef SQLITE_ENABLE_ZIPVFS
62207/*
62208** If the argument is not NULL, it points to a Wal object that holds a
62209** read-lock. This function returns the database page-size if it is known,
62210** or zero if it is not (or if pWal is NULL).
62211*/
62212SQLITE_PRIVATEstatic int sqlite3WalFramesize(Wal *pWal){
62213 assert( pWal==0 || pWal->readLock>=0 )((void) (0));
62214 return (pWal ? pWal->szPage : 0);
62215}
62216#endif
62217
62218/* Return the sqlite3_file object for the WAL file
62219*/
62220SQLITE_PRIVATEstatic sqlite3_file *sqlite3WalFile(Wal *pWal){
62221 return pWal->pWalFd;
62222}
62223
62224#endif /* #ifndef SQLITE_OMIT_WAL */
62225
62226/************** End of wal.c *************************************************/
62227/************** Begin file btmutex.c *****************************************/
62228/*
62229** 2007 August 27
62230**
62231** The author disclaims copyright to this source code. In place of
62232** a legal notice, here is a blessing:
62233**
62234** May you do good and not evil.
62235** May you find forgiveness for yourself and forgive others.
62236** May you share freely, never taking more than you give.
62237**
62238*************************************************************************
62239**
62240** This file contains code used to implement mutexes on Btree objects.
62241** This code really belongs in btree.c. But btree.c is getting too
62242** big and we want to break it down some. This packaged seemed like
62243** a good breakout.
62244*/
62245/************** Include btreeInt.h in the middle of btmutex.c ****************/
62246/************** Begin file btreeInt.h ****************************************/
62247/*
62248** 2004 April 6
62249**
62250** The author disclaims copyright to this source code. In place of
62251** a legal notice, here is a blessing:
62252**
62253** May you do good and not evil.
62254** May you find forgiveness for yourself and forgive others.
62255** May you share freely, never taking more than you give.
62256**
62257*************************************************************************
62258** This file implements an external (disk-based) database using BTrees.
62259** For a detailed discussion of BTrees, refer to
62260**
62261** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
62262** "Sorting And Searching", pages 473-480. Addison-Wesley
62263** Publishing Company, Reading, Massachusetts.
62264**
62265** The basic idea is that each page of the file contains N database
62266** entries and N+1 pointers to subpages.
62267**
62268** ----------------------------------------------------------------
62269** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
62270** ----------------------------------------------------------------
62271**
62272** All of the keys on the page that Ptr(0) points to have values less
62273** than Key(0). All of the keys on page Ptr(1) and its subpages have
62274** values greater than Key(0) and less than Key(1). All of the keys
62275** on Ptr(N) and its subpages have values greater than Key(N-1). And
62276** so forth.
62277**
62278** Finding a particular key requires reading O(log(M)) pages from the
62279** disk where M is the number of entries in the tree.
62280**
62281** In this implementation, a single file can hold one or more separate
62282** BTrees. Each BTree is identified by the index of its root page. The
62283** key and data for any entry are combined to form the "payload". A
62284** fixed amount of payload can be carried directly on the database
62285** page. If the payload is larger than the preset amount then surplus
62286** bytes are stored on overflow pages. The payload for an entry
62287** and the preceding pointer are combined to form a "Cell". Each
62288** page has a small header which contains the Ptr(N) pointer and other
62289** information such as the size of key and data.
62290**
62291** FORMAT DETAILS
62292**
62293** The file is divided into pages. The first page is called page 1,
62294** the second is page 2, and so forth. A page number of zero indicates
62295** "no such page". The page size can be any power of 2 between 512 and 65536.
62296** Each page can be either a btree page, a freelist page, an overflow
62297** page, or a pointer-map page.
62298**
62299** The first page is always a btree page. The first 100 bytes of the first
62300** page contain a special header (the "file header") that describes the file.
62301** The format of the file header is as follows:
62302**
62303** OFFSET SIZE DESCRIPTION
62304** 0 16 Header string: "SQLite format 3\000"
62305** 16 2 Page size in bytes. (1 means 65536)
62306** 18 1 File format write version
62307** 19 1 File format read version
62308** 20 1 Bytes of unused space at the end of each page
62309** 21 1 Max embedded payload fraction (must be 64)
62310** 22 1 Min embedded payload fraction (must be 32)
62311** 23 1 Min leaf payload fraction (must be 32)
62312** 24 4 File change counter
62313** 28 4 Reserved for future use
62314** 32 4 First freelist page
62315** 36 4 Number of freelist pages in the file
62316** 40 60 15 4-byte meta values passed to higher layers
62317**
62318** 40 4 Schema cookie
62319** 44 4 File format of schema layer
62320** 48 4 Size of page cache
62321** 52 4 Largest root-page (auto/incr_vacuum)
62322** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
62323** 60 4 User version
62324** 64 4 Incremental vacuum mode
62325** 68 4 Application-ID
62326** 72 20 unused
62327** 92 4 The version-valid-for number
62328** 96 4 SQLITE_VERSION_NUMBER
62329**
62330** All of the integer values are big-endian (most significant byte first).
62331**
62332** The file change counter is incremented when the database is changed
62333** This counter allows other processes to know when the file has changed
62334** and thus when they need to flush their cache.
62335**
62336** The max embedded payload fraction is the amount of the total usable
62337** space in a page that can be consumed by a single cell for standard
62338** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
62339** is to limit the maximum cell size so that at least 4 cells will fit
62340** on one page. Thus the default max embedded payload fraction is 64.
62341**
62342** If the payload for a cell is larger than the max payload, then extra
62343** payload is spilled to overflow pages. Once an overflow page is allocated,
62344** as many bytes as possible are moved into the overflow pages without letting
62345** the cell size drop below the min embedded payload fraction.
62346**
62347** The min leaf payload fraction is like the min embedded payload fraction
62348** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
62349** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
62350** not specified in the header.
62351**
62352** Each btree pages is divided into three sections: The header, the
62353** cell pointer array, and the cell content area. Page 1 also has a 100-byte
62354** file header that occurs before the page header.
62355**
62356** |----------------|
62357** | file header | 100 bytes. Page 1 only.
62358** |----------------|
62359** | page header | 8 bytes for leaves. 12 bytes for interior nodes
62360** |----------------|
62361** | cell pointer | | 2 bytes per cell. Sorted order.
62362** | array | | Grows downward
62363** | | v
62364** |----------------|
62365** | unallocated |
62366** | space |
62367** |----------------| ^ Grows upwards
62368** | cell content | | Arbitrary order interspersed with freeblocks.
62369** | area | | and free space fragments.
62370** |----------------|
62371**
62372** The page headers looks like this:
62373**
62374** OFFSET SIZE DESCRIPTION
62375** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
62376** 1 2 byte offset to the first freeblock
62377** 3 2 number of cells on this page
62378** 5 2 first byte of the cell content area
62379** 7 1 number of fragmented free bytes
62380** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
62381**
62382** The flags define the format of this btree page. The leaf flag means that
62383** this page has no children. The zerodata flag means that this page carries
62384** only keys and no data. The intkey flag means that the key is an integer
62385** which is stored in the key size entry of the cell header rather than in
62386** the payload area.
62387**
62388** The cell pointer array begins on the first byte after the page header.
62389** The cell pointer array contains zero or more 2-byte numbers which are
62390** offsets from the beginning of the page to the cell content in the cell
62391** content area. The cell pointers occur in sorted order. The system strives
62392** to keep free space after the last cell pointer so that new cells can
62393** be easily added without having to defragment the page.
62394**
62395** Cell content is stored at the very end of the page and grows toward the
62396** beginning of the page.
62397**
62398** Unused space within the cell content area is collected into a linked list of
62399** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
62400** to the first freeblock is given in the header. Freeblocks occur in
62401** increasing order. Because a freeblock must be at least 4 bytes in size,
62402** any group of 3 or fewer unused bytes in the cell content area cannot
62403** exist on the freeblock chain. A group of 3 or fewer free bytes is called
62404** a fragment. The total number of bytes in all fragments is recorded.
62405** in the page header at offset 7.
62406**
62407** SIZE DESCRIPTION
62408** 2 Byte offset of the next freeblock
62409** 2 Bytes in this freeblock
62410**
62411** Cells are of variable length. Cells are stored in the cell content area at
62412** the end of the page. Pointers to the cells are in the cell pointer array
62413** that immediately follows the page header. Cells is not necessarily
62414** contiguous or in order, but cell pointers are contiguous and in order.
62415**
62416** Cell content makes use of variable length integers. A variable
62417** length integer is 1 to 9 bytes where the lower 7 bits of each
62418** byte are used. The integer consists of all bytes that have bit 8 set and
62419** the first byte with bit 8 clear. The most significant byte of the integer
62420** appears first. A variable-length integer may not be more than 9 bytes long.
62421** As a special case, all 8 bytes of the 9th byte are used as data. This
62422** allows a 64-bit integer to be encoded in 9 bytes.
62423**
62424** 0x00 becomes 0x00000000
62425** 0x7f becomes 0x0000007f
62426** 0x81 0x00 becomes 0x00000080
62427** 0x82 0x00 becomes 0x00000100
62428** 0x80 0x7f becomes 0x0000007f
62429** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678
62430** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
62431**
62432** Variable length integers are used for rowids and to hold the number of
62433** bytes of key and data in a btree cell.
62434**
62435** The content of a cell looks like this:
62436**
62437** SIZE DESCRIPTION
62438** 4 Page number of the left child. Omitted if leaf flag is set.
62439** var Number of bytes of data. Omitted if the zerodata flag is set.
62440** var Number of bytes of key. Or the key itself if intkey flag is set.
62441** * Payload
62442** 4 First page of the overflow chain. Omitted if no overflow
62443**
62444** Overflow pages form a linked list. Each page except the last is completely
62445** filled with data (pagesize - 4 bytes). The last page can have as little
62446** as 1 byte of data.
62447**
62448** SIZE DESCRIPTION
62449** 4 Page number of next overflow page
62450** * Data
62451**
62452** Freelist pages come in two subtypes: trunk pages and leaf pages. The
62453** file header points to the first in a linked list of trunk page. Each trunk
62454** page points to multiple leaf pages. The content of a leaf page is
62455** unspecified. A trunk page looks like this:
62456**
62457** SIZE DESCRIPTION
62458** 4 Page number of next trunk page
62459** 4 Number of leaf pointers on this page
62460** * zero or more pages numbers of leaves
62461*/
62462/* #include "sqliteInt.h" */
62463
62464
62465/* The following value is the maximum cell size assuming a maximum page
62466** size give above.
62467*/
62468#define MX_CELL_SIZE(pBt)((int)(pBt->pageSize-8)) ((int)(pBt->pageSize-8))
62469
62470/* The maximum number of cells on a single page of the database. This
62471** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself
62472** plus 2 bytes for the index to the cell in the page header). Such
62473** small cells will be rare, but they are possible.
62474*/
62475#define MX_CELL(pBt)((pBt->pageSize-8)/6) ((pBt->pageSize-8)/6)
62476
62477/* Forward declarations */
62478typedef struct MemPage MemPage;
62479typedef struct BtLock BtLock;
62480typedef struct CellInfo CellInfo;
62481
62482/*
62483** This is a magic string that appears at the beginning of every
62484** SQLite database in order to identify the file as a real database.
62485**
62486** You can change this value at compile-time by specifying a
62487** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
62488** header must be exactly 16 bytes including the zero-terminator so
62489** the string itself should be 15 characters long. If you change
62490** the header, then your custom library will not be able to read
62491** databases generated by the standard tools and the standard tools
62492** will not be able to read databases created by your custom library.
62493*/
62494#ifndef SQLITE_FILE_HEADER"SQLite format 3" /* 123456789 123456 */
62495# define SQLITE_FILE_HEADER"SQLite format 3" "SQLite format 3"
62496#endif
62497
62498/*
62499** Page type flags. An ORed combination of these flags appear as the
62500** first byte of on-disk image of every BTree page.
62501*/
62502#define PTF_INTKEY0x01 0x01
62503#define PTF_ZERODATA0x02 0x02
62504#define PTF_LEAFDATA0x04 0x04
62505#define PTF_LEAF0x08 0x08
62506
62507/*
62508** An instance of this object stores information about each a single database
62509** page that has been loaded into memory. The information in this object
62510** is derived from the raw on-disk page content.
62511**
62512** As each database page is loaded into memory, the pager allocats an
62513** instance of this object and zeros the first 8 bytes. (This is the
62514** "extra" information associated with each page of the pager.)
62515**
62516** Access to all fields of this structure is controlled by the mutex
62517** stored in MemPage.pBt->mutex.
62518*/
62519struct MemPage {
62520 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
62521 u8 bBusy; /* Prevent endless loops on corrupt database files */
62522 u8 intKey; /* True if table b-trees. False for index b-trees */
62523 u8 intKeyLeaf; /* True if the leaf of an intKey table */
62524 Pgno pgno; /* Page number for this page */
62525 /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
62526 ** is allocated. All fields that follow must be initialized before use */
62527 u8 leaf; /* True if a leaf page */
62528 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
62529 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
62530 u8 max1bytePayload; /* min(maxLocal,127) */
62531 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
62532 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
62533 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
62534 u16 cellOffset; /* Index in aData of first cell pointer */
62535 int nFree; /* Number of free bytes on the page. -1 for unknown */
62536 u16 nCell; /* Number of cells on this page, local and ovfl */
62537 u16 maskPage; /* Mask for page offset */
62538 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
62539 ** non-overflow cell */
62540 u8 *apOvfl[4]; /* Pointers to the body of overflow cells */
62541 BtShared *pBt; /* Pointer to BtShared that this page is part of */
62542 u8 *aData; /* Pointer to disk image of the page data */
62543 u8 *aDataEnd; /* One byte past the end of usable data */
62544 u8 *aCellIdx; /* The cell index area */
62545 u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */
62546 DbPage *pDbPage; /* Pager page handle */
62547 u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */
62548 void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
62549};
62550
62551/*
62552** A linked list of the following structures is stored at BtShared.pLock.
62553** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
62554** is opened on the table with root page BtShared.iTable. Locks are removed
62555** from this list when a transaction is committed or rolled back, or when
62556** a btree handle is closed.
62557*/
62558struct BtLock {
62559 Btree *pBtree; /* Btree handle holding this lock */
62560 Pgno iTable; /* Root page of table */
62561 u8 eLock; /* READ_LOCK or WRITE_LOCK */
62562 BtLock *pNext; /* Next in BtShared.pLock list */
62563};
62564
62565/* Candidate values for BtLock.eLock */
62566#define READ_LOCK1 1
62567#define WRITE_LOCK2 2
62568
62569/* A Btree handle
62570**
62571** A database connection contains a pointer to an instance of
62572** this object for every database file that it has open. This structure
62573** is opaque to the database connection. The database connection cannot
62574** see the internals of this structure and only deals with pointers to
62575** this structure.
62576**
62577** For some database files, the same underlying database cache might be
62578** shared between multiple connections. In that case, each connection
62579** has it own instance of this object. But each instance of this object
62580** points to the same BtShared object. The database cache and the
62581** schema associated with the database file are all contained within
62582** the BtShared object.
62583**
62584** All fields in this structure are accessed under sqlite3.mutex.
62585** The pBt pointer itself may not be changed while there exists cursors
62586** in the referenced BtShared that point back to this Btree since those
62587** cursors have to go through this Btree to find their BtShared and
62588** they often do so without holding sqlite3.mutex.
62589*/
62590struct Btree {
62591 sqlite3 *db; /* The database connection holding this btree */
62592 BtShared *pBt; /* Sharable content of this btree */
62593 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
62594 u8 sharable; /* True if we can share pBt with another db */
62595 u8 locked; /* True if db currently has pBt locked */
62596 u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */
62597 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */
62598 int nBackup; /* Number of backup operations reading this btree */
62599 u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */
62600 Btree *pNext; /* List of other sharable Btrees from the same db */
62601 Btree *pPrev; /* Back pointer of the same list */
62602#ifndef SQLITE_OMIT_SHARED_CACHE
62603 BtLock lock; /* Object used to lock page 1 */
62604#endif
62605};
62606
62607/*
62608** Btree.inTrans may take one of the following values.
62609**
62610** If the shared-data extension is enabled, there may be multiple users
62611** of the Btree structure. At most one of these may open a write transaction,
62612** but any number may have active read transactions.
62613*/
62614#define TRANS_NONE0 0
62615#define TRANS_READ1 1
62616#define TRANS_WRITE2 2
62617
62618/*
62619** An instance of this object represents a single database file.
62620**
62621** A single database file can be in use at the same time by two
62622** or more database connections. When two or more connections are
62623** sharing the same database file, each connection has it own
62624** private Btree object for the file and each of those Btrees points
62625** to this one BtShared object. BtShared.nRef is the number of
62626** connections currently sharing this database file.
62627**
62628** Fields in this structure are accessed under the BtShared.mutex
62629** mutex, except for nRef and pNext which are accessed under the
62630** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field
62631** may not be modified once it is initially set as long as nRef>0.
62632** The pSchema field may be set once under BtShared.mutex and
62633** thereafter is unchanged as long as nRef>0.
62634**
62635** isPending:
62636**
62637** If a BtShared client fails to obtain a write-lock on a database
62638** table (because there exists one or more read-locks on the table),
62639** the shared-cache enters 'pending-lock' state and isPending is
62640** set to true.
62641**
62642** The shared-cache leaves the 'pending lock' state when either of
62643** the following occur:
62644**
62645** 1) The current writer (BtShared.pWriter) concludes its transaction, OR
62646** 2) The number of locks held by other connections drops to zero.
62647**
62648** while in the 'pending-lock' state, no connection may start a new
62649** transaction.
62650**
62651** This feature is included to help prevent writer-starvation.
62652*/
62653struct BtShared {
62654 Pager *pPager; /* The page cache */
62655 sqlite3 *db; /* Database connection currently using this Btree */
62656 BtCursor *pCursor; /* A list of all open cursors */
62657 MemPage *pPage1; /* First page of the database */
62658 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
62659#ifndef SQLITE_OMIT_AUTOVACUUM
62660 u8 autoVacuum; /* True if auto-vacuum is enabled */
62661 u8 incrVacuum; /* True if incr-vacuum is enabled */
62662 u8 bDoTruncate; /* True to truncate db on commit */
62663#endif
62664 u8 inTransaction; /* Transaction state */
62665 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
62666#ifdef SQLITE_HAS_CODEC
62667 u8 optimalReserve; /* Desired amount of reserved space per page */
62668#endif
62669 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
62670 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
62671 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
62672 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
62673 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
62674 u32 pageSize; /* Total number of bytes on a page */
62675 u32 usableSize; /* Number of usable bytes on each page */
62676 int nTransaction; /* Number of open transactions (read + write) */
62677 u32 nPage; /* Number of pages in the database */
62678 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
62679 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
62680 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
62681 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
62682#ifndef SQLITE_OMIT_SHARED_CACHE
62683 int nRef; /* Number of references to this structure */
62684 BtShared *pNext; /* Next on a list of sharable BtShared structs */
62685 BtLock *pLock; /* List of locks held on this shared-btree struct */
62686 Btree *pWriter; /* Btree with currently open write transaction */
62687#endif
62688 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */
62689};
62690
62691/*
62692** Allowed values for BtShared.btsFlags
62693*/
62694#define BTS_READ_ONLY0x0001 0x0001 /* Underlying file is readonly */
62695#define BTS_PAGESIZE_FIXED0x0002 0x0002 /* Page size can no longer be changed */
62696#define BTS_SECURE_DELETE0x0004 0x0004 /* PRAGMA secure_delete is enabled */
62697#define BTS_OVERWRITE0x0008 0x0008 /* Overwrite deleted content with zeros */
62698#define BTS_FAST_SECURE0x000c 0x000c /* Combination of the previous two */
62699#define BTS_INITIALLY_EMPTY0x0010 0x0010 /* Database was empty at trans start */
62700#define BTS_NO_WAL0x0020 0x0020 /* Do not open write-ahead-log files */
62701#define BTS_EXCLUSIVE0x0040 0x0040 /* pWriter has an exclusive lock */
62702#define BTS_PENDING0x0080 0x0080 /* Waiting for read-locks to clear */
62703
62704/*
62705** An instance of the following structure is used to hold information
62706** about a cell. The parseCellPtr() function fills in this structure
62707** based on information extract from the raw disk page.
62708*/
62709struct CellInfo {
62710 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */
62711 u8 *pPayload; /* Pointer to the start of payload */
62712 u32 nPayload; /* Bytes of payload */
62713 u16 nLocal; /* Amount of payload held locally, not on overflow */
62714 u16 nSize; /* Size of the cell content on the main b-tree page */
62715};
62716
62717/*
62718** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
62719** this will be declared corrupt. This value is calculated based on a
62720** maximum database size of 2^31 pages a minimum fanout of 2 for a
62721** root-node and 3 for all other internal nodes.
62722**
62723** If a tree that appears to be taller than this is encountered, it is
62724** assumed that the database is corrupt.
62725*/
62726#define BTCURSOR_MAX_DEPTH20 20
62727
62728/*
62729** A cursor is a pointer to a particular entry within a particular
62730** b-tree within a database file.
62731**
62732** The entry is identified by its MemPage and the index in
62733** MemPage.aCell[] of the entry.
62734**
62735** A single database file can be shared by two more database connections,
62736** but cursors cannot be shared. Each cursor is associated with a
62737** particular database connection identified BtCursor.pBtree.db.
62738**
62739** Fields in this structure are accessed under the BtShared.mutex
62740** found at self->pBt->mutex.
62741**
62742** skipNext meaning:
62743** The meaning of skipNext depends on the value of eState:
62744**
62745** eState Meaning of skipNext
62746** VALID skipNext is meaningless and is ignored
62747** INVALID skipNext is meaningless and is ignored
62748** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
62749** sqlite3BtreePrevious() is no-op if skipNext<0.
62750** REQUIRESEEK restoreCursorPosition() restores the cursor to
62751** eState=SKIPNEXT if skipNext!=0
62752** FAULT skipNext holds the cursor fault error code.
62753*/
62754struct BtCursor {
62755 u8 eState; /* One of the CURSOR_XXX constants (see below) */
62756 u8 curFlags; /* zero or more BTCF_* flags defined below */
62757 u8 curPagerFlags; /* Flags to send to sqlite3PagerGet() */
62758 u8 hints; /* As configured by CursorSetHints() */
62759 int skipNext; /* Prev() is noop if negative. Next() is noop if positive.
62760 ** Error code if eState==CURSOR_FAULT */
62761 Btree *pBtree; /* The Btree to which this cursor belongs */
62762 Pgno *aOverflow; /* Cache of overflow page locations */
62763 void *pKey; /* Saved key that was cursor last known position */
62764 /* All fields above are zeroed when the cursor is allocated. See
62765 ** sqlite3BtreeCursorZero(). Fields that follow must be manually
62766 ** initialized. */
62767#define BTCURSOR_FIRST_UNINITpBt pBt /* Name of first uninitialized field */
62768 BtShared *pBt; /* The BtShared this cursor points to */
62769 BtCursor *pNext; /* Forms a linked list of all cursors */
62770 CellInfo info; /* A parse of the cell we are pointing at */
62771 i64 nKey; /* Size of pKey, or last integer key */
62772 Pgno pgnoRoot; /* The root page of this tree */
62773 i8 iPage; /* Index of current page in apPage */
62774 u8 curIntKey; /* Value of apPage[0]->intKey */
62775 u16 ix; /* Current index for apPage[iPage] */
62776 u16 aiIdx[BTCURSOR_MAX_DEPTH20-1]; /* Current index in apPage[i] */
62777 struct KeyInfo *pKeyInfo; /* Arg passed to comparison function */
62778 MemPage *pPage; /* Current page */
62779 MemPage *apPage[BTCURSOR_MAX_DEPTH20-1]; /* Stack of parents of current page */
62780};
62781
62782/*
62783** Legal values for BtCursor.curFlags
62784*/
62785#define BTCF_WriteFlag0x01 0x01 /* True if a write cursor */
62786#define BTCF_ValidNKey0x02 0x02 /* True if info.nKey is valid */
62787#define BTCF_ValidOvfl0x04 0x04 /* True if aOverflow is valid */
62788#define BTCF_AtLast0x08 0x08 /* Cursor is pointing ot the last entry */
62789#define BTCF_Incrblob0x10 0x10 /* True if an incremental I/O handle */
62790#define BTCF_Multiple0x20 0x20 /* Maybe another cursor on the same btree */
62791
62792/*
62793** Potential values for BtCursor.eState.
62794**
62795** CURSOR_INVALID:
62796** Cursor does not point to a valid entry. This can happen (for example)
62797** because the table is empty or because BtreeCursorFirst() has not been
62798** called.
62799**
62800** CURSOR_VALID:
62801** Cursor points to a valid entry. getPayload() etc. may be called.
62802**
62803** CURSOR_SKIPNEXT:
62804** Cursor is valid except that the Cursor.skipNext field is non-zero
62805** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
62806** operation should be a no-op.
62807**
62808** CURSOR_REQUIRESEEK:
62809** The table that this cursor was opened on still exists, but has been
62810** modified since the cursor was last used. The cursor position is saved
62811** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
62812** this state, restoreCursorPosition() can be called to attempt to
62813** seek the cursor to the saved position.
62814**
62815** CURSOR_FAULT:
62816** An unrecoverable error (an I/O error or a malloc failure) has occurred
62817** on a different connection that shares the BtShared cache with this
62818** cursor. The error has left the cache in an inconsistent state.
62819** Do nothing else with this cursor. Any attempt to use the cursor
62820** should return the error code stored in BtCursor.skipNext
62821*/
62822#define CURSOR_VALID0 0
62823#define CURSOR_INVALID1 1
62824#define CURSOR_SKIPNEXT2 2
62825#define CURSOR_REQUIRESEEK3 3
62826#define CURSOR_FAULT4 4
62827
62828/*
62829** The database page the PENDING_BYTE occupies. This page is never used.
62830*/
62831# define PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) PAGER_MJ_PGNO(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
62832
62833/*
62834** These macros define the location of the pointer-map entry for a
62835** database page. The first argument to each is the number of usable
62836** bytes on each page of the database (often 1024). The second is the
62837** page number to look up in the pointer map.
62838**
62839** PTRMAP_PAGENO returns the database page number of the pointer-map
62840** page that stores the required pointer. PTRMAP_PTROFFSET returns
62841** the offset of the requested map entry.
62842**
62843** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
62844** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
62845** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
62846** this test.
62847*/
62848#define PTRMAP_PAGENO(pBt, pgno)ptrmapPageno(pBt, pgno) ptrmapPageno(pBt, pgno)
62849#define PTRMAP_PTROFFSET(pgptrmap, pgno)(5*(pgno-pgptrmap-1)) (5*(pgno-pgptrmap-1))
62850#define PTRMAP_ISPAGE(pBt, pgno)(ptrmapPageno((pBt), (pgno))==(pgno)) (PTRMAP_PAGENO((pBt),(pgno))ptrmapPageno((pBt), (pgno))==(pgno))
62851
62852/*
62853** The pointer map is a lookup table that identifies the parent page for
62854** each child page in the database file. The parent page is the page that
62855** contains a pointer to the child. Every page in the database contains
62856** 0 or 1 parent pages. (In this context 'database page' refers
62857** to any page that is not part of the pointer map itself.) Each pointer map
62858** entry consists of a single byte 'type' and a 4 byte parent page number.
62859** The PTRMAP_XXX identifiers below are the valid types.
62860**
62861** The purpose of the pointer map is to facility moving pages from one
62862** position in the file to another as part of autovacuum. When a page
62863** is moved, the pointer in its parent must be updated to point to the
62864** new location. The pointer map is used to locate the parent page quickly.
62865**
62866** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
62867** used in this case.
62868**
62869** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
62870** is not used in this case.
62871**
62872** PTRMAP_OVERFLOW1: The database page is the first page in a list of
62873** overflow pages. The page number identifies the page that
62874** contains the cell with a pointer to this overflow page.
62875**
62876** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
62877** overflow pages. The page-number identifies the previous
62878** page in the overflow page list.
62879**
62880** PTRMAP_BTREE: The database page is a non-root btree page. The page number
62881** identifies the parent page in the btree.
62882*/
62883#define PTRMAP_ROOTPAGE1 1
62884#define PTRMAP_FREEPAGE2 2
62885#define PTRMAP_OVERFLOW13 3
62886#define PTRMAP_OVERFLOW24 4
62887#define PTRMAP_BTREE5 5
62888
62889/* A bunch of assert() statements to check the transaction state variables
62890** of handle p (type Btree*) are internally consistent.
62891*/
62892#define btreeIntegrity(p)((void) (0)); ((void) (0)); \
62893 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 )((void) (0)); \
62894 assert( p->pBt->inTransaction>=p->inTrans )((void) (0));
62895
62896
62897/*
62898** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
62899** if the database supports auto-vacuum or not. Because it is used
62900** within an expression that is an argument to another macro
62901** (sqliteMallocRaw), it is not possible to use conditional compilation.
62902** So, this macro is defined instead.
62903*/
62904#ifndef SQLITE_OMIT_AUTOVACUUM
62905#define ISAUTOVACUUM(pBt->autoVacuum) (pBt->autoVacuum)
62906#else
62907#define ISAUTOVACUUM(pBt->autoVacuum) 0
62908#endif
62909
62910
62911/*
62912** This structure is passed around through all the sanity checking routines
62913** in order to keep track of some global state information.
62914**
62915** The aRef[] array is allocated so that there is 1 bit for each page in
62916** the database. As the integrity-check proceeds, for each page used in
62917** the database the corresponding bit is set. This allows integrity-check to
62918** detect pages that are used twice and orphaned pages (both of which
62919** indicate corruption).
62920*/
62921typedef struct IntegrityCk IntegrityCk;
62922struct IntegrityCk {
62923 BtShared *pBt; /* The tree being checked out */
62924 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
62925 u8 *aPgRef; /* 1 bit per page in the db (see above) */
62926 Pgno nPage; /* Number of pages in the database */
62927 int mxErr; /* Stop accumulating errors when this reaches zero */
62928 int nErr; /* Number of messages written to zErrMsg so far */
62929 int mallocFailed; /* A memory allocation error has occurred */
62930 const char *zPfx; /* Error message prefix */
62931 int v1, v2; /* Values for up to two %d fields in zPfx */
62932 StrAccum errMsg; /* Accumulate the error message text here */
62933 u32 *heap; /* Min-heap used for analyzing cell coverage */
62934};
62935
62936/*
62937** Routines to read or write a two- and four-byte big-endian integer values.
62938*/
62939#define get2byte(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
62940#define put2byte(p,v)((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
62941#define get4bytesqlite3Get4byte sqlite3Get4byte
62942#define put4bytesqlite3Put4byte sqlite3Put4byte
62943
62944/*
62945** get2byteAligned(), unlike get2byte(), requires that its argument point to a
62946** two-byte aligned address. get2bytea() is only used for accessing the
62947** cell addresses in a btree header.
62948*/
62949#if SQLITE_BYTEORDER1234==4321
62950# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) (*(u16*)(x))
62951#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4008000
62952# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) __builtin_bswap16(*(u16*)(x))
62953#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
62954# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) _byteswap_ushort(*(u16*)(x))
62955#else
62956# define get2byteAligned(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
62957#endif
62958
62959/************** End of btreeInt.h ********************************************/
62960/************** Continuing where we left off in btmutex.c ********************/
62961#ifndef SQLITE_OMIT_SHARED_CACHE
62962#if SQLITE_THREADSAFE1
62963
62964/*
62965** Obtain the BtShared mutex associated with B-Tree handle p. Also,
62966** set BtShared.db to the database handle associated with p and the
62967** p->locked boolean to true.
62968*/
62969static void lockBtreeMutex(Btree *p){
62970 assert( p->locked==0 )((void) (0));
62971 assert( sqlite3_mutex_notheld(p->pBt->mutex) )((void) (0));
62972 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
62973
62974 sqlite3_mutex_enter(p->pBt->mutex);
62975 p->pBt->db = p->db;
62976 p->locked = 1;
62977}
62978
62979/*
62980** Release the BtShared mutex associated with B-Tree handle p and
62981** clear the p->locked boolean.
62982*/
62983static void SQLITE_NOINLINE__attribute__((noinline)) unlockBtreeMutex(Btree *p){
62984 BtShared *pBt = p->pBt;
62985 assert( p->locked==1 )((void) (0));
62986 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
62987 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
62988 assert( p->db==pBt->db )((void) (0));
62989
62990 sqlite3_mutex_leave(pBt->mutex);
62991 p->locked = 0;
62992}
62993
62994/* Forward reference */
62995static void SQLITE_NOINLINE__attribute__((noinline)) btreeLockCarefully(Btree *p);
62996
62997/*
62998** Enter a mutex on the given BTree object.
62999**
63000** If the object is not sharable, then no mutex is ever required
63001** and this routine is a no-op. The underlying mutex is non-recursive.
63002** But we keep a reference count in Btree.wantToLock so the behavior
63003** of this interface is recursive.
63004**
63005** To avoid deadlocks, multiple Btrees are locked in the same order
63006** by all database connections. The p->pNext is a list of other
63007** Btrees belonging to the same database connection as the p Btree
63008** which need to be locked after p. If we cannot get a lock on
63009** p, then first unlock all of the others on p->pNext, then wait
63010** for the lock to become available on p, then relock all of the
63011** subsequent Btrees that desire a lock.
63012*/
63013SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree *p){
63014 /* Some basic sanity checking on the Btree. The list of Btrees
63015 ** connected by pNext and pPrev should be in sorted order by
63016 ** Btree.pBt value. All elements of the list should belong to
63017 ** the same connection. Only shared Btrees are on the list. */
63018 assert( p->pNext==0 || p->pNext->pBt>p->pBt )((void) (0));
63019 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt )((void) (0));
63020 assert( p->pNext==0 || p->pNext->db==p->db )((void) (0));
63021 assert( p->pPrev==0 || p->pPrev->db==p->db )((void) (0));
63022 assert( p->sharable || (p->pNext==0 && p->pPrev==0) )((void) (0));
63023
63024 /* Check for locking consistency */
63025 assert( !p->locked || p->wantToLock>0 )((void) (0));
63026 assert( p->sharable || p->wantToLock==0 )((void) (0));
63027
63028 /* We should already hold a lock on the database connection */
63029 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
63030
63031 /* Unless the database is sharable and unlocked, then BtShared.db
63032 ** should already be set correctly. */
63033 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db )((void) (0));
63034
63035 if( !p->sharable ) return;
63036 p->wantToLock++;
63037 if( p->locked ) return;
63038 btreeLockCarefully(p);
63039}
63040
63041/* This is a helper function for sqlite3BtreeLock(). By moving
63042** complex, but seldom used logic, out of sqlite3BtreeLock() and
63043** into this routine, we avoid unnecessary stack pointer changes
63044** and thus help the sqlite3BtreeLock() routine to run much faster
63045** in the common case.
63046*/
63047static void SQLITE_NOINLINE__attribute__((noinline)) btreeLockCarefully(Btree *p){
63048 Btree *pLater;
63049
63050 /* In most cases, we should be able to acquire the lock we
63051 ** want without having to go through the ascending lock
63052 ** procedure that follows. Just be sure not to block.
63053 */
63054 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK0 ){
63055 p->pBt->db = p->db;
63056 p->locked = 1;
63057 return;
63058 }
63059
63060 /* To avoid deadlock, first release all locks with a larger
63061 ** BtShared address. Then acquire our lock. Then reacquire
63062 ** the other BtShared locks that we used to hold in ascending
63063 ** order.
63064 */
63065 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
63066 assert( pLater->sharable )((void) (0));
63067 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt )((void) (0));
63068 assert( !pLater->locked || pLater->wantToLock>0 )((void) (0));
63069 if( pLater->locked ){
63070 unlockBtreeMutex(pLater);
63071 }
63072 }
63073 lockBtreeMutex(p);
63074 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
63075 if( pLater->wantToLock ){
63076 lockBtreeMutex(pLater);
63077 }
63078 }
63079}
63080
63081
63082/*
63083** Exit the recursive mutex on a Btree.
63084*/
63085SQLITE_PRIVATEstatic void sqlite3BtreeLeave(Btree *p){
63086 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
63087 if( p->sharable ){
63088 assert( p->wantToLock>0 )((void) (0));
63089 p->wantToLock--;
63090 if( p->wantToLock==0 ){
63091 unlockBtreeMutex(p);
63092 }
63093 }
63094}
63095
63096#ifndef NDEBUG1
63097/*
63098** Return true if the BtShared mutex is held on the btree, or if the
63099** B-Tree is not marked as sharable.
63100**
63101** This routine is used only from within assert() statements.
63102*/
63103SQLITE_PRIVATEstatic int sqlite3BtreeHoldsMutex(Btree *p){
63104 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 )((void) (0));
63105 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db )((void) (0));
63106 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) )((void) (0));
63107 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
63108
63109 return (p->sharable==0 || p->locked);
63110}
63111#endif
63112
63113
63114/*
63115** Enter the mutex on every Btree associated with a database
63116** connection. This is needed (for example) prior to parsing
63117** a statement since we will be comparing table and column names
63118** against all schemas and we do not want those schemas being
63119** reset out from under us.
63120**
63121** There is a corresponding leave-all procedures.
63122**
63123** Enter the mutexes in accending order by BtShared pointer address
63124** to avoid the possibility of deadlock when two threads with
63125** two or more btrees in common both try to lock all their btrees
63126** at the same instant.
63127*/
63128static void SQLITE_NOINLINE__attribute__((noinline)) btreeEnterAll(sqlite3 *db){
63129 int i;
63130 int skipOk = 1;
63131 Btree *p;
63132 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
63133 for(i=0; i<db->nDb; i++){
63134 p = db->aDb[i].pBt;
63135 if( p && p->sharable ){
63136 sqlite3BtreeEnter(p);
63137 skipOk = 0;
63138 }
63139 }
63140 db->noSharedCache = skipOk;
63141}
63142SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3 *db){
63143 if( db->noSharedCache==0 ) btreeEnterAll(db);
63144}
63145static void SQLITE_NOINLINE__attribute__((noinline)) btreeLeaveAll(sqlite3 *db){
63146 int i;
63147 Btree *p;
63148 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
63149 for(i=0; i<db->nDb; i++){
63150 p = db->aDb[i].pBt;
63151 if( p ) sqlite3BtreeLeave(p);
63152 }
63153}
63154SQLITE_PRIVATEstatic void sqlite3BtreeLeaveAll(sqlite3 *db){
63155 if( db->noSharedCache==0 ) btreeLeaveAll(db);
63156}
63157
63158#ifndef NDEBUG1
63159/*
63160** Return true if the current thread holds the database connection
63161** mutex and all required BtShared mutexes.
63162**
63163** This routine is used inside assert() statements only.
63164*/
63165SQLITE_PRIVATEstatic int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
63166 int i;
63167 if( !sqlite3_mutex_held(db->mutex) ){
63168 return 0;
63169 }
63170 for(i=0; i<db->nDb; i++){
63171 Btree *p;
63172 p = db->aDb[i].pBt;
63173 if( p && p->sharable &&
63174 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
63175 return 0;
63176 }
63177 }
63178 return 1;
63179}
63180#endif /* NDEBUG */
63181
63182#ifndef NDEBUG1
63183/*
63184** Return true if the correct mutexes are held for accessing the
63185** db->aDb[iDb].pSchema structure. The mutexes required for schema
63186** access are:
63187**
63188** (1) The mutex on db
63189** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
63190**
63191** If pSchema is not NULL, then iDb is computed from pSchema and
63192** db using sqlite3SchemaToIndex().
63193*/
63194SQLITE_PRIVATEstatic int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
63195 Btree *p;
63196 assert( db!=0 )((void) (0));
63197 if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
63198 assert( iDb>=0 && iDb<db->nDb )((void) (0));
63199 if( !sqlite3_mutex_held(db->mutex) ) return 0;
63200 if( iDb==1 ) return 1;
63201 p = db->aDb[iDb].pBt;
63202 assert( p!=0 )((void) (0));
63203 return p->sharable==0 || p->locked==1;
63204}
63205#endif /* NDEBUG */
63206
63207#else /* SQLITE_THREADSAFE>0 above. SQLITE_THREADSAFE==0 below */
63208/*
63209** The following are special cases for mutex enter routines for use
63210** in single threaded applications that use shared cache. Except for
63211** these two routines, all mutex operations are no-ops in that case and
63212** are null #defines in btree.h.
63213**
63214** If shared cache is disabled, then all btree mutex routines, including
63215** the ones below, are no-ops and are null #defines in btree.h.
63216*/
63217
63218SQLITE_PRIVATEstatic void sqlite3BtreeEnter(Btree *p){
63219 p->pBt->db = p->db;
63220}
63221SQLITE_PRIVATEstatic void sqlite3BtreeEnterAll(sqlite3 *db){
63222 int i;
63223 for(i=0; i<db->nDb; i++){
63224 Btree *p = db->aDb[i].pBt;
63225 if( p ){
63226 p->pBt->db = p->db;
63227 }
63228 }
63229}
63230#endif /* if SQLITE_THREADSAFE */
63231
63232#ifndef SQLITE_OMIT_INCRBLOB
63233/*
63234** Enter a mutex on a Btree given a cursor owned by that Btree.
63235**
63236** These entry points are used by incremental I/O only. Enter() is required
63237** any time OMIT_SHARED_CACHE is not defined, regardless of whether or not
63238** the build is threadsafe. Leave() is only required by threadsafe builds.
63239*/
63240SQLITE_PRIVATEstatic void sqlite3BtreeEnterCursor(BtCursor *pCur){
63241 sqlite3BtreeEnter(pCur->pBtree);
63242}
63243# if SQLITE_THREADSAFE1
63244SQLITE_PRIVATEstatic void sqlite3BtreeLeaveCursor(BtCursor *pCur){
63245 sqlite3BtreeLeave(pCur->pBtree);
63246}
63247# endif
63248#endif /* ifndef SQLITE_OMIT_INCRBLOB */
63249
63250#endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
63251
63252/************** End of btmutex.c *********************************************/
63253/************** Begin file btree.c *******************************************/
63254/*
63255** 2004 April 6
63256**
63257** The author disclaims copyright to this source code. In place of
63258** a legal notice, here is a blessing:
63259**
63260** May you do good and not evil.
63261** May you find forgiveness for yourself and forgive others.
63262** May you share freely, never taking more than you give.
63263**
63264*************************************************************************
63265** This file implements an external (disk-based) database using BTrees.
63266** See the header comment on "btreeInt.h" for additional information.
63267** Including a description of file format and an overview of operation.
63268*/
63269/* #include "btreeInt.h" */
63270
63271/*
63272** The header string that appears at the beginning of every
63273** SQLite database.
63274*/
63275static const char zMagicHeader[] = SQLITE_FILE_HEADER"SQLite format 3";
63276
63277/*
63278** Set this global variable to 1 to enable tracing using the TRACE
63279** macro.
63280*/
63281#if 0
63282int sqlite3BtreeTrace=1; /* True to enable tracing */
63283# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdoutstdout);}
63284#else
63285# define TRACE(X)
63286#endif
63287
63288/*
63289** Extract a 2-byte big-endian integer from an array of unsigned bytes.
63290** But if the value is zero, make it 65536.
63291**
63292** This routine is used to extract the "offset to cell content area" value
63293** from the header of a btree page. If the page size is 65536 and the page
63294** is empty, the offset should be 65536, but the 2-byte value stores zero.
63295** This routine makes the necessary adjustment to 65536.
63296*/
63297#define get2byteNotZero(X)(((((int)((X)[0]<<8 | (X)[1]))-1)&0xffff)+1) (((((int)get2byte(X)((X)[0]<<8 | (X)[1]))-1)&0xffff)+1)
63298
63299/*
63300** Values passed as the 5th argument to allocateBtreePage()
63301*/
63302#define BTALLOC_ANY0 0 /* Allocate any page */
63303#define BTALLOC_EXACT1 1 /* Allocate exact page if possible */
63304#define BTALLOC_LE2 2 /* Allocate any page <= the parameter */
63305
63306/*
63307** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
63308** defined, or 0 if it is. For example:
63309**
63310** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
63311*/
63312#ifndef SQLITE_OMIT_AUTOVACUUM
63313#define IfNotOmitAV(expr)(expr) (expr)
63314#else
63315#define IfNotOmitAV(expr)(expr) 0
63316#endif
63317
63318#ifndef SQLITE_OMIT_SHARED_CACHE
63319/*
63320** A list of BtShared objects that are eligible for participation
63321** in shared cache. This variable has file scope during normal builds,
63322** but the test harness needs to access it so we make it global for
63323** test builds.
63324**
63325** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
63326*/
63327#ifdef SQLITE_TEST
63328SQLITE_PRIVATEstatic BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
63329#else
63330static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
63331#endif
63332#endif /* SQLITE_OMIT_SHARED_CACHE */
63333
63334#ifndef SQLITE_OMIT_SHARED_CACHE
63335/*
63336** Enable or disable the shared pager and schema features.
63337**
63338** This routine has no effect on existing database connections.
63339** The shared cache setting effects only future calls to
63340** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
63341*/
63342SQLITE_API int sqlite3_enable_shared_cache(int enable){
63343 sqlite3GlobalConfigsqlite3Config.sharedCacheEnabled = enable;
63344 return SQLITE_OK0;
63345}
63346#endif
63347
63348
63349
63350#ifdef SQLITE_OMIT_SHARED_CACHE
63351 /*
63352 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
63353 ** and clearAllSharedCacheTableLocks()
63354 ** manipulate entries in the BtShared.pLock linked list used to store
63355 ** shared-cache table level locks. If the library is compiled with the
63356 ** shared-cache feature disabled, then there is only ever one user
63357 ** of each BtShared structure and so this locking is not necessary.
63358 ** So define the lock related functions as no-ops.
63359 */
63360 #define querySharedCacheTableLock(a,b,c) SQLITE_OK0
63361 #define setSharedCacheTableLock(a,b,c) SQLITE_OK0
63362 #define clearAllSharedCacheTableLocks(a)
63363 #define downgradeAllSharedCacheTableLocks(a)
63364 #define hasSharedCacheTableLock(a,b,c,d) 1
63365 #define hasReadConflicts(a, b) 0
63366#endif
63367
63368/*
63369** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
63370** (MemPage*) as an argument. The (MemPage*) must not be NULL.
63371**
63372** If SQLITE_DEBUG is not defined, then this macro is equivalent to
63373** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
63374** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
63375** with the page number and filename associated with the (MemPage*).
63376*/
63377#ifdef SQLITE_DEBUG
63378int corruptPageError(int lineno, MemPage *p){
63379 char *zMsg;
63380 sqlite3BeginBenignMalloc();
63381 zMsg = sqlite3_mprintf("database corruption page %d of %s",
63382 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
63383 );
63384 sqlite3EndBenignMalloc();
63385 if( zMsg ){
63386 sqlite3ReportError(SQLITE_CORRUPT11, lineno, zMsg);
63387 }
63388 sqlite3_free(zMsg);
63389 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(63389);
63390}
63391# define SQLITE_CORRUPT_PAGE(pMemPage)sqlite3CorruptError(63391) corruptPageError(__LINE__63391, pMemPage)
63392#else
63393# define SQLITE_CORRUPT_PAGE(pMemPage)sqlite3CorruptError(63393) SQLITE_CORRUPT_PGNO(pMemPage->pgno)sqlite3CorruptError(63393)
63394#endif
63395
63396#ifndef SQLITE_OMIT_SHARED_CACHE
63397
63398#ifdef SQLITE_DEBUG
63399/*
63400**** This function is only used as part of an assert() statement. ***
63401**
63402** Check to see if pBtree holds the required locks to read or write to the
63403** table with root page iRoot. Return 1 if it does and 0 if not.
63404**
63405** For example, when writing to a table with root-page iRoot via
63406** Btree connection pBtree:
63407**
63408** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
63409**
63410** When writing to an index that resides in a sharable database, the
63411** caller should have first obtained a lock specifying the root page of
63412** the corresponding table. This makes things a bit more complicated,
63413** as this module treats each table as a separate structure. To determine
63414** the table corresponding to the index being written, this
63415** function has to search through the database schema.
63416**
63417** Instead of a lock on the table/index rooted at page iRoot, the caller may
63418** hold a write-lock on the schema table (root page 1). This is also
63419** acceptable.
63420*/
63421static int hasSharedCacheTableLock(
63422 Btree *pBtree, /* Handle that must hold lock */
63423 Pgno iRoot, /* Root page of b-tree */
63424 int isIndex, /* True if iRoot is the root of an index b-tree */
63425 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
63426){
63427 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
63428 Pgno iTab = 0;
63429 BtLock *pLock;
63430
63431 /* If this database is not shareable, or if the client is reading
63432 ** and has the read-uncommitted flag set, then no lock is required.
63433 ** Return true immediately.
63434 */
63435 if( (pBtree->sharable==0)
63436 || (eLockType==READ_LOCK1 && (pBtree->db->flags & SQLITE_ReadUncommit0x00000400))
63437 ){
63438 return 1;
63439 }
63440
63441 /* If the client is reading or writing an index and the schema is
63442 ** not loaded, then it is too difficult to actually check to see if
63443 ** the correct locks are held. So do not bother - just return true.
63444 ** This case does not come up very often anyhow.
63445 */
63446 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded0x0001)==0) ){
63447 return 1;
63448 }
63449
63450 /* Figure out the root-page that the lock should be held on. For table
63451 ** b-trees, this is just the root page of the b-tree being read or
63452 ** written. For index b-trees, it is the root page of the associated
63453 ** table. */
63454 if( isIndex ){
63455 HashElem *p;
63456 for(p=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); p; p=sqliteHashNext(p)((p)->next)){
63457 Index *pIdx = (Index *)sqliteHashData(p)((p)->data);
63458 if( pIdx->tnum==(int)iRoot ){
63459 if( iTab ){
63460 /* Two or more indexes share the same root page. There must
63461 ** be imposter tables. So just return true. The assert is not
63462 ** useful in that case. */
63463 return 1;
63464 }
63465 iTab = pIdx->pTable->tnum;
63466 }
63467 }
63468 }else{
63469 iTab = iRoot;
63470 }
63471
63472 /* Search for the required lock. Either a write-lock on root-page iTab, a
63473 ** write-lock on the schema table, or (if the client is reading) a
63474 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
63475 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
63476 if( pLock->pBtree==pBtree
63477 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK2 && pLock->iTable==1))
63478 && pLock->eLock>=eLockType
63479 ){
63480 return 1;
63481 }
63482 }
63483
63484 /* Failed to find the required lock. */
63485 return 0;
63486}
63487#endif /* SQLITE_DEBUG */
63488
63489#ifdef SQLITE_DEBUG
63490/*
63491**** This function may be used as part of assert() statements only. ****
63492**
63493** Return true if it would be illegal for pBtree to write into the
63494** table or index rooted at iRoot because other shared connections are
63495** simultaneously reading that same table or index.
63496**
63497** It is illegal for pBtree to write if some other Btree object that
63498** shares the same BtShared object is currently reading or writing
63499** the iRoot table. Except, if the other Btree object has the
63500** read-uncommitted flag set, then it is OK for the other object to
63501** have a read cursor.
63502**
63503** For example, before writing to any part of the table or index
63504** rooted at page iRoot, one should call:
63505**
63506** assert( !hasReadConflicts(pBtree, iRoot) );
63507*/
63508static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
63509 BtCursor *p;
63510 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
63511 if( p->pgnoRoot==iRoot
63512 && p->pBtree!=pBtree
63513 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit0x00000400)
63514 ){
63515 return 1;
63516 }
63517 }
63518 return 0;
63519}
63520#endif /* #ifdef SQLITE_DEBUG */
63521
63522/*
63523** Query to see if Btree handle p may obtain a lock of type eLock
63524** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
63525** SQLITE_OK if the lock may be obtained (by calling
63526** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
63527*/
63528static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
63529 BtShared *pBt = p->pBt;
63530 BtLock *pIter;
63531
63532 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
63533 assert( eLock==READ_LOCK || eLock==WRITE_LOCK )((void) (0));
63534 assert( p->db!=0 )((void) (0));
63535 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 )((void) (0));
63536
63537 /* If requesting a write-lock, then the Btree must have an open write
63538 ** transaction on this file. And, obviously, for this to be so there
63539 ** must be an open write transaction on the file itself.
63540 */
63541 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) )((void) (0));
63542 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE )((void) (0));
63543
63544 /* This routine is a no-op if the shared-cache is not enabled */
63545 if( !p->sharable ){
63546 return SQLITE_OK0;
63547 }
63548
63549 /* If some other connection is holding an exclusive lock, the
63550 ** requested lock may not be obtained.
63551 */
63552 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE0x0040)!=0 ){
63553 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
63554 return SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
63555 }
63556
63557 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
63558 /* The condition (pIter->eLock!=eLock) in the following if(...)
63559 ** statement is a simplification of:
63560 **
63561 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
63562 **
63563 ** since we know that if eLock==WRITE_LOCK, then no other connection
63564 ** may hold a WRITE_LOCK on any table in this file (since there can
63565 ** only be a single writer).
63566 */
63567 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK )((void) (0));
63568 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK)((void) (0));
63569 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
63570 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
63571 if( eLock==WRITE_LOCK2 ){
63572 assert( p==pBt->pWriter )((void) (0));
63573 pBt->btsFlags |= BTS_PENDING0x0080;
63574 }
63575 return SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
63576 }
63577 }
63578 return SQLITE_OK0;
63579}
63580#endif /* !SQLITE_OMIT_SHARED_CACHE */
63581
63582#ifndef SQLITE_OMIT_SHARED_CACHE
63583/*
63584** Add a lock on the table with root-page iTable to the shared-btree used
63585** by Btree handle p. Parameter eLock must be either READ_LOCK or
63586** WRITE_LOCK.
63587**
63588** This function assumes the following:
63589**
63590** (a) The specified Btree object p is connected to a sharable
63591** database (one with the BtShared.sharable flag set), and
63592**
63593** (b) No other Btree objects hold a lock that conflicts
63594** with the requested lock (i.e. querySharedCacheTableLock() has
63595** already been called and returned SQLITE_OK).
63596**
63597** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
63598** is returned if a malloc attempt fails.
63599*/
63600static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
63601 BtShared *pBt = p->pBt;
63602 BtLock *pLock = 0;
63603 BtLock *pIter;
63604
63605 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
63606 assert( eLock==READ_LOCK || eLock==WRITE_LOCK )((void) (0));
63607 assert( p->db!=0 )((void) (0));
63608
63609 /* A connection with the read-uncommitted flag set will never try to
63610 ** obtain a read-lock using this function. The only read-lock obtained
63611 ** by a connection in read-uncommitted mode is on the sqlite_master
63612 ** table, and that lock is obtained in BtreeBeginTrans(). */
63613 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK )((void) (0));
63614
63615 /* This function should only be called on a sharable b-tree after it
63616 ** has been determined that no other b-tree holds a conflicting lock. */
63617 assert( p->sharable )((void) (0));
63618 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) )((void) (0));
63619
63620 /* First search the list for an existing lock on this table. */
63621 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
63622 if( pIter->iTable==iTable && pIter->pBtree==p ){
63623 pLock = pIter;
63624 break;
63625 }
63626 }
63627
63628 /* If the above search did not find a BtLock struct associating Btree p
63629 ** with table iTable, allocate one and link it into the list.
63630 */
63631 if( !pLock ){
63632 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
63633 if( !pLock ){
63634 return SQLITE_NOMEM_BKPT7;
63635 }
63636 pLock->iTable = iTable;
63637 pLock->pBtree = p;
63638 pLock->pNext = pBt->pLock;
63639 pBt->pLock = pLock;
63640 }
63641
63642 /* Set the BtLock.eLock variable to the maximum of the current lock
63643 ** and the requested lock. This means if a write-lock was already held
63644 ** and a read-lock requested, we don't incorrectly downgrade the lock.
63645 */
63646 assert( WRITE_LOCK>READ_LOCK )((void) (0));
63647 if( eLock>pLock->eLock ){
63648 pLock->eLock = eLock;
63649 }
63650
63651 return SQLITE_OK0;
63652}
63653#endif /* !SQLITE_OMIT_SHARED_CACHE */
63654
63655#ifndef SQLITE_OMIT_SHARED_CACHE
63656/*
63657** Release all the table locks (locks obtained via calls to
63658** the setSharedCacheTableLock() procedure) held by Btree object p.
63659**
63660** This function assumes that Btree p has an open read or write
63661** transaction. If it does not, then the BTS_PENDING flag
63662** may be incorrectly cleared.
63663*/
63664static void clearAllSharedCacheTableLocks(Btree *p){
63665 BtShared *pBt = p->pBt;
63666 BtLock **ppIter = &pBt->pLock;
63667
63668 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
63669 assert( p->sharable || 0==*ppIter )((void) (0));
63670 assert( p->inTrans>0 )((void) (0));
63671
63672 while( *ppIter ){
63673 BtLock *pLock = *ppIter;
63674 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree )((void) (0));
63675 assert( pLock->pBtree->inTrans>=pLock->eLock )((void) (0));
63676 if( pLock->pBtree==p ){
63677 *ppIter = pLock->pNext;
63678 assert( pLock->iTable!=1 || pLock==&p->lock )((void) (0));
63679 if( pLock->iTable!=1 ){
63680 sqlite3_free(pLock);
63681 }
63682 }else{
63683 ppIter = &pLock->pNext;
63684 }
63685 }
63686
63687 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter )((void) (0));
63688 if( pBt->pWriter==p ){
63689 pBt->pWriter = 0;
63690 pBt->btsFlags &= ~(BTS_EXCLUSIVE0x0040|BTS_PENDING0x0080);
63691 }else if( pBt->nTransaction==2 ){
63692 /* This function is called when Btree p is concluding its
63693 ** transaction. If there currently exists a writer, and p is not
63694 ** that writer, then the number of locks held by connections other
63695 ** than the writer must be about to drop to zero. In this case
63696 ** set the BTS_PENDING flag to 0.
63697 **
63698 ** If there is not currently a writer, then BTS_PENDING must
63699 ** be zero already. So this next line is harmless in that case.
63700 */
63701 pBt->btsFlags &= ~BTS_PENDING0x0080;
63702 }
63703}
63704
63705/*
63706** This function changes all write-locks held by Btree p into read-locks.
63707*/
63708static void downgradeAllSharedCacheTableLocks(Btree *p){
63709 BtShared *pBt = p->pBt;
63710 if( pBt->pWriter==p ){
63711 BtLock *pLock;
63712 pBt->pWriter = 0;
63713 pBt->btsFlags &= ~(BTS_EXCLUSIVE0x0040|BTS_PENDING0x0080);
63714 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
63715 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p )((void) (0));
63716 pLock->eLock = READ_LOCK1;
63717 }
63718 }
63719}
63720
63721#endif /* SQLITE_OMIT_SHARED_CACHE */
63722
63723static void releasePage(MemPage *pPage); /* Forward reference */
63724static void releasePageOne(MemPage *pPage); /* Forward reference */
63725static void releasePageNotNull(MemPage *pPage); /* Forward reference */
63726
63727/*
63728***** This routine is used inside of assert() only ****
63729**
63730** Verify that the cursor holds the mutex on its BtShared
63731*/
63732#ifdef SQLITE_DEBUG
63733static int cursorHoldsMutex(BtCursor *p){
63734 return sqlite3_mutex_held(p->pBt->mutex);
63735}
63736
63737/* Verify that the cursor and the BtShared agree about what is the current
63738** database connetion. This is important in shared-cache mode. If the database
63739** connection pointers get out-of-sync, it is possible for routines like
63740** btreeInitPage() to reference an stale connection pointer that references a
63741** a connection that has already closed. This routine is used inside assert()
63742** statements only and for the purpose of double-checking that the btree code
63743** does keep the database connection pointers up-to-date.
63744*/
63745static int cursorOwnsBtShared(BtCursor *p){
63746 assert( cursorHoldsMutex(p) )((void) (0));
63747 return (p->pBtree->db==p->pBt->db);
63748}
63749#endif
63750
63751/*
63752** Invalidate the overflow cache of the cursor passed as the first argument.
63753** on the shared btree structure pBt.
63754*/
63755#define invalidateOverflowCache(pCur)(pCur->curFlags &= ~0x04) (pCur->curFlags &= ~BTCF_ValidOvfl0x04)
63756
63757/*
63758** Invalidate the overflow page-list cache for all cursors opened
63759** on the shared btree structure pBt.
63760*/
63761static void invalidateAllOverflowCache(BtShared *pBt){
63762 BtCursor *p;
63763 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
63764 for(p=pBt->pCursor; p; p=p->pNext){
63765 invalidateOverflowCache(p)(p->curFlags &= ~0x04);
63766 }
63767}
63768
63769#ifndef SQLITE_OMIT_INCRBLOB
63770/*
63771** This function is called before modifying the contents of a table
63772** to invalidate any incrblob cursors that are open on the
63773** row or one of the rows being modified.
63774**
63775** If argument isClearTable is true, then the entire contents of the
63776** table is about to be deleted. In this case invalidate all incrblob
63777** cursors open on any row within the table with root-page pgnoRoot.
63778**
63779** Otherwise, if argument isClearTable is false, then the row with
63780** rowid iRow is being replaced or deleted. In this case invalidate
63781** only those incrblob cursors open on that specific row.
63782*/
63783static void invalidateIncrblobCursors(
63784 Btree *pBtree, /* The database file to check */
63785 Pgno pgnoRoot, /* The table that might be changing */
63786 i64 iRow, /* The rowid that might be changing */
63787 int isClearTable /* True if all rows are being deleted */
63788){
63789 BtCursor *p;
63790 if( pBtree->hasIncrblobCur==0 ) return;
63791 assert( sqlite3BtreeHoldsMutex(pBtree) )((void) (0));
63792 pBtree->hasIncrblobCur = 0;
63793 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
63794 if( (p->curFlags & BTCF_Incrblob0x10)!=0 ){
63795 pBtree->hasIncrblobCur = 1;
63796 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
63797 p->eState = CURSOR_INVALID1;
63798 }
63799 }
63800 }
63801}
63802
63803#else
63804 /* Stub function when INCRBLOB is omitted */
63805 #define invalidateIncrblobCursors(w,x,y,z)
63806#endif /* SQLITE_OMIT_INCRBLOB */
63807
63808/*
63809** Set bit pgno of the BtShared.pHasContent bitvec. This is called
63810** when a page that previously contained data becomes a free-list leaf
63811** page.
63812**
63813** The BtShared.pHasContent bitvec exists to work around an obscure
63814** bug caused by the interaction of two useful IO optimizations surrounding
63815** free-list leaf pages:
63816**
63817** 1) When all data is deleted from a page and the page becomes
63818** a free-list leaf page, the page is not written to the database
63819** (as free-list leaf pages contain no meaningful data). Sometimes
63820** such a page is not even journalled (as it will not be modified,
63821** why bother journalling it?).
63822**
63823** 2) When a free-list leaf page is reused, its content is not read
63824** from the database or written to the journal file (why should it
63825** be, if it is not at all meaningful?).
63826**
63827** By themselves, these optimizations work fine and provide a handy
63828** performance boost to bulk delete or insert operations. However, if
63829** a page is moved to the free-list and then reused within the same
63830** transaction, a problem comes up. If the page is not journalled when
63831** it is moved to the free-list and it is also not journalled when it
63832** is extracted from the free-list and reused, then the original data
63833** may be lost. In the event of a rollback, it may not be possible
63834** to restore the database to its original configuration.
63835**
63836** The solution is the BtShared.pHasContent bitvec. Whenever a page is
63837** moved to become a free-list leaf page, the corresponding bit is
63838** set in the bitvec. Whenever a leaf page is extracted from the free-list,
63839** optimization 2 above is omitted if the corresponding bit is already
63840** set in BtShared.pHasContent. The contents of the bitvec are cleared
63841** at the end of every transaction.
63842*/
63843static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
63844 int rc = SQLITE_OK0;
63845 if( !pBt->pHasContent ){
63846 assert( pgno<=pBt->nPage )((void) (0));
63847 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
63848 if( !pBt->pHasContent ){
63849 rc = SQLITE_NOMEM_BKPT7;
63850 }
63851 }
63852 if( rc==SQLITE_OK0 && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
63853 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
63854 }
63855 return rc;
63856}
63857
63858/*
63859** Query the BtShared.pHasContent vector.
63860**
63861** This function is called when a free-list leaf page is removed from the
63862** free-list for reuse. It returns false if it is safe to retrieve the
63863** page from the pager layer with the 'no-content' flag set. True otherwise.
63864*/
63865static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
63866 Bitvec *p = pBt->pHasContent;
63867 return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
63868}
63869
63870/*
63871** Clear (destroy) the BtShared.pHasContent bitvec. This should be
63872** invoked at the conclusion of each write-transaction.
63873*/
63874static void btreeClearHasContent(BtShared *pBt){
63875 sqlite3BitvecDestroy(pBt->pHasContent);
63876 pBt->pHasContent = 0;
63877}
63878
63879/*
63880** Release all of the apPage[] pages for a cursor.
63881*/
63882static void btreeReleaseAllCursorPages(BtCursor *pCur){
63883 int i;
63884 if( pCur->iPage>=0 ){
63885 for(i=0; i<pCur->iPage; i++){
63886 releasePageNotNull(pCur->apPage[i]);
63887 }
63888 releasePageNotNull(pCur->pPage);
63889 pCur->iPage = -1;
63890 }
63891}
63892
63893/*
63894** The cursor passed as the only argument must point to a valid entry
63895** when this function is called (i.e. have eState==CURSOR_VALID). This
63896** function saves the current cursor key in variables pCur->nKey and
63897** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
63898** code otherwise.
63899**
63900** If the cursor is open on an intkey table, then the integer key
63901** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
63902** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
63903** set to point to a malloced buffer pCur->nKey bytes in size containing
63904** the key.
63905*/
63906static int saveCursorKey(BtCursor *pCur){
63907 int rc = SQLITE_OK0;
63908 assert( CURSOR_VALID==pCur->eState )((void) (0));
63909 assert( 0==pCur->pKey )((void) (0));
63910 assert( cursorHoldsMutex(pCur) )((void) (0));
63911
63912 if( pCur->curIntKey ){
63913 /* Only the rowid is required for a table btree */
63914 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
63915 }else{
63916 /* For an index btree, save the complete key content. It is possible
63917 ** that the current key is corrupt. In that case, it is possible that
63918 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
63919 ** up to the size of 1 varint plus 1 8-byte value when the cursor
63920 ** position is restored. Hence the 17 bytes of padding allocated
63921 ** below. */
63922 void *pKey;
63923 pCur->nKey = sqlite3BtreePayloadSize(pCur);
63924 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
63925 if( pKey ){
63926 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
63927 if( rc==SQLITE_OK0 ){
63928 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
63929 pCur->pKey = pKey;
63930 }else{
63931 sqlite3_free(pKey);
63932 }
63933 }else{
63934 rc = SQLITE_NOMEM_BKPT7;
63935 }
63936 }
63937 assert( !pCur->curIntKey || !pCur->pKey )((void) (0));
63938 return rc;
63939}
63940
63941/*
63942** Save the current cursor position in the variables BtCursor.nKey
63943** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
63944**
63945** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
63946** prior to calling this routine.
63947*/
63948static int saveCursorPosition(BtCursor *pCur){
63949 int rc;
63950
63951 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState )((void) (0));
63952 assert( 0==pCur->pKey )((void) (0));
63953 assert( cursorHoldsMutex(pCur) )((void) (0));
63954
63955 if( pCur->eState==CURSOR_SKIPNEXT2 ){
63956 pCur->eState = CURSOR_VALID0;
63957 }else{
63958 pCur->skipNext = 0;
63959 }
63960
63961 rc = saveCursorKey(pCur);
63962 if( rc==SQLITE_OK0 ){
63963 btreeReleaseAllCursorPages(pCur);
63964 pCur->eState = CURSOR_REQUIRESEEK3;
63965 }
63966
63967 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04|BTCF_AtLast0x08);
63968 return rc;
63969}
63970
63971/* Forward reference */
63972static int SQLITE_NOINLINE__attribute__((noinline)) saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
63973
63974/*
63975** Save the positions of all cursors (except pExcept) that are open on
63976** the table with root-page iRoot. "Saving the cursor position" means that
63977** the location in the btree is remembered in such a way that it can be
63978** moved back to the same spot after the btree has been modified. This
63979** routine is called just before cursor pExcept is used to modify the
63980** table, for example in BtreeDelete() or BtreeInsert().
63981**
63982** If there are two or more cursors on the same btree, then all such
63983** cursors should have their BTCF_Multiple flag set. The btreeCursor()
63984** routine enforces that rule. This routine only needs to be called in
63985** the uncommon case when pExpect has the BTCF_Multiple flag set.
63986**
63987** If pExpect!=NULL and if no other cursors are found on the same root-page,
63988** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
63989** pointless call to this routine.
63990**
63991** Implementation note: This routine merely checks to see if any cursors
63992** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
63993** event that cursors are in need to being saved.
63994*/
63995static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
63996 BtCursor *p;
63997 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
63998 assert( pExcept==0 || pExcept->pBt==pBt )((void) (0));
63999 for(p=pBt->pCursor; p; p=p->pNext){
64000 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
64001 }
64002 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
64003 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple0x20;
64004 return SQLITE_OK0;
64005}
64006
64007/* This helper routine to saveAllCursors does the actual work of saving
64008** the cursors if and when a cursor is found that actually requires saving.
64009** The common case is that no cursors need to be saved, so this routine is
64010** broken out from its caller to avoid unnecessary stack pointer movement.
64011*/
64012static int SQLITE_NOINLINE__attribute__((noinline)) saveCursorsOnList(
64013 BtCursor *p, /* The first cursor that needs saving */
64014 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
64015 BtCursor *pExcept /* Do not save this cursor */
64016){
64017 do{
64018 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
64019 if( p->eState==CURSOR_VALID0 || p->eState==CURSOR_SKIPNEXT2 ){
64020 int rc = saveCursorPosition(p);
64021 if( SQLITE_OK0!=rc ){
64022 return rc;
64023 }
64024 }else{
64025 testcase( p->iPage>=0 );
64026 btreeReleaseAllCursorPages(p);
64027 }
64028 }
64029 p = p->pNext;
64030 }while( p );
64031 return SQLITE_OK0;
64032}
64033
64034/*
64035** Clear the current cursor position.
64036*/
64037SQLITE_PRIVATEstatic void sqlite3BtreeClearCursor(BtCursor *pCur){
64038 assert( cursorHoldsMutex(pCur) )((void) (0));
64039 sqlite3_free(pCur->pKey);
64040 pCur->pKey = 0;
64041 pCur->eState = CURSOR_INVALID1;
64042}
64043
64044/*
64045** In this version of BtreeMoveto, pKey is a packed index record
64046** such as is generated by the OP_MakeRecord opcode. Unpack the
64047** record and then call BtreeMovetoUnpacked() to do the work.
64048*/
64049static int btreeMoveto(
64050 BtCursor *pCur, /* Cursor open on the btree to be searched */
64051 const void *pKey, /* Packed key if the btree is an index */
64052 i64 nKey, /* Integer key for tables. Size of pKey for indices */
64053 int bias, /* Bias search to the high end */
64054 int *pRes /* Write search results here */
64055){
64056 int rc; /* Status code */
64057 UnpackedRecord *pIdxKey; /* Unpacked index key */
64058
64059 if( pKey ){
64060 KeyInfo *pKeyInfo = pCur->pKeyInfo;
64061 assert( nKey==(i64)(int)nKey )((void) (0));
64062 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
64063 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT7;
64064 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
64065 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
64066 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(64066);
64067 goto moveto_done;
64068 }
64069 }else{
64070 pIdxKey = 0;
64071 }
64072 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
64073moveto_done:
64074 if( pIdxKey ){
64075 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
64076 }
64077 return rc;
64078}
64079
64080/*
64081** Restore the cursor to the position it was in (or as close to as possible)
64082** when saveCursorPosition() was called. Note that this call deletes the
64083** saved position info stored by saveCursorPosition(), so there can be
64084** at most one effective restoreCursorPosition() call after each
64085** saveCursorPosition().
64086*/
64087static int btreeRestoreCursorPosition(BtCursor *pCur){
64088 int rc;
64089 int skipNext = 0;
64090 assert( cursorOwnsBtShared(pCur) )((void) (0));
64091 assert( pCur->eState>=CURSOR_REQUIRESEEK )((void) (0));
64092 if( pCur->eState==CURSOR_FAULT4 ){
64093 return pCur->skipNext;
64094 }
64095 pCur->eState = CURSOR_INVALID1;
64096 if( sqlite3FaultSim(410) ){
64097 rc = SQLITE_IOERR10;
64098 }else{
64099 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
64100 }
64101 if( rc==SQLITE_OK0 ){
64102 sqlite3_free(pCur->pKey);
64103 pCur->pKey = 0;
64104 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID )((void) (0));
64105 if( skipNext ) pCur->skipNext = skipNext;
64106 if( pCur->skipNext && pCur->eState==CURSOR_VALID0 ){
64107 pCur->eState = CURSOR_SKIPNEXT2;
64108 }
64109 }
64110 return rc;
64111}
64112
64113#define restoreCursorPosition(p)(p->eState>=3 ? btreeRestoreCursorPosition(p) : 0) \
64114 (p->eState>=CURSOR_REQUIRESEEK3 ? \
64115 btreeRestoreCursorPosition(p) : \
64116 SQLITE_OK0)
64117
64118/*
64119** Determine whether or not a cursor has moved from the position where
64120** it was last placed, or has been invalidated for any other reason.
64121** Cursors can move when the row they are pointing at is deleted out
64122** from under them, for example. Cursor might also move if a btree
64123** is rebalanced.
64124**
64125** Calling this routine with a NULL cursor pointer returns false.
64126**
64127** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
64128** back to where it ought to be if this routine returns true.
64129*/
64130SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
64131 assert( EIGHT_BYTE_ALIGNMENT(pCur)((void) (0))
64132 || pCur==sqlite3BtreeFakeValidCursor() )((void) (0));
64133 assert( offsetof(BtCursor, eState)==0 )((void) (0));
64134 assert( sizeof(pCur->eState)==1 )((void) (0));
64135 return CURSOR_VALID0 != *(u8*)pCur;
64136}
64137
64138/*
64139** Return a pointer to a fake BtCursor object that will always answer
64140** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
64141** cursor returned must not be used with any other Btree interface.
64142*/
64143SQLITE_PRIVATEstatic BtCursor *sqlite3BtreeFakeValidCursor(void){
64144 static u8 fakeCursor = CURSOR_VALID0;
64145 assert( offsetof(BtCursor, eState)==0 )((void) (0));
64146 return (BtCursor*)&fakeCursor;
64147}
64148
64149/*
64150** This routine restores a cursor back to its original position after it
64151** has been moved by some outside activity (such as a btree rebalance or
64152** a row having been deleted out from under the cursor).
64153**
64154** On success, the *pDifferentRow parameter is false if the cursor is left
64155** pointing at exactly the same row. *pDifferntRow is the row the cursor
64156** was pointing to has been deleted, forcing the cursor to point to some
64157** nearby row.
64158**
64159** This routine should only be called for a cursor that just returned
64160** TRUE from sqlite3BtreeCursorHasMoved().
64161*/
64162SQLITE_PRIVATEstatic int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
64163 int rc;
64164
64165 assert( pCur!=0 )((void) (0));
64166 assert( pCur->eState!=CURSOR_VALID )((void) (0));
64167 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
64168 if( rc ){
64169 *pDifferentRow = 1;
64170 return rc;
64171 }
64172 if( pCur->eState!=CURSOR_VALID0 ){
64173 *pDifferentRow = 1;
64174 }else{
64175 *pDifferentRow = 0;
64176 }
64177 return SQLITE_OK0;
64178}
64179
64180#ifdef SQLITE_ENABLE_CURSOR_HINTS
64181/*
64182** Provide hints to the cursor. The particular hint given (and the type
64183** and number of the varargs parameters) is determined by the eHintType
64184** parameter. See the definitions of the BTREE_HINT_* macros for details.
64185*/
64186SQLITE_PRIVATEstatic void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
64187 /* Used only by system that substitute their own storage engine */
64188}
64189#endif
64190
64191/*
64192** Provide flag hints to the cursor.
64193*/
64194SQLITE_PRIVATEstatic void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
64195 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 )((void) (0));
64196 pCur->hints = x;
64197}
64198
64199
64200#ifndef SQLITE_OMIT_AUTOVACUUM
64201/*
64202** Given a page number of a regular database page, return the page
64203** number for the pointer-map page that contains the entry for the
64204** input page number.
64205**
64206** Return 0 (not a valid page) for pgno==1 since there is
64207** no pointer map associated with page 1. The integrity_check logic
64208** requires that ptrmapPageno(*,1)!=1.
64209*/
64210static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
64211 int nPagesPerMapPage;
64212 Pgno iPtrMap, ret;
64213 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
64214 if( pgno<2 ) return 0;
64215 nPagesPerMapPage = (pBt->usableSize/5)+1;
64216 iPtrMap = (pgno-2)/nPagesPerMapPage;
64217 ret = (iPtrMap*nPagesPerMapPage) + 2;
64218 if( ret==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
64219 ret++;
64220 }
64221 return ret;
64222}
64223
64224/*
64225** Write an entry into the pointer map.
64226**
64227** This routine updates the pointer map entry for page number 'key'
64228** so that it maps to type 'eType' and parent page number 'pgno'.
64229**
64230** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
64231** a no-op. If an error occurs, the appropriate error code is written
64232** into *pRC.
64233*/
64234static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
64235 DbPage *pDbPage; /* The pointer map page */
64236 u8 *pPtrmap; /* The pointer map data */
64237 Pgno iPtrmap; /* The pointer map page number */
64238 int offset; /* Offset in pointer map page */
64239 int rc; /* Return code from subfunctions */
64240
64241 if( *pRC ) return;
64242
64243 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
64244 /* The master-journal page number must never be used as a pointer map page */
64245 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) )((void) (0));
64246
64247 assert( pBt->autoVacuum )((void) (0));
64248 if( key==0 ){
64249 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(64249);
64250 return;
64251 }
64252 iPtrmap = PTRMAP_PAGENO(pBt, key)ptrmapPageno(pBt, key);
64253 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
64254 if( rc!=SQLITE_OK0 ){
64255 *pRC = rc;
64256 return;
64257 }
64258 if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
64259 /* The first byte of the extra data is the MemPage.isInit byte.
64260 ** If that byte is set, it means this page is also being used
64261 ** as a btree page. */
64262 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(64262);
64263 goto ptrmap_exit;
64264 }
64265 offset = PTRMAP_PTROFFSET(iPtrmap, key)(5*(key-iPtrmap-1));
64266 if( offset<0 ){
64267 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(64267);
64268 goto ptrmap_exit;
64269 }
64270 assert( offset <= (int)pBt->usableSize-5 )((void) (0));
64271 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
64272
64273 if( eType!=pPtrmap[offset] || get4bytesqlite3Get4byte(&pPtrmap[offset+1])!=parent ){
64274 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
64275 *pRC= rc = sqlite3PagerWrite(pDbPage);
64276 if( rc==SQLITE_OK0 ){
64277 pPtrmap[offset] = eType;
64278 put4bytesqlite3Put4byte(&pPtrmap[offset+1], parent);
64279 }
64280 }
64281
64282ptrmap_exit:
64283 sqlite3PagerUnref(pDbPage);
64284}
64285
64286/*
64287** Read an entry from the pointer map.
64288**
64289** This routine retrieves the pointer map entry for page 'key', writing
64290** the type and parent page number to *pEType and *pPgno respectively.
64291** An error code is returned if something goes wrong, otherwise SQLITE_OK.
64292*/
64293static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
64294 DbPage *pDbPage; /* The pointer map page */
64295 int iPtrmap; /* Pointer map page index */
64296 u8 *pPtrmap; /* Pointer map page data */
64297 int offset; /* Offset of entry in pointer map */
64298 int rc;
64299
64300 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
64301
64302 iPtrmap = PTRMAP_PAGENO(pBt, key)ptrmapPageno(pBt, key);
64303 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
64304 if( rc!=0 ){
64305 return rc;
64306 }
64307 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
64308
64309 offset = PTRMAP_PTROFFSET(iPtrmap, key)(5*(key-iPtrmap-1));
64310 if( offset<0 ){
64311 sqlite3PagerUnref(pDbPage);
64312 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(64312);
64313 }
64314 assert( offset <= (int)pBt->usableSize-5 )((void) (0));
64315 assert( pEType!=0 )((void) (0));
64316 *pEType = pPtrmap[offset];
64317 if( pPgno ) *pPgno = get4bytesqlite3Get4byte(&pPtrmap[offset+1]);
64318
64319 sqlite3PagerUnref(pDbPage);
64320 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap)sqlite3CorruptError(64320);
64321 return SQLITE_OK0;
64322}
64323
64324#else /* if defined SQLITE_OMIT_AUTOVACUUM */
64325 #define ptrmapPut(w,x,y,z,rc)
64326 #define ptrmapGet(w,x,y,z) SQLITE_OK0
64327 #define ptrmapPutOvflPtr(x, y, z, rc)
64328#endif
64329
64330/*
64331** Given a btree page and a cell index (0 means the first cell on
64332** the page, 1 means the second cell, and so forth) return a pointer
64333** to the cell content.
64334**
64335** findCellPastPtr() does the same except it skips past the initial
64336** 4-byte child pointer found on interior pages, if there is one.
64337**
64338** This routine works only for pages that do not contain overflow cells.
64339*/
64340#define findCell(P,I)((P)->aData + ((P)->maskPage & ((&(P)->aCellIdx
[2*(I)])[0]<<8 | (&(P)->aCellIdx[2*(I)])[1])))
\
64341 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])((&(P)->aCellIdx[2*(I)])[0]<<8 | (&(P)->aCellIdx
[2*(I)])[1])
))
64342#define findCellPastPtr(P,I)((P)->aDataOfst + ((P)->maskPage & ((&(P)->aCellIdx
[2*(I)])[0]<<8 | (&(P)->aCellIdx[2*(I)])[1])))
\
64343 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])((&(P)->aCellIdx[2*(I)])[0]<<8 | (&(P)->aCellIdx
[2*(I)])[1])
))
64344
64345
64346/*
64347** This is common tail processing for btreeParseCellPtr() and
64348** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
64349** on a single B-tree page. Make necessary adjustments to the CellInfo
64350** structure.
64351*/
64352static SQLITE_NOINLINE__attribute__((noinline)) void btreeParseCellAdjustSizeForOverflow(
64353 MemPage *pPage, /* Page containing the cell */
64354 u8 *pCell, /* Pointer to the cell text. */
64355 CellInfo *pInfo /* Fill in this structure */
64356){
64357 /* If the payload will not fit completely on the local page, we have
64358 ** to decide how much to store locally and how much to spill onto
64359 ** overflow pages. The strategy is to minimize the amount of unused
64360 ** space on overflow pages while keeping the amount of local storage
64361 ** in between minLocal and maxLocal.
64362 **
64363 ** Warning: changing the way overflow payload is distributed in any
64364 ** way will result in an incompatible file format.
64365 */
64366 int minLocal; /* Minimum amount of payload held locally */
64367 int maxLocal; /* Maximum amount of payload held locally */
64368 int surplus; /* Overflow payload available for local storage */
64369
64370 minLocal = pPage->minLocal;
64371 maxLocal = pPage->maxLocal;
64372 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
64373 testcase( surplus==maxLocal );
64374 testcase( surplus==maxLocal+1 );
64375 if( surplus <= maxLocal ){
64376 pInfo->nLocal = (u16)surplus;
64377 }else{
64378 pInfo->nLocal = (u16)minLocal;
64379 }
64380 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
64381}
64382
64383/*
64384** The following routines are implementations of the MemPage.xParseCell()
64385** method.
64386**
64387** Parse a cell content block and fill in the CellInfo structure.
64388**
64389** btreeParseCellPtr() => table btree leaf nodes
64390** btreeParseCellNoPayload() => table btree internal nodes
64391** btreeParseCellPtrIndex() => index btree nodes
64392**
64393** There is also a wrapper function btreeParseCell() that works for
64394** all MemPage types and that references the cell by index rather than
64395** by pointer.
64396*/
64397static void btreeParseCellPtrNoPayload(
64398 MemPage *pPage, /* Page containing the cell */
64399 u8 *pCell, /* Pointer to the cell text. */
64400 CellInfo *pInfo /* Fill in this structure */
64401){
64402 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
64403 assert( pPage->leaf==0 )((void) (0));
64404 assert( pPage->childPtrSize==4 )((void) (0));
64405#ifndef SQLITE_DEBUG
64406 UNUSED_PARAMETER(pPage)(void)(pPage);
64407#endif
64408 pInfo->nSize = 4 + getVarintsqlite3GetVarint(&pCell[4], (u64*)&pInfo->nKey);
64409 pInfo->nPayload = 0;
64410 pInfo->nLocal = 0;
64411 pInfo->pPayload = 0;
64412 return;
64413}
64414static void btreeParseCellPtr(
64415 MemPage *pPage, /* Page containing the cell */
64416 u8 *pCell, /* Pointer to the cell text. */
64417 CellInfo *pInfo /* Fill in this structure */
64418){
64419 u8 *pIter; /* For scanning through pCell */
64420 u32 nPayload; /* Number of bytes of cell payload */
64421 u64 iKey; /* Extracted Key value */
64422
64423 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
64424 assert( pPage->leaf==0 || pPage->leaf==1 )((void) (0));
64425 assert( pPage->intKeyLeaf )((void) (0));
64426 assert( pPage->childPtrSize==0 )((void) (0));
64427 pIter = pCell;
64428
64429 /* The next block of code is equivalent to:
64430 **
64431 ** pIter += getVarint32(pIter, nPayload);
64432 **
64433 ** The code is inlined to avoid a function call.
64434 */
64435 nPayload = *pIter;
64436 if( nPayload>=0x80 ){
64437 u8 *pEnd = &pIter[8];
64438 nPayload &= 0x7f;
64439 do{
64440 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
64441 }while( (*pIter)>=0x80 && pIter<pEnd );
64442 }
64443 pIter++;
64444
64445 /* The next block of code is equivalent to:
64446 **
64447 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
64448 **
64449 ** The code is inlined to avoid a function call.
64450 */
64451 iKey = *pIter;
64452 if( iKey>=0x80 ){
64453 u8 *pEnd = &pIter[7];
64454 iKey &= 0x7f;
64455 while(1){
64456 iKey = (iKey<<7) | (*++pIter & 0x7f);
64457 if( (*pIter)<0x80 ) break;
64458 if( pIter>=pEnd ){
64459 iKey = (iKey<<8) | *++pIter;
64460 break;
64461 }
64462 }
64463 }
64464 pIter++;
64465
64466 pInfo->nKey = *(i64*)&iKey;
64467 pInfo->nPayload = nPayload;
64468 pInfo->pPayload = pIter;
64469 testcase( nPayload==pPage->maxLocal );
64470 testcase( nPayload==pPage->maxLocal+1 );
64471 if( nPayload<=pPage->maxLocal ){
64472 /* This is the (easy) common case where the entire payload fits
64473 ** on the local page. No overflow is required.
64474 */
64475 pInfo->nSize = nPayload + (u16)(pIter - pCell);
64476 if( pInfo->nSize<4 ) pInfo->nSize = 4;
64477 pInfo->nLocal = (u16)nPayload;
64478 }else{
64479 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
64480 }
64481}
64482static void btreeParseCellPtrIndex(
64483 MemPage *pPage, /* Page containing the cell */
64484 u8 *pCell, /* Pointer to the cell text. */
64485 CellInfo *pInfo /* Fill in this structure */
64486){
64487 u8 *pIter; /* For scanning through pCell */
64488 u32 nPayload; /* Number of bytes of cell payload */
64489
64490 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
64491 assert( pPage->leaf==0 || pPage->leaf==1 )((void) (0));
64492 assert( pPage->intKeyLeaf==0 )((void) (0));
64493 pIter = pCell + pPage->childPtrSize;
64494 nPayload = *pIter;
64495 if( nPayload>=0x80 ){
64496 u8 *pEnd = &pIter[8];
64497 nPayload &= 0x7f;
64498 do{
64499 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
64500 }while( *(pIter)>=0x80 && pIter<pEnd );
64501 }
64502 pIter++;
64503 pInfo->nKey = nPayload;
64504 pInfo->nPayload = nPayload;
64505 pInfo->pPayload = pIter;
64506 testcase( nPayload==pPage->maxLocal );
64507 testcase( nPayload==pPage->maxLocal+1 );
64508 if( nPayload<=pPage->maxLocal ){
64509 /* This is the (easy) common case where the entire payload fits
64510 ** on the local page. No overflow is required.
64511 */
64512 pInfo->nSize = nPayload + (u16)(pIter - pCell);
64513 if( pInfo->nSize<4 ) pInfo->nSize = 4;
64514 pInfo->nLocal = (u16)nPayload;
64515 }else{
64516 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
64517 }
64518}
64519static void btreeParseCell(
64520 MemPage *pPage, /* Page containing the cell */
64521 int iCell, /* The cell index. First cell is 0 */
64522 CellInfo *pInfo /* Fill in this structure */
64523){
64524 pPage->xParseCell(pPage, findCell(pPage, iCell)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCell)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iCell)])[1])))
, pInfo);
64525}
64526
64527/*
64528** The following routines are implementations of the MemPage.xCellSize
64529** method.
64530**
64531** Compute the total number of bytes that a Cell needs in the cell
64532** data area of the btree-page. The return number includes the cell
64533** data header and the local payload, but not any overflow page or
64534** the space used by the cell pointer.
64535**
64536** cellSizePtrNoPayload() => table internal nodes
64537** cellSizePtr() => all index nodes & table leaf nodes
64538*/
64539static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
64540 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
64541 u8 *pEnd; /* End mark for a varint */
64542 u32 nSize; /* Size value to return */
64543
64544#ifdef SQLITE_DEBUG
64545 /* The value returned by this function should always be the same as
64546 ** the (CellInfo.nSize) value found by doing a full parse of the
64547 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
64548 ** this function verifies that this invariant is not violated. */
64549 CellInfo debuginfo;
64550 pPage->xParseCell(pPage, pCell, &debuginfo);
64551#endif
64552
64553 nSize = *pIter;
64554 if( nSize>=0x80 ){
64555 pEnd = &pIter[8];
64556 nSize &= 0x7f;
64557 do{
64558 nSize = (nSize<<7) | (*++pIter & 0x7f);
64559 }while( *(pIter)>=0x80 && pIter<pEnd );
64560 }
64561 pIter++;
64562 if( pPage->intKey ){
64563 /* pIter now points at the 64-bit integer key value, a variable length
64564 ** integer. The following block moves pIter to point at the first byte
64565 ** past the end of the key value. */
64566 pEnd = &pIter[9];
64567 while( (*pIter++)&0x80 && pIter<pEnd );
64568 }
64569 testcase( nSize==pPage->maxLocal );
64570 testcase( nSize==pPage->maxLocal+1 );
64571 if( nSize<=pPage->maxLocal ){
64572 nSize += (u32)(pIter - pCell);
64573 if( nSize<4 ) nSize = 4;
64574 }else{
64575 int minLocal = pPage->minLocal;
64576 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
64577 testcase( nSize==pPage->maxLocal );
64578 testcase( nSize==pPage->maxLocal+1 );
64579 if( nSize>pPage->maxLocal ){
64580 nSize = minLocal;
64581 }
64582 nSize += 4 + (u16)(pIter - pCell);
64583 }
64584 assert( nSize==debuginfo.nSize || CORRUPT_DB )((void) (0));
64585 return (u16)nSize;
64586}
64587static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
64588 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
64589 u8 *pEnd; /* End mark for a varint */
64590
64591#ifdef SQLITE_DEBUG
64592 /* The value returned by this function should always be the same as
64593 ** the (CellInfo.nSize) value found by doing a full parse of the
64594 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
64595 ** this function verifies that this invariant is not violated. */
64596 CellInfo debuginfo;
64597 pPage->xParseCell(pPage, pCell, &debuginfo);
64598#else
64599 UNUSED_PARAMETER(pPage)(void)(pPage);
64600#endif
64601
64602 assert( pPage->childPtrSize==4 )((void) (0));
64603 pEnd = pIter + 9;
64604 while( (*pIter++)&0x80 && pIter<pEnd );
64605 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB )((void) (0));
64606 return (u16)(pIter - pCell);
64607}
64608
64609
64610#ifdef SQLITE_DEBUG
64611/* This variation on cellSizePtr() is used inside of assert() statements
64612** only. */
64613static u16 cellSize(MemPage *pPage, int iCell){
64614 return pPage->xCellSize(pPage, findCell(pPage, iCell)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCell)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iCell)])[1])))
);
64615}
64616#endif
64617
64618#ifndef SQLITE_OMIT_AUTOVACUUM
64619/*
64620** The cell pCell is currently part of page pSrc but will ultimately be part
64621** of pPage. (pSrc and pPager are often the same.) If pCell contains a
64622** pointer to an overflow page, insert an entry into the pointer-map for
64623** the overflow page that will be valid after pCell has been moved to pPage.
64624*/
64625static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
64626 CellInfo info;
64627 if( *pRC ) return;
64628 assert( pCell!=0 )((void) (0));
64629 pPage->xParseCell(pPage, pCell, &info);
64630 if( info.nLocal<info.nPayload ){
64631 Pgno ovfl;
64632 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal)(((uptr)(pSrc->aDataEnd)>=(uptr)(pCell))&&((uptr
)(pSrc->aDataEnd)<(uptr)(pCell+info.nLocal)))
){
64633 testcase( pSrc!=pPage );
64634 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(64634);
64635 return;
64636 }
64637 ovfl = get4bytesqlite3Get4byte(&pCell[info.nSize-4]);
64638 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW13, pPage->pgno, pRC);
64639 }
64640}
64641#endif
64642
64643
64644/*
64645** Defragment the page given. This routine reorganizes cells within the
64646** page so that there are no free-blocks on the free-block list.
64647**
64648** Parameter nMaxFrag is the maximum amount of fragmented space that may be
64649** present in the page after this routine returns.
64650**
64651** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
64652** b-tree page so that there are no freeblocks or fragment bytes, all
64653** unused bytes are contained in the unallocated space region, and all
64654** cells are packed tightly at the end of the page.
64655*/
64656static int defragmentPage(MemPage *pPage, int nMaxFrag){
64657 int i; /* Loop counter */
64658 int pc; /* Address of the i-th cell */
64659 int hdr; /* Offset to the page header */
64660 int size; /* Size of a cell */
64661 int usableSize; /* Number of usable bytes on a page */
64662 int cellOffset; /* Offset to the cell pointer array */
64663 int cbrk; /* Offset to the cell content area */
64664 int nCell; /* Number of cells on the page */
64665 unsigned char *data; /* The page data */
64666 unsigned char *temp; /* Temp area for cell content */
64667 unsigned char *src; /* Source of content */
64668 int iCellFirst; /* First allowable cell index */
64669 int iCellLast; /* Last possible cell index */
64670
64671 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
64672 assert( pPage->pBt!=0 )((void) (0));
64673 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE )((void) (0));
64674 assert( pPage->nOverflow==0 )((void) (0));
64675 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
64676 temp = 0;
64677 src = data = pPage->aData;
64678 hdr = pPage->hdrOffset;
64679 cellOffset = pPage->cellOffset;
64680 nCell = pPage->nCell;
64681 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB )((void) (0));
64682 iCellFirst = cellOffset + 2*nCell;
64683 usableSize = pPage->pBt->usableSize;
64684
64685 /* This block handles pages with two or fewer free blocks and nMaxFrag
64686 ** or fewer fragmented bytes. In this case it is faster to move the
64687 ** two (or one) blocks of cells using memmove() and add the required
64688 ** offsets to each pointer in the cell-pointer array than it is to
64689 ** reconstruct the entire page. */
64690 if( (int)data[hdr+7]<=nMaxFrag ){
64691 int iFree = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
64692 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64692);
64693 if( iFree ){
64694 int iFree2 = get2byte(&data[iFree])((&data[iFree])[0]<<8 | (&data[iFree])[1]);
64695 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64695);
64696 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
64697 u8 *pEnd = &data[cellOffset + nCell*2];
64698 u8 *pAddr;
64699 int sz2 = 0;
64700 int sz = get2byte(&data[iFree+2])((&data[iFree+2])[0]<<8 | (&data[iFree+2])[1]);
64701 int top = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
64702 if( top>=iFree ){
64703 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64703);
64704 }
64705 if( iFree2 ){
64706 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64706);
64707 sz2 = get2byte(&data[iFree2+2])((&data[iFree2+2])[0]<<8 | (&data[iFree2+2])[1]
)
;
64708 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64708);
64709 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
64710 sz += sz2;
64711 }else if( iFree+sz>usableSize ){
64712 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64712);
64713 }
64714
64715 cbrk = top+sz;
64716 assert( cbrk+(iFree-top) <= usableSize )((void) (0));
64717 memmove(&data[cbrk], &data[top], iFree-top);
64718 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
64719 pc = get2byte(pAddr)((pAddr)[0]<<8 | (pAddr)[1]);
64720 if( pc<iFree ){ put2byte(pAddr, pc+sz)((pAddr)[0] = (u8)((pc+sz)>>8), (pAddr)[1] = (u8)(pc+sz
))
; }
64721 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2)((pAddr)[0] = (u8)((pc+sz2)>>8), (pAddr)[1] = (u8)(pc+sz2
))
; }
64722 }
64723 goto defragment_out;
64724 }
64725 }
64726 }
64727
64728 cbrk = usableSize;
64729 iCellLast = usableSize - 4;
64730 for(i=0; i<nCell; i++){
64731 u8 *pAddr; /* The i-th cell pointer */
64732 pAddr = &data[cellOffset + i*2];
64733 pc = get2byte(pAddr)((pAddr)[0]<<8 | (pAddr)[1]);
64734 testcase( pc==iCellFirst );
64735 testcase( pc==iCellLast );
64736 /* These conditions have already been verified in btreeInitPage()
64737 ** if PRAGMA cell_size_check=ON.
64738 */
64739 if( pc<iCellFirst || pc>iCellLast ){
64740 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64740);
64741 }
64742 assert( pc>=iCellFirst && pc<=iCellLast )((void) (0));
64743 size = pPage->xCellSize(pPage, &src[pc]);
64744 cbrk -= size;
64745 if( cbrk<iCellFirst || pc+size>usableSize ){
64746 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64746);
64747 }
64748 assert( cbrk+size<=usableSize && cbrk>=iCellFirst )((void) (0));
64749 testcase( cbrk+size==usableSize );
64750 testcase( pc+size==usableSize );
64751 put2byte(pAddr, cbrk)((pAddr)[0] = (u8)((cbrk)>>8), (pAddr)[1] = (u8)(cbrk));
64752 if( temp==0 ){
64753 int x;
64754 if( cbrk==pc ) continue;
64755 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
64756 x = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
64757 memcpy(&temp[x], &data[x], (cbrk+size) - x);
64758 src = temp;
64759 }
64760 memcpy(&data[cbrk], &src[pc], size);
64761 }
64762 data[hdr+7] = 0;
64763
64764 defragment_out:
64765 assert( pPage->nFree>=0 )((void) (0));
64766 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
64767 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64767);
64768 }
64769 assert( cbrk>=iCellFirst )((void) (0));
64770 put2byte(&data[hdr+5], cbrk)((&data[hdr+5])[0] = (u8)((cbrk)>>8), (&data[hdr
+5])[1] = (u8)(cbrk))
;
64771 data[hdr+1] = 0;
64772 data[hdr+2] = 0;
64773 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
64774 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
64775 return SQLITE_OK0;
64776}
64777
64778/*
64779** Search the free-list on page pPg for space to store a cell nByte bytes in
64780** size. If one can be found, return a pointer to the space and remove it
64781** from the free-list.
64782**
64783** If no suitable space can be found on the free-list, return NULL.
64784**
64785** This function may detect corruption within pPg. If corruption is
64786** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
64787**
64788** Slots on the free list that are between 1 and 3 bytes larger than nByte
64789** will be ignored if adding the extra space to the fragmentation count
64790** causes the fragmentation count to exceed 60.
64791*/
64792static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
64793 const int hdr = pPg->hdrOffset; /* Offset to page header */
64794 u8 * const aData = pPg->aData; /* Page data */
64795 int iAddr = hdr + 1; /* Address of ptr to pc */
64796 int pc = get2byte(&aData[iAddr])((&aData[iAddr])[0]<<8 | (&aData[iAddr])[1]); /* Address of a free slot */
64797 int x; /* Excess size of the slot */
64798 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
64799 int size; /* Size of the free slot */
64800
64801 assert( pc>0 )((void) (0));
64802 while( pc<=maxPC ){
64803 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
64804 ** freeblock form a big-endian integer which is the size of the freeblock
64805 ** in bytes, including the 4-byte header. */
64806 size = get2byte(&aData[pc+2])((&aData[pc+2])[0]<<8 | (&aData[pc+2])[1]);
64807 if( (x = size - nByte)>=0 ){
64808 testcase( x==4 );
64809 testcase( x==3 );
64810 if( x<4 ){
64811 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
64812 ** number of bytes in fragments may not exceed 60. */
64813 if( aData[hdr+7]>57 ) return 0;
64814
64815 /* Remove the slot from the free-list. Update the number of
64816 ** fragmented bytes within the page. */
64817 memcpy(&aData[iAddr], &aData[pc], 2);
64818 aData[hdr+7] += (u8)x;
64819 }else if( x+pc > maxPC ){
64820 /* This slot extends off the end of the usable part of the page */
64821 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(64821);
64822 return 0;
64823 }else{
64824 /* The slot remains on the free-list. Reduce its size to account
64825 ** for the portion used by the new allocation. */
64826 put2byte(&aData[pc+2], x)((&aData[pc+2])[0] = (u8)((x)>>8), (&aData[pc+2
])[1] = (u8)(x))
;
64827 }
64828 return &aData[pc + x];
64829 }
64830 iAddr = pc;
64831 pc = get2byte(&aData[pc])((&aData[pc])[0]<<8 | (&aData[pc])[1]);
64832 if( pc<=iAddr+size ){
64833 if( pc ){
64834 /* The next slot in the chain is not past the end of the current slot */
64835 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(64835);
64836 }
64837 return 0;
64838 }
64839 }
64840 if( pc>maxPC+nByte-4 ){
64841 /* The free slot chain extends off the end of the page */
64842 *pRc = SQLITE_CORRUPT_PAGE(pPg)sqlite3CorruptError(64842);
64843 }
64844 return 0;
64845}
64846
64847/*
64848** Allocate nByte bytes of space from within the B-Tree page passed
64849** as the first argument. Write into *pIdx the index into pPage->aData[]
64850** of the first byte of allocated space. Return either SQLITE_OK or
64851** an error code (usually SQLITE_CORRUPT).
64852**
64853** The caller guarantees that there is sufficient space to make the
64854** allocation. This routine might need to defragment in order to bring
64855** all the space together, however. This routine will avoid using
64856** the first two bytes past the cell pointer area since presumably this
64857** allocation is being made in order to insert a new cell, so we will
64858** also end up needing a new cell pointer.
64859*/
64860static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
64861 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
64862 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
64863 int top; /* First byte of cell content area */
64864 int rc = SQLITE_OK0; /* Integer return code */
64865 int gap; /* First byte of gap between cell pointers and cell content */
64866
64867 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
64868 assert( pPage->pBt )((void) (0));
64869 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
64870 assert( nByte>=0 )((void) (0)); /* Minimum cell size is 4 */
64871 assert( pPage->nFree>=nByte )((void) (0));
64872 assert( pPage->nOverflow==0 )((void) (0));
64873 assert( nByte < (int)(pPage->pBt->usableSize-8) )((void) (0));
64874
64875 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf )((void) (0));
64876 gap = pPage->cellOffset + 2*pPage->nCell;
64877 assert( gap<=65536 )((void) (0));
64878 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
64879 ** and the reserved space is zero (the usual value for reserved space)
64880 ** then the cell content offset of an empty page wants to be 65536.
64881 ** However, that integer is too large to be stored in a 2-byte unsigned
64882 ** integer, so a value of 0 is used in its place. */
64883 top = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
64884 assert( top<=(int)pPage->pBt->usableSize )((void) (0)); /* by btreeComputeFreeSpace() */
64885 if( gap>top ){
64886 if( top==0 && pPage->pBt->usableSize==65536 ){
64887 top = 65536;
64888 }else{
64889 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64889);
64890 }
64891 }
64892
64893 /* If there is enough space between gap and top for one more cell pointer,
64894 ** and if the freelist is not empty, then search the
64895 ** freelist looking for a slot big enough to satisfy the request.
64896 */
64897 testcase( gap+2==top );
64898 testcase( gap+1==top );
64899 testcase( gap==top );
64900 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
64901 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
64902 if( pSpace ){
64903 assert( pSpace>=data && (pSpace - data)<65536 )((void) (0));
64904 *pIdx = (int)(pSpace - data);
64905 return SQLITE_OK0;
64906 }else if( rc ){
64907 return rc;
64908 }
64909 }
64910
64911 /* The request could not be fulfilled using a freelist slot. Check
64912 ** to see if defragmentation is necessary.
64913 */
64914 testcase( gap+2+nByte==top );
64915 if( gap+2+nByte>top ){
64916 assert( pPage->nCell>0 || CORRUPT_DB )((void) (0));
64917 assert( pPage->nFree>=0 )((void) (0));
64918 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte))((4)<(pPage->nFree - (2+nByte))?(4):(pPage->nFree - (
2+nByte)))
);
64919 if( rc ) return rc;
64920 top = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
64921 assert( gap+2+nByte<=top )((void) (0));
64922 }
64923
64924
64925 /* Allocate memory from the gap in between the cell pointer array
64926 ** and the cell content area. The btreeComputeFreeSpace() call has already
64927 ** validated the freelist. Given that the freelist is valid, there
64928 ** is no way that the allocation can extend off the end of the page.
64929 ** The assert() below verifies the previous sentence.
64930 */
64931 top -= nByte;
64932 put2byte(&data[hdr+5], top)((&data[hdr+5])[0] = (u8)((top)>>8), (&data[hdr
+5])[1] = (u8)(top))
;
64933 assert( top+nByte <= (int)pPage->pBt->usableSize )((void) (0));
64934 *pIdx = top;
64935 return SQLITE_OK0;
64936}
64937
64938/*
64939** Return a section of the pPage->aData to the freelist.
64940** The first byte of the new free block is pPage->aData[iStart]
64941** and the size of the block is iSize bytes.
64942**
64943** Adjacent freeblocks are coalesced.
64944**
64945** Even though the freeblock list was checked by btreeComputeFreeSpace(),
64946** that routine will not detect overlap between cells or freeblocks. Nor
64947** does it detect cells or freeblocks that encrouch into the reserved bytes
64948** at the end of the page. So do additional corruption checks inside this
64949** routine and return SQLITE_CORRUPT if any problems are found.
64950*/
64951static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
64952 u16 iPtr; /* Address of ptr to next freeblock */
64953 u16 iFreeBlk; /* Address of the next freeblock */
64954 u8 hdr; /* Page header size. 0 or 100 */
64955 u8 nFrag = 0; /* Reduction in fragmentation */
64956 u16 iOrigSize = iSize; /* Original value of iSize */
64957 u16 x; /* Offset to cell content area */
64958 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
64959 unsigned char *data = pPage->aData; /* Page content */
64960
64961 assert( pPage->pBt!=0 )((void) (0));
64962 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
64963 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize )((void) (0));
64964 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize )((void) (0));
64965 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
64966 assert( iSize>=4 )((void) (0)); /* Minimum cell size is 4 */
64967 assert( iStart<=pPage->pBt->usableSize-4 )((void) (0));
64968
64969 /* The list of freeblocks must be in ascending order. Find the
64970 ** spot on the list where iStart should be inserted.
64971 */
64972 hdr = pPage->hdrOffset;
64973 iPtr = hdr + 1;
64974 if( data[iPtr+1]==0 && data[iPtr]==0 ){
64975 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
64976 }else{
64977 while( (iFreeBlk = get2byte(&data[iPtr])((&data[iPtr])[0]<<8 | (&data[iPtr])[1]))<iStart ){
64978 if( iFreeBlk<iPtr+4 ){
64979 if( iFreeBlk==0 ) break;
64980 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64980);
64981 }
64982 iPtr = iFreeBlk;
64983 }
64984 if( iFreeBlk>pPage->pBt->usableSize-4 ){
64985 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64985);
64986 }
64987 assert( iFreeBlk>iPtr || iFreeBlk==0 )((void) (0));
64988
64989 /* At this point:
64990 ** iFreeBlk: First freeblock after iStart, or zero if none
64991 ** iPtr: The address of a pointer to iFreeBlk
64992 **
64993 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
64994 */
64995 if( iFreeBlk && iEnd+3>=iFreeBlk ){
64996 nFrag = iFreeBlk - iEnd;
64997 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(64997);
64998 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2])((&data[iFreeBlk+2])[0]<<8 | (&data[iFreeBlk+2]
)[1])
;
64999 if( iEnd > pPage->pBt->usableSize ){
65000 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65000);
65001 }
65002 iSize = iEnd - iStart;
65003 iFreeBlk = get2byte(&data[iFreeBlk])((&data[iFreeBlk])[0]<<8 | (&data[iFreeBlk])[1]
)
;
65004 }
65005
65006 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
65007 ** pointer in the page header) then check to see if iStart should be
65008 ** coalesced onto the end of iPtr.
65009 */
65010 if( iPtr>hdr+1 ){
65011 int iPtrEnd = iPtr + get2byte(&data[iPtr+2])((&data[iPtr+2])[0]<<8 | (&data[iPtr+2])[1]);
65012 if( iPtrEnd+3>=iStart ){
65013 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65013);
65014 nFrag += iStart - iPtrEnd;
65015 iSize = iEnd - iPtr;
65016 iStart = iPtr;
65017 }
65018 }
65019 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65019);
65020 data[hdr+7] -= nFrag;
65021 }
65022 x = get2byte(&data[hdr+5])((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
65023 if( iStart<=x ){
65024 /* The new freeblock is at the beginning of the cell content area,
65025 ** so just extend the cell content area rather than create another
65026 ** freelist entry */
65027 if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65027);
65028 put2byte(&data[hdr+1], iFreeBlk)((&data[hdr+1])[0] = (u8)((iFreeBlk)>>8), (&data
[hdr+1])[1] = (u8)(iFreeBlk))
;
65029 put2byte(&data[hdr+5], iEnd)((&data[hdr+5])[0] = (u8)((iEnd)>>8), (&data[hdr
+5])[1] = (u8)(iEnd))
;
65030 }else{
65031 /* Insert the new freeblock into the freelist */
65032 put2byte(&data[iPtr], iStart)((&data[iPtr])[0] = (u8)((iStart)>>8), (&data[iPtr
])[1] = (u8)(iStart))
;
65033 }
65034 if( pPage->pBt->btsFlags & BTS_FAST_SECURE0x000c ){
65035 /* Overwrite deleted information with zeros when the secure_delete
65036 ** option is enabled */
65037 memset(&data[iStart], 0, iSize);
65038 }
65039 put2byte(&data[iStart], iFreeBlk)((&data[iStart])[0] = (u8)((iFreeBlk)>>8), (&data
[iStart])[1] = (u8)(iFreeBlk))
;
65040 put2byte(&data[iStart+2], iSize)((&data[iStart+2])[0] = (u8)((iSize)>>8), (&data
[iStart+2])[1] = (u8)(iSize))
;
65041 pPage->nFree += iOrigSize;
65042 return SQLITE_OK0;
65043}
65044
65045/*
65046** Decode the flags byte (the first byte of the header) for a page
65047** and initialize fields of the MemPage structure accordingly.
65048**
65049** Only the following combinations are supported. Anything different
65050** indicates a corrupt database files:
65051**
65052** PTF_ZERODATA
65053** PTF_ZERODATA | PTF_LEAF
65054** PTF_LEAFDATA | PTF_INTKEY
65055** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
65056*/
65057static int decodeFlags(MemPage *pPage, int flagByte){
65058 BtShared *pBt; /* A copy of pPage->pBt */
65059
65060 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) )((void) (0));
65061 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
65062 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 )((void) (0));
65063 flagByte &= ~PTF_LEAF0x08;
65064 pPage->childPtrSize = 4-4*pPage->leaf;
65065 pPage->xCellSize = cellSizePtr;
65066 pBt = pPage->pBt;
65067 if( flagByte==(PTF_LEAFDATA0x04 | PTF_INTKEY0x01) ){
65068 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
65069 ** interior table b-tree page. */
65070 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 )((void) (0));
65071 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
65072 ** leaf table b-tree page. */
65073 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 )((void) (0));
65074 pPage->intKey = 1;
65075 if( pPage->leaf ){
65076 pPage->intKeyLeaf = 1;
65077 pPage->xParseCell = btreeParseCellPtr;
65078 }else{
65079 pPage->intKeyLeaf = 0;
65080 pPage->xCellSize = cellSizePtrNoPayload;
65081 pPage->xParseCell = btreeParseCellPtrNoPayload;
65082 }
65083 pPage->maxLocal = pBt->maxLeaf;
65084 pPage->minLocal = pBt->minLeaf;
65085 }else if( flagByte==PTF_ZERODATA0x02 ){
65086 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
65087 ** interior index b-tree page. */
65088 assert( (PTF_ZERODATA)==2 )((void) (0));
65089 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
65090 ** leaf index b-tree page. */
65091 assert( (PTF_ZERODATA|PTF_LEAF)==10 )((void) (0));
65092 pPage->intKey = 0;
65093 pPage->intKeyLeaf = 0;
65094 pPage->xParseCell = btreeParseCellPtrIndex;
65095 pPage->maxLocal = pBt->maxLocal;
65096 pPage->minLocal = pBt->minLocal;
65097 }else{
65098 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
65099 ** an error. */
65100 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65100);
65101 }
65102 pPage->max1bytePayload = pBt->max1bytePayload;
65103 return SQLITE_OK0;
65104}
65105
65106/*
65107** Compute the amount of freespace on the page. In other words, fill
65108** in the pPage->nFree field.
65109*/
65110static int btreeComputeFreeSpace(MemPage *pPage){
65111 int pc; /* Address of a freeblock within pPage->aData[] */
65112 u8 hdr; /* Offset to beginning of page header */
65113 u8 *data; /* Equal to pPage->aData */
65114 int usableSize; /* Amount of usable space on each page */
65115 int nFree; /* Number of unused bytes on the page */
65116 int top; /* First byte of the cell content area */
65117 int iCellFirst; /* First allowable cell or freeblock offset */
65118 int iCellLast; /* Last possible cell or freeblock offset */
65119
65120 assert( pPage->pBt!=0 )((void) (0));
65121 assert( pPage->pBt->db!=0 )((void) (0));
65122 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
65123 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) )((void) (0));
65124 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) )((void) (0));
65125 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) )((void) (0));
65126 assert( pPage->isInit==1 )((void) (0));
65127 assert( pPage->nFree<0 )((void) (0));
65128
65129 usableSize = pPage->pBt->usableSize;
65130 hdr = pPage->hdrOffset;
65131 data = pPage->aData;
65132 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
65133 ** the start of the cell content area. A zero value for this integer is
65134 ** interpreted as 65536. */
65135 top = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
65136 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
65137 iCellLast = usableSize - 4;
65138
65139 /* Compute the total free space on the page
65140 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
65141 ** start of the first freeblock on the page, or is zero if there are no
65142 ** freeblocks. */
65143 pc = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
65144 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
65145 if( pc>0 ){
65146 u32 next, size;
65147 if( pc<iCellFirst ){
65148 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
65149 ** always be at least one cell before the first freeblock.
65150 */
65151 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65151);
65152 }
65153 while( 1 ){
65154 if( pc>iCellLast ){
65155 /* Freeblock off the end of the page */
65156 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65156);
65157 }
65158 next = get2byte(&data[pc])((&data[pc])[0]<<8 | (&data[pc])[1]);
65159 size = get2byte(&data[pc+2])((&data[pc+2])[0]<<8 | (&data[pc+2])[1]);
65160 nFree = nFree + size;
65161 if( next<=pc+size+3 ) break;
65162 pc = next;
65163 }
65164 if( next>0 ){
65165 /* Freeblock not in ascending order */
65166 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65166);
65167 }
65168 if( pc+size>(unsigned int)usableSize ){
65169 /* Last freeblock extends past page end */
65170 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65170);
65171 }
65172 }
65173
65174 /* At this point, nFree contains the sum of the offset to the start
65175 ** of the cell-content area plus the number of free bytes within
65176 ** the cell-content area. If this is greater than the usable-size
65177 ** of the page, then the page must be corrupted. This check also
65178 ** serves to verify that the offset to the start of the cell-content
65179 ** area, according to the page header, lies within the page.
65180 */
65181 if( nFree>usableSize || nFree<iCellFirst ){
65182 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65182);
65183 }
65184 pPage->nFree = (u16)(nFree - iCellFirst);
65185 return SQLITE_OK0;
65186}
65187
65188/*
65189** Do additional sanity check after btreeInitPage() if
65190** PRAGMA cell_size_check=ON
65191*/
65192static SQLITE_NOINLINE__attribute__((noinline)) int btreeCellSizeCheck(MemPage *pPage){
65193 int iCellFirst; /* First allowable cell or freeblock offset */
65194 int iCellLast; /* Last possible cell or freeblock offset */
65195 int i; /* Index into the cell pointer array */
65196 int sz; /* Size of a cell */
65197 int pc; /* Address of a freeblock within pPage->aData[] */
65198 u8 *data; /* Equal to pPage->aData */
65199 int usableSize; /* Maximum usable space on the page */
65200 int cellOffset; /* Start of cell content area */
65201
65202 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
65203 usableSize = pPage->pBt->usableSize;
65204 iCellLast = usableSize - 4;
65205 data = pPage->aData;
65206 cellOffset = pPage->cellOffset;
65207 if( !pPage->leaf ) iCellLast--;
65208 for(i=0; i<pPage->nCell; i++){
65209 pc = get2byteAligned(&data[cellOffset+i*2])((&data[cellOffset+i*2])[0]<<8 | (&data[cellOffset
+i*2])[1])
;
65210 testcase( pc==iCellFirst );
65211 testcase( pc==iCellLast );
65212 if( pc<iCellFirst || pc>iCellLast ){
65213 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65213);
65214 }
65215 sz = pPage->xCellSize(pPage, &data[pc]);
65216 testcase( pc+sz==usableSize );
65217 if( pc+sz>usableSize ){
65218 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65218);
65219 }
65220 }
65221 return SQLITE_OK0;
65222}
65223
65224/*
65225** Initialize the auxiliary information for a disk block.
65226**
65227** Return SQLITE_OK on success. If we see that the page does
65228** not contain a well-formed database page, then return
65229** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
65230** guarantee that the page is well-formed. It only shows that
65231** we failed to detect any corruption.
65232*/
65233static int btreeInitPage(MemPage *pPage){
65234 u8 *data; /* Equal to pPage->aData */
65235 BtShared *pBt; /* The main btree structure */
65236
65237 assert( pPage->pBt!=0 )((void) (0));
65238 assert( pPage->pBt->db!=0 )((void) (0));
65239 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
65240 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) )((void) (0));
65241 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) )((void) (0));
65242 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) )((void) (0));
65243 assert( pPage->isInit==0 )((void) (0));
65244
65245 pBt = pPage->pBt;
65246 data = pPage->aData + pPage->hdrOffset;
65247 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
65248 ** the b-tree page type. */
65249 if( decodeFlags(pPage, data[0]) ){
65250 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65250);
65251 }
65252 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 )((void) (0));
65253 pPage->maskPage = (u16)(pBt->pageSize - 1);
65254 pPage->nOverflow = 0;
65255 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
65256 pPage->aCellIdx = data + pPage->childPtrSize + 8;
65257 pPage->aDataEnd = pPage->aData + pBt->usableSize;
65258 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
65259 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
65260 ** number of cells on the page. */
65261 pPage->nCell = get2byte(&data[3])((&data[3])[0]<<8 | (&data[3])[1]);
65262 if( pPage->nCell>MX_CELL(pBt)((pBt->pageSize-8)/6) ){
65263 /* To many cells for a single page. The page must be corrupt */
65264 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(65264);
65265 }
65266 testcase( pPage->nCell==MX_CELL(pBt) );
65267 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
65268 ** possible for a root page of a table that contains no rows) then the
65269 ** offset to the cell content area will equal the page size minus the
65270 ** bytes of reserved space. */
65271 assert( pPage->nCell>0((void) (0))
65272 || get2byteNotZero(&data[5])==(int)pBt->usableSize((void) (0))
65273 || CORRUPT_DB )((void) (0));
65274 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
65275 pPage->isInit = 1;
65276 if( pBt->db->flags & SQLITE_CellSizeCk0x00200000 ){
65277 return btreeCellSizeCheck(pPage);
65278 }
65279 return SQLITE_OK0;
65280}
65281
65282/*
65283** Set up a raw page so that it looks like a database page holding
65284** no entries.
65285*/
65286static void zeroPage(MemPage *pPage, int flags){
65287 unsigned char *data = pPage->aData;
65288 BtShared *pBt = pPage->pBt;
65289 u8 hdr = pPage->hdrOffset;
65290 u16 first;
65291
65292 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno )((void) (0));
65293 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
65294 assert( sqlite3PagerGetData(pPage->pDbPage) == data )((void) (0));
65295 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
65296 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
65297 if( pBt->btsFlags & BTS_FAST_SECURE0x000c ){
65298 memset(&data[hdr], 0, pBt->usableSize - hdr);
65299 }
65300 data[hdr] = (char)flags;
65301 first = hdr + ((flags&PTF_LEAF0x08)==0 ? 12 : 8);
65302 memset(&data[hdr+1], 0, 4);
65303 data[hdr+7] = 0;
65304 put2byte(&data[hdr+5], pBt->usableSize)((&data[hdr+5])[0] = (u8)((pBt->usableSize)>>8),
(&data[hdr+5])[1] = (u8)(pBt->usableSize))
;
65305 pPage->nFree = (u16)(pBt->usableSize - first);
65306 decodeFlags(pPage, flags);
65307 pPage->cellOffset = first;
65308 pPage->aDataEnd = &data[pBt->usableSize];
65309 pPage->aCellIdx = &data[first];
65310 pPage->aDataOfst = &data[pPage->childPtrSize];
65311 pPage->nOverflow = 0;
65312 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 )((void) (0));
65313 pPage->maskPage = (u16)(pBt->pageSize - 1);
65314 pPage->nCell = 0;
65315 pPage->isInit = 1;
65316}
65317
65318
65319/*
65320** Convert a DbPage obtained from the pager into a MemPage used by
65321** the btree layer.
65322*/
65323static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
65324 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
65325 if( pgno!=pPage->pgno ){
65326 pPage->aData = sqlite3PagerGetData(pDbPage);
65327 pPage->pDbPage = pDbPage;
65328 pPage->pBt = pBt;
65329 pPage->pgno = pgno;
65330 pPage->hdrOffset = pgno==1 ? 100 : 0;
65331 }
65332 assert( pPage->aData==sqlite3PagerGetData(pDbPage) )((void) (0));
65333 return pPage;
65334}
65335
65336/*
65337** Get a page from the pager. Initialize the MemPage.pBt and
65338** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
65339**
65340** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
65341** about the content of the page at this time. So do not go to the disk
65342** to fetch the content. Just fill in the content with zeros for now.
65343** If in the future we call sqlite3PagerWrite() on this page, that
65344** means we have started to be concerned about content and the disk
65345** read should occur at that point.
65346*/
65347static int btreeGetPage(
65348 BtShared *pBt, /* The btree */
65349 Pgno pgno, /* Number of the page to fetch */
65350 MemPage **ppPage, /* Return the page in this parameter */
65351 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
65352){
65353 int rc;
65354 DbPage *pDbPage;
65355
65356 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY )((void) (0));
65357 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
65358 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
65359 if( rc ) return rc;
65360 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
65361 return SQLITE_OK0;
65362}
65363
65364/*
65365** Retrieve a page from the pager cache. If the requested page is not
65366** already in the pager cache return NULL. Initialize the MemPage.pBt and
65367** MemPage.aData elements if needed.
65368*/
65369static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
65370 DbPage *pDbPage;
65371 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
65372 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
65373 if( pDbPage ){
65374 return btreePageFromDbPage(pDbPage, pgno, pBt);
65375 }
65376 return 0;
65377}
65378
65379/*
65380** Return the size of the database file in pages. If there is any kind of
65381** error, return ((unsigned int)-1).
65382*/
65383static Pgno btreePagecount(BtShared *pBt){
65384 return pBt->nPage;
65385}
65386SQLITE_PRIVATEstatic u32 sqlite3BtreeLastPage(Btree *p){
65387 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
65388 assert( ((p->pBt->nPage)&0x80000000)==0 )((void) (0));
65389 return btreePagecount(p->pBt);
65390}
65391
65392/*
65393** Get a page from the pager and initialize it.
65394**
65395** If pCur!=0 then the page is being fetched as part of a moveToChild()
65396** call. Do additional sanity checking on the page in this case.
65397** And if the fetch fails, this routine must decrement pCur->iPage.
65398**
65399** The page is fetched as read-write unless pCur is not NULL and is
65400** a read-only cursor.
65401**
65402** If an error occurs, then *ppPage is undefined. It
65403** may remain unchanged, or it may be set to an invalid value.
65404*/
65405static int getAndInitPage(
65406 BtShared *pBt, /* The database file */
65407 Pgno pgno, /* Number of the page to get */
65408 MemPage **ppPage, /* Write the page pointer here */
65409 BtCursor *pCur, /* Cursor to receive the page, or NULL */
65410 int bReadOnly /* True for a read-only page */
65411){
65412 int rc;
65413 DbPage *pDbPage;
65414 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
65415 assert( pCur==0 || ppPage==&pCur->pPage )((void) (0));
65416 assert( pCur==0 || bReadOnly==pCur->curPagerFlags )((void) (0));
65417 assert( pCur==0 || pCur->iPage>0 )((void) (0));
65418
65419 if( pgno>btreePagecount(pBt) ){
65420 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(65420);
65421 goto getAndInitPage_error1;
65422 }
65423 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
65424 if( rc ){
65425 goto getAndInitPage_error1;
65426 }
65427 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
65428 if( (*ppPage)->isInit==0 ){
65429 btreePageFromDbPage(pDbPage, pgno, pBt);
65430 rc = btreeInitPage(*ppPage);
65431 if( rc!=SQLITE_OK0 ){
65432 goto getAndInitPage_error2;
65433 }
65434 }
65435 assert( (*ppPage)->pgno==pgno )((void) (0));
65436 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) )((void) (0));
65437
65438 /* If obtaining a child page for a cursor, we must verify that the page is
65439 ** compatible with the root page. */
65440 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
65441 rc = SQLITE_CORRUPT_PGNO(pgno)sqlite3CorruptError(65441);
65442 goto getAndInitPage_error2;
65443 }
65444 return SQLITE_OK0;
65445
65446getAndInitPage_error2:
65447 releasePage(*ppPage);
65448getAndInitPage_error1:
65449 if( pCur ){
65450 pCur->iPage--;
65451 pCur->pPage = pCur->apPage[pCur->iPage];
65452 }
65453 testcase( pgno==0 );
65454 assert( pgno!=0 || rc==SQLITE_CORRUPT )((void) (0));
65455 return rc;
65456}
65457
65458/*
65459** Release a MemPage. This should be called once for each prior
65460** call to btreeGetPage.
65461**
65462** Page1 is a special case and must be released using releasePageOne().
65463*/
65464static void releasePageNotNull(MemPage *pPage){
65465 assert( pPage->aData )((void) (0));
65466 assert( pPage->pBt )((void) (0));
65467 assert( pPage->pDbPage!=0 )((void) (0));
65468 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
65469 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData )((void) (0));
65470 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
65471 sqlite3PagerUnrefNotNull(pPage->pDbPage);
65472}
65473static void releasePage(MemPage *pPage){
65474 if( pPage ) releasePageNotNull(pPage);
65475}
65476static void releasePageOne(MemPage *pPage){
65477 assert( pPage!=0 )((void) (0));
65478 assert( pPage->aData )((void) (0));
65479 assert( pPage->pBt )((void) (0));
65480 assert( pPage->pDbPage!=0 )((void) (0));
65481 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage )((void) (0));
65482 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData )((void) (0));
65483 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
65484 sqlite3PagerUnrefPageOne(pPage->pDbPage);
65485}
65486
65487/*
65488** Get an unused page.
65489**
65490** This works just like btreeGetPage() with the addition:
65491**
65492** * If the page is already in use for some other purpose, immediately
65493** release it and return an SQLITE_CURRUPT error.
65494** * Make sure the isInit flag is clear
65495*/
65496static int btreeGetUnusedPage(
65497 BtShared *pBt, /* The btree */
65498 Pgno pgno, /* Number of the page to fetch */
65499 MemPage **ppPage, /* Return the page in this parameter */
65500 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
65501){
65502 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
65503 if( rc==SQLITE_OK0 ){
65504 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
65505 releasePage(*ppPage);
65506 *ppPage = 0;
65507 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(65507);
65508 }
65509 (*ppPage)->isInit = 0;
65510 }else{
65511 *ppPage = 0;
65512 }
65513 return rc;
65514}
65515
65516
65517/*
65518** During a rollback, when the pager reloads information into the cache
65519** so that the cache is restored to its original state at the start of
65520** the transaction, for each page restored this routine is called.
65521**
65522** This routine needs to reset the extra data section at the end of the
65523** page to agree with the restored data.
65524*/
65525static void pageReinit(DbPage *pData){
65526 MemPage *pPage;
65527 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
65528 assert( sqlite3PagerPageRefcount(pData)>0 )((void) (0));
65529 if( pPage->isInit ){
65530 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
65531 pPage->isInit = 0;
65532 if( sqlite3PagerPageRefcount(pData)>1 ){
65533 /* pPage might not be a btree page; it might be an overflow page
65534 ** or ptrmap page or a free page. In those cases, the following
65535 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
65536 ** But no harm is done by this. And it is very important that
65537 ** btreeInitPage() be called on every btree page so we make
65538 ** the call for every page that comes in for re-initing. */
65539 btreeInitPage(pPage);
65540 }
65541 }
65542}
65543
65544/*
65545** Invoke the busy handler for a btree.
65546*/
65547static int btreeInvokeBusyHandler(void *pArg){
65548 BtShared *pBt = (BtShared*)pArg;
65549 assert( pBt->db )((void) (0));
65550 assert( sqlite3_mutex_held(pBt->db->mutex) )((void) (0));
65551 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
65552 sqlite3PagerFile(pBt->pPager));
65553}
65554
65555/*
65556** Open a database file.
65557**
65558** zFilename is the name of the database file. If zFilename is NULL
65559** then an ephemeral database is created. The ephemeral database might
65560** be exclusively in memory, or it might use a disk-based memory cache.
65561** Either way, the ephemeral database will be automatically deleted
65562** when sqlite3BtreeClose() is called.
65563**
65564** If zFilename is ":memory:" then an in-memory database is created
65565** that is automatically destroyed when it is closed.
65566**
65567** The "flags" parameter is a bitmask that might contain bits like
65568** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
65569**
65570** If the database is already opened in the same database connection
65571** and we are in shared cache mode, then the open will fail with an
65572** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
65573** objects in the same database connection since doing so will lead
65574** to problems with locking.
65575*/
65576SQLITE_PRIVATEstatic int sqlite3BtreeOpen(
65577 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
65578 const char *zFilename, /* Name of the file containing the BTree database */
65579 sqlite3 *db, /* Associated database handle */
65580 Btree **ppBtree, /* Pointer to new Btree object written here */
65581 int flags, /* Options */
65582 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
65583){
65584 BtShared *pBt = 0; /* Shared part of btree structure */
65585 Btree *p; /* Handle to return */
65586 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
65587 int rc = SQLITE_OK0; /* Result code from this function */
65588 u8 nReserve; /* Byte of unused space on each page */
65589 unsigned char zDbHeader[100]; /* Database header content */
65590
65591 /* True if opening an ephemeral, temporary database */
65592 const int isTempDb = zFilename==0 || zFilename[0]==0;
65593
65594 /* Set the variable isMemdb to true for an in-memory database, or
65595 ** false for a file-based database.
65596 */
65597#ifdef SQLITE_OMIT_MEMORYDB
65598 const int isMemdb = 0;
65599#else
65600 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
65601 || (isTempDb && sqlite3TempInMemory(db))
65602 || (vfsFlags & SQLITE_OPEN_MEMORY0x00000080)!=0;
65603#endif
65604
65605 assert( db!=0 )((void) (0));
65606 assert( pVfs!=0 )((void) (0));
65607 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
65608 assert( (flags&0xff)==flags )((void) (0)); /* flags fit in 8 bits */
65609
65610 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
65611 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 )((void) (0));
65612
65613 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
65614 assert( (flags & BTREE_SINGLE)==0 || isTempDb )((void) (0));
65615
65616 if( isMemdb ){
65617 flags |= BTREE_MEMORY2;
65618 }
65619 if( (vfsFlags & SQLITE_OPEN_MAIN_DB0x00000100)!=0 && (isMemdb || isTempDb) ){
65620 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB0x00000100) | SQLITE_OPEN_TEMP_DB0x00000200;
65621 }
65622 p = sqlite3MallocZero(sizeof(Btree));
65623 if( !p ){
65624 return SQLITE_NOMEM_BKPT7;
65625 }
65626 p->inTrans = TRANS_NONE0;
65627 p->db = db;
65628#ifndef SQLITE_OMIT_SHARED_CACHE
65629 p->lock.pBtree = p;
65630 p->lock.iTable = 1;
65631#endif
65632
65633#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
65634 /*
65635 ** If this Btree is a candidate for shared cache, try to find an
65636 ** existing BtShared object that we can share with
65637 */
65638 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI0x00000040)!=0) ){
65639 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE0x00020000 ){
65640 int nFilename = sqlite3Strlen30(zFilename)+1;
65641 int nFullPathname = pVfs->mxPathname+1;
65642 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename)((nFullPathname)>(nFilename)?(nFullPathname):(nFilename)));
65643 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )sqlite3_mutex *mutexShared;
65644
65645 p->sharable = 1;
65646 if( !zFullPathname ){
65647 sqlite3_free(p);
65648 return SQLITE_NOMEM_BKPT7;
65649 }
65650 if( isMemdb ){
65651 memcpy(zFullPathname, zFilename, nFilename);
65652 }else{
65653 rc = sqlite3OsFullPathname(pVfs, zFilename,
65654 nFullPathname, zFullPathname);
65655 if( rc ){
65656 sqlite3_free(zFullPathname);
65657 sqlite3_free(p);
65658 return rc;
65659 }
65660 }
65661#if SQLITE_THREADSAFE1
65662 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN4);
65663 sqlite3_mutex_enter(mutexOpen);
65664 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2);
65665 sqlite3_mutex_enter(mutexShared);
65666#endif
65667 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
65668 assert( pBt->nRef>0 )((void) (0));
65669 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
65670 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
65671 int iDb;
65672 for(iDb=db->nDb-1; iDb>=0; iDb--){
65673 Btree *pExisting = db->aDb[iDb].pBt;
65674 if( pExisting && pExisting->pBt==pBt ){
65675 sqlite3_mutex_leave(mutexShared);
65676 sqlite3_mutex_leave(mutexOpen);
65677 sqlite3_free(zFullPathname);
65678 sqlite3_free(p);
65679 return SQLITE_CONSTRAINT19;
65680 }
65681 }
65682 p->pBt = pBt;
65683 pBt->nRef++;
65684 break;
65685 }
65686 }
65687 sqlite3_mutex_leave(mutexShared);
65688 sqlite3_free(zFullPathname);
65689 }
65690#ifdef SQLITE_DEBUG
65691 else{
65692 /* In debug mode, we mark all persistent databases as sharable
65693 ** even when they are not. This exercises the locking code and
65694 ** gives more opportunity for asserts(sqlite3_mutex_held())
65695 ** statements to find locking problems.
65696 */
65697 p->sharable = 1;
65698 }
65699#endif
65700 }
65701#endif
65702 if( pBt==0 ){
65703 /*
65704 ** The following asserts make sure that structures used by the btree are
65705 ** the right size. This is to guard against size changes that result
65706 ** when compiling on a different architecture.
65707 */
65708 assert( sizeof(i64)==8 )((void) (0));
65709 assert( sizeof(u64)==8 )((void) (0));
65710 assert( sizeof(u32)==4 )((void) (0));
65711 assert( sizeof(u16)==2 )((void) (0));
65712 assert( sizeof(Pgno)==4 )((void) (0));
65713
65714 pBt = sqlite3MallocZero( sizeof(*pBt) );
65715 if( pBt==0 ){
65716 rc = SQLITE_NOMEM_BKPT7;
65717 goto btree_open_out;
65718 }
65719 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
65720 sizeof(MemPage), flags, vfsFlags, pageReinit);
65721 if( rc==SQLITE_OK0 ){
65722 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
65723 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
65724 }
65725 if( rc!=SQLITE_OK0 ){
65726 goto btree_open_out;
65727 }
65728 pBt->openFlags = (u8)flags;
65729 pBt->db = db;
65730 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
65731 p->pBt = pBt;
65732
65733 pBt->pCursor = 0;
65734 pBt->pPage1 = 0;
65735 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY0x0001;
65736#if defined(SQLITE_SECURE_DELETE)
65737 pBt->btsFlags |= BTS_SECURE_DELETE0x0004;
65738#elif defined(SQLITE_FAST_SECURE_DELETE)
65739 pBt->btsFlags |= BTS_OVERWRITE0x0008;
65740#endif
65741 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
65742 ** determined by the 2-byte integer located at an offset of 16 bytes from
65743 ** the beginning of the database file. */
65744 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
65745 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE65536
65746 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
65747 pBt->pageSize = 0;
65748#ifndef SQLITE_OMIT_AUTOVACUUM
65749 /* If the magic name ":memory:" will create an in-memory database, then
65750 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
65751 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
65752 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
65753 ** regular file-name. In this case the auto-vacuum applies as per normal.
65754 */
65755 if( zFilename && !isMemdb ){
65756 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM0 ? 1 : 0);
65757 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM0==2 ? 1 : 0);
65758 }
65759#endif
65760 nReserve = 0;
65761 }else{
65762 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
65763 ** determined by the one-byte unsigned integer found at an offset of 20
65764 ** into the database file header. */
65765 nReserve = zDbHeader[20];
65766 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
65767#ifndef SQLITE_OMIT_AUTOVACUUM
65768 pBt->autoVacuum = (get4bytesqlite3Get4byte(&zDbHeader[36 + 4*4])?1:0);
65769 pBt->incrVacuum = (get4bytesqlite3Get4byte(&zDbHeader[36 + 7*4])?1:0);
65770#endif
65771 }
65772 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
65773 if( rc ) goto btree_open_out;
65774 pBt->usableSize = pBt->pageSize - nReserve;
65775 assert( (pBt->pageSize & 7)==0 )((void) (0)); /* 8-byte alignment of pageSize */
65776
65777#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
65778 /* Add the new BtShared object to the linked list sharable BtShareds.
65779 */
65780 pBt->nRef = 1;
65781 if( p->sharable ){
65782 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )sqlite3_mutex *mutexShared;
65783 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)mutexShared = sqlite3MutexAlloc(2);
65784 if( SQLITE_THREADSAFE1 && sqlite3GlobalConfigsqlite3Config.bCoreMutex ){
65785 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST0);
65786 if( pBt->mutex==0 ){
65787 rc = SQLITE_NOMEM_BKPT7;
65788 goto btree_open_out;
65789 }
65790 }
65791 sqlite3_mutex_enter(mutexShared);
65792 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList;
65793 GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList = pBt;
65794 sqlite3_mutex_leave(mutexShared);
65795 }
65796#endif
65797 }
65798
65799#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
65800 /* If the new Btree uses a sharable pBtShared, then link the new
65801 ** Btree into the list of all sharable Btrees for the same connection.
65802 ** The list is kept in ascending order by pBt address.
65803 */
65804 if( p->sharable ){
65805 int i;
65806 Btree *pSib;
65807 for(i=0; i<db->nDb; i++){
65808 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
65809 while( pSib->pPrev ){ pSib = pSib->pPrev; }
65810 if( (uptr)p->pBt<(uptr)pSib->pBt ){
65811 p->pNext = pSib;
65812 p->pPrev = 0;
65813 pSib->pPrev = p;
65814 }else{
65815 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
65816 pSib = pSib->pNext;
65817 }
65818 p->pNext = pSib->pNext;
65819 p->pPrev = pSib;
65820 if( p->pNext ){
65821 p->pNext->pPrev = p;
65822 }
65823 pSib->pNext = p;
65824 }
65825 break;
65826 }
65827 }
65828 }
65829#endif
65830 *ppBtree = p;
65831
65832btree_open_out:
65833 if( rc!=SQLITE_OK0 ){
65834 if( pBt && pBt->pPager ){
65835 sqlite3PagerClose(pBt->pPager, 0);
65836 }
65837 sqlite3_free(pBt);
65838 sqlite3_free(p);
65839 *ppBtree = 0;
65840 }else{
65841 sqlite3_file *pFile;
65842
65843 /* If the B-Tree was successfully opened, set the pager-cache size to the
65844 ** default value. Except, when opening on an existing shared pager-cache,
65845 ** do not change the pager-cache size.
65846 */
65847 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
65848 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE-2000);
65849 }
65850
65851 pFile = sqlite3PagerFile(pBt->pPager);
65852 if( pFile->pMethods ){
65853 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB30, (void*)&pBt->db);
65854 }
65855 }
65856 if( mutexOpen ){
65857 assert( sqlite3_mutex_held(mutexOpen) )((void) (0));
65858 sqlite3_mutex_leave(mutexOpen);
65859 }
65860 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 )((void) (0));
65861 return rc;
65862}
65863
65864/*
65865** Decrement the BtShared.nRef counter. When it reaches zero,
65866** remove the BtShared structure from the sharing list. Return
65867** true if the BtShared.nRef counter reaches zero and return
65868** false if it is still positive.
65869*/
65870static int removeFromSharingList(BtShared *pBt){
65871#ifndef SQLITE_OMIT_SHARED_CACHE
65872 MUTEX_LOGIC( sqlite3_mutex *pMaster; )sqlite3_mutex *pMaster;
65873 BtShared *pList;
65874 int removed = 0;
65875
65876 assert( sqlite3_mutex_notheld(pBt->mutex) )((void) (0));
65877 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )pMaster = sqlite3MutexAlloc(2);
65878 sqlite3_mutex_enter(pMaster);
65879 pBt->nRef--;
65880 if( pBt->nRef<=0 ){
65881 if( GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList==pBt ){
65882 GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList = pBt->pNext;
65883 }else{
65884 pList = GLOBAL(BtShared*,sqlite3SharedCacheList)sqlite3SharedCacheList;
65885 while( ALWAYS(pList)(pList) && pList->pNext!=pBt ){
65886 pList=pList->pNext;
65887 }
65888 if( ALWAYS(pList)(pList) ){
65889 pList->pNext = pBt->pNext;
65890 }
65891 }
65892 if( SQLITE_THREADSAFE1 ){
65893 sqlite3_mutex_free(pBt->mutex);
65894 }
65895 removed = 1;
65896 }
65897 sqlite3_mutex_leave(pMaster);
65898 return removed;
65899#else
65900 return 1;
65901#endif
65902}
65903
65904/*
65905** Make sure pBt->pTmpSpace points to an allocation of
65906** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
65907** pointer.
65908*/
65909static void allocateTempSpace(BtShared *pBt){
65910 if( !pBt->pTmpSpace ){
65911 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
65912
65913 /* One of the uses of pBt->pTmpSpace is to format cells before
65914 ** inserting them into a leaf page (function fillInCell()). If
65915 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
65916 ** by the various routines that manipulate binary cells. Which
65917 ** can mean that fillInCell() only initializes the first 2 or 3
65918 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
65919 ** it into a database page. This is not actually a problem, but it
65920 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
65921 ** data is passed to system call write(). So to avoid this error,
65922 ** zero the first 4 bytes of temp space here.
65923 **
65924 ** Also: Provide four bytes of initialized space before the
65925 ** beginning of pTmpSpace as an area available to prepend the
65926 ** left-child pointer to the beginning of a cell.
65927 */
65928 if( pBt->pTmpSpace ){
65929 memset(pBt->pTmpSpace, 0, 8);
65930 pBt->pTmpSpace += 4;
65931 }
65932 }
65933}
65934
65935/*
65936** Free the pBt->pTmpSpace allocation
65937*/
65938static void freeTempSpace(BtShared *pBt){
65939 if( pBt->pTmpSpace ){
65940 pBt->pTmpSpace -= 4;
65941 sqlite3PageFree(pBt->pTmpSpace);
65942 pBt->pTmpSpace = 0;
65943 }
65944}
65945
65946/*
65947** Close an open database and invalidate all cursors.
65948*/
65949SQLITE_PRIVATEstatic int sqlite3BtreeClose(Btree *p){
65950 BtShared *pBt = p->pBt;
65951 BtCursor *pCur;
65952
65953 /* Close all cursors opened via this handle. */
65954 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
65955 sqlite3BtreeEnter(p);
65956 pCur = pBt->pCursor;
65957 while( pCur ){
65958 BtCursor *pTmp = pCur;
65959 pCur = pCur->pNext;
65960 if( pTmp->pBtree==p ){
65961 sqlite3BtreeCloseCursor(pTmp);
65962 }
65963 }
65964
65965 /* Rollback any active transaction and free the handle structure.
65966 ** The call to sqlite3BtreeRollback() drops any table-locks held by
65967 ** this handle.
65968 */
65969 sqlite3BtreeRollback(p, SQLITE_OK0, 0);
65970 sqlite3BtreeLeave(p);
65971
65972 /* If there are still other outstanding references to the shared-btree
65973 ** structure, return now. The remainder of this procedure cleans
65974 ** up the shared-btree.
65975 */
65976 assert( p->wantToLock==0 && p->locked==0 )((void) (0));
65977 if( !p->sharable || removeFromSharingList(pBt) ){
65978 /* The pBt is no longer on the sharing list, so we can access
65979 ** it without having to hold the mutex.
65980 **
65981 ** Clean out and delete the BtShared object.
65982 */
65983 assert( !pBt->pCursor )((void) (0));
65984 sqlite3PagerClose(pBt->pPager, p->db);
65985 if( pBt->xFreeSchema && pBt->pSchema ){
65986 pBt->xFreeSchema(pBt->pSchema);
65987 }
65988 sqlite3DbFree(0, pBt->pSchema);
65989 freeTempSpace(pBt);
65990 sqlite3_free(pBt);
65991 }
65992
65993#ifndef SQLITE_OMIT_SHARED_CACHE
65994 assert( p->wantToLock==0 )((void) (0));
65995 assert( p->locked==0 )((void) (0));
65996 if( p->pPrev ) p->pPrev->pNext = p->pNext;
65997 if( p->pNext ) p->pNext->pPrev = p->pPrev;
65998#endif
65999
66000 sqlite3_free(p);
66001 return SQLITE_OK0;
66002}
66003
66004/*
66005** Change the "soft" limit on the number of pages in the cache.
66006** Unused and unmodified pages will be recycled when the number of
66007** pages in the cache exceeds this soft limit. But the size of the
66008** cache is allowed to grow larger than this limit if it contains
66009** dirty pages or pages still in active use.
66010*/
66011SQLITE_PRIVATEstatic int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
66012 BtShared *pBt = p->pBt;
66013 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
66014 sqlite3BtreeEnter(p);
66015 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
66016 sqlite3BtreeLeave(p);
66017 return SQLITE_OK0;
66018}
66019
66020/*
66021** Change the "spill" limit on the number of pages in the cache.
66022** If the number of pages exceeds this limit during a write transaction,
66023** the pager might attempt to "spill" pages to the journal early in
66024** order to free up memory.
66025**
66026** The value returned is the current spill size. If zero is passed
66027** as an argument, no changes are made to the spill size setting, so
66028** using mxPage of 0 is a way to query the current spill size.
66029*/
66030SQLITE_PRIVATEstatic int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
66031 BtShared *pBt = p->pBt;
66032 int res;
66033 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
66034 sqlite3BtreeEnter(p);
66035 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
66036 sqlite3BtreeLeave(p);
66037 return res;
66038}
66039
66040#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
66041/*
66042** Change the limit on the amount of the database file that may be
66043** memory mapped.
66044*/
66045SQLITE_PRIVATEstatic int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
66046 BtShared *pBt = p->pBt;
66047 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
66048 sqlite3BtreeEnter(p);
66049 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
66050 sqlite3BtreeLeave(p);
66051 return SQLITE_OK0;
66052}
66053#endif /* SQLITE_MAX_MMAP_SIZE>0 */
66054
66055/*
66056** Change the way data is synced to disk in order to increase or decrease
66057** how well the database resists damage due to OS crashes and power
66058** failures. Level 1 is the same as asynchronous (no syncs() occur and
66059** there is a high probability of damage) Level 2 is the default. There
66060** is a very low but non-zero probability of damage. Level 3 reduces the
66061** probability of damage to near zero but with a write performance reduction.
66062*/
66063#ifndef SQLITE_OMIT_PAGER_PRAGMAS
66064SQLITE_PRIVATEstatic int sqlite3BtreeSetPagerFlags(
66065 Btree *p, /* The btree to set the safety level on */
66066 unsigned pgFlags /* Various PAGER_* flags */
66067){
66068 BtShared *pBt = p->pBt;
66069 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
66070 sqlite3BtreeEnter(p);
66071 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
66072 sqlite3BtreeLeave(p);
66073 return SQLITE_OK0;
66074}
66075#endif
66076
66077/*
66078** Change the default pages size and the number of reserved bytes per page.
66079** Or, if the page size has already been fixed, return SQLITE_READONLY
66080** without changing anything.
66081**
66082** The page size must be a power of 2 between 512 and 65536. If the page
66083** size supplied does not meet this constraint then the page size is not
66084** changed.
66085**
66086** Page sizes are constrained to be a power of two so that the region
66087** of the database file used for locking (beginning at PENDING_BYTE,
66088** the first byte past the 1GB boundary, 0x40000000) needs to occur
66089** at the beginning of a page.
66090**
66091** If parameter nReserve is less than zero, then the number of reserved
66092** bytes per page is left unchanged.
66093**
66094** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
66095** and autovacuum mode can no longer be changed.
66096*/
66097SQLITE_PRIVATEstatic int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
66098 int rc = SQLITE_OK0;
66099 BtShared *pBt = p->pBt;
66100 assert( nReserve>=-1 && nReserve<=255 )((void) (0));
66101 sqlite3BtreeEnter(p);
66102#if SQLITE_HAS_CODEC
66103 if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
66104#endif
66105 if( pBt->btsFlags & BTS_PAGESIZE_FIXED0x0002 ){
66106 sqlite3BtreeLeave(p);
66107 return SQLITE_READONLY8;
66108 }
66109 if( nReserve<0 ){
66110 nReserve = pBt->pageSize - pBt->usableSize;
66111 }
66112 assert( nReserve>=0 && nReserve<=255 )((void) (0));
66113 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE65536 &&
66114 ((pageSize-1)&pageSize)==0 ){
66115 assert( (pageSize & 7)==0 )((void) (0));
66116 assert( !pBt->pCursor )((void) (0));
66117 pBt->pageSize = (u32)pageSize;
66118 freeTempSpace(pBt);
66119 }
66120 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
66121 pBt->usableSize = pBt->pageSize - (u16)nReserve;
66122 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
66123 sqlite3BtreeLeave(p);
66124 return rc;
66125}
66126
66127/*
66128** Return the currently defined page size
66129*/
66130SQLITE_PRIVATEstatic int sqlite3BtreeGetPageSize(Btree *p){
66131 return p->pBt->pageSize;
66132}
66133
66134/*
66135** This function is similar to sqlite3BtreeGetReserve(), except that it
66136** may only be called if it is guaranteed that the b-tree mutex is already
66137** held.
66138**
66139** This is useful in one special case in the backup API code where it is
66140** known that the shared b-tree mutex is held, but the mutex on the
66141** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
66142** were to be called, it might collide with some other operation on the
66143** database handle that owns *p, causing undefined behavior.
66144*/
66145SQLITE_PRIVATEstatic int sqlite3BtreeGetReserveNoMutex(Btree *p){
66146 int n;
66147 assert( sqlite3_mutex_held(p->pBt->mutex) )((void) (0));
66148 n = p->pBt->pageSize - p->pBt->usableSize;
66149 return n;
66150}
66151
66152/*
66153** Return the number of bytes of space at the end of every page that
66154** are intentually left unused. This is the "reserved" space that is
66155** sometimes used by extensions.
66156**
66157** If SQLITE_HAS_MUTEX is defined then the number returned is the
66158** greater of the current reserved space and the maximum requested
66159** reserve space.
66160*/
66161SQLITE_PRIVATEstatic int sqlite3BtreeGetOptimalReserve(Btree *p){
66162 int n;
66163 sqlite3BtreeEnter(p);
66164 n = sqlite3BtreeGetReserveNoMutex(p);
66165#ifdef SQLITE_HAS_CODEC
66166 if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
66167#endif
66168 sqlite3BtreeLeave(p);
66169 return n;
66170}
66171
66172
66173/*
66174** Set the maximum page count for a database if mxPage is positive.
66175** No changes are made if mxPage is 0 or negative.
66176** Regardless of the value of mxPage, return the maximum page count.
66177*/
66178SQLITE_PRIVATEstatic int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
66179 int n;
66180 sqlite3BtreeEnter(p);
66181 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
66182 sqlite3BtreeLeave(p);
66183 return n;
66184}
66185
66186/*
66187** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
66188**
66189** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
66190** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
66191** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
66192** newFlag==(-1) No changes
66193**
66194** This routine acts as a query if newFlag is less than zero
66195**
66196** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
66197** freelist leaf pages are not written back to the database. Thus in-page
66198** deleted content is cleared, but freelist deleted content is not.
66199**
66200** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
66201** that freelist leaf pages are written back into the database, increasing
66202** the amount of disk I/O.
66203*/
66204SQLITE_PRIVATEstatic int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
66205 int b;
66206 if( p==0 ) return 0;
66207 sqlite3BtreeEnter(p);
66208 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 )((void) (0));
66209 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) )((void) (0));
66210 if( newFlag>=0 ){
66211 p->pBt->btsFlags &= ~BTS_FAST_SECURE0x000c;
66212 p->pBt->btsFlags |= BTS_SECURE_DELETE0x0004*newFlag;
66213 }
66214 b = (p->pBt->btsFlags & BTS_FAST_SECURE0x000c)/BTS_SECURE_DELETE0x0004;
66215 sqlite3BtreeLeave(p);
66216 return b;
66217}
66218
66219/*
66220** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
66221** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
66222** is disabled. The default value for the auto-vacuum property is
66223** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
66224*/
66225SQLITE_PRIVATEstatic int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
66226#ifdef SQLITE_OMIT_AUTOVACUUM
66227 return SQLITE_READONLY8;
66228#else
66229 BtShared *pBt = p->pBt;
66230 int rc = SQLITE_OK0;
66231 u8 av = (u8)autoVacuum;
66232
66233 sqlite3BtreeEnter(p);
66234 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED0x0002)!=0 && (av ?1:0)!=pBt->autoVacuum ){
66235 rc = SQLITE_READONLY8;
66236 }else{
66237 pBt->autoVacuum = av ?1:0;
66238 pBt->incrVacuum = av==2 ?1:0;
66239 }
66240 sqlite3BtreeLeave(p);
66241 return rc;
66242#endif
66243}
66244
66245/*
66246** Return the value of the 'auto-vacuum' property. If auto-vacuum is
66247** enabled 1 is returned. Otherwise 0.
66248*/
66249SQLITE_PRIVATEstatic int sqlite3BtreeGetAutoVacuum(Btree *p){
66250#ifdef SQLITE_OMIT_AUTOVACUUM
66251 return BTREE_AUTOVACUUM_NONE0;
66252#else
66253 int rc;
66254 sqlite3BtreeEnter(p);
66255 rc = (
66256 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE0:
66257 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL1:
66258 BTREE_AUTOVACUUM_INCR2
66259 );
66260 sqlite3BtreeLeave(p);
66261 return rc;
66262#endif
66263}
66264
66265/*
66266** If the user has not set the safety-level for this database connection
66267** using "PRAGMA synchronous", and if the safety-level is not already
66268** set to the value passed to this function as the second parameter,
66269** set it so.
66270*/
66271#if SQLITE_DEFAULT_SYNCHRONOUS2!=SQLITE_DEFAULT_WAL_SYNCHRONOUS2 \
66272 && !defined(SQLITE_OMIT_WAL)
66273static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
66274 sqlite3 *db;
66275 Db *pDb;
66276 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
66277 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
66278 if( pDb->bSyncSet==0
66279 && pDb->safety_level!=safety_level
66280 && pDb!=&db->aDb[1]
66281 ){
66282 pDb->safety_level = safety_level;
66283 sqlite3PagerSetFlags(pBt->pPager,
66284 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK0x38));
66285 }
66286 }
66287}
66288#else
66289# define setDefaultSyncFlag(pBt,safety_level)
66290#endif
66291
66292/* Forward declaration */
66293static int newDatabase(BtShared*);
66294
66295
66296/*
66297** Get a reference to pPage1 of the database file. This will
66298** also acquire a readlock on that file.
66299**
66300** SQLITE_OK is returned on success. If the file is not a
66301** well-formed database file, then SQLITE_CORRUPT is returned.
66302** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
66303** is returned if we run out of memory.
66304*/
66305static int lockBtree(BtShared *pBt){
66306 int rc; /* Result code from subfunctions */
66307 MemPage *pPage1; /* Page 1 of the database file */
66308 u32 nPage; /* Number of pages in the database */
66309 u32 nPageFile = 0; /* Number of pages in the database file */
66310 u32 nPageHeader; /* Number of pages in the database according to hdr */
66311
66312 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
66313 assert( pBt->pPage1==0 )((void) (0));
66314 rc = sqlite3PagerSharedLock(pBt->pPager);
66315 if( rc!=SQLITE_OK0 ) return rc;
66316 rc = btreeGetPage(pBt, 1, &pPage1, 0);
66317 if( rc!=SQLITE_OK0 ) return rc;
66318
66319 /* Do some checking to help insure the file we opened really is
66320 ** a valid database file.
66321 */
66322 nPage = nPageHeader = get4bytesqlite3Get4byte(28+(u8*)pPage1->aData);
66323 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
66324 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
66325 nPage = nPageFile;
66326 }
66327 if( (pBt->db->flags & SQLITE_ResetDatabase0x02000000)!=0 ){
66328 nPage = 0;
66329 }
66330 if( nPage>0 ){
66331 u32 pageSize;
66332 u32 usableSize;
66333 u8 *page1 = pPage1->aData;
66334 rc = SQLITE_NOTADB26;
66335 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
66336 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
66337 ** 61 74 20 33 00. */
66338 if( memcmp(page1, zMagicHeader, 16)!=0 ){
66339 goto page1_init_failed;
66340 }
66341
66342#ifdef SQLITE_OMIT_WAL
66343 if( page1[18]>1 ){
66344 pBt->btsFlags |= BTS_READ_ONLY0x0001;
66345 }
66346 if( page1[19]>1 ){
66347 goto page1_init_failed;
66348 }
66349#else
66350 if( page1[18]>2 ){
66351 pBt->btsFlags |= BTS_READ_ONLY0x0001;
66352 }
66353 if( page1[19]>2 ){
66354 goto page1_init_failed;
66355 }
66356
66357 /* If the write version is set to 2, this database should be accessed
66358 ** in WAL mode. If the log is not already open, open it now. Then
66359 ** return SQLITE_OK and return without populating BtShared.pPage1.
66360 ** The caller detects this and calls this function again. This is
66361 ** required as the version of page 1 currently in the page1 buffer
66362 ** may not be the latest version - there may be a newer one in the log
66363 ** file.
66364 */
66365 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL0x0020)==0 ){
66366 int isOpen = 0;
66367 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
66368 if( rc!=SQLITE_OK0 ){
66369 goto page1_init_failed;
66370 }else{
66371 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
66372 if( isOpen==0 ){
66373 releasePageOne(pPage1);
66374 return SQLITE_OK0;
66375 }
66376 }
66377 rc = SQLITE_NOTADB26;
66378 }else{
66379 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
66380 }
66381#endif
66382
66383 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
66384 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
66385 **
66386 ** The original design allowed these amounts to vary, but as of
66387 ** version 3.6.0, we require them to be fixed.
66388 */
66389 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
66390 goto page1_init_failed;
66391 }
66392 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
66393 ** determined by the 2-byte integer located at an offset of 16 bytes from
66394 ** the beginning of the database file. */
66395 pageSize = (page1[16]<<8) | (page1[17]<<16);
66396 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
66397 ** between 512 and 65536 inclusive. */
66398 if( ((pageSize-1)&pageSize)!=0
66399 || pageSize>SQLITE_MAX_PAGE_SIZE65536
66400 || pageSize<=256
66401 ){
66402 goto page1_init_failed;
66403 }
66404 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
66405 assert( (pageSize & 7)==0 )((void) (0));
66406 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
66407 ** integer at offset 20 is the number of bytes of space at the end of
66408 ** each page to reserve for extensions.
66409 **
66410 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
66411 ** determined by the one-byte unsigned integer found at an offset of 20
66412 ** into the database file header. */
66413 usableSize = pageSize - page1[20];
66414 if( (u32)pageSize!=pBt->pageSize ){
66415 /* After reading the first page of the database assuming a page size
66416 ** of BtShared.pageSize, we have discovered that the page-size is
66417 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
66418 ** zero and return SQLITE_OK. The caller will call this function
66419 ** again with the correct page-size.
66420 */
66421 releasePageOne(pPage1);
66422 pBt->usableSize = usableSize;
66423 pBt->pageSize = pageSize;
66424 freeTempSpace(pBt);
66425 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
66426 pageSize-usableSize);
66427 return rc;
66428 }
66429 if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
66430 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(66430);
66431 goto page1_init_failed;
66432 }
66433 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
66434 ** be less than 480. In other words, if the page size is 512, then the
66435 ** reserved space size cannot exceed 32. */
66436 if( usableSize<480 ){
66437 goto page1_init_failed;
66438 }
66439 pBt->pageSize = pageSize;
66440 pBt->usableSize = usableSize;
66441#ifndef SQLITE_OMIT_AUTOVACUUM
66442 pBt->autoVacuum = (get4bytesqlite3Get4byte(&page1[36 + 4*4])?1:0);
66443 pBt->incrVacuum = (get4bytesqlite3Get4byte(&page1[36 + 7*4])?1:0);
66444#endif
66445 }
66446
66447 /* maxLocal is the maximum amount of payload to store locally for
66448 ** a cell. Make sure it is small enough so that at least minFanout
66449 ** cells can will fit on one page. We assume a 10-byte page header.
66450 ** Besides the payload, the cell must store:
66451 ** 2-byte pointer to the cell
66452 ** 4-byte child pointer
66453 ** 9-byte nKey value
66454 ** 4-byte nData value
66455 ** 4-byte overflow page pointer
66456 ** So a cell consists of a 2-byte pointer, a header which is as much as
66457 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
66458 ** page pointer.
66459 */
66460 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
66461 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
66462 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
66463 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
66464 if( pBt->maxLocal>127 ){
66465 pBt->max1bytePayload = 127;
66466 }else{
66467 pBt->max1bytePayload = (u8)pBt->maxLocal;
66468 }
66469 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) )((void) (0));
66470 pBt->pPage1 = pPage1;
66471 pBt->nPage = nPage;
66472 return SQLITE_OK0;
66473
66474page1_init_failed:
66475 releasePageOne(pPage1);
66476 pBt->pPage1 = 0;
66477 return rc;
66478}
66479
66480#ifndef NDEBUG1
66481/*
66482** Return the number of cursors open on pBt. This is for use
66483** in assert() expressions, so it is only compiled if NDEBUG is not
66484** defined.
66485**
66486** Only write cursors are counted if wrOnly is true. If wrOnly is
66487** false then all cursors are counted.
66488**
66489** For the purposes of this routine, a cursor is any cursor that
66490** is capable of reading or writing to the database. Cursors that
66491** have been tripped into the CURSOR_FAULT state are not counted.
66492*/
66493static int countValidCursors(BtShared *pBt, int wrOnly){
66494 BtCursor *pCur;
66495 int r = 0;
66496 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
66497 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag0x01)!=0)
66498 && pCur->eState!=CURSOR_FAULT4 ) r++;
66499 }
66500 return r;
66501}
66502#endif
66503
66504/*
66505** If there are no outstanding cursors and we are not in the middle
66506** of a transaction but there is a read lock on the database, then
66507** this routine unrefs the first page of the database file which
66508** has the effect of releasing the read lock.
66509**
66510** If there is a transaction in progress, this routine is a no-op.
66511*/
66512static void unlockBtreeIfUnused(BtShared *pBt){
66513 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
66514 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE )((void) (0));
66515 if( pBt->inTransaction==TRANS_NONE0 && pBt->pPage1!=0 ){
66516 MemPage *pPage1 = pBt->pPage1;
66517 assert( pPage1->aData )((void) (0));
66518 assert( sqlite3PagerRefcount(pBt->pPager)==1 )((void) (0));
66519 pBt->pPage1 = 0;
66520 releasePageOne(pPage1);
66521 }
66522}
66523
66524/*
66525** If pBt points to an empty file then convert that empty file
66526** into a new empty database by initializing the first page of
66527** the database.
66528*/
66529static int newDatabase(BtShared *pBt){
66530 MemPage *pP1;
66531 unsigned char *data;
66532 int rc;
66533
66534 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
66535 if( pBt->nPage>0 ){
66536 return SQLITE_OK0;
66537 }
66538 pP1 = pBt->pPage1;
66539 assert( pP1!=0 )((void) (0));
66540 data = pP1->aData;
66541 rc = sqlite3PagerWrite(pP1->pDbPage);
66542 if( rc ) return rc;
66543 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
66544 assert( sizeof(zMagicHeader)==16 )((void) (0));
66545 data[16] = (u8)((pBt->pageSize>>8)&0xff);
66546 data[17] = (u8)((pBt->pageSize>>16)&0xff);
66547 data[18] = 1;
66548 data[19] = 1;
66549 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize)((void) (0));
66550 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
66551 data[21] = 64;
66552 data[22] = 32;
66553 data[23] = 32;
66554 memset(&data[24], 0, 100-24);
66555 zeroPage(pP1, PTF_INTKEY0x01|PTF_LEAF0x08|PTF_LEAFDATA0x04 );
66556 pBt->btsFlags |= BTS_PAGESIZE_FIXED0x0002;
66557#ifndef SQLITE_OMIT_AUTOVACUUM
66558 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 )((void) (0));
66559 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 )((void) (0));
66560 put4bytesqlite3Put4byte(&data[36 + 4*4], pBt->autoVacuum);
66561 put4bytesqlite3Put4byte(&data[36 + 7*4], pBt->incrVacuum);
66562#endif
66563 pBt->nPage = 1;
66564 data[31] = 1;
66565 return SQLITE_OK0;
66566}
66567
66568/*
66569** Initialize the first page of the database file (creating a database
66570** consisting of a single page and no schema objects). Return SQLITE_OK
66571** if successful, or an SQLite error code otherwise.
66572*/
66573SQLITE_PRIVATEstatic int sqlite3BtreeNewDb(Btree *p){
66574 int rc;
66575 sqlite3BtreeEnter(p);
66576 p->pBt->nPage = 0;
66577 rc = newDatabase(p->pBt);
66578 sqlite3BtreeLeave(p);
66579 return rc;
66580}
66581
66582/*
66583** Attempt to start a new transaction. A write-transaction
66584** is started if the second argument is nonzero, otherwise a read-
66585** transaction. If the second argument is 2 or more and exclusive
66586** transaction is started, meaning that no other process is allowed
66587** to access the database. A preexisting transaction may not be
66588** upgraded to exclusive by calling this routine a second time - the
66589** exclusivity flag only works for a new transaction.
66590**
66591** A write-transaction must be started before attempting any
66592** changes to the database. None of the following routines
66593** will work unless a transaction is started first:
66594**
66595** sqlite3BtreeCreateTable()
66596** sqlite3BtreeCreateIndex()
66597** sqlite3BtreeClearTable()
66598** sqlite3BtreeDropTable()
66599** sqlite3BtreeInsert()
66600** sqlite3BtreeDelete()
66601** sqlite3BtreeUpdateMeta()
66602**
66603** If an initial attempt to acquire the lock fails because of lock contention
66604** and the database was previously unlocked, then invoke the busy handler
66605** if there is one. But if there was previously a read-lock, do not
66606** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
66607** returned when there is already a read-lock in order to avoid a deadlock.
66608**
66609** Suppose there are two processes A and B. A has a read lock and B has
66610** a reserved lock. B tries to promote to exclusive but is blocked because
66611** of A's read lock. A tries to promote to reserved but is blocked by B.
66612** One or the other of the two processes must give way or there can be
66613** no progress. By returning SQLITE_BUSY and not invoking the busy callback
66614** when A already has a read lock, we encourage A to give up and let B
66615** proceed.
66616*/
66617SQLITE_PRIVATEstatic int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
66618 BtShared *pBt = p->pBt;
66619 int rc = SQLITE_OK0;
66620
66621 sqlite3BtreeEnter(p);
66622 btreeIntegrity(p)((void) (0)); ((void) (0));;
66623
66624 /* If the btree is already in a write-transaction, or it
66625 ** is already in a read-transaction and a read-transaction
66626 ** is requested, this is a no-op.
66627 */
66628 if( p->inTrans==TRANS_WRITE2 || (p->inTrans==TRANS_READ1 && !wrflag) ){
66629 goto trans_begun;
66630 }
66631 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 )((void) (0));
66632
66633 if( (p->db->flags & SQLITE_ResetDatabase0x02000000)
66634 && sqlite3PagerIsreadonly(pBt->pPager)==0
66635 ){
66636 pBt->btsFlags &= ~BTS_READ_ONLY0x0001;
66637 }
66638
66639 /* Write transactions are not possible on a read-only database */
66640 if( (pBt->btsFlags & BTS_READ_ONLY0x0001)!=0 && wrflag ){
66641 rc = SQLITE_READONLY8;
66642 goto trans_begun;
66643 }
66644
66645#ifndef SQLITE_OMIT_SHARED_CACHE
66646 {
66647 sqlite3 *pBlock = 0;
66648 /* If another database handle has already opened a write transaction
66649 ** on this shared-btree structure and a second write transaction is
66650 ** requested, return SQLITE_LOCKED.
66651 */
66652 if( (wrflag && pBt->inTransaction==TRANS_WRITE2)
66653 || (pBt->btsFlags & BTS_PENDING0x0080)!=0
66654 ){
66655 pBlock = pBt->pWriter->db;
66656 }else if( wrflag>1 ){
66657 BtLock *pIter;
66658 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
66659 if( pIter->pBtree!=p ){
66660 pBlock = pIter->pBtree->db;
66661 break;
66662 }
66663 }
66664 }
66665 if( pBlock ){
66666 sqlite3ConnectionBlocked(p->db, pBlock);
66667 rc = SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8));
66668 goto trans_begun;
66669 }
66670 }
66671#endif
66672
66673 /* Any read-only or read-write transaction implies a read-lock on
66674 ** page 1. So if some other shared-cache client already has a write-lock
66675 ** on page 1, the transaction cannot be opened. */
66676 rc = querySharedCacheTableLock(p, MASTER_ROOT1, READ_LOCK1);
66677 if( SQLITE_OK0!=rc ) goto trans_begun;
66678
66679 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY0x0010;
66680 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY0x0010;
66681 do {
66682 /* Call lockBtree() until either pBt->pPage1 is populated or
66683 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
66684 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
66685 ** reading page 1 it discovers that the page-size of the database
66686 ** file is not pBt->pageSize. In this case lockBtree() will update
66687 ** pBt->pageSize to the page-size of the file on disk.
66688 */
66689 while( pBt->pPage1==0 && SQLITE_OK0==(rc = lockBtree(pBt)) );
66690
66691 if( rc==SQLITE_OK0 && wrflag ){
66692 if( (pBt->btsFlags & BTS_READ_ONLY0x0001)!=0 ){
66693 rc = SQLITE_READONLY8;
66694 }else{
66695 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
66696 if( rc==SQLITE_OK0 ){
66697 rc = newDatabase(pBt);
66698 }else if( rc==SQLITE_BUSY_SNAPSHOT(5 | (2<<8)) && pBt->inTransaction==TRANS_NONE0 ){
66699 /* if there was no transaction opened when this function was
66700 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
66701 ** code to SQLITE_BUSY. */
66702 rc = SQLITE_BUSY5;
66703 }
66704 }
66705 }
66706
66707 if( rc!=SQLITE_OK0 ){
66708 unlockBtreeIfUnused(pBt);
66709 }
66710 }while( (rc&0xFF)==SQLITE_BUSY5 && pBt->inTransaction==TRANS_NONE0 &&
66711 btreeInvokeBusyHandler(pBt) );
66712 sqlite3PagerResetLockTimeout(pBt->pPager);
66713
66714 if( rc==SQLITE_OK0 ){
66715 if( p->inTrans==TRANS_NONE0 ){
66716 pBt->nTransaction++;
66717#ifndef SQLITE_OMIT_SHARED_CACHE
66718 if( p->sharable ){
66719 assert( p->lock.pBtree==p && p->lock.iTable==1 )((void) (0));
66720 p->lock.eLock = READ_LOCK1;
66721 p->lock.pNext = pBt->pLock;
66722 pBt->pLock = &p->lock;
66723 }
66724#endif
66725 }
66726 p->inTrans = (wrflag?TRANS_WRITE2:TRANS_READ1);
66727 if( p->inTrans>pBt->inTransaction ){
66728 pBt->inTransaction = p->inTrans;
66729 }
66730 if( wrflag ){
66731 MemPage *pPage1 = pBt->pPage1;
66732#ifndef SQLITE_OMIT_SHARED_CACHE
66733 assert( !pBt->pWriter )((void) (0));
66734 pBt->pWriter = p;
66735 pBt->btsFlags &= ~BTS_EXCLUSIVE0x0040;
66736 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE0x0040;
66737#endif
66738
66739 /* If the db-size header field is incorrect (as it may be if an old
66740 ** client has been writing the database file), update it now. Doing
66741 ** this sooner rather than later means the database size can safely
66742 ** re-read the database size from page 1 if a savepoint or transaction
66743 ** rollback occurs within the transaction.
66744 */
66745 if( pBt->nPage!=get4bytesqlite3Get4byte(&pPage1->aData[28]) ){
66746 rc = sqlite3PagerWrite(pPage1->pDbPage);
66747 if( rc==SQLITE_OK0 ){
66748 put4bytesqlite3Put4byte(&pPage1->aData[28], pBt->nPage);
66749 }
66750 }
66751 }
66752 }
66753
66754trans_begun:
66755 if( rc==SQLITE_OK0 ){
66756 if( pSchemaVersion ){
66757 *pSchemaVersion = get4bytesqlite3Get4byte(&pBt->pPage1->aData[40]);
66758 }
66759 if( wrflag ){
66760 /* This call makes sure that the pager has the correct number of
66761 ** open savepoints. If the second parameter is greater than 0 and
66762 ** the sub-journal is not already open, then it will be opened here.
66763 */
66764 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
66765 }
66766 }
66767
66768 btreeIntegrity(p)((void) (0)); ((void) (0));;
66769 sqlite3BtreeLeave(p);
66770 return rc;
66771}
66772
66773#ifndef SQLITE_OMIT_AUTOVACUUM
66774
66775/*
66776** Set the pointer-map entries for all children of page pPage. Also, if
66777** pPage contains cells that point to overflow pages, set the pointer
66778** map entries for the overflow pages as well.
66779*/
66780static int setChildPtrmaps(MemPage *pPage){
66781 int i; /* Counter variable */
66782 int nCell; /* Number of cells in page pPage */
66783 int rc; /* Return code */
66784 BtShared *pBt = pPage->pBt;
66785 Pgno pgno = pPage->pgno;
66786
66787 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
66788 rc = pPage->isInit ? SQLITE_OK0 : btreeInitPage(pPage);
66789 if( rc!=SQLITE_OK0 ) return rc;
66790 nCell = pPage->nCell;
66791
66792 for(i=0; i<nCell; i++){
66793 u8 *pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
66794
66795 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
66796
66797 if( !pPage->leaf ){
66798 Pgno childPgno = get4bytesqlite3Get4byte(pCell);
66799 ptrmapPut(pBt, childPgno, PTRMAP_BTREE5, pgno, &rc);
66800 }
66801 }
66802
66803 if( !pPage->leaf ){
66804 Pgno childPgno = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
66805 ptrmapPut(pBt, childPgno, PTRMAP_BTREE5, pgno, &rc);
66806 }
66807
66808 return rc;
66809}
66810
66811/*
66812** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
66813** that it points to iTo. Parameter eType describes the type of pointer to
66814** be modified, as follows:
66815**
66816** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
66817** page of pPage.
66818**
66819** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
66820** page pointed to by one of the cells on pPage.
66821**
66822** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
66823** overflow page in the list.
66824*/
66825static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
66826 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
66827 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
66828 if( eType==PTRMAP_OVERFLOW24 ){
66829 /* The pointer is always the first 4 bytes of the page in this case. */
66830 if( get4bytesqlite3Get4byte(pPage->aData)!=iFrom ){
66831 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(66831);
66832 }
66833 put4bytesqlite3Put4byte(pPage->aData, iTo);
66834 }else{
66835 int i;
66836 int nCell;
66837 int rc;
66838
66839 rc = pPage->isInit ? SQLITE_OK0 : btreeInitPage(pPage);
66840 if( rc ) return rc;
66841 nCell = pPage->nCell;
66842
66843 for(i=0; i<nCell; i++){
66844 u8 *pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
66845 if( eType==PTRMAP_OVERFLOW13 ){
66846 CellInfo info;
66847 pPage->xParseCell(pPage, pCell, &info);
66848 if( info.nLocal<info.nPayload ){
66849 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
66850 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(66850);
66851 }
66852 if( iFrom==get4bytesqlite3Get4byte(pCell+info.nSize-4) ){
66853 put4bytesqlite3Put4byte(pCell+info.nSize-4, iTo);
66854 break;
66855 }
66856 }
66857 }else{
66858 if( get4bytesqlite3Get4byte(pCell)==iFrom ){
66859 put4bytesqlite3Put4byte(pCell, iTo);
66860 break;
66861 }
66862 }
66863 }
66864
66865 if( i==nCell ){
66866 if( eType!=PTRMAP_BTREE5 ||
66867 get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
66868 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(66868);
66869 }
66870 put4bytesqlite3Put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
66871 }
66872 }
66873 return SQLITE_OK0;
66874}
66875
66876
66877/*
66878** Move the open database page pDbPage to location iFreePage in the
66879** database. The pDbPage reference remains valid.
66880**
66881** The isCommit flag indicates that there is no need to remember that
66882** the journal needs to be sync()ed before database page pDbPage->pgno
66883** can be written to. The caller has already promised not to write to that
66884** page.
66885*/
66886static int relocatePage(
66887 BtShared *pBt, /* Btree */
66888 MemPage *pDbPage, /* Open page to move */
66889 u8 eType, /* Pointer map 'type' entry for pDbPage */
66890 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
66891 Pgno iFreePage, /* The location to move pDbPage to */
66892 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
66893){
66894 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
66895 Pgno iDbPage = pDbPage->pgno;
66896 Pager *pPager = pBt->pPager;
66897 int rc;
66898
66899 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||((void) (0))
66900 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE )((void) (0));
66901 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
66902 assert( pDbPage->pBt==pBt )((void) (0));
66903 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(66903);
66904
66905 /* Move page iDbPage from its current location to page number iFreePage */
66906 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
66907 iDbPage, iFreePage, iPtrPage, eType));
66908 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
66909 if( rc!=SQLITE_OK0 ){
66910 return rc;
66911 }
66912 pDbPage->pgno = iFreePage;
66913
66914 /* If pDbPage was a btree-page, then it may have child pages and/or cells
66915 ** that point to overflow pages. The pointer map entries for all these
66916 ** pages need to be changed.
66917 **
66918 ** If pDbPage is an overflow page, then the first 4 bytes may store a
66919 ** pointer to a subsequent overflow page. If this is the case, then
66920 ** the pointer map needs to be updated for the subsequent overflow page.
66921 */
66922 if( eType==PTRMAP_BTREE5 || eType==PTRMAP_ROOTPAGE1 ){
66923 rc = setChildPtrmaps(pDbPage);
66924 if( rc!=SQLITE_OK0 ){
66925 return rc;
66926 }
66927 }else{
66928 Pgno nextOvfl = get4bytesqlite3Get4byte(pDbPage->aData);
66929 if( nextOvfl!=0 ){
66930 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW24, iFreePage, &rc);
66931 if( rc!=SQLITE_OK0 ){
66932 return rc;
66933 }
66934 }
66935 }
66936
66937 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
66938 ** that it points at iFreePage. Also fix the pointer map entry for
66939 ** iPtrPage.
66940 */
66941 if( eType!=PTRMAP_ROOTPAGE1 ){
66942 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
66943 if( rc!=SQLITE_OK0 ){
66944 return rc;
66945 }
66946 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
66947 if( rc!=SQLITE_OK0 ){
66948 releasePage(pPtrPage);
66949 return rc;
66950 }
66951 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
66952 releasePage(pPtrPage);
66953 if( rc==SQLITE_OK0 ){
66954 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
66955 }
66956 }
66957 return rc;
66958}
66959
66960/* Forward declaration required by incrVacuumStep(). */
66961static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
66962
66963/*
66964** Perform a single step of an incremental-vacuum. If successful, return
66965** SQLITE_OK. If there is no work to do (and therefore no point in
66966** calling this function again), return SQLITE_DONE. Or, if an error
66967** occurs, return some other error code.
66968**
66969** More specifically, this function attempts to re-organize the database so
66970** that the last page of the file currently in use is no longer in use.
66971**
66972** Parameter nFin is the number of pages that this database would contain
66973** were this function called until it returns SQLITE_DONE.
66974**
66975** If the bCommit parameter is non-zero, this function assumes that the
66976** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
66977** or an error. bCommit is passed true for an auto-vacuum-on-commit
66978** operation, or false for an incremental vacuum.
66979*/
66980static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
66981 Pgno nFreeList; /* Number of pages still on the free-list */
66982 int rc;
66983
66984 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
66985 assert( iLastPg>nFin )((void) (0));
66986
66987 if( !PTRMAP_ISPAGE(pBt, iLastPg)(ptrmapPageno((pBt), (iLastPg))==(iLastPg)) && iLastPg!=PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
66988 u8 eType;
66989 Pgno iPtrPage;
66990
66991 nFreeList = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
66992 if( nFreeList==0 ){
66993 return SQLITE_DONE101;
66994 }
66995
66996 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
66997 if( rc!=SQLITE_OK0 ){
66998 return rc;
66999 }
67000 if( eType==PTRMAP_ROOTPAGE1 ){
67001 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(67001);
67002 }
67003
67004 if( eType==PTRMAP_FREEPAGE2 ){
67005 if( bCommit==0 ){
67006 /* Remove the page from the files free-list. This is not required
67007 ** if bCommit is non-zero. In that case, the free-list will be
67008 ** truncated to zero after this function returns, so it doesn't
67009 ** matter if it still contains some garbage entries.
67010 */
67011 Pgno iFreePg;
67012 MemPage *pFreePg;
67013 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT1);
67014 if( rc!=SQLITE_OK0 ){
67015 return rc;
67016 }
67017 assert( iFreePg==iLastPg )((void) (0));
67018 releasePage(pFreePg);
67019 }
67020 } else {
67021 Pgno iFreePg; /* Index of free page to move pLastPg to */
67022 MemPage *pLastPg;
67023 u8 eMode = BTALLOC_ANY0; /* Mode parameter for allocateBtreePage() */
67024 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
67025
67026 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
67027 if( rc!=SQLITE_OK0 ){
67028 return rc;
67029 }
67030
67031 /* If bCommit is zero, this loop runs exactly once and page pLastPg
67032 ** is swapped with the first free page pulled off the free list.
67033 **
67034 ** On the other hand, if bCommit is greater than zero, then keep
67035 ** looping until a free-page located within the first nFin pages
67036 ** of the file is found.
67037 */
67038 if( bCommit==0 ){
67039 eMode = BTALLOC_LE2;
67040 iNear = nFin;
67041 }
67042 do {
67043 MemPage *pFreePg;
67044 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
67045 if( rc!=SQLITE_OK0 ){
67046 releasePage(pLastPg);
67047 return rc;
67048 }
67049 releasePage(pFreePg);
67050 }while( bCommit && iFreePg>nFin );
67051 assert( iFreePg<iLastPg )((void) (0));
67052
67053 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
67054 releasePage(pLastPg);
67055 if( rc!=SQLITE_OK0 ){
67056 return rc;
67057 }
67058 }
67059 }
67060
67061 if( bCommit==0 ){
67062 do {
67063 iLastPg--;
67064 }while( iLastPg==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) || PTRMAP_ISPAGE(pBt, iLastPg)(ptrmapPageno((pBt), (iLastPg))==(iLastPg)) );
67065 pBt->bDoTruncate = 1;
67066 pBt->nPage = iLastPg;
67067 }
67068 return SQLITE_OK0;
67069}
67070
67071/*
67072** The database opened by the first argument is an auto-vacuum database
67073** nOrig pages in size containing nFree free pages. Return the expected
67074** size of the database in pages following an auto-vacuum operation.
67075*/
67076static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
67077 int nEntry; /* Number of entries on one ptrmap page */
67078 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
67079 Pgno nFin; /* Return value */
67080
67081 nEntry = pBt->usableSize/5;
67082 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)ptrmapPageno(pBt, nOrig)+nEntry)/nEntry;
67083 nFin = nOrig - nFree - nPtrmap;
67084 if( nOrig>PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) && nFin<PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
67085 nFin--;
67086 }
67087 while( PTRMAP_ISPAGE(pBt, nFin)(ptrmapPageno((pBt), (nFin))==(nFin)) || nFin==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
67088 nFin--;
67089 }
67090
67091 return nFin;
67092}
67093
67094/*
67095** A write-transaction must be opened before calling this function.
67096** It performs a single unit of work towards an incremental vacuum.
67097**
67098** If the incremental vacuum is finished after this function has run,
67099** SQLITE_DONE is returned. If it is not finished, but no error occurred,
67100** SQLITE_OK is returned. Otherwise an SQLite error code.
67101*/
67102SQLITE_PRIVATEstatic int sqlite3BtreeIncrVacuum(Btree *p){
67103 int rc;
67104 BtShared *pBt = p->pBt;
67105
67106 sqlite3BtreeEnter(p);
67107 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE )((void) (0));
67108 if( !pBt->autoVacuum ){
67109 rc = SQLITE_DONE101;
67110 }else{
67111 Pgno nOrig = btreePagecount(pBt);
67112 Pgno nFree = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
67113 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
67114
67115 if( nOrig<nFin ){
67116 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(67116);
67117 }else if( nFree>0 ){
67118 rc = saveAllCursors(pBt, 0, 0);
67119 if( rc==SQLITE_OK0 ){
67120 invalidateAllOverflowCache(pBt);
67121 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
67122 }
67123 if( rc==SQLITE_OK0 ){
67124 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
67125 put4bytesqlite3Put4byte(&pBt->pPage1->aData[28], pBt->nPage);
67126 }
67127 }else{
67128 rc = SQLITE_DONE101;
67129 }
67130 }
67131 sqlite3BtreeLeave(p);
67132 return rc;
67133}
67134
67135/*
67136** This routine is called prior to sqlite3PagerCommit when a transaction
67137** is committed for an auto-vacuum database.
67138**
67139** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
67140** the database file should be truncated to during the commit process.
67141** i.e. the database has been reorganized so that only the first *pnTrunc
67142** pages are in use.
67143*/
67144static int autoVacuumCommit(BtShared *pBt){
67145 int rc = SQLITE_OK0;
67146 Pager *pPager = pBt->pPager;
67147 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
67148
67149 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
67150 invalidateAllOverflowCache(pBt);
67151 assert(pBt->autoVacuum)((void) (0));
67152 if( !pBt->incrVacuum ){
67153 Pgno nFin; /* Number of pages in database after autovacuuming */
67154 Pgno nFree; /* Number of pages on the freelist initially */
67155 Pgno iFree; /* The next page to be freed */
67156 Pgno nOrig; /* Database size before freeing */
67157
67158 nOrig = btreePagecount(pBt);
67159 if( PTRMAP_ISPAGE(pBt, nOrig)(ptrmapPageno((pBt), (nOrig))==(nOrig)) || nOrig==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
67160 /* It is not possible to create a database for which the final page
67161 ** is either a pointer-map page or the pending-byte page. If one
67162 ** is encountered, this indicates corruption.
67163 */
67164 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(67164);
67165 }
67166
67167 nFree = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]);
67168 nFin = finalDbSize(pBt, nOrig, nFree);
67169 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(67169);
67170 if( nFin<nOrig ){
67171 rc = saveAllCursors(pBt, 0, 0);
67172 }
67173 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK0; iFree--){
67174 rc = incrVacuumStep(pBt, nFin, iFree, 1);
67175 }
67176 if( (rc==SQLITE_DONE101 || rc==SQLITE_OK0) && nFree>0 ){
67177 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
67178 put4bytesqlite3Put4byte(&pBt->pPage1->aData[32], 0);
67179 put4bytesqlite3Put4byte(&pBt->pPage1->aData[36], 0);
67180 put4bytesqlite3Put4byte(&pBt->pPage1->aData[28], nFin);
67181 pBt->bDoTruncate = 1;
67182 pBt->nPage = nFin;
67183 }
67184 if( rc!=SQLITE_OK0 ){
67185 sqlite3PagerRollback(pPager);
67186 }
67187 }
67188
67189 assert( nRef>=sqlite3PagerRefcount(pPager) )((void) (0));
67190 return rc;
67191}
67192
67193#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
67194# define setChildPtrmaps(x) SQLITE_OK0
67195#endif
67196
67197/*
67198** This routine does the first phase of a two-phase commit. This routine
67199** causes a rollback journal to be created (if it does not already exist)
67200** and populated with enough information so that if a power loss occurs
67201** the database can be restored to its original state by playing back
67202** the journal. Then the contents of the journal are flushed out to
67203** the disk. After the journal is safely on oxide, the changes to the
67204** database are written into the database file and flushed to oxide.
67205** At the end of this call, the rollback journal still exists on the
67206** disk and we are still holding all locks, so the transaction has not
67207** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
67208** commit process.
67209**
67210** This call is a no-op if no write-transaction is currently active on pBt.
67211**
67212** Otherwise, sync the database file for the btree pBt. zMaster points to
67213** the name of a master journal file that should be written into the
67214** individual journal file, or is NULL, indicating no master journal file
67215** (single database transaction).
67216**
67217** When this is called, the master journal should already have been
67218** created, populated with this journal pointer and synced to disk.
67219**
67220** Once this is routine has returned, the only thing required to commit
67221** the write-transaction for this database file is to delete the journal.
67222*/
67223SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
67224 int rc = SQLITE_OK0;
67225 if( p->inTrans==TRANS_WRITE2 ){
67226 BtShared *pBt = p->pBt;
67227 sqlite3BtreeEnter(p);
67228#ifndef SQLITE_OMIT_AUTOVACUUM
67229 if( pBt->autoVacuum ){
67230 rc = autoVacuumCommit(pBt);
67231 if( rc!=SQLITE_OK0 ){
67232 sqlite3BtreeLeave(p);
67233 return rc;
67234 }
67235 }
67236 if( pBt->bDoTruncate ){
67237 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
67238 }
67239#endif
67240 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
67241 sqlite3BtreeLeave(p);
67242 }
67243 return rc;
67244}
67245
67246/*
67247** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
67248** at the conclusion of a transaction.
67249*/
67250static void btreeEndTransaction(Btree *p){
67251 BtShared *pBt = p->pBt;
67252 sqlite3 *db = p->db;
67253 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
67254
67255#ifndef SQLITE_OMIT_AUTOVACUUM
67256 pBt->bDoTruncate = 0;
67257#endif
67258 if( p->inTrans>TRANS_NONE0 && db->nVdbeRead>1 ){
67259 /* If there are other active statements that belong to this database
67260 ** handle, downgrade to a read-only transaction. The other statements
67261 ** may still be reading from the database. */
67262 downgradeAllSharedCacheTableLocks(p);
67263 p->inTrans = TRANS_READ1;
67264 }else{
67265 /* If the handle had any kind of transaction open, decrement the
67266 ** transaction count of the shared btree. If the transaction count
67267 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
67268 ** call below will unlock the pager. */
67269 if( p->inTrans!=TRANS_NONE0 ){
67270 clearAllSharedCacheTableLocks(p);
67271 pBt->nTransaction--;
67272 if( 0==pBt->nTransaction ){
67273 pBt->inTransaction = TRANS_NONE0;
67274 }
67275 }
67276
67277 /* Set the current transaction state to TRANS_NONE and unlock the
67278 ** pager if this call closed the only read or write transaction. */
67279 p->inTrans = TRANS_NONE0;
67280 unlockBtreeIfUnused(pBt);
67281 }
67282
67283 btreeIntegrity(p)((void) (0)); ((void) (0));;
67284}
67285
67286/*
67287** Commit the transaction currently in progress.
67288**
67289** This routine implements the second phase of a 2-phase commit. The
67290** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
67291** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
67292** routine did all the work of writing information out to disk and flushing the
67293** contents so that they are written onto the disk platter. All this
67294** routine has to do is delete or truncate or zero the header in the
67295** the rollback journal (which causes the transaction to commit) and
67296** drop locks.
67297**
67298** Normally, if an error occurs while the pager layer is attempting to
67299** finalize the underlying journal file, this function returns an error and
67300** the upper layer will attempt a rollback. However, if the second argument
67301** is non-zero then this b-tree transaction is part of a multi-file
67302** transaction. In this case, the transaction has already been committed
67303** (by deleting a master journal file) and the caller will ignore this
67304** functions return code. So, even if an error occurs in the pager layer,
67305** reset the b-tree objects internal state to indicate that the write
67306** transaction has been closed. This is quite safe, as the pager will have
67307** transitioned to the error state.
67308**
67309** This will release the write lock on the database file. If there
67310** are no active cursors, it also releases the read lock.
67311*/
67312SQLITE_PRIVATEstatic int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
67313
67314 if( p->inTrans==TRANS_NONE0 ) return SQLITE_OK0;
67315 sqlite3BtreeEnter(p);
67316 btreeIntegrity(p)((void) (0)); ((void) (0));;
67317
67318 /* If the handle has a write-transaction open, commit the shared-btrees
67319 ** transaction and set the shared state to TRANS_READ.
67320 */
67321 if( p->inTrans==TRANS_WRITE2 ){
67322 int rc;
67323 BtShared *pBt = p->pBt;
67324 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
67325 assert( pBt->nTransaction>0 )((void) (0));
67326 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
67327 if( rc!=SQLITE_OK0 && bCleanup==0 ){
67328 sqlite3BtreeLeave(p);
67329 return rc;
67330 }
67331 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */
67332 pBt->inTransaction = TRANS_READ1;
67333 btreeClearHasContent(pBt);
67334 }
67335
67336 btreeEndTransaction(p);
67337 sqlite3BtreeLeave(p);
67338 return SQLITE_OK0;
67339}
67340
67341/*
67342** Do both phases of a commit.
67343*/
67344SQLITE_PRIVATEstatic int sqlite3BtreeCommit(Btree *p){
67345 int rc;
67346 sqlite3BtreeEnter(p);
67347 rc = sqlite3BtreeCommitPhaseOne(p, 0);
67348 if( rc==SQLITE_OK0 ){
67349 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
67350 }
67351 sqlite3BtreeLeave(p);
67352 return rc;
67353}
67354
67355/*
67356** This routine sets the state to CURSOR_FAULT and the error
67357** code to errCode for every cursor on any BtShared that pBtree
67358** references. Or if the writeOnly flag is set to 1, then only
67359** trip write cursors and leave read cursors unchanged.
67360**
67361** Every cursor is a candidate to be tripped, including cursors
67362** that belong to other database connections that happen to be
67363** sharing the cache with pBtree.
67364**
67365** This routine gets called when a rollback occurs. If the writeOnly
67366** flag is true, then only write-cursors need be tripped - read-only
67367** cursors save their current positions so that they may continue
67368** following the rollback. Or, if writeOnly is false, all cursors are
67369** tripped. In general, writeOnly is false if the transaction being
67370** rolled back modified the database schema. In this case b-tree root
67371** pages may be moved or deleted from the database altogether, making
67372** it unsafe for read cursors to continue.
67373**
67374** If the writeOnly flag is true and an error is encountered while
67375** saving the current position of a read-only cursor, all cursors,
67376** including all read-cursors are tripped.
67377**
67378** SQLITE_OK is returned if successful, or if an error occurs while
67379** saving a cursor position, an SQLite error code.
67380*/
67381SQLITE_PRIVATEstatic int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
67382 BtCursor *p;
67383 int rc = SQLITE_OK0;
67384
67385 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 )((void) (0));
67386 if( pBtree ){
67387 sqlite3BtreeEnter(pBtree);
67388 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
67389 if( writeOnly && (p->curFlags & BTCF_WriteFlag0x01)==0 ){
67390 if( p->eState==CURSOR_VALID0 || p->eState==CURSOR_SKIPNEXT2 ){
67391 rc = saveCursorPosition(p);
67392 if( rc!=SQLITE_OK0 ){
67393 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
67394 break;
67395 }
67396 }
67397 }else{
67398 sqlite3BtreeClearCursor(p);
67399 p->eState = CURSOR_FAULT4;
67400 p->skipNext = errCode;
67401 }
67402 btreeReleaseAllCursorPages(p);
67403 }
67404 sqlite3BtreeLeave(pBtree);
67405 }
67406 return rc;
67407}
67408
67409/*
67410** Set the pBt->nPage field correctly, according to the current
67411** state of the database. Assume pBt->pPage1 is valid.
67412*/
67413static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
67414 int nPage = get4bytesqlite3Get4byte(&pPage1->aData[28]);
67415 testcase( nPage==0 );
67416 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
67417 testcase( pBt->nPage!=nPage );
67418 pBt->nPage = nPage;
67419}
67420
67421/*
67422** Rollback the transaction in progress.
67423**
67424** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
67425** Only write cursors are tripped if writeOnly is true but all cursors are
67426** tripped if writeOnly is false. Any attempt to use
67427** a tripped cursor will result in an error.
67428**
67429** This will release the write lock on the database file. If there
67430** are no active cursors, it also releases the read lock.
67431*/
67432SQLITE_PRIVATEstatic int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
67433 int rc;
67434 BtShared *pBt = p->pBt;
67435 MemPage *pPage1;
67436
67437 assert( writeOnly==1 || writeOnly==0 )((void) (0));
67438 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK )((void) (0));
67439 sqlite3BtreeEnter(p);
67440 if( tripCode==SQLITE_OK0 ){
67441 rc = tripCode = saveAllCursors(pBt, 0, 0);
67442 if( rc ) writeOnly = 0;
67443 }else{
67444 rc = SQLITE_OK0;
67445 }
67446 if( tripCode ){
67447 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
67448 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) )((void) (0));
67449 if( rc2!=SQLITE_OK0 ) rc = rc2;
67450 }
67451 btreeIntegrity(p)((void) (0)); ((void) (0));;
67452
67453 if( p->inTrans==TRANS_WRITE2 ){
67454 int rc2;
67455
67456 assert( TRANS_WRITE==pBt->inTransaction )((void) (0));
67457 rc2 = sqlite3PagerRollback(pBt->pPager);
67458 if( rc2!=SQLITE_OK0 ){
67459 rc = rc2;
67460 }
67461
67462 /* The rollback may have destroyed the pPage1->aData value. So
67463 ** call btreeGetPage() on page 1 again to make
67464 ** sure pPage1->aData is set correctly. */
67465 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK0 ){
67466 btreeSetNPage(pBt, pPage1);
67467 releasePageOne(pPage1);
67468 }
67469 assert( countValidCursors(pBt, 1)==0 )((void) (0));
67470 pBt->inTransaction = TRANS_READ1;
67471 btreeClearHasContent(pBt);
67472 }
67473
67474 btreeEndTransaction(p);
67475 sqlite3BtreeLeave(p);
67476 return rc;
67477}
67478
67479/*
67480** Start a statement subtransaction. The subtransaction can be rolled
67481** back independently of the main transaction. You must start a transaction
67482** before starting a subtransaction. The subtransaction is ended automatically
67483** if the main transaction commits or rolls back.
67484**
67485** Statement subtransactions are used around individual SQL statements
67486** that are contained within a BEGIN...COMMIT block. If a constraint
67487** error occurs within the statement, the effect of that one statement
67488** can be rolled back without having to rollback the entire transaction.
67489**
67490** A statement sub-transaction is implemented as an anonymous savepoint. The
67491** value passed as the second parameter is the total number of savepoints,
67492** including the new anonymous savepoint, open on the B-Tree. i.e. if there
67493** are no active savepoints and no other statement-transactions open,
67494** iStatement is 1. This anonymous savepoint can be released or rolled back
67495** using the sqlite3BtreeSavepoint() function.
67496*/
67497SQLITE_PRIVATEstatic int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
67498 int rc;
67499 BtShared *pBt = p->pBt;
67500 sqlite3BtreeEnter(p);
67501 assert( p->inTrans==TRANS_WRITE )((void) (0));
67502 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
67503 assert( iStatement>0 )((void) (0));
67504 assert( iStatement>p->db->nSavepoint )((void) (0));
67505 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
67506 /* At the pager level, a statement transaction is a savepoint with
67507 ** an index greater than all savepoints created explicitly using
67508 ** SQL statements. It is illegal to open, release or rollback any
67509 ** such savepoints while the statement transaction savepoint is active.
67510 */
67511 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
67512 sqlite3BtreeLeave(p);
67513 return rc;
67514}
67515
67516/*
67517** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
67518** or SAVEPOINT_RELEASE. This function either releases or rolls back the
67519** savepoint identified by parameter iSavepoint, depending on the value
67520** of op.
67521**
67522** Normally, iSavepoint is greater than or equal to zero. However, if op is
67523** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
67524** contents of the entire transaction are rolled back. This is different
67525** from a normal transaction rollback, as no locks are released and the
67526** transaction remains open.
67527*/
67528SQLITE_PRIVATEstatic int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
67529 int rc = SQLITE_OK0;
67530 if( p && p->inTrans==TRANS_WRITE2 ){
67531 BtShared *pBt = p->pBt;
67532 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK )((void) (0));
67533 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) )((void) (0));
67534 sqlite3BtreeEnter(p);
67535 if( op==SAVEPOINT_ROLLBACK2 ){
67536 rc = saveAllCursors(pBt, 0, 0);
67537 }
67538 if( rc==SQLITE_OK0 ){
67539 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
67540 }
67541 if( rc==SQLITE_OK0 ){
67542 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY0x0010)!=0 ){
67543 pBt->nPage = 0;
67544 }
67545 rc = newDatabase(pBt);
67546 btreeSetNPage(pBt, pBt->pPage1);
67547
67548 /* pBt->nPage might be zero if the database was corrupt when
67549 ** the transaction was started. Otherwise, it must be at least 1. */
67550 assert( CORRUPT_DB || pBt->nPage>0 )((void) (0));
67551 }
67552 sqlite3BtreeLeave(p);
67553 }
67554 return rc;
67555}
67556
67557/*
67558** Create a new cursor for the BTree whose root is on the page
67559** iTable. If a read-only cursor is requested, it is assumed that
67560** the caller already has at least a read-only transaction open
67561** on the database already. If a write-cursor is requested, then
67562** the caller is assumed to have an open write transaction.
67563**
67564** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
67565** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
67566** can be used for reading or for writing if other conditions for writing
67567** are also met. These are the conditions that must be met in order
67568** for writing to be allowed:
67569**
67570** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
67571**
67572** 2: Other database connections that share the same pager cache
67573** but which are not in the READ_UNCOMMITTED state may not have
67574** cursors open with wrFlag==0 on the same table. Otherwise
67575** the changes made by this write cursor would be visible to
67576** the read cursors in the other database connection.
67577**
67578** 3: The database must be writable (not on read-only media)
67579**
67580** 4: There must be an active transaction.
67581**
67582** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
67583** is set. If FORDELETE is set, that is a hint to the implementation that
67584** this cursor will only be used to seek to and delete entries of an index
67585** as part of a larger DELETE statement. The FORDELETE hint is not used by
67586** this implementation. But in a hypothetical alternative storage engine
67587** in which index entries are automatically deleted when corresponding table
67588** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
67589** operations on this cursor can be no-ops and all READ operations can
67590** return a null row (2-bytes: 0x01 0x00).
67591**
67592** No checking is done to make sure that page iTable really is the
67593** root page of a b-tree. If it is not, then the cursor acquired
67594** will not work correctly.
67595**
67596** It is assumed that the sqlite3BtreeCursorZero() has been called
67597** on pCur to initialize the memory space prior to invoking this routine.
67598*/
67599static int btreeCursor(
67600 Btree *p, /* The btree */
67601 int iTable, /* Root page of table to open */
67602 int wrFlag, /* 1 to write. 0 read-only */
67603 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
67604 BtCursor *pCur /* Space for new cursor */
67605){
67606 BtShared *pBt = p->pBt; /* Shared b-tree handle */
67607 BtCursor *pX; /* Looping over other all cursors */
67608
67609 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
67610 assert( wrFlag==0((void) (0))
67611 || wrFlag==BTREE_WRCSR((void) (0))
67612 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)((void) (0))
67613 )((void) (0));
67614
67615 /* The following assert statements verify that if this is a sharable
67616 ** b-tree database, the connection is holding the required table locks,
67617 ** and that no other connection has any open cursor that conflicts with
67618 ** this lock. */
67619 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) )((void) (0));
67620 assert( wrFlag==0 || !hasReadConflicts(p, iTable) )((void) (0));
67621
67622 /* Assert that the caller has opened the required transaction. */
67623 assert( p->inTrans>TRANS_NONE )((void) (0));
67624 assert( wrFlag==0 || p->inTrans==TRANS_WRITE )((void) (0));
67625 assert( pBt->pPage1 && pBt->pPage1->aData )((void) (0));
67626 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
67627
67628 if( wrFlag ){
67629 allocateTempSpace(pBt);
67630 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT7;
67631 }
67632 if( iTable==1 && btreePagecount(pBt)==0 ){
67633 assert( wrFlag==0 )((void) (0));
67634 iTable = 0;
67635 }
67636
67637 /* Now that no other errors can occur, finish filling in the BtCursor
67638 ** variables and link the cursor into the BtShared list. */
67639 pCur->pgnoRoot = (Pgno)iTable;
67640 pCur->iPage = -1;
67641 pCur->pKeyInfo = pKeyInfo;
67642 pCur->pBtree = p;
67643 pCur->pBt = pBt;
67644 pCur->curFlags = wrFlag ? BTCF_WriteFlag0x01 : 0;
67645 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY0x02;
67646 /* If there are two or more cursors on the same btree, then all such
67647 ** cursors *must* have the BTCF_Multiple flag set. */
67648 for(pX=pBt->pCursor; pX; pX=pX->pNext){
67649 if( pX->pgnoRoot==(Pgno)iTable ){
67650 pX->curFlags |= BTCF_Multiple0x20;
67651 pCur->curFlags |= BTCF_Multiple0x20;
67652 }
67653 }
67654 pCur->pNext = pBt->pCursor;
67655 pBt->pCursor = pCur;
67656 pCur->eState = CURSOR_INVALID1;
67657 return SQLITE_OK0;
67658}
67659SQLITE_PRIVATEstatic int sqlite3BtreeCursor(
67660 Btree *p, /* The btree */
67661 int iTable, /* Root page of table to open */
67662 int wrFlag, /* 1 to write. 0 read-only */
67663 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
67664 BtCursor *pCur /* Write new cursor here */
67665){
67666 int rc;
67667 if( iTable<1 ){
67668 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(67668);
67669 }else{
67670 sqlite3BtreeEnter(p);
67671 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
67672 sqlite3BtreeLeave(p);
67673 }
67674 return rc;
67675}
67676
67677/*
67678** Return the size of a BtCursor object in bytes.
67679**
67680** This interfaces is needed so that users of cursors can preallocate
67681** sufficient storage to hold a cursor. The BtCursor object is opaque
67682** to users so they cannot do the sizeof() themselves - they must call
67683** this routine.
67684*/
67685SQLITE_PRIVATEstatic int sqlite3BtreeCursorSize(void){
67686 return ROUND8(sizeof(BtCursor))(((sizeof(BtCursor))+7)&~7);
67687}
67688
67689/*
67690** Initialize memory that will be converted into a BtCursor object.
67691**
67692** The simple approach here would be to memset() the entire object
67693** to zero. But it turns out that the apPage[] and aiIdx[] arrays
67694** do not need to be zeroed and they are large, so we can save a lot
67695** of run-time by skipping the initialization of those elements.
67696*/
67697SQLITE_PRIVATEstatic void sqlite3BtreeCursorZero(BtCursor *p){
67698 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT)__builtin_offsetof(BtCursor, pBt));
67699}
67700
67701/*
67702** Close a cursor. The read lock on the database file is released
67703** when the last cursor is closed.
67704*/
67705SQLITE_PRIVATEstatic int sqlite3BtreeCloseCursor(BtCursor *pCur){
67706 Btree *pBtree = pCur->pBtree;
67707 if( pBtree ){
67708 BtShared *pBt = pCur->pBt;
67709 sqlite3BtreeEnter(pBtree);
67710 assert( pBt->pCursor!=0 )((void) (0));
67711 if( pBt->pCursor==pCur ){
67712 pBt->pCursor = pCur->pNext;
67713 }else{
67714 BtCursor *pPrev = pBt->pCursor;
67715 do{
67716 if( pPrev->pNext==pCur ){
67717 pPrev->pNext = pCur->pNext;
67718 break;
67719 }
67720 pPrev = pPrev->pNext;
67721 }while( ALWAYS(pPrev)(pPrev) );
67722 }
67723 btreeReleaseAllCursorPages(pCur);
67724 unlockBtreeIfUnused(pBt);
67725 sqlite3_free(pCur->aOverflow);
67726 sqlite3_free(pCur->pKey);
67727 sqlite3BtreeLeave(pBtree);
67728 pCur->pBtree = 0;
67729 }
67730 return SQLITE_OK0;
67731}
67732
67733/*
67734** Make sure the BtCursor* given in the argument has a valid
67735** BtCursor.info structure. If it is not already valid, call
67736** btreeParseCell() to fill it in.
67737**
67738** BtCursor.info is a cache of the information in the current cell.
67739** Using this cache reduces the number of calls to btreeParseCell().
67740*/
67741#ifndef NDEBUG1
67742 static int cellInfoEqual(CellInfo *a, CellInfo *b){
67743 if( a->nKey!=b->nKey ) return 0;
67744 if( a->pPayload!=b->pPayload ) return 0;
67745 if( a->nPayload!=b->nPayload ) return 0;
67746 if( a->nLocal!=b->nLocal ) return 0;
67747 if( a->nSize!=b->nSize ) return 0;
67748 return 1;
67749 }
67750 static void assertCellInfo(BtCursor *pCur){
67751 CellInfo info;
67752 memset(&info, 0, sizeof(info));
67753 btreeParseCell(pCur->pPage, pCur->ix, &info);
67754 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) )((void) (0));
67755 }
67756#else
67757 #define assertCellInfo(x)
67758#endif
67759static SQLITE_NOINLINE__attribute__((noinline)) void getCellInfo(BtCursor *pCur){
67760 if( pCur->info.nSize==0 ){
67761 pCur->curFlags |= BTCF_ValidNKey0x02;
67762 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
67763 }else{
67764 assertCellInfo(pCur);
67765 }
67766}
67767
67768#ifndef NDEBUG1 /* The next routine used only within assert() statements */
67769/*
67770** Return true if the given BtCursor is valid. A valid cursor is one
67771** that is currently pointing to a row in a (non-empty) table.
67772** This is a verification routine is used only within assert() statements.
67773*/
67774SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValid(BtCursor *pCur){
67775 return pCur && pCur->eState==CURSOR_VALID0;
67776}
67777#endif /* NDEBUG */
67778SQLITE_PRIVATEstatic int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
67779 assert( pCur!=0 )((void) (0));
67780 return pCur->eState==CURSOR_VALID0;
67781}
67782
67783/*
67784** Return the value of the integer key or "rowid" for a table btree.
67785** This routine is only valid for a cursor that is pointing into a
67786** ordinary table btree. If the cursor points to an index btree or
67787** is invalid, the result of this routine is undefined.
67788*/
67789SQLITE_PRIVATEstatic i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
67790 assert( cursorHoldsMutex(pCur) )((void) (0));
67791 assert( pCur->eState==CURSOR_VALID )((void) (0));
67792 assert( pCur->curIntKey )((void) (0));
67793 getCellInfo(pCur);
67794 return pCur->info.nKey;
67795}
67796
67797#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
67798/*
67799** Return the offset into the database file for the start of the
67800** payload to which the cursor is pointing.
67801*/
67802SQLITE_PRIVATEstatic i64 sqlite3BtreeOffset(BtCursor *pCur){
67803 assert( cursorHoldsMutex(pCur) )((void) (0));
67804 assert( pCur->eState==CURSOR_VALID )((void) (0));
67805 getCellInfo(pCur);
67806 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
67807 (i64)(pCur->info.pPayload - pCur->pPage->aData);
67808}
67809#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
67810
67811/*
67812** Return the number of bytes of payload for the entry that pCur is
67813** currently pointing to. For table btrees, this will be the amount
67814** of data. For index btrees, this will be the size of the key.
67815**
67816** The caller must guarantee that the cursor is pointing to a non-NULL
67817** valid entry. In other words, the calling procedure must guarantee
67818** that the cursor has Cursor.eState==CURSOR_VALID.
67819*/
67820SQLITE_PRIVATEstatic u32 sqlite3BtreePayloadSize(BtCursor *pCur){
67821 assert( cursorHoldsMutex(pCur) )((void) (0));
67822 assert( pCur->eState==CURSOR_VALID )((void) (0));
67823 getCellInfo(pCur);
67824 return pCur->info.nPayload;
67825}
67826
67827/*
67828** Return an upper bound on the size of any record for the table
67829** that the cursor is pointing into.
67830**
67831** This is an optimization. Everything will still work if this
67832** routine always returns 2147483647 (which is the largest record
67833** that SQLite can handle) or more. But returning a smaller value might
67834** prevent large memory allocations when trying to interpret a
67835** corrupt datrabase.
67836**
67837** The current implementation merely returns the size of the underlying
67838** database file.
67839*/
67840SQLITE_PRIVATEstatic sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
67841 assert( cursorHoldsMutex(pCur) )((void) (0));
67842 assert( pCur->eState==CURSOR_VALID )((void) (0));
67843 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
67844}
67845
67846/*
67847** Given the page number of an overflow page in the database (parameter
67848** ovfl), this function finds the page number of the next page in the
67849** linked list of overflow pages. If possible, it uses the auto-vacuum
67850** pointer-map data instead of reading the content of page ovfl to do so.
67851**
67852** If an error occurs an SQLite error code is returned. Otherwise:
67853**
67854** The page number of the next overflow page in the linked list is
67855** written to *pPgnoNext. If page ovfl is the last page in its linked
67856** list, *pPgnoNext is set to zero.
67857**
67858** If ppPage is not NULL, and a reference to the MemPage object corresponding
67859** to page number pOvfl was obtained, then *ppPage is set to point to that
67860** reference. It is the responsibility of the caller to call releasePage()
67861** on *ppPage to free the reference. In no reference was obtained (because
67862** the pointer-map was used to obtain the value for *pPgnoNext), then
67863** *ppPage is set to zero.
67864*/
67865static int getOverflowPage(
67866 BtShared *pBt, /* The database file */
67867 Pgno ovfl, /* Current overflow page number */
67868 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
67869 Pgno *pPgnoNext /* OUT: Next overflow page number */
67870){
67871 Pgno next = 0;
67872 MemPage *pPage = 0;
67873 int rc = SQLITE_OK0;
67874
67875 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
67876 assert(pPgnoNext)((void) (0));
67877
67878#ifndef SQLITE_OMIT_AUTOVACUUM
67879 /* Try to find the next page in the overflow list using the
67880 ** autovacuum pointer-map pages. Guess that the next page in
67881 ** the overflow list is page number (ovfl+1). If that guess turns
67882 ** out to be wrong, fall back to loading the data of page
67883 ** number ovfl to determine the next page number.
67884 */
67885 if( pBt->autoVacuum ){
67886 Pgno pgno;
67887 Pgno iGuess = ovfl+1;
67888 u8 eType;
67889
67890 while( PTRMAP_ISPAGE(pBt, iGuess)(ptrmapPageno((pBt), (iGuess))==(iGuess)) || iGuess==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
67891 iGuess++;
67892 }
67893
67894 if( iGuess<=btreePagecount(pBt) ){
67895 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
67896 if( rc==SQLITE_OK0 && eType==PTRMAP_OVERFLOW24 && pgno==ovfl ){
67897 next = iGuess;
67898 rc = SQLITE_DONE101;
67899 }
67900 }
67901 }
67902#endif
67903
67904 assert( next==0 || rc==SQLITE_DONE )((void) (0));
67905 if( rc==SQLITE_OK0 ){
67906 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY0x02 : 0);
67907 assert( rc==SQLITE_OK || pPage==0 )((void) (0));
67908 if( rc==SQLITE_OK0 ){
67909 next = get4bytesqlite3Get4byte(pPage->aData);
67910 }
67911 }
67912
67913 *pPgnoNext = next;
67914 if( ppPage ){
67915 *ppPage = pPage;
67916 }else{
67917 releasePage(pPage);
67918 }
67919 return (rc==SQLITE_DONE101 ? SQLITE_OK0 : rc);
67920}
67921
67922/*
67923** Copy data from a buffer to a page, or from a page to a buffer.
67924**
67925** pPayload is a pointer to data stored on database page pDbPage.
67926** If argument eOp is false, then nByte bytes of data are copied
67927** from pPayload to the buffer pointed at by pBuf. If eOp is true,
67928** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
67929** of data are copied from the buffer pBuf to pPayload.
67930**
67931** SQLITE_OK is returned on success, otherwise an error code.
67932*/
67933static int copyPayload(
67934 void *pPayload, /* Pointer to page data */
67935 void *pBuf, /* Pointer to buffer */
67936 int nByte, /* Number of bytes to copy */
67937 int eOp, /* 0 -> copy from page, 1 -> copy to page */
67938 DbPage *pDbPage /* Page containing pPayload */
67939){
67940 if( eOp ){
67941 /* Copy data from buffer to page (a write operation) */
67942 int rc = sqlite3PagerWrite(pDbPage);
67943 if( rc!=SQLITE_OK0 ){
67944 return rc;
67945 }
67946 memcpy(pPayload, pBuf, nByte);
67947 }else{
67948 /* Copy data from page to buffer (a read operation) */
67949 memcpy(pBuf, pPayload, nByte);
67950 }
67951 return SQLITE_OK0;
67952}
67953
67954/*
67955** This function is used to read or overwrite payload information
67956** for the entry that the pCur cursor is pointing to. The eOp
67957** argument is interpreted as follows:
67958**
67959** 0: The operation is a read. Populate the overflow cache.
67960** 1: The operation is a write. Populate the overflow cache.
67961**
67962** A total of "amt" bytes are read or written beginning at "offset".
67963** Data is read to or from the buffer pBuf.
67964**
67965** The content being read or written might appear on the main page
67966** or be scattered out on multiple overflow pages.
67967**
67968** If the current cursor entry uses one or more overflow pages
67969** this function may allocate space for and lazily populate
67970** the overflow page-list cache array (BtCursor.aOverflow).
67971** Subsequent calls use this cache to make seeking to the supplied offset
67972** more efficient.
67973**
67974** Once an overflow page-list cache has been allocated, it must be
67975** invalidated if some other cursor writes to the same table, or if
67976** the cursor is moved to a different row. Additionally, in auto-vacuum
67977** mode, the following events may invalidate an overflow page-list cache.
67978**
67979** * An incremental vacuum,
67980** * A commit in auto_vacuum="full" mode,
67981** * Creating a table (may require moving an overflow page).
67982*/
67983static int accessPayload(
67984 BtCursor *pCur, /* Cursor pointing to entry to read from */
67985 u32 offset, /* Begin reading this far into payload */
67986 u32 amt, /* Read this many bytes */
67987 unsigned char *pBuf, /* Write the bytes into this buffer */
67988 int eOp /* zero to read. non-zero to write. */
67989){
67990 unsigned char *aPayload;
67991 int rc = SQLITE_OK0;
67992 int iIdx = 0;
67993 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
67994 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
67995#ifdef SQLITE_DIRECT_OVERFLOW_READ
67996 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
67997#endif
67998
67999 assert( pPage )((void) (0));
68000 assert( eOp==0 || eOp==1 )((void) (0));
68001 assert( pCur->eState==CURSOR_VALID )((void) (0));
68002 assert( pCur->ix<pPage->nCell )((void) (0));
68003 assert( cursorHoldsMutex(pCur) )((void) (0));
68004
68005 getCellInfo(pCur);
68006 aPayload = pCur->info.pPayload;
68007 assert( offset+amt <= pCur->info.nPayload )((void) (0));
68008
68009 assert( aPayload > pPage->aData )((void) (0));
68010 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
68011 /* Trying to read or write past the end of the data is an error. The
68012 ** conditional above is really:
68013 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
68014 ** but is recast into its current form to avoid integer overflow problems
68015 */
68016 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68016);
68017 }
68018
68019 /* Check if data must be read/written to/from the btree page itself. */
68020 if( offset<pCur->info.nLocal ){
68021 int a = amt;
68022 if( a+offset>pCur->info.nLocal ){
68023 a = pCur->info.nLocal - offset;
68024 }
68025 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
68026 offset = 0;
68027 pBuf += a;
68028 amt -= a;
68029 }else{
68030 offset -= pCur->info.nLocal;
68031 }
68032
68033
68034 if( rc==SQLITE_OK0 && amt>0 ){
68035 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
68036 Pgno nextPage;
68037
68038 nextPage = get4bytesqlite3Get4byte(&aPayload[pCur->info.nLocal]);
68039
68040 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
68041 **
68042 ** The aOverflow[] array is sized at one entry for each overflow page
68043 ** in the overflow chain. The page number of the first overflow page is
68044 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
68045 ** means "not yet known" (the cache is lazily populated).
68046 */
68047 if( (pCur->curFlags & BTCF_ValidOvfl0x04)==0 ){
68048 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
68049 if( pCur->aOverflow==0
68050 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
68051 ){
68052 Pgno *aNew = (Pgno*)sqlite3Realloc(
68053 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
68054 );
68055 if( aNew==0 ){
68056 return SQLITE_NOMEM_BKPT7;
68057 }else{
68058 pCur->aOverflow = aNew;
68059 }
68060 }
68061 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
68062 pCur->curFlags |= BTCF_ValidOvfl0x04;
68063 }else{
68064 /* If the overflow page-list cache has been allocated and the
68065 ** entry for the first required overflow page is valid, skip
68066 ** directly to it.
68067 */
68068 if( pCur->aOverflow[offset/ovflSize] ){
68069 iIdx = (offset/ovflSize);
68070 nextPage = pCur->aOverflow[iIdx];
68071 offset = (offset%ovflSize);
68072 }
68073 }
68074
68075 assert( rc==SQLITE_OK && amt>0 )((void) (0));
68076 while( nextPage ){
68077 /* If required, populate the overflow page-list cache. */
68078 assert( pCur->aOverflow[iIdx]==0((void) (0))
68079 || pCur->aOverflow[iIdx]==nextPage((void) (0))
68080 || CORRUPT_DB )((void) (0));
68081 pCur->aOverflow[iIdx] = nextPage;
68082
68083 if( offset>=ovflSize ){
68084 /* The only reason to read this page is to obtain the page
68085 ** number for the next page in the overflow chain. The page
68086 ** data is not required. So first try to lookup the overflow
68087 ** page-list cache, if any, then fall back to the getOverflowPage()
68088 ** function.
68089 */
68090 assert( pCur->curFlags & BTCF_ValidOvfl )((void) (0));
68091 assert( pCur->pBtree->db==pBt->db )((void) (0));
68092 if( pCur->aOverflow[iIdx+1] ){
68093 nextPage = pCur->aOverflow[iIdx+1];
68094 }else{
68095 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
68096 }
68097 offset -= ovflSize;
68098 }else{
68099 /* Need to read this page properly. It contains some of the
68100 ** range of data that is being read (eOp==0) or written (eOp!=0).
68101 */
68102 int a = amt;
68103 if( a + offset > ovflSize ){
68104 a = ovflSize - offset;
68105 }
68106
68107#ifdef SQLITE_DIRECT_OVERFLOW_READ
68108 /* If all the following are true:
68109 **
68110 ** 1) this is a read operation, and
68111 ** 2) data is required from the start of this overflow page, and
68112 ** 3) there are no dirty pages in the page-cache
68113 ** 4) the database is file-backed, and
68114 ** 5) the page is not in the WAL file
68115 ** 6) at least 4 bytes have already been read into the output buffer
68116 **
68117 ** then data can be read directly from the database file into the
68118 ** output buffer, bypassing the page-cache altogether. This speeds
68119 ** up loading large records that span many overflow pages.
68120 */
68121 if( eOp==0 /* (1) */
68122 && offset==0 /* (2) */
68123 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
68124 && &pBuf[-4]>=pBufStart /* (6) */
68125 ){
68126 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
68127 u8 aSave[4];
68128 u8 *aWrite = &pBuf[-4];
68129 assert( aWrite>=pBufStart )((void) (0)); /* due to (6) */
68130 memcpy(aSave, aWrite, 4);
68131 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
68132 nextPage = get4bytesqlite3Get4byte(aWrite);
68133 memcpy(aWrite, aSave, 4);
68134 }else
68135#endif
68136
68137 {
68138 DbPage *pDbPage;
68139 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
68140 (eOp==0 ? PAGER_GET_READONLY0x02 : 0)
68141 );
68142 if( rc==SQLITE_OK0 ){
68143 aPayload = sqlite3PagerGetData(pDbPage);
68144 nextPage = get4bytesqlite3Get4byte(aPayload);
68145 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
68146 sqlite3PagerUnref(pDbPage);
68147 offset = 0;
68148 }
68149 }
68150 amt -= a;
68151 if( amt==0 ) return rc;
68152 pBuf += a;
68153 }
68154 if( rc ) break;
68155 iIdx++;
68156 }
68157 }
68158
68159 if( rc==SQLITE_OK0 && amt>0 ){
68160 /* Overflow chain ends prematurely */
68161 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68161);
68162 }
68163 return rc;
68164}
68165
68166/*
68167** Read part of the payload for the row at which that cursor pCur is currently
68168** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
68169** begins at "offset".
68170**
68171** pCur can be pointing to either a table or an index b-tree.
68172** If pointing to a table btree, then the content section is read. If
68173** pCur is pointing to an index b-tree then the key section is read.
68174**
68175** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
68176** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
68177** cursor might be invalid or might need to be restored before being read.
68178**
68179** Return SQLITE_OK on success or an error code if anything goes
68180** wrong. An error is returned if "offset+amt" is larger than
68181** the available payload.
68182*/
68183SQLITE_PRIVATEstatic int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
68184 assert( cursorHoldsMutex(pCur) )((void) (0));
68185 assert( pCur->eState==CURSOR_VALID )((void) (0));
68186 assert( pCur->iPage>=0 && pCur->pPage )((void) (0));
68187 assert( pCur->ix<pCur->pPage->nCell )((void) (0));
68188 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
68189}
68190
68191/*
68192** This variant of sqlite3BtreePayload() works even if the cursor has not
68193** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
68194** interface.
68195*/
68196#ifndef SQLITE_OMIT_INCRBLOB
68197static SQLITE_NOINLINE__attribute__((noinline)) int accessPayloadChecked(
68198 BtCursor *pCur,
68199 u32 offset,
68200 u32 amt,
68201 void *pBuf
68202){
68203 int rc;
68204 if ( pCur->eState==CURSOR_INVALID1 ){
68205 return SQLITE_ABORT4;
68206 }
68207 assert( cursorOwnsBtShared(pCur) )((void) (0));
68208 rc = btreeRestoreCursorPosition(pCur);
68209 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
68210}
68211SQLITE_PRIVATEstatic int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
68212 if( pCur->eState==CURSOR_VALID0 ){
68213 assert( cursorOwnsBtShared(pCur) )((void) (0));
68214 return accessPayload(pCur, offset, amt, pBuf, 0);
68215 }else{
68216 return accessPayloadChecked(pCur, offset, amt, pBuf);
68217 }
68218}
68219#endif /* SQLITE_OMIT_INCRBLOB */
68220
68221/*
68222** Return a pointer to payload information from the entry that the
68223** pCur cursor is pointing to. The pointer is to the beginning of
68224** the key if index btrees (pPage->intKey==0) and is the data for
68225** table btrees (pPage->intKey==1). The number of bytes of available
68226** key/data is written into *pAmt. If *pAmt==0, then the value
68227** returned will not be a valid pointer.
68228**
68229** This routine is an optimization. It is common for the entire key
68230** and data to fit on the local page and for there to be no overflow
68231** pages. When that is so, this routine can be used to access the
68232** key and data without making a copy. If the key and/or data spills
68233** onto overflow pages, then accessPayload() must be used to reassemble
68234** the key/data and copy it into a preallocated buffer.
68235**
68236** The pointer returned by this routine looks directly into the cached
68237** page of the database. The data might change or move the next time
68238** any btree routine is called.
68239*/
68240static const void *fetchPayload(
68241 BtCursor *pCur, /* Cursor pointing to entry to read from */
68242 u32 *pAmt /* Write the number of available bytes here */
68243){
68244 int amt;
68245 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage)((void) (0));
68246 assert( pCur->eState==CURSOR_VALID )((void) (0));
68247 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
68248 assert( cursorOwnsBtShared(pCur) )((void) (0));
68249 assert( pCur->ix<pCur->pPage->nCell )((void) (0));
68250 assert( pCur->info.nSize>0 )((void) (0));
68251 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB )((void) (0));
68252 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB)((void) (0));
68253 amt = pCur->info.nLocal;
68254 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
68255 /* There is too little space on the page for the expected amount
68256 ** of local content. Database must be corrupt. */
68257 assert( CORRUPT_DB )((void) (0));
68258 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload))((0)>((int)(pCur->pPage->aDataEnd - pCur->info.pPayload
))?(0):((int)(pCur->pPage->aDataEnd - pCur->info.pPayload
)))
;
68259 }
68260 *pAmt = (u32)amt;
68261 return (void*)pCur->info.pPayload;
68262}
68263
68264
68265/*
68266** For the entry that cursor pCur is point to, return as
68267** many bytes of the key or data as are available on the local
68268** b-tree page. Write the number of available bytes into *pAmt.
68269**
68270** The pointer returned is ephemeral. The key/data may move
68271** or be destroyed on the next call to any Btree routine,
68272** including calls from other threads against the same cache.
68273** Hence, a mutex on the BtShared should be held prior to calling
68274** this routine.
68275**
68276** These routines is used to get quick access to key and data
68277** in the common case where no overflow pages are used.
68278*/
68279SQLITE_PRIVATEstatic const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
68280 return fetchPayload(pCur, pAmt);
68281}
68282
68283
68284/*
68285** Move the cursor down to a new child page. The newPgno argument is the
68286** page number of the child page to move to.
68287**
68288** This function returns SQLITE_CORRUPT if the page-header flags field of
68289** the new child page does not match the flags field of the parent (i.e.
68290** if an intkey page appears to be the parent of a non-intkey page, or
68291** vice-versa).
68292*/
68293static int moveToChild(BtCursor *pCur, u32 newPgno){
68294 BtShared *pBt = pCur->pBt;
68295
68296 assert( cursorOwnsBtShared(pCur) )((void) (0));
68297 assert( pCur->eState==CURSOR_VALID )((void) (0));
68298 assert( pCur->iPage<BTCURSOR_MAX_DEPTH )((void) (0));
68299 assert( pCur->iPage>=0 )((void) (0));
68300 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH20-1) ){
68301 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(68301);
68302 }
68303 pCur->info.nSize = 0;
68304 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
68305 pCur->aiIdx[pCur->iPage] = pCur->ix;
68306 pCur->apPage[pCur->iPage] = pCur->pPage;
68307 pCur->ix = 0;
68308 pCur->iPage++;
68309 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
68310}
68311
68312#ifdef SQLITE_DEBUG
68313/*
68314** Page pParent is an internal (non-leaf) tree page. This function
68315** asserts that page number iChild is the left-child if the iIdx'th
68316** cell in page pParent. Or, if iIdx is equal to the total number of
68317** cells in pParent, that page number iChild is the right-child of
68318** the page.
68319*/
68320static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
68321 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return; /* The conditions tested below might not be true
68322 ** in a corrupt database */
68323 assert( iIdx<=pParent->nCell )((void) (0));
68324 if( iIdx==pParent->nCell ){
68325 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild )((void) (0));
68326 }else{
68327 assert( get4byte(findCell(pParent, iIdx))==iChild )((void) (0));
68328 }
68329}
68330#else
68331# define assertParentIndex(x,y,z)
68332#endif
68333
68334/*
68335** Move the cursor up to the parent page.
68336**
68337** pCur->idx is set to the cell index that contains the pointer
68338** to the page we are coming from. If we are coming from the
68339** right-most child page then pCur->idx is set to one more than
68340** the largest cell index.
68341*/
68342static void moveToParent(BtCursor *pCur){
68343 MemPage *pLeaf;
68344 assert( cursorOwnsBtShared(pCur) )((void) (0));
68345 assert( pCur->eState==CURSOR_VALID )((void) (0));
68346 assert( pCur->iPage>0 )((void) (0));
68347 assert( pCur->pPage )((void) (0));
68348 assertParentIndex(
68349 pCur->apPage[pCur->iPage-1],
68350 pCur->aiIdx[pCur->iPage-1],
68351 pCur->pPage->pgno
68352 );
68353 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
68354 pCur->info.nSize = 0;
68355 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
68356 pCur->ix = pCur->aiIdx[pCur->iPage-1];
68357 pLeaf = pCur->pPage;
68358 pCur->pPage = pCur->apPage[--pCur->iPage];
68359 releasePageNotNull(pLeaf);
68360}
68361
68362/*
68363** Move the cursor to point to the root page of its b-tree structure.
68364**
68365** If the table has a virtual root page, then the cursor is moved to point
68366** to the virtual root page instead of the actual root page. A table has a
68367** virtual root page when the actual root page contains no cells and a
68368** single child page. This can only happen with the table rooted at page 1.
68369**
68370** If the b-tree structure is empty, the cursor state is set to
68371** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
68372** the cursor is set to point to the first cell located on the root
68373** (or virtual root) page and the cursor state is set to CURSOR_VALID.
68374**
68375** If this function returns successfully, it may be assumed that the
68376** page-header flags indicate that the [virtual] root-page is the expected
68377** kind of b-tree page (i.e. if when opening the cursor the caller did not
68378** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
68379** indicating a table b-tree, or if the caller did specify a KeyInfo
68380** structure the flags byte is set to 0x02 or 0x0A, indicating an index
68381** b-tree).
68382*/
68383static int moveToRoot(BtCursor *pCur){
68384 MemPage *pRoot;
68385 int rc = SQLITE_OK0;
68386
68387 assert( cursorOwnsBtShared(pCur) )((void) (0));
68388 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK )((void) (0));
68389 assert( CURSOR_VALID < CURSOR_REQUIRESEEK )((void) (0));
68390 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK )((void) (0));
68391 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 )((void) (0));
68392 assert( pCur->pgnoRoot>0 || pCur->iPage<0 )((void) (0));
68393
68394 if( pCur->iPage>=0 ){
68395 if( pCur->iPage ){
68396 releasePageNotNull(pCur->pPage);
68397 while( --pCur->iPage ){
68398 releasePageNotNull(pCur->apPage[pCur->iPage]);
68399 }
68400 pCur->pPage = pCur->apPage[0];
68401 goto skip_init;
68402 }
68403 }else if( pCur->pgnoRoot==0 ){
68404 pCur->eState = CURSOR_INVALID1;
68405 return SQLITE_EMPTY16;
68406 }else{
68407 assert( pCur->iPage==(-1) )((void) (0));
68408 if( pCur->eState>=CURSOR_REQUIRESEEK3 ){
68409 if( pCur->eState==CURSOR_FAULT4 ){
68410 assert( pCur->skipNext!=SQLITE_OK )((void) (0));
68411 return pCur->skipNext;
68412 }
68413 sqlite3BtreeClearCursor(pCur);
68414 }
68415 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
68416 0, pCur->curPagerFlags);
68417 if( rc!=SQLITE_OK0 ){
68418 pCur->eState = CURSOR_INVALID1;
68419 return rc;
68420 }
68421 pCur->iPage = 0;
68422 pCur->curIntKey = pCur->pPage->intKey;
68423 }
68424 pRoot = pCur->pPage;
68425 assert( pRoot->pgno==pCur->pgnoRoot )((void) (0));
68426
68427 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
68428 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
68429 ** NULL, the caller expects a table b-tree. If this is not the case,
68430 ** return an SQLITE_CORRUPT error.
68431 **
68432 ** Earlier versions of SQLite assumed that this test could not fail
68433 ** if the root page was already loaded when this function was called (i.e.
68434 ** if pCur->iPage>=0). But this is not so if the database is corrupted
68435 ** in such a way that page pRoot is linked into a second b-tree table
68436 ** (or the freelist). */
68437 assert( pRoot->intKey==1 || pRoot->intKey==0 )((void) (0));
68438 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
68439 return SQLITE_CORRUPT_PAGE(pCur->pPage)sqlite3CorruptError(68439);
68440 }
68441
68442skip_init:
68443 pCur->ix = 0;
68444 pCur->info.nSize = 0;
68445 pCur->curFlags &= ~(BTCF_AtLast0x08|BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
68446
68447 pRoot = pCur->pPage;
68448 if( pRoot->nCell>0 ){
68449 pCur->eState = CURSOR_VALID0;
68450 }else if( !pRoot->leaf ){
68451 Pgno subpage;
68452 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(68452);
68453 subpage = get4bytesqlite3Get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
68454 pCur->eState = CURSOR_VALID0;
68455 rc = moveToChild(pCur, subpage);
68456 }else{
68457 pCur->eState = CURSOR_INVALID1;
68458 rc = SQLITE_EMPTY16;
68459 }
68460 return rc;
68461}
68462
68463/*
68464** Move the cursor down to the left-most leaf entry beneath the
68465** entry to which it is currently pointing.
68466**
68467** The left-most leaf is the one with the smallest key - the first
68468** in ascending order.
68469*/
68470static int moveToLeftmost(BtCursor *pCur){
68471 Pgno pgno;
68472 int rc = SQLITE_OK0;
68473 MemPage *pPage;
68474
68475 assert( cursorOwnsBtShared(pCur) )((void) (0));
68476 assert( pCur->eState==CURSOR_VALID )((void) (0));
68477 while( rc==SQLITE_OK0 && !(pPage = pCur->pPage)->leaf ){
68478 assert( pCur->ix<pPage->nCell )((void) (0));
68479 pgno = get4bytesqlite3Get4byte(findCell(pPage, pCur->ix)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(pCur->ix)])[0]<<8 | (&(pPage)->
aCellIdx[2*(pCur->ix)])[1])))
);
68480 rc = moveToChild(pCur, pgno);
68481 }
68482 return rc;
68483}
68484
68485/*
68486** Move the cursor down to the right-most leaf entry beneath the
68487** page to which it is currently pointing. Notice the difference
68488** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
68489** finds the left-most entry beneath the *entry* whereas moveToRightmost()
68490** finds the right-most entry beneath the *page*.
68491**
68492** The right-most entry is the one with the largest key - the last
68493** key in ascending order.
68494*/
68495static int moveToRightmost(BtCursor *pCur){
68496 Pgno pgno;
68497 int rc = SQLITE_OK0;
68498 MemPage *pPage = 0;
68499
68500 assert( cursorOwnsBtShared(pCur) )((void) (0));
68501 assert( pCur->eState==CURSOR_VALID )((void) (0));
68502 while( !(pPage = pCur->pPage)->leaf ){
68503 pgno = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
68504 pCur->ix = pPage->nCell;
68505 rc = moveToChild(pCur, pgno);
68506 if( rc ) return rc;
68507 }
68508 pCur->ix = pPage->nCell-1;
68509 assert( pCur->info.nSize==0 )((void) (0));
68510 assert( (pCur->curFlags & BTCF_ValidNKey)==0 )((void) (0));
68511 return SQLITE_OK0;
68512}
68513
68514/* Move the cursor to the first entry in the table. Return SQLITE_OK
68515** on success. Set *pRes to 0 if the cursor actually points to something
68516** or set *pRes to 1 if the table is empty.
68517*/
68518SQLITE_PRIVATEstatic int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
68519 int rc;
68520
68521 assert( cursorOwnsBtShared(pCur) )((void) (0));
68522 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
68523 rc = moveToRoot(pCur);
68524 if( rc==SQLITE_OK0 ){
68525 assert( pCur->pPage->nCell>0 )((void) (0));
68526 *pRes = 0;
68527 rc = moveToLeftmost(pCur);
68528 }else if( rc==SQLITE_EMPTY16 ){
68529 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
68530 *pRes = 1;
68531 rc = SQLITE_OK0;
68532 }
68533 return rc;
68534}
68535
68536/* Move the cursor to the last entry in the table. Return SQLITE_OK
68537** on success. Set *pRes to 0 if the cursor actually points to something
68538** or set *pRes to 1 if the table is empty.
68539*/
68540SQLITE_PRIVATEstatic int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
68541 int rc;
68542
68543 assert( cursorOwnsBtShared(pCur) )((void) (0));
68544 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
68545
68546 /* If the cursor already points to the last entry, this is a no-op. */
68547 if( CURSOR_VALID0==pCur->eState && (pCur->curFlags & BTCF_AtLast0x08)!=0 ){
68548#ifdef SQLITE_DEBUG
68549 /* This block serves to assert() that the cursor really does point
68550 ** to the last entry in the b-tree. */
68551 int ii;
68552 for(ii=0; ii<pCur->iPage; ii++){
68553 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell )((void) (0));
68554 }
68555 assert( pCur->ix==pCur->pPage->nCell-1 )((void) (0));
68556 assert( pCur->pPage->leaf )((void) (0));
68557#endif
68558 *pRes = 0;
68559 return SQLITE_OK0;
68560 }
68561
68562 rc = moveToRoot(pCur);
68563 if( rc==SQLITE_OK0 ){
68564 assert( pCur->eState==CURSOR_VALID )((void) (0));
68565 *pRes = 0;
68566 rc = moveToRightmost(pCur);
68567 if( rc==SQLITE_OK0 ){
68568 pCur->curFlags |= BTCF_AtLast0x08;
68569 }else{
68570 pCur->curFlags &= ~BTCF_AtLast0x08;
68571 }
68572 }else if( rc==SQLITE_EMPTY16 ){
68573 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
68574 *pRes = 1;
68575 rc = SQLITE_OK0;
68576 }
68577 return rc;
68578}
68579
68580/* Move the cursor so that it points to an entry near the key
68581** specified by pIdxKey or intKey. Return a success code.
68582**
68583** For INTKEY tables, the intKey parameter is used. pIdxKey
68584** must be NULL. For index tables, pIdxKey is used and intKey
68585** is ignored.
68586**
68587** If an exact match is not found, then the cursor is always
68588** left pointing at a leaf page which would hold the entry if it
68589** were present. The cursor might point to an entry that comes
68590** before or after the key.
68591**
68592** An integer is written into *pRes which is the result of
68593** comparing the key with the entry to which the cursor is
68594** pointing. The meaning of the integer written into
68595** *pRes is as follows:
68596**
68597** *pRes<0 The cursor is left pointing at an entry that
68598** is smaller than intKey/pIdxKey or if the table is empty
68599** and the cursor is therefore left point to nothing.
68600**
68601** *pRes==0 The cursor is left pointing at an entry that
68602** exactly matches intKey/pIdxKey.
68603**
68604** *pRes>0 The cursor is left pointing at an entry that
68605** is larger than intKey/pIdxKey.
68606**
68607** For index tables, the pIdxKey->eqSeen field is set to 1 if there
68608** exists an entry in the table that exactly matches pIdxKey.
68609*/
68610SQLITE_PRIVATEstatic int sqlite3BtreeMovetoUnpacked(
68611 BtCursor *pCur, /* The cursor to be moved */
68612 UnpackedRecord *pIdxKey, /* Unpacked index key */
68613 i64 intKey, /* The table key */
68614 int biasRight, /* If true, bias the search to the high end */
68615 int *pRes /* Write search results here */
68616){
68617 int rc;
68618 RecordCompare xRecordCompare;
68619
68620 assert( cursorOwnsBtShared(pCur) )((void) (0));
68621 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
68622 assert( pRes )((void) (0));
68623 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) )((void) (0));
68624 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) )((void) (0));
68625
68626 /* If the cursor is already positioned at the point we are trying
68627 ** to move to, then just return without doing any work */
68628 if( pIdxKey==0
68629 && pCur->eState==CURSOR_VALID0 && (pCur->curFlags & BTCF_ValidNKey0x02)!=0
68630 ){
68631 if( pCur->info.nKey==intKey ){
68632 *pRes = 0;
68633 return SQLITE_OK0;
68634 }
68635 if( pCur->info.nKey<intKey ){
68636 if( (pCur->curFlags & BTCF_AtLast0x08)!=0 ){
68637 *pRes = -1;
68638 return SQLITE_OK0;
68639 }
68640 /* If the requested key is one more than the previous key, then
68641 ** try to get there using sqlite3BtreeNext() rather than a full
68642 ** binary search. This is an optimization only. The correct answer
68643 ** is still obtained without this case, only a little more slowely */
68644 if( pCur->info.nKey+1==intKey ){
68645 *pRes = 0;
68646 rc = sqlite3BtreeNext(pCur, 0);
68647 if( rc==SQLITE_OK0 ){
68648 getCellInfo(pCur);
68649 if( pCur->info.nKey==intKey ){
68650 return SQLITE_OK0;
68651 }
68652 }else if( rc==SQLITE_DONE101 ){
68653 rc = SQLITE_OK0;
Value stored to 'rc' is never read
68654 }else{
68655 return rc;
68656 }
68657 }
68658 }
68659 }
68660
68661 if( pIdxKey ){
68662 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
68663 pIdxKey->errCode = 0;
68664 assert( pIdxKey->default_rc==1((void) (0))
68665 || pIdxKey->default_rc==0((void) (0))
68666 || pIdxKey->default_rc==-1((void) (0))
68667 )((void) (0));
68668 }else{
68669 xRecordCompare = 0; /* All keys are integers */
68670 }
68671
68672 rc = moveToRoot(pCur);
68673 if( rc ){
68674 if( rc==SQLITE_EMPTY16 ){
68675 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 )((void) (0));
68676 *pRes = -1;
68677 return SQLITE_OK0;
68678 }
68679 return rc;
68680 }
68681 assert( pCur->pPage )((void) (0));
68682 assert( pCur->pPage->isInit )((void) (0));
68683 assert( pCur->eState==CURSOR_VALID )((void) (0));
68684 assert( pCur->pPage->nCell > 0 )((void) (0));
68685 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey )((void) (0));
68686 assert( pCur->curIntKey || pIdxKey )((void) (0));
68687 for(;;){
68688 int lwr, upr, idx, c;
68689 Pgno chldPg;
68690 MemPage *pPage = pCur->pPage;
68691 u8 *pCell; /* Pointer to current cell in pPage */
68692
68693 /* pPage->nCell must be greater than zero. If this is the root-page
68694 ** the cursor would have been INVALID above and this for(;;) loop
68695 ** not run. If this is not the root-page, then the moveToChild() routine
68696 ** would have already detected db corruption. Similarly, pPage must
68697 ** be the right kind (index or table) of b-tree page. Otherwise
68698 ** a moveToChild() or moveToRoot() call would have detected corruption. */
68699 assert( pPage->nCell>0 )((void) (0));
68700 assert( pPage->intKey==(pIdxKey==0) )((void) (0));
68701 lwr = 0;
68702 upr = pPage->nCell-1;
68703 assert( biasRight==0 || biasRight==1 )((void) (0));
68704 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
68705 pCur->ix = (u16)idx;
68706 if( xRecordCompare==0 ){
68707 for(;;){
68708 i64 nCellKey;
68709 pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
68710 if( pPage->intKeyLeaf ){
68711 while( 0x80 <= *(pCell++) ){
68712 if( pCell>=pPage->aDataEnd ){
68713 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68713);
68714 }
68715 }
68716 }
68717 getVarintsqlite3GetVarint(pCell, (u64*)&nCellKey);
68718 if( nCellKey<intKey ){
68719 lwr = idx+1;
68720 if( lwr>upr ){ c = -1; break; }
68721 }else if( nCellKey>intKey ){
68722 upr = idx-1;
68723 if( lwr>upr ){ c = +1; break; }
68724 }else{
68725 assert( nCellKey==intKey )((void) (0));
68726 pCur->ix = (u16)idx;
68727 if( !pPage->leaf ){
68728 lwr = idx;
68729 goto moveto_next_layer;
68730 }else{
68731 pCur->curFlags |= BTCF_ValidNKey0x02;
68732 pCur->info.nKey = nCellKey;
68733 pCur->info.nSize = 0;
68734 *pRes = 0;
68735 return SQLITE_OK0;
68736 }
68737 }
68738 assert( lwr+upr>=0 )((void) (0));
68739 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
68740 }
68741 }else{
68742 for(;;){
68743 int nCell; /* Size of the pCell cell in bytes */
68744 pCell = findCellPastPtr(pPage, idx)((pPage)->aDataOfst + ((pPage)->maskPage & ((&(
pPage)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(idx)])[1])))
;
68745
68746 /* The maximum supported page-size is 65536 bytes. This means that
68747 ** the maximum number of record bytes stored on an index B-Tree
68748 ** page is less than 16384 bytes and may be stored as a 2-byte
68749 ** varint. This information is used to attempt to avoid parsing
68750 ** the entire cell by checking for the cases where the record is
68751 ** stored entirely within the b-tree page by inspecting the first
68752 ** 2 bytes of the cell.
68753 */
68754 nCell = pCell[0];
68755 if( nCell<=pPage->max1bytePayload ){
68756 /* This branch runs if the record-size field of the cell is a
68757 ** single byte varint and the record fits entirely on the main
68758 ** b-tree page. */
68759 testcase( pCell+nCell+1==pPage->aDataEnd );
68760 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
68761 }else if( !(pCell[1] & 0x80)
68762 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
68763 ){
68764 /* The record-size field is a 2 byte varint and the record
68765 ** fits entirely on the main b-tree page. */
68766 testcase( pCell+nCell+2==pPage->aDataEnd );
68767 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
68768 }else{
68769 /* The record flows over onto one or more overflow pages. In
68770 ** this case the whole cell needs to be parsed, a buffer allocated
68771 ** and accessPayload() used to retrieve the record into the
68772 ** buffer before VdbeRecordCompare() can be called.
68773 **
68774 ** If the record is corrupt, the xRecordCompare routine may read
68775 ** up to two varints past the end of the buffer. An extra 18
68776 ** bytes of padding is allocated at the end of the buffer in
68777 ** case this happens. */
68778 void *pCellKey;
68779 u8 * const pCellBody = pCell - pPage->childPtrSize;
68780 const int nOverrun = 18; /* Size of the overrun padding */
68781 pPage->xParseCell(pPage, pCellBody, &pCur->info);
68782 nCell = (int)pCur->info.nKey;
68783 testcase( nCell<0 ); /* True if key size is 2^32 or more */
68784 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
68785 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
68786 testcase( nCell==2 ); /* Minimum legal index key size */
68787 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
68788 rc = SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(68788);
68789 goto moveto_finish;
68790 }
68791 pCellKey = sqlite3Malloc( nCell+nOverrun );
68792 if( pCellKey==0 ){
68793 rc = SQLITE_NOMEM_BKPT7;
68794 goto moveto_finish;
68795 }
68796 pCur->ix = (u16)idx;
68797 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
68798 memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */
68799 pCur->curFlags &= ~BTCF_ValidOvfl0x04;
68800 if( rc ){
68801 sqlite3_free(pCellKey);
68802 goto moveto_finish;
68803 }
68804 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
68805 sqlite3_free(pCellKey);
68806 }
68807 assert(((void) (0))
68808 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)((void) (0))
68809 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)((void) (0))
68810 )((void) (0));
68811 if( c<0 ){
68812 lwr = idx+1;
68813 }else if( c>0 ){
68814 upr = idx-1;
68815 }else{
68816 assert( c==0 )((void) (0));
68817 *pRes = 0;
68818 rc = SQLITE_OK0;
68819 pCur->ix = (u16)idx;
68820 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(68820);
68821 goto moveto_finish;
68822 }
68823 if( lwr>upr ) break;
68824 assert( lwr+upr>=0 )((void) (0));
68825 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
68826 }
68827 }
68828 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) )((void) (0));
68829 assert( pPage->isInit )((void) (0));
68830 if( pPage->leaf ){
68831 assert( pCur->ix<pCur->pPage->nCell )((void) (0));
68832 pCur->ix = (u16)idx;
68833 *pRes = c;
68834 rc = SQLITE_OK0;
68835 goto moveto_finish;
68836 }
68837moveto_next_layer:
68838 if( lwr>=pPage->nCell ){
68839 chldPg = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
68840 }else{
68841 chldPg = get4bytesqlite3Get4byte(findCell(pPage, lwr)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(lwr)])[0]<<8 | (&(pPage)->aCellIdx
[2*(lwr)])[1])))
);
68842 }
68843 pCur->ix = (u16)lwr;
68844 rc = moveToChild(pCur, chldPg);
68845 if( rc ) break;
68846 }
68847moveto_finish:
68848 pCur->info.nSize = 0;
68849 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
68850 return rc;
68851}
68852
68853
68854/*
68855** Return TRUE if the cursor is not pointing at an entry of the table.
68856**
68857** TRUE will be returned after a call to sqlite3BtreeNext() moves
68858** past the last entry in the table or sqlite3BtreePrev() moves past
68859** the first entry. TRUE is also returned if the table is empty.
68860*/
68861SQLITE_PRIVATEstatic int sqlite3BtreeEof(BtCursor *pCur){
68862 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
68863 ** have been deleted? This API will need to change to return an error code
68864 ** as well as the boolean result value.
68865 */
68866 return (CURSOR_VALID0!=pCur->eState);
68867}
68868
68869/*
68870** Return an estimate for the number of rows in the table that pCur is
68871** pointing to. Return a negative number if no estimate is currently
68872** available.
68873*/
68874SQLITE_PRIVATEstatic i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
68875 i64 n;
68876 u8 i;
68877
68878 assert( cursorOwnsBtShared(pCur) )((void) (0));
68879 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) )((void) (0));
68880
68881 /* Currently this interface is only called by the OP_IfSmaller
68882 ** opcode, and it that case the cursor will always be valid and
68883 ** will always point to a leaf node. */
68884 if( NEVER(pCur->eState!=CURSOR_VALID)(pCur->eState!=0) ) return -1;
68885 if( NEVER(pCur->pPage->leaf==0)(pCur->pPage->leaf==0) ) return -1;
68886
68887 n = pCur->pPage->nCell;
68888 for(i=0; i<pCur->iPage; i++){
68889 n *= pCur->apPage[i]->nCell;
68890 }
68891 return n;
68892}
68893
68894/*
68895** Advance the cursor to the next entry in the database.
68896** Return value:
68897**
68898** SQLITE_OK success
68899** SQLITE_DONE cursor is already pointing at the last element
68900** otherwise some kind of error occurred
68901**
68902** The main entry point is sqlite3BtreeNext(). That routine is optimized
68903** for the common case of merely incrementing the cell counter BtCursor.aiIdx
68904** to the next cell on the current page. The (slower) btreeNext() helper
68905** routine is called when it is necessary to move to a different page or
68906** to restore the cursor.
68907**
68908** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
68909** cursor corresponds to an SQL index and this routine could have been
68910** skipped if the SQL index had been a unique index. The F argument
68911** is a hint to the implement. SQLite btree implementation does not use
68912** this hint, but COMDB2 does.
68913*/
68914static SQLITE_NOINLINE__attribute__((noinline)) int btreeNext(BtCursor *pCur){
68915 int rc;
68916 int idx;
68917 MemPage *pPage;
68918
68919 assert( cursorOwnsBtShared(pCur) )((void) (0));
68920 if( pCur->eState!=CURSOR_VALID0 ){
68921 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 )((void) (0));
68922 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
68923 if( rc!=SQLITE_OK0 ){
68924 return rc;
68925 }
68926 if( CURSOR_INVALID1==pCur->eState ){
68927 return SQLITE_DONE101;
68928 }
68929 if( pCur->eState==CURSOR_SKIPNEXT2 ){
68930 pCur->eState = CURSOR_VALID0;
68931 if( pCur->skipNext>0 ) return SQLITE_OK0;
68932 }
68933 }
68934
68935 pPage = pCur->pPage;
68936 idx = ++pCur->ix;
68937 if( !pPage->isInit ){
68938 /* The only known way for this to happen is for there to be a
68939 ** recursive SQL function that does a DELETE operation as part of a
68940 ** SELECT which deletes content out from under an active cursor
68941 ** in a corrupt database file where the table being DELETE-ed from
68942 ** has pages in common with the table being queried. See TH3
68943 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
68944 ** example. */
68945 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(68945);
68946 }
68947
68948 /* If the database file is corrupt, it is possible for the value of idx
68949 ** to be invalid here. This can only occur if a second cursor modifies
68950 ** the page while cursor pCur is holding a reference to it. Which can
68951 ** only happen if the database is corrupt in such a way as to link the
68952 ** page into more than one b-tree structure. */
68953 testcase( idx>pPage->nCell );
68954
68955 if( idx>=pPage->nCell ){
68956 if( !pPage->leaf ){
68957 rc = moveToChild(pCur, get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
68958 if( rc ) return rc;
68959 return moveToLeftmost(pCur);
68960 }
68961 do{
68962 if( pCur->iPage==0 ){
68963 pCur->eState = CURSOR_INVALID1;
68964 return SQLITE_DONE101;
68965 }
68966 moveToParent(pCur);
68967 pPage = pCur->pPage;
68968 }while( pCur->ix>=pPage->nCell );
68969 if( pPage->intKey ){
68970 return sqlite3BtreeNext(pCur, 0);
68971 }else{
68972 return SQLITE_OK0;
68973 }
68974 }
68975 if( pPage->leaf ){
68976 return SQLITE_OK0;
68977 }else{
68978 return moveToLeftmost(pCur);
68979 }
68980}
68981SQLITE_PRIVATEstatic int sqlite3BtreeNext(BtCursor *pCur, int flags){
68982 MemPage *pPage;
68983 UNUSED_PARAMETER( flags )(void)(flags); /* Used in COMDB2 but not native SQLite */
68984 assert( cursorOwnsBtShared(pCur) )((void) (0));
68985 assert( flags==0 || flags==1 )((void) (0));
68986 pCur->info.nSize = 0;
68987 pCur->curFlags &= ~(BTCF_ValidNKey0x02|BTCF_ValidOvfl0x04);
68988 if( pCur->eState!=CURSOR_VALID0 ) return btreeNext(pCur);
68989 pPage = pCur->pPage;
68990 if( (++pCur->ix)>=pPage->nCell ){
68991 pCur->ix--;
68992 return btreeNext(pCur);
68993 }
68994 if( pPage->leaf ){
68995 return SQLITE_OK0;
68996 }else{
68997 return moveToLeftmost(pCur);
68998 }
68999}
69000
69001/*
69002** Step the cursor to the back to the previous entry in the database.
69003** Return values:
69004**
69005** SQLITE_OK success
69006** SQLITE_DONE the cursor is already on the first element of the table
69007** otherwise some kind of error occurred
69008**
69009** The main entry point is sqlite3BtreePrevious(). That routine is optimized
69010** for the common case of merely decrementing the cell counter BtCursor.aiIdx
69011** to the previous cell on the current page. The (slower) btreePrevious()
69012** helper routine is called when it is necessary to move to a different page
69013** or to restore the cursor.
69014**
69015** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
69016** the cursor corresponds to an SQL index and this routine could have been
69017** skipped if the SQL index had been a unique index. The F argument is a
69018** hint to the implement. The native SQLite btree implementation does not
69019** use this hint, but COMDB2 does.
69020*/
69021static SQLITE_NOINLINE__attribute__((noinline)) int btreePrevious(BtCursor *pCur){
69022 int rc;
69023 MemPage *pPage;
69024
69025 assert( cursorOwnsBtShared(pCur) )((void) (0));
69026 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 )((void) (0));
69027 assert( pCur->info.nSize==0 )((void) (0));
69028 if( pCur->eState!=CURSOR_VALID0 ){
69029 rc = restoreCursorPosition(pCur)(pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0
)
;
69030 if( rc!=SQLITE_OK0 ){
69031 return rc;
69032 }
69033 if( CURSOR_INVALID1==pCur->eState ){
69034 return SQLITE_DONE101;
69035 }
69036 if( CURSOR_SKIPNEXT2==pCur->eState ){
69037 pCur->eState = CURSOR_VALID0;
69038 if( pCur->skipNext<0 ) return SQLITE_OK0;
69039 }
69040 }
69041
69042 pPage = pCur->pPage;
69043 assert( pPage->isInit )((void) (0));
69044 if( !pPage->leaf ){
69045 int idx = pCur->ix;
69046 rc = moveToChild(pCur, get4bytesqlite3Get4byte(findCell(pPage, idx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(idx)])[1])))
));
69047 if( rc ) return rc;
69048 rc = moveToRightmost(pCur);
69049 }else{
69050 while( pCur->ix==0 ){
69051 if( pCur->iPage==0 ){
69052 pCur->eState = CURSOR_INVALID1;
69053 return SQLITE_DONE101;
69054 }
69055 moveToParent(pCur);
69056 }
69057 assert( pCur->info.nSize==0 )((void) (0));
69058 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 )((void) (0));
69059
69060 pCur->ix--;
69061 pPage = pCur->pPage;
69062 if( pPage->intKey && !pPage->leaf ){
69063 rc = sqlite3BtreePrevious(pCur, 0);
69064 }else{
69065 rc = SQLITE_OK0;
69066 }
69067 }
69068 return rc;
69069}
69070SQLITE_PRIVATEstatic int sqlite3BtreePrevious(BtCursor *pCur, int flags){
69071 assert( cursorOwnsBtShared(pCur) )((void) (0));
69072 assert( flags==0 || flags==1 )((void) (0));
69073 UNUSED_PARAMETER( flags )(void)(flags); /* Used in COMDB2 but not native SQLite */
69074 pCur->curFlags &= ~(BTCF_AtLast0x08|BTCF_ValidOvfl0x04|BTCF_ValidNKey0x02);
69075 pCur->info.nSize = 0;
69076 if( pCur->eState!=CURSOR_VALID0
69077 || pCur->ix==0
69078 || pCur->pPage->leaf==0
69079 ){
69080 return btreePrevious(pCur);
69081 }
69082 pCur->ix--;
69083 return SQLITE_OK0;
69084}
69085
69086/*
69087** Allocate a new page from the database file.
69088**
69089** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
69090** has already been called on the new page.) The new page has also
69091** been referenced and the calling routine is responsible for calling
69092** sqlite3PagerUnref() on the new page when it is done.
69093**
69094** SQLITE_OK is returned on success. Any other return value indicates
69095** an error. *ppPage is set to NULL in the event of an error.
69096**
69097** If the "nearby" parameter is not 0, then an effort is made to
69098** locate a page close to the page number "nearby". This can be used in an
69099** attempt to keep related pages close to each other in the database file,
69100** which in turn can make database access faster.
69101**
69102** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
69103** anywhere on the free-list, then it is guaranteed to be returned. If
69104** eMode is BTALLOC_LT then the page returned will be less than or equal
69105** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
69106** are no restrictions on which page is returned.
69107*/
69108static int allocateBtreePage(
69109 BtShared *pBt, /* The btree */
69110 MemPage **ppPage, /* Store pointer to the allocated page here */
69111 Pgno *pPgno, /* Store the page number here */
69112 Pgno nearby, /* Search for a page near this one */
69113 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
69114){
69115 MemPage *pPage1;
69116 int rc;
69117 u32 n; /* Number of pages on the freelist */
69118 u32 k; /* Number of leaves on the trunk of the freelist */
69119 MemPage *pTrunk = 0;
69120 MemPage *pPrevTrunk = 0;
69121 Pgno mxPage; /* Total size of the database file */
69122
69123 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
69124 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) )((void) (0));
69125 pPage1 = pBt->pPage1;
69126 mxPage = btreePagecount(pBt);
69127 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
69128 ** stores stores the total number of pages on the freelist. */
69129 n = get4bytesqlite3Get4byte(&pPage1->aData[36]);
69130 testcase( n==mxPage-1 );
69131 if( n>=mxPage ){
69132 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(69132);
69133 }
69134 if( n>0 ){
69135 /* There are pages on the freelist. Reuse one of those pages. */
69136 Pgno iTrunk;
69137 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
69138 u32 nSearch = 0; /* Count of the number of search attempts */
69139
69140 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
69141 ** shows that the page 'nearby' is somewhere on the free-list, then
69142 ** the entire-list will be searched for that page.
69143 */
69144#ifndef SQLITE_OMIT_AUTOVACUUM
69145 if( eMode==BTALLOC_EXACT1 ){
69146 if( nearby<=mxPage ){
69147 u8 eType;
69148 assert( nearby>0 )((void) (0));
69149 assert( pBt->autoVacuum )((void) (0));
69150 rc = ptrmapGet(pBt, nearby, &eType, 0);
69151 if( rc ) return rc;
69152 if( eType==PTRMAP_FREEPAGE2 ){
69153 searchList = 1;
69154 }
69155 }
69156 }else if( eMode==BTALLOC_LE2 ){
69157 searchList = 1;
69158 }
69159#endif
69160
69161 /* Decrement the free-list count by 1. Set iTrunk to the index of the
69162 ** first free-list trunk page. iPrevTrunk is initially 1.
69163 */
69164 rc = sqlite3PagerWrite(pPage1->pDbPage);
69165 if( rc ) return rc;
69166 put4bytesqlite3Put4byte(&pPage1->aData[36], n-1);
69167
69168 /* The code within this loop is run only once if the 'searchList' variable
69169 ** is not true. Otherwise, it runs once for each trunk-page on the
69170 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
69171 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
69172 */
69173 do {
69174 pPrevTrunk = pTrunk;
69175 if( pPrevTrunk ){
69176 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
69177 ** is the page number of the next freelist trunk page in the list or
69178 ** zero if this is the last freelist trunk page. */
69179 iTrunk = get4bytesqlite3Get4byte(&pPrevTrunk->aData[0]);
69180 }else{
69181 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
69182 ** stores the page number of the first page of the freelist, or zero if
69183 ** the freelist is empty. */
69184 iTrunk = get4bytesqlite3Get4byte(&pPage1->aData[32]);
69185 }
69186 testcase( iTrunk==mxPage );
69187 if( iTrunk>mxPage || nSearch++ > n ){
69188 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1)sqlite3CorruptError(69188);
69189 }else{
69190 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
69191 }
69192 if( rc ){
69193 pTrunk = 0;
69194 goto end_allocate_page;
69195 }
69196 assert( pTrunk!=0 )((void) (0));
69197 assert( pTrunk->aData!=0 )((void) (0));
69198 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
69199 ** is the number of leaf page pointers to follow. */
69200 k = get4bytesqlite3Get4byte(&pTrunk->aData[4]);
69201 if( k==0 && !searchList ){
69202 /* The trunk has no leaves and the list is not being searched.
69203 ** So extract the trunk page itself and use it as the newly
69204 ** allocated page */
69205 assert( pPrevTrunk==0 )((void) (0));
69206 rc = sqlite3PagerWrite(pTrunk->pDbPage);
69207 if( rc ){
69208 goto end_allocate_page;
69209 }
69210 *pPgno = iTrunk;
69211 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
69212 *ppPage = pTrunk;
69213 pTrunk = 0;
69214 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
69215 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
69216 /* Value of k is out of range. Database corruption */
69217 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(69217);
69218 goto end_allocate_page;
69219#ifndef SQLITE_OMIT_AUTOVACUUM
69220 }else if( searchList
69221 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE2))
69222 ){
69223 /* The list is being searched and this trunk page is the page
69224 ** to allocate, regardless of whether it has leaves.
69225 */
69226 *pPgno = iTrunk;
69227 *ppPage = pTrunk;
69228 searchList = 0;
69229 rc = sqlite3PagerWrite(pTrunk->pDbPage);
69230 if( rc ){
69231 goto end_allocate_page;
69232 }
69233 if( k==0 ){
69234 if( !pPrevTrunk ){
69235 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
69236 }else{
69237 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
69238 if( rc!=SQLITE_OK0 ){
69239 goto end_allocate_page;
69240 }
69241 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
69242 }
69243 }else{
69244 /* The trunk page is required by the caller but it contains
69245 ** pointers to free-list leaves. The first leaf becomes a trunk
69246 ** page in this case.
69247 */
69248 MemPage *pNewTrunk;
69249 Pgno iNewTrunk = get4bytesqlite3Get4byte(&pTrunk->aData[8]);
69250 if( iNewTrunk>mxPage ){
69251 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(69251);
69252 goto end_allocate_page;
69253 }
69254 testcase( iNewTrunk==mxPage );
69255 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
69256 if( rc!=SQLITE_OK0 ){
69257 goto end_allocate_page;
69258 }
69259 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
69260 if( rc!=SQLITE_OK0 ){
69261 releasePage(pNewTrunk);
69262 goto end_allocate_page;
69263 }
69264 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
69265 put4bytesqlite3Put4byte(&pNewTrunk->aData[4], k-1);
69266 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
69267 releasePage(pNewTrunk);
69268 if( !pPrevTrunk ){
69269 assert( sqlite3PagerIswriteable(pPage1->pDbPage) )((void) (0));
69270 put4bytesqlite3Put4byte(&pPage1->aData[32], iNewTrunk);
69271 }else{
69272 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
69273 if( rc ){
69274 goto end_allocate_page;
69275 }
69276 put4bytesqlite3Put4byte(&pPrevTrunk->aData[0], iNewTrunk);
69277 }
69278 }
69279 pTrunk = 0;
69280 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
69281#endif
69282 }else if( k>0 ){
69283 /* Extract a leaf from the trunk */
69284 u32 closest;
69285 Pgno iPage;
69286 unsigned char *aData = pTrunk->aData;
69287 if( nearby>0 ){
69288 u32 i;
69289 closest = 0;
69290 if( eMode==BTALLOC_LE2 ){
69291 for(i=0; i<k; i++){
69292 iPage = get4bytesqlite3Get4byte(&aData[8+i*4]);
69293 if( iPage<=nearby ){
69294 closest = i;
69295 break;
69296 }
69297 }
69298 }else{
69299 int dist;
69300 dist = sqlite3AbsInt32(get4bytesqlite3Get4byte(&aData[8]) - nearby);
69301 for(i=1; i<k; i++){
69302 int d2 = sqlite3AbsInt32(get4bytesqlite3Get4byte(&aData[8+i*4]) - nearby);
69303 if( d2<dist ){
69304 closest = i;
69305 dist = d2;
69306 }
69307 }
69308 }
69309 }else{
69310 closest = 0;
69311 }
69312
69313 iPage = get4bytesqlite3Get4byte(&aData[8+closest*4]);
69314 testcase( iPage==mxPage );
69315 if( iPage>mxPage ){
69316 rc = SQLITE_CORRUPT_PGNO(iTrunk)sqlite3CorruptError(69316);
69317 goto end_allocate_page;
69318 }
69319 testcase( iPage==mxPage );
69320 if( !searchList
69321 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE2))
69322 ){
69323 int noContent;
69324 *pPgno = iPage;
69325 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
69326 ": %d more free pages\n",
69327 *pPgno, closest+1, k, pTrunk->pgno, n-1));
69328 rc = sqlite3PagerWrite(pTrunk->pDbPage);
69329 if( rc ) goto end_allocate_page;
69330 if( closest<k-1 ){
69331 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
69332 }
69333 put4bytesqlite3Put4byte(&aData[4], k-1);
69334 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT0x01 : 0;
69335 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
69336 if( rc==SQLITE_OK0 ){
69337 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
69338 if( rc!=SQLITE_OK0 ){
69339 releasePage(*ppPage);
69340 *ppPage = 0;
69341 }
69342 }
69343 searchList = 0;
69344 }
69345 }
69346 releasePage(pPrevTrunk);
69347 pPrevTrunk = 0;
69348 }while( searchList );
69349 }else{
69350 /* There are no pages on the freelist, so append a new page to the
69351 ** database image.
69352 **
69353 ** Normally, new pages allocated by this block can be requested from the
69354 ** pager layer with the 'no-content' flag set. This prevents the pager
69355 ** from trying to read the pages content from disk. However, if the
69356 ** current transaction has already run one or more incremental-vacuum
69357 ** steps, then the page we are about to allocate may contain content
69358 ** that is required in the event of a rollback. In this case, do
69359 ** not set the no-content flag. This causes the pager to load and journal
69360 ** the current page content before overwriting it.
69361 **
69362 ** Note that the pager will not actually attempt to load or journal
69363 ** content for any page that really does lie past the end of the database
69364 ** file on disk. So the effects of disabling the no-content optimization
69365 ** here are confined to those pages that lie between the end of the
69366 ** database image and the end of the database file.
69367 */
69368 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)(pBt->bDoTruncate))? PAGER_GET_NOCONTENT0x01:0;
69369
69370 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
69371 if( rc ) return rc;
69372 pBt->nPage++;
69373 if( pBt->nPage==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ) pBt->nPage++;
69374
69375#ifndef SQLITE_OMIT_AUTOVACUUM
69376 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage)(ptrmapPageno((pBt), (pBt->nPage))==(pBt->nPage)) ){
69377 /* If *pPgno refers to a pointer-map page, allocate two new pages
69378 ** at the end of the file instead of one. The first allocated page
69379 ** becomes a new pointer-map page, the second is used by the caller.
69380 */
69381 MemPage *pPg = 0;
69382 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
69383 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) )((void) (0));
69384 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
69385 if( rc==SQLITE_OK0 ){
69386 rc = sqlite3PagerWrite(pPg->pDbPage);
69387 releasePage(pPg);
69388 }
69389 if( rc ) return rc;
69390 pBt->nPage++;
69391 if( pBt->nPage==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){ pBt->nPage++; }
69392 }
69393#endif
69394 put4bytesqlite3Put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
69395 *pPgno = pBt->nPage;
69396
69397 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
69398 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
69399 if( rc ) return rc;
69400 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
69401 if( rc!=SQLITE_OK0 ){
69402 releasePage(*ppPage);
69403 *ppPage = 0;
69404 }
69405 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
69406 }
69407
69408 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
69409
69410end_allocate_page:
69411 releasePage(pTrunk);
69412 releasePage(pPrevTrunk);
69413 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 )((void) (0));
69414 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 )((void) (0));
69415 return rc;
69416}
69417
69418/*
69419** This function is used to add page iPage to the database file free-list.
69420** It is assumed that the page is not already a part of the free-list.
69421**
69422** The value passed as the second argument to this function is optional.
69423** If the caller happens to have a pointer to the MemPage object
69424** corresponding to page iPage handy, it may pass it as the second value.
69425** Otherwise, it may pass NULL.
69426**
69427** If a pointer to a MemPage object is passed as the second argument,
69428** its reference count is not altered by this function.
69429*/
69430static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
69431 MemPage *pTrunk = 0; /* Free-list trunk page */
69432 Pgno iTrunk = 0; /* Page number of free-list trunk page */
69433 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
69434 MemPage *pPage; /* Page being freed. May be NULL. */
69435 int rc; /* Return Code */
69436 u32 nFree; /* Initial number of pages on free-list */
69437
69438 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
69439 assert( CORRUPT_DB || iPage>1 )((void) (0));
69440 assert( !pMemPage || pMemPage->pgno==iPage )((void) (0));
69441
69442 if( iPage<2 || iPage>pBt->nPage ){
69443 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(69443);
69444 }
69445 if( pMemPage ){
69446 pPage = pMemPage;
69447 sqlite3PagerRef(pPage->pDbPage);
69448 }else{
69449 pPage = btreePageLookup(pBt, iPage);
69450 }
69451
69452 /* Increment the free page count on pPage1 */
69453 rc = sqlite3PagerWrite(pPage1->pDbPage);
69454 if( rc ) goto freepage_out;
69455 nFree = get4bytesqlite3Get4byte(&pPage1->aData[36]);
69456 put4bytesqlite3Put4byte(&pPage1->aData[36], nFree+1);
69457
69458 if( pBt->btsFlags & BTS_SECURE_DELETE0x0004 ){
69459 /* If the secure_delete option is enabled, then
69460 ** always fully overwrite deleted information with zeros.
69461 */
69462 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
69463 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
69464 ){
69465 goto freepage_out;
69466 }
69467 memset(pPage->aData, 0, pPage->pBt->pageSize);
69468 }
69469
69470 /* If the database supports auto-vacuum, write an entry in the pointer-map
69471 ** to indicate that the page is free.
69472 */
69473 if( ISAUTOVACUUM(pBt->autoVacuum) ){
69474 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE2, 0, &rc);
69475 if( rc ) goto freepage_out;
69476 }
69477
69478 /* Now manipulate the actual database free-list structure. There are two
69479 ** possibilities. If the free-list is currently empty, or if the first
69480 ** trunk page in the free-list is full, then this page will become a
69481 ** new free-list trunk page. Otherwise, it will become a leaf of the
69482 ** first trunk page in the current free-list. This block tests if it
69483 ** is possible to add the page as a new free-list leaf.
69484 */
69485 if( nFree!=0 ){
69486 u32 nLeaf; /* Initial number of leaf cells on trunk page */
69487
69488 iTrunk = get4bytesqlite3Get4byte(&pPage1->aData[32]);
69489 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
69490 if( rc!=SQLITE_OK0 ){
69491 goto freepage_out;
69492 }
69493
69494 nLeaf = get4bytesqlite3Get4byte(&pTrunk->aData[4]);
69495 assert( pBt->usableSize>32 )((void) (0));
69496 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
69497 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(69497);
69498 goto freepage_out;
69499 }
69500 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
69501 /* In this case there is room on the trunk page to insert the page
69502 ** being freed as a new leaf.
69503 **
69504 ** Note that the trunk page is not really full until it contains
69505 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
69506 ** coded. But due to a coding error in versions of SQLite prior to
69507 ** 3.6.0, databases with freelist trunk pages holding more than
69508 ** usableSize/4 - 8 entries will be reported as corrupt. In order
69509 ** to maintain backwards compatibility with older versions of SQLite,
69510 ** we will continue to restrict the number of entries to usableSize/4 - 8
69511 ** for now. At some point in the future (once everyone has upgraded
69512 ** to 3.6.0 or later) we should consider fixing the conditional above
69513 ** to read "usableSize/4-2" instead of "usableSize/4-8".
69514 **
69515 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
69516 ** avoid using the last six entries in the freelist trunk page array in
69517 ** order that database files created by newer versions of SQLite can be
69518 ** read by older versions of SQLite.
69519 */
69520 rc = sqlite3PagerWrite(pTrunk->pDbPage);
69521 if( rc==SQLITE_OK0 ){
69522 put4bytesqlite3Put4byte(&pTrunk->aData[4], nLeaf+1);
69523 put4bytesqlite3Put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
69524 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE0x0004)==0 ){
69525 sqlite3PagerDontWrite(pPage->pDbPage);
69526 }
69527 rc = btreeSetHasContent(pBt, iPage);
69528 }
69529 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
69530 goto freepage_out;
69531 }
69532 }
69533
69534 /* If control flows to this point, then it was not possible to add the
69535 ** the page being freed as a leaf page of the first trunk in the free-list.
69536 ** Possibly because the free-list is empty, or possibly because the
69537 ** first trunk in the free-list is full. Either way, the page being freed
69538 ** will become the new first trunk page in the free-list.
69539 */
69540 if( pPage==0 && SQLITE_OK0!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
69541 goto freepage_out;
69542 }
69543 rc = sqlite3PagerWrite(pPage->pDbPage);
69544 if( rc!=SQLITE_OK0 ){
69545 goto freepage_out;
69546 }
69547 put4bytesqlite3Put4byte(pPage->aData, iTrunk);
69548 put4bytesqlite3Put4byte(&pPage->aData[4], 0);
69549 put4bytesqlite3Put4byte(&pPage1->aData[32], iPage);
69550 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
69551
69552freepage_out:
69553 if( pPage ){
69554 pPage->isInit = 0;
69555 }
69556 releasePage(pPage);
69557 releasePage(pTrunk);
69558 return rc;
69559}
69560static void freePage(MemPage *pPage, int *pRC){
69561 if( (*pRC)==SQLITE_OK0 ){
69562 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
69563 }
69564}
69565
69566/*
69567** Free any overflow pages associated with the given Cell. Store
69568** size information about the cell in pInfo.
69569*/
69570static int clearCell(
69571 MemPage *pPage, /* The page that contains the Cell */
69572 unsigned char *pCell, /* First byte of the Cell */
69573 CellInfo *pInfo /* Size information about the cell */
69574){
69575 BtShared *pBt;
69576 Pgno ovflPgno;
69577 int rc;
69578 int nOvfl;
69579 u32 ovflPageSize;
69580
69581 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
69582 pPage->xParseCell(pPage, pCell, pInfo);
69583 if( pInfo->nLocal==pInfo->nPayload ){
69584 return SQLITE_OK0; /* No overflow pages. Return without doing anything */
69585 }
69586 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
69587 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
69588 if( pCell + pInfo->nSize > pPage->aDataEnd ){
69589 /* Cell extends past end of page */
69590 return SQLITE_CORRUPT_PAGE(pPage)sqlite3CorruptError(69590);
69591 }
69592 ovflPgno = get4bytesqlite3Get4byte(pCell + pInfo->nSize - 4);
69593 pBt = pPage->pBt;
69594 assert( pBt->usableSize > 4 )((void) (0));
69595 ovflPageSize = pBt->usableSize - 4;
69596 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
69597 assert( nOvfl>0 ||((void) (0))
69598 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)((void) (0))
69599 )((void) (0));
69600 while( nOvfl-- ){
69601 Pgno iNext = 0;
69602 MemPage *pOvfl = 0;
69603 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
69604 /* 0 is not a legal page number and page 1 cannot be an
69605 ** overflow page. Therefore if ovflPgno<2 or past the end of the
69606 ** file the database must be corrupt. */
69607 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(69607);
69608 }
69609 if( nOvfl ){
69610 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
69611 if( rc ) return rc;
69612 }
69613
69614 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
69615 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
69616 ){
69617 /* There is no reason any cursor should have an outstanding reference
69618 ** to an overflow page belonging to a cell that is being deleted/updated.
69619 ** So if there exists more than one reference to this page, then it
69620 ** must not really be an overflow page and the database must be corrupt.
69621 ** It is helpful to detect this before calling freePage2(), as
69622 ** freePage2() may zero the page contents if secure-delete mode is
69623 ** enabled. If this 'overflow' page happens to be a page that the
69624 ** caller is iterating through or using in some other way, this
69625 ** can be problematic.
69626 */
69627 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(69627);
69628 }else{
69629 rc = freePage2(pBt, pOvfl, ovflPgno);
69630 }
69631
69632 if( pOvfl ){
69633 sqlite3PagerUnref(pOvfl->pDbPage);
69634 }
69635 if( rc ) return rc;
69636 ovflPgno = iNext;
69637 }
69638 return SQLITE_OK0;
69639}
69640
69641/*
69642** Create the byte sequence used to represent a cell on page pPage
69643** and write that byte sequence into pCell[]. Overflow pages are
69644** allocated and filled in as necessary. The calling procedure
69645** is responsible for making sure sufficient space has been allocated
69646** for pCell[].
69647**
69648** Note that pCell does not necessary need to point to the pPage->aData
69649** area. pCell might point to some temporary storage. The cell will
69650** be constructed in this temporary area then copied into pPage->aData
69651** later.
69652*/
69653static int fillInCell(
69654 MemPage *pPage, /* The page that contains the cell */
69655 unsigned char *pCell, /* Complete text of the cell */
69656 const BtreePayload *pX, /* Payload with which to construct the cell */
69657 int *pnSize /* Write cell size here */
69658){
69659 int nPayload;
69660 const u8 *pSrc;
69661 int nSrc, n, rc, mn;
69662 int spaceLeft;
69663 MemPage *pToRelease;
69664 unsigned char *pPrior;
69665 unsigned char *pPayload;
69666 BtShared *pBt;
69667 Pgno pgnoOvfl;
69668 int nHeader;
69669
69670 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
69671
69672 /* pPage is not necessarily writeable since pCell might be auxiliary
69673 ** buffer space that is separate from the pPage buffer area */
69674 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]((void) (0))
69675 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
69676
69677 /* Fill in the header. */
69678 nHeader = pPage->childPtrSize;
69679 if( pPage->intKey ){
69680 nPayload = pX->nData + pX->nZero;
69681 pSrc = pX->pData;
69682 nSrc = pX->nData;
69683 assert( pPage->intKeyLeaf )((void) (0)); /* fillInCell() only called for leaves */
69684 nHeader += putVarint32(&pCell[nHeader], nPayload)(u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(
unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader
]),(nPayload)))
;
69685 nHeader += putVarintsqlite3PutVarint(&pCell[nHeader], *(u64*)&pX->nKey);
69686 }else{
69687 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 )((void) (0));
69688 nSrc = nPayload = (int)pX->nKey;
69689 pSrc = pX->pKey;
69690 nHeader += putVarint32(&pCell[nHeader], nPayload)(u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(
unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader
]),(nPayload)))
;
69691 }
69692
69693 /* Fill in the payload */
69694 pPayload = &pCell[nHeader];
69695 if( nPayload<=pPage->maxLocal ){
69696 /* This is the common case where everything fits on the btree page
69697 ** and no overflow pages are required. */
69698 n = nHeader + nPayload;
69699 testcase( n==3 );
69700 testcase( n==4 );
69701 if( n<4 ) n = 4;
69702 *pnSize = n;
69703 assert( nSrc<=nPayload )((void) (0));
69704 testcase( nSrc<nPayload );
69705 memcpy(pPayload, pSrc, nSrc);
69706 memset(pPayload+nSrc, 0, nPayload-nSrc);
69707 return SQLITE_OK0;
69708 }
69709
69710 /* If we reach this point, it means that some of the content will need
69711 ** to spill onto overflow pages.
69712 */
69713 mn = pPage->minLocal;
69714 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
69715 testcase( n==pPage->maxLocal );
69716 testcase( n==pPage->maxLocal+1 );
69717 if( n > pPage->maxLocal ) n = mn;
69718 spaceLeft = n;
69719 *pnSize = n + nHeader + 4;
69720 pPrior = &pCell[nHeader+n];
69721 pToRelease = 0;
69722 pgnoOvfl = 0;
69723 pBt = pPage->pBt;
69724
69725 /* At this point variables should be set as follows:
69726 **
69727 ** nPayload Total payload size in bytes
69728 ** pPayload Begin writing payload here
69729 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
69730 ** that means content must spill into overflow pages.
69731 ** *pnSize Size of the local cell (not counting overflow pages)
69732 ** pPrior Where to write the pgno of the first overflow page
69733 **
69734 ** Use a call to btreeParseCellPtr() to verify that the values above
69735 ** were computed correctly.
69736 */
69737#ifdef SQLITE_DEBUG
69738 {
69739 CellInfo info;
69740 pPage->xParseCell(pPage, pCell, &info);
69741 assert( nHeader==(int)(info.pPayload - pCell) )((void) (0));
69742 assert( info.nKey==pX->nKey )((void) (0));
69743 assert( *pnSize == info.nSize )((void) (0));
69744 assert( spaceLeft == info.nLocal )((void) (0));
69745 }
69746#endif
69747
69748 /* Write the payload into the local Cell and any extra into overflow pages */
69749 while( 1 ){
69750 n = nPayload;
69751 if( n>spaceLeft ) n = spaceLeft;
69752
69753 /* If pToRelease is not zero than pPayload points into the data area
69754 ** of pToRelease. Make sure pToRelease is still writeable. */
69755 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) )((void) (0));
69756
69757 /* If pPayload is part of the data area of pPage, then make sure pPage
69758 ** is still writeable */
69759 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]((void) (0))
69760 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
69761
69762 if( nSrc>=n ){
69763 memcpy(pPayload, pSrc, n);
69764 }else if( nSrc>0 ){
69765 n = nSrc;
69766 memcpy(pPayload, pSrc, n);
69767 }else{
69768 memset(pPayload, 0, n);
69769 }
69770 nPayload -= n;
69771 if( nPayload<=0 ) break;
69772 pPayload += n;
69773 pSrc += n;
69774 nSrc -= n;
69775 spaceLeft -= n;
69776 if( spaceLeft==0 ){
69777 MemPage *pOvfl = 0;
69778#ifndef SQLITE_OMIT_AUTOVACUUM
69779 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
69780 if( pBt->autoVacuum ){
69781 do{
69782 pgnoOvfl++;
69783 } while(
69784 PTRMAP_ISPAGE(pBt, pgnoOvfl)(ptrmapPageno((pBt), (pgnoOvfl))==(pgnoOvfl)) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
69785 );
69786 }
69787#endif
69788 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
69789#ifndef SQLITE_OMIT_AUTOVACUUM
69790 /* If the database supports auto-vacuum, and the second or subsequent
69791 ** overflow page is being allocated, add an entry to the pointer-map
69792 ** for that page now.
69793 **
69794 ** If this is the first overflow page, then write a partial entry
69795 ** to the pointer-map. If we write nothing to this pointer-map slot,
69796 ** then the optimistic overflow chain processing in clearCell()
69797 ** may misinterpret the uninitialized values and delete the
69798 ** wrong pages from the database.
69799 */
69800 if( pBt->autoVacuum && rc==SQLITE_OK0 ){
69801 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW24:PTRMAP_OVERFLOW13);
69802 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
69803 if( rc ){
69804 releasePage(pOvfl);
69805 }
69806 }
69807#endif
69808 if( rc ){
69809 releasePage(pToRelease);
69810 return rc;
69811 }
69812
69813 /* If pToRelease is not zero than pPrior points into the data area
69814 ** of pToRelease. Make sure pToRelease is still writeable. */
69815 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) )((void) (0));
69816
69817 /* If pPrior is part of the data area of pPage, then make sure pPage
69818 ** is still writeable */
69819 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]((void) (0))
69820 || sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
69821
69822 put4bytesqlite3Put4byte(pPrior, pgnoOvfl);
69823 releasePage(pToRelease);
69824 pToRelease = pOvfl;
69825 pPrior = pOvfl->aData;
69826 put4bytesqlite3Put4byte(pPrior, 0);
69827 pPayload = &pOvfl->aData[4];
69828 spaceLeft = pBt->usableSize - 4;
69829 }
69830 }
69831 releasePage(pToRelease);
69832 return SQLITE_OK0;
69833}
69834
69835/*
69836** Remove the i-th cell from pPage. This routine effects pPage only.
69837** The cell content is not freed or deallocated. It is assumed that
69838** the cell content has been copied someplace else. This routine just
69839** removes the reference to the cell from pPage.
69840**
69841** "sz" must be the number of bytes in the cell.
69842*/
69843static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
69844 u32 pc; /* Offset to cell content of cell being deleted */
69845 u8 *data; /* pPage->aData */
69846 u8 *ptr; /* Used to move bytes around within data[] */
69847 int rc; /* The return code */
69848 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
69849
69850 if( *pRC ) return;
69851 assert( idx>=0 && idx<pPage->nCell )((void) (0));
69852 assert( CORRUPT_DB || sz==cellSize(pPage, idx) )((void) (0));
69853 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
69854 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
69855 assert( pPage->nFree>=0 )((void) (0));
69856 data = pPage->aData;
69857 ptr = &pPage->aCellIdx[2*idx];
69858 pc = get2byte(ptr)((ptr)[0]<<8 | (ptr)[1]);
69859 hdr = pPage->hdrOffset;
69860 testcase( pc==get2byte(&data[hdr+5]) );
69861 testcase( pc+sz==pPage->pBt->usableSize );
69862 if( pc+sz > pPage->pBt->usableSize ){
69863 *pRC = SQLITE_CORRUPT_BKPTsqlite3CorruptError(69863);
69864 return;
69865 }
69866 rc = freeSpace(pPage, pc, sz);
69867 if( rc ){
69868 *pRC = rc;
69869 return;
69870 }
69871 pPage->nCell--;
69872 if( pPage->nCell==0 ){
69873 memset(&data[hdr+1], 0, 4);
69874 data[hdr+7] = 0;
69875 put2byte(&data[hdr+5], pPage->pBt->usableSize)((&data[hdr+5])[0] = (u8)((pPage->pBt->usableSize)>>
8), (&data[hdr+5])[1] = (u8)(pPage->pBt->usableSize
))
;
69876 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
69877 - pPage->childPtrSize - 8;
69878 }else{
69879 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
69880 put2byte(&data[hdr+3], pPage->nCell)((&data[hdr+3])[0] = (u8)((pPage->nCell)>>8), (&
data[hdr+3])[1] = (u8)(pPage->nCell))
;
69881 pPage->nFree += 2;
69882 }
69883}
69884
69885/*
69886** Insert a new cell on pPage at cell index "i". pCell points to the
69887** content of the cell.
69888**
69889** If the cell content will fit on the page, then put it there. If it
69890** will not fit, then make a copy of the cell content into pTemp if
69891** pTemp is not null. Regardless of pTemp, allocate a new entry
69892** in pPage->apOvfl[] and make it point to the cell content (either
69893** in pTemp or the original pCell) and also record its index.
69894** Allocating a new entry in pPage->aCell[] implies that
69895** pPage->nOverflow is incremented.
69896**
69897** *pRC must be SQLITE_OK when this routine is called.
69898*/
69899static void insertCell(
69900 MemPage *pPage, /* Page into which we are copying */
69901 int i, /* New cell becomes the i-th cell of the page */
69902 u8 *pCell, /* Content of the new cell */
69903 int sz, /* Bytes of content in pCell */
69904 u8 *pTemp, /* Temp storage space for pCell, if needed */
69905 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
69906 int *pRC /* Read and write return code from here */
69907){
69908 int idx = 0; /* Where to write new cell content in data[] */
69909 int j; /* Loop counter */
69910 u8 *data; /* The content of the whole page */
69911 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
69912
69913 assert( *pRC==SQLITE_OK )((void) (0));
69914 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow )((void) (0));
69915 assert( MX_CELL(pPage->pBt)<=10921 )((void) (0));
69916 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB )((void) (0));
69917 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) )((void) (0));
69918 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) )((void) (0));
69919 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
69920 /* The cell should normally be sized correctly. However, when moving a
69921 ** malformed cell from a leaf page to an interior page, if the cell size
69922 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
69923 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
69924 ** the term after the || in the following assert(). */
69925 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) )((void) (0));
69926 assert( pPage->nFree>=0 )((void) (0));
69927 if( pPage->nOverflow || sz+2>pPage->nFree ){
69928 if( pTemp ){
69929 memcpy(pTemp, pCell, sz);
69930 pCell = pTemp;
69931 }
69932 if( iChild ){
69933 put4bytesqlite3Put4byte(pCell, iChild);
69934 }
69935 j = pPage->nOverflow++;
69936 /* Comparison against ArraySize-1 since we hold back one extra slot
69937 ** as a contingency. In other words, never need more than 3 overflow
69938 ** slots but 4 are allocated, just to be safe. */
69939 assert( j < ArraySize(pPage->apOvfl)-1 )((void) (0));
69940 pPage->apOvfl[j] = pCell;
69941 pPage->aiOvfl[j] = (u16)i;
69942
69943 /* When multiple overflows occur, they are always sequential and in
69944 ** sorted order. This invariants arise because multiple overflows can
69945 ** only occur when inserting divider cells into the parent page during
69946 ** balancing, and the dividers are adjacent and sorted.
69947 */
69948 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i )((void) (0)); /* Overflows in sorted order */
69949 assert( j==0 || i==pPage->aiOvfl[j-1]+1 )((void) (0)); /* Overflows are sequential */
69950 }else{
69951 int rc = sqlite3PagerWrite(pPage->pDbPage);
69952 if( rc!=SQLITE_OK0 ){
69953 *pRC = rc;
69954 return;
69955 }
69956 assert( sqlite3PagerIswriteable(pPage->pDbPage) )((void) (0));
69957 data = pPage->aData;
69958 assert( &data[pPage->cellOffset]==pPage->aCellIdx )((void) (0));
69959 rc = allocateSpace(pPage, sz, &idx);
69960 if( rc ){ *pRC = rc; return; }
69961 /* The allocateSpace() routine guarantees the following properties
69962 ** if it returns successfully */
69963 assert( idx >= 0 )((void) (0));
69964 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB )((void) (0));
69965 assert( idx+sz <= (int)pPage->pBt->usableSize )((void) (0));
69966 pPage->nFree -= (u16)(2 + sz);
69967 if( iChild ){
69968 /* In a corrupt database where an entry in the cell index section of
69969 ** a btree page has a value of 3 or less, the pCell value might point
69970 ** as many as 4 bytes in front of the start of the aData buffer for
69971 ** the source page. Make sure this does not cause problems by not
69972 ** reading the first 4 bytes */
69973 memcpy(&data[idx+4], pCell+4, sz-4);
69974 put4bytesqlite3Put4byte(&data[idx], iChild);
69975 }else{
69976 memcpy(&data[idx], pCell, sz);
69977 }
69978 pIns = pPage->aCellIdx + i*2;
69979 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
69980 put2byte(pIns, idx)((pIns)[0] = (u8)((idx)>>8), (pIns)[1] = (u8)(idx));
69981 pPage->nCell++;
69982 /* increment the cell count */
69983 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
69984 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB )((void) (0));
69985#ifndef SQLITE_OMIT_AUTOVACUUM
69986 if( pPage->pBt->autoVacuum ){
69987 /* The cell may contain a pointer to an overflow page. If so, write
69988 ** the entry for the overflow page into the pointer map.
69989 */
69990 ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
69991 }
69992#endif
69993 }
69994}
69995
69996/*
69997** The following parameters determine how many adjacent pages get involved
69998** in a balancing operation. NN is the number of neighbors on either side
69999** of the page that participate in the balancing operation. NB is the
70000** total number of pages that participate, including the target page and
70001** NN neighbors on either side.
70002**
70003** The minimum value of NN is 1 (of course). Increasing NN above 1
70004** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
70005** in exchange for a larger degradation in INSERT and UPDATE performance.
70006** The value of NN appears to give the best results overall.
70007**
70008** (Later:) The description above makes it seem as if these values are
70009** tunable - as if you could change them and recompile and it would all work.
70010** But that is unlikely. NB has been 3 since the inception of SQLite and
70011** we have never tested any other value.
70012*/
70013#define NN1 1 /* Number of neighbors on either side of pPage */
70014#define NB3 3 /* (NN*2+1): Total pages involved in the balance */
70015
70016/*
70017** A CellArray object contains a cache of pointers and sizes for a
70018** consecutive sequence of cells that might be held on multiple pages.
70019**
70020** The cells in this array are the divider cell or cells from the pParent
70021** page plus up to three child pages. There are a total of nCell cells.
70022**
70023** pRef is a pointer to one of the pages that contributes cells. This is
70024** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
70025** which should be common to all pages that contribute cells to this array.
70026**
70027** apCell[] and szCell[] hold, respectively, pointers to the start of each
70028** cell and the size of each cell. Some of the apCell[] pointers might refer
70029** to overflow cells. In other words, some apCel[] pointers might not point
70030** to content area of the pages.
70031**
70032** A szCell[] of zero means the size of that cell has not yet been computed.
70033**
70034** The cells come from as many as four different pages:
70035**
70036** -----------
70037** | Parent |
70038** -----------
70039** / | \
70040** / | \
70041** --------- --------- ---------
70042** |Child-1| |Child-2| |Child-3|
70043** --------- --------- ---------
70044**
70045** The order of cells is in the array is for an index btree is:
70046**
70047** 1. All cells from Child-1 in order
70048** 2. The first divider cell from Parent
70049** 3. All cells from Child-2 in order
70050** 4. The second divider cell from Parent
70051** 5. All cells from Child-3 in order
70052**
70053** For a table-btree (with rowids) the items 2 and 4 are empty because
70054** content exists only in leaves and there are no divider cells.
70055**
70056** For an index btree, the apEnd[] array holds pointer to the end of page
70057** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
70058** respectively. The ixNx[] array holds the number of cells contained in
70059** each of these 5 stages, and all stages to the left. Hence:
70060**
70061** ixNx[0] = Number of cells in Child-1.
70062** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
70063** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
70064** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
70065** ixNx[4] = Total number of cells.
70066**
70067** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
70068** are used and they point to the leaf pages only, and the ixNx value are:
70069**
70070** ixNx[0] = Number of cells in Child-1.
70071** ixNx[1] = Number of cells in Child-1 and Child-2.
70072** ixNx[2] = Total number of cells.
70073**
70074** Sometimes when deleting, a child page can have zero cells. In those
70075** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
70076** entries, shift down. The end result is that each ixNx[] entry should
70077** be larger than the previous
70078*/
70079typedef struct CellArray CellArray;
70080struct CellArray {
70081 int nCell; /* Number of cells in apCell[] */
70082 MemPage *pRef; /* Reference page */
70083 u8 **apCell; /* All cells begin balanced */
70084 u16 *szCell; /* Local size of all cells in apCell[] */
70085 u8 *apEnd[NB3*2]; /* MemPage.aDataEnd values */
70086 int ixNx[NB3*2]; /* Index of at which we move to the next apEnd[] */
70087};
70088
70089/*
70090** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
70091** computed.
70092*/
70093static void populateCellCache(CellArray *p, int idx, int N){
70094 assert( idx>=0 && idx+N<=p->nCell )((void) (0));
70095 while( N>0 ){
70096 assert( p->apCell[idx]!=0 )((void) (0));
70097 if( p->szCell[idx]==0 ){
70098 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
70099 }else{
70100 assert( CORRUPT_DB ||((void) (0))
70101 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) )((void) (0));
70102 }
70103 idx++;
70104 N--;
70105 }
70106}
70107
70108/*
70109** Return the size of the Nth element of the cell array
70110*/
70111static SQLITE_NOINLINE__attribute__((noinline)) u16 computeCellSize(CellArray *p, int N){
70112 assert( N>=0 && N<p->nCell )((void) (0));
70113 assert( p->szCell[N]==0 )((void) (0));
70114 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
70115 return p->szCell[N];
70116}
70117static u16 cachedCellSize(CellArray *p, int N){
70118 assert( N>=0 && N<p->nCell )((void) (0));
70119 if( p->szCell[N] ) return p->szCell[N];
70120 return computeCellSize(p, N);
70121}
70122
70123/*
70124** Array apCell[] contains pointers to nCell b-tree page cells. The
70125** szCell[] array contains the size in bytes of each cell. This function
70126** replaces the current contents of page pPg with the contents of the cell
70127** array.
70128**
70129** Some of the cells in apCell[] may currently be stored in pPg. This
70130** function works around problems caused by this by making a copy of any
70131** such cells before overwriting the page data.
70132**
70133** The MemPage.nFree field is invalidated by this function. It is the
70134** responsibility of the caller to set it correctly.
70135*/
70136static int rebuildPage(
70137 CellArray *pCArray, /* Content to be added to page pPg */
70138 int iFirst, /* First cell in pCArray to use */
70139 int nCell, /* Final number of cells on page */
70140 MemPage *pPg /* The page to be reconstructed */
70141){
70142 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
70143 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
70144 const int usableSize = pPg->pBt->usableSize;
70145 u8 * const pEnd = &aData[usableSize];
70146 int i = iFirst; /* Which cell to copy from pCArray*/
70147 u32 j; /* Start of cell content area */
70148 int iEnd = i+nCell; /* Loop terminator */
70149 u8 *pCellptr = pPg->aCellIdx;
70150 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
70151 u8 *pData;
70152 int k; /* Current slot in pCArray->apEnd[] */
70153 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
70154
70155 assert( i<iEnd )((void) (0));
70156 j = get2byte(&aData[hdr+5])((&aData[hdr+5])[0]<<8 | (&aData[hdr+5])[1]);
70157 if( NEVER(j>(u32)usableSize)(j>(u32)usableSize) ){ j = 0; }
70158 memcpy(&pTmp[j], &aData[j], usableSize - j);
70159
70160 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2)(k<3*2); k++){}
70161 pSrcEnd = pCArray->apEnd[k];
70162
70163 pData = pEnd;
70164 while( 1/*exit by break*/ ){
70165 u8 *pCell = pCArray->apCell[i];
70166 u16 sz = pCArray->szCell[i];
70167 assert( sz>0 )((void) (0));
70168 if( SQLITE_WITHIN(pCell,aData,pEnd)(((uptr)(pCell)>=(uptr)(aData))&&((uptr)(pCell)<
(uptr)(pEnd)))
){
70169 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70169);
70170 pCell = &pTmp[pCell - aData];
70171 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
70172 && (uptr)(pCell)<(uptr)pSrcEnd
70173 ){
70174 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70174);
70175 }
70176
70177 pData -= sz;
70178 put2byte(pCellptr, (pData - aData))((pCellptr)[0] = (u8)(((pData - aData))>>8), (pCellptr)
[1] = (u8)((pData - aData)))
;
70179 pCellptr += 2;
70180 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70180);
70181 memcpy(pData, pCell, sz);
70182 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB )((void) (0));
70183 testcase( sz!=pPg->xCellSize(pPg,pCell) );
70184 i++;
70185 if( i>=iEnd ) break;
70186 if( pCArray->ixNx[k]<=i ){
70187 k++;
70188 pSrcEnd = pCArray->apEnd[k];
70189 }
70190 }
70191
70192 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
70193 pPg->nCell = nCell;
70194 pPg->nOverflow = 0;
70195
70196 put2byte(&aData[hdr+1], 0)((&aData[hdr+1])[0] = (u8)((0)>>8), (&aData[hdr
+1])[1] = (u8)(0))
;
70197 put2byte(&aData[hdr+3], pPg->nCell)((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&
aData[hdr+3])[1] = (u8)(pPg->nCell))
;
70198 put2byte(&aData[hdr+5], pData - aData)((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&
aData[hdr+5])[1] = (u8)(pData - aData))
;
70199 aData[hdr+7] = 0x00;
70200 return SQLITE_OK0;
70201}
70202
70203/*
70204** The pCArray objects contains pointers to b-tree cells and the cell sizes.
70205** This function attempts to add the cells stored in the array to page pPg.
70206** If it cannot (because the page needs to be defragmented before the cells
70207** will fit), non-zero is returned. Otherwise, if the cells are added
70208** successfully, zero is returned.
70209**
70210** Argument pCellptr points to the first entry in the cell-pointer array
70211** (part of page pPg) to populate. After cell apCell[0] is written to the
70212** page body, a 16-bit offset is written to pCellptr. And so on, for each
70213** cell in the array. It is the responsibility of the caller to ensure
70214** that it is safe to overwrite this part of the cell-pointer array.
70215**
70216** When this function is called, *ppData points to the start of the
70217** content area on page pPg. If the size of the content area is extended,
70218** *ppData is updated to point to the new start of the content area
70219** before returning.
70220**
70221** Finally, argument pBegin points to the byte immediately following the
70222** end of the space required by this page for the cell-pointer area (for
70223** all cells - not just those inserted by the current call). If the content
70224** area must be extended to before this point in order to accomodate all
70225** cells in apCell[], then the cells do not fit and non-zero is returned.
70226*/
70227static int pageInsertArray(
70228 MemPage *pPg, /* Page to add cells to */
70229 u8 *pBegin, /* End of cell-pointer array */
70230 u8 **ppData, /* IN/OUT: Page content-area pointer */
70231 u8 *pCellptr, /* Pointer to cell-pointer area */
70232 int iFirst, /* Index of first cell to add */
70233 int nCell, /* Number of cells to add to pPg */
70234 CellArray *pCArray /* Array of cells */
70235){
70236 int i = iFirst; /* Loop counter - cell index to insert */
70237 u8 *aData = pPg->aData; /* Complete page */
70238 u8 *pData = *ppData; /* Content area. A subset of aData[] */
70239 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
70240 int k; /* Current slot in pCArray->apEnd[] */
70241 u8 *pEnd; /* Maximum extent of cell data */
70242 assert( CORRUPT_DB || pPg->hdrOffset==0 )((void) (0)); /* Never called on page 1 */
70243 if( iEnd<=iFirst ) return 0;
70244 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2)(k<3*2); k++){}
70245 pEnd = pCArray->apEnd[k];
70246 while( 1 /*Exit by break*/ ){
70247 int sz, rc;
70248 u8 *pSlot;
70249 sz = cachedCellSize(pCArray, i);
70250 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
70251 if( (pData - pBegin)<sz ) return 1;
70252 pData -= sz;
70253 pSlot = pData;
70254 }
70255 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
70256 ** database. But they might for a corrupt database. Hence use memmove()
70257 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
70258 assert( (pSlot+sz)<=pCArray->apCell[i]((void) (0))
70259 || pSlot>=(pCArray->apCell[i]+sz)((void) (0))
70260 || CORRUPT_DB )((void) (0));
70261 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
70262 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
70263 ){
70264 assert( CORRUPT_DB )((void) (0));
70265 (void)SQLITE_CORRUPT_BKPTsqlite3CorruptError(70265);
70266 return 1;
70267 }
70268 memmove(pSlot, pCArray->apCell[i], sz);
70269 put2byte(pCellptr, (pSlot - aData))((pCellptr)[0] = (u8)(((pSlot - aData))>>8), (pCellptr)
[1] = (u8)((pSlot - aData)))
;
70270 pCellptr += 2;
70271 i++;
70272 if( i>=iEnd ) break;
70273 if( pCArray->ixNx[k]<=i ){
70274 k++;
70275 pEnd = pCArray->apEnd[k];
70276 }
70277 }
70278 *ppData = pData;
70279 return 0;
70280}
70281
70282/*
70283** The pCArray object contains pointers to b-tree cells and their sizes.
70284**
70285** This function adds the space associated with each cell in the array
70286** that is currently stored within the body of pPg to the pPg free-list.
70287** The cell-pointers and other fields of the page are not updated.
70288**
70289** This function returns the total number of cells added to the free-list.
70290*/
70291static int pageFreeArray(
70292 MemPage *pPg, /* Page to edit */
70293 int iFirst, /* First cell to delete */
70294 int nCell, /* Cells to delete */
70295 CellArray *pCArray /* Array of cells */
70296){
70297 u8 * const aData = pPg->aData;
70298 u8 * const pEnd = &aData[pPg->pBt->usableSize];
70299 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
70300 int nRet = 0;
70301 int i;
70302 int iEnd = iFirst + nCell;
70303 u8 *pFree = 0;
70304 int szFree = 0;
70305
70306 for(i=iFirst; i<iEnd; i++){
70307 u8 *pCell = pCArray->apCell[i];
70308 if( SQLITE_WITHIN(pCell, pStart, pEnd)(((uptr)(pCell)>=(uptr)(pStart))&&((uptr)(pCell)<
(uptr)(pEnd)))
){
70309 int sz;
70310 /* No need to use cachedCellSize() here. The sizes of all cells that
70311 ** are to be freed have already been computing while deciding which
70312 ** cells need freeing */
70313 sz = pCArray->szCell[i]; assert( sz>0 )((void) (0));
70314 if( pFree!=(pCell + sz) ){
70315 if( pFree ){
70316 assert( pFree>aData && (pFree - aData)<65536 )((void) (0));
70317 freeSpace(pPg, (u16)(pFree - aData), szFree);
70318 }
70319 pFree = pCell;
70320 szFree = sz;
70321 if( pFree+sz>pEnd ) return 0;
70322 }else{
70323 pFree = pCell;
70324 szFree += sz;
70325 }
70326 nRet++;
70327 }
70328 }
70329 if( pFree ){
70330 assert( pFree>aData && (pFree - aData)<65536 )((void) (0));
70331 freeSpace(pPg, (u16)(pFree - aData), szFree);
70332 }
70333 return nRet;
70334}
70335
70336/*
70337** pCArray contains pointers to and sizes of all cells in the page being
70338** balanced. The current page, pPg, has pPg->nCell cells starting with
70339** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
70340** starting at apCell[iNew].
70341**
70342** This routine makes the necessary adjustments to pPg so that it contains
70343** the correct cells after being balanced.
70344**
70345** The pPg->nFree field is invalid when this function returns. It is the
70346** responsibility of the caller to set it correctly.
70347*/
70348static int editPage(
70349 MemPage *pPg, /* Edit this page */
70350 int iOld, /* Index of first cell currently on page */
70351 int iNew, /* Index of new first cell on page */
70352 int nNew, /* Final number of cells on page */
70353 CellArray *pCArray /* Array of cells and sizes */
70354){
70355 u8 * const aData = pPg->aData;
70356 const int hdr = pPg->hdrOffset;
70357 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
70358 int nCell = pPg->nCell; /* Cells stored on pPg */
70359 u8 *pData;
70360 u8 *pCellptr;
70361 int i;
70362 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
70363 int iNewEnd = iNew + nNew;
70364
70365#ifdef SQLITE_DEBUG
70366 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
70367 memcpy(pTmp, aData, pPg->pBt->usableSize);
70368#endif
70369
70370 /* Remove cells from the start and end of the page */
70371 assert( nCell>=0 )((void) (0));
70372 if( iOld<iNew ){
70373 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
70374 if( nShift>nCell ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70374);
70375 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
70376 nCell -= nShift;
70377 }
70378 if( iNewEnd < iOldEnd ){
70379 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
70380 assert( nCell>=nTail )((void) (0));
70381 nCell -= nTail;
70382 }
70383
70384 pData = &aData[get2byteNotZero(&aData[hdr+5])(((((int)((&aData[hdr+5])[0]<<8 | (&aData[hdr+5
])[1]))-1)&0xffff)+1)
];
70385 if( pData<pBegin ) goto editpage_fail;
70386
70387 /* Add cells to the start of the page */
70388 if( iNew<iOld ){
70389 int nAdd = MIN(nNew,iOld-iNew)((nNew)<(iOld-iNew)?(nNew):(iOld-iNew));
70390 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB )((void) (0));
70391 assert( nAdd>=0 )((void) (0));
70392 pCellptr = pPg->aCellIdx;
70393 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
70394 if( pageInsertArray(
70395 pPg, pBegin, &pData, pCellptr,
70396 iNew, nAdd, pCArray
70397 ) ) goto editpage_fail;
70398 nCell += nAdd;
70399 }
70400
70401 /* Add any overflow cells */
70402 for(i=0; i<pPg->nOverflow; i++){
70403 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
70404 if( iCell>=0 && iCell<nNew ){
70405 pCellptr = &pPg->aCellIdx[iCell * 2];
70406 if( nCell>iCell ){
70407 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
70408 }
70409 nCell++;
70410 if( pageInsertArray(
70411 pPg, pBegin, &pData, pCellptr,
70412 iCell+iNew, 1, pCArray
70413 ) ) goto editpage_fail;
70414 }
70415 }
70416
70417 /* Append cells to the end of the page */
70418 assert( nCell>=0 )((void) (0));
70419 pCellptr = &pPg->aCellIdx[nCell*2];
70420 if( pageInsertArray(
70421 pPg, pBegin, &pData, pCellptr,
70422 iNew+nCell, nNew-nCell, pCArray
70423 ) ) goto editpage_fail;
70424
70425 pPg->nCell = nNew;
70426 pPg->nOverflow = 0;
70427
70428 put2byte(&aData[hdr+3], pPg->nCell)((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&
aData[hdr+3])[1] = (u8)(pPg->nCell))
;
70429 put2byte(&aData[hdr+5], pData - aData)((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&
aData[hdr+5])[1] = (u8)(pData - aData))
;
70430
70431#ifdef SQLITE_DEBUG
70432 for(i=0; i<nNew && !CORRUPT_DB(sqlite3Config.neverCorrupt==0); i++){
70433 u8 *pCell = pCArray->apCell[i+iNew];
70434 int iOff = get2byteAligned(&pPg->aCellIdx[i*2])((&pPg->aCellIdx[i*2])[0]<<8 | (&pPg->aCellIdx
[i*2])[1])
;
70435 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize])(((uptr)(pCell)>=(uptr)(aData))&&((uptr)(pCell)<
(uptr)(&aData[pPg->pBt->usableSize])))
){
70436 pCell = &pTmp[pCell - aData];
70437 }
70438 assert( 0==memcmp(pCell, &aData[iOff],((void) (0))
70439 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) )((void) (0));
70440 }
70441#endif
70442
70443 return SQLITE_OK0;
70444 editpage_fail:
70445 /* Unable to edit this page. Rebuild it from scratch instead. */
70446 populateCellCache(pCArray, iNew, nNew);
70447 return rebuildPage(pCArray, iNew, nNew, pPg);
70448}
70449
70450
70451#ifndef SQLITE_OMIT_QUICKBALANCE
70452/*
70453** This version of balance() handles the common special case where
70454** a new entry is being inserted on the extreme right-end of the
70455** tree, in other words, when the new entry will become the largest
70456** entry in the tree.
70457**
70458** Instead of trying to balance the 3 right-most leaf pages, just add
70459** a new page to the right-hand side and put the one new entry in
70460** that page. This leaves the right side of the tree somewhat
70461** unbalanced. But odds are that we will be inserting new entries
70462** at the end soon afterwards so the nearly empty page will quickly
70463** fill up. On average.
70464**
70465** pPage is the leaf page which is the right-most page in the tree.
70466** pParent is its parent. pPage must have a single overflow entry
70467** which is also the right-most entry on the page.
70468**
70469** The pSpace buffer is used to store a temporary copy of the divider
70470** cell that will be inserted into pParent. Such a cell consists of a 4
70471** byte page number followed by a variable length integer. In other
70472** words, at most 13 bytes. Hence the pSpace buffer must be at
70473** least 13 bytes in size.
70474*/
70475static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
70476 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
70477 MemPage *pNew; /* Newly allocated page */
70478 int rc; /* Return Code */
70479 Pgno pgnoNew; /* Page number of pNew */
70480
70481 assert( sqlite3_mutex_held(pPage->pBt->mutex) )((void) (0));
70482 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
70483 assert( pPage->nOverflow==1 )((void) (0));
70484
70485 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(70485); /* dbfuzz001.test */
70486 assert( pPage->nFree>=0 )((void) (0));
70487 assert( pParent->nFree>=0 )((void) (0));
70488
70489 /* Allocate a new page. This page will become the right-sibling of
70490 ** pPage. Make the parent page writable, so that the new divider cell
70491 ** may be inserted. If both these operations are successful, proceed.
70492 */
70493 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
70494
70495 if( rc==SQLITE_OK0 ){
70496
70497 u8 *pOut = &pSpace[4];
70498 u8 *pCell = pPage->apOvfl[0];
70499 u16 szCell = pPage->xCellSize(pPage, pCell);
70500 u8 *pStop;
70501 CellArray b;
70502
70503 assert( sqlite3PagerIswriteable(pNew->pDbPage) )((void) (0));
70504 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) )((void) (0));
70505 zeroPage(pNew, PTF_INTKEY0x01|PTF_LEAFDATA0x04|PTF_LEAF0x08);
70506 b.nCell = 1;
70507 b.pRef = pPage;
70508 b.apCell = &pCell;
70509 b.szCell = &szCell;
70510 b.apEnd[0] = pPage->aDataEnd;
70511 b.ixNx[0] = 2;
70512 rc = rebuildPage(&b, 0, 1, pNew);
70513 if( NEVER(rc)(rc) ){
70514 releasePage(pNew);
70515 return rc;
70516 }
70517 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
70518
70519 /* If this is an auto-vacuum database, update the pointer map
70520 ** with entries for the new page, and any pointer from the
70521 ** cell on the page to an overflow page. If either of these
70522 ** operations fails, the return code is set, but the contents
70523 ** of the parent page are still manipulated by thh code below.
70524 ** That is Ok, at this point the parent page is guaranteed to
70525 ** be marked as dirty. Returning an error code will cause a
70526 ** rollback, undoing any changes made to the parent page.
70527 */
70528 if( ISAUTOVACUUM(pBt->autoVacuum) ){
70529 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE5, pParent->pgno, &rc);
70530 if( szCell>pNew->minLocal ){
70531 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
70532 }
70533 }
70534
70535 /* Create a divider cell to insert into pParent. The divider cell
70536 ** consists of a 4-byte page number (the page number of pPage) and
70537 ** a variable length key value (which must be the same value as the
70538 ** largest key on pPage).
70539 **
70540 ** To find the largest key value on pPage, first find the right-most
70541 ** cell on pPage. The first two fields of this cell are the
70542 ** record-length (a variable length integer at most 32-bits in size)
70543 ** and the key value (a variable length integer, may have any value).
70544 ** The first of the while(...) loops below skips over the record-length
70545 ** field. The second while(...) loop copies the key value from the
70546 ** cell on pPage into the pSpace buffer.
70547 */
70548 pCell = findCell(pPage, pPage->nCell-1)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(pPage->nCell-1)])[0]<<8 | (&(pPage
)->aCellIdx[2*(pPage->nCell-1)])[1])))
;
70549 pStop = &pCell[9];
70550 while( (*(pCell++)&0x80) && pCell<pStop );
70551 pStop = &pCell[9];
70552 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
70553
70554 /* Insert the new divider cell into pParent. */
70555 if( rc==SQLITE_OK0 ){
70556 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
70557 0, pPage->pgno, &rc);
70558 }
70559
70560 /* Set the right-child pointer of pParent to point to the new page. */
70561 put4bytesqlite3Put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
70562
70563 /* Release the reference to the new page. */
70564 releasePage(pNew);
70565 }
70566
70567 return rc;
70568}
70569#endif /* SQLITE_OMIT_QUICKBALANCE */
70570
70571#if 0
70572/*
70573** This function does not contribute anything to the operation of SQLite.
70574** it is sometimes activated temporarily while debugging code responsible
70575** for setting pointer-map entries.
70576*/
70577static int ptrmapCheckPages(MemPage **apPage, int nPage){
70578 int i, j;
70579 for(i=0; i<nPage; i++){
70580 Pgno n;
70581 u8 e;
70582 MemPage *pPage = apPage[i];
70583 BtShared *pBt = pPage->pBt;
70584 assert( pPage->isInit )((void) (0));
70585
70586 for(j=0; j<pPage->nCell; j++){
70587 CellInfo info;
70588 u8 *z;
70589
70590 z = findCell(pPage, j)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(j)])[0]<<8 | (&(pPage)->aCellIdx
[2*(j)])[1])))
;
70591 pPage->xParseCell(pPage, z, &info);
70592 if( info.nLocal<info.nPayload ){
70593 Pgno ovfl = get4bytesqlite3Get4byte(&z[info.nSize-4]);
70594 ptrmapGet(pBt, ovfl, &e, &n);
70595 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 )((void) (0));
70596 }
70597 if( !pPage->leaf ){
70598 Pgno child = get4bytesqlite3Get4byte(z);
70599 ptrmapGet(pBt, child, &e, &n);
70600 assert( n==pPage->pgno && e==PTRMAP_BTREE )((void) (0));
70601 }
70602 }
70603 if( !pPage->leaf ){
70604 Pgno child = get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
70605 ptrmapGet(pBt, child, &e, &n);
70606 assert( n==pPage->pgno && e==PTRMAP_BTREE )((void) (0));
70607 }
70608 }
70609 return 1;
70610}
70611#endif
70612
70613/*
70614** This function is used to copy the contents of the b-tree node stored
70615** on page pFrom to page pTo. If page pFrom was not a leaf page, then
70616** the pointer-map entries for each child page are updated so that the
70617** parent page stored in the pointer map is page pTo. If pFrom contained
70618** any cells with overflow page pointers, then the corresponding pointer
70619** map entries are also updated so that the parent page is page pTo.
70620**
70621** If pFrom is currently carrying any overflow cells (entries in the
70622** MemPage.apOvfl[] array), they are not copied to pTo.
70623**
70624** Before returning, page pTo is reinitialized using btreeInitPage().
70625**
70626** The performance of this function is not critical. It is only used by
70627** the balance_shallower() and balance_deeper() procedures, neither of
70628** which are called often under normal circumstances.
70629*/
70630static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
70631 if( (*pRC)==SQLITE_OK0 ){
70632 BtShared * const pBt = pFrom->pBt;
70633 u8 * const aFrom = pFrom->aData;
70634 u8 * const aTo = pTo->aData;
70635 int const iFromHdr = pFrom->hdrOffset;
70636 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
70637 int rc;
70638 int iData;
70639
70640
70641 assert( pFrom->isInit )((void) (0));
70642 assert( pFrom->nFree>=iToHdr )((void) (0));
70643 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize )((void) (0));
70644
70645 /* Copy the b-tree node content from page pFrom to page pTo. */
70646 iData = get2byte(&aFrom[iFromHdr+5])((&aFrom[iFromHdr+5])[0]<<8 | (&aFrom[iFromHdr+
5])[1])
;
70647 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
70648 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
70649
70650 /* Reinitialize page pTo so that the contents of the MemPage structure
70651 ** match the new data. The initialization of pTo can actually fail under
70652 ** fairly obscure circumstances, even though it is a copy of initialized
70653 ** page pFrom.
70654 */
70655 pTo->isInit = 0;
70656 rc = btreeInitPage(pTo);
70657 if( rc==SQLITE_OK0 ) rc = btreeComputeFreeSpace(pTo);
70658 if( rc!=SQLITE_OK0 ){
70659 *pRC = rc;
70660 return;
70661 }
70662
70663 /* If this is an auto-vacuum database, update the pointer-map entries
70664 ** for any b-tree or overflow pages that pTo now contains the pointers to.
70665 */
70666 if( ISAUTOVACUUM(pBt->autoVacuum) ){
70667 *pRC = setChildPtrmaps(pTo);
70668 }
70669 }
70670}
70671
70672/*
70673** This routine redistributes cells on the iParentIdx'th child of pParent
70674** (hereafter "the page") and up to 2 siblings so that all pages have about the
70675** same amount of free space. Usually a single sibling on either side of the
70676** page are used in the balancing, though both siblings might come from one
70677** side if the page is the first or last child of its parent. If the page
70678** has fewer than 2 siblings (something which can only happen if the page
70679** is a root page or a child of a root page) then all available siblings
70680** participate in the balancing.
70681**
70682** The number of siblings of the page might be increased or decreased by
70683** one or two in an effort to keep pages nearly full but not over full.
70684**
70685** Note that when this routine is called, some of the cells on the page
70686** might not actually be stored in MemPage.aData[]. This can happen
70687** if the page is overfull. This routine ensures that all cells allocated
70688** to the page and its siblings fit into MemPage.aData[] before returning.
70689**
70690** In the course of balancing the page and its siblings, cells may be
70691** inserted into or removed from the parent page (pParent). Doing so
70692** may cause the parent page to become overfull or underfull. If this
70693** happens, it is the responsibility of the caller to invoke the correct
70694** balancing routine to fix this problem (see the balance() routine).
70695**
70696** If this routine fails for any reason, it might leave the database
70697** in a corrupted state. So if this routine fails, the database should
70698** be rolled back.
70699**
70700** The third argument to this function, aOvflSpace, is a pointer to a
70701** buffer big enough to hold one page. If while inserting cells into the parent
70702** page (pParent) the parent page becomes overfull, this buffer is
70703** used to store the parent's overflow cells. Because this function inserts
70704** a maximum of four divider cells into the parent page, and the maximum
70705** size of a cell stored within an internal node is always less than 1/4
70706** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
70707** enough for all overflow cells.
70708**
70709** If aOvflSpace is set to a null pointer, this function returns
70710** SQLITE_NOMEM.
70711*/
70712static int balance_nonroot(
70713 MemPage *pParent, /* Parent page of siblings being balanced */
70714 int iParentIdx, /* Index of "the page" in pParent */
70715 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
70716 int isRoot, /* True if pParent is a root-page */
70717 int bBulk /* True if this call is part of a bulk load */
70718){
70719 BtShared *pBt; /* The whole database */
70720 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
70721 int nNew = 0; /* Number of pages in apNew[] */
70722 int nOld; /* Number of pages in apOld[] */
70723 int i, j, k; /* Loop counters */
70724 int nxDiv; /* Next divider slot in pParent->aCell[] */
70725 int rc = SQLITE_OK0; /* The return code */
70726 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
70727 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
70728 int usableSpace; /* Bytes in pPage beyond the header */
70729 int pageFlags; /* Value of pPage->aData[0] */
70730 int iSpace1 = 0; /* First unused byte of aSpace1[] */
70731 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
70732 int szScratch; /* Size of scratch memory requested */
70733 MemPage *apOld[NB3]; /* pPage and up to two siblings */
70734 MemPage *apNew[NB3+2]; /* pPage and up to NB siblings after balancing */
70735 u8 *pRight; /* Location in parent of right-sibling pointer */
70736 u8 *apDiv[NB3-1]; /* Divider cells in pParent */
70737 int cntNew[NB3+2]; /* Index in b.paCell[] of cell after i-th page */
70738 int cntOld[NB3+2]; /* Old index in b.apCell[] */
70739 int szNew[NB3+2]; /* Combined size of cells placed on i-th page */
70740 u8 *aSpace1; /* Space for copies of dividers cells */
70741 Pgno pgno; /* Temp var to store a page number in */
70742 u8 abDone[NB3+2]; /* True after i'th new page is populated */
70743 Pgno aPgno[NB3+2]; /* Page numbers of new pages before shuffling */
70744 Pgno aPgOrder[NB3+2]; /* Copy of aPgno[] used for sorting pages */
70745 u16 aPgFlags[NB3+2]; /* flags field of new pages before shuffling */
70746 CellArray b; /* Parsed information on cells being balanced */
70747
70748 memset(abDone, 0, sizeof(abDone));
70749 b.nCell = 0;
70750 b.apCell = 0;
70751 pBt = pParent->pBt;
70752 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
70753 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
70754
70755 /* At this point pParent may have at most one overflow cell. And if
70756 ** this overflow cell is present, it must be the cell with
70757 ** index iParentIdx. This scenario comes about when this function
70758 ** is called (indirectly) from sqlite3BtreeDelete().
70759 */
70760 assert( pParent->nOverflow==0 || pParent->nOverflow==1 )((void) (0));
70761 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx )((void) (0));
70762
70763 if( !aOvflSpace ){
70764 return SQLITE_NOMEM_BKPT7;
70765 }
70766 assert( pParent->nFree>=0 )((void) (0));
70767
70768 /* Find the sibling pages to balance. Also locate the cells in pParent
70769 ** that divide the siblings. An attempt is made to find NN siblings on
70770 ** either side of pPage. More siblings are taken from one side, however,
70771 ** if there are fewer than NN siblings on the other side. If pParent
70772 ** has NB or fewer children then all children of pParent are taken.
70773 **
70774 ** This loop also drops the divider cells from the parent page. This
70775 ** way, the remainder of the function does not have to deal with any
70776 ** overflow cells in the parent page, since if any existed they will
70777 ** have already been removed.
70778 */
70779 i = pParent->nOverflow + pParent->nCell;
70780 if( i<2 ){
70781 nxDiv = 0;
70782 }else{
70783 assert( bBulk==0 || bBulk==1 )((void) (0));
70784 if( iParentIdx==0 ){
70785 nxDiv = 0;
70786 }else if( iParentIdx==i ){
70787 nxDiv = i-2+bBulk;
70788 }else{
70789 nxDiv = iParentIdx-1;
70790 }
70791 i = 2-bBulk;
70792 }
70793 nOld = i+1;
70794 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
70795 pRight = &pParent->aData[pParent->hdrOffset+8];
70796 }else{
70797 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow)((pParent)->aData + ((pParent)->maskPage & ((&(
pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow)])[0]<<
8 | (&(pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow
)])[1])))
;
70798 }
70799 pgno = get4bytesqlite3Get4byte(pRight);
70800 while( 1 ){
70801 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
70802 if( rc ){
70803 memset(apOld, 0, (i+1)*sizeof(MemPage*));
70804 goto balance_cleanup;
70805 }
70806 if( apOld[i]->nFree<0 ){
70807 rc = btreeComputeFreeSpace(apOld[i]);
70808 if( rc ){
70809 memset(apOld, 0, (i)*sizeof(MemPage*));
70810 goto balance_cleanup;
70811 }
70812 }
70813 if( (i--)==0 ) break;
70814
70815 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
70816 apDiv[i] = pParent->apOvfl[0];
70817 pgno = get4bytesqlite3Get4byte(apDiv[i]);
70818 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
70819 pParent->nOverflow = 0;
70820 }else{
70821 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow)((pParent)->aData + ((pParent)->maskPage & ((&(
pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow)])[0]<<
8 | (&(pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow
)])[1])))
;
70822 pgno = get4bytesqlite3Get4byte(apDiv[i]);
70823 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
70824
70825 /* Drop the cell from the parent page. apDiv[i] still points to
70826 ** the cell within the parent, even though it has been dropped.
70827 ** This is safe because dropping a cell only overwrites the first
70828 ** four bytes of it, and this function does not need the first
70829 ** four bytes of the divider cell. So the pointer is safe to use
70830 ** later on.
70831 **
70832 ** But not if we are in secure-delete mode. In secure-delete mode,
70833 ** the dropCell() routine will overwrite the entire cell with zeroes.
70834 ** In this case, temporarily copy the cell into the aOvflSpace[]
70835 ** buffer. It will be copied out again as soon as the aSpace[] buffer
70836 ** is allocated. */
70837 if( pBt->btsFlags & BTS_FAST_SECURE0x000c ){
70838 int iOff;
70839
70840 iOff = SQLITE_PTR_TO_INT(apDiv[i])((int)(long int)(apDiv[i])) - SQLITE_PTR_TO_INT(pParent->aData)((int)(long int)(pParent->aData));
70841 if( (iOff+szNew[i])>(int)pBt->usableSize ){
70842 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(70842);
70843 memset(apOld, 0, (i+1)*sizeof(MemPage*));
70844 goto balance_cleanup;
70845 }else{
70846 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
70847 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
70848 }
70849 }
70850 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
70851 }
70852 }
70853
70854 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
70855 ** alignment */
70856 nMaxCells = nOld*(MX_CELL(pBt)((pBt->pageSize-8)/6) + ArraySize(pParent->apOvfl)((int)(sizeof(pParent->apOvfl)/sizeof(pParent->apOvfl[0
])))
);
70857 nMaxCells = (nMaxCells + 3)&~3;
70858
70859 /*
70860 ** Allocate space for memory structures
70861 */
70862 szScratch =
70863 nMaxCells*sizeof(u8*) /* b.apCell */
70864 + nMaxCells*sizeof(u16) /* b.szCell */
70865 + pBt->pageSize; /* aSpace1 */
70866
70867 assert( szScratch<=7*(int)pBt->pageSize )((void) (0));
70868 b.apCell = sqlite3StackAllocRaw(0, szScratch )sqlite3DbMallocRaw(0,szScratch);
70869 if( b.apCell==0 ){
70870 rc = SQLITE_NOMEM_BKPT7;
70871 goto balance_cleanup;
70872 }
70873 b.szCell = (u16*)&b.apCell[nMaxCells];
70874 aSpace1 = (u8*)&b.szCell[nMaxCells];
70875 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) )((void) (0));
70876
70877 /*
70878 ** Load pointers to all cells on sibling pages and the divider cells
70879 ** into the local b.apCell[] array. Make copies of the divider cells
70880 ** into space obtained from aSpace1[]. The divider cells have already
70881 ** been removed from pParent.
70882 **
70883 ** If the siblings are on leaf pages, then the child pointers of the
70884 ** divider cells are stripped from the cells before they are copied
70885 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
70886 ** child pointers. If siblings are not leaves, then all cell in
70887 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
70888 ** are alike.
70889 **
70890 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
70891 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
70892 */
70893 b.pRef = apOld[0];
70894 leafCorrection = b.pRef->leaf*4;
70895 leafData = b.pRef->intKeyLeaf;
70896 for(i=0; i<nOld; i++){
70897 MemPage *pOld = apOld[i];
70898 int limit = pOld->nCell;
70899 u8 *aData = pOld->aData;
70900 u16 maskPage = pOld->maskPage;
70901 u8 *piCell = aData + pOld->cellOffset;
70902 u8 *piEnd;
70903 VVA_ONLY( int nCellAtStart = b.nCell; )
70904
70905 /* Verify that all sibling pages are of the same "type" (table-leaf,
70906 ** table-interior, index-leaf, or index-interior).
70907 */
70908 if( pOld->aData[0]!=apOld[0]->aData[0] ){
70909 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(70909);
70910 goto balance_cleanup;
70911 }
70912
70913 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
70914 ** contains overflow cells, include them in the b.apCell[] array
70915 ** in the correct spot.
70916 **
70917 ** Note that when there are multiple overflow cells, it is always the
70918 ** case that they are sequential and adjacent. This invariant arises
70919 ** because multiple overflows can only occurs when inserting divider
70920 ** cells into a parent on a prior balance, and divider cells are always
70921 ** adjacent and are inserted in order. There is an assert() tagged
70922 ** with "NOTE 1" in the overflow cell insertion loop to prove this
70923 ** invariant.
70924 **
70925 ** This must be done in advance. Once the balance starts, the cell
70926 ** offset section of the btree page will be overwritten and we will no
70927 ** long be able to find the cells if a pointer to each cell is not saved
70928 ** first.
70929 */
70930 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
70931 if( pOld->nOverflow>0 ){
70932 if( limit<pOld->aiOvfl[0] ){
70933 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(70933);
70934 goto balance_cleanup;
70935 }
70936 limit = pOld->aiOvfl[0];
70937 for(j=0; j<limit; j++){
70938 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)((piCell)[0]<<8 | (piCell)[1]));
70939 piCell += 2;
70940 b.nCell++;
70941 }
70942 for(k=0; k<pOld->nOverflow; k++){
70943 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] )((void) (0));/* NOTE 1 */
70944 b.apCell[b.nCell] = pOld->apOvfl[k];
70945 b.nCell++;
70946 }
70947 }
70948 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
70949 while( piCell<piEnd ){
70950 assert( b.nCell<nMaxCells )((void) (0));
70951 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)((piCell)[0]<<8 | (piCell)[1]));
70952 piCell += 2;
70953 b.nCell++;
70954 }
70955 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) )((void) (0));
70956
70957 cntOld[i] = b.nCell;
70958 if( i<nOld-1 && !leafData){
70959 u16 sz = (u16)szNew[i];
70960 u8 *pTemp;
70961 assert( b.nCell<nMaxCells )((void) (0));
70962 b.szCell[b.nCell] = sz;
70963 pTemp = &aSpace1[iSpace1];
70964 iSpace1 += sz;
70965 assert( sz<=pBt->maxLocal+23 )((void) (0));
70966 assert( iSpace1 <= (int)pBt->pageSize )((void) (0));
70967 memcpy(pTemp, apDiv[i], sz);
70968 b.apCell[b.nCell] = pTemp+leafCorrection;
70969 assert( leafCorrection==0 || leafCorrection==4 )((void) (0));
70970 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
70971 if( !pOld->leaf ){
70972 assert( leafCorrection==0 )((void) (0));
70973 assert( pOld->hdrOffset==0 )((void) (0));
70974 /* The right pointer of the child page pOld becomes the left
70975 ** pointer of the divider cell */
70976 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
70977 }else{
70978 assert( leafCorrection==4 )((void) (0));
70979 while( b.szCell[b.nCell]<4 ){
70980 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
70981 ** does exist, pad it with 0x00 bytes. */
70982 assert( b.szCell[b.nCell]==3 || CORRUPT_DB )((void) (0));
70983 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB )((void) (0));
70984 aSpace1[iSpace1++] = 0x00;
70985 b.szCell[b.nCell]++;
70986 }
70987 }
70988 b.nCell++;
70989 }
70990 }
70991
70992 /*
70993 ** Figure out the number of pages needed to hold all b.nCell cells.
70994 ** Store this number in "k". Also compute szNew[] which is the total
70995 ** size of all cells on the i-th page and cntNew[] which is the index
70996 ** in b.apCell[] of the cell that divides page i from page i+1.
70997 ** cntNew[k] should equal b.nCell.
70998 **
70999 ** Values computed by this block:
71000 **
71001 ** k: The total number of sibling pages
71002 ** szNew[i]: Spaced used on the i-th sibling page.
71003 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
71004 ** the right of the i-th sibling page.
71005 ** usableSpace: Number of bytes of space available on each sibling.
71006 **
71007 */
71008 usableSpace = pBt->usableSize - 12 + leafCorrection;
71009 for(i=k=0; i<nOld; i++, k++){
71010 MemPage *p = apOld[i];
71011 b.apEnd[k] = p->aDataEnd;
71012 b.ixNx[k] = cntOld[i];
71013 if( k && b.ixNx[k]==b.ixNx[k-1] ){
71014 k--; /* Omit b.ixNx[] entry for child pages with no cells */
71015 }
71016 if( !leafData ){
71017 k++;
71018 b.apEnd[k] = pParent->aDataEnd;
71019 b.ixNx[k] = cntOld[i]+1;
71020 }
71021 assert( p->nFree>=0 )((void) (0));
71022 szNew[i] = usableSpace - p->nFree;
71023 for(j=0; j<p->nOverflow; j++){
71024 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
71025 }
71026 cntNew[i] = cntOld[i];
71027 }
71028 k = nOld;
71029 for(i=0; i<k; i++){
71030 int sz;
71031 while( szNew[i]>usableSpace ){
71032 if( i+1>=k ){
71033 k = i+2;
71034 if( k>NB3+2 ){ rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71034); goto balance_cleanup; }
71035 szNew[k-1] = 0;
71036 cntNew[k-1] = b.nCell;
71037 }
71038 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
71039 szNew[i] -= sz;
71040 if( !leafData ){
71041 if( cntNew[i]<b.nCell ){
71042 sz = 2 + cachedCellSize(&b, cntNew[i]);
71043 }else{
71044 sz = 0;
71045 }
71046 }
71047 szNew[i+1] += sz;
71048 cntNew[i]--;
71049 }
71050 while( cntNew[i]<b.nCell ){
71051 sz = 2 + cachedCellSize(&b, cntNew[i]);
71052 if( szNew[i]+sz>usableSpace ) break;
71053 szNew[i] += sz;
71054 cntNew[i]++;
71055 if( !leafData ){
71056 if( cntNew[i]<b.nCell ){
71057 sz = 2 + cachedCellSize(&b, cntNew[i]);
71058 }else{
71059 sz = 0;
71060 }
71061 }
71062 szNew[i+1] -= sz;
71063 }
71064 if( cntNew[i]>=b.nCell ){
71065 k = i+1;
71066 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
71067 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71067);
71068 goto balance_cleanup;
71069 }
71070 }
71071
71072 /*
71073 ** The packing computed by the previous block is biased toward the siblings
71074 ** on the left side (siblings with smaller keys). The left siblings are
71075 ** always nearly full, while the right-most sibling might be nearly empty.
71076 ** The next block of code attempts to adjust the packing of siblings to
71077 ** get a better balance.
71078 **
71079 ** This adjustment is more than an optimization. The packing above might
71080 ** be so out of balance as to be illegal. For example, the right-most
71081 ** sibling might be completely empty. This adjustment is not optional.
71082 */
71083 for(i=k-1; i>0; i--){
71084 int szRight = szNew[i]; /* Size of sibling on the right */
71085 int szLeft = szNew[i-1]; /* Size of sibling on the left */
71086 int r; /* Index of right-most cell in left sibling */
71087 int d; /* Index of first cell to the left of right sibling */
71088
71089 r = cntNew[i-1] - 1;
71090 d = r + 1 - leafData;
71091 (void)cachedCellSize(&b, d);
71092 do{
71093 assert( d<nMaxCells )((void) (0));
71094 assert( r<nMaxCells )((void) (0));
71095 (void)cachedCellSize(&b, r);
71096 if( szRight!=0
71097 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
71098 break;
71099 }
71100 szRight += b.szCell[d] + 2;
71101 szLeft -= b.szCell[r] + 2;
71102 cntNew[i-1] = r;
71103 r--;
71104 d--;
71105 }while( r>=0 );
71106 szNew[i] = szRight;
71107 szNew[i-1] = szLeft;
71108 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
71109 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71109);
71110 goto balance_cleanup;
71111 }
71112 }
71113
71114 /* Sanity check: For a non-corrupt database file one of the follwing
71115 ** must be true:
71116 ** (1) We found one or more cells (cntNew[0])>0), or
71117 ** (2) pPage is a virtual root page. A virtual root page is when
71118 ** the real root page is page 1 and we are the only child of
71119 ** that page.
71120 */
71121 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB)((void) (0));
71122 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
71123 apOld[0]->pgno, apOld[0]->nCell,
71124 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
71125 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
71126 ));
71127
71128 /*
71129 ** Allocate k new pages. Reuse old pages where possible.
71130 */
71131 pageFlags = apOld[0]->aData[0];
71132 for(i=0; i<k; i++){
71133 MemPage *pNew;
71134 if( i<nOld ){
71135 pNew = apNew[i] = apOld[i];
71136 apOld[i] = 0;
71137 rc = sqlite3PagerWrite(pNew->pDbPage);
71138 nNew++;
71139 if( rc ) goto balance_cleanup;
71140 }else{
71141 assert( i>0 )((void) (0));
71142 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
71143 if( rc ) goto balance_cleanup;
71144 zeroPage(pNew, pageFlags);
71145 apNew[i] = pNew;
71146 nNew++;
71147 cntOld[i] = b.nCell;
71148
71149 /* Set the pointer-map entry for the new sibling page. */
71150 if( ISAUTOVACUUM(pBt->autoVacuum) ){
71151 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE5, pParent->pgno, &rc);
71152 if( rc!=SQLITE_OK0 ){
71153 goto balance_cleanup;
71154 }
71155 }
71156 }
71157 }
71158
71159 /*
71160 ** Reassign page numbers so that the new pages are in ascending order.
71161 ** This helps to keep entries in the disk file in order so that a scan
71162 ** of the table is closer to a linear scan through the file. That in turn
71163 ** helps the operating system to deliver pages from the disk more rapidly.
71164 **
71165 ** An O(n^2) insertion sort algorithm is used, but since n is never more
71166 ** than (NB+2) (a small constant), that should not be a problem.
71167 **
71168 ** When NB==3, this one optimization makes the database about 25% faster
71169 ** for large insertions and deletions.
71170 */
71171 for(i=0; i<nNew; i++){
71172 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
71173 aPgFlags[i] = apNew[i]->pDbPage->flags;
71174 for(j=0; j<i; j++){
71175 if( aPgno[j]==aPgno[i] ){
71176 /* This branch is taken if the set of sibling pages somehow contains
71177 ** duplicate entries. This can happen if the database is corrupt.
71178 ** It would be simpler to detect this as part of the loop below, but
71179 ** we do the detection here in order to avoid populating the pager
71180 ** cache with two separate objects associated with the same
71181 ** page number. */
71182 assert( CORRUPT_DB )((void) (0));
71183 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71183);
71184 goto balance_cleanup;
71185 }
71186 }
71187 }
71188 for(i=0; i<nNew; i++){
71189 int iBest = 0; /* aPgno[] index of page number to use */
71190 for(j=1; j<nNew; j++){
71191 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
71192 }
71193 pgno = aPgOrder[iBest];
71194 aPgOrder[iBest] = 0xffffffff;
71195 if( iBest!=i ){
71196 if( iBest>i ){
71197 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
71198 }
71199 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
71200 apNew[i]->pgno = pgno;
71201 }
71202 }
71203
71204 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
71205 "%d(%d nc=%d) %d(%d nc=%d)\n",
71206 apNew[0]->pgno, szNew[0], cntNew[0],
71207 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
71208 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
71209 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
71210 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
71211 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
71212 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
71213 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
71214 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
71215 ));
71216
71217 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
71218 put4bytesqlite3Put4byte(pRight, apNew[nNew-1]->pgno);
71219
71220 /* If the sibling pages are not leaves, ensure that the right-child pointer
71221 ** of the right-most new sibling page is set to the value that was
71222 ** originally in the same field of the right-most old sibling page. */
71223 if( (pageFlags & PTF_LEAF0x08)==0 && nOld!=nNew ){
71224 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
71225 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
71226 }
71227
71228 /* Make any required updates to pointer map entries associated with
71229 ** cells stored on sibling pages following the balance operation. Pointer
71230 ** map entries associated with divider cells are set by the insertCell()
71231 ** routine. The associated pointer map entries are:
71232 **
71233 ** a) if the cell contains a reference to an overflow chain, the
71234 ** entry associated with the first page in the overflow chain, and
71235 **
71236 ** b) if the sibling pages are not leaves, the child page associated
71237 ** with the cell.
71238 **
71239 ** If the sibling pages are not leaves, then the pointer map entry
71240 ** associated with the right-child of each sibling may also need to be
71241 ** updated. This happens below, after the sibling pages have been
71242 ** populated, not here.
71243 */
71244 if( ISAUTOVACUUM(pBt->autoVacuum) ){
71245 MemPage *pOld;
71246 MemPage *pNew = pOld = apNew[0];
71247 int cntOldNext = pNew->nCell + pNew->nOverflow;
71248 int iNew = 0;
71249 int iOld = 0;
71250
71251 for(i=0; i<b.nCell; i++){
71252 u8 *pCell = b.apCell[i];
71253 while( i==cntOldNext ){
71254 iOld++;
71255 assert( iOld<nNew || iOld<nOld )((void) (0));
71256 assert( iOld>=0 && iOld<NB )((void) (0));
71257 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
71258 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
71259 }
71260 if( i==cntNew[iNew] ){
71261 pNew = apNew[++iNew];
71262 if( !leafData ) continue;
71263 }
71264
71265 /* Cell pCell is destined for new sibling page pNew. Originally, it
71266 ** was either part of sibling page iOld (possibly an overflow cell),
71267 ** or else the divider cell to the left of sibling page iOld. So,
71268 ** if sibling page iOld had the same page number as pNew, and if
71269 ** pCell really was a part of sibling page iOld (not a divider or
71270 ** overflow cell), we can skip updating the pointer map entries. */
71271 if( iOld>=nNew
71272 || pNew->pgno!=aPgno[iOld]
71273 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)(((uptr)(pCell)>=(uptr)(pOld->aData))&&((uptr)(
pCell)<(uptr)(pOld->aDataEnd)))
71274 ){
71275 if( !leafCorrection ){
71276 ptrmapPut(pBt, get4bytesqlite3Get4byte(pCell), PTRMAP_BTREE5, pNew->pgno, &rc);
71277 }
71278 if( cachedCellSize(&b,i)>pNew->minLocal ){
71279 ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
71280 }
71281 if( rc ) goto balance_cleanup;
71282 }
71283 }
71284 }
71285
71286 /* Insert new divider cells into pParent. */
71287 for(i=0; i<nNew-1; i++){
71288 u8 *pCell;
71289 u8 *pTemp;
71290 int sz;
71291 MemPage *pNew = apNew[i];
71292 j = cntNew[i];
71293
71294 assert( j<nMaxCells )((void) (0));
71295 assert( b.apCell[j]!=0 )((void) (0));
71296 pCell = b.apCell[j];
71297 sz = b.szCell[j] + leafCorrection;
71298 pTemp = &aOvflSpace[iOvflSpace];
71299 if( !pNew->leaf ){
71300 memcpy(&pNew->aData[8], pCell, 4);
71301 }else if( leafData ){
71302 /* If the tree is a leaf-data tree, and the siblings are leaves,
71303 ** then there is no divider cell in b.apCell[]. Instead, the divider
71304 ** cell consists of the integer key for the right-most cell of
71305 ** the sibling-page assembled above only.
71306 */
71307 CellInfo info;
71308 j--;
71309 pNew->xParseCell(pNew, b.apCell[j], &info);
71310 pCell = pTemp;
71311 sz = 4 + putVarintsqlite3PutVarint(&pCell[4], info.nKey);
71312 pTemp = 0;
71313 }else{
71314 pCell -= 4;
71315 /* Obscure case for non-leaf-data trees: If the cell at pCell was
71316 ** previously stored on a leaf node, and its reported size was 4
71317 ** bytes, then it may actually be smaller than this
71318 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
71319 ** any cell). But it is important to pass the correct size to
71320 ** insertCell(), so reparse the cell now.
71321 **
71322 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
71323 ** and WITHOUT ROWID tables with exactly one column which is the
71324 ** primary key.
71325 */
71326 if( b.szCell[j]==4 ){
71327 assert(leafCorrection==4)((void) (0));
71328 sz = pParent->xCellSize(pParent, pCell);
71329 }
71330 }
71331 iOvflSpace += sz;
71332 assert( sz<=pBt->maxLocal+23 )((void) (0));
71333 assert( iOvflSpace <= (int)pBt->pageSize )((void) (0));
71334 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
71335 if( rc!=SQLITE_OK0 ) goto balance_cleanup;
71336 assert( sqlite3PagerIswriteable(pParent->pDbPage) )((void) (0));
71337 }
71338
71339 /* Now update the actual sibling pages. The order in which they are updated
71340 ** is important, as this code needs to avoid disrupting any page from which
71341 ** cells may still to be read. In practice, this means:
71342 **
71343 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
71344 ** then it is not safe to update page apNew[iPg] until after
71345 ** the left-hand sibling apNew[iPg-1] has been updated.
71346 **
71347 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
71348 ** then it is not safe to update page apNew[iPg] until after
71349 ** the right-hand sibling apNew[iPg+1] has been updated.
71350 **
71351 ** If neither of the above apply, the page is safe to update.
71352 **
71353 ** The iPg value in the following loop starts at nNew-1 goes down
71354 ** to 0, then back up to nNew-1 again, thus making two passes over
71355 ** the pages. On the initial downward pass, only condition (1) above
71356 ** needs to be tested because (2) will always be true from the previous
71357 ** step. On the upward pass, both conditions are always true, so the
71358 ** upwards pass simply processes pages that were missed on the downward
71359 ** pass.
71360 */
71361 for(i=1-nNew; i<nNew; i++){
71362 int iPg = i<0 ? -i : i;
71363 assert( iPg>=0 && iPg<nNew )((void) (0));
71364 if( abDone[iPg] ) continue; /* Skip pages already processed */
71365 if( i>=0 /* On the upwards pass, or... */
71366 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
71367 ){
71368 int iNew;
71369 int iOld;
71370 int nNewCell;
71371
71372 /* Verify condition (1): If cells are moving left, update iPg
71373 ** only after iPg-1 has already been updated. */
71374 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] )((void) (0));
71375
71376 /* Verify condition (2): If cells are moving right, update iPg
71377 ** only after iPg+1 has already been updated. */
71378 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] )((void) (0));
71379
71380 if( iPg==0 ){
71381 iNew = iOld = 0;
71382 nNewCell = cntNew[0];
71383 }else{
71384 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
71385 iNew = cntNew[iPg-1] + !leafData;
71386 nNewCell = cntNew[iPg] - iNew;
71387 }
71388
71389 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
71390 if( rc ) goto balance_cleanup;
71391 abDone[iPg]++;
71392 apNew[iPg]->nFree = usableSpace-szNew[iPg];
71393 assert( apNew[iPg]->nOverflow==0 )((void) (0));
71394 assert( apNew[iPg]->nCell==nNewCell )((void) (0));
71395 }
71396 }
71397
71398 /* All pages have been processed exactly once */
71399 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 )((void) (0));
71400
71401 assert( nOld>0 )((void) (0));
71402 assert( nNew>0 )((void) (0));
71403
71404 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
71405 /* The root page of the b-tree now contains no cells. The only sibling
71406 ** page is the right-child of the parent. Copy the contents of the
71407 ** child page into the parent, decreasing the overall height of the
71408 ** b-tree structure by one. This is described as the "balance-shallower"
71409 ** sub-algorithm in some documentation.
71410 **
71411 ** If this is an auto-vacuum database, the call to copyNodeContent()
71412 ** sets all pointer-map entries corresponding to database image pages
71413 ** for which the pointer is stored within the content being copied.
71414 **
71415 ** It is critical that the child page be defragmented before being
71416 ** copied into the parent, because if the parent is page 1 then it will
71417 ** by smaller than the child due to the database header, and so all the
71418 ** free space needs to be up front.
71419 */
71420 assert( nNew==1 || CORRUPT_DB )((void) (0));
71421 rc = defragmentPage(apNew[0], -1);
71422 testcase( rc!=SQLITE_OK );
71423 assert( apNew[0]->nFree ==((void) (0))
71424 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset((void) (0))
71425 - apNew[0]->nCell*2)((void) (0))
71426 || rc!=SQLITE_OK((void) (0))
71427 )((void) (0));
71428 copyNodeContent(apNew[0], pParent, &rc);
71429 freePage(apNew[0], &rc);
71430 }else if( ISAUTOVACUUM(pBt->autoVacuum) && !leafCorrection ){
71431 /* Fix the pointer map entries associated with the right-child of each
71432 ** sibling page. All other pointer map entries have already been taken
71433 ** care of. */
71434 for(i=0; i<nNew; i++){
71435 u32 key = get4bytesqlite3Get4byte(&apNew[i]->aData[8]);
71436 ptrmapPut(pBt, key, PTRMAP_BTREE5, apNew[i]->pgno, &rc);
71437 }
71438 }
71439
71440 assert( pParent->isInit )((void) (0));
71441 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
71442 nOld, nNew, b.nCell));
71443
71444 /* Free any old pages that were not reused as new pages.
71445 */
71446 for(i=nNew; i<nOld; i++){
71447 freePage(apOld[i], &rc);
71448 }
71449
71450#if 0
71451 if( ISAUTOVACUUM(pBt->autoVacuum) && rc==SQLITE_OK0 && apNew[0]->isInit ){
71452 /* The ptrmapCheckPages() contains assert() statements that verify that
71453 ** all pointer map pages are set correctly. This is helpful while
71454 ** debugging. This is usually disabled because a corrupt database may
71455 ** cause an assert() statement to fail. */
71456 ptrmapCheckPages(apNew, nNew);
71457 ptrmapCheckPages(&pParent, 1);
71458 }
71459#endif
71460
71461 /*
71462 ** Cleanup before returning.
71463 */
71464balance_cleanup:
71465 sqlite3StackFree(0, b.apCell)sqlite3DbFree(0,b.apCell);
71466 for(i=0; i<nOld; i++){
71467 releasePage(apOld[i]);
71468 }
71469 for(i=0; i<nNew; i++){
71470 releasePage(apNew[i]);
71471 }
71472
71473 return rc;
71474}
71475
71476
71477/*
71478** This function is called when the root page of a b-tree structure is
71479** overfull (has one or more overflow pages).
71480**
71481** A new child page is allocated and the contents of the current root
71482** page, including overflow cells, are copied into the child. The root
71483** page is then overwritten to make it an empty page with the right-child
71484** pointer pointing to the new page.
71485**
71486** Before returning, all pointer-map entries corresponding to pages
71487** that the new child-page now contains pointers to are updated. The
71488** entry corresponding to the new right-child pointer of the root
71489** page is also updated.
71490**
71491** If successful, *ppChild is set to contain a reference to the child
71492** page and SQLITE_OK is returned. In this case the caller is required
71493** to call releasePage() on *ppChild exactly once. If an error occurs,
71494** an error code is returned and *ppChild is set to 0.
71495*/
71496static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
71497 int rc; /* Return value from subprocedures */
71498 MemPage *pChild = 0; /* Pointer to a new child page */
71499 Pgno pgnoChild = 0; /* Page number of the new child page */
71500 BtShared *pBt = pRoot->pBt; /* The BTree */
71501
71502 assert( pRoot->nOverflow>0 )((void) (0));
71503 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
71504
71505 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
71506 ** page that will become the new right-child of pPage. Copy the contents
71507 ** of the node stored on pRoot into the new child page.
71508 */
71509 rc = sqlite3PagerWrite(pRoot->pDbPage);
71510 if( rc==SQLITE_OK0 ){
71511 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
71512 copyNodeContent(pRoot, pChild, &rc);
71513 if( ISAUTOVACUUM(pBt->autoVacuum) ){
71514 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE5, pRoot->pgno, &rc);
71515 }
71516 }
71517 if( rc ){
71518 *ppChild = 0;
71519 releasePage(pChild);
71520 return rc;
71521 }
71522 assert( sqlite3PagerIswriteable(pChild->pDbPage) )((void) (0));
71523 assert( sqlite3PagerIswriteable(pRoot->pDbPage) )((void) (0));
71524 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB )((void) (0));
71525
71526 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
71527
71528 /* Copy the overflow cells from pRoot to pChild */
71529 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
71530 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
71531 memcpy(pChild->apOvfl, pRoot->apOvfl,
71532 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
71533 pChild->nOverflow = pRoot->nOverflow;
71534
71535 /* Zero the contents of pRoot. Then install pChild as the right-child. */
71536 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF0x08);
71537 put4bytesqlite3Put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
71538
71539 *ppChild = pChild;
71540 return SQLITE_OK0;
71541}
71542
71543/*
71544** The page that pCur currently points to has just been modified in
71545** some way. This function figures out if this modification means the
71546** tree needs to be balanced, and if so calls the appropriate balancing
71547** routine. Balancing routines are:
71548**
71549** balance_quick()
71550** balance_deeper()
71551** balance_nonroot()
71552*/
71553static int balance(BtCursor *pCur){
71554 int rc = SQLITE_OK0;
71555 const int nMin = pCur->pBt->usableSize * 2 / 3;
71556 u8 aBalanceQuickSpace[13];
71557 u8 *pFree = 0;
71558
71559 VVA_ONLY( int balance_quick_called = 0 );
71560 VVA_ONLY( int balance_deeper_called = 0 );
71561
71562 do {
71563 int iPage = pCur->iPage;
71564 MemPage *pPage = pCur->pPage;
71565
71566 if( NEVER(pPage->nFree<0)(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
71567 if( iPage==0 ){
71568 if( pPage->nOverflow ){
71569 /* The root page of the b-tree is overfull. In this case call the
71570 ** balance_deeper() function to create a new child for the root-page
71571 ** and copy the current contents of the root-page to it. The
71572 ** next iteration of the do-loop will balance the child page.
71573 */
71574 assert( balance_deeper_called==0 )((void) (0));
71575 VVA_ONLY( balance_deeper_called++ );
71576 rc = balance_deeper(pPage, &pCur->apPage[1]);
71577 if( rc==SQLITE_OK0 ){
71578 pCur->iPage = 1;
71579 pCur->ix = 0;
71580 pCur->aiIdx[0] = 0;
71581 pCur->apPage[0] = pPage;
71582 pCur->pPage = pCur->apPage[1];
71583 assert( pCur->pPage->nOverflow )((void) (0));
71584 }
71585 }else{
71586 break;
71587 }
71588 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
71589 break;
71590 }else{
71591 MemPage * const pParent = pCur->apPage[iPage-1];
71592 int const iIdx = pCur->aiIdx[iPage-1];
71593
71594 rc = sqlite3PagerWrite(pParent->pDbPage);
71595 if( rc==SQLITE_OK0 && pParent->nFree<0 ){
71596 rc = btreeComputeFreeSpace(pParent);
71597 }
71598 if( rc==SQLITE_OK0 ){
71599#ifndef SQLITE_OMIT_QUICKBALANCE
71600 if( pPage->intKeyLeaf
71601 && pPage->nOverflow==1
71602 && pPage->aiOvfl[0]==pPage->nCell
71603 && pParent->pgno!=1
71604 && pParent->nCell==iIdx
71605 ){
71606 /* Call balance_quick() to create a new sibling of pPage on which
71607 ** to store the overflow cell. balance_quick() inserts a new cell
71608 ** into pParent, which may cause pParent overflow. If this
71609 ** happens, the next iteration of the do-loop will balance pParent
71610 ** use either balance_nonroot() or balance_deeper(). Until this
71611 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
71612 ** buffer.
71613 **
71614 ** The purpose of the following assert() is to check that only a
71615 ** single call to balance_quick() is made for each call to this
71616 ** function. If this were not verified, a subtle bug involving reuse
71617 ** of the aBalanceQuickSpace[] might sneak in.
71618 */
71619 assert( balance_quick_called==0 )((void) (0));
71620 VVA_ONLY( balance_quick_called++ );
71621 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
71622 }else
71623#endif
71624 {
71625 /* In this case, call balance_nonroot() to redistribute cells
71626 ** between pPage and up to 2 of its sibling pages. This involves
71627 ** modifying the contents of pParent, which may cause pParent to
71628 ** become overfull or underfull. The next iteration of the do-loop
71629 ** will balance the parent page to correct this.
71630 **
71631 ** If the parent page becomes overfull, the overflow cell or cells
71632 ** are stored in the pSpace buffer allocated immediately below.
71633 ** A subsequent iteration of the do-loop will deal with this by
71634 ** calling balance_nonroot() (balance_deeper() may be called first,
71635 ** but it doesn't deal with overflow cells - just moves them to a
71636 ** different page). Once this subsequent call to balance_nonroot()
71637 ** has completed, it is safe to release the pSpace buffer used by
71638 ** the previous call, as the overflow cell data will have been
71639 ** copied either into the body of a database page or into the new
71640 ** pSpace buffer passed to the latter call to balance_nonroot().
71641 */
71642 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
71643 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
71644 pCur->hints&BTREE_BULKLOAD0x00000001);
71645 if( pFree ){
71646 /* If pFree is not NULL, it points to the pSpace buffer used
71647 ** by a previous call to balance_nonroot(). Its contents are
71648 ** now stored either on real database pages or within the
71649 ** new pSpace buffer, so it may be safely freed here. */
71650 sqlite3PageFree(pFree);
71651 }
71652
71653 /* The pSpace buffer will be freed after the next call to
71654 ** balance_nonroot(), or just before this function returns, whichever
71655 ** comes first. */
71656 pFree = pSpace;
71657 }
71658 }
71659
71660 pPage->nOverflow = 0;
71661
71662 /* The next iteration of the do-loop balances the parent page. */
71663 releasePage(pPage);
71664 pCur->iPage--;
71665 assert( pCur->iPage>=0 )((void) (0));
71666 pCur->pPage = pCur->apPage[pCur->iPage];
71667 }
71668 }while( rc==SQLITE_OK0 );
71669
71670 if( pFree ){
71671 sqlite3PageFree(pFree);
71672 }
71673 return rc;
71674}
71675
71676/* Overwrite content from pX into pDest. Only do the write if the
71677** content is different from what is already there.
71678*/
71679static int btreeOverwriteContent(
71680 MemPage *pPage, /* MemPage on which writing will occur */
71681 u8 *pDest, /* Pointer to the place to start writing */
71682 const BtreePayload *pX, /* Source of data to write */
71683 int iOffset, /* Offset of first byte to write */
71684 int iAmt /* Number of bytes to be written */
71685){
71686 int nData = pX->nData - iOffset;
71687 if( nData<=0 ){
71688 /* Overwritting with zeros */
71689 int i;
71690 for(i=0; i<iAmt && pDest[i]==0; i++){}
71691 if( i<iAmt ){
71692 int rc = sqlite3PagerWrite(pPage->pDbPage);
71693 if( rc ) return rc;
71694 memset(pDest + i, 0, iAmt - i);
71695 }
71696 }else{
71697 if( nData<iAmt ){
71698 /* Mixed read data and zeros at the end. Make a recursive call
71699 ** to write the zeros then fall through to write the real data */
71700 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
71701 iAmt-nData);
71702 if( rc ) return rc;
71703 iAmt = nData;
71704 }
71705 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
71706 int rc = sqlite3PagerWrite(pPage->pDbPage);
71707 if( rc ) return rc;
71708 /* In a corrupt database, it is possible for the source and destination
71709 ** buffers to overlap. This is harmless since the database is already
71710 ** corrupt but it does cause valgrind and ASAN warnings. So use
71711 ** memmove(). */
71712 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
71713 }
71714 }
71715 return SQLITE_OK0;
71716}
71717
71718/*
71719** Overwrite the cell that cursor pCur is pointing to with fresh content
71720** contained in pX.
71721*/
71722static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
71723 int iOffset; /* Next byte of pX->pData to write */
71724 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
71725 int rc; /* Return code */
71726 MemPage *pPage = pCur->pPage; /* Page being written */
71727 BtShared *pBt; /* Btree */
71728 Pgno ovflPgno; /* Next overflow page to write */
71729 u32 ovflPageSize; /* Size to write on overflow page */
71730
71731 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
71732 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(71732);
71733 }
71734 /* Overwrite the local portion first */
71735 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
71736 0, pCur->info.nLocal);
71737 if( rc ) return rc;
71738 if( pCur->info.nLocal==nTotal ) return SQLITE_OK0;
71739
71740 /* Now overwrite the overflow pages */
71741 iOffset = pCur->info.nLocal;
71742 assert( nTotal>=0 )((void) (0));
71743 assert( iOffset>=0 )((void) (0));
71744 ovflPgno = get4bytesqlite3Get4byte(pCur->info.pPayload + iOffset);
71745 pBt = pPage->pBt;
71746 ovflPageSize = pBt->usableSize - 4;
71747 do{
71748 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
71749 if( rc ) return rc;
71750 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
71751 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(71751);
71752 }else{
71753 if( iOffset+ovflPageSize<(u32)nTotal ){
71754 ovflPgno = get4bytesqlite3Get4byte(pPage->aData);
71755 }else{
71756 ovflPageSize = nTotal - iOffset;
71757 }
71758 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
71759 iOffset, ovflPageSize);
71760 }
71761 sqlite3PagerUnref(pPage->pDbPage);
71762 if( rc ) return rc;
71763 iOffset += ovflPageSize;
71764 }while( iOffset<nTotal );
71765 return SQLITE_OK0;
71766}
71767
71768
71769/*
71770** Insert a new record into the BTree. The content of the new record
71771** is described by the pX object. The pCur cursor is used only to
71772** define what table the record should be inserted into, and is left
71773** pointing at a random location.
71774**
71775** For a table btree (used for rowid tables), only the pX.nKey value of
71776** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
71777** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
71778** hold the content of the row.
71779**
71780** For an index btree (used for indexes and WITHOUT ROWID tables), the
71781** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
71782** pX.pData,nData,nZero fields must be zero.
71783**
71784** If the seekResult parameter is non-zero, then a successful call to
71785** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
71786** been performed. In other words, if seekResult!=0 then the cursor
71787** is currently pointing to a cell that will be adjacent to the cell
71788** to be inserted. If seekResult<0 then pCur points to a cell that is
71789** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
71790** that is larger than (pKey,nKey).
71791**
71792** If seekResult==0, that means pCur is pointing at some unknown location.
71793** In that case, this routine must seek the cursor to the correct insertion
71794** point for (pKey,nKey) before doing the insertion. For index btrees,
71795** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
71796** key values and pX->aMem can be used instead of pX->pKey to avoid having
71797** to decode the key.
71798*/
71799SQLITE_PRIVATEstatic int sqlite3BtreeInsert(
71800 BtCursor *pCur, /* Insert data into the table of this cursor */
71801 const BtreePayload *pX, /* Content of the row to be inserted */
71802 int flags, /* True if this is likely an append */
71803 int seekResult /* Result of prior MovetoUnpacked() call */
71804){
71805 int rc;
71806 int loc = seekResult; /* -1: before desired location +1: after */
71807 int szNew = 0;
71808 int idx;
71809 MemPage *pPage;
71810 Btree *p = pCur->pBtree;
71811 BtShared *pBt = p->pBt;
71812 unsigned char *oldCell;
71813 unsigned char *newCell = 0;
71814
71815 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags )((void) (0));
71816
71817 if( pCur->eState==CURSOR_FAULT4 ){
71818 assert( pCur->skipNext!=SQLITE_OK )((void) (0));
71819 return pCur->skipNext;
71820 }
71821
71822 assert( cursorOwnsBtShared(pCur) )((void) (0));
71823 assert( (pCur->curFlags & BTCF_WriteFlag)!=0((void) (0))
71824 && pBt->inTransaction==TRANS_WRITE((void) (0))
71825 && (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
71826 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) )((void) (0));
71827
71828 /* Assert that the caller has been consistent. If this cursor was opened
71829 ** expecting an index b-tree, then the caller should be inserting blob
71830 ** keys with no associated data. If the cursor was opened expecting an
71831 ** intkey table, the caller should be inserting integer keys with a
71832 ** blob of associated data. */
71833 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) )((void) (0));
71834
71835 /* Save the positions of any other cursors open on this table.
71836 **
71837 ** In some cases, the call to btreeMoveto() below is a no-op. For
71838 ** example, when inserting data into a table with auto-generated integer
71839 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
71840 ** integer key to use. It then calls this function to actually insert the
71841 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
71842 ** that the cursor is already where it needs to be and returns without
71843 ** doing any work. To avoid thwarting these optimizations, it is important
71844 ** not to clear the cursor here.
71845 */
71846 if( pCur->curFlags & BTCF_Multiple0x20 ){
71847 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
71848 if( rc ) return rc;
71849 }
71850
71851 if( pCur->pKeyInfo==0 ){
71852 assert( pX->pKey==0 )((void) (0));
71853 /* If this is an insert into a table b-tree, invalidate any incrblob
71854 ** cursors open on the row being replaced */
71855 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
71856
71857 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
71858 ** to a row with the same key as the new entry being inserted.
71859 */
71860#ifdef SQLITE_DEBUG
71861 if( flags & BTREE_SAVEPOSITION0x02 ){
71862 assert( pCur->curFlags & BTCF_ValidNKey )((void) (0));
71863 assert( pX->nKey==pCur->info.nKey )((void) (0));
71864 assert( pCur->info.nSize!=0 )((void) (0));
71865 assert( loc==0 )((void) (0));
71866 }
71867#endif
71868
71869 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
71870 ** that the cursor is not pointing to a row to be overwritten.
71871 ** So do a complete check.
71872 */
71873 if( (pCur->curFlags&BTCF_ValidNKey0x02)!=0 && pX->nKey==pCur->info.nKey ){
71874 /* The cursor is pointing to the entry that is to be
71875 ** overwritten */
71876 assert( pX->nData>=0 && pX->nZero>=0 )((void) (0));
71877 if( pCur->info.nSize!=0
71878 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
71879 ){
71880 /* New entry is the same size as the old. Do an overwrite */
71881 return btreeOverwriteCell(pCur, pX);
71882 }
71883 assert( loc==0 )((void) (0));
71884 }else if( loc==0 ){
71885 /* The cursor is *not* pointing to the cell to be overwritten, nor
71886 ** to an adjacent cell. Move the cursor so that it is pointing either
71887 ** to the cell to be overwritten or an adjacent cell.
71888 */
71889 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
71890 if( rc ) return rc;
71891 }
71892 }else{
71893 /* This is an index or a WITHOUT ROWID table */
71894
71895 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
71896 ** to a row with the same key as the new entry being inserted.
71897 */
71898 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 )((void) (0));
71899
71900 /* If the cursor is not already pointing either to the cell to be
71901 ** overwritten, or if a new cell is being inserted, if the cursor is
71902 ** not pointing to an immediately adjacent cell, then move the cursor
71903 ** so that it does.
71904 */
71905 if( loc==0 && (flags & BTREE_SAVEPOSITION0x02)==0 ){
71906 if( pX->nMem ){
71907 UnpackedRecord r;
71908 r.pKeyInfo = pCur->pKeyInfo;
71909 r.aMem = pX->aMem;
71910 r.nField = pX->nMem;
71911 r.default_rc = 0;
71912 r.errCode = 0;
71913 r.r1 = 0;
71914 r.r2 = 0;
71915 r.eqSeen = 0;
71916 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
71917 }else{
71918 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
71919 }
71920 if( rc ) return rc;
71921 }
71922
71923 /* If the cursor is currently pointing to an entry to be overwritten
71924 ** and the new content is the same as as the old, then use the
71925 ** overwrite optimization.
71926 */
71927 if( loc==0 ){
71928 getCellInfo(pCur);
71929 if( pCur->info.nKey==pX->nKey ){
71930 BtreePayload x2;
71931 x2.pData = pX->pKey;
71932 x2.nData = pX->nKey;
71933 x2.nZero = 0;
71934 return btreeOverwriteCell(pCur, &x2);
71935 }
71936 }
71937
71938 }
71939 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) )((void) (0));
71940
71941 pPage = pCur->pPage;
71942 assert( pPage->intKey || pX->nKey>=0 )((void) (0));
71943 assert( pPage->leaf || !pPage->intKey )((void) (0));
71944 if( pPage->nFree<0 ){
71945 rc = btreeComputeFreeSpace(pPage);
71946 if( rc ) return rc;
71947 }
71948
71949 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
71950 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
71951 loc==0 ? "overwrite" : "new entry"));
71952 assert( pPage->isInit )((void) (0));
71953 newCell = pBt->pTmpSpace;
71954 assert( newCell!=0 )((void) (0));
71955 rc = fillInCell(pPage, newCell, pX, &szNew);
71956 if( rc ) goto end_insert;
71957 assert( szNew==pPage->xCellSize(pPage, newCell) )((void) (0));
71958 assert( szNew <= MX_CELL_SIZE(pBt) )((void) (0));
71959 idx = pCur->ix;
71960 if( loc==0 ){
71961 CellInfo info;
71962 assert( idx<pPage->nCell )((void) (0));
71963 rc = sqlite3PagerWrite(pPage->pDbPage);
71964 if( rc ){
71965 goto end_insert;
71966 }
71967 oldCell = findCell(pPage, idx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(idx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(idx)])[1])))
;
71968 if( !pPage->leaf ){
71969 memcpy(newCell, oldCell, 4);
71970 }
71971 rc = clearCell(pPage, oldCell, &info);
71972 if( info.nSize==szNew && info.nLocal==info.nPayload
71973 && (!ISAUTOVACUUM(pBt->autoVacuum) || szNew<pPage->minLocal)
71974 ){
71975 /* Overwrite the old cell with the new if they are the same size.
71976 ** We could also try to do this if the old cell is smaller, then add
71977 ** the leftover space to the free list. But experiments show that
71978 ** doing that is no faster then skipping this optimization and just
71979 ** calling dropCell() and insertCell().
71980 **
71981 ** This optimization cannot be used on an autovacuum database if the
71982 ** new entry uses overflow pages, as the insertCell() call below is
71983 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
71984 assert( rc==SQLITE_OK )((void) (0)); /* clearCell never fails when nLocal==nPayload */
71985 if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(71985);
71986 memcpy(oldCell, newCell, szNew);
71987 return SQLITE_OK0;
71988 }
71989 dropCell(pPage, idx, info.nSize, &rc);
71990 if( rc ) goto end_insert;
71991 }else if( loc<0 && pPage->nCell>0 ){
71992 assert( pPage->leaf )((void) (0));
71993 idx = ++pCur->ix;
71994 pCur->curFlags &= ~BTCF_ValidNKey0x02;
71995 }else{
71996 assert( pPage->leaf )((void) (0));
71997 }
71998 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
71999 assert( pPage->nOverflow==0 || rc==SQLITE_OK )((void) (0));
72000 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 )((void) (0));
72001
72002 /* If no error has occurred and pPage has an overflow cell, call balance()
72003 ** to redistribute the cells within the tree. Since balance() may move
72004 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
72005 ** variables.
72006 **
72007 ** Previous versions of SQLite called moveToRoot() to move the cursor
72008 ** back to the root page as balance() used to invalidate the contents
72009 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
72010 ** set the cursor state to "invalid". This makes common insert operations
72011 ** slightly faster.
72012 **
72013 ** There is a subtle but important optimization here too. When inserting
72014 ** multiple records into an intkey b-tree using a single cursor (as can
72015 ** happen while processing an "INSERT INTO ... SELECT" statement), it
72016 ** is advantageous to leave the cursor pointing to the last entry in
72017 ** the b-tree if possible. If the cursor is left pointing to the last
72018 ** entry in the table, and the next row inserted has an integer key
72019 ** larger than the largest existing key, it is possible to insert the
72020 ** row without seeking the cursor. This can be a big performance boost.
72021 */
72022 pCur->info.nSize = 0;
72023 if( pPage->nOverflow ){
72024 assert( rc==SQLITE_OK )((void) (0));
72025 pCur->curFlags &= ~(BTCF_ValidNKey0x02);
72026 rc = balance(pCur);
72027
72028 /* Must make sure nOverflow is reset to zero even if the balance()
72029 ** fails. Internal data structure corruption will result otherwise.
72030 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
72031 ** from trying to save the current position of the cursor. */
72032 pCur->pPage->nOverflow = 0;
72033 pCur->eState = CURSOR_INVALID1;
72034 if( (flags & BTREE_SAVEPOSITION0x02) && rc==SQLITE_OK0 ){
72035 btreeReleaseAllCursorPages(pCur);
72036 if( pCur->pKeyInfo ){
72037 assert( pCur->pKey==0 )((void) (0));
72038 pCur->pKey = sqlite3Malloc( pX->nKey );
72039 if( pCur->pKey==0 ){
72040 rc = SQLITE_NOMEM7;
72041 }else{
72042 memcpy(pCur->pKey, pX->pKey, pX->nKey);
72043 }
72044 }
72045 pCur->eState = CURSOR_REQUIRESEEK3;
72046 pCur->nKey = pX->nKey;
72047 }
72048 }
72049 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 )((void) (0));
72050
72051end_insert:
72052 return rc;
72053}
72054
72055/*
72056** Delete the entry that the cursor is pointing to.
72057**
72058** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
72059** the cursor is left pointing at an arbitrary location after the delete.
72060** But if that bit is set, then the cursor is left in a state such that
72061** the next call to BtreeNext() or BtreePrev() moves it to the same row
72062** as it would have been on if the call to BtreeDelete() had been omitted.
72063**
72064** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
72065** associated with a single table entry and its indexes. Only one of those
72066** deletes is considered the "primary" delete. The primary delete occurs
72067** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
72068** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
72069** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
72070** but which might be used by alternative storage engines.
72071*/
72072SQLITE_PRIVATEstatic int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
72073 Btree *p = pCur->pBtree;
72074 BtShared *pBt = p->pBt;
72075 int rc; /* Return code */
72076 MemPage *pPage; /* Page to delete cell from */
72077 unsigned char *pCell; /* Pointer to cell to delete */
72078 int iCellIdx; /* Index of cell to delete */
72079 int iCellDepth; /* Depth of node containing pCell */
72080 CellInfo info; /* Size of the cell being deleted */
72081 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
72082 u8 bPreserve = flags & BTREE_SAVEPOSITION0x02; /* Keep cursor valid */
72083
72084 assert( cursorOwnsBtShared(pCur) )((void) (0));
72085 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
72086 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
72087 assert( pCur->curFlags & BTCF_WriteFlag )((void) (0));
72088 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) )((void) (0));
72089 assert( !hasReadConflicts(p, pCur->pgnoRoot) )((void) (0));
72090 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 )((void) (0));
72091 if( pCur->eState==CURSOR_REQUIRESEEK3 ){
72092 rc = btreeRestoreCursorPosition(pCur);
72093 if( rc ) return rc;
72094 }
72095 assert( pCur->eState==CURSOR_VALID )((void) (0));
72096
72097 iCellDepth = pCur->iPage;
72098 iCellIdx = pCur->ix;
72099 pPage = pCur->pPage;
72100 pCell = findCell(pPage, iCellIdx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iCellIdx)])[0]<<8 | (&(pPage)->
aCellIdx[2*(iCellIdx)])[1])))
;
72101 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT11;
72102
72103 /* If the bPreserve flag is set to true, then the cursor position must
72104 ** be preserved following this delete operation. If the current delete
72105 ** will cause a b-tree rebalance, then this is done by saving the cursor
72106 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
72107 ** returning.
72108 **
72109 ** Or, if the current delete will not cause a rebalance, then the cursor
72110 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
72111 ** before or after the deleted entry. In this case set bSkipnext to true. */
72112 if( bPreserve ){
72113 if( !pPage->leaf
72114 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
72115 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
72116 ){
72117 /* A b-tree rebalance will be required after deleting this entry.
72118 ** Save the cursor key. */
72119 rc = saveCursorKey(pCur);
72120 if( rc ) return rc;
72121 }else{
72122 bSkipnext = 1;
72123 }
72124 }
72125
72126 /* If the page containing the entry to delete is not a leaf page, move
72127 ** the cursor to the largest entry in the tree that is smaller than
72128 ** the entry being deleted. This cell will replace the cell being deleted
72129 ** from the internal node. The 'previous' entry is used for this instead
72130 ** of the 'next' entry, as the previous entry is always a part of the
72131 ** sub-tree headed by the child page of the cell being deleted. This makes
72132 ** balancing the tree following the delete operation easier. */
72133 if( !pPage->leaf ){
72134 rc = sqlite3BtreePrevious(pCur, 0);
72135 assert( rc!=SQLITE_DONE )((void) (0));
72136 if( rc ) return rc;
72137 }
72138
72139 /* Save the positions of any other cursors open on this table before
72140 ** making any modifications. */
72141 if( pCur->curFlags & BTCF_Multiple0x20 ){
72142 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
72143 if( rc ) return rc;
72144 }
72145
72146 /* If this is a delete operation to remove a row from a table b-tree,
72147 ** invalidate any incrblob cursors open on the row being deleted. */
72148 if( pCur->pKeyInfo==0 ){
72149 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
72150 }
72151
72152 /* Make the page containing the entry to be deleted writable. Then free any
72153 ** overflow pages associated with the entry and finally remove the cell
72154 ** itself from within the page. */
72155 rc = sqlite3PagerWrite(pPage->pDbPage);
72156 if( rc ) return rc;
72157 rc = clearCell(pPage, pCell, &info);
72158 dropCell(pPage, iCellIdx, info.nSize, &rc);
72159 if( rc ) return rc;
72160
72161 /* If the cell deleted was not located on a leaf page, then the cursor
72162 ** is currently pointing to the largest entry in the sub-tree headed
72163 ** by the child-page of the cell that was just deleted from an internal
72164 ** node. The cell from the leaf node needs to be moved to the internal
72165 ** node to replace the deleted cell. */
72166 if( !pPage->leaf ){
72167 MemPage *pLeaf = pCur->pPage;
72168 int nCell;
72169 Pgno n;
72170 unsigned char *pTmp;
72171
72172 if( pLeaf->nFree<0 ){
72173 rc = btreeComputeFreeSpace(pLeaf);
72174 if( rc ) return rc;
72175 }
72176 if( iCellDepth<pCur->iPage-1 ){
72177 n = pCur->apPage[iCellDepth+1]->pgno;
72178 }else{
72179 n = pCur->pPage->pgno;
72180 }
72181 pCell = findCell(pLeaf, pLeaf->nCell-1)((pLeaf)->aData + ((pLeaf)->maskPage & ((&(pLeaf
)->aCellIdx[2*(pLeaf->nCell-1)])[0]<<8 | (&(pLeaf
)->aCellIdx[2*(pLeaf->nCell-1)])[1])))
;
72182 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(72182);
72183 nCell = pLeaf->xCellSize(pLeaf, pCell);
72184 assert( MX_CELL_SIZE(pBt) >= nCell )((void) (0));
72185 pTmp = pBt->pTmpSpace;
72186 assert( pTmp!=0 )((void) (0));
72187 rc = sqlite3PagerWrite(pLeaf->pDbPage);
72188 if( rc==SQLITE_OK0 ){
72189 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
72190 }
72191 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
72192 if( rc ) return rc;
72193 }
72194
72195 /* Balance the tree. If the entry deleted was located on a leaf page,
72196 ** then the cursor still points to that page. In this case the first
72197 ** call to balance() repairs the tree, and the if(...) condition is
72198 ** never true.
72199 **
72200 ** Otherwise, if the entry deleted was on an internal node page, then
72201 ** pCur is pointing to the leaf page from which a cell was removed to
72202 ** replace the cell deleted from the internal node. This is slightly
72203 ** tricky as the leaf node may be underfull, and the internal node may
72204 ** be either under or overfull. In this case run the balancing algorithm
72205 ** on the leaf node first. If the balance proceeds far enough up the
72206 ** tree that we can be sure that any problem in the internal node has
72207 ** been corrected, so be it. Otherwise, after balancing the leaf node,
72208 ** walk the cursor up the tree to the internal node and balance it as
72209 ** well. */
72210 rc = balance(pCur);
72211 if( rc==SQLITE_OK0 && pCur->iPage>iCellDepth ){
72212 releasePageNotNull(pCur->pPage);
72213 pCur->iPage--;
72214 while( pCur->iPage>iCellDepth ){
72215 releasePage(pCur->apPage[pCur->iPage--]);
72216 }
72217 pCur->pPage = pCur->apPage[pCur->iPage];
72218 rc = balance(pCur);
72219 }
72220
72221 if( rc==SQLITE_OK0 ){
72222 if( bSkipnext ){
72223 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) )((void) (0));
72224 assert( pPage==pCur->pPage || CORRUPT_DB )((void) (0));
72225 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell )((void) (0));
72226 pCur->eState = CURSOR_SKIPNEXT2;
72227 if( iCellIdx>=pPage->nCell ){
72228 pCur->skipNext = -1;
72229 pCur->ix = pPage->nCell-1;
72230 }else{
72231 pCur->skipNext = 1;
72232 }
72233 }else{
72234 rc = moveToRoot(pCur);
72235 if( bPreserve ){
72236 btreeReleaseAllCursorPages(pCur);
72237 pCur->eState = CURSOR_REQUIRESEEK3;
72238 }
72239 if( rc==SQLITE_EMPTY16 ) rc = SQLITE_OK0;
72240 }
72241 }
72242 return rc;
72243}
72244
72245/*
72246** Create a new BTree table. Write into *piTable the page
72247** number for the root page of the new table.
72248**
72249** The type of type is determined by the flags parameter. Only the
72250** following values of flags are currently in use. Other values for
72251** flags might not work:
72252**
72253** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
72254** BTREE_ZERODATA Used for SQL indices
72255*/
72256static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
72257 BtShared *pBt = p->pBt;
72258 MemPage *pRoot;
72259 Pgno pgnoRoot;
72260 int rc;
72261 int ptfFlags; /* Page-type flage for the root page of new table */
72262
72263 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
72264 assert( pBt->inTransaction==TRANS_WRITE )((void) (0));
72265 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 )((void) (0));
72266
72267#ifdef SQLITE_OMIT_AUTOVACUUM
72268 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
72269 if( rc ){
72270 return rc;
72271 }
72272#else
72273 if( pBt->autoVacuum ){
72274 Pgno pgnoMove; /* Move a page here to make room for the root-page */
72275 MemPage *pPageMove; /* The page to move to. */
72276
72277 /* Creating a new table may probably require moving an existing database
72278 ** to make room for the new tables root page. In case this page turns
72279 ** out to be an overflow page, delete all overflow page-map caches
72280 ** held by open cursors.
72281 */
72282 invalidateAllOverflowCache(pBt);
72283
72284 /* Read the value of meta[3] from the database to determine where the
72285 ** root page of the new table should go. meta[3] is the largest root-page
72286 ** created so far, so the new root-page is (meta[3]+1).
72287 */
72288 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE4, &pgnoRoot);
72289 pgnoRoot++;
72290
72291 /* The new root-page may not be allocated on a pointer-map page, or the
72292 ** PENDING_BYTE page.
72293 */
72294 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot)ptrmapPageno(pBt, pgnoRoot) ||
72295 pgnoRoot==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
72296 pgnoRoot++;
72297 }
72298 assert( pgnoRoot>=3 || CORRUPT_DB )((void) (0));
72299 testcase( pgnoRoot<3 );
72300
72301 /* Allocate a page. The page that currently resides at pgnoRoot will
72302 ** be moved to the allocated page (unless the allocated page happens
72303 ** to reside at pgnoRoot).
72304 */
72305 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT1);
72306 if( rc!=SQLITE_OK0 ){
72307 return rc;
72308 }
72309
72310 if( pgnoMove!=pgnoRoot ){
72311 /* pgnoRoot is the page that will be used for the root-page of
72312 ** the new table (assuming an error did not occur). But we were
72313 ** allocated pgnoMove. If required (i.e. if it was not allocated
72314 ** by extending the file), the current page at position pgnoMove
72315 ** is already journaled.
72316 */
72317 u8 eType = 0;
72318 Pgno iPtrPage = 0;
72319
72320 /* Save the positions of any open cursors. This is required in
72321 ** case they are holding a reference to an xFetch reference
72322 ** corresponding to page pgnoRoot. */
72323 rc = saveAllCursors(pBt, 0, 0);
72324 releasePage(pPageMove);
72325 if( rc!=SQLITE_OK0 ){
72326 return rc;
72327 }
72328
72329 /* Move the page currently at pgnoRoot to pgnoMove. */
72330 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
72331 if( rc!=SQLITE_OK0 ){
72332 return rc;
72333 }
72334 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
72335 if( eType==PTRMAP_ROOTPAGE1 || eType==PTRMAP_FREEPAGE2 ){
72336 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(72336);
72337 }
72338 if( rc!=SQLITE_OK0 ){
72339 releasePage(pRoot);
72340 return rc;
72341 }
72342 assert( eType!=PTRMAP_ROOTPAGE )((void) (0));
72343 assert( eType!=PTRMAP_FREEPAGE )((void) (0));
72344 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
72345 releasePage(pRoot);
72346
72347 /* Obtain the page at pgnoRoot */
72348 if( rc!=SQLITE_OK0 ){
72349 return rc;
72350 }
72351 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
72352 if( rc!=SQLITE_OK0 ){
72353 return rc;
72354 }
72355 rc = sqlite3PagerWrite(pRoot->pDbPage);
72356 if( rc!=SQLITE_OK0 ){
72357 releasePage(pRoot);
72358 return rc;
72359 }
72360 }else{
72361 pRoot = pPageMove;
72362 }
72363
72364 /* Update the pointer-map and meta-data with the new root-page number. */
72365 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE1, 0, &rc);
72366 if( rc ){
72367 releasePage(pRoot);
72368 return rc;
72369 }
72370
72371 /* When the new root page was allocated, page 1 was made writable in
72372 ** order either to increase the database filesize, or to decrement the
72373 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
72374 */
72375 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) )((void) (0));
72376 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
72377 if( NEVER(rc)(rc) ){
72378 releasePage(pRoot);
72379 return rc;
72380 }
72381
72382 }else{
72383 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
72384 if( rc ) return rc;
72385 }
72386#endif
72387 assert( sqlite3PagerIswriteable(pRoot->pDbPage) )((void) (0));
72388 if( createTabFlags & BTREE_INTKEY1 ){
72389 ptfFlags = PTF_INTKEY0x01 | PTF_LEAFDATA0x04 | PTF_LEAF0x08;
72390 }else{
72391 ptfFlags = PTF_ZERODATA0x02 | PTF_LEAF0x08;
72392 }
72393 zeroPage(pRoot, ptfFlags);
72394 sqlite3PagerUnref(pRoot->pDbPage);
72395 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 )((void) (0));
72396 *piTable = (int)pgnoRoot;
72397 return SQLITE_OK0;
72398}
72399SQLITE_PRIVATEstatic int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
72400 int rc;
72401 sqlite3BtreeEnter(p);
72402 rc = btreeCreateTable(p, piTable, flags);
72403 sqlite3BtreeLeave(p);
72404 return rc;
72405}
72406
72407/*
72408** Erase the given database page and all its children. Return
72409** the page to the freelist.
72410*/
72411static int clearDatabasePage(
72412 BtShared *pBt, /* The BTree that contains the table */
72413 Pgno pgno, /* Page number to clear */
72414 int freePageFlag, /* Deallocate page if true */
72415 int *pnChange /* Add number of Cells freed to this counter */
72416){
72417 MemPage *pPage;
72418 int rc;
72419 unsigned char *pCell;
72420 int i;
72421 int hdr;
72422 CellInfo info;
72423
72424 assert( sqlite3_mutex_held(pBt->mutex) )((void) (0));
72425 if( pgno>btreePagecount(pBt) ){
72426 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(72426);
72427 }
72428 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
72429 if( rc ) return rc;
72430 if( pPage->bBusy ){
72431 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(72431);
72432 goto cleardatabasepage_out;
72433 }
72434 pPage->bBusy = 1;
72435 hdr = pPage->hdrOffset;
72436 for(i=0; i<pPage->nCell; i++){
72437 pCell = findCell(pPage, i)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(i)])[0]<<8 | (&(pPage)->aCellIdx
[2*(i)])[1])))
;
72438 if( !pPage->leaf ){
72439 rc = clearDatabasePage(pBt, get4bytesqlite3Get4byte(pCell), 1, pnChange);
72440 if( rc ) goto cleardatabasepage_out;
72441 }
72442 rc = clearCell(pPage, pCell, &info);
72443 if( rc ) goto cleardatabasepage_out;
72444 }
72445 if( !pPage->leaf ){
72446 rc = clearDatabasePage(pBt, get4bytesqlite3Get4byte(&pPage->aData[hdr+8]), 1, pnChange);
72447 if( rc ) goto cleardatabasepage_out;
72448 }else if( pnChange ){
72449 assert( pPage->intKey || CORRUPT_DB )((void) (0));
72450 testcase( !pPage->intKey );
72451 *pnChange += pPage->nCell;
72452 }
72453 if( freePageFlag ){
72454 freePage(pPage, &rc);
72455 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
72456 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF0x08);
72457 }
72458
72459cleardatabasepage_out:
72460 pPage->bBusy = 0;
72461 releasePage(pPage);
72462 return rc;
72463}
72464
72465/*
72466** Delete all information from a single table in the database. iTable is
72467** the page number of the root of the table. After this routine returns,
72468** the root page is empty, but still exists.
72469**
72470** This routine will fail with SQLITE_LOCKED if there are any open
72471** read cursors on the table. Open write cursors are moved to the
72472** root of the table.
72473**
72474** If pnChange is not NULL, then table iTable must be an intkey table. The
72475** integer value pointed to by pnChange is incremented by the number of
72476** entries in the table.
72477*/
72478SQLITE_PRIVATEstatic int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
72479 int rc;
72480 BtShared *pBt = p->pBt;
72481 sqlite3BtreeEnter(p);
72482 assert( p->inTrans==TRANS_WRITE )((void) (0));
72483
72484 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
72485
72486 if( SQLITE_OK0==rc ){
72487 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
72488 ** is the root of a table b-tree - if it is not, the following call is
72489 ** a no-op). */
72490 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
72491 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
72492 }
72493 sqlite3BtreeLeave(p);
72494 return rc;
72495}
72496
72497/*
72498** Delete all information from the single table that pCur is open on.
72499**
72500** This routine only work for pCur on an ephemeral table.
72501*/
72502SQLITE_PRIVATEstatic int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
72503 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
72504}
72505
72506/*
72507** Erase all information in a table and add the root of the table to
72508** the freelist. Except, the root of the principle table (the one on
72509** page 1) is never added to the freelist.
72510**
72511** This routine will fail with SQLITE_LOCKED if there are any open
72512** cursors on the table.
72513**
72514** If AUTOVACUUM is enabled and the page at iTable is not the last
72515** root page in the database file, then the last root page
72516** in the database file is moved into the slot formerly occupied by
72517** iTable and that last slot formerly occupied by the last root page
72518** is added to the freelist instead of iTable. In this say, all
72519** root pages are kept at the beginning of the database file, which
72520** is necessary for AUTOVACUUM to work right. *piMoved is set to the
72521** page number that used to be the last root page in the file before
72522** the move. If no page gets moved, *piMoved is set to 0.
72523** The last root page is recorded in meta[3] and the value of
72524** meta[3] is updated by this procedure.
72525*/
72526static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
72527 int rc;
72528 MemPage *pPage = 0;
72529 BtShared *pBt = p->pBt;
72530
72531 assert( sqlite3BtreeHoldsMutex(p) )((void) (0));
72532 assert( p->inTrans==TRANS_WRITE )((void) (0));
72533 assert( iTable>=2 )((void) (0));
72534 if( iTable>btreePagecount(pBt) ){
72535 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(72535);
72536 }
72537
72538 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
72539 if( rc ) return rc;
72540 rc = sqlite3BtreeClearTable(p, iTable, 0);
72541 if( rc ){
72542 releasePage(pPage);
72543 return rc;
72544 }
72545
72546 *piMoved = 0;
72547
72548#ifdef SQLITE_OMIT_AUTOVACUUM
72549 freePage(pPage, &rc);
72550 releasePage(pPage);
72551#else
72552 if( pBt->autoVacuum ){
72553 Pgno maxRootPgno;
72554 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE4, &maxRootPgno);
72555
72556 if( iTable==maxRootPgno ){
72557 /* If the table being dropped is the table with the largest root-page
72558 ** number in the database, put the root page on the free list.
72559 */
72560 freePage(pPage, &rc);
72561 releasePage(pPage);
72562 if( rc!=SQLITE_OK0 ){
72563 return rc;
72564 }
72565 }else{
72566 /* The table being dropped does not have the largest root-page
72567 ** number in the database. So move the page that does into the
72568 ** gap left by the deleted root-page.
72569 */
72570 MemPage *pMove;
72571 releasePage(pPage);
72572 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
72573 if( rc!=SQLITE_OK0 ){
72574 return rc;
72575 }
72576 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE1, 0, iTable, 0);
72577 releasePage(pMove);
72578 if( rc!=SQLITE_OK0 ){
72579 return rc;
72580 }
72581 pMove = 0;
72582 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
72583 freePage(pMove, &rc);
72584 releasePage(pMove);
72585 if( rc!=SQLITE_OK0 ){
72586 return rc;
72587 }
72588 *piMoved = maxRootPgno;
72589 }
72590
72591 /* Set the new 'max-root-page' value in the database header. This
72592 ** is the old value less one, less one more if that happens to
72593 ** be a root-page number, less one again if that is the
72594 ** PENDING_BYTE_PAGE.
72595 */
72596 maxRootPgno--;
72597 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
72598 || PTRMAP_ISPAGE(pBt, maxRootPgno)(ptrmapPageno((pBt), (maxRootPgno))==(maxRootPgno)) ){
72599 maxRootPgno--;
72600 }
72601 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) )((void) (0));
72602
72603 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
72604 }else{
72605 freePage(pPage, &rc);
72606 releasePage(pPage);
72607 }
72608#endif
72609 return rc;
72610}
72611SQLITE_PRIVATEstatic int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
72612 int rc;
72613 sqlite3BtreeEnter(p);
72614 rc = btreeDropTable(p, iTable, piMoved);
72615 sqlite3BtreeLeave(p);
72616 return rc;
72617}
72618
72619
72620/*
72621** This function may only be called if the b-tree connection already
72622** has a read or write transaction open on the database.
72623**
72624** Read the meta-information out of a database file. Meta[0]
72625** is the number of free pages currently in the database. Meta[1]
72626** through meta[15] are available for use by higher layers. Meta[0]
72627** is read-only, the others are read/write.
72628**
72629** The schema layer numbers meta values differently. At the schema
72630** layer (and the SetCookie and ReadCookie opcodes) the number of
72631** free pages is not visible. So Cookie[0] is the same as Meta[1].
72632**
72633** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
72634** of reading the value out of the header, it instead loads the "DataVersion"
72635** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
72636** database file. It is a number computed by the pager. But its access
72637** pattern is the same as header meta values, and so it is convenient to
72638** read it from this routine.
72639*/
72640SQLITE_PRIVATEstatic void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
72641 BtShared *pBt = p->pBt;
72642
72643 sqlite3BtreeEnter(p);
72644 assert( p->inTrans>TRANS_NONE )((void) (0));
72645 assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) )((void) (0));
72646 assert( pBt->pPage1 )((void) (0));
72647 assert( idx>=0 && idx<=15 )((void) (0));
72648
72649 if( idx==BTREE_DATA_VERSION15 ){
72650 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
72651 }else{
72652 *pMeta = get4bytesqlite3Get4byte(&pBt->pPage1->aData[36 + idx*4]);
72653 }
72654
72655 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
72656 ** database, mark the database as read-only. */
72657#ifdef SQLITE_OMIT_AUTOVACUUM
72658 if( idx==BTREE_LARGEST_ROOT_PAGE4 && *pMeta>0 ){
72659 pBt->btsFlags |= BTS_READ_ONLY0x0001;
72660 }
72661#endif
72662
72663 sqlite3BtreeLeave(p);
72664}
72665
72666/*
72667** Write meta-information back into the database. Meta[0] is
72668** read-only and may not be written.
72669*/
72670SQLITE_PRIVATEstatic int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
72671 BtShared *pBt = p->pBt;
72672 unsigned char *pP1;
72673 int rc;
72674 assert( idx>=1 && idx<=15 )((void) (0));
72675 sqlite3BtreeEnter(p);
72676 assert( p->inTrans==TRANS_WRITE )((void) (0));
72677 assert( pBt->pPage1!=0 )((void) (0));
72678 pP1 = pBt->pPage1->aData;
72679 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
72680 if( rc==SQLITE_OK0 ){
72681 put4bytesqlite3Put4byte(&pP1[36 + idx*4], iMeta);
72682#ifndef SQLITE_OMIT_AUTOVACUUM
72683 if( idx==BTREE_INCR_VACUUM7 ){
72684 assert( pBt->autoVacuum || iMeta==0 )((void) (0));
72685 assert( iMeta==0 || iMeta==1 )((void) (0));
72686 pBt->incrVacuum = (u8)iMeta;
72687 }
72688#endif
72689 }
72690 sqlite3BtreeLeave(p);
72691 return rc;
72692}
72693
72694#ifndef SQLITE_OMIT_BTREECOUNT
72695/*
72696** The first argument, pCur, is a cursor opened on some b-tree. Count the
72697** number of entries in the b-tree and write the result to *pnEntry.
72698**
72699** SQLITE_OK is returned if the operation is successfully executed.
72700** Otherwise, if an error is encountered (i.e. an IO error or database
72701** corruption) an SQLite error code is returned.
72702*/
72703SQLITE_PRIVATEstatic int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
72704 i64 nEntry = 0; /* Value to return in *pnEntry */
72705 int rc; /* Return code */
72706
72707 rc = moveToRoot(pCur);
72708 if( rc==SQLITE_EMPTY16 ){
72709 *pnEntry = 0;
72710 return SQLITE_OK0;
72711 }
72712
72713 /* Unless an error occurs, the following loop runs one iteration for each
72714 ** page in the B-Tree structure (not including overflow pages).
72715 */
72716 while( rc==SQLITE_OK0 ){
72717 int iIdx; /* Index of child node in parent */
72718 MemPage *pPage; /* Current page of the b-tree */
72719
72720 /* If this is a leaf page or the tree is not an int-key tree, then
72721 ** this page contains countable entries. Increment the entry counter
72722 ** accordingly.
72723 */
72724 pPage = pCur->pPage;
72725 if( pPage->leaf || !pPage->intKey ){
72726 nEntry += pPage->nCell;
72727 }
72728
72729 /* pPage is a leaf node. This loop navigates the cursor so that it
72730 ** points to the first interior cell that it points to the parent of
72731 ** the next page in the tree that has not yet been visited. The
72732 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
72733 ** of the page, or to the number of cells in the page if the next page
72734 ** to visit is the right-child of its parent.
72735 **
72736 ** If all pages in the tree have been visited, return SQLITE_OK to the
72737 ** caller.
72738 */
72739 if( pPage->leaf ){
72740 do {
72741 if( pCur->iPage==0 ){
72742 /* All pages of the b-tree have been visited. Return successfully. */
72743 *pnEntry = nEntry;
72744 return moveToRoot(pCur);
72745 }
72746 moveToParent(pCur);
72747 }while ( pCur->ix>=pCur->pPage->nCell );
72748
72749 pCur->ix++;
72750 pPage = pCur->pPage;
72751 }
72752
72753 /* Descend to the child node of the cell that the cursor currently
72754 ** points at. This is the right-child if (iIdx==pPage->nCell).
72755 */
72756 iIdx = pCur->ix;
72757 if( iIdx==pPage->nCell ){
72758 rc = moveToChild(pCur, get4bytesqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
72759 }else{
72760 rc = moveToChild(pCur, get4bytesqlite3Get4byte(findCell(pPage, iIdx)((pPage)->aData + ((pPage)->maskPage & ((&(pPage
)->aCellIdx[2*(iIdx)])[0]<<8 | (&(pPage)->aCellIdx
[2*(iIdx)])[1])))
));
72761 }
72762 }
72763
72764 /* An error has occurred. Return an error code. */
72765 return rc;
72766}
72767#endif
72768
72769/*
72770** Return the pager associated with a BTree. This routine is used for
72771** testing and debugging only.
72772*/
72773SQLITE_PRIVATEstatic Pager *sqlite3BtreePager(Btree *p){
72774 return p->pBt->pPager;
72775}
72776
72777#ifndef SQLITE_OMIT_INTEGRITY_CHECK
72778/*
72779** Append a message to the error message string.
72780*/
72781static void checkAppendMsg(
72782 IntegrityCk *pCheck,
72783 const char *zFormat,
72784 ...
72785){
72786 va_list ap;
72787 if( !pCheck->mxErr ) return;
72788 pCheck->mxErr--;
72789 pCheck->nErr++;
72790 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
72791 if( pCheck->errMsg.nChar ){
72792 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
72793 }
72794 if( pCheck->zPfx ){
72795 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
72796 }
72797 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
72798 va_end(ap)__builtin_va_end(ap);
72799 if( pCheck->errMsg.accError==SQLITE_NOMEM7 ){
72800 pCheck->mallocFailed = 1;
72801 }
72802}
72803#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72804
72805#ifndef SQLITE_OMIT_INTEGRITY_CHECK
72806
72807/*
72808** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
72809** corresponds to page iPg is already set.
72810*/
72811static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
72812 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 )((void) (0));
72813 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
72814}
72815
72816/*
72817** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
72818*/
72819static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
72820 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 )((void) (0));
72821 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
72822}
72823
72824
72825/*
72826** Add 1 to the reference count for page iPage. If this is the second
72827** reference to the page, add an error message to pCheck->zErrMsg.
72828** Return 1 if there are 2 or more references to the page and 0 if
72829** if this is the first reference to the page.
72830**
72831** Also check that the page number is in bounds.
72832*/
72833static int checkRef(IntegrityCk *pCheck, Pgno iPage){
72834 if( iPage>pCheck->nPage || iPage==0 ){
72835 checkAppendMsg(pCheck, "invalid page number %d", iPage);
72836 return 1;
72837 }
72838 if( getPageReferenced(pCheck, iPage) ){
72839 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
72840 return 1;
72841 }
72842 setPageReferenced(pCheck, iPage);
72843 return 0;
72844}
72845
72846#ifndef SQLITE_OMIT_AUTOVACUUM
72847/*
72848** Check that the entry in the pointer-map for page iChild maps to
72849** page iParent, pointer type ptrType. If not, append an error message
72850** to pCheck.
72851*/
72852static void checkPtrmap(
72853 IntegrityCk *pCheck, /* Integrity check context */
72854 Pgno iChild, /* Child page number */
72855 u8 eType, /* Expected pointer map type */
72856 Pgno iParent /* Expected pointer map parent page number */
72857){
72858 int rc;
72859 u8 ePtrmapType;
72860 Pgno iPtrmapParent;
72861
72862 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
72863 if( rc!=SQLITE_OK0 ){
72864 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) pCheck->mallocFailed = 1;
72865 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
72866 return;
72867 }
72868
72869 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
72870 checkAppendMsg(pCheck,
72871 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
72872 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
72873 }
72874}
72875#endif
72876
72877/*
72878** Check the integrity of the freelist or of an overflow page list.
72879** Verify that the number of pages on the list is N.
72880*/
72881static void checkList(
72882 IntegrityCk *pCheck, /* Integrity checking context */
72883 int isFreeList, /* True for a freelist. False for overflow page list */
72884 int iPage, /* Page number for first page in the list */
72885 u32 N /* Expected number of pages in the list */
72886){
72887 int i;
72888 u32 expected = N;
72889 int nErrAtStart = pCheck->nErr;
72890 while( iPage!=0 && pCheck->mxErr ){
72891 DbPage *pOvflPage;
72892 unsigned char *pOvflData;
72893 if( checkRef(pCheck, iPage) ) break;
72894 N--;
72895 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
72896 checkAppendMsg(pCheck, "failed to get page %d", iPage);
72897 break;
72898 }
72899 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
72900 if( isFreeList ){
72901 u32 n = (u32)get4bytesqlite3Get4byte(&pOvflData[4]);
72902#ifndef SQLITE_OMIT_AUTOVACUUM
72903 if( pCheck->pBt->autoVacuum ){
72904 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE2, 0);
72905 }
72906#endif
72907 if( n>pCheck->pBt->usableSize/4-2 ){
72908 checkAppendMsg(pCheck,
72909 "freelist leaf count too big on page %d", iPage);
72910 N--;
72911 }else{
72912 for(i=0; i<(int)n; i++){
72913 Pgno iFreePage = get4bytesqlite3Get4byte(&pOvflData[8+i*4]);
72914#ifndef SQLITE_OMIT_AUTOVACUUM
72915 if( pCheck->pBt->autoVacuum ){
72916 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE2, 0);
72917 }
72918#endif
72919 checkRef(pCheck, iFreePage);
72920 }
72921 N -= n;
72922 }
72923 }
72924#ifndef SQLITE_OMIT_AUTOVACUUM
72925 else{
72926 /* If this database supports auto-vacuum and iPage is not the last
72927 ** page in this overflow list, check that the pointer-map entry for
72928 ** the following page matches iPage.
72929 */
72930 if( pCheck->pBt->autoVacuum && N>0 ){
72931 i = get4bytesqlite3Get4byte(pOvflData);
72932 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW24, iPage);
72933 }
72934 }
72935#endif
72936 iPage = get4bytesqlite3Get4byte(pOvflData);
72937 sqlite3PagerUnref(pOvflPage);
72938 }
72939 if( N && nErrAtStart==pCheck->nErr ){
72940 checkAppendMsg(pCheck,
72941 "%s is %d but should be %d",
72942 isFreeList ? "size" : "overflow list length",
72943 expected-N, expected);
72944 }
72945}
72946#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72947
72948/*
72949** An implementation of a min-heap.
72950**
72951** aHeap[0] is the number of elements on the heap. aHeap[1] is the
72952** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
72953** and aHeap[N*2+1].
72954**
72955** The heap property is this: Every node is less than or equal to both
72956** of its daughter nodes. A consequence of the heap property is that the
72957** root node aHeap[1] is always the minimum value currently in the heap.
72958**
72959** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
72960** the heap, preserving the heap property. The btreeHeapPull() routine
72961** removes the root element from the heap (the minimum value in the heap)
72962** and then moves other nodes around as necessary to preserve the heap
72963** property.
72964**
72965** This heap is used for cell overlap and coverage testing. Each u32
72966** entry represents the span of a cell or freeblock on a btree page.
72967** The upper 16 bits are the index of the first byte of a range and the
72968** lower 16 bits are the index of the last byte of that range.
72969*/
72970static void btreeHeapInsert(u32 *aHeap, u32 x){
72971 u32 j, i = ++aHeap[0];
72972 aHeap[i] = x;
72973 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
72974 x = aHeap[j];
72975 aHeap[j] = aHeap[i];
72976 aHeap[i] = x;
72977 i = j;
72978 }
72979}
72980static int btreeHeapPull(u32 *aHeap, u32 *pOut){
72981 u32 j, i, x;
72982 if( (x = aHeap[0])==0 ) return 0;
72983 *pOut = aHeap[1];
72984 aHeap[1] = aHeap[x];
72985 aHeap[x] = 0xffffffff;
72986 aHeap[0]--;
72987 i = 1;
72988 while( (j = i*2)<=aHeap[0] ){
72989 if( aHeap[j]>aHeap[j+1] ) j++;
72990 if( aHeap[i]<aHeap[j] ) break;
72991 x = aHeap[i];
72992 aHeap[i] = aHeap[j];
72993 aHeap[j] = x;
72994 i = j;
72995 }
72996 return 1;
72997}
72998
72999#ifndef SQLITE_OMIT_INTEGRITY_CHECK
73000/*
73001** Do various sanity checks on a single page of a tree. Return
73002** the tree depth. Root pages return 0. Parents of root pages
73003** return 1, and so forth.
73004**
73005** These checks are done:
73006**
73007** 1. Make sure that cells and freeblocks do not overlap
73008** but combine to completely cover the page.
73009** 2. Make sure integer cell keys are in order.
73010** 3. Check the integrity of overflow pages.
73011** 4. Recursively call checkTreePage on all children.
73012** 5. Verify that the depth of all children is the same.
73013*/
73014static int checkTreePage(
73015 IntegrityCk *pCheck, /* Context for the sanity check */
73016 int iPage, /* Page number of the page to check */
73017 i64 *piMinKey, /* Write minimum integer primary key here */
73018 i64 maxKey /* Error if integer primary key greater than this */
73019){
73020 MemPage *pPage = 0; /* The page being analyzed */
73021 int i; /* Loop counter */
73022 int rc; /* Result code from subroutine call */
73023 int depth = -1, d2; /* Depth of a subtree */
73024 int pgno; /* Page number */
73025 int nFrag; /* Number of fragmented bytes on the page */
73026 int hdr; /* Offset to the page header */
73027 int cellStart; /* Offset to the start of the cell pointer array */
73028 int nCell; /* Number of cells */
73029 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
73030 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
73031 ** False if IPK must be strictly less than maxKey */
73032 u8 *data; /* Page content */
73033 u8 *pCell; /* Cell content */
73034 u8 *pCellIdx; /* Next element of the cell pointer array */
73035 BtShared *pBt; /* The BtShared object that owns pPage */
73036 u32 pc; /* Address of a cell */
73037 u32 usableSize; /* Usable size of the page */
73038 u32 contentOffset; /* Offset to the start of the cell content area */
73039 u32 *heap = 0; /* Min-heap used for checking cell coverage */
73040 u32 x, prev = 0; /* Next and previous entry on the min-heap */
73041 const char *saved_zPfx = pCheck->zPfx;
73042 int saved_v1 = pCheck->v1;
73043 int saved_v2 = pCheck->v2;
73044 u8 savedIsInit = 0;
73045
73046 /* Check that the page exists
73047 */
73048 pBt = pCheck->pBt;
73049 usableSize = pBt->usableSize;
73050 if( iPage==0 ) return 0;
73051 if( checkRef(pCheck, iPage) ) return 0;
73052 pCheck->zPfx = "Page %d: ";
73053 pCheck->v1 = iPage;
73054 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
73055 checkAppendMsg(pCheck,
73056 "unable to get the page. error code=%d", rc);
73057 goto end_of_check;
73058 }
73059
73060 /* Clear MemPage.isInit to make sure the corruption detection code in
73061 ** btreeInitPage() is executed. */
73062 savedIsInit = pPage->isInit;
73063 pPage->isInit = 0;
73064 if( (rc = btreeInitPage(pPage))!=0 ){
73065 assert( rc==SQLITE_CORRUPT )((void) (0)); /* The only possible error from InitPage */
73066 checkAppendMsg(pCheck,
73067 "btreeInitPage() returns error code %d", rc);
73068 goto end_of_check;
73069 }
73070 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
73071 assert( rc==SQLITE_CORRUPT )((void) (0));
73072 checkAppendMsg(pCheck, "free space corruption", rc);
73073 goto end_of_check;
73074 }
73075 data = pPage->aData;
73076 hdr = pPage->hdrOffset;
73077
73078 /* Set up for cell analysis */
73079 pCheck->zPfx = "On tree page %d cell %d: ";
73080 contentOffset = get2byteNotZero(&data[hdr+5])(((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])
[1]))-1)&0xffff)+1)
;
73081 assert( contentOffset<=usableSize )((void) (0)); /* Enforced by btreeInitPage() */
73082
73083 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
73084 ** number of cells on the page. */
73085 nCell = get2byte(&data[hdr+3])((&data[hdr+3])[0]<<8 | (&data[hdr+3])[1]);
73086 assert( pPage->nCell==nCell )((void) (0));
73087
73088 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
73089 ** immediately follows the b-tree page header. */
73090 cellStart = hdr + 12 - 4*pPage->leaf;
73091 assert( pPage->aCellIdx==&data[cellStart] )((void) (0));
73092 pCellIdx = &data[cellStart + 2*(nCell-1)];
73093
73094 if( !pPage->leaf ){
73095 /* Analyze the right-child page of internal pages */
73096 pgno = get4bytesqlite3Get4byte(&data[hdr+8]);
73097#ifndef SQLITE_OMIT_AUTOVACUUM
73098 if( pBt->autoVacuum ){
73099 pCheck->zPfx = "On page %d at right child: ";
73100 checkPtrmap(pCheck, pgno, PTRMAP_BTREE5, iPage);
73101 }
73102#endif
73103 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
73104 keyCanBeEqual = 0;
73105 }else{
73106 /* For leaf pages, the coverage check will occur in the same loop
73107 ** as the other cell checks, so initialize the heap. */
73108 heap = pCheck->heap;
73109 heap[0] = 0;
73110 }
73111
73112 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
73113 ** integer offsets to the cell contents. */
73114 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
73115 CellInfo info;
73116
73117 /* Check cell size */
73118 pCheck->v2 = i;
73119 assert( pCellIdx==&data[cellStart + i*2] )((void) (0));
73120 pc = get2byteAligned(pCellIdx)((pCellIdx)[0]<<8 | (pCellIdx)[1]);
73121 pCellIdx -= 2;
73122 if( pc<contentOffset || pc>usableSize-4 ){
73123 checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
73124 pc, contentOffset, usableSize-4);
73125 doCoverageCheck = 0;
73126 continue;
73127 }
73128 pCell = &data[pc];
73129 pPage->xParseCell(pPage, pCell, &info);
73130 if( pc+info.nSize>usableSize ){
73131 checkAppendMsg(pCheck, "Extends off end of page");
73132 doCoverageCheck = 0;
73133 continue;
73134 }
73135
73136 /* Check for integer primary key out of range */
73137 if( pPage->intKey ){
73138 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
73139 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
73140 }
73141 maxKey = info.nKey;
73142 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
73143 }
73144
73145 /* Check the content overflow list */
73146 if( info.nPayload>info.nLocal ){
73147 u32 nPage; /* Number of pages on the overflow chain */
73148 Pgno pgnoOvfl; /* First page of the overflow chain */
73149 assert( pc + info.nSize - 4 <= usableSize )((void) (0));
73150 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
73151 pgnoOvfl = get4bytesqlite3Get4byte(&pCell[info.nSize - 4]);
73152#ifndef SQLITE_OMIT_AUTOVACUUM
73153 if( pBt->autoVacuum ){
73154 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW13, iPage);
73155 }
73156#endif
73157 checkList(pCheck, 0, pgnoOvfl, nPage);
73158 }
73159
73160 if( !pPage->leaf ){
73161 /* Check sanity of left child page for internal pages */
73162 pgno = get4bytesqlite3Get4byte(pCell);
73163#ifndef SQLITE_OMIT_AUTOVACUUM
73164 if( pBt->autoVacuum ){
73165 checkPtrmap(pCheck, pgno, PTRMAP_BTREE5, iPage);
73166 }
73167#endif
73168 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
73169 keyCanBeEqual = 0;
73170 if( d2!=depth ){
73171 checkAppendMsg(pCheck, "Child page depth differs");
73172 depth = d2;
73173 }
73174 }else{
73175 /* Populate the coverage-checking heap for leaf pages */
73176 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
73177 }
73178 }
73179 *piMinKey = maxKey;
73180
73181 /* Check for complete coverage of the page
73182 */
73183 pCheck->zPfx = 0;
73184 if( doCoverageCheck && pCheck->mxErr>0 ){
73185 /* For leaf pages, the min-heap has already been initialized and the
73186 ** cells have already been inserted. But for internal pages, that has
73187 ** not yet been done, so do it now */
73188 if( !pPage->leaf ){
73189 heap = pCheck->heap;
73190 heap[0] = 0;
73191 for(i=nCell-1; i>=0; i--){
73192 u32 size;
73193 pc = get2byteAligned(&data[cellStart+i*2])((&data[cellStart+i*2])[0]<<8 | (&data[cellStart
+i*2])[1])
;
73194 size = pPage->xCellSize(pPage, &data[pc]);
73195 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
73196 }
73197 }
73198 /* Add the freeblocks to the min-heap
73199 **
73200 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
73201 ** is the offset of the first freeblock, or zero if there are no
73202 ** freeblocks on the page.
73203 */
73204 i = get2byte(&data[hdr+1])((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
73205 while( i>0 ){
73206 int size, j;
73207 assert( (u32)i<=usableSize-4 )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
73208 size = get2byte(&data[i+2])((&data[i+2])[0]<<8 | (&data[i+2])[1]);
73209 assert( (u32)(i+size)<=usableSize )((void) (0)); /* due to btreeComputeFreeSpace() */
73210 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
73211 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
73212 ** big-endian integer which is the offset in the b-tree page of the next
73213 ** freeblock in the chain, or zero if the freeblock is the last on the
73214 ** chain. */
73215 j = get2byte(&data[i])((&data[i])[0]<<8 | (&data[i])[1]);
73216 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
73217 ** increasing offset. */
73218 assert( j==0 || j>i+size )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
73219 assert( (u32)j<=usableSize-4 )((void) (0)); /* Enforced by btreeComputeFreeSpace() */
73220 i = j;
73221 }
73222 /* Analyze the min-heap looking for overlap between cells and/or
73223 ** freeblocks, and counting the number of untracked bytes in nFrag.
73224 **
73225 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
73226 ** There is an implied first entry the covers the page header, the cell
73227 ** pointer index, and the gap between the cell pointer index and the start
73228 ** of cell content.
73229 **
73230 ** The loop below pulls entries from the min-heap in order and compares
73231 ** the start_address against the previous end_address. If there is an
73232 ** overlap, that means bytes are used multiple times. If there is a gap,
73233 ** that gap is added to the fragmentation count.
73234 */
73235 nFrag = 0;
73236 prev = contentOffset - 1; /* Implied first min-heap entry */
73237 while( btreeHeapPull(heap,&x) ){
73238 if( (prev&0xffff)>=(x>>16) ){
73239 checkAppendMsg(pCheck,
73240 "Multiple uses for byte %u of page %d", x>>16, iPage);
73241 break;
73242 }else{
73243 nFrag += (x>>16) - (prev&0xffff) - 1;
73244 prev = x;
73245 }
73246 }
73247 nFrag += usableSize - (prev&0xffff) - 1;
73248 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
73249 ** is stored in the fifth field of the b-tree page header.
73250 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
73251 ** number of fragmented free bytes within the cell content area.
73252 */
73253 if( heap[0]==0 && nFrag!=data[hdr+7] ){
73254 checkAppendMsg(pCheck,
73255 "Fragmentation of %d bytes reported as %d on page %d",
73256 nFrag, data[hdr+7], iPage);
73257 }
73258 }
73259
73260end_of_check:
73261 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
73262 releasePage(pPage);
73263 pCheck->zPfx = saved_zPfx;
73264 pCheck->v1 = saved_v1;
73265 pCheck->v2 = saved_v2;
73266 return depth+1;
73267}
73268#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
73269
73270#ifndef SQLITE_OMIT_INTEGRITY_CHECK
73271/*
73272** This routine does a complete check of the given BTree file. aRoot[] is
73273** an array of pages numbers were each page number is the root page of
73274** a table. nRoot is the number of entries in aRoot.
73275**
73276** A read-only or read-write transaction must be opened before calling
73277** this function.
73278**
73279** Write the number of error seen in *pnErr. Except for some memory
73280** allocation errors, an error message held in memory obtained from
73281** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
73282** returned. If a memory allocation error occurs, NULL is returned.
73283*/
73284SQLITE_PRIVATEstatic char *sqlite3BtreeIntegrityCheck(
73285 Btree *p, /* The btree to be checked */
73286 int *aRoot, /* An array of root pages numbers for individual trees */
73287 int nRoot, /* Number of entries in aRoot[] */
73288 int mxErr, /* Stop reporting errors after this many */
73289 int *pnErr /* Write number of errors seen to this variable */
73290){
73291 Pgno i;
73292 IntegrityCk sCheck;
73293 BtShared *pBt = p->pBt;
73294 u64 savedDbFlags = pBt->db->flags;
73295 char zErr[100];
73296 VVA_ONLY( int nRef );
73297
73298 sqlite3BtreeEnter(p);
73299 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE )((void) (0));
73300 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
73301 assert( nRef>=0 )((void) (0));
73302 sCheck.pBt = pBt;
73303 sCheck.pPager = pBt->pPager;
73304 sCheck.nPage = btreePagecount(sCheck.pBt);
73305 sCheck.mxErr = mxErr;
73306 sCheck.nErr = 0;
73307 sCheck.mallocFailed = 0;
73308 sCheck.zPfx = 0;
73309 sCheck.v1 = 0;
73310 sCheck.v2 = 0;
73311 sCheck.aPgRef = 0;
73312 sCheck.heap = 0;
73313 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH1000000000);
73314 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL0x01;
73315 if( sCheck.nPage==0 ){
73316 goto integrity_ck_cleanup;
73317 }
73318
73319 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
73320 if( !sCheck.aPgRef ){
73321 sCheck.mallocFailed = 1;
73322 goto integrity_ck_cleanup;
73323 }
73324 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
73325 if( sCheck.heap==0 ){
73326 sCheck.mallocFailed = 1;
73327 goto integrity_ck_cleanup;
73328 }
73329
73330 i = PENDING_BYTE_PAGE(pBt)((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1));
73331 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
73332
73333 /* Check the integrity of the freelist
73334 */
73335 sCheck.zPfx = "Main freelist: ";
73336 checkList(&sCheck, 1, get4bytesqlite3Get4byte(&pBt->pPage1->aData[32]),
73337 get4bytesqlite3Get4byte(&pBt->pPage1->aData[36]));
73338 sCheck.zPfx = 0;
73339
73340 /* Check all the tables.
73341 */
73342#ifndef SQLITE_OMIT_AUTOVACUUM
73343 if( pBt->autoVacuum ){
73344 int mx = 0;
73345 int mxInHdr;
73346 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
73347 mxInHdr = get4bytesqlite3Get4byte(&pBt->pPage1->aData[52]);
73348 if( mx!=mxInHdr ){
73349 checkAppendMsg(&sCheck,
73350 "max rootpage (%d) disagrees with header (%d)",
73351 mx, mxInHdr
73352 );
73353 }
73354 }else if( get4bytesqlite3Get4byte(&pBt->pPage1->aData[64])!=0 ){
73355 checkAppendMsg(&sCheck,
73356 "incremental_vacuum enabled with a max rootpage of zero"
73357 );
73358 }
73359#endif
73360 testcase( pBt->db->flags & SQLITE_CellSizeCk );
73361 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk0x00200000;
73362 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
73363 i64 notUsed;
73364 if( aRoot[i]==0 ) continue;
73365#ifndef SQLITE_OMIT_AUTOVACUUM
73366 if( pBt->autoVacuum && aRoot[i]>1 ){
73367 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE1, 0);
73368 }
73369#endif
73370 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
73371 }
73372 pBt->db->flags = savedDbFlags;
73373
73374 /* Make sure every page in the file is referenced
73375 */
73376 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
73377#ifdef SQLITE_OMIT_AUTOVACUUM
73378 if( getPageReferenced(&sCheck, i)==0 ){
73379 checkAppendMsg(&sCheck, "Page %d is never used", i);
73380 }
73381#else
73382 /* If the database supports auto-vacuum, make sure no tables contain
73383 ** references to pointer-map pages.
73384 */
73385 if( getPageReferenced(&sCheck, i)==0 &&
73386 (PTRMAP_PAGENO(pBt, i)ptrmapPageno(pBt, i)!=i || !pBt->autoVacuum) ){
73387 checkAppendMsg(&sCheck, "Page %d is never used", i);
73388 }
73389 if( getPageReferenced(&sCheck, i)!=0 &&
73390 (PTRMAP_PAGENO(pBt, i)ptrmapPageno(pBt, i)==i && pBt->autoVacuum) ){
73391 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
73392 }
73393#endif
73394 }
73395
73396 /* Clean up and report errors.
73397 */
73398integrity_ck_cleanup:
73399 sqlite3PageFree(sCheck.heap);
73400 sqlite3_free(sCheck.aPgRef);
73401 if( sCheck.mallocFailed ){
73402 sqlite3_str_reset(&sCheck.errMsg);
73403 sCheck.nErr++;
73404 }
73405 *pnErr = sCheck.nErr;
73406 if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
73407 /* Make sure this analysis did not leave any unref() pages. */
73408 assert( nRef==sqlite3PagerRefcount(pBt->pPager) )((void) (0));
73409 sqlite3BtreeLeave(p);
73410 return sqlite3StrAccumFinish(&sCheck.errMsg);
73411}
73412#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
73413
73414/*
73415** Return the full pathname of the underlying database file. Return
73416** an empty string if the database is in-memory or a TEMP database.
73417**
73418** The pager filename is invariant as long as the pager is
73419** open so it is safe to access without the BtShared mutex.
73420*/
73421SQLITE_PRIVATEstatic const char *sqlite3BtreeGetFilename(Btree *p){
73422 assert( p->pBt->pPager!=0 )((void) (0));
73423 return sqlite3PagerFilename(p->pBt->pPager, 1);
73424}
73425
73426/*
73427** Return the pathname of the journal file for this database. The return
73428** value of this routine is the same regardless of whether the journal file
73429** has been created or not.
73430**
73431** The pager journal filename is invariant as long as the pager is
73432** open so it is safe to access without the BtShared mutex.
73433*/
73434SQLITE_PRIVATEstatic const char *sqlite3BtreeGetJournalname(Btree *p){
73435 assert( p->pBt->pPager!=0 )((void) (0));
73436 return sqlite3PagerJournalname(p->pBt->pPager);
73437}
73438
73439/*
73440** Return non-zero if a transaction is active.
73441*/
73442SQLITE_PRIVATEstatic int sqlite3BtreeIsInTrans(Btree *p){
73443 assert( p==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
73444 return (p && (p->inTrans==TRANS_WRITE2));
73445}
73446
73447#ifndef SQLITE_OMIT_WAL
73448/*
73449** Run a checkpoint on the Btree passed as the first argument.
73450**
73451** Return SQLITE_LOCKED if this or any other connection has an open
73452** transaction on the shared-cache the argument Btree is connected to.
73453**
73454** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
73455*/
73456SQLITE_PRIVATEstatic int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
73457 int rc = SQLITE_OK0;
73458 if( p ){
73459 BtShared *pBt = p->pBt;
73460 sqlite3BtreeEnter(p);
73461 if( pBt->inTransaction!=TRANS_NONE0 ){
73462 rc = SQLITE_LOCKED6;
73463 }else{
73464 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
73465 }
73466 sqlite3BtreeLeave(p);
73467 }
73468 return rc;
73469}
73470#endif
73471
73472/*
73473** Return non-zero if a read (or write) transaction is active.
73474*/
73475SQLITE_PRIVATEstatic int sqlite3BtreeIsInReadTrans(Btree *p){
73476 assert( p )((void) (0));
73477 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
73478 return p->inTrans!=TRANS_NONE0;
73479}
73480
73481SQLITE_PRIVATEstatic int sqlite3BtreeIsInBackup(Btree *p){
73482 assert( p )((void) (0));
73483 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
73484 return p->nBackup!=0;
73485}
73486
73487/*
73488** This function returns a pointer to a blob of memory associated with
73489** a single shared-btree. The memory is used by client code for its own
73490** purposes (for example, to store a high-level schema associated with
73491** the shared-btree). The btree layer manages reference counting issues.
73492**
73493** The first time this is called on a shared-btree, nBytes bytes of memory
73494** are allocated, zeroed, and returned to the caller. For each subsequent
73495** call the nBytes parameter is ignored and a pointer to the same blob
73496** of memory returned.
73497**
73498** If the nBytes parameter is 0 and the blob of memory has not yet been
73499** allocated, a null pointer is returned. If the blob has already been
73500** allocated, it is returned as normal.
73501**
73502** Just before the shared-btree is closed, the function passed as the
73503** xFree argument when the memory allocation was made is invoked on the
73504** blob of allocated memory. The xFree function should not call sqlite3_free()
73505** on the memory, the btree layer does that.
73506*/
73507SQLITE_PRIVATEstatic void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
73508 BtShared *pBt = p->pBt;
73509 sqlite3BtreeEnter(p);
73510 if( !pBt->pSchema && nBytes ){
73511 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
73512 pBt->xFreeSchema = xFree;
73513 }
73514 sqlite3BtreeLeave(p);
73515 return pBt->pSchema;
73516}
73517
73518/*
73519** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
73520** btree as the argument handle holds an exclusive lock on the
73521** sqlite_master table. Otherwise SQLITE_OK.
73522*/
73523SQLITE_PRIVATEstatic int sqlite3BtreeSchemaLocked(Btree *p){
73524 int rc;
73525 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
73526 sqlite3BtreeEnter(p);
73527 rc = querySharedCacheTableLock(p, MASTER_ROOT1, READ_LOCK1);
73528 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE )((void) (0));
73529 sqlite3BtreeLeave(p);
73530 return rc;
73531}
73532
73533
73534#ifndef SQLITE_OMIT_SHARED_CACHE
73535/*
73536** Obtain a lock on the table whose root page is iTab. The
73537** lock is a write lock if isWritelock is true or a read lock
73538** if it is false.
73539*/
73540SQLITE_PRIVATEstatic int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
73541 int rc = SQLITE_OK0;
73542 assert( p->inTrans!=TRANS_NONE )((void) (0));
73543 if( p->sharable ){
73544 u8 lockType = READ_LOCK1 + isWriteLock;
73545 assert( READ_LOCK+1==WRITE_LOCK )((void) (0));
73546 assert( isWriteLock==0 || isWriteLock==1 )((void) (0));
73547
73548 sqlite3BtreeEnter(p);
73549 rc = querySharedCacheTableLock(p, iTab, lockType);
73550 if( rc==SQLITE_OK0 ){
73551 rc = setSharedCacheTableLock(p, iTab, lockType);
73552 }
73553 sqlite3BtreeLeave(p);
73554 }
73555 return rc;
73556}
73557#endif
73558
73559#ifndef SQLITE_OMIT_INCRBLOB
73560/*
73561** Argument pCsr must be a cursor opened for writing on an
73562** INTKEY table currently pointing at a valid table entry.
73563** This function modifies the data stored as part of that entry.
73564**
73565** Only the data content may only be modified, it is not possible to
73566** change the length of the data stored. If this function is called with
73567** parameters that attempt to write past the end of the existing data,
73568** no modifications are made and SQLITE_CORRUPT is returned.
73569*/
73570SQLITE_PRIVATEstatic int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
73571 int rc;
73572 assert( cursorOwnsBtShared(pCsr) )((void) (0));
73573 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) )((void) (0));
73574 assert( pCsr->curFlags & BTCF_Incrblob )((void) (0));
73575
73576 rc = restoreCursorPosition(pCsr)(pCsr->eState>=3 ? btreeRestoreCursorPosition(pCsr) : 0
)
;
73577 if( rc!=SQLITE_OK0 ){
73578 return rc;
73579 }
73580 assert( pCsr->eState!=CURSOR_REQUIRESEEK )((void) (0));
73581 if( pCsr->eState!=CURSOR_VALID0 ){
73582 return SQLITE_ABORT4;
73583 }
73584
73585 /* Save the positions of all other cursors open on this table. This is
73586 ** required in case any of them are holding references to an xFetch
73587 ** version of the b-tree page modified by the accessPayload call below.
73588 **
73589 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
73590 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
73591 ** saveAllCursors can only return SQLITE_OK.
73592 */
73593 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
73594 assert( rc==SQLITE_OK )((void) (0));
73595
73596 /* Check some assumptions:
73597 ** (a) the cursor is open for writing,
73598 ** (b) there is a read/write transaction open,
73599 ** (c) the connection holds a write-lock on the table (if required),
73600 ** (d) there are no conflicting read-locks, and
73601 ** (e) the cursor points at a valid row of an intKey table.
73602 */
73603 if( (pCsr->curFlags & BTCF_WriteFlag0x01)==0 ){
73604 return SQLITE_READONLY8;
73605 }
73606 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0((void) (0))
73607 && pCsr->pBt->inTransaction==TRANS_WRITE )((void) (0));
73608 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) )((void) (0));
73609 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) )((void) (0));
73610 assert( pCsr->pPage->intKey )((void) (0));
73611
73612 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
73613}
73614
73615/*
73616** Mark this cursor as an incremental blob cursor.
73617*/
73618SQLITE_PRIVATEstatic void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
73619 pCur->curFlags |= BTCF_Incrblob0x10;
73620 pCur->pBtree->hasIncrblobCur = 1;
73621}
73622#endif
73623
73624/*
73625** Set both the "read version" (single byte at byte offset 18) and
73626** "write version" (single byte at byte offset 19) fields in the database
73627** header to iVersion.
73628*/
73629SQLITE_PRIVATEstatic int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
73630 BtShared *pBt = pBtree->pBt;
73631 int rc; /* Return code */
73632
73633 assert( iVersion==1 || iVersion==2 )((void) (0));
73634
73635 /* If setting the version fields to 1, do not automatically open the
73636 ** WAL connection, even if the version fields are currently set to 2.
73637 */
73638 pBt->btsFlags &= ~BTS_NO_WAL0x0020;
73639 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL0x0020;
73640
73641 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
73642 if( rc==SQLITE_OK0 ){
73643 u8 *aData = pBt->pPage1->aData;
73644 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
73645 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
73646 if( rc==SQLITE_OK0 ){
73647 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
73648 if( rc==SQLITE_OK0 ){
73649 aData[18] = (u8)iVersion;
73650 aData[19] = (u8)iVersion;
73651 }
73652 }
73653 }
73654 }
73655
73656 pBt->btsFlags &= ~BTS_NO_WAL0x0020;
73657 return rc;
73658}
73659
73660/*
73661** Return true if the cursor has a hint specified. This routine is
73662** only used from within assert() statements
73663*/
73664SQLITE_PRIVATEstatic int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
73665 return (pCsr->hints & mask)!=0;
73666}
73667
73668/*
73669** Return true if the given Btree is read-only.
73670*/
73671SQLITE_PRIVATEstatic int sqlite3BtreeIsReadonly(Btree *p){
73672 return (p->pBt->btsFlags & BTS_READ_ONLY0x0001)!=0;
73673}
73674
73675/*
73676** Return the size of the header added to each page by this module.
73677*/
73678SQLITE_PRIVATEstatic int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage))(((sizeof(MemPage))+7)&~7); }
73679
73680#if !defined(SQLITE_OMIT_SHARED_CACHE)
73681/*
73682** Return true if the Btree passed as the only argument is sharable.
73683*/
73684SQLITE_PRIVATEstatic int sqlite3BtreeSharable(Btree *p){
73685 return p->sharable;
73686}
73687
73688/*
73689** Return the number of connections to the BtShared object accessed by
73690** the Btree handle passed as the only argument. For private caches
73691** this is always 1. For shared caches it may be 1 or greater.
73692*/
73693SQLITE_PRIVATEstatic int sqlite3BtreeConnectionCount(Btree *p){
73694 testcase( p->sharable );
73695 return p->pBt->nRef;
73696}
73697#endif
73698
73699/************** End of btree.c ***********************************************/
73700/************** Begin file backup.c ******************************************/
73701/*
73702** 2009 January 28
73703**
73704** The author disclaims copyright to this source code. In place of
73705** a legal notice, here is a blessing:
73706**
73707** May you do good and not evil.
73708** May you find forgiveness for yourself and forgive others.
73709** May you share freely, never taking more than you give.
73710**
73711*************************************************************************
73712** This file contains the implementation of the sqlite3_backup_XXX()
73713** API functions and the related features.
73714*/
73715/* #include "sqliteInt.h" */
73716/* #include "btreeInt.h" */
73717
73718/*
73719** Structure allocated for each backup operation.
73720*/
73721struct sqlite3_backup {
73722 sqlite3* pDestDb; /* Destination database handle */
73723 Btree *pDest; /* Destination b-tree file */
73724 u32 iDestSchema; /* Original schema cookie in destination */
73725 int bDestLocked; /* True once a write-transaction is open on pDest */
73726
73727 Pgno iNext; /* Page number of the next source page to copy */
73728 sqlite3* pSrcDb; /* Source database handle */
73729 Btree *pSrc; /* Source b-tree file */
73730
73731 int rc; /* Backup process error code */
73732
73733 /* These two variables are set by every call to backup_step(). They are
73734 ** read by calls to backup_remaining() and backup_pagecount().
73735 */
73736 Pgno nRemaining; /* Number of pages left to copy */
73737 Pgno nPagecount; /* Total number of pages to copy */
73738
73739 int isAttached; /* True once backup has been registered with pager */
73740 sqlite3_backup *pNext; /* Next backup associated with source pager */
73741};
73742
73743/*
73744** THREAD SAFETY NOTES:
73745**
73746** Once it has been created using backup_init(), a single sqlite3_backup
73747** structure may be accessed via two groups of thread-safe entry points:
73748**
73749** * Via the sqlite3_backup_XXX() API function backup_step() and
73750** backup_finish(). Both these functions obtain the source database
73751** handle mutex and the mutex associated with the source BtShared
73752** structure, in that order.
73753**
73754** * Via the BackupUpdate() and BackupRestart() functions, which are
73755** invoked by the pager layer to report various state changes in
73756** the page cache associated with the source database. The mutex
73757** associated with the source database BtShared structure will always
73758** be held when either of these functions are invoked.
73759**
73760** The other sqlite3_backup_XXX() API functions, backup_remaining() and
73761** backup_pagecount() are not thread-safe functions. If they are called
73762** while some other thread is calling backup_step() or backup_finish(),
73763** the values returned may be invalid. There is no way for a call to
73764** BackupUpdate() or BackupRestart() to interfere with backup_remaining()
73765** or backup_pagecount().
73766**
73767** Depending on the SQLite configuration, the database handles and/or
73768** the Btree objects may have their own mutexes that require locking.
73769** Non-sharable Btrees (in-memory databases for example), do not have
73770** associated mutexes.
73771*/
73772
73773/*
73774** Return a pointer corresponding to database zDb (i.e. "main", "temp")
73775** in connection handle pDb. If such a database cannot be found, return
73776** a NULL pointer and write an error message to pErrorDb.
73777**
73778** If the "temp" database is requested, it may need to be opened by this
73779** function. If an error occurs while doing so, return 0 and write an
73780** error message to pErrorDb.
73781*/
73782static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
73783 int i = sqlite3FindDbName(pDb, zDb);
73784
73785 if( i==1 ){
73786 Parse sParse;
73787 int rc = 0;
73788 memset(&sParse, 0, sizeof(sParse));
73789 sParse.db = pDb;
73790 if( sqlite3OpenTempDatabase(&sParse) ){
73791 sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
73792 rc = SQLITE_ERROR1;
73793 }
73794 sqlite3DbFree(pErrorDb, sParse.zErrMsg);
73795 sqlite3ParserReset(&sParse);
73796 if( rc ){
73797 return 0;
73798 }
73799 }
73800
73801 if( i<0 ){
73802 sqlite3ErrorWithMsg(pErrorDb, SQLITE_ERROR1, "unknown database %s", zDb);
73803 return 0;
73804 }
73805
73806 return pDb->aDb[i].pBt;
73807}
73808
73809/*
73810** Attempt to set the page size of the destination to match the page size
73811** of the source.
73812*/
73813static int setDestPgsz(sqlite3_backup *p){
73814 int rc;
73815 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
73816 return rc;
73817}
73818
73819/*
73820** Check that there is no open read-transaction on the b-tree passed as the
73821** second argument. If there is not, return SQLITE_OK. Otherwise, if there
73822** is an open read-transaction, return SQLITE_ERROR and leave an error
73823** message in database handle db.
73824*/
73825static int checkReadTransaction(sqlite3 *db, Btree *p){
73826 if( sqlite3BtreeIsInReadTrans(p) ){
73827 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "destination database is in use");
73828 return SQLITE_ERROR1;
73829 }
73830 return SQLITE_OK0;
73831}
73832
73833/*
73834** Create an sqlite3_backup process to copy the contents of zSrcDb from
73835** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
73836** a pointer to the new sqlite3_backup object.
73837**
73838** If an error occurs, NULL is returned and an error code and error message
73839** stored in database handle pDestDb.
73840*/
73841SQLITE_API sqlite3_backup *sqlite3_backup_init(
73842 sqlite3* pDestDb, /* Database to write to */
73843 const char *zDestDb, /* Name of database within pDestDb */
73844 sqlite3* pSrcDb, /* Database connection to read from */
73845 const char *zSrcDb /* Name of database within pSrcDb */
73846){
73847 sqlite3_backup *p; /* Value to return */
73848
73849#ifdef SQLITE_ENABLE_API_ARMOR
73850 if( !sqlite3SafetyCheckOk(pSrcDb)||!sqlite3SafetyCheckOk(pDestDb) ){
73851 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(73851);
73852 return 0;
73853 }
73854#endif
73855
73856 /* Lock the source database handle. The destination database
73857 ** handle is not locked in this routine, but it is locked in
73858 ** sqlite3_backup_step(). The user is required to ensure that no
73859 ** other thread accesses the destination handle for the duration
73860 ** of the backup operation. Any attempt to use the destination
73861 ** database connection while a backup is in progress may cause
73862 ** a malfunction or a deadlock.
73863 */
73864 sqlite3_mutex_enter(pSrcDb->mutex);
73865 sqlite3_mutex_enter(pDestDb->mutex);
73866
73867 if( pSrcDb==pDestDb ){
73868 sqlite3ErrorWithMsg(
73869 pDestDb, SQLITE_ERROR1, "source and destination must be distinct"
73870 );
73871 p = 0;
73872 }else {
73873 /* Allocate space for a new sqlite3_backup object...
73874 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
73875 ** call to sqlite3_backup_init() and is destroyed by a call to
73876 ** sqlite3_backup_finish(). */
73877 p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
73878 if( !p ){
73879 sqlite3Error(pDestDb, SQLITE_NOMEM_BKPT7);
73880 }
73881 }
73882
73883 /* If the allocation succeeded, populate the new object. */
73884 if( p ){
73885 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
73886 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
73887 p->pDestDb = pDestDb;
73888 p->pSrcDb = pSrcDb;
73889 p->iNext = 1;
73890 p->isAttached = 0;
73891
73892 if( 0==p->pSrc || 0==p->pDest
73893 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK0
73894 ){
73895 /* One (or both) of the named databases did not exist or an OOM
73896 ** error was hit. Or there is a transaction open on the destination
73897 ** database. The error has already been written into the pDestDb
73898 ** handle. All that is left to do here is free the sqlite3_backup
73899 ** structure. */
73900 sqlite3_free(p);
73901 p = 0;
73902 }
73903 }
73904 if( p ){
73905 p->pSrc->nBackup++;
73906 }
73907
73908 sqlite3_mutex_leave(pDestDb->mutex);
73909 sqlite3_mutex_leave(pSrcDb->mutex);
73910 return p;
73911}
73912
73913/*
73914** Argument rc is an SQLite error code. Return true if this error is
73915** considered fatal if encountered during a backup operation. All errors
73916** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
73917*/
73918static int isFatalError(int rc){
73919 return (rc!=SQLITE_OK0 && rc!=SQLITE_BUSY5 && ALWAYS(rc!=SQLITE_LOCKED)(rc!=6));
73920}
73921
73922/*
73923** Parameter zSrcData points to a buffer containing the data for
73924** page iSrcPg from the source database. Copy this data into the
73925** destination database.
73926*/
73927static int backupOnePage(
73928 sqlite3_backup *p, /* Backup handle */
73929 Pgno iSrcPg, /* Source database page to backup */
73930 const u8 *zSrcData, /* Source database page data */
73931 int bUpdate /* True for an update, false otherwise */
73932){
73933 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
73934 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
73935 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
73936 const int nCopy = MIN(nSrcPgsz, nDestPgsz)((nSrcPgsz)<(nDestPgsz)?(nSrcPgsz):(nDestPgsz));
73937 const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
73938#ifdef SQLITE_HAS_CODEC
73939 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
73940 ** guaranteed that the shared-mutex is held by this thread, handle
73941 ** p->pSrc may not actually be the owner. */
73942 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
73943 int nDestReserve = sqlite3BtreeGetOptimalReserve(p->pDest);
73944#endif
73945 int rc = SQLITE_OK0;
73946 i64 iOff;
73947
73948 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 )((void) (0));
73949 assert( p->bDestLocked )((void) (0));
73950 assert( !isFatalError(p->rc) )((void) (0));
73951 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) )((void) (0));
73952 assert( zSrcData )((void) (0));
73953
73954 /* Catch the case where the destination is an in-memory database and the
73955 ** page sizes of the source and destination differ.
73956 */
73957 if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
73958 rc = SQLITE_READONLY8;
73959 }
73960
73961#ifdef SQLITE_HAS_CODEC
73962 /* Backup is not possible if the page size of the destination is changing
73963 ** and a codec is in use.
73964 */
73965 if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
73966 rc = SQLITE_READONLY8;
73967 }
73968
73969 /* Backup is not possible if the number of bytes of reserve space differ
73970 ** between source and destination. If there is a difference, try to
73971 ** fix the destination to agree with the source. If that is not possible,
73972 ** then the backup cannot proceed.
73973 */
73974 if( nSrcReserve!=nDestReserve ){
73975 u32 newPgsz = nSrcPgsz;
73976 rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
73977 if( rc==SQLITE_OK0 && newPgsz!=(u32)nSrcPgsz ) rc = SQLITE_READONLY8;
73978 }
73979#endif
73980
73981 /* This loop runs once for each destination page spanned by the source
73982 ** page. For each iteration, variable iOff is set to the byte offset
73983 ** of the destination page.
73984 */
73985 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK0 && iOff<iEnd; iOff+=nDestPgsz){
73986 DbPage *pDestPg = 0;
73987 Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
73988 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
) continue;
73989 if( SQLITE_OK0==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg, 0))
73990 && SQLITE_OK0==(rc = sqlite3PagerWrite(pDestPg))
73991 ){
73992 const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
73993 u8 *zDestData = sqlite3PagerGetData(pDestPg);
73994 u8 *zOut = &zDestData[iOff%nDestPgsz];
73995
73996 /* Copy the data from the source page into the destination page.
73997 ** Then clear the Btree layer MemPage.isInit flag. Both this module
73998 ** and the pager code use this trick (clearing the first byte
73999 ** of the page 'extra' space to invalidate the Btree layers
74000 ** cached parse of the page). MemPage.isInit is marked
74001 ** "MUST BE FIRST" for this purpose.
74002 */
74003 memcpy(zOut, zIn, nCopy);
74004 ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
74005 if( iOff==0 && bUpdate==0 ){
74006 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
74007 }
74008 }
74009 sqlite3PagerUnref(pDestPg);
74010 }
74011
74012 return rc;
74013}
74014
74015/*
74016** If pFile is currently larger than iSize bytes, then truncate it to
74017** exactly iSize bytes. If pFile is not larger than iSize bytes, then
74018** this function is a no-op.
74019**
74020** Return SQLITE_OK if everything is successful, or an SQLite error
74021** code if an error occurs.
74022*/
74023static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
74024 i64 iCurrent;
74025 int rc = sqlite3OsFileSize(pFile, &iCurrent);
74026 if( rc==SQLITE_OK0 && iCurrent>iSize ){
74027 rc = sqlite3OsTruncate(pFile, iSize);
74028 }
74029 return rc;
74030}
74031
74032/*
74033** Register this backup object with the associated source pager for
74034** callbacks when pages are changed or the cache invalidated.
74035*/
74036static void attachBackupObject(sqlite3_backup *p){
74037 sqlite3_backup **pp;
74038 assert( sqlite3BtreeHoldsMutex(p->pSrc) )((void) (0));
74039 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
74040 p->pNext = *pp;
74041 *pp = p;
74042 p->isAttached = 1;
74043}
74044
74045/*
74046** Copy nPage pages from the source b-tree to the destination.
74047*/
74048SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
74049 int rc;
74050 int destMode; /* Destination journal mode */
74051 int pgszSrc = 0; /* Source page size */
74052 int pgszDest = 0; /* Destination page size */
74053
74054#ifdef SQLITE_ENABLE_API_ARMOR
74055 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(74055);
74056#endif
74057 sqlite3_mutex_enter(p->pSrcDb->mutex);
74058 sqlite3BtreeEnter(p->pSrc);
74059 if( p->pDestDb ){
74060 sqlite3_mutex_enter(p->pDestDb->mutex);
74061 }
74062
74063 rc = p->rc;
74064 if( !isFatalError(rc) ){
74065 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
74066 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
74067 int ii; /* Iterator variable */
74068 int nSrcPage = -1; /* Size of source db in pages */
74069 int bCloseTrans = 0; /* True if src db requires unlocking */
74070
74071 /* If the source pager is currently in a write-transaction, return
74072 ** SQLITE_BUSY immediately.
74073 */
74074 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE2 ){
74075 rc = SQLITE_BUSY5;
74076 }else{
74077 rc = SQLITE_OK0;
74078 }
74079
74080 /* If there is no open read-transaction on the source database, open
74081 ** one now. If a transaction is opened here, then it will be closed
74082 ** before this function exits.
74083 */
74084 if( rc==SQLITE_OK0 && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
74085 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
74086 bCloseTrans = 1;
74087 }
74088
74089 /* If the destination database has not yet been locked (i.e. if this
74090 ** is the first call to backup_step() for the current backup operation),
74091 ** try to set its page size to the same as the source database. This
74092 ** is especially important on ZipVFS systems, as in that case it is
74093 ** not possible to create a database file that uses one page size by
74094 ** writing to it with another. */
74095 if( p->bDestLocked==0 && rc==SQLITE_OK0 && setDestPgsz(p)==SQLITE_NOMEM7 ){
74096 rc = SQLITE_NOMEM7;
74097 }
74098
74099 /* Lock the destination database, if it is not locked already. */
74100 if( SQLITE_OK0==rc && p->bDestLocked==0
74101 && SQLITE_OK0==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
74102 (int*)&p->iDestSchema))
74103 ){
74104 p->bDestLocked = 1;
74105 }
74106
74107 /* Do not allow backup if the destination database is in WAL mode
74108 ** and the page sizes are different between source and destination */
74109 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
74110 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
74111 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
74112 if( SQLITE_OK0==rc && destMode==PAGER_JOURNALMODE_WAL5 && pgszSrc!=pgszDest ){
74113 rc = SQLITE_READONLY8;
74114 }
74115
74116 /* Now that there is a read-lock on the source database, query the
74117 ** source pager for the number of pages in the database.
74118 */
74119 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
74120 assert( nSrcPage>=0 )((void) (0));
74121 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
74122 const Pgno iSrcPg = p->iNext; /* Source page number */
74123 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt)((Pgno)((sqlite3PendingByte/((p->pSrc->pBt)->pageSize
))+1))
){
74124 DbPage *pSrcPg; /* Source page object */
74125 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg,PAGER_GET_READONLY0x02);
74126 if( rc==SQLITE_OK0 ){
74127 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
74128 sqlite3PagerUnref(pSrcPg);
74129 }
74130 }
74131 p->iNext++;
74132 }
74133 if( rc==SQLITE_OK0 ){
74134 p->nPagecount = nSrcPage;
74135 p->nRemaining = nSrcPage+1-p->iNext;
74136 if( p->iNext>(Pgno)nSrcPage ){
74137 rc = SQLITE_DONE101;
74138 }else if( !p->isAttached ){
74139 attachBackupObject(p);
74140 }
74141 }
74142
74143 /* Update the schema version field in the destination database. This
74144 ** is to make sure that the schema-version really does change in
74145 ** the case where the source and destination databases have the
74146 ** same schema version.
74147 */
74148 if( rc==SQLITE_DONE101 ){
74149 if( nSrcPage==0 ){
74150 rc = sqlite3BtreeNewDb(p->pDest);
74151 nSrcPage = 1;
74152 }
74153 if( rc==SQLITE_OK0 || rc==SQLITE_DONE101 ){
74154 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
74155 }
74156 if( rc==SQLITE_OK0 ){
74157 if( p->pDestDb ){
74158 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
74159 }
74160 if( destMode==PAGER_JOURNALMODE_WAL5 ){
74161 rc = sqlite3BtreeSetVersion(p->pDest, 2);
74162 }
74163 }
74164 if( rc==SQLITE_OK0 ){
74165 int nDestTruncate;
74166 /* Set nDestTruncate to the final number of pages in the destination
74167 ** database. The complication here is that the destination page
74168 ** size may be different to the source page size.
74169 **
74170 ** If the source page size is smaller than the destination page size,
74171 ** round up. In this case the call to sqlite3OsTruncate() below will
74172 ** fix the size of the file. However it is important to call
74173 ** sqlite3PagerTruncateImage() here so that any pages in the
74174 ** destination file that lie beyond the nDestTruncate page mark are
74175 ** journalled by PagerCommitPhaseOne() before they are destroyed
74176 ** by the file truncation.
74177 */
74178 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) )((void) (0));
74179 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) )((void) (0));
74180 if( pgszSrc<pgszDest ){
74181 int ratio = pgszDest/pgszSrc;
74182 nDestTruncate = (nSrcPage+ratio-1)/ratio;
74183 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
){
74184 nDestTruncate--;
74185 }
74186 }else{
74187 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
74188 }
74189 assert( nDestTruncate>0 )((void) (0));
74190
74191 if( pgszSrc<pgszDest ){
74192 /* If the source page-size is smaller than the destination page-size,
74193 ** two extra things may need to happen:
74194 **
74195 ** * The destination may need to be truncated, and
74196 **
74197 ** * Data stored on the pages immediately following the
74198 ** pending-byte page in the source database may need to be
74199 ** copied into the destination database.
74200 */
74201 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
74202 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
74203 Pgno iPg;
74204 int nDstPage;
74205 i64 iOff;
74206 i64 iEnd;
74207
74208 assert( pFile )((void) (0));
74209 assert( nDestTruncate==0((void) (0))
74210 || (i64)nDestTruncate*(i64)pgszDest >= iSize || (((void) (0))
74211 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)((void) (0))
74212 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest((void) (0))
74213 ))((void) (0));
74214
74215 /* This block ensures that all data required to recreate the original
74216 ** database has been stored in the journal for pDestPager and the
74217 ** journal synced to disk. So at this point we may safely modify
74218 ** the database file in any way, knowing that if a power failure
74219 ** occurs, the original database will be reconstructed from the
74220 ** journal file. */
74221 sqlite3PagerPagecount(pDestPager, &nDstPage);
74222 for(iPg=nDestTruncate; rc==SQLITE_OK0 && iPg<=(Pgno)nDstPage; iPg++){
74223 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt)((Pgno)((sqlite3PendingByte/((p->pDest->pBt)->pageSize
))+1))
){
74224 DbPage *pPg;
74225 rc = sqlite3PagerGet(pDestPager, iPg, &pPg, 0);
74226 if( rc==SQLITE_OK0 ){
74227 rc = sqlite3PagerWrite(pPg);
74228 sqlite3PagerUnref(pPg);
74229 }
74230 }
74231 }
74232 if( rc==SQLITE_OK0 ){
74233 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
74234 }
74235
74236 /* Write the extra pages and truncate the database file as required */
74237 iEnd = MIN(PENDING_BYTE + pgszDest, iSize)((sqlite3PendingByte + pgszDest)<(iSize)?(sqlite3PendingByte
+ pgszDest):(iSize))
;
74238 for(
74239 iOff=PENDING_BYTEsqlite3PendingByte+pgszSrc;
74240 rc==SQLITE_OK0 && iOff<iEnd;
74241 iOff+=pgszSrc
74242 ){
74243 PgHdr *pSrcPg = 0;
74244 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
74245 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg, 0);
74246 if( rc==SQLITE_OK0 ){
74247 u8 *zData = sqlite3PagerGetData(pSrcPg);
74248 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
74249 }
74250 sqlite3PagerUnref(pSrcPg);
74251 }
74252 if( rc==SQLITE_OK0 ){
74253 rc = backupTruncateFile(pFile, iSize);
74254 }
74255
74256 /* Sync the database file to disk. */
74257 if( rc==SQLITE_OK0 ){
74258 rc = sqlite3PagerSync(pDestPager, 0);
74259 }
74260 }else{
74261 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
74262 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
74263 }
74264
74265 /* Finish committing the transaction to the destination database. */
74266 if( SQLITE_OK0==rc
74267 && SQLITE_OK0==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
74268 ){
74269 rc = SQLITE_DONE101;
74270 }
74271 }
74272 }
74273
74274 /* If bCloseTrans is true, then this function opened a read transaction
74275 ** on the source database. Close the read transaction here. There is
74276 ** no need to check the return values of the btree methods here, as
74277 ** "committing" a read-only transaction cannot fail.
74278 */
74279 if( bCloseTrans ){
74280 TESTONLY( int rc2 );
74281 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
74282 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
74283 assert( rc2==SQLITE_OK )((void) (0));
74284 }
74285
74286 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
74287 rc = SQLITE_NOMEM_BKPT7;
74288 }
74289 p->rc = rc;
74290 }
74291 if( p->pDestDb ){
74292 sqlite3_mutex_leave(p->pDestDb->mutex);
74293 }
74294 sqlite3BtreeLeave(p->pSrc);
74295 sqlite3_mutex_leave(p->pSrcDb->mutex);
74296 return rc;
74297}
74298
74299/*
74300** Release all resources associated with an sqlite3_backup* handle.
74301*/
74302SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
74303 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
74304 sqlite3 *pSrcDb; /* Source database connection */
74305 int rc; /* Value to return */
74306
74307 /* Enter the mutexes */
74308 if( p==0 ) return SQLITE_OK0;
74309 pSrcDb = p->pSrcDb;
74310 sqlite3_mutex_enter(pSrcDb->mutex);
74311 sqlite3BtreeEnter(p->pSrc);
74312 if( p->pDestDb ){
74313 sqlite3_mutex_enter(p->pDestDb->mutex);
74314 }
74315
74316 /* Detach this backup from the source pager. */
74317 if( p->pDestDb ){
74318 p->pSrc->nBackup--;
74319 }
74320 if( p->isAttached ){
74321 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
74322 while( *pp!=p ){
74323 pp = &(*pp)->pNext;
74324 }
74325 *pp = p->pNext;
74326 }
74327
74328 /* If a transaction is still open on the Btree, roll it back. */
74329 sqlite3BtreeRollback(p->pDest, SQLITE_OK0, 0);
74330
74331 /* Set the error code of the destination database handle. */
74332 rc = (p->rc==SQLITE_DONE101) ? SQLITE_OK0 : p->rc;
74333 if( p->pDestDb ){
74334 sqlite3Error(p->pDestDb, rc);
74335
74336 /* Exit the mutexes and free the backup context structure. */
74337 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
74338 }
74339 sqlite3BtreeLeave(p->pSrc);
74340 if( p->pDestDb ){
74341 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
74342 ** call to sqlite3_backup_init() and is destroyed by a call to
74343 ** sqlite3_backup_finish(). */
74344 sqlite3_free(p);
74345 }
74346 sqlite3LeaveMutexAndCloseZombie(pSrcDb);
74347 return rc;
74348}
74349
74350/*
74351** Return the number of pages still to be backed up as of the most recent
74352** call to sqlite3_backup_step().
74353*/
74354SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
74355#ifdef SQLITE_ENABLE_API_ARMOR
74356 if( p==0 ){
74357 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(74357);
74358 return 0;
74359 }
74360#endif
74361 return p->nRemaining;
74362}
74363
74364/*
74365** Return the total number of pages in the source database as of the most
74366** recent call to sqlite3_backup_step().
74367*/
74368SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
74369#ifdef SQLITE_ENABLE_API_ARMOR
74370 if( p==0 ){
74371 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(74371);
74372 return 0;
74373 }
74374#endif
74375 return p->nPagecount;
74376}
74377
74378/*
74379** This function is called after the contents of page iPage of the
74380** source database have been modified. If page iPage has already been
74381** copied into the destination database, then the data written to the
74382** destination is now invalidated. The destination copy of iPage needs
74383** to be updated with the new data before the backup operation is
74384** complete.
74385**
74386** It is assumed that the mutex associated with the BtShared object
74387** corresponding to the source database is held when this function is
74388** called.
74389*/
74390static SQLITE_NOINLINE__attribute__((noinline)) void backupUpdate(
74391 sqlite3_backup *p,
74392 Pgno iPage,
74393 const u8 *aData
74394){
74395 assert( p!=0 )((void) (0));
74396 do{
74397 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) )((void) (0));
74398 if( !isFatalError(p->rc) && iPage<p->iNext ){
74399 /* The backup process p has already copied page iPage. But now it
74400 ** has been modified by a transaction on the source pager. Copy
74401 ** the new data into the backup.
74402 */
74403 int rc;
74404 assert( p->pDestDb )((void) (0));
74405 sqlite3_mutex_enter(p->pDestDb->mutex);
74406 rc = backupOnePage(p, iPage, aData, 1);
74407 sqlite3_mutex_leave(p->pDestDb->mutex);
74408 assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED )((void) (0));
74409 if( rc!=SQLITE_OK0 ){
74410 p->rc = rc;
74411 }
74412 }
74413 }while( (p = p->pNext)!=0 );
74414}
74415SQLITE_PRIVATEstatic void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
74416 if( pBackup ) backupUpdate(pBackup, iPage, aData);
74417}
74418
74419/*
74420** Restart the backup process. This is called when the pager layer
74421** detects that the database has been modified by an external database
74422** connection. In this case there is no way of knowing which of the
74423** pages that have been copied into the destination database are still
74424** valid and which are not, so the entire process needs to be restarted.
74425**
74426** It is assumed that the mutex associated with the BtShared object
74427** corresponding to the source database is held when this function is
74428** called.
74429*/
74430SQLITE_PRIVATEstatic void sqlite3BackupRestart(sqlite3_backup *pBackup){
74431 sqlite3_backup *p; /* Iterator variable */
74432 for(p=pBackup; p; p=p->pNext){
74433 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) )((void) (0));
74434 p->iNext = 1;
74435 }
74436}
74437
74438#ifndef SQLITE_OMIT_VACUUM
74439/*
74440** Copy the complete content of pBtFrom into pBtTo. A transaction
74441** must be active for both files.
74442**
74443** The size of file pTo may be reduced by this operation. If anything
74444** goes wrong, the transaction on pTo is rolled back. If successful, the
74445** transaction is committed before returning.
74446*/
74447SQLITE_PRIVATEstatic int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
74448 int rc;
74449 sqlite3_file *pFd; /* File descriptor for database pTo */
74450 sqlite3_backup b;
74451 sqlite3BtreeEnter(pTo);
74452 sqlite3BtreeEnter(pFrom);
74453
74454 assert( sqlite3BtreeIsInTrans(pTo) )((void) (0));
74455 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
74456 if( pFd->pMethods ){
74457 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
74458 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE11, &nByte);
74459 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
74460 if( rc ) goto copy_finished;
74461 }
74462
74463 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
74464 ** to 0. This is used by the implementations of sqlite3_backup_step()
74465 ** and sqlite3_backup_finish() to detect that they are being called
74466 ** from this function, not directly by the user.
74467 */
74468 memset(&b, 0, sizeof(b));
74469 b.pSrcDb = pFrom->db;
74470 b.pSrc = pFrom;
74471 b.pDest = pTo;
74472 b.iNext = 1;
74473
74474#ifdef SQLITE_HAS_CODEC
74475 sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom));
74476#endif
74477
74478 /* 0x7FFFFFFF is the hard limit for the number of pages in a database
74479 ** file. By passing this as the number of pages to copy to
74480 ** sqlite3_backup_step(), we can guarantee that the copy finishes
74481 ** within a single call (unless an error occurs). The assert() statement
74482 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
74483 ** or an error code. */
74484 sqlite3_backup_step(&b, 0x7FFFFFFF);
74485 assert( b.rc!=SQLITE_OK )((void) (0));
74486
74487 rc = sqlite3_backup_finish(&b);
74488 if( rc==SQLITE_OK0 ){
74489 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED0x0002;
74490 }else{
74491 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
74492 }
74493
74494 assert( sqlite3BtreeIsInTrans(pTo)==0 )((void) (0));
74495copy_finished:
74496 sqlite3BtreeLeave(pFrom);
74497 sqlite3BtreeLeave(pTo);
74498 return rc;
74499}
74500#endif /* SQLITE_OMIT_VACUUM */
74501
74502/************** End of backup.c **********************************************/
74503/************** Begin file vdbemem.c *****************************************/
74504/*
74505** 2004 May 26
74506**
74507** The author disclaims copyright to this source code. In place of
74508** a legal notice, here is a blessing:
74509**
74510** May you do good and not evil.
74511** May you find forgiveness for yourself and forgive others.
74512** May you share freely, never taking more than you give.
74513**
74514*************************************************************************
74515**
74516** This file contains code use to manipulate "Mem" structure. A "Mem"
74517** stores a single value in the VDBE. Mem is an opaque structure visible
74518** only within the VDBE. Interface routines refer to a Mem using the
74519** name sqlite_value
74520*/
74521/* #include "sqliteInt.h" */
74522/* #include "vdbeInt.h" */
74523
74524/* True if X is a power of two. 0 is considered a power of two here.
74525** In other words, return true if X has at most one bit set.
74526*/
74527#define ISPOWEROF2(X)(((X)&((X)-1))==0) (((X)&((X)-1))==0)
74528
74529#ifdef SQLITE_DEBUG
74530/*
74531** Check invariants on a Mem object.
74532**
74533** This routine is intended for use inside of assert() statements, like
74534** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );
74535*/
74536SQLITE_PRIVATEstatic int sqlite3VdbeCheckMemInvariants(Mem *p){
74537 /* If MEM_Dyn is set then Mem.xDel!=0.
74538 ** Mem.xDel might not be initialized if MEM_Dyn is clear.
74539 */
74540 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 )((void) (0));
74541
74542 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we
74543 ** ensure that if Mem.szMalloc>0 then it is safe to do
74544 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
74545 ** That saves a few cycles in inner loops. */
74546 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 )((void) (0));
74547
74548 /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */
74549 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) )((void) (0));
74550
74551 if( p->flags & MEM_Null0x0001 ){
74552 /* Cannot be both MEM_Null and some other type */
74553 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 )((void) (0));
74554
74555 /* If MEM_Null is set, then either the value is a pure NULL (the usual
74556 ** case) or it is a pointer set using sqlite3_bind_pointer() or
74557 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be
74558 ** set.
74559 */
74560 if( (p->flags & (MEM_Term0x0200|MEM_Subtype0x8000))==(MEM_Term0x0200|MEM_Subtype0x8000) ){
74561 /* This is a pointer type. There may be a flag to indicate what to
74562 ** do with the pointer. */
74563 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +((void) (0))
74564 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +((void) (0))
74565 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 )((void) (0));
74566
74567 /* No other bits set */
74568 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind((void) (0))
74569 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 )((void) (0));
74570 }else{
74571 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,
74572 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */
74573 }
74574 }else{
74575 /* The MEM_Cleared bit is only allowed on NULLs */
74576 assert( (p->flags & MEM_Cleared)==0 )((void) (0));
74577 }
74578
74579 /* The szMalloc field holds the correct memory allocation size */
74580 assert( p->szMalloc==0((void) (0))
74581 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) )((void) (0));
74582
74583 /* If p holds a string or blob, the Mem.z must point to exactly
74584 ** one of the following:
74585 **
74586 ** (1) Memory in Mem.zMalloc and managed by the Mem object
74587 ** (2) Memory to be freed using Mem.xDel
74588 ** (3) An ephemeral string or blob
74589 ** (4) A static string or blob
74590 */
74591 if( (p->flags & (MEM_Str0x0002|MEM_Blob0x0010)) && p->n>0 ){
74592 assert(((void) (0))
74593 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +((void) (0))
74594 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +((void) (0))
74595 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +((void) (0))
74596 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1((void) (0))
74597 )((void) (0));
74598 }
74599 return 1;
74600}
74601#endif
74602
74603/*
74604** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
74605** into a buffer.
74606*/
74607static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
74608 StrAccum acc;
74609 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) )((void) (0));
74610 sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0);
74611 if( p->flags & MEM_Int0x0004 ){
74612 sqlite3_str_appendf(&acc, "%lld", p->u.i);
74613 }else if( p->flags & MEM_IntReal0x0020 ){
74614 sqlite3_str_appendf(&acc, "%!.15g", (double)p->u.i);
74615 }else{
74616 sqlite3_str_appendf(&acc, "%!.15g", p->u.r);
74617 }
74618 assert( acc.zText==zBuf && acc.mxAlloc<=0 )((void) (0));
74619 zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
74620}
74621
74622#ifdef SQLITE_DEBUG
74623/*
74624** Validity checks on pMem. pMem holds a string.
74625**
74626** (1) Check that string value of pMem agrees with its integer or real value.
74627** (2) Check that the string is correctly zero terminated
74628**
74629** A single int or real value always converts to the same strings. But
74630** many different strings can be converted into the same int or real.
74631** If a table contains a numeric value and an index is based on the
74632** corresponding string value, then it is important that the string be
74633** derived from the numeric value, not the other way around, to ensure
74634** that the index and table are consistent. See ticket
74635** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
74636** an example.
74637**
74638** This routine looks at pMem to verify that if it has both a numeric
74639** representation and a string representation then the string rep has
74640** been derived from the numeric and not the other way around. It returns
74641** true if everything is ok and false if there is a problem.
74642**
74643** This routine is for use inside of assert() statements only.
74644*/
74645SQLITE_PRIVATEstatic int sqlite3VdbeMemValidStrRep(Mem *p){
74646 char zBuf[100];
74647 char *z;
74648 int i, j, incr;
74649 if( (p->flags & MEM_Str0x0002)==0 ) return 1;
74650 if( p->flags & MEM_Term0x0200 ){
74651 /* Insure that the string is properly zero-terminated. Pay particular
74652 ** attention to the case where p->n is odd */
74653 if( p->szMalloc>0 && p->z==p->zMalloc ){
74654 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 )((void) (0));
74655 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 )((void) (0));
74656 }
74657 assert( p->z[p->n]==0 )((void) (0));
74658 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 )((void) (0));
74659 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 )((void) (0));
74660 }
74661 if( (p->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))==0 ) return 1;
74662 vdbeMemRenderNum(sizeof(zBuf), zBuf, p);
74663 z = p->z;
74664 i = j = 0;
74665 incr = 1;
74666 if( p->enc!=SQLITE_UTF81 ){
74667 incr = 2;
74668 if( p->enc==SQLITE_UTF16BE3 ) z++;
74669 }
74670 while( zBuf[j] ){
74671 if( zBuf[j++]!=z[i] ) return 0;
74672 i += incr;
74673 }
74674 return 1;
74675}
74676#endif /* SQLITE_DEBUG */
74677
74678/*
74679** If pMem is an object with a valid string representation, this routine
74680** ensures the internal encoding for the string representation is
74681** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
74682**
74683** If pMem is not a string object, or the encoding of the string
74684** representation is already stored using the requested encoding, then this
74685** routine is a no-op.
74686**
74687** SQLITE_OK is returned if the conversion is successful (or not required).
74688** SQLITE_NOMEM may be returned if a malloc() fails during conversion
74689** between formats.
74690*/
74691SQLITE_PRIVATEstatic int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
74692#ifndef SQLITE_OMIT_UTF16
74693 int rc;
74694#endif
74695 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
74696 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE((void) (0))
74697 || desiredEnc==SQLITE_UTF16BE )((void) (0));
74698 if( !(pMem->flags&MEM_Str0x0002) || pMem->enc==desiredEnc ){
74699 return SQLITE_OK0;
74700 }
74701 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
74702#ifdef SQLITE_OMIT_UTF16
74703 return SQLITE_ERROR1;
74704#else
74705
74706 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
74707 ** then the encoding of the value may not have changed.
74708 */
74709 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
74710 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM)((void) (0));
74711 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc)((void) (0));
74712 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc)((void) (0));
74713 return rc;
74714#endif
74715}
74716
74717/*
74718** Make sure pMem->z points to a writable allocation of at least n bytes.
74719**
74720** If the bPreserve argument is true, then copy of the content of
74721** pMem->z into the new allocation. pMem must be either a string or
74722** blob if bPreserve is true. If bPreserve is false, any prior content
74723** in pMem->z is discarded.
74724*/
74725SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
74726 assert( sqlite3VdbeCheckMemInvariants(pMem) )((void) (0));
74727 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
74728 testcase( pMem->db==0 );
74729
74730 /* If the bPreserve flag is set to true, then the memory cell must already
74731 ** contain a valid string or blob value. */
74732 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) )((void) (0));
74733 testcase( bPreserve && pMem->z==0 );
74734
74735 assert( pMem->szMalloc==0((void) (0))
74736 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) )((void) (0));
74737 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
74738 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
74739 bPreserve = 0;
74740 }else{
74741 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
74742 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
74743 }
74744 if( pMem->zMalloc==0 ){
74745 sqlite3VdbeMemSetNull(pMem);
74746 pMem->z = 0;
74747 pMem->szMalloc = 0;
74748 return SQLITE_NOMEM_BKPT7;
74749 }else{
74750 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
74751 }
74752
74753 if( bPreserve && pMem->z ){
74754 assert( pMem->z!=pMem->zMalloc )((void) (0));
74755 memcpy(pMem->zMalloc, pMem->z, pMem->n);
74756 }
74757 if( (pMem->flags&MEM_Dyn0x0400)!=0 ){
74758 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC )((void) (0));
74759 pMem->xDel((void *)(pMem->z));
74760 }
74761
74762 pMem->z = pMem->zMalloc;
74763 pMem->flags &= ~(MEM_Dyn0x0400|MEM_Ephem0x1000|MEM_Static0x0800);
74764 return SQLITE_OK0;
74765}
74766
74767/*
74768** Change the pMem->zMalloc allocation to be at least szNew bytes.
74769** If pMem->zMalloc already meets or exceeds the requested size, this
74770** routine is a no-op.
74771**
74772** Any prior string or blob content in the pMem object may be discarded.
74773** The pMem->xDel destructor is called, if it exists. Though MEM_Str
74774** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,
74775** and MEM_Null values are preserved.
74776**
74777** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
74778** if unable to complete the resizing.
74779*/
74780SQLITE_PRIVATEstatic int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
74781 assert( CORRUPT_DB || szNew>0 )((void) (0));
74782 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 )((void) (0));
74783 if( pMem->szMalloc<szNew ){
74784 return sqlite3VdbeMemGrow(pMem, szNew, 0);
74785 }
74786 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
74787 pMem->z = pMem->zMalloc;
74788 pMem->flags &= (MEM_Null0x0001|MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
74789 return SQLITE_OK0;
74790}
74791
74792/*
74793** It is already known that pMem contains an unterminated string.
74794** Add the zero terminator.
74795**
74796** Three bytes of zero are added. In this way, there is guaranteed
74797** to be a double-zero byte at an even byte boundary in order to
74798** terminate a UTF16 string, even if the initial size of the buffer
74799** is an odd number of bytes.
74800*/
74801static SQLITE_NOINLINE__attribute__((noinline)) int vdbeMemAddTerminator(Mem *pMem){
74802 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
74803 return SQLITE_NOMEM_BKPT7;
74804 }
74805 pMem->z[pMem->n] = 0;
74806 pMem->z[pMem->n+1] = 0;
74807 pMem->z[pMem->n+2] = 0;
74808 pMem->flags |= MEM_Term0x0200;
74809 return SQLITE_OK0;
74810}
74811
74812/*
74813** Change pMem so that its MEM_Str or MEM_Blob value is stored in
74814** MEM.zMalloc, where it can be safely written.
74815**
74816** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
74817*/
74818SQLITE_PRIVATEstatic int sqlite3VdbeMemMakeWriteable(Mem *pMem){
74819 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
74820 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
74821 if( (pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 ){
74822 if( ExpandBlob(pMem)(((pMem)->flags&0x4000)?sqlite3VdbeMemExpandBlob(pMem)
:0)
) return SQLITE_NOMEM7;
74823 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
74824 int rc = vdbeMemAddTerminator(pMem);
74825 if( rc ) return rc;
74826 }
74827 }
74828 pMem->flags &= ~MEM_Ephem0x1000;
74829#ifdef SQLITE_DEBUG
74830 pMem->pScopyFrom = 0;
74831#endif
74832
74833 return SQLITE_OK0;
74834}
74835
74836/*
74837** If the given Mem* has a zero-filled tail, turn it into an ordinary
74838** blob stored in dynamically allocated space.
74839*/
74840#ifndef SQLITE_OMIT_INCRBLOB
74841SQLITE_PRIVATEstatic int sqlite3VdbeMemExpandBlob(Mem *pMem){
74842 int nByte;
74843 assert( pMem->flags & MEM_Zero )((void) (0));
74844 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) )((void) (0));
74845 testcase( sqlite3_value_nochange(pMem) );
74846 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
74847 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
74848
74849 /* Set nByte to the number of bytes required to store the expanded blob. */
74850 nByte = pMem->n + pMem->u.nZero;
74851 if( nByte<=0 ){
74852 if( (pMem->flags & MEM_Blob0x0010)==0 ) return SQLITE_OK0;
74853 nByte = 1;
74854 }
74855 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
74856 return SQLITE_NOMEM_BKPT7;
74857 }
74858
74859 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
74860 pMem->n += pMem->u.nZero;
74861 pMem->flags &= ~(MEM_Zero0x4000|MEM_Term0x0200);
74862 return SQLITE_OK0;
74863}
74864#endif
74865
74866/*
74867** Make sure the given Mem is \u0000 terminated.
74868*/
74869SQLITE_PRIVATEstatic int sqlite3VdbeMemNulTerminate(Mem *pMem){
74870 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
74871 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
74872 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
74873 if( (pMem->flags & (MEM_Term0x0200|MEM_Str0x0002))!=MEM_Str0x0002 ){
74874 return SQLITE_OK0; /* Nothing to do */
74875 }else{
74876 return vdbeMemAddTerminator(pMem);
74877 }
74878}
74879
74880/*
74881** Add MEM_Str to the set of representations for the given Mem. This
74882** routine is only called if pMem is a number of some kind, not a NULL
74883** or a BLOB.
74884**
74885** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
74886** if bForce is true but are retained if bForce is false.
74887**
74888** A MEM_Null value will never be passed to this function. This function is
74889** used for converting values to text for returning to the user (i.e. via
74890** sqlite3_value_text()), or for ensuring that values to be used as btree
74891** keys are strings. In the former case a NULL pointer is returned the
74892** user and the latter is an internal programming error.
74893*/
74894SQLITE_PRIVATEstatic int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
74895 const int nByte = 32;
74896
74897 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
74898 assert( !(pMem->flags&MEM_Zero) )((void) (0));
74899 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) )((void) (0));
74900 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) )((void) (0));
74901 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
74902 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
74903
74904
74905 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
74906 pMem->enc = 0;
74907 return SQLITE_NOMEM_BKPT7;
74908 }
74909
74910 vdbeMemRenderNum(nByte, pMem->z, pMem);
74911 assert( pMem->z!=0 )((void) (0));
74912 pMem->n = sqlite3Strlen30NN(pMem->z)(strlen(pMem->z)&0x3fffffff);
74913 pMem->enc = SQLITE_UTF81;
74914 pMem->flags |= MEM_Str0x0002|MEM_Term0x0200;
74915 if( bForce ) pMem->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
74916 sqlite3VdbeChangeEncoding(pMem, enc);
74917 return SQLITE_OK0;
74918}
74919
74920/*
74921** Memory cell pMem contains the context of an aggregate function.
74922** This routine calls the finalize method for that function. The
74923** result of the aggregate is stored back into pMem.
74924**
74925** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
74926** otherwise.
74927*/
74928SQLITE_PRIVATEstatic int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
74929 sqlite3_context ctx;
74930 Mem t;
74931 assert( pFunc!=0 )((void) (0));
74932 assert( pFunc->xFinalize!=0 )((void) (0));
74933 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef )((void) (0));
74934 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
74935 memset(&ctx, 0, sizeof(ctx));
74936 memset(&t, 0, sizeof(t));
74937 t.flags = MEM_Null0x0001;
74938 t.db = pMem->db;
74939 ctx.pOut = &t;
74940 ctx.pMem = pMem;
74941 ctx.pFunc = pFunc;
74942 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
74943 assert( (pMem->flags & MEM_Dyn)==0 )((void) (0));
74944 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
74945 memcpy(pMem, &t, sizeof(t));
74946 return ctx.isError;
74947}
74948
74949/*
74950** Memory cell pAccum contains the context of an aggregate function.
74951** This routine calls the xValue method for that function and stores
74952** the results in memory cell pMem.
74953**
74954** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK
74955** otherwise.
74956*/
74957#ifndef SQLITE_OMIT_WINDOWFUNC
74958SQLITE_PRIVATEstatic int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){
74959 sqlite3_context ctx;
74960 Mem t;
74961 assert( pFunc!=0 )((void) (0));
74962 assert( pFunc->xValue!=0 )((void) (0));
74963 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef )((void) (0));
74964 assert( pAccum->db==0 || sqlite3_mutex_held(pAccum->db->mutex) )((void) (0));
74965 memset(&ctx, 0, sizeof(ctx));
74966 memset(&t, 0, sizeof(t));
74967 t.flags = MEM_Null0x0001;
74968 t.db = pAccum->db;
74969 sqlite3VdbeMemSetNull(pOut);
74970 ctx.pOut = pOut;
74971 ctx.pMem = pAccum;
74972 ctx.pFunc = pFunc;
74973 pFunc->xValue(&ctx);
74974 return ctx.isError;
74975}
74976#endif /* SQLITE_OMIT_WINDOWFUNC */
74977
74978/*
74979** If the memory cell contains a value that must be freed by
74980** invoking the external callback in Mem.xDel, then this routine
74981** will free that value. It also sets Mem.flags to MEM_Null.
74982**
74983** This is a helper routine for sqlite3VdbeMemSetNull() and
74984** for sqlite3VdbeMemRelease(). Use those other routines as the
74985** entry point for releasing Mem resources.
74986*/
74987static SQLITE_NOINLINE__attribute__((noinline)) void vdbeMemClearExternAndSetNull(Mem *p){
74988 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) )((void) (0));
74989 assert( VdbeMemDynamic(p) )((void) (0));
74990 if( p->flags&MEM_Agg0x2000 ){
74991 sqlite3VdbeMemFinalize(p, p->u.pDef);
74992 assert( (p->flags & MEM_Agg)==0 )((void) (0));
74993 testcase( p->flags & MEM_Dyn );
74994 }
74995 if( p->flags&MEM_Dyn0x0400 ){
74996 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 )((void) (0));
74997 p->xDel((void *)p->z);
74998 }
74999 p->flags = MEM_Null0x0001;
75000}
75001
75002/*
75003** Release memory held by the Mem p, both external memory cleared
75004** by p->xDel and memory in p->zMalloc.
75005**
75006** This is a helper routine invoked by sqlite3VdbeMemRelease() in
75007** the unusual case where there really is memory in p that needs
75008** to be freed.
75009*/
75010static SQLITE_NOINLINE__attribute__((noinline)) void vdbeMemClear(Mem *p){
75011 if( VdbeMemDynamic(p)(((p)->flags&(0x2000|0x0400))!=0) ){
75012 vdbeMemClearExternAndSetNull(p);
75013 }
75014 if( p->szMalloc ){
75015 sqlite3DbFreeNN(p->db, p->zMalloc);
75016 p->szMalloc = 0;
75017 }
75018 p->z = 0;
75019}
75020
75021/*
75022** Release any memory resources held by the Mem. Both the memory that is
75023** free by Mem.xDel and the Mem.zMalloc allocation are freed.
75024**
75025** Use this routine prior to clean up prior to abandoning a Mem, or to
75026** reset a Mem back to its minimum memory utilization.
75027**
75028** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
75029** prior to inserting new content into the Mem.
75030*/
75031SQLITE_PRIVATEstatic void sqlite3VdbeMemRelease(Mem *p){
75032 assert( sqlite3VdbeCheckMemInvariants(p) )((void) (0));
75033 if( VdbeMemDynamic(p)(((p)->flags&(0x2000|0x0400))!=0) || p->szMalloc ){
75034 vdbeMemClear(p);
75035 }
75036}
75037
75038/*
75039** Convert a 64-bit IEEE double into a 64-bit signed integer.
75040** If the double is out of range of a 64-bit signed integer then
75041** return the closest available 64-bit signed integer.
75042*/
75043static SQLITE_NOINLINE__attribute__((noinline)) i64 doubleToInt64(double r){
75044#ifdef SQLITE_OMIT_FLOATING_POINT
75045 /* When floating-point is omitted, double and int64 are the same thing */
75046 return r;
75047#else
75048 /*
75049 ** Many compilers we encounter do not define constants for the
75050 ** minimum and maximum 64-bit integers, or they define them
75051 ** inconsistently. And many do not understand the "LL" notation.
75052 ** So we define our own static constants here using nothing
75053 ** larger than a 32-bit integer constant.
75054 */
75055 static const i64 maxInt = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
75056 static const i64 minInt = SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
75057
75058 if( r<=(double)minInt ){
75059 return minInt;
75060 }else if( r>=(double)maxInt ){
75061 return maxInt;
75062 }else{
75063 return (i64)r;
75064 }
75065#endif
75066}
75067
75068/*
75069** Return some kind of integer value which is the best we can do
75070** at representing the value that *pMem describes as an integer.
75071** If pMem is an integer, then the value is exact. If pMem is
75072** a floating-point then the value returned is the integer part.
75073** If pMem is a string or blob, then we make an attempt to convert
75074** it into an integer and return that. If pMem represents an
75075** an SQL-NULL value, return 0.
75076**
75077** If pMem represents a string value, its encoding might be changed.
75078*/
75079static SQLITE_NOINLINE__attribute__((noinline)) i64 memIntValue(Mem *pMem){
75080 i64 value = 0;
75081 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
75082 return value;
75083}
75084SQLITE_PRIVATEstatic i64 sqlite3VdbeIntValue(Mem *pMem){
75085 int flags;
75086 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
75087 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
75088 flags = pMem->flags;
75089 if( flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
75090 testcase( flags & MEM_IntReal );
75091 return pMem->u.i;
75092 }else if( flags & MEM_Real0x0008 ){
75093 return doubleToInt64(pMem->u.r);
75094 }else if( flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
75095 assert( pMem->z || pMem->n==0 )((void) (0));
75096 return memIntValue(pMem);
75097 }else{
75098 return 0;
75099 }
75100}
75101
75102/*
75103** Return the best representation of pMem that we can get into a
75104** double. If pMem is already a double or an integer, return its
75105** value. If it is a string or blob, try to convert it to a double.
75106** If it is a NULL, return 0.0.
75107*/
75108static SQLITE_NOINLINE__attribute__((noinline)) double memRealValue(Mem *pMem){
75109 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
75110 double val = (double)0;
75111 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
75112 return val;
75113}
75114SQLITE_PRIVATEstatic double sqlite3VdbeRealValue(Mem *pMem){
75115 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
75116 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
75117 if( pMem->flags & MEM_Real0x0008 ){
75118 return pMem->u.r;
75119 }else if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
75120 testcase( pMem->flags & MEM_IntReal );
75121 return (double)pMem->u.i;
75122 }else if( pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
75123 return memRealValue(pMem);
75124 }else{
75125 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
75126 return (double)0;
75127 }
75128}
75129
75130/*
75131** Return 1 if pMem represents true, and return 0 if pMem represents false.
75132** Return the value ifNull if pMem is NULL.
75133*/
75134SQLITE_PRIVATEstatic int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
75135 testcase( pMem->flags & MEM_IntReal );
75136 if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ) return pMem->u.i!=0;
75137 if( pMem->flags & MEM_Null0x0001 ) return ifNull;
75138 return sqlite3VdbeRealValue(pMem)!=0.0;
75139}
75140
75141/*
75142** The MEM structure is already a MEM_Real. Try to also make it a
75143** MEM_Int if we can.
75144*/
75145SQLITE_PRIVATEstatic void sqlite3VdbeIntegerAffinity(Mem *pMem){
75146 i64 ix;
75147 assert( pMem->flags & MEM_Real )((void) (0));
75148 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
75149 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
75150 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
75151
75152 ix = doubleToInt64(pMem->u.r);
75153
75154 /* Only mark the value as an integer if
75155 **
75156 ** (1) the round-trip conversion real->int->real is a no-op, and
75157 ** (2) The integer is neither the largest nor the smallest
75158 ** possible integer (ticket #3922)
75159 **
75160 ** The second and third terms in the following conditional enforces
75161 ** the second condition under the assumption that addition overflow causes
75162 ** values to wrap around.
75163 */
75164 if( pMem->u.r==ix && ix>SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) && ix<LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) ){
75165 pMem->u.i = ix;
75166 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0xc1bf|0x4000))|0x0004
)
;
75167 }
75168}
75169
75170/*
75171** Convert pMem to type integer. Invalidate any prior representations.
75172*/
75173SQLITE_PRIVATEstatic int sqlite3VdbeMemIntegerify(Mem *pMem){
75174 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
75175 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
75176 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
75177
75178 pMem->u.i = sqlite3VdbeIntValue(pMem);
75179 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0xc1bf|0x4000))|0x0004
)
;
75180 return SQLITE_OK0;
75181}
75182
75183/*
75184** Convert pMem so that it is of type MEM_Real.
75185** Invalidate any prior representations.
75186*/
75187SQLITE_PRIVATEstatic int sqlite3VdbeMemRealify(Mem *pMem){
75188 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
75189 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
75190
75191 pMem->u.r = sqlite3VdbeRealValue(pMem);
75192 MemSetTypeFlag(pMem, MEM_Real)((pMem)->flags = ((pMem)->flags&~(0xc1bf|0x4000))|0x0008
)
;
75193 return SQLITE_OK0;
75194}
75195
75196/* Compare a floating point value to an integer. Return true if the two
75197** values are the same within the precision of the floating point value.
75198**
75199** This function assumes that i was obtained by assignment from r1.
75200**
75201** For some versions of GCC on 32-bit machines, if you do the more obvious
75202** comparison of "r1==(double)i" you sometimes get an answer of false even
75203** though the r1 and (double)i values are bit-for-bit the same.
75204*/
75205SQLITE_PRIVATEstatic int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
75206 double r2 = (double)i;
75207 return r1==0.0
75208 || (memcmp(&r1, &r2, sizeof(r1))==0
75209 && i >= -2251799813685248LL && i < 2251799813685248LL);
75210}
75211
75212/*
75213** Convert pMem so that it has type MEM_Real or MEM_Int.
75214** Invalidate any prior representations.
75215**
75216** Every effort is made to force the conversion, even if the input
75217** is a string that does not look completely like a number. Convert
75218** as much of the string as we can and ignore the rest.
75219*/
75220SQLITE_PRIVATEstatic int sqlite3VdbeMemNumerify(Mem *pMem){
75221 testcase( pMem->flags & MEM_Int );
75222 testcase( pMem->flags & MEM_Real );
75223 testcase( pMem->flags & MEM_IntReal );
75224 testcase( pMem->flags & MEM_Null );
75225 if( (pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001))==0 ){
75226 int rc;
75227 sqlite3_int64 ix;
75228 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 )((void) (0));
75229 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
75230 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
75231 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
75232 || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r))
75233 ){
75234 pMem->u.i = ix;
75235 MemSetTypeFlag(pMem, MEM_Int)((pMem)->flags = ((pMem)->flags&~(0xc1bf|0x4000))|0x0004
)
;
75236 }else{
75237 MemSetTypeFlag(pMem, MEM_Real)((pMem)->flags = ((pMem)->flags&~(0xc1bf|0x4000))|0x0008
)
;
75238 }
75239 }
75240 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 )((void) (0));
75241 pMem->flags &= ~(MEM_Str0x0002|MEM_Blob0x0010|MEM_Zero0x4000);
75242 return SQLITE_OK0;
75243}
75244
75245/*
75246** Cast the datatype of the value in pMem according to the affinity
75247** "aff". Casting is different from applying affinity in that a cast
75248** is forced. In other words, the value is converted into the desired
75249** affinity even if that results in loss of data. This routine is
75250** used (for example) to implement the SQL "cast()" operator.
75251*/
75252SQLITE_PRIVATEstatic void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
75253 if( pMem->flags & MEM_Null0x0001 ) return;
75254 switch( aff ){
75255 case SQLITE_AFF_BLOB'A': { /* Really a cast to BLOB */
75256 if( (pMem->flags & MEM_Blob0x0010)==0 ){
75257 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT'B', encoding);
75258 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed )((void) (0));
75259 if( pMem->flags & MEM_Str0x0002 ) MemSetTypeFlag(pMem, MEM_Blob)((pMem)->flags = ((pMem)->flags&~(0xc1bf|0x4000))|0x0010
)
;
75260 }else{
75261 pMem->flags &= ~(MEM_TypeMask0xc1bf&~MEM_Blob0x0010);
75262 }
75263 break;
75264 }
75265 case SQLITE_AFF_NUMERIC'C': {
75266 sqlite3VdbeMemNumerify(pMem);
75267 break;
75268 }
75269 case SQLITE_AFF_INTEGER'D': {
75270 sqlite3VdbeMemIntegerify(pMem);
75271 break;
75272 }
75273 case SQLITE_AFF_REAL'E': {
75274 sqlite3VdbeMemRealify(pMem);
75275 break;
75276 }
75277 default: {
75278 assert( aff==SQLITE_AFF_TEXT )((void) (0));
75279 assert( MEM_Str==(MEM_Blob>>3) )((void) (0));
75280 pMem->flags |= (pMem->flags&MEM_Blob0x0010)>>3;
75281 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT'B', encoding);
75282 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed )((void) (0));
75283 pMem->flags &= ~(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Blob0x0010|MEM_Zero0x4000);
75284 break;
75285 }
75286 }
75287}
75288
75289/*
75290** Initialize bulk memory to be a consistent Mem object.
75291**
75292** The minimum amount of initialization feasible is performed.
75293*/
75294SQLITE_PRIVATEstatic void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
75295 assert( (flags & ~MEM_TypeMask)==0 )((void) (0));
75296 pMem->flags = flags;
75297 pMem->db = db;
75298 pMem->szMalloc = 0;
75299}
75300
75301
75302/*
75303** Delete any previous value and set the value stored in *pMem to NULL.
75304**
75305** This routine calls the Mem.xDel destructor to dispose of values that
75306** require the destructor. But it preserves the Mem.zMalloc memory allocation.
75307** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
75308** routine to invoke the destructor and deallocates Mem.zMalloc.
75309**
75310** Use this routine to reset the Mem prior to insert a new value.
75311**
75312** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
75313*/
75314SQLITE_PRIVATEstatic void sqlite3VdbeMemSetNull(Mem *pMem){
75315 if( VdbeMemDynamic(pMem)(((pMem)->flags&(0x2000|0x0400))!=0) ){
75316 vdbeMemClearExternAndSetNull(pMem);
75317 }else{
75318 pMem->flags = MEM_Null0x0001;
75319 }
75320}
75321SQLITE_PRIVATEstatic void sqlite3ValueSetNull(sqlite3_value *p){
75322 sqlite3VdbeMemSetNull((Mem*)p);
75323}
75324
75325/*
75326** Delete any previous value and set the value to be a BLOB of length
75327** n containing all zeros.
75328*/
75329SQLITE_PRIVATEstatic void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
75330 sqlite3VdbeMemRelease(pMem);
75331 pMem->flags = MEM_Blob0x0010|MEM_Zero0x4000;
75332 pMem->n = 0;
75333 if( n<0 ) n = 0;
75334 pMem->u.nZero = n;
75335 pMem->enc = SQLITE_UTF81;
75336 pMem->z = 0;
75337}
75338
75339/*
75340** The pMem is known to contain content that needs to be destroyed prior
75341** to a value change. So invoke the destructor, then set the value to
75342** a 64-bit integer.
75343*/
75344static SQLITE_NOINLINE__attribute__((noinline)) void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
75345 sqlite3VdbeMemSetNull(pMem);
75346 pMem->u.i = val;
75347 pMem->flags = MEM_Int0x0004;
75348}
75349
75350/*
75351** Delete any previous value and set the value stored in *pMem to val,
75352** manifest type INTEGER.
75353*/
75354SQLITE_PRIVATEstatic void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
75355 if( VdbeMemDynamic(pMem)(((pMem)->flags&(0x2000|0x0400))!=0) ){
75356 vdbeReleaseAndSetInt64(pMem, val);
75357 }else{
75358 pMem->u.i = val;
75359 pMem->flags = MEM_Int0x0004;
75360 }
75361}
75362
75363/* A no-op destructor */
75364SQLITE_PRIVATEstatic void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p)(void)(p); }
75365
75366/*
75367** Set the value stored in *pMem should already be a NULL.
75368** Also store a pointer to go with it.
75369*/
75370SQLITE_PRIVATEstatic void sqlite3VdbeMemSetPointer(
75371 Mem *pMem,
75372 void *pPtr,
75373 const char *zPType,
75374 void (*xDestructor)(void*)
75375){
75376 assert( pMem->flags==MEM_Null )((void) (0));
75377 pMem->u.zPType = zPType ? zPType : "";
75378 pMem->z = pPtr;
75379 pMem->flags = MEM_Null0x0001|MEM_Dyn0x0400|MEM_Subtype0x8000|MEM_Term0x0200;
75380 pMem->eSubtype = 'p';
75381 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
75382}
75383
75384#ifndef SQLITE_OMIT_FLOATING_POINT
75385/*
75386** Delete any previous value and set the value stored in *pMem to val,
75387** manifest type REAL.
75388*/
75389SQLITE_PRIVATEstatic void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
75390 sqlite3VdbeMemSetNull(pMem);
75391 if( !sqlite3IsNaN(val) ){
75392 pMem->u.r = val;
75393 pMem->flags = MEM_Real0x0008;
75394 }
75395}
75396#endif
75397
75398#ifdef SQLITE_DEBUG
75399/*
75400** Return true if the Mem holds a RowSet object. This routine is intended
75401** for use inside of assert() statements.
75402*/
75403SQLITE_PRIVATEstatic int sqlite3VdbeMemIsRowSet(const Mem *pMem){
75404 return (pMem->flags&(MEM_Blob0x0010|MEM_Dyn0x0400))==(MEM_Blob0x0010|MEM_Dyn0x0400)
75405 && pMem->xDel==sqlite3RowSetDelete;
75406}
75407#endif
75408
75409/*
75410** Delete any previous value and set the value of pMem to be an
75411** empty boolean index.
75412**
75413** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation
75414** error occurs.
75415*/
75416SQLITE_PRIVATEstatic int sqlite3VdbeMemSetRowSet(Mem *pMem){
75417 sqlite3 *db = pMem->db;
75418 RowSet *p;
75419 assert( db!=0 )((void) (0));
75420 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
75421 sqlite3VdbeMemRelease(pMem);
75422 p = sqlite3RowSetInit(db);
75423 if( p==0 ) return SQLITE_NOMEM7;
75424 pMem->z = (char*)p;
75425 pMem->flags = MEM_Blob0x0010|MEM_Dyn0x0400;
75426 pMem->xDel = sqlite3RowSetDelete;
75427 return SQLITE_OK0;
75428}
75429
75430/*
75431** Return true if the Mem object contains a TEXT or BLOB that is
75432** too large - whose size exceeds SQLITE_MAX_LENGTH.
75433*/
75434SQLITE_PRIVATEstatic int sqlite3VdbeMemTooBig(Mem *p){
75435 assert( p->db!=0 )((void) (0));
75436 if( p->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
75437 int n = p->n;
75438 if( p->flags & MEM_Zero0x4000 ){
75439 n += p->u.nZero;
75440 }
75441 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH0];
75442 }
75443 return 0;
75444}
75445
75446#ifdef SQLITE_DEBUG
75447/*
75448** This routine prepares a memory cell for modification by breaking
75449** its link to a shallow copy and by marking any current shallow
75450** copies of this cell as invalid.
75451**
75452** This is used for testing and debugging only - to make sure shallow
75453** copies are not misused.
75454*/
75455SQLITE_PRIVATEstatic void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
75456 int i;
75457 Mem *pX;
75458 for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){
75459 if( pX->pScopyFrom==pMem ){
75460 /* If pX is marked as a shallow copy of pMem, then verify that
75461 ** no significant changes have been made to pX since the OP_SCopy.
75462 ** A significant change would indicated a missed call to this
75463 ** function for pX. Minor changes, such as adding or removing a
75464 ** dual type, are allowed, as long as the underlying value is the
75465 ** same. */
75466 u16 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
75467 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i )((void) (0));
75468 assert( (mFlags&MEM_Real)==0 || pMem->u.r==pX->u.r )((void) (0));
75469 assert( (mFlags&MEM_Str)==0 || (pMem->n==pX->n && pMem->z==pX->z) )((void) (0));
75470 assert( (mFlags&MEM_Blob)==0 || sqlite3BlobCompare(pMem,pX)==0 )((void) (0));
75471
75472 /* pMem is the register that is changing. But also mark pX as
75473 ** undefined so that we can quickly detect the shallow-copy error */
75474 pX->flags = MEM_Undefined0x0080;
75475 pX->pScopyFrom = 0;
75476 }
75477 }
75478 pMem->pScopyFrom = 0;
75479}
75480#endif /* SQLITE_DEBUG */
75481
75482
75483/*
75484** Make an shallow copy of pFrom into pTo. Prior contents of
75485** pTo are freed. The pFrom->z field is not duplicated. If
75486** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
75487** and flags gets srcType (either MEM_Ephem or MEM_Static).
75488*/
75489static SQLITE_NOINLINE__attribute__((noinline)) void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
75490 vdbeMemClearExternAndSetNull(pTo);
75491 assert( !VdbeMemDynamic(pTo) )((void) (0));
75492 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
75493}
75494SQLITE_PRIVATEstatic void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
75495 assert( !sqlite3VdbeMemIsRowSet(pFrom) )((void) (0));
75496 assert( pTo->db==pFrom->db )((void) (0));
75497 if( VdbeMemDynamic(pTo)(((pTo)->flags&(0x2000|0x0400))!=0) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
75498 memcpy(pTo, pFrom, MEMCELLSIZE__builtin_offsetof(Mem, zMalloc));
75499 if( (pFrom->flags&MEM_Static0x0800)==0 ){
75500 pTo->flags &= ~(MEM_Dyn0x0400|MEM_Static0x0800|MEM_Ephem0x1000);
75501 assert( srcType==MEM_Ephem || srcType==MEM_Static )((void) (0));
75502 pTo->flags |= srcType;
75503 }
75504}
75505
75506/*
75507** Make a full copy of pFrom into pTo. Prior contents of pTo are
75508** freed before the copy is made.
75509*/
75510SQLITE_PRIVATEstatic int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
75511 int rc = SQLITE_OK0;
75512
75513 assert( !sqlite3VdbeMemIsRowSet(pFrom) )((void) (0));
75514 if( VdbeMemDynamic(pTo)(((pTo)->flags&(0x2000|0x0400))!=0) ) vdbeMemClearExternAndSetNull(pTo);
75515 memcpy(pTo, pFrom, MEMCELLSIZE__builtin_offsetof(Mem, zMalloc));
75516 pTo->flags &= ~MEM_Dyn0x0400;
75517 if( pTo->flags&(MEM_Str0x0002|MEM_Blob0x0010) ){
75518 if( 0==(pFrom->flags&MEM_Static0x0800) ){
75519 pTo->flags |= MEM_Ephem0x1000;
75520 rc = sqlite3VdbeMemMakeWriteable(pTo);
75521 }
75522 }
75523
75524 return rc;
75525}
75526
75527/*
75528** Transfer the contents of pFrom to pTo. Any existing value in pTo is
75529** freed. If pFrom contains ephemeral data, a copy is made.
75530**
75531** pFrom contains an SQL NULL when this routine returns.
75532*/
75533SQLITE_PRIVATEstatic void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
75534 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) )((void) (0));
75535 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) )((void) (0));
75536 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db )((void) (0));
75537
75538 sqlite3VdbeMemRelease(pTo);
75539 memcpy(pTo, pFrom, sizeof(Mem));
75540 pFrom->flags = MEM_Null0x0001;
75541 pFrom->szMalloc = 0;
75542}
75543
75544/*
75545** Change the value of a Mem to be a string or a BLOB.
75546**
75547** The memory management strategy depends on the value of the xDel
75548** parameter. If the value passed is SQLITE_TRANSIENT, then the
75549** string is copied into a (possibly existing) buffer managed by the
75550** Mem structure. Otherwise, any existing buffer is freed and the
75551** pointer copied.
75552**
75553** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
75554** size limit) then no memory allocation occurs. If the string can be
75555** stored without allocating memory, then it is. If a memory allocation
75556** is required to store the string, then value of pMem is unchanged. In
75557** either case, SQLITE_TOOBIG is returned.
75558*/
75559SQLITE_PRIVATEstatic int sqlite3VdbeMemSetStr(
75560 Mem *pMem, /* Memory cell to set to string value */
75561 const char *z, /* String pointer */
75562 int n, /* Bytes in string, or negative */
75563 u8 enc, /* Encoding of z. 0 for BLOBs */
75564 void (*xDel)(void*) /* Destructor function */
75565){
75566 int nByte = n; /* New value for pMem->n */
75567 int iLimit; /* Maximum allowed string or blob size */
75568 u16 flags = 0; /* New value for pMem->flags */
75569
75570 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) )((void) (0));
75571 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
75572
75573 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
75574 if( !z ){
75575 sqlite3VdbeMemSetNull(pMem);
75576 return SQLITE_OK0;
75577 }
75578
75579 if( pMem->db ){
75580 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH0];
75581 }else{
75582 iLimit = SQLITE_MAX_LENGTH1000000000;
75583 }
75584 flags = (enc==0?MEM_Blob0x0010:MEM_Str0x0002);
75585 if( nByte<0 ){
75586 assert( enc!=0 )((void) (0));
75587 if( enc==SQLITE_UTF81 ){
75588 nByte = 0x7fffffff & (int)strlen(z);
75589 }else{
75590 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
75591 }
75592 flags |= MEM_Term0x0200;
75593 }
75594
75595 /* The following block sets the new values of Mem.z and Mem.xDel. It
75596 ** also sets a flag in local variable "flags" to indicate the memory
75597 ** management (one of MEM_Dyn or MEM_Static).
75598 */
75599 if( xDel==SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
75600 u32 nAlloc = nByte;
75601 if( flags&MEM_Term0x0200 ){
75602 nAlloc += (enc==SQLITE_UTF81?1:2);
75603 }
75604 if( nByte>iLimit ){
75605 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG18);
75606 }
75607 testcase( nAlloc==0 );
75608 testcase( nAlloc==31 );
75609 testcase( nAlloc==32 );
75610 if( sqlite3VdbeMemClearAndResize(pMem, (int)MAX(nAlloc,32)((nAlloc)>(32)?(nAlloc):(32))) ){
75611 return SQLITE_NOMEM_BKPT7;
75612 }
75613 memcpy(pMem->z, z, nAlloc);
75614 }else{
75615 sqlite3VdbeMemRelease(pMem);
75616 pMem->z = (char *)z;
75617 if( xDel==SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3MallocSize) ){
75618 pMem->zMalloc = pMem->z;
75619 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
75620 }else{
75621 pMem->xDel = xDel;
75622 flags |= ((xDel==SQLITE_STATIC((sqlite3_destructor_type)0))?MEM_Static0x0800:MEM_Dyn0x0400);
75623 }
75624 }
75625
75626 pMem->n = nByte;
75627 pMem->flags = flags;
75628 pMem->enc = (enc==0 ? SQLITE_UTF81 : enc);
75629
75630#ifndef SQLITE_OMIT_UTF16
75631 if( pMem->enc!=SQLITE_UTF81 && sqlite3VdbeMemHandleBom(pMem) ){
75632 return SQLITE_NOMEM_BKPT7;
75633 }
75634#endif
75635
75636 if( nByte>iLimit ){
75637 return SQLITE_TOOBIG18;
75638 }
75639
75640 return SQLITE_OK0;
75641}
75642
75643/*
75644** Move data out of a btree key or data field and into a Mem structure.
75645** The data is payload from the entry that pCur is currently pointing
75646** to. offset and amt determine what portion of the data or key to retrieve.
75647** The result is written into the pMem element.
75648**
75649** The pMem object must have been initialized. This routine will use
75650** pMem->zMalloc to hold the content from the btree, if possible. New
75651** pMem->zMalloc space will be allocated if necessary. The calling routine
75652** is responsible for making sure that the pMem object is eventually
75653** destroyed.
75654**
75655** If this routine fails for any reason (malloc returns NULL or unable
75656** to read from the disk) then the pMem is left in an inconsistent state.
75657*/
75658static SQLITE_NOINLINE__attribute__((noinline)) int vdbeMemFromBtreeResize(
75659 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
75660 u32 offset, /* Offset from the start of data to return bytes from. */
75661 u32 amt, /* Number of bytes to return. */
75662 Mem *pMem /* OUT: Return data in this Mem structure. */
75663){
75664 int rc;
75665 pMem->flags = MEM_Null0x0001;
75666 if( sqlite3BtreeMaxRecordSize(pCur)<offset+amt ){
75667 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(75667);
75668 }
75669 if( SQLITE_OK0==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+1)) ){
75670 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
75671 if( rc==SQLITE_OK0 ){
75672 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
75673 pMem->flags = MEM_Blob0x0010;
75674 pMem->n = (int)amt;
75675 }else{
75676 sqlite3VdbeMemRelease(pMem);
75677 }
75678 }
75679 return rc;
75680}
75681SQLITE_PRIVATEstatic int sqlite3VdbeMemFromBtree(
75682 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
75683 u32 offset, /* Offset from the start of data to return bytes from. */
75684 u32 amt, /* Number of bytes to return. */
75685 Mem *pMem /* OUT: Return data in this Mem structure. */
75686){
75687 char *zData; /* Data from the btree layer */
75688 u32 available = 0; /* Number of bytes available on the local btree page */
75689 int rc = SQLITE_OK0; /* Return code */
75690
75691 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
75692 assert( !VdbeMemDynamic(pMem) )((void) (0));
75693
75694 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
75695 ** that both the BtShared and database handle mutexes are held. */
75696 assert( !sqlite3VdbeMemIsRowSet(pMem) )((void) (0));
75697 zData = (char *)sqlite3BtreePayloadFetch(pCur, &available);
75698 assert( zData!=0 )((void) (0));
75699
75700 if( offset+amt<=available ){
75701 pMem->z = &zData[offset];
75702 pMem->flags = MEM_Blob0x0010|MEM_Ephem0x1000;
75703 pMem->n = (int)amt;
75704 }else{
75705 rc = vdbeMemFromBtreeResize(pCur, offset, amt, pMem);
75706 }
75707
75708 return rc;
75709}
75710
75711/*
75712** The pVal argument is known to be a value other than NULL.
75713** Convert it into a string with encoding enc and return a pointer
75714** to a zero-terminated version of that string.
75715*/
75716static SQLITE_NOINLINE__attribute__((noinline)) const void *valueToText(sqlite3_value* pVal, u8 enc){
75717 assert( pVal!=0 )((void) (0));
75718 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) )((void) (0));
75719 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) )((void) (0));
75720 assert( !sqlite3VdbeMemIsRowSet(pVal) )((void) (0));
75721 assert( (pVal->flags & (MEM_Null))==0 )((void) (0));
75722 if( pVal->flags & (MEM_Blob0x0010|MEM_Str0x0002) ){
75723 if( ExpandBlob(pVal)(((pVal)->flags&0x4000)?sqlite3VdbeMemExpandBlob(pVal)
:0)
) return 0;
75724 pVal->flags |= MEM_Str0x0002;
75725 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED8) ){
75726 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED8);
75727 }
75728 if( (enc & SQLITE_UTF16_ALIGNED8)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)((int)(long int)(pVal->z))) ){
75729 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 )((void) (0));
75730 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK0 ){
75731 return 0;
75732 }
75733 }
75734 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
75735 }else{
75736 sqlite3VdbeMemStringify(pVal, enc, 0);
75737 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) )((void) (0));
75738 }
75739 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0((void) (0))
75740 || pVal->db->mallocFailed )((void) (0));
75741 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED8) ){
75742 assert( sqlite3VdbeMemValidStrRep(pVal) )((void) (0));
75743 return pVal->z;
75744 }else{
75745 return 0;
75746 }
75747}
75748
75749/* This function is only available internally, it is not part of the
75750** external API. It works in a similar way to sqlite3_value_text(),
75751** except the data returned is in the encoding specified by the second
75752** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
75753** SQLITE_UTF8.
75754**
75755** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
75756** If that is the case, then the result must be aligned on an even byte
75757** boundary.
75758*/
75759SQLITE_PRIVATEstatic const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
75760 if( !pVal ) return 0;
75761 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) )((void) (0));
75762 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) )((void) (0));
75763 assert( !sqlite3VdbeMemIsRowSet(pVal) )((void) (0));
75764 if( (pVal->flags&(MEM_Str0x0002|MEM_Term0x0200))==(MEM_Str0x0002|MEM_Term0x0200) && pVal->enc==enc ){
75765 assert( sqlite3VdbeMemValidStrRep(pVal) )((void) (0));
75766 return pVal->z;
75767 }
75768 if( pVal->flags&MEM_Null0x0001 ){
75769 return 0;
75770 }
75771 return valueToText(pVal, enc);
75772}
75773
75774/*
75775** Create a new sqlite3_value object.
75776*/
75777SQLITE_PRIVATEstatic sqlite3_value *sqlite3ValueNew(sqlite3 *db){
75778 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
75779 if( p ){
75780 p->flags = MEM_Null0x0001;
75781 p->db = db;
75782 }
75783 return p;
75784}
75785
75786/*
75787** Context object passed by sqlite3Stat4ProbeSetValue() through to
75788** valueNew(). See comments above valueNew() for details.
75789*/
75790struct ValueNewStat4Ctx {
75791 Parse *pParse;
75792 Index *pIdx;
75793 UnpackedRecord **ppRec;
75794 int iVal;
75795};
75796
75797/*
75798** Allocate and return a pointer to a new sqlite3_value object. If
75799** the second argument to this function is NULL, the object is allocated
75800** by calling sqlite3ValueNew().
75801**
75802** Otherwise, if the second argument is non-zero, then this function is
75803** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
75804** already been allocated, allocate the UnpackedRecord structure that
75805** that function will return to its caller here. Then return a pointer to
75806** an sqlite3_value within the UnpackedRecord.a[] array.
75807*/
75808static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
75809#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75810 if( p ){
75811 UnpackedRecord *pRec = p->ppRec[0];
75812
75813 if( pRec==0 ){
75814 Index *pIdx = p->pIdx; /* Index being probed */
75815 int nByte; /* Bytes of space to allocate */
75816 int i; /* Counter variable */
75817 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
75818
75819 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7);
75820 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
75821 if( pRec ){
75822 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
75823 if( pRec->pKeyInfo ){
75824 assert( pRec->pKeyInfo->nAllField==nCol )((void) (0));
75825 assert( pRec->pKeyInfo->enc==ENC(db) )((void) (0));
75826 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7));
75827 for(i=0; i<nCol; i++){
75828 pRec->aMem[i].flags = MEM_Null0x0001;
75829 pRec->aMem[i].db = db;
75830 }
75831 }else{
75832 sqlite3DbFreeNN(db, pRec);
75833 pRec = 0;
75834 }
75835 }
75836 if( pRec==0 ) return 0;
75837 p->ppRec[0] = pRec;
75838 }
75839
75840 pRec->nField = p->iVal+1;
75841 return &pRec->aMem[p->iVal];
75842 }
75843#else
75844 UNUSED_PARAMETER(p)(void)(p);
75845#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
75846 return sqlite3ValueNew(db);
75847}
75848
75849/*
75850** The expression object indicated by the second argument is guaranteed
75851** to be a scalar SQL function. If
75852**
75853** * all function arguments are SQL literals,
75854** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
75855** * the SQLITE_FUNC_NEEDCOLL function flag is not set,
75856**
75857** then this routine attempts to invoke the SQL function. Assuming no
75858** error occurs, output parameter (*ppVal) is set to point to a value
75859** object containing the result before returning SQLITE_OK.
75860**
75861** Affinity aff is applied to the result of the function before returning.
75862** If the result is a text value, the sqlite3_value object uses encoding
75863** enc.
75864**
75865** If the conditions above are not met, this function returns SQLITE_OK
75866** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
75867** NULL and an SQLite error code returned.
75868*/
75869#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
75870static int valueFromFunction(0
75871 sqlite3 *db, /* The database connection */0
75872 Expr *p, /* The expression to evaluate */0
75873 u8 enc, /* Encoding to use */0
75874 u8 aff, /* Affinity to use */0
75875 sqlite3_value **ppVal, /* Write the new value here */0
75876 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */0
75877)0{
75878 sqlite3_context ctx; /* Context object for function invocation */
75879 sqlite3_value **apVal = 0; /* Function arguments */
75880 int nVal = 0; /* Size of apVal[] array */
75881 FuncDef *pFunc = 0; /* Function definition */
75882 sqlite3_value *pVal = 0; /* New value */
75883 int rc = SQLITE_OK0; /* Return code */
75884 ExprList *pList = 0; /* Function arguments */
75885 int i; /* Iterator variable */
75886
75887 assert( pCtx!=0 )((void) (0));
75888 assert( (p->flags & EP_TokenOnly)==0 )((void) (0));
75889 pList = p->x.pList;
75890 if( pList ) nVal = pList->nExpr;
75891 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
75892 assert( pFunc )((void) (0));
75893 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000))==0
75894 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)
75895 ){
75896 return SQLITE_OK0;
75897 }
75898
75899 if( pList ){
75900 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
75901 if( apVal==0 ){
75902 rc = SQLITE_NOMEM_BKPT7;
75903 goto value_from_function_out;
75904 }
75905 for(i=0; i<nVal; i++){
75906 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
75907 if( apVal[i]==0 || rc!=SQLITE_OK0 ) goto value_from_function_out;
75908 }
75909 }
75910
75911 pVal = valueNew(db, pCtx);
75912 if( pVal==0 ){
75913 rc = SQLITE_NOMEM_BKPT7;
75914 goto value_from_function_out;
75915 }
75916
75917 assert( pCtx->pParse->rc==SQLITE_OK )((void) (0));
75918 memset(&ctx, 0, sizeof(ctx));
75919 ctx.pOut = pVal;
75920 ctx.pFunc = pFunc;
75921 pFunc->xSFunc(&ctx, nVal, apVal);
75922 if( ctx.isError ){
75923 rc = ctx.isError;
75924 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
75925 }else{
75926 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF81);
75927 assert( rc==SQLITE_OK )((void) (0));
75928 rc = sqlite3VdbeChangeEncoding(pVal, enc);
75929 if( rc==SQLITE_OK0 && sqlite3VdbeMemTooBig(pVal) ){
75930 rc = SQLITE_TOOBIG18;
75931 pCtx->pParse->nErr++;
75932 }
75933 }
75934 pCtx->pParse->rc = rc;
75935
75936 value_from_function_out:
75937 if( rc!=SQLITE_OK0 ){
75938 pVal = 0;
75939 }
75940 if( apVal ){
75941 for(i=0; i<nVal; i++){
75942 sqlite3ValueFree(apVal[i]);
75943 }
75944 sqlite3DbFreeNN(db, apVal);
75945 }
75946
75947 *ppVal = pVal;
75948 return rc;
75949}
75950#else
75951# define valueFromFunction(a,b,c,d,e,f)0 SQLITE_OK0
75952#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
75953
75954/*
75955** Extract a value from the supplied expression in the manner described
75956** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
75957** using valueNew().
75958**
75959** If pCtx is NULL and an error occurs after the sqlite3_value object
75960** has been allocated, it is freed before returning. Or, if pCtx is not
75961** NULL, it is assumed that the caller will free any allocated object
75962** in all cases.
75963*/
75964static int valueFromExpr(
75965 sqlite3 *db, /* The database connection */
75966 Expr *pExpr, /* The expression to evaluate */
75967 u8 enc, /* Encoding to use */
75968 u8 affinity, /* Affinity to use */
75969 sqlite3_value **ppVal, /* Write the new value here */
75970 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
75971){
75972 int op;
75973 char *zVal = 0;
75974 sqlite3_value *pVal = 0;
75975 int negInt = 1;
75976 const char *zNeg = "";
75977 int rc = SQLITE_OK0;
75978
75979 assert( pExpr!=0 )((void) (0));
75980 while( (op = pExpr->op)==TK_UPLUS166 || op==TK_SPAN173 ) pExpr = pExpr->pLeft;
75981#if defined(SQLITE_ENABLE_STAT3_OR_STAT4)
75982 if( op==TK_REGISTER168 ) op = pExpr->op2;
75983#else
75984 if( NEVER(op==TK_REGISTER)(op==168) ) op = pExpr->op2;
75985#endif
75986
75987 /* Compressed expressions only appear when parsing the DEFAULT clause
75988 ** on a table column definition, and hence only when pCtx==0. This
75989 ** check ensures that an EP_TokenOnly expression is never passed down
75990 ** into valueFromFunction(). */
75991 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 )((void) (0));
75992
75993 if( op==TK_CAST36 ){
75994 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
75995 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
75996 testcase( rc!=SQLITE_OK );
75997 if( *ppVal ){
75998 sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF81);
75999 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF81);
76000 }
76001 return rc;
76002 }
76003
76004 /* Handle negative integers in a single step. This is needed in the
76005 ** case when the value is -9223372036854775808.
76006 */
76007 if( op==TK_UMINUS165
76008 && (pExpr->pLeft->op==TK_INTEGER147 || pExpr->pLeft->op==TK_FLOAT145) ){
76009 pExpr = pExpr->pLeft;
76010 op = pExpr->op;
76011 negInt = -1;
76012 zNeg = "-";
76013 }
76014
76015 if( op==TK_STRING110 || op==TK_FLOAT145 || op==TK_INTEGER147 ){
76016 pVal = valueNew(db, pCtx);
76017 if( pVal==0 ) goto no_mem;
76018 if( ExprHasProperty(pExpr, EP_IntValue)(((pExpr)->flags&(0x000400))!=0) ){
76019 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
76020 }else{
76021 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
76022 if( zVal==0 ) goto no_mem;
76023 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF81, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3MallocSize));
76024 }
76025 if( (op==TK_INTEGER147 || op==TK_FLOAT145 ) && affinity==SQLITE_AFF_BLOB'A' ){
76026 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC'C', SQLITE_UTF81);
76027 }else{
76028 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF81);
76029 }
76030 assert( (pVal->flags & MEM_IntReal)==0 )((void) (0));
76031 if( pVal->flags & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008) ){
76032 testcase( pVal->flags & MEM_Int );
76033 testcase( pVal->flags & MEM_Real );
76034 pVal->flags &= ~MEM_Str0x0002;
76035 }
76036 if( enc!=SQLITE_UTF81 ){
76037 rc = sqlite3VdbeChangeEncoding(pVal, enc);
76038 }
76039 }else if( op==TK_UMINUS165 ) {
76040 /* This branch happens for multiple negative signs. Ex: -(-5) */
76041 if( SQLITE_OK0==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
76042 && pVal!=0
76043 ){
76044 sqlite3VdbeMemNumerify(pVal);
76045 if( pVal->flags & MEM_Real0x0008 ){
76046 pVal->u.r = -pVal->u.r;
76047 }else if( pVal->u.i==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
76048 pVal->u.r = -(double)SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
76049 MemSetTypeFlag(pVal, MEM_Real)((pVal)->flags = ((pVal)->flags&~(0xc1bf|0x4000))|0x0008
)
;
76050 }else{
76051 pVal->u.i = -pVal->u.i;
76052 }
76053 sqlite3ValueApplyAffinity(pVal, affinity, enc);
76054 }
76055 }else if( op==TK_NULL114 ){
76056 pVal = valueNew(db, pCtx);
76057 if( pVal==0 ) goto no_mem;
76058 sqlite3VdbeMemSetNull(pVal);
76059 }
76060#ifndef SQLITE_OMIT_BLOB_LITERAL
76061 else if( op==TK_BLOB146 ){
76062 int nVal;
76063 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' )((void) (0));
76064 assert( pExpr->u.zToken[1]=='\'' )((void) (0));
76065 pVal = valueNew(db, pCtx);
76066 if( !pVal ) goto no_mem;
76067 zVal = &pExpr->u.zToken[2];
76068 nVal = sqlite3Strlen30(zVal)-1;
76069 assert( zVal[nVal]=='\'' )((void) (0));
76070 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
76071 0, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3MallocSize));
76072 }
76073#endif
76074#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
76075 else if( op==TK_FUNCTION161 && pCtx!=0 ){
76076 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx)0;
76077 }
76078#endif
76079 else if( op==TK_TRUEFALSE159 ){
76080 pVal = valueNew(db, pCtx);
76081 if( pVal ){
76082 pVal->flags = MEM_Int0x0004;
76083 pVal->u.i = pExpr->u.zToken[4]==0;
76084 }
76085 }
76086
76087 *ppVal = pVal;
76088 return rc;
76089
76090no_mem:
76091#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
76092 if( pCtx==0 || pCtx->pParse->nErr==0 )
76093#endif
76094 sqlite3OomFault(db);
76095 sqlite3DbFree(db, zVal);
76096 assert( *ppVal==0 )((void) (0));
76097#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
76098 if( pCtx==0 ) sqlite3ValueFree(pVal);
76099#else
76100 assert( pCtx==0 )((void) (0)); sqlite3ValueFree(pVal);
76101#endif
76102 return SQLITE_NOMEM_BKPT7;
76103}
76104
76105/*
76106** Create a new sqlite3_value object, containing the value of pExpr.
76107**
76108** This only works for very simple expressions that consist of one constant
76109** token (i.e. "5", "5.1", "'a string'"). If the expression can
76110** be converted directly into a value, then the value is allocated and
76111** a pointer written to *ppVal. The caller is responsible for deallocating
76112** the value by passing it to sqlite3ValueFree() later on. If the expression
76113** cannot be converted to a value, then *ppVal is set to NULL.
76114*/
76115SQLITE_PRIVATEstatic int sqlite3ValueFromExpr(
76116 sqlite3 *db, /* The database connection */
76117 Expr *pExpr, /* The expression to evaluate */
76118 u8 enc, /* Encoding to use */
76119 u8 affinity, /* Affinity to use */
76120 sqlite3_value **ppVal /* Write the new value here */
76121){
76122 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
76123}
76124
76125#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
76126/*
76127** The implementation of the sqlite_record() function. This function accepts
76128** a single argument of any type. The return value is a formatted database
76129** record (a blob) containing the argument value.
76130**
76131** This is used to convert the value stored in the 'sample' column of the
76132** sqlite_stat3 table to the record format SQLite uses internally.
76133*/
76134static void recordFunc(
76135 sqlite3_context *context,
76136 int argc,
76137 sqlite3_value **argv
76138){
76139 const int file_format = 1;
76140 u32 iSerial; /* Serial type */
76141 int nSerial; /* Bytes of space for iSerial as varint */
76142 u32 nVal; /* Bytes of space required for argv[0] */
76143 int nRet;
76144 sqlite3 *db;
76145 u8 *aRet;
76146
76147 UNUSED_PARAMETER( argc )(void)(argc);
76148 iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
76149 nSerial = sqlite3VarintLen(iSerial);
76150 db = sqlite3_context_db_handle(context);
76151
76152 nRet = 1 + nSerial + nVal;
76153 aRet = sqlite3DbMallocRawNN(db, nRet);
76154 if( aRet==0 ){
76155 sqlite3_result_error_nomem(context);
76156 }else{
76157 aRet[0] = nSerial+1;
76158 putVarint32(&aRet[1], iSerial)(u8)(((u32)(iSerial)<(u32)0x80)?(*(&aRet[1])=(unsigned
char)(iSerial)),1: sqlite3PutVarint((&aRet[1]),(iSerial)
))
;
76159 sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
76160 sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
76161 sqlite3DbFreeNN(db, aRet);
76162 }
76163}
76164
76165/*
76166** Register built-in functions used to help read ANALYZE data.
76167*/
76168SQLITE_PRIVATEstatic void sqlite3AnalyzeFunctions(void){
76169 static FuncDef aAnalyzeTableFuncs[] = {
76170 FUNCTION(sqlite_record, 1, 0, 0, recordFunc){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, recordFunc
, 0, 0, 0, "sqlite_record", {0} }
,
76171 };
76172 sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs)((int)(sizeof(aAnalyzeTableFuncs)/sizeof(aAnalyzeTableFuncs[0
])))
);
76173}
76174
76175/*
76176** Attempt to extract a value from pExpr and use it to construct *ppVal.
76177**
76178** If pAlloc is not NULL, then an UnpackedRecord object is created for
76179** pAlloc if one does not exist and the new value is added to the
76180** UnpackedRecord object.
76181**
76182** A value is extracted in the following cases:
76183**
76184** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
76185**
76186** * The expression is a bound variable, and this is a reprepare, or
76187**
76188** * The expression is a literal value.
76189**
76190** On success, *ppVal is made to point to the extracted value. The caller
76191** is responsible for ensuring that the value is eventually freed.
76192*/
76193static int stat4ValueFromExpr(
76194 Parse *pParse, /* Parse context */
76195 Expr *pExpr, /* The expression to extract a value from */
76196 u8 affinity, /* Affinity to use */
76197 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */
76198 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
76199){
76200 int rc = SQLITE_OK0;
76201 sqlite3_value *pVal = 0;
76202 sqlite3 *db = pParse->db;
76203
76204 /* Skip over any TK_COLLATE nodes */
76205 pExpr = sqlite3ExprSkipCollate(pExpr);
76206
76207 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE )((void) (0));
76208 if( !pExpr ){
76209 pVal = valueNew(db, pAlloc);
76210 if( pVal ){
76211 sqlite3VdbeMemSetNull((Mem*)pVal);
76212 }
76213 }else if( pExpr->op==TK_VARIABLE148 && (db->flags & SQLITE_EnableQPSG0x00800000)==0 ){
76214 Vdbe *v;
76215 int iBindVar = pExpr->iColumn;
76216 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
76217 if( (v = pParse->pReprepare)!=0 ){
76218 pVal = valueNew(db, pAlloc);
76219 if( pVal ){
76220 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
76221 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)((db)->enc));
76222 pVal->db = pParse->db;
76223 }
76224 }
76225 }else{
76226 rc = valueFromExpr(db, pExpr, ENC(db)((db)->enc), affinity, &pVal, pAlloc);
76227 }
76228
76229 assert( pVal==0 || pVal->db==db )((void) (0));
76230 *ppVal = pVal;
76231 return rc;
76232}
76233
76234/*
76235** This function is used to allocate and populate UnpackedRecord
76236** structures intended to be compared against sample index keys stored
76237** in the sqlite_stat4 table.
76238**
76239** A single call to this function populates zero or more fields of the
76240** record starting with field iVal (fields are numbered from left to
76241** right starting with 0). A single field is populated if:
76242**
76243** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
76244**
76245** * The expression is a bound variable, and this is a reprepare, or
76246**
76247** * The sqlite3ValueFromExpr() function is able to extract a value
76248** from the expression (i.e. the expression is a literal value).
76249**
76250** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
76251** vector components that match either of the two latter criteria listed
76252** above.
76253**
76254** Before any value is appended to the record, the affinity of the
76255** corresponding column within index pIdx is applied to it. Before
76256** this function returns, output parameter *pnExtract is set to the
76257** number of values appended to the record.
76258**
76259** When this function is called, *ppRec must either point to an object
76260** allocated by an earlier call to this function, or must be NULL. If it
76261** is NULL and a value can be successfully extracted, a new UnpackedRecord
76262** is allocated (and *ppRec set to point to it) before returning.
76263**
76264** Unless an error is encountered, SQLITE_OK is returned. It is not an
76265** error if a value cannot be extracted from pExpr. If an error does
76266** occur, an SQLite error code is returned.
76267*/
76268SQLITE_PRIVATEstatic int sqlite3Stat4ProbeSetValue(
76269 Parse *pParse, /* Parse context */
76270 Index *pIdx, /* Index being probed */
76271 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
76272 Expr *pExpr, /* The expression to extract a value from */
76273 int nElem, /* Maximum number of values to append */
76274 int iVal, /* Array element to populate */
76275 int *pnExtract /* OUT: Values appended to the record */
76276){
76277 int rc = SQLITE_OK0;
76278 int nExtract = 0;
76279
76280 if( pExpr==0 || pExpr->op!=TK_SELECT131 ){
76281 int i;
76282 struct ValueNewStat4Ctx alloc;
76283
76284 alloc.pParse = pParse;
76285 alloc.pIdx = pIdx;
76286 alloc.ppRec = ppRec;
76287
76288 for(i=0; i<nElem; i++){
76289 sqlite3_value *pVal = 0;
76290 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
76291 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
76292 alloc.iVal = iVal+i;
76293 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
76294 if( !pVal ) break;
76295 nExtract++;
76296 }
76297 }
76298
76299 *pnExtract = nExtract;
76300 return rc;
76301}
76302
76303/*
76304** Attempt to extract a value from expression pExpr using the methods
76305** as described for sqlite3Stat4ProbeSetValue() above.
76306**
76307** If successful, set *ppVal to point to a new value object and return
76308** SQLITE_OK. If no value can be extracted, but no other error occurs
76309** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
76310** does occur, return an SQLite error code. The final value of *ppVal
76311** is undefined in this case.
76312*/
76313SQLITE_PRIVATEstatic int sqlite3Stat4ValueFromExpr(
76314 Parse *pParse, /* Parse context */
76315 Expr *pExpr, /* The expression to extract a value from */
76316 u8 affinity, /* Affinity to use */
76317 sqlite3_value **ppVal /* OUT: New value object (or NULL) */
76318){
76319 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
76320}
76321
76322/*
76323** Extract the iCol-th column from the nRec-byte record in pRec. Write
76324** the column value into *ppVal. If *ppVal is initially NULL then a new
76325** sqlite3_value object is allocated.
76326**
76327** If *ppVal is initially NULL then the caller is responsible for
76328** ensuring that the value written into *ppVal is eventually freed.
76329*/
76330SQLITE_PRIVATEstatic int sqlite3Stat4Column(
76331 sqlite3 *db, /* Database handle */
76332 const void *pRec, /* Pointer to buffer containing record */
76333 int nRec, /* Size of buffer pRec in bytes */
76334 int iCol, /* Column to extract */
76335 sqlite3_value **ppVal /* OUT: Extracted value */
76336){
76337 u32 t = 0; /* a column type code */
76338 int nHdr; /* Size of the header in the record */
76339 int iHdr; /* Next unread header byte */
76340 int iField; /* Next unread data byte */
76341 int szField = 0; /* Size of the current data field */
76342 int i; /* Column index */
76343 u8 *a = (u8*)pRec; /* Typecast byte array */
76344 Mem *pMem = *ppVal; /* Write result into this Mem object */
76345
76346 assert( iCol>0 )((void) (0));
76347 iHdr = getVarint32(a, nHdr)(u8)((*(a)<(u8)0x80)?((nHdr)=(u32)*(a)),1:sqlite3GetVarint32
((a),(u32 *)&(nHdr)))
;
76348 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76348);
76349 iField = nHdr;
76350 for(i=0; i<=iCol; i++){
76351 iHdr += getVarint32(&a[iHdr], t)(u8)((*(&a[iHdr])<(u8)0x80)?((t)=(u32)*(&a[iHdr]))
,1:sqlite3GetVarint32((&a[iHdr]),(u32 *)&(t)))
;
76352 testcase( iHdr==nHdr );
76353 testcase( iHdr==nHdr+1 );
76354 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76354);
76355 szField = sqlite3VdbeSerialTypeLen(t);
76356 iField += szField;
76357 }
76358 testcase( iField==nRec );
76359 testcase( iField==nRec+1 );
76360 if( iField>nRec ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(76360);
76361 if( pMem==0 ){
76362 pMem = *ppVal = sqlite3ValueNew(db);
76363 if( pMem==0 ) return SQLITE_NOMEM_BKPT7;
76364 }
76365 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
76366 pMem->enc = ENC(db)((db)->enc);
76367 return SQLITE_OK0;
76368}
76369
76370/*
76371** Unless it is NULL, the argument must be an UnpackedRecord object returned
76372** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
76373** the object.
76374*/
76375SQLITE_PRIVATEstatic void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
76376 if( pRec ){
76377 int i;
76378 int nCol = pRec->pKeyInfo->nAllField;
76379 Mem *aMem = pRec->aMem;
76380 sqlite3 *db = aMem[0].db;
76381 for(i=0; i<nCol; i++){
76382 sqlite3VdbeMemRelease(&aMem[i]);
76383 }
76384 sqlite3KeyInfoUnref(pRec->pKeyInfo);
76385 sqlite3DbFreeNN(db, pRec);
76386 }
76387}
76388#endif /* ifdef SQLITE_ENABLE_STAT4 */
76389
76390/*
76391** Change the string value of an sqlite3_value object
76392*/
76393SQLITE_PRIVATEstatic void sqlite3ValueSetStr(
76394 sqlite3_value *v, /* Value to be set */
76395 int n, /* Length of string z */
76396 const void *z, /* Text of the new string */
76397 u8 enc, /* Encoding to use */
76398 void (*xDel)(void*) /* Destructor for the string */
76399){
76400 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
76401}
76402
76403/*
76404** Free an sqlite3_value object
76405*/
76406SQLITE_PRIVATEstatic void sqlite3ValueFree(sqlite3_value *v){
76407 if( !v ) return;
76408 sqlite3VdbeMemRelease((Mem *)v);
76409 sqlite3DbFreeNN(((Mem*)v)->db, v);
76410}
76411
76412/*
76413** The sqlite3ValueBytes() routine returns the number of bytes in the
76414** sqlite3_value object assuming that it uses the encoding "enc".
76415** The valueBytes() routine is a helper function.
76416*/
76417static SQLITE_NOINLINE__attribute__((noinline)) int valueBytes(sqlite3_value *pVal, u8 enc){
76418 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
76419}
76420SQLITE_PRIVATEstatic int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
76421 Mem *p = (Mem*)pVal;
76422 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 )((void) (0));
76423 if( (p->flags & MEM_Str0x0002)!=0 && pVal->enc==enc ){
76424 return p->n;
76425 }
76426 if( (p->flags & MEM_Blob0x0010)!=0 ){
76427 if( p->flags & MEM_Zero0x4000 ){
76428 return p->n + p->u.nZero;
76429 }else{
76430 return p->n;
76431 }
76432 }
76433 if( p->flags & MEM_Null0x0001 ) return 0;
76434 return valueBytes(pVal, enc);
76435}
76436
76437/************** End of vdbemem.c *********************************************/
76438/************** Begin file vdbeaux.c *****************************************/
76439/*
76440** 2003 September 6
76441**
76442** The author disclaims copyright to this source code. In place of
76443** a legal notice, here is a blessing:
76444**
76445** May you do good and not evil.
76446** May you find forgiveness for yourself and forgive others.
76447** May you share freely, never taking more than you give.
76448**
76449*************************************************************************
76450** This file contains code used for creating, destroying, and populating
76451** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
76452*/
76453/* #include "sqliteInt.h" */
76454/* #include "vdbeInt.h" */
76455
76456/*
76457** Create a new virtual database engine.
76458*/
76459SQLITE_PRIVATEstatic Vdbe *sqlite3VdbeCreate(Parse *pParse){
76460 sqlite3 *db = pParse->db;
76461 Vdbe *p;
76462 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) );
76463 if( p==0 ) return 0;
76464 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp)__builtin_offsetof(Vdbe, aOp));
76465 p->db = db;
76466 if( db->pVdbe ){
76467 db->pVdbe->pPrev = p;
76468 }
76469 p->pNext = db->pVdbe;
76470 p->pPrev = 0;
76471 db->pVdbe = p;
76472 p->magic = VDBE_MAGIC_INIT0x16bceaa5;
76473 p->pParse = pParse;
76474 pParse->pVdbe = p;
76475 assert( pParse->aLabel==0 )((void) (0));
76476 assert( pParse->nLabel==0 )((void) (0));
76477 assert( p->nOpAlloc==0 )((void) (0));
76478 assert( pParse->szOpAlloc==0 )((void) (0));
76479 sqlite3VdbeAddOp2(p, OP_Init61, 0, 1);
76480 return p;
76481}
76482
76483/*
76484** Change the error string stored in Vdbe.zErrMsg
76485*/
76486SQLITE_PRIVATEstatic void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){
76487 va_list ap;
76488 sqlite3DbFree(p->db, p->zErrMsg);
76489 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
76490 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
76491 va_end(ap)__builtin_va_end(ap);
76492}
76493
76494/*
76495** Remember the SQL string for a prepared statement.
76496*/
76497SQLITE_PRIVATEstatic void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){
76498 if( p==0 ) return;
76499 p->prepFlags = prepFlags;
76500 if( (prepFlags & SQLITE_PREPARE_SAVESQL0x80)==0 ){
76501 p->expmask = 0;
76502 }
76503 assert( p->zSql==0 )((void) (0));
76504 p->zSql = sqlite3DbStrNDup(p->db, z, n);
76505}
76506
76507#ifdef SQLITE_ENABLE_NORMALIZE
76508/*
76509** Add a new element to the Vdbe->pDblStr list.
76510*/
76511SQLITE_PRIVATEstatic void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){
76512 if( p ){
76513 int n = sqlite3Strlen30(z);
76514 DblquoteStr *pStr = sqlite3DbMallocRawNN(db,
76515 sizeof(*pStr)+n+1-sizeof(pStr->z));
76516 if( pStr ){
76517 pStr->pNextStr = p->pDblStr;
76518 p->pDblStr = pStr;
76519 memcpy(pStr->z, z, n+1);
76520 }
76521 }
76522}
76523#endif
76524
76525#ifdef SQLITE_ENABLE_NORMALIZE
76526/*
76527** zId of length nId is a double-quoted identifier. Check to see if
76528** that identifier is really used as a string literal.
76529*/
76530SQLITE_PRIVATEstatic int sqlite3VdbeUsesDoubleQuotedString(
76531 Vdbe *pVdbe, /* The prepared statement */
76532 const char *zId /* The double-quoted identifier, already dequoted */
76533){
76534 DblquoteStr *pStr;
76535 assert( zId!=0 )((void) (0));
76536 if( pVdbe->pDblStr==0 ) return 0;
76537 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
76538 if( strcmp(zId, pStr->z)==0 ) return 1;
76539 }
76540 return 0;
76541}
76542#endif
76543
76544/*
76545** Swap all content between two VDBE structures.
76546*/
76547SQLITE_PRIVATEstatic void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
76548 Vdbe tmp, *pTmp;
76549 char *zTmp;
76550 assert( pA->db==pB->db )((void) (0));
76551 tmp = *pA;
76552 *pA = *pB;
76553 *pB = tmp;
76554 pTmp = pA->pNext;
76555 pA->pNext = pB->pNext;
76556 pB->pNext = pTmp;
76557 pTmp = pA->pPrev;
76558 pA->pPrev = pB->pPrev;
76559 pB->pPrev = pTmp;
76560 zTmp = pA->zSql;
76561 pA->zSql = pB->zSql;
76562 pB->zSql = zTmp;
76563#if 0
76564 zTmp = pA->zNormSql;
76565 pA->zNormSql = pB->zNormSql;
76566 pB->zNormSql = zTmp;
76567#endif
76568 pB->expmask = pA->expmask;
76569 pB->prepFlags = pA->prepFlags;
76570 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
76571 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE5]++;
76572}
76573
76574/*
76575** Resize the Vdbe.aOp array so that it is at least nOp elements larger
76576** than its current size. nOp is guaranteed to be less than or equal
76577** to 1024/sizeof(Op).
76578**
76579** If an out-of-memory error occurs while resizing the array, return
76580** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
76581** unchanged (this is so that any opcodes already allocated can be
76582** correctly deallocated along with the rest of the Vdbe).
76583*/
76584static int growOpArray(Vdbe *v, int nOp){
76585 VdbeOp *pNew;
76586 Parse *p = v->pParse;
76587
76588 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
76589 ** more frequent reallocs and hence provide more opportunities for
76590 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
76591 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
76592 ** by the minimum* amount required until the size reaches 512. Normal
76593 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
76594 ** size of the op array or add 1KB of space, whichever is smaller. */
76595#ifdef SQLITE_TEST_REALLOC_STRESS
76596 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
76597 : (sqlite3_int64)v->nOpAlloc+nOp);
76598#else
76599 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
76600 : (sqlite3_int64)(1024/sizeof(Op)));
76601 UNUSED_PARAMETER(nOp)(void)(nOp);
76602#endif
76603
76604 /* Ensure that the size of a VDBE does not grow too large */
76605 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP5] ){
76606 sqlite3OomFault(p->db);
76607 return SQLITE_NOMEM7;
76608 }
76609
76610 assert( nOp<=(1024/sizeof(Op)) )((void) (0));
76611 assert( nNew>=(v->nOpAlloc+nOp) )((void) (0));
76612 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
76613 if( pNew ){
76614 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
76615 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
76616 v->aOp = pNew;
76617 }
76618 return (pNew ? SQLITE_OK0 : SQLITE_NOMEM_BKPT7);
76619}
76620
76621#ifdef SQLITE_DEBUG
76622/* This routine is just a convenient place to set a breakpoint that will
76623** fire after each opcode is inserted and displayed using
76624** "PRAGMA vdbe_addoptrace=on".
76625*/
76626static void test_addop_breakpoint(void){
76627 static int n = 0;
76628 n++;
76629}
76630#endif
76631
76632/*
76633** Add a new instruction to the list of instructions current in the
76634** VDBE. Return the address of the new instruction.
76635**
76636** Parameters:
76637**
76638** p Pointer to the VDBE
76639**
76640** op The opcode for this instruction
76641**
76642** p1, p2, p3 Operands
76643**
76644** Use the sqlite3VdbeResolveLabel() function to fix an address and
76645** the sqlite3VdbeChangeP4() function to change the value of the P4
76646** operand.
76647*/
76648static SQLITE_NOINLINE__attribute__((noinline)) int growOp3(Vdbe *p, int op, int p1, int p2, int p3){
76649 assert( p->nOpAlloc<=p->nOp )((void) (0));
76650 if( growOpArray(p, 1) ) return 1;
76651 assert( p->nOpAlloc>p->nOp )((void) (0));
76652 return sqlite3VdbeAddOp3(p, op, p1, p2, p3);
76653}
76654SQLITE_PRIVATEstatic int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
76655 int i;
76656 VdbeOp *pOp;
76657
76658 i = p->nOp;
76659 assert( p->magic==VDBE_MAGIC_INIT )((void) (0));
76660 assert( op>=0 && op<0xff )((void) (0));
76661 if( p->nOpAlloc<=i ){
76662 return growOp3(p, op, p1, p2, p3);
76663 }
76664 p->nOp++;
76665 pOp = &p->aOp[i];
76666 pOp->opcode = (u8)op;
76667 pOp->p5 = 0;
76668 pOp->p1 = p1;
76669 pOp->p2 = p2;
76670 pOp->p3 = p3;
76671 pOp->p4.p = 0;
76672 pOp->p4type = P4_NOTUSED0;
76673#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
76674 pOp->zComment = 0;
76675#endif
76676#ifdef SQLITE_DEBUG
76677 if( p->db->flags & SQLITE_VdbeAddopTrace ){
76678 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
76679 test_addop_breakpoint();
76680 }
76681#endif
76682#ifdef VDBE_PROFILE
76683 pOp->cycles = 0;
76684 pOp->cnt = 0;
76685#endif
76686#ifdef SQLITE_VDBE_COVERAGE
76687 pOp->iSrcLine = 0;
76688#endif
76689 return i;
76690}
76691SQLITE_PRIVATEstatic int sqlite3VdbeAddOp0(Vdbe *p, int op){
76692 return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
76693}
76694SQLITE_PRIVATEstatic int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
76695 return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
76696}
76697SQLITE_PRIVATEstatic int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
76698 return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
76699}
76700
76701/* Generate code for an unconditional jump to instruction iDest
76702*/
76703SQLITE_PRIVATEstatic int sqlite3VdbeGoto(Vdbe *p, int iDest){
76704 return sqlite3VdbeAddOp3(p, OP_Goto11, 0, iDest, 0);
76705}
76706
76707/* Generate code to cause the string zStr to be loaded into
76708** register iDest
76709*/
76710SQLITE_PRIVATEstatic int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){
76711 return sqlite3VdbeAddOp4(p, OP_String8110, 0, iDest, 0, zStr, 0);
76712}
76713
76714/*
76715** Generate code that initializes multiple registers to string or integer
76716** constants. The registers begin with iDest and increase consecutively.
76717** One register is initialized for each characgter in zTypes[]. For each
76718** "s" character in zTypes[], the register is a string if the argument is
76719** not NULL, or OP_Null if the value is a null pointer. For each "i" character
76720** in zTypes[], the register is initialized to an integer.
76721**
76722** If the input string does not end with "X" then an OP_ResultRow instruction
76723** is generated for the values inserted.
76724*/
76725SQLITE_PRIVATEstatic void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){
76726 va_list ap;
76727 int i;
76728 char c;
76729 va_start(ap, zTypes)__builtin_va_start(ap, zTypes);
76730 for(i=0; (c = zTypes[i])!=0; i++){
76731 if( c=='s' ){
76732 const char *z = va_arg(ap, const char*)__builtin_va_arg(ap, const char*);
76733 sqlite3VdbeAddOp4(p, z==0 ? OP_Null73 : OP_String8110, 0, iDest+i, 0, z, 0);
76734 }else if( c=='i' ){
76735 sqlite3VdbeAddOp2(p, OP_Integer70, va_arg(ap, int)__builtin_va_arg(ap, int), iDest+i);
76736 }else{
76737 goto skip_op_resultrow;
76738 }
76739 }
76740 sqlite3VdbeAddOp2(p, OP_ResultRow81, iDest, i);
76741skip_op_resultrow:
76742 va_end(ap)__builtin_va_end(ap);
76743}
76744
76745/*
76746** Add an opcode that includes the p4 value as a pointer.
76747*/
76748SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4(
76749 Vdbe *p, /* Add the opcode to this VM */
76750 int op, /* The new opcode */
76751 int p1, /* The P1 operand */
76752 int p2, /* The P2 operand */
76753 int p3, /* The P3 operand */
76754 const char *zP4, /* The P4 operand */
76755 int p4type /* P4 operand type */
76756){
76757 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
76758 sqlite3VdbeChangeP4(p, addr, zP4, p4type);
76759 return addr;
76760}
76761
76762/*
76763** Add an opcode that includes the p4 value with a P4_INT64 or
76764** P4_REAL type.
76765*/
76766SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Dup8(
76767 Vdbe *p, /* Add the opcode to this VM */
76768 int op, /* The new opcode */
76769 int p1, /* The P1 operand */
76770 int p2, /* The P2 operand */
76771 int p3, /* The P3 operand */
76772 const u8 *zP4, /* The P4 operand */
76773 int p4type /* P4 operand type */
76774){
76775 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
76776 if( p4copy ) memcpy(p4copy, zP4, 8);
76777 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
76778}
76779
76780#ifndef SQLITE_OMIT_EXPLAIN
76781/*
76782** Return the address of the current EXPLAIN QUERY PLAN baseline.
76783** 0 means "none".
76784*/
76785SQLITE_PRIVATEstatic int sqlite3VdbeExplainParent(Parse *pParse){
76786 VdbeOp *pOp;
76787 if( pParse->addrExplain==0 ) return 0;
76788 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
76789 return pOp->p2;
76790}
76791
76792/*
76793** Set a debugger breakpoint on the following routine in order to
76794** monitor the EXPLAIN QUERY PLAN code generation.
76795*/
76796#if defined(SQLITE_DEBUG)
76797SQLITE_PRIVATEstatic void sqlite3ExplainBreakpoint(const char *z1, const char *z2){
76798 (void)z1;
76799 (void)z2;
76800}
76801#endif
76802
76803/*
76804** Add a new OP_ opcode.
76805**
76806** If the bPush flag is true, then make this opcode the parent for
76807** subsequent Explains until sqlite3VdbeExplainPop() is called.
76808*/
76809SQLITE_PRIVATEstatic void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){
76810#ifndef SQLITE_DEBUG
76811 /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined.
76812 ** But omit them (for performance) during production builds */
76813 if( pParse->explain==2 )
76814#endif
76815 {
76816 char *zMsg;
76817 Vdbe *v;
76818 va_list ap;
76819 int iThis;
76820 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
76821 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
76822 va_end(ap)__builtin_va_end(ap);
76823 v = pParse->pVdbe;
76824 iThis = v->nOp;
76825 sqlite3VdbeAddOp4(v, OP_Explain171, iThis, pParse->addrExplain, 0,
76826 zMsg, P4_DYNAMIC(-7));
76827 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
76828 if( bPush){
76829 pParse->addrExplain = iThis;
76830 }
76831 }
76832}
76833
76834/*
76835** Pop the EXPLAIN QUERY PLAN stack one level.
76836*/
76837SQLITE_PRIVATEstatic void sqlite3VdbeExplainPop(Parse *pParse){
76838 sqlite3ExplainBreakpoint("POP", 0);
76839 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
76840}
76841#endif /* SQLITE_OMIT_EXPLAIN */
76842
76843/*
76844** Add an OP_ParseSchema opcode. This routine is broken out from
76845** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
76846** as having been used.
76847**
76848** The zWhere string must have been obtained from sqlite3_malloc().
76849** This routine will take ownership of the allocated memory.
76850*/
76851SQLITE_PRIVATEstatic void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
76852 int j;
76853 sqlite3VdbeAddOp4(p, OP_ParseSchema141, iDb, 0, 0, zWhere, P4_DYNAMIC(-7));
76854 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
76855}
76856
76857/*
76858** Add an opcode that includes the p4 value as an integer.
76859*/
76860SQLITE_PRIVATEstatic int sqlite3VdbeAddOp4Int(
76861 Vdbe *p, /* Add the opcode to this VM */
76862 int op, /* The new opcode */
76863 int p1, /* The P1 operand */
76864 int p2, /* The P2 operand */
76865 int p3, /* The P3 operand */
76866 int p4 /* The P4 operand as an integer */
76867){
76868 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
76869 if( p->db->mallocFailed==0 ){
76870 VdbeOp *pOp = &p->aOp[addr];
76871 pOp->p4type = P4_INT32(-3);
76872 pOp->p4.i = p4;
76873 }
76874 return addr;
76875}
76876
76877/* Insert the end of a co-routine
76878*/
76879SQLITE_PRIVATEstatic void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
76880 sqlite3VdbeAddOp1(v, OP_EndCoroutine67, regYield);
76881
76882 /* Clear the temporary register cache, thereby ensuring that each
76883 ** co-routine has its own independent set of registers, because co-routines
76884 ** might expect their registers to be preserved across an OP_Yield, and
76885 ** that could cause problems if two or more co-routines are using the same
76886 ** temporary register.
76887 */
76888 v->pParse->nTempReg = 0;
76889 v->pParse->nRangeReg = 0;
76890}
76891
76892/*
76893** Create a new symbolic label for an instruction that has yet to be
76894** coded. The symbolic label is really just a negative number. The
76895** label can be used as the P2 value of an operation. Later, when
76896** the label is resolved to a specific address, the VDBE will scan
76897** through its operation list and change all values of P2 which match
76898** the label into the resolved address.
76899**
76900** The VDBE knows that a P2 value is a label because labels are
76901** always negative and P2 values are suppose to be non-negative.
76902** Hence, a negative P2 value is a label that has yet to be resolved.
76903** (Later:) This is only true for opcodes that have the OPFLG_JUMP
76904** property.
76905**
76906** Variable usage notes:
76907**
76908** Parse.aLabel[x] Stores the address that the x-th label resolves
76909** into. For testing (SQLITE_DEBUG), unresolved
76910** labels stores -1, but that is not required.
76911** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[]
76912** Parse.nLabel The *negative* of the number of labels that have
76913** been issued. The negative is stored because
76914** that gives a performance improvement over storing
76915** the equivalent positive value.
76916*/
76917SQLITE_PRIVATEstatic int sqlite3VdbeMakeLabel(Parse *pParse){
76918 return --pParse->nLabel;
76919}
76920
76921/*
76922** Resolve label "x" to be the address of the next instruction to
76923** be inserted. The parameter "x" must have been obtained from
76924** a prior call to sqlite3VdbeMakeLabel().
76925*/
76926static SQLITE_NOINLINE__attribute__((noinline)) void resizeResolveLabel(Parse *p, Vdbe *v, int j){
76927 int nNewSize = 10 - p->nLabel;
76928 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
76929 nNewSize*sizeof(p->aLabel[0]));
76930 if( p->aLabel==0 ){
76931 p->nLabelAlloc = 0;
76932 }else{
76933#ifdef SQLITE_DEBUG
76934 int i;
76935 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
76936#endif
76937 p->nLabelAlloc = nNewSize;
76938 p->aLabel[j] = v->nOp;
76939 }
76940}
76941SQLITE_PRIVATEstatic void sqlite3VdbeResolveLabel(Vdbe *v, int x){
76942 Parse *p = v->pParse;
76943 int j = ADDR(x)(~(x));
76944 assert( v->magic==VDBE_MAGIC_INIT )((void) (0));
76945 assert( j<-p->nLabel )((void) (0));
76946 assert( j>=0 )((void) (0));
76947#ifdef SQLITE_DEBUG
76948 if( p->db->flags & SQLITE_VdbeAddopTrace ){
76949 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
76950 }
76951#endif
76952 if( p->nLabelAlloc + p->nLabel < 0 ){
76953 resizeResolveLabel(p,v,j);
76954 }else{
76955 assert( p->aLabel[j]==(-1) )((void) (0)); /* Labels may only be resolved once */
76956 p->aLabel[j] = v->nOp;
76957 }
76958}
76959
76960/*
76961** Mark the VDBE as one that can only be run one time.
76962*/
76963SQLITE_PRIVATEstatic void sqlite3VdbeRunOnlyOnce(Vdbe *p){
76964 p->runOnlyOnce = 1;
76965}
76966
76967/*
76968** Mark the VDBE as one that can only be run multiple times.
76969*/
76970SQLITE_PRIVATEstatic void sqlite3VdbeReusable(Vdbe *p){
76971 p->runOnlyOnce = 0;
76972}
76973
76974#ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
76975
76976/*
76977** The following type and function are used to iterate through all opcodes
76978** in a Vdbe main program and each of the sub-programs (triggers) it may
76979** invoke directly or indirectly. It should be used as follows:
76980**
76981** Op *pOp;
76982** VdbeOpIter sIter;
76983**
76984** memset(&sIter, 0, sizeof(sIter));
76985** sIter.v = v; // v is of type Vdbe*
76986** while( (pOp = opIterNext(&sIter)) ){
76987** // Do something with pOp
76988** }
76989** sqlite3DbFree(v->db, sIter.apSub);
76990**
76991*/
76992typedef struct VdbeOpIter VdbeOpIter;
76993struct VdbeOpIter {
76994 Vdbe *v; /* Vdbe to iterate through the opcodes of */
76995 SubProgram **apSub; /* Array of subprograms */
76996 int nSub; /* Number of entries in apSub */
76997 int iAddr; /* Address of next instruction to return */
76998 int iSub; /* 0 = main program, 1 = first sub-program etc. */
76999};
77000static Op *opIterNext(VdbeOpIter *p){
77001 Vdbe *v = p->v;
77002 Op *pRet = 0;
77003 Op *aOp;
77004 int nOp;
77005
77006 if( p->iSub<=p->nSub ){
77007
77008 if( p->iSub==0 ){
77009 aOp = v->aOp;
77010 nOp = v->nOp;
77011 }else{
77012 aOp = p->apSub[p->iSub-1]->aOp;
77013 nOp = p->apSub[p->iSub-1]->nOp;
77014 }
77015 assert( p->iAddr<nOp )((void) (0));
77016
77017 pRet = &aOp[p->iAddr];
77018 p->iAddr++;
77019 if( p->iAddr==nOp ){
77020 p->iSub++;
77021 p->iAddr = 0;
77022 }
77023
77024 if( pRet->p4type==P4_SUBPROGRAM(-4) ){
77025 int nByte = (p->nSub+1)*sizeof(SubProgram*);
77026 int j;
77027 for(j=0; j<p->nSub; j++){
77028 if( p->apSub[j]==pRet->p4.pProgram ) break;
77029 }
77030 if( j==p->nSub ){
77031 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
77032 if( !p->apSub ){
77033 pRet = 0;
77034 }else{
77035 p->apSub[p->nSub++] = pRet->p4.pProgram;
77036 }
77037 }
77038 }
77039 }
77040
77041 return pRet;
77042}
77043
77044/*
77045** Check if the program stored in the VM associated with pParse may
77046** throw an ABORT exception (causing the statement, but not entire transaction
77047** to be rolled back). This condition is true if the main program or any
77048** sub-programs contains any of the following:
77049**
77050** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
77051** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
77052** * OP_Destroy
77053** * OP_VUpdate
77054** * OP_VRename
77055** * OP_FkCounter with P2==0 (immediate foreign key constraint)
77056** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine
77057** (for CREATE TABLE AS SELECT ...)
77058**
77059** Then check that the value of Parse.mayAbort is true if an
77060** ABORT may be thrown, or false otherwise. Return true if it does
77061** match, or false otherwise. This function is intended to be used as
77062** part of an assert statement in the compiler. Similar to:
77063**
77064** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
77065*/
77066SQLITE_PRIVATEstatic int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
77067 int hasAbort = 0;
77068 int hasFkCounter = 0;
77069 int hasCreateTable = 0;
77070 int hasCreateIndex = 0;
77071 int hasInitCoroutine = 0;
77072 Op *pOp;
77073 VdbeOpIter sIter;
77074 memset(&sIter, 0, sizeof(sIter));
77075 sIter.v = v;
77076
77077 while( (pOp = opIterNext(&sIter))!=0 ){
77078 int opcode = pOp->opcode;
77079 if( opcode==OP_Destroy136 || opcode==OP_VUpdate10 || opcode==OP_VRename165
77080 || opcode==OP_VDestroy162
77081 || (opcode==OP_Function063 && pOp->p4.pFunc->funcFlags&SQLITE_FUNC_INTERNAL0x00040000)
77082 || ((opcode==OP_Halt69 || opcode==OP_HaltIfNull68)
77083 && ((pOp->p1)!=SQLITE_OK0 && pOp->p2==OE_Abort2))
77084 ){
77085 hasAbort = 1;
77086 break;
77087 }
77088 if( opcode==OP_CreateBtree139 && pOp->p3==BTREE_INTKEY1 ) hasCreateTable = 1;
77089 if( mayAbort ){
77090 /* hasCreateIndex may also be set for some DELETE statements that use
77091 ** OP_Clear. So this routine may end up returning true in the case
77092 ** where a "DELETE FROM tbl" has a statement-journal but does not
77093 ** require one. This is not so bad - it is an inefficiency, not a bug. */
77094 if( opcode==OP_CreateBtree139 && pOp->p3==BTREE_BLOBKEY2 ) hasCreateIndex = 1;
77095 if( opcode==OP_Clear137 ) hasCreateIndex = 1;
77096 }
77097 if( opcode==OP_InitCoroutine13 ) hasInitCoroutine = 1;
77098#ifndef SQLITE_OMIT_FOREIGN_KEY
77099 if( opcode==OP_FkCounter150 && pOp->p1==0 && pOp->p2==1 ){
77100 hasFkCounter = 1;
77101 }
77102#endif
77103 }
77104 sqlite3DbFree(v->db, sIter.apSub);
77105
77106 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
77107 ** If malloc failed, then the while() loop above may not have iterated
77108 ** through all opcodes and hasAbort may be set incorrectly. Return
77109 ** true for this case to prevent the assert() in the callers frame
77110 ** from failing. */
77111 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
77112 || (hasCreateTable && hasInitCoroutine) || hasCreateIndex
77113 );
77114}
77115#endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
77116
77117#ifdef SQLITE_DEBUG
77118/*
77119** Increment the nWrite counter in the VDBE if the cursor is not an
77120** ephemeral cursor, or if the cursor argument is NULL.
77121*/
77122SQLITE_PRIVATEstatic void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
77123 if( pC==0
77124 || (pC->eCurType!=CURTYPE_SORTER1
77125 && pC->eCurType!=CURTYPE_PSEUDO3
77126 && !pC->isEphemeral)
77127 ){
77128 p->nWrite++;
77129 }
77130}
77131#endif
77132
77133#ifdef SQLITE_DEBUG
77134/*
77135** Assert if an Abort at this point in time might result in a corrupt
77136** database.
77137*/
77138SQLITE_PRIVATEstatic void sqlite3VdbeAssertAbortable(Vdbe *p){
77139 assert( p->nWrite==0 || p->usesStmtJournal )((void) (0));
77140}
77141#endif
77142
77143/*
77144** This routine is called after all opcodes have been inserted. It loops
77145** through all the opcodes and fixes up some details.
77146**
77147** (1) For each jump instruction with a negative P2 value (a label)
77148** resolve the P2 value to an actual address.
77149**
77150** (2) Compute the maximum number of arguments used by any SQL function
77151** and store that value in *pMaxFuncArgs.
77152**
77153** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately
77154** indicate what the prepared statement actually does.
77155**
77156** (4) Initialize the p4.xAdvance pointer on opcodes that use it.
77157**
77158** (5) Reclaim the memory allocated for storing labels.
77159**
77160** This routine will only function correctly if the mkopcodeh.tcl generator
77161** script numbers the opcodes correctly. Changes to this routine must be
77162** coordinated with changes to mkopcodeh.tcl.
77163*/
77164static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
77165 int nMaxArgs = *pMaxFuncArgs;
77166 Op *pOp;
77167 Parse *pParse = p->pParse;
77168 int *aLabel = pParse->aLabel;
77169 p->readOnly = 1;
77170 p->bIsReader = 0;
77171 pOp = &p->aOp[p->nOp-1];
77172 while(1){
77173
77174 /* Only JUMP opcodes and the short list of special opcodes in the switch
77175 ** below need to be considered. The mkopcodeh.tcl generator script groups
77176 ** all these opcodes together near the front of the opcode list. Skip
77177 ** any opcode that does not need processing by virtual of the fact that
77178 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization.
77179 */
77180 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE61 ){
77181 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
77182 ** cases from this switch! */
77183 switch( pOp->opcode ){
77184 case OP_Transaction2: {
77185 if( pOp->p2!=0 ) p->readOnly = 0;
77186 /* fall thru */
77187 }
77188 case OP_AutoCommit1:
77189 case OP_Savepoint0: {
77190 p->bIsReader = 1;
77191 break;
77192 }
77193#ifndef SQLITE_OMIT_WAL
77194 case OP_Checkpoint6:
77195#endif
77196 case OP_Vacuum8:
77197 case OP_JournalMode7: {
77198 p->readOnly = 0;
77199 p->bIsReader = 1;
77200 break;
77201 }
77202 case OP_Next5:
77203 case OP_SorterNext3: {
77204 pOp->p4.xAdvance = sqlite3BtreeNext;
77205 pOp->p4type = P4_ADVANCE(-5);
77206 /* The code generator never codes any of these opcodes as a jump
77207 ** to a label. They are always coded as a jump backwards to a
77208 ** known address */
77209 assert( pOp->p2>=0 )((void) (0));
77210 break;
77211 }
77212 case OP_Prev4: {
77213 pOp->p4.xAdvance = sqlite3BtreePrevious;
77214 pOp->p4type = P4_ADVANCE(-5);
77215 /* The code generator never codes any of these opcodes as a jump
77216 ** to a label. They are always coded as a jump backwards to a
77217 ** known address */
77218 assert( pOp->p2>=0 )((void) (0));
77219 break;
77220 }
77221#ifndef SQLITE_OMIT_VIRTUALTABLE
77222 case OP_VUpdate10: {
77223 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
77224 break;
77225 }
77226 case OP_VFilter9: {
77227 int n;
77228 assert( (pOp - p->aOp) >= 3 )((void) (0));
77229 assert( pOp[-1].opcode==OP_Integer )((void) (0));
77230 n = pOp[-1].p1;
77231 if( n>nMaxArgs ) nMaxArgs = n;
77232 /* Fall through into the default case */
77233 }
77234#endif
77235 default: {
77236 if( pOp->p2<0 ){
77237 /* The mkopcodeh.tcl script has so arranged things that the only
77238 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
77239 ** have non-negative values for P2. */
77240 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 )((void) (0));
77241 assert( ADDR(pOp->p2)<-pParse->nLabel )((void) (0));
77242 pOp->p2 = aLabel[ADDR(pOp->p2)(~(pOp->p2))];
77243 }
77244 break;
77245 }
77246 }
77247 /* The mkopcodeh.tcl script has so arranged things that the only
77248 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
77249 ** have non-negative values for P2. */
77250 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0)((void) (0));
77251 }
77252 if( pOp==p->aOp ) break;
77253 pOp--;
77254 }
77255 sqlite3DbFree(p->db, pParse->aLabel);
77256 pParse->aLabel = 0;
77257 pParse->nLabel = 0;
77258 *pMaxFuncArgs = nMaxArgs;
77259 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) )((void) (0));
77260}
77261
77262/*
77263** Return the address of the next instruction to be inserted.
77264*/
77265SQLITE_PRIVATEstatic int sqlite3VdbeCurrentAddr(Vdbe *p){
77266 assert( p->magic==VDBE_MAGIC_INIT )((void) (0));
77267 return p->nOp;
77268}
77269
77270/*
77271** Verify that at least N opcode slots are available in p without
77272** having to malloc for more space (except when compiled using
77273** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing
77274** to verify that certain calls to sqlite3VdbeAddOpList() can never
77275** fail due to a OOM fault and hence that the return value from
77276** sqlite3VdbeAddOpList() will always be non-NULL.
77277*/
77278#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
77279SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){
77280 assert( p->nOp + N <= p->nOpAlloc )((void) (0));
77281}
77282#endif
77283
77284/*
77285** Verify that the VM passed as the only argument does not contain
77286** an OP_ResultRow opcode. Fail an assert() if it does. This is used
77287** by code in pragma.c to ensure that the implementation of certain
77288** pragmas comports with the flags specified in the mkpragmatab.tcl
77289** script.
77290*/
77291#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
77292SQLITE_PRIVATEstatic void sqlite3VdbeVerifyNoResultRow(Vdbe *p){
77293 int i;
77294 for(i=0; i<p->nOp; i++){
77295 assert( p->aOp[i].opcode!=OP_ResultRow )((void) (0));
77296 }
77297}
77298#endif
77299
77300/*
77301** Generate code (a single OP_Abortable opcode) that will
77302** verify that the VDBE program can safely call Abort in the current
77303** context.
77304*/
77305#if defined(SQLITE_DEBUG)
77306SQLITE_PRIVATEstatic void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
77307 if( onError==OE_Abort2 ) sqlite3VdbeAddOp0(p, OP_Abortable172);
77308}
77309#endif
77310
77311/*
77312** This function returns a pointer to the array of opcodes associated with
77313** the Vdbe passed as the first argument. It is the callers responsibility
77314** to arrange for the returned array to be eventually freed using the
77315** vdbeFreeOpArray() function.
77316**
77317** Before returning, *pnOp is set to the number of entries in the returned
77318** array. Also, *pnMaxArg is set to the larger of its current value and
77319** the number of entries in the Vdbe.apArg[] array required to execute the
77320** returned program.
77321*/
77322SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
77323 VdbeOp *aOp = p->aOp;
77324 assert( aOp && !p->db->mallocFailed )((void) (0));
77325
77326 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
77327 assert( DbMaskAllZero(p->btreeMask) )((void) (0));
77328
77329 resolveP2Values(p, pnMaxArg);
77330 *pnOp = p->nOp;
77331 p->aOp = 0;
77332 return aOp;
77333}
77334
77335/*
77336** Add a whole list of operations to the operation stack. Return a
77337** pointer to the first operation inserted.
77338**
77339** Non-zero P2 arguments to jump instructions are automatically adjusted
77340** so that the jump target is relative to the first operation inserted.
77341*/
77342SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeAddOpList(
77343 Vdbe *p, /* Add opcodes to the prepared statement */
77344 int nOp, /* Number of opcodes to add */
77345 VdbeOpList const *aOp, /* The opcodes to be added */
77346 int iLineno /* Source-file line number of first opcode */
77347){
77348 int i;
77349 VdbeOp *pOut, *pFirst;
77350 assert( nOp>0 )((void) (0));
77351 assert( p->magic==VDBE_MAGIC_INIT )((void) (0));
77352 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
77353 return 0;
77354 }
77355 pFirst = pOut = &p->aOp[p->nOp];
77356 for(i=0; i<nOp; i++, aOp++, pOut++){
77357 pOut->opcode = aOp->opcode;
77358 pOut->p1 = aOp->p1;
77359 pOut->p2 = aOp->p2;
77360 assert( aOp->p2>=0 )((void) (0));
77361 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP0x01)!=0 && aOp->p2>0 ){
77362 pOut->p2 += p->nOp;
77363 }
77364 pOut->p3 = aOp->p3;
77365 pOut->p4type = P4_NOTUSED0;
77366 pOut->p4.p = 0;
77367 pOut->p5 = 0;
77368#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
77369 pOut->zComment = 0;
77370#endif
77371#ifdef SQLITE_VDBE_COVERAGE
77372 pOut->iSrcLine = iLineno+i;
77373#else
77374 (void)iLineno;
77375#endif
77376#ifdef SQLITE_DEBUG
77377 if( p->db->flags & SQLITE_VdbeAddopTrace ){
77378 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
77379 }
77380#endif
77381 }
77382 p->nOp += nOp;
77383 return pFirst;
77384}
77385
77386#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
77387/*
77388** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus().
77389*/
77390SQLITE_PRIVATEstatic void sqlite3VdbeScanStatus(
77391 Vdbe *p, /* VM to add scanstatus() to */
77392 int addrExplain, /* Address of OP_Explain (or 0) */
77393 int addrLoop, /* Address of loop counter */
77394 int addrVisit, /* Address of rows visited counter */
77395 LogEst nEst, /* Estimated number of output rows */
77396 const char *zName /* Name of table or index being scanned */
77397){
77398 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
77399 ScanStatus *aNew;
77400 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
77401 if( aNew ){
77402 ScanStatus *pNew = &aNew[p->nScan++];
77403 pNew->addrExplain = addrExplain;
77404 pNew->addrLoop = addrLoop;
77405 pNew->addrVisit = addrVisit;
77406 pNew->nEst = nEst;
77407 pNew->zName = sqlite3DbStrDup(p->db, zName);
77408 p->aScan = aNew;
77409 }
77410}
77411#endif
77412
77413
77414/*
77415** Change the value of the opcode, or P1, P2, P3, or P5 operands
77416** for a specific instruction.
77417*/
77418SQLITE_PRIVATEstatic void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){
77419 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
77420}
77421SQLITE_PRIVATEstatic void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
77422 sqlite3VdbeGetOp(p,addr)->p1 = val;
77423}
77424SQLITE_PRIVATEstatic void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
77425 sqlite3VdbeGetOp(p,addr)->p2 = val;
77426}
77427SQLITE_PRIVATEstatic void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
77428 sqlite3VdbeGetOp(p,addr)->p3 = val;
77429}
77430SQLITE_PRIVATEstatic void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
77431 assert( p->nOp>0 || p->db->mallocFailed )((void) (0));
77432 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
77433}
77434
77435/*
77436** Change the P2 operand of instruction addr so that it points to
77437** the address of the next instruction to be coded.
77438*/
77439SQLITE_PRIVATEstatic void sqlite3VdbeJumpHere(Vdbe *p, int addr){
77440 sqlite3VdbeChangeP2(p, addr, p->nOp);
77441}
77442
77443
77444/*
77445** If the input FuncDef structure is ephemeral, then free it. If
77446** the FuncDef is not ephermal, then do nothing.
77447*/
77448static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
77449 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM0x0010)!=0 ){
77450 sqlite3DbFreeNN(db, pDef);
77451 }
77452}
77453
77454static void vdbeFreeOpArray(sqlite3 *, Op *, int);
77455
77456/*
77457** Delete a P4 value if necessary.
77458*/
77459static SQLITE_NOINLINE__attribute__((noinline)) void freeP4Mem(sqlite3 *db, Mem *p){
77460 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
77461 sqlite3DbFreeNN(db, p);
77462}
77463static SQLITE_NOINLINE__attribute__((noinline)) void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){
77464 freeEphemeralFunction(db, p->pFunc);
77465 sqlite3DbFreeNN(db, p);
77466}
77467static void freeP4(sqlite3 *db, int p4type, void *p4){
77468 assert( db )((void) (0));
77469 switch( p4type ){
77470 case P4_FUNCCTX(-16): {
77471 freeP4FuncCtx(db, (sqlite3_context*)p4);
77472 break;
77473 }
77474 case P4_REAL(-13):
77475 case P4_INT64(-14):
77476 case P4_DYNAMIC(-7):
77477 case P4_DYNBLOB(-17):
77478 case P4_INTARRAY(-15): {
77479 sqlite3DbFree(db, p4);
77480 break;
77481 }
77482 case P4_KEYINFO(-9): {
77483 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
77484 break;
77485 }
77486#ifdef SQLITE_ENABLE_CURSOR_HINTS
77487 case P4_EXPR(-10): {
77488 sqlite3ExprDelete(db, (Expr*)p4);
77489 break;
77490 }
77491#endif
77492 case P4_FUNCDEF(-8): {
77493 freeEphemeralFunction(db, (FuncDef*)p4);
77494 break;
77495 }
77496 case P4_MEM(-11): {
77497 if( db->pnBytesFreed==0 ){
77498 sqlite3ValueFree((sqlite3_value*)p4);
77499 }else{
77500 freeP4Mem(db, (Mem*)p4);
77501 }
77502 break;
77503 }
77504 case P4_VTAB(-12) : {
77505 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
77506 break;
77507 }
77508 }
77509}
77510
77511/*
77512** Free the space allocated for aOp and any p4 values allocated for the
77513** opcodes contained within. If aOp is not NULL it is assumed to contain
77514** nOp entries.
77515*/
77516static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
77517 if( aOp ){
77518 Op *pOp;
77519 for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
77520 if( pOp->p4type <= P4_FREE_IF_LE(-7) ) freeP4(db, pOp->p4type, pOp->p4.p);
77521#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
77522 sqlite3DbFree(db, pOp->zComment);
77523#endif
77524 }
77525 sqlite3DbFreeNN(db, aOp);
77526 }
77527}
77528
77529/*
77530** Link the SubProgram object passed as the second argument into the linked
77531** list at Vdbe.pSubProgram. This list is used to delete all sub-program
77532** objects when the VM is no longer required.
77533*/
77534SQLITE_PRIVATEstatic void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
77535 p->pNext = pVdbe->pProgram;
77536 pVdbe->pProgram = p;
77537}
77538
77539/*
77540** Change the opcode at addr into OP_Noop
77541*/
77542SQLITE_PRIVATEstatic int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
77543 VdbeOp *pOp;
77544 if( p->db->mallocFailed ) return 0;
77545 assert( addr>=0 && addr<p->nOp )((void) (0));
77546 pOp = &p->aOp[addr];
77547 freeP4(p->db, pOp->p4type, pOp->p4.p);
77548 pOp->p4type = P4_NOTUSED0;
77549 pOp->p4.z = 0;
77550 pOp->opcode = OP_Noop170;
77551 return 1;
77552}
77553
77554/*
77555** If the last opcode is "op" and it is not a jump destination,
77556** then remove it. Return true if and only if an opcode was removed.
77557*/
77558SQLITE_PRIVATEstatic int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
77559 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
77560 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
77561 }else{
77562 return 0;
77563 }
77564}
77565
77566/*
77567** Change the value of the P4 operand for a specific instruction.
77568** This routine is useful when a large program is loaded from a
77569** static array using sqlite3VdbeAddOpList but we want to make a
77570** few minor changes to the program.
77571**
77572** If n>=0 then the P4 operand is dynamic, meaning that a copy of
77573** the string is made into memory obtained from sqlite3_malloc().
77574** A value of n==0 means copy bytes of zP4 up to and including the
77575** first null byte. If n>0 then copy n+1 bytes of zP4.
77576**
77577** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
77578** to a string or structure that is guaranteed to exist for the lifetime of
77579** the Vdbe. In these cases we can just copy the pointer.
77580**
77581** If addr<0 then change P4 on the most recently inserted instruction.
77582*/
77583static void SQLITE_NOINLINE__attribute__((noinline)) vdbeChangeP4Full(
77584 Vdbe *p,
77585 Op *pOp,
77586 const char *zP4,
77587 int n
77588){
77589 if( pOp->p4type ){
77590 freeP4(p->db, pOp->p4type, pOp->p4.p);
77591 pOp->p4type = 0;
77592 pOp->p4.p = 0;
77593 }
77594 if( n<0 ){
77595 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
77596 }else{
77597 if( n==0 ) n = sqlite3Strlen30(zP4);
77598 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
77599 pOp->p4type = P4_DYNAMIC(-7);
77600 }
77601}
77602SQLITE_PRIVATEstatic void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
77603 Op *pOp;
77604 sqlite3 *db;
77605 assert( p!=0 )((void) (0));
77606 db = p->db;
77607 assert( p->magic==VDBE_MAGIC_INIT )((void) (0));
77608 assert( p->aOp!=0 || db->mallocFailed )((void) (0));
77609 if( db->mallocFailed ){
77610 if( n!=P4_VTAB(-12) ) freeP4(db, n, (void*)*(char**)&zP4);
77611 return;
77612 }
77613 assert( p->nOp>0 )((void) (0));
77614 assert( addr<p->nOp )((void) (0));
77615 if( addr<0 ){
77616 addr = p->nOp - 1;
77617 }
77618 pOp = &p->aOp[addr];
77619 if( n>=0 || pOp->p4type ){
77620 vdbeChangeP4Full(p, pOp, zP4, n);
77621 return;
77622 }
77623 if( n==P4_INT32(-3) ){
77624 /* Note: this cast is safe, because the origin data point was an int
77625 ** that was cast to a (const char *). */
77626 pOp->p4.i = SQLITE_PTR_TO_INT(zP4)((int)(long int)(zP4));
77627 pOp->p4type = P4_INT32(-3);
77628 }else if( zP4!=0 ){
77629 assert( n<0 )((void) (0));
77630 pOp->p4.p = (void*)zP4;
77631 pOp->p4type = (signed char)n;
77632 if( n==P4_VTAB(-12) ) sqlite3VtabLock((VTable*)zP4);
77633 }
77634}
77635
77636/*
77637** Change the P4 operand of the most recently coded instruction
77638** to the value defined by the arguments. This is a high-speed
77639** version of sqlite3VdbeChangeP4().
77640**
77641** The P4 operand must not have been previously defined. And the new
77642** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of
77643** those cases.
77644*/
77645SQLITE_PRIVATEstatic void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
77646 VdbeOp *pOp;
77647 assert( n!=P4_INT32 && n!=P4_VTAB )((void) (0));
77648 assert( n<=0 )((void) (0));
77649 if( p->db->mallocFailed ){
77650 freeP4(p->db, n, pP4);
77651 }else{
77652 assert( pP4!=0 )((void) (0));
77653 assert( p->nOp>0 )((void) (0));
77654 pOp = &p->aOp[p->nOp-1];
77655 assert( pOp->p4type==P4_NOTUSED )((void) (0));
77656 pOp->p4type = n;
77657 pOp->p4.p = pP4;
77658 }
77659}
77660
77661/*
77662** Set the P4 on the most recently added opcode to the KeyInfo for the
77663** index given.
77664*/
77665SQLITE_PRIVATEstatic void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
77666 Vdbe *v = pParse->pVdbe;
77667 KeyInfo *pKeyInfo;
77668 assert( v!=0 )((void) (0));
77669 assert( pIdx!=0 )((void) (0));
77670 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
77671 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-9));
77672}
77673
77674#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
77675/*
77676** Change the comment on the most recently coded instruction. Or
77677** insert a No-op and add the comment to that new instruction. This
77678** makes the code easier to read during debugging. None of this happens
77679** in a production build.
77680*/
77681static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
77682 assert( p->nOp>0 || p->aOp==0 )((void) (0));
77683 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed )((void) (0));
77684 if( p->nOp ){
77685 assert( p->aOp )((void) (0));
77686 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
77687 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
77688 }
77689}
77690SQLITE_PRIVATEstatic void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
77691 va_list ap;
77692 if( p ){
77693 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
77694 vdbeVComment(p, zFormat, ap);
77695 va_end(ap)__builtin_va_end(ap);
77696 }
77697}
77698SQLITE_PRIVATEstatic void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
77699 va_list ap;
77700 if( p ){
77701 sqlite3VdbeAddOp0(p, OP_Noop170);
77702 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
77703 vdbeVComment(p, zFormat, ap);
77704 va_end(ap)__builtin_va_end(ap);
77705 }
77706}
77707#endif /* NDEBUG */
77708
77709#ifdef SQLITE_VDBE_COVERAGE
77710/*
77711** Set the value if the iSrcLine field for the previously coded instruction.
77712*/
77713SQLITE_PRIVATEstatic void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
77714 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
77715}
77716#endif /* SQLITE_VDBE_COVERAGE */
77717
77718/*
77719** Return the opcode for a given address. If the address is -1, then
77720** return the most recently inserted opcode.
77721**
77722** If a memory allocation error has occurred prior to the calling of this
77723** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
77724** is readable but not writable, though it is cast to a writable value.
77725** The return of a dummy opcode allows the call to continue functioning
77726** after an OOM fault without having to check to see if the return from
77727** this routine is a valid pointer. But because the dummy.opcode is 0,
77728** dummy will never be written to. This is verified by code inspection and
77729** by running with Valgrind.
77730*/
77731SQLITE_PRIVATEstatic VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
77732 /* C89 specifies that the constant "dummy" will be initialized to all
77733 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
77734 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */
77735 assert( p->magic==VDBE_MAGIC_INIT )((void) (0));
77736 if( addr<0 ){
77737 addr = p->nOp - 1;
77738 }
77739 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed )((void) (0));
77740 if( p->db->mallocFailed ){
77741 return (VdbeOp*)&dummy;
77742 }else{
77743 return &p->aOp[addr];
77744 }
77745}
77746
77747#if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
77748/*
77749** Return an integer value for one of the parameters to the opcode pOp
77750** determined by character c.
77751*/
77752static int translateP(char c, const Op *pOp){
77753 if( c=='1' ) return pOp->p1;
77754 if( c=='2' ) return pOp->p2;
77755 if( c=='3' ) return pOp->p3;
77756 if( c=='4' ) return pOp->p4.i;
77757 return pOp->p5;
77758}
77759
77760/*
77761** Compute a string for the "comment" field of a VDBE opcode listing.
77762**
77763** The Synopsis: field in comments in the vdbe.c source file gets converted
77764** to an extra string that is appended to the sqlite3OpcodeName(). In the
77765** absence of other comments, this synopsis becomes the comment on the opcode.
77766** Some translation occurs:
77767**
77768** "PX" -> "r[X]"
77769** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
77770** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
77771** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
77772*/
77773static int displayComment(
77774 const Op *pOp, /* The opcode to be commented */
77775 const char *zP4, /* Previously obtained value for P4 */
77776 char *zTemp, /* Write result here */
77777 int nTemp /* Space available in zTemp[] */
77778){
77779 const char *zOpName;
77780 const char *zSynopsis;
77781 int nOpName;
77782 int ii, jj;
77783 char zAlt[50];
77784 zOpName = sqlite3OpcodeName(pOp->opcode);
77785 nOpName = sqlite3Strlen30(zOpName);
77786 if( zOpName[nOpName+1] ){
77787 int seenCom = 0;
77788 char c;
77789 zSynopsis = zOpName += nOpName + 1;
77790 if( strncmp(zSynopsis,"IF ",3)==0 ){
77791 if( pOp->p5 & SQLITE_STOREP20x20 ){
77792 sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3);
77793 }else{
77794 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3);
77795 }
77796 zSynopsis = zAlt;
77797 }
77798 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
77799 if( c=='P' ){
77800 c = zSynopsis[++ii];
77801 if( c=='4' ){
77802 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
77803 }else if( c=='X' ){
77804 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
77805 seenCom = 1;
77806 }else{
77807 int v1 = translateP(c, pOp);
77808 int v2;
77809 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
77810 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
77811 ii += 3;
77812 jj += sqlite3Strlen30(zTemp+jj);
77813 v2 = translateP(zSynopsis[ii], pOp);
77814 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
77815 ii += 2;
77816 v2++;
77817 }
77818 if( v2>1 ){
77819 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
77820 }
77821 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
77822 ii += 4;
77823 }
77824 }
77825 jj += sqlite3Strlen30(zTemp+jj);
77826 }else{
77827 zTemp[jj++] = c;
77828 }
77829 }
77830 if( !seenCom && jj<nTemp-5 && pOp->zComment ){
77831 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
77832 jj += sqlite3Strlen30(zTemp+jj);
77833 }
77834 if( jj<nTemp ) zTemp[jj] = 0;
77835 }else if( pOp->zComment ){
77836 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
77837 jj = sqlite3Strlen30(zTemp);
77838 }else{
77839 zTemp[0] = 0;
77840 jj = 0;
77841 }
77842 return jj;
77843}
77844#endif /* SQLITE_DEBUG */
77845
77846#if VDBE_DISPLAY_P41 && defined(SQLITE_ENABLE_CURSOR_HINTS)
77847/*
77848** Translate the P4.pExpr value for an OP_CursorHint opcode into text
77849** that can be displayed in the P4 column of EXPLAIN output.
77850*/
77851static void displayP4Expr(StrAccum *p, Expr *pExpr){
77852 const char *zOp = 0;
77853 switch( pExpr->op ){
77854 case TK_STRING110:
77855 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
77856 break;
77857 case TK_INTEGER147:
77858 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
77859 break;
77860 case TK_NULL114:
77861 sqlite3_str_appendf(p, "NULL");
77862 break;
77863 case TK_REGISTER168: {
77864 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
77865 break;
77866 }
77867 case TK_COLUMN162: {
77868 if( pExpr->iColumn<0 ){
77869 sqlite3_str_appendf(p, "rowid");
77870 }else{
77871 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
77872 }
77873 break;
77874 }
77875 case TK_LT56: zOp = "LT"; break;
77876 case TK_LE55: zOp = "LE"; break;
77877 case TK_GT54: zOp = "GT"; break;
77878 case TK_GE57: zOp = "GE"; break;
77879 case TK_NE52: zOp = "NE"; break;
77880 case TK_EQ53: zOp = "EQ"; break;
77881 case TK_IS45: zOp = "IS"; break;
77882 case TK_ISNOT160: zOp = "ISNOT"; break;
77883 case TK_AND44: zOp = "AND"; break;
77884 case TK_OR43: zOp = "OR"; break;
77885 case TK_PLUS100: zOp = "ADD"; break;
77886 case TK_STAR102: zOp = "MUL"; break;
77887 case TK_MINUS101: zOp = "SUB"; break;
77888 case TK_REM104: zOp = "REM"; break;
77889 case TK_BITAND96: zOp = "BITAND"; break;
77890 case TK_BITOR97: zOp = "BITOR"; break;
77891 case TK_SLASH103: zOp = "DIV"; break;
77892 case TK_LSHIFT98: zOp = "LSHIFT"; break;
77893 case TK_RSHIFT99: zOp = "RSHIFT"; break;
77894 case TK_CONCAT105: zOp = "CONCAT"; break;
77895 case TK_UMINUS165: zOp = "MINUS"; break;
77896 case TK_UPLUS166: zOp = "PLUS"; break;
77897 case TK_BITNOT107: zOp = "BITNOT"; break;
77898 case TK_NOT19: zOp = "NOT"; break;
77899 case TK_ISNULL50: zOp = "ISNULL"; break;
77900 case TK_NOTNULL51: zOp = "NOTNULL"; break;
77901
77902 default:
77903 sqlite3_str_appendf(p, "%s", "expr");
77904 break;
77905 }
77906
77907 if( zOp ){
77908 sqlite3_str_appendf(p, "%s(", zOp);
77909 displayP4Expr(p, pExpr->pLeft);
77910 if( pExpr->pRight ){
77911 sqlite3_str_append(p, ",", 1);
77912 displayP4Expr(p, pExpr->pRight);
77913 }
77914 sqlite3_str_append(p, ")", 1);
77915 }
77916}
77917#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
77918
77919
77920#if VDBE_DISPLAY_P41
77921/*
77922** Compute a string that describes the P4 parameter for an opcode.
77923** Use zTemp for any required temporary buffer space.
77924*/
77925static char *displayP4(Op *pOp, char *zTemp, int nTemp){
77926 char *zP4 = zTemp;
77927 StrAccum x;
77928 assert( nTemp>=20 )((void) (0));
77929 sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
77930 switch( pOp->p4type ){
77931 case P4_KEYINFO(-9): {
77932 int j;
77933 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
77934 assert( pKeyInfo->aSortOrder!=0 )((void) (0));
77935 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
77936 for(j=0; j<pKeyInfo->nKeyField; j++){
77937 CollSeq *pColl = pKeyInfo->aColl[j];
77938 const char *zColl = pColl ? pColl->zName : "";
77939 if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
77940 sqlite3_str_appendf(&x, ",%s%s",
77941 pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
77942 }
77943 sqlite3_str_append(&x, ")", 1);
77944 break;
77945 }
77946#ifdef SQLITE_ENABLE_CURSOR_HINTS
77947 case P4_EXPR(-10): {
77948 displayP4Expr(&x, pOp->p4.pExpr);
77949 break;
77950 }
77951#endif
77952 case P4_COLLSEQ(-2): {
77953 CollSeq *pColl = pOp->p4.pColl;
77954 sqlite3_str_appendf(&x, "(%.20s)", pColl->zName);
77955 break;
77956 }
77957 case P4_FUNCDEF(-8): {
77958 FuncDef *pDef = pOp->p4.pFunc;
77959 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
77960 break;
77961 }
77962#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
77963 case P4_FUNCCTX(-16): {
77964 FuncDef *pDef = pOp->p4.pCtx->pFunc;
77965 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
77966 break;
77967 }
77968#endif
77969 case P4_INT64(-14): {
77970 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
77971 break;
77972 }
77973 case P4_INT32(-3): {
77974 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
77975 break;
77976 }
77977 case P4_REAL(-13): {
77978 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
77979 break;
77980 }
77981 case P4_MEM(-11): {
77982 Mem *pMem = pOp->p4.pMem;
77983 if( pMem->flags & MEM_Str0x0002 ){
77984 zP4 = pMem->z;
77985 }else if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
77986 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
77987 }else if( pMem->flags & MEM_Real0x0008 ){
77988 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
77989 }else if( pMem->flags & MEM_Null0x0001 ){
77990 zP4 = "NULL";
77991 }else{
77992 assert( pMem->flags & MEM_Blob )((void) (0));
77993 zP4 = "(blob)";
77994 }
77995 break;
77996 }
77997#ifndef SQLITE_OMIT_VIRTUALTABLE
77998 case P4_VTAB(-12): {
77999 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
78000 sqlite3_str_appendf(&x, "vtab:%p", pVtab);
78001 break;
78002 }
78003#endif
78004 case P4_INTARRAY(-15): {
78005 int i;
78006 int *ai = pOp->p4.ai;
78007 int n = ai[0]; /* The first element of an INTARRAY is always the
78008 ** count of the number of elements to follow */
78009 for(i=1; i<=n; i++){
78010 sqlite3_str_appendf(&x, ",%d", ai[i]);
78011 }
78012 zTemp[0] = '[';
78013 sqlite3_str_append(&x, "]", 1);
78014 break;
78015 }
78016 case P4_SUBPROGRAM(-4): {
78017 sqlite3_str_appendf(&x, "program");
78018 break;
78019 }
78020 case P4_DYNBLOB(-17):
78021 case P4_ADVANCE(-5): {
78022 zTemp[0] = 0;
78023 break;
78024 }
78025 case P4_TABLE(-6): {
78026 sqlite3_str_appendf(&x, "%s", pOp->p4.pTab->zName);
78027 break;
78028 }
78029 default: {
78030 zP4 = pOp->p4.z;
78031 if( zP4==0 ){
78032 zP4 = zTemp;
78033 zTemp[0] = 0;
78034 }
78035 }
78036 }
78037 sqlite3StrAccumFinish(&x);
78038 assert( zP4!=0 )((void) (0));
78039 return zP4;
78040}
78041#endif /* VDBE_DISPLAY_P4 */
78042
78043/*
78044** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
78045**
78046** The prepared statements need to know in advance the complete set of
78047** attached databases that will be use. A mask of these databases
78048** is maintained in p->btreeMask. The p->lockMask value is the subset of
78049** p->btreeMask of databases that will require a lock.
78050*/
78051SQLITE_PRIVATEstatic void sqlite3VdbeUsesBtree(Vdbe *p, int i){
78052 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 )((void) (0));
78053 assert( i<(int)sizeof(p->btreeMask)*8 )((void) (0));
78054 DbMaskSet(p->btreeMask, i)(p->btreeMask)|=(((yDbMask)1)<<(i));
78055 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
78056 DbMaskSet(p->lockMask, i)(p->lockMask)|=(((yDbMask)1)<<(i));
78057 }
78058}
78059
78060#if !defined(SQLITE_OMIT_SHARED_CACHE)
78061/*
78062** If SQLite is compiled to support shared-cache mode and to be threadsafe,
78063** this routine obtains the mutex associated with each BtShared structure
78064** that may be accessed by the VM passed as an argument. In doing so it also
78065** sets the BtShared.db member of each of the BtShared structures, ensuring
78066** that the correct busy-handler callback is invoked if required.
78067**
78068** If SQLite is not threadsafe but does support shared-cache mode, then
78069** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
78070** of all of BtShared structures accessible via the database handle
78071** associated with the VM.
78072**
78073** If SQLite is not threadsafe and does not support shared-cache mode, this
78074** function is a no-op.
78075**
78076** The p->btreeMask field is a bitmask of all btrees that the prepared
78077** statement p will ever use. Let N be the number of bits in p->btreeMask
78078** corresponding to btrees that use shared cache. Then the runtime of
78079** this routine is N*N. But as N is rarely more than 1, this should not
78080** be a problem.
78081*/
78082SQLITE_PRIVATEstatic void sqlite3VdbeEnter(Vdbe *p){
78083 int i;
78084 sqlite3 *db;
78085 Db *aDb;
78086 int nDb;
78087 if( DbMaskAllZero(p->lockMask)(p->lockMask)==0 ) return; /* The common case */
78088 db = p->db;
78089 aDb = db->aDb;
78090 nDb = db->nDb;
78091 for(i=0; i<nDb; i++){
78092 if( i!=1 && DbMaskTest(p->lockMask,i)(((p->lockMask)&(((yDbMask)1)<<(i)))!=0) && ALWAYS(aDb[i].pBt!=0)(aDb[i].pBt!=0) ){
78093 sqlite3BtreeEnter(aDb[i].pBt);
78094 }
78095 }
78096}
78097#endif
78098
78099#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE1>0
78100/*
78101** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
78102*/
78103static SQLITE_NOINLINE__attribute__((noinline)) void vdbeLeave(Vdbe *p){
78104 int i;
78105 sqlite3 *db;
78106 Db *aDb;
78107 int nDb;
78108 db = p->db;
78109 aDb = db->aDb;
78110 nDb = db->nDb;
78111 for(i=0; i<nDb; i++){
78112 if( i!=1 && DbMaskTest(p->lockMask,i)(((p->lockMask)&(((yDbMask)1)<<(i)))!=0) && ALWAYS(aDb[i].pBt!=0)(aDb[i].pBt!=0) ){
78113 sqlite3BtreeLeave(aDb[i].pBt);
78114 }
78115 }
78116}
78117SQLITE_PRIVATEstatic void sqlite3VdbeLeave(Vdbe *p){
78118 if( DbMaskAllZero(p->lockMask)(p->lockMask)==0 ) return; /* The common case */
78119 vdbeLeave(p);
78120}
78121#endif
78122
78123#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
78124/*
78125** Print a single opcode. This routine is used for debugging only.
78126*/
78127SQLITE_PRIVATEstatic void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
78128 char *zP4;
78129 char zPtr[50];
78130 char zCom[100];
78131 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
78132 if( pOut==0 ) pOut = stdoutstdout;
78133 zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
78134#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
78135 displayComment(pOp, zP4, zCom, sizeof(zCom));
78136#else
78137 zCom[0] = 0;
78138#endif
78139 /* NB: The sqlite3OpcodeName() function is implemented by code created
78140 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
78141 ** information from the vdbe.c source text */
78142 fprintf(pOut, zFormat1, pc,
78143 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
78144 zCom
78145 );
78146 fflush(pOut);
78147}
78148#endif
78149
78150/*
78151** Initialize an array of N Mem element.
78152*/
78153static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
78154 while( (N--)>0 ){
78155 p->db = db;
78156 p->flags = flags;
78157 p->szMalloc = 0;
78158#ifdef SQLITE_DEBUG
78159 p->pScopyFrom = 0;
78160#endif
78161 p++;
78162 }
78163}
78164
78165/*
78166** Release an array of N Mem elements
78167*/
78168static void releaseMemArray(Mem *p, int N){
78169 if( p && N ){
78170 Mem *pEnd = &p[N];
78171 sqlite3 *db = p->db;
78172 if( db->pnBytesFreed ){
78173 do{
78174 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
78175 }while( (++p)<pEnd );
78176 return;
78177 }
78178 do{
78179 assert( (&p[1])==pEnd || p[0].db==p[1].db )((void) (0));
78180 assert( sqlite3VdbeCheckMemInvariants(p) )((void) (0));
78181
78182 /* This block is really an inlined version of sqlite3VdbeMemRelease()
78183 ** that takes advantage of the fact that the memory cell value is
78184 ** being set to NULL after releasing any dynamic resources.
78185 **
78186 ** The justification for duplicating code is that according to
78187 ** callgrind, this causes a certain test case to hit the CPU 4.7
78188 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
78189 ** sqlite3MemRelease() were called from here. With -O2, this jumps
78190 ** to 6.6 percent. The test case is inserting 1000 rows into a table
78191 ** with no indexes using a single prepared INSERT statement, bind()
78192 ** and reset(). Inserts are grouped into a transaction.
78193 */
78194 testcase( p->flags & MEM_Agg );
78195 testcase( p->flags & MEM_Dyn );
78196 testcase( p->xDel==sqlite3VdbeFrameMemDel );
78197 if( p->flags&(MEM_Agg0x2000|MEM_Dyn0x0400) ){
78198 sqlite3VdbeMemRelease(p);
78199 }else if( p->szMalloc ){
78200 sqlite3DbFreeNN(db, p->zMalloc);
78201 p->szMalloc = 0;
78202 }
78203
78204 p->flags = MEM_Undefined0x0080;
78205 }while( (++p)<pEnd );
78206 }
78207}
78208
78209#ifdef SQLITE_DEBUG
78210/*
78211** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is
78212** and false if something is wrong.
78213**
78214** This routine is intended for use inside of assert() statements only.
78215*/
78216SQLITE_PRIVATEstatic int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){
78217 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC0x879fb71e ) return 0;
78218 return 1;
78219}
78220#endif
78221
78222
78223/*
78224** This is a destructor on a Mem object (which is really an sqlite3_value)
78225** that deletes the Frame object that is attached to it as a blob.
78226**
78227** This routine does not delete the Frame right away. It merely adds the
78228** frame to a list of frames to be deleted when the Vdbe halts.
78229*/
78230SQLITE_PRIVATEstatic void sqlite3VdbeFrameMemDel(void *pArg){
78231 VdbeFrame *pFrame = (VdbeFrame*)pArg;
78232 assert( sqlite3VdbeFrameIsValid(pFrame) )((void) (0));
78233 pFrame->pParent = pFrame->v->pDelFrame;
78234 pFrame->v->pDelFrame = pFrame;
78235}
78236
78237
78238/*
78239** Delete a VdbeFrame object and its contents. VdbeFrame objects are
78240** allocated by the OP_Program opcode in sqlite3VdbeExec().
78241*/
78242SQLITE_PRIVATEstatic void sqlite3VdbeFrameDelete(VdbeFrame *p){
78243 int i;
78244 Mem *aMem = VdbeFrameMem(p)((Mem *)&((u8 *)p)[(((sizeof(VdbeFrame))+7)&~7)]);
78245 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
78246 assert( sqlite3VdbeFrameIsValid(p) )((void) (0));
78247 for(i=0; i<p->nChildCsr; i++){
78248 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
78249 }
78250 releaseMemArray(aMem, p->nChildMem);
78251 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
78252 sqlite3DbFree(p->v->db, p);
78253}
78254
78255#ifndef SQLITE_OMIT_EXPLAIN
78256/*
78257** Give a listing of the program in the virtual machine.
78258**
78259** The interface is the same as sqlite3VdbeExec(). But instead of
78260** running the code, it invokes the callback once for each instruction.
78261** This feature is used to implement "EXPLAIN".
78262**
78263** When p->explain==1, each instruction is listed. When
78264** p->explain==2, only OP_Explain instructions are listed and these
78265** are shown in a different format. p->explain==2 is used to implement
78266** EXPLAIN QUERY PLAN.
78267** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
78268** are also shown, so that the boundaries between the main program and
78269** each trigger are clear.
78270**
78271** When p->explain==1, first the main program is listed, then each of
78272** the trigger subprograms are listed one by one.
78273*/
78274SQLITE_PRIVATEstatic int sqlite3VdbeList(
78275 Vdbe *p /* The VDBE */
78276){
78277 int nRow; /* Stop when row count reaches this */
78278 int nSub = 0; /* Number of sub-vdbes seen so far */
78279 SubProgram **apSub = 0; /* Array of sub-vdbes */
78280 Mem *pSub = 0; /* Memory cell hold array of subprogs */
78281 sqlite3 *db = p->db; /* The database connection */
78282 int i; /* Loop counter */
78283 int rc = SQLITE_OK0; /* Return code */
78284 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
78285 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP0x01000000)!=0);
78286 Op *pOp = 0;
78287
78288 assert( p->explain )((void) (0));
78289 assert( p->magic==VDBE_MAGIC_RUN )((void) (0));
78290 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM )((void) (0));
78291
78292 /* Even though this opcode does not use dynamic strings for
78293 ** the result, result columns may become dynamic if the user calls
78294 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
78295 */
78296 releaseMemArray(pMem, 8);
78297 p->pResultSet = 0;
78298
78299 if( p->rc==SQLITE_NOMEM7 ){
78300 /* This happens if a malloc() inside a call to sqlite3_column_text() or
78301 ** sqlite3_column_text16() failed. */
78302 sqlite3OomFault(db);
78303 return SQLITE_ERROR1;
78304 }
78305
78306 /* When the number of output rows reaches nRow, that means the
78307 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
78308 ** nRow is the sum of the number of rows in the main program, plus
78309 ** the sum of the number of rows in all trigger subprograms encountered
78310 ** so far. The nRow value will increase as new trigger subprograms are
78311 ** encountered, but p->pc will eventually catch up to nRow.
78312 */
78313 nRow = p->nOp;
78314 if( bListSubprogs ){
78315 /* The first 8 memory cells are used for the result set. So we will
78316 ** commandeer the 9th cell to use as storage for an array of pointers
78317 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
78318 ** cells. */
78319 assert( p->nMem>9 )((void) (0));
78320 pSub = &p->aMem[9];
78321 if( pSub->flags&MEM_Blob0x0010 ){
78322 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
78323 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
78324 nSub = pSub->n/sizeof(Vdbe*);
78325 apSub = (SubProgram **)pSub->z;
78326 }
78327 for(i=0; i<nSub; i++){
78328 nRow += apSub[i]->nOp;
78329 }
78330 }
78331
78332 while(1){ /* Loop exits via break */
78333 i = p->pc++;
78334 if( i>=nRow ){
78335 p->rc = SQLITE_OK0;
78336 rc = SQLITE_DONE101;
78337 break;
78338 }
78339 if( i<p->nOp ){
78340 /* The output line number is small enough that we are still in the
78341 ** main program. */
78342 pOp = &p->aOp[i];
78343 }else{
78344 /* We are currently listing subprograms. Figure out which one and
78345 ** pick up the appropriate opcode. */
78346 int j;
78347 i -= p->nOp;
78348 for(j=0; i>=apSub[j]->nOp; j++){
78349 i -= apSub[j]->nOp;
78350 }
78351 pOp = &apSub[j]->aOp[i];
78352 }
78353
78354 /* When an OP_Program opcode is encounter (the only opcode that has
78355 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
78356 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
78357 ** has not already been seen.
78358 */
78359 if( bListSubprogs && pOp->p4type==P4_SUBPROGRAM(-4) ){
78360 int nByte = (nSub+1)*sizeof(SubProgram*);
78361 int j;
78362 for(j=0; j<nSub; j++){
78363 if( apSub[j]==pOp->p4.pProgram ) break;
78364 }
78365 if( j==nSub ){
78366 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
78367 if( p->rc!=SQLITE_OK0 ){
78368 rc = SQLITE_ERROR1;
78369 break;
78370 }
78371 apSub = (SubProgram **)pSub->z;
78372 apSub[nSub++] = pOp->p4.pProgram;
78373 pSub->flags |= MEM_Blob0x0010;
78374 pSub->n = nSub*sizeof(SubProgram*);
78375 nRow += pOp->p4.pProgram->nOp;
78376 }
78377 }
78378 if( p->explain<2 ) break;
78379 if( pOp->opcode==OP_Explain171 ) break;
78380 if( pOp->opcode==OP_Init61 && p->pc>1 ) break;
78381 }
78382
78383 if( rc==SQLITE_OK0 ){
78384 if( db->u1.isInterrupted ){
78385 p->rc = SQLITE_INTERRUPT9;
78386 rc = SQLITE_ERROR1;
78387 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
78388 }else{
78389 char *zP4;
78390 if( p->explain==1 ){
78391 pMem->flags = MEM_Int0x0004;
78392 pMem->u.i = i; /* Program counter */
78393 pMem++;
78394
78395 pMem->flags = MEM_Static0x0800|MEM_Str0x0002|MEM_Term0x0200;
78396 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
78397 assert( pMem->z!=0 )((void) (0));
78398 pMem->n = sqlite3Strlen30(pMem->z);
78399 pMem->enc = SQLITE_UTF81;
78400 pMem++;
78401 }
78402
78403 pMem->flags = MEM_Int0x0004;
78404 pMem->u.i = pOp->p1; /* P1 */
78405 pMem++;
78406
78407 pMem->flags = MEM_Int0x0004;
78408 pMem->u.i = pOp->p2; /* P2 */
78409 pMem++;
78410
78411 pMem->flags = MEM_Int0x0004;
78412 pMem->u.i = pOp->p3; /* P3 */
78413 pMem++;
78414
78415 if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */
78416 assert( p->db->mallocFailed )((void) (0));
78417 return SQLITE_ERROR1;
78418 }
78419 pMem->flags = MEM_Str0x0002|MEM_Term0x0200;
78420 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc);
78421 if( zP4!=pMem->z ){
78422 pMem->n = 0;
78423 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF81, 0);
78424 }else{
78425 assert( pMem->z!=0 )((void) (0));
78426 pMem->n = sqlite3Strlen30(pMem->z);
78427 pMem->enc = SQLITE_UTF81;
78428 }
78429 pMem++;
78430
78431 if( p->explain==1 ){
78432 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){
78433 assert( p->db->mallocFailed )((void) (0));
78434 return SQLITE_ERROR1;
78435 }
78436 pMem->flags = MEM_Str0x0002|MEM_Term0x0200;
78437 pMem->n = 2;
78438 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
78439 pMem->enc = SQLITE_UTF81;
78440 pMem++;
78441
78442#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
78443 if( sqlite3VdbeMemClearAndResize(pMem, 500) ){
78444 assert( p->db->mallocFailed )((void) (0));
78445 return SQLITE_ERROR1;
78446 }
78447 pMem->flags = MEM_Str0x0002|MEM_Term0x0200;
78448 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
78449 pMem->enc = SQLITE_UTF81;
78450#else
78451 pMem->flags = MEM_Null0x0001; /* Comment */
78452#endif
78453 }
78454
78455 p->nResColumn = 8 - 4*(p->explain-1);
78456 p->pResultSet = &p->aMem[1];
78457 p->rc = SQLITE_OK0;
78458 rc = SQLITE_ROW100;
78459 }
78460 }
78461 return rc;
78462}
78463#endif /* SQLITE_OMIT_EXPLAIN */
78464
78465#ifdef SQLITE_DEBUG
78466/*
78467** Print the SQL that was used to generate a VDBE program.
78468*/
78469SQLITE_PRIVATEstatic void sqlite3VdbePrintSql(Vdbe *p){
78470 const char *z = 0;
78471 if( p->zSql ){
78472 z = p->zSql;
78473 }else if( p->nOp>=1 ){
78474 const VdbeOp *pOp = &p->aOp[0];
78475 if( pOp->opcode==OP_Init61 && pOp->p4.z!=0 ){
78476 z = pOp->p4.z;
78477 while( sqlite3Isspace(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x01) ) z++;
78478 }
78479 }
78480 if( z ) printf("SQL: [%s]\n", z);
78481}
78482#endif
78483
78484#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
78485/*
78486** Print an IOTRACE message showing SQL content.
78487*/
78488SQLITE_PRIVATEstatic void sqlite3VdbeIOTraceSql(Vdbe *p){
78489 int nOp = p->nOp;
78490 VdbeOp *pOp;
78491 if( sqlite3IoTrace==0 ) return;
78492 if( nOp<1 ) return;
78493 pOp = &p->aOp[0];
78494 if( pOp->opcode==OP_Init61 && pOp->p4.z!=0 ){
78495 int i, j;
78496 char z[1000];
78497 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
78498 for(i=0; sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01); i++){}
78499 for(j=0; z[i]; i++){
78500 if( sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01) ){
78501 if( z[i-1]!=' ' ){
78502 z[j++] = ' ';
78503 }
78504 }else{
78505 z[j++] = z[i];
78506 }
78507 }
78508 z[j] = 0;
78509 sqlite3IoTrace("SQL %s\n", z);
78510 }
78511}
78512#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
78513
78514/* An instance of this object describes bulk memory available for use
78515** by subcomponents of a prepared statement. Space is allocated out
78516** of a ReusableSpace object by the allocSpace() routine below.
78517*/
78518struct ReusableSpace {
78519 u8 *pSpace; /* Available memory */
78520 sqlite3_int64 nFree; /* Bytes of available memory */
78521 sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */
78522};
78523
78524/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
78525** from the ReusableSpace object. Return a pointer to the allocated
78526** memory on success. If insufficient memory is available in the
78527** ReusableSpace object, increase the ReusableSpace.nNeeded
78528** value by the amount needed and return NULL.
78529**
78530** If pBuf is not initially NULL, that means that the memory has already
78531** been allocated by a prior call to this routine, so just return a copy
78532** of pBuf and leave ReusableSpace unchanged.
78533**
78534** This allocator is employed to repurpose unused slots at the end of the
78535** opcode array of prepared state for other memory needs of the prepared
78536** statement.
78537*/
78538static void *allocSpace(
78539 struct ReusableSpace *p, /* Bulk memory available for allocation */
78540 void *pBuf, /* Pointer to a prior allocation */
78541 sqlite3_int64 nByte /* Bytes of memory needed */
78542){
78543 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) )((void) (0));
78544 if( pBuf==0 ){
78545 nByte = ROUND8(nByte)(((nByte)+7)&~7);
78546 if( nByte <= p->nFree ){
78547 p->nFree -= nByte;
78548 pBuf = &p->pSpace[p->nFree];
78549 }else{
78550 p->nNeeded += nByte;
78551 }
78552 }
78553 assert( EIGHT_BYTE_ALIGNMENT(pBuf) )((void) (0));
78554 return pBuf;
78555}
78556
78557/*
78558** Rewind the VDBE back to the beginning in preparation for
78559** running it.
78560*/
78561SQLITE_PRIVATEstatic void sqlite3VdbeRewind(Vdbe *p){
78562#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
78563 int i;
78564#endif
78565 assert( p!=0 )((void) (0));
78566 assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET )((void) (0));
78567
78568 /* There should be at least one opcode.
78569 */
78570 assert( p->nOp>0 )((void) (0));
78571
78572 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
78573 p->magic = VDBE_MAGIC_RUN0x2df20da3;
78574
78575#ifdef SQLITE_DEBUG
78576 for(i=0; i<p->nMem; i++){
78577 assert( p->aMem[i].db==p->db )((void) (0));
78578 }
78579#endif
78580 p->pc = -1;
78581 p->rc = SQLITE_OK0;
78582 p->errorAction = OE_Abort2;
78583 p->nChange = 0;
78584 p->cacheCtr = 1;
78585 p->minWriteFileFormat = 255;
78586 p->iStatement = 0;
78587 p->nFkConstraint = 0;
78588#ifdef VDBE_PROFILE
78589 for(i=0; i<p->nOp; i++){
78590 p->aOp[i].cnt = 0;
78591 p->aOp[i].cycles = 0;
78592 }
78593#endif
78594}
78595
78596/*
78597** Prepare a virtual machine for execution for the first time after
78598** creating the virtual machine. This involves things such
78599** as allocating registers and initializing the program counter.
78600** After the VDBE has be prepped, it can be executed by one or more
78601** calls to sqlite3VdbeExec().
78602**
78603** This function may be called exactly once on each virtual machine.
78604** After this routine is called the VM has been "packaged" and is ready
78605** to run. After this routine is called, further calls to
78606** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
78607** the Vdbe from the Parse object that helped generate it so that the
78608** the Vdbe becomes an independent entity and the Parse object can be
78609** destroyed.
78610**
78611** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
78612** to its initial state after it has been run.
78613*/
78614SQLITE_PRIVATEstatic void sqlite3VdbeMakeReady(
78615 Vdbe *p, /* The VDBE */
78616 Parse *pParse /* Parsing context */
78617){
78618 sqlite3 *db; /* The database connection */
78619 int nVar; /* Number of parameters */
78620 int nMem; /* Number of VM memory registers */
78621 int nCursor; /* Number of cursors required */
78622 int nArg; /* Number of arguments in subprograms */
78623 int n; /* Loop counter */
78624 struct ReusableSpace x; /* Reusable bulk memory */
78625
78626 assert( p!=0 )((void) (0));
78627 assert( p->nOp>0 )((void) (0));
78628 assert( pParse!=0 )((void) (0));
78629 assert( p->magic==VDBE_MAGIC_INIT )((void) (0));
78630 assert( pParse==p->pParse )((void) (0));
78631 db = p->db;
78632 assert( db->mallocFailed==0 )((void) (0));
78633 nVar = pParse->nVar;
78634 nMem = pParse->nMem;
78635 nCursor = pParse->nTab;
78636 nArg = pParse->nMaxArg;
78637
78638 /* Each cursor uses a memory cell. The first cursor (cursor 0) can
78639 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate
78640 ** space at the end of aMem[] for cursors 1 and greater.
78641 ** See also: allocateCursor().
78642 */
78643 nMem += nCursor;
78644 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */
78645
78646 /* Figure out how much reusable memory is available at the end of the
78647 ** opcode array. This extra memory will be reallocated for other elements
78648 ** of the prepared statement.
78649 */
78650 n = ROUND8(sizeof(Op)*p->nOp)(((sizeof(Op)*p->nOp)+7)&~7); /* Bytes of opcode memory used */
78651 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
78652 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) )((void) (0));
78653 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n)((pParse->szOpAlloc - n)&~7); /* Bytes of unused memory */
78654 assert( x.nFree>=0 )((void) (0));
78655 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) )((void) (0));
78656
78657 resolveP2Values(p, &nArg);
78658 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
78659 if( pParse->explain && nMem<10 ){
78660 nMem = 10;
78661 }
78662 p->expired = 0;
78663
78664 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
78665 ** passes. On the first pass, we try to reuse unused memory at the
78666 ** end of the opcode array. If we are unable to satisfy all memory
78667 ** requirements by reusing the opcode array tail, then the second
78668 ** pass will fill in the remainder using a fresh memory allocation.
78669 **
78670 ** This two-pass approach that reuses as much memory as possible from
78671 ** the leftover memory at the end of the opcode array. This can significantly
78672 ** reduce the amount of memory held by a prepared statement.
78673 */
78674 x.nNeeded = 0;
78675 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
78676 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
78677 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
78678 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
78679#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
78680 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
78681#endif
78682 if( x.nNeeded ){
78683 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
78684 x.nFree = x.nNeeded;
78685 if( !db->mallocFailed ){
78686 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
78687 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
78688 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
78689 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
78690#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
78691 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
78692#endif
78693 }
78694 }
78695
78696 p->pVList = pParse->pVList;
78697 pParse->pVList = 0;
78698 p->explain = pParse->explain;
78699 if( db->mallocFailed ){
78700 p->nVar = 0;
78701 p->nCursor = 0;
78702 p->nMem = 0;
78703 }else{
78704 p->nCursor = nCursor;
78705 p->nVar = (ynVar)nVar;
78706 initMemArray(p->aVar, nVar, db, MEM_Null0x0001);
78707 p->nMem = nMem;
78708 initMemArray(p->aMem, nMem, db, MEM_Undefined0x0080);
78709 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
78710#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
78711 memset(p->anExec, 0, p->nOp*sizeof(i64));
78712#endif
78713 }
78714 sqlite3VdbeRewind(p);
78715}
78716
78717/*
78718** Close a VDBE cursor and release all the resources that cursor
78719** happens to hold.
78720*/
78721SQLITE_PRIVATEstatic void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
78722 if( pCx==0 ){
78723 return;
78724 }
78725 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE )((void) (0));
78726 switch( pCx->eCurType ){
78727 case CURTYPE_SORTER1: {
78728 sqlite3VdbeSorterClose(p->db, pCx);
78729 break;
78730 }
78731 case CURTYPE_BTREE0: {
78732 if( pCx->isEphemeral ){
78733 if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx);
78734 /* The pCx->pCursor will be close automatically, if it exists, by
78735 ** the call above. */
78736 }else{
78737 assert( pCx->uc.pCursor!=0 )((void) (0));
78738 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
78739 }
78740 break;
78741 }
78742#ifndef SQLITE_OMIT_VIRTUALTABLE
78743 case CURTYPE_VTAB2: {
78744 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
78745 const sqlite3_module *pModule = pVCur->pVtab->pModule;
78746 assert( pVCur->pVtab->nRef>0 )((void) (0));
78747 pVCur->pVtab->nRef--;
78748 pModule->xClose(pVCur);
78749 break;
78750 }
78751#endif
78752 }
78753}
78754
78755/*
78756** Close all cursors in the current frame.
78757*/
78758static void closeCursorsInFrame(Vdbe *p){
78759 if( p->apCsr ){
78760 int i;
78761 for(i=0; i<p->nCursor; i++){
78762 VdbeCursor *pC = p->apCsr[i];
78763 if( pC ){
78764 sqlite3VdbeFreeCursor(p, pC);
78765 p->apCsr[i] = 0;
78766 }
78767 }
78768 }
78769}
78770
78771/*
78772** Copy the values stored in the VdbeFrame structure to its Vdbe. This
78773** is used, for example, when a trigger sub-program is halted to restore
78774** control to the main program.
78775*/
78776SQLITE_PRIVATEstatic int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
78777 Vdbe *v = pFrame->v;
78778 closeCursorsInFrame(v);
78779#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
78780 v->anExec = pFrame->anExec;
78781#endif
78782 v->aOp = pFrame->aOp;
78783 v->nOp = pFrame->nOp;
78784 v->aMem = pFrame->aMem;
78785 v->nMem = pFrame->nMem;
78786 v->apCsr = pFrame->apCsr;
78787 v->nCursor = pFrame->nCursor;
78788 v->db->lastRowid = pFrame->lastRowid;
78789 v->nChange = pFrame->nChange;
78790 v->db->nChange = pFrame->nDbChange;
78791 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
78792 v->pAuxData = pFrame->pAuxData;
78793 pFrame->pAuxData = 0;
78794 return pFrame->pc;
78795}
78796
78797/*
78798** Close all cursors.
78799**
78800** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
78801** cell array. This is necessary as the memory cell array may contain
78802** pointers to VdbeFrame objects, which may in turn contain pointers to
78803** open cursors.
78804*/
78805static void closeAllCursors(Vdbe *p){
78806 if( p->pFrame ){
78807 VdbeFrame *pFrame;
78808 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
78809 sqlite3VdbeFrameRestore(pFrame);
78810 p->pFrame = 0;
78811 p->nFrame = 0;
78812 }
78813 assert( p->nFrame==0 )((void) (0));
78814 closeCursorsInFrame(p);
78815 if( p->aMem ){
78816 releaseMemArray(p->aMem, p->nMem);
78817 }
78818 while( p->pDelFrame ){
78819 VdbeFrame *pDel = p->pDelFrame;
78820 p->pDelFrame = pDel->pParent;
78821 sqlite3VdbeFrameDelete(pDel);
78822 }
78823
78824 /* Delete any auxdata allocations made by the VM */
78825 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
78826 assert( p->pAuxData==0 )((void) (0));
78827}
78828
78829/*
78830** Set the number of result columns that will be returned by this SQL
78831** statement. This is now set at compile time, rather than during
78832** execution of the vdbe program so that sqlite3_column_count() can
78833** be called on an SQL statement before sqlite3_step().
78834*/
78835SQLITE_PRIVATEstatic void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
78836 int n;
78837 sqlite3 *db = p->db;
78838
78839 if( p->nResColumn ){
78840 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N2);
78841 sqlite3DbFree(db, p->aColName);
78842 }
78843 n = nResColumn*COLNAME_N2;
78844 p->nResColumn = (u16)nResColumn;
78845 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
78846 if( p->aColName==0 ) return;
78847 initMemArray(p->aColName, n, db, MEM_Null0x0001);
78848}
78849
78850/*
78851** Set the name of the idx'th column to be returned by the SQL statement.
78852** zName must be a pointer to a nul terminated string.
78853**
78854** This call must be made after a call to sqlite3VdbeSetNumCols().
78855**
78856** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
78857** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
78858** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
78859*/
78860SQLITE_PRIVATEstatic int sqlite3VdbeSetColName(
78861 Vdbe *p, /* Vdbe being configured */
78862 int idx, /* Index of column zName applies to */
78863 int var, /* One of the COLNAME_* constants */
78864 const char *zName, /* Pointer to buffer containing name */
78865 void (*xDel)(void*) /* Memory management strategy for zName */
78866){
78867 int rc;
78868 Mem *pColName;
78869 assert( idx<p->nResColumn )((void) (0));
78870 assert( var<COLNAME_N )((void) (0));
78871 if( p->db->mallocFailed ){
78872 assert( !zName || xDel!=SQLITE_DYNAMIC )((void) (0));
78873 return SQLITE_NOMEM_BKPT7;
78874 }
78875 assert( p->aColName!=0 )((void) (0));
78876 pColName = &(p->aColName[idx+var*p->nResColumn]);
78877 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF81, xDel);
78878 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 )((void) (0));
78879 return rc;
78880}
78881
78882/*
78883** A read or write transaction may or may not be active on database handle
78884** db. If a transaction is active, commit it. If there is a
78885** write-transaction spanning more than one database file, this routine
78886** takes care of the master journal trickery.
78887*/
78888static int vdbeCommit(sqlite3 *db, Vdbe *p){
78889 int i;
78890 int nTrans = 0; /* Number of databases with an active write-transaction
78891 ** that are candidates for a two-phase commit using a
78892 ** master-journal */
78893 int rc = SQLITE_OK0;
78894 int needXcommit = 0;
78895
78896#ifdef SQLITE_OMIT_VIRTUALTABLE
78897 /* With this option, sqlite3VtabSync() is defined to be simply
78898 ** SQLITE_OK so p is not used.
78899 */
78900 UNUSED_PARAMETER(p)(void)(p);
78901#endif
78902
78903 /* Before doing anything else, call the xSync() callback for any
78904 ** virtual module tables written in this transaction. This has to
78905 ** be done before determining whether a master journal file is
78906 ** required, as an xSync() callback may add an attached database
78907 ** to the transaction.
78908 */
78909 rc = sqlite3VtabSync(db, p);
78910
78911 /* This loop determines (a) if the commit hook should be invoked and
78912 ** (b) how many database files have open write transactions, not
78913 ** including the temp database. (b) is important because if more than
78914 ** one database file has an open write transaction, a master journal
78915 ** file is required for an atomic commit.
78916 */
78917 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
78918 Btree *pBt = db->aDb[i].pBt;
78919 if( sqlite3BtreeIsInTrans(pBt) ){
78920 /* Whether or not a database might need a master journal depends upon
78921 ** its journal mode (among other things). This matrix determines which
78922 ** journal modes use a master journal and which do not */
78923 static const u8 aMJNeeded[] = {
78924 /* DELETE */ 1,
78925 /* PERSIST */ 1,
78926 /* OFF */ 0,
78927 /* TRUNCATE */ 1,
78928 /* MEMORY */ 0,
78929 /* WAL */ 0
78930 };
78931 Pager *pPager; /* Pager associated with pBt */
78932 needXcommit = 1;
78933 sqlite3BtreeEnter(pBt);
78934 pPager = sqlite3BtreePager(pBt);
78935 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF0x01
78936 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
78937 && sqlite3PagerIsMemdb(pPager)==0
78938 ){
78939 assert( i!=1 )((void) (0));
78940 nTrans++;
78941 }
78942 rc = sqlite3PagerExclusiveLock(pPager);
78943 sqlite3BtreeLeave(pBt);
78944 }
78945 }
78946 if( rc!=SQLITE_OK0 ){
78947 return rc;
78948 }
78949
78950 /* If there are any write-transactions at all, invoke the commit hook */
78951 if( needXcommit && db->xCommitCallback ){
78952 rc = db->xCommitCallback(db->pCommitArg);
78953 if( rc ){
78954 return SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8));
78955 }
78956 }
78957
78958 /* The simple case - no more than one database file (not counting the
78959 ** TEMP database) has a transaction active. There is no need for the
78960 ** master-journal.
78961 **
78962 ** If the return value of sqlite3BtreeGetFilename() is a zero length
78963 ** string, it means the main database is :memory: or a temp file. In
78964 ** that case we do not support atomic multi-file commits, so use the
78965 ** simple case then too.
78966 */
78967 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
78968 || nTrans<=1
78969 ){
78970 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
78971 Btree *pBt = db->aDb[i].pBt;
78972 if( pBt ){
78973 rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
78974 }
78975 }
78976
78977 /* Do the commit only if all databases successfully complete phase 1.
78978 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
78979 ** IO error while deleting or truncating a journal file. It is unlikely,
78980 ** but could happen. In this case abandon processing and return the error.
78981 */
78982 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
78983 Btree *pBt = db->aDb[i].pBt;
78984 if( pBt ){
78985 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
78986 }
78987 }
78988 if( rc==SQLITE_OK0 ){
78989 sqlite3VtabCommit(db);
78990 }
78991 }
78992
78993 /* The complex case - There is a multi-file write-transaction active.
78994 ** This requires a master journal file to ensure the transaction is
78995 ** committed atomically.
78996 */
78997#ifndef SQLITE_OMIT_DISKIO
78998 else{
78999 sqlite3_vfs *pVfs = db->pVfs;
79000 char *zMaster = 0; /* File-name for the master journal */
79001 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
79002 sqlite3_file *pMaster = 0;
79003 i64 offset = 0;
79004 int res;
79005 int retryCount = 0;
79006 int nMainFile;
79007
79008 /* Select a master journal file name */
79009 nMainFile = sqlite3Strlen30(zMainFile);
79010 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
79011 if( zMaster==0 ) return SQLITE_NOMEM_BKPT7;
79012 do {
79013 u32 iRandom;
79014 if( retryCount ){
79015 if( retryCount>100 ){
79016 sqlite3_log(SQLITE_FULL13, "MJ delete: %s", zMaster);
79017 sqlite3OsDelete(pVfs, zMaster, 0);
79018 break;
79019 }else if( retryCount==1 ){
79020 sqlite3_log(SQLITE_FULL13, "MJ collide: %s", zMaster);
79021 }
79022 }
79023 retryCount++;
79024 sqlite3_randomness(sizeof(iRandom), &iRandom);
79025 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
79026 (iRandom>>8)&0xffffff, iRandom&0xff);
79027 /* The antipenultimate character of the master journal name must
79028 ** be "9" to avoid name collisions when using 8+3 filenames. */
79029 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' )((void) (0));
79030 sqlite3FileSuffix3(zMainFile, zMaster);
79031 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS0, &res);
79032 }while( rc==SQLITE_OK0 && res );
79033 if( rc==SQLITE_OK0 ){
79034 /* Open the master journal. */
79035 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
79036 SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|
79037 SQLITE_OPEN_EXCLUSIVE0x00000010|SQLITE_OPEN_MASTER_JOURNAL0x00004000, 0
79038 );
79039 }
79040 if( rc!=SQLITE_OK0 ){
79041 sqlite3DbFree(db, zMaster);
79042 return rc;
79043 }
79044
79045 /* Write the name of each database file in the transaction into the new
79046 ** master journal file. If an error occurs at this point close
79047 ** and delete the master journal file. All the individual journal files
79048 ** still have 'null' as the master journal pointer, so they will roll
79049 ** back independently if a failure occurs.
79050 */
79051 for(i=0; i<db->nDb; i++){
79052 Btree *pBt = db->aDb[i].pBt;
79053 if( sqlite3BtreeIsInTrans(pBt) ){
79054 char const *zFile = sqlite3BtreeGetJournalname(pBt);
79055 if( zFile==0 ){
79056 continue; /* Ignore TEMP and :memory: databases */
79057 }
79058 assert( zFile[0]!=0 )((void) (0));
79059 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
79060 offset += sqlite3Strlen30(zFile)+1;
79061 if( rc!=SQLITE_OK0 ){
79062 sqlite3OsCloseFree(pMaster);
79063 sqlite3OsDelete(pVfs, zMaster, 0);
79064 sqlite3DbFree(db, zMaster);
79065 return rc;
79066 }
79067 }
79068 }
79069
79070 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
79071 ** flag is set this is not required.
79072 */
79073 if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL0x00000400)
79074 && SQLITE_OK0!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL0x00002))
79075 ){
79076 sqlite3OsCloseFree(pMaster);
79077 sqlite3OsDelete(pVfs, zMaster, 0);
79078 sqlite3DbFree(db, zMaster);
79079 return rc;
79080 }
79081
79082 /* Sync all the db files involved in the transaction. The same call
79083 ** sets the master journal pointer in each individual journal. If
79084 ** an error occurs here, do not delete the master journal file.
79085 **
79086 ** If the error occurs during the first call to
79087 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
79088 ** master journal file will be orphaned. But we cannot delete it,
79089 ** in case the master journal file name was written into the journal
79090 ** file before the failure occurred.
79091 */
79092 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
79093 Btree *pBt = db->aDb[i].pBt;
79094 if( pBt ){
79095 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
79096 }
79097 }
79098 sqlite3OsCloseFree(pMaster);
79099 assert( rc!=SQLITE_BUSY )((void) (0));
79100 if( rc!=SQLITE_OK0 ){
79101 sqlite3DbFree(db, zMaster);
79102 return rc;
79103 }
79104
79105 /* Delete the master journal file. This commits the transaction. After
79106 ** doing this the directory is synced again before any individual
79107 ** transaction files are deleted.
79108 */
79109 rc = sqlite3OsDelete(pVfs, zMaster, 1);
79110 sqlite3DbFree(db, zMaster);
79111 zMaster = 0;
79112 if( rc ){
79113 return rc;
79114 }
79115
79116 /* All files and directories have already been synced, so the following
79117 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
79118 ** deleting or truncating journals. If something goes wrong while
79119 ** this is happening we don't really care. The integrity of the
79120 ** transaction is already guaranteed, but some stray 'cold' journals
79121 ** may be lying around. Returning an error code won't help matters.
79122 */
79123 disable_simulated_io_errors();
79124 sqlite3BeginBenignMalloc();
79125 for(i=0; i<db->nDb; i++){
79126 Btree *pBt = db->aDb[i].pBt;
79127 if( pBt ){
79128 sqlite3BtreeCommitPhaseTwo(pBt, 1);
79129 }
79130 }
79131 sqlite3EndBenignMalloc();
79132 enable_simulated_io_errors();
79133
79134 sqlite3VtabCommit(db);
79135 }
79136#endif
79137
79138 return rc;
79139}
79140
79141/*
79142** This routine checks that the sqlite3.nVdbeActive count variable
79143** matches the number of vdbe's in the list sqlite3.pVdbe that are
79144** currently active. An assertion fails if the two counts do not match.
79145** This is an internal self-check only - it is not an essential processing
79146** step.
79147**
79148** This is a no-op if NDEBUG is defined.
79149*/
79150#ifndef NDEBUG1
79151static void checkActiveVdbeCnt(sqlite3 *db){
79152 Vdbe *p;
79153 int cnt = 0;
79154 int nWrite = 0;
79155 int nRead = 0;
79156 p = db->pVdbe;
79157 while( p ){
79158 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){
79159 cnt++;
79160 if( p->readOnly==0 ) nWrite++;
79161 if( p->bIsReader ) nRead++;
79162 }
79163 p = p->pNext;
79164 }
79165 assert( cnt==db->nVdbeActive )((void) (0));
79166 assert( nWrite==db->nVdbeWrite )((void) (0));
79167 assert( nRead==db->nVdbeRead )((void) (0));
79168}
79169#else
79170#define checkActiveVdbeCnt(x)
79171#endif
79172
79173/*
79174** If the Vdbe passed as the first argument opened a statement-transaction,
79175** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
79176** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
79177** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
79178** statement transaction is committed.
79179**
79180** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
79181** Otherwise SQLITE_OK.
79182*/
79183static SQLITE_NOINLINE__attribute__((noinline)) int vdbeCloseStatement(Vdbe *p, int eOp){
79184 sqlite3 *const db = p->db;
79185 int rc = SQLITE_OK0;
79186 int i;
79187 const int iSavepoint = p->iStatement-1;
79188
79189 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE)((void) (0));
79190 assert( db->nStatement>0 )((void) (0));
79191 assert( p->iStatement==(db->nStatement+db->nSavepoint) )((void) (0));
79192
79193 for(i=0; i<db->nDb; i++){
79194 int rc2 = SQLITE_OK0;
79195 Btree *pBt = db->aDb[i].pBt;
79196 if( pBt ){
79197 if( eOp==SAVEPOINT_ROLLBACK2 ){
79198 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK2, iSavepoint);
79199 }
79200 if( rc2==SQLITE_OK0 ){
79201 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE1, iSavepoint);
79202 }
79203 if( rc==SQLITE_OK0 ){
79204 rc = rc2;
79205 }
79206 }
79207 }
79208 db->nStatement--;
79209 p->iStatement = 0;
79210
79211 if( rc==SQLITE_OK0 ){
79212 if( eOp==SAVEPOINT_ROLLBACK2 ){
79213 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK2, iSavepoint);
79214 }
79215 if( rc==SQLITE_OK0 ){
79216 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE1, iSavepoint);
79217 }
79218 }
79219
79220 /* If the statement transaction is being rolled back, also restore the
79221 ** database handles deferred constraint counter to the value it had when
79222 ** the statement transaction was opened. */
79223 if( eOp==SAVEPOINT_ROLLBACK2 ){
79224 db->nDeferredCons = p->nStmtDefCons;
79225 db->nDeferredImmCons = p->nStmtDefImmCons;
79226 }
79227 return rc;
79228}
79229SQLITE_PRIVATEstatic int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
79230 if( p->db->nStatement && p->iStatement ){
79231 return vdbeCloseStatement(p, eOp);
79232 }
79233 return SQLITE_OK0;
79234}
79235
79236
79237/*
79238** This function is called when a transaction opened by the database
79239** handle associated with the VM passed as an argument is about to be
79240** committed. If there are outstanding deferred foreign key constraint
79241** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
79242**
79243** If there are outstanding FK violations and this function returns
79244** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
79245** and write an error message to it. Then return SQLITE_ERROR.
79246*/
79247#ifndef SQLITE_OMIT_FOREIGN_KEY
79248SQLITE_PRIVATEstatic int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
79249 sqlite3 *db = p->db;
79250 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
79251 || (!deferred && p->nFkConstraint>0)
79252 ){
79253 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
79254 p->errorAction = OE_Abort2;
79255 sqlite3VdbeError(p, "FOREIGN KEY constraint failed");
79256 return SQLITE_ERROR1;
79257 }
79258 return SQLITE_OK0;
79259}
79260#endif
79261
79262/*
79263** This routine is called the when a VDBE tries to halt. If the VDBE
79264** has made changes and is in autocommit mode, then commit those
79265** changes. If a rollback is needed, then do the rollback.
79266**
79267** This routine is the only way to move the state of a VM from
79268** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
79269** call this on a VM that is in the SQLITE_MAGIC_HALT state.
79270**
79271** Return an error code. If the commit could not complete because of
79272** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
79273** means the close did not happen and needs to be repeated.
79274*/
79275SQLITE_PRIVATEstatic int sqlite3VdbeHalt(Vdbe *p){
79276 int rc; /* Used to store transient return codes */
79277 sqlite3 *db = p->db;
79278
79279 /* This function contains the logic that determines if a statement or
79280 ** transaction will be committed or rolled back as a result of the
79281 ** execution of this virtual machine.
79282 **
79283 ** If any of the following errors occur:
79284 **
79285 ** SQLITE_NOMEM
79286 ** SQLITE_IOERR
79287 ** SQLITE_FULL
79288 ** SQLITE_INTERRUPT
79289 **
79290 ** Then the internal cache might have been left in an inconsistent
79291 ** state. We need to rollback the statement transaction, if there is
79292 ** one, or the complete transaction if there is no statement transaction.
79293 */
79294
79295 if( p->magic!=VDBE_MAGIC_RUN0x2df20da3 ){
79296 return SQLITE_OK0;
79297 }
79298 if( db->mallocFailed ){
79299 p->rc = SQLITE_NOMEM_BKPT7;
79300 }
79301 closeAllCursors(p);
79302 checkActiveVdbeCnt(db);
79303
79304 /* No commit or rollback needed if the program never started or if the
79305 ** SQL statement does not read or write a database file. */
79306 if( p->pc>=0 && p->bIsReader ){
79307 int mrc; /* Primary error code from p->rc */
79308 int eStatementOp = 0;
79309 int isSpecialError; /* Set to true if a 'special' error */
79310
79311 /* Lock all btrees used by the statement */
79312 sqlite3VdbeEnter(p);
79313
79314 /* Check for one of the special errors */
79315 mrc = p->rc & 0xff;
79316 isSpecialError = mrc==SQLITE_NOMEM7 || mrc==SQLITE_IOERR10
79317 || mrc==SQLITE_INTERRUPT9 || mrc==SQLITE_FULL13;
79318 if( isSpecialError ){
79319 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
79320 ** no rollback is necessary. Otherwise, at least a savepoint
79321 ** transaction must be rolled back to restore the database to a
79322 ** consistent state.
79323 **
79324 ** Even if the statement is read-only, it is important to perform
79325 ** a statement or transaction rollback operation. If the error
79326 ** occurred while writing to the journal, sub-journal or database
79327 ** file as part of an effort to free up cache space (see function
79328 ** pagerStress() in pager.c), the rollback is required to restore
79329 ** the pager to a consistent state.
79330 */
79331 if( !p->readOnly || mrc!=SQLITE_INTERRUPT9 ){
79332 if( (mrc==SQLITE_NOMEM7 || mrc==SQLITE_FULL13) && p->usesStmtJournal ){
79333 eStatementOp = SAVEPOINT_ROLLBACK2;
79334 }else{
79335 /* We are forced to roll back the active transaction. Before doing
79336 ** so, abort any other statements this handle currently has active.
79337 */
79338 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
79339 sqlite3CloseSavepoints(db);
79340 db->autoCommit = 1;
79341 p->nChange = 0;
79342 }
79343 }
79344 }
79345
79346 /* Check for immediate foreign key violations. */
79347 if( p->rc==SQLITE_OK0 || (p->errorAction==OE_Fail3 && !isSpecialError) ){
79348 sqlite3VdbeCheckFk(p, 0);
79349 }
79350
79351 /* If the auto-commit flag is set and this is the only active writer
79352 ** VM, then we do either a commit or rollback of the current transaction.
79353 **
79354 ** Note: This block also runs if one of the special errors handled
79355 ** above has occurred.
79356 */
79357 if( !sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0)
79358 && db->autoCommit
79359 && db->nVdbeWrite==(p->readOnly==0)
79360 ){
79361 if( p->rc==SQLITE_OK0 || (p->errorAction==OE_Fail3 && !isSpecialError) ){
79362 rc = sqlite3VdbeCheckFk(p, 1);
79363 if( rc!=SQLITE_OK0 ){
79364 if( NEVER(p->readOnly)(p->readOnly) ){
79365 sqlite3VdbeLeave(p);
79366 return SQLITE_ERROR1;
79367 }
79368 rc = SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8));
79369 }else{
79370 /* The auto-commit flag is true, the vdbe program was successful
79371 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
79372 ** key constraints to hold up the transaction. This means a commit
79373 ** is required. */
79374 rc = vdbeCommit(db, p);
79375 }
79376 if( rc==SQLITE_BUSY5 && p->readOnly ){
79377 sqlite3VdbeLeave(p);
79378 return SQLITE_BUSY5;
79379 }else if( rc!=SQLITE_OK0 ){
79380 p->rc = rc;
79381 sqlite3RollbackAll(db, SQLITE_OK0);
79382 p->nChange = 0;
79383 }else{
79384 db->nDeferredCons = 0;
79385 db->nDeferredImmCons = 0;
79386 db->flags &= ~(u64)SQLITE_DeferFKs0x00080000;
79387 sqlite3CommitInternalChanges(db);
79388 }
79389 }else{
79390 sqlite3RollbackAll(db, SQLITE_OK0);
79391 p->nChange = 0;
79392 }
79393 db->nStatement = 0;
79394 }else if( eStatementOp==0 ){
79395 if( p->rc==SQLITE_OK0 || p->errorAction==OE_Fail3 ){
79396 eStatementOp = SAVEPOINT_RELEASE1;
79397 }else if( p->errorAction==OE_Abort2 ){
79398 eStatementOp = SAVEPOINT_ROLLBACK2;
79399 }else{
79400 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
79401 sqlite3CloseSavepoints(db);
79402 db->autoCommit = 1;
79403 p->nChange = 0;
79404 }
79405 }
79406
79407 /* If eStatementOp is non-zero, then a statement transaction needs to
79408 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
79409 ** do so. If this operation returns an error, and the current statement
79410 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
79411 ** current statement error code.
79412 */
79413 if( eStatementOp ){
79414 rc = sqlite3VdbeCloseStatement(p, eStatementOp);
79415 if( rc ){
79416 if( p->rc==SQLITE_OK0 || (p->rc&0xff)==SQLITE_CONSTRAINT19 ){
79417 p->rc = rc;
79418 sqlite3DbFree(db, p->zErrMsg);
79419 p->zErrMsg = 0;
79420 }
79421 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
79422 sqlite3CloseSavepoints(db);
79423 db->autoCommit = 1;
79424 p->nChange = 0;
79425 }
79426 }
79427
79428 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
79429 ** has been rolled back, update the database connection change-counter.
79430 */
79431 if( p->changeCntOn ){
79432 if( eStatementOp!=SAVEPOINT_ROLLBACK2 ){
79433 sqlite3VdbeSetChanges(db, p->nChange);
79434 }else{
79435 sqlite3VdbeSetChanges(db, 0);
79436 }
79437 p->nChange = 0;
79438 }
79439
79440 /* Release the locks */
79441 sqlite3VdbeLeave(p);
79442 }
79443
79444 /* We have successfully halted and closed the VM. Record this fact. */
79445 if( p->pc>=0 ){
79446 db->nVdbeActive--;
79447 if( !p->readOnly ) db->nVdbeWrite--;
79448 if( p->bIsReader ) db->nVdbeRead--;
79449 assert( db->nVdbeActive>=db->nVdbeRead )((void) (0));
79450 assert( db->nVdbeRead>=db->nVdbeWrite )((void) (0));
79451 assert( db->nVdbeWrite>=0 )((void) (0));
79452 }
79453 p->magic = VDBE_MAGIC_HALT0x319c2973;
79454 checkActiveVdbeCnt(db);
79455 if( db->mallocFailed ){
79456 p->rc = SQLITE_NOMEM_BKPT7;
79457 }
79458
79459 /* If the auto-commit flag is set to true, then any locks that were held
79460 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
79461 ** to invoke any required unlock-notify callbacks.
79462 */
79463 if( db->autoCommit ){
79464 sqlite3ConnectionUnlocked(db);
79465 }
79466
79467 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 )((void) (0));
79468 return (p->rc==SQLITE_BUSY5 ? SQLITE_BUSY5 : SQLITE_OK0);
79469}
79470
79471
79472/*
79473** Each VDBE holds the result of the most recent sqlite3_step() call
79474** in p->rc. This routine sets that result back to SQLITE_OK.
79475*/
79476SQLITE_PRIVATEstatic void sqlite3VdbeResetStepResult(Vdbe *p){
79477 p->rc = SQLITE_OK0;
79478}
79479
79480/*
79481** Copy the error code and error message belonging to the VDBE passed
79482** as the first argument to its database handle (so that they will be
79483** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
79484**
79485** This function does not clear the VDBE error code or message, just
79486** copies them to the database handle.
79487*/
79488SQLITE_PRIVATEstatic int sqlite3VdbeTransferError(Vdbe *p){
79489 sqlite3 *db = p->db;
79490 int rc = p->rc;
79491 if( p->zErrMsg ){
79492 db->bBenignMalloc++;
79493 sqlite3BeginBenignMalloc();
79494 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
79495 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF81, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
79496 sqlite3EndBenignMalloc();
79497 db->bBenignMalloc--;
79498 }else if( db->pErr ){
79499 sqlite3ValueSetNull(db->pErr);
79500 }
79501 db->errCode = rc;
79502 return rc;
79503}
79504
79505#ifdef SQLITE_ENABLE_SQLLOG
79506/*
79507** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
79508** invoke it.
79509*/
79510static void vdbeInvokeSqllog(Vdbe *v){
79511 if( sqlite3GlobalConfigsqlite3Config.xSqllog && v->rc==SQLITE_OK0 && v->zSql && v->pc>=0 ){
79512 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
79513 assert( v->db->init.busy==0 )((void) (0));
79514 if( zExpanded ){
79515 sqlite3GlobalConfigsqlite3Config.xSqllog(
79516 sqlite3GlobalConfigsqlite3Config.pSqllogArg, v->db, zExpanded, 1
79517 );
79518 sqlite3DbFree(v->db, zExpanded);
79519 }
79520 }
79521}
79522#else
79523# define vdbeInvokeSqllog(x)
79524#endif
79525
79526/*
79527** Clean up a VDBE after execution but do not delete the VDBE just yet.
79528** Write any error messages into *pzErrMsg. Return the result code.
79529**
79530** After this routine is run, the VDBE should be ready to be executed
79531** again.
79532**
79533** To look at it another way, this routine resets the state of the
79534** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
79535** VDBE_MAGIC_INIT.
79536*/
79537SQLITE_PRIVATEstatic int sqlite3VdbeReset(Vdbe *p){
79538#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
79539 int i;
79540#endif
79541
79542 sqlite3 *db;
79543 db = p->db;
79544
79545 /* If the VM did not run to completion or if it encountered an
79546 ** error, then it might not have been halted properly. So halt
79547 ** it now.
79548 */
79549 sqlite3VdbeHalt(p);
79550
79551 /* If the VDBE has been run even partially, then transfer the error code
79552 ** and error message from the VDBE into the main database structure. But
79553 ** if the VDBE has just been set to run but has not actually executed any
79554 ** instructions yet, leave the main database error information unchanged.
79555 */
79556 if( p->pc>=0 ){
79557 vdbeInvokeSqllog(p);
79558 sqlite3VdbeTransferError(p);
79559 if( p->runOnlyOnce ) p->expired = 1;
79560 }else if( p->rc && p->expired ){
79561 /* The expired flag was set on the VDBE before the first call
79562 ** to sqlite3_step(). For consistency (since sqlite3_step() was
79563 ** called), set the database error in this case as well.
79564 */
79565 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
79566 }
79567
79568 /* Reset register contents and reclaim error message memory.
79569 */
79570#ifdef SQLITE_DEBUG
79571 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
79572 ** Vdbe.aMem[] arrays have already been cleaned up. */
79573 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 )((void) (0));
79574 if( p->aMem ){
79575 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined )((void) (0));
79576 }
79577#endif
79578 sqlite3DbFree(db, p->zErrMsg);
79579 p->zErrMsg = 0;
79580 p->pResultSet = 0;
79581#ifdef SQLITE_DEBUG
79582 p->nWrite = 0;
79583#endif
79584
79585 /* Save profiling information from this VDBE run.
79586 */
79587#ifdef VDBE_PROFILE
79588 {
79589 FILE *out = fopen("vdbe_profile.out", "a");
79590 if( out ){
79591 fprintf(out, "---- ");
79592 for(i=0; i<p->nOp; i++){
79593 fprintf(out, "%02x", p->aOp[i].opcode);
79594 }
79595 fprintf(out, "\n");
79596 if( p->zSql ){
79597 char c, pc = 0;
79598 fprintf(out, "-- ");
79599 for(i=0; (c = p->zSql[i])!=0; i++){
79600 if( pc=='\n' ) fprintf(out, "-- ");
79601 putc(c, out);
79602 pc = c;
79603 }
79604 if( pc!='\n' ) fprintf(out, "\n");
79605 }
79606 for(i=0; i<p->nOp; i++){
79607 char zHdr[100];
79608 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
79609 p->aOp[i].cnt,
79610 p->aOp[i].cycles,
79611 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
79612 );
79613 fprintf(out, "%s", zHdr);
79614 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
79615 }
79616 fclose(out);
79617 }
79618 }
79619#endif
79620 p->magic = VDBE_MAGIC_RESET0x48fa9f76;
79621 return p->rc & db->errMask;
79622}
79623
79624/*
79625** Clean up and delete a VDBE after execution. Return an integer which is
79626** the result code. Write any error message text into *pzErrMsg.
79627*/
79628SQLITE_PRIVATEstatic int sqlite3VdbeFinalize(Vdbe *p){
79629 int rc = SQLITE_OK0;
79630 if( p->magic==VDBE_MAGIC_RUN0x2df20da3 || p->magic==VDBE_MAGIC_HALT0x319c2973 ){
79631 rc = sqlite3VdbeReset(p);
79632 assert( (rc & p->db->errMask)==rc )((void) (0));
79633 }
79634 sqlite3VdbeDelete(p);
79635 return rc;
79636}
79637
79638/*
79639** If parameter iOp is less than zero, then invoke the destructor for
79640** all auxiliary data pointers currently cached by the VM passed as
79641** the first argument.
79642**
79643** Or, if iOp is greater than or equal to zero, then the destructor is
79644** only invoked for those auxiliary data pointers created by the user
79645** function invoked by the OP_Function opcode at instruction iOp of
79646** VM pVdbe, and only then if:
79647**
79648** * the associated function parameter is the 32nd or later (counting
79649** from left to right), or
79650**
79651** * the corresponding bit in argument mask is clear (where the first
79652** function parameter corresponds to bit 0 etc.).
79653*/
79654SQLITE_PRIVATEstatic void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){
79655 while( *pp ){
79656 AuxData *pAux = *pp;
79657 if( (iOp<0)
79658 || (pAux->iAuxOp==iOp
79659 && pAux->iAuxArg>=0
79660 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg)(((unsigned int)1)<<(pAux->iAuxArg)))))
79661 ){
79662 testcase( pAux->iAuxArg==31 );
79663 if( pAux->xDeleteAux ){
79664 pAux->xDeleteAux(pAux->pAux);
79665 }
79666 *pp = pAux->pNextAux;
79667 sqlite3DbFree(db, pAux);
79668 }else{
79669 pp= &pAux->pNextAux;
79670 }
79671 }
79672}
79673
79674/*
79675** Free all memory associated with the Vdbe passed as the second argument,
79676** except for object itself, which is preserved.
79677**
79678** The difference between this function and sqlite3VdbeDelete() is that
79679** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
79680** the database connection and frees the object itself.
79681*/
79682SQLITE_PRIVATEstatic void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
79683 SubProgram *pSub, *pNext;
79684 assert( p->db==0 || p->db==db )((void) (0));
79685 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N2);
79686 for(pSub=p->pProgram; pSub; pSub=pNext){
79687 pNext = pSub->pNext;
79688 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
79689 sqlite3DbFree(db, pSub);
79690 }
79691 if( p->magic!=VDBE_MAGIC_INIT0x16bceaa5 ){
79692 releaseMemArray(p->aVar, p->nVar);
79693 sqlite3DbFree(db, p->pVList);
79694 sqlite3DbFree(db, p->pFree);
79695 }
79696 vdbeFreeOpArray(db, p->aOp, p->nOp);
79697 sqlite3DbFree(db, p->aColName);
79698 sqlite3DbFree(db, p->zSql);
79699#ifdef SQLITE_ENABLE_NORMALIZE
79700 sqlite3DbFree(db, p->zNormSql);
79701 {
79702 DblquoteStr *pThis, *pNext;
79703 for(pThis=p->pDblStr; pThis; pThis=pNext){
79704 pNext = pThis->pNextStr;
79705 sqlite3DbFree(db, pThis);
79706 }
79707 }
79708#endif
79709#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
79710 {
79711 int i;
79712 for(i=0; i<p->nScan; i++){
79713 sqlite3DbFree(db, p->aScan[i].zName);
79714 }
79715 sqlite3DbFree(db, p->aScan);
79716 }
79717#endif
79718}
79719
79720/*
79721** Delete an entire VDBE.
79722*/
79723SQLITE_PRIVATEstatic void sqlite3VdbeDelete(Vdbe *p){
79724 sqlite3 *db;
79725
79726 assert( p!=0 )((void) (0));
79727 db = p->db;
79728 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
79729 sqlite3VdbeClearObject(db, p);
79730 if( p->pPrev ){
79731 p->pPrev->pNext = p->pNext;
79732 }else{
79733 assert( db->pVdbe==p )((void) (0));
79734 db->pVdbe = p->pNext;
79735 }
79736 if( p->pNext ){
79737 p->pNext->pPrev = p->pPrev;
79738 }
79739 p->magic = VDBE_MAGIC_DEAD0x5606c3c8;
79740 p->db = 0;
79741 sqlite3DbFreeNN(db, p);
79742}
79743
79744/*
79745** The cursor "p" has a pending seek operation that has not yet been
79746** carried out. Seek the cursor now. If an error occurs, return
79747** the appropriate error code.
79748*/
79749static int SQLITE_NOINLINE__attribute__((noinline)) handleDeferredMoveto(VdbeCursor *p){
79750 int res, rc;
79751#ifdef SQLITE_TEST
79752 extern int sqlite3_search_count;
79753#endif
79754 assert( p->deferredMoveto )((void) (0));
79755 assert( p->isTable )((void) (0));
79756 assert( p->eCurType==CURTYPE_BTREE )((void) (0));
79757 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
79758 if( rc ) return rc;
79759 if( res!=0 ) return SQLITE_CORRUPT_BKPTsqlite3CorruptError(79759);
79760#ifdef SQLITE_TEST
79761 sqlite3_search_count++;
79762#endif
79763 p->deferredMoveto = 0;
79764 p->cacheStatus = CACHE_STALE0;
79765 return SQLITE_OK0;
79766}
79767
79768/*
79769** Something has moved cursor "p" out of place. Maybe the row it was
79770** pointed to was deleted out from under it. Or maybe the btree was
79771** rebalanced. Whatever the cause, try to restore "p" to the place it
79772** is supposed to be pointing. If the row was deleted out from under the
79773** cursor, set the cursor to point to a NULL row.
79774*/
79775static int SQLITE_NOINLINE__attribute__((noinline)) handleMovedCursor(VdbeCursor *p){
79776 int isDifferentRow, rc;
79777 assert( p->eCurType==CURTYPE_BTREE )((void) (0));
79778 assert( p->uc.pCursor!=0 )((void) (0));
79779 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) )((void) (0));
79780 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
79781 p->cacheStatus = CACHE_STALE0;
79782 if( isDifferentRow ) p->nullRow = 1;
79783 return rc;
79784}
79785
79786/*
79787** Check to ensure that the cursor is valid. Restore the cursor
79788** if need be. Return any I/O error from the restore operation.
79789*/
79790SQLITE_PRIVATEstatic int sqlite3VdbeCursorRestore(VdbeCursor *p){
79791 assert( p->eCurType==CURTYPE_BTREE )((void) (0));
79792 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
79793 return handleMovedCursor(p);
79794 }
79795 return SQLITE_OK0;
79796}
79797
79798/*
79799** Make sure the cursor p is ready to read or write the row to which it
79800** was last positioned. Return an error code if an OOM fault or I/O error
79801** prevents us from positioning the cursor to its correct position.
79802**
79803** If a MoveTo operation is pending on the given cursor, then do that
79804** MoveTo now. If no move is pending, check to see if the row has been
79805** deleted out from under the cursor and if it has, mark the row as
79806** a NULL row.
79807**
79808** If the cursor is already pointing to the correct row and that row has
79809** not been deleted out from under the cursor, then this routine is a no-op.
79810*/
79811SQLITE_PRIVATEstatic int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
79812 VdbeCursor *p = *pp;
79813 assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO )((void) (0));
79814 if( p->deferredMoveto ){
79815 int iMap;
79816 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
79817 *pp = p->pAltCursor;
79818 *piCol = iMap - 1;
79819 return SQLITE_OK0;
79820 }
79821 return handleDeferredMoveto(p);
79822 }
79823 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
79824 return handleMovedCursor(p);
79825 }
79826 return SQLITE_OK0;
79827}
79828
79829/*
79830** The following functions:
79831**
79832** sqlite3VdbeSerialType()
79833** sqlite3VdbeSerialTypeLen()
79834** sqlite3VdbeSerialLen()
79835** sqlite3VdbeSerialPut()
79836** sqlite3VdbeSerialGet()
79837**
79838** encapsulate the code that serializes values for storage in SQLite
79839** data and index records. Each serialized value consists of a
79840** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
79841** integer, stored as a varint.
79842**
79843** In an SQLite index record, the serial type is stored directly before
79844** the blob of data that it corresponds to. In a table record, all serial
79845** types are stored at the start of the record, and the blobs of data at
79846** the end. Hence these functions allow the caller to handle the
79847** serial-type and data blob separately.
79848**
79849** The following table describes the various storage classes for data:
79850**
79851** serial type bytes of data type
79852** -------------- --------------- ---------------
79853** 0 0 NULL
79854** 1 1 signed integer
79855** 2 2 signed integer
79856** 3 3 signed integer
79857** 4 4 signed integer
79858** 5 6 signed integer
79859** 6 8 signed integer
79860** 7 8 IEEE float
79861** 8 0 Integer constant 0
79862** 9 0 Integer constant 1
79863** 10,11 reserved for expansion
79864** N>=12 and even (N-12)/2 BLOB
79865** N>=13 and odd (N-13)/2 text
79866**
79867** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
79868** of SQLite will not understand those serial types.
79869*/
79870
79871/*
79872** Return the serial-type for the value stored in pMem.
79873**
79874** This routine might convert a large MEM_IntReal value into MEM_Real.
79875*/
79876SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
79877 int flags = pMem->flags;
79878 u32 n;
79879
79880 assert( pLen!=0 )((void) (0));
79881 if( flags&MEM_Null0x0001 ){
79882 *pLen = 0;
79883 return 0;
79884 }
79885 if( flags&(MEM_Int0x0004|MEM_IntReal0x0020) ){
79886 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
79887# define MAX_6BYTE((((i64)0x00008000)<<32)-1) ((((i64)0x00008000)<<32)-1)
79888 i64 i = pMem->u.i;
79889 u64 u;
79890 testcase( flags & MEM_Int );
79891 testcase( flags & MEM_IntReal );
79892 if( i<0 ){
79893 u = ~i;
79894 }else{
79895 u = i;
79896 }
79897 if( u<=127 ){
79898 if( (i&1)==i && file_format>=4 ){
79899 *pLen = 0;
79900 return 8+(u32)u;
79901 }else{
79902 *pLen = 1;
79903 return 1;
79904 }
79905 }
79906 if( u<=32767 ){ *pLen = 2; return 2; }
79907 if( u<=8388607 ){ *pLen = 3; return 3; }
79908 if( u<=2147483647 ){ *pLen = 4; return 4; }
79909 if( u<=MAX_6BYTE((((i64)0x00008000)<<32)-1) ){ *pLen = 6; return 5; }
79910 *pLen = 8;
79911 if( flags&MEM_IntReal0x0020 ){
79912 /* If the value is IntReal and is going to take up 8 bytes to store
79913 ** as an integer, then we might as well make it an 8-byte floating
79914 ** point value */
79915 pMem->u.r = (double)pMem->u.i;
79916 pMem->flags &= ~MEM_IntReal0x0020;
79917 pMem->flags |= MEM_Real0x0008;
79918 return 7;
79919 }
79920 return 6;
79921 }
79922 if( flags&MEM_Real0x0008 ){
79923 *pLen = 8;
79924 return 7;
79925 }
79926 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) )((void) (0));
79927 assert( pMem->n>=0 )((void) (0));
79928 n = (u32)pMem->n;
79929 if( flags & MEM_Zero0x4000 ){
79930 n += pMem->u.nZero;
79931 }
79932 *pLen = n;
79933 return ((n*2) + 12 + ((flags&MEM_Str0x0002)!=0));
79934}
79935
79936/*
79937** The sizes for serial types less than 128
79938*/
79939static const u8 sqlite3SmallTypeSizes[] = {
79940 /* 0 1 2 3 4 5 6 7 8 9 */
79941/* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0,
79942/* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
79943/* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
79944/* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
79945/* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18,
79946/* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23,
79947/* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28,
79948/* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33,
79949/* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
79950/* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43,
79951/* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48,
79952/* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53,
79953/* 120 */ 54, 54, 55, 55, 56, 56, 57, 57
79954};
79955
79956/*
79957** Return the length of the data corresponding to the supplied serial-type.
79958*/
79959SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
79960 if( serial_type>=128 ){
79961 return (serial_type-12)/2;
79962 }else{
79963 assert( serial_type<12((void) (0))
79964 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 )((void) (0));
79965 return sqlite3SmallTypeSizes[serial_type];
79966 }
79967}
79968SQLITE_PRIVATEstatic u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){
79969 assert( serial_type<128 )((void) (0));
79970 return sqlite3SmallTypeSizes[serial_type];
79971}
79972
79973/*
79974** If we are on an architecture with mixed-endian floating
79975** points (ex: ARM7) then swap the lower 4 bytes with the
79976** upper 4 bytes. Return the result.
79977**
79978** For most architectures, this is a no-op.
79979**
79980** (later): It is reported to me that the mixed-endian problem
79981** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
79982** that early versions of GCC stored the two words of a 64-bit
79983** float in the wrong order. And that error has been propagated
79984** ever since. The blame is not necessarily with GCC, though.
79985** GCC might have just copying the problem from a prior compiler.
79986** I am also told that newer versions of GCC that follow a different
79987** ABI get the byte order right.
79988**
79989** Developers using SQLite on an ARM7 should compile and run their
79990** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
79991** enabled, some asserts below will ensure that the byte order of
79992** floating point values is correct.
79993**
79994** (2007-08-30) Frank van Vugt has studied this problem closely
79995** and has send his findings to the SQLite developers. Frank
79996** writes that some Linux kernels offer floating point hardware
79997** emulation that uses only 32-bit mantissas instead of a full
79998** 48-bits as required by the IEEE standard. (This is the
79999** CONFIG_FPE_FASTFPE option.) On such systems, floating point
80000** byte swapping becomes very complicated. To avoid problems,
80001** the necessary byte swapping is carried out using a 64-bit integer
80002** rather than a 64-bit float. Frank assures us that the code here
80003** works for him. We, the developers, have no way to independently
80004** verify this, but Frank seems to know what he is talking about
80005** so we trust him.
80006*/
80007#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
80008static u64 floatSwap(u64 in){
80009 union {
80010 u64 r;
80011 u32 i[2];
80012 } u;
80013 u32 t;
80014
80015 u.r = in;
80016 t = u.i[0];
80017 u.i[0] = u.i[1];
80018 u.i[1] = t;
80019 return u.r;
80020}
80021# define swapMixedEndianFloat(X) X = floatSwap(X)
80022#else
80023# define swapMixedEndianFloat(X)
80024#endif
80025
80026/*
80027** Write the serialized data blob for the value stored in pMem into
80028** buf. It is assumed that the caller has allocated sufficient space.
80029** Return the number of bytes written.
80030**
80031** nBuf is the amount of space left in buf[]. The caller is responsible
80032** for allocating enough space to buf[] to hold the entire field, exclusive
80033** of the pMem->u.nZero bytes for a MEM_Zero value.
80034**
80035** Return the number of bytes actually written into buf[]. The number
80036** of bytes in the zero-filled tail is included in the return value only
80037** if those bytes were zeroed in buf[].
80038*/
80039SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
80040 u32 len;
80041
80042 /* Integer and Real */
80043 if( serial_type<=7 && serial_type>0 ){
80044 u64 v;
80045 u32 i;
80046 if( serial_type==7 ){
80047 assert( sizeof(v)==sizeof(pMem->u.r) )((void) (0));
80048 memcpy(&v, &pMem->u.r, sizeof(v));
80049 swapMixedEndianFloat(v);
80050 }else{
80051 v = pMem->u.i;
80052 }
80053 len = i = sqlite3SmallTypeSizes[serial_type];
80054 assert( i>0 )((void) (0));
80055 do{
80056 buf[--i] = (u8)(v&0xFF);
80057 v >>= 8;
80058 }while( i );
80059 return len;
80060 }
80061
80062 /* String or blob */
80063 if( serial_type>=12 ){
80064 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)((void) (0))
80065 == (int)sqlite3VdbeSerialTypeLen(serial_type) )((void) (0));
80066 len = pMem->n;
80067 if( len>0 ) memcpy(buf, pMem->z, len);
80068 return len;
80069 }
80070
80071 /* NULL or constants 0 or 1 */
80072 return 0;
80073}
80074
80075/* Input "x" is a sequence of unsigned characters that represent a
80076** big-endian integer. Return the equivalent native integer
80077*/
80078#define ONE_BYTE_INT(x)((i8)(x)[0]) ((i8)(x)[0])
80079#define TWO_BYTE_INT(x)(256*(i8)((x)[0])|(x)[1]) (256*(i8)((x)[0])|(x)[1])
80080#define THREE_BYTE_INT(x)(65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
80081#define FOUR_BYTE_UINT(x)(((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)
|(x)[3])
(((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
80082#define FOUR_BYTE_INT(x)(16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(
x)[3])
(16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
80083
80084/*
80085** Deserialize the data blob pointed to by buf as serial type serial_type
80086** and store the result in pMem. Return the number of bytes read.
80087**
80088** This function is implemented as two separate routines for performance.
80089** The few cases that require local variables are broken out into a separate
80090** routine so that in most cases the overhead of moving the stack pointer
80091** is avoided.
80092*/
80093static u32 serialGet(
80094 const unsigned char *buf, /* Buffer to deserialize from */
80095 u32 serial_type, /* Serial type to deserialize */
80096 Mem *pMem /* Memory cell to write value into */
80097){
80098 u64 x = FOUR_BYTE_UINT(buf)(((u32)(buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
80099 u32 y = FOUR_BYTE_UINT(buf+4)(((u32)(buf+4)[0]<<24)|((buf+4)[1]<<16)|((buf+4)[
2]<<8)|(buf+4)[3])
;
80100 x = (x<<32) + y;
80101 if( serial_type==6 ){
80102 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
80103 ** twos-complement integer. */
80104 pMem->u.i = *(i64*)&x;
80105 pMem->flags = MEM_Int0x0004;
80106 testcase( pMem->u.i<0 );
80107 }else{
80108 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
80109 ** floating point number. */
80110#if !defined(NDEBUG1) && !defined(SQLITE_OMIT_FLOATING_POINT)
80111 /* Verify that integers and floating point values use the same
80112 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
80113 ** defined that 64-bit floating point values really are mixed
80114 ** endian.
80115 */
80116 static const u64 t1 = ((u64)0x3ff00000)<<32;
80117 static const double r1 = 1.0;
80118 u64 t2 = t1;
80119 swapMixedEndianFloat(t2);
80120 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 )((void) (0));
80121#endif
80122 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 )((void) (0));
80123 swapMixedEndianFloat(x);
80124 memcpy(&pMem->u.r, &x, sizeof(x));
80125 pMem->flags = IsNaN(x)(((x)&(((u64)0x7ff)<<52))==(((u64)0x7ff)<<52)
&& ((x)&((((u64)1)<<52)-1))!=0)
? MEM_Null0x0001 : MEM_Real0x0008;
80126 }
80127 return 8;
80128}
80129SQLITE_PRIVATEstatic u32 sqlite3VdbeSerialGet(
80130 const unsigned char *buf, /* Buffer to deserialize from */
80131 u32 serial_type, /* Serial type to deserialize */
80132 Mem *pMem /* Memory cell to write value into */
80133){
80134 switch( serial_type ){
80135 case 10: { /* Internal use only: NULL with virtual table
80136 ** UPDATE no-change flag set */
80137 pMem->flags = MEM_Null0x0001|MEM_Zero0x4000;
80138 pMem->n = 0;
80139 pMem->u.nZero = 0;
80140 break;
80141 }
80142 case 11: /* Reserved for future use */
80143 case 0: { /* Null */
80144 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
80145 pMem->flags = MEM_Null0x0001;
80146 break;
80147 }
80148 case 1: {
80149 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
80150 ** integer. */
80151 pMem->u.i = ONE_BYTE_INT(buf)((i8)(buf)[0]);
80152 pMem->flags = MEM_Int0x0004;
80153 testcase( pMem->u.i<0 );
80154 return 1;
80155 }
80156 case 2: { /* 2-byte signed integer */
80157 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
80158 ** twos-complement integer. */
80159 pMem->u.i = TWO_BYTE_INT(buf)(256*(i8)((buf)[0])|(buf)[1]);
80160 pMem->flags = MEM_Int0x0004;
80161 testcase( pMem->u.i<0 );
80162 return 2;
80163 }
80164 case 3: { /* 3-byte signed integer */
80165 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
80166 ** twos-complement integer. */
80167 pMem->u.i = THREE_BYTE_INT(buf)(65536*(i8)((buf)[0])|((buf)[1]<<8)|(buf)[2]);
80168 pMem->flags = MEM_Int0x0004;
80169 testcase( pMem->u.i<0 );
80170 return 3;
80171 }
80172 case 4: { /* 4-byte signed integer */
80173 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
80174 ** twos-complement integer. */
80175 pMem->u.i = FOUR_BYTE_INT(buf)(16777216*(i8)((buf)[0])|((buf)[1]<<16)|((buf)[2]<<
8)|(buf)[3])
;
80176#ifdef __HP_cc
80177 /* Work around a sign-extension bug in the HP compiler for HP/UX */
80178 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
80179#endif
80180 pMem->flags = MEM_Int0x0004;
80181 testcase( pMem->u.i<0 );
80182 return 4;
80183 }
80184 case 5: { /* 6-byte signed integer */
80185 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
80186 ** twos-complement integer. */
80187 pMem->u.i = FOUR_BYTE_UINT(buf+2)(((u32)(buf+2)[0]<<24)|((buf+2)[1]<<16)|((buf+2)[
2]<<8)|(buf+2)[3])
+ (((i64)1)<<32)*TWO_BYTE_INT(buf)(256*(i8)((buf)[0])|(buf)[1]);
80188 pMem->flags = MEM_Int0x0004;
80189 testcase( pMem->u.i<0 );
80190 return 6;
80191 }
80192 case 6: /* 8-byte signed integer */
80193 case 7: { /* IEEE floating point */
80194 /* These use local variables, so do them in a separate routine
80195 ** to avoid having to move the frame pointer in the common case */
80196 return serialGet(buf,serial_type,pMem);
80197 }
80198 case 8: /* Integer 0 */
80199 case 9: { /* Integer 1 */
80200 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
80201 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
80202 pMem->u.i = serial_type-8;
80203 pMem->flags = MEM_Int0x0004;
80204 return 0;
80205 }
80206 default: {
80207 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
80208 ** length.
80209 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
80210 ** (N-13)/2 bytes in length. */
80211 static const u16 aFlag[] = { MEM_Blob0x0010|MEM_Ephem0x1000, MEM_Str0x0002|MEM_Ephem0x1000 };
80212 pMem->z = (char *)buf;
80213 pMem->n = (serial_type-12)/2;
80214 pMem->flags = aFlag[serial_type&1];
80215 return pMem->n;
80216 }
80217 }
80218 return 0;
80219}
80220/*
80221** This routine is used to allocate sufficient space for an UnpackedRecord
80222** structure large enough to be used with sqlite3VdbeRecordUnpack() if
80223** the first argument is a pointer to KeyInfo structure pKeyInfo.
80224**
80225** The space is either allocated using sqlite3DbMallocRaw() or from within
80226** the unaligned buffer passed via the second and third arguments (presumably
80227** stack space). If the former, then *ppFree is set to a pointer that should
80228** be eventually freed by the caller using sqlite3DbFree(). Or, if the
80229** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
80230** before returning.
80231**
80232** If an OOM error occurs, NULL is returned.
80233*/
80234SQLITE_PRIVATEstatic UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
80235 KeyInfo *pKeyInfo /* Description of the record */
80236){
80237 UnpackedRecord *p; /* Unpacked record to return */
80238 int nByte; /* Number of bytes required for *p */
80239 nByte = ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
80240 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
80241 if( !p ) return 0;
80242 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))(((sizeof(UnpackedRecord))+7)&~7)];
80243 assert( pKeyInfo->aSortOrder!=0 )((void) (0));
80244 p->pKeyInfo = pKeyInfo;
80245 p->nField = pKeyInfo->nKeyField + 1;
80246 return p;
80247}
80248
80249/*
80250** Given the nKey-byte encoding of a record in pKey[], populate the
80251** UnpackedRecord structure indicated by the fourth argument with the
80252** contents of the decoded record.
80253*/
80254SQLITE_PRIVATEstatic void sqlite3VdbeRecordUnpack(
80255 KeyInfo *pKeyInfo, /* Information about the record format */
80256 int nKey, /* Size of the binary record */
80257 const void *pKey, /* The binary record */
80258 UnpackedRecord *p /* Populate this structure before returning. */
80259){
80260 const unsigned char *aKey = (const unsigned char *)pKey;
80261 u32 d;
80262 u32 idx; /* Offset in aKey[] to read from */
80263 u16 u; /* Unsigned loop counter */
80264 u32 szHdr;
80265 Mem *pMem = p->aMem;
80266
80267 p->default_rc = 0;
80268 assert( EIGHT_BYTE_ALIGNMENT(pMem) )((void) (0));
80269 idx = getVarint32(aKey, szHdr)(u8)((*(aKey)<(u8)0x80)?((szHdr)=(u32)*(aKey)),1:sqlite3GetVarint32
((aKey),(u32 *)&(szHdr)))
;
80270 d = szHdr;
80271 u = 0;
80272 while( idx<szHdr && d<=(u32)nKey ){
80273 u32 serial_type;
80274
80275 idx += getVarint32(&aKey[idx], serial_type)(u8)((*(&aKey[idx])<(u8)0x80)?((serial_type)=(u32)*(&
aKey[idx])),1:sqlite3GetVarint32((&aKey[idx]),(u32 *)&
(serial_type)))
;
80276 pMem->enc = pKeyInfo->enc;
80277 pMem->db = pKeyInfo->db;
80278 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
80279 pMem->szMalloc = 0;
80280 pMem->z = 0;
80281 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
80282 pMem++;
80283 if( (++u)>=p->nField ) break;
80284 }
80285 if( d>(u32)nKey && u ){
80286 assert( CORRUPT_DB )((void) (0));
80287 /* In a corrupt record entry, the last pMem might have been set up using
80288 ** uninitialized memory. Overwrite its value with NULL, to prevent
80289 ** warnings from MSAN. */
80290 sqlite3VdbeMemSetNull(pMem-1);
80291 }
80292 assert( u<=pKeyInfo->nKeyField + 1 )((void) (0));
80293 p->nField = u;
80294}
80295
80296#ifdef SQLITE_DEBUG
80297/*
80298** This function compares two index or table record keys in the same way
80299** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
80300** this function deserializes and compares values using the
80301** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
80302** in assert() statements to ensure that the optimized code in
80303** sqlite3VdbeRecordCompare() returns results with these two primitives.
80304**
80305** Return true if the result of comparison is equivalent to desiredResult.
80306** Return false if there is a disagreement.
80307*/
80308static int vdbeRecordCompareDebug(
80309 int nKey1, const void *pKey1, /* Left key */
80310 const UnpackedRecord *pPKey2, /* Right key */
80311 int desiredResult /* Correct answer */
80312){
80313 u32 d1; /* Offset into aKey[] of next data element */
80314 u32 idx1; /* Offset into aKey[] of next header element */
80315 u32 szHdr1; /* Number of bytes in header */
80316 int i = 0;
80317 int rc = 0;
80318 const unsigned char *aKey1 = (const unsigned char *)pKey1;
80319 KeyInfo *pKeyInfo;
80320 Mem mem1;
80321
80322 pKeyInfo = pPKey2->pKeyInfo;
80323 if( pKeyInfo->db==0 ) return 1;
80324 mem1.enc = pKeyInfo->enc;
80325 mem1.db = pKeyInfo->db;
80326 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
80327 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
80328
80329 /* Compilers may complain that mem1.u.i is potentially uninitialized.
80330 ** We could initialize it, as shown here, to silence those complaints.
80331 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
80332 ** the unnecessary initialization has a measurable negative performance
80333 ** impact, since this routine is a very high runner. And so, we choose
80334 ** to ignore the compiler warnings and leave this variable uninitialized.
80335 */
80336 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
80337
80338 idx1 = getVarint32(aKey1, szHdr1)(u8)((*(aKey1)<(u8)0x80)?((szHdr1)=(u32)*(aKey1)),1:sqlite3GetVarint32
((aKey1),(u32 *)&(szHdr1)))
;
80339 if( szHdr1>98307 ) return SQLITE_CORRUPT11;
80340 d1 = szHdr1;
80341 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB )((void) (0));
80342 assert( pKeyInfo->aSortOrder!=0 )((void) (0));
80343 assert( pKeyInfo->nKeyField>0 )((void) (0));
80344 assert( idx1<=szHdr1 || CORRUPT_DB )((void) (0));
80345 do{
80346 u32 serial_type1;
80347
80348 /* Read the serial types for the next element in each key. */
80349 idx1 += getVarint32( aKey1+idx1, serial_type1 )(u8)((*(aKey1+idx1)<(u8)0x80)?((serial_type1)=(u32)*(aKey1
+idx1)),1:sqlite3GetVarint32((aKey1+idx1),(u32 *)&(serial_type1
)))
;
80350
80351 /* Verify that there is enough key space remaining to avoid
80352 ** a buffer overread. The "d1+serial_type1+2" subexpression will
80353 ** always be greater than or equal to the amount of required key space.
80354 ** Use that approximation to avoid the more expensive call to
80355 ** sqlite3VdbeSerialTypeLen() in the common case.
80356 */
80357 if( d1+(u64)serial_type1+2>(u64)nKey1
80358 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1
80359 ){
80360 break;
80361 }
80362
80363 /* Extract the values to be compared.
80364 */
80365 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
80366
80367 /* Do the comparison
80368 */
80369 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
80370 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
80371 if( rc!=0 ){
80372 assert( mem1.szMalloc==0 )((void) (0)); /* See comment below */
80373 if( pKeyInfo->aSortOrder[i] ){
80374 rc = -rc; /* Invert the result for DESC sort order. */
80375 }
80376 goto debugCompareEnd;
80377 }
80378 i++;
80379 }while( idx1<szHdr1 && i<pPKey2->nField );
80380
80381 /* No memory allocation is ever used on mem1. Prove this using
80382 ** the following assert(). If the assert() fails, it indicates a
80383 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
80384 */
80385 assert( mem1.szMalloc==0 )((void) (0));
80386
80387 /* rc==0 here means that one of the keys ran out of fields and
80388 ** all the fields up to that point were equal. Return the default_rc
80389 ** value. */
80390 rc = pPKey2->default_rc;
80391
80392debugCompareEnd:
80393 if( desiredResult==0 && rc==0 ) return 1;
80394 if( desiredResult<0 && rc<0 ) return 1;
80395 if( desiredResult>0 && rc>0 ) return 1;
80396 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return 1;
80397 if( pKeyInfo->db->mallocFailed ) return 1;
80398 return 0;
80399}
80400#endif
80401
80402#ifdef SQLITE_DEBUG
80403/*
80404** Count the number of fields (a.k.a. columns) in the record given by
80405** pKey,nKey. The verify that this count is less than or equal to the
80406** limit given by pKeyInfo->nAllField.
80407**
80408** If this constraint is not satisfied, it means that the high-speed
80409** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will
80410** not work correctly. If this assert() ever fires, it probably means
80411** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed
80412** incorrectly.
80413*/
80414static void vdbeAssertFieldCountWithinLimits(
80415 int nKey, const void *pKey, /* The record to verify */
80416 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */
80417){
80418 int nField = 0;
80419 u32 szHdr;
80420 u32 idx;
80421 u32 notUsed;
80422 const unsigned char *aKey = (const unsigned char*)pKey;
80423
80424 if( CORRUPT_DB(sqlite3Config.neverCorrupt==0) ) return;
80425 idx = getVarint32(aKey, szHdr)(u8)((*(aKey)<(u8)0x80)?((szHdr)=(u32)*(aKey)),1:sqlite3GetVarint32
((aKey),(u32 *)&(szHdr)))
;
80426 assert( nKey>=0 )((void) (0));
80427 assert( szHdr<=(u32)nKey )((void) (0));
80428 while( idx<szHdr ){
80429 idx += getVarint32(aKey+idx, notUsed)(u8)((*(aKey+idx)<(u8)0x80)?((notUsed)=(u32)*(aKey+idx)),1
:sqlite3GetVarint32((aKey+idx),(u32 *)&(notUsed)))
;
80430 nField++;
80431 }
80432 assert( nField <= pKeyInfo->nAllField )((void) (0));
80433}
80434#else
80435# define vdbeAssertFieldCountWithinLimits(A,B,C)
80436#endif
80437
80438/*
80439** Both *pMem1 and *pMem2 contain string values. Compare the two values
80440** using the collation sequence pColl. As usual, return a negative , zero
80441** or positive value if *pMem1 is less than, equal to or greater than
80442** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
80443*/
80444static int vdbeCompareMemString(
80445 const Mem *pMem1,
80446 const Mem *pMem2,
80447 const CollSeq *pColl,
80448 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */
80449){
80450 if( pMem1->enc==pColl->enc ){
80451 /* The strings are already in the correct encoding. Call the
80452 ** comparison function directly */
80453 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
80454 }else{
80455 int rc;
80456 const void *v1, *v2;
80457 Mem c1;
80458 Mem c2;
80459 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null0x0001);
80460 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null0x0001);
80461 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem0x1000);
80462 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem0x1000);
80463 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
80464 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
80465 if( (v1==0 || v2==0) ){
80466 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT7;
80467 rc = 0;
80468 }else{
80469 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
80470 }
80471 sqlite3VdbeMemRelease(&c1);
80472 sqlite3VdbeMemRelease(&c2);
80473 return rc;
80474 }
80475}
80476
80477/*
80478** The input pBlob is guaranteed to be a Blob that is not marked
80479** with MEM_Zero. Return true if it could be a zero-blob.
80480*/
80481static int isAllZero(const char *z, int n){
80482 int i;
80483 for(i=0; i<n; i++){
80484 if( z[i] ) return 0;
80485 }
80486 return 1;
80487}
80488
80489/*
80490** Compare two blobs. Return negative, zero, or positive if the first
80491** is less than, equal to, or greater than the second, respectively.
80492** If one blob is a prefix of the other, then the shorter is the lessor.
80493*/
80494SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){
80495 int c;
80496 int n1 = pB1->n;
80497 int n2 = pB2->n;
80498
80499 /* It is possible to have a Blob value that has some non-zero content
80500 ** followed by zero content. But that only comes up for Blobs formed
80501 ** by the OP_MakeRecord opcode, and such Blobs never get passed into
80502 ** sqlite3MemCompare(). */
80503 assert( (pB1->flags & MEM_Zero)==0 || n1==0 )((void) (0));
80504 assert( (pB2->flags & MEM_Zero)==0 || n2==0 )((void) (0));
80505
80506 if( (pB1->flags|pB2->flags) & MEM_Zero0x4000 ){
80507 if( pB1->flags & pB2->flags & MEM_Zero0x4000 ){
80508 return pB1->u.nZero - pB2->u.nZero;
80509 }else if( pB1->flags & MEM_Zero0x4000 ){
80510 if( !isAllZero(pB2->z, pB2->n) ) return -1;
80511 return pB1->u.nZero - n2;
80512 }else{
80513 if( !isAllZero(pB1->z, pB1->n) ) return +1;
80514 return n1 - pB2->u.nZero;
80515 }
80516 }
80517 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
80518 if( c ) return c;
80519 return n1 - n2;
80520}
80521
80522/*
80523** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
80524** number. Return negative, zero, or positive if the first (i64) is less than,
80525** equal to, or greater than the second (double).
80526*/
80527static int sqlite3IntFloatCompare(i64 i, double r){
80528 if( sizeof(LONGDOUBLE_TYPElong double)>8 ){
80529 LONGDOUBLE_TYPElong double x = (LONGDOUBLE_TYPElong double)i;
80530 if( x<r ) return -1;
80531 if( x>r ) return +1;
80532 return 0;
80533 }else{
80534 i64 y;
80535 double s;
80536 if( r<-9223372036854775808.0 ) return +1;
80537 if( r>=9223372036854775808.0 ) return -1;
80538 y = (i64)r;
80539 if( i<y ) return -1;
80540 if( i>y ) return +1;
80541 s = (double)i;
80542 if( s<r ) return -1;
80543 if( s>r ) return +1;
80544 return 0;
80545 }
80546}
80547
80548/*
80549** Compare the values contained by the two memory cells, returning
80550** negative, zero or positive if pMem1 is less than, equal to, or greater
80551** than pMem2. Sorting order is NULL's first, followed by numbers (integers
80552** and reals) sorted numerically, followed by text ordered by the collating
80553** sequence pColl and finally blob's ordered by memcmp().
80554**
80555** Two NULL values are considered equal by this function.
80556*/
80557SQLITE_PRIVATEstatic int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
80558 int f1, f2;
80559 int combined_flags;
80560
80561 f1 = pMem1->flags;
80562 f2 = pMem2->flags;
80563 combined_flags = f1|f2;
80564 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) )((void) (0));
80565
80566 /* If one value is NULL, it is less than the other. If both values
80567 ** are NULL, return 0.
80568 */
80569 if( combined_flags&MEM_Null0x0001 ){
80570 return (f2&MEM_Null0x0001) - (f1&MEM_Null0x0001);
80571 }
80572
80573 /* At least one of the two values is a number
80574 */
80575 if( combined_flags&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020) ){
80576 testcase( combined_flags & MEM_Int );
80577 testcase( combined_flags & MEM_Real );
80578 testcase( combined_flags & MEM_IntReal );
80579 if( (f1 & f2 & (MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
80580 testcase( f1 & f2 & MEM_Int );
80581 testcase( f1 & f2 & MEM_IntReal );
80582 if( pMem1->u.i < pMem2->u.i ) return -1;
80583 if( pMem1->u.i > pMem2->u.i ) return +1;
80584 return 0;
80585 }
80586 if( (f1 & f2 & MEM_Real0x0008)!=0 ){
80587 if( pMem1->u.r < pMem2->u.r ) return -1;
80588 if( pMem1->u.r > pMem2->u.r ) return +1;
80589 return 0;
80590 }
80591 if( (f1&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
80592 testcase( f1 & MEM_Int );
80593 testcase( f1 & MEM_IntReal );
80594 if( (f2&MEM_Real0x0008)!=0 ){
80595 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
80596 }else if( (f2&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
80597 if( pMem1->u.i < pMem2->u.i ) return -1;
80598 if( pMem1->u.i > pMem2->u.i ) return +1;
80599 return 0;
80600 }else{
80601 return -1;
80602 }
80603 }
80604 if( (f1&MEM_Real0x0008)!=0 ){
80605 if( (f2&(MEM_Int0x0004|MEM_IntReal0x0020))!=0 ){
80606 testcase( f2 & MEM_Int );
80607 testcase( f2 & MEM_IntReal );
80608 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
80609 }else{
80610 return -1;
80611 }
80612 }
80613 return +1;
80614 }
80615
80616 /* If one value is a string and the other is a blob, the string is less.
80617 ** If both are strings, compare using the collating functions.
80618 */
80619 if( combined_flags&MEM_Str0x0002 ){
80620 if( (f1 & MEM_Str0x0002)==0 ){
80621 return 1;
80622 }
80623 if( (f2 & MEM_Str0x0002)==0 ){
80624 return -1;
80625 }
80626
80627 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed )((void) (0));
80628 assert( pMem1->enc==SQLITE_UTF8 ||((void) (0))
80629 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE )((void) (0));
80630
80631 /* The collation sequence must be defined at this point, even if
80632 ** the user deletes the collation sequence after the vdbe program is
80633 ** compiled (this was not always the case).
80634 */
80635 assert( !pColl || pColl->xCmp )((void) (0));
80636
80637 if( pColl ){
80638 return vdbeCompareMemString(pMem1, pMem2, pColl, 0);
80639 }
80640 /* If a NULL pointer was passed as the collate function, fall through
80641 ** to the blob case and use memcmp(). */
80642 }
80643
80644 /* Both values must be blobs. Compare using memcmp(). */
80645 return sqlite3BlobCompare(pMem1, pMem2);
80646}
80647
80648
80649/*
80650** The first argument passed to this function is a serial-type that
80651** corresponds to an integer - all values between 1 and 9 inclusive
80652** except 7. The second points to a buffer containing an integer value
80653** serialized according to serial_type. This function deserializes
80654** and returns the value.
80655*/
80656static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
80657 u32 y;
80658 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) )((void) (0));
80659 switch( serial_type ){
80660 case 0:
80661 case 1:
80662 testcase( aKey[0]&0x80 );
80663 return ONE_BYTE_INT(aKey)((i8)(aKey)[0]);
80664 case 2:
80665 testcase( aKey[0]&0x80 );
80666 return TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
80667 case 3:
80668 testcase( aKey[0]&0x80 );
80669 return THREE_BYTE_INT(aKey)(65536*(i8)((aKey)[0])|((aKey)[1]<<8)|(aKey)[2]);
80670 case 4: {
80671 testcase( aKey[0]&0x80 );
80672 y = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
80673 return (i64)*(int*)&y;
80674 }
80675 case 5: {
80676 testcase( aKey[0]&0x80 );
80677 return FOUR_BYTE_UINT(aKey+2)(((u32)(aKey+2)[0]<<24)|((aKey+2)[1]<<16)|((aKey+
2)[2]<<8)|(aKey+2)[3])
+ (((i64)1)<<32)*TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
80678 }
80679 case 6: {
80680 u64 x = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
80681 testcase( aKey[0]&0x80 );
80682 x = (x<<32) | FOUR_BYTE_UINT(aKey+4)(((u32)(aKey+4)[0]<<24)|((aKey+4)[1]<<16)|((aKey+
4)[2]<<8)|(aKey+4)[3])
;
80683 return (i64)*(i64*)&x;
80684 }
80685 }
80686
80687 return (serial_type - 8);
80688}
80689
80690/*
80691** This function compares the two table rows or index records
80692** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
80693** or positive integer if key1 is less than, equal to or
80694** greater than key2. The {nKey1, pKey1} key must be a blob
80695** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
80696** key must be a parsed key such as obtained from
80697** sqlite3VdbeParseRecord.
80698**
80699** If argument bSkip is non-zero, it is assumed that the caller has already
80700** determined that the first fields of the keys are equal.
80701**
80702** Key1 and Key2 do not have to contain the same number of fields. If all
80703** fields that appear in both keys are equal, then pPKey2->default_rc is
80704** returned.
80705**
80706** If database corruption is discovered, set pPKey2->errCode to
80707** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
80708** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
80709** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
80710*/
80711SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompareWithSkip(
80712 int nKey1, const void *pKey1, /* Left key */
80713 UnpackedRecord *pPKey2, /* Right key */
80714 int bSkip /* If true, skip the first field */
80715){
80716 u32 d1; /* Offset into aKey[] of next data element */
80717 int i; /* Index of next field to compare */
80718 u32 szHdr1; /* Size of record header in bytes */
80719 u32 idx1; /* Offset of first type in header */
80720 int rc = 0; /* Return value */
80721 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
80722 KeyInfo *pKeyInfo;
80723 const unsigned char *aKey1 = (const unsigned char *)pKey1;
80724 Mem mem1;
80725
80726 /* If bSkip is true, then the caller has already determined that the first
80727 ** two elements in the keys are equal. Fix the various stack variables so
80728 ** that this routine begins comparing at the second field. */
80729 if( bSkip ){
80730 u32 s1;
80731 idx1 = 1 + getVarint32(&aKey1[1], s1)(u8)((*(&aKey1[1])<(u8)0x80)?((s1)=(u32)*(&aKey1[1
])),1:sqlite3GetVarint32((&aKey1[1]),(u32 *)&(s1)))
;
80732 szHdr1 = aKey1[0];
80733 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
80734 i = 1;
80735 pRhs++;
80736 }else{
80737 idx1 = getVarint32(aKey1, szHdr1)(u8)((*(aKey1)<(u8)0x80)?((szHdr1)=(u32)*(aKey1)),1:sqlite3GetVarint32
((aKey1),(u32 *)&(szHdr1)))
;
80738 d1 = szHdr1;
80739 i = 0;
80740 }
80741 if( d1>(unsigned)nKey1 ){
80742 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(80742);
80743 return 0; /* Corruption */
80744 }
80745
80746 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */
80747 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField((void) (0))
80748 || CORRUPT_DB )((void) (0));
80749 assert( pPKey2->pKeyInfo->aSortOrder!=0 )((void) (0));
80750 assert( pPKey2->pKeyInfo->nKeyField>0 )((void) (0));
80751 assert( idx1<=szHdr1 || CORRUPT_DB )((void) (0));
80752 do{
80753 u32 serial_type;
80754
80755 /* RHS is an integer */
80756 if( pRhs->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
80757 testcase( pRhs->flags & MEM_Int );
80758 testcase( pRhs->flags & MEM_IntReal );
80759 serial_type = aKey1[idx1];
80760 testcase( serial_type==12 );
80761 if( serial_type>=10 ){
80762 rc = +1;
80763 }else if( serial_type==0 ){
80764 rc = -1;
80765 }else if( serial_type==7 ){
80766 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
80767 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
80768 }else{
80769 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
80770 i64 rhs = pRhs->u.i;
80771 if( lhs<rhs ){
80772 rc = -1;
80773 }else if( lhs>rhs ){
80774 rc = +1;
80775 }
80776 }
80777 }
80778
80779 /* RHS is real */
80780 else if( pRhs->flags & MEM_Real0x0008 ){
80781 serial_type = aKey1[idx1];
80782 if( serial_type>=10 ){
80783 /* Serial types 12 or greater are strings and blobs (greater than
80784 ** numbers). Types 10 and 11 are currently "reserved for future
80785 ** use", so it doesn't really matter what the results of comparing
80786 ** them to numberic values are. */
80787 rc = +1;
80788 }else if( serial_type==0 ){
80789 rc = -1;
80790 }else{
80791 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
80792 if( serial_type==7 ){
80793 if( mem1.u.r<pRhs->u.r ){
80794 rc = -1;
80795 }else if( mem1.u.r>pRhs->u.r ){
80796 rc = +1;
80797 }
80798 }else{
80799 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
80800 }
80801 }
80802 }
80803
80804 /* RHS is a string */
80805 else if( pRhs->flags & MEM_Str0x0002 ){
80806 getVarint32(&aKey1[idx1], serial_type)(u8)((*(&aKey1[idx1])<(u8)0x80)?((serial_type)=(u32)*(
&aKey1[idx1])),1:sqlite3GetVarint32((&aKey1[idx1]),(u32
*)&(serial_type)))
;
80807 testcase( serial_type==12 );
80808 if( serial_type<12 ){
80809 rc = -1;
80810 }else if( !(serial_type & 0x01) ){
80811 rc = +1;
80812 }else{
80813 mem1.n = (serial_type - 12) / 2;
80814 testcase( (d1+mem1.n)==(unsigned)nKey1 );
80815 testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
80816 if( (d1+mem1.n) > (unsigned)nKey1
80817 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
80818 ){
80819 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(80819);
80820 return 0; /* Corruption */
80821 }else if( pKeyInfo->aColl[i] ){
80822 mem1.enc = pKeyInfo->enc;
80823 mem1.db = pKeyInfo->db;
80824 mem1.flags = MEM_Str0x0002;
80825 mem1.z = (char*)&aKey1[d1];
80826 rc = vdbeCompareMemString(
80827 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
80828 );
80829 }else{
80830 int nCmp = MIN(mem1.n, pRhs->n)((mem1.n)<(pRhs->n)?(mem1.n):(pRhs->n));
80831 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
80832 if( rc==0 ) rc = mem1.n - pRhs->n;
80833 }
80834 }
80835 }
80836
80837 /* RHS is a blob */
80838 else if( pRhs->flags & MEM_Blob0x0010 ){
80839 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 )((void) (0));
80840 getVarint32(&aKey1[idx1], serial_type)(u8)((*(&aKey1[idx1])<(u8)0x80)?((serial_type)=(u32)*(
&aKey1[idx1])),1:sqlite3GetVarint32((&aKey1[idx1]),(u32
*)&(serial_type)))
;
80841 testcase( serial_type==12 );
80842 if( serial_type<12 || (serial_type & 0x01) ){
80843 rc = -1;
80844 }else{
80845 int nStr = (serial_type - 12) / 2;
80846 testcase( (d1+nStr)==(unsigned)nKey1 );
80847 testcase( (d1+nStr+1)==(unsigned)nKey1 );
80848 if( (d1+nStr) > (unsigned)nKey1 ){
80849 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(80849);
80850 return 0; /* Corruption */
80851 }else if( pRhs->flags & MEM_Zero0x4000 ){
80852 if( !isAllZero((const char*)&aKey1[d1],nStr) ){
80853 rc = 1;
80854 }else{
80855 rc = nStr - pRhs->u.nZero;
80856 }
80857 }else{
80858 int nCmp = MIN(nStr, pRhs->n)((nStr)<(pRhs->n)?(nStr):(pRhs->n));
80859 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
80860 if( rc==0 ) rc = nStr - pRhs->n;
80861 }
80862 }
80863 }
80864
80865 /* RHS is null */
80866 else{
80867 serial_type = aKey1[idx1];
80868 rc = (serial_type!=0);
80869 }
80870
80871 if( rc!=0 ){
80872 if( pPKey2->pKeyInfo->aSortOrder[i] ){
80873 rc = -rc;
80874 }
80875 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) )((void) (0));
80876 assert( mem1.szMalloc==0 )((void) (0)); /* See comment below */
80877 return rc;
80878 }
80879
80880 i++;
80881 if( i==pPKey2->nField ) break;
80882 pRhs++;
80883 d1 += sqlite3VdbeSerialTypeLen(serial_type);
80884 idx1 += sqlite3VarintLen(serial_type);
80885 }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 );
80886
80887 /* No memory allocation is ever used on mem1. Prove this using
80888 ** the following assert(). If the assert() fails, it indicates a
80889 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
80890 assert( mem1.szMalloc==0 )((void) (0));
80891
80892 /* rc==0 here means that one or both of the keys ran out of fields and
80893 ** all the fields up to that point were equal. Return the default_rc
80894 ** value. */
80895 assert( CORRUPT_DB((void) (0))
80896 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)((void) (0))
80897 || pPKey2->pKeyInfo->db->mallocFailed((void) (0))
80898 )((void) (0));
80899 pPKey2->eqSeen = 1;
80900 return pPKey2->default_rc;
80901}
80902SQLITE_PRIVATEstatic int sqlite3VdbeRecordCompare(
80903 int nKey1, const void *pKey1, /* Left key */
80904 UnpackedRecord *pPKey2 /* Right key */
80905){
80906 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0);
80907}
80908
80909
80910/*
80911** This function is an optimized version of sqlite3VdbeRecordCompare()
80912** that (a) the first field of pPKey2 is an integer, and (b) the
80913** size-of-header varint at the start of (pKey1/nKey1) fits in a single
80914** byte (i.e. is less than 128).
80915**
80916** To avoid concerns about buffer overreads, this routine is only used
80917** on schemas where the maximum valid header size is 63 bytes or less.
80918*/
80919static int vdbeRecordCompareInt(
80920 int nKey1, const void *pKey1, /* Left key */
80921 UnpackedRecord *pPKey2 /* Right key */
80922){
80923 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
80924 int serial_type = ((const u8*)pKey1)[1];
80925 int res;
80926 u32 y;
80927 u64 x;
80928 i64 v;
80929 i64 lhs;
80930
80931 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
80932 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB )((void) (0));
80933 switch( serial_type ){
80934 case 1: { /* 1-byte signed integer */
80935 lhs = ONE_BYTE_INT(aKey)((i8)(aKey)[0]);
80936 testcase( lhs<0 );
80937 break;
80938 }
80939 case 2: { /* 2-byte signed integer */
80940 lhs = TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
80941 testcase( lhs<0 );
80942 break;
80943 }
80944 case 3: { /* 3-byte signed integer */
80945 lhs = THREE_BYTE_INT(aKey)(65536*(i8)((aKey)[0])|((aKey)[1]<<8)|(aKey)[2]);
80946 testcase( lhs<0 );
80947 break;
80948 }
80949 case 4: { /* 4-byte signed integer */
80950 y = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
80951 lhs = (i64)*(int*)&y;
80952 testcase( lhs<0 );
80953 break;
80954 }
80955 case 5: { /* 6-byte signed integer */
80956 lhs = FOUR_BYTE_UINT(aKey+2)(((u32)(aKey+2)[0]<<24)|((aKey+2)[1]<<16)|((aKey+
2)[2]<<8)|(aKey+2)[3])
+ (((i64)1)<<32)*TWO_BYTE_INT(aKey)(256*(i8)((aKey)[0])|(aKey)[1]);
80957 testcase( lhs<0 );
80958 break;
80959 }
80960 case 6: { /* 8-byte signed integer */
80961 x = FOUR_BYTE_UINT(aKey)(((u32)(aKey)[0]<<24)|((aKey)[1]<<16)|((aKey)[2]<<
8)|(aKey)[3])
;
80962 x = (x<<32) | FOUR_BYTE_UINT(aKey+4)(((u32)(aKey+4)[0]<<24)|((aKey+4)[1]<<16)|((aKey+
4)[2]<<8)|(aKey+4)[3])
;
80963 lhs = *(i64*)&x;
80964 testcase( lhs<0 );
80965 break;
80966 }
80967 case 8:
80968 lhs = 0;
80969 break;
80970 case 9:
80971 lhs = 1;
80972 break;
80973
80974 /* This case could be removed without changing the results of running
80975 ** this code. Including it causes gcc to generate a faster switch
80976 ** statement (since the range of switch targets now starts at zero and
80977 ** is contiguous) but does not cause any duplicate code to be generated
80978 ** (as gcc is clever enough to combine the two like cases). Other
80979 ** compilers might be similar. */
80980 case 0: case 7:
80981 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
80982
80983 default:
80984 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2);
80985 }
80986
80987 v = pPKey2->aMem[0].u.i;
80988 if( v>lhs ){
80989 res = pPKey2->r1;
80990 }else if( v<lhs ){
80991 res = pPKey2->r2;
80992 }else if( pPKey2->nField>1 ){
80993 /* The first fields of the two keys are equal. Compare the trailing
80994 ** fields. */
80995 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
80996 }else{
80997 /* The first fields of the two keys are equal and there are no trailing
80998 ** fields. Return pPKey2->default_rc in this case. */
80999 res = pPKey2->default_rc;
81000 pPKey2->eqSeen = 1;
81001 }
81002
81003 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) )((void) (0));
81004 return res;
81005}
81006
81007/*
81008** This function is an optimized version of sqlite3VdbeRecordCompare()
81009** that (a) the first field of pPKey2 is a string, that (b) the first field
81010** uses the collation sequence BINARY and (c) that the size-of-header varint
81011** at the start of (pKey1/nKey1) fits in a single byte.
81012*/
81013static int vdbeRecordCompareString(
81014 int nKey1, const void *pKey1, /* Left key */
81015 UnpackedRecord *pPKey2 /* Right key */
81016){
81017 const u8 *aKey1 = (const u8*)pKey1;
81018 int serial_type;
81019 int res;
81020
81021 assert( pPKey2->aMem[0].flags & MEM_Str )((void) (0));
81022 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
81023 getVarint32(&aKey1[1], serial_type)(u8)((*(&aKey1[1])<(u8)0x80)?((serial_type)=(u32)*(&
aKey1[1])),1:sqlite3GetVarint32((&aKey1[1]),(u32 *)&(
serial_type)))
;
81024 if( serial_type<12 ){
81025 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
81026 }else if( !(serial_type & 0x01) ){
81027 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
81028 }else{
81029 int nCmp;
81030 int nStr;
81031 int szHdr = aKey1[0];
81032
81033 nStr = (serial_type-12) / 2;
81034 if( (szHdr + nStr) > nKey1 ){
81035 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPTsqlite3CorruptError(81035);
81036 return 0; /* Corruption */
81037 }
81038 nCmp = MIN( pPKey2->aMem[0].n, nStr )((pPKey2->aMem[0].n)<(nStr)?(pPKey2->aMem[0].n):(nStr
))
;
81039 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
81040
81041 if( res==0 ){
81042 res = nStr - pPKey2->aMem[0].n;
81043 if( res==0 ){
81044 if( pPKey2->nField>1 ){
81045 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
81046 }else{
81047 res = pPKey2->default_rc;
81048 pPKey2->eqSeen = 1;
81049 }
81050 }else if( res>0 ){
81051 res = pPKey2->r2;
81052 }else{
81053 res = pPKey2->r1;
81054 }
81055 }else if( res>0 ){
81056 res = pPKey2->r2;
81057 }else{
81058 res = pPKey2->r1;
81059 }
81060 }
81061
81062 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)((void) (0))
81063 || CORRUPT_DB((void) (0))
81064 || pPKey2->pKeyInfo->db->mallocFailed((void) (0))
81065 )((void) (0));
81066 return res;
81067}
81068
81069/*
81070** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
81071** suitable for comparing serialized records to the unpacked record passed
81072** as the only argument.
81073*/
81074SQLITE_PRIVATEstatic RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
81075 /* varintRecordCompareInt() and varintRecordCompareString() both assume
81076 ** that the size-of-header varint that occurs at the start of each record
81077 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
81078 ** also assumes that it is safe to overread a buffer by at least the
81079 ** maximum possible legal header size plus 8 bytes. Because there is
81080 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
81081 ** buffer passed to varintRecordCompareInt() this makes it convenient to
81082 ** limit the size of the header to 64 bytes in cases where the first field
81083 ** is an integer.
81084 **
81085 ** The easiest way to enforce this limit is to consider only records with
81086 ** 13 fields or less. If the first field is an integer, the maximum legal
81087 ** header size is (12*5 + 1 + 1) bytes. */
81088 if( p->pKeyInfo->nAllField<=13 ){
81089 int flags = p->aMem[0].flags;
81090 if( p->pKeyInfo->aSortOrder[0] ){
81091 p->r1 = 1;
81092 p->r2 = -1;
81093 }else{
81094 p->r1 = -1;
81095 p->r2 = 1;
81096 }
81097 if( (flags & MEM_Int0x0004) ){
81098 return vdbeRecordCompareInt;
81099 }
81100 testcase( flags & MEM_Real );
81101 testcase( flags & MEM_Null );
81102 testcase( flags & MEM_Blob );
81103 if( (flags & (MEM_Real0x0008|MEM_IntReal0x0020|MEM_Null0x0001|MEM_Blob0x0010))==0
81104 && p->pKeyInfo->aColl[0]==0
81105 ){
81106 assert( flags & MEM_Str )((void) (0));
81107 return vdbeRecordCompareString;
81108 }
81109 }
81110
81111 return sqlite3VdbeRecordCompare;
81112}
81113
81114/*
81115** pCur points at an index entry created using the OP_MakeRecord opcode.
81116** Read the rowid (the last field in the record) and store it in *rowid.
81117** Return SQLITE_OK if everything works, or an error code otherwise.
81118**
81119** pCur might be pointing to text obtained from a corrupt database file.
81120** So the content cannot be trusted. Do appropriate checks on the content.
81121*/
81122SQLITE_PRIVATEstatic int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
81123 i64 nCellKey = 0;
81124 int rc;
81125 u32 szHdr; /* Size of the header */
81126 u32 typeRowid; /* Serial type of the rowid */
81127 u32 lenRowid; /* Size of the rowid */
81128 Mem m, v;
81129
81130 /* Get the size of the index entry. Only indices entries of less
81131 ** than 2GiB are support - anything large must be database corruption.
81132 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
81133 ** this code can safely assume that nCellKey is 32-bits
81134 */
81135 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
81136 nCellKey = sqlite3BtreePayloadSize(pCur);
81137 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey )((void) (0));
81138
81139 /* Read in the complete content of the index entry */
81140 sqlite3VdbeMemInit(&m, db, 0);
81141 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
81142 if( rc ){
81143 return rc;
81144 }
81145
81146 /* The index entry must begin with a header size */
81147 (void)getVarint32((u8*)m.z, szHdr)(u8)((*((u8*)m.z)<(u8)0x80)?((szHdr)=(u32)*((u8*)m.z)),1:sqlite3GetVarint32
(((u8*)m.z),(u32 *)&(szHdr)))
;
81148 testcase( szHdr==3 );
81149 testcase( szHdr==m.n );
81150 testcase( szHdr>0x7fffffff );
81151 assert( m.n>=0 )((void) (0));
81152 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n)(szHdr<3 || szHdr>(unsigned)m.n) ){
81153 goto idx_rowid_corruption;
81154 }
81155
81156 /* The last field of the index should be an integer - the ROWID.
81157 ** Verify that the last entry really is an integer. */
81158 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid)(u8)((*((u8*)&m.z[szHdr-1])<(u8)0x80)?((typeRowid)=(u32
)*((u8*)&m.z[szHdr-1])),1:sqlite3GetVarint32(((u8*)&m
.z[szHdr-1]),(u32 *)&(typeRowid)))
;
81159 testcase( typeRowid==1 );
81160 testcase( typeRowid==2 );
81161 testcase( typeRowid==3 );
81162 testcase( typeRowid==4 );
81163 testcase( typeRowid==5 );
81164 testcase( typeRowid==6 );
81165 testcase( typeRowid==8 );
81166 testcase( typeRowid==9 );
81167 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7)(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
81168 goto idx_rowid_corruption;
81169 }
81170 lenRowid = sqlite3SmallTypeSizes[typeRowid];
81171 testcase( (u32)m.n==szHdr+lenRowid );
81172 if( unlikely((u32)m.n<szHdr+lenRowid)((u32)m.n<szHdr+lenRowid) ){
81173 goto idx_rowid_corruption;
81174 }
81175
81176 /* Fetch the integer off the end of the index record */
81177 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
81178 *rowid = v.u.i;
81179 sqlite3VdbeMemRelease(&m);
81180 return SQLITE_OK0;
81181
81182 /* Jump here if database corruption is detected after m has been
81183 ** allocated. Free the m object and return SQLITE_CORRUPT. */
81184idx_rowid_corruption:
81185 testcase( m.szMalloc!=0 );
81186 sqlite3VdbeMemRelease(&m);
81187 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(81187);
81188}
81189
81190/*
81191** Compare the key of the index entry that cursor pC is pointing to against
81192** the key string in pUnpacked. Write into *pRes a number
81193** that is negative, zero, or positive if pC is less than, equal to,
81194** or greater than pUnpacked. Return SQLITE_OK on success.
81195**
81196** pUnpacked is either created without a rowid or is truncated so that it
81197** omits the rowid at the end. The rowid at the end of the index entry
81198** is ignored as well. Hence, this routine only compares the prefixes
81199** of the keys prior to the final rowid, not the entire key.
81200*/
81201SQLITE_PRIVATEstatic int sqlite3VdbeIdxKeyCompare(
81202 sqlite3 *db, /* Database connection */
81203 VdbeCursor *pC, /* The cursor to compare against */
81204 UnpackedRecord *pUnpacked, /* Unpacked version of key */
81205 int *res /* Write the comparison result here */
81206){
81207 i64 nCellKey = 0;
81208 int rc;
81209 BtCursor *pCur;
81210 Mem m;
81211
81212 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
81213 pCur = pC->uc.pCursor;
81214 assert( sqlite3BtreeCursorIsValid(pCur) )((void) (0));
81215 nCellKey = sqlite3BtreePayloadSize(pCur);
81216 /* nCellKey will always be between 0 and 0xffffffff because of the way
81217 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
81218 if( nCellKey<=0 || nCellKey>0x7fffffff ){
81219 *res = 0;
81220 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(81220);
81221 }
81222 sqlite3VdbeMemInit(&m, db, 0);
81223 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m);
81224 if( rc ){
81225 return rc;
81226 }
81227 *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0);
81228 sqlite3VdbeMemRelease(&m);
81229 return SQLITE_OK0;
81230}
81231
81232/*
81233** This routine sets the value to be returned by subsequent calls to
81234** sqlite3_changes() on the database handle 'db'.
81235*/
81236SQLITE_PRIVATEstatic void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
81237 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
81238 db->nChange = nChange;
81239 db->nTotalChange += nChange;
81240}
81241
81242/*
81243** Set a flag in the vdbe to update the change counter when it is finalised
81244** or reset.
81245*/
81246SQLITE_PRIVATEstatic void sqlite3VdbeCountChanges(Vdbe *v){
81247 v->changeCntOn = 1;
81248}
81249
81250/*
81251** Mark every prepared statement associated with a database connection
81252** as expired.
81253**
81254** An expired statement means that recompilation of the statement is
81255** recommend. Statements expire when things happen that make their
81256** programs obsolete. Removing user-defined functions or collating
81257** sequences, or changing an authorization function are the types of
81258** things that make prepared statements obsolete.
81259**
81260** If iCode is 1, then expiration is advisory. The statement should
81261** be reprepared before being restarted, but if it is already running
81262** it is allowed to run to completion.
81263**
81264** Internally, this function just sets the Vdbe.expired flag on all
81265** prepared statements. The flag is set to 1 for an immediate expiration
81266** and set to 2 for an advisory expiration.
81267*/
81268SQLITE_PRIVATEstatic void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){
81269 Vdbe *p;
81270 for(p = db->pVdbe; p; p=p->pNext){
81271 p->expired = iCode+1;
81272 }
81273}
81274
81275/*
81276** Return the database associated with the Vdbe.
81277*/
81278SQLITE_PRIVATEstatic sqlite3 *sqlite3VdbeDb(Vdbe *v){
81279 return v->db;
81280}
81281
81282/*
81283** Return the SQLITE_PREPARE flags for a Vdbe.
81284*/
81285SQLITE_PRIVATEstatic u8 sqlite3VdbePrepareFlags(Vdbe *v){
81286 return v->prepFlags;
81287}
81288
81289/*
81290** Return a pointer to an sqlite3_value structure containing the value bound
81291** parameter iVar of VM v. Except, if the value is an SQL NULL, return
81292** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
81293** constants) to the value before returning it.
81294**
81295** The returned value must be freed by the caller using sqlite3ValueFree().
81296*/
81297SQLITE_PRIVATEstatic sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
81298 assert( iVar>0 )((void) (0));
81299 if( v ){
81300 Mem *pMem = &v->aVar[iVar-1];
81301 assert( (v->db->flags & SQLITE_EnableQPSG)==0 )((void) (0));
81302 if( 0==(pMem->flags & MEM_Null0x0001) ){
81303 sqlite3_value *pRet = sqlite3ValueNew(v->db);
81304 if( pRet ){
81305 sqlite3VdbeMemCopy((Mem *)pRet, pMem);
81306 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF81);
81307 }
81308 return pRet;
81309 }
81310 }
81311 return 0;
81312}
81313
81314/*
81315** Configure SQL variable iVar so that binding a new value to it signals
81316** to sqlite3_reoptimize() that re-preparing the statement may result
81317** in a better query plan.
81318*/
81319SQLITE_PRIVATEstatic void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
81320 assert( iVar>0 )((void) (0));
81321 assert( (v->db->flags & SQLITE_EnableQPSG)==0 )((void) (0));
81322 if( iVar>=32 ){
81323 v->expmask |= 0x80000000;
81324 }else{
81325 v->expmask |= ((u32)1 << (iVar-1));
81326 }
81327}
81328
81329/*
81330** Cause a function to throw an error if it was call from OP_PureFunc
81331** rather than OP_Function.
81332**
81333** OP_PureFunc means that the function must be deterministic, and should
81334** throw an error if it is given inputs that would make it non-deterministic.
81335** This routine is invoked by date/time functions that use non-deterministic
81336** features such as 'now'.
81337*/
81338SQLITE_PRIVATEstatic int sqlite3NotPureFunc(sqlite3_context *pCtx){
81339#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81340 if( pCtx->pVdbe==0 ) return 1;
81341#endif
81342 if( pCtx->pVdbe->aOp[pCtx->iOp].opcode==OP_PureFunc64 ){
81343 sqlite3_result_error(pCtx,
81344 "non-deterministic function in index expression or CHECK constraint",
81345 -1);
81346 return 0;
81347 }
81348 return 1;
81349}
81350
81351#ifndef SQLITE_OMIT_VIRTUALTABLE
81352/*
81353** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
81354** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
81355** in memory obtained from sqlite3DbMalloc).
81356*/
81357SQLITE_PRIVATEstatic void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
81358 if( pVtab->zErrMsg ){
81359 sqlite3 *db = p->db;
81360 sqlite3DbFree(db, p->zErrMsg);
81361 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
81362 sqlite3_free(pVtab->zErrMsg);
81363 pVtab->zErrMsg = 0;
81364 }
81365}
81366#endif /* SQLITE_OMIT_VIRTUALTABLE */
81367
81368#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
81369
81370/*
81371** If the second argument is not NULL, release any allocations associated
81372** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
81373** structure itself, using sqlite3DbFree().
81374**
81375** This function is used to free UnpackedRecord structures allocated by
81376** the vdbeUnpackRecord() function found in vdbeapi.c.
81377*/
81378static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){
81379 if( p ){
81380 int i;
81381 for(i=0; i<nField; i++){
81382 Mem *pMem = &p->aMem[i];
81383 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
81384 }
81385 sqlite3DbFreeNN(db, p);
81386 }
81387}
81388#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
81389
81390#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
81391/*
81392** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
81393** then cursor passed as the second argument should point to the row about
81394** to be update or deleted. If the application calls sqlite3_preupdate_old(),
81395** the required value will be read from the row the cursor points to.
81396*/
81397SQLITE_PRIVATEstatic void sqlite3VdbePreUpdateHook(
81398 Vdbe *v, /* Vdbe pre-update hook is invoked by */
81399 VdbeCursor *pCsr, /* Cursor to grab old.* values from */
81400 int op, /* SQLITE_INSERT, UPDATE or DELETE */
81401 const char *zDb, /* Database name */
81402 Table *pTab, /* Modified table */
81403 i64 iKey1, /* Initial key value */
81404 int iReg /* Register for new.* record */
81405){
81406 sqlite3 *db = v->db;
81407 i64 iKey2;
81408 PreUpdate preupdate;
81409 const char *zTbl = pTab->zName;
81410 static const u8 fakeSortOrder = 0;
81411
81412 assert( db->pPreUpdate==0 )((void) (0));
81413 memset(&preupdate, 0, sizeof(PreUpdate));
81414 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0)==0 ){
81415 iKey1 = iKey2 = 0;
81416 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab);
81417 }else{
81418 if( op==SQLITE_UPDATE23 ){
81419 iKey2 = v->aMem[iReg].u.i;
81420 }else{
81421 iKey2 = iKey1;
81422 }
81423 }
81424
81425 assert( pCsr->nField==pTab->nCol((void) (0))
81426 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)((void) (0))
81427 )((void) (0));
81428
81429 preupdate.v = v;
81430 preupdate.pCsr = pCsr;
81431 preupdate.op = op;
81432 preupdate.iNewReg = iReg;
81433 preupdate.keyinfo.db = db;
81434 preupdate.keyinfo.enc = ENC(db)((db)->enc);
81435 preupdate.keyinfo.nKeyField = pTab->nCol;
81436 preupdate.keyinfo.aSortOrder = (u8*)&fakeSortOrder;
81437 preupdate.iKey1 = iKey1;
81438 preupdate.iKey2 = iKey2;
81439 preupdate.pTab = pTab;
81440
81441 db->pPreUpdate = &preupdate;
81442 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
81443 db->pPreUpdate = 0;
81444 sqlite3DbFree(db, preupdate.aRecord);
81445 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked);
81446 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked);
81447 if( preupdate.aNew ){
81448 int i;
81449 for(i=0; i<pCsr->nField; i++){
81450 sqlite3VdbeMemRelease(&preupdate.aNew[i]);
81451 }
81452 sqlite3DbFreeNN(db, preupdate.aNew);
81453 }
81454}
81455#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
81456
81457/************** End of vdbeaux.c *********************************************/
81458/************** Begin file vdbeapi.c *****************************************/
81459/*
81460** 2004 May 26
81461**
81462** The author disclaims copyright to this source code. In place of
81463** a legal notice, here is a blessing:
81464**
81465** May you do good and not evil.
81466** May you find forgiveness for yourself and forgive others.
81467** May you share freely, never taking more than you give.
81468**
81469*************************************************************************
81470**
81471** This file contains code use to implement APIs that are part of the
81472** VDBE.
81473*/
81474/* #include "sqliteInt.h" */
81475/* #include "vdbeInt.h" */
81476
81477#ifndef SQLITE_OMIT_DEPRECATED
81478/*
81479** Return TRUE (non-zero) of the statement supplied as an argument needs
81480** to be recompiled. A statement needs to be recompiled whenever the
81481** execution environment changes in a way that would alter the program
81482** that sqlite3_prepare() generates. For example, if new functions or
81483** collating sequences are registered or if an authorizer function is
81484** added or changed.
81485*/
81486SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
81487 Vdbe *p = (Vdbe*)pStmt;
81488 return p==0 || p->expired;
81489}
81490#endif
81491
81492/*
81493** Check on a Vdbe to make sure it has not been finalized. Log
81494** an error and return true if it has been finalized (or is otherwise
81495** invalid). Return false if it is ok.
81496*/
81497static int vdbeSafety(Vdbe *p){
81498 if( p->db==0 ){
81499 sqlite3_log(SQLITE_MISUSE21, "API called with finalized prepared statement");
81500 return 1;
81501 }else{
81502 return 0;
81503 }
81504}
81505static int vdbeSafetyNotNull(Vdbe *p){
81506 if( p==0 ){
81507 sqlite3_log(SQLITE_MISUSE21, "API called with NULL prepared statement");
81508 return 1;
81509 }else{
81510 return vdbeSafety(p);
81511 }
81512}
81513
81514#ifndef SQLITE_OMIT_TRACE
81515/*
81516** Invoke the profile callback. This routine is only called if we already
81517** know that the profile callback is defined and needs to be invoked.
81518*/
81519static SQLITE_NOINLINE__attribute__((noinline)) void invokeProfileCallback(sqlite3 *db, Vdbe *p){
81520 sqlite3_int64 iNow;
81521 sqlite3_int64 iElapse;
81522 assert( p->startTime>0 )((void) (0));
81523 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 )((void) (0));
81524 assert( db->init.busy==0 )((void) (0));
81525 assert( p->zSql!=0 )((void) (0));
81526 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
81527 iElapse = (iNow - p->startTime)*1000000;
81528#ifndef SQLITE_OMIT_DEPRECATED
81529 if( db->xProfile ){
81530 db->xProfile(db->pProfileArg, p->zSql, iElapse);
81531 }
81532#endif
81533 if( db->mTrace & SQLITE_TRACE_PROFILE0x02 ){
81534 db->xTrace(SQLITE_TRACE_PROFILE0x02, db->pTraceArg, p, (void*)&iElapse);
81535 }
81536 p->startTime = 0;
81537}
81538/*
81539** The checkProfileCallback(DB,P) macro checks to see if a profile callback
81540** is needed, and it invokes the callback if it is needed.
81541*/
81542# define checkProfileCallback(DB,P)if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } \
81543 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
81544#else
81545# define checkProfileCallback(DB,P)if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } /*no-op*/
81546#endif
81547
81548/*
81549** The following routine destroys a virtual machine that is created by
81550** the sqlite3_compile() routine. The integer returned is an SQLITE_
81551** success/failure code that describes the result of executing the virtual
81552** machine.
81553**
81554** This routine sets the error code and string returned by
81555** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
81556*/
81557SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
81558 int rc;
81559 if( pStmt==0 ){
81560 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
81561 ** pointer is a harmless no-op. */
81562 rc = SQLITE_OK0;
81563 }else{
81564 Vdbe *v = (Vdbe*)pStmt;
81565 sqlite3 *db = v->db;
81566 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(81566);
81567 sqlite3_mutex_enter(db->mutex);
81568 checkProfileCallback(db, v)if( ((v)->startTime)>0 ){ invokeProfileCallback(db,v); };
81569 rc = sqlite3VdbeFinalize(v);
81570 rc = sqlite3ApiExit(db, rc);
81571 sqlite3LeaveMutexAndCloseZombie(db);
81572 }
81573 return rc;
81574}
81575
81576/*
81577** Terminate the current execution of an SQL statement and reset it
81578** back to its starting state so that it can be reused. A success code from
81579** the prior execution is returned.
81580**
81581** This routine sets the error code and string returned by
81582** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
81583*/
81584SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
81585 int rc;
81586 if( pStmt==0 ){
81587 rc = SQLITE_OK0;
81588 }else{
81589 Vdbe *v = (Vdbe*)pStmt;
81590 sqlite3 *db = v->db;
81591 sqlite3_mutex_enter(db->mutex);
81592 checkProfileCallback(db, v)if( ((v)->startTime)>0 ){ invokeProfileCallback(db,v); };
81593 rc = sqlite3VdbeReset(v);
81594 sqlite3VdbeRewind(v);
81595 assert( (rc & (db->errMask))==rc )((void) (0));
81596 rc = sqlite3ApiExit(db, rc);
81597 sqlite3_mutex_leave(db->mutex);
81598 }
81599 return rc;
81600}
81601
81602/*
81603** Set all the parameters in the compiled SQL statement to NULL.
81604*/
81605SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
81606 int i;
81607 int rc = SQLITE_OK0;
81608 Vdbe *p = (Vdbe*)pStmt;
81609#if SQLITE_THREADSAFE1
81610 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
81611#endif
81612 sqlite3_mutex_enter(mutex);
81613 for(i=0; i<p->nVar; i++){
81614 sqlite3VdbeMemRelease(&p->aVar[i]);
81615 p->aVar[i].flags = MEM_Null0x0001;
81616 }
81617 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 )((void) (0));
81618 if( p->expmask ){
81619 p->expired = 1;
81620 }
81621 sqlite3_mutex_leave(mutex);
81622 return rc;
81623}
81624
81625
81626/**************************** sqlite3_value_ *******************************
81627** The following routines extract information from a Mem or sqlite3_value
81628** structure.
81629*/
81630SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
81631 Mem *p = (Mem*)pVal;
81632 if( p->flags & (MEM_Blob0x0010|MEM_Str0x0002) ){
81633 if( ExpandBlob(p)(((p)->flags&0x4000)?sqlite3VdbeMemExpandBlob(p):0)!=SQLITE_OK0 ){
81634 assert( p->flags==MEM_Null && p->z==0 )((void) (0));
81635 return 0;
81636 }
81637 p->flags |= MEM_Blob0x0010;
81638 return p->n ? p->z : 0;
81639 }else{
81640 return sqlite3_value_text(pVal);
81641 }
81642}
81643SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
81644 return sqlite3ValueBytes(pVal, SQLITE_UTF81);
81645}
81646SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
81647 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE2);
81648}
81649SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
81650 return sqlite3VdbeRealValue((Mem*)pVal);
81651}
81652SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
81653 return (int)sqlite3VdbeIntValue((Mem*)pVal);
81654}
81655SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
81656 return sqlite3VdbeIntValue((Mem*)pVal);
81657}
81658SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value *pVal){
81659 Mem *pMem = (Mem*)pVal;
81660 return ((pMem->flags & MEM_Subtype0x8000) ? pMem->eSubtype : 0);
81661}
81662SQLITE_API void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){
81663 Mem *p = (Mem*)pVal;
81664 if( (p->flags&(MEM_TypeMask0xc1bf|MEM_Term0x0200|MEM_Subtype0x8000)) ==
81665 (MEM_Null0x0001|MEM_Term0x0200|MEM_Subtype0x8000)
81666 && zPType!=0
81667 && p->eSubtype=='p'
81668 && strcmp(p->u.zPType, zPType)==0
81669 ){
81670 return (void*)p->z;
81671 }else{
81672 return 0;
81673 }
81674}
81675SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
81676 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF81);
81677}
81678#ifndef SQLITE_OMIT_UTF16
81679SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
81680 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE2);
81681}
81682SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
81683 return sqlite3ValueText(pVal, SQLITE_UTF16BE3);
81684}
81685SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
81686 return sqlite3ValueText(pVal, SQLITE_UTF16LE2);
81687}
81688#endif /* SQLITE_OMIT_UTF16 */
81689/* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
81690** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
81691** point number string BLOB NULL
81692*/
81693SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
81694 static const u8 aType[] = {
81695 SQLITE_BLOB4, /* 0x00 (not possible) */
81696 SQLITE_NULL5, /* 0x01 NULL */
81697 SQLITE_TEXT3, /* 0x02 TEXT */
81698 SQLITE_NULL5, /* 0x03 (not possible) */
81699 SQLITE_INTEGER1, /* 0x04 INTEGER */
81700 SQLITE_NULL5, /* 0x05 (not possible) */
81701 SQLITE_INTEGER1, /* 0x06 INTEGER + TEXT */
81702 SQLITE_NULL5, /* 0x07 (not possible) */
81703 SQLITE_FLOAT2, /* 0x08 FLOAT */
81704 SQLITE_NULL5, /* 0x09 (not possible) */
81705 SQLITE_FLOAT2, /* 0x0a FLOAT + TEXT */
81706 SQLITE_NULL5, /* 0x0b (not possible) */
81707 SQLITE_INTEGER1, /* 0x0c (not possible) */
81708 SQLITE_NULL5, /* 0x0d (not possible) */
81709 SQLITE_INTEGER1, /* 0x0e (not possible) */
81710 SQLITE_NULL5, /* 0x0f (not possible) */
81711 SQLITE_BLOB4, /* 0x10 BLOB */
81712 SQLITE_NULL5, /* 0x11 (not possible) */
81713 SQLITE_TEXT3, /* 0x12 (not possible) */
81714 SQLITE_NULL5, /* 0x13 (not possible) */
81715 SQLITE_INTEGER1, /* 0x14 INTEGER + BLOB */
81716 SQLITE_NULL5, /* 0x15 (not possible) */
81717 SQLITE_INTEGER1, /* 0x16 (not possible) */
81718 SQLITE_NULL5, /* 0x17 (not possible) */
81719 SQLITE_FLOAT2, /* 0x18 FLOAT + BLOB */
81720 SQLITE_NULL5, /* 0x19 (not possible) */
81721 SQLITE_FLOAT2, /* 0x1a (not possible) */
81722 SQLITE_NULL5, /* 0x1b (not possible) */
81723 SQLITE_INTEGER1, /* 0x1c (not possible) */
81724 SQLITE_NULL5, /* 0x1d (not possible) */
81725 SQLITE_INTEGER1, /* 0x1e (not possible) */
81726 SQLITE_NULL5, /* 0x1f (not possible) */
81727 SQLITE_FLOAT2, /* 0x20 INTREAL */
81728 SQLITE_NULL5, /* 0x21 (not possible) */
81729 SQLITE_TEXT3, /* 0x22 INTREAL + TEXT */
81730 SQLITE_NULL5, /* 0x23 (not possible) */
81731 SQLITE_FLOAT2, /* 0x24 (not possible) */
81732 SQLITE_NULL5, /* 0x25 (not possible) */
81733 SQLITE_FLOAT2, /* 0x26 (not possible) */
81734 SQLITE_NULL5, /* 0x27 (not possible) */
81735 SQLITE_FLOAT2, /* 0x28 (not possible) */
81736 SQLITE_NULL5, /* 0x29 (not possible) */
81737 SQLITE_FLOAT2, /* 0x2a (not possible) */
81738 SQLITE_NULL5, /* 0x2b (not possible) */
81739 SQLITE_FLOAT2, /* 0x2c (not possible) */
81740 SQLITE_NULL5, /* 0x2d (not possible) */
81741 SQLITE_FLOAT2, /* 0x2e (not possible) */
81742 SQLITE_NULL5, /* 0x2f (not possible) */
81743 SQLITE_BLOB4, /* 0x30 (not possible) */
81744 SQLITE_NULL5, /* 0x31 (not possible) */
81745 SQLITE_TEXT3, /* 0x32 (not possible) */
81746 SQLITE_NULL5, /* 0x33 (not possible) */
81747 SQLITE_FLOAT2, /* 0x34 (not possible) */
81748 SQLITE_NULL5, /* 0x35 (not possible) */
81749 SQLITE_FLOAT2, /* 0x36 (not possible) */
81750 SQLITE_NULL5, /* 0x37 (not possible) */
81751 SQLITE_FLOAT2, /* 0x38 (not possible) */
81752 SQLITE_NULL5, /* 0x39 (not possible) */
81753 SQLITE_FLOAT2, /* 0x3a (not possible) */
81754 SQLITE_NULL5, /* 0x3b (not possible) */
81755 SQLITE_FLOAT2, /* 0x3c (not possible) */
81756 SQLITE_NULL5, /* 0x3d (not possible) */
81757 SQLITE_FLOAT2, /* 0x3e (not possible) */
81758 SQLITE_NULL5, /* 0x3f (not possible) */
81759 };
81760#ifdef SQLITE_DEBUG
81761 {
81762 int eType = SQLITE_BLOB4;
81763 if( pVal->flags & MEM_Null0x0001 ){
81764 eType = SQLITE_NULL5;
81765 }else if( pVal->flags & (MEM_Real0x0008|MEM_IntReal0x0020) ){
81766 eType = SQLITE_FLOAT2;
81767 }else if( pVal->flags & MEM_Int0x0004 ){
81768 eType = SQLITE_INTEGER1;
81769 }else if( pVal->flags & MEM_Str0x0002 ){
81770 eType = SQLITE_TEXT3;
81771 }
81772 assert( eType == aType[pVal->flags&MEM_AffMask] )((void) (0));
81773 }
81774#endif
81775 return aType[pVal->flags&MEM_AffMask0x003f];
81776}
81777
81778/* Return true if a parameter to xUpdate represents an unchanged column */
81779SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){
81780 return (pVal->flags&(MEM_Null0x0001|MEM_Zero0x4000))==(MEM_Null0x0001|MEM_Zero0x4000);
81781}
81782
81783/* Return true if a parameter value originated from an sqlite3_bind() */
81784SQLITE_API int sqlite3_value_frombind(sqlite3_value *pVal){
81785 return (pVal->flags&MEM_FromBind0x0040)!=0;
81786}
81787
81788/* Make a copy of an sqlite3_value object
81789*/
81790SQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
81791 sqlite3_value *pNew;
81792 if( pOrig==0 ) return 0;
81793 pNew = sqlite3_malloc( sizeof(*pNew) );
81794 if( pNew==0 ) return 0;
81795 memset(pNew, 0, sizeof(*pNew));
81796 memcpy(pNew, pOrig, MEMCELLSIZE__builtin_offsetof(Mem, zMalloc));
81797 pNew->flags &= ~MEM_Dyn0x0400;
81798 pNew->db = 0;
81799 if( pNew->flags&(MEM_Str0x0002|MEM_Blob0x0010) ){
81800 pNew->flags &= ~(MEM_Static0x0800|MEM_Dyn0x0400);
81801 pNew->flags |= MEM_Ephem0x1000;
81802 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK0 ){
81803 sqlite3ValueFree(pNew);
81804 pNew = 0;
81805 }
81806 }
81807 return pNew;
81808}
81809
81810/* Destroy an sqlite3_value object previously obtained from
81811** sqlite3_value_dup().
81812*/
81813SQLITE_API void sqlite3_value_free(sqlite3_value *pOld){
81814 sqlite3ValueFree(pOld);
81815}
81816
81817
81818/**************************** sqlite3_result_ *******************************
81819** The following routines are used by user-defined functions to specify
81820** the function result.
81821**
81822** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
81823** result as a string or blob but if the string or blob is too large, it
81824** then sets the error code to SQLITE_TOOBIG
81825**
81826** The invokeValueDestructor(P,X) routine invokes destructor function X()
81827** on value P is not going to be used and need to be destroyed.
81828*/
81829static void setResultStrOrError(
81830 sqlite3_context *pCtx, /* Function context */
81831 const char *z, /* String pointer */
81832 int n, /* Bytes in string, or negative */
81833 u8 enc, /* Encoding of z. 0 for BLOBs */
81834 void (*xDel)(void*) /* Destructor function */
81835){
81836 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG18 ){
81837 sqlite3_result_error_toobig(pCtx);
81838 }
81839}
81840static int invokeValueDestructor(
81841 const void *p, /* Value to destroy */
81842 void (*xDel)(void*), /* The destructor */
81843 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */
81844){
81845 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
81846 if( xDel==0 ){
81847 /* noop */
81848 }else if( xDel==SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
81849 /* noop */
81850 }else{
81851 xDel((void*)p);
81852 }
81853 if( pCtx ) sqlite3_result_error_toobig(pCtx);
81854 return SQLITE_TOOBIG18;
81855}
81856SQLITE_API void sqlite3_result_blob(
81857 sqlite3_context *pCtx,
81858 const void *z,
81859 int n,
81860 void (*xDel)(void *)
81861){
81862 assert( n>=0 )((void) (0));
81863 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81864 setResultStrOrError(pCtx, z, n, 0, xDel);
81865}
81866SQLITE_API void sqlite3_result_blob64(
81867 sqlite3_context *pCtx,
81868 const void *z,
81869 sqlite3_uint64 n,
81870 void (*xDel)(void *)
81871){
81872 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81873 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
81874 if( n>0x7fffffff ){
81875 (void)invokeValueDestructor(z, xDel, pCtx);
81876 }else{
81877 setResultStrOrError(pCtx, z, (int)n, 0, xDel);
81878 }
81879}
81880SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
81881 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81882 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
81883}
81884SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
81885 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81886 pCtx->isError = SQLITE_ERROR1;
81887 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF81, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
81888}
81889#ifndef SQLITE_OMIT_UTF16
81890SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
81891 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81892 pCtx->isError = SQLITE_ERROR1;
81893 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE2, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
81894}
81895#endif
81896SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
81897 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81898 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
81899}
81900SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
81901 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81902 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
81903}
81904SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
81905 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81906 sqlite3VdbeMemSetNull(pCtx->pOut);
81907}
81908SQLITE_API void sqlite3_result_pointer(
81909 sqlite3_context *pCtx,
81910 void *pPtr,
81911 const char *zPType,
81912 void (*xDestructor)(void*)
81913){
81914 Mem *pOut = pCtx->pOut;
81915 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
81916 sqlite3VdbeMemRelease(pOut);
81917 pOut->flags = MEM_Null0x0001;
81918 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor);
81919}
81920SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
81921 Mem *pOut = pCtx->pOut;
81922 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
81923 pOut->eSubtype = eSubtype & 0xff;
81924 pOut->flags |= MEM_Subtype0x8000;
81925}
81926SQLITE_API void sqlite3_result_text(
81927 sqlite3_context *pCtx,
81928 const char *z,
81929 int n,
81930 void (*xDel)(void *)
81931){
81932 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81933 setResultStrOrError(pCtx, z, n, SQLITE_UTF81, xDel);
81934}
81935SQLITE_API void sqlite3_result_text64(
81936 sqlite3_context *pCtx,
81937 const char *z,
81938 sqlite3_uint64 n,
81939 void (*xDel)(void *),
81940 unsigned char enc
81941){
81942 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81943 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
81944 if( enc==SQLITE_UTF164 ) enc = SQLITE_UTF16NATIVE2;
81945 if( n>0x7fffffff ){
81946 (void)invokeValueDestructor(z, xDel, pCtx);
81947 }else{
81948 setResultStrOrError(pCtx, z, (int)n, enc, xDel);
81949 }
81950}
81951#ifndef SQLITE_OMIT_UTF16
81952SQLITE_API void sqlite3_result_text16(
81953 sqlite3_context *pCtx,
81954 const void *z,
81955 int n,
81956 void (*xDel)(void *)
81957){
81958 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81959 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE2, xDel);
81960}
81961SQLITE_API void sqlite3_result_text16be(
81962 sqlite3_context *pCtx,
81963 const void *z,
81964 int n,
81965 void (*xDel)(void *)
81966){
81967 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81968 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE3, xDel);
81969}
81970SQLITE_API void sqlite3_result_text16le(
81971 sqlite3_context *pCtx,
81972 const void *z,
81973 int n,
81974 void (*xDel)(void *)
81975){
81976 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81977 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE2, xDel);
81978}
81979#endif /* SQLITE_OMIT_UTF16 */
81980SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
81981 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81982 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
81983}
81984SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
81985 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
81986 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
81987}
81988SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){
81989 Mem *pOut = pCtx->pOut;
81990 assert( sqlite3_mutex_held(pOut->db->mutex) )((void) (0));
81991 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH0] ){
81992 return SQLITE_TOOBIG18;
81993 }
81994 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
81995 return SQLITE_OK0;
81996}
81997SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
81998 pCtx->isError = errCode ? errCode : -1;
81999#ifdef SQLITE_DEBUG
82000 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
82001#endif
82002 if( pCtx->pOut->flags & MEM_Null0x0001 ){
82003 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
82004 SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
82005 }
82006}
82007
82008/* Force an SQLITE_TOOBIG error. */
82009SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
82010 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
82011 pCtx->isError = SQLITE_TOOBIG18;
82012 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
82013 SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
82014}
82015
82016/* An SQLITE_NOMEM error. */
82017SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
82018 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
82019 sqlite3VdbeMemSetNull(pCtx->pOut);
82020 pCtx->isError = SQLITE_NOMEM_BKPT7;
82021 sqlite3OomFault(pCtx->pOut->db);
82022}
82023
82024#ifndef SQLITE_UNTESTABLE
82025/* Force the INT64 value currently stored as the result to be
82026** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL
82027** test-control.
82028*/
82029SQLITE_PRIVATEstatic void sqlite3ResultIntReal(sqlite3_context *pCtx){
82030 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
82031 if( pCtx->pOut->flags & MEM_Int0x0004 ){
82032 pCtx->pOut->flags &= ~MEM_Int0x0004;
82033 pCtx->pOut->flags |= MEM_IntReal0x0020;
82034 }
82035}
82036#endif
82037
82038
82039/*
82040** This function is called after a transaction has been committed. It
82041** invokes callbacks registered with sqlite3_wal_hook() as required.
82042*/
82043static int doWalCallbacks(sqlite3 *db){
82044 int rc = SQLITE_OK0;
82045#ifndef SQLITE_OMIT_WAL
82046 int i;
82047 for(i=0; i<db->nDb; i++){
82048 Btree *pBt = db->aDb[i].pBt;
82049 if( pBt ){
82050 int nEntry;
82051 sqlite3BtreeEnter(pBt);
82052 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
82053 sqlite3BtreeLeave(pBt);
82054 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK0 ){
82055 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
82056 }
82057 }
82058 }
82059#endif
82060 return rc;
82061}
82062
82063
82064/*
82065** Execute the statement pStmt, either until a row of data is ready, the
82066** statement is completely executed or an error occurs.
82067**
82068** This routine implements the bulk of the logic behind the sqlite_step()
82069** API. The only thing omitted is the automatic recompile if a
82070** schema change has occurred. That detail is handled by the
82071** outer sqlite3_step() wrapper procedure.
82072*/
82073static int sqlite3Step(Vdbe *p){
82074 sqlite3 *db;
82075 int rc;
82076
82077 assert(p)((void) (0));
82078 if( p->magic!=VDBE_MAGIC_RUN0x2df20da3 ){
82079 /* We used to require that sqlite3_reset() be called before retrying
82080 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning
82081 ** with version 3.7.0, we changed this so that sqlite3_reset() would
82082 ** be called automatically instead of throwing the SQLITE_MISUSE error.
82083 ** This "automatic-reset" change is not technically an incompatibility,
82084 ** since any application that receives an SQLITE_MISUSE is broken by
82085 ** definition.
82086 **
82087 ** Nevertheless, some published applications that were originally written
82088 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
82089 ** returns, and those were broken by the automatic-reset change. As a
82090 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
82091 ** legacy behavior of returning SQLITE_MISUSE for cases where the
82092 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
82093 ** or SQLITE_BUSY error.
82094 */
82095#ifdef SQLITE_OMIT_AUTORESET
82096 if( (rc = p->rc&0xff)==SQLITE_BUSY5 || rc==SQLITE_LOCKED6 ){
82097 sqlite3_reset((sqlite3_stmt*)p);
82098 }else{
82099 return SQLITE_MISUSE_BKPTsqlite3MisuseError(82099);
82100 }
82101#else
82102 sqlite3_reset((sqlite3_stmt*)p);
82103#endif
82104 }
82105
82106 /* Check that malloc() has not failed. If it has, return early. */
82107 db = p->db;
82108 if( db->mallocFailed ){
82109 p->rc = SQLITE_NOMEM7;
82110 return SQLITE_NOMEM_BKPT7;
82111 }
82112
82113 if( p->pc<0 && p->expired ){
82114 p->rc = SQLITE_SCHEMA17;
82115 rc = SQLITE_ERROR1;
82116 goto end_of_step;
82117 }
82118 if( p->pc<0 ){
82119 /* If there are no other statements currently running, then
82120 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt
82121 ** from interrupting a statement that has not yet started.
82122 */
82123 if( db->nVdbeActive==0 ){
82124 db->u1.isInterrupted = 0;
82125 }
82126
82127 assert( db->nVdbeWrite>0 || db->autoCommit==0((void) (0))
82128 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)((void) (0))
82129 )((void) (0));
82130
82131#ifndef SQLITE_OMIT_TRACE
82132 if( (db->mTrace & (SQLITE_TRACE_PROFILE0x02|SQLITE_TRACE_XPROFILE0x80))!=0
82133 && !db->init.busy && p->zSql ){
82134 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
82135 }else{
82136 assert( p->startTime==0 )((void) (0));
82137 }
82138#endif
82139
82140 db->nVdbeActive++;
82141 if( p->readOnly==0 ) db->nVdbeWrite++;
82142 if( p->bIsReader ) db->nVdbeRead++;
82143 p->pc = 0;
82144 }
82145#ifdef SQLITE_DEBUG
82146 p->rcApp = SQLITE_OK0;
82147#endif
82148#ifndef SQLITE_OMIT_EXPLAIN
82149 if( p->explain ){
82150 rc = sqlite3VdbeList(p);
82151 }else
82152#endif /* SQLITE_OMIT_EXPLAIN */
82153 {
82154 db->nVdbeExec++;
82155 rc = sqlite3VdbeExec(p);
82156 db->nVdbeExec--;
82157 }
82158
82159 if( rc!=SQLITE_ROW100 ){
82160#ifndef SQLITE_OMIT_TRACE
82161 /* If the statement completed successfully, invoke the profile callback */
82162 checkProfileCallback(db, p)if( ((p)->startTime)>0 ){ invokeProfileCallback(db,p); };
82163#endif
82164
82165 if( rc==SQLITE_DONE101 && db->autoCommit ){
82166 assert( p->rc==SQLITE_OK )((void) (0));
82167 p->rc = doWalCallbacks(db);
82168 if( p->rc!=SQLITE_OK0 ){
82169 rc = SQLITE_ERROR1;
82170 }
82171 }
82172 }
82173
82174 db->errCode = rc;
82175 if( SQLITE_NOMEM7==sqlite3ApiExit(p->db, p->rc) ){
82176 p->rc = SQLITE_NOMEM_BKPT7;
82177 }
82178end_of_step:
82179 /* At this point local variable rc holds the value that should be
82180 ** returned if this statement was compiled using the legacy
82181 ** sqlite3_prepare() interface. According to the docs, this can only
82182 ** be one of the values in the first assert() below. Variable p->rc
82183 ** contains the value that would be returned if sqlite3_finalize()
82184 ** were called on statement p.
82185 */
82186 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR((void) (0))
82187 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE((void) (0))
82188 )((void) (0));
82189 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp )((void) (0));
82190 if( rc!=SQLITE_ROW100
82191 && rc!=SQLITE_DONE101
82192 && (p->prepFlags & SQLITE_PREPARE_SAVESQL0x80)!=0
82193 ){
82194 /* If this statement was prepared using saved SQL and an
82195 ** error has occurred, then return the error code in p->rc to the
82196 ** caller. Set the error code in the database handle to the same value.
82197 */
82198 rc = sqlite3VdbeTransferError(p);
82199 }
82200 return (rc&db->errMask);
82201}
82202
82203/*
82204** This is the top-level implementation of sqlite3_step(). Call
82205** sqlite3Step() to do most of the work. If a schema error occurs,
82206** call sqlite3Reprepare() and try again.
82207*/
82208SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
82209 int rc = SQLITE_OK0; /* Result from sqlite3Step() */
82210 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */
82211 int cnt = 0; /* Counter to prevent infinite loop of reprepares */
82212 sqlite3 *db; /* The database connection */
82213
82214 if( vdbeSafetyNotNull(v) ){
82215 return SQLITE_MISUSE_BKPTsqlite3MisuseError(82215);
82216 }
82217 db = v->db;
82218 sqlite3_mutex_enter(db->mutex);
82219 v->doingRerun = 0;
82220 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA17
82221 && cnt++ < SQLITE_MAX_SCHEMA_RETRY50 ){
82222 int savedPc = v->pc;
82223 rc = sqlite3Reprepare(v);
82224 if( rc!=SQLITE_OK0 ){
82225 /* This case occurs after failing to recompile an sql statement.
82226 ** The error message from the SQL compiler has already been loaded
82227 ** into the database handle. This block copies the error message
82228 ** from the database handle into the statement and sets the statement
82229 ** program counter to 0 to ensure that when the statement is
82230 ** finalized or reset the parser error message is available via
82231 ** sqlite3_errmsg() and sqlite3_errcode().
82232 */
82233 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
82234 sqlite3DbFree(db, v->zErrMsg);
82235 if( !db->mallocFailed ){
82236 v->zErrMsg = sqlite3DbStrDup(db, zErr);
82237 v->rc = rc = sqlite3ApiExit(db, rc);
82238 } else {
82239 v->zErrMsg = 0;
82240 v->rc = rc = SQLITE_NOMEM_BKPT7;
82241 }
82242 break;
82243 }
82244 sqlite3_reset(pStmt);
82245 if( savedPc>=0 ) v->doingRerun = 1;
82246 assert( v->expired==0 )((void) (0));
82247 }
82248 sqlite3_mutex_leave(db->mutex);
82249 return rc;
82250}
82251
82252
82253/*
82254** Extract the user data from a sqlite3_context structure and return a
82255** pointer to it.
82256*/
82257SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
82258 assert( p && p->pFunc )((void) (0));
82259 return p->pFunc->pUserData;
82260}
82261
82262/*
82263** Extract the user data from a sqlite3_context structure and return a
82264** pointer to it.
82265**
82266** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
82267** returns a copy of the pointer to the database connection (the 1st
82268** parameter) of the sqlite3_create_function() and
82269** sqlite3_create_function16() routines that originally registered the
82270** application defined function.
82271*/
82272SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
82273 assert( p && p->pOut )((void) (0));
82274 return p->pOut->db;
82275}
82276
82277/*
82278** If this routine is invoked from within an xColumn method of a virtual
82279** table, then it returns true if and only if the the call is during an
82280** UPDATE operation and the value of the column will not be modified
82281** by the UPDATE.
82282**
82283** If this routine is called from any context other than within the
82284** xColumn method of a virtual table, then the return value is meaningless
82285** and arbitrary.
82286**
82287** Virtual table implements might use this routine to optimize their
82288** performance by substituting a NULL result, or some other light-weight
82289** value, as a signal to the xUpdate routine that the column is unchanged.
82290*/
82291SQLITE_API int sqlite3_vtab_nochange(sqlite3_context *p){
82292 assert( p )((void) (0));
82293 return sqlite3_value_nochange(p->pOut);
82294}
82295
82296/*
82297** Return the current time for a statement. If the current time
82298** is requested more than once within the same run of a single prepared
82299** statement, the exact same time is returned for each invocation regardless
82300** of the amount of time that elapses between invocations. In other words,
82301** the time returned is always the time of the first call.
82302*/
82303SQLITE_PRIVATEstatic sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
82304 int rc;
82305#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
82306 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
82307 assert( p->pVdbe!=0 )((void) (0));
82308#else
82309 sqlite3_int64 iTime = 0;
82310 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
82311#endif
82312 if( *piTime==0 ){
82313 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
82314 if( rc ) *piTime = 0;
82315 }
82316 return *piTime;
82317}
82318
82319/*
82320** Create a new aggregate context for p and return a pointer to
82321** its pMem->z element.
82322*/
82323static SQLITE_NOINLINE__attribute__((noinline)) void *createAggContext(sqlite3_context *p, int nByte){
82324 Mem *pMem = p->pMem;
82325 assert( (pMem->flags & MEM_Agg)==0 )((void) (0));
82326 if( nByte<=0 ){
82327 sqlite3VdbeMemSetNull(pMem);
82328 pMem->z = 0;
82329 }else{
82330 sqlite3VdbeMemClearAndResize(pMem, nByte);
82331 pMem->flags = MEM_Agg0x2000;
82332 pMem->u.pDef = p->pFunc;
82333 if( pMem->z ){
82334 memset(pMem->z, 0, nByte);
82335 }
82336 }
82337 return (void*)pMem->z;
82338}
82339
82340/*
82341** Allocate or return the aggregate context for a user function. A new
82342** context is allocated on the first call. Subsequent calls return the
82343** same context that was returned on prior calls.
82344*/
82345SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
82346 assert( p && p->pFunc && p->pFunc->xFinalize )((void) (0));
82347 assert( sqlite3_mutex_held(p->pOut->db->mutex) )((void) (0));
82348 testcase( nByte<0 );
82349 if( (p->pMem->flags & MEM_Agg0x2000)==0 ){
82350 return createAggContext(p, nByte);
82351 }else{
82352 return (void*)p->pMem->z;
82353 }
82354}
82355
82356/*
82357** Return the auxiliary data pointer, if any, for the iArg'th argument to
82358** the user-function defined by pCtx.
82359**
82360** The left-most argument is 0.
82361**
82362** Undocumented behavior: If iArg is negative then access a cache of
82363** auxiliary data pointers that is available to all functions within a
82364** single prepared statement. The iArg values must match.
82365*/
82366SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
82367 AuxData *pAuxData;
82368
82369 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
82370#if SQLITE_ENABLE_STAT3_OR_STAT4
82371 if( pCtx->pVdbe==0 ) return 0;
82372#else
82373 assert( pCtx->pVdbe!=0 )((void) (0));
82374#endif
82375 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
82376 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
82377 return pAuxData->pAux;
82378 }
82379 }
82380 return 0;
82381}
82382
82383/*
82384** Set the auxiliary data pointer and delete function, for the iArg'th
82385** argument to the user-function defined by pCtx. Any previous value is
82386** deleted by calling the delete function specified when it was set.
82387**
82388** The left-most argument is 0.
82389**
82390** Undocumented behavior: If iArg is negative then make the data available
82391** to all functions within the current prepared statement using iArg as an
82392** access code.
82393*/
82394SQLITE_API void sqlite3_set_auxdata(
82395 sqlite3_context *pCtx,
82396 int iArg,
82397 void *pAux,
82398 void (*xDelete)(void*)
82399){
82400 AuxData *pAuxData;
82401 Vdbe *pVdbe = pCtx->pVdbe;
82402
82403 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) )((void) (0));
82404#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82405 if( pVdbe==0 ) goto failed;
82406#else
82407 assert( pVdbe!=0 )((void) (0));
82408#endif
82409
82410 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
82411 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
82412 break;
82413 }
82414 }
82415 if( pAuxData==0 ){
82416 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
82417 if( !pAuxData ) goto failed;
82418 pAuxData->iAuxOp = pCtx->iOp;
82419 pAuxData->iAuxArg = iArg;
82420 pAuxData->pNextAux = pVdbe->pAuxData;
82421 pVdbe->pAuxData = pAuxData;
82422 if( pCtx->isError==0 ) pCtx->isError = -1;
82423 }else if( pAuxData->xDeleteAux ){
82424 pAuxData->xDeleteAux(pAuxData->pAux);
82425 }
82426
82427 pAuxData->pAux = pAux;
82428 pAuxData->xDeleteAux = xDelete;
82429 return;
82430
82431failed:
82432 if( xDelete ){
82433 xDelete(pAux);
82434 }
82435}
82436
82437#ifndef SQLITE_OMIT_DEPRECATED
82438/*
82439** Return the number of times the Step function of an aggregate has been
82440** called.
82441**
82442** This function is deprecated. Do not use it for new code. It is
82443** provide only to avoid breaking legacy code. New aggregate function
82444** implementations should keep their own counts within their aggregate
82445** context.
82446*/
82447SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
82448 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize )((void) (0));
82449 return p->pMem->n;
82450}
82451#endif
82452
82453/*
82454** Return the number of columns in the result set for the statement pStmt.
82455*/
82456SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
82457 Vdbe *pVm = (Vdbe *)pStmt;
82458 return pVm ? pVm->nResColumn : 0;
82459}
82460
82461/*
82462** Return the number of values available from the current row of the
82463** currently executing statement pStmt.
82464*/
82465SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
82466 Vdbe *pVm = (Vdbe *)pStmt;
82467 if( pVm==0 || pVm->pResultSet==0 ) return 0;
82468 return pVm->nResColumn;
82469}
82470
82471/*
82472** Return a pointer to static memory containing an SQL NULL value.
82473*/
82474static const Mem *columnNullValue(void){
82475 /* Even though the Mem structure contains an element
82476 ** of type i64, on certain architectures (x86) with certain compiler
82477 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
82478 ** instead of an 8-byte one. This all works fine, except that when
82479 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
82480 ** that a Mem structure is located on an 8-byte boundary. To prevent
82481 ** these assert()s from failing, when building with SQLITE_DEBUG defined
82482 ** using gcc, we force nullMem to be 8-byte aligned using the magical
82483 ** __attribute__((aligned(8))) macro. */
82484 static const Mem nullMem
82485#if defined(SQLITE_DEBUG) && defined(__GNUC__4)
82486 __attribute__((aligned(8)))
82487#endif
82488 = {
82489 /* .u = */ {0},
82490 /* .flags = */ (u16)MEM_Null0x0001,
82491 /* .enc = */ (u8)0,
82492 /* .eSubtype = */ (u8)0,
82493 /* .n = */ (int)0,
82494 /* .z = */ (char*)0,
82495 /* .zMalloc = */ (char*)0,
82496 /* .szMalloc = */ (int)0,
82497 /* .uTemp = */ (u32)0,
82498 /* .db = */ (sqlite3*)0,
82499 /* .xDel = */ (void(*)(void*))0,
82500#ifdef SQLITE_DEBUG
82501 /* .pScopyFrom = */ (Mem*)0,
82502 /* .mScopyFlags= */ 0,
82503#endif
82504 };
82505 return &nullMem;
82506}
82507
82508/*
82509** Check to see if column iCol of the given statement is valid. If
82510** it is, return a pointer to the Mem for the value of that column.
82511** If iCol is not valid, return a pointer to a Mem which has a value
82512** of NULL.
82513*/
82514static Mem *columnMem(sqlite3_stmt *pStmt, int i){
82515 Vdbe *pVm;
82516 Mem *pOut;
82517
82518 pVm = (Vdbe *)pStmt;
82519 if( pVm==0 ) return (Mem*)columnNullValue();
82520 assert( pVm->db )((void) (0));
82521 sqlite3_mutex_enter(pVm->db->mutex);
82522 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
82523 pOut = &pVm->pResultSet[i];
82524 }else{
82525 sqlite3Error(pVm->db, SQLITE_RANGE25);
82526 pOut = (Mem*)columnNullValue();
82527 }
82528 return pOut;
82529}
82530
82531/*
82532** This function is called after invoking an sqlite3_value_XXX function on a
82533** column value (i.e. a value returned by evaluating an SQL expression in the
82534** select list of a SELECT statement) that may cause a malloc() failure. If
82535** malloc() has failed, the threads mallocFailed flag is cleared and the result
82536** code of statement pStmt set to SQLITE_NOMEM.
82537**
82538** Specifically, this is called from within:
82539**
82540** sqlite3_column_int()
82541** sqlite3_column_int64()
82542** sqlite3_column_text()
82543** sqlite3_column_text16()
82544** sqlite3_column_real()
82545** sqlite3_column_bytes()
82546** sqlite3_column_bytes16()
82547** sqiite3_column_blob()
82548*/
82549static void columnMallocFailure(sqlite3_stmt *pStmt)
82550{
82551 /* If malloc() failed during an encoding conversion within an
82552 ** sqlite3_column_XXX API, then set the return code of the statement to
82553 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
82554 ** and _finalize() will return NOMEM.
82555 */
82556 Vdbe *p = (Vdbe *)pStmt;
82557 if( p ){
82558 assert( p->db!=0 )((void) (0));
82559 assert( sqlite3_mutex_held(p->db->mutex) )((void) (0));
82560 p->rc = sqlite3ApiExit(p->db, p->rc);
82561 sqlite3_mutex_leave(p->db->mutex);
82562 }
82563}
82564
82565/**************************** sqlite3_column_ *******************************
82566** The following routines are used to access elements of the current row
82567** in the result set.
82568*/
82569SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
82570 const void *val;
82571 val = sqlite3_value_blob( columnMem(pStmt,i) );
82572 /* Even though there is no encoding conversion, value_blob() might
82573 ** need to call malloc() to expand the result of a zeroblob()
82574 ** expression.
82575 */
82576 columnMallocFailure(pStmt);
82577 return val;
82578}
82579SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
82580 int val = sqlite3_value_bytes( columnMem(pStmt,i) );
82581 columnMallocFailure(pStmt);
82582 return val;
82583}
82584SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
82585 int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
82586 columnMallocFailure(pStmt);
82587 return val;
82588}
82589SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
82590 double val = sqlite3_value_double( columnMem(pStmt,i) );
82591 columnMallocFailure(pStmt);
82592 return val;
82593}
82594SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
82595 int val = sqlite3_value_int( columnMem(pStmt,i) );
82596 columnMallocFailure(pStmt);
82597 return val;
82598}
82599SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
82600 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
82601 columnMallocFailure(pStmt);
82602 return val;
82603}
82604SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
82605 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
82606 columnMallocFailure(pStmt);
82607 return val;
82608}
82609SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
82610 Mem *pOut = columnMem(pStmt, i);
82611 if( pOut->flags&MEM_Static0x0800 ){
82612 pOut->flags &= ~MEM_Static0x0800;
82613 pOut->flags |= MEM_Ephem0x1000;
82614 }
82615 columnMallocFailure(pStmt);
82616 return (sqlite3_value *)pOut;
82617}
82618#ifndef SQLITE_OMIT_UTF16
82619SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
82620 const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
82621 columnMallocFailure(pStmt);
82622 return val;
82623}
82624#endif /* SQLITE_OMIT_UTF16 */
82625SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
82626 int iType = sqlite3_value_type( columnMem(pStmt,i) );
82627 columnMallocFailure(pStmt);
82628 return iType;
82629}
82630
82631/*
82632** Convert the N-th element of pStmt->pColName[] into a string using
82633** xFunc() then return that string. If N is out of range, return 0.
82634**
82635** There are up to 5 names for each column. useType determines which
82636** name is returned. Here are the names:
82637**
82638** 0 The column name as it should be displayed for output
82639** 1 The datatype name for the column
82640** 2 The name of the database that the column derives from
82641** 3 The name of the table that the column derives from
82642** 4 The name of the table column that the result column derives from
82643**
82644** If the result is not a simple column reference (if it is an expression
82645** or a constant) then useTypes 2, 3, and 4 return NULL.
82646*/
82647static const void *columnName(
82648 sqlite3_stmt *pStmt, /* The statement */
82649 int N, /* Which column to get the name for */
82650 int useUtf16, /* True to return the name as UTF16 */
82651 int useType /* What type of name */
82652){
82653 const void *ret;
82654 Vdbe *p;
82655 int n;
82656 sqlite3 *db;
82657#ifdef SQLITE_ENABLE_API_ARMOR
82658 if( pStmt==0 ){
82659 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(82659);
82660 return 0;
82661 }
82662#endif
82663 ret = 0;
82664 p = (Vdbe *)pStmt;
82665 db = p->db;
82666 assert( db!=0 )((void) (0));
82667 n = sqlite3_column_count(pStmt);
82668 if( N<n && N>=0 ){
82669 N += useType*n;
82670 sqlite3_mutex_enter(db->mutex);
82671 assert( db->mallocFailed==0 )((void) (0));
82672#ifndef SQLITE_OMIT_UTF16
82673 if( useUtf16 ){
82674 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
82675 }else
82676#endif
82677 {
82678 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
82679 }
82680 /* A malloc may have failed inside of the _text() call. If this
82681 ** is the case, clear the mallocFailed flag and return NULL.
82682 */
82683 if( db->mallocFailed ){
82684 sqlite3OomClear(db);
82685 ret = 0;
82686 }
82687 sqlite3_mutex_leave(db->mutex);
82688 }
82689 return ret;
82690}
82691
82692/*
82693** Return the name of the Nth column of the result set returned by SQL
82694** statement pStmt.
82695*/
82696SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
82697 return columnName(pStmt, N, 0, COLNAME_NAME0);
82698}
82699#ifndef SQLITE_OMIT_UTF16
82700SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
82701 return columnName(pStmt, N, 1, COLNAME_NAME0);
82702}
82703#endif
82704
82705/*
82706** Constraint: If you have ENABLE_COLUMN_METADATA then you must
82707** not define OMIT_DECLTYPE.
82708*/
82709#if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
82710# error "Must not define both SQLITE_OMIT_DECLTYPE \
82711 and SQLITE_ENABLE_COLUMN_METADATA"
82712#endif
82713
82714#ifndef SQLITE_OMIT_DECLTYPE
82715/*
82716** Return the column declaration type (if applicable) of the 'i'th column
82717** of the result set of SQL statement pStmt.
82718*/
82719SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
82720 return columnName(pStmt, N, 0, COLNAME_DECLTYPE1);
82721}
82722#ifndef SQLITE_OMIT_UTF16
82723SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
82724 return columnName(pStmt, N, 1, COLNAME_DECLTYPE1);
82725}
82726#endif /* SQLITE_OMIT_UTF16 */
82727#endif /* SQLITE_OMIT_DECLTYPE */
82728
82729#ifdef SQLITE_ENABLE_COLUMN_METADATA
82730/*
82731** Return the name of the database from which a result column derives.
82732** NULL is returned if the result column is an expression or constant or
82733** anything else which is not an unambiguous reference to a database column.
82734*/
82735SQLITE_API const char *sqlite3_column_database_name0(sqlite3_stmt *pStmt, int N){
82736 return columnName(pStmt, N, 0, COLNAME_DATABASE2);
82737}
82738#ifndef SQLITE_OMIT_UTF16
82739SQLITE_API const void *sqlite3_column_database_name160(sqlite3_stmt *pStmt, int N){
82740 return columnName(pStmt, N, 1, COLNAME_DATABASE2);
82741}
82742#endif /* SQLITE_OMIT_UTF16 */
82743
82744/*
82745** Return the name of the table from which a result column derives.
82746** NULL is returned if the result column is an expression or constant or
82747** anything else which is not an unambiguous reference to a database column.
82748*/
82749SQLITE_API const char *sqlite3_column_table_name0(sqlite3_stmt *pStmt, int N){
82750 return columnName(pStmt, N, 0, COLNAME_TABLE3);
82751}
82752#ifndef SQLITE_OMIT_UTF16
82753SQLITE_API const void *sqlite3_column_table_name160(sqlite3_stmt *pStmt, int N){
82754 return columnName(pStmt, N, 1, COLNAME_TABLE3);
82755}
82756#endif /* SQLITE_OMIT_UTF16 */
82757
82758/*
82759** Return the name of the table column from which a result column derives.
82760** NULL is returned if the result column is an expression or constant or
82761** anything else which is not an unambiguous reference to a database column.
82762*/
82763SQLITE_API const char *sqlite3_column_origin_name0(sqlite3_stmt *pStmt, int N){
82764 return columnName(pStmt, N, 0, COLNAME_COLUMN4);
82765}
82766#ifndef SQLITE_OMIT_UTF16
82767SQLITE_API const void *sqlite3_column_origin_name160(sqlite3_stmt *pStmt, int N){
82768 return columnName(pStmt, N, 1, COLNAME_COLUMN4);
82769}
82770#endif /* SQLITE_OMIT_UTF16 */
82771#endif /* SQLITE_ENABLE_COLUMN_METADATA */
82772
82773
82774/******************************* sqlite3_bind_ ***************************
82775**
82776** Routines used to attach values to wildcards in a compiled SQL statement.
82777*/
82778/*
82779** Unbind the value bound to variable i in virtual machine p. This is the
82780** the same as binding a NULL value to the column. If the "i" parameter is
82781** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
82782**
82783** A successful evaluation of this routine acquires the mutex on p.
82784** the mutex is released if any kind of error occurs.
82785**
82786** The error code stored in database p->db is overwritten with the return
82787** value in any case.
82788*/
82789static int vdbeUnbind(Vdbe *p, int i){
82790 Mem *pVar;
82791 if( vdbeSafetyNotNull(p) ){
82792 return SQLITE_MISUSE_BKPTsqlite3MisuseError(82792);
82793 }
82794 sqlite3_mutex_enter(p->db->mutex);
82795 if( p->magic!=VDBE_MAGIC_RUN0x2df20da3 || p->pc>=0 ){
82796 sqlite3Error(p->db, SQLITE_MISUSE21);
82797 sqlite3_mutex_leave(p->db->mutex);
82798 sqlite3_log(SQLITE_MISUSE21,
82799 "bind on a busy prepared statement: [%s]", p->zSql);
82800 return SQLITE_MISUSE_BKPTsqlite3MisuseError(82800);
82801 }
82802 if( i<1 || i>p->nVar ){
82803 sqlite3Error(p->db, SQLITE_RANGE25);
82804 sqlite3_mutex_leave(p->db->mutex);
82805 return SQLITE_RANGE25;
82806 }
82807 i--;
82808 pVar = &p->aVar[i];
82809 sqlite3VdbeMemRelease(pVar);
82810 pVar->flags = MEM_Null0x0001;
82811 p->db->errCode = SQLITE_OK0;
82812
82813 /* If the bit corresponding to this variable in Vdbe.expmask is set, then
82814 ** binding a new value to this variable invalidates the current query plan.
82815 **
82816 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
82817 ** parameter in the WHERE clause might influence the choice of query plan
82818 ** for a statement, then the statement will be automatically recompiled,
82819 ** as if there had been a schema change, on the first sqlite3_step() call
82820 ** following any change to the bindings of that parameter.
82821 */
82822 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 )((void) (0));
82823 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
82824 p->expired = 1;
82825 }
82826 return SQLITE_OK0;
82827}
82828
82829/*
82830** Bind a text or BLOB value.
82831*/
82832static int bindText(
82833 sqlite3_stmt *pStmt, /* The statement to bind against */
82834 int i, /* Index of the parameter to bind */
82835 const void *zData, /* Pointer to the data to be bound */
82836 int nData, /* Number of bytes of data to be bound */
82837 void (*xDel)(void*), /* Destructor for the data */
82838 u8 encoding /* Encoding for the data */
82839){
82840 Vdbe *p = (Vdbe *)pStmt;
82841 Mem *pVar;
82842 int rc;
82843
82844 rc = vdbeUnbind(p, i);
82845 if( rc==SQLITE_OK0 ){
82846 if( zData!=0 ){
82847 pVar = &p->aVar[i-1];
82848 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
82849 if( rc==SQLITE_OK0 && encoding!=0 ){
82850 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)((p->db)->enc));
82851 }
82852 if( rc ){
82853 sqlite3Error(p->db, rc);
82854 rc = sqlite3ApiExit(p->db, rc);
82855 }
82856 }
82857 sqlite3_mutex_leave(p->db->mutex);
82858 }else if( xDel!=SQLITE_STATIC((sqlite3_destructor_type)0) && xDel!=SQLITE_TRANSIENT((sqlite3_destructor_type)-1) ){
82859 xDel((void*)zData);
82860 }
82861 return rc;
82862}
82863
82864
82865/*
82866** Bind a blob value to an SQL statement variable.
82867*/
82868SQLITE_API int sqlite3_bind_blob(
82869 sqlite3_stmt *pStmt,
82870 int i,
82871 const void *zData,
82872 int nData,
82873 void (*xDel)(void*)
82874){
82875#ifdef SQLITE_ENABLE_API_ARMOR
82876 if( nData<0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(82876);
82877#endif
82878 return bindText(pStmt, i, zData, nData, xDel, 0);
82879}
82880SQLITE_API int sqlite3_bind_blob64(
82881 sqlite3_stmt *pStmt,
82882 int i,
82883 const void *zData,
82884 sqlite3_uint64 nData,
82885 void (*xDel)(void*)
82886){
82887 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
82888 if( nData>0x7fffffff ){
82889 return invokeValueDestructor(zData, xDel, 0);
82890 }else{
82891 return bindText(pStmt, i, zData, (int)nData, xDel, 0);
82892 }
82893}
82894SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
82895 int rc;
82896 Vdbe *p = (Vdbe *)pStmt;
82897 rc = vdbeUnbind(p, i);
82898 if( rc==SQLITE_OK0 ){
82899 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
82900 sqlite3_mutex_leave(p->db->mutex);
82901 }
82902 return rc;
82903}
82904SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
82905 return sqlite3_bind_int64(p, i, (i64)iValue);
82906}
82907SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
82908 int rc;
82909 Vdbe *p = (Vdbe *)pStmt;
82910 rc = vdbeUnbind(p, i);
82911 if( rc==SQLITE_OK0 ){
82912 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
82913 sqlite3_mutex_leave(p->db->mutex);
82914 }
82915 return rc;
82916}
82917SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
82918 int rc;
82919 Vdbe *p = (Vdbe*)pStmt;
82920 rc = vdbeUnbind(p, i);
82921 if( rc==SQLITE_OK0 ){
82922 sqlite3_mutex_leave(p->db->mutex);
82923 }
82924 return rc;
82925}
82926SQLITE_API int sqlite3_bind_pointer(
82927 sqlite3_stmt *pStmt,
82928 int i,
82929 void *pPtr,
82930 const char *zPTtype,
82931 void (*xDestructor)(void*)
82932){
82933 int rc;
82934 Vdbe *p = (Vdbe*)pStmt;
82935 rc = vdbeUnbind(p, i);
82936 if( rc==SQLITE_OK0 ){
82937 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
82938 sqlite3_mutex_leave(p->db->mutex);
82939 }else if( xDestructor ){
82940 xDestructor(pPtr);
82941 }
82942 return rc;
82943}
82944SQLITE_API int sqlite3_bind_text(
82945 sqlite3_stmt *pStmt,
82946 int i,
82947 const char *zData,
82948 int nData,
82949 void (*xDel)(void*)
82950){
82951 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF81);
82952}
82953SQLITE_API int sqlite3_bind_text64(
82954 sqlite3_stmt *pStmt,
82955 int i,
82956 const char *zData,
82957 sqlite3_uint64 nData,
82958 void (*xDel)(void*),
82959 unsigned char enc
82960){
82961 assert( xDel!=SQLITE_DYNAMIC )((void) (0));
82962 if( nData>0x7fffffff ){
82963 return invokeValueDestructor(zData, xDel, 0);
82964 }else{
82965 if( enc==SQLITE_UTF164 ) enc = SQLITE_UTF16NATIVE2;
82966 return bindText(pStmt, i, zData, (int)nData, xDel, enc);
82967 }
82968}
82969#ifndef SQLITE_OMIT_UTF16
82970SQLITE_API int sqlite3_bind_text16(
82971 sqlite3_stmt *pStmt,
82972 int i,
82973 const void *zData,
82974 int nData,
82975 void (*xDel)(void*)
82976){
82977 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE2);
82978}
82979#endif /* SQLITE_OMIT_UTF16 */
82980SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
82981 int rc;
82982 switch( sqlite3_value_type((sqlite3_value*)pValue) ){
82983 case SQLITE_INTEGER1: {
82984 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
82985 break;
82986 }
82987 case SQLITE_FLOAT2: {
82988 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
82989 break;
82990 }
82991 case SQLITE_BLOB4: {
82992 if( pValue->flags & MEM_Zero0x4000 ){
82993 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
82994 }else{
82995 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
82996 }
82997 break;
82998 }
82999 case SQLITE_TEXT3: {
83000 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1),
83001 pValue->enc);
83002 break;
83003 }
83004 default: {
83005 rc = sqlite3_bind_null(pStmt, i);
83006 break;
83007 }
83008 }
83009 return rc;
83010}
83011SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
83012 int rc;
83013 Vdbe *p = (Vdbe *)pStmt;
83014 rc = vdbeUnbind(p, i);
83015 if( rc==SQLITE_OK0 ){
83016 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
83017 sqlite3_mutex_leave(p->db->mutex);
83018 }
83019 return rc;
83020}
83021SQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){
83022 int rc;
83023 Vdbe *p = (Vdbe *)pStmt;
83024 sqlite3_mutex_enter(p->db->mutex);
83025 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH0] ){
83026 rc = SQLITE_TOOBIG18;
83027 }else{
83028 assert( (n & 0x7FFFFFFF)==n )((void) (0));
83029 rc = sqlite3_bind_zeroblob(pStmt, i, n);
83030 }
83031 rc = sqlite3ApiExit(p->db, rc);
83032 sqlite3_mutex_leave(p->db->mutex);
83033 return rc;
83034}
83035
83036/*
83037** Return the number of wildcards that can be potentially bound to.
83038** This routine is added to support DBD::SQLite.
83039*/
83040SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
83041 Vdbe *p = (Vdbe*)pStmt;
83042 return p ? p->nVar : 0;
83043}
83044
83045/*
83046** Return the name of a wildcard parameter. Return NULL if the index
83047** is out of range or if the wildcard is unnamed.
83048**
83049** The result is always UTF-8.
83050*/
83051SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
83052 Vdbe *p = (Vdbe*)pStmt;
83053 if( p==0 ) return 0;
83054 return sqlite3VListNumToName(p->pVList, i);
83055}
83056
83057/*
83058** Given a wildcard parameter name, return the index of the variable
83059** with that name. If there is no variable with the given name,
83060** return 0.
83061*/
83062SQLITE_PRIVATEstatic int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
83063 if( p==0 || zName==0 ) return 0;
83064 return sqlite3VListNameToNum(p->pVList, zName, nName);
83065}
83066SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
83067 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
83068}
83069
83070/*
83071** Transfer all bindings from the first statement over to the second.
83072*/
83073SQLITE_PRIVATEstatic int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
83074 Vdbe *pFrom = (Vdbe*)pFromStmt;
83075 Vdbe *pTo = (Vdbe*)pToStmt;
83076 int i;
83077 assert( pTo->db==pFrom->db )((void) (0));
83078 assert( pTo->nVar==pFrom->nVar )((void) (0));
83079 sqlite3_mutex_enter(pTo->db->mutex);
83080 for(i=0; i<pFrom->nVar; i++){
83081 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
83082 }
83083 sqlite3_mutex_leave(pTo->db->mutex);
83084 return SQLITE_OK0;
83085}
83086
83087#ifndef SQLITE_OMIT_DEPRECATED
83088/*
83089** Deprecated external interface. Internal/core SQLite code
83090** should call sqlite3TransferBindings.
83091**
83092** It is misuse to call this routine with statements from different
83093** database connections. But as this is a deprecated interface, we
83094** will not bother to check for that condition.
83095**
83096** If the two statements contain a different number of bindings, then
83097** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise
83098** SQLITE_OK is returned.
83099*/
83100SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
83101 Vdbe *pFrom = (Vdbe*)pFromStmt;
83102 Vdbe *pTo = (Vdbe*)pToStmt;
83103 if( pFrom->nVar!=pTo->nVar ){
83104 return SQLITE_ERROR1;
83105 }
83106 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 )((void) (0));
83107 if( pTo->expmask ){
83108 pTo->expired = 1;
83109 }
83110 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 )((void) (0));
83111 if( pFrom->expmask ){
83112 pFrom->expired = 1;
83113 }
83114 return sqlite3TransferBindings(pFromStmt, pToStmt);
83115}
83116#endif
83117
83118/*
83119** Return the sqlite3* database handle to which the prepared statement given
83120** in the argument belongs. This is the same database handle that was
83121** the first argument to the sqlite3_prepare() that was used to create
83122** the statement in the first place.
83123*/
83124SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
83125 return pStmt ? ((Vdbe*)pStmt)->db : 0;
83126}
83127
83128/*
83129** Return true if the prepared statement is guaranteed to not modify the
83130** database.
83131*/
83132SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
83133 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
83134}
83135
83136/*
83137** Return 1 if the statement is an EXPLAIN and return 2 if the
83138** statement is an EXPLAIN QUERY PLAN
83139*/
83140SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){
83141 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
83142}
83143
83144/*
83145** Return true if the prepared statement is in need of being reset.
83146*/
83147SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
83148 Vdbe *v = (Vdbe*)pStmt;
83149 return v!=0 && v->magic==VDBE_MAGIC_RUN0x2df20da3 && v->pc>=0;
83150}
83151
83152/*
83153** Return a pointer to the next prepared statement after pStmt associated
83154** with database connection pDb. If pStmt is NULL, return the first
83155** prepared statement for the database connection. Return NULL if there
83156** are no more.
83157*/
83158SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
83159 sqlite3_stmt *pNext;
83160#ifdef SQLITE_ENABLE_API_ARMOR
83161 if( !sqlite3SafetyCheckOk(pDb) ){
83162 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(83162);
83163 return 0;
83164 }
83165#endif
83166 sqlite3_mutex_enter(pDb->mutex);
83167 if( pStmt==0 ){
83168 pNext = (sqlite3_stmt*)pDb->pVdbe;
83169 }else{
83170 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
83171 }
83172 sqlite3_mutex_leave(pDb->mutex);
83173 return pNext;
83174}
83175
83176/*
83177** Return the value of a status counter for a prepared statement
83178*/
83179SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
83180 Vdbe *pVdbe = (Vdbe*)pStmt;
83181 u32 v;
83182#ifdef SQLITE_ENABLE_API_ARMOR
83183 if( !pStmt
83184 || (op!=SQLITE_STMTSTATUS_MEMUSED99 && (op<0||op>=ArraySize(pVdbe->aCounter)((int)(sizeof(pVdbe->aCounter)/sizeof(pVdbe->aCounter[0
])))
))
83185 ){
83186 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(83186);
83187 return 0;
83188 }
83189#endif
83190 if( op==SQLITE_STMTSTATUS_MEMUSED99 ){
83191 sqlite3 *db = pVdbe->db;
83192 sqlite3_mutex_enter(db->mutex);
83193 v = 0;
83194 db->pnBytesFreed = (int*)&v;
83195 sqlite3VdbeClearObject(db, pVdbe);
83196 sqlite3DbFree(db, pVdbe);
83197 db->pnBytesFreed = 0;
83198 sqlite3_mutex_leave(db->mutex);
83199 }else{
83200 v = pVdbe->aCounter[op];
83201 if( resetFlag ) pVdbe->aCounter[op] = 0;
83202 }
83203 return (int)v;
83204}
83205
83206/*
83207** Return the SQL associated with a prepared statement
83208*/
83209SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
83210 Vdbe *p = (Vdbe *)pStmt;
83211 return p ? p->zSql : 0;
83212}
83213
83214/*
83215** Return the SQL associated with a prepared statement with
83216** bound parameters expanded. Space to hold the returned string is
83217** obtained from sqlite3_malloc(). The caller is responsible for
83218** freeing the returned string by passing it to sqlite3_free().
83219**
83220** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of
83221** expanded bound parameters.
83222*/
83223SQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){
83224#ifdef SQLITE_OMIT_TRACE
83225 return 0;
83226#else
83227 char *z = 0;
83228 const char *zSql = sqlite3_sql(pStmt);
83229 if( zSql ){
83230 Vdbe *p = (Vdbe *)pStmt;
83231 sqlite3_mutex_enter(p->db->mutex);
83232 z = sqlite3VdbeExpandSql(p, zSql);
83233 sqlite3_mutex_leave(p->db->mutex);
83234 }
83235 return z;
83236#endif
83237}
83238
83239#ifdef SQLITE_ENABLE_NORMALIZE
83240/*
83241** Return the normalized SQL associated with a prepared statement.
83242*/
83243SQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){
83244 Vdbe *p = (Vdbe *)pStmt;
83245 if( p==0 ) return 0;
83246 if( p->zNormSql==0 && ALWAYS(p->zSql!=0)(p->zSql!=0) ){
83247 sqlite3_mutex_enter(p->db->mutex);
83248 p->zNormSql = sqlite3Normalize(p, p->zSql);
83249 sqlite3_mutex_leave(p->db->mutex);
83250 }
83251 return p->zNormSql;
83252}
83253#endif /* SQLITE_ENABLE_NORMALIZE */
83254
83255#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
83256/*
83257** Allocate and populate an UnpackedRecord structure based on the serialized
83258** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure
83259** if successful, or a NULL pointer if an OOM error is encountered.
83260*/
83261static UnpackedRecord *vdbeUnpackRecord(
83262 KeyInfo *pKeyInfo,
83263 int nKey,
83264 const void *pKey
83265){
83266 UnpackedRecord *pRet; /* Return value */
83267
83268 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
83269 if( pRet ){
83270 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
83271 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet);
83272 }
83273 return pRet;
83274}
83275
83276/*
83277** This function is called from within a pre-update callback to retrieve
83278** a field of the row currently being updated or deleted.
83279*/
83280SQLITE_API int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
83281 PreUpdate *p = db->pPreUpdate;
83282 Mem *pMem;
83283 int rc = SQLITE_OK0;
83284
83285 /* Test that this call is being made from within an SQLITE_DELETE or
83286 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
83287 if( !p || p->op==SQLITE_INSERT18 ){
83288 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(83288);
83289 goto preupdate_old_out;
83290 }
83291 if( p->pPk ){
83292 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
83293 }
83294 if( iIdx>=p->pCsr->nField || iIdx<0 ){
83295 rc = SQLITE_RANGE25;
83296 goto preupdate_old_out;
83297 }
83298
83299 /* If the old.* record has not yet been loaded into memory, do so now. */
83300 if( p->pUnpacked==0 ){
83301 u32 nRec;
83302 u8 *aRec;
83303
83304 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
83305 aRec = sqlite3DbMallocRaw(db, nRec);
83306 if( !aRec ) goto preupdate_old_out;
83307 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
83308 if( rc==SQLITE_OK0 ){
83309 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
83310 if( !p->pUnpacked ) rc = SQLITE_NOMEM7;
83311 }
83312 if( rc!=SQLITE_OK0 ){
83313 sqlite3DbFree(db, aRec);
83314 goto preupdate_old_out;
83315 }
83316 p->aRecord = aRec;
83317 }
83318
83319 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
83320 if( iIdx==p->pTab->iPKey ){
83321 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
83322 }else if( iIdx>=p->pUnpacked->nField ){
83323 *ppValue = (sqlite3_value *)columnNullValue();
83324 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL'E' ){
83325 if( pMem->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
83326 testcase( pMem->flags & MEM_Int );
83327 testcase( pMem->flags & MEM_IntReal );
83328 sqlite3VdbeMemRealify(pMem);
83329 }
83330 }
83331
83332 preupdate_old_out:
83333 sqlite3Error(db, rc);
83334 return sqlite3ApiExit(db, rc);
83335}
83336#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
83337
83338#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
83339/*
83340** This function is called from within a pre-update callback to retrieve
83341** the number of columns in the row being updated, deleted or inserted.
83342*/
83343SQLITE_API int sqlite3_preupdate_count(sqlite3 *db){
83344 PreUpdate *p = db->pPreUpdate;
83345 return (p ? p->keyinfo.nKeyField : 0);
83346}
83347#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
83348
83349#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
83350/*
83351** This function is designed to be called from within a pre-update callback
83352** only. It returns zero if the change that caused the callback was made
83353** immediately by a user SQL statement. Or, if the change was made by a
83354** trigger program, it returns the number of trigger programs currently
83355** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
83356** top-level trigger etc.).
83357**
83358** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL
83359** or SET DEFAULT action is considered a trigger.
83360*/
83361SQLITE_API int sqlite3_preupdate_depth(sqlite3 *db){
83362 PreUpdate *p = db->pPreUpdate;
83363 return (p ? p->v->nFrame : 0);
83364}
83365#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
83366
83367#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
83368/*
83369** This function is called from within a pre-update callback to retrieve
83370** a field of the row currently being updated or inserted.
83371*/
83372SQLITE_API int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){
83373 PreUpdate *p = db->pPreUpdate;
83374 int rc = SQLITE_OK0;
83375 Mem *pMem;
83376
83377 if( !p || p->op==SQLITE_DELETE9 ){
83378 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(83378);
83379 goto preupdate_new_out;
83380 }
83381 if( p->pPk && p->op!=SQLITE_UPDATE23 ){
83382 iIdx = sqlite3ColumnOfIndex(p->pPk, iIdx);
83383 }
83384 if( iIdx>=p->pCsr->nField || iIdx<0 ){
83385 rc = SQLITE_RANGE25;
83386 goto preupdate_new_out;
83387 }
83388
83389 if( p->op==SQLITE_INSERT18 ){
83390 /* For an INSERT, memory cell p->iNewReg contains the serialized record
83391 ** that is being inserted. Deserialize it. */
83392 UnpackedRecord *pUnpack = p->pNewUnpacked;
83393 if( !pUnpack ){
83394 Mem *pData = &p->v->aMem[p->iNewReg];
83395 rc = ExpandBlob(pData)(((pData)->flags&0x4000)?sqlite3VdbeMemExpandBlob(pData
):0)
;
83396 if( rc!=SQLITE_OK0 ) goto preupdate_new_out;
83397 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
83398 if( !pUnpack ){
83399 rc = SQLITE_NOMEM7;
83400 goto preupdate_new_out;
83401 }
83402 p->pNewUnpacked = pUnpack;
83403 }
83404 pMem = &pUnpack->aMem[iIdx];
83405 if( iIdx==p->pTab->iPKey ){
83406 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
83407 }else if( iIdx>=pUnpack->nField ){
83408 pMem = (sqlite3_value *)columnNullValue();
83409 }
83410 }else{
83411 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
83412 ** value. Make a copy of the cell contents and return a pointer to it.
83413 ** It is not safe to return a pointer to the memory cell itself as the
83414 ** caller may modify the value text encoding.
83415 */
83416 assert( p->op==SQLITE_UPDATE )((void) (0));
83417 if( !p->aNew ){
83418 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
83419 if( !p->aNew ){
83420 rc = SQLITE_NOMEM7;
83421 goto preupdate_new_out;
83422 }
83423 }
83424 assert( iIdx>=0 && iIdx<p->pCsr->nField )((void) (0));
83425 pMem = &p->aNew[iIdx];
83426 if( pMem->flags==0 ){
83427 if( iIdx==p->pTab->iPKey ){
83428 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
83429 }else{
83430 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
83431 if( rc!=SQLITE_OK0 ) goto preupdate_new_out;
83432 }
83433 }
83434 }
83435 *ppValue = pMem;
83436
83437 preupdate_new_out:
83438 sqlite3Error(db, rc);
83439 return sqlite3ApiExit(db, rc);
83440}
83441#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
83442
83443#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
83444/*
83445** Return status data for a single loop within query pStmt.
83446*/
83447SQLITE_API int sqlite3_stmt_scanstatus(
83448 sqlite3_stmt *pStmt, /* Prepared statement being queried */
83449 int idx, /* Index of loop to report on */
83450 int iScanStatusOp, /* Which metric to return */
83451 void *pOut /* OUT: Write the answer here */
83452){
83453 Vdbe *p = (Vdbe*)pStmt;
83454 ScanStatus *pScan;
83455 if( idx<0 || idx>=p->nScan ) return 1;
83456 pScan = &p->aScan[idx];
83457 switch( iScanStatusOp ){
83458 case SQLITE_SCANSTAT_NLOOP0: {
83459 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
83460 break;
83461 }
83462 case SQLITE_SCANSTAT_NVISIT1: {
83463 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
83464 break;
83465 }
83466 case SQLITE_SCANSTAT_EST2: {
83467 double r = 1.0;
83468 LogEst x = pScan->nEst;
83469 while( x<100 ){
83470 x += 10;
83471 r *= 0.5;
83472 }
83473 *(double*)pOut = r*sqlite3LogEstToInt(x);
83474 break;
83475 }
83476 case SQLITE_SCANSTAT_NAME3: {
83477 *(const char**)pOut = pScan->zName;
83478 break;
83479 }
83480 case SQLITE_SCANSTAT_EXPLAIN4: {
83481 if( pScan->addrExplain ){
83482 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
83483 }else{
83484 *(const char**)pOut = 0;
83485 }
83486 break;
83487 }
83488 case SQLITE_SCANSTAT_SELECTID5: {
83489 if( pScan->addrExplain ){
83490 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
83491 }else{
83492 *(int*)pOut = -1;
83493 }
83494 break;
83495 }
83496 default: {
83497 return 1;
83498 }
83499 }
83500 return 0;
83501}
83502
83503/*
83504** Zero all counters associated with the sqlite3_stmt_scanstatus() data.
83505*/
83506SQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
83507 Vdbe *p = (Vdbe*)pStmt;
83508 memset(p->anExec, 0, p->nOp * sizeof(i64));
83509}
83510#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */
83511
83512/************** End of vdbeapi.c *********************************************/
83513/************** Begin file vdbetrace.c ***************************************/
83514/*
83515** 2009 November 25
83516**
83517** The author disclaims copyright to this source code. In place of
83518** a legal notice, here is a blessing:
83519**
83520** May you do good and not evil.
83521** May you find forgiveness for yourself and forgive others.
83522** May you share freely, never taking more than you give.
83523**
83524*************************************************************************
83525**
83526** This file contains code used to insert the values of host parameters
83527** (aka "wildcards") into the SQL text output by sqlite3_trace().
83528**
83529** The Vdbe parse-tree explainer is also found here.
83530*/
83531/* #include "sqliteInt.h" */
83532/* #include "vdbeInt.h" */
83533
83534#ifndef SQLITE_OMIT_TRACE
83535
83536/*
83537** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
83538** bytes in this text up to but excluding the first character in
83539** a host parameter. If the text contains no host parameters, return
83540** the total number of bytes in the text.
83541*/
83542static int findNextHostParameter(const char *zSql, int *pnToken){
83543 int tokenType;
83544 int nTotal = 0;
83545 int n;
83546
83547 *pnToken = 0;
83548 while( zSql[0] ){
83549 n = sqlite3GetToken((u8*)zSql, &tokenType);
83550 assert( n>0 && tokenType!=TK_ILLEGAL )((void) (0));
83551 if( tokenType==TK_VARIABLE148 ){
83552 *pnToken = n;
83553 break;
83554 }
83555 nTotal += n;
83556 zSql += n;
83557 }
83558 return nTotal;
83559}
83560
83561/*
83562** This function returns a pointer to a nul-terminated string in memory
83563** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
83564** string contains a copy of zRawSql but with host parameters expanded to
83565** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
83566** then the returned string holds a copy of zRawSql with "-- " prepended
83567** to each line of text.
83568**
83569** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
83570** then long strings and blobs are truncated to that many bytes. This
83571** can be used to prevent unreasonably large trace strings when dealing
83572** with large (multi-megabyte) strings and blobs.
83573**
83574** The calling function is responsible for making sure the memory returned
83575** is eventually freed.
83576**
83577** ALGORITHM: Scan the input string looking for host parameters in any of
83578** these forms: ?, ?N, $A, @A, :A. Take care to avoid text within
83579** string literals, quoted identifier names, and comments. For text forms,
83580** the host parameter index is found by scanning the prepared
83581** statement for the corresponding OP_Variable opcode. Once the host
83582** parameter index is known, locate the value in p->aVar[]. Then render
83583** the value as a literal in place of the host parameter name.
83584*/
83585SQLITE_PRIVATEstatic char *sqlite3VdbeExpandSql(
83586 Vdbe *p, /* The prepared statement being evaluated */
83587 const char *zRawSql /* Raw text of the SQL statement */
83588){
83589 sqlite3 *db; /* The database connection */
83590 int idx = 0; /* Index of a host parameter */
83591 int nextIndex = 1; /* Index of next ? host parameter */
83592 int n; /* Length of a token prefix */
83593 int nToken; /* Length of the parameter token */
83594 int i; /* Loop counter */
83595 Mem *pVar; /* Value of a host parameter */
83596 StrAccum out; /* Accumulate the output here */
83597#ifndef SQLITE_OMIT_UTF16
83598 Mem utf8; /* Used to convert UTF16 into UTF8 for display */
83599#endif
83600 char zBase[100]; /* Initial working space */
83601
83602 db = p->db;
83603 sqlite3StrAccumInit(&out, 0, zBase, sizeof(zBase),
83604 db->aLimit[SQLITE_LIMIT_LENGTH0]);
83605 if( db->nVdbeExec>1 ){
83606 while( *zRawSql ){
83607 const char *zStart = zRawSql;
83608 while( *(zRawSql++)!='\n' && *zRawSql );
83609 sqlite3_str_append(&out, "-- ", 3);
83610 assert( (zRawSql - zStart) > 0 )((void) (0));
83611 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
83612 }
83613 }else if( p->nVar==0 ){
83614 sqlite3_str_append(&out, zRawSql, sqlite3Strlen30(zRawSql));
83615 }else{
83616 while( zRawSql[0] ){
83617 n = findNextHostParameter(zRawSql, &nToken);
83618 assert( n>0 )((void) (0));
83619 sqlite3_str_append(&out, zRawSql, n);
83620 zRawSql += n;
83621 assert( zRawSql[0] || nToken==0 )((void) (0));
83622 if( nToken==0 ) break;
83623 if( zRawSql[0]=='?' ){
83624 if( nToken>1 ){
83625 assert( sqlite3Isdigit(zRawSql[1]) )((void) (0));
83626 sqlite3GetInt32(&zRawSql[1], &idx);
83627 }else{
83628 idx = nextIndex;
83629 }
83630 }else{
83631 assert( zRawSql[0]==':' || zRawSql[0]=='$' ||((void) (0))
83632 zRawSql[0]=='@' || zRawSql[0]=='#' )((void) (0));
83633 testcase( zRawSql[0]==':' );
83634 testcase( zRawSql[0]=='$' );
83635 testcase( zRawSql[0]=='@' );
83636 testcase( zRawSql[0]=='#' );
83637 idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
83638 assert( idx>0 )((void) (0));
83639 }
83640 zRawSql += nToken;
83641 nextIndex = idx + 1;
83642 assert( idx>0 && idx<=p->nVar )((void) (0));
83643 pVar = &p->aVar[idx-1];
83644 if( pVar->flags & MEM_Null0x0001 ){
83645 sqlite3_str_append(&out, "NULL", 4);
83646 }else if( pVar->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
83647 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
83648 }else if( pVar->flags & MEM_Real0x0008 ){
83649 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
83650 }else if( pVar->flags & MEM_Str0x0002 ){
83651 int nOut; /* Number of bytes of the string text to include in output */
83652#ifndef SQLITE_OMIT_UTF16
83653 u8 enc = ENC(db)((db)->enc);
83654 if( enc!=SQLITE_UTF81 ){
83655 memset(&utf8, 0, sizeof(utf8));
83656 utf8.db = db;
83657 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC((sqlite3_destructor_type)0));
83658 if( SQLITE_NOMEM7==sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF81) ){
83659 out.accError = SQLITE_NOMEM7;
83660 out.nAlloc = 0;
83661 }
83662 pVar = &utf8;
83663 }
83664#endif
83665 nOut = pVar->n;
83666#ifdef SQLITE_TRACE_SIZE_LIMIT
83667 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
83668 nOut = SQLITE_TRACE_SIZE_LIMIT;
83669 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
83670 }
83671#endif
83672 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
83673#ifdef SQLITE_TRACE_SIZE_LIMIT
83674 if( nOut<pVar->n ){
83675 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
83676 }
83677#endif
83678#ifndef SQLITE_OMIT_UTF16
83679 if( enc!=SQLITE_UTF81 ) sqlite3VdbeMemRelease(&utf8);
83680#endif
83681 }else if( pVar->flags & MEM_Zero0x4000 ){
83682 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
83683 }else{
83684 int nOut; /* Number of bytes of the blob to include in output */
83685 assert( pVar->flags & MEM_Blob )((void) (0));
83686 sqlite3_str_append(&out, "x'", 2);
83687 nOut = pVar->n;
83688#ifdef SQLITE_TRACE_SIZE_LIMIT
83689 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
83690#endif
83691 for(i=0; i<nOut; i++){
83692 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
83693 }
83694 sqlite3_str_append(&out, "'", 1);
83695#ifdef SQLITE_TRACE_SIZE_LIMIT
83696 if( nOut<pVar->n ){
83697 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
83698 }
83699#endif
83700 }
83701 }
83702 }
83703 if( out.accError ) sqlite3_str_reset(&out);
83704 return sqlite3StrAccumFinish(&out);
83705}
83706
83707#endif /* #ifndef SQLITE_OMIT_TRACE */
83708
83709/************** End of vdbetrace.c *******************************************/
83710/************** Begin file vdbe.c ********************************************/
83711/*
83712** 2001 September 15
83713**
83714** The author disclaims copyright to this source code. In place of
83715** a legal notice, here is a blessing:
83716**
83717** May you do good and not evil.
83718** May you find forgiveness for yourself and forgive others.
83719** May you share freely, never taking more than you give.
83720**
83721*************************************************************************
83722** The code in this file implements the function that runs the
83723** bytecode of a prepared statement.
83724**
83725** Various scripts scan this source file in order to generate HTML
83726** documentation, headers files, or other derived files. The formatting
83727** of the code in this file is, therefore, important. See other comments
83728** in this file for details. If in doubt, do not deviate from existing
83729** commenting and indentation practices when changing or adding code.
83730*/
83731/* #include "sqliteInt.h" */
83732/* #include "vdbeInt.h" */
83733
83734/*
83735** Invoke this macro on memory cells just prior to changing the
83736** value of the cell. This macro verifies that shallow copies are
83737** not misused. A shallow copy of a string or blob just copies a
83738** pointer to the string or blob, not the content. If the original
83739** is changed while the copy is still in use, the string or blob might
83740** be changed out from under the copy. This macro verifies that nothing
83741** like that ever happens.
83742*/
83743#ifdef SQLITE_DEBUG
83744# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
83745#else
83746# define memAboutToChange(P,M)
83747#endif
83748
83749/*
83750** The following global variable is incremented every time a cursor
83751** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
83752** procedures use this information to make sure that indices are
83753** working correctly. This variable has no function other than to
83754** help verify the correct operation of the library.
83755*/
83756#ifdef SQLITE_TEST
83757SQLITE_API int sqlite3_search_count = 0;
83758#endif
83759
83760/*
83761** When this global variable is positive, it gets decremented once before
83762** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
83763** field of the sqlite3 structure is set in order to simulate an interrupt.
83764**
83765** This facility is used for testing purposes only. It does not function
83766** in an ordinary build.
83767*/
83768#ifdef SQLITE_TEST
83769SQLITE_API int sqlite3_interrupt_count = 0;
83770#endif
83771
83772/*
83773** The next global variable is incremented each type the OP_Sort opcode
83774** is executed. The test procedures use this information to make sure that
83775** sorting is occurring or not occurring at appropriate times. This variable
83776** has no function other than to help verify the correct operation of the
83777** library.
83778*/
83779#ifdef SQLITE_TEST
83780SQLITE_API int sqlite3_sort_count = 0;
83781#endif
83782
83783/*
83784** The next global variable records the size of the largest MEM_Blob
83785** or MEM_Str that has been used by a VDBE opcode. The test procedures
83786** use this information to make sure that the zero-blob functionality
83787** is working correctly. This variable has no function other than to
83788** help verify the correct operation of the library.
83789*/
83790#ifdef SQLITE_TEST
83791SQLITE_API int sqlite3_max_blobsize = 0;
83792static void updateMaxBlobsize(Mem *p){
83793 if( (p->flags & (MEM_Str0x0002|MEM_Blob0x0010))!=0 && p->n>sqlite3_max_blobsize ){
83794 sqlite3_max_blobsize = p->n;
83795 }
83796}
83797#endif
83798
83799/*
83800** This macro evaluates to true if either the update hook or the preupdate
83801** hook are enabled for database connect DB.
83802*/
83803#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
83804# define HAS_UPDATE_HOOK(DB)((DB)->xUpdateCallback) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
83805#else
83806# define HAS_UPDATE_HOOK(DB)((DB)->xUpdateCallback) ((DB)->xUpdateCallback)
83807#endif
83808
83809/*
83810** The next global variable is incremented each time the OP_Found opcode
83811** is executed. This is used to test whether or not the foreign key
83812** operation implemented using OP_FkIsZero is working. This variable
83813** has no function other than to help verify the correct operation of the
83814** library.
83815*/
83816#ifdef SQLITE_TEST
83817SQLITE_API int sqlite3_found_count = 0;
83818#endif
83819
83820/*
83821** Test a register to see if it exceeds the current maximum blob size.
83822** If it does, record the new maximum blob size.
83823*/
83824#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
83825# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
83826#else
83827# define UPDATE_MAX_BLOBSIZE(P)
83828#endif
83829
83830/*
83831** Invoke the VDBE coverage callback, if that callback is defined. This
83832** feature is used for test suite validation only and does not appear an
83833** production builds.
83834**
83835** M is the type of branch. I is the direction taken for this instance of
83836** the branch.
83837**
83838** M: 2 - two-way branch (I=0: fall-thru 1: jump )
83839** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
83840** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
83841**
83842** In other words, if M is 2, then I is either 0 (for fall-through) or
83843** 1 (for when the branch is taken). If M is 3, the I is 0 for an
83844** ordinary fall-through, I is 1 if the branch was taken, and I is 2
83845** if the result of comparison is NULL. For M=3, I=2 the jump may or
83846** may not be taken, depending on the SQLITE_JUMPIFNULL flags in p5.
83847** When M is 4, that means that an OP_Jump is being run. I is 0, 1, or 2
83848** depending on if the operands are less than, equal, or greater than.
83849**
83850** iSrcLine is the source code line (from the __LINE__ macro) that
83851** generated the VDBE instruction combined with flag bits. The source
83852** code line number is in the lower 24 bits of iSrcLine and the upper
83853** 8 bytes are flags. The lower three bits of the flags indicate
83854** values for I that should never occur. For example, if the branch is
83855** always taken, the flags should be 0x05 since the fall-through and
83856** alternate branch are never taken. If a branch is never taken then
83857** flags should be 0x06 since only the fall-through approach is allowed.
83858**
83859** Bit 0x08 of the flags indicates an OP_Jump opcode that is only
83860** interested in equal or not-equal. In other words, I==0 and I==2
83861** should be treated as equivalent
83862**
83863** Since only a line number is retained, not the filename, this macro
83864** only works for amalgamation builds. But that is ok, since these macros
83865** should be no-ops except for special builds used to measure test coverage.
83866*/
83867#if !defined(SQLITE_VDBE_COVERAGE)
83868# define VdbeBranchTaken(I,M)
83869#else
83870# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
83871 static void vdbeTakeBranch(u32 iSrcLine, u8 I, u8 M){
83872 u8 mNever;
83873 assert( I<=2 )((void) (0)); /* 0: fall through, 1: taken, 2: alternate taken */
83874 assert( M<=4 )((void) (0)); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
83875 assert( I<M )((void) (0)); /* I can only be 2 if M is 3 or 4 */
83876 /* Transform I from a integer [0,1,2] into a bitmask of [1,2,4] */
83877 I = 1<<I;
83878 /* The upper 8 bits of iSrcLine are flags. The lower three bits of
83879 ** the flags indicate directions that the branch can never go. If
83880 ** a branch really does go in one of those directions, assert right
83881 ** away. */
83882 mNever = iSrcLine >> 24;
83883 assert( (I & mNever)==0 )((void) (0));
83884 if( sqlite3GlobalConfigsqlite3Config.xVdbeBranch==0 ) return; /*NO_TEST*/
83885 /* Invoke the branch coverage callback with three arguments:
83886 ** iSrcLine - the line number of the VdbeCoverage() macro, with
83887 ** flags removed.
83888 ** I - Mask of bits 0x07 indicating which cases are are
83889 ** fulfilled by this instance of the jump. 0x01 means
83890 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
83891 ** impossible cases (ex: if the comparison is never NULL)
83892 ** are filled in automatically so that the coverage
83893 ** measurement logic does not flag those impossible cases
83894 ** as missed coverage.
83895 ** M - Type of jump. Same as M argument above
83896 */
83897 I |= mNever;
83898 if( M==2 ) I |= 0x04;
83899 if( M==4 ){
83900 I |= 0x08;
83901 if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
83902 }
83903 sqlite3GlobalConfigsqlite3Config.xVdbeBranch(sqlite3GlobalConfigsqlite3Config.pVdbeBranchArg,
83904 iSrcLine&0xffffff, I, M);
83905 }
83906#endif
83907
83908/*
83909** An ephemeral string value (signified by the MEM_Ephem flag) contains
83910** a pointer to a dynamically allocated string where some other entity
83911** is responsible for deallocating that string. Because the register
83912** does not control the string, it might be deleted without the register
83913** knowing it.
83914**
83915** This routine converts an ephemeral string into a dynamically allocated
83916** string that the register itself controls. In other words, it
83917** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
83918*/
83919#define Deephemeralize(P)if( ((P)->flags&0x1000)!=0 && sqlite3VdbeMemMakeWriteable
(P) ){ goto no_mem;}
\
83920 if( ((P)->flags&MEM_Ephem0x1000)!=0 \
83921 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
83922
83923/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
83924#define isSorter(x)((x)->eCurType==1) ((x)->eCurType==CURTYPE_SORTER1)
83925
83926/*
83927** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
83928** if we run out of memory.
83929*/
83930static VdbeCursor *allocateCursor(
83931 Vdbe *p, /* The virtual machine */
83932 int iCur, /* Index of the new VdbeCursor */
83933 int nField, /* Number of fields in the table or index */
83934 int iDb, /* Database the cursor belongs to, or -1 */
83935 u8 eCurType /* Type of the new cursor */
83936){
83937 /* Find the memory cell that will be used to store the blob of memory
83938 ** required for this VdbeCursor structure. It is convenient to use a
83939 ** vdbe memory cell to manage the memory allocation required for a
83940 ** VdbeCursor structure for the following reasons:
83941 **
83942 ** * Sometimes cursor numbers are used for a couple of different
83943 ** purposes in a vdbe program. The different uses might require
83944 ** different sized allocations. Memory cells provide growable
83945 ** allocations.
83946 **
83947 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
83948 ** be freed lazily via the sqlite3_release_memory() API. This
83949 ** minimizes the number of malloc calls made by the system.
83950 **
83951 ** The memory cell for cursor 0 is aMem[0]. The rest are allocated from
83952 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
83953 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
83954 */
83955 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
83956
83957 int nByte;
83958 VdbeCursor *pCx = 0;
83959 nByte =
83960 ROUND8(sizeof(VdbeCursor))(((sizeof(VdbeCursor))+7)&~7) + 2*sizeof(u32)*nField +
83961 (eCurType==CURTYPE_BTREE0?sqlite3BtreeCursorSize():0);
83962
83963 assert( iCur>=0 && iCur<p->nCursor )((void) (0));
83964 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
83965 /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
83966 ** is clear. Otherwise, if this is an ephemeral cursor created by
83967 ** OP_OpenDup, the cursor will not be closed and will still be part
83968 ** of a BtShared.pCursor list. */
83969 if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0;
83970 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
83971 p->apCsr[iCur] = 0;
83972 }
83973 if( SQLITE_OK0==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
83974 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
83975 memset(pCx, 0, offsetof(VdbeCursor,pAltCursor)__builtin_offsetof(VdbeCursor, pAltCursor));
83976 pCx->eCurType = eCurType;
83977 pCx->iDb = iDb;
83978 pCx->nField = nField;
83979 pCx->aOffset = &pCx->aType[nField];
83980 if( eCurType==CURTYPE_BTREE0 ){
83981 pCx->uc.pCursor = (BtCursor*)
83982 &pMem->z[ROUND8(sizeof(VdbeCursor))(((sizeof(VdbeCursor))+7)&~7)+2*sizeof(u32)*nField];
83983 sqlite3BtreeCursorZero(pCx->uc.pCursor);
83984 }
83985 }
83986 return pCx;
83987}
83988
83989/*
83990** The string in pRec is known to look like an integer and to have a
83991** floating point value of rValue. Return true and set *piValue to the
83992** integer value if the string is in range to be an integer. Otherwise,
83993** return false.
83994*/
83995static int alsoAnInt(Mem *pRec, double rValue, i64 *piValue){
83996 i64 iValue = (double)rValue;
83997 if( sqlite3RealSameAsInt(rValue,iValue) ){
83998 *piValue = iValue;
83999 return 1;
84000 }
84001 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
84002}
84003
84004/*
84005** Try to convert a value into a numeric representation if we can
84006** do so without loss of information. In other words, if the string
84007** looks like a number, convert it into a number. If it does not
84008** look like a number, leave it alone.
84009**
84010** If the bTryForInt flag is true, then extra effort is made to give
84011** an integer representation. Strings that look like floating point
84012** values but which have no fractional component (example: '48.00')
84013** will have a MEM_Int representation when bTryForInt is true.
84014**
84015** If bTryForInt is false, then if the input string contains a decimal
84016** point or exponential notation, the result is only MEM_Real, even
84017** if there is an exact integer representation of the quantity.
84018*/
84019static void applyNumericAffinity(Mem *pRec, int bTryForInt){
84020 double rValue;
84021 u8 enc = pRec->enc;
84022 int rc;
84023 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str )((void) (0));
84024 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
84025 if( rc<=0 ) return;
84026 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
84027 pRec->flags |= MEM_Int0x0004;
84028 }else{
84029 pRec->u.r = rValue;
84030 pRec->flags |= MEM_Real0x0008;
84031 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
84032 }
84033 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
84034 ** string representation after computing a numeric equivalent, because the
84035 ** string representation might not be the canonical representation for the
84036 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
84037 pRec->flags &= ~MEM_Str0x0002;
84038}
84039
84040/*
84041** Processing is determine by the affinity parameter:
84042**
84043** SQLITE_AFF_INTEGER:
84044** SQLITE_AFF_REAL:
84045** SQLITE_AFF_NUMERIC:
84046** Try to convert pRec to an integer representation or a
84047** floating-point representation if an integer representation
84048** is not possible. Note that the integer representation is
84049** always preferred, even if the affinity is REAL, because
84050** an integer representation is more space efficient on disk.
84051**
84052** SQLITE_AFF_TEXT:
84053** Convert pRec to a text representation.
84054**
84055** SQLITE_AFF_BLOB:
84056** No-op. pRec is unchanged.
84057*/
84058static void applyAffinity(
84059 Mem *pRec, /* The value to apply affinity to */
84060 char affinity, /* The affinity to be applied */
84061 u8 enc /* Use this text encoding */
84062){
84063 if( affinity>=SQLITE_AFF_NUMERIC'C' ){
84064 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL((void) (0))
84065 || affinity==SQLITE_AFF_NUMERIC )((void) (0));
84066 if( (pRec->flags & MEM_Int0x0004)==0 ){ /*OPTIMIZATION-IF-FALSE*/
84067 if( (pRec->flags & MEM_Real0x0008)==0 ){
84068 if( pRec->flags & MEM_Str0x0002 ) applyNumericAffinity(pRec,1);
84069 }else{
84070 sqlite3VdbeIntegerAffinity(pRec);
84071 }
84072 }
84073 }else if( affinity==SQLITE_AFF_TEXT'B' ){
84074 /* Only attempt the conversion to TEXT if there is an integer or real
84075 ** representation (blob and NULL do not get converted) but no string
84076 ** representation. It would be harmless to repeat the conversion if
84077 ** there is already a string rep, but it is pointless to waste those
84078 ** CPU cycles. */
84079 if( 0==(pRec->flags&MEM_Str0x0002) ){ /*OPTIMIZATION-IF-FALSE*/
84080 if( (pRec->flags&(MEM_Real0x0008|MEM_Int0x0004|MEM_IntReal0x0020)) ){
84081 testcase( pRec->flags & MEM_Int );
84082 testcase( pRec->flags & MEM_Real );
84083 testcase( pRec->flags & MEM_IntReal );
84084 sqlite3VdbeMemStringify(pRec, enc, 1);
84085 }
84086 }
84087 pRec->flags &= ~(MEM_Real0x0008|MEM_Int0x0004|MEM_IntReal0x0020);
84088 }
84089}
84090
84091/*
84092** Try to convert the type of a function argument or a result column
84093** into a numeric representation. Use either INTEGER or REAL whichever
84094** is appropriate. But only do the conversion if it is possible without
84095** loss of information and return the revised type of the argument.
84096*/
84097SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
84098 int eType = sqlite3_value_type(pVal);
84099 if( eType==SQLITE_TEXT3 ){
84100 Mem *pMem = (Mem*)pVal;
84101 applyNumericAffinity(pMem, 0);
84102 eType = sqlite3_value_type(pVal);
84103 }
84104 return eType;
84105}
84106
84107/*
84108** Exported version of applyAffinity(). This one works on sqlite3_value*,
84109** not the internal Mem* type.
84110*/
84111SQLITE_PRIVATEstatic void sqlite3ValueApplyAffinity(
84112 sqlite3_value *pVal,
84113 u8 affinity,
84114 u8 enc
84115){
84116 applyAffinity((Mem *)pVal, affinity, enc);
84117}
84118
84119/*
84120** pMem currently only holds a string type (or maybe a BLOB that we can
84121** interpret as a string if we want to). Compute its corresponding
84122** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
84123** accordingly.
84124*/
84125static u16 SQLITE_NOINLINE__attribute__((noinline)) computeNumericType(Mem *pMem){
84126 int rc;
84127 sqlite3_int64 ix;
84128 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 )((void) (0));
84129 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 )((void) (0));
84130 ExpandBlob(pMem)(((pMem)->flags&0x4000)?sqlite3VdbeMemExpandBlob(pMem)
:0)
;
84131 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
84132 if( rc<=0 ){
84133 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
84134 pMem->u.i = ix;
84135 return MEM_Int0x0004;
84136 }else{
84137 return MEM_Real0x0008;
84138 }
84139 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
84140 pMem->u.i = ix;
84141 return MEM_Int0x0004;
84142 }
84143 return MEM_Real0x0008;
84144}
84145
84146/*
84147** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
84148** none.
84149**
84150** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
84151** But it does set pMem->u.r and pMem->u.i appropriately.
84152*/
84153static u16 numericType(Mem *pMem){
84154 if( pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020) ){
84155 testcase( pMem->flags & MEM_Int );
84156 testcase( pMem->flags & MEM_Real );
84157 testcase( pMem->flags & MEM_IntReal );
84158 return pMem->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020);
84159 }
84160 if( pMem->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
84161 testcase( pMem->flags & MEM_Str );
84162 testcase( pMem->flags & MEM_Blob );
84163 return computeNumericType(pMem);
84164 }
84165 return 0;
84166}
84167
84168#ifdef SQLITE_DEBUG
84169/*
84170** Write a nice string representation of the contents of cell pMem
84171** into buffer zBuf, length nBuf.
84172*/
84173SQLITE_PRIVATEstatic void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
84174 char *zCsr = zBuf;
84175 int f = pMem->flags;
84176
84177 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
84178
84179 if( f&MEM_Blob0x0010 ){
84180 int i;
84181 char c;
84182 if( f & MEM_Dyn0x0400 ){
84183 c = 'z';
84184 assert( (f & (MEM_Static|MEM_Ephem))==0 )((void) (0));
84185 }else if( f & MEM_Static0x0800 ){
84186 c = 't';
84187 assert( (f & (MEM_Dyn|MEM_Ephem))==0 )((void) (0));
84188 }else if( f & MEM_Ephem0x1000 ){
84189 c = 'e';
84190 assert( (f & (MEM_Static|MEM_Dyn))==0 )((void) (0));
84191 }else{
84192 c = 's';
84193 }
84194 *(zCsr++) = c;
84195 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
84196 zCsr += sqlite3Strlen30(zCsr);
84197 for(i=0; i<16 && i<pMem->n; i++){
84198 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
84199 zCsr += sqlite3Strlen30(zCsr);
84200 }
84201 for(i=0; i<16 && i<pMem->n; i++){
84202 char z = pMem->z[i];
84203 if( z<32 || z>126 ) *zCsr++ = '.';
84204 else *zCsr++ = z;
84205 }
84206 *(zCsr++) = ']';
84207 if( f & MEM_Zero0x4000 ){
84208 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
84209 zCsr += sqlite3Strlen30(zCsr);
84210 }
84211 *zCsr = '\0';
84212 }else if( f & MEM_Str0x0002 ){
84213 int j, k;
84214 zBuf[0] = ' ';
84215 if( f & MEM_Dyn0x0400 ){
84216 zBuf[1] = 'z';
84217 assert( (f & (MEM_Static|MEM_Ephem))==0 )((void) (0));
84218 }else if( f & MEM_Static0x0800 ){
84219 zBuf[1] = 't';
84220 assert( (f & (MEM_Dyn|MEM_Ephem))==0 )((void) (0));
84221 }else if( f & MEM_Ephem0x1000 ){
84222 zBuf[1] = 'e';
84223 assert( (f & (MEM_Static|MEM_Dyn))==0 )((void) (0));
84224 }else{
84225 zBuf[1] = 's';
84226 }
84227 k = 2;
84228 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
84229 k += sqlite3Strlen30(&zBuf[k]);
84230 zBuf[k++] = '[';
84231 for(j=0; j<15 && j<pMem->n; j++){
84232 u8 c = pMem->z[j];
84233 if( c>=0x20 && c<0x7f ){
84234 zBuf[k++] = c;
84235 }else{
84236 zBuf[k++] = '.';
84237 }
84238 }
84239 zBuf[k++] = ']';
84240 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
84241 k += sqlite3Strlen30(&zBuf[k]);
84242 zBuf[k++] = 0;
84243 }
84244}
84245#endif
84246
84247#ifdef SQLITE_DEBUG
84248/*
84249** Print the value of a register for tracing purposes:
84250*/
84251static void memTracePrint(Mem *p){
84252 if( p->flags & MEM_Undefined0x0080 ){
84253 printf(" undefined");
84254 }else if( p->flags & MEM_Null0x0001 ){
84255 printf(p->flags & MEM_Zero0x4000 ? " NULL-nochng" : " NULL");
84256 }else if( (p->flags & (MEM_Int0x0004|MEM_Str0x0002))==(MEM_Int0x0004|MEM_Str0x0002) ){
84257 printf(" si:%lld", p->u.i);
84258 }else if( (p->flags & (MEM_IntReal0x0020))!=0 ){
84259 printf(" ir:%lld", p->u.i);
84260 }else if( p->flags & MEM_Int0x0004 ){
84261 printf(" i:%lld", p->u.i);
84262#ifndef SQLITE_OMIT_FLOATING_POINT
84263 }else if( p->flags & MEM_Real0x0008 ){
84264 printf(" r:%g", p->u.r);
84265#endif
84266 }else if( sqlite3VdbeMemIsRowSet(p) ){
84267 printf(" (rowset)");
84268 }else{
84269 char zBuf[200];
84270 sqlite3VdbeMemPrettyPrint(p, zBuf);
84271 printf(" %s", zBuf);
84272 }
84273 if( p->flags & MEM_Subtype0x8000 ) printf(" subtype=0x%02x", p->eSubtype);
84274}
84275static void registerTrace(int iReg, Mem *p){
84276 printf("REG[%d] = ", iReg);
84277 memTracePrint(p);
84278 printf("\n");
84279 sqlite3VdbeCheckMemInvariants(p);
84280}
84281#endif
84282
84283#ifdef SQLITE_DEBUG
84284# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
84285#else
84286# define REGISTER_TRACE(R,M)
84287#endif
84288
84289
84290#ifdef VDBE_PROFILE
84291
84292/*
84293** hwtime.h contains inline assembler code for implementing
84294** high-performance timing routines.
84295*/
84296/************** Include hwtime.h in the middle of vdbe.c *********************/
84297/************** Begin file hwtime.h ******************************************/
84298/*
84299** 2008 May 27
84300**
84301** The author disclaims copyright to this source code. In place of
84302** a legal notice, here is a blessing:
84303**
84304** May you do good and not evil.
84305** May you find forgiveness for yourself and forgive others.
84306** May you share freely, never taking more than you give.
84307**
84308******************************************************************************
84309**
84310** This file contains inline asm code for retrieving "high-performance"
84311** counters for x86 class CPUs.
84312*/
84313#ifndef SQLITE_HWTIME_H
84314#define SQLITE_HWTIME_H
84315
84316/*
84317** The following routine only works on pentium-class (or newer) processors.
84318** It uses the RDTSC opcode to read the cycle count value out of the
84319** processor and returns that value. This can be used for high-res
84320** profiling.
84321*/
84322#if (defined(__GNUC__4) || defined(_MSC_VER)) && \
84323 (defined(i386) || defined(__i386__) || defined(_M_IX86))
84324
84325 #if defined(__GNUC__4)
84326
84327 __inline__ sqlite_uint64 sqlite3Hwtime(void){
84328 unsigned int lo, hi;
84329 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
84330 return (sqlite_uint64)hi << 32 | lo;
84331 }
84332
84333 #elif defined(_MSC_VER)
84334
84335 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
84336 __asm {
84337 rdtsc
84338 ret ; return value at EDX:EAX
84339 }
84340 }
84341
84342 #endif
84343
84344#elif (defined(__GNUC__4) && defined(__x86_64__1))
84345
84346 __inline__ sqlite_uint64 sqlite3Hwtime(void){
84347 unsigned long val;
84348 __asm__ __volatile__ ("rdtsc" : "=A" (val));
84349 return val;
84350 }
84351
84352#elif (defined(__GNUC__4) && defined(__ppc__))
84353
84354 __inline__ sqlite_uint64 sqlite3Hwtime(void){
84355 unsigned long long retval;
84356 unsigned long junk;
84357 __asm__ __volatile__ ("\n\
84358 1: mftbu %1\n\
84359 mftb %L0\n\
84360 mftbu %0\n\
84361 cmpw %0,%1\n\
84362 bne 1b"
84363 : "=r" (retval), "=r" (junk));
84364 return retval;
84365 }
84366
84367#else
84368
84369 #error Need implementation of sqlite3Hwtime() for your platform.
84370
84371 /*
84372 ** To compile without implementing sqlite3Hwtime() for your platform,
84373 ** you can remove the above #error and use the following
84374 ** stub function. You will lose timing support for many
84375 ** of the debugging and testing utilities, but it should at
84376 ** least compile and run.
84377 */
84378SQLITE_PRIVATEstatic sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
84379
84380#endif
84381
84382#endif /* !defined(SQLITE_HWTIME_H) */
84383
84384/************** End of hwtime.h **********************************************/
84385/************** Continuing where we left off in vdbe.c ***********************/
84386
84387#endif
84388
84389#ifndef NDEBUG1
84390/*
84391** This function is only called from within an assert() expression. It
84392** checks that the sqlite3.nTransaction variable is correctly set to
84393** the number of non-transaction savepoints currently in the
84394** linked list starting at sqlite3.pSavepoint.
84395**
84396** Usage:
84397**
84398** assert( checkSavepointCount(db) );
84399*/
84400static int checkSavepointCount(sqlite3 *db){
84401 int n = 0;
84402 Savepoint *p;
84403 for(p=db->pSavepoint; p; p=p->pNext) n++;
84404 assert( n==(db->nSavepoint + db->isTransactionSavepoint) )((void) (0));
84405 return 1;
84406}
84407#endif
84408
84409/*
84410** Return the register of pOp->p2 after first preparing it to be
84411** overwritten with an integer value.
84412*/
84413static SQLITE_NOINLINE__attribute__((noinline)) Mem *out2PrereleaseWithClear(Mem *pOut){
84414 sqlite3VdbeMemSetNull(pOut);
84415 pOut->flags = MEM_Int0x0004;
84416 return pOut;
84417}
84418static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
84419 Mem *pOut;
84420 assert( pOp->p2>0 )((void) (0));
84421 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
84422 pOut = &p->aMem[pOp->p2];
84423 memAboutToChange(p, pOut);
84424 if( VdbeMemDynamic(pOut)(((pOut)->flags&(0x2000|0x0400))!=0) ){ /*OPTIMIZATION-IF-FALSE*/
84425 return out2PrereleaseWithClear(pOut);
84426 }else{
84427 pOut->flags = MEM_Int0x0004;
84428 return pOut;
84429 }
84430}
84431
84432
84433/*
84434** Execute as much of a VDBE program as we can.
84435** This is the core of sqlite3_step().
84436*/
84437SQLITE_PRIVATEstatic int sqlite3VdbeExec(
84438 Vdbe *p /* The VDBE */
84439){
84440 Op *aOp = p->aOp; /* Copy of p->aOp */
84441 Op *pOp = aOp; /* Current operation */
84442#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
84443 Op *pOrigOp; /* Value of pOp at the top of the loop */
84444#endif
84445#ifdef SQLITE_DEBUG
84446 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
84447#endif
84448 int rc = SQLITE_OK0; /* Value to return */
84449 sqlite3 *db = p->db; /* The database */
84450 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
84451 u8 encoding = ENC(db)((db)->enc); /* The database encoding */
84452 int iCompare = 0; /* Result of last comparison */
84453 unsigned nVmStep = 0; /* Number of virtual machine steps */
84454#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
84455 unsigned nProgressLimit; /* Invoke xProgress() when nVmStep reaches this */
84456#endif
84457 Mem *aMem = p->aMem; /* Copy of p->aMem */
84458 Mem *pIn1 = 0; /* 1st input operand */
84459 Mem *pIn2 = 0; /* 2nd input operand */
84460 Mem *pIn3 = 0; /* 3rd input operand */
84461 Mem *pOut = 0; /* Output operand */
84462#ifdef VDBE_PROFILE
84463 u64 start; /* CPU clock count at start of opcode */
84464#endif
84465 /*** INSERT STACK UNION HERE ***/
84466
84467 assert( p->magic==VDBE_MAGIC_RUN )((void) (0)); /* sqlite3_step() verifies this */
84468 sqlite3VdbeEnter(p);
84469#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
84470 if( db->xProgress ){
84471 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP4];
84472 assert( 0 < db->nProgressOps )((void) (0));
84473 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
84474 }else{
84475 nProgressLimit = 0xffffffff;
84476 }
84477#endif
84478 if( p->rc==SQLITE_NOMEM7 ){
84479 /* This happens if a malloc() inside a call to sqlite3_column_text() or
84480 ** sqlite3_column_text16() failed. */
84481 goto no_mem;
84482 }
84483 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY )((void) (0));
84484 assert( p->bIsReader || p->readOnly!=0 )((void) (0));
84485 p->iCurrentTime = 0;
84486 assert( p->explain==0 )((void) (0));
84487 p->pResultSet = 0;
84488 db->busyHandler.nBusy = 0;
84489 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
84490 sqlite3VdbeIOTraceSql(p);
84491#ifdef SQLITE_DEBUG
84492 sqlite3BeginBenignMalloc();
84493 if( p->pc==0
84494 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
84495 ){
84496 int i;
84497 int once = 1;
84498 sqlite3VdbePrintSql(p);
84499 if( p->db->flags & SQLITE_VdbeListing ){
84500 printf("VDBE Program Listing:\n");
84501 for(i=0; i<p->nOp; i++){
84502 sqlite3VdbePrintOp(stdoutstdout, i, &aOp[i]);
84503 }
84504 }
84505 if( p->db->flags & SQLITE_VdbeEQP ){
84506 for(i=0; i<p->nOp; i++){
84507 if( aOp[i].opcode==OP_Explain171 ){
84508 if( once ) printf("VDBE Query Plan:\n");
84509 printf("%s\n", aOp[i].p4.z);
84510 once = 0;
84511 }
84512 }
84513 }
84514 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
84515 }
84516 sqlite3EndBenignMalloc();
84517#endif
84518 for(pOp=&aOp[p->pc]; 1; pOp++){
84519 /* Errors are detected by individual opcodes, with an immediate
84520 ** jumps to abort_due_to_error. */
84521 assert( rc==SQLITE_OK )((void) (0));
84522
84523 assert( pOp>=aOp && pOp<&aOp[p->nOp])((void) (0));
84524#ifdef VDBE_PROFILE
84525 start = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
84526#endif
84527 nVmStep++;
84528#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
84529 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
84530#endif
84531
84532 /* Only allow tracing if SQLITE_DEBUG is defined.
84533 */
84534#ifdef SQLITE_DEBUG
84535 if( db->flags & SQLITE_VdbeTrace ){
84536 sqlite3VdbePrintOp(stdoutstdout, (int)(pOp - aOp), pOp);
84537 }
84538#endif
84539
84540
84541 /* Check to see if we need to simulate an interrupt. This only happens
84542 ** if we have a special test build.
84543 */
84544#ifdef SQLITE_TEST
84545 if( sqlite3_interrupt_count>0 ){
84546 sqlite3_interrupt_count--;
84547 if( sqlite3_interrupt_count==0 ){
84548 sqlite3_interrupt(db);
84549 }
84550 }
84551#endif
84552
84553 /* Sanity checking on other operands */
84554#ifdef SQLITE_DEBUG
84555 {
84556 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
84557 if( (opProperty & OPFLG_IN10x02)!=0 ){
84558 assert( pOp->p1>0 )((void) (0));
84559 assert( pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
84560 assert( memIsValid(&aMem[pOp->p1]) )((void) (0));
84561 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) )((void) (0));
84562 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
84563 }
84564 if( (opProperty & OPFLG_IN20x04)!=0 ){
84565 assert( pOp->p2>0 )((void) (0));
84566 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
84567 assert( memIsValid(&aMem[pOp->p2]) )((void) (0));
84568 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) )((void) (0));
84569 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
84570 }
84571 if( (opProperty & OPFLG_IN30x08)!=0 ){
84572 assert( pOp->p3>0 )((void) (0));
84573 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
84574 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
84575 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) )((void) (0));
84576 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
84577 }
84578 if( (opProperty & OPFLG_OUT20x10)!=0 ){
84579 assert( pOp->p2>0 )((void) (0));
84580 assert( pOp->p2<=(p->nMem+1 - p->nCursor) )((void) (0));
84581 memAboutToChange(p, &aMem[pOp->p2]);
84582 }
84583 if( (opProperty & OPFLG_OUT30x20)!=0 ){
84584 assert( pOp->p3>0 )((void) (0));
84585 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
84586 memAboutToChange(p, &aMem[pOp->p3]);
84587 }
84588 }
84589#endif
84590#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
84591 pOrigOp = pOp;
84592#endif
84593
84594 switch( pOp->opcode ){
84595
84596/*****************************************************************************
84597** What follows is a massive switch statement where each case implements a
84598** separate instruction in the virtual machine. If we follow the usual
84599** indentation conventions, each case should be indented by 6 spaces. But
84600** that is a lot of wasted space on the left margin. So the code within
84601** the switch statement will break with convention and be flush-left. Another
84602** big comment (similar to this one) will mark the point in the code where
84603** we transition back to normal indentation.
84604**
84605** The formatting of each case is important. The makefile for SQLite
84606** generates two C files "opcodes.h" and "opcodes.c" by scanning this
84607** file looking for lines that begin with "case OP_". The opcodes.h files
84608** will be filled with #defines that give unique integer values to each
84609** opcode and the opcodes.c file is filled with an array of strings where
84610** each string is the symbolic name for the corresponding opcode. If the
84611** case statement is followed by a comment of the form "/# same as ... #/"
84612** that comment is used to determine the particular value of the opcode.
84613**
84614** Other keywords in the comment that follows each case are used to
84615** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
84616** Keywords include: in1, in2, in3, out2, out3. See
84617** the mkopcodeh.awk script for additional information.
84618**
84619** Documentation about VDBE opcodes is generated by scanning this file
84620** for lines of that contain "Opcode:". That line and all subsequent
84621** comment lines are used in the generation of the opcode.html documentation
84622** file.
84623**
84624** SUMMARY:
84625**
84626** Formatting is important to scripts that scan this file.
84627** Do not deviate from the formatting style currently in use.
84628**
84629*****************************************************************************/
84630
84631/* Opcode: Goto * P2 * * *
84632**
84633** An unconditional jump to address P2.
84634** The next instruction executed will be
84635** the one at index P2 from the beginning of
84636** the program.
84637**
84638** The P1 parameter is not actually used by this opcode. However, it
84639** is sometimes set to 1 instead of 0 as a hint to the command-line shell
84640** that this Goto is the bottom of a loop and that the lines from P2 down
84641** to the current line should be indented for EXPLAIN output.
84642*/
84643case OP_Goto11: { /* jump */
84644jump_to_p2_and_check_for_interrupt:
84645 pOp = &aOp[pOp->p2 - 1];
84646
84647 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
84648 ** OP_VNext, or OP_SorterNext) all jump here upon
84649 ** completion. Check to see if sqlite3_interrupt() has been called
84650 ** or if the progress callback needs to be invoked.
84651 **
84652 ** This code uses unstructured "goto" statements and does not look clean.
84653 ** But that is not due to sloppy coding habits. The code is written this
84654 ** way for performance, to avoid having to run the interrupt and progress
84655 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
84656 ** faster according to "valgrind --tool=cachegrind" */
84657check_for_interrupt:
84658 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
84659#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
84660 /* Call the progress callback if it is configured and the required number
84661 ** of VDBE ops have been executed (either since this invocation of
84662 ** sqlite3VdbeExec() or since last time the progress callback was called).
84663 ** If the progress callback returns non-zero, exit the virtual machine with
84664 ** a return code SQLITE_ABORT.
84665 */
84666 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
84667 assert( db->nProgressOps!=0 )((void) (0));
84668 nProgressLimit += db->nProgressOps;
84669 if( db->xProgress(db->pProgressArg) ){
84670 nProgressLimit = 0xffffffff;
84671 rc = SQLITE_INTERRUPT9;
84672 goto abort_due_to_error;
84673 }
84674 }
84675#endif
84676
84677 break;
84678}
84679
84680/* Opcode: Gosub P1 P2 * * *
84681**
84682** Write the current address onto register P1
84683** and then jump to address P2.
84684*/
84685case OP_Gosub12: { /* jump */
84686 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
84687 pIn1 = &aMem[pOp->p1];
84688 assert( VdbeMemDynamic(pIn1)==0 )((void) (0));
84689 memAboutToChange(p, pIn1);
84690 pIn1->flags = MEM_Int0x0004;
84691 pIn1->u.i = (int)(pOp-aOp);
84692 REGISTER_TRACE(pOp->p1, pIn1);
84693
84694 /* Most jump operations do a goto to this spot in order to update
84695 ** the pOp pointer. */
84696jump_to_p2:
84697 pOp = &aOp[pOp->p2 - 1];
84698 break;
84699}
84700
84701/* Opcode: Return P1 * * * *
84702**
84703** Jump to the next instruction after the address in register P1. After
84704** the jump, register P1 becomes undefined.
84705*/
84706case OP_Return66: { /* in1 */
84707 pIn1 = &aMem[pOp->p1];
84708 assert( pIn1->flags==MEM_Int )((void) (0));
84709 pOp = &aOp[pIn1->u.i];
84710 pIn1->flags = MEM_Undefined0x0080;
84711 break;
84712}
84713
84714/* Opcode: InitCoroutine P1 P2 P3 * *
84715**
84716** Set up register P1 so that it will Yield to the coroutine
84717** located at address P3.
84718**
84719** If P2!=0 then the coroutine implementation immediately follows
84720** this opcode. So jump over the coroutine implementation to
84721** address P2.
84722**
84723** See also: EndCoroutine
84724*/
84725case OP_InitCoroutine13: { /* jump */
84726 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
84727 assert( pOp->p2>=0 && pOp->p2<p->nOp )((void) (0));
84728 assert( pOp->p3>=0 && pOp->p3<p->nOp )((void) (0));
84729 pOut = &aMem[pOp->p1];
84730 assert( !VdbeMemDynamic(pOut) )((void) (0));
84731 pOut->u.i = pOp->p3 - 1;
84732 pOut->flags = MEM_Int0x0004;
84733 if( pOp->p2 ) goto jump_to_p2;
84734 break;
84735}
84736
84737/* Opcode: EndCoroutine P1 * * * *
84738**
84739** The instruction at the address in register P1 is a Yield.
84740** Jump to the P2 parameter of that Yield.
84741** After the jump, register P1 becomes undefined.
84742**
84743** See also: InitCoroutine
84744*/
84745case OP_EndCoroutine67: { /* in1 */
84746 VdbeOp *pCaller;
84747 pIn1 = &aMem[pOp->p1];
84748 assert( pIn1->flags==MEM_Int )((void) (0));
84749 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp )((void) (0));
84750 pCaller = &aOp[pIn1->u.i];
84751 assert( pCaller->opcode==OP_Yield )((void) (0));
84752 assert( pCaller->p2>=0 && pCaller->p2<p->nOp )((void) (0));
84753 pOp = &aOp[pCaller->p2 - 1];
84754 pIn1->flags = MEM_Undefined0x0080;
84755 break;
84756}
84757
84758/* Opcode: Yield P1 P2 * * *
84759**
84760** Swap the program counter with the value in register P1. This
84761** has the effect of yielding to a coroutine.
84762**
84763** If the coroutine that is launched by this instruction ends with
84764** Yield or Return then continue to the next instruction. But if
84765** the coroutine launched by this instruction ends with
84766** EndCoroutine, then jump to P2 rather than continuing with the
84767** next instruction.
84768**
84769** See also: InitCoroutine
84770*/
84771case OP_Yield14: { /* in1, jump */
84772 int pcDest;
84773 pIn1 = &aMem[pOp->p1];
84774 assert( VdbeMemDynamic(pIn1)==0 )((void) (0));
84775 pIn1->flags = MEM_Int0x0004;
84776 pcDest = (int)pIn1->u.i;
84777 pIn1->u.i = (int)(pOp - aOp);
84778 REGISTER_TRACE(pOp->p1, pIn1);
84779 pOp = &aOp[pcDest];
84780 break;
84781}
84782
84783/* Opcode: HaltIfNull P1 P2 P3 P4 P5
84784** Synopsis: if r[P3]=null halt
84785**
84786** Check the value in register P3. If it is NULL then Halt using
84787** parameter P1, P2, and P4 as if this were a Halt instruction. If the
84788** value in register P3 is not NULL, then this routine is a no-op.
84789** The P5 parameter should be 1.
84790*/
84791case OP_HaltIfNull68: { /* in3 */
84792 pIn3 = &aMem[pOp->p3];
84793#ifdef SQLITE_DEBUG
84794 if( pOp->p2==OE_Abort2 ){ sqlite3VdbeAssertAbortable(p); }
84795#endif
84796 if( (pIn3->flags & MEM_Null0x0001)==0 ) break;
84797 /* Fall through into OP_Halt */
84798}
84799
84800/* Opcode: Halt P1 P2 * P4 P5
84801**
84802** Exit immediately. All open cursors, etc are closed
84803** automatically.
84804**
84805** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
84806** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
84807** For errors, it can be some other value. If P1!=0 then P2 will determine
84808** whether or not to rollback the current transaction. Do not rollback
84809** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
84810** then back out all changes that have occurred during this execution of the
84811** VDBE, but do not rollback the transaction.
84812**
84813** If P4 is not null then it is an error message string.
84814**
84815** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
84816**
84817** 0: (no change)
84818** 1: NOT NULL contraint failed: P4
84819** 2: UNIQUE constraint failed: P4
84820** 3: CHECK constraint failed: P4
84821** 4: FOREIGN KEY constraint failed: P4
84822**
84823** If P5 is not zero and P4 is NULL, then everything after the ":" is
84824** omitted.
84825**
84826** There is an implied "Halt 0 0 0" instruction inserted at the very end of
84827** every program. So a jump past the last instruction of the program
84828** is the same as executing Halt.
84829*/
84830case OP_Halt69: {
84831 VdbeFrame *pFrame;
84832 int pcx;
84833
84834 pcx = (int)(pOp - aOp);
84835#ifdef SQLITE_DEBUG
84836 if( pOp->p2==OE_Abort2 ){ sqlite3VdbeAssertAbortable(p); }
84837#endif
84838 if( pOp->p1==SQLITE_OK0 && p->pFrame ){
84839 /* Halt the sub-program. Return control to the parent frame. */
84840 pFrame = p->pFrame;
84841 p->pFrame = pFrame->pParent;
84842 p->nFrame--;
84843 sqlite3VdbeSetChanges(db, p->nChange);
84844 pcx = sqlite3VdbeFrameRestore(pFrame);
84845 if( pOp->p2==OE_Ignore4 ){
84846 /* Instruction pcx is the OP_Program that invoked the sub-program
84847 ** currently being halted. If the p2 instruction of this OP_Halt
84848 ** instruction is set to OE_Ignore, then the sub-program is throwing
84849 ** an IGNORE exception. In this case jump to the address specified
84850 ** as the p2 of the calling OP_Program. */
84851 pcx = p->aOp[pcx].p2-1;
84852 }
84853 aOp = p->aOp;
84854 aMem = p->aMem;
84855 pOp = &aOp[pcx];
84856 break;
84857 }
84858 p->rc = pOp->p1;
84859 p->errorAction = (u8)pOp->p2;
84860 p->pc = pcx;
84861 assert( pOp->p5<=4 )((void) (0));
84862 if( p->rc ){
84863 if( pOp->p5 ){
84864 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
84865 "FOREIGN KEY" };
84866 testcase( pOp->p5==1 );
84867 testcase( pOp->p5==2 );
84868 testcase( pOp->p5==3 );
84869 testcase( pOp->p5==4 );
84870 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
84871 if( pOp->p4.z ){
84872 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
84873 }
84874 }else{
84875 sqlite3VdbeError(p, "%s", pOp->p4.z);
84876 }
84877 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
84878 }
84879 rc = sqlite3VdbeHalt(p);
84880 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR )((void) (0));
84881 if( rc==SQLITE_BUSY5 ){
84882 p->rc = SQLITE_BUSY5;
84883 }else{
84884 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT )((void) (0));
84885 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 )((void) (0));
84886 rc = p->rc ? SQLITE_ERROR1 : SQLITE_DONE101;
84887 }
84888 goto vdbe_return;
84889}
84890
84891/* Opcode: Integer P1 P2 * * *
84892** Synopsis: r[P2]=P1
84893**
84894** The 32-bit integer value P1 is written into register P2.
84895*/
84896case OP_Integer70: { /* out2 */
84897 pOut = out2Prerelease(p, pOp);
84898 pOut->u.i = pOp->p1;
84899 break;
84900}
84901
84902/* Opcode: Int64 * P2 * P4 *
84903** Synopsis: r[P2]=P4
84904**
84905** P4 is a pointer to a 64-bit integer value.
84906** Write that value into register P2.
84907*/
84908case OP_Int6471: { /* out2 */
84909 pOut = out2Prerelease(p, pOp);
84910 assert( pOp->p4.pI64!=0 )((void) (0));
84911 pOut->u.i = *pOp->p4.pI64;
84912 break;
84913}
84914
84915#ifndef SQLITE_OMIT_FLOATING_POINT
84916/* Opcode: Real * P2 * P4 *
84917** Synopsis: r[P2]=P4
84918**
84919** P4 is a pointer to a 64-bit floating point value.
84920** Write that value into register P2.
84921*/
84922case OP_Real145: { /* same as TK_FLOAT, out2 */
84923 pOut = out2Prerelease(p, pOp);
84924 pOut->flags = MEM_Real0x0008;
84925 assert( !sqlite3IsNaN(*pOp->p4.pReal) )((void) (0));
84926 pOut->u.r = *pOp->p4.pReal;
84927 break;
84928}
84929#endif
84930
84931/* Opcode: String8 * P2 * P4 *
84932** Synopsis: r[P2]='P4'
84933**
84934** P4 points to a nul terminated UTF-8 string. This opcode is transformed
84935** into a String opcode before it is executed for the first time. During
84936** this transformation, the length of string P4 is computed and stored
84937** as the P1 parameter.
84938*/
84939case OP_String8110: { /* same as TK_STRING, out2 */
84940 assert( pOp->p4.z!=0 )((void) (0));
84941 pOut = out2Prerelease(p, pOp);
84942 pOp->opcode = OP_String72;
84943 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
84944
84945#ifndef SQLITE_OMIT_UTF16
84946 if( encoding!=SQLITE_UTF81 ){
84947 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
84948 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG )((void) (0));
84949 if( rc ) goto too_big;
84950 if( SQLITE_OK0!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
84951 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z )((void) (0));
84952 assert( VdbeMemDynamic(pOut)==0 )((void) (0));
84953 pOut->szMalloc = 0;
84954 pOut->flags |= MEM_Static0x0800;
84955 if( pOp->p4type==P4_DYNAMIC(-7) ){
84956 sqlite3DbFree(db, pOp->p4.z);
84957 }
84958 pOp->p4type = P4_DYNAMIC(-7);
84959 pOp->p4.z = pOut->z;
84960 pOp->p1 = pOut->n;
84961 }
84962#endif
84963 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
84964 goto too_big;
84965 }
84966 assert( rc==SQLITE_OK )((void) (0));
84967 /* Fall through to the next case, OP_String */
84968}
84969
84970/* Opcode: String P1 P2 P3 P4 P5
84971** Synopsis: r[P2]='P4' (len=P1)
84972**
84973** The string value P4 of length P1 (bytes) is stored in register P2.
84974**
84975** If P3 is not zero and the content of register P3 is equal to P5, then
84976** the datatype of the register P2 is converted to BLOB. The content is
84977** the same sequence of bytes, it is merely interpreted as a BLOB instead
84978** of a string, as if it had been CAST. In other words:
84979**
84980** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
84981*/
84982case OP_String72: { /* out2 */
84983 assert( pOp->p4.z!=0 )((void) (0));
84984 pOut = out2Prerelease(p, pOp);
84985 pOut->flags = MEM_Str0x0002|MEM_Static0x0800|MEM_Term0x0200;
84986 pOut->z = pOp->p4.z;
84987 pOut->n = pOp->p1;
84988 pOut->enc = encoding;
84989 UPDATE_MAX_BLOBSIZE(pOut);
84990#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
84991 if( pOp->p3>0 ){
84992 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
84993 pIn3 = &aMem[pOp->p3];
84994 assert( pIn3->flags & MEM_Int )((void) (0));
84995 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob0x0010|MEM_Static0x0800|MEM_Term0x0200;
84996 }
84997#endif
84998 break;
84999}
85000
85001/* Opcode: Null P1 P2 P3 * *
85002** Synopsis: r[P2..P3]=NULL
85003**
85004** Write a NULL into registers P2. If P3 greater than P2, then also write
85005** NULL into register P3 and every register in between P2 and P3. If P3
85006** is less than P2 (typically P3 is zero) then only register P2 is
85007** set to NULL.
85008**
85009** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
85010** NULL values will not compare equal even if SQLITE_NULLEQ is set on
85011** OP_Ne or OP_Eq.
85012*/
85013case OP_Null73: { /* out2 */
85014 int cnt;
85015 u16 nullFlag;
85016 pOut = out2Prerelease(p, pOp);
85017 cnt = pOp->p3-pOp->p2;
85018 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
85019 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null0x0001|MEM_Cleared0x0100) : MEM_Null0x0001;
85020 pOut->n = 0;
85021#ifdef SQLITE_DEBUG
85022 pOut->uTemp = 0;
85023#endif
85024 while( cnt>0 ){
85025 pOut++;
85026 memAboutToChange(p, pOut);
85027 sqlite3VdbeMemSetNull(pOut);
85028 pOut->flags = nullFlag;
85029 pOut->n = 0;
85030 cnt--;
85031 }
85032 break;
85033}
85034
85035/* Opcode: SoftNull P1 * * * *
85036** Synopsis: r[P1]=NULL
85037**
85038** Set register P1 to have the value NULL as seen by the OP_MakeRecord
85039** instruction, but do not free any string or blob memory associated with
85040** the register, so that if the value was a string or blob that was
85041** previously copied using OP_SCopy, the copies will continue to be valid.
85042*/
85043case OP_SoftNull74: {
85044 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
85045 pOut = &aMem[pOp->p1];
85046 pOut->flags = (pOut->flags&~(MEM_Undefined0x0080|MEM_AffMask0x003f))|MEM_Null0x0001;
85047 break;
85048}
85049
85050/* Opcode: Blob P1 P2 * P4 *
85051** Synopsis: r[P2]=P4 (len=P1)
85052**
85053** P4 points to a blob of data P1 bytes long. Store this
85054** blob in register P2.
85055*/
85056case OP_Blob75: { /* out2 */
85057 assert( pOp->p1 <= SQLITE_MAX_LENGTH )((void) (0));
85058 pOut = out2Prerelease(p, pOp);
85059 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
85060 pOut->enc = encoding;
85061 UPDATE_MAX_BLOBSIZE(pOut);
85062 break;
85063}
85064
85065/* Opcode: Variable P1 P2 * P4 *
85066** Synopsis: r[P2]=parameter(P1,P4)
85067**
85068** Transfer the values of bound parameter P1 into register P2
85069**
85070** If the parameter is named, then its name appears in P4.
85071** The P4 value is used by sqlite3_bind_parameter_name().
85072*/
85073case OP_Variable76: { /* out2 */
85074 Mem *pVar; /* Value being transferred */
85075
85076 assert( pOp->p1>0 && pOp->p1<=p->nVar )((void) (0));
85077 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) )((void) (0));
85078 pVar = &p->aVar[pOp->p1 - 1];
85079 if( sqlite3VdbeMemTooBig(pVar) ){
85080 goto too_big;
85081 }
85082 pOut = &aMem[pOp->p2];
85083 if( VdbeMemDynamic(pOut)(((pOut)->flags&(0x2000|0x0400))!=0) ) sqlite3VdbeMemSetNull(pOut);
85084 memcpy(pOut, pVar, MEMCELLSIZE__builtin_offsetof(Mem, zMalloc));
85085 pOut->flags &= ~(MEM_Dyn0x0400|MEM_Ephem0x1000);
85086 pOut->flags |= MEM_Static0x0800|MEM_FromBind0x0040;
85087 UPDATE_MAX_BLOBSIZE(pOut);
85088 break;
85089}
85090
85091/* Opcode: Move P1 P2 P3 * *
85092** Synopsis: r[P2@P3]=r[P1@P3]
85093**
85094** Move the P3 values in register P1..P1+P3-1 over into
85095** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
85096** left holding a NULL. It is an error for register ranges
85097** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
85098** for P3 to be less than 1.
85099*/
85100case OP_Move77: {
85101 int n; /* Number of registers left to copy */
85102 int p1; /* Register to copy from */
85103 int p2; /* Register to copy to */
85104
85105 n = pOp->p3;
85106 p1 = pOp->p1;
85107 p2 = pOp->p2;
85108 assert( n>0 && p1>0 && p2>0 )((void) (0));
85109 assert( p1+n<=p2 || p2+n<=p1 )((void) (0));
85110
85111 pIn1 = &aMem[p1];
85112 pOut = &aMem[p2];
85113 do{
85114 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
85115 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
85116 assert( memIsValid(pIn1) )((void) (0));
85117 memAboutToChange(p, pOut);
85118 sqlite3VdbeMemMove(pOut, pIn1);
85119#ifdef SQLITE_DEBUG
85120 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
85121 pOut->pScopyFrom += pOp->p2 - p1;
85122 }
85123#endif
85124 Deephemeralize(pOut)if( ((pOut)->flags&0x1000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
85125 REGISTER_TRACE(p2++, pOut);
85126 pIn1++;
85127 pOut++;
85128 }while( --n );
85129 break;
85130}
85131
85132/* Opcode: Copy P1 P2 P3 * *
85133** Synopsis: r[P2@P3+1]=r[P1@P3+1]
85134**
85135** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
85136**
85137** This instruction makes a deep copy of the value. A duplicate
85138** is made of any string or blob constant. See also OP_SCopy.
85139*/
85140case OP_Copy78: {
85141 int n;
85142
85143 n = pOp->p3;
85144 pIn1 = &aMem[pOp->p1];
85145 pOut = &aMem[pOp->p2];
85146 assert( pOut!=pIn1 )((void) (0));
85147 while( 1 ){
85148 memAboutToChange(p, pOut);
85149 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem0x1000);
85150 Deephemeralize(pOut)if( ((pOut)->flags&0x1000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
85151#ifdef SQLITE_DEBUG
85152 pOut->pScopyFrom = 0;
85153#endif
85154 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
85155 if( (n--)==0 ) break;
85156 pOut++;
85157 pIn1++;
85158 }
85159 break;
85160}
85161
85162/* Opcode: SCopy P1 P2 * * *
85163** Synopsis: r[P2]=r[P1]
85164**
85165** Make a shallow copy of register P1 into register P2.
85166**
85167** This instruction makes a shallow copy of the value. If the value
85168** is a string or blob, then the copy is only a pointer to the
85169** original and hence if the original changes so will the copy.
85170** Worse, if the original is deallocated, the copy becomes invalid.
85171** Thus the program must guarantee that the original will not change
85172** during the lifetime of the copy. Use OP_Copy to make a complete
85173** copy.
85174*/
85175case OP_SCopy79: { /* out2 */
85176 pIn1 = &aMem[pOp->p1];
85177 pOut = &aMem[pOp->p2];
85178 assert( pOut!=pIn1 )((void) (0));
85179 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem0x1000);
85180#ifdef SQLITE_DEBUG
85181 pOut->pScopyFrom = pIn1;
85182 pOut->mScopyFlags = pIn1->flags;
85183#endif
85184 break;
85185}
85186
85187/* Opcode: IntCopy P1 P2 * * *
85188** Synopsis: r[P2]=r[P1]
85189**
85190** Transfer the integer value held in register P1 into register P2.
85191**
85192** This is an optimized version of SCopy that works only for integer
85193** values.
85194*/
85195case OP_IntCopy80: { /* out2 */
85196 pIn1 = &aMem[pOp->p1];
85197 assert( (pIn1->flags & MEM_Int)!=0 )((void) (0));
85198 pOut = &aMem[pOp->p2];
85199 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
85200 break;
85201}
85202
85203/* Opcode: ResultRow P1 P2 * * *
85204** Synopsis: output=r[P1@P2]
85205**
85206** The registers P1 through P1+P2-1 contain a single row of
85207** results. This opcode causes the sqlite3_step() call to terminate
85208** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
85209** structure to provide access to the r(P1)..r(P1+P2-1) values as
85210** the result row.
85211*/
85212case OP_ResultRow81: {
85213 Mem *pMem;
85214 int i;
85215 assert( p->nResColumn==pOp->p2 )((void) (0));
85216 assert( pOp->p1>0 )((void) (0));
85217 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
85218
85219 /* If this statement has violated immediate foreign key constraints, do
85220 ** not return the number of rows modified. And do not RELEASE the statement
85221 ** transaction. It needs to be rolled back. */
85222 if( SQLITE_OK0!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
85223 assert( db->flags&SQLITE_CountRows )((void) (0));
85224 assert( p->usesStmtJournal )((void) (0));
85225 goto abort_due_to_error;
85226 }
85227
85228 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
85229 ** DML statements invoke this opcode to return the number of rows
85230 ** modified to the user. This is the only way that a VM that
85231 ** opens a statement transaction may invoke this opcode.
85232 **
85233 ** In case this is such a statement, close any statement transaction
85234 ** opened by this VM before returning control to the user. This is to
85235 ** ensure that statement-transactions are always nested, not overlapping.
85236 ** If the open statement-transaction is not closed here, then the user
85237 ** may step another VM that opens its own statement transaction. This
85238 ** may lead to overlapping statement transactions.
85239 **
85240 ** The statement transaction is never a top-level transaction. Hence
85241 ** the RELEASE call below can never fail.
85242 */
85243 assert( p->iStatement==0 || db->flags&SQLITE_CountRows )((void) (0));
85244 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE1);
85245 assert( rc==SQLITE_OK )((void) (0));
85246
85247 /* Invalidate all ephemeral cursor row caches */
85248 p->cacheCtr = (p->cacheCtr + 2)|1;
85249
85250 /* Make sure the results of the current row are \000 terminated
85251 ** and have an assigned type. The results are de-ephemeralized as
85252 ** a side effect.
85253 */
85254 pMem = p->pResultSet = &aMem[pOp->p1];
85255 for(i=0; i<pOp->p2; i++){
85256 assert( memIsValid(&pMem[i]) )((void) (0));
85257 Deephemeralize(&pMem[i])if( ((&pMem[i])->flags&0x1000)!=0 && sqlite3VdbeMemMakeWriteable
(&pMem[i]) ){ goto no_mem;}
;
85258 assert( (pMem[i].flags & MEM_Ephem)==0((void) (0))
85259 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 )((void) (0));
85260 sqlite3VdbeMemNulTerminate(&pMem[i]);
85261 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
85262 }
85263 if( db->mallocFailed ) goto no_mem;
85264
85265 if( db->mTrace & SQLITE_TRACE_ROW0x04 ){
85266 db->xTrace(SQLITE_TRACE_ROW0x04, db->pTraceArg, p, 0);
85267 }
85268
85269 /* Return SQLITE_ROW
85270 */
85271 p->pc = (int)(pOp - aOp) + 1;
85272 rc = SQLITE_ROW100;
85273 goto vdbe_return;
85274}
85275
85276/* Opcode: Concat P1 P2 P3 * *
85277** Synopsis: r[P3]=r[P2]+r[P1]
85278**
85279** Add the text in register P1 onto the end of the text in
85280** register P2 and store the result in register P3.
85281** If either the P1 or P2 text are NULL then store NULL in P3.
85282**
85283** P3 = P2 || P1
85284**
85285** It is illegal for P1 and P3 to be the same register. Sometimes,
85286** if P3 is the same register as P2, the implementation is able
85287** to avoid a memcpy().
85288*/
85289case OP_Concat105: { /* same as TK_CONCAT, in1, in2, out3 */
85290 i64 nByte; /* Total size of the output string or blob */
85291 u16 flags1; /* Initial flags for P1 */
85292 u16 flags2; /* Initial flags for P2 */
85293
85294 pIn1 = &aMem[pOp->p1];
85295 pIn2 = &aMem[pOp->p2];
85296 pOut = &aMem[pOp->p3];
85297 testcase( pIn1==pIn2 );
85298 testcase( pOut==pIn2 );
85299 assert( pIn1!=pOut )((void) (0));
85300 flags1 = pIn1->flags;
85301 testcase( flags1 & MEM_Null );
85302 testcase( pIn2->flags & MEM_Null );
85303 if( (flags1 | pIn2->flags) & MEM_Null0x0001 ){
85304 sqlite3VdbeMemSetNull(pOut);
85305 break;
85306 }
85307 if( (flags1 & (MEM_Str0x0002|MEM_Blob0x0010))==0 ){
85308 if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
85309 flags1 = pIn1->flags & ~MEM_Str0x0002;
85310 }else if( (flags1 & MEM_Zero0x4000)!=0 ){
85311 if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
85312 flags1 = pIn1->flags & ~MEM_Str0x0002;
85313 }
85314 flags2 = pIn2->flags;
85315 if( (flags2 & (MEM_Str0x0002|MEM_Blob0x0010))==0 ){
85316 if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
85317 flags2 = pIn2->flags & ~MEM_Str0x0002;
85318 }else if( (flags2 & MEM_Zero0x4000)!=0 ){
85319 if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
85320 flags2 = pIn2->flags & ~MEM_Str0x0002;
85321 }
85322 nByte = pIn1->n + pIn2->n;
85323 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
85324 goto too_big;
85325 }
85326 if( sqlite3VdbeMemGrow(pOut, (int)nByte+3, pOut==pIn2) ){
85327 goto no_mem;
85328 }
85329 MemSetTypeFlag(pOut, MEM_Str)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0002
)
;
85330 if( pOut!=pIn2 ){
85331 memcpy(pOut->z, pIn2->z, pIn2->n);
85332 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) )((void) (0));
85333 pIn2->flags = flags2;
85334 }
85335 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
85336 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) )((void) (0));
85337 pIn1->flags = flags1;
85338 pOut->z[nByte]=0;
85339 pOut->z[nByte+1] = 0;
85340 pOut->z[nByte+2] = 0;
85341 pOut->flags |= MEM_Term0x0200;
85342 pOut->n = (int)nByte;
85343 pOut->enc = encoding;
85344 UPDATE_MAX_BLOBSIZE(pOut);
85345 break;
85346}
85347
85348/* Opcode: Add P1 P2 P3 * *
85349** Synopsis: r[P3]=r[P1]+r[P2]
85350**
85351** Add the value in register P1 to the value in register P2
85352** and store the result in register P3.
85353** If either input is NULL, the result is NULL.
85354*/
85355/* Opcode: Multiply P1 P2 P3 * *
85356** Synopsis: r[P3]=r[P1]*r[P2]
85357**
85358**
85359** Multiply the value in register P1 by the value in register P2
85360** and store the result in register P3.
85361** If either input is NULL, the result is NULL.
85362*/
85363/* Opcode: Subtract P1 P2 P3 * *
85364** Synopsis: r[P3]=r[P2]-r[P1]
85365**
85366** Subtract the value in register P1 from the value in register P2
85367** and store the result in register P3.
85368** If either input is NULL, the result is NULL.
85369*/
85370/* Opcode: Divide P1 P2 P3 * *
85371** Synopsis: r[P3]=r[P2]/r[P1]
85372**
85373** Divide the value in register P1 by the value in register P2
85374** and store the result in register P3 (P3=P2/P1). If the value in
85375** register P1 is zero, then the result is NULL. If either input is
85376** NULL, the result is NULL.
85377*/
85378/* Opcode: Remainder P1 P2 P3 * *
85379** Synopsis: r[P3]=r[P2]%r[P1]
85380**
85381** Compute the remainder after integer register P2 is divided by
85382** register P1 and store the result in register P3.
85383** If the value in register P1 is zero the result is NULL.
85384** If either operand is NULL, the result is NULL.
85385*/
85386case OP_Add100: /* same as TK_PLUS, in1, in2, out3 */
85387case OP_Subtract101: /* same as TK_MINUS, in1, in2, out3 */
85388case OP_Multiply102: /* same as TK_STAR, in1, in2, out3 */
85389case OP_Divide103: /* same as TK_SLASH, in1, in2, out3 */
85390case OP_Remainder104: { /* same as TK_REM, in1, in2, out3 */
85391 u16 flags; /* Combined MEM_* flags from both inputs */
85392 u16 type1; /* Numeric type of left operand */
85393 u16 type2; /* Numeric type of right operand */
85394 i64 iA; /* Integer value of left operand */
85395 i64 iB; /* Integer value of right operand */
85396 double rA; /* Real value of left operand */
85397 double rB; /* Real value of right operand */
85398
85399 pIn1 = &aMem[pOp->p1];
85400 type1 = numericType(pIn1);
85401 pIn2 = &aMem[pOp->p2];
85402 type2 = numericType(pIn2);
85403 pOut = &aMem[pOp->p3];
85404 flags = pIn1->flags | pIn2->flags;
85405 if( (type1 & type2 & MEM_Int0x0004)!=0 ){
85406 iA = pIn1->u.i;
85407 iB = pIn2->u.i;
85408 switch( pOp->opcode ){
85409 case OP_Add100: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
85410 case OP_Subtract101: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
85411 case OP_Multiply102: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
85412 case OP_Divide103: {
85413 if( iA==0 ) goto arithmetic_result_is_null;
85414 if( iA==-1 && iB==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) goto fp_math;
85415 iB /= iA;
85416 break;
85417 }
85418 default: {
85419 if( iA==0 ) goto arithmetic_result_is_null;
85420 if( iA==-1 ) iA = 1;
85421 iB %= iA;
85422 break;
85423 }
85424 }
85425 pOut->u.i = iB;
85426 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0004
)
;
85427 }else if( (flags & MEM_Null0x0001)!=0 ){
85428 goto arithmetic_result_is_null;
85429 }else{
85430fp_math:
85431 rA = sqlite3VdbeRealValue(pIn1);
85432 rB = sqlite3VdbeRealValue(pIn2);
85433 switch( pOp->opcode ){
85434 case OP_Add100: rB += rA; break;
85435 case OP_Subtract101: rB -= rA; break;
85436 case OP_Multiply102: rB *= rA; break;
85437 case OP_Divide103: {
85438 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
85439 if( rA==(double)0 ) goto arithmetic_result_is_null;
85440 rB /= rA;
85441 break;
85442 }
85443 default: {
85444 iA = sqlite3VdbeIntValue(pIn1);
85445 iB = sqlite3VdbeIntValue(pIn2);
85446 if( iA==0 ) goto arithmetic_result_is_null;
85447 if( iA==-1 ) iA = 1;
85448 rB = (double)(iB % iA);
85449 break;
85450 }
85451 }
85452#ifdef SQLITE_OMIT_FLOATING_POINT
85453 pOut->u.i = rB;
85454 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0004
)
;
85455#else
85456 if( sqlite3IsNaN(rB) ){
85457 goto arithmetic_result_is_null;
85458 }
85459 pOut->u.r = rB;
85460 MemSetTypeFlag(pOut, MEM_Real)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0008
)
;
85461#endif
85462 }
85463 break;
85464
85465arithmetic_result_is_null:
85466 sqlite3VdbeMemSetNull(pOut);
85467 break;
85468}
85469
85470/* Opcode: CollSeq P1 * * P4
85471**
85472** P4 is a pointer to a CollSeq object. If the next call to a user function
85473** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
85474** be returned. This is used by the built-in min(), max() and nullif()
85475** functions.
85476**
85477** If P1 is not zero, then it is a register that a subsequent min() or
85478** max() aggregate will set to 1 if the current row is not the minimum or
85479** maximum. The P1 register is initialized to 0 by this instruction.
85480**
85481** The interface used by the implementation of the aforementioned functions
85482** to retrieve the collation sequence set by this opcode is not available
85483** publicly. Only built-in functions have access to this feature.
85484*/
85485case OP_CollSeq82: {
85486 assert( pOp->p4type==P4_COLLSEQ )((void) (0));
85487 if( pOp->p1 ){
85488 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
85489 }
85490 break;
85491}
85492
85493/* Opcode: BitAnd P1 P2 P3 * *
85494** Synopsis: r[P3]=r[P1]&r[P2]
85495**
85496** Take the bit-wise AND of the values in register P1 and P2 and
85497** store the result in register P3.
85498** If either input is NULL, the result is NULL.
85499*/
85500/* Opcode: BitOr P1 P2 P3 * *
85501** Synopsis: r[P3]=r[P1]|r[P2]
85502**
85503** Take the bit-wise OR of the values in register P1 and P2 and
85504** store the result in register P3.
85505** If either input is NULL, the result is NULL.
85506*/
85507/* Opcode: ShiftLeft P1 P2 P3 * *
85508** Synopsis: r[P3]=r[P2]<<r[P1]
85509**
85510** Shift the integer value in register P2 to the left by the
85511** number of bits specified by the integer in register P1.
85512** Store the result in register P3.
85513** If either input is NULL, the result is NULL.
85514*/
85515/* Opcode: ShiftRight P1 P2 P3 * *
85516** Synopsis: r[P3]=r[P2]>>r[P1]
85517**
85518** Shift the integer value in register P2 to the right by the
85519** number of bits specified by the integer in register P1.
85520** Store the result in register P3.
85521** If either input is NULL, the result is NULL.
85522*/
85523case OP_BitAnd96: /* same as TK_BITAND, in1, in2, out3 */
85524case OP_BitOr97: /* same as TK_BITOR, in1, in2, out3 */
85525case OP_ShiftLeft98: /* same as TK_LSHIFT, in1, in2, out3 */
85526case OP_ShiftRight99: { /* same as TK_RSHIFT, in1, in2, out3 */
85527 i64 iA;
85528 u64 uA;
85529 i64 iB;
85530 u8 op;
85531
85532 pIn1 = &aMem[pOp->p1];
85533 pIn2 = &aMem[pOp->p2];
85534 pOut = &aMem[pOp->p3];
85535 if( (pIn1->flags | pIn2->flags) & MEM_Null0x0001 ){
85536 sqlite3VdbeMemSetNull(pOut);
85537 break;
85538 }
85539 iA = sqlite3VdbeIntValue(pIn2);
85540 iB = sqlite3VdbeIntValue(pIn1);
85541 op = pOp->opcode;
85542 if( op==OP_BitAnd96 ){
85543 iA &= iB;
85544 }else if( op==OP_BitOr97 ){
85545 iA |= iB;
85546 }else if( iB!=0 ){
85547 assert( op==OP_ShiftRight || op==OP_ShiftLeft )((void) (0));
85548
85549 /* If shifting by a negative amount, shift in the other direction */
85550 if( iB<0 ){
85551 assert( OP_ShiftRight==OP_ShiftLeft+1 )((void) (0));
85552 op = 2*OP_ShiftLeft98 + 1 - op;
85553 iB = iB>(-64) ? -iB : 64;
85554 }
85555
85556 if( iB>=64 ){
85557 iA = (iA>=0 || op==OP_ShiftLeft98) ? 0 : -1;
85558 }else{
85559 memcpy(&uA, &iA, sizeof(uA));
85560 if( op==OP_ShiftLeft98 ){
85561 uA <<= iB;
85562 }else{
85563 uA >>= iB;
85564 /* Sign-extend on a right shift of a negative number */
85565 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
85566 }
85567 memcpy(&iA, &uA, sizeof(iA));
85568 }
85569 }
85570 pOut->u.i = iA;
85571 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0004
)
;
85572 break;
85573}
85574
85575/* Opcode: AddImm P1 P2 * * *
85576** Synopsis: r[P1]=r[P1]+P2
85577**
85578** Add the constant P2 to the value in register P1.
85579** The result is always an integer.
85580**
85581** To force any register to be an integer, just add 0.
85582*/
85583case OP_AddImm83: { /* in1 */
85584 pIn1 = &aMem[pOp->p1];
85585 memAboutToChange(p, pIn1);
85586 sqlite3VdbeMemIntegerify(pIn1);
85587 pIn1->u.i += pOp->p2;
85588 break;
85589}
85590
85591/* Opcode: MustBeInt P1 P2 * * *
85592**
85593** Force the value in register P1 to be an integer. If the value
85594** in P1 is not an integer and cannot be converted into an integer
85595** without data loss, then jump immediately to P2, or if P2==0
85596** raise an SQLITE_MISMATCH exception.
85597*/
85598case OP_MustBeInt15: { /* jump, in1 */
85599 pIn1 = &aMem[pOp->p1];
85600 if( (pIn1->flags & MEM_Int0x0004)==0 ){
85601 applyAffinity(pIn1, SQLITE_AFF_NUMERIC'C', encoding);
85602 if( (pIn1->flags & MEM_Int0x0004)==0 ){
85603 VdbeBranchTaken(1, 2);
85604 if( pOp->p2==0 ){
85605 rc = SQLITE_MISMATCH20;
85606 goto abort_due_to_error;
85607 }else{
85608 goto jump_to_p2;
85609 }
85610 }
85611 }
85612 VdbeBranchTaken(0, 2);
85613 MemSetTypeFlag(pIn1, MEM_Int)((pIn1)->flags = ((pIn1)->flags&~(0xc1bf|0x4000))|0x0004
)
;
85614 break;
85615}
85616
85617#ifndef SQLITE_OMIT_FLOATING_POINT
85618/* Opcode: RealAffinity P1 * * * *
85619**
85620** If register P1 holds an integer convert it to a real value.
85621**
85622** This opcode is used when extracting information from a column that
85623** has REAL affinity. Such column values may still be stored as
85624** integers, for space efficiency, but after extraction we want them
85625** to have only a real value.
85626*/
85627case OP_RealAffinity84: { /* in1 */
85628 pIn1 = &aMem[pOp->p1];
85629 if( pIn1->flags & (MEM_Int0x0004|MEM_IntReal0x0020) ){
85630 testcase( pIn1->flags & MEM_Int );
85631 testcase( pIn1->flags & MEM_IntReal );
85632 sqlite3VdbeMemRealify(pIn1);
85633 }
85634 break;
85635}
85636#endif
85637
85638#ifndef SQLITE_OMIT_CAST
85639/* Opcode: Cast P1 P2 * * *
85640** Synopsis: affinity(r[P1])
85641**
85642** Force the value in register P1 to be the type defined by P2.
85643**
85644** <ul>
85645** <li> P2=='A' &rarr; BLOB
85646** <li> P2=='B' &rarr; TEXT
85647** <li> P2=='C' &rarr; NUMERIC
85648** <li> P2=='D' &rarr; INTEGER
85649** <li> P2=='E' &rarr; REAL
85650** </ul>
85651**
85652** A NULL value is not changed by this routine. It remains NULL.
85653*/
85654case OP_Cast85: { /* in1 */
85655 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL )((void) (0));
85656 testcase( pOp->p2==SQLITE_AFF_TEXT );
85657 testcase( pOp->p2==SQLITE_AFF_BLOB );
85658 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
85659 testcase( pOp->p2==SQLITE_AFF_INTEGER );
85660 testcase( pOp->p2==SQLITE_AFF_REAL );
85661 pIn1 = &aMem[pOp->p1];
85662 memAboutToChange(p, pIn1);
85663 rc = ExpandBlob(pIn1)(((pIn1)->flags&0x4000)?sqlite3VdbeMemExpandBlob(pIn1)
:0)
;
85664 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
85665 UPDATE_MAX_BLOBSIZE(pIn1);
85666 if( rc ) goto abort_due_to_error;
85667 break;
85668}
85669#endif /* SQLITE_OMIT_CAST */
85670
85671/* Opcode: Eq P1 P2 P3 P4 P5
85672** Synopsis: IF r[P3]==r[P1]
85673**
85674** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
85675** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
85676** store the result of comparison in register P2.
85677**
85678** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
85679** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
85680** to coerce both inputs according to this affinity before the
85681** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
85682** affinity is used. Note that the affinity conversions are stored
85683** back into the input registers P1 and P3. So this opcode can cause
85684** persistent changes to registers P1 and P3.
85685**
85686** Once any conversions have taken place, and neither value is NULL,
85687** the values are compared. If both values are blobs then memcmp() is
85688** used to determine the results of the comparison. If both values
85689** are text, then the appropriate collating function specified in
85690** P4 is used to do the comparison. If P4 is not specified then
85691** memcmp() is used to compare text string. If both values are
85692** numeric, then a numeric comparison is used. If the two values
85693** are of different types, then numbers are considered less than
85694** strings and strings are considered less than blobs.
85695**
85696** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
85697** true or false and is never NULL. If both operands are NULL then the result
85698** of comparison is true. If either operand is NULL then the result is false.
85699** If neither operand is NULL the result is the same as it would be if
85700** the SQLITE_NULLEQ flag were omitted from P5.
85701**
85702** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
85703** content of r[P2] is only changed if the new value is NULL or 0 (false).
85704** In other words, a prior r[P2] value will not be overwritten by 1 (true).
85705*/
85706/* Opcode: Ne P1 P2 P3 P4 P5
85707** Synopsis: IF r[P3]!=r[P1]
85708**
85709** This works just like the Eq opcode except that the jump is taken if
85710** the operands in registers P1 and P3 are not equal. See the Eq opcode for
85711** additional information.
85712**
85713** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
85714** content of r[P2] is only changed if the new value is NULL or 1 (true).
85715** In other words, a prior r[P2] value will not be overwritten by 0 (false).
85716*/
85717/* Opcode: Lt P1 P2 P3 P4 P5
85718** Synopsis: IF r[P3]<r[P1]
85719**
85720** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
85721** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
85722** the result of comparison (0 or 1 or NULL) into register P2.
85723**
85724** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
85725** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
85726** bit is clear then fall through if either operand is NULL.
85727**
85728** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
85729** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
85730** to coerce both inputs according to this affinity before the
85731** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
85732** affinity is used. Note that the affinity conversions are stored
85733** back into the input registers P1 and P3. So this opcode can cause
85734** persistent changes to registers P1 and P3.
85735**
85736** Once any conversions have taken place, and neither value is NULL,
85737** the values are compared. If both values are blobs then memcmp() is
85738** used to determine the results of the comparison. If both values
85739** are text, then the appropriate collating function specified in
85740** P4 is used to do the comparison. If P4 is not specified then
85741** memcmp() is used to compare text string. If both values are
85742** numeric, then a numeric comparison is used. If the two values
85743** are of different types, then numbers are considered less than
85744** strings and strings are considered less than blobs.
85745*/
85746/* Opcode: Le P1 P2 P3 P4 P5
85747** Synopsis: IF r[P3]<=r[P1]
85748**
85749** This works just like the Lt opcode except that the jump is taken if
85750** the content of register P3 is less than or equal to the content of
85751** register P1. See the Lt opcode for additional information.
85752*/
85753/* Opcode: Gt P1 P2 P3 P4 P5
85754** Synopsis: IF r[P3]>r[P1]
85755**
85756** This works just like the Lt opcode except that the jump is taken if
85757** the content of register P3 is greater than the content of
85758** register P1. See the Lt opcode for additional information.
85759*/
85760/* Opcode: Ge P1 P2 P3 P4 P5
85761** Synopsis: IF r[P3]>=r[P1]
85762**
85763** This works just like the Lt opcode except that the jump is taken if
85764** the content of register P3 is greater than or equal to the content of
85765** register P1. See the Lt opcode for additional information.
85766*/
85767case OP_Eq53: /* same as TK_EQ, jump, in1, in3 */
85768case OP_Ne52: /* same as TK_NE, jump, in1, in3 */
85769case OP_Lt56: /* same as TK_LT, jump, in1, in3 */
85770case OP_Le55: /* same as TK_LE, jump, in1, in3 */
85771case OP_Gt54: /* same as TK_GT, jump, in1, in3 */
85772case OP_Ge57: { /* same as TK_GE, jump, in1, in3 */
85773 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
85774 char affinity; /* Affinity to use for comparison */
85775 u16 flags1; /* Copy of initial value of pIn1->flags */
85776 u16 flags3; /* Copy of initial value of pIn3->flags */
85777
85778 pIn1 = &aMem[pOp->p1];
85779 pIn3 = &aMem[pOp->p3];
85780 flags1 = pIn1->flags;
85781 flags3 = pIn3->flags;
85782 if( (flags1 | flags3)&MEM_Null0x0001 ){
85783 /* One or both operands are NULL */
85784 if( pOp->p5 & SQLITE_NULLEQ0x80 ){
85785 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
85786 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
85787 ** or not both operands are null.
85788 */
85789 assert( (flags1 & MEM_Cleared)==0 )((void) (0));
85790 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB )((void) (0));
85791 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
85792 if( (flags1&flags3&MEM_Null0x0001)!=0
85793 && (flags3&MEM_Cleared0x0100)==0
85794 ){
85795 res = 0; /* Operands are equal */
85796 }else{
85797 res = ((flags3 & MEM_Null0x0001) ? -1 : +1); /* Operands are not equal */
85798 }
85799 }else{
85800 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
85801 ** then the result is always NULL.
85802 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
85803 */
85804 if( pOp->p5 & SQLITE_STOREP20x20 ){
85805 pOut = &aMem[pOp->p2];
85806 iCompare = 1; /* Operands are not equal */
85807 memAboutToChange(p, pOut);
85808 MemSetTypeFlag(pOut, MEM_Null)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0001
)
;
85809 REGISTER_TRACE(pOp->p2, pOut);
85810 }else{
85811 VdbeBranchTaken(2,3);
85812 if( pOp->p5 & SQLITE_JUMPIFNULL0x10 ){
85813 goto jump_to_p2;
85814 }
85815 }
85816 break;
85817 }
85818 }else{
85819 /* Neither operand is NULL. Do a comparison. */
85820 affinity = pOp->p5 & SQLITE_AFF_MASK0x47;
85821 if( affinity>=SQLITE_AFF_NUMERIC'C' ){
85822 if( (flags1 | flags3)&MEM_Str0x0002 ){
85823 if( (flags1 & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008|MEM_Str0x0002))==MEM_Str0x0002 ){
85824 applyNumericAffinity(pIn1,0);
85825 assert( flags3==pIn3->flags )((void) (0));
85826 /* testcase( flags3!=pIn3->flags );
85827 ** this used to be possible with pIn1==pIn3, but not since
85828 ** the column cache was removed. The following assignment
85829 ** is essentially a no-op. But, it provides defense-in-depth
85830 ** in case our analysis is incorrect, so it is left in. */
85831 flags3 = pIn3->flags;
85832 }
85833 if( (flags3 & (MEM_Int0x0004|MEM_IntReal0x0020|MEM_Real0x0008|MEM_Str0x0002))==MEM_Str0x0002 ){
85834 applyNumericAffinity(pIn3,0);
85835 }
85836 }
85837 /* Handle the common case of integer comparison here, as an
85838 ** optimization, to avoid a call to sqlite3MemCompare() */
85839 if( (pIn1->flags & pIn3->flags & MEM_Int0x0004)!=0 ){
85840 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
85841 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
85842 res = 0;
85843 goto compare_op;
85844 }
85845 }else if( affinity==SQLITE_AFF_TEXT'B' ){
85846 if( (flags1 & MEM_Str0x0002)==0 && (flags1&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))!=0 ){
85847 testcase( pIn1->flags & MEM_Int );
85848 testcase( pIn1->flags & MEM_Real );
85849 testcase( pIn1->flags & MEM_IntReal );
85850 sqlite3VdbeMemStringify(pIn1, encoding, 1);
85851 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
85852 flags1 = (pIn1->flags & ~MEM_TypeMask0xc1bf) | (flags1 & MEM_TypeMask0xc1bf);
85853 assert( pIn1!=pIn3 )((void) (0));
85854 }
85855 if( (flags3 & MEM_Str0x0002)==0 && (flags3&(MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020))!=0 ){
85856 testcase( pIn3->flags & MEM_Int );
85857 testcase( pIn3->flags & MEM_Real );
85858 testcase( pIn3->flags & MEM_IntReal );
85859 sqlite3VdbeMemStringify(pIn3, encoding, 1);
85860 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
85861 flags3 = (pIn3->flags & ~MEM_TypeMask0xc1bf) | (flags3 & MEM_TypeMask0xc1bf);
85862 }
85863 }
85864 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 )((void) (0));
85865 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
85866 }
85867compare_op:
85868 /* At this point, res is negative, zero, or positive if reg[P1] is
85869 ** less than, equal to, or greater than reg[P3], respectively. Compute
85870 ** the answer to this operator in res2, depending on what the comparison
85871 ** operator actually is. The next block of code depends on the fact
85872 ** that the 6 comparison operators are consecutive integers in this
85873 ** order: NE, EQ, GT, LE, LT, GE */
85874 assert( OP_Eq==OP_Ne+1 )((void) (0)); assert( OP_Gt==OP_Ne+2 )((void) (0)); assert( OP_Le==OP_Ne+3 )((void) (0));
85875 assert( OP_Lt==OP_Ne+4 )((void) (0)); assert( OP_Ge==OP_Ne+5 )((void) (0));
85876 if( res<0 ){ /* ne, eq, gt, le, lt, ge */
85877 static const unsigned char aLTb[] = { 1, 0, 0, 1, 1, 0 };
85878 res2 = aLTb[pOp->opcode - OP_Ne52];
85879 }else if( res==0 ){
85880 static const unsigned char aEQb[] = { 0, 1, 0, 1, 0, 1 };
85881 res2 = aEQb[pOp->opcode - OP_Ne52];
85882 }else{
85883 static const unsigned char aGTb[] = { 1, 0, 1, 0, 0, 1 };
85884 res2 = aGTb[pOp->opcode - OP_Ne52];
85885 }
85886
85887 /* Undo any changes made by applyAffinity() to the input registers. */
85888 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) )((void) (0));
85889 pIn1->flags = flags1;
85890 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) )((void) (0));
85891 pIn3->flags = flags3;
85892
85893 if( pOp->p5 & SQLITE_STOREP20x20 ){
85894 pOut = &aMem[pOp->p2];
85895 iCompare = res;
85896 if( (pOp->p5 & SQLITE_KEEPNULL0x08)!=0 ){
85897 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
85898 ** and prevents OP_Ne from overwriting NULL with 0. This flag
85899 ** is only used in contexts where either:
85900 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
85901 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
85902 ** Therefore it is not necessary to check the content of r[P2] for
85903 ** NULL. */
85904 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq )((void) (0));
85905 assert( res2==0 || res2==1 )((void) (0));
85906 testcase( res2==0 && pOp->opcode==OP_Eq );
85907 testcase( res2==1 && pOp->opcode==OP_Eq );
85908 testcase( res2==0 && pOp->opcode==OP_Ne );
85909 testcase( res2==1 && pOp->opcode==OP_Ne );
85910 if( (pOp->opcode==OP_Eq53)==res2 ) break;
85911 }
85912 memAboutToChange(p, pOut);
85913 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0004
)
;
85914 pOut->u.i = res2;
85915 REGISTER_TRACE(pOp->p2, pOut);
85916 }else{
85917 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
85918 if( res2 ){
85919 goto jump_to_p2;
85920 }
85921 }
85922 break;
85923}
85924
85925/* Opcode: ElseNotEq * P2 * * *
85926**
85927** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
85928** If result of an OP_Eq comparison on the same two operands
85929** would have be NULL or false (0), then then jump to P2.
85930** If the result of an OP_Eq comparison on the two previous operands
85931** would have been true (1), then fall through.
85932*/
85933case OP_ElseNotEq58: { /* same as TK_ESCAPE, jump */
85934 assert( pOp>aOp )((void) (0));
85935 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt )((void) (0));
85936 assert( pOp[-1].p5 & SQLITE_STOREP2 )((void) (0));
85937 VdbeBranchTaken(iCompare!=0, 2);
85938 if( iCompare!=0 ) goto jump_to_p2;
85939 break;
85940}
85941
85942
85943/* Opcode: Permutation * * * P4 *
85944**
85945** Set the permutation used by the OP_Compare operator in the next
85946** instruction. The permutation is stored in the P4 operand.
85947**
85948** The permutation is only valid until the next OP_Compare that has
85949** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
85950** occur immediately prior to the OP_Compare.
85951**
85952** The first integer in the P4 integer array is the length of the array
85953** and does not become part of the permutation.
85954*/
85955case OP_Permutation86: {
85956 assert( pOp->p4type==P4_INTARRAY )((void) (0));
85957 assert( pOp->p4.ai )((void) (0));
85958 assert( pOp[1].opcode==OP_Compare )((void) (0));
85959 assert( pOp[1].p5 & OPFLAG_PERMUTE )((void) (0));
85960 break;
85961}
85962
85963/* Opcode: Compare P1 P2 P3 P4 P5
85964** Synopsis: r[P1@P3] <-> r[P2@P3]
85965**
85966** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
85967** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
85968** the comparison for use by the next OP_Jump instruct.
85969**
85970** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
85971** determined by the most recent OP_Permutation operator. If the
85972** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
85973** order.
85974**
85975** P4 is a KeyInfo structure that defines collating sequences and sort
85976** orders for the comparison. The permutation applies to registers
85977** only. The KeyInfo elements are used sequentially.
85978**
85979** The comparison is a sort comparison, so NULLs compare equal,
85980** NULLs are less than numbers, numbers are less than strings,
85981** and strings are less than blobs.
85982*/
85983case OP_Compare87: {
85984 int n;
85985 int i;
85986 int p1;
85987 int p2;
85988 const KeyInfo *pKeyInfo;
85989 int idx;
85990 CollSeq *pColl; /* Collating sequence to use on this term */
85991 int bRev; /* True for DESCENDING sort order */
85992 int *aPermute; /* The permutation */
85993
85994 if( (pOp->p5 & OPFLAG_PERMUTE0x01)==0 ){
85995 aPermute = 0;
85996 }else{
85997 assert( pOp>aOp )((void) (0));
85998 assert( pOp[-1].opcode==OP_Permutation )((void) (0));
85999 assert( pOp[-1].p4type==P4_INTARRAY )((void) (0));
86000 aPermute = pOp[-1].p4.ai + 1;
86001 assert( aPermute!=0 )((void) (0));
86002 }
86003 n = pOp->p3;
86004 pKeyInfo = pOp->p4.pKeyInfo;
86005 assert( n>0 )((void) (0));
86006 assert( pKeyInfo!=0 )((void) (0));
86007 p1 = pOp->p1;
86008 p2 = pOp->p2;
86009#ifdef SQLITE_DEBUG
86010 if( aPermute ){
86011 int k, mx = 0;
86012 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
86013 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
86014 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
86015 }else{
86016 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
86017 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
86018 }
86019#endif /* SQLITE_DEBUG */
86020 for(i=0; i<n; i++){
86021 idx = aPermute ? aPermute[i] : i;
86022 assert( memIsValid(&aMem[p1+idx]) )((void) (0));
86023 assert( memIsValid(&aMem[p2+idx]) )((void) (0));
86024 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
86025 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
86026 assert( i<pKeyInfo->nKeyField )((void) (0));
86027 pColl = pKeyInfo->aColl[i];
86028 bRev = pKeyInfo->aSortOrder[i];
86029 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
86030 if( iCompare ){
86031 if( bRev ) iCompare = -iCompare;
86032 break;
86033 }
86034 }
86035 break;
86036}
86037
86038/* Opcode: Jump P1 P2 P3 * *
86039**
86040** Jump to the instruction at address P1, P2, or P3 depending on whether
86041** in the most recent OP_Compare instruction the P1 vector was less than
86042** equal to, or greater than the P2 vector, respectively.
86043*/
86044case OP_Jump16: { /* jump */
86045 if( iCompare<0 ){
86046 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
86047 }else if( iCompare==0 ){
86048 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
86049 }else{
86050 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
86051 }
86052 break;
86053}
86054
86055/* Opcode: And P1 P2 P3 * *
86056** Synopsis: r[P3]=(r[P1] && r[P2])
86057**
86058** Take the logical AND of the values in registers P1 and P2 and
86059** write the result into register P3.
86060**
86061** If either P1 or P2 is 0 (false) then the result is 0 even if
86062** the other input is NULL. A NULL and true or two NULLs give
86063** a NULL output.
86064*/
86065/* Opcode: Or P1 P2 P3 * *
86066** Synopsis: r[P3]=(r[P1] || r[P2])
86067**
86068** Take the logical OR of the values in register P1 and P2 and
86069** store the answer in register P3.
86070**
86071** If either P1 or P2 is nonzero (true) then the result is 1 (true)
86072** even if the other input is NULL. A NULL and false or two NULLs
86073** give a NULL output.
86074*/
86075case OP_And44: /* same as TK_AND, in1, in2, out3 */
86076case OP_Or43: { /* same as TK_OR, in1, in2, out3 */
86077 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
86078 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
86079
86080 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
86081 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
86082 if( pOp->opcode==OP_And44 ){
86083 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
86084 v1 = and_logic[v1*3+v2];
86085 }else{
86086 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
86087 v1 = or_logic[v1*3+v2];
86088 }
86089 pOut = &aMem[pOp->p3];
86090 if( v1==2 ){
86091 MemSetTypeFlag(pOut, MEM_Null)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0001
)
;
86092 }else{
86093 pOut->u.i = v1;
86094 MemSetTypeFlag(pOut, MEM_Int)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0004
)
;
86095 }
86096 break;
86097}
86098
86099/* Opcode: IsTrue P1 P2 P3 P4 *
86100** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
86101**
86102** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
86103** IS NOT FALSE operators.
86104**
86105** Interpret the value in register P1 as a boolean value. Store that
86106** boolean (a 0 or 1) in register P2. Or if the value in register P1 is
86107** NULL, then the P3 is stored in register P2. Invert the answer if P4
86108** is 1.
86109**
86110** The logic is summarized like this:
86111**
86112** <ul>
86113** <li> If P3==0 and P4==0 then r[P2] := r[P1] IS TRUE
86114** <li> If P3==1 and P4==1 then r[P2] := r[P1] IS FALSE
86115** <li> If P3==0 and P4==1 then r[P2] := r[P1] IS NOT TRUE
86116** <li> If P3==1 and P4==0 then r[P2] := r[P1] IS NOT FALSE
86117** </ul>
86118*/
86119case OP_IsTrue88: { /* in1, out2 */
86120 assert( pOp->p4type==P4_INT32 )((void) (0));
86121 assert( pOp->p4.i==0 || pOp->p4.i==1 )((void) (0));
86122 assert( pOp->p3==0 || pOp->p3==1 )((void) (0));
86123 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
86124 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
86125 break;
86126}
86127
86128/* Opcode: Not P1 P2 * * *
86129** Synopsis: r[P2]= !r[P1]
86130**
86131** Interpret the value in register P1 as a boolean value. Store the
86132** boolean complement in register P2. If the value in register P1 is
86133** NULL, then a NULL is stored in P2.
86134*/
86135case OP_Not19: { /* same as TK_NOT, in1, out2 */
86136 pIn1 = &aMem[pOp->p1];
86137 pOut = &aMem[pOp->p2];
86138 if( (pIn1->flags & MEM_Null0x0001)==0 ){
86139 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeBooleanValue(pIn1,0));
86140 }else{
86141 sqlite3VdbeMemSetNull(pOut);
86142 }
86143 break;
86144}
86145
86146/* Opcode: BitNot P1 P2 * * *
86147** Synopsis: r[P2]= ~r[P1]
86148**
86149** Interpret the content of register P1 as an integer. Store the
86150** ones-complement of the P1 value into register P2. If P1 holds
86151** a NULL then store a NULL in P2.
86152*/
86153case OP_BitNot107: { /* same as TK_BITNOT, in1, out2 */
86154 pIn1 = &aMem[pOp->p1];
86155 pOut = &aMem[pOp->p2];
86156 sqlite3VdbeMemSetNull(pOut);
86157 if( (pIn1->flags & MEM_Null0x0001)==0 ){
86158 pOut->flags = MEM_Int0x0004;
86159 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
86160 }
86161 break;
86162}
86163
86164/* Opcode: Once P1 P2 * * *
86165**
86166** Fall through to the next instruction the first time this opcode is
86167** encountered on each invocation of the byte-code program. Jump to P2
86168** on the second and all subsequent encounters during the same invocation.
86169**
86170** Top-level programs determine first invocation by comparing the P1
86171** operand against the P1 operand on the OP_Init opcode at the beginning
86172** of the program. If the P1 values differ, then fall through and make
86173** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
86174** the same then take the jump.
86175**
86176** For subprograms, there is a bitmask in the VdbeFrame that determines
86177** whether or not the jump should be taken. The bitmask is necessary
86178** because the self-altering code trick does not work for recursive
86179** triggers.
86180*/
86181case OP_Once17: { /* jump */
86182 u32 iAddr; /* Address of this instruction */
86183 assert( p->aOp[0].opcode==OP_Init )((void) (0));
86184 if( p->pFrame ){
86185 iAddr = (int)(pOp - p->aOp);
86186 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
86187 VdbeBranchTaken(1, 2);
86188 goto jump_to_p2;
86189 }
86190 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
86191 }else{
86192 if( p->aOp[0].p1==pOp->p1 ){
86193 VdbeBranchTaken(1, 2);
86194 goto jump_to_p2;
86195 }
86196 }
86197 VdbeBranchTaken(0, 2);
86198 pOp->p1 = p->aOp[0].p1;
86199 break;
86200}
86201
86202/* Opcode: If P1 P2 P3 * *
86203**
86204** Jump to P2 if the value in register P1 is true. The value
86205** is considered true if it is numeric and non-zero. If the value
86206** in P1 is NULL then take the jump if and only if P3 is non-zero.
86207*/
86208case OP_If18: { /* jump, in1 */
86209 int c;
86210 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
86211 VdbeBranchTaken(c!=0, 2);
86212 if( c ) goto jump_to_p2;
86213 break;
86214}
86215
86216/* Opcode: IfNot P1 P2 P3 * *
86217**
86218** Jump to P2 if the value in register P1 is False. The value
86219** is considered false if it has a numeric value of zero. If the value
86220** in P1 is NULL then take the jump if and only if P3 is non-zero.
86221*/
86222case OP_IfNot20: { /* jump, in1 */
86223 int c;
86224 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
86225 VdbeBranchTaken(c!=0, 2);
86226 if( c ) goto jump_to_p2;
86227 break;
86228}
86229
86230/* Opcode: IsNull P1 P2 * * *
86231** Synopsis: if r[P1]==NULL goto P2
86232**
86233** Jump to P2 if the value in register P1 is NULL.
86234*/
86235case OP_IsNull50: { /* same as TK_ISNULL, jump, in1 */
86236 pIn1 = &aMem[pOp->p1];
86237 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
86238 if( (pIn1->flags & MEM_Null0x0001)!=0 ){
86239 goto jump_to_p2;
86240 }
86241 break;
86242}
86243
86244/* Opcode: NotNull P1 P2 * * *
86245** Synopsis: if r[P1]!=NULL goto P2
86246**
86247** Jump to P2 if the value in register P1 is not NULL.
86248*/
86249case OP_NotNull51: { /* same as TK_NOTNULL, jump, in1 */
86250 pIn1 = &aMem[pOp->p1];
86251 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
86252 if( (pIn1->flags & MEM_Null0x0001)==0 ){
86253 goto jump_to_p2;
86254 }
86255 break;
86256}
86257
86258/* Opcode: IfNullRow P1 P2 P3 * *
86259** Synopsis: if P1.nullRow then r[P3]=NULL, goto P2
86260**
86261** Check the cursor P1 to see if it is currently pointing at a NULL row.
86262** If it is, then set register P3 to NULL and jump immediately to P2.
86263** If P1 is not on a NULL row, then fall through without making any
86264** changes.
86265*/
86266case OP_IfNullRow21: { /* jump */
86267 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
86268 assert( p->apCsr[pOp->p1]!=0 )((void) (0));
86269 if( p->apCsr[pOp->p1]->nullRow ){
86270 sqlite3VdbeMemSetNull(aMem + pOp->p3);
86271 goto jump_to_p2;
86272 }
86273 break;
86274}
86275
86276#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
86277/* Opcode: Offset P1 P2 P3 * *
86278** Synopsis: r[P3] = sqlite_offset(P1)
86279**
86280** Store in register r[P3] the byte offset into the database file that is the
86281** start of the payload for the record at which that cursor P1 is currently
86282** pointing.
86283**
86284** P2 is the column number for the argument to the sqlite_offset() function.
86285** This opcode does not use P2 itself, but the P2 value is used by the
86286** code generator. The P1, P2, and P3 operands to this opcode are the
86287** same as for OP_Column.
86288**
86289** This opcode is only available if SQLite is compiled with the
86290** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
86291*/
86292case OP_Offset89: { /* out3 */
86293 VdbeCursor *pC; /* The VDBE cursor */
86294 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
86295 pC = p->apCsr[pOp->p1];
86296 pOut = &p->aMem[pOp->p3];
86297 if( NEVER(pC==0)(pC==0) || pC->eCurType!=CURTYPE_BTREE0 ){
86298 sqlite3VdbeMemSetNull(pOut);
86299 }else{
86300 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
86301 }
86302 break;
86303}
86304#endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
86305
86306/* Opcode: Column P1 P2 P3 P4 P5
86307** Synopsis: r[P3]=PX
86308**
86309** Interpret the data that cursor P1 points to as a structure built using
86310** the MakeRecord instruction. (See the MakeRecord opcode for additional
86311** information about the format of the data.) Extract the P2-th column
86312** from this record. If there are less that (P2+1)
86313** values in the record, extract a NULL.
86314**
86315** The value extracted is stored in register P3.
86316**
86317** If the record contains fewer than P2 fields, then extract a NULL. Or,
86318** if the P4 argument is a P4_MEM use the value of the P4 argument as
86319** the result.
86320**
86321** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
86322** then the cache of the cursor is reset prior to extracting the column.
86323** The first OP_Column against a pseudo-table after the value of the content
86324** register has changed should have this bit set.
86325**
86326** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
86327** the result is guaranteed to only be used as the argument of a length()
86328** or typeof() function, respectively. The loading of large blobs can be
86329** skipped for length() and all content loading can be skipped for typeof().
86330*/
86331case OP_Column90: {
86332 int p2; /* column number to retrieve */
86333 VdbeCursor *pC; /* The VDBE cursor */
86334 BtCursor *pCrsr; /* The BTree cursor */
86335 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
86336 int len; /* The length of the serialized data for the column */
86337 int i; /* Loop counter */
86338 Mem *pDest; /* Where to write the extracted value */
86339 Mem sMem; /* For storing the record being decoded */
86340 const u8 *zData; /* Part of the record being decoded */
86341 const u8 *zHdr; /* Next unparsed byte of the header */
86342 const u8 *zEndHdr; /* Pointer to first byte after the header */
86343 u64 offset64; /* 64-bit offset */
86344 u32 t; /* A type code from the record header */
86345 Mem *pReg; /* PseudoTable input register */
86346
86347 pC = p->apCsr[pOp->p1];
86348 p2 = pOp->p2;
86349
86350 /* If the cursor cache is stale (meaning it is not currently point at
86351 ** the correct row) then bring it up-to-date by doing the necessary
86352 ** B-Tree seek. */
86353 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
86354 if( rc ) goto abort_due_to_error;
86355
86356 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
86357 pDest = &aMem[pOp->p3];
86358 memAboutToChange(p, pDest);
86359 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
86360 assert( pC!=0 )((void) (0));
86361 assert( p2<pC->nField )((void) (0));
86362 aOffset = pC->aOffset;
86363 assert( pC->eCurType!=CURTYPE_VTAB )((void) (0));
86364 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow )((void) (0));
86365 assert( pC->eCurType!=CURTYPE_SORTER )((void) (0));
86366
86367 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
86368 if( pC->nullRow ){
86369 if( pC->eCurType==CURTYPE_PSEUDO3 ){
86370 /* For the special case of as pseudo-cursor, the seekResult field
86371 ** identifies the register that holds the record */
86372 assert( pC->seekResult>0 )((void) (0));
86373 pReg = &aMem[pC->seekResult];
86374 assert( pReg->flags & MEM_Blob )((void) (0));
86375 assert( memIsValid(pReg) )((void) (0));
86376 pC->payloadSize = pC->szRow = pReg->n;
86377 pC->aRow = (u8*)pReg->z;
86378 }else{
86379 sqlite3VdbeMemSetNull(pDest);
86380 goto op_column_out;
86381 }
86382 }else{
86383 pCrsr = pC->uc.pCursor;
86384 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
86385 assert( pCrsr )((void) (0));
86386 assert( sqlite3BtreeCursorIsValid(pCrsr) )((void) (0));
86387 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
86388 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
86389 assert( pC->szRow<=pC->payloadSize )((void) (0));
86390 assert( pC->szRow<=65536 )((void) (0)); /* Maximum page size is 64KiB */
86391 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH0] ){
86392 goto too_big;
86393 }
86394 }
86395 pC->cacheStatus = p->cacheCtr;
86396 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0])(u8)((*(pC->aRow)<(u8)0x80)?((aOffset[0])=(u32)*(pC->
aRow)),1:sqlite3GetVarint32((pC->aRow),(u32 *)&(aOffset
[0])))
;
86397 pC->nHdrParsed = 0;
86398
86399
86400 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
86401 /* pC->aRow does not have to hold the entire row, but it does at least
86402 ** need to cover the header of the record. If pC->aRow does not contain
86403 ** the complete header, then set it to zero, forcing the header to be
86404 ** dynamically allocated. */
86405 pC->aRow = 0;
86406 pC->szRow = 0;
86407
86408 /* Make sure a corrupt database has not given us an oversize header.
86409 ** Do this now to avoid an oversize memory allocation.
86410 **
86411 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
86412 ** types use so much data space that there can only be 4096 and 32 of
86413 ** them, respectively. So the maximum header length results from a
86414 ** 3-byte type for each of the maximum of 32768 columns plus three
86415 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
86416 */
86417 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
86418 goto op_column_corrupt;
86419 }
86420 }else{
86421 /* This is an optimization. By skipping over the first few tests
86422 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
86423 ** measurable performance gain.
86424 **
86425 ** This branch is taken even if aOffset[0]==0. Such a record is never
86426 ** generated by SQLite, and could be considered corruption, but we
86427 ** accept it for historical reasons. When aOffset[0]==0, the code this
86428 ** branch jumps to reads past the end of the record, but never more
86429 ** than a few bytes. Even if the record occurs at the end of the page
86430 ** content area, the "page header" comes after the page content and so
86431 ** this overread is harmless. Similar overreads can occur for a corrupt
86432 ** database file.
86433 */
86434 zData = pC->aRow;
86435 assert( pC->nHdrParsed<=p2 )((void) (0)); /* Conditional skipped */
86436 testcase( aOffset[0]==0 );
86437 goto op_column_read_header;
86438 }
86439 }
86440
86441 /* Make sure at least the first p2+1 entries of the header have been
86442 ** parsed and valid information is in aOffset[] and pC->aType[].
86443 */
86444 if( pC->nHdrParsed<=p2 ){
86445 /* If there is more header available for parsing in the record, try
86446 ** to extract additional fields up through the p2+1-th field
86447 */
86448 if( pC->iHdrOffset<aOffset[0] ){
86449 /* Make sure zData points to enough of the record to cover the header. */
86450 if( pC->aRow==0 ){
86451 memset(&sMem, 0, sizeof(sMem));
86452 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
86453 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
86454 zData = (u8*)sMem.z;
86455 }else{
86456 zData = pC->aRow;
86457 }
86458
86459 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
86460 op_column_read_header:
86461 i = pC->nHdrParsed;
86462 offset64 = aOffset[i];
86463 zHdr = zData + pC->iHdrOffset;
86464 zEndHdr = zData + aOffset[0];
86465 testcase( zHdr>=zEndHdr );
86466 do{
86467 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
86468 zHdr++;
86469 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
86470 }else{
86471 zHdr += sqlite3GetVarint32(zHdr, &t);
86472 pC->aType[i] = t;
86473 offset64 += sqlite3VdbeSerialTypeLen(t);
86474 }
86475 aOffset[++i] = (u32)(offset64 & 0xffffffff);
86476 }while( i<=p2 && zHdr<zEndHdr );
86477
86478 /* The record is corrupt if any of the following are true:
86479 ** (1) the bytes of the header extend past the declared header size
86480 ** (2) the entire header was used but not all data was used
86481 ** (3) the end of the data extends beyond the end of the record.
86482 */
86483 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
86484 || (offset64 > pC->payloadSize)
86485 ){
86486 if( aOffset[0]==0 ){
86487 i = 0;
86488 zHdr = zEndHdr;
86489 }else{
86490 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
86491 goto op_column_corrupt;
86492 }
86493 }
86494
86495 pC->nHdrParsed = i;
86496 pC->iHdrOffset = (u32)(zHdr - zData);
86497 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
86498 }else{
86499 t = 0;
86500 }
86501
86502 /* If after trying to extract new entries from the header, nHdrParsed is
86503 ** still not up to p2, that means that the record has fewer than p2
86504 ** columns. So the result will be either the default value or a NULL.
86505 */
86506 if( pC->nHdrParsed<=p2 ){
86507 if( pOp->p4type==P4_MEM(-11) ){
86508 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static0x0800);
86509 }else{
86510 sqlite3VdbeMemSetNull(pDest);
86511 }
86512 goto op_column_out;
86513 }
86514 }else{
86515 t = pC->aType[p2];
86516 }
86517
86518 /* Extract the content for the p2+1-th column. Control can only
86519 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
86520 ** all valid.
86521 */
86522 assert( p2<pC->nHdrParsed )((void) (0));
86523 assert( rc==SQLITE_OK )((void) (0));
86524 assert( sqlite3VdbeCheckMemInvariants(pDest) )((void) (0));
86525 if( VdbeMemDynamic(pDest)(((pDest)->flags&(0x2000|0x0400))!=0) ){
86526 sqlite3VdbeMemSetNull(pDest);
86527 }
86528 assert( t==pC->aType[p2] )((void) (0));
86529 if( pC->szRow>=aOffset[p2+1] ){
86530 /* This is the common case where the desired content fits on the original
86531 ** page - where the content is not on an overflow page */
86532 zData = pC->aRow + aOffset[p2];
86533 if( t<12 ){
86534 sqlite3VdbeSerialGet(zData, t, pDest);
86535 }else{
86536 /* If the column value is a string, we need a persistent value, not
86537 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
86538 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
86539 */
86540 static const u16 aFlag[] = { MEM_Blob0x0010, MEM_Str0x0002|MEM_Term0x0200 };
86541 pDest->n = len = (t-12)/2;
86542 pDest->enc = encoding;
86543 if( pDest->szMalloc < len+2 ){
86544 pDest->flags = MEM_Null0x0001;
86545 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
86546 }else{
86547 pDest->z = pDest->zMalloc;
86548 }
86549 memcpy(pDest->z, zData, len);
86550 pDest->z[len] = 0;
86551 pDest->z[len+1] = 0;
86552 pDest->flags = aFlag[t&1];
86553 }
86554 }else{
86555 pDest->enc = encoding;
86556 /* This branch happens only when content is on overflow pages */
86557 if( ((pOp->p5 & (OPFLAG_LENGTHARG0x40|OPFLAG_TYPEOFARG0x80))!=0
86558 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG0x80)!=0))
86559 || (len = sqlite3VdbeSerialTypeLen(t))==0
86560 ){
86561 /* Content is irrelevant for
86562 ** 1. the typeof() function,
86563 ** 2. the length(X) function if X is a blob, and
86564 ** 3. if the content length is zero.
86565 ** So we might as well use bogus content rather than reading
86566 ** content from disk.
86567 **
86568 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
86569 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
86570 ** read up to 16. So 16 bytes of bogus content is supplied.
86571 */
86572 static u8 aZero[16]; /* This is the bogus content */
86573 sqlite3VdbeSerialGet(aZero, t, pDest);
86574 }else{
86575 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
86576 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
86577 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
86578 pDest->flags &= ~MEM_Ephem0x1000;
86579 }
86580 }
86581
86582op_column_out:
86583 UPDATE_MAX_BLOBSIZE(pDest);
86584 REGISTER_TRACE(pOp->p3, pDest);
86585 break;
86586
86587op_column_corrupt:
86588 if( aOp[0].p3>0 ){
86589 pOp = &aOp[aOp[0].p3-1];
86590 break;
86591 }else{
86592 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(86592);
86593 goto abort_due_to_error;
86594 }
86595}
86596
86597/* Opcode: Affinity P1 P2 * P4 *
86598** Synopsis: affinity(r[P1@P2])
86599**
86600** Apply affinities to a range of P2 registers starting with P1.
86601**
86602** P4 is a string that is P2 characters long. The N-th character of the
86603** string indicates the column affinity that should be used for the N-th
86604** memory cell in the range.
86605*/
86606case OP_Affinity91: {
86607 const char *zAffinity; /* The affinity to be applied */
86608
86609 zAffinity = pOp->p4.z;
86610 assert( zAffinity!=0 )((void) (0));
86611 assert( pOp->p2>0 )((void) (0));
86612 assert( zAffinity[pOp->p2]==0 )((void) (0));
86613 pIn1 = &aMem[pOp->p1];
86614 while( 1 /*edit-by-break*/ ){
86615 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] )((void) (0));
86616 assert( memIsValid(pIn1) )((void) (0));
86617 applyAffinity(pIn1, zAffinity[0], encoding);
86618 if( zAffinity[0]==SQLITE_AFF_REAL'E' && (pIn1->flags & MEM_Int0x0004)!=0 ){
86619 /* When applying REAL affinity, if the result is still MEM_Int,
86620 ** indicate that REAL is actually desired */
86621 pIn1->flags |= MEM_IntReal0x0020;
86622 pIn1->flags &= ~MEM_Int0x0004;
86623 }
86624 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
86625 zAffinity++;
86626 if( zAffinity[0]==0 ) break;
86627 pIn1++;
86628 }
86629 break;
86630}
86631
86632/* Opcode: MakeRecord P1 P2 P3 P4 *
86633** Synopsis: r[P3]=mkrec(r[P1@P2])
86634**
86635** Convert P2 registers beginning with P1 into the [record format]
86636** use as a data record in a database table or as a key
86637** in an index. The OP_Column opcode can decode the record later.
86638**
86639** P4 may be a string that is P2 characters long. The N-th character of the
86640** string indicates the column affinity that should be used for the N-th
86641** field of the index key.
86642**
86643** The mapping from character to affinity is given by the SQLITE_AFF_
86644** macros defined in sqliteInt.h.
86645**
86646** If P4 is NULL then all index fields have the affinity BLOB.
86647*/
86648case OP_MakeRecord92: {
86649 Mem *pRec; /* The new record */
86650 u64 nData; /* Number of bytes of data space */
86651 int nHdr; /* Number of bytes of header space */
86652 i64 nByte; /* Data space required for this record */
86653 i64 nZero; /* Number of zero bytes at the end of the record */
86654 int nVarint; /* Number of bytes in a varint */
86655 u32 serial_type; /* Type field */
86656 Mem *pData0; /* First field to be combined into the record */
86657 Mem *pLast; /* Last field of the record */
86658 int nField; /* Number of fields in the record */
86659 char *zAffinity; /* The affinity string for the record */
86660 int file_format; /* File format to use for encoding */
86661 u32 len; /* Length of a field */
86662 u8 *zHdr; /* Where to write next byte of the header */
86663 u8 *zPayload; /* Where to write next byte of the payload */
86664
86665 /* Assuming the record contains N fields, the record format looks
86666 ** like this:
86667 **
86668 ** ------------------------------------------------------------------------
86669 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
86670 ** ------------------------------------------------------------------------
86671 **
86672 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
86673 ** and so forth.
86674 **
86675 ** Each type field is a varint representing the serial type of the
86676 ** corresponding data element (see sqlite3VdbeSerialType()). The
86677 ** hdr-size field is also a varint which is the offset from the beginning
86678 ** of the record to data0.
86679 */
86680 nData = 0; /* Number of bytes of data space */
86681 nHdr = 0; /* Number of bytes of header space */
86682 nZero = 0; /* Number of zero bytes at the end of the record */
86683 nField = pOp->p1;
86684 zAffinity = pOp->p4.z;
86685 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
86686 pData0 = &aMem[nField];
86687 nField = pOp->p2;
86688 pLast = &pData0[nField-1];
86689 file_format = p->minWriteFileFormat;
86690
86691 /* Identify the output register */
86692 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 )((void) (0));
86693 pOut = &aMem[pOp->p3];
86694 memAboutToChange(p, pOut);
86695
86696 /* Apply the requested affinity to all inputs
86697 */
86698 assert( pData0<=pLast )((void) (0));
86699 if( zAffinity ){
86700 pRec = pData0;
86701 do{
86702 applyAffinity(pRec, zAffinity[0], encoding);
86703 if( zAffinity[0]==SQLITE_AFF_REAL'E' && (pRec->flags & MEM_Int0x0004) ){
86704 pRec->flags |= MEM_IntReal0x0020;
86705 pRec->flags &= ~(MEM_Int0x0004);
86706 }
86707 REGISTER_TRACE((int)(pRec-aMem), pRec);
86708 zAffinity++;
86709 pRec++;
86710 assert( zAffinity[0]==0 || pRec<=pLast )((void) (0));
86711 }while( zAffinity[0] );
86712 }
86713
86714#ifdef SQLITE_ENABLE_NULL_TRIM
86715 /* NULLs can be safely trimmed from the end of the record, as long as
86716 ** as the schema format is 2 or more and none of the omitted columns
86717 ** have a non-NULL default value. Also, the record must be left with
86718 ** at least one field. If P5>0 then it will be one more than the
86719 ** index of the right-most column with a non-NULL default value */
86720 if( pOp->p5 ){
86721 while( (pLast->flags & MEM_Null0x0001)!=0 && nField>pOp->p5 ){
86722 pLast--;
86723 nField--;
86724 }
86725 }
86726#endif
86727
86728 /* Loop through the elements that will make up the record to figure
86729 ** out how much space is required for the new record.
86730 */
86731 pRec = pLast;
86732 do{
86733 assert( memIsValid(pRec) )((void) (0));
86734 serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
86735 if( pRec->flags & MEM_Zero0x4000 ){
86736 if( serial_type==0 ){
86737 /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
86738 ** table methods that never invoke sqlite3_result_xxxxx() while
86739 ** computing an unchanging column value in an UPDATE statement.
86740 ** Give such values a special internal-use-only serial-type of 10
86741 ** so that they can be passed through to xUpdate and have
86742 ** a true sqlite3_value_nochange(). */
86743 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB )((void) (0));
86744 serial_type = 10;
86745 }else if( nData ){
86746 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
86747 }else{
86748 nZero += pRec->u.nZero;
86749 len -= pRec->u.nZero;
86750 }
86751 }
86752 nData += len;
86753 testcase( serial_type==127 );
86754 testcase( serial_type==128 );
86755 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
86756 pRec->uTemp = serial_type;
86757 if( pRec==pData0 ) break;
86758 pRec--;
86759 }while(1);
86760
86761 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
86762 ** which determines the total number of bytes in the header. The varint
86763 ** value is the size of the header in bytes including the size varint
86764 ** itself. */
86765 testcase( nHdr==126 );
86766 testcase( nHdr==127 );
86767 if( nHdr<=126 ){
86768 /* The common case */
86769 nHdr += 1;
86770 }else{
86771 /* Rare case of a really large header */
86772 nVarint = sqlite3VarintLen(nHdr);
86773 nHdr += nVarint;
86774 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
86775 }
86776 nByte = nHdr+nData;
86777
86778 /* Make sure the output register has a buffer large enough to store
86779 ** the new record. The output register (pOp->p3) is not allowed to
86780 ** be one of the input registers (because the following call to
86781 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
86782 */
86783 if( nByte+nZero<=pOut->szMalloc ){
86784 /* The output register is already large enough to hold the record.
86785 ** No error checks or buffer enlargement is required */
86786 pOut->z = pOut->zMalloc;
86787 }else{
86788 /* Need to make sure that the output is not too big and then enlarge
86789 ** the output register to hold the full result */
86790 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
86791 goto too_big;
86792 }
86793 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
86794 goto no_mem;
86795 }
86796 }
86797 pOut->n = (int)nByte;
86798 pOut->flags = MEM_Blob0x0010;
86799 if( nZero ){
86800 pOut->u.nZero = nZero;
86801 pOut->flags |= MEM_Zero0x4000;
86802 }
86803 UPDATE_MAX_BLOBSIZE(pOut);
86804 zHdr = (u8 *)pOut->z;
86805 zPayload = zHdr + nHdr;
86806
86807 /* Write the record */
86808 zHdr += putVarint32(zHdr, nHdr)(u8)(((u32)(nHdr)<(u32)0x80)?(*(zHdr)=(unsigned char)(nHdr
)),1: sqlite3PutVarint((zHdr),(nHdr)))
;
86809 assert( pData0<=pLast )((void) (0));
86810 pRec = pData0;
86811 do{
86812 serial_type = pRec->uTemp;
86813 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
86814 ** additional varints, one per column. */
86815 zHdr += putVarint32(zHdr, serial_type)(u8)(((u32)(serial_type)<(u32)0x80)?(*(zHdr)=(unsigned char
)(serial_type)),1: sqlite3PutVarint((zHdr),(serial_type)))
; /* serial type */
86816 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
86817 ** immediately follow the header. */
86818 zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */
86819 }while( (++pRec)<=pLast );
86820 assert( nHdr==(int)(zHdr - (u8*)pOut->z) )((void) (0));
86821 assert( nByte==(int)(zPayload - (u8*)pOut->z) )((void) (0));
86822
86823 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
86824 REGISTER_TRACE(pOp->p3, pOut);
86825 break;
86826}
86827
86828/* Opcode: Count P1 P2 * * *
86829** Synopsis: r[P2]=count()
86830**
86831** Store the number of entries (an integer value) in the table or index
86832** opened by cursor P1 in register P2
86833*/
86834#ifndef SQLITE_OMIT_BTREECOUNT
86835case OP_Count93: { /* out2 */
86836 i64 nEntry;
86837 BtCursor *pCrsr;
86838
86839 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE )((void) (0));
86840 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
86841 assert( pCrsr )((void) (0));
86842 nEntry = 0; /* Not needed. Only used to silence a warning. */
86843 rc = sqlite3BtreeCount(pCrsr, &nEntry);
86844 if( rc ) goto abort_due_to_error;
86845 pOut = out2Prerelease(p, pOp);
86846 pOut->u.i = nEntry;
86847 break;
86848}
86849#endif
86850
86851/* Opcode: Savepoint P1 * * P4 *
86852**
86853** Open, release or rollback the savepoint named by parameter P4, depending
86854** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
86855** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
86856** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
86857*/
86858case OP_Savepoint0: {
86859 int p1; /* Value of P1 operand */
86860 char *zName; /* Name of savepoint */
86861 int nName;
86862 Savepoint *pNew;
86863 Savepoint *pSavepoint;
86864 Savepoint *pTmp;
86865 int iSavepoint;
86866 int ii;
86867
86868 p1 = pOp->p1;
86869 zName = pOp->p4.z;
86870
86871 /* Assert that the p1 parameter is valid. Also that if there is no open
86872 ** transaction, then there cannot be any savepoints.
86873 */
86874 assert( db->pSavepoint==0 || db->autoCommit==0 )((void) (0));
86875 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK )((void) (0));
86876 assert( db->pSavepoint || db->isTransactionSavepoint==0 )((void) (0));
86877 assert( checkSavepointCount(db) )((void) (0));
86878 assert( p->bIsReader )((void) (0));
86879
86880 if( p1==SAVEPOINT_BEGIN0 ){
86881 if( db->nVdbeWrite>0 ){
86882 /* A new savepoint cannot be created if there are active write
86883 ** statements (i.e. open read/write incremental blob handles).
86884 */
86885 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
86886 rc = SQLITE_BUSY5;
86887 }else{
86888 nName = sqlite3Strlen30(zName);
86889
86890#ifndef SQLITE_OMIT_VIRTUALTABLE
86891 /* This call is Ok even if this savepoint is actually a transaction
86892 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
86893 ** If this is a transaction savepoint being opened, it is guaranteed
86894 ** that the db->aVTrans[] array is empty. */
86895 assert( db->autoCommit==0 || db->nVTrans==0 )((void) (0));
86896 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN0,
86897 db->nStatement+db->nSavepoint);
86898 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
86899#endif
86900
86901 /* Create a new savepoint structure. */
86902 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
86903 if( pNew ){
86904 pNew->zName = (char *)&pNew[1];
86905 memcpy(pNew->zName, zName, nName+1);
86906
86907 /* If there is no open transaction, then mark this as a special
86908 ** "transaction savepoint". */
86909 if( db->autoCommit ){
86910 db->autoCommit = 0;
86911 db->isTransactionSavepoint = 1;
86912 }else{
86913 db->nSavepoint++;
86914 }
86915
86916 /* Link the new savepoint into the database handle's list. */
86917 pNew->pNext = db->pSavepoint;
86918 db->pSavepoint = pNew;
86919 pNew->nDeferredCons = db->nDeferredCons;
86920 pNew->nDeferredImmCons = db->nDeferredImmCons;
86921 }
86922 }
86923 }else{
86924 assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK )((void) (0));
86925 iSavepoint = 0;
86926
86927 /* Find the named savepoint. If there is no such savepoint, then an
86928 ** an error is returned to the user. */
86929 for(
86930 pSavepoint = db->pSavepoint;
86931 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
86932 pSavepoint = pSavepoint->pNext
86933 ){
86934 iSavepoint++;
86935 }
86936 if( !pSavepoint ){
86937 sqlite3VdbeError(p, "no such savepoint: %s", zName);
86938 rc = SQLITE_ERROR1;
86939 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE1 ){
86940 /* It is not possible to release (commit) a savepoint if there are
86941 ** active write statements.
86942 */
86943 sqlite3VdbeError(p, "cannot release savepoint - "
86944 "SQL statements in progress");
86945 rc = SQLITE_BUSY5;
86946 }else{
86947
86948 /* Determine whether or not this is a transaction savepoint. If so,
86949 ** and this is a RELEASE command, then the current transaction
86950 ** is committed.
86951 */
86952 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
86953 if( isTransaction && p1==SAVEPOINT_RELEASE1 ){
86954 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK0 ){
86955 goto vdbe_return;
86956 }
86957 db->autoCommit = 1;
86958 if( sqlite3VdbeHalt(p)==SQLITE_BUSY5 ){
86959 p->pc = (int)(pOp - aOp);
86960 db->autoCommit = 0;
86961 p->rc = rc = SQLITE_BUSY5;
86962 goto vdbe_return;
86963 }
86964 db->isTransactionSavepoint = 0;
86965 rc = p->rc;
86966 }else{
86967 int isSchemaChange;
86968 iSavepoint = db->nSavepoint - iSavepoint - 1;
86969 if( p1==SAVEPOINT_ROLLBACK2 ){
86970 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange0x0001)!=0;
86971 for(ii=0; ii<db->nDb; ii++){
86972 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
86973 SQLITE_ABORT_ROLLBACK(4 | (2<<8)),
86974 isSchemaChange==0);
86975 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
86976 }
86977 }else{
86978 assert( p1==SAVEPOINT_RELEASE )((void) (0));
86979 isSchemaChange = 0;
86980 }
86981 for(ii=0; ii<db->nDb; ii++){
86982 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
86983 if( rc!=SQLITE_OK0 ){
86984 goto abort_due_to_error;
86985 }
86986 }
86987 if( isSchemaChange ){
86988 sqlite3ExpirePreparedStatements(db, 0);
86989 sqlite3ResetAllSchemasOfConnection(db);
86990 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
86991 }
86992 }
86993
86994 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
86995 ** savepoints nested inside of the savepoint being operated on. */
86996 while( db->pSavepoint!=pSavepoint ){
86997 pTmp = db->pSavepoint;
86998 db->pSavepoint = pTmp->pNext;
86999 sqlite3DbFree(db, pTmp);
87000 db->nSavepoint--;
87001 }
87002
87003 /* If it is a RELEASE, then destroy the savepoint being operated on
87004 ** too. If it is a ROLLBACK TO, then set the number of deferred
87005 ** constraint violations present in the database to the value stored
87006 ** when the savepoint was created. */
87007 if( p1==SAVEPOINT_RELEASE1 ){
87008 assert( pSavepoint==db->pSavepoint )((void) (0));
87009 db->pSavepoint = pSavepoint->pNext;
87010 sqlite3DbFree(db, pSavepoint);
87011 if( !isTransaction ){
87012 db->nSavepoint--;
87013 }
87014 }else{
87015 assert( p1==SAVEPOINT_ROLLBACK )((void) (0));
87016 db->nDeferredCons = pSavepoint->nDeferredCons;
87017 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
87018 }
87019
87020 if( !isTransaction || p1==SAVEPOINT_ROLLBACK2 ){
87021 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
87022 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
87023 }
87024 }
87025 }
87026 if( rc ) goto abort_due_to_error;
87027
87028 break;
87029}
87030
87031/* Opcode: AutoCommit P1 P2 * * *
87032**
87033** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
87034** back any currently active btree transactions. If there are any active
87035** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
87036** there are active writing VMs or active VMs that use shared cache.
87037**
87038** This instruction causes the VM to halt.
87039*/
87040case OP_AutoCommit1: {
87041 int desiredAutoCommit;
87042 int iRollback;
87043
87044 desiredAutoCommit = pOp->p1;
87045 iRollback = pOp->p2;
87046 assert( desiredAutoCommit==1 || desiredAutoCommit==0 )((void) (0));
87047 assert( desiredAutoCommit==1 || iRollback==0 )((void) (0));
87048 assert( db->nVdbeActive>0 )((void) (0)); /* At least this one VM is active */
87049 assert( p->bIsReader )((void) (0));
87050
87051 if( desiredAutoCommit!=db->autoCommit ){
87052 if( iRollback ){
87053 assert( desiredAutoCommit==1 )((void) (0));
87054 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK(4 | (2<<8)));
87055 db->autoCommit = 1;
87056 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
87057 /* If this instruction implements a COMMIT and other VMs are writing
87058 ** return an error indicating that the other VMs must complete first.
87059 */
87060 sqlite3VdbeError(p, "cannot commit transaction - "
87061 "SQL statements in progress");
87062 rc = SQLITE_BUSY5;
87063 goto abort_due_to_error;
87064 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK0 ){
87065 goto vdbe_return;
87066 }else{
87067 db->autoCommit = (u8)desiredAutoCommit;
87068 }
87069 if( sqlite3VdbeHalt(p)==SQLITE_BUSY5 ){
87070 p->pc = (int)(pOp - aOp);
87071 db->autoCommit = (u8)(1-desiredAutoCommit);
87072 p->rc = rc = SQLITE_BUSY5;
87073 goto vdbe_return;
87074 }
87075 assert( db->nStatement==0 )((void) (0));
87076 sqlite3CloseSavepoints(db);
87077 if( p->rc==SQLITE_OK0 ){
87078 rc = SQLITE_DONE101;
87079 }else{
87080 rc = SQLITE_ERROR1;
87081 }
87082 goto vdbe_return;
87083 }else{
87084 sqlite3VdbeError(p,
87085 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
87086 (iRollback)?"cannot rollback - no transaction is active":
87087 "cannot commit - no transaction is active"));
87088
87089 rc = SQLITE_ERROR1;
87090 goto abort_due_to_error;
87091 }
87092 break;
87093}
87094
87095/* Opcode: Transaction P1 P2 P3 P4 P5
87096**
87097** Begin a transaction on database P1 if a transaction is not already
87098** active.
87099** If P2 is non-zero, then a write-transaction is started, or if a
87100** read-transaction is already active, it is upgraded to a write-transaction.
87101** If P2 is zero, then a read-transaction is started.
87102**
87103** P1 is the index of the database file on which the transaction is
87104** started. Index 0 is the main database file and index 1 is the
87105** file used for temporary tables. Indices of 2 or more are used for
87106** attached databases.
87107**
87108** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
87109** true (this flag is set if the Vdbe may modify more than one row and may
87110** throw an ABORT exception), a statement transaction may also be opened.
87111** More specifically, a statement transaction is opened iff the database
87112** connection is currently not in autocommit mode, or if there are other
87113** active statements. A statement transaction allows the changes made by this
87114** VDBE to be rolled back after an error without having to roll back the
87115** entire transaction. If no error is encountered, the statement transaction
87116** will automatically commit when the VDBE halts.
87117**
87118** If P5!=0 then this opcode also checks the schema cookie against P3
87119** and the schema generation counter against P4.
87120** The cookie changes its value whenever the database schema changes.
87121** This operation is used to detect when that the cookie has changed
87122** and that the current process needs to reread the schema. If the schema
87123** cookie in P3 differs from the schema cookie in the database header or
87124** if the schema generation counter in P4 differs from the current
87125** generation counter, then an SQLITE_SCHEMA error is raised and execution
87126** halts. The sqlite3_step() wrapper function might then reprepare the
87127** statement and rerun it from the beginning.
87128*/
87129case OP_Transaction2: {
87130 Btree *pBt;
87131 int iMeta = 0;
87132
87133 assert( p->bIsReader )((void) (0));
87134 assert( p->readOnly==0 || pOp->p2==0 )((void) (0));
87135 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
87136 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
87137 if( pOp->p2 && (db->flags & SQLITE_QueryOnly0x00100000)!=0 ){
87138 rc = SQLITE_READONLY8;
87139 goto abort_due_to_error;
87140 }
87141 pBt = db->aDb[pOp->p1].pBt;
87142
87143 if( pBt ){
87144 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
87145 testcase( rc==SQLITE_BUSY_SNAPSHOT );
87146 testcase( rc==SQLITE_BUSY_RECOVERY );
87147 if( rc!=SQLITE_OK0 ){
87148 if( (rc&0xff)==SQLITE_BUSY5 ){
87149 p->pc = (int)(pOp - aOp);
87150 p->rc = rc;
87151 goto vdbe_return;
87152 }
87153 goto abort_due_to_error;
87154 }
87155
87156 if( pOp->p2 && p->usesStmtJournal
87157 && (db->autoCommit==0 || db->nVdbeRead>1)
87158 ){
87159 assert( sqlite3BtreeIsInTrans(pBt) )((void) (0));
87160 if( p->iStatement==0 ){
87161 assert( db->nStatement>=0 && db->nSavepoint>=0 )((void) (0));
87162 db->nStatement++;
87163 p->iStatement = db->nSavepoint + db->nStatement;
87164 }
87165
87166 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN0, p->iStatement-1);
87167 if( rc==SQLITE_OK0 ){
87168 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
87169 }
87170
87171 /* Store the current value of the database handles deferred constraint
87172 ** counter. If the statement transaction needs to be rolled back,
87173 ** the value of this counter needs to be restored too. */
87174 p->nStmtDefCons = db->nDeferredCons;
87175 p->nStmtDefImmCons = db->nDeferredImmCons;
87176 }
87177 }
87178 assert( pOp->p5==0 || pOp->p4type==P4_INT32 )((void) (0));
87179 if( pOp->p5
87180 && (iMeta!=pOp->p3
87181 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
87182 ){
87183 /*
87184 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
87185 ** version is checked to ensure that the schema has not changed since the
87186 ** SQL statement was prepared.
87187 */
87188 sqlite3DbFree(db, p->zErrMsg);
87189 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
87190 /* If the schema-cookie from the database file matches the cookie
87191 ** stored with the in-memory representation of the schema, do
87192 ** not reload the schema from the database file.
87193 **
87194 ** If virtual-tables are in use, this is not just an optimization.
87195 ** Often, v-tables store their data in other SQLite tables, which
87196 ** are queried from within xNext() and other v-table methods using
87197 ** prepared queries. If such a query is out-of-date, we do not want to
87198 ** discard the database schema, as the user code implementing the
87199 ** v-table would have to be ready for the sqlite3_vtab structure itself
87200 ** to be invalidated whenever sqlite3_step() is called from within
87201 ** a v-table method.
87202 */
87203 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
87204 sqlite3ResetOneSchema(db, pOp->p1);
87205 }
87206 p->expired = 1;
87207 rc = SQLITE_SCHEMA17;
87208 }
87209 if( rc ) goto abort_due_to_error;
87210 break;
87211}
87212
87213/* Opcode: ReadCookie P1 P2 P3 * *
87214**
87215** Read cookie number P3 from database P1 and write it into register P2.
87216** P3==1 is the schema version. P3==2 is the database format.
87217** P3==3 is the recommended pager cache size, and so forth. P1==0 is
87218** the main database file and P1==1 is the database file used to store
87219** temporary tables.
87220**
87221** There must be a read-lock on the database (either a transaction
87222** must be started or there must be an open cursor) before
87223** executing this instruction.
87224*/
87225case OP_ReadCookie94: { /* out2 */
87226 int iMeta;
87227 int iDb;
87228 int iCookie;
87229
87230 assert( p->bIsReader )((void) (0));
87231 iDb = pOp->p1;
87232 iCookie = pOp->p3;
87233 assert( pOp->p3<SQLITE_N_BTREE_META )((void) (0));
87234 assert( iDb>=0 && iDb<db->nDb )((void) (0));
87235 assert( db->aDb[iDb].pBt!=0 )((void) (0));
87236 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
87237
87238 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
87239 pOut = out2Prerelease(p, pOp);
87240 pOut->u.i = iMeta;
87241 break;
87242}
87243
87244/* Opcode: SetCookie P1 P2 P3 * *
87245**
87246** Write the integer value P3 into cookie number P2 of database P1.
87247** P2==1 is the schema version. P2==2 is the database format.
87248** P2==3 is the recommended pager cache
87249** size, and so forth. P1==0 is the main database file and P1==1 is the
87250** database file used to store temporary tables.
87251**
87252** A transaction must be started before executing this opcode.
87253*/
87254case OP_SetCookie95: {
87255 Db *pDb;
87256
87257 sqlite3VdbeIncrWriteCounter(p, 0);
87258 assert( pOp->p2<SQLITE_N_BTREE_META )((void) (0));
87259 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
87260 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
87261 assert( p->readOnly==0 )((void) (0));
87262 pDb = &db->aDb[pOp->p1];
87263 assert( pDb->pBt!=0 )((void) (0));
87264 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) )((void) (0));
87265 /* See note about index shifting on OP_ReadCookie */
87266 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
87267 if( pOp->p2==BTREE_SCHEMA_VERSION1 ){
87268 /* When the schema cookie changes, record the new cookie internally */
87269 pDb->pSchema->schema_cookie = pOp->p3;
87270 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
87271 }else if( pOp->p2==BTREE_FILE_FORMAT2 ){
87272 /* Record changes in the file format */
87273 pDb->pSchema->file_format = pOp->p3;
87274 }
87275 if( pOp->p1==1 ){
87276 /* Invalidate all prepared statements whenever the TEMP database
87277 ** schema is changed. Ticket #1644 */
87278 sqlite3ExpirePreparedStatements(db, 0);
87279 p->expired = 0;
87280 }
87281 if( rc ) goto abort_due_to_error;
87282 break;
87283}
87284
87285/* Opcode: OpenRead P1 P2 P3 P4 P5
87286** Synopsis: root=P2 iDb=P3
87287**
87288** Open a read-only cursor for the database table whose root page is
87289** P2 in a database file. The database file is determined by P3.
87290** P3==0 means the main database, P3==1 means the database used for
87291** temporary tables, and P3>1 means used the corresponding attached
87292** database. Give the new cursor an identifier of P1. The P1
87293** values need not be contiguous but all P1 values should be small integers.
87294** It is an error for P1 to be negative.
87295**
87296** Allowed P5 bits:
87297** <ul>
87298** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
87299** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
87300** of OP_SeekLE/OP_IdxGT)
87301** </ul>
87302**
87303** The P4 value may be either an integer (P4_INT32) or a pointer to
87304** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
87305** object, then table being opened must be an [index b-tree] where the
87306** KeyInfo object defines the content and collating
87307** sequence of that index b-tree. Otherwise, if P4 is an integer
87308** value, then the table being opened must be a [table b-tree] with a
87309** number of columns no less than the value of P4.
87310**
87311** See also: OpenWrite, ReopenIdx
87312*/
87313/* Opcode: ReopenIdx P1 P2 P3 P4 P5
87314** Synopsis: root=P2 iDb=P3
87315**
87316** The ReopenIdx opcode works like OP_OpenRead except that it first
87317** checks to see if the cursor on P1 is already open on the same
87318** b-tree and if it is this opcode becomes a no-op. In other words,
87319** if the cursor is already open, do not reopen it.
87320**
87321** The ReopenIdx opcode may only be used with P5==0 or P5==OPFLAG_SEEKEQ
87322** and with P4 being a P4_KEYINFO object. Furthermore, the P3 value must
87323** be the same as every other ReopenIdx or OpenRead for the same cursor
87324** number.
87325**
87326** Allowed P5 bits:
87327** <ul>
87328** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
87329** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
87330** of OP_SeekLE/OP_IdxGT)
87331** </ul>
87332**
87333** See also: OP_OpenRead, OP_OpenWrite
87334*/
87335/* Opcode: OpenWrite P1 P2 P3 P4 P5
87336** Synopsis: root=P2 iDb=P3
87337**
87338** Open a read/write cursor named P1 on the table or index whose root
87339** page is P2 (or whose root page is held in register P2 if the
87340** OPFLAG_P2ISREG bit is set in P5 - see below).
87341**
87342** The P4 value may be either an integer (P4_INT32) or a pointer to
87343** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
87344** object, then table being opened must be an [index b-tree] where the
87345** KeyInfo object defines the content and collating
87346** sequence of that index b-tree. Otherwise, if P4 is an integer
87347** value, then the table being opened must be a [table b-tree] with a
87348** number of columns no less than the value of P4.
87349**
87350** Allowed P5 bits:
87351** <ul>
87352** <li> <b>0x02 OPFLAG_SEEKEQ</b>: This cursor will only be used for
87353** equality lookups (implemented as a pair of opcodes OP_SeekGE/OP_IdxGT
87354** of OP_SeekLE/OP_IdxGT)
87355** <li> <b>0x08 OPFLAG_FORDELETE</b>: This cursor is used only to seek
87356** and subsequently delete entries in an index btree. This is a
87357** hint to the storage engine that the storage engine is allowed to
87358** ignore. The hint is not used by the official SQLite b*tree storage
87359** engine, but is used by COMDB2.
87360** <li> <b>0x10 OPFLAG_P2ISREG</b>: Use the content of register P2
87361** as the root page, not the value of P2 itself.
87362** </ul>
87363**
87364** This instruction works like OpenRead except that it opens the cursor
87365** in read/write mode.
87366**
87367** See also: OP_OpenRead, OP_ReopenIdx
87368*/
87369case OP_ReopenIdx106: {
87370 int nField;
87371 KeyInfo *pKeyInfo;
87372 int p2;
87373 int iDb;
87374 int wrFlag;
87375 Btree *pX;
87376 VdbeCursor *pCur;
87377 Db *pDb;
87378
87379 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ )((void) (0));
87380 assert( pOp->p4type==P4_KEYINFO )((void) (0));
87381 pCur = p->apCsr[pOp->p1];
87382 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
87383 assert( pCur->iDb==pOp->p3 )((void) (0)); /* Guaranteed by the code generator */
87384 goto open_cursor_set_hints;
87385 }
87386 /* If the cursor is not currently open or is open on a different
87387 ** index, then fall through into OP_OpenRead to force a reopen */
87388case OP_OpenRead108:
87389case OP_OpenWrite109:
87390
87391 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ )((void) (0));
87392 assert( p->bIsReader )((void) (0));
87393 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx((void) (0))
87394 || p->readOnly==0 )((void) (0));
87395
87396 if( p->expired==1 ){
87397 rc = SQLITE_ABORT_ROLLBACK(4 | (2<<8));
87398 goto abort_due_to_error;
87399 }
87400
87401 nField = 0;
87402 pKeyInfo = 0;
87403 p2 = pOp->p2;
87404 iDb = pOp->p3;
87405 assert( iDb>=0 && iDb<db->nDb )((void) (0));
87406 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
87407 pDb = &db->aDb[iDb];
87408 pX = pDb->pBt;
87409 assert( pX!=0 )((void) (0));
87410 if( pOp->opcode==OP_OpenWrite109 ){
87411 assert( OPFLAG_FORDELETE==BTREE_FORDELETE )((void) (0));
87412 wrFlag = BTREE_WRCSR0x00000004 | (pOp->p5 & OPFLAG_FORDELETE0x08);
87413 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
87414 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
87415 p->minWriteFileFormat = pDb->pSchema->file_format;
87416 }
87417 }else{
87418 wrFlag = 0;
87419 }
87420 if( pOp->p5 & OPFLAG_P2ISREG0x10 ){
87421 assert( p2>0 )((void) (0));
87422 assert( p2<=(p->nMem+1 - p->nCursor) )((void) (0));
87423 assert( pOp->opcode==OP_OpenWrite )((void) (0));
87424 pIn2 = &aMem[p2];
87425 assert( memIsValid(pIn2) )((void) (0));
87426 assert( (pIn2->flags & MEM_Int)!=0 )((void) (0));
87427 sqlite3VdbeMemIntegerify(pIn2);
87428 p2 = (int)pIn2->u.i;
87429 /* The p2 value always comes from a prior OP_CreateBtree opcode and
87430 ** that opcode will always set the p2 value to 2 or more or else fail.
87431 ** If there were a failure, the prepared statement would have halted
87432 ** before reaching this instruction. */
87433 assert( p2>=2 )((void) (0));
87434 }
87435 if( pOp->p4type==P4_KEYINFO(-9) ){
87436 pKeyInfo = pOp->p4.pKeyInfo;
87437 assert( pKeyInfo->enc==ENC(db) )((void) (0));
87438 assert( pKeyInfo->db==db )((void) (0));
87439 nField = pKeyInfo->nAllField;
87440 }else if( pOp->p4type==P4_INT32(-3) ){
87441 nField = pOp->p4.i;
87442 }
87443 assert( pOp->p1>=0 )((void) (0));
87444 assert( nField>=0 )((void) (0));
87445 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
87446 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE0);
87447 if( pCur==0 ) goto no_mem;
87448 pCur->nullRow = 1;
87449 pCur->isOrdered = 1;
87450 pCur->pgnoRoot = p2;
87451#ifdef SQLITE_DEBUG
87452 pCur->wrFlag = wrFlag;
87453#endif
87454 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
87455 pCur->pKeyInfo = pKeyInfo;
87456 /* Set the VdbeCursor.isTable variable. Previous versions of
87457 ** SQLite used to check if the root-page flags were sane at this point
87458 ** and report database corruption if they were not, but this check has
87459 ** since moved into the btree layer. */
87460 pCur->isTable = pOp->p4type!=P4_KEYINFO(-9);
87461
87462open_cursor_set_hints:
87463 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD )((void) (0));
87464 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ )((void) (0));
87465 testcase( pOp->p5 & OPFLAG_BULKCSR );
87466#ifdef SQLITE_ENABLE_CURSOR_HINTS
87467 testcase( pOp->p2 & OPFLAG_SEEKEQ );
87468#endif
87469 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
87470 (pOp->p5 & (OPFLAG_BULKCSR0x01|OPFLAG_SEEKEQ0x02)));
87471 if( rc ) goto abort_due_to_error;
87472 break;
87473}
87474
87475/* Opcode: OpenDup P1 P2 * * *
87476**
87477** Open a new cursor P1 that points to the same ephemeral table as
87478** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral
87479** opcode. Only ephemeral cursors may be duplicated.
87480**
87481** Duplicate ephemeral cursors are used for self-joins of materialized views.
87482*/
87483case OP_OpenDup111: {
87484 VdbeCursor *pOrig; /* The original cursor to be duplicated */
87485 VdbeCursor *pCx; /* The new cursor */
87486
87487 pOrig = p->apCsr[pOp->p2];
87488 assert( pOrig->pBtx!=0 )((void) (0)); /* Only ephemeral cursors can be duplicated */
87489
87490 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE0);
87491 if( pCx==0 ) goto no_mem;
87492 pCx->nullRow = 1;
87493 pCx->isEphemeral = 1;
87494 pCx->pKeyInfo = pOrig->pKeyInfo;
87495 pCx->isTable = pOrig->isTable;
87496 pCx->pgnoRoot = pOrig->pgnoRoot;
87497 pCx->isOrdered = pOrig->isOrdered;
87498 rc = sqlite3BtreeCursor(pOrig->pBtx, pCx->pgnoRoot, BTREE_WRCSR0x00000004,
87499 pCx->pKeyInfo, pCx->uc.pCursor);
87500 /* The sqlite3BtreeCursor() routine can only fail for the first cursor
87501 ** opened for a database. Since there is already an open cursor when this
87502 ** opcode is run, the sqlite3BtreeCursor() cannot fail */
87503 assert( rc==SQLITE_OK )((void) (0));
87504 break;
87505}
87506
87507
87508/* Opcode: OpenEphemeral P1 P2 * P4 P5
87509** Synopsis: nColumn=P2
87510**
87511** Open a new cursor P1 to a transient table.
87512** The cursor is always opened read/write even if
87513** the main database is read-only. The ephemeral
87514** table is deleted automatically when the cursor is closed.
87515**
87516** If the cursor P1 is already opened on an ephemeral table, the table
87517** is cleared (all content is erased).
87518**
87519** P2 is the number of columns in the ephemeral table.
87520** The cursor points to a BTree table if P4==0 and to a BTree index
87521** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
87522** that defines the format of keys in the index.
87523**
87524** The P5 parameter can be a mask of the BTREE_* flags defined
87525** in btree.h. These flags control aspects of the operation of
87526** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
87527** added automatically.
87528*/
87529/* Opcode: OpenAutoindex P1 P2 * P4 *
87530** Synopsis: nColumn=P2
87531**
87532** This opcode works the same as OP_OpenEphemeral. It has a
87533** different name to distinguish its use. Tables created using
87534** by this opcode will be used for automatically created transient
87535** indices in joins.
87536*/
87537case OP_OpenAutoindex112:
87538case OP_OpenEphemeral113: {
87539 VdbeCursor *pCx;
87540 KeyInfo *pKeyInfo;
87541
87542 static const int vfsFlags =
87543 SQLITE_OPEN_READWRITE0x00000002 |
87544 SQLITE_OPEN_CREATE0x00000004 |
87545 SQLITE_OPEN_EXCLUSIVE0x00000010 |
87546 SQLITE_OPEN_DELETEONCLOSE0x00000008 |
87547 SQLITE_OPEN_TRANSIENT_DB0x00000400;
87548 assert( pOp->p1>=0 )((void) (0));
87549 assert( pOp->p2>=0 )((void) (0));
87550 pCx = p->apCsr[pOp->p1];
87551 if( pCx ){
87552 /* If the ephermeral table is already open, erase all existing content
87553 ** so that the table is empty again, rather than creating a new table. */
87554 assert( pCx->isEphemeral )((void) (0));
87555 pCx->seqCount = 0;
87556 pCx->cacheStatus = CACHE_STALE0;
87557 if( pCx->pBtx ){
87558 rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
87559 }
87560 }else{
87561 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE0);
87562 if( pCx==0 ) goto no_mem;
87563 pCx->isEphemeral = 1;
87564 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
87565 BTREE_OMIT_JOURNAL1 | BTREE_SINGLE4 | pOp->p5,
87566 vfsFlags);
87567 if( rc==SQLITE_OK0 ){
87568 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
87569 }
87570 if( rc==SQLITE_OK0 ){
87571 /* If a transient index is required, create it by calling
87572 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
87573 ** opening it. If a transient table is required, just use the
87574 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
87575 */
87576 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
87577 assert( pOp->p4type==P4_KEYINFO )((void) (0));
87578 rc = sqlite3BtreeCreateTable(pCx->pBtx, (int*)&pCx->pgnoRoot,
87579 BTREE_BLOBKEY2 | pOp->p5);
87580 if( rc==SQLITE_OK0 ){
87581 assert( pCx->pgnoRoot==MASTER_ROOT+1 )((void) (0));
87582 assert( pKeyInfo->db==db )((void) (0));
87583 assert( pKeyInfo->enc==ENC(db) )((void) (0));
87584 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR0x00000004,
87585 pKeyInfo, pCx->uc.pCursor);
87586 }
87587 pCx->isTable = 0;
87588 }else{
87589 pCx->pgnoRoot = MASTER_ROOT1;
87590 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT1, BTREE_WRCSR0x00000004,
87591 0, pCx->uc.pCursor);
87592 pCx->isTable = 1;
87593 }
87594 }
87595 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED8);
87596 }
87597 if( rc ) goto abort_due_to_error;
87598 pCx->nullRow = 1;
87599 break;
87600}
87601
87602/* Opcode: SorterOpen P1 P2 P3 P4 *
87603**
87604** This opcode works like OP_OpenEphemeral except that it opens
87605** a transient index that is specifically designed to sort large
87606** tables using an external merge-sort algorithm.
87607**
87608** If argument P3 is non-zero, then it indicates that the sorter may
87609** assume that a stable sort considering the first P3 fields of each
87610** key is sufficient to produce the required results.
87611*/
87612case OP_SorterOpen114: {
87613 VdbeCursor *pCx;
87614
87615 assert( pOp->p1>=0 )((void) (0));
87616 assert( pOp->p2>=0 )((void) (0));
87617 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER1);
87618 if( pCx==0 ) goto no_mem;
87619 pCx->pKeyInfo = pOp->p4.pKeyInfo;
87620 assert( pCx->pKeyInfo->db==db )((void) (0));
87621 assert( pCx->pKeyInfo->enc==ENC(db) )((void) (0));
87622 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
87623 if( rc ) goto abort_due_to_error;
87624 break;
87625}
87626
87627/* Opcode: SequenceTest P1 P2 * * *
87628** Synopsis: if( cursor[P1].ctr++ ) pc = P2
87629**
87630** P1 is a sorter cursor. If the sequence counter is currently zero, jump
87631** to P2. Regardless of whether or not the jump is taken, increment the
87632** the sequence value.
87633*/
87634case OP_SequenceTest115: {
87635 VdbeCursor *pC;
87636 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
87637 pC = p->apCsr[pOp->p1];
87638 assert( isSorter(pC) )((void) (0));
87639 if( (pC->seqCount++)==0 ){
87640 goto jump_to_p2;
87641 }
87642 break;
87643}
87644
87645/* Opcode: OpenPseudo P1 P2 P3 * *
87646** Synopsis: P3 columns in r[P2]
87647**
87648** Open a new cursor that points to a fake table that contains a single
87649** row of data. The content of that one row is the content of memory
87650** register P2. In other words, cursor P1 becomes an alias for the
87651** MEM_Blob content contained in register P2.
87652**
87653** A pseudo-table created by this opcode is used to hold a single
87654** row output from the sorter so that the row can be decomposed into
87655** individual columns using the OP_Column opcode. The OP_Column opcode
87656** is the only cursor opcode that works with a pseudo-table.
87657**
87658** P3 is the number of fields in the records that will be stored by
87659** the pseudo-table.
87660*/
87661case OP_OpenPseudo116: {
87662 VdbeCursor *pCx;
87663
87664 assert( pOp->p1>=0 )((void) (0));
87665 assert( pOp->p3>=0 )((void) (0));
87666 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO3);
87667 if( pCx==0 ) goto no_mem;
87668 pCx->nullRow = 1;
87669 pCx->seekResult = pOp->p2;
87670 pCx->isTable = 1;
87671 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
87672 ** can be safely passed to sqlite3VdbeCursorMoveto(). This avoids a test
87673 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
87674 ** which is a performance optimization */
87675 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
87676 assert( pOp->p5==0 )((void) (0));
87677 break;
87678}
87679
87680/* Opcode: Close P1 * * * *
87681**
87682** Close a cursor previously opened as P1. If P1 is not
87683** currently open, this instruction is a no-op.
87684*/
87685case OP_Close117: {
87686 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
87687 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
87688 p->apCsr[pOp->p1] = 0;
87689 break;
87690}
87691
87692#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
87693/* Opcode: ColumnsUsed P1 * * P4 *
87694**
87695** This opcode (which only exists if SQLite was compiled with
87696** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
87697** table or index for cursor P1 are used. P4 is a 64-bit integer
87698** (P4_INT64) in which the first 63 bits are one for each of the
87699** first 63 columns of the table or index that are actually used
87700** by the cursor. The high-order bit is set if any column after
87701** the 64th is used.
87702*/
87703case OP_ColumnsUsed118: {
87704 VdbeCursor *pC;
87705 pC = p->apCsr[pOp->p1];
87706 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
87707 pC->maskUsed = *(u64*)pOp->p4.pI64;
87708 break;
87709}
87710#endif
87711
87712/* Opcode: SeekGE P1 P2 P3 P4 *
87713** Synopsis: key=r[P3@P4]
87714**
87715** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
87716** use the value in register P3 as the key. If cursor P1 refers
87717** to an SQL index, then P3 is the first in an array of P4 registers
87718** that are used as an unpacked index key.
87719**
87720** Reposition cursor P1 so that it points to the smallest entry that
87721** is greater than or equal to the key value. If there are no records
87722** greater than or equal to the key and P2 is not zero, then jump to P2.
87723**
87724** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
87725** opcode will always land on a record that equally equals the key, or
87726** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
87727** opcode must be followed by an IdxLE opcode with the same arguments.
87728** The IdxLE opcode will be skipped if this opcode succeeds, but the
87729** IdxLE opcode will be used on subsequent loop iterations.
87730**
87731** This opcode leaves the cursor configured to move in forward order,
87732** from the beginning toward the end. In other words, the cursor is
87733** configured to use Next, not Prev.
87734**
87735** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
87736*/
87737/* Opcode: SeekGT P1 P2 P3 P4 *
87738** Synopsis: key=r[P3@P4]
87739**
87740** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
87741** use the value in register P3 as a key. If cursor P1 refers
87742** to an SQL index, then P3 is the first in an array of P4 registers
87743** that are used as an unpacked index key.
87744**
87745** Reposition cursor P1 so that it points to the smallest entry that
87746** is greater than the key value. If there are no records greater than
87747** the key and P2 is not zero, then jump to P2.
87748**
87749** This opcode leaves the cursor configured to move in forward order,
87750** from the beginning toward the end. In other words, the cursor is
87751** configured to use Next, not Prev.
87752**
87753** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
87754*/
87755/* Opcode: SeekLT P1 P2 P3 P4 *
87756** Synopsis: key=r[P3@P4]
87757**
87758** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
87759** use the value in register P3 as a key. If cursor P1 refers
87760** to an SQL index, then P3 is the first in an array of P4 registers
87761** that are used as an unpacked index key.
87762**
87763** Reposition cursor P1 so that it points to the largest entry that
87764** is less than the key value. If there are no records less than
87765** the key and P2 is not zero, then jump to P2.
87766**
87767** This opcode leaves the cursor configured to move in reverse order,
87768** from the end toward the beginning. In other words, the cursor is
87769** configured to use Prev, not Next.
87770**
87771** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
87772*/
87773/* Opcode: SeekLE P1 P2 P3 P4 *
87774** Synopsis: key=r[P3@P4]
87775**
87776** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
87777** use the value in register P3 as a key. If cursor P1 refers
87778** to an SQL index, then P3 is the first in an array of P4 registers
87779** that are used as an unpacked index key.
87780**
87781** Reposition cursor P1 so that it points to the largest entry that
87782** is less than or equal to the key value. If there are no records
87783** less than or equal to the key and P2 is not zero, then jump to P2.
87784**
87785** This opcode leaves the cursor configured to move in reverse order,
87786** from the end toward the beginning. In other words, the cursor is
87787** configured to use Prev, not Next.
87788**
87789** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
87790** opcode will always land on a record that equally equals the key, or
87791** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
87792** opcode must be followed by an IdxGE opcode with the same arguments.
87793** The IdxGE opcode will be skipped if this opcode succeeds, but the
87794** IdxGE opcode will be used on subsequent loop iterations.
87795**
87796** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
87797*/
87798case OP_SeekLT22: /* jump, in3, group */
87799case OP_SeekLE23: /* jump, in3, group */
87800case OP_SeekGE24: /* jump, in3, group */
87801case OP_SeekGT25: { /* jump, in3, group */
87802 int res; /* Comparison result */
87803 int oc; /* Opcode */
87804 VdbeCursor *pC; /* The cursor to seek */
87805 UnpackedRecord r; /* The key to seek for */
87806 int nField; /* Number of columns or fields in the key */
87807 i64 iKey; /* The rowid we are to seek to */
87808 int eqOnly; /* Only interested in == results */
87809
87810 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
87811 assert( pOp->p2!=0 )((void) (0));
87812 pC = p->apCsr[pOp->p1];
87813 assert( pC!=0 )((void) (0));
87814 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
87815 assert( OP_SeekLE == OP_SeekLT+1 )((void) (0));
87816 assert( OP_SeekGE == OP_SeekLT+2 )((void) (0));
87817 assert( OP_SeekGT == OP_SeekLT+3 )((void) (0));
87818 assert( pC->isOrdered )((void) (0));
87819 assert( pC->uc.pCursor!=0 )((void) (0));
87820 oc = pOp->opcode;
87821 eqOnly = 0;
87822 pC->nullRow = 0;
87823#ifdef SQLITE_DEBUG
87824 pC->seekOp = pOp->opcode;
87825#endif
87826
87827 pC->deferredMoveto = 0;
87828 pC->cacheStatus = CACHE_STALE0;
87829 if( pC->isTable ){
87830 /* The BTREE_SEEK_EQ flag is only set on index cursors */
87831 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0((void) (0))
87832 || CORRUPT_DB )((void) (0));
87833
87834 /* The input value in P3 might be of any type: integer, real, string,
87835 ** blob, or NULL. But it needs to be an integer before we can do
87836 ** the seek, so convert it. */
87837 pIn3 = &aMem[pOp->p3];
87838 if( (pIn3->flags & (MEM_Int0x0004|MEM_Real0x0008|MEM_IntReal0x0020|MEM_Str0x0002))==MEM_Str0x0002 ){
87839 applyNumericAffinity(pIn3, 0);
87840 }
87841 iKey = sqlite3VdbeIntValue(pIn3);
87842
87843 /* If the P3 value could not be converted into an integer without
87844 ** loss of information, then special processing is required... */
87845 if( (pIn3->flags & (MEM_Int0x0004|MEM_IntReal0x0020))==0 ){
87846 if( (pIn3->flags & MEM_Real0x0008)==0 ){
87847 if( (pIn3->flags & MEM_Null0x0001) || oc>=OP_SeekGE24 ){
87848 VdbeBranchTaken(1,2); goto jump_to_p2;
87849 break;
87850 }else{
87851 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
87852 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
87853 goto seek_not_found;
87854 }
87855 }else
87856
87857 /* If the approximation iKey is larger than the actual real search
87858 ** term, substitute >= for > and < for <=. e.g. if the search term
87859 ** is 4.9 and the integer approximation 5:
87860 **
87861 ** (x > 4.9) -> (x >= 5)
87862 ** (x <= 4.9) -> (x < 5)
87863 */
87864 if( pIn3->u.r<(double)iKey ){
87865 assert( OP_SeekGE==(OP_SeekGT-1) )((void) (0));
87866 assert( OP_SeekLT==(OP_SeekLE-1) )((void) (0));
87867 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) )((void) (0));
87868 if( (oc & 0x0001)==(OP_SeekGT25 & 0x0001) ) oc--;
87869 }
87870
87871 /* If the approximation iKey is smaller than the actual real search
87872 ** term, substitute <= for < and > for >=. */
87873 else if( pIn3->u.r>(double)iKey ){
87874 assert( OP_SeekLE==(OP_SeekLT+1) )((void) (0));
87875 assert( OP_SeekGT==(OP_SeekGE+1) )((void) (0));
87876 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) )((void) (0));
87877 if( (oc & 0x0001)==(OP_SeekLT22 & 0x0001) ) oc++;
87878 }
87879 }
87880 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
87881 pC->movetoTarget = iKey; /* Used by OP_Delete */
87882 if( rc!=SQLITE_OK0 ){
87883 goto abort_due_to_error;
87884 }
87885 }else{
87886 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
87887 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
87888 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
87889 */
87890 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ0x00000002) ){
87891 eqOnly = 1;
87892 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE )((void) (0));
87893 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT )((void) (0));
87894 assert( pOp[1].p1==pOp[0].p1 )((void) (0));
87895 assert( pOp[1].p2==pOp[0].p2 )((void) (0));
87896 assert( pOp[1].p3==pOp[0].p3 )((void) (0));
87897 assert( pOp[1].p4.i==pOp[0].p4.i )((void) (0));
87898 }
87899
87900 nField = pOp->p4.i;
87901 assert( pOp->p4type==P4_INT32 )((void) (0));
87902 assert( nField>0 )((void) (0));
87903 r.pKeyInfo = pC->pKeyInfo;
87904 r.nField = (u16)nField;
87905
87906 /* The next line of code computes as follows, only faster:
87907 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
87908 ** r.default_rc = -1;
87909 ** }else{
87910 ** r.default_rc = +1;
87911 ** }
87912 */
87913 r.default_rc = ((1 & (oc - OP_SeekLT22)) ? -1 : +1);
87914 assert( oc!=OP_SeekGT || r.default_rc==-1 )((void) (0));
87915 assert( oc!=OP_SeekLE || r.default_rc==-1 )((void) (0));
87916 assert( oc!=OP_SeekGE || r.default_rc==+1 )((void) (0));
87917 assert( oc!=OP_SeekLT || r.default_rc==+1 )((void) (0));
87918
87919 r.aMem = &aMem[pOp->p3];
87920#ifdef SQLITE_DEBUG
87921 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) )((void) (0)); }
87922#endif
87923 r.eqSeen = 0;
87924 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
87925 if( rc!=SQLITE_OK0 ){
87926 goto abort_due_to_error;
87927 }
87928 if( eqOnly && r.eqSeen==0 ){
87929 assert( res!=0 )((void) (0));
87930 goto seek_not_found;
87931 }
87932 }
87933#ifdef SQLITE_TEST
87934 sqlite3_search_count++;
87935#endif
87936 if( oc>=OP_SeekGE24 ){ assert( oc==OP_SeekGE || oc==OP_SeekGT )((void) (0));
87937 if( res<0 || (res==0 && oc==OP_SeekGT25) ){
87938 res = 0;
87939 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
87940 if( rc!=SQLITE_OK0 ){
87941 if( rc==SQLITE_DONE101 ){
87942 rc = SQLITE_OK0;
87943 res = 1;
87944 }else{
87945 goto abort_due_to_error;
87946 }
87947 }
87948 }else{
87949 res = 0;
87950 }
87951 }else{
87952 assert( oc==OP_SeekLT || oc==OP_SeekLE )((void) (0));
87953 if( res>0 || (res==0 && oc==OP_SeekLT22) ){
87954 res = 0;
87955 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
87956 if( rc!=SQLITE_OK0 ){
87957 if( rc==SQLITE_DONE101 ){
87958 rc = SQLITE_OK0;
87959 res = 1;
87960 }else{
87961 goto abort_due_to_error;
87962 }
87963 }
87964 }else{
87965 /* res might be negative because the table is empty. Check to
87966 ** see if this is the case.
87967 */
87968 res = sqlite3BtreeEof(pC->uc.pCursor);
87969 }
87970 }
87971seek_not_found:
87972 assert( pOp->p2>0 )((void) (0));
87973 VdbeBranchTaken(res!=0,2);
87974 if( res ){
87975 goto jump_to_p2;
87976 }else if( eqOnly ){
87977 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT )((void) (0));
87978 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
87979 }
87980 break;
87981}
87982
87983/* Opcode: SeekHit P1 P2 * * *
87984** Synopsis: seekHit=P2
87985**
87986** Set the seekHit flag on cursor P1 to the value in P2.
87987** The seekHit flag is used by the IfNoHope opcode.
87988**
87989** P1 must be a valid b-tree cursor. P2 must be a boolean value,
87990** either 0 or 1.
87991*/
87992case OP_SeekHit119: {
87993 VdbeCursor *pC;
87994 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
87995 pC = p->apCsr[pOp->p1];
87996 assert( pC!=0 )((void) (0));
87997 assert( pOp->p2==0 || pOp->p2==1 )((void) (0));
87998 pC->seekHit = pOp->p2 & 1;
87999 break;
88000}
88001
88002/* Opcode: Found P1 P2 P3 P4 *
88003** Synopsis: key=r[P3@P4]
88004**
88005** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
88006** P4>0 then register P3 is the first of P4 registers that form an unpacked
88007** record.
88008**
88009** Cursor P1 is on an index btree. If the record identified by P3 and P4
88010** is a prefix of any entry in P1 then a jump is made to P2 and
88011** P1 is left pointing at the matching entry.
88012**
88013** This operation leaves the cursor in a state where it can be
88014** advanced in the forward direction. The Next instruction will work,
88015** but not the Prev instruction.
88016**
88017** See also: NotFound, NoConflict, NotExists. SeekGe
88018*/
88019/* Opcode: NotFound P1 P2 P3 P4 *
88020** Synopsis: key=r[P3@P4]
88021**
88022** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
88023** P4>0 then register P3 is the first of P4 registers that form an unpacked
88024** record.
88025**
88026** Cursor P1 is on an index btree. If the record identified by P3 and P4
88027** is not the prefix of any entry in P1 then a jump is made to P2. If P1
88028** does contain an entry whose prefix matches the P3/P4 record then control
88029** falls through to the next instruction and P1 is left pointing at the
88030** matching entry.
88031**
88032** This operation leaves the cursor in a state where it cannot be
88033** advanced in either direction. In other words, the Next and Prev
88034** opcodes do not work after this operation.
88035**
88036** See also: Found, NotExists, NoConflict, IfNoHope
88037*/
88038/* Opcode: IfNoHope P1 P2 P3 P4 *
88039** Synopsis: key=r[P3@P4]
88040**
88041** Register P3 is the first of P4 registers that form an unpacked
88042** record.
88043**
88044** Cursor P1 is on an index btree. If the seekHit flag is set on P1, then
88045** this opcode is a no-op. But if the seekHit flag of P1 is clear, then
88046** check to see if there is any entry in P1 that matches the
88047** prefix identified by P3 and P4. If no entry matches the prefix,
88048** jump to P2. Otherwise fall through.
88049**
88050** This opcode behaves like OP_NotFound if the seekHit
88051** flag is clear and it behaves like OP_Noop if the seekHit flag is set.
88052**
88053** This opcode is used in IN clause processing for a multi-column key.
88054** If an IN clause is attached to an element of the key other than the
88055** left-most element, and if there are no matches on the most recent
88056** seek over the whole key, then it might be that one of the key element
88057** to the left is prohibiting a match, and hence there is "no hope" of
88058** any match regardless of how many IN clause elements are checked.
88059** In such a case, we abandon the IN clause search early, using this
88060** opcode. The opcode name comes from the fact that the
88061** jump is taken if there is "no hope" of achieving a match.
88062**
88063** See also: NotFound, SeekHit
88064*/
88065/* Opcode: NoConflict P1 P2 P3 P4 *
88066** Synopsis: key=r[P3@P4]
88067**
88068** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
88069** P4>0 then register P3 is the first of P4 registers that form an unpacked
88070** record.
88071**
88072** Cursor P1 is on an index btree. If the record identified by P3 and P4
88073** contains any NULL value, jump immediately to P2. If all terms of the
88074** record are not-NULL then a check is done to determine if any row in the
88075** P1 index btree has a matching key prefix. If there are no matches, jump
88076** immediately to P2. If there is a match, fall through and leave the P1
88077** cursor pointing to the matching row.
88078**
88079** This opcode is similar to OP_NotFound with the exceptions that the
88080** branch is always taken if any part of the search key input is NULL.
88081**
88082** This operation leaves the cursor in a state where it cannot be
88083** advanced in either direction. In other words, the Next and Prev
88084** opcodes do not work after this operation.
88085**
88086** See also: NotFound, Found, NotExists
88087*/
88088case OP_IfNoHope26: { /* jump, in3 */
88089 VdbeCursor *pC;
88090 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88091 pC = p->apCsr[pOp->p1];
88092 assert( pC!=0 )((void) (0));
88093 if( pC->seekHit ) break;
88094 /* Fall through into OP_NotFound */
88095}
88096case OP_NoConflict27: /* jump, in3 */
88097case OP_NotFound28: /* jump, in3 */
88098case OP_Found29: { /* jump, in3 */
88099 int alreadyExists;
88100 int takeJump;
88101 int ii;
88102 VdbeCursor *pC;
88103 int res;
88104 UnpackedRecord *pFree;
88105 UnpackedRecord *pIdxKey;
88106 UnpackedRecord r;
88107
88108#ifdef SQLITE_TEST
88109 if( pOp->opcode!=OP_NoConflict27 ) sqlite3_found_count++;
88110#endif
88111
88112 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88113 assert( pOp->p4type==P4_INT32 )((void) (0));
88114 pC = p->apCsr[pOp->p1];
88115 assert( pC!=0 )((void) (0));
88116#ifdef SQLITE_DEBUG
88117 pC->seekOp = pOp->opcode;
88118#endif
88119 pIn3 = &aMem[pOp->p3];
88120 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
88121 assert( pC->uc.pCursor!=0 )((void) (0));
88122 assert( pC->isTable==0 )((void) (0));
88123 if( pOp->p4.i>0 ){
88124 r.pKeyInfo = pC->pKeyInfo;
88125 r.nField = (u16)pOp->p4.i;
88126 r.aMem = pIn3;
88127#ifdef SQLITE_DEBUG
88128 for(ii=0; ii<r.nField; ii++){
88129 assert( memIsValid(&r.aMem[ii]) )((void) (0));
88130 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 )((void) (0));
88131 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
88132 }
88133#endif
88134 pIdxKey = &r;
88135 pFree = 0;
88136 }else{
88137 assert( pIn3->flags & MEM_Blob )((void) (0));
88138 rc = ExpandBlob(pIn3)(((pIn3)->flags&0x4000)?sqlite3VdbeMemExpandBlob(pIn3)
:0)
;
88139 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
88140 if( rc ) goto no_mem;
88141 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
88142 if( pIdxKey==0 ) goto no_mem;
88143 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
88144 }
88145 pIdxKey->default_rc = 0;
88146 takeJump = 0;
88147 if( pOp->opcode==OP_NoConflict27 ){
88148 /* For the OP_NoConflict opcode, take the jump if any of the
88149 ** input fields are NULL, since any key with a NULL will not
88150 ** conflict */
88151 for(ii=0; ii<pIdxKey->nField; ii++){
88152 if( pIdxKey->aMem[ii].flags & MEM_Null0x0001 ){
88153 takeJump = 1;
88154 break;
88155 }
88156 }
88157 }
88158 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
88159 if( pFree ) sqlite3DbFreeNN(db, pFree);
88160 if( rc!=SQLITE_OK0 ){
88161 goto abort_due_to_error;
88162 }
88163 pC->seekResult = res;
88164 alreadyExists = (res==0);
88165 pC->nullRow = 1-alreadyExists;
88166 pC->deferredMoveto = 0;
88167 pC->cacheStatus = CACHE_STALE0;
88168 if( pOp->opcode==OP_Found29 ){
88169 VdbeBranchTaken(alreadyExists!=0,2);
88170 if( alreadyExists ) goto jump_to_p2;
88171 }else{
88172 VdbeBranchTaken(takeJump||alreadyExists==0,2);
88173 if( takeJump || !alreadyExists ) goto jump_to_p2;
88174 }
88175 break;
88176}
88177
88178/* Opcode: SeekRowid P1 P2 P3 * *
88179** Synopsis: intkey=r[P3]
88180**
88181** P1 is the index of a cursor open on an SQL table btree (with integer
88182** keys). If register P3 does not contain an integer or if P1 does not
88183** contain a record with rowid P3 then jump immediately to P2.
88184** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
88185** a record with rowid P3 then
88186** leave the cursor pointing at that record and fall through to the next
88187** instruction.
88188**
88189** The OP_NotExists opcode performs the same operation, but with OP_NotExists
88190** the P3 register must be guaranteed to contain an integer value. With this
88191** opcode, register P3 might not contain an integer.
88192**
88193** The OP_NotFound opcode performs the same operation on index btrees
88194** (with arbitrary multi-value keys).
88195**
88196** This opcode leaves the cursor in a state where it cannot be advanced
88197** in either direction. In other words, the Next and Prev opcodes will
88198** not work following this opcode.
88199**
88200** See also: Found, NotFound, NoConflict, SeekRowid
88201*/
88202/* Opcode: NotExists P1 P2 P3 * *
88203** Synopsis: intkey=r[P3]
88204**
88205** P1 is the index of a cursor open on an SQL table btree (with integer
88206** keys). P3 is an integer rowid. If P1 does not contain a record with
88207** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
88208** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
88209** leave the cursor pointing at that record and fall through to the next
88210** instruction.
88211**
88212** The OP_SeekRowid opcode performs the same operation but also allows the
88213** P3 register to contain a non-integer value, in which case the jump is
88214** always taken. This opcode requires that P3 always contain an integer.
88215**
88216** The OP_NotFound opcode performs the same operation on index btrees
88217** (with arbitrary multi-value keys).
88218**
88219** This opcode leaves the cursor in a state where it cannot be advanced
88220** in either direction. In other words, the Next and Prev opcodes will
88221** not work following this opcode.
88222**
88223** See also: Found, NotFound, NoConflict, SeekRowid
88224*/
88225case OP_SeekRowid30: { /* jump, in3 */
88226 VdbeCursor *pC;
88227 BtCursor *pCrsr;
88228 int res;
88229 u64 iKey;
88230
88231 pIn3 = &aMem[pOp->p3];
88232 testcase( pIn3->flags & MEM_Int );
88233 testcase( pIn3->flags & MEM_IntReal );
88234 if( (pIn3->flags & (MEM_Int0x0004|MEM_IntReal0x0020))==0 ){
88235 /* Make sure pIn3->u.i contains a valid integer representation of
88236 ** the key value, but do not change the datatype of the register, as
88237 ** other parts of the perpared statement might be depending on the
88238 ** current datatype. */
88239 u16 origFlags = pIn3->flags;
88240 int isNotInt;
88241 applyAffinity(pIn3, SQLITE_AFF_NUMERIC'C', encoding);
88242 isNotInt = (pIn3->flags & MEM_Int0x0004)==0;
88243 pIn3->flags = origFlags;
88244 if( isNotInt ) goto jump_to_p2;
88245 }
88246 /* Fall through into OP_NotExists */
88247case OP_NotExists31: /* jump, in3 */
88248 pIn3 = &aMem[pOp->p3];
88249 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid )((void) (0));
88250 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88251 pC = p->apCsr[pOp->p1];
88252 assert( pC!=0 )((void) (0));
88253#ifdef SQLITE_DEBUG
88254 if( pOp->opcode==OP_SeekRowid30 ) pC->seekOp = OP_SeekRowid30;
88255#endif
88256 assert( pC->isTable )((void) (0));
88257 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
88258 pCrsr = pC->uc.pCursor;
88259 assert( pCrsr!=0 )((void) (0));
88260 res = 0;
88261 iKey = pIn3->u.i;
88262 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
88263 assert( rc==SQLITE_OK || res==0 )((void) (0));
88264 pC->movetoTarget = iKey; /* Used by OP_Delete */
88265 pC->nullRow = 0;
88266 pC->cacheStatus = CACHE_STALE0;
88267 pC->deferredMoveto = 0;
88268 VdbeBranchTaken(res!=0,2);
88269 pC->seekResult = res;
88270 if( res!=0 ){
88271 assert( rc==SQLITE_OK )((void) (0));
88272 if( pOp->p2==0 ){
88273 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(88273);
88274 }else{
88275 goto jump_to_p2;
88276 }
88277 }
88278 if( rc ) goto abort_due_to_error;
88279 break;
88280}
88281
88282/* Opcode: Sequence P1 P2 * * *
88283** Synopsis: r[P2]=cursor[P1].ctr++
88284**
88285** Find the next available sequence number for cursor P1.
88286** Write the sequence number into register P2.
88287** The sequence number on the cursor is incremented after this
88288** instruction.
88289*/
88290case OP_Sequence120: { /* out2 */
88291 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88292 assert( p->apCsr[pOp->p1]!=0 )((void) (0));
88293 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB )((void) (0));
88294 pOut = out2Prerelease(p, pOp);
88295 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
88296 break;
88297}
88298
88299
88300/* Opcode: NewRowid P1 P2 P3 * *
88301** Synopsis: r[P2]=rowid
88302**
88303** Get a new integer record number (a.k.a "rowid") used as the key to a table.
88304** The record number is not previously used as a key in the database
88305** table that cursor P1 points to. The new record number is written
88306** written to register P2.
88307**
88308** If P3>0 then P3 is a register in the root frame of this VDBE that holds
88309** the largest previously generated record number. No new record numbers are
88310** allowed to be less than this value. When this value reaches its maximum,
88311** an SQLITE_FULL error is generated. The P3 register is updated with the '
88312** generated record number. This P3 mechanism is used to help implement the
88313** AUTOINCREMENT feature.
88314*/
88315case OP_NewRowid121: { /* out2 */
88316 i64 v; /* The new rowid */
88317 VdbeCursor *pC; /* Cursor of table to get the new rowid */
88318 int res; /* Result of an sqlite3BtreeLast() */
88319 int cnt; /* Counter to limit the number of searches */
88320 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
88321 VdbeFrame *pFrame; /* Root frame of VDBE */
88322
88323 v = 0;
88324 res = 0;
88325 pOut = out2Prerelease(p, pOp);
88326 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88327 pC = p->apCsr[pOp->p1];
88328 assert( pC!=0 )((void) (0));
88329 assert( pC->isTable )((void) (0));
88330 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
88331 assert( pC->uc.pCursor!=0 )((void) (0));
88332 {
88333 /* The next rowid or record number (different terms for the same
88334 ** thing) is obtained in a two-step algorithm.
88335 **
88336 ** First we attempt to find the largest existing rowid and add one
88337 ** to that. But if the largest existing rowid is already the maximum
88338 ** positive integer, we have to fall through to the second
88339 ** probabilistic algorithm
88340 **
88341 ** The second algorithm is to select a rowid at random and see if
88342 ** it already exists in the table. If it does not exist, we have
88343 ** succeeded. If the random rowid does exist, we select a new one
88344 ** and try again, up to 100 times.
88345 */
88346 assert( pC->isTable )((void) (0));
88347
88348#ifdef SQLITE_32BIT_ROWID
88349# define MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) 0x7fffffff
88350#else
88351 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
88352 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
88353 ** to provide the constant while making all compilers happy.
88354 */
88355# define MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
88356#endif
88357
88358 if( !pC->useRandomRowid ){
88359 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
88360 if( rc!=SQLITE_OK0 ){
88361 goto abort_due_to_error;
88362 }
88363 if( res ){
88364 v = 1; /* IMP: R-61914-48074 */
88365 }else{
88366 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) )((void) (0));
88367 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
88368 if( v>=MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) ){
88369 pC->useRandomRowid = 1;
88370 }else{
88371 v++; /* IMP: R-29538-34987 */
88372 }
88373 }
88374 }
88375
88376#ifndef SQLITE_OMIT_AUTOINCREMENT
88377 if( pOp->p3 ){
88378 /* Assert that P3 is a valid memory cell. */
88379 assert( pOp->p3>0 )((void) (0));
88380 if( p->pFrame ){
88381 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
88382 /* Assert that P3 is a valid memory cell. */
88383 assert( pOp->p3<=pFrame->nMem )((void) (0));
88384 pMem = &pFrame->aMem[pOp->p3];
88385 }else{
88386 /* Assert that P3 is a valid memory cell. */
88387 assert( pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
88388 pMem = &aMem[pOp->p3];
88389 memAboutToChange(p, pMem);
88390 }
88391 assert( memIsValid(pMem) )((void) (0));
88392
88393 REGISTER_TRACE(pOp->p3, pMem);
88394 sqlite3VdbeMemIntegerify(pMem);
88395 assert( (pMem->flags & MEM_Int)!=0 )((void) (0)); /* mem(P3) holds an integer */
88396 if( pMem->u.i==MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) || pC->useRandomRowid ){
88397 rc = SQLITE_FULL13; /* IMP: R-17817-00630 */
88398 goto abort_due_to_error;
88399 }
88400 if( v<pMem->u.i+1 ){
88401 v = pMem->u.i + 1;
88402 }
88403 pMem->u.i = v;
88404 }
88405#endif
88406 if( pC->useRandomRowid ){
88407 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
88408 ** largest possible integer (9223372036854775807) then the database
88409 ** engine starts picking positive candidate ROWIDs at random until
88410 ** it finds one that is not previously used. */
88411 assert( pOp->p3==0 )((void) (0)); /* We cannot be in random rowid mode if this is
88412 ** an AUTOINCREMENT table. */
88413 cnt = 0;
88414 do{
88415 sqlite3_randomness(sizeof(v), &v);
88416 v &= (MAX_ROWID(i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )>>1); v++; /* Ensure that v is greater than zero */
88417 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
88418 0, &res))==SQLITE_OK0)
88419 && (res==0)
88420 && (++cnt<100));
88421 if( rc ) goto abort_due_to_error;
88422 if( res==0 ){
88423 rc = SQLITE_FULL13; /* IMP: R-38219-53002 */
88424 goto abort_due_to_error;
88425 }
88426 assert( v>0 )((void) (0)); /* EV: R-40812-03570 */
88427 }
88428 pC->deferredMoveto = 0;
88429 pC->cacheStatus = CACHE_STALE0;
88430 }
88431 pOut->u.i = v;
88432 break;
88433}
88434
88435/* Opcode: Insert P1 P2 P3 P4 P5
88436** Synopsis: intkey=r[P3] data=r[P2]
88437**
88438** Write an entry into the table of cursor P1. A new entry is
88439** created if it doesn't already exist or the data for an existing
88440** entry is overwritten. The data is the value MEM_Blob stored in register
88441** number P2. The key is stored in register P3. The key must
88442** be a MEM_Int.
88443**
88444** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
88445** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
88446** then rowid is stored for subsequent return by the
88447** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
88448**
88449** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
88450** run faster by avoiding an unnecessary seek on cursor P1. However,
88451** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
88452** seeks on the cursor or if the most recent seek used a key equal to P3.
88453**
88454** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
88455** UPDATE operation. Otherwise (if the flag is clear) then this opcode
88456** is part of an INSERT operation. The difference is only important to
88457** the update hook.
88458**
88459** Parameter P4 may point to a Table structure, or may be NULL. If it is
88460** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
88461** following a successful insert.
88462**
88463** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
88464** allocated, then ownership of P2 is transferred to the pseudo-cursor
88465** and register P2 becomes ephemeral. If the cursor is changed, the
88466** value of register P2 will then change. Make sure this does not
88467** cause any problems.)
88468**
88469** This instruction only works on tables. The equivalent instruction
88470** for indices is OP_IdxInsert.
88471*/
88472case OP_Insert122: {
88473 Mem *pData; /* MEM cell holding data for the record to be inserted */
88474 Mem *pKey; /* MEM cell holding key for the record */
88475 VdbeCursor *pC; /* Cursor to table into which insert is written */
88476 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
88477 const char *zDb; /* database name - used by the update hook */
88478 Table *pTab; /* Table structure - used by update and pre-update hooks */
88479 BtreePayload x; /* Payload to be inserted */
88480
88481 pData = &aMem[pOp->p2];
88482 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88483 assert( memIsValid(pData) )((void) (0));
88484 pC = p->apCsr[pOp->p1];
88485 assert( pC!=0 )((void) (0));
88486 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
88487 assert( pC->uc.pCursor!=0 )((void) (0));
88488 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable )((void) (0));
88489 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC )((void) (0));
88490 REGISTER_TRACE(pOp->p2, pData);
88491 sqlite3VdbeIncrWriteCounter(p, pC);
88492
88493 pKey = &aMem[pOp->p3];
88494 assert( pKey->flags & MEM_Int )((void) (0));
88495 assert( memIsValid(pKey) )((void) (0));
88496 REGISTER_TRACE(pOp->p3, pKey);
88497 x.nKey = pKey->u.i;
88498
88499 if( pOp->p4type==P4_TABLE(-6) && HAS_UPDATE_HOOK(db)((db)->xUpdateCallback) ){
88500 assert( pC->iDb>=0 )((void) (0));
88501 zDb = db->aDb[pC->iDb].zDbSName;
88502 pTab = pOp->p4.pTab;
88503 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) )((void) (0));
88504 }else{
88505 pTab = 0;
88506 zDb = 0; /* Not needed. Silence a compiler warning. */
88507 }
88508
88509#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
88510 /* Invoke the pre-update hook, if any */
88511 if( pTab ){
88512 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE0x04) ){
88513 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT18, zDb, pTab, x.nKey,pOp->p2);
88514 }
88515 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
88516 /* Prevent post-update hook from running in cases when it should not */
88517 pTab = 0;
88518 }
88519 }
88520 if( pOp->p5 & OPFLAG_ISNOOP0x40 ) break;
88521#endif
88522
88523 if( pOp->p5 & OPFLAG_NCHANGE0x01 ) p->nChange++;
88524 if( pOp->p5 & OPFLAG_LASTROWID0x20 ) db->lastRowid = x.nKey;
88525 assert( pData->flags & (MEM_Blob|MEM_Str) )((void) (0));
88526 x.pData = pData->z;
88527 x.nData = pData->n;
88528 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT0x10) ? pC->seekResult : 0);
88529 if( pData->flags & MEM_Zero0x4000 ){
88530 x.nZero = pData->u.nZero;
88531 }else{
88532 x.nZero = 0;
88533 }
88534 x.pKey = 0;
88535 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
88536 (pOp->p5 & (OPFLAG_APPEND0x08|OPFLAG_SAVEPOSITION0x02)), seekResult
88537 );
88538 pC->deferredMoveto = 0;
88539 pC->cacheStatus = CACHE_STALE0;
88540
88541 /* Invoke the update-hook if required. */
88542 if( rc ) goto abort_due_to_error;
88543 if( pTab ){
88544 assert( db->xUpdateCallback!=0 )((void) (0));
88545 assert( pTab->aCol!=0 )((void) (0));
88546 db->xUpdateCallback(db->pUpdateArg,
88547 (pOp->p5 & OPFLAG_ISUPDATE0x04) ? SQLITE_UPDATE23 : SQLITE_INSERT18,
88548 zDb, pTab->zName, x.nKey);
88549 }
88550 break;
88551}
88552
88553/* Opcode: Delete P1 P2 P3 P4 P5
88554**
88555** Delete the record at which the P1 cursor is currently pointing.
88556**
88557** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
88558** the cursor will be left pointing at either the next or the previous
88559** record in the table. If it is left pointing at the next record, then
88560** the next Next instruction will be a no-op. As a result, in this case
88561** it is ok to delete a record from within a Next loop. If
88562** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
88563** left in an undefined state.
88564**
88565** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
88566** delete one of several associated with deleting a table row and all its
88567** associated index entries. Exactly one of those deletes is the "primary"
88568** delete. The others are all on OPFLAG_FORDELETE cursors or else are
88569** marked with the AUXDELETE flag.
88570**
88571** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
88572** change count is incremented (otherwise not).
88573**
88574** P1 must not be pseudo-table. It has to be a real table with
88575** multiple rows.
88576**
88577** If P4 is not NULL then it points to a Table object. In this case either
88578** the update or pre-update hook, or both, may be invoked. The P1 cursor must
88579** have been positioned using OP_NotFound prior to invoking this opcode in
88580** this case. Specifically, if one is configured, the pre-update hook is
88581** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
88582** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
88583**
88584** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
88585** of the memory cell that contains the value that the rowid of the row will
88586** be set to by the update.
88587*/
88588case OP_Delete123: {
88589 VdbeCursor *pC;
88590 const char *zDb;
88591 Table *pTab;
88592 int opflags;
88593
88594 opflags = pOp->p2;
88595 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88596 pC = p->apCsr[pOp->p1];
88597 assert( pC!=0 )((void) (0));
88598 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
88599 assert( pC->uc.pCursor!=0 )((void) (0));
88600 assert( pC->deferredMoveto==0 )((void) (0));
88601 sqlite3VdbeIncrWriteCounter(p, pC);
88602
88603#ifdef SQLITE_DEBUG
88604 if( pOp->p4type==P4_TABLE(-6) && HasRowid(pOp->p4.pTab)(((pOp->p4.pTab)->tabFlags & 0x0020)==0) && pOp->p5==0 ){
88605 /* If p5 is zero, the seek operation that positioned the cursor prior to
88606 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
88607 ** the row that is being deleted */
88608 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
88609 assert( CORRUPT_DB || pC->movetoTarget==iKey )((void) (0));
88610 }
88611#endif
88612
88613 /* If the update-hook or pre-update-hook will be invoked, set zDb to
88614 ** the name of the db to pass as to it. Also set local pTab to a copy
88615 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
88616 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
88617 ** VdbeCursor.movetoTarget to the current rowid. */
88618 if( pOp->p4type==P4_TABLE(-6) && HAS_UPDATE_HOOK(db)((db)->xUpdateCallback) ){
88619 assert( pC->iDb>=0 )((void) (0));
88620 assert( pOp->p4.pTab!=0 )((void) (0));
88621 zDb = db->aDb[pC->iDb].zDbSName;
88622 pTab = pOp->p4.pTab;
88623 if( (pOp->p5 & OPFLAG_SAVEPOSITION0x02)!=0 && pC->isTable ){
88624 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
88625 }
88626 }else{
88627 zDb = 0; /* Not needed. Silence a compiler warning. */
88628 pTab = 0; /* Not needed. Silence a compiler warning. */
88629 }
88630
88631#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
88632 /* Invoke the pre-update-hook if required. */
88633 if( db->xPreUpdateCallback && pOp->p4.pTab ){
88634 assert( !(opflags & OPFLAG_ISUPDATE)((void) (0))
88635 || HasRowid(pTab)==0((void) (0))
88636 || (aMem[pOp->p3].flags & MEM_Int)((void) (0))
88637 )((void) (0));
88638 sqlite3VdbePreUpdateHook(p, pC,
88639 (opflags & OPFLAG_ISUPDATE0x04) ? SQLITE_UPDATE23 : SQLITE_DELETE9,
88640 zDb, pTab, pC->movetoTarget,
88641 pOp->p3
88642 );
88643 }
88644 if( opflags & OPFLAG_ISNOOP0x40 ) break;
88645#endif
88646
88647 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
88648 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 )((void) (0));
88649 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION )((void) (0));
88650 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE )((void) (0));
88651
88652#ifdef SQLITE_DEBUG
88653 if( p->pFrame==0 ){
88654 if( pC->isEphemeral==0
88655 && (pOp->p5 & OPFLAG_AUXDELETE0x04)==0
88656 && (pC->wrFlag & OPFLAG_FORDELETE0x08)==0
88657 ){
88658 nExtraDelete++;
88659 }
88660 if( pOp->p2 & OPFLAG_NCHANGE0x01 ){
88661 nExtraDelete--;
88662 }
88663 }
88664#endif
88665
88666 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
88667 pC->cacheStatus = CACHE_STALE0;
88668 pC->seekResult = 0;
88669 if( rc ) goto abort_due_to_error;
88670
88671 /* Invoke the update-hook if required. */
88672 if( opflags & OPFLAG_NCHANGE0x01 ){
88673 p->nChange++;
88674 if( db->xUpdateCallback && HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
88675 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE9, zDb, pTab->zName,
88676 pC->movetoTarget);
88677 assert( pC->iDb>=0 )((void) (0));
88678 }
88679 }
88680
88681 break;
88682}
88683/* Opcode: ResetCount * * * * *
88684**
88685** The value of the change counter is copied to the database handle
88686** change counter (returned by subsequent calls to sqlite3_changes()).
88687** Then the VMs internal change counter resets to 0.
88688** This is used by trigger programs.
88689*/
88690case OP_ResetCount124: {
88691 sqlite3VdbeSetChanges(db, p->nChange);
88692 p->nChange = 0;
88693 break;
88694}
88695
88696/* Opcode: SorterCompare P1 P2 P3 P4
88697** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
88698**
88699** P1 is a sorter cursor. This instruction compares a prefix of the
88700** record blob in register P3 against a prefix of the entry that
88701** the sorter cursor currently points to. Only the first P4 fields
88702** of r[P3] and the sorter record are compared.
88703**
88704** If either P3 or the sorter contains a NULL in one of their significant
88705** fields (not counting the P4 fields at the end which are ignored) then
88706** the comparison is assumed to be equal.
88707**
88708** Fall through to next instruction if the two records compare equal to
88709** each other. Jump to P2 if they are different.
88710*/
88711case OP_SorterCompare125: {
88712 VdbeCursor *pC;
88713 int res;
88714 int nKeyCol;
88715
88716 pC = p->apCsr[pOp->p1];
88717 assert( isSorter(pC) )((void) (0));
88718 assert( pOp->p4type==P4_INT32 )((void) (0));
88719 pIn3 = &aMem[pOp->p3];
88720 nKeyCol = pOp->p4.i;
88721 res = 0;
88722 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
88723 VdbeBranchTaken(res!=0,2);
88724 if( rc ) goto abort_due_to_error;
88725 if( res ) goto jump_to_p2;
88726 break;
88727};
88728
88729/* Opcode: SorterData P1 P2 P3 * *
88730** Synopsis: r[P2]=data
88731**
88732** Write into register P2 the current sorter data for sorter cursor P1.
88733** Then clear the column header cache on cursor P3.
88734**
88735** This opcode is normally use to move a record out of the sorter and into
88736** a register that is the source for a pseudo-table cursor created using
88737** OpenPseudo. That pseudo-table cursor is the one that is identified by
88738** parameter P3. Clearing the P3 column cache as part of this opcode saves
88739** us from having to issue a separate NullRow instruction to clear that cache.
88740*/
88741case OP_SorterData126: {
88742 VdbeCursor *pC;
88743
88744 pOut = &aMem[pOp->p2];
88745 pC = p->apCsr[pOp->p1];
88746 assert( isSorter(pC) )((void) (0));
88747 rc = sqlite3VdbeSorterRowkey(pC, pOut);
88748 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) )((void) (0));
88749 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88750 if( rc ) goto abort_due_to_error;
88751 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE0;
88752 break;
88753}
88754
88755/* Opcode: RowData P1 P2 P3 * *
88756** Synopsis: r[P2]=data
88757**
88758** Write into register P2 the complete row content for the row at
88759** which cursor P1 is currently pointing.
88760** There is no interpretation of the data.
88761** It is just copied onto the P2 register exactly as
88762** it is found in the database file.
88763**
88764** If cursor P1 is an index, then the content is the key of the row.
88765** If cursor P2 is a table, then the content extracted is the data.
88766**
88767** If the P1 cursor must be pointing to a valid row (not a NULL row)
88768** of a real table, not a pseudo-table.
88769**
88770** If P3!=0 then this opcode is allowed to make an ephemeral pointer
88771** into the database page. That means that the content of the output
88772** register will be invalidated as soon as the cursor moves - including
88773** moves caused by other cursors that "save" the current cursors
88774** position in order that they can write to the same table. If P3==0
88775** then a copy of the data is made into memory. P3!=0 is faster, but
88776** P3==0 is safer.
88777**
88778** If P3!=0 then the content of the P2 register is unsuitable for use
88779** in OP_Result and any OP_Result will invalidate the P2 register content.
88780** The P2 register content is invalidated by opcodes like OP_Function or
88781** by any use of another cursor pointing to the same table.
88782*/
88783case OP_RowData127: {
88784 VdbeCursor *pC;
88785 BtCursor *pCrsr;
88786 u32 n;
88787
88788 pOut = out2Prerelease(p, pOp);
88789
88790 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88791 pC = p->apCsr[pOp->p1];
88792 assert( pC!=0 )((void) (0));
88793 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
88794 assert( isSorter(pC)==0 )((void) (0));
88795 assert( pC->nullRow==0 )((void) (0));
88796 assert( pC->uc.pCursor!=0 )((void) (0));
88797 pCrsr = pC->uc.pCursor;
88798
88799 /* The OP_RowData opcodes always follow OP_NotExists or
88800 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
88801 ** that might invalidate the cursor.
88802 ** If this where not the case, on of the following assert()s
88803 ** would fail. Should this ever change (because of changes in the code
88804 ** generator) then the fix would be to insert a call to
88805 ** sqlite3VdbeCursorMoveto().
88806 */
88807 assert( pC->deferredMoveto==0 )((void) (0));
88808 assert( sqlite3BtreeCursorIsValid(pCrsr) )((void) (0));
88809#if 0 /* Not required due to the previous to assert() statements */
88810 rc = sqlite3VdbeCursorMoveto(pC);
88811 if( rc!=SQLITE_OK0 ) goto abort_due_to_error;
88812#endif
88813
88814 n = sqlite3BtreePayloadSize(pCrsr);
88815 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH0] ){
88816 goto too_big;
88817 }
88818 testcase( n==0 );
88819 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
88820 if( rc ) goto abort_due_to_error;
88821 if( !pOp->p3 ) Deephemeralize(pOut)if( ((pOut)->flags&0x1000)!=0 && sqlite3VdbeMemMakeWriteable
(pOut) ){ goto no_mem;}
;
88822 UPDATE_MAX_BLOBSIZE(pOut);
88823 REGISTER_TRACE(pOp->p2, pOut);
88824 break;
88825}
88826
88827/* Opcode: Rowid P1 P2 * * *
88828** Synopsis: r[P2]=rowid
88829**
88830** Store in register P2 an integer which is the key of the table entry that
88831** P1 is currently point to.
88832**
88833** P1 can be either an ordinary table or a virtual table. There used to
88834** be a separate OP_VRowid opcode for use with virtual tables, but this
88835** one opcode now works for both table types.
88836*/
88837case OP_Rowid128: { /* out2 */
88838 VdbeCursor *pC;
88839 i64 v;
88840 sqlite3_vtab *pVtab;
88841 const sqlite3_module *pModule;
88842
88843 pOut = out2Prerelease(p, pOp);
88844 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88845 pC = p->apCsr[pOp->p1];
88846 assert( pC!=0 )((void) (0));
88847 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow )((void) (0));
88848 if( pC->nullRow ){
88849 pOut->flags = MEM_Null0x0001;
88850 break;
88851 }else if( pC->deferredMoveto ){
88852 v = pC->movetoTarget;
88853#ifndef SQLITE_OMIT_VIRTUALTABLE
88854 }else if( pC->eCurType==CURTYPE_VTAB2 ){
88855 assert( pC->uc.pVCur!=0 )((void) (0));
88856 pVtab = pC->uc.pVCur->pVtab;
88857 pModule = pVtab->pModule;
88858 assert( pModule->xRowid )((void) (0));
88859 rc = pModule->xRowid(pC->uc.pVCur, &v);
88860 sqlite3VtabImportErrmsg(p, pVtab);
88861 if( rc ) goto abort_due_to_error;
88862#endif /* SQLITE_OMIT_VIRTUALTABLE */
88863 }else{
88864 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
88865 assert( pC->uc.pCursor!=0 )((void) (0));
88866 rc = sqlite3VdbeCursorRestore(pC);
88867 if( rc ) goto abort_due_to_error;
88868 if( pC->nullRow ){
88869 pOut->flags = MEM_Null0x0001;
88870 break;
88871 }
88872 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
88873 }
88874 pOut->u.i = v;
88875 break;
88876}
88877
88878/* Opcode: NullRow P1 * * * *
88879**
88880** Move the cursor P1 to a null row. Any OP_Column operations
88881** that occur while the cursor is on the null row will always
88882** write a NULL.
88883*/
88884case OP_NullRow129: {
88885 VdbeCursor *pC;
88886
88887 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88888 pC = p->apCsr[pOp->p1];
88889 assert( pC!=0 )((void) (0));
88890 pC->nullRow = 1;
88891 pC->cacheStatus = CACHE_STALE0;
88892 if( pC->eCurType==CURTYPE_BTREE0 ){
88893 assert( pC->uc.pCursor!=0 )((void) (0));
88894 sqlite3BtreeClearCursor(pC->uc.pCursor);
88895 }
88896#ifdef SQLITE_DEBUG
88897 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow129;
88898#endif
88899 break;
88900}
88901
88902/* Opcode: SeekEnd P1 * * * *
88903**
88904** Position cursor P1 at the end of the btree for the purpose of
88905** appending a new entry onto the btree.
88906**
88907** It is assumed that the cursor is used only for appending and so
88908** if the cursor is valid, then the cursor must already be pointing
88909** at the end of the btree and so no changes are made to
88910** the cursor.
88911*/
88912/* Opcode: Last P1 P2 * * *
88913**
88914** The next use of the Rowid or Column or Prev instruction for P1
88915** will refer to the last entry in the database table or index.
88916** If the table or index is empty and P2>0, then jump immediately to P2.
88917** If P2 is 0 or if the table or index is not empty, fall through
88918** to the following instruction.
88919**
88920** This opcode leaves the cursor configured to move in reverse order,
88921** from the end toward the beginning. In other words, the cursor is
88922** configured to use Prev, not Next.
88923*/
88924case OP_SeekEnd130:
88925case OP_Last32: { /* jump */
88926 VdbeCursor *pC;
88927 BtCursor *pCrsr;
88928 int res;
88929
88930 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88931 pC = p->apCsr[pOp->p1];
88932 assert( pC!=0 )((void) (0));
88933 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
88934 pCrsr = pC->uc.pCursor;
88935 res = 0;
88936 assert( pCrsr!=0 )((void) (0));
88937#ifdef SQLITE_DEBUG
88938 pC->seekOp = pOp->opcode;
88939#endif
88940 if( pOp->opcode==OP_SeekEnd130 ){
88941 assert( pOp->p2==0 )((void) (0));
88942 pC->seekResult = -1;
88943 if( sqlite3BtreeCursorIsValidNN(pCrsr) ){
88944 break;
88945 }
88946 }
88947 rc = sqlite3BtreeLast(pCrsr, &res);
88948 pC->nullRow = (u8)res;
88949 pC->deferredMoveto = 0;
88950 pC->cacheStatus = CACHE_STALE0;
88951 if( rc ) goto abort_due_to_error;
88952 if( pOp->p2>0 ){
88953 VdbeBranchTaken(res!=0,2);
88954 if( res ) goto jump_to_p2;
88955 }
88956 break;
88957}
88958
88959/* Opcode: IfSmaller P1 P2 P3 * *
88960**
88961** Estimate the number of rows in the table P1. Jump to P2 if that
88962** estimate is less than approximately 2**(0.1*P3).
88963*/
88964case OP_IfSmaller33: { /* jump */
88965 VdbeCursor *pC;
88966 BtCursor *pCrsr;
88967 int res;
88968 i64 sz;
88969
88970 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
88971 pC = p->apCsr[pOp->p1];
88972 assert( pC!=0 )((void) (0));
88973 pCrsr = pC->uc.pCursor;
88974 assert( pCrsr )((void) (0));
88975 rc = sqlite3BtreeFirst(pCrsr, &res);
88976 if( rc ) goto abort_due_to_error;
88977 if( res==0 ){
88978 sz = sqlite3BtreeRowCountEst(pCrsr);
88979 if( ALWAYS(sz>=0)(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
88980 }
88981 VdbeBranchTaken(res!=0,2);
88982 if( res ) goto jump_to_p2;
88983 break;
88984}
88985
88986
88987/* Opcode: SorterSort P1 P2 * * *
88988**
88989** After all records have been inserted into the Sorter object
88990** identified by P1, invoke this opcode to actually do the sorting.
88991** Jump to P2 if there are no records to be sorted.
88992**
88993** This opcode is an alias for OP_Sort and OP_Rewind that is used
88994** for Sorter objects.
88995*/
88996/* Opcode: Sort P1 P2 * * *
88997**
88998** This opcode does exactly the same thing as OP_Rewind except that
88999** it increments an undocumented global variable used for testing.
89000**
89001** Sorting is accomplished by writing records into a sorting index,
89002** then rewinding that index and playing it back from beginning to
89003** end. We use the OP_Sort opcode instead of OP_Rewind to do the
89004** rewinding so that the global variable will be incremented and
89005** regression tests can determine whether or not the optimizer is
89006** correctly optimizing out sorts.
89007*/
89008case OP_SorterSort34: /* jump */
89009case OP_Sort35: { /* jump */
89010#ifdef SQLITE_TEST
89011 sqlite3_sort_count++;
89012 sqlite3_search_count--;
89013#endif
89014 p->aCounter[SQLITE_STMTSTATUS_SORT2]++;
89015 /* Fall through into OP_Rewind */
89016}
89017/* Opcode: Rewind P1 P2 * * *
89018**
89019** The next use of the Rowid or Column or Next instruction for P1
89020** will refer to the first entry in the database table or index.
89021** If the table or index is empty, jump immediately to P2.
89022** If the table or index is not empty, fall through to the following
89023** instruction.
89024**
89025** This opcode leaves the cursor configured to move in forward order,
89026** from the beginning toward the end. In other words, the cursor is
89027** configured to use Next, not Prev.
89028*/
89029case OP_Rewind36: { /* jump */
89030 VdbeCursor *pC;
89031 BtCursor *pCrsr;
89032 int res;
89033
89034 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
89035 assert( pOp->p5==0 )((void) (0));
89036 pC = p->apCsr[pOp->p1];
89037 assert( pC!=0 )((void) (0));
89038 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) )((void) (0));
89039 res = 1;
89040#ifdef SQLITE_DEBUG
89041 pC->seekOp = OP_Rewind36;
89042#endif
89043 if( isSorter(pC)((pC)->eCurType==1) ){
89044 rc = sqlite3VdbeSorterRewind(pC, &res);
89045 }else{
89046 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
89047 pCrsr = pC->uc.pCursor;
89048 assert( pCrsr )((void) (0));
89049 rc = sqlite3BtreeFirst(pCrsr, &res);
89050 pC->deferredMoveto = 0;
89051 pC->cacheStatus = CACHE_STALE0;
89052 }
89053 if( rc ) goto abort_due_to_error;
89054 pC->nullRow = (u8)res;
89055 assert( pOp->p2>0 && pOp->p2<p->nOp )((void) (0));
89056 VdbeBranchTaken(res!=0,2);
89057 if( res ) goto jump_to_p2;
89058 break;
89059}
89060
89061/* Opcode: Next P1 P2 P3 P4 P5
89062**
89063** Advance cursor P1 so that it points to the next key/data pair in its
89064** table or index. If there are no more key/value pairs then fall through
89065** to the following instruction. But if the cursor advance was successful,
89066** jump immediately to P2.
89067**
89068** The Next opcode is only valid following an SeekGT, SeekGE, or
89069** OP_Rewind opcode used to position the cursor. Next is not allowed
89070** to follow SeekLT, SeekLE, or OP_Last.
89071**
89072** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
89073** been opened prior to this opcode or the program will segfault.
89074**
89075** The P3 value is a hint to the btree implementation. If P3==1, that
89076** means P1 is an SQL index and that this instruction could have been
89077** omitted if that index had been unique. P3 is usually 0. P3 is
89078** always either 0 or 1.
89079**
89080** P4 is always of type P4_ADVANCE. The function pointer points to
89081** sqlite3BtreeNext().
89082**
89083** If P5 is positive and the jump is taken, then event counter
89084** number P5-1 in the prepared statement is incremented.
89085**
89086** See also: Prev
89087*/
89088/* Opcode: Prev P1 P2 P3 P4 P5
89089**
89090** Back up cursor P1 so that it points to the previous key/data pair in its
89091** table or index. If there is no previous key/value pairs then fall through
89092** to the following instruction. But if the cursor backup was successful,
89093** jump immediately to P2.
89094**
89095**
89096** The Prev opcode is only valid following an SeekLT, SeekLE, or
89097** OP_Last opcode used to position the cursor. Prev is not allowed
89098** to follow SeekGT, SeekGE, or OP_Rewind.
89099**
89100** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
89101** not open then the behavior is undefined.
89102**
89103** The P3 value is a hint to the btree implementation. If P3==1, that
89104** means P1 is an SQL index and that this instruction could have been
89105** omitted if that index had been unique. P3 is usually 0. P3 is
89106** always either 0 or 1.
89107**
89108** P4 is always of type P4_ADVANCE. The function pointer points to
89109** sqlite3BtreePrevious().
89110**
89111** If P5 is positive and the jump is taken, then event counter
89112** number P5-1 in the prepared statement is incremented.
89113*/
89114/* Opcode: SorterNext P1 P2 * * P5
89115**
89116** This opcode works just like OP_Next except that P1 must be a
89117** sorter object for which the OP_SorterSort opcode has been
89118** invoked. This opcode advances the cursor to the next sorted
89119** record, or jumps to P2 if there are no more sorted records.
89120*/
89121case OP_SorterNext3: { /* jump */
89122 VdbeCursor *pC;
89123
89124 pC = p->apCsr[pOp->p1];
89125 assert( isSorter(pC) )((void) (0));
89126 rc = sqlite3VdbeSorterNext(db, pC);
89127 goto next_tail;
89128case OP_Prev4: /* jump */
89129case OP_Next5: /* jump */
89130 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
89131 assert( pOp->p5<ArraySize(p->aCounter) )((void) (0));
89132 pC = p->apCsr[pOp->p1];
89133 assert( pC!=0 )((void) (0));
89134 assert( pC->deferredMoveto==0 )((void) (0));
89135 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
89136 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext )((void) (0));
89137 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious )((void) (0));
89138
89139 /* The Next opcode is only used after SeekGT, SeekGE, Rewind, and Found.
89140 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
89141 assert( pOp->opcode!=OP_Next((void) (0))
89142 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE((void) (0))
89143 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found((void) (0))
89144 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid)((void) (0));
89145 assert( pOp->opcode!=OP_Prev((void) (0))
89146 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE((void) (0))
89147 || pC->seekOp==OP_Last((void) (0))
89148 || pC->seekOp==OP_NullRow)((void) (0));
89149
89150 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
89151next_tail:
89152 pC->cacheStatus = CACHE_STALE0;
89153 VdbeBranchTaken(rc==SQLITE_OK,2);
89154 if( rc==SQLITE_OK0 ){
89155 pC->nullRow = 0;
89156 p->aCounter[pOp->p5]++;
89157#ifdef SQLITE_TEST
89158 sqlite3_search_count++;
89159#endif
89160 goto jump_to_p2_and_check_for_interrupt;
89161 }
89162 if( rc!=SQLITE_DONE101 ) goto abort_due_to_error;
89163 rc = SQLITE_OK0;
89164 pC->nullRow = 1;
89165 goto check_for_interrupt;
89166}
89167
89168/* Opcode: IdxInsert P1 P2 P3 P4 P5
89169** Synopsis: key=r[P2]
89170**
89171** Register P2 holds an SQL index key made using the
89172** MakeRecord instructions. This opcode writes that key
89173** into the index P1. Data for the entry is nil.
89174**
89175** If P4 is not zero, then it is the number of values in the unpacked
89176** key of reg(P2). In that case, P3 is the index of the first register
89177** for the unpacked key. The availability of the unpacked key can sometimes
89178** be an optimization.
89179**
89180** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
89181** that this insert is likely to be an append.
89182**
89183** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
89184** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
89185** then the change counter is unchanged.
89186**
89187** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
89188** run faster by avoiding an unnecessary seek on cursor P1. However,
89189** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
89190** seeks on the cursor or if the most recent seek used a key equivalent
89191** to P2.
89192**
89193** This instruction only works for indices. The equivalent instruction
89194** for tables is OP_Insert.
89195*/
89196/* Opcode: SorterInsert P1 P2 * * *
89197** Synopsis: key=r[P2]
89198**
89199** Register P2 holds an SQL index key made using the
89200** MakeRecord instructions. This opcode writes that key
89201** into the sorter P1. Data for the entry is nil.
89202*/
89203case OP_SorterInsert131: /* in2 */
89204case OP_IdxInsert132: { /* in2 */
89205 VdbeCursor *pC;
89206 BtreePayload x;
89207
89208 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
89209 pC = p->apCsr[pOp->p1];
89210 sqlite3VdbeIncrWriteCounter(p, pC);
89211 assert( pC!=0 )((void) (0));
89212 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) )((void) (0));
89213 pIn2 = &aMem[pOp->p2];
89214 assert( pIn2->flags & MEM_Blob )((void) (0));
89215 if( pOp->p5 & OPFLAG_NCHANGE0x01 ) p->nChange++;
89216 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert )((void) (0));
89217 assert( pC->isTable==0 )((void) (0));
89218 rc = ExpandBlob(pIn2)(((pIn2)->flags&0x4000)?sqlite3VdbeMemExpandBlob(pIn2)
:0)
;
89219 if( rc ) goto abort_due_to_error;
89220 if( pOp->opcode==OP_SorterInsert131 ){
89221 rc = sqlite3VdbeSorterWrite(pC, pIn2);
89222 }else{
89223 x.nKey = pIn2->n;
89224 x.pKey = pIn2->z;
89225 x.aMem = aMem + pOp->p3;
89226 x.nMem = (u16)pOp->p4.i;
89227 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
89228 (pOp->p5 & (OPFLAG_APPEND0x08|OPFLAG_SAVEPOSITION0x02)),
89229 ((pOp->p5 & OPFLAG_USESEEKRESULT0x10) ? pC->seekResult : 0)
89230 );
89231 assert( pC->deferredMoveto==0 )((void) (0));
89232 pC->cacheStatus = CACHE_STALE0;
89233 }
89234 if( rc) goto abort_due_to_error;
89235 break;
89236}
89237
89238/* Opcode: IdxDelete P1 P2 P3 * *
89239** Synopsis: key=r[P2@P3]
89240**
89241** The content of P3 registers starting at register P2 form
89242** an unpacked index key. This opcode removes that entry from the
89243** index opened by cursor P1.
89244*/
89245case OP_IdxDelete133: {
89246 VdbeCursor *pC;
89247 BtCursor *pCrsr;
89248 int res;
89249 UnpackedRecord r;
89250
89251 assert( pOp->p3>0 )((void) (0));
89252 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 )((void) (0));
89253 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
89254 pC = p->apCsr[pOp->p1];
89255 assert( pC!=0 )((void) (0));
89256 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
89257 sqlite3VdbeIncrWriteCounter(p, pC);
89258 pCrsr = pC->uc.pCursor;
89259 assert( pCrsr!=0 )((void) (0));
89260 assert( pOp->p5==0 )((void) (0));
89261 r.pKeyInfo = pC->pKeyInfo;
89262 r.nField = (u16)pOp->p3;
89263 r.default_rc = 0;
89264 r.aMem = &aMem[pOp->p2];
89265 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
89266 if( rc ) goto abort_due_to_error;
89267 if( res==0 ){
89268 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE0x04);
89269 if( rc ) goto abort_due_to_error;
89270 }
89271 assert( pC->deferredMoveto==0 )((void) (0));
89272 pC->cacheStatus = CACHE_STALE0;
89273 pC->seekResult = 0;
89274 break;
89275}
89276
89277/* Opcode: DeferredSeek P1 * P3 P4 *
89278** Synopsis: Move P3 to P1.rowid if needed
89279**
89280** P1 is an open index cursor and P3 is a cursor on the corresponding
89281** table. This opcode does a deferred seek of the P3 table cursor
89282** to the row that corresponds to the current row of P1.
89283**
89284** This is a deferred seek. Nothing actually happens until
89285** the cursor is used to read a record. That way, if no reads
89286** occur, no unnecessary I/O happens.
89287**
89288** P4 may be an array of integers (type P4_INTARRAY) containing
89289** one entry for each column in the P3 table. If array entry a(i)
89290** is non-zero, then reading column a(i)-1 from cursor P3 is
89291** equivalent to performing the deferred seek and then reading column i
89292** from P1. This information is stored in P3 and used to redirect
89293** reads against P3 over to P1, thus possibly avoiding the need to
89294** seek and read cursor P3.
89295*/
89296/* Opcode: IdxRowid P1 P2 * * *
89297** Synopsis: r[P2]=rowid
89298**
89299** Write into register P2 an integer which is the last entry in the record at
89300** the end of the index key pointed to by cursor P1. This integer should be
89301** the rowid of the table entry to which this index entry points.
89302**
89303** See also: Rowid, MakeRecord.
89304*/
89305case OP_DeferredSeek134:
89306case OP_IdxRowid135: { /* out2 */
89307 VdbeCursor *pC; /* The P1 index cursor */
89308 VdbeCursor *pTabCur; /* The P2 table cursor (OP_DeferredSeek only) */
89309 i64 rowid; /* Rowid that P1 current points to */
89310
89311 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
89312 pC = p->apCsr[pOp->p1];
89313 assert( pC!=0 )((void) (0));
89314 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
89315 assert( pC->uc.pCursor!=0 )((void) (0));
89316 assert( pC->isTable==0 )((void) (0));
89317 assert( pC->deferredMoveto==0 )((void) (0));
89318 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid )((void) (0));
89319
89320 /* The IdxRowid and Seek opcodes are combined because of the commonality
89321 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
89322 rc = sqlite3VdbeCursorRestore(pC);
89323
89324 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
89325 ** out from under the cursor. That will never happens for an IdxRowid
89326 ** or Seek opcode */
89327 if( NEVER(rc!=SQLITE_OK)(rc!=0) ) goto abort_due_to_error;
89328
89329 if( !pC->nullRow ){
89330 rowid = 0; /* Not needed. Only used to silence a warning. */
89331 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
89332 if( rc!=SQLITE_OK0 ){
89333 goto abort_due_to_error;
89334 }
89335 if( pOp->opcode==OP_DeferredSeek134 ){
89336 assert( pOp->p3>=0 && pOp->p3<p->nCursor )((void) (0));
89337 pTabCur = p->apCsr[pOp->p3];
89338 assert( pTabCur!=0 )((void) (0));
89339 assert( pTabCur->eCurType==CURTYPE_BTREE )((void) (0));
89340 assert( pTabCur->uc.pCursor!=0 )((void) (0));
89341 assert( pTabCur->isTable )((void) (0));
89342 pTabCur->nullRow = 0;
89343 pTabCur->movetoTarget = rowid;
89344 pTabCur->deferredMoveto = 1;
89345 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 )((void) (0));
89346 pTabCur->aAltMap = pOp->p4.ai;
89347 pTabCur->pAltCursor = pC;
89348 }else{
89349 pOut = out2Prerelease(p, pOp);
89350 pOut->u.i = rowid;
89351 }
89352 }else{
89353 assert( pOp->opcode==OP_IdxRowid )((void) (0));
89354 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
89355 }
89356 break;
89357}
89358
89359/* Opcode: IdxGE P1 P2 P3 P4 P5
89360** Synopsis: key=r[P3@P4]
89361**
89362** The P4 register values beginning with P3 form an unpacked index
89363** key that omits the PRIMARY KEY. Compare this key value against the index
89364** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
89365** fields at the end.
89366**
89367** If the P1 index entry is greater than or equal to the key value
89368** then jump to P2. Otherwise fall through to the next instruction.
89369*/
89370/* Opcode: IdxGT P1 P2 P3 P4 P5
89371** Synopsis: key=r[P3@P4]
89372**
89373** The P4 register values beginning with P3 form an unpacked index
89374** key that omits the PRIMARY KEY. Compare this key value against the index
89375** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
89376** fields at the end.
89377**
89378** If the P1 index entry is greater than the key value
89379** then jump to P2. Otherwise fall through to the next instruction.
89380*/
89381/* Opcode: IdxLT P1 P2 P3 P4 P5
89382** Synopsis: key=r[P3@P4]
89383**
89384** The P4 register values beginning with P3 form an unpacked index
89385** key that omits the PRIMARY KEY or ROWID. Compare this key value against
89386** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
89387** ROWID on the P1 index.
89388**
89389** If the P1 index entry is less than the key value then jump to P2.
89390** Otherwise fall through to the next instruction.
89391*/
89392/* Opcode: IdxLE P1 P2 P3 P4 P5
89393** Synopsis: key=r[P3@P4]
89394**
89395** The P4 register values beginning with P3 form an unpacked index
89396** key that omits the PRIMARY KEY or ROWID. Compare this key value against
89397** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
89398** ROWID on the P1 index.
89399**
89400** If the P1 index entry is less than or equal to the key value then jump
89401** to P2. Otherwise fall through to the next instruction.
89402*/
89403case OP_IdxLE37: /* jump */
89404case OP_IdxGT38: /* jump */
89405case OP_IdxLT39: /* jump */
89406case OP_IdxGE40: { /* jump */
89407 VdbeCursor *pC;
89408 int res;
89409 UnpackedRecord r;
89410
89411 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
89412 pC = p->apCsr[pOp->p1];
89413 assert( pC!=0 )((void) (0));
89414 assert( pC->isOrdered )((void) (0));
89415 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
89416 assert( pC->uc.pCursor!=0)((void) (0));
89417 assert( pC->deferredMoveto==0 )((void) (0));
89418 assert( pOp->p5==0 || pOp->p5==1 )((void) (0));
89419 assert( pOp->p4type==P4_INT32 )((void) (0));
89420 r.pKeyInfo = pC->pKeyInfo;
89421 r.nField = (u16)pOp->p4.i;
89422 if( pOp->opcode<OP_IdxLT39 ){
89423 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT )((void) (0));
89424 r.default_rc = -1;
89425 }else{
89426 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT )((void) (0));
89427 r.default_rc = 0;
89428 }
89429 r.aMem = &aMem[pOp->p3];
89430#ifdef SQLITE_DEBUG
89431 {
89432 int i;
89433 for(i=0; i<r.nField; i++){
89434 assert( memIsValid(&r.aMem[i]) )((void) (0));
89435 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
89436 }
89437 }
89438#endif
89439 res = 0; /* Not needed. Only used to silence a warning. */
89440 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
89441 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) )((void) (0));
89442 if( (pOp->opcode&1)==(OP_IdxLT39&1) ){
89443 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT )((void) (0));
89444 res = -res;
89445 }else{
89446 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT )((void) (0));
89447 res++;
89448 }
89449 VdbeBranchTaken(res>0,2);
89450 if( rc ) goto abort_due_to_error;
89451 if( res>0 ) goto jump_to_p2;
89452 break;
89453}
89454
89455/* Opcode: Destroy P1 P2 P3 * *
89456**
89457** Delete an entire database table or index whose root page in the database
89458** file is given by P1.
89459**
89460** The table being destroyed is in the main database file if P3==0. If
89461** P3==1 then the table to be clear is in the auxiliary database file
89462** that is used to store tables create using CREATE TEMPORARY TABLE.
89463**
89464** If AUTOVACUUM is enabled then it is possible that another root page
89465** might be moved into the newly deleted root page in order to keep all
89466** root pages contiguous at the beginning of the database. The former
89467** value of the root page that moved - its value before the move occurred -
89468** is stored in register P2. If no page movement was required (because the
89469** table being dropped was already the last one in the database) then a
89470** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
89471** is stored in register P2.
89472**
89473** This opcode throws an error if there are any active reader VMs when
89474** it is invoked. This is done to avoid the difficulty associated with
89475** updating existing cursors when a root page is moved in an AUTOVACUUM
89476** database. This error is thrown even if the database is not an AUTOVACUUM
89477** db in order to avoid introducing an incompatibility between autovacuum
89478** and non-autovacuum modes.
89479**
89480** See also: Clear
89481*/
89482case OP_Destroy136: { /* out2 */
89483 int iMoved;
89484 int iDb;
89485
89486 sqlite3VdbeIncrWriteCounter(p, 0);
89487 assert( p->readOnly==0 )((void) (0));
89488 assert( pOp->p1>1 )((void) (0));
89489 pOut = out2Prerelease(p, pOp);
89490 pOut->flags = MEM_Null0x0001;
89491 if( db->nVdbeRead > db->nVDestroy+1 ){
89492 rc = SQLITE_LOCKED6;
89493 p->errorAction = OE_Abort2;
89494 goto abort_due_to_error;
89495 }else{
89496 iDb = pOp->p3;
89497 assert( DbMaskTest(p->btreeMask, iDb) )((void) (0));
89498 iMoved = 0; /* Not needed. Only to silence a warning. */
89499 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
89500 pOut->flags = MEM_Int0x0004;
89501 pOut->u.i = iMoved;
89502 if( rc ) goto abort_due_to_error;
89503#ifndef SQLITE_OMIT_AUTOVACUUM
89504 if( iMoved!=0 ){
89505 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
89506 /* All OP_Destroy operations occur on the same btree */
89507 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 )((void) (0));
89508 resetSchemaOnFault = iDb+1;
89509 }
89510#endif
89511 }
89512 break;
89513}
89514
89515/* Opcode: Clear P1 P2 P3
89516**
89517** Delete all contents of the database table or index whose root page
89518** in the database file is given by P1. But, unlike Destroy, do not
89519** remove the table or index from the database file.
89520**
89521** The table being clear is in the main database file if P2==0. If
89522** P2==1 then the table to be clear is in the auxiliary database file
89523** that is used to store tables create using CREATE TEMPORARY TABLE.
89524**
89525** If the P3 value is non-zero, then the table referred to must be an
89526** intkey table (an SQL table, not an index). In this case the row change
89527** count is incremented by the number of rows in the table being cleared.
89528** If P3 is greater than zero, then the value stored in register P3 is
89529** also incremented by the number of rows in the table being cleared.
89530**
89531** See also: Destroy
89532*/
89533case OP_Clear137: {
89534 int nChange;
89535
89536 sqlite3VdbeIncrWriteCounter(p, 0);
89537 nChange = 0;
89538 assert( p->readOnly==0 )((void) (0));
89539 assert( DbMaskTest(p->btreeMask, pOp->p2) )((void) (0));
89540 rc = sqlite3BtreeClearTable(
89541 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
89542 );
89543 if( pOp->p3 ){
89544 p->nChange += nChange;
89545 if( pOp->p3>0 ){
89546 assert( memIsValid(&aMem[pOp->p3]) )((void) (0));
89547 memAboutToChange(p, &aMem[pOp->p3]);
89548 aMem[pOp->p3].u.i += nChange;
89549 }
89550 }
89551 if( rc ) goto abort_due_to_error;
89552 break;
89553}
89554
89555/* Opcode: ResetSorter P1 * * * *
89556**
89557** Delete all contents from the ephemeral table or sorter
89558** that is open on cursor P1.
89559**
89560** This opcode only works for cursors used for sorting and
89561** opened with OP_OpenEphemeral or OP_SorterOpen.
89562*/
89563case OP_ResetSorter138: {
89564 VdbeCursor *pC;
89565
89566 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
89567 pC = p->apCsr[pOp->p1];
89568 assert( pC!=0 )((void) (0));
89569 if( isSorter(pC)((pC)->eCurType==1) ){
89570 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
89571 }else{
89572 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
89573 assert( pC->isEphemeral )((void) (0));
89574 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
89575 if( rc ) goto abort_due_to_error;
89576 }
89577 break;
89578}
89579
89580/* Opcode: CreateBtree P1 P2 P3 * *
89581** Synopsis: r[P2]=root iDb=P1 flags=P3
89582**
89583** Allocate a new b-tree in the main database file if P1==0 or in the
89584** TEMP database file if P1==1 or in an attached database if
89585** P1>1. The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
89586** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
89587** The root page number of the new b-tree is stored in register P2.
89588*/
89589case OP_CreateBtree139: { /* out2 */
89590 int pgno;
89591 Db *pDb;
89592
89593 sqlite3VdbeIncrWriteCounter(p, 0);
89594 pOut = out2Prerelease(p, pOp);
89595 pgno = 0;
89596 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY )((void) (0));
89597 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
89598 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
89599 assert( p->readOnly==0 )((void) (0));
89600 pDb = &db->aDb[pOp->p1];
89601 assert( pDb->pBt!=0 )((void) (0));
89602 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
89603 if( rc ) goto abort_due_to_error;
89604 pOut->u.i = pgno;
89605 break;
89606}
89607
89608/* Opcode: SqlExec * * * P4 *
89609**
89610** Run the SQL statement or statements specified in the P4 string.
89611*/
89612case OP_SqlExec140: {
89613 sqlite3VdbeIncrWriteCounter(p, 0);
89614 db->nSqlExec++;
89615 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
89616 db->nSqlExec--;
89617 if( rc ) goto abort_due_to_error;
89618 break;
89619}
89620
89621/* Opcode: ParseSchema P1 * * P4 *
89622**
89623** Read and parse all entries from the SQLITE_MASTER table of database P1
89624** that match the WHERE clause P4. If P4 is a NULL pointer, then the
89625** entire schema for P1 is reparsed.
89626**
89627** This opcode invokes the parser to create a new virtual machine,
89628** then runs the new virtual machine. It is thus a re-entrant opcode.
89629*/
89630case OP_ParseSchema141: {
89631 int iDb;
89632 const char *zMaster;
89633 char *zSql;
89634 InitData initData;
89635
89636 /* Any prepared statement that invokes this opcode will hold mutexes
89637 ** on every btree. This is a prerequisite for invoking
89638 ** sqlite3InitCallback().
89639 */
89640#ifdef SQLITE_DEBUG
89641 for(iDb=0; iDb<db->nDb; iDb++){
89642 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) )((void) (0));
89643 }
89644#endif
89645
89646 iDb = pOp->p1;
89647 assert( iDb>=0 && iDb<db->nDb )((void) (0));
89648 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) )((void) (0));
89649
89650#ifndef SQLITE_OMIT_ALTERTABLE
89651 if( pOp->p4.z==0 ){
89652 sqlite3SchemaClear(db->aDb[iDb].pSchema);
89653 db->mDbFlags &= ~DBFLAG_SchemaKnownOk0x0010;
89654 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, INITFLAG_AlterTable0x0001);
89655 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
89656 p->expired = 0;
89657 }else
89658#endif
89659 {
89660 zMaster = MASTER_NAME"sqlite_master";
89661 initData.db = db;
89662 initData.iDb = iDb;
89663 initData.pzErrMsg = &p->zErrMsg;
89664 initData.mInitFlags = 0;
89665 zSql = sqlite3MPrintf(db,
89666 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
89667 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
89668 if( zSql==0 ){
89669 rc = SQLITE_NOMEM_BKPT7;
89670 }else{
89671 assert( db->init.busy==0 )((void) (0));
89672 db->init.busy = 1;
89673 initData.rc = SQLITE_OK0;
89674 initData.nInitRow = 0;
89675 assert( !db->mallocFailed )((void) (0));
89676 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
89677 if( rc==SQLITE_OK0 ) rc = initData.rc;
89678 if( rc==SQLITE_OK0 && initData.nInitRow==0 ){
89679 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
89680 ** at least one SQL statement. Any less than that indicates that
89681 ** the sqlite_master table is corrupt. */
89682 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(89682);
89683 }
89684 sqlite3DbFreeNN(db, zSql);
89685 db->init.busy = 0;
89686 }
89687 }
89688 if( rc ){
89689 sqlite3ResetAllSchemasOfConnection(db);
89690 if( rc==SQLITE_NOMEM7 ){
89691 goto no_mem;
89692 }
89693 goto abort_due_to_error;
89694 }
89695 break;
89696}
89697
89698#if !defined(SQLITE_OMIT_ANALYZE)
89699/* Opcode: LoadAnalysis P1 * * * *
89700**
89701** Read the sqlite_stat1 table for database P1 and load the content
89702** of that table into the internal index hash table. This will cause
89703** the analysis to be used when preparing all subsequent queries.
89704*/
89705case OP_LoadAnalysis142: {
89706 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
89707 rc = sqlite3AnalysisLoad(db, pOp->p1);
89708 if( rc ) goto abort_due_to_error;
89709 break;
89710}
89711#endif /* !defined(SQLITE_OMIT_ANALYZE) */
89712
89713/* Opcode: DropTable P1 * * P4 *
89714**
89715** Remove the internal (in-memory) data structures that describe
89716** the table named P4 in database P1. This is called after a table
89717** is dropped from disk (using the Destroy opcode) in order to keep
89718** the internal representation of the
89719** schema consistent with what is on disk.
89720*/
89721case OP_DropTable143: {
89722 sqlite3VdbeIncrWriteCounter(p, 0);
89723 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
89724 break;
89725}
89726
89727/* Opcode: DropIndex P1 * * P4 *
89728**
89729** Remove the internal (in-memory) data structures that describe
89730** the index named P4 in database P1. This is called after an index
89731** is dropped from disk (using the Destroy opcode)
89732** in order to keep the internal representation of the
89733** schema consistent with what is on disk.
89734*/
89735case OP_DropIndex144: {
89736 sqlite3VdbeIncrWriteCounter(p, 0);
89737 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
89738 break;
89739}
89740
89741/* Opcode: DropTrigger P1 * * P4 *
89742**
89743** Remove the internal (in-memory) data structures that describe
89744** the trigger named P4 in database P1. This is called after a trigger
89745** is dropped from disk (using the Destroy opcode) in order to keep
89746** the internal representation of the
89747** schema consistent with what is on disk.
89748*/
89749case OP_DropTrigger146: {
89750 sqlite3VdbeIncrWriteCounter(p, 0);
89751 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
89752 break;
89753}
89754
89755
89756#ifndef SQLITE_OMIT_INTEGRITY_CHECK
89757/* Opcode: IntegrityCk P1 P2 P3 P4 P5
89758**
89759** Do an analysis of the currently open database. Store in
89760** register P1 the text of an error message describing any problems.
89761** If no problems are found, store a NULL in register P1.
89762**
89763** The register P3 contains one less than the maximum number of allowed errors.
89764** At most reg(P3) errors will be reported.
89765** In other words, the analysis stops as soon as reg(P1) errors are
89766** seen. Reg(P1) is updated with the number of errors remaining.
89767**
89768** The root page numbers of all tables in the database are integers
89769** stored in P4_INTARRAY argument.
89770**
89771** If P5 is not zero, the check is done on the auxiliary database
89772** file, not the main database file.
89773**
89774** This opcode is used to implement the integrity_check pragma.
89775*/
89776case OP_IntegrityCk147: {
89777 int nRoot; /* Number of tables to check. (Number of root pages.) */
89778 int *aRoot; /* Array of rootpage numbers for tables to be checked */
89779 int nErr; /* Number of errors reported */
89780 char *z; /* Text of the error report */
89781 Mem *pnErr; /* Register keeping track of errors remaining */
89782
89783 assert( p->bIsReader )((void) (0));
89784 nRoot = pOp->p2;
89785 aRoot = pOp->p4.ai;
89786 assert( nRoot>0 )((void) (0));
89787 assert( aRoot[0]==nRoot )((void) (0));
89788 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
89789 pnErr = &aMem[pOp->p3];
89790 assert( (pnErr->flags & MEM_Int)!=0 )((void) (0));
89791 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 )((void) (0));
89792 pIn1 = &aMem[pOp->p1];
89793 assert( pOp->p5<db->nDb )((void) (0));
89794 assert( DbMaskTest(p->btreeMask, pOp->p5) )((void) (0));
89795 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
89796 (int)pnErr->u.i+1, &nErr);
89797 sqlite3VdbeMemSetNull(pIn1);
89798 if( nErr==0 ){
89799 assert( z==0 )((void) (0));
89800 }else if( z==0 ){
89801 goto no_mem;
89802 }else{
89803 pnErr->u.i -= nErr-1;
89804 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF81, sqlite3_free);
89805 }
89806 UPDATE_MAX_BLOBSIZE(pIn1);
89807 sqlite3VdbeChangeEncoding(pIn1, encoding);
89808 break;
89809}
89810#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
89811
89812/* Opcode: RowSetAdd P1 P2 * * *
89813** Synopsis: rowset(P1)=r[P2]
89814**
89815** Insert the integer value held by register P2 into a RowSet object
89816** held in register P1.
89817**
89818** An assertion fails if P2 is not an integer.
89819*/
89820case OP_RowSetAdd148: { /* in1, in2 */
89821 pIn1 = &aMem[pOp->p1];
89822 pIn2 = &aMem[pOp->p2];
89823 assert( (pIn2->flags & MEM_Int)!=0 )((void) (0));
89824 if( (pIn1->flags & MEM_Blob0x0010)==0 ){
89825 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
89826 }
89827 assert( sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
89828 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
89829 break;
89830}
89831
89832/* Opcode: RowSetRead P1 P2 P3 * *
89833** Synopsis: r[P3]=rowset(P1)
89834**
89835** Extract the smallest value from the RowSet object in P1
89836** and put that value into register P3.
89837** Or, if RowSet object P1 is initially empty, leave P3
89838** unchanged and jump to instruction P2.
89839*/
89840case OP_RowSetRead41: { /* jump, in1, out3 */
89841 i64 val;
89842
89843 pIn1 = &aMem[pOp->p1];
89844 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
89845 if( (pIn1->flags & MEM_Blob0x0010)==0
89846 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
89847 ){
89848 /* The boolean index is empty */
89849 sqlite3VdbeMemSetNull(pIn1);
89850 VdbeBranchTaken(1,2);
89851 goto jump_to_p2_and_check_for_interrupt;
89852 }else{
89853 /* A value was pulled from the index */
89854 VdbeBranchTaken(0,2);
89855 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
89856 }
89857 goto check_for_interrupt;
89858}
89859
89860/* Opcode: RowSetTest P1 P2 P3 P4
89861** Synopsis: if r[P3] in rowset(P1) goto P2
89862**
89863** Register P3 is assumed to hold a 64-bit integer value. If register P1
89864** contains a RowSet object and that RowSet object contains
89865** the value held in P3, jump to register P2. Otherwise, insert the
89866** integer in P3 into the RowSet and continue on to the
89867** next opcode.
89868**
89869** The RowSet object is optimized for the case where sets of integers
89870** are inserted in distinct phases, which each set contains no duplicates.
89871** Each set is identified by a unique P4 value. The first set
89872** must have P4==0, the final set must have P4==-1, and for all other sets
89873** must have P4>0.
89874**
89875** This allows optimizations: (a) when P4==0 there is no need to test
89876** the RowSet object for P3, as it is guaranteed not to contain it,
89877** (b) when P4==-1 there is no need to insert the value, as it will
89878** never be tested for, and (c) when a value that is part of set X is
89879** inserted, there is no need to search to see if the same value was
89880** previously inserted as part of set X (only if it was previously
89881** inserted as part of some other set).
89882*/
89883case OP_RowSetTest42: { /* jump, in1, in3 */
89884 int iSet;
89885 int exists;
89886
89887 pIn1 = &aMem[pOp->p1];
89888 pIn3 = &aMem[pOp->p3];
89889 iSet = pOp->p4.i;
89890 assert( pIn3->flags&MEM_Int )((void) (0));
89891
89892 /* If there is anything other than a rowset object in memory cell P1,
89893 ** delete it now and initialize P1 with an empty rowset
89894 */
89895 if( (pIn1->flags & MEM_Blob0x0010)==0 ){
89896 if( sqlite3VdbeMemSetRowSet(pIn1) ) goto no_mem;
89897 }
89898 assert( sqlite3VdbeMemIsRowSet(pIn1) )((void) (0));
89899 assert( pOp->p4type==P4_INT32 )((void) (0));
89900 assert( iSet==-1 || iSet>=0 )((void) (0));
89901 if( iSet ){
89902 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
89903 VdbeBranchTaken(exists!=0,2);
89904 if( exists ) goto jump_to_p2;
89905 }
89906 if( iSet>=0 ){
89907 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
89908 }
89909 break;
89910}
89911
89912
89913#ifndef SQLITE_OMIT_TRIGGER
89914
89915/* Opcode: Program P1 P2 P3 P4 P5
89916**
89917** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
89918**
89919** P1 contains the address of the memory cell that contains the first memory
89920** cell in an array of values used as arguments to the sub-program. P2
89921** contains the address to jump to if the sub-program throws an IGNORE
89922** exception using the RAISE() function. Register P3 contains the address
89923** of a memory cell in this (the parent) VM that is used to allocate the
89924** memory required by the sub-vdbe at runtime.
89925**
89926** P4 is a pointer to the VM containing the trigger program.
89927**
89928** If P5 is non-zero, then recursive program invocation is enabled.
89929*/
89930case OP_Program45: { /* jump */
89931 int nMem; /* Number of memory registers for sub-program */
89932 int nByte; /* Bytes of runtime space required for sub-program */
89933 Mem *pRt; /* Register to allocate runtime space */
89934 Mem *pMem; /* Used to iterate through memory cells */
89935 Mem *pEnd; /* Last memory cell in new array */
89936 VdbeFrame *pFrame; /* New vdbe frame to execute in */
89937 SubProgram *pProgram; /* Sub-program to execute */
89938 void *t; /* Token identifying trigger */
89939
89940 pProgram = pOp->p4.pProgram;
89941 pRt = &aMem[pOp->p3];
89942 assert( pProgram->nOp>0 )((void) (0));
89943
89944 /* If the p5 flag is clear, then recursive invocation of triggers is
89945 ** disabled for backwards compatibility (p5 is set if this sub-program
89946 ** is really a trigger, not a foreign key action, and the flag set
89947 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
89948 **
89949 ** It is recursive invocation of triggers, at the SQL level, that is
89950 ** disabled. In some cases a single trigger may generate more than one
89951 ** SubProgram (if the trigger may be executed with more than one different
89952 ** ON CONFLICT algorithm). SubProgram structures associated with a
89953 ** single trigger all have the same value for the SubProgram.token
89954 ** variable. */
89955 if( pOp->p5 ){
89956 t = pProgram->token;
89957 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
89958 if( pFrame ) break;
89959 }
89960
89961 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH10] ){
89962 rc = SQLITE_ERROR1;
89963 sqlite3VdbeError(p, "too many levels of trigger recursion");
89964 goto abort_due_to_error;
89965 }
89966
89967 /* Register pRt is used to store the memory required to save the state
89968 ** of the current program, and the memory required at runtime to execute
89969 ** the trigger program. If this trigger has been fired before, then pRt
89970 ** is already allocated. Otherwise, it must be initialized. */
89971 if( (pRt->flags&MEM_Blob0x0010)==0 ){
89972 /* SubProgram.nMem is set to the number of memory cells used by the
89973 ** program stored in SubProgram.aOp. As well as these, one memory
89974 ** cell is required for each cursor used by the program. Set local
89975 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
89976 */
89977 nMem = pProgram->nMem + pProgram->nCsr;
89978 assert( nMem>0 )((void) (0));
89979 if( pProgram->nCsr==0 ) nMem++;
89980 nByte = ROUND8(sizeof(VdbeFrame))(((sizeof(VdbeFrame))+7)&~7)
89981 + nMem * sizeof(Mem)
89982 + pProgram->nCsr * sizeof(VdbeCursor*)
89983 + (pProgram->nOp + 7)/8;
89984 pFrame = sqlite3DbMallocZero(db, nByte);
89985 if( !pFrame ){
89986 goto no_mem;
89987 }
89988 sqlite3VdbeMemRelease(pRt);
89989 pRt->flags = MEM_Blob0x0010|MEM_Dyn0x0400;
89990 pRt->z = (char*)pFrame;
89991 pRt->n = nByte;
89992 pRt->xDel = sqlite3VdbeFrameMemDel;
89993
89994 pFrame->v = p;
89995 pFrame->nChildMem = nMem;
89996 pFrame->nChildCsr = pProgram->nCsr;
89997 pFrame->pc = (int)(pOp - aOp);
89998 pFrame->aMem = p->aMem;
89999 pFrame->nMem = p->nMem;
90000 pFrame->apCsr = p->apCsr;
90001 pFrame->nCursor = p->nCursor;
90002 pFrame->aOp = p->aOp;
90003 pFrame->nOp = p->nOp;
90004 pFrame->token = pProgram->token;
90005#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
90006 pFrame->anExec = p->anExec;
90007#endif
90008#ifdef SQLITE_DEBUG
90009 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC0x879fb71e;
90010#endif
90011
90012 pEnd = &VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
[pFrame->nChildMem];
90013 for(pMem=VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
; pMem!=pEnd; pMem++){
90014 pMem->flags = MEM_Undefined0x0080;
90015 pMem->db = db;
90016 }
90017 }else{
90018 pFrame = (VdbeFrame*)pRt->z;
90019 assert( pRt->xDel==sqlite3VdbeFrameMemDel )((void) (0));
90020 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem((void) (0))
90021 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) )((void) (0));
90022 assert( pProgram->nCsr==pFrame->nChildCsr )((void) (0));
90023 assert( (int)(pOp - aOp)==pFrame->pc )((void) (0));
90024 }
90025
90026 p->nFrame++;
90027 pFrame->pParent = p->pFrame;
90028 pFrame->lastRowid = db->lastRowid;
90029 pFrame->nChange = p->nChange;
90030 pFrame->nDbChange = p->db->nChange;
90031 assert( pFrame->pAuxData==0 )((void) (0));
90032 pFrame->pAuxData = p->pAuxData;
90033 p->pAuxData = 0;
90034 p->nChange = 0;
90035 p->pFrame = pFrame;
90036 p->aMem = aMem = VdbeFrameMem(pFrame)((Mem *)&((u8 *)pFrame)[(((sizeof(VdbeFrame))+7)&~7)]
)
;
90037 p->nMem = pFrame->nChildMem;
90038 p->nCursor = (u16)pFrame->nChildCsr;
90039 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
90040 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
90041 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
90042 p->aOp = aOp = pProgram->aOp;
90043 p->nOp = pProgram->nOp;
90044#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
90045 p->anExec = 0;
90046#endif
90047#ifdef SQLITE_DEBUG
90048 /* Verify that second and subsequent executions of the same trigger do not
90049 ** try to reuse register values from the first use. */
90050 {
90051 int i;
90052 for(i=0; i<p->nMem; i++){
90053 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
90054 aMem[i].flags |= MEM_Undefined0x0080; /* Cause a fault if this reg is reused */
90055 }
90056 }
90057#endif
90058 pOp = &aOp[-1];
90059 goto check_for_interrupt;
90060}
90061
90062/* Opcode: Param P1 P2 * * *
90063**
90064** This opcode is only ever present in sub-programs called via the
90065** OP_Program instruction. Copy a value currently stored in a memory
90066** cell of the calling (parent) frame to cell P2 in the current frames
90067** address space. This is used by trigger programs to access the new.*
90068** and old.* values.
90069**
90070** The address of the cell in the parent frame is determined by adding
90071** the value of the P1 argument to the value of the P1 argument to the
90072** calling OP_Program instruction.
90073*/
90074case OP_Param149: { /* out2 */
90075 VdbeFrame *pFrame;
90076 Mem *pIn;
90077 pOut = out2Prerelease(p, pOp);
90078 pFrame = p->pFrame;
90079 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
90080 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem0x1000);
90081 break;
90082}
90083
90084#endif /* #ifndef SQLITE_OMIT_TRIGGER */
90085
90086#ifndef SQLITE_OMIT_FOREIGN_KEY
90087/* Opcode: FkCounter P1 P2 * * *
90088** Synopsis: fkctr[P1]+=P2
90089**
90090** Increment a "constraint counter" by P2 (P2 may be negative or positive).
90091** If P1 is non-zero, the database constraint counter is incremented
90092** (deferred foreign key constraints). Otherwise, if P1 is zero, the
90093** statement counter is incremented (immediate foreign key constraints).
90094*/
90095case OP_FkCounter150: {
90096 if( db->flags & SQLITE_DeferFKs0x00080000 ){
90097 db->nDeferredImmCons += pOp->p2;
90098 }else if( pOp->p1 ){
90099 db->nDeferredCons += pOp->p2;
90100 }else{
90101 p->nFkConstraint += pOp->p2;
90102 }
90103 break;
90104}
90105
90106/* Opcode: FkIfZero P1 P2 * * *
90107** Synopsis: if fkctr[P1]==0 goto P2
90108**
90109** This opcode tests if a foreign key constraint-counter is currently zero.
90110** If so, jump to instruction P2. Otherwise, fall through to the next
90111** instruction.
90112**
90113** If P1 is non-zero, then the jump is taken if the database constraint-counter
90114** is zero (the one that counts deferred constraint violations). If P1 is
90115** zero, the jump is taken if the statement constraint-counter is zero
90116** (immediate foreign key constraint violations).
90117*/
90118case OP_FkIfZero46: { /* jump */
90119 if( pOp->p1 ){
90120 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
90121 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
90122 }else{
90123 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
90124 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
90125 }
90126 break;
90127}
90128#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
90129
90130#ifndef SQLITE_OMIT_AUTOINCREMENT
90131/* Opcode: MemMax P1 P2 * * *
90132** Synopsis: r[P1]=max(r[P1],r[P2])
90133**
90134** P1 is a register in the root frame of this VM (the root frame is
90135** different from the current frame if this instruction is being executed
90136** within a sub-program). Set the value of register P1 to the maximum of
90137** its current value and the value in register P2.
90138**
90139** This instruction throws an error if the memory cell is not initially
90140** an integer.
90141*/
90142case OP_MemMax151: { /* in2 */
90143 VdbeFrame *pFrame;
90144 if( p->pFrame ){
90145 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
90146 pIn1 = &pFrame->aMem[pOp->p1];
90147 }else{
90148 pIn1 = &aMem[pOp->p1];
90149 }
90150 assert( memIsValid(pIn1) )((void) (0));
90151 sqlite3VdbeMemIntegerify(pIn1);
90152 pIn2 = &aMem[pOp->p2];
90153 sqlite3VdbeMemIntegerify(pIn2);
90154 if( pIn1->u.i<pIn2->u.i){
90155 pIn1->u.i = pIn2->u.i;
90156 }
90157 break;
90158}
90159#endif /* SQLITE_OMIT_AUTOINCREMENT */
90160
90161/* Opcode: IfPos P1 P2 P3 * *
90162** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
90163**
90164** Register P1 must contain an integer.
90165** If the value of register P1 is 1 or greater, subtract P3 from the
90166** value in P1 and jump to P2.
90167**
90168** If the initial value of register P1 is less than 1, then the
90169** value is unchanged and control passes through to the next instruction.
90170*/
90171case OP_IfPos47: { /* jump, in1 */
90172 pIn1 = &aMem[pOp->p1];
90173 assert( pIn1->flags&MEM_Int )((void) (0));
90174 VdbeBranchTaken( pIn1->u.i>0, 2);
90175 if( pIn1->u.i>0 ){
90176 pIn1->u.i -= pOp->p3;
90177 goto jump_to_p2;
90178 }
90179 break;
90180}
90181
90182/* Opcode: OffsetLimit P1 P2 P3 * *
90183** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
90184**
90185** This opcode performs a commonly used computation associated with
90186** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
90187** holds the offset counter. The opcode computes the combined value
90188** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
90189** value computed is the total number of rows that will need to be
90190** visited in order to complete the query.
90191**
90192** If r[P3] is zero or negative, that means there is no OFFSET
90193** and r[P2] is set to be the value of the LIMIT, r[P1].
90194**
90195** if r[P1] is zero or negative, that means there is no LIMIT
90196** and r[P2] is set to -1.
90197**
90198** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
90199*/
90200case OP_OffsetLimit152: { /* in1, out2, in3 */
90201 i64 x;
90202 pIn1 = &aMem[pOp->p1];
90203 pIn3 = &aMem[pOp->p3];
90204 pOut = out2Prerelease(p, pOp);
90205 assert( pIn1->flags & MEM_Int )((void) (0));
90206 assert( pIn3->flags & MEM_Int )((void) (0));
90207 x = pIn1->u.i;
90208 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
90209 /* If the LIMIT is less than or equal to zero, loop forever. This
90210 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
90211 ** also loop forever. This is undocumented. In fact, one could argue
90212 ** that the loop should terminate. But assuming 1 billion iterations
90213 ** per second (far exceeding the capabilities of any current hardware)
90214 ** it would take nearly 300 years to actually reach the limit. So
90215 ** looping forever is a reasonable approximation. */
90216 pOut->u.i = -1;
90217 }else{
90218 pOut->u.i = x;
90219 }
90220 break;
90221}
90222
90223/* Opcode: IfNotZero P1 P2 * * *
90224** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
90225**
90226** Register P1 must contain an integer. If the content of register P1 is
90227** initially greater than zero, then decrement the value in register P1.
90228** If it is non-zero (negative or positive) and then also jump to P2.
90229** If register P1 is initially zero, leave it unchanged and fall through.
90230*/
90231case OP_IfNotZero48: { /* jump, in1 */
90232 pIn1 = &aMem[pOp->p1];
90233 assert( pIn1->flags&MEM_Int )((void) (0));
90234 VdbeBranchTaken(pIn1->u.i<0, 2);
90235 if( pIn1->u.i ){
90236 if( pIn1->u.i>0 ) pIn1->u.i--;
90237 goto jump_to_p2;
90238 }
90239 break;
90240}
90241
90242/* Opcode: DecrJumpZero P1 P2 * * *
90243** Synopsis: if (--r[P1])==0 goto P2
90244**
90245** Register P1 must hold an integer. Decrement the value in P1
90246** and jump to P2 if the new value is exactly zero.
90247*/
90248case OP_DecrJumpZero49: { /* jump, in1 */
90249 pIn1 = &aMem[pOp->p1];
90250 assert( pIn1->flags&MEM_Int )((void) (0));
90251 if( pIn1->u.i>SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ) pIn1->u.i--;
90252 VdbeBranchTaken(pIn1->u.i==0, 2);
90253 if( pIn1->u.i==0 ) goto jump_to_p2;
90254 break;
90255}
90256
90257
90258/* Opcode: AggStep * P2 P3 P4 P5
90259** Synopsis: accum=r[P3] step(r[P2@P5])
90260**
90261** Execute the xStep function for an aggregate.
90262** The function has P5 arguments. P4 is a pointer to the
90263** FuncDef structure that specifies the function. Register P3 is the
90264** accumulator.
90265**
90266** The P5 arguments are taken from register P2 and its
90267** successors.
90268*/
90269/* Opcode: AggInverse * P2 P3 P4 P5
90270** Synopsis: accum=r[P3] inverse(r[P2@P5])
90271**
90272** Execute the xInverse function for an aggregate.
90273** The function has P5 arguments. P4 is a pointer to the
90274** FuncDef structure that specifies the function. Register P3 is the
90275** accumulator.
90276**
90277** The P5 arguments are taken from register P2 and its
90278** successors.
90279*/
90280/* Opcode: AggStep1 P1 P2 P3 P4 P5
90281** Synopsis: accum=r[P3] step(r[P2@P5])
90282**
90283** Execute the xStep (if P1==0) or xInverse (if P1!=0) function for an
90284** aggregate. The function has P5 arguments. P4 is a pointer to the
90285** FuncDef structure that specifies the function. Register P3 is the
90286** accumulator.
90287**
90288** The P5 arguments are taken from register P2 and its
90289** successors.
90290**
90291** This opcode is initially coded as OP_AggStep0. On first evaluation,
90292** the FuncDef stored in P4 is converted into an sqlite3_context and
90293** the opcode is changed. In this way, the initialization of the
90294** sqlite3_context only happens once, instead of on each call to the
90295** step function.
90296*/
90297case OP_AggInverse153:
90298case OP_AggStep154: {
90299 int n;
90300 sqlite3_context *pCtx;
90301
90302 assert( pOp->p4type==P4_FUNCDEF )((void) (0));
90303 n = pOp->p5;
90304 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
90305 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) )((void) (0));
90306 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n )((void) (0));
90307 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
90308 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
90309 if( pCtx==0 ) goto no_mem;
90310 pCtx->pMem = 0;
90311 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
90312 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null0x0001);
90313 pCtx->pFunc = pOp->p4.pFunc;
90314 pCtx->iOp = (int)(pOp - aOp);
90315 pCtx->pVdbe = p;
90316 pCtx->skipFlag = 0;
90317 pCtx->isError = 0;
90318 pCtx->argc = n;
90319 pOp->p4type = P4_FUNCCTX(-16);
90320 pOp->p4.pCtx = pCtx;
90321
90322 /* OP_AggInverse must have P1==1 and OP_AggStep must have P1==0 */
90323 assert( pOp->p1==(pOp->opcode==OP_AggInverse) )((void) (0));
90324
90325 pOp->opcode = OP_AggStep1155;
90326 /* Fall through into OP_AggStep */
90327}
90328case OP_AggStep1155: {
90329 int i;
90330 sqlite3_context *pCtx;
90331 Mem *pMem;
90332
90333 assert( pOp->p4type==P4_FUNCCTX )((void) (0));
90334 pCtx = pOp->p4.pCtx;
90335 pMem = &aMem[pOp->p3];
90336
90337#ifdef SQLITE_DEBUG
90338 if( pOp->p1 ){
90339 /* This is an OP_AggInverse call. Verify that xStep has always
90340 ** been called at least once prior to any xInverse call. */
90341 assert( pMem->uTemp==0x1122e0e3 )((void) (0));
90342 }else{
90343 /* This is an OP_AggStep call. Mark it as such. */
90344 pMem->uTemp = 0x1122e0e3;
90345 }
90346#endif
90347
90348 /* If this function is inside of a trigger, the register array in aMem[]
90349 ** might change from one evaluation to the next. The next block of code
90350 ** checks to see if the register array has changed, and if so it
90351 ** reinitializes the relavant parts of the sqlite3_context object */
90352 if( pCtx->pMem != pMem ){
90353 pCtx->pMem = pMem;
90354 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
90355 }
90356
90357#ifdef SQLITE_DEBUG
90358 for(i=0; i<pCtx->argc; i++){
90359 assert( memIsValid(pCtx->argv[i]) )((void) (0));
90360 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
90361 }
90362#endif
90363
90364 pMem->n++;
90365 assert( pCtx->pOut->flags==MEM_Null )((void) (0));
90366 assert( pCtx->isError==0 )((void) (0));
90367 assert( pCtx->skipFlag==0 )((void) (0));
90368#ifndef SQLITE_OMIT_WINDOWFUNC
90369 if( pOp->p1 ){
90370 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
90371 }else
90372#endif
90373 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
90374
90375 if( pCtx->isError ){
90376 if( pCtx->isError>0 ){
90377 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
90378 rc = pCtx->isError;
90379 }
90380 if( pCtx->skipFlag ){
90381 assert( pOp[-1].opcode==OP_CollSeq )((void) (0));
90382 i = pOp[-1].p1;
90383 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
90384 pCtx->skipFlag = 0;
90385 }
90386 sqlite3VdbeMemRelease(pCtx->pOut);
90387 pCtx->pOut->flags = MEM_Null0x0001;
90388 pCtx->isError = 0;
90389 if( rc ) goto abort_due_to_error;
90390 }
90391 assert( pCtx->pOut->flags==MEM_Null )((void) (0));
90392 assert( pCtx->skipFlag==0 )((void) (0));
90393 break;
90394}
90395
90396/* Opcode: AggFinal P1 P2 * P4 *
90397** Synopsis: accum=r[P1] N=P2
90398**
90399** P1 is the memory location that is the accumulator for an aggregate
90400** or window function. Execute the finalizer function
90401** for an aggregate and store the result in P1.
90402**
90403** P2 is the number of arguments that the step function takes and
90404** P4 is a pointer to the FuncDef for this function. The P2
90405** argument is not used by this opcode. It is only there to disambiguate
90406** functions that can take varying numbers of arguments. The
90407** P4 argument is only needed for the case where
90408** the step function was not previously called.
90409*/
90410/* Opcode: AggValue * P2 P3 P4 *
90411** Synopsis: r[P3]=value N=P2
90412**
90413** Invoke the xValue() function and store the result in register P3.
90414**
90415** P2 is the number of arguments that the step function takes and
90416** P4 is a pointer to the FuncDef for this function. The P2
90417** argument is not used by this opcode. It is only there to disambiguate
90418** functions that can take varying numbers of arguments. The
90419** P4 argument is only needed for the case where
90420** the step function was not previously called.
90421*/
90422case OP_AggValue156:
90423case OP_AggFinal157: {
90424 Mem *pMem;
90425 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) )((void) (0));
90426 assert( pOp->p3==0 || pOp->opcode==OP_AggValue )((void) (0));
90427 pMem = &aMem[pOp->p1];
90428 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 )((void) (0));
90429#ifndef SQLITE_OMIT_WINDOWFUNC
90430 if( pOp->p3 ){
90431 memAboutToChange(p, &aMem[pOp->p3]);
90432 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
90433 pMem = &aMem[pOp->p3];
90434 }else
90435#endif
90436 {
90437 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
90438 }
90439
90440 if( rc ){
90441 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
90442 goto abort_due_to_error;
90443 }
90444 sqlite3VdbeChangeEncoding(pMem, encoding);
90445 UPDATE_MAX_BLOBSIZE(pMem);
90446 if( sqlite3VdbeMemTooBig(pMem) ){
90447 goto too_big;
90448 }
90449 break;
90450}
90451
90452#ifndef SQLITE_OMIT_WAL
90453/* Opcode: Checkpoint P1 P2 P3 * *
90454**
90455** Checkpoint database P1. This is a no-op if P1 is not currently in
90456** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
90457** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
90458** SQLITE_BUSY or not, respectively. Write the number of pages in the
90459** WAL after the checkpoint into mem[P3+1] and the number of pages
90460** in the WAL that have been checkpointed after the checkpoint
90461** completes into mem[P3+2]. However on an error, mem[P3+1] and
90462** mem[P3+2] are initialized to -1.
90463*/
90464case OP_Checkpoint6: {
90465 int i; /* Loop counter */
90466 int aRes[3]; /* Results */
90467 Mem *pMem; /* Write results here */
90468
90469 assert( p->readOnly==0 )((void) (0));
90470 aRes[0] = 0;
90471 aRes[1] = aRes[2] = -1;
90472 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE((void) (0))
90473 || pOp->p2==SQLITE_CHECKPOINT_FULL((void) (0))
90474 || pOp->p2==SQLITE_CHECKPOINT_RESTART((void) (0))
90475 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE((void) (0))
90476 )((void) (0));
90477 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
90478 if( rc ){
90479 if( rc!=SQLITE_BUSY5 ) goto abort_due_to_error;
90480 rc = SQLITE_OK0;
90481 aRes[0] = 1;
90482 }
90483 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
90484 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
90485 }
90486 break;
90487};
90488#endif
90489
90490#ifndef SQLITE_OMIT_PRAGMA
90491/* Opcode: JournalMode P1 P2 P3 * *
90492**
90493** Change the journal mode of database P1 to P3. P3 must be one of the
90494** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
90495** modes (delete, truncate, persist, off and memory), this is a simple
90496** operation. No IO is required.
90497**
90498** If changing into or out of WAL mode the procedure is more complicated.
90499**
90500** Write a string containing the final journal-mode to register P2.
90501*/
90502case OP_JournalMode7: { /* out2 */
90503 Btree *pBt; /* Btree to change journal mode of */
90504 Pager *pPager; /* Pager associated with pBt */
90505 int eNew; /* New journal mode */
90506 int eOld; /* The old journal mode */
90507#ifndef SQLITE_OMIT_WAL
90508 const char *zFilename; /* Name of database file for pPager */
90509#endif
90510
90511 pOut = out2Prerelease(p, pOp);
90512 eNew = pOp->p3;
90513 assert( eNew==PAGER_JOURNALMODE_DELETE((void) (0))
90514 || eNew==PAGER_JOURNALMODE_TRUNCATE((void) (0))
90515 || eNew==PAGER_JOURNALMODE_PERSIST((void) (0))
90516 || eNew==PAGER_JOURNALMODE_OFF((void) (0))
90517 || eNew==PAGER_JOURNALMODE_MEMORY((void) (0))
90518 || eNew==PAGER_JOURNALMODE_WAL((void) (0))
90519 || eNew==PAGER_JOURNALMODE_QUERY((void) (0))
90520 )((void) (0));
90521 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
90522 assert( p->readOnly==0 )((void) (0));
90523
90524 pBt = db->aDb[pOp->p1].pBt;
90525 pPager = sqlite3BtreePager(pBt);
90526 eOld = sqlite3PagerGetJournalMode(pPager);
90527 if( eNew==PAGER_JOURNALMODE_QUERY(-1) ) eNew = eOld;
90528 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
90529
90530#ifndef SQLITE_OMIT_WAL
90531 zFilename = sqlite3PagerFilename(pPager, 1);
90532
90533 /* Do not allow a transition to journal_mode=WAL for a database
90534 ** in temporary storage or if the VFS does not support shared memory
90535 */
90536 if( eNew==PAGER_JOURNALMODE_WAL5
90537 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
90538 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
90539 ){
90540 eNew = eOld;
90541 }
90542
90543 if( (eNew!=eOld)
90544 && (eOld==PAGER_JOURNALMODE_WAL5 || eNew==PAGER_JOURNALMODE_WAL5)
90545 ){
90546 if( !db->autoCommit || db->nVdbeRead>1 ){
90547 rc = SQLITE_ERROR1;
90548 sqlite3VdbeError(p,
90549 "cannot change %s wal mode from within a transaction",
90550 (eNew==PAGER_JOURNALMODE_WAL5 ? "into" : "out of")
90551 );
90552 goto abort_due_to_error;
90553 }else{
90554
90555 if( eOld==PAGER_JOURNALMODE_WAL5 ){
90556 /* If leaving WAL mode, close the log file. If successful, the call
90557 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
90558 ** file. An EXCLUSIVE lock may still be held on the database file
90559 ** after a successful return.
90560 */
90561 rc = sqlite3PagerCloseWal(pPager, db);
90562 if( rc==SQLITE_OK0 ){
90563 sqlite3PagerSetJournalMode(pPager, eNew);
90564 }
90565 }else if( eOld==PAGER_JOURNALMODE_MEMORY4 ){
90566 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
90567 ** as an intermediate */
90568 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF2);
90569 }
90570
90571 /* Open a transaction on the database file. Regardless of the journal
90572 ** mode, this transaction always uses a rollback journal.
90573 */
90574 assert( sqlite3BtreeIsInTrans(pBt)==0 )((void) (0));
90575 if( rc==SQLITE_OK0 ){
90576 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL5 ? 2 : 1));
90577 }
90578 }
90579 }
90580#endif /* ifndef SQLITE_OMIT_WAL */
90581
90582 if( rc ) eNew = eOld;
90583 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
90584
90585 pOut->flags = MEM_Str0x0002|MEM_Static0x0800|MEM_Term0x0200;
90586 pOut->z = (char *)sqlite3JournalModename(eNew);
90587 pOut->n = sqlite3Strlen30(pOut->z);
90588 pOut->enc = SQLITE_UTF81;
90589 sqlite3VdbeChangeEncoding(pOut, encoding);
90590 if( rc ) goto abort_due_to_error;
90591 break;
90592};
90593#endif /* SQLITE_OMIT_PRAGMA */
90594
90595#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
90596/* Opcode: Vacuum P1 P2 * * *
90597**
90598** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
90599** for an attached database. The "temp" database may not be vacuumed.
90600**
90601** If P2 is not zero, then it is a register holding a string which is
90602** the file into which the result of vacuum should be written. When
90603** P2 is zero, the vacuum overwrites the original database.
90604*/
90605case OP_Vacuum8: {
90606 assert( p->readOnly==0 )((void) (0));
90607 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
90608 pOp->p2 ? &aMem[pOp->p2] : 0);
90609 if( rc ) goto abort_due_to_error;
90610 break;
90611}
90612#endif
90613
90614#if !defined(SQLITE_OMIT_AUTOVACUUM)
90615/* Opcode: IncrVacuum P1 P2 * * *
90616**
90617** Perform a single step of the incremental vacuum procedure on
90618** the P1 database. If the vacuum has finished, jump to instruction
90619** P2. Otherwise, fall through to the next instruction.
90620*/
90621case OP_IncrVacuum59: { /* jump */
90622 Btree *pBt;
90623
90624 assert( pOp->p1>=0 && pOp->p1<db->nDb )((void) (0));
90625 assert( DbMaskTest(p->btreeMask, pOp->p1) )((void) (0));
90626 assert( p->readOnly==0 )((void) (0));
90627 pBt = db->aDb[pOp->p1].pBt;
90628 rc = sqlite3BtreeIncrVacuum(pBt);
90629 VdbeBranchTaken(rc==SQLITE_DONE,2);
90630 if( rc ){
90631 if( rc!=SQLITE_DONE101 ) goto abort_due_to_error;
90632 rc = SQLITE_OK0;
90633 goto jump_to_p2;
90634 }
90635 break;
90636}
90637#endif
90638
90639/* Opcode: Expire P1 P2 * * *
90640**
90641** Cause precompiled statements to expire. When an expired statement
90642** is executed using sqlite3_step() it will either automatically
90643** reprepare itself (if it was originally created using sqlite3_prepare_v2())
90644** or it will fail with SQLITE_SCHEMA.
90645**
90646** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
90647** then only the currently executing statement is expired.
90648**
90649** If P2 is 0, then SQL statements are expired immediately. If P2 is 1,
90650** then running SQL statements are allowed to continue to run to completion.
90651** The P2==1 case occurs when a CREATE INDEX or similar schema change happens
90652** that might help the statement run faster but which does not affect the
90653** correctness of operation.
90654*/
90655case OP_Expire158: {
90656 assert( pOp->p2==0 || pOp->p2==1 )((void) (0));
90657 if( !pOp->p1 ){
90658 sqlite3ExpirePreparedStatements(db, pOp->p2);
90659 }else{
90660 p->expired = pOp->p2+1;
90661 }
90662 break;
90663}
90664
90665#ifndef SQLITE_OMIT_SHARED_CACHE
90666/* Opcode: TableLock P1 P2 P3 P4 *
90667** Synopsis: iDb=P1 root=P2 write=P3
90668**
90669** Obtain a lock on a particular table. This instruction is only used when
90670** the shared-cache feature is enabled.
90671**
90672** P1 is the index of the database in sqlite3.aDb[] of the database
90673** on which the lock is acquired. A readlock is obtained if P3==0 or
90674** a write lock if P3==1.
90675**
90676** P2 contains the root-page of the table to lock.
90677**
90678** P4 contains a pointer to the name of the table being locked. This is only
90679** used to generate an error message if the lock cannot be obtained.
90680*/
90681case OP_TableLock159: {
90682 u8 isWriteLock = (u8)pOp->p3;
90683 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit0x00000400) ){
90684 int p1 = pOp->p1;
90685 assert( p1>=0 && p1<db->nDb )((void) (0));
90686 assert( DbMaskTest(p->btreeMask, p1) )((void) (0));
90687 assert( isWriteLock==0 || isWriteLock==1 )((void) (0));
90688 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
90689 if( rc ){
90690 if( (rc&0xFF)==SQLITE_LOCKED6 ){
90691 const char *z = pOp->p4.z;
90692 sqlite3VdbeError(p, "database table is locked: %s", z);
90693 }
90694 goto abort_due_to_error;
90695 }
90696 }
90697 break;
90698}
90699#endif /* SQLITE_OMIT_SHARED_CACHE */
90700
90701#ifndef SQLITE_OMIT_VIRTUALTABLE
90702/* Opcode: VBegin * * * P4 *
90703**
90704** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
90705** xBegin method for that table.
90706**
90707** Also, whether or not P4 is set, check that this is not being called from
90708** within a callback to a virtual table xSync() method. If it is, the error
90709** code will be set to SQLITE_LOCKED.
90710*/
90711case OP_VBegin160: {
90712 VTable *pVTab;
90713 pVTab = pOp->p4.pVtab;
90714 rc = sqlite3VtabBegin(db, pVTab);
90715 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
90716 if( rc ) goto abort_due_to_error;
90717 break;
90718}
90719#endif /* SQLITE_OMIT_VIRTUALTABLE */
90720
90721#ifndef SQLITE_OMIT_VIRTUALTABLE
90722/* Opcode: VCreate P1 P2 * * *
90723**
90724** P2 is a register that holds the name of a virtual table in database
90725** P1. Call the xCreate method for that table.
90726*/
90727case OP_VCreate161: {
90728 Mem sMem; /* For storing the record being decoded */
90729 const char *zTab; /* Name of the virtual table */
90730
90731 memset(&sMem, 0, sizeof(sMem));
90732 sMem.db = db;
90733 /* Because P2 is always a static string, it is impossible for the
90734 ** sqlite3VdbeMemCopy() to fail */
90735 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 )((void) (0));
90736 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 )((void) (0));
90737 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
90738 assert( rc==SQLITE_OK )((void) (0));
90739 zTab = (const char*)sqlite3_value_text(&sMem);
90740 assert( zTab || db->mallocFailed )((void) (0));
90741 if( zTab ){
90742 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
90743 }
90744 sqlite3VdbeMemRelease(&sMem);
90745 if( rc ) goto abort_due_to_error;
90746 break;
90747}
90748#endif /* SQLITE_OMIT_VIRTUALTABLE */
90749
90750#ifndef SQLITE_OMIT_VIRTUALTABLE
90751/* Opcode: VDestroy P1 * * P4 *
90752**
90753** P4 is the name of a virtual table in database P1. Call the xDestroy method
90754** of that table.
90755*/
90756case OP_VDestroy162: {
90757 db->nVDestroy++;
90758 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
90759 db->nVDestroy--;
90760 assert( p->errorAction==OE_Abort && p->usesStmtJournal )((void) (0));
90761 if( rc ) goto abort_due_to_error;
90762 break;
90763}
90764#endif /* SQLITE_OMIT_VIRTUALTABLE */
90765
90766#ifndef SQLITE_OMIT_VIRTUALTABLE
90767/* Opcode: VOpen P1 * * P4 *
90768**
90769** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
90770** P1 is a cursor number. This opcode opens a cursor to the virtual
90771** table and stores that cursor in P1.
90772*/
90773case OP_VOpen163: {
90774 VdbeCursor *pCur;
90775 sqlite3_vtab_cursor *pVCur;
90776 sqlite3_vtab *pVtab;
90777 const sqlite3_module *pModule;
90778
90779 assert( p->bIsReader )((void) (0));
90780 pCur = 0;
90781 pVCur = 0;
90782 pVtab = pOp->p4.pVtab->pVtab;
90783 if( pVtab==0 || NEVER(pVtab->pModule==0)(pVtab->pModule==0) ){
90784 rc = SQLITE_LOCKED6;
90785 goto abort_due_to_error;
90786 }
90787 pModule = pVtab->pModule;
90788 rc = pModule->xOpen(pVtab, &pVCur);
90789 sqlite3VtabImportErrmsg(p, pVtab);
90790 if( rc ) goto abort_due_to_error;
90791
90792 /* Initialize sqlite3_vtab_cursor base class */
90793 pVCur->pVtab = pVtab;
90794
90795 /* Initialize vdbe cursor object */
90796 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB2);
90797 if( pCur ){
90798 pCur->uc.pVCur = pVCur;
90799 pVtab->nRef++;
90800 }else{
90801 assert( db->mallocFailed )((void) (0));
90802 pModule->xClose(pVCur);
90803 goto no_mem;
90804 }
90805 break;
90806}
90807#endif /* SQLITE_OMIT_VIRTUALTABLE */
90808
90809#ifndef SQLITE_OMIT_VIRTUALTABLE
90810/* Opcode: VFilter P1 P2 P3 P4 *
90811** Synopsis: iplan=r[P3] zplan='P4'
90812**
90813** P1 is a cursor opened using VOpen. P2 is an address to jump to if
90814** the filtered result set is empty.
90815**
90816** P4 is either NULL or a string that was generated by the xBestIndex
90817** method of the module. The interpretation of the P4 string is left
90818** to the module implementation.
90819**
90820** This opcode invokes the xFilter method on the virtual table specified
90821** by P1. The integer query plan parameter to xFilter is stored in register
90822** P3. Register P3+1 stores the argc parameter to be passed to the
90823** xFilter method. Registers P3+2..P3+1+argc are the argc
90824** additional parameters which are passed to
90825** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
90826**
90827** A jump is made to P2 if the result set after filtering would be empty.
90828*/
90829case OP_VFilter9: { /* jump */
90830 int nArg;
90831 int iQuery;
90832 const sqlite3_module *pModule;
90833 Mem *pQuery;
90834 Mem *pArgc;
90835 sqlite3_vtab_cursor *pVCur;
90836 sqlite3_vtab *pVtab;
90837 VdbeCursor *pCur;
90838 int res;
90839 int i;
90840 Mem **apArg;
90841
90842 pQuery = &aMem[pOp->p3];
90843 pArgc = &pQuery[1];
90844 pCur = p->apCsr[pOp->p1];
90845 assert( memIsValid(pQuery) )((void) (0));
90846 REGISTER_TRACE(pOp->p3, pQuery);
90847 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
90848 pVCur = pCur->uc.pVCur;
90849 pVtab = pVCur->pVtab;
90850 pModule = pVtab->pModule;
90851
90852 /* Grab the index number and argc parameters */
90853 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int )((void) (0));
90854 nArg = (int)pArgc->u.i;
90855 iQuery = (int)pQuery->u.i;
90856
90857 /* Invoke the xFilter method */
90858 res = 0;
90859 apArg = p->apArg;
90860 for(i = 0; i<nArg; i++){
90861 apArg[i] = &pArgc[i+1];
90862 }
90863 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
90864 sqlite3VtabImportErrmsg(p, pVtab);
90865 if( rc ) goto abort_due_to_error;
90866 res = pModule->xEof(pVCur);
90867 pCur->nullRow = 0;
90868 VdbeBranchTaken(res!=0,2);
90869 if( res ) goto jump_to_p2;
90870 break;
90871}
90872#endif /* SQLITE_OMIT_VIRTUALTABLE */
90873
90874#ifndef SQLITE_OMIT_VIRTUALTABLE
90875/* Opcode: VColumn P1 P2 P3 * P5
90876** Synopsis: r[P3]=vcolumn(P2)
90877**
90878** Store in register P3 the value of the P2-th column of
90879** the current row of the virtual-table of cursor P1.
90880**
90881** If the VColumn opcode is being used to fetch the value of
90882** an unchanging column during an UPDATE operation, then the P5
90883** value is OPFLAG_NOCHNG. This will cause the sqlite3_vtab_nochange()
90884** function to return true inside the xColumn method of the virtual
90885** table implementation. The P5 column might also contain other
90886** bits (OPFLAG_LENGTHARG or OPFLAG_TYPEOFARG) but those bits are
90887** unused by OP_VColumn.
90888*/
90889case OP_VColumn164: {
90890 sqlite3_vtab *pVtab;
90891 const sqlite3_module *pModule;
90892 Mem *pDest;
90893 sqlite3_context sContext;
90894
90895 VdbeCursor *pCur = p->apCsr[pOp->p1];
90896 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
90897 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
90898 pDest = &aMem[pOp->p3];
90899 memAboutToChange(p, pDest);
90900 if( pCur->nullRow ){
90901 sqlite3VdbeMemSetNull(pDest);
90902 break;
90903 }
90904 pVtab = pCur->uc.pVCur->pVtab;
90905 pModule = pVtab->pModule;
90906 assert( pModule->xColumn )((void) (0));
90907 memset(&sContext, 0, sizeof(sContext));
90908 sContext.pOut = pDest;
90909 testcase( (pOp->p5 & OPFLAG_NOCHNG)==0 && pOp->p5!=0 );
90910 if( pOp->p5 & OPFLAG_NOCHNG0x01 ){
90911 sqlite3VdbeMemSetNull(pDest);
90912 pDest->flags = MEM_Null0x0001|MEM_Zero0x4000;
90913 pDest->u.nZero = 0;
90914 }else{
90915 MemSetTypeFlag(pDest, MEM_Null)((pDest)->flags = ((pDest)->flags&~(0xc1bf|0x4000))
|0x0001)
;
90916 }
90917 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
90918 sqlite3VtabImportErrmsg(p, pVtab);
90919 if( sContext.isError>0 ){
90920 sqlite3VdbeError(p, "%s", sqlite3_value_text(pDest));
90921 rc = sContext.isError;
90922 }
90923 sqlite3VdbeChangeEncoding(pDest, encoding);
90924 REGISTER_TRACE(pOp->p3, pDest);
90925 UPDATE_MAX_BLOBSIZE(pDest);
90926
90927 if( sqlite3VdbeMemTooBig(pDest) ){
90928 goto too_big;
90929 }
90930 if( rc ) goto abort_due_to_error;
90931 break;
90932}
90933#endif /* SQLITE_OMIT_VIRTUALTABLE */
90934
90935#ifndef SQLITE_OMIT_VIRTUALTABLE
90936/* Opcode: VNext P1 P2 * * *
90937**
90938** Advance virtual table P1 to the next row in its result set and
90939** jump to instruction P2. Or, if the virtual table has reached
90940** the end of its result set, then fall through to the next instruction.
90941*/
90942case OP_VNext60: { /* jump */
90943 sqlite3_vtab *pVtab;
90944 const sqlite3_module *pModule;
90945 int res;
90946 VdbeCursor *pCur;
90947
90948 res = 0;
90949 pCur = p->apCsr[pOp->p1];
90950 assert( pCur->eCurType==CURTYPE_VTAB )((void) (0));
90951 if( pCur->nullRow ){
90952 break;
90953 }
90954 pVtab = pCur->uc.pVCur->pVtab;
90955 pModule = pVtab->pModule;
90956 assert( pModule->xNext )((void) (0));
90957
90958 /* Invoke the xNext() method of the module. There is no way for the
90959 ** underlying implementation to return an error if one occurs during
90960 ** xNext(). Instead, if an error occurs, true is returned (indicating that
90961 ** data is available) and the error code returned when xColumn or
90962 ** some other method is next invoked on the save virtual table cursor.
90963 */
90964 rc = pModule->xNext(pCur->uc.pVCur);
90965 sqlite3VtabImportErrmsg(p, pVtab);
90966 if( rc ) goto abort_due_to_error;
90967 res = pModule->xEof(pCur->uc.pVCur);
90968 VdbeBranchTaken(!res,2);
90969 if( !res ){
90970 /* If there is data, jump to P2 */
90971 goto jump_to_p2_and_check_for_interrupt;
90972 }
90973 goto check_for_interrupt;
90974}
90975#endif /* SQLITE_OMIT_VIRTUALTABLE */
90976
90977#ifndef SQLITE_OMIT_VIRTUALTABLE
90978/* Opcode: VRename P1 * * P4 *
90979**
90980** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
90981** This opcode invokes the corresponding xRename method. The value
90982** in register P1 is passed as the zName argument to the xRename method.
90983*/
90984case OP_VRename165: {
90985 sqlite3_vtab *pVtab;
90986 Mem *pName;
90987 int isLegacy;
90988
90989 isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
90990 db->flags |= SQLITE_LegacyAlter0x04000000;
90991 pVtab = pOp->p4.pVtab->pVtab;
90992 pName = &aMem[pOp->p1];
90993 assert( pVtab->pModule->xRename )((void) (0));
90994 assert( memIsValid(pName) )((void) (0));
90995 assert( p->readOnly==0 )((void) (0));
90996 REGISTER_TRACE(pOp->p1, pName);
90997 assert( pName->flags & MEM_Str )((void) (0));
90998 testcase( pName->enc==SQLITE_UTF8 );
90999 testcase( pName->enc==SQLITE_UTF16BE );
91000 testcase( pName->enc==SQLITE_UTF16LE );
91001 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF81);
91002 if( rc ) goto abort_due_to_error;
91003 rc = pVtab->pModule->xRename(pVtab, pName->z);
91004 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter0x04000000;
91005 sqlite3VtabImportErrmsg(p, pVtab);
91006 p->expired = 0;
91007 if( rc ) goto abort_due_to_error;
91008 break;
91009}
91010#endif
91011
91012#ifndef SQLITE_OMIT_VIRTUALTABLE
91013/* Opcode: VUpdate P1 P2 P3 P4 P5
91014** Synopsis: data=r[P3@P2]
91015**
91016** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
91017** This opcode invokes the corresponding xUpdate method. P2 values
91018** are contiguous memory cells starting at P3 to pass to the xUpdate
91019** invocation. The value in register (P3+P2-1) corresponds to the
91020** p2th element of the argv array passed to xUpdate.
91021**
91022** The xUpdate method will do a DELETE or an INSERT or both.
91023** The argv[0] element (which corresponds to memory cell P3)
91024** is the rowid of a row to delete. If argv[0] is NULL then no
91025** deletion occurs. The argv[1] element is the rowid of the new
91026** row. This can be NULL to have the virtual table select the new
91027** rowid for itself. The subsequent elements in the array are
91028** the values of columns in the new row.
91029**
91030** If P2==1 then no insert is performed. argv[0] is the rowid of
91031** a row to delete.
91032**
91033** P1 is a boolean flag. If it is set to true and the xUpdate call
91034** is successful, then the value returned by sqlite3_last_insert_rowid()
91035** is set to the value of the rowid for the row just inserted.
91036**
91037** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
91038** apply in the case of a constraint failure on an insert or update.
91039*/
91040case OP_VUpdate10: {
91041 sqlite3_vtab *pVtab;
91042 const sqlite3_module *pModule;
91043 int nArg;
91044 int i;
91045 sqlite_int64 rowid;
91046 Mem **apArg;
91047 Mem *pX;
91048
91049 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback((void) (0))
91050 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace((void) (0))
91051 )((void) (0));
91052 assert( p->readOnly==0 )((void) (0));
91053 if( db->mallocFailed ) goto no_mem;
91054 sqlite3VdbeIncrWriteCounter(p, 0);
91055 pVtab = pOp->p4.pVtab->pVtab;
91056 if( pVtab==0 || NEVER(pVtab->pModule==0)(pVtab->pModule==0) ){
91057 rc = SQLITE_LOCKED6;
91058 goto abort_due_to_error;
91059 }
91060 pModule = pVtab->pModule;
91061 nArg = pOp->p2;
91062 assert( pOp->p4type==P4_VTAB )((void) (0));
91063 if( ALWAYS(pModule->xUpdate)(pModule->xUpdate) ){
91064 u8 vtabOnConflict = db->vtabOnConflict;
91065 apArg = p->apArg;
91066 pX = &aMem[pOp->p3];
91067 for(i=0; i<nArg; i++){
91068 assert( memIsValid(pX) )((void) (0));
91069 memAboutToChange(p, pX);
91070 apArg[i] = pX;
91071 pX++;
91072 }
91073 db->vtabOnConflict = pOp->p5;
91074 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
91075 db->vtabOnConflict = vtabOnConflict;
91076 sqlite3VtabImportErrmsg(p, pVtab);
91077 if( rc==SQLITE_OK0 && pOp->p1 ){
91078 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) )((void) (0));
91079 db->lastRowid = rowid;
91080 }
91081 if( (rc&0xff)==SQLITE_CONSTRAINT19 && pOp->p4.pVtab->bConstraint ){
91082 if( pOp->p5==OE_Ignore4 ){
91083 rc = SQLITE_OK0;
91084 }else{
91085 p->errorAction = ((pOp->p5==OE_Replace5) ? OE_Abort2 : pOp->p5);
91086 }
91087 }else{
91088 p->nChange++;
91089 }
91090 if( rc ) goto abort_due_to_error;
91091 }
91092 break;
91093}
91094#endif /* SQLITE_OMIT_VIRTUALTABLE */
91095
91096#ifndef SQLITE_OMIT_PAGER_PRAGMAS
91097/* Opcode: Pagecount P1 P2 * * *
91098**
91099** Write the current number of pages in database P1 to memory cell P2.
91100*/
91101case OP_Pagecount166: { /* out2 */
91102 pOut = out2Prerelease(p, pOp);
91103 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
91104 break;
91105}
91106#endif
91107
91108
91109#ifndef SQLITE_OMIT_PAGER_PRAGMAS
91110/* Opcode: MaxPgcnt P1 P2 P3 * *
91111**
91112** Try to set the maximum page count for database P1 to the value in P3.
91113** Do not let the maximum page count fall below the current page count and
91114** do not change the maximum page count value if P3==0.
91115**
91116** Store the maximum page count after the change in register P2.
91117*/
91118case OP_MaxPgcnt167: { /* out2 */
91119 unsigned int newMax;
91120 Btree *pBt;
91121
91122 pOut = out2Prerelease(p, pOp);
91123 pBt = db->aDb[pOp->p1].pBt;
91124 newMax = 0;
91125 if( pOp->p3 ){
91126 newMax = sqlite3BtreeLastPage(pBt);
91127 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
91128 }
91129 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
91130 break;
91131}
91132#endif
91133
91134/* Opcode: Function0 P1 P2 P3 P4 P5
91135** Synopsis: r[P3]=func(r[P2@P5])
91136**
91137** Invoke a user function (P4 is a pointer to a FuncDef object that
91138** defines the function) with P5 arguments taken from register P2 and
91139** successors. The result of the function is stored in register P3.
91140** Register P3 must not be one of the function inputs.
91141**
91142** P1 is a 32-bit bitmask indicating whether or not each argument to the
91143** function was determined to be constant at compile time. If the first
91144** argument was constant then bit 0 of P1 is set. This is used to determine
91145** whether meta data associated with a user function argument using the
91146** sqlite3_set_auxdata() API may be safely retained until the next
91147** invocation of this opcode.
91148**
91149** See also: Function, AggStep, AggFinal
91150*/
91151/* Opcode: Function P1 P2 P3 P4 P5
91152** Synopsis: r[P3]=func(r[P2@P5])
91153**
91154** Invoke a user function (P4 is a pointer to an sqlite3_context object that
91155** contains a pointer to the function to be run) with P5 arguments taken
91156** from register P2 and successors. The result of the function is stored
91157** in register P3. Register P3 must not be one of the function inputs.
91158**
91159** P1 is a 32-bit bitmask indicating whether or not each argument to the
91160** function was determined to be constant at compile time. If the first
91161** argument was constant then bit 0 of P1 is set. This is used to determine
91162** whether meta data associated with a user function argument using the
91163** sqlite3_set_auxdata() API may be safely retained until the next
91164** invocation of this opcode.
91165**
91166** SQL functions are initially coded as OP_Function0 with P4 pointing
91167** to a FuncDef object. But on first evaluation, the P4 operand is
91168** automatically converted into an sqlite3_context object and the operation
91169** changed to this OP_Function opcode. In this way, the initialization of
91170** the sqlite3_context object occurs only once, rather than once for each
91171** evaluation of the function.
91172**
91173** See also: Function0, AggStep, AggFinal
91174*/
91175case OP_PureFunc062: /* group */
91176case OP_Function063: { /* group */
91177 int n;
91178 sqlite3_context *pCtx;
91179
91180 assert( pOp->p4type==P4_FUNCDEF )((void) (0));
91181 n = pOp->p5;
91182 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) )((void) (0));
91183 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) )((void) (0));
91184 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n )((void) (0));
91185 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
91186 if( pCtx==0 ) goto no_mem;
91187 pCtx->pOut = 0;
91188 pCtx->pFunc = pOp->p4.pFunc;
91189 pCtx->iOp = (int)(pOp - aOp);
91190 pCtx->pVdbe = p;
91191 pCtx->isError = 0;
91192 pCtx->argc = n;
91193 pOp->p4type = P4_FUNCCTX(-16);
91194 pOp->p4.pCtx = pCtx;
91195 assert( OP_PureFunc == OP_PureFunc0+2 )((void) (0));
91196 assert( OP_Function == OP_Function0+2 )((void) (0));
91197 pOp->opcode += 2;
91198 /* Fall through into OP_Function */
91199}
91200case OP_PureFunc64: /* group */
91201case OP_Function65: { /* group */
91202 int i;
91203 sqlite3_context *pCtx;
91204
91205 assert( pOp->p4type==P4_FUNCCTX )((void) (0));
91206 pCtx = pOp->p4.pCtx;
91207
91208 /* If this function is inside of a trigger, the register array in aMem[]
91209 ** might change from one evaluation to the next. The next block of code
91210 ** checks to see if the register array has changed, and if so it
91211 ** reinitializes the relavant parts of the sqlite3_context object */
91212 pOut = &aMem[pOp->p3];
91213 if( pCtx->pOut != pOut ){
91214 pCtx->pOut = pOut;
91215 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
91216 }
91217
91218 memAboutToChange(p, pOut);
91219#ifdef SQLITE_DEBUG
91220 for(i=0; i<pCtx->argc; i++){
91221 assert( memIsValid(pCtx->argv[i]) )((void) (0));
91222 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
91223 }
91224#endif
91225 MemSetTypeFlag(pOut, MEM_Null)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0001
)
;
91226 assert( pCtx->isError==0 )((void) (0));
91227 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
91228
91229 /* If the function returned an error, throw an exception */
91230 if( pCtx->isError ){
91231 if( pCtx->isError>0 ){
91232 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
91233 rc = pCtx->isError;
91234 }
91235 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
91236 pCtx->isError = 0;
91237 if( rc ) goto abort_due_to_error;
91238 }
91239
91240 /* Copy the result of the function into register P3 */
91241 if( pOut->flags & (MEM_Str0x0002|MEM_Blob0x0010) ){
91242 sqlite3VdbeChangeEncoding(pOut, encoding);
91243 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
91244 }
91245
91246 REGISTER_TRACE(pOp->p3, pOut);
91247 UPDATE_MAX_BLOBSIZE(pOut);
91248 break;
91249}
91250
91251/* Opcode: Trace P1 P2 * P4 *
91252**
91253** Write P4 on the statement trace output if statement tracing is
91254** enabled.
91255**
91256** Operand P1 must be 0x7fffffff and P2 must positive.
91257*/
91258/* Opcode: Init P1 P2 P3 P4 *
91259** Synopsis: Start at P2
91260**
91261** Programs contain a single instance of this opcode as the very first
91262** opcode.
91263**
91264** If tracing is enabled (by the sqlite3_trace()) interface, then
91265** the UTF-8 string contained in P4 is emitted on the trace callback.
91266** Or if P4 is blank, use the string returned by sqlite3_sql().
91267**
91268** If P2 is not zero, jump to instruction P2.
91269**
91270** Increment the value of P1 so that OP_Once opcodes will jump the
91271** first time they are evaluated for this run.
91272**
91273** If P3 is not zero, then it is an address to jump to if an SQLITE_CORRUPT
91274** error is encountered.
91275*/
91276case OP_Trace168:
91277case OP_Init61: { /* jump */
91278 int i;
91279#ifndef SQLITE_OMIT_TRACE
91280 char *zTrace;
91281#endif
91282
91283 /* If the P4 argument is not NULL, then it must be an SQL comment string.
91284 ** The "--" string is broken up to prevent false-positives with srcck1.c.
91285 **
91286 ** This assert() provides evidence for:
91287 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
91288 ** would have been returned by the legacy sqlite3_trace() interface by
91289 ** using the X argument when X begins with "--" and invoking
91290 ** sqlite3_expanded_sql(P) otherwise.
91291 */
91292 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 )((void) (0));
91293
91294 /* OP_Init is always instruction 0 */
91295 assert( pOp==p->aOp || pOp->opcode==OP_Trace )((void) (0));
91296
91297#ifndef SQLITE_OMIT_TRACE
91298 if( (db->mTrace & (SQLITE_TRACE_STMT0x01|SQLITE_TRACE_LEGACY0x40))!=0
91299 && !p->doingRerun
91300 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
91301 ){
91302#ifndef SQLITE_OMIT_DEPRECATED
91303 if( db->mTrace & SQLITE_TRACE_LEGACY0x40 ){
91304 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
91305 char *z = sqlite3VdbeExpandSql(p, zTrace);
91306 x(db->pTraceArg, z);
91307 sqlite3_free(z);
91308 }else
91309#endif
91310 if( db->nVdbeExec>1 ){
91311 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
91312 (void)db->xTrace(SQLITE_TRACE_STMT0x01, db->pTraceArg, p, z);
91313 sqlite3DbFree(db, z);
91314 }else{
91315 (void)db->xTrace(SQLITE_TRACE_STMT0x01, db->pTraceArg, p, zTrace);
91316 }
91317 }
91318#ifdef SQLITE_USE_FCNTL_TRACE
91319 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
91320 if( zTrace ){
91321 int j;
91322 for(j=0; j<db->nDb; j++){
91323 if( DbMaskTest(p->btreeMask, j)(((p->btreeMask)&(((yDbMask)1)<<(j)))!=0)==0 ) continue;
91324 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE19, zTrace);
91325 }
91326 }
91327#endif /* SQLITE_USE_FCNTL_TRACE */
91328#ifdef SQLITE_DEBUG
91329 if( (db->flags & SQLITE_SqlTrace)!=0
91330 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
91331 ){
91332 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
91333 }
91334#endif /* SQLITE_DEBUG */
91335#endif /* SQLITE_OMIT_TRACE */
91336 assert( pOp->p2>0 )((void) (0));
91337 if( pOp->p1>=sqlite3GlobalConfigsqlite3Config.iOnceResetThreshold ){
91338 if( pOp->opcode==OP_Trace168 ) break;
91339 for(i=1; i<p->nOp; i++){
91340 if( p->aOp[i].opcode==OP_Once17 ) p->aOp[i].p1 = 0;
91341 }
91342 pOp->p1 = 0;
91343 }
91344 pOp->p1++;
91345 p->aCounter[SQLITE_STMTSTATUS_RUN6]++;
91346 goto jump_to_p2;
91347}
91348
91349#ifdef SQLITE_ENABLE_CURSOR_HINTS
91350/* Opcode: CursorHint P1 * * P4 *
91351**
91352** Provide a hint to cursor P1 that it only needs to return rows that
91353** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
91354** to values currently held in registers. TK_COLUMN terms in the P4
91355** expression refer to columns in the b-tree to which cursor P1 is pointing.
91356*/
91357case OP_CursorHint169: {
91358 VdbeCursor *pC;
91359
91360 assert( pOp->p1>=0 && pOp->p1<p->nCursor )((void) (0));
91361 assert( pOp->p4type==P4_EXPR )((void) (0));
91362 pC = p->apCsr[pOp->p1];
91363 if( pC ){
91364 assert( pC->eCurType==CURTYPE_BTREE )((void) (0));
91365 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE0,
91366 pOp->p4.pExpr, aMem);
91367 }
91368 break;
91369}
91370#endif /* SQLITE_ENABLE_CURSOR_HINTS */
91371
91372#ifdef SQLITE_DEBUG
91373/* Opcode: Abortable * * * * *
91374**
91375** Verify that an Abort can happen. Assert if an Abort at this point
91376** might cause database corruption. This opcode only appears in debugging
91377** builds.
91378**
91379** An Abort is safe if either there have been no writes, or if there is
91380** an active statement journal.
91381*/
91382case OP_Abortable172: {
91383 sqlite3VdbeAssertAbortable(p);
91384 break;
91385}
91386#endif
91387
91388/* Opcode: Noop * * * * *
91389**
91390** Do nothing. This instruction is often useful as a jump
91391** destination.
91392*/
91393/*
91394** The magic Explain opcode are only inserted when explain==2 (which
91395** is to say when the EXPLAIN QUERY PLAN syntax is used.)
91396** This opcode records information from the optimizer. It is the
91397** the same as a no-op. This opcodesnever appears in a real VM program.
91398*/
91399default: { /* This is really OP_Noop, OP_Explain */
91400 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain )((void) (0));
91401
91402 break;
91403}
91404
91405/*****************************************************************************
91406** The cases of the switch statement above this line should all be indented
91407** by 6 spaces. But the left-most 6 spaces have been removed to improve the
91408** readability. From this point on down, the normal indentation rules are
91409** restored.
91410*****************************************************************************/
91411 }
91412
91413#ifdef VDBE_PROFILE
91414 {
91415 u64 endTime = sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
91416 if( endTime>start ) pOrigOp->cycles += endTime - start;
91417 pOrigOp->cnt++;
91418 }
91419#endif
91420
91421 /* The following code adds nothing to the actual functionality
91422 ** of the program. It is only here for testing and debugging.
91423 ** On the other hand, it does burn CPU cycles every time through
91424 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
91425 */
91426#ifndef NDEBUG1
91427 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] )((void) (0));
91428
91429#ifdef SQLITE_DEBUG
91430 if( db->flags & SQLITE_VdbeTrace ){
91431 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
91432 if( rc!=0 ) printf("rc=%d\n",rc);
91433 if( opProperty & (OPFLG_OUT20x10) ){
91434 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
91435 }
91436 if( opProperty & OPFLG_OUT30x20 ){
91437 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
91438 }
91439 }
91440#endif /* SQLITE_DEBUG */
91441#endif /* NDEBUG */
91442 } /* The end of the for(;;) loop the loops through opcodes */
91443
91444 /* If we reach this point, it means that execution is finished with
91445 ** an error of some kind.
91446 */
91447abort_due_to_error:
91448 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT7;
91449 assert( rc )((void) (0));
91450 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
91451 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
91452 }
91453 p->rc = rc;
91454 sqlite3SystemError(db, rc);
91455 testcase( sqlite3GlobalConfig.xLog!=0 );
91456 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
91457 (int)(pOp - aOp), p->zSql, p->zErrMsg);
91458 sqlite3VdbeHalt(p);
91459 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ) sqlite3OomFault(db);
91460 rc = SQLITE_ERROR1;
91461 if( resetSchemaOnFault>0 ){
91462 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
91463 }
91464
91465 /* This is the only way out of this procedure. We have to
91466 ** release the mutexes on btrees that were acquired at the
91467 ** top. */
91468vdbe_return:
91469#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
91470 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
91471 nProgressLimit += db->nProgressOps;
91472 if( db->xProgress(db->pProgressArg) ){
91473 nProgressLimit = 0xffffffff;
91474 rc = SQLITE_INTERRUPT9;
91475 goto abort_due_to_error;
91476 }
91477 }
91478#endif
91479 p->aCounter[SQLITE_STMTSTATUS_VM_STEP4] += (int)nVmStep;
91480 sqlite3VdbeLeave(p);
91481 assert( rc!=SQLITE_OK || nExtraDelete==0((void) (0))
91482 || sqlite3_strlike("DELETE%",p->zSql,0)!=0((void) (0))
91483 )((void) (0));
91484 return rc;
91485
91486 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
91487 ** is encountered.
91488 */
91489too_big:
91490 sqlite3VdbeError(p, "string or blob too big");
91491 rc = SQLITE_TOOBIG18;
91492 goto abort_due_to_error;
91493
91494 /* Jump to here if a malloc() fails.
91495 */
91496no_mem:
91497 sqlite3OomFault(db);
91498 sqlite3VdbeError(p, "out of memory");
91499 rc = SQLITE_NOMEM_BKPT7;
91500 goto abort_due_to_error;
91501
91502 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
91503 ** flag.
91504 */
91505abort_due_to_interrupt:
91506 assert( db->u1.isInterrupted )((void) (0));
91507 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT7 : SQLITE_INTERRUPT9;
91508 p->rc = rc;
91509 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
91510 goto abort_due_to_error;
91511}
91512
91513
91514/************** End of vdbe.c ************************************************/
91515/************** Begin file vdbeblob.c ****************************************/
91516/*
91517** 2007 May 1
91518**
91519** The author disclaims copyright to this source code. In place of
91520** a legal notice, here is a blessing:
91521**
91522** May you do good and not evil.
91523** May you find forgiveness for yourself and forgive others.
91524** May you share freely, never taking more than you give.
91525**
91526*************************************************************************
91527**
91528** This file contains code used to implement incremental BLOB I/O.
91529*/
91530
91531/* #include "sqliteInt.h" */
91532/* #include "vdbeInt.h" */
91533
91534#ifndef SQLITE_OMIT_INCRBLOB
91535
91536/*
91537** Valid sqlite3_blob* handles point to Incrblob structures.
91538*/
91539typedef struct Incrblob Incrblob;
91540struct Incrblob {
91541 int nByte; /* Size of open blob, in bytes */
91542 int iOffset; /* Byte offset of blob in cursor data */
91543 u16 iCol; /* Table column this handle is open on */
91544 BtCursor *pCsr; /* Cursor pointing at blob row */
91545 sqlite3_stmt *pStmt; /* Statement holding cursor open */
91546 sqlite3 *db; /* The associated database */
91547 char *zDb; /* Database name */
91548 Table *pTab; /* Table object */
91549};
91550
91551
91552/*
91553** This function is used by both blob_open() and blob_reopen(). It seeks
91554** the b-tree cursor associated with blob handle p to point to row iRow.
91555** If successful, SQLITE_OK is returned and subsequent calls to
91556** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
91557**
91558** If an error occurs, or if the specified row does not exist or does not
91559** contain a value of type TEXT or BLOB in the column nominated when the
91560** blob handle was opened, then an error code is returned and *pzErr may
91561** be set to point to a buffer containing an error message. It is the
91562** responsibility of the caller to free the error message buffer using
91563** sqlite3DbFree().
91564**
91565** If an error does occur, then the b-tree cursor is closed. All subsequent
91566** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
91567** immediately return SQLITE_ABORT.
91568*/
91569static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
91570 int rc; /* Error code */
91571 char *zErr = 0; /* Error message */
91572 Vdbe *v = (Vdbe *)p->pStmt;
91573
91574 /* Set the value of register r[1] in the SQL statement to integer iRow.
91575 ** This is done directly as a performance optimization
91576 */
91577 v->aMem[1].flags = MEM_Int0x0004;
91578 v->aMem[1].u.i = iRow;
91579
91580 /* If the statement has been run before (and is paused at the OP_ResultRow)
91581 ** then back it up to the point where it does the OP_NotExists. This could
91582 ** have been down with an extra OP_Goto, but simply setting the program
91583 ** counter is faster. */
91584 if( v->pc>4 ){
91585 v->pc = 4;
91586 assert( v->aOp[v->pc].opcode==OP_NotExists )((void) (0));
91587 rc = sqlite3VdbeExec(v);
91588 }else{
91589 rc = sqlite3_step(p->pStmt);
91590 }
91591 if( rc==SQLITE_ROW100 ){
91592 VdbeCursor *pC = v->apCsr[0];
91593 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
91594 testcase( pC->nHdrParsed==p->iCol );
91595 testcase( pC->nHdrParsed==p->iCol+1 );
91596 if( type<12 ){
91597 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
91598 type==0?"null": type==7?"real": "integer"
91599 );
91600 rc = SQLITE_ERROR1;
91601 sqlite3_finalize(p->pStmt);
91602 p->pStmt = 0;
91603 }else{
91604 p->iOffset = pC->aType[p->iCol + pC->nField];
91605 p->nByte = sqlite3VdbeSerialTypeLen(type);
91606 p->pCsr = pC->uc.pCursor;
91607 sqlite3BtreeIncrblobCursor(p->pCsr);
91608 }
91609 }
91610
91611 if( rc==SQLITE_ROW100 ){
91612 rc = SQLITE_OK0;
91613 }else if( p->pStmt ){
91614 rc = sqlite3_finalize(p->pStmt);
91615 p->pStmt = 0;
91616 if( rc==SQLITE_OK0 ){
91617 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
91618 rc = SQLITE_ERROR1;
91619 }else{
91620 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
91621 }
91622 }
91623
91624 assert( rc!=SQLITE_OK || zErr==0 )((void) (0));
91625 assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE )((void) (0));
91626
91627 *pzErr = zErr;
91628 return rc;
91629}
91630
91631/*
91632** Open a blob handle.
91633*/
91634SQLITE_API int sqlite3_blob_open(
91635 sqlite3* db, /* The database connection */
91636 const char *zDb, /* The attached database containing the blob */
91637 const char *zTable, /* The table containing the blob */
91638 const char *zColumn, /* The column containing the blob */
91639 sqlite_int64 iRow, /* The row containing the glob */
91640 int wrFlag, /* True -> read/write access, false -> read-only */
91641 sqlite3_blob **ppBlob /* Handle for accessing the blob returned here */
91642){
91643 int nAttempt = 0;
91644 int iCol; /* Index of zColumn in row-record */
91645 int rc = SQLITE_OK0;
91646 char *zErr = 0;
91647 Table *pTab;
91648 Incrblob *pBlob = 0;
91649 Parse sParse;
91650
91651#ifdef SQLITE_ENABLE_API_ARMOR
91652 if( ppBlob==0 ){
91653 return SQLITE_MISUSE_BKPTsqlite3MisuseError(91653);
91654 }
91655#endif
91656 *ppBlob = 0;
91657#ifdef SQLITE_ENABLE_API_ARMOR
91658 if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
91659 return SQLITE_MISUSE_BKPTsqlite3MisuseError(91659);
91660 }
91661#endif
91662 wrFlag = !!wrFlag; /* wrFlag = (wrFlag ? 1 : 0); */
91663
91664 sqlite3_mutex_enter(db->mutex);
91665
91666 pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
91667 do {
91668 memset(&sParse, 0, sizeof(Parse));
91669 if( !pBlob ) goto blob_open_out;
91670 sParse.db = db;
91671 sqlite3DbFree(db, zErr);
91672 zErr = 0;
91673
91674 sqlite3BtreeEnterAll(db);
91675 pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
91676 if( pTab && IsVirtual(pTab)((pTab)->nModuleArg) ){
91677 pTab = 0;
91678 sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
91679 }
91680 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
91681 pTab = 0;
91682 sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
91683 }
91684#ifndef SQLITE_OMIT_VIEW
91685 if( pTab && pTab->pSelect ){
91686 pTab = 0;
91687 sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
91688 }
91689#endif
91690 if( !pTab ){
91691 if( sParse.zErrMsg ){
91692 sqlite3DbFree(db, zErr);
91693 zErr = sParse.zErrMsg;
91694 sParse.zErrMsg = 0;
91695 }
91696 rc = SQLITE_ERROR1;
91697 sqlite3BtreeLeaveAll(db);
91698 goto blob_open_out;
91699 }
91700 pBlob->pTab = pTab;
91701 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
91702
91703 /* Now search pTab for the exact column. */
91704 for(iCol=0; iCol<pTab->nCol; iCol++) {
91705 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
91706 break;
91707 }
91708 }
91709 if( iCol==pTab->nCol ){
91710 sqlite3DbFree(db, zErr);
91711 zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
91712 rc = SQLITE_ERROR1;
91713 sqlite3BtreeLeaveAll(db);
91714 goto blob_open_out;
91715 }
91716
91717 /* If the value is being opened for writing, check that the
91718 ** column is not indexed, and that it is not part of a foreign key.
91719 */
91720 if( wrFlag ){
91721 const char *zFault = 0;
91722 Index *pIdx;
91723#ifndef SQLITE_OMIT_FOREIGN_KEY
91724 if( db->flags&SQLITE_ForeignKeys0x00004000 ){
91725 /* Check that the column is not part of an FK child key definition. It
91726 ** is not necessary to check if it is part of a parent key, as parent
91727 ** key columns must be indexed. The check below will pick up this
91728 ** case. */
91729 FKey *pFKey;
91730 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
91731 int j;
91732 for(j=0; j<pFKey->nCol; j++){
91733 if( pFKey->aCol[j].iFrom==iCol ){
91734 zFault = "foreign key";
91735 }
91736 }
91737 }
91738 }
91739#endif
91740 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
91741 int j;
91742 for(j=0; j<pIdx->nKeyCol; j++){
91743 /* FIXME: Be smarter about indexes that use expressions */
91744 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR(-2) ){
91745 zFault = "indexed";
91746 }
91747 }
91748 }
91749 if( zFault ){
91750 sqlite3DbFree(db, zErr);
91751 zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
91752 rc = SQLITE_ERROR1;
91753 sqlite3BtreeLeaveAll(db);
91754 goto blob_open_out;
91755 }
91756 }
91757
91758 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
91759 assert( pBlob->pStmt || db->mallocFailed )((void) (0));
91760 if( pBlob->pStmt ){
91761
91762 /* This VDBE program seeks a btree cursor to the identified
91763 ** db/table/row entry. The reason for using a vdbe program instead
91764 ** of writing code to use the b-tree layer directly is that the
91765 ** vdbe program will take advantage of the various transaction,
91766 ** locking and error handling infrastructure built into the vdbe.
91767 **
91768 ** After seeking the cursor, the vdbe executes an OP_ResultRow.
91769 ** Code external to the Vdbe then "borrows" the b-tree cursor and
91770 ** uses it to implement the blob_read(), blob_write() and
91771 ** blob_bytes() functions.
91772 **
91773 ** The sqlite3_blob_close() function finalizes the vdbe program,
91774 ** which closes the b-tree cursor and (possibly) commits the
91775 ** transaction.
91776 */
91777 static const int iLn = VDBE_OFFSET_LINENO(2)0;
91778 static const VdbeOpList openBlob[] = {
91779 {OP_TableLock159, 0, 0, 0}, /* 0: Acquire a read or write lock */
91780 {OP_OpenRead108, 0, 0, 0}, /* 1: Open a cursor */
91781 /* blobSeekToRow() will initialize r[1] to the desired rowid */
91782 {OP_NotExists31, 0, 5, 1}, /* 2: Seek the cursor to rowid=r[1] */
91783 {OP_Column90, 0, 0, 1}, /* 3 */
91784 {OP_ResultRow81, 1, 0, 0}, /* 4 */
91785 {OP_Halt69, 0, 0, 0}, /* 5 */
91786 };
91787 Vdbe *v = (Vdbe *)pBlob->pStmt;
91788 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
91789 VdbeOp *aOp;
91790
91791 sqlite3VdbeAddOp4Int(v, OP_Transaction2, iDb, wrFlag,
91792 pTab->pSchema->schema_cookie,
91793 pTab->pSchema->iGeneration);
91794 sqlite3VdbeChangeP5(v, 1);
91795 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed )((void) (0));
91796 aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob)((int)(sizeof(openBlob)/sizeof(openBlob[0]))), openBlob, iLn);
91797
91798 /* Make sure a mutex is held on the table to be accessed */
91799 sqlite3VdbeUsesBtree(v, iDb);
91800
91801 if( db->mallocFailed==0 ){
91802 assert( aOp!=0 )((void) (0));
91803 /* Configure the OP_TableLock instruction */
91804#ifdef SQLITE_OMIT_SHARED_CACHE
91805 aOp[0].opcode = OP_Noop170;
91806#else
91807 aOp[0].p1 = iDb;
91808 aOp[0].p2 = pTab->tnum;
91809 aOp[0].p3 = wrFlag;
91810 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT0);
91811 }
91812 if( db->mallocFailed==0 ){
91813#endif
91814
91815 /* Remove either the OP_OpenWrite or OpenRead. Set the P2
91816 ** parameter of the other to pTab->tnum. */
91817 if( wrFlag ) aOp[1].opcode = OP_OpenWrite109;
91818 aOp[1].p2 = pTab->tnum;
91819 aOp[1].p3 = iDb;
91820
91821 /* Configure the number of columns. Configure the cursor to
91822 ** think that the table has one more column than it really
91823 ** does. An OP_Column to retrieve this imaginary column will
91824 ** always return an SQL NULL. This is useful because it means
91825 ** we can invoke OP_Column to fill in the vdbe cursors type
91826 ** and offset cache without causing any IO.
91827 */
91828 aOp[1].p4type = P4_INT32(-3);
91829 aOp[1].p4.i = pTab->nCol+1;
91830 aOp[3].p2 = pTab->nCol;
91831
91832 sParse.nVar = 0;
91833 sParse.nMem = 1;
91834 sParse.nTab = 1;
91835 sqlite3VdbeMakeReady(v, &sParse);
91836 }
91837 }
91838
91839 pBlob->iCol = iCol;
91840 pBlob->db = db;
91841 sqlite3BtreeLeaveAll(db);
91842 if( db->mallocFailed ){
91843 goto blob_open_out;
91844 }
91845 rc = blobSeekToRow(pBlob, iRow, &zErr);
91846 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY50 && rc==SQLITE_SCHEMA17 );
91847
91848blob_open_out:
91849 if( rc==SQLITE_OK0 && db->mallocFailed==0 ){
91850 *ppBlob = (sqlite3_blob *)pBlob;
91851 }else{
91852 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
91853 sqlite3DbFree(db, pBlob);
91854 }
91855 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
91856 sqlite3DbFree(db, zErr);
91857 sqlite3ParserReset(&sParse);
91858 rc = sqlite3ApiExit(db, rc);
91859 sqlite3_mutex_leave(db->mutex);
91860 return rc;
91861}
91862
91863/*
91864** Close a blob handle that was previously created using
91865** sqlite3_blob_open().
91866*/
91867SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
91868 Incrblob *p = (Incrblob *)pBlob;
91869 int rc;
91870 sqlite3 *db;
91871
91872 if( p ){
91873 db = p->db;
91874 sqlite3_mutex_enter(db->mutex);
91875 rc = sqlite3_finalize(p->pStmt);
91876 sqlite3DbFree(db, p);
91877 sqlite3_mutex_leave(db->mutex);
91878 }else{
91879 rc = SQLITE_OK0;
91880 }
91881 return rc;
91882}
91883
91884/*
91885** Perform a read or write operation on a blob
91886*/
91887static int blobReadWrite(
91888 sqlite3_blob *pBlob,
91889 void *z,
91890 int n,
91891 int iOffset,
91892 int (*xCall)(BtCursor*, u32, u32, void*)
91893){
91894 int rc;
91895 Incrblob *p = (Incrblob *)pBlob;
91896 Vdbe *v;
91897 sqlite3 *db;
91898
91899 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(91899);
91900 db = p->db;
91901 sqlite3_mutex_enter(db->mutex);
91902 v = (Vdbe*)p->pStmt;
91903
91904 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
91905 /* Request is out of range. Return a transient error. */
91906 rc = SQLITE_ERROR1;
91907 }else if( v==0 ){
91908 /* If there is no statement handle, then the blob-handle has
91909 ** already been invalidated. Return SQLITE_ABORT in this case.
91910 */
91911 rc = SQLITE_ABORT4;
91912 }else{
91913 /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
91914 ** returned, clean-up the statement handle.
91915 */
91916 assert( db == v->db )((void) (0));
91917 sqlite3BtreeEnterCursor(p->pCsr);
91918
91919#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
91920 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
91921 /* If a pre-update hook is registered and this is a write cursor,
91922 ** invoke it here.
91923 **
91924 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
91925 ** operation should really be an SQLITE_UPDATE. This is probably
91926 ** incorrect, but is convenient because at this point the new.* values
91927 ** are not easily obtainable. And for the sessions module, an
91928 ** SQLITE_UPDATE where the PK columns do not change is handled in the
91929 ** same way as an SQLITE_DELETE (the SQLITE_DELETE code is actually
91930 ** slightly more efficient). Since you cannot write to a PK column
91931 ** using the incremental-blob API, this works. For the sessions module
91932 ** anyhow.
91933 */
91934 sqlite3_int64 iKey;
91935 iKey = sqlite3BtreeIntegerKey(p->pCsr);
91936 sqlite3VdbePreUpdateHook(
91937 v, v->apCsr[0], SQLITE_DELETE9, p->zDb, p->pTab, iKey, -1
91938 );
91939 }
91940#endif
91941
91942 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
91943 sqlite3BtreeLeaveCursor(p->pCsr);
91944 if( rc==SQLITE_ABORT4 ){
91945 sqlite3VdbeFinalize(v);
91946 p->pStmt = 0;
91947 }else{
91948 v->rc = rc;
91949 }
91950 }
91951 sqlite3Error(db, rc);
91952 rc = sqlite3ApiExit(db, rc);
91953 sqlite3_mutex_leave(db->mutex);
91954 return rc;
91955}
91956
91957/*
91958** Read data from a blob handle.
91959*/
91960SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
91961 return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreePayloadChecked);
91962}
91963
91964/*
91965** Write data to a blob handle.
91966*/
91967SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
91968 return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
91969}
91970
91971/*
91972** Query a blob handle for the size of the data.
91973**
91974** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
91975** so no mutex is required for access.
91976*/
91977SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
91978 Incrblob *p = (Incrblob *)pBlob;
91979 return (p && p->pStmt) ? p->nByte : 0;
91980}
91981
91982/*
91983** Move an existing blob handle to point to a different row of the same
91984** database table.
91985**
91986** If an error occurs, or if the specified row does not exist or does not
91987** contain a blob or text value, then an error code is returned and the
91988** database handle error code and message set. If this happens, then all
91989** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
91990** immediately return SQLITE_ABORT.
91991*/
91992SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
91993 int rc;
91994 Incrblob *p = (Incrblob *)pBlob;
91995 sqlite3 *db;
91996
91997 if( p==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(91997);
91998 db = p->db;
91999 sqlite3_mutex_enter(db->mutex);
92000
92001 if( p->pStmt==0 ){
92002 /* If there is no statement handle, then the blob-handle has
92003 ** already been invalidated. Return SQLITE_ABORT in this case.
92004 */
92005 rc = SQLITE_ABORT4;
92006 }else{
92007 char *zErr;
92008 rc = blobSeekToRow(p, iRow, &zErr);
92009 if( rc!=SQLITE_OK0 ){
92010 sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
92011 sqlite3DbFree(db, zErr);
92012 }
92013 assert( rc!=SQLITE_SCHEMA )((void) (0));
92014 }
92015
92016 rc = sqlite3ApiExit(db, rc);
92017 assert( rc==SQLITE_OK || p->pStmt==0 )((void) (0));
92018 sqlite3_mutex_leave(db->mutex);
92019 return rc;
92020}
92021
92022#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
92023
92024/************** End of vdbeblob.c ********************************************/
92025/************** Begin file vdbesort.c ****************************************/
92026/*
92027** 2011-07-09
92028**
92029** The author disclaims copyright to this source code. In place of
92030** a legal notice, here is a blessing:
92031**
92032** May you do good and not evil.
92033** May you find forgiveness for yourself and forgive others.
92034** May you share freely, never taking more than you give.
92035**
92036*************************************************************************
92037** This file contains code for the VdbeSorter object, used in concert with
92038** a VdbeCursor to sort large numbers of keys for CREATE INDEX statements
92039** or by SELECT statements with ORDER BY clauses that cannot be satisfied
92040** using indexes and without LIMIT clauses.
92041**
92042** The VdbeSorter object implements a multi-threaded external merge sort
92043** algorithm that is efficient even if the number of elements being sorted
92044** exceeds the available memory.
92045**
92046** Here is the (internal, non-API) interface between this module and the
92047** rest of the SQLite system:
92048**
92049** sqlite3VdbeSorterInit() Create a new VdbeSorter object.
92050**
92051** sqlite3VdbeSorterWrite() Add a single new row to the VdbeSorter
92052** object. The row is a binary blob in the
92053** OP_MakeRecord format that contains both
92054** the ORDER BY key columns and result columns
92055** in the case of a SELECT w/ ORDER BY, or
92056** the complete record for an index entry
92057** in the case of a CREATE INDEX.
92058**
92059** sqlite3VdbeSorterRewind() Sort all content previously added.
92060** Position the read cursor on the
92061** first sorted element.
92062**
92063** sqlite3VdbeSorterNext() Advance the read cursor to the next sorted
92064** element.
92065**
92066** sqlite3VdbeSorterRowkey() Return the complete binary blob for the
92067** row currently under the read cursor.
92068**
92069** sqlite3VdbeSorterCompare() Compare the binary blob for the row
92070** currently under the read cursor against
92071** another binary blob X and report if
92072** X is strictly less than the read cursor.
92073** Used to enforce uniqueness in a
92074** CREATE UNIQUE INDEX statement.
92075**
92076** sqlite3VdbeSorterClose() Close the VdbeSorter object and reclaim
92077** all resources.
92078**
92079** sqlite3VdbeSorterReset() Refurbish the VdbeSorter for reuse. This
92080** is like Close() followed by Init() only
92081** much faster.
92082**
92083** The interfaces above must be called in a particular order. Write() can
92084** only occur in between Init()/Reset() and Rewind(). Next(), Rowkey(), and
92085** Compare() can only occur in between Rewind() and Close()/Reset(). i.e.
92086**
92087** Init()
92088** for each record: Write()
92089** Rewind()
92090** Rowkey()/Compare()
92091** Next()
92092** Close()
92093**
92094** Algorithm:
92095**
92096** Records passed to the sorter via calls to Write() are initially held
92097** unsorted in main memory. Assuming the amount of memory used never exceeds
92098** a threshold, when Rewind() is called the set of records is sorted using
92099** an in-memory merge sort. In this case, no temporary files are required
92100** and subsequent calls to Rowkey(), Next() and Compare() read records
92101** directly from main memory.
92102**
92103** If the amount of space used to store records in main memory exceeds the
92104** threshold, then the set of records currently in memory are sorted and
92105** written to a temporary file in "Packed Memory Array" (PMA) format.
92106** A PMA created at this point is known as a "level-0 PMA". Higher levels
92107** of PMAs may be created by merging existing PMAs together - for example
92108** merging two or more level-0 PMAs together creates a level-1 PMA.
92109**
92110** The threshold for the amount of main memory to use before flushing
92111** records to a PMA is roughly the same as the limit configured for the
92112** page-cache of the main database. Specifically, the threshold is set to
92113** the value returned by "PRAGMA main.page_size" multipled by
92114** that returned by "PRAGMA main.cache_size", in bytes.
92115**
92116** If the sorter is running in single-threaded mode, then all PMAs generated
92117** are appended to a single temporary file. Or, if the sorter is running in
92118** multi-threaded mode then up to (N+1) temporary files may be opened, where
92119** N is the configured number of worker threads. In this case, instead of
92120** sorting the records and writing the PMA to a temporary file itself, the
92121** calling thread usually launches a worker thread to do so. Except, if
92122** there are already N worker threads running, the main thread does the work
92123** itself.
92124**
92125** The sorter is running in multi-threaded mode if (a) the library was built
92126** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
92127** than zero, and (b) worker threads have been enabled at runtime by calling
92128** "PRAGMA threads=N" with some value of N greater than 0.
92129**
92130** When Rewind() is called, any data remaining in memory is flushed to a
92131** final PMA. So at this point the data is stored in some number of sorted
92132** PMAs within temporary files on disk.
92133**
92134** If there are fewer than SORTER_MAX_MERGE_COUNT PMAs in total and the
92135** sorter is running in single-threaded mode, then these PMAs are merged
92136** incrementally as keys are retreived from the sorter by the VDBE. The
92137** MergeEngine object, described in further detail below, performs this
92138** merge.
92139**
92140** Or, if running in multi-threaded mode, then a background thread is
92141** launched to merge the existing PMAs. Once the background thread has
92142** merged T bytes of data into a single sorted PMA, the main thread
92143** begins reading keys from that PMA while the background thread proceeds
92144** with merging the next T bytes of data. And so on.
92145**
92146** Parameter T is set to half the value of the memory threshold used
92147** by Write() above to determine when to create a new PMA.
92148**
92149** If there are more than SORTER_MAX_MERGE_COUNT PMAs in total when
92150** Rewind() is called, then a hierarchy of incremental-merges is used.
92151** First, T bytes of data from the first SORTER_MAX_MERGE_COUNT PMAs on
92152** disk are merged together. Then T bytes of data from the second set, and
92153** so on, such that no operation ever merges more than SORTER_MAX_MERGE_COUNT
92154** PMAs at a time. This done is to improve locality.
92155**
92156** If running in multi-threaded mode and there are more than
92157** SORTER_MAX_MERGE_COUNT PMAs on disk when Rewind() is called, then more
92158** than one background thread may be created. Specifically, there may be
92159** one background thread for each temporary file on disk, and one background
92160** thread to merge the output of each of the others to a single PMA for
92161** the main thread to read from.
92162*/
92163/* #include "sqliteInt.h" */
92164/* #include "vdbeInt.h" */
92165
92166/*
92167** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
92168** messages to stderr that may be helpful in understanding the performance
92169** characteristics of the sorter in multi-threaded mode.
92170*/
92171#if 0
92172# define SQLITE_DEBUG_SORTER_THREADS 1
92173#endif
92174
92175/*
92176** Hard-coded maximum amount of data to accumulate in memory before flushing
92177** to a level 0 PMA. The purpose of this limit is to prevent various integer
92178** overflows. 512MiB.
92179*/
92180#define SQLITE_MAX_PMASZ(1<<29) (1<<29)
92181
92182/*
92183** Private objects used by the sorter
92184*/
92185typedef struct MergeEngine MergeEngine; /* Merge PMAs together */
92186typedef struct PmaReader PmaReader; /* Incrementally read one PMA */
92187typedef struct PmaWriter PmaWriter; /* Incrementally write one PMA */
92188typedef struct SorterRecord SorterRecord; /* A record being sorted */
92189typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
92190typedef struct SorterFile SorterFile; /* Temporary file object wrapper */
92191typedef struct SorterList SorterList; /* In-memory list of records */
92192typedef struct IncrMerger IncrMerger; /* Read & merge multiple PMAs */
92193
92194/*
92195** A container for a temp file handle and the current amount of data
92196** stored in the file.
92197*/
92198struct SorterFile {
92199 sqlite3_file *pFd; /* File handle */
92200 i64 iEof; /* Bytes of data stored in pFd */
92201};
92202
92203/*
92204** An in-memory list of objects to be sorted.
92205**
92206** If aMemory==0 then each object is allocated separately and the objects
92207** are connected using SorterRecord.u.pNext. If aMemory!=0 then all objects
92208** are stored in the aMemory[] bulk memory, one right after the other, and
92209** are connected using SorterRecord.u.iNext.
92210*/
92211struct SorterList {
92212 SorterRecord *pList; /* Linked list of records */
92213 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
92214 int szPMA; /* Size of pList as PMA in bytes */
92215};
92216
92217/*
92218** The MergeEngine object is used to combine two or more smaller PMAs into
92219** one big PMA using a merge operation. Separate PMAs all need to be
92220** combined into one big PMA in order to be able to step through the sorted
92221** records in order.
92222**
92223** The aReadr[] array contains a PmaReader object for each of the PMAs being
92224** merged. An aReadr[] object either points to a valid key or else is at EOF.
92225** ("EOF" means "End Of File". When aReadr[] is at EOF there is no more data.)
92226** For the purposes of the paragraphs below, we assume that the array is
92227** actually N elements in size, where N is the smallest power of 2 greater
92228** to or equal to the number of PMAs being merged. The extra aReadr[] elements
92229** are treated as if they are empty (always at EOF).
92230**
92231** The aTree[] array is also N elements in size. The value of N is stored in
92232** the MergeEngine.nTree variable.
92233**
92234** The final (N/2) elements of aTree[] contain the results of comparing
92235** pairs of PMA keys together. Element i contains the result of
92236** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
92237** aTree element is set to the index of it.
92238**
92239** For the purposes of this comparison, EOF is considered greater than any
92240** other key value. If the keys are equal (only possible with two EOF
92241** values), it doesn't matter which index is stored.
92242**
92243** The (N/4) elements of aTree[] that precede the final (N/2) described
92244** above contains the index of the smallest of each block of 4 PmaReaders
92245** And so on. So that aTree[1] contains the index of the PmaReader that
92246** currently points to the smallest key value. aTree[0] is unused.
92247**
92248** Example:
92249**
92250** aReadr[0] -> Banana
92251** aReadr[1] -> Feijoa
92252** aReadr[2] -> Elderberry
92253** aReadr[3] -> Currant
92254** aReadr[4] -> Grapefruit
92255** aReadr[5] -> Apple
92256** aReadr[6] -> Durian
92257** aReadr[7] -> EOF
92258**
92259** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
92260**
92261** The current element is "Apple" (the value of the key indicated by
92262** PmaReader 5). When the Next() operation is invoked, PmaReader 5 will
92263** be advanced to the next key in its segment. Say the next key is
92264** "Eggplant":
92265**
92266** aReadr[5] -> Eggplant
92267**
92268** The contents of aTree[] are updated first by comparing the new PmaReader
92269** 5 key to the current key of PmaReader 4 (still "Grapefruit"). The PmaReader
92270** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
92271** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
92272** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
92273** so the value written into element 1 of the array is 0. As follows:
92274**
92275** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
92276**
92277** In other words, each time we advance to the next sorter element, log2(N)
92278** key comparison operations are required, where N is the number of segments
92279** being merged (rounded up to the next power of 2).
92280*/
92281struct MergeEngine {
92282 int nTree; /* Used size of aTree/aReadr (power of 2) */
92283 SortSubtask *pTask; /* Used by this thread only */
92284 int *aTree; /* Current state of incremental merge */
92285 PmaReader *aReadr; /* Array of PmaReaders to merge data from */
92286};
92287
92288/*
92289** This object represents a single thread of control in a sort operation.
92290** Exactly VdbeSorter.nTask instances of this object are allocated
92291** as part of each VdbeSorter object. Instances are never allocated any
92292** other way. VdbeSorter.nTask is set to the number of worker threads allowed
92293** (see SQLITE_CONFIG_WORKER_THREADS) plus one (the main thread). Thus for
92294** single-threaded operation, there is exactly one instance of this object
92295** and for multi-threaded operation there are two or more instances.
92296**
92297** Essentially, this structure contains all those fields of the VdbeSorter
92298** structure for which each thread requires a separate instance. For example,
92299** each thread requries its own UnpackedRecord object to unpack records in
92300** as part of comparison operations.
92301**
92302** Before a background thread is launched, variable bDone is set to 0. Then,
92303** right before it exits, the thread itself sets bDone to 1. This is used for
92304** two purposes:
92305**
92306** 1. When flushing the contents of memory to a level-0 PMA on disk, to
92307** attempt to select a SortSubtask for which there is not already an
92308** active background thread (since doing so causes the main thread
92309** to block until it finishes).
92310**
92311** 2. If SQLITE_DEBUG_SORTER_THREADS is defined, to determine if a call
92312** to sqlite3ThreadJoin() is likely to block. Cases that are likely to
92313** block provoke debugging output.
92314**
92315** In both cases, the effects of the main thread seeing (bDone==0) even
92316** after the thread has finished are not dire. So we don't worry about
92317** memory barriers and such here.
92318*/
92319typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int);
92320struct SortSubtask {
92321 SQLiteThread *pThread; /* Background thread, if any */
92322 int bDone; /* Set if thread is finished but not joined */
92323 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
92324 UnpackedRecord *pUnpacked; /* Space to unpack a record */
92325 SorterList list; /* List for thread to write to a PMA */
92326 int nPMA; /* Number of PMAs currently in file */
92327 SorterCompare xCompare; /* Compare function to use */
92328 SorterFile file; /* Temp file for level-0 PMAs */
92329 SorterFile file2; /* Space for other PMAs */
92330};
92331
92332
92333/*
92334** Main sorter structure. A single instance of this is allocated for each
92335** sorter cursor created by the VDBE.
92336**
92337** mxKeysize:
92338** As records are added to the sorter by calls to sqlite3VdbeSorterWrite(),
92339** this variable is updated so as to be set to the size on disk of the
92340** largest record in the sorter.
92341*/
92342struct VdbeSorter {
92343 int mnPmaSize; /* Minimum PMA size, in bytes */
92344 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
92345 int mxKeysize; /* Largest serialized key seen so far */
92346 int pgsz; /* Main database page size */
92347 PmaReader *pReader; /* Readr data from here after Rewind() */
92348 MergeEngine *pMerger; /* Or here, if bUseThreads==0 */
92349 sqlite3 *db; /* Database connection */
92350 KeyInfo *pKeyInfo; /* How to compare records */
92351 UnpackedRecord *pUnpacked; /* Used by VdbeSorterCompare() */
92352 SorterList list; /* List of in-memory records */
92353 int iMemory; /* Offset of free space in list.aMemory */
92354 int nMemory; /* Size of list.aMemory allocation in bytes */
92355 u8 bUsePMA; /* True if one or more PMAs created */
92356 u8 bUseThreads; /* True to use background threads */
92357 u8 iPrev; /* Previous thread used to flush PMA */
92358 u8 nTask; /* Size of aTask[] array */
92359 u8 typeMask;
92360 SortSubtask aTask[1]; /* One or more subtasks */
92361};
92362
92363#define SORTER_TYPE_INTEGER0x01 0x01
92364#define SORTER_TYPE_TEXT0x02 0x02
92365
92366/*
92367** An instance of the following object is used to read records out of a
92368** PMA, in sorted order. The next key to be read is cached in nKey/aKey.
92369** aKey might point into aMap or into aBuffer. If neither of those locations
92370** contain a contiguous representation of the key, then aAlloc is allocated
92371** and the key is copied into aAlloc and aKey is made to poitn to aAlloc.
92372**
92373** pFd==0 at EOF.
92374*/
92375struct PmaReader {
92376 i64 iReadOff; /* Current read offset */
92377 i64 iEof; /* 1 byte past EOF for this PmaReader */
92378 int nAlloc; /* Bytes of space at aAlloc */
92379 int nKey; /* Number of bytes in key */
92380 sqlite3_file *pFd; /* File handle we are reading from */
92381 u8 *aAlloc; /* Space for aKey if aBuffer and pMap wont work */
92382 u8 *aKey; /* Pointer to current key */
92383 u8 *aBuffer; /* Current read buffer */
92384 int nBuffer; /* Size of read buffer in bytes */
92385 u8 *aMap; /* Pointer to mapping of entire file */
92386 IncrMerger *pIncr; /* Incremental merger */
92387};
92388
92389/*
92390** Normally, a PmaReader object iterates through an existing PMA stored
92391** within a temp file. However, if the PmaReader.pIncr variable points to
92392** an object of the following type, it may be used to iterate/merge through
92393** multiple PMAs simultaneously.
92394**
92395** There are two types of IncrMerger object - single (bUseThread==0) and
92396** multi-threaded (bUseThread==1).
92397**
92398** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
92399** and aFile[1]. Neither file is allowed to grow to more than mxSz bytes in
92400** size. When the IncrMerger is initialized, it reads enough data from
92401** pMerger to populate aFile[0]. It then sets variables within the
92402** corresponding PmaReader object to read from that file and kicks off
92403** a background thread to populate aFile[1] with the next mxSz bytes of
92404** sorted record data from pMerger.
92405**
92406** When the PmaReader reaches the end of aFile[0], it blocks until the
92407** background thread has finished populating aFile[1]. It then exchanges
92408** the contents of the aFile[0] and aFile[1] variables within this structure,
92409** sets the PmaReader fields to read from the new aFile[0] and kicks off
92410** another background thread to populate the new aFile[1]. And so on, until
92411** the contents of pMerger are exhausted.
92412**
92413** A single-threaded IncrMerger does not open any temporary files of its
92414** own. Instead, it has exclusive access to mxSz bytes of space beginning
92415** at offset iStartOff of file pTask->file2. And instead of using a
92416** background thread to prepare data for the PmaReader, with a single
92417** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
92418** keys from pMerger by the calling thread whenever the PmaReader runs out
92419** of data.
92420*/
92421struct IncrMerger {
92422 SortSubtask *pTask; /* Task that owns this merger */
92423 MergeEngine *pMerger; /* Merge engine thread reads data from */
92424 i64 iStartOff; /* Offset to start writing file at */
92425 int mxSz; /* Maximum bytes of data to store */
92426 int bEof; /* Set to true when merge is finished */
92427 int bUseThread; /* True to use a bg thread for this object */
92428 SorterFile aFile[2]; /* aFile[0] for reading, [1] for writing */
92429};
92430
92431/*
92432** An instance of this object is used for writing a PMA.
92433**
92434** The PMA is written one record at a time. Each record is of an arbitrary
92435** size. But I/O is more efficient if it occurs in page-sized blocks where
92436** each block is aligned on a page boundary. This object caches writes to
92437** the PMA so that aligned, page-size blocks are written.
92438*/
92439struct PmaWriter {
92440 int eFWErr; /* Non-zero if in an error state */
92441 u8 *aBuffer; /* Pointer to write buffer */
92442 int nBuffer; /* Size of write buffer in bytes */
92443 int iBufStart; /* First byte of buffer to write */
92444 int iBufEnd; /* Last byte of buffer to write */
92445 i64 iWriteOff; /* Offset of start of buffer in file */
92446 sqlite3_file *pFd; /* File handle to write to */
92447};
92448
92449/*
92450** This object is the header on a single record while that record is being
92451** held in memory and prior to being written out as part of a PMA.
92452**
92453** How the linked list is connected depends on how memory is being managed
92454** by this module. If using a separate allocation for each in-memory record
92455** (VdbeSorter.list.aMemory==0), then the list is always connected using the
92456** SorterRecord.u.pNext pointers.
92457**
92458** Or, if using the single large allocation method (VdbeSorter.list.aMemory!=0),
92459** then while records are being accumulated the list is linked using the
92460** SorterRecord.u.iNext offset. This is because the aMemory[] array may
92461** be sqlite3Realloc()ed while records are being accumulated. Once the VM
92462** has finished passing records to the sorter, or when the in-memory buffer
92463** is full, the list is sorted. As part of the sorting process, it is
92464** converted to use the SorterRecord.u.pNext pointers. See function
92465** vdbeSorterSort() for details.
92466*/
92467struct SorterRecord {
92468 int nVal; /* Size of the record in bytes */
92469 union {
92470 SorterRecord *pNext; /* Pointer to next record in list */
92471 int iNext; /* Offset within aMemory of next record */
92472 } u;
92473 /* The data for the record immediately follows this header */
92474};
92475
92476/* Return a pointer to the buffer containing the record data for SorterRecord
92477** object p. Should be used as if:
92478**
92479** void *SRVAL(SorterRecord *p) { return (void*)&p[1]; }
92480*/
92481#define SRVAL(p)((void*)((SorterRecord*)(p) + 1)) ((void*)((SorterRecord*)(p) + 1))
92482
92483
92484/* Maximum number of PMAs that a single MergeEngine can merge */
92485#define SORTER_MAX_MERGE_COUNT16 16
92486
92487static int vdbeIncrSwap(IncrMerger*);
92488static void vdbeIncrFree(IncrMerger *);
92489
92490/*
92491** Free all memory belonging to the PmaReader object passed as the
92492** argument. All structure fields are set to zero before returning.
92493*/
92494static void vdbePmaReaderClear(PmaReader *pReadr){
92495 sqlite3_free(pReadr->aAlloc);
92496 sqlite3_free(pReadr->aBuffer);
92497 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
92498 vdbeIncrFree(pReadr->pIncr);
92499 memset(pReadr, 0, sizeof(PmaReader));
92500}
92501
92502/*
92503** Read the next nByte bytes of data from the PMA p.
92504** If successful, set *ppOut to point to a buffer containing the data
92505** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
92506** error code.
92507**
92508** The buffer returned in *ppOut is only valid until the
92509** next call to this function.
92510*/
92511static int vdbePmaReadBlob(
92512 PmaReader *p, /* PmaReader from which to take the blob */
92513 int nByte, /* Bytes of data to read */
92514 u8 **ppOut /* OUT: Pointer to buffer containing data */
92515){
92516 int iBuf; /* Offset within buffer to read from */
92517 int nAvail; /* Bytes of data available in buffer */
92518
92519 if( p->aMap ){
92520 *ppOut = &p->aMap[p->iReadOff];
92521 p->iReadOff += nByte;
92522 return SQLITE_OK0;
92523 }
92524
92525 assert( p->aBuffer )((void) (0));
92526
92527 /* If there is no more data to be read from the buffer, read the next
92528 ** p->nBuffer bytes of data from the file into it. Or, if there are less
92529 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
92530 iBuf = p->iReadOff % p->nBuffer;
92531 if( iBuf==0 ){
92532 int nRead; /* Bytes to read from disk */
92533 int rc; /* sqlite3OsRead() return code */
92534
92535 /* Determine how many bytes of data to read. */
92536 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
92537 nRead = p->nBuffer;
92538 }else{
92539 nRead = (int)(p->iEof - p->iReadOff);
92540 }
92541 assert( nRead>0 )((void) (0));
92542
92543 /* Readr data from the file. Return early if an error occurs. */
92544 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
92545 assert( rc!=SQLITE_IOERR_SHORT_READ )((void) (0));
92546 if( rc!=SQLITE_OK0 ) return rc;
92547 }
92548 nAvail = p->nBuffer - iBuf;
92549
92550 if( nByte<=nAvail ){
92551 /* The requested data is available in the in-memory buffer. In this
92552 ** case there is no need to make a copy of the data, just return a
92553 ** pointer into the buffer to the caller. */
92554 *ppOut = &p->aBuffer[iBuf];
92555 p->iReadOff += nByte;
92556 }else{
92557 /* The requested data is not all available in the in-memory buffer.
92558 ** In this case, allocate space at p->aAlloc[] to copy the requested
92559 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
92560 int nRem; /* Bytes remaining to copy */
92561
92562 /* Extend the p->aAlloc[] allocation if required. */
92563 if( p->nAlloc<nByte ){
92564 u8 *aNew;
92565 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc)((128)>(2*(sqlite3_int64)p->nAlloc)?(128):(2*(sqlite3_int64
)p->nAlloc))
;
92566 while( nByte>nNew ) nNew = nNew*2;
92567 aNew = sqlite3Realloc(p->aAlloc, nNew);
92568 if( !aNew ) return SQLITE_NOMEM_BKPT7;
92569 p->nAlloc = nNew;
92570 p->aAlloc = aNew;
92571 }
92572
92573 /* Copy as much data as is available in the buffer into the start of
92574 ** p->aAlloc[]. */
92575 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
92576 p->iReadOff += nAvail;
92577 nRem = nByte - nAvail;
92578
92579 /* The following loop copies up to p->nBuffer bytes per iteration into
92580 ** the p->aAlloc[] buffer. */
92581 while( nRem>0 ){
92582 int rc; /* vdbePmaReadBlob() return code */
92583 int nCopy; /* Number of bytes to copy */
92584 u8 *aNext; /* Pointer to buffer to copy data from */
92585
92586 nCopy = nRem;
92587 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
92588 rc = vdbePmaReadBlob(p, nCopy, &aNext);
92589 if( rc!=SQLITE_OK0 ) return rc;
92590 assert( aNext!=p->aAlloc )((void) (0));
92591 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
92592 nRem -= nCopy;
92593 }
92594
92595 *ppOut = p->aAlloc;
92596 }
92597
92598 return SQLITE_OK0;
92599}
92600
92601/*
92602** Read a varint from the stream of data accessed by p. Set *pnOut to
92603** the value read.
92604*/
92605static int vdbePmaReadVarint(PmaReader *p, u64 *pnOut){
92606 int iBuf;
92607
92608 if( p->aMap ){
92609 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
92610 }else{
92611 iBuf = p->iReadOff % p->nBuffer;
92612 if( iBuf && (p->nBuffer-iBuf)>=9 ){
92613 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
92614 }else{
92615 u8 aVarint[16], *a;
92616 int i = 0, rc;
92617 do{
92618 rc = vdbePmaReadBlob(p, 1, &a);
92619 if( rc ) return rc;
92620 aVarint[(i++)&0xf] = a[0];
92621 }while( (a[0]&0x80)!=0 );
92622 sqlite3GetVarint(aVarint, pnOut);
92623 }
92624 }
92625
92626 return SQLITE_OK0;
92627}
92628
92629/*
92630** Attempt to memory map file pFile. If successful, set *pp to point to the
92631** new mapping and return SQLITE_OK. If the mapping is not attempted
92632** (because the file is too large or the VFS layer is configured not to use
92633** mmap), return SQLITE_OK and set *pp to NULL.
92634**
92635** Or, if an error occurs, return an SQLite error code. The final value of
92636** *pp is undefined in this case.
92637*/
92638static int vdbeSorterMapFile(SortSubtask *pTask, SorterFile *pFile, u8 **pp){
92639 int rc = SQLITE_OK0;
92640 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
92641 sqlite3_file *pFd = pFile->pFd;
92642 if( pFd->pMethods->iVersion>=3 ){
92643 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
92644 testcase( rc!=SQLITE_OK );
92645 }
92646 }
92647 return rc;
92648}
92649
92650/*
92651** Attach PmaReader pReadr to file pFile (if it is not already attached to
92652** that file) and seek it to offset iOff within the file. Return SQLITE_OK
92653** if successful, or an SQLite error code if an error occurs.
92654*/
92655static int vdbePmaReaderSeek(
92656 SortSubtask *pTask, /* Task context */
92657 PmaReader *pReadr, /* Reader whose cursor is to be moved */
92658 SorterFile *pFile, /* Sorter file to read from */
92659 i64 iOff /* Offset in pFile */
92660){
92661 int rc = SQLITE_OK0;
92662
92663 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 )((void) (0));
92664
92665 if( sqlite3FaultSim(201) ) return SQLITE_IOERR_READ(10 | (1<<8));
92666 if( pReadr->aMap ){
92667 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
92668 pReadr->aMap = 0;
92669 }
92670 pReadr->iReadOff = iOff;
92671 pReadr->iEof = pFile->iEof;
92672 pReadr->pFd = pFile->pFd;
92673
92674 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
92675 if( rc==SQLITE_OK0 && pReadr->aMap==0 ){
92676 int pgsz = pTask->pSorter->pgsz;
92677 int iBuf = pReadr->iReadOff % pgsz;
92678 if( pReadr->aBuffer==0 ){
92679 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
92680 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT7;
92681 pReadr->nBuffer = pgsz;
92682 }
92683 if( rc==SQLITE_OK0 && iBuf ){
92684 int nRead = pgsz - iBuf;
92685 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
92686 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
92687 }
92688 rc = sqlite3OsRead(
92689 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
92690 );
92691 testcase( rc!=SQLITE_OK );
92692 }
92693 }
92694
92695 return rc;
92696}
92697
92698/*
92699** Advance PmaReader pReadr to the next key in its PMA. Return SQLITE_OK if
92700** no error occurs, or an SQLite error code if one does.
92701*/
92702static int vdbePmaReaderNext(PmaReader *pReadr){
92703 int rc = SQLITE_OK0; /* Return Code */
92704 u64 nRec = 0; /* Size of record in bytes */
92705
92706
92707 if( pReadr->iReadOff>=pReadr->iEof ){
92708 IncrMerger *pIncr = pReadr->pIncr;
92709 int bEof = 1;
92710 if( pIncr ){
92711 rc = vdbeIncrSwap(pIncr);
92712 if( rc==SQLITE_OK0 && pIncr->bEof==0 ){
92713 rc = vdbePmaReaderSeek(
92714 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
92715 );
92716 bEof = 0;
92717 }
92718 }
92719
92720 if( bEof ){
92721 /* This is an EOF condition */
92722 vdbePmaReaderClear(pReadr);
92723 testcase( rc!=SQLITE_OK );
92724 return rc;
92725 }
92726 }
92727
92728 if( rc==SQLITE_OK0 ){
92729 rc = vdbePmaReadVarint(pReadr, &nRec);
92730 }
92731 if( rc==SQLITE_OK0 ){
92732 pReadr->nKey = (int)nRec;
92733 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
92734 testcase( rc!=SQLITE_OK );
92735 }
92736
92737 return rc;
92738}
92739
92740/*
92741** Initialize PmaReader pReadr to scan through the PMA stored in file pFile
92742** starting at offset iStart and ending at offset iEof-1. This function
92743** leaves the PmaReader pointing to the first key in the PMA (or EOF if the
92744** PMA is empty).
92745**
92746** If the pnByte parameter is NULL, then it is assumed that the file
92747** contains a single PMA, and that that PMA omits the initial length varint.
92748*/
92749static int vdbePmaReaderInit(
92750 SortSubtask *pTask, /* Task context */
92751 SorterFile *pFile, /* Sorter file to read from */
92752 i64 iStart, /* Start offset in pFile */
92753 PmaReader *pReadr, /* PmaReader to populate */
92754 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
92755){
92756 int rc;
92757
92758 assert( pFile->iEof>iStart )((void) (0));
92759 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 )((void) (0));
92760 assert( pReadr->aBuffer==0 )((void) (0));
92761 assert( pReadr->aMap==0 )((void) (0));
92762
92763 rc = vdbePmaReaderSeek(pTask, pReadr, pFile, iStart);
92764 if( rc==SQLITE_OK0 ){
92765 u64 nByte = 0; /* Size of PMA in bytes */
92766 rc = vdbePmaReadVarint(pReadr, &nByte);
92767 pReadr->iEof = pReadr->iReadOff + nByte;
92768 *pnByte += nByte;
92769 }
92770
92771 if( rc==SQLITE_OK0 ){
92772 rc = vdbePmaReaderNext(pReadr);
92773 }
92774 return rc;
92775}
92776
92777/*
92778** A version of vdbeSorterCompare() that assumes that it has already been
92779** determined that the first field of key1 is equal to the first field of
92780** key2.
92781*/
92782static int vdbeSorterCompareTail(
92783 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
92784 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
92785 const void *pKey1, int nKey1, /* Left side of comparison */
92786 const void *pKey2, int nKey2 /* Right side of comparison */
92787){
92788 UnpackedRecord *r2 = pTask->pUnpacked;
92789 if( *pbKey2Cached==0 ){
92790 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
92791 *pbKey2Cached = 1;
92792 }
92793 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1);
92794}
92795
92796/*
92797** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
92798** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
92799** used by the comparison. Return the result of the comparison.
92800**
92801** If IN/OUT parameter *pbKey2Cached is true when this function is called,
92802** it is assumed that (pTask->pUnpacked) contains the unpacked version
92803** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
92804** version of key2 and *pbKey2Cached set to true before returning.
92805**
92806** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
92807** to SQLITE_NOMEM.
92808*/
92809static int vdbeSorterCompare(
92810 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
92811 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
92812 const void *pKey1, int nKey1, /* Left side of comparison */
92813 const void *pKey2, int nKey2 /* Right side of comparison */
92814){
92815 UnpackedRecord *r2 = pTask->pUnpacked;
92816 if( !*pbKey2Cached ){
92817 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
92818 *pbKey2Cached = 1;
92819 }
92820 return sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
92821}
92822
92823/*
92824** A specially optimized version of vdbeSorterCompare() that assumes that
92825** the first field of each key is a TEXT value and that the collation
92826** sequence to compare them with is BINARY.
92827*/
92828static int vdbeSorterCompareText(
92829 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
92830 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
92831 const void *pKey1, int nKey1, /* Left side of comparison */
92832 const void *pKey2, int nKey2 /* Right side of comparison */
92833){
92834 const u8 * const p1 = (const u8 * const)pKey1;
92835 const u8 * const p2 = (const u8 * const)pKey2;
92836 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
92837 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
92838
92839 int n1;
92840 int n2;
92841 int res;
92842
92843 getVarint32(&p1[1], n1)(u8)((*(&p1[1])<(u8)0x80)?((n1)=(u32)*(&p1[1])),1:
sqlite3GetVarint32((&p1[1]),(u32 *)&(n1)))
;
92844 getVarint32(&p2[1], n2)(u8)((*(&p2[1])<(u8)0x80)?((n2)=(u32)*(&p2[1])),1:
sqlite3GetVarint32((&p2[1]),(u32 *)&(n2)))
;
92845 res = memcmp(v1, v2, (MIN(n1, n2)((n1)<(n2)?(n1):(n2)) - 13)/2);
92846 if( res==0 ){
92847 res = n1 - n2;
92848 }
92849
92850 if( res==0 ){
92851 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
92852 res = vdbeSorterCompareTail(
92853 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
92854 );
92855 }
92856 }else{
92857 if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
92858 res = res * -1;
92859 }
92860 }
92861
92862 return res;
92863}
92864
92865/*
92866** A specially optimized version of vdbeSorterCompare() that assumes that
92867** the first field of each key is an INTEGER value.
92868*/
92869static int vdbeSorterCompareInt(
92870 SortSubtask *pTask, /* Subtask context (for pKeyInfo) */
92871 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
92872 const void *pKey1, int nKey1, /* Left side of comparison */
92873 const void *pKey2, int nKey2 /* Right side of comparison */
92874){
92875 const u8 * const p1 = (const u8 * const)pKey1;
92876 const u8 * const p2 = (const u8 * const)pKey2;
92877 const int s1 = p1[1]; /* Left hand serial type */
92878 const int s2 = p2[1]; /* Right hand serial type */
92879 const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */
92880 const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */
92881 int res; /* Return value */
92882
92883 assert( (s1>0 && s1<7) || s1==8 || s1==9 )((void) (0));
92884 assert( (s2>0 && s2<7) || s2==8 || s2==9 )((void) (0));
92885
92886 if( s1==s2 ){
92887 /* The two values have the same sign. Compare using memcmp(). */
92888 static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8, 0, 0, 0 };
92889 const u8 n = aLen[s1];
92890 int i;
92891 res = 0;
92892 for(i=0; i<n; i++){
92893 if( (res = v1[i] - v2[i])!=0 ){
92894 if( ((v1[0] ^ v2[0]) & 0x80)!=0 ){
92895 res = v1[0] & 0x80 ? -1 : +1;
92896 }
92897 break;
92898 }
92899 }
92900 }else if( s1>7 && s2>7 ){
92901 res = s1 - s2;
92902 }else{
92903 if( s2>7 ){
92904 res = +1;
92905 }else if( s1>7 ){
92906 res = -1;
92907 }else{
92908 res = s1 - s2;
92909 }
92910 assert( res!=0 )((void) (0));
92911
92912 if( res>0 ){
92913 if( *v1 & 0x80 ) res = -1;
92914 }else{
92915 if( *v2 & 0x80 ) res = +1;
92916 }
92917 }
92918
92919 if( res==0 ){
92920 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
92921 res = vdbeSorterCompareTail(
92922 pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2
92923 );
92924 }
92925 }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){
92926 res = res * -1;
92927 }
92928
92929 return res;
92930}
92931
92932/*
92933** Initialize the temporary index cursor just opened as a sorter cursor.
92934**
92935** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
92936** to determine the number of fields that should be compared from the
92937** records being sorted. However, if the value passed as argument nField
92938** is non-zero and the sorter is able to guarantee a stable sort, nField
92939** is used instead. This is used when sorting records for a CREATE INDEX
92940** statement. In this case, keys are always delivered to the sorter in
92941** order of the primary key, which happens to be make up the final part
92942** of the records being sorted. So if the sort is stable, there is never
92943** any reason to compare PK fields and they can be ignored for a small
92944** performance boost.
92945**
92946** The sorter can guarantee a stable sort when running in single-threaded
92947** mode, but not in multi-threaded mode.
92948**
92949** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
92950*/
92951SQLITE_PRIVATEstatic int sqlite3VdbeSorterInit(
92952 sqlite3 *db, /* Database connection (for malloc()) */
92953 int nField, /* Number of key fields in each record */
92954 VdbeCursor *pCsr /* Cursor that holds the new sorter */
92955){
92956 int pgsz; /* Page size of main database */
92957 int i; /* Used to iterate through aTask[] */
92958 VdbeSorter *pSorter; /* The new sorter */
92959 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
92960 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
92961 int sz; /* Size of pSorter in bytes */
92962 int rc = SQLITE_OK0;
92963#if SQLITE_MAX_WORKER_THREADS8==0
92964# define nWorker 0
92965#else
92966 int nWorker;
92967#endif
92968
92969 /* Initialize the upper limit on the number of worker threads */
92970#if SQLITE_MAX_WORKER_THREADS8>0
92971 if( sqlite3TempInMemory(db) || sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 ){
92972 nWorker = 0;
92973 }else{
92974 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS11];
92975 }
92976#endif
92977
92978 /* Do not allow the total number of threads (main thread + all workers)
92979 ** to exceed the maximum merge count */
92980#if SQLITE_MAX_WORKER_THREADS8>=SORTER_MAX_MERGE_COUNT16
92981 if( nWorker>=SORTER_MAX_MERGE_COUNT16 ){
92982 nWorker = SORTER_MAX_MERGE_COUNT16-1;
92983 }
92984#endif
92985
92986 assert( pCsr->pKeyInfo && pCsr->pBtx==0 )((void) (0));
92987 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
92988 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
92989 sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
92990
92991 pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
92992 pCsr->uc.pSorter = pSorter;
92993 if( pSorter==0 ){
92994 rc = SQLITE_NOMEM_BKPT7;
92995 }else{
92996 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
92997 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
92998 pKeyInfo->db = 0;
92999 if( nField && nWorker==0 ){
93000 pKeyInfo->nKeyField = nField;
93001 }
93002 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
93003 pSorter->nTask = nWorker + 1;
93004 pSorter->iPrev = (u8)(nWorker - 1);
93005 pSorter->bUseThreads = (pSorter->nTask>1);
93006 pSorter->db = db;
93007 for(i=0; i<pSorter->nTask; i++){
93008 SortSubtask *pTask = &pSorter->aTask[i];
93009 pTask->pSorter = pSorter;
93010 }
93011
93012 if( !sqlite3TempInMemory(db) ){
93013 i64 mxCache; /* Cache size in bytes*/
93014 u32 szPma = sqlite3GlobalConfigsqlite3Config.szPma;
93015 pSorter->mnPmaSize = szPma * pgsz;
93016
93017 mxCache = db->aDb[0].pSchema->cache_size;
93018 if( mxCache<0 ){
93019 /* A negative cache-size value C indicates that the cache is abs(C)
93020 ** KiB in size. */
93021 mxCache = mxCache * -1024;
93022 }else{
93023 mxCache = mxCache * pgsz;
93024 }
93025 mxCache = MIN(mxCache, SQLITE_MAX_PMASZ)((mxCache)<((1<<29))?(mxCache):((1<<29)));
93026 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache)((pSorter->mnPmaSize)>((int)mxCache)?(pSorter->mnPmaSize
):((int)mxCache))
;
93027
93028 /* Avoid large memory allocations if the application has requested
93029 ** SQLITE_CONFIG_SMALL_MALLOC. */
93030 if( sqlite3GlobalConfigsqlite3Config.bSmallMalloc==0 ){
93031 assert( pSorter->iMemory==0 )((void) (0));
93032 pSorter->nMemory = pgsz;
93033 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
93034 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT7;
93035 }
93036 }
93037
93038 if( pKeyInfo->nAllField<13
93039 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
93040 ){
93041 pSorter->typeMask = SORTER_TYPE_INTEGER0x01 | SORTER_TYPE_TEXT0x02;
93042 }
93043 }
93044
93045 return rc;
93046}
93047#undef nWorker /* Defined at the top of this function */
93048
93049/*
93050** Free the list of sorted records starting at pRecord.
93051*/
93052static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
93053 SorterRecord *p;
93054 SorterRecord *pNext;
93055 for(p=pRecord; p; p=pNext){
93056 pNext = p->u.pNext;
93057 sqlite3DbFree(db, p);
93058 }
93059}
93060
93061/*
93062** Free all resources owned by the object indicated by argument pTask. All
93063** fields of *pTask are zeroed before returning.
93064*/
93065static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){
93066 sqlite3DbFree(db, pTask->pUnpacked);
93067#if SQLITE_MAX_WORKER_THREADS8>0
93068 /* pTask->list.aMemory can only be non-zero if it was handed memory
93069 ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */
93070 if( pTask->list.aMemory ){
93071 sqlite3_free(pTask->list.aMemory);
93072 }else
93073#endif
93074 {
93075 assert( pTask->list.aMemory==0 )((void) (0));
93076 vdbeSorterRecordFree(0, pTask->list.pList);
93077 }
93078 if( pTask->file.pFd ){
93079 sqlite3OsCloseFree(pTask->file.pFd);
93080 }
93081 if( pTask->file2.pFd ){
93082 sqlite3OsCloseFree(pTask->file2.pFd);
93083 }
93084 memset(pTask, 0, sizeof(SortSubtask));
93085}
93086
93087#ifdef SQLITE_DEBUG_SORTER_THREADS
93088static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){
93089 i64 t;
93090 int iTask = (pTask - pTask->pSorter->aTask);
93091 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
93092 fprintf(stderrstderr, "%lld:%d %s\n", t, iTask, zEvent);
93093}
93094static void vdbeSorterRewindDebug(const char *zEvent){
93095 i64 t;
93096 sqlite3OsCurrentTimeInt64(sqlite3_vfs_find(0), &t);
93097 fprintf(stderrstderr, "%lld:X %s\n", t, zEvent);
93098}
93099static void vdbeSorterPopulateDebug(
93100 SortSubtask *pTask,
93101 const char *zEvent
93102){
93103 i64 t;
93104 int iTask = (pTask - pTask->pSorter->aTask);
93105 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
93106 fprintf(stderrstderr, "%lld:bg%d %s\n", t, iTask, zEvent);
93107}
93108static void vdbeSorterBlockDebug(
93109 SortSubtask *pTask,
93110 int bBlocked,
93111 const char *zEvent
93112){
93113 if( bBlocked ){
93114 i64 t;
93115 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
93116 fprintf(stderrstderr, "%lld:main %s\n", t, zEvent);
93117 }
93118}
93119#else
93120# define vdbeSorterWorkDebug(x,y)
93121# define vdbeSorterRewindDebug(y)
93122# define vdbeSorterPopulateDebug(x,y)
93123# define vdbeSorterBlockDebug(x,y,z)
93124#endif
93125
93126#if SQLITE_MAX_WORKER_THREADS8>0
93127/*
93128** Join thread pTask->thread.
93129*/
93130static int vdbeSorterJoinThread(SortSubtask *pTask){
93131 int rc = SQLITE_OK0;
93132 if( pTask->pThread ){
93133#ifdef SQLITE_DEBUG_SORTER_THREADS
93134 int bDone = pTask->bDone;
93135#endif
93136 void *pRet = SQLITE_INT_TO_PTR(SQLITE_ERROR)((void*)(long int)(1));
93137 vdbeSorterBlockDebug(pTask, !bDone, "enter");
93138 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
93139 vdbeSorterBlockDebug(pTask, !bDone, "exit");
93140 rc = SQLITE_PTR_TO_INT(pRet)((int)(long int)(pRet));
93141 assert( pTask->bDone==1 )((void) (0));
93142 pTask->bDone = 0;
93143 pTask->pThread = 0;
93144 }
93145 return rc;
93146}
93147
93148/*
93149** Launch a background thread to run xTask(pIn).
93150*/
93151static int vdbeSorterCreateThread(
93152 SortSubtask *pTask, /* Thread will use this task object */
93153 void *(*xTask)(void*), /* Routine to run in a separate thread */
93154 void *pIn /* Argument passed into xTask() */
93155){
93156 assert( pTask->pThread==0 && pTask->bDone==0 )((void) (0));
93157 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
93158}
93159
93160/*
93161** Join all outstanding threads launched by SorterWrite() to create
93162** level-0 PMAs.
93163*/
93164static int vdbeSorterJoinAll(VdbeSorter *pSorter, int rcin){
93165 int rc = rcin;
93166 int i;
93167
93168 /* This function is always called by the main user thread.
93169 **
93170 ** If this function is being called after SorterRewind() has been called,
93171 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
93172 ** is currently attempt to join one of the other threads. To avoid a race
93173 ** condition where this thread also attempts to join the same object, join
93174 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
93175 for(i=pSorter->nTask-1; i>=0; i--){
93176 SortSubtask *pTask = &pSorter->aTask[i];
93177 int rc2 = vdbeSorterJoinThread(pTask);
93178 if( rc==SQLITE_OK0 ) rc = rc2;
93179 }
93180 return rc;
93181}
93182#else
93183# define vdbeSorterJoinAll(x,rcin) (rcin)
93184# define vdbeSorterJoinThread(pTask) SQLITE_OK0
93185#endif
93186
93187/*
93188** Allocate a new MergeEngine object capable of handling up to
93189** nReader PmaReader inputs.
93190**
93191** nReader is automatically rounded up to the next power of two.
93192** nReader may not exceed SORTER_MAX_MERGE_COUNT even after rounding up.
93193*/
93194static MergeEngine *vdbeMergeEngineNew(int nReader){
93195 int N = 2; /* Smallest power of two >= nReader */
93196 int nByte; /* Total bytes of space to allocate */
93197 MergeEngine *pNew; /* Pointer to allocated object to return */
93198
93199 assert( nReader<=SORTER_MAX_MERGE_COUNT )((void) (0));
93200
93201 while( N<nReader ) N += N;
93202 nByte = sizeof(MergeEngine) + N * (sizeof(int) + sizeof(PmaReader));
93203
93204 pNew = sqlite3FaultSim(100) ? 0 : (MergeEngine*)sqlite3MallocZero(nByte);
93205 if( pNew ){
93206 pNew->nTree = N;
93207 pNew->pTask = 0;
93208 pNew->aReadr = (PmaReader*)&pNew[1];
93209 pNew->aTree = (int*)&pNew->aReadr[N];
93210 }
93211 return pNew;
93212}
93213
93214/*
93215** Free the MergeEngine object passed as the only argument.
93216*/
93217static void vdbeMergeEngineFree(MergeEngine *pMerger){
93218 int i;
93219 if( pMerger ){
93220 for(i=0; i<pMerger->nTree; i++){
93221 vdbePmaReaderClear(&pMerger->aReadr[i]);
93222 }
93223 }
93224 sqlite3_free(pMerger);
93225}
93226
93227/*
93228** Free all resources associated with the IncrMerger object indicated by
93229** the first argument.
93230*/
93231static void vdbeIncrFree(IncrMerger *pIncr){
93232 if( pIncr ){
93233#if SQLITE_MAX_WORKER_THREADS8>0
93234 if( pIncr->bUseThread ){
93235 vdbeSorterJoinThread(pIncr->pTask);
93236 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
93237 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
93238 }
93239#endif
93240 vdbeMergeEngineFree(pIncr->pMerger);
93241 sqlite3_free(pIncr);
93242 }
93243}
93244
93245/*
93246** Reset a sorting cursor back to its original empty state.
93247*/
93248SQLITE_PRIVATEstatic void sqlite3VdbeSorterReset(sqlite3 *db, VdbeSorter *pSorter){
93249 int i;
93250 (void)vdbeSorterJoinAll(pSorter, SQLITE_OK0);
93251 assert( pSorter->bUseThreads || pSorter->pReader==0 )((void) (0));
93252#if SQLITE_MAX_WORKER_THREADS8>0
93253 if( pSorter->pReader ){
93254 vdbePmaReaderClear(pSorter->pReader);
93255 sqlite3DbFree(db, pSorter->pReader);
93256 pSorter->pReader = 0;
93257 }
93258#endif
93259 vdbeMergeEngineFree(pSorter->pMerger);
93260 pSorter->pMerger = 0;
93261 for(i=0; i<pSorter->nTask; i++){
93262 SortSubtask *pTask = &pSorter->aTask[i];
93263 vdbeSortSubtaskCleanup(db, pTask);
93264 pTask->pSorter = pSorter;
93265 }
93266 if( pSorter->list.aMemory==0 ){
93267 vdbeSorterRecordFree(0, pSorter->list.pList);
93268 }
93269 pSorter->list.pList = 0;
93270 pSorter->list.szPMA = 0;
93271 pSorter->bUsePMA = 0;
93272 pSorter->iMemory = 0;
93273 pSorter->mxKeysize = 0;
93274 sqlite3DbFree(db, pSorter->pUnpacked);
93275 pSorter->pUnpacked = 0;
93276}
93277
93278/*
93279** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
93280*/
93281SQLITE_PRIVATEstatic void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
93282 VdbeSorter *pSorter;
93283 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
93284 pSorter = pCsr->uc.pSorter;
93285 if( pSorter ){
93286 sqlite3VdbeSorterReset(db, pSorter);
93287 sqlite3_free(pSorter->list.aMemory);
93288 sqlite3DbFree(db, pSorter);
93289 pCsr->uc.pSorter = 0;
93290 }
93291}
93292
93293#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
93294/*
93295** The first argument is a file-handle open on a temporary file. The file
93296** is guaranteed to be nByte bytes or smaller in size. This function
93297** attempts to extend the file to nByte bytes in size and to ensure that
93298** the VFS has memory mapped it.
93299**
93300** Whether or not the file does end up memory mapped of course depends on
93301** the specific VFS implementation.
93302*/
93303static void vdbeSorterExtendFile(sqlite3 *db, sqlite3_file *pFd, i64 nByte){
93304 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
93305 void *p = 0;
93306 int chunksize = 4*1024;
93307 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_CHUNK_SIZE6, &chunksize);
93308 sqlite3OsFileControlHint(pFd, SQLITE_FCNTL_SIZE_HINT5, &nByte);
93309 sqlite3OsFetch(pFd, 0, (int)nByte, &p);
93310 sqlite3OsUnfetch(pFd, 0, p);
93311 }
93312}
93313#else
93314# define vdbeSorterExtendFile(x,y,z)
93315#endif
93316
93317/*
93318** Allocate space for a file-handle and open a temporary file. If successful,
93319** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
93320** Otherwise, set *ppFd to 0 and return an SQLite error code.
93321*/
93322static int vdbeSorterOpenTempFile(
93323 sqlite3 *db, /* Database handle doing sort */
93324 i64 nExtend, /* Attempt to extend file to this size */
93325 sqlite3_file **ppFd
93326){
93327 int rc;
93328 if( sqlite3FaultSim(202) ) return SQLITE_IOERR_ACCESS(10 | (13<<8));
93329 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
93330 SQLITE_OPEN_TEMP_JOURNAL0x00001000 |
93331 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
93332 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008, &rc
93333 );
93334 if( rc==SQLITE_OK0 ){
93335 i64 max = SQLITE_MAX_MMAP_SIZE0x7fff0000;
93336 sqlite3OsFileControlHint(*ppFd, SQLITE_FCNTL_MMAP_SIZE18, (void*)&max);
93337 if( nExtend>0 ){
93338 vdbeSorterExtendFile(db, *ppFd, nExtend);
93339 }
93340 }
93341 return rc;
93342}
93343
93344/*
93345** If it has not already been allocated, allocate the UnpackedRecord
93346** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
93347** if no allocation was required), or SQLITE_NOMEM otherwise.
93348*/
93349static int vdbeSortAllocUnpacked(SortSubtask *pTask){
93350 if( pTask->pUnpacked==0 ){
93351 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
93352 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT7;
93353 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
93354 pTask->pUnpacked->errCode = 0;
93355 }
93356 return SQLITE_OK0;
93357}
93358
93359
93360/*
93361** Merge the two sorted lists p1 and p2 into a single list.
93362*/
93363static SorterRecord *vdbeSorterMerge(
93364 SortSubtask *pTask, /* Calling thread context */
93365 SorterRecord *p1, /* First list to merge */
93366 SorterRecord *p2 /* Second list to merge */
93367){
93368 SorterRecord *pFinal = 0;
93369 SorterRecord **pp = &pFinal;
93370 int bCached = 0;
93371
93372 assert( p1!=0 && p2!=0 )((void) (0));
93373 for(;;){
93374 int res;
93375 res = pTask->xCompare(
93376 pTask, &bCached, SRVAL(p1)((void*)((SorterRecord*)(p1) + 1)), p1->nVal, SRVAL(p2)((void*)((SorterRecord*)(p2) + 1)), p2->nVal
93377 );
93378
93379 if( res<=0 ){
93380 *pp = p1;
93381 pp = &p1->u.pNext;
93382 p1 = p1->u.pNext;
93383 if( p1==0 ){
93384 *pp = p2;
93385 break;
93386 }
93387 }else{
93388 *pp = p2;
93389 pp = &p2->u.pNext;
93390 p2 = p2->u.pNext;
93391 bCached = 0;
93392 if( p2==0 ){
93393 *pp = p1;
93394 break;
93395 }
93396 }
93397 }
93398 return pFinal;
93399}
93400
93401/*
93402** Return the SorterCompare function to compare values collected by the
93403** sorter object passed as the only argument.
93404*/
93405static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){
93406 if( p->typeMask==SORTER_TYPE_INTEGER0x01 ){
93407 return vdbeSorterCompareInt;
93408 }else if( p->typeMask==SORTER_TYPE_TEXT0x02 ){
93409 return vdbeSorterCompareText;
93410 }
93411 return vdbeSorterCompare;
93412}
93413
93414/*
93415** Sort the linked list of records headed at pTask->pList. Return
93416** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if
93417** an error occurs.
93418*/
93419static int vdbeSorterSort(SortSubtask *pTask, SorterList *pList){
93420 int i;
93421 SorterRecord **aSlot;
93422 SorterRecord *p;
93423 int rc;
93424
93425 rc = vdbeSortAllocUnpacked(pTask);
93426 if( rc!=SQLITE_OK0 ) return rc;
93427
93428 p = pList->pList;
93429 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
93430
93431 aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
93432 if( !aSlot ){
93433 return SQLITE_NOMEM_BKPT7;
93434 }
93435
93436 while( p ){
93437 SorterRecord *pNext;
93438 if( pList->aMemory ){
93439 if( (u8*)p==pList->aMemory ){
93440 pNext = 0;
93441 }else{
93442 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) )((void) (0));
93443 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
93444 }
93445 }else{
93446 pNext = p->u.pNext;
93447 }
93448
93449 p->u.pNext = 0;
93450 for(i=0; aSlot[i]; i++){
93451 p = vdbeSorterMerge(pTask, p, aSlot[i]);
93452 aSlot[i] = 0;
93453 }
93454 aSlot[i] = p;
93455 p = pNext;
93456 }
93457
93458 p = 0;
93459 for(i=0; i<64; i++){
93460 if( aSlot[i]==0 ) continue;
93461 p = p ? vdbeSorterMerge(pTask, p, aSlot[i]) : aSlot[i];
93462 }
93463 pList->pList = p;
93464
93465 sqlite3_free(aSlot);
93466 assert( pTask->pUnpacked->errCode==SQLITE_OK((void) (0))
93467 || pTask->pUnpacked->errCode==SQLITE_NOMEM((void) (0))
93468 )((void) (0));
93469 return pTask->pUnpacked->errCode;
93470}
93471
93472/*
93473** Initialize a PMA-writer object.
93474*/
93475static void vdbePmaWriterInit(
93476 sqlite3_file *pFd, /* File handle to write to */
93477 PmaWriter *p, /* Object to populate */
93478 int nBuf, /* Buffer size */
93479 i64 iStart /* Offset of pFd to begin writing at */
93480){
93481 memset(p, 0, sizeof(PmaWriter));
93482 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
93483 if( !p->aBuffer ){
93484 p->eFWErr = SQLITE_NOMEM_BKPT7;
93485 }else{
93486 p->iBufEnd = p->iBufStart = (iStart % nBuf);
93487 p->iWriteOff = iStart - p->iBufStart;
93488 p->nBuffer = nBuf;
93489 p->pFd = pFd;
93490 }
93491}
93492
93493/*
93494** Write nData bytes of data to the PMA. Return SQLITE_OK
93495** if successful, or an SQLite error code if an error occurs.
93496*/
93497static void vdbePmaWriteBlob(PmaWriter *p, u8 *pData, int nData){
93498 int nRem = nData;
93499 while( nRem>0 && p->eFWErr==0 ){
93500 int nCopy = nRem;
93501 if( nCopy>(p->nBuffer - p->iBufEnd) ){
93502 nCopy = p->nBuffer - p->iBufEnd;
93503 }
93504
93505 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
93506 p->iBufEnd += nCopy;
93507 if( p->iBufEnd==p->nBuffer ){
93508 p->eFWErr = sqlite3OsWrite(p->pFd,
93509 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
93510 p->iWriteOff + p->iBufStart
93511 );
93512 p->iBufStart = p->iBufEnd = 0;
93513 p->iWriteOff += p->nBuffer;
93514 }
93515 assert( p->iBufEnd<p->nBuffer )((void) (0));
93516
93517 nRem -= nCopy;
93518 }
93519}
93520
93521/*
93522** Flush any buffered data to disk and clean up the PMA-writer object.
93523** The results of using the PMA-writer after this call are undefined.
93524** Return SQLITE_OK if flushing the buffered data succeeds or is not
93525** required. Otherwise, return an SQLite error code.
93526**
93527** Before returning, set *piEof to the offset immediately following the
93528** last byte written to the file.
93529*/
93530static int vdbePmaWriterFinish(PmaWriter *p, i64 *piEof){
93531 int rc;
93532 if( p->eFWErr==0 && ALWAYS(p->aBuffer)(p->aBuffer) && p->iBufEnd>p->iBufStart ){
93533 p->eFWErr = sqlite3OsWrite(p->pFd,
93534 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
93535 p->iWriteOff + p->iBufStart
93536 );
93537 }
93538 *piEof = (p->iWriteOff + p->iBufEnd);
93539 sqlite3_free(p->aBuffer);
93540 rc = p->eFWErr;
93541 memset(p, 0, sizeof(PmaWriter));
93542 return rc;
93543}
93544
93545/*
93546** Write value iVal encoded as a varint to the PMA. Return
93547** SQLITE_OK if successful, or an SQLite error code if an error occurs.
93548*/
93549static void vdbePmaWriteVarint(PmaWriter *p, u64 iVal){
93550 int nByte;
93551 u8 aByte[10];
93552 nByte = sqlite3PutVarint(aByte, iVal);
93553 vdbePmaWriteBlob(p, aByte, nByte);
93554}
93555
93556/*
93557** Write the current contents of in-memory linked-list pList to a level-0
93558** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
93559** successful, or an SQLite error code otherwise.
93560**
93561** The format of a PMA is:
93562**
93563** * A varint. This varint contains the total number of bytes of content
93564** in the PMA (not including the varint itself).
93565**
93566** * One or more records packed end-to-end in order of ascending keys.
93567** Each record consists of a varint followed by a blob of data (the
93568** key). The varint is the number of bytes in the blob of data.
93569*/
93570static int vdbeSorterListToPMA(SortSubtask *pTask, SorterList *pList){
93571 sqlite3 *db = pTask->pSorter->db;
93572 int rc = SQLITE_OK0; /* Return code */
93573 PmaWriter writer; /* Object used to write to the file */
93574
93575#ifdef SQLITE_DEBUG
93576 /* Set iSz to the expected size of file pTask->file after writing the PMA.
93577 ** This is used by an assert() statement at the end of this function. */
93578 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
93579#endif
93580
93581 vdbeSorterWorkDebug(pTask, "enter");
93582 memset(&writer, 0, sizeof(PmaWriter));
93583 assert( pList->szPMA>0 )((void) (0));
93584
93585 /* If the first temporary PMA file has not been opened, open it now. */
93586 if( pTask->file.pFd==0 ){
93587 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
93588 assert( rc!=SQLITE_OK || pTask->file.pFd )((void) (0));
93589 assert( pTask->file.iEof==0 )((void) (0));
93590 assert( pTask->nPMA==0 )((void) (0));
93591 }
93592
93593 /* Try to get the file to memory map */
93594 if( rc==SQLITE_OK0 ){
93595 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
93596 }
93597
93598 /* Sort the list */
93599 if( rc==SQLITE_OK0 ){
93600 rc = vdbeSorterSort(pTask, pList);
93601 }
93602
93603 if( rc==SQLITE_OK0 ){
93604 SorterRecord *p;
93605 SorterRecord *pNext = 0;
93606
93607 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
93608 pTask->file.iEof);
93609 pTask->nPMA++;
93610 vdbePmaWriteVarint(&writer, pList->szPMA);
93611 for(p=pList->pList; p; p=pNext){
93612 pNext = p->u.pNext;
93613 vdbePmaWriteVarint(&writer, p->nVal);
93614 vdbePmaWriteBlob(&writer, SRVAL(p)((void*)((SorterRecord*)(p) + 1)), p->nVal);
93615 if( pList->aMemory==0 ) sqlite3_free(p);
93616 }
93617 pList->pList = p;
93618 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
93619 }
93620
93621 vdbeSorterWorkDebug(pTask, "exit");
93622 assert( rc!=SQLITE_OK || pList->pList==0 )((void) (0));
93623 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz )((void) (0));
93624 return rc;
93625}
93626
93627/*
93628** Advance the MergeEngine to its next entry.
93629** Set *pbEof to true there is no next entry because
93630** the MergeEngine has reached the end of all its inputs.
93631**
93632** Return SQLITE_OK if successful or an error code if an error occurs.
93633*/
93634static int vdbeMergeEngineStep(
93635 MergeEngine *pMerger, /* The merge engine to advance to the next row */
93636 int *pbEof /* Set TRUE at EOF. Set false for more content */
93637){
93638 int rc;
93639 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
93640 SortSubtask *pTask = pMerger->pTask;
93641
93642 /* Advance the current PmaReader */
93643 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
93644
93645 /* Update contents of aTree[] */
93646 if( rc==SQLITE_OK0 ){
93647 int i; /* Index of aTree[] to recalculate */
93648 PmaReader *pReadr1; /* First PmaReader to compare */
93649 PmaReader *pReadr2; /* Second PmaReader to compare */
93650 int bCached = 0;
93651
93652 /* Find the first two PmaReaders to compare. The one that was just
93653 ** advanced (iPrev) and the one next to it in the array. */
93654 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
93655 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
93656
93657 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
93658 /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
93659 int iRes;
93660 if( pReadr1->pFd==0 ){
93661 iRes = +1;
93662 }else if( pReadr2->pFd==0 ){
93663 iRes = -1;
93664 }else{
93665 iRes = pTask->xCompare(pTask, &bCached,
93666 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
93667 );
93668 }
93669
93670 /* If pReadr1 contained the smaller value, set aTree[i] to its index.
93671 ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
93672 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
93673 ** pKey2 to point to the record belonging to pReadr2.
93674 **
93675 ** Alternatively, if pReadr2 contains the smaller of the two values,
93676 ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
93677 ** was actually called above, then pTask->pUnpacked now contains
93678 ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
93679 ** vdbeSorterCompare() from decoding pReadr2 again.
93680 **
93681 ** If the two values were equal, then the value from the oldest
93682 ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
93683 ** is sorted from oldest to newest, so pReadr1 contains older values
93684 ** than pReadr2 iff (pReadr1<pReadr2). */
93685 if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
93686 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
93687 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
93688 bCached = 0;
93689 }else{
93690 if( pReadr1->pFd ) bCached = 0;
93691 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
93692 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
93693 }
93694 }
93695 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
93696 }
93697
93698 return (rc==SQLITE_OK0 ? pTask->pUnpacked->errCode : rc);
93699}
93700
93701#if SQLITE_MAX_WORKER_THREADS8>0
93702/*
93703** The main routine for background threads that write level-0 PMAs.
93704*/
93705static void *vdbeSorterFlushThread(void *pCtx){
93706 SortSubtask *pTask = (SortSubtask*)pCtx;
93707 int rc; /* Return code */
93708 assert( pTask->bDone==0 )((void) (0));
93709 rc = vdbeSorterListToPMA(pTask, &pTask->list);
93710 pTask->bDone = 1;
93711 return SQLITE_INT_TO_PTR(rc)((void*)(long int)(rc));
93712}
93713#endif /* SQLITE_MAX_WORKER_THREADS>0 */
93714
93715/*
93716** Flush the current contents of VdbeSorter.list to a new PMA, possibly
93717** using a background thread.
93718*/
93719static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
93720#if SQLITE_MAX_WORKER_THREADS8==0
93721 pSorter->bUsePMA = 1;
93722 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
93723#else
93724 int rc = SQLITE_OK0;
93725 int i;
93726 SortSubtask *pTask = 0; /* Thread context used to create new PMA */
93727 int nWorker = (pSorter->nTask-1);
93728
93729 /* Set the flag to indicate that at least one PMA has been written.
93730 ** Or will be, anyhow. */
93731 pSorter->bUsePMA = 1;
93732
93733 /* Select a sub-task to sort and flush the current list of in-memory
93734 ** records to disk. If the sorter is running in multi-threaded mode,
93735 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
93736 ** the background thread from a sub-tasks previous turn is still running,
93737 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
93738 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
93739 ** sub-tasks are prefered as they use background threads - the final
93740 ** sub-task uses the main thread. */
93741 for(i=0; i<nWorker; i++){
93742 int iTest = (pSorter->iPrev + i + 1) % nWorker;
93743 pTask = &pSorter->aTask[iTest];
93744 if( pTask->bDone ){
93745 rc = vdbeSorterJoinThread(pTask);
93746 }
93747 if( rc!=SQLITE_OK0 || pTask->pThread==0 ) break;
93748 }
93749
93750 if( rc==SQLITE_OK0 ){
93751 if( i==nWorker ){
93752 /* Use the foreground thread for this operation */
93753 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
93754 }else{
93755 /* Launch a background thread for this operation */
93756 u8 *aMem = pTask->list.aMemory;
93757 void *pCtx = (void*)pTask;
93758
93759 assert( pTask->pThread==0 && pTask->bDone==0 )((void) (0));
93760 assert( pTask->list.pList==0 )((void) (0));
93761 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 )((void) (0));
93762
93763 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
93764 pTask->list = pSorter->list;
93765 pSorter->list.pList = 0;
93766 pSorter->list.szPMA = 0;
93767 if( aMem ){
93768 pSorter->list.aMemory = aMem;
93769 pSorter->nMemory = sqlite3MallocSize(aMem);
93770 }else if( pSorter->list.aMemory ){
93771 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
93772 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT7;
93773 }
93774
93775 rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
93776 }
93777 }
93778
93779 return rc;
93780#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
93781}
93782
93783/*
93784** Add a record to the sorter.
93785*/
93786SQLITE_PRIVATEstatic int sqlite3VdbeSorterWrite(
93787 const VdbeCursor *pCsr, /* Sorter cursor */
93788 Mem *pVal /* Memory cell containing record */
93789){
93790 VdbeSorter *pSorter;
93791 int rc = SQLITE_OK0; /* Return Code */
93792 SorterRecord *pNew; /* New list element */
93793 int bFlush; /* True to flush contents of memory to PMA */
93794 int nReq; /* Bytes of memory required */
93795 int nPMA; /* Bytes of PMA space required */
93796 int t; /* serial type of first record field */
93797
93798 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
93799 pSorter = pCsr->uc.pSorter;
93800 getVarint32((const u8*)&pVal->z[1], t)(u8)((*((const u8*)&pVal->z[1])<(u8)0x80)?((t)=(u32
)*((const u8*)&pVal->z[1])),1:sqlite3GetVarint32(((const
u8*)&pVal->z[1]),(u32 *)&(t)))
;
93801 if( t>0 && t<10 && t!=7 ){
93802 pSorter->typeMask &= SORTER_TYPE_INTEGER0x01;
93803 }else if( t>10 && (t & 0x01) ){
93804 pSorter->typeMask &= SORTER_TYPE_TEXT0x02;
93805 }else{
93806 pSorter->typeMask = 0;
93807 }
93808
93809 assert( pSorter )((void) (0));
93810
93811 /* Figure out whether or not the current contents of memory should be
93812 ** flushed to a PMA before continuing. If so, do so.
93813 **
93814 ** If using the single large allocation mode (pSorter->aMemory!=0), then
93815 ** flush the contents of memory to a new PMA if (a) at least one value is
93816 ** already in memory and (b) the new value will not fit in memory.
93817 **
93818 ** Or, if using separate allocations for each record, flush the contents
93819 ** of memory to a PMA if either of the following are true:
93820 **
93821 ** * The total memory allocated for the in-memory list is greater
93822 ** than (page-size * cache-size), or
93823 **
93824 ** * The total memory allocated for the in-memory list is greater
93825 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
93826 */
93827 nReq = pVal->n + sizeof(SorterRecord);
93828 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
93829 if( pSorter->mxPmaSize ){
93830 if( pSorter->list.aMemory ){
93831 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
93832 }else{
93833 bFlush = (
93834 (pSorter->list.szPMA > pSorter->mxPmaSize)
93835 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
93836 );
93837 }
93838 if( bFlush ){
93839 rc = vdbeSorterFlushPMA(pSorter);
93840 pSorter->list.szPMA = 0;
93841 pSorter->iMemory = 0;
93842 assert( rc!=SQLITE_OK || pSorter->list.pList==0 )((void) (0));
93843 }
93844 }
93845
93846 pSorter->list.szPMA += nPMA;
93847 if( nPMA>pSorter->mxKeysize ){
93848 pSorter->mxKeysize = nPMA;
93849 }
93850
93851 if( pSorter->list.aMemory ){
93852 int nMin = pSorter->iMemory + nReq;
93853
93854 if( nMin>pSorter->nMemory ){
93855 u8 *aNew;
93856 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
93857 int iListOff = -1;
93858 if( pSorter->list.pList ){
93859 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
93860 }
93861 while( nNew < nMin ) nNew = nNew*2;
93862 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
93863 if( nNew < nMin ) nNew = nMin;
93864 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
93865 if( !aNew ) return SQLITE_NOMEM_BKPT7;
93866 if( iListOff>=0 ){
93867 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
93868 }
93869 pSorter->list.aMemory = aNew;
93870 pSorter->nMemory = nNew;
93871 }
93872
93873 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
93874 pSorter->iMemory += ROUND8(nReq)(((nReq)+7)&~7);
93875 if( pSorter->list.pList ){
93876 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
93877 }
93878 }else{
93879 pNew = (SorterRecord *)sqlite3Malloc(nReq);
93880 if( pNew==0 ){
93881 return SQLITE_NOMEM_BKPT7;
93882 }
93883 pNew->u.pNext = pSorter->list.pList;
93884 }
93885
93886 memcpy(SRVAL(pNew)((void*)((SorterRecord*)(pNew) + 1)), pVal->z, pVal->n);
93887 pNew->nVal = pVal->n;
93888 pSorter->list.pList = pNew;
93889
93890 return rc;
93891}
93892
93893/*
93894** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
93895** of the data stored in aFile[1] is the same as that used by regular PMAs,
93896** except that the number-of-bytes varint is omitted from the start.
93897*/
93898static int vdbeIncrPopulate(IncrMerger *pIncr){
93899 int rc = SQLITE_OK0;
93900 int rc2;
93901 i64 iStart = pIncr->iStartOff;
93902 SorterFile *pOut = &pIncr->aFile[1];
93903 SortSubtask *pTask = pIncr->pTask;
93904 MergeEngine *pMerger = pIncr->pMerger;
93905 PmaWriter writer;
93906 assert( pIncr->bEof==0 )((void) (0));
93907
93908 vdbeSorterPopulateDebug(pTask, "enter");
93909
93910 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
93911 while( rc==SQLITE_OK0 ){
93912 int dummy;
93913 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
93914 int nKey = pReader->nKey;
93915 i64 iEof = writer.iWriteOff + writer.iBufEnd;
93916
93917 /* Check if the output file is full or if the input has been exhausted.
93918 ** In either case exit the loop. */
93919 if( pReader->pFd==0 ) break;
93920 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
93921
93922 /* Write the next key to the output. */
93923 vdbePmaWriteVarint(&writer, nKey);
93924 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
93925 assert( pIncr->pMerger->pTask==pTask )((void) (0));
93926 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
93927 }
93928
93929 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
93930 if( rc==SQLITE_OK0 ) rc = rc2;
93931 vdbeSorterPopulateDebug(pTask, "exit");
93932 return rc;
93933}
93934
93935#if SQLITE_MAX_WORKER_THREADS8>0
93936/*
93937** The main routine for background threads that populate aFile[1] of
93938** multi-threaded IncrMerger objects.
93939*/
93940static void *vdbeIncrPopulateThread(void *pCtx){
93941 IncrMerger *pIncr = (IncrMerger*)pCtx;
93942 void *pRet = SQLITE_INT_TO_PTR( vdbeIncrPopulate(pIncr) )((void*)(long int)(vdbeIncrPopulate(pIncr)));
93943 pIncr->pTask->bDone = 1;
93944 return pRet;
93945}
93946
93947/*
93948** Launch a background thread to populate aFile[1] of pIncr.
93949*/
93950static int vdbeIncrBgPopulate(IncrMerger *pIncr){
93951 void *p = (void*)pIncr;
93952 assert( pIncr->bUseThread )((void) (0));
93953 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
93954}
93955#endif
93956
93957/*
93958** This function is called when the PmaReader corresponding to pIncr has
93959** finished reading the contents of aFile[0]. Its purpose is to "refill"
93960** aFile[0] such that the PmaReader should start rereading it from the
93961** beginning.
93962**
93963** For single-threaded objects, this is accomplished by literally reading
93964** keys from pIncr->pMerger and repopulating aFile[0].
93965**
93966** For multi-threaded objects, all that is required is to wait until the
93967** background thread is finished (if it is not already) and then swap
93968** aFile[0] and aFile[1] in place. If the contents of pMerger have not
93969** been exhausted, this function also launches a new background thread
93970** to populate the new aFile[1].
93971**
93972** SQLITE_OK is returned on success, or an SQLite error code otherwise.
93973*/
93974static int vdbeIncrSwap(IncrMerger *pIncr){
93975 int rc = SQLITE_OK0;
93976
93977#if SQLITE_MAX_WORKER_THREADS8>0
93978 if( pIncr->bUseThread ){
93979 rc = vdbeSorterJoinThread(pIncr->pTask);
93980
93981 if( rc==SQLITE_OK0 ){
93982 SorterFile f0 = pIncr->aFile[0];
93983 pIncr->aFile[0] = pIncr->aFile[1];
93984 pIncr->aFile[1] = f0;
93985 }
93986
93987 if( rc==SQLITE_OK0 ){
93988 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
93989 pIncr->bEof = 1;
93990 }else{
93991 rc = vdbeIncrBgPopulate(pIncr);
93992 }
93993 }
93994 }else
93995#endif
93996 {
93997 rc = vdbeIncrPopulate(pIncr);
93998 pIncr->aFile[0] = pIncr->aFile[1];
93999 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
94000 pIncr->bEof = 1;
94001 }
94002 }
94003
94004 return rc;
94005}
94006
94007/*
94008** Allocate and return a new IncrMerger object to read data from pMerger.
94009**
94010** If an OOM condition is encountered, return NULL. In this case free the
94011** pMerger argument before returning.
94012*/
94013static int vdbeIncrMergerNew(
94014 SortSubtask *pTask, /* The thread that will be using the new IncrMerger */
94015 MergeEngine *pMerger, /* The MergeEngine that the IncrMerger will control */
94016 IncrMerger **ppOut /* Write the new IncrMerger here */
94017){
94018 int rc = SQLITE_OK0;
94019 IncrMerger *pIncr = *ppOut = (IncrMerger*)
94020 (sqlite3FaultSim(100) ? 0 : sqlite3MallocZero(sizeof(*pIncr)));
94021 if( pIncr ){
94022 pIncr->pMerger = pMerger;
94023 pIncr->pTask = pTask;
94024 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2)((pTask->pSorter->mxKeysize+9)>(pTask->pSorter->
mxPmaSize/2)?(pTask->pSorter->mxKeysize+9):(pTask->pSorter
->mxPmaSize/2))
;
94025 pTask->file2.iEof += pIncr->mxSz;
94026 }else{
94027 vdbeMergeEngineFree(pMerger);
94028 rc = SQLITE_NOMEM_BKPT7;
94029 }
94030 return rc;
94031}
94032
94033#if SQLITE_MAX_WORKER_THREADS8>0
94034/*
94035** Set the "use-threads" flag on object pIncr.
94036*/
94037static void vdbeIncrMergerSetThreads(IncrMerger *pIncr){
94038 pIncr->bUseThread = 1;
94039 pIncr->pTask->file2.iEof -= pIncr->mxSz;
94040}
94041#endif /* SQLITE_MAX_WORKER_THREADS>0 */
94042
94043
94044
94045/*
94046** Recompute pMerger->aTree[iOut] by comparing the next keys on the
94047** two PmaReaders that feed that entry. Neither of the PmaReaders
94048** are advanced. This routine merely does the comparison.
94049*/
94050static void vdbeMergeEngineCompare(
94051 MergeEngine *pMerger, /* Merge engine containing PmaReaders to compare */
94052 int iOut /* Store the result in pMerger->aTree[iOut] */
94053){
94054 int i1;
94055 int i2;
94056 int iRes;
94057 PmaReader *p1;
94058 PmaReader *p2;
94059
94060 assert( iOut<pMerger->nTree && iOut>0 )((void) (0));
94061
94062 if( iOut>=(pMerger->nTree/2) ){
94063 i1 = (iOut - pMerger->nTree/2) * 2;
94064 i2 = i1 + 1;
94065 }else{
94066 i1 = pMerger->aTree[iOut*2];
94067 i2 = pMerger->aTree[iOut*2+1];
94068 }
94069
94070 p1 = &pMerger->aReadr[i1];
94071 p2 = &pMerger->aReadr[i2];
94072
94073 if( p1->pFd==0 ){
94074 iRes = i2;
94075 }else if( p2->pFd==0 ){
94076 iRes = i1;
94077 }else{
94078 SortSubtask *pTask = pMerger->pTask;
94079 int bCached = 0;
94080 int res;
94081 assert( pTask->pUnpacked!=0 )((void) (0)); /* from vdbeSortSubtaskMain() */
94082 res = pTask->xCompare(
94083 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
94084 );
94085 if( res<=0 ){
94086 iRes = i1;
94087 }else{
94088 iRes = i2;
94089 }
94090 }
94091
94092 pMerger->aTree[iOut] = iRes;
94093}
94094
94095/*
94096** Allowed values for the eMode parameter to vdbeMergeEngineInit()
94097** and vdbePmaReaderIncrMergeInit().
94098**
94099** Only INCRINIT_NORMAL is valid in single-threaded builds (when
94100** SQLITE_MAX_WORKER_THREADS==0). The other values are only used
94101** when there exists one or more separate worker threads.
94102*/
94103#define INCRINIT_NORMAL0 0
94104#define INCRINIT_TASK1 1
94105#define INCRINIT_ROOT2 2
94106
94107/*
94108** Forward reference required as the vdbeIncrMergeInit() and
94109** vdbePmaReaderIncrInit() routines are called mutually recursively when
94110** building a merge tree.
94111*/
94112static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode);
94113
94114/*
94115** Initialize the MergeEngine object passed as the second argument. Once this
94116** function returns, the first key of merged data may be read from the
94117** MergeEngine object in the usual fashion.
94118**
94119** If argument eMode is INCRINIT_ROOT, then it is assumed that any IncrMerge
94120** objects attached to the PmaReader objects that the merger reads from have
94121** already been populated, but that they have not yet populated aFile[0] and
94122** set the PmaReader objects up to read from it. In this case all that is
94123** required is to call vdbePmaReaderNext() on each PmaReader to point it at
94124** its first key.
94125**
94126** Otherwise, if eMode is any value other than INCRINIT_ROOT, then use
94127** vdbePmaReaderIncrMergeInit() to initialize each PmaReader that feeds data
94128** to pMerger.
94129**
94130** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
94131*/
94132static int vdbeMergeEngineInit(
94133 SortSubtask *pTask, /* Thread that will run pMerger */
94134 MergeEngine *pMerger, /* MergeEngine to initialize */
94135 int eMode /* One of the INCRINIT_XXX constants */
94136){
94137 int rc = SQLITE_OK0; /* Return code */
94138 int i; /* For looping over PmaReader objects */
94139 int nTree; /* Number of subtrees to merge */
94140
94141 /* Failure to allocate the merge would have been detected prior to
94142 ** invoking this routine */
94143 assert( pMerger!=0 )((void) (0));
94144
94145 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
94146 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL )((void) (0));
94147
94148 /* Verify that the MergeEngine is assigned to a single thread */
94149 assert( pMerger->pTask==0 )((void) (0));
94150 pMerger->pTask = pTask;
94151
94152 nTree = pMerger->nTree;
94153 for(i=0; i<nTree; i++){
94154 if( SQLITE_MAX_WORKER_THREADS8>0 && eMode==INCRINIT_ROOT2 ){
94155 /* PmaReaders should be normally initialized in order, as if they are
94156 ** reading from the same temp file this makes for more linear file IO.
94157 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
94158 ** in use it will block the vdbePmaReaderNext() call while it uses
94159 ** the main thread to fill its buffer. So calling PmaReaderNext()
94160 ** on this PmaReader before any of the multi-threaded PmaReaders takes
94161 ** better advantage of multi-processor hardware. */
94162 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
94163 }else{
94164 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL0);
94165 }
94166 if( rc!=SQLITE_OK0 ) return rc;
94167 }
94168
94169 for(i=pMerger->nTree-1; i>0; i--){
94170 vdbeMergeEngineCompare(pMerger, i);
94171 }
94172 return pTask->pUnpacked->errCode;
94173}
94174
94175/*
94176** The PmaReader passed as the first argument is guaranteed to be an
94177** incremental-reader (pReadr->pIncr!=0). This function serves to open
94178** and/or initialize the temp file related fields of the IncrMerge
94179** object at (pReadr->pIncr).
94180**
94181** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders
94182** in the sub-tree headed by pReadr are also initialized. Data is then
94183** loaded into the buffers belonging to pReadr and it is set to point to
94184** the first key in its range.
94185**
94186** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed
94187** to be a multi-threaded PmaReader and this function is being called in a
94188** background thread. In this case all PmaReaders in the sub-tree are
94189** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to
94190** pReadr is populated. However, pReadr itself is not set up to point
94191** to its first key. A call to vdbePmaReaderNext() is still required to do
94192** that.
94193**
94194** The reason this function does not call vdbePmaReaderNext() immediately
94195** in the INCRINIT_TASK case is that vdbePmaReaderNext() assumes that it has
94196** to block on thread (pTask->thread) before accessing aFile[1]. But, since
94197** this entire function is being run by thread (pTask->thread), that will
94198** lead to the current background thread attempting to join itself.
94199**
94200** Finally, if argument eMode is set to INCRINIT_ROOT, it may be assumed
94201** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
94202** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
94203** In this case vdbePmaReaderNext() is called on all child PmaReaders and
94204** the current PmaReader set to point to the first key in its range.
94205**
94206** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
94207*/
94208static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){
94209 int rc = SQLITE_OK0;
94210 IncrMerger *pIncr = pReadr->pIncr;
94211 SortSubtask *pTask = pIncr->pTask;
94212 sqlite3 *db = pTask->pSorter->db;
94213
94214 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
94215 assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL )((void) (0));
94216
94217 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
94218
94219 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
94220 ** requires two temp files to itself, whereas a single-threaded object
94221 ** only requires a region of pTask->file2. */
94222 if( rc==SQLITE_OK0 ){
94223 int mxSz = pIncr->mxSz;
94224#if SQLITE_MAX_WORKER_THREADS8>0
94225 if( pIncr->bUseThread ){
94226 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
94227 if( rc==SQLITE_OK0 ){
94228 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
94229 }
94230 }else
94231#endif
94232 /*if( !pIncr->bUseThread )*/{
94233 if( pTask->file2.pFd==0 ){
94234 assert( pTask->file2.iEof>0 )((void) (0));
94235 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
94236 pTask->file2.iEof = 0;
94237 }
94238 if( rc==SQLITE_OK0 ){
94239 pIncr->aFile[1].pFd = pTask->file2.pFd;
94240 pIncr->iStartOff = pTask->file2.iEof;
94241 pTask->file2.iEof += mxSz;
94242 }
94243 }
94244 }
94245
94246#if SQLITE_MAX_WORKER_THREADS8>0
94247 if( rc==SQLITE_OK0 && pIncr->bUseThread ){
94248 /* Use the current thread to populate aFile[1], even though this
94249 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
94250 ** then this function is already running in background thread
94251 ** pIncr->pTask->thread.
94252 **
94253 ** If this is the INCRINIT_ROOT object, then it is running in the
94254 ** main VDBE thread. But that is Ok, as that thread cannot return
94255 ** control to the VDBE or proceed with anything useful until the
94256 ** first results are ready from this merger object anyway.
94257 */
94258 assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK )((void) (0));
94259 rc = vdbeIncrPopulate(pIncr);
94260 }
94261#endif
94262
94263 if( rc==SQLITE_OK0 && (SQLITE_MAX_WORKER_THREADS8==0 || eMode!=INCRINIT_TASK1) ){
94264 rc = vdbePmaReaderNext(pReadr);
94265 }
94266
94267 return rc;
94268}
94269
94270#if SQLITE_MAX_WORKER_THREADS8>0
94271/*
94272** The main routine for vdbePmaReaderIncrMergeInit() operations run in
94273** background threads.
94274*/
94275static void *vdbePmaReaderBgIncrInit(void *pCtx){
94276 PmaReader *pReader = (PmaReader*)pCtx;
94277 void *pRet = SQLITE_INT_TO_PTR(((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)))
94278 vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK)((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)))
94279 )((void*)(long int)(vdbePmaReaderIncrMergeInit(pReader,1)));
94280 pReader->pIncr->pTask->bDone = 1;
94281 return pRet;
94282}
94283#endif
94284
94285/*
94286** If the PmaReader passed as the first argument is not an incremental-reader
94287** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
94288** the vdbePmaReaderIncrMergeInit() function with the parameters passed to
94289** this routine to initialize the incremental merge.
94290**
94291** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
94292** then a background thread is launched to call vdbePmaReaderIncrMergeInit().
94293** Or, if the IncrMerger is single threaded, the same function is called
94294** using the current thread.
94295*/
94296static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){
94297 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
94298 int rc = SQLITE_OK0; /* Return code */
94299 if( pIncr ){
94300#if SQLITE_MAX_WORKER_THREADS8>0
94301 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK )((void) (0));
94302 if( pIncr->bUseThread ){
94303 void *pCtx = (void*)pReadr;
94304 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
94305 }else
94306#endif
94307 {
94308 rc = vdbePmaReaderIncrMergeInit(pReadr, eMode);
94309 }
94310 }
94311 return rc;
94312}
94313
94314/*
94315** Allocate a new MergeEngine object to merge the contents of nPMA level-0
94316** PMAs from pTask->file. If no error occurs, set *ppOut to point to
94317** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut
94318** to NULL and return an SQLite error code.
94319**
94320** When this function is called, *piOffset is set to the offset of the
94321** first PMA to read from pTask->file. Assuming no error occurs, it is
94322** set to the offset immediately following the last byte of the last
94323** PMA before returning. If an error does occur, then the final value of
94324** *piOffset is undefined.
94325*/
94326static int vdbeMergeEngineLevel0(
94327 SortSubtask *pTask, /* Sorter task to read from */
94328 int nPMA, /* Number of PMAs to read */
94329 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
94330 MergeEngine **ppOut /* OUT: New merge-engine */
94331){
94332 MergeEngine *pNew; /* Merge engine to return */
94333 i64 iOff = *piOffset;
94334 int i;
94335 int rc = SQLITE_OK0;
94336
94337 *ppOut = pNew = vdbeMergeEngineNew(nPMA);
94338 if( pNew==0 ) rc = SQLITE_NOMEM_BKPT7;
94339
94340 for(i=0; i<nPMA && rc==SQLITE_OK0; i++){
94341 i64 nDummy = 0;
94342 PmaReader *pReadr = &pNew->aReadr[i];
94343 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
94344 iOff = pReadr->iEof;
94345 }
94346
94347 if( rc!=SQLITE_OK0 ){
94348 vdbeMergeEngineFree(pNew);
94349 *ppOut = 0;
94350 }
94351 *piOffset = iOff;
94352 return rc;
94353}
94354
94355/*
94356** Return the depth of a tree comprising nPMA PMAs, assuming a fanout of
94357** SORTER_MAX_MERGE_COUNT. The returned value does not include leaf nodes.
94358**
94359** i.e.
94360**
94361** nPMA<=16 -> TreeDepth() == 0
94362** nPMA<=256 -> TreeDepth() == 1
94363** nPMA<=65536 -> TreeDepth() == 2
94364*/
94365static int vdbeSorterTreeDepth(int nPMA){
94366 int nDepth = 0;
94367 i64 nDiv = SORTER_MAX_MERGE_COUNT16;
94368 while( nDiv < (i64)nPMA ){
94369 nDiv = nDiv * SORTER_MAX_MERGE_COUNT16;
94370 nDepth++;
94371 }
94372 return nDepth;
94373}
94374
94375/*
94376** pRoot is the root of an incremental merge-tree with depth nDepth (according
94377** to vdbeSorterTreeDepth()). pLeaf is the iSeq'th leaf to be added to the
94378** tree, counting from zero. This function adds pLeaf to the tree.
94379**
94380** If successful, SQLITE_OK is returned. If an error occurs, an SQLite error
94381** code is returned and pLeaf is freed.
94382*/
94383static int vdbeSorterAddToTree(
94384 SortSubtask *pTask, /* Task context */
94385 int nDepth, /* Depth of tree according to TreeDepth() */
94386 int iSeq, /* Sequence number of leaf within tree */
94387 MergeEngine *pRoot, /* Root of tree */
94388 MergeEngine *pLeaf /* Leaf to add to tree */
94389){
94390 int rc = SQLITE_OK0;
94391 int nDiv = 1;
94392 int i;
94393 MergeEngine *p = pRoot;
94394 IncrMerger *pIncr;
94395
94396 rc = vdbeIncrMergerNew(pTask, pLeaf, &pIncr);
94397
94398 for(i=1; i<nDepth; i++){
94399 nDiv = nDiv * SORTER_MAX_MERGE_COUNT16;
94400 }
94401
94402 for(i=1; i<nDepth && rc==SQLITE_OK0; i++){
94403 int iIter = (iSeq / nDiv) % SORTER_MAX_MERGE_COUNT16;
94404 PmaReader *pReadr = &p->aReadr[iIter];
94405
94406 if( pReadr->pIncr==0 ){
94407 MergeEngine *pNew = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT16);
94408 if( pNew==0 ){
94409 rc = SQLITE_NOMEM_BKPT7;
94410 }else{
94411 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
94412 }
94413 }
94414 if( rc==SQLITE_OK0 ){
94415 p = pReadr->pIncr->pMerger;
94416 nDiv = nDiv / SORTER_MAX_MERGE_COUNT16;
94417 }
94418 }
94419
94420 if( rc==SQLITE_OK0 ){
94421 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT16].pIncr = pIncr;
94422 }else{
94423 vdbeIncrFree(pIncr);
94424 }
94425 return rc;
94426}
94427
94428/*
94429** This function is called as part of a SorterRewind() operation on a sorter
94430** that has already written two or more level-0 PMAs to one or more temp
94431** files. It builds a tree of MergeEngine/IncrMerger/PmaReader objects that
94432** can be used to incrementally merge all PMAs on disk.
94433**
94434** If successful, SQLITE_OK is returned and *ppOut set to point to the
94435** MergeEngine object at the root of the tree before returning. Or, if an
94436** error occurs, an SQLite error code is returned and the final value
94437** of *ppOut is undefined.
94438*/
94439static int vdbeSorterMergeTreeBuild(
94440 VdbeSorter *pSorter, /* The VDBE cursor that implements the sort */
94441 MergeEngine **ppOut /* Write the MergeEngine here */
94442){
94443 MergeEngine *pMain = 0;
94444 int rc = SQLITE_OK0;
94445 int iTask;
94446
94447#if SQLITE_MAX_WORKER_THREADS8>0
94448 /* If the sorter uses more than one task, then create the top-level
94449 ** MergeEngine here. This MergeEngine will read data from exactly
94450 ** one PmaReader per sub-task. */
94451 assert( pSorter->bUseThreads || pSorter->nTask==1 )((void) (0));
94452 if( pSorter->nTask>1 ){
94453 pMain = vdbeMergeEngineNew(pSorter->nTask);
94454 if( pMain==0 ) rc = SQLITE_NOMEM_BKPT7;
94455 }
94456#endif
94457
94458 for(iTask=0; rc==SQLITE_OK0 && iTask<pSorter->nTask; iTask++){
94459 SortSubtask *pTask = &pSorter->aTask[iTask];
94460 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 )((void) (0));
94461 if( SQLITE_MAX_WORKER_THREADS8==0 || pTask->nPMA ){
94462 MergeEngine *pRoot = 0; /* Root node of tree for this task */
94463 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
94464 i64 iReadOff = 0;
94465
94466 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT16 ){
94467 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
94468 }else{
94469 int i;
94470 int iSeq = 0;
94471 pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT16);
94472 if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT7;
94473 for(i=0; i<pTask->nPMA && rc==SQLITE_OK0; i += SORTER_MAX_MERGE_COUNT16){
94474 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
94475 int nReader; /* Number of level-0 PMAs to merge */
94476
94477 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT)((pTask->nPMA - i)<(16)?(pTask->nPMA - i):(16));
94478 rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
94479 if( rc==SQLITE_OK0 ){
94480 rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
94481 }
94482 }
94483 }
94484
94485 if( rc==SQLITE_OK0 ){
94486#if SQLITE_MAX_WORKER_THREADS8>0
94487 if( pMain!=0 ){
94488 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
94489 }else
94490#endif
94491 {
94492 assert( pMain==0 )((void) (0));
94493 pMain = pRoot;
94494 }
94495 }else{
94496 vdbeMergeEngineFree(pRoot);
94497 }
94498 }
94499 }
94500
94501 if( rc!=SQLITE_OK0 ){
94502 vdbeMergeEngineFree(pMain);
94503 pMain = 0;
94504 }
94505 *ppOut = pMain;
94506 return rc;
94507}
94508
94509/*
94510** This function is called as part of an sqlite3VdbeSorterRewind() operation
94511** on a sorter that has written two or more PMAs to temporary files. It sets
94512** up either VdbeSorter.pMerger (for single threaded sorters) or pReader
94513** (for multi-threaded sorters) so that it can be used to iterate through
94514** all records stored in the sorter.
94515**
94516** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
94517*/
94518static int vdbeSorterSetupMerge(VdbeSorter *pSorter){
94519 int rc; /* Return code */
94520 SortSubtask *pTask0 = &pSorter->aTask[0];
94521 MergeEngine *pMain = 0;
94522#if SQLITE_MAX_WORKER_THREADS8
94523 sqlite3 *db = pTask0->pSorter->db;
94524 int i;
94525 SorterCompare xCompare = vdbeSorterGetCompare(pSorter);
94526 for(i=0; i<pSorter->nTask; i++){
94527 pSorter->aTask[i].xCompare = xCompare;
94528 }
94529#endif
94530
94531 rc = vdbeSorterMergeTreeBuild(pSorter, &pMain);
94532 if( rc==SQLITE_OK0 ){
94533#if SQLITE_MAX_WORKER_THREADS8
94534 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 )((void) (0));
94535 if( pSorter->bUseThreads ){
94536 int iTask;
94537 PmaReader *pReadr = 0;
94538 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
94539 rc = vdbeSortAllocUnpacked(pLast);
94540 if( rc==SQLITE_OK0 ){
94541 pReadr = (PmaReader*)sqlite3DbMallocZero(db, sizeof(PmaReader));
94542 pSorter->pReader = pReadr;
94543 if( pReadr==0 ) rc = SQLITE_NOMEM_BKPT7;
94544 }
94545 if( rc==SQLITE_OK0 ){
94546 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
94547 if( rc==SQLITE_OK0 ){
94548 vdbeIncrMergerSetThreads(pReadr->pIncr);
94549 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
94550 IncrMerger *pIncr;
94551 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
94552 vdbeIncrMergerSetThreads(pIncr);
94553 assert( pIncr->pTask!=pLast )((void) (0));
94554 }
94555 }
94556 for(iTask=0; rc==SQLITE_OK0 && iTask<pSorter->nTask; iTask++){
94557 /* Check that:
94558 **
94559 ** a) The incremental merge object is configured to use the
94560 ** right task, and
94561 ** b) If it is using task (nTask-1), it is configured to run
94562 ** in single-threaded mode. This is important, as the
94563 ** root merge (INCRINIT_ROOT) will be using the same task
94564 ** object.
94565 */
94566 PmaReader *p = &pMain->aReadr[iTask];
94567 assert( p->pIncr==0 || (((void) (0))
94568 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */((void) (0))
94569 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */((void) (0))
94570 ))((void) (0));
94571 rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK1);
94572 }
94573 }
94574 pMain = 0;
94575 }
94576 if( rc==SQLITE_OK0 ){
94577 rc = vdbePmaReaderIncrMergeInit(pReadr, INCRINIT_ROOT2);
94578 }
94579 }else
94580#endif
94581 {
94582 rc = vdbeMergeEngineInit(pTask0, pMain, INCRINIT_NORMAL0);
94583 pSorter->pMerger = pMain;
94584 pMain = 0;
94585 }
94586 }
94587
94588 if( rc!=SQLITE_OK0 ){
94589 vdbeMergeEngineFree(pMain);
94590 }
94591 return rc;
94592}
94593
94594
94595/*
94596** Once the sorter has been populated by calls to sqlite3VdbeSorterWrite,
94597** this function is called to prepare for iterating through the records
94598** in sorted order.
94599*/
94600SQLITE_PRIVATEstatic int sqlite3VdbeSorterRewind(const VdbeCursor *pCsr, int *pbEof){
94601 VdbeSorter *pSorter;
94602 int rc = SQLITE_OK0; /* Return code */
94603
94604 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
94605 pSorter = pCsr->uc.pSorter;
94606 assert( pSorter )((void) (0));
94607
94608 /* If no data has been written to disk, then do not do so now. Instead,
94609 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
94610 ** from the in-memory list. */
94611 if( pSorter->bUsePMA==0 ){
94612 if( pSorter->list.pList ){
94613 *pbEof = 0;
94614 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
94615 }else{
94616 *pbEof = 1;
94617 }
94618 return rc;
94619 }
94620
94621 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
94622 ** function flushes the contents of memory to disk, it immediately always
94623 ** creates a new list consisting of a single key immediately afterwards.
94624 ** So the list is never empty at this point. */
94625 assert( pSorter->list.pList )((void) (0));
94626 rc = vdbeSorterFlushPMA(pSorter);
94627
94628 /* Join all threads */
94629 rc = vdbeSorterJoinAll(pSorter, rc);
94630
94631 vdbeSorterRewindDebug("rewind");
94632
94633 /* Assuming no errors have occurred, set up a merger structure to
94634 ** incrementally read and merge all remaining PMAs. */
94635 assert( pSorter->pReader==0 )((void) (0));
94636 if( rc==SQLITE_OK0 ){
94637 rc = vdbeSorterSetupMerge(pSorter);
94638 *pbEof = 0;
94639 }
94640
94641 vdbeSorterRewindDebug("rewinddone");
94642 return rc;
94643}
94644
94645/*
94646** Advance to the next element in the sorter. Return value:
94647**
94648** SQLITE_OK success
94649** SQLITE_DONE end of data
94650** otherwise some kind of error.
94651*/
94652SQLITE_PRIVATEstatic int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr){
94653 VdbeSorter *pSorter;
94654 int rc; /* Return code */
94655
94656 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
94657 pSorter = pCsr->uc.pSorter;
94658 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) )((void) (0));
94659 if( pSorter->bUsePMA ){
94660 assert( pSorter->pReader==0 || pSorter->pMerger==0 )((void) (0));
94661 assert( pSorter->bUseThreads==0 || pSorter->pReader )((void) (0));
94662 assert( pSorter->bUseThreads==1 || pSorter->pMerger )((void) (0));
94663#if SQLITE_MAX_WORKER_THREADS8>0
94664 if( pSorter->bUseThreads ){
94665 rc = vdbePmaReaderNext(pSorter->pReader);
94666 if( rc==SQLITE_OK0 && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE101;
94667 }else
94668#endif
94669 /*if( !pSorter->bUseThreads )*/ {
94670 int res = 0;
94671 assert( pSorter->pMerger!=0 )((void) (0));
94672 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) )((void) (0));
94673 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
94674 if( rc==SQLITE_OK0 && res ) rc = SQLITE_DONE101;
94675 }
94676 }else{
94677 SorterRecord *pFree = pSorter->list.pList;
94678 pSorter->list.pList = pFree->u.pNext;
94679 pFree->u.pNext = 0;
94680 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
94681 rc = pSorter->list.pList ? SQLITE_OK0 : SQLITE_DONE101;
94682 }
94683 return rc;
94684}
94685
94686/*
94687** Return a pointer to a buffer owned by the sorter that contains the
94688** current key.
94689*/
94690static void *vdbeSorterRowkey(
94691 const VdbeSorter *pSorter, /* Sorter object */
94692 int *pnKey /* OUT: Size of current key in bytes */
94693){
94694 void *pKey;
94695 if( pSorter->bUsePMA ){
94696 PmaReader *pReader;
94697#if SQLITE_MAX_WORKER_THREADS8>0
94698 if( pSorter->bUseThreads ){
94699 pReader = pSorter->pReader;
94700 }else
94701#endif
94702 /*if( !pSorter->bUseThreads )*/{
94703 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
94704 }
94705 *pnKey = pReader->nKey;
94706 pKey = pReader->aKey;
94707 }else{
94708 *pnKey = pSorter->list.pList->nVal;
94709 pKey = SRVAL(pSorter->list.pList)((void*)((SorterRecord*)(pSorter->list.pList) + 1));
94710 }
94711 return pKey;
94712}
94713
94714/*
94715** Copy the current sorter key into the memory cell pOut.
94716*/
94717SQLITE_PRIVATEstatic int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
94718 VdbeSorter *pSorter;
94719 void *pKey; int nKey; /* Sorter key to copy into pOut */
94720
94721 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
94722 pSorter = pCsr->uc.pSorter;
94723 pKey = vdbeSorterRowkey(pSorter, &nKey);
94724 if( sqlite3VdbeMemClearAndResize(pOut, nKey) ){
94725 return SQLITE_NOMEM_BKPT7;
94726 }
94727 pOut->n = nKey;
94728 MemSetTypeFlag(pOut, MEM_Blob)((pOut)->flags = ((pOut)->flags&~(0xc1bf|0x4000))|0x0010
)
;
94729 memcpy(pOut->z, pKey, nKey);
94730
94731 return SQLITE_OK0;
94732}
94733
94734/*
94735** Compare the key in memory cell pVal with the key that the sorter cursor
94736** passed as the first argument currently points to. For the purposes of
94737** the comparison, ignore the rowid field at the end of each record.
94738**
94739** If the sorter cursor key contains any NULL values, consider it to be
94740** less than pVal. Even if pVal also contains NULL values.
94741**
94742** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
94743** Otherwise, set *pRes to a negative, zero or positive value if the
94744** key in pVal is smaller than, equal to or larger than the current sorter
94745** key.
94746**
94747** This routine forms the core of the OP_SorterCompare opcode, which in
94748** turn is used to verify uniqueness when constructing a UNIQUE INDEX.
94749*/
94750SQLITE_PRIVATEstatic int sqlite3VdbeSorterCompare(
94751 const VdbeCursor *pCsr, /* Sorter cursor */
94752 Mem *pVal, /* Value to compare to current sorter key */
94753 int nKeyCol, /* Compare this many columns */
94754 int *pRes /* OUT: Result of comparison */
94755){
94756 VdbeSorter *pSorter;
94757 UnpackedRecord *r2;
94758 KeyInfo *pKeyInfo;
94759 int i;
94760 void *pKey; int nKey; /* Sorter key to compare pVal with */
94761
94762 assert( pCsr->eCurType==CURTYPE_SORTER )((void) (0));
94763 pSorter = pCsr->uc.pSorter;
94764 r2 = pSorter->pUnpacked;
94765 pKeyInfo = pCsr->pKeyInfo;
94766 if( r2==0 ){
94767 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
94768 if( r2==0 ) return SQLITE_NOMEM_BKPT7;
94769 r2->nField = nKeyCol;
94770 }
94771 assert( r2->nField==nKeyCol )((void) (0));
94772
94773 pKey = vdbeSorterRowkey(pSorter, &nKey);
94774 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, r2);
94775 for(i=0; i<nKeyCol; i++){
94776 if( r2->aMem[i].flags & MEM_Null0x0001 ){
94777 *pRes = -1;
94778 return SQLITE_OK0;
94779 }
94780 }
94781
94782 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
94783 return SQLITE_OK0;
94784}
94785
94786/************** End of vdbesort.c ********************************************/
94787/************** Begin file memjournal.c **************************************/
94788/*
94789** 2008 October 7
94790**
94791** The author disclaims copyright to this source code. In place of
94792** a legal notice, here is a blessing:
94793**
94794** May you do good and not evil.
94795** May you find forgiveness for yourself and forgive others.
94796** May you share freely, never taking more than you give.
94797**
94798*************************************************************************
94799**
94800** This file contains code use to implement an in-memory rollback journal.
94801** The in-memory rollback journal is used to journal transactions for
94802** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
94803**
94804** Update: The in-memory journal is also used to temporarily cache
94805** smaller journals that are not critical for power-loss recovery.
94806** For example, statement journals that are not too big will be held
94807** entirely in memory, thus reducing the number of file I/O calls, and
94808** more importantly, reducing temporary file creation events. If these
94809** journals become too large for memory, they are spilled to disk. But
94810** in the common case, they are usually small and no file I/O needs to
94811** occur.
94812*/
94813/* #include "sqliteInt.h" */
94814
94815/* Forward references to internal structures */
94816typedef struct MemJournal MemJournal;
94817typedef struct FilePoint FilePoint;
94818typedef struct FileChunk FileChunk;
94819
94820/*
94821** The rollback journal is composed of a linked list of these structures.
94822**
94823** The zChunk array is always at least 8 bytes in size - usually much more.
94824** Its actual size is stored in the MemJournal.nChunkSize variable.
94825*/
94826struct FileChunk {
94827 FileChunk *pNext; /* Next chunk in the journal */
94828 u8 zChunk[8]; /* Content of this chunk */
94829};
94830
94831/*
94832** By default, allocate this many bytes of memory for each FileChunk object.
94833*/
94834#define MEMJOURNAL_DFLT_FILECHUNKSIZE1024 1024
94835
94836/*
94837** For chunk size nChunkSize, return the number of bytes that should
94838** be allocated for each FileChunk structure.
94839*/
94840#define fileChunkSize(nChunkSize)(sizeof(FileChunk) + ((nChunkSize)-8)) (sizeof(FileChunk) + ((nChunkSize)-8))
94841
94842/*
94843** An instance of this object serves as a cursor into the rollback journal.
94844** The cursor can be either for reading or writing.
94845*/
94846struct FilePoint {
94847 sqlite3_int64 iOffset; /* Offset from the beginning of the file */
94848 FileChunk *pChunk; /* Specific chunk into which cursor points */
94849};
94850
94851/*
94852** This structure is a subclass of sqlite3_file. Each open memory-journal
94853** is an instance of this class.
94854*/
94855struct MemJournal {
94856 const sqlite3_io_methods *pMethod; /* Parent class. MUST BE FIRST */
94857 int nChunkSize; /* In-memory chunk-size */
94858
94859 int nSpill; /* Bytes of data before flushing */
94860 int nSize; /* Bytes of data currently in memory */
94861 FileChunk *pFirst; /* Head of in-memory chunk-list */
94862 FilePoint endpoint; /* Pointer to the end of the file */
94863 FilePoint readpoint; /* Pointer to the end of the last xRead() */
94864
94865 int flags; /* xOpen flags */
94866 sqlite3_vfs *pVfs; /* The "real" underlying VFS */
94867 const char *zJournal; /* Name of the journal file */
94868};
94869
94870/*
94871** Read data from the in-memory journal file. This is the implementation
94872** of the sqlite3_vfs.xRead method.
94873*/
94874static int memjrnlRead(
94875 sqlite3_file *pJfd, /* The journal file from which to read */
94876 void *zBuf, /* Put the results here */
94877 int iAmt, /* Number of bytes to read */
94878 sqlite_int64 iOfst /* Begin reading at this offset */
94879){
94880 MemJournal *p = (MemJournal *)pJfd;
94881 u8 *zOut = zBuf;
94882 int nRead = iAmt;
94883 int iChunkOffset;
94884 FileChunk *pChunk;
94885
94886#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
94887 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
94888 if( (iAmt+iOfst)>p->endpoint.iOffset ){
94889 return SQLITE_IOERR_SHORT_READ(10 | (2<<8));
94890 }
94891#endif
94892
94893 assert( (iAmt+iOfst)<=p->endpoint.iOffset )((void) (0));
94894 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 )((void) (0));
94895 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
94896 sqlite3_int64 iOff = 0;
94897 for(pChunk=p->pFirst;
94898 ALWAYS(pChunk)(pChunk) && (iOff+p->nChunkSize)<=iOfst;
94899 pChunk=pChunk->pNext
94900 ){
94901 iOff += p->nChunkSize;
94902 }
94903 }else{
94904 pChunk = p->readpoint.pChunk;
94905 assert( pChunk!=0 )((void) (0));
94906 }
94907
94908 iChunkOffset = (int)(iOfst%p->nChunkSize);
94909 do {
94910 int iSpace = p->nChunkSize - iChunkOffset;
94911 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset))((nRead)<((p->nChunkSize - iChunkOffset))?(nRead):((p->
nChunkSize - iChunkOffset)))
;
94912 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
94913 zOut += nCopy;
94914 nRead -= iSpace;
94915 iChunkOffset = 0;
94916 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
94917 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
94918 p->readpoint.pChunk = pChunk;
94919
94920 return SQLITE_OK0;
94921}
94922
94923/*
94924** Free the list of FileChunk structures headed at MemJournal.pFirst.
94925*/
94926static void memjrnlFreeChunks(MemJournal *p){
94927 FileChunk *pIter;
94928 FileChunk *pNext;
94929 for(pIter=p->pFirst; pIter; pIter=pNext){
94930 pNext = pIter->pNext;
94931 sqlite3_free(pIter);
94932 }
94933 p->pFirst = 0;
94934}
94935
94936/*
94937** Flush the contents of memory to a real file on disk.
94938*/
94939static int memjrnlCreateFile(MemJournal *p){
94940 int rc;
94941 sqlite3_file *pReal = (sqlite3_file*)p;
94942 MemJournal copy = *p;
94943
94944 memset(p, 0, sizeof(MemJournal));
94945 rc = sqlite3OsOpen(copy.pVfs, copy.zJournal, pReal, copy.flags, 0);
94946 if( rc==SQLITE_OK0 ){
94947 int nChunk = copy.nChunkSize;
94948 i64 iOff = 0;
94949 FileChunk *pIter;
94950 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
94951 if( iOff + nChunk > copy.endpoint.iOffset ){
94952 nChunk = copy.endpoint.iOffset - iOff;
94953 }
94954 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
94955 if( rc ) break;
94956 iOff += nChunk;
94957 }
94958 if( rc==SQLITE_OK0 ){
94959 /* No error has occurred. Free the in-memory buffers. */
94960 memjrnlFreeChunks(&copy);
94961 }
94962 }
94963 if( rc!=SQLITE_OK0 ){
94964 /* If an error occurred while creating or writing to the file, restore
94965 ** the original before returning. This way, SQLite uses the in-memory
94966 ** journal data to roll back changes made to the internal page-cache
94967 ** before this function was called. */
94968 sqlite3OsClose(pReal);
94969 *p = copy;
94970 }
94971 return rc;
94972}
94973
94974
94975/*
94976** Write data to the file.
94977*/
94978static int memjrnlWrite(
94979 sqlite3_file *pJfd, /* The journal file into which to write */
94980 const void *zBuf, /* Take data to be written from here */
94981 int iAmt, /* Number of bytes to write */
94982 sqlite_int64 iOfst /* Begin writing at this offset into the file */
94983){
94984 MemJournal *p = (MemJournal *)pJfd;
94985 int nWrite = iAmt;
94986 u8 *zWrite = (u8 *)zBuf;
94987
94988 /* If the file should be created now, create it and write the new data
94989 ** into the file on disk. */
94990 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
94991 int rc = memjrnlCreateFile(p);
94992 if( rc==SQLITE_OK0 ){
94993 rc = sqlite3OsWrite(pJfd, zBuf, iAmt, iOfst);
94994 }
94995 return rc;
94996 }
94997
94998 /* If the contents of this write should be stored in memory */
94999 else{
95000 /* An in-memory journal file should only ever be appended to. Random
95001 ** access writes are not required. The only exception to this is when
95002 ** the in-memory journal is being used by a connection using the
95003 ** atomic-write optimization. In this case the first 28 bytes of the
95004 ** journal file may be written as part of committing the transaction. */
95005 assert( iOfst==p->endpoint.iOffset || iOfst==0 )((void) (0));
95006#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
95007 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
95008 if( iOfst==0 && p->pFirst ){
95009 assert( p->nChunkSize>iAmt )((void) (0));
95010 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
95011 }else
95012#else
95013 assert( iOfst>0 || p->pFirst==0 )((void) (0));
95014#endif
95015 {
95016 while( nWrite>0 ){
95017 FileChunk *pChunk = p->endpoint.pChunk;
95018 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
95019 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset)((nWrite)<(p->nChunkSize - iChunkOffset)?(nWrite):(p->
nChunkSize - iChunkOffset))
;
95020
95021 if( iChunkOffset==0 ){
95022 /* New chunk is required to extend the file. */
95023 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize)(sizeof(FileChunk) + ((p->nChunkSize)-8)));
95024 if( !pNew ){
95025 return SQLITE_IOERR_NOMEM_BKPT(10 | (12<<8));
95026 }
95027 pNew->pNext = 0;
95028 if( pChunk ){
95029 assert( p->pFirst )((void) (0));
95030 pChunk->pNext = pNew;
95031 }else{
95032 assert( !p->pFirst )((void) (0));
95033 p->pFirst = pNew;
95034 }
95035 p->endpoint.pChunk = pNew;
95036 }
95037
95038 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
95039 zWrite += iSpace;
95040 nWrite -= iSpace;
95041 p->endpoint.iOffset += iSpace;
95042 }
95043 p->nSize = iAmt + iOfst;
95044 }
95045 }
95046
95047 return SQLITE_OK0;
95048}
95049
95050/*
95051** Truncate the file.
95052**
95053** If the journal file is already on disk, truncate it there. Or, if it
95054** is still in main memory but is being truncated to zero bytes in size,
95055** ignore
95056*/
95057static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
95058 MemJournal *p = (MemJournal *)pJfd;
95059 if( ALWAYS(size==0)(size==0) ){
95060 memjrnlFreeChunks(p);
95061 p->nSize = 0;
95062 p->endpoint.pChunk = 0;
95063 p->endpoint.iOffset = 0;
95064 p->readpoint.pChunk = 0;
95065 p->readpoint.iOffset = 0;
95066 }
95067 return SQLITE_OK0;
95068}
95069
95070/*
95071** Close the file.
95072*/
95073static int memjrnlClose(sqlite3_file *pJfd){
95074 MemJournal *p = (MemJournal *)pJfd;
95075 memjrnlFreeChunks(p);
95076 return SQLITE_OK0;
95077}
95078
95079/*
95080** Sync the file.
95081**
95082** If the real file has been created, call its xSync method. Otherwise,
95083** syncing an in-memory journal is a no-op.
95084*/
95085static int memjrnlSync(sqlite3_file *pJfd, int flags){
95086 UNUSED_PARAMETER2(pJfd, flags)(void)(pJfd),(void)(flags);
95087 return SQLITE_OK0;
95088}
95089
95090/*
95091** Query the size of the file in bytes.
95092*/
95093static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
95094 MemJournal *p = (MemJournal *)pJfd;
95095 *pSize = (sqlite_int64) p->endpoint.iOffset;
95096 return SQLITE_OK0;
95097}
95098
95099/*
95100** Table of methods for MemJournal sqlite3_file object.
95101*/
95102static const struct sqlite3_io_methods MemJournalMethods = {
95103 1, /* iVersion */
95104 memjrnlClose, /* xClose */
95105 memjrnlRead, /* xRead */
95106 memjrnlWrite, /* xWrite */
95107 memjrnlTruncate, /* xTruncate */
95108 memjrnlSync, /* xSync */
95109 memjrnlFileSize, /* xFileSize */
95110 0, /* xLock */
95111 0, /* xUnlock */
95112 0, /* xCheckReservedLock */
95113 0, /* xFileControl */
95114 0, /* xSectorSize */
95115 0, /* xDeviceCharacteristics */
95116 0, /* xShmMap */
95117 0, /* xShmLock */
95118 0, /* xShmBarrier */
95119 0, /* xShmUnmap */
95120 0, /* xFetch */
95121 0 /* xUnfetch */
95122};
95123
95124/*
95125** Open a journal file.
95126**
95127** The behaviour of the journal file depends on the value of parameter
95128** nSpill. If nSpill is 0, then the journal file is always create and
95129** accessed using the underlying VFS. If nSpill is less than zero, then
95130** all content is always stored in main-memory. Finally, if nSpill is a
95131** positive value, then the journal file is initially created in-memory
95132** but may be flushed to disk later on. In this case the journal file is
95133** flushed to disk either when it grows larger than nSpill bytes in size,
95134** or when sqlite3JournalCreate() is called.
95135*/
95136SQLITE_PRIVATEstatic int sqlite3JournalOpen(
95137 sqlite3_vfs *pVfs, /* The VFS to use for actual file I/O */
95138 const char *zName, /* Name of the journal file */
95139 sqlite3_file *pJfd, /* Preallocated, blank file handle */
95140 int flags, /* Opening flags */
95141 int nSpill /* Bytes buffered before opening the file */
95142){
95143 MemJournal *p = (MemJournal*)pJfd;
95144
95145 /* Zero the file-handle object. If nSpill was passed zero, initialize
95146 ** it using the sqlite3OsOpen() function of the underlying VFS. In this
95147 ** case none of the code in this module is executed as a result of calls
95148 ** made on the journal file-handle. */
95149 memset(p, 0, sizeof(MemJournal));
95150 if( nSpill==0 ){
95151 return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
95152 }
95153
95154 if( nSpill>0 ){
95155 p->nChunkSize = nSpill;
95156 }else{
95157 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE1024 - sizeof(FileChunk);
95158 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) )((void) (0));
95159 }
95160
95161 p->pMethod = (const sqlite3_io_methods*)&MemJournalMethods;
95162 p->nSpill = nSpill;
95163 p->flags = flags;
95164 p->zJournal = zName;
95165 p->pVfs = pVfs;
95166 return SQLITE_OK0;
95167}
95168
95169/*
95170** Open an in-memory journal file.
95171*/
95172SQLITE_PRIVATEstatic void sqlite3MemJournalOpen(sqlite3_file *pJfd){
95173 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
95174}
95175
95176#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
95177 || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
95178/*
95179** If the argument p points to a MemJournal structure that is not an
95180** in-memory-only journal file (i.e. is one that was opened with a +ve
95181** nSpill parameter or as SQLITE_OPEN_MAIN_JOURNAL), and the underlying
95182** file has not yet been created, create it now.
95183*/
95184SQLITE_PRIVATEstatic int sqlite3JournalCreate(sqlite3_file *pJfd){
95185 int rc = SQLITE_OK0;
95186 MemJournal *p = (MemJournal*)pJfd;
95187 if( p->pMethod==&MemJournalMethods && (
95188#ifdef SQLITE_ENABLE_ATOMIC_WRITE
95189 p->nSpill>0
95190#else
95191 /* While this appears to not be possible without ATOMIC_WRITE, the
95192 ** paths are complex, so it seems prudent to leave the test in as
95193 ** a NEVER(), in case our analysis is subtly flawed. */
95194 NEVER(p->nSpill>0)(p->nSpill>0)
95195#endif
95196#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
95197 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL0x00000800)
95198#endif
95199 )){
95200 rc = memjrnlCreateFile(p);
95201 }
95202 return rc;
95203}
95204#endif
95205
95206/*
95207** The file-handle passed as the only argument is open on a journal file.
95208** Return true if this "journal file" is currently stored in heap memory,
95209** or false otherwise.
95210*/
95211SQLITE_PRIVATEstatic int sqlite3JournalIsInMemory(sqlite3_file *p){
95212 return p->pMethods==&MemJournalMethods;
95213}
95214
95215/*
95216** Return the number of bytes required to store a JournalFile that uses vfs
95217** pVfs to create the underlying on-disk files.
95218*/
95219SQLITE_PRIVATEstatic int sqlite3JournalSize(sqlite3_vfs *pVfs){
95220 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal))((pVfs->szOsFile)>((int)sizeof(MemJournal))?(pVfs->szOsFile
):((int)sizeof(MemJournal)))
;
95221}
95222
95223/************** End of memjournal.c ******************************************/
95224/************** Begin file walker.c ******************************************/
95225/*
95226** 2008 August 16
95227**
95228** The author disclaims copyright to this source code. In place of
95229** a legal notice, here is a blessing:
95230**
95231** May you do good and not evil.
95232** May you find forgiveness for yourself and forgive others.
95233** May you share freely, never taking more than you give.
95234**
95235*************************************************************************
95236** This file contains routines used for walking the parser tree for
95237** an SQL statement.
95238*/
95239/* #include "sqliteInt.h" */
95240/* #include <stdlib.h> */
95241/* #include <string.h> */
95242
95243
95244#if !defined(SQLITE_OMIT_WINDOWFUNC)
95245/*
95246** Walk all expressions linked into the list of Window objects passed
95247** as the second argument.
95248*/
95249static int walkWindowList(Walker *pWalker, Window *pList){
95250 Window *pWin;
95251 for(pWin=pList; pWin; pWin=pWin->pNextWin){
95252 if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort2;
95253 if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort2;
95254 if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort2;
95255 }
95256 return WRC_Continue0;
95257}
95258#endif
95259
95260/*
95261** Walk an expression tree. Invoke the callback once for each node
95262** of the expression, while descending. (In other words, the callback
95263** is invoked before visiting children.)
95264**
95265** The return value from the callback should be one of the WRC_*
95266** constants to specify how to proceed with the walk.
95267**
95268** WRC_Continue Continue descending down the tree.
95269**
95270** WRC_Prune Do not descend into child nodes, but allow
95271** the walk to continue with sibling nodes.
95272**
95273** WRC_Abort Do no more callbacks. Unwind the stack and
95274** return from the top-level walk call.
95275**
95276** The return value from this routine is WRC_Abort to abandon the tree walk
95277** and WRC_Continue to continue.
95278*/
95279static SQLITE_NOINLINE__attribute__((noinline)) int walkExpr(Walker *pWalker, Expr *pExpr){
95280 int rc;
95281 testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
95282 testcase( ExprHasProperty(pExpr, EP_Reduced) );
95283 while(1){
95284 rc = pWalker->xExprCallback(pWalker, pExpr);
95285 if( rc ) return rc & WRC_Abort2;
95286 if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf))(((pExpr)->flags&((0x004000|0x800000)))!=0) ){
95287 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort2;
95288 assert( pExpr->x.pList==0 || pExpr->pRight==0 )((void) (0));
95289 if( pExpr->pRight ){
95290 pExpr = pExpr->pRight;
95291 continue;
95292 }else if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
95293 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort2;
95294 }else if( pExpr->x.pList ){
95295 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort2;
95296 }
95297#ifndef SQLITE_OMIT_WINDOWFUNC
95298 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
95299 if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort2;
95300 }
95301#endif
95302 }
95303 break;
95304 }
95305 return WRC_Continue0;
95306}
95307SQLITE_PRIVATEstatic int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
95308 return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue0;
95309}
95310
95311/*
95312** Call sqlite3WalkExpr() for every expression in list p or until
95313** an abort request is seen.
95314*/
95315SQLITE_PRIVATEstatic int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
95316 int i;
95317 struct ExprList_item *pItem;
95318 if( p ){
95319 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
95320 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort2;
95321 }
95322 }
95323 return WRC_Continue0;
95324}
95325
95326/*
95327** Walk all expressions associated with SELECT statement p. Do
95328** not invoke the SELECT callback on p, but do (of course) invoke
95329** any expr callbacks and SELECT callbacks that come from subqueries.
95330** Return WRC_Abort or WRC_Continue.
95331*/
95332SQLITE_PRIVATEstatic int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
95333 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort2;
95334 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort2;
95335 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort2;
95336 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort2;
95337 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort2;
95338 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort2;
95339#if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE)
95340 {
95341 Parse *pParse = pWalker->pParse;
95342 if( pParse && IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
95343 int rc = walkWindowList(pWalker, p->pWinDefn);
95344 assert( rc==WRC_Continue )((void) (0));
95345 return rc;
95346 }
95347 }
95348#endif
95349 return WRC_Continue0;
95350}
95351
95352/*
95353** Walk the parse trees associated with all subqueries in the
95354** FROM clause of SELECT statement p. Do not invoke the select
95355** callback on p, but do invoke it on each FROM clause subquery
95356** and on any subqueries further down in the tree. Return
95357** WRC_Abort or WRC_Continue;
95358*/
95359SQLITE_PRIVATEstatic int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
95360 SrcList *pSrc;
95361 int i;
95362 struct SrcList_item *pItem;
95363
95364 pSrc = p->pSrc;
95365 assert( pSrc!=0 )((void) (0));
95366 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
95367 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
95368 return WRC_Abort2;
95369 }
95370 if( pItem->fg.isTabFunc
95371 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
95372 ){
95373 return WRC_Abort2;
95374 }
95375 }
95376 return WRC_Continue0;
95377}
95378
95379/*
95380** Call sqlite3WalkExpr() for every expression in Select statement p.
95381** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
95382** on the compound select chain, p->pPrior.
95383**
95384** If it is not NULL, the xSelectCallback() callback is invoked before
95385** the walk of the expressions and FROM clause. The xSelectCallback2()
95386** method is invoked following the walk of the expressions and FROM clause,
95387** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
95388** and if the expressions and FROM clause both return WRC_Continue;
95389**
95390** Return WRC_Continue under normal conditions. Return WRC_Abort if
95391** there is an abort request.
95392**
95393** If the Walker does not have an xSelectCallback() then this routine
95394** is a no-op returning WRC_Continue.
95395*/
95396SQLITE_PRIVATEstatic int sqlite3WalkSelect(Walker *pWalker, Select *p){
95397 int rc;
95398 if( p==0 ) return WRC_Continue0;
95399 if( pWalker->xSelectCallback==0 ) return WRC_Continue0;
95400 do{
95401 rc = pWalker->xSelectCallback(pWalker, p);
95402 if( rc ) return rc & WRC_Abort2;
95403 if( sqlite3WalkSelectExpr(pWalker, p)
95404 || sqlite3WalkSelectFrom(pWalker, p)
95405 ){
95406 return WRC_Abort2;
95407 }
95408 if( pWalker->xSelectCallback2 ){
95409 pWalker->xSelectCallback2(pWalker, p);
95410 }
95411 p = p->pPrior;
95412 }while( p!=0 );
95413 return WRC_Continue0;
95414}
95415
95416/************** End of walker.c **********************************************/
95417/************** Begin file resolve.c *****************************************/
95418/*
95419** 2008 August 18
95420**
95421** The author disclaims copyright to this source code. In place of
95422** a legal notice, here is a blessing:
95423**
95424** May you do good and not evil.
95425** May you find forgiveness for yourself and forgive others.
95426** May you share freely, never taking more than you give.
95427**
95428*************************************************************************
95429**
95430** This file contains routines used for walking the parser tree and
95431** resolve all identifiers by associating them with a particular
95432** table and column.
95433*/
95434/* #include "sqliteInt.h" */
95435
95436/*
95437** Walk the expression tree pExpr and increase the aggregate function
95438** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
95439** This needs to occur when copying a TK_AGG_FUNCTION node from an
95440** outer query into an inner subquery.
95441**
95442** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
95443** is a helper function - a callback for the tree walker.
95444*/
95445static int incrAggDepth(Walker *pWalker, Expr *pExpr){
95446 if( pExpr->op==TK_AGG_FUNCTION163 ) pExpr->op2 += pWalker->u.n;
95447 return WRC_Continue0;
95448}
95449static void incrAggFunctionDepth(Expr *pExpr, int N){
95450 if( N>0 ){
95451 Walker w;
95452 memset(&w, 0, sizeof(w));
95453 w.xExprCallback = incrAggDepth;
95454 w.u.n = N;
95455 sqlite3WalkExpr(&w, pExpr);
95456 }
95457}
95458
95459/*
95460** Turn the pExpr expression into an alias for the iCol-th column of the
95461** result set in pEList.
95462**
95463** If the reference is followed by a COLLATE operator, then make sure
95464** the COLLATE operator is preserved. For example:
95465**
95466** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
95467**
95468** Should be transformed into:
95469**
95470** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
95471**
95472** The nSubquery parameter specifies how many levels of subquery the
95473** alias is removed from the original expression. The usual value is
95474** zero but it might be more if the alias is contained within a subquery
95475** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
95476** structures must be increased by the nSubquery amount.
95477*/
95478static void resolveAlias(
95479 Parse *pParse, /* Parsing context */
95480 ExprList *pEList, /* A result set */
95481 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
95482 Expr *pExpr, /* Transform this into an alias to the result set */
95483 const char *zType, /* "GROUP" or "ORDER" or "" */
95484 int nSubquery /* Number of subqueries that the label is moving */
95485){
95486 Expr *pOrig; /* The iCol-th column of the result set */
95487 Expr *pDup; /* Copy of pOrig */
95488 sqlite3 *db; /* The database connection */
95489
95490 assert( iCol>=0 && iCol<pEList->nExpr )((void) (0));
95491 pOrig = pEList->a[iCol].pExpr;
95492 assert( pOrig!=0 )((void) (0));
95493 db = pParse->db;
95494 pDup = sqlite3ExprDup(db, pOrig, 0);
95495 if( pDup!=0 ){
95496 if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
95497 if( pExpr->op==TK_COLLATE106 ){
95498 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
95499 }
95500
95501 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
95502 ** prevents ExprDelete() from deleting the Expr structure itself,
95503 ** allowing it to be repopulated by the memcpy() on the following line.
95504 ** The pExpr->u.zToken might point into memory that will be freed by the
95505 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
95506 ** make a copy of the token before doing the sqlite3DbFree().
95507 */
95508 ExprSetProperty(pExpr, EP_Static)(pExpr)->flags|=(0x8000000);
95509 sqlite3ExprDelete(db, pExpr);
95510 memcpy(pExpr, pDup, sizeof(*pExpr));
95511 if( !ExprHasProperty(pExpr, EP_IntValue)(((pExpr)->flags&(0x000400))!=0) && pExpr->u.zToken!=0 ){
95512 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 )((void) (0));
95513 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
95514 pExpr->flags |= EP_MemToken0x010000;
95515 }
95516 sqlite3DbFree(db, pDup);
95517 }
95518 ExprSetProperty(pExpr, EP_Alias)(pExpr)->flags|=(0x400000);
95519}
95520
95521
95522/*
95523** Return TRUE if the name zCol occurs anywhere in the USING clause.
95524**
95525** Return FALSE if the USING clause is NULL or if it does not contain
95526** zCol.
95527*/
95528static int nameInUsingClause(IdList *pUsing, const char *zCol){
95529 if( pUsing ){
95530 int k;
95531 for(k=0; k<pUsing->nId; k++){
95532 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
95533 }
95534 }
95535 return 0;
95536}
95537
95538/*
95539** Subqueries stores the original database, table and column names for their
95540** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
95541** Check to see if the zSpan given to this routine matches the zDb, zTab,
95542** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
95543** match anything.
95544*/
95545SQLITE_PRIVATEstatic int sqlite3MatchSpanName(
95546 const char *zSpan,
95547 const char *zCol,
95548 const char *zTab,
95549 const char *zDb
95550){
95551 int n;
95552 for(n=0; ALWAYS(zSpan[n])(zSpan[n]) && zSpan[n]!='.'; n++){}
95553 if( zDb && (sqlite3StrNICmpsqlite3_strnicmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
95554 return 0;
95555 }
95556 zSpan += n+1;
95557 for(n=0; ALWAYS(zSpan[n])(zSpan[n]) && zSpan[n]!='.'; n++){}
95558 if( zTab && (sqlite3StrNICmpsqlite3_strnicmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
95559 return 0;
95560 }
95561 zSpan += n+1;
95562 if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
95563 return 0;
95564 }
95565 return 1;
95566}
95567
95568/*
95569** Return TRUE if the double-quoted string mis-feature should be supported.
95570*/
95571static int areDoubleQuotedStringsEnabled(sqlite3 *db, NameContext *pTopNC){
95572 if( db->init.busy ) return 1; /* Always support for legacy schemas */
95573 if( pTopNC->ncFlags & NC_IsDDL0x10000 ){
95574 /* Currently parsing a DDL statement */
95575 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML0x40000000)!=0 ){
95576 return 1;
95577 }
95578 return (db->flags & SQLITE_DqsDDL0x20000000)!=0;
95579 }else{
95580 /* Currently parsing a DML statement */
95581 return (db->flags & SQLITE_DqsDML0x40000000)!=0;
95582 }
95583}
95584
95585/*
95586** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
95587** that name in the set of source tables in pSrcList and make the pExpr
95588** expression node refer back to that source column. The following changes
95589** are made to pExpr:
95590**
95591** pExpr->iDb Set the index in db->aDb[] of the database X
95592** (even if X is implied).
95593** pExpr->iTable Set to the cursor number for the table obtained
95594** from pSrcList.
95595** pExpr->y.pTab Points to the Table structure of X.Y (even if
95596** X and/or Y are implied.)
95597** pExpr->iColumn Set to the column number within the table.
95598** pExpr->op Set to TK_COLUMN.
95599** pExpr->pLeft Any expression this points to is deleted
95600** pExpr->pRight Any expression this points to is deleted.
95601**
95602** The zDb variable is the name of the database (the "X"). This value may be
95603** NULL meaning that name is of the form Y.Z or Z. Any available database
95604** can be used. The zTable variable is the name of the table (the "Y"). This
95605** value can be NULL if zDb is also NULL. If zTable is NULL it
95606** means that the form of the name is Z and that columns from any table
95607** can be used.
95608**
95609** If the name cannot be resolved unambiguously, leave an error message
95610** in pParse and return WRC_Abort. Return WRC_Prune on success.
95611*/
95612static int lookupName(
95613 Parse *pParse, /* The parsing context */
95614 const char *zDb, /* Name of the database containing table, or NULL */
95615 const char *zTab, /* Name of table containing column, or NULL */
95616 const char *zCol, /* Name of the column. */
95617 NameContext *pNC, /* The name context used to resolve the name */
95618 Expr *pExpr /* Make this EXPR node point to the selected column */
95619){
95620 int i, j; /* Loop counters */
95621 int cnt = 0; /* Number of matching column names */
95622 int cntTab = 0; /* Number of matching table names */
95623 int nSubquery = 0; /* How many levels of subquery */
95624 sqlite3 *db = pParse->db; /* The database connection */
95625 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
95626 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
95627 NameContext *pTopNC = pNC; /* First namecontext in the list */
95628 Schema *pSchema = 0; /* Schema of the expression */
95629 int eNewExprOp = TK_COLUMN162; /* New value for pExpr->op on success */
95630 Table *pTab = 0; /* Table hold the row */
95631 Column *pCol; /* A column of pTab */
95632
95633 assert( pNC )((void) (0)); /* the name context cannot be NULL. */
95634 assert( zCol )((void) (0)); /* The Z in X.Y.Z cannot be NULL */
95635 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
95636
95637 /* Initialize the node to no-match */
95638 pExpr->iTable = -1;
95639 ExprSetVVAProperty(pExpr, EP_NoReduce);
95640
95641 /* Translate the schema name in zDb into a pointer to the corresponding
95642 ** schema. If not found, pSchema will remain NULL and nothing will match
95643 ** resulting in an appropriate error message toward the end of this routine
95644 */
95645 if( zDb ){
95646 testcase( pNC->ncFlags & NC_PartIdx );
95647 testcase( pNC->ncFlags & NC_IsCheck );
95648 if( (pNC->ncFlags & (NC_PartIdx0x0002|NC_IsCheck0x0004))!=0 ){
95649 /* Silently ignore database qualifiers inside CHECK constraints and
95650 ** partial indices. Do not raise errors because that might break
95651 ** legacy and because it does not hurt anything to just ignore the
95652 ** database name. */
95653 zDb = 0;
95654 }else{
95655 for(i=0; i<db->nDb; i++){
95656 assert( db->aDb[i].zDbSName )((void) (0));
95657 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
95658 pSchema = db->aDb[i].pSchema;
95659 break;
95660 }
95661 }
95662 }
95663 }
95664
95665 /* Start at the inner-most context and move outward until a match is found */
95666 assert( pNC && cnt==0 )((void) (0));
95667 do{
95668 ExprList *pEList;
95669 SrcList *pSrcList = pNC->pSrcList;
95670
95671 if( pSrcList ){
95672 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
95673 pTab = pItem->pTab;
95674 assert( pTab!=0 && pTab->zName!=0 )((void) (0));
95675 assert( pTab->nCol>0 )((void) (0));
95676 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom0x00800)!=0 ){
95677 int hit = 0;
95678 pEList = pItem->pSelect->pEList;
95679 for(j=0; j<pEList->nExpr; j++){
95680 if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
95681 cnt++;
95682 cntTab = 2;
95683 pMatch = pItem;
95684 pExpr->iColumn = j;
95685 hit = 1;
95686 }
95687 }
95688 if( hit || zTab==0 ) continue;
95689 }
95690 if( zDb && pTab->pSchema!=pSchema ){
95691 continue;
95692 }
95693 if( zTab ){
95694 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
95695 assert( zTabName!=0 )((void) (0));
95696 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
95697 continue;
95698 }
95699 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pItem->zAlias ){
95700 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
95701 }
95702 }
95703 if( 0==(cntTab++) ){
95704 pMatch = pItem;
95705 }
95706 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
95707 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
95708 /* If there has been exactly one prior match and this match
95709 ** is for the right-hand table of a NATURAL JOIN or is in a
95710 ** USING clause, then skip this match.
95711 */
95712 if( cnt==1 ){
95713 if( pItem->fg.jointype & JT_NATURAL0x0004 ) continue;
95714 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
95715 }
95716 cnt++;
95717 pMatch = pItem;
95718 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
95719 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
95720 break;
95721 }
95722 }
95723 }
95724 if( pMatch ){
95725 pExpr->iTable = pMatch->iCursor;
95726 pExpr->y.pTab = pMatch->pTab;
95727 /* RIGHT JOIN not (yet) supported */
95728 assert( (pMatch->fg.jointype & JT_RIGHT)==0 )((void) (0));
95729 if( (pMatch->fg.jointype & JT_LEFT0x0008)!=0 ){
95730 ExprSetProperty(pExpr, EP_CanBeNull)(pExpr)->flags|=(0x100000);
95731 }
95732 pSchema = pExpr->y.pTab->pSchema;
95733 }
95734 } /* if( pSrcList ) */
95735
95736#if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
95737 /* If we have not already resolved the name, then maybe
95738 ** it is a new.* or old.* trigger argument reference. Or
95739 ** maybe it is an excluded.* from an upsert.
95740 */
95741 if( zDb==0 && zTab!=0 && cntTab==0 ){
95742 pTab = 0;
95743#ifndef SQLITE_OMIT_TRIGGER
95744 if( pParse->pTriggerTab!=0 ){
95745 int op = pParse->eTriggerOp;
95746 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT )((void) (0));
95747 if( op!=TK_DELETE121 && sqlite3StrICmp("new",zTab) == 0 ){
95748 pExpr->iTable = 1;
95749 pTab = pParse->pTriggerTab;
95750 }else if( op!=TK_INSERT120 && sqlite3StrICmp("old",zTab)==0 ){
95751 pExpr->iTable = 0;
95752 pTab = pParse->pTriggerTab;
95753 }
95754 }
95755#endif /* SQLITE_OMIT_TRIGGER */
95756#ifndef SQLITE_OMIT_UPSERT
95757 if( (pNC->ncFlags & NC_UUpsert0x0200)!=0 ){
95758 Upsert *pUpsert = pNC->uNC.pUpsert;
95759 if( pUpsert && sqlite3StrICmp("excluded",zTab)==0 ){
95760 pTab = pUpsert->pUpsertSrc->a[0].pTab;
95761 pExpr->iTable = 2;
95762 }
95763 }
95764#endif /* SQLITE_OMIT_UPSERT */
95765
95766 if( pTab ){
95767 int iCol;
95768 pSchema = pTab->pSchema;
95769 cntTab++;
95770 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
95771 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
95772 if( iCol==pTab->iPKey ){
95773 iCol = -1;
95774 }
95775 break;
95776 }
95777 }
95778 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab)(((pTab)->tabFlags & 0x0040)==0) ){
95779 /* IMP: R-51414-32910 */
95780 iCol = -1;
95781 }
95782 if( iCol<pTab->nCol ){
95783 cnt++;
95784#ifndef SQLITE_OMIT_UPSERT
95785 if( pExpr->iTable==2 ){
95786 testcase( iCol==(-1) );
95787 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
95788 pExpr->iColumn = iCol;
95789 pExpr->y.pTab = pTab;
95790 eNewExprOp = TK_COLUMN162;
95791 }else{
95792 pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
95793 eNewExprOp = TK_REGISTER168;
95794 ExprSetProperty(pExpr, EP_Alias)(pExpr)->flags|=(0x400000);
95795 }
95796 }else
95797#endif /* SQLITE_OMIT_UPSERT */
95798 {
95799#ifndef SQLITE_OMIT_TRIGGER
95800 if( iCol<0 ){
95801 pExpr->affinity = SQLITE_AFF_INTEGER'D';
95802 }else if( pExpr->iTable==0 ){
95803 testcase( iCol==31 );
95804 testcase( iCol==32 );
95805 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
95806 }else{
95807 testcase( iCol==31 );
95808 testcase( iCol==32 );
95809 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
95810 }
95811 pExpr->y.pTab = pTab;
95812 pExpr->iColumn = (i16)iCol;
95813 eNewExprOp = TK_TRIGGER77;
95814#endif /* SQLITE_OMIT_TRIGGER */
95815 }
95816 }
95817 }
95818 }
95819#endif /* !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT) */
95820
95821 /*
95822 ** Perhaps the name is a reference to the ROWID
95823 */
95824 if( cnt==0
95825 && cntTab==1
95826 && pMatch
95827 && (pNC->ncFlags & NC_IdxExpr0x0020)==0
95828 && sqlite3IsRowid(zCol)
95829 && VisibleRowid(pMatch->pTab)(((pMatch->pTab)->tabFlags & 0x0040)==0)
95830 ){
95831 cnt = 1;
95832 pExpr->iColumn = -1;
95833 pExpr->affinity = SQLITE_AFF_INTEGER'D';
95834 }
95835
95836 /*
95837 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
95838 ** might refer to an result-set alias. This happens, for example, when
95839 ** we are resolving names in the WHERE clause of the following command:
95840 **
95841 ** SELECT a+b AS x FROM table WHERE x<10;
95842 **
95843 ** In cases like this, replace pExpr with a copy of the expression that
95844 ** forms the result set entry ("a+b" in the example) and return immediately.
95845 ** Note that the expression in the result set should have already been
95846 ** resolved by the time the WHERE clause is resolved.
95847 **
95848 ** The ability to use an output result-set column in the WHERE, GROUP BY,
95849 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
95850 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
95851 ** is supported for backwards compatibility only. Hence, we issue a warning
95852 ** on sqlite3_log() whenever the capability is used.
95853 */
95854 if( (pNC->ncFlags & NC_UEList0x0080)!=0
95855 && cnt==0
95856 && zTab==0
95857 ){
95858 pEList = pNC->uNC.pEList;
95859 assert( pEList!=0 )((void) (0));
95860 for(j=0; j<pEList->nExpr; j++){
95861 char *zAs = pEList->a[j].zName;
95862 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
95863 Expr *pOrig;
95864 assert( pExpr->pLeft==0 && pExpr->pRight==0 )((void) (0));
95865 assert( pExpr->x.pList==0 )((void) (0));
95866 assert( pExpr->x.pSelect==0 )((void) (0));
95867 pOrig = pEList->a[j].pExpr;
95868 if( (pNC->ncFlags&NC_AllowAgg0x0001)==0 && ExprHasProperty(pOrig, EP_Agg)(((pOrig)->flags&(0x000010))!=0) ){
95869 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
95870 return WRC_Abort2;
95871 }
95872 if( (pNC->ncFlags&NC_AllowWin0x4000)==0 && ExprHasProperty(pOrig, EP_Win)(((pOrig)->flags&(0x008000))!=0) ){
95873 sqlite3ErrorMsg(pParse, "misuse of aliased window function %s",zAs);
95874 return WRC_Abort2;
95875 }
95876 if( sqlite3ExprVectorSize(pOrig)!=1 ){
95877 sqlite3ErrorMsg(pParse, "row value misused");
95878 return WRC_Abort2;
95879 }
95880 resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
95881 cnt = 1;
95882 pMatch = 0;
95883 assert( zTab==0 && zDb==0 )((void) (0));
95884 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
95885 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
95886 }
95887 goto lookupname_end;
95888 }
95889 }
95890 }
95891
95892 /* Advance to the next name context. The loop will exit when either
95893 ** we have a match (cnt>0) or when we run out of name contexts.
95894 */
95895 if( cnt ) break;
95896 pNC = pNC->pNext;
95897 nSubquery++;
95898 }while( pNC );
95899
95900
95901 /*
95902 ** If X and Y are NULL (in other words if only the column name Z is
95903 ** supplied) and the value of Z is enclosed in double-quotes, then
95904 ** Z is a string literal if it doesn't match any column names. In that
95905 ** case, we need to return right away and not make any changes to
95906 ** pExpr.
95907 **
95908 ** Because no reference was made to outer contexts, the pNC->nRef
95909 ** fields are not changed in any context.
95910 */
95911 if( cnt==0 && zTab==0 ){
95912 assert( pExpr->op==TK_ID )((void) (0));
95913 if( ExprHasProperty(pExpr,EP_DblQuoted)(((pExpr)->flags&(0x000040))!=0)
95914 && areDoubleQuotedStringsEnabled(db, pTopNC)
95915 ){
95916 /* If a double-quoted identifier does not match any known column name,
95917 ** then treat it as a string.
95918 **
95919 ** This hack was added in the early days of SQLite in a misguided attempt
95920 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
95921 ** I now sorely regret putting in this hack. The effect of this hack is
95922 ** that misspelled identifier names are silently converted into strings
95923 ** rather than causing an error, to the frustration of countless
95924 ** programmers. To all those frustrated programmers, my apologies.
95925 **
95926 ** Someday, I hope to get rid of this hack. Unfortunately there is
95927 ** a huge amount of legacy SQL that uses it. So for now, we just
95928 ** issue a warning.
95929 */
95930 sqlite3_log(SQLITE_WARNING28,
95931 "double-quoted string literal: \"%w\"", zCol);
95932#ifdef SQLITE_ENABLE_NORMALIZE
95933 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
95934#endif
95935 pExpr->op = TK_STRING110;
95936 pExpr->y.pTab = 0;
95937 return WRC_Prune1;
95938 }
95939 if( sqlite3ExprIdToTrueFalse(pExpr) ){
95940 return WRC_Prune1;
95941 }
95942 }
95943
95944 /*
95945 ** cnt==0 means there was not match. cnt>1 means there were two or
95946 ** more matches. Either way, we have an error.
95947 */
95948 if( cnt!=1 ){
95949 const char *zErr;
95950 zErr = cnt==0 ? "no such column" : "ambiguous column name";
95951 if( zDb ){
95952 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
95953 }else if( zTab ){
95954 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
95955 }else{
95956 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
95957 }
95958 pParse->checkSchema = 1;
95959 pTopNC->nErr++;
95960 }
95961
95962 /* If a column from a table in pSrcList is referenced, then record
95963 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
95964 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
95965 ** column number is greater than the number of bits in the bitmask
95966 ** then set the high-order bit of the bitmask.
95967 */
95968 if( pExpr->iColumn>=0 && pMatch!=0 ){
95969 int n = pExpr->iColumn;
95970 testcase( n==BMS-1 );
95971 if( n>=BMS((int)(sizeof(Bitmask)*8)) ){
95972 n = BMS((int)(sizeof(Bitmask)*8))-1;
95973 }
95974 assert( pMatch->iCursor==pExpr->iTable )((void) (0));
95975 pMatch->colUsed |= ((Bitmask)1)<<n;
95976 }
95977
95978 /* Clean up and return
95979 */
95980 sqlite3ExprDelete(db, pExpr->pLeft);
95981 pExpr->pLeft = 0;
95982 sqlite3ExprDelete(db, pExpr->pRight);
95983 pExpr->pRight = 0;
95984 pExpr->op = eNewExprOp;
95985 ExprSetProperty(pExpr, EP_Leaf)(pExpr)->flags|=(0x800000);
95986lookupname_end:
95987 if( cnt==1 ){
95988 assert( pNC!=0 )((void) (0));
95989 if( !ExprHasProperty(pExpr, EP_Alias)(((pExpr)->flags&(0x400000))!=0) ){
95990 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
95991 }
95992 /* Increment the nRef value on all name contexts from TopNC up to
95993 ** the point where the name matched. */
95994 for(;;){
95995 assert( pTopNC!=0 )((void) (0));
95996 pTopNC->nRef++;
95997 if( pTopNC==pNC ) break;
95998 pTopNC = pTopNC->pNext;
95999 }
96000 return WRC_Prune1;
96001 } else {
96002 return WRC_Abort2;
96003 }
96004}
96005
96006/*
96007** Allocate and return a pointer to an expression to load the column iCol
96008** from datasource iSrc in SrcList pSrc.
96009*/
96010SQLITE_PRIVATEstatic Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
96011 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN162, 0, 0);
96012 if( p ){
96013 struct SrcList_item *pItem = &pSrc->a[iSrc];
96014 p->y.pTab = pItem->pTab;
96015 p->iTable = pItem->iCursor;
96016 if( p->y.pTab->iPKey==iCol ){
96017 p->iColumn = -1;
96018 }else{
96019 p->iColumn = (ynVar)iCol;
96020 testcase( iCol==BMS );
96021 testcase( iCol==BMS-1 );
96022 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS((int)(sizeof(Bitmask)*8)) ? BMS((int)(sizeof(Bitmask)*8))-1 : iCol);
96023 }
96024 }
96025 return p;
96026}
96027
96028/*
96029** Report an error that an expression is not valid for some set of
96030** pNC->ncFlags values determined by validMask.
96031*/
96032static void notValid(
96033 Parse *pParse, /* Leave error message here */
96034 NameContext *pNC, /* The name context */
96035 const char *zMsg, /* Type of error */
96036 int validMask /* Set of contexts for which prohibited */
96037){
96038 assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 )((void) (0));
96039 if( (pNC->ncFlags & validMask)!=0 ){
96040 const char *zIn = "partial index WHERE clauses";
96041 if( pNC->ncFlags & NC_IdxExpr0x0020 ) zIn = "index expressions";
96042#ifndef SQLITE_OMIT_CHECK
96043 else if( pNC->ncFlags & NC_IsCheck0x0004 ) zIn = "CHECK constraints";
96044#endif
96045 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
96046 }
96047}
96048
96049/*
96050** Expression p should encode a floating point value between 1.0 and 0.0.
96051** Return 1024 times this value. Or return -1 if p is not a floating point
96052** value between 1.0 and 0.0.
96053*/
96054static int exprProbability(Expr *p){
96055 double r = -1.0;
96056 if( p->op!=TK_FLOAT145 ) return -1;
96057 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF81);
96058 assert( r>=0.0 )((void) (0));
96059 if( r>1.0 ) return -1;
96060 return (int)(r*134217728.0);
96061}
96062
96063/*
96064** This routine is callback for sqlite3WalkExpr().
96065**
96066** Resolve symbolic names into TK_COLUMN operators for the current
96067** node in the expression tree. Return 0 to continue the search down
96068** the tree or 2 to abort the tree walk.
96069**
96070** This routine also does error checking and name resolution for
96071** function names. The operator for aggregate functions is changed
96072** to TK_AGG_FUNCTION.
96073*/
96074static int resolveExprStep(Walker *pWalker, Expr *pExpr){
96075 NameContext *pNC;
96076 Parse *pParse;
96077
96078 pNC = pWalker->u.pNC;
96079 assert( pNC!=0 )((void) (0));
96080 pParse = pNC->pParse;
96081 assert( pParse==pWalker->pParse )((void) (0));
96082
96083#ifndef NDEBUG1
96084 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
96085 SrcList *pSrcList = pNC->pSrcList;
96086 int i;
96087 for(i=0; i<pNC->pSrcList->nSrc; i++){
96088 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab)((void) (0));
96089 }
96090 }
96091#endif
96092 switch( pExpr->op ){
96093
96094#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
96095 /* The special operator TK_ROW means use the rowid for the first
96096 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
96097 ** clause processing on UPDATE and DELETE statements.
96098 */
96099 case TK_ROW75: {
96100 SrcList *pSrcList = pNC->pSrcList;
96101 struct SrcList_item *pItem;
96102 assert( pSrcList && pSrcList->nSrc==1 )((void) (0));
96103 pItem = pSrcList->a;
96104 assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 )((void) (0));
96105 pExpr->op = TK_COLUMN162;
96106 pExpr->y.pTab = pItem->pTab;
96107 pExpr->iTable = pItem->iCursor;
96108 pExpr->iColumn = -1;
96109 pExpr->affinity = SQLITE_AFF_INTEGER'D';
96110 break;
96111 }
96112#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
96113 && !defined(SQLITE_OMIT_SUBQUERY) */
96114
96115 /* A column name: ID
96116 ** Or table name and column name: ID.ID
96117 ** Or a database, table and column: ID.ID.ID
96118 **
96119 ** The TK_ID and TK_OUT cases are combined so that there will only
96120 ** be one call to lookupName(). Then the compiler will in-line
96121 ** lookupName() for a size reduction and performance increase.
96122 */
96123 case TK_ID59:
96124 case TK_DOT134: {
96125 const char *zColumn;
96126 const char *zTable;
96127 const char *zDb;
96128 Expr *pRight;
96129
96130 if( pExpr->op==TK_ID59 ){
96131 zDb = 0;
96132 zTable = 0;
96133 zColumn = pExpr->u.zToken;
96134 }else{
96135 Expr *pLeft = pExpr->pLeft;
96136 notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr0x0020);
96137 pRight = pExpr->pRight;
96138 if( pRight->op==TK_ID59 ){
96139 zDb = 0;
96140 }else{
96141 assert( pRight->op==TK_DOT )((void) (0));
96142 zDb = pLeft->u.zToken;
96143 pLeft = pRight->pLeft;
96144 pRight = pRight->pRight;
96145 }
96146 zTable = pLeft->u.zToken;
96147 zColumn = pRight->u.zToken;
96148 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
96149 sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
96150 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
96151 }
96152 }
96153 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
96154 }
96155
96156 /* Resolve function names
96157 */
96158 case TK_FUNCTION161: {
96159 ExprList *pList = pExpr->x.pList; /* The argument list */
96160 int n = pList ? pList->nExpr : 0; /* Number of arguments */
96161 int no_such_func = 0; /* True if no such function exists */
96162 int wrong_num_args = 0; /* True if wrong number of arguments */
96163 int is_agg = 0; /* True if is an aggregate function */
96164 int nId; /* Number of characters in function name */
96165 const char *zId; /* The function name. */
96166 FuncDef *pDef; /* Information about the function */
96167 u8 enc = ENC(pParse->db)((pParse->db)->enc); /* The database encoding */
96168 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg0x0001 | NC_AllowWin0x4000));
96169
96170 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
96171 zId = pExpr->u.zToken;
96172 nId = sqlite3Strlen30(zId);
96173 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
96174 if( pDef==0 ){
96175 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
96176 if( pDef==0 ){
96177 no_such_func = 1;
96178 }else{
96179 wrong_num_args = 1;
96180 }
96181 }else{
96182 is_agg = pDef->xFinalize!=0;
96183 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY0x0400 ){
96184 ExprSetProperty(pExpr, EP_Unlikely)(pExpr)->flags|=(0x040000);
96185 if( n==2 ){
96186 pExpr->iTable = exprProbability(pList->a[1].pExpr);
96187 if( pExpr->iTable<0 ){
96188 sqlite3ErrorMsg(pParse,
96189 "second argument to likelihood() must be a "
96190 "constant between 0.0 and 1.0");
96191 pNC->nErr++;
96192 }
96193 }else{
96194 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
96195 ** equivalent to likelihood(X, 0.0625).
96196 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
96197 ** short-hand for likelihood(X,0.0625).
96198 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
96199 ** for likelihood(X,0.9375).
96200 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
96201 ** to likelihood(X,0.9375). */
96202 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
96203 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
96204 }
96205 }
96206#ifndef SQLITE_OMIT_AUTHORIZATION
96207 {
96208 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION31, 0,pDef->zName,0);
96209 if( auth!=SQLITE_OK0 ){
96210 if( auth==SQLITE_DENY1 ){
96211 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
96212 pDef->zName);
96213 pNC->nErr++;
96214 }
96215 pExpr->op = TK_NULL114;
96216 return WRC_Prune1;
96217 }
96218 }
96219#endif
96220 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT0x0800|SQLITE_FUNC_SLOCHNG0x2000) ){
96221 /* For the purposes of the EP_ConstFunc flag, date and time
96222 ** functions and other functions that change slowly are considered
96223 ** constant because they are constant for the duration of one query */
96224 ExprSetProperty(pExpr,EP_ConstFunc)(pExpr)->flags|=(0x080000);
96225 }
96226 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT0x0800)==0 ){
96227 /* Date/time functions that use 'now', and other functions like
96228 ** sqlite_version() that might change over time cannot be used
96229 ** in an index. */
96230 notValid(pParse, pNC, "non-deterministic functions",
96231 NC_IdxExpr0x0020|NC_PartIdx0x0002);
96232 }
96233 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL0x00040000)!=0
96234 && pParse->nested==0
96235 && sqlite3Config.bInternalFunctions==0
96236 ){
96237 /* Internal-use-only functions are disallowed unless the
96238 ** SQL is being compiled using sqlite3NestedParse() */
96239 no_such_func = 1;
96240 pDef = 0;
96241 }
96242 }
96243
96244 if( 0==IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
96245#ifndef SQLITE_OMIT_WINDOWFUNC
96246 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)((void) (0))
96247 || (pDef->xValue==0 && pDef->xInverse==0)((void) (0))
96248 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)((void) (0))
96249 )((void) (0));
96250 if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
96251 sqlite3ErrorMsg(pParse,
96252 "%.*s() may not be used as a window function", nId, zId
96253 );
96254 pNC->nErr++;
96255 }else if(
96256 (is_agg && (pNC->ncFlags & NC_AllowAgg0x0001)==0)
96257 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW0x00010000) && !pExpr->y.pWin)
96258 || (is_agg && pExpr->y.pWin && (pNC->ncFlags & NC_AllowWin0x4000)==0)
96259 ){
96260 const char *zType;
96261 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW0x00010000) || pExpr->y.pWin ){
96262 zType = "window";
96263 }else{
96264 zType = "aggregate";
96265 }
96266 sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
96267 pNC->nErr++;
96268 is_agg = 0;
96269 }
96270#else
96271 if( (is_agg && (pNC->ncFlags & NC_AllowAgg0x0001)==0) ){
96272 sqlite3ErrorMsg(pParse,"misuse of aggregate function %.*s()",nId,zId);
96273 pNC->nErr++;
96274 is_agg = 0;
96275 }
96276#endif
96277 else if( no_such_func && pParse->db->init.busy==0
96278#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
96279 && pParse->explain==0
96280#endif
96281 ){
96282 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
96283 pNC->nErr++;
96284 }else if( wrong_num_args ){
96285 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
96286 nId, zId);
96287 pNC->nErr++;
96288 }
96289 if( is_agg ){
96290 /* Window functions may not be arguments of aggregate functions.
96291 ** Or arguments of other window functions. But aggregate functions
96292 ** may be arguments for window functions. */
96293#ifndef SQLITE_OMIT_WINDOWFUNC
96294 pNC->ncFlags &= ~(NC_AllowWin0x4000 | (!pExpr->y.pWin ? NC_AllowAgg0x0001 : 0));
96295#else
96296 pNC->ncFlags &= ~NC_AllowAgg0x0001;
96297#endif
96298 }
96299 }
96300 sqlite3WalkExprList(pWalker, pList);
96301 if( is_agg ){
96302#ifndef SQLITE_OMIT_WINDOWFUNC
96303 if( pExpr->y.pWin ){
96304 Select *pSel = pNC->pWinSelect;
96305 if( IN_RENAME_OBJECT(pParse->eParseMode>=2)==0 ){
96306 sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
96307 }
96308 sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition);
96309 sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy);
96310 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
96311 if( 0==pSel->pWin
96312 || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin)
96313 ){
96314 pExpr->y.pWin->pNextWin = pSel->pWin;
96315 pSel->pWin = pExpr->y.pWin;
96316 }
96317 pNC->ncFlags |= NC_HasWin0x8000;
96318 }else
96319#endif /* SQLITE_OMIT_WINDOWFUNC */
96320 {
96321 NameContext *pNC2 = pNC;
96322 pExpr->op = TK_AGG_FUNCTION163;
96323 pExpr->op2 = 0;
96324 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
96325 pExpr->op2++;
96326 pNC2 = pNC2->pNext;
96327 }
96328 assert( pDef!=0 )((void) (0));
96329 if( pNC2 ){
96330 assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg )((void) (0));
96331 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
96332 pNC2->ncFlags |= NC_HasAgg0x0010 | (pDef->funcFlags & SQLITE_FUNC_MINMAX0x1000);
96333
96334 }
96335 }
96336 pNC->ncFlags |= savedAllowFlags;
96337 }
96338 /* FIX ME: Compute pExpr->affinity based on the expected return
96339 ** type of the function
96340 */
96341 return WRC_Prune1;
96342 }
96343#ifndef SQLITE_OMIT_SUBQUERY
96344 case TK_SELECT131:
96345 case TK_EXISTS20: testcase( pExpr->op==TK_EXISTS );
96346#endif
96347 case TK_IN49: {
96348 testcase( pExpr->op==TK_IN );
96349 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
96350 int nRef = pNC->nRef;
96351 notValid(pParse, pNC, "subqueries", NC_IsCheck0x0004|NC_PartIdx0x0002|NC_IdxExpr0x0020);
96352 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
96353 assert( pNC->nRef>=nRef )((void) (0));
96354 if( nRef!=pNC->nRef ){
96355 ExprSetProperty(pExpr, EP_VarSelect)(pExpr)->flags|=(0x000020);
96356 pNC->ncFlags |= NC_VarSelect0x0040;
96357 }
96358 }
96359 break;
96360 }
96361 case TK_VARIABLE148: {
96362 notValid(pParse, pNC, "parameters", NC_IsCheck0x0004|NC_PartIdx0x0002|NC_IdxExpr0x0020);
96363 break;
96364 }
96365 case TK_IS45:
96366 case TK_ISNOT160: {
96367 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
96368 assert( !ExprHasProperty(pExpr, EP_Reduced) )((void) (0));
96369 /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
96370 ** and "x IS NOT FALSE". */
96371 if( pRight->op==TK_ID59 ){
96372 int rc = resolveExprStep(pWalker, pRight);
96373 if( rc==WRC_Abort2 ) return WRC_Abort2;
96374 if( pRight->op==TK_TRUEFALSE159 ){
96375 pExpr->op2 = pExpr->op;
96376 pExpr->op = TK_TRUTH167;
96377 return WRC_Continue0;
96378 }
96379 }
96380 /* Fall thru */
96381 }
96382 case TK_BETWEEN48:
96383 case TK_EQ53:
96384 case TK_NE52:
96385 case TK_LT56:
96386 case TK_LE55:
96387 case TK_GT54:
96388 case TK_GE57: {
96389 int nLeft, nRight;
96390 if( pParse->db->mallocFailed ) break;
96391 assert( pExpr->pLeft!=0 )((void) (0));
96392 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
96393 if( pExpr->op==TK_BETWEEN48 ){
96394 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
96395 if( nRight==nLeft ){
96396 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
96397 }
96398 }else{
96399 assert( pExpr->pRight!=0 )((void) (0));
96400 nRight = sqlite3ExprVectorSize(pExpr->pRight);
96401 }
96402 if( nLeft!=nRight ){
96403 testcase( pExpr->op==TK_EQ );
96404 testcase( pExpr->op==TK_NE );
96405 testcase( pExpr->op==TK_LT );
96406 testcase( pExpr->op==TK_LE );
96407 testcase( pExpr->op==TK_GT );
96408 testcase( pExpr->op==TK_GE );
96409 testcase( pExpr->op==TK_IS );
96410 testcase( pExpr->op==TK_ISNOT );
96411 testcase( pExpr->op==TK_BETWEEN );
96412 sqlite3ErrorMsg(pParse, "row value misused");
96413 }
96414 break;
96415 }
96416 }
96417 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort2 : WRC_Continue0;
96418}
96419
96420/*
96421** pEList is a list of expressions which are really the result set of the
96422** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
96423** This routine checks to see if pE is a simple identifier which corresponds
96424** to the AS-name of one of the terms of the expression list. If it is,
96425** this routine return an integer between 1 and N where N is the number of
96426** elements in pEList, corresponding to the matching entry. If there is
96427** no match, or if pE is not a simple identifier, then this routine
96428** return 0.
96429**
96430** pEList has been resolved. pE has not.
96431*/
96432static int resolveAsName(
96433 Parse *pParse, /* Parsing context for error messages */
96434 ExprList *pEList, /* List of expressions to scan */
96435 Expr *pE /* Expression we are trying to match */
96436){
96437 int i; /* Loop counter */
96438
96439 UNUSED_PARAMETER(pParse)(void)(pParse);
96440
96441 if( pE->op==TK_ID59 ){
96442 char *zCol = pE->u.zToken;
96443 for(i=0; i<pEList->nExpr; i++){
96444 char *zAs = pEList->a[i].zName;
96445 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
96446 return i+1;
96447 }
96448 }
96449 }
96450 return 0;
96451}
96452
96453/*
96454** pE is a pointer to an expression which is a single term in the
96455** ORDER BY of a compound SELECT. The expression has not been
96456** name resolved.
96457**
96458** At the point this routine is called, we already know that the
96459** ORDER BY term is not an integer index into the result set. That
96460** case is handled by the calling routine.
96461**
96462** Attempt to match pE against result set columns in the left-most
96463** SELECT statement. Return the index i of the matching column,
96464** as an indication to the caller that it should sort by the i-th column.
96465** The left-most column is 1. In other words, the value returned is the
96466** same integer value that would be used in the SQL statement to indicate
96467** the column.
96468**
96469** If there is no match, return 0. Return -1 if an error occurs.
96470*/
96471static int resolveOrderByTermToExprList(
96472 Parse *pParse, /* Parsing context for error messages */
96473 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
96474 Expr *pE /* The specific ORDER BY term */
96475){
96476 int i; /* Loop counter */
96477 ExprList *pEList; /* The columns of the result set */
96478 NameContext nc; /* Name context for resolving pE */
96479 sqlite3 *db; /* Database connection */
96480 int rc; /* Return code from subprocedures */
96481 u8 savedSuppErr; /* Saved value of db->suppressErr */
96482
96483 assert( sqlite3ExprIsInteger(pE, &i)==0 )((void) (0));
96484 pEList = pSelect->pEList;
96485
96486 /* Resolve all names in the ORDER BY term expression
96487 */
96488 memset(&nc, 0, sizeof(nc));
96489 nc.pParse = pParse;
96490 nc.pSrcList = pSelect->pSrc;
96491 nc.uNC.pEList = pEList;
96492 nc.ncFlags = NC_AllowAgg0x0001|NC_UEList0x0080;
96493 nc.nErr = 0;
96494 db = pParse->db;
96495 savedSuppErr = db->suppressErr;
96496 db->suppressErr = 1;
96497 rc = sqlite3ResolveExprNames(&nc, pE);
96498 db->suppressErr = savedSuppErr;
96499 if( rc ) return 0;
96500
96501 /* Try to match the ORDER BY expression against an expression
96502 ** in the result set. Return an 1-based index of the matching
96503 ** result-set entry.
96504 */
96505 for(i=0; i<pEList->nExpr; i++){
96506 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
96507 return i+1;
96508 }
96509 }
96510
96511 /* If no match, return 0. */
96512 return 0;
96513}
96514
96515/*
96516** Generate an ORDER BY or GROUP BY term out-of-range error.
96517*/
96518static void resolveOutOfRangeError(
96519 Parse *pParse, /* The error context into which to write the error */
96520 const char *zType, /* "ORDER" or "GROUP" */
96521 int i, /* The index (1-based) of the term out of range */
96522 int mx /* Largest permissible value of i */
96523){
96524 sqlite3ErrorMsg(pParse,
96525 "%r %s BY term out of range - should be "
96526 "between 1 and %d", i, zType, mx);
96527}
96528
96529/*
96530** Analyze the ORDER BY clause in a compound SELECT statement. Modify
96531** each term of the ORDER BY clause is a constant integer between 1
96532** and N where N is the number of columns in the compound SELECT.
96533**
96534** ORDER BY terms that are already an integer between 1 and N are
96535** unmodified. ORDER BY terms that are integers outside the range of
96536** 1 through N generate an error. ORDER BY terms that are expressions
96537** are matched against result set expressions of compound SELECT
96538** beginning with the left-most SELECT and working toward the right.
96539** At the first match, the ORDER BY expression is transformed into
96540** the integer column number.
96541**
96542** Return the number of errors seen.
96543*/
96544static int resolveCompoundOrderBy(
96545 Parse *pParse, /* Parsing context. Leave error messages here */
96546 Select *pSelect /* The SELECT statement containing the ORDER BY */
96547){
96548 int i;
96549 ExprList *pOrderBy;
96550 ExprList *pEList;
96551 sqlite3 *db;
96552 int moreToDo = 1;
96553
96554 pOrderBy = pSelect->pOrderBy;
96555 if( pOrderBy==0 ) return 0;
96556 db = pParse->db;
96557 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
96558 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
96559 return 1;
96560 }
96561 for(i=0; i<pOrderBy->nExpr; i++){
96562 pOrderBy->a[i].done = 0;
96563 }
96564 pSelect->pNext = 0;
96565 while( pSelect->pPrior ){
96566 pSelect->pPrior->pNext = pSelect;
96567 pSelect = pSelect->pPrior;
96568 }
96569 while( pSelect && moreToDo ){
96570 struct ExprList_item *pItem;
96571 moreToDo = 0;
96572 pEList = pSelect->pEList;
96573 assert( pEList!=0 )((void) (0));
96574 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
96575 int iCol = -1;
96576 Expr *pE, *pDup;
96577 if( pItem->done ) continue;
96578 pE = sqlite3ExprSkipCollate(pItem->pExpr);
96579 if( sqlite3ExprIsInteger(pE, &iCol) ){
96580 if( iCol<=0 || iCol>pEList->nExpr ){
96581 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
96582 return 1;
96583 }
96584 }else{
96585 iCol = resolveAsName(pParse, pEList, pE);
96586 if( iCol==0 ){
96587 /* Now test if expression pE matches one of the values returned
96588 ** by pSelect. In the usual case this is done by duplicating the
96589 ** expression, resolving any symbols in it, and then comparing
96590 ** it against each expression returned by the SELECT statement.
96591 ** Once the comparisons are finished, the duplicate expression
96592 ** is deleted.
96593 **
96594 ** Or, if this is running as part of an ALTER TABLE operation,
96595 ** resolve the symbols in the actual expression, not a duplicate.
96596 ** And, if one of the comparisons is successful, leave the expression
96597 ** as is instead of transforming it to an integer as in the usual
96598 ** case. This allows the code in alter.c to modify column
96599 ** refererences within the ORDER BY expression as required. */
96600 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
96601 pDup = pE;
96602 }else{
96603 pDup = sqlite3ExprDup(db, pE, 0);
96604 }
96605 if( !db->mallocFailed ){
96606 assert(pDup)((void) (0));
96607 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
96608 }
96609 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
96610 sqlite3ExprDelete(db, pDup);
96611 }
96612 }
96613 }
96614 if( iCol>0 ){
96615 /* Convert the ORDER BY term into an integer column number iCol,
96616 ** taking care to preserve the COLLATE clause if it exists */
96617 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
96618 Expr *pNew = sqlite3Expr(db, TK_INTEGER147, 0);
96619 if( pNew==0 ) return 1;
96620 pNew->flags |= EP_IntValue0x000400;
96621 pNew->u.iValue = iCol;
96622 if( pItem->pExpr==pE ){
96623 pItem->pExpr = pNew;
96624 }else{
96625 Expr *pParent = pItem->pExpr;
96626 assert( pParent->op==TK_COLLATE )((void) (0));
96627 while( pParent->pLeft->op==TK_COLLATE106 ) pParent = pParent->pLeft;
96628 assert( pParent->pLeft==pE )((void) (0));
96629 pParent->pLeft = pNew;
96630 }
96631 sqlite3ExprDelete(db, pE);
96632 pItem->u.x.iOrderByCol = (u16)iCol;
96633 }
96634 pItem->done = 1;
96635 }else{
96636 moreToDo = 1;
96637 }
96638 }
96639 pSelect = pSelect->pNext;
96640 }
96641 for(i=0; i<pOrderBy->nExpr; i++){
96642 if( pOrderBy->a[i].done==0 ){
96643 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
96644 "column in the result set", i+1);
96645 return 1;
96646 }
96647 }
96648 return 0;
96649}
96650
96651/*
96652** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
96653** the SELECT statement pSelect. If any term is reference to a
96654** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
96655** field) then convert that term into a copy of the corresponding result set
96656** column.
96657**
96658** If any errors are detected, add an error message to pParse and
96659** return non-zero. Return zero if no errors are seen.
96660*/
96661SQLITE_PRIVATEstatic int sqlite3ResolveOrderGroupBy(
96662 Parse *pParse, /* Parsing context. Leave error messages here */
96663 Select *pSelect, /* The SELECT statement containing the clause */
96664 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
96665 const char *zType /* "ORDER" or "GROUP" */
96666){
96667 int i;
96668 sqlite3 *db = pParse->db;
96669 ExprList *pEList;
96670 struct ExprList_item *pItem;
96671
96672 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
96673 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
96674 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
96675 return 1;
96676 }
96677 pEList = pSelect->pEList;
96678 assert( pEList!=0 )((void) (0)); /* sqlite3SelectNew() guarantees this */
96679 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
96680 if( pItem->u.x.iOrderByCol ){
96681 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
96682 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
96683 return 1;
96684 }
96685 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
96686 zType,0);
96687 }
96688 }
96689 return 0;
96690}
96691
96692#ifndef SQLITE_OMIT_WINDOWFUNC
96693/*
96694** Walker callback for resolveRemoveWindows().
96695*/
96696static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
96697 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
96698 Window **pp;
96699 for(pp=&pWalker->u.pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
96700 if( *pp==pExpr->y.pWin ){
96701 *pp = (*pp)->pNextWin;
96702 break;
96703 }
96704 }
96705 }
96706 return WRC_Continue0;
96707}
96708
96709/*
96710** Remove any Window objects owned by the expression pExpr from the
96711** Select.pWin list of Select object pSelect.
96712*/
96713static void resolveRemoveWindows(Select *pSelect, Expr *pExpr){
96714 Walker sWalker;
96715 memset(&sWalker, 0, sizeof(Walker));
96716 sWalker.xExprCallback = resolveRemoveWindowsCb;
96717 sWalker.u.pSelect = pSelect;
96718 sqlite3WalkExpr(&sWalker, pExpr);
96719}
96720#else
96721# define resolveRemoveWindows(x,y)
96722#endif
96723
96724/*
96725** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
96726** The Name context of the SELECT statement is pNC. zType is either
96727** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
96728**
96729** This routine resolves each term of the clause into an expression.
96730** If the order-by term is an integer I between 1 and N (where N is the
96731** number of columns in the result set of the SELECT) then the expression
96732** in the resolution is a copy of the I-th result-set expression. If
96733** the order-by term is an identifier that corresponds to the AS-name of
96734** a result-set expression, then the term resolves to a copy of the
96735** result-set expression. Otherwise, the expression is resolved in
96736** the usual way - using sqlite3ResolveExprNames().
96737**
96738** This routine returns the number of errors. If errors occur, then
96739** an appropriate error message might be left in pParse. (OOM errors
96740** excepted.)
96741*/
96742static int resolveOrderGroupBy(
96743 NameContext *pNC, /* The name context of the SELECT statement */
96744 Select *pSelect, /* The SELECT statement holding pOrderBy */
96745 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
96746 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
96747){
96748 int i, j; /* Loop counters */
96749 int iCol; /* Column number */
96750 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
96751 Parse *pParse; /* Parsing context */
96752 int nResult; /* Number of terms in the result set */
96753
96754 if( pOrderBy==0 ) return 0;
96755 nResult = pSelect->pEList->nExpr;
96756 pParse = pNC->pParse;
96757 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
96758 Expr *pE = pItem->pExpr;
96759 Expr *pE2 = sqlite3ExprSkipCollate(pE);
96760 if( zType[0]!='G' ){
96761 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
96762 if( iCol>0 ){
96763 /* If an AS-name match is found, mark this ORDER BY column as being
96764 ** a copy of the iCol-th result-set column. The subsequent call to
96765 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
96766 ** copy of the iCol-th result-set expression. */
96767 pItem->u.x.iOrderByCol = (u16)iCol;
96768 continue;
96769 }
96770 }
96771 if( sqlite3ExprIsInteger(pE2, &iCol) ){
96772 /* The ORDER BY term is an integer constant. Again, set the column
96773 ** number so that sqlite3ResolveOrderGroupBy() will convert the
96774 ** order-by term to a copy of the result-set expression */
96775 if( iCol<1 || iCol>0xffff ){
96776 resolveOutOfRangeError(pParse, zType, i+1, nResult);
96777 return 1;
96778 }
96779 pItem->u.x.iOrderByCol = (u16)iCol;
96780 continue;
96781 }
96782
96783 /* Otherwise, treat the ORDER BY term as an ordinary expression */
96784 pItem->u.x.iOrderByCol = 0;
96785 if( sqlite3ResolveExprNames(pNC, pE) ){
96786 return 1;
96787 }
96788 for(j=0; j<pSelect->pEList->nExpr; j++){
96789 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
96790 /* Since this expresion is being changed into a reference
96791 ** to an identical expression in the result set, remove all Window
96792 ** objects belonging to the expression from the Select.pWin list. */
96793 resolveRemoveWindows(pSelect, pE);
96794 pItem->u.x.iOrderByCol = j+1;
96795 }
96796 }
96797 }
96798 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
96799}
96800
96801/*
96802** Resolve names in the SELECT statement p and all of its descendants.
96803*/
96804static int resolveSelectStep(Walker *pWalker, Select *p){
96805 NameContext *pOuterNC; /* Context that contains this SELECT */
96806 NameContext sNC; /* Name context of this SELECT */
96807 int isCompound; /* True if p is a compound select */
96808 int nCompound; /* Number of compound terms processed so far */
96809 Parse *pParse; /* Parsing context */
96810 int i; /* Loop counter */
96811 ExprList *pGroupBy; /* The GROUP BY clause */
96812 Select *pLeftmost; /* Left-most of SELECT of a compound */
96813 sqlite3 *db; /* Database connection */
96814
96815
96816 assert( p!=0 )((void) (0));
96817 if( p->selFlags & SF_Resolved0x00004 ){
96818 return WRC_Prune1;
96819 }
96820 pOuterNC = pWalker->u.pNC;
96821 pParse = pWalker->pParse;
96822 db = pParse->db;
96823
96824 /* Normally sqlite3SelectExpand() will be called first and will have
96825 ** already expanded this SELECT. However, if this is a subquery within
96826 ** an expression, sqlite3ResolveExprNames() will be called without a
96827 ** prior call to sqlite3SelectExpand(). When that happens, let
96828 ** sqlite3SelectPrep() do all of the processing for this SELECT.
96829 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
96830 ** this routine in the correct order.
96831 */
96832 if( (p->selFlags & SF_Expanded0x00040)==0 ){
96833 sqlite3SelectPrep(pParse, p, pOuterNC);
96834 return (pParse->nErr || db->mallocFailed) ? WRC_Abort2 : WRC_Prune1;
96835 }
96836
96837 isCompound = p->pPrior!=0;
96838 nCompound = 0;
96839 pLeftmost = p;
96840 while( p ){
96841 assert( (p->selFlags & SF_Expanded)!=0 )((void) (0));
96842 assert( (p->selFlags & SF_Resolved)==0 )((void) (0));
96843 p->selFlags |= SF_Resolved0x00004;
96844
96845 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
96846 ** are not allowed to refer to any names, so pass an empty NameContext.
96847 */
96848 memset(&sNC, 0, sizeof(sNC));
96849 sNC.pParse = pParse;
96850 sNC.pWinSelect = p;
96851 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
96852 return WRC_Abort2;
96853 }
96854
96855 /* If the SF_Converted flags is set, then this Select object was
96856 ** was created by the convertCompoundSelectToSubquery() function.
96857 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
96858 ** as if it were part of the sub-query, not the parent. This block
96859 ** moves the pOrderBy down to the sub-query. It will be moved back
96860 ** after the names have been resolved. */
96861 if( p->selFlags & SF_Converted0x10000 ){
96862 Select *pSub = p->pSrc->a[0].pSelect;
96863 assert( p->pSrc->nSrc==1 && p->pOrderBy )((void) (0));
96864 assert( pSub->pPrior && pSub->pOrderBy==0 )((void) (0));
96865 pSub->pOrderBy = p->pOrderBy;
96866 p->pOrderBy = 0;
96867 }
96868
96869 /* Recursively resolve names in all subqueries
96870 */
96871 for(i=0; i<p->pSrc->nSrc; i++){
96872 struct SrcList_item *pItem = &p->pSrc->a[i];
96873 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved0x00004)==0 ){
96874 NameContext *pNC; /* Used to iterate name contexts */
96875 int nRef = 0; /* Refcount for pOuterNC and outer contexts */
96876 const char *zSavedContext = pParse->zAuthContext;
96877
96878 /* Count the total number of references to pOuterNC and all of its
96879 ** parent contexts. After resolving references to expressions in
96880 ** pItem->pSelect, check if this value has changed. If so, then
96881 ** SELECT statement pItem->pSelect must be correlated. Set the
96882 ** pItem->fg.isCorrelated flag if this is the case. */
96883 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
96884
96885 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
96886 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
96887 pParse->zAuthContext = zSavedContext;
96888 if( pParse->nErr || db->mallocFailed ) return WRC_Abort2;
96889
96890 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
96891 assert( pItem->fg.isCorrelated==0 && nRef<=0 )((void) (0));
96892 pItem->fg.isCorrelated = (nRef!=0);
96893 }
96894 }
96895
96896 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
96897 ** resolve the result-set expression list.
96898 */
96899 sNC.ncFlags = NC_AllowAgg0x0001|NC_AllowWin0x4000;
96900 sNC.pSrcList = p->pSrc;
96901 sNC.pNext = pOuterNC;
96902
96903 /* Resolve names in the result set. */
96904 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort2;
96905 sNC.ncFlags &= ~NC_AllowWin0x4000;
96906
96907 /* If there are no aggregate functions in the result-set, and no GROUP BY
96908 ** expression, do not allow aggregates in any of the other expressions.
96909 */
96910 assert( (p->selFlags & SF_Aggregate)==0 )((void) (0));
96911 pGroupBy = p->pGroupBy;
96912 if( pGroupBy || (sNC.ncFlags & NC_HasAgg0x0010)!=0 ){
96913 assert( NC_MinMaxAgg==SF_MinMaxAgg )((void) (0));
96914 p->selFlags |= SF_Aggregate0x00008 | (sNC.ncFlags&NC_MinMaxAgg0x1000);
96915 }else{
96916 sNC.ncFlags &= ~NC_AllowAgg0x0001;
96917 }
96918
96919 /* If a HAVING clause is present, then there must be a GROUP BY clause.
96920 */
96921 if( p->pHaving && !pGroupBy ){
96922 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
96923 return WRC_Abort2;
96924 }
96925
96926 /* Add the output column list to the name-context before parsing the
96927 ** other expressions in the SELECT statement. This is so that
96928 ** expressions in the WHERE clause (etc.) can refer to expressions by
96929 ** aliases in the result set.
96930 **
96931 ** Minor point: If this is the case, then the expression will be
96932 ** re-evaluated for each reference to it.
96933 */
96934 assert( (sNC.ncFlags & (NC_UAggInfo|NC_UUpsert))==0 )((void) (0));
96935 sNC.uNC.pEList = p->pEList;
96936 sNC.ncFlags |= NC_UEList0x0080;
96937 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort2;
96938 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort2;
96939
96940 /* Resolve names in table-valued-function arguments */
96941 for(i=0; i<p->pSrc->nSrc; i++){
96942 struct SrcList_item *pItem = &p->pSrc->a[i];
96943 if( pItem->fg.isTabFunc
96944 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
96945 ){
96946 return WRC_Abort2;
96947 }
96948 }
96949
96950 /* The ORDER BY and GROUP BY clauses may not refer to terms in
96951 ** outer queries
96952 */
96953 sNC.pNext = 0;
96954 sNC.ncFlags |= NC_AllowAgg0x0001|NC_AllowWin0x4000;
96955
96956 /* If this is a converted compound query, move the ORDER BY clause from
96957 ** the sub-query back to the parent query. At this point each term
96958 ** within the ORDER BY clause has been transformed to an integer value.
96959 ** These integers will be replaced by copies of the corresponding result
96960 ** set expressions by the call to resolveOrderGroupBy() below. */
96961 if( p->selFlags & SF_Converted0x10000 ){
96962 Select *pSub = p->pSrc->a[0].pSelect;
96963 p->pOrderBy = pSub->pOrderBy;
96964 pSub->pOrderBy = 0;
96965 }
96966
96967 /* Process the ORDER BY clause for singleton SELECT statements.
96968 ** The ORDER BY clause for compounds SELECT statements is handled
96969 ** below, after all of the result-sets for all of the elements of
96970 ** the compound have been resolved.
96971 **
96972 ** If there is an ORDER BY clause on a term of a compound-select other
96973 ** than the right-most term, then that is a syntax error. But the error
96974 ** is not detected until much later, and so we need to go ahead and
96975 ** resolve those symbols on the incorrect ORDER BY for consistency.
96976 */
96977 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
96978 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
96979 ){
96980 return WRC_Abort2;
96981 }
96982 if( db->mallocFailed ){
96983 return WRC_Abort2;
96984 }
96985 sNC.ncFlags &= ~NC_AllowWin0x4000;
96986
96987 /* Resolve the GROUP BY clause. At the same time, make sure
96988 ** the GROUP BY clause does not contain aggregate functions.
96989 */
96990 if( pGroupBy ){
96991 struct ExprList_item *pItem;
96992
96993 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
96994 return WRC_Abort2;
96995 }
96996 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
96997 if( ExprHasProperty(pItem->pExpr, EP_Agg)(((pItem->pExpr)->flags&(0x000010))!=0) ){
96998 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
96999 "the GROUP BY clause");
97000 return WRC_Abort2;
97001 }
97002 }
97003 }
97004
97005#ifndef SQLITE_OMIT_WINDOWFUNC
97006 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
97007 Window *pWin;
97008 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
97009 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
97010 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
97011 ){
97012 return WRC_Abort2;
97013 }
97014 }
97015 }
97016#endif
97017
97018 /* If this is part of a compound SELECT, check that it has the right
97019 ** number of expressions in the select list. */
97020 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
97021 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
97022 return WRC_Abort2;
97023 }
97024
97025 /* Advance to the next term of the compound
97026 */
97027 p = p->pPrior;
97028 nCompound++;
97029 }
97030
97031 /* Resolve the ORDER BY on a compound SELECT after all terms of
97032 ** the compound have been resolved.
97033 */
97034 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
97035 return WRC_Abort2;
97036 }
97037
97038 return WRC_Prune1;
97039}
97040
97041/*
97042** This routine walks an expression tree and resolves references to
97043** table columns and result-set columns. At the same time, do error
97044** checking on function usage and set a flag if any aggregate functions
97045** are seen.
97046**
97047** To resolve table columns references we look for nodes (or subtrees) of the
97048** form X.Y.Z or Y.Z or just Z where
97049**
97050** X: The name of a database. Ex: "main" or "temp" or
97051** the symbolic name assigned to an ATTACH-ed database.
97052**
97053** Y: The name of a table in a FROM clause. Or in a trigger
97054** one of the special names "old" or "new".
97055**
97056** Z: The name of a column in table Y.
97057**
97058** The node at the root of the subtree is modified as follows:
97059**
97060** Expr.op Changed to TK_COLUMN
97061** Expr.pTab Points to the Table object for X.Y
97062** Expr.iColumn The column index in X.Y. -1 for the rowid.
97063** Expr.iTable The VDBE cursor number for X.Y
97064**
97065**
97066** To resolve result-set references, look for expression nodes of the
97067** form Z (with no X and Y prefix) where the Z matches the right-hand
97068** size of an AS clause in the result-set of a SELECT. The Z expression
97069** is replaced by a copy of the left-hand side of the result-set expression.
97070** Table-name and function resolution occurs on the substituted expression
97071** tree. For example, in:
97072**
97073** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
97074**
97075** The "x" term of the order by is replaced by "a+b" to render:
97076**
97077** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
97078**
97079** Function calls are checked to make sure that the function is
97080** defined and that the correct number of arguments are specified.
97081** If the function is an aggregate function, then the NC_HasAgg flag is
97082** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
97083** If an expression contains aggregate functions then the EP_Agg
97084** property on the expression is set.
97085**
97086** An error message is left in pParse if anything is amiss. The number
97087** if errors is returned.
97088*/
97089SQLITE_PRIVATEstatic int sqlite3ResolveExprNames(
97090 NameContext *pNC, /* Namespace to resolve expressions in. */
97091 Expr *pExpr /* The expression to be analyzed. */
97092){
97093 int savedHasAgg;
97094 Walker w;
97095
97096 if( pExpr==0 ) return SQLITE_OK0;
97097 savedHasAgg = pNC->ncFlags & (NC_HasAgg0x0010|NC_MinMaxAgg0x1000|NC_HasWin0x8000);
97098 pNC->ncFlags &= ~(NC_HasAgg0x0010|NC_MinMaxAgg0x1000|NC_HasWin0x8000);
97099 w.pParse = pNC->pParse;
97100 w.xExprCallback = resolveExprStep;
97101 w.xSelectCallback = resolveSelectStep;
97102 w.xSelectCallback2 = 0;
97103 w.u.pNC = pNC;
97104#if SQLITE_MAX_EXPR_DEPTH1000>0
97105 w.pParse->nHeight += pExpr->nHeight;
97106 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
97107 return SQLITE_ERROR1;
97108 }
97109#endif
97110 sqlite3WalkExpr(&w, pExpr);
97111#if SQLITE_MAX_EXPR_DEPTH1000>0
97112 w.pParse->nHeight -= pExpr->nHeight;
97113#endif
97114 assert( EP_Agg==NC_HasAgg )((void) (0));
97115 assert( EP_Win==NC_HasWin )((void) (0));
97116 testcase( pNC->ncFlags & NC_HasAgg );
97117 testcase( pNC->ncFlags & NC_HasWin );
97118 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) )(pExpr)->flags|=(pNC->ncFlags & (0x0010|0x8000));
97119 pNC->ncFlags |= savedHasAgg;
97120 return pNC->nErr>0 || w.pParse->nErr>0;
97121}
97122
97123/*
97124** Resolve all names for all expression in an expression list. This is
97125** just like sqlite3ResolveExprNames() except that it works for an expression
97126** list rather than a single expression.
97127*/
97128SQLITE_PRIVATEstatic int sqlite3ResolveExprListNames(
97129 NameContext *pNC, /* Namespace to resolve expressions in. */
97130 ExprList *pList /* The expression list to be analyzed. */
97131){
97132 int i;
97133 if( pList ){
97134 for(i=0; i<pList->nExpr; i++){
97135 if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort2;
97136 }
97137 }
97138 return WRC_Continue0;
97139}
97140
97141/*
97142** Resolve all names in all expressions of a SELECT and in all
97143** decendents of the SELECT, including compounds off of p->pPrior,
97144** subqueries in expressions, and subqueries used as FROM clause
97145** terms.
97146**
97147** See sqlite3ResolveExprNames() for a description of the kinds of
97148** transformations that occur.
97149**
97150** All SELECT statements should have been expanded using
97151** sqlite3SelectExpand() prior to invoking this routine.
97152*/
97153SQLITE_PRIVATEstatic void sqlite3ResolveSelectNames(
97154 Parse *pParse, /* The parser context */
97155 Select *p, /* The SELECT statement being coded. */
97156 NameContext *pOuterNC /* Name context for parent SELECT statement */
97157){
97158 Walker w;
97159
97160 assert( p!=0 )((void) (0));
97161 w.xExprCallback = resolveExprStep;
97162 w.xSelectCallback = resolveSelectStep;
97163 w.xSelectCallback2 = 0;
97164 w.pParse = pParse;
97165 w.u.pNC = pOuterNC;
97166 sqlite3WalkSelect(&w, p);
97167}
97168
97169/*
97170** Resolve names in expressions that can only reference a single table
97171** or which cannot reference any tables at all. Examples:
97172**
97173** (1) CHECK constraints
97174** (2) WHERE clauses on partial indices
97175** (3) Expressions in indexes on expressions
97176** (4) Expression arguments to VACUUM INTO.
97177**
97178** In all cases except (4), the Expr.iTable value for Expr.op==TK_COLUMN
97179** nodes of the expression is set to -1 and the Expr.iColumn value is
97180** set to the column number. In case (4), TK_COLUMN nodes cause an error.
97181**
97182** Any errors cause an error message to be set in pParse.
97183*/
97184SQLITE_PRIVATEstatic int sqlite3ResolveSelfReference(
97185 Parse *pParse, /* Parsing context */
97186 Table *pTab, /* The table being referenced, or NULL */
97187 int type, /* NC_IsCheck or NC_PartIdx or NC_IdxExpr, or 0 */
97188 Expr *pExpr, /* Expression to resolve. May be NULL. */
97189 ExprList *pList /* Expression list to resolve. May be NULL. */
97190){
97191 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
97192 NameContext sNC; /* Name context for pParse->pNewTable */
97193 int rc;
97194
97195 assert( type==0 || pTab!=0 )((void) (0));
97196 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr || pTab==0 )((void) (0));
97197 memset(&sNC, 0, sizeof(sNC));
97198 memset(&sSrc, 0, sizeof(sSrc));
97199 if( pTab ){
97200 sSrc.nSrc = 1;
97201 sSrc.a[0].zName = pTab->zName;
97202 sSrc.a[0].pTab = pTab;
97203 sSrc.a[0].iCursor = -1;
97204 }
97205 sNC.pParse = pParse;
97206 sNC.pSrcList = &sSrc;
97207 sNC.ncFlags = type | NC_IsDDL0x10000;
97208 if( (rc = sqlite3ResolveExprNames(&sNC, pExpr))!=SQLITE_OK0 ) return rc;
97209 if( pList ) rc = sqlite3ResolveExprListNames(&sNC, pList);
97210 return rc;
97211}
97212
97213/************** End of resolve.c *********************************************/
97214/************** Begin file expr.c ********************************************/
97215/*
97216** 2001 September 15
97217**
97218** The author disclaims copyright to this source code. In place of
97219** a legal notice, here is a blessing:
97220**
97221** May you do good and not evil.
97222** May you find forgiveness for yourself and forgive others.
97223** May you share freely, never taking more than you give.
97224**
97225*************************************************************************
97226** This file contains routines used for analyzing expressions and
97227** for generating VDBE code that evaluates expressions in SQLite.
97228*/
97229/* #include "sqliteInt.h" */
97230
97231/* Forward declarations */
97232static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
97233static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
97234
97235/*
97236** Return the affinity character for a single column of a table.
97237*/
97238SQLITE_PRIVATEstatic char sqlite3TableColumnAffinity(Table *pTab, int iCol){
97239 assert( iCol<pTab->nCol )((void) (0));
97240 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER'D';
97241}
97242
97243/*
97244** Return the 'affinity' of the expression pExpr if any.
97245**
97246** If pExpr is a column, a reference to a column via an 'AS' alias,
97247** or a sub-select with a column as the return value, then the
97248** affinity of that column is returned. Otherwise, 0x00 is returned,
97249** indicating no affinity for the expression.
97250**
97251** i.e. the WHERE clause expressions in the following statements all
97252** have an affinity:
97253**
97254** CREATE TABLE t1(a);
97255** SELECT * FROM t1 WHERE a;
97256** SELECT a AS b FROM t1 WHERE b;
97257** SELECT * FROM t1 WHERE (select a from t1);
97258*/
97259SQLITE_PRIVATEstatic char sqlite3ExprAffinity(Expr *pExpr){
97260 int op;
97261 if( pExpr->flags & EP_Generic0x000200 ) return 0;
97262 while( ExprHasProperty(pExpr, EP_Skip)(((pExpr)->flags&(0x001000))!=0) ){
97263 assert( pExpr->op==TK_COLLATE )((void) (0));
97264 pExpr = pExpr->pLeft;
97265 assert( pExpr!=0 )((void) (0));
97266 }
97267 op = pExpr->op;
97268 if( op==TK_SELECT131 ){
97269 assert( pExpr->flags&EP_xIsSelect )((void) (0));
97270 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
97271 }
97272 if( op==TK_REGISTER168 ) op = pExpr->op2;
97273#ifndef SQLITE_OMIT_CAST
97274 if( op==TK_CAST36 ){
97275 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
97276 return sqlite3AffinityType(pExpr->u.zToken, 0);
97277 }
97278#endif
97279 if( (op==TK_AGG_COLUMN164 || op==TK_COLUMN162) && pExpr->y.pTab ){
97280 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
97281 }
97282 if( op==TK_SELECT_COLUMN170 ){
97283 assert( pExpr->pLeft->flags&EP_xIsSelect )((void) (0));
97284 return sqlite3ExprAffinity(
97285 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
97286 );
97287 }
97288 return pExpr->affinity;
97289}
97290
97291/*
97292** Set the collating sequence for expression pExpr to be the collating
97293** sequence named by pToken. Return a pointer to a new Expr node that
97294** implements the COLLATE operator.
97295**
97296** If a memory allocation error occurs, that fact is recorded in pParse->db
97297** and the pExpr parameter is returned unchanged.
97298*/
97299SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateToken(
97300 Parse *pParse, /* Parsing context */
97301 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
97302 const Token *pCollName, /* Name of collating sequence */
97303 int dequote /* True to dequote pCollName */
97304){
97305 if( pCollName->n>0 ){
97306 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE106, pCollName, dequote);
97307 if( pNew ){
97308 pNew->pLeft = pExpr;
97309 pNew->flags |= EP_Collate0x000100|EP_Skip0x001000;
97310 pExpr = pNew;
97311 }
97312 }
97313 return pExpr;
97314}
97315SQLITE_PRIVATEstatic Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
97316 Token s;
97317 assert( zC!=0 )((void) (0));
97318 sqlite3TokenInit(&s, (char*)zC);
97319 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
97320}
97321
97322/*
97323** Skip over any TK_COLLATE operators and any unlikely()
97324** or likelihood() function at the root of an expression.
97325*/
97326SQLITE_PRIVATEstatic Expr *sqlite3ExprSkipCollate(Expr *pExpr){
97327 while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely)(((pExpr)->flags&(0x001000|0x040000))!=0) ){
97328 if( ExprHasProperty(pExpr, EP_Unlikely)(((pExpr)->flags&(0x040000))!=0) ){
97329 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
97330 assert( pExpr->x.pList->nExpr>0 )((void) (0));
97331 assert( pExpr->op==TK_FUNCTION )((void) (0));
97332 pExpr = pExpr->x.pList->a[0].pExpr;
97333 }else{
97334 assert( pExpr->op==TK_COLLATE )((void) (0));
97335 pExpr = pExpr->pLeft;
97336 }
97337 }
97338 return pExpr;
97339}
97340
97341/*
97342** Return the collation sequence for the expression pExpr. If
97343** there is no defined collating sequence, return NULL.
97344**
97345** See also: sqlite3ExprNNCollSeq()
97346**
97347** The sqlite3ExprNNCollSeq() works the same exact that it returns the
97348** default collation if pExpr has no defined collation.
97349**
97350** The collating sequence might be determined by a COLLATE operator
97351** or by the presence of a column with a defined collating sequence.
97352** COLLATE operators take first precedence. Left operands take
97353** precedence over right operands.
97354*/
97355SQLITE_PRIVATEstatic CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
97356 sqlite3 *db = pParse->db;
97357 CollSeq *pColl = 0;
97358 Expr *p = pExpr;
97359 while( p ){
97360 int op = p->op;
97361 if( p->flags & EP_Generic0x000200 ) break;
97362 if( op==TK_REGISTER168 ) op = p->op2;
97363 if( (op==TK_AGG_COLUMN164 || op==TK_COLUMN162 || op==TK_TRIGGER77)
97364 && p->y.pTab!=0
97365 ){
97366 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
97367 ** a TK_COLUMN but was previously evaluated and cached in a register */
97368 int j = p->iColumn;
97369 if( j>=0 ){
97370 const char *zColl = p->y.pTab->aCol[j].zColl;
97371 pColl = sqlite3FindCollSeq(db, ENC(db)((db)->enc), zColl, 0);
97372 }
97373 break;
97374 }
97375 if( op==TK_CAST36 || op==TK_UPLUS166 ){
97376 p = p->pLeft;
97377 continue;
97378 }
97379 if( op==TK_COLLATE106 ){
97380 pColl = sqlite3GetCollSeq(pParse, ENC(db)((db)->enc), 0, p->u.zToken);
97381 break;
97382 }
97383 if( p->flags & EP_Collate0x000100 ){
97384 if( p->pLeft && (p->pLeft->flags & EP_Collate0x000100)!=0 ){
97385 p = p->pLeft;
97386 }else{
97387 Expr *pNext = p->pRight;
97388 /* The Expr.x union is never used at the same time as Expr.pRight */
97389 assert( p->x.pList==0 || p->pRight==0 )((void) (0));
97390 /* p->flags holds EP_Collate and p->pLeft->flags does not. And
97391 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at
97392 ** least one EP_Collate. Thus the following two ALWAYS. */
97393 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect))(!(((p)->flags&(0x000800))!=0)) ){
97394 int i;
97395 for(i=0; ALWAYS(i<p->x.pList->nExpr)(i<p->x.pList->nExpr); i++){
97396 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate)(((p->x.pList->a[i].pExpr)->flags&(0x000100))!=0
)
){
97397 pNext = p->x.pList->a[i].pExpr;
97398 break;
97399 }
97400 }
97401 }
97402 p = pNext;
97403 }
97404 }else{
97405 break;
97406 }
97407 }
97408 if( sqlite3CheckCollSeq(pParse, pColl) ){
97409 pColl = 0;
97410 }
97411 return pColl;
97412}
97413
97414/*
97415** Return the collation sequence for the expression pExpr. If
97416** there is no defined collating sequence, return a pointer to the
97417** defautl collation sequence.
97418**
97419** See also: sqlite3ExprCollSeq()
97420**
97421** The sqlite3ExprCollSeq() routine works the same except that it
97422** returns NULL if there is no defined collation.
97423*/
97424SQLITE_PRIVATEstatic CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){
97425 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
97426 if( p==0 ) p = pParse->db->pDfltColl;
97427 assert( p!=0 )((void) (0));
97428 return p;
97429}
97430
97431/*
97432** Return TRUE if the two expressions have equivalent collating sequences.
97433*/
97434SQLITE_PRIVATEstatic int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){
97435 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
97436 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
97437 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
97438}
97439
97440/*
97441** pExpr is an operand of a comparison operator. aff2 is the
97442** type affinity of the other operand. This routine returns the
97443** type affinity that should be used for the comparison operator.
97444*/
97445SQLITE_PRIVATEstatic char sqlite3CompareAffinity(Expr *pExpr, char aff2){
97446 char aff1 = sqlite3ExprAffinity(pExpr);
97447 if( aff1 && aff2 ){
97448 /* Both sides of the comparison are columns. If one has numeric
97449 ** affinity, use that. Otherwise use no affinity.
97450 */
97451 if( sqlite3IsNumericAffinity(aff1)((aff1)>='C') || sqlite3IsNumericAffinity(aff2)((aff2)>='C') ){
97452 return SQLITE_AFF_NUMERIC'C';
97453 }else{
97454 return SQLITE_AFF_BLOB'A';
97455 }
97456 }else if( !aff1 && !aff2 ){
97457 /* Neither side of the comparison is a column. Compare the
97458 ** results directly.
97459 */
97460 return SQLITE_AFF_BLOB'A';
97461 }else{
97462 /* One side is a column, the other is not. Use the columns affinity. */
97463 assert( aff1==0 || aff2==0 )((void) (0));
97464 return (aff1 + aff2);
97465 }
97466}
97467
97468/*
97469** pExpr is a comparison operator. Return the type affinity that should
97470** be applied to both operands prior to doing the comparison.
97471*/
97472static char comparisonAffinity(Expr *pExpr){
97473 char aff;
97474 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||((void) (0))
97475 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||((void) (0))
97476 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT )((void) (0));
97477 assert( pExpr->pLeft )((void) (0));
97478 aff = sqlite3ExprAffinity(pExpr->pLeft);
97479 if( pExpr->pRight ){
97480 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
97481 }else if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
97482 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
97483 }else if( aff==0 ){
97484 aff = SQLITE_AFF_BLOB'A';
97485 }
97486 return aff;
97487}
97488
97489/*
97490** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
97491** idx_affinity is the affinity of an indexed column. Return true
97492** if the index with affinity idx_affinity may be used to implement
97493** the comparison in pExpr.
97494*/
97495SQLITE_PRIVATEstatic int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
97496 char aff = comparisonAffinity(pExpr);
97497 switch( aff ){
97498 case SQLITE_AFF_BLOB'A':
97499 return 1;
97500 case SQLITE_AFF_TEXT'B':
97501 return idx_affinity==SQLITE_AFF_TEXT'B';
97502 default:
97503 return sqlite3IsNumericAffinity(idx_affinity)((idx_affinity)>='C');
97504 }
97505}
97506
97507/*
97508** Return the P5 value that should be used for a binary comparison
97509** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
97510*/
97511static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
97512 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
97513 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
97514 return aff;
97515}
97516
97517/*
97518** Return a pointer to the collation sequence that should be used by
97519** a binary comparison operator comparing pLeft and pRight.
97520**
97521** If the left hand expression has a collating sequence type, then it is
97522** used. Otherwise the collation sequence for the right hand expression
97523** is used, or the default (BINARY) if neither expression has a collating
97524** type.
97525**
97526** Argument pRight (but not pLeft) may be a null pointer. In this case,
97527** it is not considered.
97528*/
97529SQLITE_PRIVATEstatic CollSeq *sqlite3BinaryCompareCollSeq(
97530 Parse *pParse,
97531 Expr *pLeft,
97532 Expr *pRight
97533){
97534 CollSeq *pColl;
97535 assert( pLeft )((void) (0));
97536 if( pLeft->flags & EP_Collate0x000100 ){
97537 pColl = sqlite3ExprCollSeq(pParse, pLeft);
97538 }else if( pRight && (pRight->flags & EP_Collate0x000100)!=0 ){
97539 pColl = sqlite3ExprCollSeq(pParse, pRight);
97540 }else{
97541 pColl = sqlite3ExprCollSeq(pParse, pLeft);
97542 if( !pColl ){
97543 pColl = sqlite3ExprCollSeq(pParse, pRight);
97544 }
97545 }
97546 return pColl;
97547}
97548
97549/*
97550** Generate code for a comparison operator.
97551*/
97552static int codeCompare(
97553 Parse *pParse, /* The parsing (and code generating) context */
97554 Expr *pLeft, /* The left operand */
97555 Expr *pRight, /* The right operand */
97556 int opcode, /* The comparison opcode */
97557 int in1, int in2, /* Register holding operands */
97558 int dest, /* Jump here if true. */
97559 int jumpIfNull /* If true, jump if either operand is NULL */
97560){
97561 int p5;
97562 int addr;
97563 CollSeq *p4;
97564
97565 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
97566 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
97567 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
97568 (void*)p4, P4_COLLSEQ(-2));
97569 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
97570 return addr;
97571}
97572
97573/*
97574** Return true if expression pExpr is a vector, or false otherwise.
97575**
97576** A vector is defined as any expression that results in two or more
97577** columns of result. Every TK_VECTOR node is an vector because the
97578** parser will not generate a TK_VECTOR with fewer than two entries.
97579** But a TK_SELECT might be either a vector or a scalar. It is only
97580** considered a vector if it has two or more result columns.
97581*/
97582SQLITE_PRIVATEstatic int sqlite3ExprIsVector(Expr *pExpr){
97583 return sqlite3ExprVectorSize(pExpr)>1;
97584}
97585
97586/*
97587** If the expression passed as the only argument is of type TK_VECTOR
97588** return the number of expressions in the vector. Or, if the expression
97589** is a sub-select, return the number of columns in the sub-select. For
97590** any other type of expression, return 1.
97591*/
97592SQLITE_PRIVATEstatic int sqlite3ExprVectorSize(Expr *pExpr){
97593 u8 op = pExpr->op;
97594 if( op==TK_REGISTER168 ) op = pExpr->op2;
97595 if( op==TK_VECTOR169 ){
97596 return pExpr->x.pList->nExpr;
97597 }else if( op==TK_SELECT131 ){
97598 return pExpr->x.pSelect->pEList->nExpr;
97599 }else{
97600 return 1;
97601 }
97602}
97603
97604/*
97605** Return a pointer to a subexpression of pVector that is the i-th
97606** column of the vector (numbered starting with 0). The caller must
97607** ensure that i is within range.
97608**
97609** If pVector is really a scalar (and "scalar" here includes subqueries
97610** that return a single column!) then return pVector unmodified.
97611**
97612** pVector retains ownership of the returned subexpression.
97613**
97614** If the vector is a (SELECT ...) then the expression returned is
97615** just the expression for the i-th term of the result set, and may
97616** not be ready for evaluation because the table cursor has not yet
97617** been positioned.
97618*/
97619SQLITE_PRIVATEstatic Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
97620 assert( i<sqlite3ExprVectorSize(pVector) )((void) (0));
97621 if( sqlite3ExprIsVector(pVector) ){
97622 assert( pVector->op2==0 || pVector->op==TK_REGISTER )((void) (0));
97623 if( pVector->op==TK_SELECT131 || pVector->op2==TK_SELECT131 ){
97624 return pVector->x.pSelect->pEList->a[i].pExpr;
97625 }else{
97626 return pVector->x.pList->a[i].pExpr;
97627 }
97628 }
97629 return pVector;
97630}
97631
97632/*
97633** Compute and return a new Expr object which when passed to
97634** sqlite3ExprCode() will generate all necessary code to compute
97635** the iField-th column of the vector expression pVector.
97636**
97637** It is ok for pVector to be a scalar (as long as iField==0).
97638** In that case, this routine works like sqlite3ExprDup().
97639**
97640** The caller owns the returned Expr object and is responsible for
97641** ensuring that the returned value eventually gets freed.
97642**
97643** The caller retains ownership of pVector. If pVector is a TK_SELECT,
97644** then the returned object will reference pVector and so pVector must remain
97645** valid for the life of the returned object. If pVector is a TK_VECTOR
97646** or a scalar expression, then it can be deleted as soon as this routine
97647** returns.
97648**
97649** A trick to cause a TK_SELECT pVector to be deleted together with
97650** the returned Expr object is to attach the pVector to the pRight field
97651** of the returned TK_SELECT_COLUMN Expr object.
97652*/
97653SQLITE_PRIVATEstatic Expr *sqlite3ExprForVectorField(
97654 Parse *pParse, /* Parsing context */
97655 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
97656 int iField /* Which column of the vector to return */
97657){
97658 Expr *pRet;
97659 if( pVector->op==TK_SELECT131 ){
97660 assert( pVector->flags & EP_xIsSelect )((void) (0));
97661 /* The TK_SELECT_COLUMN Expr node:
97662 **
97663 ** pLeft: pVector containing TK_SELECT. Not deleted.
97664 ** pRight: not used. But recursively deleted.
97665 ** iColumn: Index of a column in pVector
97666 ** iTable: 0 or the number of columns on the LHS of an assignment
97667 ** pLeft->iTable: First in an array of register holding result, or 0
97668 ** if the result is not yet computed.
97669 **
97670 ** sqlite3ExprDelete() specifically skips the recursive delete of
97671 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
97672 ** can be attached to pRight to cause this node to take ownership of
97673 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
97674 ** with the same pLeft pointer to the pVector, but only one of them
97675 ** will own the pVector.
97676 */
97677 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN170, 0, 0);
97678 if( pRet ){
97679 pRet->iColumn = iField;
97680 pRet->pLeft = pVector;
97681 }
97682 assert( pRet==0 || pRet->iTable==0 )((void) (0));
97683 }else{
97684 if( pVector->op==TK_VECTOR169 ) pVector = pVector->x.pList->a[iField].pExpr;
97685 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
97686 sqlite3RenameTokenRemap(pParse, pRet, pVector);
97687 }
97688 return pRet;
97689}
97690
97691/*
97692** If expression pExpr is of type TK_SELECT, generate code to evaluate
97693** it. Return the register in which the result is stored (or, if the
97694** sub-select returns more than one column, the first in an array
97695** of registers in which the result is stored).
97696**
97697** If pExpr is not a TK_SELECT expression, return 0.
97698*/
97699static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
97700 int reg = 0;
97701#ifndef SQLITE_OMIT_SUBQUERY
97702 if( pExpr->op==TK_SELECT131 ){
97703 reg = sqlite3CodeSubselect(pParse, pExpr);
97704 }
97705#endif
97706 return reg;
97707}
97708
97709/*
97710** Argument pVector points to a vector expression - either a TK_VECTOR
97711** or TK_SELECT that returns more than one column. This function returns
97712** the register number of a register that contains the value of
97713** element iField of the vector.
97714**
97715** If pVector is a TK_SELECT expression, then code for it must have
97716** already been generated using the exprCodeSubselect() routine. In this
97717** case parameter regSelect should be the first in an array of registers
97718** containing the results of the sub-select.
97719**
97720** If pVector is of type TK_VECTOR, then code for the requested field
97721** is generated. In this case (*pRegFree) may be set to the number of
97722** a temporary register to be freed by the caller before returning.
97723**
97724** Before returning, output parameter (*ppExpr) is set to point to the
97725** Expr object corresponding to element iElem of the vector.
97726*/
97727static int exprVectorRegister(
97728 Parse *pParse, /* Parse context */
97729 Expr *pVector, /* Vector to extract element from */
97730 int iField, /* Field to extract from pVector */
97731 int regSelect, /* First in array of registers */
97732 Expr **ppExpr, /* OUT: Expression element */
97733 int *pRegFree /* OUT: Temp register to free */
97734){
97735 u8 op = pVector->op;
97736 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT )((void) (0));
97737 if( op==TK_REGISTER168 ){
97738 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
97739 return pVector->iTable+iField;
97740 }
97741 if( op==TK_SELECT131 ){
97742 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
97743 return regSelect+iField;
97744 }
97745 *ppExpr = pVector->x.pList->a[iField].pExpr;
97746 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
97747}
97748
97749/*
97750** Expression pExpr is a comparison between two vector values. Compute
97751** the result of the comparison (1, 0, or NULL) and write that
97752** result into register dest.
97753**
97754** The caller must satisfy the following preconditions:
97755**
97756** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
97757** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
97758** otherwise: op==pExpr->op and p5==0
97759*/
97760static void codeVectorCompare(
97761 Parse *pParse, /* Code generator context */
97762 Expr *pExpr, /* The comparison operation */
97763 int dest, /* Write results into this register */
97764 u8 op, /* Comparison operator */
97765 u8 p5 /* SQLITE_NULLEQ or zero */
97766){
97767 Vdbe *v = pParse->pVdbe;
97768 Expr *pLeft = pExpr->pLeft;
97769 Expr *pRight = pExpr->pRight;
97770 int nLeft = sqlite3ExprVectorSize(pLeft);
97771 int i;
97772 int regLeft = 0;
97773 int regRight = 0;
97774 u8 opx = op;
97775 int addrDone = sqlite3VdbeMakeLabel(pParse);
97776
97777 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
97778 sqlite3ErrorMsg(pParse, "row value misused");
97779 return;
97780 }
97781 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE((void) (0))
97782 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT((void) (0))
97783 || pExpr->op==TK_LT || pExpr->op==TK_GT((void) (0))
97784 || pExpr->op==TK_LE || pExpr->op==TK_GE((void) (0))
97785 )((void) (0));
97786 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)((void) (0))
97787 || (pExpr->op==TK_ISNOT && op==TK_NE) )((void) (0));
97788 assert( p5==0 || pExpr->op!=op )((void) (0));
97789 assert( p5==SQLITE_NULLEQ || pExpr->op==op )((void) (0));
97790
97791 p5 |= SQLITE_STOREP20x20;
97792 if( opx==TK_LE55 ) opx = TK_LT56;
97793 if( opx==TK_GE57 ) opx = TK_GT54;
97794
97795 regLeft = exprCodeSubselect(pParse, pLeft);
97796 regRight = exprCodeSubselect(pParse, pRight);
97797
97798 for(i=0; 1 /*Loop exits by "break"*/; i++){
97799 int regFree1 = 0, regFree2 = 0;
97800 Expr *pL, *pR;
97801 int r1, r2;
97802 assert( i>=0 && i<nLeft )((void) (0));
97803 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
97804 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
97805 codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5);
97806 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
97807 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
97808 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
97809 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
97810 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
97811 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
97812 sqlite3ReleaseTempReg(pParse, regFree1);
97813 sqlite3ReleaseTempReg(pParse, regFree2);
97814 if( i==nLeft-1 ){
97815 break;
97816 }
97817 if( opx==TK_EQ53 ){
97818 sqlite3VdbeAddOp2(v, OP_IfNot20, dest, addrDone); VdbeCoverage(v);
97819 p5 |= SQLITE_KEEPNULL0x08;
97820 }else if( opx==TK_NE52 ){
97821 sqlite3VdbeAddOp2(v, OP_If18, dest, addrDone); VdbeCoverage(v);
97822 p5 |= SQLITE_KEEPNULL0x08;
97823 }else{
97824 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE )((void) (0));
97825 sqlite3VdbeAddOp2(v, OP_ElseNotEq58, 0, addrDone);
97826 VdbeCoverageIf(v, op==TK_LT);
97827 VdbeCoverageIf(v, op==TK_GT);
97828 VdbeCoverageIf(v, op==TK_LE);
97829 VdbeCoverageIf(v, op==TK_GE);
97830 if( i==nLeft-2 ) opx = op;
97831 }
97832 }
97833 sqlite3VdbeResolveLabel(v, addrDone);
97834}
97835
97836#if SQLITE_MAX_EXPR_DEPTH1000>0
97837/*
97838** Check that argument nHeight is less than or equal to the maximum
97839** expression depth allowed. If it is not, leave an error message in
97840** pParse.
97841*/
97842SQLITE_PRIVATEstatic int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
97843 int rc = SQLITE_OK0;
97844 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH3];
97845 if( nHeight>mxHeight ){
97846 sqlite3ErrorMsg(pParse,
97847 "Expression tree is too large (maximum depth %d)", mxHeight
97848 );
97849 rc = SQLITE_ERROR1;
97850 }
97851 return rc;
97852}
97853
97854/* The following three functions, heightOfExpr(), heightOfExprList()
97855** and heightOfSelect(), are used to determine the maximum height
97856** of any expression tree referenced by the structure passed as the
97857** first argument.
97858**
97859** If this maximum height is greater than the current value pointed
97860** to by pnHeight, the second parameter, then set *pnHeight to that
97861** value.
97862*/
97863static void heightOfExpr(Expr *p, int *pnHeight){
97864 if( p ){
97865 if( p->nHeight>*pnHeight ){
97866 *pnHeight = p->nHeight;
97867 }
97868 }
97869}
97870static void heightOfExprList(ExprList *p, int *pnHeight){
97871 if( p ){
97872 int i;
97873 for(i=0; i<p->nExpr; i++){
97874 heightOfExpr(p->a[i].pExpr, pnHeight);
97875 }
97876 }
97877}
97878static void heightOfSelect(Select *pSelect, int *pnHeight){
97879 Select *p;
97880 for(p=pSelect; p; p=p->pPrior){
97881 heightOfExpr(p->pWhere, pnHeight);
97882 heightOfExpr(p->pHaving, pnHeight);
97883 heightOfExpr(p->pLimit, pnHeight);
97884 heightOfExprList(p->pEList, pnHeight);
97885 heightOfExprList(p->pGroupBy, pnHeight);
97886 heightOfExprList(p->pOrderBy, pnHeight);
97887 }
97888}
97889
97890/*
97891** Set the Expr.nHeight variable in the structure passed as an
97892** argument. An expression with no children, Expr.pList or
97893** Expr.pSelect member has a height of 1. Any other expression
97894** has a height equal to the maximum height of any other
97895** referenced Expr plus one.
97896**
97897** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
97898** if appropriate.
97899*/
97900static void exprSetHeight(Expr *p){
97901 int nHeight = 0;
97902 heightOfExpr(p->pLeft, &nHeight);
97903 heightOfExpr(p->pRight, &nHeight);
97904 if( ExprHasProperty(p, EP_xIsSelect)(((p)->flags&(0x000800))!=0) ){
97905 heightOfSelect(p->x.pSelect, &nHeight);
97906 }else if( p->x.pList ){
97907 heightOfExprList(p->x.pList, &nHeight);
97908 p->flags |= EP_Propagate(0x000100|0x200000|0x000004) & sqlite3ExprListFlags(p->x.pList);
97909 }
97910 p->nHeight = nHeight + 1;
97911}
97912
97913/*
97914** Set the Expr.nHeight variable using the exprSetHeight() function. If
97915** the height is greater than the maximum allowed expression depth,
97916** leave an error in pParse.
97917**
97918** Also propagate all EP_Propagate flags from the Expr.x.pList into
97919** Expr.flags.
97920*/
97921SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
97922 if( pParse->nErr ) return;
97923 exprSetHeight(p);
97924 sqlite3ExprCheckHeight(pParse, p->nHeight);
97925}
97926
97927/*
97928** Return the maximum height of any expression tree referenced
97929** by the select statement passed as an argument.
97930*/
97931SQLITE_PRIVATEstatic int sqlite3SelectExprHeight(Select *p){
97932 int nHeight = 0;
97933 heightOfSelect(p, &nHeight);
97934 return nHeight;
97935}
97936#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
97937/*
97938** Propagate all EP_Propagate flags from the Expr.x.pList into
97939** Expr.flags.
97940*/
97941SQLITE_PRIVATEstatic void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
97942 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect)(((p)->flags&(0x000800))!=0) ){
97943 p->flags |= EP_Propagate(0x000100|0x200000|0x000004) & sqlite3ExprListFlags(p->x.pList);
97944 }
97945}
97946#define exprSetHeight(y)
97947#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
97948
97949/*
97950** This routine is the core allocator for Expr nodes.
97951**
97952** Construct a new expression node and return a pointer to it. Memory
97953** for this node and for the pToken argument is a single allocation
97954** obtained from sqlite3DbMalloc(). The calling function
97955** is responsible for making sure the node eventually gets freed.
97956**
97957** If dequote is true, then the token (if it exists) is dequoted.
97958** If dequote is false, no dequoting is performed. The deQuote
97959** parameter is ignored if pToken is NULL or if the token does not
97960** appear to be quoted. If the quotes were of the form "..." (double-quotes)
97961** then the EP_DblQuoted flag is set on the expression node.
97962**
97963** Special case: If op==TK_INTEGER and pToken points to a string that
97964** can be translated into a 32-bit integer, then the token is not
97965** stored in u.zToken. Instead, the integer values is written
97966** into u.iValue and the EP_IntValue flag is set. No extra storage
97967** is allocated to hold the integer text and the dequote flag is ignored.
97968*/
97969SQLITE_PRIVATEstatic Expr *sqlite3ExprAlloc(
97970 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
97971 int op, /* Expression opcode */
97972 const Token *pToken, /* Token argument. Might be NULL */
97973 int dequote /* True to dequote */
97974){
97975 Expr *pNew;
97976 int nExtra = 0;
97977 int iValue = 0;
97978
97979 assert( db!=0 )((void) (0));
97980 if( pToken ){
97981 if( op!=TK_INTEGER147 || pToken->z==0
97982 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
97983 nExtra = pToken->n+1;
97984 assert( iValue>=0 )((void) (0));
97985 }
97986 }
97987 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
97988 if( pNew ){
97989 memset(pNew, 0, sizeof(Expr));
97990 pNew->op = (u8)op;
97991 pNew->iAgg = -1;
97992 if( pToken ){
97993 if( nExtra==0 ){
97994 pNew->flags |= EP_IntValue0x000400|EP_Leaf0x800000|(iValue?EP_IsTrue0x10000000:EP_IsFalse0x20000000);
97995 pNew->u.iValue = iValue;
97996 }else{
97997 pNew->u.zToken = (char*)&pNew[1];
97998 assert( pToken->z!=0 || pToken->n==0 )((void) (0));
97999 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
98000 pNew->u.zToken[pToken->n] = 0;
98001 if( dequote && sqlite3Isquote(pNew->u.zToken[0])(sqlite3CtypeMap[(unsigned char)(pNew->u.zToken[0])]&0x80
)
){
98002 sqlite3DequoteExpr(pNew);
98003 }
98004 }
98005 }
98006#if SQLITE_MAX_EXPR_DEPTH1000>0
98007 pNew->nHeight = 1;
98008#endif
98009 }
98010 return pNew;
98011}
98012
98013/*
98014** Allocate a new expression node from a zero-terminated token that has
98015** already been dequoted.
98016*/
98017SQLITE_PRIVATEstatic Expr *sqlite3Expr(
98018 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
98019 int op, /* Expression opcode */
98020 const char *zToken /* Token argument. Might be NULL */
98021){
98022 Token x;
98023 x.z = zToken;
98024 x.n = sqlite3Strlen30(zToken);
98025 return sqlite3ExprAlloc(db, op, &x, 0);
98026}
98027
98028/*
98029** Attach subtrees pLeft and pRight to the Expr node pRoot.
98030**
98031** If pRoot==NULL that means that a memory allocation error has occurred.
98032** In that case, delete the subtrees pLeft and pRight.
98033*/
98034SQLITE_PRIVATEstatic void sqlite3ExprAttachSubtrees(
98035 sqlite3 *db,
98036 Expr *pRoot,
98037 Expr *pLeft,
98038 Expr *pRight
98039){
98040 if( pRoot==0 ){
98041 assert( db->mallocFailed )((void) (0));
98042 sqlite3ExprDelete(db, pLeft);
98043 sqlite3ExprDelete(db, pRight);
98044 }else{
98045 if( pRight ){
98046 pRoot->pRight = pRight;
98047 pRoot->flags |= EP_Propagate(0x000100|0x200000|0x000004) & pRight->flags;
98048 }
98049 if( pLeft ){
98050 pRoot->pLeft = pLeft;
98051 pRoot->flags |= EP_Propagate(0x000100|0x200000|0x000004) & pLeft->flags;
98052 }
98053 exprSetHeight(pRoot);
98054 }
98055}
98056
98057/*
98058** Allocate an Expr node which joins as many as two subtrees.
98059**
98060** One or both of the subtrees can be NULL. Return a pointer to the new
98061** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
98062** free the subtrees and return NULL.
98063*/
98064SQLITE_PRIVATEstatic Expr *sqlite3PExpr(
98065 Parse *pParse, /* Parsing context */
98066 int op, /* Expression opcode */
98067 Expr *pLeft, /* Left operand */
98068 Expr *pRight /* Right operand */
98069){
98070 Expr *p;
98071 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
98072 if( p ){
98073 memset(p, 0, sizeof(Expr));
98074 p->op = op & 0xff;
98075 p->iAgg = -1;
98076 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
98077 sqlite3ExprCheckHeight(pParse, p->nHeight);
98078 }else{
98079 sqlite3ExprDelete(pParse->db, pLeft);
98080 sqlite3ExprDelete(pParse->db, pRight);
98081 }
98082 return p;
98083}
98084
98085/*
98086** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
98087** do a memory allocation failure) then delete the pSelect object.
98088*/
98089SQLITE_PRIVATEstatic void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
98090 if( pExpr ){
98091 pExpr->x.pSelect = pSelect;
98092 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery)(pExpr)->flags|=(0x000800|0x200000);
98093 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
98094 }else{
98095 assert( pParse->db->mallocFailed )((void) (0));
98096 sqlite3SelectDelete(pParse->db, pSelect);
98097 }
98098}
98099
98100
98101/*
98102** Join two expressions using an AND operator. If either expression is
98103** NULL, then just return the other expression.
98104**
98105** If one side or the other of the AND is known to be false, then instead
98106** of returning an AND expression, just return a constant expression with
98107** a value of false.
98108*/
98109SQLITE_PRIVATEstatic Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
98110 sqlite3 *db = pParse->db;
98111 if( pLeft==0 ){
98112 return pRight;
98113 }else if( pRight==0 ){
98114 return pLeft;
98115 }else if( ExprAlwaysFalse(pLeft)(((pLeft)->flags&(0x000001|0x20000000))==0x20000000) || ExprAlwaysFalse(pRight)(((pRight)->flags&(0x000001|0x20000000))==0x20000000) ){
98116 sqlite3ExprUnmapAndDelete(pParse, pLeft);
98117 sqlite3ExprUnmapAndDelete(pParse, pRight);
98118 return sqlite3ExprAlloc(db, TK_INTEGER147, &sqlite3IntTokens[0], 0);
98119 }else{
98120 return sqlite3PExpr(pParse, TK_AND44, pLeft, pRight);
98121 }
98122}
98123
98124/*
98125** Construct a new expression node for a function with multiple
98126** arguments.
98127*/
98128SQLITE_PRIVATEstatic Expr *sqlite3ExprFunction(
98129 Parse *pParse, /* Parsing context */
98130 ExprList *pList, /* Argument list */
98131 Token *pToken, /* Name of the function */
98132 int eDistinct /* SF_Distinct or SF_ALL or 0 */
98133){
98134 Expr *pNew;
98135 sqlite3 *db = pParse->db;
98136 assert( pToken )((void) (0));
98137 pNew = sqlite3ExprAlloc(db, TK_FUNCTION161, pToken, 1);
98138 if( pNew==0 ){
98139 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
98140 return 0;
98141 }
98142 if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG6] ){
98143 sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
98144 }
98145 pNew->x.pList = pList;
98146 ExprSetProperty(pNew, EP_HasFunc)(pNew)->flags|=(0x000004);
98147 assert( !ExprHasProperty(pNew, EP_xIsSelect) )((void) (0));
98148 sqlite3ExprSetHeightAndFlags(pParse, pNew);
98149 if( eDistinct==SF_Distinct0x00001 ) ExprSetProperty(pNew, EP_Distinct)(pNew)->flags|=(0x000002);
98150 return pNew;
98151}
98152
98153/*
98154** Assign a variable number to an expression that encodes a wildcard
98155** in the original SQL statement.
98156**
98157** Wildcards consisting of a single "?" are assigned the next sequential
98158** variable number.
98159**
98160** Wildcards of the form "?nnn" are assigned the number "nnn". We make
98161** sure "nnn" is not too big to avoid a denial of service attack when
98162** the SQL statement comes from an external source.
98163**
98164** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
98165** as the previous instance of the same wildcard. Or if this is the first
98166** instance of the wildcard, the next sequential variable number is
98167** assigned.
98168*/
98169SQLITE_PRIVATEstatic void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
98170 sqlite3 *db = pParse->db;
98171 const char *z;
98172 ynVar x;
98173
98174 if( pExpr==0 ) return;
98175 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) )((void) (0));
98176 z = pExpr->u.zToken;
98177 assert( z!=0 )((void) (0));
98178 assert( z[0]!=0 )((void) (0));
98179 assert( n==(u32)sqlite3Strlen30(z) )((void) (0));
98180 if( z[1]==0 ){
98181 /* Wildcard of the form "?". Assign the next variable number */
98182 assert( z[0]=='?' )((void) (0));
98183 x = (ynVar)(++pParse->nVar);
98184 }else{
98185 int doAdd = 0;
98186 if( z[0]=='?' ){
98187 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
98188 ** use it as the variable number */
98189 i64 i;
98190 int bOk;
98191 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
98192 i = z[1]-'0'; /* The common case of ?N for a single digit N */
98193 bOk = 1;
98194 }else{
98195 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF81);
98196 }
98197 testcase( i==0 );
98198 testcase( i==1 );
98199 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
98200 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
98201 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9] ){
98202 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
98203 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9]);
98204 return;
98205 }
98206 x = (ynVar)i;
98207 if( x>pParse->nVar ){
98208 pParse->nVar = (int)x;
98209 doAdd = 1;
98210 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
98211 doAdd = 1;
98212 }
98213 }else{
98214 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
98215 ** number as the prior appearance of the same name, or if the name
98216 ** has never appeared before, reuse the same variable number
98217 */
98218 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
98219 if( x==0 ){
98220 x = (ynVar)(++pParse->nVar);
98221 doAdd = 1;
98222 }
98223 }
98224 if( doAdd ){
98225 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
98226 }
98227 }
98228 pExpr->iColumn = x;
98229 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER9] ){
98230 sqlite3ErrorMsg(pParse, "too many SQL variables");
98231 }
98232}
98233
98234/*
98235** Recursively delete an expression tree.
98236*/
98237static SQLITE_NOINLINE__attribute__((noinline)) void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
98238 assert( p!=0 )((void) (0));
98239 /* Sanity check: Assert that the IntValue is non-negative if it exists */
98240 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 )((void) (0));
98241
98242 assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed )((void) (0));
98243 assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)((void) (0))
98244 || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) )((void) (0));
98245#ifdef SQLITE_DEBUG
98246 if( ExprHasProperty(p, EP_Leaf)(((p)->flags&(0x800000))!=0) && !ExprHasProperty(p, EP_TokenOnly)(((p)->flags&(0x004000))!=0) ){
98247 assert( p->pLeft==0 )((void) (0));
98248 assert( p->pRight==0 )((void) (0));
98249 assert( p->x.pSelect==0 )((void) (0));
98250 }
98251#endif
98252 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf))(((p)->flags&((0x004000|0x800000)))!=0) ){
98253 /* The Expr.x union is never used at the same time as Expr.pRight */
98254 assert( p->x.pList==0 || p->pRight==0 )((void) (0));
98255 if( p->pLeft && p->op!=TK_SELECT_COLUMN170 ) sqlite3ExprDeleteNN(db, p->pLeft);
98256 if( p->pRight ){
98257 sqlite3ExprDeleteNN(db, p->pRight);
98258 }else if( ExprHasProperty(p, EP_xIsSelect)(((p)->flags&(0x000800))!=0) ){
98259 sqlite3SelectDelete(db, p->x.pSelect);
98260 }else{
98261 sqlite3ExprListDelete(db, p->x.pList);
98262 }
98263 if( ExprHasProperty(p, EP_WinFunc)(((p)->flags&(0x1000000))!=0) ){
98264 assert( p->op==TK_FUNCTION )((void) (0));
98265 sqlite3WindowDelete(db, p->y.pWin);
98266 }
98267 }
98268 if( ExprHasProperty(p, EP_MemToken)(((p)->flags&(0x010000))!=0) ) sqlite3DbFree(db, p->u.zToken);
98269 if( !ExprHasProperty(p, EP_Static)(((p)->flags&(0x8000000))!=0) ){
98270 sqlite3DbFreeNN(db, p);
98271 }
98272}
98273SQLITE_PRIVATEstatic void sqlite3ExprDelete(sqlite3 *db, Expr *p){
98274 if( p ) sqlite3ExprDeleteNN(db, p);
98275}
98276
98277/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
98278** expression.
98279*/
98280SQLITE_PRIVATEstatic void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){
98281 if( p ){
98282 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
98283 sqlite3RenameExprUnmap(pParse, p);
98284 }
98285 sqlite3ExprDeleteNN(pParse->db, p);
98286 }
98287}
98288
98289/*
98290** Return the number of bytes allocated for the expression structure
98291** passed as the first argument. This is always one of EXPR_FULLSIZE,
98292** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
98293*/
98294static int exprStructSize(Expr *p){
98295 if( ExprHasProperty(p, EP_TokenOnly)(((p)->flags&(0x004000))!=0) ) return EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft);
98296 if( ExprHasProperty(p, EP_Reduced)(((p)->flags&(0x002000))!=0) ) return EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable);
98297 return EXPR_FULLSIZEsizeof(Expr);
98298}
98299
98300/*
98301** Copy the complete content of an Expr node, taking care not to read
98302** past the end of the structure for a reduced-size version of the source
98303** Expr.
98304*/
98305static void exprNodeCopy(Expr *pDest, Expr *pSrc){
98306 memset(pDest, 0, sizeof(Expr));
98307 memcpy(pDest, pSrc, exprStructSize(pSrc));
98308}
98309
98310/*
98311** The dupedExpr*Size() routines each return the number of bytes required
98312** to store a copy of an expression or expression tree. They differ in
98313** how much of the tree is measured.
98314**
98315** dupedExprStructSize() Size of only the Expr structure
98316** dupedExprNodeSize() Size of Expr + space for token
98317** dupedExprSize() Expr + token + subtree components
98318**
98319***************************************************************************
98320**
98321** The dupedExprStructSize() function returns two values OR-ed together:
98322** (1) the space required for a copy of the Expr structure only and
98323** (2) the EP_xxx flags that indicate what the structure size should be.
98324** The return values is always one of:
98325**
98326** EXPR_FULLSIZE
98327** EXPR_REDUCEDSIZE | EP_Reduced
98328** EXPR_TOKENONLYSIZE | EP_TokenOnly
98329**
98330** The size of the structure can be found by masking the return value
98331** of this routine with 0xfff. The flags can be found by masking the
98332** return value with EP_Reduced|EP_TokenOnly.
98333**
98334** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
98335** (unreduced) Expr objects as they or originally constructed by the parser.
98336** During expression analysis, extra information is computed and moved into
98337** later parts of the Expr object and that extra information might get chopped
98338** off if the expression is reduced. Note also that it does not work to
98339** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
98340** to reduce a pristine expression tree from the parser. The implementation
98341** of dupedExprStructSize() contain multiple assert() statements that attempt
98342** to enforce this constraint.
98343*/
98344static int dupedExprStructSize(Expr *p, int flags){
98345 int nSize;
98346 assert( flags==EXPRDUP_REDUCE || flags==0 )((void) (0)); /* Only one flag value allowed */
98347 assert( EXPR_FULLSIZE<=0xfff )((void) (0));
98348 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 )((void) (0));
98349 if( 0==flags || p->op==TK_SELECT_COLUMN170
98350#ifndef SQLITE_OMIT_WINDOWFUNC
98351 || ExprHasProperty(p, EP_WinFunc)(((p)->flags&(0x1000000))!=0)
98352#endif
98353 ){
98354 nSize = EXPR_FULLSIZEsizeof(Expr);
98355 }else{
98356 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) )((void) (0));
98357 assert( !ExprHasProperty(p, EP_FromJoin) )((void) (0));
98358 assert( !ExprHasProperty(p, EP_MemToken) )((void) (0));
98359 assert( !ExprHasProperty(p, EP_NoReduce) )((void) (0));
98360 if( p->pLeft || p->x.pList ){
98361 nSize = EXPR_REDUCEDSIZE__builtin_offsetof(Expr, iTable) | EP_Reduced0x002000;
98362 }else{
98363 assert( p->pRight==0 )((void) (0));
98364 nSize = EXPR_TOKENONLYSIZE__builtin_offsetof(Expr, pLeft) | EP_TokenOnly0x004000;
98365 }
98366 }
98367 return nSize;
98368}
98369
98370/*
98371** This function returns the space in bytes required to store the copy
98372** of the Expr structure and a copy of the Expr.u.zToken string (if that
98373** string is defined.)
98374*/
98375static int dupedExprNodeSize(Expr *p, int flags){
98376 int nByte = dupedExprStructSize(p, flags) & 0xfff;
98377 if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(0x000400))!=0) && p->u.zToken ){
98378 nByte += sqlite3Strlen30NN(p->u.zToken)(strlen(p->u.zToken)&0x3fffffff)+1;
98379 }
98380 return ROUND8(nByte)(((nByte)+7)&~7);
98381}
98382
98383/*
98384** Return the number of bytes required to create a duplicate of the
98385** expression passed as the first argument. The second argument is a
98386** mask containing EXPRDUP_XXX flags.
98387**
98388** The value returned includes space to create a copy of the Expr struct
98389** itself and the buffer referred to by Expr.u.zToken, if any.
98390**
98391** If the EXPRDUP_REDUCE flag is set, then the return value includes
98392** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
98393** and Expr.pRight variables (but not for any structures pointed to or
98394** descended from the Expr.x.pList or Expr.x.pSelect variables).
98395*/
98396static int dupedExprSize(Expr *p, int flags){
98397 int nByte = 0;
98398 if( p ){
98399 nByte = dupedExprNodeSize(p, flags);
98400 if( flags&EXPRDUP_REDUCE0x0001 ){
98401 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
98402 }
98403 }
98404 return nByte;
98405}
98406
98407/*
98408** This function is similar to sqlite3ExprDup(), except that if pzBuffer
98409** is not NULL then *pzBuffer is assumed to point to a buffer large enough
98410** to store the copy of expression p, the copies of p->u.zToken
98411** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
98412** if any. Before returning, *pzBuffer is set to the first byte past the
98413** portion of the buffer copied into by this function.
98414*/
98415static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
98416 Expr *pNew; /* Value to return */
98417 u8 *zAlloc; /* Memory space from which to build Expr object */
98418 u32 staticFlag; /* EP_Static if space not obtained from malloc */
98419
98420 assert( db!=0 )((void) (0));
98421 assert( p )((void) (0));
98422 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE )((void) (0));
98423 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE )((void) (0));
98424
98425 /* Figure out where to write the new Expr structure. */
98426 if( pzBuffer ){
98427 zAlloc = *pzBuffer;
98428 staticFlag = EP_Static0x8000000;
98429 }else{
98430 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
98431 staticFlag = 0;
98432 }
98433 pNew = (Expr *)zAlloc;
98434
98435 if( pNew ){
98436 /* Set nNewSize to the size allocated for the structure pointed to
98437 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
98438 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
98439 ** by the copy of the p->u.zToken string (if any).
98440 */
98441 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
98442 const int nNewSize = nStructSize & 0xfff;
98443 int nToken;
98444 if( !ExprHasProperty(p, EP_IntValue)(((p)->flags&(0x000400))!=0) && p->u.zToken ){
98445 nToken = sqlite3Strlen30(p->u.zToken) + 1;
98446 }else{
98447 nToken = 0;
98448 }
98449 if( dupFlags ){
98450 assert( ExprHasProperty(p, EP_Reduced)==0 )((void) (0));
98451 memcpy(zAlloc, p, nNewSize);
98452 }else{
98453 u32 nSize = (u32)exprStructSize(p);
98454 memcpy(zAlloc, p, nSize);
98455 if( nSize<EXPR_FULLSIZEsizeof(Expr) ){
98456 memset(&zAlloc[nSize], 0, EXPR_FULLSIZEsizeof(Expr)-nSize);
98457 }
98458 }
98459
98460 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
98461 pNew->flags &= ~(EP_Reduced0x002000|EP_TokenOnly0x004000|EP_Static0x8000000|EP_MemToken0x010000);
98462 pNew->flags |= nStructSize & (EP_Reduced0x002000|EP_TokenOnly0x004000);
98463 pNew->flags |= staticFlag;
98464
98465 /* Copy the p->u.zToken string, if any. */
98466 if( nToken ){
98467 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
98468 memcpy(zToken, p->u.zToken, nToken);
98469 }
98470
98471 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly0x004000|EP_Leaf0x800000)) ){
98472 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
98473 if( ExprHasProperty(p, EP_xIsSelect)(((p)->flags&(0x000800))!=0) ){
98474 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
98475 }else{
98476 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
98477 }
98478 }
98479
98480 /* Fill in pNew->pLeft and pNew->pRight. */
98481 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc)(((pNew)->flags&(0x002000|0x004000|0x1000000))!=0) ){
98482 zAlloc += dupedExprNodeSize(p, dupFlags);
98483 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf)(((pNew)->flags&(0x004000|0x800000))!=0) ){
98484 pNew->pLeft = p->pLeft ?
98485 exprDup(db, p->pLeft, EXPRDUP_REDUCE0x0001, &zAlloc) : 0;
98486 pNew->pRight = p->pRight ?
98487 exprDup(db, p->pRight, EXPRDUP_REDUCE0x0001, &zAlloc) : 0;
98488 }
98489#ifndef SQLITE_OMIT_WINDOWFUNC
98490 if( ExprHasProperty(p, EP_WinFunc)(((p)->flags&(0x1000000))!=0) ){
98491 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
98492 assert( ExprHasProperty(pNew, EP_WinFunc) )((void) (0));
98493 }
98494#endif /* SQLITE_OMIT_WINDOWFUNC */
98495 if( pzBuffer ){
98496 *pzBuffer = zAlloc;
98497 }
98498 }else{
98499 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf)(((p)->flags&(0x004000|0x800000))!=0) ){
98500 if( pNew->op==TK_SELECT_COLUMN170 ){
98501 pNew->pLeft = p->pLeft;
98502 assert( p->iColumn==0 || p->pRight==0 )((void) (0));
98503 assert( p->pRight==0 || p->pRight==p->pLeft )((void) (0));
98504 }else{
98505 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
98506 }
98507 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
98508 }
98509 }
98510 }
98511 return pNew;
98512}
98513
98514/*
98515** Create and return a deep copy of the object passed as the second
98516** argument. If an OOM condition is encountered, NULL is returned
98517** and the db->mallocFailed flag set.
98518*/
98519#ifndef SQLITE_OMIT_CTE
98520static With *withDup(sqlite3 *db, With *p){
98521 With *pRet = 0;
98522 if( p ){
98523 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
98524 pRet = sqlite3DbMallocZero(db, nByte);
98525 if( pRet ){
98526 int i;
98527 pRet->nCte = p->nCte;
98528 for(i=0; i<p->nCte; i++){
98529 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
98530 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
98531 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
98532 }
98533 }
98534 }
98535 return pRet;
98536}
98537#else
98538# define withDup(x,y) 0
98539#endif
98540
98541#ifndef SQLITE_OMIT_WINDOWFUNC
98542/*
98543** The gatherSelectWindows() procedure and its helper routine
98544** gatherSelectWindowsCallback() are used to scan all the expressions
98545** an a newly duplicated SELECT statement and gather all of the Window
98546** objects found there, assembling them onto the linked list at Select->pWin.
98547*/
98548static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
98549 if( pExpr->op==TK_FUNCTION161 && pExpr->y.pWin!=0 ){
98550 assert( ExprHasProperty(pExpr, EP_WinFunc) )((void) (0));
98551 pExpr->y.pWin->pNextWin = pWalker->u.pSelect->pWin;
98552 pWalker->u.pSelect->pWin = pExpr->y.pWin;
98553 }
98554 return WRC_Continue0;
98555}
98556static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
98557 return p==pWalker->u.pSelect ? WRC_Continue0 : WRC_Prune1;
98558}
98559static void gatherSelectWindows(Select *p){
98560 Walker w;
98561 w.xExprCallback = gatherSelectWindowsCallback;
98562 w.xSelectCallback = gatherSelectWindowsSelectCallback;
98563 w.xSelectCallback2 = 0;
98564 w.pParse = 0;
98565 w.u.pSelect = p;
98566 sqlite3WalkSelect(&w, p);
98567}
98568#endif
98569
98570
98571/*
98572** The following group of routines make deep copies of expressions,
98573** expression lists, ID lists, and select statements. The copies can
98574** be deleted (by being passed to their respective ...Delete() routines)
98575** without effecting the originals.
98576**
98577** The expression list, ID, and source lists return by sqlite3ExprListDup(),
98578** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
98579** by subsequent calls to sqlite*ListAppend() routines.
98580**
98581** Any tables that the SrcList might point to are not duplicated.
98582**
98583** The flags parameter contains a combination of the EXPRDUP_XXX flags.
98584** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
98585** truncated version of the usual Expr structure that will be stored as
98586** part of the in-memory representation of the database schema.
98587*/
98588SQLITE_PRIVATEstatic Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
98589 assert( flags==0 || flags==EXPRDUP_REDUCE )((void) (0));
98590 return p ? exprDup(db, p, flags, 0) : 0;
98591}
98592SQLITE_PRIVATEstatic ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
98593 ExprList *pNew;
98594 struct ExprList_item *pItem, *pOldItem;
98595 int i;
98596 Expr *pPriorSelectCol = 0;
98597 assert( db!=0 )((void) (0));
98598 if( p==0 ) return 0;
98599 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
98600 if( pNew==0 ) return 0;
98601 pNew->nExpr = p->nExpr;
98602 pItem = pNew->a;
98603 pOldItem = p->a;
98604 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
98605 Expr *pOldExpr = pOldItem->pExpr;
98606 Expr *pNewExpr;
98607 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
98608 if( pOldExpr
98609 && pOldExpr->op==TK_SELECT_COLUMN170
98610 && (pNewExpr = pItem->pExpr)!=0
98611 ){
98612 assert( pNewExpr->iColumn==0 || i>0 )((void) (0));
98613 if( pNewExpr->iColumn==0 ){
98614 assert( pOldExpr->pLeft==pOldExpr->pRight )((void) (0));
98615 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
98616 }else{
98617 assert( i>0 )((void) (0));
98618 assert( pItem[-1].pExpr!=0 )((void) (0));
98619 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 )((void) (0));
98620 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft )((void) (0));
98621 pNewExpr->pLeft = pPriorSelectCol;
98622 }
98623 }
98624 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
98625 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
98626 pItem->sortOrder = pOldItem->sortOrder;
98627 pItem->done = 0;
98628 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
98629 pItem->bSorterRef = pOldItem->bSorterRef;
98630 pItem->u = pOldItem->u;
98631 }
98632 return pNew;
98633}
98634
98635/*
98636** If cursors, triggers, views and subqueries are all omitted from
98637** the build, then none of the following routines, except for
98638** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
98639** called with a NULL argument.
98640*/
98641#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
98642 || !defined(SQLITE_OMIT_SUBQUERY)
98643SQLITE_PRIVATEstatic SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
98644 SrcList *pNew;
98645 int i;
98646 int nByte;
98647 assert( db!=0 )((void) (0));
98648 if( p==0 ) return 0;
98649 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
98650 pNew = sqlite3DbMallocRawNN(db, nByte );
98651 if( pNew==0 ) return 0;
98652 pNew->nSrc = pNew->nAlloc = p->nSrc;
98653 for(i=0; i<p->nSrc; i++){
98654 struct SrcList_item *pNewItem = &pNew->a[i];
98655 struct SrcList_item *pOldItem = &p->a[i];
98656 Table *pTab;
98657 pNewItem->pSchema = pOldItem->pSchema;
98658 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
98659 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
98660 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
98661 pNewItem->fg = pOldItem->fg;
98662 pNewItem->iCursor = pOldItem->iCursor;
98663 pNewItem->addrFillSub = pOldItem->addrFillSub;
98664 pNewItem->regReturn = pOldItem->regReturn;
98665 if( pNewItem->fg.isIndexedBy ){
98666 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
98667 }
98668 pNewItem->pIBIndex = pOldItem->pIBIndex;
98669 if( pNewItem->fg.isTabFunc ){
98670 pNewItem->u1.pFuncArg =
98671 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
98672 }
98673 pTab = pNewItem->pTab = pOldItem->pTab;
98674 if( pTab ){
98675 pTab->nTabRef++;
98676 }
98677 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
98678 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
98679 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
98680 pNewItem->colUsed = pOldItem->colUsed;
98681 }
98682 return pNew;
98683}
98684SQLITE_PRIVATEstatic IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
98685 IdList *pNew;
98686 int i;
98687 assert( db!=0 )((void) (0));
98688 if( p==0 ) return 0;
98689 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
98690 if( pNew==0 ) return 0;
98691 pNew->nId = p->nId;
98692 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
98693 if( pNew->a==0 ){
98694 sqlite3DbFreeNN(db, pNew);
98695 return 0;
98696 }
98697 /* Note that because the size of the allocation for p->a[] is not
98698 ** necessarily a power of two, sqlite3IdListAppend() may not be called
98699 ** on the duplicate created by this function. */
98700 for(i=0; i<p->nId; i++){
98701 struct IdList_item *pNewItem = &pNew->a[i];
98702 struct IdList_item *pOldItem = &p->a[i];
98703 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
98704 pNewItem->idx = pOldItem->idx;
98705 }
98706 return pNew;
98707}
98708SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
98709 Select *pRet = 0;
98710 Select *pNext = 0;
98711 Select **pp = &pRet;
98712 Select *p;
98713
98714 assert( db!=0 )((void) (0));
98715 for(p=pDup; p; p=p->pPrior){
98716 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
98717 if( pNew==0 ) break;
98718 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
98719 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
98720 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
98721 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
98722 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
98723 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
98724 pNew->op = p->op;
98725 pNew->pNext = pNext;
98726 pNew->pPrior = 0;
98727 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
98728 pNew->iLimit = 0;
98729 pNew->iOffset = 0;
98730 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral0x00020;
98731 pNew->addrOpenEphm[0] = -1;
98732 pNew->addrOpenEphm[1] = -1;
98733 pNew->nSelectRow = p->nSelectRow;
98734 pNew->pWith = withDup(db, p->pWith);
98735#ifndef SQLITE_OMIT_WINDOWFUNC
98736 pNew->pWin = 0;
98737 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
98738 if( p->pWin ) gatherSelectWindows(pNew);
98739#endif
98740 pNew->selId = p->selId;
98741 *pp = pNew;
98742 pp = &pNew->pPrior;
98743 pNext = pNew;
98744 }
98745
98746 return pRet;
98747}
98748#else
98749SQLITE_PRIVATEstatic Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
98750 assert( p==0 )((void) (0));
98751 return 0;
98752}
98753#endif
98754
98755
98756/*
98757** Add a new element to the end of an expression list. If pList is
98758** initially NULL, then create a new expression list.
98759**
98760** The pList argument must be either NULL or a pointer to an ExprList
98761** obtained from a prior call to sqlite3ExprListAppend(). This routine
98762** may not be used with an ExprList obtained from sqlite3ExprListDup().
98763** Reason: This routine assumes that the number of slots in pList->a[]
98764** is a power of two. That is true for sqlite3ExprListAppend() returns
98765** but is not necessarily true from the return value of sqlite3ExprListDup().
98766**
98767** If a memory allocation error occurs, the entire list is freed and
98768** NULL is returned. If non-NULL is returned, then it is guaranteed
98769** that the new entry was successfully appended.
98770*/
98771SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppend(
98772 Parse *pParse, /* Parsing context */
98773 ExprList *pList, /* List to which to append. Might be NULL */
98774 Expr *pExpr /* Expression to be appended. Might be NULL */
98775){
98776 struct ExprList_item *pItem;
98777 sqlite3 *db = pParse->db;
98778 assert( db!=0 )((void) (0));
98779 if( pList==0 ){
98780 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
98781 if( pList==0 ){
98782 goto no_mem;
98783 }
98784 pList->nExpr = 0;
98785 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
98786 ExprList *pNew;
98787 pNew = sqlite3DbRealloc(db, pList,
98788 sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0]));
98789 if( pNew==0 ){
98790 goto no_mem;
98791 }
98792 pList = pNew;
98793 }
98794 pItem = &pList->a[pList->nExpr++];
98795 assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) )((void) (0));
98796 assert( offsetof(struct ExprList_item,pExpr)==0 )((void) (0));
98797 memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName)__builtin_offsetof(struct ExprList_item, zName));
98798 pItem->pExpr = pExpr;
98799 return pList;
98800
98801no_mem:
98802 /* Avoid leaking memory if malloc has failed. */
98803 sqlite3ExprDelete(db, pExpr);
98804 sqlite3ExprListDelete(db, pList);
98805 return 0;
98806}
98807
98808/*
98809** pColumns and pExpr form a vector assignment which is part of the SET
98810** clause of an UPDATE statement. Like this:
98811**
98812** (a,b,c) = (expr1,expr2,expr3)
98813** Or: (a,b,c) = (SELECT x,y,z FROM ....)
98814**
98815** For each term of the vector assignment, append new entries to the
98816** expression list pList. In the case of a subquery on the RHS, append
98817** TK_SELECT_COLUMN expressions.
98818*/
98819SQLITE_PRIVATEstatic ExprList *sqlite3ExprListAppendVector(
98820 Parse *pParse, /* Parsing context */
98821 ExprList *pList, /* List to which to append. Might be NULL */
98822 IdList *pColumns, /* List of names of LHS of the assignment */
98823 Expr *pExpr /* Vector expression to be appended. Might be NULL */
98824){
98825 sqlite3 *db = pParse->db;
98826 int n;
98827 int i;
98828 int iFirst = pList ? pList->nExpr : 0;
98829 /* pColumns can only be NULL due to an OOM but an OOM will cause an
98830 ** exit prior to this routine being invoked */
98831 if( NEVER(pColumns==0)(pColumns==0) ) goto vector_append_error;
98832 if( pExpr==0 ) goto vector_append_error;
98833
98834 /* If the RHS is a vector, then we can immediately check to see that
98835 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
98836 ** wildcards ("*") in the result set of the SELECT must be expanded before
98837 ** we can do the size check, so defer the size check until code generation.
98838 */
98839 if( pExpr->op!=TK_SELECT131 && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
98840 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
98841 pColumns->nId, n);
98842 goto vector_append_error;
98843 }
98844
98845 for(i=0; i<pColumns->nId; i++){
98846 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
98847 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
98848 if( pList ){
98849 assert( pList->nExpr==iFirst+i+1 )((void) (0));
98850 pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
98851 pColumns->a[i].zName = 0;
98852 }
98853 }
98854
98855 if( !db->mallocFailed && pExpr->op==TK_SELECT131 && ALWAYS(pList!=0)(pList!=0) ){
98856 Expr *pFirst = pList->a[iFirst].pExpr;
98857 assert( pFirst!=0 )((void) (0));
98858 assert( pFirst->op==TK_SELECT_COLUMN )((void) (0));
98859
98860 /* Store the SELECT statement in pRight so it will be deleted when
98861 ** sqlite3ExprListDelete() is called */
98862 pFirst->pRight = pExpr;
98863 pExpr = 0;
98864
98865 /* Remember the size of the LHS in iTable so that we can check that
98866 ** the RHS and LHS sizes match during code generation. */
98867 pFirst->iTable = pColumns->nId;
98868 }
98869
98870vector_append_error:
98871 sqlite3ExprUnmapAndDelete(pParse, pExpr);
98872 sqlite3IdListDelete(db, pColumns);
98873 return pList;
98874}
98875
98876/*
98877** Set the sort order for the last element on the given ExprList.
98878*/
98879SQLITE_PRIVATEstatic void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
98880 if( p==0 ) return;
98881 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 )((void) (0));
98882 assert( p->nExpr>0 )((void) (0));
98883 if( iSortOrder<0 ){
98884 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC )((void) (0));
98885 return;
98886 }
98887 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
98888}
98889
98890/*
98891** Set the ExprList.a[].zName element of the most recently added item
98892** on the expression list.
98893**
98894** pList might be NULL following an OOM error. But pName should never be
98895** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
98896** is set.
98897*/
98898SQLITE_PRIVATEstatic void sqlite3ExprListSetName(
98899 Parse *pParse, /* Parsing context */
98900 ExprList *pList, /* List to which to add the span. */
98901 Token *pName, /* Name to be added */
98902 int dequote /* True to cause the name to be dequoted */
98903){
98904 assert( pList!=0 || pParse->db->mallocFailed!=0 )((void) (0));
98905 if( pList ){
98906 struct ExprList_item *pItem;
98907 assert( pList->nExpr>0 )((void) (0));
98908 pItem = &pList->a[pList->nExpr-1];
98909 assert( pItem->zName==0 )((void) (0));
98910 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
98911 if( dequote ) sqlite3Dequote(pItem->zName);
98912 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
98913 sqlite3RenameTokenMap(pParse, (void*)pItem->zName, pName);
98914 }
98915 }
98916}
98917
98918/*
98919** Set the ExprList.a[].zSpan element of the most recently added item
98920** on the expression list.
98921**
98922** pList might be NULL following an OOM error. But pSpan should never be
98923** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
98924** is set.
98925*/
98926SQLITE_PRIVATEstatic void sqlite3ExprListSetSpan(
98927 Parse *pParse, /* Parsing context */
98928 ExprList *pList, /* List to which to add the span. */
98929 const char *zStart, /* Start of the span */
98930 const char *zEnd /* End of the span */
98931){
98932 sqlite3 *db = pParse->db;
98933 assert( pList!=0 || db->mallocFailed!=0 )((void) (0));
98934 if( pList ){
98935 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
98936 assert( pList->nExpr>0 )((void) (0));
98937 sqlite3DbFree(db, pItem->zSpan);
98938 pItem->zSpan = sqlite3DbSpanDup(db, zStart, zEnd);
98939 }
98940}
98941
98942/*
98943** If the expression list pEList contains more than iLimit elements,
98944** leave an error message in pParse.
98945*/
98946SQLITE_PRIVATEstatic void sqlite3ExprListCheckLength(
98947 Parse *pParse,
98948 ExprList *pEList,
98949 const char *zObject
98950){
98951 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN2];
98952 testcase( pEList && pEList->nExpr==mx );
98953 testcase( pEList && pEList->nExpr==mx+1 );
98954 if( pEList && pEList->nExpr>mx ){
98955 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
98956 }
98957}
98958
98959/*
98960** Delete an entire expression list.
98961*/
98962static SQLITE_NOINLINE__attribute__((noinline)) void exprListDeleteNN(sqlite3 *db, ExprList *pList){
98963 int i = pList->nExpr;
98964 struct ExprList_item *pItem = pList->a;
98965 assert( pList->nExpr>0 )((void) (0));
98966 do{
98967 sqlite3ExprDelete(db, pItem->pExpr);
98968 sqlite3DbFree(db, pItem->zName);
98969 sqlite3DbFree(db, pItem->zSpan);
98970 pItem++;
98971 }while( --i>0 );
98972 sqlite3DbFreeNN(db, pList);
98973}
98974SQLITE_PRIVATEstatic void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
98975 if( pList ) exprListDeleteNN(db, pList);
98976}
98977
98978/*
98979** Return the bitwise-OR of all Expr.flags fields in the given
98980** ExprList.
98981*/
98982SQLITE_PRIVATEstatic u32 sqlite3ExprListFlags(const ExprList *pList){
98983 int i;
98984 u32 m = 0;
98985 assert( pList!=0 )((void) (0));
98986 for(i=0; i<pList->nExpr; i++){
98987 Expr *pExpr = pList->a[i].pExpr;
98988 assert( pExpr!=0 )((void) (0));
98989 m |= pExpr->flags;
98990 }
98991 return m;
98992}
98993
98994/*
98995** This is a SELECT-node callback for the expression walker that
98996** always "fails". By "fail" in this case, we mean set
98997** pWalker->eCode to zero and abort.
98998**
98999** This callback is used by multiple expression walkers.
99000*/
99001SQLITE_PRIVATEstatic int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
99002 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
99003 pWalker->eCode = 0;
99004 return WRC_Abort2;
99005}
99006
99007/*
99008** If the input expression is an ID with the name "true" or "false"
99009** then convert it into an TK_TRUEFALSE term. Return non-zero if
99010** the conversion happened, and zero if the expression is unaltered.
99011*/
99012SQLITE_PRIVATEstatic int sqlite3ExprIdToTrueFalse(Expr *pExpr){
99013 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING )((void) (0));
99014 if( !ExprHasProperty(pExpr, EP_Quoted)(((pExpr)->flags&(0x4000000))!=0)
99015 && (sqlite3StrICmp(pExpr->u.zToken, "true")==0
99016 || sqlite3StrICmp(pExpr->u.zToken, "false")==0)
99017 ){
99018 pExpr->op = TK_TRUEFALSE159;
99019 ExprSetProperty(pExpr, pExpr->u.zToken[4]==0 ? EP_IsTrue : EP_IsFalse)(pExpr)->flags|=(pExpr->u.zToken[4]==0 ? 0x10000000 : 0x20000000
)
;
99020 return 1;
99021 }
99022 return 0;
99023}
99024
99025/*
99026** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
99027** and 0 if it is FALSE.
99028*/
99029SQLITE_PRIVATEstatic int sqlite3ExprTruthValue(const Expr *pExpr){
99030 pExpr = sqlite3ExprSkipCollate((Expr*)pExpr);
99031 assert( pExpr->op==TK_TRUEFALSE )((void) (0));
99032 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0((void) (0))
99033 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 )((void) (0));
99034 return pExpr->u.zToken[4]==0;
99035}
99036
99037/*
99038** If pExpr is an AND or OR expression, try to simplify it by eliminating
99039** terms that are always true or false. Return the simplified expression.
99040** Or return the original expression if no simplification is possible.
99041**
99042** Examples:
99043**
99044** (x<10) AND true => (x<10)
99045** (x<10) AND false => false
99046** (x<10) AND (y=22 OR false) => (x<10) AND (y=22)
99047** (x<10) AND (y=22 OR true) => (x<10)
99048** (y=22) OR true => true
99049*/
99050SQLITE_PRIVATEstatic Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
99051 assert( pExpr!=0 )((void) (0));
99052 if( pExpr->op==TK_AND44 || pExpr->op==TK_OR43 ){
99053 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
99054 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
99055 if( ExprAlwaysTrue(pLeft)(((pLeft)->flags&(0x000001|0x10000000))==0x10000000) || ExprAlwaysFalse(pRight)(((pRight)->flags&(0x000001|0x20000000))==0x20000000) ){
99056 pExpr = pExpr->op==TK_AND44 ? pRight : pLeft;
99057 }else if( ExprAlwaysTrue(pRight)(((pRight)->flags&(0x000001|0x10000000))==0x10000000) || ExprAlwaysFalse(pLeft)(((pLeft)->flags&(0x000001|0x20000000))==0x20000000) ){
99058 pExpr = pExpr->op==TK_AND44 ? pLeft : pRight;
99059 }
99060 }
99061 return pExpr;
99062}
99063
99064
99065/*
99066** These routines are Walker callbacks used to check expressions to
99067** see if they are "constant" for some definition of constant. The
99068** Walker.eCode value determines the type of "constant" we are looking
99069** for.
99070**
99071** These callback routines are used to implement the following:
99072**
99073** sqlite3ExprIsConstant() pWalker->eCode==1
99074** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
99075** sqlite3ExprIsTableConstant() pWalker->eCode==3
99076** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
99077**
99078** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
99079** is found to not be a constant.
99080**
99081** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
99082** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
99083** an existing schema and 4 when processing a new statement. A bound
99084** parameter raises an error for new statements, but is silently converted
99085** to NULL for existing schemas. This allows sqlite_master tables that
99086** contain a bound parameter because they were generated by older versions
99087** of SQLite to be parsed by newer versions of SQLite without raising a
99088** malformed schema error.
99089*/
99090static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
99091
99092 /* If pWalker->eCode is 2 then any term of the expression that comes from
99093 ** the ON or USING clauses of a left join disqualifies the expression
99094 ** from being considered constant. */
99095 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0) ){
99096 pWalker->eCode = 0;
99097 return WRC_Abort2;
99098 }
99099
99100 switch( pExpr->op ){
99101 /* Consider functions to be constant if all their arguments are constant
99102 ** and either pWalker->eCode==4 or 5 or the function has the
99103 ** SQLITE_FUNC_CONST flag. */
99104 case TK_FUNCTION161:
99105 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc)(((pExpr)->flags&(0x080000))!=0) ){
99106 return WRC_Continue0;
99107 }else{
99108 pWalker->eCode = 0;
99109 return WRC_Abort2;
99110 }
99111 case TK_ID59:
99112 /* Convert "true" or "false" in a DEFAULT clause into the
99113 ** appropriate TK_TRUEFALSE operator */
99114 if( sqlite3ExprIdToTrueFalse(pExpr) ){
99115 return WRC_Prune1;
99116 }
99117 /* Fall thru */
99118 case TK_COLUMN162:
99119 case TK_AGG_FUNCTION163:
99120 case TK_AGG_COLUMN164:
99121 testcase( pExpr->op==TK_ID );
99122 testcase( pExpr->op==TK_COLUMN );
99123 testcase( pExpr->op==TK_AGG_FUNCTION );
99124 testcase( pExpr->op==TK_AGG_COLUMN );
99125 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000008))!=0) && pWalker->eCode!=2 ){
99126 return WRC_Continue0;
99127 }
99128 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
99129 return WRC_Continue0;
99130 }
99131 /* Fall through */
99132 case TK_IF_NULL_ROW171:
99133 case TK_REGISTER168:
99134 testcase( pExpr->op==TK_REGISTER );
99135 testcase( pExpr->op==TK_IF_NULL_ROW );
99136 pWalker->eCode = 0;
99137 return WRC_Abort2;
99138 case TK_VARIABLE148:
99139 if( pWalker->eCode==5 ){
99140 /* Silently convert bound parameters that appear inside of CREATE
99141 ** statements into a NULL when parsing the CREATE statement text out
99142 ** of the sqlite_master table */
99143 pExpr->op = TK_NULL114;
99144 }else if( pWalker->eCode==4 ){
99145 /* A bound parameter in a CREATE statement that originates from
99146 ** sqlite3_prepare() causes an error */
99147 pWalker->eCode = 0;
99148 return WRC_Abort2;
99149 }
99150 /* Fall through */
99151 default:
99152 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
99153 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
99154 return WRC_Continue0;
99155 }
99156}
99157static int exprIsConst(Expr *p, int initFlag, int iCur){
99158 Walker w;
99159 w.eCode = initFlag;
99160 w.xExprCallback = exprNodeIsConstant;
99161 w.xSelectCallback = sqlite3SelectWalkFail;
99162#ifdef SQLITE_DEBUG
99163 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
99164#endif
99165 w.u.iCur = iCur;
99166 sqlite3WalkExpr(&w, p);
99167 return w.eCode;
99168}
99169
99170/*
99171** Walk an expression tree. Return non-zero if the expression is constant
99172** and 0 if it involves variables or function calls.
99173**
99174** For the purposes of this function, a double-quoted string (ex: "abc")
99175** is considered a variable but a single-quoted string (ex: 'abc') is
99176** a constant.
99177*/
99178SQLITE_PRIVATEstatic int sqlite3ExprIsConstant(Expr *p){
99179 return exprIsConst(p, 1, 0);
99180}
99181
99182/*
99183** Walk an expression tree. Return non-zero if
99184**
99185** (1) the expression is constant, and
99186** (2) the expression does originate in the ON or USING clause
99187** of a LEFT JOIN, and
99188** (3) the expression does not contain any EP_FixedCol TK_COLUMN
99189** operands created by the constant propagation optimization.
99190**
99191** When this routine returns true, it indicates that the expression
99192** can be added to the pParse->pConstExpr list and evaluated once when
99193** the prepared statement starts up. See sqlite3ExprCodeAtInit().
99194*/
99195SQLITE_PRIVATEstatic int sqlite3ExprIsConstantNotJoin(Expr *p){
99196 return exprIsConst(p, 2, 0);
99197}
99198
99199/*
99200** Walk an expression tree. Return non-zero if the expression is constant
99201** for any single row of the table with cursor iCur. In other words, the
99202** expression must not refer to any non-deterministic function nor any
99203** table other than iCur.
99204*/
99205SQLITE_PRIVATEstatic int sqlite3ExprIsTableConstant(Expr *p, int iCur){
99206 return exprIsConst(p, 3, iCur);
99207}
99208
99209
99210/*
99211** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
99212*/
99213static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
99214 ExprList *pGroupBy = pWalker->u.pGroupBy;
99215 int i;
99216
99217 /* Check if pExpr is identical to any GROUP BY term. If so, consider
99218 ** it constant. */
99219 for(i=0; i<pGroupBy->nExpr; i++){
99220 Expr *p = pGroupBy->a[i].pExpr;
99221 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
99222 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
99223 if( sqlite3IsBinary(pColl) ){
99224 return WRC_Prune1;
99225 }
99226 }
99227 }
99228
99229 /* Check if pExpr is a sub-select. If so, consider it variable. */
99230 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
99231 pWalker->eCode = 0;
99232 return WRC_Abort2;
99233 }
99234
99235 return exprNodeIsConstant(pWalker, pExpr);
99236}
99237
99238/*
99239** Walk the expression tree passed as the first argument. Return non-zero
99240** if the expression consists entirely of constants or copies of terms
99241** in pGroupBy that sort with the BINARY collation sequence.
99242**
99243** This routine is used to determine if a term of the HAVING clause can
99244** be promoted into the WHERE clause. In order for such a promotion to work,
99245** the value of the HAVING clause term must be the same for all members of
99246** a "group". The requirement that the GROUP BY term must be BINARY
99247** assumes that no other collating sequence will have a finer-grained
99248** grouping than binary. In other words (A=B COLLATE binary) implies
99249** A=B in every other collating sequence. The requirement that the
99250** GROUP BY be BINARY is stricter than necessary. It would also work
99251** to promote HAVING clauses that use the same alternative collating
99252** sequence as the GROUP BY term, but that is much harder to check,
99253** alternative collating sequences are uncommon, and this is only an
99254** optimization, so we take the easy way out and simply require the
99255** GROUP BY to use the BINARY collating sequence.
99256*/
99257SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
99258 Walker w;
99259 w.eCode = 1;
99260 w.xExprCallback = exprNodeIsConstantOrGroupBy;
99261 w.xSelectCallback = 0;
99262 w.u.pGroupBy = pGroupBy;
99263 w.pParse = pParse;
99264 sqlite3WalkExpr(&w, p);
99265 return w.eCode;
99266}
99267
99268/*
99269** Walk an expression tree. Return non-zero if the expression is constant
99270** or a function call with constant arguments. Return and 0 if there
99271** are any variables.
99272**
99273** For the purposes of this function, a double-quoted string (ex: "abc")
99274** is considered a variable but a single-quoted string (ex: 'abc') is
99275** a constant.
99276*/
99277SQLITE_PRIVATEstatic int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
99278 assert( isInit==0 || isInit==1 )((void) (0));
99279 return exprIsConst(p, 4+isInit, 0);
99280}
99281
99282#ifdef SQLITE_ENABLE_CURSOR_HINTS
99283/*
99284** Walk an expression tree. Return 1 if the expression contains a
99285** subquery of some kind. Return 0 if there are no subqueries.
99286*/
99287SQLITE_PRIVATEstatic int sqlite3ExprContainsSubquery(Expr *p){
99288 Walker w;
99289 w.eCode = 1;
99290 w.xExprCallback = sqlite3ExprWalkNoop;
99291 w.xSelectCallback = sqlite3SelectWalkFail;
99292#ifdef SQLITE_DEBUG
99293 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
99294#endif
99295 sqlite3WalkExpr(&w, p);
99296 return w.eCode==0;
99297}
99298#endif
99299
99300/*
99301** If the expression p codes a constant integer that is small enough
99302** to fit in a 32-bit integer, return 1 and put the value of the integer
99303** in *pValue. If the expression is not an integer or if it is too big
99304** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
99305*/
99306SQLITE_PRIVATEstatic int sqlite3ExprIsInteger(Expr *p, int *pValue){
99307 int rc = 0;
99308 if( NEVER(p==0)(p==0) ) return 0; /* Used to only happen following on OOM */
99309
99310 /* If an expression is an integer literal that fits in a signed 32-bit
99311 ** integer, then the EP_IntValue flag will have already been set */
99312 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0((void) (0))
99313 || sqlite3GetInt32(p->u.zToken, &rc)==0 )((void) (0));
99314
99315 if( p->flags & EP_IntValue0x000400 ){
99316 *pValue = p->u.iValue;
99317 return 1;
99318 }
99319 switch( p->op ){
99320 case TK_UPLUS166: {
99321 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
99322 break;
99323 }
99324 case TK_UMINUS165: {
99325 int v;
99326 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
99327 assert( v!=(-2147483647-1) )((void) (0));
99328 *pValue = -v;
99329 rc = 1;
99330 }
99331 break;
99332 }
99333 default: break;
99334 }
99335 return rc;
99336}
99337
99338/*
99339** Return FALSE if there is no chance that the expression can be NULL.
99340**
99341** If the expression might be NULL or if the expression is too complex
99342** to tell return TRUE.
99343**
99344** This routine is used as an optimization, to skip OP_IsNull opcodes
99345** when we know that a value cannot be NULL. Hence, a false positive
99346** (returning TRUE when in fact the expression can never be NULL) might
99347** be a small performance hit but is otherwise harmless. On the other
99348** hand, a false negative (returning FALSE when the result could be NULL)
99349** will likely result in an incorrect answer. So when in doubt, return
99350** TRUE.
99351*/
99352SQLITE_PRIVATEstatic int sqlite3ExprCanBeNull(const Expr *p){
99353 u8 op;
99354 while( p->op==TK_UPLUS166 || p->op==TK_UMINUS165 ){
99355 p = p->pLeft;
99356 }
99357 op = p->op;
99358 if( op==TK_REGISTER168 ) op = p->op2;
99359 switch( op ){
99360 case TK_INTEGER147:
99361 case TK_STRING110:
99362 case TK_FLOAT145:
99363 case TK_BLOB146:
99364 return 0;
99365 case TK_COLUMN162:
99366 return ExprHasProperty(p, EP_CanBeNull)(((p)->flags&(0x100000))!=0) ||
99367 p->y.pTab==0 || /* Reference to column of index on expression */
99368 (p->iColumn>=0 && p->y.pTab->aCol[p->iColumn].notNull==0);
99369 default:
99370 return 1;
99371 }
99372}
99373
99374/*
99375** Return TRUE if the given expression is a constant which would be
99376** unchanged by OP_Affinity with the affinity given in the second
99377** argument.
99378**
99379** This routine is used to determine if the OP_Affinity operation
99380** can be omitted. When in doubt return FALSE. A false negative
99381** is harmless. A false positive, however, can result in the wrong
99382** answer.
99383*/
99384SQLITE_PRIVATEstatic int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
99385 u8 op;
99386 if( aff==SQLITE_AFF_BLOB'A' ) return 1;
99387 while( p->op==TK_UPLUS166 || p->op==TK_UMINUS165 ){ p = p->pLeft; }
99388 op = p->op;
99389 if( op==TK_REGISTER168 ) op = p->op2;
99390 switch( op ){
99391 case TK_INTEGER147: {
99392 return aff==SQLITE_AFF_INTEGER'D' || aff==SQLITE_AFF_NUMERIC'C';
99393 }
99394 case TK_FLOAT145: {
99395 return aff==SQLITE_AFF_REAL'E' || aff==SQLITE_AFF_NUMERIC'C';
99396 }
99397 case TK_STRING110: {
99398 return aff==SQLITE_AFF_TEXT'B';
99399 }
99400 case TK_BLOB146: {
99401 return 1;
99402 }
99403 case TK_COLUMN162: {
99404 assert( p->iTable>=0 )((void) (0)); /* p cannot be part of a CHECK constraint */
99405 return p->iColumn<0
99406 && (aff==SQLITE_AFF_INTEGER'D' || aff==SQLITE_AFF_NUMERIC'C');
99407 }
99408 default: {
99409 return 0;
99410 }
99411 }
99412}
99413
99414/*
99415** Return TRUE if the given string is a row-id column name.
99416*/
99417SQLITE_PRIVATEstatic int sqlite3IsRowid(const char *z){
99418 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
99419 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
99420 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
99421 return 0;
99422}
99423
99424/*
99425** pX is the RHS of an IN operator. If pX is a SELECT statement
99426** that can be simplified to a direct table access, then return
99427** a pointer to the SELECT statement. If pX is not a SELECT statement,
99428** or if the SELECT statement needs to be manifested into a transient
99429** table, then return NULL.
99430*/
99431#ifndef SQLITE_OMIT_SUBQUERY
99432static Select *isCandidateForInOpt(Expr *pX){
99433 Select *p;
99434 SrcList *pSrc;
99435 ExprList *pEList;
99436 Table *pTab;
99437 int i;
99438 if( !ExprHasProperty(pX, EP_xIsSelect)(((pX)->flags&(0x000800))!=0) ) return 0; /* Not a subquery */
99439 if( ExprHasProperty(pX, EP_VarSelect)(((pX)->flags&(0x000020))!=0) ) return 0; /* Correlated subq */
99440 p = pX->x.pSelect;
99441 if( p->pPrior ) return 0; /* Not a compound SELECT */
99442 if( p->selFlags & (SF_Distinct0x00001|SF_Aggregate0x00008) ){
99443 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
99444 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
99445 return 0; /* No DISTINCT keyword and no aggregate functions */
99446 }
99447 assert( p->pGroupBy==0 )((void) (0)); /* Has no GROUP BY clause */
99448 if( p->pLimit ) return 0; /* Has no LIMIT clause */
99449 if( p->pWhere ) return 0; /* Has no WHERE clause */
99450 pSrc = p->pSrc;
99451 assert( pSrc!=0 )((void) (0));
99452 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
99453 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
99454 pTab = pSrc->a[0].pTab;
99455 assert( pTab!=0 )((void) (0));
99456 assert( pTab->pSelect==0 )((void) (0)); /* FROM clause is not a view */
99457 if( IsVirtual(pTab)((pTab)->nModuleArg) ) return 0; /* FROM clause not a virtual table */
99458 pEList = p->pEList;
99459 assert( pEList!=0 )((void) (0));
99460 /* All SELECT results must be columns. */
99461 for(i=0; i<pEList->nExpr; i++){
99462 Expr *pRes = pEList->a[i].pExpr;
99463 if( pRes->op!=TK_COLUMN162 ) return 0;
99464 assert( pRes->iTable==pSrc->a[0].iCursor )((void) (0)); /* Not a correlated subquery */
99465 }
99466 return p;
99467}
99468#endif /* SQLITE_OMIT_SUBQUERY */
99469
99470#ifndef SQLITE_OMIT_SUBQUERY
99471/*
99472** Generate code that checks the left-most column of index table iCur to see if
99473** it contains any NULL entries. Cause the register at regHasNull to be set
99474** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
99475** to be set to NULL if iCur contains one or more NULL values.
99476*/
99477static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
99478 int addr1;
99479 sqlite3VdbeAddOp2(v, OP_Integer70, 0, regHasNull);
99480 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind36, iCur); VdbeCoverage(v);
99481 sqlite3VdbeAddOp3(v, OP_Column90, iCur, 0, regHasNull);
99482 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG0x80);
99483 VdbeComment((v, "first_entry_in(%d)", iCur));
99484 sqlite3VdbeJumpHere(v, addr1);
99485}
99486#endif
99487
99488
99489#ifndef SQLITE_OMIT_SUBQUERY
99490/*
99491** The argument is an IN operator with a list (not a subquery) on the
99492** right-hand side. Return TRUE if that list is constant.
99493*/
99494static int sqlite3InRhsIsConstant(Expr *pIn){
99495 Expr *pLHS;
99496 int res;
99497 assert( !ExprHasProperty(pIn, EP_xIsSelect) )((void) (0));
99498 pLHS = pIn->pLeft;
99499 pIn->pLeft = 0;
99500 res = sqlite3ExprIsConstant(pIn);
99501 pIn->pLeft = pLHS;
99502 return res;
99503}
99504#endif
99505
99506/*
99507** This function is used by the implementation of the IN (...) operator.
99508** The pX parameter is the expression on the RHS of the IN operator, which
99509** might be either a list of expressions or a subquery.
99510**
99511** The job of this routine is to find or create a b-tree object that can
99512** be used either to test for membership in the RHS set or to iterate through
99513** all members of the RHS set, skipping duplicates.
99514**
99515** A cursor is opened on the b-tree object that is the RHS of the IN operator
99516** and pX->iTable is set to the index of that cursor.
99517**
99518** The returned value of this function indicates the b-tree type, as follows:
99519**
99520** IN_INDEX_ROWID - The cursor was opened on a database table.
99521** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
99522** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
99523** IN_INDEX_EPH - The cursor was opened on a specially created and
99524** populated epheremal table.
99525** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
99526** implemented as a sequence of comparisons.
99527**
99528** An existing b-tree might be used if the RHS expression pX is a simple
99529** subquery such as:
99530**
99531** SELECT <column1>, <column2>... FROM <table>
99532**
99533** If the RHS of the IN operator is a list or a more complex subquery, then
99534** an ephemeral table might need to be generated from the RHS and then
99535** pX->iTable made to point to the ephemeral table instead of an
99536** existing table.
99537**
99538** The inFlags parameter must contain, at a minimum, one of the bits
99539** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
99540** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
99541** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
99542** be used to loop over all values of the RHS of the IN operator.
99543**
99544** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
99545** through the set members) then the b-tree must not contain duplicates.
99546** An epheremal table will be created unless the selected columns are guaranteed
99547** to be unique - either because it is an INTEGER PRIMARY KEY or due to
99548** a UNIQUE constraint or index.
99549**
99550** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
99551** for fast set membership tests) then an epheremal table must
99552** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
99553** index can be found with the specified <columns> as its left-most.
99554**
99555** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
99556** if the RHS of the IN operator is a list (not a subquery) then this
99557** routine might decide that creating an ephemeral b-tree for membership
99558** testing is too expensive and return IN_INDEX_NOOP. In that case, the
99559** calling routine should implement the IN operator using a sequence
99560** of Eq or Ne comparison operations.
99561**
99562** When the b-tree is being used for membership tests, the calling function
99563** might need to know whether or not the RHS side of the IN operator
99564** contains a NULL. If prRhsHasNull is not a NULL pointer and
99565** if there is any chance that the (...) might contain a NULL value at
99566** runtime, then a register is allocated and the register number written
99567** to *prRhsHasNull. If there is no chance that the (...) contains a
99568** NULL value, then *prRhsHasNull is left unchanged.
99569**
99570** If a register is allocated and its location stored in *prRhsHasNull, then
99571** the value in that register will be NULL if the b-tree contains one or more
99572** NULL values, and it will be some non-NULL value if the b-tree contains no
99573** NULL values.
99574**
99575** If the aiMap parameter is not NULL, it must point to an array containing
99576** one element for each column returned by the SELECT statement on the RHS
99577** of the IN(...) operator. The i'th entry of the array is populated with the
99578** offset of the index column that matches the i'th column returned by the
99579** SELECT. For example, if the expression and selected index are:
99580**
99581** (?,?,?) IN (SELECT a, b, c FROM t1)
99582** CREATE INDEX i1 ON t1(b, c, a);
99583**
99584** then aiMap[] is populated with {2, 0, 1}.
99585*/
99586#ifndef SQLITE_OMIT_SUBQUERY
99587SQLITE_PRIVATEstatic int sqlite3FindInIndex(
99588 Parse *pParse, /* Parsing context */
99589 Expr *pX, /* The right-hand side (RHS) of the IN operator */
99590 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
99591 int *prRhsHasNull, /* Register holding NULL status. See notes */
99592 int *aiMap, /* Mapping from Index fields to RHS fields */
99593 int *piTab /* OUT: index to use */
99594){
99595 Select *p; /* SELECT to the right of IN operator */
99596 int eType = 0; /* Type of RHS table. IN_INDEX_* */
99597 int iTab = pParse->nTab++; /* Cursor of the RHS table */
99598 int mustBeUnique; /* True if RHS must be unique */
99599 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
99600
99601 assert( pX->op==TK_IN )((void) (0));
99602 mustBeUnique = (inFlags & IN_INDEX_LOOP0x0004)!=0;
99603
99604 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
99605 ** whether or not the SELECT result contains NULL values, check whether
99606 ** or not NULL is actually possible (it may not be, for example, due
99607 ** to NOT NULL constraints in the schema). If no NULL values are possible,
99608 ** set prRhsHasNull to 0 before continuing. */
99609 if( prRhsHasNull && (pX->flags & EP_xIsSelect0x000800) ){
99610 int i;
99611 ExprList *pEList = pX->x.pSelect->pEList;
99612 for(i=0; i<pEList->nExpr; i++){
99613 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
99614 }
99615 if( i==pEList->nExpr ){
99616 prRhsHasNull = 0;
99617 }
99618 }
99619
99620 /* Check to see if an existing table or index can be used to
99621 ** satisfy the query. This is preferable to generating a new
99622 ** ephemeral table. */
99623 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
99624 sqlite3 *db = pParse->db; /* Database connection */
99625 Table *pTab; /* Table <table>. */
99626 i16 iDb; /* Database idx for pTab */
99627 ExprList *pEList = p->pEList;
99628 int nExpr = pEList->nExpr;
99629
99630 assert( p->pEList!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
99631 assert( p->pEList->a[0].pExpr!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
99632 assert( p->pSrc!=0 )((void) (0)); /* Because of isCandidateForInOpt(p) */
99633 pTab = p->pSrc->a[0].pTab;
99634
99635 /* Code an OP_Transaction and OP_TableLock for <table>. */
99636 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
99637 sqlite3CodeVerifySchema(pParse, iDb);
99638 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
99639
99640 assert(v)((void) (0)); /* sqlite3GetVdbe() has always been previously called */
99641 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
99642 /* The "x IN (SELECT rowid FROM table)" case */
99643 int iAddr = sqlite3VdbeAddOp0(v, OP_Once17);
99644 VdbeCoverage(v);
99645
99646 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead108);
99647 eType = IN_INDEX_ROWID1;
99648 ExplainQueryPlan((pParse, 0,sqlite3VdbeExplain (pParse, 0, "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR"
,pTab->zName)
99649 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName))sqlite3VdbeExplain (pParse, 0, "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR"
,pTab->zName)
;
99650 sqlite3VdbeJumpHere(v, iAddr);
99651 }else{
99652 Index *pIdx; /* Iterator variable */
99653 int affinity_ok = 1;
99654 int i;
99655
99656 /* Check that the affinity that will be used to perform each
99657 ** comparison is the same as the affinity of each column in table
99658 ** on the RHS of the IN operator. If it not, it is not possible to
99659 ** use any index of the RHS table. */
99660 for(i=0; i<nExpr && affinity_ok; i++){
99661 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
99662 int iCol = pEList->a[i].pExpr->iColumn;
99663 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
99664 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
99665 testcase( cmpaff==SQLITE_AFF_BLOB );
99666 testcase( cmpaff==SQLITE_AFF_TEXT );
99667 switch( cmpaff ){
99668 case SQLITE_AFF_BLOB'A':
99669 break;
99670 case SQLITE_AFF_TEXT'B':
99671 /* sqlite3CompareAffinity() only returns TEXT if one side or the
99672 ** other has no affinity and the other side is TEXT. Hence,
99673 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
99674 ** and for the term on the LHS of the IN to have no affinity. */
99675 assert( idxaff==SQLITE_AFF_TEXT )((void) (0));
99676 break;
99677 default:
99678 affinity_ok = sqlite3IsNumericAffinity(idxaff)((idxaff)>='C');
99679 }
99680 }
99681
99682 if( affinity_ok ){
99683 /* Search for an existing index that will work for this IN operator */
99684 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
99685 Bitmask colUsed; /* Columns of the index used */
99686 Bitmask mCol; /* Mask for the current column */
99687 if( pIdx->nColumn<nExpr ) continue;
99688 if( pIdx->pPartIdxWhere!=0 ) continue;
99689 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
99690 ** BITMASK(nExpr) without overflowing */
99691 testcase( pIdx->nColumn==BMS-2 );
99692 testcase( pIdx->nColumn==BMS-1 );
99693 if( pIdx->nColumn>=BMS((int)(sizeof(Bitmask)*8))-1 ) continue;
99694 if( mustBeUnique ){
99695 if( pIdx->nKeyCol>nExpr
99696 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx)((pIdx)->onError!=0))
99697 ){
99698 continue; /* This index is not unique over the IN RHS columns */
99699 }
99700 }
99701
99702 colUsed = 0; /* Columns of index used so far */
99703 for(i=0; i<nExpr; i++){
99704 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
99705 Expr *pRhs = pEList->a[i].pExpr;
99706 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
99707 int j;
99708
99709 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr )((void) (0));
99710 for(j=0; j<nExpr; j++){
99711 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
99712 assert( pIdx->azColl[j] )((void) (0));
99713 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
99714 continue;
99715 }
99716 break;
99717 }
99718 if( j==nExpr ) break;
99719 mCol = MASKBIT(j)(((Bitmask)1)<<(j));
99720 if( mCol & colUsed ) break; /* Each column used only once */
99721 colUsed |= mCol;
99722 if( aiMap ) aiMap[i] = j;
99723 }
99724
99725 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) )((void) (0));
99726 if( colUsed==(MASKBIT(nExpr)(((Bitmask)1)<<(nExpr))-1) ){
99727 /* If we reach this point, that means the index pIdx is usable */
99728 int iAddr = sqlite3VdbeAddOp0(v, OP_Once17); VdbeCoverage(v);
99729 ExplainQueryPlan((pParse, 0,sqlite3VdbeExplain (pParse, 0, "USING INDEX %s FOR IN-OPERATOR"
,pIdx->zName)
99730 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName))sqlite3VdbeExplain (pParse, 0, "USING INDEX %s FOR IN-OPERATOR"
,pIdx->zName)
;
99731 sqlite3VdbeAddOp3(v, OP_OpenRead108, iTab, pIdx->tnum, iDb);
99732 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
99733 VdbeComment((v, "%s", pIdx->zName));
99734 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 )((void) (0));
99735 eType = IN_INDEX_INDEX_ASC3 + pIdx->aSortOrder[0];
99736
99737 if( prRhsHasNull ){
99738#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
99739 i64 mask = (1<<nExpr)-1;
99740 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed118,
99741 iTab, 0, 0, (u8*)&mask, P4_INT64(-14));
99742#endif
99743 *prRhsHasNull = ++pParse->nMem;
99744 if( nExpr==1 ){
99745 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
99746 }
99747 }
99748 sqlite3VdbeJumpHere(v, iAddr);
99749 }
99750 } /* End loop over indexes */
99751 } /* End if( affinity_ok ) */
99752 } /* End if not an rowid index */
99753 } /* End attempt to optimize using an index */
99754
99755 /* If no preexisting index is available for the IN clause
99756 ** and IN_INDEX_NOOP is an allowed reply
99757 ** and the RHS of the IN operator is a list, not a subquery
99758 ** and the RHS is not constant or has two or fewer terms,
99759 ** then it is not worth creating an ephemeral table to evaluate
99760 ** the IN operator so return IN_INDEX_NOOP.
99761 */
99762 if( eType==0
99763 && (inFlags & IN_INDEX_NOOP_OK0x0001)
99764 && !ExprHasProperty(pX, EP_xIsSelect)(((pX)->flags&(0x000800))!=0)
99765 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
99766 ){
99767 eType = IN_INDEX_NOOP5;
99768 }
99769
99770 if( eType==0 ){
99771 /* Could not find an existing table or index to use as the RHS b-tree.
99772 ** We will have to generate an ephemeral table to do the job.
99773 */
99774 u32 savedNQueryLoop = pParse->nQueryLoop;
99775 int rMayHaveNull = 0;
99776 eType = IN_INDEX_EPH2;
99777 if( inFlags & IN_INDEX_LOOP0x0004 ){
99778 pParse->nQueryLoop = 0;
99779 }else if( prRhsHasNull ){
99780 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
99781 }
99782 assert( pX->op==TK_IN )((void) (0));
99783 sqlite3CodeRhsOfIN(pParse, pX, iTab);
99784 if( rMayHaveNull ){
99785 sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
99786 }
99787 pParse->nQueryLoop = savedNQueryLoop;
99788 }
99789
99790 if( aiMap && eType!=IN_INDEX_INDEX_ASC3 && eType!=IN_INDEX_INDEX_DESC4 ){
99791 int i, n;
99792 n = sqlite3ExprVectorSize(pX->pLeft);
99793 for(i=0; i<n; i++) aiMap[i] = i;
99794 }
99795 *piTab = iTab;
99796 return eType;
99797}
99798#endif
99799
99800#ifndef SQLITE_OMIT_SUBQUERY
99801/*
99802** Argument pExpr is an (?, ?...) IN(...) expression. This
99803** function allocates and returns a nul-terminated string containing
99804** the affinities to be used for each column of the comparison.
99805**
99806** It is the responsibility of the caller to ensure that the returned
99807** string is eventually freed using sqlite3DbFree().
99808*/
99809static char *exprINAffinity(Parse *pParse, Expr *pExpr){
99810 Expr *pLeft = pExpr->pLeft;
99811 int nVal = sqlite3ExprVectorSize(pLeft);
99812 Select *pSelect = (pExpr->flags & EP_xIsSelect0x000800) ? pExpr->x.pSelect : 0;
99813 char *zRet;
99814
99815 assert( pExpr->op==TK_IN )((void) (0));
99816 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
99817 if( zRet ){
99818 int i;
99819 for(i=0; i<nVal; i++){
99820 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
99821 char a = sqlite3ExprAffinity(pA);
99822 if( pSelect ){
99823 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
99824 }else{
99825 zRet[i] = a;
99826 }
99827 }
99828 zRet[nVal] = '\0';
99829 }
99830 return zRet;
99831}
99832#endif
99833
99834#ifndef SQLITE_OMIT_SUBQUERY
99835/*
99836** Load the Parse object passed as the first argument with an error
99837** message of the form:
99838**
99839** "sub-select returns N columns - expected M"
99840*/
99841SQLITE_PRIVATEstatic void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
99842 const char *zFmt = "sub-select returns %d columns - expected %d";
99843 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
99844}
99845#endif
99846
99847/*
99848** Expression pExpr is a vector that has been used in a context where
99849** it is not permitted. If pExpr is a sub-select vector, this routine
99850** loads the Parse object with a message of the form:
99851**
99852** "sub-select returns N columns - expected 1"
99853**
99854** Or, if it is a regular scalar vector:
99855**
99856** "row value misused"
99857*/
99858SQLITE_PRIVATEstatic void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
99859#ifndef SQLITE_OMIT_SUBQUERY
99860 if( pExpr->flags & EP_xIsSelect0x000800 ){
99861 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
99862 }else
99863#endif
99864 {
99865 sqlite3ErrorMsg(pParse, "row value misused");
99866 }
99867}
99868
99869#ifndef SQLITE_OMIT_SUBQUERY
99870/*
99871** Generate code that will construct an ephemeral table containing all terms
99872** in the RHS of an IN operator. The IN operator can be in either of two
99873** forms:
99874**
99875** x IN (4,5,11) -- IN operator with list on right-hand side
99876** x IN (SELECT a FROM b) -- IN operator with subquery on the right
99877**
99878** The pExpr parameter is the IN operator. The cursor number for the
99879** constructed ephermeral table is returned. The first time the ephemeral
99880** table is computed, the cursor number is also stored in pExpr->iTable,
99881** however the cursor number returned might not be the same, as it might
99882** have been duplicated using OP_OpenDup.
99883**
99884** If the LHS expression ("x" in the examples) is a column value, or
99885** the SELECT statement returns a column value, then the affinity of that
99886** column is used to build the index keys. If both 'x' and the
99887** SELECT... statement are columns, then numeric affinity is used
99888** if either column has NUMERIC or INTEGER affinity. If neither
99889** 'x' nor the SELECT... statement are columns, then numeric affinity
99890** is used.
99891*/
99892SQLITE_PRIVATEstatic void sqlite3CodeRhsOfIN(
99893 Parse *pParse, /* Parsing context */
99894 Expr *pExpr, /* The IN operator */
99895 int iTab /* Use this cursor number */
99896){
99897 int addrOnce = 0; /* Address of the OP_Once instruction at top */
99898 int addr; /* Address of OP_OpenEphemeral instruction */
99899 Expr *pLeft; /* the LHS of the IN operator */
99900 KeyInfo *pKeyInfo = 0; /* Key information */
99901 int nVal; /* Size of vector pLeft */
99902 Vdbe *v; /* The prepared statement under construction */
99903
99904 v = pParse->pVdbe;
99905 assert( v!=0 )((void) (0));
99906
99907 /* The evaluation of the IN must be repeated every time it
99908 ** is encountered if any of the following is true:
99909 **
99910 ** * The right-hand side is a correlated subquery
99911 ** * The right-hand side is an expression list containing variables
99912 ** * We are inside a trigger
99913 **
99914 ** If all of the above are false, then we can compute the RHS just once
99915 ** and reuse it many names.
99916 */
99917 if( !ExprHasProperty(pExpr, EP_VarSelect)(((pExpr)->flags&(0x000020))!=0) && pParse->iSelfTab==0 ){
99918 /* Reuse of the RHS is allowed */
99919 /* If this routine has already been coded, but the previous code
99920 ** might not have been invoked yet, so invoke it now as a subroutine.
99921 */
99922 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
99923 addrOnce = sqlite3VdbeAddOp0(v, OP_Once17); VdbeCoverage(v);
99924 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
99925 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",sqlite3VdbeExplain (pParse, 0, "REUSE LIST SUBQUERY %d", pExpr
->x.pSelect->selId)
99926 pExpr->x.pSelect->selId))sqlite3VdbeExplain (pParse, 0, "REUSE LIST SUBQUERY %d", pExpr
->x.pSelect->selId)
;
99927 }
99928 sqlite3VdbeAddOp2(v, OP_Gosub12, pExpr->y.sub.regReturn,
99929 pExpr->y.sub.iAddr);
99930 sqlite3VdbeAddOp2(v, OP_OpenDup111, iTab, pExpr->iTable);
99931 sqlite3VdbeJumpHere(v, addrOnce);
99932 return;
99933 }
99934
99935 /* Begin coding the subroutine */
99936 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(0x2000000);
99937 pExpr->y.sub.regReturn = ++pParse->nMem;
99938 pExpr->y.sub.iAddr =
99939 sqlite3VdbeAddOp2(v, OP_Integer70, 0, pExpr->y.sub.regReturn) + 1;
99940 VdbeComment((v, "return address"));
99941
99942 addrOnce = sqlite3VdbeAddOp0(v, OP_Once17); VdbeCoverage(v);
99943 }
99944
99945 /* Check to see if this is a vector IN operator */
99946 pLeft = pExpr->pLeft;
99947 nVal = sqlite3ExprVectorSize(pLeft);
99948
99949 /* Construct the ephemeral table that will contain the content of
99950 ** RHS of the IN operator.
99951 */
99952 pExpr->iTable = iTab;
99953 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, pExpr->iTable, nVal);
99954#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
99955 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
99956 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
99957 }else{
99958 VdbeComment((v, "RHS of IN operator"));
99959 }
99960#endif
99961 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
99962
99963 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
99964 /* Case 1: expr IN (SELECT ...)
99965 **
99966 ** Generate code to write the results of the select into the temporary
99967 ** table allocated and opened above.
99968 */
99969 Select *pSelect = pExpr->x.pSelect;
99970 ExprList *pEList = pSelect->pEList;
99971
99972 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",sqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
99973 addrOnce?"":"CORRELATED ", pSelect->selIdsqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
99974 ))sqlite3VdbeExplain (pParse, 1, "%sLIST SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSelect->selId )
;
99975 /* If the LHS and RHS of the IN operator do not match, that
99976 ** error will have been caught long before we reach this point. */
99977 if( ALWAYS(pEList->nExpr==nVal)(pEList->nExpr==nVal) ){
99978 SelectDest dest;
99979 int i;
99980 sqlite3SelectDestInit(&dest, SRT_Set11, iTab);
99981 dest.zAffSdst = exprINAffinity(pParse, pExpr);
99982 pSelect->iLimit = 0;
99983 testcase( pSelect->selFlags & SF_Distinct );
99984 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
99985 if( sqlite3Select(pParse, pSelect, &dest) ){
99986 sqlite3DbFree(pParse->db, dest.zAffSdst);
99987 sqlite3KeyInfoUnref(pKeyInfo);
99988 return;
99989 }
99990 sqlite3DbFree(pParse->db, dest.zAffSdst);
99991 assert( pKeyInfo!=0 )((void) (0)); /* OOM will cause exit after sqlite3Select() */
99992 assert( pEList!=0 )((void) (0));
99993 assert( pEList->nExpr>0 )((void) (0));
99994 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) )((void) (0));
99995 for(i=0; i<nVal; i++){
99996 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
99997 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
99998 pParse, p, pEList->a[i].pExpr
99999 );
100000 }
100001 }
100002 }else if( ALWAYS(pExpr->x.pList!=0)(pExpr->x.pList!=0) ){
100003 /* Case 2: expr IN (exprlist)
100004 **
100005 ** For each expression, build an index key from the evaluation and
100006 ** store it in the temporary table. If <expr> is a column, then use
100007 ** that columns affinity when building index keys. If <expr> is not
100008 ** a column, use numeric affinity.
100009 */
100010 char affinity; /* Affinity of the LHS of the IN */
100011 int i;
100012 ExprList *pList = pExpr->x.pList;
100013 struct ExprList_item *pItem;
100014 int r1, r2, r3;
100015 affinity = sqlite3ExprAffinity(pLeft);
100016 if( !affinity ){
100017 affinity = SQLITE_AFF_BLOB'A';
100018 }
100019 if( pKeyInfo ){
100020 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) )((void) (0));
100021 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
100022 }
100023
100024 /* Loop through each expression in <exprlist>. */
100025 r1 = sqlite3GetTempReg(pParse);
100026 r2 = sqlite3GetTempReg(pParse);
100027 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
100028 Expr *pE2 = pItem->pExpr;
100029
100030 /* If the expression is not constant then we will need to
100031 ** disable the test that was generated above that makes sure
100032 ** this code only executes once. Because for a non-constant
100033 ** expression we need to rerun this code each time.
100034 */
100035 if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
100036 sqlite3VdbeChangeToNoop(v, addrOnce);
100037 ExprClearProperty(pExpr, EP_Subrtn)(pExpr)->flags&=~(0x2000000);
100038 addrOnce = 0;
100039 }
100040
100041 /* Evaluate the expression and insert it into the temp table */
100042 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
100043 sqlite3VdbeAddOp4(v, OP_MakeRecord92, r3, 1, r2, &affinity, 1);
100044 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iTab, r2, r3, 1);
100045 }
100046 sqlite3ReleaseTempReg(pParse, r1);
100047 sqlite3ReleaseTempReg(pParse, r2);
100048 }
100049 if( pKeyInfo ){
100050 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO(-9));
100051 }
100052 if( addrOnce ){
100053 sqlite3VdbeJumpHere(v, addrOnce);
100054 /* Subroutine return */
100055 sqlite3VdbeAddOp1(v, OP_Return66, pExpr->y.sub.regReturn);
100056 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
100057 }
100058}
100059#endif /* SQLITE_OMIT_SUBQUERY */
100060
100061/*
100062** Generate code for scalar subqueries used as a subquery expression
100063** or EXISTS operator:
100064**
100065** (SELECT a FROM b) -- subquery
100066** EXISTS (SELECT a FROM b) -- EXISTS subquery
100067**
100068** The pExpr parameter is the SELECT or EXISTS operator to be coded.
100069**
100070** The register that holds the result. For a multi-column SELECT,
100071** the result is stored in a contiguous array of registers and the
100072** return value is the register of the left-most result column.
100073** Return 0 if an error occurs.
100074*/
100075#ifndef SQLITE_OMIT_SUBQUERY
100076SQLITE_PRIVATEstatic int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
100077 int addrOnce = 0; /* Address of OP_Once at top of subroutine */
100078 int rReg = 0; /* Register storing resulting */
100079 Select *pSel; /* SELECT statement to encode */
100080 SelectDest dest; /* How to deal with SELECT result */
100081 int nReg; /* Registers to allocate */
100082 Expr *pLimit; /* New limit expression */
100083
100084 Vdbe *v = pParse->pVdbe;
100085 assert( v!=0 )((void) (0));
100086 testcase( pExpr->op==TK_EXISTS );
100087 testcase( pExpr->op==TK_SELECT );
100088 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT )((void) (0));
100089 assert( ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
100090 pSel = pExpr->x.pSelect;
100091
100092 /* The evaluation of the EXISTS/SELECT must be repeated every time it
100093 ** is encountered if any of the following is true:
100094 **
100095 ** * The right-hand side is a correlated subquery
100096 ** * The right-hand side is an expression list containing variables
100097 ** * We are inside a trigger
100098 **
100099 ** If all of the above are false, then we can run this code just once
100100 ** save the results, and reuse the same result on subsequent invocations.
100101 */
100102 if( !ExprHasProperty(pExpr, EP_VarSelect)(((pExpr)->flags&(0x000020))!=0) ){
100103 /* If this routine has already been coded, then invoke it as a
100104 ** subroutine. */
100105 if( ExprHasProperty(pExpr, EP_Subrtn)(((pExpr)->flags&(0x2000000))!=0) ){
100106 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId))sqlite3VdbeExplain (pParse, 0, "REUSE SUBQUERY %d", pSel->
selId)
;
100107 sqlite3VdbeAddOp2(v, OP_Gosub12, pExpr->y.sub.regReturn,
100108 pExpr->y.sub.iAddr);
100109 return pExpr->iTable;
100110 }
100111
100112 /* Begin coding the subroutine */
100113 ExprSetProperty(pExpr, EP_Subrtn)(pExpr)->flags|=(0x2000000);
100114 pExpr->y.sub.regReturn = ++pParse->nMem;
100115 pExpr->y.sub.iAddr =
100116 sqlite3VdbeAddOp2(v, OP_Integer70, 0, pExpr->y.sub.regReturn) + 1;
100117 VdbeComment((v, "return address"));
100118
100119 addrOnce = sqlite3VdbeAddOp0(v, OP_Once17); VdbeCoverage(v);
100120 }
100121
100122 /* For a SELECT, generate code to put the values for all columns of
100123 ** the first row into an array of registers and return the index of
100124 ** the first register.
100125 **
100126 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
100127 ** into a register and return that register number.
100128 **
100129 ** In both cases, the query is augmented with "LIMIT 1". Any
100130 ** preexisting limit is discarded in place of the new LIMIT 1.
100131 */
100132 ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY %d",sqlite3VdbeExplain (pParse, 1, "%sSCALAR SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSel->selId)
100133 addrOnce?"":"CORRELATED ", pSel->selId))sqlite3VdbeExplain (pParse, 1, "%sSCALAR SUBQUERY %d", addrOnce
?"":"CORRELATED ", pSel->selId)
;
100134 nReg = pExpr->op==TK_SELECT131 ? pSel->pEList->nExpr : 1;
100135 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
100136 pParse->nMem += nReg;
100137 if( pExpr->op==TK_SELECT131 ){
100138 dest.eDest = SRT_Mem10;
100139 dest.iSdst = dest.iSDParm;
100140 dest.nSdst = nReg;
100141 sqlite3VdbeAddOp3(v, OP_Null73, 0, dest.iSDParm, dest.iSDParm+nReg-1);
100142 VdbeComment((v, "Init subquery result"));
100143 }else{
100144 dest.eDest = SRT_Exists3;
100145 sqlite3VdbeAddOp2(v, OP_Integer70, 0, dest.iSDParm);
100146 VdbeComment((v, "Init EXISTS result"));
100147 }
100148 pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER147,&sqlite3IntTokens[1], 0);
100149 if( pSel->pLimit ){
100150 sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft);
100151 pSel->pLimit->pLeft = pLimit;
100152 }else{
100153 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT141, pLimit, 0);
100154 }
100155 pSel->iLimit = 0;
100156 if( sqlite3Select(pParse, pSel, &dest) ){
100157 return 0;
100158 }
100159 pExpr->iTable = rReg = dest.iSDParm;
100160 ExprSetVVAProperty(pExpr, EP_NoReduce);
100161 if( addrOnce ){
100162 sqlite3VdbeJumpHere(v, addrOnce);
100163
100164 /* Subroutine return */
100165 sqlite3VdbeAddOp1(v, OP_Return66, pExpr->y.sub.regReturn);
100166 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
100167 }
100168
100169 return rReg;
100170}
100171#endif /* SQLITE_OMIT_SUBQUERY */
100172
100173#ifndef SQLITE_OMIT_SUBQUERY
100174/*
100175** Expr pIn is an IN(...) expression. This function checks that the
100176** sub-select on the RHS of the IN() operator has the same number of
100177** columns as the vector on the LHS. Or, if the RHS of the IN() is not
100178** a sub-query, that the LHS is a vector of size 1.
100179*/
100180SQLITE_PRIVATEstatic int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
100181 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
100182 if( (pIn->flags & EP_xIsSelect0x000800) ){
100183 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
100184 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
100185 return 1;
100186 }
100187 }else if( nVector!=1 ){
100188 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
100189 return 1;
100190 }
100191 return 0;
100192}
100193#endif
100194
100195#ifndef SQLITE_OMIT_SUBQUERY
100196/*
100197** Generate code for an IN expression.
100198**
100199** x IN (SELECT ...)
100200** x IN (value, value, ...)
100201**
100202** The left-hand side (LHS) is a scalar or vector expression. The
100203** right-hand side (RHS) is an array of zero or more scalar values, or a
100204** subquery. If the RHS is a subquery, the number of result columns must
100205** match the number of columns in the vector on the LHS. If the RHS is
100206** a list of values, the LHS must be a scalar.
100207**
100208** The IN operator is true if the LHS value is contained within the RHS.
100209** The result is false if the LHS is definitely not in the RHS. The
100210** result is NULL if the presence of the LHS in the RHS cannot be
100211** determined due to NULLs.
100212**
100213** This routine generates code that jumps to destIfFalse if the LHS is not
100214** contained within the RHS. If due to NULLs we cannot determine if the LHS
100215** is contained in the RHS then jump to destIfNull. If the LHS is contained
100216** within the RHS then fall through.
100217**
100218** See the separate in-operator.md documentation file in the canonical
100219** SQLite source tree for additional information.
100220*/
100221static void sqlite3ExprCodeIN(
100222 Parse *pParse, /* Parsing and code generating context */
100223 Expr *pExpr, /* The IN expression */
100224 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
100225 int destIfNull /* Jump here if the results are unknown due to NULLs */
100226){
100227 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
100228 int eType; /* Type of the RHS */
100229 int rLhs; /* Register(s) holding the LHS values */
100230 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
100231 Vdbe *v; /* Statement under construction */
100232 int *aiMap = 0; /* Map from vector field to index column */
100233 char *zAff = 0; /* Affinity string for comparisons */
100234 int nVector; /* Size of vectors for this IN operator */
100235 int iDummy; /* Dummy parameter to exprCodeVector() */
100236 Expr *pLeft; /* The LHS of the IN operator */
100237 int i; /* loop counter */
100238 int destStep2; /* Where to jump when NULLs seen in step 2 */
100239 int destStep6 = 0; /* Start of code for Step 6 */
100240 int addrTruthOp; /* Address of opcode that determines the IN is true */
100241 int destNotNull; /* Jump here if a comparison is not true in step 6 */
100242 int addrTop; /* Top of the step-6 loop */
100243 int iTab = 0; /* Index to use */
100244
100245 pLeft = pExpr->pLeft;
100246 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
100247 zAff = exprINAffinity(pParse, pExpr);
100248 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
100249 aiMap = (int*)sqlite3DbMallocZero(
100250 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
100251 );
100252 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
100253
100254 /* Attempt to compute the RHS. After this step, if anything other than
100255 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
100256 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
100257 ** the RHS has not yet been coded. */
100258 v = pParse->pVdbe;
100259 assert( v!=0 )((void) (0)); /* OOM detected prior to this routine */
100260 VdbeNoopComment((v, "begin IN expr"));
100261 eType = sqlite3FindInIndex(pParse, pExpr,
100262 IN_INDEX_MEMBERSHIP0x0002 | IN_INDEX_NOOP_OK0x0001,
100263 destIfFalse==destIfNull ? 0 : &rRhsHasNull,
100264 aiMap, &iTab);
100265
100266 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH((void) (0))
100267 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC((void) (0))
100268 )((void) (0));
100269#ifdef SQLITE_DEBUG
100270 /* Confirm that aiMap[] contains nVector integer values between 0 and
100271 ** nVector-1. */
100272 for(i=0; i<nVector; i++){
100273 int j, cnt;
100274 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
100275 assert( cnt==1 )((void) (0));
100276 }
100277#endif
100278
100279 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
100280 ** vector, then it is stored in an array of nVector registers starting
100281 ** at r1.
100282 **
100283 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
100284 ** so that the fields are in the same order as an existing index. The
100285 ** aiMap[] array contains a mapping from the original LHS field order to
100286 ** the field order that matches the RHS index.
100287 */
100288 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
100289 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
100290 if( i==nVector ){
100291 /* LHS fields are not reordered */
100292 rLhs = rLhsOrig;
100293 }else{
100294 /* Need to reorder the LHS fields according to aiMap */
100295 rLhs = sqlite3GetTempRange(pParse, nVector);
100296 for(i=0; i<nVector; i++){
100297 sqlite3VdbeAddOp3(v, OP_Copy78, rLhsOrig+i, rLhs+aiMap[i], 0);
100298 }
100299 }
100300
100301 /* If sqlite3FindInIndex() did not find or create an index that is
100302 ** suitable for evaluating the IN operator, then evaluate using a
100303 ** sequence of comparisons.
100304 **
100305 ** This is step (1) in the in-operator.md optimized algorithm.
100306 */
100307 if( eType==IN_INDEX_NOOP5 ){
100308 ExprList *pList = pExpr->x.pList;
100309 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
100310 int labelOk = sqlite3VdbeMakeLabel(pParse);
100311 int r2, regToFree;
100312 int regCkNull = 0;
100313 int ii;
100314 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
100315 if( destIfNull!=destIfFalse ){
100316 regCkNull = sqlite3GetTempReg(pParse);
100317 sqlite3VdbeAddOp3(v, OP_BitAnd96, rLhs, rLhs, regCkNull);
100318 }
100319 for(ii=0; ii<pList->nExpr; ii++){
100320 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
100321 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
100322 sqlite3VdbeAddOp3(v, OP_BitAnd96, regCkNull, r2, regCkNull);
100323 }
100324 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
100325 sqlite3VdbeAddOp4(v, OP_Eq53, rLhs, labelOk, r2,
100326 (void*)pColl, P4_COLLSEQ(-2));
100327 VdbeCoverageIf(v, ii<pList->nExpr-1);
100328 VdbeCoverageIf(v, ii==pList->nExpr-1);
100329 sqlite3VdbeChangeP5(v, zAff[0]);
100330 }else{
100331 assert( destIfNull==destIfFalse )((void) (0));
100332 sqlite3VdbeAddOp4(v, OP_Ne52, rLhs, destIfFalse, r2,
100333 (void*)pColl, P4_COLLSEQ(-2)); VdbeCoverage(v);
100334 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL0x10);
100335 }
100336 sqlite3ReleaseTempReg(pParse, regToFree);
100337 }
100338 if( regCkNull ){
100339 sqlite3VdbeAddOp2(v, OP_IsNull50, regCkNull, destIfNull); VdbeCoverage(v);
100340 sqlite3VdbeGoto(v, destIfFalse);
100341 }
100342 sqlite3VdbeResolveLabel(v, labelOk);
100343 sqlite3ReleaseTempReg(pParse, regCkNull);
100344 goto sqlite3ExprCodeIN_finished;
100345 }
100346
100347 /* Step 2: Check to see if the LHS contains any NULL columns. If the
100348 ** LHS does contain NULLs then the result must be either FALSE or NULL.
100349 ** We will then skip the binary search of the RHS.
100350 */
100351 if( destIfNull==destIfFalse ){
100352 destStep2 = destIfFalse;
100353 }else{
100354 destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
100355 }
100356 for(i=0; i<nVector; i++){
100357 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
100358 if( sqlite3ExprCanBeNull(p) ){
100359 sqlite3VdbeAddOp2(v, OP_IsNull50, rLhs+i, destStep2);
100360 VdbeCoverage(v);
100361 }
100362 }
100363
100364 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
100365 ** of the RHS using the LHS as a probe. If found, the result is
100366 ** true.
100367 */
100368 if( eType==IN_INDEX_ROWID1 ){
100369 /* In this case, the RHS is the ROWID of table b-tree and so we also
100370 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
100371 ** into a single opcode. */
100372 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iTab, destIfFalse, rLhs);
100373 VdbeCoverage(v);
100374 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto11); /* Return True */
100375 }else{
100376 sqlite3VdbeAddOp4(v, OP_Affinity91, rLhs, nVector, 0, zAff, nVector);
100377 if( destIfFalse==destIfNull ){
100378 /* Combine Step 3 and Step 5 into a single opcode */
100379 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iTab, destIfFalse,
100380 rLhs, nVector); VdbeCoverage(v);
100381 goto sqlite3ExprCodeIN_finished;
100382 }
100383 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
100384 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found29, iTab, 0,
100385 rLhs, nVector); VdbeCoverage(v);
100386 }
100387
100388 /* Step 4. If the RHS is known to be non-NULL and we did not find
100389 ** an match on the search above, then the result must be FALSE.
100390 */
100391 if( rRhsHasNull && nVector==1 ){
100392 sqlite3VdbeAddOp2(v, OP_NotNull51, rRhsHasNull, destIfFalse);
100393 VdbeCoverage(v);
100394 }
100395
100396 /* Step 5. If we do not care about the difference between NULL and
100397 ** FALSE, then just return false.
100398 */
100399 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
100400
100401 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
100402 ** If any comparison is NULL, then the result is NULL. If all
100403 ** comparisons are FALSE then the final result is FALSE.
100404 **
100405 ** For a scalar LHS, it is sufficient to check just the first row
100406 ** of the RHS.
100407 */
100408 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
100409 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind36, iTab, destIfFalse);
100410 VdbeCoverage(v);
100411 if( nVector>1 ){
100412 destNotNull = sqlite3VdbeMakeLabel(pParse);
100413 }else{
100414 /* For nVector==1, combine steps 6 and 7 by immediately returning
100415 ** FALSE if the first comparison is not NULL */
100416 destNotNull = destIfFalse;
100417 }
100418 for(i=0; i<nVector; i++){
100419 Expr *p;
100420 CollSeq *pColl;
100421 int r3 = sqlite3GetTempReg(pParse);
100422 p = sqlite3VectorFieldSubexpr(pLeft, i);
100423 pColl = sqlite3ExprCollSeq(pParse, p);
100424 sqlite3VdbeAddOp3(v, OP_Column90, iTab, i, r3);
100425 sqlite3VdbeAddOp4(v, OP_Ne52, rLhs+i, destNotNull, r3,
100426 (void*)pColl, P4_COLLSEQ(-2));
100427 VdbeCoverage(v);
100428 sqlite3ReleaseTempReg(pParse, r3);
100429 }
100430 sqlite3VdbeAddOp2(v, OP_Goto11, 0, destIfNull);
100431 if( nVector>1 ){
100432 sqlite3VdbeResolveLabel(v, destNotNull);
100433 sqlite3VdbeAddOp2(v, OP_Next5, iTab, addrTop+1);
100434 VdbeCoverage(v);
100435
100436 /* Step 7: If we reach this point, we know that the result must
100437 ** be false. */
100438 sqlite3VdbeAddOp2(v, OP_Goto11, 0, destIfFalse);
100439 }
100440
100441 /* Jumps here in order to return true. */
100442 sqlite3VdbeJumpHere(v, addrTruthOp);
100443
100444sqlite3ExprCodeIN_finished:
100445 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
100446 VdbeComment((v, "end IN expr"));
100447sqlite3ExprCodeIN_oom_error:
100448 sqlite3DbFree(pParse->db, aiMap);
100449 sqlite3DbFree(pParse->db, zAff);
100450}
100451#endif /* SQLITE_OMIT_SUBQUERY */
100452
100453#ifndef SQLITE_OMIT_FLOATING_POINT
100454/*
100455** Generate an instruction that will put the floating point
100456** value described by z[0..n-1] into register iMem.
100457**
100458** The z[] string will probably not be zero-terminated. But the
100459** z[n] character is guaranteed to be something that does not look
100460** like the continuation of the number.
100461*/
100462static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
100463 if( ALWAYS(z!=0)(z!=0) ){
100464 double value;
100465 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF81);
100466 assert( !sqlite3IsNaN(value) )((void) (0)); /* The new AtoF never returns NaN */
100467 if( negateFlag ) value = -value;
100468 sqlite3VdbeAddOp4Dup8(v, OP_Real145, 0, iMem, 0, (u8*)&value, P4_REAL(-13));
100469 }
100470}
100471#endif
100472
100473
100474/*
100475** Generate an instruction that will put the integer describe by
100476** text z[0..n-1] into register iMem.
100477**
100478** Expr.u.zToken is always UTF8 and zero-terminated.
100479*/
100480static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
100481 Vdbe *v = pParse->pVdbe;
100482 if( pExpr->flags & EP_IntValue0x000400 ){
100483 int i = pExpr->u.iValue;
100484 assert( i>=0 )((void) (0));
100485 if( negFlag ) i = -i;
100486 sqlite3VdbeAddOp2(v, OP_Integer70, i, iMem);
100487 }else{
100488 int c;
100489 i64 value;
100490 const char *z = pExpr->u.zToken;
100491 assert( z!=0 )((void) (0));
100492 c = sqlite3DecOrHexToI64(z, &value);
100493 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))))){
100494#ifdef SQLITE_OMIT_FLOATING_POINT
100495 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
100496#else
100497#ifndef SQLITE_OMIT_HEX_INTEGER
100498 if( sqlite3_strnicmp(z,"0x",2)==0 ){
100499 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
100500 }else
100501#endif
100502 {
100503 codeReal(v, z, negFlag, iMem);
100504 }
100505#endif
100506 }else{
100507 if( negFlag ){ value = c==3 ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : -value; }
100508 sqlite3VdbeAddOp4Dup8(v, OP_Int6471, 0, iMem, 0, (u8*)&value, P4_INT64(-14));
100509 }
100510 }
100511}
100512
100513
100514/* Generate code that will load into register regOut a value that is
100515** appropriate for the iIdxCol-th column of index pIdx.
100516*/
100517SQLITE_PRIVATEstatic void sqlite3ExprCodeLoadIndexColumn(
100518 Parse *pParse, /* The parsing context */
100519 Index *pIdx, /* The index whose column is to be loaded */
100520 int iTabCur, /* Cursor pointing to a table row */
100521 int iIdxCol, /* The column of the index to be loaded */
100522 int regOut /* Store the index column value in this register */
100523){
100524 i16 iTabCol = pIdx->aiColumn[iIdxCol];
100525 if( iTabCol==XN_EXPR(-2) ){
100526 assert( pIdx->aColExpr )((void) (0));
100527 assert( pIdx->aColExpr->nExpr>iIdxCol )((void) (0));
100528 pParse->iSelfTab = iTabCur + 1;
100529 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
100530 pParse->iSelfTab = 0;
100531 }else{
100532 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
100533 iTabCol, regOut);
100534 }
100535}
100536
100537/*
100538** Generate code to extract the value of the iCol-th column of a table.
100539*/
100540SQLITE_PRIVATEstatic void sqlite3ExprCodeGetColumnOfTable(
100541 Vdbe *v, /* The VDBE under construction */
100542 Table *pTab, /* The table containing the value */
100543 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
100544 int iCol, /* Index of the column to extract */
100545 int regOut /* Extract the value into this register */
100546){
100547 if( pTab==0 ){
100548 sqlite3VdbeAddOp3(v, OP_Column90, iTabCur, iCol, regOut);
100549 return;
100550 }
100551 if( iCol<0 || iCol==pTab->iPKey ){
100552 sqlite3VdbeAddOp2(v, OP_Rowid128, iTabCur, regOut);
100553 }else{
100554 int op = IsVirtual(pTab)((pTab)->nModuleArg) ? OP_VColumn164 : OP_Column90;
100555 int x = iCol;
100556 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) && !IsVirtual(pTab)((pTab)->nModuleArg) ){
100557 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
100558 }
100559 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
100560 }
100561 if( iCol>=0 ){
100562 sqlite3ColumnDefault(v, pTab, iCol, regOut);
100563 }
100564}
100565
100566/*
100567** Generate code that will extract the iColumn-th column from
100568** table pTab and store the column value in register iReg.
100569**
100570** There must be an open cursor to pTab in iTable when this routine
100571** is called. If iColumn<0 then code is generated that extracts the rowid.
100572*/
100573SQLITE_PRIVATEstatic int sqlite3ExprCodeGetColumn(
100574 Parse *pParse, /* Parsing and code generating context */
100575 Table *pTab, /* Description of the table we are reading from */
100576 int iColumn, /* Index of the table column */
100577 int iTable, /* The cursor pointing to the table */
100578 int iReg, /* Store results here */
100579 u8 p5 /* P5 value for OP_Column + FLAGS */
100580){
100581 Vdbe *v = pParse->pVdbe;
100582 assert( v!=0 )((void) (0));
100583 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
100584 if( p5 ){
100585 sqlite3VdbeChangeP5(v, p5);
100586 }
100587 return iReg;
100588}
100589
100590/*
100591** Generate code to move content from registers iFrom...iFrom+nReg-1
100592** over to iTo..iTo+nReg-1.
100593*/
100594SQLITE_PRIVATEstatic void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
100595 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo )((void) (0));
100596 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move77, iFrom, iTo, nReg);
100597}
100598
100599/*
100600** Convert a scalar expression node to a TK_REGISTER referencing
100601** register iReg. The caller must ensure that iReg already contains
100602** the correct value for the expression.
100603*/
100604static void exprToRegister(Expr *pExpr, int iReg){
100605 Expr *p = sqlite3ExprSkipCollate(pExpr);
100606 p->op2 = p->op;
100607 p->op = TK_REGISTER168;
100608 p->iTable = iReg;
100609 ExprClearProperty(p, EP_Skip)(p)->flags&=~(0x001000);
100610}
100611
100612/*
100613** Evaluate an expression (either a vector or a scalar expression) and store
100614** the result in continguous temporary registers. Return the index of
100615** the first register used to store the result.
100616**
100617** If the returned result register is a temporary scalar, then also write
100618** that register number into *piFreeable. If the returned result register
100619** is not a temporary or if the expression is a vector set *piFreeable
100620** to 0.
100621*/
100622static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
100623 int iResult;
100624 int nResult = sqlite3ExprVectorSize(p);
100625 if( nResult==1 ){
100626 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
100627 }else{
100628 *piFreeable = 0;
100629 if( p->op==TK_SELECT131 ){
100630#if SQLITE_OMIT_SUBQUERY
100631 iResult = 0;
100632#else
100633 iResult = sqlite3CodeSubselect(pParse, p);
100634#endif
100635 }else{
100636 int i;
100637 iResult = pParse->nMem+1;
100638 pParse->nMem += nResult;
100639 for(i=0; i<nResult; i++){
100640 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
100641 }
100642 }
100643 }
100644 return iResult;
100645}
100646
100647
100648/*
100649** Generate code into the current Vdbe to evaluate the given
100650** expression. Attempt to store the results in register "target".
100651** Return the register where results are stored.
100652**
100653** With this routine, there is no guarantee that results will
100654** be stored in target. The result might be stored in some other
100655** register if it is convenient to do so. The calling function
100656** must check the return code and move the results to the desired
100657** register.
100658*/
100659SQLITE_PRIVATEstatic int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
100660 Vdbe *v = pParse->pVdbe; /* The VM under construction */
100661 int op; /* The opcode being coded */
100662 int inReg = target; /* Results stored in register inReg */
100663 int regFree1 = 0; /* If non-zero free this temporary register */
100664 int regFree2 = 0; /* If non-zero free this temporary register */
100665 int r1, r2; /* Various register numbers */
100666 Expr tempX; /* Temporary expression node */
100667 int p5 = 0;
100668
100669 assert( target>0 && target<=pParse->nMem )((void) (0));
100670 if( v==0 ){
100671 assert( pParse->db->mallocFailed )((void) (0));
100672 return 0;
100673 }
100674
100675expr_code_doover:
100676 if( pExpr==0 ){
100677 op = TK_NULL114;
100678 }else{
100679 op = pExpr->op;
100680 }
100681 switch( op ){
100682 case TK_AGG_COLUMN164: {
100683 AggInfo *pAggInfo = pExpr->pAggInfo;
100684 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
100685 if( !pAggInfo->directMode ){
100686 assert( pCol->iMem>0 )((void) (0));
100687 return pCol->iMem;
100688 }else if( pAggInfo->useSortingIdx ){
100689 sqlite3VdbeAddOp3(v, OP_Column90, pAggInfo->sortingIdxPTab,
100690 pCol->iSorterColumn, target);
100691 return target;
100692 }
100693 /* Otherwise, fall thru into the TK_COLUMN case */
100694 }
100695 case TK_COLUMN162: {
100696 int iTab = pExpr->iTable;
100697 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000008))!=0) ){
100698 /* This COLUMN expression is really a constant due to WHERE clause
100699 ** constraints, and that constant is coded by the pExpr->pLeft
100700 ** expresssion. However, make sure the constant has the correct
100701 ** datatype by applying the Affinity of the table column to the
100702 ** constant.
100703 */
100704 int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
100705 int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
100706 if( aff!=SQLITE_AFF_BLOB'A' ){
100707 static const char zAff[] = "B\000C\000D\000E";
100708 assert( SQLITE_AFF_BLOB=='A' )((void) (0));
100709 assert( SQLITE_AFF_TEXT=='B' )((void) (0));
100710 if( iReg!=target ){
100711 sqlite3VdbeAddOp2(v, OP_SCopy79, iReg, target);
100712 iReg = target;
100713 }
100714 sqlite3VdbeAddOp4(v, OP_Affinity91, iReg, 1, 0,
100715 &zAff[(aff-'B')*2], P4_STATIC(-1));
100716 }
100717 return iReg;
100718 }
100719 if( iTab<0 ){
100720 if( pParse->iSelfTab<0 ){
100721 /* Generating CHECK constraints or inserting into partial index */
100722 return pExpr->iColumn - pParse->iSelfTab;
100723 }else{
100724 /* Coding an expression that is part of an index where column names
100725 ** in the index refer to the table to which the index belongs */
100726 iTab = pParse->iSelfTab - 1;
100727 }
100728 }
100729 return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
100730 pExpr->iColumn, iTab, target,
100731 pExpr->op2);
100732 }
100733 case TK_INTEGER147: {
100734 codeInteger(pParse, pExpr, 0, target);
100735 return target;
100736 }
100737 case TK_TRUEFALSE159: {
100738 sqlite3VdbeAddOp2(v, OP_Integer70, sqlite3ExprTruthValue(pExpr), target);
100739 return target;
100740 }
100741#ifndef SQLITE_OMIT_FLOATING_POINT
100742 case TK_FLOAT145: {
100743 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
100744 codeReal(v, pExpr->u.zToken, 0, target);
100745 return target;
100746 }
100747#endif
100748 case TK_STRING110: {
100749 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
100750 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
100751 return target;
100752 }
100753 case TK_NULL114: {
100754 sqlite3VdbeAddOp2(v, OP_Null73, 0, target);
100755 return target;
100756 }
100757#ifndef SQLITE_OMIT_BLOB_LITERAL
100758 case TK_BLOB146: {
100759 int n;
100760 const char *z;
100761 char *zBlob;
100762 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
100763 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' )((void) (0));
100764 assert( pExpr->u.zToken[1]=='\'' )((void) (0));
100765 z = &pExpr->u.zToken[2];
100766 n = sqlite3Strlen30(z) - 1;
100767 assert( z[n]=='\'' )((void) (0));
100768 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
100769 sqlite3VdbeAddOp4(v, OP_Blob75, n/2, target, 0, zBlob, P4_DYNAMIC(-7));
100770 return target;
100771 }
100772#endif
100773 case TK_VARIABLE148: {
100774 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
100775 assert( pExpr->u.zToken!=0 )((void) (0));
100776 assert( pExpr->u.zToken[0]!=0 )((void) (0));
100777 sqlite3VdbeAddOp2(v, OP_Variable76, pExpr->iColumn, target);
100778 if( pExpr->u.zToken[1]!=0 ){
100779 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
100780 assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 )((void) (0));
100781 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
100782 sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC(-1));
100783 }
100784 return target;
100785 }
100786 case TK_REGISTER168: {
100787 return pExpr->iTable;
100788 }
100789#ifndef SQLITE_OMIT_CAST
100790 case TK_CAST36: {
100791 /* Expressions of the form: CAST(pLeft AS token) */
100792 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
100793 if( inReg!=target ){
100794 sqlite3VdbeAddOp2(v, OP_SCopy79, inReg, target);
100795 inReg = target;
100796 }
100797 sqlite3VdbeAddOp2(v, OP_Cast85, target,
100798 sqlite3AffinityType(pExpr->u.zToken, 0));
100799 return inReg;
100800 }
100801#endif /* SQLITE_OMIT_CAST */
100802 case TK_IS45:
100803 case TK_ISNOT160:
100804 op = (op==TK_IS45) ? TK_EQ53 : TK_NE52;
100805 p5 = SQLITE_NULLEQ0x80;
100806 /* fall-through */
100807 case TK_LT56:
100808 case TK_LE55:
100809 case TK_GT54:
100810 case TK_GE57:
100811 case TK_NE52:
100812 case TK_EQ53: {
100813 Expr *pLeft = pExpr->pLeft;
100814 if( sqlite3ExprIsVector(pLeft) ){
100815 codeVectorCompare(pParse, pExpr, target, op, p5);
100816 }else{
100817 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
100818 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
100819 codeCompare(pParse, pLeft, pExpr->pRight, op,
100820 r1, r2, inReg, SQLITE_STOREP20x20 | p5);
100821 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
100822 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
100823 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
100824 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
100825 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
100826 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
100827 testcase( regFree1==0 );
100828 testcase( regFree2==0 );
100829 }
100830 break;
100831 }
100832 case TK_AND44:
100833 case TK_OR43:
100834 case TK_PLUS100:
100835 case TK_STAR102:
100836 case TK_MINUS101:
100837 case TK_REM104:
100838 case TK_BITAND96:
100839 case TK_BITOR97:
100840 case TK_SLASH103:
100841 case TK_LSHIFT98:
100842 case TK_RSHIFT99:
100843 case TK_CONCAT105: {
100844 assert( TK_AND==OP_And )((void) (0)); testcase( op==TK_AND );
100845 assert( TK_OR==OP_Or )((void) (0)); testcase( op==TK_OR );
100846 assert( TK_PLUS==OP_Add )((void) (0)); testcase( op==TK_PLUS );
100847 assert( TK_MINUS==OP_Subtract )((void) (0)); testcase( op==TK_MINUS );
100848 assert( TK_REM==OP_Remainder )((void) (0)); testcase( op==TK_REM );
100849 assert( TK_BITAND==OP_BitAnd )((void) (0)); testcase( op==TK_BITAND );
100850 assert( TK_BITOR==OP_BitOr )((void) (0)); testcase( op==TK_BITOR );
100851 assert( TK_SLASH==OP_Divide )((void) (0)); testcase( op==TK_SLASH );
100852 assert( TK_LSHIFT==OP_ShiftLeft )((void) (0)); testcase( op==TK_LSHIFT );
100853 assert( TK_RSHIFT==OP_ShiftRight )((void) (0)); testcase( op==TK_RSHIFT );
100854 assert( TK_CONCAT==OP_Concat )((void) (0)); testcase( op==TK_CONCAT );
100855 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100856 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
100857 sqlite3VdbeAddOp3(v, op, r2, r1, target);
100858 testcase( regFree1==0 );
100859 testcase( regFree2==0 );
100860 break;
100861 }
100862 case TK_UMINUS165: {
100863 Expr *pLeft = pExpr->pLeft;
100864 assert( pLeft )((void) (0));
100865 if( pLeft->op==TK_INTEGER147 ){
100866 codeInteger(pParse, pLeft, 1, target);
100867 return target;
100868#ifndef SQLITE_OMIT_FLOATING_POINT
100869 }else if( pLeft->op==TK_FLOAT145 ){
100870 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
100871 codeReal(v, pLeft->u.zToken, 1, target);
100872 return target;
100873#endif
100874 }else{
100875 tempX.op = TK_INTEGER147;
100876 tempX.flags = EP_IntValue0x000400|EP_TokenOnly0x004000;
100877 tempX.u.iValue = 0;
100878 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
100879 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
100880 sqlite3VdbeAddOp3(v, OP_Subtract101, r2, r1, target);
100881 testcase( regFree2==0 );
100882 }
100883 break;
100884 }
100885 case TK_BITNOT107:
100886 case TK_NOT19: {
100887 assert( TK_BITNOT==OP_BitNot )((void) (0)); testcase( op==TK_BITNOT );
100888 assert( TK_NOT==OP_Not )((void) (0)); testcase( op==TK_NOT );
100889 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100890 testcase( regFree1==0 );
100891 sqlite3VdbeAddOp2(v, op, r1, inReg);
100892 break;
100893 }
100894 case TK_TRUTH167: {
100895 int isTrue; /* IS TRUE or IS NOT TRUE */
100896 int bNormal; /* IS TRUE or IS FALSE */
100897 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100898 testcase( regFree1==0 );
100899 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
100900 bNormal = pExpr->op2==TK_IS45;
100901 testcase( isTrue && bNormal);
100902 testcase( !isTrue && bNormal);
100903 sqlite3VdbeAddOp4Int(v, OP_IsTrue88, r1, inReg, !isTrue, isTrue ^ bNormal);
100904 break;
100905 }
100906 case TK_ISNULL50:
100907 case TK_NOTNULL51: {
100908 int addr;
100909 assert( TK_ISNULL==OP_IsNull )((void) (0)); testcase( op==TK_ISNULL );
100910 assert( TK_NOTNULL==OP_NotNull )((void) (0)); testcase( op==TK_NOTNULL );
100911 sqlite3VdbeAddOp2(v, OP_Integer70, 1, target);
100912 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
100913 testcase( regFree1==0 );
100914 addr = sqlite3VdbeAddOp1(v, op, r1);
100915 VdbeCoverageIf(v, op==TK_ISNULL);
100916 VdbeCoverageIf(v, op==TK_NOTNULL);
100917 sqlite3VdbeAddOp2(v, OP_Integer70, 0, target);
100918 sqlite3VdbeJumpHere(v, addr);
100919 break;
100920 }
100921 case TK_AGG_FUNCTION163: {
100922 AggInfo *pInfo = pExpr->pAggInfo;
100923 if( pInfo==0 ){
100924 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
100925 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
100926 }else{
100927 return pInfo->aFunc[pExpr->iAgg].iMem;
100928 }
100929 break;
100930 }
100931 case TK_FUNCTION161: {
100932 ExprList *pFarg; /* List of function arguments */
100933 int nFarg; /* Number of function arguments */
100934 FuncDef *pDef; /* The function definition object */
100935 const char *zId; /* The function name */
100936 u32 constMask = 0; /* Mask of function arguments that are constant */
100937 int i; /* Loop counter */
100938 sqlite3 *db = pParse->db; /* The database connection */
100939 u8 enc = ENC(db)((db)->enc); /* The text encoding used by this database */
100940 CollSeq *pColl = 0; /* A collating sequence */
100941
100942#ifndef SQLITE_OMIT_WINDOWFUNC
100943 if( ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
100944 return pExpr->y.pWin->regResult;
100945 }
100946#endif
100947
100948 if( ConstFactorOk(pParse)((pParse)->okConstFactor) && sqlite3ExprIsConstantNotJoin(pExpr) ){
100949 /* SQL functions can be expensive. So try to move constant functions
100950 ** out of the inner loop, even if that means an extra OP_Copy. */
100951 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
100952 }
100953 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
100954 if( ExprHasProperty(pExpr, EP_TokenOnly)(((pExpr)->flags&(0x004000))!=0) ){
100955 pFarg = 0;
100956 }else{
100957 pFarg = pExpr->x.pList;
100958 }
100959 nFarg = pFarg ? pFarg->nExpr : 0;
100960 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
100961 zId = pExpr->u.zToken;
100962 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
100963#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
100964 if( pDef==0 && pParse->explain ){
100965 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
100966 }
100967#endif
100968 if( pDef==0 || pDef->xFinalize!=0 ){
100969 sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
100970 break;
100971 }
100972
100973 /* Attempt a direct implementation of the built-in COALESCE() and
100974 ** IFNULL() functions. This avoids unnecessary evaluation of
100975 ** arguments past the first non-NULL argument.
100976 */
100977 if( pDef->funcFlags & SQLITE_FUNC_COALESCE0x0200 ){
100978 int endCoalesce = sqlite3VdbeMakeLabel(pParse);
100979 assert( nFarg>=2 )((void) (0));
100980 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
100981 for(i=1; i<nFarg; i++){
100982 sqlite3VdbeAddOp2(v, OP_NotNull51, target, endCoalesce);
100983 VdbeCoverage(v);
100984 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
100985 }
100986 sqlite3VdbeResolveLabel(v, endCoalesce);
100987 break;
100988 }
100989
100990 /* The UNLIKELY() function is a no-op. The result is the value
100991 ** of the first argument.
100992 */
100993 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY0x0400 ){
100994 assert( nFarg>=1 )((void) (0));
100995 return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
100996 }
100997
100998#ifdef SQLITE_DEBUG
100999 /* The AFFINITY() function evaluates to a string that describes
101000 ** the type affinity of the argument. This is used for testing of
101001 ** the SQLite type logic.
101002 */
101003 if( pDef->funcFlags & SQLITE_FUNC_AFFINITY0x4000 ){
101004 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
101005 char aff;
101006 assert( nFarg==1 )((void) (0));
101007 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
101008 sqlite3VdbeLoadString(v, target,
101009 aff ? azAff[aff-SQLITE_AFF_BLOB'A'] : "none");
101010 return target;
101011 }
101012#endif
101013
101014 for(i=0; i<nFarg; i++){
101015 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
101016 testcase( i==31 );
101017 constMask |= MASKBIT32(i)(((unsigned int)1)<<(i));
101018 }
101019 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020)!=0 && !pColl ){
101020 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
101021 }
101022 }
101023 if( pFarg ){
101024 if( constMask ){
101025 r1 = pParse->nMem+1;
101026 pParse->nMem += nFarg;
101027 }else{
101028 r1 = sqlite3GetTempRange(pParse, nFarg);
101029 }
101030
101031 /* For length() and typeof() functions with a column argument,
101032 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
101033 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
101034 ** loading.
101035 */
101036 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH0x0040|SQLITE_FUNC_TYPEOF0x0080))!=0 ){
101037 u8 exprOp;
101038 assert( nFarg==1 )((void) (0));
101039 assert( pFarg->a[0].pExpr!=0 )((void) (0));
101040 exprOp = pFarg->a[0].pExpr->op;
101041 if( exprOp==TK_COLUMN162 || exprOp==TK_AGG_COLUMN164 ){
101042 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG )((void) (0));
101043 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG )((void) (0));
101044 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
101045 pFarg->a[0].pExpr->op2 =
101046 pDef->funcFlags & (OPFLAG_LENGTHARG0x40|OPFLAG_TYPEOFARG0x80);
101047 }
101048 }
101049
101050 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
101051 SQLITE_ECEL_DUP0x01|SQLITE_ECEL_FACTOR0x02);
101052 }else{
101053 r1 = 0;
101054 }
101055#ifndef SQLITE_OMIT_VIRTUALTABLE
101056 /* Possibly overload the function if the first argument is
101057 ** a virtual table column.
101058 **
101059 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
101060 ** second argument, not the first, as the argument to test to
101061 ** see if it is a column in a virtual table. This is done because
101062 ** the left operand of infix functions (the operand we want to
101063 ** control overloading) ends up as the second argument to the
101064 ** function. The expression "A glob B" is equivalent to
101065 ** "glob(B,A). We want to use the A in "A glob B" to test
101066 ** for function overloading. But we use the B term in "glob(B,A)".
101067 */
101068 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc)(((pExpr)->flags&(0x000080))!=0) ){
101069 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
101070 }else if( nFarg>0 ){
101071 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
101072 }
101073#endif
101074 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
101075 if( !pColl ) pColl = db->pDfltColl;
101076 sqlite3VdbeAddOp4(v, OP_CollSeq82, 0, 0, 0, (char *)pColl, P4_COLLSEQ(-2));
101077 }
101078#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
101079 if( pDef->funcFlags & SQLITE_FUNC_OFFSET0x8000 ){
101080 Expr *pArg = pFarg->a[0].pExpr;
101081 if( pArg->op==TK_COLUMN162 ){
101082 sqlite3VdbeAddOp3(v, OP_Offset89, pArg->iTable, pArg->iColumn, target);
101083 }else{
101084 sqlite3VdbeAddOp2(v, OP_Null73, 0, target);
101085 }
101086 }else
101087#endif
101088 {
101089 sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc062 : OP_Function063,
101090 constMask, r1, target, (char*)pDef, P4_FUNCDEF(-8));
101091 sqlite3VdbeChangeP5(v, (u8)nFarg);
101092 }
101093 if( nFarg && constMask==0 ){
101094 sqlite3ReleaseTempRange(pParse, r1, nFarg);
101095 }
101096 return target;
101097 }
101098#ifndef SQLITE_OMIT_SUBQUERY
101099 case TK_EXISTS20:
101100 case TK_SELECT131: {
101101 int nCol;
101102 testcase( op==TK_EXISTS );
101103 testcase( op==TK_SELECT );
101104 if( op==TK_SELECT131 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
101105 sqlite3SubselectError(pParse, nCol, 1);
101106 }else{
101107 return sqlite3CodeSubselect(pParse, pExpr);
101108 }
101109 break;
101110 }
101111 case TK_SELECT_COLUMN170: {
101112 int n;
101113 if( pExpr->pLeft->iTable==0 ){
101114 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft);
101115 }
101116 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT )((void) (0));
101117 if( pExpr->iTable
101118 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
101119 ){
101120 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
101121 pExpr->iTable, n);
101122 }
101123 return pExpr->pLeft->iTable + pExpr->iColumn;
101124 }
101125 case TK_IN49: {
101126 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
101127 int destIfNull = sqlite3VdbeMakeLabel(pParse);
101128 sqlite3VdbeAddOp2(v, OP_Null73, 0, target);
101129 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
101130 sqlite3VdbeAddOp2(v, OP_Integer70, 1, target);
101131 sqlite3VdbeResolveLabel(v, destIfFalse);
101132 sqlite3VdbeAddOp2(v, OP_AddImm83, target, 0);
101133 sqlite3VdbeResolveLabel(v, destIfNull);
101134 return target;
101135 }
101136#endif /* SQLITE_OMIT_SUBQUERY */
101137
101138
101139 /*
101140 ** x BETWEEN y AND z
101141 **
101142 ** This is equivalent to
101143 **
101144 ** x>=y AND x<=z
101145 **
101146 ** X is stored in pExpr->pLeft.
101147 ** Y is stored in pExpr->pList->a[0].pExpr.
101148 ** Z is stored in pExpr->pList->a[1].pExpr.
101149 */
101150 case TK_BETWEEN48: {
101151 exprCodeBetween(pParse, pExpr, target, 0, 0);
101152 return target;
101153 }
101154 case TK_SPAN173:
101155 case TK_COLLATE106:
101156 case TK_UPLUS166: {
101157 pExpr = pExpr->pLeft;
101158 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
101159 }
101160
101161 case TK_TRIGGER77: {
101162 /* If the opcode is TK_TRIGGER, then the expression is a reference
101163 ** to a column in the new.* or old.* pseudo-tables available to
101164 ** trigger programs. In this case Expr.iTable is set to 1 for the
101165 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
101166 ** is set to the column of the pseudo-table to read, or to -1 to
101167 ** read the rowid field.
101168 **
101169 ** The expression is implemented using an OP_Param opcode. The p1
101170 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
101171 ** to reference another column of the old.* pseudo-table, where
101172 ** i is the index of the column. For a new.rowid reference, p1 is
101173 ** set to (n+1), where n is the number of columns in each pseudo-table.
101174 ** For a reference to any other column in the new.* pseudo-table, p1
101175 ** is set to (n+2+i), where n and i are as defined previously. For
101176 ** example, if the table on which triggers are being fired is
101177 ** declared as:
101178 **
101179 ** CREATE TABLE t1(a, b);
101180 **
101181 ** Then p1 is interpreted as follows:
101182 **
101183 ** p1==0 -> old.rowid p1==3 -> new.rowid
101184 ** p1==1 -> old.a p1==4 -> new.a
101185 ** p1==2 -> old.b p1==5 -> new.b
101186 */
101187 Table *pTab = pExpr->y.pTab;
101188 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
101189
101190 assert( pExpr->iTable==0 || pExpr->iTable==1 )((void) (0));
101191 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol )((void) (0));
101192 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey )((void) (0));
101193 assert( p1>=0 && p1<(pTab->nCol*2+2) )((void) (0));
101194
101195 sqlite3VdbeAddOp2(v, OP_Param149, p1, target);
101196 VdbeComment((v, "r[%d]=%s.%s", target,
101197 (pExpr->iTable ? "new" : "old"),
101198 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName)
101199 ));
101200
101201#ifndef SQLITE_OMIT_FLOATING_POINT
101202 /* If the column has REAL affinity, it may currently be stored as an
101203 ** integer. Use OP_RealAffinity to make sure it is really real.
101204 **
101205 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
101206 ** floating point when extracting it from the record. */
101207 if( pExpr->iColumn>=0
101208 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL'E'
101209 ){
101210 sqlite3VdbeAddOp1(v, OP_RealAffinity84, target);
101211 }
101212#endif
101213 break;
101214 }
101215
101216 case TK_VECTOR169: {
101217 sqlite3ErrorMsg(pParse, "row value misused");
101218 break;
101219 }
101220
101221 case TK_IF_NULL_ROW171: {
101222 int addrINR;
101223 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow21, pExpr->iTable);
101224 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
101225 sqlite3VdbeJumpHere(v, addrINR);
101226 sqlite3VdbeChangeP3(v, addrINR, inReg);
101227 break;
101228 }
101229
101230 /*
101231 ** Form A:
101232 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
101233 **
101234 ** Form B:
101235 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
101236 **
101237 ** Form A is can be transformed into the equivalent form B as follows:
101238 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
101239 ** WHEN x=eN THEN rN ELSE y END
101240 **
101241 ** X (if it exists) is in pExpr->pLeft.
101242 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
101243 ** odd. The Y is also optional. If the number of elements in x.pList
101244 ** is even, then Y is omitted and the "otherwise" result is NULL.
101245 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
101246 **
101247 ** The result of the expression is the Ri for the first matching Ei,
101248 ** or if there is no matching Ei, the ELSE term Y, or if there is
101249 ** no ELSE term, NULL.
101250 */
101251 default: assert( op==TK_CASE )((void) (0)); {
101252 int endLabel; /* GOTO label for end of CASE stmt */
101253 int nextCase; /* GOTO label for next WHEN clause */
101254 int nExpr; /* 2x number of WHEN terms */
101255 int i; /* Loop counter */
101256 ExprList *pEList; /* List of WHEN terms */
101257 struct ExprList_item *aListelem; /* Array of WHEN terms */
101258 Expr opCompare; /* The X==Ei expression */
101259 Expr *pX; /* The X expression */
101260 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
101261
101262 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList )((void) (0));
101263 assert(pExpr->x.pList->nExpr > 0)((void) (0));
101264 pEList = pExpr->x.pList;
101265 aListelem = pEList->a;
101266 nExpr = pEList->nExpr;
101267 endLabel = sqlite3VdbeMakeLabel(pParse);
101268 if( (pX = pExpr->pLeft)!=0 ){
101269 exprNodeCopy(&tempX, pX);
101270 testcase( pX->op==TK_COLUMN );
101271 exprToRegister(&tempX, exprCodeVector(pParse, &tempX, &regFree1));
101272 testcase( regFree1==0 );
101273 memset(&opCompare, 0, sizeof(opCompare));
101274 opCompare.op = TK_EQ53;
101275 opCompare.pLeft = &tempX;
101276 pTest = &opCompare;
101277 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
101278 ** The value in regFree1 might get SCopy-ed into the file result.
101279 ** So make sure that the regFree1 register is not reused for other
101280 ** purposes and possibly overwritten. */
101281 regFree1 = 0;
101282 }
101283 for(i=0; i<nExpr-1; i=i+2){
101284 if( pX ){
101285 assert( pTest!=0 )((void) (0));
101286 opCompare.pRight = aListelem[i].pExpr;
101287 }else{
101288 pTest = aListelem[i].pExpr;
101289 }
101290 nextCase = sqlite3VdbeMakeLabel(pParse);
101291 testcase( pTest->op==TK_COLUMN );
101292 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL0x10);
101293 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
101294 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
101295 sqlite3VdbeGoto(v, endLabel);
101296 sqlite3VdbeResolveLabel(v, nextCase);
101297 }
101298 if( (nExpr&1)!=0 ){
101299 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
101300 }else{
101301 sqlite3VdbeAddOp2(v, OP_Null73, 0, target);
101302 }
101303 sqlite3VdbeResolveLabel(v, endLabel);
101304 break;
101305 }
101306#ifndef SQLITE_OMIT_TRIGGER
101307 case TK_RAISE71: {
101308 assert( pExpr->affinity==OE_Rollback((void) (0))
101309 || pExpr->affinity==OE_Abort((void) (0))
101310 || pExpr->affinity==OE_Fail((void) (0))
101311 || pExpr->affinity==OE_Ignore((void) (0))
101312 )((void) (0));
101313 if( !pParse->pTriggerTab ){
101314 sqlite3ErrorMsg(pParse,
101315 "RAISE() may only be used within a trigger-program");
101316 return 0;
101317 }
101318 if( pExpr->affinity==OE_Abort2 ){
101319 sqlite3MayAbort(pParse);
101320 }
101321 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
101322 if( pExpr->affinity==OE_Ignore4 ){
101323 sqlite3VdbeAddOp4(
101324 v, OP_Halt69, SQLITE_OK0, OE_Ignore4, 0, pExpr->u.zToken,0);
101325 VdbeCoverage(v);
101326 }else{
101327 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)),
101328 pExpr->affinity, pExpr->u.zToken, 0, 0);
101329 }
101330
101331 break;
101332 }
101333#endif
101334 }
101335 sqlite3ReleaseTempReg(pParse, regFree1);
101336 sqlite3ReleaseTempReg(pParse, regFree2);
101337 return inReg;
101338}
101339
101340/*
101341** Factor out the code of the given expression to initialization time.
101342**
101343** If regDest>=0 then the result is always stored in that register and the
101344** result is not reusable. If regDest<0 then this routine is free to
101345** store the value whereever it wants. The register where the expression
101346** is stored is returned. When regDest<0, two identical expressions will
101347** code to the same register.
101348*/
101349SQLITE_PRIVATEstatic int sqlite3ExprCodeAtInit(
101350 Parse *pParse, /* Parsing context */
101351 Expr *pExpr, /* The expression to code when the VDBE initializes */
101352 int regDest /* Store the value in this register */
101353){
101354 ExprList *p;
101355 assert( ConstFactorOk(pParse) )((void) (0));
101356 p = pParse->pConstExpr;
101357 if( regDest<0 && p ){
101358 struct ExprList_item *pItem;
101359 int i;
101360 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
101361 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
101362 return pItem->u.iConstExprReg;
101363 }
101364 }
101365 }
101366 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
101367 p = sqlite3ExprListAppend(pParse, p, pExpr);
101368 if( p ){
101369 struct ExprList_item *pItem = &p->a[p->nExpr-1];
101370 pItem->reusable = regDest<0;
101371 if( regDest<0 ) regDest = ++pParse->nMem;
101372 pItem->u.iConstExprReg = regDest;
101373 }
101374 pParse->pConstExpr = p;
101375 return regDest;
101376}
101377
101378/*
101379** Generate code to evaluate an expression and store the results
101380** into a register. Return the register number where the results
101381** are stored.
101382**
101383** If the register is a temporary register that can be deallocated,
101384** then write its number into *pReg. If the result register is not
101385** a temporary, then set *pReg to zero.
101386**
101387** If pExpr is a constant, then this routine might generate this
101388** code to fill the register in the initialization section of the
101389** VDBE program, in order to factor it out of the evaluation loop.
101390*/
101391SQLITE_PRIVATEstatic int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
101392 int r2;
101393 pExpr = sqlite3ExprSkipCollate(pExpr);
101394 if( ConstFactorOk(pParse)((pParse)->okConstFactor)
101395 && pExpr->op!=TK_REGISTER168
101396 && sqlite3ExprIsConstantNotJoin(pExpr)
101397 ){
101398 *pReg = 0;
101399 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
101400 }else{
101401 int r1 = sqlite3GetTempReg(pParse);
101402 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
101403 if( r2==r1 ){
101404 *pReg = r1;
101405 }else{
101406 sqlite3ReleaseTempReg(pParse, r1);
101407 *pReg = 0;
101408 }
101409 }
101410 return r2;
101411}
101412
101413/*
101414** Generate code that will evaluate expression pExpr and store the
101415** results in register target. The results are guaranteed to appear
101416** in register target.
101417*/
101418SQLITE_PRIVATEstatic void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
101419 int inReg;
101420
101421 assert( target>0 && target<=pParse->nMem )((void) (0));
101422 if( pExpr && pExpr->op==TK_REGISTER168 ){
101423 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy78, pExpr->iTable, target);
101424 }else{
101425 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
101426 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed )((void) (0));
101427 if( inReg!=target && pParse->pVdbe ){
101428 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy79, inReg, target);
101429 }
101430 }
101431}
101432
101433/*
101434** Make a transient copy of expression pExpr and then code it using
101435** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
101436** except that the input expression is guaranteed to be unchanged.
101437*/
101438SQLITE_PRIVATEstatic void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
101439 sqlite3 *db = pParse->db;
101440 pExpr = sqlite3ExprDup(db, pExpr, 0);
101441 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
101442 sqlite3ExprDelete(db, pExpr);
101443}
101444
101445/*
101446** Generate code that will evaluate expression pExpr and store the
101447** results in register target. The results are guaranteed to appear
101448** in register target. If the expression is constant, then this routine
101449** might choose to code the expression at initialization time.
101450*/
101451SQLITE_PRIVATEstatic void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
101452 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
101453 sqlite3ExprCodeAtInit(pParse, pExpr, target);
101454 }else{
101455 sqlite3ExprCode(pParse, pExpr, target);
101456 }
101457}
101458
101459/*
101460** Generate code that evaluates the given expression and puts the result
101461** in register target.
101462**
101463** Also make a copy of the expression results into another "cache" register
101464** and modify the expression so that the next time it is evaluated,
101465** the result is a copy of the cache register.
101466**
101467** This routine is used for expressions that are used multiple
101468** times. They are evaluated once and the results of the expression
101469** are reused.
101470*/
101471SQLITE_PRIVATEstatic void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
101472 Vdbe *v = pParse->pVdbe;
101473 int iMem;
101474
101475 assert( target>0 )((void) (0));
101476 assert( pExpr->op!=TK_REGISTER )((void) (0));
101477 sqlite3ExprCode(pParse, pExpr, target);
101478 iMem = ++pParse->nMem;
101479 sqlite3VdbeAddOp2(v, OP_Copy78, target, iMem);
101480 exprToRegister(pExpr, iMem);
101481}
101482
101483/*
101484** Generate code that pushes the value of every element of the given
101485** expression list into a sequence of registers beginning at target.
101486**
101487** Return the number of elements evaluated. The number returned will
101488** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
101489** is defined.
101490**
101491** The SQLITE_ECEL_DUP flag prevents the arguments from being
101492** filled using OP_SCopy. OP_Copy must be used instead.
101493**
101494** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
101495** factored out into initialization code.
101496**
101497** The SQLITE_ECEL_REF flag means that expressions in the list with
101498** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
101499** in registers at srcReg, and so the value can be copied from there.
101500** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
101501** are simply omitted rather than being copied from srcReg.
101502*/
101503SQLITE_PRIVATEstatic int sqlite3ExprCodeExprList(
101504 Parse *pParse, /* Parsing context */
101505 ExprList *pList, /* The expression list to be coded */
101506 int target, /* Where to write results */
101507 int srcReg, /* Source registers if SQLITE_ECEL_REF */
101508 u8 flags /* SQLITE_ECEL_* flags */
101509){
101510 struct ExprList_item *pItem;
101511 int i, j, n;
101512 u8 copyOp = (flags & SQLITE_ECEL_DUP0x01) ? OP_Copy78 : OP_SCopy79;
101513 Vdbe *v = pParse->pVdbe;
101514 assert( pList!=0 )((void) (0));
101515 assert( target>0 )((void) (0));
101516 assert( pParse->pVdbe!=0 )((void) (0)); /* Never gets this far otherwise */
101517 n = pList->nExpr;
101518 if( !ConstFactorOk(pParse)((pParse)->okConstFactor) ) flags &= ~SQLITE_ECEL_FACTOR0x02;
101519 for(pItem=pList->a, i=0; i<n; i++, pItem++){
101520 Expr *pExpr = pItem->pExpr;
101521#ifdef SQLITE_ENABLE_SORTER_REFERENCES
101522 if( pItem->bSorterRef ){
101523 i--;
101524 n--;
101525 }else
101526#endif
101527 if( (flags & SQLITE_ECEL_REF0x04)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
101528 if( flags & SQLITE_ECEL_OMITREF0x08 ){
101529 i--;
101530 n--;
101531 }else{
101532 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
101533 }
101534 }else if( (flags & SQLITE_ECEL_FACTOR0x02)!=0
101535 && sqlite3ExprIsConstantNotJoin(pExpr)
101536 ){
101537 sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
101538 }else{
101539 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
101540 if( inReg!=target+i ){
101541 VdbeOp *pOp;
101542 if( copyOp==OP_Copy78
101543 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy78
101544 && pOp->p1+pOp->p3+1==inReg
101545 && pOp->p2+pOp->p3+1==target+i
101546 ){
101547 pOp->p3++;
101548 }else{
101549 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
101550 }
101551 }
101552 }
101553 }
101554 return n;
101555}
101556
101557/*
101558** Generate code for a BETWEEN operator.
101559**
101560** x BETWEEN y AND z
101561**
101562** The above is equivalent to
101563**
101564** x>=y AND x<=z
101565**
101566** Code it as such, taking care to do the common subexpression
101567** elimination of x.
101568**
101569** The xJumpIf parameter determines details:
101570**
101571** NULL: Store the boolean result in reg[dest]
101572** sqlite3ExprIfTrue: Jump to dest if true
101573** sqlite3ExprIfFalse: Jump to dest if false
101574**
101575** The jumpIfNull parameter is ignored if xJumpIf is NULL.
101576*/
101577static void exprCodeBetween(
101578 Parse *pParse, /* Parsing and code generating context */
101579 Expr *pExpr, /* The BETWEEN expression */
101580 int dest, /* Jump destination or storage location */
101581 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
101582 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
101583){
101584 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
101585 Expr compLeft; /* The x>=y term */
101586 Expr compRight; /* The x<=z term */
101587 Expr exprX; /* The x subexpression */
101588 int regFree1 = 0; /* Temporary use register */
101589
101590 memset(&compLeft, 0, sizeof(Expr));
101591 memset(&compRight, 0, sizeof(Expr));
101592 memset(&exprAnd, 0, sizeof(Expr));
101593
101594 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
101595 exprNodeCopy(&exprX, pExpr->pLeft);
101596 exprAnd.op = TK_AND44;
101597 exprAnd.pLeft = &compLeft;
101598 exprAnd.pRight = &compRight;
101599 compLeft.op = TK_GE57;
101600 compLeft.pLeft = &exprX;
101601 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
101602 compRight.op = TK_LE55;
101603 compRight.pLeft = &exprX;
101604 compRight.pRight = pExpr->x.pList->a[1].pExpr;
101605 exprToRegister(&exprX, exprCodeVector(pParse, &exprX, &regFree1));
101606 if( xJump ){
101607 xJump(pParse, &exprAnd, dest, jumpIfNull);
101608 }else{
101609 /* Mark the expression is being from the ON or USING clause of a join
101610 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
101611 ** it into the Parse.pConstExpr list. We should use a new bit for this,
101612 ** for clarity, but we are out of bits in the Expr.flags field so we
101613 ** have to reuse the EP_FromJoin bit. Bummer. */
101614 exprX.flags |= EP_FromJoin0x000001;
101615 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
101616 }
101617 sqlite3ReleaseTempReg(pParse, regFree1);
101618
101619 /* Ensure adequate test coverage */
101620 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
101621 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
101622 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
101623 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
101624 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
101625 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
101626 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
101627 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
101628 testcase( xJump==0 );
101629}
101630
101631/*
101632** Generate code for a boolean expression such that a jump is made
101633** to the label "dest" if the expression is true but execution
101634** continues straight thru if the expression is false.
101635**
101636** If the expression evaluates to NULL (neither true nor false), then
101637** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
101638**
101639** This code depends on the fact that certain token values (ex: TK_EQ)
101640** are the same as opcode values (ex: OP_Eq) that implement the corresponding
101641** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
101642** the make process cause these values to align. Assert()s in the code
101643** below verify that the numbers are aligned correctly.
101644*/
101645SQLITE_PRIVATEstatic void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
101646 Vdbe *v = pParse->pVdbe;
101647 int op = 0;
101648 int regFree1 = 0;
101649 int regFree2 = 0;
101650 int r1, r2;
101651
101652 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 )((void) (0));
101653 if( NEVER(v==0)(v==0) ) return; /* Existence of VDBE checked by caller */
101654 if( NEVER(pExpr==0)(pExpr==0) ) return; /* No way this can happen */
101655 op = pExpr->op;
101656 switch( op ){
101657 case TK_AND44:
101658 case TK_OR43: {
101659 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
101660 if( pAlt!=pExpr ){
101661 sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull);
101662 }else if( op==TK_AND44 ){
101663 int d2 = sqlite3VdbeMakeLabel(pParse);
101664 testcase( jumpIfNull==0 );
101665 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
101666 jumpIfNull^SQLITE_JUMPIFNULL0x10);
101667 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
101668 sqlite3VdbeResolveLabel(v, d2);
101669 }else{
101670 testcase( jumpIfNull==0 );
101671 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
101672 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
101673 }
101674 break;
101675 }
101676 case TK_NOT19: {
101677 testcase( jumpIfNull==0 );
101678 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
101679 break;
101680 }
101681 case TK_TRUTH167: {
101682 int isNot; /* IS NOT TRUE or IS NOT FALSE */
101683 int isTrue; /* IS TRUE or IS NOT TRUE */
101684 testcase( jumpIfNull==0 );
101685 isNot = pExpr->op2==TK_ISNOT160;
101686 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
101687 testcase( isTrue && isNot );
101688 testcase( !isTrue && isNot );
101689 if( isTrue ^ isNot ){
101690 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
101691 isNot ? SQLITE_JUMPIFNULL0x10 : 0);
101692 }else{
101693 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
101694 isNot ? SQLITE_JUMPIFNULL0x10 : 0);
101695 }
101696 break;
101697 }
101698 case TK_IS45:
101699 case TK_ISNOT160:
101700 testcase( op==TK_IS );
101701 testcase( op==TK_ISNOT );
101702 op = (op==TK_IS45) ? TK_EQ53 : TK_NE52;
101703 jumpIfNull = SQLITE_NULLEQ0x80;
101704 /* Fall thru */
101705 case TK_LT56:
101706 case TK_LE55:
101707 case TK_GT54:
101708 case TK_GE57:
101709 case TK_NE52:
101710 case TK_EQ53: {
101711 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
101712 testcase( jumpIfNull==0 );
101713 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
101714 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
101715 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
101716 r1, r2, dest, jumpIfNull);
101717 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
101718 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
101719 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
101720 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
101721 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq);
101722 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
101723 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
101724 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne);
101725 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
101726 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
101727 testcase( regFree1==0 );
101728 testcase( regFree2==0 );
101729 break;
101730 }
101731 case TK_ISNULL50:
101732 case TK_NOTNULL51: {
101733 assert( TK_ISNULL==OP_IsNull )((void) (0)); testcase( op==TK_ISNULL );
101734 assert( TK_NOTNULL==OP_NotNull )((void) (0)); testcase( op==TK_NOTNULL );
101735 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
101736 sqlite3VdbeAddOp2(v, op, r1, dest);
101737 VdbeCoverageIf(v, op==TK_ISNULL);
101738 VdbeCoverageIf(v, op==TK_NOTNULL);
101739 testcase( regFree1==0 );
101740 break;
101741 }
101742 case TK_BETWEEN48: {
101743 testcase( jumpIfNull==0 );
101744 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
101745 break;
101746 }
101747#ifndef SQLITE_OMIT_SUBQUERY
101748 case TK_IN49: {
101749 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
101750 int destIfNull = jumpIfNull ? dest : destIfFalse;
101751 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
101752 sqlite3VdbeGoto(v, dest);
101753 sqlite3VdbeResolveLabel(v, destIfFalse);
101754 break;
101755 }
101756#endif
101757 default: {
101758 default_expr:
101759 if( ExprAlwaysTrue(pExpr)(((pExpr)->flags&(0x000001|0x10000000))==0x10000000) ){
101760 sqlite3VdbeGoto(v, dest);
101761 }else if( ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000) ){
101762 /* No-op */
101763 }else{
101764 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
101765 sqlite3VdbeAddOp3(v, OP_If18, r1, dest, jumpIfNull!=0);
101766 VdbeCoverage(v);
101767 testcase( regFree1==0 );
101768 testcase( jumpIfNull==0 );
101769 }
101770 break;
101771 }
101772 }
101773 sqlite3ReleaseTempReg(pParse, regFree1);
101774 sqlite3ReleaseTempReg(pParse, regFree2);
101775}
101776
101777/*
101778** Generate code for a boolean expression such that a jump is made
101779** to the label "dest" if the expression is false but execution
101780** continues straight thru if the expression is true.
101781**
101782** If the expression evaluates to NULL (neither true nor false) then
101783** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
101784** is 0.
101785*/
101786SQLITE_PRIVATEstatic void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
101787 Vdbe *v = pParse->pVdbe;
101788 int op = 0;
101789 int regFree1 = 0;
101790 int regFree2 = 0;
101791 int r1, r2;
101792
101793 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 )((void) (0));
101794 if( NEVER(v==0)(v==0) ) return; /* Existence of VDBE checked by caller */
101795 if( pExpr==0 ) return;
101796
101797 /* The value of pExpr->op and op are related as follows:
101798 **
101799 ** pExpr->op op
101800 ** --------- ----------
101801 ** TK_ISNULL OP_NotNull
101802 ** TK_NOTNULL OP_IsNull
101803 ** TK_NE OP_Eq
101804 ** TK_EQ OP_Ne
101805 ** TK_GT OP_Le
101806 ** TK_LE OP_Gt
101807 ** TK_GE OP_Lt
101808 ** TK_LT OP_Ge
101809 **
101810 ** For other values of pExpr->op, op is undefined and unused.
101811 ** The value of TK_ and OP_ constants are arranged such that we
101812 ** can compute the mapping above using the following expression.
101813 ** Assert()s verify that the computation is correct.
101814 */
101815 op = ((pExpr->op+(TK_ISNULL50&1))^1)-(TK_ISNULL50&1);
101816
101817 /* Verify correct alignment of TK_ and OP_ constants
101818 */
101819 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull )((void) (0));
101820 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull )((void) (0));
101821 assert( pExpr->op!=TK_NE || op==OP_Eq )((void) (0));
101822 assert( pExpr->op!=TK_EQ || op==OP_Ne )((void) (0));
101823 assert( pExpr->op!=TK_LT || op==OP_Ge )((void) (0));
101824 assert( pExpr->op!=TK_LE || op==OP_Gt )((void) (0));
101825 assert( pExpr->op!=TK_GT || op==OP_Le )((void) (0));
101826 assert( pExpr->op!=TK_GE || op==OP_Lt )((void) (0));
101827
101828 switch( pExpr->op ){
101829 case TK_AND44:
101830 case TK_OR43: {
101831 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
101832 if( pAlt!=pExpr ){
101833 sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull);
101834 }else if( pExpr->op==TK_AND44 ){
101835 testcase( jumpIfNull==0 );
101836 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
101837 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
101838 }else{
101839 int d2 = sqlite3VdbeMakeLabel(pParse);
101840 testcase( jumpIfNull==0 );
101841 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
101842 jumpIfNull^SQLITE_JUMPIFNULL0x10);
101843 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
101844 sqlite3VdbeResolveLabel(v, d2);
101845 }
101846 break;
101847 }
101848 case TK_NOT19: {
101849 testcase( jumpIfNull==0 );
101850 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
101851 break;
101852 }
101853 case TK_TRUTH167: {
101854 int isNot; /* IS NOT TRUE or IS NOT FALSE */
101855 int isTrue; /* IS TRUE or IS NOT TRUE */
101856 testcase( jumpIfNull==0 );
101857 isNot = pExpr->op2==TK_ISNOT160;
101858 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
101859 testcase( isTrue && isNot );
101860 testcase( !isTrue && isNot );
101861 if( isTrue ^ isNot ){
101862 /* IS TRUE and IS NOT FALSE */
101863 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
101864 isNot ? 0 : SQLITE_JUMPIFNULL0x10);
101865
101866 }else{
101867 /* IS FALSE and IS NOT TRUE */
101868 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
101869 isNot ? 0 : SQLITE_JUMPIFNULL0x10);
101870 }
101871 break;
101872 }
101873 case TK_IS45:
101874 case TK_ISNOT160:
101875 testcase( pExpr->op==TK_IS );
101876 testcase( pExpr->op==TK_ISNOT );
101877 op = (pExpr->op==TK_IS45) ? TK_NE52 : TK_EQ53;
101878 jumpIfNull = SQLITE_NULLEQ0x80;
101879 /* Fall thru */
101880 case TK_LT56:
101881 case TK_LE55:
101882 case TK_GT54:
101883 case TK_GE57:
101884 case TK_NE52:
101885 case TK_EQ53: {
101886 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
101887 testcase( jumpIfNull==0 );
101888 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
101889 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
101890 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
101891 r1, r2, dest, jumpIfNull);
101892 assert(TK_LT==OP_Lt)((void) (0)); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
101893 assert(TK_LE==OP_Le)((void) (0)); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
101894 assert(TK_GT==OP_Gt)((void) (0)); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
101895 assert(TK_GE==OP_Ge)((void) (0)); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
101896 assert(TK_EQ==OP_Eq)((void) (0)); testcase(op==OP_Eq);
101897 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
101898 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
101899 assert(TK_NE==OP_Ne)((void) (0)); testcase(op==OP_Ne);
101900 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
101901 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
101902 testcase( regFree1==0 );
101903 testcase( regFree2==0 );
101904 break;
101905 }
101906 case TK_ISNULL50:
101907 case TK_NOTNULL51: {
101908 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
101909 sqlite3VdbeAddOp2(v, op, r1, dest);
101910 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
101911 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
101912 testcase( regFree1==0 );
101913 break;
101914 }
101915 case TK_BETWEEN48: {
101916 testcase( jumpIfNull==0 );
101917 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
101918 break;
101919 }
101920#ifndef SQLITE_OMIT_SUBQUERY
101921 case TK_IN49: {
101922 if( jumpIfNull ){
101923 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
101924 }else{
101925 int destIfNull = sqlite3VdbeMakeLabel(pParse);
101926 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
101927 sqlite3VdbeResolveLabel(v, destIfNull);
101928 }
101929 break;
101930 }
101931#endif
101932 default: {
101933 default_expr:
101934 if( ExprAlwaysFalse(pExpr)(((pExpr)->flags&(0x000001|0x20000000))==0x20000000) ){
101935 sqlite3VdbeGoto(v, dest);
101936 }else if( ExprAlwaysTrue(pExpr)(((pExpr)->flags&(0x000001|0x10000000))==0x10000000) ){
101937 /* no-op */
101938 }else{
101939 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
101940 sqlite3VdbeAddOp3(v, OP_IfNot20, r1, dest, jumpIfNull!=0);
101941 VdbeCoverage(v);
101942 testcase( regFree1==0 );
101943 testcase( jumpIfNull==0 );
101944 }
101945 break;
101946 }
101947 }
101948 sqlite3ReleaseTempReg(pParse, regFree1);
101949 sqlite3ReleaseTempReg(pParse, regFree2);
101950}
101951
101952/*
101953** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
101954** code generation, and that copy is deleted after code generation. This
101955** ensures that the original pExpr is unchanged.
101956*/
101957SQLITE_PRIVATEstatic void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
101958 sqlite3 *db = pParse->db;
101959 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
101960 if( db->mallocFailed==0 ){
101961 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
101962 }
101963 sqlite3ExprDelete(db, pCopy);
101964}
101965
101966/*
101967** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
101968** type of expression.
101969**
101970** If pExpr is a simple SQL value - an integer, real, string, blob
101971** or NULL value - then the VDBE currently being prepared is configured
101972** to re-prepare each time a new value is bound to variable pVar.
101973**
101974** Additionally, if pExpr is a simple SQL value and the value is the
101975** same as that currently bound to variable pVar, non-zero is returned.
101976** Otherwise, if the values are not the same or if pExpr is not a simple
101977** SQL value, zero is returned.
101978*/
101979static int exprCompareVariable(Parse *pParse, Expr *pVar, Expr *pExpr){
101980 int res = 0;
101981 int iVar;
101982 sqlite3_value *pL, *pR = 0;
101983
101984 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF81, SQLITE_AFF_BLOB'A', &pR);
101985 if( pR ){
101986 iVar = pVar->iColumn;
101987 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
101988 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB'A');
101989 if( pL ){
101990 if( sqlite3_value_type(pL)==SQLITE_TEXT3 ){
101991 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
101992 }
101993 res = 0==sqlite3MemCompare(pL, pR, 0);
101994 }
101995 sqlite3ValueFree(pR);
101996 sqlite3ValueFree(pL);
101997 }
101998
101999 return res;
102000}
102001
102002/*
102003** Do a deep comparison of two expression trees. Return 0 if the two
102004** expressions are completely identical. Return 1 if they differ only
102005** by a COLLATE operator at the top level. Return 2 if there are differences
102006** other than the top-level COLLATE operator.
102007**
102008** If any subelement of pB has Expr.iTable==(-1) then it is allowed
102009** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
102010**
102011** The pA side might be using TK_REGISTER. If that is the case and pB is
102012** not using TK_REGISTER but is otherwise equivalent, then still return 0.
102013**
102014** Sometimes this routine will return 2 even if the two expressions
102015** really are equivalent. If we cannot prove that the expressions are
102016** identical, we return 2 just to be safe. So if this routine
102017** returns 2, then you do not really know for certain if the two
102018** expressions are the same. But if you get a 0 or 1 return, then you
102019** can be sure the expressions are the same. In the places where
102020** this routine is used, it does not hurt to get an extra 2 - that
102021** just might result in some slightly slower code. But returning
102022** an incorrect 0 or 1 could lead to a malfunction.
102023**
102024** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
102025** pParse->pReprepare can be matched against literals in pB. The
102026** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
102027** If pParse is NULL (the normal case) then any TK_VARIABLE term in
102028** Argument pParse should normally be NULL. If it is not NULL and pA or
102029** pB causes a return value of 2.
102030*/
102031SQLITE_PRIVATEstatic int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){
102032 u32 combinedFlags;
102033 if( pA==0 || pB==0 ){
102034 return pB==pA ? 0 : 2;
102035 }
102036 if( pParse && pA->op==TK_VARIABLE148 && exprCompareVariable(pParse, pA, pB) ){
102037 return 0;
102038 }
102039 combinedFlags = pA->flags | pB->flags;
102040 if( combinedFlags & EP_IntValue0x000400 ){
102041 if( (pA->flags&pB->flags&EP_IntValue0x000400)!=0 && pA->u.iValue==pB->u.iValue ){
102042 return 0;
102043 }
102044 return 2;
102045 }
102046 if( pA->op!=pB->op || pA->op==TK_RAISE71 ){
102047 if( pA->op==TK_COLLATE106 && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
102048 return 1;
102049 }
102050 if( pB->op==TK_COLLATE106 && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
102051 return 1;
102052 }
102053 return 2;
102054 }
102055 if( pA->op!=TK_COLUMN162 && pA->op!=TK_AGG_COLUMN164 && pA->u.zToken ){
102056 if( pA->op==TK_FUNCTION161 ){
102057 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
102058#ifndef SQLITE_OMIT_WINDOWFUNC
102059 /* Justification for the assert():
102060 ** window functions have p->op==TK_FUNCTION but aggregate functions
102061 ** have p->op==TK_AGG_FUNCTION. So any comparison between an aggregate
102062 ** function and a window function should have failed before reaching
102063 ** this point. And, it is not possible to have a window function and
102064 ** a scalar function with the same name and number of arguments. So
102065 ** if we reach this point, either A and B both window functions or
102066 ** neither are a window functions. */
102067 assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) )((void) (0));
102068 if( ExprHasProperty(pA,EP_WinFunc)(((pA)->flags&(0x1000000))!=0) ){
102069 if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2;
102070 }
102071#endif
102072 }else if( pA->op==TK_NULL114 ){
102073 return 0;
102074 }else if( pA->op==TK_COLLATE106 ){
102075 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
102076 }else if( ALWAYS(pB->u.zToken!=0)(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
102077 return 2;
102078 }
102079 }
102080 if( (pA->flags & EP_Distinct0x000002)!=(pB->flags & EP_Distinct0x000002) ) return 2;
102081 if( (combinedFlags & EP_TokenOnly0x004000)==0 ){
102082 if( combinedFlags & EP_xIsSelect0x000800 ) return 2;
102083 if( (combinedFlags & EP_FixedCol0x000008)==0
102084 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
102085 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
102086 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
102087 if( pA->op!=TK_STRING110
102088 && pA->op!=TK_TRUEFALSE159
102089 && (combinedFlags & EP_Reduced0x002000)==0
102090 ){
102091 if( pA->iColumn!=pB->iColumn ) return 2;
102092 if( pA->op2!=pB->op2 ) return 2;
102093 if( pA->iTable!=pB->iTable
102094 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)(pB->iTable>=0)) ) return 2;
102095 }
102096 }
102097 return 0;
102098}
102099
102100/*
102101** Compare two ExprList objects. Return 0 if they are identical and
102102** non-zero if they differ in any way.
102103**
102104** If any subelement of pB has Expr.iTable==(-1) then it is allowed
102105** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
102106**
102107** This routine might return non-zero for equivalent ExprLists. The
102108** only consequence will be disabled optimizations. But this routine
102109** must never return 0 if the two ExprList objects are different, or
102110** a malfunction will result.
102111**
102112** Two NULL pointers are considered to be the same. But a NULL pointer
102113** always differs from a non-NULL pointer.
102114*/
102115SQLITE_PRIVATEstatic int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
102116 int i;
102117 if( pA==0 && pB==0 ) return 0;
102118 if( pA==0 || pB==0 ) return 1;
102119 if( pA->nExpr!=pB->nExpr ) return 1;
102120 for(i=0; i<pA->nExpr; i++){
102121 Expr *pExprA = pA->a[i].pExpr;
102122 Expr *pExprB = pB->a[i].pExpr;
102123 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
102124 if( sqlite3ExprCompare(0, pExprA, pExprB, iTab) ) return 1;
102125 }
102126 return 0;
102127}
102128
102129/*
102130** Like sqlite3ExprCompare() except COLLATE operators at the top-level
102131** are ignored.
102132*/
102133SQLITE_PRIVATEstatic int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
102134 return sqlite3ExprCompare(0,
102135 sqlite3ExprSkipCollate(pA),
102136 sqlite3ExprSkipCollate(pB),
102137 iTab);
102138}
102139
102140/*
102141** Return non-zero if Expr p can only be true if pNN is not NULL.
102142*/
102143static int exprImpliesNotNull(
102144 Parse *pParse, /* Parsing context */
102145 Expr *p, /* The expression to be checked */
102146 Expr *pNN, /* The expression that is NOT NULL */
102147 int iTab, /* Table being evaluated */
102148 int seenNot /* True if p is an operand of NOT */
102149){
102150 assert( p )((void) (0));
102151 assert( pNN )((void) (0));
102152 if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ) return 1;
102153 switch( p->op ){
102154 case TK_IN49: {
102155 if( seenNot && ExprHasProperty(p, EP_xIsSelect)(((p)->flags&(0x000800))!=0) ) return 0;
102156 assert( ExprHasProperty(p,EP_xIsSelect)((void) (0))
102157 || (p->x.pList!=0 && p->x.pList->nExpr>0) )((void) (0));
102158 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
102159 }
102160 case TK_BETWEEN48: {
102161 ExprList *pList = p->x.pList;
102162 assert( pList!=0 )((void) (0));
102163 assert( pList->nExpr==2 )((void) (0));
102164 if( seenNot ) return 0;
102165 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, seenNot)
102166 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, seenNot)
102167 ){
102168 return 1;
102169 }
102170 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
102171 }
102172 case TK_EQ53:
102173 case TK_NE52:
102174 case TK_LT56:
102175 case TK_LE55:
102176 case TK_GT54:
102177 case TK_GE57:
102178 case TK_PLUS100:
102179 case TK_MINUS101:
102180 case TK_STAR102:
102181 case TK_REM104:
102182 case TK_BITAND96:
102183 case TK_BITOR97:
102184 case TK_SLASH103:
102185 case TK_LSHIFT98:
102186 case TK_RSHIFT99:
102187 case TK_CONCAT105: {
102188 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
102189 /* Fall thru into the next case */
102190 }
102191 case TK_SPAN173:
102192 case TK_COLLATE106:
102193 case TK_BITNOT107:
102194 case TK_UPLUS166:
102195 case TK_UMINUS165: {
102196 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
102197 }
102198 case TK_TRUTH167: {
102199 if( seenNot ) return 0;
102200 if( p->op2!=TK_IS45 ) return 0;
102201 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
102202 }
102203 case TK_NOT19: {
102204 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
102205 }
102206 }
102207 return 0;
102208}
102209
102210/*
102211** Return true if we can prove the pE2 will always be true if pE1 is
102212** true. Return false if we cannot complete the proof or if pE2 might
102213** be false. Examples:
102214**
102215** pE1: x==5 pE2: x==5 Result: true
102216** pE1: x>0 pE2: x==5 Result: false
102217** pE1: x=21 pE2: x=21 OR y=43 Result: true
102218** pE1: x!=123 pE2: x IS NOT NULL Result: true
102219** pE1: x!=?1 pE2: x IS NOT NULL Result: true
102220** pE1: x IS NULL pE2: x IS NOT NULL Result: false
102221** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
102222**
102223** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
102224** Expr.iTable<0 then assume a table number given by iTab.
102225**
102226** If pParse is not NULL, then the values of bound variables in pE1 are
102227** compared against literal values in pE2 and pParse->pVdbe->expmask is
102228** modified to record which bound variables are referenced. If pParse
102229** is NULL, then false will be returned if pE1 contains any bound variables.
102230**
102231** When in doubt, return false. Returning true might give a performance
102232** improvement. Returning false might cause a performance reduction, but
102233** it will always give the correct answer and is hence always safe.
102234*/
102235SQLITE_PRIVATEstatic int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){
102236 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
102237 return 1;
102238 }
102239 if( pE2->op==TK_OR43
102240 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
102241 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
102242 ){
102243 return 1;
102244 }
102245 if( pE2->op==TK_NOTNULL51
102246 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
102247 ){
102248 return 1;
102249 }
102250 return 0;
102251}
102252
102253/*
102254** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
102255** If the expression node requires that the table at pWalker->iCur
102256** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
102257**
102258** This routine controls an optimization. False positives (setting
102259** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
102260** (never setting pWalker->eCode) is a harmless missed optimization.
102261*/
102262static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
102263 testcase( pExpr->op==TK_AGG_COLUMN );
102264 testcase( pExpr->op==TK_AGG_FUNCTION );
102265 if( ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0) ) return WRC_Prune1;
102266 switch( pExpr->op ){
102267 case TK_ISNOT160:
102268 case TK_NOT19:
102269 case TK_ISNULL50:
102270 case TK_NOTNULL51:
102271 case TK_IS45:
102272 case TK_OR43:
102273 case TK_CASE149:
102274 case TK_IN49:
102275 case TK_FUNCTION161:
102276 testcase( pExpr->op==TK_ISNOT );
102277 testcase( pExpr->op==TK_NOT );
102278 testcase( pExpr->op==TK_ISNULL );
102279 testcase( pExpr->op==TK_NOTNULL );
102280 testcase( pExpr->op==TK_IS );
102281 testcase( pExpr->op==TK_OR );
102282 testcase( pExpr->op==TK_CASE );
102283 testcase( pExpr->op==TK_IN );
102284 testcase( pExpr->op==TK_FUNCTION );
102285 return WRC_Prune1;
102286 case TK_COLUMN162:
102287 if( pWalker->u.iCur==pExpr->iTable ){
102288 pWalker->eCode = 1;
102289 return WRC_Abort2;
102290 }
102291 return WRC_Prune1;
102292
102293 /* Virtual tables are allowed to use constraints like x=NULL. So
102294 ** a term of the form x=y does not prove that y is not null if x
102295 ** is the column of a virtual table */
102296 case TK_EQ53:
102297 case TK_NE52:
102298 case TK_LT56:
102299 case TK_LE55:
102300 case TK_GT54:
102301 case TK_GE57:
102302 testcase( pExpr->op==TK_EQ );
102303 testcase( pExpr->op==TK_NE );
102304 testcase( pExpr->op==TK_LT );
102305 testcase( pExpr->op==TK_LE );
102306 testcase( pExpr->op==TK_GT );
102307 testcase( pExpr->op==TK_GE );
102308 if( (pExpr->pLeft->op==TK_COLUMN162 && IsVirtual(pExpr->pLeft->y.pTab)((pExpr->pLeft->y.pTab)->nModuleArg))
102309 || (pExpr->pRight->op==TK_COLUMN162 && IsVirtual(pExpr->pRight->y.pTab)((pExpr->pRight->y.pTab)->nModuleArg))
102310 ){
102311 return WRC_Prune1;
102312 }
102313 default:
102314 return WRC_Continue0;
102315 }
102316}
102317
102318/*
102319** Return true (non-zero) if expression p can only be true if at least
102320** one column of table iTab is non-null. In other words, return true
102321** if expression p will always be NULL or false if every column of iTab
102322** is NULL.
102323**
102324** False negatives are acceptable. In other words, it is ok to return
102325** zero even if expression p will never be true of every column of iTab
102326** is NULL. A false negative is merely a missed optimization opportunity.
102327**
102328** False positives are not allowed, however. A false positive may result
102329** in an incorrect answer.
102330**
102331** Terms of p that are marked with EP_FromJoin (and hence that come from
102332** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
102333**
102334** This routine is used to check if a LEFT JOIN can be converted into
102335** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
102336** clause requires that some column of the right table of the LEFT JOIN
102337** be non-NULL, then the LEFT JOIN can be safely converted into an
102338** ordinary join.
102339*/
102340SQLITE_PRIVATEstatic int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
102341 Walker w;
102342 p = sqlite3ExprSkipCollate(p);
102343 while( p ){
102344 if( p->op==TK_NOTNULL51 ){
102345 p = p->pLeft;
102346 }else if( p->op==TK_AND44 ){
102347 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
102348 p = p->pRight;
102349 }else{
102350 break;
102351 }
102352 }
102353 w.xExprCallback = impliesNotNullRow;
102354 w.xSelectCallback = 0;
102355 w.xSelectCallback2 = 0;
102356 w.eCode = 0;
102357 w.u.iCur = iTab;
102358 sqlite3WalkExpr(&w, p);
102359 return w.eCode;
102360}
102361
102362/*
102363** An instance of the following structure is used by the tree walker
102364** to determine if an expression can be evaluated by reference to the
102365** index only, without having to do a search for the corresponding
102366** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
102367** is the cursor for the table.
102368*/
102369struct IdxCover {
102370 Index *pIdx; /* The index to be tested for coverage */
102371 int iCur; /* Cursor number for the table corresponding to the index */
102372};
102373
102374/*
102375** Check to see if there are references to columns in table
102376** pWalker->u.pIdxCover->iCur can be satisfied using the index
102377** pWalker->u.pIdxCover->pIdx.
102378*/
102379static int exprIdxCover(Walker *pWalker, Expr *pExpr){
102380 if( pExpr->op==TK_COLUMN162
102381 && pExpr->iTable==pWalker->u.pIdxCover->iCur
102382 && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
102383 ){
102384 pWalker->eCode = 1;
102385 return WRC_Abort2;
102386 }
102387 return WRC_Continue0;
102388}
102389
102390/*
102391** Determine if an index pIdx on table with cursor iCur contains will
102392** the expression pExpr. Return true if the index does cover the
102393** expression and false if the pExpr expression references table columns
102394** that are not found in the index pIdx.
102395**
102396** An index covering an expression means that the expression can be
102397** evaluated using only the index and without having to lookup the
102398** corresponding table entry.
102399*/
102400SQLITE_PRIVATEstatic int sqlite3ExprCoveredByIndex(
102401 Expr *pExpr, /* The index to be tested */
102402 int iCur, /* The cursor number for the corresponding table */
102403 Index *pIdx /* The index that might be used for coverage */
102404){
102405 Walker w;
102406 struct IdxCover xcov;
102407 memset(&w, 0, sizeof(w));
102408 xcov.iCur = iCur;
102409 xcov.pIdx = pIdx;
102410 w.xExprCallback = exprIdxCover;
102411 w.u.pIdxCover = &xcov;
102412 sqlite3WalkExpr(&w, pExpr);
102413 return !w.eCode;
102414}
102415
102416
102417/*
102418** An instance of the following structure is used by the tree walker
102419** to count references to table columns in the arguments of an
102420** aggregate function, in order to implement the
102421** sqlite3FunctionThisSrc() routine.
102422*/
102423struct SrcCount {
102424 SrcList *pSrc; /* One particular FROM clause in a nested query */
102425 int nThis; /* Number of references to columns in pSrcList */
102426 int nOther; /* Number of references to columns in other FROM clauses */
102427};
102428
102429/*
102430** Count the number of references to columns.
102431*/
102432static int exprSrcCount(Walker *pWalker, Expr *pExpr){
102433 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
102434 ** is always called before sqlite3ExprAnalyzeAggregates() and so the
102435 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If
102436 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
102437 ** NEVER() will need to be removed. */
102438 if( pExpr->op==TK_COLUMN162 || NEVER(pExpr->op==TK_AGG_COLUMN)(pExpr->op==164) ){
102439 int i;
102440 struct SrcCount *p = pWalker->u.pSrcCount;
102441 SrcList *pSrc = p->pSrc;
102442 int nSrc = pSrc ? pSrc->nSrc : 0;
102443 for(i=0; i<nSrc; i++){
102444 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
102445 }
102446 if( i<nSrc ){
102447 p->nThis++;
102448 }else{
102449 p->nOther++;
102450 }
102451 }
102452 return WRC_Continue0;
102453}
102454
102455/*
102456** Determine if any of the arguments to the pExpr Function reference
102457** pSrcList. Return true if they do. Also return true if the function
102458** has no arguments or has only constant arguments. Return false if pExpr
102459** references columns but not columns of tables found in pSrcList.
102460*/
102461SQLITE_PRIVATEstatic int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
102462 Walker w;
102463 struct SrcCount cnt;
102464 assert( pExpr->op==TK_AGG_FUNCTION )((void) (0));
102465 w.xExprCallback = exprSrcCount;
102466 w.xSelectCallback = 0;
102467 w.u.pSrcCount = &cnt;
102468 cnt.pSrc = pSrcList;
102469 cnt.nThis = 0;
102470 cnt.nOther = 0;
102471 sqlite3WalkExprList(&w, pExpr->x.pList);
102472 return cnt.nThis>0 || cnt.nOther==0;
102473}
102474
102475/*
102476** Add a new element to the pAggInfo->aCol[] array. Return the index of
102477** the new element. Return a negative number if malloc fails.
102478*/
102479static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
102480 int i;
102481 pInfo->aCol = sqlite3ArrayAllocate(
102482 db,
102483 pInfo->aCol,
102484 sizeof(pInfo->aCol[0]),
102485 &pInfo->nColumn,
102486 &i
102487 );
102488 return i;
102489}
102490
102491/*
102492** Add a new element to the pAggInfo->aFunc[] array. Return the index of
102493** the new element. Return a negative number if malloc fails.
102494*/
102495static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
102496 int i;
102497 pInfo->aFunc = sqlite3ArrayAllocate(
102498 db,
102499 pInfo->aFunc,
102500 sizeof(pInfo->aFunc[0]),
102501 &pInfo->nFunc,
102502 &i
102503 );
102504 return i;
102505}
102506
102507/*
102508** This is the xExprCallback for a tree walker. It is used to
102509** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
102510** for additional information.
102511*/
102512static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
102513 int i;
102514 NameContext *pNC = pWalker->u.pNC;
102515 Parse *pParse = pNC->pParse;
102516 SrcList *pSrcList = pNC->pSrcList;
102517 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
102518
102519 assert( pNC->ncFlags & NC_UAggInfo )((void) (0));
102520 switch( pExpr->op ){
102521 case TK_AGG_COLUMN164:
102522 case TK_COLUMN162: {
102523 testcase( pExpr->op==TK_AGG_COLUMN );
102524 testcase( pExpr->op==TK_COLUMN );
102525 /* Check to see if the column is in one of the tables in the FROM
102526 ** clause of the aggregate query */
102527 if( ALWAYS(pSrcList!=0)(pSrcList!=0) ){
102528 struct SrcList_item *pItem = pSrcList->a;
102529 for(i=0; i<pSrcList->nSrc; i++, pItem++){
102530 struct AggInfo_col *pCol;
102531 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
102532 if( pExpr->iTable==pItem->iCursor ){
102533 /* If we reach this point, it means that pExpr refers to a table
102534 ** that is in the FROM clause of the aggregate query.
102535 **
102536 ** Make an entry for the column in pAggInfo->aCol[] if there
102537 ** is not an entry there already.
102538 */
102539 int k;
102540 pCol = pAggInfo->aCol;
102541 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
102542 if( pCol->iTable==pExpr->iTable &&
102543 pCol->iColumn==pExpr->iColumn ){
102544 break;
102545 }
102546 }
102547 if( (k>=pAggInfo->nColumn)
102548 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
102549 ){
102550 pCol = &pAggInfo->aCol[k];
102551 pCol->pTab = pExpr->y.pTab;
102552 pCol->iTable = pExpr->iTable;
102553 pCol->iColumn = pExpr->iColumn;
102554 pCol->iMem = ++pParse->nMem;
102555 pCol->iSorterColumn = -1;
102556 pCol->pExpr = pExpr;
102557 if( pAggInfo->pGroupBy ){
102558 int j, n;
102559 ExprList *pGB = pAggInfo->pGroupBy;
102560 struct ExprList_item *pTerm = pGB->a;
102561 n = pGB->nExpr;
102562 for(j=0; j<n; j++, pTerm++){
102563 Expr *pE = pTerm->pExpr;
102564 if( pE->op==TK_COLUMN162 && pE->iTable==pExpr->iTable &&
102565 pE->iColumn==pExpr->iColumn ){
102566 pCol->iSorterColumn = j;
102567 break;
102568 }
102569 }
102570 }
102571 if( pCol->iSorterColumn<0 ){
102572 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
102573 }
102574 }
102575 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
102576 ** because it was there before or because we just created it).
102577 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
102578 ** pAggInfo->aCol[] entry.
102579 */
102580 ExprSetVVAProperty(pExpr, EP_NoReduce);
102581 pExpr->pAggInfo = pAggInfo;
102582 pExpr->op = TK_AGG_COLUMN164;
102583 pExpr->iAgg = (i16)k;
102584 break;
102585 } /* endif pExpr->iTable==pItem->iCursor */
102586 } /* end loop over pSrcList */
102587 }
102588 return WRC_Prune1;
102589 }
102590 case TK_AGG_FUNCTION163: {
102591 if( (pNC->ncFlags & NC_InAggFunc0x0008)==0
102592 && pWalker->walkerDepth==pExpr->op2
102593 ){
102594 /* Check to see if pExpr is a duplicate of another aggregate
102595 ** function that is already in the pAggInfo structure
102596 */
102597 struct AggInfo_func *pItem = pAggInfo->aFunc;
102598 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
102599 if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){
102600 break;
102601 }
102602 }
102603 if( i>=pAggInfo->nFunc ){
102604 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
102605 */
102606 u8 enc = ENC(pParse->db)((pParse->db)->enc);
102607 i = addAggInfoFunc(pParse->db, pAggInfo);
102608 if( i>=0 ){
102609 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
102610 pItem = &pAggInfo->aFunc[i];
102611 pItem->pExpr = pExpr;
102612 pItem->iMem = ++pParse->nMem;
102613 assert( !ExprHasProperty(pExpr, EP_IntValue) )((void) (0));
102614 pItem->pFunc = sqlite3FindFunction(pParse->db,
102615 pExpr->u.zToken,
102616 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
102617 if( pExpr->flags & EP_Distinct0x000002 ){
102618 pItem->iDistinct = pParse->nTab++;
102619 }else{
102620 pItem->iDistinct = -1;
102621 }
102622 }
102623 }
102624 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
102625 */
102626 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) )((void) (0));
102627 ExprSetVVAProperty(pExpr, EP_NoReduce);
102628 pExpr->iAgg = (i16)i;
102629 pExpr->pAggInfo = pAggInfo;
102630 return WRC_Prune1;
102631 }else{
102632 return WRC_Continue0;
102633 }
102634 }
102635 }
102636 return WRC_Continue0;
102637}
102638static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
102639 UNUSED_PARAMETER(pSelect)(void)(pSelect);
102640 pWalker->walkerDepth++;
102641 return WRC_Continue0;
102642}
102643static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){
102644 UNUSED_PARAMETER(pSelect)(void)(pSelect);
102645 pWalker->walkerDepth--;
102646}
102647
102648/*
102649** Analyze the pExpr expression looking for aggregate functions and
102650** for variables that need to be added to AggInfo object that pNC->pAggInfo
102651** points to. Additional entries are made on the AggInfo object as
102652** necessary.
102653**
102654** This routine should only be called after the expression has been
102655** analyzed by sqlite3ResolveExprNames().
102656*/
102657SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
102658 Walker w;
102659 w.xExprCallback = analyzeAggregate;
102660 w.xSelectCallback = analyzeAggregatesInSelect;
102661 w.xSelectCallback2 = analyzeAggregatesInSelectEnd;
102662 w.walkerDepth = 0;
102663 w.u.pNC = pNC;
102664 w.pParse = 0;
102665 assert( pNC->pSrcList!=0 )((void) (0));
102666 sqlite3WalkExpr(&w, pExpr);
102667}
102668
102669/*
102670** Call sqlite3ExprAnalyzeAggregates() for every expression in an
102671** expression list. Return the number of errors.
102672**
102673** If an error is found, the analysis is cut short.
102674*/
102675SQLITE_PRIVATEstatic void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
102676 struct ExprList_item *pItem;
102677 int i;
102678 if( pList ){
102679 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
102680 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
102681 }
102682 }
102683}
102684
102685/*
102686** Allocate a single new register for use to hold some intermediate result.
102687*/
102688SQLITE_PRIVATEstatic int sqlite3GetTempReg(Parse *pParse){
102689 if( pParse->nTempReg==0 ){
102690 return ++pParse->nMem;
102691 }
102692 return pParse->aTempReg[--pParse->nTempReg];
102693}
102694
102695/*
102696** Deallocate a register, making available for reuse for some other
102697** purpose.
102698*/
102699SQLITE_PRIVATEstatic void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
102700 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg)((int)(sizeof(pParse->aTempReg)/sizeof(pParse->aTempReg
[0])))
){
102701 pParse->aTempReg[pParse->nTempReg++] = iReg;
102702 }
102703}
102704
102705/*
102706** Allocate or deallocate a block of nReg consecutive registers.
102707*/
102708SQLITE_PRIVATEstatic int sqlite3GetTempRange(Parse *pParse, int nReg){
102709 int i, n;
102710 if( nReg==1 ) return sqlite3GetTempReg(pParse);
102711 i = pParse->iRangeReg;
102712 n = pParse->nRangeReg;
102713 if( nReg<=n ){
102714 pParse->iRangeReg += nReg;
102715 pParse->nRangeReg -= nReg;
102716 }else{
102717 i = pParse->nMem+1;
102718 pParse->nMem += nReg;
102719 }
102720 return i;
102721}
102722SQLITE_PRIVATEstatic void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
102723 if( nReg==1 ){
102724 sqlite3ReleaseTempReg(pParse, iReg);
102725 return;
102726 }
102727 if( nReg>pParse->nRangeReg ){
102728 pParse->nRangeReg = nReg;
102729 pParse->iRangeReg = iReg;
102730 }
102731}
102732
102733/*
102734** Mark all temporary registers as being unavailable for reuse.
102735*/
102736SQLITE_PRIVATEstatic void sqlite3ClearTempRegCache(Parse *pParse){
102737 pParse->nTempReg = 0;
102738 pParse->nRangeReg = 0;
102739}
102740
102741/*
102742** Validate that no temporary register falls within the range of
102743** iFirst..iLast, inclusive. This routine is only call from within assert()
102744** statements.
102745*/
102746#ifdef SQLITE_DEBUG
102747SQLITE_PRIVATEstatic int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
102748 int i;
102749 if( pParse->nRangeReg>0
102750 && pParse->iRangeReg+pParse->nRangeReg > iFirst
102751 && pParse->iRangeReg <= iLast
102752 ){
102753 return 0;
102754 }
102755 for(i=0; i<pParse->nTempReg; i++){
102756 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
102757 return 0;
102758 }
102759 }
102760 return 1;
102761}
102762#endif /* SQLITE_DEBUG */
102763
102764/************** End of expr.c ************************************************/
102765/************** Begin file alter.c *******************************************/
102766/*
102767** 2005 February 15
102768**
102769** The author disclaims copyright to this source code. In place of
102770** a legal notice, here is a blessing:
102771**
102772** May you do good and not evil.
102773** May you find forgiveness for yourself and forgive others.
102774** May you share freely, never taking more than you give.
102775**
102776*************************************************************************
102777** This file contains C code routines that used to generate VDBE code
102778** that implements the ALTER TABLE command.
102779*/
102780/* #include "sqliteInt.h" */
102781
102782/*
102783** The code in this file only exists if we are not omitting the
102784** ALTER TABLE logic from the build.
102785*/
102786#ifndef SQLITE_OMIT_ALTERTABLE
102787
102788/*
102789** Parameter zName is the name of a table that is about to be altered
102790** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
102791** If the table is a system table, this function leaves an error message
102792** in pParse->zErr (system tables may not be altered) and returns non-zero.
102793**
102794** Or, if zName is not a system table, zero is returned.
102795*/
102796static int isAlterableTable(Parse *pParse, Table *pTab){
102797 if( 0==sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)
102798#ifndef SQLITE_OMIT_VIRTUALTABLE
102799 || ( (pTab->tabFlags & TF_Shadow0x0400)
102800 && (pParse->db->flags & SQLITE_Defensive0x10000000)
102801 && pParse->db->nVdbeExec==0
102802 )
102803#endif
102804 ){
102805 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
102806 return 1;
102807 }
102808 return 0;
102809}
102810
102811/*
102812** Generate code to verify that the schemas of database zDb and, if
102813** bTemp is not true, database "temp", can still be parsed. This is
102814** called at the end of the generation of an ALTER TABLE ... RENAME ...
102815** statement to ensure that the operation has not rendered any schema
102816** objects unusable.
102817*/
102818static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
102819 sqlite3NestedParse(pParse,
102820 "SELECT 1 "
102821 "FROM \"%w\".%s "
102822 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
102823 " AND sql NOT LIKE 'create virtual%%'"
102824 " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
102825 zDb, MASTER_NAME"sqlite_master",
102826 zDb, bTemp
102827 );
102828
102829 if( bTemp==0 ){
102830 sqlite3NestedParse(pParse,
102831 "SELECT 1 "
102832 "FROM temp.%s "
102833 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
102834 " AND sql NOT LIKE 'create virtual%%'"
102835 " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
102836 MASTER_NAME"sqlite_master", zDb
102837 );
102838 }
102839}
102840
102841/*
102842** Generate code to reload the schema for database iDb. And, if iDb!=1, for
102843** the temp database as well.
102844*/
102845static void renameReloadSchema(Parse *pParse, int iDb){
102846 Vdbe *v = pParse->pVdbe;
102847 if( v ){
102848 sqlite3ChangeCookie(pParse, iDb);
102849 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0);
102850 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0);
102851 }
102852}
102853
102854/*
102855** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
102856** command.
102857*/
102858SQLITE_PRIVATEstatic void sqlite3AlterRenameTable(
102859 Parse *pParse, /* Parser context. */
102860 SrcList *pSrc, /* The table to rename. */
102861 Token *pName /* The new table name. */
102862){
102863 int iDb; /* Database that contains the table */
102864 char *zDb; /* Name of database iDb */
102865 Table *pTab; /* Table being renamed */
102866 char *zName = 0; /* NULL-terminated version of pName */
102867 sqlite3 *db = pParse->db; /* Database connection */
102868 int nTabName; /* Number of UTF-8 characters in zTabName */
102869 const char *zTabName; /* Original name of the table */
102870 Vdbe *v;
102871 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
102872 u32 savedDbFlags; /* Saved value of db->mDbFlags */
102873
102874 savedDbFlags = db->mDbFlags;
102875 if( NEVER(db->mallocFailed)(db->mallocFailed) ) goto exit_rename_table;
102876 assert( pSrc->nSrc==1 )((void) (0));
102877 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
102878
102879 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
102880 if( !pTab ) goto exit_rename_table;
102881 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
102882 zDb = db->aDb[iDb].zDbSName;
102883 db->mDbFlags |= DBFLAG_PreferBuiltin0x0002;
102884
102885 /* Get a NULL terminated version of the new table name. */
102886 zName = sqlite3NameFromToken(db, pName);
102887 if( !zName ) goto exit_rename_table;
102888
102889 /* Check that a table or index named 'zName' does not already exist
102890 ** in database iDb. If so, this is an error.
102891 */
102892 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
102893 sqlite3ErrorMsg(pParse,
102894 "there is already another table or index with this name: %s", zName);
102895 goto exit_rename_table;
102896 }
102897
102898 /* Make sure it is not a system table being altered, or a reserved name
102899 ** that the table is being renamed to.
102900 */
102901 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ){
102902 goto exit_rename_table;
102903 }
102904 if( SQLITE_OK0!=sqlite3CheckObjectName(pParse, zName) ){ goto
102905 exit_rename_table;
102906 }
102907
102908#ifndef SQLITE_OMIT_VIEW
102909 if( pTab->pSelect ){
102910 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
102911 goto exit_rename_table;
102912 }
102913#endif
102914
102915#ifndef SQLITE_OMIT_AUTHORIZATION
102916 /* Invoke the authorization callback. */
102917 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
102918 goto exit_rename_table;
102919 }
102920#endif
102921
102922#ifndef SQLITE_OMIT_VIRTUALTABLE
102923 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
102924 goto exit_rename_table;
102925 }
102926 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
102927 pVTab = sqlite3GetVTable(db, pTab);
102928 if( pVTab->pVtab->pModule->xRename==0 ){
102929 pVTab = 0;
102930 }
102931 }
102932#endif
102933
102934 /* Begin a transaction for database iDb. Then modify the schema cookie
102935 ** (since the ALTER TABLE modifies the schema). Call sqlite3MayAbort(),
102936 ** as the scalar functions (e.g. sqlite_rename_table()) invoked by the
102937 ** nested SQL may raise an exception. */
102938 v = sqlite3GetVdbe(pParse);
102939 if( v==0 ){
102940 goto exit_rename_table;
102941 }
102942 sqlite3MayAbort(pParse);
102943
102944 /* figure out how many UTF-8 characters are in zName */
102945 zTabName = pTab->zName;
102946 nTabName = sqlite3Utf8CharLen(zTabName, -1);
102947
102948 /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
102949 ** the schema to use the new table name. */
102950 sqlite3NestedParse(pParse,
102951 "UPDATE \"%w\".%s SET "
102952 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
102953 "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
102954 "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
102955 , zDb, MASTER_NAME"sqlite_master", zDb, zTabName, zName, (iDb==1), zTabName
102956 );
102957
102958 /* Update the tbl_name and name columns of the sqlite_master table
102959 ** as required. */
102960 sqlite3NestedParse(pParse,
102961 "UPDATE %Q.%s SET "
102962 "tbl_name = %Q, "
102963 "name = CASE "
102964 "WHEN type='table' THEN %Q "
102965 "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
102966 " AND type='index' THEN "
102967 "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
102968 "ELSE name END "
102969 "WHERE tbl_name=%Q COLLATE nocase AND "
102970 "(type='table' OR type='index' OR type='trigger');",
102971 zDb, MASTER_NAME"sqlite_master",
102972 zName, zName, zName,
102973 nTabName, zTabName
102974 );
102975
102976#ifndef SQLITE_OMIT_AUTOINCREMENT
102977 /* If the sqlite_sequence table exists in this database, then update
102978 ** it with the new table name.
102979 */
102980 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
102981 sqlite3NestedParse(pParse,
102982 "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
102983 zDb, zName, pTab->zName);
102984 }
102985#endif
102986
102987 /* If the table being renamed is not itself part of the temp database,
102988 ** edit view and trigger definitions within the temp database
102989 ** as required. */
102990 if( iDb!=1 ){
102991 sqlite3NestedParse(pParse,
102992 "UPDATE sqlite_temp_master SET "
102993 "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, 1), "
102994 "tbl_name = "
102995 "CASE WHEN tbl_name=%Q COLLATE nocase AND "
102996 " sqlite_rename_test(%Q, sql, type, name, 1) "
102997 "THEN %Q ELSE tbl_name END "
102998 "WHERE type IN ('view', 'trigger')"
102999 , zDb, zTabName, zName, zTabName, zDb, zName);
103000 }
103001
103002 /* If this is a virtual table, invoke the xRename() function if
103003 ** one is defined. The xRename() callback will modify the names
103004 ** of any resources used by the v-table implementation (including other
103005 ** SQLite tables) that are identified by the name of the virtual table.
103006 */
103007#ifndef SQLITE_OMIT_VIRTUALTABLE
103008 if( pVTab ){
103009 int i = ++pParse->nMem;
103010 sqlite3VdbeLoadString(v, i, zName);
103011 sqlite3VdbeAddOp4(v, OP_VRename165, i, 0, 0,(const char*)pVTab, P4_VTAB(-12));
103012 }
103013#endif
103014
103015 renameReloadSchema(pParse, iDb);
103016 renameTestSchema(pParse, zDb, iDb==1);
103017
103018exit_rename_table:
103019 sqlite3SrcListDelete(db, pSrc);
103020 sqlite3DbFree(db, zName);
103021 db->mDbFlags = savedDbFlags;
103022}
103023
103024/*
103025** This function is called after an "ALTER TABLE ... ADD" statement
103026** has been parsed. Argument pColDef contains the text of the new
103027** column definition.
103028**
103029** The Table structure pParse->pNewTable was extended to include
103030** the new column during parsing.
103031*/
103032SQLITE_PRIVATEstatic void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
103033 Table *pNew; /* Copy of pParse->pNewTable */
103034 Table *pTab; /* Table being altered */
103035 int iDb; /* Database number */
103036 const char *zDb; /* Database name */
103037 const char *zTab; /* Table name */
103038 char *zCol; /* Null-terminated column definition */
103039 Column *pCol; /* The new column */
103040 Expr *pDflt; /* Default value for the new column */
103041 sqlite3 *db; /* The database connection; */
103042 Vdbe *v; /* The prepared statement under construction */
103043 int r1; /* Temporary registers */
103044
103045 db = pParse->db;
103046 if( pParse->nErr || db->mallocFailed ) return;
103047 pNew = pParse->pNewTable;
103048 assert( pNew )((void) (0));
103049
103050 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
103051 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
103052 zDb = db->aDb[iDb].zDbSName;
103053 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
103054 pCol = &pNew->aCol[pNew->nCol-1];
103055 pDflt = pCol->pDflt;
103056 pTab = sqlite3FindTable(db, zTab, zDb);
103057 assert( pTab )((void) (0));
103058
103059#ifndef SQLITE_OMIT_AUTHORIZATION
103060 /* Invoke the authorization callback. */
103061 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
103062 return;
103063 }
103064#endif
103065
103066 /* If the default value for the new column was specified with a
103067 ** literal NULL, then set pDflt to 0. This simplifies checking
103068 ** for an SQL NULL default below.
103069 */
103070 assert( pDflt==0 || pDflt->op==TK_SPAN )((void) (0));
103071 if( pDflt && pDflt->pLeft->op==TK_NULL114 ){
103072 pDflt = 0;
103073 }
103074
103075 /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
103076 ** If there is a NOT NULL constraint, then the default value for the
103077 ** column must not be NULL.
103078 */
103079 if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
103080 sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
103081 return;
103082 }
103083 if( pNew->pIndex ){
103084 sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
103085 return;
103086 }
103087 if( (db->flags&SQLITE_ForeignKeys0x00004000) && pNew->pFKey && pDflt ){
103088 sqlite3ErrorMsg(pParse,
103089 "Cannot add a REFERENCES column with non-NULL default value");
103090 return;
103091 }
103092 if( pCol->notNull && !pDflt ){
103093 sqlite3ErrorMsg(pParse,
103094 "Cannot add a NOT NULL column with default value NULL");
103095 return;
103096 }
103097
103098 /* Ensure the default expression is something that sqlite3ValueFromExpr()
103099 ** can handle (i.e. not CURRENT_TIME etc.)
103100 */
103101 if( pDflt ){
103102 sqlite3_value *pVal = 0;
103103 int rc;
103104 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF81, SQLITE_AFF_BLOB'A', &pVal);
103105 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM )((void) (0));
103106 if( rc!=SQLITE_OK0 ){
103107 assert( db->mallocFailed == 1 )((void) (0));
103108 return;
103109 }
103110 if( !pVal ){
103111 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
103112 return;
103113 }
103114 sqlite3ValueFree(pVal);
103115 }
103116
103117 /* Modify the CREATE TABLE statement. */
103118 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
103119 if( zCol ){
103120 char *zEnd = &zCol[pColDef->n-1];
103121 u32 savedDbFlags = db->mDbFlags;
103122 while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)(sqlite3CtypeMap[(unsigned char)(*zEnd)]&0x01)) ){
103123 *zEnd-- = '\0';
103124 }
103125 db->mDbFlags |= DBFLAG_PreferBuiltin0x0002;
103126 sqlite3NestedParse(pParse,
103127 "UPDATE \"%w\".%s SET "
103128 "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
103129 "WHERE type = 'table' AND name = %Q",
103130 zDb, MASTER_NAME"sqlite_master", pNew->addColOffset, zCol, pNew->addColOffset+1,
103131 zTab
103132 );
103133 sqlite3DbFree(db, zCol);
103134 db->mDbFlags = savedDbFlags;
103135 }
103136
103137 /* Make sure the schema version is at least 3. But do not upgrade
103138 ** from less than 3 to 4, as that will corrupt any preexisting DESC
103139 ** index.
103140 */
103141 v = sqlite3GetVdbe(pParse);
103142 if( v ){
103143 r1 = sqlite3GetTempReg(pParse);
103144 sqlite3VdbeAddOp3(v, OP_ReadCookie94, iDb, r1, BTREE_FILE_FORMAT2);
103145 sqlite3VdbeUsesBtree(v, iDb);
103146 sqlite3VdbeAddOp2(v, OP_AddImm83, r1, -2);
103147 sqlite3VdbeAddOp2(v, OP_IfPos47, r1, sqlite3VdbeCurrentAddr(v)+2);
103148 VdbeCoverage(v);
103149 sqlite3VdbeAddOp3(v, OP_SetCookie95, iDb, BTREE_FILE_FORMAT2, 3);
103150 sqlite3ReleaseTempReg(pParse, r1);
103151 }
103152
103153 /* Reload the table definition */
103154 renameReloadSchema(pParse, iDb);
103155}
103156
103157/*
103158** This function is called by the parser after the table-name in
103159** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
103160** pSrc is the full-name of the table being altered.
103161**
103162** This routine makes a (partial) copy of the Table structure
103163** for the table being altered and sets Parse.pNewTable to point
103164** to it. Routines called by the parser as the column definition
103165** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
103166** the copy. The copy of the Table structure is deleted by tokenize.c
103167** after parsing is finished.
103168**
103169** Routine sqlite3AlterFinishAddColumn() will be called to complete
103170** coding the "ALTER TABLE ... ADD" statement.
103171*/
103172SQLITE_PRIVATEstatic void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
103173 Table *pNew;
103174 Table *pTab;
103175 int iDb;
103176 int i;
103177 int nAlloc;
103178 sqlite3 *db = pParse->db;
103179
103180 /* Look up the table being altered. */
103181 assert( pParse->pNewTable==0 )((void) (0));
103182 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
103183 if( db->mallocFailed ) goto exit_begin_add_column;
103184 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
103185 if( !pTab ) goto exit_begin_add_column;
103186
103187#ifndef SQLITE_OMIT_VIRTUALTABLE
103188 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
103189 sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
103190 goto exit_begin_add_column;
103191 }
103192#endif
103193
103194 /* Make sure this is not an attempt to ALTER a view. */
103195 if( pTab->pSelect ){
103196 sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
103197 goto exit_begin_add_column;
103198 }
103199 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ){
103200 goto exit_begin_add_column;
103201 }
103202
103203 assert( pTab->addColOffset>0 )((void) (0));
103204 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
103205
103206 /* Put a copy of the Table struct in Parse.pNewTable for the
103207 ** sqlite3AddColumn() function and friends to modify. But modify
103208 ** the name by adding an "sqlite_altertab_" prefix. By adding this
103209 ** prefix, we insure that the name will not collide with an existing
103210 ** table because user table are not allowed to have the "sqlite_"
103211 ** prefix on their name.
103212 */
103213 pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
103214 if( !pNew ) goto exit_begin_add_column;
103215 pParse->pNewTable = pNew;
103216 pNew->nTabRef = 1;
103217 pNew->nCol = pTab->nCol;
103218 assert( pNew->nCol>0 )((void) (0));
103219 nAlloc = (((pNew->nCol-1)/8)*8)+8;
103220 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 )((void) (0));
103221 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
103222 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
103223 if( !pNew->aCol || !pNew->zName ){
103224 assert( db->mallocFailed )((void) (0));
103225 goto exit_begin_add_column;
103226 }
103227 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
103228 for(i=0; i<pNew->nCol; i++){
103229 Column *pCol = &pNew->aCol[i];
103230 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
103231 pCol->zColl = 0;
103232 pCol->pDflt = 0;
103233 }
103234 pNew->pSchema = db->aDb[iDb].pSchema;
103235 pNew->addColOffset = pTab->addColOffset;
103236 pNew->nTabRef = 1;
103237
103238exit_begin_add_column:
103239 sqlite3SrcListDelete(db, pSrc);
103240 return;
103241}
103242
103243/*
103244** Parameter pTab is the subject of an ALTER TABLE ... RENAME COLUMN
103245** command. This function checks if the table is a view or virtual
103246** table (columns of views or virtual tables may not be renamed). If so,
103247** it loads an error message into pParse and returns non-zero.
103248**
103249** Or, if pTab is not a view or virtual table, zero is returned.
103250*/
103251#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
103252static int isRealTable(Parse *pParse, Table *pTab){
103253 const char *zType = 0;
103254#ifndef SQLITE_OMIT_VIEW
103255 if( pTab->pSelect ){
103256 zType = "view";
103257 }
103258#endif
103259#ifndef SQLITE_OMIT_VIRTUALTABLE
103260 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
103261 zType = "virtual table";
103262 }
103263#endif
103264 if( zType ){
103265 sqlite3ErrorMsg(
103266 pParse, "cannot rename columns of %s \"%s\"", zType, pTab->zName
103267 );
103268 return 1;
103269 }
103270 return 0;
103271}
103272#else /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
103273# define isRealTable(x,y) (0)
103274#endif
103275
103276/*
103277** Handles the following parser reduction:
103278**
103279** cmd ::= ALTER TABLE pSrc RENAME COLUMN pOld TO pNew
103280*/
103281SQLITE_PRIVATEstatic void sqlite3AlterRenameColumn(
103282 Parse *pParse, /* Parsing context */
103283 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
103284 Token *pOld, /* Name of column being changed */
103285 Token *pNew /* New column name */
103286){
103287 sqlite3 *db = pParse->db; /* Database connection */
103288 Table *pTab; /* Table being updated */
103289 int iCol; /* Index of column being renamed */
103290 char *zOld = 0; /* Old column name */
103291 char *zNew = 0; /* New column name */
103292 const char *zDb; /* Name of schema containing the table */
103293 int iSchema; /* Index of the schema */
103294 int bQuote; /* True to quote the new name */
103295
103296 /* Locate the table to be altered */
103297 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
103298 if( !pTab ) goto exit_rename_column;
103299
103300 /* Cannot alter a system table */
103301 if( SQLITE_OK0!=isAlterableTable(pParse, pTab) ) goto exit_rename_column;
103302 if( SQLITE_OK0!=isRealTable(pParse, pTab) ) goto exit_rename_column;
103303
103304 /* Which schema holds the table to be altered */
103305 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
103306 assert( iSchema>=0 )((void) (0));
103307 zDb = db->aDb[iSchema].zDbSName;
103308
103309#ifndef SQLITE_OMIT_AUTHORIZATION
103310 /* Invoke the authorization callback. */
103311 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE26, zDb, pTab->zName, 0) ){
103312 goto exit_rename_column;
103313 }
103314#endif
103315
103316 /* Make sure the old name really is a column name in the table to be
103317 ** altered. Set iCol to be the index of the column being renamed */
103318 zOld = sqlite3NameFromToken(db, pOld);
103319 if( !zOld ) goto exit_rename_column;
103320 for(iCol=0; iCol<pTab->nCol; iCol++){
103321 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
103322 }
103323 if( iCol==pTab->nCol ){
103324 sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
103325 goto exit_rename_column;
103326 }
103327
103328 /* Do the rename operation using a recursive UPDATE statement that
103329 ** uses the sqlite_rename_column() SQL function to compute the new
103330 ** CREATE statement text for the sqlite_master table.
103331 */
103332 sqlite3MayAbort(pParse);
103333 zNew = sqlite3NameFromToken(db, pNew);
103334 if( !zNew ) goto exit_rename_column;
103335 assert( pNew->n>0 )((void) (0));
103336 bQuote = sqlite3Isquote(pNew->z[0])(sqlite3CtypeMap[(unsigned char)(pNew->z[0])]&0x80);
103337 sqlite3NestedParse(pParse,
103338 "UPDATE \"%w\".%s SET "
103339 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
103340 "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
103341 " AND (type != 'index' OR tbl_name = %Q)"
103342 " AND sql NOT LIKE 'create virtual%%'",
103343 zDb, MASTER_NAME"sqlite_master",
103344 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
103345 pTab->zName
103346 );
103347
103348 sqlite3NestedParse(pParse,
103349 "UPDATE temp.%s SET "
103350 "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, 1) "
103351 "WHERE type IN ('trigger', 'view')",
103352 MASTER_NAME"sqlite_master",
103353 zDb, pTab->zName, iCol, zNew, bQuote
103354 );
103355
103356 /* Drop and reload the database schema. */
103357 renameReloadSchema(pParse, iSchema);
103358 renameTestSchema(pParse, zDb, iSchema==1);
103359
103360 exit_rename_column:
103361 sqlite3SrcListDelete(db, pSrc);
103362 sqlite3DbFree(db, zOld);
103363 sqlite3DbFree(db, zNew);
103364 return;
103365}
103366
103367/*
103368** Each RenameToken object maps an element of the parse tree into
103369** the token that generated that element. The parse tree element
103370** might be one of:
103371**
103372** * A pointer to an Expr that represents an ID
103373** * The name of a table column in Column.zName
103374**
103375** A list of RenameToken objects can be constructed during parsing.
103376** Each new object is created by sqlite3RenameTokenMap().
103377** As the parse tree is transformed, the sqlite3RenameTokenRemap()
103378** routine is used to keep the mapping current.
103379**
103380** After the parse finishes, renameTokenFind() routine can be used
103381** to look up the actual token value that created some element in
103382** the parse tree.
103383*/
103384struct RenameToken {
103385 void *p; /* Parse tree element created by token t */
103386 Token t; /* The token that created parse tree element p */
103387 RenameToken *pNext; /* Next is a list of all RenameToken objects */
103388};
103389
103390/*
103391** The context of an ALTER TABLE RENAME COLUMN operation that gets passed
103392** down into the Walker.
103393*/
103394typedef struct RenameCtx RenameCtx;
103395struct RenameCtx {
103396 RenameToken *pList; /* List of tokens to overwrite */
103397 int nList; /* Number of tokens in pList */
103398 int iCol; /* Index of column being renamed */
103399 Table *pTab; /* Table being ALTERed */
103400 const char *zOld; /* Old column name */
103401};
103402
103403#ifdef SQLITE_DEBUG
103404/*
103405** This function is only for debugging. It performs two tasks:
103406**
103407** 1. Checks that pointer pPtr does not already appear in the
103408** rename-token list.
103409**
103410** 2. Dereferences each pointer in the rename-token list.
103411**
103412** The second is most effective when debugging under valgrind or
103413** address-sanitizer or similar. If any of these pointers no longer
103414** point to valid objects, an exception is raised by the memory-checking
103415** tool.
103416**
103417** The point of this is to prevent comparisons of invalid pointer values.
103418** Even though this always seems to work, it is undefined according to the
103419** C standard. Example of undefined comparison:
103420**
103421** sqlite3_free(x);
103422** if( x==y ) ...
103423**
103424** Technically, as x no longer points into a valid object or to the byte
103425** following a valid object, it may not be used in comparison operations.
103426*/
103427static void renameTokenCheckAll(Parse *pParse, void *pPtr){
103428 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
103429 RenameToken *p;
103430 u8 i = 0;
103431 for(p=pParse->pRename; p; p=p->pNext){
103432 if( p->p ){
103433 assert( p->p!=pPtr )((void) (0));
103434 i += *(u8*)(p->p);
103435 }
103436 }
103437 }
103438}
103439#else
103440# define renameTokenCheckAll(x,y)
103441#endif
103442
103443/*
103444** Remember that the parser tree element pPtr was created using
103445** the token pToken.
103446**
103447** In other words, construct a new RenameToken object and add it
103448** to the list of RenameToken objects currently being built up
103449** in pParse->pRename.
103450**
103451** The pPtr argument is returned so that this routine can be used
103452** with tail recursion in tokenExpr() routine, for a small performance
103453** improvement.
103454*/
103455SQLITE_PRIVATEstatic void *sqlite3RenameTokenMap(Parse *pParse, void *pPtr, Token *pToken){
103456 RenameToken *pNew;
103457 assert( pPtr || pParse->db->mallocFailed )((void) (0));
103458 renameTokenCheckAll(pParse, pPtr);
103459 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
103460 if( pNew ){
103461 pNew->p = pPtr;
103462 pNew->t = *pToken;
103463 pNew->pNext = pParse->pRename;
103464 pParse->pRename = pNew;
103465 }
103466
103467 return pPtr;
103468}
103469
103470/*
103471** It is assumed that there is already a RenameToken object associated
103472** with parse tree element pFrom. This function remaps the associated token
103473** to parse tree element pTo.
103474*/
103475SQLITE_PRIVATEstatic void sqlite3RenameTokenRemap(Parse *pParse, void *pTo, void *pFrom){
103476 RenameToken *p;
103477 renameTokenCheckAll(pParse, pTo);
103478 for(p=pParse->pRename; p; p=p->pNext){
103479 if( p->p==pFrom ){
103480 p->p = pTo;
103481 break;
103482 }
103483 }
103484}
103485
103486/*
103487** Walker callback used by sqlite3RenameExprUnmap().
103488*/
103489static int renameUnmapExprCb(Walker *pWalker, Expr *pExpr){
103490 Parse *pParse = pWalker->pParse;
103491 sqlite3RenameTokenRemap(pParse, 0, (void*)pExpr);
103492 return WRC_Continue0;
103493}
103494
103495/*
103496** Walker callback used by sqlite3RenameExprUnmap().
103497*/
103498static int renameUnmapSelectCb(Walker *pWalker, Select *p){
103499 Parse *pParse = pWalker->pParse;
103500 int i;
103501 if( ALWAYS(p->pEList)(p->pEList) ){
103502 ExprList *pList = p->pEList;
103503 for(i=0; i<pList->nExpr; i++){
103504 if( pList->a[i].zName ){
103505 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zName);
103506 }
103507 }
103508 }
103509 if( ALWAYS(p->pSrc)(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
103510 SrcList *pSrc = p->pSrc;
103511 for(i=0; i<pSrc->nSrc; i++){
103512 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
103513 }
103514 }
103515 return WRC_Continue0;
103516}
103517
103518/*
103519** Remove all nodes that are part of expression pExpr from the rename list.
103520*/
103521SQLITE_PRIVATEstatic void sqlite3RenameExprUnmap(Parse *pParse, Expr *pExpr){
103522 Walker sWalker;
103523 memset(&sWalker, 0, sizeof(Walker));
103524 sWalker.pParse = pParse;
103525 sWalker.xExprCallback = renameUnmapExprCb;
103526 sWalker.xSelectCallback = renameUnmapSelectCb;
103527 sqlite3WalkExpr(&sWalker, pExpr);
103528}
103529
103530/*
103531** Remove all nodes that are part of expression-list pEList from the
103532** rename list.
103533*/
103534SQLITE_PRIVATEstatic void sqlite3RenameExprlistUnmap(Parse *pParse, ExprList *pEList){
103535 if( pEList ){
103536 int i;
103537 Walker sWalker;
103538 memset(&sWalker, 0, sizeof(Walker));
103539 sWalker.pParse = pParse;
103540 sWalker.xExprCallback = renameUnmapExprCb;
103541 sqlite3WalkExprList(&sWalker, pEList);
103542 for(i=0; i<pEList->nExpr; i++){
103543 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zName);
103544 }
103545 }
103546}
103547
103548/*
103549** Free the list of RenameToken objects given in the second argument
103550*/
103551static void renameTokenFree(sqlite3 *db, RenameToken *pToken){
103552 RenameToken *pNext;
103553 RenameToken *p;
103554 for(p=pToken; p; p=pNext){
103555 pNext = p->pNext;
103556 sqlite3DbFree(db, p);
103557 }
103558}
103559
103560/*
103561** Search the Parse object passed as the first argument for a RenameToken
103562** object associated with parse tree element pPtr. If found, remove it
103563** from the Parse object and add it to the list maintained by the
103564** RenameCtx object passed as the second argument.
103565*/
103566static void renameTokenFind(Parse *pParse, struct RenameCtx *pCtx, void *pPtr){
103567 RenameToken **pp;
103568 assert( pPtr!=0 )((void) (0));
103569 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
103570 if( (*pp)->p==pPtr ){
103571 RenameToken *pToken = *pp;
103572 *pp = pToken->pNext;
103573 pToken->pNext = pCtx->pList;
103574 pCtx->pList = pToken;
103575 pCtx->nList++;
103576 break;
103577 }
103578 }
103579}
103580
103581/*
103582** Iterate through the Select objects that are part of WITH clauses attached
103583** to select statement pSelect.
103584*/
103585static void renameWalkWith(Walker *pWalker, Select *pSelect){
103586 if( pSelect->pWith ){
103587 int i;
103588 for(i=0; i<pSelect->pWith->nCte; i++){
103589 Select *p = pSelect->pWith->a[i].pSelect;
103590 NameContext sNC;
103591 memset(&sNC, 0, sizeof(sNC));
103592 sNC.pParse = pWalker->pParse;
103593 sqlite3SelectPrep(sNC.pParse, p, &sNC);
103594 sqlite3WalkSelect(pWalker, p);
103595 }
103596 }
103597}
103598
103599/*
103600** This is a Walker select callback. It does nothing. It is only required
103601** because without a dummy callback, sqlite3WalkExpr() and similar do not
103602** descend into sub-select statements.
103603*/
103604static int renameColumnSelectCb(Walker *pWalker, Select *p){
103605 renameWalkWith(pWalker, p);
103606 return WRC_Continue0;
103607}
103608
103609/*
103610** This is a Walker expression callback.
103611**
103612** For every TK_COLUMN node in the expression tree, search to see
103613** if the column being references is the column being renamed by an
103614** ALTER TABLE statement. If it is, then attach its associated
103615** RenameToken object to the list of RenameToken objects being
103616** constructed in RenameCtx object at pWalker->u.pRename.
103617*/
103618static int renameColumnExprCb(Walker *pWalker, Expr *pExpr){
103619 RenameCtx *p = pWalker->u.pRename;
103620 if( pExpr->op==TK_TRIGGER77
103621 && pExpr->iColumn==p->iCol
103622 && pWalker->pParse->pTriggerTab==p->pTab
103623 ){
103624 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
103625 }else if( pExpr->op==TK_COLUMN162
103626 && pExpr->iColumn==p->iCol
103627 && p->pTab==pExpr->y.pTab
103628 ){
103629 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
103630 }
103631 return WRC_Continue0;
103632}
103633
103634/*
103635** The RenameCtx contains a list of tokens that reference a column that
103636** is being renamed by an ALTER TABLE statement. Return the "last"
103637** RenameToken in the RenameCtx and remove that RenameToken from the
103638** RenameContext. "Last" means the last RenameToken encountered when
103639** the input SQL is parsed from left to right. Repeated calls to this routine
103640** return all column name tokens in the order that they are encountered
103641** in the SQL statement.
103642*/
103643static RenameToken *renameColumnTokenNext(RenameCtx *pCtx){
103644 RenameToken *pBest = pCtx->pList;
103645 RenameToken *pToken;
103646 RenameToken **pp;
103647
103648 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
103649 if( pToken->t.z>pBest->t.z ) pBest = pToken;
103650 }
103651 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
103652 *pp = pBest->pNext;
103653
103654 return pBest;
103655}
103656
103657/*
103658** An error occured while parsing or otherwise processing a database
103659** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
103660** ALTER TABLE RENAME COLUMN program. The error message emitted by the
103661** sub-routine is currently stored in pParse->zErrMsg. This function
103662** adds context to the error message and then stores it in pCtx.
103663*/
103664static void renameColumnParseError(
103665 sqlite3_context *pCtx,
103666 int bPost,
103667 sqlite3_value *pType,
103668 sqlite3_value *pObject,
103669 Parse *pParse
103670){
103671 const char *zT = (const char*)sqlite3_value_text(pType);
103672 const char *zN = (const char*)sqlite3_value_text(pObject);
103673 char *zErr;
103674
103675 zErr = sqlite3_mprintf("error in %s %s%s: %s",
103676 zT, zN, (bPost ? " after rename" : ""),
103677 pParse->zErrMsg
103678 );
103679 sqlite3_result_error(pCtx, zErr, -1);
103680 sqlite3_free(zErr);
103681}
103682
103683/*
103684** For each name in the the expression-list pEList (i.e. each
103685** pEList->a[i].zName) that matches the string in zOld, extract the
103686** corresponding rename-token from Parse object pParse and add it
103687** to the RenameCtx pCtx.
103688*/
103689static void renameColumnElistNames(
103690 Parse *pParse,
103691 RenameCtx *pCtx,
103692 ExprList *pEList,
103693 const char *zOld
103694){
103695 if( pEList ){
103696 int i;
103697 for(i=0; i<pEList->nExpr; i++){
103698 char *zName = pEList->a[i].zName;
103699 if( 0==sqlite3_stricmp(zName, zOld) ){
103700 renameTokenFind(pParse, pCtx, (void*)zName);
103701 }
103702 }
103703 }
103704}
103705
103706/*
103707** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
103708** that matches the string in zOld, extract the corresponding rename-token
103709** from Parse object pParse and add it to the RenameCtx pCtx.
103710*/
103711static void renameColumnIdlistNames(
103712 Parse *pParse,
103713 RenameCtx *pCtx,
103714 IdList *pIdList,
103715 const char *zOld
103716){
103717 if( pIdList ){
103718 int i;
103719 for(i=0; i<pIdList->nId; i++){
103720 char *zName = pIdList->a[i].zName;
103721 if( 0==sqlite3_stricmp(zName, zOld) ){
103722 renameTokenFind(pParse, pCtx, (void*)zName);
103723 }
103724 }
103725 }
103726}
103727
103728/*
103729** Parse the SQL statement zSql using Parse object (*p). The Parse object
103730** is initialized by this function before it is used.
103731*/
103732static int renameParseSql(
103733 Parse *p, /* Memory to use for Parse object */
103734 const char *zDb, /* Name of schema SQL belongs to */
103735 int bTable, /* 1 -> RENAME TABLE, 0 -> RENAME COLUMN */
103736 sqlite3 *db, /* Database handle */
103737 const char *zSql, /* SQL to parse */
103738 int bTemp /* True if SQL is from temp schema */
103739){
103740 int rc;
103741 char *zErr = 0;
103742
103743 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
103744
103745 /* Parse the SQL statement passed as the first argument. If no error
103746 ** occurs and the parse does not result in a new table, index or
103747 ** trigger object, the database must be corrupt. */
103748 memset(p, 0, sizeof(Parse));
103749 p->eParseMode = (bTable ? PARSE_MODE_RENAME_TABLE3 : PARSE_MODE_RENAME_COLUMN2);
103750 p->db = db;
103751 p->nQueryLoop = 1;
103752 rc = sqlite3RunParser(p, zSql, &zErr);
103753 assert( p->zErrMsg==0 )((void) (0));
103754 assert( rc!=SQLITE_OK || zErr==0 )((void) (0));
103755 p->zErrMsg = zErr;
103756 if( db->mallocFailed ) rc = SQLITE_NOMEM7;
103757 if( rc==SQLITE_OK0
103758 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
103759 ){
103760 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(103760);
103761 }
103762
103763#ifdef SQLITE_DEBUG
103764 /* Ensure that all mappings in the Parse.pRename list really do map to
103765 ** a part of the input string. */
103766 if( rc==SQLITE_OK0 ){
103767 int nSql = sqlite3Strlen30(zSql);
103768 RenameToken *pToken;
103769 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
103770 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] )((void) (0));
103771 }
103772 }
103773#endif
103774
103775 db->init.iDb = 0;
103776 return rc;
103777}
103778
103779/*
103780** This function edits SQL statement zSql, replacing each token identified
103781** by the linked list pRename with the text of zNew. If argument bQuote is
103782** true, then zNew is always quoted first. If no error occurs, the result
103783** is loaded into context object pCtx as the result.
103784**
103785** Or, if an error occurs (i.e. an OOM condition), an error is left in
103786** pCtx and an SQLite error code returned.
103787*/
103788static int renameEditSql(
103789 sqlite3_context *pCtx, /* Return result here */
103790 RenameCtx *pRename, /* Rename context */
103791 const char *zSql, /* SQL statement to edit */
103792 const char *zNew, /* New token text */
103793 int bQuote /* True to always quote token */
103794){
103795 int nNew = sqlite3Strlen30(zNew);
103796 int nSql = sqlite3Strlen30(zSql);
103797 sqlite3 *db = sqlite3_context_db_handle(pCtx);
103798 int rc = SQLITE_OK0;
103799 char *zQuot;
103800 char *zOut;
103801 int nQuot;
103802
103803 /* Set zQuot to point to a buffer containing a quoted copy of the
103804 ** identifier zNew. If the corresponding identifier in the original
103805 ** ALTER TABLE statement was quoted (bQuote==1), then set zNew to
103806 ** point to zQuot so that all substitutions are made using the
103807 ** quoted version of the new column name. */
103808 zQuot = sqlite3MPrintf(db, "\"%w\"", zNew);
103809 if( zQuot==0 ){
103810 return SQLITE_NOMEM7;
103811 }else{
103812 nQuot = sqlite3Strlen30(zQuot);
103813 }
103814 if( bQuote ){
103815 zNew = zQuot;
103816 nNew = nQuot;
103817 }
103818
103819 /* At this point pRename->pList contains a list of RenameToken objects
103820 ** corresponding to all tokens in the input SQL that must be replaced
103821 ** with the new column name. All that remains is to construct and
103822 ** return the edited SQL string. */
103823 assert( nQuot>=nNew )((void) (0));
103824 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
103825 if( zOut ){
103826 int nOut = nSql;
103827 memcpy(zOut, zSql, nSql);
103828 while( pRename->pList ){
103829 int iOff; /* Offset of token to replace in zOut */
103830 RenameToken *pBest = renameColumnTokenNext(pRename);
103831
103832 u32 nReplace;
103833 const char *zReplace;
103834 if( sqlite3IsIdChar(*pBest->t.z) ){
103835 nReplace = nNew;
103836 zReplace = zNew;
103837 }else{
103838 nReplace = nQuot;
103839 zReplace = zQuot;
103840 }
103841
103842 iOff = pBest->t.z - zSql;
103843 if( pBest->t.n!=nReplace ){
103844 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
103845 nOut - (iOff + pBest->t.n)
103846 );
103847 nOut += nReplace - pBest->t.n;
103848 zOut[nOut] = '\0';
103849 }
103850 memcpy(&zOut[iOff], zReplace, nReplace);
103851 sqlite3DbFree(db, pBest);
103852 }
103853
103854 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
103855 sqlite3DbFree(db, zOut);
103856 }else{
103857 rc = SQLITE_NOMEM7;
103858 }
103859
103860 sqlite3_free(zQuot);
103861 return rc;
103862}
103863
103864/*
103865** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
103866** it was read from the schema of database zDb. Return SQLITE_OK if
103867** successful. Otherwise, return an SQLite error code and leave an error
103868** message in the Parse object.
103869*/
103870static int renameResolveTrigger(Parse *pParse, const char *zDb){
103871 sqlite3 *db = pParse->db;
103872 Trigger *pNew = pParse->pNewTrigger;
103873 TriggerStep *pStep;
103874 NameContext sNC;
103875 int rc = SQLITE_OK0;
103876
103877 memset(&sNC, 0, sizeof(sNC));
103878 sNC.pParse = pParse;
103879 assert( pNew->pTabSchema )((void) (0));
103880 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
103881 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
103882 );
103883 pParse->eTriggerOp = pNew->op;
103884 /* ALWAYS() because if the table of the trigger does not exist, the
103885 ** error would have been hit before this point */
103886 if( ALWAYS(pParse->pTriggerTab)(pParse->pTriggerTab) ){
103887 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
103888 }
103889
103890 /* Resolve symbols in WHEN clause */
103891 if( rc==SQLITE_OK0 && pNew->pWhen ){
103892 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
103893 }
103894
103895 for(pStep=pNew->step_list; rc==SQLITE_OK0 && pStep; pStep=pStep->pNext){
103896 if( pStep->pSelect ){
103897 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
103898 if( pParse->nErr ) rc = pParse->rc;
103899 }
103900 if( rc==SQLITE_OK0 && pStep->zTarget ){
103901 Table *pTarget = sqlite3LocateTable(pParse, 0, pStep->zTarget, zDb);
103902 if( pTarget==0 ){
103903 rc = SQLITE_ERROR1;
103904 }else if( SQLITE_OK0==(rc = sqlite3ViewGetColumnNames(pParse, pTarget)) ){
103905 SrcList sSrc;
103906 memset(&sSrc, 0, sizeof(sSrc));
103907 sSrc.nSrc = 1;
103908 sSrc.a[0].zName = pStep->zTarget;
103909 sSrc.a[0].pTab = pTarget;
103910 sNC.pSrcList = &sSrc;
103911 if( pStep->pWhere ){
103912 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
103913 }
103914 if( rc==SQLITE_OK0 ){
103915 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
103916 }
103917 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) )((void) (0));
103918 if( pStep->pUpsert ){
103919 Upsert *pUpsert = pStep->pUpsert;
103920 assert( rc==SQLITE_OK )((void) (0));
103921 pUpsert->pUpsertSrc = &sSrc;
103922 sNC.uNC.pUpsert = pUpsert;
103923 sNC.ncFlags = NC_UUpsert0x0200;
103924 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
103925 if( rc==SQLITE_OK0 ){
103926 ExprList *pUpsertSet = pUpsert->pUpsertSet;
103927 rc = sqlite3ResolveExprListNames(&sNC, pUpsertSet);
103928 }
103929 if( rc==SQLITE_OK0 ){
103930 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
103931 }
103932 if( rc==SQLITE_OK0 ){
103933 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
103934 }
103935 sNC.ncFlags = 0;
103936 }
103937 sNC.pSrcList = 0;
103938 }
103939 }
103940 }
103941 return rc;
103942}
103943
103944/*
103945** Invoke sqlite3WalkExpr() or sqlite3WalkSelect() on all Select or Expr
103946** objects that are part of the trigger passed as the second argument.
103947*/
103948static void renameWalkTrigger(Walker *pWalker, Trigger *pTrigger){
103949 TriggerStep *pStep;
103950
103951 /* Find tokens to edit in WHEN clause */
103952 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
103953
103954 /* Find tokens to edit in trigger steps */
103955 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
103956 sqlite3WalkSelect(pWalker, pStep->pSelect);
103957 sqlite3WalkExpr(pWalker, pStep->pWhere);
103958 sqlite3WalkExprList(pWalker, pStep->pExprList);
103959 if( pStep->pUpsert ){
103960 Upsert *pUpsert = pStep->pUpsert;
103961 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
103962 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
103963 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
103964 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
103965 }
103966 }
103967}
103968
103969/*
103970** Free the contents of Parse object (*pParse). Do not free the memory
103971** occupied by the Parse object itself.
103972*/
103973static void renameParseCleanup(Parse *pParse){
103974 sqlite3 *db = pParse->db;
103975 Index *pIdx;
103976 if( pParse->pVdbe ){
103977 sqlite3VdbeFinalize(pParse->pVdbe);
103978 }
103979 sqlite3DeleteTable(db, pParse->pNewTable);
103980 while( (pIdx = pParse->pNewIndex)!=0 ){
103981 pParse->pNewIndex = pIdx->pNext;
103982 sqlite3FreeIndex(db, pIdx);
103983 }
103984 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
103985 sqlite3DbFree(db, pParse->zErrMsg);
103986 renameTokenFree(db, pParse->pRename);
103987 sqlite3ParserReset(pParse);
103988}
103989
103990/*
103991** SQL function:
103992**
103993** sqlite_rename_column(zSql, iCol, bQuote, zNew, zTable, zOld)
103994**
103995** 0. zSql: SQL statement to rewrite
103996** 1. type: Type of object ("table", "view" etc.)
103997** 2. object: Name of object
103998** 3. Database: Database name (e.g. "main")
103999** 4. Table: Table name
104000** 5. iCol: Index of column to rename
104001** 6. zNew: New column name
104002** 7. bQuote: Non-zero if the new column name should be quoted.
104003** 8. bTemp: True if zSql comes from temp schema
104004**
104005** Do a column rename operation on the CREATE statement given in zSql.
104006** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
104007** into zNew. The name should be quoted if bQuote is true.
104008**
104009** This function is used internally by the ALTER TABLE RENAME COLUMN command.
104010** It is only accessible to SQL created using sqlite3NestedParse(). It is
104011** not reachable from ordinary SQL passed into sqlite3_prepare().
104012*/
104013static void renameColumnFunc(
104014 sqlite3_context *context,
104015 int NotUsed,
104016 sqlite3_value **argv
104017){
104018 sqlite3 *db = sqlite3_context_db_handle(context);
104019 RenameCtx sCtx;
104020 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
104021 const char *zDb = (const char*)sqlite3_value_text(argv[3]);
104022 const char *zTable = (const char*)sqlite3_value_text(argv[4]);
104023 int iCol = sqlite3_value_int(argv[5]);
104024 const char *zNew = (const char*)sqlite3_value_text(argv[6]);
104025 int bQuote = sqlite3_value_int(argv[7]);
104026 int bTemp = sqlite3_value_int(argv[8]);
104027 const char *zOld;
104028 int rc;
104029 Parse sParse;
104030 Walker sWalker;
104031 Index *pIdx;
104032 int i;
104033 Table *pTab;
104034#ifndef SQLITE_OMIT_AUTHORIZATION
104035 sqlite3_xauth xAuth = db->xAuth;
104036#endif
104037
104038 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
104039 if( zSql==0 ) return;
104040 if( zTable==0 ) return;
104041 if( zNew==0 ) return;
104042 if( iCol<0 ) return;
104043 sqlite3BtreeEnterAll(db);
104044 pTab = sqlite3FindTable(db, zTable, zDb);
104045 if( pTab==0 || iCol>=pTab->nCol ){
104046 sqlite3BtreeLeaveAll(db);
104047 return;
104048 }
104049 zOld = pTab->aCol[iCol].zName;
104050 memset(&sCtx, 0, sizeof(sCtx));
104051 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
104052
104053#ifndef SQLITE_OMIT_AUTHORIZATION
104054 db->xAuth = 0;
104055#endif
104056 rc = renameParseSql(&sParse, zDb, 0, db, zSql, bTemp);
104057
104058 /* Find tokens that need to be replaced. */
104059 memset(&sWalker, 0, sizeof(Walker));
104060 sWalker.pParse = &sParse;
104061 sWalker.xExprCallback = renameColumnExprCb;
104062 sWalker.xSelectCallback = renameColumnSelectCb;
104063 sWalker.u.pRename = &sCtx;
104064
104065 sCtx.pTab = pTab;
104066 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
104067 if( sParse.pNewTable ){
104068 Select *pSelect = sParse.pNewTable->pSelect;
104069 if( pSelect ){
104070 sParse.rc = SQLITE_OK0;
104071 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, 0);
104072 rc = (db->mallocFailed ? SQLITE_NOMEM7 : sParse.rc);
104073 if( rc==SQLITE_OK0 ){
104074 sqlite3WalkSelect(&sWalker, pSelect);
104075 }
104076 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
104077 }else{
104078 /* A regular table */
104079 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
104080 FKey *pFKey;
104081 assert( sParse.pNewTable->pSelect==0 )((void) (0));
104082 sCtx.pTab = sParse.pNewTable;
104083 if( bFKOnly==0 ){
104084 renameTokenFind(
104085 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
104086 );
104087 if( sCtx.iCol<0 ){
104088 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
104089 }
104090 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
104091 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
104092 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
104093 }
104094 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
104095 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
104096 }
104097 }
104098
104099 for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
104100 for(i=0; i<pFKey->nCol; i++){
104101 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
104102 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
104103 }
104104 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
104105 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
104106 ){
104107 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
104108 }
104109 }
104110 }
104111 }
104112 }else if( sParse.pNewIndex ){
104113 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
104114 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
104115 }else{
104116 /* A trigger */
104117 TriggerStep *pStep;
104118 rc = renameResolveTrigger(&sParse, (bTemp ? 0 : zDb));
104119 if( rc!=SQLITE_OK0 ) goto renameColumnFunc_done;
104120
104121 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
104122 if( pStep->zTarget ){
104123 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
104124 if( pTarget==pTab ){
104125 if( pStep->pUpsert ){
104126 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
104127 renameColumnElistNames(&sParse, &sCtx, pUpsertSet, zOld);
104128 }
104129 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
104130 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
104131 }
104132 }
104133 }
104134
104135
104136 /* Find tokens to edit in UPDATE OF clause */
104137 if( sParse.pTriggerTab==pTab ){
104138 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
104139 }
104140
104141 /* Find tokens to edit in various expressions and selects */
104142 renameWalkTrigger(&sWalker, sParse.pNewTrigger);
104143 }
104144
104145 assert( rc==SQLITE_OK )((void) (0));
104146 rc = renameEditSql(context, &sCtx, zSql, zNew, bQuote);
104147
104148renameColumnFunc_done:
104149 if( rc!=SQLITE_OK0 ){
104150 if( sParse.zErrMsg ){
104151 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
104152 }else{
104153 sqlite3_result_error_code(context, rc);
104154 }
104155 }
104156
104157 renameParseCleanup(&sParse);
104158 renameTokenFree(db, sCtx.pList);
104159#ifndef SQLITE_OMIT_AUTHORIZATION
104160 db->xAuth = xAuth;
104161#endif
104162 sqlite3BtreeLeaveAll(db);
104163}
104164
104165/*
104166** Walker expression callback used by "RENAME TABLE".
104167*/
104168static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
104169 RenameCtx *p = pWalker->u.pRename;
104170 if( pExpr->op==TK_COLUMN162 && p->pTab==pExpr->y.pTab ){
104171 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
104172 }
104173 return WRC_Continue0;
104174}
104175
104176/*
104177** Walker select callback used by "RENAME TABLE".
104178*/
104179static int renameTableSelectCb(Walker *pWalker, Select *pSelect){
104180 int i;
104181 RenameCtx *p = pWalker->u.pRename;
104182 SrcList *pSrc = pSelect->pSrc;
104183 if( pSrc==0 ){
104184 assert( pWalker->pParse->db->mallocFailed )((void) (0));
104185 return WRC_Abort2;
104186 }
104187 for(i=0; i<pSrc->nSrc; i++){
104188 struct SrcList_item *pItem = &pSrc->a[i];
104189 if( pItem->pTab==p->pTab ){
104190 renameTokenFind(pWalker->pParse, p, pItem->zName);
104191 }
104192 }
104193 renameWalkWith(pWalker, pSelect);
104194
104195 return WRC_Continue0;
104196}
104197
104198
104199/*
104200** This C function implements an SQL user function that is used by SQL code
104201** generated by the ALTER TABLE ... RENAME command to modify the definition
104202** of any foreign key constraints that use the table being renamed as the
104203** parent table. It is passed three arguments:
104204**
104205** 0: The database containing the table being renamed.
104206** 1. type: Type of object ("table", "view" etc.)
104207** 2. object: Name of object
104208** 3: The complete text of the schema statement being modified,
104209** 4: The old name of the table being renamed, and
104210** 5: The new name of the table being renamed.
104211** 6: True if the schema statement comes from the temp db.
104212**
104213** It returns the new schema statement. For example:
104214**
104215** sqlite_rename_table('main', 'CREATE TABLE t1(a REFERENCES t2)','t2','t3',0)
104216** -> 'CREATE TABLE t1(a REFERENCES t3)'
104217*/
104218static void renameTableFunc(
104219 sqlite3_context *context,
104220 int NotUsed,
104221 sqlite3_value **argv
104222){
104223 sqlite3 *db = sqlite3_context_db_handle(context);
104224 const char *zDb = (const char*)sqlite3_value_text(argv[0]);
104225 const char *zInput = (const char*)sqlite3_value_text(argv[3]);
104226 const char *zOld = (const char*)sqlite3_value_text(argv[4]);
104227 const char *zNew = (const char*)sqlite3_value_text(argv[5]);
104228 int bTemp = sqlite3_value_int(argv[6]);
104229 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
104230
104231 if( zInput && zOld && zNew ){
104232 Parse sParse;
104233 int rc;
104234 int bQuote = 1;
104235 RenameCtx sCtx;
104236 Walker sWalker;
104237
104238#ifndef SQLITE_OMIT_AUTHORIZATION
104239 sqlite3_xauth xAuth = db->xAuth;
104240 db->xAuth = 0;
104241#endif
104242
104243 sqlite3BtreeEnterAll(db);
104244
104245 memset(&sCtx, 0, sizeof(RenameCtx));
104246 sCtx.pTab = sqlite3FindTable(db, zOld, zDb);
104247 memset(&sWalker, 0, sizeof(Walker));
104248 sWalker.pParse = &sParse;
104249 sWalker.xExprCallback = renameTableExprCb;
104250 sWalker.xSelectCallback = renameTableSelectCb;
104251 sWalker.u.pRename = &sCtx;
104252
104253 rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
104254
104255 if( rc==SQLITE_OK0 ){
104256 int isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
104257 if( sParse.pNewTable ){
104258 Table *pTab = sParse.pNewTable;
104259
104260 if( pTab->pSelect ){
104261 if( isLegacy==0 ){
104262 NameContext sNC;
104263 memset(&sNC, 0, sizeof(sNC));
104264 sNC.pParse = &sParse;
104265
104266 sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
104267 if( sParse.nErr ) rc = sParse.rc;
104268 sqlite3WalkSelect(&sWalker, pTab->pSelect);
104269 }
104270 }else{
104271 /* Modify any FK definitions to point to the new table. */
104272#ifndef SQLITE_OMIT_FOREIGN_KEY
104273 if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys0x00004000) ){
104274 FKey *pFKey;
104275 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
104276 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
104277 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
104278 }
104279 }
104280 }
104281#endif
104282
104283 /* If this is the table being altered, fix any table refs in CHECK
104284 ** expressions. Also update the name that appears right after the
104285 ** "CREATE [VIRTUAL] TABLE" bit. */
104286 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
104287 sCtx.pTab = pTab;
104288 if( isLegacy==0 ){
104289 sqlite3WalkExprList(&sWalker, pTab->pCheck);
104290 }
104291 renameTokenFind(&sParse, &sCtx, pTab->zName);
104292 }
104293 }
104294 }
104295
104296 else if( sParse.pNewIndex ){
104297 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
104298 if( isLegacy==0 ){
104299 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
104300 }
104301 }
104302
104303#ifndef SQLITE_OMIT_TRIGGER
104304 else{
104305 Trigger *pTrigger = sParse.pNewTrigger;
104306 TriggerStep *pStep;
104307 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
104308 && sCtx.pTab->pSchema==pTrigger->pTabSchema
104309 ){
104310 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
104311 }
104312
104313 if( isLegacy==0 ){
104314 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
104315 if( rc==SQLITE_OK0 ){
104316 renameWalkTrigger(&sWalker, pTrigger);
104317 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
104318 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
104319 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
104320 }
104321 }
104322 }
104323 }
104324 }
104325#endif
104326 }
104327
104328 if( rc==SQLITE_OK0 ){
104329 rc = renameEditSql(context, &sCtx, zInput, zNew, bQuote);
104330 }
104331 if( rc!=SQLITE_OK0 ){
104332 if( sParse.zErrMsg ){
104333 renameColumnParseError(context, 0, argv[1], argv[2], &sParse);
104334 }else{
104335 sqlite3_result_error_code(context, rc);
104336 }
104337 }
104338
104339 renameParseCleanup(&sParse);
104340 renameTokenFree(db, sCtx.pList);
104341 sqlite3BtreeLeaveAll(db);
104342#ifndef SQLITE_OMIT_AUTHORIZATION
104343 db->xAuth = xAuth;
104344#endif
104345 }
104346
104347 return;
104348}
104349
104350/*
104351** An SQL user function that checks that there are no parse or symbol
104352** resolution problems in a CREATE TRIGGER|TABLE|VIEW|INDEX statement.
104353** After an ALTER TABLE .. RENAME operation is performed and the schema
104354** reloaded, this function is called on each SQL statement in the schema
104355** to ensure that it is still usable.
104356**
104357** 0: Database name ("main", "temp" etc.).
104358** 1: SQL statement.
104359** 2: Object type ("view", "table", "trigger" or "index").
104360** 3: Object name.
104361** 4: True if object is from temp schema.
104362**
104363** Unless it finds an error, this function normally returns NULL. However, it
104364** returns integer value 1 if:
104365**
104366** * the SQL argument creates a trigger, and
104367** * the table that the trigger is attached to is in database zDb.
104368*/
104369static void renameTableTest(
104370 sqlite3_context *context,
104371 int NotUsed,
104372 sqlite3_value **argv
104373){
104374 sqlite3 *db = sqlite3_context_db_handle(context);
104375 char const *zDb = (const char*)sqlite3_value_text(argv[0]);
104376 char const *zInput = (const char*)sqlite3_value_text(argv[1]);
104377 int bTemp = sqlite3_value_int(argv[4]);
104378 int isLegacy = (db->flags & SQLITE_LegacyAlter0x04000000);
104379
104380#ifndef SQLITE_OMIT_AUTHORIZATION
104381 sqlite3_xauth xAuth = db->xAuth;
104382 db->xAuth = 0;
104383#endif
104384
104385 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
104386 if( zDb && zInput ){
104387 int rc;
104388 Parse sParse;
104389 rc = renameParseSql(&sParse, zDb, 1, db, zInput, bTemp);
104390 if( rc==SQLITE_OK0 ){
104391 if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
104392 NameContext sNC;
104393 memset(&sNC, 0, sizeof(sNC));
104394 sNC.pParse = &sParse;
104395 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
104396 if( sParse.nErr ) rc = sParse.rc;
104397 }
104398
104399 else if( sParse.pNewTrigger ){
104400 if( isLegacy==0 ){
104401 rc = renameResolveTrigger(&sParse, bTemp ? 0 : zDb);
104402 }
104403 if( rc==SQLITE_OK0 ){
104404 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
104405 int i2 = sqlite3FindDbName(db, zDb);
104406 if( i1==i2 ) sqlite3_result_int(context, 1);
104407 }
104408 }
104409 }
104410
104411 if( rc!=SQLITE_OK0 ){
104412 renameColumnParseError(context, 1, argv[2], argv[3], &sParse);
104413 }
104414 renameParseCleanup(&sParse);
104415 }
104416
104417#ifndef SQLITE_OMIT_AUTHORIZATION
104418 db->xAuth = xAuth;
104419#endif
104420}
104421
104422/*
104423** Register built-in functions used to help implement ALTER TABLE
104424*/
104425SQLITE_PRIVATEstatic void sqlite3AlterFunctions(void){
104426 static FuncDef aAlterTableFuncs[] = {
104427 INTERNAL_FUNCTION(sqlite_rename_column, 9, renameColumnFunc){9, 0x00040000|1|0x0800, 0, 0, renameColumnFunc, 0, 0, 0, "sqlite_rename_column"
, {0} }
,
104428 INTERNAL_FUNCTION(sqlite_rename_table, 7, renameTableFunc){7, 0x00040000|1|0x0800, 0, 0, renameTableFunc, 0, 0, 0, "sqlite_rename_table"
, {0} }
,
104429 INTERNAL_FUNCTION(sqlite_rename_test, 5, renameTableTest){5, 0x00040000|1|0x0800, 0, 0, renameTableTest, 0, 0, 0, "sqlite_rename_test"
, {0} }
,
104430 };
104431 sqlite3InsertBuiltinFuncs(aAlterTableFuncs, ArraySize(aAlterTableFuncs)((int)(sizeof(aAlterTableFuncs)/sizeof(aAlterTableFuncs[0]))));
104432}
104433#endif /* SQLITE_ALTER_TABLE */
104434
104435/************** End of alter.c ***********************************************/
104436/************** Begin file analyze.c *****************************************/
104437/*
104438** 2005-07-08
104439**
104440** The author disclaims copyright to this source code. In place of
104441** a legal notice, here is a blessing:
104442**
104443** May you do good and not evil.
104444** May you find forgiveness for yourself and forgive others.
104445** May you share freely, never taking more than you give.
104446**
104447*************************************************************************
104448** This file contains code associated with the ANALYZE command.
104449**
104450** The ANALYZE command gather statistics about the content of tables
104451** and indices. These statistics are made available to the query planner
104452** to help it make better decisions about how to perform queries.
104453**
104454** The following system tables are or have been supported:
104455**
104456** CREATE TABLE sqlite_stat1(tbl, idx, stat);
104457** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
104458** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
104459** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
104460**
104461** Additional tables might be added in future releases of SQLite.
104462** The sqlite_stat2 table is not created or used unless the SQLite version
104463** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
104464** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
104465** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
104466** created and used by SQLite versions 3.7.9 and later and with
104467** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
104468** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
104469** version of sqlite_stat3 and is only available when compiled with
104470** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later. It is
104471** not possible to enable both STAT3 and STAT4 at the same time. If they
104472** are both enabled, then STAT4 takes precedence.
104473**
104474** For most applications, sqlite_stat1 provides all the statistics required
104475** for the query planner to make good choices.
104476**
104477** Format of sqlite_stat1:
104478**
104479** There is normally one row per index, with the index identified by the
104480** name in the idx column. The tbl column is the name of the table to
104481** which the index belongs. In each such row, the stat column will be
104482** a string consisting of a list of integers. The first integer in this
104483** list is the number of rows in the index. (This is the same as the
104484** number of rows in the table, except for partial indices.) The second
104485** integer is the average number of rows in the index that have the same
104486** value in the first column of the index. The third integer is the average
104487** number of rows in the index that have the same value for the first two
104488** columns. The N-th integer (for N>1) is the average number of rows in
104489** the index which have the same value for the first N-1 columns. For
104490** a K-column index, there will be K+1 integers in the stat column. If
104491** the index is unique, then the last integer will be 1.
104492**
104493** The list of integers in the stat column can optionally be followed
104494** by the keyword "unordered". The "unordered" keyword, if it is present,
104495** must be separated from the last integer by a single space. If the
104496** "unordered" keyword is present, then the query planner assumes that
104497** the index is unordered and will not use the index for a range query.
104498**
104499** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
104500** column contains a single integer which is the (estimated) number of
104501** rows in the table identified by sqlite_stat1.tbl.
104502**
104503** Format of sqlite_stat2:
104504**
104505** The sqlite_stat2 is only created and is only used if SQLite is compiled
104506** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
104507** 3.6.18 and 3.7.8. The "stat2" table contains additional information
104508** about the distribution of keys within an index. The index is identified by
104509** the "idx" column and the "tbl" column is the name of the table to which
104510** the index belongs. There are usually 10 rows in the sqlite_stat2
104511** table for each index.
104512**
104513** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
104514** inclusive are samples of the left-most key value in the index taken at
104515** evenly spaced points along the index. Let the number of samples be S
104516** (10 in the standard build) and let C be the number of rows in the index.
104517** Then the sampled rows are given by:
104518**
104519** rownumber = (i*C*2 + C)/(S*2)
104520**
104521** For i between 0 and S-1. Conceptually, the index space is divided into
104522** S uniform buckets and the samples are the middle row from each bucket.
104523**
104524** The format for sqlite_stat2 is recorded here for legacy reference. This
104525** version of SQLite does not support sqlite_stat2. It neither reads nor
104526** writes the sqlite_stat2 table. This version of SQLite only supports
104527** sqlite_stat3.
104528**
104529** Format for sqlite_stat3:
104530**
104531** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
104532** sqlite_stat4 format will be described first. Further information
104533** about sqlite_stat3 follows the sqlite_stat4 description.
104534**
104535** Format for sqlite_stat4:
104536**
104537** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
104538** to aid the query planner in choosing good indices based on the values
104539** that indexed columns are compared against in the WHERE clauses of
104540** queries.
104541**
104542** The sqlite_stat4 table contains multiple entries for each index.
104543** The idx column names the index and the tbl column is the table of the
104544** index. If the idx and tbl columns are the same, then the sample is
104545** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
104546** binary encoding of a key from the index. The nEq column is a
104547** list of integers. The first integer is the approximate number
104548** of entries in the index whose left-most column exactly matches
104549** the left-most column of the sample. The second integer in nEq
104550** is the approximate number of entries in the index where the
104551** first two columns match the first two columns of the sample.
104552** And so forth. nLt is another list of integers that show the approximate
104553** number of entries that are strictly less than the sample. The first
104554** integer in nLt contains the number of entries in the index where the
104555** left-most column is less than the left-most column of the sample.
104556** The K-th integer in the nLt entry is the number of index entries
104557** where the first K columns are less than the first K columns of the
104558** sample. The nDLt column is like nLt except that it contains the
104559** number of distinct entries in the index that are less than the
104560** sample.
104561**
104562** There can be an arbitrary number of sqlite_stat4 entries per index.
104563** The ANALYZE command will typically generate sqlite_stat4 tables
104564** that contain between 10 and 40 samples which are distributed across
104565** the key space, though not uniformly, and which include samples with
104566** large nEq values.
104567**
104568** Format for sqlite_stat3 redux:
104569**
104570** The sqlite_stat3 table is like sqlite_stat4 except that it only
104571** looks at the left-most column of the index. The sqlite_stat3.sample
104572** column contains the actual value of the left-most column instead
104573** of a blob encoding of the complete index key as is found in
104574** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
104575** all contain just a single integer which is the same as the first
104576** integer in the equivalent columns in sqlite_stat4.
104577*/
104578#ifndef SQLITE_OMIT_ANALYZE
104579/* #include "sqliteInt.h" */
104580
104581#if defined(SQLITE_ENABLE_STAT4)
104582# define IsStat40 1
104583# define IsStat30 0
104584#elif defined(SQLITE_ENABLE_STAT3)
104585# define IsStat40 0
104586# define IsStat30 1
104587#else
104588# define IsStat40 0
104589# define IsStat30 0
104590# undef SQLITE_STAT4_SAMPLES1
104591# define SQLITE_STAT4_SAMPLES1 1
104592#endif
104593#define IsStat34(0 +0) (IsStat30+IsStat40) /* 1 for STAT3 or STAT4. 0 otherwise */
104594
104595/*
104596** This routine generates code that opens the sqlite_statN tables.
104597** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
104598** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
104599** appropriate compile-time options are provided.
104600**
104601** If the sqlite_statN tables do not previously exist, it is created.
104602**
104603** Argument zWhere may be a pointer to a buffer containing a table name,
104604** or it may be a NULL pointer. If it is not NULL, then all entries in
104605** the sqlite_statN tables associated with the named table are deleted.
104606** If zWhere==0, then code is generated to delete all stat table entries.
104607*/
104608static void openStatTable(
104609 Parse *pParse, /* Parsing context */
104610 int iDb, /* The database we are looking in */
104611 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
104612 const char *zWhere, /* Delete entries for this table or index */
104613 const char *zWhereType /* Either "tbl" or "idx" */
104614){
104615 static const struct {
104616 const char *zName;
104617 const char *zCols;
104618 } aTable[] = {
104619 { "sqlite_stat1", "tbl,idx,stat" },
104620#if defined(SQLITE_ENABLE_STAT4)
104621 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
104622 { "sqlite_stat3", 0 },
104623#elif defined(SQLITE_ENABLE_STAT3)
104624 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
104625 { "sqlite_stat4", 0 },
104626#else
104627 { "sqlite_stat3", 0 },
104628 { "sqlite_stat4", 0 },
104629#endif
104630 };
104631 int i;
104632 sqlite3 *db = pParse->db;
104633 Db *pDb;
104634 Vdbe *v = sqlite3GetVdbe(pParse);
104635 int aRoot[ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0])))];
104636 u8 aCreateTbl[ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0])))];
104637
104638 if( v==0 ) return;
104639 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
104640 assert( sqlite3VdbeDb(v)==db )((void) (0));
104641 pDb = &db->aDb[iDb];
104642
104643 /* Create new statistic tables if they do not exist, or clear them
104644 ** if they do already exist.
104645 */
104646 for(i=0; i<ArraySize(aTable)((int)(sizeof(aTable)/sizeof(aTable[0]))); i++){
104647 const char *zTab = aTable[i].zName;
104648 Table *pStat;
104649 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
104650 if( aTable[i].zCols ){
104651 /* The sqlite_statN table does not exist. Create it. Note that a
104652 ** side-effect of the CREATE TABLE statement is to leave the rootpage
104653 ** of the new table in register pParse->regRoot. This is important
104654 ** because the OpenWrite opcode below will be needing it. */
104655 sqlite3NestedParse(pParse,
104656 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
104657 );
104658 aRoot[i] = pParse->regRoot;
104659 aCreateTbl[i] = OPFLAG_P2ISREG0x10;
104660 }
104661 }else{
104662 /* The table already exists. If zWhere is not NULL, delete all entries
104663 ** associated with the table zWhere. If zWhere is NULL, delete the
104664 ** entire contents of the table. */
104665 aRoot[i] = pStat->tnum;
104666 aCreateTbl[i] = 0;
104667 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
104668 if( zWhere ){
104669 sqlite3NestedParse(pParse,
104670 "DELETE FROM %Q.%s WHERE %s=%Q",
104671 pDb->zDbSName, zTab, zWhereType, zWhere
104672 );
104673#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
104674 }else if( db->xPreUpdateCallback ){
104675 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
104676#endif
104677 }else{
104678 /* The sqlite_stat[134] table already exists. Delete all rows. */
104679 sqlite3VdbeAddOp2(v, OP_Clear137, aRoot[i], iDb);
104680 }
104681 }
104682 }
104683
104684 /* Open the sqlite_stat[134] tables for writing. */
104685 for(i=0; aTable[i].zCols; i++){
104686 assert( i<ArraySize(aTable) )((void) (0));
104687 sqlite3VdbeAddOp4Int(v, OP_OpenWrite109, iStatCur+i, aRoot[i], iDb, 3);
104688 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
104689 VdbeComment((v, aTable[i].zName));
104690 }
104691}
104692
104693/*
104694** Recommended number of samples for sqlite_stat4
104695*/
104696#ifndef SQLITE_STAT4_SAMPLES1
104697# define SQLITE_STAT4_SAMPLES1 24
104698#endif
104699
104700/*
104701** Three SQL functions - stat_init(), stat_push(), and stat_get() -
104702** share an instance of the following structure to hold their state
104703** information.
104704*/
104705typedef struct Stat4Accum Stat4Accum;
104706typedef struct Stat4Sample Stat4Sample;
104707struct Stat4Sample {
104708 tRowcnt *anEq; /* sqlite_stat4.nEq */
104709 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
104710#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104711 tRowcnt *anLt; /* sqlite_stat4.nLt */
104712 union {
104713 i64 iRowid; /* Rowid in main table of the key */
104714 u8 *aRowid; /* Key for WITHOUT ROWID tables */
104715 } u;
104716 u32 nRowid; /* Sizeof aRowid[] */
104717 u8 isPSample; /* True if a periodic sample */
104718 int iCol; /* If !isPSample, the reason for inclusion */
104719 u32 iHash; /* Tiebreaker hash */
104720#endif
104721};
104722struct Stat4Accum {
104723 tRowcnt nRow; /* Number of rows in the entire table */
104724 tRowcnt nPSample; /* How often to do a periodic sample */
104725 int nCol; /* Number of columns in index + pk/rowid */
104726 int nKeyCol; /* Number of index columns w/o the pk/rowid */
104727 int mxSample; /* Maximum number of samples to accumulate */
104728 Stat4Sample current; /* Current row as a Stat4Sample */
104729 u32 iPrn; /* Pseudo-random number used for sampling */
104730 Stat4Sample *aBest; /* Array of nCol best samples */
104731 int iMin; /* Index in a[] of entry with minimum score */
104732 int nSample; /* Current number of samples */
104733 int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
104734 int iGet; /* Index of current sample accessed by stat_get() */
104735 Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
104736 sqlite3 *db; /* Database connection, for malloc() */
104737};
104738
104739/* Reclaim memory used by a Stat4Sample
104740*/
104741#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104742static void sampleClear(sqlite3 *db, Stat4Sample *p){
104743 assert( db!=0 )((void) (0));
104744 if( p->nRowid ){
104745 sqlite3DbFree(db, p->u.aRowid);
104746 p->nRowid = 0;
104747 }
104748}
104749#endif
104750
104751/* Initialize the BLOB value of a ROWID
104752*/
104753#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104754static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
104755 assert( db!=0 )((void) (0));
104756 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
104757 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
104758 if( p->u.aRowid ){
104759 p->nRowid = n;
104760 memcpy(p->u.aRowid, pData, n);
104761 }else{
104762 p->nRowid = 0;
104763 }
104764}
104765#endif
104766
104767/* Initialize the INTEGER value of a ROWID.
104768*/
104769#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104770static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
104771 assert( db!=0 )((void) (0));
104772 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
104773 p->nRowid = 0;
104774 p->u.iRowid = iRowid;
104775}
104776#endif
104777
104778
104779/*
104780** Copy the contents of object (*pFrom) into (*pTo).
104781*/
104782#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104783static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
104784 pTo->isPSample = pFrom->isPSample;
104785 pTo->iCol = pFrom->iCol;
104786 pTo->iHash = pFrom->iHash;
104787 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
104788 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
104789 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
104790 if( pFrom->nRowid ){
104791 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
104792 }else{
104793 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
104794 }
104795}
104796#endif
104797
104798/*
104799** Reclaim all memory of a Stat4Accum structure.
104800*/
104801static void stat4Destructor(void *pOld){
104802 Stat4Accum *p = (Stat4Accum*)pOld;
104803#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104804 int i;
104805 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
104806 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
104807 sampleClear(p->db, &p->current);
104808#endif
104809 sqlite3DbFree(p->db, p);
104810}
104811
104812/*
104813** Implementation of the stat_init(N,K,C) SQL function. The three parameters
104814** are:
104815** N: The number of columns in the index including the rowid/pk (note 1)
104816** K: The number of columns in the index excluding the rowid/pk.
104817** C: The number of rows in the index (note 2)
104818**
104819** Note 1: In the special case of the covering index that implements a
104820** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
104821** total number of columns in the table.
104822**
104823** Note 2: C is only used for STAT3 and STAT4.
104824**
104825** For indexes on ordinary rowid tables, N==K+1. But for indexes on
104826** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
104827** PRIMARY KEY of the table. The covering index that implements the
104828** original WITHOUT ROWID table as N==K as a special case.
104829**
104830** This routine allocates the Stat4Accum object in heap memory. The return
104831** value is a pointer to the Stat4Accum object. The datatype of the
104832** return value is BLOB, but it is really just a pointer to the Stat4Accum
104833** object.
104834*/
104835static void statInit(
104836 sqlite3_context *context,
104837 int argc,
104838 sqlite3_value **argv
104839){
104840 Stat4Accum *p;
104841 int nCol; /* Number of columns in index being sampled */
104842 int nKeyCol; /* Number of key columns */
104843 int nColUp; /* nCol rounded up for alignment */
104844 int n; /* Bytes of space to allocate */
104845 sqlite3 *db; /* Database connection */
104846#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104847 int mxSample = SQLITE_STAT4_SAMPLES1;
104848#endif
104849
104850 /* Decode the three function arguments */
104851 UNUSED_PARAMETER(argc)(void)(argc);
104852 nCol = sqlite3_value_int(argv[0]);
104853 assert( nCol>0 )((void) (0));
104854 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
104855 nKeyCol = sqlite3_value_int(argv[1]);
104856 assert( nKeyCol<=nCol )((void) (0));
104857 assert( nKeyCol>0 )((void) (0));
104858
104859 /* Allocate the space required for the Stat4Accum object */
104860 n = sizeof(*p)
104861 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
104862 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
104863#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104864 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
104865 + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
104866 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
104867#endif
104868 ;
104869 db = sqlite3_context_db_handle(context);
104870 p = sqlite3DbMallocZero(db, n);
104871 if( p==0 ){
104872 sqlite3_result_error_nomem(context);
104873 return;
104874 }
104875
104876 p->db = db;
104877 p->nRow = 0;
104878 p->nCol = nCol;
104879 p->nKeyCol = nKeyCol;
104880 p->current.anDLt = (tRowcnt*)&p[1];
104881 p->current.anEq = &p->current.anDLt[nColUp];
104882
104883#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104884 {
104885 u8 *pSpace; /* Allocated space not yet assigned */
104886 int i; /* Used to iterate through p->aSample[] */
104887
104888 p->iGet = -1;
104889 p->mxSample = mxSample;
104890 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
104891 p->current.anLt = &p->current.anEq[nColUp];
104892 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
104893
104894 /* Set up the Stat4Accum.a[] and aBest[] arrays */
104895 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
104896 p->aBest = &p->a[mxSample];
104897 pSpace = (u8*)(&p->a[mxSample+nCol]);
104898 for(i=0; i<(mxSample+nCol); i++){
104899 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
104900 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
104901 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
104902 }
104903 assert( (pSpace - (u8*)p)==n )((void) (0));
104904
104905 for(i=0; i<nCol; i++){
104906 p->aBest[i].iCol = i;
104907 }
104908 }
104909#endif
104910
104911 /* Return a pointer to the allocated object to the caller. Note that
104912 ** only the pointer (the 2nd parameter) matters. The size of the object
104913 ** (given by the 3rd parameter) is never used and can be any positive
104914 ** value. */
104915 sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
104916}
104917static const FuncDef statInitFuncdef = {
104918 2+IsStat34(0 +0), /* nArg */
104919 SQLITE_UTF81, /* funcFlags */
104920 0, /* pUserData */
104921 0, /* pNext */
104922 statInit, /* xSFunc */
104923 0, /* xFinalize */
104924 0, 0, /* xValue, xInverse */
104925 "stat_init", /* zName */
104926 {0}
104927};
104928
104929#ifdef SQLITE_ENABLE_STAT4
104930/*
104931** pNew and pOld are both candidate non-periodic samples selected for
104932** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
104933** considering only any trailing columns and the sample hash value, this
104934** function returns true if sample pNew is to be preferred over pOld.
104935** In other words, if we assume that the cardinalities of the selected
104936** column for pNew and pOld are equal, is pNew to be preferred over pOld.
104937**
104938** This function assumes that for each argument sample, the contents of
104939** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
104940*/
104941static int sampleIsBetterPost(
104942 Stat4Accum *pAccum,
104943 Stat4Sample *pNew,
104944 Stat4Sample *pOld
104945){
104946 int nCol = pAccum->nCol;
104947 int i;
104948 assert( pNew->iCol==pOld->iCol )((void) (0));
104949 for(i=pNew->iCol+1; i<nCol; i++){
104950 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
104951 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
104952 }
104953 if( pNew->iHash>pOld->iHash ) return 1;
104954 return 0;
104955}
104956#endif
104957
104958#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104959/*
104960** Return true if pNew is to be preferred over pOld.
104961**
104962** This function assumes that for each argument sample, the contents of
104963** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
104964*/
104965static int sampleIsBetter(
104966 Stat4Accum *pAccum,
104967 Stat4Sample *pNew,
104968 Stat4Sample *pOld
104969){
104970 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
104971 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
104972
104973 assert( pOld->isPSample==0 && pNew->isPSample==0 )((void) (0));
104974 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) )((void) (0));
104975
104976 if( (nEqNew>nEqOld) ) return 1;
104977#ifdef SQLITE_ENABLE_STAT4
104978 if( nEqNew==nEqOld ){
104979 if( pNew->iCol<pOld->iCol ) return 1;
104980 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
104981 }
104982 return 0;
104983#else
104984 return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
104985#endif
104986}
104987
104988/*
104989** Copy the contents of sample *pNew into the p->a[] array. If necessary,
104990** remove the least desirable sample from p->a[] to make room.
104991*/
104992static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
104993 Stat4Sample *pSample = 0;
104994 int i;
104995
104996 assert( IsStat4 || nEqZero==0 )((void) (0));
104997
104998#ifdef SQLITE_ENABLE_STAT4
104999 /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
105000 ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
105001 ** other words, if nMaxEqZero is n, then it is guaranteed that there
105002 ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
105003 if( nEqZero>p->nMaxEqZero ){
105004 p->nMaxEqZero = nEqZero;
105005 }
105006 if( pNew->isPSample==0 ){
105007 Stat4Sample *pUpgrade = 0;
105008 assert( pNew->anEq[pNew->iCol]>0 )((void) (0));
105009
105010 /* This sample is being added because the prefix that ends in column
105011 ** iCol occurs many times in the table. However, if we have already
105012 ** added a sample that shares this prefix, there is no need to add
105013 ** this one. Instead, upgrade the priority of the highest priority
105014 ** existing sample that shares this prefix. */
105015 for(i=p->nSample-1; i>=0; i--){
105016 Stat4Sample *pOld = &p->a[i];
105017 if( pOld->anEq[pNew->iCol]==0 ){
105018 if( pOld->isPSample ) return;
105019 assert( pOld->iCol>pNew->iCol )((void) (0));
105020 assert( sampleIsBetter(p, pNew, pOld) )((void) (0));
105021 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
105022 pUpgrade = pOld;
105023 }
105024 }
105025 }
105026 if( pUpgrade ){
105027 pUpgrade->iCol = pNew->iCol;
105028 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
105029 goto find_new_min;
105030 }
105031 }
105032#endif
105033
105034 /* If necessary, remove sample iMin to make room for the new sample. */
105035 if( p->nSample>=p->mxSample ){
105036 Stat4Sample *pMin = &p->a[p->iMin];
105037 tRowcnt *anEq = pMin->anEq;
105038 tRowcnt *anLt = pMin->anLt;
105039 tRowcnt *anDLt = pMin->anDLt;
105040 sampleClear(p->db, pMin);
105041 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
105042 pSample = &p->a[p->nSample-1];
105043 pSample->nRowid = 0;
105044 pSample->anEq = anEq;
105045 pSample->anDLt = anDLt;
105046 pSample->anLt = anLt;
105047 p->nSample = p->mxSample-1;
105048 }
105049
105050 /* The "rows less-than" for the rowid column must be greater than that
105051 ** for the last sample in the p->a[] array. Otherwise, the samples would
105052 ** be out of order. */
105053#ifdef SQLITE_ENABLE_STAT4
105054 assert( p->nSample==0((void) (0))
105055 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] )((void) (0));
105056#endif
105057
105058 /* Insert the new sample */
105059 pSample = &p->a[p->nSample];
105060 sampleCopy(p, pSample, pNew);
105061 p->nSample++;
105062
105063 /* Zero the first nEqZero entries in the anEq[] array. */
105064 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
105065
105066#ifdef SQLITE_ENABLE_STAT4
105067 find_new_min:
105068#endif
105069 if( p->nSample>=p->mxSample ){
105070 int iMin = -1;
105071 for(i=0; i<p->mxSample; i++){
105072 if( p->a[i].isPSample ) continue;
105073 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
105074 iMin = i;
105075 }
105076 }
105077 assert( iMin>=0 )((void) (0));
105078 p->iMin = iMin;
105079 }
105080}
105081#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
105082
105083/*
105084** Field iChng of the index being scanned has changed. So at this point
105085** p->current contains a sample that reflects the previous row of the
105086** index. The value of anEq[iChng] and subsequent anEq[] elements are
105087** correct at this point.
105088*/
105089static void samplePushPrevious(Stat4Accum *p, int iChng){
105090#ifdef SQLITE_ENABLE_STAT4
105091 int i;
105092
105093 /* Check if any samples from the aBest[] array should be pushed
105094 ** into IndexSample.a[] at this point. */
105095 for(i=(p->nCol-2); i>=iChng; i--){
105096 Stat4Sample *pBest = &p->aBest[i];
105097 pBest->anEq[i] = p->current.anEq[i];
105098 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
105099 sampleInsert(p, pBest, i);
105100 }
105101 }
105102
105103 /* Check that no sample contains an anEq[] entry with an index of
105104 ** p->nMaxEqZero or greater set to zero. */
105105 for(i=p->nSample-1; i>=0; i--){
105106 int j;
105107 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 )((void) (0));
105108 }
105109
105110 /* Update the anEq[] fields of any samples already collected. */
105111 if( iChng<p->nMaxEqZero ){
105112 for(i=p->nSample-1; i>=0; i--){
105113 int j;
105114 for(j=iChng; j<p->nCol; j++){
105115 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
105116 }
105117 }
105118 p->nMaxEqZero = iChng;
105119 }
105120#endif
105121
105122#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
105123 if( iChng==0 ){
105124 tRowcnt nLt = p->current.anLt[0];
105125 tRowcnt nEq = p->current.anEq[0];
105126
105127 /* Check if this is to be a periodic sample. If so, add it. */
105128 if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
105129 p->current.isPSample = 1;
105130 sampleInsert(p, &p->current, 0);
105131 p->current.isPSample = 0;
105132 }else
105133
105134 /* Or if it is a non-periodic sample. Add it in this case too. */
105135 if( p->nSample<p->mxSample
105136 || sampleIsBetter(p, &p->current, &p->a[p->iMin])
105137 ){
105138 sampleInsert(p, &p->current, 0);
105139 }
105140 }
105141#endif
105142
105143#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
105144 UNUSED_PARAMETER( p )(void)(p);
105145 UNUSED_PARAMETER( iChng )(void)(iChng);
105146#endif
105147}
105148
105149/*
105150** Implementation of the stat_push SQL function: stat_push(P,C,R)
105151** Arguments:
105152**
105153** P Pointer to the Stat4Accum object created by stat_init()
105154** C Index of left-most column to differ from previous row
105155** R Rowid for the current row. Might be a key record for
105156** WITHOUT ROWID tables.
105157**
105158** This SQL function always returns NULL. It's purpose it to accumulate
105159** statistical data and/or samples in the Stat4Accum object about the
105160** index being analyzed. The stat_get() SQL function will later be used to
105161** extract relevant information for constructing the sqlite_statN tables.
105162**
105163** The R parameter is only used for STAT3 and STAT4
105164*/
105165static void statPush(
105166 sqlite3_context *context,
105167 int argc,
105168 sqlite3_value **argv
105169){
105170 int i;
105171
105172 /* The three function arguments */
105173 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
105174 int iChng = sqlite3_value_int(argv[1]);
105175
105176 UNUSED_PARAMETER( argc )(void)(argc);
105177 UNUSED_PARAMETER( context )(void)(context);
105178 assert( p->nCol>0 )((void) (0));
105179 assert( iChng<p->nCol )((void) (0));
105180
105181 if( p->nRow==0 ){
105182 /* This is the first call to this function. Do initialization. */
105183 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
105184 }else{
105185 /* Second and subsequent calls get processed here */
105186 samplePushPrevious(p, iChng);
105187
105188 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
105189 ** to the current row of the index. */
105190 for(i=0; i<iChng; i++){
105191 p->current.anEq[i]++;
105192 }
105193 for(i=iChng; i<p->nCol; i++){
105194 p->current.anDLt[i]++;
105195#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105196 p->current.anLt[i] += p->current.anEq[i];
105197#endif
105198 p->current.anEq[i] = 1;
105199 }
105200 }
105201 p->nRow++;
105202#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105203 if( sqlite3_value_type(argv[2])==SQLITE_INTEGER1 ){
105204 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
105205 }else{
105206 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
105207 sqlite3_value_blob(argv[2]));
105208 }
105209 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
105210#endif
105211
105212#ifdef SQLITE_ENABLE_STAT4
105213 {
105214 tRowcnt nLt = p->current.anLt[p->nCol-1];
105215
105216 /* Check if this is to be a periodic sample. If so, add it. */
105217 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
105218 p->current.isPSample = 1;
105219 p->current.iCol = 0;
105220 sampleInsert(p, &p->current, p->nCol-1);
105221 p->current.isPSample = 0;
105222 }
105223
105224 /* Update the aBest[] array. */
105225 for(i=0; i<(p->nCol-1); i++){
105226 p->current.iCol = i;
105227 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
105228 sampleCopy(p, &p->aBest[i], &p->current);
105229 }
105230 }
105231 }
105232#endif
105233}
105234static const FuncDef statPushFuncdef = {
105235 2+IsStat34(0 +0), /* nArg */
105236 SQLITE_UTF81, /* funcFlags */
105237 0, /* pUserData */
105238 0, /* pNext */
105239 statPush, /* xSFunc */
105240 0, /* xFinalize */
105241 0, 0, /* xValue, xInverse */
105242 "stat_push", /* zName */
105243 {0}
105244};
105245
105246#define STAT_GET_STAT10 0 /* "stat" column of stat1 table */
105247#define STAT_GET_ROWID1 1 /* "rowid" column of stat[34] entry */
105248#define STAT_GET_NEQ2 2 /* "neq" column of stat[34] entry */
105249#define STAT_GET_NLT3 3 /* "nlt" column of stat[34] entry */
105250#define STAT_GET_NDLT4 4 /* "ndlt" column of stat[34] entry */
105251
105252/*
105253** Implementation of the stat_get(P,J) SQL function. This routine is
105254** used to query statistical information that has been gathered into
105255** the Stat4Accum object by prior calls to stat_push(). The P parameter
105256** has type BLOB but it is really just a pointer to the Stat4Accum object.
105257** The content to returned is determined by the parameter J
105258** which is one of the STAT_GET_xxxx values defined above.
105259**
105260** The stat_get(P,J) function is not available to generic SQL. It is
105261** inserted as part of a manually constructed bytecode program. (See
105262** the callStatGet() routine below.) It is guaranteed that the P
105263** parameter will always be a poiner to a Stat4Accum object, never a
105264** NULL.
105265**
105266** If neither STAT3 nor STAT4 are enabled, then J is always
105267** STAT_GET_STAT1 and is hence omitted and this routine becomes
105268** a one-parameter function, stat_get(P), that always returns the
105269** stat1 table entry information.
105270*/
105271static void statGet(
105272 sqlite3_context *context,
105273 int argc,
105274 sqlite3_value **argv
105275){
105276 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
105277#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105278 /* STAT3 and STAT4 have a parameter on this routine. */
105279 int eCall = sqlite3_value_int(argv[1]);
105280 assert( argc==2 )((void) (0));
105281 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ((void) (0))
105282 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT((void) (0))
105283 || eCall==STAT_GET_NDLT((void) (0))
105284 )((void) (0));
105285 if( eCall==STAT_GET_STAT10 )
105286#else
105287 assert( argc==1 )((void) (0));
105288#endif
105289 {
105290 /* Return the value to store in the "stat" column of the sqlite_stat1
105291 ** table for this index.
105292 **
105293 ** The value is a string composed of a list of integers describing
105294 ** the index. The first integer in the list is the total number of
105295 ** entries in the index. There is one additional integer in the list
105296 ** for each indexed column. This additional integer is an estimate of
105297 ** the number of rows matched by a stabbing query on the index using
105298 ** a key with the corresponding number of fields. In other words,
105299 ** if the index is on columns (a,b) and the sqlite_stat1 value is
105300 ** "100 10 2", then SQLite estimates that:
105301 **
105302 ** * the index contains 100 rows,
105303 ** * "WHERE a=?" matches 10 rows, and
105304 ** * "WHERE a=? AND b=?" matches 2 rows.
105305 **
105306 ** If D is the count of distinct values and K is the total number of
105307 ** rows, then each estimate is computed as:
105308 **
105309 ** I = (K+D-1)/D
105310 */
105311 char *z;
105312 int i;
105313
105314 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
105315 if( zRet==0 ){
105316 sqlite3_result_error_nomem(context);
105317 return;
105318 }
105319
105320 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
105321 z = zRet + sqlite3Strlen30(zRet);
105322 for(i=0; i<p->nKeyCol; i++){
105323 u64 nDistinct = p->current.anDLt[i] + 1;
105324 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
105325 sqlite3_snprintf(24, z, " %llu", iVal);
105326 z += sqlite3Strlen30(z);
105327 assert( p->current.anEq[i] )((void) (0));
105328 }
105329 assert( z[0]=='\0' && z>zRet )((void) (0));
105330
105331 sqlite3_result_text(context, zRet, -1, sqlite3_free);
105332 }
105333#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105334 else if( eCall==STAT_GET_ROWID1 ){
105335 if( p->iGet<0 ){
105336 samplePushPrevious(p, 0);
105337 p->iGet = 0;
105338 }
105339 if( p->iGet<p->nSample ){
105340 Stat4Sample *pS = p->a + p->iGet;
105341 if( pS->nRowid==0 ){
105342 sqlite3_result_int64(context, pS->u.iRowid);
105343 }else{
105344 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
105345 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
105346 }
105347 }
105348 }else{
105349 tRowcnt *aCnt = 0;
105350
105351 assert( p->iGet<p->nSample )((void) (0));
105352 switch( eCall ){
105353 case STAT_GET_NEQ2: aCnt = p->a[p->iGet].anEq; break;
105354 case STAT_GET_NLT3: aCnt = p->a[p->iGet].anLt; break;
105355 default: {
105356 aCnt = p->a[p->iGet].anDLt;
105357 p->iGet++;
105358 break;
105359 }
105360 }
105361
105362 if( IsStat30 ){
105363 sqlite3_result_int64(context, (i64)aCnt[0]);
105364 }else{
105365 char *zRet = sqlite3MallocZero(p->nCol * 25);
105366 if( zRet==0 ){
105367 sqlite3_result_error_nomem(context);
105368 }else{
105369 int i;
105370 char *z = zRet;
105371 for(i=0; i<p->nCol; i++){
105372 sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
105373 z += sqlite3Strlen30(z);
105374 }
105375 assert( z[0]=='\0' && z>zRet )((void) (0));
105376 z[-1] = '\0';
105377 sqlite3_result_text(context, zRet, -1, sqlite3_free);
105378 }
105379 }
105380 }
105381#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
105382#ifndef SQLITE_DEBUG
105383 UNUSED_PARAMETER( argc )(void)(argc);
105384#endif
105385}
105386static const FuncDef statGetFuncdef = {
105387 1+IsStat34(0 +0), /* nArg */
105388 SQLITE_UTF81, /* funcFlags */
105389 0, /* pUserData */
105390 0, /* pNext */
105391 statGet, /* xSFunc */
105392 0, /* xFinalize */
105393 0, 0, /* xValue, xInverse */
105394 "stat_get", /* zName */
105395 {0}
105396};
105397
105398static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
105399 assert( regOut!=regStat4 && regOut!=regStat4+1 )((void) (0));
105400#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105401 sqlite3VdbeAddOp2(v, OP_Integer70, iParam, regStat4+1);
105402#elif SQLITE_DEBUG
105403 assert( iParam==STAT_GET_STAT1 )((void) (0));
105404#else
105405 UNUSED_PARAMETER( iParam )(void)(iParam);
105406#endif
105407 sqlite3VdbeAddOp4(v, OP_Function063, 0, regStat4, regOut,
105408 (char*)&statGetFuncdef, P4_FUNCDEF(-8));
105409 sqlite3VdbeChangeP5(v, 1 + IsStat34(0 +0));
105410}
105411
105412/*
105413** Generate code to do an analysis of all indices associated with
105414** a single table.
105415*/
105416static void analyzeOneTable(
105417 Parse *pParse, /* Parser context */
105418 Table *pTab, /* Table whose indices are to be analyzed */
105419 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
105420 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
105421 int iMem, /* Available memory locations begin here */
105422 int iTab /* Next available cursor */
105423){
105424 sqlite3 *db = pParse->db; /* Database handle */
105425 Index *pIdx; /* An index to being analyzed */
105426 int iIdxCur; /* Cursor open on index being analyzed */
105427 int iTabCur; /* Table cursor */
105428 Vdbe *v; /* The virtual machine being built up */
105429 int i; /* Loop counter */
105430 int jZeroRows = -1; /* Jump from here if number of rows is zero */
105431 int iDb; /* Index of database containing pTab */
105432 u8 needTableCnt = 1; /* True to count the table */
105433 int regNewRowid = iMem++; /* Rowid for the inserted record */
105434 int regStat4 = iMem++; /* Register to hold Stat4Accum object */
105435 int regChng = iMem++; /* Index of changed index field */
105436#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105437 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
105438#endif
105439 int regTemp = iMem++; /* Temporary use register */
105440 int regTabname = iMem++; /* Register containing table name */
105441 int regIdxname = iMem++; /* Register containing index name */
105442 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
105443 int regPrev = iMem; /* MUST BE LAST (see below) */
105444#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
105445 Table *pStat1 = 0;
105446#endif
105447
105448 pParse->nMem = MAX(pParse->nMem, iMem)((pParse->nMem)>(iMem)?(pParse->nMem):(iMem));
105449 v = sqlite3GetVdbe(pParse);
105450 if( v==0 || NEVER(pTab==0)(pTab==0) ){
105451 return;
105452 }
105453 if( pTab->tnum==0 ){
105454 /* Do not gather statistics on views or virtual tables */
105455 return;
105456 }
105457 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
105458 /* Do not gather statistics on system tables */
105459 return;
105460 }
105461 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
105462 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
105463 assert( iDb>=0 )((void) (0));
105464 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
105465#ifndef SQLITE_OMIT_AUTHORIZATION
105466 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE28, pTab->zName, 0,
105467 db->aDb[iDb].zDbSName ) ){
105468 return;
105469 }
105470#endif
105471
105472#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
105473 if( db->xPreUpdateCallback ){
105474 pStat1 = (Table*)sqlite3DbMallocZero(db, sizeof(Table) + 13);
105475 if( pStat1==0 ) return;
105476 pStat1->zName = (char*)&pStat1[1];
105477 memcpy(pStat1->zName, "sqlite_stat1", 13);
105478 pStat1->nCol = 3;
105479 pStat1->iPKey = -1;
105480 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop170, 0, 0, 0,(char*)pStat1,P4_DYNBLOB(-17));
105481 }
105482#endif
105483
105484 /* Establish a read-lock on the table at the shared-cache level.
105485 ** Open a read-only cursor on the table. Also allocate a cursor number
105486 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
105487 ** this time though. */
105488 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
105489 iTabCur = iTab++;
105490 iIdxCur = iTab++;
105491 pParse->nTab = MAX(pParse->nTab, iTab)((pParse->nTab)>(iTab)?(pParse->nTab):(iTab));
105492 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead108);
105493 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
105494
105495 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
105496 int nCol; /* Number of columns in pIdx. "N" */
105497 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
105498 int addrNextRow; /* Address of "next_row:" */
105499 const char *zIdxName; /* Name of the index */
105500 int nColTest; /* Number of columns to test for changes */
105501
105502 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
105503 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
105504 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
105505 nCol = pIdx->nKeyCol;
105506 zIdxName = pTab->zName;
105507 nColTest = nCol - 1;
105508 }else{
105509 nCol = pIdx->nColumn;
105510 zIdxName = pIdx->zName;
105511 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
105512 }
105513
105514 /* Populate the register containing the index name. */
105515 sqlite3VdbeLoadString(v, regIdxname, zIdxName);
105516 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
105517
105518 /*
105519 ** Pseudo-code for loop that calls stat_push():
105520 **
105521 ** Rewind csr
105522 ** if eof(csr) goto end_of_scan;
105523 ** regChng = 0
105524 ** goto chng_addr_0;
105525 **
105526 ** next_row:
105527 ** regChng = 0
105528 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
105529 ** regChng = 1
105530 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
105531 ** ...
105532 ** regChng = N
105533 ** goto chng_addr_N
105534 **
105535 ** chng_addr_0:
105536 ** regPrev(0) = idx(0)
105537 ** chng_addr_1:
105538 ** regPrev(1) = idx(1)
105539 ** ...
105540 **
105541 ** endDistinctTest:
105542 ** regRowid = idx(rowid)
105543 ** stat_push(P, regChng, regRowid)
105544 ** Next csr
105545 ** if !eof(csr) goto next_row;
105546 **
105547 ** end_of_scan:
105548 */
105549
105550 /* Make sure there are enough memory cells allocated to accommodate
105551 ** the regPrev array and a trailing rowid (the rowid slot is required
105552 ** when building a record to insert into the sample column of
105553 ** the sqlite_stat4 table. */
105554 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest)((pParse->nMem)>(regPrev+nColTest)?(pParse->nMem):(regPrev
+nColTest))
;
105555
105556 /* Open a read-only cursor on the index being analyzed. */
105557 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) )((void) (0));
105558 sqlite3VdbeAddOp3(v, OP_OpenRead108, iIdxCur, pIdx->tnum, iDb);
105559 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
105560 VdbeComment((v, "%s", pIdx->zName));
105561
105562 /* Invoke the stat_init() function. The arguments are:
105563 **
105564 ** (1) the number of columns in the index including the rowid
105565 ** (or for a WITHOUT ROWID table, the number of PK columns),
105566 ** (2) the number of columns in the key without the rowid/pk
105567 ** (3) the number of rows in the index,
105568 **
105569 **
105570 ** The third argument is only used for STAT3 and STAT4
105571 */
105572#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105573 sqlite3VdbeAddOp2(v, OP_Count93, iIdxCur, regStat4+3);
105574#endif
105575 sqlite3VdbeAddOp2(v, OP_Integer70, nCol, regStat4+1);
105576 sqlite3VdbeAddOp2(v, OP_Integer70, pIdx->nKeyCol, regStat4+2);
105577 sqlite3VdbeAddOp4(v, OP_Function063, 0, regStat4+1, regStat4,
105578 (char*)&statInitFuncdef, P4_FUNCDEF(-8));
105579 sqlite3VdbeChangeP5(v, 2+IsStat34(0 +0));
105580
105581 /* Implementation of the following:
105582 **
105583 ** Rewind csr
105584 ** if eof(csr) goto end_of_scan;
105585 ** regChng = 0
105586 ** goto next_push_0;
105587 **
105588 */
105589 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind36, iIdxCur);
105590 VdbeCoverage(v);
105591 sqlite3VdbeAddOp2(v, OP_Integer70, 0, regChng);
105592 addrNextRow = sqlite3VdbeCurrentAddr(v);
105593
105594 if( nColTest>0 ){
105595 int endDistinctTest = sqlite3VdbeMakeLabel(pParse);
105596 int *aGotoChng; /* Array of jump instruction addresses */
105597 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
105598 if( aGotoChng==0 ) continue;
105599
105600 /*
105601 ** next_row:
105602 ** regChng = 0
105603 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
105604 ** regChng = 1
105605 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
105606 ** ...
105607 ** regChng = N
105608 ** goto endDistinctTest
105609 */
105610 sqlite3VdbeAddOp0(v, OP_Goto11);
105611 addrNextRow = sqlite3VdbeCurrentAddr(v);
105612 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)((pIdx)->onError!=0) ){
105613 /* For a single-column UNIQUE index, once we have found a non-NULL
105614 ** row, we know that all the rest will be distinct, so skip
105615 ** subsequent distinctness tests. */
105616 sqlite3VdbeAddOp2(v, OP_NotNull51, regPrev, endDistinctTest);
105617 VdbeCoverage(v);
105618 }
105619 for(i=0; i<nColTest; i++){
105620 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
105621 sqlite3VdbeAddOp2(v, OP_Integer70, i, regChng);
105622 sqlite3VdbeAddOp3(v, OP_Column90, iIdxCur, i, regTemp);
105623 aGotoChng[i] =
105624 sqlite3VdbeAddOp4(v, OP_Ne52, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ(-2));
105625 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
105626 VdbeCoverage(v);
105627 }
105628 sqlite3VdbeAddOp2(v, OP_Integer70, nColTest, regChng);
105629 sqlite3VdbeGoto(v, endDistinctTest);
105630
105631
105632 /*
105633 ** chng_addr_0:
105634 ** regPrev(0) = idx(0)
105635 ** chng_addr_1:
105636 ** regPrev(1) = idx(1)
105637 ** ...
105638 */
105639 sqlite3VdbeJumpHere(v, addrNextRow-1);
105640 for(i=0; i<nColTest; i++){
105641 sqlite3VdbeJumpHere(v, aGotoChng[i]);
105642 sqlite3VdbeAddOp3(v, OP_Column90, iIdxCur, i, regPrev+i);
105643 }
105644 sqlite3VdbeResolveLabel(v, endDistinctTest);
105645 sqlite3DbFree(db, aGotoChng);
105646 }
105647
105648 /*
105649 ** chng_addr_N:
105650 ** regRowid = idx(rowid) // STAT34 only
105651 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
105652 ** Next csr
105653 ** if !eof(csr) goto next_row;
105654 */
105655#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105656 assert( regRowid==(regStat4+2) )((void) (0));
105657 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
105658 sqlite3VdbeAddOp2(v, OP_IdxRowid135, iIdxCur, regRowid);
105659 }else{
105660 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
105661 int j, k, regKey;
105662 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
105663 for(j=0; j<pPk->nKeyCol; j++){
105664 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
105665 assert( k>=0 && k<pIdx->nColumn )((void) (0));
105666 sqlite3VdbeAddOp3(v, OP_Column90, iIdxCur, k, regKey+j);
105667 VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
105668 }
105669 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regKey, pPk->nKeyCol, regRowid);
105670 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
105671 }
105672#endif
105673 assert( regChng==(regStat4+1) )((void) (0));
105674 sqlite3VdbeAddOp4(v, OP_Function063, 1, regStat4, regTemp,
105675 (char*)&statPushFuncdef, P4_FUNCDEF(-8));
105676 sqlite3VdbeChangeP5(v, 2+IsStat34(0 +0));
105677 sqlite3VdbeAddOp2(v, OP_Next5, iIdxCur, addrNextRow); VdbeCoverage(v);
105678
105679 /* Add the entry to the stat1 table. */
105680 callStatGet(v, regStat4, STAT_GET_STAT10, regStat1);
105681 assert( "BBB"[0]==SQLITE_AFF_TEXT )((void) (0));
105682 sqlite3VdbeAddOp4(v, OP_MakeRecord92, regTabname, 3, regTemp, "BBB", 0);
105683 sqlite3VdbeAddOp2(v, OP_NewRowid121, iStatCur, regNewRowid);
105684 sqlite3VdbeAddOp3(v, OP_Insert122, iStatCur, regTemp, regNewRowid);
105685#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
105686 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE(-6));
105687#endif
105688 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
105689
105690 /* Add the entries to the stat3 or stat4 table. */
105691#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105692 {
105693 int regEq = regStat1;
105694 int regLt = regStat1+1;
105695 int regDLt = regStat1+2;
105696 int regSample = regStat1+3;
105697 int regCol = regStat1+4;
105698 int regSampleRowid = regCol + nCol;
105699 int addrNext;
105700 int addrIsNull;
105701 u8 seekOp = HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ? OP_NotExists31 : OP_NotFound28;
105702
105703 pParse->nMem = MAX(pParse->nMem, regCol+nCol)((pParse->nMem)>(regCol+nCol)?(pParse->nMem):(regCol
+nCol))
;
105704
105705 addrNext = sqlite3VdbeCurrentAddr(v);
105706 callStatGet(v, regStat4, STAT_GET_ROWID1, regSampleRowid);
105707 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull50, regSampleRowid);
105708 VdbeCoverage(v);
105709 callStatGet(v, regStat4, STAT_GET_NEQ2, regEq);
105710 callStatGet(v, regStat4, STAT_GET_NLT3, regLt);
105711 callStatGet(v, regStat4, STAT_GET_NDLT4, regDLt);
105712 sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
105713 VdbeCoverage(v);
105714#ifdef SQLITE_ENABLE_STAT3
105715 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample);
105716#else
105717 for(i=0; i<nCol; i++){
105718 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
105719 }
105720 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regCol, nCol, regSample);
105721#endif
105722 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regTabname, 6, regTemp);
105723 sqlite3VdbeAddOp2(v, OP_NewRowid121, iStatCur+1, regNewRowid);
105724 sqlite3VdbeAddOp3(v, OP_Insert122, iStatCur+1, regTemp, regNewRowid);
105725 sqlite3VdbeAddOp2(v, OP_Goto11, 1, addrNext); /* P1==1 for end-of-loop */
105726 sqlite3VdbeJumpHere(v, addrIsNull);
105727 }
105728#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
105729
105730 /* End of analysis */
105731 sqlite3VdbeJumpHere(v, addrRewind);
105732 }
105733
105734
105735 /* Create a single sqlite_stat1 entry containing NULL as the index
105736 ** name and the row count as the content.
105737 */
105738 if( pOnlyIdx==0 && needTableCnt ){
105739 VdbeComment((v, "%s", pTab->zName));
105740 sqlite3VdbeAddOp2(v, OP_Count93, iTabCur, regStat1);
105741 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot20, regStat1); VdbeCoverage(v);
105742 sqlite3VdbeAddOp2(v, OP_Null73, 0, regIdxname);
105743 assert( "BBB"[0]==SQLITE_AFF_TEXT )((void) (0));
105744 sqlite3VdbeAddOp4(v, OP_MakeRecord92, regTabname, 3, regTemp, "BBB", 0);
105745 sqlite3VdbeAddOp2(v, OP_NewRowid121, iStatCur, regNewRowid);
105746 sqlite3VdbeAddOp3(v, OP_Insert122, iStatCur, regTemp, regNewRowid);
105747 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
105748#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
105749 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE(-6));
105750#endif
105751 sqlite3VdbeJumpHere(v, jZeroRows);
105752 }
105753}
105754
105755
105756/*
105757** Generate code that will cause the most recent index analysis to
105758** be loaded into internal hash tables where is can be used.
105759*/
105760static void loadAnalysis(Parse *pParse, int iDb){
105761 Vdbe *v = sqlite3GetVdbe(pParse);
105762 if( v ){
105763 sqlite3VdbeAddOp1(v, OP_LoadAnalysis142, iDb);
105764 }
105765}
105766
105767/*
105768** Generate code that will do an analysis of an entire database
105769*/
105770static void analyzeDatabase(Parse *pParse, int iDb){
105771 sqlite3 *db = pParse->db;
105772 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
105773 HashElem *k;
105774 int iStatCur;
105775 int iMem;
105776 int iTab;
105777
105778 sqlite3BeginWriteOperation(pParse, 0, iDb);
105779 iStatCur = pParse->nTab;
105780 pParse->nTab += 3;
105781 openStatTable(pParse, iDb, iStatCur, 0, 0);
105782 iMem = pParse->nMem+1;
105783 iTab = pParse->nTab;
105784 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
105785 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
105786 Table *pTab = (Table*)sqliteHashData(k)((k)->data);
105787 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
105788 }
105789 loadAnalysis(pParse, iDb);
105790}
105791
105792/*
105793** Generate code that will do an analysis of a single table in
105794** a database. If pOnlyIdx is not NULL then it is a single index
105795** in pTab that should be analyzed.
105796*/
105797static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
105798 int iDb;
105799 int iStatCur;
105800
105801 assert( pTab!=0 )((void) (0));
105802 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
105803 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
105804 sqlite3BeginWriteOperation(pParse, 0, iDb);
105805 iStatCur = pParse->nTab;
105806 pParse->nTab += 3;
105807 if( pOnlyIdx ){
105808 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
105809 }else{
105810 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
105811 }
105812 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
105813 loadAnalysis(pParse, iDb);
105814}
105815
105816/*
105817** Generate code for the ANALYZE command. The parser calls this routine
105818** when it recognizes an ANALYZE command.
105819**
105820** ANALYZE -- 1
105821** ANALYZE <database> -- 2
105822** ANALYZE ?<database>.?<tablename> -- 3
105823**
105824** Form 1 causes all indices in all attached databases to be analyzed.
105825** Form 2 analyzes all indices the single database named.
105826** Form 3 analyzes all indices associated with the named table.
105827*/
105828SQLITE_PRIVATEstatic void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
105829 sqlite3 *db = pParse->db;
105830 int iDb;
105831 int i;
105832 char *z, *zDb;
105833 Table *pTab;
105834 Index *pIdx;
105835 Token *pTableName;
105836 Vdbe *v;
105837
105838 /* Read the database schema. If an error occurs, leave an error message
105839 ** and code in pParse and return NULL. */
105840 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) )((void) (0));
105841 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
105842 return;
105843 }
105844
105845 assert( pName2!=0 || pName1==0 )((void) (0));
105846 if( pName1==0 ){
105847 /* Form 1: Analyze everything */
105848 for(i=0; i<db->nDb; i++){
105849 if( i==1 ) continue; /* Do not analyze the TEMP database */
105850 analyzeDatabase(pParse, i);
105851 }
105852 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
105853 /* Analyze the schema named as the argument */
105854 analyzeDatabase(pParse, iDb);
105855 }else{
105856 /* Form 3: Analyze the table or index named as an argument */
105857 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
105858 if( iDb>=0 ){
105859 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
105860 z = sqlite3NameFromToken(db, pTableName);
105861 if( z ){
105862 if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
105863 analyzeTable(pParse, pIdx->pTable, pIdx);
105864 }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
105865 analyzeTable(pParse, pTab, 0);
105866 }
105867 sqlite3DbFree(db, z);
105868 }
105869 }
105870 }
105871 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
105872 sqlite3VdbeAddOp0(v, OP_Expire158);
105873 }
105874}
105875
105876/*
105877** Used to pass information from the analyzer reader through to the
105878** callback routine.
105879*/
105880typedef struct analysisInfo analysisInfo;
105881struct analysisInfo {
105882 sqlite3 *db;
105883 const char *zDatabase;
105884};
105885
105886/*
105887** The first argument points to a nul-terminated string containing a
105888** list of space separated integers. Read the first nOut of these into
105889** the array aOut[].
105890*/
105891static void decodeIntArray(
105892 char *zIntArray, /* String containing int array to decode */
105893 int nOut, /* Number of slots in aOut[] */
105894 tRowcnt *aOut, /* Store integers here */
105895 LogEst *aLog, /* Or, if aOut==0, here */
105896 Index *pIndex /* Handle extra flags for this index, if not NULL */
105897){
105898 char *z = zIntArray;
105899 int c;
105900 int i;
105901 tRowcnt v;
105902
105903#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105904 if( z==0 ) z = "";
105905#else
105906 assert( z!=0 )((void) (0));
105907#endif
105908 for(i=0; *z && i<nOut; i++){
105909 v = 0;
105910 while( (c=z[0])>='0' && c<='9' ){
105911 v = v*10 + c - '0';
105912 z++;
105913 }
105914#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105915 if( aOut ) aOut[i] = v;
105916 if( aLog ) aLog[i] = sqlite3LogEst(v);
105917#else
105918 assert( aOut==0 )((void) (0));
105919 UNUSED_PARAMETER(aOut)(void)(aOut);
105920 assert( aLog!=0 )((void) (0));
105921 aLog[i] = sqlite3LogEst(v);
105922#endif
105923 if( *z==' ' ) z++;
105924 }
105925#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
105926 assert( pIndex!=0 )((void) (0)); {
105927#else
105928 if( pIndex ){
105929#endif
105930 pIndex->bUnordered = 0;
105931 pIndex->noSkipScan = 0;
105932 while( z[0] ){
105933 if( sqlite3_strglob("unordered*", z)==0 ){
105934 pIndex->bUnordered = 1;
105935 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
105936 pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
105937 }else if( sqlite3_strglob("noskipscan*", z)==0 ){
105938 pIndex->noSkipScan = 1;
105939 }
105940#ifdef SQLITE_ENABLE_COSTMULT
105941 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
105942 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
105943 }
105944#endif
105945 while( z[0]!=0 && z[0]!=' ' ) z++;
105946 while( z[0]==' ' ) z++;
105947 }
105948 }
105949}
105950
105951/*
105952** This callback is invoked once for each index when reading the
105953** sqlite_stat1 table.
105954**
105955** argv[0] = name of the table
105956** argv[1] = name of the index (might be NULL)
105957** argv[2] = results of analysis - on integer for each column
105958**
105959** Entries for which argv[1]==NULL simply record the number of rows in
105960** the table.
105961*/
105962static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
105963 analysisInfo *pInfo = (analysisInfo*)pData;
105964 Index *pIndex;
105965 Table *pTable;
105966 const char *z;
105967
105968 assert( argc==3 )((void) (0));
105969 UNUSED_PARAMETER2(NotUsed, argc)(void)(NotUsed),(void)(argc);
105970
105971 if( argv==0 || argv[0]==0 || argv[2]==0 ){
105972 return 0;
105973 }
105974 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
105975 if( pTable==0 ){
105976 return 0;
105977 }
105978 if( argv[1]==0 ){
105979 pIndex = 0;
105980 }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
105981 pIndex = sqlite3PrimaryKeyIndex(pTable);
105982 }else{
105983 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
105984 }
105985 z = argv[2];
105986
105987 if( pIndex ){
105988 tRowcnt *aiRowEst = 0;
105989 int nCol = pIndex->nKeyCol+1;
105990#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105991 /* Index.aiRowEst may already be set here if there are duplicate
105992 ** sqlite_stat1 entries for this index. In that case just clobber
105993 ** the old data with the new instead of allocating a new array. */
105994 if( pIndex->aiRowEst==0 ){
105995 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
105996 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
105997 }
105998 aiRowEst = pIndex->aiRowEst;
105999#endif
106000 pIndex->bUnordered = 0;
106001 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
106002 pIndex->hasStat1 = 1;
106003 if( pIndex->pPartIdxWhere==0 ){
106004 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
106005 pTable->tabFlags |= TF_HasStat10x0010;
106006 }
106007 }else{
106008 Index fakeIdx;
106009 fakeIdx.szIdxRow = pTable->szTabRow;
106010#ifdef SQLITE_ENABLE_COSTMULT
106011 fakeIdx.pTable = pTable;
106012#endif
106013 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
106014 pTable->szTabRow = fakeIdx.szIdxRow;
106015 pTable->tabFlags |= TF_HasStat10x0010;
106016 }
106017
106018 return 0;
106019}
106020
106021/*
106022** If the Index.aSample variable is not NULL, delete the aSample[] array
106023** and its contents.
106024*/
106025SQLITE_PRIVATEstatic void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
106026#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106027 if( pIdx->aSample ){
106028 int j;
106029 for(j=0; j<pIdx->nSample; j++){
106030 IndexSample *p = &pIdx->aSample[j];
106031 sqlite3DbFree(db, p->p);
106032 }
106033 sqlite3DbFree(db, pIdx->aSample);
106034 }
106035 if( db && db->pnBytesFreed==0 ){
106036 pIdx->nSample = 0;
106037 pIdx->aSample = 0;
106038 }
106039#else
106040 UNUSED_PARAMETER(db)(void)(db);
106041 UNUSED_PARAMETER(pIdx)(void)(pIdx);
106042#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
106043}
106044
106045#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106046/*
106047** Populate the pIdx->aAvgEq[] array based on the samples currently
106048** stored in pIdx->aSample[].
106049*/
106050static void initAvgEq(Index *pIdx){
106051 if( pIdx ){
106052 IndexSample *aSample = pIdx->aSample;
106053 IndexSample *pFinal = &aSample[pIdx->nSample-1];
106054 int iCol;
106055 int nCol = 1;
106056 if( pIdx->nSampleCol>1 ){
106057 /* If this is stat4 data, then calculate aAvgEq[] values for all
106058 ** sample columns except the last. The last is always set to 1, as
106059 ** once the trailing PK fields are considered all index keys are
106060 ** unique. */
106061 nCol = pIdx->nSampleCol-1;
106062 pIdx->aAvgEq[nCol] = 1;
106063 }
106064 for(iCol=0; iCol<nCol; iCol++){
106065 int nSample = pIdx->nSample;
106066 int i; /* Used to iterate through samples */
106067 tRowcnt sumEq = 0; /* Sum of the nEq values */
106068 tRowcnt avgEq = 0;
106069 tRowcnt nRow; /* Number of rows in index */
106070 i64 nSum100 = 0; /* Number of terms contributing to sumEq */
106071 i64 nDist100; /* Number of distinct values in index */
106072
106073 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
106074 nRow = pFinal->anLt[iCol];
106075 nDist100 = (i64)100 * pFinal->anDLt[iCol];
106076 nSample--;
106077 }else{
106078 nRow = pIdx->aiRowEst[0];
106079 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
106080 }
106081 pIdx->nRowEst0 = nRow;
106082
106083 /* Set nSum to the number of distinct (iCol+1) field prefixes that
106084 ** occur in the stat4 table for this index. Set sumEq to the sum of
106085 ** the nEq values for column iCol for the same set (adding the value
106086 ** only once where there exist duplicate prefixes). */
106087 for(i=0; i<nSample; i++){
106088 if( i==(pIdx->nSample-1)
106089 || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol]
106090 ){
106091 sumEq += aSample[i].anEq[iCol];
106092 nSum100 += 100;
106093 }
106094 }
106095
106096 if( nDist100>nSum100 && sumEq<nRow ){
106097 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
106098 }
106099 if( avgEq==0 ) avgEq = 1;
106100 pIdx->aAvgEq[iCol] = avgEq;
106101 }
106102 }
106103}
106104
106105/*
106106** Look up an index by name. Or, if the name of a WITHOUT ROWID table
106107** is supplied instead, find the PRIMARY KEY index for that table.
106108*/
106109static Index *findIndexOrPrimaryKey(
106110 sqlite3 *db,
106111 const char *zName,
106112 const char *zDb
106113){
106114 Index *pIdx = sqlite3FindIndex(db, zName, zDb);
106115 if( pIdx==0 ){
106116 Table *pTab = sqlite3FindTable(db, zName, zDb);
106117 if( pTab && !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
106118 }
106119 return pIdx;
106120}
106121
106122/*
106123** Load the content from either the sqlite_stat4 or sqlite_stat3 table
106124** into the relevant Index.aSample[] arrays.
106125**
106126** Arguments zSql1 and zSql2 must point to SQL statements that return
106127** data equivalent to the following (statements are different for stat3,
106128** see the caller of this function for details):
106129**
106130** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
106131** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
106132**
106133** where %Q is replaced with the database name before the SQL is executed.
106134*/
106135static int loadStatTbl(
106136 sqlite3 *db, /* Database handle */
106137 int bStat3, /* Assume single column records only */
106138 const char *zSql1, /* SQL statement 1 (see above) */
106139 const char *zSql2, /* SQL statement 2 (see above) */
106140 const char *zDb /* Database name (e.g. "main") */
106141){
106142 int rc; /* Result codes from subroutines */
106143 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
106144 char *zSql; /* Text of the SQL statement */
106145 Index *pPrevIdx = 0; /* Previous index in the loop */
106146 IndexSample *pSample; /* A slot in pIdx->aSample[] */
106147
106148 assert( db->lookaside.bDisable )((void) (0));
106149 zSql = sqlite3MPrintf(db, zSql1, zDb);
106150 if( !zSql ){
106151 return SQLITE_NOMEM_BKPT7;
106152 }
106153 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
106154 sqlite3DbFree(db, zSql);
106155 if( rc ) return rc;
106156
106157 while( sqlite3_step(pStmt)==SQLITE_ROW100 ){
106158 int nIdxCol = 1; /* Number of columns in stat4 records */
106159
106160 char *zIndex; /* Index name */
106161 Index *pIdx; /* Pointer to the index object */
106162 int nSample; /* Number of samples */
106163 int nByte; /* Bytes of space required */
106164 int i; /* Bytes of space required */
106165 tRowcnt *pSpace;
106166
106167 zIndex = (char *)sqlite3_column_text(pStmt, 0);
106168 if( zIndex==0 ) continue;
106169 nSample = sqlite3_column_int(pStmt, 1);
106170 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
106171 assert( pIdx==0 || bStat3 || pIdx->nSample==0 )((void) (0));
106172 /* Index.nSample is non-zero at this point if data has already been
106173 ** loaded from the stat4 table. In this case ignore stat3 data. */
106174 if( pIdx==0 || pIdx->nSample ) continue;
106175 if( bStat3==0 ){
106176 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 )((void) (0));
106177 if( !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x0020)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
106178 nIdxCol = pIdx->nKeyCol;
106179 }else{
106180 nIdxCol = pIdx->nColumn;
106181 }
106182 }
106183 pIdx->nSampleCol = nIdxCol;
106184 nByte = sizeof(IndexSample) * nSample;
106185 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
106186 nByte += nIdxCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
106187
106188 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
106189 if( pIdx->aSample==0 ){
106190 sqlite3_finalize(pStmt);
106191 return SQLITE_NOMEM_BKPT7;
106192 }
106193 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
106194 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
106195 for(i=0; i<nSample; i++){
106196 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
106197 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
106198 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
106199 }
106200 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) )((void) (0));
106201 }
106202 rc = sqlite3_finalize(pStmt);
106203 if( rc ) return rc;
106204
106205 zSql = sqlite3MPrintf(db, zSql2, zDb);
106206 if( !zSql ){
106207 return SQLITE_NOMEM_BKPT7;
106208 }
106209 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
106210 sqlite3DbFree(db, zSql);
106211 if( rc ) return rc;
106212
106213 while( sqlite3_step(pStmt)==SQLITE_ROW100 ){
106214 char *zIndex; /* Index name */
106215 Index *pIdx; /* Pointer to the index object */
106216 int nCol = 1; /* Number of columns in index */
106217
106218 zIndex = (char *)sqlite3_column_text(pStmt, 0);
106219 if( zIndex==0 ) continue;
106220 pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
106221 if( pIdx==0 ) continue;
106222 /* This next condition is true if data has already been loaded from
106223 ** the sqlite_stat4 table. In this case ignore stat3 data. */
106224 nCol = pIdx->nSampleCol;
106225 if( bStat3 && nCol>1 ) continue;
106226 if( pIdx!=pPrevIdx ){
106227 initAvgEq(pPrevIdx);
106228 pPrevIdx = pIdx;
106229 }
106230 pSample = &pIdx->aSample[pIdx->nSample];
106231 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
106232 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
106233 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
106234
106235 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
106236 ** This is in case the sample record is corrupted. In that case, the
106237 ** sqlite3VdbeRecordCompare() may read up to two varints past the
106238 ** end of the allocated buffer before it realizes it is dealing with
106239 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
106240 ** a buffer overread. */
106241 pSample->n = sqlite3_column_bytes(pStmt, 4);
106242 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
106243 if( pSample->p==0 ){
106244 sqlite3_finalize(pStmt);
106245 return SQLITE_NOMEM_BKPT7;
106246 }
106247 if( pSample->n ){
106248 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
106249 }
106250 pIdx->nSample++;
106251 }
106252 rc = sqlite3_finalize(pStmt);
106253 if( rc==SQLITE_OK0 ) initAvgEq(pPrevIdx);
106254 return rc;
106255}
106256
106257/*
106258** Load content from the sqlite_stat4 and sqlite_stat3 tables into
106259** the Index.aSample[] arrays of all indices.
106260*/
106261static int loadStat4(sqlite3 *db, const char *zDb){
106262 int rc = SQLITE_OK0; /* Result codes from subroutines */
106263
106264 assert( db->lookaside.bDisable )((void) (0));
106265 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
106266 rc = loadStatTbl(db, 0,
106267 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
106268 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
106269 zDb
106270 );
106271 }
106272
106273 if( rc==SQLITE_OK0 && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
106274 rc = loadStatTbl(db, 1,
106275 "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
106276 "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
106277 zDb
106278 );
106279 }
106280
106281 return rc;
106282}
106283#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
106284
106285/*
106286** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
106287** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
106288** arrays. The contents of sqlite_stat3/4 are used to populate the
106289** Index.aSample[] arrays.
106290**
106291** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
106292** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
106293** during compilation and the sqlite_stat3/4 table is present, no data is
106294** read from it.
106295**
106296** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
106297** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
106298** returned. However, in this case, data is read from the sqlite_stat1
106299** table (if it is present) before returning.
106300**
106301** If an OOM error occurs, this function always sets db->mallocFailed.
106302** This means if the caller does not care about other errors, the return
106303** code may be ignored.
106304*/
106305SQLITE_PRIVATEstatic int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
106306 analysisInfo sInfo;
106307 HashElem *i;
106308 char *zSql;
106309 int rc = SQLITE_OK0;
106310 Schema *pSchema = db->aDb[iDb].pSchema;
106311
106312 assert( iDb>=0 && iDb<db->nDb )((void) (0));
106313 assert( db->aDb[iDb].pBt!=0 )((void) (0));
106314
106315 /* Clear any prior statistics */
106316 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
106317 for(i=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); i; i=sqliteHashNext(i)((i)->next)){
106318 Table *pTab = sqliteHashData(i)((i)->data);
106319 pTab->tabFlags &= ~TF_HasStat10x0010;
106320 }
106321 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
106322 Index *pIdx = sqliteHashData(i)((i)->data);
106323 pIdx->hasStat1 = 0;
106324#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106325 sqlite3DeleteIndexSamples(db, pIdx);
106326 pIdx->aSample = 0;
106327#endif
106328 }
106329
106330 /* Load new statistics out of the sqlite_stat1 table */
106331 sInfo.db = db;
106332 sInfo.zDatabase = db->aDb[iDb].zDbSName;
106333 if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)!=0 ){
106334 zSql = sqlite3MPrintf(db,
106335 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
106336 if( zSql==0 ){
106337 rc = SQLITE_NOMEM_BKPT7;
106338 }else{
106339 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
106340 sqlite3DbFree(db, zSql);
106341 }
106342 }
106343
106344 /* Set appropriate defaults on all indexes not in the sqlite_stat1 table */
106345 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
106346 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
106347 Index *pIdx = sqliteHashData(i)((i)->data);
106348 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
106349 }
106350
106351 /* Load the statistics from the sqlite_stat4 table. */
106352#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106353 if( rc==SQLITE_OK0 ){
106354 db->lookaside.bDisable++;
106355 rc = loadStat4(db, sInfo.zDatabase);
106356 db->lookaside.bDisable--;
106357 }
106358 for(i=sqliteHashFirst(&pSchema->idxHash)((&pSchema->idxHash)->first); i; i=sqliteHashNext(i)((i)->next)){
106359 Index *pIdx = sqliteHashData(i)((i)->data);
106360 sqlite3_free(pIdx->aiRowEst);
106361 pIdx->aiRowEst = 0;
106362 }
106363#endif
106364
106365 if( rc==SQLITE_NOMEM7 ){
106366 sqlite3OomFault(db);
106367 }
106368 return rc;
106369}
106370
106371
106372#endif /* SQLITE_OMIT_ANALYZE */
106373
106374/************** End of analyze.c *********************************************/
106375/************** Begin file attach.c ******************************************/
106376/*
106377** 2003 April 6
106378**
106379** The author disclaims copyright to this source code. In place of
106380** a legal notice, here is a blessing:
106381**
106382** May you do good and not evil.
106383** May you find forgiveness for yourself and forgive others.
106384** May you share freely, never taking more than you give.
106385**
106386*************************************************************************
106387** This file contains code used to implement the ATTACH and DETACH commands.
106388*/
106389/* #include "sqliteInt.h" */
106390
106391#ifndef SQLITE_OMIT_ATTACH
106392/*
106393** Resolve an expression that was part of an ATTACH or DETACH statement. This
106394** is slightly different from resolving a normal SQL expression, because simple
106395** identifiers are treated as strings, not possible column names or aliases.
106396**
106397** i.e. if the parser sees:
106398**
106399** ATTACH DATABASE abc AS def
106400**
106401** it treats the two expressions as literal strings 'abc' and 'def' instead of
106402** looking for columns of the same name.
106403**
106404** This only applies to the root node of pExpr, so the statement:
106405**
106406** ATTACH DATABASE abc||def AS 'db2'
106407**
106408** will fail because neither abc or def can be resolved.
106409*/
106410static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
106411{
106412 int rc = SQLITE_OK0;
106413 if( pExpr ){
106414 if( pExpr->op!=TK_ID59 ){
106415 rc = sqlite3ResolveExprNames(pName, pExpr);
106416 }else{
106417 pExpr->op = TK_STRING110;
106418 }
106419 }
106420 return rc;
106421}
106422
106423/*
106424** An SQL user-function registered to do the work of an ATTACH statement. The
106425** three arguments to the function come directly from an attach statement:
106426**
106427** ATTACH DATABASE x AS y KEY z
106428**
106429** SELECT sqlite_attach(x, y, z)
106430**
106431** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
106432** third argument.
106433**
106434** If the db->init.reopenMemdb flags is set, then instead of attaching a
106435** new database, close the database on db->init.iDb and reopen it as an
106436** empty MemDB.
106437*/
106438static void attachFunc(
106439 sqlite3_context *context,
106440 int NotUsed,
106441 sqlite3_value **argv
106442){
106443 int i;
106444 int rc = 0;
106445 sqlite3 *db = sqlite3_context_db_handle(context);
106446 const char *zName;
106447 const char *zFile;
106448 char *zPath = 0;
106449 char *zErr = 0;
106450 unsigned int flags;
106451 Db *aNew; /* New array of Db pointers */
106452 Db *pNew; /* Db object for the newly attached database */
106453 char *zErrDyn = 0;
106454 sqlite3_vfs *pVfs;
106455
106456 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
106457 zFile = (const char *)sqlite3_value_text(argv[0]);
106458 zName = (const char *)sqlite3_value_text(argv[1]);
106459 if( zFile==0 ) zFile = "";
106460 if( zName==0 ) zName = "";
106461
106462#ifdef SQLITE_ENABLE_DESERIALIZE
106463# define REOPEN_AS_MEMDB(db)(0) (db->init.reopenMemdb)
106464#else
106465# define REOPEN_AS_MEMDB(db)(0) (0)
106466#endif
106467
106468 if( REOPEN_AS_MEMDB(db)(0) ){
106469 /* This is not a real ATTACH. Instead, this routine is being called
106470 ** from sqlite3_deserialize() to close database db->init.iDb and
106471 ** reopen it as a MemDB */
106472 pVfs = sqlite3_vfs_find("memdb");
106473 if( pVfs==0 ) return;
106474 pNew = &db->aDb[db->init.iDb];
106475 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
106476 pNew->pBt = 0;
106477 pNew->pSchema = 0;
106478 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB0x00000100);
106479 }else{
106480 /* This is a real ATTACH
106481 **
106482 ** Check for the following errors:
106483 **
106484 ** * Too many attached databases,
106485 ** * Transaction currently open
106486 ** * Specified database name already being used.
106487 */
106488 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED7]+2 ){
106489 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
106490 db->aLimit[SQLITE_LIMIT_ATTACHED7]
106491 );
106492 goto attach_error;
106493 }
106494 for(i=0; i<db->nDb; i++){
106495 char *z = db->aDb[i].zDbSName;
106496 assert( z && zName )((void) (0));
106497 if( sqlite3StrICmp(z, zName)==0 ){
106498 zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
106499 goto attach_error;
106500 }
106501 }
106502
106503 /* Allocate the new entry in the db->aDb[] array and initialize the schema
106504 ** hash tables.
106505 */
106506 if( db->aDb==db->aDbStatic ){
106507 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
106508 if( aNew==0 ) return;
106509 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
106510 }else{
106511 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
106512 if( aNew==0 ) return;
106513 }
106514 db->aDb = aNew;
106515 pNew = &db->aDb[db->nDb];
106516 memset(pNew, 0, sizeof(*pNew));
106517
106518 /* Open the database file. If the btree is successfully opened, use
106519 ** it to obtain the database schema. At this point the schema may
106520 ** or may not be initialized.
106521 */
106522 flags = db->openFlags;
106523 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
106524 if( rc!=SQLITE_OK0 ){
106525 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
106526 sqlite3_result_error(context, zErr, -1);
106527 sqlite3_free(zErr);
106528 return;
106529 }
106530 assert( pVfs )((void) (0));
106531 flags |= SQLITE_OPEN_MAIN_DB0x00000100;
106532 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
106533 db->nDb++;
106534 pNew->zDbSName = sqlite3DbStrDup(db, zName);
106535 }
106536 db->noSharedCache = 0;
106537 if( rc==SQLITE_CONSTRAINT19 ){
106538 rc = SQLITE_ERROR1;
106539 zErrDyn = sqlite3MPrintf(db, "database is already attached");
106540 }else if( rc==SQLITE_OK0 ){
106541 Pager *pPager;
106542 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
106543 if( !pNew->pSchema ){
106544 rc = SQLITE_NOMEM_BKPT7;
106545 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db)((db)->enc) ){
106546 zErrDyn = sqlite3MPrintf(db,
106547 "attached databases must use the same text encoding as main database");
106548 rc = SQLITE_ERROR1;
106549 }
106550 sqlite3BtreeEnter(pNew->pBt);
106551 pPager = sqlite3BtreePager(pNew->pBt);
106552 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
106553 sqlite3BtreeSecureDelete(pNew->pBt,
106554 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
106555#ifndef SQLITE_OMIT_PAGER_PRAGMAS
106556 sqlite3BtreeSetPagerFlags(pNew->pBt,
106557 PAGER_SYNCHRONOUS_FULL0x03 | (db->flags & PAGER_FLAGS_MASK0x38));
106558#endif
106559 sqlite3BtreeLeave(pNew->pBt);
106560 }
106561 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS2+1;
106562 if( rc==SQLITE_OK0 && pNew->zDbSName==0 ){
106563 rc = SQLITE_NOMEM_BKPT7;
106564 }
106565
106566
106567#ifdef SQLITE_HAS_CODEC
106568 if( rc==SQLITE_OK0 ){
106569 extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
106570 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
106571 int nKey;
106572 char *zKey;
106573 int t = sqlite3_value_type(argv[2]);
106574 switch( t ){
106575 case SQLITE_INTEGER1:
106576 case SQLITE_FLOAT2:
106577 zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
106578 rc = SQLITE_ERROR1;
106579 break;
106580
106581 case SQLITE_TEXT3:
106582 case SQLITE_BLOB4:
106583 nKey = sqlite3_value_bytes(argv[2]);
106584 zKey = (char *)sqlite3_value_blob(argv[2]);
106585 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
106586 break;
106587
106588 case SQLITE_NULL5:
106589 /* No key specified. Use the key from URI filename, or if none,
106590 ** use the key from the main database. */
106591 if( sqlite3CodecQueryParameters(db, zName, zPath)0==0 ){
106592 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
106593 if( nKey || sqlite3BtreeGetOptimalReserve(db->aDb[0].pBt)>0 ){
106594 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
106595 }
106596 }
106597 break;
106598 }
106599 }
106600#endif
106601 sqlite3_free( zPath );
106602
106603 /* If the file was opened successfully, read the schema for the new database.
106604 ** If this fails, or if opening the file failed, then close the file and
106605 ** remove the entry from the db->aDb[] array. i.e. put everything back the
106606 ** way we found it.
106607 */
106608 if( rc==SQLITE_OK0 ){
106609 sqlite3BtreeEnterAll(db);
106610 db->init.iDb = 0;
106611 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk0x0010);
106612 if( !REOPEN_AS_MEMDB(db)(0) ){
106613 rc = sqlite3Init(db, &zErrDyn);
106614 }
106615 sqlite3BtreeLeaveAll(db);
106616 assert( zErrDyn==0 || rc!=SQLITE_OK )((void) (0));
106617 }
106618#ifdef SQLITE_USER_AUTHENTICATION
106619 if( rc==SQLITE_OK0 && !REOPEN_AS_MEMDB(db)(0) ){
106620 u8 newAuth = 0;
106621 rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
106622 if( newAuth<db->auth.authLevel ){
106623 rc = SQLITE_AUTH_USER(23 | (1<<8));
106624 }
106625 }
106626#endif
106627 if( rc ){
106628 if( !REOPEN_AS_MEMDB(db)(0) ){
106629 int iDb = db->nDb - 1;
106630 assert( iDb>=2 )((void) (0));
106631 if( db->aDb[iDb].pBt ){
106632 sqlite3BtreeClose(db->aDb[iDb].pBt);
106633 db->aDb[iDb].pBt = 0;
106634 db->aDb[iDb].pSchema = 0;
106635 }
106636 sqlite3ResetAllSchemasOfConnection(db);
106637 db->nDb = iDb;
106638 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
106639 sqlite3OomFault(db);
106640 sqlite3DbFree(db, zErrDyn);
106641 zErrDyn = sqlite3MPrintf(db, "out of memory");
106642 }else if( zErrDyn==0 ){
106643 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
106644 }
106645 }
106646 goto attach_error;
106647 }
106648
106649 return;
106650
106651attach_error:
106652 /* Return an error if we get here */
106653 if( zErrDyn ){
106654 sqlite3_result_error(context, zErrDyn, -1);
106655 sqlite3DbFree(db, zErrDyn);
106656 }
106657 if( rc ) sqlite3_result_error_code(context, rc);
106658}
106659
106660/*
106661** An SQL user-function registered to do the work of an DETACH statement. The
106662** three arguments to the function come directly from a detach statement:
106663**
106664** DETACH DATABASE x
106665**
106666** SELECT sqlite_detach(x)
106667*/
106668static void detachFunc(
106669 sqlite3_context *context,
106670 int NotUsed,
106671 sqlite3_value **argv
106672){
106673 const char *zName = (const char *)sqlite3_value_text(argv[0]);
106674 sqlite3 *db = sqlite3_context_db_handle(context);
106675 int i;
106676 Db *pDb = 0;
106677 char zErr[128];
106678
106679 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
106680
106681 if( zName==0 ) zName = "";
106682 for(i=0; i<db->nDb; i++){
106683 pDb = &db->aDb[i];
106684 if( pDb->pBt==0 ) continue;
106685 if( sqlite3StrICmp(pDb->zDbSName, zName)==0 ) break;
106686 }
106687
106688 if( i>=db->nDb ){
106689 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
106690 goto detach_error;
106691 }
106692 if( i<2 ){
106693 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
106694 goto detach_error;
106695 }
106696 if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
106697 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
106698 goto detach_error;
106699 }
106700
106701 sqlite3BtreeClose(pDb->pBt);
106702 pDb->pBt = 0;
106703 pDb->pSchema = 0;
106704 sqlite3CollapseDatabaseArray(db);
106705 return;
106706
106707detach_error:
106708 sqlite3_result_error(context, zErr, -1);
106709}
106710
106711/*
106712** This procedure generates VDBE code for a single invocation of either the
106713** sqlite_detach() or sqlite_attach() SQL user functions.
106714*/
106715static void codeAttach(
106716 Parse *pParse, /* The parser context */
106717 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
106718 FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
106719 Expr *pAuthArg, /* Expression to pass to authorization callback */
106720 Expr *pFilename, /* Name of database file */
106721 Expr *pDbname, /* Name of the database to use internally */
106722 Expr *pKey /* Database key for encryption extension */
106723){
106724 int rc;
106725 NameContext sName;
106726 Vdbe *v;
106727 sqlite3* db = pParse->db;
106728 int regArgs;
106729
106730 if( pParse->nErr ) goto attach_end;
106731 memset(&sName, 0, sizeof(NameContext));
106732 sName.pParse = pParse;
106733
106734 if(
106735 SQLITE_OK0!=(rc = resolveAttachExpr(&sName, pFilename)) ||
106736 SQLITE_OK0!=(rc = resolveAttachExpr(&sName, pDbname)) ||
106737 SQLITE_OK0!=(rc = resolveAttachExpr(&sName, pKey))
106738 ){
106739 goto attach_end;
106740 }
106741
106742#ifndef SQLITE_OMIT_AUTHORIZATION
106743 if( pAuthArg ){
106744 char *zAuthArg;
106745 if( pAuthArg->op==TK_STRING110 ){
106746 zAuthArg = pAuthArg->u.zToken;
106747 }else{
106748 zAuthArg = 0;
106749 }
106750 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
106751 if(rc!=SQLITE_OK0 ){
106752 goto attach_end;
106753 }
106754 }
106755#endif /* SQLITE_OMIT_AUTHORIZATION */
106756
106757
106758 v = sqlite3GetVdbe(pParse);
106759 regArgs = sqlite3GetTempRange(pParse, 4);
106760 sqlite3ExprCode(pParse, pFilename, regArgs);
106761 sqlite3ExprCode(pParse, pDbname, regArgs+1);
106762 sqlite3ExprCode(pParse, pKey, regArgs+2);
106763
106764 assert( v || db->mallocFailed )((void) (0));
106765 if( v ){
106766 sqlite3VdbeAddOp4(v, OP_Function063, 0, regArgs+3-pFunc->nArg, regArgs+3,
106767 (char *)pFunc, P4_FUNCDEF(-8));
106768 assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg )((void) (0));
106769 sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
106770
106771 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
106772 ** statement only). For DETACH, set it to false (expire all existing
106773 ** statements).
106774 */
106775 sqlite3VdbeAddOp1(v, OP_Expire158, (type==SQLITE_ATTACH24));
106776 }
106777
106778attach_end:
106779 sqlite3ExprDelete(db, pFilename);
106780 sqlite3ExprDelete(db, pDbname);
106781 sqlite3ExprDelete(db, pKey);
106782}
106783
106784/*
106785** Called by the parser to compile a DETACH statement.
106786**
106787** DETACH pDbname
106788*/
106789SQLITE_PRIVATEstatic void sqlite3Detach(Parse *pParse, Expr *pDbname){
106790 static const FuncDef detach_func = {
106791 1, /* nArg */
106792 SQLITE_UTF81, /* funcFlags */
106793 0, /* pUserData */
106794 0, /* pNext */
106795 detachFunc, /* xSFunc */
106796 0, /* xFinalize */
106797 0, 0, /* xValue, xInverse */
106798 "sqlite_detach", /* zName */
106799 {0}
106800 };
106801 codeAttach(pParse, SQLITE_DETACH25, &detach_func, pDbname, 0, 0, pDbname);
106802}
106803
106804/*
106805** Called by the parser to compile an ATTACH statement.
106806**
106807** ATTACH p AS pDbname KEY pKey
106808*/
106809SQLITE_PRIVATEstatic void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
106810 static const FuncDef attach_func = {
106811 3, /* nArg */
106812 SQLITE_UTF81, /* funcFlags */
106813 0, /* pUserData */
106814 0, /* pNext */
106815 attachFunc, /* xSFunc */
106816 0, /* xFinalize */
106817 0, 0, /* xValue, xInverse */
106818 "sqlite_attach", /* zName */
106819 {0}
106820 };
106821 codeAttach(pParse, SQLITE_ATTACH24, &attach_func, p, p, pDbname, pKey);
106822}
106823#endif /* SQLITE_OMIT_ATTACH */
106824
106825/*
106826** Initialize a DbFixer structure. This routine must be called prior
106827** to passing the structure to one of the sqliteFixAAAA() routines below.
106828*/
106829SQLITE_PRIVATEstatic void sqlite3FixInit(
106830 DbFixer *pFix, /* The fixer to be initialized */
106831 Parse *pParse, /* Error messages will be written here */
106832 int iDb, /* This is the database that must be used */
106833 const char *zType, /* "view", "trigger", or "index" */
106834 const Token *pName /* Name of the view, trigger, or index */
106835){
106836 sqlite3 *db;
106837
106838 db = pParse->db;
106839 assert( db->nDb>iDb )((void) (0));
106840 pFix->pParse = pParse;
106841 pFix->zDb = db->aDb[iDb].zDbSName;
106842 pFix->pSchema = db->aDb[iDb].pSchema;
106843 pFix->zType = zType;
106844 pFix->pName = pName;
106845 pFix->bVarOnly = (iDb==1);
106846}
106847
106848/*
106849** The following set of routines walk through the parse tree and assign
106850** a specific database to all table references where the database name
106851** was left unspecified in the original SQL statement. The pFix structure
106852** must have been initialized by a prior call to sqlite3FixInit().
106853**
106854** These routines are used to make sure that an index, trigger, or
106855** view in one database does not refer to objects in a different database.
106856** (Exception: indices, triggers, and views in the TEMP database are
106857** allowed to refer to anything.) If a reference is explicitly made
106858** to an object in a different database, an error message is added to
106859** pParse->zErrMsg and these routines return non-zero. If everything
106860** checks out, these routines return 0.
106861*/
106862SQLITE_PRIVATEstatic int sqlite3FixSrcList(
106863 DbFixer *pFix, /* Context of the fixation */
106864 SrcList *pList /* The Source list to check and modify */
106865){
106866 int i;
106867 const char *zDb;
106868 struct SrcList_item *pItem;
106869
106870 if( NEVER(pList==0)(pList==0) ) return 0;
106871 zDb = pFix->zDb;
106872 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
106873 if( pFix->bVarOnly==0 ){
106874 if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
106875 sqlite3ErrorMsg(pFix->pParse,
106876 "%s %T cannot reference objects in database %s",
106877 pFix->zType, pFix->pName, pItem->zDatabase);
106878 return 1;
106879 }
106880 sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
106881 pItem->zDatabase = 0;
106882 pItem->pSchema = pFix->pSchema;
106883 }
106884#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
106885 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
106886 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
106887#endif
106888 if( pItem->fg.isTabFunc && sqlite3FixExprList(pFix, pItem->u1.pFuncArg) ){
106889 return 1;
106890 }
106891 }
106892 return 0;
106893}
106894#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
106895SQLITE_PRIVATEstatic int sqlite3FixSelect(
106896 DbFixer *pFix, /* Context of the fixation */
106897 Select *pSelect /* The SELECT statement to be fixed to one database */
106898){
106899 while( pSelect ){
106900 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
106901 return 1;
106902 }
106903 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
106904 return 1;
106905 }
106906 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
106907 return 1;
106908 }
106909 if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
106910 return 1;
106911 }
106912 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
106913 return 1;
106914 }
106915 if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
106916 return 1;
106917 }
106918 if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
106919 return 1;
106920 }
106921 if( pSelect->pWith ){
106922 int i;
106923 for(i=0; i<pSelect->pWith->nCte; i++){
106924 if( sqlite3FixSelect(pFix, pSelect->pWith->a[i].pSelect) ){
106925 return 1;
106926 }
106927 }
106928 }
106929 pSelect = pSelect->pPrior;
106930 }
106931 return 0;
106932}
106933SQLITE_PRIVATEstatic int sqlite3FixExpr(
106934 DbFixer *pFix, /* Context of the fixation */
106935 Expr *pExpr /* The expression to be fixed to one database */
106936){
106937 while( pExpr ){
106938 if( pExpr->op==TK_VARIABLE148 ){
106939 if( pFix->pParse->db->init.busy ){
106940 pExpr->op = TK_NULL114;
106941 }else{
106942 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
106943 return 1;
106944 }
106945 }
106946 if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf)(((pExpr)->flags&(0x004000|0x800000))!=0) ) break;
106947 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
106948 if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
106949 }else{
106950 if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
106951 }
106952 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
106953 return 1;
106954 }
106955 pExpr = pExpr->pLeft;
106956 }
106957 return 0;
106958}
106959SQLITE_PRIVATEstatic int sqlite3FixExprList(
106960 DbFixer *pFix, /* Context of the fixation */
106961 ExprList *pList /* The expression to be fixed to one database */
106962){
106963 int i;
106964 struct ExprList_item *pItem;
106965 if( pList==0 ) return 0;
106966 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
106967 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
106968 return 1;
106969 }
106970 }
106971 return 0;
106972}
106973#endif
106974
106975#ifndef SQLITE_OMIT_TRIGGER
106976SQLITE_PRIVATEstatic int sqlite3FixTriggerStep(
106977 DbFixer *pFix, /* Context of the fixation */
106978 TriggerStep *pStep /* The trigger step be fixed to one database */
106979){
106980 while( pStep ){
106981 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
106982 return 1;
106983 }
106984 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
106985 return 1;
106986 }
106987 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
106988 return 1;
106989 }
106990#ifndef SQLITE_OMIT_UPSERT
106991 if( pStep->pUpsert ){
106992 Upsert *pUp = pStep->pUpsert;
106993 if( sqlite3FixExprList(pFix, pUp->pUpsertTarget)
106994 || sqlite3FixExpr(pFix, pUp->pUpsertTargetWhere)
106995 || sqlite3FixExprList(pFix, pUp->pUpsertSet)
106996 || sqlite3FixExpr(pFix, pUp->pUpsertWhere)
106997 ){
106998 return 1;
106999 }
107000 }
107001#endif
107002 pStep = pStep->pNext;
107003 }
107004 return 0;
107005}
107006#endif
107007
107008/************** End of attach.c **********************************************/
107009/************** Begin file auth.c ********************************************/
107010/*
107011** 2003 January 11
107012**
107013** The author disclaims copyright to this source code. In place of
107014** a legal notice, here is a blessing:
107015**
107016** May you do good and not evil.
107017** May you find forgiveness for yourself and forgive others.
107018** May you share freely, never taking more than you give.
107019**
107020*************************************************************************
107021** This file contains code used to implement the sqlite3_set_authorizer()
107022** API. This facility is an optional feature of the library. Embedded
107023** systems that do not need this facility may omit it by recompiling
107024** the library with -DSQLITE_OMIT_AUTHORIZATION=1
107025*/
107026/* #include "sqliteInt.h" */
107027
107028/*
107029** All of the code in this file may be omitted by defining a single
107030** macro.
107031*/
107032#ifndef SQLITE_OMIT_AUTHORIZATION
107033
107034/*
107035** Set or clear the access authorization function.
107036**
107037** The access authorization function is be called during the compilation
107038** phase to verify that the user has read and/or write access permission on
107039** various fields of the database. The first argument to the auth function
107040** is a copy of the 3rd argument to this routine. The second argument
107041** to the auth function is one of these constants:
107042**
107043** SQLITE_CREATE_INDEX
107044** SQLITE_CREATE_TABLE
107045** SQLITE_CREATE_TEMP_INDEX
107046** SQLITE_CREATE_TEMP_TABLE
107047** SQLITE_CREATE_TEMP_TRIGGER
107048** SQLITE_CREATE_TEMP_VIEW
107049** SQLITE_CREATE_TRIGGER
107050** SQLITE_CREATE_VIEW
107051** SQLITE_DELETE
107052** SQLITE_DROP_INDEX
107053** SQLITE_DROP_TABLE
107054** SQLITE_DROP_TEMP_INDEX
107055** SQLITE_DROP_TEMP_TABLE
107056** SQLITE_DROP_TEMP_TRIGGER
107057** SQLITE_DROP_TEMP_VIEW
107058** SQLITE_DROP_TRIGGER
107059** SQLITE_DROP_VIEW
107060** SQLITE_INSERT
107061** SQLITE_PRAGMA
107062** SQLITE_READ
107063** SQLITE_SELECT
107064** SQLITE_TRANSACTION
107065** SQLITE_UPDATE
107066**
107067** The third and fourth arguments to the auth function are the name of
107068** the table and the column that are being accessed. The auth function
107069** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If
107070** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY
107071** means that the SQL statement will never-run - the sqlite3_exec() call
107072** will return with an error. SQLITE_IGNORE means that the SQL statement
107073** should run but attempts to read the specified column will return NULL
107074** and attempts to write the column will be ignored.
107075**
107076** Setting the auth function to NULL disables this hook. The default
107077** setting of the auth function is NULL.
107078*/
107079SQLITE_API int sqlite3_set_authorizer(
107080 sqlite3 *db,
107081 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
107082 void *pArg
107083){
107084#ifdef SQLITE_ENABLE_API_ARMOR
107085 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(107085);
107086#endif
107087 sqlite3_mutex_enter(db->mutex);
107088 db->xAuth = (sqlite3_xauth)xAuth;
107089 db->pAuthArg = pArg;
107090 sqlite3ExpirePreparedStatements(db, 0);
107091 sqlite3_mutex_leave(db->mutex);
107092 return SQLITE_OK0;
107093}
107094
107095/*
107096** Write an error message into pParse->zErrMsg that explains that the
107097** user-supplied authorization function returned an illegal value.
107098*/
107099static void sqliteAuthBadReturnCode(Parse *pParse){
107100 sqlite3ErrorMsg(pParse, "authorizer malfunction");
107101 pParse->rc = SQLITE_ERROR1;
107102}
107103
107104/*
107105** Invoke the authorization callback for permission to read column zCol from
107106** table zTab in database zDb. This function assumes that an authorization
107107** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
107108**
107109** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
107110** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
107111** is treated as SQLITE_DENY. In this case an error is left in pParse.
107112*/
107113SQLITE_PRIVATEstatic int sqlite3AuthReadCol(
107114 Parse *pParse, /* The parser context */
107115 const char *zTab, /* Table name */
107116 const char *zCol, /* Column name */
107117 int iDb /* Index of containing database. */
107118){
107119 sqlite3 *db = pParse->db; /* Database handle */
107120 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
107121 int rc; /* Auth callback return code */
107122
107123 if( db->init.busy ) return SQLITE_OK0;
107124 rc = db->xAuth(db->pAuthArg, SQLITE_READ20, zTab,zCol,zDb,pParse->zAuthContext
107125#ifdef SQLITE_USER_AUTHENTICATION
107126 ,db->auth.zAuthUser
107127#endif
107128 );
107129 if( rc==SQLITE_DENY1 ){
107130 char *z = sqlite3_mprintf("%s.%s", zTab, zCol);
107131 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
107132 sqlite3ErrorMsg(pParse, "access to %z is prohibited", z);
107133 pParse->rc = SQLITE_AUTH23;
107134 }else if( rc!=SQLITE_IGNORE2 && rc!=SQLITE_OK0 ){
107135 sqliteAuthBadReturnCode(pParse);
107136 }
107137 return rc;
107138}
107139
107140/*
107141** The pExpr should be a TK_COLUMN expression. The table referred to
107142** is in pTabList or else it is the NEW or OLD table of a trigger.
107143** Check to see if it is OK to read this particular column.
107144**
107145** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
107146** instruction into a TK_NULL. If the auth function returns SQLITE_DENY,
107147** then generate an error.
107148*/
107149SQLITE_PRIVATEstatic void sqlite3AuthRead(
107150 Parse *pParse, /* The parser context */
107151 Expr *pExpr, /* The expression to check authorization on */
107152 Schema *pSchema, /* The schema of the expression */
107153 SrcList *pTabList /* All table that pExpr might refer to */
107154){
107155 sqlite3 *db = pParse->db;
107156 Table *pTab = 0; /* The table being read */
107157 const char *zCol; /* Name of the column of the table */
107158 int iSrc; /* Index in pTabList->a[] of table being read */
107159 int iDb; /* The index of the database the expression refers to */
107160 int iCol; /* Index of column in table */
107161
107162 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER )((void) (0));
107163 assert( !IN_RENAME_OBJECT || db->xAuth==0 )((void) (0));
107164 if( db->xAuth==0 ) return;
107165 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
107166 if( iDb<0 ){
107167 /* An attempt to read a column out of a subquery or other
107168 ** temporary table. */
107169 return;
107170 }
107171
107172 if( pExpr->op==TK_TRIGGER77 ){
107173 pTab = pParse->pTriggerTab;
107174 }else{
107175 assert( pTabList )((void) (0));
107176 for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc)(iSrc<pTabList->nSrc); iSrc++){
107177 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
107178 pTab = pTabList->a[iSrc].pTab;
107179 break;
107180 }
107181 }
107182 }
107183 iCol = pExpr->iColumn;
107184 if( NEVER(pTab==0)(pTab==0) ) return;
107185
107186 if( iCol>=0 ){
107187 assert( iCol<pTab->nCol )((void) (0));
107188 zCol = pTab->aCol[iCol].zName;
107189 }else if( pTab->iPKey>=0 ){
107190 assert( pTab->iPKey<pTab->nCol )((void) (0));
107191 zCol = pTab->aCol[pTab->iPKey].zName;
107192 }else{
107193 zCol = "ROWID";
107194 }
107195 assert( iDb>=0 && iDb<db->nDb )((void) (0));
107196 if( SQLITE_IGNORE2==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
107197 pExpr->op = TK_NULL114;
107198 }
107199}
107200
107201/*
107202** Do an authorization check using the code and arguments given. Return
107203** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY
107204** is returned, then the error count and error message in pParse are
107205** modified appropriately.
107206*/
107207SQLITE_PRIVATEstatic int sqlite3AuthCheck(
107208 Parse *pParse,
107209 int code,
107210 const char *zArg1,
107211 const char *zArg2,
107212 const char *zArg3
107213){
107214 sqlite3 *db = pParse->db;
107215 int rc;
107216
107217 /* Don't do any authorization checks if the database is initialising
107218 ** or if the parser is being invoked from within sqlite3_declare_vtab.
107219 */
107220 assert( !IN_RENAME_OBJECT || db->xAuth==0 )((void) (0));
107221 if( db->init.busy || IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
107222 return SQLITE_OK0;
107223 }
107224
107225 if( db->xAuth==0 ){
107226 return SQLITE_OK0;
107227 }
107228
107229 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
107230 ** callback are either NULL pointers or zero-terminated strings that
107231 ** contain additional details about the action to be authorized.
107232 **
107233 ** The following testcase() macros show that any of the 3rd through 6th
107234 ** parameters can be either NULL or a string. */
107235 testcase( zArg1==0 );
107236 testcase( zArg2==0 );
107237 testcase( zArg3==0 );
107238 testcase( pParse->zAuthContext==0 );
107239
107240 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
107241#ifdef SQLITE_USER_AUTHENTICATION
107242 ,db->auth.zAuthUser
107243#endif
107244 );
107245 if( rc==SQLITE_DENY1 ){
107246 sqlite3ErrorMsg(pParse, "not authorized");
107247 pParse->rc = SQLITE_AUTH23;
107248 }else if( rc!=SQLITE_OK0 && rc!=SQLITE_IGNORE2 ){
107249 rc = SQLITE_DENY1;
107250 sqliteAuthBadReturnCode(pParse);
107251 }
107252 return rc;
107253}
107254
107255/*
107256** Push an authorization context. After this routine is called, the
107257** zArg3 argument to authorization callbacks will be zContext until
107258** popped. Or if pParse==0, this routine is a no-op.
107259*/
107260SQLITE_PRIVATEstatic void sqlite3AuthContextPush(
107261 Parse *pParse,
107262 AuthContext *pContext,
107263 const char *zContext
107264){
107265 assert( pParse )((void) (0));
107266 pContext->pParse = pParse;
107267 pContext->zAuthContext = pParse->zAuthContext;
107268 pParse->zAuthContext = zContext;
107269}
107270
107271/*
107272** Pop an authorization context that was previously pushed
107273** by sqlite3AuthContextPush
107274*/
107275SQLITE_PRIVATEstatic void sqlite3AuthContextPop(AuthContext *pContext){
107276 if( pContext->pParse ){
107277 pContext->pParse->zAuthContext = pContext->zAuthContext;
107278 pContext->pParse = 0;
107279 }
107280}
107281
107282#endif /* SQLITE_OMIT_AUTHORIZATION */
107283
107284/************** End of auth.c ************************************************/
107285/************** Begin file build.c *******************************************/
107286/*
107287** 2001 September 15
107288**
107289** The author disclaims copyright to this source code. In place of
107290** a legal notice, here is a blessing:
107291**
107292** May you do good and not evil.
107293** May you find forgiveness for yourself and forgive others.
107294** May you share freely, never taking more than you give.
107295**
107296*************************************************************************
107297** This file contains C code routines that are called by the SQLite parser
107298** when syntax rules are reduced. The routines in this file handle the
107299** following kinds of SQL syntax:
107300**
107301** CREATE TABLE
107302** DROP TABLE
107303** CREATE INDEX
107304** DROP INDEX
107305** creating ID lists
107306** BEGIN TRANSACTION
107307** COMMIT
107308** ROLLBACK
107309*/
107310/* #include "sqliteInt.h" */
107311
107312#ifndef SQLITE_OMIT_SHARED_CACHE
107313/*
107314** The TableLock structure is only used by the sqlite3TableLock() and
107315** codeTableLocks() functions.
107316*/
107317struct TableLock {
107318 int iDb; /* The database containing the table to be locked */
107319 int iTab; /* The root page of the table to be locked */
107320 u8 isWriteLock; /* True for write lock. False for a read lock */
107321 const char *zLockName; /* Name of the table */
107322};
107323
107324/*
107325** Record the fact that we want to lock a table at run-time.
107326**
107327** The table to be locked has root page iTab and is found in database iDb.
107328** A read or a write lock can be taken depending on isWritelock.
107329**
107330** This routine just records the fact that the lock is desired. The
107331** code to make the lock occur is generated by a later call to
107332** codeTableLocks() which occurs during sqlite3FinishCoding().
107333*/
107334SQLITE_PRIVATEstatic void sqlite3TableLock(
107335 Parse *pParse, /* Parsing context */
107336 int iDb, /* Index of the database containing the table to lock */
107337 int iTab, /* Root page number of the table to be locked */
107338 u8 isWriteLock, /* True for a write lock */
107339 const char *zName /* Name of the table to be locked */
107340){
107341 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
107342 int i;
107343 int nBytes;
107344 TableLock *p;
107345 assert( iDb>=0 )((void) (0));
107346
107347 if( iDb==1 ) return;
107348 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
107349 for(i=0; i<pToplevel->nTableLock; i++){
107350 p = &pToplevel->aTableLock[i];
107351 if( p->iDb==iDb && p->iTab==iTab ){
107352 p->isWriteLock = (p->isWriteLock || isWriteLock);
107353 return;
107354 }
107355 }
107356
107357 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
107358 pToplevel->aTableLock =
107359 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
107360 if( pToplevel->aTableLock ){
107361 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
107362 p->iDb = iDb;
107363 p->iTab = iTab;
107364 p->isWriteLock = isWriteLock;
107365 p->zLockName = zName;
107366 }else{
107367 pToplevel->nTableLock = 0;
107368 sqlite3OomFault(pToplevel->db);
107369 }
107370}
107371
107372/*
107373** Code an OP_TableLock instruction for each table locked by the
107374** statement (configured by calls to sqlite3TableLock()).
107375*/
107376static void codeTableLocks(Parse *pParse){
107377 int i;
107378 Vdbe *pVdbe;
107379
107380 pVdbe = sqlite3GetVdbe(pParse);
107381 assert( pVdbe!=0 )((void) (0)); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
107382
107383 for(i=0; i<pParse->nTableLock; i++){
107384 TableLock *p = &pParse->aTableLock[i];
107385 int p1 = p->iDb;
107386 sqlite3VdbeAddOp4(pVdbe, OP_TableLock159, p1, p->iTab, p->isWriteLock,
107387 p->zLockName, P4_STATIC(-1));
107388 }
107389}
107390#else
107391 #define codeTableLocks(x)
107392#endif
107393
107394/*
107395** Return TRUE if the given yDbMask object is empty - if it contains no
107396** 1 bits. This routine is used by the DbMaskAllZero() and DbMaskNotZero()
107397** macros when SQLITE_MAX_ATTACHED is greater than 30.
107398*/
107399#if SQLITE_MAX_ATTACHED10>30
107400SQLITE_PRIVATEstatic int sqlite3DbMaskAllZero(yDbMask m){
107401 int i;
107402 for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
107403 return 1;
107404}
107405#endif
107406
107407/*
107408** This routine is called after a single SQL statement has been
107409** parsed and a VDBE program to execute that statement has been
107410** prepared. This routine puts the finishing touches on the
107411** VDBE program and resets the pParse structure for the next
107412** parse.
107413**
107414** Note that if an error occurred, it might be the case that
107415** no VDBE code was generated.
107416*/
107417SQLITE_PRIVATEstatic void sqlite3FinishCoding(Parse *pParse){
107418 sqlite3 *db;
107419 Vdbe *v;
107420
107421 assert( pParse->pToplevel==0 )((void) (0));
107422 db = pParse->db;
107423 if( pParse->nested ) return;
107424 if( db->mallocFailed || pParse->nErr ){
107425 if( pParse->rc==SQLITE_OK0 ) pParse->rc = SQLITE_ERROR1;
107426 return;
107427 }
107428
107429 /* Begin by generating some termination code at the end of the
107430 ** vdbe program
107431 */
107432 v = sqlite3GetVdbe(pParse);
107433 assert( !pParse->isMultiWrite((void) (0))
107434 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort))((void) (0));
107435 if( v ){
107436 sqlite3VdbeAddOp0(v, OP_Halt69);
107437
107438#if SQLITE_USER_AUTHENTICATION
107439 if( pParse->nTableLock>0 && db->init.busy==0 ){
107440 sqlite3UserAuthInit(db);
107441 if( db->auth.authLevel<UAUTH_User ){
107442 sqlite3ErrorMsg(pParse, "user not authenticated");
107443 pParse->rc = SQLITE_AUTH_USER(23 | (1<<8));
107444 return;
107445 }
107446 }
107447#endif
107448
107449 /* The cookie mask contains one bit for each database file open.
107450 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
107451 ** set for each database that is used. Generate code to start a
107452 ** transaction on each used database and to verify the schema cookie
107453 ** on each used database.
107454 */
107455 if( db->mallocFailed==0
107456 && (DbMaskNonZero(pParse->cookieMask)(pParse->cookieMask)!=0 || pParse->pConstExpr)
107457 ){
107458 int iDb, i;
107459 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init )((void) (0));
107460 sqlite3VdbeJumpHere(v, 0);
107461 for(iDb=0; iDb<db->nDb; iDb++){
107462 Schema *pSchema;
107463 if( DbMaskTest(pParse->cookieMask, iDb)(((pParse->cookieMask)&(((yDbMask)1)<<(iDb)))!=0
)
==0 ) continue;
107464 sqlite3VdbeUsesBtree(v, iDb);
107465 pSchema = db->aDb[iDb].pSchema;
107466 sqlite3VdbeAddOp4Int(v,
107467 OP_Transaction2, /* Opcode */
107468 iDb, /* P1 */
107469 DbMaskTest(pParse->writeMask,iDb)(((pParse->writeMask)&(((yDbMask)1)<<(iDb)))!=0), /* P2 */
107470 pSchema->schema_cookie, /* P3 */
107471 pSchema->iGeneration /* P4 */
107472 );
107473 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
107474 VdbeComment((v,
107475 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
107476 }
107477#ifndef SQLITE_OMIT_VIRTUALTABLE
107478 for(i=0; i<pParse->nVtabLock; i++){
107479 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
107480 sqlite3VdbeAddOp4(v, OP_VBegin160, 0, 0, 0, vtab, P4_VTAB(-12));
107481 }
107482 pParse->nVtabLock = 0;
107483#endif
107484
107485 /* Once all the cookies have been verified and transactions opened,
107486 ** obtain the required table-locks. This is a no-op unless the
107487 ** shared-cache feature is enabled.
107488 */
107489 codeTableLocks(pParse);
107490
107491 /* Initialize any AUTOINCREMENT data structures required.
107492 */
107493 sqlite3AutoincrementBegin(pParse);
107494
107495 /* Code constant expressions that where factored out of inner loops */
107496 if( pParse->pConstExpr ){
107497 ExprList *pEL = pParse->pConstExpr;
107498 pParse->okConstFactor = 0;
107499 for(i=0; i<pEL->nExpr; i++){
107500 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
107501 }
107502 }
107503
107504 /* Finally, jump back to the beginning of the executable code. */
107505 sqlite3VdbeGoto(v, 1);
107506 }
107507 }
107508
107509
107510 /* Get the VDBE program ready for execution
107511 */
107512 if( v && pParse->nErr==0 && !db->mallocFailed ){
107513 /* A minimum of one cursor is required if autoincrement is used
107514 * See ticket [a696379c1f08866] */
107515 assert( pParse->pAinc==0 || pParse->nTab>0 )((void) (0));
107516 sqlite3VdbeMakeReady(v, pParse);
107517 pParse->rc = SQLITE_DONE101;
107518 }else{
107519 pParse->rc = SQLITE_ERROR1;
107520 }
107521}
107522
107523/*
107524** Run the parser and code generator recursively in order to generate
107525** code for the SQL statement given onto the end of the pParse context
107526** currently under construction. When the parser is run recursively
107527** this way, the final OP_Halt is not appended and other initialization
107528** and finalization steps are omitted because those are handling by the
107529** outermost parser.
107530**
107531** Not everything is nestable. This facility is designed to permit
107532** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
107533** care if you decide to try to use this routine for some other purposes.
107534*/
107535SQLITE_PRIVATEstatic void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
107536 va_list ap;
107537 char *zSql;
107538 char *zErrMsg = 0;
107539 sqlite3 *db = pParse->db;
107540 char saveBuf[PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken))];
107541
107542 if( pParse->nErr ) return;
107543 assert( pParse->nested<10 )((void) (0)); /* Nesting should only be of limited depth */
107544 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
107545 zSql = sqlite3VMPrintf(db, zFormat, ap);
107546 va_end(ap)__builtin_va_end(ap);
107547 if( zSql==0 ){
107548 /* This can result either from an OOM or because the formatted string
107549 ** exceeds SQLITE_LIMIT_LENGTH. In the latter case, we need to set
107550 ** an error */
107551 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG18;
107552 pParse->nErr++;
107553 return;
107554 }
107555 pParse->nested++;
107556 memcpy(saveBuf, PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
107557 memset(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
107558 sqlite3RunParser(pParse, zSql, &zErrMsg);
107559 sqlite3DbFree(db, zErrMsg);
107560 sqlite3DbFree(db, zSql);
107561 memcpy(PARSE_TAIL(pParse)(((char*)(pParse))+__builtin_offsetof(Parse, sLastToken)), saveBuf, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
107562 pParse->nested--;
107563}
107564
107565#if SQLITE_USER_AUTHENTICATION
107566/*
107567** Return TRUE if zTable is the name of the system table that stores the
107568** list of users and their access credentials.
107569*/
107570SQLITE_PRIVATEstatic int sqlite3UserAuthTable(const char *zTable){
107571 return sqlite3_stricmp(zTable, "sqlite_user")==0;
107572}
107573#endif
107574
107575/*
107576** Locate the in-memory structure that describes a particular database
107577** table given the name of that table and (optionally) the name of the
107578** database containing the table. Return NULL if not found.
107579**
107580** If zDatabase is 0, all databases are searched for the table and the
107581** first matching table is returned. (No checking for duplicate table
107582** names is done.) The search order is TEMP first, then MAIN, then any
107583** auxiliary databases added using the ATTACH command.
107584**
107585** See also sqlite3LocateTable().
107586*/
107587SQLITE_PRIVATEstatic Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
107588 Table *p = 0;
107589 int i;
107590
107591 /* All mutexes are required for schema access. Make sure we hold them. */
107592 assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
107593#if SQLITE_USER_AUTHENTICATION
107594 /* Only the admin user is allowed to know that the sqlite_user table
107595 ** exists */
107596 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
107597 return 0;
107598 }
107599#endif
107600 while(1){
107601 for(i=OMIT_TEMPDB0; i<db->nDb; i++){
107602 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
107603 if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
107604 assert( sqlite3SchemaMutexHeld(db, j, 0) )((void) (0));
107605 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
107606 if( p ) return p;
107607 }
107608 }
107609 /* Not found. If the name we were looking for was temp.sqlite_master
107610 ** then change the name to sqlite_temp_master and try again. */
107611 if( sqlite3StrICmp(zName, MASTER_NAME"sqlite_master")!=0 ) break;
107612 if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break;
107613 zName = TEMP_MASTER_NAME"sqlite_temp_master";
107614 }
107615 return 0;
107616}
107617
107618/*
107619** Locate the in-memory structure that describes a particular database
107620** table given the name of that table and (optionally) the name of the
107621** database containing the table. Return NULL if not found. Also leave an
107622** error message in pParse->zErrMsg.
107623**
107624** The difference between this routine and sqlite3FindTable() is that this
107625** routine leaves an error message in pParse->zErrMsg where
107626** sqlite3FindTable() does not.
107627*/
107628SQLITE_PRIVATEstatic Table *sqlite3LocateTable(
107629 Parse *pParse, /* context in which to report errors */
107630 u32 flags, /* LOCATE_VIEW or LOCATE_NOERR */
107631 const char *zName, /* Name of the table we are looking for */
107632 const char *zDbase /* Name of the database. Might be NULL */
107633){
107634 Table *p;
107635 sqlite3 *db = pParse->db;
107636
107637 /* Read the database schema. If an error occurs, leave an error message
107638 ** and code in pParse and return NULL. */
107639 if( (db->mDbFlags & DBFLAG_SchemaKnownOk0x0010)==0
107640 && SQLITE_OK0!=sqlite3ReadSchema(pParse)
107641 ){
107642 return 0;
107643 }
107644
107645 p = sqlite3FindTable(db, zName, zDbase);
107646 if( p==0 ){
107647#ifndef SQLITE_OMIT_VIRTUALTABLE
107648 /* If zName is the not the name of a table in the schema created using
107649 ** CREATE, then check to see if it is the name of an virtual table that
107650 ** can be an eponymous virtual table. */
107651 if( pParse->disableVtab==0 ){
107652 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
107653 if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
107654 pMod = sqlite3PragmaVtabRegister(db, zName);
107655 }
107656 if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
107657 return pMod->pEpoTab;
107658 }
107659 }
107660#endif
107661 if( flags & LOCATE_NOERR0x02 ) return 0;
107662 pParse->checkSchema = 1;
107663 }else if( IsVirtual(p)((p)->nModuleArg) && pParse->disableVtab ){
107664 p = 0;
107665 }
107666
107667 if( p==0 ){
107668 const char *zMsg = flags & LOCATE_VIEW0x01 ? "no such view" : "no such table";
107669 if( zDbase ){
107670 sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
107671 }else{
107672 sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
107673 }
107674 }
107675
107676 return p;
107677}
107678
107679/*
107680** Locate the table identified by *p.
107681**
107682** This is a wrapper around sqlite3LocateTable(). The difference between
107683** sqlite3LocateTable() and this function is that this function restricts
107684** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
107685** non-NULL if it is part of a view or trigger program definition. See
107686** sqlite3FixSrcList() for details.
107687*/
107688SQLITE_PRIVATEstatic Table *sqlite3LocateTableItem(
107689 Parse *pParse,
107690 u32 flags,
107691 struct SrcList_item *p
107692){
107693 const char *zDb;
107694 assert( p->pSchema==0 || p->zDatabase==0 )((void) (0));
107695 if( p->pSchema ){
107696 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
107697 zDb = pParse->db->aDb[iDb].zDbSName;
107698 }else{
107699 zDb = p->zDatabase;
107700 }
107701 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
107702}
107703
107704/*
107705** Locate the in-memory structure that describes
107706** a particular index given the name of that index
107707** and the name of the database that contains the index.
107708** Return NULL if not found.
107709**
107710** If zDatabase is 0, all databases are searched for the
107711** table and the first matching index is returned. (No checking
107712** for duplicate index names is done.) The search order is
107713** TEMP first, then MAIN, then any auxiliary databases added
107714** using the ATTACH command.
107715*/
107716SQLITE_PRIVATEstatic Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
107717 Index *p = 0;
107718 int i;
107719 /* All mutexes are required for schema access. Make sure we hold them. */
107720 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
107721 for(i=OMIT_TEMPDB0; i<db->nDb; i++){
107722 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
107723 Schema *pSchema = db->aDb[j].pSchema;
107724 assert( pSchema )((void) (0));
107725 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
107726 assert( sqlite3SchemaMutexHeld(db, j, 0) )((void) (0));
107727 p = sqlite3HashFind(&pSchema->idxHash, zName);
107728 if( p ) break;
107729 }
107730 return p;
107731}
107732
107733/*
107734** Reclaim the memory used by an index
107735*/
107736SQLITE_PRIVATEstatic void sqlite3FreeIndex(sqlite3 *db, Index *p){
107737#ifndef SQLITE_OMIT_ANALYZE
107738 sqlite3DeleteIndexSamples(db, p);
107739#endif
107740 sqlite3ExprDelete(db, p->pPartIdxWhere);
107741 sqlite3ExprListDelete(db, p->aColExpr);
107742 sqlite3DbFree(db, p->zColAff);
107743 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
107744#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
107745 sqlite3_free(p->aiRowEst);
107746#endif
107747 sqlite3DbFree(db, p);
107748}
107749
107750/*
107751** For the index called zIdxName which is found in the database iDb,
107752** unlike that index from its Table then remove the index from
107753** the index hash table and free all memory structures associated
107754** with the index.
107755*/
107756SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
107757 Index *pIndex;
107758 Hash *pHash;
107759
107760 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
107761 pHash = &db->aDb[iDb].pSchema->idxHash;
107762 pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
107763 if( ALWAYS(pIndex)(pIndex) ){
107764 if( pIndex->pTable->pIndex==pIndex ){
107765 pIndex->pTable->pIndex = pIndex->pNext;
107766 }else{
107767 Index *p;
107768 /* Justification of ALWAYS(); The index must be on the list of
107769 ** indices. */
107770 p = pIndex->pTable->pIndex;
107771 while( ALWAYS(p)(p) && p->pNext!=pIndex ){ p = p->pNext; }
107772 if( ALWAYS(p && p->pNext==pIndex)(p && p->pNext==pIndex) ){
107773 p->pNext = pIndex->pNext;
107774 }
107775 }
107776 sqlite3FreeIndex(db, pIndex);
107777 }
107778 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
107779}
107780
107781/*
107782** Look through the list of open database files in db->aDb[] and if
107783** any have been closed, remove them from the list. Reallocate the
107784** db->aDb[] structure to a smaller size, if possible.
107785**
107786** Entry 0 (the "main" database) and entry 1 (the "temp" database)
107787** are never candidates for being collapsed.
107788*/
107789SQLITE_PRIVATEstatic void sqlite3CollapseDatabaseArray(sqlite3 *db){
107790 int i, j;
107791 for(i=j=2; i<db->nDb; i++){
107792 struct Db *pDb = &db->aDb[i];
107793 if( pDb->pBt==0 ){
107794 sqlite3DbFree(db, pDb->zDbSName);
107795 pDb->zDbSName = 0;
107796 continue;
107797 }
107798 if( j<i ){
107799 db->aDb[j] = db->aDb[i];
107800 }
107801 j++;
107802 }
107803 db->nDb = j;
107804 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
107805 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
107806 sqlite3DbFree(db, db->aDb);
107807 db->aDb = db->aDbStatic;
107808 }
107809}
107810
107811/*
107812** Reset the schema for the database at index iDb. Also reset the
107813** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
107814** Deferred resets may be run by calling with iDb<0.
107815*/
107816SQLITE_PRIVATEstatic void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
107817 int i;
107818 assert( iDb<db->nDb )((void) (0));
107819
107820 if( iDb>=0 ){
107821 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
107822 DbSetProperty(db, iDb, DB_ResetWanted)(db)->aDb[iDb].pSchema->schemaFlags|=(0x0008);
107823 DbSetProperty(db, 1, DB_ResetWanted)(db)->aDb[1].pSchema->schemaFlags|=(0x0008);
107824 db->mDbFlags &= ~DBFLAG_SchemaKnownOk0x0010;
107825 }
107826
107827 if( db->nSchemaLock==0 ){
107828 for(i=0; i<db->nDb; i++){
107829 if( DbHasProperty(db, i, DB_ResetWanted)(((db)->aDb[i].pSchema->schemaFlags&(0x0008))==(0x0008
))
){
107830 sqlite3SchemaClear(db->aDb[i].pSchema);
107831 }
107832 }
107833 }
107834}
107835
107836/*
107837** Erase all schema information from all attached databases (including
107838** "main" and "temp") for a single database connection.
107839*/
107840SQLITE_PRIVATEstatic void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
107841 int i;
107842 sqlite3BtreeEnterAll(db);
107843 for(i=0; i<db->nDb; i++){
107844 Db *pDb = &db->aDb[i];
107845 if( pDb->pSchema ){
107846 if( db->nSchemaLock==0 ){
107847 sqlite3SchemaClear(pDb->pSchema);
107848 }else{
107849 DbSetProperty(db, i, DB_ResetWanted)(db)->aDb[i].pSchema->schemaFlags|=(0x0008);
107850 }
107851 }
107852 }
107853 db->mDbFlags &= ~(DBFLAG_SchemaChange0x0001|DBFLAG_SchemaKnownOk0x0010);
107854 sqlite3VtabUnlockList(db);
107855 sqlite3BtreeLeaveAll(db);
107856 if( db->nSchemaLock==0 ){
107857 sqlite3CollapseDatabaseArray(db);
107858 }
107859}
107860
107861/*
107862** This routine is called when a commit occurs.
107863*/
107864SQLITE_PRIVATEstatic void sqlite3CommitInternalChanges(sqlite3 *db){
107865 db->mDbFlags &= ~DBFLAG_SchemaChange0x0001;
107866}
107867
107868/*
107869** Delete memory allocated for the column names of a table or view (the
107870** Table.aCol[] array).
107871*/
107872SQLITE_PRIVATEstatic void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
107873 int i;
107874 Column *pCol;
107875 assert( pTable!=0 )((void) (0));
107876 if( (pCol = pTable->aCol)!=0 ){
107877 for(i=0; i<pTable->nCol; i++, pCol++){
107878 sqlite3DbFree(db, pCol->zName);
107879 sqlite3ExprDelete(db, pCol->pDflt);
107880 sqlite3DbFree(db, pCol->zColl);
107881 }
107882 sqlite3DbFree(db, pTable->aCol);
107883 }
107884}
107885
107886/*
107887** Remove the memory data structures associated with the given
107888** Table. No changes are made to disk by this routine.
107889**
107890** This routine just deletes the data structure. It does not unlink
107891** the table data structure from the hash table. But it does destroy
107892** memory structures of the indices and foreign keys associated with
107893** the table.
107894**
107895** The db parameter is optional. It is needed if the Table object
107896** contains lookaside memory. (Table objects in the schema do not use
107897** lookaside memory, but some ephemeral Table objects do.) Or the
107898** db parameter can be used with db->pnBytesFreed to measure the memory
107899** used by the Table object.
107900*/
107901static void SQLITE_NOINLINE__attribute__((noinline)) deleteTable(sqlite3 *db, Table *pTable){
107902 Index *pIndex, *pNext;
107903
107904#ifdef SQLITE_DEBUG
107905 /* Record the number of outstanding lookaside allocations in schema Tables
107906 ** prior to doing any free() operations. Since schema Tables do not use
107907 ** lookaside, this number should not change.
107908 **
107909 ** If malloc has already failed, it may be that it failed while allocating
107910 ** a Table object that was going to be marked ephemeral. So do not check
107911 ** that no lookaside memory is used in this case either. */
107912 int nLookaside = 0;
107913 if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral0x0002)==0 ){
107914 nLookaside = sqlite3LookasideUsed(db, 0);
107915 }
107916#endif
107917
107918 /* Delete all indices associated with this table. */
107919 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
107920 pNext = pIndex->pNext;
107921 assert( pIndex->pSchema==pTable->pSchema((void) (0))
107922 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) )((void) (0));
107923 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable)((pTable)->nModuleArg) ){
107924 char *zName = pIndex->zName;
107925 TESTONLY ( Index *pOld = ) sqlite3HashInsert(
107926 &pIndex->pSchema->idxHash, zName, 0
107927 );
107928 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) )((void) (0));
107929 assert( pOld==pIndex || pOld==0 )((void) (0));
107930 }
107931 sqlite3FreeIndex(db, pIndex);
107932 }
107933
107934 /* Delete any foreign keys attached to this table. */
107935 sqlite3FkDelete(db, pTable);
107936
107937 /* Delete the Table structure itself.
107938 */
107939 sqlite3DeleteColumnNames(db, pTable);
107940 sqlite3DbFree(db, pTable->zName);
107941 sqlite3DbFree(db, pTable->zColAff);
107942 sqlite3SelectDelete(db, pTable->pSelect);
107943 sqlite3ExprListDelete(db, pTable->pCheck);
107944#ifndef SQLITE_OMIT_VIRTUALTABLE
107945 sqlite3VtabClear(db, pTable);
107946#endif
107947 sqlite3DbFree(db, pTable);
107948
107949 /* Verify that no lookaside memory was used by schema tables */
107950 assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) )((void) (0));
107951}
107952SQLITE_PRIVATEstatic void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
107953 /* Do not delete the table until the reference count reaches zero. */
107954 if( !pTable ) return;
107955 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
107956 deleteTable(db, pTable);
107957}
107958
107959
107960/*
107961** Unlink the given table from the hash tables and the delete the
107962** table structure with all its indices and foreign keys.
107963*/
107964SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
107965 Table *p;
107966 Db *pDb;
107967
107968 assert( db!=0 )((void) (0));
107969 assert( iDb>=0 && iDb<db->nDb )((void) (0));
107970 assert( zTabName )((void) (0));
107971 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
107972 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
107973 pDb = &db->aDb[iDb];
107974 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
107975 sqlite3DeleteTable(db, p);
107976 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
107977}
107978
107979/*
107980** Given a token, return a string that consists of the text of that
107981** token. Space to hold the returned string
107982** is obtained from sqliteMalloc() and must be freed by the calling
107983** function.
107984**
107985** Any quotation marks (ex: "name", 'name', [name], or `name`) that
107986** surround the body of the token are removed.
107987**
107988** Tokens are often just pointers into the original SQL text and so
107989** are not \000 terminated and are not persistent. The returned string
107990** is \000 terminated and is persistent.
107991*/
107992SQLITE_PRIVATEstatic char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
107993 char *zName;
107994 if( pName ){
107995 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
107996 sqlite3Dequote(zName);
107997 }else{
107998 zName = 0;
107999 }
108000 return zName;
108001}
108002
108003/*
108004** Open the sqlite_master table stored in database number iDb for
108005** writing. The table is opened using cursor 0.
108006*/
108007SQLITE_PRIVATEstatic void sqlite3OpenMasterTable(Parse *p, int iDb){
108008 Vdbe *v = sqlite3GetVdbe(p);
108009 sqlite3TableLock(p, iDb, MASTER_ROOT1, 1, MASTER_NAME"sqlite_master");
108010 sqlite3VdbeAddOp4Int(v, OP_OpenWrite109, 0, MASTER_ROOT1, iDb, 5);
108011 if( p->nTab==0 ){
108012 p->nTab = 1;
108013 }
108014}
108015
108016/*
108017** Parameter zName points to a nul-terminated buffer containing the name
108018** of a database ("main", "temp" or the name of an attached db). This
108019** function returns the index of the named database in db->aDb[], or
108020** -1 if the named db cannot be found.
108021*/
108022SQLITE_PRIVATEstatic int sqlite3FindDbName(sqlite3 *db, const char *zName){
108023 int i = -1; /* Database number */
108024 if( zName ){
108025 Db *pDb;
108026 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
108027 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
108028 /* "main" is always an acceptable alias for the primary database
108029 ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
108030 if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
108031 }
108032 }
108033 return i;
108034}
108035
108036/*
108037** The token *pName contains the name of a database (either "main" or
108038** "temp" or the name of an attached db). This routine returns the
108039** index of the named database in db->aDb[], or -1 if the named db
108040** does not exist.
108041*/
108042SQLITE_PRIVATEstatic int sqlite3FindDb(sqlite3 *db, Token *pName){
108043 int i; /* Database number */
108044 char *zName; /* Name we are searching for */
108045 zName = sqlite3NameFromToken(db, pName);
108046 i = sqlite3FindDbName(db, zName);
108047 sqlite3DbFree(db, zName);
108048 return i;
108049}
108050
108051/* The table or view or trigger name is passed to this routine via tokens
108052** pName1 and pName2. If the table name was fully qualified, for example:
108053**
108054** CREATE TABLE xxx.yyy (...);
108055**
108056** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
108057** the table name is not fully qualified, i.e.:
108058**
108059** CREATE TABLE yyy(...);
108060**
108061** Then pName1 is set to "yyy" and pName2 is "".
108062**
108063** This routine sets the *ppUnqual pointer to point at the token (pName1 or
108064** pName2) that stores the unqualified table name. The index of the
108065** database "xxx" is returned.
108066*/
108067SQLITE_PRIVATEstatic int sqlite3TwoPartName(
108068 Parse *pParse, /* Parsing and code generating context */
108069 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
108070 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
108071 Token **pUnqual /* Write the unqualified object name here */
108072){
108073 int iDb; /* Database holding the object */
108074 sqlite3 *db = pParse->db;
108075
108076 assert( pName2!=0 )((void) (0));
108077 if( pName2->n>0 ){
108078 if( db->init.busy ) {
108079 sqlite3ErrorMsg(pParse, "corrupt database");
108080 return -1;
108081 }
108082 *pUnqual = pName2;
108083 iDb = sqlite3FindDb(db, pName1);
108084 if( iDb<0 ){
108085 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
108086 return -1;
108087 }
108088 }else{
108089 assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT((void) (0))
108090 || (db->mDbFlags & DBFLAG_Vacuum)!=0)((void) (0));
108091 iDb = db->init.iDb;
108092 *pUnqual = pName1;
108093 }
108094 return iDb;
108095}
108096
108097/*
108098** True if PRAGMA writable_schema is ON
108099*/
108100SQLITE_PRIVATEstatic int sqlite3WritableSchema(sqlite3 *db){
108101 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
108102 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
108103 SQLITE_WriteSchema );
108104 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
108105 SQLITE_Defensive );
108106 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
108107 (SQLITE_WriteSchema|SQLITE_Defensive) );
108108 return (db->flags&(SQLITE_WriteSchema0x00000001|SQLITE_Defensive0x10000000))==SQLITE_WriteSchema0x00000001;
108109}
108110
108111/*
108112** This routine is used to check if the UTF-8 string zName is a legal
108113** unqualified name for a new schema object (table, index, view or
108114** trigger). All names are legal except those that begin with the string
108115** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
108116** is reserved for internal use.
108117*/
108118SQLITE_PRIVATEstatic int sqlite3CheckObjectName(Parse *pParse, const char *zName){
108119 if( !pParse->db->init.busy && pParse->nested==0
108120 && sqlite3WritableSchema(pParse->db)==0
108121 && 0==sqlite3StrNICmpsqlite3_strnicmp(zName, "sqlite_", 7) ){
108122 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
108123 return SQLITE_ERROR1;
108124 }
108125 return SQLITE_OK0;
108126}
108127
108128/*
108129** Return the PRIMARY KEY index of a table
108130*/
108131SQLITE_PRIVATEstatic Index *sqlite3PrimaryKeyIndex(Table *pTab){
108132 Index *p;
108133 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p)((p)->idxType==2); p=p->pNext){}
108134 return p;
108135}
108136
108137/*
108138** Return the column of index pIdx that corresponds to table
108139** column iCol. Return -1 if not found.
108140*/
108141SQLITE_PRIVATEstatic i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
108142 int i;
108143 for(i=0; i<pIdx->nColumn; i++){
108144 if( iCol==pIdx->aiColumn[i] ) return i;
108145 }
108146 return -1;
108147}
108148
108149/*
108150** Begin constructing a new table representation in memory. This is
108151** the first of several action routines that get called in response
108152** to a CREATE TABLE statement. In particular, this routine is called
108153** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
108154** flag is true if the table should be stored in the auxiliary database
108155** file instead of in the main database file. This is normally the case
108156** when the "TEMP" or "TEMPORARY" keyword occurs in between
108157** CREATE and TABLE.
108158**
108159** The new table record is initialized and put in pParse->pNewTable.
108160** As more of the CREATE TABLE statement is parsed, additional action
108161** routines will be called to add more information to this record.
108162** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
108163** is called to complete the construction of the new table record.
108164*/
108165SQLITE_PRIVATEstatic void sqlite3StartTable(
108166 Parse *pParse, /* Parser context */
108167 Token *pName1, /* First part of the name of the table or view */
108168 Token *pName2, /* Second part of the name of the table or view */
108169 int isTemp, /* True if this is a TEMP table */
108170 int isView, /* True if this is a VIEW */
108171 int isVirtual, /* True if this is a VIRTUAL table */
108172 int noErr /* Do nothing if table already exists */
108173){
108174 Table *pTable;
108175 char *zName = 0; /* The name of the new table */
108176 sqlite3 *db = pParse->db;
108177 Vdbe *v;
108178 int iDb; /* Database number to create the table in */
108179 Token *pName; /* Unqualified name of the table to create */
108180
108181 if( db->init.busy && db->init.newTnum==1 ){
108182 /* Special case: Parsing the sqlite_master or sqlite_temp_master schema */
108183 iDb = db->init.iDb;
108184 zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master"));
108185 pName = pName1;
108186 }else{
108187 /* The common case */
108188 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
108189 if( iDb<0 ) return;
108190 if( !OMIT_TEMPDB0 && isTemp && pName2->n>0 && iDb!=1 ){
108191 /* If creating a temp table, the name may not be qualified. Unless
108192 ** the database name is "temp" anyway. */
108193 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
108194 return;
108195 }
108196 if( !OMIT_TEMPDB0 && isTemp ) iDb = 1;
108197 zName = sqlite3NameFromToken(db, pName);
108198 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
108199 sqlite3RenameTokenMap(pParse, (void*)zName, pName);
108200 }
108201 }
108202 pParse->sNameToken = *pName;
108203 if( zName==0 ) return;
108204 if( SQLITE_OK0!=sqlite3CheckObjectName(pParse, zName) ){
108205 goto begin_table_error;
108206 }
108207 if( db->init.iDb==1 ) isTemp = 1;
108208#ifndef SQLITE_OMIT_AUTHORIZATION
108209 assert( isTemp==0 || isTemp==1 )((void) (0));
108210 assert( isView==0 || isView==1 )((void) (0));
108211 {
108212 static const u8 aCode[] = {
108213 SQLITE_CREATE_TABLE2,
108214 SQLITE_CREATE_TEMP_TABLE4,
108215 SQLITE_CREATE_VIEW8,
108216 SQLITE_CREATE_TEMP_VIEW6
108217 };
108218 char *zDb = db->aDb[iDb].zDbSName;
108219 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(isTemp)((!0)&&(isTemp==1)?"sqlite_temp_master":"sqlite_master"
)
, 0, zDb) ){
108220 goto begin_table_error;
108221 }
108222 if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
108223 zName, 0, zDb) ){
108224 goto begin_table_error;
108225 }
108226 }
108227#endif
108228
108229 /* Make sure the new table name does not collide with an existing
108230 ** index or table name in the same database. Issue an error message if
108231 ** it does. The exception is if the statement being parsed was passed
108232 ** to an sqlite3_declare_vtab() call. In that case only the column names
108233 ** and types will be used, so there is no need to test for namespace
108234 ** collisions.
108235 */
108236 if( !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
108237 char *zDb = db->aDb[iDb].zDbSName;
108238 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
108239 goto begin_table_error;
108240 }
108241 pTable = sqlite3FindTable(db, zName, zDb);
108242 if( pTable ){
108243 if( !noErr ){
108244 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
108245 }else{
108246 assert( !db->init.busy || CORRUPT_DB )((void) (0));
108247 sqlite3CodeVerifySchema(pParse, iDb);
108248 }
108249 goto begin_table_error;
108250 }
108251 if( sqlite3FindIndex(db, zName, zDb)!=0 ){
108252 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
108253 goto begin_table_error;
108254 }
108255 }
108256
108257 pTable = sqlite3DbMallocZero(db, sizeof(Table));
108258 if( pTable==0 ){
108259 assert( db->mallocFailed )((void) (0));
108260 pParse->rc = SQLITE_NOMEM_BKPT7;
108261 pParse->nErr++;
108262 goto begin_table_error;
108263 }
108264 pTable->zName = zName;
108265 pTable->iPKey = -1;
108266 pTable->pSchema = db->aDb[iDb].pSchema;
108267 pTable->nTabRef = 1;
108268#ifdef SQLITE_DEFAULT_ROWEST
108269 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
108270#else
108271 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
108272#endif
108273 assert( pParse->pNewTable==0 )((void) (0));
108274 pParse->pNewTable = pTable;
108275
108276 /* If this is the magic sqlite_sequence table used by autoincrement,
108277 ** then record a pointer to this table in the main database structure
108278 ** so that INSERT can find the table easily.
108279 */
108280#ifndef SQLITE_OMIT_AUTOINCREMENT
108281 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
108282 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
108283 pTable->pSchema->pSeqTab = pTable;
108284 }
108285#endif
108286
108287 /* Begin generating the code that will insert the table record into
108288 ** the SQLITE_MASTER table. Note in particular that we must go ahead
108289 ** and allocate the record number for the table entry now. Before any
108290 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
108291 ** indices to be created and the table record must come before the
108292 ** indices. Hence, the record number for the table must be allocated
108293 ** now.
108294 */
108295 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
108296 int addr1;
108297 int fileFormat;
108298 int reg1, reg2, reg3;
108299 /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
108300 static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
108301 sqlite3BeginWriteOperation(pParse, 1, iDb);
108302
108303#ifndef SQLITE_OMIT_VIRTUALTABLE
108304 if( isVirtual ){
108305 sqlite3VdbeAddOp0(v, OP_VBegin160);
108306 }
108307#endif
108308
108309 /* If the file format and encoding in the database have not been set,
108310 ** set them now.
108311 */
108312 reg1 = pParse->regRowid = ++pParse->nMem;
108313 reg2 = pParse->regRoot = ++pParse->nMem;
108314 reg3 = ++pParse->nMem;
108315 sqlite3VdbeAddOp3(v, OP_ReadCookie94, iDb, reg3, BTREE_FILE_FORMAT2);
108316 sqlite3VdbeUsesBtree(v, iDb);
108317 addr1 = sqlite3VdbeAddOp1(v, OP_If18, reg3); VdbeCoverage(v);
108318 fileFormat = (db->flags & SQLITE_LegacyFileFmt0x00000002)!=0 ?
108319 1 : SQLITE_MAX_FILE_FORMAT4;
108320 sqlite3VdbeAddOp3(v, OP_SetCookie95, iDb, BTREE_FILE_FORMAT2, fileFormat);
108321 sqlite3VdbeAddOp3(v, OP_SetCookie95, iDb, BTREE_TEXT_ENCODING5, ENC(db)((db)->enc));
108322 sqlite3VdbeJumpHere(v, addr1);
108323
108324 /* This just creates a place-holder record in the sqlite_master table.
108325 ** The record created does not contain anything yet. It will be replaced
108326 ** by the real entry in code generated at sqlite3EndTable().
108327 **
108328 ** The rowid for the new entry is left in register pParse->regRowid.
108329 ** The root page number of the new table is left in reg pParse->regRoot.
108330 ** The rowid and root page number values are needed by the code that
108331 ** sqlite3EndTable will generate.
108332 */
108333#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
108334 if( isView || isVirtual ){
108335 sqlite3VdbeAddOp2(v, OP_Integer70, 0, reg2);
108336 }else
108337#endif
108338 {
108339 pParse->addrCrTab =
108340 sqlite3VdbeAddOp3(v, OP_CreateBtree139, iDb, reg2, BTREE_INTKEY1);
108341 }
108342 sqlite3OpenMasterTable(pParse, iDb);
108343 sqlite3VdbeAddOp2(v, OP_NewRowid121, 0, reg1);
108344 sqlite3VdbeAddOp4(v, OP_Blob75, 6, reg3, 0, nullRow, P4_STATIC(-1));
108345 sqlite3VdbeAddOp3(v, OP_Insert122, 0, reg3, reg1);
108346 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
108347 sqlite3VdbeAddOp0(v, OP_Close117);
108348 }
108349
108350 /* Normal (non-error) return. */
108351 return;
108352
108353 /* If an error occurs, we jump here */
108354begin_table_error:
108355 sqlite3DbFree(db, zName);
108356 return;
108357}
108358
108359/* Set properties of a table column based on the (magical)
108360** name of the column.
108361*/
108362#if SQLITE_ENABLE_HIDDEN_COLUMNS
108363SQLITE_PRIVATEstatic void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
108364 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
108365 pCol->colFlags |= COLFLAG_HIDDEN0x0002;
108366 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN0x0002) ){
108367 pTab->tabFlags |= TF_OOOHidden0x0080;
108368 }
108369}
108370#endif
108371
108372
108373/*
108374** Add a new column to the table currently being constructed.
108375**
108376** The parser calls this routine once for each column declaration
108377** in a CREATE TABLE statement. sqlite3StartTable() gets called
108378** first to get things going. Then this routine is called for each
108379** column.
108380*/
108381SQLITE_PRIVATEstatic void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){
108382 Table *p;
108383 int i;
108384 char *z;
108385 char *zType;
108386 Column *pCol;
108387 sqlite3 *db = pParse->db;
108388 if( (p = pParse->pNewTable)==0 ) return;
108389 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
108390 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
108391 return;
108392 }
108393 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
108394 if( z==0 ) return;
108395 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
108396 memcpy(z, pName->z, pName->n);
108397 z[pName->n] = 0;
108398 sqlite3Dequote(z);
108399 for(i=0; i<p->nCol; i++){
108400 if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
108401 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
108402 sqlite3DbFree(db, z);
108403 return;
108404 }
108405 }
108406 if( (p->nCol & 0x7)==0 ){
108407 Column *aNew;
108408 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
108409 if( aNew==0 ){
108410 sqlite3DbFree(db, z);
108411 return;
108412 }
108413 p->aCol = aNew;
108414 }
108415 pCol = &p->aCol[p->nCol];
108416 memset(pCol, 0, sizeof(p->aCol[0]));
108417 pCol->zName = z;
108418 sqlite3ColumnPropertiesFromName(p, pCol);
108419
108420 if( pType->n==0 ){
108421 /* If there is no type specified, columns have the default affinity
108422 ** 'BLOB' with a default size of 4 bytes. */
108423 pCol->affinity = SQLITE_AFF_BLOB'A';
108424 pCol->szEst = 1;
108425#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108426 if( 4>=sqlite3GlobalConfigsqlite3Config.szSorterRef ){
108427 pCol->colFlags |= COLFLAG_SORTERREF0x0010;
108428 }
108429#endif
108430 }else{
108431 zType = z + sqlite3Strlen30(z) + 1;
108432 memcpy(zType, pType->z, pType->n);
108433 zType[pType->n] = 0;
108434 sqlite3Dequote(zType);
108435 pCol->affinity = sqlite3AffinityType(zType, pCol);
108436 pCol->colFlags |= COLFLAG_HASTYPE0x0004;
108437 }
108438 p->nCol++;
108439 pParse->constraintName.n = 0;
108440}
108441
108442/*
108443** This routine is called by the parser while in the middle of
108444** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
108445** been seen on a column. This routine sets the notNull flag on
108446** the column currently under construction.
108447*/
108448SQLITE_PRIVATEstatic void sqlite3AddNotNull(Parse *pParse, int onError){
108449 Table *p;
108450 Column *pCol;
108451 p = pParse->pNewTable;
108452 if( p==0 || NEVER(p->nCol<1)(p->nCol<1) ) return;
108453 pCol = &p->aCol[p->nCol-1];
108454 pCol->notNull = (u8)onError;
108455 p->tabFlags |= TF_HasNotNull0x0200;
108456
108457 /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
108458 ** on this column. */
108459 if( pCol->colFlags & COLFLAG_UNIQUE0x0008 ){
108460 Index *pIdx;
108461 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
108462 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None )((void) (0));
108463 if( pIdx->aiColumn[0]==p->nCol-1 ){
108464 pIdx->uniqNotNull = 1;
108465 }
108466 }
108467 }
108468}
108469
108470/*
108471** Scan the column type name zType (length nType) and return the
108472** associated affinity type.
108473**
108474** This routine does a case-independent search of zType for the
108475** substrings in the following table. If one of the substrings is
108476** found, the corresponding affinity is returned. If zType contains
108477** more than one of the substrings, entries toward the top of
108478** the table take priority. For example, if zType is 'BLOBINT',
108479** SQLITE_AFF_INTEGER is returned.
108480**
108481** Substring | Affinity
108482** --------------------------------
108483** 'INT' | SQLITE_AFF_INTEGER
108484** 'CHAR' | SQLITE_AFF_TEXT
108485** 'CLOB' | SQLITE_AFF_TEXT
108486** 'TEXT' | SQLITE_AFF_TEXT
108487** 'BLOB' | SQLITE_AFF_BLOB
108488** 'REAL' | SQLITE_AFF_REAL
108489** 'FLOA' | SQLITE_AFF_REAL
108490** 'DOUB' | SQLITE_AFF_REAL
108491**
108492** If none of the substrings in the above table are found,
108493** SQLITE_AFF_NUMERIC is returned.
108494*/
108495SQLITE_PRIVATEstatic char sqlite3AffinityType(const char *zIn, Column *pCol){
108496 u32 h = 0;
108497 char aff = SQLITE_AFF_NUMERIC'C';
108498 const char *zChar = 0;
108499
108500 assert( zIn!=0 )((void) (0));
108501 while( zIn[0] ){
108502 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
108503 zIn++;
108504 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
108505 aff = SQLITE_AFF_TEXT'B';
108506 zChar = zIn;
108507 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
108508 aff = SQLITE_AFF_TEXT'B';
108509 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
108510 aff = SQLITE_AFF_TEXT'B';
108511 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
108512 && (aff==SQLITE_AFF_NUMERIC'C' || aff==SQLITE_AFF_REAL'E') ){
108513 aff = SQLITE_AFF_BLOB'A';
108514 if( zIn[0]=='(' ) zChar = zIn;
108515#ifndef SQLITE_OMIT_FLOATING_POINT
108516 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
108517 && aff==SQLITE_AFF_NUMERIC'C' ){
108518 aff = SQLITE_AFF_REAL'E';
108519 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
108520 && aff==SQLITE_AFF_NUMERIC'C' ){
108521 aff = SQLITE_AFF_REAL'E';
108522 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */
108523 && aff==SQLITE_AFF_NUMERIC'C' ){
108524 aff = SQLITE_AFF_REAL'E';
108525#endif
108526 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
108527 aff = SQLITE_AFF_INTEGER'D';
108528 break;
108529 }
108530 }
108531
108532 /* If pCol is not NULL, store an estimate of the field size. The
108533 ** estimate is scaled so that the size of an integer is 1. */
108534 if( pCol ){
108535 int v = 0; /* default size is approx 4 bytes */
108536 if( aff<SQLITE_AFF_NUMERIC'C' ){
108537 if( zChar ){
108538 while( zChar[0] ){
108539 if( sqlite3Isdigit(zChar[0])(sqlite3CtypeMap[(unsigned char)(zChar[0])]&0x04) ){
108540 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
108541 sqlite3GetInt32(zChar, &v);
108542 break;
108543 }
108544 zChar++;
108545 }
108546 }else{
108547 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
108548 }
108549 }
108550#ifdef SQLITE_ENABLE_SORTER_REFERENCES
108551 if( v>=sqlite3GlobalConfigsqlite3Config.szSorterRef ){
108552 pCol->colFlags |= COLFLAG_SORTERREF0x0010;
108553 }
108554#endif
108555 v = v/4 + 1;
108556 if( v>255 ) v = 255;
108557 pCol->szEst = v;
108558 }
108559 return aff;
108560}
108561
108562/*
108563** The expression is the default value for the most recently added column
108564** of the table currently under construction.
108565**
108566** Default value expressions must be constant. Raise an exception if this
108567** is not the case.
108568**
108569** This routine is called by the parser while in the middle of
108570** parsing a CREATE TABLE statement.
108571*/
108572SQLITE_PRIVATEstatic void sqlite3AddDefaultValue(
108573 Parse *pParse, /* Parsing context */
108574 Expr *pExpr, /* The parsed expression of the default value */
108575 const char *zStart, /* Start of the default value text */
108576 const char *zEnd /* First character past end of defaut value text */
108577){
108578 Table *p;
108579 Column *pCol;
108580 sqlite3 *db = pParse->db;
108581 p = pParse->pNewTable;
108582 if( p!=0 ){
108583 pCol = &(p->aCol[p->nCol-1]);
108584 if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
108585 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
108586 pCol->zName);
108587 }else{
108588 /* A copy of pExpr is used instead of the original, as pExpr contains
108589 ** tokens that point to volatile memory.
108590 */
108591 Expr x;
108592 sqlite3ExprDelete(db, pCol->pDflt);
108593 memset(&x, 0, sizeof(x));
108594 x.op = TK_SPAN173;
108595 x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
108596 x.pLeft = pExpr;
108597 x.flags = EP_Skip0x001000;
108598 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE0x0001);
108599 sqlite3DbFree(db, x.u.zToken);
108600 }
108601 }
108602 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
108603 sqlite3RenameExprUnmap(pParse, pExpr);
108604 }
108605 sqlite3ExprDelete(db, pExpr);
108606}
108607
108608/*
108609** Backwards Compatibility Hack:
108610**
108611** Historical versions of SQLite accepted strings as column names in
108612** indexes and PRIMARY KEY constraints and in UNIQUE constraints. Example:
108613**
108614** CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
108615** CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
108616**
108617** This is goofy. But to preserve backwards compatibility we continue to
108618** accept it. This routine does the necessary conversion. It converts
108619** the expression given in its argument from a TK_STRING into a TK_ID
108620** if the expression is just a TK_STRING with an optional COLLATE clause.
108621** If the expression is anything other than TK_STRING, the expression is
108622** unchanged.
108623*/
108624static void sqlite3StringToId(Expr *p){
108625 if( p->op==TK_STRING110 ){
108626 p->op = TK_ID59;
108627 }else if( p->op==TK_COLLATE106 && p->pLeft->op==TK_STRING110 ){
108628 p->pLeft->op = TK_ID59;
108629 }
108630}
108631
108632/*
108633** Designate the PRIMARY KEY for the table. pList is a list of names
108634** of columns that form the primary key. If pList is NULL, then the
108635** most recently added column of the table is the primary key.
108636**
108637** A table can have at most one primary key. If the table already has
108638** a primary key (and this is the second primary key) then create an
108639** error.
108640**
108641** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
108642** then we will try to use that column as the rowid. Set the Table.iPKey
108643** field of the table under construction to be the index of the
108644** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
108645** no INTEGER PRIMARY KEY.
108646**
108647** If the key is not an INTEGER PRIMARY KEY, then create a unique
108648** index for the key. No index is created for INTEGER PRIMARY KEYs.
108649*/
108650SQLITE_PRIVATEstatic void sqlite3AddPrimaryKey(
108651 Parse *pParse, /* Parsing context */
108652 ExprList *pList, /* List of field names to be indexed */
108653 int onError, /* What to do with a uniqueness conflict */
108654 int autoInc, /* True if the AUTOINCREMENT keyword is present */
108655 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */
108656){
108657 Table *pTab = pParse->pNewTable;
108658 Column *pCol = 0;
108659 int iCol = -1, i;
108660 int nTerm;
108661 if( pTab==0 ) goto primary_key_exit;
108662 if( pTab->tabFlags & TF_HasPrimaryKey0x0004 ){
108663 sqlite3ErrorMsg(pParse,
108664 "table \"%s\" has more than one primary key", pTab->zName);
108665 goto primary_key_exit;
108666 }
108667 pTab->tabFlags |= TF_HasPrimaryKey0x0004;
108668 if( pList==0 ){
108669 iCol = pTab->nCol - 1;
108670 pCol = &pTab->aCol[iCol];
108671 pCol->colFlags |= COLFLAG_PRIMKEY0x0001;
108672 nTerm = 1;
108673 }else{
108674 nTerm = pList->nExpr;
108675 for(i=0; i<nTerm; i++){
108676 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
108677 assert( pCExpr!=0 )((void) (0));
108678 sqlite3StringToId(pCExpr);
108679 if( pCExpr->op==TK_ID59 ){
108680 const char *zCName = pCExpr->u.zToken;
108681 for(iCol=0; iCol<pTab->nCol; iCol++){
108682 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
108683 pCol = &pTab->aCol[iCol];
108684 pCol->colFlags |= COLFLAG_PRIMKEY0x0001;
108685 break;
108686 }
108687 }
108688 }
108689 }
108690 }
108691 if( nTerm==1
108692 && pCol
108693 && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
108694 && sortOrder!=SQLITE_SO_DESC1
108695 ){
108696 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pList ){
108697 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
108698 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
108699 }
108700 pTab->iPKey = iCol;
108701 pTab->keyConf = (u8)onError;
108702 assert( autoInc==0 || autoInc==1 )((void) (0));
108703 pTab->tabFlags |= autoInc*TF_Autoincrement0x0008;
108704 if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
108705 }else if( autoInc ){
108706#ifndef SQLITE_OMIT_AUTOINCREMENT
108707 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
108708 "INTEGER PRIMARY KEY");
108709#endif
108710 }else{
108711 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
108712 0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY2);
108713 pList = 0;
108714 }
108715
108716primary_key_exit:
108717 sqlite3ExprListDelete(pParse->db, pList);
108718 return;
108719}
108720
108721/*
108722** Add a new CHECK constraint to the table currently under construction.
108723*/
108724SQLITE_PRIVATEstatic void sqlite3AddCheckConstraint(
108725 Parse *pParse, /* Parsing context */
108726 Expr *pCheckExpr /* The check expression */
108727){
108728#ifndef SQLITE_OMIT_CHECK
108729 Table *pTab = pParse->pNewTable;
108730 sqlite3 *db = pParse->db;
108731 if( pTab && !IN_DECLARE_VTAB(pParse->eParseMode==1)
108732 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
108733 ){
108734 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
108735 if( pParse->constraintName.n ){
108736 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
108737 }
108738 }else
108739#endif
108740 {
108741 sqlite3ExprDelete(pParse->db, pCheckExpr);
108742 }
108743}
108744
108745/*
108746** Set the collation function of the most recently parsed table column
108747** to the CollSeq given.
108748*/
108749SQLITE_PRIVATEstatic void sqlite3AddCollateType(Parse *pParse, Token *pToken){
108750 Table *p;
108751 int i;
108752 char *zColl; /* Dequoted name of collation sequence */
108753 sqlite3 *db;
108754
108755 if( (p = pParse->pNewTable)==0 ) return;
108756 i = p->nCol-1;
108757 db = pParse->db;
108758 zColl = sqlite3NameFromToken(db, pToken);
108759 if( !zColl ) return;
108760
108761 if( sqlite3LocateCollSeq(pParse, zColl) ){
108762 Index *pIdx;
108763 sqlite3DbFree(db, p->aCol[i].zColl);
108764 p->aCol[i].zColl = zColl;
108765
108766 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
108767 ** then an index may have been created on this column before the
108768 ** collation type was added. Correct this if it is the case.
108769 */
108770 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
108771 assert( pIdx->nKeyCol==1 )((void) (0));
108772 if( pIdx->aiColumn[0]==i ){
108773 pIdx->azColl[0] = p->aCol[i].zColl;
108774 }
108775 }
108776 }else{
108777 sqlite3DbFree(db, zColl);
108778 }
108779}
108780
108781/*
108782** This function returns the collation sequence for database native text
108783** encoding identified by the string zName, length nName.
108784**
108785** If the requested collation sequence is not available, or not available
108786** in the database native encoding, the collation factory is invoked to
108787** request it. If the collation factory does not supply such a sequence,
108788** and the sequence is available in another text encoding, then that is
108789** returned instead.
108790**
108791** If no versions of the requested collations sequence are available, or
108792** another error occurs, NULL is returned and an error message written into
108793** pParse.
108794**
108795** This routine is a wrapper around sqlite3FindCollSeq(). This routine
108796** invokes the collation factory if the named collation cannot be found
108797** and generates an error message.
108798**
108799** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
108800*/
108801SQLITE_PRIVATEstatic CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
108802 sqlite3 *db = pParse->db;
108803 u8 enc = ENC(db)((db)->enc);
108804 u8 initbusy = db->init.busy;
108805 CollSeq *pColl;
108806
108807 pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
108808 if( !initbusy && (!pColl || !pColl->xCmp) ){
108809 pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
108810 }
108811
108812 return pColl;
108813}
108814
108815
108816/*
108817** Generate code that will increment the schema cookie.
108818**
108819** The schema cookie is used to determine when the schema for the
108820** database changes. After each schema change, the cookie value
108821** changes. When a process first reads the schema it records the
108822** cookie. Thereafter, whenever it goes to access the database,
108823** it checks the cookie to make sure the schema has not changed
108824** since it was last read.
108825**
108826** This plan is not completely bullet-proof. It is possible for
108827** the schema to change multiple times and for the cookie to be
108828** set back to prior value. But schema changes are infrequent
108829** and the probability of hitting the same cookie value is only
108830** 1 chance in 2^32. So we're safe enough.
108831**
108832** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
108833** the schema-version whenever the schema changes.
108834*/
108835SQLITE_PRIVATEstatic void sqlite3ChangeCookie(Parse *pParse, int iDb){
108836 sqlite3 *db = pParse->db;
108837 Vdbe *v = pParse->pVdbe;
108838 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
108839 sqlite3VdbeAddOp3(v, OP_SetCookie95, iDb, BTREE_SCHEMA_VERSION1,
108840 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
108841}
108842
108843/*
108844** Measure the number of characters needed to output the given
108845** identifier. The number returned includes any quotes used
108846** but does not include the null terminator.
108847**
108848** The estimate is conservative. It might be larger that what is
108849** really needed.
108850*/
108851static int identLength(const char *z){
108852 int n;
108853 for(n=0; *z; n++, z++){
108854 if( *z=='"' ){ n++; }
108855 }
108856 return n + 2;
108857}
108858
108859/*
108860** The first parameter is a pointer to an output buffer. The second
108861** parameter is a pointer to an integer that contains the offset at
108862** which to write into the output buffer. This function copies the
108863** nul-terminated string pointed to by the third parameter, zSignedIdent,
108864** to the specified offset in the buffer and updates *pIdx to refer
108865** to the first byte after the last byte written before returning.
108866**
108867** If the string zSignedIdent consists entirely of alpha-numeric
108868** characters, does not begin with a digit and is not an SQL keyword,
108869** then it is copied to the output buffer exactly as it is. Otherwise,
108870** it is quoted using double-quotes.
108871*/
108872static void identPut(char *z, int *pIdx, char *zSignedIdent){
108873 unsigned char *zIdent = (unsigned char*)zSignedIdent;
108874 int i, j, needQuote;
108875 i = *pIdx;
108876
108877 for(j=0; zIdent[j]; j++){
108878 if( !sqlite3Isalnum(zIdent[j])(sqlite3CtypeMap[(unsigned char)(zIdent[j])]&0x06) && zIdent[j]!='_' ) break;
108879 }
108880 needQuote = sqlite3Isdigit(zIdent[0])(sqlite3CtypeMap[(unsigned char)(zIdent[0])]&0x04)
108881 || sqlite3KeywordCode(zIdent, j)!=TK_ID59
108882 || zIdent[j]!=0
108883 || j==0;
108884
108885 if( needQuote ) z[i++] = '"';
108886 for(j=0; zIdent[j]; j++){
108887 z[i++] = zIdent[j];
108888 if( zIdent[j]=='"' ) z[i++] = '"';
108889 }
108890 if( needQuote ) z[i++] = '"';
108891 z[i] = 0;
108892 *pIdx = i;
108893}
108894
108895/*
108896** Generate a CREATE TABLE statement appropriate for the given
108897** table. Memory to hold the text of the statement is obtained
108898** from sqliteMalloc() and must be freed by the calling function.
108899*/
108900static char *createTableStmt(sqlite3 *db, Table *p){
108901 int i, k, n;
108902 char *zStmt;
108903 char *zSep, *zSep2, *zEnd;
108904 Column *pCol;
108905 n = 0;
108906 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
108907 n += identLength(pCol->zName) + 5;
108908 }
108909 n += identLength(p->zName);
108910 if( n<50 ){
108911 zSep = "";
108912 zSep2 = ",";
108913 zEnd = ")";
108914 }else{
108915 zSep = "\n ";
108916 zSep2 = ",\n ";
108917 zEnd = "\n)";
108918 }
108919 n += 35 + 6*p->nCol;
108920 zStmt = sqlite3DbMallocRaw(0, n);
108921 if( zStmt==0 ){
108922 sqlite3OomFault(db);
108923 return 0;
108924 }
108925 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
108926 k = sqlite3Strlen30(zStmt);
108927 identPut(zStmt, &k, p->zName);
108928 zStmt[k++] = '(';
108929 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
108930 static const char * const azType[] = {
108931 /* SQLITE_AFF_BLOB */ "",
108932 /* SQLITE_AFF_TEXT */ " TEXT",
108933 /* SQLITE_AFF_NUMERIC */ " NUM",
108934 /* SQLITE_AFF_INTEGER */ " INT",
108935 /* SQLITE_AFF_REAL */ " REAL"
108936 };
108937 int len;
108938 const char *zType;
108939
108940 sqlite3_snprintf(n-k, &zStmt[k], zSep);
108941 k += sqlite3Strlen30(&zStmt[k]);
108942 zSep = zSep2;
108943 identPut(zStmt, &k, pCol->zName);
108944 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 )((void) (0));
108945 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) )((void) (0));
108946 testcase( pCol->affinity==SQLITE_AFF_BLOB );
108947 testcase( pCol->affinity==SQLITE_AFF_TEXT );
108948 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
108949 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
108950 testcase( pCol->affinity==SQLITE_AFF_REAL );
108951
108952 zType = azType[pCol->affinity - SQLITE_AFF_BLOB'A'];
108953 len = sqlite3Strlen30(zType);
108954 assert( pCol->affinity==SQLITE_AFF_BLOB((void) (0))
108955 || pCol->affinity==sqlite3AffinityType(zType, 0) )((void) (0));
108956 memcpy(&zStmt[k], zType, len);
108957 k += len;
108958 assert( k<=n )((void) (0));
108959 }
108960 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
108961 return zStmt;
108962}
108963
108964/*
108965** Resize an Index object to hold N columns total. Return SQLITE_OK
108966** on success and SQLITE_NOMEM on an OOM error.
108967*/
108968static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
108969 char *zExtra;
108970 int nByte;
108971 if( pIdx->nColumn>=N ) return SQLITE_OK0;
108972 assert( pIdx->isResized==0 )((void) (0));
108973 nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
108974 zExtra = sqlite3DbMallocZero(db, nByte);
108975 if( zExtra==0 ) return SQLITE_NOMEM_BKPT7;
108976 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
108977 pIdx->azColl = (const char**)zExtra;
108978 zExtra += sizeof(char*)*N;
108979 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
108980 pIdx->aiColumn = (i16*)zExtra;
108981 zExtra += sizeof(i16)*N;
108982 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
108983 pIdx->aSortOrder = (u8*)zExtra;
108984 pIdx->nColumn = N;
108985 pIdx->isResized = 1;
108986 return SQLITE_OK0;
108987}
108988
108989/*
108990** Estimate the total row width for a table.
108991*/
108992static void estimateTableWidth(Table *pTab){
108993 unsigned wTable = 0;
108994 const Column *pTabCol;
108995 int i;
108996 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
108997 wTable += pTabCol->szEst;
108998 }
108999 if( pTab->iPKey<0 ) wTable++;
109000 pTab->szTabRow = sqlite3LogEst(wTable*4);
109001}
109002
109003/*
109004** Estimate the average size of a row for an index.
109005*/
109006static void estimateIndexWidth(Index *pIdx){
109007 unsigned wIndex = 0;
109008 int i;
109009 const Column *aCol = pIdx->pTable->aCol;
109010 for(i=0; i<pIdx->nColumn; i++){
109011 i16 x = pIdx->aiColumn[i];
109012 assert( x<pIdx->pTable->nCol )((void) (0));
109013 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
109014 }
109015 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
109016}
109017
109018/* Return true if column number x is any of the first nCol entries of aiCol[].
109019** This is used to determine if the column number x appears in any of the
109020** first nCol entries of an index.
109021*/
109022static int hasColumn(const i16 *aiCol, int nCol, int x){
109023 while( nCol-- > 0 ){
109024 assert( aiCol[0]>=0 )((void) (0));
109025 if( x==*(aiCol++) ){
109026 return 1;
109027 }
109028 }
109029 return 0;
109030}
109031
109032/*
109033** Return true if any of the first nKey entries of index pIdx exactly
109034** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
109035** PRIMARY KEY index. pIdx is an index on the same table. pIdx may
109036** or may not be the same index as pPk.
109037**
109038** The first nKey entries of pIdx are guaranteed to be ordinary columns,
109039** not a rowid or expression.
109040**
109041** This routine differs from hasColumn() in that both the column and the
109042** collating sequence must match for this routine, but for hasColumn() only
109043** the column name must match.
109044*/
109045static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
109046 int i, j;
109047 assert( nKey<=pIdx->nColumn )((void) (0));
109048 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) )((void) (0));
109049 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY )((void) (0));
109050 assert( pPk->pTable->tabFlags & TF_WithoutRowid )((void) (0));
109051 assert( pPk->pTable==pIdx->pTable )((void) (0));
109052 testcase( pPk==pIdx );
109053 j = pPk->aiColumn[iCol];
109054 assert( j!=XN_ROWID && j!=XN_EXPR )((void) (0));
109055 for(i=0; i<nKey; i++){
109056 assert( pIdx->aiColumn[i]>=0 || j>=0 )((void) (0));
109057 if( pIdx->aiColumn[i]==j
109058 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
109059 ){
109060 return 1;
109061 }
109062 }
109063 return 0;
109064}
109065
109066/* Recompute the colNotIdxed field of the Index.
109067**
109068** colNotIdxed is a bitmask that has a 0 bit representing each indexed
109069** columns that are within the first 63 columns of the table. The
109070** high-order bit of colNotIdxed is always 1. All unindexed columns
109071** of the table have a 1.
109072**
109073** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
109074** to determine if the index is covering index.
109075*/
109076static void recomputeColumnsNotIndexed(Index *pIdx){
109077 Bitmask m = 0;
109078 int j;
109079 for(j=pIdx->nColumn-1; j>=0; j--){
109080 int x = pIdx->aiColumn[j];
109081 if( x>=0 ){
109082 testcase( x==BMS-1 );
109083 testcase( x==BMS-2 );
109084 if( x<BMS((int)(sizeof(Bitmask)*8))-1 ) m |= MASKBIT(x)(((Bitmask)1)<<(x));
109085 }
109086 }
109087 pIdx->colNotIdxed = ~m;
109088 assert( (pIdx->colNotIdxed>>63)==1 )((void) (0));
109089}
109090
109091/*
109092** This routine runs at the end of parsing a CREATE TABLE statement that
109093** has a WITHOUT ROWID clause. The job of this routine is to convert both
109094** internal schema data structures and the generated VDBE code so that they
109095** are appropriate for a WITHOUT ROWID table instead of a rowid table.
109096** Changes include:
109097**
109098** (1) Set all columns of the PRIMARY KEY schema object to be NOT NULL.
109099** (2) Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
109100** into BTREE_BLOBKEY.
109101** (3) Bypass the creation of the sqlite_master table entry
109102** for the PRIMARY KEY as the primary key index is now
109103** identified by the sqlite_master table entry of the table itself.
109104** (4) Set the Index.tnum of the PRIMARY KEY Index object in the
109105** schema to the rootpage from the main table.
109106** (5) Add all table columns to the PRIMARY KEY Index object
109107** so that the PRIMARY KEY is a covering index. The surplus
109108** columns are part of KeyInfo.nAllField and are not used for
109109** sorting or lookup or uniqueness checks.
109110** (6) Replace the rowid tail on all automatically generated UNIQUE
109111** indices with the PRIMARY KEY columns.
109112**
109113** For virtual tables, only (1) is performed.
109114*/
109115static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
109116 Index *pIdx;
109117 Index *pPk;
109118 int nPk;
109119 int i, j;
109120 sqlite3 *db = pParse->db;
109121 Vdbe *v = pParse->pVdbe;
109122
109123 /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
109124 */
109125 if( !db->init.imposterTable ){
109126 for(i=0; i<pTab->nCol; i++){
109127 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001)!=0 ){
109128 pTab->aCol[i].notNull = OE_Abort2;
109129 }
109130 }
109131 }
109132
109133 /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
109134 ** into BTREE_BLOBKEY.
109135 */
109136 if( pParse->addrCrTab ){
109137 assert( v )((void) (0));
109138 sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY2);
109139 }
109140
109141 /* Locate the PRIMARY KEY index. Or, if this table was originally
109142 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
109143 */
109144 if( pTab->iPKey>=0 ){
109145 ExprList *pList;
109146 Token ipkToken;
109147 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
109148 pList = sqlite3ExprListAppend(pParse, 0,
109149 sqlite3ExprAlloc(db, TK_ID59, &ipkToken, 0));
109150 if( pList==0 ) return;
109151 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
109152 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
109153 }
109154 pList->a[0].sortOrder = pParse->iPkSortOrder;
109155 assert( pParse->pNewTable==pTab )((void) (0));
109156 pTab->iPKey = -1;
109157 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
109158 SQLITE_IDXTYPE_PRIMARYKEY2);
109159 if( db->mallocFailed || pParse->nErr ) return;
109160 pPk = sqlite3PrimaryKeyIndex(pTab);
109161 }else{
109162 pPk = sqlite3PrimaryKeyIndex(pTab);
109163 assert( pPk!=0 )((void) (0));
109164
109165 /*
109166 ** Remove all redundant columns from the PRIMARY KEY. For example, change
109167 ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later
109168 ** code assumes the PRIMARY KEY contains no repeated columns.
109169 */
109170 for(i=j=1; i<pPk->nKeyCol; i++){
109171 if( isDupColumn(pPk, j, pPk, i) ){
109172 pPk->nColumn--;
109173 }else{
109174 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
109175 pPk->aiColumn[j++] = pPk->aiColumn[i];
109176 }
109177 }
109178 pPk->nKeyCol = j;
109179 }
109180 assert( pPk!=0 )((void) (0));
109181 pPk->isCovering = 1;
109182 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
109183 nPk = pPk->nKeyCol;
109184
109185 /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
109186 ** table entry. This is only required if currently generating VDBE
109187 ** code for a CREATE TABLE (not when parsing one as part of reading
109188 ** a database schema). */
109189 if( v && pPk->tnum>0 ){
109190 assert( db->init.busy==0 )((void) (0));
109191 sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto11);
109192 }
109193
109194 /* The root page of the PRIMARY KEY is the table root page */
109195 pPk->tnum = pTab->tnum;
109196
109197 /* Update the in-memory representation of all UNIQUE indices by converting
109198 ** the final rowid column into one or more columns of the PRIMARY KEY.
109199 */
109200 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
109201 int n;
109202 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ) continue;
109203 for(i=n=0; i<nPk; i++){
109204 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
109205 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
109206 n++;
109207 }
109208 }
109209 if( n==0 ){
109210 /* This index is a superset of the primary key */
109211 pIdx->nColumn = pIdx->nKeyCol;
109212 continue;
109213 }
109214 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
109215 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
109216 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
109217 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
109218 pIdx->aiColumn[j] = pPk->aiColumn[i];
109219 pIdx->azColl[j] = pPk->azColl[i];
109220 if( pPk->aSortOrder[i] ){
109221 /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
109222 pIdx->bAscKeyBug = 1;
109223 }
109224 j++;
109225 }
109226 }
109227 assert( pIdx->nColumn>=pIdx->nKeyCol+n )((void) (0));
109228 assert( pIdx->nColumn>=j )((void) (0));
109229 }
109230
109231 /* Add all table columns to the PRIMARY KEY index
109232 */
109233 if( nPk<pTab->nCol ){
109234 if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
109235 for(i=0, j=nPk; i<pTab->nCol; i++){
109236 if( !hasColumn(pPk->aiColumn, j, i) ){
109237 assert( j<pPk->nColumn )((void) (0));
109238 pPk->aiColumn[j] = i;
109239 pPk->azColl[j] = sqlite3StrBINARY;
109240 j++;
109241 }
109242 }
109243 assert( pPk->nColumn==j )((void) (0));
109244 assert( pTab->nCol==j )((void) (0));
109245 }else{
109246 pPk->nColumn = pTab->nCol;
109247 }
109248 recomputeColumnsNotIndexed(pPk);
109249}
109250
109251#ifndef SQLITE_OMIT_VIRTUALTABLE
109252/*
109253** Return true if zName is a shadow table name in the current database
109254** connection.
109255**
109256** zName is temporarily modified while this routine is running, but is
109257** restored to its original value prior to this routine returning.
109258*/
109259static int isShadowTableName(sqlite3 *db, char *zName){
109260 char *zTail; /* Pointer to the last "_" in zName */
109261 Table *pTab; /* Table that zName is a shadow of */
109262 Module *pMod; /* Module for the virtual table */
109263
109264 zTail = strrchr(zName, '_');
109265 if( zTail==0 ) return 0;
109266 *zTail = 0;
109267 pTab = sqlite3FindTable(db, zName, 0);
109268 *zTail = '_';
109269 if( pTab==0 ) return 0;
109270 if( !IsVirtual(pTab)((pTab)->nModuleArg) ) return 0;
109271 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
109272 if( pMod==0 ) return 0;
109273 if( pMod->pModule->iVersion<3 ) return 0;
109274 if( pMod->pModule->xShadowName==0 ) return 0;
109275 return pMod->pModule->xShadowName(zTail+1);
109276}
109277#else
109278# define isShadowTableName(x,y) 0
109279#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
109280
109281/*
109282** This routine is called to report the final ")" that terminates
109283** a CREATE TABLE statement.
109284**
109285** The table structure that other action routines have been building
109286** is added to the internal hash tables, assuming no errors have
109287** occurred.
109288**
109289** An entry for the table is made in the master table on disk, unless
109290** this is a temporary table or db->init.busy==1. When db->init.busy==1
109291** it means we are reading the sqlite_master table because we just
109292** connected to the database or because the sqlite_master table has
109293** recently changed, so the entry for this table already exists in
109294** the sqlite_master table. We do not want to create it again.
109295**
109296** If the pSelect argument is not NULL, it means that this routine
109297** was called to create a table generated from a
109298** "CREATE TABLE ... AS SELECT ..." statement. The column names of
109299** the new table will match the result set of the SELECT.
109300*/
109301SQLITE_PRIVATEstatic void sqlite3EndTable(
109302 Parse *pParse, /* Parse context */
109303 Token *pCons, /* The ',' token after the last column defn. */
109304 Token *pEnd, /* The ')' before options in the CREATE TABLE */
109305 u8 tabOpts, /* Extra table options. Usually 0. */
109306 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
109307){
109308 Table *p; /* The new table */
109309 sqlite3 *db = pParse->db; /* The database connection */
109310 int iDb; /* Database in which the table lives */
109311 Index *pIdx; /* An implied index of the table */
109312
109313 if( pEnd==0 && pSelect==0 ){
109314 return;
109315 }
109316 assert( !db->mallocFailed )((void) (0));
109317 p = pParse->pNewTable;
109318 if( p==0 ) return;
109319
109320 if( pSelect==0 && isShadowTableName(db, p->zName) ){
109321 p->tabFlags |= TF_Shadow0x0400;
109322 }
109323
109324 /* If the db->init.busy is 1 it means we are reading the SQL off the
109325 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
109326 ** So do not write to the disk again. Extract the root page number
109327 ** for the table from the db->init.newTnum field. (The page number
109328 ** should have been put there by the sqliteOpenCb routine.)
109329 **
109330 ** If the root page number is 1, that means this is the sqlite_master
109331 ** table itself. So mark it read-only.
109332 */
109333 if( db->init.busy ){
109334 if( pSelect ){
109335 sqlite3ErrorMsg(pParse, "");
109336 return;
109337 }
109338 p->tnum = db->init.newTnum;
109339 if( p->tnum==1 ) p->tabFlags |= TF_Readonly0x0001;
109340 }
109341
109342 assert( (p->tabFlags & TF_HasPrimaryKey)==0((void) (0))
109343 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 )((void) (0));
109344 assert( (p->tabFlags & TF_HasPrimaryKey)!=0((void) (0))
109345 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) )((void) (0));
109346
109347 /* Special processing for WITHOUT ROWID Tables */
109348 if( tabOpts & TF_WithoutRowid0x0020 ){
109349 if( (p->tabFlags & TF_Autoincrement0x0008) ){
109350 sqlite3ErrorMsg(pParse,
109351 "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
109352 return;
109353 }
109354 if( (p->tabFlags & TF_HasPrimaryKey0x0004)==0 ){
109355 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
109356 }else{
109357 p->tabFlags |= TF_WithoutRowid0x0020 | TF_NoVisibleRowid0x0040;
109358 convertToWithoutRowidTable(pParse, p);
109359 }
109360 }
109361
109362 iDb = sqlite3SchemaToIndex(db, p->pSchema);
109363
109364#ifndef SQLITE_OMIT_CHECK
109365 /* Resolve names in all CHECK constraint expressions.
109366 */
109367 if( p->pCheck ){
109368 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck0x0004, 0, p->pCheck);
109369 }
109370#endif /* !defined(SQLITE_OMIT_CHECK) */
109371
109372 /* Estimate the average row size for the table and for all implied indices */
109373 estimateTableWidth(p);
109374 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
109375 estimateIndexWidth(pIdx);
109376 }
109377
109378 /* If not initializing, then create a record for the new table
109379 ** in the SQLITE_MASTER table of the database.
109380 **
109381 ** If this is a TEMPORARY table, write the entry into the auxiliary
109382 ** file instead of into the main database file.
109383 */
109384 if( !db->init.busy ){
109385 int n;
109386 Vdbe *v;
109387 char *zType; /* "view" or "table" */
109388 char *zType2; /* "VIEW" or "TABLE" */
109389 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
109390
109391 v = sqlite3GetVdbe(pParse);
109392 if( NEVER(v==0)(v==0) ) return;
109393
109394 sqlite3VdbeAddOp1(v, OP_Close117, 0);
109395
109396 /*
109397 ** Initialize zType for the new view or table.
109398 */
109399 if( p->pSelect==0 ){
109400 /* A regular table */
109401 zType = "table";
109402 zType2 = "TABLE";
109403#ifndef SQLITE_OMIT_VIEW
109404 }else{
109405 /* A view */
109406 zType = "view";
109407 zType2 = "VIEW";
109408#endif
109409 }
109410
109411 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
109412 ** statement to populate the new table. The root-page number for the
109413 ** new table is in register pParse->regRoot.
109414 **
109415 ** Once the SELECT has been coded by sqlite3Select(), it is in a
109416 ** suitable state to query for the column names and types to be used
109417 ** by the new table.
109418 **
109419 ** A shared-cache write-lock is not required to write to the new table,
109420 ** as a schema-lock must have already been obtained to create it. Since
109421 ** a schema-lock excludes all other database users, the write-lock would
109422 ** be redundant.
109423 */
109424 if( pSelect ){
109425 SelectDest dest; /* Where the SELECT should store results */
109426 int regYield; /* Register holding co-routine entry-point */
109427 int addrTop; /* Top of the co-routine */
109428 int regRec; /* A record to be insert into the new table */
109429 int regRowid; /* Rowid of the next row to insert */
109430 int addrInsLoop; /* Top of the loop for inserting rows */
109431 Table *pSelTab; /* A table that describes the SELECT results */
109432
109433 regYield = ++pParse->nMem;
109434 regRec = ++pParse->nMem;
109435 regRowid = ++pParse->nMem;
109436 assert(pParse->nTab==1)((void) (0));
109437 sqlite3MayAbort(pParse);
109438 sqlite3VdbeAddOp3(v, OP_OpenWrite109, 1, pParse->regRoot, iDb);
109439 sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG0x10);
109440 pParse->nTab = 2;
109441 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
109442 sqlite3VdbeAddOp3(v, OP_InitCoroutine13, regYield, 0, addrTop);
109443 if( pParse->nErr ) return;
109444 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
109445 if( pSelTab==0 ) return;
109446 assert( p->aCol==0 )((void) (0));
109447 p->nCol = pSelTab->nCol;
109448 p->aCol = pSelTab->aCol;
109449 pSelTab->nCol = 0;
109450 pSelTab->aCol = 0;
109451 sqlite3DeleteTable(db, pSelTab);
109452 sqlite3SelectDestInit(&dest, SRT_Coroutine13, regYield);
109453 sqlite3Select(pParse, pSelect, &dest);
109454 if( pParse->nErr ) return;
109455 sqlite3VdbeEndCoroutine(v, regYield);
109456 sqlite3VdbeJumpHere(v, addrTop - 1);
109457 addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield14, dest.iSDParm);
109458 VdbeCoverage(v);
109459 sqlite3VdbeAddOp3(v, OP_MakeRecord92, dest.iSdst, dest.nSdst, regRec);
109460 sqlite3TableAffinity(v, p, 0);
109461 sqlite3VdbeAddOp2(v, OP_NewRowid121, 1, regRowid);
109462 sqlite3VdbeAddOp3(v, OP_Insert122, 1, regRec, regRowid);
109463 sqlite3VdbeGoto(v, addrInsLoop);
109464 sqlite3VdbeJumpHere(v, addrInsLoop);
109465 sqlite3VdbeAddOp1(v, OP_Close117, 1);
109466 }
109467
109468 /* Compute the complete text of the CREATE statement */
109469 if( pSelect ){
109470 zStmt = createTableStmt(db, p);
109471 }else{
109472 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
109473 n = (int)(pEnd2->z - pParse->sNameToken.z);
109474 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
109475 zStmt = sqlite3MPrintf(db,
109476 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
109477 );
109478 }
109479
109480 /* A slot for the record has already been allocated in the
109481 ** SQLITE_MASTER table. We just need to update that slot with all
109482 ** the information we've collected.
109483 */
109484 sqlite3NestedParse(pParse,
109485 "UPDATE %Q.%s "
109486 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
109487 "WHERE rowid=#%d",
109488 db->aDb[iDb].zDbSName, MASTER_NAME"sqlite_master",
109489 zType,
109490 p->zName,
109491 p->zName,
109492 pParse->regRoot,
109493 zStmt,
109494 pParse->regRowid
109495 );
109496 sqlite3DbFree(db, zStmt);
109497 sqlite3ChangeCookie(pParse, iDb);
109498
109499#ifndef SQLITE_OMIT_AUTOINCREMENT
109500 /* Check to see if we need to create an sqlite_sequence table for
109501 ** keeping track of autoincrement keys.
109502 */
109503 if( (p->tabFlags & TF_Autoincrement0x0008)!=0 ){
109504 Db *pDb = &db->aDb[iDb];
109505 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
109506 if( pDb->pSchema->pSeqTab==0 ){
109507 sqlite3NestedParse(pParse,
109508 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
109509 pDb->zDbSName
109510 );
109511 }
109512 }
109513#endif
109514
109515 /* Reparse everything to update our internal data structures */
109516 sqlite3VdbeAddParseSchemaOp(v, iDb,
109517 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
109518 }
109519
109520
109521 /* Add the table to the in-memory representation of the database.
109522 */
109523 if( db->init.busy ){
109524 Table *pOld;
109525 Schema *pSchema = p->pSchema;
109526 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
109527 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
109528 if( pOld ){
109529 assert( p==pOld )((void) (0)); /* Malloc must have failed inside HashInsert() */
109530 sqlite3OomFault(db);
109531 return;
109532 }
109533 pParse->pNewTable = 0;
109534 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
109535
109536#ifndef SQLITE_OMIT_ALTERTABLE
109537 if( !p->pSelect ){
109538 const char *zName = (const char *)pParse->sNameToken.z;
109539 int nName;
109540 assert( !pSelect && pCons && pEnd )((void) (0));
109541 if( pCons->z==0 ){
109542 pCons = pEnd;
109543 }
109544 nName = (int)((const char *)pCons->z - zName);
109545 p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
109546 }
109547#endif
109548 }
109549}
109550
109551#ifndef SQLITE_OMIT_VIEW
109552/*
109553** The parser calls this routine in order to create a new VIEW
109554*/
109555SQLITE_PRIVATEstatic void sqlite3CreateView(
109556 Parse *pParse, /* The parsing context */
109557 Token *pBegin, /* The CREATE token that begins the statement */
109558 Token *pName1, /* The token that holds the name of the view */
109559 Token *pName2, /* The token that holds the name of the view */
109560 ExprList *pCNames, /* Optional list of view column names */
109561 Select *pSelect, /* A SELECT statement that will become the new view */
109562 int isTemp, /* TRUE for a TEMPORARY view */
109563 int noErr /* Suppress error messages if VIEW already exists */
109564){
109565 Table *p;
109566 int n;
109567 const char *z;
109568 Token sEnd;
109569 DbFixer sFix;
109570 Token *pName = 0;
109571 int iDb;
109572 sqlite3 *db = pParse->db;
109573
109574 if( pParse->nVar>0 ){
109575 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
109576 goto create_view_fail;
109577 }
109578 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
109579 p = pParse->pNewTable;
109580 if( p==0 || pParse->nErr ) goto create_view_fail;
109581 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
109582 iDb = sqlite3SchemaToIndex(db, p->pSchema);
109583 sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
109584 if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
109585
109586 /* Make a copy of the entire SELECT statement that defines the view.
109587 ** This will force all the Expr.token.z values to be dynamically
109588 ** allocated rather than point to the input string - which means that
109589 ** they will persist after the current sqlite3_exec() call returns.
109590 */
109591 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
109592 p->pSelect = pSelect;
109593 pSelect = 0;
109594 }else{
109595 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
109596 }
109597 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE0x0001);
109598 if( db->mallocFailed ) goto create_view_fail;
109599
109600 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
109601 ** the end.
109602 */
109603 sEnd = pParse->sLastToken;
109604 assert( sEnd.z[0]!=0 || sEnd.n==0 )((void) (0));
109605 if( sEnd.z[0]!=';' ){
109606 sEnd.z += sEnd.n;
109607 }
109608 sEnd.n = 0;
109609 n = (int)(sEnd.z - pBegin->z);
109610 assert( n>0 )((void) (0));
109611 z = pBegin->z;
109612 while( sqlite3Isspace(z[n-1])(sqlite3CtypeMap[(unsigned char)(z[n-1])]&0x01) ){ n--; }
109613 sEnd.z = &z[n-1];
109614 sEnd.n = 1;
109615
109616 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
109617 sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
109618
109619create_view_fail:
109620 sqlite3SelectDelete(db, pSelect);
109621 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
109622 sqlite3RenameExprlistUnmap(pParse, pCNames);
109623 }
109624 sqlite3ExprListDelete(db, pCNames);
109625 return;
109626}
109627#endif /* SQLITE_OMIT_VIEW */
109628
109629#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
109630/*
109631** The Table structure pTable is really a VIEW. Fill in the names of
109632** the columns of the view in the pTable structure. Return the number
109633** of errors. If an error is seen leave an error message in pParse->zErrMsg.
109634*/
109635SQLITE_PRIVATEstatic int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
109636 Table *pSelTab; /* A fake table from which we get the result set */
109637 Select *pSel; /* Copy of the SELECT that implements the view */
109638 int nErr = 0; /* Number of errors encountered */
109639 int n; /* Temporarily holds the number of cursors assigned */
109640 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
109641#ifndef SQLITE_OMIT_VIRTUALTABLE
109642 int rc;
109643#endif
109644#ifndef SQLITE_OMIT_AUTHORIZATION
109645 sqlite3_xauth xAuth; /* Saved xAuth pointer */
109646#endif
109647
109648 assert( pTable )((void) (0));
109649
109650#ifndef SQLITE_OMIT_VIRTUALTABLE
109651 db->nSchemaLock++;
109652 rc = sqlite3VtabCallConnect(pParse, pTable);
109653 db->nSchemaLock--;
109654 if( rc ){
109655 return 1;
109656 }
109657 if( IsVirtual(pTable)((pTable)->nModuleArg) ) return 0;
109658#endif
109659
109660#ifndef SQLITE_OMIT_VIEW
109661 /* A positive nCol means the columns names for this view are
109662 ** already known.
109663 */
109664 if( pTable->nCol>0 ) return 0;
109665
109666 /* A negative nCol is a special marker meaning that we are currently
109667 ** trying to compute the column names. If we enter this routine with
109668 ** a negative nCol, it means two or more views form a loop, like this:
109669 **
109670 ** CREATE VIEW one AS SELECT * FROM two;
109671 ** CREATE VIEW two AS SELECT * FROM one;
109672 **
109673 ** Actually, the error above is now caught prior to reaching this point.
109674 ** But the following test is still important as it does come up
109675 ** in the following:
109676 **
109677 ** CREATE TABLE main.ex1(a);
109678 ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
109679 ** SELECT * FROM temp.ex1;
109680 */
109681 if( pTable->nCol<0 ){
109682 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
109683 return 1;
109684 }
109685 assert( pTable->nCol>=0 )((void) (0));
109686
109687 /* If we get this far, it means we need to compute the table names.
109688 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
109689 ** "*" elements in the results set of the view and will assign cursors
109690 ** to the elements of the FROM clause. But we do not want these changes
109691 ** to be permanent. So the computation is done on a copy of the SELECT
109692 ** statement that defines the view.
109693 */
109694 assert( pTable->pSelect )((void) (0));
109695 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
109696 if( pSel ){
109697#ifndef SQLITE_OMIT_ALTERTABLE
109698 u8 eParseMode = pParse->eParseMode;
109699 pParse->eParseMode = PARSE_MODE_NORMAL0;
109700#endif
109701 n = pParse->nTab;
109702 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
109703 pTable->nCol = -1;
109704 db->lookaside.bDisable++;
109705#ifndef SQLITE_OMIT_AUTHORIZATION
109706 xAuth = db->xAuth;
109707 db->xAuth = 0;
109708 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
109709 db->xAuth = xAuth;
109710#else
109711 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
109712#endif
109713 pParse->nTab = n;
109714 if( pTable->pCheck ){
109715 /* CREATE VIEW name(arglist) AS ...
109716 ** The names of the columns in the table are taken from
109717 ** arglist which is stored in pTable->pCheck. The pCheck field
109718 ** normally holds CHECK constraints on an ordinary table, but for
109719 ** a VIEW it holds the list of column names.
109720 */
109721 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
109722 &pTable->nCol, &pTable->aCol);
109723 if( db->mallocFailed==0
109724 && pParse->nErr==0
109725 && pTable->nCol==pSel->pEList->nExpr
109726 ){
109727 sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel);
109728 }
109729 }else if( pSelTab ){
109730 /* CREATE VIEW name AS... without an argument list. Construct
109731 ** the column names from the SELECT statement that defines the view.
109732 */
109733 assert( pTable->aCol==0 )((void) (0));
109734 pTable->nCol = pSelTab->nCol;
109735 pTable->aCol = pSelTab->aCol;
109736 pSelTab->nCol = 0;
109737 pSelTab->aCol = 0;
109738 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) )((void) (0));
109739 }else{
109740 pTable->nCol = 0;
109741 nErr++;
109742 }
109743 sqlite3DeleteTable(db, pSelTab);
109744 sqlite3SelectDelete(db, pSel);
109745 db->lookaside.bDisable--;
109746#ifndef SQLITE_OMIT_ALTERTABLE
109747 pParse->eParseMode = eParseMode;
109748#endif
109749 } else {
109750 nErr++;
109751 }
109752 pTable->pSchema->schemaFlags |= DB_UnresetViews0x0002;
109753 if( db->mallocFailed ){
109754 sqlite3DeleteColumnNames(db, pTable);
109755 pTable->aCol = 0;
109756 pTable->nCol = 0;
109757 }
109758#endif /* SQLITE_OMIT_VIEW */
109759 return nErr;
109760}
109761#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
109762
109763#ifndef SQLITE_OMIT_VIEW
109764/*
109765** Clear the column names from every VIEW in database idx.
109766*/
109767static void sqliteViewResetAll(sqlite3 *db, int idx){
109768 HashElem *i;
109769 assert( sqlite3SchemaMutexHeld(db, idx, 0) )((void) (0));
109770 if( !DbHasProperty(db, idx, DB_UnresetViews)(((db)->aDb[idx].pSchema->schemaFlags&(0x0002))==(0x0002
))
) return;
109771 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash)((&db->aDb[idx].pSchema->tblHash)->first); i;i=sqliteHashNext(i)((i)->next)){
109772 Table *pTab = sqliteHashData(i)((i)->data);
109773 if( pTab->pSelect ){
109774 sqlite3DeleteColumnNames(db, pTab);
109775 pTab->aCol = 0;
109776 pTab->nCol = 0;
109777 }
109778 }
109779 DbClearProperty(db, idx, DB_UnresetViews)(db)->aDb[idx].pSchema->schemaFlags&=~(0x0002);
109780}
109781#else
109782# define sqliteViewResetAll(A,B)
109783#endif /* SQLITE_OMIT_VIEW */
109784
109785/*
109786** This function is called by the VDBE to adjust the internal schema
109787** used by SQLite when the btree layer moves a table root page. The
109788** root-page of a table or index in database iDb has changed from iFrom
109789** to iTo.
109790**
109791** Ticket #1728: The symbol table might still contain information
109792** on tables and/or indices that are the process of being deleted.
109793** If you are unlucky, one of those deleted indices or tables might
109794** have the same rootpage number as the real table or index that is
109795** being moved. So we cannot stop searching after the first match
109796** because the first match might be for one of the deleted indices
109797** or tables and not the table/index that is actually being moved.
109798** We must continue looping until all tables and indices with
109799** rootpage==iFrom have been converted to have a rootpage of iTo
109800** in order to be certain that we got the right one.
109801*/
109802#ifndef SQLITE_OMIT_AUTOVACUUM
109803SQLITE_PRIVATEstatic void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
109804 HashElem *pElem;
109805 Hash *pHash;
109806 Db *pDb;
109807
109808 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
109809 pDb = &db->aDb[iDb];
109810 pHash = &pDb->pSchema->tblHash;
109811 for(pElem=sqliteHashFirst(pHash)((pHash)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
109812 Table *pTab = sqliteHashData(pElem)((pElem)->data);
109813 if( pTab->tnum==iFrom ){
109814 pTab->tnum = iTo;
109815 }
109816 }
109817 pHash = &pDb->pSchema->idxHash;
109818 for(pElem=sqliteHashFirst(pHash)((pHash)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
109819 Index *pIdx = sqliteHashData(pElem)((pElem)->data);
109820 if( pIdx->tnum==iFrom ){
109821 pIdx->tnum = iTo;
109822 }
109823 }
109824}
109825#endif
109826
109827/*
109828** Write code to erase the table with root-page iTable from database iDb.
109829** Also write code to modify the sqlite_master table and internal schema
109830** if a root-page of another table is moved by the btree-layer whilst
109831** erasing iTable (this can happen with an auto-vacuum database).
109832*/
109833static void destroyRootPage(Parse *pParse, int iTable, int iDb){
109834 Vdbe *v = sqlite3GetVdbe(pParse);
109835 int r1 = sqlite3GetTempReg(pParse);
109836 if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
109837 sqlite3VdbeAddOp3(v, OP_Destroy136, iTable, r1, iDb);
109838 sqlite3MayAbort(pParse);
109839#ifndef SQLITE_OMIT_AUTOVACUUM
109840 /* OP_Destroy stores an in integer r1. If this integer
109841 ** is non-zero, then it is the root page number of a table moved to
109842 ** location iTable. The following code modifies the sqlite_master table to
109843 ** reflect this.
109844 **
109845 ** The "#NNN" in the SQL is a special constant that means whatever value
109846 ** is in register NNN. See grammar rules associated with the TK_REGISTER
109847 ** token for additional information.
109848 */
109849 sqlite3NestedParse(pParse,
109850 "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
109851 pParse->db->aDb[iDb].zDbSName, MASTER_NAME"sqlite_master", iTable, r1, r1);
109852#endif
109853 sqlite3ReleaseTempReg(pParse, r1);
109854}
109855
109856/*
109857** Write VDBE code to erase table pTab and all associated indices on disk.
109858** Code to update the sqlite_master tables and internal schema definitions
109859** in case a root-page belonging to another table is moved by the btree layer
109860** is also added (this can happen with an auto-vacuum database).
109861*/
109862static void destroyTable(Parse *pParse, Table *pTab){
109863 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
109864 ** is not defined), then it is important to call OP_Destroy on the
109865 ** table and index root-pages in order, starting with the numerically
109866 ** largest root-page number. This guarantees that none of the root-pages
109867 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
109868 ** following were coded:
109869 **
109870 ** OP_Destroy 4 0
109871 ** ...
109872 ** OP_Destroy 5 0
109873 **
109874 ** and root page 5 happened to be the largest root-page number in the
109875 ** database, then root page 5 would be moved to page 4 by the
109876 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
109877 ** a free-list page.
109878 */
109879 int iTab = pTab->tnum;
109880 int iDestroyed = 0;
109881
109882 while( 1 ){
109883 Index *pIdx;
109884 int iLargest = 0;
109885
109886 if( iDestroyed==0 || iTab<iDestroyed ){
109887 iLargest = iTab;
109888 }
109889 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
109890 int iIdx = pIdx->tnum;
109891 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
109892 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
109893 iLargest = iIdx;
109894 }
109895 }
109896 if( iLargest==0 ){
109897 return;
109898 }else{
109899 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
109900 assert( iDb>=0 && iDb<pParse->db->nDb )((void) (0));
109901 destroyRootPage(pParse, iLargest, iDb);
109902 iDestroyed = iLargest;
109903 }
109904 }
109905}
109906
109907/*
109908** Remove entries from the sqlite_statN tables (for N in (1,2,3))
109909** after a DROP INDEX or DROP TABLE command.
109910*/
109911static void sqlite3ClearStatTables(
109912 Parse *pParse, /* The parsing context */
109913 int iDb, /* The database number */
109914 const char *zType, /* "idx" or "tbl" */
109915 const char *zName /* Name of index or table */
109916){
109917 int i;
109918 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
109919 for(i=1; i<=4; i++){
109920 char zTab[24];
109921 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
109922 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
109923 sqlite3NestedParse(pParse,
109924 "DELETE FROM %Q.%s WHERE %s=%Q",
109925 zDbName, zTab, zType, zName
109926 );
109927 }
109928 }
109929}
109930
109931/*
109932** Generate code to drop a table.
109933*/
109934SQLITE_PRIVATEstatic void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
109935 Vdbe *v;
109936 sqlite3 *db = pParse->db;
109937 Trigger *pTrigger;
109938 Db *pDb = &db->aDb[iDb];
109939
109940 v = sqlite3GetVdbe(pParse);
109941 assert( v!=0 )((void) (0));
109942 sqlite3BeginWriteOperation(pParse, 1, iDb);
109943
109944#ifndef SQLITE_OMIT_VIRTUALTABLE
109945 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
109946 sqlite3VdbeAddOp0(v, OP_VBegin160);
109947 }
109948#endif
109949
109950 /* Drop all triggers associated with the table being dropped. Code
109951 ** is generated to remove entries from sqlite_master and/or
109952 ** sqlite_temp_master if required.
109953 */
109954 pTrigger = sqlite3TriggerList(pParse, pTab);
109955 while( pTrigger ){
109956 assert( pTrigger->pSchema==pTab->pSchema ||((void) (0))
109957 pTrigger->pSchema==db->aDb[1].pSchema )((void) (0));
109958 sqlite3DropTriggerPtr(pParse, pTrigger);
109959 pTrigger = pTrigger->pNext;
109960 }
109961
109962#ifndef SQLITE_OMIT_AUTOINCREMENT
109963 /* Remove any entries of the sqlite_sequence table associated with
109964 ** the table being dropped. This is done before the table is dropped
109965 ** at the btree level, in case the sqlite_sequence table needs to
109966 ** move as a result of the drop (can happen in auto-vacuum mode).
109967 */
109968 if( pTab->tabFlags & TF_Autoincrement0x0008 ){
109969 sqlite3NestedParse(pParse,
109970 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
109971 pDb->zDbSName, pTab->zName
109972 );
109973 }
109974#endif
109975
109976 /* Drop all SQLITE_MASTER table and index entries that refer to the
109977 ** table. The program name loops through the master table and deletes
109978 ** every row that refers to a table of the same name as the one being
109979 ** dropped. Triggers are handled separately because a trigger can be
109980 ** created in the temp database that refers to a table in another
109981 ** database.
109982 */
109983 sqlite3NestedParse(pParse,
109984 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
109985 pDb->zDbSName, MASTER_NAME"sqlite_master", pTab->zName);
109986 if( !isView && !IsVirtual(pTab)((pTab)->nModuleArg) ){
109987 destroyTable(pParse, pTab);
109988 }
109989
109990 /* Remove the table entry from SQLite's internal schema and modify
109991 ** the schema cookie.
109992 */
109993 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
109994 sqlite3VdbeAddOp4(v, OP_VDestroy162, iDb, 0, 0, pTab->zName, 0);
109995 sqlite3MayAbort(pParse);
109996 }
109997 sqlite3VdbeAddOp4(v, OP_DropTable143, iDb, 0, 0, pTab->zName, 0);
109998 sqlite3ChangeCookie(pParse, iDb);
109999 sqliteViewResetAll(db, iDb);
110000}
110001
110002/*
110003** This routine is called to do the work of a DROP TABLE statement.
110004** pName is the name of the table to be dropped.
110005*/
110006SQLITE_PRIVATEstatic void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
110007 Table *pTab;
110008 Vdbe *v;
110009 sqlite3 *db = pParse->db;
110010 int iDb;
110011
110012 if( db->mallocFailed ){
110013 goto exit_drop_table;
110014 }
110015 assert( pParse->nErr==0 )((void) (0));
110016 assert( pName->nSrc==1 )((void) (0));
110017 if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
110018 if( noErr ) db->suppressErr++;
110019 assert( isView==0 || isView==LOCATE_VIEW )((void) (0));
110020 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
110021 if( noErr ) db->suppressErr--;
110022
110023 if( pTab==0 ){
110024 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
110025 goto exit_drop_table;
110026 }
110027 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110028 assert( iDb>=0 && iDb<db->nDb )((void) (0));
110029
110030 /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
110031 ** it is initialized.
110032 */
110033 if( IsVirtual(pTab)((pTab)->nModuleArg) && sqlite3ViewGetColumnNames(pParse, pTab) ){
110034 goto exit_drop_table;
110035 }
110036#ifndef SQLITE_OMIT_AUTHORIZATION
110037 {
110038 int code;
110039 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
110040 const char *zDb = db->aDb[iDb].zDbSName;
110041 const char *zArg2 = 0;
110042 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb)){
110043 goto exit_drop_table;
110044 }
110045 if( isView ){
110046 if( !OMIT_TEMPDB0 && iDb==1 ){
110047 code = SQLITE_DROP_TEMP_VIEW15;
110048 }else{
110049 code = SQLITE_DROP_VIEW17;
110050 }
110051#ifndef SQLITE_OMIT_VIRTUALTABLE
110052 }else if( IsVirtual(pTab)((pTab)->nModuleArg) ){
110053 code = SQLITE_DROP_VTABLE30;
110054 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
110055#endif
110056 }else{
110057 if( !OMIT_TEMPDB0 && iDb==1 ){
110058 code = SQLITE_DROP_TEMP_TABLE13;
110059 }else{
110060 code = SQLITE_DROP_TABLE11;
110061 }
110062 }
110063 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
110064 goto exit_drop_table;
110065 }
110066 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, pTab->zName, 0, zDb) ){
110067 goto exit_drop_table;
110068 }
110069 }
110070#endif
110071 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0
110072 && sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_stat", 11)!=0 ){
110073 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
110074 goto exit_drop_table;
110075 }
110076
110077#ifndef SQLITE_OMIT_VIEW
110078 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
110079 ** on a table.
110080 */
110081 if( isView && pTab->pSelect==0 ){
110082 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
110083 goto exit_drop_table;
110084 }
110085 if( !isView && pTab->pSelect ){
110086 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
110087 goto exit_drop_table;
110088 }
110089#endif
110090
110091 /* Generate code to remove the table from the master table
110092 ** on disk.
110093 */
110094 v = sqlite3GetVdbe(pParse);
110095 if( v ){
110096 sqlite3BeginWriteOperation(pParse, 1, iDb);
110097 if( !isView ){
110098 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
110099 sqlite3FkDropTable(pParse, pName, pTab);
110100 }
110101 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
110102 }
110103
110104exit_drop_table:
110105 sqlite3SrcListDelete(db, pName);
110106}
110107
110108/*
110109** This routine is called to create a new foreign key on the table
110110** currently under construction. pFromCol determines which columns
110111** in the current table point to the foreign key. If pFromCol==0 then
110112** connect the key to the last column inserted. pTo is the name of
110113** the table referred to (a.k.a the "parent" table). pToCol is a list
110114** of tables in the parent pTo table. flags contains all
110115** information about the conflict resolution algorithms specified
110116** in the ON DELETE, ON UPDATE and ON INSERT clauses.
110117**
110118** An FKey structure is created and added to the table currently
110119** under construction in the pParse->pNewTable field.
110120**
110121** The foreign key is set for IMMEDIATE processing. A subsequent call
110122** to sqlite3DeferForeignKey() might change this to DEFERRED.
110123*/
110124SQLITE_PRIVATEstatic void sqlite3CreateForeignKey(
110125 Parse *pParse, /* Parsing context */
110126 ExprList *pFromCol, /* Columns in this table that point to other table */
110127 Token *pTo, /* Name of the other table */
110128 ExprList *pToCol, /* Columns in the other table */
110129 int flags /* Conflict resolution algorithms. */
110130){
110131 sqlite3 *db = pParse->db;
110132#ifndef SQLITE_OMIT_FOREIGN_KEY
110133 FKey *pFKey = 0;
110134 FKey *pNextTo;
110135 Table *p = pParse->pNewTable;
110136 int nByte;
110137 int i;
110138 int nCol;
110139 char *z;
110140
110141 assert( pTo!=0 )((void) (0));
110142 if( p==0 || IN_DECLARE_VTAB(pParse->eParseMode==1) ) goto fk_end;
110143 if( pFromCol==0 ){
110144 int iCol = p->nCol-1;
110145 if( NEVER(iCol<0)(iCol<0) ) goto fk_end;
110146 if( pToCol && pToCol->nExpr!=1 ){
110147 sqlite3ErrorMsg(pParse, "foreign key on %s"
110148 " should reference only one column of table %T",
110149 p->aCol[iCol].zName, pTo);
110150 goto fk_end;
110151 }
110152 nCol = 1;
110153 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
110154 sqlite3ErrorMsg(pParse,
110155 "number of columns in foreign key does not match the number of "
110156 "columns in the referenced table");
110157 goto fk_end;
110158 }else{
110159 nCol = pFromCol->nExpr;
110160 }
110161 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
110162 if( pToCol ){
110163 for(i=0; i<pToCol->nExpr; i++){
110164 nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
110165 }
110166 }
110167 pFKey = sqlite3DbMallocZero(db, nByte );
110168 if( pFKey==0 ){
110169 goto fk_end;
110170 }
110171 pFKey->pFrom = p;
110172 pFKey->pNextFrom = p->pFKey;
110173 z = (char*)&pFKey->aCol[nCol];
110174 pFKey->zTo = z;
110175 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110176 sqlite3RenameTokenMap(pParse, (void*)z, pTo);
110177 }
110178 memcpy(z, pTo->z, pTo->n);
110179 z[pTo->n] = 0;
110180 sqlite3Dequote(z);
110181 z += pTo->n+1;
110182 pFKey->nCol = nCol;
110183 if( pFromCol==0 ){
110184 pFKey->aCol[0].iFrom = p->nCol-1;
110185 }else{
110186 for(i=0; i<nCol; i++){
110187 int j;
110188 for(j=0; j<p->nCol; j++){
110189 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
110190 pFKey->aCol[i].iFrom = j;
110191 break;
110192 }
110193 }
110194 if( j>=p->nCol ){
110195 sqlite3ErrorMsg(pParse,
110196 "unknown column \"%s\" in foreign key definition",
110197 pFromCol->a[i].zName);
110198 goto fk_end;
110199 }
110200 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110201 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName);
110202 }
110203 }
110204 }
110205 if( pToCol ){
110206 for(i=0; i<nCol; i++){
110207 int n = sqlite3Strlen30(pToCol->a[i].zName);
110208 pFKey->aCol[i].zCol = z;
110209 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110210 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName);
110211 }
110212 memcpy(z, pToCol->a[i].zName, n);
110213 z[n] = 0;
110214 z += n+1;
110215 }
110216 }
110217 pFKey->isDeferred = 0;
110218 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
110219 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
110220
110221 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) )((void) (0));
110222 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
110223 pFKey->zTo, (void *)pFKey
110224 );
110225 if( pNextTo==pFKey ){
110226 sqlite3OomFault(db);
110227 goto fk_end;
110228 }
110229 if( pNextTo ){
110230 assert( pNextTo->pPrevTo==0 )((void) (0));
110231 pFKey->pNextTo = pNextTo;
110232 pNextTo->pPrevTo = pFKey;
110233 }
110234
110235 /* Link the foreign key to the table as the last step.
110236 */
110237 p->pFKey = pFKey;
110238 pFKey = 0;
110239
110240fk_end:
110241 sqlite3DbFree(db, pFKey);
110242#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
110243 sqlite3ExprListDelete(db, pFromCol);
110244 sqlite3ExprListDelete(db, pToCol);
110245}
110246
110247/*
110248** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
110249** clause is seen as part of a foreign key definition. The isDeferred
110250** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
110251** The behavior of the most recently created foreign key is adjusted
110252** accordingly.
110253*/
110254SQLITE_PRIVATEstatic void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
110255#ifndef SQLITE_OMIT_FOREIGN_KEY
110256 Table *pTab;
110257 FKey *pFKey;
110258 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
110259 assert( isDeferred==0 || isDeferred==1 )((void) (0)); /* EV: R-30323-21917 */
110260 pFKey->isDeferred = (u8)isDeferred;
110261#endif
110262}
110263
110264/*
110265** Generate code that will erase and refill index *pIdx. This is
110266** used to initialize a newly created index or to recompute the
110267** content of an index in response to a REINDEX command.
110268**
110269** if memRootPage is not negative, it means that the index is newly
110270** created. The register specified by memRootPage contains the
110271** root page number of the index. If memRootPage is negative, then
110272** the index already exists and must be cleared before being refilled and
110273** the root page number of the index is taken from pIndex->tnum.
110274*/
110275static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
110276 Table *pTab = pIndex->pTable; /* The table that is indexed */
110277 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
110278 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
110279 int iSorter; /* Cursor opened by OpenSorter (if in use) */
110280 int addr1; /* Address of top of loop */
110281 int addr2; /* Address to jump to for next iteration */
110282 int tnum; /* Root page of index */
110283 int iPartIdxLabel; /* Jump to this label to skip a row */
110284 Vdbe *v; /* Generate code into this virtual machine */
110285 KeyInfo *pKey; /* KeyInfo for index */
110286 int regRecord; /* Register holding assembled index record */
110287 sqlite3 *db = pParse->db; /* The database connection */
110288 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
110289
110290#ifndef SQLITE_OMIT_AUTHORIZATION
110291 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX27, pIndex->zName, 0,
110292 db->aDb[iDb].zDbSName ) ){
110293 return;
110294 }
110295#endif
110296
110297 /* Require a write-lock on the table to perform this operation */
110298 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
110299
110300 v = sqlite3GetVdbe(pParse);
110301 if( v==0 ) return;
110302 if( memRootPage>=0 ){
110303 tnum = memRootPage;
110304 }else{
110305 tnum = pIndex->tnum;
110306 }
110307 pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
110308 assert( pKey!=0 || db->mallocFailed || pParse->nErr )((void) (0));
110309
110310 /* Open the sorter cursor if we are to use one. */
110311 iSorter = pParse->nTab++;
110312 sqlite3VdbeAddOp4(v, OP_SorterOpen114, iSorter, 0, pIndex->nKeyCol, (char*)
110313 sqlite3KeyInfoRef(pKey), P4_KEYINFO(-9));
110314
110315 /* Open the table. Loop through all rows of the table, inserting index
110316 ** records into the sorter. */
110317 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead108);
110318 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind36, iTab, 0); VdbeCoverage(v);
110319 regRecord = sqlite3GetTempReg(pParse);
110320 sqlite3MultiWrite(pParse);
110321
110322 sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
110323 sqlite3VdbeAddOp2(v, OP_SorterInsert131, iSorter, regRecord);
110324 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
110325 sqlite3VdbeAddOp2(v, OP_Next5, iTab, addr1+1); VdbeCoverage(v);
110326 sqlite3VdbeJumpHere(v, addr1);
110327 if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear137, tnum, iDb);
110328 sqlite3VdbeAddOp4(v, OP_OpenWrite109, iIdx, tnum, iDb,
110329 (char *)pKey, P4_KEYINFO(-9));
110330 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR0x01|((memRootPage>=0)?OPFLAG_P2ISREG0x10:0));
110331
110332 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort34, iSorter, 0); VdbeCoverage(v);
110333 if( IsUniqueIndex(pIndex)((pIndex)->onError!=0) ){
110334 int j2 = sqlite3VdbeGoto(v, 1);
110335 addr2 = sqlite3VdbeCurrentAddr(v);
110336 sqlite3VdbeVerifyAbortable(v, OE_Abort);
110337 sqlite3VdbeAddOp4Int(v, OP_SorterCompare125, iSorter, j2, regRecord,
110338 pIndex->nKeyCol); VdbeCoverage(v);
110339 sqlite3UniqueConstraint(pParse, OE_Abort2, pIndex);
110340 sqlite3VdbeJumpHere(v, j2);
110341 }else{
110342 /* Most CREATE INDEX and REINDEX statements that are not UNIQUE can not
110343 ** abort. The exception is if one of the indexed expressions contains a
110344 ** user function that throws an exception when it is evaluated. But the
110345 ** overhead of adding a statement journal to a CREATE INDEX statement is
110346 ** very small (since most of the pages written do not contain content that
110347 ** needs to be restored if the statement aborts), so we call
110348 ** sqlite3MayAbort() for all CREATE INDEX statements. */
110349 sqlite3MayAbort(pParse);
110350 addr2 = sqlite3VdbeCurrentAddr(v);
110351 }
110352 sqlite3VdbeAddOp3(v, OP_SorterData126, iSorter, regRecord, iIdx);
110353 if( !pIndex->bAscKeyBug ){
110354 /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
110355 ** faster by avoiding unnecessary seeks. But the optimization does
110356 ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
110357 ** with DESC primary keys, since those indexes have there keys in
110358 ** a different order from the main table.
110359 ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
110360 */
110361 sqlite3VdbeAddOp1(v, OP_SeekEnd130, iIdx);
110362 }
110363 sqlite3VdbeAddOp2(v, OP_IdxInsert132, iIdx, regRecord);
110364 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
110365 sqlite3ReleaseTempReg(pParse, regRecord);
110366 sqlite3VdbeAddOp2(v, OP_SorterNext3, iSorter, addr2); VdbeCoverage(v);
110367 sqlite3VdbeJumpHere(v, addr1);
110368
110369 sqlite3VdbeAddOp1(v, OP_Close117, iTab);
110370 sqlite3VdbeAddOp1(v, OP_Close117, iIdx);
110371 sqlite3VdbeAddOp1(v, OP_Close117, iSorter);
110372}
110373
110374/*
110375** Allocate heap space to hold an Index object with nCol columns.
110376**
110377** Increase the allocation size to provide an extra nExtra bytes
110378** of 8-byte aligned space after the Index object and return a
110379** pointer to this extra space in *ppExtra.
110380*/
110381SQLITE_PRIVATEstatic Index *sqlite3AllocateIndexObject(
110382 sqlite3 *db, /* Database connection */
110383 i16 nCol, /* Total number of columns in the index */
110384 int nExtra, /* Number of bytes of extra space to alloc */
110385 char **ppExtra /* Pointer to the "extra" space */
110386){
110387 Index *p; /* Allocated index object */
110388 int nByte; /* Bytes of space for Index object + arrays */
110389
110390 nByte = ROUND8(sizeof(Index))(((sizeof(Index))+7)&~7) + /* Index structure */
110391 ROUND8(sizeof(char*)*nCol)(((sizeof(char*)*nCol)+7)&~7) + /* Index.azColl */
110392 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
110393 sizeof(i16)*nCol + /* Index.aiColumn */(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
110394 sizeof(u8)*nCol)(((sizeof(LogEst)*(nCol+1) + sizeof(i16)*nCol + sizeof(u8)*nCol
)+7)&~7)
; /* Index.aSortOrder */
110395 p = sqlite3DbMallocZero(db, nByte + nExtra);
110396 if( p ){
110397 char *pExtra = ((char*)p)+ROUND8(sizeof(Index))(((sizeof(Index))+7)&~7);
110398 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol)(((sizeof(char*)*nCol)+7)&~7);
110399 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
110400 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
110401 p->aSortOrder = (u8*)pExtra;
110402 p->nColumn = nCol;
110403 p->nKeyCol = nCol - 1;
110404 *ppExtra = ((char*)p) + nByte;
110405 }
110406 return p;
110407}
110408
110409/*
110410** Create a new index for an SQL table. pName1.pName2 is the name of the index
110411** and pTblList is the name of the table that is to be indexed. Both will
110412** be NULL for a primary key or an index that is created to satisfy a
110413** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
110414** as the table to be indexed. pParse->pNewTable is a table that is
110415** currently being constructed by a CREATE TABLE statement.
110416**
110417** pList is a list of columns to be indexed. pList will be NULL if this
110418** is a primary key or unique-constraint on the most recent column added
110419** to the table currently under construction.
110420*/
110421SQLITE_PRIVATEstatic void sqlite3CreateIndex(
110422 Parse *pParse, /* All information about this parse */
110423 Token *pName1, /* First part of index name. May be NULL */
110424 Token *pName2, /* Second part of index name. May be NULL */
110425 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
110426 ExprList *pList, /* A list of columns to be indexed */
110427 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
110428 Token *pStart, /* The CREATE token that begins this statement */
110429 Expr *pPIWhere, /* WHERE clause for partial indices */
110430 int sortOrder, /* Sort order of primary key when pList==NULL */
110431 int ifNotExist, /* Omit error if index already exists */
110432 u8 idxType /* The index type */
110433){
110434 Table *pTab = 0; /* Table to be indexed */
110435 Index *pIndex = 0; /* The index to be created */
110436 char *zName = 0; /* Name of the index */
110437 int nName; /* Number of characters in zName */
110438 int i, j;
110439 DbFixer sFix; /* For assigning database names to pTable */
110440 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */
110441 sqlite3 *db = pParse->db;
110442 Db *pDb; /* The specific table containing the indexed database */
110443 int iDb; /* Index of the database that is being written */
110444 Token *pName = 0; /* Unqualified name of the index to create */
110445 struct ExprList_item *pListItem; /* For looping over pList */
110446 int nExtra = 0; /* Space allocated for zExtra[] */
110447 int nExtraCol; /* Number of extra columns needed */
110448 char *zExtra = 0; /* Extra space after the Index object */
110449 Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */
110450
110451 if( db->mallocFailed || pParse->nErr>0 ){
110452 goto exit_create_index;
110453 }
110454 if( IN_DECLARE_VTAB(pParse->eParseMode==1) && idxType!=SQLITE_IDXTYPE_PRIMARYKEY2 ){
110455 goto exit_create_index;
110456 }
110457 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
110458 goto exit_create_index;
110459 }
110460
110461 /*
110462 ** Find the table that is to be indexed. Return early if not found.
110463 */
110464 if( pTblName!=0 ){
110465
110466 /* Use the two-part index name to determine the database
110467 ** to search for the table. 'Fix' the table name to this db
110468 ** before looking up the table.
110469 */
110470 assert( pName1 && pName2 )((void) (0));
110471 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
110472 if( iDb<0 ) goto exit_create_index;
110473 assert( pName && pName->z )((void) (0));
110474
110475#ifndef SQLITE_OMIT_TEMPDB
110476 /* If the index name was unqualified, check if the table
110477 ** is a temp table. If so, set the database to 1. Do not do this
110478 ** if initialising a database schema.
110479 */
110480 if( !db->init.busy ){
110481 pTab = sqlite3SrcListLookup(pParse, pTblName);
110482 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
110483 iDb = 1;
110484 }
110485 }
110486#endif
110487
110488 sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
110489 if( sqlite3FixSrcList(&sFix, pTblName) ){
110490 /* Because the parser constructs pTblName from a single identifier,
110491 ** sqlite3FixSrcList can never fail. */
110492 assert(0)((void) (0));
110493 }
110494 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
110495 assert( db->mallocFailed==0 || pTab==0 )((void) (0));
110496 if( pTab==0 ) goto exit_create_index;
110497 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
110498 sqlite3ErrorMsg(pParse,
110499 "cannot create a TEMP index on non-TEMP table \"%s\"",
110500 pTab->zName);
110501 goto exit_create_index;
110502 }
110503 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ) pPk = sqlite3PrimaryKeyIndex(pTab);
110504 }else{
110505 assert( pName==0 )((void) (0));
110506 assert( pStart==0 )((void) (0));
110507 pTab = pParse->pNewTable;
110508 if( !pTab ) goto exit_create_index;
110509 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110510 }
110511 pDb = &db->aDb[iDb];
110512
110513 assert( pTab!=0 )((void) (0));
110514 assert( pParse->nErr==0 )((void) (0));
110515 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0
110516 && db->init.busy==0
110517 && pTblName!=0
110518#if SQLITE_USER_AUTHENTICATION
110519 && sqlite3UserAuthTable(pTab->zName)==0
110520#endif
110521#ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
110522 && sqlite3StrICmp(&pTab->zName[7],"master")!=0
110523#endif
110524 ){
110525 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
110526 goto exit_create_index;
110527 }
110528#ifndef SQLITE_OMIT_VIEW
110529 if( pTab->pSelect ){
110530 sqlite3ErrorMsg(pParse, "views may not be indexed");
110531 goto exit_create_index;
110532 }
110533#endif
110534#ifndef SQLITE_OMIT_VIRTUALTABLE
110535 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
110536 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
110537 goto exit_create_index;
110538 }
110539#endif
110540
110541 /*
110542 ** Find the name of the index. Make sure there is not already another
110543 ** index or table with the same name.
110544 **
110545 ** Exception: If we are reading the names of permanent indices from the
110546 ** sqlite_master table (because some other process changed the schema) and
110547 ** one of the index names collides with the name of a temporary table or
110548 ** index, then we will continue to process this index.
110549 **
110550 ** If pName==0 it means that we are
110551 ** dealing with a primary key or UNIQUE constraint. We have to invent our
110552 ** own name.
110553 */
110554 if( pName ){
110555 zName = sqlite3NameFromToken(db, pName);
110556 if( zName==0 ) goto exit_create_index;
110557 assert( pName->z!=0 )((void) (0));
110558 if( SQLITE_OK0!=sqlite3CheckObjectName(pParse, zName) ){
110559 goto exit_create_index;
110560 }
110561 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110562 if( !db->init.busy ){
110563 if( sqlite3FindTable(db, zName, 0)!=0 ){
110564 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
110565 goto exit_create_index;
110566 }
110567 }
110568 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
110569 if( !ifNotExist ){
110570 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
110571 }else{
110572 assert( !db->init.busy )((void) (0));
110573 sqlite3CodeVerifySchema(pParse, iDb);
110574 }
110575 goto exit_create_index;
110576 }
110577 }
110578 }else{
110579 int n;
110580 Index *pLoop;
110581 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
110582 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
110583 if( zName==0 ){
110584 goto exit_create_index;
110585 }
110586
110587 /* Automatic index names generated from within sqlite3_declare_vtab()
110588 ** must have names that are distinct from normal automatic index names.
110589 ** The following statement converts "sqlite3_autoindex..." into
110590 ** "sqlite3_butoindex..." in order to make the names distinct.
110591 ** The "vtab_err.test" test demonstrates the need of this statement. */
110592 if( IN_SPECIAL_PARSE(pParse->eParseMode!=0) ) zName[7]++;
110593 }
110594
110595 /* Check for authorization to create an index.
110596 */
110597#ifndef SQLITE_OMIT_AUTHORIZATION
110598 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110599 const char *zDb = pDb->zDbSName;
110600 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master"), 0, zDb) ){
110601 goto exit_create_index;
110602 }
110603 i = SQLITE_CREATE_INDEX1;
110604 if( !OMIT_TEMPDB0 && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX3;
110605 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
110606 goto exit_create_index;
110607 }
110608 }
110609#endif
110610
110611 /* If pList==0, it means this routine was called to make a primary
110612 ** key out of the last column added to the table under construction.
110613 ** So create a fake list to simulate this.
110614 */
110615 if( pList==0 ){
110616 Token prevCol;
110617 Column *pCol = &pTab->aCol[pTab->nCol-1];
110618 pCol->colFlags |= COLFLAG_UNIQUE0x0008;
110619 sqlite3TokenInit(&prevCol, pCol->zName);
110620 pList = sqlite3ExprListAppend(pParse, 0,
110621 sqlite3ExprAlloc(db, TK_ID59, &prevCol, 0));
110622 if( pList==0 ) goto exit_create_index;
110623 assert( pList->nExpr==1 )((void) (0));
110624 sqlite3ExprListSetSortOrder(pList, sortOrder);
110625 }else{
110626 sqlite3ExprListCheckLength(pParse, pList, "index");
110627 if( pParse->nErr ) goto exit_create_index;
110628 }
110629
110630 /* Figure out how many bytes of space are required to store explicitly
110631 ** specified collation sequence names.
110632 */
110633 for(i=0; i<pList->nExpr; i++){
110634 Expr *pExpr = pList->a[i].pExpr;
110635 assert( pExpr!=0 )((void) (0));
110636 if( pExpr->op==TK_COLLATE106 ){
110637 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
110638 }
110639 }
110640
110641 /*
110642 ** Allocate the index structure.
110643 */
110644 nName = sqlite3Strlen30(zName);
110645 nExtraCol = pPk ? pPk->nKeyCol : 1;
110646 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ )((void) (0));
110647 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
110648 nName + nExtra + 1, &zExtra);
110649 if( db->mallocFailed ){
110650 goto exit_create_index;
110651 }
110652 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) )((void) (0));
110653 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) )((void) (0));
110654 pIndex->zName = zExtra;
110655 zExtra += nName + 1;
110656 memcpy(pIndex->zName, zName, nName+1);
110657 pIndex->pTable = pTab;
110658 pIndex->onError = (u8)onError;
110659 pIndex->uniqNotNull = onError!=OE_None0;
110660 pIndex->idxType = idxType;
110661 pIndex->pSchema = db->aDb[iDb].pSchema;
110662 pIndex->nKeyCol = pList->nExpr;
110663 if( pPIWhere ){
110664 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx0x0002, pPIWhere, 0);
110665 pIndex->pPartIdxWhere = pPIWhere;
110666 pPIWhere = 0;
110667 }
110668 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
110669
110670 /* Check to see if we should honor DESC requests on index columns
110671 */
110672 if( pDb->pSchema->file_format>=4 ){
110673 sortOrderMask = -1; /* Honor DESC */
110674 }else{
110675 sortOrderMask = 0; /* Ignore DESC */
110676 }
110677
110678 /* Analyze the list of expressions that form the terms of the index and
110679 ** report any errors. In the common case where the expression is exactly
110680 ** a table column, store that column in aiColumn[]. For general expressions,
110681 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
110682 **
110683 ** TODO: Issue a warning if two or more columns of the index are identical.
110684 ** TODO: Issue a warning if the table primary key is used as part of the
110685 ** index key.
110686 */
110687 pListItem = pList->a;
110688 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110689 pIndex->aColExpr = pList;
110690 pList = 0;
110691 }
110692 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
110693 Expr *pCExpr; /* The i-th index expression */
110694 int requestedSortOrder; /* ASC or DESC on the i-th expression */
110695 const char *zColl; /* Collation sequence name */
110696
110697 sqlite3StringToId(pListItem->pExpr);
110698 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr0x0020, pListItem->pExpr, 0);
110699 if( pParse->nErr ) goto exit_create_index;
110700 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
110701 if( pCExpr->op!=TK_COLUMN162 ){
110702 if( pTab==pParse->pNewTable ){
110703 sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
110704 "UNIQUE constraints");
110705 goto exit_create_index;
110706 }
110707 if( pIndex->aColExpr==0 ){
110708 pIndex->aColExpr = pList;
110709 pList = 0;
110710 }
110711 j = XN_EXPR(-2);
110712 pIndex->aiColumn[i] = XN_EXPR(-2);
110713 pIndex->uniqNotNull = 0;
110714 }else{
110715 j = pCExpr->iColumn;
110716 assert( j<=0x7fff )((void) (0));
110717 if( j<0 ){
110718 j = pTab->iPKey;
110719 }else if( pTab->aCol[j].notNull==0 ){
110720 pIndex->uniqNotNull = 0;
110721 }
110722 pIndex->aiColumn[i] = (i16)j;
110723 }
110724 zColl = 0;
110725 if( pListItem->pExpr->op==TK_COLLATE106 ){
110726 int nColl;
110727 zColl = pListItem->pExpr->u.zToken;
110728 nColl = sqlite3Strlen30(zColl) + 1;
110729 assert( nExtra>=nColl )((void) (0));
110730 memcpy(zExtra, zColl, nColl);
110731 zColl = zExtra;
110732 zExtra += nColl;
110733 nExtra -= nColl;
110734 }else if( j>=0 ){
110735 zColl = pTab->aCol[j].zColl;
110736 }
110737 if( !zColl ) zColl = sqlite3StrBINARY;
110738 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
110739 goto exit_create_index;
110740 }
110741 pIndex->azColl[i] = zColl;
110742 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
110743 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
110744 }
110745
110746 /* Append the table key to the end of the index. For WITHOUT ROWID
110747 ** tables (when pPk!=0) this will be the declared PRIMARY KEY. For
110748 ** normal tables (when pPk==0) this will be the rowid.
110749 */
110750 if( pPk ){
110751 for(j=0; j<pPk->nKeyCol; j++){
110752 int x = pPk->aiColumn[j];
110753 assert( x>=0 )((void) (0));
110754 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
110755 pIndex->nColumn--;
110756 }else{
110757 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
110758 pIndex->aiColumn[i] = x;
110759 pIndex->azColl[i] = pPk->azColl[j];
110760 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
110761 i++;
110762 }
110763 }
110764 assert( i==pIndex->nColumn )((void) (0));
110765 }else{
110766 pIndex->aiColumn[i] = XN_ROWID(-1);
110767 pIndex->azColl[i] = sqlite3StrBINARY;
110768 }
110769 sqlite3DefaultRowEst(pIndex);
110770 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
110771
110772 /* If this index contains every column of its table, then mark
110773 ** it as a covering index */
110774 assert( HasRowid(pTab)((void) (0))
110775 || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 )((void) (0));
110776 recomputeColumnsNotIndexed(pIndex);
110777 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
110778 pIndex->isCovering = 1;
110779 for(j=0; j<pTab->nCol; j++){
110780 if( j==pTab->iPKey ) continue;
110781 if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue;
110782 pIndex->isCovering = 0;
110783 break;
110784 }
110785 }
110786
110787 if( pTab==pParse->pNewTable ){
110788 /* This routine has been called to create an automatic index as a
110789 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
110790 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
110791 ** i.e. one of:
110792 **
110793 ** CREATE TABLE t(x PRIMARY KEY, y);
110794 ** CREATE TABLE t(x, y, UNIQUE(x, y));
110795 **
110796 ** Either way, check to see if the table already has such an index. If
110797 ** so, don't bother creating this one. This only applies to
110798 ** automatically created indices. Users can do as they wish with
110799 ** explicit indices.
110800 **
110801 ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
110802 ** (and thus suppressing the second one) even if they have different
110803 ** sort orders.
110804 **
110805 ** If there are different collating sequences or if the columns of
110806 ** the constraint occur in different orders, then the constraints are
110807 ** considered distinct and both result in separate indices.
110808 */
110809 Index *pIdx;
110810 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
110811 int k;
110812 assert( IsUniqueIndex(pIdx) )((void) (0));
110813 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF )((void) (0));
110814 assert( IsUniqueIndex(pIndex) )((void) (0));
110815
110816 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
110817 for(k=0; k<pIdx->nKeyCol; k++){
110818 const char *z1;
110819 const char *z2;
110820 assert( pIdx->aiColumn[k]>=0 )((void) (0));
110821 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
110822 z1 = pIdx->azColl[k];
110823 z2 = pIndex->azColl[k];
110824 if( sqlite3StrICmp(z1, z2) ) break;
110825 }
110826 if( k==pIdx->nKeyCol ){
110827 if( pIdx->onError!=pIndex->onError ){
110828 /* This constraint creates the same index as a previous
110829 ** constraint specified somewhere in the CREATE TABLE statement.
110830 ** However the ON CONFLICT clauses are different. If both this
110831 ** constraint and the previous equivalent constraint have explicit
110832 ** ON CONFLICT clauses this is an error. Otherwise, use the
110833 ** explicitly specified behavior for the index.
110834 */
110835 if( !(pIdx->onError==OE_Default11 || pIndex->onError==OE_Default11) ){
110836 sqlite3ErrorMsg(pParse,
110837 "conflicting ON CONFLICT clauses specified", 0);
110838 }
110839 if( pIdx->onError==OE_Default11 ){
110840 pIdx->onError = pIndex->onError;
110841 }
110842 }
110843 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ) pIdx->idxType = idxType;
110844 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110845 pIndex->pNext = pParse->pNewIndex;
110846 pParse->pNewIndex = pIndex;
110847 pIndex = 0;
110848 }
110849 goto exit_create_index;
110850 }
110851 }
110852 }
110853
110854 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110855
110856 /* Link the new Index structure to its table and to the other
110857 ** in-memory database structures.
110858 */
110859 assert( pParse->nErr==0 )((void) (0));
110860 if( db->init.busy ){
110861 Index *p;
110862 assert( !IN_SPECIAL_PARSE )((void) (0));
110863 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) )((void) (0));
110864 if( pTblName!=0 ){
110865 pIndex->tnum = db->init.newTnum;
110866 if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
110867 sqlite3ErrorMsg(pParse, "invalid rootpage");
110868 pParse->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(110868);
110869 goto exit_create_index;
110870 }
110871 }
110872 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
110873 pIndex->zName, pIndex);
110874 if( p ){
110875 assert( p==pIndex )((void) (0)); /* Malloc must have failed */
110876 sqlite3OomFault(db);
110877 goto exit_create_index;
110878 }
110879 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
110880 }
110881
110882 /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
110883 ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
110884 ** emit code to allocate the index rootpage on disk and make an entry for
110885 ** the index in the sqlite_master table and populate the index with
110886 ** content. But, do not do this if we are simply reading the sqlite_master
110887 ** table to parse the schema, or if this index is the PRIMARY KEY index
110888 ** of a WITHOUT ROWID table.
110889 **
110890 ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
110891 ** or UNIQUE index in a CREATE TABLE statement. Since the table
110892 ** has just been created, it contains no data and the index initialization
110893 ** step can be skipped.
110894 */
110895 else if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) || pTblName!=0 ){
110896 Vdbe *v;
110897 char *zStmt;
110898 int iMem = ++pParse->nMem;
110899
110900 v = sqlite3GetVdbe(pParse);
110901 if( v==0 ) goto exit_create_index;
110902
110903 sqlite3BeginWriteOperation(pParse, 1, iDb);
110904
110905 /* Create the rootpage for the index using CreateIndex. But before
110906 ** doing so, code a Noop instruction and store its address in
110907 ** Index.tnum. This is required in case this index is actually a
110908 ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
110909 ** that case the convertToWithoutRowidTable() routine will replace
110910 ** the Noop with a Goto to jump over the VDBE code generated below. */
110911 pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop170);
110912 sqlite3VdbeAddOp3(v, OP_CreateBtree139, iDb, iMem, BTREE_BLOBKEY2);
110913
110914 /* Gather the complete text of the CREATE INDEX statement into
110915 ** the zStmt variable
110916 */
110917 if( pStart ){
110918 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
110919 if( pName->z[n-1]==';' ) n--;
110920 /* A named index with an explicit CREATE INDEX statement */
110921 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
110922 onError==OE_None0 ? "" : " UNIQUE", n, pName->z);
110923 }else{
110924 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
110925 /* zStmt = sqlite3MPrintf(""); */
110926 zStmt = 0;
110927 }
110928
110929 /* Add an entry in sqlite_master for this index
110930 */
110931 sqlite3NestedParse(pParse,
110932 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
110933 db->aDb[iDb].zDbSName, MASTER_NAME"sqlite_master",
110934 pIndex->zName,
110935 pTab->zName,
110936 iMem,
110937 zStmt
110938 );
110939 sqlite3DbFree(db, zStmt);
110940
110941 /* Fill the index with data and reparse the schema. Code an OP_Expire
110942 ** to invalidate all pre-compiled statements.
110943 */
110944 if( pTblName ){
110945 sqlite3RefillIndex(pParse, pIndex, iMem);
110946 sqlite3ChangeCookie(pParse, iDb);
110947 sqlite3VdbeAddParseSchemaOp(v, iDb,
110948 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
110949 sqlite3VdbeAddOp2(v, OP_Expire158, 0, 1);
110950 }
110951
110952 sqlite3VdbeJumpHere(v, pIndex->tnum);
110953 }
110954 }
110955
110956 /* When adding an index to the list of indices for a table, make
110957 ** sure all indices labeled OE_Replace come after all those labeled
110958 ** OE_Ignore. This is necessary for the correct constraint check
110959 ** processing (in sqlite3GenerateConstraintChecks()) as part of
110960 ** UPDATE and INSERT statements.
110961 */
110962 if( db->init.busy || pTblName==0 ){
110963 if( onError!=OE_Replace5 || pTab->pIndex==0
110964 || pTab->pIndex->onError==OE_Replace5){
110965 pIndex->pNext = pTab->pIndex;
110966 pTab->pIndex = pIndex;
110967 }else{
110968 Index *pOther = pTab->pIndex;
110969 while( pOther->pNext && pOther->pNext->onError!=OE_Replace5 ){
110970 pOther = pOther->pNext;
110971 }
110972 pIndex->pNext = pOther->pNext;
110973 pOther->pNext = pIndex;
110974 }
110975 pIndex = 0;
110976 }
110977 else if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
110978 assert( pParse->pNewIndex==0 )((void) (0));
110979 pParse->pNewIndex = pIndex;
110980 pIndex = 0;
110981 }
110982
110983 /* Clean up before exiting */
110984exit_create_index:
110985 if( pIndex ) sqlite3FreeIndex(db, pIndex);
110986 sqlite3ExprDelete(db, pPIWhere);
110987 sqlite3ExprListDelete(db, pList);
110988 sqlite3SrcListDelete(db, pTblName);
110989 sqlite3DbFree(db, zName);
110990}
110991
110992/*
110993** Fill the Index.aiRowEst[] array with default information - information
110994** to be used when we have not run the ANALYZE command.
110995**
110996** aiRowEst[0] is supposed to contain the number of elements in the index.
110997** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
110998** number of rows in the table that match any particular value of the
110999** first column of the index. aiRowEst[2] is an estimate of the number
111000** of rows that match any particular combination of the first 2 columns
111001** of the index. And so forth. It must always be the case that
111002*
111003** aiRowEst[N]<=aiRowEst[N-1]
111004** aiRowEst[N]>=1
111005**
111006** Apart from that, we have little to go on besides intuition as to
111007** how aiRowEst[] should be initialized. The numbers generated here
111008** are based on typical values found in actual indices.
111009*/
111010SQLITE_PRIVATEstatic void sqlite3DefaultRowEst(Index *pIdx){
111011 /* 10, 9, 8, 7, 6 */
111012 LogEst aVal[] = { 33, 32, 30, 28, 26 };
111013 LogEst *a = pIdx->aiRowLogEst;
111014 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol)((((int)(sizeof(aVal)/sizeof(aVal[0]))))<(pIdx->nKeyCol
)?(((int)(sizeof(aVal)/sizeof(aVal[0])))):(pIdx->nKeyCol))
;
111015 int i;
111016
111017 /* Indexes with default row estimates should not have stat1 data */
111018 assert( !pIdx->hasStat1 )((void) (0));
111019
111020 /* Set the first entry (number of rows in the index) to the estimated
111021 ** number of rows in the table, or half the number of rows in the table
111022 ** for a partial index. But do not let the estimate drop below 10. */
111023 a[0] = pIdx->pTable->nRowLogEst;
111024 if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10; assert( 10==sqlite3LogEst(2) )((void) (0));
111025 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) )((void) (0));
111026
111027 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
111028 ** 6 and each subsequent value (if any) is 5. */
111029 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
111030 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
111031 a[i] = 23; assert( 23==sqlite3LogEst(5) )((void) (0));
111032 }
111033
111034 assert( 0==sqlite3LogEst(1) )((void) (0));
111035 if( IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) a[pIdx->nKeyCol] = 0;
111036}
111037
111038/*
111039** This routine will drop an existing named index. This routine
111040** implements the DROP INDEX statement.
111041*/
111042SQLITE_PRIVATEstatic void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
111043 Index *pIndex;
111044 Vdbe *v;
111045 sqlite3 *db = pParse->db;
111046 int iDb;
111047
111048 assert( pParse->nErr==0 )((void) (0)); /* Never called with prior errors */
111049 if( db->mallocFailed ){
111050 goto exit_drop_index;
111051 }
111052 assert( pName->nSrc==1 )((void) (0));
111053 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
111054 goto exit_drop_index;
111055 }
111056 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
111057 if( pIndex==0 ){
111058 if( !ifExists ){
111059 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
111060 }else{
111061 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
111062 }
111063 pParse->checkSchema = 1;
111064 goto exit_drop_index;
111065 }
111066 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF0 ){
111067 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
111068 "or PRIMARY KEY constraint cannot be dropped", 0);
111069 goto exit_drop_index;
111070 }
111071 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
111072#ifndef SQLITE_OMIT_AUTHORIZATION
111073 {
111074 int code = SQLITE_DROP_INDEX10;
111075 Table *pTab = pIndex->pTable;
111076 const char *zDb = db->aDb[iDb].zDbSName;
111077 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
111078 if( sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb) ){
111079 goto exit_drop_index;
111080 }
111081 if( !OMIT_TEMPDB0 && iDb ) code = SQLITE_DROP_TEMP_INDEX12;
111082 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
111083 goto exit_drop_index;
111084 }
111085 }
111086#endif
111087
111088 /* Generate code to remove the index and from the master table */
111089 v = sqlite3GetVdbe(pParse);
111090 if( v ){
111091 sqlite3BeginWriteOperation(pParse, 1, iDb);
111092 sqlite3NestedParse(pParse,
111093 "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
111094 db->aDb[iDb].zDbSName, MASTER_NAME"sqlite_master", pIndex->zName
111095 );
111096 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
111097 sqlite3ChangeCookie(pParse, iDb);
111098 destroyRootPage(pParse, pIndex->tnum, iDb);
111099 sqlite3VdbeAddOp4(v, OP_DropIndex144, iDb, 0, 0, pIndex->zName, 0);
111100 }
111101
111102exit_drop_index:
111103 sqlite3SrcListDelete(db, pName);
111104}
111105
111106/*
111107** pArray is a pointer to an array of objects. Each object in the
111108** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
111109** to extend the array so that there is space for a new object at the end.
111110**
111111** When this function is called, *pnEntry contains the current size of
111112** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
111113** in total).
111114**
111115** If the realloc() is successful (i.e. if no OOM condition occurs), the
111116** space allocated for the new object is zeroed, *pnEntry updated to
111117** reflect the new size of the array and a pointer to the new allocation
111118** returned. *pIdx is set to the index of the new array entry in this case.
111119**
111120** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
111121** unchanged and a copy of pArray returned.
111122*/
111123SQLITE_PRIVATEstatic void *sqlite3ArrayAllocate(
111124 sqlite3 *db, /* Connection to notify of malloc failures */
111125 void *pArray, /* Array of objects. Might be reallocated */
111126 int szEntry, /* Size of each object in the array */
111127 int *pnEntry, /* Number of objects currently in use */
111128 int *pIdx /* Write the index of a new slot here */
111129){
111130 char *z;
111131 sqlite3_int64 n = *pIdx = *pnEntry;
111132 if( (n & (n-1))==0 ){
111133 sqlite3_int64 sz = (n==0) ? 1 : 2*n;
111134 void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
111135 if( pNew==0 ){
111136 *pIdx = -1;
111137 return pArray;
111138 }
111139 pArray = pNew;
111140 }
111141 z = (char*)pArray;
111142 memset(&z[n * szEntry], 0, szEntry);
111143 ++*pnEntry;
111144 return pArray;
111145}
111146
111147/*
111148** Append a new element to the given IdList. Create a new IdList if
111149** need be.
111150**
111151** A new IdList is returned, or NULL if malloc() fails.
111152*/
111153SQLITE_PRIVATEstatic IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
111154 sqlite3 *db = pParse->db;
111155 int i;
111156 if( pList==0 ){
111157 pList = sqlite3DbMallocZero(db, sizeof(IdList) );
111158 if( pList==0 ) return 0;
111159 }
111160 pList->a = sqlite3ArrayAllocate(
111161 db,
111162 pList->a,
111163 sizeof(pList->a[0]),
111164 &pList->nId,
111165 &i
111166 );
111167 if( i<0 ){
111168 sqlite3IdListDelete(db, pList);
111169 return 0;
111170 }
111171 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
111172 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pList->a[i].zName ){
111173 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
111174 }
111175 return pList;
111176}
111177
111178/*
111179** Delete an IdList.
111180*/
111181SQLITE_PRIVATEstatic void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
111182 int i;
111183 if( pList==0 ) return;
111184 for(i=0; i<pList->nId; i++){
111185 sqlite3DbFree(db, pList->a[i].zName);
111186 }
111187 sqlite3DbFree(db, pList->a);
111188 sqlite3DbFreeNN(db, pList);
111189}
111190
111191/*
111192** Return the index in pList of the identifier named zId. Return -1
111193** if not found.
111194*/
111195SQLITE_PRIVATEstatic int sqlite3IdListIndex(IdList *pList, const char *zName){
111196 int i;
111197 if( pList==0 ) return -1;
111198 for(i=0; i<pList->nId; i++){
111199 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
111200 }
111201 return -1;
111202}
111203
111204/*
111205** Maximum size of a SrcList object.
111206** The SrcList object is used to represent the FROM clause of a
111207** SELECT statement, and the query planner cannot deal with more
111208** than 64 tables in a join. So any value larger than 64 here
111209** is sufficient for most uses. Smaller values, like say 10, are
111210** appropriate for small and memory-limited applications.
111211*/
111212#ifndef SQLITE_MAX_SRCLIST200
111213# define SQLITE_MAX_SRCLIST200 200
111214#endif
111215
111216/*
111217** Expand the space allocated for the given SrcList object by
111218** creating nExtra new slots beginning at iStart. iStart is zero based.
111219** New slots are zeroed.
111220**
111221** For example, suppose a SrcList initially contains two entries: A,B.
111222** To append 3 new entries onto the end, do this:
111223**
111224** sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
111225**
111226** After the call above it would contain: A, B, nil, nil, nil.
111227** If the iStart argument had been 1 instead of 2, then the result
111228** would have been: A, nil, nil, nil, B. To prepend the new slots,
111229** the iStart value would be 0. The result then would
111230** be: nil, nil, nil, A, B.
111231**
111232** If a memory allocation fails or the SrcList becomes too large, leave
111233** the original SrcList unchanged, return NULL, and leave an error message
111234** in pParse.
111235*/
111236SQLITE_PRIVATEstatic SrcList *sqlite3SrcListEnlarge(
111237 Parse *pParse, /* Parsing context into which errors are reported */
111238 SrcList *pSrc, /* The SrcList to be enlarged */
111239 int nExtra, /* Number of new slots to add to pSrc->a[] */
111240 int iStart /* Index in pSrc->a[] of first new slot */
111241){
111242 int i;
111243
111244 /* Sanity checking on calling parameters */
111245 assert( iStart>=0 )((void) (0));
111246 assert( nExtra>=1 )((void) (0));
111247 assert( pSrc!=0 )((void) (0));
111248 assert( iStart<=pSrc->nSrc )((void) (0));
111249
111250 /* Allocate additional space if needed */
111251 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
111252 SrcList *pNew;
111253 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
111254 sqlite3 *db = pParse->db;
111255
111256 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST200 ){
111257 sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
111258 SQLITE_MAX_SRCLIST200);
111259 return 0;
111260 }
111261 if( nAlloc>SQLITE_MAX_SRCLIST200 ) nAlloc = SQLITE_MAX_SRCLIST200;
111262 pNew = sqlite3DbRealloc(db, pSrc,
111263 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
111264 if( pNew==0 ){
111265 assert( db->mallocFailed )((void) (0));
111266 return 0;
111267 }
111268 pSrc = pNew;
111269 pSrc->nAlloc = nAlloc;
111270 }
111271
111272 /* Move existing slots that come after the newly inserted slots
111273 ** out of the way */
111274 for(i=pSrc->nSrc-1; i>=iStart; i--){
111275 pSrc->a[i+nExtra] = pSrc->a[i];
111276 }
111277 pSrc->nSrc += nExtra;
111278
111279 /* Zero the newly allocated slots */
111280 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
111281 for(i=iStart; i<iStart+nExtra; i++){
111282 pSrc->a[i].iCursor = -1;
111283 }
111284
111285 /* Return a pointer to the enlarged SrcList */
111286 return pSrc;
111287}
111288
111289
111290/*
111291** Append a new table name to the given SrcList. Create a new SrcList if
111292** need be. A new entry is created in the SrcList even if pTable is NULL.
111293**
111294** A SrcList is returned, or NULL if there is an OOM error or if the
111295** SrcList grows to large. The returned
111296** SrcList might be the same as the SrcList that was input or it might be
111297** a new one. If an OOM error does occurs, then the prior value of pList
111298** that is input to this routine is automatically freed.
111299**
111300** If pDatabase is not null, it means that the table has an optional
111301** database name prefix. Like this: "database.table". The pDatabase
111302** points to the table name and the pTable points to the database name.
111303** The SrcList.a[].zName field is filled with the table name which might
111304** come from pTable (if pDatabase is NULL) or from pDatabase.
111305** SrcList.a[].zDatabase is filled with the database name from pTable,
111306** or with NULL if no database is specified.
111307**
111308** In other words, if call like this:
111309**
111310** sqlite3SrcListAppend(D,A,B,0);
111311**
111312** Then B is a table name and the database name is unspecified. If called
111313** like this:
111314**
111315** sqlite3SrcListAppend(D,A,B,C);
111316**
111317** Then C is the table name and B is the database name. If C is defined
111318** then so is B. In other words, we never have a case where:
111319**
111320** sqlite3SrcListAppend(D,A,0,C);
111321**
111322** Both pTable and pDatabase are assumed to be quoted. They are dequoted
111323** before being added to the SrcList.
111324*/
111325SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppend(
111326 Parse *pParse, /* Parsing context, in which errors are reported */
111327 SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */
111328 Token *pTable, /* Table to append */
111329 Token *pDatabase /* Database of the table */
111330){
111331 struct SrcList_item *pItem;
111332 sqlite3 *db;
111333 assert( pDatabase==0 || pTable!=0 )((void) (0)); /* Cannot have C without B */
111334 assert( pParse!=0 )((void) (0));
111335 assert( pParse->db!=0 )((void) (0));
111336 db = pParse->db;
111337 if( pList==0 ){
111338 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
111339 if( pList==0 ) return 0;
111340 pList->nAlloc = 1;
111341 pList->nSrc = 1;
111342 memset(&pList->a[0], 0, sizeof(pList->a[0]));
111343 pList->a[0].iCursor = -1;
111344 }else{
111345 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
111346 if( pNew==0 ){
111347 sqlite3SrcListDelete(db, pList);
111348 return 0;
111349 }else{
111350 pList = pNew;
111351 }
111352 }
111353 pItem = &pList->a[pList->nSrc-1];
111354 if( pDatabase && pDatabase->z==0 ){
111355 pDatabase = 0;
111356 }
111357 if( pDatabase ){
111358 pItem->zName = sqlite3NameFromToken(db, pDatabase);
111359 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
111360 }else{
111361 pItem->zName = sqlite3NameFromToken(db, pTable);
111362 pItem->zDatabase = 0;
111363 }
111364 return pList;
111365}
111366
111367/*
111368** Assign VdbeCursor index numbers to all tables in a SrcList
111369*/
111370SQLITE_PRIVATEstatic void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
111371 int i;
111372 struct SrcList_item *pItem;
111373 assert(pList || pParse->db->mallocFailed )((void) (0));
111374 if( pList ){
111375 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
111376 if( pItem->iCursor>=0 ) break;
111377 pItem->iCursor = pParse->nTab++;
111378 if( pItem->pSelect ){
111379 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
111380 }
111381 }
111382 }
111383}
111384
111385/*
111386** Delete an entire SrcList including all its substructure.
111387*/
111388SQLITE_PRIVATEstatic void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
111389 int i;
111390 struct SrcList_item *pItem;
111391 if( pList==0 ) return;
111392 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
111393 sqlite3DbFree(db, pItem->zDatabase);
111394 sqlite3DbFree(db, pItem->zName);
111395 sqlite3DbFree(db, pItem->zAlias);
111396 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
111397 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
111398 sqlite3DeleteTable(db, pItem->pTab);
111399 sqlite3SelectDelete(db, pItem->pSelect);
111400 sqlite3ExprDelete(db, pItem->pOn);
111401 sqlite3IdListDelete(db, pItem->pUsing);
111402 }
111403 sqlite3DbFreeNN(db, pList);
111404}
111405
111406/*
111407** This routine is called by the parser to add a new term to the
111408** end of a growing FROM clause. The "p" parameter is the part of
111409** the FROM clause that has already been constructed. "p" is NULL
111410** if this is the first term of the FROM clause. pTable and pDatabase
111411** are the name of the table and database named in the FROM clause term.
111412** pDatabase is NULL if the database name qualifier is missing - the
111413** usual case. If the term has an alias, then pAlias points to the
111414** alias token. If the term is a subquery, then pSubquery is the
111415** SELECT statement that the subquery encodes. The pTable and
111416** pDatabase parameters are NULL for subqueries. The pOn and pUsing
111417** parameters are the content of the ON and USING clauses.
111418**
111419** Return a new SrcList which encodes is the FROM with the new
111420** term added.
111421*/
111422SQLITE_PRIVATEstatic SrcList *sqlite3SrcListAppendFromTerm(
111423 Parse *pParse, /* Parsing context */
111424 SrcList *p, /* The left part of the FROM clause already seen */
111425 Token *pTable, /* Name of the table to add to the FROM clause */
111426 Token *pDatabase, /* Name of the database containing pTable */
111427 Token *pAlias, /* The right-hand side of the AS subexpression */
111428 Select *pSubquery, /* A subquery used in place of a table name */
111429 Expr *pOn, /* The ON clause of a join */
111430 IdList *pUsing /* The USING clause of a join */
111431){
111432 struct SrcList_item *pItem;
111433 sqlite3 *db = pParse->db;
111434 if( !p && (pOn || pUsing) ){
111435 sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
111436 (pOn ? "ON" : "USING")
111437 );
111438 goto append_from_error;
111439 }
111440 p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
111441 if( p==0 ){
111442 goto append_from_error;
111443 }
111444 assert( p->nSrc>0 )((void) (0));
111445 pItem = &p->a[p->nSrc-1];
111446 assert( (pTable==0)==(pDatabase==0) )((void) (0));
111447 assert( pItem->zName==0 || pDatabase!=0 )((void) (0));
111448 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pItem->zName ){
111449 Token *pToken = (ALWAYS(pDatabase)(pDatabase) && pDatabase->z) ? pDatabase : pTable;
111450 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
111451 }
111452 assert( pAlias!=0 )((void) (0));
111453 if( pAlias->n ){
111454 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
111455 }
111456 pItem->pSelect = pSubquery;
111457 pItem->pOn = pOn;
111458 pItem->pUsing = pUsing;
111459 return p;
111460
111461 append_from_error:
111462 assert( p==0 )((void) (0));
111463 sqlite3ExprDelete(db, pOn);
111464 sqlite3IdListDelete(db, pUsing);
111465 sqlite3SelectDelete(db, pSubquery);
111466 return 0;
111467}
111468
111469/*
111470** Add an INDEXED BY or NOT INDEXED clause to the most recently added
111471** element of the source-list passed as the second argument.
111472*/
111473SQLITE_PRIVATEstatic void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
111474 assert( pIndexedBy!=0 )((void) (0));
111475 if( p && pIndexedBy->n>0 ){
111476 struct SrcList_item *pItem;
111477 assert( p->nSrc>0 )((void) (0));
111478 pItem = &p->a[p->nSrc-1];
111479 assert( pItem->fg.notIndexed==0 )((void) (0));
111480 assert( pItem->fg.isIndexedBy==0 )((void) (0));
111481 assert( pItem->fg.isTabFunc==0 )((void) (0));
111482 if( pIndexedBy->n==1 && !pIndexedBy->z ){
111483 /* A "NOT INDEXED" clause was supplied. See parse.y
111484 ** construct "indexed_opt" for details. */
111485 pItem->fg.notIndexed = 1;
111486 }else{
111487 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
111488 pItem->fg.isIndexedBy = 1;
111489 }
111490 }
111491}
111492
111493/*
111494** Add the list of function arguments to the SrcList entry for a
111495** table-valued-function.
111496*/
111497SQLITE_PRIVATEstatic void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
111498 if( p ){
111499 struct SrcList_item *pItem = &p->a[p->nSrc-1];
111500 assert( pItem->fg.notIndexed==0 )((void) (0));
111501 assert( pItem->fg.isIndexedBy==0 )((void) (0));
111502 assert( pItem->fg.isTabFunc==0 )((void) (0));
111503 pItem->u1.pFuncArg = pList;
111504 pItem->fg.isTabFunc = 1;
111505 }else{
111506 sqlite3ExprListDelete(pParse->db, pList);
111507 }
111508}
111509
111510/*
111511** When building up a FROM clause in the parser, the join operator
111512** is initially attached to the left operand. But the code generator
111513** expects the join operator to be on the right operand. This routine
111514** Shifts all join operators from left to right for an entire FROM
111515** clause.
111516**
111517** Example: Suppose the join is like this:
111518**
111519** A natural cross join B
111520**
111521** The operator is "natural cross join". The A and B operands are stored
111522** in p->a[0] and p->a[1], respectively. The parser initially stores the
111523** operator with A. This routine shifts that operator over to B.
111524*/
111525SQLITE_PRIVATEstatic void sqlite3SrcListShiftJoinType(SrcList *p){
111526 if( p ){
111527 int i;
111528 for(i=p->nSrc-1; i>0; i--){
111529 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
111530 }
111531 p->a[0].fg.jointype = 0;
111532 }
111533}
111534
111535/*
111536** Generate VDBE code for a BEGIN statement.
111537*/
111538SQLITE_PRIVATEstatic void sqlite3BeginTransaction(Parse *pParse, int type){
111539 sqlite3 *db;
111540 Vdbe *v;
111541 int i;
111542
111543 assert( pParse!=0 )((void) (0));
111544 db = pParse->db;
111545 assert( db!=0 )((void) (0));
111546 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION22, "BEGIN", 0, 0) ){
111547 return;
111548 }
111549 v = sqlite3GetVdbe(pParse);
111550 if( !v ) return;
111551 if( type!=TK_DEFERRED7 ){
111552 for(i=0; i<db->nDb; i++){
111553 sqlite3VdbeAddOp2(v, OP_Transaction2, i, (type==TK_EXCLUSIVE9)+1);
111554 sqlite3VdbeUsesBtree(v, i);
111555 }
111556 }
111557 sqlite3VdbeAddOp0(v, OP_AutoCommit1);
111558}
111559
111560/*
111561** Generate VDBE code for a COMMIT or ROLLBACK statement.
111562** Code for ROLLBACK is generated if eType==TK_ROLLBACK. Otherwise
111563** code is generated for a COMMIT.
111564*/
111565SQLITE_PRIVATEstatic void sqlite3EndTransaction(Parse *pParse, int eType){
111566 Vdbe *v;
111567 int isRollback;
111568
111569 assert( pParse!=0 )((void) (0));
111570 assert( pParse->db!=0 )((void) (0));
111571 assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK )((void) (0));
111572 isRollback = eType==TK_ROLLBACK12;
111573 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION22,
111574 isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
111575 return;
111576 }
111577 v = sqlite3GetVdbe(pParse);
111578 if( v ){
111579 sqlite3VdbeAddOp2(v, OP_AutoCommit1, 1, isRollback);
111580 }
111581}
111582
111583/*
111584** This function is called by the parser when it parses a command to create,
111585** release or rollback an SQL savepoint.
111586*/
111587SQLITE_PRIVATEstatic void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
111588 char *zName = sqlite3NameFromToken(pParse->db, pName);
111589 if( zName ){
111590 Vdbe *v = sqlite3GetVdbe(pParse);
111591#ifndef SQLITE_OMIT_AUTHORIZATION
111592 static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
111593 assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 )((void) (0));
111594#endif
111595 if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT32, az[op], zName, 0) ){
111596 sqlite3DbFree(pParse->db, zName);
111597 return;
111598 }
111599 sqlite3VdbeAddOp4(v, OP_Savepoint0, op, 0, 0, zName, P4_DYNAMIC(-7));
111600 }
111601}
111602
111603/*
111604** Make sure the TEMP database is open and available for use. Return
111605** the number of errors. Leave any error messages in the pParse structure.
111606*/
111607SQLITE_PRIVATEstatic int sqlite3OpenTempDatabase(Parse *pParse){
111608 sqlite3 *db = pParse->db;
111609 if( db->aDb[1].pBt==0 && !pParse->explain ){
111610 int rc;
111611 Btree *pBt;
111612 static const int flags =
111613 SQLITE_OPEN_READWRITE0x00000002 |
111614 SQLITE_OPEN_CREATE0x00000004 |
111615 SQLITE_OPEN_EXCLUSIVE0x00000010 |
111616 SQLITE_OPEN_DELETEONCLOSE0x00000008 |
111617 SQLITE_OPEN_TEMP_DB0x00000200;
111618
111619 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
111620 if( rc!=SQLITE_OK0 ){
111621 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
111622 "file for storing temporary tables");
111623 pParse->rc = rc;
111624 return 1;
111625 }
111626 db->aDb[1].pBt = pBt;
111627 assert( db->aDb[1].pSchema )((void) (0));
111628 if( SQLITE_NOMEM7==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
111629 sqlite3OomFault(db);
111630 return 1;
111631 }
111632 }
111633 return 0;
111634}
111635
111636/*
111637** Record the fact that the schema cookie will need to be verified
111638** for database iDb. The code to actually verify the schema cookie
111639** will occur at the end of the top-level VDBE and will be generated
111640** later, by sqlite3FinishCoding().
111641*/
111642SQLITE_PRIVATEstatic void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
111643 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
111644
111645 assert( iDb>=0 && iDb<pParse->db->nDb )((void) (0));
111646 assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 )((void) (0));
111647 assert( iDb<SQLITE_MAX_ATTACHED+2 )((void) (0));
111648 assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) )((void) (0));
111649 if( DbMaskTest(pToplevel->cookieMask, iDb)(((pToplevel->cookieMask)&(((yDbMask)1)<<(iDb)))
!=0)
==0 ){
111650 DbMaskSet(pToplevel->cookieMask, iDb)(pToplevel->cookieMask)|=(((yDbMask)1)<<(iDb));
111651 if( !OMIT_TEMPDB0 && iDb==1 ){
111652 sqlite3OpenTempDatabase(pToplevel);
111653 }
111654 }
111655}
111656
111657/*
111658** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
111659** attached database. Otherwise, invoke it for the database named zDb only.
111660*/
111661SQLITE_PRIVATEstatic void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
111662 sqlite3 *db = pParse->db;
111663 int i;
111664 for(i=0; i<db->nDb; i++){
111665 Db *pDb = &db->aDb[i];
111666 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
111667 sqlite3CodeVerifySchema(pParse, i);
111668 }
111669 }
111670}
111671
111672/*
111673** Generate VDBE code that prepares for doing an operation that
111674** might change the database.
111675**
111676** This routine starts a new transaction if we are not already within
111677** a transaction. If we are already within a transaction, then a checkpoint
111678** is set if the setStatement parameter is true. A checkpoint should
111679** be set for operations that might fail (due to a constraint) part of
111680** the way through and which will need to undo some writes without having to
111681** rollback the whole transaction. For operations where all constraints
111682** can be checked before any changes are made to the database, it is never
111683** necessary to undo a write and the checkpoint should not be set.
111684*/
111685SQLITE_PRIVATEstatic void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
111686 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
111687 sqlite3CodeVerifySchema(pParse, iDb);
111688 DbMaskSet(pToplevel->writeMask, iDb)(pToplevel->writeMask)|=(((yDbMask)1)<<(iDb));
111689 pToplevel->isMultiWrite |= setStatement;
111690}
111691
111692/*
111693** Indicate that the statement currently under construction might write
111694** more than one entry (example: deleting one row then inserting another,
111695** inserting multiple rows in a table, or inserting a row and index entries.)
111696** If an abort occurs after some of these writes have completed, then it will
111697** be necessary to undo the completed writes.
111698*/
111699SQLITE_PRIVATEstatic void sqlite3MultiWrite(Parse *pParse){
111700 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
111701 pToplevel->isMultiWrite = 1;
111702}
111703
111704/*
111705** The code generator calls this routine if is discovers that it is
111706** possible to abort a statement prior to completion. In order to
111707** perform this abort without corrupting the database, we need to make
111708** sure that the statement is protected by a statement transaction.
111709**
111710** Technically, we only need to set the mayAbort flag if the
111711** isMultiWrite flag was previously set. There is a time dependency
111712** such that the abort must occur after the multiwrite. This makes
111713** some statements involving the REPLACE conflict resolution algorithm
111714** go a little faster. But taking advantage of this time dependency
111715** makes it more difficult to prove that the code is correct (in
111716** particular, it prevents us from writing an effective
111717** implementation of sqlite3AssertMayAbort()) and so we have chosen
111718** to take the safe route and skip the optimization.
111719*/
111720SQLITE_PRIVATEstatic void sqlite3MayAbort(Parse *pParse){
111721 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
111722 pToplevel->mayAbort = 1;
111723}
111724
111725/*
111726** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
111727** error. The onError parameter determines which (if any) of the statement
111728** and/or current transaction is rolled back.
111729*/
111730SQLITE_PRIVATEstatic void sqlite3HaltConstraint(
111731 Parse *pParse, /* Parsing context */
111732 int errCode, /* extended error code */
111733 int onError, /* Constraint type */
111734 char *p4, /* Error message */
111735 i8 p4type, /* P4_STATIC or P4_TRANSIENT */
111736 u8 p5Errmsg /* P5_ErrMsg type */
111737){
111738 Vdbe *v = sqlite3GetVdbe(pParse);
111739 assert( (errCode&0xff)==SQLITE_CONSTRAINT )((void) (0));
111740 if( onError==OE_Abort2 ){
111741 sqlite3MayAbort(pParse);
111742 }
111743 sqlite3VdbeAddOp4(v, OP_Halt69, errCode, onError, 0, p4, p4type);
111744 sqlite3VdbeChangeP5(v, p5Errmsg);
111745}
111746
111747/*
111748** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
111749*/
111750SQLITE_PRIVATEstatic void sqlite3UniqueConstraint(
111751 Parse *pParse, /* Parsing context */
111752 int onError, /* Constraint type */
111753 Index *pIdx /* The index that triggers the constraint */
111754){
111755 char *zErr;
111756 int j;
111757 StrAccum errMsg;
111758 Table *pTab = pIdx->pTable;
111759
111760 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
111761 pParse->db->aLimit[SQLITE_LIMIT_LENGTH0]);
111762 if( pIdx->aColExpr ){
111763 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
111764 }else{
111765 for(j=0; j<pIdx->nKeyCol; j++){
111766 char *zCol;
111767 assert( pIdx->aiColumn[j]>=0 )((void) (0));
111768 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
111769 if( j ) sqlite3_str_append(&errMsg, ", ", 2);
111770 sqlite3_str_appendall(&errMsg, pTab->zName);
111771 sqlite3_str_append(&errMsg, ".", 1);
111772 sqlite3_str_appendall(&errMsg, zCol);
111773 }
111774 }
111775 zErr = sqlite3StrAccumFinish(&errMsg);
111776 sqlite3HaltConstraint(pParse,
111777 IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ? SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8))
111778 : SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)),
111779 onError, zErr, P4_DYNAMIC(-7), P5_ConstraintUnique2);
111780}
111781
111782
111783/*
111784** Code an OP_Halt due to non-unique rowid.
111785*/
111786SQLITE_PRIVATEstatic void sqlite3RowidConstraint(
111787 Parse *pParse, /* Parsing context */
111788 int onError, /* Conflict resolution algorithm */
111789 Table *pTab /* The table with the non-unique rowid */
111790){
111791 char *zMsg;
111792 int rc;
111793 if( pTab->iPKey>=0 ){
111794 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
111795 pTab->aCol[pTab->iPKey].zName);
111796 rc = SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8));
111797 }else{
111798 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
111799 rc = SQLITE_CONSTRAINT_ROWID(19 |(10<<8));
111800 }
111801 sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC(-7),
111802 P5_ConstraintUnique2);
111803}
111804
111805/*
111806** Check to see if pIndex uses the collating sequence pColl. Return
111807** true if it does and false if it does not.
111808*/
111809#ifndef SQLITE_OMIT_REINDEX
111810static int collationMatch(const char *zColl, Index *pIndex){
111811 int i;
111812 assert( zColl!=0 )((void) (0));
111813 for(i=0; i<pIndex->nColumn; i++){
111814 const char *z = pIndex->azColl[i];
111815 assert( z!=0 || pIndex->aiColumn[i]<0 )((void) (0));
111816 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
111817 return 1;
111818 }
111819 }
111820 return 0;
111821}
111822#endif
111823
111824/*
111825** Recompute all indices of pTab that use the collating sequence pColl.
111826** If pColl==0 then recompute all indices of pTab.
111827*/
111828#ifndef SQLITE_OMIT_REINDEX
111829static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
111830 if( !IsVirtual(pTab)((pTab)->nModuleArg) ){
111831 Index *pIndex; /* An index associated with pTab */
111832
111833 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
111834 if( zColl==0 || collationMatch(zColl, pIndex) ){
111835 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
111836 sqlite3BeginWriteOperation(pParse, 0, iDb);
111837 sqlite3RefillIndex(pParse, pIndex, -1);
111838 }
111839 }
111840 }
111841}
111842#endif
111843
111844/*
111845** Recompute all indices of all tables in all databases where the
111846** indices use the collating sequence pColl. If pColl==0 then recompute
111847** all indices everywhere.
111848*/
111849#ifndef SQLITE_OMIT_REINDEX
111850static void reindexDatabases(Parse *pParse, char const *zColl){
111851 Db *pDb; /* A single database */
111852 int iDb; /* The database index number */
111853 sqlite3 *db = pParse->db; /* The database connection */
111854 HashElem *k; /* For looping over tables in pDb */
111855 Table *pTab; /* A table in the database */
111856
111857 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0)); /* Needed for schema access */
111858 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
111859 assert( pDb!=0 )((void) (0));
111860 for(k=sqliteHashFirst(&pDb->pSchema->tblHash)((&pDb->pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
111861 pTab = (Table*)sqliteHashData(k)((k)->data);
111862 reindexTable(pParse, pTab, zColl);
111863 }
111864 }
111865}
111866#endif
111867
111868/*
111869** Generate code for the REINDEX command.
111870**
111871** REINDEX -- 1
111872** REINDEX <collation> -- 2
111873** REINDEX ?<database>.?<tablename> -- 3
111874** REINDEX ?<database>.?<indexname> -- 4
111875**
111876** Form 1 causes all indices in all attached databases to be rebuilt.
111877** Form 2 rebuilds all indices in all databases that use the named
111878** collating function. Forms 3 and 4 rebuild the named index or all
111879** indices associated with the named table.
111880*/
111881#ifndef SQLITE_OMIT_REINDEX
111882SQLITE_PRIVATEstatic void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
111883 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
111884 char *z; /* Name of a table or index */
111885 const char *zDb; /* Name of the database */
111886 Table *pTab; /* A table in the database */
111887 Index *pIndex; /* An index associated with pTab */
111888 int iDb; /* The database index number */
111889 sqlite3 *db = pParse->db; /* The database connection */
111890 Token *pObjName; /* Name of the table or index to be reindexed */
111891
111892 /* Read the database schema. If an error occurs, leave an error message
111893 ** and code in pParse and return NULL. */
111894 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
111895 return;
111896 }
111897
111898 if( pName1==0 ){
111899 reindexDatabases(pParse, 0);
111900 return;
111901 }else if( NEVER(pName2==0)(pName2==0) || pName2->z==0 ){
111902 char *zColl;
111903 assert( pName1->z )((void) (0));
111904 zColl = sqlite3NameFromToken(pParse->db, pName1);
111905 if( !zColl ) return;
111906 pColl = sqlite3FindCollSeq(db, ENC(db)((db)->enc), zColl, 0);
111907 if( pColl ){
111908 reindexDatabases(pParse, zColl);
111909 sqlite3DbFree(db, zColl);
111910 return;
111911 }
111912 sqlite3DbFree(db, zColl);
111913 }
111914 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
111915 if( iDb<0 ) return;
111916 z = sqlite3NameFromToken(db, pObjName);
111917 if( z==0 ) return;
111918 zDb = db->aDb[iDb].zDbSName;
111919 pTab = sqlite3FindTable(db, z, zDb);
111920 if( pTab ){
111921 reindexTable(pParse, pTab, 0);
111922 sqlite3DbFree(db, z);
111923 return;
111924 }
111925 pIndex = sqlite3FindIndex(db, z, zDb);
111926 sqlite3DbFree(db, z);
111927 if( pIndex ){
111928 sqlite3BeginWriteOperation(pParse, 0, iDb);
111929 sqlite3RefillIndex(pParse, pIndex, -1);
111930 return;
111931 }
111932 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
111933}
111934#endif
111935
111936/*
111937** Return a KeyInfo structure that is appropriate for the given Index.
111938**
111939** The caller should invoke sqlite3KeyInfoUnref() on the returned object
111940** when it has finished using it.
111941*/
111942SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
111943 int i;
111944 int nCol = pIdx->nColumn;
111945 int nKey = pIdx->nKeyCol;
111946 KeyInfo *pKey;
111947 if( pParse->nErr ) return 0;
111948 if( pIdx->uniqNotNull ){
111949 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
111950 }else{
111951 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
111952 }
111953 if( pKey ){
111954 assert( sqlite3KeyInfoIsWriteable(pKey) )((void) (0));
111955 for(i=0; i<nCol; i++){
111956 const char *zColl = pIdx->azColl[i];
111957 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
111958 sqlite3LocateCollSeq(pParse, zColl);
111959 pKey->aSortOrder[i] = pIdx->aSortOrder[i];
111960 }
111961 if( pParse->nErr ){
111962 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ )((void) (0));
111963 if( pIdx->bNoQuery==0 ){
111964 /* Deactivate the index because it contains an unknown collating
111965 ** sequence. The only way to reactive the index is to reload the
111966 ** schema. Adding the missing collating sequence later does not
111967 ** reactive the index. The application had the chance to register
111968 ** the missing index using the collation-needed callback. For
111969 ** simplicity, SQLite will not give the application a second chance.
111970 */
111971 pIdx->bNoQuery = 1;
111972 pParse->rc = SQLITE_ERROR_RETRY(1 | (2<<8));
111973 }
111974 sqlite3KeyInfoUnref(pKey);
111975 pKey = 0;
111976 }
111977 }
111978 return pKey;
111979}
111980
111981#ifndef SQLITE_OMIT_CTE
111982/*
111983** This routine is invoked once per CTE by the parser while parsing a
111984** WITH clause.
111985*/
111986SQLITE_PRIVATEstatic With *sqlite3WithAdd(
111987 Parse *pParse, /* Parsing context */
111988 With *pWith, /* Existing WITH clause, or NULL */
111989 Token *pName, /* Name of the common-table */
111990 ExprList *pArglist, /* Optional column name list for the table */
111991 Select *pQuery /* Query used to initialize the table */
111992){
111993 sqlite3 *db = pParse->db;
111994 With *pNew;
111995 char *zName;
111996
111997 /* Check that the CTE name is unique within this WITH clause. If
111998 ** not, store an error in the Parse structure. */
111999 zName = sqlite3NameFromToken(pParse->db, pName);
112000 if( zName && pWith ){
112001 int i;
112002 for(i=0; i<pWith->nCte; i++){
112003 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
112004 sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
112005 }
112006 }
112007 }
112008
112009 if( pWith ){
112010 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
112011 pNew = sqlite3DbRealloc(db, pWith, nByte);
112012 }else{
112013 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
112014 }
112015 assert( (pNew!=0 && zName!=0) || db->mallocFailed )((void) (0));
112016
112017 if( db->mallocFailed ){
112018 sqlite3ExprListDelete(db, pArglist);
112019 sqlite3SelectDelete(db, pQuery);
112020 sqlite3DbFree(db, zName);
112021 pNew = pWith;
112022 }else{
112023 pNew->a[pNew->nCte].pSelect = pQuery;
112024 pNew->a[pNew->nCte].pCols = pArglist;
112025 pNew->a[pNew->nCte].zName = zName;
112026 pNew->a[pNew->nCte].zCteErr = 0;
112027 pNew->nCte++;
112028 }
112029
112030 return pNew;
112031}
112032
112033/*
112034** Free the contents of the With object passed as the second argument.
112035*/
112036SQLITE_PRIVATEstatic void sqlite3WithDelete(sqlite3 *db, With *pWith){
112037 if( pWith ){
112038 int i;
112039 for(i=0; i<pWith->nCte; i++){
112040 struct Cte *pCte = &pWith->a[i];
112041 sqlite3ExprListDelete(db, pCte->pCols);
112042 sqlite3SelectDelete(db, pCte->pSelect);
112043 sqlite3DbFree(db, pCte->zName);
112044 }
112045 sqlite3DbFree(db, pWith);
112046 }
112047}
112048#endif /* !defined(SQLITE_OMIT_CTE) */
112049
112050/************** End of build.c ***********************************************/
112051/************** Begin file callback.c ****************************************/
112052/*
112053** 2005 May 23
112054**
112055** The author disclaims copyright to this source code. In place of
112056** a legal notice, here is a blessing:
112057**
112058** May you do good and not evil.
112059** May you find forgiveness for yourself and forgive others.
112060** May you share freely, never taking more than you give.
112061**
112062*************************************************************************
112063**
112064** This file contains functions used to access the internal hash tables
112065** of user defined functions and collation sequences.
112066*/
112067
112068/* #include "sqliteInt.h" */
112069
112070/*
112071** Invoke the 'collation needed' callback to request a collation sequence
112072** in the encoding enc of name zName, length nName.
112073*/
112074static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
112075 assert( !db->xCollNeeded || !db->xCollNeeded16 )((void) (0));
112076 if( db->xCollNeeded ){
112077 char *zExternal = sqlite3DbStrDup(db, zName);
112078 if( !zExternal ) return;
112079 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
112080 sqlite3DbFree(db, zExternal);
112081 }
112082#ifndef SQLITE_OMIT_UTF16
112083 if( db->xCollNeeded16 ){
112084 char const *zExternal;
112085 sqlite3_value *pTmp = sqlite3ValueNew(db);
112086 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF81, SQLITE_STATIC((sqlite3_destructor_type)0));
112087 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE2);
112088 if( zExternal ){
112089 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db)((db)->enc), zExternal);
112090 }
112091 sqlite3ValueFree(pTmp);
112092 }
112093#endif
112094}
112095
112096/*
112097** This routine is called if the collation factory fails to deliver a
112098** collation function in the best encoding but there may be other versions
112099** of this collation function (for other text encodings) available. Use one
112100** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
112101** possible.
112102*/
112103static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
112104 CollSeq *pColl2;
112105 char *z = pColl->zName;
112106 int i;
112107 static const u8 aEnc[] = { SQLITE_UTF16BE3, SQLITE_UTF16LE2, SQLITE_UTF81 };
112108 for(i=0; i<3; i++){
112109 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
112110 if( pColl2->xCmp!=0 ){
112111 memcpy(pColl, pColl2, sizeof(CollSeq));
112112 pColl->xDel = 0; /* Do not copy the destructor */
112113 return SQLITE_OK0;
112114 }
112115 }
112116 return SQLITE_ERROR1;
112117}
112118
112119/*
112120** This function is responsible for invoking the collation factory callback
112121** or substituting a collation sequence of a different encoding when the
112122** requested collation sequence is not available in the desired encoding.
112123**
112124** If it is not NULL, then pColl must point to the database native encoding
112125** collation sequence with name zName, length nName.
112126**
112127** The return value is either the collation sequence to be used in database
112128** db for collation type name zName, length nName, or NULL, if no collation
112129** sequence can be found. If no collation is found, leave an error message.
112130**
112131** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
112132*/
112133SQLITE_PRIVATEstatic CollSeq *sqlite3GetCollSeq(
112134 Parse *pParse, /* Parsing context */
112135 u8 enc, /* The desired encoding for the collating sequence */
112136 CollSeq *pColl, /* Collating sequence with native encoding, or NULL */
112137 const char *zName /* Collating sequence name */
112138){
112139 CollSeq *p;
112140 sqlite3 *db = pParse->db;
112141
112142 p = pColl;
112143 if( !p ){
112144 p = sqlite3FindCollSeq(db, enc, zName, 0);
112145 }
112146 if( !p || !p->xCmp ){
112147 /* No collation sequence of this type for this encoding is registered.
112148 ** Call the collation factory to see if it can supply us with one.
112149 */
112150 callCollNeeded(db, enc, zName);
112151 p = sqlite3FindCollSeq(db, enc, zName, 0);
112152 }
112153 if( p && !p->xCmp && synthCollSeq(db, p) ){
112154 p = 0;
112155 }
112156 assert( !p || p->xCmp )((void) (0));
112157 if( p==0 ){
112158 sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
112159 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ(1 | (1<<8));
112160 }
112161 return p;
112162}
112163
112164/*
112165** This routine is called on a collation sequence before it is used to
112166** check that it is defined. An undefined collation sequence exists when
112167** a database is loaded that contains references to collation sequences
112168** that have not been defined by sqlite3_create_collation() etc.
112169**
112170** If required, this routine calls the 'collation needed' callback to
112171** request a definition of the collating sequence. If this doesn't work,
112172** an equivalent collating sequence that uses a text encoding different
112173** from the main database is substituted, if one is available.
112174*/
112175SQLITE_PRIVATEstatic int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
112176 if( pColl && pColl->xCmp==0 ){
112177 const char *zName = pColl->zName;
112178 sqlite3 *db = pParse->db;
112179 CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db)((db)->enc), pColl, zName);
112180 if( !p ){
112181 return SQLITE_ERROR1;
112182 }
112183 assert( p==pColl )((void) (0));
112184 }
112185 return SQLITE_OK0;
112186}
112187
112188
112189
112190/*
112191** Locate and return an entry from the db.aCollSeq hash table. If the entry
112192** specified by zName and nName is not found and parameter 'create' is
112193** true, then create a new entry. Otherwise return NULL.
112194**
112195** Each pointer stored in the sqlite3.aCollSeq hash table contains an
112196** array of three CollSeq structures. The first is the collation sequence
112197** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
112198**
112199** Stored immediately after the three collation sequences is a copy of
112200** the collation sequence name. A pointer to this string is stored in
112201** each collation sequence structure.
112202*/
112203static CollSeq *findCollSeqEntry(
112204 sqlite3 *db, /* Database connection */
112205 const char *zName, /* Name of the collating sequence */
112206 int create /* Create a new entry if true */
112207){
112208 CollSeq *pColl;
112209 pColl = sqlite3HashFind(&db->aCollSeq, zName);
112210
112211 if( 0==pColl && create ){
112212 int nName = sqlite3Strlen30(zName) + 1;
112213 pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName);
112214 if( pColl ){
112215 CollSeq *pDel = 0;
112216 pColl[0].zName = (char*)&pColl[3];
112217 pColl[0].enc = SQLITE_UTF81;
112218 pColl[1].zName = (char*)&pColl[3];
112219 pColl[1].enc = SQLITE_UTF16LE2;
112220 pColl[2].zName = (char*)&pColl[3];
112221 pColl[2].enc = SQLITE_UTF16BE3;
112222 memcpy(pColl[0].zName, zName, nName);
112223 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
112224
112225 /* If a malloc() failure occurred in sqlite3HashInsert(), it will
112226 ** return the pColl pointer to be deleted (because it wasn't added
112227 ** to the hash table).
112228 */
112229 assert( pDel==0 || pDel==pColl )((void) (0));
112230 if( pDel!=0 ){
112231 sqlite3OomFault(db);
112232 sqlite3DbFree(db, pDel);
112233 pColl = 0;
112234 }
112235 }
112236 }
112237 return pColl;
112238}
112239
112240/*
112241** Parameter zName points to a UTF-8 encoded string nName bytes long.
112242** Return the CollSeq* pointer for the collation sequence named zName
112243** for the encoding 'enc' from the database 'db'.
112244**
112245** If the entry specified is not found and 'create' is true, then create a
112246** new entry. Otherwise return NULL.
112247**
112248** A separate function sqlite3LocateCollSeq() is a wrapper around
112249** this routine. sqlite3LocateCollSeq() invokes the collation factory
112250** if necessary and generates an error message if the collating sequence
112251** cannot be found.
112252**
112253** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
112254*/
112255SQLITE_PRIVATEstatic CollSeq *sqlite3FindCollSeq(
112256 sqlite3 *db,
112257 u8 enc,
112258 const char *zName,
112259 int create
112260){
112261 CollSeq *pColl;
112262 if( zName ){
112263 pColl = findCollSeqEntry(db, zName, create);
112264 }else{
112265 pColl = db->pDfltColl;
112266 }
112267 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 )((void) (0));
112268 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE )((void) (0));
112269 if( pColl ) pColl += enc-1;
112270 return pColl;
112271}
112272
112273/* During the search for the best function definition, this procedure
112274** is called to test how well the function passed as the first argument
112275** matches the request for a function with nArg arguments in a system
112276** that uses encoding enc. The value returned indicates how well the
112277** request is matched. A higher value indicates a better match.
112278**
112279** If nArg is -1 that means to only return a match (non-zero) if p->nArg
112280** is also -1. In other words, we are searching for a function that
112281** takes a variable number of arguments.
112282**
112283** If nArg is -2 that means that we are searching for any function
112284** regardless of the number of arguments it uses, so return a positive
112285** match score for any
112286**
112287** The returned value is always between 0 and 6, as follows:
112288**
112289** 0: Not a match.
112290** 1: UTF8/16 conversion required and function takes any number of arguments.
112291** 2: UTF16 byte order change required and function takes any number of args.
112292** 3: encoding matches and function takes any number of arguments
112293** 4: UTF8/16 conversion required - argument count matches exactly
112294** 5: UTF16 byte order conversion required - argument count matches exactly
112295** 6: Perfect match: encoding and argument count match exactly.
112296**
112297** If nArg==(-2) then any function with a non-null xSFunc is
112298** a perfect match and any function with xSFunc NULL is
112299** a non-match.
112300*/
112301#define FUNC_PERFECT_MATCH6 6 /* The score for a perfect match */
112302static int matchQuality(
112303 FuncDef *p, /* The function we are evaluating for match quality */
112304 int nArg, /* Desired number of arguments. (-1)==any */
112305 u8 enc /* Desired text encoding */
112306){
112307 int match;
112308
112309 /* nArg of -2 is a special case */
112310 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH6;
112311
112312 /* Wrong number of arguments means "no match" */
112313 if( p->nArg!=nArg && p->nArg>=0 ) return 0;
112314
112315 /* Give a better score to a function with a specific number of arguments
112316 ** than to function that accepts any number of arguments. */
112317 if( p->nArg==nArg ){
112318 match = 4;
112319 }else{
112320 match = 1;
112321 }
112322
112323 /* Bonus points if the text encoding matches */
112324 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK0x0003) ){
112325 match += 2; /* Exact encoding match */
112326 }else if( (enc & p->funcFlags & 2)!=0 ){
112327 match += 1; /* Both are UTF16, but with different byte orders */
112328 }
112329
112330 return match;
112331}
112332
112333/*
112334** Search a FuncDefHash for a function with the given name. Return
112335** a pointer to the matching FuncDef if found, or 0 if there is no match.
112336*/
112337SQLITE_PRIVATEstatic FuncDef *sqlite3FunctionSearch(
112338 int h, /* Hash of the name */
112339 const char *zFunc /* Name of function */
112340){
112341 FuncDef *p;
112342 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
112343 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
112344 return p;
112345 }
112346 }
112347 return 0;
112348}
112349
112350/*
112351** Insert a new FuncDef into a FuncDefHash hash table.
112352*/
112353SQLITE_PRIVATEstatic void sqlite3InsertBuiltinFuncs(
112354 FuncDef *aDef, /* List of global functions to be inserted */
112355 int nDef /* Length of the apDef[] list */
112356){
112357 int i;
112358 for(i=0; i<nDef; i++){
112359 FuncDef *pOther;
112360 const char *zName = aDef[i].zName;
112361 int nName = sqlite3Strlen30(zName);
112362 int h = SQLITE_FUNC_HASH(zName[0], nName)(((zName[0])+(nName))%23);
112363 assert( zName[0]>='a' && zName[0]<='z' )((void) (0));
112364 pOther = sqlite3FunctionSearch(h, zName);
112365 if( pOther ){
112366 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] )((void) (0));
112367 aDef[i].pNext = pOther->pNext;
112368 pOther->pNext = &aDef[i];
112369 }else{
112370 aDef[i].pNext = 0;
112371 aDef[i].u.pHash = sqlite3BuiltinFunctions.a[h];
112372 sqlite3BuiltinFunctions.a[h] = &aDef[i];
112373 }
112374 }
112375}
112376
112377
112378
112379/*
112380** Locate a user function given a name, a number of arguments and a flag
112381** indicating whether the function prefers UTF-16 over UTF-8. Return a
112382** pointer to the FuncDef structure that defines that function, or return
112383** NULL if the function does not exist.
112384**
112385** If the createFlag argument is true, then a new (blank) FuncDef
112386** structure is created and liked into the "db" structure if a
112387** no matching function previously existed.
112388**
112389** If nArg is -2, then the first valid function found is returned. A
112390** function is valid if xSFunc is non-zero. The nArg==(-2)
112391** case is used to see if zName is a valid function name for some number
112392** of arguments. If nArg is -2, then createFlag must be 0.
112393**
112394** If createFlag is false, then a function with the required name and
112395** number of arguments may be returned even if the eTextRep flag does not
112396** match that requested.
112397*/
112398SQLITE_PRIVATEstatic FuncDef *sqlite3FindFunction(
112399 sqlite3 *db, /* An open database */
112400 const char *zName, /* Name of the function. zero-terminated */
112401 int nArg, /* Number of arguments. -1 means any number */
112402 u8 enc, /* Preferred text encoding */
112403 u8 createFlag /* Create new entry if true and does not otherwise exist */
112404){
112405 FuncDef *p; /* Iterator variable */
112406 FuncDef *pBest = 0; /* Best match found so far */
112407 int bestScore = 0; /* Score of best match */
112408 int h; /* Hash value */
112409 int nName; /* Length of the name */
112410
112411 assert( nArg>=(-2) )((void) (0));
112412 assert( nArg>=(-1) || createFlag==0 )((void) (0));
112413 nName = sqlite3Strlen30(zName);
112414
112415 /* First search for a match amongst the application-defined functions.
112416 */
112417 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
112418 while( p ){
112419 int score = matchQuality(p, nArg, enc);
112420 if( score>bestScore ){
112421 pBest = p;
112422 bestScore = score;
112423 }
112424 p = p->pNext;
112425 }
112426
112427 /* If no match is found, search the built-in functions.
112428 **
112429 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
112430 ** functions even if a prior app-defined function was found. And give
112431 ** priority to built-in functions.
112432 **
112433 ** Except, if createFlag is true, that means that we are trying to
112434 ** install a new function. Whatever FuncDef structure is returned it will
112435 ** have fields overwritten with new information appropriate for the
112436 ** new function. But the FuncDefs for built-in functions are read-only.
112437 ** So we must not search for built-ins when creating a new function.
112438 */
112439 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin0x0002)!=0) ){
112440 bestScore = 0;
112441 h = SQLITE_FUNC_HASH(sqlite3UpperToLower[(u8)zName[0]], nName)(((sqlite3UpperToLower[(u8)zName[0]])+(nName))%23);
112442 p = sqlite3FunctionSearch(h, zName);
112443 while( p ){
112444 int score = matchQuality(p, nArg, enc);
112445 if( score>bestScore ){
112446 pBest = p;
112447 bestScore = score;
112448 }
112449 p = p->pNext;
112450 }
112451 }
112452
112453 /* If the createFlag parameter is true and the search did not reveal an
112454 ** exact match for the name, number of arguments and encoding, then add a
112455 ** new entry to the hash table and return it.
112456 */
112457 if( createFlag && bestScore<FUNC_PERFECT_MATCH6 &&
112458 (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
112459 FuncDef *pOther;
112460 u8 *z;
112461 pBest->zName = (const char*)&pBest[1];
112462 pBest->nArg = (u16)nArg;
112463 pBest->funcFlags = enc;
112464 memcpy((char*)&pBest[1], zName, nName+1);
112465 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
112466 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
112467 if( pOther==pBest ){
112468 sqlite3DbFree(db, pBest);
112469 sqlite3OomFault(db);
112470 return 0;
112471 }else{
112472 pBest->pNext = pOther;
112473 }
112474 }
112475
112476 if( pBest && (pBest->xSFunc || createFlag) ){
112477 return pBest;
112478 }
112479 return 0;
112480}
112481
112482/*
112483** Free all resources held by the schema structure. The void* argument points
112484** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
112485** pointer itself, it just cleans up subsidiary resources (i.e. the contents
112486** of the schema hash tables).
112487**
112488** The Schema.cache_size variable is not cleared.
112489*/
112490SQLITE_PRIVATEstatic void sqlite3SchemaClear(void *p){
112491 Hash temp1;
112492 Hash temp2;
112493 HashElem *pElem;
112494 Schema *pSchema = (Schema *)p;
112495
112496 temp1 = pSchema->tblHash;
112497 temp2 = pSchema->trigHash;
112498 sqlite3HashInit(&pSchema->trigHash);
112499 sqlite3HashClear(&pSchema->idxHash);
112500 for(pElem=sqliteHashFirst(&temp2)((&temp2)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
112501 sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem)((pElem)->data));
112502 }
112503 sqlite3HashClear(&temp2);
112504 sqlite3HashInit(&pSchema->tblHash);
112505 for(pElem=sqliteHashFirst(&temp1)((&temp1)->first); pElem; pElem=sqliteHashNext(pElem)((pElem)->next)){
112506 Table *pTab = sqliteHashData(pElem)((pElem)->data);
112507 sqlite3DeleteTable(0, pTab);
112508 }
112509 sqlite3HashClear(&temp1);
112510 sqlite3HashClear(&pSchema->fkeyHash);
112511 pSchema->pSeqTab = 0;
112512 if( pSchema->schemaFlags & DB_SchemaLoaded0x0001 ){
112513 pSchema->iGeneration++;
112514 }
112515 pSchema->schemaFlags &= ~(DB_SchemaLoaded0x0001|DB_ResetWanted0x0008);
112516}
112517
112518/*
112519** Find and return the schema associated with a BTree. Create
112520** a new one if necessary.
112521*/
112522SQLITE_PRIVATEstatic Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
112523 Schema * p;
112524 if( pBt ){
112525 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
112526 }else{
112527 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
112528 }
112529 if( !p ){
112530 sqlite3OomFault(db);
112531 }else if ( 0==p->file_format ){
112532 sqlite3HashInit(&p->tblHash);
112533 sqlite3HashInit(&p->idxHash);
112534 sqlite3HashInit(&p->trigHash);
112535 sqlite3HashInit(&p->fkeyHash);
112536 p->enc = SQLITE_UTF81;
112537 }
112538 return p;
112539}
112540
112541/************** End of callback.c ********************************************/
112542/************** Begin file delete.c ******************************************/
112543/*
112544** 2001 September 15
112545**
112546** The author disclaims copyright to this source code. In place of
112547** a legal notice, here is a blessing:
112548**
112549** May you do good and not evil.
112550** May you find forgiveness for yourself and forgive others.
112551** May you share freely, never taking more than you give.
112552**
112553*************************************************************************
112554** This file contains C code routines that are called by the parser
112555** in order to generate code for DELETE FROM statements.
112556*/
112557/* #include "sqliteInt.h" */
112558
112559/*
112560** While a SrcList can in general represent multiple tables and subqueries
112561** (as in the FROM clause of a SELECT statement) in this case it contains
112562** the name of a single table, as one might find in an INSERT, DELETE,
112563** or UPDATE statement. Look up that table in the symbol table and
112564** return a pointer. Set an error message and return NULL if the table
112565** name is not found or if any other error occurs.
112566**
112567** The following fields are initialized appropriate in pSrc:
112568**
112569** pSrc->a[0].pTab Pointer to the Table object
112570** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
112571**
112572*/
112573SQLITE_PRIVATEstatic Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
112574 struct SrcList_item *pItem = pSrc->a;
112575 Table *pTab;
112576 assert( pItem && pSrc->nSrc==1 )((void) (0));
112577 pTab = sqlite3LocateTableItem(pParse, 0, pItem);
112578 sqlite3DeleteTable(pParse->db, pItem->pTab);
112579 pItem->pTab = pTab;
112580 if( pTab ){
112581 pTab->nTabRef++;
112582 }
112583 if( sqlite3IndexedByLookup(pParse, pItem) ){
112584 pTab = 0;
112585 }
112586 return pTab;
112587}
112588
112589/* Return true if table pTab is read-only.
112590**
112591** A table is read-only if any of the following are true:
112592**
112593** 1) It is a virtual table and no implementation of the xUpdate method
112594** has been provided
112595**
112596** 2) It is a system table (i.e. sqlite_master), this call is not
112597** part of a nested parse and writable_schema pragma has not
112598** been specified
112599**
112600** 3) The table is a shadow table, the database connection is in
112601** defensive mode, and the current sqlite3_prepare()
112602** is for a top-level SQL statement.
112603*/
112604static int tabIsReadOnly(Parse *pParse, Table *pTab){
112605 sqlite3 *db;
112606 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
112607 return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
112608 }
112609 if( (pTab->tabFlags & (TF_Readonly0x0001|TF_Shadow0x0400))==0 ) return 0;
112610 db = pParse->db;
112611 if( (pTab->tabFlags & TF_Readonly0x0001)!=0 ){
112612 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
112613 }
112614 assert( pTab->tabFlags & TF_Shadow )((void) (0));
112615 return (db->flags & SQLITE_Defensive0x10000000)!=0
112616#ifndef SQLITE_OMIT_VIRTUALTABLE
112617 && db->pVtabCtx==0
112618#endif
112619 && db->nVdbeExec==0;
112620}
112621
112622/*
112623** Check to make sure the given table is writable. If it is not
112624** writable, generate an error message and return 1. If it is
112625** writable return 0;
112626*/
112627SQLITE_PRIVATEstatic int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
112628 if( tabIsReadOnly(pParse, pTab) ){
112629 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
112630 return 1;
112631 }
112632#ifndef SQLITE_OMIT_VIEW
112633 if( !viewOk && pTab->pSelect ){
112634 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
112635 return 1;
112636 }
112637#endif
112638 return 0;
112639}
112640
112641
112642#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
112643/*
112644** Evaluate a view and store its result in an ephemeral table. The
112645** pWhere argument is an optional WHERE clause that restricts the
112646** set of rows in the view that are to be added to the ephemeral table.
112647*/
112648SQLITE_PRIVATEstatic void sqlite3MaterializeView(
112649 Parse *pParse, /* Parsing context */
112650 Table *pView, /* View definition */
112651 Expr *pWhere, /* Optional WHERE clause to be added */
112652 ExprList *pOrderBy, /* Optional ORDER BY clause */
112653 Expr *pLimit, /* Optional LIMIT clause */
112654 int iCur /* Cursor number for ephemeral table */
112655){
112656 SelectDest dest;
112657 Select *pSel;
112658 SrcList *pFrom;
112659 sqlite3 *db = pParse->db;
112660 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
112661 pWhere = sqlite3ExprDup(db, pWhere, 0);
112662 pFrom = sqlite3SrcListAppend(pParse, 0, 0, 0);
112663 if( pFrom ){
112664 assert( pFrom->nSrc==1 )((void) (0));
112665 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
112666 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
112667 assert( pFrom->a[0].pOn==0 )((void) (0));
112668 assert( pFrom->a[0].pUsing==0 )((void) (0));
112669 }
112670 pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy,
112671 SF_IncludeHidden0x20000, pLimit);
112672 sqlite3SelectDestInit(&dest, SRT_EphemTab12, iCur);
112673 sqlite3Select(pParse, pSel, &dest);
112674 sqlite3SelectDelete(db, pSel);
112675}
112676#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
112677
112678#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
112679/*
112680** Generate an expression tree to implement the WHERE, ORDER BY,
112681** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
112682**
112683** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
112684** \__________________________/
112685** pLimitWhere (pInClause)
112686*/
112687SQLITE_PRIVATEstatic Expr *sqlite3LimitWhere(
112688 Parse *pParse, /* The parser context */
112689 SrcList *pSrc, /* the FROM clause -- which tables to scan */
112690 Expr *pWhere, /* The WHERE clause. May be null */
112691 ExprList *pOrderBy, /* The ORDER BY clause. May be null */
112692 Expr *pLimit, /* The LIMIT clause. May be null */
112693 char *zStmtType /* Either DELETE or UPDATE. For err msgs. */
112694){
112695 sqlite3 *db = pParse->db;
112696 Expr *pLhs = NULL((void*)0); /* LHS of IN(SELECT...) operator */
112697 Expr *pInClause = NULL((void*)0); /* WHERE rowid IN ( select ) */
112698 ExprList *pEList = NULL((void*)0); /* Expression list contaning only pSelectRowid */
112699 SrcList *pSelectSrc = NULL((void*)0); /* SELECT rowid FROM x ... (dup of pSrc) */
112700 Select *pSelect = NULL((void*)0); /* Complete SELECT tree */
112701 Table *pTab;
112702
112703 /* Check that there isn't an ORDER BY without a LIMIT clause.
112704 */
112705 if( pOrderBy && pLimit==0 ) {
112706 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
112707 sqlite3ExprDelete(pParse->db, pWhere);
112708 sqlite3ExprListDelete(pParse->db, pOrderBy);
112709 return 0;
112710 }
112711
112712 /* We only need to generate a select expression if there
112713 ** is a limit/offset term to enforce.
112714 */
112715 if( pLimit == 0 ) {
112716 return pWhere;
112717 }
112718
112719 /* Generate a select expression tree to enforce the limit/offset
112720 ** term for the DELETE or UPDATE statement. For example:
112721 ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
112722 ** becomes:
112723 ** DELETE FROM table_a WHERE rowid IN (
112724 ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
112725 ** );
112726 */
112727
112728 pTab = pSrc->a[0].pTab;
112729 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
112730 pLhs = sqlite3PExpr(pParse, TK_ROW75, 0, 0);
112731 pEList = sqlite3ExprListAppend(
112732 pParse, 0, sqlite3PExpr(pParse, TK_ROW75, 0, 0)
112733 );
112734 }else{
112735 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
112736 if( pPk->nKeyCol==1 ){
112737 const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
112738 pLhs = sqlite3Expr(db, TK_ID59, zName);
112739 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID59, zName));
112740 }else{
112741 int i;
112742 for(i=0; i<pPk->nKeyCol; i++){
112743 Expr *p = sqlite3Expr(db, TK_ID59, pTab->aCol[pPk->aiColumn[i]].zName);
112744 pEList = sqlite3ExprListAppend(pParse, pEList, p);
112745 }
112746 pLhs = sqlite3PExpr(pParse, TK_VECTOR169, 0, 0);
112747 if( pLhs ){
112748 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
112749 }
112750 }
112751 }
112752
112753 /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
112754 ** and the SELECT subtree. */
112755 pSrc->a[0].pTab = 0;
112756 pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
112757 pSrc->a[0].pTab = pTab;
112758 pSrc->a[0].pIBIndex = 0;
112759
112760 /* generate the SELECT expression tree. */
112761 pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0,
112762 pOrderBy,0,pLimit
112763 );
112764
112765 /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
112766 pInClause = sqlite3PExpr(pParse, TK_IN49, pLhs, 0);
112767 sqlite3PExprAddSelect(pParse, pInClause, pSelect);
112768 return pInClause;
112769}
112770#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
112771 /* && !defined(SQLITE_OMIT_SUBQUERY) */
112772
112773/*
112774** Generate code for a DELETE FROM statement.
112775**
112776** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
112777** \________/ \________________/
112778** pTabList pWhere
112779*/
112780SQLITE_PRIVATEstatic void sqlite3DeleteFrom(
112781 Parse *pParse, /* The parser context */
112782 SrcList *pTabList, /* The table from which we should delete things */
112783 Expr *pWhere, /* The WHERE clause. May be null */
112784 ExprList *pOrderBy, /* ORDER BY clause. May be null */
112785 Expr *pLimit /* LIMIT clause. May be null */
112786){
112787 Vdbe *v; /* The virtual database engine */
112788 Table *pTab; /* The table from which records will be deleted */
112789 int i; /* Loop counter */
112790 WhereInfo *pWInfo; /* Information about the WHERE clause */
112791 Index *pIdx; /* For looping over indices of the table */
112792 int iTabCur; /* Cursor number for the table */
112793 int iDataCur = 0; /* VDBE cursor for the canonical data source */
112794 int iIdxCur = 0; /* Cursor number of the first index */
112795 int nIdx; /* Number of indices */
112796 sqlite3 *db; /* Main database structure */
112797 AuthContext sContext; /* Authorization context */
112798 NameContext sNC; /* Name context to resolve expressions in */
112799 int iDb; /* Database number */
112800 int memCnt = 0; /* Memory cell used for change counting */
112801 int rcauth; /* Value returned by authorization callback */
112802 int eOnePass; /* ONEPASS_OFF or _SINGLE or _MULTI */
112803 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
112804 u8 *aToOpen = 0; /* Open cursor iTabCur+j if aToOpen[j] is true */
112805 Index *pPk; /* The PRIMARY KEY index on the table */
112806 int iPk = 0; /* First of nPk registers holding PRIMARY KEY value */
112807 i16 nPk = 1; /* Number of columns in the PRIMARY KEY */
112808 int iKey; /* Memory cell holding key of row to be deleted */
112809 i16 nKey; /* Number of memory cells in the row key */
112810 int iEphCur = 0; /* Ephemeral table holding all primary key values */
112811 int iRowSet = 0; /* Register for rowset of rows to delete */
112812 int addrBypass = 0; /* Address of jump over the delete logic */
112813 int addrLoop = 0; /* Top of the delete loop */
112814 int addrEphOpen = 0; /* Instruction to open the Ephemeral table */
112815 int bComplex; /* True if there are triggers or FKs or
112816 ** subqueries in the WHERE clause */
112817
112818#ifndef SQLITE_OMIT_TRIGGER
112819 int isView; /* True if attempting to delete from a view */
112820 Trigger *pTrigger; /* List of table triggers, if required */
112821#endif
112822
112823 memset(&sContext, 0, sizeof(sContext));
112824 db = pParse->db;
112825 if( pParse->nErr || db->mallocFailed ){
112826 goto delete_from_cleanup;
112827 }
112828 assert( pTabList->nSrc==1 )((void) (0));
112829
112830
112831 /* Locate the table which we want to delete. This table has to be
112832 ** put in an SrcList structure because some of the subroutines we
112833 ** will be calling are designed to work with multiple tables and expect
112834 ** an SrcList* parameter instead of just a Table* parameter.
112835 */
112836 pTab = sqlite3SrcListLookup(pParse, pTabList);
112837 if( pTab==0 ) goto delete_from_cleanup;
112838
112839 /* Figure out if we have any triggers and if the table being
112840 ** deleted from is a view
112841 */
112842#ifndef SQLITE_OMIT_TRIGGER
112843 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE121, 0, 0);
112844 isView = pTab->pSelect!=0;
112845#else
112846# define pTrigger 0
112847# define isView 0
112848#endif
112849 bComplex = pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0);
112850#ifdef SQLITE_OMIT_VIEW
112851# undef isView
112852# define isView 0
112853#endif
112854
112855#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
112856 if( !isView ){
112857 pWhere = sqlite3LimitWhere(
112858 pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
112859 );
112860 pOrderBy = 0;
112861 pLimit = 0;
112862 }
112863#endif
112864
112865 /* If pTab is really a view, make sure it has been initialized.
112866 */
112867 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
112868 goto delete_from_cleanup;
112869 }
112870
112871 if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
112872 goto delete_from_cleanup;
112873 }
112874 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
112875 assert( iDb<db->nDb )((void) (0));
112876 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE9, pTab->zName, 0,
112877 db->aDb[iDb].zDbSName);
112878 assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE )((void) (0));
112879 if( rcauth==SQLITE_DENY1 ){
112880 goto delete_from_cleanup;
112881 }
112882 assert(!isView || pTrigger)((void) (0));
112883
112884 /* Assign cursor numbers to the table and all its indices.
112885 */
112886 assert( pTabList->nSrc==1 )((void) (0));
112887 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
112888 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
112889 pParse->nTab++;
112890 }
112891
112892 /* Start the view context
112893 */
112894 if( isView ){
112895 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
112896 }
112897
112898 /* Begin generating code.
112899 */
112900 v = sqlite3GetVdbe(pParse);
112901 if( v==0 ){
112902 goto delete_from_cleanup;
112903 }
112904 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
112905 sqlite3BeginWriteOperation(pParse, bComplex, iDb);
112906
112907 /* If we are trying to delete from a view, realize that view into
112908 ** an ephemeral table.
112909 */
112910#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
112911 if( isView ){
112912 sqlite3MaterializeView(pParse, pTab,
112913 pWhere, pOrderBy, pLimit, iTabCur
112914 );
112915 iDataCur = iIdxCur = iTabCur;
112916 pOrderBy = 0;
112917 pLimit = 0;
112918 }
112919#endif
112920
112921 /* Resolve the column names in the WHERE clause.
112922 */
112923 memset(&sNC, 0, sizeof(sNC));
112924 sNC.pParse = pParse;
112925 sNC.pSrcList = pTabList;
112926 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
112927 goto delete_from_cleanup;
112928 }
112929
112930 /* Initialize the counter of the number of rows deleted, if
112931 ** we are counting rows.
112932 */
112933 if( (db->flags & SQLITE_CountRows0x00000080)!=0
112934 && !pParse->nested
112935 && !pParse->pTriggerTab
112936 ){
112937 memCnt = ++pParse->nMem;
112938 sqlite3VdbeAddOp2(v, OP_Integer70, 0, memCnt);
112939 }
112940
112941#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
112942 /* Special case: A DELETE without a WHERE clause deletes everything.
112943 ** It is easier just to erase the whole table. Prior to version 3.6.5,
112944 ** this optimization caused the row change count (the value returned by
112945 ** API function sqlite3_count_changes) to be set incorrectly.
112946 **
112947 ** The "rcauth==SQLITE_OK" terms is the
112948 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
112949 ** the callback returns SQLITE_IGNORE then the DELETE operation proceeds but
112950 ** the truncate optimization is disabled and all rows are deleted
112951 ** individually.
112952 */
112953 if( rcauth==SQLITE_OK0
112954 && pWhere==0
112955 && !bComplex
112956 && !IsVirtual(pTab)((pTab)->nModuleArg)
112957#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
112958 && db->xPreUpdateCallback==0
112959#endif
112960 ){
112961 assert( !isView )((void) (0));
112962 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
112963 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
112964 sqlite3VdbeAddOp4(v, OP_Clear137, pTab->tnum, iDb, memCnt ? memCnt : -1,
112965 pTab->zName, P4_STATIC(-1));
112966 }
112967 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
112968 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
112969 sqlite3VdbeAddOp2(v, OP_Clear137, pIdx->tnum, iDb);
112970 }
112971 }else
112972#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
112973 {
112974 u16 wcf = WHERE_ONEPASS_DESIRED0x0004|WHERE_DUPLICATES_OK0x0010|WHERE_SEEK_TABLE0x0400;
112975 if( sNC.ncFlags & NC_VarSelect0x0040 ) bComplex = 1;
112976 wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW0x0008);
112977 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
112978 /* For a rowid table, initialize the RowSet to an empty set */
112979 pPk = 0;
112980 nPk = 1;
112981 iRowSet = ++pParse->nMem;
112982 sqlite3VdbeAddOp2(v, OP_Null73, 0, iRowSet);
112983 }else{
112984 /* For a WITHOUT ROWID table, create an ephemeral table used to
112985 ** hold all primary keys for rows to be deleted. */
112986 pPk = sqlite3PrimaryKeyIndex(pTab);
112987 assert( pPk!=0 )((void) (0));
112988 nPk = pPk->nKeyCol;
112989 iPk = pParse->nMem+1;
112990 pParse->nMem += nPk;
112991 iEphCur = pParse->nTab++;
112992 addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, iEphCur, nPk);
112993 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
112994 }
112995
112996 /* Construct a query to find the rowid or primary key for every row
112997 ** to be deleted, based on the WHERE clause. Set variable eOnePass
112998 ** to indicate the strategy used to implement this delete:
112999 **
113000 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
113001 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
113002 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
113003 */
113004 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, wcf, iTabCur+1);
113005 if( pWInfo==0 ) goto delete_from_cleanup;
113006 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
113007 assert( IsVirtual(pTab)==0 || eOnePass!=ONEPASS_MULTI )((void) (0));
113008 assert( IsVirtual(pTab) || bComplex || eOnePass!=ONEPASS_OFF )((void) (0));
113009 if( eOnePass!=ONEPASS_SINGLE1 ) sqlite3MultiWrite(pParse);
113010
113011 /* Keep track of the number of rows to be deleted */
113012 if( memCnt ){
113013 sqlite3VdbeAddOp2(v, OP_AddImm83, memCnt, 1);
113014 }
113015
113016 /* Extract the rowid or primary key for the current row */
113017 if( pPk ){
113018 for(i=0; i<nPk; i++){
113019 assert( pPk->aiColumn[i]>=0 )((void) (0));
113020 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
113021 pPk->aiColumn[i], iPk+i);
113022 }
113023 iKey = iPk;
113024 }else{
113025 iKey = ++pParse->nMem;
113026 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
113027 }
113028
113029 if( eOnePass!=ONEPASS_OFF0 ){
113030 /* For ONEPASS, no need to store the rowid/primary-key. There is only
113031 ** one, so just keep it in its register(s) and fall through to the
113032 ** delete code. */
113033 nKey = nPk; /* OP_Found will use an unpacked key */
113034 aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
113035 if( aToOpen==0 ){
113036 sqlite3WhereEnd(pWInfo);
113037 goto delete_from_cleanup;
113038 }
113039 memset(aToOpen, 1, nIdx+1);
113040 aToOpen[nIdx+1] = 0;
113041 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
113042 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
113043 if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
113044 }else{
113045 if( pPk ){
113046 /* Add the PK key for this row to the temporary table */
113047 iKey = ++pParse->nMem;
113048 nKey = 0; /* Zero tells OP_Found to use a composite key */
113049 sqlite3VdbeAddOp4(v, OP_MakeRecord92, iPk, nPk, iKey,
113050 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
113051 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iEphCur, iKey, iPk, nPk);
113052 }else{
113053 /* Add the rowid of the row to be deleted to the RowSet */
113054 nKey = 1; /* OP_DeferredSeek always uses a single rowid */
113055 sqlite3VdbeAddOp2(v, OP_RowSetAdd148, iRowSet, iKey);
113056 }
113057 }
113058
113059 /* If this DELETE cannot use the ONEPASS strategy, this is the
113060 ** end of the WHERE loop */
113061 if( eOnePass!=ONEPASS_OFF0 ){
113062 addrBypass = sqlite3VdbeMakeLabel(pParse);
113063 }else{
113064 sqlite3WhereEnd(pWInfo);
113065 }
113066
113067 /* Unless this is a view, open cursors for the table we are
113068 ** deleting from and all its indices. If this is a view, then the
113069 ** only effect this statement has is to fire the INSTEAD OF
113070 ** triggers.
113071 */
113072 if( !isView ){
113073 int iAddrOnce = 0;
113074 if( eOnePass==ONEPASS_MULTI2 ){
113075 iAddrOnce = sqlite3VdbeAddOp0(v, OP_Once17); VdbeCoverage(v);
113076 }
113077 testcase( IsVirtual(pTab) );
113078 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite109, OPFLAG_FORDELETE0x08,
113079 iTabCur, aToOpen, &iDataCur, &iIdxCur);
113080 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur )((void) (0));
113081 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 )((void) (0));
113082 if( eOnePass==ONEPASS_MULTI2 ) sqlite3VdbeJumpHere(v, iAddrOnce);
113083 }
113084
113085 /* Set up a loop over the rowids/primary-keys that were found in the
113086 ** where-clause loop above.
113087 */
113088 if( eOnePass!=ONEPASS_OFF0 ){
113089 assert( nKey==nPk )((void) (0)); /* OP_Found will use an unpacked key */
113090 if( !IsVirtual(pTab)((pTab)->nModuleArg) && aToOpen[iDataCur-iTabCur] ){
113091 assert( pPk!=0 || pTab->pSelect!=0 )((void) (0));
113092 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, addrBypass, iKey, nKey);
113093 VdbeCoverage(v);
113094 }
113095 }else if( pPk ){
113096 addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind36, iEphCur); VdbeCoverage(v);
113097 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
113098 sqlite3VdbeAddOp3(v, OP_Column90, iEphCur, 0, iKey);
113099 }else{
113100 sqlite3VdbeAddOp2(v, OP_RowData127, iEphCur, iKey);
113101 }
113102 assert( nKey==0 )((void) (0)); /* OP_Found will use a composite key */
113103 }else{
113104 addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead41, iRowSet, 0, iKey);
113105 VdbeCoverage(v);
113106 assert( nKey==1 )((void) (0));
113107 }
113108
113109 /* Delete the row */
113110#ifndef SQLITE_OMIT_VIRTUALTABLE
113111 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
113112 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
113113 sqlite3VtabMakeWritable(pParse, pTab);
113114 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE )((void) (0));
113115 sqlite3MayAbort(pParse);
113116 if( eOnePass==ONEPASS_SINGLE1 ){
113117 sqlite3VdbeAddOp1(v, OP_Close117, iTabCur);
113118 if( sqlite3IsToplevel(pParse)((pParse)->pToplevel==0) ){
113119 pParse->isMultiWrite = 0;
113120 }
113121 }
113122 sqlite3VdbeAddOp4(v, OP_VUpdate10, 0, 1, iKey, pVTab, P4_VTAB(-12));
113123 sqlite3VdbeChangeP5(v, OE_Abort2);
113124 }else
113125#endif
113126 {
113127 int count = (pParse->nested==0); /* True to count changes */
113128 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
113129 iKey, nKey, count, OE_Default11, eOnePass, aiCurOnePass[1]);
113130 }
113131
113132 /* End of the loop over all rowids/primary-keys. */
113133 if( eOnePass!=ONEPASS_OFF0 ){
113134 sqlite3VdbeResolveLabel(v, addrBypass);
113135 sqlite3WhereEnd(pWInfo);
113136 }else if( pPk ){
113137 sqlite3VdbeAddOp2(v, OP_Next5, iEphCur, addrLoop+1); VdbeCoverage(v);
113138 sqlite3VdbeJumpHere(v, addrLoop);
113139 }else{
113140 sqlite3VdbeGoto(v, addrLoop);
113141 sqlite3VdbeJumpHere(v, addrLoop);
113142 }
113143 } /* End non-truncate path */
113144
113145 /* Update the sqlite_sequence table by storing the content of the
113146 ** maximum rowid counter values recorded while inserting into
113147 ** autoincrement tables.
113148 */
113149 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
113150 sqlite3AutoincrementEnd(pParse);
113151 }
113152
113153 /* Return the number of rows that were deleted. If this routine is
113154 ** generating code because of a call to sqlite3NestedParse(), do not
113155 ** invoke the callback function.
113156 */
113157 if( memCnt ){
113158 sqlite3VdbeAddOp2(v, OP_ResultRow81, memCnt, 1);
113159 sqlite3VdbeSetNumCols(v, 1);
113160 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, "rows deleted", SQLITE_STATIC((sqlite3_destructor_type)0));
113161 }
113162
113163delete_from_cleanup:
113164 sqlite3AuthContextPop(&sContext);
113165 sqlite3SrcListDelete(db, pTabList);
113166 sqlite3ExprDelete(db, pWhere);
113167#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
113168 sqlite3ExprListDelete(db, pOrderBy);
113169 sqlite3ExprDelete(db, pLimit);
113170#endif
113171 sqlite3DbFree(db, aToOpen);
113172 return;
113173}
113174/* Make sure "isView" and other macros defined above are undefined. Otherwise
113175** they may interfere with compilation of other functions in this file
113176** (or in another file, if this file becomes part of the amalgamation). */
113177#ifdef isView
113178 #undef isView
113179#endif
113180#ifdef pTrigger
113181 #undef pTrigger
113182#endif
113183
113184/*
113185** This routine generates VDBE code that causes a single row of a
113186** single table to be deleted. Both the original table entry and
113187** all indices are removed.
113188**
113189** Preconditions:
113190**
113191** 1. iDataCur is an open cursor on the btree that is the canonical data
113192** store for the table. (This will be either the table itself,
113193** in the case of a rowid table, or the PRIMARY KEY index in the case
113194** of a WITHOUT ROWID table.)
113195**
113196** 2. Read/write cursors for all indices of pTab must be open as
113197** cursor number iIdxCur+i for the i-th index.
113198**
113199** 3. The primary key for the row to be deleted must be stored in a
113200** sequence of nPk memory cells starting at iPk. If nPk==0 that means
113201** that a search record formed from OP_MakeRecord is contained in the
113202** single memory location iPk.
113203**
113204** eMode:
113205** Parameter eMode may be passed either ONEPASS_OFF (0), ONEPASS_SINGLE, or
113206** ONEPASS_MULTI. If eMode is not ONEPASS_OFF, then the cursor
113207** iDataCur already points to the row to delete. If eMode is ONEPASS_OFF
113208** then this function must seek iDataCur to the entry identified by iPk
113209** and nPk before reading from it.
113210**
113211** If eMode is ONEPASS_MULTI, then this call is being made as part
113212** of a ONEPASS delete that affects multiple rows. In this case, if
113213** iIdxNoSeek is a valid cursor number (>=0) and is not the same as
113214** iDataCur, then its position should be preserved following the delete
113215** operation. Or, if iIdxNoSeek is not a valid cursor number, the
113216** position of iDataCur should be preserved instead.
113217**
113218** iIdxNoSeek:
113219** If iIdxNoSeek is a valid cursor number (>=0) not equal to iDataCur,
113220** then it identifies an index cursor (from within array of cursors
113221** starting at iIdxCur) that already points to the index entry to be deleted.
113222** Except, this optimization is disabled if there are BEFORE triggers since
113223** the trigger body might have moved the cursor.
113224*/
113225SQLITE_PRIVATEstatic void sqlite3GenerateRowDelete(
113226 Parse *pParse, /* Parsing context */
113227 Table *pTab, /* Table containing the row to be deleted */
113228 Trigger *pTrigger, /* List of triggers to (potentially) fire */
113229 int iDataCur, /* Cursor from which column data is extracted */
113230 int iIdxCur, /* First index cursor */
113231 int iPk, /* First memory cell containing the PRIMARY KEY */
113232 i16 nPk, /* Number of PRIMARY KEY memory cells */
113233 u8 count, /* If non-zero, increment the row change counter */
113234 u8 onconf, /* Default ON CONFLICT policy for triggers */
113235 u8 eMode, /* ONEPASS_OFF, _SINGLE, or _MULTI. See above */
113236 int iIdxNoSeek /* Cursor number of cursor that does not need seeking */
113237){
113238 Vdbe *v = pParse->pVdbe; /* Vdbe */
113239 int iOld = 0; /* First register in OLD.* array */
113240 int iLabel; /* Label resolved to end of generated code */
113241 u8 opSeek; /* Seek opcode */
113242
113243 /* Vdbe is guaranteed to have been allocated by this stage. */
113244 assert( v )((void) (0));
113245 VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
113246 iDataCur, iIdxCur, iPk, (int)nPk));
113247
113248 /* Seek cursor iCur to the row to delete. If this row no longer exists
113249 ** (this can happen if a trigger program has already deleted it), do
113250 ** not attempt to delete it or fire any DELETE triggers. */
113251 iLabel = sqlite3VdbeMakeLabel(pParse);
113252 opSeek = HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ? OP_NotExists31 : OP_NotFound28;
113253 if( eMode==ONEPASS_OFF0 ){
113254 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
113255 VdbeCoverageIf(v, opSeek==OP_NotExists);
113256 VdbeCoverageIf(v, opSeek==OP_NotFound);
113257 }
113258
113259 /* If there are any triggers to fire, allocate a range of registers to
113260 ** use for the old.* references in the triggers. */
113261 if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
113262 u32 mask; /* Mask of OLD.* columns in use */
113263 int iCol; /* Iterator used while populating OLD.* */
113264 int addrStart; /* Start of BEFORE trigger programs */
113265
113266 /* TODO: Could use temporary registers here. Also could attempt to
113267 ** avoid copying the contents of the rowid register. */
113268 mask = sqlite3TriggerColmask(
113269 pParse, pTrigger, 0, 0, TRIGGER_BEFORE1|TRIGGER_AFTER2, pTab, onconf
113270 );
113271 mask |= sqlite3FkOldmask(pParse, pTab);
113272 iOld = pParse->nMem+1;
113273 pParse->nMem += (1 + pTab->nCol);
113274
113275 /* Populate the OLD.* pseudo-table register array. These values will be
113276 ** used by any BEFORE and AFTER triggers that exist. */
113277 sqlite3VdbeAddOp2(v, OP_Copy78, iPk, iOld);
113278 for(iCol=0; iCol<pTab->nCol; iCol++){
113279 testcase( mask!=0xffffffff && iCol==31 );
113280 testcase( mask!=0xffffffff && iCol==32 );
113281 if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol)(((unsigned int)1)<<(iCol)))!=0) ){
113282 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
113283 }
113284 }
113285
113286 /* Invoke BEFORE DELETE trigger programs. */
113287 addrStart = sqlite3VdbeCurrentAddr(v);
113288 sqlite3CodeRowTrigger(pParse, pTrigger,
113289 TK_DELETE121, 0, TRIGGER_BEFORE1, pTab, iOld, onconf, iLabel
113290 );
113291
113292 /* If any BEFORE triggers were coded, then seek the cursor to the
113293 ** row to be deleted again. It may be that the BEFORE triggers moved
113294 ** the cursor or already deleted the row that the cursor was
113295 ** pointing to.
113296 **
113297 ** Also disable the iIdxNoSeek optimization since the BEFORE trigger
113298 ** may have moved that cursor.
113299 */
113300 if( addrStart<sqlite3VdbeCurrentAddr(v) ){
113301 sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
113302 VdbeCoverageIf(v, opSeek==OP_NotExists);
113303 VdbeCoverageIf(v, opSeek==OP_NotFound);
113304 testcase( iIdxNoSeek>=0 );
113305 iIdxNoSeek = -1;
113306 }
113307
113308 /* Do FK processing. This call checks that any FK constraints that
113309 ** refer to this table (i.e. constraints attached to other tables)
113310 ** are not violated by deleting this row. */
113311 sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
113312 }
113313
113314 /* Delete the index and table entries. Skip this step if pTab is really
113315 ** a view (in which case the only effect of the DELETE statement is to
113316 ** fire the INSTEAD OF triggers).
113317 **
113318 ** If variable 'count' is non-zero, then this OP_Delete instruction should
113319 ** invoke the update-hook. The pre-update-hook, on the other hand should
113320 ** be invoked unless table pTab is a system table. The difference is that
113321 ** the update-hook is not invoked for rows removed by REPLACE, but the
113322 ** pre-update-hook is.
113323 */
113324 if( pTab->pSelect==0 ){
113325 u8 p5 = 0;
113326 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
113327 sqlite3VdbeAddOp2(v, OP_Delete123, iDataCur, (count?OPFLAG_NCHANGE0x01:0));
113328 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
113329 sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE(-6));
113330 }
113331 if( eMode!=ONEPASS_OFF0 ){
113332 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE0x04);
113333 }
113334 if( iIdxNoSeek>=0 && iIdxNoSeek!=iDataCur ){
113335 sqlite3VdbeAddOp1(v, OP_Delete123, iIdxNoSeek);
113336 }
113337 if( eMode==ONEPASS_MULTI2 ) p5 |= OPFLAG_SAVEPOSITION0x02;
113338 sqlite3VdbeChangeP5(v, p5);
113339 }
113340
113341 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
113342 ** handle rows (possibly in other tables) that refer via a foreign key
113343 ** to the row just deleted. */
113344 sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
113345
113346 /* Invoke AFTER DELETE trigger programs. */
113347 sqlite3CodeRowTrigger(pParse, pTrigger,
113348 TK_DELETE121, 0, TRIGGER_AFTER2, pTab, iOld, onconf, iLabel
113349 );
113350
113351 /* Jump here if the row had already been deleted before any BEFORE
113352 ** trigger programs were invoked. Or if a trigger program throws a
113353 ** RAISE(IGNORE) exception. */
113354 sqlite3VdbeResolveLabel(v, iLabel);
113355 VdbeModuleComment((v, "END: GenRowDel()"));
113356}
113357
113358/*
113359** This routine generates VDBE code that causes the deletion of all
113360** index entries associated with a single row of a single table, pTab
113361**
113362** Preconditions:
113363**
113364** 1. A read/write cursor "iDataCur" must be open on the canonical storage
113365** btree for the table pTab. (This will be either the table itself
113366** for rowid tables or to the primary key index for WITHOUT ROWID
113367** tables.)
113368**
113369** 2. Read/write cursors for all indices of pTab must be open as
113370** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
113371** index is the 0-th index.)
113372**
113373** 3. The "iDataCur" cursor must be already be positioned on the row
113374** that is to be deleted.
113375*/
113376SQLITE_PRIVATEstatic void sqlite3GenerateRowIndexDelete(
113377 Parse *pParse, /* Parsing and code generating context */
113378 Table *pTab, /* Table containing the row to be deleted */
113379 int iDataCur, /* Cursor of table holding data. */
113380 int iIdxCur, /* First index cursor */
113381 int *aRegIdx, /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
113382 int iIdxNoSeek /* Do not delete from this cursor */
113383){
113384 int i; /* Index loop counter */
113385 int r1 = -1; /* Register holding an index key */
113386 int iPartIdxLabel; /* Jump destination for skipping partial index entries */
113387 Index *pIdx; /* Current index */
113388 Index *pPrior = 0; /* Prior index */
113389 Vdbe *v; /* The prepared statement under construction */
113390 Index *pPk; /* PRIMARY KEY index, or NULL for rowid tables */
113391
113392 v = pParse->pVdbe;
113393 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
113394 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
113395 assert( iIdxCur+i!=iDataCur || pPk==pIdx )((void) (0));
113396 if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
113397 if( pIdx==pPk ) continue;
113398 if( iIdxCur+i==iIdxNoSeek ) continue;
113399 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
113400 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
113401 &iPartIdxLabel, pPrior, r1);
113402 sqlite3VdbeAddOp3(v, OP_IdxDelete133, iIdxCur+i, r1,
113403 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
113404 sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
113405 pPrior = pIdx;
113406 }
113407}
113408
113409/*
113410** Generate code that will assemble an index key and stores it in register
113411** regOut. The key with be for index pIdx which is an index on pTab.
113412** iCur is the index of a cursor open on the pTab table and pointing to
113413** the entry that needs indexing. If pTab is a WITHOUT ROWID table, then
113414** iCur must be the cursor of the PRIMARY KEY index.
113415**
113416** Return a register number which is the first in a block of
113417** registers that holds the elements of the index key. The
113418** block of registers has already been deallocated by the time
113419** this routine returns.
113420**
113421** If *piPartIdxLabel is not NULL, fill it in with a label and jump
113422** to that label if pIdx is a partial index that should be skipped.
113423** The label should be resolved using sqlite3ResolvePartIdxLabel().
113424** A partial index should be skipped if its WHERE clause evaluates
113425** to false or null. If pIdx is not a partial index, *piPartIdxLabel
113426** will be set to zero which is an empty label that is ignored by
113427** sqlite3ResolvePartIdxLabel().
113428**
113429** The pPrior and regPrior parameters are used to implement a cache to
113430** avoid unnecessary register loads. If pPrior is not NULL, then it is
113431** a pointer to a different index for which an index key has just been
113432** computed into register regPrior. If the current pIdx index is generating
113433** its key into the same sequence of registers and if pPrior and pIdx share
113434** a column in common, then the register corresponding to that column already
113435** holds the correct value and the loading of that register is skipped.
113436** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
113437** on a table with multiple indices, and especially with the ROWID or
113438** PRIMARY KEY columns of the index.
113439*/
113440SQLITE_PRIVATEstatic int sqlite3GenerateIndexKey(
113441 Parse *pParse, /* Parsing context */
113442 Index *pIdx, /* The index for which to generate a key */
113443 int iDataCur, /* Cursor number from which to take column data */
113444 int regOut, /* Put the new key into this register if not 0 */
113445 int prefixOnly, /* Compute only a unique prefix of the key */
113446 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
113447 Index *pPrior, /* Previously generated index key */
113448 int regPrior /* Register holding previous generated key */
113449){
113450 Vdbe *v = pParse->pVdbe;
113451 int j;
113452 int regBase;
113453 int nCol;
113454
113455 if( piPartIdxLabel ){
113456 if( pIdx->pPartIdxWhere ){
113457 *piPartIdxLabel = sqlite3VdbeMakeLabel(pParse);
113458 pParse->iSelfTab = iDataCur + 1;
113459 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
113460 SQLITE_JUMPIFNULL0x10);
113461 pParse->iSelfTab = 0;
113462 }else{
113463 *piPartIdxLabel = 0;
113464 }
113465 }
113466 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
113467 regBase = sqlite3GetTempRange(pParse, nCol);
113468 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
113469 for(j=0; j<nCol; j++){
113470 if( pPrior
113471 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
113472 && pPrior->aiColumn[j]!=XN_EXPR(-2)
113473 ){
113474 /* This column was already computed by the previous index */
113475 continue;
113476 }
113477 sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iDataCur, j, regBase+j);
113478 /* If the column affinity is REAL but the number is an integer, then it
113479 ** might be stored in the table as an integer (using a compact
113480 ** representation) then converted to REAL by an OP_RealAffinity opcode.
113481 ** But we are getting ready to store this value back into an index, where
113482 ** it should be converted by to INTEGER again. So omit the OP_RealAffinity
113483 ** opcode if it is present */
113484 sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity84);
113485 }
113486 if( regOut ){
113487 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regBase, nCol, regOut);
113488 if( pIdx->pTable->pSelect ){
113489 const char *zAff = sqlite3IndexAffinityStr(pParse->db, pIdx);
113490 sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT0);
113491 }
113492 }
113493 sqlite3ReleaseTempRange(pParse, regBase, nCol);
113494 return regBase;
113495}
113496
113497/*
113498** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
113499** because it was a partial index, then this routine should be called to
113500** resolve that label.
113501*/
113502SQLITE_PRIVATEstatic void sqlite3ResolvePartIdxLabel(Parse *pParse, int iLabel){
113503 if( iLabel ){
113504 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
113505 }
113506}
113507
113508/************** End of delete.c **********************************************/
113509/************** Begin file func.c ********************************************/
113510/*
113511** 2002 February 23
113512**
113513** The author disclaims copyright to this source code. In place of
113514** a legal notice, here is a blessing:
113515**
113516** May you do good and not evil.
113517** May you find forgiveness for yourself and forgive others.
113518** May you share freely, never taking more than you give.
113519**
113520*************************************************************************
113521** This file contains the C-language implementations for many of the SQL
113522** functions of SQLite. (Some function, and in particular the date and
113523** time functions, are implemented separately.)
113524*/
113525/* #include "sqliteInt.h" */
113526/* #include <stdlib.h> */
113527/* #include <assert.h> */
113528/* #include <math.h> */
113529/* #include "vdbeInt.h" */
113530
113531/*
113532** Return the collating function associated with a function.
113533*/
113534static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
113535 VdbeOp *pOp;
113536 assert( context->pVdbe!=0 )((void) (0));
113537 pOp = &context->pVdbe->aOp[context->iOp-1];
113538 assert( pOp->opcode==OP_CollSeq )((void) (0));
113539 assert( pOp->p4type==P4_COLLSEQ )((void) (0));
113540 return pOp->p4.pColl;
113541}
113542
113543/*
113544** Indicate that the accumulator load should be skipped on this
113545** iteration of the aggregate loop.
113546*/
113547static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
113548 assert( context->isError<=0 )((void) (0));
113549 context->isError = -1;
113550 context->skipFlag = 1;
113551}
113552
113553/*
113554** Implementation of the non-aggregate min() and max() functions
113555*/
113556static void minmaxFunc(
113557 sqlite3_context *context,
113558 int argc,
113559 sqlite3_value **argv
113560){
113561 int i;
113562 int mask; /* 0 for min() or 0xffffffff for max() */
113563 int iBest;
113564 CollSeq *pColl;
113565
113566 assert( argc>1 )((void) (0));
113567 mask = sqlite3_user_data(context)==0 ? 0 : -1;
113568 pColl = sqlite3GetFuncCollSeq(context);
113569 assert( pColl )((void) (0));
113570 assert( mask==-1 || mask==0 )((void) (0));
113571 iBest = 0;
113572 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
113573 for(i=1; i<argc; i++){
113574 if( sqlite3_value_type(argv[i])==SQLITE_NULL5 ) return;
113575 if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
113576 testcase( mask==0 );
113577 iBest = i;
113578 }
113579 }
113580 sqlite3_result_value(context, argv[iBest]);
113581}
113582
113583/*
113584** Return the type of the argument.
113585*/
113586static void typeofFunc(
113587 sqlite3_context *context,
113588 int NotUsed,
113589 sqlite3_value **argv
113590){
113591 static const char *azType[] = { "integer", "real", "text", "blob", "null" };
113592 int i = sqlite3_value_type(argv[0]) - 1;
113593 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
113594 assert( i>=0 && i<ArraySize(azType) )((void) (0));
113595 assert( SQLITE_INTEGER==1 )((void) (0));
113596 assert( SQLITE_FLOAT==2 )((void) (0));
113597 assert( SQLITE_TEXT==3 )((void) (0));
113598 assert( SQLITE_BLOB==4 )((void) (0));
113599 assert( SQLITE_NULL==5 )((void) (0));
113600 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
113601 ** the datatype code for the initial datatype of the sqlite3_value object
113602 ** V. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT,
113603 ** SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. */
113604 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
113605}
113606
113607
113608/*
113609** Implementation of the length() function
113610*/
113611static void lengthFunc(
113612 sqlite3_context *context,
113613 int argc,
113614 sqlite3_value **argv
113615){
113616 assert( argc==1 )((void) (0));
113617 UNUSED_PARAMETER(argc)(void)(argc);
113618 switch( sqlite3_value_type(argv[0]) ){
113619 case SQLITE_BLOB4:
113620 case SQLITE_INTEGER1:
113621 case SQLITE_FLOAT2: {
113622 sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
113623 break;
113624 }
113625 case SQLITE_TEXT3: {
113626 const unsigned char *z = sqlite3_value_text(argv[0]);
113627 const unsigned char *z0;
113628 unsigned char c;
113629 if( z==0 ) return;
113630 z0 = z;
113631 while( (c = *z)!=0 ){
113632 z++;
113633 if( c>=0xc0 ){
113634 while( (*z & 0xc0)==0x80 ){ z++; z0++; }
113635 }
113636 }
113637 sqlite3_result_int(context, (int)(z-z0));
113638 break;
113639 }
113640 default: {
113641 sqlite3_result_null(context);
113642 break;
113643 }
113644 }
113645}
113646
113647/*
113648** Implementation of the abs() function.
113649**
113650** IMP: R-23979-26855 The abs(X) function returns the absolute value of
113651** the numeric argument X.
113652*/
113653static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
113654 assert( argc==1 )((void) (0));
113655 UNUSED_PARAMETER(argc)(void)(argc);
113656 switch( sqlite3_value_type(argv[0]) ){
113657 case SQLITE_INTEGER1: {
113658 i64 iVal = sqlite3_value_int64(argv[0]);
113659 if( iVal<0 ){
113660 if( iVal==SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) ){
113661 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
113662 ** then abs(X) throws an integer overflow error since there is no
113663 ** equivalent positive 64-bit two complement value. */
113664 sqlite3_result_error(context, "integer overflow", -1);
113665 return;
113666 }
113667 iVal = -iVal;
113668 }
113669 sqlite3_result_int64(context, iVal);
113670 break;
113671 }
113672 case SQLITE_NULL5: {
113673 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
113674 sqlite3_result_null(context);
113675 break;
113676 }
113677 default: {
113678 /* Because sqlite3_value_double() returns 0.0 if the argument is not
113679 ** something that can be converted into a number, we have:
113680 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
113681 ** that cannot be converted to a numeric value.
113682 */
113683 double rVal = sqlite3_value_double(argv[0]);
113684 if( rVal<0 ) rVal = -rVal;
113685 sqlite3_result_double(context, rVal);
113686 break;
113687 }
113688 }
113689}
113690
113691/*
113692** Implementation of the instr() function.
113693**
113694** instr(haystack,needle) finds the first occurrence of needle
113695** in haystack and returns the number of previous characters plus 1,
113696** or 0 if needle does not occur within haystack.
113697**
113698** If both haystack and needle are BLOBs, then the result is one more than
113699** the number of bytes in haystack prior to the first occurrence of needle,
113700** or 0 if needle never occurs in haystack.
113701*/
113702static void instrFunc(
113703 sqlite3_context *context,
113704 int argc,
113705 sqlite3_value **argv
113706){
113707 const unsigned char *zHaystack;
113708 const unsigned char *zNeedle;
113709 int nHaystack;
113710 int nNeedle;
113711 int typeHaystack, typeNeedle;
113712 int N = 1;
113713 int isText;
113714 unsigned char firstChar;
113715
113716 UNUSED_PARAMETER(argc)(void)(argc);
113717 typeHaystack = sqlite3_value_type(argv[0]);
113718 typeNeedle = sqlite3_value_type(argv[1]);
113719 if( typeHaystack==SQLITE_NULL5 || typeNeedle==SQLITE_NULL5 ) return;
113720 nHaystack = sqlite3_value_bytes(argv[0]);
113721 nNeedle = sqlite3_value_bytes(argv[1]);
113722 if( nNeedle>0 ){
113723 if( typeHaystack==SQLITE_BLOB4 && typeNeedle==SQLITE_BLOB4 ){
113724 zHaystack = sqlite3_value_blob(argv[0]);
113725 zNeedle = sqlite3_value_blob(argv[1]);
113726 isText = 0;
113727 }else{
113728 zHaystack = sqlite3_value_text(argv[0]);
113729 zNeedle = sqlite3_value_text(argv[1]);
113730 isText = 1;
113731 }
113732 if( zNeedle==0 || (nHaystack && zHaystack==0) ) return;
113733 firstChar = zNeedle[0];
113734 while( nNeedle<=nHaystack
113735 && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
113736 ){
113737 N++;
113738 do{
113739 nHaystack--;
113740 zHaystack++;
113741 }while( isText && (zHaystack[0]&0xc0)==0x80 );
113742 }
113743 if( nNeedle>nHaystack ) N = 0;
113744 }
113745 sqlite3_result_int(context, N);
113746}
113747
113748/*
113749** Implementation of the printf() function.
113750*/
113751static void printfFunc(
113752 sqlite3_context *context,
113753 int argc,
113754 sqlite3_value **argv
113755){
113756 PrintfArguments x;
113757 StrAccum str;
113758 const char *zFormat;
113759 int n;
113760 sqlite3 *db = sqlite3_context_db_handle(context);
113761
113762 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
113763 x.nArg = argc-1;
113764 x.nUsed = 0;
113765 x.apArg = argv+1;
113766 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH0]);
113767 str.printfFlags = SQLITE_PRINTF_SQLFUNC0x02;
113768 sqlite3_str_appendf(&str, zFormat, &x);
113769 n = str.nChar;
113770 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
113771 SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3MallocSize));
113772 }
113773}
113774
113775/*
113776** Implementation of the substr() function.
113777**
113778** substr(x,p1,p2) returns p2 characters of x[] beginning with p1.
113779** p1 is 1-indexed. So substr(x,1,1) returns the first character
113780** of x. If x is text, then we actually count UTF-8 characters.
113781** If x is a blob, then we count bytes.
113782**
113783** If p1 is negative, then we begin abs(p1) from the end of x[].
113784**
113785** If p2 is negative, return the p2 characters preceding p1.
113786*/
113787static void substrFunc(
113788 sqlite3_context *context,
113789 int argc,
113790 sqlite3_value **argv
113791){
113792 const unsigned char *z;
113793 const unsigned char *z2;
113794 int len;
113795 int p0type;
113796 i64 p1, p2;
113797 int negP2 = 0;
113798
113799 assert( argc==3 || argc==2 )((void) (0));
113800 if( sqlite3_value_type(argv[1])==SQLITE_NULL5
113801 || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL5)
113802 ){
113803 return;
113804 }
113805 p0type = sqlite3_value_type(argv[0]);
113806 p1 = sqlite3_value_int(argv[1]);
113807 if( p0type==SQLITE_BLOB4 ){
113808 len = sqlite3_value_bytes(argv[0]);
113809 z = sqlite3_value_blob(argv[0]);
113810 if( z==0 ) return;
113811 assert( len==sqlite3_value_bytes(argv[0]) )((void) (0));
113812 }else{
113813 z = sqlite3_value_text(argv[0]);
113814 if( z==0 ) return;
113815 len = 0;
113816 if( p1<0 ){
113817 for(z2=z; *z2; len++){
113818 SQLITE_SKIP_UTF8(z2){ if( (*(z2++))>=0xc0 ){ while( (*z2 & 0xc0)==0x80 ){ z2
++; } } }
;
113819 }
113820 }
113821 }
113822#ifdef SQLITE_SUBSTR_COMPATIBILITY
113823 /* If SUBSTR_COMPATIBILITY is defined then substr(X,0,N) work the same as
113824 ** as substr(X,1,N) - it returns the first N characters of X. This
113825 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
113826 ** from 2009-02-02 for compatibility of applications that exploited the
113827 ** old buggy behavior. */
113828 if( p1==0 ) p1 = 1; /* <rdar://problem/6778339> */
113829#endif
113830 if( argc==3 ){
113831 p2 = sqlite3_value_int(argv[2]);
113832 if( p2<0 ){
113833 p2 = -p2;
113834 negP2 = 1;
113835 }
113836 }else{
113837 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH0];
113838 }
113839 if( p1<0 ){
113840 p1 += len;
113841 if( p1<0 ){
113842 p2 += p1;
113843 if( p2<0 ) p2 = 0;
113844 p1 = 0;
113845 }
113846 }else if( p1>0 ){
113847 p1--;
113848 }else if( p2>0 ){
113849 p2--;
113850 }
113851 if( negP2 ){
113852 p1 -= p2;
113853 if( p1<0 ){
113854 p2 += p1;
113855 p1 = 0;
113856 }
113857 }
113858 assert( p1>=0 && p2>=0 )((void) (0));
113859 if( p0type!=SQLITE_BLOB4 ){
113860 while( *z && p1 ){
113861 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
113862 p1--;
113863 }
113864 for(z2=z; *z2 && p2; p2--){
113865 SQLITE_SKIP_UTF8(z2){ if( (*(z2++))>=0xc0 ){ while( (*z2 & 0xc0)==0x80 ){ z2
++; } } }
;
113866 }
113867 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT((sqlite3_destructor_type)-1),
113868 SQLITE_UTF81);
113869 }else{
113870 if( p1+p2>len ){
113871 p2 = len-p1;
113872 if( p2<0 ) p2 = 0;
113873 }
113874 sqlite3_result_blob64(context, (char*)&z[p1], (u64)p2, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
113875 }
113876}
113877
113878/*
113879** Implementation of the round() function
113880*/
113881#ifndef SQLITE_OMIT_FLOATING_POINT
113882static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
113883 int n = 0;
113884 double r;
113885 char *zBuf;
113886 assert( argc==1 || argc==2 )((void) (0));
113887 if( argc==2 ){
113888 if( SQLITE_NULL5==sqlite3_value_type(argv[1]) ) return;
113889 n = sqlite3_value_int(argv[1]);
113890 if( n>30 ) n = 30;
113891 if( n<0 ) n = 0;
113892 }
113893 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
113894 r = sqlite3_value_double(argv[0]);
113895 /* If Y==0 and X will fit in a 64-bit int,
113896 ** handle the rounding directly,
113897 ** otherwise use printf.
113898 */
113899 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
113900 /* The value has no fractional part so there is nothing to round */
113901 }else if( n==0 ){
113902 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
113903 }else{
113904 zBuf = sqlite3_mprintf("%.*f",n,r);
113905 if( zBuf==0 ){
113906 sqlite3_result_error_nomem(context);
113907 return;
113908 }
113909 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF81);
113910 sqlite3_free(zBuf);
113911 }
113912 sqlite3_result_double(context, r);
113913}
113914#endif
113915
113916/*
113917** Allocate nByte bytes of space using sqlite3Malloc(). If the
113918** allocation fails, call sqlite3_result_error_nomem() to notify
113919** the database handle that malloc() has failed and return NULL.
113920** If nByte is larger than the maximum string or blob length, then
113921** raise an SQLITE_TOOBIG exception and return NULL.
113922*/
113923static void *contextMalloc(sqlite3_context *context, i64 nByte){
113924 char *z;
113925 sqlite3 *db = sqlite3_context_db_handle(context);
113926 assert( nByte>0 )((void) (0));
113927 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
113928 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
113929 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
113930 sqlite3_result_error_toobig(context);
113931 z = 0;
113932 }else{
113933 z = sqlite3Malloc(nByte);
113934 if( !z ){
113935 sqlite3_result_error_nomem(context);
113936 }
113937 }
113938 return z;
113939}
113940
113941/*
113942** Implementation of the upper() and lower() SQL functions.
113943*/
113944static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
113945 char *z1;
113946 const char *z2;
113947 int i, n;
113948 UNUSED_PARAMETER(argc)(void)(argc);
113949 z2 = (char*)sqlite3_value_text(argv[0]);
113950 n = sqlite3_value_bytes(argv[0]);
113951 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
113952 assert( z2==(char*)sqlite3_value_text(argv[0]) )((void) (0));
113953 if( z2 ){
113954 z1 = contextMalloc(context, ((i64)n)+1);
113955 if( z1 ){
113956 for(i=0; i<n; i++){
113957 z1[i] = (char)sqlite3Toupper(z2[i])((z2[i])&~(sqlite3CtypeMap[(unsigned char)(z2[i])]&0x20
))
;
113958 }
113959 sqlite3_result_text(context, z1, n, sqlite3_free);
113960 }
113961 }
113962}
113963static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
113964 char *z1;
113965 const char *z2;
113966 int i, n;
113967 UNUSED_PARAMETER(argc)(void)(argc);
113968 z2 = (char*)sqlite3_value_text(argv[0]);
113969 n = sqlite3_value_bytes(argv[0]);
113970 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
113971 assert( z2==(char*)sqlite3_value_text(argv[0]) )((void) (0));
113972 if( z2 ){
113973 z1 = contextMalloc(context, ((i64)n)+1);
113974 if( z1 ){
113975 for(i=0; i<n; i++){
113976 z1[i] = sqlite3Tolower(z2[i])(sqlite3UpperToLower[(unsigned char)(z2[i])]);
113977 }
113978 sqlite3_result_text(context, z1, n, sqlite3_free);
113979 }
113980 }
113981}
113982
113983/*
113984** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
113985** as VDBE code so that unused argument values do not have to be computed.
113986** However, we still need some kind of function implementation for this
113987** routines in the function table. The noopFunc macro provides this.
113988** noopFunc will never be called so it doesn't matter what the implementation
113989** is. We might as well use the "version()" function as a substitute.
113990*/
113991#define noopFuncversionFunc versionFunc /* Substitute function - never called */
113992
113993/*
113994** Implementation of random(). Return a random integer.
113995*/
113996static void randomFunc(
113997 sqlite3_context *context,
113998 int NotUsed,
113999 sqlite3_value **NotUsed2
114000){
114001 sqlite_int64 r;
114002 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
114003 sqlite3_randomness(sizeof(r), &r);
114004 if( r<0 ){
114005 /* We need to prevent a random number of 0x8000000000000000
114006 ** (or -9223372036854775808) since when you do abs() of that
114007 ** number of you get the same value back again. To do this
114008 ** in a way that is testable, mask the sign bit off of negative
114009 ** values, resulting in a positive value. Then take the
114010 ** 2s complement of that positive value. The end result can
114011 ** therefore be no less than -9223372036854775807.
114012 */
114013 r = -(r & LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
114014 }
114015 sqlite3_result_int64(context, r);
114016}
114017
114018/*
114019** Implementation of randomblob(N). Return a random blob
114020** that is N bytes long.
114021*/
114022static void randomBlob(
114023 sqlite3_context *context,
114024 int argc,
114025 sqlite3_value **argv
114026){
114027 sqlite3_int64 n;
114028 unsigned char *p;
114029 assert( argc==1 )((void) (0));
114030 UNUSED_PARAMETER(argc)(void)(argc);
114031 n = sqlite3_value_int64(argv[0]);
114032 if( n<1 ){
114033 n = 1;
114034 }
114035 p = contextMalloc(context, n);
114036 if( p ){
114037 sqlite3_randomness(n, p);
114038 sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
114039 }
114040}
114041
114042/*
114043** Implementation of the last_insert_rowid() SQL function. The return
114044** value is the same as the sqlite3_last_insert_rowid() API function.
114045*/
114046static void last_insert_rowid(
114047 sqlite3_context *context,
114048 int NotUsed,
114049 sqlite3_value **NotUsed2
114050){
114051 sqlite3 *db = sqlite3_context_db_handle(context);
114052 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
114053 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
114054 ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
114055 ** function. */
114056 sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
114057}
114058
114059/*
114060** Implementation of the changes() SQL function.
114061**
114062** IMP: R-62073-11209 The changes() SQL function is a wrapper
114063** around the sqlite3_changes() C/C++ function and hence follows the same
114064** rules for counting changes.
114065*/
114066static void changes(
114067 sqlite3_context *context,
114068 int NotUsed,
114069 sqlite3_value **NotUsed2
114070){
114071 sqlite3 *db = sqlite3_context_db_handle(context);
114072 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
114073 sqlite3_result_int(context, sqlite3_changes(db));
114074}
114075
114076/*
114077** Implementation of the total_changes() SQL function. The return value is
114078** the same as the sqlite3_total_changes() API function.
114079*/
114080static void total_changes(
114081 sqlite3_context *context,
114082 int NotUsed,
114083 sqlite3_value **NotUsed2
114084){
114085 sqlite3 *db = sqlite3_context_db_handle(context);
114086 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
114087 /* IMP: R-52756-41993 This function is a wrapper around the
114088 ** sqlite3_total_changes() C/C++ interface. */
114089 sqlite3_result_int(context, sqlite3_total_changes(db));
114090}
114091
114092/*
114093** A structure defining how to do GLOB-style comparisons.
114094*/
114095struct compareInfo {
114096 u8 matchAll; /* "*" or "%" */
114097 u8 matchOne; /* "?" or "_" */
114098 u8 matchSet; /* "[" or 0 */
114099 u8 noCase; /* true to ignore case differences */
114100};
114101
114102/*
114103** For LIKE and GLOB matching on EBCDIC machines, assume that every
114104** character is exactly one byte in size. Also, provde the Utf8Read()
114105** macro for fast reading of the next character in the common case where
114106** the next character is ASCII.
114107*/
114108#if defined(SQLITE_EBCDIC)
114109# define sqlite3Utf8Read(A) (*((*A)++))
114110# define Utf8Read(A)(A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) (*(A++))
114111#else
114112# define Utf8Read(A)(A[0]<0x80?*(A++):sqlite3Utf8Read(&A)) (A[0]<0x80?*(A++):sqlite3Utf8Read(&A))
114113#endif
114114
114115static const struct compareInfo globInfo = { '*', '?', '[', 0 };
114116/* The correct SQL-92 behavior is for the LIKE operator to ignore
114117** case. Thus 'a' LIKE 'A' would be true. */
114118static const struct compareInfo likeInfoNorm = { '%', '_', 0, 1 };
114119/* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
114120** is case sensitive causing 'a' LIKE 'A' to be false */
114121static const struct compareInfo likeInfoAlt = { '%', '_', 0, 0 };
114122
114123/*
114124** Possible error returns from patternMatch()
114125*/
114126#define SQLITE_MATCH0 0
114127#define SQLITE_NOMATCH1 1
114128#define SQLITE_NOWILDCARDMATCH2 2
114129
114130/*
114131** Compare two UTF-8 strings for equality where the first string is
114132** a GLOB or LIKE expression. Return values:
114133**
114134** SQLITE_MATCH: Match
114135** SQLITE_NOMATCH: No match
114136** SQLITE_NOWILDCARDMATCH: No match in spite of having * or % wildcards.
114137**
114138** Globbing rules:
114139**
114140** '*' Matches any sequence of zero or more characters.
114141**
114142** '?' Matches exactly one character.
114143**
114144** [...] Matches one character from the enclosed list of
114145** characters.
114146**
114147** [^...] Matches one character not in the enclosed list.
114148**
114149** With the [...] and [^...] matching, a ']' character can be included
114150** in the list by making it the first character after '[' or '^'. A
114151** range of characters can be specified using '-'. Example:
114152** "[a-z]" matches any single lower-case letter. To match a '-', make
114153** it the last character in the list.
114154**
114155** Like matching rules:
114156**
114157** '%' Matches any sequence of zero or more characters
114158**
114159*** '_' Matches any one character
114160**
114161** Ec Where E is the "esc" character and c is any other
114162** character, including '%', '_', and esc, match exactly c.
114163**
114164** The comments within this routine usually assume glob matching.
114165**
114166** This routine is usually quick, but can be N**2 in the worst case.
114167*/
114168static int patternCompare(
114169 const u8 *zPattern, /* The glob pattern */
114170 const u8 *zString, /* The string to compare against the glob */
114171 const struct compareInfo *pInfo, /* Information about how to do the compare */
114172 u32 matchOther /* The escape char (LIKE) or '[' (GLOB) */
114173){
114174 u32 c, c2; /* Next pattern and input string chars */
114175 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
114176 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
114177 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
114178 const u8 *zEscaped = 0; /* One past the last escaped input char */
114179
114180 while( (c = Utf8Read(zPattern)(zPattern[0]<0x80?*(zPattern++):sqlite3Utf8Read(&zPattern
))
)!=0 ){
114181 if( c==matchAll ){ /* Match "*" */
114182 /* Skip over multiple "*" characters in the pattern. If there
114183 ** are also "?" characters, skip those as well, but consume a
114184 ** single character of the input string for each "?" skipped */
114185 while( (c=Utf8Read(zPattern)(zPattern[0]<0x80?*(zPattern++):sqlite3Utf8Read(&zPattern
))
) == matchAll || c == matchOne ){
114186 if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
114187 return SQLITE_NOWILDCARDMATCH2;
114188 }
114189 }
114190 if( c==0 ){
114191 return SQLITE_MATCH0; /* "*" at the end of the pattern matches */
114192 }else if( c==matchOther ){
114193 if( pInfo->matchSet==0 ){
114194 c = sqlite3Utf8Read(&zPattern);
114195 if( c==0 ) return SQLITE_NOWILDCARDMATCH2;
114196 }else{
114197 /* "[...]" immediately follows the "*". We have to do a slow
114198 ** recursive search in this case, but it is an unusual case. */
114199 assert( matchOther<0x80 )((void) (0)); /* '[' is a single-byte character */
114200 while( *zString ){
114201 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
114202 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
114203 SQLITE_SKIP_UTF8(zString){ if( (*(zString++))>=0xc0 ){ while( (*zString & 0xc0)
==0x80 ){ zString++; } } }
;
114204 }
114205 return SQLITE_NOWILDCARDMATCH2;
114206 }
114207 }
114208
114209 /* At this point variable c contains the first character of the
114210 ** pattern string past the "*". Search in the input string for the
114211 ** first matching character and recursively continue the match from
114212 ** that point.
114213 **
114214 ** For a case-insensitive search, set variable cx to be the same as
114215 ** c but in the other case and search the input string for either
114216 ** c or cx.
114217 */
114218 if( c<=0x80 ){
114219 char zStop[3];
114220 int bMatch;
114221 if( noCase ){
114222 zStop[0] = sqlite3Toupper(c)((c)&~(sqlite3CtypeMap[(unsigned char)(c)]&0x20));
114223 zStop[1] = sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)]);
114224 zStop[2] = 0;
114225 }else{
114226 zStop[0] = c;
114227 zStop[1] = 0;
114228 }
114229 while(1){
114230 zString += strcspn((const char*)zString, zStop);
114231 if( zString[0]==0 ) break;
114232 zString++;
114233 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
114234 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
114235 }
114236 }else{
114237 int bMatch;
114238 while( (c2 = Utf8Read(zString)(zString[0]<0x80?*(zString++):sqlite3Utf8Read(&zString
))
)!=0 ){
114239 if( c2!=c ) continue;
114240 bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
114241 if( bMatch!=SQLITE_NOMATCH1 ) return bMatch;
114242 }
114243 }
114244 return SQLITE_NOWILDCARDMATCH2;
114245 }
114246 if( c==matchOther ){
114247 if( pInfo->matchSet==0 ){
114248 c = sqlite3Utf8Read(&zPattern);
114249 if( c==0 ) return SQLITE_NOMATCH1;
114250 zEscaped = zPattern;
114251 }else{
114252 u32 prior_c = 0;
114253 int seen = 0;
114254 int invert = 0;
114255 c = sqlite3Utf8Read(&zString);
114256 if( c==0 ) return SQLITE_NOMATCH1;
114257 c2 = sqlite3Utf8Read(&zPattern);
114258 if( c2=='^' ){
114259 invert = 1;
114260 c2 = sqlite3Utf8Read(&zPattern);
114261 }
114262 if( c2==']' ){
114263 if( c==']' ) seen = 1;
114264 c2 = sqlite3Utf8Read(&zPattern);
114265 }
114266 while( c2 && c2!=']' ){
114267 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
114268 c2 = sqlite3Utf8Read(&zPattern);
114269 if( c>=prior_c && c<=c2 ) seen = 1;
114270 prior_c = 0;
114271 }else{
114272 if( c==c2 ){
114273 seen = 1;
114274 }
114275 prior_c = c2;
114276 }
114277 c2 = sqlite3Utf8Read(&zPattern);
114278 }
114279 if( c2==0 || (seen ^ invert)==0 ){
114280 return SQLITE_NOMATCH1;
114281 }
114282 continue;
114283 }
114284 }
114285 c2 = Utf8Read(zString)(zString[0]<0x80?*(zString++):sqlite3Utf8Read(&zString
))
;
114286 if( c==c2 ) continue;
114287 if( noCase && sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)])==sqlite3Tolower(c2)(sqlite3UpperToLower[(unsigned char)(c2)]) && c<0x80 && c2<0x80 ){
114288 continue;
114289 }
114290 if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
114291 return SQLITE_NOMATCH1;
114292 }
114293 return *zString==0 ? SQLITE_MATCH0 : SQLITE_NOMATCH1;
114294}
114295
114296/*
114297** The sqlite3_strglob() interface. Return 0 on a match (like strcmp()) and
114298** non-zero if there is no match.
114299*/
114300SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
114301 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
114302}
114303
114304/*
114305** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
114306** a miss - like strcmp().
114307*/
114308SQLITE_API int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
114309 return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
114310}
114311
114312/*
114313** Count the number of times that the LIKE operator (or GLOB which is
114314** just a variation of LIKE) gets called. This is used for testing
114315** only.
114316*/
114317#ifdef SQLITE_TEST
114318SQLITE_API int sqlite3_like_count = 0;
114319#endif
114320
114321
114322/*
114323** Implementation of the like() SQL function. This function implements
114324** the build-in LIKE operator. The first argument to the function is the
114325** pattern and the second argument is the string. So, the SQL statements:
114326**
114327** A LIKE B
114328**
114329** is implemented as like(B,A).
114330**
114331** This same function (with a different compareInfo structure) computes
114332** the GLOB operator.
114333*/
114334static void likeFunc(
114335 sqlite3_context *context,
114336 int argc,
114337 sqlite3_value **argv
114338){
114339 const unsigned char *zA, *zB;
114340 u32 escape;
114341 int nPat;
114342 sqlite3 *db = sqlite3_context_db_handle(context);
114343 struct compareInfo *pInfo = sqlite3_user_data(context);
114344
114345#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
114346 if( sqlite3_value_type(argv[0])==SQLITE_BLOB4
114347 || sqlite3_value_type(argv[1])==SQLITE_BLOB4
114348 ){
114349#ifdef SQLITE_TEST
114350 sqlite3_like_count++;
114351#endif
114352 sqlite3_result_int(context, 0);
114353 return;
114354 }
114355#endif
114356
114357 /* Limit the length of the LIKE or GLOB pattern to avoid problems
114358 ** of deep recursion and N*N behavior in patternCompare().
114359 */
114360 nPat = sqlite3_value_bytes(argv[0]);
114361 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
114362 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
114363 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH8] ){
114364 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
114365 return;
114366 }
114367 if( argc==3 ){
114368 /* The escape character string must consist of a single UTF-8 character.
114369 ** Otherwise, return an error.
114370 */
114371 const unsigned char *zEsc = sqlite3_value_text(argv[2]);
114372 if( zEsc==0 ) return;
114373 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
114374 sqlite3_result_error(context,
114375 "ESCAPE expression must be a single character", -1);
114376 return;
114377 }
114378 escape = sqlite3Utf8Read(&zEsc);
114379 }else{
114380 escape = pInfo->matchSet;
114381 }
114382 zB = sqlite3_value_text(argv[0]);
114383 zA = sqlite3_value_text(argv[1]);
114384 if( zA && zB ){
114385#ifdef SQLITE_TEST
114386 sqlite3_like_count++;
114387#endif
114388 sqlite3_result_int(context,
114389 patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH0);
114390 }
114391}
114392
114393/*
114394** Implementation of the NULLIF(x,y) function. The result is the first
114395** argument if the arguments are different. The result is NULL if the
114396** arguments are equal to each other.
114397*/
114398static void nullifFunc(
114399 sqlite3_context *context,
114400 int NotUsed,
114401 sqlite3_value **argv
114402){
114403 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
114404 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
114405 if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
114406 sqlite3_result_value(context, argv[0]);
114407 }
114408}
114409
114410/*
114411** Implementation of the sqlite_version() function. The result is the version
114412** of the SQLite library that is running.
114413*/
114414static void versionFunc(
114415 sqlite3_context *context,
114416 int NotUsed,
114417 sqlite3_value **NotUsed2
114418){
114419 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
114420 /* IMP: R-48699-48617 This function is an SQL wrapper around the
114421 ** sqlite3_libversion() C-interface. */
114422 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
114423}
114424
114425/*
114426** Implementation of the sqlite_source_id() function. The result is a string
114427** that identifies the particular version of the source code used to build
114428** SQLite.
114429*/
114430static void sourceidFunc(
114431 sqlite3_context *context,
114432 int NotUsed,
114433 sqlite3_value **NotUsed2
114434){
114435 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
114436 /* IMP: R-24470-31136 This function is an SQL wrapper around the
114437 ** sqlite3_sourceid() C interface. */
114438 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
114439}
114440
114441/*
114442** Implementation of the sqlite_log() function. This is a wrapper around
114443** sqlite3_log(). The return value is NULL. The function exists purely for
114444** its side-effects.
114445*/
114446static void errlogFunc(
114447 sqlite3_context *context,
114448 int argc,
114449 sqlite3_value **argv
114450){
114451 UNUSED_PARAMETER(argc)(void)(argc);
114452 UNUSED_PARAMETER(context)(void)(context);
114453 sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
114454}
114455
114456/*
114457** Implementation of the sqlite_compileoption_used() function.
114458** The result is an integer that identifies if the compiler option
114459** was used to build SQLite.
114460*/
114461#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
114462static void compileoptionusedFunc(
114463 sqlite3_context *context,
114464 int argc,
114465 sqlite3_value **argv
114466){
114467 const char *zOptName;
114468 assert( argc==1 )((void) (0));
114469 UNUSED_PARAMETER(argc)(void)(argc);
114470 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
114471 ** function is a wrapper around the sqlite3_compileoption_used() C/C++
114472 ** function.
114473 */
114474 if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
114475 sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
114476 }
114477}
114478#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
114479
114480/*
114481** Implementation of the sqlite_compileoption_get() function.
114482** The result is a string that identifies the compiler options
114483** used to build SQLite.
114484*/
114485#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
114486static void compileoptiongetFunc(
114487 sqlite3_context *context,
114488 int argc,
114489 sqlite3_value **argv
114490){
114491 int n;
114492 assert( argc==1 )((void) (0));
114493 UNUSED_PARAMETER(argc)(void)(argc);
114494 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
114495 ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
114496 */
114497 n = sqlite3_value_int(argv[0]);
114498 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC((sqlite3_destructor_type)0));
114499}
114500#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
114501
114502/* Array for converting from half-bytes (nybbles) into ASCII hex
114503** digits. */
114504static const char hexdigits[] = {
114505 '0', '1', '2', '3', '4', '5', '6', '7',
114506 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
114507};
114508
114509/*
114510** Implementation of the QUOTE() function. This function takes a single
114511** argument. If the argument is numeric, the return value is the same as
114512** the argument. If the argument is NULL, the return value is the string
114513** "NULL". Otherwise, the argument is enclosed in single quotes with
114514** single-quote escapes.
114515*/
114516static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
114517 assert( argc==1 )((void) (0));
114518 UNUSED_PARAMETER(argc)(void)(argc);
114519 switch( sqlite3_value_type(argv[0]) ){
114520 case SQLITE_FLOAT2: {
114521 double r1, r2;
114522 char zBuf[50];
114523 r1 = sqlite3_value_double(argv[0]);
114524 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
114525 sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF81);
114526 if( r1!=r2 ){
114527 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
114528 }
114529 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
114530 break;
114531 }
114532 case SQLITE_INTEGER1: {
114533 sqlite3_result_value(context, argv[0]);
114534 break;
114535 }
114536 case SQLITE_BLOB4: {
114537 char *zText = 0;
114538 char const *zBlob = sqlite3_value_blob(argv[0]);
114539 int nBlob = sqlite3_value_bytes(argv[0]);
114540 assert( zBlob==sqlite3_value_blob(argv[0]) )((void) (0)); /* No encoding change */
114541 zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
114542 if( zText ){
114543 int i;
114544 for(i=0; i<nBlob; i++){
114545 zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
114546 zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
114547 }
114548 zText[(nBlob*2)+2] = '\'';
114549 zText[(nBlob*2)+3] = '\0';
114550 zText[0] = 'X';
114551 zText[1] = '\'';
114552 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
114553 sqlite3_free(zText);
114554 }
114555 break;
114556 }
114557 case SQLITE_TEXT3: {
114558 int i,j;
114559 u64 n;
114560 const unsigned char *zArg = sqlite3_value_text(argv[0]);
114561 char *z;
114562
114563 if( zArg==0 ) return;
114564 for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
114565 z = contextMalloc(context, ((i64)i)+((i64)n)+3);
114566 if( z ){
114567 z[0] = '\'';
114568 for(i=0, j=1; zArg[i]; i++){
114569 z[j++] = zArg[i];
114570 if( zArg[i]=='\'' ){
114571 z[j++] = '\'';
114572 }
114573 }
114574 z[j++] = '\'';
114575 z[j] = 0;
114576 sqlite3_result_text(context, z, j, sqlite3_free);
114577 }
114578 break;
114579 }
114580 default: {
114581 assert( sqlite3_value_type(argv[0])==SQLITE_NULL )((void) (0));
114582 sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC((sqlite3_destructor_type)0));
114583 break;
114584 }
114585 }
114586}
114587
114588/*
114589** The unicode() function. Return the integer unicode code-point value
114590** for the first character of the input string.
114591*/
114592static void unicodeFunc(
114593 sqlite3_context *context,
114594 int argc,
114595 sqlite3_value **argv
114596){
114597 const unsigned char *z = sqlite3_value_text(argv[0]);
114598 (void)argc;
114599 if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
114600}
114601
114602/*
114603** The char() function takes zero or more arguments, each of which is
114604** an integer. It constructs a string where each character of the string
114605** is the unicode character for the corresponding integer argument.
114606*/
114607static void charFunc(
114608 sqlite3_context *context,
114609 int argc,
114610 sqlite3_value **argv
114611){
114612 unsigned char *z, *zOut;
114613 int i;
114614 zOut = z = sqlite3_malloc64( argc*4+1 );
114615 if( z==0 ){
114616 sqlite3_result_error_nomem(context);
114617 return;
114618 }
114619 for(i=0; i<argc; i++){
114620 sqlite3_int64 x;
114621 unsigned c;
114622 x = sqlite3_value_int64(argv[i]);
114623 if( x<0 || x>0x10ffff ) x = 0xfffd;
114624 c = (unsigned)(x & 0x1fffff);
114625 if( c<0x00080 ){
114626 *zOut++ = (u8)(c&0xFF);
114627 }else if( c<0x00800 ){
114628 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
114629 *zOut++ = 0x80 + (u8)(c & 0x3F);
114630 }else if( c<0x10000 ){
114631 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
114632 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
114633 *zOut++ = 0x80 + (u8)(c & 0x3F);
114634 }else{
114635 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
114636 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
114637 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
114638 *zOut++ = 0x80 + (u8)(c & 0x3F);
114639 } \
114640 }
114641 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF81);
114642}
114643
114644/*
114645** The hex() function. Interpret the argument as a blob. Return
114646** a hexadecimal rendering as text.
114647*/
114648static void hexFunc(
114649 sqlite3_context *context,
114650 int argc,
114651 sqlite3_value **argv
114652){
114653 int i, n;
114654 const unsigned char *pBlob;
114655 char *zHex, *z;
114656 assert( argc==1 )((void) (0));
114657 UNUSED_PARAMETER(argc)(void)(argc);
114658 pBlob = sqlite3_value_blob(argv[0]);
114659 n = sqlite3_value_bytes(argv[0]);
114660 assert( pBlob==sqlite3_value_blob(argv[0]) )((void) (0)); /* No encoding change */
114661 z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
114662 if( zHex ){
114663 for(i=0; i<n; i++, pBlob++){
114664 unsigned char c = *pBlob;
114665 *(z++) = hexdigits[(c>>4)&0xf];
114666 *(z++) = hexdigits[c&0xf];
114667 }
114668 *z = 0;
114669 sqlite3_result_text(context, zHex, n*2, sqlite3_free);
114670 }
114671}
114672
114673/*
114674** The zeroblob(N) function returns a zero-filled blob of size N bytes.
114675*/
114676static void zeroblobFunc(
114677 sqlite3_context *context,
114678 int argc,
114679 sqlite3_value **argv
114680){
114681 i64 n;
114682 int rc;
114683 assert( argc==1 )((void) (0));
114684 UNUSED_PARAMETER(argc)(void)(argc);
114685 n = sqlite3_value_int64(argv[0]);
114686 if( n<0 ) n = 0;
114687 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
114688 if( rc ){
114689 sqlite3_result_error_code(context, rc);
114690 }
114691}
114692
114693/*
114694** The replace() function. Three arguments are all strings: call
114695** them A, B, and C. The result is also a string which is derived
114696** from A by replacing every occurrence of B with C. The match
114697** must be exact. Collating sequences are not used.
114698*/
114699static void replaceFunc(
114700 sqlite3_context *context,
114701 int argc,
114702 sqlite3_value **argv
114703){
114704 const unsigned char *zStr; /* The input string A */
114705 const unsigned char *zPattern; /* The pattern string B */
114706 const unsigned char *zRep; /* The replacement string C */
114707 unsigned char *zOut; /* The output */
114708 int nStr; /* Size of zStr */
114709 int nPattern; /* Size of zPattern */
114710 int nRep; /* Size of zRep */
114711 i64 nOut; /* Maximum size of zOut */
114712 int loopLimit; /* Last zStr[] that might match zPattern[] */
114713 int i, j; /* Loop counters */
114714 unsigned cntExpand; /* Number zOut expansions */
114715 sqlite3 *db = sqlite3_context_db_handle(context);
114716
114717 assert( argc==3 )((void) (0));
114718 UNUSED_PARAMETER(argc)(void)(argc);
114719 zStr = sqlite3_value_text(argv[0]);
114720 if( zStr==0 ) return;
114721 nStr = sqlite3_value_bytes(argv[0]);
114722 assert( zStr==sqlite3_value_text(argv[0]) )((void) (0)); /* No encoding change */
114723 zPattern = sqlite3_value_text(argv[1]);
114724 if( zPattern==0 ){
114725 assert( sqlite3_value_type(argv[1])==SQLITE_NULL((void) (0))
114726 || sqlite3_context_db_handle(context)->mallocFailed )((void) (0));
114727 return;
114728 }
114729 if( zPattern[0]==0 ){
114730 assert( sqlite3_value_type(argv[1])!=SQLITE_NULL )((void) (0));
114731 sqlite3_result_value(context, argv[0]);
114732 return;
114733 }
114734 nPattern = sqlite3_value_bytes(argv[1]);
114735 assert( zPattern==sqlite3_value_text(argv[1]) )((void) (0)); /* No encoding change */
114736 zRep = sqlite3_value_text(argv[2]);
114737 if( zRep==0 ) return;
114738 nRep = sqlite3_value_bytes(argv[2]);
114739 assert( zRep==sqlite3_value_text(argv[2]) )((void) (0));
114740 nOut = nStr + 1;
114741 assert( nOut<SQLITE_MAX_LENGTH )((void) (0));
114742 zOut = contextMalloc(context, (i64)nOut);
114743 if( zOut==0 ){
114744 return;
114745 }
114746 loopLimit = nStr - nPattern;
114747 cntExpand = 0;
114748 for(i=j=0; i<=loopLimit; i++){
114749 if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
114750 zOut[j++] = zStr[i];
114751 }else{
114752 if( nRep>nPattern ){
114753 nOut += nRep - nPattern;
114754 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
114755 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
114756 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH0] ){
114757 sqlite3_result_error_toobig(context);
114758 sqlite3_free(zOut);
114759 return;
114760 }
114761 cntExpand++;
114762 if( (cntExpand&(cntExpand-1))==0 ){
114763 /* Grow the size of the output buffer only on substitutions
114764 ** whose index is a power of two: 1, 2, 4, 8, 16, 32, ... */
114765 u8 *zOld;
114766 zOld = zOut;
114767 zOut = sqlite3_realloc64(zOut, (int)nOut + (nOut - nStr - 1));
114768 if( zOut==0 ){
114769 sqlite3_result_error_nomem(context);
114770 sqlite3_free(zOld);
114771 return;
114772 }
114773 }
114774 }
114775 memcpy(&zOut[j], zRep, nRep);
114776 j += nRep;
114777 i += nPattern-1;
114778 }
114779 }
114780 assert( j+nStr-i+1<=nOut )((void) (0));
114781 memcpy(&zOut[j], &zStr[i], nStr-i);
114782 j += nStr - i;
114783 assert( j<=nOut )((void) (0));
114784 zOut[j] = 0;
114785 sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
114786}
114787
114788/*
114789** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
114790** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
114791*/
114792static void trimFunc(
114793 sqlite3_context *context,
114794 int argc,
114795 sqlite3_value **argv
114796){
114797 const unsigned char *zIn; /* Input string */
114798 const unsigned char *zCharSet; /* Set of characters to trim */
114799 int nIn; /* Number of bytes in input */
114800 int flags; /* 1: trimleft 2: trimright 3: trim */
114801 int i; /* Loop counter */
114802 unsigned char *aLen = 0; /* Length of each character in zCharSet */
114803 unsigned char **azChar = 0; /* Individual characters in zCharSet */
114804 int nChar; /* Number of characters in zCharSet */
114805
114806 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ){
114807 return;
114808 }
114809 zIn = sqlite3_value_text(argv[0]);
114810 if( zIn==0 ) return;
114811 nIn = sqlite3_value_bytes(argv[0]);
114812 assert( zIn==sqlite3_value_text(argv[0]) )((void) (0));
114813 if( argc==1 ){
114814 static const unsigned char lenOne[] = { 1 };
114815 static unsigned char * const azOne[] = { (u8*)" " };
114816 nChar = 1;
114817 aLen = (u8*)lenOne;
114818 azChar = (unsigned char **)azOne;
114819 zCharSet = 0;
114820 }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
114821 return;
114822 }else{
114823 const unsigned char *z;
114824 for(z=zCharSet, nChar=0; *z; nChar++){
114825 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
114826 }
114827 if( nChar>0 ){
114828 azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
114829 if( azChar==0 ){
114830 return;
114831 }
114832 aLen = (unsigned char*)&azChar[nChar];
114833 for(z=zCharSet, nChar=0; *z; nChar++){
114834 azChar[nChar] = (unsigned char *)z;
114835 SQLITE_SKIP_UTF8(z){ if( (*(z++))>=0xc0 ){ while( (*z & 0xc0)==0x80 ){ z++
; } } }
;
114836 aLen[nChar] = (u8)(z - azChar[nChar]);
114837 }
114838 }
114839 }
114840 if( nChar>0 ){
114841 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context))((int)(long int)(sqlite3_user_data(context)));
114842 if( flags & 1 ){
114843 while( nIn>0 ){
114844 int len = 0;
114845 for(i=0; i<nChar; i++){
114846 len = aLen[i];
114847 if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
114848 }
114849 if( i>=nChar ) break;
114850 zIn += len;
114851 nIn -= len;
114852 }
114853 }
114854 if( flags & 2 ){
114855 while( nIn>0 ){
114856 int len = 0;
114857 for(i=0; i<nChar; i++){
114858 len = aLen[i];
114859 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
114860 }
114861 if( i>=nChar ) break;
114862 nIn -= len;
114863 }
114864 }
114865 if( zCharSet ){
114866 sqlite3_free(azChar);
114867 }
114868 }
114869 sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
114870}
114871
114872
114873#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
114874/*
114875** The "unknown" function is automatically substituted in place of
114876** any unrecognized function name when doing an EXPLAIN or EXPLAIN QUERY PLAN
114877** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
114878** When the "sqlite3" command-line shell is built using this functionality,
114879** that allows an EXPLAIN or EXPLAIN QUERY PLAN for complex queries
114880** involving application-defined functions to be examined in a generic
114881** sqlite3 shell.
114882*/
114883static void unknownFunc(
114884 sqlite3_context *context,
114885 int argc,
114886 sqlite3_value **argv
114887){
114888 /* no-op */
114889}
114890#endif /*SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION*/
114891
114892
114893/* IMP: R-25361-16150 This function is omitted from SQLite by default. It
114894** is only available if the SQLITE_SOUNDEX compile-time option is used
114895** when SQLite is built.
114896*/
114897#ifdef SQLITE_SOUNDEX
114898/*
114899** Compute the soundex encoding of a word.
114900**
114901** IMP: R-59782-00072 The soundex(X) function returns a string that is the
114902** soundex encoding of the string X.
114903*/
114904static void soundexFunc(
114905 sqlite3_context *context,
114906 int argc,
114907 sqlite3_value **argv
114908){
114909 char zResult[8];
114910 const u8 *zIn;
114911 int i, j;
114912 static const unsigned char iCode[] = {
114913 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114914 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114915 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114916 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114917 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
114918 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
114919 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
114920 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
114921 };
114922 assert( argc==1 )((void) (0));
114923 zIn = (u8*)sqlite3_value_text(argv[0]);
114924 if( zIn==0 ) zIn = (u8*)"";
114925 for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i])(sqlite3CtypeMap[(unsigned char)(zIn[i])]&0x02); i++){}
114926 if( zIn[i] ){
114927 u8 prevcode = iCode[zIn[i]&0x7f];
114928 zResult[0] = sqlite3Toupper(zIn[i])((zIn[i])&~(sqlite3CtypeMap[(unsigned char)(zIn[i])]&
0x20))
;
114929 for(j=1; j<4 && zIn[i]; i++){
114930 int code = iCode[zIn[i]&0x7f];
114931 if( code>0 ){
114932 if( code!=prevcode ){
114933 prevcode = code;
114934 zResult[j++] = code + '0';
114935 }
114936 }else{
114937 prevcode = 0;
114938 }
114939 }
114940 while( j<4 ){
114941 zResult[j++] = '0';
114942 }
114943 zResult[j] = 0;
114944 sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
114945 }else{
114946 /* IMP: R-64894-50321 The string "?000" is returned if the argument
114947 ** is NULL or contains no ASCII alphabetic characters. */
114948 sqlite3_result_text(context, "?000", 4, SQLITE_STATIC((sqlite3_destructor_type)0));
114949 }
114950}
114951#endif /* SQLITE_SOUNDEX */
114952
114953#ifndef SQLITE_OMIT_LOAD_EXTENSION
114954/*
114955** A function that loads a shared-library extension then returns NULL.
114956*/
114957static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
114958 const char *zFile = (const char *)sqlite3_value_text(argv[0]);
114959 const char *zProc;
114960 sqlite3 *db = sqlite3_context_db_handle(context);
114961 char *zErrMsg = 0;
114962
114963 /* Disallow the load_extension() SQL function unless the SQLITE_LoadExtFunc
114964 ** flag is set. See the sqlite3_enable_load_extension() API.
114965 */
114966 if( (db->flags & SQLITE_LoadExtFunc0x00020000)==0 ){
114967 sqlite3_result_error(context, "not authorized", -1);
114968 return;
114969 }
114970
114971 if( argc==2 ){
114972 zProc = (const char *)sqlite3_value_text(argv[1]);
114973 }else{
114974 zProc = 0;
114975 }
114976 if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
114977 sqlite3_result_error(context, zErrMsg, -1);
114978 sqlite3_free(zErrMsg);
114979 }
114980}
114981#endif
114982
114983
114984/*
114985** An instance of the following structure holds the context of a
114986** sum() or avg() aggregate computation.
114987*/
114988typedef struct SumCtx SumCtx;
114989struct SumCtx {
114990 double rSum; /* Floating point sum */
114991 i64 iSum; /* Integer sum */
114992 i64 cnt; /* Number of elements summed */
114993 u8 overflow; /* True if integer overflow seen */
114994 u8 approx; /* True if non-integer value was input to the sum */
114995};
114996
114997/*
114998** Routines used to compute the sum, average, and total.
114999**
115000** The SUM() function follows the (broken) SQL standard which means
115001** that it returns NULL if it sums over no inputs. TOTAL returns
115002** 0.0 in that case. In addition, TOTAL always returns a float where
115003** SUM might return an integer if it never encounters a floating point
115004** value. TOTAL never fails, but SUM might through an exception if
115005** it overflows an integer.
115006*/
115007static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
115008 SumCtx *p;
115009 int type;
115010 assert( argc==1 )((void) (0));
115011 UNUSED_PARAMETER(argc)(void)(argc);
115012 p = sqlite3_aggregate_context(context, sizeof(*p));
115013 type = sqlite3_value_numeric_type(argv[0]);
115014 if( p && type!=SQLITE_NULL5 ){
115015 p->cnt++;
115016 if( type==SQLITE_INTEGER1 ){
115017 i64 v = sqlite3_value_int64(argv[0]);
115018 p->rSum += v;
115019 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
115020 p->approx = p->overflow = 1;
115021 }
115022 }else{
115023 p->rSum += sqlite3_value_double(argv[0]);
115024 p->approx = 1;
115025 }
115026 }
115027}
115028#ifndef SQLITE_OMIT_WINDOWFUNC
115029static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
115030 SumCtx *p;
115031 int type;
115032 assert( argc==1 )((void) (0));
115033 UNUSED_PARAMETER(argc)(void)(argc);
115034 p = sqlite3_aggregate_context(context, sizeof(*p));
115035 type = sqlite3_value_numeric_type(argv[0]);
115036 /* p is always non-NULL because sumStep() will have been called first
115037 ** to initialize it */
115038 if( ALWAYS(p)(p) && type!=SQLITE_NULL5 ){
115039 assert( p->cnt>0 )((void) (0));
115040 p->cnt--;
115041 assert( type==SQLITE_INTEGER || p->approx )((void) (0));
115042 if( type==SQLITE_INTEGER1 && p->approx==0 ){
115043 i64 v = sqlite3_value_int64(argv[0]);
115044 p->rSum -= v;
115045 p->iSum -= v;
115046 }else{
115047 p->rSum -= sqlite3_value_double(argv[0]);
115048 }
115049 }
115050}
115051#else
115052# define sumInverse 0
115053#endif /* SQLITE_OMIT_WINDOWFUNC */
115054static void sumFinalize(sqlite3_context *context){
115055 SumCtx *p;
115056 p = sqlite3_aggregate_context(context, 0);
115057 if( p && p->cnt>0 ){
115058 if( p->overflow ){
115059 sqlite3_result_error(context,"integer overflow",-1);
115060 }else if( p->approx ){
115061 sqlite3_result_double(context, p->rSum);
115062 }else{
115063 sqlite3_result_int64(context, p->iSum);
115064 }
115065 }
115066}
115067static void avgFinalize(sqlite3_context *context){
115068 SumCtx *p;
115069 p = sqlite3_aggregate_context(context, 0);
115070 if( p && p->cnt>0 ){
115071 sqlite3_result_double(context, p->rSum/(double)p->cnt);
115072 }
115073}
115074static void totalFinalize(sqlite3_context *context){
115075 SumCtx *p;
115076 p = sqlite3_aggregate_context(context, 0);
115077 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
115078 sqlite3_result_double(context, p ? p->rSum : (double)0);
115079}
115080
115081/*
115082** The following structure keeps track of state information for the
115083** count() aggregate function.
115084*/
115085typedef struct CountCtx CountCtx;
115086struct CountCtx {
115087 i64 n;
115088#ifdef SQLITE_DEBUG
115089 int bInverse; /* True if xInverse() ever called */
115090#endif
115091};
115092
115093/*
115094** Routines to implement the count() aggregate function.
115095*/
115096static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
115097 CountCtx *p;
115098 p = sqlite3_aggregate_context(context, sizeof(*p));
115099 if( (argc==0 || SQLITE_NULL5!=sqlite3_value_type(argv[0])) && p ){
115100 p->n++;
115101 }
115102
115103#ifndef SQLITE_OMIT_DEPRECATED
115104 /* The sqlite3_aggregate_count() function is deprecated. But just to make
115105 ** sure it still operates correctly, verify that its count agrees with our
115106 ** internal count when using count(*) and when the total count can be
115107 ** expressed as a 32-bit integer. */
115108 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse((void) (0))
115109 || p->n==sqlite3_aggregate_count(context) )((void) (0));
115110#endif
115111}
115112static void countFinalize(sqlite3_context *context){
115113 CountCtx *p;
115114 p = sqlite3_aggregate_context(context, 0);
115115 sqlite3_result_int64(context, p ? p->n : 0);
115116}
115117#ifndef SQLITE_OMIT_WINDOWFUNC
115118static void countInverse(sqlite3_context *ctx, int argc, sqlite3_value **argv){
115119 CountCtx *p;
115120 p = sqlite3_aggregate_context(ctx, sizeof(*p));
115121 /* p is always non-NULL since countStep() will have been called first */
115122 if( (argc==0 || SQLITE_NULL5!=sqlite3_value_type(argv[0])) && ALWAYS(p)(p) ){
115123 p->n--;
115124#ifdef SQLITE_DEBUG
115125 p->bInverse = 1;
115126#endif
115127 }
115128}
115129#else
115130# define countInverse 0
115131#endif /* SQLITE_OMIT_WINDOWFUNC */
115132
115133/*
115134** Routines to implement min() and max() aggregate functions.
115135*/
115136static void minmaxStep(
115137 sqlite3_context *context,
115138 int NotUsed,
115139 sqlite3_value **argv
115140){
115141 Mem *pArg = (Mem *)argv[0];
115142 Mem *pBest;
115143 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
115144
115145 pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
115146 if( !pBest ) return;
115147
115148 if( sqlite3_value_type(pArg)==SQLITE_NULL5 ){
115149 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
115150 }else if( pBest->flags ){
115151 int max;
115152 int cmp;
115153 CollSeq *pColl = sqlite3GetFuncCollSeq(context);
115154 /* This step function is used for both the min() and max() aggregates,
115155 ** the only difference between the two being that the sense of the
115156 ** comparison is inverted. For the max() aggregate, the
115157 ** sqlite3_user_data() function returns (void *)-1. For min() it
115158 ** returns (void *)db, where db is the sqlite3* database pointer.
115159 ** Therefore the next statement sets variable 'max' to 1 for the max()
115160 ** aggregate, or 0 for min().
115161 */
115162 max = sqlite3_user_data(context)!=0;
115163 cmp = sqlite3MemCompare(pBest, pArg, pColl);
115164 if( (max && cmp<0) || (!max && cmp>0) ){
115165 sqlite3VdbeMemCopy(pBest, pArg);
115166 }else{
115167 sqlite3SkipAccumulatorLoad(context);
115168 }
115169 }else{
115170 pBest->db = sqlite3_context_db_handle(context);
115171 sqlite3VdbeMemCopy(pBest, pArg);
115172 }
115173}
115174static void minMaxValueFinalize(sqlite3_context *context, int bValue){
115175 sqlite3_value *pRes;
115176 pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
115177 if( pRes ){
115178 if( pRes->flags ){
115179 sqlite3_result_value(context, pRes);
115180 }
115181 if( bValue==0 ) sqlite3VdbeMemRelease(pRes);
115182 }
115183}
115184#ifndef SQLITE_OMIT_WINDOWFUNC
115185static void minMaxValue(sqlite3_context *context){
115186 minMaxValueFinalize(context, 1);
115187}
115188#else
115189# define minMaxValue 0
115190#endif /* SQLITE_OMIT_WINDOWFUNC */
115191static void minMaxFinalize(sqlite3_context *context){
115192 minMaxValueFinalize(context, 0);
115193}
115194
115195/*
115196** group_concat(EXPR, ?SEPARATOR?)
115197*/
115198static void groupConcatStep(
115199 sqlite3_context *context,
115200 int argc,
115201 sqlite3_value **argv
115202){
115203 const char *zVal;
115204 StrAccum *pAccum;
115205 const char *zSep;
115206 int nVal, nSep;
115207 assert( argc==1 || argc==2 )((void) (0));
115208 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
115209 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
115210
115211 if( pAccum ){
115212 sqlite3 *db = sqlite3_context_db_handle(context);
115213 int firstTerm = pAccum->mxAlloc==0;
115214 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH0];
115215 if( !firstTerm ){
115216 if( argc==2 ){
115217 zSep = (char*)sqlite3_value_text(argv[1]);
115218 nSep = sqlite3_value_bytes(argv[1]);
115219 }else{
115220 zSep = ",";
115221 nSep = 1;
115222 }
115223 if( zSep ) sqlite3_str_append(pAccum, zSep, nSep);
115224 }
115225 zVal = (char*)sqlite3_value_text(argv[0]);
115226 nVal = sqlite3_value_bytes(argv[0]);
115227 if( zVal ) sqlite3_str_append(pAccum, zVal, nVal);
115228 }
115229}
115230#ifndef SQLITE_OMIT_WINDOWFUNC
115231static void groupConcatInverse(
115232 sqlite3_context *context,
115233 int argc,
115234 sqlite3_value **argv
115235){
115236 int n;
115237 StrAccum *pAccum;
115238 assert( argc==1 || argc==2 )((void) (0));
115239 if( sqlite3_value_type(argv[0])==SQLITE_NULL5 ) return;
115240 pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
115241 /* pAccum is always non-NULL since groupConcatStep() will have always
115242 ** run frist to initialize it */
115243 if( ALWAYS(pAccum)(pAccum) ){
115244 n = sqlite3_value_bytes(argv[0]);
115245 if( argc==2 ){
115246 n += sqlite3_value_bytes(argv[1]);
115247 }else{
115248 n++;
115249 }
115250 if( n>=(int)pAccum->nChar ){
115251 pAccum->nChar = 0;
115252 }else{
115253 pAccum->nChar -= n;
115254 memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar);
115255 }
115256 if( pAccum->nChar==0 ) pAccum->mxAlloc = 0;
115257 }
115258}
115259#else
115260# define groupConcatInverse 0
115261#endif /* SQLITE_OMIT_WINDOWFUNC */
115262static void groupConcatFinalize(sqlite3_context *context){
115263 StrAccum *pAccum;
115264 pAccum = sqlite3_aggregate_context(context, 0);
115265 if( pAccum ){
115266 if( pAccum->accError==SQLITE_TOOBIG18 ){
115267 sqlite3_result_error_toobig(context);
115268 }else if( pAccum->accError==SQLITE_NOMEM7 ){
115269 sqlite3_result_error_nomem(context);
115270 }else{
115271 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
115272 sqlite3_free);
115273 }
115274 }
115275}
115276#ifndef SQLITE_OMIT_WINDOWFUNC
115277static void groupConcatValue(sqlite3_context *context){
115278 sqlite3_str *pAccum;
115279 pAccum = (sqlite3_str*)sqlite3_aggregate_context(context, 0);
115280 if( pAccum ){
115281 if( pAccum->accError==SQLITE_TOOBIG18 ){
115282 sqlite3_result_error_toobig(context);
115283 }else if( pAccum->accError==SQLITE_NOMEM7 ){
115284 sqlite3_result_error_nomem(context);
115285 }else{
115286 const char *zText = sqlite3_str_value(pAccum);
115287 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
115288 }
115289 }
115290}
115291#else
115292# define groupConcatValue 0
115293#endif /* SQLITE_OMIT_WINDOWFUNC */
115294
115295/*
115296** This routine does per-connection function registration. Most
115297** of the built-in functions above are part of the global function set.
115298** This routine only deals with those that are not global.
115299*/
115300SQLITE_PRIVATEstatic void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3 *db){
115301 int rc = sqlite3_overload_function(db, "MATCH", 2);
115302 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK )((void) (0));
115303 if( rc==SQLITE_NOMEM7 ){
115304 sqlite3OomFault(db);
115305 }
115306}
115307
115308/*
115309** Re-register the built-in LIKE functions. The caseSensitive
115310** parameter determines whether or not the LIKE operator is case
115311** sensitive.
115312*/
115313SQLITE_PRIVATEstatic void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
115314 struct compareInfo *pInfo;
115315 int flags;
115316 if( caseSensitive ){
115317 pInfo = (struct compareInfo*)&likeInfoAlt;
115318 flags = SQLITE_FUNC_LIKE0x0004 | SQLITE_FUNC_CASE0x0008;
115319 }else{
115320 pInfo = (struct compareInfo*)&likeInfoNorm;
115321 flags = SQLITE_FUNC_LIKE0x0004;
115322 }
115323 sqlite3CreateFunc(db, "like", 2, SQLITE_UTF81, pInfo, likeFunc, 0, 0, 0, 0, 0);
115324 sqlite3CreateFunc(db, "like", 3, SQLITE_UTF81, pInfo, likeFunc, 0, 0, 0, 0, 0);
115325 sqlite3FindFunction(db, "like", 2, SQLITE_UTF81, 0)->funcFlags |= flags;
115326 sqlite3FindFunction(db, "like", 3, SQLITE_UTF81, 0)->funcFlags |= flags;
115327}
115328
115329/*
115330** pExpr points to an expression which implements a function. If
115331** it is appropriate to apply the LIKE optimization to that function
115332** then set aWc[0] through aWc[2] to the wildcard characters and the
115333** escape character and then return TRUE. If the function is not a
115334** LIKE-style function then return FALSE.
115335**
115336** The expression "a LIKE b ESCAPE c" is only considered a valid LIKE
115337** operator if c is a string literal that is exactly one byte in length.
115338** That one byte is stored in aWc[3]. aWc[3] is set to zero if there is
115339** no ESCAPE clause.
115340**
115341** *pIsNocase is set to true if uppercase and lowercase are equivalent for
115342** the function (default for LIKE). If the function makes the distinction
115343** between uppercase and lowercase (as does GLOB) then *pIsNocase is set to
115344** false.
115345*/
115346SQLITE_PRIVATEstatic int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
115347 FuncDef *pDef;
115348 int nExpr;
115349 if( pExpr->op!=TK_FUNCTION161 || !pExpr->x.pList ){
115350 return 0;
115351 }
115352 assert( !ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
115353 nExpr = pExpr->x.pList->nExpr;
115354 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF81, 0);
115355 if( NEVER(pDef==0)(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE0x0004)==0 ){
115356 return 0;
115357 }
115358 if( nExpr<3 ){
115359 aWc[3] = 0;
115360 }else{
115361 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
115362 char *zEscape;
115363 if( pEscape->op!=TK_STRING110 ) return 0;
115364 zEscape = pEscape->u.zToken;
115365 if( zEscape[0]==0 || zEscape[1]!=0 ) return 0;
115366 aWc[3] = zEscape[0];
115367 }
115368
115369 /* The memcpy() statement assumes that the wildcard characters are
115370 ** the first three statements in the compareInfo structure. The
115371 ** asserts() that follow verify that assumption
115372 */
115373 memcpy(aWc, pDef->pUserData, 3);
115374 assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll )((void) (0));
115375 assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne )((void) (0));
115376 assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet )((void) (0));
115377 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE0x0008)==0;
115378 return 1;
115379}
115380
115381/*
115382** All of the FuncDef structures in the aBuiltinFunc[] array above
115383** to the global function hash table. This occurs at start-time (as
115384** a consequence of calling sqlite3_initialize()).
115385**
115386** After this routine runs
115387*/
115388SQLITE_PRIVATEstatic void sqlite3RegisterBuiltinFunctions(void){
115389 /*
115390 ** The following array holds FuncDef structures for all of the functions
115391 ** defined in this file.
115392 **
115393 ** The array cannot be constant since changes are made to the
115394 ** FuncDef.pHash elements at start-time. The elements of this array
115395 ** are read-only after initialization is complete.
115396 **
115397 ** For peak efficiency, put the most frequently used function last.
115398 */
115399 static FuncDef aBuiltinFunc[] = {
115400#ifdef SQLITE_SOUNDEX
115401 FUNCTION(soundex, 1, 0, 0, soundexFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, soundexFunc
, 0, 0, 0, "soundex", {0} }
,
115402#endif
115403#ifndef SQLITE_OMIT_LOAD_EXTENSION
115404 VFUNCTION(load_extension, 1, 0, 0, loadExt ){1, 1|(0*0x0020), ((void*)(long int)(0)), 0, loadExt, 0, 0, 0
, "load_extension", {0} }
,
115405 VFUNCTION(load_extension, 2, 0, 0, loadExt ){2, 1|(0*0x0020), ((void*)(long int)(0)), 0, loadExt, 0, 0, 0
, "load_extension", {0} }
,
115406#endif
115407#if SQLITE_USER_AUTHENTICATION
115408 FUNCTION(sqlite_crypt, 2, 0, 0, sqlite3CryptFunc ){2, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, sqlite3CryptFunc
, 0, 0, 0, "sqlite_crypt", {0} }
,
115409#endif
115410#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
115411 DFUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ){1, 0x2000|1, 0, 0, compileoptionusedFunc, 0, 0, 0, "sqlite_compileoption_used"
, {0} }
,
115412 DFUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ){1, 0x2000|1, 0, 0, compileoptiongetFunc, 0, 0, 0, "sqlite_compileoption_get"
, {0} }
,
115413#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
115414 FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY){1,0x0800|1|(0*0x0020)|0x0400, ((void*)(long int)(0)), 0, versionFunc
, 0, 0, 0, "unlikely", {0} }
,
115415 FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY){2,0x0800|1|(0*0x0020)|0x0400, ((void*)(long int)(0)), 0, versionFunc
, 0, 0, 0, "likelihood", {0} }
,
115416 FUNCTION2(likely, 1, 0, 0, noopFunc, SQLITE_FUNC_UNLIKELY){1,0x0800|1|(0*0x0020)|0x0400, ((void*)(long int)(0)), 0, versionFunc
, 0, 0, 0, "likely", {0} }
,
115417#ifdef SQLITE_DEBUG
115418 FUNCTION2(affinity, 1, 0, 0, noopFunc, SQLITE_FUNC_AFFINITY){1,0x0800|1|(0*0x0020)|0x4000, ((void*)(long int)(0)), 0, versionFunc
, 0, 0, 0, "affinity", {0} }
,
115419#endif
115420#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
115421 FUNCTION2(sqlite_offset, 1, 0, 0, noopFunc, SQLITE_FUNC_OFFSET|{1,0x0800|1|(0*0x0020)|0x8000| 0x0080, ((void*)(long int)(0))
, 0, versionFunc, 0, 0, 0, "sqlite_offset", {0} }
115422 SQLITE_FUNC_TYPEOF){1,0x0800|1|(0*0x0020)|0x8000| 0x0080, ((void*)(long int)(0))
, 0, versionFunc, 0, 0, 0, "sqlite_offset", {0} }
,
115423#endif
115424 FUNCTION(ltrim, 1, 1, 0, trimFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0, trimFunc,
0, 0, 0, "ltrim", {0} }
,
115425 FUNCTION(ltrim, 2, 1, 0, trimFunc ){2, 0x0800|1|(0*0x0020), ((void*)(long int)(1)), 0, trimFunc,
0, 0, 0, "ltrim", {0} }
,
115426 FUNCTION(rtrim, 1, 2, 0, trimFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0, trimFunc,
0, 0, 0, "rtrim", {0} }
,
115427 FUNCTION(rtrim, 2, 2, 0, trimFunc ){2, 0x0800|1|(0*0x0020), ((void*)(long int)(2)), 0, trimFunc,
0, 0, 0, "rtrim", {0} }
,
115428 FUNCTION(trim, 1, 3, 0, trimFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(3)), 0, trimFunc,
0, 0, 0, "trim", {0} }
,
115429 FUNCTION(trim, 2, 3, 0, trimFunc ){2, 0x0800|1|(0*0x0020), ((void*)(long int)(3)), 0, trimFunc,
0, 0, 0, "trim", {0} }
,
115430 FUNCTION(min, -1, 0, 1, minmaxFunc ){-1, 0x0800|1|(1*0x0020), ((void*)(long int)(0)), 0, minmaxFunc
, 0, 0, 0, "min", {0} }
,
115431 FUNCTION(min, 0, 0, 1, 0 ){0, 0x0800|1|(1*0x0020), ((void*)(long int)(0)), 0, 0, 0, 0, 0
, "min", {0} }
,
115432 WAGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,{1, 1|(1*0x0020)|0x1000, ((void*)(long int)(0)), 0, minmaxStep
,minMaxFinalize,minMaxValue,0,"min", {0}}
115433 SQLITE_FUNC_MINMAX ){1, 1|(1*0x0020)|0x1000, ((void*)(long int)(0)), 0, minmaxStep
,minMaxFinalize,minMaxValue,0,"min", {0}}
,
115434 FUNCTION(max, -1, 1, 1, minmaxFunc ){-1, 0x0800|1|(1*0x0020), ((void*)(long int)(1)), 0, minmaxFunc
, 0, 0, 0, "max", {0} }
,
115435 FUNCTION(max, 0, 1, 1, 0 ){0, 0x0800|1|(1*0x0020), ((void*)(long int)(1)), 0, 0, 0, 0, 0
, "max", {0} }
,
115436 WAGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize, minMaxValue, 0,{1, 1|(1*0x0020)|0x1000, ((void*)(long int)(1)), 0, minmaxStep
,minMaxFinalize,minMaxValue,0,"max", {0}}
115437 SQLITE_FUNC_MINMAX ){1, 1|(1*0x0020)|0x1000, ((void*)(long int)(1)), 0, minmaxStep
,minMaxFinalize,minMaxValue,0,"max", {0}}
,
115438 FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF){1,0x0800|1|(0*0x0020)|0x0080, ((void*)(long int)(0)), 0, typeofFunc
, 0, 0, 0, "typeof", {0} }
,
115439 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH){1,0x0800|1|(0*0x0020)|0x0040, ((void*)(long int)(0)), 0, lengthFunc
, 0, 0, 0, "length", {0} }
,
115440 FUNCTION(instr, 2, 0, 0, instrFunc ){2, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, instrFunc
, 0, 0, 0, "instr", {0} }
,
115441 FUNCTION(printf, -1, 0, 0, printfFunc ){-1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, printfFunc
, 0, 0, 0, "printf", {0} }
,
115442 FUNCTION(unicode, 1, 0, 0, unicodeFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, unicodeFunc
, 0, 0, 0, "unicode", {0} }
,
115443 FUNCTION(char, -1, 0, 0, charFunc ){-1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, charFunc
, 0, 0, 0, "char", {0} }
,
115444 FUNCTION(abs, 1, 0, 0, absFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, absFunc, 0
, 0, 0, "abs", {0} }
,
115445#ifndef SQLITE_OMIT_FLOATING_POINT
115446 FUNCTION(round, 1, 0, 0, roundFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, roundFunc
, 0, 0, 0, "round", {0} }
,
115447 FUNCTION(round, 2, 0, 0, roundFunc ){2, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, roundFunc
, 0, 0, 0, "round", {0} }
,
115448#endif
115449 FUNCTION(upper, 1, 0, 0, upperFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, upperFunc
, 0, 0, 0, "upper", {0} }
,
115450 FUNCTION(lower, 1, 0, 0, lowerFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, lowerFunc
, 0, 0, 0, "lower", {0} }
,
115451 FUNCTION(hex, 1, 0, 0, hexFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, hexFunc, 0
, 0, 0, "hex", {0} }
,
115452 FUNCTION2(ifnull, 2, 0, 0, noopFunc, SQLITE_FUNC_COALESCE){2,0x0800|1|(0*0x0020)|0x0200, ((void*)(long int)(0)), 0, versionFunc
, 0, 0, 0, "ifnull", {0} }
,
115453 VFUNCTION(random, 0, 0, 0, randomFunc ){0, 1|(0*0x0020), ((void*)(long int)(0)), 0, randomFunc, 0, 0
, 0, "random", {0} }
,
115454 VFUNCTION(randomblob, 1, 0, 0, randomBlob ){1, 1|(0*0x0020), ((void*)(long int)(0)), 0, randomBlob, 0, 0
, 0, "randomblob", {0} }
,
115455 FUNCTION(nullif, 2, 0, 1, nullifFunc ){2, 0x0800|1|(1*0x0020), ((void*)(long int)(0)), 0, nullifFunc
, 0, 0, 0, "nullif", {0} }
,
115456 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ){0, 0x2000|1, 0, 0, versionFunc, 0, 0, 0, "sqlite_version", {
0} }
,
115457 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ){0, 0x2000|1, 0, 0, sourceidFunc, 0, 0, 0, "sqlite_source_id"
, {0} }
,
115458 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ){2, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, errlogFunc
, 0, 0, 0, "sqlite_log", {0} }
,
115459 FUNCTION(quote, 1, 0, 0, quoteFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, quoteFunc
, 0, 0, 0, "quote", {0} }
,
115460 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid){0, 1|(0*0x0020), ((void*)(long int)(0)), 0, last_insert_rowid
, 0, 0, 0, "last_insert_rowid", {0} }
,
115461 VFUNCTION(changes, 0, 0, 0, changes ){0, 1|(0*0x0020), ((void*)(long int)(0)), 0, changes, 0, 0, 0
, "changes", {0} }
,
115462 VFUNCTION(total_changes, 0, 0, 0, total_changes ){0, 1|(0*0x0020), ((void*)(long int)(0)), 0, total_changes, 0
, 0, 0, "total_changes", {0} }
,
115463 FUNCTION(replace, 3, 0, 0, replaceFunc ){3, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, replaceFunc
, 0, 0, 0, "replace", {0} }
,
115464 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, zeroblobFunc
, 0, 0, 0, "zeroblob", {0} }
,
115465 FUNCTION(substr, 2, 0, 0, substrFunc ){2, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, substrFunc
, 0, 0, 0, "substr", {0} }
,
115466 FUNCTION(substr, 3, 0, 0, substrFunc ){3, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, substrFunc
, 0, 0, 0, "substr", {0} }
,
115467 WAGGREGATE(sum, 1,0,0, sumStep, sumFinalize, sumFinalize, sumInverse, 0){1, 1|(0*0x0020)|0, ((void*)(long int)(0)), 0, sumStep,sumFinalize
,sumFinalize,sumInverse,"sum", {0}}
,
115468 WAGGREGATE(total, 1,0,0, sumStep,totalFinalize,totalFinalize,sumInverse, 0){1, 1|(0*0x0020)|0, ((void*)(long int)(0)), 0, sumStep,totalFinalize
,totalFinalize,sumInverse,"total", {0}}
,
115469 WAGGREGATE(avg, 1,0,0, sumStep, avgFinalize, avgFinalize, sumInverse, 0){1, 1|(0*0x0020)|0, ((void*)(long int)(0)), 0, sumStep,avgFinalize
,avgFinalize,sumInverse,"avg", {0}}
,
115470 WAGGREGATE(count, 0,0,0, countStep,{0, 1|(0*0x0020)|0x0100, ((void*)(long int)(0)), 0, countStep
,countFinalize,countFinalize,countInverse,"count", {0}}
115471 countFinalize, countFinalize, countInverse, SQLITE_FUNC_COUNT ){0, 1|(0*0x0020)|0x0100, ((void*)(long int)(0)), 0, countStep
,countFinalize,countFinalize,countInverse,"count", {0}}
,
115472 WAGGREGATE(count, 1,0,0, countStep,{1, 1|(0*0x0020)|0, ((void*)(long int)(0)), 0, countStep,countFinalize
,countFinalize,countInverse,"count", {0}}
115473 countFinalize, countFinalize, countInverse, 0 ){1, 1|(0*0x0020)|0, ((void*)(long int)(0)), 0, countStep,countFinalize
,countFinalize,countInverse,"count", {0}}
,
115474 WAGGREGATE(group_concat, 1, 0, 0, groupConcatStep,{1, 1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
115475 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){1, 1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
,
115476 WAGGREGATE(group_concat, 2, 0, 0, groupConcatStep,{2, 1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
115477 groupConcatFinalize, groupConcatValue, groupConcatInverse, 0){2, 1|(0*0x0020)|0, ((void*)(long int)(0)), 0, groupConcatStep
,groupConcatFinalize,groupConcatValue,groupConcatInverse,"group_concat"
, {0}}
,
115478
115479 LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE){2, 0x0800|1|0x0004|0x0008, (void *)&globInfo, 0, likeFunc
, 0, 0, 0, "glob", {0} }
,
115480#ifdef SQLITE_CASE_SENSITIVE_LIKE
115481 LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE){2, 0x0800|1|0x0004|0x0008, (void *)&likeInfoAlt, 0, likeFunc
, 0, 0, 0, "like", {0} }
,
115482 LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE){3, 0x0800|1|0x0004|0x0008, (void *)&likeInfoAlt, 0, likeFunc
, 0, 0, 0, "like", {0} }
,
115483#else
115484 LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE){2, 0x0800|1|0x0004, (void *)&likeInfoNorm, 0, likeFunc, 0
, 0, 0, "like", {0} }
,
115485 LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE){3, 0x0800|1|0x0004, (void *)&likeInfoNorm, 0, likeFunc, 0
, 0, 0, "like", {0} }
,
115486#endif
115487#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
115488 FUNCTION(unknown, -1, 0, 0, unknownFunc ){-1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, unknownFunc
, 0, 0, 0, "unknown", {0} }
,
115489#endif
115490 FUNCTION(coalesce, 1, 0, 0, 0 ){1, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, 0, 0, 0, 0
, "coalesce", {0} }
,
115491 FUNCTION(coalesce, 0, 0, 0, 0 ){0, 0x0800|1|(0*0x0020), ((void*)(long int)(0)), 0, 0, 0, 0, 0
, "coalesce", {0} }
,
115492 FUNCTION2(coalesce, -1, 0, 0, noopFunc, SQLITE_FUNC_COALESCE){-1,0x0800|1|(0*0x0020)|0x0200, ((void*)(long int)(0)), 0, versionFunc
, 0, 0, 0, "coalesce", {0} }
,
115493 };
115494#ifndef SQLITE_OMIT_ALTERTABLE
115495 sqlite3AlterFunctions();
115496#endif
115497 sqlite3WindowFunctions();
115498#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
115499 sqlite3AnalyzeFunctions();
115500#endif
115501 sqlite3RegisterDateTimeFunctions();
115502 sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc)((int)(sizeof(aBuiltinFunc)/sizeof(aBuiltinFunc[0]))));
115503
115504#if 0 /* Enable to print out how the built-in functions are hashed */
115505 {
115506 int i;
115507 FuncDef *p;
115508 for(i=0; i<SQLITE_FUNC_HASH_SZ23; i++){
115509 printf("FUNC-HASH %02d:", i);
115510 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
115511 int n = sqlite3Strlen30(p->zName);
115512 int h = p->zName[0] + n;
115513 printf(" %s(%d)", p->zName, h);
115514 }
115515 printf("\n");
115516 }
115517 }
115518#endif
115519}
115520
115521/************** End of func.c ************************************************/
115522/************** Begin file fkey.c ********************************************/
115523/*
115524**
115525** The author disclaims copyright to this source code. In place of
115526** a legal notice, here is a blessing:
115527**
115528** May you do good and not evil.
115529** May you find forgiveness for yourself and forgive others.
115530** May you share freely, never taking more than you give.
115531**
115532*************************************************************************
115533** This file contains code used by the compiler to add foreign key
115534** support to compiled SQL statements.
115535*/
115536/* #include "sqliteInt.h" */
115537
115538#ifndef SQLITE_OMIT_FOREIGN_KEY
115539#ifndef SQLITE_OMIT_TRIGGER
115540
115541/*
115542** Deferred and Immediate FKs
115543** --------------------------
115544**
115545** Foreign keys in SQLite come in two flavours: deferred and immediate.
115546** If an immediate foreign key constraint is violated,
115547** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
115548** statement transaction rolled back. If a
115549** deferred foreign key constraint is violated, no action is taken
115550** immediately. However if the application attempts to commit the
115551** transaction before fixing the constraint violation, the attempt fails.
115552**
115553** Deferred constraints are implemented using a simple counter associated
115554** with the database handle. The counter is set to zero each time a
115555** database transaction is opened. Each time a statement is executed
115556** that causes a foreign key violation, the counter is incremented. Each
115557** time a statement is executed that removes an existing violation from
115558** the database, the counter is decremented. When the transaction is
115559** committed, the commit fails if the current value of the counter is
115560** greater than zero. This scheme has two big drawbacks:
115561**
115562** * When a commit fails due to a deferred foreign key constraint,
115563** there is no way to tell which foreign constraint is not satisfied,
115564** or which row it is not satisfied for.
115565**
115566** * If the database contains foreign key violations when the
115567** transaction is opened, this may cause the mechanism to malfunction.
115568**
115569** Despite these problems, this approach is adopted as it seems simpler
115570** than the alternatives.
115571**
115572** INSERT operations:
115573**
115574** I.1) For each FK for which the table is the child table, search
115575** the parent table for a match. If none is found increment the
115576** constraint counter.
115577**
115578** I.2) For each FK for which the table is the parent table,
115579** search the child table for rows that correspond to the new
115580** row in the parent table. Decrement the counter for each row
115581** found (as the constraint is now satisfied).
115582**
115583** DELETE operations:
115584**
115585** D.1) For each FK for which the table is the child table,
115586** search the parent table for a row that corresponds to the
115587** deleted row in the child table. If such a row is not found,
115588** decrement the counter.
115589**
115590** D.2) For each FK for which the table is the parent table, search
115591** the child table for rows that correspond to the deleted row
115592** in the parent table. For each found increment the counter.
115593**
115594** UPDATE operations:
115595**
115596** An UPDATE command requires that all 4 steps above are taken, but only
115597** for FK constraints for which the affected columns are actually
115598** modified (values must be compared at runtime).
115599**
115600** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
115601** This simplifies the implementation a bit.
115602**
115603** For the purposes of immediate FK constraints, the OR REPLACE conflict
115604** resolution is considered to delete rows before the new row is inserted.
115605** If a delete caused by OR REPLACE violates an FK constraint, an exception
115606** is thrown, even if the FK constraint would be satisfied after the new
115607** row is inserted.
115608**
115609** Immediate constraints are usually handled similarly. The only difference
115610** is that the counter used is stored as part of each individual statement
115611** object (struct Vdbe). If, after the statement has run, its immediate
115612** constraint counter is greater than zero,
115613** it returns SQLITE_CONSTRAINT_FOREIGNKEY
115614** and the statement transaction is rolled back. An exception is an INSERT
115615** statement that inserts a single row only (no triggers). In this case,
115616** instead of using a counter, an exception is thrown immediately if the
115617** INSERT violates a foreign key constraint. This is necessary as such
115618** an INSERT does not open a statement transaction.
115619**
115620** TODO: How should dropping a table be handled? How should renaming a
115621** table be handled?
115622**
115623**
115624** Query API Notes
115625** ---------------
115626**
115627** Before coding an UPDATE or DELETE row operation, the code-generator
115628** for those two operations needs to know whether or not the operation
115629** requires any FK processing and, if so, which columns of the original
115630** row are required by the FK processing VDBE code (i.e. if FKs were
115631** implemented using triggers, which of the old.* columns would be
115632** accessed). No information is required by the code-generator before
115633** coding an INSERT operation. The functions used by the UPDATE/DELETE
115634** generation code to query for this information are:
115635**
115636** sqlite3FkRequired() - Test to see if FK processing is required.
115637** sqlite3FkOldmask() - Query for the set of required old.* columns.
115638**
115639**
115640** Externally accessible module functions
115641** --------------------------------------
115642**
115643** sqlite3FkCheck() - Check for foreign key violations.
115644** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
115645** sqlite3FkDelete() - Delete an FKey structure.
115646*/
115647
115648/*
115649** VDBE Calling Convention
115650** -----------------------
115651**
115652** Example:
115653**
115654** For the following INSERT statement:
115655**
115656** CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
115657** INSERT INTO t1 VALUES(1, 2, 3.1);
115658**
115659** Register (x): 2 (type integer)
115660** Register (x+1): 1 (type integer)
115661** Register (x+2): NULL (type NULL)
115662** Register (x+3): 3.1 (type real)
115663*/
115664
115665/*
115666** A foreign key constraint requires that the key columns in the parent
115667** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
115668** Given that pParent is the parent table for foreign key constraint pFKey,
115669** search the schema for a unique index on the parent key columns.
115670**
115671** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
115672** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
115673** is set to point to the unique index.
115674**
115675** If the parent key consists of a single column (the foreign key constraint
115676** is not a composite foreign key), output variable *paiCol is set to NULL.
115677** Otherwise, it is set to point to an allocated array of size N, where
115678** N is the number of columns in the parent key. The first element of the
115679** array is the index of the child table column that is mapped by the FK
115680** constraint to the parent table column stored in the left-most column
115681** of index *ppIdx. The second element of the array is the index of the
115682** child table column that corresponds to the second left-most column of
115683** *ppIdx, and so on.
115684**
115685** If the required index cannot be found, either because:
115686**
115687** 1) The named parent key columns do not exist, or
115688**
115689** 2) The named parent key columns do exist, but are not subject to a
115690** UNIQUE or PRIMARY KEY constraint, or
115691**
115692** 3) No parent key columns were provided explicitly as part of the
115693** foreign key definition, and the parent table does not have a
115694** PRIMARY KEY, or
115695**
115696** 4) No parent key columns were provided explicitly as part of the
115697** foreign key definition, and the PRIMARY KEY of the parent table
115698** consists of a different number of columns to the child key in
115699** the child table.
115700**
115701** then non-zero is returned, and a "foreign key mismatch" error loaded
115702** into pParse. If an OOM error occurs, non-zero is returned and the
115703** pParse->db->mallocFailed flag is set.
115704*/
115705SQLITE_PRIVATEstatic int sqlite3FkLocateIndex(
115706 Parse *pParse, /* Parse context to store any error in */
115707 Table *pParent, /* Parent table of FK constraint pFKey */
115708 FKey *pFKey, /* Foreign key to find index for */
115709 Index **ppIdx, /* OUT: Unique index on parent table */
115710 int **paiCol /* OUT: Map of index columns in pFKey */
115711){
115712 Index *pIdx = 0; /* Value to return via *ppIdx */
115713 int *aiCol = 0; /* Value to return via *paiCol */
115714 int nCol = pFKey->nCol; /* Number of columns in parent key */
115715 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
115716
115717 /* The caller is responsible for zeroing output parameters. */
115718 assert( ppIdx && *ppIdx==0 )((void) (0));
115719 assert( !paiCol || *paiCol==0 )((void) (0));
115720 assert( pParse )((void) (0));
115721
115722 /* If this is a non-composite (single column) foreign key, check if it
115723 ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
115724 ** and *paiCol set to zero and return early.
115725 **
115726 ** Otherwise, for a composite foreign key (more than one column), allocate
115727 ** space for the aiCol array (returned via output parameter *paiCol).
115728 ** Non-composite foreign keys do not require the aiCol array.
115729 */
115730 if( nCol==1 ){
115731 /* The FK maps to the IPK if any of the following are true:
115732 **
115733 ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
115734 ** mapped to the primary key of table pParent, or
115735 ** 2) The FK is explicitly mapped to a column declared as INTEGER
115736 ** PRIMARY KEY.
115737 */
115738 if( pParent->iPKey>=0 ){
115739 if( !zKey ) return 0;
115740 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
115741 }
115742 }else if( paiCol ){
115743 assert( nCol>1 )((void) (0));
115744 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
115745 if( !aiCol ) return 1;
115746 *paiCol = aiCol;
115747 }
115748
115749 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
115750 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx)((pIdx)->onError!=0) && pIdx->pPartIdxWhere==0 ){
115751 /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
115752 ** of columns. If each indexed column corresponds to a foreign key
115753 ** column of pFKey, then this index is a winner. */
115754
115755 if( zKey==0 ){
115756 /* If zKey is NULL, then this foreign key is implicitly mapped to
115757 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
115758 ** identified by the test. */
115759 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
115760 if( aiCol ){
115761 int i;
115762 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
115763 }
115764 break;
115765 }
115766 }else{
115767 /* If zKey is non-NULL, then this foreign key was declared to
115768 ** map to an explicit list of columns in table pParent. Check if this
115769 ** index matches those columns. Also, check that the index uses
115770 ** the default collation sequences for each column. */
115771 int i, j;
115772 for(i=0; i<nCol; i++){
115773 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
115774 const char *zDfltColl; /* Def. collation for column */
115775 char *zIdxCol; /* Name of indexed column */
115776
115777 if( iCol<0 ) break; /* No foreign keys against expression indexes */
115778
115779 /* If the index uses a collation sequence that is different from
115780 ** the default collation sequence for the column, this index is
115781 ** unusable. Bail out early in this case. */
115782 zDfltColl = pParent->aCol[iCol].zColl;
115783 if( !zDfltColl ) zDfltColl = sqlite3StrBINARY;
115784 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
115785
115786 zIdxCol = pParent->aCol[iCol].zName;
115787 for(j=0; j<nCol; j++){
115788 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
115789 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
115790 break;
115791 }
115792 }
115793 if( j==nCol ) break;
115794 }
115795 if( i==nCol ) break; /* pIdx is usable */
115796 }
115797 }
115798 }
115799
115800 if( !pIdx ){
115801 if( !pParse->disableTriggers ){
115802 sqlite3ErrorMsg(pParse,
115803 "foreign key mismatch - \"%w\" referencing \"%w\"",
115804 pFKey->pFrom->zName, pFKey->zTo);
115805 }
115806 sqlite3DbFree(pParse->db, aiCol);
115807 return 1;
115808 }
115809
115810 *ppIdx = pIdx;
115811 return 0;
115812}
115813
115814/*
115815** This function is called when a row is inserted into or deleted from the
115816** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
115817** on the child table of pFKey, this function is invoked twice for each row
115818** affected - once to "delete" the old row, and then again to "insert" the
115819** new row.
115820**
115821** Each time it is called, this function generates VDBE code to locate the
115822** row in the parent table that corresponds to the row being inserted into
115823** or deleted from the child table. If the parent row can be found, no
115824** special action is taken. Otherwise, if the parent row can *not* be
115825** found in the parent table:
115826**
115827** Operation | FK type | Action taken
115828** --------------------------------------------------------------------------
115829** INSERT immediate Increment the "immediate constraint counter".
115830**
115831** DELETE immediate Decrement the "immediate constraint counter".
115832**
115833** INSERT deferred Increment the "deferred constraint counter".
115834**
115835** DELETE deferred Decrement the "deferred constraint counter".
115836**
115837** These operations are identified in the comment at the top of this file
115838** (fkey.c) as "I.1" and "D.1".
115839*/
115840static void fkLookupParent(
115841 Parse *pParse, /* Parse context */
115842 int iDb, /* Index of database housing pTab */
115843 Table *pTab, /* Parent table of FK pFKey */
115844 Index *pIdx, /* Unique index on parent key columns in pTab */
115845 FKey *pFKey, /* Foreign key constraint */
115846 int *aiCol, /* Map from parent key columns to child table columns */
115847 int regData, /* Address of array containing child table row */
115848 int nIncr, /* Increment constraint counter by this */
115849 int isIgnore /* If true, pretend pTab contains all NULL values */
115850){
115851 int i; /* Iterator variable */
115852 Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */
115853 int iCur = pParse->nTab - 1; /* Cursor number to use */
115854 int iOk = sqlite3VdbeMakeLabel(pParse); /* jump here if parent key found */
115855
115856 sqlite3VdbeVerifyAbortable(v,
115857 (!pFKey->isDeferred
115858 && !(pParse->db->flags & SQLITE_DeferFKs)
115859 && !pParse->pToplevel
115860 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
115861
115862 /* If nIncr is less than zero, then check at runtime if there are any
115863 ** outstanding constraints to resolve. If there are not, there is no need
115864 ** to check if deleting this row resolves any outstanding violations.
115865 **
115866 ** Check if any of the key columns in the child table row are NULL. If
115867 ** any are, then the constraint is considered satisfied. No need to
115868 ** search for a matching row in the parent table. */
115869 if( nIncr<0 ){
115870 sqlite3VdbeAddOp2(v, OP_FkIfZero46, pFKey->isDeferred, iOk);
115871 VdbeCoverage(v);
115872 }
115873 for(i=0; i<pFKey->nCol; i++){
115874 int iReg = aiCol[i] + regData + 1;
115875 sqlite3VdbeAddOp2(v, OP_IsNull50, iReg, iOk); VdbeCoverage(v);
115876 }
115877
115878 if( isIgnore==0 ){
115879 if( pIdx==0 ){
115880 /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
115881 ** column of the parent table (table pTab). */
115882 int iMustBeInt; /* Address of MustBeInt instruction */
115883 int regTemp = sqlite3GetTempReg(pParse);
115884
115885 /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
115886 ** apply the affinity of the parent key). If this fails, then there
115887 ** is no matching parent key. Before using MustBeInt, make a copy of
115888 ** the value. Otherwise, the value inserted into the child key column
115889 ** will have INTEGER affinity applied to it, which may not be correct. */
115890 sqlite3VdbeAddOp2(v, OP_SCopy79, aiCol[0]+1+regData, regTemp);
115891 iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt15, regTemp, 0);
115892 VdbeCoverage(v);
115893
115894 /* If the parent table is the same as the child table, and we are about
115895 ** to increment the constraint-counter (i.e. this is an INSERT operation),
115896 ** then check if the row being inserted matches itself. If so, do not
115897 ** increment the constraint-counter. */
115898 if( pTab==pFKey->pFrom && nIncr==1 ){
115899 sqlite3VdbeAddOp3(v, OP_Eq53, regData, iOk, regTemp); VdbeCoverage(v);
115900 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
115901 }
115902
115903 sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead108);
115904 sqlite3VdbeAddOp3(v, OP_NotExists31, iCur, 0, regTemp); VdbeCoverage(v);
115905 sqlite3VdbeGoto(v, iOk);
115906 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
115907 sqlite3VdbeJumpHere(v, iMustBeInt);
115908 sqlite3ReleaseTempReg(pParse, regTemp);
115909 }else{
115910 int nCol = pFKey->nCol;
115911 int regTemp = sqlite3GetTempRange(pParse, nCol);
115912 int regRec = sqlite3GetTempReg(pParse);
115913
115914 sqlite3VdbeAddOp3(v, OP_OpenRead108, iCur, pIdx->tnum, iDb);
115915 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
115916 for(i=0; i<nCol; i++){
115917 sqlite3VdbeAddOp2(v, OP_Copy78, aiCol[i]+1+regData, regTemp+i);
115918 }
115919
115920 /* If the parent table is the same as the child table, and we are about
115921 ** to increment the constraint-counter (i.e. this is an INSERT operation),
115922 ** then check if the row being inserted matches itself. If so, do not
115923 ** increment the constraint-counter.
115924 **
115925 ** If any of the parent-key values are NULL, then the row cannot match
115926 ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
115927 ** of the parent-key values are NULL (at this point it is known that
115928 ** none of the child key values are).
115929 */
115930 if( pTab==pFKey->pFrom && nIncr==1 ){
115931 int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
115932 for(i=0; i<nCol; i++){
115933 int iChild = aiCol[i]+1+regData;
115934 int iParent = pIdx->aiColumn[i]+1+regData;
115935 assert( pIdx->aiColumn[i]>=0 )((void) (0));
115936 assert( aiCol[i]!=pTab->iPKey )((void) (0));
115937 if( pIdx->aiColumn[i]==pTab->iPKey ){
115938 /* The parent key is a composite key that includes the IPK column */
115939 iParent = regData;
115940 }
115941 sqlite3VdbeAddOp3(v, OP_Ne52, iChild, iJump, iParent); VdbeCoverage(v);
115942 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL0x10);
115943 }
115944 sqlite3VdbeGoto(v, iOk);
115945 }
115946
115947 sqlite3VdbeAddOp4(v, OP_MakeRecord92, regTemp, nCol, regRec,
115948 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
115949 sqlite3VdbeAddOp4Int(v, OP_Found29, iCur, iOk, regRec, 0); VdbeCoverage(v);
115950
115951 sqlite3ReleaseTempReg(pParse, regRec);
115952 sqlite3ReleaseTempRange(pParse, regTemp, nCol);
115953 }
115954 }
115955
115956 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs0x00080000)
115957 && !pParse->pToplevel
115958 && !pParse->isMultiWrite
115959 ){
115960 /* Special case: If this is an INSERT statement that will insert exactly
115961 ** one row into the table, raise a constraint immediately instead of
115962 ** incrementing a counter. This is necessary as the VM code is being
115963 ** generated for will not open a statement transaction. */
115964 assert( nIncr==1 )((void) (0));
115965 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)),
115966 OE_Abort2, 0, P4_STATIC(-1), P5_ConstraintFK4);
115967 }else{
115968 if( nIncr>0 && pFKey->isDeferred==0 ){
115969 sqlite3MayAbort(pParse);
115970 }
115971 sqlite3VdbeAddOp2(v, OP_FkCounter150, pFKey->isDeferred, nIncr);
115972 }
115973
115974 sqlite3VdbeResolveLabel(v, iOk);
115975 sqlite3VdbeAddOp1(v, OP_Close117, iCur);
115976}
115977
115978
115979/*
115980** Return an Expr object that refers to a memory register corresponding
115981** to column iCol of table pTab.
115982**
115983** regBase is the first of an array of register that contains the data
115984** for pTab. regBase itself holds the rowid. regBase+1 holds the first
115985** column. regBase+2 holds the second column, and so forth.
115986*/
115987static Expr *exprTableRegister(
115988 Parse *pParse, /* Parsing and code generating context */
115989 Table *pTab, /* The table whose content is at r[regBase]... */
115990 int regBase, /* Contents of table pTab */
115991 i16 iCol /* Which column of pTab is desired */
115992){
115993 Expr *pExpr;
115994 Column *pCol;
115995 const char *zColl;
115996 sqlite3 *db = pParse->db;
115997
115998 pExpr = sqlite3Expr(db, TK_REGISTER168, 0);
115999 if( pExpr ){
116000 if( iCol>=0 && iCol!=pTab->iPKey ){
116001 pCol = &pTab->aCol[iCol];
116002 pExpr->iTable = regBase + iCol + 1;
116003 pExpr->affinity = pCol->affinity;
116004 zColl = pCol->zColl;
116005 if( zColl==0 ) zColl = db->pDfltColl->zName;
116006 pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
116007 }else{
116008 pExpr->iTable = regBase;
116009 pExpr->affinity = SQLITE_AFF_INTEGER'D';
116010 }
116011 }
116012 return pExpr;
116013}
116014
116015/*
116016** Return an Expr object that refers to column iCol of table pTab which
116017** has cursor iCur.
116018*/
116019static Expr *exprTableColumn(
116020 sqlite3 *db, /* The database connection */
116021 Table *pTab, /* The table whose column is desired */
116022 int iCursor, /* The open cursor on the table */
116023 i16 iCol /* The column that is wanted */
116024){
116025 Expr *pExpr = sqlite3Expr(db, TK_COLUMN162, 0);
116026 if( pExpr ){
116027 pExpr->y.pTab = pTab;
116028 pExpr->iTable = iCursor;
116029 pExpr->iColumn = iCol;
116030 }
116031 return pExpr;
116032}
116033
116034/*
116035** This function is called to generate code executed when a row is deleted
116036** from the parent table of foreign key constraint pFKey and, if pFKey is
116037** deferred, when a row is inserted into the same table. When generating
116038** code for an SQL UPDATE operation, this function may be called twice -
116039** once to "delete" the old row and once to "insert" the new row.
116040**
116041** Parameter nIncr is passed -1 when inserting a row (as this may decrease
116042** the number of FK violations in the db) or +1 when deleting one (as this
116043** may increase the number of FK constraint problems).
116044**
116045** The code generated by this function scans through the rows in the child
116046** table that correspond to the parent table row being deleted or inserted.
116047** For each child row found, one of the following actions is taken:
116048**
116049** Operation | FK type | Action taken
116050** --------------------------------------------------------------------------
116051** DELETE immediate Increment the "immediate constraint counter".
116052** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
116053** throw a "FOREIGN KEY constraint failed" exception.
116054**
116055** INSERT immediate Decrement the "immediate constraint counter".
116056**
116057** DELETE deferred Increment the "deferred constraint counter".
116058** Or, if the ON (UPDATE|DELETE) action is RESTRICT,
116059** throw a "FOREIGN KEY constraint failed" exception.
116060**
116061** INSERT deferred Decrement the "deferred constraint counter".
116062**
116063** These operations are identified in the comment at the top of this file
116064** (fkey.c) as "I.2" and "D.2".
116065*/
116066static void fkScanChildren(
116067 Parse *pParse, /* Parse context */
116068 SrcList *pSrc, /* The child table to be scanned */
116069 Table *pTab, /* The parent table */
116070 Index *pIdx, /* Index on parent covering the foreign key */
116071 FKey *pFKey, /* The foreign key linking pSrc to pTab */
116072 int *aiCol, /* Map from pIdx cols to child table cols */
116073 int regData, /* Parent row data starts here */
116074 int nIncr /* Amount to increment deferred counter by */
116075){
116076 sqlite3 *db = pParse->db; /* Database handle */
116077 int i; /* Iterator variable */
116078 Expr *pWhere = 0; /* WHERE clause to scan with */
116079 NameContext sNameContext; /* Context used to resolve WHERE clause */
116080 WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */
116081 int iFkIfZero = 0; /* Address of OP_FkIfZero */
116082 Vdbe *v = sqlite3GetVdbe(pParse);
116083
116084 assert( pIdx==0 || pIdx->pTable==pTab )((void) (0));
116085 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol )((void) (0));
116086 assert( pIdx!=0 || pFKey->nCol==1 )((void) (0));
116087 assert( pIdx!=0 || HasRowid(pTab) )((void) (0));
116088
116089 if( nIncr<0 ){
116090 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero46, pFKey->isDeferred, 0);
116091 VdbeCoverage(v);
116092 }
116093
116094 /* Create an Expr object representing an SQL expression like:
116095 **
116096 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
116097 **
116098 ** The collation sequence used for the comparison should be that of
116099 ** the parent key columns. The affinity of the parent key column should
116100 ** be applied to each child key value before the comparison takes place.
116101 */
116102 for(i=0; i<pFKey->nCol; i++){
116103 Expr *pLeft; /* Value from parent table row */
116104 Expr *pRight; /* Column ref to child table */
116105 Expr *pEq; /* Expression (pLeft = pRight) */
116106 i16 iCol; /* Index of column in child table */
116107 const char *zCol; /* Name of column in child table */
116108
116109 iCol = pIdx ? pIdx->aiColumn[i] : -1;
116110 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
116111 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
116112 assert( iCol>=0 )((void) (0));
116113 zCol = pFKey->pFrom->aCol[iCol].zName;
116114 pRight = sqlite3Expr(db, TK_ID59, zCol);
116115 pEq = sqlite3PExpr(pParse, TK_EQ53, pLeft, pRight);
116116 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
116117 }
116118
116119 /* If the child table is the same as the parent table, then add terms
116120 ** to the WHERE clause that prevent this entry from being scanned.
116121 ** The added WHERE clause terms are like this:
116122 **
116123 ** $current_rowid!=rowid
116124 ** NOT( $current_a==a AND $current_b==b AND ... )
116125 **
116126 ** The first form is used for rowid tables. The second form is used
116127 ** for WITHOUT ROWID tables. In the second form, the *parent* key is
116128 ** (a,b,...). Either the parent or primary key could be used to
116129 ** uniquely identify the current row, but the parent key is more convenient
116130 ** as the required values have already been loaded into registers
116131 ** by the caller.
116132 */
116133 if( pTab==pFKey->pFrom && nIncr>0 ){
116134 Expr *pNe; /* Expression (pLeft != pRight) */
116135 Expr *pLeft; /* Value from parent table row */
116136 Expr *pRight; /* Column ref to child table */
116137 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
116138 pLeft = exprTableRegister(pParse, pTab, regData, -1);
116139 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
116140 pNe = sqlite3PExpr(pParse, TK_NE52, pLeft, pRight);
116141 }else{
116142 Expr *pEq, *pAll = 0;
116143 assert( pIdx!=0 )((void) (0));
116144 for(i=0; i<pIdx->nKeyCol; i++){
116145 i16 iCol = pIdx->aiColumn[i];
116146 assert( iCol>=0 )((void) (0));
116147 pLeft = exprTableRegister(pParse, pTab, regData, iCol);
116148 pRight = sqlite3Expr(db, TK_ID59, pTab->aCol[iCol].zName);
116149 pEq = sqlite3PExpr(pParse, TK_IS45, pLeft, pRight);
116150 pAll = sqlite3ExprAnd(pParse, pAll, pEq);
116151 }
116152 pNe = sqlite3PExpr(pParse, TK_NOT19, pAll, 0);
116153 }
116154 pWhere = sqlite3ExprAnd(pParse, pWhere, pNe);
116155 }
116156
116157 /* Resolve the references in the WHERE clause. */
116158 memset(&sNameContext, 0, sizeof(NameContext));
116159 sNameContext.pSrcList = pSrc;
116160 sNameContext.pParse = pParse;
116161 sqlite3ResolveExprNames(&sNameContext, pWhere);
116162
116163 /* Create VDBE to loop through the entries in pSrc that match the WHERE
116164 ** clause. For each row found, increment either the deferred or immediate
116165 ** foreign key constraint counter. */
116166 if( pParse->nErr==0 ){
116167 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
116168 sqlite3VdbeAddOp2(v, OP_FkCounter150, pFKey->isDeferred, nIncr);
116169 if( pWInfo ){
116170 sqlite3WhereEnd(pWInfo);
116171 }
116172 }
116173
116174 /* Clean up the WHERE clause constructed above. */
116175 sqlite3ExprDelete(db, pWhere);
116176 if( iFkIfZero ){
116177 sqlite3VdbeJumpHere(v, iFkIfZero);
116178 }
116179}
116180
116181/*
116182** This function returns a linked list of FKey objects (connected by
116183** FKey.pNextTo) holding all children of table pTab. For example,
116184** given the following schema:
116185**
116186** CREATE TABLE t1(a PRIMARY KEY);
116187** CREATE TABLE t2(b REFERENCES t1(a);
116188**
116189** Calling this function with table "t1" as an argument returns a pointer
116190** to the FKey structure representing the foreign key constraint on table
116191** "t2". Calling this function with "t2" as the argument would return a
116192** NULL pointer (as there are no FK constraints for which t2 is the parent
116193** table).
116194*/
116195SQLITE_PRIVATEstatic FKey *sqlite3FkReferences(Table *pTab){
116196 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
116197}
116198
116199/*
116200** The second argument is a Trigger structure allocated by the
116201** fkActionTrigger() routine. This function deletes the Trigger structure
116202** and all of its sub-components.
116203**
116204** The Trigger structure or any of its sub-components may be allocated from
116205** the lookaside buffer belonging to database handle dbMem.
116206*/
116207static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
116208 if( p ){
116209 TriggerStep *pStep = p->step_list;
116210 sqlite3ExprDelete(dbMem, pStep->pWhere);
116211 sqlite3ExprListDelete(dbMem, pStep->pExprList);
116212 sqlite3SelectDelete(dbMem, pStep->pSelect);
116213 sqlite3ExprDelete(dbMem, p->pWhen);
116214 sqlite3DbFree(dbMem, p);
116215 }
116216}
116217
116218/*
116219** This function is called to generate code that runs when table pTab is
116220** being dropped from the database. The SrcList passed as the second argument
116221** to this function contains a single entry guaranteed to resolve to
116222** table pTab.
116223**
116224** Normally, no code is required. However, if either
116225**
116226** (a) The table is the parent table of a FK constraint, or
116227** (b) The table is the child table of a deferred FK constraint and it is
116228** determined at runtime that there are outstanding deferred FK
116229** constraint violations in the database,
116230**
116231** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
116232** the table from the database. Triggers are disabled while running this
116233** DELETE, but foreign key actions are not.
116234*/
116235SQLITE_PRIVATEstatic void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
116236 sqlite3 *db = pParse->db;
116237 if( (db->flags&SQLITE_ForeignKeys0x00004000) && !IsVirtual(pTab)((pTab)->nModuleArg) ){
116238 int iSkip = 0;
116239 Vdbe *v = sqlite3GetVdbe(pParse);
116240
116241 assert( v )((void) (0)); /* VDBE has already been allocated */
116242 assert( pTab->pSelect==0 )((void) (0)); /* Not a view */
116243 if( sqlite3FkReferences(pTab)==0 ){
116244 /* Search for a deferred foreign key constraint for which this table
116245 ** is the child table. If one cannot be found, return without
116246 ** generating any VDBE code. If one can be found, then jump over
116247 ** the entire DELETE if there are no outstanding deferred constraints
116248 ** when this statement is run. */
116249 FKey *p;
116250 for(p=pTab->pFKey; p; p=p->pNextFrom){
116251 if( p->isDeferred || (db->flags & SQLITE_DeferFKs0x00080000) ) break;
116252 }
116253 if( !p ) return;
116254 iSkip = sqlite3VdbeMakeLabel(pParse);
116255 sqlite3VdbeAddOp2(v, OP_FkIfZero46, 1, iSkip); VdbeCoverage(v);
116256 }
116257
116258 pParse->disableTriggers = 1;
116259 sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
116260 pParse->disableTriggers = 0;
116261
116262 /* If the DELETE has generated immediate foreign key constraint
116263 ** violations, halt the VDBE and return an error at this point, before
116264 ** any modifications to the schema are made. This is because statement
116265 ** transactions are not able to rollback schema changes.
116266 **
116267 ** If the SQLITE_DeferFKs flag is set, then this is not required, as
116268 ** the statement transaction will not be rolled back even if FK
116269 ** constraints are violated.
116270 */
116271 if( (db->flags & SQLITE_DeferFKs0x00080000)==0 ){
116272 sqlite3VdbeVerifyAbortable(v, OE_Abort);
116273 sqlite3VdbeAddOp2(v, OP_FkIfZero46, 0, sqlite3VdbeCurrentAddr(v)+2);
116274 VdbeCoverage(v);
116275 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)),
116276 OE_Abort2, 0, P4_STATIC(-1), P5_ConstraintFK4);
116277 }
116278
116279 if( iSkip ){
116280 sqlite3VdbeResolveLabel(v, iSkip);
116281 }
116282 }
116283}
116284
116285
116286/*
116287** The second argument points to an FKey object representing a foreign key
116288** for which pTab is the child table. An UPDATE statement against pTab
116289** is currently being processed. For each column of the table that is
116290** actually updated, the corresponding element in the aChange[] array
116291** is zero or greater (if a column is unmodified the corresponding element
116292** is set to -1). If the rowid column is modified by the UPDATE statement
116293** the bChngRowid argument is non-zero.
116294**
116295** This function returns true if any of the columns that are part of the
116296** child key for FK constraint *p are modified.
116297*/
116298static int fkChildIsModified(
116299 Table *pTab, /* Table being updated */
116300 FKey *p, /* Foreign key for which pTab is the child */
116301 int *aChange, /* Array indicating modified columns */
116302 int bChngRowid /* True if rowid is modified by this update */
116303){
116304 int i;
116305 for(i=0; i<p->nCol; i++){
116306 int iChildKey = p->aCol[i].iFrom;
116307 if( aChange[iChildKey]>=0 ) return 1;
116308 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
116309 }
116310 return 0;
116311}
116312
116313/*
116314** The second argument points to an FKey object representing a foreign key
116315** for which pTab is the parent table. An UPDATE statement against pTab
116316** is currently being processed. For each column of the table that is
116317** actually updated, the corresponding element in the aChange[] array
116318** is zero or greater (if a column is unmodified the corresponding element
116319** is set to -1). If the rowid column is modified by the UPDATE statement
116320** the bChngRowid argument is non-zero.
116321**
116322** This function returns true if any of the columns that are part of the
116323** parent key for FK constraint *p are modified.
116324*/
116325static int fkParentIsModified(
116326 Table *pTab,
116327 FKey *p,
116328 int *aChange,
116329 int bChngRowid
116330){
116331 int i;
116332 for(i=0; i<p->nCol; i++){
116333 char *zKey = p->aCol[i].zCol;
116334 int iKey;
116335 for(iKey=0; iKey<pTab->nCol; iKey++){
116336 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
116337 Column *pCol = &pTab->aCol[iKey];
116338 if( zKey ){
116339 if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
116340 }else if( pCol->colFlags & COLFLAG_PRIMKEY0x0001 ){
116341 return 1;
116342 }
116343 }
116344 }
116345 }
116346 return 0;
116347}
116348
116349/*
116350** Return true if the parser passed as the first argument is being
116351** used to code a trigger that is really a "SET NULL" action belonging
116352** to trigger pFKey.
116353*/
116354static int isSetNullAction(Parse *pParse, FKey *pFKey){
116355 Parse *pTop = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
116356 if( pTop->pTriggerPrg ){
116357 Trigger *p = pTop->pTriggerPrg->pTrigger;
116358 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull8)
116359 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull8)
116360 ){
116361 return 1;
116362 }
116363 }
116364 return 0;
116365}
116366
116367/*
116368** This function is called when inserting, deleting or updating a row of
116369** table pTab to generate VDBE code to perform foreign key constraint
116370** processing for the operation.
116371**
116372** For a DELETE operation, parameter regOld is passed the index of the
116373** first register in an array of (pTab->nCol+1) registers containing the
116374** rowid of the row being deleted, followed by each of the column values
116375** of the row being deleted, from left to right. Parameter regNew is passed
116376** zero in this case.
116377**
116378** For an INSERT operation, regOld is passed zero and regNew is passed the
116379** first register of an array of (pTab->nCol+1) registers containing the new
116380** row data.
116381**
116382** For an UPDATE operation, this function is called twice. Once before
116383** the original record is deleted from the table using the calling convention
116384** described for DELETE. Then again after the original record is deleted
116385** but before the new record is inserted using the INSERT convention.
116386*/
116387SQLITE_PRIVATEstatic void sqlite3FkCheck(
116388 Parse *pParse, /* Parse context */
116389 Table *pTab, /* Row is being deleted from this table */
116390 int regOld, /* Previous row data is stored here */
116391 int regNew, /* New row data is stored here */
116392 int *aChange, /* Array indicating UPDATEd columns (or 0) */
116393 int bChngRowid /* True if rowid is UPDATEd */
116394){
116395 sqlite3 *db = pParse->db; /* Database handle */
116396 FKey *pFKey; /* Used to iterate through FKs */
116397 int iDb; /* Index of database containing pTab */
116398 const char *zDb; /* Name of database containing pTab */
116399 int isIgnoreErrors = pParse->disableTriggers;
116400
116401 /* Exactly one of regOld and regNew should be non-zero. */
116402 assert( (regOld==0)!=(regNew==0) )((void) (0));
116403
116404 /* If foreign-keys are disabled, this function is a no-op. */
116405 if( (db->flags&SQLITE_ForeignKeys0x00004000)==0 ) return;
116406
116407 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
116408 zDb = db->aDb[iDb].zDbSName;
116409
116410 /* Loop through all the foreign key constraints for which pTab is the
116411 ** child table (the table that the foreign key definition is part of). */
116412 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
116413 Table *pTo; /* Parent table of foreign key pFKey */
116414 Index *pIdx = 0; /* Index on key columns in pTo */
116415 int *aiFree = 0;
116416 int *aiCol;
116417 int iCol;
116418 int i;
116419 int bIgnore = 0;
116420
116421 if( aChange
116422 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
116423 && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
116424 ){
116425 continue;
116426 }
116427
116428 /* Find the parent table of this foreign key. Also find a unique index
116429 ** on the parent key columns in the parent table. If either of these
116430 ** schema items cannot be located, set an error in pParse and return
116431 ** early. */
116432 if( pParse->disableTriggers ){
116433 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
116434 }else{
116435 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
116436 }
116437 if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
116438 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) )((void) (0));
116439 if( !isIgnoreErrors || db->mallocFailed ) return;
116440 if( pTo==0 ){
116441 /* If isIgnoreErrors is true, then a table is being dropped. In this
116442 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
116443 ** before actually dropping it in order to check FK constraints.
116444 ** If the parent table of an FK constraint on the current table is
116445 ** missing, behave as if it is empty. i.e. decrement the relevant
116446 ** FK counter for each row of the current table with non-NULL keys.
116447 */
116448 Vdbe *v = sqlite3GetVdbe(pParse);
116449 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
116450 for(i=0; i<pFKey->nCol; i++){
116451 int iReg = pFKey->aCol[i].iFrom + regOld + 1;
116452 sqlite3VdbeAddOp2(v, OP_IsNull50, iReg, iJump); VdbeCoverage(v);
116453 }
116454 sqlite3VdbeAddOp2(v, OP_FkCounter150, pFKey->isDeferred, -1);
116455 }
116456 continue;
116457 }
116458 assert( pFKey->nCol==1 || (aiFree && pIdx) )((void) (0));
116459
116460 if( aiFree ){
116461 aiCol = aiFree;
116462 }else{
116463 iCol = pFKey->aCol[0].iFrom;
116464 aiCol = &iCol;
116465 }
116466 for(i=0; i<pFKey->nCol; i++){
116467 if( aiCol[i]==pTab->iPKey ){
116468 aiCol[i] = -1;
116469 }
116470 assert( pIdx==0 || pIdx->aiColumn[i]>=0 )((void) (0));
116471#ifndef SQLITE_OMIT_AUTHORIZATION
116472 /* Request permission to read the parent key columns. If the
116473 ** authorization callback returns SQLITE_IGNORE, behave as if any
116474 ** values read from the parent table are NULL. */
116475 if( db->xAuth ){
116476 int rcauth;
116477 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
116478 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
116479 bIgnore = (rcauth==SQLITE_IGNORE2);
116480 }
116481#endif
116482 }
116483
116484 /* Take a shared-cache advisory read-lock on the parent table. Allocate
116485 ** a cursor to use to search the unique index on the parent key columns
116486 ** in the parent table. */
116487 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
116488 pParse->nTab++;
116489
116490 if( regOld!=0 ){
116491 /* A row is being removed from the child table. Search for the parent.
116492 ** If the parent does not exist, removing the child row resolves an
116493 ** outstanding foreign key constraint violation. */
116494 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
116495 }
116496 if( regNew!=0 && !isSetNullAction(pParse, pFKey) ){
116497 /* A row is being added to the child table. If a parent row cannot
116498 ** be found, adding the child row has violated the FK constraint.
116499 **
116500 ** If this operation is being performed as part of a trigger program
116501 ** that is actually a "SET NULL" action belonging to this very
116502 ** foreign key, then omit this scan altogether. As all child key
116503 ** values are guaranteed to be NULL, it is not possible for adding
116504 ** this row to cause an FK violation. */
116505 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1, bIgnore);
116506 }
116507
116508 sqlite3DbFree(db, aiFree);
116509 }
116510
116511 /* Loop through all the foreign key constraints that refer to this table.
116512 ** (the "child" constraints) */
116513 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
116514 Index *pIdx = 0; /* Foreign key index for pFKey */
116515 SrcList *pSrc;
116516 int *aiCol = 0;
116517
116518 if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
116519 continue;
116520 }
116521
116522 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs0x00080000)
116523 && !pParse->pToplevel && !pParse->isMultiWrite
116524 ){
116525 assert( regOld==0 && regNew!=0 )((void) (0));
116526 /* Inserting a single row into a parent table cannot cause (or fix)
116527 ** an immediate foreign key violation. So do nothing in this case. */
116528 continue;
116529 }
116530
116531 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
116532 if( !isIgnoreErrors || db->mallocFailed ) return;
116533 continue;
116534 }
116535 assert( aiCol || pFKey->nCol==1 )((void) (0));
116536
116537 /* Create a SrcList structure containing the child table. We need the
116538 ** child table as a SrcList for sqlite3WhereBegin() */
116539 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
116540 if( pSrc ){
116541 struct SrcList_item *pItem = pSrc->a;
116542 pItem->pTab = pFKey->pFrom;
116543 pItem->zName = pFKey->pFrom->zName;
116544 pItem->pTab->nTabRef++;
116545 pItem->iCursor = pParse->nTab++;
116546
116547 if( regNew!=0 ){
116548 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
116549 }
116550 if( regOld!=0 ){
116551 int eAction = pFKey->aAction[aChange!=0];
116552 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
116553 /* If this is a deferred FK constraint, or a CASCADE or SET NULL
116554 ** action applies, then any foreign key violations caused by
116555 ** removing the parent key will be rectified by the action trigger.
116556 ** So do not set the "may-abort" flag in this case.
116557 **
116558 ** Note 1: If the FK is declared "ON UPDATE CASCADE", then the
116559 ** may-abort flag will eventually be set on this statement anyway
116560 ** (when this function is called as part of processing the UPDATE
116561 ** within the action trigger).
116562 **
116563 ** Note 2: At first glance it may seem like SQLite could simply omit
116564 ** all OP_FkCounter related scans when either CASCADE or SET NULL
116565 ** applies. The trouble starts if the CASCADE or SET NULL action
116566 ** trigger causes other triggers or action rules attached to the
116567 ** child table to fire. In these cases the fk constraint counters
116568 ** might be set incorrectly if any OP_FkCounter related scans are
116569 ** omitted. */
116570 if( !pFKey->isDeferred && eAction!=OE_Cascade10 && eAction!=OE_SetNull8 ){
116571 sqlite3MayAbort(pParse);
116572 }
116573 }
116574 pItem->zName = 0;
116575 sqlite3SrcListDelete(db, pSrc);
116576 }
116577 sqlite3DbFree(db, aiCol);
116578 }
116579}
116580
116581#define COLUMN_MASK(x)(((x)>31) ? 0xffffffff : ((u32)1<<(x))) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
116582
116583/*
116584** This function is called before generating code to update or delete a
116585** row contained in table pTab.
116586*/
116587SQLITE_PRIVATEstatic u32 sqlite3FkOldmask(
116588 Parse *pParse, /* Parse context */
116589 Table *pTab /* Table being modified */
116590){
116591 u32 mask = 0;
116592 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 ){
116593 FKey *p;
116594 int i;
116595 for(p=pTab->pFKey; p; p=p->pNextFrom){
116596 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom)(((p->aCol[i].iFrom)>31) ? 0xffffffff : ((u32)1<<
(p->aCol[i].iFrom)))
;
116597 }
116598 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
116599 Index *pIdx = 0;
116600 sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
116601 if( pIdx ){
116602 for(i=0; i<pIdx->nKeyCol; i++){
116603 assert( pIdx->aiColumn[i]>=0 )((void) (0));
116604 mask |= COLUMN_MASK(pIdx->aiColumn[i])(((pIdx->aiColumn[i])>31) ? 0xffffffff : ((u32)1<<
(pIdx->aiColumn[i])))
;
116605 }
116606 }
116607 }
116608 }
116609 return mask;
116610}
116611
116612
116613/*
116614** This function is called before generating code to update or delete a
116615** row contained in table pTab. If the operation is a DELETE, then
116616** parameter aChange is passed a NULL value. For an UPDATE, aChange points
116617** to an array of size N, where N is the number of columns in table pTab.
116618** If the i'th column is not modified by the UPDATE, then the corresponding
116619** entry in the aChange[] array is set to -1. If the column is modified,
116620** the value is 0 or greater. Parameter chngRowid is set to true if the
116621** UPDATE statement modifies the rowid fields of the table.
116622**
116623** If any foreign key processing will be required, this function returns
116624** non-zero. If there is no foreign key related processing, this function
116625** returns zero.
116626**
116627** For an UPDATE, this function returns 2 if:
116628**
116629** * There are any FKs for which pTab is the child and the parent table, or
116630** * the UPDATE modifies one or more parent keys for which the action is
116631** not "NO ACTION" (i.e. is CASCADE, SET DEFAULT or SET NULL).
116632**
116633** Or, assuming some other foreign key processing is required, 1.
116634*/
116635SQLITE_PRIVATEstatic int sqlite3FkRequired(
116636 Parse *pParse, /* Parse context */
116637 Table *pTab, /* Table being modified */
116638 int *aChange, /* Non-NULL for UPDATE operations */
116639 int chngRowid /* True for UPDATE that affects rowid */
116640){
116641 int eRet = 0;
116642 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 ){
116643 if( !aChange ){
116644 /* A DELETE operation. Foreign key processing is required if the
116645 ** table in question is either the child or parent table for any
116646 ** foreign key constraint. */
116647 eRet = (sqlite3FkReferences(pTab) || pTab->pFKey);
116648 }else{
116649 /* This is an UPDATE. Foreign key processing is only required if the
116650 ** operation modifies one or more child or parent key columns. */
116651 FKey *p;
116652
116653 /* Check if any child key columns are being modified. */
116654 for(p=pTab->pFKey; p; p=p->pNextFrom){
116655 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) return 2;
116656 if( fkChildIsModified(pTab, p, aChange, chngRowid) ){
116657 eRet = 1;
116658 }
116659 }
116660
116661 /* Check if any parent key columns are being modified. */
116662 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
116663 if( fkParentIsModified(pTab, p, aChange, chngRowid) ){
116664 if( p->aAction[1]!=OE_None0 ) return 2;
116665 eRet = 1;
116666 }
116667 }
116668 }
116669 }
116670 return eRet;
116671}
116672
116673/*
116674** This function is called when an UPDATE or DELETE operation is being
116675** compiled on table pTab, which is the parent table of foreign-key pFKey.
116676** If the current operation is an UPDATE, then the pChanges parameter is
116677** passed a pointer to the list of columns being modified. If it is a
116678** DELETE, pChanges is passed a NULL pointer.
116679**
116680** It returns a pointer to a Trigger structure containing a trigger
116681** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
116682** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
116683** returned (these actions require no special handling by the triggers
116684** sub-system, code for them is created by fkScanChildren()).
116685**
116686** For example, if pFKey is the foreign key and pTab is table "p" in
116687** the following schema:
116688**
116689** CREATE TABLE p(pk PRIMARY KEY);
116690** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
116691**
116692** then the returned trigger structure is equivalent to:
116693**
116694** CREATE TRIGGER ... DELETE ON p BEGIN
116695** DELETE FROM c WHERE ck = old.pk;
116696** END;
116697**
116698** The returned pointer is cached as part of the foreign key object. It
116699** is eventually freed along with the rest of the foreign key object by
116700** sqlite3FkDelete().
116701*/
116702static Trigger *fkActionTrigger(
116703 Parse *pParse, /* Parse context */
116704 Table *pTab, /* Table being updated or deleted from */
116705 FKey *pFKey, /* Foreign key to get action for */
116706 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
116707){
116708 sqlite3 *db = pParse->db; /* Database handle */
116709 int action; /* One of OE_None, OE_Cascade etc. */
116710 Trigger *pTrigger; /* Trigger definition to return */
116711 int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */
116712
116713 action = pFKey->aAction[iAction];
116714 if( action==OE_Restrict7 && (db->flags & SQLITE_DeferFKs0x00080000) ){
116715 return 0;
116716 }
116717 pTrigger = pFKey->apTrigger[iAction];
116718
116719 if( action!=OE_None0 && !pTrigger ){
116720 char const *zFrom; /* Name of child table */
116721 int nFrom; /* Length in bytes of zFrom */
116722 Index *pIdx = 0; /* Parent key index for this FK */
116723 int *aiCol = 0; /* child table cols -> parent key cols */
116724 TriggerStep *pStep = 0; /* First (only) step of trigger program */
116725 Expr *pWhere = 0; /* WHERE clause of trigger step */
116726 ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */
116727 Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */
116728 int i; /* Iterator variable */
116729 Expr *pWhen = 0; /* WHEN clause for the trigger */
116730
116731 if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
116732 assert( aiCol || pFKey->nCol==1 )((void) (0));
116733
116734 for(i=0; i<pFKey->nCol; i++){
116735 Token tOld = { "old", 3 }; /* Literal "old" token */
116736 Token tNew = { "new", 3 }; /* Literal "new" token */
116737 Token tFromCol; /* Name of column in child table */
116738 Token tToCol; /* Name of column in parent table */
116739 int iFromCol; /* Idx of column in child table */
116740 Expr *pEq; /* tFromCol = OLD.tToCol */
116741
116742 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
116743 assert( iFromCol>=0 )((void) (0));
116744 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) )((void) (0));
116745 assert( pIdx==0 || pIdx->aiColumn[i]>=0 )((void) (0));
116746 sqlite3TokenInit(&tToCol,
116747 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
116748 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
116749
116750 /* Create the expression "OLD.zToCol = zFromCol". It is important
116751 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
116752 ** that the affinity and collation sequence associated with the
116753 ** parent table are used for the comparison. */
116754 pEq = sqlite3PExpr(pParse, TK_EQ53,
116755 sqlite3PExpr(pParse, TK_DOT134,
116756 sqlite3ExprAlloc(db, TK_ID59, &tOld, 0),
116757 sqlite3ExprAlloc(db, TK_ID59, &tToCol, 0)),
116758 sqlite3ExprAlloc(db, TK_ID59, &tFromCol, 0)
116759 );
116760 pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
116761
116762 /* For ON UPDATE, construct the next term of the WHEN clause.
116763 ** The final WHEN clause will be like this:
116764 **
116765 ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
116766 */
116767 if( pChanges ){
116768 pEq = sqlite3PExpr(pParse, TK_IS45,
116769 sqlite3PExpr(pParse, TK_DOT134,
116770 sqlite3ExprAlloc(db, TK_ID59, &tOld, 0),
116771 sqlite3ExprAlloc(db, TK_ID59, &tToCol, 0)),
116772 sqlite3PExpr(pParse, TK_DOT134,
116773 sqlite3ExprAlloc(db, TK_ID59, &tNew, 0),
116774 sqlite3ExprAlloc(db, TK_ID59, &tToCol, 0))
116775 );
116776 pWhen = sqlite3ExprAnd(pParse, pWhen, pEq);
116777 }
116778
116779 if( action!=OE_Restrict7 && (action!=OE_Cascade10 || pChanges) ){
116780 Expr *pNew;
116781 if( action==OE_Cascade10 ){
116782 pNew = sqlite3PExpr(pParse, TK_DOT134,
116783 sqlite3ExprAlloc(db, TK_ID59, &tNew, 0),
116784 sqlite3ExprAlloc(db, TK_ID59, &tToCol, 0));
116785 }else if( action==OE_SetDflt9 ){
116786 Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
116787 if( pDflt ){
116788 pNew = sqlite3ExprDup(db, pDflt, 0);
116789 }else{
116790 pNew = sqlite3ExprAlloc(db, TK_NULL114, 0, 0);
116791 }
116792 }else{
116793 pNew = sqlite3ExprAlloc(db, TK_NULL114, 0, 0);
116794 }
116795 pList = sqlite3ExprListAppend(pParse, pList, pNew);
116796 sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
116797 }
116798 }
116799 sqlite3DbFree(db, aiCol);
116800
116801 zFrom = pFKey->pFrom->zName;
116802 nFrom = sqlite3Strlen30(zFrom);
116803
116804 if( action==OE_Restrict7 ){
116805 Token tFrom;
116806 Expr *pRaise;
116807
116808 tFrom.z = zFrom;
116809 tFrom.n = nFrom;
116810 pRaise = sqlite3Expr(db, TK_RAISE71, "FOREIGN KEY constraint failed");
116811 if( pRaise ){
116812 pRaise->affinity = OE_Abort2;
116813 }
116814 pSelect = sqlite3SelectNew(pParse,
116815 sqlite3ExprListAppend(pParse, 0, pRaise),
116816 sqlite3SrcListAppend(pParse, 0, &tFrom, 0),
116817 pWhere,
116818 0, 0, 0, 0, 0
116819 );
116820 pWhere = 0;
116821 }
116822
116823 /* Disable lookaside memory allocation */
116824 db->lookaside.bDisable++;
116825
116826 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
116827 sizeof(Trigger) + /* struct Trigger */
116828 sizeof(TriggerStep) + /* Single step in trigger program */
116829 nFrom + 1 /* Space for pStep->zTarget */
116830 );
116831 if( pTrigger ){
116832 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
116833 pStep->zTarget = (char *)&pStep[1];
116834 memcpy((char *)pStep->zTarget, zFrom, nFrom);
116835
116836 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
116837 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE0x0001);
116838 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
116839 if( pWhen ){
116840 pWhen = sqlite3PExpr(pParse, TK_NOT19, pWhen, 0);
116841 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE0x0001);
116842 }
116843 }
116844
116845 /* Re-enable the lookaside buffer, if it was disabled earlier. */
116846 db->lookaside.bDisable--;
116847
116848 sqlite3ExprDelete(db, pWhere);
116849 sqlite3ExprDelete(db, pWhen);
116850 sqlite3ExprListDelete(db, pList);
116851 sqlite3SelectDelete(db, pSelect);
116852 if( db->mallocFailed==1 ){
116853 fkTriggerDelete(db, pTrigger);
116854 return 0;
116855 }
116856 assert( pStep!=0 )((void) (0));
116857
116858 switch( action ){
116859 case OE_Restrict7:
116860 pStep->op = TK_SELECT131;
116861 break;
116862 case OE_Cascade10:
116863 if( !pChanges ){
116864 pStep->op = TK_DELETE121;
116865 break;
116866 }
116867 default:
116868 pStep->op = TK_UPDATE122;
116869 }
116870 pStep->pTrig = pTrigger;
116871 pTrigger->pSchema = pTab->pSchema;
116872 pTrigger->pTabSchema = pTab->pSchema;
116873 pFKey->apTrigger[iAction] = pTrigger;
116874 pTrigger->op = (pChanges ? TK_UPDATE122 : TK_DELETE121);
116875 }
116876
116877 return pTrigger;
116878}
116879
116880/*
116881** This function is called when deleting or updating a row to implement
116882** any required CASCADE, SET NULL or SET DEFAULT actions.
116883*/
116884SQLITE_PRIVATEstatic void sqlite3FkActions(
116885 Parse *pParse, /* Parse context */
116886 Table *pTab, /* Table being updated or deleted from */
116887 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
116888 int regOld, /* Address of array containing old row */
116889 int *aChange, /* Array indicating UPDATEd columns (or 0) */
116890 int bChngRowid /* True if rowid is UPDATEd */
116891){
116892 /* If foreign-key support is enabled, iterate through all FKs that
116893 ** refer to table pTab. If there is an action associated with the FK
116894 ** for this operation (either update or delete), invoke the associated
116895 ** trigger sub-program. */
116896 if( pParse->db->flags&SQLITE_ForeignKeys0x00004000 ){
116897 FKey *pFKey; /* Iterator variable */
116898 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
116899 if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
116900 Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
116901 if( pAct ){
116902 sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort2, 0);
116903 }
116904 }
116905 }
116906 }
116907}
116908
116909#endif /* ifndef SQLITE_OMIT_TRIGGER */
116910
116911/*
116912** Free all memory associated with foreign key definitions attached to
116913** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
116914** hash table.
116915*/
116916SQLITE_PRIVATEstatic void sqlite3FkDelete(sqlite3 *db, Table *pTab){
116917 FKey *pFKey; /* Iterator variable */
116918 FKey *pNext; /* Copy of pFKey->pNextFrom */
116919
116920 assert( db==0 || IsVirtual(pTab)((void) (0))
116921 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) )((void) (0));
116922 for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
116923
116924 /* Remove the FK from the fkeyHash hash table. */
116925 if( !db || db->pnBytesFreed==0 ){
116926 if( pFKey->pPrevTo ){
116927 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
116928 }else{
116929 void *p = (void *)pFKey->pNextTo;
116930 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
116931 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
116932 }
116933 if( pFKey->pNextTo ){
116934 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
116935 }
116936 }
116937
116938 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
116939 ** classified as either immediate or deferred.
116940 */
116941 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 )((void) (0));
116942
116943 /* Delete any triggers created to implement actions for this FK. */
116944#ifndef SQLITE_OMIT_TRIGGER
116945 fkTriggerDelete(db, pFKey->apTrigger[0]);
116946 fkTriggerDelete(db, pFKey->apTrigger[1]);
116947#endif
116948
116949 pNext = pFKey->pNextFrom;
116950 sqlite3DbFree(db, pFKey);
116951 }
116952}
116953#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
116954
116955/************** End of fkey.c ************************************************/
116956/************** Begin file insert.c ******************************************/
116957/*
116958** 2001 September 15
116959**
116960** The author disclaims copyright to this source code. In place of
116961** a legal notice, here is a blessing:
116962**
116963** May you do good and not evil.
116964** May you find forgiveness for yourself and forgive others.
116965** May you share freely, never taking more than you give.
116966**
116967*************************************************************************
116968** This file contains C code routines that are called by the parser
116969** to handle INSERT statements in SQLite.
116970*/
116971/* #include "sqliteInt.h" */
116972
116973/*
116974** Generate code that will
116975**
116976** (1) acquire a lock for table pTab then
116977** (2) open pTab as cursor iCur.
116978**
116979** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
116980** for that table that is actually opened.
116981*/
116982SQLITE_PRIVATEstatic void sqlite3OpenTable(
116983 Parse *pParse, /* Generate code into this VDBE */
116984 int iCur, /* The cursor number of the table */
116985 int iDb, /* The database index in sqlite3.aDb[] */
116986 Table *pTab, /* The table to be opened */
116987 int opcode /* OP_OpenRead or OP_OpenWrite */
116988){
116989 Vdbe *v;
116990 assert( !IsVirtual(pTab) )((void) (0));
116991 v = sqlite3GetVdbe(pParse);
116992 assert( opcode==OP_OpenWrite || opcode==OP_OpenRead )((void) (0));
116993 sqlite3TableLock(pParse, iDb, pTab->tnum,
116994 (opcode==OP_OpenWrite109)?1:0, pTab->zName);
116995 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
116996 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
116997 VdbeComment((v, "%s", pTab->zName));
116998 }else{
116999 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
117000 assert( pPk!=0 )((void) (0));
117001 assert( pPk->tnum==pTab->tnum )((void) (0));
117002 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
117003 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
117004 VdbeComment((v, "%s", pTab->zName));
117005 }
117006}
117007
117008/*
117009** Return a pointer to the column affinity string associated with index
117010** pIdx. A column affinity string has one character for each column in
117011** the table, according to the affinity of the column:
117012**
117013** Character Column affinity
117014** ------------------------------
117015** 'A' BLOB
117016** 'B' TEXT
117017** 'C' NUMERIC
117018** 'D' INTEGER
117019** 'F' REAL
117020**
117021** An extra 'D' is appended to the end of the string to cover the
117022** rowid that appears as the last column in every index.
117023**
117024** Memory for the buffer containing the column index affinity string
117025** is managed along with the rest of the Index structure. It will be
117026** released when sqlite3DeleteIndex() is called.
117027*/
117028SQLITE_PRIVATEstatic const char *sqlite3IndexAffinityStr(sqlite3 *db, Index *pIdx){
117029 if( !pIdx->zColAff ){
117030 /* The first time a column affinity string for a particular index is
117031 ** required, it is allocated and populated here. It is then stored as
117032 ** a member of the Index structure for subsequent use.
117033 **
117034 ** The column affinity string will eventually be deleted by
117035 ** sqliteDeleteIndex() when the Index structure itself is cleaned
117036 ** up.
117037 */
117038 int n;
117039 Table *pTab = pIdx->pTable;
117040 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
117041 if( !pIdx->zColAff ){
117042 sqlite3OomFault(db);
117043 return 0;
117044 }
117045 for(n=0; n<pIdx->nColumn; n++){
117046 i16 x = pIdx->aiColumn[n];
117047 if( x>=0 ){
117048 pIdx->zColAff[n] = pTab->aCol[x].affinity;
117049 }else if( x==XN_ROWID(-1) ){
117050 pIdx->zColAff[n] = SQLITE_AFF_INTEGER'D';
117051 }else{
117052 char aff;
117053 assert( x==XN_EXPR )((void) (0));
117054 assert( pIdx->aColExpr!=0 )((void) (0));
117055 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
117056 if( aff==0 ) aff = SQLITE_AFF_BLOB'A';
117057 pIdx->zColAff[n] = aff;
117058 }
117059 }
117060 pIdx->zColAff[n] = 0;
117061 }
117062
117063 return pIdx->zColAff;
117064}
117065
117066/*
117067** Compute the affinity string for table pTab, if it has not already been
117068** computed. As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
117069**
117070** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
117071** if iReg>0 then code an OP_Affinity opcode that will set the affinities
117072** for register iReg and following. Or if affinities exists and iReg==0,
117073** then just set the P4 operand of the previous opcode (which should be
117074** an OP_MakeRecord) to the affinity string.
117075**
117076** A column affinity string has one character per column:
117077**
117078** Character Column affinity
117079** ------------------------------
117080** 'A' BLOB
117081** 'B' TEXT
117082** 'C' NUMERIC
117083** 'D' INTEGER
117084** 'E' REAL
117085*/
117086SQLITE_PRIVATEstatic void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
117087 int i;
117088 char *zColAff = pTab->zColAff;
117089 if( zColAff==0 ){
117090 sqlite3 *db = sqlite3VdbeDb(v);
117091 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
117092 if( !zColAff ){
117093 sqlite3OomFault(db);
117094 return;
117095 }
117096
117097 for(i=0; i<pTab->nCol; i++){
117098 zColAff[i] = pTab->aCol[i].affinity;
117099 }
117100 do{
117101 zColAff[i--] = 0;
117102 }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB'A' );
117103 pTab->zColAff = zColAff;
117104 }
117105 assert( zColAff!=0 )((void) (0));
117106 i = sqlite3Strlen30NN(zColAff)(strlen(zColAff)&0x3fffffff);
117107 if( i ){
117108 if( iReg ){
117109 sqlite3VdbeAddOp4(v, OP_Affinity91, iReg, i, 0, zColAff, i);
117110 }else{
117111 sqlite3VdbeChangeP4(v, -1, zColAff, i);
117112 }
117113 }
117114}
117115
117116/*
117117** Return non-zero if the table pTab in database iDb or any of its indices
117118** have been opened at any point in the VDBE program. This is used to see if
117119** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
117120** run without using a temporary table for the results of the SELECT.
117121*/
117122static int readsTable(Parse *p, int iDb, Table *pTab){
117123 Vdbe *v = sqlite3GetVdbe(p);
117124 int i;
117125 int iEnd = sqlite3VdbeCurrentAddr(v);
117126#ifndef SQLITE_OMIT_VIRTUALTABLE
117127 VTable *pVTab = IsVirtual(pTab)((pTab)->nModuleArg) ? sqlite3GetVTable(p->db, pTab) : 0;
117128#endif
117129
117130 for(i=1; i<iEnd; i++){
117131 VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
117132 assert( pOp!=0 )((void) (0));
117133 if( pOp->opcode==OP_OpenRead108 && pOp->p3==iDb ){
117134 Index *pIndex;
117135 int tnum = pOp->p2;
117136 if( tnum==pTab->tnum ){
117137 return 1;
117138 }
117139 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
117140 if( tnum==pIndex->tnum ){
117141 return 1;
117142 }
117143 }
117144 }
117145#ifndef SQLITE_OMIT_VIRTUALTABLE
117146 if( pOp->opcode==OP_VOpen163 && pOp->p4.pVtab==pVTab ){
117147 assert( pOp->p4.pVtab!=0 )((void) (0));
117148 assert( pOp->p4type==P4_VTAB )((void) (0));
117149 return 1;
117150 }
117151#endif
117152 }
117153 return 0;
117154}
117155
117156#ifndef SQLITE_OMIT_AUTOINCREMENT
117157/*
117158** Locate or create an AutoincInfo structure associated with table pTab
117159** which is in database iDb. Return the register number for the register
117160** that holds the maximum rowid. Return zero if pTab is not an AUTOINCREMENT
117161** table. (Also return zero when doing a VACUUM since we do not want to
117162** update the AUTOINCREMENT counters during a VACUUM.)
117163**
117164** There is at most one AutoincInfo structure per table even if the
117165** same table is autoincremented multiple times due to inserts within
117166** triggers. A new AutoincInfo structure is created if this is the
117167** first use of table pTab. On 2nd and subsequent uses, the original
117168** AutoincInfo structure is used.
117169**
117170** Four consecutive registers are allocated:
117171**
117172** (1) The name of the pTab table.
117173** (2) The maximum ROWID of pTab.
117174** (3) The rowid in sqlite_sequence of pTab
117175** (4) The original value of the max ROWID in pTab, or NULL if none
117176**
117177** The 2nd register is the one that is returned. That is all the
117178** insert routine needs to know about.
117179*/
117180static int autoIncBegin(
117181 Parse *pParse, /* Parsing context */
117182 int iDb, /* Index of the database holding pTab */
117183 Table *pTab /* The table we are writing to */
117184){
117185 int memId = 0; /* Register holding maximum rowid */
117186 assert( pParse->db->aDb[iDb].pSchema!=0 )((void) (0));
117187 if( (pTab->tabFlags & TF_Autoincrement0x0008)!=0
117188 && (pParse->db->mDbFlags & DBFLAG_Vacuum0x0004)==0
117189 ){
117190 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
117191 AutoincInfo *pInfo;
117192 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
117193
117194 /* Verify that the sqlite_sequence table exists and is an ordinary
117195 ** rowid table with exactly two columns.
117196 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
117197 if( pSeqTab==0
117198 || !HasRowid(pSeqTab)(((pSeqTab)->tabFlags & 0x0020)==0)
117199 || IsVirtual(pSeqTab)((pSeqTab)->nModuleArg)
117200 || pSeqTab->nCol!=2
117201 ){
117202 pParse->nErr++;
117203 pParse->rc = SQLITE_CORRUPT_SEQUENCE(11 | (2<<8));
117204 return 0;
117205 }
117206
117207 pInfo = pToplevel->pAinc;
117208 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
117209 if( pInfo==0 ){
117210 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
117211 if( pInfo==0 ) return 0;
117212 pInfo->pNext = pToplevel->pAinc;
117213 pToplevel->pAinc = pInfo;
117214 pInfo->pTab = pTab;
117215 pInfo->iDb = iDb;
117216 pToplevel->nMem++; /* Register to hold name of table */
117217 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
117218 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
117219 }
117220 memId = pInfo->regCtr;
117221 }
117222 return memId;
117223}
117224
117225/*
117226** This routine generates code that will initialize all of the
117227** register used by the autoincrement tracker.
117228*/
117229SQLITE_PRIVATEstatic void sqlite3AutoincrementBegin(Parse *pParse){
117230 AutoincInfo *p; /* Information about an AUTOINCREMENT */
117231 sqlite3 *db = pParse->db; /* The database connection */
117232 Db *pDb; /* Database only autoinc table */
117233 int memId; /* Register holding max rowid */
117234 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
117235
117236 /* This routine is never called during trigger-generation. It is
117237 ** only called from the top-level */
117238 assert( pParse->pTriggerTab==0 )((void) (0));
117239 assert( sqlite3IsToplevel(pParse) )((void) (0));
117240
117241 assert( v )((void) (0)); /* We failed long ago if this is not so */
117242 for(p = pParse->pAinc; p; p = p->pNext){
117243 static const int iLn = VDBE_OFFSET_LINENO(2)0;
117244 static const VdbeOpList autoInc[] = {
117245 /* 0 */ {OP_Null73, 0, 0, 0},
117246 /* 1 */ {OP_Rewind36, 0, 10, 0},
117247 /* 2 */ {OP_Column90, 0, 0, 0},
117248 /* 3 */ {OP_Ne52, 0, 9, 0},
117249 /* 4 */ {OP_Rowid128, 0, 0, 0},
117250 /* 5 */ {OP_Column90, 0, 1, 0},
117251 /* 6 */ {OP_AddImm83, 0, 0, 0},
117252 /* 7 */ {OP_Copy78, 0, 0, 0},
117253 /* 8 */ {OP_Goto11, 0, 11, 0},
117254 /* 9 */ {OP_Next5, 0, 2, 0},
117255 /* 10 */ {OP_Integer70, 0, 0, 0},
117256 /* 11 */ {OP_Close117, 0, 0, 0}
117257 };
117258 VdbeOp *aOp;
117259 pDb = &db->aDb[p->iDb];
117260 memId = p->regCtr;
117261 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) )((void) (0));
117262 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead108);
117263 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
117264 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc)((int)(sizeof(autoInc)/sizeof(autoInc[0]))), autoInc, iLn);
117265 if( aOp==0 ) break;
117266 aOp[0].p2 = memId;
117267 aOp[0].p3 = memId+2;
117268 aOp[2].p3 = memId;
117269 aOp[3].p1 = memId-1;
117270 aOp[3].p3 = memId;
117271 aOp[3].p5 = SQLITE_JUMPIFNULL0x10;
117272 aOp[4].p2 = memId+1;
117273 aOp[5].p3 = memId;
117274 aOp[6].p1 = memId;
117275 aOp[7].p2 = memId+2;
117276 aOp[7].p1 = memId;
117277 aOp[10].p2 = memId;
117278 if( pParse->nTab==0 ) pParse->nTab = 1;
117279 }
117280}
117281
117282/*
117283** Update the maximum rowid for an autoincrement calculation.
117284**
117285** This routine should be called when the regRowid register holds a
117286** new rowid that is about to be inserted. If that new rowid is
117287** larger than the maximum rowid in the memId memory cell, then the
117288** memory cell is updated.
117289*/
117290static void autoIncStep(Parse *pParse, int memId, int regRowid){
117291 if( memId>0 ){
117292 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax151, memId, regRowid);
117293 }
117294}
117295
117296/*
117297** This routine generates the code needed to write autoincrement
117298** maximum rowid values back into the sqlite_sequence register.
117299** Every statement that might do an INSERT into an autoincrement
117300** table (either directly or through triggers) needs to call this
117301** routine just before the "exit" code.
117302*/
117303static SQLITE_NOINLINE__attribute__((noinline)) void autoIncrementEnd(Parse *pParse){
117304 AutoincInfo *p;
117305 Vdbe *v = pParse->pVdbe;
117306 sqlite3 *db = pParse->db;
117307
117308 assert( v )((void) (0));
117309 for(p = pParse->pAinc; p; p = p->pNext){
117310 static const int iLn = VDBE_OFFSET_LINENO(2)0;
117311 static const VdbeOpList autoIncEnd[] = {
117312 /* 0 */ {OP_NotNull51, 0, 2, 0},
117313 /* 1 */ {OP_NewRowid121, 0, 0, 0},
117314 /* 2 */ {OP_MakeRecord92, 0, 2, 0},
117315 /* 3 */ {OP_Insert122, 0, 0, 0},
117316 /* 4 */ {OP_Close117, 0, 0, 0}
117317 };
117318 VdbeOp *aOp;
117319 Db *pDb = &db->aDb[p->iDb];
117320 int iRec;
117321 int memId = p->regCtr;
117322
117323 iRec = sqlite3GetTempReg(pParse);
117324 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) )((void) (0));
117325 sqlite3VdbeAddOp3(v, OP_Le55, memId+2, sqlite3VdbeCurrentAddr(v)+7, memId);
117326 VdbeCoverage(v);
117327 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite109);
117328 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd)((int)(sizeof(autoIncEnd)/sizeof(autoIncEnd[0]))), autoIncEnd, iLn);
117329 if( aOp==0 ) break;
117330 aOp[0].p1 = memId+1;
117331 aOp[1].p2 = memId+1;
117332 aOp[2].p1 = memId-1;
117333 aOp[2].p3 = iRec;
117334 aOp[3].p2 = iRec;
117335 aOp[3].p3 = memId+1;
117336 aOp[3].p5 = OPFLAG_APPEND0x08;
117337 sqlite3ReleaseTempReg(pParse, iRec);
117338 }
117339}
117340SQLITE_PRIVATEstatic void sqlite3AutoincrementEnd(Parse *pParse){
117341 if( pParse->pAinc ) autoIncrementEnd(pParse);
117342}
117343#else
117344/*
117345** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
117346** above are all no-ops
117347*/
117348# define autoIncBegin(A,B,C) (0)
117349# define autoIncStep(A,B,C)
117350#endif /* SQLITE_OMIT_AUTOINCREMENT */
117351
117352
117353/* Forward declaration */
117354static int xferOptimization(
117355 Parse *pParse, /* Parser context */
117356 Table *pDest, /* The table we are inserting into */
117357 Select *pSelect, /* A SELECT statement to use as the data source */
117358 int onError, /* How to handle constraint errors */
117359 int iDbDest /* The database of pDest */
117360);
117361
117362/*
117363** This routine is called to handle SQL of the following forms:
117364**
117365** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),...
117366** insert into TABLE (IDLIST) select
117367** insert into TABLE (IDLIST) default values
117368**
117369** The IDLIST following the table name is always optional. If omitted,
117370** then a list of all (non-hidden) columns for the table is substituted.
117371** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST
117372** is omitted.
117373**
117374** For the pSelect parameter holds the values to be inserted for the
117375** first two forms shown above. A VALUES clause is really just short-hand
117376** for a SELECT statement that omits the FROM clause and everything else
117377** that follows. If the pSelect parameter is NULL, that means that the
117378** DEFAULT VALUES form of the INSERT statement is intended.
117379**
117380** The code generated follows one of four templates. For a simple
117381** insert with data coming from a single-row VALUES clause, the code executes
117382** once straight down through. Pseudo-code follows (we call this
117383** the "1st template"):
117384**
117385** open write cursor to <table> and its indices
117386** put VALUES clause expressions into registers
117387** write the resulting record into <table>
117388** cleanup
117389**
117390** The three remaining templates assume the statement is of the form
117391**
117392** INSERT INTO <table> SELECT ...
117393**
117394** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
117395** in other words if the SELECT pulls all columns from a single table
117396** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
117397** if <table2> and <table1> are distinct tables but have identical
117398** schemas, including all the same indices, then a special optimization
117399** is invoked that copies raw records from <table2> over to <table1>.
117400** See the xferOptimization() function for the implementation of this
117401** template. This is the 2nd template.
117402**
117403** open a write cursor to <table>
117404** open read cursor on <table2>
117405** transfer all records in <table2> over to <table>
117406** close cursors
117407** foreach index on <table>
117408** open a write cursor on the <table> index
117409** open a read cursor on the corresponding <table2> index
117410** transfer all records from the read to the write cursors
117411** close cursors
117412** end foreach
117413**
117414** The 3rd template is for when the second template does not apply
117415** and the SELECT clause does not read from <table> at any time.
117416** The generated code follows this template:
117417**
117418** X <- A
117419** goto B
117420** A: setup for the SELECT
117421** loop over the rows in the SELECT
117422** load values into registers R..R+n
117423** yield X
117424** end loop
117425** cleanup after the SELECT
117426** end-coroutine X
117427** B: open write cursor to <table> and its indices
117428** C: yield X, at EOF goto D
117429** insert the select result into <table> from R..R+n
117430** goto C
117431** D: cleanup
117432**
117433** The 4th template is used if the insert statement takes its
117434** values from a SELECT but the data is being inserted into a table
117435** that is also read as part of the SELECT. In the third form,
117436** we have to use an intermediate table to store the results of
117437** the select. The template is like this:
117438**
117439** X <- A
117440** goto B
117441** A: setup for the SELECT
117442** loop over the tables in the SELECT
117443** load value into register R..R+n
117444** yield X
117445** end loop
117446** cleanup after the SELECT
117447** end co-routine R
117448** B: open temp table
117449** L: yield X, at EOF goto M
117450** insert row from R..R+n into temp table
117451** goto L
117452** M: open write cursor to <table> and its indices
117453** rewind temp table
117454** C: loop over rows of intermediate table
117455** transfer values form intermediate table into <table>
117456** end loop
117457** D: cleanup
117458*/
117459SQLITE_PRIVATEstatic void sqlite3Insert(
117460 Parse *pParse, /* Parser context */
117461 SrcList *pTabList, /* Name of table into which we are inserting */
117462 Select *pSelect, /* A SELECT statement to use as the data source */
117463 IdList *pColumn, /* Column names corresponding to IDLIST. */
117464 int onError, /* How to handle constraint errors */
117465 Upsert *pUpsert /* ON CONFLICT clauses for upsert, or NULL */
117466){
117467 sqlite3 *db; /* The main database structure */
117468 Table *pTab; /* The table to insert into. aka TABLE */
117469 int i, j; /* Loop counters */
117470 Vdbe *v; /* Generate code into this virtual machine */
117471 Index *pIdx; /* For looping over indices of the table */
117472 int nColumn; /* Number of columns in the data */
117473 int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
117474 int iDataCur = 0; /* VDBE cursor that is the main data repository */
117475 int iIdxCur = 0; /* First index cursor */
117476 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
117477 int endOfLoop; /* Label for the end of the insertion loop */
117478 int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
117479 int addrInsTop = 0; /* Jump to label "D" */
117480 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
117481 SelectDest dest; /* Destination for SELECT on rhs of INSERT */
117482 int iDb; /* Index of database holding TABLE */
117483 u8 useTempTable = 0; /* Store SELECT results in intermediate table */
117484 u8 appendFlag = 0; /* True if the insert is likely to be an append */
117485 u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */
117486 u8 bIdListInOrder; /* True if IDLIST is in table order */
117487 ExprList *pList = 0; /* List of VALUES() to be inserted */
117488
117489 /* Register allocations */
117490 int regFromSelect = 0;/* Base register for data coming from SELECT */
117491 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
117492 int regRowCount = 0; /* Memory cell used for the row counter */
117493 int regIns; /* Block of regs holding rowid+data being inserted */
117494 int regRowid; /* registers holding insert rowid */
117495 int regData; /* register holding first column to insert */
117496 int *aRegIdx = 0; /* One register allocated to each index */
117497
117498#ifndef SQLITE_OMIT_TRIGGER
117499 int isView; /* True if attempting to insert into a view */
117500 Trigger *pTrigger; /* List of triggers on pTab, if required */
117501 int tmask; /* Mask of trigger times */
117502#endif
117503
117504 db = pParse->db;
117505 if( pParse->nErr || db->mallocFailed ){
117506 goto insert_cleanup;
117507 }
117508 dest.iSDParm = 0; /* Suppress a harmless compiler warning */
117509
117510 /* If the Select object is really just a simple VALUES() list with a
117511 ** single row (the common case) then keep that one row of values
117512 ** and discard the other (unused) parts of the pSelect object
117513 */
117514 if( pSelect && (pSelect->selFlags & SF_Values0x00200)!=0 && pSelect->pPrior==0 ){
117515 pList = pSelect->pEList;
117516 pSelect->pEList = 0;
117517 sqlite3SelectDelete(db, pSelect);
117518 pSelect = 0;
117519 }
117520
117521 /* Locate the table into which we will be inserting new information.
117522 */
117523 assert( pTabList->nSrc==1 )((void) (0));
117524 pTab = sqlite3SrcListLookup(pParse, pTabList);
117525 if( pTab==0 ){
117526 goto insert_cleanup;
117527 }
117528 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
117529 assert( iDb<db->nDb )((void) (0));
117530 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, pTab->zName, 0,
117531 db->aDb[iDb].zDbSName) ){
117532 goto insert_cleanup;
117533 }
117534 withoutRowid = !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0);
117535
117536 /* Figure out if we have any triggers and if the table being
117537 ** inserted into is a view
117538 */
117539#ifndef SQLITE_OMIT_TRIGGER
117540 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT120, 0, &tmask);
117541 isView = pTab->pSelect!=0;
117542#else
117543# define pTrigger 0
117544# define tmask 0
117545# define isView 0
117546#endif
117547#ifdef SQLITE_OMIT_VIEW
117548# undef isView
117549# define isView 0
117550#endif
117551 assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) )((void) (0));
117552
117553 /* If pTab is really a view, make sure it has been initialized.
117554 ** ViewGetColumnNames() is a no-op if pTab is not a view.
117555 */
117556 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
117557 goto insert_cleanup;
117558 }
117559
117560 /* Cannot insert into a read-only table.
117561 */
117562 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
117563 goto insert_cleanup;
117564 }
117565
117566 /* Allocate a VDBE
117567 */
117568 v = sqlite3GetVdbe(pParse);
117569 if( v==0 ) goto insert_cleanup;
117570 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
117571 sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
117572
117573#ifndef SQLITE_OMIT_XFER_OPT
117574 /* If the statement is of the form
117575 **
117576 ** INSERT INTO <table1> SELECT * FROM <table2>;
117577 **
117578 ** Then special optimizations can be applied that make the transfer
117579 ** very fast and which reduce fragmentation of indices.
117580 **
117581 ** This is the 2nd template.
117582 */
117583 if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
117584 assert( !pTrigger )((void) (0));
117585 assert( pList==0 )((void) (0));
117586 goto insert_end;
117587 }
117588#endif /* SQLITE_OMIT_XFER_OPT */
117589
117590 /* If this is an AUTOINCREMENT table, look up the sequence number in the
117591 ** sqlite_sequence table and store it in memory cell regAutoinc.
117592 */
117593 regAutoinc = autoIncBegin(pParse, iDb, pTab);
117594
117595 /* Allocate registers for holding the rowid of the new row,
117596 ** the content of the new row, and the assembled row record.
117597 */
117598 regRowid = regIns = pParse->nMem+1;
117599 pParse->nMem += pTab->nCol + 1;
117600 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
117601 regRowid++;
117602 pParse->nMem++;
117603 }
117604 regData = regRowid+1;
117605
117606 /* If the INSERT statement included an IDLIST term, then make sure
117607 ** all elements of the IDLIST really are columns of the table and
117608 ** remember the column indices.
117609 **
117610 ** If the table has an INTEGER PRIMARY KEY column and that column
117611 ** is named in the IDLIST, then record in the ipkColumn variable
117612 ** the index into IDLIST of the primary key column. ipkColumn is
117613 ** the index of the primary key as it appears in IDLIST, not as
117614 ** is appears in the original table. (The index of the INTEGER
117615 ** PRIMARY KEY in the original table is pTab->iPKey.)
117616 */
117617 bIdListInOrder = (pTab->tabFlags & TF_OOOHidden0x0080)==0;
117618 if( pColumn ){
117619 for(i=0; i<pColumn->nId; i++){
117620 pColumn->a[i].idx = -1;
117621 }
117622 for(i=0; i<pColumn->nId; i++){
117623 for(j=0; j<pTab->nCol; j++){
117624 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
117625 pColumn->a[i].idx = j;
117626 if( i!=j ) bIdListInOrder = 0;
117627 if( j==pTab->iPKey ){
117628 ipkColumn = i; assert( !withoutRowid )((void) (0));
117629 }
117630 break;
117631 }
117632 }
117633 if( j>=pTab->nCol ){
117634 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
117635 ipkColumn = i;
117636 bIdListInOrder = 0;
117637 }else{
117638 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
117639 pTabList, 0, pColumn->a[i].zName);
117640 pParse->checkSchema = 1;
117641 goto insert_cleanup;
117642 }
117643 }
117644 }
117645 }
117646
117647 /* Figure out how many columns of data are supplied. If the data
117648 ** is coming from a SELECT statement, then generate a co-routine that
117649 ** produces a single row of the SELECT on each invocation. The
117650 ** co-routine is the common header to the 3rd and 4th templates.
117651 */
117652 if( pSelect ){
117653 /* Data is coming from a SELECT or from a multi-row VALUES clause.
117654 ** Generate a co-routine to run the SELECT. */
117655 int regYield; /* Register holding co-routine entry-point */
117656 int addrTop; /* Top of the co-routine */
117657 int rc; /* Result code */
117658
117659 regYield = ++pParse->nMem;
117660 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
117661 sqlite3VdbeAddOp3(v, OP_InitCoroutine13, regYield, 0, addrTop);
117662 sqlite3SelectDestInit(&dest, SRT_Coroutine13, regYield);
117663 dest.iSdst = bIdListInOrder ? regData : 0;
117664 dest.nSdst = pTab->nCol;
117665 rc = sqlite3Select(pParse, pSelect, &dest);
117666 regFromSelect = dest.iSdst;
117667 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
117668 sqlite3VdbeEndCoroutine(v, regYield);
117669 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
117670 assert( pSelect->pEList )((void) (0));
117671 nColumn = pSelect->pEList->nExpr;
117672
117673 /* Set useTempTable to TRUE if the result of the SELECT statement
117674 ** should be written into a temporary table (template 4). Set to
117675 ** FALSE if each output row of the SELECT can be written directly into
117676 ** the destination table (template 3).
117677 **
117678 ** A temp table must be used if the table being updated is also one
117679 ** of the tables being read by the SELECT statement. Also use a
117680 ** temp table in the case of row triggers.
117681 */
117682 if( pTrigger || readsTable(pParse, iDb, pTab) ){
117683 useTempTable = 1;
117684 }
117685
117686 if( useTempTable ){
117687 /* Invoke the coroutine to extract information from the SELECT
117688 ** and add it to a transient table srcTab. The code generated
117689 ** here is from the 4th template:
117690 **
117691 ** B: open temp table
117692 ** L: yield X, goto M at EOF
117693 ** insert row from R..R+n into temp table
117694 ** goto L
117695 ** M: ...
117696 */
117697 int regRec; /* Register to hold packed record */
117698 int regTempRowid; /* Register to hold temp table ROWID */
117699 int addrL; /* Label "L" */
117700
117701 srcTab = pParse->nTab++;
117702 regRec = sqlite3GetTempReg(pParse);
117703 regTempRowid = sqlite3GetTempReg(pParse);
117704 sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, srcTab, nColumn);
117705 addrL = sqlite3VdbeAddOp1(v, OP_Yield14, dest.iSDParm); VdbeCoverage(v);
117706 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regFromSelect, nColumn, regRec);
117707 sqlite3VdbeAddOp2(v, OP_NewRowid121, srcTab, regTempRowid);
117708 sqlite3VdbeAddOp3(v, OP_Insert122, srcTab, regRec, regTempRowid);
117709 sqlite3VdbeGoto(v, addrL);
117710 sqlite3VdbeJumpHere(v, addrL);
117711 sqlite3ReleaseTempReg(pParse, regRec);
117712 sqlite3ReleaseTempReg(pParse, regTempRowid);
117713 }
117714 }else{
117715 /* This is the case if the data for the INSERT is coming from a
117716 ** single-row VALUES clause
117717 */
117718 NameContext sNC;
117719 memset(&sNC, 0, sizeof(sNC));
117720 sNC.pParse = pParse;
117721 srcTab = -1;
117722 assert( useTempTable==0 )((void) (0));
117723 if( pList ){
117724 nColumn = pList->nExpr;
117725 if( sqlite3ResolveExprListNames(&sNC, pList) ){
117726 goto insert_cleanup;
117727 }
117728 }else{
117729 nColumn = 0;
117730 }
117731 }
117732
117733 /* If there is no IDLIST term but the table has an integer primary
117734 ** key, the set the ipkColumn variable to the integer primary key
117735 ** column index in the original table definition.
117736 */
117737 if( pColumn==0 && nColumn>0 ){
117738 ipkColumn = pTab->iPKey;
117739 }
117740
117741 /* Make sure the number of columns in the source data matches the number
117742 ** of columns to be inserted into the table.
117743 */
117744 for(i=0; i<pTab->nCol; i++){
117745 nHidden += (IsHiddenColumn(&pTab->aCol[i])(((&pTab->aCol[i])->colFlags & 0x0002)!=0) ? 1 : 0);
117746 }
117747 if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
117748 sqlite3ErrorMsg(pParse,
117749 "table %S has %d columns but %d values were supplied",
117750 pTabList, 0, pTab->nCol-nHidden, nColumn);
117751 goto insert_cleanup;
117752 }
117753 if( pColumn!=0 && nColumn!=pColumn->nId ){
117754 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
117755 goto insert_cleanup;
117756 }
117757
117758 /* Initialize the count of rows to be inserted
117759 */
117760 if( (db->flags & SQLITE_CountRows0x00000080)!=0
117761 && !pParse->nested
117762 && !pParse->pTriggerTab
117763 ){
117764 regRowCount = ++pParse->nMem;
117765 sqlite3VdbeAddOp2(v, OP_Integer70, 0, regRowCount);
117766 }
117767
117768 /* If this is not a view, open the table and and all indices */
117769 if( !isView ){
117770 int nIdx;
117771 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite109, 0, -1, 0,
117772 &iDataCur, &iIdxCur);
117773 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
117774 if( aRegIdx==0 ){
117775 goto insert_cleanup;
117776 }
117777 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
117778 assert( pIdx )((void) (0));
117779 aRegIdx[i] = ++pParse->nMem;
117780 pParse->nMem += pIdx->nColumn;
117781 }
117782 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
117783 }
117784#ifndef SQLITE_OMIT_UPSERT
117785 if( pUpsert ){
117786 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
117787 sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
117788 pTab->zName);
117789 goto insert_cleanup;
117790 }
117791 pTabList->a[0].iCursor = iDataCur;
117792 pUpsert->pUpsertSrc = pTabList;
117793 pUpsert->regData = regData;
117794 pUpsert->iDataCur = iDataCur;
117795 pUpsert->iIdxCur = iIdxCur;
117796 if( pUpsert->pUpsertTarget ){
117797 sqlite3UpsertAnalyzeTarget(pParse, pTabList, pUpsert);
117798 }
117799 }
117800#endif
117801
117802
117803 /* This is the top of the main insertion loop */
117804 if( useTempTable ){
117805 /* This block codes the top of loop only. The complete loop is the
117806 ** following pseudocode (template 4):
117807 **
117808 ** rewind temp table, if empty goto D
117809 ** C: loop over rows of intermediate table
117810 ** transfer values form intermediate table into <table>
117811 ** end loop
117812 ** D: ...
117813 */
117814 addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind36, srcTab); VdbeCoverage(v);
117815 addrCont = sqlite3VdbeCurrentAddr(v);
117816 }else if( pSelect ){
117817 /* This block codes the top of loop only. The complete loop is the
117818 ** following pseudocode (template 3):
117819 **
117820 ** C: yield X, at EOF goto D
117821 ** insert the select result into <table> from R..R+n
117822 ** goto C
117823 ** D: ...
117824 */
117825 addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield14, dest.iSDParm);
117826 VdbeCoverage(v);
117827 }
117828
117829 /* Run the BEFORE and INSTEAD OF triggers, if there are any
117830 */
117831 endOfLoop = sqlite3VdbeMakeLabel(pParse);
117832 if( tmask & TRIGGER_BEFORE1 ){
117833 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
117834
117835 /* build the NEW.* reference row. Note that if there is an INTEGER
117836 ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
117837 ** translated into a unique ID for the row. But on a BEFORE trigger,
117838 ** we do not know what the unique ID will be (because the insert has
117839 ** not happened yet) so we substitute a rowid of -1
117840 */
117841 if( ipkColumn<0 ){
117842 sqlite3VdbeAddOp2(v, OP_Integer70, -1, regCols);
117843 }else{
117844 int addr1;
117845 assert( !withoutRowid )((void) (0));
117846 if( useTempTable ){
117847 sqlite3VdbeAddOp3(v, OP_Column90, srcTab, ipkColumn, regCols);
117848 }else{
117849 assert( pSelect==0 )((void) (0)); /* Otherwise useTempTable is true */
117850 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
117851 }
117852 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull51, regCols); VdbeCoverage(v);
117853 sqlite3VdbeAddOp2(v, OP_Integer70, -1, regCols);
117854 sqlite3VdbeJumpHere(v, addr1);
117855 sqlite3VdbeAddOp1(v, OP_MustBeInt15, regCols); VdbeCoverage(v);
117856 }
117857
117858 /* Cannot have triggers on a virtual table. If it were possible,
117859 ** this block would have to account for hidden column.
117860 */
117861 assert( !IsVirtual(pTab) )((void) (0));
117862
117863 /* Create the new column data
117864 */
117865 for(i=j=0; i<pTab->nCol; i++){
117866 if( pColumn ){
117867 for(j=0; j<pColumn->nId; j++){
117868 if( pColumn->a[j].idx==i ) break;
117869 }
117870 }
117871 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId)
117872 || (pColumn==0 && IsOrdinaryHiddenColumn(&pTab->aCol[i])0) ){
117873 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
117874 }else if( useTempTable ){
117875 sqlite3VdbeAddOp3(v, OP_Column90, srcTab, j, regCols+i+1);
117876 }else{
117877 assert( pSelect==0 )((void) (0)); /* Otherwise useTempTable is true */
117878 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
117879 }
117880 if( pColumn==0 && !IsOrdinaryHiddenColumn(&pTab->aCol[i])0 ) j++;
117881 }
117882
117883 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
117884 ** do not attempt any conversions before assembling the record.
117885 ** If this is a real table, attempt conversions as required by the
117886 ** table column affinities.
117887 */
117888 if( !isView ){
117889 sqlite3TableAffinity(v, pTab, regCols+1);
117890 }
117891
117892 /* Fire BEFORE or INSTEAD OF triggers */
117893 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT120, 0, TRIGGER_BEFORE1,
117894 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
117895
117896 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
117897 }
117898
117899 /* Compute the content of the next row to insert into a range of
117900 ** registers beginning at regIns.
117901 */
117902 if( !isView ){
117903 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
117904 /* The row that the VUpdate opcode will delete: none */
117905 sqlite3VdbeAddOp2(v, OP_Null73, 0, regIns);
117906 }
117907 if( ipkColumn>=0 ){
117908 if( useTempTable ){
117909 sqlite3VdbeAddOp3(v, OP_Column90, srcTab, ipkColumn, regRowid);
117910 }else if( pSelect ){
117911 sqlite3VdbeAddOp2(v, OP_Copy78, regFromSelect+ipkColumn, regRowid);
117912 }else{
117913 Expr *pIpk = pList->a[ipkColumn].pExpr;
117914 if( pIpk->op==TK_NULL114 && !IsVirtual(pTab)((pTab)->nModuleArg) ){
117915 sqlite3VdbeAddOp3(v, OP_NewRowid121, iDataCur, regRowid, regAutoinc);
117916 appendFlag = 1;
117917 }else{
117918 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
117919 }
117920 }
117921 /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
117922 ** to generate a unique primary key value.
117923 */
117924 if( !appendFlag ){
117925 int addr1;
117926 if( !IsVirtual(pTab)((pTab)->nModuleArg) ){
117927 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull51, regRowid); VdbeCoverage(v);
117928 sqlite3VdbeAddOp3(v, OP_NewRowid121, iDataCur, regRowid, regAutoinc);
117929 sqlite3VdbeJumpHere(v, addr1);
117930 }else{
117931 addr1 = sqlite3VdbeCurrentAddr(v);
117932 sqlite3VdbeAddOp2(v, OP_IsNull50, regRowid, addr1+2); VdbeCoverage(v);
117933 }
117934 sqlite3VdbeAddOp1(v, OP_MustBeInt15, regRowid); VdbeCoverage(v);
117935 }
117936 }else if( IsVirtual(pTab)((pTab)->nModuleArg) || withoutRowid ){
117937 sqlite3VdbeAddOp2(v, OP_Null73, 0, regRowid);
117938 }else{
117939 sqlite3VdbeAddOp3(v, OP_NewRowid121, iDataCur, regRowid, regAutoinc);
117940 appendFlag = 1;
117941 }
117942 autoIncStep(pParse, regAutoinc, regRowid);
117943
117944 /* Compute data for all columns of the new entry, beginning
117945 ** with the first column.
117946 */
117947 nHidden = 0;
117948 for(i=0; i<pTab->nCol; i++){
117949 int iRegStore = regRowid+1+i;
117950 if( i==pTab->iPKey ){
117951 /* The value of the INTEGER PRIMARY KEY column is always a NULL.
117952 ** Whenever this column is read, the rowid will be substituted
117953 ** in its place. Hence, fill this column with a NULL to avoid
117954 ** taking up data space with information that will never be used.
117955 ** As there may be shallow copies of this value, make it a soft-NULL */
117956 sqlite3VdbeAddOp1(v, OP_SoftNull74, iRegStore);
117957 continue;
117958 }
117959 if( pColumn==0 ){
117960 if( IsHiddenColumn(&pTab->aCol[i])(((&pTab->aCol[i])->colFlags & 0x0002)!=0) ){
117961 j = -1;
117962 nHidden++;
117963 }else{
117964 j = i - nHidden;
117965 }
117966 }else{
117967 for(j=0; j<pColumn->nId; j++){
117968 if( pColumn->a[j].idx==i ) break;
117969 }
117970 }
117971 if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
117972 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
117973 }else if( useTempTable ){
117974 sqlite3VdbeAddOp3(v, OP_Column90, srcTab, j, iRegStore);
117975 }else if( pSelect ){
117976 if( regFromSelect!=regData ){
117977 sqlite3VdbeAddOp2(v, OP_SCopy79, regFromSelect+j, iRegStore);
117978 }
117979 }else{
117980 sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
117981 }
117982 }
117983
117984 /* Generate code to check constraints and generate index keys and
117985 ** do the insertion.
117986 */
117987#ifndef SQLITE_OMIT_VIRTUALTABLE
117988 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
117989 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
117990 sqlite3VtabMakeWritable(pParse, pTab);
117991 sqlite3VdbeAddOp4(v, OP_VUpdate10, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB(-12));
117992 sqlite3VdbeChangeP5(v, onError==OE_Default11 ? OE_Abort2 : onError);
117993 sqlite3MayAbort(pParse);
117994 }else
117995#endif
117996 {
117997 int isReplace; /* Set to true if constraints may cause a replace */
117998 int bUseSeek; /* True to use OPFLAG_SEEKRESULT */
117999 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
118000 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0, pUpsert
118001 );
118002 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
118003
118004 /* Set the OPFLAG_USESEEKRESULT flag if either (a) there are no REPLACE
118005 ** constraints or (b) there are no triggers and this table is not a
118006 ** parent table in a foreign key constraint. It is safe to set the
118007 ** flag in the second case as if any REPLACE constraint is hit, an
118008 ** OP_Delete or OP_IdxDelete instruction will be executed on each
118009 ** cursor that is disturbed. And these instructions both clear the
118010 ** VdbeCursor.seekResult variable, disabling the OPFLAG_USESEEKRESULT
118011 ** functionality. */
118012 bUseSeek = (isReplace==0 || (pTrigger==0 &&
118013 ((db->flags & SQLITE_ForeignKeys0x00004000)==0 || sqlite3FkReferences(pTab)==0)
118014 ));
118015 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
118016 regIns, aRegIdx, 0, appendFlag, bUseSeek
118017 );
118018 }
118019 }
118020
118021 /* Update the count of rows that are inserted
118022 */
118023 if( regRowCount ){
118024 sqlite3VdbeAddOp2(v, OP_AddImm83, regRowCount, 1);
118025 }
118026
118027 if( pTrigger ){
118028 /* Code AFTER triggers */
118029 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT120, 0, TRIGGER_AFTER2,
118030 pTab, regData-2-pTab->nCol, onError, endOfLoop);
118031 }
118032
118033 /* The bottom of the main insertion loop, if the data source
118034 ** is a SELECT statement.
118035 */
118036 sqlite3VdbeResolveLabel(v, endOfLoop);
118037 if( useTempTable ){
118038 sqlite3VdbeAddOp2(v, OP_Next5, srcTab, addrCont); VdbeCoverage(v);
118039 sqlite3VdbeJumpHere(v, addrInsTop);
118040 sqlite3VdbeAddOp1(v, OP_Close117, srcTab);
118041 }else if( pSelect ){
118042 sqlite3VdbeGoto(v, addrCont);
118043 sqlite3VdbeJumpHere(v, addrInsTop);
118044 }
118045
118046insert_end:
118047 /* Update the sqlite_sequence table by storing the content of the
118048 ** maximum rowid counter values recorded while inserting into
118049 ** autoincrement tables.
118050 */
118051 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
118052 sqlite3AutoincrementEnd(pParse);
118053 }
118054
118055 /*
118056 ** Return the number of rows inserted. If this routine is
118057 ** generating code because of a call to sqlite3NestedParse(), do not
118058 ** invoke the callback function.
118059 */
118060 if( regRowCount ){
118061 sqlite3VdbeAddOp2(v, OP_ResultRow81, regRowCount, 1);
118062 sqlite3VdbeSetNumCols(v, 1);
118063 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, "rows inserted", SQLITE_STATIC((sqlite3_destructor_type)0));
118064 }
118065
118066insert_cleanup:
118067 sqlite3SrcListDelete(db, pTabList);
118068 sqlite3ExprListDelete(db, pList);
118069 sqlite3UpsertDelete(db, pUpsert);
118070 sqlite3SelectDelete(db, pSelect);
118071 sqlite3IdListDelete(db, pColumn);
118072 sqlite3DbFree(db, aRegIdx);
118073}
118074
118075/* Make sure "isView" and other macros defined above are undefined. Otherwise
118076** they may interfere with compilation of other functions in this file
118077** (or in another file, if this file becomes part of the amalgamation). */
118078#ifdef isView
118079 #undef isView
118080#endif
118081#ifdef pTrigger
118082 #undef pTrigger
118083#endif
118084#ifdef tmask
118085 #undef tmask
118086#endif
118087
118088/*
118089** Meanings of bits in of pWalker->eCode for
118090** sqlite3ExprReferencesUpdatedColumn()
118091*/
118092#define CKCNSTRNT_COLUMN0x01 0x01 /* CHECK constraint uses a changing column */
118093#define CKCNSTRNT_ROWID0x02 0x02 /* CHECK constraint references the ROWID */
118094
118095/* This is the Walker callback from sqlite3ExprReferencesUpdatedColumn().
118096* Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
118097** expression node references any of the
118098** columns that are being modifed by an UPDATE statement.
118099*/
118100static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
118101 if( pExpr->op==TK_COLUMN162 ){
118102 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 )((void) (0));
118103 if( pExpr->iColumn>=0 ){
118104 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
118105 pWalker->eCode |= CKCNSTRNT_COLUMN0x01;
118106 }
118107 }else{
118108 pWalker->eCode |= CKCNSTRNT_ROWID0x02;
118109 }
118110 }
118111 return WRC_Continue0;
118112}
118113
118114/*
118115** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
118116** only columns that are modified by the UPDATE are those for which
118117** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
118118**
118119** Return true if CHECK constraint pExpr uses any of the
118120** changing columns (or the rowid if it is changing). In other words,
118121** return true if this CHECK constraint must be validated for
118122** the new row in the UPDATE statement.
118123**
118124** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
118125** The operation of this routine is the same - return true if an only if
118126** the expression uses one or more of columns identified by the second and
118127** third arguments.
118128*/
118129SQLITE_PRIVATEstatic int sqlite3ExprReferencesUpdatedColumn(
118130 Expr *pExpr, /* The expression to be checked */
118131 int *aiChng, /* aiChng[x]>=0 if column x changed by the UPDATE */
118132 int chngRowid /* True if UPDATE changes the rowid */
118133){
118134 Walker w;
118135 memset(&w, 0, sizeof(w));
118136 w.eCode = 0;
118137 w.xExprCallback = checkConstraintExprNode;
118138 w.u.aiCol = aiChng;
118139 sqlite3WalkExpr(&w, pExpr);
118140 if( !chngRowid ){
118141 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
118142 w.eCode &= ~CKCNSTRNT_ROWID0x02;
118143 }
118144 testcase( w.eCode==0 );
118145 testcase( w.eCode==CKCNSTRNT_COLUMN );
118146 testcase( w.eCode==CKCNSTRNT_ROWID );
118147 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
118148 return w.eCode!=0;
118149}
118150
118151/*
118152** Generate code to do constraint checks prior to an INSERT or an UPDATE
118153** on table pTab.
118154**
118155** The regNewData parameter is the first register in a range that contains
118156** the data to be inserted or the data after the update. There will be
118157** pTab->nCol+1 registers in this range. The first register (the one
118158** that regNewData points to) will contain the new rowid, or NULL in the
118159** case of a WITHOUT ROWID table. The second register in the range will
118160** contain the content of the first table column. The third register will
118161** contain the content of the second table column. And so forth.
118162**
118163** The regOldData parameter is similar to regNewData except that it contains
118164** the data prior to an UPDATE rather than afterwards. regOldData is zero
118165** for an INSERT. This routine can distinguish between UPDATE and INSERT by
118166** checking regOldData for zero.
118167**
118168** For an UPDATE, the pkChng boolean is true if the true primary key (the
118169** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
118170** might be modified by the UPDATE. If pkChng is false, then the key of
118171** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
118172**
118173** For an INSERT, the pkChng boolean indicates whether or not the rowid
118174** was explicitly specified as part of the INSERT statement. If pkChng
118175** is zero, it means that the either rowid is computed automatically or
118176** that the table is a WITHOUT ROWID table and has no rowid. On an INSERT,
118177** pkChng will only be true if the INSERT statement provides an integer
118178** value for either the rowid column or its INTEGER PRIMARY KEY alias.
118179**
118180** The code generated by this routine will store new index entries into
118181** registers identified by aRegIdx[]. No index entry is created for
118182** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
118183** the same as the order of indices on the linked list of indices
118184** at pTab->pIndex.
118185**
118186** (2019-05-07) The generated code also creates a new record for the
118187** main table, if pTab is a rowid table, and stores that record in the
118188** register identified by aRegIdx[nIdx] - in other words in the first
118189** entry of aRegIdx[] past the last index. It is important that the
118190** record be generated during constraint checks to avoid affinity changes
118191** to the register content that occur after constraint checks but before
118192** the new record is inserted.
118193**
118194** The caller must have already opened writeable cursors on the main
118195** table and all applicable indices (that is to say, all indices for which
118196** aRegIdx[] is not zero). iDataCur is the cursor for the main table when
118197** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
118198** index when operating on a WITHOUT ROWID table. iIdxCur is the cursor
118199** for the first index in the pTab->pIndex list. Cursors for other indices
118200** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
118201**
118202** This routine also generates code to check constraints. NOT NULL,
118203** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
118204** then the appropriate action is performed. There are five possible
118205** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
118206**
118207** Constraint type Action What Happens
118208** --------------- ---------- ----------------------------------------
118209** any ROLLBACK The current transaction is rolled back and
118210** sqlite3_step() returns immediately with a
118211** return code of SQLITE_CONSTRAINT.
118212**
118213** any ABORT Back out changes from the current command
118214** only (do not do a complete rollback) then
118215** cause sqlite3_step() to return immediately
118216** with SQLITE_CONSTRAINT.
118217**
118218** any FAIL Sqlite3_step() returns immediately with a
118219** return code of SQLITE_CONSTRAINT. The
118220** transaction is not rolled back and any
118221** changes to prior rows are retained.
118222**
118223** any IGNORE The attempt in insert or update the current
118224** row is skipped, without throwing an error.
118225** Processing continues with the next row.
118226** (There is an immediate jump to ignoreDest.)
118227**
118228** NOT NULL REPLACE The NULL value is replace by the default
118229** value for that column. If the default value
118230** is NULL, the action is the same as ABORT.
118231**
118232** UNIQUE REPLACE The other row that conflicts with the row
118233** being inserted is removed.
118234**
118235** CHECK REPLACE Illegal. The results in an exception.
118236**
118237** Which action to take is determined by the overrideError parameter.
118238** Or if overrideError==OE_Default, then the pParse->onError parameter
118239** is used. Or if pParse->onError==OE_Default then the onError value
118240** for the constraint is used.
118241*/
118242SQLITE_PRIVATEstatic void sqlite3GenerateConstraintChecks(
118243 Parse *pParse, /* The parser context */
118244 Table *pTab, /* The table being inserted or updated */
118245 int *aRegIdx, /* Use register aRegIdx[i] for index i. 0 for unused */
118246 int iDataCur, /* Canonical data cursor (main table or PK index) */
118247 int iIdxCur, /* First index cursor */
118248 int regNewData, /* First register in a range holding values to insert */
118249 int regOldData, /* Previous content. 0 for INSERTs */
118250 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
118251 u8 overrideError, /* Override onError to this if not OE_Default */
118252 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
118253 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
118254 int *aiChng, /* column i is unchanged if aiChng[i]<0 */
118255 Upsert *pUpsert /* ON CONFLICT clauses, if any. NULL otherwise */
118256){
118257 Vdbe *v; /* VDBE under constrution */
118258 Index *pIdx; /* Pointer to one of the indices */
118259 Index *pPk = 0; /* The PRIMARY KEY index */
118260 sqlite3 *db; /* Database connection */
118261 int i; /* loop counter */
118262 int ix; /* Index loop counter */
118263 int nCol; /* Number of columns */
118264 int onError; /* Conflict resolution strategy */
118265 int addr1; /* Address of jump instruction */
118266 int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
118267 int nPkField; /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
118268 Index *pUpIdx = 0; /* Index to which to apply the upsert */
118269 u8 isUpdate; /* True if this is an UPDATE operation */
118270 u8 bAffinityDone = 0; /* True if the OP_Affinity operation has been run */
118271 int upsertBypass = 0; /* Address of Goto to bypass upsert subroutine */
118272 int upsertJump = 0; /* Address of Goto that jumps into upsert subroutine */
118273 int ipkTop = 0; /* Top of the IPK uniqueness check */
118274 int ipkBottom = 0; /* OP_Goto at the end of the IPK uniqueness check */
118275
118276 isUpdate = regOldData!=0;
118277 db = pParse->db;
118278 v = sqlite3GetVdbe(pParse);
118279 assert( v!=0 )((void) (0));
118280 assert( pTab->pSelect==0 )((void) (0)); /* This table is not a VIEW */
118281 nCol = pTab->nCol;
118282
118283 /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
118284 ** normal rowid tables. nPkField is the number of key fields in the
118285 ** pPk index or 1 for a rowid table. In other words, nPkField is the
118286 ** number of fields in the true primary key of the table. */
118287 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
118288 pPk = 0;
118289 nPkField = 1;
118290 }else{
118291 pPk = sqlite3PrimaryKeyIndex(pTab);
118292 nPkField = pPk->nKeyCol;
118293 }
118294
118295 /* Record that this module has started */
118296 VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
118297 iDataCur, iIdxCur, regNewData, regOldData, pkChng));
118298
118299 /* Test all NOT NULL constraints.
118300 */
118301 for(i=0; i<nCol; i++){
118302 if( i==pTab->iPKey ){
118303 continue; /* ROWID is never NULL */
118304 }
118305 if( aiChng && aiChng[i]<0 ){
118306 /* Don't bother checking for NOT NULL on columns that do not change */
118307 continue;
118308 }
118309 onError = pTab->aCol[i].notNull;
118310 if( onError==OE_None0 ) continue; /* This column is allowed to be NULL */
118311 if( overrideError!=OE_Default11 ){
118312 onError = overrideError;
118313 }else if( onError==OE_Default11 ){
118314 onError = OE_Abort2;
118315 }
118316 if( onError==OE_Replace5 && pTab->aCol[i].pDflt==0 ){
118317 onError = OE_Abort2;
118318 }
118319 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail((void) (0))
118320 || onError==OE_Ignore || onError==OE_Replace )((void) (0));
118321 addr1 = 0;
118322 switch( onError ){
118323 case OE_Replace5: {
118324 assert( onError==OE_Replace )((void) (0));
118325 addr1 = sqlite3VdbeMakeLabel(pParse);
118326 sqlite3VdbeAddOp2(v, OP_NotNull51, regNewData+1+i, addr1);
118327 VdbeCoverage(v);
118328 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
118329 sqlite3VdbeAddOp2(v, OP_NotNull51, regNewData+1+i, addr1);
118330 VdbeCoverage(v);
118331 onError = OE_Abort2;
118332 /* Fall through into the OE_Abort case to generate code that runs
118333 ** if both the input and the default value are NULL */
118334 }
118335 case OE_Abort2:
118336 sqlite3MayAbort(pParse);
118337 /* Fall through */
118338 case OE_Rollback1:
118339 case OE_Fail3: {
118340 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
118341 pTab->aCol[i].zName);
118342 sqlite3VdbeAddOp3(v, OP_HaltIfNull68, SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)), onError,
118343 regNewData+1+i);
118344 sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC(-7));
118345 sqlite3VdbeChangeP5(v, P5_ConstraintNotNull1);
118346 VdbeCoverage(v);
118347 if( addr1 ) sqlite3VdbeResolveLabel(v, addr1);
118348 break;
118349 }
118350 default: {
118351 assert( onError==OE_Ignore )((void) (0));
118352 sqlite3VdbeAddOp2(v, OP_IsNull50, regNewData+1+i, ignoreDest);
118353 VdbeCoverage(v);
118354 break;
118355 }
118356 }
118357 }
118358
118359 /* Test all CHECK constraints
118360 */
118361#ifndef SQLITE_OMIT_CHECK
118362 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks0x00000200)==0 ){
118363 ExprList *pCheck = pTab->pCheck;
118364 pParse->iSelfTab = -(regNewData+1);
118365 onError = overrideError!=OE_Default11 ? overrideError : OE_Abort2;
118366 for(i=0; i<pCheck->nExpr; i++){
118367 int allOk;
118368 Expr *pExpr = pCheck->a[i].pExpr;
118369 if( aiChng
118370 && !sqlite3ExprReferencesUpdatedColumn(pExpr, aiChng, pkChng)
118371 ){
118372 /* The check constraints do not reference any of the columns being
118373 ** updated so there is no point it verifying the check constraint */
118374 continue;
118375 }
118376 allOk = sqlite3VdbeMakeLabel(pParse);
118377 sqlite3VdbeVerifyAbortable(v, onError);
118378 sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL0x10);
118379 if( onError==OE_Ignore4 ){
118380 sqlite3VdbeGoto(v, ignoreDest);
118381 }else{
118382 char *zName = pCheck->a[i].zName;
118383 if( zName==0 ) zName = pTab->zName;
118384 if( onError==OE_Replace5 ) onError = OE_Abort2; /* IMP: R-26383-51744 */
118385 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK(19 | (1<<8)),
118386 onError, zName, P4_TRANSIENT0,
118387 P5_ConstraintCheck3);
118388 }
118389 sqlite3VdbeResolveLabel(v, allOk);
118390 }
118391 pParse->iSelfTab = 0;
118392 }
118393#endif /* !defined(SQLITE_OMIT_CHECK) */
118394
118395 /* UNIQUE and PRIMARY KEY constraints should be handled in the following
118396 ** order:
118397 **
118398 ** (1) OE_Update
118399 ** (2) OE_Abort, OE_Fail, OE_Rollback, OE_Ignore
118400 ** (3) OE_Replace
118401 **
118402 ** OE_Fail and OE_Ignore must happen before any changes are made.
118403 ** OE_Update guarantees that only a single row will change, so it
118404 ** must happen before OE_Replace. Technically, OE_Abort and OE_Rollback
118405 ** could happen in any order, but they are grouped up front for
118406 ** convenience.
118407 **
118408 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
118409 ** The order of constraints used to have OE_Update as (2) and OE_Abort
118410 ** and so forth as (1). But apparently PostgreSQL checks the OE_Update
118411 ** constraint before any others, so it had to be moved.
118412 **
118413 ** Constraint checking code is generated in this order:
118414 ** (A) The rowid constraint
118415 ** (B) Unique index constraints that do not have OE_Replace as their
118416 ** default conflict resolution strategy
118417 ** (C) Unique index that do use OE_Replace by default.
118418 **
118419 ** The ordering of (2) and (3) is accomplished by making sure the linked
118420 ** list of indexes attached to a table puts all OE_Replace indexes last
118421 ** in the list. See sqlite3CreateIndex() for where that happens.
118422 */
118423
118424 if( pUpsert ){
118425 if( pUpsert->pUpsertTarget==0 ){
118426 /* An ON CONFLICT DO NOTHING clause, without a constraint-target.
118427 ** Make all unique constraint resolution be OE_Ignore */
118428 assert( pUpsert->pUpsertSet==0 )((void) (0));
118429 overrideError = OE_Ignore4;
118430 pUpsert = 0;
118431 }else if( (pUpIdx = pUpsert->pUpsertIdx)!=0 ){
118432 /* If the constraint-target uniqueness check must be run first.
118433 ** Jump to that uniqueness check now */
118434 upsertJump = sqlite3VdbeAddOp0(v, OP_Goto11);
118435 VdbeComment((v, "UPSERT constraint goes first"));
118436 }
118437 }
118438
118439 /* If rowid is changing, make sure the new rowid does not previously
118440 ** exist in the table.
118441 */
118442 if( pkChng && pPk==0 ){
118443 int addrRowidOk = sqlite3VdbeMakeLabel(pParse);
118444
118445 /* Figure out what action to take in case of a rowid collision */
118446 onError = pTab->keyConf;
118447 if( overrideError!=OE_Default11 ){
118448 onError = overrideError;
118449 }else if( onError==OE_Default11 ){
118450 onError = OE_Abort2;
118451 }
118452
118453 /* figure out whether or not upsert applies in this case */
118454 if( pUpsert && pUpsert->pUpsertIdx==0 ){
118455 if( pUpsert->pUpsertSet==0 ){
118456 onError = OE_Ignore4; /* DO NOTHING is the same as INSERT OR IGNORE */
118457 }else{
118458 onError = OE_Update6; /* DO UPDATE */
118459 }
118460 }
118461
118462 /* If the response to a rowid conflict is REPLACE but the response
118463 ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
118464 ** to defer the running of the rowid conflict checking until after
118465 ** the UNIQUE constraints have run.
118466 */
118467 if( onError==OE_Replace5 /* IPK rule is REPLACE */
118468 && onError!=overrideError /* Rules for other contraints are different */
118469 && pTab->pIndex /* There exist other constraints */
118470 ){
118471 ipkTop = sqlite3VdbeAddOp0(v, OP_Goto11)+1;
118472 VdbeComment((v, "defer IPK REPLACE until last"));
118473 }
118474
118475 if( isUpdate ){
118476 /* pkChng!=0 does not mean that the rowid has changed, only that
118477 ** it might have changed. Skip the conflict logic below if the rowid
118478 ** is unchanged. */
118479 sqlite3VdbeAddOp3(v, OP_Eq53, regNewData, addrRowidOk, regOldData);
118480 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
118481 VdbeCoverage(v);
118482 }
118483
118484 /* Check to see if the new rowid already exists in the table. Skip
118485 ** the following conflict logic if it does not. */
118486 VdbeNoopComment((v, "uniqueness check for ROWID"));
118487 sqlite3VdbeVerifyAbortable(v, onError);
118488 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, addrRowidOk, regNewData);
118489 VdbeCoverage(v);
118490
118491 switch( onError ){
118492 default: {
118493 onError = OE_Abort2;
118494 /* Fall thru into the next case */
118495 }
118496 case OE_Rollback1:
118497 case OE_Abort2:
118498 case OE_Fail3: {
118499 testcase( onError==OE_Rollback );
118500 testcase( onError==OE_Abort );
118501 testcase( onError==OE_Fail );
118502 sqlite3RowidConstraint(pParse, onError, pTab);
118503 break;
118504 }
118505 case OE_Replace5: {
118506 /* If there are DELETE triggers on this table and the
118507 ** recursive-triggers flag is set, call GenerateRowDelete() to
118508 ** remove the conflicting row from the table. This will fire
118509 ** the triggers and remove both the table and index b-tree entries.
118510 **
118511 ** Otherwise, if there are no triggers or the recursive-triggers
118512 ** flag is not set, but the table has one or more indexes, call
118513 ** GenerateRowIndexDelete(). This removes the index b-tree entries
118514 ** only. The table b-tree entry will be replaced by the new entry
118515 ** when it is inserted.
118516 **
118517 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
118518 ** also invoke MultiWrite() to indicate that this VDBE may require
118519 ** statement rollback (if the statement is aborted after the delete
118520 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
118521 ** but being more selective here allows statements like:
118522 **
118523 ** REPLACE INTO t(rowid) VALUES($newrowid)
118524 **
118525 ** to run without a statement journal if there are no indexes on the
118526 ** table.
118527 */
118528 Trigger *pTrigger = 0;
118529 if( db->flags&SQLITE_RecTriggers0x00002000 ){
118530 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE121, 0, 0);
118531 }
118532 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
118533 sqlite3MultiWrite(pParse);
118534 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
118535 regNewData, 1, 0, OE_Replace5, 1, -1);
118536 }else{
118537#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
118538 assert( HasRowid(pTab) )((void) (0));
118539 /* This OP_Delete opcode fires the pre-update-hook only. It does
118540 ** not modify the b-tree. It is more efficient to let the coming
118541 ** OP_Insert replace the existing entry than it is to delete the
118542 ** existing entry and then insert a new one. */
118543 sqlite3VdbeAddOp2(v, OP_Delete123, iDataCur, OPFLAG_ISNOOP0x40);
118544 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-6));
118545#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
118546 if( pTab->pIndex ){
118547 sqlite3MultiWrite(pParse);
118548 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
118549 }
118550 }
118551 seenReplace = 1;
118552 break;
118553 }
118554#ifndef SQLITE_OMIT_UPSERT
118555 case OE_Update6: {
118556 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, 0, iDataCur);
118557 /* Fall through */
118558 }
118559#endif
118560 case OE_Ignore4: {
118561 testcase( onError==OE_Ignore );
118562 sqlite3VdbeGoto(v, ignoreDest);
118563 break;
118564 }
118565 }
118566 sqlite3VdbeResolveLabel(v, addrRowidOk);
118567 if( ipkTop ){
118568 ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto11);
118569 sqlite3VdbeJumpHere(v, ipkTop-1);
118570 }
118571 }
118572
118573 /* Test all UNIQUE constraints by creating entries for each UNIQUE
118574 ** index and making sure that duplicate entries do not already exist.
118575 ** Compute the revised record entries for indices as we go.
118576 **
118577 ** This loop also handles the case of the PRIMARY KEY index for a
118578 ** WITHOUT ROWID table.
118579 */
118580 for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
118581 int regIdx; /* Range of registers hold conent for pIdx */
118582 int regR; /* Range of registers holding conflicting PK */
118583 int iThisCur; /* Cursor for this UNIQUE index */
118584 int addrUniqueOk; /* Jump here if the UNIQUE constraint is satisfied */
118585
118586 if( aRegIdx[ix]==0 ) continue; /* Skip indices that do not change */
118587 if( pUpIdx==pIdx ){
118588 addrUniqueOk = upsertJump+1;
118589 upsertBypass = sqlite3VdbeGoto(v, 0);
118590 VdbeComment((v, "Skip upsert subroutine"));
118591 sqlite3VdbeJumpHere(v, upsertJump);
118592 }else{
118593 addrUniqueOk = sqlite3VdbeMakeLabel(pParse);
118594 }
118595 if( bAffinityDone==0 && (pUpIdx==0 || pUpIdx==pIdx) ){
118596 sqlite3TableAffinity(v, pTab, regNewData+1);
118597 bAffinityDone = 1;
118598 }
118599 VdbeNoopComment((v, "uniqueness check for %s", pIdx->zName));
118600 iThisCur = iIdxCur+ix;
118601
118602
118603 /* Skip partial indices for which the WHERE clause is not true */
118604 if( pIdx->pPartIdxWhere ){
118605 sqlite3VdbeAddOp2(v, OP_Null73, 0, aRegIdx[ix]);
118606 pParse->iSelfTab = -(regNewData+1);
118607 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
118608 SQLITE_JUMPIFNULL0x10);
118609 pParse->iSelfTab = 0;
118610 }
118611
118612 /* Create a record for this index entry as it should appear after
118613 ** the insert or update. Store that record in the aRegIdx[ix] register
118614 */
118615 regIdx = aRegIdx[ix]+1;
118616 for(i=0; i<pIdx->nColumn; i++){
118617 int iField = pIdx->aiColumn[i];
118618 int x;
118619 if( iField==XN_EXPR(-2) ){
118620 pParse->iSelfTab = -(regNewData+1);
118621 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
118622 pParse->iSelfTab = 0;
118623 VdbeComment((v, "%s column %d", pIdx->zName, i));
118624 }else{
118625 if( iField==XN_ROWID(-1) || iField==pTab->iPKey ){
118626 x = regNewData;
118627 }else{
118628 x = iField + regNewData + 1;
118629 }
118630 sqlite3VdbeAddOp2(v, iField<0 ? OP_IntCopy80 : OP_SCopy79, x, regIdx+i);
118631 VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
118632 }
118633 }
118634 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regIdx, pIdx->nColumn, aRegIdx[ix]);
118635 VdbeComment((v, "for %s", pIdx->zName));
118636#ifdef SQLITE_ENABLE_NULL_TRIM
118637 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ){
118638 sqlite3SetMakeRecordP5(v, pIdx->pTable);
118639 }
118640#endif
118641
118642 /* In an UPDATE operation, if this index is the PRIMARY KEY index
118643 ** of a WITHOUT ROWID table and there has been no change the
118644 ** primary key, then no collision is possible. The collision detection
118645 ** logic below can all be skipped. */
118646 if( isUpdate && pPk==pIdx && pkChng==0 ){
118647 sqlite3VdbeResolveLabel(v, addrUniqueOk);
118648 continue;
118649 }
118650
118651 /* Find out what action to take in case there is a uniqueness conflict */
118652 onError = pIdx->onError;
118653 if( onError==OE_None0 ){
118654 sqlite3VdbeResolveLabel(v, addrUniqueOk);
118655 continue; /* pIdx is not a UNIQUE index */
118656 }
118657 if( overrideError!=OE_Default11 ){
118658 onError = overrideError;
118659 }else if( onError==OE_Default11 ){
118660 onError = OE_Abort2;
118661 }
118662
118663 /* Figure out if the upsert clause applies to this index */
118664 if( pUpIdx==pIdx ){
118665 if( pUpsert->pUpsertSet==0 ){
118666 onError = OE_Ignore4; /* DO NOTHING is the same as INSERT OR IGNORE */
118667 }else{
118668 onError = OE_Update6; /* DO UPDATE */
118669 }
118670 }
118671
118672 /* Collision detection may be omitted if all of the following are true:
118673 ** (1) The conflict resolution algorithm is REPLACE
118674 ** (2) The table is a WITHOUT ROWID table
118675 ** (3) There are no secondary indexes on the table
118676 ** (4) No delete triggers need to be fired if there is a conflict
118677 ** (5) No FK constraint counters need to be updated if a conflict occurs.
118678 **
118679 ** This is not possible for ENABLE_PREUPDATE_HOOK builds, as the row
118680 ** must be explicitly deleted in order to ensure any pre-update hook
118681 ** is invoked. */
118682#ifndef SQLITE_ENABLE_PREUPDATE_HOOK
118683 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
118684 && pPk==pIdx /* Condition 2 */
118685 && onError==OE_Replace5 /* Condition 1 */
118686 && ( 0==(db->flags&SQLITE_RecTriggers0x00002000) || /* Condition 4 */
118687 0==sqlite3TriggersExist(pParse, pTab, TK_DELETE121, 0, 0))
118688 && ( 0==(db->flags&SQLITE_ForeignKeys0x00004000) || /* Condition 5 */
118689 (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
118690 ){
118691 sqlite3VdbeResolveLabel(v, addrUniqueOk);
118692 continue;
118693 }
118694#endif /* ifndef SQLITE_ENABLE_PREUPDATE_HOOK */
118695
118696 /* Check to see if the new index entry will be unique */
118697 sqlite3VdbeVerifyAbortable(v, onError);
118698 sqlite3VdbeAddOp4Int(v, OP_NoConflict27, iThisCur, addrUniqueOk,
118699 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
118700
118701 /* Generate code to handle collisions */
118702 regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
118703 if( isUpdate || onError==OE_Replace5 ){
118704 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
118705 sqlite3VdbeAddOp2(v, OP_IdxRowid135, iThisCur, regR);
118706 /* Conflict only if the rowid of the existing index entry
118707 ** is different from old-rowid */
118708 if( isUpdate ){
118709 sqlite3VdbeAddOp3(v, OP_Eq53, regR, addrUniqueOk, regOldData);
118710 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
118711 VdbeCoverage(v);
118712 }
118713 }else{
118714 int x;
118715 /* Extract the PRIMARY KEY from the end of the index entry and
118716 ** store it in registers regR..regR+nPk-1 */
118717 if( pIdx!=pPk ){
118718 for(i=0; i<pPk->nKeyCol; i++){
118719 assert( pPk->aiColumn[i]>=0 )((void) (0));
118720 x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
118721 sqlite3VdbeAddOp3(v, OP_Column90, iThisCur, x, regR+i);
118722 VdbeComment((v, "%s.%s", pTab->zName,
118723 pTab->aCol[pPk->aiColumn[i]].zName));
118724 }
118725 }
118726 if( isUpdate ){
118727 /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
118728 ** table, only conflict if the new PRIMARY KEY values are actually
118729 ** different from the old.
118730 **
118731 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
118732 ** of the matched index row are different from the original PRIMARY
118733 ** KEY values of this row before the update. */
118734 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
118735 int op = OP_Ne52;
118736 int regCmp = (IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ? regIdx : regR);
118737
118738 for(i=0; i<pPk->nKeyCol; i++){
118739 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
118740 x = pPk->aiColumn[i];
118741 assert( x>=0 )((void) (0));
118742 if( i==(pPk->nKeyCol-1) ){
118743 addrJump = addrUniqueOk;
118744 op = OP_Eq53;
118745 }
118746 sqlite3VdbeAddOp4(v, op,
118747 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ(-2)
118748 );
118749 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
118750 VdbeCoverageIf(v, op==OP_Eq);
118751 VdbeCoverageIf(v, op==OP_Ne);
118752 }
118753 }
118754 }
118755 }
118756
118757 /* Generate code that executes if the new index entry is not unique */
118758 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail((void) (0))
118759 || onError==OE_Ignore || onError==OE_Replace || onError==OE_Update )((void) (0));
118760 switch( onError ){
118761 case OE_Rollback1:
118762 case OE_Abort2:
118763 case OE_Fail3: {
118764 testcase( onError==OE_Rollback );
118765 testcase( onError==OE_Abort );
118766 testcase( onError==OE_Fail );
118767 sqlite3UniqueConstraint(pParse, onError, pIdx);
118768 break;
118769 }
118770#ifndef SQLITE_OMIT_UPSERT
118771 case OE_Update6: {
118772 sqlite3UpsertDoUpdate(pParse, pUpsert, pTab, pIdx, iIdxCur+ix);
118773 /* Fall through */
118774 }
118775#endif
118776 case OE_Ignore4: {
118777 testcase( onError==OE_Ignore );
118778 sqlite3VdbeGoto(v, ignoreDest);
118779 break;
118780 }
118781 default: {
118782 Trigger *pTrigger = 0;
118783 assert( onError==OE_Replace )((void) (0));
118784 if( db->flags&SQLITE_RecTriggers0x00002000 ){
118785 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE121, 0, 0);
118786 }
118787 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
118788 sqlite3MultiWrite(pParse);
118789 }
118790 sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
118791 regR, nPkField, 0, OE_Replace5,
118792 (pIdx==pPk ? ONEPASS_SINGLE1 : ONEPASS_OFF0), iThisCur);
118793 seenReplace = 1;
118794 break;
118795 }
118796 }
118797 if( pUpIdx==pIdx ){
118798 sqlite3VdbeGoto(v, upsertJump+1);
118799 sqlite3VdbeJumpHere(v, upsertBypass);
118800 }else{
118801 sqlite3VdbeResolveLabel(v, addrUniqueOk);
118802 }
118803 if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
118804 }
118805
118806 /* If the IPK constraint is a REPLACE, run it last */
118807 if( ipkTop ){
118808 sqlite3VdbeGoto(v, ipkTop);
118809 VdbeComment((v, "Do IPK REPLACE"));
118810 sqlite3VdbeJumpHere(v, ipkBottom);
118811 }
118812
118813 /* Generate the table record */
118814 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
118815 int regRec = aRegIdx[ix];
118816 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regNewData+1, pTab->nCol, regRec);
118817 sqlite3SetMakeRecordP5(v, pTab);
118818 if( !bAffinityDone ){
118819 sqlite3TableAffinity(v, pTab, 0);
118820 }
118821 }
118822
118823 *pbMayReplace = seenReplace;
118824 VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
118825}
118826
118827#ifdef SQLITE_ENABLE_NULL_TRIM
118828/*
118829** Change the P5 operand on the last opcode (which should be an OP_MakeRecord)
118830** to be the number of columns in table pTab that must not be NULL-trimmed.
118831**
118832** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
118833*/
118834SQLITE_PRIVATEstatic void sqlite3SetMakeRecordP5(Vdbe *v, Table *pTab){
118835 u16 i;
118836
118837 /* Records with omitted columns are only allowed for schema format
118838 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
118839 if( pTab->pSchema->file_format<2 ) return;
118840
118841 for(i=pTab->nCol-1; i>0; i--){
118842 if( pTab->aCol[i].pDflt!=0 ) break;
118843 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001 ) break;
118844 }
118845 sqlite3VdbeChangeP5(v, i+1);
118846}
118847#endif
118848
118849/*
118850** This routine generates code to finish the INSERT or UPDATE operation
118851** that was started by a prior call to sqlite3GenerateConstraintChecks.
118852** A consecutive range of registers starting at regNewData contains the
118853** rowid and the content to be inserted.
118854**
118855** The arguments to this routine should be the same as the first six
118856** arguments to sqlite3GenerateConstraintChecks.
118857*/
118858SQLITE_PRIVATEstatic void sqlite3CompleteInsertion(
118859 Parse *pParse, /* The parser context */
118860 Table *pTab, /* the table into which we are inserting */
118861 int iDataCur, /* Cursor of the canonical data source */
118862 int iIdxCur, /* First index cursor */
118863 int regNewData, /* Range of content */
118864 int *aRegIdx, /* Register used by each index. 0 for unused indices */
118865 int update_flags, /* True for UPDATE, False for INSERT */
118866 int appendBias, /* True if this is likely to be an append */
118867 int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
118868){
118869 Vdbe *v; /* Prepared statements under construction */
118870 Index *pIdx; /* An index being inserted or updated */
118871 u8 pik_flags; /* flag values passed to the btree insert */
118872 int i; /* Loop counter */
118873
118874 assert( update_flags==0((void) (0))
118875 || update_flags==OPFLAG_ISUPDATE((void) (0))
118876 || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)((void) (0))
118877 )((void) (0));
118878
118879 v = sqlite3GetVdbe(pParse);
118880 assert( v!=0 )((void) (0));
118881 assert( pTab->pSelect==0 )((void) (0)); /* This table is not a VIEW */
118882 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
118883 if( aRegIdx[i]==0 ) continue;
118884 if( pIdx->pPartIdxWhere ){
118885 sqlite3VdbeAddOp2(v, OP_IsNull50, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
118886 VdbeCoverage(v);
118887 }
118888 pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT0x10 : 0);
118889 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
118890 assert( pParse->nested==0 )((void) (0));
118891 pik_flags |= OPFLAG_NCHANGE0x01;
118892 pik_flags |= (update_flags & OPFLAG_SAVEPOSITION0x02);
118893#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
118894 if( update_flags==0 ){
118895 int r = sqlite3GetTempReg(pParse);
118896 sqlite3VdbeAddOp2(v, OP_Integer70, 0, r);
118897 sqlite3VdbeAddOp4(v, OP_Insert122,
118898 iIdxCur+i, aRegIdx[i], r, (char*)pTab, P4_TABLE(-6)
118899 );
118900 sqlite3VdbeChangeP5(v, OPFLAG_ISNOOP0x40);
118901 sqlite3ReleaseTempReg(pParse, r);
118902 }
118903#endif
118904 }
118905 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iIdxCur+i, aRegIdx[i],
118906 aRegIdx[i]+1,
118907 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
118908 sqlite3VdbeChangeP5(v, pik_flags);
118909 }
118910 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ) return;
118911 if( pParse->nested ){
118912 pik_flags = 0;
118913 }else{
118914 pik_flags = OPFLAG_NCHANGE0x01;
118915 pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID0x20);
118916 }
118917 if( appendBias ){
118918 pik_flags |= OPFLAG_APPEND0x08;
118919 }
118920 if( useSeekResult ){
118921 pik_flags |= OPFLAG_USESEEKRESULT0x10;
118922 }
118923 sqlite3VdbeAddOp3(v, OP_Insert122, iDataCur, aRegIdx[i], regNewData);
118924 if( !pParse->nested ){
118925 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-6));
118926 }
118927 sqlite3VdbeChangeP5(v, pik_flags);
118928}
118929
118930/*
118931** Allocate cursors for the pTab table and all its indices and generate
118932** code to open and initialized those cursors.
118933**
118934** The cursor for the object that contains the complete data (normally
118935** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
118936** ROWID table) is returned in *piDataCur. The first index cursor is
118937** returned in *piIdxCur. The number of indices is returned.
118938**
118939** Use iBase as the first cursor (either the *piDataCur for rowid tables
118940** or the first index for WITHOUT ROWID tables) if it is non-negative.
118941** If iBase is negative, then allocate the next available cursor.
118942**
118943** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
118944** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
118945** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
118946** pTab->pIndex list.
118947**
118948** If pTab is a virtual table, then this routine is a no-op and the
118949** *piDataCur and *piIdxCur values are left uninitialized.
118950*/
118951SQLITE_PRIVATEstatic int sqlite3OpenTableAndIndices(
118952 Parse *pParse, /* Parsing context */
118953 Table *pTab, /* Table to be opened */
118954 int op, /* OP_OpenRead or OP_OpenWrite */
118955 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
118956 int iBase, /* Use this for the table cursor, if there is one */
118957 u8 *aToOpen, /* If not NULL: boolean for each table and index */
118958 int *piDataCur, /* Write the database source cursor number here */
118959 int *piIdxCur /* Write the first index cursor number here */
118960){
118961 int i;
118962 int iDb;
118963 int iDataCur;
118964 Index *pIdx;
118965 Vdbe *v;
118966
118967 assert( op==OP_OpenRead || op==OP_OpenWrite )((void) (0));
118968 assert( op==OP_OpenWrite || p5==0 )((void) (0));
118969 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
118970 /* This routine is a no-op for virtual tables. Leave the output
118971 ** variables *piDataCur and *piIdxCur uninitialized so that valgrind
118972 ** can detect if they are used by mistake in the caller. */
118973 return 0;
118974 }
118975 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
118976 v = sqlite3GetVdbe(pParse);
118977 assert( v!=0 )((void) (0));
118978 if( iBase<0 ) iBase = pParse->nTab;
118979 iDataCur = iBase++;
118980 if( piDataCur ) *piDataCur = iDataCur;
118981 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) && (aToOpen==0 || aToOpen[0]) ){
118982 sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
118983 }else{
118984 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite109, pTab->zName);
118985 }
118986 if( piIdxCur ) *piIdxCur = iBase;
118987 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
118988 int iIdxCur = iBase++;
118989 assert( pIdx->pSchema==pTab->pSchema )((void) (0));
118990 if( IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) && !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
118991 if( piDataCur ) *piDataCur = iIdxCur;
118992 p5 = 0;
118993 }
118994 if( aToOpen==0 || aToOpen[i+1] ){
118995 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
118996 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
118997 sqlite3VdbeChangeP5(v, p5);
118998 VdbeComment((v, "%s", pIdx->zName));
118999 }
119000 }
119001 if( iBase>pParse->nTab ) pParse->nTab = iBase;
119002 return i;
119003}
119004
119005
119006#ifdef SQLITE_TEST
119007/*
119008** The following global variable is incremented whenever the
119009** transfer optimization is used. This is used for testing
119010** purposes only - to make sure the transfer optimization really
119011** is happening when it is supposed to.
119012*/
119013SQLITE_API int sqlite3_xferopt_count;
119014#endif /* SQLITE_TEST */
119015
119016
119017#ifndef SQLITE_OMIT_XFER_OPT
119018/*
119019** Check to see if index pSrc is compatible as a source of data
119020** for index pDest in an insert transfer optimization. The rules
119021** for a compatible index:
119022**
119023** * The index is over the same set of columns
119024** * The same DESC and ASC markings occurs on all columns
119025** * The same onError processing (OE_Abort, OE_Ignore, etc)
119026** * The same collating sequence on each column
119027** * The index has the exact same WHERE clause
119028*/
119029static int xferCompatibleIndex(Index *pDest, Index *pSrc){
119030 int i;
119031 assert( pDest && pSrc )((void) (0));
119032 assert( pDest->pTable!=pSrc->pTable )((void) (0));
119033 if( pDest->nKeyCol!=pSrc->nKeyCol ){
119034 return 0; /* Different number of columns */
119035 }
119036 if( pDest->onError!=pSrc->onError ){
119037 return 0; /* Different conflict resolution strategies */
119038 }
119039 for(i=0; i<pSrc->nKeyCol; i++){
119040 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
119041 return 0; /* Different columns indexed */
119042 }
119043 if( pSrc->aiColumn[i]==XN_EXPR(-2) ){
119044 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 )((void) (0));
119045 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
119046 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
119047 return 0; /* Different expressions in the index */
119048 }
119049 }
119050 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
119051 return 0; /* Different sort orders */
119052 }
119053 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
119054 return 0; /* Different collating sequences */
119055 }
119056 }
119057 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
119058 return 0; /* Different WHERE clauses */
119059 }
119060
119061 /* If no test above fails then the indices must be compatible */
119062 return 1;
119063}
119064
119065/*
119066** Attempt the transfer optimization on INSERTs of the form
119067**
119068** INSERT INTO tab1 SELECT * FROM tab2;
119069**
119070** The xfer optimization transfers raw records from tab2 over to tab1.
119071** Columns are not decoded and reassembled, which greatly improves
119072** performance. Raw index records are transferred in the same way.
119073**
119074** The xfer optimization is only attempted if tab1 and tab2 are compatible.
119075** There are lots of rules for determining compatibility - see comments
119076** embedded in the code for details.
119077**
119078** This routine returns TRUE if the optimization is guaranteed to be used.
119079** Sometimes the xfer optimization will only work if the destination table
119080** is empty - a factor that can only be determined at run-time. In that
119081** case, this routine generates code for the xfer optimization but also
119082** does a test to see if the destination table is empty and jumps over the
119083** xfer optimization code if the test fails. In that case, this routine
119084** returns FALSE so that the caller will know to go ahead and generate
119085** an unoptimized transfer. This routine also returns FALSE if there
119086** is no chance that the xfer optimization can be applied.
119087**
119088** This optimization is particularly useful at making VACUUM run faster.
119089*/
119090static int xferOptimization(
119091 Parse *pParse, /* Parser context */
119092 Table *pDest, /* The table we are inserting into */
119093 Select *pSelect, /* A SELECT statement to use as the data source */
119094 int onError, /* How to handle constraint errors */
119095 int iDbDest /* The database of pDest */
119096){
119097 sqlite3 *db = pParse->db;
119098 ExprList *pEList; /* The result set of the SELECT */
119099 Table *pSrc; /* The table in the FROM clause of SELECT */
119100 Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
119101 struct SrcList_item *pItem; /* An element of pSelect->pSrc */
119102 int i; /* Loop counter */
119103 int iDbSrc; /* The database of pSrc */
119104 int iSrc, iDest; /* Cursors from source and destination */
119105 int addr1, addr2; /* Loop addresses */
119106 int emptyDestTest = 0; /* Address of test for empty pDest */
119107 int emptySrcTest = 0; /* Address of test for empty pSrc */
119108 Vdbe *v; /* The VDBE we are building */
119109 int regAutoinc; /* Memory register used by AUTOINC */
119110 int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
119111 int regData, regRowid; /* Registers holding data and rowid */
119112
119113 if( pSelect==0 ){
119114 return 0; /* Must be of the form INSERT INTO ... SELECT ... */
119115 }
119116 if( pParse->pWith || pSelect->pWith ){
119117 /* Do not attempt to process this query if there are an WITH clauses
119118 ** attached to it. Proceeding may generate a false "no such table: xxx"
119119 ** error if pSelect reads from a CTE named "xxx". */
119120 return 0;
119121 }
119122 if( sqlite3TriggerList(pParse, pDest) ){
119123 return 0; /* tab1 must not have triggers */
119124 }
119125#ifndef SQLITE_OMIT_VIRTUALTABLE
119126 if( IsVirtual(pDest)((pDest)->nModuleArg) ){
119127 return 0; /* tab1 must not be a virtual table */
119128 }
119129#endif
119130 if( onError==OE_Default11 ){
119131 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
119132 if( onError==OE_Default11 ) onError = OE_Abort2;
119133 }
119134 assert(pSelect->pSrc)((void) (0)); /* allocated even if there is no FROM clause */
119135 if( pSelect->pSrc->nSrc!=1 ){
119136 return 0; /* FROM clause must have exactly one term */
119137 }
119138 if( pSelect->pSrc->a[0].pSelect ){
119139 return 0; /* FROM clause cannot contain a subquery */
119140 }
119141 if( pSelect->pWhere ){
119142 return 0; /* SELECT may not have a WHERE clause */
119143 }
119144 if( pSelect->pOrderBy ){
119145 return 0; /* SELECT may not have an ORDER BY clause */
119146 }
119147 /* Do not need to test for a HAVING clause. If HAVING is present but
119148 ** there is no ORDER BY, we will get an error. */
119149 if( pSelect->pGroupBy ){
119150 return 0; /* SELECT may not have a GROUP BY clause */
119151 }
119152 if( pSelect->pLimit ){
119153 return 0; /* SELECT may not have a LIMIT clause */
119154 }
119155 if( pSelect->pPrior ){
119156 return 0; /* SELECT may not be a compound query */
119157 }
119158 if( pSelect->selFlags & SF_Distinct0x00001 ){
119159 return 0; /* SELECT may not be DISTINCT */
119160 }
119161 pEList = pSelect->pEList;
119162 assert( pEList!=0 )((void) (0));
119163 if( pEList->nExpr!=1 ){
119164 return 0; /* The result set must have exactly one column */
119165 }
119166 assert( pEList->a[0].pExpr )((void) (0));
119167 if( pEList->a[0].pExpr->op!=TK_ASTERISK172 ){
119168 return 0; /* The result set must be the special operator "*" */
119169 }
119170
119171 /* At this point we have established that the statement is of the
119172 ** correct syntactic form to participate in this optimization. Now
119173 ** we have to check the semantics.
119174 */
119175 pItem = pSelect->pSrc->a;
119176 pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
119177 if( pSrc==0 ){
119178 return 0; /* FROM clause does not contain a real table */
119179 }
119180 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
119181 testcase( pSrc!=pDest ); /* Possible due to bad sqlite_master.rootpage */
119182 return 0; /* tab1 and tab2 may not be the same table */
119183 }
119184 if( HasRowid(pDest)(((pDest)->tabFlags & 0x0020)==0)!=HasRowid(pSrc)(((pSrc)->tabFlags & 0x0020)==0) ){
119185 return 0; /* source and destination must both be WITHOUT ROWID or not */
119186 }
119187#ifndef SQLITE_OMIT_VIRTUALTABLE
119188 if( IsVirtual(pSrc)((pSrc)->nModuleArg) ){
119189 return 0; /* tab2 must not be a virtual table */
119190 }
119191#endif
119192 if( pSrc->pSelect ){
119193 return 0; /* tab2 may not be a view */
119194 }
119195 if( pDest->nCol!=pSrc->nCol ){
119196 return 0; /* Number of columns must be the same in tab1 and tab2 */
119197 }
119198 if( pDest->iPKey!=pSrc->iPKey ){
119199 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
119200 }
119201 for(i=0; i<pDest->nCol; i++){
119202 Column *pDestCol = &pDest->aCol[i];
119203 Column *pSrcCol = &pSrc->aCol[i];
119204#ifdef SQLITE_ENABLE_HIDDEN_COLUMNS
119205 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0
119206 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN0x0002
119207 ){
119208 return 0; /* Neither table may have __hidden__ columns */
119209 }
119210#endif
119211 if( pDestCol->affinity!=pSrcCol->affinity ){
119212 return 0; /* Affinity must be the same on all columns */
119213 }
119214 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
119215 return 0; /* Collating sequence must be the same on all columns */
119216 }
119217 if( pDestCol->notNull && !pSrcCol->notNull ){
119218 return 0; /* tab2 must be NOT NULL if tab1 is */
119219 }
119220 /* Default values for second and subsequent columns need to match. */
119221 if( i>0 ){
119222 assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN )((void) (0));
119223 assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN )((void) (0));
119224 if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
119225 || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
119226 pSrcCol->pDflt->u.zToken)!=0)
119227 ){
119228 return 0; /* Default values must be the same for all columns */
119229 }
119230 }
119231 }
119232 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
119233 if( IsUniqueIndex(pDestIdx)((pDestIdx)->onError!=0) ){
119234 destHasUniqueIdx = 1;
119235 }
119236 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
119237 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
119238 }
119239 if( pSrcIdx==0 ){
119240 return 0; /* pDestIdx has no corresponding index in pSrc */
119241 }
119242 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
119243 && sqlite3FaultSim(411)==SQLITE_OK0 ){
119244 /* The sqlite3FaultSim() call allows this corruption test to be
119245 ** bypassed during testing, in order to exercise other corruption tests
119246 ** further downstream. */
119247 return 0; /* Corrupt schema - two indexes on the same btree */
119248 }
119249 }
119250#ifndef SQLITE_OMIT_CHECK
119251 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
119252 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
119253 }
119254#endif
119255#ifndef SQLITE_OMIT_FOREIGN_KEY
119256 /* Disallow the transfer optimization if the destination table constains
119257 ** any foreign key constraints. This is more restrictive than necessary.
119258 ** But the main beneficiary of the transfer optimization is the VACUUM
119259 ** command, and the VACUUM command disables foreign key constraints. So
119260 ** the extra complication to make this rule less restrictive is probably
119261 ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
119262 */
119263 if( (db->flags & SQLITE_ForeignKeys0x00004000)!=0 && pDest->pFKey!=0 ){
119264 return 0;
119265 }
119266#endif
119267 if( (db->flags & SQLITE_CountRows0x00000080)!=0 ){
119268 return 0; /* xfer opt does not play well with PRAGMA count_changes */
119269 }
119270
119271 /* If we get this far, it means that the xfer optimization is at
119272 ** least a possibility, though it might only work if the destination
119273 ** table (tab1) is initially empty.
119274 */
119275#ifdef SQLITE_TEST
119276 sqlite3_xferopt_count++;
119277#endif
119278 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
119279 v = sqlite3GetVdbe(pParse);
119280 sqlite3CodeVerifySchema(pParse, iDbSrc);
119281 iSrc = pParse->nTab++;
119282 iDest = pParse->nTab++;
119283 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
119284 regData = sqlite3GetTempReg(pParse);
119285 regRowid = sqlite3GetTempReg(pParse);
119286 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite109);
119287 assert( HasRowid(pDest) || destHasUniqueIdx )((void) (0));
119288 if( (db->mDbFlags & DBFLAG_Vacuum0x0004)==0 && (
119289 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
119290 || destHasUniqueIdx /* (2) */
119291 || (onError!=OE_Abort2 && onError!=OE_Rollback1) /* (3) */
119292 )){
119293 /* In some circumstances, we are able to run the xfer optimization
119294 ** only if the destination table is initially empty. Unless the
119295 ** DBFLAG_Vacuum flag is set, this block generates code to make
119296 ** that determination. If DBFLAG_Vacuum is set, then the destination
119297 ** table is always empty.
119298 **
119299 ** Conditions under which the destination must be empty:
119300 **
119301 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
119302 ** (If the destination is not initially empty, the rowid fields
119303 ** of index entries might need to change.)
119304 **
119305 ** (2) The destination has a unique index. (The xfer optimization
119306 ** is unable to test uniqueness.)
119307 **
119308 ** (3) onError is something other than OE_Abort and OE_Rollback.
119309 */
119310 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind36, iDest, 0); VdbeCoverage(v);
119311 emptyDestTest = sqlite3VdbeAddOp0(v, OP_Goto11);
119312 sqlite3VdbeJumpHere(v, addr1);
119313 }
119314 if( HasRowid(pSrc)(((pSrc)->tabFlags & 0x0020)==0) ){
119315 u8 insFlags;
119316 sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead108);
119317 emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind36, iSrc, 0); VdbeCoverage(v);
119318 if( pDest->iPKey>=0 ){
119319 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid128, iSrc, regRowid);
119320 sqlite3VdbeVerifyAbortable(v, onError);
119321 addr2 = sqlite3VdbeAddOp3(v, OP_NotExists31, iDest, 0, regRowid);
119322 VdbeCoverage(v);
119323 sqlite3RowidConstraint(pParse, onError, pDest);
119324 sqlite3VdbeJumpHere(v, addr2);
119325 autoIncStep(pParse, regAutoinc, regRowid);
119326 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto0x0008) ){
119327 addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid121, iDest, regRowid);
119328 }else{
119329 addr1 = sqlite3VdbeAddOp2(v, OP_Rowid128, iSrc, regRowid);
119330 assert( (pDest->tabFlags & TF_Autoincrement)==0 )((void) (0));
119331 }
119332 sqlite3VdbeAddOp3(v, OP_RowData127, iSrc, regData, 1);
119333 if( db->mDbFlags & DBFLAG_Vacuum0x0004 ){
119334 sqlite3VdbeAddOp1(v, OP_SeekEnd130, iDest);
119335 insFlags = OPFLAG_NCHANGE0x01|OPFLAG_LASTROWID0x20|
119336 OPFLAG_APPEND0x08|OPFLAG_USESEEKRESULT0x10;
119337 }else{
119338 insFlags = OPFLAG_NCHANGE0x01|OPFLAG_LASTROWID0x20|OPFLAG_APPEND0x08;
119339 }
119340 sqlite3VdbeAddOp4(v, OP_Insert122, iDest, regData, regRowid,
119341 (char*)pDest, P4_TABLE(-6));
119342 sqlite3VdbeChangeP5(v, insFlags);
119343 sqlite3VdbeAddOp2(v, OP_Next5, iSrc, addr1); VdbeCoverage(v);
119344 sqlite3VdbeAddOp2(v, OP_Close117, iSrc, 0);
119345 sqlite3VdbeAddOp2(v, OP_Close117, iDest, 0);
119346 }else{
119347 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
119348 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
119349 }
119350 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
119351 u8 idxInsFlags = 0;
119352 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx)(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
119353 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
119354 }
119355 assert( pSrcIdx )((void) (0));
119356 sqlite3VdbeAddOp3(v, OP_OpenRead108, iSrc, pSrcIdx->tnum, iDbSrc);
119357 sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
119358 VdbeComment((v, "%s", pSrcIdx->zName));
119359 sqlite3VdbeAddOp3(v, OP_OpenWrite109, iDest, pDestIdx->tnum, iDbDest);
119360 sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
119361 sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR0x01);
119362 VdbeComment((v, "%s", pDestIdx->zName));
119363 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind36, iSrc, 0); VdbeCoverage(v);
119364 sqlite3VdbeAddOp3(v, OP_RowData127, iSrc, regData, 1);
119365 if( db->mDbFlags & DBFLAG_Vacuum0x0004 ){
119366 /* This INSERT command is part of a VACUUM operation, which guarantees
119367 ** that the destination table is empty. If all indexed columns use
119368 ** collation sequence BINARY, then it can also be assumed that the
119369 ** index will be populated by inserting keys in strictly sorted
119370 ** order. In this case, instead of seeking within the b-tree as part
119371 ** of every OP_IdxInsert opcode, an OP_SeekEnd is added before the
119372 ** OP_IdxInsert to seek to the point within the b-tree where each key
119373 ** should be inserted. This is faster.
119374 **
119375 ** If any of the indexed columns use a collation sequence other than
119376 ** BINARY, this optimization is disabled. This is because the user
119377 ** might change the definition of a collation sequence and then run
119378 ** a VACUUM command. In that case keys may not be written in strictly
119379 ** sorted order. */
119380 for(i=0; i<pSrcIdx->nColumn; i++){
119381 const char *zColl = pSrcIdx->azColl[i];
119382 if( sqlite3_stricmp(sqlite3StrBINARY, zColl) ) break;
119383 }
119384 if( i==pSrcIdx->nColumn ){
119385 idxInsFlags = OPFLAG_USESEEKRESULT0x10;
119386 sqlite3VdbeAddOp1(v, OP_SeekEnd130, iDest);
119387 }
119388 }
119389 if( !HasRowid(pSrc)(((pSrc)->tabFlags & 0x0020)==0) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY2 ){
119390 idxInsFlags |= OPFLAG_NCHANGE0x01;
119391 }
119392 sqlite3VdbeAddOp2(v, OP_IdxInsert132, iDest, regData);
119393 sqlite3VdbeChangeP5(v, idxInsFlags|OPFLAG_APPEND0x08);
119394 sqlite3VdbeAddOp2(v, OP_Next5, iSrc, addr1+1); VdbeCoverage(v);
119395 sqlite3VdbeJumpHere(v, addr1);
119396 sqlite3VdbeAddOp2(v, OP_Close117, iSrc, 0);
119397 sqlite3VdbeAddOp2(v, OP_Close117, iDest, 0);
119398 }
119399 if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
119400 sqlite3ReleaseTempReg(pParse, regRowid);
119401 sqlite3ReleaseTempReg(pParse, regData);
119402 if( emptyDestTest ){
119403 sqlite3AutoincrementEnd(pParse);
119404 sqlite3VdbeAddOp2(v, OP_Halt69, SQLITE_OK0, 0);
119405 sqlite3VdbeJumpHere(v, emptyDestTest);
119406 sqlite3VdbeAddOp2(v, OP_Close117, iDest, 0);
119407 return 0;
119408 }else{
119409 return 1;
119410 }
119411}
119412#endif /* SQLITE_OMIT_XFER_OPT */
119413
119414/************** End of insert.c **********************************************/
119415/************** Begin file legacy.c ******************************************/
119416/*
119417** 2001 September 15
119418**
119419** The author disclaims copyright to this source code. In place of
119420** a legal notice, here is a blessing:
119421**
119422** May you do good and not evil.
119423** May you find forgiveness for yourself and forgive others.
119424** May you share freely, never taking more than you give.
119425**
119426*************************************************************************
119427** Main file for the SQLite library. The routines in this file
119428** implement the programmer interface to the library. Routines in
119429** other files are for internal use by SQLite and should not be
119430** accessed by users of the library.
119431*/
119432
119433/* #include "sqliteInt.h" */
119434
119435/*
119436** Execute SQL code. Return one of the SQLITE_ success/failure
119437** codes. Also write an error message into memory obtained from
119438** malloc() and make *pzErrMsg point to that message.
119439**
119440** If the SQL is a query, then for each row in the query result
119441** the xCallback() function is called. pArg becomes the first
119442** argument to xCallback(). If xCallback=NULL then no callback
119443** is invoked, even for queries.
119444*/
119445SQLITE_API int sqlite3_exec(
119446 sqlite3 *db, /* The database on which the SQL executes */
119447 const char *zSql, /* The SQL to be executed */
119448 sqlite3_callback xCallback, /* Invoke this callback routine */
119449 void *pArg, /* First argument to xCallback() */
119450 char **pzErrMsg /* Write error messages here */
119451){
119452 int rc = SQLITE_OK0; /* Return code */
119453 const char *zLeftover; /* Tail of unprocessed SQL */
119454 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
119455 char **azCols = 0; /* Names of result columns */
119456 int callbackIsInit; /* True if callback data is initialized */
119457
119458 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(119458);
119459 if( zSql==0 ) zSql = "";
119460
119461 sqlite3_mutex_enter(db->mutex);
119462 sqlite3Error(db, SQLITE_OK0);
119463 while( rc==SQLITE_OK0 && zSql[0] ){
119464 int nCol = 0;
119465 char **azVals = 0;
119466
119467 pStmt = 0;
119468 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
119469 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
119470 if( rc!=SQLITE_OK0 ){
119471 continue;
119472 }
119473 if( !pStmt ){
119474 /* this happens for a comment or white-space */
119475 zSql = zLeftover;
119476 continue;
119477 }
119478 callbackIsInit = 0;
119479
119480 while( 1 ){
119481 int i;
119482 rc = sqlite3_step(pStmt);
119483
119484 /* Invoke the callback function if required */
119485 if( xCallback && (SQLITE_ROW100==rc ||
119486 (SQLITE_DONE101==rc && !callbackIsInit
119487 && db->flags&SQLITE_NullCallback0x00000100)) ){
119488 if( !callbackIsInit ){
119489 nCol = sqlite3_column_count(pStmt);
119490 azCols = sqlite3DbMallocRaw(db, (2*nCol+1)*sizeof(const char*));
119491 if( azCols==0 ){
119492 goto exec_out;
119493 }
119494 for(i=0; i<nCol; i++){
119495 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
119496 /* sqlite3VdbeSetColName() installs column names as UTF8
119497 ** strings so there is no way for sqlite3_column_name() to fail. */
119498 assert( azCols[i]!=0 )((void) (0));
119499 }
119500 callbackIsInit = 1;
119501 }
119502 if( rc==SQLITE_ROW100 ){
119503 azVals = &azCols[nCol];
119504 for(i=0; i<nCol; i++){
119505 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
119506 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL5 ){
119507 sqlite3OomFault(db);
119508 goto exec_out;
119509 }
119510 }
119511 azVals[i] = 0;
119512 }
119513 if( xCallback(pArg, nCol, azVals, azCols) ){
119514 /* EVIDENCE-OF: R-38229-40159 If the callback function to
119515 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
119516 ** return SQLITE_ABORT. */
119517 rc = SQLITE_ABORT4;
119518 sqlite3VdbeFinalize((Vdbe *)pStmt);
119519 pStmt = 0;
119520 sqlite3Error(db, SQLITE_ABORT4);
119521 goto exec_out;
119522 }
119523 }
119524
119525 if( rc!=SQLITE_ROW100 ){
119526 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
119527 pStmt = 0;
119528 zSql = zLeftover;
119529 while( sqlite3Isspace(zSql[0])(sqlite3CtypeMap[(unsigned char)(zSql[0])]&0x01) ) zSql++;
119530 break;
119531 }
119532 }
119533
119534 sqlite3DbFree(db, azCols);
119535 azCols = 0;
119536 }
119537
119538exec_out:
119539 if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
119540 sqlite3DbFree(db, azCols);
119541
119542 rc = sqlite3ApiExit(db, rc);
119543 if( rc!=SQLITE_OK0 && pzErrMsg ){
119544 *pzErrMsg = sqlite3DbStrDup(0, sqlite3_errmsg(db));
119545 if( *pzErrMsg==0 ){
119546 rc = SQLITE_NOMEM_BKPT7;
119547 sqlite3Error(db, SQLITE_NOMEM7);
119548 }
119549 }else if( pzErrMsg ){
119550 *pzErrMsg = 0;
119551 }
119552
119553 assert( (rc&db->errMask)==rc )((void) (0));
119554 sqlite3_mutex_leave(db->mutex);
119555 return rc;
119556}
119557
119558/************** End of legacy.c **********************************************/
119559/************** Begin file loadext.c *****************************************/
119560/*
119561** 2006 June 7
119562**
119563** The author disclaims copyright to this source code. In place of
119564** a legal notice, here is a blessing:
119565**
119566** May you do good and not evil.
119567** May you find forgiveness for yourself and forgive others.
119568** May you share freely, never taking more than you give.
119569**
119570*************************************************************************
119571** This file contains code used to dynamically load extensions into
119572** the SQLite library.
119573*/
119574
119575#ifndef SQLITE_CORE1
119576 #define SQLITE_CORE1 1 /* Disable the API redefinition in sqlite3ext.h */
119577#endif
119578/************** Include sqlite3ext.h in the middle of loadext.c **************/
119579/************** Begin file sqlite3ext.h **************************************/
119580/*
119581** 2006 June 7
119582**
119583** The author disclaims copyright to this source code. In place of
119584** a legal notice, here is a blessing:
119585**
119586** May you do good and not evil.
119587** May you find forgiveness for yourself and forgive others.
119588** May you share freely, never taking more than you give.
119589**
119590*************************************************************************
119591** This header file defines the SQLite interface for use by
119592** shared libraries that want to be imported as extensions into
119593** an SQLite instance. Shared libraries that intend to be loaded
119594** as extensions by SQLite should #include this file instead of
119595** sqlite3.h.
119596*/
119597#ifndef SQLITE3EXT_H
119598#define SQLITE3EXT_H
119599/* #include "sqlite3.h" */
119600
119601/*
119602** The following structure holds pointers to all of the SQLite API
119603** routines.
119604**
119605** WARNING: In order to maintain backwards compatibility, add new
119606** interfaces to the end of this structure only. If you insert new
119607** interfaces in the middle of this structure, then older different
119608** versions of SQLite will not be able to load each other's shared
119609** libraries!
119610*/
119611struct sqlite3_api_routines {
119612 void * (*aggregate_context)(sqlite3_context*,int nBytes);
119613 int (*aggregate_count)(sqlite3_context*);
119614 int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
119615 int (*bind_double)(sqlite3_stmt*,int,double);
119616 int (*bind_int)(sqlite3_stmt*,int,int);
119617 int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
119618 int (*bind_null)(sqlite3_stmt*,int);
119619 int (*bind_parameter_count)(sqlite3_stmt*);
119620 int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
119621 const char * (*bind_parameter_name)(sqlite3_stmt*,int);
119622 int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
119623 int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
119624 int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
119625 int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
119626 int (*busy_timeout)(sqlite3*,int ms);
119627 int (*changes)(sqlite3*);
119628 int (*close)(sqlite3*);
119629 int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
119630 int eTextRep,const char*));
119631 int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
119632 int eTextRep,const void*));
119633 const void * (*column_blob)(sqlite3_stmt*,int iCol);
119634 int (*column_bytes)(sqlite3_stmt*,int iCol);
119635 int (*column_bytes16)(sqlite3_stmt*,int iCol);
119636 int (*column_count)(sqlite3_stmt*pStmt);
119637 const char * (*column_database_name)(sqlite3_stmt*,int);
119638 const void * (*column_database_name16)(sqlite3_stmt*,int);
119639 const char * (*column_decltype)(sqlite3_stmt*,int i);
119640 const void * (*column_decltype16)(sqlite3_stmt*,int);
119641 double (*column_double)(sqlite3_stmt*,int iCol);
119642 int (*column_int)(sqlite3_stmt*,int iCol);
119643 sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
119644 const char * (*column_name)(sqlite3_stmt*,int);
119645 const void * (*column_name16)(sqlite3_stmt*,int);
119646 const char * (*column_origin_name)(sqlite3_stmt*,int);
119647 const void * (*column_origin_name16)(sqlite3_stmt*,int);
119648 const char * (*column_table_name)(sqlite3_stmt*,int);
119649 const void * (*column_table_name16)(sqlite3_stmt*,int);
119650 const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
119651 const void * (*column_text16)(sqlite3_stmt*,int iCol);
119652 int (*column_type)(sqlite3_stmt*,int iCol);
119653 sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
119654 void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
119655 int (*complete)(const char*sql);
119656 int (*complete16)(const void*sql);
119657 int (*create_collation)(sqlite3*,const char*,int,void*,
119658 int(*)(void*,int,const void*,int,const void*));
119659 int (*create_collation16)(sqlite3*,const void*,int,void*,
119660 int(*)(void*,int,const void*,int,const void*));
119661 int (*create_function)(sqlite3*,const char*,int,int,void*,
119662 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
119663 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
119664 void (*xFinal)(sqlite3_context*));
119665 int (*create_function16)(sqlite3*,const void*,int,int,void*,
119666 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
119667 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
119668 void (*xFinal)(sqlite3_context*));
119669 int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
119670 int (*data_count)(sqlite3_stmt*pStmt);
119671 sqlite3 * (*db_handle)(sqlite3_stmt*);
119672 int (*declare_vtab)(sqlite3*,const char*);
119673 int (*enable_shared_cache)(int);
119674 int (*errcode)(sqlite3*db);
119675 const char * (*errmsg)(sqlite3*);
119676 const void * (*errmsg16)(sqlite3*);
119677 int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
119678 int (*expired)(sqlite3_stmt*);
119679 int (*finalize)(sqlite3_stmt*pStmt);
119680 void (*free)(void*);
119681 void (*free_table)(char**result);
119682 int (*get_autocommit)(sqlite3*);
119683 void * (*get_auxdata)(sqlite3_context*,int);
119684 int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
119685 int (*global_recover)(void);
119686 void (*interruptx)(sqlite3*);
119687 sqlite_int64 (*last_insert_rowid)(sqlite3*);
119688 const char * (*libversion)(void);
119689 int (*libversion_number)(void);
119690 void *(*malloc)(int);
119691 char * (*mprintf)(const char*,...);
119692 int (*open)(const char*,sqlite3**);
119693 int (*open16)(const void*,sqlite3**);
119694 int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
119695 int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
119696 void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
119697 void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
119698 void *(*realloc)(void*,int);
119699 int (*reset)(sqlite3_stmt*pStmt);
119700 void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
119701 void (*result_double)(sqlite3_context*,double);
119702 void (*result_error)(sqlite3_context*,const char*,int);
119703 void (*result_error16)(sqlite3_context*,const void*,int);
119704 void (*result_int)(sqlite3_context*,int);
119705 void (*result_int64)(sqlite3_context*,sqlite_int64);
119706 void (*result_null)(sqlite3_context*);
119707 void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
119708 void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
119709 void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
119710 void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
119711 void (*result_value)(sqlite3_context*,sqlite3_value*);
119712 void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
119713 int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
119714 const char*,const char*),void*);
119715 void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
119716 char * (*xsnprintf)(int,char*,const char*,...);
119717 int (*step)(sqlite3_stmt*);
119718 int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
119719 char const**,char const**,int*,int*,int*);
119720 void (*thread_cleanup)(void);
119721 int (*total_changes)(sqlite3*);
119722 void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
119723 int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
119724 void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
119725 sqlite_int64),void*);
119726 void * (*user_data)(sqlite3_context*);
119727 const void * (*value_blob)(sqlite3_value*);
119728 int (*value_bytes)(sqlite3_value*);
119729 int (*value_bytes16)(sqlite3_value*);
119730 double (*value_double)(sqlite3_value*);
119731 int (*value_int)(sqlite3_value*);
119732 sqlite_int64 (*value_int64)(sqlite3_value*);
119733 int (*value_numeric_type)(sqlite3_value*);
119734 const unsigned char * (*value_text)(sqlite3_value*);
119735 const void * (*value_text16)(sqlite3_value*);
119736 const void * (*value_text16be)(sqlite3_value*);
119737 const void * (*value_text16le)(sqlite3_value*);
119738 int (*value_type)(sqlite3_value*);
119739 char *(*vmprintf)(const char*,va_list);
119740 /* Added ??? */
119741 int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
119742 /* Added by 3.3.13 */
119743 int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
119744 int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
119745 int (*clear_bindings)(sqlite3_stmt*);
119746 /* Added by 3.4.1 */
119747 int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
119748 void (*xDestroy)(void *));
119749 /* Added by 3.5.0 */
119750 int (*bind_zeroblob)(sqlite3_stmt*,int,int);
119751 int (*blob_bytes)(sqlite3_blob*);
119752 int (*blob_close)(sqlite3_blob*);
119753 int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
119754 int,sqlite3_blob**);
119755 int (*blob_read)(sqlite3_blob*,void*,int,int);
119756 int (*blob_write)(sqlite3_blob*,const void*,int,int);
119757 int (*create_collation_v2)(sqlite3*,const char*,int,void*,
119758 int(*)(void*,int,const void*,int,const void*),
119759 void(*)(void*));
119760 int (*file_control)(sqlite3*,const char*,int,void*);
119761 sqlite3_int64 (*memory_highwater)(int);
119762 sqlite3_int64 (*memory_used)(void);
119763 sqlite3_mutex *(*mutex_alloc)(int);
119764 void (*mutex_enter)(sqlite3_mutex*);
119765 void (*mutex_free)(sqlite3_mutex*);
119766 void (*mutex_leave)(sqlite3_mutex*);
119767 int (*mutex_try)(sqlite3_mutex*);
119768 int (*open_v2)(const char*,sqlite3**,int,const char*);
119769 int (*release_memory)(int);
119770 void (*result_error_nomem)(sqlite3_context*);
119771 void (*result_error_toobig)(sqlite3_context*);
119772 int (*sleep)(int);
119773 void (*soft_heap_limit)(int);
119774 sqlite3_vfs *(*vfs_find)(const char*);
119775 int (*vfs_register)(sqlite3_vfs*,int);
119776 int (*vfs_unregister)(sqlite3_vfs*);
119777 int (*xthreadsafe)(void);
119778 void (*result_zeroblob)(sqlite3_context*,int);
119779 void (*result_error_code)(sqlite3_context*,int);
119780 int (*test_control)(int, ...);
119781 void (*randomness)(int,void*);
119782 sqlite3 *(*context_db_handle)(sqlite3_context*);
119783 int (*extended_result_codes)(sqlite3*,int);
119784 int (*limit)(sqlite3*,int,int);
119785 sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
119786 const char *(*sql)(sqlite3_stmt*);
119787 int (*status)(int,int*,int*,int);
119788 int (*backup_finish)(sqlite3_backup*);
119789 sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
119790 int (*backup_pagecount)(sqlite3_backup*);
119791 int (*backup_remaining)(sqlite3_backup*);
119792 int (*backup_step)(sqlite3_backup*,int);
119793 const char *(*compileoption_get)(int);
119794 int (*compileoption_used)(const char*);
119795 int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
119796 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
119797 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
119798 void (*xFinal)(sqlite3_context*),
119799 void(*xDestroy)(void*));
119800 int (*db_config)(sqlite3*,int,...);
119801 sqlite3_mutex *(*db_mutex)(sqlite3*);
119802 int (*db_status)(sqlite3*,int,int*,int*,int);
119803 int (*extended_errcode)(sqlite3*);
119804 void (*log)(int,const char*,...);
119805 sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
119806 const char *(*sourceid)(void);
119807 int (*stmt_status)(sqlite3_stmt*,int,int);
119808 int (*strnicmp)(const char*,const char*,int);
119809 int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
119810 int (*wal_autocheckpoint)(sqlite3*,int);
119811 int (*wal_checkpoint)(sqlite3*,const char*);
119812 void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
119813 int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
119814 int (*vtab_config)(sqlite3*,int op,...);
119815 int (*vtab_on_conflict)(sqlite3*);
119816 /* Version 3.7.16 and later */
119817 int (*close_v2)(sqlite3*);
119818 const char *(*db_filename)(sqlite3*,const char*);
119819 int (*db_readonly)(sqlite3*,const char*);
119820 int (*db_release_memory)(sqlite3*);
119821 const char *(*errstr)(int);
119822 int (*stmt_busy)(sqlite3_stmt*);
119823 int (*stmt_readonly)(sqlite3_stmt*);
119824 int (*stricmp)(const char*,const char*);
119825 int (*uri_boolean)(const char*,const char*,int);
119826 sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
119827 const char *(*uri_parameter)(const char*,const char*);
119828 char *(*xvsnprintf)(int,char*,const char*,va_list);
119829 int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
119830 /* Version 3.8.7 and later */
119831 int (*auto_extension)(void(*)(void));
119832 int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
119833 void(*)(void*));
119834 int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
119835 void(*)(void*),unsigned char);
119836 int (*cancel_auto_extension)(void(*)(void));
119837 int (*load_extension)(sqlite3*,const char*,const char*,char**);
119838 void *(*malloc64)(sqlite3_uint64);
119839 sqlite3_uint64 (*msize)(void*);
119840 void *(*realloc64)(void*,sqlite3_uint64);
119841 void (*reset_auto_extension)(void);
119842 void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
119843 void(*)(void*));
119844 void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
119845 void(*)(void*), unsigned char);
119846 int (*strglob)(const char*,const char*);
119847 /* Version 3.8.11 and later */
119848 sqlite3_value *(*value_dup)(const sqlite3_value*);
119849 void (*value_free)(sqlite3_value*);
119850 int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
119851 int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
119852 /* Version 3.9.0 and later */
119853 unsigned int (*value_subtype)(sqlite3_value*);
119854 void (*result_subtype)(sqlite3_context*,unsigned int);
119855 /* Version 3.10.0 and later */
119856 int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
119857 int (*strlike)(const char*,const char*,unsigned int);
119858 int (*db_cacheflush)(sqlite3*);
119859 /* Version 3.12.0 and later */
119860 int (*system_errno)(sqlite3*);
119861 /* Version 3.14.0 and later */
119862 int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
119863 char *(*expanded_sql)(sqlite3_stmt*);
119864 /* Version 3.18.0 and later */
119865 void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
119866 /* Version 3.20.0 and later */
119867 int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
119868 sqlite3_stmt**,const char**);
119869 int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
119870 sqlite3_stmt**,const void**);
119871 int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
119872 void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
119873 void *(*value_pointer)(sqlite3_value*,const char*);
119874 int (*vtab_nochange)(sqlite3_context*);
119875 int (*value_nochange)(sqlite3_value*);
119876 const char *(*vtab_collation)(sqlite3_index_info*,int);
119877 /* Version 3.24.0 and later */
119878 int (*keyword_count)(void);
119879 int (*keyword_name)(int,const char**,int*);
119880 int (*keyword_check)(const char*,int);
119881 sqlite3_str *(*str_new)(sqlite3*);
119882 char *(*str_finish)(sqlite3_str*);
119883 void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
119884 void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
119885 void (*str_append)(sqlite3_str*, const char *zIn, int N);
119886 void (*str_appendall)(sqlite3_str*, const char *zIn);
119887 void (*str_appendchar)(sqlite3_str*, int N, char C);
119888 void (*str_reset)(sqlite3_str*);
119889 int (*str_errcode)(sqlite3_str*);
119890 int (*str_length)(sqlite3_str*);
119891 char *(*str_value)(sqlite3_str*);
119892 /* Version 3.25.0 and later */
119893 int (*create_window_function)(sqlite3*,const char*,int,int,void*,
119894 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
119895 void (*xFinal)(sqlite3_context*),
119896 void (*xValue)(sqlite3_context*),
119897 void (*xInv)(sqlite3_context*,int,sqlite3_value**),
119898 void(*xDestroy)(void*));
119899 /* Version 3.26.0 and later */
119900 const char *(*normalized_sql)(sqlite3_stmt*);
119901 /* Version 3.28.0 and later */
119902 int (*stmt_isexplain)(sqlite3_stmt*);
119903 int (*value_frombind)(sqlite3_value*);
119904};
119905
119906/*
119907** This is the function signature used for all extension entry points. It
119908** is also defined in the file "loadext.c".
119909*/
119910typedef int (*sqlite3_loadext_entry)(
119911 sqlite3 *db, /* Handle to the database. */
119912 char **pzErrMsg, /* Used to set error string on failure. */
119913 const sqlite3_api_routines *pThunk /* Extension API function pointers. */
119914);
119915
119916/*
119917** The following macros redefine the API routines so that they are
119918** redirected through the global sqlite3_api structure.
119919**
119920** This header file is also used by the loadext.c source file
119921** (part of the main SQLite library - not an extension) so that
119922** it can get access to the sqlite3_api_routines structure
119923** definition. But the main library does not want to redefine
119924** the API. So the redefinition macros are only valid if the
119925** SQLITE_CORE macros is undefined.
119926*/
119927#if !defined(SQLITE_CORE1) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
119928#define sqlite3_aggregate_context sqlite3_api->aggregate_context
119929#ifndef SQLITE_OMIT_DEPRECATED
119930#define sqlite3_aggregate_count sqlite3_api->aggregate_count
119931#endif
119932#define sqlite3_bind_blob sqlite3_api->bind_blob
119933#define sqlite3_bind_double sqlite3_api->bind_double
119934#define sqlite3_bind_int sqlite3_api->bind_int
119935#define sqlite3_bind_int64 sqlite3_api->bind_int64
119936#define sqlite3_bind_null sqlite3_api->bind_null
119937#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
119938#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
119939#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
119940#define sqlite3_bind_text sqlite3_api->bind_text
119941#define sqlite3_bind_text16 sqlite3_api->bind_text16
119942#define sqlite3_bind_value sqlite3_api->bind_value
119943#define sqlite3_busy_handler sqlite3_api->busy_handler
119944#define sqlite3_busy_timeout sqlite3_api->busy_timeout
119945#define sqlite3_changes sqlite3_api->changes
119946#define sqlite3_close sqlite3_api->close
119947#define sqlite3_collation_needed sqlite3_api->collation_needed
119948#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
119949#define sqlite3_column_blob sqlite3_api->column_blob
119950#define sqlite3_column_bytes sqlite3_api->column_bytes
119951#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
119952#define sqlite3_column_count sqlite3_api->column_count
119953#define sqlite3_column_database_name0 sqlite3_api->column_database_name
119954#define sqlite3_column_database_name160 sqlite3_api->column_database_name16
119955#define sqlite3_column_decltype sqlite3_api->column_decltype
119956#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
119957#define sqlite3_column_double sqlite3_api->column_double
119958#define sqlite3_column_int sqlite3_api->column_int
119959#define sqlite3_column_int64 sqlite3_api->column_int64
119960#define sqlite3_column_name sqlite3_api->column_name
119961#define sqlite3_column_name16 sqlite3_api->column_name16
119962#define sqlite3_column_origin_name0 sqlite3_api->column_origin_name
119963#define sqlite3_column_origin_name160 sqlite3_api->column_origin_name16
119964#define sqlite3_column_table_name0 sqlite3_api->column_table_name
119965#define sqlite3_column_table_name160 sqlite3_api->column_table_name16
119966#define sqlite3_column_text sqlite3_api->column_text
119967#define sqlite3_column_text16 sqlite3_api->column_text16
119968#define sqlite3_column_type sqlite3_api->column_type
119969#define sqlite3_column_value sqlite3_api->column_value
119970#define sqlite3_commit_hook sqlite3_api->commit_hook
119971#define sqlite3_complete sqlite3_api->complete
119972#define sqlite3_complete16 sqlite3_api->complete16
119973#define sqlite3_create_collation sqlite3_api->create_collation
119974#define sqlite3_create_collation16 sqlite3_api->create_collation16
119975#define sqlite3_create_function sqlite3_api->create_function
119976#define sqlite3_create_function16 sqlite3_api->create_function16
119977#define sqlite3_create_module sqlite3_api->create_module
119978#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
119979#define sqlite3_data_count sqlite3_api->data_count
119980#define sqlite3_db_handle sqlite3_api->db_handle
119981#define sqlite3_declare_vtab sqlite3_api->declare_vtab
119982#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
119983#define sqlite3_errcode sqlite3_api->errcode
119984#define sqlite3_errmsg sqlite3_api->errmsg
119985#define sqlite3_errmsg16 sqlite3_api->errmsg16
119986#define sqlite3_exec sqlite3_api->exec
119987#ifndef SQLITE_OMIT_DEPRECATED
119988#define sqlite3_expired sqlite3_api->expired
119989#endif
119990#define sqlite3_finalize sqlite3_api->finalize
119991#define sqlite3_free sqlite3_api->free
119992#define sqlite3_free_table sqlite3_api->free_table
119993#define sqlite3_get_autocommit sqlite3_api->get_autocommit
119994#define sqlite3_get_auxdata sqlite3_api->get_auxdata
119995#define sqlite3_get_table sqlite3_api->get_table
119996#ifndef SQLITE_OMIT_DEPRECATED
119997#define sqlite3_global_recover sqlite3_api->global_recover
119998#endif
119999#define sqlite3_interrupt sqlite3_api->interruptx
120000#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
120001#define sqlite3_libversion sqlite3_api->libversion
120002#define sqlite3_libversion_number sqlite3_api->libversion_number
120003#define sqlite3_malloc sqlite3_api->malloc
120004#define sqlite3_mprintf sqlite3_api->mprintf
120005#define sqlite3_open sqlite3_api->open
120006#define sqlite3_open16 sqlite3_api->open16
120007#define sqlite3_prepare sqlite3_api->prepare
120008#define sqlite3_prepare16 sqlite3_api->prepare16
120009#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
120010#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
120011#define sqlite3_profile sqlite3_api->profile
120012#define sqlite3_progress_handler sqlite3_api->progress_handler
120013#define sqlite3_realloc sqlite3_api->realloc
120014#define sqlite3_reset sqlite3_api->reset
120015#define sqlite3_result_blob sqlite3_api->result_blob
120016#define sqlite3_result_double sqlite3_api->result_double
120017#define sqlite3_result_error sqlite3_api->result_error
120018#define sqlite3_result_error16 sqlite3_api->result_error16
120019#define sqlite3_result_int sqlite3_api->result_int
120020#define sqlite3_result_int64 sqlite3_api->result_int64
120021#define sqlite3_result_null sqlite3_api->result_null
120022#define sqlite3_result_text sqlite3_api->result_text
120023#define sqlite3_result_text16 sqlite3_api->result_text16
120024#define sqlite3_result_text16be sqlite3_api->result_text16be
120025#define sqlite3_result_text16le sqlite3_api->result_text16le
120026#define sqlite3_result_value sqlite3_api->result_value
120027#define sqlite3_rollback_hook sqlite3_api->rollback_hook
120028#define sqlite3_set_authorizer sqlite3_api->set_authorizer
120029#define sqlite3_set_auxdata sqlite3_api->set_auxdata
120030#define sqlite3_snprintf sqlite3_api->xsnprintf
120031#define sqlite3_step sqlite3_api->step
120032#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
120033#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
120034#define sqlite3_total_changes sqlite3_api->total_changes
120035#define sqlite3_trace sqlite3_api->trace
120036#ifndef SQLITE_OMIT_DEPRECATED
120037#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
120038#endif
120039#define sqlite3_update_hook sqlite3_api->update_hook
120040#define sqlite3_user_data sqlite3_api->user_data
120041#define sqlite3_value_blob sqlite3_api->value_blob
120042#define sqlite3_value_bytes sqlite3_api->value_bytes
120043#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
120044#define sqlite3_value_double sqlite3_api->value_double
120045#define sqlite3_value_int sqlite3_api->value_int
120046#define sqlite3_value_int64 sqlite3_api->value_int64
120047#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
120048#define sqlite3_value_text sqlite3_api->value_text
120049#define sqlite3_value_text16 sqlite3_api->value_text16
120050#define sqlite3_value_text16be sqlite3_api->value_text16be
120051#define sqlite3_value_text16le sqlite3_api->value_text16le
120052#define sqlite3_value_type sqlite3_api->value_type
120053#define sqlite3_vmprintf sqlite3_api->vmprintf
120054#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
120055#define sqlite3_overload_function sqlite3_api->overload_function
120056#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
120057#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
120058#define sqlite3_clear_bindings sqlite3_api->clear_bindings
120059#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
120060#define sqlite3_blob_bytes sqlite3_api->blob_bytes
120061#define sqlite3_blob_close sqlite3_api->blob_close
120062#define sqlite3_blob_open sqlite3_api->blob_open
120063#define sqlite3_blob_read sqlite3_api->blob_read
120064#define sqlite3_blob_write sqlite3_api->blob_write
120065#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
120066#define sqlite3_file_control sqlite3_api->file_control
120067#define sqlite3_memory_highwater sqlite3_api->memory_highwater
120068#define sqlite3_memory_used sqlite3_api->memory_used
120069#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
120070#define sqlite3_mutex_enter sqlite3_api->mutex_enter
120071#define sqlite3_mutex_free sqlite3_api->mutex_free
120072#define sqlite3_mutex_leave sqlite3_api->mutex_leave
120073#define sqlite3_mutex_try sqlite3_api->mutex_try
120074#define sqlite3_open_v2 sqlite3_api->open_v2
120075#define sqlite3_release_memory sqlite3_api->release_memory
120076#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
120077#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
120078#define sqlite3_sleep sqlite3_api->sleep
120079#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
120080#define sqlite3_vfs_find sqlite3_api->vfs_find
120081#define sqlite3_vfs_register sqlite3_api->vfs_register
120082#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
120083#define sqlite3_threadsafe sqlite3_api->xthreadsafe
120084#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
120085#define sqlite3_result_error_code sqlite3_api->result_error_code
120086#define sqlite3_test_control sqlite3_api->test_control
120087#define sqlite3_randomness sqlite3_api->randomness
120088#define sqlite3_context_db_handle sqlite3_api->context_db_handle
120089#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
120090#define sqlite3_limit sqlite3_api->limit
120091#define sqlite3_next_stmt sqlite3_api->next_stmt
120092#define sqlite3_sql sqlite3_api->sql
120093#define sqlite3_status sqlite3_api->status
120094#define sqlite3_backup_finish sqlite3_api->backup_finish
120095#define sqlite3_backup_init sqlite3_api->backup_init
120096#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
120097#define sqlite3_backup_remaining sqlite3_api->backup_remaining
120098#define sqlite3_backup_step sqlite3_api->backup_step
120099#define sqlite3_compileoption_get sqlite3_api->compileoption_get
120100#define sqlite3_compileoption_used sqlite3_api->compileoption_used
120101#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
120102#define sqlite3_db_config sqlite3_api->db_config
120103#define sqlite3_db_mutex sqlite3_api->db_mutex
120104#define sqlite3_db_status sqlite3_api->db_status
120105#define sqlite3_extended_errcode sqlite3_api->extended_errcode
120106#define sqlite3_log sqlite3_api->log
120107#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
120108#define sqlite3_sourceid sqlite3_api->sourceid
120109#define sqlite3_stmt_status sqlite3_api->stmt_status
120110#define sqlite3_strnicmp sqlite3_api->strnicmp
120111#define sqlite3_unlock_notify sqlite3_api->unlock_notify
120112#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
120113#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
120114#define sqlite3_wal_hook sqlite3_api->wal_hook
120115#define sqlite3_blob_reopen sqlite3_api->blob_reopen
120116#define sqlite3_vtab_config sqlite3_api->vtab_config
120117#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
120118/* Version 3.7.16 and later */
120119#define sqlite3_close_v2 sqlite3_api->close_v2
120120#define sqlite3_db_filename sqlite3_api->db_filename
120121#define sqlite3_db_readonly sqlite3_api->db_readonly
120122#define sqlite3_db_release_memory sqlite3_api->db_release_memory
120123#define sqlite3_errstr sqlite3_api->errstr
120124#define sqlite3_stmt_busy sqlite3_api->stmt_busy
120125#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
120126#define sqlite3_stricmp sqlite3_api->stricmp
120127#define sqlite3_uri_boolean sqlite3_api->uri_boolean
120128#define sqlite3_uri_int64 sqlite3_api->uri_int64
120129#define sqlite3_uri_parameter sqlite3_api->uri_parameter
120130#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
120131#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
120132/* Version 3.8.7 and later */
120133#define sqlite3_auto_extension sqlite3_api->auto_extension
120134#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
120135#define sqlite3_bind_text64 sqlite3_api->bind_text64
120136#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
120137#define sqlite3_load_extension sqlite3_api->load_extension
120138#define sqlite3_malloc64 sqlite3_api->malloc64
120139#define sqlite3_msize sqlite3_api->msize
120140#define sqlite3_realloc64 sqlite3_api->realloc64
120141#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
120142#define sqlite3_result_blob64 sqlite3_api->result_blob64
120143#define sqlite3_result_text64 sqlite3_api->result_text64
120144#define sqlite3_strglob sqlite3_api->strglob
120145/* Version 3.8.11 and later */
120146#define sqlite3_value_dup sqlite3_api->value_dup
120147#define sqlite3_value_free sqlite3_api->value_free
120148#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
120149#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
120150/* Version 3.9.0 and later */
120151#define sqlite3_value_subtype sqlite3_api->value_subtype
120152#define sqlite3_result_subtype sqlite3_api->result_subtype
120153/* Version 3.10.0 and later */
120154#define sqlite3_status64 sqlite3_api->status64
120155#define sqlite3_strlike sqlite3_api->strlike
120156#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
120157/* Version 3.12.0 and later */
120158#define sqlite3_system_errno sqlite3_api->system_errno
120159/* Version 3.14.0 and later */
120160#define sqlite3_trace_v2 sqlite3_api->trace_v2
120161#define sqlite3_expanded_sql sqlite3_api->expanded_sql
120162/* Version 3.18.0 and later */
120163#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
120164/* Version 3.20.0 and later */
120165#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
120166#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
120167#define sqlite3_bind_pointer sqlite3_api->bind_pointer
120168#define sqlite3_result_pointer sqlite3_api->result_pointer
120169#define sqlite3_value_pointer sqlite3_api->value_pointer
120170/* Version 3.22.0 and later */
120171#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
120172#define sqlite3_value_nochange sqlite3_api->value_nochange
120173#define sqlite3_vtab_collation sqlite3_api->vtab_collation
120174/* Version 3.24.0 and later */
120175#define sqlite3_keyword_count sqlite3_api->keyword_count
120176#define sqlite3_keyword_name sqlite3_api->keyword_name
120177#define sqlite3_keyword_check sqlite3_api->keyword_check
120178#define sqlite3_str_new sqlite3_api->str_new
120179#define sqlite3_str_finish sqlite3_api->str_finish
120180#define sqlite3_str_appendf sqlite3_api->str_appendf
120181#define sqlite3_str_vappendf sqlite3_api->str_vappendf
120182#define sqlite3_str_append sqlite3_api->str_append
120183#define sqlite3_str_appendall sqlite3_api->str_appendall
120184#define sqlite3_str_appendchar sqlite3_api->str_appendchar
120185#define sqlite3_str_reset sqlite3_api->str_reset
120186#define sqlite3_str_errcode sqlite3_api->str_errcode
120187#define sqlite3_str_length sqlite3_api->str_length
120188#define sqlite3_str_value sqlite3_api->str_value
120189/* Version 3.25.0 and later */
120190#define sqlite3_create_window_function sqlite3_api->create_window_function
120191/* Version 3.26.0 and later */
120192#define sqlite3_normalized_sql sqlite3_api->normalized_sql
120193/* Version 3.28.0 and later */
120194#define sqlite3_stmt_isexplain sqlite3_api->isexplain
120195#define sqlite3_value_frombind sqlite3_api->frombind
120196#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
120197
120198#if !defined(SQLITE_CORE1) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
120199 /* This case when the file really is being compiled as a loadable
120200 ** extension */
120201# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
120202# define SQLITE_EXTENSION_INIT2(v)(void)v; sqlite3_api=v;
120203# define SQLITE_EXTENSION_INIT3 \
120204 extern const sqlite3_api_routines *sqlite3_api;
120205#else
120206 /* This case when the file is being statically linked into the
120207 ** application */
120208# define SQLITE_EXTENSION_INIT1 /*no-op*/
120209# define SQLITE_EXTENSION_INIT2(v)(void)v; (void)v; /* unused parameter */
120210# define SQLITE_EXTENSION_INIT3 /*no-op*/
120211#endif
120212
120213#endif /* SQLITE3EXT_H */
120214
120215/************** End of sqlite3ext.h ******************************************/
120216/************** Continuing where we left off in loadext.c ********************/
120217/* #include "sqliteInt.h" */
120218
120219#ifndef SQLITE_OMIT_LOAD_EXTENSION
120220/*
120221** Some API routines are omitted when various features are
120222** excluded from a build of SQLite. Substitute a NULL pointer
120223** for any missing APIs.
120224*/
120225#ifndef SQLITE_ENABLE_COLUMN_METADATA
120226# define sqlite3_column_database_name0 0
120227# define sqlite3_column_database_name160 0
120228# define sqlite3_column_table_name0 0
120229# define sqlite3_column_table_name160 0
120230# define sqlite3_column_origin_name0 0
120231# define sqlite3_column_origin_name160 0
120232#endif
120233
120234#ifdef SQLITE_OMIT_AUTHORIZATION
120235# define sqlite3_set_authorizer 0
120236#endif
120237
120238#ifdef SQLITE_OMIT_UTF16
120239# define sqlite3_bind_text16 0
120240# define sqlite3_collation_needed16 0
120241# define sqlite3_column_decltype16 0
120242# define sqlite3_column_name16 0
120243# define sqlite3_column_text16 0
120244# define sqlite3_complete16 0
120245# define sqlite3_create_collation16 0
120246# define sqlite3_create_function16 0
120247# define sqlite3_errmsg16 0
120248# define sqlite3_open16 0
120249# define sqlite3_prepare16 0
120250# define sqlite3_prepare16_v2 0
120251# define sqlite3_prepare16_v3 0
120252# define sqlite3_result_error16 0
120253# define sqlite3_result_text16 0
120254# define sqlite3_result_text16be 0
120255# define sqlite3_result_text16le 0
120256# define sqlite3_value_text16 0
120257# define sqlite3_value_text16be 0
120258# define sqlite3_value_text16le 0
120259# define sqlite3_column_database_name160 0
120260# define sqlite3_column_table_name160 0
120261# define sqlite3_column_origin_name160 0
120262#endif
120263
120264#ifdef SQLITE_OMIT_COMPLETE
120265# define sqlite3_complete 0
120266# define sqlite3_complete16 0
120267#endif
120268
120269#ifdef SQLITE_OMIT_DECLTYPE
120270# define sqlite3_column_decltype16 0
120271# define sqlite3_column_decltype 0
120272#endif
120273
120274#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
120275# define sqlite3_progress_handler 0
120276#endif
120277
120278#ifdef SQLITE_OMIT_VIRTUALTABLE
120279# define sqlite3_create_module 0
120280# define sqlite3_create_module_v2 0
120281# define sqlite3_declare_vtab 0
120282# define sqlite3_vtab_config 0
120283# define sqlite3_vtab_on_conflict 0
120284# define sqlite3_vtab_collation 0
120285#endif
120286
120287#ifdef SQLITE_OMIT_SHARED_CACHE
120288# define sqlite3_enable_shared_cache 0
120289#endif
120290
120291#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
120292# define sqlite3_profile 0
120293# define sqlite3_trace 0
120294#endif
120295
120296#ifdef SQLITE_OMIT_GET_TABLE
120297# define sqlite3_free_table 0
120298# define sqlite3_get_table 0
120299#endif
120300
120301#ifdef SQLITE_OMIT_INCRBLOB
120302#define sqlite3_bind_zeroblob 0
120303#define sqlite3_blob_bytes 0
120304#define sqlite3_blob_close 0
120305#define sqlite3_blob_open 0
120306#define sqlite3_blob_read 0
120307#define sqlite3_blob_write 0
120308#define sqlite3_blob_reopen 0
120309#endif
120310
120311#if defined(SQLITE_OMIT_TRACE)
120312# define sqlite3_trace_v2 0
120313#endif
120314
120315/*
120316** The following structure contains pointers to all SQLite API routines.
120317** A pointer to this structure is passed into extensions when they are
120318** loaded so that the extension can make calls back into the SQLite
120319** library.
120320**
120321** When adding new APIs, add them to the bottom of this structure
120322** in order to preserve backwards compatibility.
120323**
120324** Extensions that use newer APIs should first call the
120325** sqlite3_libversion_number() to make sure that the API they
120326** intend to use is supported by the library. Extensions should
120327** also check to make sure that the pointer to the function is
120328** not NULL before calling it.
120329*/
120330static const sqlite3_api_routines sqlite3Apis = {
120331 sqlite3_aggregate_context,
120332#ifndef SQLITE_OMIT_DEPRECATED
120333 sqlite3_aggregate_count,
120334#else
120335 0,
120336#endif
120337 sqlite3_bind_blob,
120338 sqlite3_bind_double,
120339 sqlite3_bind_int,
120340 sqlite3_bind_int64,
120341 sqlite3_bind_null,
120342 sqlite3_bind_parameter_count,
120343 sqlite3_bind_parameter_index,
120344 sqlite3_bind_parameter_name,
120345 sqlite3_bind_text,
120346 sqlite3_bind_text16,
120347 sqlite3_bind_value,
120348 sqlite3_busy_handler,
120349 sqlite3_busy_timeout,
120350 sqlite3_changes,
120351 sqlite3_close,
120352 sqlite3_collation_needed,
120353 sqlite3_collation_needed16,
120354 sqlite3_column_blob,
120355 sqlite3_column_bytes,
120356 sqlite3_column_bytes16,
120357 sqlite3_column_count,
120358 sqlite3_column_database_name0,
120359 sqlite3_column_database_name160,
120360 sqlite3_column_decltype,
120361 sqlite3_column_decltype16,
120362 sqlite3_column_double,
120363 sqlite3_column_int,
120364 sqlite3_column_int64,
120365 sqlite3_column_name,
120366 sqlite3_column_name16,
120367 sqlite3_column_origin_name0,
120368 sqlite3_column_origin_name160,
120369 sqlite3_column_table_name0,
120370 sqlite3_column_table_name160,
120371 sqlite3_column_text,
120372 sqlite3_column_text16,
120373 sqlite3_column_type,
120374 sqlite3_column_value,
120375 sqlite3_commit_hook,
120376 sqlite3_complete,
120377 sqlite3_complete16,
120378 sqlite3_create_collation,
120379 sqlite3_create_collation16,
120380 sqlite3_create_function,
120381 sqlite3_create_function16,
120382 sqlite3_create_module,
120383 sqlite3_data_count,
120384 sqlite3_db_handle,
120385 sqlite3_declare_vtab,
120386 sqlite3_enable_shared_cache,
120387 sqlite3_errcode,
120388 sqlite3_errmsg,
120389 sqlite3_errmsg16,
120390 sqlite3_exec,
120391#ifndef SQLITE_OMIT_DEPRECATED
120392 sqlite3_expired,
120393#else
120394 0,
120395#endif
120396 sqlite3_finalize,
120397 sqlite3_free,
120398 sqlite3_free_table,
120399 sqlite3_get_autocommit,
120400 sqlite3_get_auxdata,
120401 sqlite3_get_table,
120402 0, /* Was sqlite3_global_recover(), but that function is deprecated */
120403 sqlite3_interrupt,
120404 sqlite3_last_insert_rowid,
120405 sqlite3_libversion,
120406 sqlite3_libversion_number,
120407 sqlite3_malloc,
120408 sqlite3_mprintf,
120409 sqlite3_open,
120410 sqlite3_open16,
120411 sqlite3_prepare,
120412 sqlite3_prepare16,
120413 sqlite3_profile,
120414 sqlite3_progress_handler,
120415 sqlite3_realloc,
120416 sqlite3_reset,
120417 sqlite3_result_blob,
120418 sqlite3_result_double,
120419 sqlite3_result_error,
120420 sqlite3_result_error16,
120421 sqlite3_result_int,
120422 sqlite3_result_int64,
120423 sqlite3_result_null,
120424 sqlite3_result_text,
120425 sqlite3_result_text16,
120426 sqlite3_result_text16be,
120427 sqlite3_result_text16le,
120428 sqlite3_result_value,
120429 sqlite3_rollback_hook,
120430 sqlite3_set_authorizer,
120431 sqlite3_set_auxdata,
120432 sqlite3_snprintf,
120433 sqlite3_step,
120434 sqlite3_table_column_metadata,
120435#ifndef SQLITE_OMIT_DEPRECATED
120436 sqlite3_thread_cleanup,
120437#else
120438 0,
120439#endif
120440 sqlite3_total_changes,
120441 sqlite3_trace,
120442#ifndef SQLITE_OMIT_DEPRECATED
120443 sqlite3_transfer_bindings,
120444#else
120445 0,
120446#endif
120447 sqlite3_update_hook,
120448 sqlite3_user_data,
120449 sqlite3_value_blob,
120450 sqlite3_value_bytes,
120451 sqlite3_value_bytes16,
120452 sqlite3_value_double,
120453 sqlite3_value_int,
120454 sqlite3_value_int64,
120455 sqlite3_value_numeric_type,
120456 sqlite3_value_text,
120457 sqlite3_value_text16,
120458 sqlite3_value_text16be,
120459 sqlite3_value_text16le,
120460 sqlite3_value_type,
120461 sqlite3_vmprintf,
120462 /*
120463 ** The original API set ends here. All extensions can call any
120464 ** of the APIs above provided that the pointer is not NULL. But
120465 ** before calling APIs that follow, extension should check the
120466 ** sqlite3_libversion_number() to make sure they are dealing with
120467 ** a library that is new enough to support that API.
120468 *************************************************************************
120469 */
120470 sqlite3_overload_function,
120471
120472 /*
120473 ** Added after 3.3.13
120474 */
120475 sqlite3_prepare_v2,
120476 sqlite3_prepare16_v2,
120477 sqlite3_clear_bindings,
120478
120479 /*
120480 ** Added for 3.4.1
120481 */
120482 sqlite3_create_module_v2,
120483
120484 /*
120485 ** Added for 3.5.0
120486 */
120487 sqlite3_bind_zeroblob,
120488 sqlite3_blob_bytes,
120489 sqlite3_blob_close,
120490 sqlite3_blob_open,
120491 sqlite3_blob_read,
120492 sqlite3_blob_write,
120493 sqlite3_create_collation_v2,
120494 sqlite3_file_control,
120495 sqlite3_memory_highwater,
120496 sqlite3_memory_used,
120497#ifdef SQLITE_MUTEX_OMIT
120498 0,
120499 0,
120500 0,
120501 0,
120502 0,
120503#else
120504 sqlite3_mutex_alloc,
120505 sqlite3_mutex_enter,
120506 sqlite3_mutex_free,
120507 sqlite3_mutex_leave,
120508 sqlite3_mutex_try,
120509#endif
120510 sqlite3_open_v2,
120511 sqlite3_release_memory,
120512 sqlite3_result_error_nomem,
120513 sqlite3_result_error_toobig,
120514 sqlite3_sleep,
120515 sqlite3_soft_heap_limit,
120516 sqlite3_vfs_find,
120517 sqlite3_vfs_register,
120518 sqlite3_vfs_unregister,
120519
120520 /*
120521 ** Added for 3.5.8
120522 */
120523 sqlite3_threadsafe,
120524 sqlite3_result_zeroblob,
120525 sqlite3_result_error_code,
120526 sqlite3_test_control,
120527 sqlite3_randomness,
120528 sqlite3_context_db_handle,
120529
120530 /*
120531 ** Added for 3.6.0
120532 */
120533 sqlite3_extended_result_codes,
120534 sqlite3_limit,
120535 sqlite3_next_stmt,
120536 sqlite3_sql,
120537 sqlite3_status,
120538
120539 /*
120540 ** Added for 3.7.4
120541 */
120542 sqlite3_backup_finish,
120543 sqlite3_backup_init,
120544 sqlite3_backup_pagecount,
120545 sqlite3_backup_remaining,
120546 sqlite3_backup_step,
120547#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
120548 sqlite3_compileoption_get,
120549 sqlite3_compileoption_used,
120550#else
120551 0,
120552 0,
120553#endif
120554 sqlite3_create_function_v2,
120555 sqlite3_db_config,
120556 sqlite3_db_mutex,
120557 sqlite3_db_status,
120558 sqlite3_extended_errcode,
120559 sqlite3_log,
120560 sqlite3_soft_heap_limit64,
120561 sqlite3_sourceid,
120562 sqlite3_stmt_status,
120563 sqlite3_strnicmp,
120564#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
120565 sqlite3_unlock_notify,
120566#else
120567 0,
120568#endif
120569#ifndef SQLITE_OMIT_WAL
120570 sqlite3_wal_autocheckpoint,
120571 sqlite3_wal_checkpoint,
120572 sqlite3_wal_hook,
120573#else
120574 0,
120575 0,
120576 0,
120577#endif
120578 sqlite3_blob_reopen,
120579 sqlite3_vtab_config,
120580 sqlite3_vtab_on_conflict,
120581 sqlite3_close_v2,
120582 sqlite3_db_filename,
120583 sqlite3_db_readonly,
120584 sqlite3_db_release_memory,
120585 sqlite3_errstr,
120586 sqlite3_stmt_busy,
120587 sqlite3_stmt_readonly,
120588 sqlite3_stricmp,
120589 sqlite3_uri_boolean,
120590 sqlite3_uri_int64,
120591 sqlite3_uri_parameter,
120592 sqlite3_vsnprintf,
120593 sqlite3_wal_checkpoint_v2,
120594 /* Version 3.8.7 and later */
120595 sqlite3_auto_extension,
120596 sqlite3_bind_blob64,
120597 sqlite3_bind_text64,
120598 sqlite3_cancel_auto_extension,
120599 sqlite3_load_extension,
120600 sqlite3_malloc64,
120601 sqlite3_msize,
120602 sqlite3_realloc64,
120603 sqlite3_reset_auto_extension,
120604 sqlite3_result_blob64,
120605 sqlite3_result_text64,
120606 sqlite3_strglob,
120607 /* Version 3.8.11 and later */
120608 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
120609 sqlite3_value_free,
120610 sqlite3_result_zeroblob64,
120611 sqlite3_bind_zeroblob64,
120612 /* Version 3.9.0 and later */
120613 sqlite3_value_subtype,
120614 sqlite3_result_subtype,
120615 /* Version 3.10.0 and later */
120616 sqlite3_status64,
120617 sqlite3_strlike,
120618 sqlite3_db_cacheflush,
120619 /* Version 3.12.0 and later */
120620 sqlite3_system_errno,
120621 /* Version 3.14.0 and later */
120622 sqlite3_trace_v2,
120623 sqlite3_expanded_sql,
120624 /* Version 3.18.0 and later */
120625 sqlite3_set_last_insert_rowid,
120626 /* Version 3.20.0 and later */
120627 sqlite3_prepare_v3,
120628 sqlite3_prepare16_v3,
120629 sqlite3_bind_pointer,
120630 sqlite3_result_pointer,
120631 sqlite3_value_pointer,
120632 /* Version 3.22.0 and later */
120633 sqlite3_vtab_nochange,
120634 sqlite3_value_nochange,
120635 sqlite3_vtab_collation,
120636 /* Version 3.24.0 and later */
120637 sqlite3_keyword_count,
120638 sqlite3_keyword_name,
120639 sqlite3_keyword_check,
120640 sqlite3_str_new,
120641 sqlite3_str_finish,
120642 sqlite3_str_appendf,
120643 sqlite3_str_vappendf,
120644 sqlite3_str_append,
120645 sqlite3_str_appendall,
120646 sqlite3_str_appendchar,
120647 sqlite3_str_reset,
120648 sqlite3_str_errcode,
120649 sqlite3_str_length,
120650 sqlite3_str_value,
120651 /* Version 3.25.0 and later */
120652 sqlite3_create_window_function,
120653 /* Version 3.26.0 and later */
120654#ifdef SQLITE_ENABLE_NORMALIZE
120655 sqlite3_normalized_sql,
120656#else
120657 0,
120658#endif
120659 /* Version 3.28.0 and later */
120660 sqlite3_stmt_isexplain,
120661 sqlite3_value_frombind
120662};
120663
120664/*
120665** Attempt to load an SQLite extension library contained in the file
120666** zFile. The entry point is zProc. zProc may be 0 in which case a
120667** default entry point name (sqlite3_extension_init) is used. Use
120668** of the default name is recommended.
120669**
120670** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
120671**
120672** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
120673** error message text. The calling function should free this memory
120674** by calling sqlite3DbFree(db, ).
120675*/
120676static int sqlite3LoadExtension(
120677 sqlite3 *db, /* Load the extension into this database connection */
120678 const char *zFile, /* Name of the shared library containing extension */
120679 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
120680 char **pzErrMsg /* Put error message here if not 0 */
120681){
120682 sqlite3_vfs *pVfs = db->pVfs;
120683 void *handle;
120684 sqlite3_loadext_entry xInit;
120685 char *zErrmsg = 0;
120686 const char *zEntry;
120687 char *zAltEntry = 0;
120688 void **aHandle;
120689 u64 nMsg = 300 + sqlite3Strlen30(zFile);
120690 int ii;
120691 int rc;
120692
120693 /* Shared library endings to try if zFile cannot be loaded as written */
120694 static const char *azEndings[] = {
120695#if SQLITE_OS_WIN0
120696 "dll"
120697#elif defined(__APPLE__)
120698 "dylib"
120699#else
120700 "so"
120701#endif
120702 };
120703
120704
120705 if( pzErrMsg ) *pzErrMsg = 0;
120706
120707 /* Ticket #1863. To avoid a creating security problems for older
120708 ** applications that relink against newer versions of SQLite, the
120709 ** ability to run load_extension is turned off by default. One
120710 ** must call either sqlite3_enable_load_extension(db) or
120711 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
120712 ** to turn on extension loading.
120713 */
120714 if( (db->flags & SQLITE_LoadExtension0x00010000)==0 ){
120715 if( pzErrMsg ){
120716 *pzErrMsg = sqlite3_mprintf("not authorized");
120717 }
120718 return SQLITE_ERROR1;
120719 }
120720
120721 zEntry = zProc ? zProc : "sqlite3_extension_init";
120722
120723 handle = sqlite3OsDlOpen(pVfs, zFile);
120724#if SQLITE_OS_UNIX1 || SQLITE_OS_WIN0
120725 for(ii=0; ii<ArraySize(azEndings)((int)(sizeof(azEndings)/sizeof(azEndings[0]))) && handle==0; ii++){
120726 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
120727 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT7;
120728 handle = sqlite3OsDlOpen(pVfs, zAltFile);
120729 sqlite3_free(zAltFile);
120730 }
120731#endif
120732 if( handle==0 ){
120733 if( pzErrMsg ){
120734 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
120735 if( zErrmsg ){
120736 sqlite3_snprintf(nMsg, zErrmsg,
120737 "unable to open shared library [%s]", zFile);
120738 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
120739 }
120740 }
120741 return SQLITE_ERROR1;
120742 }
120743 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
120744
120745 /* If no entry point was specified and the default legacy
120746 ** entry point name "sqlite3_extension_init" was not found, then
120747 ** construct an entry point name "sqlite3_X_init" where the X is
120748 ** replaced by the lowercase value of every ASCII alphabetic
120749 ** character in the filename after the last "/" upto the first ".",
120750 ** and eliding the first three characters if they are "lib".
120751 ** Examples:
120752 **
120753 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
120754 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
120755 */
120756 if( xInit==0 && zProc==0 ){
120757 int iFile, iEntry, c;
120758 int ncFile = sqlite3Strlen30(zFile);
120759 zAltEntry = sqlite3_malloc64(ncFile+30);
120760 if( zAltEntry==0 ){
120761 sqlite3OsDlClose(pVfs, handle);
120762 return SQLITE_NOMEM_BKPT7;
120763 }
120764 memcpy(zAltEntry, "sqlite3_", 8);
120765 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
120766 iFile++;
120767 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
120768 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
120769 if( sqlite3Isalpha(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x02) ){
120770 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
120771 }
120772 }
120773 memcpy(zAltEntry+iEntry, "_init", 6);
120774 zEntry = zAltEntry;
120775 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
120776 }
120777 if( xInit==0 ){
120778 if( pzErrMsg ){
120779 nMsg += sqlite3Strlen30(zEntry);
120780 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
120781 if( zErrmsg ){
120782 sqlite3_snprintf(nMsg, zErrmsg,
120783 "no entry point [%s] in shared library [%s]", zEntry, zFile);
120784 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
120785 }
120786 }
120787 sqlite3OsDlClose(pVfs, handle);
120788 sqlite3_free(zAltEntry);
120789 return SQLITE_ERROR1;
120790 }
120791 sqlite3_free(zAltEntry);
120792 rc = xInit(db, &zErrmsg, &sqlite3Apis);
120793 if( rc ){
120794 if( rc==SQLITE_OK_LOAD_PERMANENTLY(0 | (1<<8)) ) return SQLITE_OK0;
120795 if( pzErrMsg ){
120796 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
120797 }
120798 sqlite3_free(zErrmsg);
120799 sqlite3OsDlClose(pVfs, handle);
120800 return SQLITE_ERROR1;
120801 }
120802
120803 /* Append the new shared library handle to the db->aExtension array. */
120804 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
120805 if( aHandle==0 ){
120806 return SQLITE_NOMEM_BKPT7;
120807 }
120808 if( db->nExtension>0 ){
120809 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
120810 }
120811 sqlite3DbFree(db, db->aExtension);
120812 db->aExtension = aHandle;
120813
120814 db->aExtension[db->nExtension++] = handle;
120815 return SQLITE_OK0;
120816}
120817SQLITE_API int sqlite3_load_extension(
120818 sqlite3 *db, /* Load the extension into this database connection */
120819 const char *zFile, /* Name of the shared library containing extension */
120820 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
120821 char **pzErrMsg /* Put error message here if not 0 */
120822){
120823 int rc;
120824 sqlite3_mutex_enter(db->mutex);
120825 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
120826 rc = sqlite3ApiExit(db, rc);
120827 sqlite3_mutex_leave(db->mutex);
120828 return rc;
120829}
120830
120831/*
120832** Call this routine when the database connection is closing in order
120833** to clean up loaded extensions
120834*/
120835SQLITE_PRIVATEstatic void sqlite3CloseExtensions(sqlite3 *db){
120836 int i;
120837 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
120838 for(i=0; i<db->nExtension; i++){
120839 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
120840 }
120841 sqlite3DbFree(db, db->aExtension);
120842}
120843
120844/*
120845** Enable or disable extension loading. Extension loading is disabled by
120846** default so as not to open security holes in older applications.
120847*/
120848SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
120849 sqlite3_mutex_enter(db->mutex);
120850 if( onoff ){
120851 db->flags |= SQLITE_LoadExtension0x00010000|SQLITE_LoadExtFunc0x00020000;
120852 }else{
120853 db->flags &= ~(u64)(SQLITE_LoadExtension0x00010000|SQLITE_LoadExtFunc0x00020000);
120854 }
120855 sqlite3_mutex_leave(db->mutex);
120856 return SQLITE_OK0;
120857}
120858
120859#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
120860
120861/*
120862** The following object holds the list of automatically loaded
120863** extensions.
120864**
120865** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
120866** mutex must be held while accessing this list.
120867*/
120868typedef struct sqlite3AutoExtList sqlite3AutoExtList;
120869static SQLITE_WSD struct sqlite3AutoExtList {
120870 u32 nExt; /* Number of entries in aExt[] */
120871 void (**aExt)(void); /* Pointers to the extension init functions */
120872} sqlite3Autoext = { 0, 0 };
120873
120874/* The "wsdAutoext" macro will resolve to the autoextension
120875** state vector. If writable static data is unsupported on the target,
120876** we have to locate the state vector at run-time. In the more common
120877** case where writable static data is supported, wsdStat can refer directly
120878** to the "sqlite3Autoext" state vector declared above.
120879*/
120880#ifdef SQLITE_OMIT_WSD
120881# define wsdAutoextInit \
120882 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)sqlite3Autoext
120883# define wsdAutoextsqlite3Autoext x[0]
120884#else
120885# define wsdAutoextInit
120886# define wsdAutoextsqlite3Autoext sqlite3Autoext
120887#endif
120888
120889
120890/*
120891** Register a statically linked extension that is automatically
120892** loaded by every new database connection.
120893*/
120894SQLITE_API int sqlite3_auto_extension(
120895 void (*xInit)(void)
120896){
120897 int rc = SQLITE_OK0;
120898#ifndef SQLITE_OMIT_AUTOINIT
120899 rc = sqlite3_initialize();
120900 if( rc ){
120901 return rc;
120902 }else
120903#endif
120904 {
120905 u32 i;
120906#if SQLITE_THREADSAFE1
120907 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2);
120908#endif
120909 wsdAutoextInit;
120910 sqlite3_mutex_enter(mutex);
120911 for(i=0; i<wsdAutoextsqlite3Autoext.nExt; i++){
120912 if( wsdAutoextsqlite3Autoext.aExt[i]==xInit ) break;
120913 }
120914 if( i==wsdAutoextsqlite3Autoext.nExt ){
120915 u64 nByte = (wsdAutoextsqlite3Autoext.nExt+1)*sizeof(wsdAutoextsqlite3Autoext.aExt[0]);
120916 void (**aNew)(void);
120917 aNew = sqlite3_realloc64(wsdAutoextsqlite3Autoext.aExt, nByte);
120918 if( aNew==0 ){
120919 rc = SQLITE_NOMEM_BKPT7;
120920 }else{
120921 wsdAutoextsqlite3Autoext.aExt = aNew;
120922 wsdAutoextsqlite3Autoext.aExt[wsdAutoextsqlite3Autoext.nExt] = xInit;
120923 wsdAutoextsqlite3Autoext.nExt++;
120924 }
120925 }
120926 sqlite3_mutex_leave(mutex);
120927 assert( (rc&0xff)==rc )((void) (0));
120928 return rc;
120929 }
120930}
120931
120932/*
120933** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
120934** set of routines that is invoked for each new database connection, if it
120935** is currently on the list. If xInit is not on the list, then this
120936** routine is a no-op.
120937**
120938** Return 1 if xInit was found on the list and removed. Return 0 if xInit
120939** was not on the list.
120940*/
120941SQLITE_API int sqlite3_cancel_auto_extension(
120942 void (*xInit)(void)
120943){
120944#if SQLITE_THREADSAFE1
120945 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2);
120946#endif
120947 int i;
120948 int n = 0;
120949 wsdAutoextInit;
120950 sqlite3_mutex_enter(mutex);
120951 for(i=(int)wsdAutoextsqlite3Autoext.nExt-1; i>=0; i--){
120952 if( wsdAutoextsqlite3Autoext.aExt[i]==xInit ){
120953 wsdAutoextsqlite3Autoext.nExt--;
120954 wsdAutoextsqlite3Autoext.aExt[i] = wsdAutoextsqlite3Autoext.aExt[wsdAutoextsqlite3Autoext.nExt];
120955 n++;
120956 break;
120957 }
120958 }
120959 sqlite3_mutex_leave(mutex);
120960 return n;
120961}
120962
120963/*
120964** Reset the automatic extension loading mechanism.
120965*/
120966SQLITE_API void sqlite3_reset_auto_extension(void){
120967#ifndef SQLITE_OMIT_AUTOINIT
120968 if( sqlite3_initialize()==SQLITE_OK0 )
120969#endif
120970 {
120971#if SQLITE_THREADSAFE1
120972 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2);
120973#endif
120974 wsdAutoextInit;
120975 sqlite3_mutex_enter(mutex);
120976 sqlite3_free(wsdAutoextsqlite3Autoext.aExt);
120977 wsdAutoextsqlite3Autoext.aExt = 0;
120978 wsdAutoextsqlite3Autoext.nExt = 0;
120979 sqlite3_mutex_leave(mutex);
120980 }
120981}
120982
120983/*
120984** Load all automatic extensions.
120985**
120986** If anything goes wrong, set an error in the database connection.
120987*/
120988SQLITE_PRIVATEstatic void sqlite3AutoLoadExtensions(sqlite3 *db){
120989 u32 i;
120990 int go = 1;
120991 int rc;
120992 sqlite3_loadext_entry xInit;
120993
120994 wsdAutoextInit;
120995 if( wsdAutoextsqlite3Autoext.nExt==0 ){
120996 /* Common case: early out without every having to acquire a mutex */
120997 return;
120998 }
120999 for(i=0; go; i++){
121000 char *zErrmsg;
121001#if SQLITE_THREADSAFE1
121002 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2);
121003#endif
121004#ifdef SQLITE_OMIT_LOAD_EXTENSION
121005 const sqlite3_api_routines *pThunk = 0;
121006#else
121007 const sqlite3_api_routines *pThunk = &sqlite3Apis;
121008#endif
121009 sqlite3_mutex_enter(mutex);
121010 if( i>=wsdAutoextsqlite3Autoext.nExt ){
121011 xInit = 0;
121012 go = 0;
121013 }else{
121014 xInit = (sqlite3_loadext_entry)wsdAutoextsqlite3Autoext.aExt[i];
121015 }
121016 sqlite3_mutex_leave(mutex);
121017 zErrmsg = 0;
121018 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
121019 sqlite3ErrorWithMsg(db, rc,
121020 "automatic extension loading failed: %s", zErrmsg);
121021 go = 0;
121022 }
121023 sqlite3_free(zErrmsg);
121024 }
121025}
121026
121027/************** End of loadext.c *********************************************/
121028/************** Begin file pragma.c ******************************************/
121029/*
121030** 2003 April 6
121031**
121032** The author disclaims copyright to this source code. In place of
121033** a legal notice, here is a blessing:
121034**
121035** May you do good and not evil.
121036** May you find forgiveness for yourself and forgive others.
121037** May you share freely, never taking more than you give.
121038**
121039*************************************************************************
121040** This file contains code used to implement the PRAGMA command.
121041*/
121042/* #include "sqliteInt.h" */
121043
121044#if !defined(SQLITE_ENABLE_LOCKING_STYLE0)
121045# if defined(__APPLE__)
121046# define SQLITE_ENABLE_LOCKING_STYLE0 1
121047# else
121048# define SQLITE_ENABLE_LOCKING_STYLE0 0
121049# endif
121050#endif
121051
121052/***************************************************************************
121053** The "pragma.h" include file is an automatically generated file that
121054** that includes the PragType_XXXX macro definitions and the aPragmaName[]
121055** object. This ensures that the aPragmaName[] table is arranged in
121056** lexicographical order to facility a binary search of the pragma name.
121057** Do not edit pragma.h directly. Edit and rerun the script in at
121058** ../tool/mkpragmatab.tcl. */
121059/************** Include pragma.h in the middle of pragma.c *******************/
121060/************** Begin file pragma.h ******************************************/
121061/* DO NOT EDIT!
121062** This file is automatically generated by the script at
121063** ../tool/mkpragmatab.tcl. To update the set of pragmas, edit
121064** that script and rerun it.
121065*/
121066
121067/* The various pragma types */
121068#define PragTyp_HEADER_VALUE0 0
121069#define PragTyp_AUTO_VACUUM1 1
121070#define PragTyp_FLAG2 2
121071#define PragTyp_BUSY_TIMEOUT3 3
121072#define PragTyp_CACHE_SIZE4 4
121073#define PragTyp_CACHE_SPILL5 5
121074#define PragTyp_CASE_SENSITIVE_LIKE6 6
121075#define PragTyp_COLLATION_LIST7 7
121076#define PragTyp_COMPILE_OPTIONS8 8
121077#define PragTyp_DATA_STORE_DIRECTORY9 9
121078#define PragTyp_DATABASE_LIST10 10
121079#define PragTyp_DEFAULT_CACHE_SIZE11 11
121080#define PragTyp_ENCODING12 12
121081#define PragTyp_FOREIGN_KEY_CHECK13 13
121082#define PragTyp_FOREIGN_KEY_LIST14 14
121083#define PragTyp_FUNCTION_LIST15 15
121084#define PragTyp_INCREMENTAL_VACUUM16 16
121085#define PragTyp_INDEX_INFO17 17
121086#define PragTyp_INDEX_LIST18 18
121087#define PragTyp_INTEGRITY_CHECK19 19
121088#define PragTyp_JOURNAL_MODE20 20
121089#define PragTyp_JOURNAL_SIZE_LIMIT21 21
121090#define PragTyp_LOCK_PROXY_FILE22 22
121091#define PragTyp_LOCKING_MODE23 23
121092#define PragTyp_PAGE_COUNT24 24
121093#define PragTyp_MMAP_SIZE25 25
121094#define PragTyp_MODULE_LIST26 26
121095#define PragTyp_OPTIMIZE27 27
121096#define PragTyp_PAGE_SIZE28 28
121097#define PragTyp_PRAGMA_LIST29 29
121098#define PragTyp_SECURE_DELETE30 30
121099#define PragTyp_SHRINK_MEMORY31 31
121100#define PragTyp_SOFT_HEAP_LIMIT32 32
121101#define PragTyp_SYNCHRONOUS33 33
121102#define PragTyp_TABLE_INFO34 34
121103#define PragTyp_TEMP_STORE35 35
121104#define PragTyp_TEMP_STORE_DIRECTORY36 36
121105#define PragTyp_THREADS37 37
121106#define PragTyp_WAL_AUTOCHECKPOINT38 38
121107#define PragTyp_WAL_CHECKPOINT39 39
121108#define PragTyp_ACTIVATE_EXTENSIONS40 40
121109#define PragTyp_KEY41 41
121110#define PragTyp_LOCK_STATUS42 42
121111#define PragTyp_STATS43 43
121112
121113/* Property flags associated with various pragma. */
121114#define PragFlg_NeedSchema0x01 0x01 /* Force schema load before running */
121115#define PragFlg_NoColumns0x02 0x02 /* OP_ResultRow called with zero columns */
121116#define PragFlg_NoColumns10x04 0x04 /* zero columns if RHS argument is present */
121117#define PragFlg_ReadOnly0x08 0x08 /* Read-only HEADER_VALUE */
121118#define PragFlg_Result00x10 0x10 /* Acts as query when no argument */
121119#define PragFlg_Result10x20 0x20 /* Acts as query when has one argument */
121120#define PragFlg_SchemaOpt0x40 0x40 /* Schema restricts name search if present */
121121#define PragFlg_SchemaReq0x80 0x80 /* Schema required - "main" is default */
121122
121123/* Names of columns for pragmas that return multi-column result
121124** or that return single-column results where the name of the
121125** result column is different from the name of the pragma
121126*/
121127static const char *const pragCName[] = {
121128 /* 0 */ "id", /* Used by: foreign_key_list */
121129 /* 1 */ "seq",
121130 /* 2 */ "table",
121131 /* 3 */ "from",
121132 /* 4 */ "to",
121133 /* 5 */ "on_update",
121134 /* 6 */ "on_delete",
121135 /* 7 */ "match",
121136 /* 8 */ "cid", /* Used by: table_xinfo */
121137 /* 9 */ "name",
121138 /* 10 */ "type",
121139 /* 11 */ "notnull",
121140 /* 12 */ "dflt_value",
121141 /* 13 */ "pk",
121142 /* 14 */ "hidden",
121143 /* table_info reuses 8 */
121144 /* 15 */ "seqno", /* Used by: index_xinfo */
121145 /* 16 */ "cid",
121146 /* 17 */ "name",
121147 /* 18 */ "desc",
121148 /* 19 */ "coll",
121149 /* 20 */ "key",
121150 /* 21 */ "tbl", /* Used by: stats */
121151 /* 22 */ "idx",
121152 /* 23 */ "wdth",
121153 /* 24 */ "hght",
121154 /* 25 */ "flgs",
121155 /* 26 */ "seq", /* Used by: index_list */
121156 /* 27 */ "name",
121157 /* 28 */ "unique",
121158 /* 29 */ "origin",
121159 /* 30 */ "partial",
121160 /* 31 */ "table", /* Used by: foreign_key_check */
121161 /* 32 */ "rowid",
121162 /* 33 */ "parent",
121163 /* 34 */ "fkid",
121164 /* index_info reuses 15 */
121165 /* 35 */ "seq", /* Used by: database_list */
121166 /* 36 */ "name",
121167 /* 37 */ "file",
121168 /* 38 */ "busy", /* Used by: wal_checkpoint */
121169 /* 39 */ "log",
121170 /* 40 */ "checkpointed",
121171 /* 41 */ "name", /* Used by: function_list */
121172 /* 42 */ "builtin",
121173 /* collation_list reuses 26 */
121174 /* 43 */ "database", /* Used by: lock_status */
121175 /* 44 */ "status",
121176 /* 45 */ "cache_size", /* Used by: default_cache_size */
121177 /* module_list pragma_list reuses 9 */
121178 /* 46 */ "timeout", /* Used by: busy_timeout */
121179};
121180
121181/* Definitions of all built-in pragmas */
121182typedef struct PragmaName {
121183 const char *const zName; /* Name of pragma */
121184 u8 ePragTyp; /* PragTyp_XXX value */
121185 u8 mPragFlg; /* Zero or more PragFlg_XXX values */
121186 u8 iPragCName; /* Start of column names in pragCName[] */
121187 u8 nPragCName; /* Num of col names. 0 means use pragma name */
121188 u64 iArg; /* Extra argument */
121189} PragmaName;
121190static const PragmaName aPragmaName[] = {
121191#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
121192 {/* zName: */ "activate_extensions",
121193 /* ePragTyp: */ PragTyp_ACTIVATE_EXTENSIONS40,
121194 /* ePragFlg: */ 0,
121195 /* ColNames: */ 0, 0,
121196 /* iArg: */ 0 },
121197#endif
121198#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
121199 {/* zName: */ "application_id",
121200 /* ePragTyp: */ PragTyp_HEADER_VALUE0,
121201 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
121202 /* ColNames: */ 0, 0,
121203 /* iArg: */ BTREE_APPLICATION_ID8 },
121204#endif
121205#if !defined(SQLITE_OMIT_AUTOVACUUM)
121206 {/* zName: */ "auto_vacuum",
121207 /* ePragTyp: */ PragTyp_AUTO_VACUUM1,
121208 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
121209 /* ColNames: */ 0, 0,
121210 /* iArg: */ 0 },
121211#endif
121212#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121213#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
121214 {/* zName: */ "automatic_index",
121215 /* ePragTyp: */ PragTyp_FLAG2,
121216 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121217 /* ColNames: */ 0, 0,
121218 /* iArg: */ SQLITE_AutoIndex0x00008000 },
121219#endif
121220#endif
121221 {/* zName: */ "busy_timeout",
121222 /* ePragTyp: */ PragTyp_BUSY_TIMEOUT3,
121223 /* ePragFlg: */ PragFlg_Result00x10,
121224 /* ColNames: */ 46, 1,
121225 /* iArg: */ 0 },
121226#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
121227 {/* zName: */ "cache_size",
121228 /* ePragTyp: */ PragTyp_CACHE_SIZE4,
121229 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
121230 /* ColNames: */ 0, 0,
121231 /* iArg: */ 0 },
121232#endif
121233#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121234 {/* zName: */ "cache_spill",
121235 /* ePragTyp: */ PragTyp_CACHE_SPILL5,
121236 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
121237 /* ColNames: */ 0, 0,
121238 /* iArg: */ 0 },
121239#endif
121240#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
121241 {/* zName: */ "case_sensitive_like",
121242 /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE6,
121243 /* ePragFlg: */ PragFlg_NoColumns0x02,
121244 /* ColNames: */ 0, 0,
121245 /* iArg: */ 0 },
121246#endif
121247 {/* zName: */ "cell_size_check",
121248 /* ePragTyp: */ PragTyp_FLAG2,
121249 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121250 /* ColNames: */ 0, 0,
121251 /* iArg: */ SQLITE_CellSizeCk0x00200000 },
121252#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121253 {/* zName: */ "checkpoint_fullfsync",
121254 /* ePragTyp: */ PragTyp_FLAG2,
121255 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121256 /* ColNames: */ 0, 0,
121257 /* iArg: */ SQLITE_CkptFullFSync0x00000010 },
121258#endif
121259#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
121260 {/* zName: */ "collation_list",
121261 /* ePragTyp: */ PragTyp_COLLATION_LIST7,
121262 /* ePragFlg: */ PragFlg_Result00x10,
121263 /* ColNames: */ 26, 2,
121264 /* iArg: */ 0 },
121265#endif
121266#if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
121267 {/* zName: */ "compile_options",
121268 /* ePragTyp: */ PragTyp_COMPILE_OPTIONS8,
121269 /* ePragFlg: */ PragFlg_Result00x10,
121270 /* ColNames: */ 0, 0,
121271 /* iArg: */ 0 },
121272#endif
121273#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121274 {/* zName: */ "count_changes",
121275 /* ePragTyp: */ PragTyp_FLAG2,
121276 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121277 /* ColNames: */ 0, 0,
121278 /* iArg: */ SQLITE_CountRows0x00000080 },
121279#endif
121280#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN0
121281 {/* zName: */ "data_store_directory",
121282 /* ePragTyp: */ PragTyp_DATA_STORE_DIRECTORY9,
121283 /* ePragFlg: */ PragFlg_NoColumns10x04,
121284 /* ColNames: */ 0, 0,
121285 /* iArg: */ 0 },
121286#endif
121287#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
121288 {/* zName: */ "data_version",
121289 /* ePragTyp: */ PragTyp_HEADER_VALUE0,
121290 /* ePragFlg: */ PragFlg_ReadOnly0x08|PragFlg_Result00x10,
121291 /* ColNames: */ 0, 0,
121292 /* iArg: */ BTREE_DATA_VERSION15 },
121293#endif
121294#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
121295 {/* zName: */ "database_list",
121296 /* ePragTyp: */ PragTyp_DATABASE_LIST10,
121297 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10,
121298 /* ColNames: */ 35, 3,
121299 /* iArg: */ 0 },
121300#endif
121301#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
121302 {/* zName: */ "default_cache_size",
121303 /* ePragTyp: */ PragTyp_DEFAULT_CACHE_SIZE11,
121304 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
121305 /* ColNames: */ 45, 1,
121306 /* iArg: */ 0 },
121307#endif
121308#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121309#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
121310 {/* zName: */ "defer_foreign_keys",
121311 /* ePragTyp: */ PragTyp_FLAG2,
121312 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121313 /* ColNames: */ 0, 0,
121314 /* iArg: */ SQLITE_DeferFKs0x00080000 },
121315#endif
121316#endif
121317#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121318 {/* zName: */ "empty_result_callbacks",
121319 /* ePragTyp: */ PragTyp_FLAG2,
121320 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121321 /* ColNames: */ 0, 0,
121322 /* iArg: */ SQLITE_NullCallback0x00000100 },
121323#endif
121324#if !defined(SQLITE_OMIT_UTF16)
121325 {/* zName: */ "encoding",
121326 /* ePragTyp: */ PragTyp_ENCODING12,
121327 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121328 /* ColNames: */ 0, 0,
121329 /* iArg: */ 0 },
121330#endif
121331#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
121332 {/* zName: */ "foreign_key_check",
121333 /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK13,
121334 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10,
121335 /* ColNames: */ 31, 4,
121336 /* iArg: */ 0 },
121337#endif
121338#if !defined(SQLITE_OMIT_FOREIGN_KEY)
121339 {/* zName: */ "foreign_key_list",
121340 /* ePragTyp: */ PragTyp_FOREIGN_KEY_LIST14,
121341 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
121342 /* ColNames: */ 0, 8,
121343 /* iArg: */ 0 },
121344#endif
121345#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121346#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
121347 {/* zName: */ "foreign_keys",
121348 /* ePragTyp: */ PragTyp_FLAG2,
121349 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121350 /* ColNames: */ 0, 0,
121351 /* iArg: */ SQLITE_ForeignKeys0x00004000 },
121352#endif
121353#endif
121354#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
121355 {/* zName: */ "freelist_count",
121356 /* ePragTyp: */ PragTyp_HEADER_VALUE0,
121357 /* ePragFlg: */ PragFlg_ReadOnly0x08|PragFlg_Result00x10,
121358 /* ColNames: */ 0, 0,
121359 /* iArg: */ BTREE_FREE_PAGE_COUNT0 },
121360#endif
121361#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121362 {/* zName: */ "full_column_names",
121363 /* ePragTyp: */ PragTyp_FLAG2,
121364 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121365 /* ColNames: */ 0, 0,
121366 /* iArg: */ SQLITE_FullColNames0x00000004 },
121367 {/* zName: */ "fullfsync",
121368 /* ePragTyp: */ PragTyp_FLAG2,
121369 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121370 /* ColNames: */ 0, 0,
121371 /* iArg: */ SQLITE_FullFSync0x00000008 },
121372#endif
121373#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
121374#if defined(SQLITE_INTROSPECTION_PRAGMAS)
121375 {/* zName: */ "function_list",
121376 /* ePragTyp: */ PragTyp_FUNCTION_LIST15,
121377 /* ePragFlg: */ PragFlg_Result00x10,
121378 /* ColNames: */ 41, 2,
121379 /* iArg: */ 0 },
121380#endif
121381#endif
121382#if defined(SQLITE_HAS_CODEC)
121383 {/* zName: */ "hexkey",
121384 /* ePragTyp: */ PragTyp_KEY41,
121385 /* ePragFlg: */ 0,
121386 /* ColNames: */ 0, 0,
121387 /* iArg: */ 2 },
121388 {/* zName: */ "hexrekey",
121389 /* ePragTyp: */ PragTyp_KEY41,
121390 /* ePragFlg: */ 0,
121391 /* ColNames: */ 0, 0,
121392 /* iArg: */ 3 },
121393#endif
121394#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121395#if !defined(SQLITE_OMIT_CHECK)
121396 {/* zName: */ "ignore_check_constraints",
121397 /* ePragTyp: */ PragTyp_FLAG2,
121398 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121399 /* ColNames: */ 0, 0,
121400 /* iArg: */ SQLITE_IgnoreChecks0x00000200 },
121401#endif
121402#endif
121403#if !defined(SQLITE_OMIT_AUTOVACUUM)
121404 {/* zName: */ "incremental_vacuum",
121405 /* ePragTyp: */ PragTyp_INCREMENTAL_VACUUM16,
121406 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_NoColumns0x02,
121407 /* ColNames: */ 0, 0,
121408 /* iArg: */ 0 },
121409#endif
121410#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
121411 {/* zName: */ "index_info",
121412 /* ePragTyp: */ PragTyp_INDEX_INFO17,
121413 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
121414 /* ColNames: */ 15, 3,
121415 /* iArg: */ 0 },
121416 {/* zName: */ "index_list",
121417 /* ePragTyp: */ PragTyp_INDEX_LIST18,
121418 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
121419 /* ColNames: */ 26, 5,
121420 /* iArg: */ 0 },
121421 {/* zName: */ "index_xinfo",
121422 /* ePragTyp: */ PragTyp_INDEX_INFO17,
121423 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
121424 /* ColNames: */ 15, 6,
121425 /* iArg: */ 1 },
121426#endif
121427#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
121428 {/* zName: */ "integrity_check",
121429 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK19,
121430 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20,
121431 /* ColNames: */ 0, 0,
121432 /* iArg: */ 0 },
121433#endif
121434#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
121435 {/* zName: */ "journal_mode",
121436 /* ePragTyp: */ PragTyp_JOURNAL_MODE20,
121437 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
121438 /* ColNames: */ 0, 0,
121439 /* iArg: */ 0 },
121440 {/* zName: */ "journal_size_limit",
121441 /* ePragTyp: */ PragTyp_JOURNAL_SIZE_LIMIT21,
121442 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80,
121443 /* ColNames: */ 0, 0,
121444 /* iArg: */ 0 },
121445#endif
121446#if defined(SQLITE_HAS_CODEC)
121447 {/* zName: */ "key",
121448 /* ePragTyp: */ PragTyp_KEY41,
121449 /* ePragFlg: */ 0,
121450 /* ColNames: */ 0, 0,
121451 /* iArg: */ 0 },
121452#endif
121453#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121454 {/* zName: */ "legacy_alter_table",
121455 /* ePragTyp: */ PragTyp_FLAG2,
121456 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121457 /* ColNames: */ 0, 0,
121458 /* iArg: */ SQLITE_LegacyAlter0x04000000 },
121459 {/* zName: */ "legacy_file_format",
121460 /* ePragTyp: */ PragTyp_FLAG2,
121461 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121462 /* ColNames: */ 0, 0,
121463 /* iArg: */ SQLITE_LegacyFileFmt0x00000002 },
121464#endif
121465#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE0
121466 {/* zName: */ "lock_proxy_file",
121467 /* ePragTyp: */ PragTyp_LOCK_PROXY_FILE22,
121468 /* ePragFlg: */ PragFlg_NoColumns10x04,
121469 /* ColNames: */ 0, 0,
121470 /* iArg: */ 0 },
121471#endif
121472#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
121473 {/* zName: */ "lock_status",
121474 /* ePragTyp: */ PragTyp_LOCK_STATUS42,
121475 /* ePragFlg: */ PragFlg_Result00x10,
121476 /* ColNames: */ 43, 2,
121477 /* iArg: */ 0 },
121478#endif
121479#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
121480 {/* zName: */ "locking_mode",
121481 /* ePragTyp: */ PragTyp_LOCKING_MODE23,
121482 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80,
121483 /* ColNames: */ 0, 0,
121484 /* iArg: */ 0 },
121485 {/* zName: */ "max_page_count",
121486 /* ePragTyp: */ PragTyp_PAGE_COUNT24,
121487 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
121488 /* ColNames: */ 0, 0,
121489 /* iArg: */ 0 },
121490 {/* zName: */ "mmap_size",
121491 /* ePragTyp: */ PragTyp_MMAP_SIZE25,
121492 /* ePragFlg: */ 0,
121493 /* ColNames: */ 0, 0,
121494 /* iArg: */ 0 },
121495#endif
121496#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
121497#if !defined(SQLITE_OMIT_VIRTUALTABLE)
121498#if defined(SQLITE_INTROSPECTION_PRAGMAS)
121499 {/* zName: */ "module_list",
121500 /* ePragTyp: */ PragTyp_MODULE_LIST26,
121501 /* ePragFlg: */ PragFlg_Result00x10,
121502 /* ColNames: */ 9, 1,
121503 /* iArg: */ 0 },
121504#endif
121505#endif
121506#endif
121507 {/* zName: */ "optimize",
121508 /* ePragTyp: */ PragTyp_OPTIMIZE27,
121509 /* ePragFlg: */ PragFlg_Result10x20|PragFlg_NeedSchema0x01,
121510 /* ColNames: */ 0, 0,
121511 /* iArg: */ 0 },
121512#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
121513 {/* zName: */ "page_count",
121514 /* ePragTyp: */ PragTyp_PAGE_COUNT24,
121515 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
121516 /* ColNames: */ 0, 0,
121517 /* iArg: */ 0 },
121518 {/* zName: */ "page_size",
121519 /* ePragTyp: */ PragTyp_PAGE_SIZE28,
121520 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
121521 /* ColNames: */ 0, 0,
121522 /* iArg: */ 0 },
121523#endif
121524#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121525#if defined(SQLITE_DEBUG)
121526 {/* zName: */ "parser_trace",
121527 /* ePragTyp: */ PragTyp_FLAG2,
121528 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121529 /* ColNames: */ 0, 0,
121530 /* iArg: */ SQLITE_ParserTrace },
121531#endif
121532#endif
121533#if defined(SQLITE_INTROSPECTION_PRAGMAS)
121534 {/* zName: */ "pragma_list",
121535 /* ePragTyp: */ PragTyp_PRAGMA_LIST29,
121536 /* ePragFlg: */ PragFlg_Result00x10,
121537 /* ColNames: */ 9, 1,
121538 /* iArg: */ 0 },
121539#endif
121540#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121541 {/* zName: */ "query_only",
121542 /* ePragTyp: */ PragTyp_FLAG2,
121543 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121544 /* ColNames: */ 0, 0,
121545 /* iArg: */ SQLITE_QueryOnly0x00100000 },
121546#endif
121547#if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
121548 {/* zName: */ "quick_check",
121549 /* ePragTyp: */ PragTyp_INTEGRITY_CHECK19,
121550 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_Result10x20,
121551 /* ColNames: */ 0, 0,
121552 /* iArg: */ 0 },
121553#endif
121554#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121555 {/* zName: */ "read_uncommitted",
121556 /* ePragTyp: */ PragTyp_FLAG2,
121557 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121558 /* ColNames: */ 0, 0,
121559 /* iArg: */ SQLITE_ReadUncommit0x00000400 },
121560 {/* zName: */ "recursive_triggers",
121561 /* ePragTyp: */ PragTyp_FLAG2,
121562 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121563 /* ColNames: */ 0, 0,
121564 /* iArg: */ SQLITE_RecTriggers0x00002000 },
121565#endif
121566#if defined(SQLITE_HAS_CODEC)
121567 {/* zName: */ "rekey",
121568 /* ePragTyp: */ PragTyp_KEY41,
121569 /* ePragFlg: */ 0,
121570 /* ColNames: */ 0, 0,
121571 /* iArg: */ 1 },
121572#endif
121573#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121574 {/* zName: */ "reverse_unordered_selects",
121575 /* ePragTyp: */ PragTyp_FLAG2,
121576 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121577 /* ColNames: */ 0, 0,
121578 /* iArg: */ SQLITE_ReverseOrder0x00001000 },
121579#endif
121580#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
121581 {/* zName: */ "schema_version",
121582 /* ePragTyp: */ PragTyp_HEADER_VALUE0,
121583 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
121584 /* ColNames: */ 0, 0,
121585 /* iArg: */ BTREE_SCHEMA_VERSION1 },
121586#endif
121587#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
121588 {/* zName: */ "secure_delete",
121589 /* ePragTyp: */ PragTyp_SECURE_DELETE30,
121590 /* ePragFlg: */ PragFlg_Result00x10,
121591 /* ColNames: */ 0, 0,
121592 /* iArg: */ 0 },
121593#endif
121594#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121595 {/* zName: */ "short_column_names",
121596 /* ePragTyp: */ PragTyp_FLAG2,
121597 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121598 /* ColNames: */ 0, 0,
121599 /* iArg: */ SQLITE_ShortColNames0x00000040 },
121600#endif
121601 {/* zName: */ "shrink_memory",
121602 /* ePragTyp: */ PragTyp_SHRINK_MEMORY31,
121603 /* ePragFlg: */ PragFlg_NoColumns0x02,
121604 /* ColNames: */ 0, 0,
121605 /* iArg: */ 0 },
121606 {/* zName: */ "soft_heap_limit",
121607 /* ePragTyp: */ PragTyp_SOFT_HEAP_LIMIT32,
121608 /* ePragFlg: */ PragFlg_Result00x10,
121609 /* ColNames: */ 0, 0,
121610 /* iArg: */ 0 },
121611#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121612#if defined(SQLITE_DEBUG)
121613 {/* zName: */ "sql_trace",
121614 /* ePragTyp: */ PragTyp_FLAG2,
121615 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121616 /* ColNames: */ 0, 0,
121617 /* iArg: */ SQLITE_SqlTrace },
121618#endif
121619#endif
121620#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && defined(SQLITE_DEBUG)
121621 {/* zName: */ "stats",
121622 /* ePragTyp: */ PragTyp_STATS43,
121623 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80,
121624 /* ColNames: */ 21, 5,
121625 /* iArg: */ 0 },
121626#endif
121627#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
121628 {/* zName: */ "synchronous",
121629 /* ePragTyp: */ PragTyp_SYNCHRONOUS33,
121630 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result00x10|PragFlg_SchemaReq0x80|PragFlg_NoColumns10x04,
121631 /* ColNames: */ 0, 0,
121632 /* iArg: */ 0 },
121633#endif
121634#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
121635 {/* zName: */ "table_info",
121636 /* ePragTyp: */ PragTyp_TABLE_INFO34,
121637 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
121638 /* ColNames: */ 8, 6,
121639 /* iArg: */ 0 },
121640 {/* zName: */ "table_xinfo",
121641 /* ePragTyp: */ PragTyp_TABLE_INFO34,
121642 /* ePragFlg: */ PragFlg_NeedSchema0x01|PragFlg_Result10x20|PragFlg_SchemaOpt0x40,
121643 /* ColNames: */ 8, 7,
121644 /* iArg: */ 1 },
121645#endif
121646#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
121647 {/* zName: */ "temp_store",
121648 /* ePragTyp: */ PragTyp_TEMP_STORE35,
121649 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121650 /* ColNames: */ 0, 0,
121651 /* iArg: */ 0 },
121652 {/* zName: */ "temp_store_directory",
121653 /* ePragTyp: */ PragTyp_TEMP_STORE_DIRECTORY36,
121654 /* ePragFlg: */ PragFlg_NoColumns10x04,
121655 /* ColNames: */ 0, 0,
121656 /* iArg: */ 0 },
121657#endif
121658#if defined(SQLITE_HAS_CODEC)
121659 {/* zName: */ "textkey",
121660 /* ePragTyp: */ PragTyp_KEY41,
121661 /* ePragFlg: */ 0,
121662 /* ColNames: */ 0, 0,
121663 /* iArg: */ 4 },
121664 {/* zName: */ "textrekey",
121665 /* ePragTyp: */ PragTyp_KEY41,
121666 /* ePragFlg: */ 0,
121667 /* ColNames: */ 0, 0,
121668 /* iArg: */ 5 },
121669#endif
121670 {/* zName: */ "threads",
121671 /* ePragTyp: */ PragTyp_THREADS37,
121672 /* ePragFlg: */ PragFlg_Result00x10,
121673 /* ColNames: */ 0, 0,
121674 /* iArg: */ 0 },
121675#if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
121676 {/* zName: */ "user_version",
121677 /* ePragTyp: */ PragTyp_HEADER_VALUE0,
121678 /* ePragFlg: */ PragFlg_NoColumns10x04|PragFlg_Result00x10,
121679 /* ColNames: */ 0, 0,
121680 /* iArg: */ BTREE_USER_VERSION6 },
121681#endif
121682#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121683#if defined(SQLITE_DEBUG)
121684 {/* zName: */ "vdbe_addoptrace",
121685 /* ePragTyp: */ PragTyp_FLAG2,
121686 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121687 /* ColNames: */ 0, 0,
121688 /* iArg: */ SQLITE_VdbeAddopTrace },
121689 {/* zName: */ "vdbe_debug",
121690 /* ePragTyp: */ PragTyp_FLAG2,
121691 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121692 /* ColNames: */ 0, 0,
121693 /* iArg: */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
121694 {/* zName: */ "vdbe_eqp",
121695 /* ePragTyp: */ PragTyp_FLAG2,
121696 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121697 /* ColNames: */ 0, 0,
121698 /* iArg: */ SQLITE_VdbeEQP },
121699 {/* zName: */ "vdbe_listing",
121700 /* ePragTyp: */ PragTyp_FLAG2,
121701 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121702 /* ColNames: */ 0, 0,
121703 /* iArg: */ SQLITE_VdbeListing },
121704 {/* zName: */ "vdbe_trace",
121705 /* ePragTyp: */ PragTyp_FLAG2,
121706 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121707 /* ColNames: */ 0, 0,
121708 /* iArg: */ SQLITE_VdbeTrace },
121709#endif
121710#endif
121711#if !defined(SQLITE_OMIT_WAL)
121712 {/* zName: */ "wal_autocheckpoint",
121713 /* ePragTyp: */ PragTyp_WAL_AUTOCHECKPOINT38,
121714 /* ePragFlg: */ 0,
121715 /* ColNames: */ 0, 0,
121716 /* iArg: */ 0 },
121717 {/* zName: */ "wal_checkpoint",
121718 /* ePragTyp: */ PragTyp_WAL_CHECKPOINT39,
121719 /* ePragFlg: */ PragFlg_NeedSchema0x01,
121720 /* ColNames: */ 38, 3,
121721 /* iArg: */ 0 },
121722#endif
121723#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
121724 {/* zName: */ "writable_schema",
121725 /* ePragTyp: */ PragTyp_FLAG2,
121726 /* ePragFlg: */ PragFlg_Result00x10|PragFlg_NoColumns10x04,
121727 /* ColNames: */ 0, 0,
121728 /* iArg: */ SQLITE_WriteSchema0x00000001|SQLITE_NoSchemaError0x08000000 },
121729#endif
121730};
121731/* Number of pragmas: 62 on by default, 81 total. */
121732
121733/************** End of pragma.h **********************************************/
121734/************** Continuing where we left off in pragma.c *********************/
121735
121736/*
121737** Interpret the given string as a safety level. Return 0 for OFF,
121738** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
121739** unrecognized string argument. The FULL and EXTRA option is disallowed
121740** if the omitFull parameter it 1.
121741**
121742** Note that the values returned are one less that the values that
121743** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
121744** to support legacy SQL code. The safety level used to be boolean
121745** and older scripts may have used numbers 0 for OFF and 1 for ON.
121746*/
121747static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
121748 /* 123456789 123456789 123 */
121749 static const char zText[] = "onoffalseyestruextrafull";
121750 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
121751 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
121752 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
121753 /* on no off false yes true extra full */
121754 int i, n;
121755 if( sqlite3Isdigit(*z)(sqlite3CtypeMap[(unsigned char)(*z)]&0x04) ){
121756 return (u8)sqlite3Atoi(z);
121757 }
121758 n = sqlite3Strlen30(z);
121759 for(i=0; i<ArraySize(iLength)((int)(sizeof(iLength)/sizeof(iLength[0]))); i++){
121760 if( iLength[i]==n && sqlite3StrNICmpsqlite3_strnicmp(&zText[iOffset[i]],z,n)==0
121761 && (!omitFull || iValue[i]<=1)
121762 ){
121763 return iValue[i];
121764 }
121765 }
121766 return dflt;
121767}
121768
121769/*
121770** Interpret the given string as a boolean value.
121771*/
121772SQLITE_PRIVATEstatic u8 sqlite3GetBoolean(const char *z, u8 dflt){
121773 return getSafetyLevel(z,1,dflt)!=0;
121774}
121775
121776/* The sqlite3GetBoolean() function is used by other modules but the
121777** remainder of this file is specific to PRAGMA processing. So omit
121778** the rest of the file if PRAGMAs are omitted from the build.
121779*/
121780#if !defined(SQLITE_OMIT_PRAGMA)
121781
121782/*
121783** Interpret the given string as a locking mode value.
121784*/
121785static int getLockingMode(const char *z){
121786 if( z ){
121787 if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE1;
121788 if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL0;
121789 }
121790 return PAGER_LOCKINGMODE_QUERY-1;
121791}
121792
121793#ifndef SQLITE_OMIT_AUTOVACUUM
121794/*
121795** Interpret the given string as an auto-vacuum mode value.
121796**
121797** The following strings, "none", "full" and "incremental" are
121798** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
121799*/
121800static int getAutoVacuum(const char *z){
121801 int i;
121802 if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE0;
121803 if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL1;
121804 if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR2;
121805 i = sqlite3Atoi(z);
121806 return (u8)((i>=0&&i<=2)?i:0);
121807}
121808#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
121809
121810#ifndef SQLITE_OMIT_PAGER_PRAGMAS
121811/*
121812** Interpret the given string as a temp db location. Return 1 for file
121813** backed temporary databases, 2 for the Red-Black tree in memory database
121814** and 0 to use the compile-time default.
121815*/
121816static int getTempStore(const char *z){
121817 if( z[0]>='0' && z[0]<='2' ){
121818 return z[0] - '0';
121819 }else if( sqlite3StrICmp(z, "file")==0 ){
121820 return 1;
121821 }else if( sqlite3StrICmp(z, "memory")==0 ){
121822 return 2;
121823 }else{
121824 return 0;
121825 }
121826}
121827#endif /* SQLITE_PAGER_PRAGMAS */
121828
121829#ifndef SQLITE_OMIT_PAGER_PRAGMAS
121830/*
121831** Invalidate temp storage, either when the temp storage is changed
121832** from default, or when 'file' and the temp_store_directory has changed
121833*/
121834static int invalidateTempStorage(Parse *pParse){
121835 sqlite3 *db = pParse->db;
121836 if( db->aDb[1].pBt!=0 ){
121837 if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
121838 sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
121839 "from within a transaction");
121840 return SQLITE_ERROR1;
121841 }
121842 sqlite3BtreeClose(db->aDb[1].pBt);
121843 db->aDb[1].pBt = 0;
121844 sqlite3ResetAllSchemasOfConnection(db);
121845 }
121846 return SQLITE_OK0;
121847}
121848#endif /* SQLITE_PAGER_PRAGMAS */
121849
121850#ifndef SQLITE_OMIT_PAGER_PRAGMAS
121851/*
121852** If the TEMP database is open, close it and mark the database schema
121853** as needing reloading. This must be done when using the SQLITE_TEMP_STORE
121854** or DEFAULT_TEMP_STORE pragmas.
121855*/
121856static int changeTempStorage(Parse *pParse, const char *zStorageType){
121857 int ts = getTempStore(zStorageType);
121858 sqlite3 *db = pParse->db;
121859 if( db->temp_store==ts ) return SQLITE_OK0;
121860 if( invalidateTempStorage( pParse ) != SQLITE_OK0 ){
121861 return SQLITE_ERROR1;
121862 }
121863 db->temp_store = (u8)ts;
121864 return SQLITE_OK0;
121865}
121866#endif /* SQLITE_PAGER_PRAGMAS */
121867
121868/*
121869** Set result column names for a pragma.
121870*/
121871static void setPragmaResultColumnNames(
121872 Vdbe *v, /* The query under construction */
121873 const PragmaName *pPragma /* The pragma */
121874){
121875 u8 n = pPragma->nPragCName;
121876 sqlite3VdbeSetNumCols(v, n==0 ? 1 : n);
121877 if( n==0 ){
121878 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, pPragma->zName, SQLITE_STATIC((sqlite3_destructor_type)0));
121879 }else{
121880 int i, j;
121881 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
121882 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, pragCName[j], SQLITE_STATIC((sqlite3_destructor_type)0));
121883 }
121884 }
121885}
121886
121887/*
121888** Generate code to return a single integer value.
121889*/
121890static void returnSingleInt(Vdbe *v, i64 value){
121891 sqlite3VdbeAddOp4Dup8(v, OP_Int6471, 0, 1, 0, (const u8*)&value, P4_INT64(-14));
121892 sqlite3VdbeAddOp2(v, OP_ResultRow81, 1, 1);
121893}
121894
121895/*
121896** Generate code to return a single text value.
121897*/
121898static void returnSingleText(
121899 Vdbe *v, /* Prepared statement under construction */
121900 const char *zValue /* Value to be returned */
121901){
121902 if( zValue ){
121903 sqlite3VdbeLoadString(v, 1, (const char*)zValue);
121904 sqlite3VdbeAddOp2(v, OP_ResultRow81, 1, 1);
121905 }
121906}
121907
121908
121909/*
121910** Set the safety_level and pager flags for pager iDb. Or if iDb<0
121911** set these values for all pagers.
121912*/
121913#ifndef SQLITE_OMIT_PAGER_PRAGMAS
121914static void setAllPagerFlags(sqlite3 *db){
121915 if( db->autoCommit ){
121916 Db *pDb = db->aDb;
121917 int n = db->nDb;
121918 assert( SQLITE_FullFSync==PAGER_FULLFSYNC )((void) (0));
121919 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC )((void) (0));
121920 assert( SQLITE_CacheSpill==PAGER_CACHESPILL )((void) (0));
121921 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)((void) (0))
121922 == PAGER_FLAGS_MASK )((void) (0));
121923 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level )((void) (0));
121924 while( (n--) > 0 ){
121925 if( pDb->pBt ){
121926 sqlite3BtreeSetPagerFlags(pDb->pBt,
121927 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK0x38) );
121928 }
121929 pDb++;
121930 }
121931 }
121932}
121933#else
121934# define setAllPagerFlags(X) /* no-op */
121935#endif
121936
121937
121938/*
121939** Return a human-readable name for a constraint resolution action.
121940*/
121941#ifndef SQLITE_OMIT_FOREIGN_KEY
121942static const char *actionName(u8 action){
121943 const char *zName;
121944 switch( action ){
121945 case OE_SetNull8: zName = "SET NULL"; break;
121946 case OE_SetDflt9: zName = "SET DEFAULT"; break;
121947 case OE_Cascade10: zName = "CASCADE"; break;
121948 case OE_Restrict7: zName = "RESTRICT"; break;
121949 default: zName = "NO ACTION";
121950 assert( action==OE_None )((void) (0)); break;
121951 }
121952 return zName;
121953}
121954#endif
121955
121956
121957/*
121958** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
121959** defined in pager.h. This function returns the associated lowercase
121960** journal-mode name.
121961*/
121962SQLITE_PRIVATEstatic const char *sqlite3JournalModename(int eMode){
121963 static char * const azModeName[] = {
121964 "delete", "persist", "off", "truncate", "memory"
121965#ifndef SQLITE_OMIT_WAL
121966 , "wal"
121967#endif
121968 };
121969 assert( PAGER_JOURNALMODE_DELETE==0 )((void) (0));
121970 assert( PAGER_JOURNALMODE_PERSIST==1 )((void) (0));
121971 assert( PAGER_JOURNALMODE_OFF==2 )((void) (0));
121972 assert( PAGER_JOURNALMODE_TRUNCATE==3 )((void) (0));
121973 assert( PAGER_JOURNALMODE_MEMORY==4 )((void) (0));
121974 assert( PAGER_JOURNALMODE_WAL==5 )((void) (0));
121975 assert( eMode>=0 && eMode<=ArraySize(azModeName) )((void) (0));
121976
121977 if( eMode==ArraySize(azModeName)((int)(sizeof(azModeName)/sizeof(azModeName[0]))) ) return 0;
121978 return azModeName[eMode];
121979}
121980
121981/*
121982** Locate a pragma in the aPragmaName[] array.
121983*/
121984static const PragmaName *pragmaLocate(const char *zName){
121985 int upr, lwr, mid = 0, rc;
121986 lwr = 0;
121987 upr = ArraySize(aPragmaName)((int)(sizeof(aPragmaName)/sizeof(aPragmaName[0])))-1;
121988 while( lwr<=upr ){
121989 mid = (lwr+upr)/2;
121990 rc = sqlite3_stricmp(zName, aPragmaName[mid].zName);
121991 if( rc==0 ) break;
121992 if( rc<0 ){
121993 upr = mid - 1;
121994 }else{
121995 lwr = mid + 1;
121996 }
121997 }
121998 return lwr>upr ? 0 : &aPragmaName[mid];
121999}
122000
122001/*
122002** Helper subroutine for PRAGMA integrity_check:
122003**
122004** Generate code to output a single-column result row with a value of the
122005** string held in register 3. Decrement the result count in register 1
122006** and halt if the maximum number of result rows have been issued.
122007*/
122008static int integrityCheckResultRow(Vdbe *v){
122009 int addr;
122010 sqlite3VdbeAddOp2(v, OP_ResultRow81, 3, 1);
122011 addr = sqlite3VdbeAddOp3(v, OP_IfPos47, 1, sqlite3VdbeCurrentAddr(v)+2, 1);
122012 VdbeCoverage(v);
122013 sqlite3VdbeAddOp0(v, OP_Halt69);
122014 return addr;
122015}
122016
122017/*
122018** Process a pragma statement.
122019**
122020** Pragmas are of this form:
122021**
122022** PRAGMA [schema.]id [= value]
122023**
122024** The identifier might also be a string. The value is a string, and
122025** identifier, or a number. If minusFlag is true, then the value is
122026** a number that was preceded by a minus sign.
122027**
122028** If the left side is "database.id" then pId1 is the database name
122029** and pId2 is the id. If the left side is just "id" then pId1 is the
122030** id and pId2 is any empty string.
122031*/
122032SQLITE_PRIVATEstatic void sqlite3Pragma(
122033 Parse *pParse,
122034 Token *pId1, /* First part of [schema.]id field */
122035 Token *pId2, /* Second part of [schema.]id field, or NULL */
122036 Token *pValue, /* Token for <value>, or NULL */
122037 int minusFlag /* True if a '-' sign preceded <value> */
122038){
122039 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
122040 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
122041 const char *zDb = 0; /* The database name */
122042 Token *pId; /* Pointer to <id> token */
122043 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
122044 int iDb; /* Database index for <database> */
122045 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
122046 sqlite3 *db = pParse->db; /* The database connection */
122047 Db *pDb; /* The specific database being pragmaed */
122048 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
122049 const PragmaName *pPragma; /* The pragma */
122050
122051 if( v==0 ) return;
122052 sqlite3VdbeRunOnlyOnce(v);
122053 pParse->nMem = 2;
122054
122055 /* Interpret the [schema.] part of the pragma statement. iDb is the
122056 ** index of the database this pragma is being applied to in db.aDb[]. */
122057 iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
122058 if( iDb<0 ) return;
122059 pDb = &db->aDb[iDb];
122060
122061 /* If the temp database has been explicitly named as part of the
122062 ** pragma, make sure it is open.
122063 */
122064 if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
122065 return;
122066 }
122067
122068 zLeft = sqlite3NameFromToken(db, pId);
122069 if( !zLeft ) return;
122070 if( minusFlag ){
122071 zRight = sqlite3MPrintf(db, "-%T", pValue);
122072 }else{
122073 zRight = sqlite3NameFromToken(db, pValue);
122074 }
122075
122076 assert( pId2 )((void) (0));
122077 zDb = pId2->n>0 ? pDb->zDbSName : 0;
122078 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA19, zLeft, zRight, zDb) ){
122079 goto pragma_out;
122080 }
122081
122082 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
122083 ** connection. If it returns SQLITE_OK, then assume that the VFS
122084 ** handled the pragma and generate a no-op prepared statement.
122085 **
122086 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
122087 ** an SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
122088 ** object corresponding to the database file to which the pragma
122089 ** statement refers.
122090 **
122091 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
122092 ** file control is an array of pointers to strings (char**) in which the
122093 ** second element of the array is the name of the pragma and the third
122094 ** element is the argument to the pragma or NULL if the pragma has no
122095 ** argument.
122096 */
122097 aFcntl[0] = 0;
122098 aFcntl[1] = zLeft;
122099 aFcntl[2] = zRight;
122100 aFcntl[3] = 0;
122101 db->busyHandler.nBusy = 0;
122102 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA14, (void*)aFcntl);
122103 if( rc==SQLITE_OK0 ){
122104 sqlite3VdbeSetNumCols(v, 1);
122105 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, aFcntl[0], SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
122106 returnSingleText(v, aFcntl[0]);
122107 sqlite3_free(aFcntl[0]);
122108 goto pragma_out;
122109 }
122110 if( rc!=SQLITE_NOTFOUND12 ){
122111 if( aFcntl[0] ){
122112 sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
122113 sqlite3_free(aFcntl[0]);
122114 }
122115 pParse->nErr++;
122116 pParse->rc = rc;
122117 goto pragma_out;
122118 }
122119
122120 /* Locate the pragma in the lookup table */
122121 pPragma = pragmaLocate(zLeft);
122122 if( pPragma==0 ) goto pragma_out;
122123
122124 /* Make sure the database schema is loaded if the pragma requires that */
122125 if( (pPragma->mPragFlg & PragFlg_NeedSchema0x01)!=0 ){
122126 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
122127 }
122128
122129 /* Register the result column names for pragmas that return results */
122130 if( (pPragma->mPragFlg & PragFlg_NoColumns0x02)==0
122131 && ((pPragma->mPragFlg & PragFlg_NoColumns10x04)==0 || zRight==0)
122132 ){
122133 setPragmaResultColumnNames(v, pPragma);
122134 }
122135
122136 /* Jump to the appropriate pragma handler */
122137 switch( pPragma->ePragTyp ){
122138
122139#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
122140 /*
122141 ** PRAGMA [schema.]default_cache_size
122142 ** PRAGMA [schema.]default_cache_size=N
122143 **
122144 ** The first form reports the current persistent setting for the
122145 ** page cache size. The value returned is the maximum number of
122146 ** pages in the page cache. The second form sets both the current
122147 ** page cache size value and the persistent page cache size value
122148 ** stored in the database file.
122149 **
122150 ** Older versions of SQLite would set the default cache size to a
122151 ** negative number to indicate synchronous=OFF. These days, synchronous
122152 ** is always on by default regardless of the sign of the default cache
122153 ** size. But continue to take the absolute value of the default cache
122154 ** size of historical compatibility.
122155 */
122156 case PragTyp_DEFAULT_CACHE_SIZE11: {
122157 static const int iLn = VDBE_OFFSET_LINENO(2)0;
122158 static const VdbeOpList getCacheSize[] = {
122159 { OP_Transaction2, 0, 0, 0}, /* 0 */
122160 { OP_ReadCookie94, 0, 1, BTREE_DEFAULT_CACHE_SIZE3}, /* 1 */
122161 { OP_IfPos47, 1, 8, 0},
122162 { OP_Integer70, 0, 2, 0},
122163 { OP_Subtract101, 1, 2, 1},
122164 { OP_IfPos47, 1, 8, 0},
122165 { OP_Integer70, 0, 1, 0}, /* 6 */
122166 { OP_Noop170, 0, 0, 0},
122167 { OP_ResultRow81, 1, 1, 0},
122168 };
122169 VdbeOp *aOp;
122170 sqlite3VdbeUsesBtree(v, iDb);
122171 if( !zRight ){
122172 pParse->nMem += 2;
122173 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(getCacheSize));
122174 aOp = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize)((int)(sizeof(getCacheSize)/sizeof(getCacheSize[0]))), getCacheSize, iLn);
122175 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
122176 aOp[0].p1 = iDb;
122177 aOp[1].p1 = iDb;
122178 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE-2000;
122179 }else{
122180 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
122181 sqlite3BeginWriteOperation(pParse, 0, iDb);
122182 sqlite3VdbeAddOp3(v, OP_SetCookie95, iDb, BTREE_DEFAULT_CACHE_SIZE3, size);
122183 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
122184 pDb->pSchema->cache_size = size;
122185 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
122186 }
122187 break;
122188 }
122189#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
122190
122191#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
122192 /*
122193 ** PRAGMA [schema.]page_size
122194 ** PRAGMA [schema.]page_size=N
122195 **
122196 ** The first form reports the current setting for the
122197 ** database page size in bytes. The second form sets the
122198 ** database page size value. The value can only be set if
122199 ** the database has not yet been created.
122200 */
122201 case PragTyp_PAGE_SIZE28: {
122202 Btree *pBt = pDb->pBt;
122203 assert( pBt!=0 )((void) (0));
122204 if( !zRight ){
122205 int size = ALWAYS(pBt)(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
122206 returnSingleInt(v, size);
122207 }else{
122208 /* Malloc may fail when setting the page-size, as there is an internal
122209 ** buffer that the pager module resizes using sqlite3_realloc().
122210 */
122211 db->nextPagesize = sqlite3Atoi(zRight);
122212 if( SQLITE_NOMEM7==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
122213 sqlite3OomFault(db);
122214 }
122215 }
122216 break;
122217 }
122218
122219 /*
122220 ** PRAGMA [schema.]secure_delete
122221 ** PRAGMA [schema.]secure_delete=ON/OFF/FAST
122222 **
122223 ** The first form reports the current setting for the
122224 ** secure_delete flag. The second form changes the secure_delete
122225 ** flag setting and reports the new value.
122226 */
122227 case PragTyp_SECURE_DELETE30: {
122228 Btree *pBt = pDb->pBt;
122229 int b = -1;
122230 assert( pBt!=0 )((void) (0));
122231 if( zRight ){
122232 if( sqlite3_stricmp(zRight, "fast")==0 ){
122233 b = 2;
122234 }else{
122235 b = sqlite3GetBoolean(zRight, 0);
122236 }
122237 }
122238 if( pId2->n==0 && b>=0 ){
122239 int ii;
122240 for(ii=0; ii<db->nDb; ii++){
122241 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
122242 }
122243 }
122244 b = sqlite3BtreeSecureDelete(pBt, b);
122245 returnSingleInt(v, b);
122246 break;
122247 }
122248
122249 /*
122250 ** PRAGMA [schema.]max_page_count
122251 ** PRAGMA [schema.]max_page_count=N
122252 **
122253 ** The first form reports the current setting for the
122254 ** maximum number of pages in the database file. The
122255 ** second form attempts to change this setting. Both
122256 ** forms return the current setting.
122257 **
122258 ** The absolute value of N is used. This is undocumented and might
122259 ** change. The only purpose is to provide an easy way to test
122260 ** the sqlite3AbsInt32() function.
122261 **
122262 ** PRAGMA [schema.]page_count
122263 **
122264 ** Return the number of pages in the specified database.
122265 */
122266 case PragTyp_PAGE_COUNT24: {
122267 int iReg;
122268 sqlite3CodeVerifySchema(pParse, iDb);
122269 iReg = ++pParse->nMem;
122270 if( sqlite3Tolower(zLeft[0])(sqlite3UpperToLower[(unsigned char)(zLeft[0])])=='p' ){
122271 sqlite3VdbeAddOp2(v, OP_Pagecount166, iDb, iReg);
122272 }else{
122273 sqlite3VdbeAddOp3(v, OP_MaxPgcnt167, iDb, iReg,
122274 sqlite3AbsInt32(sqlite3Atoi(zRight)));
122275 }
122276 sqlite3VdbeAddOp2(v, OP_ResultRow81, iReg, 1);
122277 break;
122278 }
122279
122280 /*
122281 ** PRAGMA [schema.]locking_mode
122282 ** PRAGMA [schema.]locking_mode = (normal|exclusive)
122283 */
122284 case PragTyp_LOCKING_MODE23: {
122285 const char *zRet = "normal";
122286 int eMode = getLockingMode(zRight);
122287
122288 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY-1 ){
122289 /* Simple "PRAGMA locking_mode;" statement. This is a query for
122290 ** the current default locking mode (which may be different to
122291 ** the locking-mode of the main database).
122292 */
122293 eMode = db->dfltLockMode;
122294 }else{
122295 Pager *pPager;
122296 if( pId2->n==0 ){
122297 /* This indicates that no database name was specified as part
122298 ** of the PRAGMA command. In this case the locking-mode must be
122299 ** set on all attached databases, as well as the main db file.
122300 **
122301 ** Also, the sqlite3.dfltLockMode variable is set so that
122302 ** any subsequently attached databases also use the specified
122303 ** locking mode.
122304 */
122305 int ii;
122306 assert(pDb==&db->aDb[0])((void) (0));
122307 for(ii=2; ii<db->nDb; ii++){
122308 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
122309 sqlite3PagerLockingMode(pPager, eMode);
122310 }
122311 db->dfltLockMode = (u8)eMode;
122312 }
122313 pPager = sqlite3BtreePager(pDb->pBt);
122314 eMode = sqlite3PagerLockingMode(pPager, eMode);
122315 }
122316
122317 assert( eMode==PAGER_LOCKINGMODE_NORMAL((void) (0))
122318 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE )((void) (0));
122319 if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE1 ){
122320 zRet = "exclusive";
122321 }
122322 returnSingleText(v, zRet);
122323 break;
122324 }
122325
122326 /*
122327 ** PRAGMA [schema.]journal_mode
122328 ** PRAGMA [schema.]journal_mode =
122329 ** (delete|persist|off|truncate|memory|wal|off)
122330 */
122331 case PragTyp_JOURNAL_MODE20: {
122332 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
122333 int ii; /* Loop counter */
122334
122335 if( zRight==0 ){
122336 /* If there is no "=MODE" part of the pragma, do a query for the
122337 ** current mode */
122338 eMode = PAGER_JOURNALMODE_QUERY(-1);
122339 }else{
122340 const char *zMode;
122341 int n = sqlite3Strlen30(zRight);
122342 for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
122343 if( sqlite3StrNICmpsqlite3_strnicmp(zRight, zMode, n)==0 ) break;
122344 }
122345 if( !zMode ){
122346 /* If the "=MODE" part does not match any known journal mode,
122347 ** then do a query */
122348 eMode = PAGER_JOURNALMODE_QUERY(-1);
122349 }
122350 if( eMode==PAGER_JOURNALMODE_OFF2 && (db->flags & SQLITE_Defensive0x10000000)!=0 ){
122351 /* Do not allow journal-mode "OFF" in defensive since the database
122352 ** can become corrupted using ordinary SQL when the journal is off */
122353 eMode = PAGER_JOURNALMODE_QUERY(-1);
122354 }
122355 }
122356 if( eMode==PAGER_JOURNALMODE_QUERY(-1) && pId2->n==0 ){
122357 /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
122358 iDb = 0;
122359 pId2->n = 1;
122360 }
122361 for(ii=db->nDb-1; ii>=0; ii--){
122362 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
122363 sqlite3VdbeUsesBtree(v, ii);
122364 sqlite3VdbeAddOp3(v, OP_JournalMode7, ii, 1, eMode);
122365 }
122366 }
122367 sqlite3VdbeAddOp2(v, OP_ResultRow81, 1, 1);
122368 break;
122369 }
122370
122371 /*
122372 ** PRAGMA [schema.]journal_size_limit
122373 ** PRAGMA [schema.]journal_size_limit=N
122374 **
122375 ** Get or set the size limit on rollback journal files.
122376 */
122377 case PragTyp_JOURNAL_SIZE_LIMIT21: {
122378 Pager *pPager = sqlite3BtreePager(pDb->pBt);
122379 i64 iLimit = -2;
122380 if( zRight ){
122381 sqlite3DecOrHexToI64(zRight, &iLimit);
122382 if( iLimit<-1 ) iLimit = -1;
122383 }
122384 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
122385 returnSingleInt(v, iLimit);
122386 break;
122387 }
122388
122389#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
122390
122391 /*
122392 ** PRAGMA [schema.]auto_vacuum
122393 ** PRAGMA [schema.]auto_vacuum=N
122394 **
122395 ** Get or set the value of the database 'auto-vacuum' parameter.
122396 ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL
122397 */
122398#ifndef SQLITE_OMIT_AUTOVACUUM
122399 case PragTyp_AUTO_VACUUM1: {
122400 Btree *pBt = pDb->pBt;
122401 assert( pBt!=0 )((void) (0));
122402 if( !zRight ){
122403 returnSingleInt(v, sqlite3BtreeGetAutoVacuum(pBt));
122404 }else{
122405 int eAuto = getAutoVacuum(zRight);
122406 assert( eAuto>=0 && eAuto<=2 )((void) (0));
122407 db->nextAutovac = (u8)eAuto;
122408 /* Call SetAutoVacuum() to set initialize the internal auto and
122409 ** incr-vacuum flags. This is required in case this connection
122410 ** creates the database file. It is important that it is created
122411 ** as an auto-vacuum capable db.
122412 */
122413 rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
122414 if( rc==SQLITE_OK0 && (eAuto==1 || eAuto==2) ){
122415 /* When setting the auto_vacuum mode to either "full" or
122416 ** "incremental", write the value of meta[6] in the database
122417 ** file. Before writing to meta[6], check that meta[3] indicates
122418 ** that this really is an auto-vacuum capable database.
122419 */
122420 static const int iLn = VDBE_OFFSET_LINENO(2)0;
122421 static const VdbeOpList setMeta6[] = {
122422 { OP_Transaction2, 0, 1, 0}, /* 0 */
122423 { OP_ReadCookie94, 0, 1, BTREE_LARGEST_ROOT_PAGE4},
122424 { OP_If18, 1, 0, 0}, /* 2 */
122425 { OP_Halt69, SQLITE_OK0, OE_Abort2, 0}, /* 3 */
122426 { OP_SetCookie95, 0, BTREE_INCR_VACUUM7, 0}, /* 4 */
122427 };
122428 VdbeOp *aOp;
122429 int iAddr = sqlite3VdbeCurrentAddr(v);
122430 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
122431 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6)((int)(sizeof(setMeta6)/sizeof(setMeta6[0]))), setMeta6, iLn);
122432 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
122433 aOp[0].p1 = iDb;
122434 aOp[1].p1 = iDb;
122435 aOp[2].p2 = iAddr+4;
122436 aOp[4].p1 = iDb;
122437 aOp[4].p3 = eAuto - 1;
122438 sqlite3VdbeUsesBtree(v, iDb);
122439 }
122440 }
122441 break;
122442 }
122443#endif
122444
122445 /*
122446 ** PRAGMA [schema.]incremental_vacuum(N)
122447 **
122448 ** Do N steps of incremental vacuuming on a database.
122449 */
122450#ifndef SQLITE_OMIT_AUTOVACUUM
122451 case PragTyp_INCREMENTAL_VACUUM16: {
122452 int iLimit, addr;
122453 if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
122454 iLimit = 0x7fffffff;
122455 }
122456 sqlite3BeginWriteOperation(pParse, 0, iDb);
122457 sqlite3VdbeAddOp2(v, OP_Integer70, iLimit, 1);
122458 addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum59, iDb); VdbeCoverage(v);
122459 sqlite3VdbeAddOp1(v, OP_ResultRow81, 1);
122460 sqlite3VdbeAddOp2(v, OP_AddImm83, 1, -1);
122461 sqlite3VdbeAddOp2(v, OP_IfPos47, 1, addr); VdbeCoverage(v);
122462 sqlite3VdbeJumpHere(v, addr);
122463 break;
122464 }
122465#endif
122466
122467#ifndef SQLITE_OMIT_PAGER_PRAGMAS
122468 /*
122469 ** PRAGMA [schema.]cache_size
122470 ** PRAGMA [schema.]cache_size=N
122471 **
122472 ** The first form reports the current local setting for the
122473 ** page cache size. The second form sets the local
122474 ** page cache size value. If N is positive then that is the
122475 ** number of pages in the cache. If N is negative, then the
122476 ** number of pages is adjusted so that the cache uses -N kibibytes
122477 ** of memory.
122478 */
122479 case PragTyp_CACHE_SIZE4: {
122480 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
122481 if( !zRight ){
122482 returnSingleInt(v, pDb->pSchema->cache_size);
122483 }else{
122484 int size = sqlite3Atoi(zRight);
122485 pDb->pSchema->cache_size = size;
122486 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
122487 }
122488 break;
122489 }
122490
122491 /*
122492 ** PRAGMA [schema.]cache_spill
122493 ** PRAGMA cache_spill=BOOLEAN
122494 ** PRAGMA [schema.]cache_spill=N
122495 **
122496 ** The first form reports the current local setting for the
122497 ** page cache spill size. The second form turns cache spill on
122498 ** or off. When turnning cache spill on, the size is set to the
122499 ** current cache_size. The third form sets a spill size that
122500 ** may be different form the cache size.
122501 ** If N is positive then that is the
122502 ** number of pages in the cache. If N is negative, then the
122503 ** number of pages is adjusted so that the cache uses -N kibibytes
122504 ** of memory.
122505 **
122506 ** If the number of cache_spill pages is less then the number of
122507 ** cache_size pages, no spilling occurs until the page count exceeds
122508 ** the number of cache_size pages.
122509 **
122510 ** The cache_spill=BOOLEAN setting applies to all attached schemas,
122511 ** not just the schema specified.
122512 */
122513 case PragTyp_CACHE_SPILL5: {
122514 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
122515 if( !zRight ){
122516 returnSingleInt(v,
122517 (db->flags & SQLITE_CacheSpill0x00000020)==0 ? 0 :
122518 sqlite3BtreeSetSpillSize(pDb->pBt,0));
122519 }else{
122520 int size = 1;
122521 if( sqlite3GetInt32(zRight, &size) ){
122522 sqlite3BtreeSetSpillSize(pDb->pBt, size);
122523 }
122524 if( sqlite3GetBoolean(zRight, size!=0) ){
122525 db->flags |= SQLITE_CacheSpill0x00000020;
122526 }else{
122527 db->flags &= ~(u64)SQLITE_CacheSpill0x00000020;
122528 }
122529 setAllPagerFlags(db);
122530 }
122531 break;
122532 }
122533
122534 /*
122535 ** PRAGMA [schema.]mmap_size(N)
122536 **
122537 ** Used to set mapping size limit. The mapping size limit is
122538 ** used to limit the aggregate size of all memory mapped regions of the
122539 ** database file. If this parameter is set to zero, then memory mapping
122540 ** is not used at all. If N is negative, then the default memory map
122541 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
122542 ** The parameter N is measured in bytes.
122543 **
122544 ** This value is advisory. The underlying VFS is free to memory map
122545 ** as little or as much as it wants. Except, if N is set to 0 then the
122546 ** upper layers will never invoke the xFetch interfaces to the VFS.
122547 */
122548 case PragTyp_MMAP_SIZE25: {
122549 sqlite3_int64 sz;
122550#if SQLITE_MAX_MMAP_SIZE0x7fff0000>0
122551 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
122552 if( zRight ){
122553 int ii;
122554 sqlite3DecOrHexToI64(zRight, &sz);
122555 if( sz<0 ) sz = sqlite3GlobalConfigsqlite3Config.szMmap;
122556 if( pId2->n==0 ) db->szMmap = sz;
122557 for(ii=db->nDb-1; ii>=0; ii--){
122558 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
122559 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
122560 }
122561 }
122562 }
122563 sz = -1;
122564 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE18, &sz);
122565#else
122566 sz = 0;
122567 rc = SQLITE_OK0;
122568#endif
122569 if( rc==SQLITE_OK0 ){
122570 returnSingleInt(v, sz);
122571 }else if( rc!=SQLITE_NOTFOUND12 ){
122572 pParse->nErr++;
122573 pParse->rc = rc;
122574 }
122575 break;
122576 }
122577
122578 /*
122579 ** PRAGMA temp_store
122580 ** PRAGMA temp_store = "default"|"memory"|"file"
122581 **
122582 ** Return or set the local value of the temp_store flag. Changing
122583 ** the local value does not make changes to the disk file and the default
122584 ** value will be restored the next time the database is opened.
122585 **
122586 ** Note that it is possible for the library compile-time options to
122587 ** override this setting
122588 */
122589 case PragTyp_TEMP_STORE35: {
122590 if( !zRight ){
122591 returnSingleInt(v, db->temp_store);
122592 }else{
122593 changeTempStorage(pParse, zRight);
122594 }
122595 break;
122596 }
122597
122598 /*
122599 ** PRAGMA temp_store_directory
122600 ** PRAGMA temp_store_directory = ""|"directory_name"
122601 **
122602 ** Return or set the local value of the temp_store_directory flag. Changing
122603 ** the value sets a specific directory to be used for temporary files.
122604 ** Setting to a null string reverts to the default temporary directory search.
122605 ** If temporary directory is changed, then invalidateTempStorage.
122606 **
122607 */
122608 case PragTyp_TEMP_STORE_DIRECTORY36: {
122609 if( !zRight ){
122610 returnSingleText(v, sqlite3_temp_directory);
122611 }else{
122612#ifndef SQLITE_OMIT_WSD
122613 if( zRight[0] ){
122614 int res;
122615 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE1, &res);
122616 if( rc!=SQLITE_OK0 || res==0 ){
122617 sqlite3ErrorMsg(pParse, "not a writable directory");
122618 goto pragma_out;
122619 }
122620 }
122621 if( SQLITE_TEMP_STORE1==0
122622 || (SQLITE_TEMP_STORE1==1 && db->temp_store<=1)
122623 || (SQLITE_TEMP_STORE1==2 && db->temp_store==1)
122624 ){
122625 invalidateTempStorage(pParse);
122626 }
122627 sqlite3_free(sqlite3_temp_directory);
122628 if( zRight[0] ){
122629 sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
122630 }else{
122631 sqlite3_temp_directory = 0;
122632 }
122633#endif /* SQLITE_OMIT_WSD */
122634 }
122635 break;
122636 }
122637
122638#if SQLITE_OS_WIN0
122639 /*
122640 ** PRAGMA data_store_directory
122641 ** PRAGMA data_store_directory = ""|"directory_name"
122642 **
122643 ** Return or set the local value of the data_store_directory flag. Changing
122644 ** the value sets a specific directory to be used for database files that
122645 ** were specified with a relative pathname. Setting to a null string reverts
122646 ** to the default database directory, which for database files specified with
122647 ** a relative path will probably be based on the current directory for the
122648 ** process. Database file specified with an absolute path are not impacted
122649 ** by this setting, regardless of its value.
122650 **
122651 */
122652 case PragTyp_DATA_STORE_DIRECTORY9: {
122653 if( !zRight ){
122654 returnSingleText(v, sqlite3_data_directory);
122655 }else{
122656#ifndef SQLITE_OMIT_WSD
122657 if( zRight[0] ){
122658 int res;
122659 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE1, &res);
122660 if( rc!=SQLITE_OK0 || res==0 ){
122661 sqlite3ErrorMsg(pParse, "not a writable directory");
122662 goto pragma_out;
122663 }
122664 }
122665 sqlite3_free(sqlite3_data_directory);
122666 if( zRight[0] ){
122667 sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
122668 }else{
122669 sqlite3_data_directory = 0;
122670 }
122671#endif /* SQLITE_OMIT_WSD */
122672 }
122673 break;
122674 }
122675#endif
122676
122677#if SQLITE_ENABLE_LOCKING_STYLE0
122678 /*
122679 ** PRAGMA [schema.]lock_proxy_file
122680 ** PRAGMA [schema.]lock_proxy_file = ":auto:"|"lock_file_path"
122681 **
122682 ** Return or set the value of the lock_proxy_file flag. Changing
122683 ** the value sets a specific file to be used for database access locks.
122684 **
122685 */
122686 case PragTyp_LOCK_PROXY_FILE22: {
122687 if( !zRight ){
122688 Pager *pPager = sqlite3BtreePager(pDb->pBt);
122689 char *proxy_file_path = NULL((void*)0);
122690 sqlite3_file *pFile = sqlite3PagerFile(pPager);
122691 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE2,
122692 &proxy_file_path);
122693 returnSingleText(v, proxy_file_path);
122694 }else{
122695 Pager *pPager = sqlite3BtreePager(pDb->pBt);
122696 sqlite3_file *pFile = sqlite3PagerFile(pPager);
122697 int res;
122698 if( zRight[0] ){
122699 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE3,
122700 zRight);
122701 } else {
122702 res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE3,
122703 NULL((void*)0));
122704 }
122705 if( res!=SQLITE_OK0 ){
122706 sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
122707 goto pragma_out;
122708 }
122709 }
122710 break;
122711 }
122712#endif /* SQLITE_ENABLE_LOCKING_STYLE */
122713
122714 /*
122715 ** PRAGMA [schema.]synchronous
122716 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
122717 **
122718 ** Return or set the local value of the synchronous flag. Changing
122719 ** the local value does not make changes to the disk file and the
122720 ** default value will be restored the next time the database is
122721 ** opened.
122722 */
122723 case PragTyp_SYNCHRONOUS33: {
122724 if( !zRight ){
122725 returnSingleInt(v, pDb->safety_level-1);
122726 }else{
122727 if( !db->autoCommit ){
122728 sqlite3ErrorMsg(pParse,
122729 "Safety level may not be changed inside a transaction");
122730 }else if( iDb!=1 ){
122731 int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK0x07;
122732 if( iLevel==0 ) iLevel = 1;
122733 pDb->safety_level = iLevel;
122734 pDb->bSyncSet = 1;
122735 setAllPagerFlags(db);
122736 }
122737 }
122738 break;
122739 }
122740#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
122741
122742#ifndef SQLITE_OMIT_FLAG_PRAGMAS
122743 case PragTyp_FLAG2: {
122744 if( zRight==0 ){
122745 setPragmaResultColumnNames(v, pPragma);
122746 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
122747 }else{
122748 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
122749 if( db->autoCommit==0 ){
122750 /* Foreign key support may not be enabled or disabled while not
122751 ** in auto-commit mode. */
122752 mask &= ~(SQLITE_ForeignKeys0x00004000);
122753 }
122754#if SQLITE_USER_AUTHENTICATION
122755 if( db->auth.authLevel==UAUTH_User ){
122756 /* Do not allow non-admin users to modify the schema arbitrarily */
122757 mask &= ~(SQLITE_WriteSchema0x00000001);
122758 }
122759#endif
122760
122761 if( sqlite3GetBoolean(zRight, 0) ){
122762 db->flags |= mask;
122763 }else{
122764 db->flags &= ~mask;
122765 if( mask==SQLITE_DeferFKs0x00080000 ) db->nDeferredImmCons = 0;
122766 }
122767
122768 /* Many of the flag-pragmas modify the code generated by the SQL
122769 ** compiler (eg. count_changes). So add an opcode to expire all
122770 ** compiled SQL statements after modifying a pragma value.
122771 */
122772 sqlite3VdbeAddOp0(v, OP_Expire158);
122773 setAllPagerFlags(db);
122774 }
122775 break;
122776 }
122777#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
122778
122779#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
122780 /*
122781 ** PRAGMA table_info(<table>)
122782 **
122783 ** Return a single row for each column of the named table. The columns of
122784 ** the returned data set are:
122785 **
122786 ** cid: Column id (numbered from left to right, starting at 0)
122787 ** name: Column name
122788 ** type: Column declaration type.
122789 ** notnull: True if 'NOT NULL' is part of column declaration
122790 ** dflt_value: The default value for the column, if any.
122791 ** pk: Non-zero for PK fields.
122792 */
122793 case PragTyp_TABLE_INFO34: if( zRight ){
122794 Table *pTab;
122795 pTab = sqlite3LocateTable(pParse, LOCATE_NOERR0x02, zRight, zDb);
122796 if( pTab ){
122797 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
122798 int i, k;
122799 int nHidden = 0;
122800 Column *pCol;
122801 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
122802 pParse->nMem = 7;
122803 sqlite3CodeVerifySchema(pParse, iTabDb);
122804 sqlite3ViewGetColumnNames(pParse, pTab);
122805 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
122806 int isHidden = IsHiddenColumn(pCol)(((pCol)->colFlags & 0x0002)!=0);
122807 if( isHidden && pPragma->iArg==0 ){
122808 nHidden++;
122809 continue;
122810 }
122811 if( (pCol->colFlags & COLFLAG_PRIMKEY0x0001)==0 ){
122812 k = 0;
122813 }else if( pPk==0 ){
122814 k = 1;
122815 }else{
122816 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
122817 }
122818 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN )((void) (0));
122819 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
122820 i-nHidden,
122821 pCol->zName,
122822 sqlite3ColumnType(pCol,""),
122823 pCol->notNull ? 1 : 0,
122824 pCol->pDflt ? pCol->pDflt->u.zToken : 0,
122825 k,
122826 isHidden);
122827 }
122828 }
122829 }
122830 break;
122831
122832#ifdef SQLITE_DEBUG
122833 case PragTyp_STATS43: {
122834 Index *pIdx;
122835 HashElem *i;
122836 pParse->nMem = 5;
122837 sqlite3CodeVerifySchema(pParse, iDb);
122838 for(i=sqliteHashFirst(&pDb->pSchema->tblHash)((&pDb->pSchema->tblHash)->first); i; i=sqliteHashNext(i)((i)->next)){
122839 Table *pTab = sqliteHashData(i)((i)->data);
122840 sqlite3VdbeMultiLoad(v, 1, "ssiii",
122841 pTab->zName,
122842 0,
122843 pTab->szTabRow,
122844 pTab->nRowLogEst,
122845 pTab->tabFlags);
122846 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122847 sqlite3VdbeMultiLoad(v, 2, "siiiX",
122848 pIdx->zName,
122849 pIdx->szIdxRow,
122850 pIdx->aiRowLogEst[0],
122851 pIdx->hasStat1);
122852 sqlite3VdbeAddOp2(v, OP_ResultRow81, 1, 5);
122853 }
122854 }
122855 }
122856 break;
122857#endif
122858
122859 case PragTyp_INDEX_INFO17: if( zRight ){
122860 Index *pIdx;
122861 Table *pTab;
122862 pIdx = sqlite3FindIndex(db, zRight, zDb);
122863 if( pIdx ){
122864 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
122865 int i;
122866 int mx;
122867 if( pPragma->iArg ){
122868 /* PRAGMA index_xinfo (newer version with more rows and columns) */
122869 mx = pIdx->nColumn;
122870 pParse->nMem = 6;
122871 }else{
122872 /* PRAGMA index_info (legacy version) */
122873 mx = pIdx->nKeyCol;
122874 pParse->nMem = 3;
122875 }
122876 pTab = pIdx->pTable;
122877 sqlite3CodeVerifySchema(pParse, iIdxDb);
122878 assert( pParse->nMem<=pPragma->nPragCName )((void) (0));
122879 for(i=0; i<mx; i++){
122880 i16 cnum = pIdx->aiColumn[i];
122881 sqlite3VdbeMultiLoad(v, 1, "iisX", i, cnum,
122882 cnum<0 ? 0 : pTab->aCol[cnum].zName);
122883 if( pPragma->iArg ){
122884 sqlite3VdbeMultiLoad(v, 4, "isiX",
122885 pIdx->aSortOrder[i],
122886 pIdx->azColl[i],
122887 i<pIdx->nKeyCol);
122888 }
122889 sqlite3VdbeAddOp2(v, OP_ResultRow81, 1, pParse->nMem);
122890 }
122891 }
122892 }
122893 break;
122894
122895 case PragTyp_INDEX_LIST18: if( zRight ){
122896 Index *pIdx;
122897 Table *pTab;
122898 int i;
122899 pTab = sqlite3FindTable(db, zRight, zDb);
122900 if( pTab ){
122901 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
122902 pParse->nMem = 5;
122903 sqlite3CodeVerifySchema(pParse, iTabDb);
122904 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
122905 const char *azOrigin[] = { "c", "u", "pk" };
122906 sqlite3VdbeMultiLoad(v, 1, "isisi",
122907 i,
122908 pIdx->zName,
122909 IsUniqueIndex(pIdx)((pIdx)->onError!=0),
122910 azOrigin[pIdx->idxType],
122911 pIdx->pPartIdxWhere!=0);
122912 }
122913 }
122914 }
122915 break;
122916
122917 case PragTyp_DATABASE_LIST10: {
122918 int i;
122919 pParse->nMem = 3;
122920 for(i=0; i<db->nDb; i++){
122921 if( db->aDb[i].pBt==0 ) continue;
122922 assert( db->aDb[i].zDbSName!=0 )((void) (0));
122923 sqlite3VdbeMultiLoad(v, 1, "iss",
122924 i,
122925 db->aDb[i].zDbSName,
122926 sqlite3BtreeGetFilename(db->aDb[i].pBt));
122927 }
122928 }
122929 break;
122930
122931 case PragTyp_COLLATION_LIST7: {
122932 int i = 0;
122933 HashElem *p;
122934 pParse->nMem = 2;
122935 for(p=sqliteHashFirst(&db->aCollSeq)((&db->aCollSeq)->first); p; p=sqliteHashNext(p)((p)->next)){
122936 CollSeq *pColl = (CollSeq *)sqliteHashData(p)((p)->data);
122937 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
122938 }
122939 }
122940 break;
122941
122942#ifdef SQLITE_INTROSPECTION_PRAGMAS
122943 case PragTyp_FUNCTION_LIST15: {
122944 int i;
122945 HashElem *j;
122946 FuncDef *p;
122947 pParse->nMem = 2;
122948 for(i=0; i<SQLITE_FUNC_HASH_SZ23; i++){
122949 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
122950 if( p->funcFlags & SQLITE_FUNC_INTERNAL0x00040000 ) continue;
122951 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 1);
122952 }
122953 }
122954 for(j=sqliteHashFirst(&db->aFunc)((&db->aFunc)->first); j; j=sqliteHashNext(j)((j)->next)){
122955 p = (FuncDef*)sqliteHashData(j)((j)->data);
122956 sqlite3VdbeMultiLoad(v, 1, "si", p->zName, 0);
122957 }
122958 }
122959 break;
122960
122961#ifndef SQLITE_OMIT_VIRTUALTABLE
122962 case PragTyp_MODULE_LIST26: {
122963 HashElem *j;
122964 pParse->nMem = 1;
122965 for(j=sqliteHashFirst(&db->aModule)((&db->aModule)->first); j; j=sqliteHashNext(j)((j)->next)){
122966 Module *pMod = (Module*)sqliteHashData(j)((j)->data);
122967 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
122968 }
122969 }
122970 break;
122971#endif /* SQLITE_OMIT_VIRTUALTABLE */
122972
122973 case PragTyp_PRAGMA_LIST29: {
122974 int i;
122975 for(i=0; i<ArraySize(aPragmaName)((int)(sizeof(aPragmaName)/sizeof(aPragmaName[0]))); i++){
122976 sqlite3VdbeMultiLoad(v, 1, "s", aPragmaName[i].zName);
122977 }
122978 }
122979 break;
122980#endif /* SQLITE_INTROSPECTION_PRAGMAS */
122981
122982#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
122983
122984#ifndef SQLITE_OMIT_FOREIGN_KEY
122985 case PragTyp_FOREIGN_KEY_LIST14: if( zRight ){
122986 FKey *pFK;
122987 Table *pTab;
122988 pTab = sqlite3FindTable(db, zRight, zDb);
122989 if( pTab ){
122990 pFK = pTab->pFKey;
122991 if( pFK ){
122992 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
122993 int i = 0;
122994 pParse->nMem = 8;
122995 sqlite3CodeVerifySchema(pParse, iTabDb);
122996 while(pFK){
122997 int j;
122998 for(j=0; j<pFK->nCol; j++){
122999 sqlite3VdbeMultiLoad(v, 1, "iissssss",
123000 i,
123001 j,
123002 pFK->zTo,
123003 pTab->aCol[pFK->aCol[j].iFrom].zName,
123004 pFK->aCol[j].zCol,
123005 actionName(pFK->aAction[1]), /* ON UPDATE */
123006 actionName(pFK->aAction[0]), /* ON DELETE */
123007 "NONE");
123008 }
123009 ++i;
123010 pFK = pFK->pNextFrom;
123011 }
123012 }
123013 }
123014 }
123015 break;
123016#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
123017
123018#ifndef SQLITE_OMIT_FOREIGN_KEY
123019#ifndef SQLITE_OMIT_TRIGGER
123020 case PragTyp_FOREIGN_KEY_CHECK13: {
123021 FKey *pFK; /* A foreign key constraint */
123022 Table *pTab; /* Child table contain "REFERENCES" keyword */
123023 Table *pParent; /* Parent table that child points to */
123024 Index *pIdx; /* Index in the parent table */
123025 int i; /* Loop counter: Foreign key number for pTab */
123026 int j; /* Loop counter: Field of the foreign key */
123027 HashElem *k; /* Loop counter: Next table in schema */
123028 int x; /* result variable */
123029 int regResult; /* 3 registers to hold a result row */
123030 int regKey; /* Register to hold key for checking the FK */
123031 int regRow; /* Registers to hold a row from pTab */
123032 int addrTop; /* Top of a loop checking foreign keys */
123033 int addrOk; /* Jump here if the key is OK */
123034 int *aiCols; /* child to parent column mapping */
123035
123036 regResult = pParse->nMem+1;
123037 pParse->nMem += 4;
123038 regKey = ++pParse->nMem;
123039 regRow = ++pParse->nMem;
123040 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash)((&db->aDb[iDb].pSchema->tblHash)->first);
123041 while( k ){
123042 int iTabDb;
123043 if( zRight ){
123044 pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
123045 k = 0;
123046 }else{
123047 pTab = (Table*)sqliteHashData(k)((k)->data);
123048 k = sqliteHashNext(k)((k)->next);
123049 }
123050 if( pTab==0 || pTab->pFKey==0 ) continue;
123051 iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
123052 sqlite3CodeVerifySchema(pParse, iTabDb);
123053 sqlite3TableLock(pParse, iTabDb, pTab->tnum, 0, pTab->zName);
123054 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
123055 sqlite3OpenTable(pParse, 0, iTabDb, pTab, OP_OpenRead108);
123056 sqlite3VdbeLoadString(v, regResult, pTab->zName);
123057 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
123058 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
123059 if( pParent==0 ) continue;
123060 pIdx = 0;
123061 sqlite3TableLock(pParse, iTabDb, pParent->tnum, 0, pParent->zName);
123062 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
123063 if( x==0 ){
123064 if( pIdx==0 ){
123065 sqlite3OpenTable(pParse, i, iTabDb, pParent, OP_OpenRead108);
123066 }else{
123067 sqlite3VdbeAddOp3(v, OP_OpenRead108, i, pIdx->tnum, iTabDb);
123068 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
123069 }
123070 }else{
123071 k = 0;
123072 break;
123073 }
123074 }
123075 assert( pParse->nErr>0 || pFK==0 )((void) (0));
123076 if( pFK ) break;
123077 if( pParse->nTab<i ) pParse->nTab = i;
123078 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind36, 0); VdbeCoverage(v);
123079 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
123080 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
123081 pIdx = 0;
123082 aiCols = 0;
123083 if( pParent ){
123084 x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
123085 assert( x==0 )((void) (0));
123086 }
123087 addrOk = sqlite3VdbeMakeLabel(pParse);
123088
123089 /* Generate code to read the child key values into registers
123090 ** regRow..regRow+n. If any of the child key values are NULL, this
123091 ** row cannot cause an FK violation. Jump directly to addrOk in
123092 ** this case. */
123093 for(j=0; j<pFK->nCol; j++){
123094 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
123095 sqlite3ExprCodeGetColumnOfTable(v, pTab, 0, iCol, regRow+j);
123096 sqlite3VdbeAddOp2(v, OP_IsNull50, regRow+j, addrOk); VdbeCoverage(v);
123097 }
123098
123099 /* Generate code to query the parent index for a matching parent
123100 ** key. If a match is found, jump to addrOk. */
123101 if( pIdx ){
123102 sqlite3VdbeAddOp4(v, OP_MakeRecord92, regRow, pFK->nCol, regKey,
123103 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
123104 sqlite3VdbeAddOp4Int(v, OP_Found29, i, addrOk, regKey, 0);
123105 VdbeCoverage(v);
123106 }else if( pParent ){
123107 int jmp = sqlite3VdbeCurrentAddr(v)+2;
123108 sqlite3VdbeAddOp3(v, OP_SeekRowid30, i, jmp, regRow); VdbeCoverage(v);
123109 sqlite3VdbeGoto(v, addrOk);
123110 assert( pFK->nCol==1 )((void) (0));
123111 }
123112
123113 /* Generate code to report an FK violation to the caller. */
123114 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
123115 sqlite3VdbeAddOp2(v, OP_Rowid128, 0, regResult+1);
123116 }else{
123117 sqlite3VdbeAddOp2(v, OP_Null73, 0, regResult+1);
123118 }
123119 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
123120 sqlite3VdbeAddOp2(v, OP_ResultRow81, regResult, 4);
123121 sqlite3VdbeResolveLabel(v, addrOk);
123122 sqlite3DbFree(db, aiCols);
123123 }
123124 sqlite3VdbeAddOp2(v, OP_Next5, 0, addrTop+1); VdbeCoverage(v);
123125 sqlite3VdbeJumpHere(v, addrTop);
123126 }
123127 }
123128 break;
123129#endif /* !defined(SQLITE_OMIT_TRIGGER) */
123130#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
123131
123132#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
123133 /* Reinstall the LIKE and GLOB functions. The variant of LIKE
123134 ** used will be case sensitive or not depending on the RHS.
123135 */
123136 case PragTyp_CASE_SENSITIVE_LIKE6: {
123137 if( zRight ){
123138 sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
123139 }
123140 }
123141 break;
123142#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
123143
123144#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX100
123145# define SQLITE_INTEGRITY_CHECK_ERROR_MAX100 100
123146#endif
123147
123148#ifndef SQLITE_OMIT_INTEGRITY_CHECK
123149 /* PRAGMA integrity_check
123150 ** PRAGMA integrity_check(N)
123151 ** PRAGMA quick_check
123152 ** PRAGMA quick_check(N)
123153 **
123154 ** Verify the integrity of the database.
123155 **
123156 ** The "quick_check" is reduced version of
123157 ** integrity_check designed to detect most database corruption
123158 ** without the overhead of cross-checking indexes. Quick_check
123159 ** is linear time wherease integrity_check is O(NlogN).
123160 */
123161 case PragTyp_INTEGRITY_CHECK19: {
123162 int i, j, addr, mxErr;
123163
123164 int isQuick = (sqlite3Tolower(zLeft[0])(sqlite3UpperToLower[(unsigned char)(zLeft[0])])=='q');
123165
123166 /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
123167 ** then iDb is set to the index of the database identified by <db>.
123168 ** In this case, the integrity of database iDb only is verified by
123169 ** the VDBE created below.
123170 **
123171 ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
123172 ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
123173 ** to -1 here, to indicate that the VDBE should verify the integrity
123174 ** of all attached databases. */
123175 assert( iDb>=0 )((void) (0));
123176 assert( iDb==0 || pId2->z )((void) (0));
123177 if( pId2->z==0 ) iDb = -1;
123178
123179 /* Initialize the VDBE program */
123180 pParse->nMem = 6;
123181
123182 /* Set the maximum error count */
123183 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX100;
123184 if( zRight ){
123185 sqlite3GetInt32(zRight, &mxErr);
123186 if( mxErr<=0 ){
123187 mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX100;
123188 }
123189 }
123190 sqlite3VdbeAddOp2(v, OP_Integer70, mxErr-1, 1); /* reg[1] holds errors left */
123191
123192 /* Do an integrity check on each database file */
123193 for(i=0; i<db->nDb; i++){
123194 HashElem *x; /* For looping over tables in the schema */
123195 Hash *pTbls; /* Set of all tables in the schema */
123196 int *aRoot; /* Array of root page numbers of all btrees */
123197 int cnt = 0; /* Number of entries in aRoot[] */
123198 int mxIdx = 0; /* Maximum number of indexes for any table */
123199
123200 if( OMIT_TEMPDB0 && i==1 ) continue;
123201 if( iDb>=0 && i!=iDb ) continue;
123202
123203 sqlite3CodeVerifySchema(pParse, i);
123204
123205 /* Do an integrity check of the B-Tree
123206 **
123207 ** Begin by finding the root pages numbers
123208 ** for all tables and indices in the database.
123209 */
123210 assert( sqlite3SchemaMutexHeld(db, i, 0) )((void) (0));
123211 pTbls = &db->aDb[i].pSchema->tblHash;
123212 for(cnt=0, x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
123213 Table *pTab = sqliteHashData(x)((x)->data); /* Current table */
123214 Index *pIdx; /* An index on pTab */
123215 int nIdx; /* Number of indexes on pTab */
123216 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ) cnt++;
123217 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
123218 if( nIdx>mxIdx ) mxIdx = nIdx;
123219 }
123220 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(cnt+1));
123221 if( aRoot==0 ) break;
123222 for(cnt=0, x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
123223 Table *pTab = sqliteHashData(x)((x)->data);
123224 Index *pIdx;
123225 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ) aRoot[++cnt] = pTab->tnum;
123226 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
123227 aRoot[++cnt] = pIdx->tnum;
123228 }
123229 }
123230 aRoot[0] = cnt;
123231
123232 /* Make sure sufficient number of registers have been allocated */
123233 pParse->nMem = MAX( pParse->nMem, 8+mxIdx )((pParse->nMem)>(8+mxIdx)?(pParse->nMem):(8+mxIdx));
123234 sqlite3ClearTempRegCache(pParse);
123235
123236 /* Do the b-tree integrity checks */
123237 sqlite3VdbeAddOp4(v, OP_IntegrityCk147, 2, cnt, 1, (char*)aRoot,P4_INTARRAY(-15));
123238 sqlite3VdbeChangeP5(v, (u8)i);
123239 addr = sqlite3VdbeAddOp1(v, OP_IsNull50, 2); VdbeCoverage(v);
123240 sqlite3VdbeAddOp4(v, OP_String8110, 0, 3, 0,
123241 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
123242 P4_DYNAMIC(-7));
123243 sqlite3VdbeAddOp3(v, OP_Concat105, 2, 3, 3);
123244 integrityCheckResultRow(v);
123245 sqlite3VdbeJumpHere(v, addr);
123246
123247 /* Make sure all the indices are constructed correctly.
123248 */
123249 for(x=sqliteHashFirst(pTbls)((pTbls)->first); x; x=sqliteHashNext(x)((x)->next)){
123250 Table *pTab = sqliteHashData(x)((x)->data);
123251 Index *pIdx, *pPk;
123252 Index *pPrior = 0;
123253 int loopTop;
123254 int iDataCur, iIdxCur;
123255 int r1 = -1;
123256
123257 if( pTab->tnum<1 ) continue; /* Skip VIEWs or VIRTUAL TABLEs */
123258 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
123259 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead108, 0,
123260 1, 0, &iDataCur, &iIdxCur);
123261 /* reg[7] counts the number of entries in the table.
123262 ** reg[8+i] counts the number of entries in the i-th index
123263 */
123264 sqlite3VdbeAddOp2(v, OP_Integer70, 0, 7);
123265 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
123266 sqlite3VdbeAddOp2(v, OP_Integer70, 0, 8+j); /* index entries counter */
123267 }
123268 assert( pParse->nMem>=8+j )((void) (0));
123269 assert( sqlite3NoTempsInRange(pParse,1,7+j) )((void) (0));
123270 sqlite3VdbeAddOp2(v, OP_Rewind36, iDataCur, 0); VdbeCoverage(v);
123271 loopTop = sqlite3VdbeAddOp2(v, OP_AddImm83, 7, 1);
123272 if( !isQuick ){
123273 /* Sanity check on record header decoding */
123274 sqlite3VdbeAddOp3(v, OP_Column90, iDataCur, pTab->nCol-1, 3);
123275 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG0x80);
123276 }
123277 /* Verify that all NOT NULL columns really are NOT NULL */
123278 for(j=0; j<pTab->nCol; j++){
123279 char *zErr;
123280 int jmp2;
123281 if( j==pTab->iPKey ) continue;
123282 if( pTab->aCol[j].notNull==0 ) continue;
123283 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
123284 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG0x80);
123285 jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull51, 3); VdbeCoverage(v);
123286 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
123287 pTab->aCol[j].zName);
123288 sqlite3VdbeAddOp4(v, OP_String8110, 0, 3, 0, zErr, P4_DYNAMIC(-7));
123289 integrityCheckResultRow(v);
123290 sqlite3VdbeJumpHere(v, jmp2);
123291 }
123292 /* Verify CHECK constraints */
123293 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks0x00000200)==0 ){
123294 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
123295 if( db->mallocFailed==0 ){
123296 int addrCkFault = sqlite3VdbeMakeLabel(pParse);
123297 int addrCkOk = sqlite3VdbeMakeLabel(pParse);
123298 char *zErr;
123299 int k;
123300 pParse->iSelfTab = iDataCur + 1;
123301 for(k=pCheck->nExpr-1; k>0; k--){
123302 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
123303 }
123304 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
123305 SQLITE_JUMPIFNULL0x10);
123306 sqlite3VdbeResolveLabel(v, addrCkFault);
123307 pParse->iSelfTab = 0;
123308 zErr = sqlite3MPrintf(db, "CHECK constraint failed in %s",
123309 pTab->zName);
123310 sqlite3VdbeAddOp4(v, OP_String8110, 0, 3, 0, zErr, P4_DYNAMIC(-7));
123311 integrityCheckResultRow(v);
123312 sqlite3VdbeResolveLabel(v, addrCkOk);
123313 }
123314 sqlite3ExprListDelete(db, pCheck);
123315 }
123316 if( !isQuick ){ /* Omit the remaining tests for quick_check */
123317 /* Validate index entries for the current row */
123318 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
123319 int jmp2, jmp3, jmp4, jmp5;
123320 int ckUniq = sqlite3VdbeMakeLabel(pParse);
123321 if( pPk==pIdx ) continue;
123322 r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
123323 pPrior, r1);
123324 pPrior = pIdx;
123325 sqlite3VdbeAddOp2(v, OP_AddImm83, 8+j, 1);/* increment entry count */
123326 /* Verify that an index entry exists for the current table row */
123327 jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found29, iIdxCur+j, ckUniq, r1,
123328 pIdx->nColumn); VdbeCoverage(v);
123329 sqlite3VdbeLoadString(v, 3, "row ");
123330 sqlite3VdbeAddOp3(v, OP_Concat105, 7, 3, 3);
123331 sqlite3VdbeLoadString(v, 4, " missing from index ");
123332 sqlite3VdbeAddOp3(v, OP_Concat105, 4, 3, 3);
123333 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
123334 sqlite3VdbeAddOp3(v, OP_Concat105, 4, 3, 3);
123335 jmp4 = integrityCheckResultRow(v);
123336 sqlite3VdbeJumpHere(v, jmp2);
123337 /* For UNIQUE indexes, verify that only one entry exists with the
123338 ** current key. The entry is unique if (1) any column is NULL
123339 ** or (2) the next entry has a different key */
123340 if( IsUniqueIndex(pIdx)((pIdx)->onError!=0) ){
123341 int uniqOk = sqlite3VdbeMakeLabel(pParse);
123342 int jmp6;
123343 int kk;
123344 for(kk=0; kk<pIdx->nKeyCol; kk++){
123345 int iCol = pIdx->aiColumn[kk];
123346 assert( iCol!=XN_ROWID && iCol<pTab->nCol )((void) (0));
123347 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
123348 sqlite3VdbeAddOp2(v, OP_IsNull50, r1+kk, uniqOk);
123349 VdbeCoverage(v);
123350 }
123351 jmp6 = sqlite3VdbeAddOp1(v, OP_Next5, iIdxCur+j); VdbeCoverage(v);
123352 sqlite3VdbeGoto(v, uniqOk);
123353 sqlite3VdbeJumpHere(v, jmp6);
123354 sqlite3VdbeAddOp4Int(v, OP_IdxGT38, iIdxCur+j, uniqOk, r1,
123355 pIdx->nKeyCol); VdbeCoverage(v);
123356 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
123357 sqlite3VdbeGoto(v, jmp5);
123358 sqlite3VdbeResolveLabel(v, uniqOk);
123359 }
123360 sqlite3VdbeJumpHere(v, jmp4);
123361 sqlite3ResolvePartIdxLabel(pParse, jmp3);
123362 }
123363 }
123364 sqlite3VdbeAddOp2(v, OP_Next5, iDataCur, loopTop); VdbeCoverage(v);
123365 sqlite3VdbeJumpHere(v, loopTop-1);
123366#ifndef SQLITE_OMIT_BTREECOUNT
123367 if( !isQuick ){
123368 sqlite3VdbeLoadString(v, 2, "wrong # of entries in index ");
123369 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
123370 if( pPk==pIdx ) continue;
123371 sqlite3VdbeAddOp2(v, OP_Count93, iIdxCur+j, 3);
123372 addr = sqlite3VdbeAddOp3(v, OP_Eq53, 8+j, 0, 3); VdbeCoverage(v);
123373 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL0x90);
123374 sqlite3VdbeLoadString(v, 4, pIdx->zName);
123375 sqlite3VdbeAddOp3(v, OP_Concat105, 4, 2, 3);
123376 integrityCheckResultRow(v);
123377 sqlite3VdbeJumpHere(v, addr);
123378 }
123379 }
123380#endif /* SQLITE_OMIT_BTREECOUNT */
123381 }
123382 }
123383 {
123384 static const int iLn = VDBE_OFFSET_LINENO(2)0;
123385 static const VdbeOpList endCode[] = {
123386 { OP_AddImm83, 1, 0, 0}, /* 0 */
123387 { OP_IfNotZero48, 1, 4, 0}, /* 1 */
123388 { OP_String8110, 0, 3, 0}, /* 2 */
123389 { OP_ResultRow81, 3, 1, 0}, /* 3 */
123390 { OP_Halt69, 0, 0, 0}, /* 4 */
123391 { OP_String8110, 0, 3, 0}, /* 5 */
123392 { OP_Goto11, 0, 3, 0}, /* 6 */
123393 };
123394 VdbeOp *aOp;
123395
123396 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode)((int)(sizeof(endCode)/sizeof(endCode[0]))), endCode, iLn);
123397 if( aOp ){
123398 aOp[0].p2 = 1-mxErr;
123399 aOp[2].p4type = P4_STATIC(-1);
123400 aOp[2].p4.z = "ok";
123401 aOp[5].p4type = P4_STATIC(-1);
123402 aOp[5].p4.z = (char*)sqlite3ErrStr(SQLITE_CORRUPT11);
123403 }
123404 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
123405 }
123406 }
123407 break;
123408#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
123409
123410#ifndef SQLITE_OMIT_UTF16
123411 /*
123412 ** PRAGMA encoding
123413 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
123414 **
123415 ** In its first form, this pragma returns the encoding of the main
123416 ** database. If the database is not initialized, it is initialized now.
123417 **
123418 ** The second form of this pragma is a no-op if the main database file
123419 ** has not already been initialized. In this case it sets the default
123420 ** encoding that will be used for the main database file if a new file
123421 ** is created. If an existing main database file is opened, then the
123422 ** default text encoding for the existing database is used.
123423 **
123424 ** In all cases new databases created using the ATTACH command are
123425 ** created to use the same default text encoding as the main database. If
123426 ** the main database has not been initialized and/or created when ATTACH
123427 ** is executed, this is done before the ATTACH operation.
123428 **
123429 ** In the second form this pragma sets the text encoding to be used in
123430 ** new database files created using this database handle. It is only
123431 ** useful if invoked immediately after the main database i
123432 */
123433 case PragTyp_ENCODING12: {
123434 static const struct EncName {
123435 char *zName;
123436 u8 enc;
123437 } encnames[] = {
123438 { "UTF8", SQLITE_UTF81 },
123439 { "UTF-8", SQLITE_UTF81 }, /* Must be element [1] */
123440 { "UTF-16le", SQLITE_UTF16LE2 }, /* Must be element [2] */
123441 { "UTF-16be", SQLITE_UTF16BE3 }, /* Must be element [3] */
123442 { "UTF16le", SQLITE_UTF16LE2 },
123443 { "UTF16be", SQLITE_UTF16BE3 },
123444 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
123445 { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */
123446 { 0, 0 }
123447 };
123448 const struct EncName *pEnc;
123449 if( !zRight ){ /* "PRAGMA encoding" */
123450 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
123451 assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 )((void) (0));
123452 assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE )((void) (0));
123453 assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE )((void) (0));
123454 returnSingleText(v, encnames[ENC(pParse->db)((pParse->db)->enc)].zName);
123455 }else{ /* "PRAGMA encoding = XXX" */
123456 /* Only change the value of sqlite.enc if the database handle is not
123457 ** initialized. If the main database exists, the new sqlite.enc value
123458 ** will be overwritten when the schema is next loaded. If it does not
123459 ** already exists, it will be created to use the new encoding value.
123460 */
123461 if(
123462 !(DbHasProperty(db, 0, DB_SchemaLoaded)(((db)->aDb[0].pSchema->schemaFlags&(0x0001))==(0x0001
))
) ||
123463 DbHasProperty(db, 0, DB_Empty)(((db)->aDb[0].pSchema->schemaFlags&(0x0004))==(0x0004
))
123464 ){
123465 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
123466 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
123467 SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc) = ENC(db)((db)->enc) =
123468 pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE2;
123469 break;
123470 }
123471 }
123472 if( !pEnc->zName ){
123473 sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
123474 }
123475 }
123476 }
123477 }
123478 break;
123479#endif /* SQLITE_OMIT_UTF16 */
123480
123481#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
123482 /*
123483 ** PRAGMA [schema.]schema_version
123484 ** PRAGMA [schema.]schema_version = <integer>
123485 **
123486 ** PRAGMA [schema.]user_version
123487 ** PRAGMA [schema.]user_version = <integer>
123488 **
123489 ** PRAGMA [schema.]freelist_count
123490 **
123491 ** PRAGMA [schema.]data_version
123492 **
123493 ** PRAGMA [schema.]application_id
123494 ** PRAGMA [schema.]application_id = <integer>
123495 **
123496 ** The pragma's schema_version and user_version are used to set or get
123497 ** the value of the schema-version and user-version, respectively. Both
123498 ** the schema-version and the user-version are 32-bit signed integers
123499 ** stored in the database header.
123500 **
123501 ** The schema-cookie is usually only manipulated internally by SQLite. It
123502 ** is incremented by SQLite whenever the database schema is modified (by
123503 ** creating or dropping a table or index). The schema version is used by
123504 ** SQLite each time a query is executed to ensure that the internal cache
123505 ** of the schema used when compiling the SQL query matches the schema of
123506 ** the database against which the compiled query is actually executed.
123507 ** Subverting this mechanism by using "PRAGMA schema_version" to modify
123508 ** the schema-version is potentially dangerous and may lead to program
123509 ** crashes or database corruption. Use with caution!
123510 **
123511 ** The user-version is not used internally by SQLite. It may be used by
123512 ** applications for any purpose.
123513 */
123514 case PragTyp_HEADER_VALUE0: {
123515 int iCookie = pPragma->iArg; /* Which cookie to read or write */
123516 sqlite3VdbeUsesBtree(v, iDb);
123517 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly0x08)==0 ){
123518 /* Write the specified cookie value */
123519 static const VdbeOpList setCookie[] = {
123520 { OP_Transaction2, 0, 1, 0}, /* 0 */
123521 { OP_SetCookie95, 0, 0, 0}, /* 1 */
123522 };
123523 VdbeOp *aOp;
123524 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
123525 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie)((int)(sizeof(setCookie)/sizeof(setCookie[0]))), setCookie, 0);
123526 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
123527 aOp[0].p1 = iDb;
123528 aOp[1].p1 = iDb;
123529 aOp[1].p2 = iCookie;
123530 aOp[1].p3 = sqlite3Atoi(zRight);
123531 }else{
123532 /* Read the specified cookie value */
123533 static const VdbeOpList readCookie[] = {
123534 { OP_Transaction2, 0, 0, 0}, /* 0 */
123535 { OP_ReadCookie94, 0, 1, 0}, /* 1 */
123536 { OP_ResultRow81, 1, 1, 0}
123537 };
123538 VdbeOp *aOp;
123539 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(readCookie));
123540 aOp = sqlite3VdbeAddOpList(v, ArraySize(readCookie)((int)(sizeof(readCookie)/sizeof(readCookie[0]))),readCookie,0);
123541 if( ONLY_IF_REALLOC_STRESS(aOp==0)(0) ) break;
123542 aOp[0].p1 = iDb;
123543 aOp[1].p1 = iDb;
123544 aOp[1].p3 = iCookie;
123545 sqlite3VdbeReusable(v);
123546 }
123547 }
123548 break;
123549#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
123550
123551#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
123552 /*
123553 ** PRAGMA compile_options
123554 **
123555 ** Return the names of all compile-time options used in this build,
123556 ** one option per row.
123557 */
123558 case PragTyp_COMPILE_OPTIONS8: {
123559 int i = 0;
123560 const char *zOpt;
123561 pParse->nMem = 1;
123562 while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
123563 sqlite3VdbeLoadString(v, 1, zOpt);
123564 sqlite3VdbeAddOp2(v, OP_ResultRow81, 1, 1);
123565 }
123566 sqlite3VdbeReusable(v);
123567 }
123568 break;
123569#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
123570
123571#ifndef SQLITE_OMIT_WAL
123572 /*
123573 ** PRAGMA [schema.]wal_checkpoint = passive|full|restart|truncate
123574 **
123575 ** Checkpoint the database.
123576 */
123577 case PragTyp_WAL_CHECKPOINT39: {
123578 int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED10);
123579 int eMode = SQLITE_CHECKPOINT_PASSIVE0;
123580 if( zRight ){
123581 if( sqlite3StrICmp(zRight, "full")==0 ){
123582 eMode = SQLITE_CHECKPOINT_FULL1;
123583 }else if( sqlite3StrICmp(zRight, "restart")==0 ){
123584 eMode = SQLITE_CHECKPOINT_RESTART2;
123585 }else if( sqlite3StrICmp(zRight, "truncate")==0 ){
123586 eMode = SQLITE_CHECKPOINT_TRUNCATE3;
123587 }
123588 }
123589 pParse->nMem = 3;
123590 sqlite3VdbeAddOp3(v, OP_Checkpoint6, iBt, eMode, 1);
123591 sqlite3VdbeAddOp2(v, OP_ResultRow81, 1, 3);
123592 }
123593 break;
123594
123595 /*
123596 ** PRAGMA wal_autocheckpoint
123597 ** PRAGMA wal_autocheckpoint = N
123598 **
123599 ** Configure a database connection to automatically checkpoint a database
123600 ** after accumulating N frames in the log. Or query for the current value
123601 ** of N.
123602 */
123603 case PragTyp_WAL_AUTOCHECKPOINT38: {
123604 if( zRight ){
123605 sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
123606 }
123607 returnSingleInt(v,
123608 db->xWalCallback==sqlite3WalDefaultHook ?
123609 SQLITE_PTR_TO_INT(db->pWalArg)((int)(long int)(db->pWalArg)) : 0);
123610 }
123611 break;
123612#endif
123613
123614 /*
123615 ** PRAGMA shrink_memory
123616 **
123617 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
123618 ** connection on which it is invoked to free up as much memory as it
123619 ** can, by calling sqlite3_db_release_memory().
123620 */
123621 case PragTyp_SHRINK_MEMORY31: {
123622 sqlite3_db_release_memory(db);
123623 break;
123624 }
123625
123626 /*
123627 ** PRAGMA optimize
123628 ** PRAGMA optimize(MASK)
123629 ** PRAGMA schema.optimize
123630 ** PRAGMA schema.optimize(MASK)
123631 **
123632 ** Attempt to optimize the database. All schemas are optimized in the first
123633 ** two forms, and only the specified schema is optimized in the latter two.
123634 **
123635 ** The details of optimizations performed by this pragma are expected
123636 ** to change and improve over time. Applications should anticipate that
123637 ** this pragma will perform new optimizations in future releases.
123638 **
123639 ** The optional argument is a bitmask of optimizations to perform:
123640 **
123641 ** 0x0001 Debugging mode. Do not actually perform any optimizations
123642 ** but instead return one line of text for each optimization
123643 ** that would have been done. Off by default.
123644 **
123645 ** 0x0002 Run ANALYZE on tables that might benefit. On by default.
123646 ** See below for additional information.
123647 **
123648 ** 0x0004 (Not yet implemented) Record usage and performance
123649 ** information from the current session in the
123650 ** database file so that it will be available to "optimize"
123651 ** pragmas run by future database connections.
123652 **
123653 ** 0x0008 (Not yet implemented) Create indexes that might have
123654 ** been helpful to recent queries
123655 **
123656 ** The default MASK is and always shall be 0xfffe. 0xfffe means perform all
123657 ** of the optimizations listed above except Debug Mode, including new
123658 ** optimizations that have not yet been invented. If new optimizations are
123659 ** ever added that should be off by default, those off-by-default
123660 ** optimizations will have bitmasks of 0x10000 or larger.
123661 **
123662 ** DETERMINATION OF WHEN TO RUN ANALYZE
123663 **
123664 ** In the current implementation, a table is analyzed if only if all of
123665 ** the following are true:
123666 **
123667 ** (1) MASK bit 0x02 is set.
123668 **
123669 ** (2) The query planner used sqlite_stat1-style statistics for one or
123670 ** more indexes of the table at some point during the lifetime of
123671 ** the current connection.
123672 **
123673 ** (3) One or more indexes of the table are currently unanalyzed OR
123674 ** the number of rows in the table has increased by 25 times or more
123675 ** since the last time ANALYZE was run.
123676 **
123677 ** The rules for when tables are analyzed are likely to change in
123678 ** future releases.
123679 */
123680 case PragTyp_OPTIMIZE27: {
123681 int iDbLast; /* Loop termination point for the schema loop */
123682 int iTabCur; /* Cursor for a table whose size needs checking */
123683 HashElem *k; /* Loop over tables of a schema */
123684 Schema *pSchema; /* The current schema */
123685 Table *pTab; /* A table in the schema */
123686 Index *pIdx; /* An index of the table */
123687 LogEst szThreshold; /* Size threshold above which reanalysis is needd */
123688 char *zSubSql; /* SQL statement for the OP_SqlExec opcode */
123689 u32 opMask; /* Mask of operations to perform */
123690
123691 if( zRight ){
123692 opMask = (u32)sqlite3Atoi(zRight);
123693 if( (opMask & 0x02)==0 ) break;
123694 }else{
123695 opMask = 0xfffe;
123696 }
123697 iTabCur = pParse->nTab++;
123698 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
123699 if( iDb==1 ) continue;
123700 sqlite3CodeVerifySchema(pParse, iDb);
123701 pSchema = db->aDb[iDb].pSchema;
123702 for(k=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); k; k=sqliteHashNext(k)((k)->next)){
123703 pTab = (Table*)sqliteHashData(k)((k)->data);
123704
123705 /* If table pTab has not been used in a way that would benefit from
123706 ** having analysis statistics during the current session, then skip it.
123707 ** This also has the effect of skipping virtual tables and views */
123708 if( (pTab->tabFlags & TF_StatsUsed0x0100)==0 ) continue;
123709
123710 /* Reanalyze if the table is 25 times larger than the last analysis */
123711 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 )((void) (0));
123712 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
123713 if( !pIdx->hasStat1 ){
123714 szThreshold = 0; /* Always analyze if any index lacks statistics */
123715 break;
123716 }
123717 }
123718 if( szThreshold ){
123719 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead108);
123720 sqlite3VdbeAddOp3(v, OP_IfSmaller33, iTabCur,
123721 sqlite3VdbeCurrentAddr(v)+2+(opMask&1), szThreshold);
123722 VdbeCoverage(v);
123723 }
123724 zSubSql = sqlite3MPrintf(db, "ANALYZE \"%w\".\"%w\"",
123725 db->aDb[iDb].zDbSName, pTab->zName);
123726 if( opMask & 0x01 ){
123727 int r1 = sqlite3GetTempReg(pParse);
123728 sqlite3VdbeAddOp4(v, OP_String8110, 0, r1, 0, zSubSql, P4_DYNAMIC(-7));
123729 sqlite3VdbeAddOp2(v, OP_ResultRow81, r1, 1);
123730 }else{
123731 sqlite3VdbeAddOp4(v, OP_SqlExec140, 0, 0, 0, zSubSql, P4_DYNAMIC(-7));
123732 }
123733 }
123734 }
123735 sqlite3VdbeAddOp0(v, OP_Expire158);
123736 break;
123737 }
123738
123739 /*
123740 ** PRAGMA busy_timeout
123741 ** PRAGMA busy_timeout = N
123742 **
123743 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
123744 ** if one is set. If no busy handler or a different busy handler is set
123745 ** then 0 is returned. Setting the busy_timeout to 0 or negative
123746 ** disables the timeout.
123747 */
123748 /*case PragTyp_BUSY_TIMEOUT*/ default: {
123749 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT )((void) (0));
123750 if( zRight ){
123751 sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
123752 }
123753 returnSingleInt(v, db->busyTimeout);
123754 break;
123755 }
123756
123757 /*
123758 ** PRAGMA soft_heap_limit
123759 ** PRAGMA soft_heap_limit = N
123760 **
123761 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
123762 ** sqlite3_soft_heap_limit64() interface with the argument N, if N is
123763 ** specified and is a non-negative integer.
123764 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
123765 ** returns the same integer that would be returned by the
123766 ** sqlite3_soft_heap_limit64(-1) C-language function.
123767 */
123768 case PragTyp_SOFT_HEAP_LIMIT32: {
123769 sqlite3_int64 N;
123770 if( zRight && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0 ){
123771 sqlite3_soft_heap_limit64(N);
123772 }
123773 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
123774 break;
123775 }
123776
123777 /*
123778 ** PRAGMA threads
123779 ** PRAGMA threads = N
123780 **
123781 ** Configure the maximum number of worker threads. Return the new
123782 ** maximum, which might be less than requested.
123783 */
123784 case PragTyp_THREADS37: {
123785 sqlite3_int64 N;
123786 if( zRight
123787 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK0
123788 && N>=0
123789 ){
123790 sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS11, (int)(N&0x7fffffff));
123791 }
123792 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS11, -1));
123793 break;
123794 }
123795
123796#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
123797 /*
123798 ** Report the current state of file logs for all databases
123799 */
123800 case PragTyp_LOCK_STATUS42: {
123801 static const char *const azLockName[] = {
123802 "unlocked", "shared", "reserved", "pending", "exclusive"
123803 };
123804 int i;
123805 pParse->nMem = 2;
123806 for(i=0; i<db->nDb; i++){
123807 Btree *pBt;
123808 const char *zState = "unknown";
123809 int j;
123810 if( db->aDb[i].zDbSName==0 ) continue;
123811 pBt = db->aDb[i].pBt;
123812 if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
123813 zState = "closed";
123814 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
123815 SQLITE_FCNTL_LOCKSTATE1, &j)==SQLITE_OK0 ){
123816 zState = azLockName[j];
123817 }
123818 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
123819 }
123820 break;
123821 }
123822#endif
123823
123824#ifdef SQLITE_HAS_CODEC
123825 /* Pragma iArg
123826 ** ---------- ------
123827 ** key 0
123828 ** rekey 1
123829 ** hexkey 2
123830 ** hexrekey 3
123831 ** textkey 4
123832 ** textrekey 5
123833 */
123834 case PragTyp_KEY41: {
123835 if( zRight ){
123836 char zBuf[40];
123837 const char *zKey = zRight;
123838 int n;
123839 if( pPragma->iArg==2 || pPragma->iArg==3 ){
123840 u8 iByte;
123841 int i;
123842 for(i=0, iByte=0; i<sizeof(zBuf)*2 && sqlite3Isxdigit(zRight[i])(sqlite3CtypeMap[(unsigned char)(zRight[i])]&0x08); i++){
123843 iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
123844 if( (i&1)!=0 ) zBuf[i/2] = iByte;
123845 }
123846 zKey = zBuf;
123847 n = i/2;
123848 }else{
123849 n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1;
123850 }
123851 if( (pPragma->iArg & 1)==0 ){
123852 rc = sqlite3_key_v2(db, zDb, zKey, n);
123853 }else{
123854 rc = sqlite3_rekey_v2(db, zDb, zKey, n);
123855 }
123856 if( rc==SQLITE_OK0 && n!=0 ){
123857 sqlite3VdbeSetNumCols(v, 1);
123858 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, "ok", SQLITE_STATIC((sqlite3_destructor_type)0));
123859 returnSingleText(v, "ok");
123860 }
123861 }
123862 break;
123863 }
123864#endif
123865#if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
123866 case PragTyp_ACTIVATE_EXTENSIONS40: if( zRight ){
123867#ifdef SQLITE_HAS_CODEC
123868 if( sqlite3StrNICmpsqlite3_strnicmp(zRight, "see-", 4)==0 ){
123869 sqlite3_activate_see(&zRight[4]);
123870 }
123871#endif
123872#ifdef SQLITE_ENABLE_CEROD
123873 if( sqlite3StrNICmpsqlite3_strnicmp(zRight, "cerod-", 6)==0 ){
123874 sqlite3_activate_cerod(&zRight[6]);
123875 }
123876#endif
123877 }
123878 break;
123879#endif
123880
123881 } /* End of the PRAGMA switch */
123882
123883 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
123884 ** purpose is to execute assert() statements to verify that if the
123885 ** PragFlg_NoColumns1 flag is set and the caller specified an argument
123886 ** to the PRAGMA, the implementation has not added any OP_ResultRow
123887 ** instructions to the VM. */
123888 if( (pPragma->mPragFlg & PragFlg_NoColumns10x04) && zRight ){
123889 sqlite3VdbeVerifyNoResultRow(v);
123890 }
123891
123892pragma_out:
123893 sqlite3DbFree(db, zLeft);
123894 sqlite3DbFree(db, zRight);
123895}
123896#ifndef SQLITE_OMIT_VIRTUALTABLE
123897/*****************************************************************************
123898** Implementation of an eponymous virtual table that runs a pragma.
123899**
123900*/
123901typedef struct PragmaVtab PragmaVtab;
123902typedef struct PragmaVtabCursor PragmaVtabCursor;
123903struct PragmaVtab {
123904 sqlite3_vtab base; /* Base class. Must be first */
123905 sqlite3 *db; /* The database connection to which it belongs */
123906 const PragmaName *pName; /* Name of the pragma */
123907 u8 nHidden; /* Number of hidden columns */
123908 u8 iHidden; /* Index of the first hidden column */
123909};
123910struct PragmaVtabCursor {
123911 sqlite3_vtab_cursor base; /* Base class. Must be first */
123912 sqlite3_stmt *pPragma; /* The pragma statement to run */
123913 sqlite_int64 iRowid; /* Current rowid */
123914 char *azArg[2]; /* Value of the argument and schema */
123915};
123916
123917/*
123918** Pragma virtual table module xConnect method.
123919*/
123920static int pragmaVtabConnect(
123921 sqlite3 *db,
123922 void *pAux,
123923 int argc, const char *const*argv,
123924 sqlite3_vtab **ppVtab,
123925 char **pzErr
123926){
123927 const PragmaName *pPragma = (const PragmaName*)pAux;
123928 PragmaVtab *pTab = 0;
123929 int rc;
123930 int i, j;
123931 char cSep = '(';
123932 StrAccum acc;
123933 char zBuf[200];
123934
123935 UNUSED_PARAMETER(argc)(void)(argc);
123936 UNUSED_PARAMETER(argv)(void)(argv);
123937 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
123938 sqlite3_str_appendall(&acc, "CREATE TABLE x");
123939 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
123940 sqlite3_str_appendf(&acc, "%c\"%s\"", cSep, pragCName[j]);
123941 cSep = ',';
123942 }
123943 if( i==0 ){
123944 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
123945 i++;
123946 }
123947 j = 0;
123948 if( pPragma->mPragFlg & PragFlg_Result10x20 ){
123949 sqlite3_str_appendall(&acc, ",arg HIDDEN");
123950 j++;
123951 }
123952 if( pPragma->mPragFlg & (PragFlg_SchemaOpt0x40|PragFlg_SchemaReq0x80) ){
123953 sqlite3_str_appendall(&acc, ",schema HIDDEN");
123954 j++;
123955 }
123956 sqlite3_str_append(&acc, ")", 1);
123957 sqlite3StrAccumFinish(&acc);
123958 assert( strlen(zBuf) < sizeof(zBuf)-1 )((void) (0));
123959 rc = sqlite3_declare_vtab(db, zBuf);
123960 if( rc==SQLITE_OK0 ){
123961 pTab = (PragmaVtab*)sqlite3_malloc(sizeof(PragmaVtab));
123962 if( pTab==0 ){
123963 rc = SQLITE_NOMEM7;
123964 }else{
123965 memset(pTab, 0, sizeof(PragmaVtab));
123966 pTab->pName = pPragma;
123967 pTab->db = db;
123968 pTab->iHidden = i;
123969 pTab->nHidden = j;
123970 }
123971 }else{
123972 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
123973 }
123974
123975 *ppVtab = (sqlite3_vtab*)pTab;
123976 return rc;
123977}
123978
123979/*
123980** Pragma virtual table module xDisconnect method.
123981*/
123982static int pragmaVtabDisconnect(sqlite3_vtab *pVtab){
123983 PragmaVtab *pTab = (PragmaVtab*)pVtab;
123984 sqlite3_free(pTab);
123985 return SQLITE_OK0;
123986}
123987
123988/* Figure out the best index to use to search a pragma virtual table.
123989**
123990** There are not really any index choices. But we want to encourage the
123991** query planner to give == constraints on as many hidden parameters as
123992** possible, and especially on the first hidden parameter. So return a
123993** high cost if hidden parameters are unconstrained.
123994*/
123995static int pragmaVtabBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
123996 PragmaVtab *pTab = (PragmaVtab*)tab;
123997 const struct sqlite3_index_constraint *pConstraint;
123998 int i, j;
123999 int seen[2];
124000
124001 pIdxInfo->estimatedCost = (double)1;
124002 if( pTab->nHidden==0 ){ return SQLITE_OK0; }
124003 pConstraint = pIdxInfo->aConstraint;
124004 seen[0] = 0;
124005 seen[1] = 0;
124006 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
124007 if( pConstraint->usable==0 ) continue;
124008 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
124009 if( pConstraint->iColumn < pTab->iHidden ) continue;
124010 j = pConstraint->iColumn - pTab->iHidden;
124011 assert( j < 2 )((void) (0));
124012 seen[j] = i+1;
124013 }
124014 if( seen[0]==0 ){
124015 pIdxInfo->estimatedCost = (double)2147483647;
124016 pIdxInfo->estimatedRows = 2147483647;
124017 return SQLITE_OK0;
124018 }
124019 j = seen[0]-1;
124020 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
124021 pIdxInfo->aConstraintUsage[j].omit = 1;
124022 if( seen[1]==0 ) return SQLITE_OK0;
124023 pIdxInfo->estimatedCost = (double)20;
124024 pIdxInfo->estimatedRows = 20;
124025 j = seen[1]-1;
124026 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
124027 pIdxInfo->aConstraintUsage[j].omit = 1;
124028 return SQLITE_OK0;
124029}
124030
124031/* Create a new cursor for the pragma virtual table */
124032static int pragmaVtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
124033 PragmaVtabCursor *pCsr;
124034 pCsr = (PragmaVtabCursor*)sqlite3_malloc(sizeof(*pCsr));
124035 if( pCsr==0 ) return SQLITE_NOMEM7;
124036 memset(pCsr, 0, sizeof(PragmaVtabCursor));
124037 pCsr->base.pVtab = pVtab;
124038 *ppCursor = &pCsr->base;
124039 return SQLITE_OK0;
124040}
124041
124042/* Clear all content from pragma virtual table cursor. */
124043static void pragmaVtabCursorClear(PragmaVtabCursor *pCsr){
124044 int i;
124045 sqlite3_finalize(pCsr->pPragma);
124046 pCsr->pPragma = 0;
124047 for(i=0; i<ArraySize(pCsr->azArg)((int)(sizeof(pCsr->azArg)/sizeof(pCsr->azArg[0]))); i++){
124048 sqlite3_free(pCsr->azArg[i]);
124049 pCsr->azArg[i] = 0;
124050 }
124051}
124052
124053/* Close a pragma virtual table cursor */
124054static int pragmaVtabClose(sqlite3_vtab_cursor *cur){
124055 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)cur;
124056 pragmaVtabCursorClear(pCsr);
124057 sqlite3_free(pCsr);
124058 return SQLITE_OK0;
124059}
124060
124061/* Advance the pragma virtual table cursor to the next row */
124062static int pragmaVtabNext(sqlite3_vtab_cursor *pVtabCursor){
124063 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
124064 int rc = SQLITE_OK0;
124065
124066 /* Increment the xRowid value */
124067 pCsr->iRowid++;
124068 assert( pCsr->pPragma )((void) (0));
124069 if( SQLITE_ROW100!=sqlite3_step(pCsr->pPragma) ){
124070 rc = sqlite3_finalize(pCsr->pPragma);
124071 pCsr->pPragma = 0;
124072 pragmaVtabCursorClear(pCsr);
124073 }
124074 return rc;
124075}
124076
124077/*
124078** Pragma virtual table module xFilter method.
124079*/
124080static int pragmaVtabFilter(
124081 sqlite3_vtab_cursor *pVtabCursor,
124082 int idxNum, const char *idxStr,
124083 int argc, sqlite3_value **argv
124084){
124085 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
124086 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
124087 int rc;
124088 int i, j;
124089 StrAccum acc;
124090 char *zSql;
124091
124092 UNUSED_PARAMETER(idxNum)(void)(idxNum);
124093 UNUSED_PARAMETER(idxStr)(void)(idxStr);
124094 pragmaVtabCursorClear(pCsr);
124095 j = (pTab->pName->mPragFlg & PragFlg_Result10x20)!=0 ? 0 : 1;
124096 for(i=0; i<argc; i++, j++){
124097 const char *zText = (const char*)sqlite3_value_text(argv[i]);
124098 assert( j<ArraySize(pCsr->azArg) )((void) (0));
124099 assert( pCsr->azArg[j]==0 )((void) (0));
124100 if( zText ){
124101 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
124102 if( pCsr->azArg[j]==0 ){
124103 return SQLITE_NOMEM7;
124104 }
124105 }
124106 }
124107 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH1]);
124108 sqlite3_str_appendall(&acc, "PRAGMA ");
124109 if( pCsr->azArg[1] ){
124110 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
124111 }
124112 sqlite3_str_appendall(&acc, pTab->pName->zName);
124113 if( pCsr->azArg[0] ){
124114 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
124115 }
124116 zSql = sqlite3StrAccumFinish(&acc);
124117 if( zSql==0 ) return SQLITE_NOMEM7;
124118 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
124119 sqlite3_free(zSql);
124120 if( rc!=SQLITE_OK0 ){
124121 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
124122 return rc;
124123 }
124124 return pragmaVtabNext(pVtabCursor);
124125}
124126
124127/*
124128** Pragma virtual table module xEof method.
124129*/
124130static int pragmaVtabEof(sqlite3_vtab_cursor *pVtabCursor){
124131 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
124132 return (pCsr->pPragma==0);
124133}
124134
124135/* The xColumn method simply returns the corresponding column from
124136** the PRAGMA.
124137*/
124138static int pragmaVtabColumn(
124139 sqlite3_vtab_cursor *pVtabCursor,
124140 sqlite3_context *ctx,
124141 int i
124142){
124143 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
124144 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
124145 if( i<pTab->iHidden ){
124146 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
124147 }else{
124148 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
124149 }
124150 return SQLITE_OK0;
124151}
124152
124153/*
124154** Pragma virtual table module xRowid method.
124155*/
124156static int pragmaVtabRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *p){
124157 PragmaVtabCursor *pCsr = (PragmaVtabCursor*)pVtabCursor;
124158 *p = pCsr->iRowid;
124159 return SQLITE_OK0;
124160}
124161
124162/* The pragma virtual table object */
124163static const sqlite3_module pragmaVtabModule = {
124164 0, /* iVersion */
124165 0, /* xCreate - create a table */
124166 pragmaVtabConnect, /* xConnect - connect to an existing table */
124167 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
124168 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
124169 0, /* xDestroy - Drop a table */
124170 pragmaVtabOpen, /* xOpen - open a cursor */
124171 pragmaVtabClose, /* xClose - close a cursor */
124172 pragmaVtabFilter, /* xFilter - configure scan constraints */
124173 pragmaVtabNext, /* xNext - advance a cursor */
124174 pragmaVtabEof, /* xEof */
124175 pragmaVtabColumn, /* xColumn - read data */
124176 pragmaVtabRowid, /* xRowid - read data */
124177 0, /* xUpdate - write data */
124178 0, /* xBegin - begin transaction */
124179 0, /* xSync - sync transaction */
124180 0, /* xCommit - commit transaction */
124181 0, /* xRollback - rollback transaction */
124182 0, /* xFindFunction - function overloading */
124183 0, /* xRename - rename the table */
124184 0, /* xSavepoint */
124185 0, /* xRelease */
124186 0, /* xRollbackTo */
124187 0 /* xShadowName */
124188};
124189
124190/*
124191** Check to see if zTabName is really the name of a pragma. If it is,
124192** then register an eponymous virtual table for that pragma and return
124193** a pointer to the Module object for the new virtual table.
124194*/
124195SQLITE_PRIVATEstatic Module *sqlite3PragmaVtabRegister(sqlite3 *db, const char *zName){
124196 const PragmaName *pName;
124197 assert( sqlite3_strnicmp(zName, "pragma_", 7)==0 )((void) (0));
124198 pName = pragmaLocate(zName+7);
124199 if( pName==0 ) return 0;
124200 if( (pName->mPragFlg & (PragFlg_Result00x10|PragFlg_Result10x20))==0 ) return 0;
124201 assert( sqlite3HashFind(&db->aModule, zName)==0 )((void) (0));
124202 return sqlite3VtabCreateModule(db, zName, &pragmaVtabModule, (void*)pName, 0);
124203}
124204
124205#endif /* SQLITE_OMIT_VIRTUALTABLE */
124206
124207#endif /* SQLITE_OMIT_PRAGMA */
124208
124209/************** End of pragma.c **********************************************/
124210/************** Begin file prepare.c *****************************************/
124211/*
124212** 2005 May 25
124213**
124214** The author disclaims copyright to this source code. In place of
124215** a legal notice, here is a blessing:
124216**
124217** May you do good and not evil.
124218** May you find forgiveness for yourself and forgive others.
124219** May you share freely, never taking more than you give.
124220**
124221*************************************************************************
124222** This file contains the implementation of the sqlite3_prepare()
124223** interface, and routines that contribute to loading the database schema
124224** from disk.
124225*/
124226/* #include "sqliteInt.h" */
124227
124228/*
124229** Fill the InitData structure with an error message that indicates
124230** that the database is corrupt.
124231*/
124232static void corruptSchema(
124233 InitData *pData, /* Initialization context */
124234 const char *zObj, /* Object being parsed at the point of error */
124235 const char *zExtra /* Error information */
124236){
124237 sqlite3 *db = pData->db;
124238 if( db->mallocFailed ){
124239 pData->rc = SQLITE_NOMEM_BKPT7;
124240 }else if( pData->pzErrMsg[0]!=0 ){
124241 /* A error message has already been generated. Do not overwrite it */
124242 }else if( pData->mInitFlags & INITFLAG_AlterTable0x0001 ){
124243 *pData->pzErrMsg = sqlite3DbStrDup(db, zExtra);
124244 pData->rc = SQLITE_ERROR1;
124245 }else if( db->flags & SQLITE_WriteSchema0x00000001 ){
124246 pData->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(124246);
124247 }else{
124248 char *z;
124249 if( zObj==0 ) zObj = "?";
124250 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
124251 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
124252 *pData->pzErrMsg = z;
124253 pData->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(124253);
124254 }
124255}
124256
124257/*
124258** Check to see if any sibling index (another index on the same table)
124259** of pIndex has the same root page number, and if it does, return true.
124260** This would indicate a corrupt schema.
124261*/
124262SQLITE_PRIVATEstatic int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
124263 Index *p;
124264 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
124265 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
124266 }
124267 return 0;
124268}
124269
124270/*
124271** This is the callback routine for the code that initializes the
124272** database. See sqlite3Init() below for additional information.
124273** This routine is also called from the OP_ParseSchema opcode of the VDBE.
124274**
124275** Each callback contains the following information:
124276**
124277** argv[0] = name of thing being created
124278** argv[1] = root page number for table or index. 0 for trigger or view.
124279** argv[2] = SQL text for the CREATE statement.
124280**
124281*/
124282SQLITE_PRIVATEstatic int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
124283 InitData *pData = (InitData*)pInit;
124284 sqlite3 *db = pData->db;
124285 int iDb = pData->iDb;
124286
124287 assert( argc==3 )((void) (0));
124288 UNUSED_PARAMETER2(NotUsed, argc)(void)(NotUsed),(void)(argc);
124289 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
124290 DbClearProperty(db, iDb, DB_Empty)(db)->aDb[iDb].pSchema->schemaFlags&=~(0x0004);
124291 pData->nInitRow++;
124292 if( db->mallocFailed ){
124293 corruptSchema(pData, argv[0], 0);
124294 return 1;
124295 }
124296
124297 assert( iDb>=0 && iDb<db->nDb )((void) (0));
124298 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
124299 if( argv[1]==0 ){
124300 corruptSchema(pData, argv[0], 0);
124301 }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
124302 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
124303 ** But because db->init.busy is set to 1, no VDBE code is generated
124304 ** or executed. All the parser does is build the internal data
124305 ** structures that describe the table, index, or view.
124306 */
124307 int rc;
124308 u8 saved_iDb = db->init.iDb;
124309 sqlite3_stmt *pStmt;
124310 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
124311
124312 assert( db->init.busy )((void) (0));
124313 db->init.iDb = iDb;
124314 db->init.newTnum = sqlite3Atoi(argv[1]);
124315 db->init.orphanTrigger = 0;
124316 TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
124317 rc = db->errCode;
124318 assert( (rc&0xFF)==(rcp&0xFF) )((void) (0));
124319 db->init.iDb = saved_iDb;
124320 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
124321 if( SQLITE_OK0!=rc ){
124322 if( db->init.orphanTrigger ){
124323 assert( iDb==1 )((void) (0));
124324 }else{
124325 pData->rc = rc;
124326 if( rc==SQLITE_NOMEM7 ){
124327 sqlite3OomFault(db);
124328 }else if( rc!=SQLITE_INTERRUPT9 && (rc&0xFF)!=SQLITE_LOCKED6 ){
124329 corruptSchema(pData, argv[0], sqlite3_errmsg(db));
124330 }
124331 }
124332 }
124333 sqlite3_finalize(pStmt);
124334 }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
124335 corruptSchema(pData, argv[0], 0);
124336 }else{
124337 /* If the SQL column is blank it means this is an index that
124338 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
124339 ** constraint for a CREATE TABLE. The index should have already
124340 ** been created when we processed the CREATE TABLE. All we have
124341 ** to do here is record the root page number for that index.
124342 */
124343 Index *pIndex;
124344 pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
124345 if( pIndex==0
124346 || sqlite3GetInt32(argv[1],&pIndex->tnum)==0
124347 || pIndex->tnum<2
124348 || sqlite3IndexHasDuplicateRootPage(pIndex)
124349 ){
124350 corruptSchema(pData, argv[0], pIndex?"invalid rootpage":"orphan index");
124351 }
124352 }
124353 return 0;
124354}
124355
124356/*
124357** Attempt to read the database schema and initialize internal
124358** data structures for a single database file. The index of the
124359** database file is given by iDb. iDb==0 is used for the main
124360** database. iDb==1 should never be used. iDb>=2 is used for
124361** auxiliary databases. Return one of the SQLITE_ error codes to
124362** indicate success or failure.
124363*/
124364SQLITE_PRIVATEstatic int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
124365 int rc;
124366 int i;
124367#ifndef SQLITE_OMIT_DEPRECATED
124368 int size;
124369#endif
124370 Db *pDb;
124371 char const *azArg[4];
124372 int meta[5];
124373 InitData initData;
124374 const char *zMasterName;
124375 int openedTransaction = 0;
124376
124377 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 )((void) (0));
124378 assert( iDb>=0 && iDb<db->nDb )((void) (0));
124379 assert( db->aDb[iDb].pSchema )((void) (0));
124380 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
124381 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) )((void) (0));
124382
124383 db->init.busy = 1;
124384
124385 /* Construct the in-memory representation schema tables (sqlite_master or
124386 ** sqlite_temp_master) by invoking the parser directly. The appropriate
124387 ** table name will be inserted automatically by the parser so we can just
124388 ** use the abbreviation "x" here. The parser will also automatically tag
124389 ** the schema table as read-only. */
124390 azArg[0] = zMasterName = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
124391 azArg[1] = "1";
124392 azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
124393 "rootpage int,sql text)";
124394 azArg[3] = 0;
124395 initData.db = db;
124396 initData.iDb = iDb;
124397 initData.rc = SQLITE_OK0;
124398 initData.pzErrMsg = pzErrMsg;
124399 initData.mInitFlags = mFlags;
124400 initData.nInitRow = 0;
124401 sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
124402 if( initData.rc ){
124403 rc = initData.rc;
124404 goto error_out;
124405 }
124406
124407 /* Create a cursor to hold the database open
124408 */
124409 pDb = &db->aDb[iDb];
124410 if( pDb->pBt==0 ){
124411 assert( iDb==1 )((void) (0));
124412 DbSetProperty(db, 1, DB_SchemaLoaded)(db)->aDb[1].pSchema->schemaFlags|=(0x0001);
124413 rc = SQLITE_OK0;
124414 goto error_out;
124415 }
124416
124417 /* If there is not already a read-only (or read-write) transaction opened
124418 ** on the b-tree database, open one now. If a transaction is opened, it
124419 ** will be closed before this function returns. */
124420 sqlite3BtreeEnter(pDb->pBt);
124421 if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
124422 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
124423 if( rc!=SQLITE_OK0 ){
124424 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
124425 goto initone_error_out;
124426 }
124427 openedTransaction = 1;
124428 }
124429
124430 /* Get the database meta information.
124431 **
124432 ** Meta values are as follows:
124433 ** meta[0] Schema cookie. Changes with each schema change.
124434 ** meta[1] File format of schema layer.
124435 ** meta[2] Size of the page cache.
124436 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
124437 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
124438 ** meta[5] User version
124439 ** meta[6] Incremental vacuum mode
124440 ** meta[7] unused
124441 ** meta[8] unused
124442 ** meta[9] unused
124443 **
124444 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
124445 ** the possible values of meta[4].
124446 */
124447 for(i=0; i<ArraySize(meta)((int)(sizeof(meta)/sizeof(meta[0]))); i++){
124448 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
124449 }
124450 if( (db->flags & SQLITE_ResetDatabase0x02000000)!=0 ){
124451 memset(meta, 0, sizeof(meta));
124452 }
124453 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION1-1];
124454
124455 /* If opening a non-empty database, check the text encoding. For the
124456 ** main database, set sqlite3.enc to the encoding of the main database.
124457 ** For an attached db, it is an error if the encoding is not the same
124458 ** as sqlite3.enc.
124459 */
124460 if( meta[BTREE_TEXT_ENCODING5-1] ){ /* text encoding */
124461 if( iDb==0 ){
124462#ifndef SQLITE_OMIT_UTF16
124463 u8 encoding;
124464 /* If opening the main database, set ENC(db). */
124465 encoding = (u8)meta[BTREE_TEXT_ENCODING5-1] & 3;
124466 if( encoding==0 ) encoding = SQLITE_UTF81;
124467 ENC(db)((db)->enc) = encoding;
124468#else
124469 ENC(db)((db)->enc) = SQLITE_UTF81;
124470#endif
124471 }else{
124472 /* If opening an attached database, the encoding much match ENC(db) */
124473 if( meta[BTREE_TEXT_ENCODING5-1]!=ENC(db)((db)->enc) ){
124474 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
124475 " text encoding as main database");
124476 rc = SQLITE_ERROR1;
124477 goto initone_error_out;
124478 }
124479 }
124480 }else{
124481 DbSetProperty(db, iDb, DB_Empty)(db)->aDb[iDb].pSchema->schemaFlags|=(0x0004);
124482 }
124483 pDb->pSchema->enc = ENC(db)((db)->enc);
124484
124485 if( pDb->pSchema->cache_size==0 ){
124486#ifndef SQLITE_OMIT_DEPRECATED
124487 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE3-1]);
124488 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE-2000; }
124489 pDb->pSchema->cache_size = size;
124490#else
124491 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE-2000;
124492#endif
124493 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
124494 }
124495
124496 /*
124497 ** file_format==1 Version 3.0.0.
124498 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
124499 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
124500 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
124501 */
124502 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT2-1];
124503 if( pDb->pSchema->file_format==0 ){
124504 pDb->pSchema->file_format = 1;
124505 }
124506 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT4 ){
124507 sqlite3SetString(pzErrMsg, db, "unsupported file format");
124508 rc = SQLITE_ERROR1;
124509 goto initone_error_out;
124510 }
124511
124512 /* Ticket #2804: When we open a database in the newer file format,
124513 ** clear the legacy_file_format pragma flag so that a VACUUM will
124514 ** not downgrade the database and thus invalidate any descending
124515 ** indices that the user might have created.
124516 */
124517 if( iDb==0 && meta[BTREE_FILE_FORMAT2-1]>=4 ){
124518 db->flags &= ~(u64)SQLITE_LegacyFileFmt0x00000002;
124519 }
124520
124521 /* Read the schema information out of the schema tables
124522 */
124523 assert( db->init.busy )((void) (0));
124524 {
124525 char *zSql;
124526 zSql = sqlite3MPrintf(db,
124527 "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
124528 db->aDb[iDb].zDbSName, zMasterName);
124529#ifndef SQLITE_OMIT_AUTHORIZATION
124530 {
124531 sqlite3_xauth xAuth;
124532 xAuth = db->xAuth;
124533 db->xAuth = 0;
124534#endif
124535 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
124536#ifndef SQLITE_OMIT_AUTHORIZATION
124537 db->xAuth = xAuth;
124538 }
124539#endif
124540 if( rc==SQLITE_OK0 ) rc = initData.rc;
124541 sqlite3DbFree(db, zSql);
124542#ifndef SQLITE_OMIT_ANALYZE
124543 if( rc==SQLITE_OK0 ){
124544 sqlite3AnalysisLoad(db, iDb);
124545 }
124546#endif
124547 }
124548 if( db->mallocFailed ){
124549 rc = SQLITE_NOMEM_BKPT7;
124550 sqlite3ResetAllSchemasOfConnection(db);
124551 }
124552 if( rc==SQLITE_OK0 || (db->flags&SQLITE_NoSchemaError0x08000000)){
124553 /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
124554 ** the schema loaded, even if errors occurred. In this situation the
124555 ** current sqlite3_prepare() operation will fail, but the following one
124556 ** will attempt to compile the supplied statement against whatever subset
124557 ** of the schema was loaded before the error occurred. The primary
124558 ** purpose of this is to allow access to the sqlite_master table
124559 ** even when its contents have been corrupted.
124560 */
124561 DbSetProperty(db, iDb, DB_SchemaLoaded)(db)->aDb[iDb].pSchema->schemaFlags|=(0x0001);
124562 rc = SQLITE_OK0;
124563 }
124564
124565 /* Jump here for an error that occurs after successfully allocating
124566 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
124567 ** before that point, jump to error_out.
124568 */
124569initone_error_out:
124570 if( openedTransaction ){
124571 sqlite3BtreeCommit(pDb->pBt);
124572 }
124573 sqlite3BtreeLeave(pDb->pBt);
124574
124575error_out:
124576 if( rc ){
124577 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
124578 sqlite3OomFault(db);
124579 }
124580 sqlite3ResetOneSchema(db, iDb);
124581 }
124582 db->init.busy = 0;
124583 return rc;
124584}
124585
124586/*
124587** Initialize all database files - the main database file, the file
124588** used to store temporary tables, and any additional database files
124589** created using ATTACH statements. Return a success code. If an
124590** error occurs, write an error message into *pzErrMsg.
124591**
124592** After a database is initialized, the DB_SchemaLoaded bit is set
124593** bit is set in the flags field of the Db structure. If the database
124594** file was of zero-length, then the DB_Empty flag is also set.
124595*/
124596SQLITE_PRIVATEstatic int sqlite3Init(sqlite3 *db, char **pzErrMsg){
124597 int i, rc;
124598 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange0x0001);
124599
124600 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
124601 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) )((void) (0));
124602 assert( db->init.busy==0 )((void) (0));
124603 ENC(db)((db)->enc) = SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc);
124604 assert( db->nDb>0 )((void) (0));
124605 /* Do the main schema first */
124606 if( !DbHasProperty(db, 0, DB_SchemaLoaded)(((db)->aDb[0].pSchema->schemaFlags&(0x0001))==(0x0001
))
){
124607 rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
124608 if( rc ) return rc;
124609 }
124610 /* All other schemas after the main schema. The "temp" schema must be last */
124611 for(i=db->nDb-1; i>0; i--){
124612 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) )((void) (0));
124613 if( !DbHasProperty(db, i, DB_SchemaLoaded)(((db)->aDb[i].pSchema->schemaFlags&(0x0001))==(0x0001
))
){
124614 rc = sqlite3InitOne(db, i, pzErrMsg, 0);
124615 if( rc ) return rc;
124616 }
124617 }
124618 if( commit_internal ){
124619 sqlite3CommitInternalChanges(db);
124620 }
124621 return SQLITE_OK0;
124622}
124623
124624/*
124625** This routine is a no-op if the database schema is already initialized.
124626** Otherwise, the schema is loaded. An error code is returned.
124627*/
124628SQLITE_PRIVATEstatic int sqlite3ReadSchema(Parse *pParse){
124629 int rc = SQLITE_OK0;
124630 sqlite3 *db = pParse->db;
124631 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
124632 if( !db->init.busy ){
124633 rc = sqlite3Init(db, &pParse->zErrMsg);
124634 if( rc!=SQLITE_OK0 ){
124635 pParse->rc = rc;
124636 pParse->nErr++;
124637 }else if( db->noSharedCache ){
124638 db->mDbFlags |= DBFLAG_SchemaKnownOk0x0010;
124639 }
124640 }
124641 return rc;
124642}
124643
124644
124645/*
124646** Check schema cookies in all databases. If any cookie is out
124647** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
124648** make no changes to pParse->rc.
124649*/
124650static void schemaIsValid(Parse *pParse){
124651 sqlite3 *db = pParse->db;
124652 int iDb;
124653 int rc;
124654 int cookie;
124655
124656 assert( pParse->checkSchema )((void) (0));
124657 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
124658 for(iDb=0; iDb<db->nDb; iDb++){
124659 int openedTransaction = 0; /* True if a transaction is opened */
124660 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
124661 if( pBt==0 ) continue;
124662
124663 /* If there is not already a read-only (or read-write) transaction opened
124664 ** on the b-tree database, open one now. If a transaction is opened, it
124665 ** will be closed immediately after reading the meta-value. */
124666 if( !sqlite3BtreeIsInReadTrans(pBt) ){
124667 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
124668 if( rc==SQLITE_NOMEM7 || rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
124669 sqlite3OomFault(db);
124670 }
124671 if( rc!=SQLITE_OK0 ) return;
124672 openedTransaction = 1;
124673 }
124674
124675 /* Read the schema cookie from the database. If it does not match the
124676 ** value stored as part of the in-memory schema representation,
124677 ** set Parse.rc to SQLITE_SCHEMA. */
124678 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION1, (u32 *)&cookie);
124679 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
124680 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
124681 sqlite3ResetOneSchema(db, iDb);
124682 pParse->rc = SQLITE_SCHEMA17;
124683 }
124684
124685 /* Close the transaction, if one was opened. */
124686 if( openedTransaction ){
124687 sqlite3BtreeCommit(pBt);
124688 }
124689 }
124690}
124691
124692/*
124693** Convert a schema pointer into the iDb index that indicates
124694** which database file in db->aDb[] the schema refers to.
124695**
124696** If the same database is attached more than once, the first
124697** attached database is returned.
124698*/
124699SQLITE_PRIVATEstatic int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
124700 int i = -1000000;
124701
124702 /* If pSchema is NULL, then return -1000000. This happens when code in
124703 ** expr.c is trying to resolve a reference to a transient table (i.e. one
124704 ** created by a sub-select). In this case the return value of this
124705 ** function should never be used.
124706 **
124707 ** We return -1000000 instead of the more usual -1 simply because using
124708 ** -1000000 as the incorrect index into db->aDb[] is much
124709 ** more likely to cause a segfault than -1 (of course there are assert()
124710 ** statements too, but it never hurts to play the odds).
124711 */
124712 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
124713 if( pSchema ){
124714 for(i=0; 1; i++){
124715 assert( i<db->nDb )((void) (0));
124716 if( db->aDb[i].pSchema==pSchema ){
124717 break;
124718 }
124719 }
124720 assert( i>=0 && i<db->nDb )((void) (0));
124721 }
124722 return i;
124723}
124724
124725/*
124726** Free all memory allocations in the pParse object
124727*/
124728SQLITE_PRIVATEstatic void sqlite3ParserReset(Parse *pParse){
124729 sqlite3 *db = pParse->db;
124730 sqlite3DbFree(db, pParse->aLabel);
124731 sqlite3ExprListDelete(db, pParse->pConstExpr);
124732 if( db ){
124733 assert( db->lookaside.bDisable >= pParse->disableLookaside )((void) (0));
124734 db->lookaside.bDisable -= pParse->disableLookaside;
124735 }
124736 pParse->disableLookaside = 0;
124737}
124738
124739/*
124740** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
124741*/
124742static int sqlite3Prepare(
124743 sqlite3 *db, /* Database handle. */
124744 const char *zSql, /* UTF-8 encoded SQL statement. */
124745 int nBytes, /* Length of zSql in bytes. */
124746 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
124747 Vdbe *pReprepare, /* VM being reprepared */
124748 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
124749 const char **pzTail /* OUT: End of parsed string */
124750){
124751 char *zErrMsg = 0; /* Error message */
124752 int rc = SQLITE_OK0; /* Result code */
124753 int i; /* Loop counter */
124754 Parse sParse; /* Parsing context */
124755
124756 memset(&sParse, 0, PARSE_HDR_SZ__builtin_offsetof(Parse, aTempReg));
124757 memset(PARSE_TAIL(&sParse)(((char*)(&sParse))+__builtin_offsetof(Parse, sLastToken)
)
, 0, PARSE_TAIL_SZ(sizeof(Parse)-__builtin_offsetof(Parse, sLastToken)));
124758 sParse.pReprepare = pReprepare;
124759 assert( ppStmt && *ppStmt==0 )((void) (0));
124760 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
124761 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
124762
124763 /* For a long-term use prepared statement avoid the use of
124764 ** lookaside memory.
124765 */
124766 if( prepFlags & SQLITE_PREPARE_PERSISTENT0x01 ){
124767 sParse.disableLookaside++;
124768 db->lookaside.bDisable++;
124769 }
124770 sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB0x04)!=0;
124771
124772 /* Check to verify that it is possible to get a read lock on all
124773 ** database schemas. The inability to get a read lock indicates that
124774 ** some other database connection is holding a write-lock, which in
124775 ** turn means that the other connection has made uncommitted changes
124776 ** to the schema.
124777 **
124778 ** Were we to proceed and prepare the statement against the uncommitted
124779 ** schema changes and if those schema changes are subsequently rolled
124780 ** back and different changes are made in their place, then when this
124781 ** prepared statement goes to run the schema cookie would fail to detect
124782 ** the schema change. Disaster would follow.
124783 **
124784 ** This thread is currently holding mutexes on all Btrees (because
124785 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
124786 ** is not possible for another thread to start a new schema change
124787 ** while this routine is running. Hence, we do not need to hold
124788 ** locks on the schema, we just need to make sure nobody else is
124789 ** holding them.
124790 **
124791 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
124792 ** but it does *not* override schema lock detection, so this all still
124793 ** works even if READ_UNCOMMITTED is set.
124794 */
124795 for(i=0; i<db->nDb; i++) {
124796 Btree *pBt = db->aDb[i].pBt;
124797 if( pBt ){
124798 assert( sqlite3BtreeHoldsMutex(pBt) )((void) (0));
124799 rc = sqlite3BtreeSchemaLocked(pBt);
124800 if( rc ){
124801 const char *zDb = db->aDb[i].zDbSName;
124802 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
124803 testcase( db->flags & SQLITE_ReadUncommit );
124804 goto end_prepare;
124805 }
124806 }
124807 }
124808
124809 sqlite3VtabUnlockList(db);
124810
124811 sParse.db = db;
124812 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
124813 char *zSqlCopy;
124814 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH1];
124815 testcase( nBytes==mxLen );
124816 testcase( nBytes==mxLen+1 );
124817 if( nBytes>mxLen ){
124818 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG18, "statement too long");
124819 rc = sqlite3ApiExit(db, SQLITE_TOOBIG18);
124820 goto end_prepare;
124821 }
124822 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
124823 if( zSqlCopy ){
124824 sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
124825 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
124826 sqlite3DbFree(db, zSqlCopy);
124827 }else{
124828 sParse.zTail = &zSql[nBytes];
124829 }
124830 }else{
124831 sqlite3RunParser(&sParse, zSql, &zErrMsg);
124832 }
124833 assert( 0==sParse.nQueryLoop )((void) (0));
124834
124835 if( sParse.rc==SQLITE_DONE101 ) sParse.rc = SQLITE_OK0;
124836 if( sParse.checkSchema ){
124837 schemaIsValid(&sParse);
124838 }
124839 if( db->mallocFailed ){
124840 sParse.rc = SQLITE_NOMEM_BKPT7;
124841 }
124842 if( pzTail ){
124843 *pzTail = sParse.zTail;
124844 }
124845 rc = sParse.rc;
124846
124847#ifndef SQLITE_OMIT_EXPLAIN
124848 if( rc==SQLITE_OK0 && sParse.pVdbe && sParse.explain ){
124849 static const char * const azColName[] = {
124850 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
124851 "id", "parent", "notused", "detail"
124852 };
124853 int iFirst, mx;
124854 if( sParse.explain==2 ){
124855 sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
124856 iFirst = 8;
124857 mx = 12;
124858 }else{
124859 sqlite3VdbeSetNumCols(sParse.pVdbe, 8);
124860 iFirst = 0;
124861 mx = 8;
124862 }
124863 for(i=iFirst; i<mx; i++){
124864 sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME0,
124865 azColName[i], SQLITE_STATIC((sqlite3_destructor_type)0));
124866 }
124867 }
124868#endif
124869
124870 if( db->init.busy==0 ){
124871 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
124872 }
124873 if( sParse.pVdbe && (rc!=SQLITE_OK0 || db->mallocFailed) ){
124874 sqlite3VdbeFinalize(sParse.pVdbe);
124875 assert(!(*ppStmt))((void) (0));
124876 }else{
124877 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
124878 }
124879
124880 if( zErrMsg ){
124881 sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
124882 sqlite3DbFree(db, zErrMsg);
124883 }else{
124884 sqlite3Error(db, rc);
124885 }
124886
124887 /* Delete any TriggerPrg structures allocated while parsing this statement. */
124888 while( sParse.pTriggerPrg ){
124889 TriggerPrg *pT = sParse.pTriggerPrg;
124890 sParse.pTriggerPrg = pT->pNext;
124891 sqlite3DbFree(db, pT);
124892 }
124893
124894end_prepare:
124895
124896 sqlite3ParserReset(&sParse);
124897 return rc;
124898}
124899static int sqlite3LockAndPrepare(
124900 sqlite3 *db, /* Database handle. */
124901 const char *zSql, /* UTF-8 encoded SQL statement. */
124902 int nBytes, /* Length of zSql in bytes. */
124903 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
124904 Vdbe *pOld, /* VM being reprepared */
124905 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
124906 const char **pzTail /* OUT: End of parsed string */
124907){
124908 int rc;
124909 int cnt = 0;
124910
124911#ifdef SQLITE_ENABLE_API_ARMOR
124912 if( ppStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(124912);
124913#endif
124914 *ppStmt = 0;
124915 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
124916 return SQLITE_MISUSE_BKPTsqlite3MisuseError(124916);
124917 }
124918 sqlite3_mutex_enter(db->mutex);
124919 sqlite3BtreeEnterAll(db);
124920 do{
124921 /* Make multiple attempts to compile the SQL, until it either succeeds
124922 ** or encounters a permanent error. A schema problem after one schema
124923 ** reset is considered a permanent error. */
124924 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
124925 assert( rc==SQLITE_OK || *ppStmt==0 )((void) (0));
124926 }while( rc==SQLITE_ERROR_RETRY(1 | (2<<8))
124927 || (rc==SQLITE_SCHEMA17 && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
124928 sqlite3BtreeLeaveAll(db);
124929 rc = sqlite3ApiExit(db, rc);
124930 assert( (rc&db->errMask)==rc )((void) (0));
124931 sqlite3_mutex_leave(db->mutex);
124932 return rc;
124933}
124934
124935
124936/*
124937** Rerun the compilation of a statement after a schema change.
124938**
124939** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
124940** if the statement cannot be recompiled because another connection has
124941** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
124942** occurs, return SQLITE_SCHEMA.
124943*/
124944SQLITE_PRIVATEstatic int sqlite3Reprepare(Vdbe *p){
124945 int rc;
124946 sqlite3_stmt *pNew;
124947 const char *zSql;
124948 sqlite3 *db;
124949 u8 prepFlags;
124950
124951 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) )((void) (0));
124952 zSql = sqlite3_sql((sqlite3_stmt *)p);
124953 assert( zSql!=0 )((void) (0)); /* Reprepare only called for prepare_v2() statements */
124954 db = sqlite3VdbeDb(p);
124955 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
124956 prepFlags = sqlite3VdbePrepareFlags(p);
124957 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
124958 if( rc ){
124959 if( rc==SQLITE_NOMEM7 ){
124960 sqlite3OomFault(db);
124961 }
124962 assert( pNew==0 )((void) (0));
124963 return rc;
124964 }else{
124965 assert( pNew!=0 )((void) (0));
124966 }
124967 sqlite3VdbeSwap((Vdbe*)pNew, p);
124968 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
124969 sqlite3VdbeResetStepResult((Vdbe*)pNew);
124970 sqlite3VdbeFinalize((Vdbe*)pNew);
124971 return SQLITE_OK0;
124972}
124973
124974
124975/*
124976** Two versions of the official API. Legacy and new use. In the legacy
124977** version, the original SQL text is not saved in the prepared statement
124978** and so if a schema change occurs, SQLITE_SCHEMA is returned by
124979** sqlite3_step(). In the new version, the original SQL text is retained
124980** and the statement is automatically recompiled if an schema change
124981** occurs.
124982*/
124983SQLITE_API int sqlite3_prepare(
124984 sqlite3 *db, /* Database handle. */
124985 const char *zSql, /* UTF-8 encoded SQL statement. */
124986 int nBytes, /* Length of zSql in bytes. */
124987 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
124988 const char **pzTail /* OUT: End of parsed string */
124989){
124990 int rc;
124991 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
124992 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
124993 return rc;
124994}
124995SQLITE_API int sqlite3_prepare_v2(
124996 sqlite3 *db, /* Database handle. */
124997 const char *zSql, /* UTF-8 encoded SQL statement. */
124998 int nBytes, /* Length of zSql in bytes. */
124999 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
125000 const char **pzTail /* OUT: End of parsed string */
125001){
125002 int rc;
125003 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
125004 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
125005 ** parameter.
125006 **
125007 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
125008 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL0x80,0,
125009 ppStmt,pzTail);
125010 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0));
125011 return rc;
125012}
125013SQLITE_API int sqlite3_prepare_v3(
125014 sqlite3 *db, /* Database handle. */
125015 const char *zSql, /* UTF-8 encoded SQL statement. */
125016 int nBytes, /* Length of zSql in bytes. */
125017 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
125018 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
125019 const char **pzTail /* OUT: End of parsed string */
125020){
125021 int rc;
125022 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
125023 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
125024 ** which is a bit array consisting of zero or more of the
125025 ** SQLITE_PREPARE_* flags.
125026 **
125027 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
125028 ** directly above. */
125029 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
125030 SQLITE_PREPARE_SAVESQL0x80|(prepFlags&SQLITE_PREPARE_MASK0x0f),
125031 0,ppStmt,pzTail);
125032 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0));
125033 return rc;
125034}
125035
125036
125037#ifndef SQLITE_OMIT_UTF16
125038/*
125039** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
125040*/
125041static int sqlite3Prepare16(
125042 sqlite3 *db, /* Database handle. */
125043 const void *zSql, /* UTF-16 encoded SQL statement. */
125044 int nBytes, /* Length of zSql in bytes. */
125045 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
125046 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
125047 const void **pzTail /* OUT: End of parsed string */
125048){
125049 /* This function currently works by first transforming the UTF-16
125050 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
125051 ** tricky bit is figuring out the pointer to return in *pzTail.
125052 */
125053 char *zSql8;
125054 const char *zTail8 = 0;
125055 int rc = SQLITE_OK0;
125056
125057#ifdef SQLITE_ENABLE_API_ARMOR
125058 if( ppStmt==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(125058);
125059#endif
125060 *ppStmt = 0;
125061 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
125062 return SQLITE_MISUSE_BKPTsqlite3MisuseError(125062);
125063 }
125064 if( nBytes>=0 ){
125065 int sz;
125066 const char *z = (const char*)zSql;
125067 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
125068 nBytes = sz;
125069 }
125070 sqlite3_mutex_enter(db->mutex);
125071 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE2);
125072 if( zSql8 ){
125073 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
125074 }
125075
125076 if( zTail8 && pzTail ){
125077 /* If sqlite3_prepare returns a tail pointer, we calculate the
125078 ** equivalent pointer into the UTF-16 string by counting the unicode
125079 ** characters between zSql8 and zTail8, and then returning a pointer
125080 ** the same number of characters into the UTF-16 string.
125081 */
125082 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
125083 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
125084 }
125085 sqlite3DbFree(db, zSql8);
125086 rc = sqlite3ApiExit(db, rc);
125087 sqlite3_mutex_leave(db->mutex);
125088 return rc;
125089}
125090
125091/*
125092** Two versions of the official API. Legacy and new use. In the legacy
125093** version, the original SQL text is not saved in the prepared statement
125094** and so if a schema change occurs, SQLITE_SCHEMA is returned by
125095** sqlite3_step(). In the new version, the original SQL text is retained
125096** and the statement is automatically recompiled if an schema change
125097** occurs.
125098*/
125099SQLITE_API int sqlite3_prepare16(
125100 sqlite3 *db, /* Database handle. */
125101 const void *zSql, /* UTF-16 encoded SQL statement. */
125102 int nBytes, /* Length of zSql in bytes. */
125103 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
125104 const void **pzTail /* OUT: End of parsed string */
125105){
125106 int rc;
125107 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
125108 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
125109 return rc;
125110}
125111SQLITE_API int sqlite3_prepare16_v2(
125112 sqlite3 *db, /* Database handle. */
125113 const void *zSql, /* UTF-16 encoded SQL statement. */
125114 int nBytes, /* Length of zSql in bytes. */
125115 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
125116 const void **pzTail /* OUT: End of parsed string */
125117){
125118 int rc;
125119 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL0x80,ppStmt,pzTail);
125120 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
125121 return rc;
125122}
125123SQLITE_API int sqlite3_prepare16_v3(
125124 sqlite3 *db, /* Database handle. */
125125 const void *zSql, /* UTF-16 encoded SQL statement. */
125126 int nBytes, /* Length of zSql in bytes. */
125127 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
125128 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
125129 const void **pzTail /* OUT: End of parsed string */
125130){
125131 int rc;
125132 rc = sqlite3Prepare16(db,zSql,nBytes,
125133 SQLITE_PREPARE_SAVESQL0x80|(prepFlags&SQLITE_PREPARE_MASK0x0f),
125134 ppStmt,pzTail);
125135 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 )((void) (0)); /* VERIFY: F13021 */
125136 return rc;
125137}
125138
125139#endif /* SQLITE_OMIT_UTF16 */
125140
125141/************** End of prepare.c *********************************************/
125142/************** Begin file select.c ******************************************/
125143/*
125144** 2001 September 15
125145**
125146** The author disclaims copyright to this source code. In place of
125147** a legal notice, here is a blessing:
125148**
125149** May you do good and not evil.
125150** May you find forgiveness for yourself and forgive others.
125151** May you share freely, never taking more than you give.
125152**
125153*************************************************************************
125154** This file contains C code routines that are called by the parser
125155** to handle SELECT statements in SQLite.
125156*/
125157/* #include "sqliteInt.h" */
125158
125159/*
125160** Trace output macros
125161*/
125162#if SELECTTRACE_ENABLED0
125163/***/ int sqlite3SelectTrace = 0;
125164# define SELECTTRACE(K,P,S,X) \
125165 if(sqlite3SelectTrace&(K)) \
125166 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
125167 sqlite3DebugPrintf X
125168#else
125169# define SELECTTRACE(K,P,S,X)
125170#endif
125171
125172
125173/*
125174** An instance of the following object is used to record information about
125175** how to process the DISTINCT keyword, to simplify passing that information
125176** into the selectInnerLoop() routine.
125177*/
125178typedef struct DistinctCtx DistinctCtx;
125179struct DistinctCtx {
125180 u8 isTnct; /* True if the DISTINCT keyword is present */
125181 u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */
125182 int tabTnct; /* Ephemeral table used for DISTINCT processing */
125183 int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */
125184};
125185
125186/*
125187** An instance of the following object is used to record information about
125188** the ORDER BY (or GROUP BY) clause of query is being coded.
125189**
125190** The aDefer[] array is used by the sorter-references optimization. For
125191** example, assuming there is no index that can be used for the ORDER BY,
125192** for the query:
125193**
125194** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10;
125195**
125196** it may be more efficient to add just the "a" values to the sorter, and
125197** retrieve the associated "bigblob" values directly from table t1 as the
125198** 10 smallest "a" values are extracted from the sorter.
125199**
125200** When the sorter-reference optimization is used, there is one entry in the
125201** aDefer[] array for each database table that may be read as values are
125202** extracted from the sorter.
125203*/
125204typedef struct SortCtx SortCtx;
125205struct SortCtx {
125206 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */
125207 int nOBSat; /* Number of ORDER BY terms satisfied by indices */
125208 int iECursor; /* Cursor number for the sorter */
125209 int regReturn; /* Register holding block-output return address */
125210 int labelBkOut; /* Start label for the block-output subroutine */
125211 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */
125212 int labelDone; /* Jump here when done, ex: LIMIT reached */
125213 int labelOBLopt; /* Jump here when sorter is full */
125214 u8 sortFlags; /* Zero or more SORTFLAG_* bits */
125215#ifdef SQLITE_ENABLE_SORTER_REFERENCES
125216 u8 nDefer; /* Number of valid entries in aDefer[] */
125217 struct DeferredCsr {
125218 Table *pTab; /* Table definition */
125219 int iCsr; /* Cursor number for table */
125220 int nKey; /* Number of PK columns for table pTab (>=1) */
125221 } aDefer[4];
125222#endif
125223 struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */
125224};
125225#define SORTFLAG_UseSorter0x01 0x01 /* Use SorterOpen instead of OpenEphemeral */
125226
125227/*
125228** Delete all the content of a Select structure. Deallocate the structure
125229** itself only if bFree is true.
125230*/
125231static void clearSelect(sqlite3 *db, Select *p, int bFree){
125232 while( p ){
125233 Select *pPrior = p->pPrior;
125234 sqlite3ExprListDelete(db, p->pEList);
125235 sqlite3SrcListDelete(db, p->pSrc);
125236 sqlite3ExprDelete(db, p->pWhere);
125237 sqlite3ExprListDelete(db, p->pGroupBy);
125238 sqlite3ExprDelete(db, p->pHaving);
125239 sqlite3ExprListDelete(db, p->pOrderBy);
125240 sqlite3ExprDelete(db, p->pLimit);
125241#ifndef SQLITE_OMIT_WINDOWFUNC
125242 if( OK_IF_ALWAYS_TRUE(p->pWinDefn)(p->pWinDefn) ){
125243 sqlite3WindowListDelete(db, p->pWinDefn);
125244 }
125245#endif
125246 if( OK_IF_ALWAYS_TRUE(p->pWith)(p->pWith) ) sqlite3WithDelete(db, p->pWith);
125247 if( bFree ) sqlite3DbFreeNN(db, p);
125248 p = pPrior;
125249 bFree = 1;
125250 }
125251}
125252
125253/*
125254** Initialize a SelectDest structure.
125255*/
125256SQLITE_PRIVATEstatic void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
125257 pDest->eDest = (u8)eDest;
125258 pDest->iSDParm = iParm;
125259 pDest->zAffSdst = 0;
125260 pDest->iSdst = 0;
125261 pDest->nSdst = 0;
125262}
125263
125264
125265/*
125266** Allocate a new Select structure and return a pointer to that
125267** structure.
125268*/
125269SQLITE_PRIVATEstatic Select *sqlite3SelectNew(
125270 Parse *pParse, /* Parsing context */
125271 ExprList *pEList, /* which columns to include in the result */
125272 SrcList *pSrc, /* the FROM clause -- which tables to scan */
125273 Expr *pWhere, /* the WHERE clause */
125274 ExprList *pGroupBy, /* the GROUP BY clause */
125275 Expr *pHaving, /* the HAVING clause */
125276 ExprList *pOrderBy, /* the ORDER BY clause */
125277 u32 selFlags, /* Flag parameters, such as SF_Distinct */
125278 Expr *pLimit /* LIMIT value. NULL means not used */
125279){
125280 Select *pNew;
125281 Select standin;
125282 pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
125283 if( pNew==0 ){
125284 assert( pParse->db->mallocFailed )((void) (0));
125285 pNew = &standin;
125286 }
125287 if( pEList==0 ){
125288 pEList = sqlite3ExprListAppend(pParse, 0,
125289 sqlite3Expr(pParse->db,TK_ASTERISK172,0));
125290 }
125291 pNew->pEList = pEList;
125292 pNew->op = TK_SELECT131;
125293 pNew->selFlags = selFlags;
125294 pNew->iLimit = 0;
125295 pNew->iOffset = 0;
125296 pNew->selId = ++pParse->nSelect;
125297 pNew->addrOpenEphm[0] = -1;
125298 pNew->addrOpenEphm[1] = -1;
125299 pNew->nSelectRow = 0;
125300 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
125301 pNew->pSrc = pSrc;
125302 pNew->pWhere = pWhere;
125303 pNew->pGroupBy = pGroupBy;
125304 pNew->pHaving = pHaving;
125305 pNew->pOrderBy = pOrderBy;
125306 pNew->pPrior = 0;
125307 pNew->pNext = 0;
125308 pNew->pLimit = pLimit;
125309 pNew->pWith = 0;
125310#ifndef SQLITE_OMIT_WINDOWFUNC
125311 pNew->pWin = 0;
125312 pNew->pWinDefn = 0;
125313#endif
125314 if( pParse->db->mallocFailed ) {
125315 clearSelect(pParse->db, pNew, pNew!=&standin);
125316 pNew = 0;
125317 }else{
125318 assert( pNew->pSrc!=0 || pParse->nErr>0 )((void) (0));
125319 }
125320 assert( pNew!=&standin )((void) (0));
125321 return pNew;
125322}
125323
125324
125325/*
125326** Delete the given Select structure and all of its substructures.
125327*/
125328SQLITE_PRIVATEstatic void sqlite3SelectDelete(sqlite3 *db, Select *p){
125329 if( OK_IF_ALWAYS_TRUE(p)(p) ) clearSelect(db, p, 1);
125330}
125331
125332/*
125333** Return a pointer to the right-most SELECT statement in a compound.
125334*/
125335static Select *findRightmost(Select *p){
125336 while( p->pNext ) p = p->pNext;
125337 return p;
125338}
125339
125340/*
125341** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
125342** type of join. Return an integer constant that expresses that type
125343** in terms of the following bit values:
125344**
125345** JT_INNER
125346** JT_CROSS
125347** JT_OUTER
125348** JT_NATURAL
125349** JT_LEFT
125350** JT_RIGHT
125351**
125352** A full outer join is the combination of JT_LEFT and JT_RIGHT.
125353**
125354** If an illegal or unsupported join type is seen, then still return
125355** a join type, but put an error in the pParse structure.
125356*/
125357SQLITE_PRIVATEstatic int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
125358 int jointype = 0;
125359 Token *apAll[3];
125360 Token *p;
125361 /* 0123456789 123456789 123456789 123 */
125362 static const char zKeyText[] = "naturaleftouterightfullinnercross";
125363 static const struct {
125364 u8 i; /* Beginning of keyword text in zKeyText[] */
125365 u8 nChar; /* Length of the keyword in characters */
125366 u8 code; /* Join type mask */
125367 } aKeyword[] = {
125368 /* natural */ { 0, 7, JT_NATURAL0x0004 },
125369 /* left */ { 6, 4, JT_LEFT0x0008|JT_OUTER0x0020 },
125370 /* outer */ { 10, 5, JT_OUTER0x0020 },
125371 /* right */ { 14, 5, JT_RIGHT0x0010|JT_OUTER0x0020 },
125372 /* full */ { 19, 4, JT_LEFT0x0008|JT_RIGHT0x0010|JT_OUTER0x0020 },
125373 /* inner */ { 23, 5, JT_INNER0x0001 },
125374 /* cross */ { 28, 5, JT_INNER0x0001|JT_CROSS0x0002 },
125375 };
125376 int i, j;
125377 apAll[0] = pA;
125378 apAll[1] = pB;
125379 apAll[2] = pC;
125380 for(i=0; i<3 && apAll[i]; i++){
125381 p = apAll[i];
125382 for(j=0; j<ArraySize(aKeyword)((int)(sizeof(aKeyword)/sizeof(aKeyword[0]))); j++){
125383 if( p->n==aKeyword[j].nChar
125384 && sqlite3StrNICmpsqlite3_strnicmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
125385 jointype |= aKeyword[j].code;
125386 break;
125387 }
125388 }
125389 testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
125390 if( j>=ArraySize(aKeyword)((int)(sizeof(aKeyword)/sizeof(aKeyword[0]))) ){
125391 jointype |= JT_ERROR0x0040;
125392 break;
125393 }
125394 }
125395 if(
125396 (jointype & (JT_INNER0x0001|JT_OUTER0x0020))==(JT_INNER0x0001|JT_OUTER0x0020) ||
125397 (jointype & JT_ERROR0x0040)!=0
125398 ){
125399 const char *zSp = " ";
125400 assert( pB!=0 )((void) (0));
125401 if( pC==0 ){ zSp++; }
125402 sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
125403 "%T %T%s%T", pA, pB, zSp, pC);
125404 jointype = JT_INNER0x0001;
125405 }else if( (jointype & JT_OUTER0x0020)!=0
125406 && (jointype & (JT_LEFT0x0008|JT_RIGHT0x0010))!=JT_LEFT0x0008 ){
125407 sqlite3ErrorMsg(pParse,
125408 "RIGHT and FULL OUTER JOINs are not currently supported");
125409 jointype = JT_INNER0x0001;
125410 }
125411 return jointype;
125412}
125413
125414/*
125415** Return the index of a column in a table. Return -1 if the column
125416** is not contained in the table.
125417*/
125418static int columnIndex(Table *pTab, const char *zCol){
125419 int i;
125420 for(i=0; i<pTab->nCol; i++){
125421 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
125422 }
125423 return -1;
125424}
125425
125426/*
125427** Search the first N tables in pSrc, from left to right, looking for a
125428** table that has a column named zCol.
125429**
125430** When found, set *piTab and *piCol to the table index and column index
125431** of the matching column and return TRUE.
125432**
125433** If not found, return FALSE.
125434*/
125435static int tableAndColumnIndex(
125436 SrcList *pSrc, /* Array of tables to search */
125437 int N, /* Number of tables in pSrc->a[] to search */
125438 const char *zCol, /* Name of the column we are looking for */
125439 int *piTab, /* Write index of pSrc->a[] here */
125440 int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
125441){
125442 int i; /* For looping over tables in pSrc */
125443 int iCol; /* Index of column matching zCol */
125444
125445 assert( (piTab==0)==(piCol==0) )((void) (0)); /* Both or neither are NULL */
125446 for(i=0; i<N; i++){
125447 iCol = columnIndex(pSrc->a[i].pTab, zCol);
125448 if( iCol>=0 ){
125449 if( piTab ){
125450 *piTab = i;
125451 *piCol = iCol;
125452 }
125453 return 1;
125454 }
125455 }
125456 return 0;
125457}
125458
125459/*
125460** This function is used to add terms implied by JOIN syntax to the
125461** WHERE clause expression of a SELECT statement. The new term, which
125462** is ANDed with the existing WHERE clause, is of the form:
125463**
125464** (tab1.col1 = tab2.col2)
125465**
125466** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
125467** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
125468** column iColRight of tab2.
125469*/
125470static void addWhereTerm(
125471 Parse *pParse, /* Parsing context */
125472 SrcList *pSrc, /* List of tables in FROM clause */
125473 int iLeft, /* Index of first table to join in pSrc */
125474 int iColLeft, /* Index of column in first table */
125475 int iRight, /* Index of second table in pSrc */
125476 int iColRight, /* Index of column in second table */
125477 int isOuterJoin, /* True if this is an OUTER join */
125478 Expr **ppWhere /* IN/OUT: The WHERE clause to add to */
125479){
125480 sqlite3 *db = pParse->db;
125481 Expr *pE1;
125482 Expr *pE2;
125483 Expr *pEq;
125484
125485 assert( iLeft<iRight )((void) (0));
125486 assert( pSrc->nSrc>iRight )((void) (0));
125487 assert( pSrc->a[iLeft].pTab )((void) (0));
125488 assert( pSrc->a[iRight].pTab )((void) (0));
125489
125490 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
125491 pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
125492
125493 pEq = sqlite3PExpr(pParse, TK_EQ53, pE1, pE2);
125494 if( pEq && isOuterJoin ){
125495 ExprSetProperty(pEq, EP_FromJoin)(pEq)->flags|=(0x000001);
125496 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) )((void) (0));
125497 ExprSetVVAProperty(pEq, EP_NoReduce);
125498 pEq->iRightJoinTable = (i16)pE2->iTable;
125499 }
125500 *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq);
125501}
125502
125503/*
125504** Set the EP_FromJoin property on all terms of the given expression.
125505** And set the Expr.iRightJoinTable to iTable for every term in the
125506** expression.
125507**
125508** The EP_FromJoin property is used on terms of an expression to tell
125509** the LEFT OUTER JOIN processing logic that this term is part of the
125510** join restriction specified in the ON or USING clause and not a part
125511** of the more general WHERE clause. These terms are moved over to the
125512** WHERE clause during join processing but we need to remember that they
125513** originated in the ON or USING clause.
125514**
125515** The Expr.iRightJoinTable tells the WHERE clause processing that the
125516** expression depends on table iRightJoinTable even if that table is not
125517** explicitly mentioned in the expression. That information is needed
125518** for cases like this:
125519**
125520** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
125521**
125522** The where clause needs to defer the handling of the t1.x=5
125523** term until after the t2 loop of the join. In that way, a
125524** NULL t2 row will be inserted whenever t1.x!=5. If we do not
125525** defer the handling of t1.x=5, it will be processed immediately
125526** after the t1 loop and rows with t1.x!=5 will never appear in
125527** the output, which is incorrect.
125528*/
125529static void setJoinExpr(Expr *p, int iTable){
125530 while( p ){
125531 ExprSetProperty(p, EP_FromJoin)(p)->flags|=(0x000001);
125532 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) )((void) (0));
125533 ExprSetVVAProperty(p, EP_NoReduce);
125534 p->iRightJoinTable = (i16)iTable;
125535 if( p->op==TK_FUNCTION161 && p->x.pList ){
125536 int i;
125537 for(i=0; i<p->x.pList->nExpr; i++){
125538 setJoinExpr(p->x.pList->a[i].pExpr, iTable);
125539 }
125540 }
125541 setJoinExpr(p->pLeft, iTable);
125542 p = p->pRight;
125543 }
125544}
125545
125546/* Undo the work of setJoinExpr(). In the expression tree p, convert every
125547** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
125548** an ordinary term that omits the EP_FromJoin mark.
125549**
125550** This happens when a LEFT JOIN is simplified into an ordinary JOIN.
125551*/
125552static void unsetJoinExpr(Expr *p, int iTable){
125553 while( p ){
125554 if( ExprHasProperty(p, EP_FromJoin)(((p)->flags&(0x000001))!=0)
125555 && (iTable<0 || p->iRightJoinTable==iTable) ){
125556 ExprClearProperty(p, EP_FromJoin)(p)->flags&=~(0x000001);
125557 }
125558 if( p->op==TK_FUNCTION161 && p->x.pList ){
125559 int i;
125560 for(i=0; i<p->x.pList->nExpr; i++){
125561 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
125562 }
125563 }
125564 unsetJoinExpr(p->pLeft, iTable);
125565 p = p->pRight;
125566 }
125567}
125568
125569/*
125570** This routine processes the join information for a SELECT statement.
125571** ON and USING clauses are converted into extra terms of the WHERE clause.
125572** NATURAL joins also create extra WHERE clause terms.
125573**
125574** The terms of a FROM clause are contained in the Select.pSrc structure.
125575** The left most table is the first entry in Select.pSrc. The right-most
125576** table is the last entry. The join operator is held in the entry to
125577** the left. Thus entry 0 contains the join operator for the join between
125578** entries 0 and 1. Any ON or USING clauses associated with the join are
125579** also attached to the left entry.
125580**
125581** This routine returns the number of errors encountered.
125582*/
125583static int sqliteProcessJoin(Parse *pParse, Select *p){
125584 SrcList *pSrc; /* All tables in the FROM clause */
125585 int i, j; /* Loop counters */
125586 struct SrcList_item *pLeft; /* Left table being joined */
125587 struct SrcList_item *pRight; /* Right table being joined */
125588
125589 pSrc = p->pSrc;
125590 pLeft = &pSrc->a[0];
125591 pRight = &pLeft[1];
125592 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
125593 Table *pRightTab = pRight->pTab;
125594 int isOuter;
125595
125596 if( NEVER(pLeft->pTab==0 || pRightTab==0)(pLeft->pTab==0 || pRightTab==0) ) continue;
125597 isOuter = (pRight->fg.jointype & JT_OUTER0x0020)!=0;
125598
125599 /* When the NATURAL keyword is present, add WHERE clause terms for
125600 ** every column that the two tables have in common.
125601 */
125602 if( pRight->fg.jointype & JT_NATURAL0x0004 ){
125603 if( pRight->pOn || pRight->pUsing ){
125604 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
125605 "an ON or USING clause", 0);
125606 return 1;
125607 }
125608 for(j=0; j<pRightTab->nCol; j++){
125609 char *zName; /* Name of column in the right table */
125610 int iLeft; /* Matching left table */
125611 int iLeftCol; /* Matching column in the left table */
125612
125613 zName = pRightTab->aCol[j].zName;
125614 if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
125615 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
125616 isOuter, &p->pWhere);
125617 }
125618 }
125619 }
125620
125621 /* Disallow both ON and USING clauses in the same join
125622 */
125623 if( pRight->pOn && pRight->pUsing ){
125624 sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
125625 "clauses in the same join");
125626 return 1;
125627 }
125628
125629 /* Add the ON clause to the end of the WHERE clause, connected by
125630 ** an AND operator.
125631 */
125632 if( pRight->pOn ){
125633 if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
125634 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->pOn);
125635 pRight->pOn = 0;
125636 }
125637
125638 /* Create extra terms on the WHERE clause for each column named
125639 ** in the USING clause. Example: If the two tables to be joined are
125640 ** A and B and the USING clause names X, Y, and Z, then add this
125641 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
125642 ** Report an error if any column mentioned in the USING clause is
125643 ** not contained in both tables to be joined.
125644 */
125645 if( pRight->pUsing ){
125646 IdList *pList = pRight->pUsing;
125647 for(j=0; j<pList->nId; j++){
125648 char *zName; /* Name of the term in the USING clause */
125649 int iLeft; /* Table on the left with matching column name */
125650 int iLeftCol; /* Column number of matching column on the left */
125651 int iRightCol; /* Column number of matching column on the right */
125652
125653 zName = pList->a[j].zName;
125654 iRightCol = columnIndex(pRightTab, zName);
125655 if( iRightCol<0
125656 || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
125657 ){
125658 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
125659 "not present in both tables", zName);
125660 return 1;
125661 }
125662 addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
125663 isOuter, &p->pWhere);
125664 }
125665 }
125666 }
125667 return 0;
125668}
125669
125670/*
125671** An instance of this object holds information (beyond pParse and pSelect)
125672** needed to load the next result row that is to be added to the sorter.
125673*/
125674typedef struct RowLoadInfo RowLoadInfo;
125675struct RowLoadInfo {
125676 int regResult; /* Store results in array of registers here */
125677 u8 ecelFlags; /* Flag argument to ExprCodeExprList() */
125678#ifdef SQLITE_ENABLE_SORTER_REFERENCES
125679 ExprList *pExtra; /* Extra columns needed by sorter refs */
125680 int regExtraResult; /* Where to load the extra columns */
125681#endif
125682};
125683
125684/*
125685** This routine does the work of loading query data into an array of
125686** registers so that it can be added to the sorter.
125687*/
125688static void innerLoopLoadRow(
125689 Parse *pParse, /* Statement under construction */
125690 Select *pSelect, /* The query being coded */
125691 RowLoadInfo *pInfo /* Info needed to complete the row load */
125692){
125693 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
125694 0, pInfo->ecelFlags);
125695#ifdef SQLITE_ENABLE_SORTER_REFERENCES
125696 if( pInfo->pExtra ){
125697 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
125698 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
125699 }
125700#endif
125701}
125702
125703/*
125704** Code the OP_MakeRecord instruction that generates the entry to be
125705** added into the sorter.
125706**
125707** Return the register in which the result is stored.
125708*/
125709static int makeSorterRecord(
125710 Parse *pParse,
125711 SortCtx *pSort,
125712 Select *pSelect,
125713 int regBase,
125714 int nBase
125715){
125716 int nOBSat = pSort->nOBSat;
125717 Vdbe *v = pParse->pVdbe;
125718 int regOut = ++pParse->nMem;
125719 if( pSort->pDeferredRowLoad ){
125720 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
125721 }
125722 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regBase+nOBSat, nBase-nOBSat, regOut);
125723 return regOut;
125724}
125725
125726/*
125727** Generate code that will push the record in registers regData
125728** through regData+nData-1 onto the sorter.
125729*/
125730static void pushOntoSorter(
125731 Parse *pParse, /* Parser context */
125732 SortCtx *pSort, /* Information about the ORDER BY clause */
125733 Select *pSelect, /* The whole SELECT statement */
125734 int regData, /* First register holding data to be sorted */
125735 int regOrigData, /* First register holding data before packing */
125736 int nData, /* Number of elements in the regData data array */
125737 int nPrefixReg /* No. of reg prior to regData available for use */
125738){
125739 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
125740 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter0x01)==0);
125741 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
125742 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */
125743 int regBase; /* Regs for sorter record */
125744 int regRecord = 0; /* Assembled sorter record */
125745 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
125746 int op; /* Opcode to add sorter record to sorter */
125747 int iLimit; /* LIMIT counter */
125748 int iSkip = 0; /* End of the sorter insert loop */
125749
125750 assert( bSeq==0 || bSeq==1 )((void) (0));
125751
125752 /* Three cases:
125753 ** (1) The data to be sorted has already been packed into a Record
125754 ** by a prior OP_MakeRecord. In this case nData==1 and regData
125755 ** will be completely unrelated to regOrigData.
125756 ** (2) All output columns are included in the sort record. In that
125757 ** case regData==regOrigData.
125758 ** (3) Some output columns are omitted from the sort record due to
125759 ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the
125760 ** SQLITE_ECEL_OMITREF optimization, or due to the
125761 ** SortCtx.pDeferredRowLoad optimiation. In any of these cases
125762 ** regOrigData is 0 to prevent this routine from trying to copy
125763 ** values that might not yet exist.
125764 */
125765 assert( nData==1 || regData==regOrigData || regOrigData==0 )((void) (0));
125766
125767 if( nPrefixReg ){
125768 assert( nPrefixReg==nExpr+bSeq )((void) (0));
125769 regBase = regData - nPrefixReg;
125770 }else{
125771 regBase = pParse->nMem + 1;
125772 pParse->nMem += nBase;
125773 }
125774 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 )((void) (0));
125775 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
125776 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
125777 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
125778 SQLITE_ECEL_DUP0x01 | (regOrigData? SQLITE_ECEL_REF0x04 : 0));
125779 if( bSeq ){
125780 sqlite3VdbeAddOp2(v, OP_Sequence120, pSort->iECursor, regBase+nExpr);
125781 }
125782 if( nPrefixReg==0 && nData>0 ){
125783 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData);
125784 }
125785 if( nOBSat>0 ){
125786 int regPrevKey; /* The first nOBSat columns of the previous row */
125787 int addrFirst; /* Address of the OP_IfNot opcode */
125788 int addrJmp; /* Address of the OP_Jump opcode */
125789 VdbeOp *pOp; /* Opcode that opens the sorter */
125790 int nKey; /* Number of sorting key columns, including OP_Sequence */
125791 KeyInfo *pKI; /* Original KeyInfo on the sorter table */
125792
125793 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
125794 regPrevKey = pParse->nMem+1;
125795 pParse->nMem += pSort->nOBSat;
125796 nKey = nExpr - pSort->nOBSat + bSeq;
125797 if( bSeq ){
125798 addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot20, regBase+nExpr);
125799 }else{
125800 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest115, pSort->iECursor);
125801 }
125802 VdbeCoverage(v);
125803 sqlite3VdbeAddOp3(v, OP_Compare87, regPrevKey, regBase, pSort->nOBSat);
125804 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
125805 if( pParse->db->mallocFailed ) return;
125806 pOp->p2 = nKey + nData;
125807 pKI = pOp->p4.pKeyInfo;
125808 memset(pKI->aSortOrder, 0, pKI->nKeyField); /* Makes OP_Jump testable */
125809 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO(-9));
125810 testcase( pKI->nAllField > pKI->nKeyField+2 );
125811 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
125812 pKI->nAllField-pKI->nKeyField-1);
125813 addrJmp = sqlite3VdbeCurrentAddr(v);
125814 sqlite3VdbeAddOp3(v, OP_Jump16, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
125815 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
125816 pSort->regReturn = ++pParse->nMem;
125817 sqlite3VdbeAddOp2(v, OP_Gosub12, pSort->regReturn, pSort->labelBkOut);
125818 sqlite3VdbeAddOp1(v, OP_ResetSorter138, pSort->iECursor);
125819 if( iLimit ){
125820 sqlite3VdbeAddOp2(v, OP_IfNot20, iLimit, pSort->labelDone);
125821 VdbeCoverage(v);
125822 }
125823 sqlite3VdbeJumpHere(v, addrFirst);
125824 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
125825 sqlite3VdbeJumpHere(v, addrJmp);
125826 }
125827 if( iLimit ){
125828 /* At this point the values for the new sorter entry are stored
125829 ** in an array of registers. They need to be composed into a record
125830 ** and inserted into the sorter if either (a) there are currently
125831 ** less than LIMIT+OFFSET items or (b) the new record is smaller than
125832 ** the largest record currently in the sorter. If (b) is true and there
125833 ** are already LIMIT+OFFSET items in the sorter, delete the largest
125834 ** entry before inserting the new one. This way there are never more
125835 ** than LIMIT+OFFSET items in the sorter.
125836 **
125837 ** If the new record does not need to be inserted into the sorter,
125838 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
125839 ** value is not zero, then it is a label of where to jump. Otherwise,
125840 ** just bypass the row insert logic. See the header comment on the
125841 ** sqlite3WhereOrderByLimitOptLabel() function for additional info.
125842 */
125843 int iCsr = pSort->iECursor;
125844 sqlite3VdbeAddOp2(v, OP_IfNotZero48, iLimit, sqlite3VdbeCurrentAddr(v)+4);
125845 VdbeCoverage(v);
125846 sqlite3VdbeAddOp2(v, OP_Last32, iCsr, 0);
125847 iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE37,
125848 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
125849 VdbeCoverage(v);
125850 sqlite3VdbeAddOp1(v, OP_Delete123, iCsr);
125851 }
125852 if( regRecord==0 ){
125853 regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase);
125854 }
125855 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
125856 op = OP_SorterInsert131;
125857 }else{
125858 op = OP_IdxInsert132;
125859 }
125860 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
125861 regBase+nOBSat, nBase-nOBSat);
125862 if( iSkip ){
125863 sqlite3VdbeChangeP2(v, iSkip,
125864 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
125865 }
125866}
125867
125868/*
125869** Add code to implement the OFFSET
125870*/
125871static void codeOffset(
125872 Vdbe *v, /* Generate code into this VM */
125873 int iOffset, /* Register holding the offset counter */
125874 int iContinue /* Jump here to skip the current record */
125875){
125876 if( iOffset>0 ){
125877 sqlite3VdbeAddOp3(v, OP_IfPos47, iOffset, iContinue, 1); VdbeCoverage(v);
125878 VdbeComment((v, "OFFSET"));
125879 }
125880}
125881
125882/*
125883** Add code that will check to make sure the N registers starting at iMem
125884** form a distinct entry. iTab is a sorting index that holds previously
125885** seen combinations of the N values. A new entry is made in iTab
125886** if the current N values are new.
125887**
125888** A jump to addrRepeat is made and the N+1 values are popped from the
125889** stack if the top N elements are not distinct.
125890*/
125891static void codeDistinct(
125892 Parse *pParse, /* Parsing and code generating context */
125893 int iTab, /* A sorting index used to test for distinctness */
125894 int addrRepeat, /* Jump to here if not distinct */
125895 int N, /* Number of elements */
125896 int iMem /* First element */
125897){
125898 Vdbe *v;
125899 int r1;
125900
125901 v = pParse->pVdbe;
125902 r1 = sqlite3GetTempReg(pParse);
125903 sqlite3VdbeAddOp4Int(v, OP_Found29, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
125904 sqlite3VdbeAddOp3(v, OP_MakeRecord92, iMem, N, r1);
125905 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iTab, r1, iMem, N);
125906 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
125907 sqlite3ReleaseTempReg(pParse, r1);
125908}
125909
125910#ifdef SQLITE_ENABLE_SORTER_REFERENCES
125911/*
125912** This function is called as part of inner-loop generation for a SELECT
125913** statement with an ORDER BY that is not optimized by an index. It
125914** determines the expressions, if any, that the sorter-reference
125915** optimization should be used for. The sorter-reference optimization
125916** is used for SELECT queries like:
125917**
125918** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10
125919**
125920** If the optimization is used for expression "bigblob", then instead of
125921** storing values read from that column in the sorter records, the PK of
125922** the row from table t1 is stored instead. Then, as records are extracted from
125923** the sorter to return to the user, the required value of bigblob is
125924** retrieved directly from table t1. If the values are very large, this
125925** can be more efficient than storing them directly in the sorter records.
125926**
125927** The ExprList_item.bSorterRef flag is set for each expression in pEList
125928** for which the sorter-reference optimization should be enabled.
125929** Additionally, the pSort->aDefer[] array is populated with entries
125930** for all cursors required to evaluate all selected expressions. Finally.
125931** output variable (*ppExtra) is set to an expression list containing
125932** expressions for all extra PK values that should be stored in the
125933** sorter records.
125934*/
125935static void selectExprDefer(
125936 Parse *pParse, /* Leave any error here */
125937 SortCtx *pSort, /* Sorter context */
125938 ExprList *pEList, /* Expressions destined for sorter */
125939 ExprList **ppExtra /* Expressions to append to sorter record */
125940){
125941 int i;
125942 int nDefer = 0;
125943 ExprList *pExtra = 0;
125944 for(i=0; i<pEList->nExpr; i++){
125945 struct ExprList_item *pItem = &pEList->a[i];
125946 if( pItem->u.x.iOrderByCol==0 ){
125947 Expr *pExpr = pItem->pExpr;
125948 Table *pTab = pExpr->y.pTab;
125949 if( pExpr->op==TK_COLUMN162 && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)((pTab)->nModuleArg)
125950 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF0x0010)
125951 ){
125952 int j;
125953 for(j=0; j<nDefer; j++){
125954 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
125955 }
125956 if( j==nDefer ){
125957 if( nDefer==ArraySize(pSort->aDefer)((int)(sizeof(pSort->aDefer)/sizeof(pSort->aDefer[0]))) ){
125958 continue;
125959 }else{
125960 int nKey = 1;
125961 int k;
125962 Index *pPk = 0;
125963 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
125964 pPk = sqlite3PrimaryKeyIndex(pTab);
125965 nKey = pPk->nKeyCol;
125966 }
125967 for(k=0; k<nKey; k++){
125968 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN162, 0, 0);
125969 if( pNew ){
125970 pNew->iTable = pExpr->iTable;
125971 pNew->y.pTab = pExpr->y.pTab;
125972 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
125973 pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
125974 }
125975 }
125976 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
125977 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
125978 pSort->aDefer[nDefer].nKey = nKey;
125979 nDefer++;
125980 }
125981 }
125982 pItem->bSorterRef = 1;
125983 }
125984 }
125985 }
125986 pSort->nDefer = (u8)nDefer;
125987 *ppExtra = pExtra;
125988}
125989#endif
125990
125991/*
125992** This routine generates the code for the inside of the inner loop
125993** of a SELECT.
125994**
125995** If srcTab is negative, then the p->pEList expressions
125996** are evaluated in order to get the data for this row. If srcTab is
125997** zero or more, then data is pulled from srcTab and p->pEList is used only
125998** to get the number of columns and the collation sequence for each column.
125999*/
126000static void selectInnerLoop(
126001 Parse *pParse, /* The parser context */
126002 Select *p, /* The complete select statement being coded */
126003 int srcTab, /* Pull data from this table if non-negative */
126004 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */
126005 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
126006 SelectDest *pDest, /* How to dispose of the results */
126007 int iContinue, /* Jump here to continue with next row */
126008 int iBreak /* Jump here to break out of the inner loop */
126009){
126010 Vdbe *v = pParse->pVdbe;
126011 int i;
126012 int hasDistinct; /* True if the DISTINCT keyword is present */
126013 int eDest = pDest->eDest; /* How to dispose of results */
126014 int iParm = pDest->iSDParm; /* First argument to disposal method */
126015 int nResultCol; /* Number of result columns */
126016 int nPrefixReg = 0; /* Number of extra registers before regResult */
126017 RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */
126018
126019 /* Usually, regResult is the first cell in an array of memory cells
126020 ** containing the current result row. In this case regOrig is set to the
126021 ** same value. However, if the results are being sent to the sorter, the
126022 ** values for any expressions that are also part of the sort-key are omitted
126023 ** from this array. In this case regOrig is set to zero. */
126024 int regResult; /* Start of memory holding current results */
126025 int regOrig; /* Start of memory holding full result (or 0) */
126026
126027 assert( v )((void) (0));
126028 assert( p->pEList!=0 )((void) (0));
126029 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP0;
126030 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
126031 if( pSort==0 && !hasDistinct ){
126032 assert( iContinue!=0 )((void) (0));
126033 codeOffset(v, p->iOffset, iContinue);
126034 }
126035
126036 /* Pull the requested columns.
126037 */
126038 nResultCol = p->pEList->nExpr;
126039
126040 if( pDest->iSdst==0 ){
126041 if( pSort ){
126042 nPrefixReg = pSort->pOrderBy->nExpr;
126043 if( !(pSort->sortFlags & SORTFLAG_UseSorter0x01) ) nPrefixReg++;
126044 pParse->nMem += nPrefixReg;
126045 }
126046 pDest->iSdst = pParse->nMem+1;
126047 pParse->nMem += nResultCol;
126048 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
126049 /* This is an error condition that can result, for example, when a SELECT
126050 ** on the right-hand side of an INSERT contains more result columns than
126051 ** there are columns in the table on the left. The error will be caught
126052 ** and reported later. But we need to make sure enough memory is allocated
126053 ** to avoid other spurious errors in the meantime. */
126054 pParse->nMem += nResultCol;
126055 }
126056 pDest->nSdst = nResultCol;
126057 regOrig = regResult = pDest->iSdst;
126058 if( srcTab>=0 ){
126059 for(i=0; i<nResultCol; i++){
126060 sqlite3VdbeAddOp3(v, OP_Column90, srcTab, i, regResult+i);
126061 VdbeComment((v, "%s", p->pEList->a[i].zName));
126062 }
126063 }else if( eDest!=SRT_Exists3 ){
126064#ifdef SQLITE_ENABLE_SORTER_REFERENCES
126065 ExprList *pExtra = 0;
126066#endif
126067 /* If the destination is an EXISTS(...) expression, the actual
126068 ** values returned by the SELECT are not required.
126069 */
126070 u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */
126071 ExprList *pEList;
126072 if( eDest==SRT_Mem10 || eDest==SRT_Output9 || eDest==SRT_Coroutine13 ){
126073 ecelFlags = SQLITE_ECEL_DUP0x01;
126074 }else{
126075 ecelFlags = 0;
126076 }
126077 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab12 && eDest!=SRT_Table14 ){
126078 /* For each expression in p->pEList that is a copy of an expression in
126079 ** the ORDER BY clause (pSort->pOrderBy), set the associated
126080 ** iOrderByCol value to one more than the index of the ORDER BY
126081 ** expression within the sort-key that pushOntoSorter() will generate.
126082 ** This allows the p->pEList field to be omitted from the sorted record,
126083 ** saving space and CPU cycles. */
126084 ecelFlags |= (SQLITE_ECEL_OMITREF0x08|SQLITE_ECEL_REF0x04);
126085
126086 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
126087 int j;
126088 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
126089 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
126090 }
126091 }
126092#ifdef SQLITE_ENABLE_SORTER_REFERENCES
126093 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
126094 if( pExtra && pParse->db->mallocFailed==0 ){
126095 /* If there are any extra PK columns to add to the sorter records,
126096 ** allocate extra memory cells and adjust the OpenEphemeral
126097 ** instruction to account for the larger records. This is only
126098 ** required if there are one or more WITHOUT ROWID tables with
126099 ** composite primary keys in the SortCtx.aDefer[] array. */
126100 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
126101 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
126102 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
126103 pParse->nMem += pExtra->nExpr;
126104 }
126105#endif
126106
126107 /* Adjust nResultCol to account for columns that are omitted
126108 ** from the sorter by the optimizations in this branch */
126109 pEList = p->pEList;
126110 for(i=0; i<pEList->nExpr; i++){
126111 if( pEList->a[i].u.x.iOrderByCol>0
126112#ifdef SQLITE_ENABLE_SORTER_REFERENCES
126113 || pEList->a[i].bSorterRef
126114#endif
126115 ){
126116 nResultCol--;
126117 regOrig = 0;
126118 }
126119 }
126120
126121 testcase( regOrig );
126122 testcase( eDest==SRT_Set );
126123 testcase( eDest==SRT_Mem );
126124 testcase( eDest==SRT_Coroutine );
126125 testcase( eDest==SRT_Output );
126126 assert( eDest==SRT_Set || eDest==SRT_Mem((void) (0))
126127 || eDest==SRT_Coroutine || eDest==SRT_Output )((void) (0));
126128 }
126129 sRowLoadInfo.regResult = regResult;
126130 sRowLoadInfo.ecelFlags = ecelFlags;
126131#ifdef SQLITE_ENABLE_SORTER_REFERENCES
126132 sRowLoadInfo.pExtra = pExtra;
126133 sRowLoadInfo.regExtraResult = regResult + nResultCol;
126134 if( pExtra ) nResultCol += pExtra->nExpr;
126135#endif
126136 if( p->iLimit
126137 && (ecelFlags & SQLITE_ECEL_OMITREF0x08)!=0
126138 && nPrefixReg>0
126139 ){
126140 assert( pSort!=0 )((void) (0));
126141 assert( hasDistinct==0 )((void) (0));
126142 pSort->pDeferredRowLoad = &sRowLoadInfo;
126143 regOrig = 0;
126144 }else{
126145 innerLoopLoadRow(pParse, p, &sRowLoadInfo);
126146 }
126147 }
126148
126149 /* If the DISTINCT keyword was present on the SELECT statement
126150 ** and this row has been seen before, then do not make this row
126151 ** part of the result.
126152 */
126153 if( hasDistinct ){
126154 switch( pDistinct->eTnctType ){
126155 case WHERE_DISTINCT_ORDERED2: {
126156 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
126157 int iJump; /* Jump destination */
126158 int regPrev; /* Previous row content */
126159
126160 /* Allocate space for the previous row */
126161 regPrev = pParse->nMem+1;
126162 pParse->nMem += nResultCol;
126163
126164 /* Change the OP_OpenEphemeral coded earlier to an OP_Null
126165 ** sets the MEM_Cleared bit on the first register of the
126166 ** previous value. This will cause the OP_Ne below to always
126167 ** fail on the first iteration of the loop even if the first
126168 ** row is all NULLs.
126169 */
126170 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
126171 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
126172 pOp->opcode = OP_Null73;
126173 pOp->p1 = 1;
126174 pOp->p2 = regPrev;
126175
126176 iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
126177 for(i=0; i<nResultCol; i++){
126178 CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
126179 if( i<nResultCol-1 ){
126180 sqlite3VdbeAddOp3(v, OP_Ne52, regResult+i, iJump, regPrev+i);
126181 VdbeCoverage(v);
126182 }else{
126183 sqlite3VdbeAddOp3(v, OP_Eq53, regResult+i, iContinue, regPrev+i);
126184 VdbeCoverage(v);
126185 }
126186 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ(-2));
126187 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
126188 }
126189 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed )((void) (0));
126190 sqlite3VdbeAddOp3(v, OP_Copy78, regResult, regPrev, nResultCol-1);
126191 break;
126192 }
126193
126194 case WHERE_DISTINCT_UNIQUE1: {
126195 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
126196 break;
126197 }
126198
126199 default: {
126200 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED )((void) (0));
126201 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
126202 regResult);
126203 break;
126204 }
126205 }
126206 if( pSort==0 ){
126207 codeOffset(v, p->iOffset, iContinue);
126208 }
126209 }
126210
126211 switch( eDest ){
126212 /* In this mode, write each query result to the key of the temporary
126213 ** table iParm.
126214 */
126215#ifndef SQLITE_OMIT_COMPOUND_SELECT
126216 case SRT_Union1: {
126217 int r1;
126218 r1 = sqlite3GetTempReg(pParse);
126219 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regResult, nResultCol, r1);
126220 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iParm, r1, regResult, nResultCol);
126221 sqlite3ReleaseTempReg(pParse, r1);
126222 break;
126223 }
126224
126225 /* Construct a record from the query result, but instead of
126226 ** saving that record, use it as a key to delete elements from
126227 ** the temporary table iParm.
126228 */
126229 case SRT_Except2: {
126230 sqlite3VdbeAddOp3(v, OP_IdxDelete133, iParm, regResult, nResultCol);
126231 break;
126232 }
126233#endif /* SQLITE_OMIT_COMPOUND_SELECT */
126234
126235 /* Store the result as data using a unique key.
126236 */
126237 case SRT_Fifo5:
126238 case SRT_DistFifo6:
126239 case SRT_Table14:
126240 case SRT_EphemTab12: {
126241 int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1);
126242 testcase( eDest==SRT_Table );
126243 testcase( eDest==SRT_EphemTab );
126244 testcase( eDest==SRT_Fifo );
126245 testcase( eDest==SRT_DistFifo );
126246 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regResult, nResultCol, r1+nPrefixReg);
126247#ifndef SQLITE_OMIT_CTE
126248 if( eDest==SRT_DistFifo6 ){
126249 /* If the destination is DistFifo, then cursor (iParm+1) is open
126250 ** on an ephemeral index. If the current row is already present
126251 ** in the index, do not write it to the output. If not, add the
126252 ** current row to the index and proceed with writing it to the
126253 ** output table as well. */
126254 int addr = sqlite3VdbeCurrentAddr(v) + 4;
126255 sqlite3VdbeAddOp4Int(v, OP_Found29, iParm+1, addr, r1, 0);
126256 VdbeCoverage(v);
126257 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iParm+1, r1,regResult,nResultCol);
126258 assert( pSort==0 )((void) (0));
126259 }
126260#endif
126261 if( pSort ){
126262 assert( regResult==regOrig )((void) (0));
126263 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg);
126264 }else{
126265 int r2 = sqlite3GetTempReg(pParse);
126266 sqlite3VdbeAddOp2(v, OP_NewRowid121, iParm, r2);
126267 sqlite3VdbeAddOp3(v, OP_Insert122, iParm, r1, r2);
126268 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
126269 sqlite3ReleaseTempReg(pParse, r2);
126270 }
126271 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1);
126272 break;
126273 }
126274
126275#ifndef SQLITE_OMIT_SUBQUERY
126276 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
126277 ** then there should be a single item on the stack. Write this
126278 ** item into the set table with bogus data.
126279 */
126280 case SRT_Set11: {
126281 if( pSort ){
126282 /* At first glance you would think we could optimize out the
126283 ** ORDER BY in this case since the order of entries in the set
126284 ** does not matter. But there might be a LIMIT clause, in which
126285 ** case the order does matter */
126286 pushOntoSorter(
126287 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
126288 }else{
126289 int r1 = sqlite3GetTempReg(pParse);
126290 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol )((void) (0));
126291 sqlite3VdbeAddOp4(v, OP_MakeRecord92, regResult, nResultCol,
126292 r1, pDest->zAffSdst, nResultCol);
126293 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iParm, r1, regResult, nResultCol);
126294 sqlite3ReleaseTempReg(pParse, r1);
126295 }
126296 break;
126297 }
126298
126299 /* If any row exist in the result set, record that fact and abort.
126300 */
126301 case SRT_Exists3: {
126302 sqlite3VdbeAddOp2(v, OP_Integer70, 1, iParm);
126303 /* The LIMIT clause will terminate the loop for us */
126304 break;
126305 }
126306
126307 /* If this is a scalar select that is part of an expression, then
126308 ** store the results in the appropriate memory cell or array of
126309 ** memory cells and break out of the scan loop.
126310 */
126311 case SRT_Mem10: {
126312 if( pSort ){
126313 assert( nResultCol<=pDest->nSdst )((void) (0));
126314 pushOntoSorter(
126315 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg);
126316 }else{
126317 assert( nResultCol==pDest->nSdst )((void) (0));
126318 assert( regResult==iParm )((void) (0));
126319 /* The LIMIT clause will jump out of the loop for us */
126320 }
126321 break;
126322 }
126323#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
126324
126325 case SRT_Coroutine13: /* Send data to a co-routine */
126326 case SRT_Output9: { /* Return the results */
126327 testcase( eDest==SRT_Coroutine );
126328 testcase( eDest==SRT_Output );
126329 if( pSort ){
126330 pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol,
126331 nPrefixReg);
126332 }else if( eDest==SRT_Coroutine13 ){
126333 sqlite3VdbeAddOp1(v, OP_Yield14, pDest->iSDParm);
126334 }else{
126335 sqlite3VdbeAddOp2(v, OP_ResultRow81, regResult, nResultCol);
126336 }
126337 break;
126338 }
126339
126340#ifndef SQLITE_OMIT_CTE
126341 /* Write the results into a priority queue that is order according to
126342 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
126343 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
126344 ** pSO->nExpr columns, then make sure all keys are unique by adding a
126345 ** final OP_Sequence column. The last column is the record as a blob.
126346 */
126347 case SRT_DistQueue8:
126348 case SRT_Queue7: {
126349 int nKey;
126350 int r1, r2, r3;
126351 int addrTest = 0;
126352 ExprList *pSO;
126353 pSO = pDest->pOrderBy;
126354 assert( pSO )((void) (0));
126355 nKey = pSO->nExpr;
126356 r1 = sqlite3GetTempReg(pParse);
126357 r2 = sqlite3GetTempRange(pParse, nKey+2);
126358 r3 = r2+nKey+1;
126359 if( eDest==SRT_DistQueue8 ){
126360 /* If the destination is DistQueue, then cursor (iParm+1) is open
126361 ** on a second ephemeral index that holds all values every previously
126362 ** added to the queue. */
126363 addrTest = sqlite3VdbeAddOp4Int(v, OP_Found29, iParm+1, 0,
126364 regResult, nResultCol);
126365 VdbeCoverage(v);
126366 }
126367 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regResult, nResultCol, r3);
126368 if( eDest==SRT_DistQueue8 ){
126369 sqlite3VdbeAddOp2(v, OP_IdxInsert132, iParm+1, r3);
126370 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
126371 }
126372 for(i=0; i<nKey; i++){
126373 sqlite3VdbeAddOp2(v, OP_SCopy79,
126374 regResult + pSO->a[i].u.x.iOrderByCol - 1,
126375 r2+i);
126376 }
126377 sqlite3VdbeAddOp2(v, OP_Sequence120, iParm, r2+nKey);
126378 sqlite3VdbeAddOp3(v, OP_MakeRecord92, r2, nKey+2, r1);
126379 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iParm, r1, r2, nKey+2);
126380 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
126381 sqlite3ReleaseTempReg(pParse, r1);
126382 sqlite3ReleaseTempRange(pParse, r2, nKey+2);
126383 break;
126384 }
126385#endif /* SQLITE_OMIT_CTE */
126386
126387
126388
126389#if !defined(SQLITE_OMIT_TRIGGER)
126390 /* Discard the results. This is used for SELECT statements inside
126391 ** the body of a TRIGGER. The purpose of such selects is to call
126392 ** user-defined functions that have side effects. We do not care
126393 ** about the actual results of the select.
126394 */
126395 default: {
126396 assert( eDest==SRT_Discard )((void) (0));
126397 break;
126398 }
126399#endif
126400 }
126401
126402 /* Jump to the end of the loop if the LIMIT is reached. Except, if
126403 ** there is a sorter, in which case the sorter has already limited
126404 ** the output for us.
126405 */
126406 if( pSort==0 && p->iLimit ){
126407 sqlite3VdbeAddOp2(v, OP_DecrJumpZero49, p->iLimit, iBreak); VdbeCoverage(v);
126408 }
126409}
126410
126411/*
126412** Allocate a KeyInfo object sufficient for an index of N key columns and
126413** X extra columns.
126414*/
126415SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
126416 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
126417 KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
126418 if( p ){
126419 p->aSortOrder = (u8*)&p->aColl[N+X];
126420 p->nKeyField = (u16)N;
126421 p->nAllField = (u16)(N+X);
126422 p->enc = ENC(db)((db)->enc);
126423 p->db = db;
126424 p->nRef = 1;
126425 memset(&p[1], 0, nExtra);
126426 }else{
126427 sqlite3OomFault(db);
126428 }
126429 return p;
126430}
126431
126432/*
126433** Deallocate a KeyInfo object
126434*/
126435SQLITE_PRIVATEstatic void sqlite3KeyInfoUnref(KeyInfo *p){
126436 if( p ){
126437 assert( p->nRef>0 )((void) (0));
126438 p->nRef--;
126439 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
126440 }
126441}
126442
126443/*
126444** Make a new pointer to a KeyInfo object
126445*/
126446SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
126447 if( p ){
126448 assert( p->nRef>0 )((void) (0));
126449 p->nRef++;
126450 }
126451 return p;
126452}
126453
126454#ifdef SQLITE_DEBUG
126455/*
126456** Return TRUE if a KeyInfo object can be change. The KeyInfo object
126457** can only be changed if this is just a single reference to the object.
126458**
126459** This routine is used only inside of assert() statements.
126460*/
126461SQLITE_PRIVATEstatic int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
126462#endif /* SQLITE_DEBUG */
126463
126464/*
126465** Given an expression list, generate a KeyInfo structure that records
126466** the collating sequence for each expression in that expression list.
126467**
126468** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
126469** KeyInfo structure is appropriate for initializing a virtual index to
126470** implement that clause. If the ExprList is the result set of a SELECT
126471** then the KeyInfo structure is appropriate for initializing a virtual
126472** index to implement a DISTINCT test.
126473**
126474** Space to hold the KeyInfo structure is obtained from malloc. The calling
126475** function is responsible for seeing that this structure is eventually
126476** freed.
126477*/
126478SQLITE_PRIVATEstatic KeyInfo *sqlite3KeyInfoFromExprList(
126479 Parse *pParse, /* Parsing context */
126480 ExprList *pList, /* Form the KeyInfo object from this ExprList */
126481 int iStart, /* Begin with this column of pList */
126482 int nExtra /* Add this many extra columns to the end */
126483){
126484 int nExpr;
126485 KeyInfo *pInfo;
126486 struct ExprList_item *pItem;
126487 sqlite3 *db = pParse->db;
126488 int i;
126489
126490 nExpr = pList->nExpr;
126491 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
126492 if( pInfo ){
126493 assert( sqlite3KeyInfoIsWriteable(pInfo) )((void) (0));
126494 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
126495 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
126496 pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
126497 }
126498 }
126499 return pInfo;
126500}
126501
126502/*
126503** Name of the connection operator, used for error messages.
126504*/
126505static const char *selectOpName(int id){
126506 char *z;
126507 switch( id ){
126508 case TK_ALL128: z = "UNION ALL"; break;
126509 case TK_INTERSECT130: z = "INTERSECT"; break;
126510 case TK_EXCEPT129: z = "EXCEPT"; break;
126511 default: z = "UNION"; break;
126512 }
126513 return z;
126514}
126515
126516#ifndef SQLITE_OMIT_EXPLAIN
126517/*
126518** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
126519** is a no-op. Otherwise, it adds a single row of output to the EQP result,
126520** where the caption is of the form:
126521**
126522** "USE TEMP B-TREE FOR xxx"
126523**
126524** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
126525** is determined by the zUsage argument.
126526*/
126527static void explainTempTable(Parse *pParse, const char *zUsage){
126528 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage))sqlite3VdbeExplain (pParse, 0, "USE TEMP B-TREE FOR %s", zUsage
)
;
126529}
126530
126531/*
126532** Assign expression b to lvalue a. A second, no-op, version of this macro
126533** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
126534** in sqlite3Select() to assign values to structure member variables that
126535** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
126536** code with #ifndef directives.
126537*/
126538# define explainSetInteger(a, b)a = b a = b
126539
126540#else
126541/* No-op versions of the explainXXX() functions and macros. */
126542# define explainTempTable(y,z)
126543# define explainSetInteger(y,z)y = z
126544#endif
126545
126546
126547/*
126548** If the inner loop was generated using a non-null pOrderBy argument,
126549** then the results were placed in a sorter. After the loop is terminated
126550** we need to run the sorter and output the results. The following
126551** routine generates the code needed to do that.
126552*/
126553static void generateSortTail(
126554 Parse *pParse, /* Parsing context */
126555 Select *p, /* The SELECT statement */
126556 SortCtx *pSort, /* Information on the ORDER BY clause */
126557 int nColumn, /* Number of columns of data */
126558 SelectDest *pDest /* Write the sorted results here */
126559){
126560 Vdbe *v = pParse->pVdbe; /* The prepared statement */
126561 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
126562 int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */
126563 int addr; /* Top of output loop. Jump for Next. */
126564 int addrOnce = 0;
126565 int iTab;
126566 ExprList *pOrderBy = pSort->pOrderBy;
126567 int eDest = pDest->eDest;
126568 int iParm = pDest->iSDParm;
126569 int regRow;
126570 int regRowid;
126571 int iCol;
126572 int nKey; /* Number of key columns in sorter record */
126573 int iSortTab; /* Sorter cursor to read from */
126574 int i;
126575 int bSeq; /* True if sorter record includes seq. no. */
126576 int nRefKey = 0;
126577 struct ExprList_item *aOutEx = p->pEList->a;
126578
126579 assert( addrBreak<0 )((void) (0));
126580 if( pSort->labelBkOut ){
126581 sqlite3VdbeAddOp2(v, OP_Gosub12, pSort->regReturn, pSort->labelBkOut);
126582 sqlite3VdbeGoto(v, addrBreak);
126583 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
126584 }
126585
126586#ifdef SQLITE_ENABLE_SORTER_REFERENCES
126587 /* Open any cursors needed for sorter-reference expressions */
126588 for(i=0; i<pSort->nDefer; i++){
126589 Table *pTab = pSort->aDefer[i].pTab;
126590 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
126591 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead108);
126592 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey)((nRefKey)>(pSort->aDefer[i].nKey)?(nRefKey):(pSort->
aDefer[i].nKey))
;
126593 }
126594#endif
126595
126596 iTab = pSort->iECursor;
126597 if( eDest==SRT_Output9 || eDest==SRT_Coroutine13 || eDest==SRT_Mem10 ){
126598 regRowid = 0;
126599 regRow = pDest->iSdst;
126600 }else{
126601 regRowid = sqlite3GetTempReg(pParse);
126602 if( eDest==SRT_EphemTab12 || eDest==SRT_Table14 ){
126603 regRow = sqlite3GetTempReg(pParse);
126604 nColumn = 0;
126605 }else{
126606 regRow = sqlite3GetTempRange(pParse, nColumn);
126607 }
126608 }
126609 nKey = pOrderBy->nExpr - pSort->nOBSat;
126610 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
126611 int regSortOut = ++pParse->nMem;
126612 iSortTab = pParse->nTab++;
126613 if( pSort->labelBkOut ){
126614 addrOnce = sqlite3VdbeAddOp0(v, OP_Once17); VdbeCoverage(v);
126615 }
126616 sqlite3VdbeAddOp3(v, OP_OpenPseudo116, iSortTab, regSortOut,
126617 nKey+1+nColumn+nRefKey);
126618 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
126619 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort34, iTab, addrBreak);
126620 VdbeCoverage(v);
126621 codeOffset(v, p->iOffset, addrContinue);
126622 sqlite3VdbeAddOp3(v, OP_SorterData126, iTab, regSortOut, iSortTab);
126623 bSeq = 0;
126624 }else{
126625 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort35, iTab, addrBreak); VdbeCoverage(v);
126626 codeOffset(v, p->iOffset, addrContinue);
126627 iSortTab = iTab;
126628 bSeq = 1;
126629 }
126630 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
126631#ifdef SQLITE_ENABLE_SORTER_REFERENCES
126632 if( aOutEx[i].bSorterRef ) continue;
126633#endif
126634 if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++;
126635 }
126636#ifdef SQLITE_ENABLE_SORTER_REFERENCES
126637 if( pSort->nDefer ){
126638 int iKey = iCol+1;
126639 int regKey = sqlite3GetTempRange(pParse, nRefKey);
126640
126641 for(i=0; i<pSort->nDefer; i++){
126642 int iCsr = pSort->aDefer[i].iCsr;
126643 Table *pTab = pSort->aDefer[i].pTab;
126644 int nKey = pSort->aDefer[i].nKey;
126645
126646 sqlite3VdbeAddOp1(v, OP_NullRow129, iCsr);
126647 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
126648 sqlite3VdbeAddOp3(v, OP_Column90, iSortTab, iKey++, regKey);
126649 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iCsr,
126650 sqlite3VdbeCurrentAddr(v)+1, regKey);
126651 }else{
126652 int k;
126653 int iJmp;
126654 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey )((void) (0));
126655 for(k=0; k<nKey; k++){
126656 sqlite3VdbeAddOp3(v, OP_Column90, iSortTab, iKey++, regKey+k);
126657 }
126658 iJmp = sqlite3VdbeCurrentAddr(v);
126659 sqlite3VdbeAddOp4Int(v, OP_SeekGE24, iCsr, iJmp+2, regKey, nKey);
126660 sqlite3VdbeAddOp4Int(v, OP_IdxLE37, iCsr, iJmp+3, regKey, nKey);
126661 sqlite3VdbeAddOp1(v, OP_NullRow129, iCsr);
126662 }
126663 }
126664 sqlite3ReleaseTempRange(pParse, regKey, nRefKey);
126665 }
126666#endif
126667 for(i=nColumn-1; i>=0; i--){
126668#ifdef SQLITE_ENABLE_SORTER_REFERENCES
126669 if( aOutEx[i].bSorterRef ){
126670 sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i);
126671 }else
126672#endif
126673 {
126674 int iRead;
126675 if( aOutEx[i].u.x.iOrderByCol ){
126676 iRead = aOutEx[i].u.x.iOrderByCol-1;
126677 }else{
126678 iRead = iCol--;
126679 }
126680 sqlite3VdbeAddOp3(v, OP_Column90, iSortTab, iRead, regRow+i);
126681 VdbeComment((v, "%s", aOutEx[i].zName?aOutEx[i].zName : aOutEx[i].zSpan));
126682 }
126683 }
126684 switch( eDest ){
126685 case SRT_Table14:
126686 case SRT_EphemTab12: {
126687 sqlite3VdbeAddOp3(v, OP_Column90, iSortTab, nKey+bSeq, regRow);
126688 sqlite3VdbeAddOp2(v, OP_NewRowid121, iParm, regRowid);
126689 sqlite3VdbeAddOp3(v, OP_Insert122, iParm, regRow, regRowid);
126690 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
126691 break;
126692 }
126693#ifndef SQLITE_OMIT_SUBQUERY
126694 case SRT_Set11: {
126695 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) )((void) (0));
126696 sqlite3VdbeAddOp4(v, OP_MakeRecord92, regRow, nColumn, regRowid,
126697 pDest->zAffSdst, nColumn);
126698 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iParm, regRowid, regRow, nColumn);
126699 break;
126700 }
126701 case SRT_Mem10: {
126702 /* The LIMIT clause will terminate the loop for us */
126703 break;
126704 }
126705#endif
126706 default: {
126707 assert( eDest==SRT_Output || eDest==SRT_Coroutine )((void) (0));
126708 testcase( eDest==SRT_Output );
126709 testcase( eDest==SRT_Coroutine );
126710 if( eDest==SRT_Output9 ){
126711 sqlite3VdbeAddOp2(v, OP_ResultRow81, pDest->iSdst, nColumn);
126712 }else{
126713 sqlite3VdbeAddOp1(v, OP_Yield14, pDest->iSDParm);
126714 }
126715 break;
126716 }
126717 }
126718 if( regRowid ){
126719 if( eDest==SRT_Set11 ){
126720 sqlite3ReleaseTempRange(pParse, regRow, nColumn);
126721 }else{
126722 sqlite3ReleaseTempReg(pParse, regRow);
126723 }
126724 sqlite3ReleaseTempReg(pParse, regRowid);
126725 }
126726 /* The bottom of the loop
126727 */
126728 sqlite3VdbeResolveLabel(v, addrContinue);
126729 if( pSort->sortFlags & SORTFLAG_UseSorter0x01 ){
126730 sqlite3VdbeAddOp2(v, OP_SorterNext3, iTab, addr); VdbeCoverage(v);
126731 }else{
126732 sqlite3VdbeAddOp2(v, OP_Next5, iTab, addr); VdbeCoverage(v);
126733 }
126734 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return66, pSort->regReturn);
126735 sqlite3VdbeResolveLabel(v, addrBreak);
126736}
126737
126738/*
126739** Return a pointer to a string containing the 'declaration type' of the
126740** expression pExpr. The string may be treated as static by the caller.
126741**
126742** Also try to estimate the size of the returned value and return that
126743** result in *pEstWidth.
126744**
126745** The declaration type is the exact datatype definition extracted from the
126746** original CREATE TABLE statement if the expression is a column. The
126747** declaration type for a ROWID field is INTEGER. Exactly when an expression
126748** is considered a column can be complex in the presence of subqueries. The
126749** result-set expression in all of the following SELECT statements is
126750** considered a column by this function.
126751**
126752** SELECT col FROM tbl;
126753** SELECT (SELECT col FROM tbl;
126754** SELECT (SELECT col FROM tbl);
126755** SELECT abc FROM (SELECT col AS abc FROM tbl);
126756**
126757** The declaration type for any expression other than a column is NULL.
126758**
126759** This routine has either 3 or 6 parameters depending on whether or not
126760** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
126761*/
126762#ifdef SQLITE_ENABLE_COLUMN_METADATA
126763# define columnType(A,B,C,D,E)columnTypeImpl(A,B) columnTypeImpl(A,B,C,D,E)
126764#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
126765# define columnType(A,B,C,D,E)columnTypeImpl(A,B) columnTypeImpl(A,B)
126766#endif
126767static const char *columnTypeImpl(
126768 NameContext *pNC,
126769#ifndef SQLITE_ENABLE_COLUMN_METADATA
126770 Expr *pExpr
126771#else
126772 Expr *pExpr,
126773 const char **pzOrigDb,
126774 const char **pzOrigTab,
126775 const char **pzOrigCol
126776#endif
126777){
126778 char const *zType = 0;
126779 int j;
126780#ifdef SQLITE_ENABLE_COLUMN_METADATA
126781 char const *zOrigDb = 0;
126782 char const *zOrigTab = 0;
126783 char const *zOrigCol = 0;
126784#endif
126785
126786 assert( pExpr!=0 )((void) (0));
126787 assert( pNC->pSrcList!=0 )((void) (0));
126788 assert( pExpr->op!=TK_AGG_COLUMN )((void) (0)); /* This routine runes before aggregates
126789 ** are processed */
126790 switch( pExpr->op ){
126791 case TK_COLUMN162: {
126792 /* The expression is a column. Locate the table the column is being
126793 ** extracted from in NameContext.pSrcList. This table may be real
126794 ** database table or a subquery.
126795 */
126796 Table *pTab = 0; /* Table structure column is extracted from */
126797 Select *pS = 0; /* Select the column is extracted from */
126798 int iCol = pExpr->iColumn; /* Index of column in pTab */
126799 while( pNC && !pTab ){
126800 SrcList *pTabList = pNC->pSrcList;
126801 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
126802 if( j<pTabList->nSrc ){
126803 pTab = pTabList->a[j].pTab;
126804 pS = pTabList->a[j].pSelect;
126805 }else{
126806 pNC = pNC->pNext;
126807 }
126808 }
126809
126810 if( pTab==0 ){
126811 /* At one time, code such as "SELECT new.x" within a trigger would
126812 ** cause this condition to run. Since then, we have restructured how
126813 ** trigger code is generated and so this condition is no longer
126814 ** possible. However, it can still be true for statements like
126815 ** the following:
126816 **
126817 ** CREATE TABLE t1(col INTEGER);
126818 ** SELECT (SELECT t1.col) FROM FROM t1;
126819 **
126820 ** when columnType() is called on the expression "t1.col" in the
126821 ** sub-select. In this case, set the column type to NULL, even
126822 ** though it should really be "INTEGER".
126823 **
126824 ** This is not a problem, as the column type of "t1.col" is never
126825 ** used. When columnType() is called on the expression
126826 ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
126827 ** branch below. */
126828 break;
126829 }
126830
126831 assert( pTab && pExpr->y.pTab==pTab )((void) (0));
126832 if( pS ){
126833 /* The "table" is actually a sub-select or a view in the FROM clause
126834 ** of the SELECT statement. Return the declaration type and origin
126835 ** data for the result-set column of the sub-select.
126836 */
126837 if( iCol>=0 && iCol<pS->pEList->nExpr ){
126838 /* If iCol is less than zero, then the expression requests the
126839 ** rowid of the sub-select or view. This expression is legal (see
126840 ** test case misc2.2.2) - it always evaluates to NULL.
126841 */
126842 NameContext sNC;
126843 Expr *p = pS->pEList->a[iCol].pExpr;
126844 sNC.pSrcList = pS->pSrc;
126845 sNC.pNext = pNC;
126846 sNC.pParse = pNC->pParse;
126847 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol)columnTypeImpl(&sNC,p);
126848 }
126849 }else{
126850 /* A real table or a CTE table */
126851 assert( !pS )((void) (0));
126852#ifdef SQLITE_ENABLE_COLUMN_METADATA
126853 if( iCol<0 ) iCol = pTab->iPKey;
126854 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
126855 if( iCol<0 ){
126856 zType = "INTEGER";
126857 zOrigCol = "rowid";
126858 }else{
126859 zOrigCol = pTab->aCol[iCol].zName;
126860 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
126861 }
126862 zOrigTab = pTab->zName;
126863 if( pNC->pParse && pTab->pSchema ){
126864 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
126865 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
126866 }
126867#else
126868 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
126869 if( iCol<0 ){
126870 zType = "INTEGER";
126871 }else{
126872 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
126873 }
126874#endif
126875 }
126876 break;
126877 }
126878#ifndef SQLITE_OMIT_SUBQUERY
126879 case TK_SELECT131: {
126880 /* The expression is a sub-select. Return the declaration type and
126881 ** origin info for the single column in the result set of the SELECT
126882 ** statement.
126883 */
126884 NameContext sNC;
126885 Select *pS = pExpr->x.pSelect;
126886 Expr *p = pS->pEList->a[0].pExpr;
126887 assert( ExprHasProperty(pExpr, EP_xIsSelect) )((void) (0));
126888 sNC.pSrcList = pS->pSrc;
126889 sNC.pNext = pNC;
126890 sNC.pParse = pNC->pParse;
126891 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol)columnTypeImpl(&sNC,p);
126892 break;
126893 }
126894#endif
126895 }
126896
126897#ifdef SQLITE_ENABLE_COLUMN_METADATA
126898 if( pzOrigDb ){
126899 assert( pzOrigTab && pzOrigCol )((void) (0));
126900 *pzOrigDb = zOrigDb;
126901 *pzOrigTab = zOrigTab;
126902 *pzOrigCol = zOrigCol;
126903 }
126904#endif
126905 return zType;
126906}
126907
126908/*
126909** Generate code that will tell the VDBE the declaration types of columns
126910** in the result set.
126911*/
126912static void generateColumnTypes(
126913 Parse *pParse, /* Parser context */
126914 SrcList *pTabList, /* List of tables */
126915 ExprList *pEList /* Expressions defining the result set */
126916){
126917#ifndef SQLITE_OMIT_DECLTYPE
126918 Vdbe *v = pParse->pVdbe;
126919 int i;
126920 NameContext sNC;
126921 sNC.pSrcList = pTabList;
126922 sNC.pParse = pParse;
126923 sNC.pNext = 0;
126924 for(i=0; i<pEList->nExpr; i++){
126925 Expr *p = pEList->a[i].pExpr;
126926 const char *zType;
126927#ifdef SQLITE_ENABLE_COLUMN_METADATA
126928 const char *zOrigDb = 0;
126929 const char *zOrigTab = 0;
126930 const char *zOrigCol = 0;
126931 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol)columnTypeImpl(&sNC,p);
126932
126933 /* The vdbe must make its own copy of the column-type and other
126934 ** column specific strings, in case the schema is reset before this
126935 ** virtual machine is deleted.
126936 */
126937 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE2, zOrigDb, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
126938 sqlite3VdbeSetColName(v, i, COLNAME_TABLE3, zOrigTab, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
126939 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN4, zOrigCol, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
126940#else
126941 zType = columnType(&sNC, p, 0, 0, 0)columnTypeImpl(&sNC,p);
126942#endif
126943 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE1, zType, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
126944 }
126945#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
126946}
126947
126948
126949/*
126950** Compute the column names for a SELECT statement.
126951**
126952** The only guarantee that SQLite makes about column names is that if the
126953** column has an AS clause assigning it a name, that will be the name used.
126954** That is the only documented guarantee. However, countless applications
126955** developed over the years have made baseless assumptions about column names
126956** and will break if those assumptions changes. Hence, use extreme caution
126957** when modifying this routine to avoid breaking legacy.
126958**
126959** See Also: sqlite3ColumnsFromExprList()
126960**
126961** The PRAGMA short_column_names and PRAGMA full_column_names settings are
126962** deprecated. The default setting is short=ON, full=OFF. 99.9% of all
126963** applications should operate this way. Nevertheless, we need to support the
126964** other modes for legacy:
126965**
126966** short=OFF, full=OFF: Column name is the text of the expression has it
126967** originally appears in the SELECT statement. In
126968** other words, the zSpan of the result expression.
126969**
126970** short=ON, full=OFF: (This is the default setting). If the result
126971** refers directly to a table column, then the
126972** result column name is just the table column
126973** name: COLUMN. Otherwise use zSpan.
126974**
126975** full=ON, short=ANY: If the result refers directly to a table column,
126976** then the result column name with the table name
126977** prefix, ex: TABLE.COLUMN. Otherwise use zSpan.
126978*/
126979static void generateColumnNames(
126980 Parse *pParse, /* Parser context */
126981 Select *pSelect /* Generate column names for this SELECT statement */
126982){
126983 Vdbe *v = pParse->pVdbe;
126984 int i;
126985 Table *pTab;
126986 SrcList *pTabList;
126987 ExprList *pEList;
126988 sqlite3 *db = pParse->db;
126989 int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */
126990 int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */
126991
126992#ifndef SQLITE_OMIT_EXPLAIN
126993 /* If this is an EXPLAIN, skip this step */
126994 if( pParse->explain ){
126995 return;
126996 }
126997#endif
126998
126999 if( pParse->colNamesSet ) return;
127000 /* Column names are determined by the left-most term of a compound select */
127001 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
127002 SELECTTRACE(1,pParse,pSelect,("generating column names\n"));
127003 pTabList = pSelect->pSrc;
127004 pEList = pSelect->pEList;
127005 assert( v!=0 )((void) (0));
127006 assert( pTabList!=0 )((void) (0));
127007 pParse->colNamesSet = 1;
127008 fullName = (db->flags & SQLITE_FullColNames0x00000004)!=0;
127009 srcName = (db->flags & SQLITE_ShortColNames0x00000040)!=0 || fullName;
127010 sqlite3VdbeSetNumCols(v, pEList->nExpr);
127011 for(i=0; i<pEList->nExpr; i++){
127012 Expr *p = pEList->a[i].pExpr;
127013
127014 assert( p!=0 )((void) (0));
127015 assert( p->op!=TK_AGG_COLUMN )((void) (0)); /* Agg processing has not run yet */
127016 assert( p->op!=TK_COLUMN || p->y.pTab!=0 )((void) (0)); /* Covering idx not yet coded */
127017 if( pEList->a[i].zName ){
127018 /* An AS clause always takes first priority */
127019 char *zName = pEList->a[i].zName;
127020 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zName, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
127021 }else if( srcName && p->op==TK_COLUMN162 ){
127022 char *zCol;
127023 int iCol = p->iColumn;
127024 pTab = p->y.pTab;
127025 assert( pTab!=0 )((void) (0));
127026 if( iCol<0 ) iCol = pTab->iPKey;
127027 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) )((void) (0));
127028 if( iCol<0 ){
127029 zCol = "rowid";
127030 }else{
127031 zCol = pTab->aCol[iCol].zName;
127032 }
127033 if( fullName ){
127034 char *zName = 0;
127035 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
127036 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zName, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3MallocSize));
127037 }else{
127038 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, zCol, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
127039 }
127040 }else{
127041 const char *z = pEList->a[i].zSpan;
127042 z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
127043 sqlite3VdbeSetColName(v, i, COLNAME_NAME0, z, SQLITE_DYNAMIC((sqlite3_destructor_type)sqlite3MallocSize));
127044 }
127045 }
127046 generateColumnTypes(pParse, pTabList, pEList);
127047}
127048
127049/*
127050** Given an expression list (which is really the list of expressions
127051** that form the result set of a SELECT statement) compute appropriate
127052** column names for a table that would hold the expression list.
127053**
127054** All column names will be unique.
127055**
127056** Only the column names are computed. Column.zType, Column.zColl,
127057** and other fields of Column are zeroed.
127058**
127059** Return SQLITE_OK on success. If a memory allocation error occurs,
127060** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
127061**
127062** The only guarantee that SQLite makes about column names is that if the
127063** column has an AS clause assigning it a name, that will be the name used.
127064** That is the only documented guarantee. However, countless applications
127065** developed over the years have made baseless assumptions about column names
127066** and will break if those assumptions changes. Hence, use extreme caution
127067** when modifying this routine to avoid breaking legacy.
127068**
127069** See Also: generateColumnNames()
127070*/
127071SQLITE_PRIVATEstatic int sqlite3ColumnsFromExprList(
127072 Parse *pParse, /* Parsing context */
127073 ExprList *pEList, /* Expr list from which to derive column names */
127074 i16 *pnCol, /* Write the number of columns here */
127075 Column **paCol /* Write the new column list here */
127076){
127077 sqlite3 *db = pParse->db; /* Database connection */
127078 int i, j; /* Loop counters */
127079 u32 cnt; /* Index added to make the name unique */
127080 Column *aCol, *pCol; /* For looping over result columns */
127081 int nCol; /* Number of columns in the result set */
127082 char *zName; /* Column name */
127083 int nName; /* Size of name in zName[] */
127084 Hash ht; /* Hash table of column names */
127085
127086 sqlite3HashInit(&ht);
127087 if( pEList ){
127088 nCol = pEList->nExpr;
127089 aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
127090 testcase( aCol==0 );
127091 if( nCol>32767 ) nCol = 32767;
127092 }else{
127093 nCol = 0;
127094 aCol = 0;
127095 }
127096 assert( nCol==(i16)nCol )((void) (0));
127097 *pnCol = nCol;
127098 *paCol = aCol;
127099
127100 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
127101 /* Get an appropriate name for the column
127102 */
127103 if( (zName = pEList->a[i].zName)!=0 ){
127104 /* If the column contains an "AS <name>" phrase, use <name> as the name */
127105 }else{
127106 Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
127107 while( pColExpr->op==TK_DOT134 ){
127108 pColExpr = pColExpr->pRight;
127109 assert( pColExpr!=0 )((void) (0));
127110 }
127111 assert( pColExpr->op!=TK_AGG_COLUMN )((void) (0));
127112 if( pColExpr->op==TK_COLUMN162 ){
127113 /* For columns use the column name name */
127114 int iCol = pColExpr->iColumn;
127115 Table *pTab = pColExpr->y.pTab;
127116 assert( pTab!=0 )((void) (0));
127117 if( iCol<0 ) iCol = pTab->iPKey;
127118 zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
127119 }else if( pColExpr->op==TK_ID59 ){
127120 assert( !ExprHasProperty(pColExpr, EP_IntValue) )((void) (0));
127121 zName = pColExpr->u.zToken;
127122 }else{
127123 /* Use the original text of the column expression as its name */
127124 zName = pEList->a[i].zSpan;
127125 }
127126 }
127127 if( zName ){
127128 zName = sqlite3DbStrDup(db, zName);
127129 }else{
127130 zName = sqlite3MPrintf(db,"column%d",i+1);
127131 }
127132
127133 /* Make sure the column name is unique. If the name is not unique,
127134 ** append an integer to the name so that it becomes unique.
127135 */
127136 cnt = 0;
127137 while( zName && sqlite3HashFind(&ht, zName)!=0 ){
127138 nName = sqlite3Strlen30(zName);
127139 if( nName>0 ){
127140 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j])(sqlite3CtypeMap[(unsigned char)(zName[j])]&0x04); j--){}
127141 if( zName[j]==':' ) nName = j;
127142 }
127143 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt);
127144 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
127145 }
127146 pCol->zName = zName;
127147 sqlite3ColumnPropertiesFromName(0, pCol);
127148 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
127149 sqlite3OomFault(db);
127150 }
127151 }
127152 sqlite3HashClear(&ht);
127153 if( db->mallocFailed ){
127154 for(j=0; j<i; j++){
127155 sqlite3DbFree(db, aCol[j].zName);
127156 }
127157 sqlite3DbFree(db, aCol);
127158 *paCol = 0;
127159 *pnCol = 0;
127160 return SQLITE_NOMEM_BKPT7;
127161 }
127162 return SQLITE_OK0;
127163}
127164
127165/*
127166** Add type and collation information to a column list based on
127167** a SELECT statement.
127168**
127169** The column list presumably came from selectColumnNamesFromExprList().
127170** The column list has only names, not types or collations. This
127171** routine goes through and adds the types and collations.
127172**
127173** This routine requires that all identifiers in the SELECT
127174** statement be resolved.
127175*/
127176SQLITE_PRIVATEstatic void sqlite3SelectAddColumnTypeAndCollation(
127177 Parse *pParse, /* Parsing contexts */
127178 Table *pTab, /* Add column type information to this table */
127179 Select *pSelect /* SELECT used to determine types and collations */
127180){
127181 sqlite3 *db = pParse->db;
127182 NameContext sNC;
127183 Column *pCol;
127184 CollSeq *pColl;
127185 int i;
127186 Expr *p;
127187 struct ExprList_item *a;
127188
127189 assert( pSelect!=0 )((void) (0));
127190 assert( (pSelect->selFlags & SF_Resolved)!=0 )((void) (0));
127191 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed )((void) (0));
127192 if( db->mallocFailed ) return;
127193 memset(&sNC, 0, sizeof(sNC));
127194 sNC.pSrcList = pSelect->pSrc;
127195 a = pSelect->pEList->a;
127196 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
127197 const char *zType;
127198 int n, m;
127199 p = a[i].pExpr;
127200 zType = columnType(&sNC, p, 0, 0, 0)columnTypeImpl(&sNC,p);
127201 /* pCol->szEst = ... // Column size est for SELECT tables never used */
127202 pCol->affinity = sqlite3ExprAffinity(p);
127203 if( zType ){
127204 m = sqlite3Strlen30(zType);
127205 n = sqlite3Strlen30(pCol->zName);
127206 pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
127207 if( pCol->zName ){
127208 memcpy(&pCol->zName[n+1], zType, m+1);
127209 pCol->colFlags |= COLFLAG_HASTYPE0x0004;
127210 }
127211 }
127212 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB'A';
127213 pColl = sqlite3ExprCollSeq(pParse, p);
127214 if( pColl && pCol->zColl==0 ){
127215 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
127216 }
127217 }
127218 pTab->szTabRow = 1; /* Any non-zero value works */
127219}
127220
127221/*
127222** Given a SELECT statement, generate a Table structure that describes
127223** the result set of that SELECT.
127224*/
127225SQLITE_PRIVATEstatic Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
127226 Table *pTab;
127227 sqlite3 *db = pParse->db;
127228 u64 savedFlags;
127229
127230 savedFlags = db->flags;
127231 db->flags &= ~(u64)SQLITE_FullColNames0x00000004;
127232 db->flags |= SQLITE_ShortColNames0x00000040;
127233 sqlite3SelectPrep(pParse, pSelect, 0);
127234 db->flags = savedFlags;
127235 if( pParse->nErr ) return 0;
127236 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
127237 pTab = sqlite3DbMallocZero(db, sizeof(Table) );
127238 if( pTab==0 ){
127239 return 0;
127240 }
127241 pTab->nTabRef = 1;
127242 pTab->zName = 0;
127243 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
127244 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
127245 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect);
127246 pTab->iPKey = -1;
127247 if( db->mallocFailed ){
127248 sqlite3DeleteTable(db, pTab);
127249 return 0;
127250 }
127251 return pTab;
127252}
127253
127254/*
127255** Get a VDBE for the given parser context. Create a new one if necessary.
127256** If an error occurs, return NULL and leave a message in pParse.
127257*/
127258SQLITE_PRIVATEstatic Vdbe *sqlite3GetVdbe(Parse *pParse){
127259 if( pParse->pVdbe ){
127260 return pParse->pVdbe;
127261 }
127262 if( pParse->pToplevel==0
127263 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)(((pParse->db)->dbOptFlags&(0x0008))==0)
127264 ){
127265 pParse->okConstFactor = 1;
127266 }
127267 return sqlite3VdbeCreate(pParse);
127268}
127269
127270
127271/*
127272** Compute the iLimit and iOffset fields of the SELECT based on the
127273** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
127274** that appear in the original SQL statement after the LIMIT and OFFSET
127275** keywords. Or NULL if those keywords are omitted. iLimit and iOffset
127276** are the integer memory register numbers for counters used to compute
127277** the limit and offset. If there is no limit and/or offset, then
127278** iLimit and iOffset are negative.
127279**
127280** This routine changes the values of iLimit and iOffset only if
127281** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
127282** and iOffset should have been preset to appropriate default values (zero)
127283** prior to calling this routine.
127284**
127285** The iOffset register (if it exists) is initialized to the value
127286** of the OFFSET. The iLimit register is initialized to LIMIT. Register
127287** iOffset+1 is initialized to LIMIT+OFFSET.
127288**
127289** Only if pLimit->pLeft!=0 do the limit registers get
127290** redefined. The UNION ALL operator uses this property to force
127291** the reuse of the same limit and offset registers across multiple
127292** SELECT statements.
127293*/
127294static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
127295 Vdbe *v = 0;
127296 int iLimit = 0;
127297 int iOffset;
127298 int n;
127299 Expr *pLimit = p->pLimit;
127300
127301 if( p->iLimit ) return;
127302
127303 /*
127304 ** "LIMIT -1" always shows all rows. There is some
127305 ** controversy about what the correct behavior should be.
127306 ** The current implementation interprets "LIMIT 0" to mean
127307 ** no rows.
127308 */
127309 if( pLimit ){
127310 assert( pLimit->op==TK_LIMIT )((void) (0));
127311 assert( pLimit->pLeft!=0 )((void) (0));
127312 p->iLimit = iLimit = ++pParse->nMem;
127313 v = sqlite3GetVdbe(pParse);
127314 assert( v!=0 )((void) (0));
127315 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
127316 sqlite3VdbeAddOp2(v, OP_Integer70, n, iLimit);
127317 VdbeComment((v, "LIMIT counter"));
127318 if( n==0 ){
127319 sqlite3VdbeGoto(v, iBreak);
127320 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
127321 p->nSelectRow = sqlite3LogEst((u64)n);
127322 p->selFlags |= SF_FixedLimit0x04000;
127323 }
127324 }else{
127325 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
127326 sqlite3VdbeAddOp1(v, OP_MustBeInt15, iLimit); VdbeCoverage(v);
127327 VdbeComment((v, "LIMIT counter"));
127328 sqlite3VdbeAddOp2(v, OP_IfNot20, iLimit, iBreak); VdbeCoverage(v);
127329 }
127330 if( pLimit->pRight ){
127331 p->iOffset = iOffset = ++pParse->nMem;
127332 pParse->nMem++; /* Allocate an extra register for limit+offset */
127333 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
127334 sqlite3VdbeAddOp1(v, OP_MustBeInt15, iOffset); VdbeCoverage(v);
127335 VdbeComment((v, "OFFSET counter"));
127336 sqlite3VdbeAddOp3(v, OP_OffsetLimit152, iLimit, iOffset+1, iOffset);
127337 VdbeComment((v, "LIMIT+OFFSET"));
127338 }
127339 }
127340}
127341
127342#ifndef SQLITE_OMIT_COMPOUND_SELECT
127343/*
127344** Return the appropriate collating sequence for the iCol-th column of
127345** the result set for the compound-select statement "p". Return NULL if
127346** the column has no default collating sequence.
127347**
127348** The collating sequence for the compound select is taken from the
127349** left-most term of the select that has a collating sequence.
127350*/
127351static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
127352 CollSeq *pRet;
127353 if( p->pPrior ){
127354 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
127355 }else{
127356 pRet = 0;
127357 }
127358 assert( iCol>=0 )((void) (0));
127359 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
127360 ** have been thrown during name resolution and we would not have gotten
127361 ** this far */
127362 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr)(iCol<p->pEList->nExpr) ){
127363 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
127364 }
127365 return pRet;
127366}
127367
127368/*
127369** The select statement passed as the second parameter is a compound SELECT
127370** with an ORDER BY clause. This function allocates and returns a KeyInfo
127371** structure suitable for implementing the ORDER BY.
127372**
127373** Space to hold the KeyInfo structure is obtained from malloc. The calling
127374** function is responsible for ensuring that this structure is eventually
127375** freed.
127376*/
127377static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
127378 ExprList *pOrderBy = p->pOrderBy;
127379 int nOrderBy = p->pOrderBy->nExpr;
127380 sqlite3 *db = pParse->db;
127381 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
127382 if( pRet ){
127383 int i;
127384 for(i=0; i<nOrderBy; i++){
127385 struct ExprList_item *pItem = &pOrderBy->a[i];
127386 Expr *pTerm = pItem->pExpr;
127387 CollSeq *pColl;
127388
127389 if( pTerm->flags & EP_Collate0x000100 ){
127390 pColl = sqlite3ExprCollSeq(pParse, pTerm);
127391 }else{
127392 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
127393 if( pColl==0 ) pColl = db->pDfltColl;
127394 pOrderBy->a[i].pExpr =
127395 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
127396 }
127397 assert( sqlite3KeyInfoIsWriteable(pRet) )((void) (0));
127398 pRet->aColl[i] = pColl;
127399 pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
127400 }
127401 }
127402
127403 return pRet;
127404}
127405
127406#ifndef SQLITE_OMIT_CTE
127407/*
127408** This routine generates VDBE code to compute the content of a WITH RECURSIVE
127409** query of the form:
127410**
127411** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
127412** \___________/ \_______________/
127413** p->pPrior p
127414**
127415**
127416** There is exactly one reference to the recursive-table in the FROM clause
127417** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
127418**
127419** The setup-query runs once to generate an initial set of rows that go
127420** into a Queue table. Rows are extracted from the Queue table one by
127421** one. Each row extracted from Queue is output to pDest. Then the single
127422** extracted row (now in the iCurrent table) becomes the content of the
127423** recursive-table for a recursive-query run. The output of the recursive-query
127424** is added back into the Queue table. Then another row is extracted from Queue
127425** and the iteration continues until the Queue table is empty.
127426**
127427** If the compound query operator is UNION then no duplicate rows are ever
127428** inserted into the Queue table. The iDistinct table keeps a copy of all rows
127429** that have ever been inserted into Queue and causes duplicates to be
127430** discarded. If the operator is UNION ALL, then duplicates are allowed.
127431**
127432** If the query has an ORDER BY, then entries in the Queue table are kept in
127433** ORDER BY order and the first entry is extracted for each cycle. Without
127434** an ORDER BY, the Queue table is just a FIFO.
127435**
127436** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
127437** have been output to pDest. A LIMIT of zero means to output no rows and a
127438** negative LIMIT means to output all rows. If there is also an OFFSET clause
127439** with a positive value, then the first OFFSET outputs are discarded rather
127440** than being sent to pDest. The LIMIT count does not begin until after OFFSET
127441** rows have been skipped.
127442*/
127443static void generateWithRecursiveQuery(
127444 Parse *pParse, /* Parsing context */
127445 Select *p, /* The recursive SELECT to be coded */
127446 SelectDest *pDest /* What to do with query results */
127447){
127448 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
127449 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
127450 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
127451 Select *pSetup = p->pPrior; /* The setup query */
127452 int addrTop; /* Top of the loop */
127453 int addrCont, addrBreak; /* CONTINUE and BREAK addresses */
127454 int iCurrent = 0; /* The Current table */
127455 int regCurrent; /* Register holding Current table */
127456 int iQueue; /* The Queue table */
127457 int iDistinct = 0; /* To ensure unique results if UNION */
127458 int eDest = SRT_Fifo5; /* How to write to Queue */
127459 SelectDest destQueue; /* SelectDest targetting the Queue table */
127460 int i; /* Loop counter */
127461 int rc; /* Result code */
127462 ExprList *pOrderBy; /* The ORDER BY clause */
127463 Expr *pLimit; /* Saved LIMIT and OFFSET */
127464 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */
127465
127466#ifndef SQLITE_OMIT_WINDOWFUNC
127467 if( p->pWin ){
127468 sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries");
127469 return;
127470 }
127471#endif
127472
127473 /* Obtain authorization to do a recursive query */
127474 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE33, 0, 0, 0) ) return;
127475
127476 /* Process the LIMIT and OFFSET clauses, if they exist */
127477 addrBreak = sqlite3VdbeMakeLabel(pParse);
127478 p->nSelectRow = 320; /* 4 billion rows */
127479 computeLimitRegisters(pParse, p, addrBreak);
127480 pLimit = p->pLimit;
127481 regLimit = p->iLimit;
127482 regOffset = p->iOffset;
127483 p->pLimit = 0;
127484 p->iLimit = p->iOffset = 0;
127485 pOrderBy = p->pOrderBy;
127486
127487 /* Locate the cursor number of the Current table */
127488 for(i=0; ALWAYS(i<pSrc->nSrc)(i<pSrc->nSrc); i++){
127489 if( pSrc->a[i].fg.isRecursive ){
127490 iCurrent = pSrc->a[i].iCursor;
127491 break;
127492 }
127493 }
127494
127495 /* Allocate cursors numbers for Queue and Distinct. The cursor number for
127496 ** the Distinct table must be exactly one greater than Queue in order
127497 ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */
127498 iQueue = pParse->nTab++;
127499 if( p->op==TK_UNION127 ){
127500 eDest = pOrderBy ? SRT_DistQueue8 : SRT_DistFifo6;
127501 iDistinct = pParse->nTab++;
127502 }else{
127503 eDest = pOrderBy ? SRT_Queue7 : SRT_Fifo5;
127504 }
127505 sqlite3SelectDestInit(&destQueue, eDest, iQueue);
127506
127507 /* Allocate cursors for Current, Queue, and Distinct. */
127508 regCurrent = ++pParse->nMem;
127509 sqlite3VdbeAddOp3(v, OP_OpenPseudo116, iCurrent, regCurrent, nCol);
127510 if( pOrderBy ){
127511 KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
127512 sqlite3VdbeAddOp4(v, OP_OpenEphemeral113, iQueue, pOrderBy->nExpr+2, 0,
127513 (char*)pKeyInfo, P4_KEYINFO(-9));
127514 destQueue.pOrderBy = pOrderBy;
127515 }else{
127516 sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, iQueue, nCol);
127517 }
127518 VdbeComment((v, "Queue table"));
127519 if( iDistinct ){
127520 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, iDistinct, 0);
127521 p->selFlags |= SF_UsesEphemeral0x00020;
127522 }
127523
127524 /* Detach the ORDER BY clause from the compound SELECT */
127525 p->pOrderBy = 0;
127526
127527 /* Store the results of the setup-query in Queue. */
127528 pSetup->pNext = 0;
127529 ExplainQueryPlan((pParse, 1, "SETUP"))sqlite3VdbeExplain (pParse, 1, "SETUP");
127530 rc = sqlite3Select(pParse, pSetup, &destQueue);
127531 pSetup->pNext = p;
127532 if( rc ) goto end_of_recursive_query;
127533
127534 /* Find the next row in the Queue and output that row */
127535 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind36, iQueue, addrBreak); VdbeCoverage(v);
127536
127537 /* Transfer the next row in Queue over to Current */
127538 sqlite3VdbeAddOp1(v, OP_NullRow129, iCurrent); /* To reset column cache */
127539 if( pOrderBy ){
127540 sqlite3VdbeAddOp3(v, OP_Column90, iQueue, pOrderBy->nExpr+1, regCurrent);
127541 }else{
127542 sqlite3VdbeAddOp2(v, OP_RowData127, iQueue, regCurrent);
127543 }
127544 sqlite3VdbeAddOp1(v, OP_Delete123, iQueue);
127545
127546 /* Output the single row in Current */
127547 addrCont = sqlite3VdbeMakeLabel(pParse);
127548 codeOffset(v, regOffset, addrCont);
127549 selectInnerLoop(pParse, p, iCurrent,
127550 0, 0, pDest, addrCont, addrBreak);
127551 if( regLimit ){
127552 sqlite3VdbeAddOp2(v, OP_DecrJumpZero49, regLimit, addrBreak);
127553 VdbeCoverage(v);
127554 }
127555 sqlite3VdbeResolveLabel(v, addrCont);
127556
127557 /* Execute the recursive SELECT taking the single row in Current as
127558 ** the value for the recursive-table. Store the results in the Queue.
127559 */
127560 if( p->selFlags & SF_Aggregate0x00008 ){
127561 sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported");
127562 }else{
127563 p->pPrior = 0;
127564 ExplainQueryPlan((pParse, 1, "RECURSIVE STEP"))sqlite3VdbeExplain (pParse, 1, "RECURSIVE STEP");
127565 sqlite3Select(pParse, p, &destQueue);
127566 assert( p->pPrior==0 )((void) (0));
127567 p->pPrior = pSetup;
127568 }
127569
127570 /* Keep running the loop until the Queue is empty */
127571 sqlite3VdbeGoto(v, addrTop);
127572 sqlite3VdbeResolveLabel(v, addrBreak);
127573
127574end_of_recursive_query:
127575 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
127576 p->pOrderBy = pOrderBy;
127577 p->pLimit = pLimit;
127578 return;
127579}
127580#endif /* SQLITE_OMIT_CTE */
127581
127582/* Forward references */
127583static int multiSelectOrderBy(
127584 Parse *pParse, /* Parsing context */
127585 Select *p, /* The right-most of SELECTs to be coded */
127586 SelectDest *pDest /* What to do with query results */
127587);
127588
127589/*
127590** Handle the special case of a compound-select that originates from a
127591** VALUES clause. By handling this as a special case, we avoid deep
127592** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
127593** on a VALUES clause.
127594**
127595** Because the Select object originates from a VALUES clause:
127596** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1
127597** (2) All terms are UNION ALL
127598** (3) There is no ORDER BY clause
127599**
127600** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES
127601** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))").
127602** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case.
127603** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
127604*/
127605static int multiSelectValues(
127606 Parse *pParse, /* Parsing context */
127607 Select *p, /* The right-most of SELECTs to be coded */
127608 SelectDest *pDest /* What to do with query results */
127609){
127610 int nRow = 1;
127611 int rc = 0;
127612 int bShowAll = p->pLimit==0;
127613 assert( p->selFlags & SF_MultiValue )((void) (0));
127614 do{
127615 assert( p->selFlags & SF_Values )((void) (0));
127616 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) )((void) (0));
127617 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr )((void) (0));
127618 if( p->pPrior==0 ) break;
127619 assert( p->pPrior->pNext==p )((void) (0));
127620 p = p->pPrior;
127621 nRow += bShowAll;
127622 }while(1);
127623 ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow,sqlite3VdbeExplain (pParse, 0, "SCAN %d CONSTANT ROW%s", nRow
, nRow==1 ? "" : "S")
127624 nRow==1 ? "" : "S"))sqlite3VdbeExplain (pParse, 0, "SCAN %d CONSTANT ROW%s", nRow
, nRow==1 ? "" : "S")
;
127625 while( p ){
127626 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
127627 if( !bShowAll ) break;
127628 p->nSelectRow = nRow;
127629 p = p->pNext;
127630 }
127631 return rc;
127632}
127633
127634/*
127635** This routine is called to process a compound query form from
127636** two or more separate queries using UNION, UNION ALL, EXCEPT, or
127637** INTERSECT
127638**
127639** "p" points to the right-most of the two queries. the query on the
127640** left is p->pPrior. The left query could also be a compound query
127641** in which case this routine will be called recursively.
127642**
127643** The results of the total query are to be written into a destination
127644** of type eDest with parameter iParm.
127645**
127646** Example 1: Consider a three-way compound SQL statement.
127647**
127648** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
127649**
127650** This statement is parsed up as follows:
127651**
127652** SELECT c FROM t3
127653** |
127654** `-----> SELECT b FROM t2
127655** |
127656** `------> SELECT a FROM t1
127657**
127658** The arrows in the diagram above represent the Select.pPrior pointer.
127659** So if this routine is called with p equal to the t3 query, then
127660** pPrior will be the t2 query. p->op will be TK_UNION in this case.
127661**
127662** Notice that because of the way SQLite parses compound SELECTs, the
127663** individual selects always group from left to right.
127664*/
127665static int multiSelect(
127666 Parse *pParse, /* Parsing context */
127667 Select *p, /* The right-most of SELECTs to be coded */
127668 SelectDest *pDest /* What to do with query results */
127669){
127670 int rc = SQLITE_OK0; /* Success code from a subroutine */
127671 Select *pPrior; /* Another SELECT immediately to our left */
127672 Vdbe *v; /* Generate code to this VDBE */
127673 SelectDest dest; /* Alternative data destination */
127674 Select *pDelete = 0; /* Chain of simple selects to delete */
127675 sqlite3 *db; /* Database connection */
127676
127677 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
127678 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
127679 */
127680 assert( p && p->pPrior )((void) (0)); /* Calling function guarantees this much */
127681 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION )((void) (0));
127682 assert( p->selFlags & SF_Compound )((void) (0));
127683 db = pParse->db;
127684 pPrior = p->pPrior;
127685 dest = *pDest;
127686 if( pPrior->pOrderBy || pPrior->pLimit ){
127687 sqlite3ErrorMsg(pParse,"%s clause should come after %s not before",
127688 pPrior->pOrderBy!=0 ? "ORDER BY" : "LIMIT", selectOpName(p->op));
127689 rc = 1;
127690 goto multi_select_end;
127691 }
127692
127693 v = sqlite3GetVdbe(pParse);
127694 assert( v!=0 )((void) (0)); /* The VDBE already created by calling function */
127695
127696 /* Create the destination temporary table if necessary
127697 */
127698 if( dest.eDest==SRT_EphemTab12 ){
127699 assert( p->pEList )((void) (0));
127700 sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, dest.iSDParm, p->pEList->nExpr);
127701 dest.eDest = SRT_Table14;
127702 }
127703
127704 /* Special handling for a compound-select that originates as a VALUES clause.
127705 */
127706 if( p->selFlags & SF_MultiValue0x00400 ){
127707 rc = multiSelectValues(pParse, p, &dest);
127708 goto multi_select_end;
127709 }
127710
127711 /* Make sure all SELECTs in the statement have the same number of elements
127712 ** in their result sets.
127713 */
127714 assert( p->pEList && pPrior->pEList )((void) (0));
127715 assert( p->pEList->nExpr==pPrior->pEList->nExpr )((void) (0));
127716
127717#ifndef SQLITE_OMIT_CTE
127718 if( p->selFlags & SF_Recursive0x02000 ){
127719 generateWithRecursiveQuery(pParse, p, &dest);
127720 }else
127721#endif
127722
127723 /* Compound SELECTs that have an ORDER BY clause are handled separately.
127724 */
127725 if( p->pOrderBy ){
127726 return multiSelectOrderBy(pParse, p, pDest);
127727 }else{
127728
127729#ifndef SQLITE_OMIT_EXPLAIN
127730 if( pPrior->pPrior==0 ){
127731 ExplainQueryPlan((pParse, 1, "COMPOUND QUERY"))sqlite3VdbeExplain (pParse, 1, "COMPOUND QUERY");
127732 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"))sqlite3VdbeExplain (pParse, 1, "LEFT-MOST SUBQUERY");
127733 }
127734#endif
127735
127736 /* Generate code for the left and right SELECT statements.
127737 */
127738 switch( p->op ){
127739 case TK_ALL128: {
127740 int addr = 0;
127741 int nLimit;
127742 assert( !pPrior->pLimit )((void) (0));
127743 pPrior->iLimit = p->iLimit;
127744 pPrior->iOffset = p->iOffset;
127745 pPrior->pLimit = p->pLimit;
127746 rc = sqlite3Select(pParse, pPrior, &dest);
127747 p->pLimit = 0;
127748 if( rc ){
127749 goto multi_select_end;
127750 }
127751 p->pPrior = 0;
127752 p->iLimit = pPrior->iLimit;
127753 p->iOffset = pPrior->iOffset;
127754 if( p->iLimit ){
127755 addr = sqlite3VdbeAddOp1(v, OP_IfNot20, p->iLimit); VdbeCoverage(v);
127756 VdbeComment((v, "Jump ahead if LIMIT reached"));
127757 if( p->iOffset ){
127758 sqlite3VdbeAddOp3(v, OP_OffsetLimit152,
127759 p->iLimit, p->iOffset+1, p->iOffset);
127760 }
127761 }
127762 ExplainQueryPlan((pParse, 1, "UNION ALL"))sqlite3VdbeExplain (pParse, 1, "UNION ALL");
127763 rc = sqlite3Select(pParse, p, &dest);
127764 testcase( rc!=SQLITE_OK );
127765 pDelete = p->pPrior;
127766 p->pPrior = pPrior;
127767 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
127768 if( pPrior->pLimit
127769 && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
127770 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
127771 ){
127772 p->nSelectRow = sqlite3LogEst((u64)nLimit);
127773 }
127774 if( addr ){
127775 sqlite3VdbeJumpHere(v, addr);
127776 }
127777 break;
127778 }
127779 case TK_EXCEPT129:
127780 case TK_UNION127: {
127781 int unionTab; /* Cursor number of the temp table holding result */
127782 u8 op = 0; /* One of the SRT_ operations to apply to self */
127783 int priorOp; /* The SRT_ operation to apply to prior selects */
127784 Expr *pLimit; /* Saved values of p->nLimit */
127785 int addr;
127786 SelectDest uniondest;
127787
127788 testcase( p->op==TK_EXCEPT );
127789 testcase( p->op==TK_UNION );
127790 priorOp = SRT_Union1;
127791 if( dest.eDest==priorOp ){
127792 /* We can reuse a temporary table generated by a SELECT to our
127793 ** right.
127794 */
127795 assert( p->pLimit==0 )((void) (0)); /* Not allowed on leftward elements */
127796 unionTab = dest.iSDParm;
127797 }else{
127798 /* We will need to create our own temporary table to hold the
127799 ** intermediate results.
127800 */
127801 unionTab = pParse->nTab++;
127802 assert( p->pOrderBy==0 )((void) (0));
127803 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, unionTab, 0);
127804 assert( p->addrOpenEphm[0] == -1 )((void) (0));
127805 p->addrOpenEphm[0] = addr;
127806 findRightmost(p)->selFlags |= SF_UsesEphemeral0x00020;
127807 assert( p->pEList )((void) (0));
127808 }
127809
127810 /* Code the SELECT statements to our left
127811 */
127812 assert( !pPrior->pOrderBy )((void) (0));
127813 sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
127814 rc = sqlite3Select(pParse, pPrior, &uniondest);
127815 if( rc ){
127816 goto multi_select_end;
127817 }
127818
127819 /* Code the current SELECT statement
127820 */
127821 if( p->op==TK_EXCEPT129 ){
127822 op = SRT_Except2;
127823 }else{
127824 assert( p->op==TK_UNION )((void) (0));
127825 op = SRT_Union1;
127826 }
127827 p->pPrior = 0;
127828 pLimit = p->pLimit;
127829 p->pLimit = 0;
127830 uniondest.eDest = op;
127831 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", selectOpName
(p->op))
127832 selectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", selectOpName
(p->op))
;
127833 rc = sqlite3Select(pParse, p, &uniondest);
127834 testcase( rc!=SQLITE_OK );
127835 /* Query flattening in sqlite3Select() might refill p->pOrderBy.
127836 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
127837 sqlite3ExprListDelete(db, p->pOrderBy);
127838 pDelete = p->pPrior;
127839 p->pPrior = pPrior;
127840 p->pOrderBy = 0;
127841 if( p->op==TK_UNION127 ){
127842 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
127843 }
127844 sqlite3ExprDelete(db, p->pLimit);
127845 p->pLimit = pLimit;
127846 p->iLimit = 0;
127847 p->iOffset = 0;
127848
127849 /* Convert the data in the temporary table into whatever form
127850 ** it is that we currently need.
127851 */
127852 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp )((void) (0));
127853 if( dest.eDest!=priorOp ){
127854 int iCont, iBreak, iStart;
127855 assert( p->pEList )((void) (0));
127856 iBreak = sqlite3VdbeMakeLabel(pParse);
127857 iCont = sqlite3VdbeMakeLabel(pParse);
127858 computeLimitRegisters(pParse, p, iBreak);
127859 sqlite3VdbeAddOp2(v, OP_Rewind36, unionTab, iBreak); VdbeCoverage(v);
127860 iStart = sqlite3VdbeCurrentAddr(v);
127861 selectInnerLoop(pParse, p, unionTab,
127862 0, 0, &dest, iCont, iBreak);
127863 sqlite3VdbeResolveLabel(v, iCont);
127864 sqlite3VdbeAddOp2(v, OP_Next5, unionTab, iStart); VdbeCoverage(v);
127865 sqlite3VdbeResolveLabel(v, iBreak);
127866 sqlite3VdbeAddOp2(v, OP_Close117, unionTab, 0);
127867 }
127868 break;
127869 }
127870 default: assert( p->op==TK_INTERSECT )((void) (0)); {
127871 int tab1, tab2;
127872 int iCont, iBreak, iStart;
127873 Expr *pLimit;
127874 int addr;
127875 SelectDest intersectdest;
127876 int r1;
127877
127878 /* INTERSECT is different from the others since it requires
127879 ** two temporary tables. Hence it has its own case. Begin
127880 ** by allocating the tables we will need.
127881 */
127882 tab1 = pParse->nTab++;
127883 tab2 = pParse->nTab++;
127884 assert( p->pOrderBy==0 )((void) (0));
127885
127886 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, tab1, 0);
127887 assert( p->addrOpenEphm[0] == -1 )((void) (0));
127888 p->addrOpenEphm[0] = addr;
127889 findRightmost(p)->selFlags |= SF_UsesEphemeral0x00020;
127890 assert( p->pEList )((void) (0));
127891
127892 /* Code the SELECTs to our left into temporary table "tab1".
127893 */
127894 sqlite3SelectDestInit(&intersectdest, SRT_Union1, tab1);
127895 rc = sqlite3Select(pParse, pPrior, &intersectdest);
127896 if( rc ){
127897 goto multi_select_end;
127898 }
127899
127900 /* Code the current SELECT into temporary table "tab2"
127901 */
127902 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, tab2, 0);
127903 assert( p->addrOpenEphm[1] == -1 )((void) (0));
127904 p->addrOpenEphm[1] = addr;
127905 p->pPrior = 0;
127906 pLimit = p->pLimit;
127907 p->pLimit = 0;
127908 intersectdest.iSDParm = tab2;
127909 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", selectOpName
(p->op))
127910 selectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "%s USING TEMP B-TREE", selectOpName
(p->op))
;
127911 rc = sqlite3Select(pParse, p, &intersectdest);
127912 testcase( rc!=SQLITE_OK );
127913 pDelete = p->pPrior;
127914 p->pPrior = pPrior;
127915 if( p->nSelectRow>pPrior->nSelectRow ){
127916 p->nSelectRow = pPrior->nSelectRow;
127917 }
127918 sqlite3ExprDelete(db, p->pLimit);
127919 p->pLimit = pLimit;
127920
127921 /* Generate code to take the intersection of the two temporary
127922 ** tables.
127923 */
127924 assert( p->pEList )((void) (0));
127925 iBreak = sqlite3VdbeMakeLabel(pParse);
127926 iCont = sqlite3VdbeMakeLabel(pParse);
127927 computeLimitRegisters(pParse, p, iBreak);
127928 sqlite3VdbeAddOp2(v, OP_Rewind36, tab1, iBreak); VdbeCoverage(v);
127929 r1 = sqlite3GetTempReg(pParse);
127930 iStart = sqlite3VdbeAddOp2(v, OP_RowData127, tab1, r1);
127931 sqlite3VdbeAddOp4Int(v, OP_NotFound28, tab2, iCont, r1, 0);
127932 VdbeCoverage(v);
127933 sqlite3ReleaseTempReg(pParse, r1);
127934 selectInnerLoop(pParse, p, tab1,
127935 0, 0, &dest, iCont, iBreak);
127936 sqlite3VdbeResolveLabel(v, iCont);
127937 sqlite3VdbeAddOp2(v, OP_Next5, tab1, iStart); VdbeCoverage(v);
127938 sqlite3VdbeResolveLabel(v, iBreak);
127939 sqlite3VdbeAddOp2(v, OP_Close117, tab2, 0);
127940 sqlite3VdbeAddOp2(v, OP_Close117, tab1, 0);
127941 break;
127942 }
127943 }
127944
127945 #ifndef SQLITE_OMIT_EXPLAIN
127946 if( p->pNext==0 ){
127947 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
127948 }
127949 #endif
127950 }
127951
127952 /* Compute collating sequences used by
127953 ** temporary tables needed to implement the compound select.
127954 ** Attach the KeyInfo structure to all temporary tables.
127955 **
127956 ** This section is run by the right-most SELECT statement only.
127957 ** SELECT statements to the left always skip this part. The right-most
127958 ** SELECT might also skip this part if it has no ORDER BY clause and
127959 ** no temp tables are required.
127960 */
127961 if( p->selFlags & SF_UsesEphemeral0x00020 ){
127962 int i; /* Loop counter */
127963 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
127964 Select *pLoop; /* For looping through SELECT statements */
127965 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
127966 int nCol; /* Number of columns in result set */
127967
127968 assert( p->pNext==0 )((void) (0));
127969 nCol = p->pEList->nExpr;
127970 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
127971 if( !pKeyInfo ){
127972 rc = SQLITE_NOMEM_BKPT7;
127973 goto multi_select_end;
127974 }
127975 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
127976 *apColl = multiSelectCollSeq(pParse, p, i);
127977 if( 0==*apColl ){
127978 *apColl = db->pDfltColl;
127979 }
127980 }
127981
127982 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
127983 for(i=0; i<2; i++){
127984 int addr = pLoop->addrOpenEphm[i];
127985 if( addr<0 ){
127986 /* If [0] is unused then [1] is also unused. So we can
127987 ** always safely abort as soon as the first unused slot is found */
127988 assert( pLoop->addrOpenEphm[1]<0 )((void) (0));
127989 break;
127990 }
127991 sqlite3VdbeChangeP2(v, addr, nCol);
127992 sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
127993 P4_KEYINFO(-9));
127994 pLoop->addrOpenEphm[i] = -1;
127995 }
127996 }
127997 sqlite3KeyInfoUnref(pKeyInfo);
127998 }
127999
128000multi_select_end:
128001 pDest->iSdst = dest.iSdst;
128002 pDest->nSdst = dest.nSdst;
128003 sqlite3SelectDelete(db, pDelete);
128004 return rc;
128005}
128006#endif /* SQLITE_OMIT_COMPOUND_SELECT */
128007
128008/*
128009** Error message for when two or more terms of a compound select have different
128010** size result sets.
128011*/
128012SQLITE_PRIVATEstatic void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
128013 if( p->selFlags & SF_Values0x00200 ){
128014 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
128015 }else{
128016 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
128017 " do not have the same number of result columns", selectOpName(p->op));
128018 }
128019}
128020
128021/*
128022** Code an output subroutine for a coroutine implementation of a
128023** SELECT statment.
128024**
128025** The data to be output is contained in pIn->iSdst. There are
128026** pIn->nSdst columns to be output. pDest is where the output should
128027** be sent.
128028**
128029** regReturn is the number of the register holding the subroutine
128030** return address.
128031**
128032** If regPrev>0 then it is the first register in a vector that
128033** records the previous output. mem[regPrev] is a flag that is false
128034** if there has been no previous output. If regPrev>0 then code is
128035** generated to suppress duplicates. pKeyInfo is used for comparing
128036** keys.
128037**
128038** If the LIMIT found in p->iLimit is reached, jump immediately to
128039** iBreak.
128040*/
128041static int generateOutputSubroutine(
128042 Parse *pParse, /* Parsing context */
128043 Select *p, /* The SELECT statement */
128044 SelectDest *pIn, /* Coroutine supplying data */
128045 SelectDest *pDest, /* Where to send the data */
128046 int regReturn, /* The return address register */
128047 int regPrev, /* Previous result register. No uniqueness if 0 */
128048 KeyInfo *pKeyInfo, /* For comparing with previous entry */
128049 int iBreak /* Jump here if we hit the LIMIT */
128050){
128051 Vdbe *v = pParse->pVdbe;
128052 int iContinue;
128053 int addr;
128054
128055 addr = sqlite3VdbeCurrentAddr(v);
128056 iContinue = sqlite3VdbeMakeLabel(pParse);
128057
128058 /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
128059 */
128060 if( regPrev ){
128061 int addr1, addr2;
128062 addr1 = sqlite3VdbeAddOp1(v, OP_IfNot20, regPrev); VdbeCoverage(v);
128063 addr2 = sqlite3VdbeAddOp4(v, OP_Compare87, pIn->iSdst, regPrev+1, pIn->nSdst,
128064 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO(-9));
128065 sqlite3VdbeAddOp3(v, OP_Jump16, addr2+2, iContinue, addr2+2); VdbeCoverage(v);
128066 sqlite3VdbeJumpHere(v, addr1);
128067 sqlite3VdbeAddOp3(v, OP_Copy78, pIn->iSdst, regPrev+1, pIn->nSdst-1);
128068 sqlite3VdbeAddOp2(v, OP_Integer70, 1, regPrev);
128069 }
128070 if( pParse->db->mallocFailed ) return 0;
128071
128072 /* Suppress the first OFFSET entries if there is an OFFSET clause
128073 */
128074 codeOffset(v, p->iOffset, iContinue);
128075
128076 assert( pDest->eDest!=SRT_Exists )((void) (0));
128077 assert( pDest->eDest!=SRT_Table )((void) (0));
128078 switch( pDest->eDest ){
128079 /* Store the result as data using a unique key.
128080 */
128081 case SRT_EphemTab12: {
128082 int r1 = sqlite3GetTempReg(pParse);
128083 int r2 = sqlite3GetTempReg(pParse);
128084 sqlite3VdbeAddOp3(v, OP_MakeRecord92, pIn->iSdst, pIn->nSdst, r1);
128085 sqlite3VdbeAddOp2(v, OP_NewRowid121, pDest->iSDParm, r2);
128086 sqlite3VdbeAddOp3(v, OP_Insert122, pDest->iSDParm, r1, r2);
128087 sqlite3VdbeChangeP5(v, OPFLAG_APPEND0x08);
128088 sqlite3ReleaseTempReg(pParse, r2);
128089 sqlite3ReleaseTempReg(pParse, r1);
128090 break;
128091 }
128092
128093#ifndef SQLITE_OMIT_SUBQUERY
128094 /* If we are creating a set for an "expr IN (SELECT ...)".
128095 */
128096 case SRT_Set11: {
128097 int r1;
128098 testcase( pIn->nSdst>1 );
128099 r1 = sqlite3GetTempReg(pParse);
128100 sqlite3VdbeAddOp4(v, OP_MakeRecord92, pIn->iSdst, pIn->nSdst,
128101 r1, pDest->zAffSdst, pIn->nSdst);
128102 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, pDest->iSDParm, r1,
128103 pIn->iSdst, pIn->nSdst);
128104 sqlite3ReleaseTempReg(pParse, r1);
128105 break;
128106 }
128107
128108 /* If this is a scalar select that is part of an expression, then
128109 ** store the results in the appropriate memory cell and break out
128110 ** of the scan loop.
128111 */
128112 case SRT_Mem10: {
128113 assert( pIn->nSdst==1 || pParse->nErr>0 )((void) (0)); testcase( pIn->nSdst!=1 );
128114 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
128115 /* The LIMIT clause will jump out of the loop for us */
128116 break;
128117 }
128118#endif /* #ifndef SQLITE_OMIT_SUBQUERY */
128119
128120 /* The results are stored in a sequence of registers
128121 ** starting at pDest->iSdst. Then the co-routine yields.
128122 */
128123 case SRT_Coroutine13: {
128124 if( pDest->iSdst==0 ){
128125 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
128126 pDest->nSdst = pIn->nSdst;
128127 }
128128 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
128129 sqlite3VdbeAddOp1(v, OP_Yield14, pDest->iSDParm);
128130 break;
128131 }
128132
128133 /* If none of the above, then the result destination must be
128134 ** SRT_Output. This routine is never called with any other
128135 ** destination other than the ones handled above or SRT_Output.
128136 **
128137 ** For SRT_Output, results are stored in a sequence of registers.
128138 ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
128139 ** return the next row of result.
128140 */
128141 default: {
128142 assert( pDest->eDest==SRT_Output )((void) (0));
128143 sqlite3VdbeAddOp2(v, OP_ResultRow81, pIn->iSdst, pIn->nSdst);
128144 break;
128145 }
128146 }
128147
128148 /* Jump to the end of the loop if the LIMIT is reached.
128149 */
128150 if( p->iLimit ){
128151 sqlite3VdbeAddOp2(v, OP_DecrJumpZero49, p->iLimit, iBreak); VdbeCoverage(v);
128152 }
128153
128154 /* Generate the subroutine return
128155 */
128156 sqlite3VdbeResolveLabel(v, iContinue);
128157 sqlite3VdbeAddOp1(v, OP_Return66, regReturn);
128158
128159 return addr;
128160}
128161
128162/*
128163** Alternative compound select code generator for cases when there
128164** is an ORDER BY clause.
128165**
128166** We assume a query of the following form:
128167**
128168** <selectA> <operator> <selectB> ORDER BY <orderbylist>
128169**
128170** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea
128171** is to code both <selectA> and <selectB> with the ORDER BY clause as
128172** co-routines. Then run the co-routines in parallel and merge the results
128173** into the output. In addition to the two coroutines (called selectA and
128174** selectB) there are 7 subroutines:
128175**
128176** outA: Move the output of the selectA coroutine into the output
128177** of the compound query.
128178**
128179** outB: Move the output of the selectB coroutine into the output
128180** of the compound query. (Only generated for UNION and
128181** UNION ALL. EXCEPT and INSERTSECT never output a row that
128182** appears only in B.)
128183**
128184** AltB: Called when there is data from both coroutines and A<B.
128185**
128186** AeqB: Called when there is data from both coroutines and A==B.
128187**
128188** AgtB: Called when there is data from both coroutines and A>B.
128189**
128190** EofA: Called when data is exhausted from selectA.
128191**
128192** EofB: Called when data is exhausted from selectB.
128193**
128194** The implementation of the latter five subroutines depend on which
128195** <operator> is used:
128196**
128197**
128198** UNION ALL UNION EXCEPT INTERSECT
128199** ------------- ----------------- -------------- -----------------
128200** AltB: outA, nextA outA, nextA outA, nextA nextA
128201**
128202** AeqB: outA, nextA nextA nextA outA, nextA
128203**
128204** AgtB: outB, nextB outB, nextB nextB nextB
128205**
128206** EofA: outB, nextB outB, nextB halt halt
128207**
128208** EofB: outA, nextA outA, nextA outA, nextA halt
128209**
128210** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
128211** causes an immediate jump to EofA and an EOF on B following nextB causes
128212** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or
128213** following nextX causes a jump to the end of the select processing.
128214**
128215** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
128216** within the output subroutine. The regPrev register set holds the previously
128217** output value. A comparison is made against this value and the output
128218** is skipped if the next results would be the same as the previous.
128219**
128220** The implementation plan is to implement the two coroutines and seven
128221** subroutines first, then put the control logic at the bottom. Like this:
128222**
128223** goto Init
128224** coA: coroutine for left query (A)
128225** coB: coroutine for right query (B)
128226** outA: output one row of A
128227** outB: output one row of B (UNION and UNION ALL only)
128228** EofA: ...
128229** EofB: ...
128230** AltB: ...
128231** AeqB: ...
128232** AgtB: ...
128233** Init: initialize coroutine registers
128234** yield coA
128235** if eof(A) goto EofA
128236** yield coB
128237** if eof(B) goto EofB
128238** Cmpr: Compare A, B
128239** Jump AltB, AeqB, AgtB
128240** End: ...
128241**
128242** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
128243** actually called using Gosub and they do not Return. EofA and EofB loop
128244** until all data is exhausted then jump to the "end" labe. AltB, AeqB,
128245** and AgtB jump to either L2 or to one of EofA or EofB.
128246*/
128247#ifndef SQLITE_OMIT_COMPOUND_SELECT
128248static int multiSelectOrderBy(
128249 Parse *pParse, /* Parsing context */
128250 Select *p, /* The right-most of SELECTs to be coded */
128251 SelectDest *pDest /* What to do with query results */
128252){
128253 int i, j; /* Loop counters */
128254 Select *pPrior; /* Another SELECT immediately to our left */
128255 Vdbe *v; /* Generate code to this VDBE */
128256 SelectDest destA; /* Destination for coroutine A */
128257 SelectDest destB; /* Destination for coroutine B */
128258 int regAddrA; /* Address register for select-A coroutine */
128259 int regAddrB; /* Address register for select-B coroutine */
128260 int addrSelectA; /* Address of the select-A coroutine */
128261 int addrSelectB; /* Address of the select-B coroutine */
128262 int regOutA; /* Address register for the output-A subroutine */
128263 int regOutB; /* Address register for the output-B subroutine */
128264 int addrOutA; /* Address of the output-A subroutine */
128265 int addrOutB = 0; /* Address of the output-B subroutine */
128266 int addrEofA; /* Address of the select-A-exhausted subroutine */
128267 int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */
128268 int addrEofB; /* Address of the select-B-exhausted subroutine */
128269 int addrAltB; /* Address of the A<B subroutine */
128270 int addrAeqB; /* Address of the A==B subroutine */
128271 int addrAgtB; /* Address of the A>B subroutine */
128272 int regLimitA; /* Limit register for select-A */
128273 int regLimitB; /* Limit register for select-A */
128274 int regPrev; /* A range of registers to hold previous output */
128275 int savedLimit; /* Saved value of p->iLimit */
128276 int savedOffset; /* Saved value of p->iOffset */
128277 int labelCmpr; /* Label for the start of the merge algorithm */
128278 int labelEnd; /* Label for the end of the overall SELECT stmt */
128279 int addr1; /* Jump instructions that get retargetted */
128280 int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
128281 KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
128282 KeyInfo *pKeyMerge; /* Comparison information for merging rows */
128283 sqlite3 *db; /* Database connection */
128284 ExprList *pOrderBy; /* The ORDER BY clause */
128285 int nOrderBy; /* Number of terms in the ORDER BY clause */
128286 int *aPermute; /* Mapping from ORDER BY terms to result set columns */
128287
128288 assert( p->pOrderBy!=0 )((void) (0));
128289 assert( pKeyDup==0 )((void) (0)); /* "Managed" code needs this. Ticket #3382. */
128290 db = pParse->db;
128291 v = pParse->pVdbe;
128292 assert( v!=0 )((void) (0)); /* Already thrown the error if VDBE alloc failed */
128293 labelEnd = sqlite3VdbeMakeLabel(pParse);
128294 labelCmpr = sqlite3VdbeMakeLabel(pParse);
128295
128296
128297 /* Patch up the ORDER BY clause
128298 */
128299 op = p->op;
128300 pPrior = p->pPrior;
128301 assert( pPrior->pOrderBy==0 )((void) (0));
128302 pOrderBy = p->pOrderBy;
128303 assert( pOrderBy )((void) (0));
128304 nOrderBy = pOrderBy->nExpr;
128305
128306 /* For operators other than UNION ALL we have to make sure that
128307 ** the ORDER BY clause covers every term of the result set. Add
128308 ** terms to the ORDER BY clause as necessary.
128309 */
128310 if( op!=TK_ALL128 ){
128311 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
128312 struct ExprList_item *pItem;
128313 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
128314 assert( pItem->u.x.iOrderByCol>0 )((void) (0));
128315 if( pItem->u.x.iOrderByCol==i ) break;
128316 }
128317 if( j==nOrderBy ){
128318 Expr *pNew = sqlite3Expr(db, TK_INTEGER147, 0);
128319 if( pNew==0 ) return SQLITE_NOMEM_BKPT7;
128320 pNew->flags |= EP_IntValue0x000400;
128321 pNew->u.iValue = i;
128322 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
128323 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
128324 }
128325 }
128326 }
128327
128328 /* Compute the comparison permutation and keyinfo that is used with
128329 ** the permutation used to determine if the next
128330 ** row of results comes from selectA or selectB. Also add explicit
128331 ** collations to the ORDER BY clause terms so that when the subqueries
128332 ** to the right and the left are evaluated, they use the correct
128333 ** collation.
128334 */
128335 aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
128336 if( aPermute ){
128337 struct ExprList_item *pItem;
128338 aPermute[0] = nOrderBy;
128339 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
128340 assert( pItem->u.x.iOrderByCol>0 )((void) (0));
128341 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr )((void) (0));
128342 aPermute[i] = pItem->u.x.iOrderByCol - 1;
128343 }
128344 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
128345 }else{
128346 pKeyMerge = 0;
128347 }
128348
128349 /* Reattach the ORDER BY clause to the query.
128350 */
128351 p->pOrderBy = pOrderBy;
128352 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
128353
128354 /* Allocate a range of temporary registers and the KeyInfo needed
128355 ** for the logic that removes duplicate result rows when the
128356 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
128357 */
128358 if( op==TK_ALL128 ){
128359 regPrev = 0;
128360 }else{
128361 int nExpr = p->pEList->nExpr;
128362 assert( nOrderBy>=nExpr || db->mallocFailed )((void) (0));
128363 regPrev = pParse->nMem+1;
128364 pParse->nMem += nExpr+1;
128365 sqlite3VdbeAddOp2(v, OP_Integer70, 0, regPrev);
128366 pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
128367 if( pKeyDup ){
128368 assert( sqlite3KeyInfoIsWriteable(pKeyDup) )((void) (0));
128369 for(i=0; i<nExpr; i++){
128370 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
128371 pKeyDup->aSortOrder[i] = 0;
128372 }
128373 }
128374 }
128375
128376 /* Separate the left and the right query from one another
128377 */
128378 p->pPrior = 0;
128379 pPrior->pNext = 0;
128380 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
128381 if( pPrior->pPrior==0 ){
128382 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
128383 }
128384
128385 /* Compute the limit registers */
128386 computeLimitRegisters(pParse, p, labelEnd);
128387 if( p->iLimit && op==TK_ALL128 ){
128388 regLimitA = ++pParse->nMem;
128389 regLimitB = ++pParse->nMem;
128390 sqlite3VdbeAddOp2(v, OP_Copy78, p->iOffset ? p->iOffset+1 : p->iLimit,
128391 regLimitA);
128392 sqlite3VdbeAddOp2(v, OP_Copy78, regLimitA, regLimitB);
128393 }else{
128394 regLimitA = regLimitB = 0;
128395 }
128396 sqlite3ExprDelete(db, p->pLimit);
128397 p->pLimit = 0;
128398
128399 regAddrA = ++pParse->nMem;
128400 regAddrB = ++pParse->nMem;
128401 regOutA = ++pParse->nMem;
128402 regOutB = ++pParse->nMem;
128403 sqlite3SelectDestInit(&destA, SRT_Coroutine13, regAddrA);
128404 sqlite3SelectDestInit(&destB, SRT_Coroutine13, regAddrB);
128405
128406 ExplainQueryPlan((pParse, 1, "MERGE (%s)", selectOpName(p->op)))sqlite3VdbeExplain (pParse, 1, "MERGE (%s)", selectOpName(p->
op))
;
128407
128408 /* Generate a coroutine to evaluate the SELECT statement to the
128409 ** left of the compound operator - the "A" select.
128410 */
128411 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
128412 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine13, regAddrA, 0, addrSelectA);
128413 VdbeComment((v, "left SELECT"));
128414 pPrior->iLimit = regLimitA;
128415 ExplainQueryPlan((pParse, 1, "LEFT"))sqlite3VdbeExplain (pParse, 1, "LEFT");
128416 sqlite3Select(pParse, pPrior, &destA);
128417 sqlite3VdbeEndCoroutine(v, regAddrA);
128418 sqlite3VdbeJumpHere(v, addr1);
128419
128420 /* Generate a coroutine to evaluate the SELECT statement on
128421 ** the right - the "B" select
128422 */
128423 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
128424 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine13, regAddrB, 0, addrSelectB);
128425 VdbeComment((v, "right SELECT"));
128426 savedLimit = p->iLimit;
128427 savedOffset = p->iOffset;
128428 p->iLimit = regLimitB;
128429 p->iOffset = 0;
128430 ExplainQueryPlan((pParse, 1, "RIGHT"))sqlite3VdbeExplain (pParse, 1, "RIGHT");
128431 sqlite3Select(pParse, p, &destB);
128432 p->iLimit = savedLimit;
128433 p->iOffset = savedOffset;
128434 sqlite3VdbeEndCoroutine(v, regAddrB);
128435
128436 /* Generate a subroutine that outputs the current row of the A
128437 ** select as the next output row of the compound select.
128438 */
128439 VdbeNoopComment((v, "Output routine for A"));
128440 addrOutA = generateOutputSubroutine(pParse,
128441 p, &destA, pDest, regOutA,
128442 regPrev, pKeyDup, labelEnd);
128443
128444 /* Generate a subroutine that outputs the current row of the B
128445 ** select as the next output row of the compound select.
128446 */
128447 if( op==TK_ALL128 || op==TK_UNION127 ){
128448 VdbeNoopComment((v, "Output routine for B"));
128449 addrOutB = generateOutputSubroutine(pParse,
128450 p, &destB, pDest, regOutB,
128451 regPrev, pKeyDup, labelEnd);
128452 }
128453 sqlite3KeyInfoUnref(pKeyDup);
128454
128455 /* Generate a subroutine to run when the results from select A
128456 ** are exhausted and only data in select B remains.
128457 */
128458 if( op==TK_EXCEPT129 || op==TK_INTERSECT130 ){
128459 addrEofA_noB = addrEofA = labelEnd;
128460 }else{
128461 VdbeNoopComment((v, "eof-A subroutine"));
128462 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub12, regOutB, addrOutB);
128463 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield14, regAddrB, labelEnd);
128464 VdbeCoverage(v);
128465 sqlite3VdbeGoto(v, addrEofA);
128466 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
128467 }
128468
128469 /* Generate a subroutine to run when the results from select B
128470 ** are exhausted and only data in select A remains.
128471 */
128472 if( op==TK_INTERSECT130 ){
128473 addrEofB = addrEofA;
128474 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
128475 }else{
128476 VdbeNoopComment((v, "eof-B subroutine"));
128477 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub12, regOutA, addrOutA);
128478 sqlite3VdbeAddOp2(v, OP_Yield14, regAddrA, labelEnd); VdbeCoverage(v);
128479 sqlite3VdbeGoto(v, addrEofB);
128480 }
128481
128482 /* Generate code to handle the case of A<B
128483 */
128484 VdbeNoopComment((v, "A-lt-B subroutine"));
128485 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub12, regOutA, addrOutA);
128486 sqlite3VdbeAddOp2(v, OP_Yield14, regAddrA, addrEofA); VdbeCoverage(v);
128487 sqlite3VdbeGoto(v, labelCmpr);
128488
128489 /* Generate code to handle the case of A==B
128490 */
128491 if( op==TK_ALL128 ){
128492 addrAeqB = addrAltB;
128493 }else if( op==TK_INTERSECT130 ){
128494 addrAeqB = addrAltB;
128495 addrAltB++;
128496 }else{
128497 VdbeNoopComment((v, "A-eq-B subroutine"));
128498 addrAeqB =
128499 sqlite3VdbeAddOp2(v, OP_Yield14, regAddrA, addrEofA); VdbeCoverage(v);
128500 sqlite3VdbeGoto(v, labelCmpr);
128501 }
128502
128503 /* Generate code to handle the case of A>B
128504 */
128505 VdbeNoopComment((v, "A-gt-B subroutine"));
128506 addrAgtB = sqlite3VdbeCurrentAddr(v);
128507 if( op==TK_ALL128 || op==TK_UNION127 ){
128508 sqlite3VdbeAddOp2(v, OP_Gosub12, regOutB, addrOutB);
128509 }
128510 sqlite3VdbeAddOp2(v, OP_Yield14, regAddrB, addrEofB); VdbeCoverage(v);
128511 sqlite3VdbeGoto(v, labelCmpr);
128512
128513 /* This code runs once to initialize everything.
128514 */
128515 sqlite3VdbeJumpHere(v, addr1);
128516 sqlite3VdbeAddOp2(v, OP_Yield14, regAddrA, addrEofA_noB); VdbeCoverage(v);
128517 sqlite3VdbeAddOp2(v, OP_Yield14, regAddrB, addrEofB); VdbeCoverage(v);
128518
128519 /* Implement the main merge loop
128520 */
128521 sqlite3VdbeResolveLabel(v, labelCmpr);
128522 sqlite3VdbeAddOp4(v, OP_Permutation86, 0, 0, 0, (char*)aPermute, P4_INTARRAY(-15));
128523 sqlite3VdbeAddOp4(v, OP_Compare87, destA.iSdst, destB.iSdst, nOrderBy,
128524 (char*)pKeyMerge, P4_KEYINFO(-9));
128525 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE0x01);
128526 sqlite3VdbeAddOp3(v, OP_Jump16, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
128527
128528 /* Jump to the this point in order to terminate the query.
128529 */
128530 sqlite3VdbeResolveLabel(v, labelEnd);
128531
128532 /* Reassembly the compound query so that it will be freed correctly
128533 ** by the calling function */
128534 if( p->pPrior ){
128535 sqlite3SelectDelete(db, p->pPrior);
128536 }
128537 p->pPrior = pPrior;
128538 pPrior->pNext = p;
128539
128540 /*** TBD: Insert subroutine calls to close cursors on incomplete
128541 **** subqueries ****/
128542 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
128543 return pParse->nErr!=0;
128544}
128545#endif
128546
128547#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
128548
128549/* An instance of the SubstContext object describes an substitution edit
128550** to be performed on a parse tree.
128551**
128552** All references to columns in table iTable are to be replaced by corresponding
128553** expressions in pEList.
128554*/
128555typedef struct SubstContext {
128556 Parse *pParse; /* The parsing context */
128557 int iTable; /* Replace references to this table */
128558 int iNewTable; /* New table number */
128559 int isLeftJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */
128560 ExprList *pEList; /* Replacement expressions */
128561} SubstContext;
128562
128563/* Forward Declarations */
128564static void substExprList(SubstContext*, ExprList*);
128565static void substSelect(SubstContext*, Select*, int);
128566
128567/*
128568** Scan through the expression pExpr. Replace every reference to
128569** a column in table number iTable with a copy of the iColumn-th
128570** entry in pEList. (But leave references to the ROWID column
128571** unchanged.)
128572**
128573** This routine is part of the flattening procedure. A subquery
128574** whose result set is defined by pEList appears as entry in the
128575** FROM clause of a SELECT such that the VDBE cursor assigned to that
128576** FORM clause entry is iTable. This routine makes the necessary
128577** changes to pExpr so that it refers directly to the source table
128578** of the subquery rather the result set of the subquery.
128579*/
128580static Expr *substExpr(
128581 SubstContext *pSubst, /* Description of the substitution */
128582 Expr *pExpr /* Expr in which substitution occurs */
128583){
128584 if( pExpr==0 ) return 0;
128585 if( ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0)
128586 && pExpr->iRightJoinTable==pSubst->iTable
128587 ){
128588 pExpr->iRightJoinTable = pSubst->iNewTable;
128589 }
128590 if( pExpr->op==TK_COLUMN162 && pExpr->iTable==pSubst->iTable ){
128591 if( pExpr->iColumn<0 ){
128592 pExpr->op = TK_NULL114;
128593 }else{
128594 Expr *pNew;
128595 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
128596 Expr ifNullRow;
128597 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr )((void) (0));
128598 assert( pExpr->pRight==0 )((void) (0));
128599 if( sqlite3ExprIsVector(pCopy) ){
128600 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
128601 }else{
128602 sqlite3 *db = pSubst->pParse->db;
128603 if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN162 ){
128604 memset(&ifNullRow, 0, sizeof(ifNullRow));
128605 ifNullRow.op = TK_IF_NULL_ROW171;
128606 ifNullRow.pLeft = pCopy;
128607 ifNullRow.iTable = pSubst->iNewTable;
128608 pCopy = &ifNullRow;
128609 }
128610 testcase( ExprHasProperty(pCopy, EP_Subquery) );
128611 pNew = sqlite3ExprDup(db, pCopy, 0);
128612 if( pNew && pSubst->isLeftJoin ){
128613 ExprSetProperty(pNew, EP_CanBeNull)(pNew)->flags|=(0x100000);
128614 }
128615 if( pNew && ExprHasProperty(pExpr,EP_FromJoin)(((pExpr)->flags&(0x000001))!=0) ){
128616 pNew->iRightJoinTable = pExpr->iRightJoinTable;
128617 ExprSetProperty(pNew, EP_FromJoin)(pNew)->flags|=(0x000001);
128618 }
128619 sqlite3ExprDelete(db, pExpr);
128620 pExpr = pNew;
128621 }
128622 }
128623 }else{
128624 if( pExpr->op==TK_IF_NULL_ROW171 && pExpr->iTable==pSubst->iTable ){
128625 pExpr->iTable = pSubst->iNewTable;
128626 }
128627 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
128628 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
128629 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
128630 substSelect(pSubst, pExpr->x.pSelect, 1);
128631 }else{
128632 substExprList(pSubst, pExpr->x.pList);
128633 }
128634 }
128635 return pExpr;
128636}
128637static void substExprList(
128638 SubstContext *pSubst, /* Description of the substitution */
128639 ExprList *pList /* List to scan and in which to make substitutes */
128640){
128641 int i;
128642 if( pList==0 ) return;
128643 for(i=0; i<pList->nExpr; i++){
128644 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
128645 }
128646}
128647static void substSelect(
128648 SubstContext *pSubst, /* Description of the substitution */
128649 Select *p, /* SELECT statement in which to make substitutions */
128650 int doPrior /* Do substitutes on p->pPrior too */
128651){
128652 SrcList *pSrc;
128653 struct SrcList_item *pItem;
128654 int i;
128655 if( !p ) return;
128656 do{
128657 substExprList(pSubst, p->pEList);
128658 substExprList(pSubst, p->pGroupBy);
128659 substExprList(pSubst, p->pOrderBy);
128660 p->pHaving = substExpr(pSubst, p->pHaving);
128661 p->pWhere = substExpr(pSubst, p->pWhere);
128662 pSrc = p->pSrc;
128663 assert( pSrc!=0 )((void) (0));
128664 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
128665 substSelect(pSubst, pItem->pSelect, 1);
128666 if( pItem->fg.isTabFunc ){
128667 substExprList(pSubst, pItem->u1.pFuncArg);
128668 }
128669 }
128670 }while( doPrior && (p = p->pPrior)!=0 );
128671}
128672#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
128673
128674#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
128675/*
128676** This routine attempts to flatten subqueries as a performance optimization.
128677** This routine returns 1 if it makes changes and 0 if no flattening occurs.
128678**
128679** To understand the concept of flattening, consider the following
128680** query:
128681**
128682** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
128683**
128684** The default way of implementing this query is to execute the
128685** subquery first and store the results in a temporary table, then
128686** run the outer query on that temporary table. This requires two
128687** passes over the data. Furthermore, because the temporary table
128688** has no indices, the WHERE clause on the outer query cannot be
128689** optimized.
128690**
128691** This routine attempts to rewrite queries such as the above into
128692** a single flat select, like this:
128693**
128694** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
128695**
128696** The code generated for this simplification gives the same result
128697** but only has to scan the data once. And because indices might
128698** exist on the table t1, a complete scan of the data might be
128699** avoided.
128700**
128701** Flattening is subject to the following constraints:
128702**
128703** (**) We no longer attempt to flatten aggregate subqueries. Was:
128704** The subquery and the outer query cannot both be aggregates.
128705**
128706** (**) We no longer attempt to flatten aggregate subqueries. Was:
128707** (2) If the subquery is an aggregate then
128708** (2a) the outer query must not be a join and
128709** (2b) the outer query must not use subqueries
128710** other than the one FROM-clause subquery that is a candidate
128711** for flattening. (This is due to ticket [2f7170d73bf9abf80]
128712** from 2015-02-09.)
128713**
128714** (3) If the subquery is the right operand of a LEFT JOIN then
128715** (3a) the subquery may not be a join and
128716** (3b) the FROM clause of the subquery may not contain a virtual
128717** table and
128718** (3c) the outer query may not be an aggregate.
128719**
128720** (4) The subquery can not be DISTINCT.
128721**
128722** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT
128723** sub-queries that were excluded from this optimization. Restriction
128724** (4) has since been expanded to exclude all DISTINCT subqueries.
128725**
128726** (**) We no longer attempt to flatten aggregate subqueries. Was:
128727** If the subquery is aggregate, the outer query may not be DISTINCT.
128728**
128729** (7) The subquery must have a FROM clause. TODO: For subqueries without
128730** A FROM clause, consider adding a FROM clause with the special
128731** table sqlite_once that consists of a single row containing a
128732** single NULL.
128733**
128734** (8) If the subquery uses LIMIT then the outer query may not be a join.
128735**
128736** (9) If the subquery uses LIMIT then the outer query may not be aggregate.
128737**
128738** (**) Restriction (10) was removed from the code on 2005-02-05 but we
128739** accidently carried the comment forward until 2014-09-15. Original
128740** constraint: "If the subquery is aggregate then the outer query
128741** may not use LIMIT."
128742**
128743** (11) The subquery and the outer query may not both have ORDER BY clauses.
128744**
128745** (**) Not implemented. Subsumed into restriction (3). Was previously
128746** a separate restriction deriving from ticket #350.
128747**
128748** (13) The subquery and outer query may not both use LIMIT.
128749**
128750** (14) The subquery may not use OFFSET.
128751**
128752** (15) If the outer query is part of a compound select, then the
128753** subquery may not use LIMIT.
128754** (See ticket #2339 and ticket [02a8e81d44]).
128755**
128756** (16) If the outer query is aggregate, then the subquery may not
128757** use ORDER BY. (Ticket #2942) This used to not matter
128758** until we introduced the group_concat() function.
128759**
128760** (17) If the subquery is a compound select, then
128761** (17a) all compound operators must be a UNION ALL, and
128762** (17b) no terms within the subquery compound may be aggregate
128763** or DISTINCT, and
128764** (17c) every term within the subquery compound must have a FROM clause
128765** (17d) the outer query may not be
128766** (17d1) aggregate, or
128767** (17d2) DISTINCT, or
128768** (17d3) a join.
128769**
128770** The parent and sub-query may contain WHERE clauses. Subject to
128771** rules (11), (13) and (14), they may also contain ORDER BY,
128772** LIMIT and OFFSET clauses. The subquery cannot use any compound
128773** operator other than UNION ALL because all the other compound
128774** operators have an implied DISTINCT which is disallowed by
128775** restriction (4).
128776**
128777** Also, each component of the sub-query must return the same number
128778** of result columns. This is actually a requirement for any compound
128779** SELECT statement, but all the code here does is make sure that no
128780** such (illegal) sub-query is flattened. The caller will detect the
128781** syntax error and return a detailed message.
128782**
128783** (18) If the sub-query is a compound select, then all terms of the
128784** ORDER BY clause of the parent must be simple references to
128785** columns of the sub-query.
128786**
128787** (19) If the subquery uses LIMIT then the outer query may not
128788** have a WHERE clause.
128789**
128790** (20) If the sub-query is a compound select, then it must not use
128791** an ORDER BY clause. Ticket #3773. We could relax this constraint
128792** somewhat by saying that the terms of the ORDER BY clause must
128793** appear as unmodified result columns in the outer query. But we
128794** have other optimizations in mind to deal with that case.
128795**
128796** (21) If the subquery uses LIMIT then the outer query may not be
128797** DISTINCT. (See ticket [752e1646fc]).
128798**
128799** (22) The subquery may not be a recursive CTE.
128800**
128801** (**) Subsumed into restriction (17d3). Was: If the outer query is
128802** a recursive CTE, then the sub-query may not be a compound query.
128803** This restriction is because transforming the
128804** parent to a compound query confuses the code that handles
128805** recursive queries in multiSelect().
128806**
128807** (**) We no longer attempt to flatten aggregate subqueries. Was:
128808** The subquery may not be an aggregate that uses the built-in min() or
128809** or max() functions. (Without this restriction, a query like:
128810** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily
128811** return the value X for which Y was maximal.)
128812**
128813** (25) If either the subquery or the parent query contains a window
128814** function in the select list or ORDER BY clause, flattening
128815** is not attempted.
128816**
128817**
128818** In this routine, the "p" parameter is a pointer to the outer query.
128819** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
128820** uses aggregates.
128821**
128822** If flattening is not attempted, this routine is a no-op and returns 0.
128823** If flattening is attempted this routine returns 1.
128824**
128825** All of the expression analysis must occur on both the outer query and
128826** the subquery before this routine runs.
128827*/
128828static int flattenSubquery(
128829 Parse *pParse, /* Parsing context */
128830 Select *p, /* The parent or outer SELECT statement */
128831 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
128832 int isAgg /* True if outer SELECT uses aggregate functions */
128833){
128834 const char *zSavedAuthContext = pParse->zAuthContext;
128835 Select *pParent; /* Current UNION ALL term of the other query */
128836 Select *pSub; /* The inner query or "subquery" */
128837 Select *pSub1; /* Pointer to the rightmost select in sub-query */
128838 SrcList *pSrc; /* The FROM clause of the outer query */
128839 SrcList *pSubSrc; /* The FROM clause of the subquery */
128840 int iParent; /* VDBE cursor number of the pSub result set temp table */
128841 int iNewParent = -1;/* Replacement table for iParent */
128842 int isLeftJoin = 0; /* True if pSub is the right side of a LEFT JOIN */
128843 int i; /* Loop counter */
128844 Expr *pWhere; /* The WHERE clause */
128845 struct SrcList_item *pSubitem; /* The subquery */
128846 sqlite3 *db = pParse->db;
128847
128848 /* Check to see if flattening is permitted. Return 0 if not.
128849 */
128850 assert( p!=0 )((void) (0));
128851 assert( p->pPrior==0 )((void) (0));
128852 if( OptimizationDisabled(db, SQLITE_QueryFlattener)(((db)->dbOptFlags&(0x0001))!=0) ) return 0;
128853 pSrc = p->pSrc;
128854 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc )((void) (0));
128855 pSubitem = &pSrc->a[iFrom];
128856 iParent = pSubitem->iCursor;
128857 pSub = pSubitem->pSelect;
128858 assert( pSub!=0 )((void) (0));
128859
128860#ifndef SQLITE_OMIT_WINDOWFUNC
128861 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
128862#endif
128863
128864 pSubSrc = pSub->pSrc;
128865 assert( pSubSrc )((void) (0));
128866 /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
128867 ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
128868 ** because they could be computed at compile-time. But when LIMIT and OFFSET
128869 ** became arbitrary expressions, we were forced to add restrictions (13)
128870 ** and (14). */
128871 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
128872 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
128873 if( (p->selFlags & SF_Compound0x00100)!=0 && pSub->pLimit ){
128874 return 0; /* Restriction (15) */
128875 }
128876 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
128877 if( pSub->selFlags & SF_Distinct0x00001 ) return 0; /* Restriction (4) */
128878 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
128879 return 0; /* Restrictions (8)(9) */
128880 }
128881 if( p->pOrderBy && pSub->pOrderBy ){
128882 return 0; /* Restriction (11) */
128883 }
128884 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
128885 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
128886 if( pSub->pLimit && (p->selFlags & SF_Distinct0x00001)!=0 ){
128887 return 0; /* Restriction (21) */
128888 }
128889 if( pSub->selFlags & (SF_Recursive0x02000) ){
128890 return 0; /* Restrictions (22) */
128891 }
128892
128893 /*
128894 ** If the subquery is the right operand of a LEFT JOIN, then the
128895 ** subquery may not be a join itself (3a). Example of why this is not
128896 ** allowed:
128897 **
128898 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
128899 **
128900 ** If we flatten the above, we would get
128901 **
128902 ** (t1 LEFT OUTER JOIN t2) JOIN t3
128903 **
128904 ** which is not at all the same thing.
128905 **
128906 ** If the subquery is the right operand of a LEFT JOIN, then the outer
128907 ** query cannot be an aggregate. (3c) This is an artifact of the way
128908 ** aggregates are processed - there is no mechanism to determine if
128909 ** the LEFT JOIN table should be all-NULL.
128910 **
128911 ** See also tickets #306, #350, and #3300.
128912 */
128913 if( (pSubitem->fg.jointype & JT_OUTER0x0020)!=0 ){
128914 isLeftJoin = 1;
128915 if( pSubSrc->nSrc>1 || isAgg || IsVirtual(pSubSrc->a[0].pTab)((pSubSrc->a[0].pTab)->nModuleArg) ){
128916 /* (3a) (3c) (3b) */
128917 return 0;
128918 }
128919 }
128920#ifdef SQLITE_EXTRA_IFNULLROW
128921 else if( iFrom>0 && !isAgg ){
128922 /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
128923 ** every reference to any result column from subquery in a join, even
128924 ** though they are not necessary. This will stress-test the OP_IfNullRow
128925 ** opcode. */
128926 isLeftJoin = -1;
128927 }
128928#endif
128929
128930 /* Restriction (17): If the sub-query is a compound SELECT, then it must
128931 ** use only the UNION ALL operator. And none of the simple select queries
128932 ** that make up the compound SELECT are allowed to be aggregate or distinct
128933 ** queries.
128934 */
128935 if( pSub->pPrior ){
128936 if( pSub->pOrderBy ){
128937 return 0; /* Restriction (20) */
128938 }
128939 if( isAgg || (p->selFlags & SF_Distinct0x00001)!=0 || pSrc->nSrc!=1 ){
128940 return 0; /* (17d1), (17d2), or (17d3) */
128941 }
128942 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
128943 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
128944 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
128945 assert( pSub->pSrc!=0 )((void) (0));
128946 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr )((void) (0));
128947 if( (pSub1->selFlags & (SF_Distinct0x00001|SF_Aggregate0x00008))!=0 /* (17b) */
128948 || (pSub1->pPrior && pSub1->op!=TK_ALL128) /* (17a) */
128949 || pSub1->pSrc->nSrc<1 /* (17c) */
128950 ){
128951 return 0;
128952 }
128953 testcase( pSub1->pSrc->nSrc>1 );
128954 }
128955
128956 /* Restriction (18). */
128957 if( p->pOrderBy ){
128958 int ii;
128959 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
128960 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
128961 }
128962 }
128963 }
128964
128965 /* Ex-restriction (23):
128966 ** The only way that the recursive part of a CTE can contain a compound
128967 ** subquery is for the subquery to be one term of a join. But if the
128968 ** subquery is a join, then the flattening has already been stopped by
128969 ** restriction (17d3)
128970 */
128971 assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 )((void) (0));
128972
128973 /***** If we reach this point, flattening is permitted. *****/
128974 SELECTTRACE(1,pParse,p,("flatten %u.%p from term %d\n",
128975 pSub->selId, pSub, iFrom));
128976
128977 /* Authorize the subquery */
128978 pParse->zAuthContext = pSubitem->zName;
128979 TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT21, 0, 0, 0);
128980 testcase( i==SQLITE_DENY );
128981 pParse->zAuthContext = zSavedAuthContext;
128982
128983 /* If the sub-query is a compound SELECT statement, then (by restrictions
128984 ** 17 and 18 above) it must be a UNION ALL and the parent query must
128985 ** be of the form:
128986 **
128987 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
128988 **
128989 ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
128990 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
128991 ** OFFSET clauses and joins them to the left-hand-side of the original
128992 ** using UNION ALL operators. In this case N is the number of simple
128993 ** select statements in the compound sub-query.
128994 **
128995 ** Example:
128996 **
128997 ** SELECT a+1 FROM (
128998 ** SELECT x FROM tab
128999 ** UNION ALL
129000 ** SELECT y FROM tab
129001 ** UNION ALL
129002 ** SELECT abs(z*2) FROM tab2
129003 ** ) WHERE a!=5 ORDER BY 1
129004 **
129005 ** Transformed into:
129006 **
129007 ** SELECT x+1 FROM tab WHERE x+1!=5
129008 ** UNION ALL
129009 ** SELECT y+1 FROM tab WHERE y+1!=5
129010 ** UNION ALL
129011 ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
129012 ** ORDER BY 1
129013 **
129014 ** We call this the "compound-subquery flattening".
129015 */
129016 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
129017 Select *pNew;
129018 ExprList *pOrderBy = p->pOrderBy;
129019 Expr *pLimit = p->pLimit;
129020 Select *pPrior = p->pPrior;
129021 p->pOrderBy = 0;
129022 p->pSrc = 0;
129023 p->pPrior = 0;
129024 p->pLimit = 0;
129025 pNew = sqlite3SelectDup(db, p, 0);
129026 p->pLimit = pLimit;
129027 p->pOrderBy = pOrderBy;
129028 p->pSrc = pSrc;
129029 p->op = TK_ALL128;
129030 if( pNew==0 ){
129031 p->pPrior = pPrior;
129032 }else{
129033 pNew->pPrior = pPrior;
129034 if( pPrior ) pPrior->pNext = pNew;
129035 pNew->pNext = p;
129036 p->pPrior = pNew;
129037 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
129038 " creates %u as peer\n",pNew->selId));
129039 }
129040 if( db->mallocFailed ) return 1;
129041 }
129042
129043 /* Begin flattening the iFrom-th entry of the FROM clause
129044 ** in the outer query.
129045 */
129046 pSub = pSub1 = pSubitem->pSelect;
129047
129048 /* Delete the transient table structure associated with the
129049 ** subquery
129050 */
129051 sqlite3DbFree(db, pSubitem->zDatabase);
129052 sqlite3DbFree(db, pSubitem->zName);
129053 sqlite3DbFree(db, pSubitem->zAlias);
129054 pSubitem->zDatabase = 0;
129055 pSubitem->zName = 0;
129056 pSubitem->zAlias = 0;
129057 pSubitem->pSelect = 0;
129058
129059 /* Defer deleting the Table object associated with the
129060 ** subquery until code generation is
129061 ** complete, since there may still exist Expr.pTab entries that
129062 ** refer to the subquery even after flattening. Ticket #3346.
129063 **
129064 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
129065 */
129066 if( ALWAYS(pSubitem->pTab!=0)(pSubitem->pTab!=0) ){
129067 Table *pTabToDel = pSubitem->pTab;
129068 if( pTabToDel->nTabRef==1 ){
129069 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
129070 pTabToDel->pNextZombie = pToplevel->pZombieTab;
129071 pToplevel->pZombieTab = pTabToDel;
129072 }else{
129073 pTabToDel->nTabRef--;
129074 }
129075 pSubitem->pTab = 0;
129076 }
129077
129078 /* The following loop runs once for each term in a compound-subquery
129079 ** flattening (as described above). If we are doing a different kind
129080 ** of flattening - a flattening other than a compound-subquery flattening -
129081 ** then this loop only runs once.
129082 **
129083 ** This loop moves all of the FROM elements of the subquery into the
129084 ** the FROM clause of the outer query. Before doing this, remember
129085 ** the cursor number for the original outer query FROM element in
129086 ** iParent. The iParent cursor will never be used. Subsequent code
129087 ** will scan expressions looking for iParent references and replace
129088 ** those references with expressions that resolve to the subquery FROM
129089 ** elements we are now copying in.
129090 */
129091 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
129092 int nSubSrc;
129093 u8 jointype = 0;
129094 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
129095 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
129096 pSrc = pParent->pSrc; /* FROM clause of the outer query */
129097
129098 if( pSrc ){
129099 assert( pParent==p )((void) (0)); /* First time through the loop */
129100 jointype = pSubitem->fg.jointype;
129101 }else{
129102 assert( pParent!=p )((void) (0)); /* 2nd and subsequent times through the loop */
129103 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
129104 if( pSrc==0 ) break;
129105 pParent->pSrc = pSrc;
129106 }
129107
129108 /* The subquery uses a single slot of the FROM clause of the outer
129109 ** query. If the subquery has more than one element in its FROM clause,
129110 ** then expand the outer query to make space for it to hold all elements
129111 ** of the subquery.
129112 **
129113 ** Example:
129114 **
129115 ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
129116 **
129117 ** The outer query has 3 slots in its FROM clause. One slot of the
129118 ** outer query (the middle slot) is used by the subquery. The next
129119 ** block of code will expand the outer query FROM clause to 4 slots.
129120 ** The middle slot is expanded to two slots in order to make space
129121 ** for the two elements in the FROM clause of the subquery.
129122 */
129123 if( nSubSrc>1 ){
129124 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
129125 if( pSrc==0 ) break;
129126 pParent->pSrc = pSrc;
129127 }
129128
129129 /* Transfer the FROM clause terms from the subquery into the
129130 ** outer query.
129131 */
129132 for(i=0; i<nSubSrc; i++){
129133 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
129134 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 )((void) (0));
129135 pSrc->a[i+iFrom] = pSubSrc->a[i];
129136 iNewParent = pSubSrc->a[i].iCursor;
129137 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
129138 }
129139 pSrc->a[iFrom].fg.jointype = jointype;
129140
129141 /* Now begin substituting subquery result set expressions for
129142 ** references to the iParent in the outer query.
129143 **
129144 ** Example:
129145 **
129146 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
129147 ** \ \_____________ subquery __________/ /
129148 ** \_____________________ outer query ______________________________/
129149 **
129150 ** We look at every expression in the outer query and every place we see
129151 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
129152 */
129153 if( pSub->pOrderBy ){
129154 /* At this point, any non-zero iOrderByCol values indicate that the
129155 ** ORDER BY column expression is identical to the iOrderByCol'th
129156 ** expression returned by SELECT statement pSub. Since these values
129157 ** do not necessarily correspond to columns in SELECT statement pParent,
129158 ** zero them before transfering the ORDER BY clause.
129159 **
129160 ** Not doing this may cause an error if a subsequent call to this
129161 ** function attempts to flatten a compound sub-query into pParent
129162 ** (the only way this can happen is if the compound sub-query is
129163 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
129164 ExprList *pOrderBy = pSub->pOrderBy;
129165 for(i=0; i<pOrderBy->nExpr; i++){
129166 pOrderBy->a[i].u.x.iOrderByCol = 0;
129167 }
129168 assert( pParent->pOrderBy==0 )((void) (0));
129169 pParent->pOrderBy = pOrderBy;
129170 pSub->pOrderBy = 0;
129171 }
129172 pWhere = pSub->pWhere;
129173 pSub->pWhere = 0;
129174 if( isLeftJoin>0 ){
129175 setJoinExpr(pWhere, iNewParent);
129176 }
129177 pParent->pWhere = sqlite3ExprAnd(pParse, pWhere, pParent->pWhere);
129178 if( db->mallocFailed==0 ){
129179 SubstContext x;
129180 x.pParse = pParse;
129181 x.iTable = iParent;
129182 x.iNewTable = iNewParent;
129183 x.isLeftJoin = isLeftJoin;
129184 x.pEList = pSub->pEList;
129185 substSelect(&x, pParent, 0);
129186 }
129187
129188 /* The flattened query is a compound if either the inner or the
129189 ** outer query is a compound. */
129190 pParent->selFlags |= pSub->selFlags & SF_Compound0x00100;
129191 assert( (pSub->selFlags & SF_Distinct)==0 )((void) (0)); /* restriction (17b) */
129192
129193 /*
129194 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
129195 **
129196 ** One is tempted to try to add a and b to combine the limits. But this
129197 ** does not work if either limit is negative.
129198 */
129199 if( pSub->pLimit ){
129200 pParent->pLimit = pSub->pLimit;
129201 pSub->pLimit = 0;
129202 }
129203 }
129204
129205 /* Finially, delete what is left of the subquery and return
129206 ** success.
129207 */
129208 sqlite3SelectDelete(db, pSub1);
129209
129210#if SELECTTRACE_ENABLED0
129211 if( sqlite3SelectTrace & 0x100 ){
129212 SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
129213 sqlite3TreeViewSelect(0, p, 0);
129214 }
129215#endif
129216
129217 return 1;
129218}
129219#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
129220
129221/*
129222** A structure to keep track of all of the column values that are fixed to
129223** a known value due to WHERE clause constraints of the form COLUMN=VALUE.
129224*/
129225typedef struct WhereConst WhereConst;
129226struct WhereConst {
129227 Parse *pParse; /* Parsing context */
129228 int nConst; /* Number for COLUMN=CONSTANT terms */
129229 int nChng; /* Number of times a constant is propagated */
129230 Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */
129231};
129232
129233/*
129234** Add a new entry to the pConst object. Except, do not add duplicate
129235** pColumn entires.
129236*/
129237static void constInsert(
129238 WhereConst *pConst, /* The WhereConst into which we are inserting */
129239 Expr *pColumn, /* The COLUMN part of the constraint */
129240 Expr *pValue /* The VALUE part of the constraint */
129241){
129242 int i;
129243 assert( pColumn->op==TK_COLUMN )((void) (0));
129244
129245 /* 2018-10-25 ticket [cf5ed20f]
129246 ** Make sure the same pColumn is not inserted more than once */
129247 for(i=0; i<pConst->nConst; i++){
129248 const Expr *pExpr = pConst->apExpr[i*2];
129249 assert( pExpr->op==TK_COLUMN )((void) (0));
129250 if( pExpr->iTable==pColumn->iTable
129251 && pExpr->iColumn==pColumn->iColumn
129252 ){
129253 return; /* Already present. Return without doing anything. */
129254 }
129255 }
129256
129257 pConst->nConst++;
129258 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
129259 pConst->nConst*2*sizeof(Expr*));
129260 if( pConst->apExpr==0 ){
129261 pConst->nConst = 0;
129262 }else{
129263 if( ExprHasProperty(pValue, EP_FixedCol)(((pValue)->flags&(0x000008))!=0) ) pValue = pValue->pLeft;
129264 pConst->apExpr[pConst->nConst*2-2] = pColumn;
129265 pConst->apExpr[pConst->nConst*2-1] = pValue;
129266 }
129267}
129268
129269/*
129270** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE
129271** is a constant expression and where the term must be true because it
129272** is part of the AND-connected terms of the expression. For each term
129273** found, add it to the pConst structure.
129274*/
129275static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
129276 Expr *pRight, *pLeft;
129277 if( pExpr==0 ) return;
129278 if( ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0) ) return;
129279 if( pExpr->op==TK_AND44 ){
129280 findConstInWhere(pConst, pExpr->pRight);
129281 findConstInWhere(pConst, pExpr->pLeft);
129282 return;
129283 }
129284 if( pExpr->op!=TK_EQ53 ) return;
129285 pRight = pExpr->pRight;
129286 pLeft = pExpr->pLeft;
129287 assert( pRight!=0 )((void) (0));
129288 assert( pLeft!=0 )((void) (0));
129289 if( pRight->op==TK_COLUMN162
129290 && !ExprHasProperty(pRight, EP_FixedCol)(((pRight)->flags&(0x000008))!=0)
129291 && sqlite3ExprIsConstant(pLeft)
129292 && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
129293 ){
129294 constInsert(pConst, pRight, pLeft);
129295 }else
129296 if( pLeft->op==TK_COLUMN162
129297 && !ExprHasProperty(pLeft, EP_FixedCol)(((pLeft)->flags&(0x000008))!=0)
129298 && sqlite3ExprIsConstant(pRight)
129299 && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
129300 ){
129301 constInsert(pConst, pLeft, pRight);
129302 }
129303}
129304
129305/*
129306** This is a Walker expression callback. pExpr is a candidate expression
129307** to be replaced by a value. If pExpr is equivalent to one of the
129308** columns named in pWalker->u.pConst, then overwrite it with its
129309** corresponding value.
129310*/
129311static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){
129312 int i;
129313 WhereConst *pConst;
129314 if( pExpr->op!=TK_COLUMN162 ) return WRC_Continue0;
129315 if( ExprHasProperty(pExpr, EP_FixedCol)(((pExpr)->flags&(0x000008))!=0) ) return WRC_Continue0;
129316 pConst = pWalker->u.pConst;
129317 for(i=0; i<pConst->nConst; i++){
129318 Expr *pColumn = pConst->apExpr[i*2];
129319 if( pColumn==pExpr ) continue;
129320 if( pColumn->iTable!=pExpr->iTable ) continue;
129321 if( pColumn->iColumn!=pExpr->iColumn ) continue;
129322 /* A match is found. Add the EP_FixedCol property */
129323 pConst->nChng++;
129324 ExprClearProperty(pExpr, EP_Leaf)(pExpr)->flags&=~(0x800000);
129325 ExprSetProperty(pExpr, EP_FixedCol)(pExpr)->flags|=(0x000008);
129326 assert( pExpr->pLeft==0 )((void) (0));
129327 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
129328 break;
129329 }
129330 return WRC_Prune1;
129331}
129332
129333/*
129334** The WHERE-clause constant propagation optimization.
129335**
129336** If the WHERE clause contains terms of the form COLUMN=CONSTANT or
129337** CONSTANT=COLUMN that must be tree (in other words, if the terms top-level
129338** AND-connected terms that are not part of a ON clause from a LEFT JOIN)
129339** then throughout the query replace all other occurrences of COLUMN
129340** with CONSTANT within the WHERE clause.
129341**
129342** For example, the query:
129343**
129344** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b
129345**
129346** Is transformed into
129347**
129348** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39
129349**
129350** Return true if any transformations where made and false if not.
129351**
129352** Implementation note: Constant propagation is tricky due to affinity
129353** and collating sequence interactions. Consider this example:
129354**
129355** CREATE TABLE t1(a INT,b TEXT);
129356** INSERT INTO t1 VALUES(123,'0123');
129357** SELECT * FROM t1 WHERE a=123 AND b=a;
129358** SELECT * FROM t1 WHERE a=123 AND b=123;
129359**
129360** The two SELECT statements above should return different answers. b=a
129361** is alway true because the comparison uses numeric affinity, but b=123
129362** is false because it uses text affinity and '0123' is not the same as '123'.
129363** To work around this, the expression tree is not actually changed from
129364** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol
129365** and the "123" value is hung off of the pLeft pointer. Code generator
129366** routines know to generate the constant "123" instead of looking up the
129367** column value. Also, to avoid collation problems, this optimization is
129368** only attempted if the "a=123" term uses the default BINARY collation.
129369*/
129370static int propagateConstants(
129371 Parse *pParse, /* The parsing context */
129372 Select *p /* The query in which to propagate constants */
129373){
129374 WhereConst x;
129375 Walker w;
129376 int nChng = 0;
129377 x.pParse = pParse;
129378 do{
129379 x.nConst = 0;
129380 x.nChng = 0;
129381 x.apExpr = 0;
129382 findConstInWhere(&x, p->pWhere);
129383 if( x.nConst ){
129384 memset(&w, 0, sizeof(w));
129385 w.pParse = pParse;
129386 w.xExprCallback = propagateConstantExprRewrite;
129387 w.xSelectCallback = sqlite3SelectWalkNoop;
129388 w.xSelectCallback2 = 0;
129389 w.walkerDepth = 0;
129390 w.u.pConst = &x;
129391 sqlite3WalkExpr(&w, p->pWhere);
129392 sqlite3DbFree(x.pParse->db, x.apExpr);
129393 nChng += x.nChng;
129394 }
129395 }while( x.nChng );
129396 return nChng;
129397}
129398
129399#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
129400/*
129401** Make copies of relevant WHERE clause terms of the outer query into
129402** the WHERE clause of subquery. Example:
129403**
129404** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
129405**
129406** Transformed into:
129407**
129408** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
129409** WHERE x=5 AND y=10;
129410**
129411** The hope is that the terms added to the inner query will make it more
129412** efficient.
129413**
129414** Do not attempt this optimization if:
129415**
129416** (1) (** This restriction was removed on 2017-09-29. We used to
129417** disallow this optimization for aggregate subqueries, but now
129418** it is allowed by putting the extra terms on the HAVING clause.
129419** The added HAVING clause is pointless if the subquery lacks
129420** a GROUP BY clause. But such a HAVING clause is also harmless
129421** so there does not appear to be any reason to add extra logic
129422** to suppress it. **)
129423**
129424** (2) The inner query is the recursive part of a common table expression.
129425**
129426** (3) The inner query has a LIMIT clause (since the changes to the WHERE
129427** clause would change the meaning of the LIMIT).
129428**
129429** (4) The inner query is the right operand of a LEFT JOIN and the
129430** expression to be pushed down does not come from the ON clause
129431** on that LEFT JOIN.
129432**
129433** (5) The WHERE clause expression originates in the ON or USING clause
129434** of a LEFT JOIN where iCursor is not the right-hand table of that
129435** left join. An example:
129436**
129437** SELECT *
129438** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa
129439** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2)
129440** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2);
129441**
129442** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9).
129443** But if the (b2=2) term were to be pushed down into the bb subquery,
129444** then the (1,1,NULL) row would be suppressed.
129445**
129446** (6) The inner query features one or more window-functions (since
129447** changes to the WHERE clause of the inner query could change the
129448** window over which window functions are calculated).
129449**
129450** Return 0 if no changes are made and non-zero if one or more WHERE clause
129451** terms are duplicated into the subquery.
129452*/
129453static int pushDownWhereTerms(
129454 Parse *pParse, /* Parse context (for malloc() and error reporting) */
129455 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */
129456 Expr *pWhere, /* The WHERE clause of the outer query */
129457 int iCursor, /* Cursor number of the subquery */
129458 int isLeftJoin /* True if pSubq is the right term of a LEFT JOIN */
129459){
129460 Expr *pNew;
129461 int nChng = 0;
129462 if( pWhere==0 ) return 0;
129463 if( pSubq->selFlags & SF_Recursive0x02000 ) return 0; /* restriction (2) */
129464
129465#ifndef SQLITE_OMIT_WINDOWFUNC
129466 if( pSubq->pWin ) return 0; /* restriction (6) */
129467#endif
129468
129469#ifdef SQLITE_DEBUG
129470 /* Only the first term of a compound can have a WITH clause. But make
129471 ** sure no other terms are marked SF_Recursive in case something changes
129472 ** in the future.
129473 */
129474 {
129475 Select *pX;
129476 for(pX=pSubq; pX; pX=pX->pPrior){
129477 assert( (pX->selFlags & (SF_Recursive))==0 )((void) (0));
129478 }
129479 }
129480#endif
129481
129482 if( pSubq->pLimit!=0 ){
129483 return 0; /* restriction (3) */
129484 }
129485 while( pWhere->op==TK_AND44 ){
129486 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
129487 iCursor, isLeftJoin);
129488 pWhere = pWhere->pLeft;
129489 }
129490 if( isLeftJoin
129491 && (ExprHasProperty(pWhere,EP_FromJoin)(((pWhere)->flags&(0x000001))!=0)==0
129492 || pWhere->iRightJoinTable!=iCursor)
129493 ){
129494 return 0; /* restriction (4) */
129495 }
129496 if( ExprHasProperty(pWhere,EP_FromJoin)(((pWhere)->flags&(0x000001))!=0) && pWhere->iRightJoinTable!=iCursor ){
129497 return 0; /* restriction (5) */
129498 }
129499 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
129500 nChng++;
129501 while( pSubq ){
129502 SubstContext x;
129503 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
129504 unsetJoinExpr(pNew, -1);
129505 x.pParse = pParse;
129506 x.iTable = iCursor;
129507 x.iNewTable = iCursor;
129508 x.isLeftJoin = 0;
129509 x.pEList = pSubq->pEList;
129510 pNew = substExpr(&x, pNew);
129511 if( pSubq->selFlags & SF_Aggregate0x00008 ){
129512 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
129513 }else{
129514 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
129515 }
129516 pSubq = pSubq->pPrior;
129517 }
129518 }
129519 return nChng;
129520}
129521#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
129522
129523/*
129524** The pFunc is the only aggregate function in the query. Check to see
129525** if the query is a candidate for the min/max optimization.
129526**
129527** If the query is a candidate for the min/max optimization, then set
129528** *ppMinMax to be an ORDER BY clause to be used for the optimization
129529** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
129530** whether pFunc is a min() or max() function.
129531**
129532** If the query is not a candidate for the min/max optimization, return
129533** WHERE_ORDERBY_NORMAL (which must be zero).
129534**
129535** This routine must be called after aggregate functions have been
129536** located but before their arguments have been subjected to aggregate
129537** analysis.
129538*/
129539static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
129540 int eRet = WHERE_ORDERBY_NORMAL0x0000; /* Return value */
129541 ExprList *pEList = pFunc->x.pList; /* Arguments to agg function */
129542 const char *zFunc; /* Name of aggregate function pFunc */
129543 ExprList *pOrderBy;
129544 u8 sortOrder;
129545
129546 assert( *ppMinMax==0 )((void) (0));
129547 assert( pFunc->op==TK_AGG_FUNCTION )((void) (0));
129548 if( pEList==0 || pEList->nExpr!=1 ) return eRet;
129549 zFunc = pFunc->u.zToken;
129550 if( sqlite3StrICmp(zFunc, "min")==0 ){
129551 eRet = WHERE_ORDERBY_MIN0x0001;
129552 sortOrder = SQLITE_SO_ASC0;
129553 }else if( sqlite3StrICmp(zFunc, "max")==0 ){
129554 eRet = WHERE_ORDERBY_MAX0x0002;
129555 sortOrder = SQLITE_SO_DESC1;
129556 }else{
129557 return eRet;
129558 }
129559 *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
129560 assert( pOrderBy!=0 || db->mallocFailed )((void) (0));
129561 if( pOrderBy ) pOrderBy->a[0].sortOrder = sortOrder;
129562 return eRet;
129563}
129564
129565/*
129566** The select statement passed as the first argument is an aggregate query.
129567** The second argument is the associated aggregate-info object. This
129568** function tests if the SELECT is of the form:
129569**
129570** SELECT count(*) FROM <tbl>
129571**
129572** where table is a database table, not a sub-select or view. If the query
129573** does match this pattern, then a pointer to the Table object representing
129574** <tbl> is returned. Otherwise, 0 is returned.
129575*/
129576static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
129577 Table *pTab;
129578 Expr *pExpr;
129579
129580 assert( !p->pGroupBy )((void) (0));
129581
129582 if( p->pWhere || p->pEList->nExpr!=1
129583 || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
129584 ){
129585 return 0;
129586 }
129587 pTab = p->pSrc->a[0].pTab;
129588 pExpr = p->pEList->a[0].pExpr;
129589 assert( pTab && !pTab->pSelect && pExpr )((void) (0));
129590
129591 if( IsVirtual(pTab)((pTab)->nModuleArg) ) return 0;
129592 if( pExpr->op!=TK_AGG_FUNCTION163 ) return 0;
129593 if( NEVER(pAggInfo->nFunc==0)(pAggInfo->nFunc==0) ) return 0;
129594 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT0x0100)==0 ) return 0;
129595 if( pExpr->flags&EP_Distinct0x000002 ) return 0;
129596
129597 return pTab;
129598}
129599
129600/*
129601** If the source-list item passed as an argument was augmented with an
129602** INDEXED BY clause, then try to locate the specified index. If there
129603** was such a clause and the named index cannot be found, return
129604** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
129605** pFrom->pIndex and return SQLITE_OK.
129606*/
129607SQLITE_PRIVATEstatic int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
129608 if( pFrom->pTab && pFrom->fg.isIndexedBy ){
129609 Table *pTab = pFrom->pTab;
129610 char *zIndexedBy = pFrom->u1.zIndexedBy;
129611 Index *pIdx;
129612 for(pIdx=pTab->pIndex;
129613 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
129614 pIdx=pIdx->pNext
129615 );
129616 if( !pIdx ){
129617 sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
129618 pParse->checkSchema = 1;
129619 return SQLITE_ERROR1;
129620 }
129621 pFrom->pIBIndex = pIdx;
129622 }
129623 return SQLITE_OK0;
129624}
129625/*
129626** Detect compound SELECT statements that use an ORDER BY clause with
129627** an alternative collating sequence.
129628**
129629** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
129630**
129631** These are rewritten as a subquery:
129632**
129633** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
129634** ORDER BY ... COLLATE ...
129635**
129636** This transformation is necessary because the multiSelectOrderBy() routine
129637** above that generates the code for a compound SELECT with an ORDER BY clause
129638** uses a merge algorithm that requires the same collating sequence on the
129639** result columns as on the ORDER BY clause. See ticket
129640** http://www.sqlite.org/src/info/6709574d2a
129641**
129642** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
129643** The UNION ALL operator works fine with multiSelectOrderBy() even when
129644** there are COLLATE terms in the ORDER BY.
129645*/
129646static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
129647 int i;
129648 Select *pNew;
129649 Select *pX;
129650 sqlite3 *db;
129651 struct ExprList_item *a;
129652 SrcList *pNewSrc;
129653 Parse *pParse;
129654 Token dummy;
129655
129656 if( p->pPrior==0 ) return WRC_Continue0;
129657 if( p->pOrderBy==0 ) return WRC_Continue0;
129658 for(pX=p; pX && (pX->op==TK_ALL128 || pX->op==TK_SELECT131); pX=pX->pPrior){}
129659 if( pX==0 ) return WRC_Continue0;
129660 a = p->pOrderBy->a;
129661 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
129662 if( a[i].pExpr->flags & EP_Collate0x000100 ) break;
129663 }
129664 if( i<0 ) return WRC_Continue0;
129665
129666 /* If we reach this point, that means the transformation is required. */
129667
129668 pParse = pWalker->pParse;
129669 db = pParse->db;
129670 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
129671 if( pNew==0 ) return WRC_Abort2;
129672 memset(&dummy, 0, sizeof(dummy));
129673 pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
129674 if( pNewSrc==0 ) return WRC_Abort2;
129675 *pNew = *p;
129676 p->pSrc = pNewSrc;
129677 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK172, 0));
129678 p->op = TK_SELECT131;
129679 p->pWhere = 0;
129680 pNew->pGroupBy = 0;
129681 pNew->pHaving = 0;
129682 pNew->pOrderBy = 0;
129683 p->pPrior = 0;
129684 p->pNext = 0;
129685 p->pWith = 0;
129686 p->selFlags &= ~SF_Compound0x00100;
129687 assert( (p->selFlags & SF_Converted)==0 )((void) (0));
129688 p->selFlags |= SF_Converted0x10000;
129689 assert( pNew->pPrior!=0 )((void) (0));
129690 pNew->pPrior->pNext = pNew;
129691 pNew->pLimit = 0;
129692 return WRC_Continue0;
129693}
129694
129695/*
129696** Check to see if the FROM clause term pFrom has table-valued function
129697** arguments. If it does, leave an error message in pParse and return
129698** non-zero, since pFrom is not allowed to be a table-valued function.
129699*/
129700static int cannotBeFunction(Parse *pParse, struct SrcList_item *pFrom){
129701 if( pFrom->fg.isTabFunc ){
129702 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
129703 return 1;
129704 }
129705 return 0;
129706}
129707
129708#ifndef SQLITE_OMIT_CTE
129709/*
129710** Argument pWith (which may be NULL) points to a linked list of nested
129711** WITH contexts, from inner to outermost. If the table identified by
129712** FROM clause element pItem is really a common-table-expression (CTE)
129713** then return a pointer to the CTE definition for that table. Otherwise
129714** return NULL.
129715**
129716** If a non-NULL value is returned, set *ppContext to point to the With
129717** object that the returned CTE belongs to.
129718*/
129719static struct Cte *searchWith(
129720 With *pWith, /* Current innermost WITH clause */
129721 struct SrcList_item *pItem, /* FROM clause element to resolve */
129722 With **ppContext /* OUT: WITH clause return value belongs to */
129723){
129724 const char *zName;
129725 if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
129726 With *p;
129727 for(p=pWith; p; p=p->pOuter){
129728 int i;
129729 for(i=0; i<p->nCte; i++){
129730 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
129731 *ppContext = p;
129732 return &p->a[i];
129733 }
129734 }
129735 }
129736 }
129737 return 0;
129738}
129739
129740/* The code generator maintains a stack of active WITH clauses
129741** with the inner-most WITH clause being at the top of the stack.
129742**
129743** This routine pushes the WITH clause passed as the second argument
129744** onto the top of the stack. If argument bFree is true, then this
129745** WITH clause will never be popped from the stack. In this case it
129746** should be freed along with the Parse object. In other cases, when
129747** bFree==0, the With object will be freed along with the SELECT
129748** statement with which it is associated.
129749*/
129750SQLITE_PRIVATEstatic void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
129751 assert( bFree==0 || (pParse->pWith==0 && pParse->pWithToFree==0) )((void) (0));
129752 if( pWith ){
129753 assert( pParse->pWith!=pWith )((void) (0));
129754 pWith->pOuter = pParse->pWith;
129755 pParse->pWith = pWith;
129756 if( bFree ) pParse->pWithToFree = pWith;
129757 }
129758}
129759
129760/*
129761** This function checks if argument pFrom refers to a CTE declared by
129762** a WITH clause on the stack currently maintained by the parser. And,
129763** if currently processing a CTE expression, if it is a recursive
129764** reference to the current CTE.
129765**
129766** If pFrom falls into either of the two categories above, pFrom->pTab
129767** and other fields are populated accordingly. The caller should check
129768** (pFrom->pTab!=0) to determine whether or not a successful match
129769** was found.
129770**
129771** Whether or not a match is found, SQLITE_OK is returned if no error
129772** occurs. If an error does occur, an error message is stored in the
129773** parser and some error code other than SQLITE_OK returned.
129774*/
129775static int withExpand(
129776 Walker *pWalker,
129777 struct SrcList_item *pFrom
129778){
129779 Parse *pParse = pWalker->pParse;
129780 sqlite3 *db = pParse->db;
129781 struct Cte *pCte; /* Matched CTE (or NULL if no match) */
129782 With *pWith; /* WITH clause that pCte belongs to */
129783
129784 assert( pFrom->pTab==0 )((void) (0));
129785
129786 pCte = searchWith(pParse->pWith, pFrom, &pWith);
129787 if( pCte ){
129788 Table *pTab;
129789 ExprList *pEList;
129790 Select *pSel;
129791 Select *pLeft; /* Left-most SELECT statement */
129792 int bMayRecursive; /* True if compound joined by UNION [ALL] */
129793 With *pSavedWith; /* Initial value of pParse->pWith */
129794
129795 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
129796 ** recursive reference to CTE pCte. Leave an error in pParse and return
129797 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
129798 ** In this case, proceed. */
129799 if( pCte->zCteErr ){
129800 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
129801 return SQLITE_ERROR1;
129802 }
129803 if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR1;
129804
129805 assert( pFrom->pTab==0 )((void) (0));
129806 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
129807 if( pTab==0 ) return WRC_Abort2;
129808 pTab->nTabRef = 1;
129809 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
129810 pTab->iPKey = -1;
129811 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
129812 pTab->tabFlags |= TF_Ephemeral0x0002 | TF_NoVisibleRowid0x0040;
129813 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
129814 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT7;
129815 assert( pFrom->pSelect )((void) (0));
129816
129817 /* Check if this is a recursive CTE. */
129818 pSel = pFrom->pSelect;
129819 bMayRecursive = ( pSel->op==TK_ALL128 || pSel->op==TK_UNION127 );
129820 if( bMayRecursive ){
129821 int i;
129822 SrcList *pSrc = pFrom->pSelect->pSrc;
129823 for(i=0; i<pSrc->nSrc; i++){
129824 struct SrcList_item *pItem = &pSrc->a[i];
129825 if( pItem->zDatabase==0
129826 && pItem->zName!=0
129827 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
129828 ){
129829 pItem->pTab = pTab;
129830 pItem->fg.isRecursive = 1;
129831 pTab->nTabRef++;
129832 pSel->selFlags |= SF_Recursive0x02000;
129833 }
129834 }
129835 }
129836
129837 /* Only one recursive reference is permitted. */
129838 if( pTab->nTabRef>2 ){
129839 sqlite3ErrorMsg(
129840 pParse, "multiple references to recursive table: %s", pCte->zName
129841 );
129842 return SQLITE_ERROR1;
129843 }
129844 assert( pTab->nTabRef==1 ||((void) (0))
129845 ((pSel->selFlags&SF_Recursive) && pTab->nTabRef==2 ))((void) (0));
129846
129847 pCte->zCteErr = "circular reference: %s";
129848 pSavedWith = pParse->pWith;
129849 pParse->pWith = pWith;
129850 if( bMayRecursive ){
129851 Select *pPrior = pSel->pPrior;
129852 assert( pPrior->pWith==0 )((void) (0));
129853 pPrior->pWith = pSel->pWith;
129854 sqlite3WalkSelect(pWalker, pPrior);
129855 pPrior->pWith = 0;
129856 }else{
129857 sqlite3WalkSelect(pWalker, pSel);
129858 }
129859 pParse->pWith = pWith;
129860
129861 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
129862 pEList = pLeft->pEList;
129863 if( pCte->pCols ){
129864 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
129865 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
129866 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
129867 );
129868 pParse->pWith = pSavedWith;
129869 return SQLITE_ERROR1;
129870 }
129871 pEList = pCte->pCols;
129872 }
129873
129874 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
129875 if( bMayRecursive ){
129876 if( pSel->selFlags & SF_Recursive0x02000 ){
129877 pCte->zCteErr = "multiple recursive references: %s";
129878 }else{
129879 pCte->zCteErr = "recursive reference in a subquery: %s";
129880 }
129881 sqlite3WalkSelect(pWalker, pSel);
129882 }
129883 pCte->zCteErr = 0;
129884 pParse->pWith = pSavedWith;
129885 }
129886
129887 return SQLITE_OK0;
129888}
129889#endif
129890
129891#ifndef SQLITE_OMIT_CTE
129892/*
129893** If the SELECT passed as the second argument has an associated WITH
129894** clause, pop it from the stack stored as part of the Parse object.
129895**
129896** This function is used as the xSelectCallback2() callback by
129897** sqlite3SelectExpand() when walking a SELECT tree to resolve table
129898** names and other FROM clause elements.
129899*/
129900static void selectPopWith(Walker *pWalker, Select *p){
129901 Parse *pParse = pWalker->pParse;
129902 if( OK_IF_ALWAYS_TRUE(pParse->pWith)(pParse->pWith) && p->pPrior==0 ){
129903 With *pWith = findRightmost(p)->pWith;
129904 if( pWith!=0 ){
129905 assert( pParse->pWith==pWith )((void) (0));
129906 pParse->pWith = pWith->pOuter;
129907 }
129908 }
129909}
129910#else
129911#define selectPopWith 0
129912#endif
129913
129914/*
129915** The SrcList_item structure passed as the second argument represents a
129916** sub-query in the FROM clause of a SELECT statement. This function
129917** allocates and populates the SrcList_item.pTab object. If successful,
129918** SQLITE_OK is returned. Otherwise, if an OOM error is encountered,
129919** SQLITE_NOMEM.
129920*/
129921SQLITE_PRIVATEstatic int sqlite3ExpandSubquery(Parse *pParse, struct SrcList_item *pFrom){
129922 Select *pSel = pFrom->pSelect;
129923 Table *pTab;
129924
129925 assert( pSel )((void) (0));
129926 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
129927 if( pTab==0 ) return SQLITE_NOMEM7;
129928 pTab->nTabRef = 1;
129929 if( pFrom->zAlias ){
129930 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
129931 }else{
129932 pTab->zName = sqlite3MPrintf(pParse->db, "subquery_%u", pSel->selId);
129933 }
129934 while( pSel->pPrior ){ pSel = pSel->pPrior; }
129935 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
129936 pTab->iPKey = -1;
129937 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) )((void) (0));
129938 pTab->tabFlags |= TF_Ephemeral0x0002;
129939
129940 return pParse->nErr ? SQLITE_ERROR1 : SQLITE_OK0;
129941}
129942
129943/*
129944** This routine is a Walker callback for "expanding" a SELECT statement.
129945** "Expanding" means to do the following:
129946**
129947** (1) Make sure VDBE cursor numbers have been assigned to every
129948** element of the FROM clause.
129949**
129950** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
129951** defines FROM clause. When views appear in the FROM clause,
129952** fill pTabList->a[].pSelect with a copy of the SELECT statement
129953** that implements the view. A copy is made of the view's SELECT
129954** statement so that we can freely modify or delete that statement
129955** without worrying about messing up the persistent representation
129956** of the view.
129957**
129958** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword
129959** on joins and the ON and USING clause of joins.
129960**
129961** (4) Scan the list of columns in the result set (pEList) looking
129962** for instances of the "*" operator or the TABLE.* operator.
129963** If found, expand each "*" to be every column in every table
129964** and TABLE.* to be every column in TABLE.
129965**
129966*/
129967static int selectExpander(Walker *pWalker, Select *p){
129968 Parse *pParse = pWalker->pParse;
129969 int i, j, k;
129970 SrcList *pTabList;
129971 ExprList *pEList;
129972 struct SrcList_item *pFrom;
129973 sqlite3 *db = pParse->db;
129974 Expr *pE, *pRight, *pExpr;
129975 u16 selFlags = p->selFlags;
129976 u32 elistFlags = 0;
129977
129978 p->selFlags |= SF_Expanded0x00040;
129979 if( db->mallocFailed ){
129980 return WRC_Abort2;
129981 }
129982 assert( p->pSrc!=0 )((void) (0));
129983 if( (selFlags & SF_Expanded0x00040)!=0 ){
129984 return WRC_Prune1;
129985 }
129986 if( pWalker->eCode ){
129987 /* Renumber selId because it has been copied from a view */
129988 p->selId = ++pParse->nSelect;
129989 }
129990 pTabList = p->pSrc;
129991 pEList = p->pEList;
129992 sqlite3WithPush(pParse, p->pWith, 0);
129993
129994 /* Make sure cursor numbers have been assigned to all entries in
129995 ** the FROM clause of the SELECT statement.
129996 */
129997 sqlite3SrcListAssignCursors(pParse, pTabList);
129998
129999 /* Look up every table named in the FROM clause of the select. If
130000 ** an entry of the FROM clause is a subquery instead of a table or view,
130001 ** then create a transient table structure to describe the subquery.
130002 */
130003 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
130004 Table *pTab;
130005 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 )((void) (0));
130006 if( pFrom->fg.isRecursive ) continue;
130007 assert( pFrom->pTab==0 )((void) (0));
130008#ifndef SQLITE_OMIT_CTE
130009 if( withExpand(pWalker, pFrom) ) return WRC_Abort2;
130010 if( pFrom->pTab ) {} else
130011#endif
130012 if( pFrom->zName==0 ){
130013#ifndef SQLITE_OMIT_SUBQUERY
130014 Select *pSel = pFrom->pSelect;
130015 /* A sub-query in the FROM clause of a SELECT */
130016 assert( pSel!=0 )((void) (0));
130017 assert( pFrom->pTab==0 )((void) (0));
130018 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort2;
130019 if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort2;
130020#endif
130021 }else{
130022 /* An ordinary table or view name in the FROM clause */
130023 assert( pFrom->pTab==0 )((void) (0));
130024 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
130025 if( pTab==0 ) return WRC_Abort2;
130026 if( pTab->nTabRef>=0xffff ){
130027 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
130028 pTab->zName);
130029 pFrom->pTab = 0;
130030 return WRC_Abort2;
130031 }
130032 pTab->nTabRef++;
130033 if( !IsVirtual(pTab)((pTab)->nModuleArg) && cannotBeFunction(pParse, pFrom) ){
130034 return WRC_Abort2;
130035 }
130036#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
130037 if( IsVirtual(pTab)((pTab)->nModuleArg) || pTab->pSelect ){
130038 i16 nCol;
130039 u8 eCodeOrig = pWalker->eCode;
130040 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort2;
130041 assert( pFrom->pSelect==0 )((void) (0));
130042 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
130043 nCol = pTab->nCol;
130044 pTab->nCol = -1;
130045 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
130046 sqlite3WalkSelect(pWalker, pFrom->pSelect);
130047 pWalker->eCode = eCodeOrig;
130048 pTab->nCol = nCol;
130049 }
130050#endif
130051 }
130052
130053 /* Locate the index named by the INDEXED BY clause, if any. */
130054 if( sqlite3IndexedByLookup(pParse, pFrom) ){
130055 return WRC_Abort2;
130056 }
130057 }
130058
130059 /* Process NATURAL keywords, and ON and USING clauses of joins.
130060 */
130061 if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
130062 return WRC_Abort2;
130063 }
130064
130065 /* For every "*" that occurs in the column list, insert the names of
130066 ** all columns in all tables. And for every TABLE.* insert the names
130067 ** of all columns in TABLE. The parser inserted a special expression
130068 ** with the TK_ASTERISK operator for each "*" that it found in the column
130069 ** list. The following code just has to locate the TK_ASTERISK
130070 ** expressions and expand each one to the list of all columns in
130071 ** all tables.
130072 **
130073 ** The first loop just checks to see if there are any "*" operators
130074 ** that need expanding.
130075 */
130076 for(k=0; k<pEList->nExpr; k++){
130077 pE = pEList->a[k].pExpr;
130078 if( pE->op==TK_ASTERISK172 ) break;
130079 assert( pE->op!=TK_DOT || pE->pRight!=0 )((void) (0));
130080 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) )((void) (0));
130081 if( pE->op==TK_DOT134 && pE->pRight->op==TK_ASTERISK172 ) break;
130082 elistFlags |= pE->flags;
130083 }
130084 if( k<pEList->nExpr ){
130085 /*
130086 ** If we get here it means the result set contains one or more "*"
130087 ** operators that need to be expanded. Loop through each expression
130088 ** in the result set and expand them one by one.
130089 */
130090 struct ExprList_item *a = pEList->a;
130091 ExprList *pNew = 0;
130092 int flags = pParse->db->flags;
130093 int longNames = (flags & SQLITE_FullColNames0x00000004)!=0
130094 && (flags & SQLITE_ShortColNames0x00000040)==0;
130095
130096 for(k=0; k<pEList->nExpr; k++){
130097 pE = a[k].pExpr;
130098 elistFlags |= pE->flags;
130099 pRight = pE->pRight;
130100 assert( pE->op!=TK_DOT || pRight!=0 )((void) (0));
130101 if( pE->op!=TK_ASTERISK172
130102 && (pE->op!=TK_DOT134 || pRight->op!=TK_ASTERISK172)
130103 ){
130104 /* This particular expression does not need to be expanded.
130105 */
130106 pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
130107 if( pNew ){
130108 pNew->a[pNew->nExpr-1].zName = a[k].zName;
130109 pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
130110 a[k].zName = 0;
130111 a[k].zSpan = 0;
130112 }
130113 a[k].pExpr = 0;
130114 }else{
130115 /* This expression is a "*" or a "TABLE.*" and needs to be
130116 ** expanded. */
130117 int tableSeen = 0; /* Set to 1 when TABLE matches */
130118 char *zTName = 0; /* text of name of TABLE */
130119 if( pE->op==TK_DOT134 ){
130120 assert( pE->pLeft!=0 )((void) (0));
130121 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) )((void) (0));
130122 zTName = pE->pLeft->u.zToken;
130123 }
130124 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
130125 Table *pTab = pFrom->pTab;
130126 Select *pSub = pFrom->pSelect;
130127 char *zTabName = pFrom->zAlias;
130128 const char *zSchemaName = 0;
130129 int iDb;
130130 if( zTabName==0 ){
130131 zTabName = pTab->zName;
130132 }
130133 if( db->mallocFailed ) break;
130134 if( pSub==0 || (pSub->selFlags & SF_NestedFrom0x00800)==0 ){
130135 pSub = 0;
130136 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
130137 continue;
130138 }
130139 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
130140 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
130141 }
130142 for(j=0; j<pTab->nCol; j++){
130143 char *zName = pTab->aCol[j].zName;
130144 char *zColname; /* The computed column name */
130145 char *zToFree; /* Malloced string that needs to be freed */
130146 Token sColname; /* Computed column name as a token */
130147
130148 assert( zName )((void) (0));
130149 if( zTName && pSub
130150 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
130151 ){
130152 continue;
130153 }
130154
130155 /* If a column is marked as 'hidden', omit it from the expanded
130156 ** result-set list unless the SELECT has the SF_IncludeHidden
130157 ** bit set.
130158 */
130159 if( (p->selFlags & SF_IncludeHidden0x20000)==0
130160 && IsHiddenColumn(&pTab->aCol[j])(((&pTab->aCol[j])->colFlags & 0x0002)!=0)
130161 ){
130162 continue;
130163 }
130164 tableSeen = 1;
130165
130166 if( i>0 && zTName==0 ){
130167 if( (pFrom->fg.jointype & JT_NATURAL0x0004)!=0
130168 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
130169 ){
130170 /* In a NATURAL join, omit the join columns from the
130171 ** table to the right of the join */
130172 continue;
130173 }
130174 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
130175 /* In a join with a USING clause, omit columns in the
130176 ** using clause from the table on the right. */
130177 continue;
130178 }
130179 }
130180 pRight = sqlite3Expr(db, TK_ID59, zName);
130181 zColname = zName;
130182 zToFree = 0;
130183 if( longNames || pTabList->nSrc>1 ){
130184 Expr *pLeft;
130185 pLeft = sqlite3Expr(db, TK_ID59, zTabName);
130186 pExpr = sqlite3PExpr(pParse, TK_DOT134, pLeft, pRight);
130187 if( zSchemaName ){
130188 pLeft = sqlite3Expr(db, TK_ID59, zSchemaName);
130189 pExpr = sqlite3PExpr(pParse, TK_DOT134, pLeft, pExpr);
130190 }
130191 if( longNames ){
130192 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
130193 zToFree = zColname;
130194 }
130195 }else{
130196 pExpr = pRight;
130197 }
130198 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
130199 sqlite3TokenInit(&sColname, zColname);
130200 sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
130201 if( pNew && (p->selFlags & SF_NestedFrom0x00800)!=0 ){
130202 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
130203 if( pSub ){
130204 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
130205 testcase( pX->zSpan==0 );
130206 }else{
130207 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
130208 zSchemaName, zTabName, zColname);
130209 testcase( pX->zSpan==0 );
130210 }
130211 pX->bSpanIsTab = 1;
130212 }
130213 sqlite3DbFree(db, zToFree);
130214 }
130215 }
130216 if( !tableSeen ){
130217 if( zTName ){
130218 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
130219 }else{
130220 sqlite3ErrorMsg(pParse, "no tables specified");
130221 }
130222 }
130223 }
130224 }
130225 sqlite3ExprListDelete(db, pEList);
130226 p->pEList = pNew;
130227 }
130228 if( p->pEList ){
130229 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN2] ){
130230 sqlite3ErrorMsg(pParse, "too many columns in result set");
130231 return WRC_Abort2;
130232 }
130233 if( (elistFlags & (EP_HasFunc0x000004|EP_Subquery0x200000))!=0 ){
130234 p->selFlags |= SF_ComplexResult0x40000;
130235 }
130236 }
130237 return WRC_Continue0;
130238}
130239
130240/*
130241** No-op routine for the parse-tree walker.
130242**
130243** When this routine is the Walker.xExprCallback then expression trees
130244** are walked without any actions being taken at each node. Presumably,
130245** when this routine is used for Walker.xExprCallback then
130246** Walker.xSelectCallback is set to do something useful for every
130247** subquery in the parser tree.
130248*/
130249SQLITE_PRIVATEstatic int sqlite3ExprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
130250 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
130251 return WRC_Continue0;
130252}
130253
130254/*
130255** No-op routine for the parse-tree walker for SELECT statements.
130256** subquery in the parser tree.
130257*/
130258SQLITE_PRIVATEstatic int sqlite3SelectWalkNoop(Walker *NotUsed, Select *NotUsed2){
130259 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
130260 return WRC_Continue0;
130261}
130262
130263#if SQLITE_DEBUG
130264/*
130265** Always assert. This xSelectCallback2 implementation proves that the
130266** xSelectCallback2 is never invoked.
130267*/
130268SQLITE_PRIVATEstatic void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){
130269 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
130270 assert( 0 )((void) (0));
130271}
130272#endif
130273/*
130274** This routine "expands" a SELECT statement and all of its subqueries.
130275** For additional information on what it means to "expand" a SELECT
130276** statement, see the comment on the selectExpand worker callback above.
130277**
130278** Expanding a SELECT statement is the first step in processing a
130279** SELECT statement. The SELECT statement must be expanded before
130280** name resolution is performed.
130281**
130282** If anything goes wrong, an error message is written into pParse.
130283** The calling function can detect the problem by looking at pParse->nErr
130284** and/or pParse->db->mallocFailed.
130285*/
130286static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
130287 Walker w;
130288 w.xExprCallback = sqlite3ExprWalkNoop;
130289 w.pParse = pParse;
130290 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound)(pParse->hasCompound) ){
130291 w.xSelectCallback = convertCompoundSelectToSubquery;
130292 w.xSelectCallback2 = 0;
130293 sqlite3WalkSelect(&w, pSelect);
130294 }
130295 w.xSelectCallback = selectExpander;
130296 w.xSelectCallback2 = selectPopWith;
130297 w.eCode = 0;
130298 sqlite3WalkSelect(&w, pSelect);
130299}
130300
130301
130302#ifndef SQLITE_OMIT_SUBQUERY
130303/*
130304** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
130305** interface.
130306**
130307** For each FROM-clause subquery, add Column.zType and Column.zColl
130308** information to the Table structure that represents the result set
130309** of that subquery.
130310**
130311** The Table structure that represents the result set was constructed
130312** by selectExpander() but the type and collation information was omitted
130313** at that point because identifiers had not yet been resolved. This
130314** routine is called after identifier resolution.
130315*/
130316static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
130317 Parse *pParse;
130318 int i;
130319 SrcList *pTabList;
130320 struct SrcList_item *pFrom;
130321
130322 assert( p->selFlags & SF_Resolved )((void) (0));
130323 if( p->selFlags & SF_HasTypeInfo0x00080 ) return;
130324 p->selFlags |= SF_HasTypeInfo0x00080;
130325 pParse = pWalker->pParse;
130326 pTabList = p->pSrc;
130327 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
130328 Table *pTab = pFrom->pTab;
130329 assert( pTab!=0 )((void) (0));
130330 if( (pTab->tabFlags & TF_Ephemeral0x0002)!=0 ){
130331 /* A sub-query in the FROM clause of a SELECT */
130332 Select *pSel = pFrom->pSelect;
130333 if( pSel ){
130334 while( pSel->pPrior ) pSel = pSel->pPrior;
130335 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel);
130336 }
130337 }
130338 }
130339}
130340#endif
130341
130342
130343/*
130344** This routine adds datatype and collating sequence information to
130345** the Table structures of all FROM-clause subqueries in a
130346** SELECT statement.
130347**
130348** Use this routine after name resolution.
130349*/
130350static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
130351#ifndef SQLITE_OMIT_SUBQUERY
130352 Walker w;
130353 w.xSelectCallback = sqlite3SelectWalkNoop;
130354 w.xSelectCallback2 = selectAddSubqueryTypeInfo;
130355 w.xExprCallback = sqlite3ExprWalkNoop;
130356 w.pParse = pParse;
130357 sqlite3WalkSelect(&w, pSelect);
130358#endif
130359}
130360
130361
130362/*
130363** This routine sets up a SELECT statement for processing. The
130364** following is accomplished:
130365**
130366** * VDBE Cursor numbers are assigned to all FROM-clause terms.
130367** * Ephemeral Table objects are created for all FROM-clause subqueries.
130368** * ON and USING clauses are shifted into WHERE statements
130369** * Wildcards "*" and "TABLE.*" in result sets are expanded.
130370** * Identifiers in expression are matched to tables.
130371**
130372** This routine acts recursively on all subqueries within the SELECT.
130373*/
130374SQLITE_PRIVATEstatic void sqlite3SelectPrep(
130375 Parse *pParse, /* The parser context */
130376 Select *p, /* The SELECT statement being coded. */
130377 NameContext *pOuterNC /* Name context for container */
130378){
130379 assert( p!=0 || pParse->db->mallocFailed )((void) (0));
130380 if( pParse->db->mallocFailed ) return;
130381 if( p->selFlags & SF_HasTypeInfo0x00080 ) return;
130382 sqlite3SelectExpand(pParse, p);
130383 if( pParse->nErr || pParse->db->mallocFailed ) return;
130384 sqlite3ResolveSelectNames(pParse, p, pOuterNC);
130385 if( pParse->nErr || pParse->db->mallocFailed ) return;
130386 sqlite3SelectAddTypeInfo(pParse, p);
130387}
130388
130389/*
130390** Reset the aggregate accumulator.
130391**
130392** The aggregate accumulator is a set of memory cells that hold
130393** intermediate results while calculating an aggregate. This
130394** routine generates code that stores NULLs in all of those memory
130395** cells.
130396*/
130397static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
130398 Vdbe *v = pParse->pVdbe;
130399 int i;
130400 struct AggInfo_func *pFunc;
130401 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
130402 if( nReg==0 ) return;
130403#ifdef SQLITE_DEBUG
130404 /* Verify that all AggInfo registers are within the range specified by
130405 ** AggInfo.mnReg..AggInfo.mxReg */
130406 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 )((void) (0));
130407 for(i=0; i<pAggInfo->nColumn; i++){
130408 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg((void) (0))
130409 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg )((void) (0));
130410 }
130411 for(i=0; i<pAggInfo->nFunc; i++){
130412 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg((void) (0))
130413 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg )((void) (0));
130414 }
130415#endif
130416 sqlite3VdbeAddOp3(v, OP_Null73, 0, pAggInfo->mnReg, pAggInfo->mxReg);
130417 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
130418 if( pFunc->iDistinct>=0 ){
130419 Expr *pE = pFunc->pExpr;
130420 assert( !ExprHasProperty(pE, EP_xIsSelect) )((void) (0));
130421 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
130422 sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
130423 "argument");
130424 pFunc->iDistinct = -1;
130425 }else{
130426 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
130427 sqlite3VdbeAddOp4(v, OP_OpenEphemeral113, pFunc->iDistinct, 0, 0,
130428 (char*)pKeyInfo, P4_KEYINFO(-9));
130429 }
130430 }
130431 }
130432}
130433
130434/*
130435** Invoke the OP_AggFinalize opcode for every aggregate function
130436** in the AggInfo structure.
130437*/
130438static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
130439 Vdbe *v = pParse->pVdbe;
130440 int i;
130441 struct AggInfo_func *pF;
130442 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
130443 ExprList *pList = pF->pExpr->x.pList;
130444 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) )((void) (0));
130445 sqlite3VdbeAddOp2(v, OP_AggFinal157, pF->iMem, pList ? pList->nExpr : 0);
130446 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-8));
130447 }
130448}
130449
130450
130451/*
130452** Update the accumulator memory cells for an aggregate based on
130453** the current cursor position.
130454**
130455** If regAcc is non-zero and there are no min() or max() aggregates
130456** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
130457** registers if register regAcc contains 0. The caller will take care
130458** of setting and clearing regAcc.
130459*/
130460static void updateAccumulator(Parse *pParse, int regAcc, AggInfo *pAggInfo){
130461 Vdbe *v = pParse->pVdbe;
130462 int i;
130463 int regHit = 0;
130464 int addrHitTest = 0;
130465 struct AggInfo_func *pF;
130466 struct AggInfo_col *pC;
130467
130468 pAggInfo->directMode = 1;
130469 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
130470 int nArg;
130471 int addrNext = 0;
130472 int regAgg;
130473 ExprList *pList = pF->pExpr->x.pList;
130474 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) )((void) (0));
130475 if( pList ){
130476 nArg = pList->nExpr;
130477 regAgg = sqlite3GetTempRange(pParse, nArg);
130478 sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP0x01);
130479 }else{
130480 nArg = 0;
130481 regAgg = 0;
130482 }
130483 if( pF->iDistinct>=0 ){
130484 addrNext = sqlite3VdbeMakeLabel(pParse);
130485 testcase( nArg==0 ); /* Error condition */
130486 testcase( nArg>1 ); /* Also an error */
130487 codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
130488 }
130489 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
130490 CollSeq *pColl = 0;
130491 struct ExprList_item *pItem;
130492 int j;
130493 assert( pList!=0 )((void) (0)); /* pList!=0 if pF->pFunc has NEEDCOLL */
130494 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
130495 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
130496 }
130497 if( !pColl ){
130498 pColl = pParse->db->pDfltColl;
130499 }
130500 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
130501 sqlite3VdbeAddOp4(v, OP_CollSeq82, regHit, 0, 0, (char *)pColl, P4_COLLSEQ(-2));
130502 }
130503 sqlite3VdbeAddOp3(v, OP_AggStep154, 0, regAgg, pF->iMem);
130504 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF(-8));
130505 sqlite3VdbeChangeP5(v, (u8)nArg);
130506 sqlite3ReleaseTempRange(pParse, regAgg, nArg);
130507 if( addrNext ){
130508 sqlite3VdbeResolveLabel(v, addrNext);
130509 }
130510 }
130511 if( regHit==0 && pAggInfo->nAccumulator ){
130512 regHit = regAcc;
130513 }
130514 if( regHit ){
130515 addrHitTest = sqlite3VdbeAddOp1(v, OP_If18, regHit); VdbeCoverage(v);
130516 }
130517 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
130518 sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
130519 }
130520 pAggInfo->directMode = 0;
130521 if( addrHitTest ){
130522 sqlite3VdbeJumpHere(v, addrHitTest);
130523 }
130524}
130525
130526/*
130527** Add a single OP_Explain instruction to the VDBE to explain a simple
130528** count(*) query ("SELECT count(*) FROM pTab").
130529*/
130530#ifndef SQLITE_OMIT_EXPLAIN
130531static void explainSimpleCount(
130532 Parse *pParse, /* Parse context */
130533 Table *pTab, /* Table being queried */
130534 Index *pIdx /* Index used to optimize scan, or NULL */
130535){
130536 if( pParse->explain==2 ){
130537 int bCover = (pIdx!=0 && (HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) || !IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2)));
130538 sqlite3VdbeExplain(pParse, 0, "SCAN TABLE %s%s%s",
130539 pTab->zName,
130540 bCover ? " USING COVERING INDEX " : "",
130541 bCover ? pIdx->zName : ""
130542 );
130543 }
130544}
130545#else
130546# define explainSimpleCount(a,b,c)
130547#endif
130548
130549/*
130550** sqlite3WalkExpr() callback used by havingToWhere().
130551**
130552** If the node passed to the callback is a TK_AND node, return
130553** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes.
130554**
130555** Otherwise, return WRC_Prune. In this case, also check if the
130556** sub-expression matches the criteria for being moved to the WHERE
130557** clause. If so, add it to the WHERE clause and replace the sub-expression
130558** within the HAVING expression with a constant "1".
130559*/
130560static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){
130561 if( pExpr->op!=TK_AND44 ){
130562 Select *pS = pWalker->u.pSelect;
130563 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
130564 sqlite3 *db = pWalker->pParse->db;
130565 Expr *pNew = sqlite3ExprAlloc(db, TK_INTEGER147, &sqlite3IntTokens[1], 0);
130566 if( pNew ){
130567 Expr *pWhere = pS->pWhere;
130568 SWAP(Expr, *pNew, *pExpr){Expr t=*pNew; *pNew=*pExpr; *pExpr=t;};
130569 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
130570 pS->pWhere = pNew;
130571 pWalker->eCode = 1;
130572 }
130573 }
130574 return WRC_Prune1;
130575 }
130576 return WRC_Continue0;
130577}
130578
130579/*
130580** Transfer eligible terms from the HAVING clause of a query, which is
130581** processed after grouping, to the WHERE clause, which is processed before
130582** grouping. For example, the query:
130583**
130584** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=?
130585**
130586** can be rewritten as:
130587**
130588** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=?
130589**
130590** A term of the HAVING expression is eligible for transfer if it consists
130591** entirely of constants and expressions that are also GROUP BY terms that
130592** use the "BINARY" collation sequence.
130593*/
130594static void havingToWhere(Parse *pParse, Select *p){
130595 Walker sWalker;
130596 memset(&sWalker, 0, sizeof(sWalker));
130597 sWalker.pParse = pParse;
130598 sWalker.xExprCallback = havingToWhereExprCb;
130599 sWalker.u.pSelect = p;
130600 sqlite3WalkExpr(&sWalker, p->pHaving);
130601#if SELECTTRACE_ENABLED0
130602 if( sWalker.eCode && (sqlite3SelectTrace & 0x100)!=0 ){
130603 SELECTTRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n"));
130604 sqlite3TreeViewSelect(0, p, 0);
130605 }
130606#endif
130607}
130608
130609/*
130610** Check to see if the pThis entry of pTabList is a self-join of a prior view.
130611** If it is, then return the SrcList_item for the prior view. If it is not,
130612** then return 0.
130613*/
130614static struct SrcList_item *isSelfJoinView(
130615 SrcList *pTabList, /* Search for self-joins in this FROM clause */
130616 struct SrcList_item *pThis /* Search for prior reference to this subquery */
130617){
130618 struct SrcList_item *pItem;
130619 for(pItem = pTabList->a; pItem<pThis; pItem++){
130620 Select *pS1;
130621 if( pItem->pSelect==0 ) continue;
130622 if( pItem->fg.viaCoroutine ) continue;
130623 if( pItem->zName==0 ) continue;
130624 assert( pItem->pTab!=0 )((void) (0));
130625 assert( pThis->pTab!=0 )((void) (0));
130626 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
130627 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
130628 pS1 = pItem->pSelect;
130629 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
130630 /* The query flattener left two different CTE tables with identical
130631 ** names in the same FROM clause. */
130632 continue;
130633 }
130634 if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1)
130635 || sqlite3ExprCompare(0, pThis->pSelect->pHaving, pS1->pHaving, -1)
130636 ){
130637 /* The view was modified by some other optimization such as
130638 ** pushDownWhereTerms() */
130639 continue;
130640 }
130641 return pItem;
130642 }
130643 return 0;
130644}
130645
130646#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
130647/*
130648** Attempt to transform a query of the form
130649**
130650** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2)
130651**
130652** Into this:
130653**
130654** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2)
130655**
130656** The transformation only works if all of the following are true:
130657**
130658** * The subquery is a UNION ALL of two or more terms
130659** * The subquery does not have a LIMIT clause
130660** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries
130661** * The outer query is a simple count(*) with no WHERE clause or other
130662** extraneous syntax.
130663**
130664** Return TRUE if the optimization is undertaken.
130665*/
130666static int countOfViewOptimization(Parse *pParse, Select *p){
130667 Select *pSub, *pPrior;
130668 Expr *pExpr;
130669 Expr *pCount;
130670 sqlite3 *db;
130671 if( (p->selFlags & SF_Aggregate0x00008)==0 ) return 0; /* This is an aggregate */
130672 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
130673 if( p->pWhere ) return 0;
130674 if( p->pGroupBy ) return 0;
130675 pExpr = p->pEList->a[0].pExpr;
130676 if( pExpr->op!=TK_AGG_FUNCTION163 ) return 0; /* Result is an aggregate */
130677 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
130678 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
130679 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
130680 pSub = p->pSrc->a[0].pSelect;
130681 if( pSub==0 ) return 0; /* The FROM is a subquery */
130682 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
130683 do{
130684 if( pSub->op!=TK_ALL128 && pSub->pPrior ) return 0; /* Must be UNION ALL */
130685 if( pSub->pWhere ) return 0; /* No WHERE clause */
130686 if( pSub->pLimit ) return 0; /* No LIMIT clause */
130687 if( pSub->selFlags & SF_Aggregate0x00008 ) return 0; /* Not an aggregate */
130688 pSub = pSub->pPrior; /* Repeat over compound */
130689 }while( pSub );
130690
130691 /* If we reach this point then it is OK to perform the transformation */
130692
130693 db = pParse->db;
130694 pCount = pExpr;
130695 pExpr = 0;
130696 pSub = p->pSrc->a[0].pSelect;
130697 p->pSrc->a[0].pSelect = 0;
130698 sqlite3SrcListDelete(db, p->pSrc);
130699 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
130700 while( pSub ){
130701 Expr *pTerm;
130702 pPrior = pSub->pPrior;
130703 pSub->pPrior = 0;
130704 pSub->pNext = 0;
130705 pSub->selFlags |= SF_Aggregate0x00008;
130706 pSub->selFlags &= ~SF_Compound0x00100;
130707 pSub->nSelectRow = 0;
130708 sqlite3ExprListDelete(db, pSub->pEList);
130709 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
130710 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
130711 pTerm = sqlite3PExpr(pParse, TK_SELECT131, 0, 0);
130712 sqlite3PExprAddSelect(pParse, pTerm, pSub);
130713 if( pExpr==0 ){
130714 pExpr = pTerm;
130715 }else{
130716 pExpr = sqlite3PExpr(pParse, TK_PLUS100, pTerm, pExpr);
130717 }
130718 pSub = pPrior;
130719 }
130720 p->pEList->a[0].pExpr = pExpr;
130721 p->selFlags &= ~SF_Aggregate0x00008;
130722
130723#if SELECTTRACE_ENABLED0
130724 if( sqlite3SelectTrace & 0x400 ){
130725 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
130726 sqlite3TreeViewSelect(0, p, 0);
130727 }
130728#endif
130729 return 1;
130730}
130731#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
130732
130733/*
130734** Generate code for the SELECT statement given in the p argument.
130735**
130736** The results are returned according to the SelectDest structure.
130737** See comments in sqliteInt.h for further information.
130738**
130739** This routine returns the number of errors. If any errors are
130740** encountered, then an appropriate error message is left in
130741** pParse->zErrMsg.
130742**
130743** This routine does NOT free the Select structure passed in. The
130744** calling function needs to do that.
130745*/
130746SQLITE_PRIVATEstatic int sqlite3Select(
130747 Parse *pParse, /* The parser context */
130748 Select *p, /* The SELECT statement being coded. */
130749 SelectDest *pDest /* What to do with the query results */
130750){
130751 int i, j; /* Loop counters */
130752 WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */
130753 Vdbe *v; /* The virtual machine under construction */
130754 int isAgg; /* True for select lists like "count(*)" */
130755 ExprList *pEList = 0; /* List of columns to extract. */
130756 SrcList *pTabList; /* List of tables to select from */
130757 Expr *pWhere; /* The WHERE clause. May be NULL */
130758 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
130759 Expr *pHaving; /* The HAVING clause. May be NULL */
130760 int rc = 1; /* Value to return from this function */
130761 DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
130762 SortCtx sSort; /* Info on how to code the ORDER BY clause */
130763 AggInfo sAggInfo; /* Information used by aggregate queries */
130764 int iEnd; /* Address of the end of the query */
130765 sqlite3 *db; /* The database connection */
130766 ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */
130767 u8 minMaxFlag; /* Flag for min/max queries */
130768
130769 db = pParse->db;
130770 v = sqlite3GetVdbe(pParse);
130771 if( p==0 || db->mallocFailed || pParse->nErr ){
130772 return 1;
130773 }
130774 if( sqlite3AuthCheck(pParse, SQLITE_SELECT21, 0, 0, 0) ) return 1;
130775 memset(&sAggInfo, 0, sizeof(sAggInfo));
130776#if SELECTTRACE_ENABLED0
130777 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
130778 if( sqlite3SelectTrace & 0x100 ){
130779 sqlite3TreeViewSelect(0, p, 0);
130780 }
130781#endif
130782
130783 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo )((void) (0));
130784 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo )((void) (0));
130785 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue )((void) (0));
130786 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue )((void) (0));
130787 if( IgnorableOrderby(pDest)((pDest->eDest)<=8) ){
130788 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||((void) (0))
130789 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||((void) (0))
130790 pDest->eDest==SRT_Queue || pDest->eDest==SRT_DistFifo ||((void) (0))
130791 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_Fifo)((void) (0));
130792 /* If ORDER BY makes no difference in the output then neither does
130793 ** DISTINCT so it can be removed too. */
130794 sqlite3ExprListDelete(db, p->pOrderBy);
130795 p->pOrderBy = 0;
130796 p->selFlags &= ~SF_Distinct0x00001;
130797 }
130798 sqlite3SelectPrep(pParse, p, 0);
130799 if( pParse->nErr || db->mallocFailed ){
130800 goto select_end;
130801 }
130802 assert( p->pEList!=0 )((void) (0));
130803#if SELECTTRACE_ENABLED0
130804 if( sqlite3SelectTrace & 0x104 ){
130805 SELECTTRACE(0x104,pParse,p, ("after name resolution:\n"));
130806 sqlite3TreeViewSelect(0, p, 0);
130807 }
130808#endif
130809
130810 if( pDest->eDest==SRT_Output9 ){
130811 generateColumnNames(pParse, p);
130812 }
130813
130814#ifndef SQLITE_OMIT_WINDOWFUNC
130815 if( sqlite3WindowRewrite(pParse, p) ){
130816 goto select_end;
130817 }
130818#if SELECTTRACE_ENABLED0
130819 if( sqlite3SelectTrace & 0x108 ){
130820 SELECTTRACE(0x104,pParse,p, ("after window rewrite:\n"));
130821 sqlite3TreeViewSelect(0, p, 0);
130822 }
130823#endif
130824#endif /* SQLITE_OMIT_WINDOWFUNC */
130825 pTabList = p->pSrc;
130826 isAgg = (p->selFlags & SF_Aggregate0x00008)!=0;
130827 memset(&sSort, 0, sizeof(sSort));
130828 sSort.pOrderBy = p->pOrderBy;
130829
130830 /* Try to various optimizations (flattening subqueries, and strength
130831 ** reduction of join operators) in the FROM clause up into the main query
130832 */
130833#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
130834 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
130835 struct SrcList_item *pItem = &pTabList->a[i];
130836 Select *pSub = pItem->pSelect;
130837 Table *pTab = pItem->pTab;
130838
130839 /* Convert LEFT JOIN into JOIN if there are terms of the right table
130840 ** of the LEFT JOIN used in the WHERE clause.
130841 */
130842 if( (pItem->fg.jointype & JT_LEFT0x0008)!=0
130843 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
130844 && OptimizationEnabled(db, SQLITE_SimplifyJoin)(((db)->dbOptFlags&(0x2000))==0)
130845 ){
130846 SELECTTRACE(0x100,pParse,p,
130847 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
130848 pItem->fg.jointype &= ~(JT_LEFT0x0008|JT_OUTER0x0020);
130849 unsetJoinExpr(p->pWhere, pItem->iCursor);
130850 }
130851
130852 /* No futher action if this term of the FROM clause is no a subquery */
130853 if( pSub==0 ) continue;
130854
130855 /* Catch mismatch in the declared columns of a view and the number of
130856 ** columns in the SELECT on the RHS */
130857 if( pTab->nCol!=pSub->pEList->nExpr ){
130858 sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d",
130859 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
130860 goto select_end;
130861 }
130862
130863 /* Do not try to flatten an aggregate subquery.
130864 **
130865 ** Flattening an aggregate subquery is only possible if the outer query
130866 ** is not a join. But if the outer query is not a join, then the subquery
130867 ** will be implemented as a co-routine and there is no advantage to
130868 ** flattening in that case.
130869 */
130870 if( (pSub->selFlags & SF_Aggregate0x00008)!=0 ) continue;
130871 assert( pSub->pGroupBy==0 )((void) (0));
130872
130873 /* If the outer query contains a "complex" result set (that is,
130874 ** if the result set of the outer query uses functions or subqueries)
130875 ** and if the subquery contains an ORDER BY clause and if
130876 ** it will be implemented as a co-routine, then do not flatten. This
130877 ** restriction allows SQL constructs like this:
130878 **
130879 ** SELECT expensive_function(x)
130880 ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
130881 **
130882 ** The expensive_function() is only computed on the 10 rows that
130883 ** are output, rather than every row of the table.
130884 **
130885 ** The requirement that the outer query have a complex result set
130886 ** means that flattening does occur on simpler SQL constraints without
130887 ** the expensive_function() like:
130888 **
130889 ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
130890 */
130891 if( pSub->pOrderBy!=0
130892 && i==0
130893 && (p->selFlags & SF_ComplexResult0x40000)!=0
130894 && (pTabList->nSrc==1
130895 || (pTabList->a[1].fg.jointype&(JT_LEFT0x0008|JT_CROSS0x0002))!=0)
130896 ){
130897 continue;
130898 }
130899
130900 if( flattenSubquery(pParse, p, i, isAgg) ){
130901 if( pParse->nErr ) goto select_end;
130902 /* This subquery can be absorbed into its parent. */
130903 i = -1;
130904 }
130905 pTabList = p->pSrc;
130906 if( db->mallocFailed ) goto select_end;
130907 if( !IgnorableOrderby(pDest)((pDest->eDest)<=8) ){
130908 sSort.pOrderBy = p->pOrderBy;
130909 }
130910 }
130911#endif
130912
130913#ifndef SQLITE_OMIT_COMPOUND_SELECT
130914 /* Handle compound SELECT statements using the separate multiSelect()
130915 ** procedure.
130916 */
130917 if( p->pPrior ){
130918 rc = multiSelect(pParse, p, pDest);
130919#if SELECTTRACE_ENABLED0
130920 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
130921 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)sqlite3VdbeExplainParent(pParse)==0 ){
130922 sqlite3TreeViewSelect(0, p, 0);
130923 }
130924#endif
130925 if( p->pNext==0 ) ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
130926 return rc;
130927 }
130928#endif
130929
130930 /* Do the WHERE-clause constant propagation optimization if this is
130931 ** a join. No need to speed time on this operation for non-join queries
130932 ** as the equivalent optimization will be handled by query planner in
130933 ** sqlite3WhereBegin().
130934 */
130935 if( pTabList->nSrc>1
130936 && OptimizationEnabled(db, SQLITE_PropagateConst)(((db)->dbOptFlags&(0x8000))==0)
130937 && propagateConstants(pParse, p)
130938 ){
130939#if SELECTTRACE_ENABLED0
130940 if( sqlite3SelectTrace & 0x100 ){
130941 SELECTTRACE(0x100,pParse,p,("After constant propagation:\n"));
130942 sqlite3TreeViewSelect(0, p, 0);
130943 }
130944#endif
130945 }else{
130946 SELECTTRACE(0x100,pParse,p,("Constant propagation not helpful\n"));
130947 }
130948
130949#ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION
130950 if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView)(((db)->dbOptFlags&(0x0001|0x0200))==0)
130951 && countOfViewOptimization(pParse, p)
130952 ){
130953 if( db->mallocFailed ) goto select_end;
130954 pEList = p->pEList;
130955 pTabList = p->pSrc;
130956 }
130957#endif
130958
130959 /* For each term in the FROM clause, do two things:
130960 ** (1) Authorized unreferenced tables
130961 ** (2) Generate code for all sub-queries
130962 */
130963 for(i=0; i<pTabList->nSrc; i++){
130964 struct SrcList_item *pItem = &pTabList->a[i];
130965 SelectDest dest;
130966 Select *pSub;
130967#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
130968 const char *zSavedAuthContext;
130969#endif
130970
130971 /* Issue SQLITE_READ authorizations with a fake column name for any
130972 ** tables that are referenced but from which no values are extracted.
130973 ** Examples of where these kinds of null SQLITE_READ authorizations
130974 ** would occur:
130975 **
130976 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
130977 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
130978 **
130979 ** The fake column name is an empty string. It is possible for a table to
130980 ** have a column named by the empty string, in which case there is no way to
130981 ** distinguish between an unreferenced table and an actual reference to the
130982 ** "" column. The original design was for the fake column name to be a NULL,
130983 ** which would be unambiguous. But legacy authorization callbacks might
130984 ** assume the column name is non-NULL and segfault. The use of an empty
130985 ** string for the fake column name seems safer.
130986 */
130987 if( pItem->colUsed==0 ){
130988 sqlite3AuthCheck(pParse, SQLITE_READ20, pItem->zName, "", pItem->zDatabase);
130989 }
130990
130991#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
130992 /* Generate code for all sub-queries in the FROM clause
130993 */
130994 pSub = pItem->pSelect;
130995 if( pSub==0 ) continue;
130996
130997 /* The code for a subquery should only be generated once, though it is
130998 ** technically harmless for it to be generated multiple times. The
130999 ** following assert() will detect if something changes to cause
131000 ** the same subquery to be coded multiple times, as a signal to the
131001 ** developers to try to optimize the situation. */
131002 assert( pItem->addrFillSub==0 )((void) (0));
131003
131004 /* Increment Parse.nHeight by the height of the largest expression
131005 ** tree referred to by this, the parent select. The child select
131006 ** may contain expression trees of at most
131007 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
131008 ** more conservative than necessary, but much easier than enforcing
131009 ** an exact limit.
131010 */
131011 pParse->nHeight += sqlite3SelectExprHeight(p);
131012
131013 /* Make copies of constant WHERE-clause terms in the outer query down
131014 ** inside the subquery. This can help the subquery to run more efficiently.
131015 */
131016 if( OptimizationEnabled(db, SQLITE_PushDown)(((db)->dbOptFlags&(0x1000))==0)
131017 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
131018 (pItem->fg.jointype & JT_OUTER0x0020)!=0)
131019 ){
131020#if SELECTTRACE_ENABLED0
131021 if( sqlite3SelectTrace & 0x100 ){
131022 SELECTTRACE(0x100,pParse,p,
131023 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
131024 sqlite3TreeViewSelect(0, p, 0);
131025 }
131026#endif
131027 }else{
131028 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
131029 }
131030
131031 zSavedAuthContext = pParse->zAuthContext;
131032 pParse->zAuthContext = pItem->zName;
131033
131034 /* Generate code to implement the subquery
131035 **
131036 ** The subquery is implemented as a co-routine if the subquery is
131037 ** guaranteed to be the outer loop (so that it does not need to be
131038 ** computed more than once)
131039 **
131040 ** TODO: Are there other reasons beside (1) to use a co-routine
131041 ** implementation?
131042 */
131043 if( i==0
131044 && (pTabList->nSrc==1
131045 || (pTabList->a[1].fg.jointype&(JT_LEFT0x0008|JT_CROSS0x0002))!=0) /* (1) */
131046 ){
131047 /* Implement a co-routine that will return a single row of the result
131048 ** set on each invocation.
131049 */
131050 int addrTop = sqlite3VdbeCurrentAddr(v)+1;
131051
131052 pItem->regReturn = ++pParse->nMem;
131053 sqlite3VdbeAddOp3(v, OP_InitCoroutine13, pItem->regReturn, 0, addrTop);
131054 VdbeComment((v, "%s", pItem->pTab->zName));
131055 pItem->addrFillSub = addrTop;
131056 sqlite3SelectDestInit(&dest, SRT_Coroutine13, pItem->regReturn);
131057 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %u", pSub->selId))sqlite3VdbeExplain (pParse, 1, "CO-ROUTINE %u", pSub->selId
)
;
131058 sqlite3Select(pParse, pSub, &dest);
131059 pItem->pTab->nRowLogEst = pSub->nSelectRow;
131060 pItem->fg.viaCoroutine = 1;
131061 pItem->regResult = dest.iSdst;
131062 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
131063 sqlite3VdbeJumpHere(v, addrTop-1);
131064 sqlite3ClearTempRegCache(pParse);
131065 }else{
131066 /* Generate a subroutine that will fill an ephemeral table with
131067 ** the content of this subquery. pItem->addrFillSub will point
131068 ** to the address of the generated subroutine. pItem->regReturn
131069 ** is a register allocated to hold the subroutine return address
131070 */
131071 int topAddr;
131072 int onceAddr = 0;
131073 int retAddr;
131074 struct SrcList_item *pPrior;
131075
131076 assert( pItem->addrFillSub==0 )((void) (0));
131077 pItem->regReturn = ++pParse->nMem;
131078 topAddr = sqlite3VdbeAddOp2(v, OP_Integer70, 0, pItem->regReturn);
131079 pItem->addrFillSub = topAddr+1;
131080 if( pItem->fg.isCorrelated==0 ){
131081 /* If the subquery is not correlated and if we are not inside of
131082 ** a trigger, then we only need to compute the value of the subquery
131083 ** once. */
131084 onceAddr = sqlite3VdbeAddOp0(v, OP_Once17); VdbeCoverage(v);
131085 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
131086 }else{
131087 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
131088 }
131089 pPrior = isSelfJoinView(pTabList, pItem);
131090 if( pPrior ){
131091 sqlite3VdbeAddOp2(v, OP_OpenDup111, pItem->iCursor, pPrior->iCursor);
131092 assert( pPrior->pSelect!=0 )((void) (0));
131093 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
131094 }else{
131095 sqlite3SelectDestInit(&dest, SRT_EphemTab12, pItem->iCursor);
131096 ExplainQueryPlan((pParse, 1, "MATERIALIZE %u", pSub->selId))sqlite3VdbeExplain (pParse, 1, "MATERIALIZE %u", pSub->selId
)
;
131097 sqlite3Select(pParse, pSub, &dest);
131098 }
131099 pItem->pTab->nRowLogEst = pSub->nSelectRow;
131100 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
131101 retAddr = sqlite3VdbeAddOp1(v, OP_Return66, pItem->regReturn);
131102 VdbeComment((v, "end %s", pItem->pTab->zName));
131103 sqlite3VdbeChangeP1(v, topAddr, retAddr);
131104 sqlite3ClearTempRegCache(pParse);
131105 }
131106 if( db->mallocFailed ) goto select_end;
131107 pParse->nHeight -= sqlite3SelectExprHeight(p);
131108 pParse->zAuthContext = zSavedAuthContext;
131109#endif
131110 }
131111
131112 /* Various elements of the SELECT copied into local variables for
131113 ** convenience */
131114 pEList = p->pEList;
131115 pWhere = p->pWhere;
131116 pGroupBy = p->pGroupBy;
131117 pHaving = p->pHaving;
131118 sDistinct.isTnct = (p->selFlags & SF_Distinct0x00001)!=0;
131119
131120#if SELECTTRACE_ENABLED0
131121 if( sqlite3SelectTrace & 0x400 ){
131122 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
131123 sqlite3TreeViewSelect(0, p, 0);
131124 }
131125#endif
131126
131127 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
131128 ** if the select-list is the same as the ORDER BY list, then this query
131129 ** can be rewritten as a GROUP BY. In other words, this:
131130 **
131131 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
131132 **
131133 ** is transformed to:
131134 **
131135 ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz
131136 **
131137 ** The second form is preferred as a single index (or temp-table) may be
131138 ** used for both the ORDER BY and DISTINCT processing. As originally
131139 ** written the query must use a temp-table for at least one of the ORDER
131140 ** BY and DISTINCT, and an index or separate temp-table for the other.
131141 */
131142 if( (p->selFlags & (SF_Distinct0x00001|SF_Aggregate0x00008))==SF_Distinct0x00001
131143 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
131144 ){
131145 p->selFlags &= ~SF_Distinct0x00001;
131146 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
131147 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
131148 ** the sDistinct.isTnct is still set. Hence, isTnct represents the
131149 ** original setting of the SF_Distinct flag, not the current setting */
131150 assert( sDistinct.isTnct )((void) (0));
131151
131152#if SELECTTRACE_ENABLED0
131153 if( sqlite3SelectTrace & 0x400 ){
131154 SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n"));
131155 sqlite3TreeViewSelect(0, p, 0);
131156 }
131157#endif
131158 }
131159
131160 /* If there is an ORDER BY clause, then create an ephemeral index to
131161 ** do the sorting. But this sorting ephemeral index might end up
131162 ** being unused if the data can be extracted in pre-sorted order.
131163 ** If that is the case, then the OP_OpenEphemeral instruction will be
131164 ** changed to an OP_Noop once we figure out that the sorting index is
131165 ** not needed. The sSort.addrSortIndex variable is used to facilitate
131166 ** that change.
131167 */
131168 if( sSort.pOrderBy ){
131169 KeyInfo *pKeyInfo;
131170 pKeyInfo = sqlite3KeyInfoFromExprList(
131171 pParse, sSort.pOrderBy, 0, pEList->nExpr);
131172 sSort.iECursor = pParse->nTab++;
131173 sSort.addrSortIndex =
131174 sqlite3VdbeAddOp4(v, OP_OpenEphemeral113,
131175 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
131176 (char*)pKeyInfo, P4_KEYINFO(-9)
131177 );
131178 }else{
131179 sSort.addrSortIndex = -1;
131180 }
131181
131182 /* If the output is destined for a temporary table, open that table.
131183 */
131184 if( pDest->eDest==SRT_EphemTab12 ){
131185 sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, pDest->iSDParm, pEList->nExpr);
131186 }
131187
131188 /* Set the limiter.
131189 */
131190 iEnd = sqlite3VdbeMakeLabel(pParse);
131191 if( (p->selFlags & SF_FixedLimit0x04000)==0 ){
131192 p->nSelectRow = 320; /* 4 billion rows */
131193 }
131194 computeLimitRegisters(pParse, p, iEnd);
131195 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
131196 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen114);
131197 sSort.sortFlags |= SORTFLAG_UseSorter0x01;
131198 }
131199
131200 /* Open an ephemeral index to use for the distinct set.
131201 */
131202 if( p->selFlags & SF_Distinct0x00001 ){
131203 sDistinct.tabTnct = pParse->nTab++;
131204 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral113,
131205 sDistinct.tabTnct, 0, 0,
131206 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
131207 P4_KEYINFO(-9));
131208 sqlite3VdbeChangeP5(v, BTREE_UNORDERED8);
131209 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED3;
131210 }else{
131211 sDistinct.eTnctType = WHERE_DISTINCT_NOOP0;
131212 }
131213
131214 if( !isAgg && pGroupBy==0 ){
131215 /* No aggregate functions and no GROUP BY clause */
131216 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT0x0100 : 0)
131217 | (p->selFlags & SF_FixedLimit0x04000);
131218#ifndef SQLITE_OMIT_WINDOWFUNC
131219 Window *pWin = p->pWin; /* Master window object (or NULL) */
131220 if( pWin ){
131221 sqlite3WindowCodeInit(pParse, pWin);
131222 }
131223#endif
131224 assert( WHERE_USE_LIMIT==SF_FixedLimit )((void) (0));
131225
131226
131227 /* Begin the database scan. */
131228 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
131229 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy,
131230 p->pEList, wctrlFlags, p->nSelectRow);
131231 if( pWInfo==0 ) goto select_end;
131232 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
131233 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
131234 }
131235 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
131236 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
131237 }
131238 if( sSort.pOrderBy ){
131239 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo);
131240 sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo);
131241 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
131242 sSort.pOrderBy = 0;
131243 }
131244 }
131245
131246 /* If sorting index that was created by a prior OP_OpenEphemeral
131247 ** instruction ended up not being needed, then change the OP_OpenEphemeral
131248 ** into an OP_Noop.
131249 */
131250 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){
131251 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
131252 }
131253
131254 assert( p->pEList==pEList )((void) (0));
131255#ifndef SQLITE_OMIT_WINDOWFUNC
131256 if( pWin ){
131257 int addrGosub = sqlite3VdbeMakeLabel(pParse);
131258 int iCont = sqlite3VdbeMakeLabel(pParse);
131259 int iBreak = sqlite3VdbeMakeLabel(pParse);
131260 int regGosub = ++pParse->nMem;
131261
131262 sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub);
131263
131264 sqlite3VdbeAddOp2(v, OP_Goto11, 0, iBreak);
131265 sqlite3VdbeResolveLabel(v, addrGosub);
131266 VdbeNoopComment((v, "inner-loop subroutine"));
131267 sSort.labelOBLopt = 0;
131268 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
131269 sqlite3VdbeResolveLabel(v, iCont);
131270 sqlite3VdbeAddOp1(v, OP_Return66, regGosub);
131271 VdbeComment((v, "end inner-loop subroutine"));
131272 sqlite3VdbeResolveLabel(v, iBreak);
131273 }else
131274#endif /* SQLITE_OMIT_WINDOWFUNC */
131275 {
131276 /* Use the standard inner loop. */
131277 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
131278 sqlite3WhereContinueLabel(pWInfo),
131279 sqlite3WhereBreakLabel(pWInfo));
131280
131281 /* End the database scan loop.
131282 */
131283 sqlite3WhereEnd(pWInfo);
131284 }
131285 }else{
131286 /* This case when there exist aggregate functions or a GROUP BY clause
131287 ** or both */
131288 NameContext sNC; /* Name context for processing aggregate information */
131289 int iAMem; /* First Mem address for storing current GROUP BY */
131290 int iBMem; /* First Mem address for previous GROUP BY */
131291 int iUseFlag; /* Mem address holding flag indicating that at least
131292 ** one row of the input to the aggregator has been
131293 ** processed */
131294 int iAbortFlag; /* Mem address which causes query abort if positive */
131295 int groupBySort; /* Rows come from source in GROUP BY order */
131296 int addrEnd; /* End of processing for this SELECT */
131297 int sortPTab = 0; /* Pseudotable used to decode sorting results */
131298 int sortOut = 0; /* Output register from the sorter */
131299 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
131300
131301 /* Remove any and all aliases between the result set and the
131302 ** GROUP BY clause.
131303 */
131304 if( pGroupBy ){
131305 int k; /* Loop counter */
131306 struct ExprList_item *pItem; /* For looping over expression in a list */
131307
131308 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
131309 pItem->u.x.iAlias = 0;
131310 }
131311 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
131312 pItem->u.x.iAlias = 0;
131313 }
131314 assert( 66==sqlite3LogEst(100) )((void) (0));
131315 if( p->nSelectRow>66 ) p->nSelectRow = 66;
131316 }else{
131317 assert( 0==sqlite3LogEst(1) )((void) (0));
131318 p->nSelectRow = 0;
131319 }
131320
131321 /* If there is both a GROUP BY and an ORDER BY clause and they are
131322 ** identical, then it may be possible to disable the ORDER BY clause
131323 ** on the grounds that the GROUP BY will cause elements to come out
131324 ** in the correct order. It also may not - the GROUP BY might use a
131325 ** database index that causes rows to be grouped together as required
131326 ** but not actually sorted. Either way, record the fact that the
131327 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
131328 ** variable. */
131329 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
131330 orderByGrp = 1;
131331 }
131332
131333 /* Create a label to jump to when we want to abort the query */
131334 addrEnd = sqlite3VdbeMakeLabel(pParse);
131335
131336 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
131337 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
131338 ** SELECT statement.
131339 */
131340 memset(&sNC, 0, sizeof(sNC));
131341 sNC.pParse = pParse;
131342 sNC.pSrcList = pTabList;
131343 sNC.uNC.pAggInfo = &sAggInfo;
131344 VVA_ONLY( sNC.ncFlags = NC_UAggInfo; )
131345 sAggInfo.mnReg = pParse->nMem+1;
131346 sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
131347 sAggInfo.pGroupBy = pGroupBy;
131348 sqlite3ExprAnalyzeAggList(&sNC, pEList);
131349 sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy);
131350 if( pHaving ){
131351 if( pGroupBy ){
131352 assert( pWhere==p->pWhere )((void) (0));
131353 assert( pHaving==p->pHaving )((void) (0));
131354 assert( pGroupBy==p->pGroupBy )((void) (0));
131355 havingToWhere(pParse, p);
131356 pWhere = p->pWhere;
131357 }
131358 sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
131359 }
131360 sAggInfo.nAccumulator = sAggInfo.nColumn;
131361 if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
131362 minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
131363 }else{
131364 minMaxFlag = WHERE_ORDERBY_NORMAL0x0000;
131365 }
131366 for(i=0; i<sAggInfo.nFunc; i++){
131367 assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) )((void) (0));
131368 sNC.ncFlags |= NC_InAggFunc0x0008;
131369 sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
131370 sNC.ncFlags &= ~NC_InAggFunc0x0008;
131371 }
131372 sAggInfo.mxReg = pParse->nMem;
131373 if( db->mallocFailed ) goto select_end;
131374#if SELECTTRACE_ENABLED0
131375 if( sqlite3SelectTrace & 0x400 ){
131376 int ii;
131377 SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n"));
131378 sqlite3TreeViewSelect(0, p, 0);
131379 for(ii=0; ii<sAggInfo.nColumn; ii++){
131380 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
131381 ii, sAggInfo.aCol[ii].iMem);
131382 sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0);
131383 }
131384 for(ii=0; ii<sAggInfo.nFunc; ii++){
131385 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
131386 ii, sAggInfo.aFunc[ii].iMem);
131387 sqlite3TreeViewExpr(0, sAggInfo.aFunc[ii].pExpr, 0);
131388 }
131389 }
131390#endif
131391
131392
131393 /* Processing for aggregates with GROUP BY is very different and
131394 ** much more complex than aggregates without a GROUP BY.
131395 */
131396 if( pGroupBy ){
131397 KeyInfo *pKeyInfo; /* Keying information for the group by clause */
131398 int addr1; /* A-vs-B comparision jump */
131399 int addrOutputRow; /* Start of subroutine that outputs a result row */
131400 int regOutputRow; /* Return address register for output subroutine */
131401 int addrSetAbort; /* Set the abort flag and return */
131402 int addrTopOfLoop; /* Top of the input loop */
131403 int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
131404 int addrReset; /* Subroutine for resetting the accumulator */
131405 int regReset; /* Return address register for reset subroutine */
131406
131407 /* If there is a GROUP BY clause we might need a sorting index to
131408 ** implement it. Allocate that sorting index now. If it turns out
131409 ** that we do not need it after all, the OP_SorterOpen instruction
131410 ** will be converted into a Noop.
131411 */
131412 sAggInfo.sortingIdx = pParse->nTab++;
131413 pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pGroupBy,0,sAggInfo.nColumn);
131414 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen114,
131415 sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
131416 0, (char*)pKeyInfo, P4_KEYINFO(-9));
131417
131418 /* Initialize memory locations used by GROUP BY aggregate processing
131419 */
131420 iUseFlag = ++pParse->nMem;
131421 iAbortFlag = ++pParse->nMem;
131422 regOutputRow = ++pParse->nMem;
131423 addrOutputRow = sqlite3VdbeMakeLabel(pParse);
131424 regReset = ++pParse->nMem;
131425 addrReset = sqlite3VdbeMakeLabel(pParse);
131426 iAMem = pParse->nMem + 1;
131427 pParse->nMem += pGroupBy->nExpr;
131428 iBMem = pParse->nMem + 1;
131429 pParse->nMem += pGroupBy->nExpr;
131430 sqlite3VdbeAddOp2(v, OP_Integer70, 0, iAbortFlag);
131431 VdbeComment((v, "clear abort flag"));
131432 sqlite3VdbeAddOp3(v, OP_Null73, 0, iAMem, iAMem+pGroupBy->nExpr-1);
131433
131434 /* Begin a loop that will extract all source rows in GROUP BY order.
131435 ** This might involve two separate loops with an OP_Sort in between, or
131436 ** it might be a single loop that uses an index to extract information
131437 ** in the right order to begin with.
131438 */
131439 sqlite3VdbeAddOp2(v, OP_Gosub12, regReset, addrReset);
131440 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
131441 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
131442 WHERE_GROUPBY0x0040 | (orderByGrp ? WHERE_SORTBYGROUP0x0200 : 0), 0
131443 );
131444 if( pWInfo==0 ) goto select_end;
131445 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
131446 /* The optimizer is able to deliver rows in group by order so
131447 ** we do not have to sort. The OP_OpenEphemeral table will be
131448 ** cancelled later because we still need to use the pKeyInfo
131449 */
131450 groupBySort = 0;
131451 }else{
131452 /* Rows are coming out in undetermined order. We have to push
131453 ** each row into a sorting index, terminate the first loop,
131454 ** then loop over the sorting index in order to get the output
131455 ** in sorted order
131456 */
131457 int regBase;
131458 int regRecord;
131459 int nCol;
131460 int nGroupBy;
131461
131462 explainTempTable(pParse,
131463 (sDistinct.isTnct && (p->selFlags&SF_Distinct0x00001)==0) ?
131464 "DISTINCT" : "GROUP BY");
131465
131466 groupBySort = 1;
131467 nGroupBy = pGroupBy->nExpr;
131468 nCol = nGroupBy;
131469 j = nGroupBy;
131470 for(i=0; i<sAggInfo.nColumn; i++){
131471 if( sAggInfo.aCol[i].iSorterColumn>=j ){
131472 nCol++;
131473 j++;
131474 }
131475 }
131476 regBase = sqlite3GetTempRange(pParse, nCol);
131477 sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0);
131478 j = nGroupBy;
131479 for(i=0; i<sAggInfo.nColumn; i++){
131480 struct AggInfo_col *pCol = &sAggInfo.aCol[i];
131481 if( pCol->iSorterColumn>=j ){
131482 int r1 = j + regBase;
131483 sqlite3ExprCodeGetColumnOfTable(v,
131484 pCol->pTab, pCol->iTable, pCol->iColumn, r1);
131485 j++;
131486 }
131487 }
131488 regRecord = sqlite3GetTempReg(pParse);
131489 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regBase, nCol, regRecord);
131490 sqlite3VdbeAddOp2(v, OP_SorterInsert131, sAggInfo.sortingIdx, regRecord);
131491 sqlite3ReleaseTempReg(pParse, regRecord);
131492 sqlite3ReleaseTempRange(pParse, regBase, nCol);
131493 sqlite3WhereEnd(pWInfo);
131494 sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
131495 sortOut = sqlite3GetTempReg(pParse);
131496 sqlite3VdbeAddOp3(v, OP_OpenPseudo116, sortPTab, sortOut, nCol);
131497 sqlite3VdbeAddOp2(v, OP_SorterSort34, sAggInfo.sortingIdx, addrEnd);
131498 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
131499 sAggInfo.useSortingIdx = 1;
131500 }
131501
131502 /* If the index or temporary table used by the GROUP BY sort
131503 ** will naturally deliver rows in the order required by the ORDER BY
131504 ** clause, cancel the ephemeral table open coded earlier.
131505 **
131506 ** This is an optimization - the correct answer should result regardless.
131507 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
131508 ** disable this optimization for testing purposes. */
131509 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)(((db)->dbOptFlags&(0x0004))==0)
131510 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
131511 ){
131512 sSort.pOrderBy = 0;
131513 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
131514 }
131515
131516 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
131517 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
131518 ** Then compare the current GROUP BY terms against the GROUP BY terms
131519 ** from the previous row currently stored in a0, a1, a2...
131520 */
131521 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
131522 if( groupBySort ){
131523 sqlite3VdbeAddOp3(v, OP_SorterData126, sAggInfo.sortingIdx,
131524 sortOut, sortPTab);
131525 }
131526 for(j=0; j<pGroupBy->nExpr; j++){
131527 if( groupBySort ){
131528 sqlite3VdbeAddOp3(v, OP_Column90, sortPTab, j, iBMem+j);
131529 }else{
131530 sAggInfo.directMode = 1;
131531 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
131532 }
131533 }
131534 sqlite3VdbeAddOp4(v, OP_Compare87, iAMem, iBMem, pGroupBy->nExpr,
131535 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO(-9));
131536 addr1 = sqlite3VdbeCurrentAddr(v);
131537 sqlite3VdbeAddOp3(v, OP_Jump16, addr1+1, 0, addr1+1); VdbeCoverage(v);
131538
131539 /* Generate code that runs whenever the GROUP BY changes.
131540 ** Changes in the GROUP BY are detected by the previous code
131541 ** block. If there were no changes, this block is skipped.
131542 **
131543 ** This code copies current group by terms in b0,b1,b2,...
131544 ** over to a0,a1,a2. It then calls the output subroutine
131545 ** and resets the aggregate accumulator registers in preparation
131546 ** for the next GROUP BY batch.
131547 */
131548 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
131549 sqlite3VdbeAddOp2(v, OP_Gosub12, regOutputRow, addrOutputRow);
131550 VdbeComment((v, "output one row"));
131551 sqlite3VdbeAddOp2(v, OP_IfPos47, iAbortFlag, addrEnd); VdbeCoverage(v);
131552 VdbeComment((v, "check abort flag"));
131553 sqlite3VdbeAddOp2(v, OP_Gosub12, regReset, addrReset);
131554 VdbeComment((v, "reset accumulator"));
131555
131556 /* Update the aggregate accumulators based on the content of
131557 ** the current row
131558 */
131559 sqlite3VdbeJumpHere(v, addr1);
131560 updateAccumulator(pParse, iUseFlag, &sAggInfo);
131561 sqlite3VdbeAddOp2(v, OP_Integer70, 1, iUseFlag);
131562 VdbeComment((v, "indicate data in accumulator"));
131563
131564 /* End of the loop
131565 */
131566 if( groupBySort ){
131567 sqlite3VdbeAddOp2(v, OP_SorterNext3, sAggInfo.sortingIdx, addrTopOfLoop);
131568 VdbeCoverage(v);
131569 }else{
131570 sqlite3WhereEnd(pWInfo);
131571 sqlite3VdbeChangeToNoop(v, addrSortingIdx);
131572 }
131573
131574 /* Output the final row of result
131575 */
131576 sqlite3VdbeAddOp2(v, OP_Gosub12, regOutputRow, addrOutputRow);
131577 VdbeComment((v, "output final row"));
131578
131579 /* Jump over the subroutines
131580 */
131581 sqlite3VdbeGoto(v, addrEnd);
131582
131583 /* Generate a subroutine that outputs a single row of the result
131584 ** set. This subroutine first looks at the iUseFlag. If iUseFlag
131585 ** is less than or equal to zero, the subroutine is a no-op. If
131586 ** the processing calls for the query to abort, this subroutine
131587 ** increments the iAbortFlag memory location before returning in
131588 ** order to signal the caller to abort.
131589 */
131590 addrSetAbort = sqlite3VdbeCurrentAddr(v);
131591 sqlite3VdbeAddOp2(v, OP_Integer70, 1, iAbortFlag);
131592 VdbeComment((v, "set abort flag"));
131593 sqlite3VdbeAddOp1(v, OP_Return66, regOutputRow);
131594 sqlite3VdbeResolveLabel(v, addrOutputRow);
131595 addrOutputRow = sqlite3VdbeCurrentAddr(v);
131596 sqlite3VdbeAddOp2(v, OP_IfPos47, iUseFlag, addrOutputRow+2);
131597 VdbeCoverage(v);
131598 VdbeComment((v, "Groupby result generator entry point"));
131599 sqlite3VdbeAddOp1(v, OP_Return66, regOutputRow);
131600 finalizeAggFunctions(pParse, &sAggInfo);
131601 sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL0x10);
131602 selectInnerLoop(pParse, p, -1, &sSort,
131603 &sDistinct, pDest,
131604 addrOutputRow+1, addrSetAbort);
131605 sqlite3VdbeAddOp1(v, OP_Return66, regOutputRow);
131606 VdbeComment((v, "end groupby result generator"));
131607
131608 /* Generate a subroutine that will reset the group-by accumulator
131609 */
131610 sqlite3VdbeResolveLabel(v, addrReset);
131611 resetAccumulator(pParse, &sAggInfo);
131612 sqlite3VdbeAddOp2(v, OP_Integer70, 0, iUseFlag);
131613 VdbeComment((v, "indicate accumulator empty"));
131614 sqlite3VdbeAddOp1(v, OP_Return66, regReset);
131615
131616 } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */
131617 else {
131618#ifndef SQLITE_OMIT_BTREECOUNT
131619 Table *pTab;
131620 if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
131621 /* If isSimpleCount() returns a pointer to a Table structure, then
131622 ** the SQL statement is of the form:
131623 **
131624 ** SELECT count(*) FROM <tbl>
131625 **
131626 ** where the Table structure returned represents table <tbl>.
131627 **
131628 ** This statement is so common that it is optimized specially. The
131629 ** OP_Count instruction is executed either on the intkey table that
131630 ** contains the data for table <tbl> or on one of its indexes. It
131631 ** is better to execute the op on an index, as indexes are almost
131632 ** always spread across less pages than their corresponding tables.
131633 */
131634 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
131635 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
131636 Index *pIdx; /* Iterator variable */
131637 KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */
131638 Index *pBest = 0; /* Best index found so far */
131639 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
131640
131641 sqlite3CodeVerifySchema(pParse, iDb);
131642 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
131643
131644 /* Search for the index that has the lowest scan cost.
131645 **
131646 ** (2011-04-15) Do not do a full scan of an unordered index.
131647 **
131648 ** (2013-10-03) Do not count the entries in a partial index.
131649 **
131650 ** In practice the KeyInfo structure will not be used. It is only
131651 ** passed to keep OP_OpenRead happy.
131652 */
131653 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ) pBest = sqlite3PrimaryKeyIndex(pTab);
131654 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
131655 if( pIdx->bUnordered==0
131656 && pIdx->szIdxRow<pTab->szTabRow
131657 && pIdx->pPartIdxWhere==0
131658 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
131659 ){
131660 pBest = pIdx;
131661 }
131662 }
131663 if( pBest ){
131664 iRoot = pBest->tnum;
131665 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
131666 }
131667
131668 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
131669 sqlite3VdbeAddOp4Int(v, OP_OpenRead108, iCsr, iRoot, iDb, 1);
131670 if( pKeyInfo ){
131671 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO(-9));
131672 }
131673 sqlite3VdbeAddOp2(v, OP_Count93, iCsr, sAggInfo.aFunc[0].iMem);
131674 sqlite3VdbeAddOp1(v, OP_Close117, iCsr);
131675 explainSimpleCount(pParse, pTab, pBest);
131676 }else
131677#endif /* SQLITE_OMIT_BTREECOUNT */
131678 {
131679 int regAcc = 0; /* "populate accumulators" flag */
131680
131681 /* If there are accumulator registers but no min() or max() functions,
131682 ** allocate register regAcc. Register regAcc will contain 0 the first
131683 ** time the inner loop runs, and 1 thereafter. The code generated
131684 ** by updateAccumulator() only updates the accumulator registers if
131685 ** regAcc contains 0. */
131686 if( sAggInfo.nAccumulator ){
131687 for(i=0; i<sAggInfo.nFunc; i++){
131688 if( sAggInfo.aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL0x0020 ) break;
131689 }
131690 if( i==sAggInfo.nFunc ){
131691 regAcc = ++pParse->nMem;
131692 sqlite3VdbeAddOp2(v, OP_Integer70, 0, regAcc);
131693 }
131694 }
131695
131696 /* This case runs if the aggregate has no GROUP BY clause. The
131697 ** processing is much simpler since there is only a single row
131698 ** of output.
131699 */
131700 assert( p->pGroupBy==0 )((void) (0));
131701 resetAccumulator(pParse, &sAggInfo);
131702
131703 /* If this query is a candidate for the min/max optimization, then
131704 ** minMaxFlag will have been previously set to either
131705 ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
131706 ** be an appropriate ORDER BY expression for the optimization.
131707 */
131708 assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 )((void) (0));
131709 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 )((void) (0));
131710
131711 SELECTTRACE(1,pParse,p,("WhereBegin\n"));
131712 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
131713 0, minMaxFlag, 0);
131714 if( pWInfo==0 ){
131715 goto select_end;
131716 }
131717 updateAccumulator(pParse, regAcc, &sAggInfo);
131718 if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer70, 1, regAcc);
131719 if( sqlite3WhereIsOrdered(pWInfo)>0 ){
131720 sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
131721 VdbeComment((v, "%s() by index",
131722 (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
131723 }
131724 sqlite3WhereEnd(pWInfo);
131725 finalizeAggFunctions(pParse, &sAggInfo);
131726 }
131727
131728 sSort.pOrderBy = 0;
131729 sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL0x10);
131730 selectInnerLoop(pParse, p, -1, 0, 0,
131731 pDest, addrEnd, addrEnd);
131732 }
131733 sqlite3VdbeResolveLabel(v, addrEnd);
131734
131735 } /* endif aggregate query */
131736
131737 if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED3 ){
131738 explainTempTable(pParse, "DISTINCT");
131739 }
131740
131741 /* If there is an ORDER BY clause, then we need to sort the results
131742 ** and send them to the callback one by one.
131743 */
131744 if( sSort.pOrderBy ){
131745 explainTempTable(pParse,
131746 sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
131747 assert( p->pEList==pEList )((void) (0));
131748 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
131749 }
131750
131751 /* Jump here to skip this query
131752 */
131753 sqlite3VdbeResolveLabel(v, iEnd);
131754
131755 /* The SELECT has been coded. If there is an error in the Parse structure,
131756 ** set the return code to 1. Otherwise 0. */
131757 rc = (pParse->nErr>0);
131758
131759 /* Control jumps to here if an error is encountered above, or upon
131760 ** successful coding of the SELECT.
131761 */
131762select_end:
131763 sqlite3ExprListDelete(db, pMinMaxOrderBy);
131764 sqlite3DbFree(db, sAggInfo.aCol);
131765 sqlite3DbFree(db, sAggInfo.aFunc);
131766#if SELECTTRACE_ENABLED0
131767 SELECTTRACE(0x1,pParse,p,("end processing\n"));
131768 if( (sqlite3SelectTrace & 0x2000)!=0 && ExplainQueryPlanParent(pParse)sqlite3VdbeExplainParent(pParse)==0 ){
131769 sqlite3TreeViewSelect(0, p, 0);
131770 }
131771#endif
131772 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
131773 return rc;
131774}
131775
131776/************** End of select.c **********************************************/
131777/************** Begin file table.c *******************************************/
131778/*
131779** 2001 September 15
131780**
131781** The author disclaims copyright to this source code. In place of
131782** a legal notice, here is a blessing:
131783**
131784** May you do good and not evil.
131785** May you find forgiveness for yourself and forgive others.
131786** May you share freely, never taking more than you give.
131787**
131788*************************************************************************
131789** This file contains the sqlite3_get_table() and sqlite3_free_table()
131790** interface routines. These are just wrappers around the main
131791** interface routine of sqlite3_exec().
131792**
131793** These routines are in a separate files so that they will not be linked
131794** if they are not used.
131795*/
131796/* #include "sqliteInt.h" */
131797
131798#ifndef SQLITE_OMIT_GET_TABLE
131799
131800/*
131801** This structure is used to pass data from sqlite3_get_table() through
131802** to the callback function is uses to build the result.
131803*/
131804typedef struct TabResult {
131805 char **azResult; /* Accumulated output */
131806 char *zErrMsg; /* Error message text, if an error occurs */
131807 u32 nAlloc; /* Slots allocated for azResult[] */
131808 u32 nRow; /* Number of rows in the result */
131809 u32 nColumn; /* Number of columns in the result */
131810 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
131811 int rc; /* Return code from sqlite3_exec() */
131812} TabResult;
131813
131814/*
131815** This routine is called once for each row in the result table. Its job
131816** is to fill in the TabResult structure appropriately, allocating new
131817** memory as necessary.
131818*/
131819static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
131820 TabResult *p = (TabResult*)pArg; /* Result accumulator */
131821 int need; /* Slots needed in p->azResult[] */
131822 int i; /* Loop counter */
131823 char *z; /* A single column of result */
131824
131825 /* Make sure there is enough space in p->azResult to hold everything
131826 ** we need to remember from this invocation of the callback.
131827 */
131828 if( p->nRow==0 && argv!=0 ){
131829 need = nCol*2;
131830 }else{
131831 need = nCol;
131832 }
131833 if( p->nData + need > p->nAlloc ){
131834 char **azNew;
131835 p->nAlloc = p->nAlloc*2 + need;
131836 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
131837 if( azNew==0 ) goto malloc_failed;
131838 p->azResult = azNew;
131839 }
131840
131841 /* If this is the first row, then generate an extra row containing
131842 ** the names of all columns.
131843 */
131844 if( p->nRow==0 ){
131845 p->nColumn = nCol;
131846 for(i=0; i<nCol; i++){
131847 z = sqlite3_mprintf("%s", colv[i]);
131848 if( z==0 ) goto malloc_failed;
131849 p->azResult[p->nData++] = z;
131850 }
131851 }else if( (int)p->nColumn!=nCol ){
131852 sqlite3_free(p->zErrMsg);
131853 p->zErrMsg = sqlite3_mprintf(
131854 "sqlite3_get_table() called with two or more incompatible queries"
131855 );
131856 p->rc = SQLITE_ERROR1;
131857 return 1;
131858 }
131859
131860 /* Copy over the row data
131861 */
131862 if( argv!=0 ){
131863 for(i=0; i<nCol; i++){
131864 if( argv[i]==0 ){
131865 z = 0;
131866 }else{
131867 int n = sqlite3Strlen30(argv[i])+1;
131868 z = sqlite3_malloc64( n );
131869 if( z==0 ) goto malloc_failed;
131870 memcpy(z, argv[i], n);
131871 }
131872 p->azResult[p->nData++] = z;
131873 }
131874 p->nRow++;
131875 }
131876 return 0;
131877
131878malloc_failed:
131879 p->rc = SQLITE_NOMEM_BKPT7;
131880 return 1;
131881}
131882
131883/*
131884** Query the database. But instead of invoking a callback for each row,
131885** malloc() for space to hold the result and return the entire results
131886** at the conclusion of the call.
131887**
131888** The result that is written to ***pazResult is held in memory obtained
131889** from malloc(). But the caller cannot free this memory directly.
131890** Instead, the entire table should be passed to sqlite3_free_table() when
131891** the calling procedure is finished using it.
131892*/
131893SQLITE_API int sqlite3_get_table(
131894 sqlite3 *db, /* The database on which the SQL executes */
131895 const char *zSql, /* The SQL to be executed */
131896 char ***pazResult, /* Write the result table here */
131897 int *pnRow, /* Write the number of rows in the result here */
131898 int *pnColumn, /* Write the number of columns of result here */
131899 char **pzErrMsg /* Write error messages here */
131900){
131901 int rc;
131902 TabResult res;
131903
131904#ifdef SQLITE_ENABLE_API_ARMOR
131905 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(131905);
131906#endif
131907 *pazResult = 0;
131908 if( pnColumn ) *pnColumn = 0;
131909 if( pnRow ) *pnRow = 0;
131910 if( pzErrMsg ) *pzErrMsg = 0;
131911 res.zErrMsg = 0;
131912 res.nRow = 0;
131913 res.nColumn = 0;
131914 res.nData = 1;
131915 res.nAlloc = 20;
131916 res.rc = SQLITE_OK0;
131917 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
131918 if( res.azResult==0 ){
131919 db->errCode = SQLITE_NOMEM7;
131920 return SQLITE_NOMEM_BKPT7;
131921 }
131922 res.azResult[0] = 0;
131923 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
131924 assert( sizeof(res.azResult[0])>= sizeof(res.nData) )((void) (0));
131925 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData)((void*)(long int)(res.nData));
131926 if( (rc&0xff)==SQLITE_ABORT4 ){
131927 sqlite3_free_table(&res.azResult[1]);
131928 if( res.zErrMsg ){
131929 if( pzErrMsg ){
131930 sqlite3_free(*pzErrMsg);
131931 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
131932 }
131933 sqlite3_free(res.zErrMsg);
131934 }
131935 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
131936 return res.rc;
131937 }
131938 sqlite3_free(res.zErrMsg);
131939 if( rc!=SQLITE_OK0 ){
131940 sqlite3_free_table(&res.azResult[1]);
131941 return rc;
131942 }
131943 if( res.nAlloc>res.nData ){
131944 char **azNew;
131945 azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData );
131946 if( azNew==0 ){
131947 sqlite3_free_table(&res.azResult[1]);
131948 db->errCode = SQLITE_NOMEM7;
131949 return SQLITE_NOMEM_BKPT7;
131950 }
131951 res.azResult = azNew;
131952 }
131953 *pazResult = &res.azResult[1];
131954 if( pnColumn ) *pnColumn = res.nColumn;
131955 if( pnRow ) *pnRow = res.nRow;
131956 return rc;
131957}
131958
131959/*
131960** This routine frees the space the sqlite3_get_table() malloced.
131961*/
131962SQLITE_API void sqlite3_free_table(
131963 char **azResult /* Result returned from sqlite3_get_table() */
131964){
131965 if( azResult ){
131966 int i, n;
131967 azResult--;
131968 assert( azResult!=0 )((void) (0));
131969 n = SQLITE_PTR_TO_INT(azResult[0])((int)(long int)(azResult[0]));
131970 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
131971 sqlite3_free(azResult);
131972 }
131973}
131974
131975#endif /* SQLITE_OMIT_GET_TABLE */
131976
131977/************** End of table.c ***********************************************/
131978/************** Begin file trigger.c *****************************************/
131979/*
131980**
131981** The author disclaims copyright to this source code. In place of
131982** a legal notice, here is a blessing:
131983**
131984** May you do good and not evil.
131985** May you find forgiveness for yourself and forgive others.
131986** May you share freely, never taking more than you give.
131987**
131988*************************************************************************
131989** This file contains the implementation for TRIGGERs
131990*/
131991/* #include "sqliteInt.h" */
131992
131993#ifndef SQLITE_OMIT_TRIGGER
131994/*
131995** Delete a linked list of TriggerStep structures.
131996*/
131997SQLITE_PRIVATEstatic void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
131998 while( pTriggerStep ){
131999 TriggerStep * pTmp = pTriggerStep;
132000 pTriggerStep = pTriggerStep->pNext;
132001
132002 sqlite3ExprDelete(db, pTmp->pWhere);
132003 sqlite3ExprListDelete(db, pTmp->pExprList);
132004 sqlite3SelectDelete(db, pTmp->pSelect);
132005 sqlite3IdListDelete(db, pTmp->pIdList);
132006 sqlite3UpsertDelete(db, pTmp->pUpsert);
132007 sqlite3DbFree(db, pTmp->zSpan);
132008
132009 sqlite3DbFree(db, pTmp);
132010 }
132011}
132012
132013/*
132014** Given table pTab, return a list of all the triggers attached to
132015** the table. The list is connected by Trigger.pNext pointers.
132016**
132017** All of the triggers on pTab that are in the same database as pTab
132018** are already attached to pTab->pTrigger. But there might be additional
132019** triggers on pTab in the TEMP schema. This routine prepends all
132020** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
132021** and returns the combined list.
132022**
132023** To state it another way: This routine returns a list of all triggers
132024** that fire off of pTab. The list will include any TEMP triggers on
132025** pTab as well as the triggers lised in pTab->pTrigger.
132026*/
132027SQLITE_PRIVATEstatic Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
132028 Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
132029 Trigger *pList = 0; /* List of triggers to return */
132030
132031 if( pParse->disableTriggers ){
132032 return 0;
132033 }
132034
132035 if( pTmpSchema!=pTab->pSchema ){
132036 HashElem *p;
132037 assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) )((void) (0));
132038 for(p=sqliteHashFirst(&pTmpSchema->trigHash)((&pTmpSchema->trigHash)->first); p; p=sqliteHashNext(p)((p)->next)){
132039 Trigger *pTrig = (Trigger *)sqliteHashData(p)((p)->data);
132040 if( pTrig->pTabSchema==pTab->pSchema
132041 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
132042 ){
132043 pTrig->pNext = (pList ? pList : pTab->pTrigger);
132044 pList = pTrig;
132045 }
132046 }
132047 }
132048
132049 return (pList ? pList : pTab->pTrigger);
132050}
132051
132052/*
132053** This is called by the parser when it sees a CREATE TRIGGER statement
132054** up to the point of the BEGIN before the trigger actions. A Trigger
132055** structure is generated based on the information available and stored
132056** in pParse->pNewTrigger. After the trigger actions have been parsed, the
132057** sqlite3FinishTrigger() function is called to complete the trigger
132058** construction process.
132059*/
132060SQLITE_PRIVATEstatic void sqlite3BeginTrigger(
132061 Parse *pParse, /* The parse context of the CREATE TRIGGER statement */
132062 Token *pName1, /* The name of the trigger */
132063 Token *pName2, /* The name of the trigger */
132064 int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
132065 int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
132066 IdList *pColumns, /* column list if this is an UPDATE OF trigger */
132067 SrcList *pTableName,/* The name of the table/view the trigger applies to */
132068 Expr *pWhen, /* WHEN clause */
132069 int isTemp, /* True if the TEMPORARY keyword is present */
132070 int noErr /* Suppress errors if the trigger already exists */
132071){
132072 Trigger *pTrigger = 0; /* The new trigger */
132073 Table *pTab; /* Table that the trigger fires off of */
132074 char *zName = 0; /* Name of the trigger */
132075 sqlite3 *db = pParse->db; /* The database connection */
132076 int iDb; /* The database to store the trigger in */
132077 Token *pName; /* The unqualified db name */
132078 DbFixer sFix; /* State vector for the DB fixer */
132079
132080 assert( pName1!=0 )((void) (0)); /* pName1->z might be NULL, but not pName1 itself */
132081 assert( pName2!=0 )((void) (0));
132082 assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE )((void) (0));
132083 assert( op>0 && op<0xff )((void) (0));
132084 if( isTemp ){
132085 /* If TEMP was specified, then the trigger name may not be qualified. */
132086 if( pName2->n>0 ){
132087 sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
132088 goto trigger_cleanup;
132089 }
132090 iDb = 1;
132091 pName = pName1;
132092 }else{
132093 /* Figure out the db that the trigger will be created in */
132094 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
132095 if( iDb<0 ){
132096 goto trigger_cleanup;
132097 }
132098 }
132099 if( !pTableName || db->mallocFailed ){
132100 goto trigger_cleanup;
132101 }
132102
132103 /* A long-standing parser bug is that this syntax was allowed:
132104 **
132105 ** CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
132106 ** ^^^^^^^^
132107 **
132108 ** To maintain backwards compatibility, ignore the database
132109 ** name on pTableName if we are reparsing out of SQLITE_MASTER.
132110 */
132111 if( db->init.busy && iDb!=1 ){
132112 sqlite3DbFree(db, pTableName->a[0].zDatabase);
132113 pTableName->a[0].zDatabase = 0;
132114 }
132115
132116 /* If the trigger name was unqualified, and the table is a temp table,
132117 ** then set iDb to 1 to create the trigger in the temporary database.
132118 ** If sqlite3SrcListLookup() returns 0, indicating the table does not
132119 ** exist, the error is caught by the block below.
132120 */
132121 pTab = sqlite3SrcListLookup(pParse, pTableName);
132122 if( db->init.busy==0 && pName2->n==0 && pTab
132123 && pTab->pSchema==db->aDb[1].pSchema ){
132124 iDb = 1;
132125 }
132126
132127 /* Ensure the table name matches database name and that the table exists */
132128 if( db->mallocFailed ) goto trigger_cleanup;
132129 assert( pTableName->nSrc==1 )((void) (0));
132130 sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
132131 if( sqlite3FixSrcList(&sFix, pTableName) ){
132132 goto trigger_cleanup;
132133 }
132134 pTab = sqlite3SrcListLookup(pParse, pTableName);
132135 if( !pTab ){
132136 /* The table does not exist. */
132137 if( db->init.iDb==1 ){
132138 /* Ticket #3810.
132139 ** Normally, whenever a table is dropped, all associated triggers are
132140 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
132141 ** and the table is dropped by a different database connection, the
132142 ** trigger is not visible to the database connection that does the
132143 ** drop so the trigger cannot be dropped. This results in an
132144 ** "orphaned trigger" - a trigger whose associated table is missing.
132145 */
132146 db->init.orphanTrigger = 1;
132147 }
132148 goto trigger_cleanup;
132149 }
132150 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
132151 sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
132152 goto trigger_cleanup;
132153 }
132154
132155 /* Check that the trigger name is not reserved and that no trigger of the
132156 ** specified name exists */
132157 zName = sqlite3NameFromToken(db, pName);
132158 if( !zName || SQLITE_OK0!=sqlite3CheckObjectName(pParse, zName) ){
132159 goto trigger_cleanup;
132160 }
132161 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
132162 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
132163 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
132164 if( !noErr ){
132165 sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
132166 }else{
132167 assert( !db->init.busy )((void) (0));
132168 sqlite3CodeVerifySchema(pParse, iDb);
132169 }
132170 goto trigger_cleanup;
132171 }
132172 }
132173
132174 /* Do not create a trigger on a system table */
132175 if( sqlite3StrNICmpsqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
132176 sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
132177 goto trigger_cleanup;
132178 }
132179
132180 /* INSTEAD of triggers are only for views and views only support INSTEAD
132181 ** of triggers.
132182 */
132183 if( pTab->pSelect && tr_tm!=TK_INSTEAD65 ){
132184 sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
132185 (tr_tm == TK_BEFORE33)?"BEFORE":"AFTER", pTableName, 0);
132186 goto trigger_cleanup;
132187 }
132188 if( !pTab->pSelect && tr_tm==TK_INSTEAD65 ){
132189 sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
132190 " trigger on table: %S", pTableName, 0);
132191 goto trigger_cleanup;
132192 }
132193
132194#ifndef SQLITE_OMIT_AUTHORIZATION
132195 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
132196 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
132197 int code = SQLITE_CREATE_TRIGGER7;
132198 const char *zDb = db->aDb[iTabDb].zDbSName;
132199 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
132200 if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER5;
132201 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
132202 goto trigger_cleanup;
132203 }
132204 if( sqlite3AuthCheck(pParse, SQLITE_INSERT18, SCHEMA_TABLE(iTabDb)((!0)&&(iTabDb==1)?"sqlite_temp_master":"sqlite_master"
)
,0,zDb)){
132205 goto trigger_cleanup;
132206 }
132207 }
132208#endif
132209
132210 /* INSTEAD OF triggers can only appear on views and BEFORE triggers
132211 ** cannot appear on views. So we might as well translate every
132212 ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code
132213 ** elsewhere.
132214 */
132215 if (tr_tm == TK_INSTEAD65){
132216 tr_tm = TK_BEFORE33;
132217 }
132218
132219 /* Build the Trigger object */
132220 pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
132221 if( pTrigger==0 ) goto trigger_cleanup;
132222 pTrigger->zName = zName;
132223 zName = 0;
132224 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
132225 pTrigger->pSchema = db->aDb[iDb].pSchema;
132226 pTrigger->pTabSchema = pTab->pSchema;
132227 pTrigger->op = (u8)op;
132228 pTrigger->tr_tm = tr_tm==TK_BEFORE33 ? TRIGGER_BEFORE1 : TRIGGER_AFTER2;
132229 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
132230 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
132231 pTrigger->pWhen = pWhen;
132232 pWhen = 0;
132233 }else{
132234 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE0x0001);
132235 }
132236 pTrigger->pColumns = pColumns;
132237 pColumns = 0;
132238 assert( pParse->pNewTrigger==0 )((void) (0));
132239 pParse->pNewTrigger = pTrigger;
132240
132241trigger_cleanup:
132242 sqlite3DbFree(db, zName);
132243 sqlite3SrcListDelete(db, pTableName);
132244 sqlite3IdListDelete(db, pColumns);
132245 sqlite3ExprDelete(db, pWhen);
132246 if( !pParse->pNewTrigger ){
132247 sqlite3DeleteTrigger(db, pTrigger);
132248 }else{
132249 assert( pParse->pNewTrigger==pTrigger )((void) (0));
132250 }
132251}
132252
132253/*
132254** This routine is called after all of the trigger actions have been parsed
132255** in order to complete the process of building the trigger.
132256*/
132257SQLITE_PRIVATEstatic void sqlite3FinishTrigger(
132258 Parse *pParse, /* Parser context */
132259 TriggerStep *pStepList, /* The triggered program */
132260 Token *pAll /* Token that describes the complete CREATE TRIGGER */
132261){
132262 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
132263 char *zName; /* Name of trigger */
132264 sqlite3 *db = pParse->db; /* The database */
132265 DbFixer sFix; /* Fixer object */
132266 int iDb; /* Database containing the trigger */
132267 Token nameToken; /* Trigger name for error reporting */
132268
132269 pParse->pNewTrigger = 0;
132270 if( NEVER(pParse->nErr)(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
132271 zName = pTrig->zName;
132272 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
132273 pTrig->step_list = pStepList;
132274 while( pStepList ){
132275 pStepList->pTrig = pTrig;
132276 pStepList = pStepList->pNext;
132277 }
132278 sqlite3TokenInit(&nameToken, pTrig->zName);
132279 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
132280 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
132281 || sqlite3FixExpr(&sFix, pTrig->pWhen)
132282 ){
132283 goto triggerfinish_cleanup;
132284 }
132285
132286#ifndef SQLITE_OMIT_ALTERTABLE
132287 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
132288 assert( !db->init.busy )((void) (0));
132289 pParse->pNewTrigger = pTrig;
132290 pTrig = 0;
132291 }else
132292#endif
132293
132294 /* if we are not initializing,
132295 ** build the sqlite_master entry
132296 */
132297 if( !db->init.busy ){
132298 Vdbe *v;
132299 char *z;
132300
132301 /* Make an entry in the sqlite_master table */
132302 v = sqlite3GetVdbe(pParse);
132303 if( v==0 ) goto triggerfinish_cleanup;
132304 sqlite3BeginWriteOperation(pParse, 0, iDb);
132305 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
132306 testcase( z==0 );
132307 sqlite3NestedParse(pParse,
132308 "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
132309 db->aDb[iDb].zDbSName, MASTER_NAME"sqlite_master", zName,
132310 pTrig->table, z);
132311 sqlite3DbFree(db, z);
132312 sqlite3ChangeCookie(pParse, iDb);
132313 sqlite3VdbeAddParseSchemaOp(v, iDb,
132314 sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
132315 }
132316
132317 if( db->init.busy ){
132318 Trigger *pLink = pTrig;
132319 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
132320 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
132321 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
132322 if( pTrig ){
132323 sqlite3OomFault(db);
132324 }else if( pLink->pSchema==pLink->pTabSchema ){
132325 Table *pTab;
132326 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
132327 assert( pTab!=0 )((void) (0));
132328 pLink->pNext = pTab->pTrigger;
132329 pTab->pTrigger = pLink;
132330 }
132331 }
132332
132333triggerfinish_cleanup:
132334 sqlite3DeleteTrigger(db, pTrig);
132335 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger )((void) (0));
132336 sqlite3DeleteTriggerStep(db, pStepList);
132337}
132338
132339/*
132340** Duplicate a range of text from an SQL statement, then convert all
132341** whitespace characters into ordinary space characters.
132342*/
132343static char *triggerSpanDup(sqlite3 *db, const char *zStart, const char *zEnd){
132344 char *z = sqlite3DbSpanDup(db, zStart, zEnd);
132345 int i;
132346 if( z ) for(i=0; z[i]; i++) if( sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01) ) z[i] = ' ';
132347 return z;
132348}
132349
132350/*
132351** Turn a SELECT statement (that the pSelect parameter points to) into
132352** a trigger step. Return a pointer to a TriggerStep structure.
132353**
132354** The parser calls this routine when it finds a SELECT statement in
132355** body of a TRIGGER.
132356*/
132357SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerSelectStep(
132358 sqlite3 *db, /* Database connection */
132359 Select *pSelect, /* The SELECT statement */
132360 const char *zStart, /* Start of SQL text */
132361 const char *zEnd /* End of SQL text */
132362){
132363 TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
132364 if( pTriggerStep==0 ) {
132365 sqlite3SelectDelete(db, pSelect);
132366 return 0;
132367 }
132368 pTriggerStep->op = TK_SELECT131;
132369 pTriggerStep->pSelect = pSelect;
132370 pTriggerStep->orconf = OE_Default11;
132371 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
132372 return pTriggerStep;
132373}
132374
132375/*
132376** Allocate space to hold a new trigger step. The allocated space
132377** holds both the TriggerStep object and the TriggerStep.target.z string.
132378**
132379** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
132380*/
132381static TriggerStep *triggerStepAllocate(
132382 Parse *pParse, /* Parser context */
132383 u8 op, /* Trigger opcode */
132384 Token *pName, /* The target name */
132385 const char *zStart, /* Start of SQL text */
132386 const char *zEnd /* End of SQL text */
132387){
132388 sqlite3 *db = pParse->db;
132389 TriggerStep *pTriggerStep;
132390
132391 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
132392 if( pTriggerStep ){
132393 char *z = (char*)&pTriggerStep[1];
132394 memcpy(z, pName->z, pName->n);
132395 sqlite3Dequote(z);
132396 pTriggerStep->zTarget = z;
132397 pTriggerStep->op = op;
132398 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
132399 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
132400 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
132401 }
132402 }
132403 return pTriggerStep;
132404}
132405
132406/*
132407** Build a trigger step out of an INSERT statement. Return a pointer
132408** to the new trigger step.
132409**
132410** The parser calls this routine when it sees an INSERT inside the
132411** body of a trigger.
132412*/
132413SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerInsertStep(
132414 Parse *pParse, /* Parser */
132415 Token *pTableName, /* Name of the table into which we insert */
132416 IdList *pColumn, /* List of columns in pTableName to insert into */
132417 Select *pSelect, /* A SELECT statement that supplies values */
132418 u8 orconf, /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
132419 Upsert *pUpsert, /* ON CONFLICT clauses for upsert */
132420 const char *zStart, /* Start of SQL text */
132421 const char *zEnd /* End of SQL text */
132422){
132423 sqlite3 *db = pParse->db;
132424 TriggerStep *pTriggerStep;
132425
132426 assert(pSelect != 0 || db->mallocFailed)((void) (0));
132427
132428 pTriggerStep = triggerStepAllocate(pParse, TK_INSERT120, pTableName,zStart,zEnd);
132429 if( pTriggerStep ){
132430 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
132431 pTriggerStep->pSelect = pSelect;
132432 pSelect = 0;
132433 }else{
132434 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE0x0001);
132435 }
132436 pTriggerStep->pIdList = pColumn;
132437 pTriggerStep->pUpsert = pUpsert;
132438 pTriggerStep->orconf = orconf;
132439 }else{
132440 testcase( pColumn );
132441 sqlite3IdListDelete(db, pColumn);
132442 testcase( pUpsert );
132443 sqlite3UpsertDelete(db, pUpsert);
132444 }
132445 sqlite3SelectDelete(db, pSelect);
132446
132447 return pTriggerStep;
132448}
132449
132450/*
132451** Construct a trigger step that implements an UPDATE statement and return
132452** a pointer to that trigger step. The parser calls this routine when it
132453** sees an UPDATE statement inside the body of a CREATE TRIGGER.
132454*/
132455SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerUpdateStep(
132456 Parse *pParse, /* Parser */
132457 Token *pTableName, /* Name of the table to be updated */
132458 ExprList *pEList, /* The SET clause: list of column and new values */
132459 Expr *pWhere, /* The WHERE clause */
132460 u8 orconf, /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
132461 const char *zStart, /* Start of SQL text */
132462 const char *zEnd /* End of SQL text */
132463){
132464 sqlite3 *db = pParse->db;
132465 TriggerStep *pTriggerStep;
132466
132467 pTriggerStep = triggerStepAllocate(pParse, TK_UPDATE122, pTableName,zStart,zEnd);
132468 if( pTriggerStep ){
132469 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
132470 pTriggerStep->pExprList = pEList;
132471 pTriggerStep->pWhere = pWhere;
132472 pEList = 0;
132473 pWhere = 0;
132474 }else{
132475 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE0x0001);
132476 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
132477 }
132478 pTriggerStep->orconf = orconf;
132479 }
132480 sqlite3ExprListDelete(db, pEList);
132481 sqlite3ExprDelete(db, pWhere);
132482 return pTriggerStep;
132483}
132484
132485/*
132486** Construct a trigger step that implements a DELETE statement and return
132487** a pointer to that trigger step. The parser calls this routine when it
132488** sees a DELETE statement inside the body of a CREATE TRIGGER.
132489*/
132490SQLITE_PRIVATEstatic TriggerStep *sqlite3TriggerDeleteStep(
132491 Parse *pParse, /* Parser */
132492 Token *pTableName, /* The table from which rows are deleted */
132493 Expr *pWhere, /* The WHERE clause */
132494 const char *zStart, /* Start of SQL text */
132495 const char *zEnd /* End of SQL text */
132496){
132497 sqlite3 *db = pParse->db;
132498 TriggerStep *pTriggerStep;
132499
132500 pTriggerStep = triggerStepAllocate(pParse, TK_DELETE121, pTableName,zStart,zEnd);
132501 if( pTriggerStep ){
132502 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
132503 pTriggerStep->pWhere = pWhere;
132504 pWhere = 0;
132505 }else{
132506 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE0x0001);
132507 }
132508 pTriggerStep->orconf = OE_Default11;
132509 }
132510 sqlite3ExprDelete(db, pWhere);
132511 return pTriggerStep;
132512}
132513
132514/*
132515** Recursively delete a Trigger structure
132516*/
132517SQLITE_PRIVATEstatic void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
132518 if( pTrigger==0 ) return;
132519 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
132520 sqlite3DbFree(db, pTrigger->zName);
132521 sqlite3DbFree(db, pTrigger->table);
132522 sqlite3ExprDelete(db, pTrigger->pWhen);
132523 sqlite3IdListDelete(db, pTrigger->pColumns);
132524 sqlite3DbFree(db, pTrigger);
132525}
132526
132527/*
132528** This function is called to drop a trigger from the database schema.
132529**
132530** This may be called directly from the parser and therefore identifies
132531** the trigger by name. The sqlite3DropTriggerPtr() routine does the
132532** same job as this routine except it takes a pointer to the trigger
132533** instead of the trigger name.
132534**/
132535SQLITE_PRIVATEstatic void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
132536 Trigger *pTrigger = 0;
132537 int i;
132538 const char *zDb;
132539 const char *zName;
132540 sqlite3 *db = pParse->db;
132541
132542 if( db->mallocFailed ) goto drop_trigger_cleanup;
132543 if( SQLITE_OK0!=sqlite3ReadSchema(pParse) ){
132544 goto drop_trigger_cleanup;
132545 }
132546
132547 assert( pName->nSrc==1 )((void) (0));
132548 zDb = pName->a[0].zDatabase;
132549 zName = pName->a[0].zName;
132550 assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
132551 for(i=OMIT_TEMPDB0; i<db->nDb; i++){
132552 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
132553 if( zDb && sqlite3StrICmp(db->aDb[j].zDbSName, zDb) ) continue;
132554 assert( sqlite3SchemaMutexHeld(db, j, 0) )((void) (0));
132555 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
132556 if( pTrigger ) break;
132557 }
132558 if( !pTrigger ){
132559 if( !noErr ){
132560 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
132561 }else{
132562 sqlite3CodeVerifyNamedSchema(pParse, zDb);
132563 }
132564 pParse->checkSchema = 1;
132565 goto drop_trigger_cleanup;
132566 }
132567 sqlite3DropTriggerPtr(pParse, pTrigger);
132568
132569drop_trigger_cleanup:
132570 sqlite3SrcListDelete(db, pName);
132571}
132572
132573/*
132574** Return a pointer to the Table structure for the table that a trigger
132575** is set on.
132576*/
132577static Table *tableOfTrigger(Trigger *pTrigger){
132578 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
132579}
132580
132581
132582/*
132583** Drop a trigger given a pointer to that trigger.
132584*/
132585SQLITE_PRIVATEstatic void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
132586 Table *pTable;
132587 Vdbe *v;
132588 sqlite3 *db = pParse->db;
132589 int iDb;
132590
132591 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
132592 assert( iDb>=0 && iDb<db->nDb )((void) (0));
132593 pTable = tableOfTrigger(pTrigger);
132594 assert( pTable )((void) (0));
132595 assert( pTable->pSchema==pTrigger->pSchema || iDb==1 )((void) (0));
132596#ifndef SQLITE_OMIT_AUTHORIZATION
132597 {
132598 int code = SQLITE_DROP_TRIGGER16;
132599 const char *zDb = db->aDb[iDb].zDbSName;
132600 const char *zTab = SCHEMA_TABLE(iDb)((!0)&&(iDb==1)?"sqlite_temp_master":"sqlite_master");
132601 if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER14;
132602 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
132603 sqlite3AuthCheck(pParse, SQLITE_DELETE9, zTab, 0, zDb) ){
132604 return;
132605 }
132606 }
132607#endif
132608
132609 /* Generate code to destroy the database record of the trigger.
132610 */
132611 assert( pTable!=0 )((void) (0));
132612 if( (v = sqlite3GetVdbe(pParse))!=0 ){
132613 sqlite3NestedParse(pParse,
132614 "DELETE FROM %Q.%s WHERE name=%Q AND type='trigger'",
132615 db->aDb[iDb].zDbSName, MASTER_NAME"sqlite_master", pTrigger->zName
132616 );
132617 sqlite3ChangeCookie(pParse, iDb);
132618 sqlite3VdbeAddOp4(v, OP_DropTrigger146, iDb, 0, 0, pTrigger->zName, 0);
132619 }
132620}
132621
132622/*
132623** Remove a trigger from the hash tables of the sqlite* pointer.
132624*/
132625SQLITE_PRIVATEstatic void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
132626 Trigger *pTrigger;
132627 Hash *pHash;
132628
132629 assert( sqlite3SchemaMutexHeld(db, iDb, 0) )((void) (0));
132630 pHash = &(db->aDb[iDb].pSchema->trigHash);
132631 pTrigger = sqlite3HashInsert(pHash, zName, 0);
132632 if( ALWAYS(pTrigger)(pTrigger) ){
132633 if( pTrigger->pSchema==pTrigger->pTabSchema ){
132634 Table *pTab = tableOfTrigger(pTrigger);
132635 Trigger **pp;
132636 for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
132637 *pp = (*pp)->pNext;
132638 }
132639 sqlite3DeleteTrigger(db, pTrigger);
132640 db->mDbFlags |= DBFLAG_SchemaChange0x0001;
132641 }
132642}
132643
132644/*
132645** pEList is the SET clause of an UPDATE statement. Each entry
132646** in pEList is of the format <id>=<expr>. If any of the entries
132647** in pEList have an <id> which matches an identifier in pIdList,
132648** then return TRUE. If pIdList==NULL, then it is considered a
132649** wildcard that matches anything. Likewise if pEList==NULL then
132650** it matches anything so always return true. Return false only
132651** if there is no match.
132652*/
132653static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
132654 int e;
132655 if( pIdList==0 || NEVER(pEList==0)(pEList==0) ) return 1;
132656 for(e=0; e<pEList->nExpr; e++){
132657 if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
132658 }
132659 return 0;
132660}
132661
132662/*
132663** Return a list of all triggers on table pTab if there exists at least
132664** one trigger that must be fired when an operation of type 'op' is
132665** performed on the table, and, if that operation is an UPDATE, if at
132666** least one of the columns in pChanges is being modified.
132667*/
132668SQLITE_PRIVATEstatic Trigger *sqlite3TriggersExist(
132669 Parse *pParse, /* Parse context */
132670 Table *pTab, /* The table the contains the triggers */
132671 int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
132672 ExprList *pChanges, /* Columns that change in an UPDATE statement */
132673 int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
132674){
132675 int mask = 0;
132676 Trigger *pList = 0;
132677 Trigger *p;
132678
132679 if( (pParse->db->flags & SQLITE_EnableTrigger0x00040000)!=0 ){
132680 pList = sqlite3TriggerList(pParse, pTab);
132681 }
132682 assert( pList==0 || IsVirtual(pTab)==0 )((void) (0));
132683 for(p=pList; p; p=p->pNext){
132684 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
132685 mask |= p->tr_tm;
132686 }
132687 }
132688 if( pMask ){
132689 *pMask = mask;
132690 }
132691 return (mask ? pList : 0);
132692}
132693
132694/*
132695** Convert the pStep->zTarget string into a SrcList and return a pointer
132696** to that SrcList.
132697**
132698** This routine adds a specific database name, if needed, to the target when
132699** forming the SrcList. This prevents a trigger in one database from
132700** referring to a target in another database. An exception is when the
132701** trigger is in TEMP in which case it can refer to any other database it
132702** wants.
132703*/
132704static SrcList *targetSrcList(
132705 Parse *pParse, /* The parsing context */
132706 TriggerStep *pStep /* The trigger containing the target token */
132707){
132708 sqlite3 *db = pParse->db;
132709 int iDb; /* Index of the database to use */
132710 SrcList *pSrc; /* SrcList to be returned */
132711
132712 pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
132713 if( pSrc ){
132714 assert( pSrc->nSrc>0 )((void) (0));
132715 pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget);
132716 iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema);
132717 if( iDb==0 || iDb>=2 ){
132718 const char *zDb;
132719 assert( iDb<db->nDb )((void) (0));
132720 zDb = db->aDb[iDb].zDbSName;
132721 pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, zDb);
132722 }
132723 }
132724 return pSrc;
132725}
132726
132727/*
132728** Generate VDBE code for the statements inside the body of a single
132729** trigger.
132730*/
132731static int codeTriggerProgram(
132732 Parse *pParse, /* The parser context */
132733 TriggerStep *pStepList, /* List of statements inside the trigger body */
132734 int orconf /* Conflict algorithm. (OE_Abort, etc) */
132735){
132736 TriggerStep *pStep;
132737 Vdbe *v = pParse->pVdbe;
132738 sqlite3 *db = pParse->db;
132739
132740 assert( pParse->pTriggerTab && pParse->pToplevel )((void) (0));
132741 assert( pStepList )((void) (0));
132742 assert( v!=0 )((void) (0));
132743 for(pStep=pStepList; pStep; pStep=pStep->pNext){
132744 /* Figure out the ON CONFLICT policy that will be used for this step
132745 ** of the trigger program. If the statement that caused this trigger
132746 ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
132747 ** the ON CONFLICT policy that was specified as part of the trigger
132748 ** step statement. Example:
132749 **
132750 ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
132751 ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
132752 ** END;
132753 **
132754 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
132755 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
132756 */
132757 pParse->eOrconf = (orconf==OE_Default11)?pStep->orconf:(u8)orconf;
132758 assert( pParse->okConstFactor==0 )((void) (0));
132759
132760#ifndef SQLITE_OMIT_TRACE
132761 if( pStep->zSpan ){
132762 sqlite3VdbeAddOp4(v, OP_Trace168, 0x7fffffff, 1, 0,
132763 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
132764 P4_DYNAMIC(-7));
132765 }
132766#endif
132767
132768 switch( pStep->op ){
132769 case TK_UPDATE122: {
132770 sqlite3Update(pParse,
132771 targetSrcList(pParse, pStep),
132772 sqlite3ExprListDup(db, pStep->pExprList, 0),
132773 sqlite3ExprDup(db, pStep->pWhere, 0),
132774 pParse->eOrconf, 0, 0, 0
132775 );
132776 break;
132777 }
132778 case TK_INSERT120: {
132779 sqlite3Insert(pParse,
132780 targetSrcList(pParse, pStep),
132781 sqlite3SelectDup(db, pStep->pSelect, 0),
132782 sqlite3IdListDup(db, pStep->pIdList),
132783 pParse->eOrconf,
132784 sqlite3UpsertDup(db, pStep->pUpsert)
132785 );
132786 break;
132787 }
132788 case TK_DELETE121: {
132789 sqlite3DeleteFrom(pParse,
132790 targetSrcList(pParse, pStep),
132791 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
132792 );
132793 break;
132794 }
132795 default: assert( pStep->op==TK_SELECT )((void) (0)); {
132796 SelectDest sDest;
132797 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
132798 sqlite3SelectDestInit(&sDest, SRT_Discard4, 0);
132799 sqlite3Select(pParse, pSelect, &sDest);
132800 sqlite3SelectDelete(db, pSelect);
132801 break;
132802 }
132803 }
132804 if( pStep->op!=TK_SELECT131 ){
132805 sqlite3VdbeAddOp0(v, OP_ResetCount124);
132806 }
132807 }
132808
132809 return 0;
132810}
132811
132812#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
132813/*
132814** This function is used to add VdbeComment() annotations to a VDBE
132815** program. It is not used in production code, only for debugging.
132816*/
132817static const char *onErrorText(int onError){
132818 switch( onError ){
132819 case OE_Abort2: return "abort";
132820 case OE_Rollback1: return "rollback";
132821 case OE_Fail3: return "fail";
132822 case OE_Replace5: return "replace";
132823 case OE_Ignore4: return "ignore";
132824 case OE_Default11: return "default";
132825 }
132826 return "n/a";
132827}
132828#endif
132829
132830/*
132831** Parse context structure pFrom has just been used to create a sub-vdbe
132832** (trigger program). If an error has occurred, transfer error information
132833** from pFrom to pTo.
132834*/
132835static void transferParseError(Parse *pTo, Parse *pFrom){
132836 assert( pFrom->zErrMsg==0 || pFrom->nErr )((void) (0));
132837 assert( pTo->zErrMsg==0 || pTo->nErr )((void) (0));
132838 if( pTo->nErr==0 ){
132839 pTo->zErrMsg = pFrom->zErrMsg;
132840 pTo->nErr = pFrom->nErr;
132841 pTo->rc = pFrom->rc;
132842 }else{
132843 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
132844 }
132845}
132846
132847/*
132848** Create and populate a new TriggerPrg object with a sub-program
132849** implementing trigger pTrigger with ON CONFLICT policy orconf.
132850*/
132851static TriggerPrg *codeRowTrigger(
132852 Parse *pParse, /* Current parse context */
132853 Trigger *pTrigger, /* Trigger to code */
132854 Table *pTab, /* The table pTrigger is attached to */
132855 int orconf /* ON CONFLICT policy to code trigger program with */
132856){
132857 Parse *pTop = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
132858 sqlite3 *db = pParse->db; /* Database handle */
132859 TriggerPrg *pPrg; /* Value to return */
132860 Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */
132861 Vdbe *v; /* Temporary VM */
132862 NameContext sNC; /* Name context for sub-vdbe */
132863 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
132864 Parse *pSubParse; /* Parse context for sub-vdbe */
132865 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
132866
132867 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) )((void) (0));
132868 assert( pTop->pVdbe )((void) (0));
132869
132870 /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
132871 ** are freed if an error occurs, link them into the Parse.pTriggerPrg
132872 ** list of the top-level Parse object sooner rather than later. */
132873 pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
132874 if( !pPrg ) return 0;
132875 pPrg->pNext = pTop->pTriggerPrg;
132876 pTop->pTriggerPrg = pPrg;
132877 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
132878 if( !pProgram ) return 0;
132879 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
132880 pPrg->pTrigger = pTrigger;
132881 pPrg->orconf = orconf;
132882 pPrg->aColmask[0] = 0xffffffff;
132883 pPrg->aColmask[1] = 0xffffffff;
132884
132885 /* Allocate and populate a new Parse context to use for coding the
132886 ** trigger sub-program. */
132887 pSubParse = sqlite3StackAllocZero(db, sizeof(Parse))sqlite3DbMallocZero(db,sizeof(Parse));
132888 if( !pSubParse ) return 0;
132889 memset(&sNC, 0, sizeof(sNC));
132890 sNC.pParse = pSubParse;
132891 pSubParse->db = db;
132892 pSubParse->pTriggerTab = pTab;
132893 pSubParse->pToplevel = pTop;
132894 pSubParse->zAuthContext = pTrigger->zName;
132895 pSubParse->eTriggerOp = pTrigger->op;
132896 pSubParse->nQueryLoop = pParse->nQueryLoop;
132897 pSubParse->disableVtab = pParse->disableVtab;
132898
132899 v = sqlite3GetVdbe(pSubParse);
132900 if( v ){
132901 VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
132902 pTrigger->zName, onErrorText(orconf),
132903 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
132904 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
132905 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
132906 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
132907 pTab->zName
132908 ));
132909#ifndef SQLITE_OMIT_TRACE
132910 if( pTrigger->zName ){
132911 sqlite3VdbeChangeP4(v, -1,
132912 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC(-7)
132913 );
132914 }
132915#endif
132916
132917 /* If one was specified, code the WHEN clause. If it evaluates to false
132918 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
132919 ** OP_Halt inserted at the end of the program. */
132920 if( pTrigger->pWhen ){
132921 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
132922 if( SQLITE_OK0==sqlite3ResolveExprNames(&sNC, pWhen)
132923 && db->mallocFailed==0
132924 ){
132925 iEndTrigger = sqlite3VdbeMakeLabel(pSubParse);
132926 sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL0x10);
132927 }
132928 sqlite3ExprDelete(db, pWhen);
132929 }
132930
132931 /* Code the trigger program into the sub-vdbe. */
132932 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
132933
132934 /* Insert an OP_Halt at the end of the sub-program. */
132935 if( iEndTrigger ){
132936 sqlite3VdbeResolveLabel(v, iEndTrigger);
132937 }
132938 sqlite3VdbeAddOp0(v, OP_Halt69);
132939 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
132940
132941 transferParseError(pParse, pSubParse);
132942 if( db->mallocFailed==0 && pParse->nErr==0 ){
132943 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
132944 }
132945 pProgram->nMem = pSubParse->nMem;
132946 pProgram->nCsr = pSubParse->nTab;
132947 pProgram->token = (void *)pTrigger;
132948 pPrg->aColmask[0] = pSubParse->oldmask;
132949 pPrg->aColmask[1] = pSubParse->newmask;
132950 sqlite3VdbeDelete(v);
132951 }
132952
132953 assert( !pSubParse->pAinc && !pSubParse->pZombieTab )((void) (0));
132954 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg )((void) (0));
132955 sqlite3ParserReset(pSubParse);
132956 sqlite3StackFree(db, pSubParse)sqlite3DbFree(db,pSubParse);
132957
132958 return pPrg;
132959}
132960
132961/*
132962** Return a pointer to a TriggerPrg object containing the sub-program for
132963** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
132964** TriggerPrg object exists, a new object is allocated and populated before
132965** being returned.
132966*/
132967static TriggerPrg *getRowTrigger(
132968 Parse *pParse, /* Current parse context */
132969 Trigger *pTrigger, /* Trigger to code */
132970 Table *pTab, /* The table trigger pTrigger is attached to */
132971 int orconf /* ON CONFLICT algorithm. */
132972){
132973 Parse *pRoot = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
132974 TriggerPrg *pPrg;
132975
132976 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) )((void) (0));
132977
132978 /* It may be that this trigger has already been coded (or is in the
132979 ** process of being coded). If this is the case, then an entry with
132980 ** a matching TriggerPrg.pTrigger field will be present somewhere
132981 ** in the Parse.pTriggerPrg list. Search for such an entry. */
132982 for(pPrg=pRoot->pTriggerPrg;
132983 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
132984 pPrg=pPrg->pNext
132985 );
132986
132987 /* If an existing TriggerPrg could not be located, create a new one. */
132988 if( !pPrg ){
132989 pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
132990 }
132991
132992 return pPrg;
132993}
132994
132995/*
132996** Generate code for the trigger program associated with trigger p on
132997** table pTab. The reg, orconf and ignoreJump parameters passed to this
132998** function are the same as those described in the header function for
132999** sqlite3CodeRowTrigger()
133000*/
133001SQLITE_PRIVATEstatic void sqlite3CodeRowTriggerDirect(
133002 Parse *pParse, /* Parse context */
133003 Trigger *p, /* Trigger to code */
133004 Table *pTab, /* The table to code triggers from */
133005 int reg, /* Reg array containing OLD.* and NEW.* values */
133006 int orconf, /* ON CONFLICT policy */
133007 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
133008){
133009 Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
133010 TriggerPrg *pPrg;
133011 pPrg = getRowTrigger(pParse, p, pTab, orconf);
133012 assert( pPrg || pParse->nErr || pParse->db->mallocFailed )((void) (0));
133013
133014 /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
133015 ** is a pointer to the sub-vdbe containing the trigger program. */
133016 if( pPrg ){
133017 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers0x00002000));
133018
133019 sqlite3VdbeAddOp4(v, OP_Program45, reg, ignoreJump, ++pParse->nMem,
133020 (const char *)pPrg->pProgram, P4_SUBPROGRAM(-4));
133021 VdbeComment(
133022 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
133023
133024 /* Set the P5 operand of the OP_Program instruction to non-zero if
133025 ** recursive invocation of this trigger program is disallowed. Recursive
133026 ** invocation is disallowed if (a) the sub-program is really a trigger,
133027 ** not a foreign key action, and (b) the flag to enable recursive triggers
133028 ** is clear. */
133029 sqlite3VdbeChangeP5(v, (u8)bRecursive);
133030 }
133031}
133032
133033/*
133034** This is called to code the required FOR EACH ROW triggers for an operation
133035** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
133036** is given by the op parameter. The tr_tm parameter determines whether the
133037** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
133038** parameter pChanges is passed the list of columns being modified.
133039**
133040** If there are no triggers that fire at the specified time for the specified
133041** operation on pTab, this function is a no-op.
133042**
133043** The reg argument is the address of the first in an array of registers
133044** that contain the values substituted for the new.* and old.* references
133045** in the trigger program. If N is the number of columns in table pTab
133046** (a copy of pTab->nCol), then registers are populated as follows:
133047**
133048** Register Contains
133049** ------------------------------------------------------
133050** reg+0 OLD.rowid
133051** reg+1 OLD.* value of left-most column of pTab
133052** ... ...
133053** reg+N OLD.* value of right-most column of pTab
133054** reg+N+1 NEW.rowid
133055** reg+N+2 OLD.* value of left-most column of pTab
133056** ... ...
133057** reg+N+N+1 NEW.* value of right-most column of pTab
133058**
133059** For ON DELETE triggers, the registers containing the NEW.* values will
133060** never be accessed by the trigger program, so they are not allocated or
133061** populated by the caller (there is no data to populate them with anyway).
133062** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
133063** are never accessed, and so are not allocated by the caller. So, for an
133064** ON INSERT trigger, the value passed to this function as parameter reg
133065** is not a readable register, although registers (reg+N) through
133066** (reg+N+N+1) are.
133067**
133068** Parameter orconf is the default conflict resolution algorithm for the
133069** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
133070** is the instruction that control should jump to if a trigger program
133071** raises an IGNORE exception.
133072*/
133073SQLITE_PRIVATEstatic void sqlite3CodeRowTrigger(
133074 Parse *pParse, /* Parse context */
133075 Trigger *pTrigger, /* List of triggers on table pTab */
133076 int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
133077 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
133078 int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
133079 Table *pTab, /* The table to code triggers from */
133080 int reg, /* The first in an array of registers (see above) */
133081 int orconf, /* ON CONFLICT policy */
133082 int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */
133083){
133084 Trigger *p; /* Used to iterate through pTrigger list */
133085
133086 assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE )((void) (0));
133087 assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER )((void) (0));
133088 assert( (op==TK_UPDATE)==(pChanges!=0) )((void) (0));
133089
133090 for(p=pTrigger; p; p=p->pNext){
133091
133092 /* Sanity checking: The schema for the trigger and for the table are
133093 ** always defined. The trigger must be in the same schema as the table
133094 ** or else it must be a TEMP trigger. */
133095 assert( p->pSchema!=0 )((void) (0));
133096 assert( p->pTabSchema!=0 )((void) (0));
133097 assert( p->pSchema==p->pTabSchema((void) (0))
133098 || p->pSchema==pParse->db->aDb[1].pSchema )((void) (0));
133099
133100 /* Determine whether we should code this trigger */
133101 if( p->op==op
133102 && p->tr_tm==tr_tm
133103 && checkColumnOverlap(p->pColumns, pChanges)
133104 ){
133105 sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
133106 }
133107 }
133108}
133109
133110/*
133111** Triggers may access values stored in the old.* or new.* pseudo-table.
133112** This function returns a 32-bit bitmask indicating which columns of the
133113** old.* or new.* tables actually are used by triggers. This information
133114** may be used by the caller, for example, to avoid having to load the entire
133115** old.* record into memory when executing an UPDATE or DELETE command.
133116**
133117** Bit 0 of the returned mask is set if the left-most column of the
133118** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
133119** the second leftmost column value is required, and so on. If there
133120** are more than 32 columns in the table, and at least one of the columns
133121** with an index greater than 32 may be accessed, 0xffffffff is returned.
133122**
133123** It is not possible to determine if the old.rowid or new.rowid column is
133124** accessed by triggers. The caller must always assume that it is.
133125**
133126** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
133127** applies to the old.* table. If 1, the new.* table.
133128**
133129** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
133130** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
133131** included in the returned mask if the TRIGGER_BEFORE bit is set in the
133132** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
133133** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
133134*/
133135SQLITE_PRIVATEstatic u32 sqlite3TriggerColmask(
133136 Parse *pParse, /* Parse context */
133137 Trigger *pTrigger, /* List of triggers on table pTab */
133138 ExprList *pChanges, /* Changes list for any UPDATE OF triggers */
133139 int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */
133140 int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
133141 Table *pTab, /* The table to code triggers from */
133142 int orconf /* Default ON CONFLICT policy for trigger steps */
133143){
133144 const int op = pChanges ? TK_UPDATE122 : TK_DELETE121;
133145 u32 mask = 0;
133146 Trigger *p;
133147
133148 assert( isNew==1 || isNew==0 )((void) (0));
133149 for(p=pTrigger; p; p=p->pNext){
133150 if( p->op==op && (tr_tm&p->tr_tm)
133151 && checkColumnOverlap(p->pColumns,pChanges)
133152 ){
133153 TriggerPrg *pPrg;
133154 pPrg = getRowTrigger(pParse, p, pTab, orconf);
133155 if( pPrg ){
133156 mask |= pPrg->aColmask[isNew];
133157 }
133158 }
133159 }
133160
133161 return mask;
133162}
133163
133164#endif /* !defined(SQLITE_OMIT_TRIGGER) */
133165
133166/************** End of trigger.c *********************************************/
133167/************** Begin file update.c ******************************************/
133168/*
133169** 2001 September 15
133170**
133171** The author disclaims copyright to this source code. In place of
133172** a legal notice, here is a blessing:
133173**
133174** May you do good and not evil.
133175** May you find forgiveness for yourself and forgive others.
133176** May you share freely, never taking more than you give.
133177**
133178*************************************************************************
133179** This file contains C code routines that are called by the parser
133180** to handle UPDATE statements.
133181*/
133182/* #include "sqliteInt.h" */
133183
133184#ifndef SQLITE_OMIT_VIRTUALTABLE
133185/* Forward declaration */
133186static void updateVirtualTable(
133187 Parse *pParse, /* The parsing context */
133188 SrcList *pSrc, /* The virtual table to be modified */
133189 Table *pTab, /* The virtual table */
133190 ExprList *pChanges, /* The columns to change in the UPDATE statement */
133191 Expr *pRowidExpr, /* Expression used to recompute the rowid */
133192 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
133193 Expr *pWhere, /* WHERE clause of the UPDATE statement */
133194 int onError /* ON CONFLICT strategy */
133195);
133196#endif /* SQLITE_OMIT_VIRTUALTABLE */
133197
133198/*
133199** The most recently coded instruction was an OP_Column to retrieve the
133200** i-th column of table pTab. This routine sets the P4 parameter of the
133201** OP_Column to the default value, if any.
133202**
133203** The default value of a column is specified by a DEFAULT clause in the
133204** column definition. This was either supplied by the user when the table
133205** was created, or added later to the table definition by an ALTER TABLE
133206** command. If the latter, then the row-records in the table btree on disk
133207** may not contain a value for the column and the default value, taken
133208** from the P4 parameter of the OP_Column instruction, is returned instead.
133209** If the former, then all row-records are guaranteed to include a value
133210** for the column and the P4 value is not required.
133211**
133212** Column definitions created by an ALTER TABLE command may only have
133213** literal default values specified: a number, null or a string. (If a more
133214** complicated default expression value was provided, it is evaluated
133215** when the ALTER TABLE is executed and one of the literal values written
133216** into the sqlite_master table.)
133217**
133218** Therefore, the P4 parameter is only required if the default value for
133219** the column is a literal number, string or null. The sqlite3ValueFromExpr()
133220** function is capable of transforming these types of expressions into
133221** sqlite3_value objects.
133222**
133223** If parameter iReg is not negative, code an OP_RealAffinity instruction
133224** on register iReg. This is used when an equivalent integer value is
133225** stored in place of an 8-byte floating point value in order to save
133226** space.
133227*/
133228SQLITE_PRIVATEstatic void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
133229 assert( pTab!=0 )((void) (0));
133230 if( !pTab->pSelect ){
133231 sqlite3_value *pValue = 0;
133232 u8 enc = ENC(sqlite3VdbeDb(v))((sqlite3VdbeDb(v))->enc);
133233 Column *pCol = &pTab->aCol[i];
133234 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
133235 assert( i<pTab->nCol )((void) (0));
133236 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
133237 pCol->affinity, &pValue);
133238 if( pValue ){
133239 sqlite3VdbeAppendP4(v, pValue, P4_MEM(-11));
133240 }
133241 }
133242#ifndef SQLITE_OMIT_FLOATING_POINT
133243 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL'E' ){
133244 sqlite3VdbeAddOp1(v, OP_RealAffinity84, iReg);
133245 }
133246#endif
133247}
133248
133249/*
133250** Check to see if column iCol of index pIdx references any of the
133251** columns defined by aXRef and chngRowid. Return true if it does
133252** and false if not. This is an optimization. False-positives are a
133253** performance degradation, but false-negatives can result in a corrupt
133254** index and incorrect answers.
133255**
133256** aXRef[j] will be non-negative if column j of the original table is
133257** being updated. chngRowid will be true if the rowid of the table is
133258** being updated.
133259*/
133260static int indexColumnIsBeingUpdated(
133261 Index *pIdx, /* The index to check */
133262 int iCol, /* Which column of the index to check */
133263 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
133264 int chngRowid /* true if the rowid is being updated */
133265){
133266 i16 iIdxCol = pIdx->aiColumn[iCol];
133267 assert( iIdxCol!=XN_ROWID )((void) (0)); /* Cannot index rowid */
133268 if( iIdxCol>=0 ){
133269 return aXRef[iIdxCol]>=0;
133270 }
133271 assert( iIdxCol==XN_EXPR )((void) (0));
133272 assert( pIdx->aColExpr!=0 )((void) (0));
133273 assert( pIdx->aColExpr->a[iCol].pExpr!=0 )((void) (0));
133274 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
133275 aXRef,chngRowid);
133276}
133277
133278/*
133279** Check to see if index pIdx is a partial index whose conditional
133280** expression might change values due to an UPDATE. Return true if
133281** the index is subject to change and false if the index is guaranteed
133282** to be unchanged. This is an optimization. False-positives are a
133283** performance degradation, but false-negatives can result in a corrupt
133284** index and incorrect answers.
133285**
133286** aXRef[j] will be non-negative if column j of the original table is
133287** being updated. chngRowid will be true if the rowid of the table is
133288** being updated.
133289*/
133290static int indexWhereClauseMightChange(
133291 Index *pIdx, /* The index to check */
133292 int *aXRef, /* aXRef[j]>=0 if column j is being updated */
133293 int chngRowid /* true if the rowid is being updated */
133294){
133295 if( pIdx->pPartIdxWhere==0 ) return 0;
133296 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
133297 aXRef, chngRowid);
133298}
133299
133300/*
133301** Process an UPDATE statement.
133302**
133303** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
133304** \_______/ \________/ \______/ \________________/
133305* onError pTabList pChanges pWhere
133306*/
133307SQLITE_PRIVATEstatic void sqlite3Update(
133308 Parse *pParse, /* The parser context */
133309 SrcList *pTabList, /* The table in which we should change things */
133310 ExprList *pChanges, /* Things to be changed */
133311 Expr *pWhere, /* The WHERE clause. May be null */
133312 int onError, /* How to handle constraint errors */
133313 ExprList *pOrderBy, /* ORDER BY clause. May be null */
133314 Expr *pLimit, /* LIMIT clause. May be null */
133315 Upsert *pUpsert /* ON CONFLICT clause, or null */
133316){
133317 int i, j; /* Loop counters */
133318 Table *pTab; /* The table to be updated */
133319 int addrTop = 0; /* VDBE instruction address of the start of the loop */
133320 WhereInfo *pWInfo; /* Information about the WHERE clause */
133321 Vdbe *v; /* The virtual database engine */
133322 Index *pIdx; /* For looping over indices */
133323 Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */
133324 int nIdx; /* Number of indices that need updating */
133325 int nAllIdx; /* Total number of indexes */
133326 int iBaseCur; /* Base cursor number */
133327 int iDataCur; /* Cursor for the canonical data btree */
133328 int iIdxCur; /* Cursor for the first index */
133329 sqlite3 *db; /* The database structure */
133330 int *aRegIdx = 0; /* Registers for to each index and the main table */
133331 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
133332 ** an expression for the i-th column of the table.
133333 ** aXRef[i]==-1 if the i-th column is not changed. */
133334 u8 *aToOpen; /* 1 for tables and indices to be opened */
133335 u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */
133336 u8 chngRowid; /* Rowid changed in a normal table */
133337 u8 chngKey; /* Either chngPk or chngRowid */
133338 Expr *pRowidExpr = 0; /* Expression defining the new record number */
133339 AuthContext sContext; /* The authorization context */
133340 NameContext sNC; /* The name-context to resolve expressions in */
133341 int iDb; /* Database containing the table being updated */
133342 int eOnePass; /* ONEPASS_XXX value from where.c */
133343 int hasFK; /* True if foreign key processing is required */
133344 int labelBreak; /* Jump here to break out of UPDATE loop */
133345 int labelContinue; /* Jump here to continue next step of UPDATE loop */
133346 int flags; /* Flags for sqlite3WhereBegin() */
133347
133348#ifndef SQLITE_OMIT_TRIGGER
133349 int isView; /* True when updating a view (INSTEAD OF trigger) */
133350 Trigger *pTrigger; /* List of triggers on pTab, if required */
133351 int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
133352#endif
133353 int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */
133354 int iEph = 0; /* Ephemeral table holding all primary key values */
133355 int nKey = 0; /* Number of elements in regKey for WITHOUT ROWID */
133356 int aiCurOnePass[2]; /* The write cursors opened by WHERE_ONEPASS */
133357 int addrOpen = 0; /* Address of OP_OpenEphemeral */
133358 int iPk = 0; /* First of nPk cells holding PRIMARY KEY value */
133359 i16 nPk = 0; /* Number of components of the PRIMARY KEY */
133360 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
133361
133362 /* Register Allocations */
133363 int regRowCount = 0; /* A count of rows changed */
133364 int regOldRowid = 0; /* The old rowid */
133365 int regNewRowid = 0; /* The new rowid */
133366 int regNew = 0; /* Content of the NEW.* table in triggers */
133367 int regOld = 0; /* Content of OLD.* table in triggers */
133368 int regRowSet = 0; /* Rowset of rows to be updated */
133369 int regKey = 0; /* composite PRIMARY KEY value */
133370
133371 memset(&sContext, 0, sizeof(sContext));
133372 db = pParse->db;
133373 if( pParse->nErr || db->mallocFailed ){
133374 goto update_cleanup;
133375 }
133376 assert( pTabList->nSrc==1 )((void) (0));
133377
133378 /* Locate the table which we want to update.
133379 */
133380 pTab = sqlite3SrcListLookup(pParse, pTabList);
133381 if( pTab==0 ) goto update_cleanup;
133382 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
133383
133384 /* Figure out if we have any triggers and if the table being
133385 ** updated is a view.
133386 */
133387#ifndef SQLITE_OMIT_TRIGGER
133388 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE122, pChanges, &tmask);
133389 isView = pTab->pSelect!=0;
133390 assert( pTrigger || tmask==0 )((void) (0));
133391#else
133392# define pTrigger 0
133393# define isView 0
133394# define tmask 0
133395#endif
133396#ifdef SQLITE_OMIT_VIEW
133397# undef isView
133398# define isView 0
133399#endif
133400
133401#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
133402 if( !isView ){
133403 pWhere = sqlite3LimitWhere(
133404 pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
133405 );
133406 pOrderBy = 0;
133407 pLimit = 0;
133408 }
133409#endif
133410
133411 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
133412 goto update_cleanup;
133413 }
133414 if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
133415 goto update_cleanup;
133416 }
133417
133418 /* Allocate a cursors for the main database table and for all indices.
133419 ** The index cursors might not be used, but if they are used they
133420 ** need to occur right after the database cursor. So go ahead and
133421 ** allocate enough space, just in case.
133422 */
133423 iBaseCur = iDataCur = pParse->nTab++;
133424 iIdxCur = iDataCur+1;
133425 pPk = HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ? 0 : sqlite3PrimaryKeyIndex(pTab);
133426 testcase( pPk!=0 && pPk!=pTab->pIndex );
133427 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
133428 if( pPk==pIdx ){
133429 iDataCur = pParse->nTab;
133430 }
133431 pParse->nTab++;
133432 }
133433 if( pUpsert ){
133434 /* On an UPSERT, reuse the same cursors already opened by INSERT */
133435 iDataCur = pUpsert->iDataCur;
133436 iIdxCur = pUpsert->iIdxCur;
133437 pParse->nTab = iBaseCur;
133438 }
133439 pTabList->a[0].iCursor = iDataCur;
133440
133441 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
133442 ** Initialize aXRef[] and aToOpen[] to their default values.
133443 */
133444 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
133445 if( aXRef==0 ) goto update_cleanup;
133446 aRegIdx = aXRef+pTab->nCol;
133447 aToOpen = (u8*)(aRegIdx+nIdx+1);
133448 memset(aToOpen, 1, nIdx+1);
133449 aToOpen[nIdx+1] = 0;
133450 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
133451
133452 /* Initialize the name-context */
133453 memset(&sNC, 0, sizeof(sNC));
133454 sNC.pParse = pParse;
133455 sNC.pSrcList = pTabList;
133456 sNC.uNC.pUpsert = pUpsert;
133457 sNC.ncFlags = NC_UUpsert0x0200;
133458
133459 /* Resolve the column names in all the expressions of the
133460 ** of the UPDATE statement. Also find the column index
133461 ** for each column to be updated in the pChanges array. For each
133462 ** column to be updated, make sure we have authorization to change
133463 ** that column.
133464 */
133465 chngRowid = chngPk = 0;
133466 for(i=0; i<pChanges->nExpr; i++){
133467 if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
133468 goto update_cleanup;
133469 }
133470 for(j=0; j<pTab->nCol; j++){
133471 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
133472 if( j==pTab->iPKey ){
133473 chngRowid = 1;
133474 pRowidExpr = pChanges->a[i].pExpr;
133475 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY0x0001)!=0 ){
133476 chngPk = 1;
133477 }
133478 aXRef[j] = i;
133479 break;
133480 }
133481 }
133482 if( j>=pTab->nCol ){
133483 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
133484 j = -1;
133485 chngRowid = 1;
133486 pRowidExpr = pChanges->a[i].pExpr;
133487 }else{
133488 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
133489 pParse->checkSchema = 1;
133490 goto update_cleanup;
133491 }
133492 }
133493#ifndef SQLITE_OMIT_AUTHORIZATION
133494 {
133495 int rc;
133496 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE23, pTab->zName,
133497 j<0 ? "ROWID" : pTab->aCol[j].zName,
133498 db->aDb[iDb].zDbSName);
133499 if( rc==SQLITE_DENY1 ){
133500 goto update_cleanup;
133501 }else if( rc==SQLITE_IGNORE2 ){
133502 aXRef[j] = -1;
133503 }
133504 }
133505#endif
133506 }
133507 assert( (chngRowid & chngPk)==0 )((void) (0));
133508 assert( chngRowid==0 || chngRowid==1 )((void) (0));
133509 assert( chngPk==0 || chngPk==1 )((void) (0));
133510 chngKey = chngRowid + chngPk;
133511
133512 /* The SET expressions are not actually used inside the WHERE loop.
133513 ** So reset the colUsed mask. Unless this is a virtual table. In that
133514 ** case, set all bits of the colUsed mask (to ensure that the virtual
133515 ** table implementation makes all columns available).
133516 */
133517 pTabList->a[0].colUsed = IsVirtual(pTab)((pTab)->nModuleArg) ? ALLBITS((Bitmask)-1) : 0;
133518
133519 hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
133520
133521 /* There is one entry in the aRegIdx[] array for each index on the table
133522 ** being updated. Fill in aRegIdx[] with a register number that will hold
133523 ** the key for accessing each index.
133524 */
133525 if( onError==OE_Replace5 ) bReplace = 1;
133526 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
133527 int reg;
133528 if( chngKey || hasFK>1 || pIdx==pPk
133529 || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
133530 ){
133531 reg = ++pParse->nMem;
133532 pParse->nMem += pIdx->nColumn;
133533 }else{
133534 reg = 0;
133535 for(i=0; i<pIdx->nKeyCol; i++){
133536 if( indexColumnIsBeingUpdated(pIdx, i, aXRef, chngRowid) ){
133537 reg = ++pParse->nMem;
133538 pParse->nMem += pIdx->nColumn;
133539 if( onError==OE_Default11 && pIdx->onError==OE_Replace5 ){
133540 bReplace = 1;
133541 }
133542 break;
133543 }
133544 }
133545 }
133546 if( reg==0 ) aToOpen[nAllIdx+1] = 0;
133547 aRegIdx[nAllIdx] = reg;
133548 }
133549 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
133550 if( bReplace ){
133551 /* If REPLACE conflict resolution might be invoked, open cursors on all
133552 ** indexes in case they are needed to delete records. */
133553 memset(aToOpen, 1, nIdx+1);
133554 }
133555
133556 /* Begin generating code. */
133557 v = sqlite3GetVdbe(pParse);
133558 if( v==0 ) goto update_cleanup;
133559 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
133560 sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
133561
133562 /* Allocate required registers. */
133563 if( !IsVirtual(pTab)((pTab)->nModuleArg) ){
133564 /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
133565 ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
133566 ** reallocated. aRegIdx[nAllIdx] is the register in which the main
133567 ** table record is written. regRowSet holds the RowSet for the
133568 ** two-pass update algorithm. */
133569 assert( aRegIdx[nAllIdx]==pParse->nMem )((void) (0));
133570 regRowSet = aRegIdx[nAllIdx];
133571 regOldRowid = regNewRowid = ++pParse->nMem;
133572 if( chngPk || pTrigger || hasFK ){
133573 regOld = pParse->nMem + 1;
133574 pParse->nMem += pTab->nCol;
133575 }
133576 if( chngKey || pTrigger || hasFK ){
133577 regNewRowid = ++pParse->nMem;
133578 }
133579 regNew = pParse->nMem + 1;
133580 pParse->nMem += pTab->nCol;
133581 }
133582
133583 /* Start the view context. */
133584 if( isView ){
133585 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
133586 }
133587
133588 /* If we are trying to update a view, realize that view into
133589 ** an ephemeral table.
133590 */
133591#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
133592 if( isView ){
133593 sqlite3MaterializeView(pParse, pTab,
133594 pWhere, pOrderBy, pLimit, iDataCur
133595 );
133596 pOrderBy = 0;
133597 pLimit = 0;
133598 }
133599#endif
133600
133601 /* Resolve the column names in all the expressions in the
133602 ** WHERE clause.
133603 */
133604 if( sqlite3ResolveExprNames(&sNC, pWhere) ){
133605 goto update_cleanup;
133606 }
133607
133608#ifndef SQLITE_OMIT_VIRTUALTABLE
133609 /* Virtual tables must be handled separately */
133610 if( IsVirtual(pTab)((pTab)->nModuleArg) ){
133611 updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
133612 pWhere, onError);
133613 goto update_cleanup;
133614 }
133615#endif
133616
133617 /* Jump to labelBreak to abandon further processing of this UPDATE */
133618 labelContinue = labelBreak = sqlite3VdbeMakeLabel(pParse);
133619
133620 /* Not an UPSERT. Normal processing. Begin by
133621 ** initialize the count of updated rows */
133622 if( (db->flags&SQLITE_CountRows0x00000080)!=0
133623 && !pParse->pTriggerTab
133624 && !pParse->nested
133625 && pUpsert==0
133626 ){
133627 regRowCount = ++pParse->nMem;
133628 sqlite3VdbeAddOp2(v, OP_Integer70, 0, regRowCount);
133629 }
133630
133631 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
133632 sqlite3VdbeAddOp3(v, OP_Null73, 0, regRowSet, regOldRowid);
133633 }else{
133634 assert( pPk!=0 )((void) (0));
133635 nPk = pPk->nKeyCol;
133636 iPk = pParse->nMem+1;
133637 pParse->nMem += nPk;
133638 regKey = ++pParse->nMem;
133639 if( pUpsert==0 ){
133640 iEph = pParse->nTab++;
133641 sqlite3VdbeAddOp3(v, OP_Null73, 0, iPk, iPk+nPk-1);
133642 addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, iEph, nPk);
133643 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
133644 }
133645 }
133646
133647 if( pUpsert ){
133648 /* If this is an UPSERT, then all cursors have already been opened by
133649 ** the outer INSERT and the data cursor should be pointing at the row
133650 ** that is to be updated. So bypass the code that searches for the
133651 ** row(s) to be updated.
133652 */
133653 pWInfo = 0;
133654 eOnePass = ONEPASS_SINGLE1;
133655 sqlite3ExprIfFalse(pParse, pWhere, labelBreak, SQLITE_JUMPIFNULL0x10);
133656 }else{
133657 /* Begin the database scan.
133658 **
133659 ** Do not consider a single-pass strategy for a multi-row update if
133660 ** there are any triggers or foreign keys to process, or rows may
133661 ** be deleted as a result of REPLACE conflict handling. Any of these
133662 ** things might disturb a cursor being used to scan through the table
133663 ** or index, causing a single-pass approach to malfunction. */
133664 flags = WHERE_ONEPASS_DESIRED0x0004|WHERE_SEEK_UNIQ_TABLE0x1000;
133665 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
133666 flags |= WHERE_ONEPASS_MULTIROW0x0008;
133667 }
133668 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur);
133669 if( pWInfo==0 ) goto update_cleanup;
133670
133671 /* A one-pass strategy that might update more than one row may not
133672 ** be used if any column of the index used for the scan is being
133673 ** updated. Otherwise, if there is an index on "b", statements like
133674 ** the following could create an infinite loop:
133675 **
133676 ** UPDATE t1 SET b=b+1 WHERE b>?
133677 **
133678 ** Fall back to ONEPASS_OFF if where.c has selected a ONEPASS_MULTI
133679 ** strategy that uses an index for which one or more columns are being
133680 ** updated. */
133681 eOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
133682 if( eOnePass!=ONEPASS_SINGLE1 ){
133683 sqlite3MultiWrite(pParse);
133684 if( eOnePass==ONEPASS_MULTI2 ){
133685 int iCur = aiCurOnePass[1];
133686 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
133687 eOnePass = ONEPASS_OFF0;
133688 }
133689 assert( iCur!=iDataCur || !HasRowid(pTab) )((void) (0));
133690 }
133691 }
133692 }
133693
133694 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
133695 /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
133696 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
133697 ** leave it in register regOldRowid. */
133698 sqlite3VdbeAddOp2(v, OP_Rowid128, iDataCur, regOldRowid);
133699 if( eOnePass==ONEPASS_OFF0 ){
133700 /* We need to use regRowSet, so reallocate aRegIdx[nAllIdx] */
133701 aRegIdx[nAllIdx] = ++pParse->nMem;
133702 sqlite3VdbeAddOp2(v, OP_RowSetAdd148, regRowSet, regOldRowid);
133703 }
133704 }else{
133705 /* Read the PK of the current row into an array of registers. In
133706 ** ONEPASS_OFF mode, serialize the array into a record and store it in
133707 ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
133708 ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table
133709 ** is not required) and leave the PK fields in the array of registers. */
133710 for(i=0; i<nPk; i++){
133711 assert( pPk->aiColumn[i]>=0 )((void) (0));
133712 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur,pPk->aiColumn[i],iPk+i);
133713 }
133714 if( eOnePass ){
133715 if( addrOpen ) sqlite3VdbeChangeToNoop(v, addrOpen);
133716 nKey = nPk;
133717 regKey = iPk;
133718 }else{
133719 sqlite3VdbeAddOp4(v, OP_MakeRecord92, iPk, nPk, regKey,
133720 sqlite3IndexAffinityStr(db, pPk), nPk);
133721 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, iEph, regKey, iPk, nPk);
133722 }
133723 }
133724
133725 if( pUpsert==0 ){
133726 if( eOnePass!=ONEPASS_MULTI2 ){
133727 sqlite3WhereEnd(pWInfo);
133728 }
133729
133730 if( !isView ){
133731 int addrOnce = 0;
133732
133733 /* Open every index that needs updating. */
133734 if( eOnePass!=ONEPASS_OFF0 ){
133735 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
133736 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
133737 }
133738
133739 if( eOnePass==ONEPASS_MULTI2 && (nIdx-(aiCurOnePass[1]>=0))>0 ){
133740 addrOnce = sqlite3VdbeAddOp0(v, OP_Once17); VdbeCoverage(v);
133741 }
133742 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite109, 0, iBaseCur,
133743 aToOpen, 0, 0);
133744 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce);
133745 }
133746
133747 /* Top of the update loop */
133748 if( eOnePass!=ONEPASS_OFF0 ){
133749 if( !isView && aiCurOnePass[0]!=iDataCur && aiCurOnePass[1]!=iDataCur ){
133750 assert( pPk )((void) (0));
133751 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, labelBreak, regKey,nKey);
133752 VdbeCoverage(v);
133753 }
133754 if( eOnePass!=ONEPASS_SINGLE1 ){
133755 labelContinue = sqlite3VdbeMakeLabel(pParse);
133756 }
133757 sqlite3VdbeAddOp2(v, OP_IsNull50, pPk ? regKey : regOldRowid, labelBreak);
133758 VdbeCoverageIf(v, pPk==0);
133759 VdbeCoverageIf(v, pPk!=0);
133760 }else if( pPk ){
133761 labelContinue = sqlite3VdbeMakeLabel(pParse);
133762 sqlite3VdbeAddOp2(v, OP_Rewind36, iEph, labelBreak); VdbeCoverage(v);
133763 addrTop = sqlite3VdbeAddOp2(v, OP_RowData127, iEph, regKey);
133764 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, labelContinue, regKey, 0);
133765 VdbeCoverage(v);
133766 }else{
133767 labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead41, regRowSet,labelBreak,
133768 regOldRowid);
133769 VdbeCoverage(v);
133770 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, labelContinue, regOldRowid);
133771 VdbeCoverage(v);
133772 }
133773 }
133774
133775 /* If the rowid value will change, set register regNewRowid to
133776 ** contain the new value. If the rowid is not being modified,
133777 ** then regNewRowid is the same register as regOldRowid, which is
133778 ** already populated. */
133779 assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid )((void) (0));
133780 if( chngRowid ){
133781 sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
133782 sqlite3VdbeAddOp1(v, OP_MustBeInt15, regNewRowid); VdbeCoverage(v);
133783 }
133784
133785 /* Compute the old pre-UPDATE content of the row being changed, if that
133786 ** information is needed */
133787 if( chngPk || hasFK || pTrigger ){
133788 u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
133789 oldmask |= sqlite3TriggerColmask(pParse,
133790 pTrigger, pChanges, 0, TRIGGER_BEFORE1|TRIGGER_AFTER2, pTab, onError
133791 );
133792 for(i=0; i<pTab->nCol; i++){
133793 if( oldmask==0xffffffff
133794 || (i<32 && (oldmask & MASKBIT32(i)(((unsigned int)1)<<(i)))!=0)
133795 || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY0x0001)!=0
133796 ){
133797 testcase( oldmask!=0xffffffff && i==31 );
133798 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
133799 }else{
133800 sqlite3VdbeAddOp2(v, OP_Null73, 0, regOld+i);
133801 }
133802 }
133803 if( chngRowid==0 && pPk==0 ){
133804 sqlite3VdbeAddOp2(v, OP_Copy78, regOldRowid, regNewRowid);
133805 }
133806 }
133807
133808 /* Populate the array of registers beginning at regNew with the new
133809 ** row data. This array is used to check constants, create the new
133810 ** table and index records, and as the values for any new.* references
133811 ** made by triggers.
133812 **
133813 ** If there are one or more BEFORE triggers, then do not populate the
133814 ** registers associated with columns that are (a) not modified by
133815 ** this UPDATE statement and (b) not accessed by new.* references. The
133816 ** values for registers not modified by the UPDATE must be reloaded from
133817 ** the database after the BEFORE triggers are fired anyway (as the trigger
133818 ** may have modified them). So not loading those that are not going to
133819 ** be used eliminates some redundant opcodes.
133820 */
133821 newmask = sqlite3TriggerColmask(
133822 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE1, pTab, onError
133823 );
133824 for(i=0; i<pTab->nCol; i++){
133825 if( i==pTab->iPKey ){
133826 sqlite3VdbeAddOp2(v, OP_Null73, 0, regNew+i);
133827 }else{
133828 j = aXRef[i];
133829 if( j>=0 ){
133830 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
133831 }else if( 0==(tmask&TRIGGER_BEFORE1) || i>31 || (newmask & MASKBIT32(i)(((unsigned int)1)<<(i))) ){
133832 /* This branch loads the value of a column that will not be changed
133833 ** into a register. This is done if there are no BEFORE triggers, or
133834 ** if there are one or more BEFORE triggers that use this value via
133835 ** a new.* reference in a trigger program.
133836 */
133837 testcase( i==31 );
133838 testcase( i==32 );
133839 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
133840 }else{
133841 sqlite3VdbeAddOp2(v, OP_Null73, 0, regNew+i);
133842 }
133843 }
133844 }
133845
133846 /* Fire any BEFORE UPDATE triggers. This happens before constraints are
133847 ** verified. One could argue that this is wrong.
133848 */
133849 if( tmask&TRIGGER_BEFORE1 ){
133850 sqlite3TableAffinity(v, pTab, regNew);
133851 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE122, pChanges,
133852 TRIGGER_BEFORE1, pTab, regOldRowid, onError, labelContinue);
133853
133854 /* The row-trigger may have deleted the row being updated. In this
133855 ** case, jump to the next row. No updates or AFTER triggers are
133856 ** required. This behavior - what happens when the row being updated
133857 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
133858 ** documentation.
133859 */
133860 if( pPk ){
133861 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, labelContinue,regKey,nKey);
133862 VdbeCoverage(v);
133863 }else{
133864 sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, labelContinue, regOldRowid);
133865 VdbeCoverage(v);
133866 }
133867
133868 /* After-BEFORE-trigger-reload-loop:
133869 ** If it did not delete it, the BEFORE trigger may still have modified
133870 ** some of the columns of the row being updated. Load the values for
133871 ** all columns not modified by the update statement into their registers
133872 ** in case this has happened. Only unmodified columns are reloaded.
133873 ** The values computed for modified columns use the values before the
133874 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
133875 ** for an example.
133876 */
133877 for(i=0; i<pTab->nCol; i++){
133878 if( aXRef[i]<0 && i!=pTab->iPKey ){
133879 sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
133880 }
133881 }
133882 }
133883
133884 if( !isView ){
133885 int addr1 = 0; /* Address of jump instruction */
133886
133887 /* Do constraint checks. */
133888 assert( regOldRowid>0 )((void) (0));
133889 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
133890 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
133891 aXRef, 0);
133892
133893 /* Do FK constraint checks. */
133894 if( hasFK ){
133895 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
133896 }
133897
133898 /* Delete the index entries associated with the current record. */
133899 if( bReplace || chngKey ){
133900 if( pPk ){
133901 addr1 = sqlite3VdbeAddOp4Int(v, OP_NotFound28, iDataCur, 0, regKey, nKey);
133902 }else{
133903 addr1 = sqlite3VdbeAddOp3(v, OP_NotExists31, iDataCur, 0, regOldRowid);
133904 }
133905 VdbeCoverageNeverTaken(v);
133906 }
133907 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
133908
133909 /* If changing the rowid value, or if there are foreign key constraints
133910 ** to process, delete the old record. Otherwise, add a noop OP_Delete
133911 ** to invoke the pre-update hook.
133912 **
133913 ** That (regNew==regnewRowid+1) is true is also important for the
133914 ** pre-update hook. If the caller invokes preupdate_new(), the returned
133915 ** value is copied from memory cell (regNewRowid+1+iCol), where iCol
133916 ** is the column index supplied by the user.
133917 */
133918 assert( regNew==regNewRowid+1 )((void) (0));
133919#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
133920 sqlite3VdbeAddOp3(v, OP_Delete123, iDataCur,
133921 OPFLAG_ISUPDATE0x04 | ((hasFK>1 || chngKey) ? 0 : OPFLAG_ISNOOP0x40),
133922 regNewRowid
133923 );
133924 if( eOnePass==ONEPASS_MULTI2 ){
133925 assert( hasFK==0 && chngKey==0 )((void) (0));
133926 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
133927 }
133928 if( !pParse->nested ){
133929 sqlite3VdbeAppendP4(v, pTab, P4_TABLE(-6));
133930 }
133931#else
133932 if( hasFK>1 || chngKey ){
133933 sqlite3VdbeAddOp2(v, OP_Delete123, iDataCur, 0);
133934 }
133935#endif
133936 if( bReplace || chngKey ){
133937 sqlite3VdbeJumpHere(v, addr1);
133938 }
133939
133940 if( hasFK ){
133941 sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
133942 }
133943
133944 /* Insert the new index entries and the new record. */
133945 sqlite3CompleteInsertion(
133946 pParse, pTab, iDataCur, iIdxCur, regNewRowid, aRegIdx,
133947 OPFLAG_ISUPDATE0x04 | (eOnePass==ONEPASS_MULTI2 ? OPFLAG_SAVEPOSITION0x02 : 0),
133948 0, 0
133949 );
133950
133951 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
133952 ** handle rows (possibly in other tables) that refer via a foreign key
133953 ** to the row just updated. */
133954 if( hasFK ){
133955 sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
133956 }
133957 }
133958
133959 /* Increment the row counter
133960 */
133961 if( regRowCount ){
133962 sqlite3VdbeAddOp2(v, OP_AddImm83, regRowCount, 1);
133963 }
133964
133965 sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE122, pChanges,
133966 TRIGGER_AFTER2, pTab, regOldRowid, onError, labelContinue);
133967
133968 /* Repeat the above with the next record to be updated, until
133969 ** all record selected by the WHERE clause have been updated.
133970 */
133971 if( eOnePass==ONEPASS_SINGLE1 ){
133972 /* Nothing to do at end-of-loop for a single-pass */
133973 }else if( eOnePass==ONEPASS_MULTI2 ){
133974 sqlite3VdbeResolveLabel(v, labelContinue);
133975 sqlite3WhereEnd(pWInfo);
133976 }else if( pPk ){
133977 sqlite3VdbeResolveLabel(v, labelContinue);
133978 sqlite3VdbeAddOp2(v, OP_Next5, iEph, addrTop); VdbeCoverage(v);
133979 }else{
133980 sqlite3VdbeGoto(v, labelContinue);
133981 }
133982 sqlite3VdbeResolveLabel(v, labelBreak);
133983
133984 /* Update the sqlite_sequence table by storing the content of the
133985 ** maximum rowid counter values recorded while inserting into
133986 ** autoincrement tables.
133987 */
133988 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
133989 sqlite3AutoincrementEnd(pParse);
133990 }
133991
133992 /*
133993 ** Return the number of rows that were changed, if we are tracking
133994 ** that information.
133995 */
133996 if( regRowCount ){
133997 sqlite3VdbeAddOp2(v, OP_ResultRow81, regRowCount, 1);
133998 sqlite3VdbeSetNumCols(v, 1);
133999 sqlite3VdbeSetColName(v, 0, COLNAME_NAME0, "rows updated", SQLITE_STATIC((sqlite3_destructor_type)0));
134000 }
134001
134002update_cleanup:
134003 sqlite3AuthContextPop(&sContext);
134004 sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
134005 sqlite3SrcListDelete(db, pTabList);
134006 sqlite3ExprListDelete(db, pChanges);
134007 sqlite3ExprDelete(db, pWhere);
134008#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
134009 sqlite3ExprListDelete(db, pOrderBy);
134010 sqlite3ExprDelete(db, pLimit);
134011#endif
134012 return;
134013}
134014/* Make sure "isView" and other macros defined above are undefined. Otherwise
134015** they may interfere with compilation of other functions in this file
134016** (or in another file, if this file becomes part of the amalgamation). */
134017#ifdef isView
134018 #undef isView
134019#endif
134020#ifdef pTrigger
134021 #undef pTrigger
134022#endif
134023
134024#ifndef SQLITE_OMIT_VIRTUALTABLE
134025/*
134026** Generate code for an UPDATE of a virtual table.
134027**
134028** There are two possible strategies - the default and the special
134029** "onepass" strategy. Onepass is only used if the virtual table
134030** implementation indicates that pWhere may match at most one row.
134031**
134032** The default strategy is to create an ephemeral table that contains
134033** for each row to be changed:
134034**
134035** (A) The original rowid of that row.
134036** (B) The revised rowid for the row.
134037** (C) The content of every column in the row.
134038**
134039** Then loop through the contents of this ephemeral table executing a
134040** VUpdate for each row. When finished, drop the ephemeral table.
134041**
134042** The "onepass" strategy does not use an ephemeral table. Instead, it
134043** stores the same values (A, B and C above) in a register array and
134044** makes a single invocation of VUpdate.
134045*/
134046static void updateVirtualTable(
134047 Parse *pParse, /* The parsing context */
134048 SrcList *pSrc, /* The virtual table to be modified */
134049 Table *pTab, /* The virtual table */
134050 ExprList *pChanges, /* The columns to change in the UPDATE statement */
134051 Expr *pRowid, /* Expression used to recompute the rowid */
134052 int *aXRef, /* Mapping from columns of pTab to entries in pChanges */
134053 Expr *pWhere, /* WHERE clause of the UPDATE statement */
134054 int onError /* ON CONFLICT strategy */
134055){
134056 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
134057 int ephemTab; /* Table holding the result of the SELECT */
134058 int i; /* Loop counter */
134059 sqlite3 *db = pParse->db; /* Database connection */
134060 const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
134061 WhereInfo *pWInfo;
134062 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
134063 int regArg; /* First register in VUpdate arg array */
134064 int regRec; /* Register in which to assemble record */
134065 int regRowid; /* Register for ephem table rowid */
134066 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
134067 int aDummy[2]; /* Unused arg for sqlite3WhereOkOnePass() */
134068 int eOnePass; /* True to use onepass strategy */
134069 int addr; /* Address of OP_OpenEphemeral */
134070
134071 /* Allocate nArg registers in which to gather the arguments for VUpdate. Then
134072 ** create and open the ephemeral table in which the records created from
134073 ** these arguments will be temporarily stored. */
134074 assert( v )((void) (0));
134075 ephemTab = pParse->nTab++;
134076 addr= sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, ephemTab, nArg);
134077 regArg = pParse->nMem + 1;
134078 pParse->nMem += nArg;
134079 regRec = ++pParse->nMem;
134080 regRowid = ++pParse->nMem;
134081
134082 /* Start scanning the virtual table */
134083 pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0,0,WHERE_ONEPASS_DESIRED0x0004,0);
134084 if( pWInfo==0 ) return;
134085
134086 /* Populate the argument registers. */
134087 for(i=0; i<pTab->nCol; i++){
134088 if( aXRef[i]>=0 ){
134089 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
134090 }else{
134091 sqlite3VdbeAddOp3(v, OP_VColumn164, iCsr, i, regArg+2+i);
134092 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG0x01);/* Enable sqlite3_vtab_nochange() */
134093 }
134094 }
134095 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
134096 sqlite3VdbeAddOp2(v, OP_Rowid128, iCsr, regArg);
134097 if( pRowid ){
134098 sqlite3ExprCode(pParse, pRowid, regArg+1);
134099 }else{
134100 sqlite3VdbeAddOp2(v, OP_Rowid128, iCsr, regArg+1);
134101 }
134102 }else{
134103 Index *pPk; /* PRIMARY KEY index */
134104 i16 iPk; /* PRIMARY KEY column */
134105 pPk = sqlite3PrimaryKeyIndex(pTab);
134106 assert( pPk!=0 )((void) (0));
134107 assert( pPk->nKeyCol==1 )((void) (0));
134108 iPk = pPk->aiColumn[0];
134109 sqlite3VdbeAddOp3(v, OP_VColumn164, iCsr, iPk, regArg);
134110 sqlite3VdbeAddOp2(v, OP_SCopy79, regArg+2+iPk, regArg+1);
134111 }
134112
134113 eOnePass = sqlite3WhereOkOnePass(pWInfo, aDummy);
134114
134115 /* There is no ONEPASS_MULTI on virtual tables */
134116 assert( eOnePass==ONEPASS_OFF || eOnePass==ONEPASS_SINGLE )((void) (0));
134117
134118 if( eOnePass ){
134119 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
134120 ** above. */
134121 sqlite3VdbeChangeToNoop(v, addr);
134122 sqlite3VdbeAddOp1(v, OP_Close117, iCsr);
134123 }else{
134124 /* Create a record from the argument register contents and insert it into
134125 ** the ephemeral table. */
134126 sqlite3MultiWrite(pParse);
134127 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regArg, nArg, regRec);
134128#ifdef SQLITE_DEBUG
134129 /* Signal an assert() within OP_MakeRecord that it is allowed to
134130 ** accept no-change records with serial_type 10 */
134131 sqlite3VdbeChangeP5(v, OPFLAG_NOCHNG_MAGIC0x6d);
134132#endif
134133 sqlite3VdbeAddOp2(v, OP_NewRowid121, ephemTab, regRowid);
134134 sqlite3VdbeAddOp3(v, OP_Insert122, ephemTab, regRec, regRowid);
134135 }
134136
134137
134138 if( eOnePass==ONEPASS_OFF0 ){
134139 /* End the virtual table scan */
134140 sqlite3WhereEnd(pWInfo);
134141
134142 /* Begin scannning through the ephemeral table. */
134143 addr = sqlite3VdbeAddOp1(v, OP_Rewind36, ephemTab); VdbeCoverage(v);
134144
134145 /* Extract arguments from the current row of the ephemeral table and
134146 ** invoke the VUpdate method. */
134147 for(i=0; i<nArg; i++){
134148 sqlite3VdbeAddOp3(v, OP_Column90, ephemTab, i, regArg+i);
134149 }
134150 }
134151 sqlite3VtabMakeWritable(pParse, pTab);
134152 sqlite3VdbeAddOp4(v, OP_VUpdate10, 0, nArg, regArg, pVTab, P4_VTAB(-12));
134153 sqlite3VdbeChangeP5(v, onError==OE_Default11 ? OE_Abort2 : onError);
134154 sqlite3MayAbort(pParse);
134155
134156 /* End of the ephemeral table scan. Or, if using the onepass strategy,
134157 ** jump to here if the scan visited zero rows. */
134158 if( eOnePass==ONEPASS_OFF0 ){
134159 sqlite3VdbeAddOp2(v, OP_Next5, ephemTab, addr+1); VdbeCoverage(v);
134160 sqlite3VdbeJumpHere(v, addr);
134161 sqlite3VdbeAddOp2(v, OP_Close117, ephemTab, 0);
134162 }else{
134163 sqlite3WhereEnd(pWInfo);
134164 }
134165}
134166#endif /* SQLITE_OMIT_VIRTUALTABLE */
134167
134168/************** End of update.c **********************************************/
134169/************** Begin file upsert.c ******************************************/
134170/*
134171** 2018-04-12
134172**
134173** The author disclaims copyright to this source code. In place of
134174** a legal notice, here is a blessing:
134175**
134176** May you do good and not evil.
134177** May you find forgiveness for yourself and forgive others.
134178** May you share freely, never taking more than you give.
134179**
134180*************************************************************************
134181** This file contains code to implement various aspects of UPSERT
134182** processing and handling of the Upsert object.
134183*/
134184/* #include "sqliteInt.h" */
134185
134186#ifndef SQLITE_OMIT_UPSERT
134187/*
134188** Free a list of Upsert objects
134189*/
134190SQLITE_PRIVATEstatic void sqlite3UpsertDelete(sqlite3 *db, Upsert *p){
134191 if( p ){
134192 sqlite3ExprListDelete(db, p->pUpsertTarget);
134193 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
134194 sqlite3ExprListDelete(db, p->pUpsertSet);
134195 sqlite3ExprDelete(db, p->pUpsertWhere);
134196 sqlite3DbFree(db, p);
134197 }
134198}
134199
134200/*
134201** Duplicate an Upsert object.
134202*/
134203SQLITE_PRIVATEstatic Upsert *sqlite3UpsertDup(sqlite3 *db, Upsert *p){
134204 if( p==0 ) return 0;
134205 return sqlite3UpsertNew(db,
134206 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
134207 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
134208 sqlite3ExprListDup(db, p->pUpsertSet, 0),
134209 sqlite3ExprDup(db, p->pUpsertWhere, 0)
134210 );
134211}
134212
134213/*
134214** Create a new Upsert object.
134215*/
134216SQLITE_PRIVATEstatic Upsert *sqlite3UpsertNew(
134217 sqlite3 *db, /* Determines which memory allocator to use */
134218 ExprList *pTarget, /* Target argument to ON CONFLICT, or NULL */
134219 Expr *pTargetWhere, /* Optional WHERE clause on the target */
134220 ExprList *pSet, /* UPDATE columns, or NULL for a DO NOTHING */
134221 Expr *pWhere /* WHERE clause for the ON CONFLICT UPDATE */
134222){
134223 Upsert *pNew;
134224 pNew = sqlite3DbMallocRaw(db, sizeof(Upsert));
134225 if( pNew==0 ){
134226 sqlite3ExprListDelete(db, pTarget);
134227 sqlite3ExprDelete(db, pTargetWhere);
134228 sqlite3ExprListDelete(db, pSet);
134229 sqlite3ExprDelete(db, pWhere);
134230 return 0;
134231 }else{
134232 pNew->pUpsertTarget = pTarget;
134233 pNew->pUpsertTargetWhere = pTargetWhere;
134234 pNew->pUpsertSet = pSet;
134235 pNew->pUpsertWhere = pWhere;
134236 pNew->pUpsertIdx = 0;
134237 }
134238 return pNew;
134239}
134240
134241/*
134242** Analyze the ON CONFLICT clause described by pUpsert. Resolve all
134243** symbols in the conflict-target.
134244**
134245** Return SQLITE_OK if everything works, or an error code is something
134246** is wrong.
134247*/
134248SQLITE_PRIVATEstatic int sqlite3UpsertAnalyzeTarget(
134249 Parse *pParse, /* The parsing context */
134250 SrcList *pTabList, /* Table into which we are inserting */
134251 Upsert *pUpsert /* The ON CONFLICT clauses */
134252){
134253 Table *pTab; /* That table into which we are inserting */
134254 int rc; /* Result code */
134255 int iCursor; /* Cursor used by pTab */
134256 Index *pIdx; /* One of the indexes of pTab */
134257 ExprList *pTarget; /* The conflict-target clause */
134258 Expr *pTerm; /* One term of the conflict-target clause */
134259 NameContext sNC; /* Context for resolving symbolic names */
134260 Expr sCol[2]; /* Index column converted into an Expr */
134261
134262 assert( pTabList->nSrc==1 )((void) (0));
134263 assert( pTabList->a[0].pTab!=0 )((void) (0));
134264 assert( pUpsert!=0 )((void) (0));
134265 assert( pUpsert->pUpsertTarget!=0 )((void) (0));
134266
134267 /* Resolve all symbolic names in the conflict-target clause, which
134268 ** includes both the list of columns and the optional partial-index
134269 ** WHERE clause.
134270 */
134271 memset(&sNC, 0, sizeof(sNC));
134272 sNC.pParse = pParse;
134273 sNC.pSrcList = pTabList;
134274 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
134275 if( rc ) return rc;
134276 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
134277 if( rc ) return rc;
134278
134279 /* Check to see if the conflict target matches the rowid. */
134280 pTab = pTabList->a[0].pTab;
134281 pTarget = pUpsert->pUpsertTarget;
134282 iCursor = pTabList->a[0].iCursor;
134283 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0)
134284 && pTarget->nExpr==1
134285 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN162
134286 && pTerm->iColumn==XN_ROWID(-1)
134287 ){
134288 /* The conflict-target is the rowid of the primary table */
134289 assert( pUpsert->pUpsertIdx==0 )((void) (0));
134290 return SQLITE_OK0;
134291 }
134292
134293 /* Initialize sCol[0..1] to be an expression parse tree for a
134294 ** single column of an index. The sCol[0] node will be the TK_COLLATE
134295 ** operator and sCol[1] will be the TK_COLUMN operator. Code below
134296 ** will populate the specific collation and column number values
134297 ** prior to comparing against the conflict-target expression.
134298 */
134299 memset(sCol, 0, sizeof(sCol));
134300 sCol[0].op = TK_COLLATE106;
134301 sCol[0].pLeft = &sCol[1];
134302 sCol[1].op = TK_COLUMN162;
134303 sCol[1].iTable = pTabList->a[0].iCursor;
134304
134305 /* Check for matches against other indexes */
134306 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
134307 int ii, jj, nn;
134308 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) continue;
134309 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
134310 if( pIdx->pPartIdxWhere ){
134311 if( pUpsert->pUpsertTargetWhere==0 ) continue;
134312 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
134313 pIdx->pPartIdxWhere, iCursor)!=0 ){
134314 continue;
134315 }
134316 }
134317 nn = pIdx->nKeyCol;
134318 for(ii=0; ii<nn; ii++){
134319 Expr *pExpr;
134320 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
134321 if( pIdx->aiColumn[ii]==XN_EXPR(-2) ){
134322 assert( pIdx->aColExpr!=0 )((void) (0));
134323 assert( pIdx->aColExpr->nExpr>ii )((void) (0));
134324 pExpr = pIdx->aColExpr->a[ii].pExpr;
134325 if( pExpr->op!=TK_COLLATE106 ){
134326 sCol[0].pLeft = pExpr;
134327 pExpr = &sCol[0];
134328 }
134329 }else{
134330 sCol[0].pLeft = &sCol[1];
134331 sCol[1].iColumn = pIdx->aiColumn[ii];
134332 pExpr = &sCol[0];
134333 }
134334 for(jj=0; jj<nn; jj++){
134335 if( sqlite3ExprCompare(pParse, pTarget->a[jj].pExpr, pExpr,iCursor)<2 ){
134336 break; /* Column ii of the index matches column jj of target */
134337 }
134338 }
134339 if( jj>=nn ){
134340 /* The target contains no match for column jj of the index */
134341 break;
134342 }
134343 }
134344 if( ii<nn ){
134345 /* Column ii of the index did not match any term of the conflict target.
134346 ** Continue the search with the next index. */
134347 continue;
134348 }
134349 pUpsert->pUpsertIdx = pIdx;
134350 return SQLITE_OK0;
134351 }
134352 sqlite3ErrorMsg(pParse, "ON CONFLICT clause does not match any "
134353 "PRIMARY KEY or UNIQUE constraint");
134354 return SQLITE_ERROR1;
134355}
134356
134357/*
134358** Generate bytecode that does an UPDATE as part of an upsert.
134359**
134360** If pIdx is NULL, then the UNIQUE constraint that failed was the IPK.
134361** In this case parameter iCur is a cursor open on the table b-tree that
134362** currently points to the conflicting table row. Otherwise, if pIdx
134363** is not NULL, then pIdx is the constraint that failed and iCur is a
134364** cursor points to the conflicting row.
134365*/
134366SQLITE_PRIVATEstatic void sqlite3UpsertDoUpdate(
134367 Parse *pParse, /* The parsing and code-generating context */
134368 Upsert *pUpsert, /* The ON CONFLICT clause for the upsert */
134369 Table *pTab, /* The table being updated */
134370 Index *pIdx, /* The UNIQUE constraint that failed */
134371 int iCur /* Cursor for pIdx (or pTab if pIdx==NULL) */
134372){
134373 Vdbe *v = pParse->pVdbe;
134374 sqlite3 *db = pParse->db;
134375 SrcList *pSrc; /* FROM clause for the UPDATE */
134376 int iDataCur;
134377
134378 assert( v!=0 )((void) (0));
134379 assert( pUpsert!=0 )((void) (0));
134380 VdbeNoopComment((v, "Begin DO UPDATE of UPSERT"));
134381 iDataCur = pUpsert->iDataCur;
134382 if( pIdx && iCur!=iDataCur ){
134383 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
134384 int regRowid = sqlite3GetTempReg(pParse);
134385 sqlite3VdbeAddOp2(v, OP_IdxRowid135, iCur, regRowid);
134386 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iDataCur, 0, regRowid);
134387 VdbeCoverage(v);
134388 sqlite3ReleaseTempReg(pParse, regRowid);
134389 }else{
134390 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
134391 int nPk = pPk->nKeyCol;
134392 int iPk = pParse->nMem+1;
134393 int i;
134394 pParse->nMem += nPk;
134395 for(i=0; i<nPk; i++){
134396 int k;
134397 assert( pPk->aiColumn[i]>=0 )((void) (0));
134398 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
134399 sqlite3VdbeAddOp3(v, OP_Column90, iCur, k, iPk+i);
134400 VdbeComment((v, "%s.%s", pIdx->zName,
134401 pTab->aCol[pPk->aiColumn[i]].zName));
134402 }
134403 sqlite3VdbeVerifyAbortable(v, OE_Abort);
134404 i = sqlite3VdbeAddOp4Int(v, OP_Found29, iDataCur, 0, iPk, nPk);
134405 VdbeCoverage(v);
134406 sqlite3VdbeAddOp4(v, OP_Halt69, SQLITE_CORRUPT11, OE_Abort2, 0,
134407 "corrupt database", P4_STATIC(-1));
134408 sqlite3VdbeJumpHere(v, i);
134409 }
134410 }
134411 /* pUpsert does not own pUpsertSrc - the outer INSERT statement does. So
134412 ** we have to make a copy before passing it down into sqlite3Update() */
134413 pSrc = sqlite3SrcListDup(db, pUpsert->pUpsertSrc, 0);
134414 sqlite3Update(pParse, pSrc, pUpsert->pUpsertSet,
134415 pUpsert->pUpsertWhere, OE_Abort2, 0, 0, pUpsert);
134416 pUpsert->pUpsertSet = 0; /* Will have been deleted by sqlite3Update() */
134417 pUpsert->pUpsertWhere = 0; /* Will have been deleted by sqlite3Update() */
134418 VdbeNoopComment((v, "End DO UPDATE of UPSERT"));
134419}
134420
134421#endif /* SQLITE_OMIT_UPSERT */
134422
134423/************** End of upsert.c **********************************************/
134424/************** Begin file vacuum.c ******************************************/
134425/*
134426** 2003 April 6
134427**
134428** The author disclaims copyright to this source code. In place of
134429** a legal notice, here is a blessing:
134430**
134431** May you do good and not evil.
134432** May you find forgiveness for yourself and forgive others.
134433** May you share freely, never taking more than you give.
134434**
134435*************************************************************************
134436** This file contains code used to implement the VACUUM command.
134437**
134438** Most of the code in this file may be omitted by defining the
134439** SQLITE_OMIT_VACUUM macro.
134440*/
134441/* #include "sqliteInt.h" */
134442/* #include "vdbeInt.h" */
134443
134444#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
134445
134446/*
134447** Execute zSql on database db.
134448**
134449** If zSql returns rows, then each row will have exactly one
134450** column. (This will only happen if zSql begins with "SELECT".)
134451** Take each row of result and call execSql() again recursively.
134452**
134453** The execSqlF() routine does the same thing, except it accepts
134454** a format string as its third argument
134455*/
134456static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
134457 sqlite3_stmt *pStmt;
134458 int rc;
134459
134460 /* printf("SQL: [%s]\n", zSql); fflush(stdout); */
134461 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
134462 if( rc!=SQLITE_OK0 ) return rc;
134463 while( SQLITE_ROW100==(rc = sqlite3_step(pStmt)) ){
134464 const char *zSubSql = (const char*)sqlite3_column_text(pStmt,0);
134465 assert( sqlite3_strnicmp(zSql,"SELECT",6)==0 )((void) (0));
134466 /* The secondary SQL must be one of CREATE TABLE, CREATE INDEX,
134467 ** or INSERT. Historically there have been attacks that first
134468 ** corrupt the sqlite_master.sql field with other kinds of statements
134469 ** then run VACUUM to get those statements to execute at inappropriate
134470 ** times. */
134471 if( zSubSql
134472 && (strncmp(zSubSql,"CRE",3)==0 || strncmp(zSubSql,"INS",3)==0)
134473 ){
134474 rc = execSql(db, pzErrMsg, zSubSql);
134475 if( rc!=SQLITE_OK0 ) break;
134476 }
134477 }
134478 assert( rc!=SQLITE_ROW )((void) (0));
134479 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
134480 if( rc ){
134481 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
134482 }
134483 (void)sqlite3_finalize(pStmt);
134484 return rc;
134485}
134486static int execSqlF(sqlite3 *db, char **pzErrMsg, const char *zSql, ...){
134487 char *z;
134488 va_list ap;
134489 int rc;
134490 va_start(ap, zSql)__builtin_va_start(ap, zSql);
134491 z = sqlite3VMPrintf(db, zSql, ap);
134492 va_end(ap)__builtin_va_end(ap);
134493 if( z==0 ) return SQLITE_NOMEM7;
134494 rc = execSql(db, pzErrMsg, z);
134495 sqlite3DbFree(db, z);
134496 return rc;
134497}
134498
134499/*
134500** The VACUUM command is used to clean up the database,
134501** collapse free space, etc. It is modelled after the VACUUM command
134502** in PostgreSQL. The VACUUM command works as follows:
134503**
134504** (1) Create a new transient database file
134505** (2) Copy all content from the database being vacuumed into
134506** the new transient database file
134507** (3) Copy content from the transient database back into the
134508** original database.
134509**
134510** The transient database requires temporary disk space approximately
134511** equal to the size of the original database. The copy operation of
134512** step (3) requires additional temporary disk space approximately equal
134513** to the size of the original database for the rollback journal.
134514** Hence, temporary disk space that is approximately 2x the size of the
134515** original database is required. Every page of the database is written
134516** approximately 3 times: Once for step (2) and twice for step (3).
134517** Two writes per page are required in step (3) because the original
134518** database content must be written into the rollback journal prior to
134519** overwriting the database with the vacuumed content.
134520**
134521** Only 1x temporary space and only 1x writes would be required if
134522** the copy of step (3) were replaced by deleting the original database
134523** and renaming the transient database as the original. But that will
134524** not work if other processes are attached to the original database.
134525** And a power loss in between deleting the original and renaming the
134526** transient would cause the database file to appear to be deleted
134527** following reboot.
134528*/
134529SQLITE_PRIVATEstatic void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
134530 Vdbe *v = sqlite3GetVdbe(pParse);
134531 int iDb = 0;
134532 if( v==0 ) goto build_vacuum_end;
134533 if( pParse->nErr ) goto build_vacuum_end;
134534 if( pNm ){
134535#ifndef SQLITE_BUG_COMPATIBLE_20160819
134536 /* Default behavior: Report an error if the argument to VACUUM is
134537 ** not recognized */
134538 iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
134539 if( iDb<0 ) goto build_vacuum_end;
134540#else
134541 /* When SQLITE_BUG_COMPATIBLE_20160819 is defined, unrecognized arguments
134542 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
134543 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
134544 ** The buggy behavior is required for binary compatibility with some
134545 ** legacy applications. */
134546 iDb = sqlite3FindDb(pParse->db, pNm);
134547 if( iDb<0 ) iDb = 0;
134548#endif
134549 }
134550 if( iDb!=1 ){
134551 int iIntoReg = 0;
134552 if( pInto && sqlite3ResolveSelfReference(pParse,0,0,pInto,0)==0 ){
134553 iIntoReg = ++pParse->nMem;
134554 sqlite3ExprCode(pParse, pInto, iIntoReg);
134555 }
134556 sqlite3VdbeAddOp2(v, OP_Vacuum8, iDb, iIntoReg);
134557 sqlite3VdbeUsesBtree(v, iDb);
134558 }
134559build_vacuum_end:
134560 sqlite3ExprDelete(pParse->db, pInto);
134561 return;
134562}
134563
134564/*
134565** This routine implements the OP_Vacuum opcode of the VDBE.
134566*/
134567SQLITE_PRIVATEstatic SQLITE_NOINLINE__attribute__((noinline)) int sqlite3RunVacuum(
134568 char **pzErrMsg, /* Write error message here */
134569 sqlite3 *db, /* Database connection */
134570 int iDb, /* Which attached DB to vacuum */
134571 sqlite3_value *pOut /* Write results here, if not NULL. VACUUM INTO */
134572){
134573 int rc = SQLITE_OK0; /* Return code from service routines */
134574 Btree *pMain; /* The database being vacuumed */
134575 Btree *pTemp; /* The temporary database we vacuum into */
134576 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
134577 u64 saved_flags; /* Saved value of db->flags */
134578 int saved_nChange; /* Saved value of db->nChange */
134579 int saved_nTotalChange; /* Saved value of db->nTotalChange */
134580 u32 saved_openFlags; /* Saved value of db->openFlags */
134581 u8 saved_mTrace; /* Saved trace settings */
134582 Db *pDb = 0; /* Database to detach at end of vacuum */
134583 int isMemDb; /* True if vacuuming a :memory: database */
134584 int nRes; /* Bytes of reserved space at the end of each page */
134585 int nDb; /* Number of attached databases */
134586 const char *zDbMain; /* Schema name of database to vacuum */
134587 const char *zOut; /* Name of output file */
134588
134589 if( !db->autoCommit ){
134590 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
134591 return SQLITE_ERROR1; /* IMP: R-12218-18073 */
134592 }
134593 if( db->nVdbeActive>1 ){
134594 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
134595 return SQLITE_ERROR1; /* IMP: R-15610-35227 */
134596 }
134597 saved_openFlags = db->openFlags;
134598 if( pOut ){
134599 if( sqlite3_value_type(pOut)!=SQLITE_TEXT3 ){
134600 sqlite3SetString(pzErrMsg, db, "non-text filename");
134601 return SQLITE_ERROR1;
134602 }
134603 zOut = (const char*)sqlite3_value_text(pOut);
134604 db->openFlags &= ~SQLITE_OPEN_READONLY0x00000001;
134605 db->openFlags |= SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_READWRITE0x00000002;
134606 }else{
134607 zOut = "";
134608 }
134609
134610 /* Save the current value of the database flags so that it can be
134611 ** restored before returning. Then set the writable-schema flag, and
134612 ** disable CHECK and foreign key constraints. */
134613 saved_flags = db->flags;
134614 saved_mDbFlags = db->mDbFlags;
134615 saved_nChange = db->nChange;
134616 saved_nTotalChange = db->nTotalChange;
134617 saved_mTrace = db->mTrace;
134618 db->flags |= SQLITE_WriteSchema0x00000001 | SQLITE_IgnoreChecks0x00000200;
134619 db->mDbFlags |= DBFLAG_PreferBuiltin0x0002 | DBFLAG_Vacuum0x0004;
134620 db->flags &= ~(u64)(SQLITE_ForeignKeys0x00004000 | SQLITE_ReverseOrder0x00001000
134621 | SQLITE_Defensive0x10000000 | SQLITE_CountRows0x00000080);
134622 db->mTrace = 0;
134623
134624 zDbMain = db->aDb[iDb].zDbSName;
134625 pMain = db->aDb[iDb].pBt;
134626 isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
134627
134628 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
134629 ** can be set to 'off' for this file, as it is not recovered if a crash
134630 ** occurs anyway. The integrity of the database is maintained by a
134631 ** (possibly synchronous) transaction opened on the main database before
134632 ** sqlite3BtreeCopyFile() is called.
134633 **
134634 ** An optimisation would be to use a non-journaled pager.
134635 ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
134636 ** that actually made the VACUUM run slower. Very little journalling
134637 ** actually occurs when doing a vacuum since the vacuum_db is initially
134638 ** empty. Only the journal header is written. Apparently it takes more
134639 ** time to parse and run the PRAGMA to turn journalling off than it does
134640 ** to write the journal header file.
134641 */
134642 nDb = db->nDb;
134643 rc = execSqlF(db, pzErrMsg, "ATTACH %Q AS vacuum_db", zOut);
134644 db->openFlags = saved_openFlags;
134645 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
134646 assert( (db->nDb-1)==nDb )((void) (0));
134647 pDb = &db->aDb[nDb];
134648 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 )((void) (0));
134649 pTemp = pDb->pBt;
134650 if( pOut ){
134651 sqlite3_file *id = sqlite3PagerFile(sqlite3BtreePager(pTemp));
134652 i64 sz = 0;
134653 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK0 || sz>0) ){
134654 rc = SQLITE_ERROR1;
134655 sqlite3SetString(pzErrMsg, db, "output file already exists");
134656 goto end_of_vacuum;
134657 }
134658 db->mDbFlags |= DBFLAG_VacuumInto0x0008;
134659 }
134660 nRes = sqlite3BtreeGetOptimalReserve(pMain);
134661
134662 /* A VACUUM cannot change the pagesize of an encrypted database. */
134663#ifdef SQLITE_HAS_CODEC
134664 if( db->nextPagesize ){
134665 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
134666 int nKey;
134667 char *zKey;
134668 sqlite3CodecGetKey(db, iDb, (void**)&zKey, &nKey);
134669 if( nKey ) db->nextPagesize = 0;
134670 }
134671#endif
134672
134673 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
134674 sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0));
134675 sqlite3BtreeSetPagerFlags(pTemp, PAGER_SYNCHRONOUS_OFF0x01|PAGER_CACHESPILL0x20);
134676
134677 /* Begin a transaction and take an exclusive lock on the main database
134678 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
134679 ** to ensure that we do not try to change the page-size on a WAL database.
134680 */
134681 rc = execSql(db, pzErrMsg, "BEGIN");
134682 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
134683 rc = sqlite3BtreeBeginTrans(pMain, pOut==0 ? 2 : 0, 0);
134684 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
134685
134686 /* Do not attempt to change the page size for a WAL database */
134687 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
134688 ==PAGER_JOURNALMODE_WAL5 ){
134689 db->nextPagesize = 0;
134690 }
134691
134692 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
134693 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
134694 || NEVER(db->mallocFailed)(db->mallocFailed)
134695 ){
134696 rc = SQLITE_NOMEM_BKPT7;
134697 goto end_of_vacuum;
134698 }
134699
134700#ifndef SQLITE_OMIT_AUTOVACUUM
134701 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
134702 sqlite3BtreeGetAutoVacuum(pMain));
134703#endif
134704
134705 /* Query the schema of the main database. Create a mirror schema
134706 ** in the temporary database.
134707 */
134708 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
134709 rc = execSqlF(db, pzErrMsg,
134710 "SELECT sql FROM \"%w\".sqlite_master"
134711 " WHERE type='table'AND name<>'sqlite_sequence'"
134712 " AND coalesce(rootpage,1)>0",
134713 zDbMain
134714 );
134715 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
134716 rc = execSqlF(db, pzErrMsg,
134717 "SELECT sql FROM \"%w\".sqlite_master"
134718 " WHERE type='index'",
134719 zDbMain
134720 );
134721 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
134722 db->init.iDb = 0;
134723
134724 /* Loop through the tables in the main database. For each, do
134725 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
134726 ** the contents to the temporary database.
134727 */
134728 rc = execSqlF(db, pzErrMsg,
134729 "SELECT'INSERT INTO vacuum_db.'||quote(name)"
134730 "||' SELECT*FROM\"%w\".'||quote(name)"
134731 "FROM vacuum_db.sqlite_master "
134732 "WHERE type='table'AND coalesce(rootpage,1)>0",
134733 zDbMain
134734 );
134735 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 )((void) (0));
134736 db->mDbFlags &= ~DBFLAG_Vacuum0x0004;
134737 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
134738
134739 /* Copy the triggers, views, and virtual tables from the main database
134740 ** over to the temporary database. None of these objects has any
134741 ** associated storage, so all we have to do is copy their entries
134742 ** from the SQLITE_MASTER table.
134743 */
134744 rc = execSqlF(db, pzErrMsg,
134745 "INSERT INTO vacuum_db.sqlite_master"
134746 " SELECT*FROM \"%w\".sqlite_master"
134747 " WHERE type IN('view','trigger')"
134748 " OR(type='table'AND rootpage=0)",
134749 zDbMain
134750 );
134751 if( rc ) goto end_of_vacuum;
134752
134753 /* At this point, there is a write transaction open on both the
134754 ** vacuum database and the main database. Assuming no error occurs,
134755 ** both transactions are closed by this block - the main database
134756 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
134757 ** call to sqlite3BtreeCommit().
134758 */
134759 {
134760 u32 meta;
134761 int i;
134762
134763 /* This array determines which meta meta values are preserved in the
134764 ** vacuum. Even entries are the meta value number and odd entries
134765 ** are an increment to apply to the meta value after the vacuum.
134766 ** The increment is used to increase the schema cookie so that other
134767 ** connections to the same database will know to reread the schema.
134768 */
134769 static const unsigned char aCopy[] = {
134770 BTREE_SCHEMA_VERSION1, 1, /* Add one to the old schema cookie */
134771 BTREE_DEFAULT_CACHE_SIZE3, 0, /* Preserve the default page cache size */
134772 BTREE_TEXT_ENCODING5, 0, /* Preserve the text encoding */
134773 BTREE_USER_VERSION6, 0, /* Preserve the user version */
134774 BTREE_APPLICATION_ID8, 0, /* Preserve the application id */
134775 };
134776
134777 assert( 1==sqlite3BtreeIsInTrans(pTemp) )((void) (0));
134778 assert( pOut!=0 || 1==sqlite3BtreeIsInTrans(pMain) )((void) (0));
134779
134780 /* Copy Btree meta values */
134781 for(i=0; i<ArraySize(aCopy)((int)(sizeof(aCopy)/sizeof(aCopy[0]))); i+=2){
134782 /* GetMeta() and UpdateMeta() cannot fail in this context because
134783 ** we already have page 1 loaded into cache and marked dirty. */
134784 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
134785 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
134786 if( NEVER(rc!=SQLITE_OK)(rc!=0) ) goto end_of_vacuum;
134787 }
134788
134789 if( pOut==0 ){
134790 rc = sqlite3BtreeCopyFile(pMain, pTemp);
134791 }
134792 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
134793 rc = sqlite3BtreeCommit(pTemp);
134794 if( rc!=SQLITE_OK0 ) goto end_of_vacuum;
134795#ifndef SQLITE_OMIT_AUTOVACUUM
134796 if( pOut==0 ){
134797 sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
134798 }
134799#endif
134800 }
134801
134802 assert( rc==SQLITE_OK )((void) (0));
134803 if( pOut==0 ){
134804 rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
134805 }
134806
134807end_of_vacuum:
134808 /* Restore the original value of db->flags */
134809 db->init.iDb = 0;
134810 db->mDbFlags = saved_mDbFlags;
134811 db->flags = saved_flags;
134812 db->nChange = saved_nChange;
134813 db->nTotalChange = saved_nTotalChange;
134814 db->mTrace = saved_mTrace;
134815 sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
134816
134817 /* Currently there is an SQL level transaction open on the vacuum
134818 ** database. No locks are held on any other files (since the main file
134819 ** was committed at the btree level). So it safe to end the transaction
134820 ** by manually setting the autoCommit flag to true and detaching the
134821 ** vacuum database. The vacuum_db journal file is deleted when the pager
134822 ** is closed by the DETACH.
134823 */
134824 db->autoCommit = 1;
134825
134826 if( pDb ){
134827 sqlite3BtreeClose(pDb->pBt);
134828 pDb->pBt = 0;
134829 pDb->pSchema = 0;
134830 }
134831
134832 /* This both clears the schemas and reduces the size of the db->aDb[]
134833 ** array. */
134834 sqlite3ResetAllSchemasOfConnection(db);
134835
134836 return rc;
134837}
134838
134839#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
134840
134841/************** End of vacuum.c **********************************************/
134842/************** Begin file vtab.c ********************************************/
134843/*
134844** 2006 June 10
134845**
134846** The author disclaims copyright to this source code. In place of
134847** a legal notice, here is a blessing:
134848**
134849** May you do good and not evil.
134850** May you find forgiveness for yourself and forgive others.
134851** May you share freely, never taking more than you give.
134852**
134853*************************************************************************
134854** This file contains code used to help implement virtual tables.
134855*/
134856#ifndef SQLITE_OMIT_VIRTUALTABLE
134857/* #include "sqliteInt.h" */
134858
134859/*
134860** Before a virtual table xCreate() or xConnect() method is invoked, the
134861** sqlite3.pVtabCtx member variable is set to point to an instance of
134862** this struct allocated on the stack. It is used by the implementation of
134863** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
134864** are invoked only from within xCreate and xConnect methods.
134865*/
134866struct VtabCtx {
134867 VTable *pVTable; /* The virtual table being constructed */
134868 Table *pTab; /* The Table object to which the virtual table belongs */
134869 VtabCtx *pPrior; /* Parent context (if any) */
134870 int bDeclared; /* True after sqlite3_declare_vtab() is called */
134871};
134872
134873/*
134874** Construct and install a Module object for a virtual table. When this
134875** routine is called, it is guaranteed that all appropriate locks are held
134876** and the module is not already part of the connection.
134877*/
134878SQLITE_PRIVATEstatic Module *sqlite3VtabCreateModule(
134879 sqlite3 *db, /* Database in which module is registered */
134880 const char *zName, /* Name assigned to this module */
134881 const sqlite3_module *pModule, /* The definition of the module */
134882 void *pAux, /* Context pointer for xCreate/xConnect */
134883 void (*xDestroy)(void *) /* Module destructor function */
134884){
134885 Module *pMod;
134886 int nName = sqlite3Strlen30(zName);
134887 pMod = (Module *)sqlite3Malloc(sizeof(Module) + nName + 1);
134888 if( pMod==0 ){
134889 sqlite3OomFault(db);
134890 }else{
134891 Module *pDel;
134892 char *zCopy = (char *)(&pMod[1]);
134893 memcpy(zCopy, zName, nName+1);
134894 pMod->zName = zCopy;
134895 pMod->pModule = pModule;
134896 pMod->pAux = pAux;
134897 pMod->xDestroy = xDestroy;
134898 pMod->pEpoTab = 0;
134899 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
134900 assert( pDel==0 || pDel==pMod )((void) (0));
134901 if( pDel ){
134902 sqlite3OomFault(db);
134903 sqlite3DbFree(db, pDel);
134904 pMod = 0;
134905 }
134906 }
134907 return pMod;
134908}
134909
134910/*
134911** The actual function that does the work of creating a new module.
134912** This function implements the sqlite3_create_module() and
134913** sqlite3_create_module_v2() interfaces.
134914*/
134915static int createModule(
134916 sqlite3 *db, /* Database in which module is registered */
134917 const char *zName, /* Name assigned to this module */
134918 const sqlite3_module *pModule, /* The definition of the module */
134919 void *pAux, /* Context pointer for xCreate/xConnect */
134920 void (*xDestroy)(void *) /* Module destructor function */
134921){
134922 int rc = SQLITE_OK0;
134923
134924 sqlite3_mutex_enter(db->mutex);
134925 if( sqlite3HashFind(&db->aModule, zName) ){
134926 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(134926);
134927 }else{
134928 (void)sqlite3VtabCreateModule(db, zName, pModule, pAux, xDestroy);
134929 }
134930 rc = sqlite3ApiExit(db, rc);
134931 if( rc!=SQLITE_OK0 && xDestroy ) xDestroy(pAux);
134932 sqlite3_mutex_leave(db->mutex);
134933 return rc;
134934}
134935
134936
134937/*
134938** External API function used to create a new virtual-table module.
134939*/
134940SQLITE_API int sqlite3_create_module(
134941 sqlite3 *db, /* Database in which module is registered */
134942 const char *zName, /* Name assigned to this module */
134943 const sqlite3_module *pModule, /* The definition of the module */
134944 void *pAux /* Context pointer for xCreate/xConnect */
134945){
134946#ifdef SQLITE_ENABLE_API_ARMOR
134947 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(134947);
134948#endif
134949 return createModule(db, zName, pModule, pAux, 0);
134950}
134951
134952/*
134953** External API function used to create a new virtual-table module.
134954*/
134955SQLITE_API int sqlite3_create_module_v2(
134956 sqlite3 *db, /* Database in which module is registered */
134957 const char *zName, /* Name assigned to this module */
134958 const sqlite3_module *pModule, /* The definition of the module */
134959 void *pAux, /* Context pointer for xCreate/xConnect */
134960 void (*xDestroy)(void *) /* Module destructor function */
134961){
134962#ifdef SQLITE_ENABLE_API_ARMOR
134963 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(134963);
134964#endif
134965 return createModule(db, zName, pModule, pAux, xDestroy);
134966}
134967
134968/*
134969** Lock the virtual table so that it cannot be disconnected.
134970** Locks nest. Every lock should have a corresponding unlock.
134971** If an unlock is omitted, resources leaks will occur.
134972**
134973** If a disconnect is attempted while a virtual table is locked,
134974** the disconnect is deferred until all locks have been removed.
134975*/
134976SQLITE_PRIVATEstatic void sqlite3VtabLock(VTable *pVTab){
134977 pVTab->nRef++;
134978}
134979
134980
134981/*
134982** pTab is a pointer to a Table structure representing a virtual-table.
134983** Return a pointer to the VTable object used by connection db to access
134984** this virtual-table, if one has been created, or NULL otherwise.
134985*/
134986SQLITE_PRIVATEstatic VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
134987 VTable *pVtab;
134988 assert( IsVirtual(pTab) )((void) (0));
134989 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
134990 return pVtab;
134991}
134992
134993/*
134994** Decrement the ref-count on a virtual table object. When the ref-count
134995** reaches zero, call the xDisconnect() method to delete the object.
134996*/
134997SQLITE_PRIVATEstatic void sqlite3VtabUnlock(VTable *pVTab){
134998 sqlite3 *db = pVTab->db;
134999
135000 assert( db )((void) (0));
135001 assert( pVTab->nRef>0 )((void) (0));
135002 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE )((void) (0));
135003
135004 pVTab->nRef--;
135005 if( pVTab->nRef==0 ){
135006 sqlite3_vtab *p = pVTab->pVtab;
135007 if( p ){
135008 p->pModule->xDisconnect(p);
135009 }
135010 sqlite3DbFree(db, pVTab);
135011 }
135012}
135013
135014/*
135015** Table p is a virtual table. This function moves all elements in the
135016** p->pVTable list to the sqlite3.pDisconnect lists of their associated
135017** database connections to be disconnected at the next opportunity.
135018** Except, if argument db is not NULL, then the entry associated with
135019** connection db is left in the p->pVTable list.
135020*/
135021static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
135022 VTable *pRet = 0;
135023 VTable *pVTable = p->pVTable;
135024 p->pVTable = 0;
135025
135026 /* Assert that the mutex (if any) associated with the BtShared database
135027 ** that contains table p is held by the caller. See header comments
135028 ** above function sqlite3VtabUnlockList() for an explanation of why
135029 ** this makes it safe to access the sqlite3.pDisconnect list of any
135030 ** database connection that may have an entry in the p->pVTable list.
135031 */
135032 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) )((void) (0));
135033
135034 while( pVTable ){
135035 sqlite3 *db2 = pVTable->db;
135036 VTable *pNext = pVTable->pNext;
135037 assert( db2 )((void) (0));
135038 if( db2==db ){
135039 pRet = pVTable;
135040 p->pVTable = pRet;
135041 pRet->pNext = 0;
135042 }else{
135043 pVTable->pNext = db2->pDisconnect;
135044 db2->pDisconnect = pVTable;
135045 }
135046 pVTable = pNext;
135047 }
135048
135049 assert( !db || pRet )((void) (0));
135050 return pRet;
135051}
135052
135053/*
135054** Table *p is a virtual table. This function removes the VTable object
135055** for table *p associated with database connection db from the linked
135056** list in p->pVTab. It also decrements the VTable ref count. This is
135057** used when closing database connection db to free all of its VTable
135058** objects without disturbing the rest of the Schema object (which may
135059** be being used by other shared-cache connections).
135060*/
135061SQLITE_PRIVATEstatic void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
135062 VTable **ppVTab;
135063
135064 assert( IsVirtual(p) )((void) (0));
135065 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
135066 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
135067
135068 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
135069 if( (*ppVTab)->db==db ){
135070 VTable *pVTab = *ppVTab;
135071 *ppVTab = pVTab->pNext;
135072 sqlite3VtabUnlock(pVTab);
135073 break;
135074 }
135075 }
135076}
135077
135078
135079/*
135080** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
135081**
135082** This function may only be called when the mutexes associated with all
135083** shared b-tree databases opened using connection db are held by the
135084** caller. This is done to protect the sqlite3.pDisconnect list. The
135085** sqlite3.pDisconnect list is accessed only as follows:
135086**
135087** 1) By this function. In this case, all BtShared mutexes and the mutex
135088** associated with the database handle itself must be held.
135089**
135090** 2) By function vtabDisconnectAll(), when it adds a VTable entry to
135091** the sqlite3.pDisconnect list. In this case either the BtShared mutex
135092** associated with the database the virtual table is stored in is held
135093** or, if the virtual table is stored in a non-sharable database, then
135094** the database handle mutex is held.
135095**
135096** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
135097** by multiple threads. It is thread-safe.
135098*/
135099SQLITE_PRIVATEstatic void sqlite3VtabUnlockList(sqlite3 *db){
135100 VTable *p = db->pDisconnect;
135101 db->pDisconnect = 0;
135102
135103 assert( sqlite3BtreeHoldsAllMutexes(db) )((void) (0));
135104 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
135105
135106 if( p ){
135107 sqlite3ExpirePreparedStatements(db, 0);
135108 do {
135109 VTable *pNext = p->pNext;
135110 sqlite3VtabUnlock(p);
135111 p = pNext;
135112 }while( p );
135113 }
135114}
135115
135116/*
135117** Clear any and all virtual-table information from the Table record.
135118** This routine is called, for example, just before deleting the Table
135119** record.
135120**
135121** Since it is a virtual-table, the Table structure contains a pointer
135122** to the head of a linked list of VTable structures. Each VTable
135123** structure is associated with a single sqlite3* user of the schema.
135124** The reference count of the VTable structure associated with database
135125** connection db is decremented immediately (which may lead to the
135126** structure being xDisconnected and free). Any other VTable structures
135127** in the list are moved to the sqlite3.pDisconnect list of the associated
135128** database connection.
135129*/
135130SQLITE_PRIVATEstatic void sqlite3VtabClear(sqlite3 *db, Table *p){
135131 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
135132 if( p->azModuleArg ){
135133 int i;
135134 for(i=0; i<p->nModuleArg; i++){
135135 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
135136 }
135137 sqlite3DbFree(db, p->azModuleArg);
135138 }
135139}
135140
135141/*
135142** Add a new module argument to pTable->azModuleArg[].
135143** The string is not copied - the pointer is stored. The
135144** string will be freed automatically when the table is
135145** deleted.
135146*/
135147static void addModuleArgument(Parse *pParse, Table *pTable, char *zArg){
135148 sqlite3_int64 nBytes = sizeof(char *)*(2+pTable->nModuleArg);
135149 char **azModuleArg;
135150 sqlite3 *db = pParse->db;
135151 if( pTable->nModuleArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN2] ){
135152 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
135153 }
135154 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
135155 if( azModuleArg==0 ){
135156 sqlite3DbFree(db, zArg);
135157 }else{
135158 int i = pTable->nModuleArg++;
135159 azModuleArg[i] = zArg;
135160 azModuleArg[i+1] = 0;
135161 pTable->azModuleArg = azModuleArg;
135162 }
135163}
135164
135165/*
135166** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
135167** statement. The module name has been parsed, but the optional list
135168** of parameters that follow the module name are still pending.
135169*/
135170SQLITE_PRIVATEstatic void sqlite3VtabBeginParse(
135171 Parse *pParse, /* Parsing context */
135172 Token *pName1, /* Name of new table, or database name */
135173 Token *pName2, /* Name of new table or NULL */
135174 Token *pModuleName, /* Name of the module for the virtual table */
135175 int ifNotExists /* No error if the table already exists */
135176){
135177 Table *pTable; /* The new virtual table */
135178 sqlite3 *db; /* Database connection */
135179
135180 sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
135181 pTable = pParse->pNewTable;
135182 if( pTable==0 ) return;
135183 assert( 0==pTable->pIndex )((void) (0));
135184
135185 db = pParse->db;
135186
135187 assert( pTable->nModuleArg==0 )((void) (0));
135188 addModuleArgument(pParse, pTable, sqlite3NameFromToken(db, pModuleName));
135189 addModuleArgument(pParse, pTable, 0);
135190 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
135191 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)((void) (0))
135192 || (pParse->sNameToken.z==pName1->z && pName2->z==0)((void) (0))
135193 )((void) (0));
135194 pParse->sNameToken.n = (int)(
135195 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
135196 );
135197
135198#ifndef SQLITE_OMIT_AUTHORIZATION
135199 /* Creating a virtual table invokes the authorization callback twice.
135200 ** The first invocation, to obtain permission to INSERT a row into the
135201 ** sqlite_master table, has already been made by sqlite3StartTable().
135202 ** The second call, to obtain permission to create the table, is made now.
135203 */
135204 if( pTable->azModuleArg ){
135205 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
135206 assert( iDb>=0 )((void) (0)); /* The database the table is being created in */
135207 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE29, pTable->zName,
135208 pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
135209 }
135210#endif
135211}
135212
135213/*
135214** This routine takes the module argument that has been accumulating
135215** in pParse->zArg[] and appends it to the list of arguments on the
135216** virtual table currently under construction in pParse->pTable.
135217*/
135218static void addArgumentToVtab(Parse *pParse){
135219 if( pParse->sArg.z && pParse->pNewTable ){
135220 const char *z = (const char*)pParse->sArg.z;
135221 int n = pParse->sArg.n;
135222 sqlite3 *db = pParse->db;
135223 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
135224 }
135225}
135226
135227/*
135228** The parser calls this routine after the CREATE VIRTUAL TABLE statement
135229** has been completely parsed.
135230*/
135231SQLITE_PRIVATEstatic void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
135232 Table *pTab = pParse->pNewTable; /* The table being constructed */
135233 sqlite3 *db = pParse->db; /* The database connection */
135234
135235 if( pTab==0 ) return;
135236 addArgumentToVtab(pParse);
135237 pParse->sArg.z = 0;
135238 if( pTab->nModuleArg<1 ) return;
135239
135240 /* If the CREATE VIRTUAL TABLE statement is being entered for the
135241 ** first time (in other words if the virtual table is actually being
135242 ** created now instead of just being read out of sqlite_master) then
135243 ** do additional initialization work and store the statement text
135244 ** in the sqlite_master table.
135245 */
135246 if( !db->init.busy ){
135247 char *zStmt;
135248 char *zWhere;
135249 int iDb;
135250 int iReg;
135251 Vdbe *v;
135252
135253 /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
135254 if( pEnd ){
135255 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
135256 }
135257 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
135258
135259 /* A slot for the record has already been allocated in the
135260 ** SQLITE_MASTER table. We just need to update that slot with all
135261 ** the information we've collected.
135262 **
135263 ** The VM register number pParse->regRowid holds the rowid of an
135264 ** entry in the sqlite_master table tht was created for this vtab
135265 ** by sqlite3StartTable().
135266 */
135267 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
135268 sqlite3NestedParse(pParse,
135269 "UPDATE %Q.%s "
135270 "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
135271 "WHERE rowid=#%d",
135272 db->aDb[iDb].zDbSName, MASTER_NAME"sqlite_master",
135273 pTab->zName,
135274 pTab->zName,
135275 zStmt,
135276 pParse->regRowid
135277 );
135278 sqlite3DbFree(db, zStmt);
135279 v = sqlite3GetVdbe(pParse);
135280 sqlite3ChangeCookie(pParse, iDb);
135281
135282 sqlite3VdbeAddOp0(v, OP_Expire158);
135283 zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
135284 sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
135285
135286 iReg = ++pParse->nMem;
135287 sqlite3VdbeLoadString(v, iReg, pTab->zName);
135288 sqlite3VdbeAddOp2(v, OP_VCreate161, iDb, iReg);
135289 }
135290
135291 /* If we are rereading the sqlite_master table create the in-memory
135292 ** record of the table. The xConnect() method is not called until
135293 ** the first time the virtual table is used in an SQL statement. This
135294 ** allows a schema that contains virtual tables to be loaded before
135295 ** the required virtual table implementations are registered. */
135296 else {
135297 Table *pOld;
135298 Schema *pSchema = pTab->pSchema;
135299 const char *zName = pTab->zName;
135300 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) )((void) (0));
135301 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
135302 if( pOld ){
135303 sqlite3OomFault(db);
135304 assert( pTab==pOld )((void) (0)); /* Malloc must have failed inside HashInsert() */
135305 return;
135306 }
135307 pParse->pNewTable = 0;
135308 }
135309}
135310
135311/*
135312** The parser calls this routine when it sees the first token
135313** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
135314*/
135315SQLITE_PRIVATEstatic void sqlite3VtabArgInit(Parse *pParse){
135316 addArgumentToVtab(pParse);
135317 pParse->sArg.z = 0;
135318 pParse->sArg.n = 0;
135319}
135320
135321/*
135322** The parser calls this routine for each token after the first token
135323** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
135324*/
135325SQLITE_PRIVATEstatic void sqlite3VtabArgExtend(Parse *pParse, Token *p){
135326 Token *pArg = &pParse->sArg;
135327 if( pArg->z==0 ){
135328 pArg->z = p->z;
135329 pArg->n = p->n;
135330 }else{
135331 assert(pArg->z <= p->z)((void) (0));
135332 pArg->n = (int)(&p->z[p->n] - pArg->z);
135333 }
135334}
135335
135336/*
135337** Invoke a virtual table constructor (either xCreate or xConnect). The
135338** pointer to the function to invoke is passed as the fourth parameter
135339** to this procedure.
135340*/
135341static int vtabCallConstructor(
135342 sqlite3 *db,
135343 Table *pTab,
135344 Module *pMod,
135345 int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
135346 char **pzErr
135347){
135348 VtabCtx sCtx;
135349 VTable *pVTable;
135350 int rc;
135351 const char *const*azArg = (const char *const*)pTab->azModuleArg;
135352 int nArg = pTab->nModuleArg;
135353 char *zErr = 0;
135354 char *zModuleName;
135355 int iDb;
135356 VtabCtx *pCtx;
135357
135358 /* Check that the virtual-table is not already being initialized */
135359 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
135360 if( pCtx->pTab==pTab ){
135361 *pzErr = sqlite3MPrintf(db,
135362 "vtable constructor called recursively: %s", pTab->zName
135363 );
135364 return SQLITE_LOCKED6;
135365 }
135366 }
135367
135368 zModuleName = sqlite3DbStrDup(db, pTab->zName);
135369 if( !zModuleName ){
135370 return SQLITE_NOMEM_BKPT7;
135371 }
135372
135373 pVTable = sqlite3MallocZero(sizeof(VTable));
135374 if( !pVTable ){
135375 sqlite3OomFault(db);
135376 sqlite3DbFree(db, zModuleName);
135377 return SQLITE_NOMEM_BKPT7;
135378 }
135379 pVTable->db = db;
135380 pVTable->pMod = pMod;
135381
135382 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
135383 pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
135384
135385 /* Invoke the virtual table constructor */
135386 assert( &db->pVtabCtx )((void) (0));
135387 assert( xConstruct )((void) (0));
135388 sCtx.pTab = pTab;
135389 sCtx.pVTable = pVTable;
135390 sCtx.pPrior = db->pVtabCtx;
135391 sCtx.bDeclared = 0;
135392 db->pVtabCtx = &sCtx;
135393 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
135394 db->pVtabCtx = sCtx.pPrior;
135395 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
135396 assert( sCtx.pTab==pTab )((void) (0));
135397
135398 if( SQLITE_OK0!=rc ){
135399 if( zErr==0 ){
135400 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
135401 }else {
135402 *pzErr = sqlite3MPrintf(db, "%s", zErr);
135403 sqlite3_free(zErr);
135404 }
135405 sqlite3DbFree(db, pVTable);
135406 }else if( ALWAYS(pVTable->pVtab)(pVTable->pVtab) ){
135407 /* Justification of ALWAYS(): A correct vtab constructor must allocate
135408 ** the sqlite3_vtab object if successful. */
135409 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
135410 pVTable->pVtab->pModule = pMod->pModule;
135411 pVTable->nRef = 1;
135412 if( sCtx.bDeclared==0 ){
135413 const char *zFormat = "vtable constructor did not declare schema: %s";
135414 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
135415 sqlite3VtabUnlock(pVTable);
135416 rc = SQLITE_ERROR1;
135417 }else{
135418 int iCol;
135419 u8 oooHidden = 0;
135420 /* If everything went according to plan, link the new VTable structure
135421 ** into the linked list headed by pTab->pVTable. Then loop through the
135422 ** columns of the table to see if any of them contain the token "hidden".
135423 ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
135424 ** the type string. */
135425 pVTable->pNext = pTab->pVTable;
135426 pTab->pVTable = pVTable;
135427
135428 for(iCol=0; iCol<pTab->nCol; iCol++){
135429 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
135430 int nType;
135431 int i = 0;
135432 nType = sqlite3Strlen30(zType);
135433 for(i=0; i<nType; i++){
135434 if( 0==sqlite3StrNICmpsqlite3_strnicmp("hidden", &zType[i], 6)
135435 && (i==0 || zType[i-1]==' ')
135436 && (zType[i+6]=='\0' || zType[i+6]==' ')
135437 ){
135438 break;
135439 }
135440 }
135441 if( i<nType ){
135442 int j;
135443 int nDel = 6 + (zType[i+6] ? 1 : 0);
135444 for(j=i; (j+nDel)<=nType; j++){
135445 zType[j] = zType[j+nDel];
135446 }
135447 if( zType[i]=='\0' && i>0 ){
135448 assert(zType[i-1]==' ')((void) (0));
135449 zType[i-1] = '\0';
135450 }
135451 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN0x0002;
135452 oooHidden = TF_OOOHidden0x0080;
135453 }else{
135454 pTab->tabFlags |= oooHidden;
135455 }
135456 }
135457 }
135458 }
135459
135460 sqlite3DbFree(db, zModuleName);
135461 return rc;
135462}
135463
135464/*
135465** This function is invoked by the parser to call the xConnect() method
135466** of the virtual table pTab. If an error occurs, an error code is returned
135467** and an error left in pParse.
135468**
135469** This call is a no-op if table pTab is not a virtual table.
135470*/
135471SQLITE_PRIVATEstatic int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
135472 sqlite3 *db = pParse->db;
135473 const char *zMod;
135474 Module *pMod;
135475 int rc;
135476
135477 assert( pTab )((void) (0));
135478 if( !IsVirtual(pTab)((pTab)->nModuleArg) || sqlite3GetVTable(db, pTab) ){
135479 return SQLITE_OK0;
135480 }
135481
135482 /* Locate the required virtual table module */
135483 zMod = pTab->azModuleArg[0];
135484 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
135485
135486 if( !pMod ){
135487 const char *zModule = pTab->azModuleArg[0];
135488 sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
135489 rc = SQLITE_ERROR1;
135490 }else{
135491 char *zErr = 0;
135492 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
135493 if( rc!=SQLITE_OK0 ){
135494 sqlite3ErrorMsg(pParse, "%s", zErr);
135495 pParse->rc = rc;
135496 }
135497 sqlite3DbFree(db, zErr);
135498 }
135499
135500 return rc;
135501}
135502/*
135503** Grow the db->aVTrans[] array so that there is room for at least one
135504** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
135505*/
135506static int growVTrans(sqlite3 *db){
135507 const int ARRAY_INCR = 5;
135508
135509 /* Grow the sqlite3.aVTrans array if required */
135510 if( (db->nVTrans%ARRAY_INCR)==0 ){
135511 VTable **aVTrans;
135512 sqlite3_int64 nBytes = sizeof(sqlite3_vtab*)*
135513 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
135514 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
135515 if( !aVTrans ){
135516 return SQLITE_NOMEM_BKPT7;
135517 }
135518 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
135519 db->aVTrans = aVTrans;
135520 }
135521
135522 return SQLITE_OK0;
135523}
135524
135525/*
135526** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
135527** have already been reserved using growVTrans().
135528*/
135529static void addToVTrans(sqlite3 *db, VTable *pVTab){
135530 /* Add pVtab to the end of sqlite3.aVTrans */
135531 db->aVTrans[db->nVTrans++] = pVTab;
135532 sqlite3VtabLock(pVTab);
135533}
135534
135535/*
135536** This function is invoked by the vdbe to call the xCreate method
135537** of the virtual table named zTab in database iDb.
135538**
135539** If an error occurs, *pzErr is set to point to an English language
135540** description of the error and an SQLITE_XXX error code is returned.
135541** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
135542*/
135543SQLITE_PRIVATEstatic int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
135544 int rc = SQLITE_OK0;
135545 Table *pTab;
135546 Module *pMod;
135547 const char *zMod;
135548
135549 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
135550 assert( pTab && IsVirtual(pTab) && !pTab->pVTable )((void) (0));
135551
135552 /* Locate the required virtual table module */
135553 zMod = pTab->azModuleArg[0];
135554 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
135555
135556 /* If the module has been registered and includes a Create method,
135557 ** invoke it now. If the module has not been registered, return an
135558 ** error. Otherwise, do nothing.
135559 */
135560 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
135561 *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
135562 rc = SQLITE_ERROR1;
135563 }else{
135564 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
135565 }
135566
135567 /* Justification of ALWAYS(): The xConstructor method is required to
135568 ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
135569 if( rc==SQLITE_OK0 && ALWAYS(sqlite3GetVTable(db, pTab))(sqlite3GetVTable(db, pTab)) ){
135570 rc = growVTrans(db);
135571 if( rc==SQLITE_OK0 ){
135572 addToVTrans(db, sqlite3GetVTable(db, pTab));
135573 }
135574 }
135575
135576 return rc;
135577}
135578
135579/*
135580** This function is used to set the schema of a virtual table. It is only
135581** valid to call this function from within the xCreate() or xConnect() of a
135582** virtual table module.
135583*/
135584SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
135585 VtabCtx *pCtx;
135586 int rc = SQLITE_OK0;
135587 Table *pTab;
135588 char *zErr = 0;
135589 Parse sParse;
135590
135591#ifdef SQLITE_ENABLE_API_ARMOR
135592 if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){
135593 return SQLITE_MISUSE_BKPTsqlite3MisuseError(135593);
135594 }
135595#endif
135596 sqlite3_mutex_enter(db->mutex);
135597 pCtx = db->pVtabCtx;
135598 if( !pCtx || pCtx->bDeclared ){
135599 sqlite3Error(db, SQLITE_MISUSE21);
135600 sqlite3_mutex_leave(db->mutex);
135601 return SQLITE_MISUSE_BKPTsqlite3MisuseError(135601);
135602 }
135603 pTab = pCtx->pTab;
135604 assert( IsVirtual(pTab) )((void) (0));
135605
135606 memset(&sParse, 0, sizeof(sParse));
135607 sParse.eParseMode = PARSE_MODE_DECLARE_VTAB1;
135608 sParse.db = db;
135609 sParse.nQueryLoop = 1;
135610 if( SQLITE_OK0==sqlite3RunParser(&sParse, zCreateTable, &zErr)
135611 && sParse.pNewTable
135612 && !db->mallocFailed
135613 && !sParse.pNewTable->pSelect
135614 && !IsVirtual(sParse.pNewTable)((sParse.pNewTable)->nModuleArg)
135615 ){
135616 if( !pTab->aCol ){
135617 Table *pNew = sParse.pNewTable;
135618 Index *pIdx;
135619 pTab->aCol = pNew->aCol;
135620 pTab->nCol = pNew->nCol;
135621 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid0x0020|TF_NoVisibleRowid0x0040);
135622 pNew->nCol = 0;
135623 pNew->aCol = 0;
135624 assert( pTab->pIndex==0 )((void) (0));
135625 assert( HasRowid(pNew) || sqlite3PrimaryKeyIndex(pNew)!=0 )((void) (0));
135626 if( !HasRowid(pNew)(((pNew)->tabFlags & 0x0020)==0)
135627 && pCtx->pVTable->pMod->pModule->xUpdate!=0
135628 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
135629 ){
135630 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
135631 ** or else must have a single-column PRIMARY KEY */
135632 rc = SQLITE_ERROR1;
135633 }
135634 pIdx = pNew->pIndex;
135635 if( pIdx ){
135636 assert( pIdx->pNext==0 )((void) (0));
135637 pTab->pIndex = pIdx;
135638 pNew->pIndex = 0;
135639 pIdx->pTable = pTab;
135640 }
135641 }
135642 pCtx->bDeclared = 1;
135643 }else{
135644 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, (zErr ? "%s" : 0), zErr);
135645 sqlite3DbFree(db, zErr);
135646 rc = SQLITE_ERROR1;
135647 }
135648 sParse.eParseMode = PARSE_MODE_NORMAL0;
135649
135650 if( sParse.pVdbe ){
135651 sqlite3VdbeFinalize(sParse.pVdbe);
135652 }
135653 sqlite3DeleteTable(db, sParse.pNewTable);
135654 sqlite3ParserReset(&sParse);
135655
135656 assert( (rc&0xff)==rc )((void) (0));
135657 rc = sqlite3ApiExit(db, rc);
135658 sqlite3_mutex_leave(db->mutex);
135659 return rc;
135660}
135661
135662/*
135663** This function is invoked by the vdbe to call the xDestroy method
135664** of the virtual table named zTab in database iDb. This occurs
135665** when a DROP TABLE is mentioned.
135666**
135667** This call is a no-op if zTab is not a virtual table.
135668*/
135669SQLITE_PRIVATEstatic int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
135670 int rc = SQLITE_OK0;
135671 Table *pTab;
135672
135673 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
135674 if( pTab!=0 && ALWAYS(pTab->pVTable!=0)(pTab->pVTable!=0) ){
135675 VTable *p;
135676 int (*xDestroy)(sqlite3_vtab *);
135677 for(p=pTab->pVTable; p; p=p->pNext){
135678 assert( p->pVtab )((void) (0));
135679 if( p->pVtab->nRef>0 ){
135680 return SQLITE_LOCKED6;
135681 }
135682 }
135683 p = vtabDisconnectAll(db, pTab);
135684 xDestroy = p->pMod->pModule->xDestroy;
135685 assert( xDestroy!=0 )((void) (0)); /* Checked before the virtual table is created */
135686 pTab->nTabRef++;
135687 rc = xDestroy(p->pVtab);
135688 /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
135689 if( rc==SQLITE_OK0 ){
135690 assert( pTab->pVTable==p && p->pNext==0 )((void) (0));
135691 p->pVtab = 0;
135692 pTab->pVTable = 0;
135693 sqlite3VtabUnlock(p);
135694 }
135695 sqlite3DeleteTable(db, pTab);
135696 }
135697
135698 return rc;
135699}
135700
135701/*
135702** This function invokes either the xRollback or xCommit method
135703** of each of the virtual tables in the sqlite3.aVTrans array. The method
135704** called is identified by the second argument, "offset", which is
135705** the offset of the method to call in the sqlite3_module structure.
135706**
135707** The array is cleared after invoking the callbacks.
135708*/
135709static void callFinaliser(sqlite3 *db, int offset){
135710 int i;
135711 if( db->aVTrans ){
135712 VTable **aVTrans = db->aVTrans;
135713 db->aVTrans = 0;
135714 for(i=0; i<db->nVTrans; i++){
135715 VTable *pVTab = aVTrans[i];
135716 sqlite3_vtab *p = pVTab->pVtab;
135717 if( p ){
135718 int (*x)(sqlite3_vtab *);
135719 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
135720 if( x ) x(p);
135721 }
135722 pVTab->iSavepoint = 0;
135723 sqlite3VtabUnlock(pVTab);
135724 }
135725 sqlite3DbFree(db, aVTrans);
135726 db->nVTrans = 0;
135727 }
135728}
135729
135730/*
135731** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
135732** array. Return the error code for the first error that occurs, or
135733** SQLITE_OK if all xSync operations are successful.
135734**
135735** If an error message is available, leave it in p->zErrMsg.
135736*/
135737SQLITE_PRIVATEstatic int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
135738 int i;
135739 int rc = SQLITE_OK0;
135740 VTable **aVTrans = db->aVTrans;
135741
135742 db->aVTrans = 0;
135743 for(i=0; rc==SQLITE_OK0 && i<db->nVTrans; i++){
135744 int (*x)(sqlite3_vtab *);
135745 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
135746 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
135747 rc = x(pVtab);
135748 sqlite3VtabImportErrmsg(p, pVtab);
135749 }
135750 }
135751 db->aVTrans = aVTrans;
135752 return rc;
135753}
135754
135755/*
135756** Invoke the xRollback method of all virtual tables in the
135757** sqlite3.aVTrans array. Then clear the array itself.
135758*/
135759SQLITE_PRIVATEstatic int sqlite3VtabRollback(sqlite3 *db){
135760 callFinaliser(db, offsetof(sqlite3_module,xRollback)__builtin_offsetof(sqlite3_module, xRollback));
135761 return SQLITE_OK0;
135762}
135763
135764/*
135765** Invoke the xCommit method of all virtual tables in the
135766** sqlite3.aVTrans array. Then clear the array itself.
135767*/
135768SQLITE_PRIVATEstatic int sqlite3VtabCommit(sqlite3 *db){
135769 callFinaliser(db, offsetof(sqlite3_module,xCommit)__builtin_offsetof(sqlite3_module, xCommit));
135770 return SQLITE_OK0;
135771}
135772
135773/*
135774** If the virtual table pVtab supports the transaction interface
135775** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
135776** not currently open, invoke the xBegin method now.
135777**
135778** If the xBegin call is successful, place the sqlite3_vtab pointer
135779** in the sqlite3.aVTrans array.
135780*/
135781SQLITE_PRIVATEstatic int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
135782 int rc = SQLITE_OK0;
135783 const sqlite3_module *pModule;
135784
135785 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
135786 ** than zero, then this function is being called from within a
135787 ** virtual module xSync() callback. It is illegal to write to
135788 ** virtual module tables in this case, so return SQLITE_LOCKED.
135789 */
135790 if( sqlite3VtabInSync(db)((db)->nVTrans>0 && (db)->aVTrans==0) ){
135791 return SQLITE_LOCKED6;
135792 }
135793 if( !pVTab ){
135794 return SQLITE_OK0;
135795 }
135796 pModule = pVTab->pVtab->pModule;
135797
135798 if( pModule->xBegin ){
135799 int i;
135800
135801 /* If pVtab is already in the aVTrans array, return early */
135802 for(i=0; i<db->nVTrans; i++){
135803 if( db->aVTrans[i]==pVTab ){
135804 return SQLITE_OK0;
135805 }
135806 }
135807
135808 /* Invoke the xBegin method. If successful, add the vtab to the
135809 ** sqlite3.aVTrans[] array. */
135810 rc = growVTrans(db);
135811 if( rc==SQLITE_OK0 ){
135812 rc = pModule->xBegin(pVTab->pVtab);
135813 if( rc==SQLITE_OK0 ){
135814 int iSvpt = db->nStatement + db->nSavepoint;
135815 addToVTrans(db, pVTab);
135816 if( iSvpt && pModule->xSavepoint ){
135817 pVTab->iSavepoint = iSvpt;
135818 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
135819 }
135820 }
135821 }
135822 }
135823 return rc;
135824}
135825
135826/*
135827** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
135828** virtual tables that currently have an open transaction. Pass iSavepoint
135829** as the second argument to the virtual table method invoked.
135830**
135831** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
135832** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
135833** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
135834** an open transaction is invoked.
135835**
135836** If any virtual table method returns an error code other than SQLITE_OK,
135837** processing is abandoned and the error returned to the caller of this
135838** function immediately. If all calls to virtual table methods are successful,
135839** SQLITE_OK is returned.
135840*/
135841SQLITE_PRIVATEstatic int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
135842 int rc = SQLITE_OK0;
135843
135844 assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN )((void) (0));
135845 assert( iSavepoint>=-1 )((void) (0));
135846 if( db->aVTrans ){
135847 int i;
135848 for(i=0; rc==SQLITE_OK0 && i<db->nVTrans; i++){
135849 VTable *pVTab = db->aVTrans[i];
135850 const sqlite3_module *pMod = pVTab->pMod->pModule;
135851 if( pVTab->pVtab && pMod->iVersion>=2 ){
135852 int (*xMethod)(sqlite3_vtab *, int);
135853 sqlite3VtabLock(pVTab);
135854 switch( op ){
135855 case SAVEPOINT_BEGIN0:
135856 xMethod = pMod->xSavepoint;
135857 pVTab->iSavepoint = iSavepoint+1;
135858 break;
135859 case SAVEPOINT_ROLLBACK2:
135860 xMethod = pMod->xRollbackTo;
135861 break;
135862 default:
135863 xMethod = pMod->xRelease;
135864 break;
135865 }
135866 if( xMethod && pVTab->iSavepoint>iSavepoint ){
135867 rc = xMethod(pVTab->pVtab, iSavepoint);
135868 }
135869 sqlite3VtabUnlock(pVTab);
135870 }
135871 }
135872 }
135873 return rc;
135874}
135875
135876/*
135877** The first parameter (pDef) is a function implementation. The
135878** second parameter (pExpr) is the first argument to this function.
135879** If pExpr is a column in a virtual table, then let the virtual
135880** table implementation have an opportunity to overload the function.
135881**
135882** This routine is used to allow virtual table implementations to
135883** overload MATCH, LIKE, GLOB, and REGEXP operators.
135884**
135885** Return either the pDef argument (indicating no change) or a
135886** new FuncDef structure that is marked as ephemeral using the
135887** SQLITE_FUNC_EPHEM flag.
135888*/
135889SQLITE_PRIVATEstatic FuncDef *sqlite3VtabOverloadFunction(
135890 sqlite3 *db, /* Database connection for reporting malloc problems */
135891 FuncDef *pDef, /* Function to possibly overload */
135892 int nArg, /* Number of arguments to the function */
135893 Expr *pExpr /* First argument to the function */
135894){
135895 Table *pTab;
135896 sqlite3_vtab *pVtab;
135897 sqlite3_module *pMod;
135898 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
135899 void *pArg = 0;
135900 FuncDef *pNew;
135901 int rc = 0;
135902
135903 /* Check to see the left operand is a column in a virtual table */
135904 if( NEVER(pExpr==0)(pExpr==0) ) return pDef;
135905 if( pExpr->op!=TK_COLUMN162 ) return pDef;
135906 pTab = pExpr->y.pTab;
135907 if( pTab==0 ) return pDef;
135908 if( !IsVirtual(pTab)((pTab)->nModuleArg) ) return pDef;
135909 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
135910 assert( pVtab!=0 )((void) (0));
135911 assert( pVtab->pModule!=0 )((void) (0));
135912 pMod = (sqlite3_module *)pVtab->pModule;
135913 if( pMod->xFindFunction==0 ) return pDef;
135914
135915 /* Call the xFindFunction method on the virtual table implementation
135916 ** to see if the implementation wants to overload this function.
135917 **
135918 ** Though undocumented, we have historically always invoked xFindFunction
135919 ** with an all lower-case function name. Continue in this tradition to
135920 ** avoid any chance of an incompatibility.
135921 */
135922#ifdef SQLITE_DEBUG
135923 {
135924 int i;
135925 for(i=0; pDef->zName[i]; i++){
135926 unsigned char x = (unsigned char)pDef->zName[i];
135927 assert( x==sqlite3UpperToLower[x] )((void) (0));
135928 }
135929 }
135930#endif
135931 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
135932 if( rc==0 ){
135933 return pDef;
135934 }
135935
135936 /* Create a new ephemeral function definition for the overloaded
135937 ** function */
135938 pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
135939 + sqlite3Strlen30(pDef->zName) + 1);
135940 if( pNew==0 ){
135941 return pDef;
135942 }
135943 *pNew = *pDef;
135944 pNew->zName = (const char*)&pNew[1];
135945 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
135946 pNew->xSFunc = xSFunc;
135947 pNew->pUserData = pArg;
135948 pNew->funcFlags |= SQLITE_FUNC_EPHEM0x0010;
135949 return pNew;
135950}
135951
135952/*
135953** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
135954** array so that an OP_VBegin will get generated for it. Add pTab to the
135955** array if it is missing. If pTab is already in the array, this routine
135956** is a no-op.
135957*/
135958SQLITE_PRIVATEstatic void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
135959 Parse *pToplevel = sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse));
135960 int i, n;
135961 Table **apVtabLock;
135962
135963 assert( IsVirtual(pTab) )((void) (0));
135964 for(i=0; i<pToplevel->nVtabLock; i++){
135965 if( pTab==pToplevel->apVtabLock[i] ) return;
135966 }
135967 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
135968 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
135969 if( apVtabLock ){
135970 pToplevel->apVtabLock = apVtabLock;
135971 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
135972 }else{
135973 sqlite3OomFault(pToplevel->db);
135974 }
135975}
135976
135977/*
135978** Check to see if virtual table module pMod can be have an eponymous
135979** virtual table instance. If it can, create one if one does not already
135980** exist. Return non-zero if the eponymous virtual table instance exists
135981** when this routine returns, and return zero if it does not exist.
135982**
135983** An eponymous virtual table instance is one that is named after its
135984** module, and more importantly, does not require a CREATE VIRTUAL TABLE
135985** statement in order to come into existance. Eponymous virtual table
135986** instances always exist. They cannot be DROP-ed.
135987**
135988** Any virtual table module for which xConnect and xCreate are the same
135989** method can have an eponymous virtual table instance.
135990*/
135991SQLITE_PRIVATEstatic int sqlite3VtabEponymousTableInit(Parse *pParse, Module *pMod){
135992 const sqlite3_module *pModule = pMod->pModule;
135993 Table *pTab;
135994 char *zErr = 0;
135995 int rc;
135996 sqlite3 *db = pParse->db;
135997 if( pMod->pEpoTab ) return 1;
135998 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
135999 pTab = sqlite3DbMallocZero(db, sizeof(Table));
136000 if( pTab==0 ) return 0;
136001 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
136002 if( pTab->zName==0 ){
136003 sqlite3DbFree(db, pTab);
136004 return 0;
136005 }
136006 pMod->pEpoTab = pTab;
136007 pTab->nTabRef = 1;
136008 pTab->pSchema = db->aDb[0].pSchema;
136009 assert( pTab->nModuleArg==0 )((void) (0));
136010 pTab->iPKey = -1;
136011 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
136012 addModuleArgument(pParse, pTab, 0);
136013 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
136014 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
136015 if( rc ){
136016 sqlite3ErrorMsg(pParse, "%s", zErr);
136017 sqlite3DbFree(db, zErr);
136018 sqlite3VtabEponymousTableClear(db, pMod);
136019 return 0;
136020 }
136021 return 1;
136022}
136023
136024/*
136025** Erase the eponymous virtual table instance associated with
136026** virtual table module pMod, if it exists.
136027*/
136028SQLITE_PRIVATEstatic void sqlite3VtabEponymousTableClear(sqlite3 *db, Module *pMod){
136029 Table *pTab = pMod->pEpoTab;
136030 if( pTab!=0 ){
136031 /* Mark the table as Ephemeral prior to deleting it, so that the
136032 ** sqlite3DeleteTable() routine will know that it is not stored in
136033 ** the schema. */
136034 pTab->tabFlags |= TF_Ephemeral0x0002;
136035 sqlite3DeleteTable(db, pTab);
136036 pMod->pEpoTab = 0;
136037 }
136038}
136039
136040/*
136041** Return the ON CONFLICT resolution mode in effect for the virtual
136042** table update operation currently in progress.
136043**
136044** The results of this routine are undefined unless it is called from
136045** within an xUpdate method.
136046*/
136047SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
136048 static const unsigned char aMap[] = {
136049 SQLITE_ROLLBACK1, SQLITE_ABORT4, SQLITE_FAIL3, SQLITE_IGNORE2, SQLITE_REPLACE5
136050 };
136051#ifdef SQLITE_ENABLE_API_ARMOR
136052 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(136052);
136053#endif
136054 assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 )((void) (0));
136055 assert( OE_Ignore==4 && OE_Replace==5 )((void) (0));
136056 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 )((void) (0));
136057 return (int)aMap[db->vtabOnConflict-1];
136058}
136059
136060/*
136061** Call from within the xCreate() or xConnect() methods to provide
136062** the SQLite core with additional information about the behavior
136063** of the virtual table being implemented.
136064*/
136065SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
136066 va_list ap;
136067 int rc = SQLITE_OK0;
136068
136069#ifdef SQLITE_ENABLE_API_ARMOR
136070 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(136070);
136071#endif
136072 sqlite3_mutex_enter(db->mutex);
136073 va_start(ap, op)__builtin_va_start(ap, op);
136074 switch( op ){
136075 case SQLITE_VTAB_CONSTRAINT_SUPPORT1: {
136076 VtabCtx *p = db->pVtabCtx;
136077 if( !p ){
136078 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(136078);
136079 }else{
136080 assert( p->pTab==0 || IsVirtual(p->pTab) )((void) (0));
136081 p->pVTable->bConstraint = (u8)va_arg(ap, int)__builtin_va_arg(ap, int);
136082 }
136083 break;
136084 }
136085 default:
136086 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(136086);
136087 break;
136088 }
136089 va_end(ap)__builtin_va_end(ap);
136090
136091 if( rc!=SQLITE_OK0 ) sqlite3Error(db, rc);
136092 sqlite3_mutex_leave(db->mutex);
136093 return rc;
136094}
136095
136096#endif /* SQLITE_OMIT_VIRTUALTABLE */
136097
136098/************** End of vtab.c ************************************************/
136099/************** Begin file wherecode.c ***************************************/
136100/*
136101** 2015-06-06
136102**
136103** The author disclaims copyright to this source code. In place of
136104** a legal notice, here is a blessing:
136105**
136106** May you do good and not evil.
136107** May you find forgiveness for yourself and forgive others.
136108** May you share freely, never taking more than you give.
136109**
136110*************************************************************************
136111** This module contains C code that generates VDBE code used to process
136112** the WHERE clause of SQL statements.
136113**
136114** This file was split off from where.c on 2015-06-06 in order to reduce the
136115** size of where.c and make it easier to edit. This file contains the routines
136116** that actually generate the bulk of the WHERE loop code. The original where.c
136117** file retains the code that does query planning and analysis.
136118*/
136119/* #include "sqliteInt.h" */
136120/************** Include whereInt.h in the middle of wherecode.c **************/
136121/************** Begin file whereInt.h ****************************************/
136122/*
136123** 2013-11-12
136124**
136125** The author disclaims copyright to this source code. In place of
136126** a legal notice, here is a blessing:
136127**
136128** May you do good and not evil.
136129** May you find forgiveness for yourself and forgive others.
136130** May you share freely, never taking more than you give.
136131**
136132*************************************************************************
136133**
136134** This file contains structure and macro definitions for the query
136135** planner logic in "where.c". These definitions are broken out into
136136** a separate source file for easier editing.
136137*/
136138#ifndef SQLITE_WHEREINT_H
136139#define SQLITE_WHEREINT_H
136140
136141/*
136142** Trace output macros
136143*/
136144#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
136145/***/ extern int sqlite3WhereTrace;
136146#endif
136147#if defined(SQLITE_DEBUG) \
136148 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
136149# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
136150# define WHERETRACE_ENABLED 1
136151#else
136152# define WHERETRACE(K,X)
136153#endif
136154
136155/* Forward references
136156*/
136157typedef struct WhereClause WhereClause;
136158typedef struct WhereMaskSet WhereMaskSet;
136159typedef struct WhereOrInfo WhereOrInfo;
136160typedef struct WhereAndInfo WhereAndInfo;
136161typedef struct WhereLevel WhereLevel;
136162typedef struct WhereLoop WhereLoop;
136163typedef struct WherePath WherePath;
136164typedef struct WhereTerm WhereTerm;
136165typedef struct WhereLoopBuilder WhereLoopBuilder;
136166typedef struct WhereScan WhereScan;
136167typedef struct WhereOrCost WhereOrCost;
136168typedef struct WhereOrSet WhereOrSet;
136169
136170/*
136171** This object contains information needed to implement a single nested
136172** loop in WHERE clause.
136173**
136174** Contrast this object with WhereLoop. This object describes the
136175** implementation of the loop. WhereLoop describes the algorithm.
136176** This object contains a pointer to the WhereLoop algorithm as one of
136177** its elements.
136178**
136179** The WhereInfo object contains a single instance of this object for
136180** each term in the FROM clause (which is to say, for each of the
136181** nested loops as implemented). The order of WhereLevel objects determines
136182** the loop nested order, with WhereInfo.a[0] being the outer loop and
136183** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
136184*/
136185struct WhereLevel {
136186 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
136187 int iTabCur; /* The VDBE cursor used to access the table */
136188 int iIdxCur; /* The VDBE cursor used to access pIdx */
136189 int addrBrk; /* Jump here to break out of the loop */
136190 int addrNxt; /* Jump here to start the next IN combination */
136191 int addrSkip; /* Jump here for next iteration of skip-scan */
136192 int addrCont; /* Jump here to continue with the next loop cycle */
136193 int addrFirst; /* First instruction of interior of the loop */
136194 int addrBody; /* Beginning of the body of this loop */
136195#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
136196 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
136197 int addrLikeRep; /* LIKE range processing address */
136198#endif
136199 u8 iFrom; /* Which entry in the FROM clause */
136200 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
136201 int p1, p2; /* Operands of the opcode used to ends the loop */
136202 union { /* Information that depends on pWLoop->wsFlags */
136203 struct {
136204 int nIn; /* Number of entries in aInLoop[] */
136205 struct InLoop {
136206 int iCur; /* The VDBE cursor used by this IN operator */
136207 int addrInTop; /* Top of the IN loop */
136208 int iBase; /* Base register of multi-key index record */
136209 int nPrefix; /* Number of prior entires in the key */
136210 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
136211 } *aInLoop; /* Information about each nested IN operator */
136212 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
136213 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
136214 } u;
136215 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
136216 Bitmask notReady; /* FROM entries not usable at this level */
136217#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
136218 int addrVisit; /* Address at which row is visited */
136219#endif
136220};
136221
136222/*
136223** Each instance of this object represents an algorithm for evaluating one
136224** term of a join. Every term of the FROM clause will have at least
136225** one corresponding WhereLoop object (unless INDEXED BY constraints
136226** prevent a query solution - which is an error) and many terms of the
136227** FROM clause will have multiple WhereLoop objects, each describing a
136228** potential way of implementing that FROM-clause term, together with
136229** dependencies and cost estimates for using the chosen algorithm.
136230**
136231** Query planning consists of building up a collection of these WhereLoop
136232** objects, then computing a particular sequence of WhereLoop objects, with
136233** one WhereLoop object per FROM clause term, that satisfy all dependencies
136234** and that minimize the overall cost.
136235*/
136236struct WhereLoop {
136237 Bitmask prereq; /* Bitmask of other loops that must run first */
136238 Bitmask maskSelf; /* Bitmask identifying table iTab */
136239#ifdef SQLITE_DEBUG
136240 char cId; /* Symbolic ID of this loop for debugging use */
136241#endif
136242 u8 iTab; /* Position in FROM clause of table for this loop */
136243 u8 iSortIdx; /* Sorting index number. 0==None */
136244 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
136245 LogEst rRun; /* Cost of running each loop */
136246 LogEst nOut; /* Estimated number of output rows */
136247 union {
136248 struct { /* Information for internal btree tables */
136249 u16 nEq; /* Number of equality constraints */
136250 u16 nBtm; /* Size of BTM vector */
136251 u16 nTop; /* Size of TOP vector */
136252 u16 nIdxCol; /* Index column used for ORDER BY */
136253 Index *pIndex; /* Index used, or NULL */
136254 } btree;
136255 struct { /* Information for virtual tables */
136256 int idxNum; /* Index number */
136257 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
136258 i8 isOrdered; /* True if satisfies ORDER BY */
136259 u16 omitMask; /* Terms that may be omitted */
136260 char *idxStr; /* Index identifier string */
136261 } vtab;
136262 } u;
136263 u32 wsFlags; /* WHERE_* flags describing the plan */
136264 u16 nLTerm; /* Number of entries in aLTerm[] */
136265 u16 nSkip; /* Number of NULL aLTerm[] entries */
136266 /**** whereLoopXfer() copies fields above ***********************/
136267# define WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot) offsetof(WhereLoop,nLSlot)__builtin_offsetof(WhereLoop, nLSlot)
136268 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
136269 WhereTerm **aLTerm; /* WhereTerms used */
136270 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
136271 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
136272};
136273
136274/* This object holds the prerequisites and the cost of running a
136275** subquery on one operand of an OR operator in the WHERE clause.
136276** See WhereOrSet for additional information
136277*/
136278struct WhereOrCost {
136279 Bitmask prereq; /* Prerequisites */
136280 LogEst rRun; /* Cost of running this subquery */
136281 LogEst nOut; /* Number of outputs for this subquery */
136282};
136283
136284/* The WhereOrSet object holds a set of possible WhereOrCosts that
136285** correspond to the subquery(s) of OR-clause processing. Only the
136286** best N_OR_COST elements are retained.
136287*/
136288#define N_OR_COST3 3
136289struct WhereOrSet {
136290 u16 n; /* Number of valid a[] entries */
136291 WhereOrCost a[N_OR_COST3]; /* Set of best costs */
136292};
136293
136294/*
136295** Each instance of this object holds a sequence of WhereLoop objects
136296** that implement some or all of a query plan.
136297**
136298** Think of each WhereLoop object as a node in a graph with arcs
136299** showing dependencies and costs for travelling between nodes. (That is
136300** not a completely accurate description because WhereLoop costs are a
136301** vector, not a scalar, and because dependencies are many-to-one, not
136302** one-to-one as are graph nodes. But it is a useful visualization aid.)
136303** Then a WherePath object is a path through the graph that visits some
136304** or all of the WhereLoop objects once.
136305**
136306** The "solver" works by creating the N best WherePath objects of length
136307** 1. Then using those as a basis to compute the N best WherePath objects
136308** of length 2. And so forth until the length of WherePaths equals the
136309** number of nodes in the FROM clause. The best (lowest cost) WherePath
136310** at the end is the chosen query plan.
136311*/
136312struct WherePath {
136313 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
136314 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
136315 LogEst nRow; /* Estimated number of rows generated by this path */
136316 LogEst rCost; /* Total cost of this path */
136317 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
136318 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
136319 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
136320};
136321
136322/*
136323** The query generator uses an array of instances of this structure to
136324** help it analyze the subexpressions of the WHERE clause. Each WHERE
136325** clause subexpression is separated from the others by AND operators,
136326** usually, or sometimes subexpressions separated by OR.
136327**
136328** All WhereTerms are collected into a single WhereClause structure.
136329** The following identity holds:
136330**
136331** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
136332**
136333** When a term is of the form:
136334**
136335** X <op> <expr>
136336**
136337** where X is a column name and <op> is one of certain operators,
136338** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
136339** cursor number and column number for X. WhereTerm.eOperator records
136340** the <op> using a bitmask encoding defined by WO_xxx below. The
136341** use of a bitmask encoding for the operator allows us to search
136342** quickly for terms that match any of several different operators.
136343**
136344** A WhereTerm might also be two or more subterms connected by OR:
136345**
136346** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
136347**
136348** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
136349** and the WhereTerm.u.pOrInfo field points to auxiliary information that
136350** is collected about the OR clause.
136351**
136352** If a term in the WHERE clause does not match either of the two previous
136353** categories, then eOperator==0. The WhereTerm.pExpr field is still set
136354** to the original subexpression content and wtFlags is set up appropriately
136355** but no other fields in the WhereTerm object are meaningful.
136356**
136357** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
136358** but they do so indirectly. A single WhereMaskSet structure translates
136359** cursor number into bits and the translated bit is stored in the prereq
136360** fields. The translation is used in order to maximize the number of
136361** bits that will fit in a Bitmask. The VDBE cursor numbers might be
136362** spread out over the non-negative integers. For example, the cursor
136363** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
136364** translates these sparse cursor numbers into consecutive integers
136365** beginning with 0 in order to make the best possible use of the available
136366** bits in the Bitmask. So, in the example above, the cursor numbers
136367** would be mapped into integers 0 through 7.
136368**
136369** The number of terms in a join is limited by the number of bits
136370** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
136371** is only able to process joins with 64 or fewer tables.
136372*/
136373struct WhereTerm {
136374 Expr *pExpr; /* Pointer to the subexpression that is this term */
136375 WhereClause *pWC; /* The clause this term is part of */
136376 LogEst truthProb; /* Probability of truth for this expression */
136377 u16 wtFlags; /* TERM_xxx bit flags. See below */
136378 u16 eOperator; /* A WO_xx value describing <op> */
136379 u8 nChild; /* Number of children that must disable us */
136380 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
136381 int iParent; /* Disable pWC->a[iParent] when this term disabled */
136382 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
136383 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
136384 union {
136385 int leftColumn; /* Column number of X in "X <op> <expr>" */
136386 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
136387 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
136388 } u;
136389 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
136390 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
136391};
136392
136393/*
136394** Allowed values of WhereTerm.wtFlags
136395*/
136396#define TERM_DYNAMIC0x01 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */
136397#define TERM_VIRTUAL0x02 0x02 /* Added by the optimizer. Do not code */
136398#define TERM_CODED0x04 0x04 /* This term is already coded */
136399#define TERM_COPIED0x08 0x08 /* Has a child */
136400#define TERM_ORINFO0x10 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
136401#define TERM_ANDINFO0x20 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
136402#define TERM_OR_OK0x40 0x40 /* Used during OR-clause processing */
136403#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
136404# define TERM_VNULL0x00 0x80 /* Manufactured x>NULL or x<=NULL term */
136405#else
136406# define TERM_VNULL0x00 0x00 /* Disabled if not using stat3 */
136407#endif
136408#define TERM_LIKEOPT0x100 0x100 /* Virtual terms from the LIKE optimization */
136409#define TERM_LIKECOND0x200 0x200 /* Conditionally this LIKE operator term */
136410#define TERM_LIKE0x400 0x400 /* The original LIKE operator */
136411#define TERM_IS0x800 0x800 /* Term.pExpr is an IS operator */
136412#define TERM_VARSELECT0x1000 0x1000 /* Term.pExpr contains a correlated sub-query */
136413
136414/*
136415** An instance of the WhereScan object is used as an iterator for locating
136416** terms in the WHERE clause that are useful to the query planner.
136417*/
136418struct WhereScan {
136419 WhereClause *pOrigWC; /* Original, innermost WhereClause */
136420 WhereClause *pWC; /* WhereClause currently being scanned */
136421 const char *zCollName; /* Required collating sequence, if not NULL */
136422 Expr *pIdxExpr; /* Search for this index expression */
136423 char idxaff; /* Must match this affinity, if zCollName!=NULL */
136424 unsigned char nEquiv; /* Number of entries in aEquiv[] */
136425 unsigned char iEquiv; /* Next unused slot in aEquiv[] */
136426 u32 opMask; /* Acceptable operators */
136427 int k; /* Resume scanning at this->pWC->a[this->k] */
136428 int aiCur[11]; /* Cursors in the equivalence class */
136429 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
136430};
136431
136432/*
136433** An instance of the following structure holds all information about a
136434** WHERE clause. Mostly this is a container for one or more WhereTerms.
136435**
136436** Explanation of pOuter: For a WHERE clause of the form
136437**
136438** a AND ((b AND c) OR (d AND e)) AND f
136439**
136440** There are separate WhereClause objects for the whole clause and for
136441** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
136442** subclauses points to the WhereClause object for the whole clause.
136443*/
136444struct WhereClause {
136445 WhereInfo *pWInfo; /* WHERE clause processing context */
136446 WhereClause *pOuter; /* Outer conjunction */
136447 u8 op; /* Split operator. TK_AND or TK_OR */
136448 u8 hasOr; /* True if any a[].eOperator is WO_OR */
136449 int nTerm; /* Number of terms */
136450 int nSlot; /* Number of entries in a[] */
136451 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
136452#if defined(SQLITE_SMALL_STACK)
136453 WhereTerm aStatic[1]; /* Initial static space for a[] */
136454#else
136455 WhereTerm aStatic[8]; /* Initial static space for a[] */
136456#endif
136457};
136458
136459/*
136460** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
136461** a dynamically allocated instance of the following structure.
136462*/
136463struct WhereOrInfo {
136464 WhereClause wc; /* Decomposition into subterms */
136465 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
136466};
136467
136468/*
136469** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
136470** a dynamically allocated instance of the following structure.
136471*/
136472struct WhereAndInfo {
136473 WhereClause wc; /* The subexpression broken out */
136474};
136475
136476/*
136477** An instance of the following structure keeps track of a mapping
136478** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
136479**
136480** The VDBE cursor numbers are small integers contained in
136481** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
136482** clause, the cursor numbers might not begin with 0 and they might
136483** contain gaps in the numbering sequence. But we want to make maximum
136484** use of the bits in our bitmasks. This structure provides a mapping
136485** from the sparse cursor numbers into consecutive integers beginning
136486** with 0.
136487**
136488** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
136489** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
136490**
136491** For example, if the WHERE clause expression used these VDBE
136492** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
136493** would map those cursor numbers into bits 0 through 5.
136494**
136495** Note that the mapping is not necessarily ordered. In the example
136496** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
136497** 57->5, 73->4. Or one of 719 other combinations might be used. It
136498** does not really matter. What is important is that sparse cursor
136499** numbers all get mapped into bit numbers that begin with 0 and contain
136500** no gaps.
136501*/
136502struct WhereMaskSet {
136503 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
136504 int n; /* Number of assigned cursor values */
136505 int ix[BMS((int)(sizeof(Bitmask)*8))]; /* Cursor assigned to each bit */
136506};
136507
136508/*
136509** Initialize a WhereMaskSet object
136510*/
136511#define initMaskSet(P)(P)->n=0 (P)->n=0
136512
136513/*
136514** This object is a convenience wrapper holding all information needed
136515** to construct WhereLoop objects for a particular query.
136516*/
136517struct WhereLoopBuilder {
136518 WhereInfo *pWInfo; /* Information about this WHERE */
136519 WhereClause *pWC; /* WHERE clause terms */
136520 ExprList *pOrderBy; /* ORDER BY clause */
136521 WhereLoop *pNew; /* Template WhereLoop */
136522 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
136523#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
136524 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
136525 int nRecValid; /* Number of valid fields currently in pRec */
136526#endif
136527 unsigned int bldFlags; /* SQLITE_BLDF_* flags */
136528 unsigned int iPlanLimit; /* Search limiter */
136529};
136530
136531/* Allowed values for WhereLoopBuider.bldFlags */
136532#define SQLITE_BLDF_INDEXED0x0001 0x0001 /* An index is used */
136533#define SQLITE_BLDF_UNIQUE0x0002 0x0002 /* All keys of a UNIQUE index used */
136534
136535/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
136536** index+constraint combinations the query planner will consider for a
136537** particular query. If this parameter is unlimited, then certain
136538** pathological queries can spend excess time in the sqlite3WhereBegin()
136539** routine. The limit is high enough that is should not impact real-world
136540** queries.
136541**
136542** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is
136543** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
136544** clause is processed, so that every table in a join is guaranteed to be
136545** able to propose a some index+constraint combinations even if the initial
136546** baseline limit was exhausted by prior tables of the join.
136547*/
136548#ifndef SQLITE_QUERY_PLANNER_LIMIT20000
136549# define SQLITE_QUERY_PLANNER_LIMIT20000 20000
136550#endif
136551#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR1000
136552# define SQLITE_QUERY_PLANNER_LIMIT_INCR1000 1000
136553#endif
136554
136555/*
136556** The WHERE clause processing routine has two halves. The
136557** first part does the start of the WHERE loop and the second
136558** half does the tail of the WHERE loop. An instance of
136559** this structure is returned by the first half and passed
136560** into the second half to give some continuity.
136561**
136562** An instance of this object holds the complete state of the query
136563** planner.
136564*/
136565struct WhereInfo {
136566 Parse *pParse; /* Parsing and code generating context */
136567 SrcList *pTabList; /* List of tables in the join */
136568 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
136569 ExprList *pResultSet; /* Result set of the query */
136570 Expr *pWhere; /* The complete WHERE clause */
136571 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
136572 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
136573 int iContinue; /* Jump here to continue with next record */
136574 int iBreak; /* Jump here to break out of the loop */
136575 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
136576 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
136577 u8 nLevel; /* Number of nested loop */
136578 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
136579 u8 sorted; /* True if really sorted (not just grouped) */
136580 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
136581 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
136582 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
136583 u8 bOrderedInnerLoop; /* True if only the inner-most loop is ordered */
136584 int iTop; /* The very beginning of the WHERE loop */
136585 WhereLoop *pLoops; /* List of all WhereLoop objects */
136586 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
136587 LogEst nRowOut; /* Estimated number of output rows */
136588 WhereClause sWC; /* Decomposition of the WHERE clause */
136589 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
136590 WhereLevel a[1]; /* Information about each nest loop in WHERE */
136591};
136592
136593/*
136594** Private interfaces - callable only by other where.c routines.
136595**
136596** where.c:
136597*/
136598SQLITE_PRIVATEstatic Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
136599#ifdef WHERETRACE_ENABLED
136600SQLITE_PRIVATEstatic void sqlite3WhereClausePrint(WhereClause *pWC);
136601#endif
136602SQLITE_PRIVATEstatic WhereTerm *sqlite3WhereFindTerm(
136603 WhereClause *pWC, /* The WHERE clause to be searched */
136604 int iCur, /* Cursor number of LHS */
136605 int iColumn, /* Column number of LHS */
136606 Bitmask notReady, /* RHS must not overlap with this mask */
136607 u32 op, /* Mask of WO_xx values describing operator */
136608 Index *pIdx /* Must be compatible with this index, if not NULL */
136609);
136610
136611/* wherecode.c: */
136612#ifndef SQLITE_OMIT_EXPLAIN
136613SQLITE_PRIVATEstatic int sqlite3WhereExplainOneScan(
136614 Parse *pParse, /* Parse context */
136615 SrcList *pTabList, /* Table list this loop refers to */
136616 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
136617 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
136618);
136619#else
136620# define sqlite3WhereExplainOneScan(u,v,w,x) 0
136621#endif /* SQLITE_OMIT_EXPLAIN */
136622#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
136623SQLITE_PRIVATEstatic void sqlite3WhereAddScanStatus(((void)int addrExplain)
136624 Vdbe *v, /* Vdbe to add scanstatus entry to */((void)int addrExplain)
136625 SrcList *pSrclist, /* FROM clause pLvl reads data from */((void)int addrExplain)
136626 WhereLevel *pLvl, /* Level to add scanstatus() entry for */((void)int addrExplain)
136627 int addrExplain /* Address of OP_Explain (or 0) */((void)int addrExplain)
136628)((void)int addrExplain);
136629#else
136630# define sqlite3WhereAddScanStatus(a, b, c, d)((void)d) ((void)d)
136631#endif
136632SQLITE_PRIVATEstatic Bitmask sqlite3WhereCodeOneLoopStart(
136633 Parse *pParse, /* Parsing context */
136634 Vdbe *v, /* Prepared statement under construction */
136635 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
136636 int iLevel, /* Which level of pWInfo->a[] should be coded */
136637 WhereLevel *pLevel, /* The current level pointer */
136638 Bitmask notReady /* Which tables are currently available */
136639);
136640
136641/* whereexpr.c: */
136642SQLITE_PRIVATEstatic void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
136643SQLITE_PRIVATEstatic void sqlite3WhereClauseClear(WhereClause*);
136644SQLITE_PRIVATEstatic void sqlite3WhereSplit(WhereClause*,Expr*,u8);
136645SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
136646SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
136647SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
136648SQLITE_PRIVATEstatic void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
136649SQLITE_PRIVATEstatic void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*);
136650
136651
136652
136653
136654
136655/*
136656** Bitmasks for the operators on WhereTerm objects. These are all
136657** operators that are of interest to the query planner. An
136658** OR-ed combination of these values can be used when searching for
136659** particular WhereTerms within a WhereClause.
136660**
136661** Value constraints:
136662** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
136663** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
136664** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
136665** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
136666** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
136667*/
136668#define WO_IN0x0001 0x0001
136669#define WO_EQ0x0002 0x0002
136670#define WO_LT(0x0002<<(56 -53)) (WO_EQ0x0002<<(TK_LT56-TK_EQ53))
136671#define WO_LE(0x0002<<(55 -53)) (WO_EQ0x0002<<(TK_LE55-TK_EQ53))
136672#define WO_GT(0x0002<<(54 -53)) (WO_EQ0x0002<<(TK_GT54-TK_EQ53))
136673#define WO_GE(0x0002<<(57 -53)) (WO_EQ0x0002<<(TK_GE57-TK_EQ53))
136674#define WO_AUX0x0040 0x0040 /* Op useful to virtual tables only */
136675#define WO_IS0x0080 0x0080
136676#define WO_ISNULL0x0100 0x0100
136677#define WO_OR0x0200 0x0200 /* Two or more OR-connected terms */
136678#define WO_AND0x0400 0x0400 /* Two or more AND-connected terms */
136679#define WO_EQUIV0x0800 0x0800 /* Of the form A==B, both columns */
136680#define WO_NOOP0x1000 0x1000 /* This term does not restrict search space */
136681
136682#define WO_ALL0x1fff 0x1fff /* Mask of all possible WO_* values */
136683#define WO_SINGLE0x01ff 0x01ff /* Mask of all non-compound WO_* values */
136684
136685/*
136686** These are definitions of bits in the WhereLoop.wsFlags field.
136687** The particular combination of bits in each WhereLoop help to
136688** determine the algorithm that WhereLoop represents.
136689*/
136690#define WHERE_COLUMN_EQ0x00000001 0x00000001 /* x=EXPR */
136691#define WHERE_COLUMN_RANGE0x00000002 0x00000002 /* x<EXPR and/or x>EXPR */
136692#define WHERE_COLUMN_IN0x00000004 0x00000004 /* x IN (...) */
136693#define WHERE_COLUMN_NULL0x00000008 0x00000008 /* x IS NULL */
136694#define WHERE_CONSTRAINT0x0000000f 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
136695#define WHERE_TOP_LIMIT0x00000010 0x00000010 /* x<EXPR or x<=EXPR constraint */
136696#define WHERE_BTM_LIMIT0x00000020 0x00000020 /* x>EXPR or x>=EXPR constraint */
136697#define WHERE_BOTH_LIMIT0x00000030 0x00000030 /* Both x>EXPR and x<EXPR */
136698#define WHERE_IDX_ONLY0x00000040 0x00000040 /* Use index only - omit table */
136699#define WHERE_IPK0x00000100 0x00000100 /* x is the INTEGER PRIMARY KEY */
136700#define WHERE_INDEXED0x00000200 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
136701#define WHERE_VIRTUALTABLE0x00000400 0x00000400 /* WhereLoop.u.vtab is valid */
136702#define WHERE_IN_ABLE0x00000800 0x00000800 /* Able to support an IN operator */
136703#define WHERE_ONEROW0x00001000 0x00001000 /* Selects no more than one row */
136704#define WHERE_MULTI_OR0x00002000 0x00002000 /* OR using multiple indices */
136705#define WHERE_AUTO_INDEX0x00004000 0x00004000 /* Uses an ephemeral index */
136706#define WHERE_SKIPSCAN0x00008000 0x00008000 /* Uses the skip-scan algorithm */
136707#define WHERE_UNQ_WANTED0x00010000 0x00010000 /* WHERE_ONEROW would have been helpful*/
136708#define WHERE_PARTIALIDX0x00020000 0x00020000 /* The automatic index is partial */
136709#define WHERE_IN_EARLYOUT0x00040000 0x00040000 /* Perhaps quit IN loops early */
136710
136711#endif /* !defined(SQLITE_WHEREINT_H) */
136712
136713/************** End of whereInt.h ********************************************/
136714/************** Continuing where we left off in wherecode.c ******************/
136715
136716#ifndef SQLITE_OMIT_EXPLAIN
136717
136718/*
136719** Return the name of the i-th column of the pIdx index.
136720*/
136721static const char *explainIndexColumnName(Index *pIdx, int i){
136722 i = pIdx->aiColumn[i];
136723 if( i==XN_EXPR(-2) ) return "<expr>";
136724 if( i==XN_ROWID(-1) ) return "rowid";
136725 return pIdx->pTable->aCol[i].zName;
136726}
136727
136728/*
136729** This routine is a helper for explainIndexRange() below
136730**
136731** pStr holds the text of an expression that we are building up one term
136732** at a time. This routine adds a new term to the end of the expression.
136733** Terms are separated by AND so add the "AND" text for second and subsequent
136734** terms only.
136735*/
136736static void explainAppendTerm(
136737 StrAccum *pStr, /* The text expression being built */
136738 Index *pIdx, /* Index to read column names from */
136739 int nTerm, /* Number of terms */
136740 int iTerm, /* Zero-based index of first term. */
136741 int bAnd, /* Non-zero to append " AND " */
136742 const char *zOp /* Name of the operator */
136743){
136744 int i;
136745
136746 assert( nTerm>=1 )((void) (0));
136747 if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
136748
136749 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
136750 for(i=0; i<nTerm; i++){
136751 if( i ) sqlite3_str_append(pStr, ",", 1);
136752 sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
136753 }
136754 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
136755
136756 sqlite3_str_append(pStr, zOp, 1);
136757
136758 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
136759 for(i=0; i<nTerm; i++){
136760 if( i ) sqlite3_str_append(pStr, ",", 1);
136761 sqlite3_str_append(pStr, "?", 1);
136762 }
136763 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
136764}
136765
136766/*
136767** Argument pLevel describes a strategy for scanning table pTab. This
136768** function appends text to pStr that describes the subset of table
136769** rows scanned by the strategy in the form of an SQL expression.
136770**
136771** For example, if the query:
136772**
136773** SELECT * FROM t1 WHERE a=1 AND b>2;
136774**
136775** is run and there is an index on (a, b), then this function returns a
136776** string similar to:
136777**
136778** "a=? AND b>?"
136779*/
136780static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
136781 Index *pIndex = pLoop->u.btree.pIndex;
136782 u16 nEq = pLoop->u.btree.nEq;
136783 u16 nSkip = pLoop->nSkip;
136784 int i, j;
136785
136786 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT0x00000020|WHERE_TOP_LIMIT0x00000010))==0 ) return;
136787 sqlite3_str_append(pStr, " (", 2);
136788 for(i=0; i<nEq; i++){
136789 const char *z = explainIndexColumnName(pIndex, i);
136790 if( i ) sqlite3_str_append(pStr, " AND ", 5);
136791 sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
136792 }
136793
136794 j = i;
136795 if( pLoop->wsFlags&WHERE_BTM_LIMIT0x00000020 ){
136796 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
136797 i = 1;
136798 }
136799 if( pLoop->wsFlags&WHERE_TOP_LIMIT0x00000010 ){
136800 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
136801 }
136802 sqlite3_str_append(pStr, ")", 1);
136803}
136804
136805/*
136806** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
136807** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
136808** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
136809** is added to the output to describe the table scan strategy in pLevel.
136810**
136811** If an OP_Explain opcode is added to the VM, its address is returned.
136812** Otherwise, if no OP_Explain is coded, zero is returned.
136813*/
136814SQLITE_PRIVATEstatic int sqlite3WhereExplainOneScan(
136815 Parse *pParse, /* Parse context */
136816 SrcList *pTabList, /* Table list this loop refers to */
136817 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
136818 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
136819){
136820 int ret = 0;
136821#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
136822 if( sqlite3ParseToplevel(pParse)((pParse)->pToplevel ? (pParse)->pToplevel : (pParse))->explain==2 )
136823#endif
136824 {
136825 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
136826 Vdbe *v = pParse->pVdbe; /* VM being constructed */
136827 sqlite3 *db = pParse->db; /* Database handle */
136828 int isSearch; /* True for a SEARCH. False for SCAN. */
136829 WhereLoop *pLoop; /* The controlling WhereLoop object */
136830 u32 flags; /* Flags that describe this loop */
136831 char *zMsg; /* Text to add to EQP output */
136832 StrAccum str; /* EQP output string */
136833 char zBuf[100]; /* Initial space for EQP output string */
136834
136835 pLoop = pLevel->pWLoop;
136836 flags = pLoop->wsFlags;
136837 if( (flags&WHERE_MULTI_OR0x00002000) || (wctrlFlags&WHERE_OR_SUBCLAUSE0x0020) ) return 0;
136838
136839 isSearch = (flags&(WHERE_BTM_LIMIT0x00000020|WHERE_TOP_LIMIT0x00000010))!=0
136840 || ((flags&WHERE_VIRTUALTABLE0x00000400)==0 && (pLoop->u.btree.nEq>0))
136841 || (wctrlFlags&(WHERE_ORDERBY_MIN0x0001|WHERE_ORDERBY_MAX0x0002));
136842
136843 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH1000000000);
136844 sqlite3_str_appendall(&str, isSearch ? "SEARCH" : "SCAN");
136845 if( pItem->pSelect ){
136846 sqlite3_str_appendf(&str, " SUBQUERY %u", pItem->pSelect->selId);
136847 }else{
136848 sqlite3_str_appendf(&str, " TABLE %s", pItem->zName);
136849 }
136850
136851 if( pItem->zAlias ){
136852 sqlite3_str_appendf(&str, " AS %s", pItem->zAlias);
136853 }
136854 if( (flags & (WHERE_IPK0x00000100|WHERE_VIRTUALTABLE0x00000400))==0 ){
136855 const char *zFmt = 0;
136856 Index *pIdx;
136857
136858 assert( pLoop->u.btree.pIndex!=0 )((void) (0));
136859 pIdx = pLoop->u.btree.pIndex;
136860 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) )((void) (0));
136861 if( !HasRowid(pItem->pTab)(((pItem->pTab)->tabFlags & 0x0020)==0) && IsPrimaryKeyIndex(pIdx)((pIdx)->idxType==2) ){
136862 if( isSearch ){
136863 zFmt = "PRIMARY KEY";
136864 }
136865 }else if( flags & WHERE_PARTIALIDX0x00020000 ){
136866 zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
136867 }else if( flags & WHERE_AUTO_INDEX0x00004000 ){
136868 zFmt = "AUTOMATIC COVERING INDEX";
136869 }else if( flags & WHERE_IDX_ONLY0x00000040 ){
136870 zFmt = "COVERING INDEX %s";
136871 }else{
136872 zFmt = "INDEX %s";
136873 }
136874 if( zFmt ){
136875 sqlite3_str_append(&str, " USING ", 7);
136876 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
136877 explainIndexRange(&str, pLoop);
136878 }
136879 }else if( (flags & WHERE_IPK0x00000100)!=0 && (flags & WHERE_CONSTRAINT0x0000000f)!=0 ){
136880 const char *zRangeOp;
136881 if( flags&(WHERE_COLUMN_EQ0x00000001|WHERE_COLUMN_IN0x00000004) ){
136882 zRangeOp = "=";
136883 }else if( (flags&WHERE_BOTH_LIMIT0x00000030)==WHERE_BOTH_LIMIT0x00000030 ){
136884 zRangeOp = ">? AND rowid<";
136885 }else if( flags&WHERE_BTM_LIMIT0x00000020 ){
136886 zRangeOp = ">";
136887 }else{
136888 assert( flags&WHERE_TOP_LIMIT)((void) (0));
136889 zRangeOp = "<";
136890 }
136891 sqlite3_str_appendf(&str,
136892 " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
136893 }
136894#ifndef SQLITE_OMIT_VIRTUALTABLE
136895 else if( (flags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
136896 sqlite3_str_appendf(&str, " VIRTUAL TABLE INDEX %d:%s",
136897 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
136898 }
136899#endif
136900#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
136901 if( pLoop->nOut>=10 ){
136902 sqlite3_str_appendf(&str, " (~%llu rows)",
136903 sqlite3LogEstToInt(pLoop->nOut));
136904 }else{
136905 sqlite3_str_append(&str, " (~1 row)", 9);
136906 }
136907#endif
136908 zMsg = sqlite3StrAccumFinish(&str);
136909 sqlite3ExplainBreakpoint("",zMsg);
136910 ret = sqlite3VdbeAddOp4(v, OP_Explain171, sqlite3VdbeCurrentAddr(v),
136911 pParse->addrExplain, 0, zMsg,P4_DYNAMIC(-7));
136912 }
136913 return ret;
136914}
136915#endif /* SQLITE_OMIT_EXPLAIN */
136916
136917#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
136918/*
136919** Configure the VM passed as the first argument with an
136920** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
136921** implement level pLvl. Argument pSrclist is a pointer to the FROM
136922** clause that the scan reads data from.
136923**
136924** If argument addrExplain is not 0, it must be the address of an
136925** OP_Explain instruction that describes the same loop.
136926*/
136927SQLITE_PRIVATEstatic void sqlite3WhereAddScanStatus(((void)int addrExplain)
136928 Vdbe *v, /* Vdbe to add scanstatus entry to */((void)int addrExplain)
136929 SrcList *pSrclist, /* FROM clause pLvl reads data from */((void)int addrExplain)
136930 WhereLevel *pLvl, /* Level to add scanstatus() entry for */((void)int addrExplain)
136931 int addrExplain /* Address of OP_Explain (or 0) */((void)int addrExplain)
136932)((void)int addrExplain){
136933 const char *zObj = 0;
136934 WhereLoop *pLoop = pLvl->pWLoop;
136935 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 && pLoop->u.btree.pIndex!=0 ){
136936 zObj = pLoop->u.btree.pIndex->zName;
136937 }else{
136938 zObj = pSrclist->a[pLvl->iFrom].zName;
136939 }
136940 sqlite3VdbeScanStatus(
136941 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
136942 );
136943}
136944#endif
136945
136946
136947/*
136948** Disable a term in the WHERE clause. Except, do not disable the term
136949** if it controls a LEFT OUTER JOIN and it did not originate in the ON
136950** or USING clause of that join.
136951**
136952** Consider the term t2.z='ok' in the following queries:
136953**
136954** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
136955** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
136956** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
136957**
136958** The t2.z='ok' is disabled in the in (2) because it originates
136959** in the ON clause. The term is disabled in (3) because it is not part
136960** of a LEFT OUTER JOIN. In (1), the term is not disabled.
136961**
136962** Disabling a term causes that term to not be tested in the inner loop
136963** of the join. Disabling is an optimization. When terms are satisfied
136964** by indices, we disable them to prevent redundant tests in the inner
136965** loop. We would get the correct results if nothing were ever disabled,
136966** but joins might run a little slower. The trick is to disable as much
136967** as we can without disabling too much. If we disabled in (1), we'd get
136968** the wrong answer. See ticket #813.
136969**
136970** If all the children of a term are disabled, then that term is also
136971** automatically disabled. In this way, terms get disabled if derived
136972** virtual terms are tested first. For example:
136973**
136974** x GLOB 'abc*' AND x>='abc' AND x<'acd'
136975** \___________/ \______/ \_____/
136976** parent child1 child2
136977**
136978** Only the parent term was in the original WHERE clause. The child1
136979** and child2 terms were added by the LIKE optimization. If both of
136980** the virtual child terms are valid, then testing of the parent can be
136981** skipped.
136982**
136983** Usually the parent term is marked as TERM_CODED. But if the parent
136984** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
136985** The TERM_LIKECOND marking indicates that the term should be coded inside
136986** a conditional such that is only evaluated on the second pass of a
136987** LIKE-optimization loop, when scanning BLOBs instead of strings.
136988*/
136989static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
136990 int nLoop = 0;
136991 assert( pTerm!=0 )((void) (0));
136992 while( (pTerm->wtFlags & TERM_CODED0x04)==0
136993 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)(((pTerm->pExpr)->flags&(0x000001))!=0))
136994 && (pLevel->notReady & pTerm->prereqAll)==0
136995 ){
136996 if( nLoop && (pTerm->wtFlags & TERM_LIKE0x400)!=0 ){
136997 pTerm->wtFlags |= TERM_LIKECOND0x200;
136998 }else{
136999 pTerm->wtFlags |= TERM_CODED0x04;
137000 }
137001 if( pTerm->iParent<0 ) break;
137002 pTerm = &pTerm->pWC->a[pTerm->iParent];
137003 assert( pTerm!=0 )((void) (0));
137004 pTerm->nChild--;
137005 if( pTerm->nChild!=0 ) break;
137006 nLoop++;
137007 }
137008}
137009
137010/*
137011** Code an OP_Affinity opcode to apply the column affinity string zAff
137012** to the n registers starting at base.
137013**
137014** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
137015** beginning and end of zAff are ignored. If all entries in zAff are
137016** SQLITE_AFF_BLOB, then no code gets generated.
137017**
137018** This routine makes its own copy of zAff so that the caller is free
137019** to modify zAff after this routine returns.
137020*/
137021static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
137022 Vdbe *v = pParse->pVdbe;
137023 if( zAff==0 ){
137024 assert( pParse->db->mallocFailed )((void) (0));
137025 return;
137026 }
137027 assert( v!=0 )((void) (0));
137028
137029 /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
137030 ** and end of the affinity string.
137031 */
137032 while( n>0 && zAff[0]==SQLITE_AFF_BLOB'A' ){
137033 n--;
137034 base++;
137035 zAff++;
137036 }
137037 while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB'A' ){
137038 n--;
137039 }
137040
137041 /* Code the OP_Affinity opcode if there is anything left to do. */
137042 if( n>0 ){
137043 sqlite3VdbeAddOp4(v, OP_Affinity91, base, n, 0, zAff, n);
137044 }
137045}
137046
137047/*
137048** Expression pRight, which is the RHS of a comparison operation, is
137049** either a vector of n elements or, if n==1, a scalar expression.
137050** Before the comparison operation, affinity zAff is to be applied
137051** to the pRight values. This function modifies characters within the
137052** affinity string to SQLITE_AFF_BLOB if either:
137053**
137054** * the comparison will be performed with no affinity, or
137055** * the affinity change in zAff is guaranteed not to change the value.
137056*/
137057static void updateRangeAffinityStr(
137058 Expr *pRight, /* RHS of comparison */
137059 int n, /* Number of vector elements in comparison */
137060 char *zAff /* Affinity string to modify */
137061){
137062 int i;
137063 for(i=0; i<n; i++){
137064 Expr *p = sqlite3VectorFieldSubexpr(pRight, i);
137065 if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB'A'
137066 || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
137067 ){
137068 zAff[i] = SQLITE_AFF_BLOB'A';
137069 }
137070 }
137071}
137072
137073
137074/*
137075** pX is an expression of the form: (vector) IN (SELECT ...)
137076** In other words, it is a vector IN operator with a SELECT clause on the
137077** LHS. But not all terms in the vector are indexable and the terms might
137078** not be in the correct order for indexing.
137079**
137080** This routine makes a copy of the input pX expression and then adjusts
137081** the vector on the LHS with corresponding changes to the SELECT so that
137082** the vector contains only index terms and those terms are in the correct
137083** order. The modified IN expression is returned. The caller is responsible
137084** for deleting the returned expression.
137085**
137086** Example:
137087**
137088** CREATE TABLE t1(a,b,c,d,e,f);
137089** CREATE INDEX t1x1 ON t1(e,c);
137090** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
137091** \_______________________________________/
137092** The pX expression
137093**
137094** Since only columns e and c can be used with the index, in that order,
137095** the modified IN expression that is returned will be:
137096**
137097** (e,c) IN (SELECT z,x FROM t2)
137098**
137099** The reduced pX is different from the original (obviously) and thus is
137100** only used for indexing, to improve performance. The original unaltered
137101** IN expression must also be run on each output row for correctness.
137102*/
137103static Expr *removeUnindexableInClauseTerms(
137104 Parse *pParse, /* The parsing context */
137105 int iEq, /* Look at loop terms starting here */
137106 WhereLoop *pLoop, /* The current loop */
137107 Expr *pX /* The IN expression to be reduced */
137108){
137109 sqlite3 *db = pParse->db;
137110 Expr *pNew = sqlite3ExprDup(db, pX, 0);
137111 if( db->mallocFailed==0 ){
137112 ExprList *pOrigRhs = pNew->x.pSelect->pEList; /* Original unmodified RHS */
137113 ExprList *pOrigLhs = pNew->pLeft->x.pList; /* Original unmodified LHS */
137114 ExprList *pRhs = 0; /* New RHS after modifications */
137115 ExprList *pLhs = 0; /* New LHS after mods */
137116 int i; /* Loop counter */
137117 Select *pSelect; /* Pointer to the SELECT on the RHS */
137118
137119 for(i=iEq; i<pLoop->nLTerm; i++){
137120 if( pLoop->aLTerm[i]->pExpr==pX ){
137121 int iField = pLoop->aLTerm[i]->iField - 1;
137122 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
137123 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
137124 pOrigRhs->a[iField].pExpr = 0;
137125 assert( pOrigLhs->a[iField].pExpr!=0 )((void) (0));
137126 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
137127 pOrigLhs->a[iField].pExpr = 0;
137128 }
137129 }
137130 sqlite3ExprListDelete(db, pOrigRhs);
137131 sqlite3ExprListDelete(db, pOrigLhs);
137132 pNew->pLeft->x.pList = pLhs;
137133 pNew->x.pSelect->pEList = pRhs;
137134 if( pLhs && pLhs->nExpr==1 ){
137135 /* Take care here not to generate a TK_VECTOR containing only a
137136 ** single value. Since the parser never creates such a vector, some
137137 ** of the subroutines do not handle this case. */
137138 Expr *p = pLhs->a[0].pExpr;
137139 pLhs->a[0].pExpr = 0;
137140 sqlite3ExprDelete(db, pNew->pLeft);
137141 pNew->pLeft = p;
137142 }
137143 pSelect = pNew->x.pSelect;
137144 if( pSelect->pOrderBy ){
137145 /* If the SELECT statement has an ORDER BY clause, zero the
137146 ** iOrderByCol variables. These are set to non-zero when an
137147 ** ORDER BY term exactly matches one of the terms of the
137148 ** result-set. Since the result-set of the SELECT statement may
137149 ** have been modified or reordered, these variables are no longer
137150 ** set correctly. Since setting them is just an optimization,
137151 ** it's easiest just to zero them here. */
137152 ExprList *pOrderBy = pSelect->pOrderBy;
137153 for(i=0; i<pOrderBy->nExpr; i++){
137154 pOrderBy->a[i].u.x.iOrderByCol = 0;
137155 }
137156 }
137157
137158#if 0
137159 printf("For indexing, change the IN expr:\n");
137160 sqlite3TreeViewExpr(0, pX, 0);
137161 printf("Into:\n");
137162 sqlite3TreeViewExpr(0, pNew, 0);
137163#endif
137164 }
137165 return pNew;
137166}
137167
137168
137169/*
137170** Generate code for a single equality term of the WHERE clause. An equality
137171** term can be either X=expr or X IN (...). pTerm is the term to be
137172** coded.
137173**
137174** The current value for the constraint is left in a register, the index
137175** of which is returned. An attempt is made store the result in iTarget but
137176** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the
137177** constraint is a TK_EQ or TK_IS, then the current value might be left in
137178** some other register and it is the caller's responsibility to compensate.
137179**
137180** For a constraint of the form X=expr, the expression is evaluated in
137181** straight-line code. For constraints of the form X IN (...)
137182** this routine sets up a loop that will iterate over all values of X.
137183*/
137184static int codeEqualityTerm(
137185 Parse *pParse, /* The parsing context */
137186 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
137187 WhereLevel *pLevel, /* The level of the FROM clause we are working on */
137188 int iEq, /* Index of the equality term within this level */
137189 int bRev, /* True for reverse-order IN operations */
137190 int iTarget /* Attempt to leave results in this register */
137191){
137192 Expr *pX = pTerm->pExpr;
137193 Vdbe *v = pParse->pVdbe;
137194 int iReg; /* Register holding results */
137195
137196 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm )((void) (0));
137197 assert( iTarget>0 )((void) (0));
137198 if( pX->op==TK_EQ53 || pX->op==TK_IS45 ){
137199 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
137200 }else if( pX->op==TK_ISNULL50 ){
137201 iReg = iTarget;
137202 sqlite3VdbeAddOp2(v, OP_Null73, 0, iReg);
137203#ifndef SQLITE_OMIT_SUBQUERY
137204 }else{
137205 int eType = IN_INDEX_NOOP5;
137206 int iTab;
137207 struct InLoop *pIn;
137208 WhereLoop *pLoop = pLevel->pWLoop;
137209 int i;
137210 int nEq = 0;
137211 int *aiMap = 0;
137212
137213 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0
137214 && pLoop->u.btree.pIndex!=0
137215 && pLoop->u.btree.pIndex->aSortOrder[iEq]
137216 ){
137217 testcase( iEq==0 );
137218 testcase( bRev );
137219 bRev = !bRev;
137220 }
137221 assert( pX->op==TK_IN )((void) (0));
137222 iReg = iTarget;
137223
137224 for(i=0; i<iEq; i++){
137225 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
137226 disableTerm(pLevel, pTerm);
137227 return iTarget;
137228 }
137229 }
137230 for(i=iEq;i<pLoop->nLTerm; i++){
137231 assert( pLoop->aLTerm[i]!=0 )((void) (0));
137232 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
137233 }
137234
137235 iTab = 0;
137236 if( (pX->flags & EP_xIsSelect0x000800)==0 || pX->x.pSelect->pEList->nExpr==1 ){
137237 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP0x0004, 0, 0, &iTab);
137238 }else{
137239 sqlite3 *db = pParse->db;
137240 pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
137241
137242 if( !db->mallocFailed ){
137243 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
137244 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP0x0004, 0, aiMap, &iTab);
137245 pTerm->pExpr->iTable = iTab;
137246 }
137247 sqlite3ExprDelete(db, pX);
137248 pX = pTerm->pExpr;
137249 }
137250
137251 if( eType==IN_INDEX_INDEX_DESC4 ){
137252 testcase( bRev );
137253 bRev = !bRev;
137254 }
137255 sqlite3VdbeAddOp2(v, bRev ? OP_Last32 : OP_Rewind36, iTab, 0);
137256 VdbeCoverageIf(v, bRev);
137257 VdbeCoverageIf(v, !bRev);
137258 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 )((void) (0));
137259
137260 pLoop->wsFlags |= WHERE_IN_ABLE0x00000800;
137261 if( pLevel->u.in.nIn==0 ){
137262 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
137263 }
137264
137265 i = pLevel->u.in.nIn;
137266 pLevel->u.in.nIn += nEq;
137267 pLevel->u.in.aInLoop =
137268 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
137269 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
137270 pIn = pLevel->u.in.aInLoop;
137271 if( pIn ){
137272 int iMap = 0; /* Index in aiMap[] */
137273 pIn += i;
137274 for(i=iEq;i<pLoop->nLTerm; i++){
137275 if( pLoop->aLTerm[i]->pExpr==pX ){
137276 int iOut = iReg + i - iEq;
137277 if( eType==IN_INDEX_ROWID1 ){
137278 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid128, iTab, iOut);
137279 }else{
137280 int iCol = aiMap ? aiMap[iMap++] : 0;
137281 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column90,iTab, iCol, iOut);
137282 }
137283 sqlite3VdbeAddOp1(v, OP_IsNull50, iOut); VdbeCoverage(v);
137284 if( i==iEq ){
137285 pIn->iCur = iTab;
137286 pIn->eEndLoopOp = bRev ? OP_Prev4 : OP_Next5;
137287 if( iEq>0 && (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 ){
137288 pIn->iBase = iReg - i;
137289 pIn->nPrefix = i;
137290 pLoop->wsFlags |= WHERE_IN_EARLYOUT0x00040000;
137291 }else{
137292 pIn->nPrefix = 0;
137293 }
137294 }else{
137295 pIn->eEndLoopOp = OP_Noop170;
137296 }
137297 pIn++;
137298 }
137299 }
137300 }else{
137301 pLevel->u.in.nIn = 0;
137302 }
137303 sqlite3DbFree(pParse->db, aiMap);
137304#endif
137305 }
137306 disableTerm(pLevel, pTerm);
137307 return iReg;
137308}
137309
137310/*
137311** Generate code that will evaluate all == and IN constraints for an
137312** index scan.
137313**
137314** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
137315** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
137316** The index has as many as three equality constraints, but in this
137317** example, the third "c" value is an inequality. So only two
137318** constraints are coded. This routine will generate code to evaluate
137319** a==5 and b IN (1,2,3). The current values for a and b will be stored
137320** in consecutive registers and the index of the first register is returned.
137321**
137322** In the example above nEq==2. But this subroutine works for any value
137323** of nEq including 0. If nEq==0, this routine is nearly a no-op.
137324** The only thing it does is allocate the pLevel->iMem memory cell and
137325** compute the affinity string.
137326**
137327** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints
137328** are == or IN and are covered by the nEq. nExtraReg is 1 if there is
137329** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
137330** occurs after the nEq quality constraints.
137331**
137332** This routine allocates a range of nEq+nExtraReg memory cells and returns
137333** the index of the first memory cell in that range. The code that
137334** calls this routine will use that memory range to store keys for
137335** start and termination conditions of the loop.
137336** key value of the loop. If one or more IN operators appear, then
137337** this routine allocates an additional nEq memory cells for internal
137338** use.
137339**
137340** Before returning, *pzAff is set to point to a buffer containing a
137341** copy of the column affinity string of the index allocated using
137342** sqlite3DbMalloc(). Except, entries in the copy of the string associated
137343** with equality constraints that use BLOB or NONE affinity are set to
137344** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
137345**
137346** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
137347** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
137348**
137349** In the example above, the index on t1(a) has TEXT affinity. But since
137350** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
137351** no conversion should be attempted before using a t2.b value as part of
137352** a key to search the index. Hence the first byte in the returned affinity
137353** string in this example would be set to SQLITE_AFF_BLOB.
137354*/
137355static int codeAllEqualityTerms(
137356 Parse *pParse, /* Parsing context */
137357 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */
137358 int bRev, /* Reverse the order of IN operators */
137359 int nExtraReg, /* Number of extra registers to allocate */
137360 char **pzAff /* OUT: Set to point to affinity string */
137361){
137362 u16 nEq; /* The number of == or IN constraints to code */
137363 u16 nSkip; /* Number of left-most columns to skip */
137364 Vdbe *v = pParse->pVdbe; /* The vm under construction */
137365 Index *pIdx; /* The index being used for this loop */
137366 WhereTerm *pTerm; /* A single constraint term */
137367 WhereLoop *pLoop; /* The WhereLoop object */
137368 int j; /* Loop counter */
137369 int regBase; /* Base register */
137370 int nReg; /* Number of registers to allocate */
137371 char *zAff; /* Affinity string to return */
137372
137373 /* This module is only called on query plans that use an index. */
137374 pLoop = pLevel->pWLoop;
137375 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
137376 nEq = pLoop->u.btree.nEq;
137377 nSkip = pLoop->nSkip;
137378 pIdx = pLoop->u.btree.pIndex;
137379 assert( pIdx!=0 )((void) (0));
137380
137381 /* Figure out how many memory cells we will need then allocate them.
137382 */
137383 regBase = pParse->nMem + 1;
137384 nReg = pLoop->u.btree.nEq + nExtraReg;
137385 pParse->nMem += nReg;
137386
137387 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
137388 assert( zAff!=0 || pParse->db->mallocFailed )((void) (0));
137389
137390 if( nSkip ){
137391 int iIdxCur = pLevel->iIdxCur;
137392 sqlite3VdbeAddOp1(v, (bRev?OP_Last32:OP_Rewind36), iIdxCur);
137393 VdbeCoverageIf(v, bRev==0);
137394 VdbeCoverageIf(v, bRev!=0);
137395 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
137396 j = sqlite3VdbeAddOp0(v, OP_Goto11);
137397 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT22:OP_SeekGT25),
137398 iIdxCur, 0, regBase, nSkip);
137399 VdbeCoverageIf(v, bRev==0);
137400 VdbeCoverageIf(v, bRev!=0);
137401 sqlite3VdbeJumpHere(v, j);
137402 for(j=0; j<nSkip; j++){
137403 sqlite3VdbeAddOp3(v, OP_Column90, iIdxCur, j, regBase+j);
137404 testcase( pIdx->aiColumn[j]==XN_EXPR );
137405 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
137406 }
137407 }
137408
137409 /* Evaluate the equality constraints
137410 */
137411 assert( zAff==0 || (int)strlen(zAff)>=nEq )((void) (0));
137412 for(j=nSkip; j<nEq; j++){
137413 int r1;
137414 pTerm = pLoop->aLTerm[j];
137415 assert( pTerm!=0 )((void) (0));
137416 /* The following testcase is true for indices with redundant columns.
137417 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
137418 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
137419 testcase( pTerm->wtFlags & TERM_VIRTUAL );
137420 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
137421 if( r1!=regBase+j ){
137422 if( nReg==1 ){
137423 sqlite3ReleaseTempReg(pParse, regBase);
137424 regBase = r1;
137425 }else{
137426 sqlite3VdbeAddOp2(v, OP_SCopy79, r1, regBase+j);
137427 }
137428 }
137429 if( pTerm->eOperator & WO_IN0x0001 ){
137430 if( pTerm->pExpr->flags & EP_xIsSelect0x000800 ){
137431 /* No affinity ever needs to be (or should be) applied to a value
137432 ** from the RHS of an "? IN (SELECT ...)" expression. The
137433 ** sqlite3FindInIndex() routine has already ensured that the
137434 ** affinity of the comparison has been applied to the value. */
137435 if( zAff ) zAff[j] = SQLITE_AFF_BLOB'A';
137436 }
137437 }else if( (pTerm->eOperator & WO_ISNULL0x0100)==0 ){
137438 Expr *pRight = pTerm->pExpr->pRight;
137439 if( (pTerm->wtFlags & TERM_IS0x800)==0 && sqlite3ExprCanBeNull(pRight) ){
137440 sqlite3VdbeAddOp2(v, OP_IsNull50, regBase+j, pLevel->addrBrk);
137441 VdbeCoverage(v);
137442 }
137443 if( zAff ){
137444 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB'A' ){
137445 zAff[j] = SQLITE_AFF_BLOB'A';
137446 }
137447 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
137448 zAff[j] = SQLITE_AFF_BLOB'A';
137449 }
137450 }
137451 }
137452 }
137453 *pzAff = zAff;
137454 return regBase;
137455}
137456
137457#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
137458/*
137459** If the most recently coded instruction is a constant range constraint
137460** (a string literal) that originated from the LIKE optimization, then
137461** set P3 and P5 on the OP_String opcode so that the string will be cast
137462** to a BLOB at appropriate times.
137463**
137464** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
137465** expression: "x>='ABC' AND x<'abd'". But this requires that the range
137466** scan loop run twice, once for strings and a second time for BLOBs.
137467** The OP_String opcodes on the second pass convert the upper and lower
137468** bound string constants to blobs. This routine makes the necessary changes
137469** to the OP_String opcodes for that to happen.
137470**
137471** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then
137472** only the one pass through the string space is required, so this routine
137473** becomes a no-op.
137474*/
137475static void whereLikeOptimizationStringFixup(
137476 Vdbe *v, /* prepared statement under construction */
137477 WhereLevel *pLevel, /* The loop that contains the LIKE operator */
137478 WhereTerm *pTerm /* The upper or lower bound just coded */
137479){
137480 if( pTerm->wtFlags & TERM_LIKEOPT0x100 ){
137481 VdbeOp *pOp;
137482 assert( pLevel->iLikeRepCntr>0 )((void) (0));
137483 pOp = sqlite3VdbeGetOp(v, -1);
137484 assert( pOp!=0 )((void) (0));
137485 assert( pOp->opcode==OP_String8((void) (0))
137486 || pTerm->pWC->pWInfo->pParse->db->mallocFailed )((void) (0));
137487 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
137488 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
137489 }
137490}
137491#else
137492# define whereLikeOptimizationStringFixup(A,B,C)
137493#endif
137494
137495#ifdef SQLITE_ENABLE_CURSOR_HINTS
137496/*
137497** Information is passed from codeCursorHint() down to individual nodes of
137498** the expression tree (by sqlite3WalkExpr()) using an instance of this
137499** structure.
137500*/
137501struct CCurHint {
137502 int iTabCur; /* Cursor for the main table */
137503 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */
137504 Index *pIdx; /* The index used to access the table */
137505};
137506
137507/*
137508** This function is called for every node of an expression that is a candidate
137509** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference
137510** the table CCurHint.iTabCur, verify that the same column can be
137511** accessed through the index. If it cannot, then set pWalker->eCode to 1.
137512*/
137513static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
137514 struct CCurHint *pHint = pWalker->u.pCCurHint;
137515 assert( pHint->pIdx!=0 )((void) (0));
137516 if( pExpr->op==TK_COLUMN162
137517 && pExpr->iTable==pHint->iTabCur
137518 && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0
137519 ){
137520 pWalker->eCode = 1;
137521 }
137522 return WRC_Continue0;
137523}
137524
137525/*
137526** Test whether or not expression pExpr, which was part of a WHERE clause,
137527** should be included in the cursor-hint for a table that is on the rhs
137528** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
137529** expression is not suitable.
137530**
137531** An expression is unsuitable if it might evaluate to non NULL even if
137532** a TK_COLUMN node that does affect the value of the expression is set
137533** to NULL. For example:
137534**
137535** col IS NULL
137536** col IS NOT NULL
137537** coalesce(col, 1)
137538** CASE WHEN col THEN 0 ELSE 1 END
137539*/
137540static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
137541 if( pExpr->op==TK_IS45
137542 || pExpr->op==TK_ISNULL50 || pExpr->op==TK_ISNOT160
137543 || pExpr->op==TK_NOTNULL51 || pExpr->op==TK_CASE149
137544 ){
137545 pWalker->eCode = 1;
137546 }else if( pExpr->op==TK_FUNCTION161 ){
137547 int d1;
137548 char d2[4];
137549 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
137550 pWalker->eCode = 1;
137551 }
137552 }
137553
137554 return WRC_Continue0;
137555}
137556
137557
137558/*
137559** This function is called on every node of an expression tree used as an
137560** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN
137561** that accesses any table other than the one identified by
137562** CCurHint.iTabCur, then do the following:
137563**
137564** 1) allocate a register and code an OP_Column instruction to read
137565** the specified column into the new register, and
137566**
137567** 2) transform the expression node to a TK_REGISTER node that reads
137568** from the newly populated register.
137569**
137570** Also, if the node is a TK_COLUMN that does access the table idenified
137571** by pCCurHint.iTabCur, and an index is being used (which we will
137572** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into
137573** an access of the index rather than the original table.
137574*/
137575static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
137576 int rc = WRC_Continue0;
137577 struct CCurHint *pHint = pWalker->u.pCCurHint;
137578 if( pExpr->op==TK_COLUMN162 ){
137579 if( pExpr->iTable!=pHint->iTabCur ){
137580 int reg = ++pWalker->pParse->nMem; /* Register for column value */
137581 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
137582 pExpr->op = TK_REGISTER168;
137583 pExpr->iTable = reg;
137584 }else if( pHint->pIdx!=0 ){
137585 pExpr->iTable = pHint->iIdxCur;
137586 pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn);
137587 assert( pExpr->iColumn>=0 )((void) (0));
137588 }
137589 }else if( pExpr->op==TK_AGG_FUNCTION163 ){
137590 /* An aggregate function in the WHERE clause of a query means this must
137591 ** be a correlated sub-query, and expression pExpr is an aggregate from
137592 ** the parent context. Do not walk the function arguments in this case.
137593 **
137594 ** todo: It should be possible to replace this node with a TK_REGISTER
137595 ** expression, as the result of the expression must be stored in a
137596 ** register at this point. The same holds for TK_AGG_COLUMN nodes. */
137597 rc = WRC_Prune1;
137598 }
137599 return rc;
137600}
137601
137602/*
137603** Insert an OP_CursorHint instruction if it is appropriate to do so.
137604*/
137605static void codeCursorHint(
137606 struct SrcList_item *pTabItem, /* FROM clause item */
137607 WhereInfo *pWInfo, /* The where clause */
137608 WhereLevel *pLevel, /* Which loop to provide hints for */
137609 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
137610){
137611 Parse *pParse = pWInfo->pParse;
137612 sqlite3 *db = pParse->db;
137613 Vdbe *v = pParse->pVdbe;
137614 Expr *pExpr = 0;
137615 WhereLoop *pLoop = pLevel->pWLoop;
137616 int iCur;
137617 WhereClause *pWC;
137618 WhereTerm *pTerm;
137619 int i, j;
137620 struct CCurHint sHint;
137621 Walker sWalker;
137622
137623 if( OptimizationDisabled(db, SQLITE_CursorHints)(((db)->dbOptFlags&(0x0400))!=0) ) return;
137624 iCur = pLevel->iTabCur;
137625 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor )((void) (0));
137626 sHint.iTabCur = iCur;
137627 sHint.iIdxCur = pLevel->iIdxCur;
137628 sHint.pIdx = pLoop->u.btree.pIndex;
137629 memset(&sWalker, 0, sizeof(sWalker));
137630 sWalker.pParse = pParse;
137631 sWalker.u.pCCurHint = &sHint;
137632 pWC = &pWInfo->sWC;
137633 for(i=0; i<pWC->nTerm; i++){
137634 pTerm = &pWC->a[i];
137635 if( pTerm->wtFlags & (TERM_VIRTUAL0x02|TERM_CODED0x04) ) continue;
137636 if( pTerm->prereqAll & pLevel->notReady ) continue;
137637
137638 /* Any terms specified as part of the ON(...) clause for any LEFT
137639 ** JOIN for which the current table is not the rhs are omitted
137640 ** from the cursor-hint.
137641 **
137642 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
137643 ** that were specified as part of the WHERE clause must be excluded.
137644 ** This is to address the following:
137645 **
137646 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
137647 **
137648 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
137649 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
137650 ** pushed down to the cursor, this row is filtered out, causing
137651 ** SQLite to synthesize a row of NULL values. Which does match the
137652 ** WHERE clause, and so the query returns a row. Which is incorrect.
137653 **
137654 ** For the same reason, WHERE terms such as:
137655 **
137656 ** WHERE 1 = (t2.c IS NULL)
137657 **
137658 ** are also excluded. See codeCursorHintIsOrFunction() for details.
137659 */
137660 if( pTabItem->fg.jointype & JT_LEFT0x0008 ){
137661 Expr *pExpr = pTerm->pExpr;
137662 if( !ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0)
137663 || pExpr->iRightJoinTable!=pTabItem->iCursor
137664 ){
137665 sWalker.eCode = 0;
137666 sWalker.xExprCallback = codeCursorHintIsOrFunction;
137667 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
137668 if( sWalker.eCode ) continue;
137669 }
137670 }else{
137671 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin)(((pTerm->pExpr)->flags&(0x000001))!=0) ) continue;
137672 }
137673
137674 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
137675 ** the cursor. These terms are not needed as hints for a pure range
137676 ** scan (that has no == terms) so omit them. */
137677 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
137678 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
137679 if( j<pLoop->nLTerm ) continue;
137680 }
137681
137682 /* No subqueries or non-deterministic functions allowed */
137683 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
137684
137685 /* For an index scan, make sure referenced columns are actually in
137686 ** the index. */
137687 if( sHint.pIdx!=0 ){
137688 sWalker.eCode = 0;
137689 sWalker.xExprCallback = codeCursorHintCheckExpr;
137690 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
137691 if( sWalker.eCode ) continue;
137692 }
137693
137694 /* If we survive all prior tests, that means this term is worth hinting */
137695 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
137696 }
137697 if( pExpr!=0 ){
137698 sWalker.xExprCallback = codeCursorHintFixExpr;
137699 sqlite3WalkExpr(&sWalker, pExpr);
137700 sqlite3VdbeAddOp4(v, OP_CursorHint169,
137701 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
137702 (const char*)pExpr, P4_EXPR(-10));
137703 }
137704}
137705#else
137706# define codeCursorHint(A,B,C,D) /* No-op */
137707#endif /* SQLITE_ENABLE_CURSOR_HINTS */
137708
137709/*
137710** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
137711** a rowid value just read from cursor iIdxCur, open on index pIdx. This
137712** function generates code to do a deferred seek of cursor iCur to the
137713** rowid stored in register iRowid.
137714**
137715** Normally, this is just:
137716**
137717** OP_DeferredSeek $iCur $iRowid
137718**
137719** However, if the scan currently being coded is a branch of an OR-loop and
137720** the statement currently being coded is a SELECT, then P3 of OP_DeferredSeek
137721** is set to iIdxCur and P4 is set to point to an array of integers
137722** containing one entry for each column of the table cursor iCur is open
137723** on. For each table column, if the column is the i'th column of the
137724** index, then the corresponding array entry is set to (i+1). If the column
137725** does not appear in the index at all, the array entry is set to 0.
137726*/
137727static void codeDeferredSeek(
137728 WhereInfo *pWInfo, /* Where clause context */
137729 Index *pIdx, /* Index scan is using */
137730 int iCur, /* Cursor for IPK b-tree */
137731 int iIdxCur /* Index cursor */
137732){
137733 Parse *pParse = pWInfo->pParse; /* Parse context */
137734 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
137735
137736 assert( iIdxCur>0 )((void) (0));
137737 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 )((void) (0));
137738
137739 sqlite3VdbeAddOp3(v, OP_DeferredSeek134, iIdxCur, 0, iCur);
137740 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)
137741 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)(((pParse)->pToplevel ? (pParse)->pToplevel : (pParse))
->writeMask)==0
137742 ){
137743 int i;
137744 Table *pTab = pIdx->pTable;
137745 int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1));
137746 if( ai ){
137747 ai[0] = pTab->nCol;
137748 for(i=0; i<pIdx->nColumn-1; i++){
137749 assert( pIdx->aiColumn[i]<pTab->nCol )((void) (0));
137750 if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1;
137751 }
137752 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY(-15));
137753 }
137754 }
137755}
137756
137757/*
137758** If the expression passed as the second argument is a vector, generate
137759** code to write the first nReg elements of the vector into an array
137760** of registers starting with iReg.
137761**
137762** If the expression is not a vector, then nReg must be passed 1. In
137763** this case, generate code to evaluate the expression and leave the
137764** result in register iReg.
137765*/
137766static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
137767 assert( nReg>0 )((void) (0));
137768 if( p && sqlite3ExprIsVector(p) ){
137769#ifndef SQLITE_OMIT_SUBQUERY
137770 if( (p->flags & EP_xIsSelect0x000800) ){
137771 Vdbe *v = pParse->pVdbe;
137772 int iSelect;
137773 assert( p->op==TK_SELECT )((void) (0));
137774 iSelect = sqlite3CodeSubselect(pParse, p);
137775 sqlite3VdbeAddOp3(v, OP_Copy78, iSelect, iReg, nReg-1);
137776 }else
137777#endif
137778 {
137779 int i;
137780 ExprList *pList = p->x.pList;
137781 assert( nReg<=pList->nExpr )((void) (0));
137782 for(i=0; i<nReg; i++){
137783 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
137784 }
137785 }
137786 }else{
137787 assert( nReg==1 )((void) (0));
137788 sqlite3ExprCode(pParse, p, iReg);
137789 }
137790}
137791
137792/* An instance of the IdxExprTrans object carries information about a
137793** mapping from an expression on table columns into a column in an index
137794** down through the Walker.
137795*/
137796typedef struct IdxExprTrans {
137797 Expr *pIdxExpr; /* The index expression */
137798 int iTabCur; /* The cursor of the corresponding table */
137799 int iIdxCur; /* The cursor for the index */
137800 int iIdxCol; /* The column for the index */
137801} IdxExprTrans;
137802
137803/* The walker node callback used to transform matching expressions into
137804** a reference to an index column for an index on an expression.
137805**
137806** If pExpr matches, then transform it into a reference to the index column
137807** that contains the value of pExpr.
137808*/
137809static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
137810 IdxExprTrans *pX = p->u.pIdxTrans;
137811 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
137812 pExpr->op = TK_COLUMN162;
137813 pExpr->iTable = pX->iIdxCur;
137814 pExpr->iColumn = pX->iIdxCol;
137815 pExpr->y.pTab = 0;
137816 return WRC_Prune1;
137817 }else{
137818 return WRC_Continue0;
137819 }
137820}
137821
137822/*
137823** For an indexes on expression X, locate every instance of expression X
137824** in pExpr and change that subexpression into a reference to the appropriate
137825** column of the index.
137826*/
137827static void whereIndexExprTrans(
137828 Index *pIdx, /* The Index */
137829 int iTabCur, /* Cursor of the table that is being indexed */
137830 int iIdxCur, /* Cursor of the index itself */
137831 WhereInfo *pWInfo /* Transform expressions in this WHERE clause */
137832){
137833 int iIdxCol; /* Column number of the index */
137834 ExprList *aColExpr; /* Expressions that are indexed */
137835 Walker w;
137836 IdxExprTrans x;
137837 aColExpr = pIdx->aColExpr;
137838 if( aColExpr==0 ) return; /* Not an index on expressions */
137839 memset(&w, 0, sizeof(w));
137840 w.xExprCallback = whereIndexExprTransNode;
137841 w.u.pIdxTrans = &x;
137842 x.iTabCur = iTabCur;
137843 x.iIdxCur = iIdxCur;
137844 for(iIdxCol=0; iIdxCol<aColExpr->nExpr; iIdxCol++){
137845 if( pIdx->aiColumn[iIdxCol]!=XN_EXPR(-2) ) continue;
137846 assert( aColExpr->a[iIdxCol].pExpr!=0 )((void) (0));
137847 x.iIdxCol = iIdxCol;
137848 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
137849 sqlite3WalkExpr(&w, pWInfo->pWhere);
137850 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
137851 sqlite3WalkExprList(&w, pWInfo->pResultSet);
137852 }
137853}
137854
137855/*
137856** The pTruth expression is always true because it is the WHERE clause
137857** a partial index that is driving a query loop. Look through all of the
137858** WHERE clause terms on the query, and if any of those terms must be
137859** true because pTruth is true, then mark those WHERE clause terms as
137860** coded.
137861*/
137862static void whereApplyPartialIndexConstraints(
137863 Expr *pTruth,
137864 int iTabCur,
137865 WhereClause *pWC
137866){
137867 int i;
137868 WhereTerm *pTerm;
137869 while( pTruth->op==TK_AND44 ){
137870 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
137871 pTruth = pTruth->pRight;
137872 }
137873 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
137874 Expr *pExpr;
137875 if( pTerm->wtFlags & TERM_CODED0x04 ) continue;
137876 pExpr = pTerm->pExpr;
137877 if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){
137878 pTerm->wtFlags |= TERM_CODED0x04;
137879 }
137880 }
137881}
137882
137883/*
137884** Generate code for the start of the iLevel-th loop in the WHERE clause
137885** implementation described by pWInfo.
137886*/
137887SQLITE_PRIVATEstatic Bitmask sqlite3WhereCodeOneLoopStart(
137888 Parse *pParse, /* Parsing context */
137889 Vdbe *v, /* Prepared statement under construction */
137890 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
137891 int iLevel, /* Which level of pWInfo->a[] should be coded */
137892 WhereLevel *pLevel, /* The current level pointer */
137893 Bitmask notReady /* Which tables are currently available */
137894){
137895 int j, k; /* Loop counters */
137896 int iCur; /* The VDBE cursor for the table */
137897 int addrNxt; /* Where to jump to continue with the next IN case */
137898 int bRev; /* True if we need to scan in reverse order */
137899 WhereLoop *pLoop; /* The WhereLoop object being coded */
137900 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
137901 WhereTerm *pTerm; /* A WHERE clause term */
137902 sqlite3 *db; /* Database connection */
137903 struct SrcList_item *pTabItem; /* FROM clause term being coded */
137904 int addrBrk; /* Jump here to break out of the loop */
137905 int addrHalt; /* addrBrk for the outermost loop */
137906 int addrCont; /* Jump here to continue with next cycle */
137907 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
137908 int iReleaseReg = 0; /* Temp register to free before returning */
137909 Index *pIdx = 0; /* Index used by loop (if any) */
137910 int iLoop; /* Iteration of constraint generator loop */
137911
137912 pWC = &pWInfo->sWC;
137913 db = pParse->db;
137914 pLoop = pLevel->pWLoop;
137915 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
137916 iCur = pTabItem->iCursor;
137917 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
137918 bRev = (pWInfo->revMask>>iLevel)&1;
137919 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
137920
137921 /* Create labels for the "break" and "continue" instructions
137922 ** for the current loop. Jump to addrBrk to break out of a loop.
137923 ** Jump to cont to go immediately to the next iteration of the
137924 ** loop.
137925 **
137926 ** When there is an IN operator, we also have a "addrNxt" label that
137927 ** means to continue with the next IN value combination. When
137928 ** there are no IN operators in the constraints, the "addrNxt" label
137929 ** is the same as "addrBrk".
137930 */
137931 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
137932 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
137933
137934 /* If this is the right table of a LEFT OUTER JOIN, allocate and
137935 ** initialize a memory cell that records if this table matches any
137936 ** row of the left table of the join.
137937 */
137938 assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)((void) (0))
137939 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0((void) (0))
137940 )((void) (0));
137941 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT0x0008)!=0 ){
137942 pLevel->iLeftJoin = ++pParse->nMem;
137943 sqlite3VdbeAddOp2(v, OP_Integer70, 0, pLevel->iLeftJoin);
137944 VdbeComment((v, "init LEFT JOIN no-match flag"));
137945 }
137946
137947 /* Compute a safe address to jump to if we discover that the table for
137948 ** this loop is empty and can never contribute content. */
137949 for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
137950 addrHalt = pWInfo->a[j].addrBrk;
137951
137952 /* Special case of a FROM clause subquery implemented as a co-routine */
137953 if( pTabItem->fg.viaCoroutine ){
137954 int regYield = pTabItem->regReturn;
137955 sqlite3VdbeAddOp3(v, OP_InitCoroutine13, regYield, 0, pTabItem->addrFillSub);
137956 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield14, regYield, addrBrk);
137957 VdbeCoverage(v);
137958 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
137959 pLevel->op = OP_Goto11;
137960 }else
137961
137962#ifndef SQLITE_OMIT_VIRTUALTABLE
137963 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
137964 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
137965 ** to access the data.
137966 */
137967 int iReg; /* P3 Value for OP_VFilter */
137968 int addrNotFound;
137969 int nConstraint = pLoop->nLTerm;
137970 int iIn; /* Counter for IN constraints */
137971
137972 iReg = sqlite3GetTempRange(pParse, nConstraint+2);
137973 addrNotFound = pLevel->addrBrk;
137974 for(j=0; j<nConstraint; j++){
137975 int iTarget = iReg+j+2;
137976 pTerm = pLoop->aLTerm[j];
137977 if( NEVER(pTerm==0)(pTerm==0) ) continue;
137978 if( pTerm->eOperator & WO_IN0x0001 ){
137979 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
137980 addrNotFound = pLevel->addrNxt;
137981 }else{
137982 Expr *pRight = pTerm->pExpr->pRight;
137983 codeExprOrVector(pParse, pRight, iTarget, 1);
137984 }
137985 }
137986 sqlite3VdbeAddOp2(v, OP_Integer70, pLoop->u.vtab.idxNum, iReg);
137987 sqlite3VdbeAddOp2(v, OP_Integer70, nConstraint, iReg+1);
137988 sqlite3VdbeAddOp4(v, OP_VFilter9, iCur, addrNotFound, iReg,
137989 pLoop->u.vtab.idxStr,
137990 pLoop->u.vtab.needFree ? P4_DYNAMIC(-7) : P4_STATIC(-1));
137991 VdbeCoverage(v);
137992 pLoop->u.vtab.needFree = 0;
137993 pLevel->p1 = iCur;
137994 pLevel->op = pWInfo->eOnePass ? OP_Noop170 : OP_VNext60;
137995 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
137996 iIn = pLevel->u.in.nIn;
137997 for(j=nConstraint-1; j>=0; j--){
137998 pTerm = pLoop->aLTerm[j];
137999 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
138000 disableTerm(pLevel, pTerm);
138001 }else if( (pTerm->eOperator & WO_IN0x0001)!=0 ){
138002 Expr *pCompare; /* The comparison operator */
138003 Expr *pRight; /* RHS of the comparison */
138004 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
138005
138006 /* Reload the constraint value into reg[iReg+j+2]. The same value
138007 ** was loaded into the same register prior to the OP_VFilter, but
138008 ** the xFilter implementation might have changed the datatype or
138009 ** encoding of the value in the register, so it *must* be reloaded. */
138010 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed )((void) (0));
138011 if( !db->mallocFailed ){
138012 assert( iIn>0 )((void) (0));
138013 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[--iIn].addrInTop);
138014 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid )((void) (0));
138015 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 )((void) (0));
138016 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 )((void) (0));
138017 testcase( pOp->opcode==OP_Rowid );
138018 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
138019 }
138020
138021 /* Generate code that will continue to the next row if
138022 ** the IN constraint is not satisfied */
138023 pCompare = sqlite3PExpr(pParse, TK_EQ53, 0, 0);
138024 assert( pCompare!=0 || db->mallocFailed )((void) (0));
138025 if( pCompare ){
138026 pCompare->pLeft = pTerm->pExpr->pLeft;
138027 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER168, 0);
138028 if( pRight ){
138029 pRight->iTable = iReg+j+2;
138030 sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0);
138031 }
138032 pCompare->pLeft = 0;
138033 sqlite3ExprDelete(db, pCompare);
138034 }
138035 }
138036 }
138037 /* These registers need to be preserved in case there is an IN operator
138038 ** loop. So we could deallocate the registers here (and potentially
138039 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
138040 ** simpler and safer to simply not reuse the registers.
138041 **
138042 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
138043 */
138044 }else
138045#endif /* SQLITE_OMIT_VIRTUALTABLE */
138046
138047 if( (pLoop->wsFlags & WHERE_IPK0x00000100)!=0
138048 && (pLoop->wsFlags & (WHERE_COLUMN_IN0x00000004|WHERE_COLUMN_EQ0x00000001))!=0
138049 ){
138050 /* Case 2: We can directly reference a single row using an
138051 ** equality comparison against the ROWID field. Or
138052 ** we reference multiple rows using a "rowid IN (...)"
138053 ** construct.
138054 */
138055 assert( pLoop->u.btree.nEq==1 )((void) (0));
138056 pTerm = pLoop->aLTerm[0];
138057 assert( pTerm!=0 )((void) (0));
138058 assert( pTerm->pExpr!=0 )((void) (0));
138059 testcase( pTerm->wtFlags & TERM_VIRTUAL );
138060 iReleaseReg = ++pParse->nMem;
138061 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
138062 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
138063 addrNxt = pLevel->addrNxt;
138064 sqlite3VdbeAddOp3(v, OP_SeekRowid30, iCur, addrNxt, iRowidReg);
138065 VdbeCoverage(v);
138066 pLevel->op = OP_Noop170;
138067 if( (pTerm->prereqAll & pLevel->notReady)==0 ){
138068 pTerm->wtFlags |= TERM_CODED0x04;
138069 }
138070 }else if( (pLoop->wsFlags & WHERE_IPK0x00000100)!=0
138071 && (pLoop->wsFlags & WHERE_COLUMN_RANGE0x00000002)!=0
138072 ){
138073 /* Case 3: We have an inequality comparison against the ROWID field.
138074 */
138075 int testOp = OP_Noop170;
138076 int start;
138077 int memEndValue = 0;
138078 WhereTerm *pStart, *pEnd;
138079
138080 j = 0;
138081 pStart = pEnd = 0;
138082 if( pLoop->wsFlags & WHERE_BTM_LIMIT0x00000020 ) pStart = pLoop->aLTerm[j++];
138083 if( pLoop->wsFlags & WHERE_TOP_LIMIT0x00000010 ) pEnd = pLoop->aLTerm[j++];
138084 assert( pStart!=0 || pEnd!=0 )((void) (0));
138085 if( bRev ){
138086 pTerm = pStart;
138087 pStart = pEnd;
138088 pEnd = pTerm;
138089 }
138090 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
138091 if( pStart ){
138092 Expr *pX; /* The expression that defines the start bound */
138093 int r1, rTemp; /* Registers for holding the start boundary */
138094 int op; /* Cursor seek operation */
138095
138096 /* The following constant maps TK_xx codes into corresponding
138097 ** seek opcodes. It depends on a particular ordering of TK_xx
138098 */
138099 const u8 aMoveOp[] = {
138100 /* TK_GT */ OP_SeekGT25,
138101 /* TK_LE */ OP_SeekLE23,
138102 /* TK_LT */ OP_SeekLT22,
138103 /* TK_GE */ OP_SeekGE24
138104 };
138105 assert( TK_LE==TK_GT+1 )((void) (0)); /* Make sure the ordering.. */
138106 assert( TK_LT==TK_GT+2 )((void) (0)); /* ... of the TK_xx values... */
138107 assert( TK_GE==TK_GT+3 )((void) (0)); /* ... is correcct. */
138108
138109 assert( (pStart->wtFlags & TERM_VNULL)==0 )((void) (0));
138110 testcase( pStart->wtFlags & TERM_VIRTUAL );
138111 pX = pStart->pExpr;
138112 assert( pX!=0 )((void) (0));
138113 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
138114 if( sqlite3ExprIsVector(pX->pRight) ){
138115 r1 = rTemp = sqlite3GetTempReg(pParse);
138116 codeExprOrVector(pParse, pX->pRight, r1, 1);
138117 testcase( pX->op==TK_GT );
138118 testcase( pX->op==TK_GE );
138119 testcase( pX->op==TK_LT );
138120 testcase( pX->op==TK_LE );
138121 op = aMoveOp[((pX->op - TK_GT54 - 1) & 0x3) | 0x1];
138122 assert( pX->op!=TK_GT || op==OP_SeekGE )((void) (0));
138123 assert( pX->op!=TK_GE || op==OP_SeekGE )((void) (0));
138124 assert( pX->op!=TK_LT || op==OP_SeekLE )((void) (0));
138125 assert( pX->op!=TK_LE || op==OP_SeekLE )((void) (0));
138126 }else{
138127 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
138128 disableTerm(pLevel, pStart);
138129 op = aMoveOp[(pX->op - TK_GT54)];
138130 }
138131 sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
138132 VdbeComment((v, "pk"));
138133 VdbeCoverageIf(v, pX->op==TK_GT);
138134 VdbeCoverageIf(v, pX->op==TK_LE);
138135 VdbeCoverageIf(v, pX->op==TK_LT);
138136 VdbeCoverageIf(v, pX->op==TK_GE);
138137 sqlite3ReleaseTempReg(pParse, rTemp);
138138 }else{
138139 sqlite3VdbeAddOp2(v, bRev ? OP_Last32 : OP_Rewind36, iCur, addrHalt);
138140 VdbeCoverageIf(v, bRev==0);
138141 VdbeCoverageIf(v, bRev!=0);
138142 }
138143 if( pEnd ){
138144 Expr *pX;
138145 pX = pEnd->pExpr;
138146 assert( pX!=0 )((void) (0));
138147 assert( (pEnd->wtFlags & TERM_VNULL)==0 )((void) (0));
138148 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
138149 testcase( pEnd->wtFlags & TERM_VIRTUAL );
138150 memEndValue = ++pParse->nMem;
138151 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
138152 if( 0==sqlite3ExprIsVector(pX->pRight)
138153 && (pX->op==TK_LT56 || pX->op==TK_GT54)
138154 ){
138155 testOp = bRev ? OP_Le55 : OP_Ge57;
138156 }else{
138157 testOp = bRev ? OP_Lt56 : OP_Gt54;
138158 }
138159 if( 0==sqlite3ExprIsVector(pX->pRight) ){
138160 disableTerm(pLevel, pEnd);
138161 }
138162 }
138163 start = sqlite3VdbeCurrentAddr(v);
138164 pLevel->op = bRev ? OP_Prev4 : OP_Next5;
138165 pLevel->p1 = iCur;
138166 pLevel->p2 = start;
138167 assert( pLevel->p5==0 )((void) (0));
138168 if( testOp!=OP_Noop170 ){
138169 iRowidReg = ++pParse->nMem;
138170 sqlite3VdbeAddOp2(v, OP_Rowid128, iCur, iRowidReg);
138171 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
138172 VdbeCoverageIf(v, testOp==OP_Le);
138173 VdbeCoverageIf(v, testOp==OP_Lt);
138174 VdbeCoverageIf(v, testOp==OP_Ge);
138175 VdbeCoverageIf(v, testOp==OP_Gt);
138176 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC'C' | SQLITE_JUMPIFNULL0x10);
138177 }
138178 }else if( pLoop->wsFlags & WHERE_INDEXED0x00000200 ){
138179 /* Case 4: A scan using an index.
138180 **
138181 ** The WHERE clause may contain zero or more equality
138182 ** terms ("==" or "IN" operators) that refer to the N
138183 ** left-most columns of the index. It may also contain
138184 ** inequality constraints (>, <, >= or <=) on the indexed
138185 ** column that immediately follows the N equalities. Only
138186 ** the right-most column can be an inequality - the rest must
138187 ** use the "==" and "IN" operators. For example, if the
138188 ** index is on (x,y,z), then the following clauses are all
138189 ** optimized:
138190 **
138191 ** x=5
138192 ** x=5 AND y=10
138193 ** x=5 AND y<10
138194 ** x=5 AND y>5 AND y<10
138195 ** x=5 AND y=5 AND z<=10
138196 **
138197 ** The z<10 term of the following cannot be used, only
138198 ** the x=5 term:
138199 **
138200 ** x=5 AND z<10
138201 **
138202 ** N may be zero if there are inequality constraints.
138203 ** If there are no inequality constraints, then N is at
138204 ** least one.
138205 **
138206 ** This case is also used when there are no WHERE clause
138207 ** constraints but an index is selected anyway, in order
138208 ** to force the output order to conform to an ORDER BY.
138209 */
138210 static const u8 aStartOp[] = {
138211 0,
138212 0,
138213 OP_Rewind36, /* 2: (!start_constraints && startEq && !bRev) */
138214 OP_Last32, /* 3: (!start_constraints && startEq && bRev) */
138215 OP_SeekGT25, /* 4: (start_constraints && !startEq && !bRev) */
138216 OP_SeekLT22, /* 5: (start_constraints && !startEq && bRev) */
138217 OP_SeekGE24, /* 6: (start_constraints && startEq && !bRev) */
138218 OP_SeekLE23 /* 7: (start_constraints && startEq && bRev) */
138219 };
138220 static const u8 aEndOp[] = {
138221 OP_IdxGE40, /* 0: (end_constraints && !bRev && !endEq) */
138222 OP_IdxGT38, /* 1: (end_constraints && !bRev && endEq) */
138223 OP_IdxLE37, /* 2: (end_constraints && bRev && !endEq) */
138224 OP_IdxLT39, /* 3: (end_constraints && bRev && endEq) */
138225 };
138226 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
138227 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
138228 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
138229 int regBase; /* Base register holding constraint values */
138230 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */
138231 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
138232 int startEq; /* True if range start uses ==, >= or <= */
138233 int endEq; /* True if range end uses ==, >= or <= */
138234 int start_constraints; /* Start of range is constrained */
138235 int nConstraint; /* Number of constraint terms */
138236 int iIdxCur; /* The VDBE cursor for the index */
138237 int nExtraReg = 0; /* Number of extra registers needed */
138238 int op; /* Instruction opcode */
138239 char *zStartAff; /* Affinity for start of range constraint */
138240 char *zEndAff = 0; /* Affinity for end of range constraint */
138241 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
138242 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
138243 int omitTable; /* True if we use the index only */
138244
138245
138246 pIdx = pLoop->u.btree.pIndex;
138247 iIdxCur = pLevel->iIdxCur;
138248 assert( nEq>=pLoop->nSkip )((void) (0));
138249
138250 /* If this loop satisfies a sort order (pOrderBy) request that
138251 ** was passed to this function to implement a "SELECT min(x) ..."
138252 ** query, then the caller will only allow the loop to run for
138253 ** a single iteration. This means that the first row returned
138254 ** should not have a NULL value stored in 'x'. If column 'x' is
138255 ** the first one after the nEq equality constraints in the index,
138256 ** this requires some special handling.
138257 */
138258 assert( pWInfo->pOrderBy==0((void) (0))
138259 || pWInfo->pOrderBy->nExpr==1((void) (0))
138260 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 )((void) (0));
138261 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN0x0001)!=0
138262 && pWInfo->nOBSat>0
138263 && (pIdx->nKeyCol>nEq)
138264 ){
138265 assert( pLoop->nSkip==0 )((void) (0));
138266 bSeekPastNull = 1;
138267 nExtraReg = 1;
138268 }
138269
138270 /* Find any inequality constraint terms for the start and end
138271 ** of the range.
138272 */
138273 j = nEq;
138274 if( pLoop->wsFlags & WHERE_BTM_LIMIT0x00000020 ){
138275 pRangeStart = pLoop->aLTerm[j++];
138276 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm)((nExtraReg)>(pLoop->u.btree.nBtm)?(nExtraReg):(pLoop->
u.btree.nBtm))
;
138277 /* Like optimization range constraints always occur in pairs */
138278 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||((void) (0))
138279 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 )((void) (0));
138280 }
138281 if( pLoop->wsFlags & WHERE_TOP_LIMIT0x00000010 ){
138282 pRangeEnd = pLoop->aLTerm[j++];
138283 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop)((nExtraReg)>(pLoop->u.btree.nTop)?(nExtraReg):(pLoop->
u.btree.nTop))
;
138284#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
138285 if( (pRangeEnd->wtFlags & TERM_LIKEOPT0x100)!=0 ){
138286 assert( pRangeStart!=0 )((void) (0)); /* LIKE opt constraints */
138287 assert( pRangeStart->wtFlags & TERM_LIKEOPT )((void) (0)); /* occur in pairs */
138288 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
138289 sqlite3VdbeAddOp2(v, OP_Integer70, 1, (int)pLevel->iLikeRepCntr);
138290 VdbeComment((v, "LIKE loop counter"));
138291 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
138292 /* iLikeRepCntr actually stores 2x the counter register number. The
138293 ** bottom bit indicates whether the search order is ASC or DESC. */
138294 testcase( bRev );
138295 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
138296 assert( (bRev & ~1)==0 )((void) (0));
138297 pLevel->iLikeRepCntr <<=1;
138298 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC1);
138299 }
138300#endif
138301 if( pRangeStart==0 ){
138302 j = pIdx->aiColumn[nEq];
138303 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR(-2) ){
138304 bSeekPastNull = 1;
138305 }
138306 }
138307 }
138308 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 )((void) (0));
138309
138310 /* If we are doing a reverse order scan on an ascending index, or
138311 ** a forward order scan on a descending index, interchange the
138312 ** start and end terms (pRangeStart and pRangeEnd).
138313 */
138314 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC0))
138315 || (bRev && pIdx->nKeyCol==nEq)
138316 ){
138317 SWAP(WhereTerm *, pRangeEnd, pRangeStart){WhereTerm * t=pRangeEnd; pRangeEnd=pRangeStart; pRangeStart=
t;}
;
138318 SWAP(u8, bSeekPastNull, bStopAtNull){u8 t=bSeekPastNull; bSeekPastNull=bStopAtNull; bStopAtNull=t
;}
;
138319 SWAP(u8, nBtm, nTop){u8 t=nBtm; nBtm=nTop; nTop=t;};
138320 }
138321
138322 /* Generate code to evaluate all constraint terms using == or IN
138323 ** and store the values of those terms in an array of registers
138324 ** starting at regBase.
138325 */
138326 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
138327 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
138328 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq )((void) (0));
138329 if( zStartAff && nTop ){
138330 zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
138331 }
138332 addrNxt = pLevel->addrNxt;
138333
138334 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
138335 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
138336 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
138337 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
138338 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE(0x0002<<(55 -53))|WO_GE(0x0002<<(57 -53)));
138339 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE(0x0002<<(55 -53))|WO_GE(0x0002<<(57 -53)));
138340 start_constraints = pRangeStart || nEq>0;
138341
138342 /* Seek the index cursor to the start of the range. */
138343 nConstraint = nEq;
138344 if( pRangeStart ){
138345 Expr *pRight = pRangeStart->pExpr->pRight;
138346 codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
138347 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
138348 if( (pRangeStart->wtFlags & TERM_VNULL0x00)==0
138349 && sqlite3ExprCanBeNull(pRight)
138350 ){
138351 sqlite3VdbeAddOp2(v, OP_IsNull50, regBase+nEq, addrNxt);
138352 VdbeCoverage(v);
138353 }
138354 if( zStartAff ){
138355 updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
138356 }
138357 nConstraint += nBtm;
138358 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
138359 if( sqlite3ExprIsVector(pRight)==0 ){
138360 disableTerm(pLevel, pRangeStart);
138361 }else{
138362 startEq = 1;
138363 }
138364 bSeekPastNull = 0;
138365 }else if( bSeekPastNull ){
138366 sqlite3VdbeAddOp2(v, OP_Null73, 0, regBase+nEq);
138367 nConstraint++;
138368 startEq = 0;
138369 start_constraints = 1;
138370 }
138371 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
138372 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
138373 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
138374 ** above has already left the cursor sitting on the correct row,
138375 ** so no further seeking is needed */
138376 }else{
138377 if( pLoop->wsFlags & WHERE_IN_EARLYOUT0x00040000 ){
138378 sqlite3VdbeAddOp1(v, OP_SeekHit119, iIdxCur);
138379 }
138380 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
138381 assert( op!=0 )((void) (0));
138382 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
138383 VdbeCoverage(v);
138384 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind );
138385 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last );
138386 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT );
138387 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
138388 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
138389 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
138390 }
138391
138392 /* Load the value for the inequality constraint at the end of the
138393 ** range (if any).
138394 */
138395 nConstraint = nEq;
138396 if( pRangeEnd ){
138397 Expr *pRight = pRangeEnd->pExpr->pRight;
138398 codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
138399 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
138400 if( (pRangeEnd->wtFlags & TERM_VNULL0x00)==0
138401 && sqlite3ExprCanBeNull(pRight)
138402 ){
138403 sqlite3VdbeAddOp2(v, OP_IsNull50, regBase+nEq, addrNxt);
138404 VdbeCoverage(v);
138405 }
138406 if( zEndAff ){
138407 updateRangeAffinityStr(pRight, nTop, zEndAff);
138408 codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
138409 }else{
138410 assert( pParse->db->mallocFailed )((void) (0));
138411 }
138412 nConstraint += nTop;
138413 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
138414
138415 if( sqlite3ExprIsVector(pRight)==0 ){
138416 disableTerm(pLevel, pRangeEnd);
138417 }else{
138418 endEq = 1;
138419 }
138420 }else if( bStopAtNull ){
138421 sqlite3VdbeAddOp2(v, OP_Null73, 0, regBase+nEq);
138422 endEq = 0;
138423 nConstraint++;
138424 }
138425 sqlite3DbFree(db, zStartAff);
138426 sqlite3DbFree(db, zEndAff);
138427
138428 /* Top of the loop body */
138429 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
138430
138431 /* Check if the index cursor is past the end of the range. */
138432 if( nConstraint ){
138433 op = aEndOp[bRev*2 + endEq];
138434 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
138435 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );
138436 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE );
138437 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT );
138438 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE );
138439 }
138440
138441 if( pLoop->wsFlags & WHERE_IN_EARLYOUT0x00040000 ){
138442 sqlite3VdbeAddOp2(v, OP_SeekHit119, iIdxCur, 1);
138443 }
138444
138445 /* Seek the table cursor, if required */
138446 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY0x00000040)!=0
138447 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)==0;
138448 if( omitTable ){
138449 /* pIdx is a covering index. No need to access the main table. */
138450 }else if( HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x0020)==0) ){
138451 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE0x0400) || (
138452 (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE0x1000)
138453 && (pWInfo->eOnePass==ONEPASS_SINGLE1)
138454 )){
138455 iRowidReg = ++pParse->nMem;
138456 sqlite3VdbeAddOp2(v, OP_IdxRowid135, iIdxCur, iRowidReg);
138457 sqlite3VdbeAddOp3(v, OP_NotExists31, iCur, 0, iRowidReg);
138458 VdbeCoverage(v);
138459 }else{
138460 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
138461 }
138462 }else if( iCur!=iIdxCur ){
138463 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
138464 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
138465 for(j=0; j<pPk->nKeyCol; j++){
138466 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
138467 sqlite3VdbeAddOp3(v, OP_Column90, iIdxCur, k, iRowidReg+j);
138468 }
138469 sqlite3VdbeAddOp4Int(v, OP_NotFound28, iCur, addrCont,
138470 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
138471 }
138472
138473 /* If pIdx is an index on one or more expressions, then look through
138474 ** all the expressions in pWInfo and try to transform matching expressions
138475 ** into reference to index columns.
138476 **
138477 ** Do not do this for the RHS of a LEFT JOIN. This is because the
138478 ** expression may be evaluated after OP_NullRow has been executed on
138479 ** the cursor. In this case it is important to do the full evaluation,
138480 ** as the result of the expression may not be NULL, even if all table
138481 ** column values are. https://www.sqlite.org/src/info/7fa8049685b50b5a
138482 **
138483 ** Also, do not do this when processing one index an a multi-index
138484 ** OR clause, since the transformation will become invalid once we
138485 ** move forward to the next index.
138486 ** https://sqlite.org/src/info/4e8e4857d32d401f
138487 */
138488 if( pLevel->iLeftJoin==0 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)==0 ){
138489 whereIndexExprTrans(pIdx, iCur, iIdxCur, pWInfo);
138490 }
138491
138492 /* If a partial index is driving the loop, try to eliminate WHERE clause
138493 ** terms from the query that must be true due to the WHERE clause of
138494 ** the partial index
138495 */
138496 if( pIdx->pPartIdxWhere ){
138497 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
138498 }
138499
138500 /* Record the instruction used to terminate the loop. */
138501 if( pLoop->wsFlags & WHERE_ONEROW0x00001000 ){
138502 pLevel->op = OP_Noop170;
138503 }else if( bRev ){
138504 pLevel->op = OP_Prev4;
138505 }else{
138506 pLevel->op = OP_Next5;
138507 }
138508 pLevel->p1 = iIdxCur;
138509 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED0x00010000)!=0 ? 1:0;
138510 if( (pLoop->wsFlags & WHERE_CONSTRAINT0x0000000f)==0 ){
138511 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP1;
138512 }else{
138513 assert( pLevel->p5==0 )((void) (0));
138514 }
138515 if( omitTable ) pIdx = 0;
138516 }else
138517
138518#ifndef SQLITE_OMIT_OR_OPTIMIZATION
138519 if( pLoop->wsFlags & WHERE_MULTI_OR0x00002000 ){
138520 /* Case 5: Two or more separately indexed terms connected by OR
138521 **
138522 ** Example:
138523 **
138524 ** CREATE TABLE t1(a,b,c,d);
138525 ** CREATE INDEX i1 ON t1(a);
138526 ** CREATE INDEX i2 ON t1(b);
138527 ** CREATE INDEX i3 ON t1(c);
138528 **
138529 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
138530 **
138531 ** In the example, there are three indexed terms connected by OR.
138532 ** The top of the loop looks like this:
138533 **
138534 ** Null 1 # Zero the rowset in reg 1
138535 **
138536 ** Then, for each indexed term, the following. The arguments to
138537 ** RowSetTest are such that the rowid of the current row is inserted
138538 ** into the RowSet. If it is already present, control skips the
138539 ** Gosub opcode and jumps straight to the code generated by WhereEnd().
138540 **
138541 ** sqlite3WhereBegin(<term>)
138542 ** RowSetTest # Insert rowid into rowset
138543 ** Gosub 2 A
138544 ** sqlite3WhereEnd()
138545 **
138546 ** Following the above, code to terminate the loop. Label A, the target
138547 ** of the Gosub above, jumps to the instruction right after the Goto.
138548 **
138549 ** Null 1 # Zero the rowset in reg 1
138550 ** Goto B # The loop is finished.
138551 **
138552 ** A: <loop body> # Return data, whatever.
138553 **
138554 ** Return 2 # Jump back to the Gosub
138555 **
138556 ** B: <after the loop>
138557 **
138558 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
138559 ** use an ephemeral index instead of a RowSet to record the primary
138560 ** keys of the rows we have already seen.
138561 **
138562 */
138563 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
138564 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
138565 Index *pCov = 0; /* Potential covering index (or NULL) */
138566 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
138567
138568 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
138569 int regRowset = 0; /* Register for RowSet object */
138570 int regRowid = 0; /* Register holding rowid */
138571 int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
138572 int iRetInit; /* Address of regReturn init */
138573 int untestedTerms = 0; /* Some terms not completely tested */
138574 int ii; /* Loop counter */
138575 u16 wctrlFlags; /* Flags for sub-WHERE clause */
138576 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
138577 Table *pTab = pTabItem->pTab;
138578
138579 pTerm = pLoop->aLTerm[0];
138580 assert( pTerm!=0 )((void) (0));
138581 assert( pTerm->eOperator & WO_OR )((void) (0));
138582 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 )((void) (0));
138583 pOrWc = &pTerm->u.pOrInfo->wc;
138584 pLevel->op = OP_Return66;
138585 pLevel->p1 = regReturn;
138586
138587 /* Set up a new SrcList in pOrTab containing the table being scanned
138588 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
138589 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
138590 */
138591 if( pWInfo->nLevel>1 ){
138592 int nNotReady; /* The number of notReady tables */
138593 struct SrcList_item *origSrc; /* Original list of tables */
138594 nNotReady = pWInfo->nLevel - iLevel - 1;
138595 pOrTab = sqlite3StackAllocRaw(db,sqlite3DbMallocRaw(db,sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab
->a[0]))
138596 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]))sqlite3DbMallocRaw(db,sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab
->a[0]))
;
138597 if( pOrTab==0 ) return notReady;
138598 pOrTab->nAlloc = (u8)(nNotReady + 1);
138599 pOrTab->nSrc = pOrTab->nAlloc;
138600 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
138601 origSrc = pWInfo->pTabList->a;
138602 for(k=1; k<=nNotReady; k++){
138603 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
138604 }
138605 }else{
138606 pOrTab = pWInfo->pTabList;
138607 }
138608
138609 /* Initialize the rowset register to contain NULL. An SQL NULL is
138610 ** equivalent to an empty rowset. Or, create an ephemeral index
138611 ** capable of holding primary keys in the case of a WITHOUT ROWID.
138612 **
138613 ** Also initialize regReturn to contain the address of the instruction
138614 ** immediately following the OP_Return at the bottom of the loop. This
138615 ** is required in a few obscure LEFT JOIN cases where control jumps
138616 ** over the top of the loop into the body of it. In this case the
138617 ** correct response for the end-of-loop code (the OP_Return) is to
138618 ** fall through to the next instruction, just as an OP_Next does if
138619 ** called on an uninitialized cursor.
138620 */
138621 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK0x0010)==0 ){
138622 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
138623 regRowset = ++pParse->nMem;
138624 sqlite3VdbeAddOp2(v, OP_Null73, 0, regRowset);
138625 }else{
138626 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
138627 regRowset = pParse->nTab++;
138628 sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, regRowset, pPk->nKeyCol);
138629 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
138630 }
138631 regRowid = ++pParse->nMem;
138632 }
138633 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer70, 0, regReturn);
138634
138635 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
138636 ** Then for every term xN, evaluate as the subexpression: xN AND z
138637 ** That way, terms in y that are factored into the disjunction will
138638 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
138639 **
138640 ** Actually, each subexpression is converted to "xN AND w" where w is
138641 ** the "interesting" terms of z - terms that did not originate in the
138642 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
138643 ** indices.
138644 **
138645 ** This optimization also only applies if the (x1 OR x2 OR ...) term
138646 ** is not contained in the ON clause of a LEFT JOIN.
138647 ** See ticket http://www.sqlite.org/src/info/f2369304e4
138648 */
138649 if( pWC->nTerm>1 ){
138650 int iTerm;
138651 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
138652 Expr *pExpr = pWC->a[iTerm].pExpr;
138653 if( &pWC->a[iTerm] == pTerm ) continue;
138654 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
138655 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
138656 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL0x02|TERM_CODED0x04))!=0 ) continue;
138657 if( (pWC->a[iTerm].eOperator & WO_ALL0x1fff)==0 ) continue;
138658 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
138659 pExpr = sqlite3ExprDup(db, pExpr, 0);
138660 pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
138661 }
138662 if( pAndExpr ){
138663 /* The extra 0x10000 bit on the opcode is masked off and does not
138664 ** become part of the new Expr.op. However, it does make the
138665 ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
138666 ** prevents sqlite3PExpr() from implementing AND short-circuit
138667 ** optimization, which we do not want here. */
138668 pAndExpr = sqlite3PExpr(pParse, TK_AND44|0x10000, 0, pAndExpr);
138669 }
138670 }
138671
138672 /* Run a separate WHERE clause for each term of the OR clause. After
138673 ** eliminating duplicates from other WHERE clauses, the action for each
138674 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
138675 */
138676 wctrlFlags = WHERE_OR_SUBCLAUSE0x0020 | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE0x0400);
138677 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"))sqlite3VdbeExplain (pParse, 1, "MULTI-INDEX OR");
138678 for(ii=0; ii<pOrWc->nTerm; ii++){
138679 WhereTerm *pOrTerm = &pOrWc->a[ii];
138680 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND0x0400)!=0 ){
138681 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
138682 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
138683 int jmp1 = 0; /* Address of jump operation */
138684 assert( (pTabItem[0].fg.jointype & JT_LEFT)==0((void) (0))
138685 || ExprHasProperty(pOrExpr, EP_FromJoin)((void) (0))
138686 )((void) (0));
138687 if( pAndExpr ){
138688 pAndExpr->pLeft = pOrExpr;
138689 pOrExpr = pAndExpr;
138690 }
138691 /* Loop through table entries that match term pOrTerm. */
138692 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1))sqlite3VdbeExplain (pParse, 1, "INDEX %d", ii+1);
138693 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
138694 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
138695 wctrlFlags, iCovCur);
138696 assert( pSubWInfo || pParse->nErr || db->mallocFailed )((void) (0));
138697 if( pSubWInfo ){
138698 WhereLoop *pSubLoop;
138699 int addrExplain = sqlite3WhereExplainOneScan(
138700 pParse, pOrTab, &pSubWInfo->a[0], 0
138701 );
138702 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain)((void)addrExplain);
138703
138704 /* This is the sub-WHERE clause body. First skip over
138705 ** duplicate rows from prior sub-WHERE clauses, and record the
138706 ** rowid (or PRIMARY KEY) for the current row so that the same
138707 ** row will be skipped in subsequent sub-WHERE clauses.
138708 */
138709 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK0x0010)==0 ){
138710 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
138711 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
138712 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
138713 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest42, regRowset, 0,
138714 regRowid, iSet);
138715 VdbeCoverage(v);
138716 }else{
138717 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
138718 int nPk = pPk->nKeyCol;
138719 int iPk;
138720 int r;
138721
138722 /* Read the PK into an array of temp registers. */
138723 r = sqlite3GetTempRange(pParse, nPk);
138724 for(iPk=0; iPk<nPk; iPk++){
138725 int iCol = pPk->aiColumn[iPk];
138726 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol, r+iPk);
138727 }
138728
138729 /* Check if the temp table already contains this key. If so,
138730 ** the row has already been included in the result set and
138731 ** can be ignored (by jumping past the Gosub below). Otherwise,
138732 ** insert the key into the temp table and proceed with processing
138733 ** the row.
138734 **
138735 ** Use some of the same optimizations as OP_RowSetTest: If iSet
138736 ** is zero, assume that the key cannot already be present in
138737 ** the temp table. And if iSet is -1, assume that there is no
138738 ** need to insert the key into the temp table, as it will never
138739 ** be tested for. */
138740 if( iSet ){
138741 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found29, regRowset, 0, r, nPk);
138742 VdbeCoverage(v);
138743 }
138744 if( iSet>=0 ){
138745 sqlite3VdbeAddOp3(v, OP_MakeRecord92, r, nPk, regRowid);
138746 sqlite3VdbeAddOp4Int(v, OP_IdxInsert132, regRowset, regRowid,
138747 r, nPk);
138748 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
138749 }
138750
138751 /* Release the array of temp registers */
138752 sqlite3ReleaseTempRange(pParse, r, nPk);
138753 }
138754 }
138755
138756 /* Invoke the main loop body as a subroutine */
138757 sqlite3VdbeAddOp2(v, OP_Gosub12, regReturn, iLoopBody);
138758
138759 /* Jump here (skipping the main loop body subroutine) if the
138760 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
138761 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
138762
138763 /* The pSubWInfo->untestedTerms flag means that this OR term
138764 ** contained one or more AND term from a notReady table. The
138765 ** terms from the notReady table could not be tested and will
138766 ** need to be tested later.
138767 */
138768 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
138769
138770 /* If all of the OR-connected terms are optimized using the same
138771 ** index, and the index is opened using the same cursor number
138772 ** by each call to sqlite3WhereBegin() made by this loop, it may
138773 ** be possible to use that index as a covering index.
138774 **
138775 ** If the call to sqlite3WhereBegin() above resulted in a scan that
138776 ** uses an index, and this is either the first OR-connected term
138777 ** processed or the index is the same as that used by all previous
138778 ** terms, set pCov to the candidate covering index. Otherwise, set
138779 ** pCov to NULL to indicate that no candidate covering index will
138780 ** be available.
138781 */
138782 pSubLoop = pSubWInfo->a[0].pWLoop;
138783 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 )((void) (0));
138784 if( (pSubLoop->wsFlags & WHERE_INDEXED0x00000200)!=0
138785 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
138786 && (HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)((pSubLoop->u.btree.pIndex)->idxType==2))
138787 ){
138788 assert( pSubWInfo->a[0].iIdxCur==iCovCur )((void) (0));
138789 pCov = pSubLoop->u.btree.pIndex;
138790 }else{
138791 pCov = 0;
138792 }
138793
138794 /* Finish the loop through table entries that match term pOrTerm. */
138795 sqlite3WhereEnd(pSubWInfo);
138796 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
138797 }
138798 }
138799 }
138800 ExplainQueryPlanPop(pParse)sqlite3VdbeExplainPop(pParse);
138801 pLevel->u.pCovidx = pCov;
138802 if( pCov ) pLevel->iIdxCur = iCovCur;
138803 if( pAndExpr ){
138804 pAndExpr->pLeft = 0;
138805 sqlite3ExprDelete(db, pAndExpr);
138806 }
138807 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
138808 sqlite3VdbeGoto(v, pLevel->addrBrk);
138809 sqlite3VdbeResolveLabel(v, iLoopBody);
138810
138811 if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab)sqlite3DbFree(db,pOrTab); }
138812 if( !untestedTerms ) disableTerm(pLevel, pTerm);
138813 }else
138814#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
138815
138816 {
138817 /* Case 6: There is no usable index. We must do a complete
138818 ** scan of the entire table.
138819 */
138820 static const u8 aStep[] = { OP_Next5, OP_Prev4 };
138821 static const u8 aStart[] = { OP_Rewind36, OP_Last32 };
138822 assert( bRev==0 || bRev==1 )((void) (0));
138823 if( pTabItem->fg.isRecursive ){
138824 /* Tables marked isRecursive have only a single row that is stored in
138825 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
138826 pLevel->op = OP_Noop170;
138827 }else{
138828 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
138829 pLevel->op = aStep[bRev];
138830 pLevel->p1 = iCur;
138831 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
138832 VdbeCoverageIf(v, bRev==0);
138833 VdbeCoverageIf(v, bRev!=0);
138834 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP1;
138835 }
138836 }
138837
138838#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
138839 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
138840#endif
138841
138842 /* Insert code to test every subexpression that can be completely
138843 ** computed using the current set of tables.
138844 **
138845 ** This loop may run between one and three times, depending on the
138846 ** constraints to be generated. The value of stack variable iLoop
138847 ** determines the constraints coded by each iteration, as follows:
138848 **
138849 ** iLoop==1: Code only expressions that are entirely covered by pIdx.
138850 ** iLoop==2: Code remaining expressions that do not contain correlated
138851 ** sub-queries.
138852 ** iLoop==3: Code all remaining expressions.
138853 **
138854 ** An effort is made to skip unnecessary iterations of the loop.
138855 */
138856 iLoop = (pIdx ? 1 : 2);
138857 do{
138858 int iNext = 0; /* Next value for iLoop */
138859 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
138860 Expr *pE;
138861 int skipLikeAddr = 0;
138862 testcase( pTerm->wtFlags & TERM_VIRTUAL );
138863 testcase( pTerm->wtFlags & TERM_CODED );
138864 if( pTerm->wtFlags & (TERM_VIRTUAL0x02|TERM_CODED0x04) ) continue;
138865 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
138866 testcase( pWInfo->untestedTerms==0
138867 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
138868 pWInfo->untestedTerms = 1;
138869 continue;
138870 }
138871 pE = pTerm->pExpr;
138872 assert( pE!=0 )((void) (0));
138873 if( (pTabItem->fg.jointype&JT_LEFT0x0008) && !ExprHasProperty(pE,EP_FromJoin)(((pE)->flags&(0x000001))!=0) ){
138874 continue;
138875 }
138876
138877 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
138878 iNext = 2;
138879 continue;
138880 }
138881 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT0x1000) ){
138882 if( iNext==0 ) iNext = 3;
138883 continue;
138884 }
138885
138886 if( (pTerm->wtFlags & TERM_LIKECOND0x200)!=0 ){
138887 /* If the TERM_LIKECOND flag is set, that means that the range search
138888 ** is sufficient to guarantee that the LIKE operator is true, so we
138889 ** can skip the call to the like(A,B) function. But this only works
138890 ** for strings. So do not skip the call to the function on the pass
138891 ** that compares BLOBs. */
138892#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
138893 continue;
138894#else
138895 u32 x = pLevel->iLikeRepCntr;
138896 if( x>0 ){
138897 skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot20:OP_If18,(int)(x>>1));
138898 VdbeCoverageIf(v, (x&1)==1);
138899 VdbeCoverageIf(v, (x&1)==0);
138900 }
138901#endif
138902 }
138903#ifdef WHERETRACE_ENABLED /* 0xffff */
138904 if( sqlite3WhereTrace ){
138905 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
138906 pWC->nTerm-j, pTerm, iLoop));
138907 }
138908#endif
138909 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL0x10);
138910 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
138911 pTerm->wtFlags |= TERM_CODED0x04;
138912 }
138913 iLoop = iNext;
138914 }while( iLoop>0 );
138915
138916 /* Insert code to test for implied constraints based on transitivity
138917 ** of the "==" operator.
138918 **
138919 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
138920 ** and we are coding the t1 loop and the t2 loop has not yet coded,
138921 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
138922 ** the implied "t1.a=123" constraint.
138923 */
138924 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
138925 Expr *pE, sEAlt;
138926 WhereTerm *pAlt;
138927 if( pTerm->wtFlags & (TERM_VIRTUAL0x02|TERM_CODED0x04) ) continue;
138928 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))==0 ) continue;
138929 if( (pTerm->eOperator & WO_EQUIV0x0800)==0 ) continue;
138930 if( pTerm->leftCursor!=iCur ) continue;
138931 if( pLevel->iLeftJoin ) continue;
138932 pE = pTerm->pExpr;
138933 assert( !ExprHasProperty(pE, EP_FromJoin) )((void) (0));
138934 assert( (pTerm->prereqRight & pLevel->notReady)!=0 )((void) (0));
138935 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
138936 WO_EQ0x0002|WO_IN0x0001|WO_IS0x0080, 0);
138937 if( pAlt==0 ) continue;
138938 if( pAlt->wtFlags & (TERM_CODED0x04) ) continue;
138939 if( (pAlt->eOperator & WO_IN0x0001)
138940 && (pAlt->pExpr->flags & EP_xIsSelect0x000800)
138941 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
138942 ){
138943 continue;
138944 }
138945 testcase( pAlt->eOperator & WO_EQ );
138946 testcase( pAlt->eOperator & WO_IS );
138947 testcase( pAlt->eOperator & WO_IN );
138948 VdbeModuleComment((v, "begin transitive constraint"));
138949 sEAlt = *pAlt->pExpr;
138950 sEAlt.pLeft = pE->pLeft;
138951 sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL0x10);
138952 }
138953
138954 /* For a LEFT OUTER JOIN, generate code that will record the fact that
138955 ** at least one row of the right table has matched the left table.
138956 */
138957 if( pLevel->iLeftJoin ){
138958 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
138959 sqlite3VdbeAddOp2(v, OP_Integer70, 1, pLevel->iLeftJoin);
138960 VdbeComment((v, "record LEFT JOIN hit"));
138961 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
138962 testcase( pTerm->wtFlags & TERM_VIRTUAL );
138963 testcase( pTerm->wtFlags & TERM_CODED );
138964 if( pTerm->wtFlags & (TERM_VIRTUAL0x02|TERM_CODED0x04) ) continue;
138965 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
138966 assert( pWInfo->untestedTerms )((void) (0));
138967 continue;
138968 }
138969 assert( pTerm->pExpr )((void) (0));
138970 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL0x10);
138971 pTerm->wtFlags |= TERM_CODED0x04;
138972 }
138973 }
138974
138975 return pLevel->notReady;
138976}
138977
138978/************** End of wherecode.c *******************************************/
138979/************** Begin file whereexpr.c ***************************************/
138980/*
138981** 2015-06-08
138982**
138983** The author disclaims copyright to this source code. In place of
138984** a legal notice, here is a blessing:
138985**
138986** May you do good and not evil.
138987** May you find forgiveness for yourself and forgive others.
138988** May you share freely, never taking more than you give.
138989**
138990*************************************************************************
138991** This module contains C code that generates VDBE code used to process
138992** the WHERE clause of SQL statements.
138993**
138994** This file was originally part of where.c but was split out to improve
138995** readability and editabiliity. This file contains utility routines for
138996** analyzing Expr objects in the WHERE clause.
138997*/
138998/* #include "sqliteInt.h" */
138999/* #include "whereInt.h" */
139000
139001/* Forward declarations */
139002static void exprAnalyze(SrcList*, WhereClause*, int);
139003
139004/*
139005** Deallocate all memory associated with a WhereOrInfo object.
139006*/
139007static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
139008 sqlite3WhereClauseClear(&p->wc);
139009 sqlite3DbFree(db, p);
139010}
139011
139012/*
139013** Deallocate all memory associated with a WhereAndInfo object.
139014*/
139015static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
139016 sqlite3WhereClauseClear(&p->wc);
139017 sqlite3DbFree(db, p);
139018}
139019
139020/*
139021** Add a single new WhereTerm entry to the WhereClause object pWC.
139022** The new WhereTerm object is constructed from Expr p and with wtFlags.
139023** The index in pWC->a[] of the new WhereTerm is returned on success.
139024** 0 is returned if the new WhereTerm could not be added due to a memory
139025** allocation error. The memory allocation failure will be recorded in
139026** the db->mallocFailed flag so that higher-level functions can detect it.
139027**
139028** This routine will increase the size of the pWC->a[] array as necessary.
139029**
139030** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
139031** for freeing the expression p is assumed by the WhereClause object pWC.
139032** This is true even if this routine fails to allocate a new WhereTerm.
139033**
139034** WARNING: This routine might reallocate the space used to store
139035** WhereTerms. All pointers to WhereTerms should be invalidated after
139036** calling this routine. Such pointers may be reinitialized by referencing
139037** the pWC->a[] array.
139038*/
139039static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
139040 WhereTerm *pTerm;
139041 int idx;
139042 testcase( wtFlags & TERM_VIRTUAL );
139043 if( pWC->nTerm>=pWC->nSlot ){
139044 WhereTerm *pOld = pWC->a;
139045 sqlite3 *db = pWC->pWInfo->pParse->db;
139046 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
139047 if( pWC->a==0 ){
139048 if( wtFlags & TERM_DYNAMIC0x01 ){
139049 sqlite3ExprDelete(db, p);
139050 }
139051 pWC->a = pOld;
139052 return 0;
139053 }
139054 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
139055 if( pOld!=pWC->aStatic ){
139056 sqlite3DbFree(db, pOld);
139057 }
139058 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
139059 }
139060 pTerm = &pWC->a[idx = pWC->nTerm++];
139061 if( p && ExprHasProperty(p, EP_Unlikely)(((p)->flags&(0x040000))!=0) ){
139062 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
139063 }else{
139064 pTerm->truthProb = 1;
139065 }
139066 pTerm->pExpr = sqlite3ExprSkipCollate(p);
139067 pTerm->wtFlags = wtFlags;
139068 pTerm->pWC = pWC;
139069 pTerm->iParent = -1;
139070 memset(&pTerm->eOperator, 0,
139071 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator)__builtin_offsetof(WhereTerm, eOperator));
139072 return idx;
139073}
139074
139075/*
139076** Return TRUE if the given operator is one of the operators that is
139077** allowed for an indexable WHERE clause term. The allowed operators are
139078** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
139079*/
139080static int allowedOp(int op){
139081 assert( TK_GT>TK_EQ && TK_GT<TK_GE )((void) (0));
139082 assert( TK_LT>TK_EQ && TK_LT<TK_GE )((void) (0));
139083 assert( TK_LE>TK_EQ && TK_LE<TK_GE )((void) (0));
139084 assert( TK_GE==TK_EQ+4 )((void) (0));
139085 return op==TK_IN49 || (op>=TK_EQ53 && op<=TK_GE57) || op==TK_ISNULL50 || op==TK_IS45;
139086}
139087
139088/*
139089** Commute a comparison operator. Expressions of the form "X op Y"
139090** are converted into "Y op X".
139091**
139092** If left/right precedence rules come into play when determining the
139093** collating sequence, then COLLATE operators are adjusted to ensure
139094** that the collating sequence does not change. For example:
139095** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
139096** the left hand side of a comparison overrides any collation sequence
139097** attached to the right. For the same reason the EP_Collate flag
139098** is not commuted.
139099*/
139100static void exprCommute(Parse *pParse, Expr *pExpr){
139101 u16 expRight = (pExpr->pRight->flags & EP_Collate0x000100);
139102 u16 expLeft = (pExpr->pLeft->flags & EP_Collate0x000100);
139103 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN )((void) (0));
139104 if( expRight==expLeft ){
139105 /* Either X and Y both have COLLATE operator or neither do */
139106 if( expRight ){
139107 /* Both X and Y have COLLATE operators. Make sure X is always
139108 ** used by clearing the EP_Collate flag from Y. */
139109 pExpr->pRight->flags &= ~EP_Collate0x000100;
139110 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
139111 /* Neither X nor Y have COLLATE operators, but X has a non-default
139112 ** collating sequence. So add the EP_Collate marker on X to cause
139113 ** it to be searched first. */
139114 pExpr->pLeft->flags |= EP_Collate0x000100;
139115 }
139116 }
139117 SWAP(Expr*,pExpr->pRight,pExpr->pLeft){Expr* t=pExpr->pRight; pExpr->pRight=pExpr->pLeft; pExpr
->pLeft=t;}
;
139118 if( pExpr->op>=TK_GT54 ){
139119 assert( TK_LT==TK_GT+2 )((void) (0));
139120 assert( TK_GE==TK_LE+2 )((void) (0));
139121 assert( TK_GT>TK_EQ )((void) (0));
139122 assert( TK_GT<TK_LE )((void) (0));
139123 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE )((void) (0));
139124 pExpr->op = ((pExpr->op-TK_GT54)^2)+TK_GT54;
139125 }
139126}
139127
139128/*
139129** Translate from TK_xx operator to WO_xx bitmask.
139130*/
139131static u16 operatorMask(int op){
139132 u16 c;
139133 assert( allowedOp(op) )((void) (0));
139134 if( op==TK_IN49 ){
139135 c = WO_IN0x0001;
139136 }else if( op==TK_ISNULL50 ){
139137 c = WO_ISNULL0x0100;
139138 }else if( op==TK_IS45 ){
139139 c = WO_IS0x0080;
139140 }else{
139141 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff )((void) (0));
139142 c = (u16)(WO_EQ0x0002<<(op-TK_EQ53));
139143 }
139144 assert( op!=TK_ISNULL || c==WO_ISNULL )((void) (0));
139145 assert( op!=TK_IN || c==WO_IN )((void) (0));
139146 assert( op!=TK_EQ || c==WO_EQ )((void) (0));
139147 assert( op!=TK_LT || c==WO_LT )((void) (0));
139148 assert( op!=TK_LE || c==WO_LE )((void) (0));
139149 assert( op!=TK_GT || c==WO_GT )((void) (0));
139150 assert( op!=TK_GE || c==WO_GE )((void) (0));
139151 assert( op!=TK_IS || c==WO_IS )((void) (0));
139152 return c;
139153}
139154
139155
139156#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
139157/*
139158** Check to see if the given expression is a LIKE or GLOB operator that
139159** can be optimized using inequality constraints. Return TRUE if it is
139160** so and false if not.
139161**
139162** In order for the operator to be optimizible, the RHS must be a string
139163** literal that does not begin with a wildcard. The LHS must be a column
139164** that may only be NULL, a string, or a BLOB, never a number. (This means
139165** that virtual tables cannot participate in the LIKE optimization.) The
139166** collating sequence for the column on the LHS must be appropriate for
139167** the operator.
139168*/
139169static int isLikeOrGlob(
139170 Parse *pParse, /* Parsing and code generating context */
139171 Expr *pExpr, /* Test this expression */
139172 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
139173 int *pisComplete, /* True if the only wildcard is % in the last character */
139174 int *pnoCase /* True if uppercase is equivalent to lowercase */
139175){
139176 const u8 *z = 0; /* String on RHS of LIKE operator */
139177 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
139178 ExprList *pList; /* List of operands to the LIKE operator */
139179 u8 c; /* One character in z[] */
139180 int cnt; /* Number of non-wildcard prefix characters */
139181 u8 wc[4]; /* Wildcard characters */
139182 sqlite3 *db = pParse->db; /* Database connection */
139183 sqlite3_value *pVal = 0;
139184 int op; /* Opcode of pRight */
139185 int rc; /* Result code to return */
139186
139187 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, (char*)wc) ){
139188 return 0;
139189 }
139190#ifdef SQLITE_EBCDIC
139191 if( *pnoCase ) return 0;
139192#endif
139193 pList = pExpr->x.pList;
139194 pLeft = pList->a[1].pExpr;
139195
139196 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
139197 op = pRight->op;
139198 if( op==TK_VARIABLE148 && (db->flags & SQLITE_EnableQPSG0x00800000)==0 ){
139199 Vdbe *pReprepare = pParse->pReprepare;
139200 int iCol = pRight->iColumn;
139201 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB'A');
139202 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT3 ){
139203 z = sqlite3_value_text(pVal);
139204 }
139205 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
139206 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER )((void) (0));
139207 }else if( op==TK_STRING110 ){
139208 z = (u8*)pRight->u.zToken;
139209 }
139210 if( z ){
139211
139212 /* Count the number of prefix characters prior to the first wildcard */
139213 cnt = 0;
139214 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
139215 cnt++;
139216 if( c==wc[3] && z[cnt]!=0 ) cnt++;
139217 }
139218
139219 /* The optimization is possible only if (1) the pattern does not begin
139220 ** with a wildcard and if (2) the non-wildcard prefix does not end with
139221 ** an (illegal 0xff) character, or (3) the pattern does not consist of
139222 ** a single escape character. The second condition is necessary so
139223 ** that we can increment the prefix key to find an upper bound for the
139224 ** range search. The third is because the caller assumes that the pattern
139225 ** consists of at least one character after all escapes have been
139226 ** removed. */
139227 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
139228 Expr *pPrefix;
139229
139230 /* A "complete" match if the pattern ends with "*" or "%" */
139231 *pisComplete = c==wc[0] && z[cnt+1]==0;
139232
139233 /* Get the pattern prefix. Remove all escapes from the prefix. */
139234 pPrefix = sqlite3Expr(db, TK_STRING110, (char*)z);
139235 if( pPrefix ){
139236 int iFrom, iTo;
139237 char *zNew = pPrefix->u.zToken;
139238 zNew[cnt] = 0;
139239 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
139240 if( zNew[iFrom]==wc[3] ) iFrom++;
139241 zNew[iTo++] = zNew[iFrom];
139242 }
139243 zNew[iTo] = 0;
139244 assert( iTo>0 )((void) (0));
139245
139246 /* If the LHS is not an ordinary column with TEXT affinity, then the
139247 ** pattern prefix boundaries (both the start and end boundaries) must
139248 ** not look like a number. Otherwise the pattern might be treated as
139249 ** a number, which will invalidate the LIKE optimization.
139250 **
139251 ** Getting this right has been a persistent source of bugs in the
139252 ** LIKE optimization. See, for example:
139253 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
139254 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
139255 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
139256 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
139257 */
139258 if( pLeft->op!=TK_COLUMN162
139259 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT'B'
139260 || IsVirtual(pLeft->y.pTab)((pLeft->y.pTab)->nModuleArg) /* Value might be numeric */
139261 ){
139262 int isNum;
139263 double rDummy;
139264 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF81);
139265 if( isNum<=0 ){
139266 zNew[iTo-1]++;
139267 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF81);
139268 zNew[iTo-1]--;
139269 }
139270 if( isNum>0 ){
139271 sqlite3ExprDelete(db, pPrefix);
139272 sqlite3ValueFree(pVal);
139273 return 0;
139274 }
139275 }
139276 }
139277 *ppPrefix = pPrefix;
139278
139279 /* If the RHS pattern is a bound parameter, make arrangements to
139280 ** reprepare the statement when that parameter is rebound */
139281 if( op==TK_VARIABLE148 ){
139282 Vdbe *v = pParse->pVdbe;
139283 sqlite3VdbeSetVarmask(v, pRight->iColumn);
139284 if( *pisComplete && pRight->u.zToken[1] ){
139285 /* If the rhs of the LIKE expression is a variable, and the current
139286 ** value of the variable means there is no need to invoke the LIKE
139287 ** function, then no OP_Variable will be added to the program.
139288 ** This causes problems for the sqlite3_bind_parameter_name()
139289 ** API. To work around them, add a dummy OP_Variable here.
139290 */
139291 int r1 = sqlite3GetTempReg(pParse);
139292 sqlite3ExprCodeTarget(pParse, pRight, r1);
139293 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
139294 sqlite3ReleaseTempReg(pParse, r1);
139295 }
139296 }
139297 }else{
139298 z = 0;
139299 }
139300 }
139301
139302 rc = (z!=0);
139303 sqlite3ValueFree(pVal);
139304 return rc;
139305}
139306#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
139307
139308
139309#ifndef SQLITE_OMIT_VIRTUALTABLE
139310/*
139311** Check to see if the pExpr expression is a form that needs to be passed
139312** to the xBestIndex method of virtual tables. Forms of interest include:
139313**
139314** Expression Virtual Table Operator
139315** ----------------------- ---------------------------------
139316** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
139317** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
139318** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
139319** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
139320** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
139321** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
139322** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
139323** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
139324** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
139325**
139326** In every case, "column" must be a column of a virtual table. If there
139327** is a match, set *ppLeft to the "column" expression, set *ppRight to the
139328** "expr" expression (even though in forms (6) and (8) the column is on the
139329** right and the expression is on the left). Also set *peOp2 to the
139330** appropriate virtual table operator. The return value is 1 or 2 if there
139331** is a match. The usual return is 1, but if the RHS is also a column
139332** of virtual table in forms (5) or (7) then return 2.
139333**
139334** If the expression matches none of the patterns above, return 0.
139335*/
139336static int isAuxiliaryVtabOperator(
139337 sqlite3 *db, /* Parsing context */
139338 Expr *pExpr, /* Test this expression */
139339 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
139340 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
139341 Expr **ppRight /* Expression to left of MATCH/op2 */
139342){
139343 if( pExpr->op==TK_FUNCTION161 ){
139344 static const struct Op2 {
139345 const char *zOp;
139346 unsigned char eOp2;
139347 } aOp[] = {
139348 { "match", SQLITE_INDEX_CONSTRAINT_MATCH64 },
139349 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB66 },
139350 { "like", SQLITE_INDEX_CONSTRAINT_LIKE65 },
139351 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP67 }
139352 };
139353 ExprList *pList;
139354 Expr *pCol; /* Column reference */
139355 int i;
139356
139357 pList = pExpr->x.pList;
139358 if( pList==0 || pList->nExpr!=2 ){
139359 return 0;
139360 }
139361
139362 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
139363 ** virtual table on their second argument, which is the same as
139364 ** the left-hand side operand in their in-fix form.
139365 **
139366 ** vtab_column MATCH expression
139367 ** MATCH(expression,vtab_column)
139368 */
139369 pCol = pList->a[1].pExpr;
139370 if( pCol->op==TK_COLUMN162 && IsVirtual(pCol->y.pTab)((pCol->y.pTab)->nModuleArg) ){
139371 for(i=0; i<ArraySize(aOp)((int)(sizeof(aOp)/sizeof(aOp[0]))); i++){
139372 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
139373 *peOp2 = aOp[i].eOp2;
139374 *ppRight = pList->a[0].pExpr;
139375 *ppLeft = pCol;
139376 return 1;
139377 }
139378 }
139379 }
139380
139381 /* We can also match against the first column of overloaded
139382 ** functions where xFindFunction returns a value of at least
139383 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
139384 **
139385 ** OVERLOADED(vtab_column,expression)
139386 **
139387 ** Historically, xFindFunction expected to see lower-case function
139388 ** names. But for this use case, xFindFunction is expected to deal
139389 ** with function names in an arbitrary case.
139390 */
139391 pCol = pList->a[0].pExpr;
139392 if( pCol->op==TK_COLUMN162 && IsVirtual(pCol->y.pTab)((pCol->y.pTab)->nModuleArg) ){
139393 sqlite3_vtab *pVtab;
139394 sqlite3_module *pMod;
139395 void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
139396 void *pNotUsed;
139397 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
139398 assert( pVtab!=0 )((void) (0));
139399 assert( pVtab->pModule!=0 )((void) (0));
139400 pMod = (sqlite3_module *)pVtab->pModule;
139401 if( pMod->xFindFunction!=0 ){
139402 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
139403 if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION150 ){
139404 *peOp2 = i;
139405 *ppRight = pList->a[1].pExpr;
139406 *ppLeft = pCol;
139407 return 1;
139408 }
139409 }
139410 }
139411 }else if( pExpr->op==TK_NE52 || pExpr->op==TK_ISNOT160 || pExpr->op==TK_NOTNULL51 ){
139412 int res = 0;
139413 Expr *pLeft = pExpr->pLeft;
139414 Expr *pRight = pExpr->pRight;
139415 if( pLeft->op==TK_COLUMN162 && IsVirtual(pLeft->y.pTab)((pLeft->y.pTab)->nModuleArg) ){
139416 res++;
139417 }
139418 if( pRight && pRight->op==TK_COLUMN162 && IsVirtual(pRight->y.pTab)((pRight->y.pTab)->nModuleArg) ){
139419 res++;
139420 SWAP(Expr*, pLeft, pRight){Expr* t=pLeft; pLeft=pRight; pRight=t;};
139421 }
139422 *ppLeft = pLeft;
139423 *ppRight = pRight;
139424 if( pExpr->op==TK_NE52 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE68;
139425 if( pExpr->op==TK_ISNOT160 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT69;
139426 if( pExpr->op==TK_NOTNULL51 ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL70;
139427 return res;
139428 }
139429 return 0;
139430}
139431#endif /* SQLITE_OMIT_VIRTUALTABLE */
139432
139433/*
139434** If the pBase expression originated in the ON or USING clause of
139435** a join, then transfer the appropriate markings over to derived.
139436*/
139437static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
139438 if( pDerived ){
139439 pDerived->flags |= pBase->flags & EP_FromJoin0x000001;
139440 pDerived->iRightJoinTable = pBase->iRightJoinTable;
139441 }
139442}
139443
139444/*
139445** Mark term iChild as being a child of term iParent
139446*/
139447static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
139448 pWC->a[iChild].iParent = iParent;
139449 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
139450 pWC->a[iParent].nChild++;
139451}
139452
139453/*
139454** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
139455** a conjunction, then return just pTerm when N==0. If N is exceeds
139456** the number of available subterms, return NULL.
139457*/
139458static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
139459 if( pTerm->eOperator!=WO_AND0x0400 ){
139460 return N==0 ? pTerm : 0;
139461 }
139462 if( N<pTerm->u.pAndInfo->wc.nTerm ){
139463 return &pTerm->u.pAndInfo->wc.a[N];
139464 }
139465 return 0;
139466}
139467
139468/*
139469** Subterms pOne and pTwo are contained within WHERE clause pWC. The
139470** two subterms are in disjunction - they are OR-ed together.
139471**
139472** If these two terms are both of the form: "A op B" with the same
139473** A and B values but different operators and if the operators are
139474** compatible (if one is = and the other is <, for example) then
139475** add a new virtual AND term to pWC that is the combination of the
139476** two.
139477**
139478** Some examples:
139479**
139480** x<y OR x=y --> x<=y
139481** x=y OR x=y --> x=y
139482** x<=y OR x<y --> x<=y
139483**
139484** The following is NOT generated:
139485**
139486** x<y OR x>y --> x!=y
139487*/
139488static void whereCombineDisjuncts(
139489 SrcList *pSrc, /* the FROM clause */
139490 WhereClause *pWC, /* The complete WHERE clause */
139491 WhereTerm *pOne, /* First disjunct */
139492 WhereTerm *pTwo /* Second disjunct */
139493){
139494 u16 eOp = pOne->eOperator | pTwo->eOperator;
139495 sqlite3 *db; /* Database connection (for malloc) */
139496 Expr *pNew; /* New virtual expression */
139497 int op; /* Operator for the combined expression */
139498 int idxNew; /* Index in pWC of the next virtual term */
139499
139500 if( (pOne->eOperator & (WO_EQ0x0002|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))))==0 ) return;
139501 if( (pTwo->eOperator & (WO_EQ0x0002|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))))==0 ) return;
139502 if( (eOp & (WO_EQ0x0002|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))))!=eOp
139503 && (eOp & (WO_EQ0x0002|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))))!=eOp ) return;
139504 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 )((void) (0));
139505 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 )((void) (0));
139506 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
139507 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
139508 /* If we reach this point, it means the two subterms can be combined */
139509 if( (eOp & (eOp-1))!=0 ){
139510 if( eOp & (WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))) ){
139511 eOp = WO_LE(0x0002<<(55 -53));
139512 }else{
139513 assert( eOp & (WO_GT|WO_GE) )((void) (0));
139514 eOp = WO_GE(0x0002<<(57 -53));
139515 }
139516 }
139517 db = pWC->pWInfo->pParse->db;
139518 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
139519 if( pNew==0 ) return;
139520 for(op=TK_EQ53; eOp!=(WO_EQ0x0002<<(op-TK_EQ53)); op++){ assert( op<TK_GE )((void) (0)); }
139521 pNew->op = op;
139522 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL0x02|TERM_DYNAMIC0x01);
139523 exprAnalyze(pSrc, pWC, idxNew);
139524}
139525
139526#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
139527/*
139528** Analyze a term that consists of two or more OR-connected
139529** subterms. So in:
139530**
139531** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
139532** ^^^^^^^^^^^^^^^^^^^^
139533**
139534** This routine analyzes terms such as the middle term in the above example.
139535** A WhereOrTerm object is computed and attached to the term under
139536** analysis, regardless of the outcome of the analysis. Hence:
139537**
139538** WhereTerm.wtFlags |= TERM_ORINFO
139539** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
139540**
139541** The term being analyzed must have two or more of OR-connected subterms.
139542** A single subterm might be a set of AND-connected sub-subterms.
139543** Examples of terms under analysis:
139544**
139545** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
139546** (B) x=expr1 OR expr2=x OR x=expr3
139547** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
139548** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
139549** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
139550** (F) x>A OR (x=A AND y>=B)
139551**
139552** CASE 1:
139553**
139554** If all subterms are of the form T.C=expr for some single column of C and
139555** a single table T (as shown in example B above) then create a new virtual
139556** term that is an equivalent IN expression. In other words, if the term
139557** being analyzed is:
139558**
139559** x = expr1 OR expr2 = x OR x = expr3
139560**
139561** then create a new virtual term like this:
139562**
139563** x IN (expr1,expr2,expr3)
139564**
139565** CASE 2:
139566**
139567** If there are exactly two disjuncts and one side has x>A and the other side
139568** has x=A (for the same x and A) then add a new virtual conjunct term to the
139569** WHERE clause of the form "x>=A". Example:
139570**
139571** x>A OR (x=A AND y>B) adds: x>=A
139572**
139573** The added conjunct can sometimes be helpful in query planning.
139574**
139575** CASE 3:
139576**
139577** If all subterms are indexable by a single table T, then set
139578**
139579** WhereTerm.eOperator = WO_OR
139580** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
139581**
139582** A subterm is "indexable" if it is of the form
139583** "T.C <op> <expr>" where C is any column of table T and
139584** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
139585** A subterm is also indexable if it is an AND of two or more
139586** subsubterms at least one of which is indexable. Indexable AND
139587** subterms have their eOperator set to WO_AND and they have
139588** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
139589**
139590** From another point of view, "indexable" means that the subterm could
139591** potentially be used with an index if an appropriate index exists.
139592** This analysis does not consider whether or not the index exists; that
139593** is decided elsewhere. This analysis only looks at whether subterms
139594** appropriate for indexing exist.
139595**
139596** All examples A through E above satisfy case 3. But if a term
139597** also satisfies case 1 (such as B) we know that the optimizer will
139598** always prefer case 1, so in that case we pretend that case 3 is not
139599** satisfied.
139600**
139601** It might be the case that multiple tables are indexable. For example,
139602** (E) above is indexable on tables P, Q, and R.
139603**
139604** Terms that satisfy case 3 are candidates for lookup by using
139605** separate indices to find rowids for each subterm and composing
139606** the union of all rowids using a RowSet object. This is similar
139607** to "bitmap indices" in other database engines.
139608**
139609** OTHERWISE:
139610**
139611** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
139612** zero. This term is not useful for search.
139613*/
139614static void exprAnalyzeOrTerm(
139615 SrcList *pSrc, /* the FROM clause */
139616 WhereClause *pWC, /* the complete WHERE clause */
139617 int idxTerm /* Index of the OR-term to be analyzed */
139618){
139619 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
139620 Parse *pParse = pWInfo->pParse; /* Parser context */
139621 sqlite3 *db = pParse->db; /* Database connection */
139622 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
139623 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
139624 int i; /* Loop counters */
139625 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
139626 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
139627 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
139628 Bitmask chngToIN; /* Tables that might satisfy case 1 */
139629 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
139630
139631 /*
139632 ** Break the OR clause into its separate subterms. The subterms are
139633 ** stored in a WhereClause structure containing within the WhereOrInfo
139634 ** object that is attached to the original OR clause term.
139635 */
139636 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 )((void) (0));
139637 assert( pExpr->op==TK_OR )((void) (0));
139638 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
139639 if( pOrInfo==0 ) return;
139640 pTerm->wtFlags |= TERM_ORINFO0x10;
139641 pOrWc = &pOrInfo->wc;
139642 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
139643 sqlite3WhereClauseInit(pOrWc, pWInfo);
139644 sqlite3WhereSplit(pOrWc, pExpr, TK_OR43);
139645 sqlite3WhereExprAnalyze(pSrc, pOrWc);
139646 if( db->mallocFailed ) return;
139647 assert( pOrWc->nTerm>=2 )((void) (0));
139648
139649 /*
139650 ** Compute the set of tables that might satisfy cases 1 or 3.
139651 */
139652 indexable = ~(Bitmask)0;
139653 chngToIN = ~(Bitmask)0;
139654 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
139655 if( (pOrTerm->eOperator & WO_SINGLE0x01ff)==0 ){
139656 WhereAndInfo *pAndInfo;
139657 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 )((void) (0));
139658 chngToIN = 0;
139659 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
139660 if( pAndInfo ){
139661 WhereClause *pAndWC;
139662 WhereTerm *pAndTerm;
139663 int j;
139664 Bitmask b = 0;
139665 pOrTerm->u.pAndInfo = pAndInfo;
139666 pOrTerm->wtFlags |= TERM_ANDINFO0x20;
139667 pOrTerm->eOperator = WO_AND0x0400;
139668 pAndWC = &pAndInfo->wc;
139669 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
139670 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
139671 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND44);
139672 sqlite3WhereExprAnalyze(pSrc, pAndWC);
139673 pAndWC->pOuter = pWC;
139674 if( !db->mallocFailed ){
139675 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
139676 assert( pAndTerm->pExpr )((void) (0));
139677 if( allowedOp(pAndTerm->pExpr->op)
139678 || pAndTerm->eOperator==WO_AUX0x0040
139679 ){
139680 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
139681 }
139682 }
139683 }
139684 indexable &= b;
139685 }
139686 }else if( pOrTerm->wtFlags & TERM_COPIED0x08 ){
139687 /* Skip this term for now. We revisit it when we process the
139688 ** corresponding TERM_VIRTUAL term */
139689 }else{
139690 Bitmask b;
139691 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
139692 if( pOrTerm->wtFlags & TERM_VIRTUAL0x02 ){
139693 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
139694 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
139695 }
139696 indexable &= b;
139697 if( (pOrTerm->eOperator & WO_EQ0x0002)==0 ){
139698 chngToIN = 0;
139699 }else{
139700 chngToIN &= b;
139701 }
139702 }
139703 }
139704
139705 /*
139706 ** Record the set of tables that satisfy case 3. The set might be
139707 ** empty.
139708 */
139709 pOrInfo->indexable = indexable;
139710 if( indexable ){
139711 pTerm->eOperator = WO_OR0x0200;
139712 pWC->hasOr = 1;
139713 }else{
139714 pTerm->eOperator = WO_OR0x0200;
139715 }
139716
139717 /* For a two-way OR, attempt to implementation case 2.
139718 */
139719 if( indexable && pOrWc->nTerm==2 ){
139720 int iOne = 0;
139721 WhereTerm *pOne;
139722 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
139723 int iTwo = 0;
139724 WhereTerm *pTwo;
139725 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
139726 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
139727 }
139728 }
139729 }
139730
139731 /*
139732 ** chngToIN holds a set of tables that *might* satisfy case 1. But
139733 ** we have to do some additional checking to see if case 1 really
139734 ** is satisfied.
139735 **
139736 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
139737 ** that there is no possibility of transforming the OR clause into an
139738 ** IN operator because one or more terms in the OR clause contain
139739 ** something other than == on a column in the single table. The 1-bit
139740 ** case means that every term of the OR clause is of the form
139741 ** "table.column=expr" for some single table. The one bit that is set
139742 ** will correspond to the common table. We still need to check to make
139743 ** sure the same column is used on all terms. The 2-bit case is when
139744 ** the all terms are of the form "table1.column=table2.column". It
139745 ** might be possible to form an IN operator with either table1.column
139746 ** or table2.column as the LHS if either is common to every term of
139747 ** the OR clause.
139748 **
139749 ** Note that terms of the form "table.column1=table.column2" (the
139750 ** same table on both sizes of the ==) cannot be optimized.
139751 */
139752 if( chngToIN ){
139753 int okToChngToIN = 0; /* True if the conversion to IN is valid */
139754 int iColumn = -1; /* Column index on lhs of IN operator */
139755 int iCursor = -1; /* Table cursor common to all terms */
139756 int j = 0; /* Loop counter */
139757
139758 /* Search for a table and column that appears on one side or the
139759 ** other of the == operator in every subterm. That table and column
139760 ** will be recorded in iCursor and iColumn. There might not be any
139761 ** such table and column. Set okToChngToIN if an appropriate table
139762 ** and column is found but leave okToChngToIN false if not found.
139763 */
139764 for(j=0; j<2 && !okToChngToIN; j++){
139765 Expr *pLeft = 0;
139766 pOrTerm = pOrWc->a;
139767 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
139768 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
139769 pOrTerm->wtFlags &= ~TERM_OR_OK0x40;
139770 if( pOrTerm->leftCursor==iCursor ){
139771 /* This is the 2-bit case and we are on the second iteration and
139772 ** current term is from the first iteration. So skip this term. */
139773 assert( j==1 )((void) (0));
139774 continue;
139775 }
139776 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
139777 pOrTerm->leftCursor))==0 ){
139778 /* This term must be of the form t1.a==t2.b where t2 is in the
139779 ** chngToIN set but t1 is not. This term will be either preceded
139780 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
139781 ** and use its inversion. */
139782 testcase( pOrTerm->wtFlags & TERM_COPIED );
139783 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
139784 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) )((void) (0));
139785 continue;
139786 }
139787 iColumn = pOrTerm->u.leftColumn;
139788 iCursor = pOrTerm->leftCursor;
139789 pLeft = pOrTerm->pExpr->pLeft;
139790 break;
139791 }
139792 if( i<0 ){
139793 /* No candidate table+column was found. This can only occur
139794 ** on the second iteration */
139795 assert( j==1 )((void) (0));
139796 assert( IsPowerOfTwo(chngToIN) )((void) (0));
139797 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) )((void) (0));
139798 break;
139799 }
139800 testcase( j==1 );
139801
139802 /* We have found a candidate table and column. Check to see if that
139803 ** table and column is common to every term in the OR clause */
139804 okToChngToIN = 1;
139805 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
139806 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
139807 if( pOrTerm->leftCursor!=iCursor ){
139808 pOrTerm->wtFlags &= ~TERM_OR_OK0x40;
139809 }else if( pOrTerm->u.leftColumn!=iColumn || (iColumn==XN_EXPR(-2)
139810 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
139811 )){
139812 okToChngToIN = 0;
139813 }else{
139814 int affLeft, affRight;
139815 /* If the right-hand side is also a column, then the affinities
139816 ** of both right and left sides must be such that no type
139817 ** conversions are required on the right. (Ticket #2249)
139818 */
139819 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
139820 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
139821 if( affRight!=0 && affRight!=affLeft ){
139822 okToChngToIN = 0;
139823 }else{
139824 pOrTerm->wtFlags |= TERM_OR_OK0x40;
139825 }
139826 }
139827 }
139828 }
139829
139830 /* At this point, okToChngToIN is true if original pTerm satisfies
139831 ** case 1. In that case, construct a new virtual term that is
139832 ** pTerm converted into an IN operator.
139833 */
139834 if( okToChngToIN ){
139835 Expr *pDup; /* A transient duplicate expression */
139836 ExprList *pList = 0; /* The RHS of the IN operator */
139837 Expr *pLeft = 0; /* The LHS of the IN operator */
139838 Expr *pNew; /* The complete IN operator */
139839
139840 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
139841 if( (pOrTerm->wtFlags & TERM_OR_OK0x40)==0 ) continue;
139842 assert( pOrTerm->eOperator & WO_EQ )((void) (0));
139843 assert( pOrTerm->leftCursor==iCursor )((void) (0));
139844 assert( pOrTerm->u.leftColumn==iColumn )((void) (0));
139845 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
139846 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
139847 pLeft = pOrTerm->pExpr->pLeft;
139848 }
139849 assert( pLeft!=0 )((void) (0));
139850 pDup = sqlite3ExprDup(db, pLeft, 0);
139851 pNew = sqlite3PExpr(pParse, TK_IN49, pDup, 0);
139852 if( pNew ){
139853 int idxNew;
139854 transferJoinMarkings(pNew, pExpr);
139855 assert( !ExprHasProperty(pNew, EP_xIsSelect) )((void) (0));
139856 pNew->x.pList = pList;
139857 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL0x02|TERM_DYNAMIC0x01);
139858 testcase( idxNew==0 );
139859 exprAnalyze(pSrc, pWC, idxNew);
139860 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where used again */
139861 markTermAsChild(pWC, idxNew, idxTerm);
139862 }else{
139863 sqlite3ExprListDelete(db, pList);
139864 }
139865 }
139866 }
139867}
139868#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
139869
139870/*
139871** We already know that pExpr is a binary operator where both operands are
139872** column references. This routine checks to see if pExpr is an equivalence
139873** relation:
139874** 1. The SQLITE_Transitive optimization must be enabled
139875** 2. Must be either an == or an IS operator
139876** 3. Not originating in the ON clause of an OUTER JOIN
139877** 4. The affinities of A and B must be compatible
139878** 5a. Both operands use the same collating sequence OR
139879** 5b. The overall collating sequence is BINARY
139880** If this routine returns TRUE, that means that the RHS can be substituted
139881** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
139882** This is an optimization. No harm comes from returning 0. But if 1 is
139883** returned when it should not be, then incorrect answers might result.
139884*/
139885static int termIsEquivalence(Parse *pParse, Expr *pExpr){
139886 char aff1, aff2;
139887 CollSeq *pColl;
139888 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive)(((pParse->db)->dbOptFlags&(0x0080))==0) ) return 0;
139889 if( pExpr->op!=TK_EQ53 && pExpr->op!=TK_IS45 ) return 0;
139890 if( ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0) ) return 0;
139891 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
139892 aff2 = sqlite3ExprAffinity(pExpr->pRight);
139893 if( aff1!=aff2
139894 && (!sqlite3IsNumericAffinity(aff1)((aff1)>='C') || !sqlite3IsNumericAffinity(aff2)((aff2)>='C'))
139895 ){
139896 return 0;
139897 }
139898 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
139899 if( sqlite3IsBinary(pColl) ) return 1;
139900 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
139901}
139902
139903/*
139904** Recursively walk the expressions of a SELECT statement and generate
139905** a bitmask indicating which tables are used in that expression
139906** tree.
139907*/
139908static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
139909 Bitmask mask = 0;
139910 while( pS ){
139911 SrcList *pSrc = pS->pSrc;
139912 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
139913 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
139914 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
139915 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
139916 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
139917 if( ALWAYS(pSrc!=0)(pSrc!=0) ){
139918 int i;
139919 for(i=0; i<pSrc->nSrc; i++){
139920 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
139921 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
139922 if( pSrc->a[i].fg.isTabFunc ){
139923 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
139924 }
139925 }
139926 }
139927 pS = pS->pPrior;
139928 }
139929 return mask;
139930}
139931
139932/*
139933** Expression pExpr is one operand of a comparison operator that might
139934** be useful for indexing. This routine checks to see if pExpr appears
139935** in any index. Return TRUE (1) if pExpr is an indexed term and return
139936** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
139937** number of the table that is indexed and aiCurCol[1] to the column number
139938** of the column that is indexed, or XN_EXPR (-2) if an expression is being
139939** indexed.
139940**
139941** If pExpr is a TK_COLUMN column reference, then this routine always returns
139942** true even if that particular column is not indexed, because the column
139943** might be added to an automatic index later.
139944*/
139945static SQLITE_NOINLINE__attribute__((noinline)) int exprMightBeIndexed2(
139946 SrcList *pFrom, /* The FROM clause */
139947 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
139948 int *aiCurCol, /* Write the referenced table cursor and column here */
139949 Expr *pExpr /* An operand of a comparison operator */
139950){
139951 Index *pIdx;
139952 int i;
139953 int iCur;
139954 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
139955 iCur = pFrom->a[i].iCursor;
139956 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
139957 if( pIdx->aColExpr==0 ) continue;
139958 for(i=0; i<pIdx->nKeyCol; i++){
139959 if( pIdx->aiColumn[i]!=XN_EXPR(-2) ) continue;
139960 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
139961 aiCurCol[0] = iCur;
139962 aiCurCol[1] = XN_EXPR(-2);
139963 return 1;
139964 }
139965 }
139966 }
139967 return 0;
139968}
139969static int exprMightBeIndexed(
139970 SrcList *pFrom, /* The FROM clause */
139971 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
139972 int *aiCurCol, /* Write the referenced table cursor & column here */
139973 Expr *pExpr, /* An operand of a comparison operator */
139974 int op /* The specific comparison operator */
139975){
139976 /* If this expression is a vector to the left or right of a
139977 ** inequality constraint (>, <, >= or <=), perform the processing
139978 ** on the first element of the vector. */
139979 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE )((void) (0));
139980 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE )((void) (0));
139981 assert( op<=TK_GE )((void) (0));
139982 if( pExpr->op==TK_VECTOR169 && (op>=TK_GT54 && ALWAYS(op<=TK_GE)(op<=57)) ){
139983 pExpr = pExpr->x.pList->a[0].pExpr;
139984 }
139985
139986 if( pExpr->op==TK_COLUMN162 ){
139987 aiCurCol[0] = pExpr->iTable;
139988 aiCurCol[1] = pExpr->iColumn;
139989 return 1;
139990 }
139991 if( mPrereq==0 ) return 0; /* No table references */
139992 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
139993 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
139994}
139995
139996/*
139997** The input to this routine is an WhereTerm structure with only the
139998** "pExpr" field filled in. The job of this routine is to analyze the
139999** subexpression and populate all the other fields of the WhereTerm
140000** structure.
140001**
140002** If the expression is of the form "<expr> <op> X" it gets commuted
140003** to the standard form of "X <op> <expr>".
140004**
140005** If the expression is of the form "X <op> Y" where both X and Y are
140006** columns, then the original expression is unchanged and a new virtual
140007** term of the form "Y <op> X" is added to the WHERE clause and
140008** analyzed separately. The original term is marked with TERM_COPIED
140009** and the new term is marked with TERM_DYNAMIC (because it's pExpr
140010** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
140011** is a commuted copy of a prior term.) The original term has nChild=1
140012** and the copy has idxParent set to the index of the original term.
140013*/
140014static void exprAnalyze(
140015 SrcList *pSrc, /* the FROM clause */
140016 WhereClause *pWC, /* the WHERE clause */
140017 int idxTerm /* Index of the term to be analyzed */
140018){
140019 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
140020 WhereTerm *pTerm; /* The term to be analyzed */
140021 WhereMaskSet *pMaskSet; /* Set of table index masks */
140022 Expr *pExpr; /* The expression to be analyzed */
140023 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
140024 Bitmask prereqAll; /* Prerequesites of pExpr */
140025 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
140026 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
140027 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
140028 int noCase = 0; /* uppercase equivalent to lowercase */
140029 int op; /* Top-level operator. pExpr->op */
140030 Parse *pParse = pWInfo->pParse; /* Parsing context */
140031 sqlite3 *db = pParse->db; /* Database connection */
140032 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
140033 int nLeft; /* Number of elements on left side vector */
140034
140035 if( db->mallocFailed ){
140036 return;
140037 }
140038 pTerm = &pWC->a[idxTerm];
140039 pMaskSet = &pWInfo->sMaskSet;
140040 pExpr = pTerm->pExpr;
140041 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE )((void) (0));
140042 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
140043 op = pExpr->op;
140044 if( op==TK_IN49 ){
140045 assert( pExpr->pRight==0 )((void) (0));
140046 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
140047 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
140048 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
140049 }else{
140050 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
140051 }
140052 }else if( op==TK_ISNULL50 ){
140053 pTerm->prereqRight = 0;
140054 }else{
140055 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
140056 }
140057 pMaskSet->bVarSelect = 0;
140058 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
140059 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT0x1000;
140060 if( ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0) ){
140061 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
140062 prereqAll |= x;
140063 extraRight = x-1; /* ON clause terms may not be used with an index
140064 ** on left table of a LEFT JOIN. Ticket #3015 */
140065 if( (prereqAll>>1)>=x ){
140066 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
140067 return;
140068 }
140069 }
140070 pTerm->prereqAll = prereqAll;
140071 pTerm->leftCursor = -1;
140072 pTerm->iParent = -1;
140073 pTerm->eOperator = 0;
140074 if( allowedOp(op) ){
140075 int aiCurCol[2];
140076 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
140077 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
140078 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL0x1fff : WO_EQUIV0x0800;
140079
140080 if( pTerm->iField>0 ){
140081 assert( op==TK_IN )((void) (0));
140082 assert( pLeft->op==TK_VECTOR )((void) (0));
140083 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
140084 }
140085
140086 if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
140087 pTerm->leftCursor = aiCurCol[0];
140088 pTerm->u.leftColumn = aiCurCol[1];
140089 pTerm->eOperator = operatorMask(op) & opMask;
140090 }
140091 if( op==TK_IS45 ) pTerm->wtFlags |= TERM_IS0x800;
140092 if( pRight
140093 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
140094 ){
140095 WhereTerm *pNew;
140096 Expr *pDup;
140097 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
140098 assert( pTerm->iField==0 )((void) (0));
140099 if( pTerm->leftCursor>=0 ){
140100 int idxNew;
140101 pDup = sqlite3ExprDup(db, pExpr, 0);
140102 if( db->mallocFailed ){
140103 sqlite3ExprDelete(db, pDup);
140104 return;
140105 }
140106 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL0x02|TERM_DYNAMIC0x01);
140107 if( idxNew==0 ) return;
140108 pNew = &pWC->a[idxNew];
140109 markTermAsChild(pWC, idxNew, idxTerm);
140110 if( op==TK_IS45 ) pNew->wtFlags |= TERM_IS0x800;
140111 pTerm = &pWC->a[idxTerm];
140112 pTerm->wtFlags |= TERM_COPIED0x08;
140113
140114 if( termIsEquivalence(pParse, pDup) ){
140115 pTerm->eOperator |= WO_EQUIV0x0800;
140116 eExtraOp = WO_EQUIV0x0800;
140117 }
140118 }else{
140119 pDup = pExpr;
140120 pNew = pTerm;
140121 }
140122 exprCommute(pParse, pDup);
140123 pNew->leftCursor = aiCurCol[0];
140124 pNew->u.leftColumn = aiCurCol[1];
140125 testcase( (prereqLeft | extraRight) != prereqLeft );
140126 pNew->prereqRight = prereqLeft | extraRight;
140127 pNew->prereqAll = prereqAll;
140128 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
140129 }
140130 }
140131
140132#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
140133 /* If a term is the BETWEEN operator, create two new virtual terms
140134 ** that define the range that the BETWEEN implements. For example:
140135 **
140136 ** a BETWEEN b AND c
140137 **
140138 ** is converted into:
140139 **
140140 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
140141 **
140142 ** The two new terms are added onto the end of the WhereClause object.
140143 ** The new terms are "dynamic" and are children of the original BETWEEN
140144 ** term. That means that if the BETWEEN term is coded, the children are
140145 ** skipped. Or, if the children are satisfied by an index, the original
140146 ** BETWEEN term is skipped.
140147 */
140148 else if( pExpr->op==TK_BETWEEN48 && pWC->op==TK_AND44 ){
140149 ExprList *pList = pExpr->x.pList;
140150 int i;
140151 static const u8 ops[] = {TK_GE57, TK_LE55};
140152 assert( pList!=0 )((void) (0));
140153 assert( pList->nExpr==2 )((void) (0));
140154 for(i=0; i<2; i++){
140155 Expr *pNewExpr;
140156 int idxNew;
140157 pNewExpr = sqlite3PExpr(pParse, ops[i],
140158 sqlite3ExprDup(db, pExpr->pLeft, 0),
140159 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
140160 transferJoinMarkings(pNewExpr, pExpr);
140161 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL0x02|TERM_DYNAMIC0x01);
140162 testcase( idxNew==0 );
140163 exprAnalyze(pSrc, pWC, idxNew);
140164 pTerm = &pWC->a[idxTerm];
140165 markTermAsChild(pWC, idxNew, idxTerm);
140166 }
140167 }
140168#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
140169
140170#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
140171 /* Analyze a term that is composed of two or more subterms connected by
140172 ** an OR operator.
140173 */
140174 else if( pExpr->op==TK_OR43 ){
140175 assert( pWC->op==TK_AND )((void) (0));
140176 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
140177 pTerm = &pWC->a[idxTerm];
140178 }
140179#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
140180
140181#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
140182 /* Add constraints to reduce the search space on a LIKE or GLOB
140183 ** operator.
140184 **
140185 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
140186 **
140187 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
140188 **
140189 ** The last character of the prefix "abc" is incremented to form the
140190 ** termination condition "abd". If case is not significant (the default
140191 ** for LIKE) then the lower-bound is made all uppercase and the upper-
140192 ** bound is made all lowercase so that the bounds also work when comparing
140193 ** BLOBs.
140194 */
140195 if( pWC->op==TK_AND44
140196 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
140197 ){
140198 Expr *pLeft; /* LHS of LIKE/GLOB operator */
140199 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
140200 Expr *pNewExpr1;
140201 Expr *pNewExpr2;
140202 int idxNew1;
140203 int idxNew2;
140204 const char *zCollSeqName; /* Name of collating sequence */
140205 const u16 wtFlags = TERM_LIKEOPT0x100 | TERM_VIRTUAL0x02 | TERM_DYNAMIC0x01;
140206
140207 pLeft = pExpr->x.pList->a[1].pExpr;
140208 pStr2 = sqlite3ExprDup(db, pStr1, 0);
140209
140210 /* Convert the lower bound to upper-case and the upper bound to
140211 ** lower-case (upper-case is less than lower-case in ASCII) so that
140212 ** the range constraints also work for BLOBs
140213 */
140214 if( noCase && !pParse->db->mallocFailed ){
140215 int i;
140216 char c;
140217 pTerm->wtFlags |= TERM_LIKE0x400;
140218 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
140219 pStr1->u.zToken[i] = sqlite3Toupper(c)((c)&~(sqlite3CtypeMap[(unsigned char)(c)]&0x20));
140220 pStr2->u.zToken[i] = sqlite3Tolower(c)(sqlite3UpperToLower[(unsigned char)(c)]);
140221 }
140222 }
140223
140224 if( !db->mallocFailed ){
140225 u8 c, *pC; /* Last character before the first wildcard */
140226 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
140227 c = *pC;
140228 if( noCase ){
140229 /* The point is to increment the last character before the first
140230 ** wildcard. But if we increment '@', that will push it into the
140231 ** alphabetic range where case conversions will mess up the
140232 ** inequality. To avoid this, make sure to also run the full
140233 ** LIKE on all candidate expressions by clearing the isComplete flag
140234 */
140235 if( c=='A'-1 ) isComplete = 0;
140236 c = sqlite3UpperToLower[c];
140237 }
140238 *pC = c + 1;
140239 }
140240 zCollSeqName = noCase ? "NOCASE" : sqlite3StrBINARY;
140241 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
140242 pNewExpr1 = sqlite3PExpr(pParse, TK_GE57,
140243 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
140244 pStr1);
140245 transferJoinMarkings(pNewExpr1, pExpr);
140246 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
140247 testcase( idxNew1==0 );
140248 exprAnalyze(pSrc, pWC, idxNew1);
140249 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
140250 pNewExpr2 = sqlite3PExpr(pParse, TK_LT56,
140251 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
140252 pStr2);
140253 transferJoinMarkings(pNewExpr2, pExpr);
140254 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
140255 testcase( idxNew2==0 );
140256 exprAnalyze(pSrc, pWC, idxNew2);
140257 pTerm = &pWC->a[idxTerm];
140258 if( isComplete ){
140259 markTermAsChild(pWC, idxNew1, idxTerm);
140260 markTermAsChild(pWC, idxNew2, idxTerm);
140261 }
140262 }
140263#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
140264
140265#ifndef SQLITE_OMIT_VIRTUALTABLE
140266 /* Add a WO_AUX auxiliary term to the constraint set if the
140267 ** current expression is of the form "column OP expr" where OP
140268 ** is an operator that gets passed into virtual tables but which is
140269 ** not normally optimized for ordinary tables. In other words, OP
140270 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
140271 ** This information is used by the xBestIndex methods of
140272 ** virtual tables. The native query optimizer does not attempt
140273 ** to do anything with MATCH functions.
140274 */
140275 if( pWC->op==TK_AND44 ){
140276 Expr *pRight = 0, *pLeft = 0;
140277 int res = isAuxiliaryVtabOperator(db, pExpr, &eOp2, &pLeft, &pRight);
140278 while( res-- > 0 ){
140279 int idxNew;
140280 WhereTerm *pNewTerm;
140281 Bitmask prereqColumn, prereqExpr;
140282
140283 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
140284 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
140285 if( (prereqExpr & prereqColumn)==0 ){
140286 Expr *pNewExpr;
140287 pNewExpr = sqlite3PExpr(pParse, TK_MATCH46,
140288 0, sqlite3ExprDup(db, pRight, 0));
140289 if( ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0) && pNewExpr ){
140290 ExprSetProperty(pNewExpr, EP_FromJoin)(pNewExpr)->flags|=(0x000001);
140291 }
140292 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL0x02|TERM_DYNAMIC0x01);
140293 testcase( idxNew==0 );
140294 pNewTerm = &pWC->a[idxNew];
140295 pNewTerm->prereqRight = prereqExpr;
140296 pNewTerm->leftCursor = pLeft->iTable;
140297 pNewTerm->u.leftColumn = pLeft->iColumn;
140298 pNewTerm->eOperator = WO_AUX0x0040;
140299 pNewTerm->eMatchOp = eOp2;
140300 markTermAsChild(pWC, idxNew, idxTerm);
140301 pTerm = &pWC->a[idxTerm];
140302 pTerm->wtFlags |= TERM_COPIED0x08;
140303 pNewTerm->prereqAll = pTerm->prereqAll;
140304 }
140305 SWAP(Expr*, pLeft, pRight){Expr* t=pLeft; pLeft=pRight; pRight=t;};
140306 }
140307 }
140308#endif /* SQLITE_OMIT_VIRTUALTABLE */
140309
140310 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
140311 ** new terms for each component comparison - "a = ?" and "b = ?". The
140312 ** new terms completely replace the original vector comparison, which is
140313 ** no longer used.
140314 **
140315 ** This is only required if at least one side of the comparison operation
140316 ** is not a sub-select. */
140317 if( pWC->op==TK_AND44
140318 && (pExpr->op==TK_EQ53 || pExpr->op==TK_IS45)
140319 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
140320 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
140321 && ( (pExpr->pLeft->flags & EP_xIsSelect0x000800)==0
140322 || (pExpr->pRight->flags & EP_xIsSelect0x000800)==0)
140323 ){
140324 int i;
140325 for(i=0; i<nLeft; i++){
140326 int idxNew;
140327 Expr *pNew;
140328 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
140329 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
140330
140331 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
140332 transferJoinMarkings(pNew, pExpr);
140333 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC0x01);
140334 exprAnalyze(pSrc, pWC, idxNew);
140335 }
140336 pTerm = &pWC->a[idxTerm];
140337 pTerm->wtFlags |= TERM_CODED0x04|TERM_VIRTUAL0x02; /* Disable the original */
140338 pTerm->eOperator = 0;
140339 }
140340
140341 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
140342 ** a virtual term for each vector component. The expression object
140343 ** used by each such virtual term is pExpr (the full vector IN(...)
140344 ** expression). The WhereTerm.iField variable identifies the index within
140345 ** the vector on the LHS that the virtual term represents.
140346 **
140347 ** This only works if the RHS is a simple SELECT, not a compound
140348 */
140349 if( pWC->op==TK_AND44 && pExpr->op==TK_IN49 && pTerm->iField==0
140350 && pExpr->pLeft->op==TK_VECTOR169
140351 && pExpr->x.pSelect->pPrior==0
140352 ){
140353 int i;
140354 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
140355 int idxNew;
140356 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL0x02);
140357 pWC->a[idxNew].iField = i+1;
140358 exprAnalyze(pSrc, pWC, idxNew);
140359 markTermAsChild(pWC, idxNew, idxTerm);
140360 }
140361 }
140362
140363#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
140364 /* When sqlite_stat3 histogram data is available an operator of the
140365 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
140366 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
140367 ** virtual term of that form.
140368 **
140369 ** Note that the virtual term must be tagged with TERM_VNULL.
140370 */
140371 if( pExpr->op==TK_NOTNULL51
140372 && pExpr->pLeft->op==TK_COLUMN162
140373 && pExpr->pLeft->iColumn>=0
140374 && !ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0)
140375 && OptimizationEnabled(db, SQLITE_Stat34)(((db)->dbOptFlags&(0x0800))==0)
140376 ){
140377 Expr *pNewExpr;
140378 Expr *pLeft = pExpr->pLeft;
140379 int idxNew;
140380 WhereTerm *pNewTerm;
140381
140382 pNewExpr = sqlite3PExpr(pParse, TK_GT54,
140383 sqlite3ExprDup(db, pLeft, 0),
140384 sqlite3ExprAlloc(db, TK_NULL114, 0, 0));
140385
140386 idxNew = whereClauseInsert(pWC, pNewExpr,
140387 TERM_VIRTUAL0x02|TERM_DYNAMIC0x01|TERM_VNULL0x00);
140388 if( idxNew ){
140389 pNewTerm = &pWC->a[idxNew];
140390 pNewTerm->prereqRight = 0;
140391 pNewTerm->leftCursor = pLeft->iTable;
140392 pNewTerm->u.leftColumn = pLeft->iColumn;
140393 pNewTerm->eOperator = WO_GT(0x0002<<(54 -53));
140394 markTermAsChild(pWC, idxNew, idxTerm);
140395 pTerm = &pWC->a[idxTerm];
140396 pTerm->wtFlags |= TERM_COPIED0x08;
140397 pNewTerm->prereqAll = pTerm->prereqAll;
140398 }
140399 }
140400#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
140401
140402 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
140403 ** an index for tables to the left of the join.
140404 */
140405 testcase( pTerm!=&pWC->a[idxTerm] );
140406 pTerm = &pWC->a[idxTerm];
140407 pTerm->prereqRight |= extraRight;
140408}
140409
140410/***************************************************************************
140411** Routines with file scope above. Interface to the rest of the where.c
140412** subsystem follows.
140413***************************************************************************/
140414
140415/*
140416** This routine identifies subexpressions in the WHERE clause where
140417** each subexpression is separated by the AND operator or some other
140418** operator specified in the op parameter. The WhereClause structure
140419** is filled with pointers to subexpressions. For example:
140420**
140421** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
140422** \________/ \_______________/ \________________/
140423** slot[0] slot[1] slot[2]
140424**
140425** The original WHERE clause in pExpr is unaltered. All this routine
140426** does is make slot[] entries point to substructure within pExpr.
140427**
140428** In the previous sentence and in the diagram, "slot[]" refers to
140429** the WhereClause.a[] array. The slot[] array grows as needed to contain
140430** all terms of the WHERE clause.
140431*/
140432SQLITE_PRIVATEstatic void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
140433 Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
140434 pWC->op = op;
140435 if( pE2==0 ) return;
140436 if( pE2->op!=op ){
140437 whereClauseInsert(pWC, pExpr, 0);
140438 }else{
140439 sqlite3WhereSplit(pWC, pE2->pLeft, op);
140440 sqlite3WhereSplit(pWC, pE2->pRight, op);
140441 }
140442}
140443
140444/*
140445** Initialize a preallocated WhereClause structure.
140446*/
140447SQLITE_PRIVATEstatic void sqlite3WhereClauseInit(
140448 WhereClause *pWC, /* The WhereClause to be initialized */
140449 WhereInfo *pWInfo /* The WHERE processing context */
140450){
140451 pWC->pWInfo = pWInfo;
140452 pWC->hasOr = 0;
140453 pWC->pOuter = 0;
140454 pWC->nTerm = 0;
140455 pWC->nSlot = ArraySize(pWC->aStatic)((int)(sizeof(pWC->aStatic)/sizeof(pWC->aStatic[0])));
140456 pWC->a = pWC->aStatic;
140457}
140458
140459/*
140460** Deallocate a WhereClause structure. The WhereClause structure
140461** itself is not freed. This routine is the inverse of
140462** sqlite3WhereClauseInit().
140463*/
140464SQLITE_PRIVATEstatic void sqlite3WhereClauseClear(WhereClause *pWC){
140465 int i;
140466 WhereTerm *a;
140467 sqlite3 *db = pWC->pWInfo->pParse->db;
140468 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
140469 if( a->wtFlags & TERM_DYNAMIC0x01 ){
140470 sqlite3ExprDelete(db, a->pExpr);
140471 }
140472 if( a->wtFlags & TERM_ORINFO0x10 ){
140473 whereOrInfoDelete(db, a->u.pOrInfo);
140474 }else if( a->wtFlags & TERM_ANDINFO0x20 ){
140475 whereAndInfoDelete(db, a->u.pAndInfo);
140476 }
140477 }
140478 if( pWC->a!=pWC->aStatic ){
140479 sqlite3DbFree(db, pWC->a);
140480 }
140481}
140482
140483
140484/*
140485** These routines walk (recursively) an expression tree and generate
140486** a bitmask indicating which tables are used in that expression
140487** tree.
140488*/
140489SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
140490 Bitmask mask;
140491 if( p->op==TK_COLUMN162 && !ExprHasProperty(p, EP_FixedCol)(((p)->flags&(0x000008))!=0) ){
140492 return sqlite3WhereGetMask(pMaskSet, p->iTable);
140493 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf)(((p)->flags&(0x004000|0x800000))!=0) ){
140494 assert( p->op!=TK_IF_NULL_ROW )((void) (0));
140495 return 0;
140496 }
140497 mask = (p->op==TK_IF_NULL_ROW171) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
140498 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
140499 if( p->pRight ){
140500 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
140501 assert( p->x.pList==0 )((void) (0));
140502 }else if( ExprHasProperty(p, EP_xIsSelect)(((p)->flags&(0x000800))!=0) ){
140503 if( ExprHasProperty(p, EP_VarSelect)(((p)->flags&(0x000020))!=0) ) pMaskSet->bVarSelect = 1;
140504 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
140505 }else if( p->x.pList ){
140506 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
140507 }
140508#ifndef SQLITE_OMIT_WINDOWFUNC
140509 if( p->op==TK_FUNCTION161 && p->y.pWin ){
140510 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
140511 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
140512 }
140513#endif
140514 return mask;
140515}
140516SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
140517 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
140518}
140519SQLITE_PRIVATEstatic Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
140520 int i;
140521 Bitmask mask = 0;
140522 if( pList ){
140523 for(i=0; i<pList->nExpr; i++){
140524 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
140525 }
140526 }
140527 return mask;
140528}
140529
140530
140531/*
140532** Call exprAnalyze on all terms in a WHERE clause.
140533**
140534** Note that exprAnalyze() might add new virtual terms onto the
140535** end of the WHERE clause. We do not want to analyze these new
140536** virtual terms, so start analyzing at the end and work forward
140537** so that the added virtual terms are never processed.
140538*/
140539SQLITE_PRIVATEstatic void sqlite3WhereExprAnalyze(
140540 SrcList *pTabList, /* the FROM clause */
140541 WhereClause *pWC /* the WHERE clause to be analyzed */
140542){
140543 int i;
140544 for(i=pWC->nTerm-1; i>=0; i--){
140545 exprAnalyze(pTabList, pWC, i);
140546 }
140547}
140548
140549/*
140550** For table-valued-functions, transform the function arguments into
140551** new WHERE clause terms.
140552**
140553** Each function argument translates into an equality constraint against
140554** a HIDDEN column in the table.
140555*/
140556SQLITE_PRIVATEstatic void sqlite3WhereTabFuncArgs(
140557 Parse *pParse, /* Parsing context */
140558 struct SrcList_item *pItem, /* The FROM clause term to process */
140559 WhereClause *pWC /* Xfer function arguments to here */
140560){
140561 Table *pTab;
140562 int j, k;
140563 ExprList *pArgs;
140564 Expr *pColRef;
140565 Expr *pTerm;
140566 if( pItem->fg.isTabFunc==0 ) return;
140567 pTab = pItem->pTab;
140568 assert( pTab!=0 )((void) (0));
140569 pArgs = pItem->u1.pFuncArg;
140570 if( pArgs==0 ) return;
140571 for(j=k=0; j<pArgs->nExpr; j++){
140572 Expr *pRhs;
140573 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN0x0002)==0 ){k++;}
140574 if( k>=pTab->nCol ){
140575 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
140576 pTab->zName, j);
140577 return;
140578 }
140579 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN162, 0, 0);
140580 if( pColRef==0 ) return;
140581 pColRef->iTable = pItem->iCursor;
140582 pColRef->iColumn = k++;
140583 pColRef->y.pTab = pTab;
140584 pRhs = sqlite3PExpr(pParse, TK_UPLUS166,
140585 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
140586 pTerm = sqlite3PExpr(pParse, TK_EQ53, pColRef, pRhs);
140587 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC0x01);
140588 }
140589}
140590
140591/************** End of whereexpr.c *******************************************/
140592/************** Begin file where.c *******************************************/
140593/*
140594** 2001 September 15
140595**
140596** The author disclaims copyright to this source code. In place of
140597** a legal notice, here is a blessing:
140598**
140599** May you do good and not evil.
140600** May you find forgiveness for yourself and forgive others.
140601** May you share freely, never taking more than you give.
140602**
140603*************************************************************************
140604** This module contains C code that generates VDBE code used to process
140605** the WHERE clause of SQL statements. This module is responsible for
140606** generating the code that loops through a table looking for applicable
140607** rows. Indices are selected and used to speed the search when doing
140608** so is applicable. Because this module is responsible for selecting
140609** indices, you might also think of this module as the "query optimizer".
140610*/
140611/* #include "sqliteInt.h" */
140612/* #include "whereInt.h" */
140613
140614/*
140615** Extra information appended to the end of sqlite3_index_info but not
140616** visible to the xBestIndex function, at least not directly. The
140617** sqlite3_vtab_collation() interface knows how to reach it, however.
140618**
140619** This object is not an API and can be changed from one release to the
140620** next. As long as allocateIndexInfo() and sqlite3_vtab_collation()
140621** agree on the structure, all will be well.
140622*/
140623typedef struct HiddenIndexInfo HiddenIndexInfo;
140624struct HiddenIndexInfo {
140625 WhereClause *pWC; /* The Where clause being analyzed */
140626 Parse *pParse; /* The parsing context */
140627};
140628
140629/* Forward declaration of methods */
140630static int whereLoopResize(sqlite3*, WhereLoop*, int);
140631
140632/* Test variable that can be set to enable WHERE tracing */
140633#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
140634/***/ int sqlite3WhereTrace = 0;
140635#endif
140636
140637
140638/*
140639** Return the estimated number of output rows from a WHERE clause
140640*/
140641SQLITE_PRIVATEstatic LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
140642 return pWInfo->nRowOut;
140643}
140644
140645/*
140646** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
140647** WHERE clause returns outputs for DISTINCT processing.
140648*/
140649SQLITE_PRIVATEstatic int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
140650 return pWInfo->eDistinct;
140651}
140652
140653/*
140654** Return TRUE if the WHERE clause returns rows in ORDER BY order.
140655** Return FALSE if the output needs to be sorted.
140656*/
140657SQLITE_PRIVATEstatic int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
140658 return pWInfo->nOBSat;
140659}
140660
140661/*
140662** In the ORDER BY LIMIT optimization, if the inner-most loop is known
140663** to emit rows in increasing order, and if the last row emitted by the
140664** inner-most loop did not fit within the sorter, then we can skip all
140665** subsequent rows for the current iteration of the inner loop (because they
140666** will not fit in the sorter either) and continue with the second inner
140667** loop - the loop immediately outside the inner-most.
140668**
140669** When a row does not fit in the sorter (because the sorter already
140670** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the
140671** label returned by this function.
140672**
140673** If the ORDER BY LIMIT optimization applies, the jump destination should
140674** be the continuation for the second-inner-most loop. If the ORDER BY
140675** LIMIT optimization does not apply, then the jump destination should
140676** be the continuation for the inner-most loop.
140677**
140678** It is always safe for this routine to return the continuation of the
140679** inner-most loop, in the sense that a correct answer will result.
140680** Returning the continuation the second inner loop is an optimization
140681** that might make the code run a little faster, but should not change
140682** the final answer.
140683*/
140684SQLITE_PRIVATEstatic int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){
140685 WhereLevel *pInner;
140686 if( !pWInfo->bOrderedInnerLoop ){
140687 /* The ORDER BY LIMIT optimization does not apply. Jump to the
140688 ** continuation of the inner-most loop. */
140689 return pWInfo->iContinue;
140690 }
140691 pInner = &pWInfo->a[pWInfo->nLevel-1];
140692 assert( pInner->addrNxt!=0 )((void) (0));
140693 return pInner->addrNxt;
140694}
140695
140696/*
140697** Return the VDBE address or label to jump to in order to continue
140698** immediately with the next row of a WHERE clause.
140699*/
140700SQLITE_PRIVATEstatic int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
140701 assert( pWInfo->iContinue!=0 )((void) (0));
140702 return pWInfo->iContinue;
140703}
140704
140705/*
140706** Return the VDBE address or label to jump to in order to break
140707** out of a WHERE loop.
140708*/
140709SQLITE_PRIVATEstatic int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
140710 return pWInfo->iBreak;
140711}
140712
140713/*
140714** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to
140715** operate directly on the rowis returned by a WHERE clause. Return
140716** ONEPASS_SINGLE (1) if the statement can operation directly because only
140717** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
140718** optimization can be used on multiple
140719**
140720** If the ONEPASS optimization is used (if this routine returns true)
140721** then also write the indices of open cursors used by ONEPASS
140722** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data
140723** table and iaCur[1] gets the cursor used by an auxiliary index.
140724** Either value may be -1, indicating that cursor is not used.
140725** Any cursors returned will have been opened for writing.
140726**
140727** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
140728** unable to use the ONEPASS optimization.
140729*/
140730SQLITE_PRIVATEstatic int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
140731 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
140732#ifdef WHERETRACE_ENABLED
140733 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF0 ){
140734 sqlite3DebugPrintf("%s cursors: %d %d\n",
140735 pWInfo->eOnePass==ONEPASS_SINGLE1 ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
140736 aiCur[0], aiCur[1]);
140737 }
140738#endif
140739 return pWInfo->eOnePass;
140740}
140741
140742/*
140743** Move the content of pSrc into pDest
140744*/
140745static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
140746 pDest->n = pSrc->n;
140747 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
140748}
140749
140750/*
140751** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
140752**
140753** The new entry might overwrite an existing entry, or it might be
140754** appended, or it might be discarded. Do whatever is the right thing
140755** so that pSet keeps the N_OR_COST best entries seen so far.
140756*/
140757static int whereOrInsert(
140758 WhereOrSet *pSet, /* The WhereOrSet to be updated */
140759 Bitmask prereq, /* Prerequisites of the new entry */
140760 LogEst rRun, /* Run-cost of the new entry */
140761 LogEst nOut /* Number of outputs for the new entry */
140762){
140763 u16 i;
140764 WhereOrCost *p;
140765 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
140766 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
140767 goto whereOrInsert_done;
140768 }
140769 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
140770 return 0;
140771 }
140772 }
140773 if( pSet->n<N_OR_COST3 ){
140774 p = &pSet->a[pSet->n++];
140775 p->nOut = nOut;
140776 }else{
140777 p = pSet->a;
140778 for(i=1; i<pSet->n; i++){
140779 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
140780 }
140781 if( p->rRun<=rRun ) return 0;
140782 }
140783whereOrInsert_done:
140784 p->prereq = prereq;
140785 p->rRun = rRun;
140786 if( p->nOut>nOut ) p->nOut = nOut;
140787 return 1;
140788}
140789
140790/*
140791** Return the bitmask for the given cursor number. Return 0 if
140792** iCursor is not in the set.
140793*/
140794SQLITE_PRIVATEstatic Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
140795 int i;
140796 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 )((void) (0));
140797 for(i=0; i<pMaskSet->n; i++){
140798 if( pMaskSet->ix[i]==iCursor ){
140799 return MASKBIT(i)(((Bitmask)1)<<(i));
140800 }
140801 }
140802 return 0;
140803}
140804
140805/*
140806** Create a new mask for cursor iCursor.
140807**
140808** There is one cursor per table in the FROM clause. The number of
140809** tables in the FROM clause is limited by a test early in the
140810** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
140811** array will never overflow.
140812*/
140813static void createMask(WhereMaskSet *pMaskSet, int iCursor){
140814 assert( pMaskSet->n < ArraySize(pMaskSet->ix) )((void) (0));
140815 pMaskSet->ix[pMaskSet->n++] = iCursor;
140816}
140817
140818/*
140819** Advance to the next WhereTerm that matches according to the criteria
140820** established when the pScan object was initialized by whereScanInit().
140821** Return NULL if there are no more matching WhereTerms.
140822*/
140823static WhereTerm *whereScanNext(WhereScan *pScan){
140824 int iCur; /* The cursor on the LHS of the term */
140825 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
140826 Expr *pX; /* An expression being tested */
140827 WhereClause *pWC; /* Shorthand for pScan->pWC */
140828 WhereTerm *pTerm; /* The term being tested */
140829 int k = pScan->k; /* Where to start scanning */
140830
140831 assert( pScan->iEquiv<=pScan->nEquiv )((void) (0));
140832 pWC = pScan->pWC;
140833 while(1){
140834 iColumn = pScan->aiColumn[pScan->iEquiv-1];
140835 iCur = pScan->aiCur[pScan->iEquiv-1];
140836 assert( pWC!=0 )((void) (0));
140837 do{
140838 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
140839 if( pTerm->leftCursor==iCur
140840 && pTerm->u.leftColumn==iColumn
140841 && (iColumn!=XN_EXPR(-2)
140842 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
140843 pScan->pIdxExpr,iCur)==0)
140844 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)(((pTerm->pExpr)->flags&(0x000001))!=0))
140845 ){
140846 if( (pTerm->eOperator & WO_EQUIV0x0800)!=0
140847 && pScan->nEquiv<ArraySize(pScan->aiCur)((int)(sizeof(pScan->aiCur)/sizeof(pScan->aiCur[0])))
140848 && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN162
140849 ){
140850 int j;
140851 for(j=0; j<pScan->nEquiv; j++){
140852 if( pScan->aiCur[j]==pX->iTable
140853 && pScan->aiColumn[j]==pX->iColumn ){
140854 break;
140855 }
140856 }
140857 if( j==pScan->nEquiv ){
140858 pScan->aiCur[j] = pX->iTable;
140859 pScan->aiColumn[j] = pX->iColumn;
140860 pScan->nEquiv++;
140861 }
140862 }
140863 if( (pTerm->eOperator & pScan->opMask)!=0 ){
140864 /* Verify the affinity and collating sequence match */
140865 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL0x0100)==0 ){
140866 CollSeq *pColl;
140867 Parse *pParse = pWC->pWInfo->pParse;
140868 pX = pTerm->pExpr;
140869 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
140870 continue;
140871 }
140872 assert(pX->pLeft)((void) (0));
140873 pColl = sqlite3BinaryCompareCollSeq(pParse,
140874 pX->pLeft, pX->pRight);
140875 if( pColl==0 ) pColl = pParse->db->pDfltColl;
140876 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
140877 continue;
140878 }
140879 }
140880 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))!=0
140881 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN162
140882 && pX->iTable==pScan->aiCur[0]
140883 && pX->iColumn==pScan->aiColumn[0]
140884 ){
140885 testcase( pTerm->eOperator & WO_IS );
140886 continue;
140887 }
140888 pScan->pWC = pWC;
140889 pScan->k = k+1;
140890 return pTerm;
140891 }
140892 }
140893 }
140894 pWC = pWC->pOuter;
140895 k = 0;
140896 }while( pWC!=0 );
140897 if( pScan->iEquiv>=pScan->nEquiv ) break;
140898 pWC = pScan->pOrigWC;
140899 k = 0;
140900 pScan->iEquiv++;
140901 }
140902 return 0;
140903}
140904
140905/*
140906** This is whereScanInit() for the case of an index on an expression.
140907** It is factored out into a separate tail-recursion subroutine so that
140908** the normal whereScanInit() routine, which is a high-runner, does not
140909** need to push registers onto the stack as part of its prologue.
140910*/
140911static SQLITE_NOINLINE__attribute__((noinline)) WhereTerm *whereScanInitIndexExpr(WhereScan *pScan){
140912 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
140913 return whereScanNext(pScan);
140914}
140915
140916/*
140917** Initialize a WHERE clause scanner object. Return a pointer to the
140918** first match. Return NULL if there are no matches.
140919**
140920** The scanner will be searching the WHERE clause pWC. It will look
140921** for terms of the form "X <op> <expr>" where X is column iColumn of table
140922** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx
140923** must be one of the indexes of table iCur.
140924**
140925** The <op> must be one of the operators described by opMask.
140926**
140927** If the search is for X and the WHERE clause contains terms of the
140928** form X=Y then this routine might also return terms of the form
140929** "Y <op> <expr>". The number of levels of transitivity is limited,
140930** but is enough to handle most commonly occurring SQL statements.
140931**
140932** If X is not the INTEGER PRIMARY KEY then X must be compatible with
140933** index pIdx.
140934*/
140935static WhereTerm *whereScanInit(
140936 WhereScan *pScan, /* The WhereScan object being initialized */
140937 WhereClause *pWC, /* The WHERE clause to be scanned */
140938 int iCur, /* Cursor to scan for */
140939 int iColumn, /* Column to scan for */
140940 u32 opMask, /* Operator(s) to scan for */
140941 Index *pIdx /* Must be compatible with this index */
140942){
140943 pScan->pOrigWC = pWC;
140944 pScan->pWC = pWC;
140945 pScan->pIdxExpr = 0;
140946 pScan->idxaff = 0;
140947 pScan->zCollName = 0;
140948 pScan->opMask = opMask;
140949 pScan->k = 0;
140950 pScan->aiCur[0] = iCur;
140951 pScan->nEquiv = 1;
140952 pScan->iEquiv = 1;
140953 if( pIdx ){
140954 int j = iColumn;
140955 iColumn = pIdx->aiColumn[j];
140956 if( iColumn==XN_EXPR(-2) ){
140957 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
140958 pScan->zCollName = pIdx->azColl[j];
140959 pScan->aiColumn[0] = XN_EXPR(-2);
140960 return whereScanInitIndexExpr(pScan);
140961 }else if( iColumn==pIdx->pTable->iPKey ){
140962 iColumn = XN_ROWID(-1);
140963 }else if( iColumn>=0 ){
140964 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
140965 pScan->zCollName = pIdx->azColl[j];
140966 }
140967 }else if( iColumn==XN_EXPR(-2) ){
140968 return 0;
140969 }
140970 pScan->aiColumn[0] = iColumn;
140971 return whereScanNext(pScan);
140972}
140973
140974/*
140975** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
140976** where X is a reference to the iColumn of table iCur or of index pIdx
140977** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by
140978** the op parameter. Return a pointer to the term. Return 0 if not found.
140979**
140980** If pIdx!=0 then it must be one of the indexes of table iCur.
140981** Search for terms matching the iColumn-th column of pIdx
140982** rather than the iColumn-th column of table iCur.
140983**
140984** The term returned might by Y=<expr> if there is another constraint in
140985** the WHERE clause that specifies that X=Y. Any such constraints will be
140986** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
140987** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11
140988** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10
140989** other equivalent values. Hence a search for X will return <expr> if X=A1
140990** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>.
140991**
140992** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
140993** then try for the one with no dependencies on <expr> - in other words where
140994** <expr> is a constant expression of some kind. Only return entries of
140995** the form "X <op> Y" where Y is a column in another table if no terms of
140996** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
140997** exist, try to return a term that does not use WO_EQUIV.
140998*/
140999SQLITE_PRIVATEstatic WhereTerm *sqlite3WhereFindTerm(
141000 WhereClause *pWC, /* The WHERE clause to be searched */
141001 int iCur, /* Cursor number of LHS */
141002 int iColumn, /* Column number of LHS */
141003 Bitmask notReady, /* RHS must not overlap with this mask */
141004 u32 op, /* Mask of WO_xx values describing operator */
141005 Index *pIdx /* Must be compatible with this index, if not NULL */
141006){
141007 WhereTerm *pResult = 0;
141008 WhereTerm *p;
141009 WhereScan scan;
141010
141011 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
141012 op &= WO_EQ0x0002|WO_IS0x0080;
141013 while( p ){
141014 if( (p->prereqRight & notReady)==0 ){
141015 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
141016 testcase( p->eOperator & WO_IS );
141017 return p;
141018 }
141019 if( pResult==0 ) pResult = p;
141020 }
141021 p = whereScanNext(&scan);
141022 }
141023 return pResult;
141024}
141025
141026/*
141027** This function searches pList for an entry that matches the iCol-th column
141028** of index pIdx.
141029**
141030** If such an expression is found, its index in pList->a[] is returned. If
141031** no expression is found, -1 is returned.
141032*/
141033static int findIndexCol(
141034 Parse *pParse, /* Parse context */
141035 ExprList *pList, /* Expression list to search */
141036 int iBase, /* Cursor for table associated with pIdx */
141037 Index *pIdx, /* Index to match column of */
141038 int iCol /* Column of index to match */
141039){
141040 int i;
141041 const char *zColl = pIdx->azColl[iCol];
141042
141043 for(i=0; i<pList->nExpr; i++){
141044 Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
141045 if( p->op==TK_COLUMN162
141046 && p->iColumn==pIdx->aiColumn[iCol]
141047 && p->iTable==iBase
141048 ){
141049 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
141050 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
141051 return i;
141052 }
141053 }
141054 }
141055
141056 return -1;
141057}
141058
141059/*
141060** Return TRUE if the iCol-th column of index pIdx is NOT NULL
141061*/
141062static int indexColumnNotNull(Index *pIdx, int iCol){
141063 int j;
141064 assert( pIdx!=0 )((void) (0));
141065 assert( iCol>=0 && iCol<pIdx->nColumn )((void) (0));
141066 j = pIdx->aiColumn[iCol];
141067 if( j>=0 ){
141068 return pIdx->pTable->aCol[j].notNull;
141069 }else if( j==(-1) ){
141070 return 1;
141071 }else{
141072 assert( j==(-2) )((void) (0));
141073 return 0; /* Assume an indexed expression can always yield a NULL */
141074
141075 }
141076}
141077
141078/*
141079** Return true if the DISTINCT expression-list passed as the third argument
141080** is redundant.
141081**
141082** A DISTINCT list is redundant if any subset of the columns in the
141083** DISTINCT list are collectively unique and individually non-null.
141084*/
141085static int isDistinctRedundant(
141086 Parse *pParse, /* Parsing context */
141087 SrcList *pTabList, /* The FROM clause */
141088 WhereClause *pWC, /* The WHERE clause */
141089 ExprList *pDistinct /* The result set that needs to be DISTINCT */
141090){
141091 Table *pTab;
141092 Index *pIdx;
141093 int i;
141094 int iBase;
141095
141096 /* If there is more than one table or sub-select in the FROM clause of
141097 ** this query, then it will not be possible to show that the DISTINCT
141098 ** clause is redundant. */
141099 if( pTabList->nSrc!=1 ) return 0;
141100 iBase = pTabList->a[0].iCursor;
141101 pTab = pTabList->a[0].pTab;
141102
141103 /* If any of the expressions is an IPK column on table iBase, then return
141104 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
141105 ** current SELECT is a correlated sub-query.
141106 */
141107 for(i=0; i<pDistinct->nExpr; i++){
141108 Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
141109 if( p->op==TK_COLUMN162 && p->iTable==iBase && p->iColumn<0 ) return 1;
141110 }
141111
141112 /* Loop through all indices on the table, checking each to see if it makes
141113 ** the DISTINCT qualifier redundant. It does so if:
141114 **
141115 ** 1. The index is itself UNIQUE, and
141116 **
141117 ** 2. All of the columns in the index are either part of the pDistinct
141118 ** list, or else the WHERE clause contains a term of the form "col=X",
141119 ** where X is a constant value. The collation sequences of the
141120 ** comparison and select-list expressions must match those of the index.
141121 **
141122 ** 3. All of those index columns for which the WHERE clause does not
141123 ** contain a "col=X" term are subject to a NOT NULL constraint.
141124 */
141125 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
141126 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0) ) continue;
141127 for(i=0; i<pIdx->nKeyCol; i++){
141128 if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ0x0002, pIdx) ){
141129 if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break;
141130 if( indexColumnNotNull(pIdx, i)==0 ) break;
141131 }
141132 }
141133 if( i==pIdx->nKeyCol ){
141134 /* This index implies that the DISTINCT qualifier is redundant. */
141135 return 1;
141136 }
141137 }
141138
141139 return 0;
141140}
141141
141142
141143/*
141144** Estimate the logarithm of the input value to base 2.
141145*/
141146static LogEst estLog(LogEst N){
141147 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
141148}
141149
141150/*
141151** Convert OP_Column opcodes to OP_Copy in previously generated code.
141152**
141153** This routine runs over generated VDBE code and translates OP_Column
141154** opcodes into OP_Copy when the table is being accessed via co-routine
141155** instead of via table lookup.
141156**
141157** If the iAutoidxCur is not zero, then any OP_Rowid instructions on
141158** cursor iTabCur are transformed into OP_Sequence opcode for the
141159** iAutoidxCur cursor, in order to generate unique rowids for the
141160** automatic index being generated.
141161*/
141162static void translateColumnToCopy(
141163 Parse *pParse, /* Parsing context */
141164 int iStart, /* Translate from this opcode to the end */
141165 int iTabCur, /* OP_Column/OP_Rowid references to this table */
141166 int iRegister, /* The first column is in this register */
141167 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
141168){
141169 Vdbe *v = pParse->pVdbe;
141170 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
141171 int iEnd = sqlite3VdbeCurrentAddr(v);
141172 if( pParse->db->mallocFailed ) return;
141173 for(; iStart<iEnd; iStart++, pOp++){
141174 if( pOp->p1!=iTabCur ) continue;
141175 if( pOp->opcode==OP_Column90 ){
141176 pOp->opcode = OP_Copy78;
141177 pOp->p1 = pOp->p2 + iRegister;
141178 pOp->p2 = pOp->p3;
141179 pOp->p3 = 0;
141180 }else if( pOp->opcode==OP_Rowid128 ){
141181 if( iAutoidxCur ){
141182 pOp->opcode = OP_Sequence120;
141183 pOp->p1 = iAutoidxCur;
141184 }else{
141185 pOp->opcode = OP_Null73;
141186 pOp->p1 = 0;
141187 pOp->p3 = 0;
141188 }
141189 }
141190 }
141191}
141192
141193/*
141194** Two routines for printing the content of an sqlite3_index_info
141195** structure. Used for testing and debugging only. If neither
141196** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
141197** are no-ops.
141198*/
141199#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
141200static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
141201 int i;
141202 if( !sqlite3WhereTrace ) return;
141203 for(i=0; i<p->nConstraint; i++){
141204 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
141205 i,
141206 p->aConstraint[i].iColumn,
141207 p->aConstraint[i].iTermOffset,
141208 p->aConstraint[i].op,
141209 p->aConstraint[i].usable);
141210 }
141211 for(i=0; i<p->nOrderBy; i++){
141212 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n",
141213 i,
141214 p->aOrderBy[i].iColumn,
141215 p->aOrderBy[i].desc);
141216 }
141217}
141218static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
141219 int i;
141220 if( !sqlite3WhereTrace ) return;
141221 for(i=0; i<p->nConstraint; i++){
141222 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n",
141223 i,
141224 p->aConstraintUsage[i].argvIndex,
141225 p->aConstraintUsage[i].omit);
141226 }
141227 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
141228 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
141229 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
141230 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
141231 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
141232}
141233#else
141234#define TRACE_IDX_INPUTS(A)
141235#define TRACE_IDX_OUTPUTS(A)
141236#endif
141237
141238#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
141239/*
141240** Return TRUE if the WHERE clause term pTerm is of a form where it
141241** could be used with an index to access pSrc, assuming an appropriate
141242** index existed.
141243*/
141244static int termCanDriveIndex(
141245 WhereTerm *pTerm, /* WHERE clause term to check */
141246 struct SrcList_item *pSrc, /* Table we are trying to access */
141247 Bitmask notReady /* Tables in outer loops of the join */
141248){
141249 char aff;
141250 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
141251 if( (pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080))==0 ) return 0;
141252 if( (pSrc->fg.jointype & JT_LEFT0x0008)
141253 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)(((pTerm->pExpr)->flags&(0x000001))!=0)
141254 && (pTerm->eOperator & WO_IS0x0080)
141255 ){
141256 /* Cannot use an IS term from the WHERE clause as an index driver for
141257 ** the RHS of a LEFT JOIN. Such a term can only be used if it is from
141258 ** the ON clause. */
141259 return 0;
141260 }
141261 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
141262 if( pTerm->u.leftColumn<0 ) return 0;
141263 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
141264 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
141265 testcase( pTerm->pExpr->op==TK_IS );
141266 return 1;
141267}
141268#endif
141269
141270
141271#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
141272/*
141273** Generate code to construct the Index object for an automatic index
141274** and to set up the WhereLevel object pLevel so that the code generator
141275** makes use of the automatic index.
141276*/
141277static void constructAutomaticIndex(
141278 Parse *pParse, /* The parsing context */
141279 WhereClause *pWC, /* The WHERE clause */
141280 struct SrcList_item *pSrc, /* The FROM clause term to get the next index */
141281 Bitmask notReady, /* Mask of cursors that are not available */
141282 WhereLevel *pLevel /* Write new index here */
141283){
141284 int nKeyCol; /* Number of columns in the constructed index */
141285 WhereTerm *pTerm; /* A single term of the WHERE clause */
141286 WhereTerm *pWCEnd; /* End of pWC->a[] */
141287 Index *pIdx; /* Object describing the transient index */
141288 Vdbe *v; /* Prepared statement under construction */
141289 int addrInit; /* Address of the initialization bypass jump */
141290 Table *pTable; /* The table being indexed */
141291 int addrTop; /* Top of the index fill loop */
141292 int regRecord; /* Register holding an index record */
141293 int n; /* Column counter */
141294 int i; /* Loop counter */
141295 int mxBitCol; /* Maximum column in pSrc->colUsed */
141296 CollSeq *pColl; /* Collating sequence to on a column */
141297 WhereLoop *pLoop; /* The Loop object */
141298 char *zNotUsed; /* Extra space on the end of pIdx */
141299 Bitmask idxCols; /* Bitmap of columns used for indexing */
141300 Bitmask extraCols; /* Bitmap of additional columns */
141301 u8 sentWarning = 0; /* True if a warnning has been issued */
141302 Expr *pPartial = 0; /* Partial Index Expression */
141303 int iContinue = 0; /* Jump here to skip excluded rows */
141304 struct SrcList_item *pTabItem; /* FROM clause term being indexed */
141305 int addrCounter = 0; /* Address where integer counter is initialized */
141306 int regBase; /* Array of registers where record is assembled */
141307
141308 /* Generate code to skip over the creation and initialization of the
141309 ** transient index on 2nd and subsequent iterations of the loop. */
141310 v = pParse->pVdbe;
141311 assert( v!=0 )((void) (0));
141312 addrInit = sqlite3VdbeAddOp0(v, OP_Once17); VdbeCoverage(v);
141313
141314 /* Count the number of columns that will be added to the index
141315 ** and used to match WHERE clause constraints */
141316 nKeyCol = 0;
141317 pTable = pSrc->pTab;
141318 pWCEnd = &pWC->a[pWC->nTerm];
141319 pLoop = pLevel->pWLoop;
141320 idxCols = 0;
141321 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
141322 Expr *pExpr = pTerm->pExpr;
141323 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */((void) (0))
141324 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */((void) (0))
141325 || pLoop->prereq!=0 )((void) (0)); /* table of a LEFT JOIN */
141326 if( pLoop->prereq==0
141327 && (pTerm->wtFlags & TERM_VIRTUAL0x02)==0
141328 && !ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0)
141329 && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
141330 pPartial = sqlite3ExprAnd(pParse, pPartial,
141331 sqlite3ExprDup(pParse->db, pExpr, 0));
141332 }
141333 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
141334 int iCol = pTerm->u.leftColumn;
141335 Bitmask cMask = iCol>=BMS((int)(sizeof(Bitmask)*8)) ? MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) : MASKBIT(iCol)(((Bitmask)1)<<(iCol));
141336 testcase( iCol==BMS );
141337 testcase( iCol==BMS-1 );
141338 if( !sentWarning ){
141339 sqlite3_log(SQLITE_WARNING_AUTOINDEX(28 | (1<<8)),
141340 "automatic index on %s(%s)", pTable->zName,
141341 pTable->aCol[iCol].zName);
141342 sentWarning = 1;
141343 }
141344 if( (idxCols & cMask)==0 ){
141345 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
141346 goto end_auto_index_create;
141347 }
141348 pLoop->aLTerm[nKeyCol++] = pTerm;
141349 idxCols |= cMask;
141350 }
141351 }
141352 }
141353 assert( nKeyCol>0 )((void) (0));
141354 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
141355 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001 | WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200
141356 | WHERE_AUTO_INDEX0x00004000;
141357
141358 /* Count the number of additional columns needed to create a
141359 ** covering index. A "covering index" is an index that contains all
141360 ** columns that are needed by the query. With a covering index, the
141361 ** original table never needs to be accessed. Automatic indices must
141362 ** be a covering index because the index will not be updated if the
141363 ** original table changes and the index and table cannot both be used
141364 ** if they go out of sync.
141365 */
141366 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)));
141367 mxBitCol = MIN(BMS-1,pTable->nCol)((((int)(sizeof(Bitmask)*8))-1)<(pTable->nCol)?(((int)(
sizeof(Bitmask)*8))-1):(pTable->nCol))
;
141368 testcase( pTable->nCol==BMS-1 );
141369 testcase( pTable->nCol==BMS-2 );
141370 for(i=0; i<mxBitCol; i++){
141371 if( extraCols & MASKBIT(i)(((Bitmask)1)<<(i)) ) nKeyCol++;
141372 }
141373 if( pSrc->colUsed & MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) ){
141374 nKeyCol += pTable->nCol - BMS((int)(sizeof(Bitmask)*8)) + 1;
141375 }
141376
141377 /* Construct the Index object to describe this index */
141378 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
141379 if( pIdx==0 ) goto end_auto_index_create;
141380 pLoop->u.btree.pIndex = pIdx;
141381 pIdx->zName = "auto-index";
141382 pIdx->pTable = pTable;
141383 n = 0;
141384 idxCols = 0;
141385 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
141386 if( termCanDriveIndex(pTerm, pSrc, notReady) ){
141387 int iCol = pTerm->u.leftColumn;
141388 Bitmask cMask = iCol>=BMS((int)(sizeof(Bitmask)*8)) ? MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) : MASKBIT(iCol)(((Bitmask)1)<<(iCol));
141389 testcase( iCol==BMS-1 );
141390 testcase( iCol==BMS );
141391 if( (idxCols & cMask)==0 ){
141392 Expr *pX = pTerm->pExpr;
141393 idxCols |= cMask;
141394 pIdx->aiColumn[n] = pTerm->u.leftColumn;
141395 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
141396 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
141397 n++;
141398 }
141399 }
141400 }
141401 assert( (u32)n==pLoop->u.btree.nEq )((void) (0));
141402
141403 /* Add additional columns needed to make the automatic index into
141404 ** a covering index */
141405 for(i=0; i<mxBitCol; i++){
141406 if( extraCols & MASKBIT(i)(((Bitmask)1)<<(i)) ){
141407 pIdx->aiColumn[n] = i;
141408 pIdx->azColl[n] = sqlite3StrBINARY;
141409 n++;
141410 }
141411 }
141412 if( pSrc->colUsed & MASKBIT(BMS-1)(((Bitmask)1)<<(((int)(sizeof(Bitmask)*8))-1)) ){
141413 for(i=BMS((int)(sizeof(Bitmask)*8))-1; i<pTable->nCol; i++){
141414 pIdx->aiColumn[n] = i;
141415 pIdx->azColl[n] = sqlite3StrBINARY;
141416 n++;
141417 }
141418 }
141419 assert( n==nKeyCol )((void) (0));
141420 pIdx->aiColumn[n] = XN_ROWID(-1);
141421 pIdx->azColl[n] = sqlite3StrBINARY;
141422
141423 /* Create the automatic index */
141424 assert( pLevel->iIdxCur>=0 )((void) (0));
141425 pLevel->iIdxCur = pParse->nTab++;
141426 sqlite3VdbeAddOp2(v, OP_OpenAutoindex112, pLevel->iIdxCur, nKeyCol+1);
141427 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
141428 VdbeComment((v, "for %s", pTable->zName));
141429
141430 /* Fill the automatic index with content */
141431 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
141432 if( pTabItem->fg.viaCoroutine ){
141433 int regYield = pTabItem->regReturn;
141434 addrCounter = sqlite3VdbeAddOp2(v, OP_Integer70, 0, 0);
141435 sqlite3VdbeAddOp3(v, OP_InitCoroutine13, regYield, 0, pTabItem->addrFillSub);
141436 addrTop = sqlite3VdbeAddOp1(v, OP_Yield14, regYield);
141437 VdbeCoverage(v);
141438 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
141439 }else{
141440 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind36, pLevel->iTabCur); VdbeCoverage(v);
141441 }
141442 if( pPartial ){
141443 iContinue = sqlite3VdbeMakeLabel(pParse);
141444 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL0x10);
141445 pLoop->wsFlags |= WHERE_PARTIALIDX0x00020000;
141446 }
141447 regRecord = sqlite3GetTempReg(pParse);
141448 regBase = sqlite3GenerateIndexKey(
141449 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
141450 );
141451 sqlite3VdbeAddOp2(v, OP_IdxInsert132, pLevel->iIdxCur, regRecord);
141452 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT0x10);
141453 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue);
141454 if( pTabItem->fg.viaCoroutine ){
141455 sqlite3VdbeChangeP2(v, addrCounter, regBase+n);
141456 testcase( pParse->db->mallocFailed );
141457 assert( pLevel->iIdxCur>0 )((void) (0));
141458 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
141459 pTabItem->regResult, pLevel->iIdxCur);
141460 sqlite3VdbeGoto(v, addrTop);
141461 pTabItem->fg.viaCoroutine = 0;
141462 }else{
141463 sqlite3VdbeAddOp2(v, OP_Next5, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
141464 }
141465 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX3);
141466 sqlite3VdbeJumpHere(v, addrTop);
141467 sqlite3ReleaseTempReg(pParse, regRecord);
141468
141469 /* Jump here when skipping the initialization */
141470 sqlite3VdbeJumpHere(v, addrInit);
141471
141472end_auto_index_create:
141473 sqlite3ExprDelete(pParse->db, pPartial);
141474}
141475#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
141476
141477#ifndef SQLITE_OMIT_VIRTUALTABLE
141478/*
141479** Allocate and populate an sqlite3_index_info structure. It is the
141480** responsibility of the caller to eventually release the structure
141481** by passing the pointer returned by this function to sqlite3_free().
141482*/
141483static sqlite3_index_info *allocateIndexInfo(
141484 Parse *pParse, /* The parsing context */
141485 WhereClause *pWC, /* The WHERE clause being analyzed */
141486 Bitmask mUnusable, /* Ignore terms with these prereqs */
141487 struct SrcList_item *pSrc, /* The FROM clause term that is the vtab */
141488 ExprList *pOrderBy, /* The ORDER BY clause */
141489 u16 *pmNoOmit /* Mask of terms not to omit */
141490){
141491 int i, j;
141492 int nTerm;
141493 struct sqlite3_index_constraint *pIdxCons;
141494 struct sqlite3_index_orderby *pIdxOrderBy;
141495 struct sqlite3_index_constraint_usage *pUsage;
141496 struct HiddenIndexInfo *pHidden;
141497 WhereTerm *pTerm;
141498 int nOrderBy;
141499 sqlite3_index_info *pIdxInfo;
141500 u16 mNoOmit = 0;
141501
141502 /* Count the number of possible WHERE clause constraints referring
141503 ** to this virtual table */
141504 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
141505 if( pTerm->leftCursor != pSrc->iCursor ) continue;
141506 if( pTerm->prereqRight & mUnusable ) continue;
141507 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) )((void) (0));
141508 testcase( pTerm->eOperator & WO_IN );
141509 testcase( pTerm->eOperator & WO_ISNULL );
141510 testcase( pTerm->eOperator & WO_IS );
141511 testcase( pTerm->eOperator & WO_ALL );
141512 if( (pTerm->eOperator & ~(WO_EQUIV0x0800))==0 ) continue;
141513 if( pTerm->wtFlags & TERM_VNULL0x00 ) continue;
141514 assert( pTerm->u.leftColumn>=(-1) )((void) (0));
141515 nTerm++;
141516 }
141517
141518 /* If the ORDER BY clause contains only columns in the current
141519 ** virtual table then allocate space for the aOrderBy part of
141520 ** the sqlite3_index_info structure.
141521 */
141522 nOrderBy = 0;
141523 if( pOrderBy ){
141524 int n = pOrderBy->nExpr;
141525 for(i=0; i<n; i++){
141526 Expr *pExpr = pOrderBy->a[i].pExpr;
141527 if( pExpr->op!=TK_COLUMN162 || pExpr->iTable!=pSrc->iCursor ) break;
141528 }
141529 if( i==n){
141530 nOrderBy = n;
141531 }
141532 }
141533
141534 /* Allocate the sqlite3_index_info structure
141535 */
141536 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
141537 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
141538 + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) );
141539 if( pIdxInfo==0 ){
141540 sqlite3ErrorMsg(pParse, "out of memory");
141541 return 0;
141542 }
141543
141544 /* Initialize the structure. The sqlite3_index_info structure contains
141545 ** many fields that are declared "const" to prevent xBestIndex from
141546 ** changing them. We have to do some funky casting in order to
141547 ** initialize those fields.
141548 */
141549 pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1];
141550 pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1];
141551 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
141552 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
141553 *(int*)&pIdxInfo->nConstraint = nTerm;
141554 *(int*)&pIdxInfo->nOrderBy = nOrderBy;
141555 *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
141556 *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
141557 *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
141558 pUsage;
141559
141560 pHidden->pWC = pWC;
141561 pHidden->pParse = pParse;
141562 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
141563 u16 op;
141564 if( pTerm->leftCursor != pSrc->iCursor ) continue;
141565 if( pTerm->prereqRight & mUnusable ) continue;
141566 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) )((void) (0));
141567 testcase( pTerm->eOperator & WO_IN );
141568 testcase( pTerm->eOperator & WO_IS );
141569 testcase( pTerm->eOperator & WO_ISNULL );
141570 testcase( pTerm->eOperator & WO_ALL );
141571 if( (pTerm->eOperator & ~(WO_EQUIV0x0800))==0 ) continue;
141572 if( pTerm->wtFlags & TERM_VNULL0x00 ) continue;
141573 if( (pSrc->fg.jointype & JT_LEFT0x0008)!=0
141574 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)(((pTerm->pExpr)->flags&(0x000001))!=0)
141575 && (pTerm->eOperator & (WO_IS0x0080|WO_ISNULL0x0100))
141576 ){
141577 /* An "IS" term in the WHERE clause where the virtual table is the rhs
141578 ** of a LEFT JOIN. Do not pass this term to the virtual table
141579 ** implementation, as this can lead to incorrect results from SQL such
141580 ** as:
141581 **
141582 ** "LEFT JOIN vtab WHERE vtab.col IS NULL" */
141583 testcase( pTerm->eOperator & WO_ISNULL );
141584 testcase( pTerm->eOperator & WO_IS );
141585 continue;
141586 }
141587 assert( pTerm->u.leftColumn>=(-1) )((void) (0));
141588 pIdxCons[j].iColumn = pTerm->u.leftColumn;
141589 pIdxCons[j].iTermOffset = i;
141590 op = pTerm->eOperator & WO_ALL0x1fff;
141591 if( op==WO_IN0x0001 ) op = WO_EQ0x0002;
141592 if( op==WO_AUX0x0040 ){
141593 pIdxCons[j].op = pTerm->eMatchOp;
141594 }else if( op & (WO_ISNULL0x0100|WO_IS0x0080) ){
141595 if( op==WO_ISNULL0x0100 ){
141596 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL71;
141597 }else{
141598 pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS72;
141599 }
141600 }else{
141601 pIdxCons[j].op = (u8)op;
141602 /* The direct assignment in the previous line is possible only because
141603 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
141604 ** following asserts verify this fact. */
141605 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ )((void) (0));
141606 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT )((void) (0));
141607 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE )((void) (0));
141608 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT )((void) (0));
141609 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE )((void) (0));
141610 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) )((void) (0));
141611
141612 if( op & (WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53)))
141613 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
141614 ){
141615 if( i<16 ) mNoOmit |= (1 << i);
141616 if( op==WO_LT(0x0002<<(56 -53)) ) pIdxCons[j].op = WO_LE(0x0002<<(55 -53));
141617 if( op==WO_GT(0x0002<<(54 -53)) ) pIdxCons[j].op = WO_GE(0x0002<<(57 -53));
141618 }
141619 }
141620
141621 j++;
141622 }
141623 for(i=0; i<nOrderBy; i++){
141624 Expr *pExpr = pOrderBy->a[i].pExpr;
141625 pIdxOrderBy[i].iColumn = pExpr->iColumn;
141626 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
141627 }
141628
141629 *pmNoOmit = mNoOmit;
141630 return pIdxInfo;
141631}
141632
141633/*
141634** The table object reference passed as the second argument to this function
141635** must represent a virtual table. This function invokes the xBestIndex()
141636** method of the virtual table with the sqlite3_index_info object that
141637** comes in as the 3rd argument to this function.
141638**
141639** If an error occurs, pParse is populated with an error message and an
141640** appropriate error code is returned. A return of SQLITE_CONSTRAINT from
141641** xBestIndex is not considered an error. SQLITE_CONSTRAINT indicates that
141642** the current configuration of "unusable" flags in sqlite3_index_info can
141643** not result in a valid plan.
141644**
141645** Whether or not an error is returned, it is the responsibility of the
141646** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
141647** that this is required.
141648*/
141649static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
141650 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
141651 int rc;
141652
141653 TRACE_IDX_INPUTS(p);
141654 rc = pVtab->pModule->xBestIndex(pVtab, p);
141655 TRACE_IDX_OUTPUTS(p);
141656
141657 if( rc!=SQLITE_OK0 && rc!=SQLITE_CONSTRAINT19 ){
141658 if( rc==SQLITE_NOMEM7 ){
141659 sqlite3OomFault(pParse->db);
141660 }else if( !pVtab->zErrMsg ){
141661 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
141662 }else{
141663 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
141664 }
141665 }
141666 sqlite3_free(pVtab->zErrMsg);
141667 pVtab->zErrMsg = 0;
141668 return rc;
141669}
141670#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
141671
141672#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
141673/*
141674** Estimate the location of a particular key among all keys in an
141675** index. Store the results in aStat as follows:
141676**
141677** aStat[0] Est. number of rows less than pRec
141678** aStat[1] Est. number of rows equal to pRec
141679**
141680** Return the index of the sample that is the smallest sample that
141681** is greater than or equal to pRec. Note that this index is not an index
141682** into the aSample[] array - it is an index into a virtual set of samples
141683** based on the contents of aSample[] and the number of fields in record
141684** pRec.
141685*/
141686static int whereKeyStats(
141687 Parse *pParse, /* Database connection */
141688 Index *pIdx, /* Index to consider domain of */
141689 UnpackedRecord *pRec, /* Vector of values to consider */
141690 int roundUp, /* Round up if true. Round down if false */
141691 tRowcnt *aStat /* OUT: stats written here */
141692){
141693 IndexSample *aSample = pIdx->aSample;
141694 int iCol; /* Index of required stats in anEq[] etc. */
141695 int i; /* Index of first sample >= pRec */
141696 int iSample; /* Smallest sample larger than or equal to pRec */
141697 int iMin = 0; /* Smallest sample not yet tested */
141698 int iTest; /* Next sample to test */
141699 int res; /* Result of comparison operation */
141700 int nField; /* Number of fields in pRec */
141701 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */
141702
141703#ifndef SQLITE_DEBUG
141704 UNUSED_PARAMETER( pParse )(void)(pParse);
141705#endif
141706 assert( pRec!=0 )((void) (0));
141707 assert( pIdx->nSample>0 )((void) (0));
141708 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol )((void) (0));
141709
141710 /* Do a binary search to find the first sample greater than or equal
141711 ** to pRec. If pRec contains a single field, the set of samples to search
141712 ** is simply the aSample[] array. If the samples in aSample[] contain more
141713 ** than one fields, all fields following the first are ignored.
141714 **
141715 ** If pRec contains N fields, where N is more than one, then as well as the
141716 ** samples in aSample[] (truncated to N fields), the search also has to
141717 ** consider prefixes of those samples. For example, if the set of samples
141718 ** in aSample is:
141719 **
141720 ** aSample[0] = (a, 5)
141721 ** aSample[1] = (a, 10)
141722 ** aSample[2] = (b, 5)
141723 ** aSample[3] = (c, 100)
141724 ** aSample[4] = (c, 105)
141725 **
141726 ** Then the search space should ideally be the samples above and the
141727 ** unique prefixes [a], [b] and [c]. But since that is hard to organize,
141728 ** the code actually searches this set:
141729 **
141730 ** 0: (a)
141731 ** 1: (a, 5)
141732 ** 2: (a, 10)
141733 ** 3: (a, 10)
141734 ** 4: (b)
141735 ** 5: (b, 5)
141736 ** 6: (c)
141737 ** 7: (c, 100)
141738 ** 8: (c, 105)
141739 ** 9: (c, 105)
141740 **
141741 ** For each sample in the aSample[] array, N samples are present in the
141742 ** effective sample array. In the above, samples 0 and 1 are based on
141743 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc.
141744 **
141745 ** Often, sample i of each block of N effective samples has (i+1) fields.
141746 ** Except, each sample may be extended to ensure that it is greater than or
141747 ** equal to the previous sample in the array. For example, in the above,
141748 ** sample 2 is the first sample of a block of N samples, so at first it
141749 ** appears that it should be 1 field in size. However, that would make it
141750 ** smaller than sample 1, so the binary search would not work. As a result,
141751 ** it is extended to two fields. The duplicates that this creates do not
141752 ** cause any problems.
141753 */
141754 nField = pRec->nField;
141755 iCol = 0;
141756 iSample = pIdx->nSample * nField;
141757 do{
141758 int iSamp; /* Index in aSample[] of test sample */
141759 int n; /* Number of fields in test sample */
141760
141761 iTest = (iMin+iSample)/2;
141762 iSamp = iTest / nField;
141763 if( iSamp>0 ){
141764 /* The proposed effective sample is a prefix of sample aSample[iSamp].
141765 ** Specifically, the shortest prefix of at least (1 + iTest%nField)
141766 ** fields that is greater than the previous effective sample. */
141767 for(n=(iTest % nField) + 1; n<nField; n++){
141768 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
141769 }
141770 }else{
141771 n = iTest + 1;
141772 }
141773
141774 pRec->nField = n;
141775 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec);
141776 if( res<0 ){
141777 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
141778 iMin = iTest+1;
141779 }else if( res==0 && n<nField ){
141780 iLower = aSample[iSamp].anLt[n-1];
141781 iMin = iTest+1;
141782 res = -1;
141783 }else{
141784 iSample = iTest;
141785 iCol = n-1;
141786 }
141787 }while( res && iMin<iSample );
141788 i = iSample / nField;
141789
141790#ifdef SQLITE_DEBUG
141791 /* The following assert statements check that the binary search code
141792 ** above found the right answer. This block serves no purpose other
141793 ** than to invoke the asserts. */
141794 if( pParse->db->mallocFailed==0 ){
141795 if( res==0 ){
141796 /* If (res==0) is true, then pRec must be equal to sample i. */
141797 assert( i<pIdx->nSample )((void) (0));
141798 assert( iCol==nField-1 )((void) (0));
141799 pRec->nField = nField;
141800 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)((void) (0))
141801 || pParse->db->mallocFailed((void) (0))
141802 )((void) (0));
141803 }else{
141804 /* Unless i==pIdx->nSample, indicating that pRec is larger than
141805 ** all samples in the aSample[] array, pRec must be smaller than the
141806 ** (iCol+1) field prefix of sample i. */
141807 assert( i<=pIdx->nSample && i>=0 )((void) (0));
141808 pRec->nField = iCol+1;
141809 assert( i==pIdx->nSample((void) (0))
141810 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0((void) (0))
141811 || pParse->db->mallocFailed )((void) (0));
141812
141813 /* if i==0 and iCol==0, then record pRec is smaller than all samples
141814 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must
141815 ** be greater than or equal to the (iCol) field prefix of sample i.
141816 ** If (i>0), then pRec must also be greater than sample (i-1). */
141817 if( iCol>0 ){
141818 pRec->nField = iCol;
141819 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0((void) (0))
141820 || pParse->db->mallocFailed )((void) (0));
141821 }
141822 if( i>0 ){
141823 pRec->nField = nField;
141824 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0((void) (0))
141825 || pParse->db->mallocFailed )((void) (0));
141826 }
141827 }
141828 }
141829#endif /* ifdef SQLITE_DEBUG */
141830
141831 if( res==0 ){
141832 /* Record pRec is equal to sample i */
141833 assert( iCol==nField-1 )((void) (0));
141834 aStat[0] = aSample[i].anLt[iCol];
141835 aStat[1] = aSample[i].anEq[iCol];
141836 }else{
141837 /* At this point, the (iCol+1) field prefix of aSample[i] is the first
141838 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
141839 ** is larger than all samples in the array. */
141840 tRowcnt iUpper, iGap;
141841 if( i>=pIdx->nSample ){
141842 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
141843 }else{
141844 iUpper = aSample[i].anLt[iCol];
141845 }
141846
141847 if( iLower>=iUpper ){
141848 iGap = 0;
141849 }else{
141850 iGap = iUpper - iLower;
141851 }
141852 if( roundUp ){
141853 iGap = (iGap*2)/3;
141854 }else{
141855 iGap = iGap/3;
141856 }
141857 aStat[0] = iLower + iGap;
141858 aStat[1] = pIdx->aAvgEq[nField-1];
141859 }
141860
141861 /* Restore the pRec->nField value before returning. */
141862 pRec->nField = nField;
141863 return i;
141864}
141865#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
141866
141867/*
141868** If it is not NULL, pTerm is a term that provides an upper or lower
141869** bound on a range scan. Without considering pTerm, it is estimated
141870** that the scan will visit nNew rows. This function returns the number
141871** estimated to be visited after taking pTerm into account.
141872**
141873** If the user explicitly specified a likelihood() value for this term,
141874** then the return value is the likelihood multiplied by the number of
141875** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
141876** has a likelihood of 0.50, and any other term a likelihood of 0.25.
141877*/
141878static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
141879 LogEst nRet = nNew;
141880 if( pTerm ){
141881 if( pTerm->truthProb<=0 ){
141882 nRet += pTerm->truthProb;
141883 }else if( (pTerm->wtFlags & TERM_VNULL0x00)==0 ){
141884 nRet -= 20; assert( 20==sqlite3LogEst(4) )((void) (0));
141885 }
141886 }
141887 return nRet;
141888}
141889
141890
141891#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
141892/*
141893** Return the affinity for a single column of an index.
141894*/
141895SQLITE_PRIVATEstatic char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
141896 assert( iCol>=0 && iCol<pIdx->nColumn )((void) (0));
141897 if( !pIdx->zColAff ){
141898 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB'A';
141899 }
141900 return pIdx->zColAff[iCol];
141901}
141902#endif
141903
141904
141905#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
141906/*
141907** This function is called to estimate the number of rows visited by a
141908** range-scan on a skip-scan index. For example:
141909**
141910** CREATE INDEX i1 ON t1(a, b, c);
141911** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
141912**
141913** Value pLoop->nOut is currently set to the estimated number of rows
141914** visited for scanning (a=? AND b=?). This function reduces that estimate
141915** by some factor to account for the (c BETWEEN ? AND ?) expression based
141916** on the stat4 data for the index. this scan will be peformed multiple
141917** times (once for each (a,b) combination that matches a=?) is dealt with
141918** by the caller.
141919**
141920** It does this by scanning through all stat4 samples, comparing values
141921** extracted from pLower and pUpper with the corresponding column in each
141922** sample. If L and U are the number of samples found to be less than or
141923** equal to the values extracted from pLower and pUpper respectively, and
141924** N is the total number of samples, the pLoop->nOut value is adjusted
141925** as follows:
141926**
141927** nOut = nOut * ( min(U - L, 1) / N )
141928**
141929** If pLower is NULL, or a value cannot be extracted from the term, L is
141930** set to zero. If pUpper is NULL, or a value cannot be extracted from it,
141931** U is set to N.
141932**
141933** Normally, this function sets *pbDone to 1 before returning. However,
141934** if no value can be extracted from either pLower or pUpper (and so the
141935** estimate of the number of rows delivered remains unchanged), *pbDone
141936** is left as is.
141937**
141938** If an error occurs, an SQLite error code is returned. Otherwise,
141939** SQLITE_OK.
141940*/
141941static int whereRangeSkipScanEst(
141942 Parse *pParse, /* Parsing & code generating context */
141943 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
141944 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
141945 WhereLoop *pLoop, /* Update the .nOut value of this loop */
141946 int *pbDone /* Set to true if at least one expr. value extracted */
141947){
141948 Index *p = pLoop->u.btree.pIndex;
141949 int nEq = pLoop->u.btree.nEq;
141950 sqlite3 *db = pParse->db;
141951 int nLower = -1;
141952 int nUpper = p->nSample+1;
141953 int rc = SQLITE_OK0;
141954 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq);
141955 CollSeq *pColl;
141956
141957 sqlite3_value *p1 = 0; /* Value extracted from pLower */
141958 sqlite3_value *p2 = 0; /* Value extracted from pUpper */
141959 sqlite3_value *pVal = 0; /* Value extracted from record */
141960
141961 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
141962 if( pLower ){
141963 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
141964 nLower = 0;
141965 }
141966 if( pUpper && rc==SQLITE_OK0 ){
141967 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
141968 nUpper = p2 ? 0 : p->nSample;
141969 }
141970
141971 if( p1 || p2 ){
141972 int i;
141973 int nDiff;
141974 for(i=0; rc==SQLITE_OK0 && i<p->nSample; i++){
141975 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
141976 if( rc==SQLITE_OK0 && p1 ){
141977 int res = sqlite3MemCompare(p1, pVal, pColl);
141978 if( res>=0 ) nLower++;
141979 }
141980 if( rc==SQLITE_OK0 && p2 ){
141981 int res = sqlite3MemCompare(p2, pVal, pColl);
141982 if( res>=0 ) nUpper++;
141983 }
141984 }
141985 nDiff = (nUpper - nLower);
141986 if( nDiff<=0 ) nDiff = 1;
141987
141988 /* If there is both an upper and lower bound specified, and the
141989 ** comparisons indicate that they are close together, use the fallback
141990 ** method (assume that the scan visits 1/64 of the rows) for estimating
141991 ** the number of rows visited. Otherwise, estimate the number of rows
141992 ** using the method described in the header comment for this function. */
141993 if( nDiff!=1 || pUpper==0 || pLower==0 ){
141994 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
141995 pLoop->nOut -= nAdjust;
141996 *pbDone = 1;
141997 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
141998 nLower, nUpper, nAdjust*-1, pLoop->nOut));
141999 }
142000
142001 }else{
142002 assert( *pbDone==0 )((void) (0));
142003 }
142004
142005 sqlite3ValueFree(p1);
142006 sqlite3ValueFree(p2);
142007 sqlite3ValueFree(pVal);
142008
142009 return rc;
142010}
142011#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
142012
142013/*
142014** This function is used to estimate the number of rows that will be visited
142015** by scanning an index for a range of values. The range may have an upper
142016** bound, a lower bound, or both. The WHERE clause terms that set the upper
142017** and lower bounds are represented by pLower and pUpper respectively. For
142018** example, assuming that index p is on t1(a):
142019**
142020** ... FROM t1 WHERE a > ? AND a < ? ...
142021** |_____| |_____|
142022** | |
142023** pLower pUpper
142024**
142025** If either of the upper or lower bound is not present, then NULL is passed in
142026** place of the corresponding WhereTerm.
142027**
142028** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
142029** column subject to the range constraint. Or, equivalently, the number of
142030** equality constraints optimized by the proposed index scan. For example,
142031** assuming index p is on t1(a, b), and the SQL query is:
142032**
142033** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
142034**
142035** then nEq is set to 1 (as the range restricted column, b, is the second
142036** left-most column of the index). Or, if the query is:
142037**
142038** ... FROM t1 WHERE a > ? AND a < ? ...
142039**
142040** then nEq is set to 0.
142041**
142042** When this function is called, *pnOut is set to the sqlite3LogEst() of the
142043** number of rows that the index scan is expected to visit without
142044** considering the range constraints. If nEq is 0, then *pnOut is the number of
142045** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
142046** to account for the range constraints pLower and pUpper.
142047**
142048** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
142049** used, a single range inequality reduces the search space by a factor of 4.
142050** and a pair of constraints (x>? AND x<?) reduces the expected number of
142051** rows visited by a factor of 64.
142052*/
142053static int whereRangeScanEst(
142054 Parse *pParse, /* Parsing & code generating context */
142055 WhereLoopBuilder *pBuilder,
142056 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
142057 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
142058 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
142059){
142060 int rc = SQLITE_OK0;
142061 int nOut = pLoop->nOut;
142062 LogEst nNew;
142063
142064#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
142065 Index *p = pLoop->u.btree.pIndex;
142066 int nEq = pLoop->u.btree.nEq;
142067
142068 if( p->nSample>0 && nEq<p->nSampleCol
142069 && OptimizationEnabled(pParse->db, SQLITE_Stat34)(((pParse->db)->dbOptFlags&(0x0800))==0)
142070 ){
142071 if( nEq==pBuilder->nRecValid ){
142072 UnpackedRecord *pRec = pBuilder->pRec;
142073 tRowcnt a[2];
142074 int nBtm = pLoop->u.btree.nBtm;
142075 int nTop = pLoop->u.btree.nTop;
142076
142077 /* Variable iLower will be set to the estimate of the number of rows in
142078 ** the index that are less than the lower bound of the range query. The
142079 ** lower bound being the concatenation of $P and $L, where $P is the
142080 ** key-prefix formed by the nEq values matched against the nEq left-most
142081 ** columns of the index, and $L is the value in pLower.
142082 **
142083 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
142084 ** is not a simple variable or literal value), the lower bound of the
142085 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
142086 ** if $L is available, whereKeyStats() is called for both ($P) and
142087 ** ($P:$L) and the larger of the two returned values is used.
142088 **
142089 ** Similarly, iUpper is to be set to the estimate of the number of rows
142090 ** less than the upper bound of the range query. Where the upper bound
142091 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
142092 ** of iUpper are requested of whereKeyStats() and the smaller used.
142093 **
142094 ** The number of rows between the two bounds is then just iUpper-iLower.
142095 */
142096 tRowcnt iLower; /* Rows less than the lower bound */
142097 tRowcnt iUpper; /* Rows less than the upper bound */
142098 int iLwrIdx = -2; /* aSample[] for the lower bound */
142099 int iUprIdx = -1; /* aSample[] for the upper bound */
142100
142101 if( pRec ){
142102 testcase( pRec->nField!=pBuilder->nRecValid );
142103 pRec->nField = pBuilder->nRecValid;
142104 }
142105 /* Determine iLower and iUpper using ($P) only. */
142106 if( nEq==0 ){
142107 iLower = 0;
142108 iUpper = p->nRowEst0;
142109 }else{
142110 /* Note: this call could be optimized away - since the same values must
142111 ** have been requested when testing key $P in whereEqualScanEst(). */
142112 whereKeyStats(pParse, p, pRec, 0, a);
142113 iLower = a[0];
142114 iUpper = a[0] + a[1];
142115 }
142116
142117 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 )((void) (0));
142118 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 )((void) (0));
142119 assert( p->aSortOrder!=0 )((void) (0));
142120 if( p->aSortOrder[nEq] ){
142121 /* The roles of pLower and pUpper are swapped for a DESC index */
142122 SWAP(WhereTerm*, pLower, pUpper){WhereTerm* t=pLower; pLower=pUpper; pUpper=t;};
142123 SWAP(int, nBtm, nTop){int t=nBtm; nBtm=nTop; nTop=t;};
142124 }
142125
142126 /* If possible, improve on the iLower estimate using ($P:$L). */
142127 if( pLower ){
142128 int n; /* Values extracted from pExpr */
142129 Expr *pExpr = pLower->pExpr->pRight;
142130 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
142131 if( rc==SQLITE_OK0 && n ){
142132 tRowcnt iNew;
142133 u16 mask = WO_GT(0x0002<<(54 -53))|WO_LE(0x0002<<(55 -53));
142134 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE(0x0002<<(55 -53))|WO_LT(0x0002<<(56 -53)));
142135 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
142136 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
142137 if( iNew>iLower ) iLower = iNew;
142138 nOut--;
142139 pLower = 0;
142140 }
142141 }
142142
142143 /* If possible, improve on the iUpper estimate using ($P:$U). */
142144 if( pUpper ){
142145 int n; /* Values extracted from pExpr */
142146 Expr *pExpr = pUpper->pExpr->pRight;
142147 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
142148 if( rc==SQLITE_OK0 && n ){
142149 tRowcnt iNew;
142150 u16 mask = WO_GT(0x0002<<(54 -53))|WO_LE(0x0002<<(55 -53));
142151 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE(0x0002<<(55 -53))|WO_LT(0x0002<<(56 -53)));
142152 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
142153 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
142154 if( iNew<iUpper ) iUpper = iNew;
142155 nOut--;
142156 pUpper = 0;
142157 }
142158 }
142159
142160 pBuilder->pRec = pRec;
142161 if( rc==SQLITE_OK0 ){
142162 if( iUpper>iLower ){
142163 nNew = sqlite3LogEst(iUpper - iLower);
142164 /* TUNING: If both iUpper and iLower are derived from the same
142165 ** sample, then assume they are 4x more selective. This brings
142166 ** the estimated selectivity more in line with what it would be
142167 ** if estimated without the use of STAT3/4 tables. */
142168 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) )((void) (0));
142169 }else{
142170 nNew = 10; assert( 10==sqlite3LogEst(2) )((void) (0));
142171 }
142172 if( nNew<nOut ){
142173 nOut = nNew;
142174 }
142175 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n",
142176 (u32)iLower, (u32)iUpper, nOut));
142177 }
142178 }else{
142179 int bDone = 0;
142180 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
142181 if( bDone ) return rc;
142182 }
142183 }
142184#else
142185 UNUSED_PARAMETER(pParse)(void)(pParse);
142186 UNUSED_PARAMETER(pBuilder)(void)(pBuilder);
142187 assert( pLower || pUpper )((void) (0));
142188#endif
142189 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 )((void) (0));
142190 nNew = whereRangeAdjust(pLower, nOut);
142191 nNew = whereRangeAdjust(pUpper, nNew);
142192
142193 /* TUNING: If there is both an upper and lower limit and neither limit
142194 ** has an application-defined likelihood(), assume the range is
142195 ** reduced by an additional 75%. This means that, by default, an open-ended
142196 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
142197 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
142198 ** match 1/64 of the index. */
142199 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
142200 nNew -= 20;
142201 }
142202
142203 nOut -= (pLower!=0) + (pUpper!=0);
142204 if( nNew<10 ) nNew = 10;
142205 if( nNew<nOut ) nOut = nNew;
142206#if defined(WHERETRACE_ENABLED)
142207 if( pLoop->nOut>nOut ){
142208 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
142209 pLoop->nOut, nOut));
142210 }
142211#endif
142212 pLoop->nOut = (LogEst)nOut;
142213 return rc;
142214}
142215
142216#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
142217/*
142218** Estimate the number of rows that will be returned based on
142219** an equality constraint x=VALUE and where that VALUE occurs in
142220** the histogram data. This only works when x is the left-most
142221** column of an index and sqlite_stat3 histogram data is available
142222** for that index. When pExpr==NULL that means the constraint is
142223** "x IS NULL" instead of "x=VALUE".
142224**
142225** Write the estimated row count into *pnRow and return SQLITE_OK.
142226** If unable to make an estimate, leave *pnRow unchanged and return
142227** non-zero.
142228**
142229** This routine can fail if it is unable to load a collating sequence
142230** required for string comparison, or if unable to allocate memory
142231** for a UTF conversion required for comparison. The error is stored
142232** in the pParse structure.
142233*/
142234static int whereEqualScanEst(
142235 Parse *pParse, /* Parsing & code generating context */
142236 WhereLoopBuilder *pBuilder,
142237 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
142238 tRowcnt *pnRow /* Write the revised row estimate here */
142239){
142240 Index *p = pBuilder->pNew->u.btree.pIndex;
142241 int nEq = pBuilder->pNew->u.btree.nEq;
142242 UnpackedRecord *pRec = pBuilder->pRec;
142243 int rc; /* Subfunction return code */
142244 tRowcnt a[2]; /* Statistics */
142245 int bOk;
142246
142247 assert( nEq>=1 )((void) (0));
142248 assert( nEq<=p->nColumn )((void) (0));
142249 assert( p->aSample!=0 )((void) (0));
142250 assert( p->nSample>0 )((void) (0));
142251 assert( pBuilder->nRecValid<nEq )((void) (0));
142252
142253 /* If values are not available for all fields of the index to the left
142254 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
142255 if( pBuilder->nRecValid<(nEq-1) ){
142256 return SQLITE_NOTFOUND12;
142257 }
142258
142259 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
142260 ** below would return the same value. */
142261 if( nEq>=p->nColumn ){
142262 *pnRow = 1;
142263 return SQLITE_OK0;
142264 }
142265
142266 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
142267 pBuilder->pRec = pRec;
142268 if( rc!=SQLITE_OK0 ) return rc;
142269 if( bOk==0 ) return SQLITE_NOTFOUND12;
142270 pBuilder->nRecValid = nEq;
142271
142272 whereKeyStats(pParse, p, pRec, 0, a);
142273 WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
142274 p->zName, nEq-1, (int)a[1]));
142275 *pnRow = a[1];
142276
142277 return rc;
142278}
142279#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
142280
142281#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
142282/*
142283** Estimate the number of rows that will be returned based on
142284** an IN constraint where the right-hand side of the IN operator
142285** is a list of values. Example:
142286**
142287** WHERE x IN (1,2,3,4)
142288**
142289** Write the estimated row count into *pnRow and return SQLITE_OK.
142290** If unable to make an estimate, leave *pnRow unchanged and return
142291** non-zero.
142292**
142293** This routine can fail if it is unable to load a collating sequence
142294** required for string comparison, or if unable to allocate memory
142295** for a UTF conversion required for comparison. The error is stored
142296** in the pParse structure.
142297*/
142298static int whereInScanEst(
142299 Parse *pParse, /* Parsing & code generating context */
142300 WhereLoopBuilder *pBuilder,
142301 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
142302 tRowcnt *pnRow /* Write the revised row estimate here */
142303){
142304 Index *p = pBuilder->pNew->u.btree.pIndex;
142305 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
142306 int nRecValid = pBuilder->nRecValid;
142307 int rc = SQLITE_OK0; /* Subfunction return code */
142308 tRowcnt nEst; /* Number of rows for a single term */
142309 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
142310 int i; /* Loop counter */
142311
142312 assert( p->aSample!=0 )((void) (0));
142313 for(i=0; rc==SQLITE_OK0 && i<pList->nExpr; i++){
142314 nEst = nRow0;
142315 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
142316 nRowEst += nEst;
142317 pBuilder->nRecValid = nRecValid;
142318 }
142319
142320 if( rc==SQLITE_OK0 ){
142321 if( nRowEst > nRow0 ) nRowEst = nRow0;
142322 *pnRow = nRowEst;
142323 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
142324 }
142325 assert( pBuilder->nRecValid==nRecValid )((void) (0));
142326 return rc;
142327}
142328#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
142329
142330
142331#ifdef WHERETRACE_ENABLED
142332/*
142333** Print the content of a WhereTerm object
142334*/
142335static void whereTermPrint(WhereTerm *pTerm, int iTerm){
142336 if( pTerm==0 ){
142337 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
142338 }else{
142339 char zType[4];
142340 char zLeft[50];
142341 memcpy(zType, "...", 4);
142342 if( pTerm->wtFlags & TERM_VIRTUAL0x02 ) zType[0] = 'V';
142343 if( pTerm->eOperator & WO_EQUIV0x0800 ) zType[1] = 'E';
142344 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin)(((pTerm->pExpr)->flags&(0x000001))!=0) ) zType[2] = 'L';
142345 if( pTerm->eOperator & WO_SINGLE0x01ff ){
142346 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}",
142347 pTerm->leftCursor, pTerm->u.leftColumn);
142348 }else if( (pTerm->eOperator & WO_OR0x0200)!=0 && pTerm->u.pOrInfo!=0 ){
142349 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%lld",
142350 pTerm->u.pOrInfo->indexable);
142351 }else{
142352 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
142353 }
142354 sqlite3DebugPrintf(
142355 "TERM-%-3d %p %s %-12s prob=%-3d op=0x%03x wtFlags=0x%04x",
142356 iTerm, pTerm, zType, zLeft, pTerm->truthProb,
142357 pTerm->eOperator, pTerm->wtFlags);
142358 if( pTerm->iField ){
142359 sqlite3DebugPrintf(" iField=%d\n", pTerm->iField);
142360 }else{
142361 sqlite3DebugPrintf("\n");
142362 }
142363 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
142364 }
142365}
142366#endif
142367
142368#ifdef WHERETRACE_ENABLED
142369/*
142370** Show the complete content of a WhereClause
142371*/
142372SQLITE_PRIVATEstatic void sqlite3WhereClausePrint(WhereClause *pWC){
142373 int i;
142374 for(i=0; i<pWC->nTerm; i++){
142375 whereTermPrint(&pWC->a[i], i);
142376 }
142377}
142378#endif
142379
142380#ifdef WHERETRACE_ENABLED
142381/*
142382** Print a WhereLoop object for debugging purposes
142383*/
142384static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
142385 WhereInfo *pWInfo = pWC->pWInfo;
142386 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
142387 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
142388 Table *pTab = pItem->pTab;
142389 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
142390 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
142391 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
142392 sqlite3DebugPrintf(" %12s",
142393 pItem->zAlias ? pItem->zAlias : pTab->zName);
142394 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 ){
142395 const char *zName;
142396 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
142397 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
142398 int i = sqlite3Strlen30(zName) - 1;
142399 while( zName[i]!='_' ) i--;
142400 zName += i;
142401 }
142402 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
142403 }else{
142404 sqlite3DebugPrintf("%20s","");
142405 }
142406 }else{
142407 char *z;
142408 if( p->u.vtab.idxStr ){
142409 z = sqlite3_mprintf("(%d,\"%s\",%x)",
142410 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
142411 }else{
142412 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
142413 }
142414 sqlite3DebugPrintf(" %-19s", z);
142415 sqlite3_free(z);
142416 }
142417 if( p->wsFlags & WHERE_SKIPSCAN0x00008000 ){
142418 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
142419 }else{
142420 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
142421 }
142422 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
142423 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
142424 int i;
142425 for(i=0; i<p->nLTerm; i++){
142426 whereTermPrint(p->aLTerm[i], i);
142427 }
142428 }
142429}
142430#endif
142431
142432/*
142433** Convert bulk memory into a valid WhereLoop that can be passed
142434** to whereLoopClear harmlessly.
142435*/
142436static void whereLoopInit(WhereLoop *p){
142437 p->aLTerm = p->aLTermSpace;
142438 p->nLTerm = 0;
142439 p->nLSlot = ArraySize(p->aLTermSpace)((int)(sizeof(p->aLTermSpace)/sizeof(p->aLTermSpace[0])
))
;
142440 p->wsFlags = 0;
142441}
142442
142443/*
142444** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact.
142445*/
142446static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
142447 if( p->wsFlags & (WHERE_VIRTUALTABLE0x00000400|WHERE_AUTO_INDEX0x00004000) ){
142448 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 && p->u.vtab.needFree ){
142449 sqlite3_free(p->u.vtab.idxStr);
142450 p->u.vtab.needFree = 0;
142451 p->u.vtab.idxStr = 0;
142452 }else if( (p->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 && p->u.btree.pIndex!=0 ){
142453 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
142454 sqlite3DbFreeNN(db, p->u.btree.pIndex);
142455 p->u.btree.pIndex = 0;
142456 }
142457 }
142458}
142459
142460/*
142461** Deallocate internal memory used by a WhereLoop object
142462*/
142463static void whereLoopClear(sqlite3 *db, WhereLoop *p){
142464 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
142465 whereLoopClearUnion(db, p);
142466 whereLoopInit(p);
142467}
142468
142469/*
142470** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
142471*/
142472static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
142473 WhereTerm **paNew;
142474 if( p->nLSlot>=n ) return SQLITE_OK0;
142475 n = (n+7)&~7;
142476 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
142477 if( paNew==0 ) return SQLITE_NOMEM_BKPT7;
142478 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
142479 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
142480 p->aLTerm = paNew;
142481 p->nLSlot = n;
142482 return SQLITE_OK0;
142483}
142484
142485/*
142486** Transfer content from the second pLoop into the first.
142487*/
142488static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
142489 whereLoopClearUnion(db, pTo);
142490 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
142491 memset(&pTo->u, 0, sizeof(pTo->u));
142492 return SQLITE_NOMEM_BKPT7;
142493 }
142494 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ__builtin_offsetof(WhereLoop, nLSlot));
142495 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
142496 if( pFrom->wsFlags & WHERE_VIRTUALTABLE0x00000400 ){
142497 pFrom->u.vtab.needFree = 0;
142498 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 ){
142499 pFrom->u.btree.pIndex = 0;
142500 }
142501 return SQLITE_OK0;
142502}
142503
142504/*
142505** Delete a WhereLoop object
142506*/
142507static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
142508 whereLoopClear(db, p);
142509 sqlite3DbFreeNN(db, p);
142510}
142511
142512/*
142513** Free a WhereInfo structure
142514*/
142515static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
142516 int i;
142517 assert( pWInfo!=0 )((void) (0));
142518 for(i=0; i<pWInfo->nLevel; i++){
142519 WhereLevel *pLevel = &pWInfo->a[i];
142520 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE0x00000800) ){
142521 sqlite3DbFree(db, pLevel->u.in.aInLoop);
142522 }
142523 }
142524 sqlite3WhereClauseClear(&pWInfo->sWC);
142525 while( pWInfo->pLoops ){
142526 WhereLoop *p = pWInfo->pLoops;
142527 pWInfo->pLoops = p->pNextLoop;
142528 whereLoopDelete(db, p);
142529 }
142530 sqlite3DbFreeNN(db, pWInfo);
142531}
142532
142533/*
142534** Return TRUE if all of the following are true:
142535**
142536** (1) X has the same or lower cost that Y
142537** (2) X uses fewer WHERE clause terms than Y
142538** (3) Every WHERE clause term used by X is also used by Y
142539** (4) X skips at least as many columns as Y
142540** (5) If X is a covering index, than Y is too
142541**
142542** Conditions (2) and (3) mean that X is a "proper subset" of Y.
142543** If X is a proper subset of Y then Y is a better choice and ought
142544** to have a lower cost. This routine returns TRUE when that cost
142545** relationship is inverted and needs to be adjusted. Constraint (4)
142546** was added because if X uses skip-scan less than Y it still might
142547** deserve a lower cost even if it is a proper subset of Y. Constraint (5)
142548** was added because a covering index probably deserves to have a lower cost
142549** than a non-covering index even if it is a proper subset.
142550*/
142551static int whereLoopCheaperProperSubset(
142552 const WhereLoop *pX, /* First WhereLoop to compare */
142553 const WhereLoop *pY /* Compare against this WhereLoop */
142554){
142555 int i, j;
142556 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
142557 return 0; /* X is not a subset of Y */
142558 }
142559 if( pY->nSkip > pX->nSkip ) return 0;
142560 if( pX->rRun >= pY->rRun ){
142561 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
142562 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
142563 }
142564 for(i=pX->nLTerm-1; i>=0; i--){
142565 if( pX->aLTerm[i]==0 ) continue;
142566 for(j=pY->nLTerm-1; j>=0; j--){
142567 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
142568 }
142569 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
142570 }
142571 if( (pX->wsFlags&WHERE_IDX_ONLY0x00000040)!=0
142572 && (pY->wsFlags&WHERE_IDX_ONLY0x00000040)==0 ){
142573 return 0; /* Constraint (5) */
142574 }
142575 return 1; /* All conditions meet */
142576}
142577
142578/*
142579** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so
142580** that:
142581**
142582** (1) pTemplate costs less than any other WhereLoops that are a proper
142583** subset of pTemplate
142584**
142585** (2) pTemplate costs more than any other WhereLoops for which pTemplate
142586** is a proper subset.
142587**
142588** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
142589** WHERE clause terms than Y and that every WHERE clause term used by X is
142590** also used by Y.
142591*/
142592static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
142593 if( (pTemplate->wsFlags & WHERE_INDEXED0x00000200)==0 ) return;
142594 for(; p; p=p->pNextLoop){
142595 if( p->iTab!=pTemplate->iTab ) continue;
142596 if( (p->wsFlags & WHERE_INDEXED0x00000200)==0 ) continue;
142597 if( whereLoopCheaperProperSubset(p, pTemplate) ){
142598 /* Adjust pTemplate cost downward so that it is cheaper than its
142599 ** subset p. */
142600 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
142601 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
142602 pTemplate->rRun = p->rRun;
142603 pTemplate->nOut = p->nOut - 1;
142604 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){
142605 /* Adjust pTemplate cost upward so that it is costlier than p since
142606 ** pTemplate is a proper subset of p */
142607 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
142608 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
142609 pTemplate->rRun = p->rRun;
142610 pTemplate->nOut = p->nOut + 1;
142611 }
142612 }
142613}
142614
142615/*
142616** Search the list of WhereLoops in *ppPrev looking for one that can be
142617** replaced by pTemplate.
142618**
142619** Return NULL if pTemplate does not belong on the WhereLoop list.
142620** In other words if pTemplate ought to be dropped from further consideration.
142621**
142622** If pX is a WhereLoop that pTemplate can replace, then return the
142623** link that points to pX.
142624**
142625** If pTemplate cannot replace any existing element of the list but needs
142626** to be added to the list as a new entry, then return a pointer to the
142627** tail of the list.
142628*/
142629static WhereLoop **whereLoopFindLesser(
142630 WhereLoop **ppPrev,
142631 const WhereLoop *pTemplate
142632){
142633 WhereLoop *p;
142634 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
142635 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
142636 /* If either the iTab or iSortIdx values for two WhereLoop are different
142637 ** then those WhereLoops need to be considered separately. Neither is
142638 ** a candidate to replace the other. */
142639 continue;
142640 }
142641 /* In the current implementation, the rSetup value is either zero
142642 ** or the cost of building an automatic index (NlogN) and the NlogN
142643 ** is the same for compatible WhereLoops. */
142644 assert( p->rSetup==0 || pTemplate->rSetup==0((void) (0))
142645 || p->rSetup==pTemplate->rSetup )((void) (0));
142646
142647 /* whereLoopAddBtree() always generates and inserts the automatic index
142648 ** case first. Hence compatible candidate WhereLoops never have a larger
142649 ** rSetup. Call this SETUP-INVARIANT */
142650 assert( p->rSetup>=pTemplate->rSetup )((void) (0));
142651
142652 /* Any loop using an appliation-defined index (or PRIMARY KEY or
142653 ** UNIQUE constraint) with one or more == constraints is better
142654 ** than an automatic index. Unless it is a skip-scan. */
142655 if( (p->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0
142656 && (pTemplate->nSkip)==0
142657 && (pTemplate->wsFlags & WHERE_INDEXED0x00000200)!=0
142658 && (pTemplate->wsFlags & WHERE_COLUMN_EQ0x00000001)!=0
142659 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
142660 ){
142661 break;
142662 }
142663
142664 /* If existing WhereLoop p is better than pTemplate, pTemplate can be
142665 ** discarded. WhereLoop p is better if:
142666 ** (1) p has no more dependencies than pTemplate, and
142667 ** (2) p has an equal or lower cost than pTemplate
142668 */
142669 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
142670 && p->rSetup<=pTemplate->rSetup /* (2a) */
142671 && p->rRun<=pTemplate->rRun /* (2b) */
142672 && p->nOut<=pTemplate->nOut /* (2c) */
142673 ){
142674 return 0; /* Discard pTemplate */
142675 }
142676
142677 /* If pTemplate is always better than p, then cause p to be overwritten
142678 ** with pTemplate. pTemplate is better than p if:
142679 ** (1) pTemplate has no more dependences than p, and
142680 ** (2) pTemplate has an equal or lower cost than p.
142681 */
142682 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
142683 && p->rRun>=pTemplate->rRun /* (2a) */
142684 && p->nOut>=pTemplate->nOut /* (2b) */
142685 ){
142686 assert( p->rSetup>=pTemplate->rSetup )((void) (0)); /* SETUP-INVARIANT above */
142687 break; /* Cause p to be overwritten by pTemplate */
142688 }
142689 }
142690 return ppPrev;
142691}
142692
142693/*
142694** Insert or replace a WhereLoop entry using the template supplied.
142695**
142696** An existing WhereLoop entry might be overwritten if the new template
142697** is better and has fewer dependencies. Or the template will be ignored
142698** and no insert will occur if an existing WhereLoop is faster and has
142699** fewer dependencies than the template. Otherwise a new WhereLoop is
142700** added based on the template.
142701**
142702** If pBuilder->pOrSet is not NULL then we care about only the
142703** prerequisites and rRun and nOut costs of the N best loops. That
142704** information is gathered in the pBuilder->pOrSet object. This special
142705** processing mode is used only for OR clause processing.
142706**
142707** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
142708** still might overwrite similar loops with the new template if the
142709** new template is better. Loops may be overwritten if the following
142710** conditions are met:
142711**
142712** (1) They have the same iTab.
142713** (2) They have the same iSortIdx.
142714** (3) The template has same or fewer dependencies than the current loop
142715** (4) The template has the same or lower cost than the current loop
142716*/
142717static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
142718 WhereLoop **ppPrev, *p;
142719 WhereInfo *pWInfo = pBuilder->pWInfo;
142720 sqlite3 *db = pWInfo->pParse->db;
142721 int rc;
142722
142723 /* Stop the search once we hit the query planner search limit */
142724 if( pBuilder->iPlanLimit==0 ){
142725 WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n"));
142726 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
142727 return SQLITE_DONE101;
142728 }
142729 pBuilder->iPlanLimit--;
142730
142731 /* If pBuilder->pOrSet is defined, then only keep track of the costs
142732 ** and prereqs.
142733 */
142734 if( pBuilder->pOrSet!=0 ){
142735 if( pTemplate->nLTerm ){
142736#if WHERETRACE_ENABLED
142737 u16 n = pBuilder->pOrSet->n;
142738 int x =
142739#endif
142740 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
142741 pTemplate->nOut);
142742#if WHERETRACE_ENABLED /* 0x8 */
142743 if( sqlite3WhereTrace & 0x8 ){
142744 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
142745 whereLoopPrint(pTemplate, pBuilder->pWC);
142746 }
142747#endif
142748 }
142749 return SQLITE_OK0;
142750 }
142751
142752 /* Look for an existing WhereLoop to replace with pTemplate
142753 */
142754 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
142755 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
142756
142757 if( ppPrev==0 ){
142758 /* There already exists a WhereLoop on the list that is better
142759 ** than pTemplate, so just ignore pTemplate */
142760#if WHERETRACE_ENABLED /* 0x8 */
142761 if( sqlite3WhereTrace & 0x8 ){
142762 sqlite3DebugPrintf(" skip: ");
142763 whereLoopPrint(pTemplate, pBuilder->pWC);
142764 }
142765#endif
142766 return SQLITE_OK0;
142767 }else{
142768 p = *ppPrev;
142769 }
142770
142771 /* If we reach this point it means that either p[] should be overwritten
142772 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
142773 ** WhereLoop and insert it.
142774 */
142775#if WHERETRACE_ENABLED /* 0x8 */
142776 if( sqlite3WhereTrace & 0x8 ){
142777 if( p!=0 ){
142778 sqlite3DebugPrintf("replace: ");
142779 whereLoopPrint(p, pBuilder->pWC);
142780 sqlite3DebugPrintf(" with: ");
142781 }else{
142782 sqlite3DebugPrintf(" add: ");
142783 }
142784 whereLoopPrint(pTemplate, pBuilder->pWC);
142785 }
142786#endif
142787 if( p==0 ){
142788 /* Allocate a new WhereLoop to add to the end of the list */
142789 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
142790 if( p==0 ) return SQLITE_NOMEM_BKPT7;
142791 whereLoopInit(p);
142792 p->pNextLoop = 0;
142793 }else{
142794 /* We will be overwriting WhereLoop p[]. But before we do, first
142795 ** go through the rest of the list and delete any other entries besides
142796 ** p[] that are also supplated by pTemplate */
142797 WhereLoop **ppTail = &p->pNextLoop;
142798 WhereLoop *pToDel;
142799 while( *ppTail ){
142800 ppTail = whereLoopFindLesser(ppTail, pTemplate);
142801 if( ppTail==0 ) break;
142802 pToDel = *ppTail;
142803 if( pToDel==0 ) break;
142804 *ppTail = pToDel->pNextLoop;
142805#if WHERETRACE_ENABLED /* 0x8 */
142806 if( sqlite3WhereTrace & 0x8 ){
142807 sqlite3DebugPrintf(" delete: ");
142808 whereLoopPrint(pToDel, pBuilder->pWC);
142809 }
142810#endif
142811 whereLoopDelete(db, pToDel);
142812 }
142813 }
142814 rc = whereLoopXfer(db, p, pTemplate);
142815 if( (p->wsFlags & WHERE_VIRTUALTABLE0x00000400)==0 ){
142816 Index *pIndex = p->u.btree.pIndex;
142817 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK3 ){
142818 p->u.btree.pIndex = 0;
142819 }
142820 }
142821 return rc;
142822}
142823
142824/*
142825** Adjust the WhereLoop.nOut value downward to account for terms of the
142826** WHERE clause that reference the loop but which are not used by an
142827** index.
142828*
142829** For every WHERE clause term that is not used by the index
142830** and which has a truth probability assigned by one of the likelihood(),
142831** likely(), or unlikely() SQL functions, reduce the estimated number
142832** of output rows by the probability specified.
142833**
142834** TUNING: For every WHERE clause term that is not used by the index
142835** and which does not have an assigned truth probability, heuristics
142836** described below are used to try to estimate the truth probability.
142837** TODO --> Perhaps this is something that could be improved by better
142838** table statistics.
142839**
142840** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75%
142841** value corresponds to -1 in LogEst notation, so this means decrement
142842** the WhereLoop.nOut field for every such WHERE clause term.
142843**
142844** Heuristic 2: If there exists one or more WHERE clause terms of the
142845** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the
142846** final output row estimate is no greater than 1/4 of the total number
142847** of rows in the table. In other words, assume that x==EXPR will filter
142848** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
142849** "x" column is boolean or else -1 or 0 or 1 is a common default value
142850** on the "x" column and so in that case only cap the output row estimate
142851** at 1/2 instead of 1/4.
142852*/
142853static void whereLoopOutputAdjust(
142854 WhereClause *pWC, /* The WHERE clause */
142855 WhereLoop *pLoop, /* The loop to adjust downward */
142856 LogEst nRow /* Number of rows in the entire table */
142857){
142858 WhereTerm *pTerm, *pX;
142859 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
142860 int i, j, k;
142861 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
142862
142863 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 )((void) (0));
142864 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
142865 if( (pTerm->wtFlags & TERM_VIRTUAL0x02)!=0 ) break;
142866 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
142867 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
142868 for(j=pLoop->nLTerm-1; j>=0; j--){
142869 pX = pLoop->aLTerm[j];
142870 if( pX==0 ) continue;
142871 if( pX==pTerm ) break;
142872 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
142873 }
142874 if( j<0 ){
142875 if( pTerm->truthProb<=0 ){
142876 /* If a truth probability is specified using the likelihood() hints,
142877 ** then use the probability provided by the application. */
142878 pLoop->nOut += pTerm->truthProb;
142879 }else{
142880 /* In the absence of explicit truth probabilities, use heuristics to
142881 ** guess a reasonable truth probability. */
142882 pLoop->nOut--;
142883 if( pTerm->eOperator&(WO_EQ0x0002|WO_IS0x0080) ){
142884 Expr *pRight = pTerm->pExpr->pRight;
142885 testcase( pTerm->pExpr->op==TK_IS );
142886 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
142887 k = 10;
142888 }else{
142889 k = 20;
142890 }
142891 if( iReduce<k ) iReduce = k;
142892 }
142893 }
142894 }
142895 }
142896 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce;
142897}
142898
142899/*
142900** Term pTerm is a vector range comparison operation. The first comparison
142901** in the vector can be optimized using column nEq of the index. This
142902** function returns the total number of vector elements that can be used
142903** as part of the range comparison.
142904**
142905** For example, if the query is:
142906**
142907** WHERE a = ? AND (b, c, d) > (?, ?, ?)
142908**
142909** and the index:
142910**
142911** CREATE INDEX ... ON (a, b, c, d, e)
142912**
142913** then this function would be invoked with nEq=1. The value returned in
142914** this case is 3.
142915*/
142916static int whereRangeVectorLen(
142917 Parse *pParse, /* Parsing context */
142918 int iCur, /* Cursor open on pIdx */
142919 Index *pIdx, /* The index to be used for a inequality constraint */
142920 int nEq, /* Number of prior equality constraints on same index */
142921 WhereTerm *pTerm /* The vector inequality constraint */
142922){
142923 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
142924 int i;
142925
142926 nCmp = MIN(nCmp, (pIdx->nColumn - nEq))((nCmp)<((pIdx->nColumn - nEq))?(nCmp):((pIdx->nColumn
- nEq)))
;
142927 for(i=1; i<nCmp; i++){
142928 /* Test if comparison i of pTerm is compatible with column (i+nEq)
142929 ** of the index. If not, exit the loop. */
142930 char aff; /* Comparison affinity */
142931 char idxaff = 0; /* Indexed columns affinity */
142932 CollSeq *pColl; /* Comparison collation sequence */
142933 Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
142934 Expr *pRhs = pTerm->pExpr->pRight;
142935 if( pRhs->flags & EP_xIsSelect0x000800 ){
142936 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
142937 }else{
142938 pRhs = pRhs->x.pList->a[i].pExpr;
142939 }
142940
142941 /* Check that the LHS of the comparison is a column reference to
142942 ** the right column of the right source table. And that the sort
142943 ** order of the index column is the same as the sort order of the
142944 ** leftmost index column. */
142945 if( pLhs->op!=TK_COLUMN162
142946 || pLhs->iTable!=iCur
142947 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
142948 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
142949 ){
142950 break;
142951 }
142952
142953 testcase( pLhs->iColumn==XN_ROWID );
142954 aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
142955 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
142956 if( aff!=idxaff ) break;
142957
142958 pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
142959 if( pColl==0 ) break;
142960 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
142961 }
142962 return i;
142963}
142964
142965/*
142966** Adjust the cost C by the costMult facter T. This only occurs if
142967** compiled with -DSQLITE_ENABLE_COSTMULT
142968*/
142969#ifdef SQLITE_ENABLE_COSTMULT
142970# define ApplyCostMultiplier(C,T) C += T
142971#else
142972# define ApplyCostMultiplier(C,T)
142973#endif
142974
142975/*
142976** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
142977** index pIndex. Try to match one more.
142978**
142979** When this function is called, pBuilder->pNew->nOut contains the
142980** number of rows expected to be visited by filtering using the nEq
142981** terms only. If it is modified, this value is restored before this
142982** function returns.
142983**
142984** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
142985** a fake index used for the INTEGER PRIMARY KEY.
142986*/
142987static int whereLoopAddBtreeIndex(
142988 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
142989 struct SrcList_item *pSrc, /* FROM clause term being analyzed */
142990 Index *pProbe, /* An index on pSrc */
142991 LogEst nInMul /* log(Number of iterations due to IN) */
142992){
142993 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
142994 Parse *pParse = pWInfo->pParse; /* Parsing context */
142995 sqlite3 *db = pParse->db; /* Database connection malloc context */
142996 WhereLoop *pNew; /* Template WhereLoop under construction */
142997 WhereTerm *pTerm; /* A WhereTerm under consideration */
142998 int opMask; /* Valid operators for constraints */
142999 WhereScan scan; /* Iterator for WHERE terms */
143000 Bitmask saved_prereq; /* Original value of pNew->prereq */
143001 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
143002 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
143003 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
143004 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
143005 u16 saved_nSkip; /* Original value of pNew->nSkip */
143006 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
143007 LogEst saved_nOut; /* Original value of pNew->nOut */
143008 int rc = SQLITE_OK0; /* Return code */
143009 LogEst rSize; /* Number of rows in the table */
143010 LogEst rLogSize; /* Logarithm of table size */
143011 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
143012
143013 pNew = pBuilder->pNew;
143014 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT7;
143015 WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d\n",
143016 pProbe->pTable->zName,pProbe->zName, pNew->u.btree.nEq));
143017
143018 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 )((void) (0));
143019 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 )((void) (0));
143020 if( pNew->wsFlags & WHERE_BTM_LIMIT0x00000020 ){
143021 opMask = WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53));
143022 }else{
143023 assert( pNew->u.btree.nBtm==0 )((void) (0));
143024 opMask = WO_EQ0x0002|WO_IN0x0001|WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53))|WO_ISNULL0x0100|WO_IS0x0080;
143025 }
143026 if( pProbe->bUnordered ) opMask &= ~(WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))|WO_LT(0x0002<<(56 -53))|WO_LE(0x0002<<(55 -53)));
143027
143028 assert( pNew->u.btree.nEq<pProbe->nColumn )((void) (0));
143029
143030 saved_nEq = pNew->u.btree.nEq;
143031 saved_nBtm = pNew->u.btree.nBtm;
143032 saved_nTop = pNew->u.btree.nTop;
143033 saved_nSkip = pNew->nSkip;
143034 saved_nLTerm = pNew->nLTerm;
143035 saved_wsFlags = pNew->wsFlags;
143036 saved_prereq = pNew->prereq;
143037 saved_nOut = pNew->nOut;
143038 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
143039 opMask, pProbe);
143040 pNew->rSetup = 0;
143041 rSize = pProbe->aiRowLogEst[0];
143042 rLogSize = estLog(rSize);
143043 for(; rc==SQLITE_OK0 && pTerm!=0; pTerm = whereScanNext(&scan)){
143044 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
143045 LogEst rCostIdx;
143046 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
143047 int nIn = 0;
143048#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
143049 int nRecValid = pBuilder->nRecValid;
143050#endif
143051 if( (eOp==WO_ISNULL0x0100 || (pTerm->wtFlags&TERM_VNULL0x00)!=0)
143052 && indexColumnNotNull(pProbe, saved_nEq)
143053 ){
143054 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
143055 }
143056 if( pTerm->prereqRight & pNew->maskSelf ) continue;
143057
143058 /* Do not allow the upper bound of a LIKE optimization range constraint
143059 ** to mix with a lower range bound from some other source */
143060 if( pTerm->wtFlags & TERM_LIKEOPT0x100 && pTerm->eOperator==WO_LT(0x0002<<(56 -53)) ) continue;
143061
143062 /* Do not allow constraints from the WHERE clause to be used by the
143063 ** right table of a LEFT JOIN. Only constraints in the ON clause are
143064 ** allowed */
143065 if( (pSrc->fg.jointype & JT_LEFT0x0008)!=0
143066 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)(((pTerm->pExpr)->flags&(0x000001))!=0)
143067 ){
143068 continue;
143069 }
143070
143071 if( IsUniqueIndex(pProbe)((pProbe)->onError!=0) && saved_nEq==pProbe->nKeyCol-1 ){
143072 pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE0x0002;
143073 }else{
143074 pBuilder->bldFlags |= SQLITE_BLDF_INDEXED0x0001;
143075 }
143076 pNew->wsFlags = saved_wsFlags;
143077 pNew->u.btree.nEq = saved_nEq;
143078 pNew->u.btree.nBtm = saved_nBtm;
143079 pNew->u.btree.nTop = saved_nTop;
143080 pNew->nLTerm = saved_nLTerm;
143081 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
143082 pNew->aLTerm[pNew->nLTerm++] = pTerm;
143083 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
143084
143085 assert( nInMul==0((void) (0))
143086 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0((void) (0))
143087 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0((void) (0))
143088 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0((void) (0))
143089 )((void) (0));
143090
143091 if( eOp & WO_IN0x0001 ){
143092 Expr *pExpr = pTerm->pExpr;
143093 if( ExprHasProperty(pExpr, EP_xIsSelect)(((pExpr)->flags&(0x000800))!=0) ){
143094 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
143095 int i;
143096 nIn = 46; assert( 46==sqlite3LogEst(25) )((void) (0));
143097
143098 /* The expression may actually be of the form (x, y) IN (SELECT...).
143099 ** In this case there is a separate term for each of (x) and (y).
143100 ** However, the nIn multiplier should only be applied once, not once
143101 ** for each such term. The following loop checks that pTerm is the
143102 ** first such term in use, and sets nIn back to 0 if it is not. */
143103 for(i=0; i<pNew->nLTerm-1; i++){
143104 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
143105 }
143106 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr)(pExpr->x.pList && pExpr->x.pList->nExpr) ){
143107 /* "x IN (value, value, ...)" */
143108 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
143109 assert( nIn>0 )((void) (0)); /* RHS always has 2 or more terms... The parser
143110 ** changes "x IN (?)" into "x=?". */
143111 }
143112 if( pProbe->hasStat1 ){
143113 LogEst M, logK, safetyMargin;
143114 /* Let:
143115 ** N = the total number of rows in the table
143116 ** K = the number of entries on the RHS of the IN operator
143117 ** M = the number of rows in the table that match terms to the
143118 ** to the left in the same index. If the IN operator is on
143119 ** the left-most index column, M==N.
143120 **
143121 ** Given the definitions above, it is better to omit the IN operator
143122 ** from the index lookup and instead do a scan of the M elements,
143123 ** testing each scanned row against the IN operator separately, if:
143124 **
143125 ** M*log(K) < K*log(N)
143126 **
143127 ** Our estimates for M, K, and N might be inaccurate, so we build in
143128 ** a safety margin of 2 (LogEst: 10) that favors using the IN operator
143129 ** with the index, as using an index has better worst-case behavior.
143130 ** If we do not have real sqlite_stat1 data, always prefer to use
143131 ** the index.
143132 */
143133 M = pProbe->aiRowLogEst[saved_nEq];
143134 logK = estLog(nIn);
143135 safetyMargin = 10; /* TUNING: extra weight for indexed IN */
143136 if( M + logK + safetyMargin < nIn + rLogSize ){
143137 WHERETRACE(0x40,
143138 ("Scan preferred over IN operator on column %d of \"%s\" (%d<%d)\n",
143139 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
143140 continue;
143141 }else{
143142 WHERETRACE(0x40,
143143 ("IN operator preferred on column %d of \"%s\" (%d>=%d)\n",
143144 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
143145 }
143146 }
143147 pNew->wsFlags |= WHERE_COLUMN_IN0x00000004;
143148 }else if( eOp & (WO_EQ0x0002|WO_IS0x0080) ){
143149 int iCol = pProbe->aiColumn[saved_nEq];
143150 pNew->wsFlags |= WHERE_COLUMN_EQ0x00000001;
143151 assert( saved_nEq==pNew->u.btree.nEq )((void) (0));
143152 if( iCol==XN_ROWID(-1)
143153 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
143154 ){
143155 if( iCol==XN_ROWID(-1) || pProbe->uniqNotNull
143156 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ0x0002)
143157 ){
143158 pNew->wsFlags |= WHERE_ONEROW0x00001000;
143159 }else{
143160 pNew->wsFlags |= WHERE_UNQ_WANTED0x00010000;
143161 }
143162 }
143163 }else if( eOp & WO_ISNULL0x0100 ){
143164 pNew->wsFlags |= WHERE_COLUMN_NULL0x00000008;
143165 }else if( eOp & (WO_GT(0x0002<<(54 -53))|WO_GE(0x0002<<(57 -53))) ){
143166 testcase( eOp & WO_GT );
143167 testcase( eOp & WO_GE );
143168 pNew->wsFlags |= WHERE_COLUMN_RANGE0x00000002|WHERE_BTM_LIMIT0x00000020;
143169 pNew->u.btree.nBtm = whereRangeVectorLen(
143170 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
143171 );
143172 pBtm = pTerm;
143173 pTop = 0;
143174 if( pTerm->wtFlags & TERM_LIKEOPT0x100 ){
143175 /* Range contraints that come from the LIKE optimization are
143176 ** always used in pairs. */
143177 pTop = &pTerm[1];
143178 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm )((void) (0));
143179 assert( pTop->wtFlags & TERM_LIKEOPT )((void) (0));
143180 assert( pTop->eOperator==WO_LT )((void) (0));
143181 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
143182 pNew->aLTerm[pNew->nLTerm++] = pTop;
143183 pNew->wsFlags |= WHERE_TOP_LIMIT0x00000010;
143184 pNew->u.btree.nTop = 1;
143185 }
143186 }else{
143187 assert( eOp & (WO_LT|WO_LE) )((void) (0));
143188 testcase( eOp & WO_LT );
143189 testcase( eOp & WO_LE );
143190 pNew->wsFlags |= WHERE_COLUMN_RANGE0x00000002|WHERE_TOP_LIMIT0x00000010;
143191 pNew->u.btree.nTop = whereRangeVectorLen(
143192 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
143193 );
143194 pTop = pTerm;
143195 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT0x00000020)!=0 ?
143196 pNew->aLTerm[pNew->nLTerm-2] : 0;
143197 }
143198
143199 /* At this point pNew->nOut is set to the number of rows expected to
143200 ** be visited by the index scan before considering term pTerm, or the
143201 ** values of nIn and nInMul. In other words, assuming that all
143202 ** "x IN(...)" terms are replaced with "x = ?". This block updates
143203 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
143204 assert( pNew->nOut==saved_nOut )((void) (0));
143205 if( pNew->wsFlags & WHERE_COLUMN_RANGE0x00000002 ){
143206 /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
143207 ** data, using some other estimate. */
143208 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
143209 }else{
143210 int nEq = ++pNew->u.btree.nEq;
143211 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) )((void) (0));
143212
143213 assert( pNew->nOut==saved_nOut )((void) (0));
143214 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
143215 assert( (eOp & WO_IN) || nIn==0 )((void) (0));
143216 testcase( eOp & WO_IN );
143217 pNew->nOut += pTerm->truthProb;
143218 pNew->nOut -= nIn;
143219 }else{
143220#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
143221 tRowcnt nOut = 0;
143222 if( nInMul==0
143223 && pProbe->nSample
143224 && pNew->u.btree.nEq<=pProbe->nSampleCol
143225 && ((eOp & WO_IN0x0001)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)(((pTerm->pExpr)->flags&(0x000800))!=0))
143226 && OptimizationEnabled(db, SQLITE_Stat34)(((db)->dbOptFlags&(0x0800))==0)
143227 ){
143228 Expr *pExpr = pTerm->pExpr;
143229 if( (eOp & (WO_EQ0x0002|WO_ISNULL0x0100|WO_IS0x0080))!=0 ){
143230 testcase( eOp & WO_EQ );
143231 testcase( eOp & WO_IS );
143232 testcase( eOp & WO_ISNULL );
143233 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
143234 }else{
143235 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
143236 }
143237 if( rc==SQLITE_NOTFOUND12 ) rc = SQLITE_OK0;
143238 if( rc!=SQLITE_OK0 ) break; /* Jump out of the pTerm loop */
143239 if( nOut ){
143240 pNew->nOut = sqlite3LogEst(nOut);
143241 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
143242 pNew->nOut -= nIn;
143243 }
143244 }
143245 if( nOut==0 )
143246#endif
143247 {
143248 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
143249 if( eOp & WO_ISNULL0x0100 ){
143250 /* TUNING: If there is no likelihood() value, assume that a
143251 ** "col IS NULL" expression matches twice as many rows
143252 ** as (col=?). */
143253 pNew->nOut += 10;
143254 }
143255 }
143256 }
143257 }
143258
143259 /* Set rCostIdx to the cost of visiting selected rows in index. Add
143260 ** it to pNew->rRun, which is currently set to the cost of the index
143261 ** seek only. Then, if this is a non-covering index, add the cost of
143262 ** visiting the rows in the main table. */
143263 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
143264 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
143265 if( (pNew->wsFlags & (WHERE_IDX_ONLY0x00000040|WHERE_IPK0x00000100))==0 ){
143266 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
143267 }
143268 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
143269
143270 nOutUnadjusted = pNew->nOut;
143271 pNew->rRun += nInMul + nIn;
143272 pNew->nOut += nInMul + nIn;
143273 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
143274 rc = whereLoopInsert(pBuilder, pNew);
143275
143276 if( pNew->wsFlags & WHERE_COLUMN_RANGE0x00000002 ){
143277 pNew->nOut = saved_nOut;
143278 }else{
143279 pNew->nOut = nOutUnadjusted;
143280 }
143281
143282 if( (pNew->wsFlags & WHERE_TOP_LIMIT0x00000010)==0
143283 && pNew->u.btree.nEq<pProbe->nColumn
143284 ){
143285 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
143286 }
143287 pNew->nOut = saved_nOut;
143288#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
143289 pBuilder->nRecValid = nRecValid;
143290#endif
143291 }
143292 pNew->prereq = saved_prereq;
143293 pNew->u.btree.nEq = saved_nEq;
143294 pNew->u.btree.nBtm = saved_nBtm;
143295 pNew->u.btree.nTop = saved_nTop;
143296 pNew->nSkip = saved_nSkip;
143297 pNew->wsFlags = saved_wsFlags;
143298 pNew->nOut = saved_nOut;
143299 pNew->nLTerm = saved_nLTerm;
143300
143301 /* Consider using a skip-scan if there are no WHERE clause constraints
143302 ** available for the left-most terms of the index, and if the average
143303 ** number of repeats in the left-most terms is at least 18.
143304 **
143305 ** The magic number 18 is selected on the basis that scanning 17 rows
143306 ** is almost always quicker than an index seek (even though if the index
143307 ** contains fewer than 2^17 rows we assume otherwise in other parts of
143308 ** the code). And, even if it is not, it should not be too much slower.
143309 ** On the other hand, the extra seeks could end up being significantly
143310 ** more expensive. */
143311 assert( 42==sqlite3LogEst(18) )((void) (0));
143312 if( saved_nEq==saved_nSkip
143313 && saved_nEq+1<pProbe->nKeyCol
143314 && pProbe->noSkipScan==0
143315 && OptimizationEnabled(db, SQLITE_SkipScan)(((db)->dbOptFlags&(0x4000))==0)
143316 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
143317 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK0
143318 ){
143319 LogEst nIter;
143320 pNew->u.btree.nEq++;
143321 pNew->nSkip++;
143322 pNew->aLTerm[pNew->nLTerm++] = 0;
143323 pNew->wsFlags |= WHERE_SKIPSCAN0x00008000;
143324 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
143325 pNew->nOut -= nIter;
143326 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
143327 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
143328 nIter += 5;
143329 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
143330 pNew->nOut = saved_nOut;
143331 pNew->u.btree.nEq = saved_nEq;
143332 pNew->nSkip = saved_nSkip;
143333 pNew->wsFlags = saved_wsFlags;
143334 }
143335
143336 WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n",
143337 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
143338 return rc;
143339}
143340
143341/*
143342** Return True if it is possible that pIndex might be useful in
143343** implementing the ORDER BY clause in pBuilder.
143344**
143345** Return False if pBuilder does not contain an ORDER BY clause or
143346** if there is no way for pIndex to be useful in implementing that
143347** ORDER BY clause.
143348*/
143349static int indexMightHelpWithOrderBy(
143350 WhereLoopBuilder *pBuilder,
143351 Index *pIndex,
143352 int iCursor
143353){
143354 ExprList *pOB;
143355 ExprList *aColExpr;
143356 int ii, jj;
143357
143358 if( pIndex->bUnordered ) return 0;
143359 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
143360 for(ii=0; ii<pOB->nExpr; ii++){
143361 Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
143362 if( pExpr->op==TK_COLUMN162 && pExpr->iTable==iCursor ){
143363 if( pExpr->iColumn<0 ) return 1;
143364 for(jj=0; jj<pIndex->nKeyCol; jj++){
143365 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
143366 }
143367 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
143368 for(jj=0; jj<pIndex->nKeyCol; jj++){
143369 if( pIndex->aiColumn[jj]!=XN_EXPR(-2) ) continue;
143370 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
143371 return 1;
143372 }
143373 }
143374 }
143375 }
143376 return 0;
143377}
143378
143379/* Check to see if a partial index with pPartIndexWhere can be used
143380** in the current query. Return true if it can be and false if not.
143381*/
143382static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
143383 int i;
143384 WhereTerm *pTerm;
143385 Parse *pParse = pWC->pWInfo->pParse;
143386 while( pWhere->op==TK_AND44 ){
143387 if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0;
143388 pWhere = pWhere->pRight;
143389 }
143390 if( pParse->db->flags & SQLITE_EnableQPSG0x00800000 ) pParse = 0;
143391 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
143392 Expr *pExpr = pTerm->pExpr;
143393 if( (!ExprHasProperty(pExpr, EP_FromJoin)(((pExpr)->flags&(0x000001))!=0) || pExpr->iRightJoinTable==iTab)
143394 && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
143395 ){
143396 return 1;
143397 }
143398 }
143399 return 0;
143400}
143401
143402/*
143403** Add all WhereLoop objects for a single table of the join where the table
143404** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
143405** a b-tree table, not a virtual table.
143406**
143407** The costs (WhereLoop.rRun) of the b-tree loops added by this function
143408** are calculated as follows:
143409**
143410** For a full scan, assuming the table (or index) contains nRow rows:
143411**
143412** cost = nRow * 3.0 // full-table scan
143413** cost = nRow * K // scan of covering index
143414** cost = nRow * (K+3.0) // scan of non-covering index
143415**
143416** where K is a value between 1.1 and 3.0 set based on the relative
143417** estimated average size of the index and table records.
143418**
143419** For an index scan, where nVisit is the number of index rows visited
143420** by the scan, and nSeek is the number of seek operations required on
143421** the index b-tree:
143422**
143423** cost = nSeek * (log(nRow) + K * nVisit) // covering index
143424** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
143425**
143426** Normally, nSeek is 1. nSeek values greater than 1 come about if the
143427** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
143428** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
143429**
143430** The estimated values (nRow, nVisit, nSeek) often contain a large amount
143431** of uncertainty. For this reason, scoring is designed to pick plans that
143432** "do the least harm" if the estimates are inaccurate. For example, a
143433** log(nRow) factor is omitted from a non-covering index scan in order to
143434** bias the scoring in favor of using an index, since the worst-case
143435** performance of using an index is far better than the worst-case performance
143436** of a full table scan.
143437*/
143438static int whereLoopAddBtree(
143439 WhereLoopBuilder *pBuilder, /* WHERE clause information */
143440 Bitmask mPrereq /* Extra prerequesites for using this table */
143441){
143442 WhereInfo *pWInfo; /* WHERE analysis context */
143443 Index *pProbe; /* An index we are evaluating */
143444 Index sPk; /* A fake index object for the primary key */
143445 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
143446 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
143447 SrcList *pTabList; /* The FROM clause */
143448 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
143449 WhereLoop *pNew; /* Template WhereLoop object */
143450 int rc = SQLITE_OK0; /* Return code */
143451 int iSortIdx = 1; /* Index number */
143452 int b; /* A boolean value */
143453 LogEst rSize; /* number of rows in the table */
143454 LogEst rLogSize; /* Logarithm of the number of rows in the table */
143455 WhereClause *pWC; /* The parsed WHERE clause */
143456 Table *pTab; /* Table being queried */
143457
143458 pNew = pBuilder->pNew;
143459 pWInfo = pBuilder->pWInfo;
143460 pTabList = pWInfo->pTabList;
143461 pSrc = pTabList->a + pNew->iTab;
143462 pTab = pSrc->pTab;
143463 pWC = pBuilder->pWC;
143464 assert( !IsVirtual(pSrc->pTab) )((void) (0));
143465
143466 if( pSrc->pIBIndex ){
143467 /* An INDEXED BY clause specifies a particular index to use */
143468 pProbe = pSrc->pIBIndex;
143469 }else if( !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
143470 pProbe = pTab->pIndex;
143471 }else{
143472 /* There is no INDEXED BY clause. Create a fake Index object in local
143473 ** variable sPk to represent the rowid primary key index. Make this
143474 ** fake index the first in a chain of Index objects with all of the real
143475 ** indices to follow */
143476 Index *pFirst; /* First of real indices on the table */
143477 memset(&sPk, 0, sizeof(Index));
143478 sPk.nKeyCol = 1;
143479 sPk.nColumn = 1;
143480 sPk.aiColumn = &aiColumnPk;
143481 sPk.aiRowLogEst = aiRowEstPk;
143482 sPk.onError = OE_Replace5;
143483 sPk.pTable = pTab;
143484 sPk.szIdxRow = pTab->szTabRow;
143485 sPk.idxType = SQLITE_IDXTYPE_IPK3;
143486 aiRowEstPk[0] = pTab->nRowLogEst;
143487 aiRowEstPk[1] = 0;
143488 pFirst = pSrc->pTab->pIndex;
143489 if( pSrc->fg.notIndexed==0 ){
143490 /* The real indices of the table are only considered if the
143491 ** NOT INDEXED qualifier is omitted from the FROM clause */
143492 sPk.pNext = pFirst;
143493 }
143494 pProbe = &sPk;
143495 }
143496 rSize = pTab->nRowLogEst;
143497 rLogSize = estLog(rSize);
143498
143499#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
143500 /* Automatic indexes */
143501 if( !pBuilder->pOrSet /* Not part of an OR optimization */
143502 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)==0
143503 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex0x00008000)!=0
143504 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */
143505 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
143506 && HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
143507 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
143508 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
143509 ){
143510 /* Generate auto-index WhereLoops */
143511 WhereTerm *pTerm;
143512 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
143513 for(pTerm=pWC->a; rc==SQLITE_OK0 && pTerm<pWCEnd; pTerm++){
143514 if( pTerm->prereqRight & pNew->maskSelf ) continue;
143515 if( termCanDriveIndex(pTerm, pSrc, 0) ){
143516 pNew->u.btree.nEq = 1;
143517 pNew->nSkip = 0;
143518 pNew->u.btree.pIndex = 0;
143519 pNew->nLTerm = 1;
143520 pNew->aLTerm[0] = pTerm;
143521 /* TUNING: One-time cost for computing the automatic index is
143522 ** estimated to be X*N*log2(N) where N is the number of rows in
143523 ** the table being indexed and where X is 7 (LogEst=28) for normal
143524 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
143525 ** of X is smaller for views and subqueries so that the query planner
143526 ** will be more aggressive about generating automatic indexes for
143527 ** those objects, since there is no opportunity to add schema
143528 ** indexes on subqueries and views. */
143529 pNew->rSetup = rLogSize + rSize;
143530 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral0x0002)==0 ){
143531 pNew->rSetup += 28;
143532 }else{
143533 pNew->rSetup -= 10;
143534 }
143535 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
143536 if( pNew->rSetup<0 ) pNew->rSetup = 0;
143537 /* TUNING: Each index lookup yields 20 rows in the table. This
143538 ** is more than the usual guess of 10 rows, since we have no way
143539 ** of knowing how selective the index will ultimately be. It would
143540 ** not be unreasonable to make this value much larger. */
143541 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) )((void) (0));
143542 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
143543 pNew->wsFlags = WHERE_AUTO_INDEX0x00004000;
143544 pNew->prereq = mPrereq | pTerm->prereqRight;
143545 rc = whereLoopInsert(pBuilder, pNew);
143546 }
143547 }
143548 }
143549#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
143550
143551 /* Loop over all indices. If there was an INDEXED BY clause, then only
143552 ** consider index pProbe. */
143553 for(; rc==SQLITE_OK0 && pProbe;
143554 pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++
143555 ){
143556 if( pProbe->pPartIdxWhere!=0
143557 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
143558 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
143559 continue; /* Partial index inappropriate for this query */
143560 }
143561 if( pProbe->bNoQuery ) continue;
143562 rSize = pProbe->aiRowLogEst[0];
143563 pNew->u.btree.nEq = 0;
143564 pNew->u.btree.nBtm = 0;
143565 pNew->u.btree.nTop = 0;
143566 pNew->nSkip = 0;
143567 pNew->nLTerm = 0;
143568 pNew->iSortIdx = 0;
143569 pNew->rSetup = 0;
143570 pNew->prereq = mPrereq;
143571 pNew->nOut = rSize;
143572 pNew->u.btree.pIndex = pProbe;
143573 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
143574 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
143575 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 )((void) (0));
143576 if( pProbe->idxType==SQLITE_IDXTYPE_IPK3 ){
143577 /* Integer primary key index */
143578 pNew->wsFlags = WHERE_IPK0x00000100;
143579
143580 /* Full table scan */
143581 pNew->iSortIdx = b ? iSortIdx : 0;
143582 /* TUNING: Cost of full table scan is (N*3.0). */
143583 pNew->rRun = rSize + 16;
143584 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
143585 whereLoopOutputAdjust(pWC, pNew, rSize);
143586 rc = whereLoopInsert(pBuilder, pNew);
143587 pNew->nOut = rSize;
143588 if( rc ) break;
143589 }else{
143590 Bitmask m;
143591 if( pProbe->isCovering ){
143592 pNew->wsFlags = WHERE_IDX_ONLY0x00000040 | WHERE_INDEXED0x00000200;
143593 m = 0;
143594 }else{
143595 m = pSrc->colUsed & pProbe->colNotIdxed;
143596 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY0x00000040|WHERE_INDEXED0x00000200) : WHERE_INDEXED0x00000200;
143597 }
143598
143599 /* Full scan via index */
143600 if( b
143601 || !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0)
143602 || pProbe->pPartIdxWhere!=0
143603 || ( m==0
143604 && pProbe->bUnordered==0
143605 && (pProbe->szIdxRow<pTab->szTabRow)
143606 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED0x0004)==0
143607 && sqlite3GlobalConfigsqlite3Config.bUseCis
143608 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)(((pWInfo->pParse->db)->dbOptFlags&(0x0020))==0)
143609 )
143610 ){
143611 pNew->iSortIdx = b ? iSortIdx : 0;
143612
143613 /* The cost of visiting the index rows is N*K, where K is
143614 ** between 1.1 and 3.0, depending on the relative sizes of the
143615 ** index and table rows. */
143616 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
143617 if( m!=0 ){
143618 /* If this is a non-covering index scan, add in the cost of
143619 ** doing table lookups. The cost will be 3x the number of
143620 ** lookups. Take into account WHERE clause terms that can be
143621 ** satisfied using just the index, and that do not require a
143622 ** table lookup. */
143623 LogEst nLookup = rSize + 16; /* Base cost: N*3 */
143624 int ii;
143625 int iCur = pSrc->iCursor;
143626 WhereClause *pWC2 = &pWInfo->sWC;
143627 for(ii=0; ii<pWC2->nTerm; ii++){
143628 WhereTerm *pTerm = &pWC2->a[ii];
143629 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
143630 break;
143631 }
143632 /* pTerm can be evaluated using just the index. So reduce
143633 ** the expected number of table lookups accordingly */
143634 if( pTerm->truthProb<=0 ){
143635 nLookup += pTerm->truthProb;
143636 }else{
143637 nLookup--;
143638 if( pTerm->eOperator & (WO_EQ0x0002|WO_IS0x0080) ) nLookup -= 19;
143639 }
143640 }
143641
143642 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
143643 }
143644 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
143645 whereLoopOutputAdjust(pWC, pNew, rSize);
143646 rc = whereLoopInsert(pBuilder, pNew);
143647 pNew->nOut = rSize;
143648 if( rc ) break;
143649 }
143650 }
143651
143652 pBuilder->bldFlags = 0;
143653 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
143654 if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED0x0001 ){
143655 /* If a non-unique index is used, or if a prefix of the key for
143656 ** unique index is used (making the index functionally non-unique)
143657 ** then the sqlite_stat1 data becomes important for scoring the
143658 ** plan */
143659 pTab->tabFlags |= TF_StatsUsed0x0100;
143660 }
143661#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
143662 sqlite3Stat4ProbeFree(pBuilder->pRec);
143663 pBuilder->nRecValid = 0;
143664 pBuilder->pRec = 0;
143665#endif
143666 }
143667 return rc;
143668}
143669
143670#ifndef SQLITE_OMIT_VIRTUALTABLE
143671
143672/*
143673** Argument pIdxInfo is already populated with all constraints that may
143674** be used by the virtual table identified by pBuilder->pNew->iTab. This
143675** function marks a subset of those constraints usable, invokes the
143676** xBestIndex method and adds the returned plan to pBuilder.
143677**
143678** A constraint is marked usable if:
143679**
143680** * Argument mUsable indicates that its prerequisites are available, and
143681**
143682** * It is not one of the operators specified in the mExclude mask passed
143683** as the fourth argument (which in practice is either WO_IN or 0).
143684**
143685** Argument mPrereq is a mask of tables that must be scanned before the
143686** virtual table in question. These are added to the plans prerequisites
143687** before it is added to pBuilder.
143688**
143689** Output parameter *pbIn is set to true if the plan added to pBuilder
143690** uses one or more WO_IN terms, or false otherwise.
143691*/
143692static int whereLoopAddVirtualOne(
143693 WhereLoopBuilder *pBuilder,
143694 Bitmask mPrereq, /* Mask of tables that must be used. */
143695 Bitmask mUsable, /* Mask of usable tables */
143696 u16 mExclude, /* Exclude terms using these operators */
143697 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */
143698 u16 mNoOmit, /* Do not omit these constraints */
143699 int *pbIn /* OUT: True if plan uses an IN(...) op */
143700){
143701 WhereClause *pWC = pBuilder->pWC;
143702 struct sqlite3_index_constraint *pIdxCons;
143703 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
143704 int i;
143705 int mxTerm;
143706 int rc = SQLITE_OK0;
143707 WhereLoop *pNew = pBuilder->pNew;
143708 Parse *pParse = pBuilder->pWInfo->pParse;
143709 struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
143710 int nConstraint = pIdxInfo->nConstraint;
143711
143712 assert( (mUsable & mPrereq)==mPrereq )((void) (0));
143713 *pbIn = 0;
143714 pNew->prereq = mPrereq;
143715
143716 /* Set the usable flag on the subset of constraints identified by
143717 ** arguments mUsable and mExclude. */
143718 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
143719 for(i=0; i<nConstraint; i++, pIdxCons++){
143720 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
143721 pIdxCons->usable = 0;
143722 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
143723 && (pTerm->eOperator & mExclude)==0
143724 ){
143725 pIdxCons->usable = 1;
143726 }
143727 }
143728
143729 /* Initialize the output fields of the sqlite3_index_info structure */
143730 memset(pUsage, 0, sizeof(pUsage[0])*nConstraint);
143731 assert( pIdxInfo->needToFreeIdxStr==0 )((void) (0));
143732 pIdxInfo->idxStr = 0;
143733 pIdxInfo->idxNum = 0;
143734 pIdxInfo->orderByConsumed = 0;
143735 pIdxInfo->estimatedCost = SQLITE_BIG_DBL(1e99) / (double)2;
143736 pIdxInfo->estimatedRows = 25;
143737 pIdxInfo->idxFlags = 0;
143738 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
143739
143740 /* Invoke the virtual table xBestIndex() method */
143741 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
143742 if( rc ){
143743 if( rc==SQLITE_CONSTRAINT19 ){
143744 /* If the xBestIndex method returns SQLITE_CONSTRAINT, that means
143745 ** that the particular combination of parameters provided is unusable.
143746 ** Make no entries in the loop table.
143747 */
143748 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
143749 return SQLITE_OK0;
143750 }
143751 return rc;
143752 }
143753
143754 mxTerm = -1;
143755 assert( pNew->nLSlot>=nConstraint )((void) (0));
143756 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
143757 pNew->u.vtab.omitMask = 0;
143758 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
143759 for(i=0; i<nConstraint; i++, pIdxCons++){
143760 int iTerm;
143761 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
143762 WhereTerm *pTerm;
143763 int j = pIdxCons->iTermOffset;
143764 if( iTerm>=nConstraint
143765 || j<0
143766 || j>=pWC->nTerm
143767 || pNew->aLTerm[iTerm]!=0
143768 || pIdxCons->usable==0
143769 ){
143770 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
143771 testcase( pIdxInfo->needToFreeIdxStr );
143772 return SQLITE_ERROR1;
143773 }
143774 testcase( iTerm==nConstraint-1 );
143775 testcase( j==0 );
143776 testcase( j==pWC->nTerm-1 );
143777 pTerm = &pWC->a[j];
143778 pNew->prereq |= pTerm->prereqRight;
143779 assert( iTerm<pNew->nLSlot )((void) (0));
143780 pNew->aLTerm[iTerm] = pTerm;
143781 if( iTerm>mxTerm ) mxTerm = iTerm;
143782 testcase( iTerm==15 );
143783 testcase( iTerm==16 );
143784 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
143785 if( (pTerm->eOperator & WO_IN0x0001)!=0 ){
143786 /* A virtual table that is constrained by an IN clause may not
143787 ** consume the ORDER BY clause because (1) the order of IN terms
143788 ** is not necessarily related to the order of output terms and
143789 ** (2) Multiple outputs from a single IN value will not merge
143790 ** together. */
143791 pIdxInfo->orderByConsumed = 0;
143792 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE1;
143793 *pbIn = 1; assert( (mExclude & WO_IN)==0 )((void) (0));
143794 }
143795 }
143796 }
143797 pNew->u.vtab.omitMask &= ~mNoOmit;
143798
143799 pNew->nLTerm = mxTerm+1;
143800 for(i=0; i<=mxTerm; i++){
143801 if( pNew->aLTerm[i]==0 ){
143802 /* The non-zero argvIdx values must be contiguous. Raise an
143803 ** error if they are not */
143804 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
143805 testcase( pIdxInfo->needToFreeIdxStr );
143806 return SQLITE_ERROR1;
143807 }
143808 }
143809 assert( pNew->nLTerm<=pNew->nLSlot )((void) (0));
143810 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
143811 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
143812 pIdxInfo->needToFreeIdxStr = 0;
143813 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
143814 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
143815 pIdxInfo->nOrderBy : 0);
143816 pNew->rSetup = 0;
143817 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
143818 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
143819
143820 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated
143821 ** that the scan will visit at most one row. Clear it otherwise. */
143822 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE1 ){
143823 pNew->wsFlags |= WHERE_ONEROW0x00001000;
143824 }else{
143825 pNew->wsFlags &= ~WHERE_ONEROW0x00001000;
143826 }
143827 rc = whereLoopInsert(pBuilder, pNew);
143828 if( pNew->u.vtab.needFree ){
143829 sqlite3_free(pNew->u.vtab.idxStr);
143830 pNew->u.vtab.needFree = 0;
143831 }
143832 WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n",
143833 *pbIn, (sqlite3_uint64)mPrereq,
143834 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
143835
143836 return rc;
143837}
143838
143839/*
143840** If this function is invoked from within an xBestIndex() callback, it
143841** returns a pointer to a buffer containing the name of the collation
143842** sequence associated with element iCons of the sqlite3_index_info.aConstraint
143843** array. Or, if iCons is out of range or there is no active xBestIndex
143844** call, return NULL.
143845*/
143846SQLITE_API const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){
143847 HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
143848 const char *zRet = 0;
143849 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
143850 CollSeq *pC = 0;
143851 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
143852 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
143853 if( pX->pLeft ){
143854 pC = sqlite3BinaryCompareCollSeq(pHidden->pParse, pX->pLeft, pX->pRight);
143855 }
143856 zRet = (pC ? pC->zName : sqlite3StrBINARY);
143857 }
143858 return zRet;
143859}
143860
143861/*
143862** Add all WhereLoop objects for a table of the join identified by
143863** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
143864**
143865** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and
143866** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause
143867** entries that occur before the virtual table in the FROM clause and are
143868** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
143869** mUnusable mask contains all FROM clause entries that occur after the
143870** virtual table and are separated from it by at least one LEFT or
143871** CROSS JOIN.
143872**
143873** For example, if the query were:
143874**
143875** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
143876**
143877** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6).
143878**
143879** All the tables in mPrereq must be scanned before the current virtual
143880** table. So any terms for which all prerequisites are satisfied by
143881** mPrereq may be specified as "usable" in all calls to xBestIndex.
143882** Conversely, all tables in mUnusable must be scanned after the current
143883** virtual table, so any terms for which the prerequisites overlap with
143884** mUnusable should always be configured as "not-usable" for xBestIndex.
143885*/
143886static int whereLoopAddVirtual(
143887 WhereLoopBuilder *pBuilder, /* WHERE clause information */
143888 Bitmask mPrereq, /* Tables that must be scanned before this one */
143889 Bitmask mUnusable /* Tables that must be scanned after this one */
143890){
143891 int rc = SQLITE_OK0; /* Return code */
143892 WhereInfo *pWInfo; /* WHERE analysis context */
143893 Parse *pParse; /* The parsing context */
143894 WhereClause *pWC; /* The WHERE clause */
143895 struct SrcList_item *pSrc; /* The FROM clause term to search */
143896 sqlite3_index_info *p; /* Object to pass to xBestIndex() */
143897 int nConstraint; /* Number of constraints in p */
143898 int bIn; /* True if plan uses IN(...) operator */
143899 WhereLoop *pNew;
143900 Bitmask mBest; /* Tables used by best possible plan */
143901 u16 mNoOmit;
143902
143903 assert( (mPrereq & mUnusable)==0 )((void) (0));
143904 pWInfo = pBuilder->pWInfo;
143905 pParse = pWInfo->pParse;
143906 pWC = pBuilder->pWC;
143907 pNew = pBuilder->pNew;
143908 pSrc = &pWInfo->pTabList->a[pNew->iTab];
143909 assert( IsVirtual(pSrc->pTab) )((void) (0));
143910 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy,
143911 &mNoOmit);
143912 if( p==0 ) return SQLITE_NOMEM_BKPT7;
143913 pNew->rSetup = 0;
143914 pNew->wsFlags = WHERE_VIRTUALTABLE0x00000400;
143915 pNew->nLTerm = 0;
143916 pNew->u.vtab.needFree = 0;
143917 nConstraint = p->nConstraint;
143918 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
143919 sqlite3DbFree(pParse->db, p);
143920 return SQLITE_NOMEM_BKPT7;
143921 }
143922
143923 /* First call xBestIndex() with all constraints usable. */
143924 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
143925 WHERETRACE(0x40, (" VirtualOne: all usable\n"));
143926 rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS((Bitmask)-1), 0, p, mNoOmit, &bIn);
143927
143928 /* If the call to xBestIndex() with all terms enabled produced a plan
143929 ** that does not require any source tables (IOW: a plan with mBest==0)
143930 ** and does not use an IN(...) operator, then there is no point in making
143931 ** any further calls to xBestIndex() since they will all return the same
143932 ** result (if the xBestIndex() implementation is sane). */
143933 if( rc==SQLITE_OK0 && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
143934 int seenZero = 0; /* True if a plan with no prereqs seen */
143935 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */
143936 Bitmask mPrev = 0;
143937 Bitmask mBestNoIn = 0;
143938
143939 /* If the plan produced by the earlier call uses an IN(...) term, call
143940 ** xBestIndex again, this time with IN(...) terms disabled. */
143941 if( bIn ){
143942 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n"));
143943 rc = whereLoopAddVirtualOne(
143944 pBuilder, mPrereq, ALLBITS((Bitmask)-1), WO_IN0x0001, p, mNoOmit, &bIn);
143945 assert( bIn==0 )((void) (0));
143946 mBestNoIn = pNew->prereq & ~mPrereq;
143947 if( mBestNoIn==0 ){
143948 seenZero = 1;
143949 seenZeroNoIN = 1;
143950 }
143951 }
143952
143953 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq)
143954 ** in the set of terms that apply to the current virtual table. */
143955 while( rc==SQLITE_OK0 ){
143956 int i;
143957 Bitmask mNext = ALLBITS((Bitmask)-1);
143958 assert( mNext>0 )((void) (0));
143959 for(i=0; i<nConstraint; i++){
143960 Bitmask mThis = (
143961 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
143962 );
143963 if( mThis>mPrev && mThis<mNext ) mNext = mThis;
143964 }
143965 mPrev = mNext;
143966 if( mNext==ALLBITS((Bitmask)-1) ) break;
143967 if( mNext==mBest || mNext==mBestNoIn ) continue;
143968 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n",
143969 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext));
143970 rc = whereLoopAddVirtualOne(
143971 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn);
143972 if( pNew->prereq==mPrereq ){
143973 seenZero = 1;
143974 if( bIn==0 ) seenZeroNoIN = 1;
143975 }
143976 }
143977
143978 /* If the calls to xBestIndex() in the above loop did not find a plan
143979 ** that requires no source tables at all (i.e. one guaranteed to be
143980 ** usable), make a call here with all source tables disabled */
143981 if( rc==SQLITE_OK0 && seenZero==0 ){
143982 WHERETRACE(0x40, (" VirtualOne: all disabled\n"));
143983 rc = whereLoopAddVirtualOne(
143984 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn);
143985 if( bIn==0 ) seenZeroNoIN = 1;
143986 }
143987
143988 /* If the calls to xBestIndex() have so far failed to find a plan
143989 ** that requires no source tables at all and does not use an IN(...)
143990 ** operator, make a final call to obtain one here. */
143991 if( rc==SQLITE_OK0 && seenZeroNoIN==0 ){
143992 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n"));
143993 rc = whereLoopAddVirtualOne(
143994 pBuilder, mPrereq, mPrereq, WO_IN0x0001, p, mNoOmit, &bIn);
143995 }
143996 }
143997
143998 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
143999 sqlite3DbFreeNN(pParse->db, p);
144000 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
144001 return rc;
144002}
144003#endif /* SQLITE_OMIT_VIRTUALTABLE */
144004
144005/*
144006** Add WhereLoop entries to handle OR terms. This works for either
144007** btrees or virtual tables.
144008*/
144009static int whereLoopAddOr(
144010 WhereLoopBuilder *pBuilder,
144011 Bitmask mPrereq,
144012 Bitmask mUnusable
144013){
144014 WhereInfo *pWInfo = pBuilder->pWInfo;
144015 WhereClause *pWC;
144016 WhereLoop *pNew;
144017 WhereTerm *pTerm, *pWCEnd;
144018 int rc = SQLITE_OK0;
144019 int iCur;
144020 WhereClause tempWC;
144021 WhereLoopBuilder sSubBuild;
144022 WhereOrSet sSum, sCur;
144023 struct SrcList_item *pItem;
144024
144025 pWC = pBuilder->pWC;
144026 pWCEnd = pWC->a + pWC->nTerm;
144027 pNew = pBuilder->pNew;
144028 memset(&sSum, 0, sizeof(sSum));
144029 pItem = pWInfo->pTabList->a + pNew->iTab;
144030 iCur = pItem->iCursor;
144031
144032 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK0; pTerm++){
144033 if( (pTerm->eOperator & WO_OR0x0200)!=0
144034 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
144035 ){
144036 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
144037 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
144038 WhereTerm *pOrTerm;
144039 int once = 1;
144040 int i, j;
144041
144042 sSubBuild = *pBuilder;
144043 sSubBuild.pOrderBy = 0;
144044 sSubBuild.pOrSet = &sCur;
144045
144046 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
144047 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
144048 if( (pOrTerm->eOperator & WO_AND0x0400)!=0 ){
144049 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
144050 }else if( pOrTerm->leftCursor==iCur ){
144051 tempWC.pWInfo = pWC->pWInfo;
144052 tempWC.pOuter = pWC;
144053 tempWC.op = TK_AND44;
144054 tempWC.nTerm = 1;
144055 tempWC.a = pOrTerm;
144056 sSubBuild.pWC = &tempWC;
144057 }else{
144058 continue;
144059 }
144060 sCur.n = 0;
144061#ifdef WHERETRACE_ENABLED
144062 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
144063 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
144064 if( sqlite3WhereTrace & 0x400 ){
144065 sqlite3WhereClausePrint(sSubBuild.pWC);
144066 }
144067#endif
144068#ifndef SQLITE_OMIT_VIRTUALTABLE
144069 if( IsVirtual(pItem->pTab)((pItem->pTab)->nModuleArg) ){
144070 rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable);
144071 }else
144072#endif
144073 {
144074 rc = whereLoopAddBtree(&sSubBuild, mPrereq);
144075 }
144076 if( rc==SQLITE_OK0 ){
144077 rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable);
144078 }
144079 assert( rc==SQLITE_OK || sCur.n==0 )((void) (0));
144080 if( sCur.n==0 ){
144081 sSum.n = 0;
144082 break;
144083 }else if( once ){
144084 whereOrMove(&sSum, &sCur);
144085 once = 0;
144086 }else{
144087 WhereOrSet sPrev;
144088 whereOrMove(&sPrev, &sSum);
144089 sSum.n = 0;
144090 for(i=0; i<sPrev.n; i++){
144091 for(j=0; j<sCur.n; j++){
144092 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
144093 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
144094 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
144095 }
144096 }
144097 }
144098 }
144099 pNew->nLTerm = 1;
144100 pNew->aLTerm[0] = pTerm;
144101 pNew->wsFlags = WHERE_MULTI_OR0x00002000;
144102 pNew->rSetup = 0;
144103 pNew->iSortIdx = 0;
144104 memset(&pNew->u, 0, sizeof(pNew->u));
144105 for(i=0; rc==SQLITE_OK0 && i<sSum.n; i++){
144106 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
144107 ** of all sub-scans required by the OR-scan. However, due to rounding
144108 ** errors, it may be that the cost of the OR-scan is equal to its
144109 ** most expensive sub-scan. Add the smallest possible penalty
144110 ** (equivalent to multiplying the cost by 1.07) to ensure that
144111 ** this does not happen. Otherwise, for WHERE clauses such as the
144112 ** following where there is an index on "y":
144113 **
144114 ** WHERE likelihood(x=?, 0.99) OR y=?
144115 **
144116 ** the planner may elect to "OR" together a full-table scan and an
144117 ** index lookup. And other similarly odd results. */
144118 pNew->rRun = sSum.a[i].rRun + 1;
144119 pNew->nOut = sSum.a[i].nOut;
144120 pNew->prereq = sSum.a[i].prereq;
144121 rc = whereLoopInsert(pBuilder, pNew);
144122 }
144123 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
144124 }
144125 }
144126 return rc;
144127}
144128
144129/*
144130** Add all WhereLoop objects for all tables
144131*/
144132static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
144133 WhereInfo *pWInfo = pBuilder->pWInfo;
144134 Bitmask mPrereq = 0;
144135 Bitmask mPrior = 0;
144136 int iTab;
144137 SrcList *pTabList = pWInfo->pTabList;
144138 struct SrcList_item *pItem;
144139 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
144140 sqlite3 *db = pWInfo->pParse->db;
144141 int rc = SQLITE_OK0;
144142 WhereLoop *pNew;
144143 u8 priorJointype = 0;
144144
144145 /* Loop over the tables in the join, from left to right */
144146 pNew = pBuilder->pNew;
144147 whereLoopInit(pNew);
144148 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT20000;
144149 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
144150 Bitmask mUnusable = 0;
144151 pNew->iTab = iTab;
144152 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR1000;
144153 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
144154 if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT0x0008|JT_CROSS0x0002))!=0 ){
144155 /* This condition is true when pItem is the FROM clause term on the
144156 ** right-hand-side of a LEFT or CROSS JOIN. */
144157 mPrereq = mPrior;
144158 }
144159 priorJointype = pItem->fg.jointype;
144160#ifndef SQLITE_OMIT_VIRTUALTABLE
144161 if( IsVirtual(pItem->pTab)((pItem->pTab)->nModuleArg) ){
144162 struct SrcList_item *p;
144163 for(p=&pItem[1]; p<pEnd; p++){
144164 if( mUnusable || (p->fg.jointype & (JT_LEFT0x0008|JT_CROSS0x0002)) ){
144165 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
144166 }
144167 }
144168 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable);
144169 }else
144170#endif /* SQLITE_OMIT_VIRTUALTABLE */
144171 {
144172 rc = whereLoopAddBtree(pBuilder, mPrereq);
144173 }
144174 if( rc==SQLITE_OK0 && pBuilder->pWC->hasOr ){
144175 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable);
144176 }
144177 mPrior |= pNew->maskSelf;
144178 if( rc || db->mallocFailed ){
144179 if( rc==SQLITE_DONE101 ){
144180 /* We hit the query planner search limit set by iPlanLimit */
144181 sqlite3_log(SQLITE_WARNING28, "abbreviated query algorithm search");
144182 rc = SQLITE_OK0;
144183 }else{
144184 break;
144185 }
144186 }
144187 }
144188
144189 whereLoopClear(db, pNew);
144190 return rc;
144191}
144192
144193/*
144194** Examine a WherePath (with the addition of the extra WhereLoop of the 6th
144195** parameters) to see if it outputs rows in the requested ORDER BY
144196** (or GROUP BY) without requiring a separate sort operation. Return N:
144197**
144198** N>0: N terms of the ORDER BY clause are satisfied
144199** N==0: No terms of the ORDER BY clause are satisfied
144200** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
144201**
144202** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
144203** strict. With GROUP BY and DISTINCT the only requirement is that
144204** equivalent rows appear immediately adjacent to one another. GROUP BY
144205** and DISTINCT do not require rows to appear in any particular order as long
144206** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT
144207** the pOrderBy terms can be matched in any order. With ORDER BY, the
144208** pOrderBy terms must be matched in strict left-to-right order.
144209*/
144210static i8 wherePathSatisfiesOrderBy(
144211 WhereInfo *pWInfo, /* The WHERE clause */
144212 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */
144213 WherePath *pPath, /* The WherePath to check */
144214 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */
144215 u16 nLoop, /* Number of entries in pPath->aLoop[] */
144216 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
144217 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */
144218){
144219 u8 revSet; /* True if rev is known */
144220 u8 rev; /* Composite sort order */
144221 u8 revIdx; /* Index sort order */
144222 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
144223 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */
144224 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */
144225 u16 eqOpMask; /* Allowed equality operators */
144226 u16 nKeyCol; /* Number of key columns in pIndex */
144227 u16 nColumn; /* Total number of ordered columns in the index */
144228 u16 nOrderBy; /* Number terms in the ORDER BY clause */
144229 int iLoop; /* Index of WhereLoop in pPath being processed */
144230 int i, j; /* Loop counters */
144231 int iCur; /* Cursor number for current WhereLoop */
144232 int iColumn; /* A column number within table iCur */
144233 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
144234 WhereTerm *pTerm; /* A single term of the WHERE clause */
144235 Expr *pOBExpr; /* An expression from the ORDER BY clause */
144236 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */
144237 Index *pIndex; /* The index associated with pLoop */
144238 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
144239 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */
144240 Bitmask obDone; /* Mask of all ORDER BY terms */
144241 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
144242 Bitmask ready; /* Mask of inner loops */
144243
144244 /*
144245 ** We say the WhereLoop is "one-row" if it generates no more than one
144246 ** row of output. A WhereLoop is one-row if all of the following are true:
144247 ** (a) All index columns match with WHERE_COLUMN_EQ.
144248 ** (b) The index is unique
144249 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
144250 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
144251 **
144252 ** We say the WhereLoop is "order-distinct" if the set of columns from
144253 ** that WhereLoop that are in the ORDER BY clause are different for every
144254 ** row of the WhereLoop. Every one-row WhereLoop is automatically
144255 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
144256 ** is not order-distinct. To be order-distinct is not quite the same as being
144257 ** UNIQUE since a UNIQUE column or index can have multiple rows that
144258 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
144259 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
144260 **
144261 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
144262 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
144263 ** automatically order-distinct.
144264 */
144265
144266 assert( pOrderBy!=0 )((void) (0));
144267 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin)(((db)->dbOptFlags&(0x0040))!=0) ) return 0;
144268
144269 nOrderBy = pOrderBy->nExpr;
144270 testcase( nOrderBy==BMS-1 );
144271 if( nOrderBy>BMS((int)(sizeof(Bitmask)*8))-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
144272 isOrderDistinct = 1;
144273 obDone = MASKBIT(nOrderBy)(((Bitmask)1)<<(nOrderBy))-1;
144274 orderDistinctMask = 0;
144275 ready = 0;
144276 eqOpMask = WO_EQ0x0002 | WO_IS0x0080 | WO_ISNULL0x0100;
144277 if( wctrlFlags & WHERE_ORDERBY_LIMIT0x0800 ) eqOpMask |= WO_IN0x0001;
144278 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
144279 if( iLoop>0 ) ready |= pLoop->maskSelf;
144280 if( iLoop<nLoop ){
144281 pLoop = pPath->aLoop[iLoop];
144282 if( wctrlFlags & WHERE_ORDERBY_LIMIT0x0800 ) continue;
144283 }else{
144284 pLoop = pLast;
144285 }
144286 if( pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400 ){
144287 if( pLoop->u.vtab.isOrdered ) obSat = obDone;
144288 break;
144289 }else{
144290 pLoop->u.btree.nIdxCol = 0;
144291 }
144292 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
144293
144294 /* Mark off any ORDER BY term X that is a column in the table of
144295 ** the current loop for which there is term in the WHERE
144296 ** clause of the form X IS NULL or X=? that reference only outer
144297 ** loops.
144298 */
144299 for(i=0; i<nOrderBy; i++){
144300 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
144301 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
144302 if( pOBExpr->op!=TK_COLUMN162 ) continue;
144303 if( pOBExpr->iTable!=iCur ) continue;
144304 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
144305 ~ready, eqOpMask, 0);
144306 if( pTerm==0 ) continue;
144307 if( pTerm->eOperator==WO_IN0x0001 ){
144308 /* IN terms are only valid for sorting in the ORDER BY LIMIT
144309 ** optimization, and then only if they are actually used
144310 ** by the query plan */
144311 assert( wctrlFlags & WHERE_ORDERBY_LIMIT )((void) (0));
144312 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
144313 if( j>=pLoop->nLTerm ) continue;
144314 }
144315 if( (pTerm->eOperator&(WO_EQ0x0002|WO_IS0x0080))!=0 && pOBExpr->iColumn>=0 ){
144316 if( sqlite3ExprCollSeqMatch(pWInfo->pParse,
144317 pOrderBy->a[i].pExpr, pTerm->pExpr)==0 ){
144318 continue;
144319 }
144320 testcase( pTerm->pExpr->op==TK_IS );
144321 }
144322 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
144323 }
144324
144325 if( (pLoop->wsFlags & WHERE_ONEROW0x00001000)==0 ){
144326 if( pLoop->wsFlags & WHERE_IPK0x00000100 ){
144327 pIndex = 0;
144328 nKeyCol = 0;
144329 nColumn = 1;
144330 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
144331 return 0;
144332 }else{
144333 nKeyCol = pIndex->nKeyCol;
144334 nColumn = pIndex->nColumn;
144335 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) )((void) (0));
144336 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID((void) (0))
144337 || !HasRowid(pIndex->pTable))((void) (0));
144338 isOrderDistinct = IsUniqueIndex(pIndex)((pIndex)->onError!=0);
144339 }
144340
144341 /* Loop through all columns of the index and deal with the ones
144342 ** that are not constrained by == or IN.
144343 */
144344 rev = revSet = 0;
144345 distinctColumns = 0;
144346 for(j=0; j<nColumn; j++){
144347 u8 bOnce = 1; /* True to run the ORDER BY search loop */
144348
144349 assert( j>=pLoop->u.btree.nEq((void) (0))
144350 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)((void) (0))
144351 )((void) (0));
144352 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
144353 u16 eOp = pLoop->aLTerm[j]->eOperator;
144354
144355 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when
144356 ** doing WHERE_ORDERBY_LIMIT processing).
144357 **
144358 ** If the current term is a column of an ((?,?) IN (SELECT...))
144359 ** expression for which the SELECT returns more than one column,
144360 ** check that it is the only column used by this loop. Otherwise,
144361 ** if it is one of two or more, none of the columns can be
144362 ** considered to match an ORDER BY term. */
144363 if( (eOp & eqOpMask)!=0 ){
144364 if( eOp & WO_ISNULL0x0100 ){
144365 testcase( isOrderDistinct );
144366 isOrderDistinct = 0;
144367 }
144368 continue;
144369 }else if( ALWAYS(eOp & WO_IN)(eOp & 0x0001) ){
144370 /* ALWAYS() justification: eOp is an equality operator due to the
144371 ** j<pLoop->u.btree.nEq constraint above. Any equality other
144372 ** than WO_IN is captured by the previous "if". So this one
144373 ** always has to be WO_IN. */
144374 Expr *pX = pLoop->aLTerm[j]->pExpr;
144375 for(i=j+1; i<pLoop->u.btree.nEq; i++){
144376 if( pLoop->aLTerm[i]->pExpr==pX ){
144377 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) )((void) (0));
144378 bOnce = 0;
144379 break;
144380 }
144381 }
144382 }
144383 }
144384
144385 /* Get the column number in the table (iColumn) and sort order
144386 ** (revIdx) for the j-th column of the index.
144387 */
144388 if( pIndex ){
144389 iColumn = pIndex->aiColumn[j];
144390 revIdx = pIndex->aSortOrder[j];
144391 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID(-1);
144392 }else{
144393 iColumn = XN_ROWID(-1);
144394 revIdx = 0;
144395 }
144396
144397 /* An unconstrained column that might be NULL means that this
144398 ** WhereLoop is not well-ordered
144399 */
144400 if( isOrderDistinct
144401 && iColumn>=0
144402 && j>=pLoop->u.btree.nEq
144403 && pIndex->pTable->aCol[iColumn].notNull==0
144404 ){
144405 isOrderDistinct = 0;
144406 }
144407
144408 /* Find the ORDER BY term that corresponds to the j-th column
144409 ** of the index and mark that ORDER BY term off
144410 */
144411 isMatch = 0;
144412 for(i=0; bOnce && i<nOrderBy; i++){
144413 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
144414 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
144415 testcase( wctrlFlags & WHERE_GROUPBY );
144416 testcase( wctrlFlags & WHERE_DISTINCTBY );
144417 if( (wctrlFlags & (WHERE_GROUPBY0x0040|WHERE_DISTINCTBY0x0080))==0 ) bOnce = 0;
144418 if( iColumn>=XN_ROWID(-1) ){
144419 if( pOBExpr->op!=TK_COLUMN162 ) continue;
144420 if( pOBExpr->iTable!=iCur ) continue;
144421 if( pOBExpr->iColumn!=iColumn ) continue;
144422 }else{
144423 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
144424 if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
144425 continue;
144426 }
144427 }
144428 if( iColumn!=XN_ROWID(-1) ){
144429 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
144430 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
144431 }
144432 pLoop->u.btree.nIdxCol = j+1;
144433 isMatch = 1;
144434 break;
144435 }
144436 if( isMatch && (wctrlFlags & WHERE_GROUPBY0x0040)==0 ){
144437 /* Make sure the sort order is compatible in an ORDER BY clause.
144438 ** Sort order is irrelevant for a GROUP BY clause. */
144439 if( revSet ){
144440 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0;
144441 }else{
144442 rev = revIdx ^ pOrderBy->a[i].sortOrder;
144443 if( rev ) *pRevMask |= MASKBIT(iLoop)(((Bitmask)1)<<(iLoop));
144444 revSet = 1;
144445 }
144446 }
144447 if( isMatch ){
144448 if( iColumn==XN_ROWID(-1) ){
144449 testcase( distinctColumns==0 );
144450 distinctColumns = 1;
144451 }
144452 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
144453 }else{
144454 /* No match found */
144455 if( j==0 || j<nKeyCol ){
144456 testcase( isOrderDistinct!=0 );
144457 isOrderDistinct = 0;
144458 }
144459 break;
144460 }
144461 } /* end Loop over all index columns */
144462 if( distinctColumns ){
144463 testcase( isOrderDistinct==0 );
144464 isOrderDistinct = 1;
144465 }
144466 } /* end-if not one-row */
144467
144468 /* Mark off any other ORDER BY terms that reference pLoop */
144469 if( isOrderDistinct ){
144470 orderDistinctMask |= pLoop->maskSelf;
144471 for(i=0; i<nOrderBy; i++){
144472 Expr *p;
144473 Bitmask mTerm;
144474 if( MASKBIT(i)(((Bitmask)1)<<(i)) & obSat ) continue;
144475 p = pOrderBy->a[i].pExpr;
144476 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
144477 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
144478 if( (mTerm&~orderDistinctMask)==0 ){
144479 obSat |= MASKBIT(i)(((Bitmask)1)<<(i));
144480 }
144481 }
144482 }
144483 } /* End the loop over all WhereLoops from outer-most down to inner-most */
144484 if( obSat==obDone ) return (i8)nOrderBy;
144485 if( !isOrderDistinct ){
144486 for(i=nOrderBy-1; i>0; i--){
144487 Bitmask m = MASKBIT(i)(((Bitmask)1)<<(i)) - 1;
144488 if( (obSat&m)==m ) return i;
144489 }
144490 return 0;
144491 }
144492 return -1;
144493}
144494
144495
144496/*
144497** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
144498** the planner assumes that the specified pOrderBy list is actually a GROUP
144499** BY clause - and so any order that groups rows as required satisfies the
144500** request.
144501**
144502** Normally, in this case it is not possible for the caller to determine
144503** whether or not the rows are really being delivered in sorted order, or
144504** just in some other order that provides the required grouping. However,
144505** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
144506** this function may be called on the returned WhereInfo object. It returns
144507** true if the rows really will be sorted in the specified order, or false
144508** otherwise.
144509**
144510** For example, assuming:
144511**
144512** CREATE INDEX i1 ON t1(x, Y);
144513**
144514** then
144515**
144516** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
144517** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
144518*/
144519SQLITE_PRIVATEstatic int sqlite3WhereIsSorted(WhereInfo *pWInfo){
144520 assert( pWInfo->wctrlFlags & WHERE_GROUPBY )((void) (0));
144521 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP )((void) (0));
144522 return pWInfo->sorted;
144523}
144524
144525#ifdef WHERETRACE_ENABLED
144526/* For debugging use only: */
144527static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
144528 static char zName[65];
144529 int i;
144530 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
144531 if( pLast ) zName[i++] = pLast->cId;
144532 zName[i] = 0;
144533 return zName;
144534}
144535#endif
144536
144537/*
144538** Return the cost of sorting nRow rows, assuming that the keys have
144539** nOrderby columns and that the first nSorted columns are already in
144540** order.
144541*/
144542static LogEst whereSortingCost(
144543 WhereInfo *pWInfo,
144544 LogEst nRow,
144545 int nOrderBy,
144546 int nSorted
144547){
144548 /* TUNING: Estimated cost of a full external sort, where N is
144549 ** the number of rows to sort is:
144550 **
144551 ** cost = (3.0 * N * log(N)).
144552 **
144553 ** Or, if the order-by clause has X terms but only the last Y
144554 ** terms are out of order, then block-sorting will reduce the
144555 ** sorting cost to:
144556 **
144557 ** cost = (3.0 * N * log(N)) * (Y/X)
144558 **
144559 ** The (Y/X) term is implemented using stack variable rScale
144560 ** below. */
144561 LogEst rScale, rSortCost;
144562 assert( nOrderBy>0 && 66==sqlite3LogEst(100) )((void) (0));
144563 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
144564 rSortCost = nRow + rScale + 16;
144565
144566 /* Multiple by log(M) where M is the number of output rows.
144567 ** Use the LIMIT for M if it is smaller */
144568 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT0x4000)!=0 && pWInfo->iLimit<nRow ){
144569 nRow = pWInfo->iLimit;
144570 }
144571 rSortCost += estLog(nRow);
144572 return rSortCost;
144573}
144574
144575/*
144576** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
144577** attempts to find the lowest cost path that visits each WhereLoop
144578** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
144579**
144580** Assume that the total number of output rows that will need to be sorted
144581** will be nRowEst (in the 10*log2 representation). Or, ignore sorting
144582** costs if nRowEst==0.
144583**
144584** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
144585** error occurs.
144586*/
144587static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
144588 int mxChoice; /* Maximum number of simultaneous paths tracked */
144589 int nLoop; /* Number of terms in the join */
144590 Parse *pParse; /* Parsing context */
144591 sqlite3 *db; /* The database connection */
144592 int iLoop; /* Loop counter over the terms of the join */
144593 int ii, jj; /* Loop counters */
144594 int mxI = 0; /* Index of next entry to replace */
144595 int nOrderBy; /* Number of ORDER BY clause terms */
144596 LogEst mxCost = 0; /* Maximum cost of a set of paths */
144597 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */
144598 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
144599 WherePath *aFrom; /* All nFrom paths at the previous level */
144600 WherePath *aTo; /* The nTo best paths at the current level */
144601 WherePath *pFrom; /* An element of aFrom[] that we are working on */
144602 WherePath *pTo; /* An element of aTo[] that we are working on */
144603 WhereLoop *pWLoop; /* One of the WhereLoop objects */
144604 WhereLoop **pX; /* Used to divy up the pSpace memory */
144605 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */
144606 char *pSpace; /* Temporary memory used by this routine */
144607 int nSpace; /* Bytes of space allocated at pSpace */
144608
144609 pParse = pWInfo->pParse;
144610 db = pParse->db;
144611 nLoop = pWInfo->nLevel;
144612 /* TUNING: For simple queries, only the best path is tracked.
144613 ** For 2-way joins, the 5 best paths are followed.
144614 ** For joins of 3 or more tables, track the 10 best paths */
144615 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
144616 assert( nLoop<=pWInfo->pTabList->nSrc )((void) (0));
144617 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
144618
144619 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this
144620 ** case the purpose of this call is to estimate the number of rows returned
144621 ** by the overall query. Once this estimate has been obtained, the caller
144622 ** will invoke this function a second time, passing the estimate as the
144623 ** nRowEst parameter. */
144624 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
144625 nOrderBy = 0;
144626 }else{
144627 nOrderBy = pWInfo->pOrderBy->nExpr;
144628 }
144629
144630 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
144631 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
144632 nSpace += sizeof(LogEst) * nOrderBy;
144633 pSpace = sqlite3DbMallocRawNN(db, nSpace);
144634 if( pSpace==0 ) return SQLITE_NOMEM_BKPT7;
144635 aTo = (WherePath*)pSpace;
144636 aFrom = aTo+mxChoice;
144637 memset(aFrom, 0, sizeof(aFrom[0]));
144638 pX = (WhereLoop**)(aFrom+mxChoice);
144639 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
144640 pFrom->aLoop = pX;
144641 }
144642 if( nOrderBy ){
144643 /* If there is an ORDER BY clause and it is not being ignored, set up
144644 ** space for the aSortCost[] array. Each element of the aSortCost array
144645 ** is either zero - meaning it has not yet been initialized - or the
144646 ** cost of sorting nRowEst rows of data where the first X terms of
144647 ** the ORDER BY clause are already in order, where X is the array
144648 ** index. */
144649 aSortCost = (LogEst*)pX;
144650 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy);
144651 }
144652 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] )((void) (0));
144653 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX )((void) (0));
144654
144655 /* Seed the search with a single WherePath containing zero WhereLoops.
144656 **
144657 ** TUNING: Do not let the number of iterations go above 28. If the cost
144658 ** of computing an automatic index is not paid back within the first 28
144659 ** rows, then do not use the automatic index. */
144660 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48)((pParse->nQueryLoop)<(48)?(pParse->nQueryLoop):(48)
)
; assert( 48==sqlite3LogEst(28) )((void) (0));
144661 nFrom = 1;
144662 assert( aFrom[0].isOrdered==0 )((void) (0));
144663 if( nOrderBy ){
144664 /* If nLoop is zero, then there are no FROM terms in the query. Since
144665 ** in this case the query may return a maximum of one row, the results
144666 ** are already in the requested order. Set isOrdered to nOrderBy to
144667 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to
144668 ** -1, indicating that the result set may or may not be ordered,
144669 ** depending on the loops added to the current plan. */
144670 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
144671 }
144672
144673 /* Compute successively longer WherePaths using the previous generation
144674 ** of WherePaths as the basis for the next. Keep track of the mxChoice
144675 ** best paths at each generation */
144676 for(iLoop=0; iLoop<nLoop; iLoop++){
144677 nTo = 0;
144678 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
144679 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
144680 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */
144681 LogEst rCost; /* Cost of path (pFrom+pWLoop) */
144682 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */
144683 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
144684 Bitmask maskNew; /* Mask of src visited by (..) */
144685 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
144686
144687 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
144688 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
144689 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 && pFrom->nRow<3 ){
144690 /* Do not use an automatic index if the this loop is expected
144691 ** to run less than 1.25 times. It is tempting to also exclude
144692 ** automatic index usage on an outer loop, but sometimes an automatic
144693 ** index is useful in the outer loop of a correlated subquery. */
144694 assert( 10==sqlite3LogEst(2) )((void) (0));
144695 continue;
144696 }
144697
144698 /* At this point, pWLoop is a candidate to be the next loop.
144699 ** Compute its cost */
144700 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
144701 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
144702 nOut = pFrom->nRow + pWLoop->nOut;
144703 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
144704 if( isOrdered<0 ){
144705 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
144706 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
144707 iLoop, pWLoop, &revMask);
144708 }else{
144709 revMask = pFrom->revLoop;
144710 }
144711 if( isOrdered>=0 && isOrdered<nOrderBy ){
144712 if( aSortCost[isOrdered]==0 ){
144713 aSortCost[isOrdered] = whereSortingCost(
144714 pWInfo, nRowEst, nOrderBy, isOrdered
144715 );
144716 }
144717 /* TUNING: Add a small extra penalty (5) to sorting as an
144718 ** extra encouragment to the query planner to select a plan
144719 ** where the rows emerge in the correct order without any sorting
144720 ** required. */
144721 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5;
144722
144723 WHERETRACE(0x002,
144724 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
144725 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
144726 rUnsorted, rCost));
144727 }else{
144728 rCost = rUnsorted;
144729 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
144730 }
144731
144732 /* Check to see if pWLoop should be added to the set of
144733 ** mxChoice best-so-far paths.
144734 **
144735 ** First look for an existing path among best-so-far paths
144736 ** that covers the same set of loops and has the same isOrdered
144737 ** setting as the current path candidate.
144738 **
144739 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
144740 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
144741 ** of legal values for isOrdered, -1..64.
144742 */
144743 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
144744 if( pTo->maskLoop==maskNew
144745 && ((pTo->isOrdered^isOrdered)&0x80)==0
144746 ){
144747 testcase( jj==nTo-1 );
144748 break;
144749 }
144750 }
144751 if( jj>=nTo ){
144752 /* None of the existing best-so-far paths match the candidate. */
144753 if( nTo>=mxChoice
144754 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
144755 ){
144756 /* The current candidate is no better than any of the mxChoice
144757 ** paths currently in the best-so-far buffer. So discard
144758 ** this candidate as not viable. */
144759#ifdef WHERETRACE_ENABLED /* 0x4 */
144760 if( sqlite3WhereTrace&0x4 ){
144761 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
144762 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
144763 isOrdered>=0 ? isOrdered+'0' : '?');
144764 }
144765#endif
144766 continue;
144767 }
144768 /* If we reach this points it means that the new candidate path
144769 ** needs to be added to the set of best-so-far paths. */
144770 if( nTo<mxChoice ){
144771 /* Increase the size of the aTo set by one */
144772 jj = nTo++;
144773 }else{
144774 /* New path replaces the prior worst to keep count below mxChoice */
144775 jj = mxI;
144776 }
144777 pTo = &aTo[jj];
144778#ifdef WHERETRACE_ENABLED /* 0x4 */
144779 if( sqlite3WhereTrace&0x4 ){
144780 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
144781 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
144782 isOrdered>=0 ? isOrdered+'0' : '?');
144783 }
144784#endif
144785 }else{
144786 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
144787 ** same set of loops and has the same isOrdered setting as the
144788 ** candidate path. Check to see if the candidate should replace
144789 ** pTo or if the candidate should be skipped.
144790 **
144791 ** The conditional is an expanded vector comparison equivalent to:
144792 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
144793 */
144794 if( pTo->rCost<rCost
144795 || (pTo->rCost==rCost
144796 && (pTo->nRow<nOut
144797 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
144798 )
144799 )
144800 ){
144801#ifdef WHERETRACE_ENABLED /* 0x4 */
144802 if( sqlite3WhereTrace&0x4 ){
144803 sqlite3DebugPrintf(
144804 "Skip %s cost=%-3d,%3d,%3d order=%c",
144805 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
144806 isOrdered>=0 ? isOrdered+'0' : '?');
144807 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
144808 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
144809 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
144810 }
144811#endif
144812 /* Discard the candidate path from further consideration */
144813 testcase( pTo->rCost==rCost );
144814 continue;
144815 }
144816 testcase( pTo->rCost==rCost+1 );
144817 /* Control reaches here if the candidate path is better than the
144818 ** pTo path. Replace pTo with the candidate. */
144819#ifdef WHERETRACE_ENABLED /* 0x4 */
144820 if( sqlite3WhereTrace&0x4 ){
144821 sqlite3DebugPrintf(
144822 "Update %s cost=%-3d,%3d,%3d order=%c",
144823 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
144824 isOrdered>=0 ? isOrdered+'0' : '?');
144825 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
144826 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
144827 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
144828 }
144829#endif
144830 }
144831 /* pWLoop is a winner. Add it to the set of best so far */
144832 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
144833 pTo->revLoop = revMask;
144834 pTo->nRow = nOut;
144835 pTo->rCost = rCost;
144836 pTo->rUnsorted = rUnsorted;
144837 pTo->isOrdered = isOrdered;
144838 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
144839 pTo->aLoop[iLoop] = pWLoop;
144840 if( nTo>=mxChoice ){
144841 mxI = 0;
144842 mxCost = aTo[0].rCost;
144843 mxUnsorted = aTo[0].nRow;
144844 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
144845 if( pTo->rCost>mxCost
144846 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
144847 ){
144848 mxCost = pTo->rCost;
144849 mxUnsorted = pTo->rUnsorted;
144850 mxI = jj;
144851 }
144852 }
144853 }
144854 }
144855 }
144856
144857#ifdef WHERETRACE_ENABLED /* >=2 */
144858 if( sqlite3WhereTrace & 0x02 ){
144859 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
144860 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
144861 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
144862 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
144863 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
144864 if( pTo->isOrdered>0 ){
144865 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
144866 }else{
144867 sqlite3DebugPrintf("\n");
144868 }
144869 }
144870 }
144871#endif
144872
144873 /* Swap the roles of aFrom and aTo for the next generation */
144874 pFrom = aTo;
144875 aTo = aFrom;
144876 aFrom = pFrom;
144877 nFrom = nTo;
144878 }
144879
144880 if( nFrom==0 ){
144881 sqlite3ErrorMsg(pParse, "no query solution");
144882 sqlite3DbFreeNN(db, pSpace);
144883 return SQLITE_ERROR1;
144884 }
144885
144886 /* Find the lowest cost path. pFrom will be left pointing to that path */
144887 pFrom = aFrom;
144888 for(ii=1; ii<nFrom; ii++){
144889 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
144890 }
144891 assert( pWInfo->nLevel==nLoop )((void) (0));
144892 /* Load the lowest cost path into pWInfo */
144893 for(iLoop=0; iLoop<nLoop; iLoop++){
144894 WhereLevel *pLevel = pWInfo->a + iLoop;
144895 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
144896 pLevel->iFrom = pWLoop->iTab;
144897 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
144898 }
144899 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100)!=0
144900 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080)==0
144901 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP0
144902 && nRowEst
144903 ){
144904 Bitmask notUsed;
144905 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
144906 WHERE_DISTINCTBY0x0080, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
144907 if( rc==pWInfo->pResultSet->nExpr ){
144908 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED2;
144909 }
144910 }
144911 pWInfo->bOrderedInnerLoop = 0;
144912 if( pWInfo->pOrderBy ){
144913 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY0x0080 ){
144914 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
144915 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED2;
144916 }
144917 }else{
144918 pWInfo->nOBSat = pFrom->isOrdered;
144919 pWInfo->revMask = pFrom->revLoop;
144920 if( pWInfo->nOBSat<=0 ){
144921 pWInfo->nOBSat = 0;
144922 if( nLoop>0 ){
144923 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
144924 if( (wsFlags & WHERE_ONEROW0x00001000)==0
144925 && (wsFlags&(WHERE_IPK0x00000100|WHERE_COLUMN_IN0x00000004))!=(WHERE_IPK0x00000100|WHERE_COLUMN_IN0x00000004)
144926 ){
144927 Bitmask m = 0;
144928 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
144929 WHERE_ORDERBY_LIMIT0x0800, nLoop-1, pFrom->aLoop[nLoop-1], &m);
144930 testcase( wsFlags & WHERE_IPK );
144931 testcase( wsFlags & WHERE_COLUMN_IN );
144932 if( rc==pWInfo->pOrderBy->nExpr ){
144933 pWInfo->bOrderedInnerLoop = 1;
144934 pWInfo->revMask = m;
144935 }
144936 }
144937 }
144938 }
144939 }
144940 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP0x0200)
144941 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
144942 ){
144943 Bitmask revMask = 0;
144944 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
144945 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
144946 );
144947 assert( pWInfo->sorted==0 )((void) (0));
144948 if( nOrder==pWInfo->pOrderBy->nExpr ){
144949 pWInfo->sorted = 1;
144950 pWInfo->revMask = revMask;
144951 }
144952 }
144953 }
144954
144955
144956 pWInfo->nRowOut = pFrom->nRow;
144957
144958 /* Free temporary memory and return success */
144959 sqlite3DbFreeNN(db, pSpace);
144960 return SQLITE_OK0;
144961}
144962
144963/*
144964** Most queries use only a single table (they are not joins) and have
144965** simple == constraints against indexed fields. This routine attempts
144966** to plan those simple cases using much less ceremony than the
144967** general-purpose query planner, and thereby yield faster sqlite3_prepare()
144968** times for the common case.
144969**
144970** Return non-zero on success, if this query can be handled by this
144971** no-frills query planner. Return zero if this query needs the
144972** general-purpose query planner.
144973*/
144974static int whereShortCut(WhereLoopBuilder *pBuilder){
144975 WhereInfo *pWInfo;
144976 struct SrcList_item *pItem;
144977 WhereClause *pWC;
144978 WhereTerm *pTerm;
144979 WhereLoop *pLoop;
144980 int iCur;
144981 int j;
144982 Table *pTab;
144983 Index *pIdx;
144984
144985 pWInfo = pBuilder->pWInfo;
144986 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE0x0020 ) return 0;
144987 assert( pWInfo->pTabList->nSrc>=1 )((void) (0));
144988 pItem = pWInfo->pTabList->a;
144989 pTab = pItem->pTab;
144990 if( IsVirtual(pTab)((pTab)->nModuleArg) ) return 0;
144991 if( pItem->fg.isIndexedBy ) return 0;
144992 iCur = pItem->iCursor;
144993 pWC = &pWInfo->sWC;
144994 pLoop = pBuilder->pNew;
144995 pLoop->wsFlags = 0;
144996 pLoop->nSkip = 0;
144997 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ0x0002|WO_IS0x0080, 0);
144998 if( pTerm ){
144999 testcase( pTerm->eOperator & WO_IS );
145000 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001|WHERE_IPK0x00000100|WHERE_ONEROW0x00001000;
145001 pLoop->aLTerm[0] = pTerm;
145002 pLoop->nLTerm = 1;
145003 pLoop->u.btree.nEq = 1;
145004 /* TUNING: Cost of a rowid lookup is 10 */
145005 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
145006 }else{
145007 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
145008 int opMask;
145009 assert( pLoop->aLTermSpace==pLoop->aLTerm )((void) (0));
145010 if( !IsUniqueIndex(pIdx)((pIdx)->onError!=0)
145011 || pIdx->pPartIdxWhere!=0
145012 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)((int)(sizeof(pLoop->aLTermSpace)/sizeof(pLoop->aLTermSpace
[0])))
145013 ) continue;
145014 opMask = pIdx->uniqNotNull ? (WO_EQ0x0002|WO_IS0x0080) : WO_EQ0x0002;
145015 for(j=0; j<pIdx->nKeyCol; j++){
145016 pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx);
145017 if( pTerm==0 ) break;
145018 testcase( pTerm->eOperator & WO_IS );
145019 pLoop->aLTerm[j] = pTerm;
145020 }
145021 if( j!=pIdx->nKeyCol ) continue;
145022 pLoop->wsFlags = WHERE_COLUMN_EQ0x00000001|WHERE_ONEROW0x00001000|WHERE_INDEXED0x00000200;
145023 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
145024 pLoop->wsFlags |= WHERE_IDX_ONLY0x00000040;
145025 }
145026 pLoop->nLTerm = j;
145027 pLoop->u.btree.nEq = j;
145028 pLoop->u.btree.pIndex = pIdx;
145029 /* TUNING: Cost of a unique index lookup is 15 */
145030 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
145031 break;
145032 }
145033 }
145034 if( pLoop->wsFlags ){
145035 pLoop->nOut = (LogEst)1;
145036 pWInfo->a[0].pWLoop = pLoop;
145037 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] )((void) (0));
145038 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
145039 pWInfo->a[0].iTabCur = iCur;
145040 pWInfo->nRowOut = 1;
145041 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
145042 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT0x0100 ){
145043 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
145044 }
145045#ifdef SQLITE_DEBUG
145046 pLoop->cId = '0';
145047#endif
145048 return 1;
145049 }
145050 return 0;
145051}
145052
145053/*
145054** Helper function for exprIsDeterministic().
145055*/
145056static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){
145057 if( pExpr->op==TK_FUNCTION161 && ExprHasProperty(pExpr, EP_ConstFunc)(((pExpr)->flags&(0x080000))!=0)==0 ){
145058 pWalker->eCode = 0;
145059 return WRC_Abort2;
145060 }
145061 return WRC_Continue0;
145062}
145063
145064/*
145065** Return true if the expression contains no non-deterministic SQL
145066** functions. Do not consider non-deterministic SQL functions that are
145067** part of sub-select statements.
145068*/
145069static int exprIsDeterministic(Expr *p){
145070 Walker w;
145071 memset(&w, 0, sizeof(w));
145072 w.eCode = 1;
145073 w.xExprCallback = exprNodeIsDeterministic;
145074 w.xSelectCallback = sqlite3SelectWalkFail;
145075 sqlite3WalkExpr(&w, p);
145076 return w.eCode;
145077}
145078
145079/*
145080** Generate the beginning of the loop used for WHERE clause processing.
145081** The return value is a pointer to an opaque structure that contains
145082** information needed to terminate the loop. Later, the calling routine
145083** should invoke sqlite3WhereEnd() with the return value of this function
145084** in order to complete the WHERE clause processing.
145085**
145086** If an error occurs, this routine returns NULL.
145087**
145088** The basic idea is to do a nested loop, one loop for each table in
145089** the FROM clause of a select. (INSERT and UPDATE statements are the
145090** same as a SELECT with only a single table in the FROM clause.) For
145091** example, if the SQL is this:
145092**
145093** SELECT * FROM t1, t2, t3 WHERE ...;
145094**
145095** Then the code generated is conceptually like the following:
145096**
145097** foreach row1 in t1 do \ Code generated
145098** foreach row2 in t2 do |-- by sqlite3WhereBegin()
145099** foreach row3 in t3 do /
145100** ...
145101** end \ Code generated
145102** end |-- by sqlite3WhereEnd()
145103** end /
145104**
145105** Note that the loops might not be nested in the order in which they
145106** appear in the FROM clause if a different order is better able to make
145107** use of indices. Note also that when the IN operator appears in
145108** the WHERE clause, it might result in additional nested loops for
145109** scanning through all values on the right-hand side of the IN.
145110**
145111** There are Btree cursors associated with each table. t1 uses cursor
145112** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
145113** And so forth. This routine generates code to open those VDBE cursors
145114** and sqlite3WhereEnd() generates the code to close them.
145115**
145116** The code that sqlite3WhereBegin() generates leaves the cursors named
145117** in pTabList pointing at their appropriate entries. The [...] code
145118** can use OP_Column and OP_Rowid opcodes on these cursors to extract
145119** data from the various tables of the loop.
145120**
145121** If the WHERE clause is empty, the foreach loops must each scan their
145122** entire tables. Thus a three-way join is an O(N^3) operation. But if
145123** the tables have indices and there are terms in the WHERE clause that
145124** refer to those indices, a complete table scan can be avoided and the
145125** code will run much faster. Most of the work of this routine is checking
145126** to see if there are indices that can be used to speed up the loop.
145127**
145128** Terms of the WHERE clause are also used to limit which rows actually
145129** make it to the "..." in the middle of the loop. After each "foreach",
145130** terms of the WHERE clause that use only terms in that loop and outer
145131** loops are evaluated and if false a jump is made around all subsequent
145132** inner loops (or around the "..." if the test occurs within the inner-
145133** most loop)
145134**
145135** OUTER JOINS
145136**
145137** An outer join of tables t1 and t2 is conceptally coded as follows:
145138**
145139** foreach row1 in t1 do
145140** flag = 0
145141** foreach row2 in t2 do
145142** start:
145143** ...
145144** flag = 1
145145** end
145146** if flag==0 then
145147** move the row2 cursor to a null row
145148** goto start
145149** fi
145150** end
145151**
145152** ORDER BY CLAUSE PROCESSING
145153**
145154** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
145155** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
145156** if there is one. If there is no ORDER BY clause or if this routine
145157** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
145158**
145159** The iIdxCur parameter is the cursor number of an index. If
145160** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index
145161** to use for OR clause processing. The WHERE clause should use this
145162** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
145163** the first cursor in an array of cursors for all indices. iIdxCur should
145164** be used to compute the appropriate cursor depending on which index is
145165** used.
145166*/
145167SQLITE_PRIVATEstatic WhereInfo *sqlite3WhereBegin(
145168 Parse *pParse, /* The parser context */
145169 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */
145170 Expr *pWhere, /* The WHERE clause */
145171 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */
145172 ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */
145173 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */
145174 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number
145175 ** If WHERE_USE_LIMIT, then the limit amount */
145176){
145177 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
145178 int nTabList; /* Number of elements in pTabList */
145179 WhereInfo *pWInfo; /* Will become the return value of this function */
145180 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
145181 Bitmask notReady; /* Cursors that are not yet positioned */
145182 WhereLoopBuilder sWLB; /* The WhereLoop builder */
145183 WhereMaskSet *pMaskSet; /* The expression mask set */
145184 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
145185 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
145186 int ii; /* Loop counter */
145187 sqlite3 *db; /* Database connection */
145188 int rc; /* Return code */
145189 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
145190
145191 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (((void) (0))
145192 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0((void) (0))
145193 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0((void) (0))
145194 ))((void) (0));
145195
145196 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */
145197 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0((void) (0))
145198 || (wctrlFlags & WHERE_USE_LIMIT)==0 )((void) (0));
145199
145200 /* Variable initialization */
145201 db = pParse->db;
145202 memset(&sWLB, 0, sizeof(sWLB));
145203
145204 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */
145205 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
145206 if( pOrderBy && pOrderBy->nExpr>=BMS((int)(sizeof(Bitmask)*8)) ) pOrderBy = 0;
145207 sWLB.pOrderBy = pOrderBy;
145208
145209 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
145210 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
145211 if( OptimizationDisabled(db, SQLITE_DistinctOpt)(((db)->dbOptFlags&(0x0010))!=0) ){
145212 wctrlFlags &= ~WHERE_WANT_DISTINCT0x0100;
145213 }
145214
145215 /* The number of tables in the FROM clause is limited by the number of
145216 ** bits in a Bitmask
145217 */
145218 testcase( pTabList->nSrc==BMS );
145219 if( pTabList->nSrc>BMS((int)(sizeof(Bitmask)*8)) ){
145220 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS((int)(sizeof(Bitmask)*8)));
145221 return 0;
145222 }
145223
145224 /* This function normally generates a nested loop for all tables in
145225 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should
145226 ** only generate code for the first table in pTabList and assume that
145227 ** any cursors associated with subsequent tables are uninitialized.
145228 */
145229 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020) ? 1 : pTabList->nSrc;
145230
145231 /* Allocate and initialize the WhereInfo structure that will become the
145232 ** return value. A single allocation is used to store the WhereInfo
145233 ** struct, the contents of WhereInfo.a[], the WhereClause structure
145234 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
145235 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
145236 ** some architectures. Hence the ROUND8() below.
145237 */
145238 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel))(((sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel))+7)&
~7)
;
145239 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop));
145240 if( db->mallocFailed ){
145241 sqlite3DbFree(db, pWInfo);
145242 pWInfo = 0;
145243 goto whereBeginError;
145244 }
145245 pWInfo->pParse = pParse;
145246 pWInfo->pTabList = pTabList;
145247 pWInfo->pOrderBy = pOrderBy;
145248 pWInfo->pWhere = pWhere;
145249 pWInfo->pResultSet = pResultSet;
145250 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
145251 pWInfo->nLevel = nTabList;
145252 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
145253 pWInfo->wctrlFlags = wctrlFlags;
145254 pWInfo->iLimit = iAuxArg;
145255 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
145256 memset(&pWInfo->nOBSat, 0,
145257 offsetof(WhereInfo,sWC)__builtin_offsetof(WhereInfo, sWC) - offsetof(WhereInfo,nOBSat)__builtin_offsetof(WhereInfo, nOBSat));
145258 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
145259 assert( pWInfo->eOnePass==ONEPASS_OFF )((void) (0)); /* ONEPASS defaults to OFF */
145260 pMaskSet = &pWInfo->sMaskSet;
145261 sWLB.pWInfo = pWInfo;
145262 sWLB.pWC = &pWInfo->sWC;
145263 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
145264 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) )((void) (0));
145265 whereLoopInit(sWLB.pNew);
145266#ifdef SQLITE_DEBUG
145267 sWLB.pNew->cId = '*';
145268#endif
145269
145270 /* Split the WHERE clause into separate subexpressions where each
145271 ** subexpression is separated by an AND operator.
145272 */
145273 initMaskSet(pMaskSet)(pMaskSet)->n=0;
145274 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
145275 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND44);
145276
145277 /* Special case: No FROM clause
145278 */
145279 if( nTabList==0 ){
145280 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
145281 if( wctrlFlags & WHERE_WANT_DISTINCT0x0100 ){
145282 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
145283 }
145284 ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW"))sqlite3VdbeExplain (pParse, 0, "SCAN CONSTANT ROW");
145285 }else{
145286 /* Assign a bit from the bitmask to every term in the FROM clause.
145287 **
145288 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
145289 **
145290 ** The rule of the previous sentence ensures thta if X is the bitmask for
145291 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
145292 ** Knowing the bitmask for all tables to the left of a left join is
145293 ** important. Ticket #3015.
145294 **
145295 ** Note that bitmasks are created for all pTabList->nSrc tables in
145296 ** pTabList, not just the first nTabList tables. nTabList is normally
145297 ** equal to pTabList->nSrc but might be shortened to 1 if the
145298 ** WHERE_OR_SUBCLAUSE flag is set.
145299 */
145300 ii = 0;
145301 do{
145302 createMask(pMaskSet, pTabList->a[ii].iCursor);
145303 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
145304 }while( (++ii)<pTabList->nSrc );
145305 #ifdef SQLITE_DEBUG
145306 {
145307 Bitmask mx = 0;
145308 for(ii=0; ii<pTabList->nSrc; ii++){
145309 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
145310 assert( m>=mx )((void) (0));
145311 mx = m;
145312 }
145313 }
145314 #endif
145315 }
145316
145317 /* Analyze all of the subexpressions. */
145318 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
145319 if( db->mallocFailed ) goto whereBeginError;
145320
145321 /* Special case: WHERE terms that do not refer to any tables in the join
145322 ** (constant expressions). Evaluate each such term, and jump over all the
145323 ** generated code if the result is not true.
145324 **
145325 ** Do not do this if the expression contains non-deterministic functions
145326 ** that are not within a sub-select. This is not strictly required, but
145327 ** preserves SQLite's legacy behaviour in the following two cases:
145328 **
145329 ** FROM ... WHERE random()>0; -- eval random() once per row
145330 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
145331 */
145332 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
145333 WhereTerm *pT = &sWLB.pWC->a[ii];
145334 if( pT->wtFlags & TERM_VIRTUAL0x02 ) continue;
145335 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
145336 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL0x10);
145337 pT->wtFlags |= TERM_CODED0x04;
145338 }
145339 }
145340
145341 if( wctrlFlags & WHERE_WANT_DISTINCT0x0100 ){
145342 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
145343 /* The DISTINCT marking is pointless. Ignore it. */
145344 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE1;
145345 }else if( pOrderBy==0 ){
145346 /* Try to ORDER BY the result set to make distinct processing easier */
145347 pWInfo->wctrlFlags |= WHERE_DISTINCTBY0x0080;
145348 pWInfo->pOrderBy = pResultSet;
145349 }
145350 }
145351
145352 /* Construct the WhereLoop objects */
145353#if defined(WHERETRACE_ENABLED)
145354 if( sqlite3WhereTrace & 0xffff ){
145355 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
145356 if( wctrlFlags & WHERE_USE_LIMIT0x4000 ){
145357 sqlite3DebugPrintf(", limit: %d", iAuxArg);
145358 }
145359 sqlite3DebugPrintf(")\n");
145360 }
145361 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
145362 sqlite3WhereClausePrint(sWLB.pWC);
145363 }
145364#endif
145365
145366 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
145367 rc = whereLoopAddAll(&sWLB);
145368 if( rc ) goto whereBeginError;
145369
145370#ifdef WHERETRACE_ENABLED
145371 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */
145372 WhereLoop *p;
145373 int i;
145374 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
145375 "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
145376 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
145377 p->cId = zLabel[i%(sizeof(zLabel)-1)];
145378 whereLoopPrint(p, sWLB.pWC);
145379 }
145380 }
145381#endif
145382
145383 wherePathSolver(pWInfo, 0);
145384 if( db->mallocFailed ) goto whereBeginError;
145385 if( pWInfo->pOrderBy ){
145386 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
145387 if( db->mallocFailed ) goto whereBeginError;
145388 }
145389 }
145390 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder0x00001000)!=0 ){
145391 pWInfo->revMask = ALLBITS((Bitmask)-1);
145392 }
145393 if( pParse->nErr || NEVER(db->mallocFailed)(db->mallocFailed) ){
145394 goto whereBeginError;
145395 }
145396#ifdef WHERETRACE_ENABLED
145397 if( sqlite3WhereTrace ){
145398 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
145399 if( pWInfo->nOBSat>0 ){
145400 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
145401 }
145402 switch( pWInfo->eDistinct ){
145403 case WHERE_DISTINCT_UNIQUE1: {
145404 sqlite3DebugPrintf(" DISTINCT=unique");
145405 break;
145406 }
145407 case WHERE_DISTINCT_ORDERED2: {
145408 sqlite3DebugPrintf(" DISTINCT=ordered");
145409 break;
145410 }
145411 case WHERE_DISTINCT_UNORDERED3: {
145412 sqlite3DebugPrintf(" DISTINCT=unordered");
145413 break;
145414 }
145415 }
145416 sqlite3DebugPrintf("\n");
145417 for(ii=0; ii<pWInfo->nLevel; ii++){
145418 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
145419 }
145420 }
145421#endif
145422
145423 /* Attempt to omit tables from the join that do not affect the result.
145424 ** For a table to not affect the result, the following must be true:
145425 **
145426 ** 1) The query must not be an aggregate.
145427 ** 2) The table must be the RHS of a LEFT JOIN.
145428 ** 3) Either the query must be DISTINCT, or else the ON or USING clause
145429 ** must contain a constraint that limits the scan of the table to
145430 ** at most a single row.
145431 ** 4) The table must not be referenced by any part of the query apart
145432 ** from its own USING or ON clause.
145433 **
145434 ** For example, given:
145435 **
145436 ** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
145437 ** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
145438 ** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
145439 **
145440 ** then table t2 can be omitted from the following:
145441 **
145442 ** SELECT v1, v3 FROM t1
145443 ** LEFT JOIN t2 USING (t1.ipk=t2.ipk)
145444 ** LEFT JOIN t3 USING (t1.ipk=t3.ipk)
145445 **
145446 ** or from:
145447 **
145448 ** SELECT DISTINCT v1, v3 FROM t1
145449 ** LEFT JOIN t2
145450 ** LEFT JOIN t3 USING (t1.ipk=t3.ipk)
145451 */
145452 notReady = ~(Bitmask)0;
145453 if( pWInfo->nLevel>=2
145454 && pResultSet!=0 /* guarantees condition (1) above */
145455 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)(((db)->dbOptFlags&(0x0100))==0)
145456 ){
145457 int i;
145458 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
145459 if( sWLB.pOrderBy ){
145460 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
145461 }
145462 for(i=pWInfo->nLevel-1; i>=1; i--){
145463 WhereTerm *pTerm, *pEnd;
145464 struct SrcList_item *pItem;
145465 pLoop = pWInfo->a[i].pWLoop;
145466 pItem = &pWInfo->pTabList->a[pLoop->iTab];
145467 if( (pItem->fg.jointype & JT_LEFT0x0008)==0 ) continue;
145468 if( (wctrlFlags & WHERE_WANT_DISTINCT0x0100)==0
145469 && (pLoop->wsFlags & WHERE_ONEROW0x00001000)==0
145470 ){
145471 continue;
145472 }
145473 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
145474 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
145475 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
145476 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
145477 if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)(((pTerm->pExpr)->flags&(0x000001))!=0)
145478 || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
145479 ){
145480 break;
145481 }
145482 }
145483 }
145484 if( pTerm<pEnd ) continue;
145485 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
145486 notReady &= ~pLoop->maskSelf;
145487 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
145488 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
145489 pTerm->wtFlags |= TERM_CODED0x04;
145490 }
145491 }
145492 if( i!=pWInfo->nLevel-1 ){
145493 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
145494 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
145495 }
145496 pWInfo->nLevel--;
145497 nTabList--;
145498 }
145499 }
145500 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
145501 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
145502
145503 /* If the caller is an UPDATE or DELETE statement that is requesting
145504 ** to use a one-pass algorithm, determine if this is appropriate.
145505 **
145506 ** A one-pass approach can be used if the caller has requested one
145507 ** and either (a) the scan visits at most one row or (b) each
145508 ** of the following are true:
145509 **
145510 ** * the caller has indicated that a one-pass approach can be used
145511 ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and
145512 ** * the table is not a virtual table, and
145513 ** * either the scan does not use the OR optimization or the caller
145514 ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified
145515 ** for DELETE).
145516 **
145517 ** The last qualification is because an UPDATE statement uses
145518 ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can
145519 ** use a one-pass approach, and this is not set accurately for scans
145520 ** that use the OR optimization.
145521 */
145522 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 )((void) (0));
145523 if( (wctrlFlags & WHERE_ONEPASS_DESIRED0x0004)!=0 ){
145524 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
145525 int bOnerow = (wsFlags & WHERE_ONEROW0x00001000)!=0;
145526 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) )((void) (0));
145527 if( bOnerow || (
145528 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW0x0008)
145529 && !IsVirtual(pTabList->a[0].pTab)((pTabList->a[0].pTab)->nModuleArg)
145530 && (0==(wsFlags & WHERE_MULTI_OR0x00002000) || (wctrlFlags & WHERE_DUPLICATES_OK0x0010))
145531 )){
145532 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE1 : ONEPASS_MULTI2;
145533 if( HasRowid(pTabList->a[0].pTab)(((pTabList->a[0].pTab)->tabFlags & 0x0020)==0) && (wsFlags & WHERE_IDX_ONLY0x00000040) ){
145534 if( wctrlFlags & WHERE_ONEPASS_MULTIROW0x0008 ){
145535 bFordelete = OPFLAG_FORDELETE0x08;
145536 }
145537 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY0x00000040);
145538 }
145539 }
145540 }
145541
145542 /* Open all tables in the pTabList and any indices selected for
145543 ** searching those tables.
145544 */
145545 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
145546 Table *pTab; /* Table to open */
145547 int iDb; /* Index of database containing table/index */
145548 struct SrcList_item *pTabItem;
145549
145550 pTabItem = &pTabList->a[pLevel->iFrom];
145551 pTab = pTabItem->pTab;
145552 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
145553 pLoop = pLevel->pWLoop;
145554 if( (pTab->tabFlags & TF_Ephemeral0x0002)!=0 || pTab->pSelect ){
145555 /* Do nothing */
145556 }else
145557#ifndef SQLITE_OMIT_VIRTUALTABLE
145558 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE0x00000400)!=0 ){
145559 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
145560 int iCur = pTabItem->iCursor;
145561 sqlite3VdbeAddOp4(v, OP_VOpen163, iCur, 0, 0, pVTab, P4_VTAB(-12));
145562 }else if( IsVirtual(pTab)((pTab)->nModuleArg) ){
145563 /* noop */
145564 }else
145565#endif
145566 if( (pLoop->wsFlags & WHERE_IDX_ONLY0x00000040)==0
145567 && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)==0 ){
145568 int op = OP_OpenRead108;
145569 if( pWInfo->eOnePass!=ONEPASS_OFF0 ){
145570 op = OP_OpenWrite109;
145571 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
145572 };
145573 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
145574 assert( pTabItem->iCursor==pLevel->iTabCur )((void) (0));
145575 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
145576 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
145577 if( pWInfo->eOnePass==ONEPASS_OFF0 && pTab->nCol<BMS((int)(sizeof(Bitmask)*8)) && HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
145578 Bitmask b = pTabItem->colUsed;
145579 int n = 0;
145580 for(; b; b=b>>1, n++){}
145581 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n)((void*)(long int)(n)), P4_INT32(-3));
145582 assert( n<=pTab->nCol )((void) (0));
145583 }
145584#ifdef SQLITE_ENABLE_CURSOR_HINTS
145585 if( pLoop->u.btree.pIndex!=0 ){
145586 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ0x02|bFordelete);
145587 }else
145588#endif
145589 {
145590 sqlite3VdbeChangeP5(v, bFordelete);
145591 }
145592#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
145593 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed118, pTabItem->iCursor, 0, 0,
145594 (const u8*)&pTabItem->colUsed, P4_INT64(-14));
145595#endif
145596 }else{
145597 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
145598 }
145599 if( pLoop->wsFlags & WHERE_INDEXED0x00000200 ){
145600 Index *pIx = pLoop->u.btree.pIndex;
145601 int iIndexCur;
145602 int op = OP_OpenRead108;
145603 /* iAuxArg is always set to a positive value if ONEPASS is possible */
145604 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 )((void) (0));
145605 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) && IsPrimaryKeyIndex(pIx)((pIx)->idxType==2)
145606 && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)!=0
145607 ){
145608 /* This is one term of an OR-optimization using the PRIMARY KEY of a
145609 ** WITHOUT ROWID table. No need for a separate index */
145610 iIndexCur = pLevel->iTabCur;
145611 op = 0;
145612 }else if( pWInfo->eOnePass!=ONEPASS_OFF0 ){
145613 Index *pJ = pTabItem->pTab->pIndex;
145614 iIndexCur = iAuxArg;
145615 assert( wctrlFlags & WHERE_ONEPASS_DESIRED )((void) (0));
145616 while( ALWAYS(pJ)(pJ) && pJ!=pIx ){
145617 iIndexCur++;
145618 pJ = pJ->pNext;
145619 }
145620 op = OP_OpenWrite109;
145621 pWInfo->aiCurOnePass[1] = iIndexCur;
145622 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)!=0 ){
145623 iIndexCur = iAuxArg;
145624 op = OP_ReopenIdx106;
145625 }else{
145626 iIndexCur = pParse->nTab++;
145627 }
145628 pLevel->iIdxCur = iIndexCur;
145629 assert( pIx->pSchema==pTab->pSchema )((void) (0));
145630 assert( iIndexCur>=0 )((void) (0));
145631 if( op ){
145632 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
145633 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
145634 if( (pLoop->wsFlags & WHERE_CONSTRAINT0x0000000f)!=0
145635 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE0x00000002|WHERE_SKIPSCAN0x00008000))==0
145636 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN0x0001)==0
145637 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED2
145638 ){
145639 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ0x02); /* Hint to COMDB2 */
145640 }
145641 VdbeComment((v, "%s", pIx->zName));
145642#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
145643 {
145644 u64 colUsed = 0;
145645 int ii, jj;
145646 for(ii=0; ii<pIx->nColumn; ii++){
145647 jj = pIx->aiColumn[ii];
145648 if( jj<0 ) continue;
145649 if( jj>63 ) jj = 63;
145650 if( (pTabItem->colUsed & MASKBIT(jj)(((Bitmask)1)<<(jj)))==0 ) continue;
145651 colUsed |= ((u64)1)<<(ii<63 ? ii : 63);
145652 }
145653 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed118, iIndexCur, 0, 0,
145654 (u8*)&colUsed, P4_INT64(-14));
145655 }
145656#endif /* SQLITE_ENABLE_COLUMN_USED_MASK */
145657 }
145658 }
145659 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
145660 }
145661 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
145662 if( db->mallocFailed ) goto whereBeginError;
145663
145664 /* Generate the code to do the search. Each iteration of the for
145665 ** loop below generates code for a single nested loop of the VM
145666 ** program.
145667 */
145668 for(ii=0; ii<nTabList; ii++){
145669 int addrExplain;
145670 int wsFlags;
145671 pLevel = &pWInfo->a[ii];
145672 wsFlags = pLevel->pWLoop->wsFlags;
145673#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
145674 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX0x00004000)!=0 ){
145675 constructAutomaticIndex(pParse, &pWInfo->sWC,
145676 &pTabList->a[pLevel->iFrom], notReady, pLevel);
145677 if( db->mallocFailed ) goto whereBeginError;
145678 }
145679#endif
145680 addrExplain = sqlite3WhereExplainOneScan(
145681 pParse, pTabList, pLevel, wctrlFlags
145682 );
145683 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
145684 notReady = sqlite3WhereCodeOneLoopStart(pParse,v,pWInfo,ii,pLevel,notReady);
145685 pWInfo->iContinue = pLevel->addrCont;
145686 if( (wsFlags&WHERE_MULTI_OR0x00002000)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE0x0020)==0 ){
145687 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain)((void)addrExplain);
145688 }
145689 }
145690
145691 /* Done. */
145692 VdbeModuleComment((v, "Begin WHERE-core"));
145693 return pWInfo;
145694
145695 /* Jump here if malloc fails */
145696whereBeginError:
145697 if( pWInfo ){
145698 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
145699 whereInfoFree(db, pWInfo);
145700 }
145701 return 0;
145702}
145703
145704/*
145705** Part of sqlite3WhereEnd() will rewrite opcodes to reference the
145706** index rather than the main table. In SQLITE_DEBUG mode, we want
145707** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine
145708** does that.
145709*/
145710#ifndef SQLITE_DEBUG
145711# define OpcodeRewriteTrace(D,K,P) /* no-op */
145712#else
145713# define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P)
145714 static void sqlite3WhereOpcodeRewriteTrace(
145715 sqlite3 *db,
145716 int pc,
145717 VdbeOp *pOp
145718 ){
145719 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
145720 sqlite3VdbePrintOp(0, pc, pOp);
145721 }
145722#endif
145723
145724/*
145725** Generate the end of the WHERE loop. See comments on
145726** sqlite3WhereBegin() for additional information.
145727*/
145728SQLITE_PRIVATEstatic void sqlite3WhereEnd(WhereInfo *pWInfo){
145729 Parse *pParse = pWInfo->pParse;
145730 Vdbe *v = pParse->pVdbe;
145731 int i;
145732 WhereLevel *pLevel;
145733 WhereLoop *pLoop;
145734 SrcList *pTabList = pWInfo->pTabList;
145735 sqlite3 *db = pParse->db;
145736
145737 /* Generate loop termination code.
145738 */
145739 VdbeModuleComment((v, "End WHERE-core"));
145740 for(i=pWInfo->nLevel-1; i>=0; i--){
145741 int addr;
145742 pLevel = &pWInfo->a[i];
145743 pLoop = pLevel->pWLoop;
145744 if( pLevel->op!=OP_Noop170 ){
145745#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
145746 int addrSeek = 0;
145747 Index *pIdx;
145748 int n;
145749 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED2
145750 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
145751 && (pLoop->wsFlags & WHERE_INDEXED0x00000200)!=0
145752 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
145753 && (n = pLoop->u.btree.nIdxCol)>0
145754 && pIdx->aiRowLogEst[n]>=36
145755 ){
145756 int r1 = pParse->nMem+1;
145757 int j, op;
145758 for(j=0; j<n; j++){
145759 sqlite3VdbeAddOp3(v, OP_Column90, pLevel->iIdxCur, j, r1+j);
145760 }
145761 pParse->nMem += n+1;
145762 op = pLevel->op==OP_Prev4 ? OP_SeekLT22 : OP_SeekGT25;
145763 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
145764 VdbeCoverageIf(v, op==OP_SeekLT);
145765 VdbeCoverageIf(v, op==OP_SeekGT);
145766 sqlite3VdbeAddOp2(v, OP_Goto11, 1, pLevel->p2);
145767 }
145768#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
145769 /* The common case: Advance to the next row */
145770 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
145771 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
145772 sqlite3VdbeChangeP5(v, pLevel->p5);
145773 VdbeCoverage(v);
145774 VdbeCoverageIf(v, pLevel->op==OP_Next);
145775 VdbeCoverageIf(v, pLevel->op==OP_Prev);
145776 VdbeCoverageIf(v, pLevel->op==OP_VNext);
145777#ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
145778 if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek);
145779#endif
145780 }else{
145781 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
145782 }
145783 if( pLoop->wsFlags & WHERE_IN_ABLE0x00000800 && pLevel->u.in.nIn>0 ){
145784 struct InLoop *pIn;
145785 int j;
145786 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
145787 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
145788 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
145789 if( pIn->eEndLoopOp!=OP_Noop170 ){
145790 if( pIn->nPrefix ){
145791 assert( pLoop->wsFlags & WHERE_IN_EARLYOUT )((void) (0));
145792 sqlite3VdbeAddOp4Int(v, OP_IfNoHope26, pLevel->iIdxCur,
145793 sqlite3VdbeCurrentAddr(v)+2,
145794 pIn->iBase, pIn->nPrefix);
145795 VdbeCoverage(v);
145796 }
145797 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
145798 VdbeCoverage(v);
145799 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
145800 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
145801 }
145802 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
145803 }
145804 }
145805 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
145806 if( pLevel->addrSkip ){
145807 sqlite3VdbeGoto(v, pLevel->addrSkip);
145808 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
145809 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
145810 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
145811 }
145812#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
145813 if( pLevel->addrLikeRep ){
145814 sqlite3VdbeAddOp2(v, OP_DecrJumpZero49, (int)(pLevel->iLikeRepCntr>>1),
145815 pLevel->addrLikeRep);
145816 VdbeCoverage(v);
145817 }
145818#endif
145819 if( pLevel->iLeftJoin ){
145820 int ws = pLoop->wsFlags;
145821 addr = sqlite3VdbeAddOp1(v, OP_IfPos47, pLevel->iLeftJoin); VdbeCoverage(v);
145822 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 )((void) (0));
145823 if( (ws & WHERE_IDX_ONLY0x00000040)==0 ){
145824 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor )((void) (0));
145825 sqlite3VdbeAddOp1(v, OP_NullRow129, pLevel->iTabCur);
145826 }
145827 if( (ws & WHERE_INDEXED0x00000200)
145828 || ((ws & WHERE_MULTI_OR0x00002000) && pLevel->u.pCovidx)
145829 ){
145830 sqlite3VdbeAddOp1(v, OP_NullRow129, pLevel->iIdxCur);
145831 }
145832 if( pLevel->op==OP_Return66 ){
145833 sqlite3VdbeAddOp2(v, OP_Gosub12, pLevel->p1, pLevel->addrFirst);
145834 }else{
145835 sqlite3VdbeGoto(v, pLevel->addrFirst);
145836 }
145837 sqlite3VdbeJumpHere(v, addr);
145838 }
145839 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
145840 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
145841 }
145842
145843 /* The "break" point is here, just past the end of the outer loop.
145844 ** Set it.
145845 */
145846 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
145847
145848 assert( pWInfo->nLevel<=pTabList->nSrc )((void) (0));
145849 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
145850 int k, last;
145851 VdbeOp *pOp;
145852 Index *pIdx = 0;
145853 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
145854 Table *pTab = pTabItem->pTab;
145855 assert( pTab!=0 )((void) (0));
145856 pLoop = pLevel->pWLoop;
145857
145858 /* For a co-routine, change all OP_Column references to the table of
145859 ** the co-routine into OP_Copy of result contained in a register.
145860 ** OP_Rowid becomes OP_Null.
145861 */
145862 if( pTabItem->fg.viaCoroutine ){
145863 testcase( pParse->db->mallocFailed );
145864 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
145865 pTabItem->regResult, 0);
145866 continue;
145867 }
145868
145869#ifdef SQLITE_ENABLE_EARLY_CURSOR_CLOSE
145870 /* Close all of the cursors that were opened by sqlite3WhereBegin.
145871 ** Except, do not close cursors that will be reused by the OR optimization
145872 ** (WHERE_OR_SUBCLAUSE). And do not close the OP_OpenWrite cursors
145873 ** created for the ONEPASS optimization.
145874 */
145875 if( (pTab->tabFlags & TF_Ephemeral0x0002)==0
145876 && pTab->pSelect==0
145877 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE0x0020)==0
145878 ){
145879 int ws = pLoop->wsFlags;
145880 if( pWInfo->eOnePass==ONEPASS_OFF0 && (ws & WHERE_IDX_ONLY0x00000040)==0 ){
145881 sqlite3VdbeAddOp1(v, OP_Close117, pTabItem->iCursor);
145882 }
145883 if( (ws & WHERE_INDEXED0x00000200)!=0
145884 && (ws & (WHERE_IPK0x00000100|WHERE_AUTO_INDEX0x00004000))==0
145885 && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
145886 ){
145887 sqlite3VdbeAddOp1(v, OP_Close117, pLevel->iIdxCur);
145888 }
145889 }
145890#endif
145891
145892 /* If this scan uses an index, make VDBE code substitutions to read data
145893 ** from the index instead of from the table where possible. In some cases
145894 ** this optimization prevents the table from ever being read, which can
145895 ** yield a significant performance boost.
145896 **
145897 ** Calls to the code generator in between sqlite3WhereBegin and
145898 ** sqlite3WhereEnd will have created code that references the table
145899 ** directly. This loop scans all that code looking for opcodes
145900 ** that reference the table and converts them into opcodes that
145901 ** reference the index.
145902 */
145903 if( pLoop->wsFlags & (WHERE_INDEXED0x00000200|WHERE_IDX_ONLY0x00000040) ){
145904 pIdx = pLoop->u.btree.pIndex;
145905 }else if( pLoop->wsFlags & WHERE_MULTI_OR0x00002000 ){
145906 pIdx = pLevel->u.pCovidx;
145907 }
145908 if( pIdx
145909 && (pWInfo->eOnePass==ONEPASS_OFF0 || !HasRowid(pIdx->pTable)(((pIdx->pTable)->tabFlags & 0x0020)==0))
145910 && !db->mallocFailed
145911 ){
145912 last = sqlite3VdbeCurrentAddr(v);
145913 k = pLevel->addrBody;
145914#ifdef SQLITE_DEBUG
145915 if( db->flags & SQLITE_VdbeAddopTrace ){
145916 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
145917 }
145918#endif
145919 pOp = sqlite3VdbeGetOp(v, k);
145920 for(; k<last; k++, pOp++){
145921 if( pOp->p1!=pLevel->iTabCur ) continue;
145922 if( pOp->opcode==OP_Column90
145923#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
145924 || pOp->opcode==OP_Offset89
145925#endif
145926 ){
145927 int x = pOp->p2;
145928 assert( pIdx->pTable==pTab )((void) (0));
145929 if( !HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) ){
145930 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
145931 x = pPk->aiColumn[x];
145932 assert( x>=0 )((void) (0));
145933 }
145934 x = sqlite3ColumnOfIndex(pIdx, x);
145935 if( x>=0 ){
145936 pOp->p2 = x;
145937 pOp->p1 = pLevel->iIdxCur;
145938 OpcodeRewriteTrace(db, k, pOp);
145939 }
145940 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0((void) (0))
145941 || pWInfo->eOnePass )((void) (0));
145942 }else if( pOp->opcode==OP_Rowid128 ){
145943 pOp->p1 = pLevel->iIdxCur;
145944 pOp->opcode = OP_IdxRowid135;
145945 OpcodeRewriteTrace(db, k, pOp);
145946 }else if( pOp->opcode==OP_IfNullRow21 ){
145947 pOp->p1 = pLevel->iIdxCur;
145948 OpcodeRewriteTrace(db, k, pOp);
145949 }
145950 }
145951#ifdef SQLITE_DEBUG
145952 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
145953#endif
145954 }
145955 }
145956
145957 /* Final cleanup
145958 */
145959 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
145960 whereInfoFree(db, pWInfo);
145961 return;
145962}
145963
145964/************** End of where.c ***********************************************/
145965/************** Begin file window.c ******************************************/
145966/*
145967** 2018 May 08
145968**
145969** The author disclaims copyright to this source code. In place of
145970** a legal notice, here is a blessing:
145971**
145972** May you do good and not evil.
145973** May you find forgiveness for yourself and forgive others.
145974** May you share freely, never taking more than you give.
145975**
145976*************************************************************************
145977*/
145978/* #include "sqliteInt.h" */
145979
145980#ifndef SQLITE_OMIT_WINDOWFUNC
145981
145982/*
145983** SELECT REWRITING
145984**
145985** Any SELECT statement that contains one or more window functions in
145986** either the select list or ORDER BY clause (the only two places window
145987** functions may be used) is transformed by function sqlite3WindowRewrite()
145988** in order to support window function processing. For example, with the
145989** schema:
145990**
145991** CREATE TABLE t1(a, b, c, d, e, f, g);
145992**
145993** the statement:
145994**
145995** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM t1 ORDER BY e;
145996**
145997** is transformed to:
145998**
145999** SELECT a+1, max(b) OVER (PARTITION BY c ORDER BY d) FROM (
146000** SELECT a, e, c, d, b FROM t1 ORDER BY c, d
146001** ) ORDER BY e;
146002**
146003** The flattening optimization is disabled when processing this transformed
146004** SELECT statement. This allows the implementation of the window function
146005** (in this case max()) to process rows sorted in order of (c, d), which
146006** makes things easier for obvious reasons. More generally:
146007**
146008** * FROM, WHERE, GROUP BY and HAVING clauses are all moved to
146009** the sub-query.
146010**
146011** * ORDER BY, LIMIT and OFFSET remain part of the parent query.
146012**
146013** * Terminals from each of the expression trees that make up the
146014** select-list and ORDER BY expressions in the parent query are
146015** selected by the sub-query. For the purposes of the transformation,
146016** terminals are column references and aggregate functions.
146017**
146018** If there is more than one window function in the SELECT that uses
146019** the same window declaration (the OVER bit), then a single scan may
146020** be used to process more than one window function. For example:
146021**
146022** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
146023** min(e) OVER (PARTITION BY c ORDER BY d)
146024** FROM t1;
146025**
146026** is transformed in the same way as the example above. However:
146027**
146028** SELECT max(b) OVER (PARTITION BY c ORDER BY d),
146029** min(e) OVER (PARTITION BY a ORDER BY b)
146030** FROM t1;
146031**
146032** Must be transformed to:
146033**
146034** SELECT max(b) OVER (PARTITION BY c ORDER BY d) FROM (
146035** SELECT e, min(e) OVER (PARTITION BY a ORDER BY b), c, d, b FROM
146036** SELECT a, e, c, d, b FROM t1 ORDER BY a, b
146037** ) ORDER BY c, d
146038** ) ORDER BY e;
146039**
146040** so that both min() and max() may process rows in the order defined by
146041** their respective window declarations.
146042**
146043** INTERFACE WITH SELECT.C
146044**
146045** When processing the rewritten SELECT statement, code in select.c calls
146046** sqlite3WhereBegin() to begin iterating through the results of the
146047** sub-query, which is always implemented as a co-routine. It then calls
146048** sqlite3WindowCodeStep() to process rows and finish the scan by calling
146049** sqlite3WhereEnd().
146050**
146051** sqlite3WindowCodeStep() generates VM code so that, for each row returned
146052** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
146053** When the sub-routine is invoked:
146054**
146055** * The results of all window-functions for the row are stored
146056** in the associated Window.regResult registers.
146057**
146058** * The required terminal values are stored in the current row of
146059** temp table Window.iEphCsr.
146060**
146061** In some cases, depending on the window frame and the specific window
146062** functions invoked, sqlite3WindowCodeStep() caches each entire partition
146063** in a temp table before returning any rows. In other cases it does not.
146064** This detail is encapsulated within this file, the code generated by
146065** select.c is the same in either case.
146066**
146067** BUILT-IN WINDOW FUNCTIONS
146068**
146069** This implementation features the following built-in window functions:
146070**
146071** row_number()
146072** rank()
146073** dense_rank()
146074** percent_rank()
146075** cume_dist()
146076** ntile(N)
146077** lead(expr [, offset [, default]])
146078** lag(expr [, offset [, default]])
146079** first_value(expr)
146080** last_value(expr)
146081** nth_value(expr, N)
146082**
146083** These are the same built-in window functions supported by Postgres.
146084** Although the behaviour of aggregate window functions (functions that
146085** can be used as either aggregates or window funtions) allows them to
146086** be implemented using an API, built-in window functions are much more
146087** esoteric. Additionally, some window functions (e.g. nth_value())
146088** may only be implemented by caching the entire partition in memory.
146089** As such, some built-in window functions use the same API as aggregate
146090** window functions and some are implemented directly using VDBE
146091** instructions. Additionally, for those functions that use the API, the
146092** window frame is sometimes modified before the SELECT statement is
146093** rewritten. For example, regardless of the specified window frame, the
146094** row_number() function always uses:
146095**
146096** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146097**
146098** See sqlite3WindowUpdate() for details.
146099**
146100** As well as some of the built-in window functions, aggregate window
146101** functions min() and max() are implemented using VDBE instructions if
146102** the start of the window frame is declared as anything other than
146103** UNBOUNDED PRECEDING.
146104*/
146105
146106/*
146107** Implementation of built-in window function row_number(). Assumes that the
146108** window frame has been coerced to:
146109**
146110** ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146111*/
146112static void row_numberStepFunc(
146113 sqlite3_context *pCtx,
146114 int nArg,
146115 sqlite3_value **apArg
146116){
146117 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146118 if( p ) (*p)++;
146119 UNUSED_PARAMETER(nArg)(void)(nArg);
146120 UNUSED_PARAMETER(apArg)(void)(apArg);
146121}
146122static void row_numberValueFunc(sqlite3_context *pCtx){
146123 i64 *p = (i64*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146124 sqlite3_result_int64(pCtx, (p ? *p : 0));
146125}
146126
146127/*
146128** Context object type used by rank(), dense_rank(), percent_rank() and
146129** cume_dist().
146130*/
146131struct CallCount {
146132 i64 nValue;
146133 i64 nStep;
146134 i64 nTotal;
146135};
146136
146137/*
146138** Implementation of built-in window function dense_rank(). Assumes that
146139** the window frame has been set to:
146140**
146141** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146142*/
146143static void dense_rankStepFunc(
146144 sqlite3_context *pCtx,
146145 int nArg,
146146 sqlite3_value **apArg
146147){
146148 struct CallCount *p;
146149 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146150 if( p ) p->nStep = 1;
146151 UNUSED_PARAMETER(nArg)(void)(nArg);
146152 UNUSED_PARAMETER(apArg)(void)(apArg);
146153}
146154static void dense_rankValueFunc(sqlite3_context *pCtx){
146155 struct CallCount *p;
146156 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146157 if( p ){
146158 if( p->nStep ){
146159 p->nValue++;
146160 p->nStep = 0;
146161 }
146162 sqlite3_result_int64(pCtx, p->nValue);
146163 }
146164}
146165
146166/*
146167** Implementation of built-in window function nth_value(). This
146168** implementation is used in "slow mode" only - when the EXCLUDE clause
146169** is not set to the default value "NO OTHERS".
146170*/
146171struct NthValueCtx {
146172 i64 nStep;
146173 sqlite3_value *pValue;
146174};
146175static void nth_valueStepFunc(
146176 sqlite3_context *pCtx,
146177 int nArg,
146178 sqlite3_value **apArg
146179){
146180 struct NthValueCtx *p;
146181 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146182 if( p ){
146183 i64 iVal;
146184 switch( sqlite3_value_numeric_type(apArg[1]) ){
146185 case SQLITE_INTEGER1:
146186 iVal = sqlite3_value_int64(apArg[1]);
146187 break;
146188 case SQLITE_FLOAT2: {
146189 double fVal = sqlite3_value_double(apArg[1]);
146190 if( ((i64)fVal)!=fVal ) goto error_out;
146191 iVal = (i64)fVal;
146192 break;
146193 }
146194 default:
146195 goto error_out;
146196 }
146197 if( iVal<=0 ) goto error_out;
146198
146199 p->nStep++;
146200 if( iVal==p->nStep ){
146201 p->pValue = sqlite3_value_dup(apArg[0]);
146202 if( !p->pValue ){
146203 sqlite3_result_error_nomem(pCtx);
146204 }
146205 }
146206 }
146207 UNUSED_PARAMETER(nArg)(void)(nArg);
146208 UNUSED_PARAMETER(apArg)(void)(apArg);
146209 return;
146210
146211 error_out:
146212 sqlite3_result_error(
146213 pCtx, "second argument to nth_value must be a positive integer", -1
146214 );
146215}
146216static void nth_valueFinalizeFunc(sqlite3_context *pCtx){
146217 struct NthValueCtx *p;
146218 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, 0);
146219 if( p && p->pValue ){
146220 sqlite3_result_value(pCtx, p->pValue);
146221 sqlite3_value_free(p->pValue);
146222 p->pValue = 0;
146223 }
146224}
146225#define nth_valueInvFuncnoopStepFunc noopStepFunc
146226#define nth_valueValueFuncnoopValueFunc noopValueFunc
146227
146228static void first_valueStepFunc(
146229 sqlite3_context *pCtx,
146230 int nArg,
146231 sqlite3_value **apArg
146232){
146233 struct NthValueCtx *p;
146234 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146235 if( p && p->pValue==0 ){
146236 p->pValue = sqlite3_value_dup(apArg[0]);
146237 if( !p->pValue ){
146238 sqlite3_result_error_nomem(pCtx);
146239 }
146240 }
146241 UNUSED_PARAMETER(nArg)(void)(nArg);
146242 UNUSED_PARAMETER(apArg)(void)(apArg);
146243}
146244static void first_valueFinalizeFunc(sqlite3_context *pCtx){
146245 struct NthValueCtx *p;
146246 p = (struct NthValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146247 if( p && p->pValue ){
146248 sqlite3_result_value(pCtx, p->pValue);
146249 sqlite3_value_free(p->pValue);
146250 p->pValue = 0;
146251 }
146252}
146253#define first_valueInvFuncnoopStepFunc noopStepFunc
146254#define first_valueValueFuncnoopValueFunc noopValueFunc
146255
146256/*
146257** Implementation of built-in window function rank(). Assumes that
146258** the window frame has been set to:
146259**
146260** RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
146261*/
146262static void rankStepFunc(
146263 sqlite3_context *pCtx,
146264 int nArg,
146265 sqlite3_value **apArg
146266){
146267 struct CallCount *p;
146268 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146269 if( p ){
146270 p->nStep++;
146271 if( p->nValue==0 ){
146272 p->nValue = p->nStep;
146273 }
146274 }
146275 UNUSED_PARAMETER(nArg)(void)(nArg);
146276 UNUSED_PARAMETER(apArg)(void)(apArg);
146277}
146278static void rankValueFunc(sqlite3_context *pCtx){
146279 struct CallCount *p;
146280 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146281 if( p ){
146282 sqlite3_result_int64(pCtx, p->nValue);
146283 p->nValue = 0;
146284 }
146285}
146286
146287/*
146288** Implementation of built-in window function percent_rank(). Assumes that
146289** the window frame has been set to:
146290**
146291** GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
146292*/
146293static void percent_rankStepFunc(
146294 sqlite3_context *pCtx,
146295 int nArg,
146296 sqlite3_value **apArg
146297){
146298 struct CallCount *p;
146299 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
146300 UNUSED_PARAMETER(apArg)(void)(apArg);
146301 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146302 if( p ){
146303 p->nTotal++;
146304 }
146305}
146306static void percent_rankInvFunc(
146307 sqlite3_context *pCtx,
146308 int nArg,
146309 sqlite3_value **apArg
146310){
146311 struct CallCount *p;
146312 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
146313 UNUSED_PARAMETER(apArg)(void)(apArg);
146314 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146315 p->nStep++;
146316}
146317static void percent_rankValueFunc(sqlite3_context *pCtx){
146318 struct CallCount *p;
146319 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146320 if( p ){
146321 p->nValue = p->nStep;
146322 if( p->nTotal>1 ){
146323 double r = (double)p->nValue / (double)(p->nTotal-1);
146324 sqlite3_result_double(pCtx, r);
146325 }else{
146326 sqlite3_result_double(pCtx, 0.0);
146327 }
146328 }
146329}
146330#define percent_rankFinalizeFuncpercent_rankValueFunc percent_rankValueFunc
146331
146332/*
146333** Implementation of built-in window function cume_dist(). Assumes that
146334** the window frame has been set to:
146335**
146336** GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING
146337*/
146338static void cume_distStepFunc(
146339 sqlite3_context *pCtx,
146340 int nArg,
146341 sqlite3_value **apArg
146342){
146343 struct CallCount *p;
146344 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
146345 UNUSED_PARAMETER(apArg)(void)(apArg);
146346 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146347 if( p ){
146348 p->nTotal++;
146349 }
146350}
146351static void cume_distInvFunc(
146352 sqlite3_context *pCtx,
146353 int nArg,
146354 sqlite3_value **apArg
146355){
146356 struct CallCount *p;
146357 UNUSED_PARAMETER(nArg)(void)(nArg); assert( nArg==0 )((void) (0));
146358 UNUSED_PARAMETER(apArg)(void)(apArg);
146359 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146360 p->nStep++;
146361}
146362static void cume_distValueFunc(sqlite3_context *pCtx){
146363 struct CallCount *p;
146364 p = (struct CallCount*)sqlite3_aggregate_context(pCtx, 0);
146365 if( p ){
146366 double r = (double)(p->nStep) / (double)(p->nTotal);
146367 sqlite3_result_double(pCtx, r);
146368 }
146369}
146370#define cume_distFinalizeFunccume_distValueFunc cume_distValueFunc
146371
146372/*
146373** Context object for ntile() window function.
146374*/
146375struct NtileCtx {
146376 i64 nTotal; /* Total rows in partition */
146377 i64 nParam; /* Parameter passed to ntile(N) */
146378 i64 iRow; /* Current row */
146379};
146380
146381/*
146382** Implementation of ntile(). This assumes that the window frame has
146383** been coerced to:
146384**
146385** ROWS CURRENT ROW AND UNBOUNDED FOLLOWING
146386*/
146387static void ntileStepFunc(
146388 sqlite3_context *pCtx,
146389 int nArg,
146390 sqlite3_value **apArg
146391){
146392 struct NtileCtx *p;
146393 assert( nArg==1 )((void) (0)); UNUSED_PARAMETER(nArg)(void)(nArg);
146394 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146395 if( p ){
146396 if( p->nTotal==0 ){
146397 p->nParam = sqlite3_value_int64(apArg[0]);
146398 if( p->nParam<=0 ){
146399 sqlite3_result_error(
146400 pCtx, "argument of ntile must be a positive integer", -1
146401 );
146402 }
146403 }
146404 p->nTotal++;
146405 }
146406}
146407static void ntileInvFunc(
146408 sqlite3_context *pCtx,
146409 int nArg,
146410 sqlite3_value **apArg
146411){
146412 struct NtileCtx *p;
146413 assert( nArg==1 )((void) (0)); UNUSED_PARAMETER(nArg)(void)(nArg);
146414 UNUSED_PARAMETER(apArg)(void)(apArg);
146415 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146416 p->iRow++;
146417}
146418static void ntileValueFunc(sqlite3_context *pCtx){
146419 struct NtileCtx *p;
146420 p = (struct NtileCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146421 if( p && p->nParam>0 ){
146422 int nSize = (p->nTotal / p->nParam);
146423 if( nSize==0 ){
146424 sqlite3_result_int64(pCtx, p->iRow+1);
146425 }else{
146426 i64 nLarge = p->nTotal - p->nParam*nSize;
146427 i64 iSmall = nLarge*(nSize+1);
146428 i64 iRow = p->iRow;
146429
146430 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal )((void) (0));
146431
146432 if( iRow<iSmall ){
146433 sqlite3_result_int64(pCtx, 1 + iRow/(nSize+1));
146434 }else{
146435 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
146436 }
146437 }
146438 }
146439}
146440#define ntileFinalizeFuncntileValueFunc ntileValueFunc
146441
146442/*
146443** Context object for last_value() window function.
146444*/
146445struct LastValueCtx {
146446 sqlite3_value *pVal;
146447 int nVal;
146448};
146449
146450/*
146451** Implementation of last_value().
146452*/
146453static void last_valueStepFunc(
146454 sqlite3_context *pCtx,
146455 int nArg,
146456 sqlite3_value **apArg
146457){
146458 struct LastValueCtx *p;
146459 UNUSED_PARAMETER(nArg)(void)(nArg);
146460 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146461 if( p ){
146462 sqlite3_value_free(p->pVal);
146463 p->pVal = sqlite3_value_dup(apArg[0]);
146464 if( p->pVal==0 ){
146465 sqlite3_result_error_nomem(pCtx);
146466 }else{
146467 p->nVal++;
146468 }
146469 }
146470}
146471static void last_valueInvFunc(
146472 sqlite3_context *pCtx,
146473 int nArg,
146474 sqlite3_value **apArg
146475){
146476 struct LastValueCtx *p;
146477 UNUSED_PARAMETER(nArg)(void)(nArg);
146478 UNUSED_PARAMETER(apArg)(void)(apArg);
146479 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146480 if( ALWAYS(p)(p) ){
146481 p->nVal--;
146482 if( p->nVal==0 ){
146483 sqlite3_value_free(p->pVal);
146484 p->pVal = 0;
146485 }
146486 }
146487}
146488static void last_valueValueFunc(sqlite3_context *pCtx){
146489 struct LastValueCtx *p;
146490 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, 0);
146491 if( p && p->pVal ){
146492 sqlite3_result_value(pCtx, p->pVal);
146493 }
146494}
146495static void last_valueFinalizeFunc(sqlite3_context *pCtx){
146496 struct LastValueCtx *p;
146497 p = (struct LastValueCtx*)sqlite3_aggregate_context(pCtx, sizeof(*p));
146498 if( p && p->pVal ){
146499 sqlite3_result_value(pCtx, p->pVal);
146500 sqlite3_value_free(p->pVal);
146501 p->pVal = 0;
146502 }
146503}
146504
146505/*
146506** Static names for the built-in window function names. These static
146507** names are used, rather than string literals, so that FuncDef objects
146508** can be associated with a particular window function by direct
146509** comparison of the zName pointer. Example:
146510**
146511** if( pFuncDef->zName==row_valueName ){ ... }
146512*/
146513static const char row_numberName[] = "row_number";
146514static const char dense_rankName[] = "dense_rank";
146515static const char rankName[] = "rank";
146516static const char percent_rankName[] = "percent_rank";
146517static const char cume_distName[] = "cume_dist";
146518static const char ntileName[] = "ntile";
146519static const char last_valueName[] = "last_value";
146520static const char nth_valueName[] = "nth_value";
146521static const char first_valueName[] = "first_value";
146522static const char leadName[] = "lead";
146523static const char lagName[] = "lag";
146524
146525/*
146526** No-op implementations of xStep() and xFinalize(). Used as place-holders
146527** for built-in window functions that never call those interfaces.
146528**
146529** The noopValueFunc() is called but is expected to do nothing. The
146530** noopStepFunc() is never called, and so it is marked with NO_TEST to
146531** let the test coverage routine know not to expect this function to be
146532** invoked.
146533*/
146534static void noopStepFunc( /*NO_TEST*/
146535 sqlite3_context *p, /*NO_TEST*/
146536 int n, /*NO_TEST*/
146537 sqlite3_value **a /*NO_TEST*/
146538){ /*NO_TEST*/
146539 UNUSED_PARAMETER(p)(void)(p); /*NO_TEST*/
146540 UNUSED_PARAMETER(n)(void)(n); /*NO_TEST*/
146541 UNUSED_PARAMETER(a)(void)(a); /*NO_TEST*/
146542 assert(0)((void) (0)); /*NO_TEST*/
146543} /*NO_TEST*/
146544static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p)(void)(p); /*no-op*/ }
146545
146546/* Window functions that use all window interfaces: xStep, xFinal,
146547** xValue, and xInverse */
146548#define WINDOWFUNCALL(name,nArg,extra){ nArg, (1|0x00010000|extra), 0, 0, nameStepFunc, nameFinalizeFunc
, nameValueFunc, nameInvFunc, nameName, {0} }
{ \
146549 nArg, (SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
146550 name ## StepFunc, name ## FinalizeFunc, name ## ValueFunc, \
146551 name ## InvFunc, name ## Name, {0} \
146552}
146553
146554/* Window functions that are implemented using bytecode and thus have
146555** no-op routines for their methods */
146556#define WINDOWFUNCNOOP(name,nArg,extra){ nArg, (1|0x00010000|extra), 0, 0, noopStepFunc, noopValueFunc
, noopValueFunc, noopStepFunc, nameName, {0} }
{ \
146557 nArg, (SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
146558 noopStepFunc, noopValueFunc, noopValueFunc, \
146559 noopStepFunc, name ## Name, {0} \
146560}
146561
146562/* Window functions that use all window interfaces: xStep, the
146563** same routine for xFinalize and xValue and which never call
146564** xInverse. */
146565#define WINDOWFUNCX(name,nArg,extra){ nArg, (1|0x00010000|extra), 0, 0, nameStepFunc, nameValueFunc
, nameValueFunc, noopStepFunc, nameName, {0} }
{ \
146566 nArg, (SQLITE_UTF81|SQLITE_FUNC_WINDOW0x00010000|extra), 0, 0, \
146567 name ## StepFunc, name ## ValueFunc, name ## ValueFunc, \
146568 noopStepFunc, name ## Name, {0} \
146569}
146570
146571
146572/*
146573** Register those built-in window functions that are not also aggregates.
146574*/
146575SQLITE_PRIVATEstatic void sqlite3WindowFunctions(void){
146576 static FuncDef aWindowFuncs[] = {
146577 WINDOWFUNCX(row_number, 0, 0){ 0, (1|0x00010000|0), 0, 0, row_numberStepFunc, row_numberValueFunc
, row_numberValueFunc, noopStepFunc, row_numberName, {0} }
,
146578 WINDOWFUNCX(dense_rank, 0, 0){ 0, (1|0x00010000|0), 0, 0, dense_rankStepFunc, dense_rankValueFunc
, dense_rankValueFunc, noopStepFunc, dense_rankName, {0} }
,
146579 WINDOWFUNCX(rank, 0, 0){ 0, (1|0x00010000|0), 0, 0, rankStepFunc, rankValueFunc, rankValueFunc
, noopStepFunc, rankName, {0} }
,
146580 WINDOWFUNCALL(percent_rank, 0, 0){ 0, (1|0x00010000|0), 0, 0, percent_rankStepFunc, percent_rankValueFunc
, percent_rankValueFunc, percent_rankInvFunc, percent_rankName
, {0} }
,
146581 WINDOWFUNCALL(cume_dist, 0, 0){ 0, (1|0x00010000|0), 0, 0, cume_distStepFunc, cume_distValueFunc
, cume_distValueFunc, cume_distInvFunc, cume_distName, {0} }
,
146582 WINDOWFUNCALL(ntile, 1, 0){ 1, (1|0x00010000|0), 0, 0, ntileStepFunc, ntileValueFunc, ntileValueFunc
, ntileInvFunc, ntileName, {0} }
,
146583 WINDOWFUNCALL(last_value, 1, 0){ 1, (1|0x00010000|0), 0, 0, last_valueStepFunc, last_valueFinalizeFunc
, last_valueValueFunc, last_valueInvFunc, last_valueName, {0}
}
,
146584 WINDOWFUNCALL(nth_value, 2, 0){ 2, (1|0x00010000|0), 0, 0, nth_valueStepFunc, nth_valueFinalizeFunc
, noopValueFunc, noopStepFunc, nth_valueName, {0} }
,
146585 WINDOWFUNCALL(first_value, 1, 0){ 1, (1|0x00010000|0), 0, 0, first_valueStepFunc, first_valueFinalizeFunc
, noopValueFunc, noopStepFunc, first_valueName, {0} }
,
146586 WINDOWFUNCNOOP(lead, 1, 0){ 1, (1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc, noopValueFunc
, noopStepFunc, leadName, {0} }
,
146587 WINDOWFUNCNOOP(lead, 2, 0){ 2, (1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc, noopValueFunc
, noopStepFunc, leadName, {0} }
,
146588 WINDOWFUNCNOOP(lead, 3, 0){ 3, (1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc, noopValueFunc
, noopStepFunc, leadName, {0} }
,
146589 WINDOWFUNCNOOP(lag, 1, 0){ 1, (1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc, noopValueFunc
, noopStepFunc, lagName, {0} }
,
146590 WINDOWFUNCNOOP(lag, 2, 0){ 2, (1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc, noopValueFunc
, noopStepFunc, lagName, {0} }
,
146591 WINDOWFUNCNOOP(lag, 3, 0){ 3, (1|0x00010000|0), 0, 0, noopStepFunc, noopValueFunc, noopValueFunc
, noopStepFunc, lagName, {0} }
,
146592 };
146593 sqlite3InsertBuiltinFuncs(aWindowFuncs, ArraySize(aWindowFuncs)((int)(sizeof(aWindowFuncs)/sizeof(aWindowFuncs[0]))));
146594}
146595
146596static Window *windowFind(Parse *pParse, Window *pList, const char *zName){
146597 Window *p;
146598 for(p=pList; p; p=p->pNextWin){
146599 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
146600 }
146601 if( p==0 ){
146602 sqlite3ErrorMsg(pParse, "no such window: %s", zName);
146603 }
146604 return p;
146605}
146606
146607/*
146608** This function is called immediately after resolving the function name
146609** for a window function within a SELECT statement. Argument pList is a
146610** linked list of WINDOW definitions for the current SELECT statement.
146611** Argument pFunc is the function definition just resolved and pWin
146612** is the Window object representing the associated OVER clause. This
146613** function updates the contents of pWin as follows:
146614**
146615** * If the OVER clause refered to a named window (as in "max(x) OVER win"),
146616** search list pList for a matching WINDOW definition, and update pWin
146617** accordingly. If no such WINDOW clause can be found, leave an error
146618** in pParse.
146619**
146620** * If the function is a built-in window function that requires the
146621** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
146622** of this file), pWin is updated here.
146623*/
146624SQLITE_PRIVATEstatic void sqlite3WindowUpdate(
146625 Parse *pParse,
146626 Window *pList, /* List of named windows for this SELECT */
146627 Window *pWin, /* Window frame to update */
146628 FuncDef *pFunc /* Window function definition */
146629){
146630 if( pWin->zName && pWin->eFrmType==0 ){
146631 Window *p = windowFind(pParse, pList, pWin->zName);
146632 if( p==0 ) return;
146633 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
146634 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
146635 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
146636 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
146637 pWin->eStart = p->eStart;
146638 pWin->eEnd = p->eEnd;
146639 pWin->eFrmType = p->eFrmType;
146640 pWin->eExclude = p->eExclude;
146641 }else{
146642 sqlite3WindowChain(pParse, pWin, pList);
146643 }
146644 if( (pWin->eFrmType==TK_RANGE86)
146645 && (pWin->pStart || pWin->pEnd)
146646 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
146647 ){
146648 sqlite3ErrorMsg(pParse,
146649 "RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression"
146650 );
146651 }else
146652 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW0x00010000 ){
146653 sqlite3 *db = pParse->db;
146654 if( pWin->pFilter ){
146655 sqlite3ErrorMsg(pParse,
146656 "FILTER clause may only be used with aggregate window functions"
146657 );
146658 }else{
146659 struct WindowUpdate {
146660 const char *zFunc;
146661 int eFrmType;
146662 int eStart;
146663 int eEnd;
146664 } aUp[] = {
146665 { row_numberName, TK_ROWS76, TK_UNBOUNDED87, TK_CURRENT82 },
146666 { dense_rankName, TK_RANGE86, TK_UNBOUNDED87, TK_CURRENT82 },
146667 { rankName, TK_RANGE86, TK_UNBOUNDED87, TK_CURRENT82 },
146668 { percent_rankName, TK_GROUPS89, TK_CURRENT82, TK_UNBOUNDED87 },
146669 { cume_distName, TK_GROUPS89, TK_FOLLOWING83, TK_UNBOUNDED87 },
146670 { ntileName, TK_ROWS76, TK_CURRENT82, TK_UNBOUNDED87 },
146671 { leadName, TK_ROWS76, TK_UNBOUNDED87, TK_UNBOUNDED87 },
146672 { lagName, TK_ROWS76, TK_UNBOUNDED87, TK_CURRENT82 },
146673 };
146674 int i;
146675 for(i=0; i<ArraySize(aUp)((int)(sizeof(aUp)/sizeof(aUp[0]))); i++){
146676 if( pFunc->zName==aUp[i].zFunc ){
146677 sqlite3ExprDelete(db, pWin->pStart);
146678 sqlite3ExprDelete(db, pWin->pEnd);
146679 pWin->pEnd = pWin->pStart = 0;
146680 pWin->eFrmType = aUp[i].eFrmType;
146681 pWin->eStart = aUp[i].eStart;
146682 pWin->eEnd = aUp[i].eEnd;
146683 pWin->eExclude = 0;
146684 if( pWin->eStart==TK_FOLLOWING83 ){
146685 pWin->pStart = sqlite3Expr(db, TK_INTEGER147, "1");
146686 }
146687 break;
146688 }
146689 }
146690 }
146691 }
146692 pWin->pFunc = pFunc;
146693}
146694
146695/*
146696** Context object passed through sqlite3WalkExprList() to
146697** selectWindowRewriteExprCb() by selectWindowRewriteEList().
146698*/
146699typedef struct WindowRewrite WindowRewrite;
146700struct WindowRewrite {
146701 Window *pWin;
146702 SrcList *pSrc;
146703 ExprList *pSub;
146704 Table *pTab;
146705 Select *pSubSelect; /* Current sub-select, if any */
146706};
146707
146708/*
146709** Callback function used by selectWindowRewriteEList(). If necessary,
146710** this function appends to the output expression-list and updates
146711** expression (*ppExpr) in place.
146712*/
146713static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
146714 struct WindowRewrite *p = pWalker->u.pRewrite;
146715 Parse *pParse = pWalker->pParse;
146716
146717 /* If this function is being called from within a scalar sub-select
146718 ** that used by the SELECT statement being processed, only process
146719 ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
146720 ** not process aggregates or window functions at all, as they belong
146721 ** to the scalar sub-select. */
146722 if( p->pSubSelect ){
146723 if( pExpr->op!=TK_COLUMN162 ){
146724 return WRC_Continue0;
146725 }else{
146726 int nSrc = p->pSrc->nSrc;
146727 int i;
146728 for(i=0; i<nSrc; i++){
146729 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
146730 }
146731 if( i==nSrc ) return WRC_Continue0;
146732 }
146733 }
146734
146735 switch( pExpr->op ){
146736
146737 case TK_FUNCTION161:
146738 if( !ExprHasProperty(pExpr, EP_WinFunc)(((pExpr)->flags&(0x1000000))!=0) ){
146739 break;
146740 }else{
146741 Window *pWin;
146742 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
146743 if( pExpr->y.pWin==pWin ){
146744 assert( pWin->pOwner==pExpr )((void) (0));
146745 return WRC_Prune1;
146746 }
146747 }
146748 }
146749 /* Fall through. */
146750
146751 case TK_AGG_FUNCTION163:
146752 case TK_COLUMN162: {
146753 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
146754 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
146755 if( p->pSub ){
146756 assert( ExprHasProperty(pExpr, EP_Static)==0 )((void) (0));
146757 ExprSetProperty(pExpr, EP_Static)(pExpr)->flags|=(0x8000000);
146758 sqlite3ExprDelete(pParse->db, pExpr);
146759 ExprClearProperty(pExpr, EP_Static)(pExpr)->flags&=~(0x8000000);
146760 memset(pExpr, 0, sizeof(Expr));
146761
146762 pExpr->op = TK_COLUMN162;
146763 pExpr->iColumn = p->pSub->nExpr-1;
146764 pExpr->iTable = p->pWin->iEphCsr;
146765 pExpr->y.pTab = p->pTab;
146766 }
146767
146768 break;
146769 }
146770
146771 default: /* no-op */
146772 break;
146773 }
146774
146775 return WRC_Continue0;
146776}
146777static int selectWindowRewriteSelectCb(Walker *pWalker, Select *pSelect){
146778 struct WindowRewrite *p = pWalker->u.pRewrite;
146779 Select *pSave = p->pSubSelect;
146780 if( pSave==pSelect ){
146781 return WRC_Continue0;
146782 }else{
146783 p->pSubSelect = pSelect;
146784 sqlite3WalkSelect(pWalker, pSelect);
146785 p->pSubSelect = pSave;
146786 }
146787 return WRC_Prune1;
146788}
146789
146790
146791/*
146792** Iterate through each expression in expression-list pEList. For each:
146793**
146794** * TK_COLUMN,
146795** * aggregate function, or
146796** * window function with a Window object that is not a member of the
146797** Window list passed as the second argument (pWin).
146798**
146799** Append the node to output expression-list (*ppSub). And replace it
146800** with a TK_COLUMN that reads the (N-1)th element of table
146801** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
146802** appending the new one.
146803*/
146804static void selectWindowRewriteEList(
146805 Parse *pParse,
146806 Window *pWin,
146807 SrcList *pSrc,
146808 ExprList *pEList, /* Rewrite expressions in this list */
146809 Table *pTab,
146810 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
146811){
146812 Walker sWalker;
146813 WindowRewrite sRewrite;
146814
146815 memset(&sWalker, 0, sizeof(Walker));
146816 memset(&sRewrite, 0, sizeof(WindowRewrite));
146817
146818 sRewrite.pSub = *ppSub;
146819 sRewrite.pWin = pWin;
146820 sRewrite.pSrc = pSrc;
146821 sRewrite.pTab = pTab;
146822
146823 sWalker.pParse = pParse;
146824 sWalker.xExprCallback = selectWindowRewriteExprCb;
146825 sWalker.xSelectCallback = selectWindowRewriteSelectCb;
146826 sWalker.u.pRewrite = &sRewrite;
146827
146828 (void)sqlite3WalkExprList(&sWalker, pEList);
146829
146830 *ppSub = sRewrite.pSub;
146831}
146832
146833/*
146834** Append a copy of each expression in expression-list pAppend to
146835** expression list pList. Return a pointer to the result list.
146836*/
146837static ExprList *exprListAppendList(
146838 Parse *pParse, /* Parsing context */
146839 ExprList *pList, /* List to which to append. Might be NULL */
146840 ExprList *pAppend, /* List of values to append. Might be NULL */
146841 int bIntToNull
146842){
146843 if( pAppend ){
146844 int i;
146845 int nInit = pList ? pList->nExpr : 0;
146846 for(i=0; i<pAppend->nExpr; i++){
146847 Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
146848 if( bIntToNull && pDup && pDup->op==TK_INTEGER147 ){
146849 pDup->op = TK_NULL114;
146850 pDup->flags &= ~(EP_IntValue0x000400|EP_IsTrue0x10000000|EP_IsFalse0x20000000);
146851 }
146852 pList = sqlite3ExprListAppend(pParse, pList, pDup);
146853 if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
146854 }
146855 }
146856 return pList;
146857}
146858
146859/*
146860** If the SELECT statement passed as the second argument does not invoke
146861** any SQL window functions, this function is a no-op. Otherwise, it
146862** rewrites the SELECT statement so that window function xStep functions
146863** are invoked in the correct order as described under "SELECT REWRITING"
146864** at the top of this file.
146865*/
146866SQLITE_PRIVATEstatic int sqlite3WindowRewrite(Parse *pParse, Select *p){
146867 int rc = SQLITE_OK0;
146868 if( p->pWin && p->pPrior==0 ){
146869 Vdbe *v = sqlite3GetVdbe(pParse);
146870 sqlite3 *db = pParse->db;
146871 Select *pSub = 0; /* The subquery */
146872 SrcList *pSrc = p->pSrc;
146873 Expr *pWhere = p->pWhere;
146874 ExprList *pGroupBy = p->pGroupBy;
146875 Expr *pHaving = p->pHaving;
146876 ExprList *pSort = 0;
146877
146878 ExprList *pSublist = 0; /* Expression list for sub-query */
146879 Window *pMWin = p->pWin; /* Master window object */
146880 Window *pWin; /* Window object iterator */
146881 Table *pTab;
146882
146883 pTab = sqlite3DbMallocZero(db, sizeof(Table));
146884 if( pTab==0 ){
146885 return SQLITE_NOMEM7;
146886 }
146887
146888 p->pSrc = 0;
146889 p->pWhere = 0;
146890 p->pGroupBy = 0;
146891 p->pHaving = 0;
146892 p->selFlags &= ~SF_Aggregate0x00008;
146893
146894 /* Create the ORDER BY clause for the sub-select. This is the concatenation
146895 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
146896 ** redundant, remove the ORDER BY from the parent SELECT. */
146897 pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
146898 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
146899 if( pSort && p->pOrderBy ){
146900 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
146901 sqlite3ExprListDelete(db, p->pOrderBy);
146902 p->pOrderBy = 0;
146903 }
146904 }
146905
146906 /* Assign a cursor number for the ephemeral table used to buffer rows.
146907 ** The OpenEphemeral instruction is coded later, after it is known how
146908 ** many columns the table will have. */
146909 pMWin->iEphCsr = pParse->nTab++;
146910 pParse->nTab += 3;
146911
146912 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
146913 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
146914 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
146915
146916 /* Append the PARTITION BY and ORDER BY expressions to the to the
146917 ** sub-select expression list. They are required to figure out where
146918 ** boundaries for partitions and sets of peer rows lie. */
146919 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
146920 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
146921
146922 /* Append the arguments passed to each window function to the
146923 ** sub-select expression list. Also allocate two registers for each
146924 ** window function - one for the accumulator, another for interim
146925 ** results. */
146926 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
146927 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
146928 pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList, 0);
146929 if( pWin->pFilter ){
146930 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
146931 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
146932 }
146933 pWin->regAccum = ++pParse->nMem;
146934 pWin->regResult = ++pParse->nMem;
146935 sqlite3VdbeAddOp2(v, OP_Null73, 0, pWin->regAccum);
146936 }
146937
146938 /* If there is no ORDER BY or PARTITION BY clause, and the window
146939 ** function accepts zero arguments, and there are no other columns
146940 ** selected (e.g. "SELECT row_number() OVER () FROM t1"), it is possible
146941 ** that pSublist is still NULL here. Add a constant expression here to
146942 ** keep everything legal in this case.
146943 */
146944 if( pSublist==0 ){
146945 pSublist = sqlite3ExprListAppend(pParse, 0,
146946 sqlite3ExprAlloc(db, TK_INTEGER147, &sqlite3IntTokens[0], 0)
146947 );
146948 }
146949
146950 pSub = sqlite3SelectNew(
146951 pParse, pSublist, pSrc, pWhere, pGroupBy, pHaving, pSort, 0, 0
146952 );
146953 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
146954 if( p->pSrc ){
146955 Table *pTab2;
146956 p->pSrc->a[0].pSelect = pSub;
146957 sqlite3SrcListAssignCursors(pParse, p->pSrc);
146958 pSub->selFlags |= SF_Expanded0x00040;
146959 pTab2 = sqlite3ResultSetOfSelect(pParse, pSub);
146960 if( pTab2==0 ){
146961 rc = SQLITE_NOMEM7;
146962 }else{
146963 memcpy(pTab, pTab2, sizeof(Table));
146964 pTab->tabFlags |= TF_Ephemeral0x0002;
146965 p->pSrc->a[0].pTab = pTab;
146966 pTab = pTab2;
146967 }
146968 sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, pMWin->iEphCsr, pSublist->nExpr);
146969 sqlite3VdbeAddOp2(v, OP_OpenDup111, pMWin->iEphCsr+1, pMWin->iEphCsr);
146970 sqlite3VdbeAddOp2(v, OP_OpenDup111, pMWin->iEphCsr+2, pMWin->iEphCsr);
146971 sqlite3VdbeAddOp2(v, OP_OpenDup111, pMWin->iEphCsr+3, pMWin->iEphCsr);
146972 }else{
146973 sqlite3SelectDelete(db, pSub);
146974 }
146975 if( db->mallocFailed ) rc = SQLITE_NOMEM7;
146976 sqlite3DbFree(db, pTab);
146977 }
146978
146979 return rc;
146980}
146981
146982/*
146983** Free the Window object passed as the second argument.
146984*/
146985SQLITE_PRIVATEstatic void sqlite3WindowDelete(sqlite3 *db, Window *p){
146986 if( p ){
146987 sqlite3ExprDelete(db, p->pFilter);
146988 sqlite3ExprListDelete(db, p->pPartition);
146989 sqlite3ExprListDelete(db, p->pOrderBy);
146990 sqlite3ExprDelete(db, p->pEnd);
146991 sqlite3ExprDelete(db, p->pStart);
146992 sqlite3DbFree(db, p->zName);
146993 sqlite3DbFree(db, p->zBase);
146994 sqlite3DbFree(db, p);
146995 }
146996}
146997
146998/*
146999** Free the linked list of Window objects starting at the second argument.
147000*/
147001SQLITE_PRIVATEstatic void sqlite3WindowListDelete(sqlite3 *db, Window *p){
147002 while( p ){
147003 Window *pNext = p->pNextWin;
147004 sqlite3WindowDelete(db, p);
147005 p = pNext;
147006 }
147007}
147008
147009/*
147010** The argument expression is an PRECEDING or FOLLOWING offset. The
147011** value should be a non-negative integer. If the value is not a
147012** constant, change it to NULL. The fact that it is then a non-negative
147013** integer will be caught later. But it is important not to leave
147014** variable values in the expression tree.
147015*/
147016static Expr *sqlite3WindowOffsetExpr(Parse *pParse, Expr *pExpr){
147017 if( 0==sqlite3ExprIsConstant(pExpr) ){
147018 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ) sqlite3RenameExprUnmap(pParse, pExpr);
147019 sqlite3ExprDelete(pParse->db, pExpr);
147020 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL114, 0, 0);
147021 }
147022 return pExpr;
147023}
147024
147025/*
147026** Allocate and return a new Window object describing a Window Definition.
147027*/
147028SQLITE_PRIVATEstatic Window *sqlite3WindowAlloc(
147029 Parse *pParse, /* Parsing context */
147030 int eType, /* Frame type. TK_RANGE, TK_ROWS, TK_GROUPS, or 0 */
147031 int eStart, /* Start type: CURRENT, PRECEDING, FOLLOWING, UNBOUNDED */
147032 Expr *pStart, /* Start window size if TK_PRECEDING or FOLLOWING */
147033 int eEnd, /* End type: CURRENT, FOLLOWING, TK_UNBOUNDED, PRECEDING */
147034 Expr *pEnd, /* End window size if TK_FOLLOWING or PRECEDING */
147035 u8 eExclude /* EXCLUDE clause */
147036){
147037 Window *pWin = 0;
147038 int bImplicitFrame = 0;
147039
147040 /* Parser assures the following: */
147041 assert( eType==0 || eType==TK_RANGE || eType==TK_ROWS || eType==TK_GROUPS )((void) (0));
147042 assert( eStart==TK_CURRENT || eStart==TK_PRECEDING((void) (0))
147043 || eStart==TK_UNBOUNDED || eStart==TK_FOLLOWING )((void) (0));
147044 assert( eEnd==TK_CURRENT || eEnd==TK_FOLLOWING((void) (0))
147045 || eEnd==TK_UNBOUNDED || eEnd==TK_PRECEDING )((void) (0));
147046 assert( (eStart==TK_PRECEDING || eStart==TK_FOLLOWING)==(pStart!=0) )((void) (0));
147047 assert( (eEnd==TK_FOLLOWING || eEnd==TK_PRECEDING)==(pEnd!=0) )((void) (0));
147048
147049 if( eType==0 ){
147050 bImplicitFrame = 1;
147051 eType = TK_RANGE86;
147052 }
147053
147054 /* Additionally, the
147055 ** starting boundary type may not occur earlier in the following list than
147056 ** the ending boundary type:
147057 **
147058 ** UNBOUNDED PRECEDING
147059 ** <expr> PRECEDING
147060 ** CURRENT ROW
147061 ** <expr> FOLLOWING
147062 ** UNBOUNDED FOLLOWING
147063 **
147064 ** The parser ensures that "UNBOUNDED PRECEDING" cannot be used as an ending
147065 ** boundary, and than "UNBOUNDED FOLLOWING" cannot be used as a starting
147066 ** frame boundary.
147067 */
147068 if( (eStart==TK_CURRENT82 && eEnd==TK_PRECEDING85)
147069 || (eStart==TK_FOLLOWING83 && (eEnd==TK_PRECEDING85 || eEnd==TK_CURRENT82))
147070 ){
147071 sqlite3ErrorMsg(pParse, "unsupported frame specification");
147072 goto windowAllocErr;
147073 }
147074
147075 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
147076 if( pWin==0 ) goto windowAllocErr;
147077 pWin->eFrmType = eType;
147078 pWin->eStart = eStart;
147079 pWin->eEnd = eEnd;
147080 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc)(((pParse->db)->dbOptFlags&(0x0002))!=0) ){
147081 eExclude = TK_NO66;
147082 }
147083 pWin->eExclude = eExclude;
147084 pWin->bImplicitFrame = bImplicitFrame;
147085 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
147086 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
147087 return pWin;
147088
147089windowAllocErr:
147090 sqlite3ExprDelete(pParse->db, pEnd);
147091 sqlite3ExprDelete(pParse->db, pStart);
147092 return 0;
147093}
147094
147095/*
147096** Attach PARTITION and ORDER BY clauses pPartition and pOrderBy to window
147097** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
147098** equivalent nul-terminated string.
147099*/
147100SQLITE_PRIVATEstatic Window *sqlite3WindowAssemble(
147101 Parse *pParse,
147102 Window *pWin,
147103 ExprList *pPartition,
147104 ExprList *pOrderBy,
147105 Token *pBase
147106){
147107 if( pWin ){
147108 pWin->pPartition = pPartition;
147109 pWin->pOrderBy = pOrderBy;
147110 if( pBase ){
147111 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
147112 }
147113 }else{
147114 sqlite3ExprListDelete(pParse->db, pPartition);
147115 sqlite3ExprListDelete(pParse->db, pOrderBy);
147116 }
147117 return pWin;
147118}
147119
147120/*
147121** Window *pWin has just been created from a WINDOW clause. Tokne pBase
147122** is the base window. Earlier windows from the same WINDOW clause are
147123** stored in the linked list starting at pWin->pNextWin. This function
147124** either updates *pWin according to the base specification, or else
147125** leaves an error in pParse.
147126*/
147127SQLITE_PRIVATEstatic void sqlite3WindowChain(Parse *pParse, Window *pWin, Window *pList){
147128 if( pWin->zBase ){
147129 sqlite3 *db = pParse->db;
147130 Window *pExist = windowFind(pParse, pList, pWin->zBase);
147131 if( pExist ){
147132 const char *zErr = 0;
147133 /* Check for errors */
147134 if( pWin->pPartition ){
147135 zErr = "PARTITION clause";
147136 }else if( pExist->pOrderBy && pWin->pOrderBy ){
147137 zErr = "ORDER BY clause";
147138 }else if( pExist->bImplicitFrame==0 ){
147139 zErr = "frame specification";
147140 }
147141 if( zErr ){
147142 sqlite3ErrorMsg(pParse,
147143 "cannot override %s of window: %s", zErr, pWin->zBase
147144 );
147145 }else{
147146 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
147147 if( pExist->pOrderBy ){
147148 assert( pWin->pOrderBy==0 )((void) (0));
147149 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
147150 }
147151 sqlite3DbFree(db, pWin->zBase);
147152 pWin->zBase = 0;
147153 }
147154 }
147155 }
147156}
147157
147158/*
147159** Attach window object pWin to expression p.
147160*/
147161SQLITE_PRIVATEstatic void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
147162 if( p ){
147163 assert( p->op==TK_FUNCTION )((void) (0));
147164 /* This routine is only called for the parser. If pWin was not
147165 ** allocated due to an OOM, then the parser would fail before ever
147166 ** invoking this routine */
147167 if( ALWAYS(pWin)(pWin) ){
147168 p->y.pWin = pWin;
147169 ExprSetProperty(p, EP_WinFunc)(p)->flags|=(0x1000000);
147170 pWin->pOwner = p;
147171 if( p->flags & EP_Distinct0x000002 ){
147172 sqlite3ErrorMsg(pParse,
147173 "DISTINCT is not supported for window functions");
147174 }
147175 }
147176 }else{
147177 sqlite3WindowDelete(pParse->db, pWin);
147178 }
147179}
147180
147181/*
147182** Return 0 if the two window objects are identical, or non-zero otherwise.
147183** Identical window objects can be processed in a single scan.
147184*/
147185SQLITE_PRIVATEstatic int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
147186 if( p1->eFrmType!=p2->eFrmType ) return 1;
147187 if( p1->eStart!=p2->eStart ) return 1;
147188 if( p1->eEnd!=p2->eEnd ) return 1;
147189 if( p1->eExclude!=p2->eExclude ) return 1;
147190 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
147191 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
147192 if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
147193 if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
147194 return 0;
147195}
147196
147197
147198/*
147199** This is called by code in select.c before it calls sqlite3WhereBegin()
147200** to begin iterating through the sub-query results. It is used to allocate
147201** and initialize registers and cursors used by sqlite3WindowCodeStep().
147202*/
147203SQLITE_PRIVATEstatic void sqlite3WindowCodeInit(Parse *pParse, Window *pMWin){
147204 Window *pWin;
147205 Vdbe *v = sqlite3GetVdbe(pParse);
147206
147207 /* Allocate registers to use for PARTITION BY values, if any. Initialize
147208 ** said registers to NULL. */
147209 if( pMWin->pPartition ){
147210 int nExpr = pMWin->pPartition->nExpr;
147211 pMWin->regPart = pParse->nMem+1;
147212 pParse->nMem += nExpr;
147213 sqlite3VdbeAddOp3(v, OP_Null73, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
147214 }
147215
147216 pMWin->regOne = ++pParse->nMem;
147217 sqlite3VdbeAddOp2(v, OP_Integer70, 1, pMWin->regOne);
147218
147219 if( pMWin->eExclude ){
147220 pMWin->regStartRowid = ++pParse->nMem;
147221 pMWin->regEndRowid = ++pParse->nMem;
147222 pMWin->csrApp = pParse->nTab++;
147223 sqlite3VdbeAddOp2(v, OP_Integer70, 1, pMWin->regStartRowid);
147224 sqlite3VdbeAddOp2(v, OP_Integer70, 0, pMWin->regEndRowid);
147225 sqlite3VdbeAddOp2(v, OP_OpenDup111, pMWin->csrApp, pMWin->iEphCsr);
147226 return;
147227 }
147228
147229 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147230 FuncDef *p = pWin->pFunc;
147231 if( (p->funcFlags & SQLITE_FUNC_MINMAX0x1000) && pWin->eStart!=TK_UNBOUNDED87 ){
147232 /* The inline versions of min() and max() require a single ephemeral
147233 ** table and 3 registers. The registers are used as follows:
147234 **
147235 ** regApp+0: slot to copy min()/max() argument to for MakeRecord
147236 ** regApp+1: integer value used to ensure keys are unique
147237 ** regApp+2: output of MakeRecord
147238 */
147239 ExprList *pList = pWin->pOwner->x.pList;
147240 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pList, 0, 0);
147241 pWin->csrApp = pParse->nTab++;
147242 pWin->regApp = pParse->nMem+1;
147243 pParse->nMem += 3;
147244 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
147245 assert( pKeyInfo->aSortOrder[0]==0 )((void) (0));
147246 pKeyInfo->aSortOrder[0] = 1;
147247 }
147248 sqlite3VdbeAddOp2(v, OP_OpenEphemeral113, pWin->csrApp, 2);
147249 sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO(-9));
147250 sqlite3VdbeAddOp2(v, OP_Integer70, 0, pWin->regApp+1);
147251 }
147252 else if( p->zName==nth_valueName || p->zName==first_valueName ){
147253 /* Allocate two registers at pWin->regApp. These will be used to
147254 ** store the start and end index of the current frame. */
147255 pWin->regApp = pParse->nMem+1;
147256 pWin->csrApp = pParse->nTab++;
147257 pParse->nMem += 2;
147258 sqlite3VdbeAddOp2(v, OP_OpenDup111, pWin->csrApp, pMWin->iEphCsr);
147259 }
147260 else if( p->zName==leadName || p->zName==lagName ){
147261 pWin->csrApp = pParse->nTab++;
147262 sqlite3VdbeAddOp2(v, OP_OpenDup111, pWin->csrApp, pMWin->iEphCsr);
147263 }
147264 }
147265}
147266
147267#define WINDOW_STARTING_INT0 0
147268#define WINDOW_ENDING_INT1 1
147269#define WINDOW_NTH_VALUE_INT2 2
147270#define WINDOW_STARTING_NUM3 3
147271#define WINDOW_ENDING_NUM4 4
147272
147273/*
147274** A "PRECEDING <expr>" (eCond==0) or "FOLLOWING <expr>" (eCond==1) or the
147275** value of the second argument to nth_value() (eCond==2) has just been
147276** evaluated and the result left in register reg. This function generates VM
147277** code to check that the value is a non-negative integer and throws an
147278** exception if it is not.
147279*/
147280static void windowCheckValue(Parse *pParse, int reg, int eCond){
147281 static const char *azErr[] = {
147282 "frame starting offset must be a non-negative integer",
147283 "frame ending offset must be a non-negative integer",
147284 "second argument to nth_value must be a positive integer",
147285 "frame starting offset must be a non-negative number",
147286 "frame ending offset must be a non-negative number",
147287 };
147288 static int aOp[] = { OP_Ge57, OP_Ge57, OP_Gt54, OP_Ge57, OP_Ge57 };
147289 Vdbe *v = sqlite3GetVdbe(pParse);
147290 int regZero = sqlite3GetTempReg(pParse);
147291 assert( eCond>=0 && eCond<ArraySize(azErr) )((void) (0));
147292 sqlite3VdbeAddOp2(v, OP_Integer70, 0, regZero);
147293 if( eCond>=WINDOW_STARTING_NUM3 ){
147294 int regString = sqlite3GetTempReg(pParse);
147295 sqlite3VdbeAddOp4(v, OP_String8110, 0, regString, 0, "", P4_STATIC(-1));
147296 sqlite3VdbeAddOp3(v, OP_Ge57, regString, sqlite3VdbeCurrentAddr(v)+2, reg);
147297 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC'C'|SQLITE_JUMPIFNULL0x10);
147298 VdbeCoverage(v);
147299 assert( eCond==3 || eCond==4 )((void) (0));
147300 VdbeCoverageIf(v, eCond==3);
147301 VdbeCoverageIf(v, eCond==4);
147302 }else{
147303 sqlite3VdbeAddOp2(v, OP_MustBeInt15, reg, sqlite3VdbeCurrentAddr(v)+2);
147304 VdbeCoverage(v);
147305 assert( eCond==0 || eCond==1 || eCond==2 )((void) (0));
147306 VdbeCoverageIf(v, eCond==0);
147307 VdbeCoverageIf(v, eCond==1);
147308 VdbeCoverageIf(v, eCond==2);
147309 }
147310 sqlite3VdbeAddOp3(v, aOp[eCond], regZero, sqlite3VdbeCurrentAddr(v)+2, reg);
147311 VdbeCoverageNeverNullIf(v, eCond==0); /* NULL case captured by */
147312 VdbeCoverageNeverNullIf(v, eCond==1); /* the OP_MustBeInt */
147313 VdbeCoverageNeverNullIf(v, eCond==2);
147314 VdbeCoverageNeverNullIf(v, eCond==3); /* NULL case caught by */
147315 VdbeCoverageNeverNullIf(v, eCond==4); /* the OP_Ge */
147316 sqlite3MayAbort(pParse);
147317 sqlite3VdbeAddOp2(v, OP_Halt69, SQLITE_ERROR1, OE_Abort2);
147318 sqlite3VdbeAppendP4(v, (void*)azErr[eCond], P4_STATIC(-1));
147319 sqlite3ReleaseTempReg(pParse, regZero);
147320}
147321
147322/*
147323** Return the number of arguments passed to the window-function associated
147324** with the object passed as the only argument to this function.
147325*/
147326static int windowArgCount(Window *pWin){
147327 ExprList *pList = pWin->pOwner->x.pList;
147328 return (pList ? pList->nExpr : 0);
147329}
147330
147331/*
147332** Generate VM code to invoke either xStep() (if bInverse is 0) or
147333** xInverse (if bInverse is non-zero) for each window function in the
147334** linked list starting at pMWin. Or, for built-in window functions
147335** that do not use the standard function API, generate the required
147336** inline VM code.
147337**
147338** If argument csr is greater than or equal to 0, then argument reg is
147339** the first register in an array of registers guaranteed to be large
147340** enough to hold the array of arguments for each function. In this case
147341** the arguments are extracted from the current row of csr into the
147342** array of registers before invoking OP_AggStep or OP_AggInverse
147343**
147344** Or, if csr is less than zero, then the array of registers at reg is
147345** already populated with all columns from the current row of the sub-query.
147346**
147347** If argument regPartSize is non-zero, then it is a register containing the
147348** number of rows in the current partition.
147349*/
147350static void windowAggStep(
147351 Parse *pParse,
147352 Window *pMWin, /* Linked list of window functions */
147353 int csr, /* Read arguments from this cursor */
147354 int bInverse, /* True to invoke xInverse instead of xStep */
147355 int reg /* Array of registers */
147356){
147357 Vdbe *v = sqlite3GetVdbe(pParse);
147358 Window *pWin;
147359 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147360 FuncDef *pFunc = pWin->pFunc;
147361 int regArg;
147362 int nArg = windowArgCount(pWin);
147363 int i;
147364
147365 for(i=0; i<nArg; i++){
147366 if( i!=1 || pFunc->zName!=nth_valueName ){
147367 sqlite3VdbeAddOp3(v, OP_Column90, csr, pWin->iArgCol+i, reg+i);
147368 }else{
147369 sqlite3VdbeAddOp3(v, OP_Column90, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
147370 }
147371 }
147372 regArg = reg;
147373
147374 if( pMWin->regStartRowid==0
147375 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000)
147376 && (pWin->eStart!=TK_UNBOUNDED87)
147377 ){
147378 int addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull50, regArg);
147379 VdbeCoverage(v);
147380 if( bInverse==0 ){
147381 sqlite3VdbeAddOp2(v, OP_AddImm83, pWin->regApp+1, 1);
147382 sqlite3VdbeAddOp2(v, OP_SCopy79, regArg, pWin->regApp);
147383 sqlite3VdbeAddOp3(v, OP_MakeRecord92, pWin->regApp, 2, pWin->regApp+2);
147384 sqlite3VdbeAddOp2(v, OP_IdxInsert132, pWin->csrApp, pWin->regApp+2);
147385 }else{
147386 sqlite3VdbeAddOp4Int(v, OP_SeekGE24, pWin->csrApp, 0, regArg, 1);
147387 VdbeCoverageNeverTaken(v);
147388 sqlite3VdbeAddOp1(v, OP_Delete123, pWin->csrApp);
147389 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
147390 }
147391 sqlite3VdbeJumpHere(v, addrIsNull);
147392 }else if( pWin->regApp ){
147393 assert( pFunc->zName==nth_valueName((void) (0))
147394 || pFunc->zName==first_valueName((void) (0))
147395 )((void) (0));
147396 assert( bInverse==0 || bInverse==1 )((void) (0));
147397 sqlite3VdbeAddOp2(v, OP_AddImm83, pWin->regApp+1-bInverse, 1);
147398 }else if( pFunc->xSFunc!=noopStepFunc ){
147399 int addrIf = 0;
147400 if( pWin->pFilter ){
147401 int regTmp;
147402 assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr )((void) (0));
147403 assert( nArg || pWin->pOwner->x.pList==0 )((void) (0));
147404 regTmp = sqlite3GetTempReg(pParse);
147405 sqlite3VdbeAddOp3(v, OP_Column90, csr, pWin->iArgCol+nArg,regTmp);
147406 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot20, regTmp, 0, 1);
147407 VdbeCoverage(v);
147408 sqlite3ReleaseTempReg(pParse, regTmp);
147409 }
147410 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL0x0020 ){
147411 CollSeq *pColl;
147412 assert( nArg>0 )((void) (0));
147413 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
147414 sqlite3VdbeAddOp4(v, OP_CollSeq82, 0,0,0, (const char*)pColl, P4_COLLSEQ(-2));
147415 }
147416 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse153 : OP_AggStep154,
147417 bInverse, regArg, pWin->regAccum);
147418 sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF(-8));
147419 sqlite3VdbeChangeP5(v, (u8)nArg);
147420 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
147421 }
147422 }
147423}
147424
147425typedef struct WindowCodeArg WindowCodeArg;
147426typedef struct WindowCsrAndReg WindowCsrAndReg;
147427struct WindowCsrAndReg {
147428 int csr;
147429 int reg;
147430};
147431
147432struct WindowCodeArg {
147433 Parse *pParse;
147434 Window *pMWin;
147435 Vdbe *pVdbe;
147436 int regGosub;
147437 int addrGosub;
147438 int regArg;
147439 int eDelete;
147440
147441 WindowCsrAndReg start;
147442 WindowCsrAndReg current;
147443 WindowCsrAndReg end;
147444};
147445
147446/*
147447** Values that may be passed as the second argument to windowCodeOp().
147448*/
147449#define WINDOW_RETURN_ROW1 1
147450#define WINDOW_AGGINVERSE2 2
147451#define WINDOW_AGGSTEP3 3
147452
147453/*
147454** Generate VM code to read the window frames peer values from cursor csr into
147455** an array of registers starting at reg.
147456*/
147457static void windowReadPeerValues(
147458 WindowCodeArg *p,
147459 int csr,
147460 int reg
147461){
147462 Window *pMWin = p->pMWin;
147463 ExprList *pOrderBy = pMWin->pOrderBy;
147464 if( pOrderBy ){
147465 Vdbe *v = sqlite3GetVdbe(p->pParse);
147466 ExprList *pPart = pMWin->pPartition;
147467 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
147468 int i;
147469 for(i=0; i<pOrderBy->nExpr; i++){
147470 sqlite3VdbeAddOp3(v, OP_Column90, csr, iColOff+i, reg+i);
147471 }
147472 }
147473}
147474
147475/*
147476** Generate VM code to invoke either xValue() (bFin==0) or xFinalize()
147477** (bFin==1) for each window function in the linked list starting at
147478** pMWin. Or, for built-in window-functions that do not use the standard
147479** API, generate the equivalent VM code.
147480*/
147481static void windowAggFinal(WindowCodeArg *p, int bFin){
147482 Parse *pParse = p->pParse;
147483 Window *pMWin = p->pMWin;
147484 Vdbe *v = sqlite3GetVdbe(pParse);
147485 Window *pWin;
147486
147487 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147488 if( pMWin->regStartRowid==0
147489 && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000)
147490 && (pWin->eStart!=TK_UNBOUNDED87)
147491 ){
147492 sqlite3VdbeAddOp2(v, OP_Null73, 0, pWin->regResult);
147493 sqlite3VdbeAddOp1(v, OP_Last32, pWin->csrApp);
147494 VdbeCoverage(v);
147495 sqlite3VdbeAddOp3(v, OP_Column90, pWin->csrApp, 0, pWin->regResult);
147496 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
147497 }else if( pWin->regApp ){
147498 assert( pMWin->regStartRowid==0 )((void) (0));
147499 }else{
147500 int nArg = windowArgCount(pWin);
147501 if( bFin ){
147502 sqlite3VdbeAddOp2(v, OP_AggFinal157, pWin->regAccum, nArg);
147503 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF(-8));
147504 sqlite3VdbeAddOp2(v, OP_Copy78, pWin->regAccum, pWin->regResult);
147505 sqlite3VdbeAddOp2(v, OP_Null73, 0, pWin->regAccum);
147506 }else{
147507 sqlite3VdbeAddOp3(v, OP_AggValue156,pWin->regAccum,nArg,pWin->regResult);
147508 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF(-8));
147509 }
147510 }
147511 }
147512}
147513
147514/*
147515** Generate code to calculate the current values of all window functions in the
147516** p->pMWin list by doing a full scan of the current window frame. Store the
147517** results in the Window.regResult registers, ready to return the upper
147518** layer.
147519*/
147520static void windowFullScan(WindowCodeArg *p){
147521 Window *pWin;
147522 Parse *pParse = p->pParse;
147523 Window *pMWin = p->pMWin;
147524 Vdbe *v = p->pVdbe;
147525
147526 int regCRowid = 0; /* Current rowid value */
147527 int regCPeer = 0; /* Current peer values */
147528 int regRowid = 0; /* AggStep rowid value */
147529 int regPeer = 0; /* AggStep peer values */
147530
147531 int nPeer;
147532 int lblNext;
147533 int lblBrk;
147534 int addrNext;
147535 int csr = pMWin->csrApp;
147536
147537 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
147538
147539 lblNext = sqlite3VdbeMakeLabel(pParse);
147540 lblBrk = sqlite3VdbeMakeLabel(pParse);
147541
147542 regCRowid = sqlite3GetTempReg(pParse);
147543 regRowid = sqlite3GetTempReg(pParse);
147544 if( nPeer ){
147545 regCPeer = sqlite3GetTempRange(pParse, nPeer);
147546 regPeer = sqlite3GetTempRange(pParse, nPeer);
147547 }
147548
147549 sqlite3VdbeAddOp2(v, OP_Rowid128, pMWin->iEphCsr, regCRowid);
147550 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
147551
147552 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147553 sqlite3VdbeAddOp2(v, OP_Null73, 0, pWin->regAccum);
147554 }
147555
147556 sqlite3VdbeAddOp3(v, OP_SeekGE24, csr, lblBrk, pMWin->regStartRowid);
147557 VdbeCoverage(v);
147558 addrNext = sqlite3VdbeCurrentAddr(v);
147559 sqlite3VdbeAddOp2(v, OP_Rowid128, csr, regRowid);
147560 sqlite3VdbeAddOp3(v, OP_Gt54, pMWin->regEndRowid, lblBrk, regRowid);
147561 VdbeCoverageNeverNull(v);
147562
147563 if( pMWin->eExclude==TK_CURRENT82 ){
147564 sqlite3VdbeAddOp3(v, OP_Eq53, regCRowid, lblNext, regRowid);
147565 VdbeCoverageNeverNull(v);
147566 }else if( pMWin->eExclude!=TK_NO66 ){
147567 int addr;
147568 int addrEq = 0;
147569 KeyInfo *pKeyInfo = 0;
147570
147571 if( pMWin->pOrderBy ){
147572 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
147573 }
147574 if( pMWin->eExclude==TK_TIES91 ){
147575 addrEq = sqlite3VdbeAddOp3(v, OP_Eq53, regCRowid, 0, regRowid);
147576 VdbeCoverageNeverNull(v);
147577 }
147578 if( pKeyInfo ){
147579 windowReadPeerValues(p, csr, regPeer);
147580 sqlite3VdbeAddOp3(v, OP_Compare87, regPeer, regCPeer, nPeer);
147581 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-9));
147582 addr = sqlite3VdbeCurrentAddr(v)+1;
147583 sqlite3VdbeAddOp3(v, OP_Jump16, addr, lblNext, addr);
147584 VdbeCoverageEqNe(v);
147585 }else{
147586 sqlite3VdbeAddOp2(v, OP_Goto11, 0, lblNext);
147587 }
147588 if( addrEq ) sqlite3VdbeJumpHere(v, addrEq);
147589 }
147590
147591 windowAggStep(pParse, pMWin, csr, 0, p->regArg);
147592
147593 sqlite3VdbeResolveLabel(v, lblNext);
147594 sqlite3VdbeAddOp2(v, OP_Next5, csr, addrNext);
147595 VdbeCoverage(v);
147596 sqlite3VdbeJumpHere(v, addrNext-1);
147597 sqlite3VdbeJumpHere(v, addrNext+1);
147598 sqlite3ReleaseTempReg(pParse, regRowid);
147599 sqlite3ReleaseTempReg(pParse, regCRowid);
147600 if( nPeer ){
147601 sqlite3ReleaseTempRange(pParse, regPeer, nPeer);
147602 sqlite3ReleaseTempRange(pParse, regCPeer, nPeer);
147603 }
147604
147605 windowAggFinal(p, 1);
147606}
147607
147608/*
147609** Invoke the sub-routine at regGosub (generated by code in select.c) to
147610** return the current row of Window.iEphCsr. If all window functions are
147611** aggregate window functions that use the standard API, a single
147612** OP_Gosub instruction is all that this routine generates. Extra VM code
147613** for per-row processing is only generated for the following built-in window
147614** functions:
147615**
147616** nth_value()
147617** first_value()
147618** lag()
147619** lead()
147620*/
147621static void windowReturnOneRow(WindowCodeArg *p){
147622 Window *pMWin = p->pMWin;
147623 Vdbe *v = p->pVdbe;
147624
147625 if( pMWin->regStartRowid ){
147626 windowFullScan(p);
147627 }else{
147628 Parse *pParse = p->pParse;
147629 Window *pWin;
147630
147631 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147632 FuncDef *pFunc = pWin->pFunc;
147633 if( pFunc->zName==nth_valueName
147634 || pFunc->zName==first_valueName
147635 ){
147636 int csr = pWin->csrApp;
147637 int lbl = sqlite3VdbeMakeLabel(pParse);
147638 int tmpReg = sqlite3GetTempReg(pParse);
147639 sqlite3VdbeAddOp2(v, OP_Null73, 0, pWin->regResult);
147640
147641 if( pFunc->zName==nth_valueName ){
147642 sqlite3VdbeAddOp3(v, OP_Column90,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
147643 windowCheckValue(pParse, tmpReg, 2);
147644 }else{
147645 sqlite3VdbeAddOp2(v, OP_Integer70, 1, tmpReg);
147646 }
147647 sqlite3VdbeAddOp3(v, OP_Add100, tmpReg, pWin->regApp, tmpReg);
147648 sqlite3VdbeAddOp3(v, OP_Gt54, pWin->regApp+1, lbl, tmpReg);
147649 VdbeCoverageNeverNull(v);
147650 sqlite3VdbeAddOp3(v, OP_SeekRowid30, csr, 0, tmpReg);
147651 VdbeCoverageNeverTaken(v);
147652 sqlite3VdbeAddOp3(v, OP_Column90, csr, pWin->iArgCol, pWin->regResult);
147653 sqlite3VdbeResolveLabel(v, lbl);
147654 sqlite3ReleaseTempReg(pParse, tmpReg);
147655 }
147656 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
147657 int nArg = pWin->pOwner->x.pList->nExpr;
147658 int csr = pWin->csrApp;
147659 int lbl = sqlite3VdbeMakeLabel(pParse);
147660 int tmpReg = sqlite3GetTempReg(pParse);
147661 int iEph = pMWin->iEphCsr;
147662
147663 if( nArg<3 ){
147664 sqlite3VdbeAddOp2(v, OP_Null73, 0, pWin->regResult);
147665 }else{
147666 sqlite3VdbeAddOp3(v, OP_Column90, iEph,pWin->iArgCol+2,pWin->regResult);
147667 }
147668 sqlite3VdbeAddOp2(v, OP_Rowid128, iEph, tmpReg);
147669 if( nArg<2 ){
147670 int val = (pFunc->zName==leadName ? 1 : -1);
147671 sqlite3VdbeAddOp2(v, OP_AddImm83, tmpReg, val);
147672 }else{
147673 int op = (pFunc->zName==leadName ? OP_Add100 : OP_Subtract101);
147674 int tmpReg2 = sqlite3GetTempReg(pParse);
147675 sqlite3VdbeAddOp3(v, OP_Column90, iEph, pWin->iArgCol+1, tmpReg2);
147676 sqlite3VdbeAddOp3(v, op, tmpReg2, tmpReg, tmpReg);
147677 sqlite3ReleaseTempReg(pParse, tmpReg2);
147678 }
147679
147680 sqlite3VdbeAddOp3(v, OP_SeekRowid30, csr, lbl, tmpReg);
147681 VdbeCoverage(v);
147682 sqlite3VdbeAddOp3(v, OP_Column90, csr, pWin->iArgCol, pWin->regResult);
147683 sqlite3VdbeResolveLabel(v, lbl);
147684 sqlite3ReleaseTempReg(pParse, tmpReg);
147685 }
147686 }
147687 }
147688 sqlite3VdbeAddOp2(v, OP_Gosub12, p->regGosub, p->addrGosub);
147689}
147690
147691/*
147692** Generate code to set the accumulator register for each window function
147693** in the linked list passed as the second argument to NULL. And perform
147694** any equivalent initialization required by any built-in window functions
147695** in the list.
147696*/
147697static int windowInitAccum(Parse *pParse, Window *pMWin){
147698 Vdbe *v = sqlite3GetVdbe(pParse);
147699 int regArg;
147700 int nArg = 0;
147701 Window *pWin;
147702 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147703 FuncDef *pFunc = pWin->pFunc;
147704 sqlite3VdbeAddOp2(v, OP_Null73, 0, pWin->regAccum);
147705 nArg = MAX(nArg, windowArgCount(pWin))((nArg)>(windowArgCount(pWin))?(nArg):(windowArgCount(pWin
)))
;
147706 if( pMWin->regStartRowid==0 ){
147707 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
147708 sqlite3VdbeAddOp2(v, OP_Integer70, 0, pWin->regApp);
147709 sqlite3VdbeAddOp2(v, OP_Integer70, 0, pWin->regApp+1);
147710 }
147711
147712 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX0x1000) && pWin->csrApp ){
147713 assert( pWin->eStart!=TK_UNBOUNDED )((void) (0));
147714 sqlite3VdbeAddOp1(v, OP_ResetSorter138, pWin->csrApp);
147715 sqlite3VdbeAddOp2(v, OP_Integer70, 0, pWin->regApp+1);
147716 }
147717 }
147718 }
147719 regArg = pParse->nMem+1;
147720 pParse->nMem += nArg;
147721 return regArg;
147722}
147723
147724/*
147725** Return true if the current frame should be cached in the ephemeral table,
147726** even if there are no xInverse() calls required.
147727*/
147728static int windowCacheFrame(Window *pMWin){
147729 Window *pWin;
147730 if( pMWin->regStartRowid ) return 1;
147731 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147732 FuncDef *pFunc = pWin->pFunc;
147733 if( (pFunc->zName==nth_valueName)
147734 || (pFunc->zName==first_valueName)
147735 || (pFunc->zName==leadName)
147736 || (pFunc->zName==lagName)
147737 ){
147738 return 1;
147739 }
147740 }
147741 return 0;
147742}
147743
147744/*
147745** regOld and regNew are each the first register in an array of size
147746** pOrderBy->nExpr. This function generates code to compare the two
147747** arrays of registers using the collation sequences and other comparison
147748** parameters specified by pOrderBy.
147749**
147750** If the two arrays are not equal, the contents of regNew is copied to
147751** regOld and control falls through. Otherwise, if the contents of the arrays
147752** are equal, an OP_Goto is executed. The address of the OP_Goto is returned.
147753*/
147754static void windowIfNewPeer(
147755 Parse *pParse,
147756 ExprList *pOrderBy,
147757 int regNew, /* First in array of new values */
147758 int regOld, /* First in array of old values */
147759 int addr /* Jump here */
147760){
147761 Vdbe *v = sqlite3GetVdbe(pParse);
147762 if( pOrderBy ){
147763 int nVal = pOrderBy->nExpr;
147764 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pOrderBy, 0, 0);
147765 sqlite3VdbeAddOp3(v, OP_Compare87, regOld, regNew, nVal);
147766 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-9));
147767 sqlite3VdbeAddOp3(v, OP_Jump16,
147768 sqlite3VdbeCurrentAddr(v)+1, addr, sqlite3VdbeCurrentAddr(v)+1
147769 );
147770 VdbeCoverageEqNe(v);
147771 sqlite3VdbeAddOp3(v, OP_Copy78, regNew, regOld, nVal-1);
147772 }else{
147773 sqlite3VdbeAddOp2(v, OP_Goto11, 0, addr);
147774 }
147775}
147776
147777/*
147778** This function is called as part of generating VM programs for RANGE
147779** offset PRECEDING/FOLLOWING frame boundaries. Assuming "ASC" order for
147780** the ORDER BY term in the window, it generates code equivalent to:
147781**
147782** if( csr1.peerVal + regVal >= csr2.peerVal ) goto lbl;
147783**
147784** A special type of arithmetic is used such that if csr.peerVal is not
147785** a numeric type (real or integer), then the result of the addition is
147786** a copy of csr1.peerVal.
147787*/
147788static void windowCodeRangeTest(
147789 WindowCodeArg *p,
147790 int op, /* OP_Ge or OP_Gt */
147791 int csr1,
147792 int regVal,
147793 int csr2,
147794 int lbl
147795){
147796 Parse *pParse = p->pParse;
147797 Vdbe *v = sqlite3GetVdbe(pParse);
147798 int reg1 = sqlite3GetTempReg(pParse);
147799 int reg2 = sqlite3GetTempReg(pParse);
147800 int arith = OP_Add100;
147801 int addrGe;
147802
147803 int regString = ++pParse->nMem;
147804
147805 assert( op==OP_Ge || op==OP_Gt || op==OP_Le )((void) (0));
147806 assert( p->pMWin->pOrderBy && p->pMWin->pOrderBy->nExpr==1 )((void) (0));
147807 if( p->pMWin->pOrderBy->a[0].sortOrder ){
147808 switch( op ){
147809 case OP_Ge57: op = OP_Le55; break;
147810 case OP_Gt54: op = OP_Lt56; break;
147811 default: assert( op==OP_Le )((void) (0)); op = OP_Ge57; break;
147812 }
147813 arith = OP_Subtract101;
147814 }
147815
147816 windowReadPeerValues(p, csr1, reg1);
147817 windowReadPeerValues(p, csr2, reg2);
147818
147819 /* Check if the peer value for csr1 value is a text or blob by comparing
147820 ** it to the smallest possible string - ''. If it is, jump over the
147821 ** OP_Add or OP_Subtract operation and proceed directly to the comparison. */
147822 sqlite3VdbeAddOp4(v, OP_String8110, 0, regString, 0, "", P4_STATIC(-1));
147823 addrGe = sqlite3VdbeAddOp3(v, OP_Ge57, regString, 0, reg1);
147824 VdbeCoverage(v);
147825 sqlite3VdbeAddOp3(v, arith, regVal, reg1, reg1);
147826 sqlite3VdbeJumpHere(v, addrGe);
147827 sqlite3VdbeAddOp3(v, op, reg2, lbl, reg1); VdbeCoverage(v);
147828 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ0x80);
147829 assert( op==OP_Ge || op==OP_Gt || op==OP_Lt || op==OP_Le )((void) (0));
147830 testcase(op==OP_Ge); VdbeCoverageIf(v, op==OP_Ge);
147831 testcase(op==OP_Lt); VdbeCoverageIf(v, op==OP_Lt);
147832 testcase(op==OP_Le); VdbeCoverageIf(v, op==OP_Le);
147833 testcase(op==OP_Gt); VdbeCoverageIf(v, op==OP_Gt);
147834
147835 sqlite3ReleaseTempReg(pParse, reg1);
147836 sqlite3ReleaseTempReg(pParse, reg2);
147837}
147838
147839/*
147840** Helper function for sqlite3WindowCodeStep(). Each call to this function
147841** generates VM code for a single RETURN_ROW, AGGSTEP or AGGINVERSE
147842** operation. Refer to the header comment for sqlite3WindowCodeStep() for
147843** details.
147844*/
147845static int windowCodeOp(
147846 WindowCodeArg *p, /* Context object */
147847 int op, /* WINDOW_RETURN_ROW, AGGSTEP or AGGINVERSE */
147848 int regCountdown, /* Register for OP_IfPos countdown */
147849 int jumpOnEof /* Jump here if stepped cursor reaches EOF */
147850){
147851 int csr, reg;
147852 Parse *pParse = p->pParse;
147853 Window *pMWin = p->pMWin;
147854 int ret = 0;
147855 Vdbe *v = p->pVdbe;
147856 int addrIf = 0;
147857 int addrContinue = 0;
147858 int addrGoto = 0;
147859 int bPeer = (pMWin->eFrmType!=TK_ROWS76);
147860
147861 int lblDone = sqlite3VdbeMakeLabel(pParse);
147862 int addrNextRange = 0;
147863
147864 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
147865 ** starts with UNBOUNDED PRECEDING. */
147866 if( op==WINDOW_AGGINVERSE2 && pMWin->eStart==TK_UNBOUNDED87 ){
147867 assert( regCountdown==0 && jumpOnEof==0 )((void) (0));
147868 return 0;
147869 }
147870
147871 if( regCountdown>0 ){
147872 if( pMWin->eFrmType==TK_RANGE86 ){
147873 addrNextRange = sqlite3VdbeCurrentAddr(v);
147874 assert( op==WINDOW_AGGINVERSE || op==WINDOW_AGGSTEP )((void) (0));
147875 if( op==WINDOW_AGGINVERSE2 ){
147876 if( pMWin->eStart==TK_FOLLOWING83 ){
147877 windowCodeRangeTest(
147878 p, OP_Le55, p->current.csr, regCountdown, p->start.csr, lblDone
147879 );
147880 }else{
147881 windowCodeRangeTest(
147882 p, OP_Ge57, p->start.csr, regCountdown, p->current.csr, lblDone
147883 );
147884 }
147885 }else{
147886 windowCodeRangeTest(
147887 p, OP_Gt54, p->end.csr, regCountdown, p->current.csr, lblDone
147888 );
147889 }
147890 }else{
147891 addrIf = sqlite3VdbeAddOp3(v, OP_IfPos47, regCountdown, 0, 1);
147892 VdbeCoverage(v);
147893 }
147894 }
147895
147896 if( op==WINDOW_RETURN_ROW1 && pMWin->regStartRowid==0 ){
147897 windowAggFinal(p, 0);
147898 }
147899 addrContinue = sqlite3VdbeCurrentAddr(v);
147900 switch( op ){
147901 case WINDOW_RETURN_ROW1:
147902 csr = p->current.csr;
147903 reg = p->current.reg;
147904 windowReturnOneRow(p);
147905 break;
147906
147907 case WINDOW_AGGINVERSE2:
147908 csr = p->start.csr;
147909 reg = p->start.reg;
147910 if( pMWin->regStartRowid ){
147911 assert( pMWin->regEndRowid )((void) (0));
147912 sqlite3VdbeAddOp2(v, OP_AddImm83, pMWin->regStartRowid, 1);
147913 }else{
147914 windowAggStep(pParse, pMWin, csr, 1, p->regArg);
147915 }
147916 break;
147917
147918 default:
147919 assert( op==WINDOW_AGGSTEP )((void) (0));
147920 csr = p->end.csr;
147921 reg = p->end.reg;
147922 if( pMWin->regStartRowid ){
147923 assert( pMWin->regEndRowid )((void) (0));
147924 sqlite3VdbeAddOp2(v, OP_AddImm83, pMWin->regEndRowid, 1);
147925 }else{
147926 windowAggStep(pParse, pMWin, csr, 0, p->regArg);
147927 }
147928 break;
147929 }
147930
147931 if( op==p->eDelete ){
147932 sqlite3VdbeAddOp1(v, OP_Delete123, csr);
147933 sqlite3VdbeChangeP5(v, OPFLAG_SAVEPOSITION0x02);
147934 }
147935
147936 if( jumpOnEof ){
147937 sqlite3VdbeAddOp2(v, OP_Next5, csr, sqlite3VdbeCurrentAddr(v)+2);
147938 VdbeCoverage(v);
147939 ret = sqlite3VdbeAddOp0(v, OP_Goto11);
147940 }else{
147941 sqlite3VdbeAddOp2(v, OP_Next5, csr, sqlite3VdbeCurrentAddr(v)+1+bPeer);
147942 VdbeCoverage(v);
147943 if( bPeer ){
147944 addrGoto = sqlite3VdbeAddOp0(v, OP_Goto11);
147945 }
147946 }
147947
147948 if( bPeer ){
147949 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
147950 int regTmp = (nReg ? sqlite3GetTempRange(pParse, nReg) : 0);
147951 windowReadPeerValues(p, csr, regTmp);
147952 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
147953 sqlite3ReleaseTempRange(pParse, regTmp, nReg);
147954 }
147955
147956 if( addrNextRange ){
147957 sqlite3VdbeAddOp2(v, OP_Goto11, 0, addrNextRange);
147958 }
147959 sqlite3VdbeResolveLabel(v, lblDone);
147960 if( addrGoto ) sqlite3VdbeJumpHere(v, addrGoto);
147961 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
147962 return ret;
147963}
147964
147965
147966/*
147967** Allocate and return a duplicate of the Window object indicated by the
147968** third argument. Set the Window.pOwner field of the new object to
147969** pOwner.
147970*/
147971SQLITE_PRIVATEstatic Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p){
147972 Window *pNew = 0;
147973 if( ALWAYS(p)(p) ){
147974 pNew = sqlite3DbMallocZero(db, sizeof(Window));
147975 if( pNew ){
147976 pNew->zName = sqlite3DbStrDup(db, p->zName);
147977 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
147978 pNew->pFunc = p->pFunc;
147979 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
147980 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
147981 pNew->eFrmType = p->eFrmType;
147982 pNew->eEnd = p->eEnd;
147983 pNew->eStart = p->eStart;
147984 pNew->eExclude = p->eExclude;
147985 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
147986 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
147987 pNew->pOwner = pOwner;
147988 }
147989 }
147990 return pNew;
147991}
147992
147993/*
147994** Return a copy of the linked list of Window objects passed as the
147995** second argument.
147996*/
147997SQLITE_PRIVATEstatic Window *sqlite3WindowListDup(sqlite3 *db, Window *p){
147998 Window *pWin;
147999 Window *pRet = 0;
148000 Window **pp = &pRet;
148001
148002 for(pWin=p; pWin; pWin=pWin->pNextWin){
148003 *pp = sqlite3WindowDup(db, 0, pWin);
148004 if( *pp==0 ) break;
148005 pp = &((*pp)->pNextWin);
148006 }
148007
148008 return pRet;
148009}
148010
148011/*
148012** Return true if it can be determined at compile time that expression
148013** pExpr evaluates to a value that, when cast to an integer, is greater
148014** than zero. False otherwise.
148015**
148016** If an OOM error occurs, this function sets the Parse.db.mallocFailed
148017** flag and returns zero.
148018*/
148019static int windowExprGtZero(Parse *pParse, Expr *pExpr){
148020 int ret = 0;
148021 sqlite3 *db = pParse->db;
148022 sqlite3_value *pVal = 0;
148023 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC'C', &pVal);
148024 if( pVal && sqlite3_value_int(pVal)>0 ){
148025 ret = 1;
148026 }
148027 sqlite3ValueFree(pVal);
148028 return ret;
148029}
148030
148031/*
148032** sqlite3WhereBegin() has already been called for the SELECT statement
148033** passed as the second argument when this function is invoked. It generates
148034** code to populate the Window.regResult register for each window function
148035** and invoke the sub-routine at instruction addrGosub once for each row.
148036** sqlite3WhereEnd() is always called before returning.
148037**
148038** This function handles several different types of window frames, which
148039** require slightly different processing. The following pseudo code is
148040** used to implement window frames of the form:
148041**
148042** ROWS BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
148043**
148044** Other window frame types use variants of the following:
148045**
148046** ... loop started by sqlite3WhereBegin() ...
148047** if( new partition ){
148048** Gosub flush
148049** }
148050** Insert new row into eph table.
148051**
148052** if( first row of partition ){
148053** // Rewind three cursors, all open on the eph table.
148054** Rewind(csrEnd);
148055** Rewind(csrStart);
148056** Rewind(csrCurrent);
148057**
148058** regEnd = <expr2> // FOLLOWING expression
148059** regStart = <expr1> // PRECEDING expression
148060** }else{
148061** // First time this branch is taken, the eph table contains two
148062** // rows. The first row in the partition, which all three cursors
148063** // currently point to, and the following row.
148064** AGGSTEP
148065** if( (regEnd--)<=0 ){
148066** RETURN_ROW
148067** if( (regStart--)<=0 ){
148068** AGGINVERSE
148069** }
148070** }
148071** }
148072** }
148073** flush:
148074** AGGSTEP
148075** while( 1 ){
148076** RETURN ROW
148077** if( csrCurrent is EOF ) break;
148078** if( (regStart--)<=0 ){
148079** AggInverse(csrStart)
148080** Next(csrStart)
148081** }
148082** }
148083**
148084** The pseudo-code above uses the following shorthand:
148085**
148086** AGGSTEP: invoke the aggregate xStep() function for each window function
148087** with arguments read from the current row of cursor csrEnd, then
148088** step cursor csrEnd forward one row (i.e. sqlite3BtreeNext()).
148089**
148090** RETURN_ROW: return a row to the caller based on the contents of the
148091** current row of csrCurrent and the current state of all
148092** aggregates. Then step cursor csrCurrent forward one row.
148093**
148094** AGGINVERSE: invoke the aggregate xInverse() function for each window
148095** functions with arguments read from the current row of cursor
148096** csrStart. Then step csrStart forward one row.
148097**
148098** There are two other ROWS window frames that are handled significantly
148099** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
148100** and "BETWEEN <expr> FOLLOWING AND <expr> FOLLOWING". These are special
148101** cases because they change the order in which the three cursors (csrStart,
148102** csrCurrent and csrEnd) iterate through the ephemeral table. Cases that
148103** use UNBOUNDED or CURRENT ROW are much simpler variations on one of these
148104** three.
148105**
148106** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
148107**
148108** ... loop started by sqlite3WhereBegin() ...
148109** if( new partition ){
148110** Gosub flush
148111** }
148112** Insert new row into eph table.
148113** if( first row of partition ){
148114** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
148115** regEnd = <expr2>
148116** regStart = <expr1>
148117** }else{
148118** if( (regEnd--)<=0 ){
148119** AGGSTEP
148120** }
148121** RETURN_ROW
148122** if( (regStart--)<=0 ){
148123** AGGINVERSE
148124** }
148125** }
148126** }
148127** flush:
148128** if( (regEnd--)<=0 ){
148129** AGGSTEP
148130** }
148131** RETURN_ROW
148132**
148133**
148134** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
148135**
148136** ... loop started by sqlite3WhereBegin() ...
148137** if( new partition ){
148138** Gosub flush
148139** }
148140** Insert new row into eph table.
148141** if( first row of partition ){
148142** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
148143** regEnd = <expr2>
148144** regStart = regEnd - <expr1>
148145** }else{
148146** AGGSTEP
148147** if( (regEnd--)<=0 ){
148148** RETURN_ROW
148149** }
148150** if( (regStart--)<=0 ){
148151** AGGINVERSE
148152** }
148153** }
148154** }
148155** flush:
148156** AGGSTEP
148157** while( 1 ){
148158** if( (regEnd--)<=0 ){
148159** RETURN_ROW
148160** if( eof ) break;
148161** }
148162** if( (regStart--)<=0 ){
148163** AGGINVERSE
148164** if( eof ) break
148165** }
148166** }
148167** while( !eof csrCurrent ){
148168** RETURN_ROW
148169** }
148170**
148171** For the most part, the patterns above are adapted to support UNBOUNDED by
148172** assuming that it is equivalent to "infinity PRECEDING/FOLLOWING" and
148173** CURRENT ROW by assuming that it is equivilent to "0 PRECEDING/FOLLOWING".
148174** This is optimized of course - branches that will never be taken and
148175** conditions that are always true are omitted from the VM code. The only
148176** exceptional case is:
148177**
148178** ROWS BETWEEN <expr1> FOLLOWING AND UNBOUNDED FOLLOWING
148179**
148180** ... loop started by sqlite3WhereBegin() ...
148181** if( new partition ){
148182** Gosub flush
148183** }
148184** Insert new row into eph table.
148185** if( first row of partition ){
148186** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
148187** regStart = <expr1>
148188** }else{
148189** AGGSTEP
148190** }
148191** }
148192** flush:
148193** AGGSTEP
148194** while( 1 ){
148195** if( (regStart--)<=0 ){
148196** AGGINVERSE
148197** if( eof ) break
148198** }
148199** RETURN_ROW
148200** }
148201** while( !eof csrCurrent ){
148202** RETURN_ROW
148203** }
148204**
148205** Also requiring special handling are the cases:
148206**
148207** ROWS BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
148208** ROWS BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
148209**
148210** when (expr1 < expr2). This is detected at runtime, not by this function.
148211** To handle this case, the pseudo-code programs depicted above are modified
148212** slightly to be:
148213**
148214** ... loop started by sqlite3WhereBegin() ...
148215** if( new partition ){
148216** Gosub flush
148217** }
148218** Insert new row into eph table.
148219** if( first row of partition ){
148220** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
148221** regEnd = <expr2>
148222** regStart = <expr1>
148223** if( regEnd < regStart ){
148224** RETURN_ROW
148225** delete eph table contents
148226** continue
148227** }
148228** ...
148229**
148230** The new "continue" statement in the above jumps to the next iteration
148231** of the outer loop - the one started by sqlite3WhereBegin().
148232**
148233** The various GROUPS cases are implemented using the same patterns as
148234** ROWS. The VM code is modified slightly so that:
148235**
148236** 1. The else branch in the main loop is only taken if the row just
148237** added to the ephemeral table is the start of a new group. In
148238** other words, it becomes:
148239**
148240** ... loop started by sqlite3WhereBegin() ...
148241** if( new partition ){
148242** Gosub flush
148243** }
148244** Insert new row into eph table.
148245** if( first row of partition ){
148246** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
148247** regEnd = <expr2>
148248** regStart = <expr1>
148249** }else if( new group ){
148250** ...
148251** }
148252** }
148253**
148254** 2. Instead of processing a single row, each RETURN_ROW, AGGSTEP or
148255** AGGINVERSE step processes the current row of the relevant cursor and
148256** all subsequent rows belonging to the same group.
148257**
148258** RANGE window frames are a little different again. As for GROUPS, the
148259** main loop runs once per group only. And RETURN_ROW, AGGSTEP and AGGINVERSE
148260** deal in groups instead of rows. As for ROWS and GROUPS, there are three
148261** basic cases:
148262**
148263** RANGE BETWEEN <expr1> PRECEDING AND <expr2> FOLLOWING
148264**
148265** ... loop started by sqlite3WhereBegin() ...
148266** if( new partition ){
148267** Gosub flush
148268** }
148269** Insert new row into eph table.
148270** if( first row of partition ){
148271** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
148272** regEnd = <expr2>
148273** regStart = <expr1>
148274** }else{
148275** AGGSTEP
148276** while( (csrCurrent.key + regEnd) < csrEnd.key ){
148277** RETURN_ROW
148278** while( csrStart.key + regStart) < csrCurrent.key ){
148279** AGGINVERSE
148280** }
148281** }
148282** }
148283** }
148284** flush:
148285** AGGSTEP
148286** while( 1 ){
148287** RETURN ROW
148288** if( csrCurrent is EOF ) break;
148289** while( csrStart.key + regStart) < csrCurrent.key ){
148290** AGGINVERSE
148291** }
148292** }
148293** }
148294**
148295** In the above notation, "csr.key" means the current value of the ORDER BY
148296** expression (there is only ever 1 for a RANGE that uses an <expr> FOLLOWING
148297** or <expr PRECEDING) read from cursor csr.
148298**
148299** RANGE BETWEEN <expr1> PRECEDING AND <expr2> PRECEDING
148300**
148301** ... loop started by sqlite3WhereBegin() ...
148302** if( new partition ){
148303** Gosub flush
148304** }
148305** Insert new row into eph table.
148306** if( first row of partition ){
148307** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
148308** regEnd = <expr2>
148309** regStart = <expr1>
148310** }else{
148311** if( (csrEnd.key + regEnd) <= csrCurrent.key ){
148312** AGGSTEP
148313** }
148314** while( (csrStart.key + regStart) < csrCurrent.key ){
148315** AGGINVERSE
148316** }
148317** RETURN_ROW
148318** }
148319** }
148320** flush:
148321** while( (csrEnd.key + regEnd) <= csrCurrent.key ){
148322** AGGSTEP
148323** }
148324** while( (csrStart.key + regStart) < csrCurrent.key ){
148325** AGGINVERSE
148326** }
148327** RETURN_ROW
148328**
148329** RANGE BETWEEN <expr1> FOLLOWING AND <expr2> FOLLOWING
148330**
148331** ... loop started by sqlite3WhereBegin() ...
148332** if( new partition ){
148333** Gosub flush
148334** }
148335** Insert new row into eph table.
148336** if( first row of partition ){
148337** Rewind(csrEnd) ; Rewind(csrStart) ; Rewind(csrCurrent)
148338** regEnd = <expr2>
148339** regStart = <expr1>
148340** }else{
148341** AGGSTEP
148342** while( (csrCurrent.key + regEnd) < csrEnd.key ){
148343** while( (csrCurrent.key + regStart) > csrStart.key ){
148344** AGGINVERSE
148345** }
148346** RETURN_ROW
148347** }
148348** }
148349** }
148350** flush:
148351** AGGSTEP
148352** while( 1 ){
148353** while( (csrCurrent.key + regStart) > csrStart.key ){
148354** AGGINVERSE
148355** if( eof ) break "while( 1 )" loop.
148356** }
148357** RETURN_ROW
148358** }
148359** while( !eof csrCurrent ){
148360** RETURN_ROW
148361** }
148362**
148363** The text above leaves out many details. Refer to the code and comments
148364** below for a more complete picture.
148365*/
148366SQLITE_PRIVATEstatic void sqlite3WindowCodeStep(
148367 Parse *pParse, /* Parse context */
148368 Select *p, /* Rewritten SELECT statement */
148369 WhereInfo *pWInfo, /* Context returned by sqlite3WhereBegin() */
148370 int regGosub, /* Register for OP_Gosub */
148371 int addrGosub /* OP_Gosub here to return each row */
148372){
148373 Window *pMWin = p->pWin;
148374 ExprList *pOrderBy = pMWin->pOrderBy;
148375 Vdbe *v = sqlite3GetVdbe(pParse);
148376 int csrWrite; /* Cursor used to write to eph. table */
148377 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
148378 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
148379 int iInput; /* To iterate through sub cols */
148380 int addrNe; /* Address of OP_Ne */
148381 int addrGosubFlush = 0; /* Address of OP_Gosub to flush: */
148382 int addrInteger = 0; /* Address of OP_Integer */
148383 int addrEmpty; /* Address of OP_Rewind in flush: */
148384 int regStart = 0; /* Value of <expr> PRECEDING */
148385 int regEnd = 0; /* Value of <expr> FOLLOWING */
148386 int regNew; /* Array of registers holding new input row */
148387 int regRecord; /* regNew array in record form */
148388 int regRowid; /* Rowid for regRecord in eph table */
148389 int regNewPeer = 0; /* Peer values for new row (part of regNew) */
148390 int regPeer = 0; /* Peer values for current row */
148391 int regFlushPart = 0; /* Register for "Gosub flush_partition" */
148392 WindowCodeArg s; /* Context object for sub-routines */
148393 int lblWhereEnd; /* Label just before sqlite3WhereEnd() code */
148394
148395 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT((void) (0))
148396 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED((void) (0))
148397 )((void) (0));
148398 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT((void) (0))
148399 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING((void) (0))
148400 )((void) (0));
148401 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT((void) (0))
148402 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES((void) (0))
148403 || pMWin->eExclude==TK_NO((void) (0))
148404 )((void) (0));
148405
148406 lblWhereEnd = sqlite3VdbeMakeLabel(pParse);
148407
148408 /* Fill in the context object */
148409 memset(&s, 0, sizeof(WindowCodeArg));
148410 s.pParse = pParse;
148411 s.pMWin = pMWin;
148412 s.pVdbe = v;
148413 s.regGosub = regGosub;
148414 s.addrGosub = addrGosub;
148415 s.current.csr = pMWin->iEphCsr;
148416 csrWrite = s.current.csr+1;
148417 s.start.csr = s.current.csr+2;
148418 s.end.csr = s.current.csr+3;
148419
148420 /* Figure out when rows may be deleted from the ephemeral table. There
148421 ** are four options - they may never be deleted (eDelete==0), they may
148422 ** be deleted as soon as they are no longer part of the window frame
148423 ** (eDelete==WINDOW_AGGINVERSE), they may be deleted as after the row
148424 ** has been returned to the caller (WINDOW_RETURN_ROW), or they may
148425 ** be deleted after they enter the frame (WINDOW_AGGSTEP). */
148426 switch( pMWin->eStart ){
148427 case TK_FOLLOWING83:
148428 if( pMWin->eFrmType!=TK_RANGE86
148429 && windowExprGtZero(pParse, pMWin->pStart)
148430 ){
148431 s.eDelete = WINDOW_RETURN_ROW1;
148432 }
148433 break;
148434 case TK_UNBOUNDED87:
148435 if( windowCacheFrame(pMWin)==0 ){
148436 if( pMWin->eEnd==TK_PRECEDING85 ){
148437 if( pMWin->eFrmType!=TK_RANGE86
148438 && windowExprGtZero(pParse, pMWin->pEnd)
148439 ){
148440 s.eDelete = WINDOW_AGGSTEP3;
148441 }
148442 }else{
148443 s.eDelete = WINDOW_RETURN_ROW1;
148444 }
148445 }
148446 break;
148447 default:
148448 s.eDelete = WINDOW_AGGINVERSE2;
148449 break;
148450 }
148451
148452 /* Allocate registers for the array of values from the sub-query, the
148453 ** samve values in record form, and the rowid used to insert said record
148454 ** into the ephemeral table. */
148455 regNew = pParse->nMem+1;
148456 pParse->nMem += nInput;
148457 regRecord = ++pParse->nMem;
148458 regRowid = ++pParse->nMem;
148459
148460 /* If the window frame contains an "<expr> PRECEDING" or "<expr> FOLLOWING"
148461 ** clause, allocate registers to store the results of evaluating each
148462 ** <expr>. */
148463 if( pMWin->eStart==TK_PRECEDING85 || pMWin->eStart==TK_FOLLOWING83 ){
148464 regStart = ++pParse->nMem;
148465 }
148466 if( pMWin->eEnd==TK_PRECEDING85 || pMWin->eEnd==TK_FOLLOWING83 ){
148467 regEnd = ++pParse->nMem;
148468 }
148469
148470 /* If this is not a "ROWS BETWEEN ..." frame, then allocate arrays of
148471 ** registers to store copies of the ORDER BY expressions (peer values)
148472 ** for the main loop, and for each cursor (start, current and end). */
148473 if( pMWin->eFrmType!=TK_ROWS76 ){
148474 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
148475 regNewPeer = regNew + pMWin->nBufferCol;
148476 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
148477 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
148478 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
148479 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
148480 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
148481 }
148482
148483 /* Load the column values for the row returned by the sub-select
148484 ** into an array of registers starting at regNew. Assemble them into
148485 ** a record in register regRecord. */
148486 for(iInput=0; iInput<nInput; iInput++){
148487 sqlite3VdbeAddOp3(v, OP_Column90, csrInput, iInput, regNew+iInput);
148488 }
148489 sqlite3VdbeAddOp3(v, OP_MakeRecord92, regNew, nInput, regRecord);
148490
148491 /* An input row has just been read into an array of registers starting
148492 ** at regNew. If the window has a PARTITION clause, this block generates
148493 ** VM code to check if the input row is the start of a new partition.
148494 ** If so, it does an OP_Gosub to an address to be filled in later. The
148495 ** address of the OP_Gosub is stored in local variable addrGosubFlush. */
148496 if( pMWin->pPartition ){
148497 int addr;
148498 ExprList *pPart = pMWin->pPartition;
148499 int nPart = pPart->nExpr;
148500 int regNewPart = regNew + pMWin->nBufferCol;
148501 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pPart, 0, 0);
148502
148503 regFlushPart = ++pParse->nMem;
148504 addr = sqlite3VdbeAddOp3(v, OP_Compare87, regNewPart, pMWin->regPart, nPart);
148505 sqlite3VdbeAppendP4(v, (void*)pKeyInfo, P4_KEYINFO(-9));
148506 sqlite3VdbeAddOp3(v, OP_Jump16, addr+2, addr+4, addr+2);
148507 VdbeCoverageEqNe(v);
148508 addrGosubFlush = sqlite3VdbeAddOp1(v, OP_Gosub12, regFlushPart);
148509 VdbeComment((v, "call flush_partition"));
148510 sqlite3VdbeAddOp3(v, OP_Copy78, regNewPart, pMWin->regPart, nPart-1);
148511 }
148512
148513 /* Insert the new row into the ephemeral table */
148514 sqlite3VdbeAddOp2(v, OP_NewRowid121, csrWrite, regRowid);
148515 sqlite3VdbeAddOp3(v, OP_Insert122, csrWrite, regRecord, regRowid);
148516 addrNe = sqlite3VdbeAddOp3(v, OP_Ne52, pMWin->regOne, 0, regRowid);
148517 VdbeCoverageNeverNull(v);
148518
148519 /* This block is run for the first row of each partition */
148520 s.regArg = windowInitAccum(pParse, pMWin);
148521
148522 if( regStart ){
148523 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
148524 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE86 ? 3 : 0));
148525 }
148526 if( regEnd ){
148527 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
148528 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE86 ? 3 : 0));
148529 }
148530
148531 if( pMWin->eStart==pMWin->eEnd && regStart ){
148532 int op = ((pMWin->eStart==TK_FOLLOWING83) ? OP_Ge57 : OP_Le55);
148533 int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
148534 VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
148535 VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
148536 windowAggFinal(&s, 0);
148537 sqlite3VdbeAddOp2(v, OP_Rewind36, s.current.csr, 1);
148538 VdbeCoverageNeverTaken(v);
148539 windowReturnOneRow(&s);
148540 sqlite3VdbeAddOp1(v, OP_ResetSorter138, s.current.csr);
148541 sqlite3VdbeAddOp2(v, OP_Goto11, 0, lblWhereEnd);
148542 sqlite3VdbeJumpHere(v, addrGe);
148543 }
148544 if( pMWin->eStart==TK_FOLLOWING83 && pMWin->eFrmType!=TK_RANGE86 && regEnd ){
148545 assert( pMWin->eEnd==TK_FOLLOWING )((void) (0));
148546 sqlite3VdbeAddOp3(v, OP_Subtract101, regStart, regEnd, regStart);
148547 }
148548
148549 if( pMWin->eStart!=TK_UNBOUNDED87 ){
148550 sqlite3VdbeAddOp2(v, OP_Rewind36, s.start.csr, 1);
148551 VdbeCoverageNeverTaken(v);
148552 }
148553 sqlite3VdbeAddOp2(v, OP_Rewind36, s.current.csr, 1);
148554 VdbeCoverageNeverTaken(v);
148555 sqlite3VdbeAddOp2(v, OP_Rewind36, s.end.csr, 1);
148556 VdbeCoverageNeverTaken(v);
148557 if( regPeer && pOrderBy ){
148558 sqlite3VdbeAddOp3(v, OP_Copy78, regNewPeer, regPeer, pOrderBy->nExpr-1);
148559 sqlite3VdbeAddOp3(v, OP_Copy78, regPeer, s.start.reg, pOrderBy->nExpr-1);
148560 sqlite3VdbeAddOp3(v, OP_Copy78, regPeer, s.current.reg, pOrderBy->nExpr-1);
148561 sqlite3VdbeAddOp3(v, OP_Copy78, regPeer, s.end.reg, pOrderBy->nExpr-1);
148562 }
148563
148564 sqlite3VdbeAddOp2(v, OP_Goto11, 0, lblWhereEnd);
148565
148566 sqlite3VdbeJumpHere(v, addrNe);
148567
148568 /* Beginning of the block executed for the second and subsequent rows. */
148569 if( regPeer ){
148570 windowIfNewPeer(pParse, pOrderBy, regNewPeer, regPeer, lblWhereEnd);
148571 }
148572 if( pMWin->eStart==TK_FOLLOWING83 ){
148573 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
148574 if( pMWin->eEnd!=TK_UNBOUNDED87 ){
148575 if( pMWin->eFrmType==TK_RANGE86 ){
148576 int lbl = sqlite3VdbeMakeLabel(pParse);
148577 int addrNext = sqlite3VdbeCurrentAddr(v);
148578 windowCodeRangeTest(&s, OP_Ge57, s.current.csr, regEnd, s.end.csr, lbl);
148579 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
148580 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
148581 sqlite3VdbeAddOp2(v, OP_Goto11, 0, addrNext);
148582 sqlite3VdbeResolveLabel(v, lbl);
148583 }else{
148584 windowCodeOp(&s, WINDOW_RETURN_ROW1, regEnd, 0);
148585 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
148586 }
148587 }
148588 }else
148589 if( pMWin->eEnd==TK_PRECEDING85 ){
148590 int bRPS = (pMWin->eStart==TK_PRECEDING85 && pMWin->eFrmType==TK_RANGE86);
148591 windowCodeOp(&s, WINDOW_AGGSTEP3, regEnd, 0);
148592 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
148593 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
148594 if( !bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
148595 }else{
148596 int addr = 0;
148597 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
148598 if( pMWin->eEnd!=TK_UNBOUNDED87 ){
148599 if( pMWin->eFrmType==TK_RANGE86 ){
148600 int lbl = 0;
148601 addr = sqlite3VdbeCurrentAddr(v);
148602 if( regEnd ){
148603 lbl = sqlite3VdbeMakeLabel(pParse);
148604 windowCodeRangeTest(&s, OP_Ge57, s.current.csr, regEnd, s.end.csr, lbl);
148605 }
148606 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
148607 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
148608 if( regEnd ){
148609 sqlite3VdbeAddOp2(v, OP_Goto11, 0, addr);
148610 sqlite3VdbeResolveLabel(v, lbl);
148611 }
148612 }else{
148613 if( regEnd ){
148614 addr = sqlite3VdbeAddOp3(v, OP_IfPos47, regEnd, 0, 1);
148615 VdbeCoverage(v);
148616 }
148617 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
148618 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
148619 if( regEnd ) sqlite3VdbeJumpHere(v, addr);
148620 }
148621 }
148622 }
148623
148624 /* End of the main input loop */
148625 sqlite3VdbeResolveLabel(v, lblWhereEnd);
148626 sqlite3WhereEnd(pWInfo);
148627
148628 /* Fall through */
148629 if( pMWin->pPartition ){
148630 addrInteger = sqlite3VdbeAddOp2(v, OP_Integer70, 0, regFlushPart);
148631 sqlite3VdbeJumpHere(v, addrGosubFlush);
148632 }
148633
148634 addrEmpty = sqlite3VdbeAddOp1(v, OP_Rewind36, csrWrite);
148635 VdbeCoverage(v);
148636 if( pMWin->eEnd==TK_PRECEDING85 ){
148637 int bRPS = (pMWin->eStart==TK_PRECEDING85 && pMWin->eFrmType==TK_RANGE86);
148638 windowCodeOp(&s, WINDOW_AGGSTEP3, regEnd, 0);
148639 if( bRPS ) windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
148640 windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 0);
148641 }else if( pMWin->eStart==TK_FOLLOWING83 ){
148642 int addrStart;
148643 int addrBreak1;
148644 int addrBreak2;
148645 int addrBreak3;
148646 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
148647 if( pMWin->eFrmType==TK_RANGE86 ){
148648 addrStart = sqlite3VdbeCurrentAddr(v);
148649 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 1);
148650 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
148651 }else
148652 if( pMWin->eEnd==TK_UNBOUNDED87 ){
148653 addrStart = sqlite3VdbeCurrentAddr(v);
148654 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, regStart, 1);
148655 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, 0, 1);
148656 }else{
148657 assert( pMWin->eEnd==TK_FOLLOWING )((void) (0));
148658 addrStart = sqlite3VdbeCurrentAddr(v);
148659 addrBreak1 = windowCodeOp(&s, WINDOW_RETURN_ROW1, regEnd, 1);
148660 addrBreak2 = windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 1);
148661 }
148662 sqlite3VdbeAddOp2(v, OP_Goto11, 0, addrStart);
148663 sqlite3VdbeJumpHere(v, addrBreak2);
148664 addrStart = sqlite3VdbeCurrentAddr(v);
148665 addrBreak3 = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
148666 sqlite3VdbeAddOp2(v, OP_Goto11, 0, addrStart);
148667 sqlite3VdbeJumpHere(v, addrBreak1);
148668 sqlite3VdbeJumpHere(v, addrBreak3);
148669 }else{
148670 int addrBreak;
148671 int addrStart;
148672 windowCodeOp(&s, WINDOW_AGGSTEP3, 0, 0);
148673 addrStart = sqlite3VdbeCurrentAddr(v);
148674 addrBreak = windowCodeOp(&s, WINDOW_RETURN_ROW1, 0, 1);
148675 windowCodeOp(&s, WINDOW_AGGINVERSE2, regStart, 0);
148676 sqlite3VdbeAddOp2(v, OP_Goto11, 0, addrStart);
148677 sqlite3VdbeJumpHere(v, addrBreak);
148678 }
148679 sqlite3VdbeJumpHere(v, addrEmpty);
148680
148681 sqlite3VdbeAddOp1(v, OP_ResetSorter138, s.current.csr);
148682 if( pMWin->pPartition ){
148683 if( pMWin->regStartRowid ){
148684 sqlite3VdbeAddOp2(v, OP_Integer70, 1, pMWin->regStartRowid);
148685 sqlite3VdbeAddOp2(v, OP_Integer70, 0, pMWin->regEndRowid);
148686 }
148687 sqlite3VdbeChangeP1(v, addrInteger, sqlite3VdbeCurrentAddr(v));
148688 sqlite3VdbeAddOp1(v, OP_Return66, regFlushPart);
148689 }
148690}
148691
148692#endif /* SQLITE_OMIT_WINDOWFUNC */
148693
148694/************** End of window.c **********************************************/
148695/************** Begin file parse.c *******************************************/
148696/*
148697** 2000-05-29
148698**
148699** The author disclaims copyright to this source code. In place of
148700** a legal notice, here is a blessing:
148701**
148702** May you do good and not evil.
148703** May you find forgiveness for yourself and forgive others.
148704** May you share freely, never taking more than you give.
148705**
148706*************************************************************************
148707** Driver template for the LEMON parser generator.
148708**
148709** The "lemon" program processes an LALR(1) input grammar file, then uses
148710** this template to construct a parser. The "lemon" program inserts text
148711** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
148712** interstitial "-" characters) contained in this template is changed into
148713** the value of the %name directive from the grammar. Otherwise, the content
148714** of this template is copied straight through into the generate parser
148715** source file.
148716**
148717** The following is the concatenation of all %include directives from the
148718** input grammar file:
148719*/
148720/* #include <stdio.h> */
148721/* #include <assert.h> */
148722/************ Begin %include sections from the grammar ************************/
148723
148724/* #include "sqliteInt.h" */
148725
148726/*
148727** Disable all error recovery processing in the parser push-down
148728** automaton.
148729*/
148730#define YYNOERRORRECOVERY1 1
148731
148732/*
148733** Make yytestcase() the same as testcase()
148734*/
148735#define yytestcase(X) testcase(X)
148736
148737/*
148738** Indicate that sqlite3ParserFree() will never be called with a null
148739** pointer.
148740*/
148741#define YYPARSEFREENEVERNULL1 1
148742
148743/*
148744** In the amalgamation, the parse.c file generated by lemon and the
148745** tokenize.c file are concatenated. In that case, sqlite3RunParser()
148746** has access to the the size of the yyParser object and so the parser
148747** engine can be allocated from stack. In that case, only the
148748** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked
148749** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be
148750** omitted.
148751*/
148752#ifdef SQLITE_AMALGAMATION1
148753# define sqlite3Parser_ENGINEALWAYSONSTACK1 1
148754#endif
148755
148756/*
148757** Alternative datatype for the argument to the malloc() routine passed
148758** into sqlite3ParserAlloc(). The default is size_t.
148759*/
148760#define YYMALLOCARGTYPEu64 u64
148761
148762/*
148763** An instance of the following structure describes the event of a
148764** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT,
148765** TK_DELETE, or TK_INSTEAD. If the event is of the form
148766**
148767** UPDATE ON (a,b,c)
148768**
148769** Then the "b" IdList records the list "a,b,c".
148770*/
148771struct TrigEvent { int a; IdList * b; };
148772
148773struct FrameBound { int eType; Expr *pExpr; };
148774
148775/*
148776** Disable lookaside memory allocation for objects that might be
148777** shared across database connections.
148778*/
148779static void disableLookaside(Parse *pParse){
148780 pParse->disableLookaside++;
148781 pParse->db->lookaside.bDisable++;
148782}
148783
148784
148785 /*
148786 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
148787 ** all elements in the list. And make sure list length does not exceed
148788 ** SQLITE_LIMIT_COMPOUND_SELECT.
148789 */
148790 static void parserDoubleLinkSelect(Parse *pParse, Select *p){
148791 if( p->pPrior ){
148792 Select *pNext = 0, *pLoop;
148793 int mxSelect, cnt = 0;
148794 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
148795 pLoop->pNext = pNext;
148796 pLoop->selFlags |= SF_Compound0x00100;
148797 }
148798 if( (p->selFlags & SF_MultiValue0x00400)==0 &&
148799 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT4])>0 &&
148800 cnt>mxSelect
148801 ){
148802 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
148803 }
148804 }
148805 }
148806
148807
148808 /* Construct a new Expr object from a single identifier. Use the
148809 ** new Expr to populate pOut. Set the span of pOut to be the identifier
148810 ** that created the expression.
148811 */
148812 static Expr *tokenExpr(Parse *pParse, int op, Token t){
148813 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
148814 if( p ){
148815 /* memset(p, 0, sizeof(Expr)); */
148816 p->op = (u8)op;
148817 p->affinity = 0;
148818 p->flags = EP_Leaf0x800000;
148819 p->iAgg = -1;
148820 p->pLeft = p->pRight = 0;
148821 p->x.pList = 0;
148822 p->pAggInfo = 0;
148823 p->y.pTab = 0;
148824 p->op2 = 0;
148825 p->iTable = 0;
148826 p->iColumn = 0;
148827 p->u.zToken = (char*)&p[1];
148828 memcpy(p->u.zToken, t.z, t.n);
148829 p->u.zToken[t.n] = 0;
148830 if( sqlite3Isquote(p->u.zToken[0])(sqlite3CtypeMap[(unsigned char)(p->u.zToken[0])]&0x80
)
){
148831 sqlite3DequoteExpr(p);
148832 }
148833#if SQLITE_MAX_EXPR_DEPTH1000>0
148834 p->nHeight = 1;
148835#endif
148836 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
148837 return (Expr*)sqlite3RenameTokenMap(pParse, (void*)p, &t);
148838 }
148839 }
148840 return p;
148841 }
148842
148843
148844 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
148845 ** unary TK_ISNULL or TK_NOTNULL expression. */
148846 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
148847 sqlite3 *db = pParse->db;
148848 if( pA && pY && pY->op==TK_NULL114 && !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
148849 pA->op = (u8)op;
148850 sqlite3ExprDelete(db, pA->pRight);
148851 pA->pRight = 0;
148852 }
148853 }
148854
148855 /* Add a single new term to an ExprList that is used to store a
148856 ** list of identifiers. Report an error if the ID list contains
148857 ** a COLLATE clause or an ASC or DESC keyword, except ignore the
148858 ** error while parsing a legacy schema.
148859 */
148860 static ExprList *parserAddExprIdListTerm(
148861 Parse *pParse,
148862 ExprList *pPrior,
148863 Token *pIdToken,
148864 int hasCollate,
148865 int sortOrder
148866 ){
148867 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0);
148868 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED-1)
148869 && pParse->db->init.busy==0
148870 ){
148871 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"",
148872 pIdToken->n, pIdToken->z);
148873 }
148874 sqlite3ExprListSetName(pParse, p, pIdToken, 1);
148875 return p;
148876 }
148877
148878#if TK_SPAN173>255
148879# error too many tokens in the grammar
148880#endif
148881/**************** End of %include directives **********************************/
148882/* These constants specify the various numeric values for terminal symbols
148883** in a format understandable to "makeheaders". This section is blank unless
148884** "lemon" is run with the "-m" command-line option.
148885***************** Begin makeheaders token definitions *************************/
148886/**************** End makeheaders token definitions ***************************/
148887
148888/* The next sections is a series of control #defines.
148889** various aspects of the generated parser.
148890** YYCODETYPE is the data type used to store the integer codes
148891** that represent terminal and non-terminal symbols.
148892** "unsigned char" is used if there are fewer than
148893** 256 symbols. Larger types otherwise.
148894** YYNOCODE is a number of type YYCODETYPE that is not used for
148895** any terminal or nonterminal symbol.
148896** YYFALLBACK If defined, this indicates that one or more tokens
148897** (also known as: "terminal symbols") have fall-back
148898** values which should be used if the original symbol
148899** would not parse. This permits keywords to sometimes
148900** be used as identifiers, for example.
148901** YYACTIONTYPE is the data type used for "action codes" - numbers
148902** that indicate what to do in response to the next
148903** token.
148904** sqlite3ParserTOKENTYPE is the data type used for minor type for terminal
148905** symbols. Background: A "minor type" is a semantic
148906** value associated with a terminal or non-terminal
148907** symbols. For example, for an "ID" terminal symbol,
148908** the minor type might be the name of the identifier.
148909** Each non-terminal can have a different minor type.
148910** Terminal symbols all have the same minor type, though.
148911** This macros defines the minor type for terminal
148912** symbols.
148913** YYMINORTYPE is the data type used for all minor types.
148914** This is typically a union of many types, one of
148915** which is sqlite3ParserTOKENTYPE. The entry in the union
148916** for terminal symbols is called "yy0".
148917** YYSTACKDEPTH is the maximum depth of the parser's stack. If
148918** zero the stack is dynamically sized using realloc()
148919** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument
148920** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument
148921** sqlite3ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
148922** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser
148923** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser
148924** sqlite3ParserCTX_* As sqlite3ParserARG_ except for %extra_context
148925** YYERRORSYMBOL is the code number of the error symbol. If not
148926** defined, then do no error processing.
148927** YYNSTATE the combined number of states.
148928** YYNRULE the number of rules in the grammar
148929** YYNTOKEN Number of terminal symbols
148930** YY_MAX_SHIFT Maximum value for shift actions
148931** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
148932** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
148933** YY_ERROR_ACTION The yy_action[] code for syntax error
148934** YY_ACCEPT_ACTION The yy_action[] code for accept
148935** YY_NO_ACTION The yy_action[] code for no-op
148936** YY_MIN_REDUCE Minimum value for reduce actions
148937** YY_MAX_REDUCE Maximum value for reduce actions
148938*/
148939#ifndef INTERFACE1
148940# define INTERFACE1 1
148941#endif
148942/************* Begin control #defines *****************************************/
148943#define YYCODETYPEunsigned short int unsigned short int
148944#define YYNOCODE302 302
148945#define YYACTIONTYPEunsigned short int unsigned short int
148946#define YYWILDCARD95 95
148947#define sqlite3ParserTOKENTYPEToken Token
148948typedef union {
148949 int yyinit;
148950 sqlite3ParserTOKENTYPEToken yy0;
148951 TriggerStep* yy11;
148952 IdList* yy76;
148953 ExprList* yy94;
148954 Upsert* yy95;
148955 int yy100;
148956 Expr* yy102;
148957 struct {int value; int mask;} yy199;
148958 u8 yy218;
148959 With* yy243;
148960 struct TrigEvent yy298;
148961 Window* yy379;
148962 struct FrameBound yy389;
148963 Select* yy391;
148964 SrcList* yy407;
148965 const char* yy528;
148966} YYMINORTYPE;
148967#ifndef YYSTACKDEPTH100
148968#define YYSTACKDEPTH100 100
148969#endif
148970#define sqlite3ParserARG_SDECL
148971#define sqlite3ParserARG_PDECL
148972#define sqlite3ParserARG_PARAM
148973#define sqlite3ParserARG_FETCH
148974#define sqlite3ParserARG_STORE
148975#define sqlite3ParserCTX_SDECLParse *pParse; Parse *pParse;
148976#define sqlite3ParserCTX_PDECL,Parse *pParse ,Parse *pParse
148977#define sqlite3ParserCTX_PARAM,pParse ,pParse
148978#define sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse; Parse *pParse=yypParser->pParse;
148979#define sqlite3ParserCTX_STOREyypParser->pParse=pParse; yypParser->pParse=pParse;
148980#define YYFALLBACK1 1
148981#define YYNSTATE540 540
148982#define YYNRULE376 376
148983#define YYNTOKEN176 176
148984#define YY_MAX_SHIFT539 539
148985#define YY_MIN_SHIFTREDUCE783 783
148986#define YY_MAX_SHIFTREDUCE1158 1158
148987#define YY_ERROR_ACTION1159 1159
148988#define YY_ACCEPT_ACTION1160 1160
148989#define YY_NO_ACTION1161 1161
148990#define YY_MIN_REDUCE1162 1162
148991#define YY_MAX_REDUCE1537 1537
148992/************* End control #defines *******************************************/
148993#define YY_NLOOKAHEAD((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0]))) ((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])))
148994
148995/* Define the yytestcase() macro to be a no-op if is not already defined
148996** otherwise.
148997**
148998** Applications can choose to define yytestcase() in the %include section
148999** to a macro that can assist in verifying code coverage. For production
149000** code the yytestcase() macro should be turned off. But it is useful
149001** for testing.
149002*/
149003#ifndef yytestcase
149004# define yytestcase(X)
149005#endif
149006
149007
149008/* Next are the tables used to determine what action to take based on the
149009** current state and lookahead token. These tables are used to implement
149010** functions that take a state number and lookahead value and return an
149011** action integer.
149012**
149013** Suppose the action integer is N. Then the action is determined as
149014** follows
149015**
149016** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead
149017** token onto the stack and goto state N.
149018**
149019** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
149020** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
149021**
149022** N == YY_ERROR_ACTION A syntax error has occurred.
149023**
149024** N == YY_ACCEPT_ACTION The parser accepts its input.
149025**
149026** N == YY_NO_ACTION No such action. Denotes unused
149027** slots in the yy_action[] table.
149028**
149029** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
149030** and YY_MAX_REDUCE
149031**
149032** The action table is constructed as a single large table named yy_action[].
149033** Given state S and lookahead X, the action is computed as either:
149034**
149035** (A) N = yy_action[ yy_shift_ofst[S] + X ]
149036** (B) N = yy_default[S]
149037**
149038** The (A) formula is preferred. The B formula is used instead if
149039** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X.
149040**
149041** The formulas above are for computing the action when the lookahead is
149042** a terminal symbol. If the lookahead is a non-terminal (as occurs after
149043** a reduce action) then the yy_reduce_ofst[] array is used in place of
149044** the yy_shift_ofst[] array.
149045**
149046** The following are the tables generated in this section:
149047**
149048** yy_action[] A single table containing all actions.
149049** yy_lookahead[] A table containing the lookahead for each entry in
149050** yy_action. Used to detect hash collisions.
149051** yy_shift_ofst[] For each state, the offset into yy_action for
149052** shifting terminals.
149053** yy_reduce_ofst[] For each state, the offset into yy_action for
149054** shifting non-terminals after a reduce.
149055** yy_default[] Default action for each state.
149056**
149057*********** Begin parsing tables **********************************************/
149058#define YY_ACTTAB_COUNT(2142) (2142)
149059static const YYACTIONTYPEunsigned short int yy_action[] = {
149060 /* 0 */ 112, 109, 209, 112, 109, 209, 1160, 1, 1, 539,
149061 /* 10 */ 2, 1164, 490, 1193, 1293, 534, 289, 1196, 134, 383,
149062 /* 20 */ 1485, 1428, 1164, 1229, 1208, 1242, 1195, 289, 491, 134,
149063 /* 30 */ 373, 915, 1229, 443, 16, 16, 1242, 70, 70, 916,
149064 /* 40 */ 242, 1292, 296, 119, 120, 110, 1136, 1136, 981, 984,
149065 /* 50 */ 974, 974, 117, 117, 118, 118, 118, 118, 264, 264,
149066 /* 60 */ 190, 264, 264, 264, 264, 112, 109, 209, 362, 264,
149067 /* 70 */ 264, 531, 376, 497, 531, 1134, 531, 1501, 239, 206,
149068 /* 80 */ 338, 9, 531, 242, 219, 1203, 118, 118, 118, 118,
149069 /* 90 */ 111, 439, 112, 109, 209, 219, 116, 116, 116, 116,
149070 /* 100 */ 115, 115, 114, 114, 114, 113, 414, 115, 115, 114,
149071 /* 110 */ 114, 114, 113, 414, 418, 12, 383, 400, 1134, 114,
149072 /* 120 */ 114, 114, 113, 414, 1115, 418, 1134, 1392, 116, 116,
149073 /* 130 */ 116, 116, 115, 115, 114, 114, 114, 113, 414, 961,
149074 /* 140 */ 119, 120, 110, 1136, 1136, 981, 984, 974, 974, 117,
149075 /* 150 */ 117, 118, 118, 118, 118, 952, 534, 414, 941, 951,
149076 /* 160 */ 1481, 539, 2, 1164, 1505, 534, 160, 175, 289, 1134,
149077 /* 170 */ 134, 434, 312, 297, 1115, 1116, 1117, 1242, 70, 70,
149078 /* 180 */ 1089, 338, 1089, 118, 118, 118, 118, 42, 42, 448,
149079 /* 190 */ 951, 951, 953, 116, 116, 116, 116, 115, 115, 114,
149080 /* 200 */ 114, 114, 113, 414, 1115, 311, 264, 264, 82, 441,
149081 /* 210 */ 264, 264, 190, 383, 284, 12, 288, 525, 407, 531,
149082 /* 220 */ 96, 159, 458, 531, 371, 116, 116, 116, 116, 115,
149083 /* 230 */ 115, 114, 114, 114, 113, 414, 219, 119, 120, 110,
149084 /* 240 */ 1136, 1136, 981, 984, 974, 974, 117, 117, 118, 118,
149085 /* 250 */ 118, 118, 511, 1477, 1115, 1116, 1117, 113, 414, 534,
149086 /* 260 */ 528, 528, 528, 121, 534, 1427, 418, 116, 116, 116,
149087 /* 270 */ 116, 115, 115, 114, 114, 114, 113, 414, 1464, 351,
149088 /* 280 */ 270, 42, 42, 383, 187, 1115, 70, 70, 533, 433,
149089 /* 290 */ 116, 116, 116, 116, 115, 115, 114, 114, 114, 113,
149090 /* 300 */ 414, 534, 1339, 405, 159, 411, 410, 119, 120, 110,
149091 /* 310 */ 1136, 1136, 981, 984, 974, 974, 117, 117, 118, 118,
149092 /* 320 */ 118, 118, 285, 42, 42, 349, 411, 410, 514, 479,
149093 /* 330 */ 1458, 79, 1084, 6, 1140, 1115, 1116, 1117, 480, 1142,
149094 /* 340 */ 501, 1115, 1084, 123, 238, 1084, 136, 1141, 1234, 1234,
149095 /* 350 */ 1143, 383, 1143, 1115, 167, 426, 80, 447, 512, 1451,
149096 /* 360 */ 116, 116, 116, 116, 115, 115, 114, 114, 114, 113,
149097 /* 370 */ 414, 1143, 1466, 1143, 350, 119, 120, 110, 1136, 1136,
149098 /* 380 */ 981, 984, 974, 974, 117, 117, 118, 118, 118, 118,
149099 /* 390 */ 402, 1115, 1116, 1117, 500, 534, 250, 267, 336, 474,
149100 /* 400 */ 331, 473, 236, 1115, 1116, 1117, 231, 1115, 329, 471,
149101 /* 410 */ 468, 467, 509, 1458, 1464, 505, 6, 70, 70, 466,
149102 /* 420 */ 181, 380, 379, 534, 971, 971, 982, 985, 116, 116,
149103 /* 430 */ 116, 116, 115, 115, 114, 114, 114, 113, 414, 1115,
149104 /* 440 */ 412, 412, 412, 496, 1115, 69, 69, 235, 383, 288,
149105 /* 450 */ 525, 273, 326, 516, 337, 458, 1084, 1115, 1116, 1117,
149106 /* 460 */ 1232, 1232, 492, 160, 508, 441, 1084, 1067, 1531, 1084,
149107 /* 470 */ 207, 1531, 119, 120, 110, 1136, 1136, 981, 984, 974,
149108 /* 480 */ 974, 117, 117, 118, 118, 118, 118, 881, 534, 1115,
149109 /* 490 */ 1116, 1117, 975, 534, 1115, 1116, 1117, 534, 421, 534,
149110 /* 500 */ 141, 534, 176, 356, 517, 1119, 32, 511, 482, 388,
149111 /* 510 */ 70, 70, 818, 288, 525, 70, 70, 441, 499, 50,
149112 /* 520 */ 50, 70, 70, 70, 70, 116, 116, 116, 116, 115,
149113 /* 530 */ 115, 114, 114, 114, 113, 414, 274, 264, 264, 1115,
149114 /* 540 */ 1065, 264, 264, 1115, 355, 383, 409, 961, 1439, 822,
149115 /* 550 */ 531, 516, 190, 419, 531, 483, 1119, 516, 337, 516,
149116 /* 560 */ 518, 1115, 818, 952, 382, 458, 515, 951, 481, 119,
149117 /* 570 */ 120, 110, 1136, 1136, 981, 984, 974, 974, 117, 117,
149118 /* 580 */ 118, 118, 118, 118, 1338, 278, 1045, 278, 275, 1115,
149119 /* 590 */ 1116, 1117, 259, 1115, 1116, 1117, 534, 5, 951, 951,
149120 /* 600 */ 953, 1046, 231, 3, 143, 471, 468, 467, 1391, 463,
149121 /* 610 */ 1115, 1115, 1116, 1117, 1452, 466, 1047, 836, 70, 70,
149122 /* 620 */ 480, 534, 116, 116, 116, 116, 115, 115, 114, 114,
149123 /* 630 */ 114, 113, 414, 95, 1115, 287, 235, 856, 902, 420,
149124 /* 640 */ 1115, 534, 383, 13, 13, 381, 815, 857, 472, 112,
149125 /* 650 */ 109, 209, 1115, 337, 413, 309, 837, 394, 1436, 534,
149126 /* 660 */ 1115, 1116, 1117, 54, 54, 291, 119, 120, 110, 1136,
149127 /* 670 */ 1136, 981, 984, 974, 974, 117, 117, 118, 118, 118,
149128 /* 680 */ 118, 13, 13, 1084, 1115, 1116, 1117, 901, 264, 264,
149129 /* 690 */ 1115, 1116, 1117, 1084, 292, 399, 1084, 800, 388, 140,
149130 /* 700 */ 295, 531, 1115, 1116, 1117, 403, 447, 532, 534, 870,
149131 /* 710 */ 870, 534, 1240, 534, 329, 534, 1185, 389, 534, 116,
149132 /* 720 */ 116, 116, 116, 115, 115, 114, 114, 114, 113, 414,
149133 /* 730 */ 13, 13, 1024, 13, 13, 13, 13, 13, 13, 383,
149134 /* 740 */ 13, 13, 424, 1100, 401, 264, 264, 277, 160, 184,
149135 /* 750 */ 1182, 185, 1533, 369, 513, 484, 432, 487, 531, 424,
149136 /* 760 */ 423, 1397, 941, 119, 120, 110, 1136, 1136, 981, 984,
149137 /* 770 */ 974, 974, 117, 117, 118, 118, 118, 118, 1397, 1399,
149138 /* 780 */ 425, 519, 392, 264, 264, 1029, 1029, 455, 264, 264,
149139 /* 790 */ 264, 264, 1004, 304, 261, 1278, 531, 900, 288, 525,
149140 /* 800 */ 310, 531, 493, 531, 1067, 1532, 458, 387, 1532, 311,
149141 /* 810 */ 429, 299, 534, 107, 264, 264, 116, 116, 116, 116,
149142 /* 820 */ 115, 115, 114, 114, 114, 113, 414, 531, 424, 1384,
149143 /* 830 */ 507, 258, 258, 1246, 55, 55, 383, 1277, 265, 265,
149144 /* 840 */ 962, 324, 434, 312, 531, 531, 506, 1397, 1026, 1241,
149145 /* 850 */ 298, 531, 1026, 445, 301, 1095, 303, 534, 368, 1156,
149146 /* 860 */ 119, 120, 110, 1136, 1136, 981, 984, 974, 974, 117,
149147 /* 870 */ 117, 118, 118, 118, 118, 1045, 534, 1065, 534, 15,
149148 /* 880 */ 15, 1084, 208, 1324, 453, 452, 534, 1324, 534, 449,
149149 /* 890 */ 1046, 1084, 494, 458, 1084, 234, 233, 232, 44, 44,
149150 /* 900 */ 56, 56, 319, 1095, 322, 1047, 534, 900, 57, 57,
149151 /* 910 */ 58, 58, 534, 116, 116, 116, 116, 115, 115, 114,
149152 /* 920 */ 114, 114, 113, 414, 534, 514, 522, 534, 59, 59,
149153 /* 930 */ 302, 1157, 534, 383, 60, 60, 1237, 946, 788, 789,
149154 /* 940 */ 790, 1459, 1456, 446, 6, 6, 61, 61, 1212, 45,
149155 /* 950 */ 45, 534, 396, 383, 46, 46, 397, 119, 120, 110,
149156 /* 960 */ 1136, 1136, 981, 984, 974, 974, 117, 117, 118, 118,
149157 /* 970 */ 118, 118, 428, 48, 48, 534, 392, 119, 120, 110,
149158 /* 980 */ 1136, 1136, 981, 984, 974, 974, 117, 117, 118, 118,
149159 /* 990 */ 118, 118, 1324, 368, 1066, 447, 825, 49, 49, 534,
149160 /* 1000 */ 458, 357, 534, 353, 534, 138, 534, 337, 1478, 478,
149161 /* 1010 */ 116, 116, 116, 116, 115, 115, 114, 114, 114, 113,
149162 /* 1020 */ 414, 62, 62, 392, 63, 63, 64, 64, 14, 14,
149163 /* 1030 */ 116, 116, 116, 116, 115, 115, 114, 114, 114, 113,
149164 /* 1040 */ 414, 534, 810, 317, 271, 534, 1457, 825, 534, 6,
149165 /* 1050 */ 534, 1324, 534, 142, 534, 1442, 534, 212, 534, 1324,
149166 /* 1060 */ 534, 398, 305, 65, 65, 534, 1157, 125, 125, 476,
149167 /* 1070 */ 66, 66, 51, 51, 67, 67, 68, 68, 52, 52,
149168 /* 1080 */ 147, 147, 148, 148, 534, 98, 534, 75, 75, 276,
149169 /* 1090 */ 534, 272, 534, 810, 534, 876, 534, 527, 389, 534,
149170 /* 1100 */ 875, 534, 1151, 202, 534, 383, 53, 53, 71, 71,
149171 /* 1110 */ 288, 525, 126, 126, 72, 72, 127, 127, 128, 128,
149172 /* 1120 */ 454, 124, 124, 146, 146, 383, 145, 145, 408, 119,
149173 /* 1130 */ 120, 110, 1136, 1136, 981, 984, 974, 974, 117, 117,
149174 /* 1140 */ 118, 118, 118, 118, 534, 900, 534, 95, 534, 119,
149175 /* 1150 */ 120, 110, 1136, 1136, 981, 984, 974, 974, 117, 117,
149176 /* 1160 */ 118, 118, 118, 118, 390, 161, 132, 132, 131, 131,
149177 /* 1170 */ 129, 129, 534, 915, 534, 1455, 534, 1454, 6, 1416,
149178 /* 1180 */ 6, 916, 116, 116, 116, 116, 115, 115, 114, 114,
149179 /* 1190 */ 114, 113, 414, 1415, 130, 130, 74, 74, 76, 76,
149180 /* 1200 */ 534, 30, 116, 116, 116, 116, 115, 115, 114, 114,
149181 /* 1210 */ 114, 113, 414, 534, 263, 206, 534, 1133, 1504, 93,
149182 /* 1220 */ 876, 845, 73, 73, 102, 875, 100, 139, 17, 38,
149183 /* 1230 */ 208, 1062, 31, 450, 370, 43, 43, 101, 47, 47,
149184 /* 1240 */ 827, 216, 436, 308, 943, 440, 95, 241, 241, 442,
149185 /* 1250 */ 313, 464, 241, 95, 237, 900, 327, 383, 266, 95,
149186 /* 1260 */ 835, 834, 193, 335, 938, 314, 1011, 435, 842, 843,
149187 /* 1270 */ 955, 1007, 909, 334, 237, 241, 873, 383, 1023, 107,
149188 /* 1280 */ 1023, 119, 120, 110, 1136, 1136, 981, 984, 974, 974,
149189 /* 1290 */ 117, 117, 118, 118, 118, 118, 1022, 808, 1022, 1274,
149190 /* 1300 */ 137, 119, 108, 110, 1136, 1136, 981, 984, 974, 974,
149191 /* 1310 */ 117, 117, 118, 118, 118, 118, 874, 1011, 318, 107,
149192 /* 1320 */ 321, 955, 323, 325, 1225, 1211, 197, 1210, 1209, 330,
149193 /* 1330 */ 339, 1265, 340, 283, 116, 116, 116, 116, 115, 115,
149194 /* 1340 */ 114, 114, 114, 113, 414, 1286, 1323, 1261, 1471, 1272,
149195 /* 1350 */ 520, 218, 521, 1329, 116, 116, 116, 116, 115, 115,
149196 /* 1360 */ 114, 114, 114, 113, 414, 1192, 1184, 1173, 1172, 1174,
149197 /* 1370 */ 1494, 1488, 459, 256, 383, 1258, 342, 199, 367, 344,
149198 /* 1380 */ 211, 195, 307, 444, 11, 346, 469, 333, 1308, 1316,
149199 /* 1390 */ 375, 427, 203, 360, 383, 1388, 188, 1387, 189, 120,
149200 /* 1400 */ 110, 1136, 1136, 981, 984, 974, 974, 117, 117, 118,
149201 /* 1410 */ 118, 118, 118, 1208, 1151, 300, 348, 1491, 245, 1148,
149202 /* 1420 */ 110, 1136, 1136, 981, 984, 974, 974, 117, 117, 118,
149203 /* 1430 */ 118, 118, 118, 198, 1435, 1433, 524, 78, 391, 163,
149204 /* 1440 */ 82, 1393, 438, 173, 81, 105, 526, 1313, 4, 35,
149205 /* 1450 */ 157, 116, 116, 116, 116, 115, 115, 114, 114, 114,
149206 /* 1460 */ 113, 414, 529, 165, 93, 430, 1305, 168, 169, 431,
149207 /* 1470 */ 462, 116, 116, 116, 116, 115, 115, 114, 114, 114,
149208 /* 1480 */ 113, 414, 170, 171, 221, 415, 372, 437, 1319, 177,
149209 /* 1490 */ 374, 36, 451, 225, 1382, 87, 457, 523, 257, 1404,
149210 /* 1500 */ 316, 105, 526, 227, 4, 182, 460, 160, 320, 228,
149211 /* 1510 */ 377, 1175, 475, 229, 1228, 404, 1227, 1226, 529, 827,
149212 /* 1520 */ 961, 1219, 378, 1200, 1199, 406, 103, 103, 1218, 332,
149213 /* 1530 */ 8, 281, 1198, 104, 1503, 415, 536, 535, 486, 282,
149214 /* 1540 */ 951, 415, 489, 495, 92, 244, 1269, 341, 243, 122,
149215 /* 1550 */ 1270, 343, 514, 523, 1268, 1462, 10, 288, 525, 345,
149216 /* 1560 */ 1461, 354, 99, 352, 503, 94, 1267, 347, 1251, 502,
149217 /* 1570 */ 498, 951, 951, 953, 954, 27, 961, 1250, 194, 358,
149218 /* 1580 */ 251, 359, 103, 103, 1181, 34, 537, 1110, 252, 104,
149219 /* 1590 */ 254, 415, 536, 535, 255, 1368, 951, 1420, 286, 538,
149220 /* 1600 */ 1170, 1165, 1421, 135, 1419, 1418, 149, 150, 279, 784,
149221 /* 1610 */ 416, 196, 151, 290, 210, 200, 77, 385, 269, 386,
149222 /* 1620 */ 133, 162, 935, 1021, 201, 1019, 153, 951, 951, 953,
149223 /* 1630 */ 954, 27, 1480, 1104, 417, 164, 217, 268, 859, 166,
149224 /* 1640 */ 306, 1035, 366, 366, 365, 253, 363, 220, 172, 797,
149225 /* 1650 */ 939, 155, 105, 526, 393, 4, 395, 174, 156, 83,
149226 /* 1660 */ 1038, 84, 213, 85, 294, 222, 86, 223, 1034, 529,
149227 /* 1670 */ 144, 18, 293, 224, 315, 456, 241, 1027, 1145, 178,
149228 /* 1680 */ 226, 179, 37, 799, 334, 461, 230, 465, 470, 838,
149229 /* 1690 */ 180, 88, 415, 19, 280, 328, 20, 89, 90, 158,
149230 /* 1700 */ 191, 477, 215, 1097, 523, 204, 192, 987, 91, 1070,
149231 /* 1710 */ 152, 39, 485, 154, 1071, 503, 40, 488, 205, 260,
149232 /* 1720 */ 504, 262, 105, 526, 214, 4, 908, 961, 183, 240,
149233 /* 1730 */ 903, 107, 1086, 103, 103, 21, 22, 1088, 23, 529,
149234 /* 1740 */ 104, 24, 415, 536, 535, 1090, 1093, 951, 1094, 25,
149235 /* 1750 */ 1074, 33, 7, 26, 510, 1002, 247, 186, 384, 95,
149236 /* 1760 */ 988, 986, 415, 288, 525, 990, 1044, 246, 1043, 991,
149237 /* 1770 */ 28, 41, 530, 956, 523, 809, 106, 29, 951, 951,
149238 /* 1780 */ 953, 954, 27, 869, 361, 503, 422, 248, 364, 1105,
149239 /* 1790 */ 502, 249, 1161, 1496, 1495, 1161, 1161, 961, 1161, 1161,
149240 /* 1800 */ 1161, 1161, 1161, 103, 103, 1161, 1161, 1161, 1161, 1161,
149241 /* 1810 */ 104, 1161, 415, 536, 535, 1104, 417, 951, 1161, 268,
149242 /* 1820 */ 1161, 1161, 1161, 1161, 366, 366, 365, 253, 363, 1161,
149243 /* 1830 */ 1161, 797, 1161, 1161, 1161, 1161, 105, 526, 1161, 4,
149244 /* 1840 */ 1161, 1161, 1161, 1161, 213, 1161, 294, 1161, 951, 951,
149245 /* 1850 */ 953, 954, 27, 529, 293, 1161, 1161, 1161, 1161, 1161,
149246 /* 1860 */ 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
149247 /* 1870 */ 1161, 1161, 1161, 1161, 1161, 1161, 415, 1161, 1161, 1161,
149248 /* 1880 */ 1161, 1161, 1161, 1161, 215, 1161, 1161, 1161, 523, 1161,
149249 /* 1890 */ 1161, 1161, 152, 1161, 1161, 154, 105, 526, 1161, 4,
149250 /* 1900 */ 1161, 1161, 1161, 1161, 1161, 1161, 214, 1161, 1161, 1161,
149251 /* 1910 */ 1161, 961, 1161, 529, 1161, 1161, 1161, 103, 103, 880,
149252 /* 1920 */ 1161, 1161, 1161, 1161, 104, 1161, 415, 536, 535, 1161,
149253 /* 1930 */ 1161, 951, 1161, 1161, 1161, 1161, 415, 1161, 1161, 1161,
149254 /* 1940 */ 384, 1161, 1161, 1161, 1161, 288, 525, 1161, 523, 1161,
149255 /* 1950 */ 1161, 1161, 1161, 1161, 1161, 1161, 97, 526, 1161, 4,
149256 /* 1960 */ 1161, 1161, 951, 951, 953, 954, 27, 1161, 422, 1161,
149257 /* 1970 */ 1161, 961, 1161, 529, 1161, 1161, 1161, 103, 103, 1161,
149258 /* 1980 */ 1161, 1161, 1161, 1161, 104, 1161, 415, 536, 535, 1161,
149259 /* 1990 */ 1161, 951, 268, 1161, 1161, 1161, 415, 366, 366, 365,
149260 /* 2000 */ 253, 363, 1161, 1161, 797, 1161, 1161, 1161, 523, 1161,
149261 /* 2010 */ 1161, 1161, 1161, 1161, 1161, 1161, 1161, 213, 1161, 294,
149262 /* 2020 */ 1161, 1161, 951, 951, 953, 954, 27, 293, 1161, 1161,
149263 /* 2030 */ 1161, 961, 1161, 1161, 1161, 1161, 1161, 103, 103, 1161,
149264 /* 2040 */ 1161, 1161, 1161, 1161, 104, 1161, 415, 536, 535, 1161,
149265 /* 2050 */ 1161, 951, 1161, 1161, 1161, 1161, 1161, 215, 1161, 1161,
149266 /* 2060 */ 1161, 1161, 1161, 1161, 1161, 152, 1161, 1161, 154, 1161,
149267 /* 2070 */ 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 214,
149268 /* 2080 */ 1161, 1161, 951, 951, 953, 954, 27, 1161, 1161, 1161,
149269 /* 2090 */ 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
149270 /* 2100 */ 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
149271 /* 2110 */ 1161, 1161, 1161, 384, 1161, 1161, 1161, 1161, 288, 525,
149272 /* 2120 */ 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
149273 /* 2130 */ 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161, 1161,
149274 /* 2140 */ 1161, 422,
149275};
149276static const YYCODETYPEunsigned short int yy_lookahead[] = {
149277 /* 0 */ 260, 261, 262, 260, 261, 262, 176, 177, 178, 179,
149278 /* 10 */ 180, 181, 184, 206, 209, 184, 186, 206, 188, 19,
149279 /* 20 */ 179, 281, 181, 213, 214, 195, 206, 186, 195, 188,
149280 /* 30 */ 195, 31, 222, 184, 206, 207, 195, 206, 207, 39,
149281 /* 40 */ 24, 209, 184, 43, 44, 45, 46, 47, 48, 49,
149282 /* 50 */ 50, 51, 52, 53, 54, 55, 56, 57, 228, 229,
149283 /* 60 */ 184, 228, 229, 228, 229, 260, 261, 262, 192, 228,
149284 /* 70 */ 229, 241, 196, 242, 241, 59, 241, 205, 245, 246,
149285 /* 80 */ 184, 22, 241, 24, 254, 213, 54, 55, 56, 57,
149286 /* 90 */ 58, 256, 260, 261, 262, 254, 96, 97, 98, 99,
149287 /* 100 */ 100, 101, 102, 103, 104, 105, 106, 100, 101, 102,
149288 /* 110 */ 103, 104, 105, 106, 284, 203, 19, 221, 59, 102,
149289 /* 120 */ 103, 104, 105, 106, 59, 284, 110, 269, 96, 97,
149290 /* 130 */ 98, 99, 100, 101, 102, 103, 104, 105, 106, 94,
149291 /* 140 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
149292 /* 150 */ 53, 54, 55, 56, 57, 110, 184, 106, 73, 114,
149293 /* 160 */ 178, 179, 180, 181, 219, 184, 81, 22, 186, 110,
149294 /* 170 */ 188, 121, 122, 195, 109, 110, 111, 195, 206, 207,
149295 /* 180 */ 83, 184, 85, 54, 55, 56, 57, 206, 207, 277,
149296 /* 190 */ 145, 146, 147, 96, 97, 98, 99, 100, 101, 102,
149297 /* 200 */ 103, 104, 105, 106, 59, 120, 228, 229, 143, 184,
149298 /* 210 */ 228, 229, 184, 19, 242, 203, 131, 132, 221, 241,
149299 /* 220 */ 26, 184, 184, 241, 196, 96, 97, 98, 99, 100,
149300 /* 230 */ 101, 102, 103, 104, 105, 106, 254, 43, 44, 45,
149301 /* 240 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
149302 /* 250 */ 56, 57, 184, 184, 109, 110, 111, 105, 106, 184,
149303 /* 260 */ 200, 201, 202, 69, 184, 227, 284, 96, 97, 98,
149304 /* 270 */ 99, 100, 101, 102, 103, 104, 105, 106, 297, 298,
149305 /* 280 */ 255, 206, 207, 19, 272, 59, 206, 207, 184, 277,
149306 /* 290 */ 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
149307 /* 300 */ 106, 184, 259, 19, 184, 100, 101, 43, 44, 45,
149308 /* 310 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
149309 /* 320 */ 56, 57, 242, 206, 207, 184, 100, 101, 138, 292,
149310 /* 330 */ 293, 67, 76, 296, 108, 109, 110, 111, 295, 113,
149311 /* 340 */ 84, 59, 86, 22, 26, 89, 156, 121, 224, 225,
149312 /* 350 */ 145, 19, 147, 59, 72, 256, 24, 184, 290, 291,
149313 /* 360 */ 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
149314 /* 370 */ 106, 145, 297, 147, 299, 43, 44, 45, 46, 47,
149315 /* 380 */ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
149316 /* 390 */ 106, 109, 110, 111, 138, 184, 112, 113, 114, 115,
149317 /* 400 */ 116, 117, 118, 109, 110, 111, 112, 59, 124, 115,
149318 /* 410 */ 116, 117, 292, 293, 297, 298, 296, 206, 207, 125,
149319 /* 420 */ 72, 100, 101, 184, 46, 47, 48, 49, 96, 97,
149320 /* 430 */ 98, 99, 100, 101, 102, 103, 104, 105, 106, 59,
149321 /* 440 */ 200, 201, 202, 184, 59, 206, 207, 46, 19, 131,
149322 /* 450 */ 132, 278, 23, 242, 184, 184, 76, 109, 110, 111,
149323 /* 460 */ 224, 225, 251, 81, 84, 184, 86, 22, 23, 89,
149324 /* 470 */ 184, 26, 43, 44, 45, 46, 47, 48, 49, 50,
149325 /* 480 */ 51, 52, 53, 54, 55, 56, 57, 102, 184, 109,
149326 /* 490 */ 110, 111, 114, 184, 109, 110, 111, 184, 227, 184,
149327 /* 500 */ 230, 184, 22, 264, 195, 59, 22, 184, 195, 108,
149328 /* 510 */ 206, 207, 59, 131, 132, 206, 207, 184, 138, 206,
149329 /* 520 */ 207, 206, 207, 206, 207, 96, 97, 98, 99, 100,
149330 /* 530 */ 101, 102, 103, 104, 105, 106, 255, 228, 229, 59,
149331 /* 540 */ 95, 228, 229, 59, 184, 19, 242, 94, 184, 23,
149332 /* 550 */ 241, 242, 184, 282, 241, 242, 110, 242, 184, 242,
149333 /* 560 */ 251, 59, 109, 110, 196, 184, 251, 114, 251, 43,
149334 /* 570 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
149335 /* 580 */ 54, 55, 56, 57, 259, 217, 12, 219, 255, 109,
149336 /* 590 */ 110, 111, 203, 109, 110, 111, 184, 22, 145, 146,
149337 /* 600 */ 147, 27, 112, 22, 230, 115, 116, 117, 227, 19,
149338 /* 610 */ 59, 109, 110, 111, 291, 125, 42, 35, 206, 207,
149339 /* 620 */ 295, 184, 96, 97, 98, 99, 100, 101, 102, 103,
149340 /* 630 */ 104, 105, 106, 26, 59, 233, 46, 63, 136, 184,
149341 /* 640 */ 59, 184, 19, 206, 207, 243, 23, 73, 66, 260,
149342 /* 650 */ 261, 262, 59, 184, 242, 195, 74, 220, 184, 184,
149343 /* 660 */ 109, 110, 111, 206, 207, 184, 43, 44, 45, 46,
149344 /* 670 */ 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
149345 /* 680 */ 57, 206, 207, 76, 109, 110, 111, 136, 228, 229,
149346 /* 690 */ 109, 110, 111, 86, 184, 220, 89, 21, 108, 230,
149347 /* 700 */ 184, 241, 109, 110, 111, 123, 184, 127, 184, 129,
149348 /* 710 */ 130, 184, 195, 184, 124, 184, 198, 199, 184, 96,
149349 /* 720 */ 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
149350 /* 730 */ 206, 207, 11, 206, 207, 206, 207, 206, 207, 19,
149351 /* 740 */ 206, 207, 184, 23, 220, 228, 229, 220, 81, 220,
149352 /* 750 */ 195, 220, 287, 288, 220, 195, 80, 195, 241, 201,
149353 /* 760 */ 202, 184, 73, 43, 44, 45, 46, 47, 48, 49,
149354 /* 770 */ 50, 51, 52, 53, 54, 55, 56, 57, 201, 202,
149355 /* 780 */ 113, 195, 184, 228, 229, 120, 121, 122, 228, 229,
149356 /* 790 */ 228, 229, 116, 16, 23, 184, 241, 26, 131, 132,
149357 /* 800 */ 278, 241, 19, 241, 22, 23, 184, 189, 26, 120,
149358 /* 810 */ 121, 122, 184, 26, 228, 229, 96, 97, 98, 99,
149359 /* 820 */ 100, 101, 102, 103, 104, 105, 106, 241, 270, 153,
149360 /* 830 */ 66, 228, 229, 229, 206, 207, 19, 184, 228, 229,
149361 /* 840 */ 23, 16, 121, 122, 241, 241, 82, 270, 29, 227,
149362 /* 850 */ 252, 241, 33, 19, 77, 91, 79, 184, 22, 23,
149363 /* 860 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
149364 /* 870 */ 53, 54, 55, 56, 57, 12, 184, 95, 184, 206,
149365 /* 880 */ 207, 76, 111, 184, 65, 267, 184, 184, 184, 271,
149366 /* 890 */ 27, 86, 109, 184, 89, 120, 121, 122, 206, 207,
149367 /* 900 */ 206, 207, 77, 139, 79, 42, 184, 136, 206, 207,
149368 /* 910 */ 206, 207, 184, 96, 97, 98, 99, 100, 101, 102,
149369 /* 920 */ 103, 104, 105, 106, 184, 138, 63, 184, 206, 207,
149370 /* 930 */ 153, 95, 184, 19, 206, 207, 227, 23, 7, 8,
149371 /* 940 */ 9, 293, 293, 109, 296, 296, 206, 207, 215, 206,
149372 /* 950 */ 207, 184, 253, 19, 206, 207, 253, 43, 44, 45,
149373 /* 960 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
149374 /* 970 */ 56, 57, 184, 206, 207, 184, 184, 43, 44, 45,
149375 /* 980 */ 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
149376 /* 990 */ 56, 57, 184, 22, 23, 184, 59, 206, 207, 184,
149377 /* 1000 */ 184, 238, 184, 240, 184, 22, 184, 184, 157, 158,
149378 /* 1010 */ 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
149379 /* 1020 */ 106, 206, 207, 184, 206, 207, 206, 207, 206, 207,
149380 /* 1030 */ 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
149381 /* 1040 */ 106, 184, 59, 227, 252, 184, 293, 110, 184, 296,
149382 /* 1050 */ 184, 184, 184, 230, 184, 184, 184, 15, 184, 184,
149383 /* 1060 */ 184, 253, 184, 206, 207, 184, 95, 206, 207, 102,
149384 /* 1070 */ 206, 207, 206, 207, 206, 207, 206, 207, 206, 207,
149385 /* 1080 */ 206, 207, 206, 207, 184, 151, 184, 206, 207, 278,
149386 /* 1090 */ 184, 252, 184, 110, 184, 128, 184, 198, 199, 184,
149387 /* 1100 */ 133, 184, 60, 26, 184, 19, 206, 207, 206, 207,
149388 /* 1110 */ 131, 132, 206, 207, 206, 207, 206, 207, 206, 207,
149389 /* 1120 */ 253, 206, 207, 206, 207, 19, 206, 207, 253, 43,
149390 /* 1130 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
149391 /* 1140 */ 54, 55, 56, 57, 184, 26, 184, 26, 184, 43,
149392 /* 1150 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
149393 /* 1160 */ 54, 55, 56, 57, 285, 286, 206, 207, 206, 207,
149394 /* 1170 */ 206, 207, 184, 31, 184, 293, 184, 293, 296, 184,
149395 /* 1180 */ 296, 39, 96, 97, 98, 99, 100, 101, 102, 103,
149396 /* 1190 */ 104, 105, 106, 184, 206, 207, 206, 207, 206, 207,
149397 /* 1200 */ 184, 22, 96, 97, 98, 99, 100, 101, 102, 103,
149398 /* 1210 */ 104, 105, 106, 184, 245, 246, 184, 26, 23, 142,
149399 /* 1220 */ 128, 26, 206, 207, 150, 133, 152, 22, 22, 24,
149400 /* 1230 */ 111, 23, 53, 184, 26, 206, 207, 151, 206, 207,
149401 /* 1240 */ 119, 24, 122, 23, 23, 23, 26, 26, 26, 23,
149402 /* 1250 */ 23, 23, 26, 26, 26, 136, 23, 19, 22, 26,
149403 /* 1260 */ 113, 114, 24, 114, 144, 184, 59, 61, 7, 8,
149404 /* 1270 */ 59, 23, 23, 124, 26, 26, 23, 19, 145, 26,
149405 /* 1280 */ 147, 43, 44, 45, 46, 47, 48, 49, 50, 51,
149406 /* 1290 */ 52, 53, 54, 55, 56, 57, 145, 23, 147, 184,
149407 /* 1300 */ 26, 43, 44, 45, 46, 47, 48, 49, 50, 51,
149408 /* 1310 */ 52, 53, 54, 55, 56, 57, 23, 110, 184, 26,
149409 /* 1320 */ 184, 110, 184, 184, 184, 215, 135, 215, 184, 184,
149410 /* 1330 */ 184, 247, 184, 244, 96, 97, 98, 99, 100, 101,
149411 /* 1340 */ 102, 103, 104, 105, 106, 184, 184, 184, 301, 184,
149412 /* 1350 */ 184, 134, 225, 184, 96, 97, 98, 99, 100, 101,
149413 /* 1360 */ 102, 103, 104, 105, 106, 184, 184, 184, 184, 184,
149414 /* 1370 */ 134, 184, 274, 273, 19, 244, 244, 204, 182, 244,
149415 /* 1380 */ 283, 231, 279, 279, 232, 244, 210, 209, 235, 235,
149416 /* 1390 */ 235, 248, 218, 234, 19, 209, 238, 209, 238, 44,
149417 /* 1400 */ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
149418 /* 1410 */ 55, 56, 57, 214, 60, 248, 248, 187, 134, 38,
149419 /* 1420 */ 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
149420 /* 1430 */ 55, 56, 57, 232, 191, 191, 266, 280, 191, 283,
149421 /* 1440 */ 143, 269, 108, 22, 280, 19, 20, 258, 22, 257,
149422 /* 1450 */ 43, 96, 97, 98, 99, 100, 101, 102, 103, 104,
149423 /* 1460 */ 105, 106, 36, 223, 142, 18, 235, 226, 226, 191,
149424 /* 1470 */ 18, 96, 97, 98, 99, 100, 101, 102, 103, 104,
149425 /* 1480 */ 105, 106, 226, 226, 190, 59, 235, 235, 223, 223,
149426 /* 1490 */ 258, 257, 191, 190, 235, 150, 62, 71, 191, 276,
149427 /* 1500 */ 275, 19, 20, 190, 22, 22, 211, 81, 191, 190,
149428 /* 1510 */ 211, 191, 108, 190, 208, 64, 208, 208, 36, 119,
149429 /* 1520 */ 94, 216, 211, 208, 210, 106, 100, 101, 216, 208,
149430 /* 1530 */ 48, 268, 208, 107, 208, 109, 110, 111, 211, 268,
149431 /* 1540 */ 114, 59, 211, 137, 108, 88, 250, 249, 191, 141,
149432 /* 1550 */ 250, 249, 138, 71, 250, 300, 22, 131, 132, 249,
149433 /* 1560 */ 300, 191, 150, 238, 82, 140, 250, 249, 239, 87,
149434 /* 1570 */ 139, 145, 146, 147, 148, 149, 94, 239, 237, 236,
149435 /* 1580 */ 25, 235, 100, 101, 194, 26, 193, 13, 185, 107,
149436 /* 1590 */ 185, 109, 110, 111, 6, 263, 114, 203, 265, 183,
149437 /* 1600 */ 183, 183, 203, 212, 203, 203, 197, 197, 212, 4,
149438 /* 1610 */ 3, 22, 197, 155, 15, 204, 203, 289, 93, 289,
149439 /* 1620 */ 16, 286, 132, 23, 204, 23, 123, 145, 146, 147,
149440 /* 1630 */ 148, 149, 0, 1, 2, 143, 24, 5, 20, 135,
149441 /* 1640 */ 16, 1, 10, 11, 12, 13, 14, 137, 135, 17,
149442 /* 1650 */ 144, 123, 19, 20, 61, 22, 37, 143, 123, 53,
149443 /* 1660 */ 109, 53, 30, 53, 32, 34, 53, 134, 1, 36,
149444 /* 1670 */ 5, 22, 40, 108, 153, 41, 26, 68, 75, 68,
149445 /* 1680 */ 134, 108, 24, 20, 124, 19, 118, 67, 67, 28,
149446 /* 1690 */ 22, 22, 59, 22, 67, 23, 22, 22, 142, 37,
149447 /* 1700 */ 23, 22, 70, 23, 71, 157, 23, 23, 26, 23,
149448 /* 1710 */ 78, 22, 24, 81, 23, 82, 22, 24, 134, 23,
149449 /* 1720 */ 87, 23, 19, 20, 92, 22, 109, 94, 22, 34,
149450 /* 1730 */ 136, 26, 85, 100, 101, 34, 34, 83, 34, 36,
149451 /* 1740 */ 107, 34, 109, 110, 111, 75, 90, 114, 75, 34,
149452 /* 1750 */ 23, 22, 44, 34, 24, 23, 22, 26, 126, 26,
149453 /* 1760 */ 23, 23, 59, 131, 132, 23, 23, 26, 23, 11,
149454 /* 1770 */ 22, 22, 26, 23, 71, 23, 22, 22, 145, 146,
149455 /* 1780 */ 147, 148, 149, 128, 23, 82, 154, 134, 15, 1,
149456 /* 1790 */ 87, 134, 302, 134, 134, 302, 302, 94, 302, 302,
149457 /* 1800 */ 302, 302, 302, 100, 101, 302, 302, 302, 302, 302,
149458 /* 1810 */ 107, 302, 109, 110, 111, 1, 2, 114, 302, 5,
149459 /* 1820 */ 302, 302, 302, 302, 10, 11, 12, 13, 14, 302,
149460 /* 1830 */ 302, 17, 302, 302, 302, 302, 19, 20, 302, 22,
149461 /* 1840 */ 302, 302, 302, 302, 30, 302, 32, 302, 145, 146,
149462 /* 1850 */ 147, 148, 149, 36, 40, 302, 302, 302, 302, 302,
149463 /* 1860 */ 302, 302, 302, 302, 302, 302, 302, 302, 302, 302,
149464 /* 1870 */ 302, 302, 302, 302, 302, 302, 59, 302, 302, 302,
149465 /* 1880 */ 302, 302, 302, 302, 70, 302, 302, 302, 71, 302,
149466 /* 1890 */ 302, 302, 78, 302, 302, 81, 19, 20, 302, 22,
149467 /* 1900 */ 302, 302, 302, 302, 302, 302, 92, 302, 302, 302,
149468 /* 1910 */ 302, 94, 302, 36, 302, 302, 302, 100, 101, 102,
149469 /* 1920 */ 302, 302, 302, 302, 107, 302, 109, 110, 111, 302,
149470 /* 1930 */ 302, 114, 302, 302, 302, 302, 59, 302, 302, 302,
149471 /* 1940 */ 126, 302, 302, 302, 302, 131, 132, 302, 71, 302,
149472 /* 1950 */ 302, 302, 302, 302, 302, 302, 19, 20, 302, 22,
149473 /* 1960 */ 302, 302, 145, 146, 147, 148, 149, 302, 154, 302,
149474 /* 1970 */ 302, 94, 302, 36, 302, 302, 302, 100, 101, 302,
149475 /* 1980 */ 302, 302, 302, 302, 107, 302, 109, 110, 111, 302,
149476 /* 1990 */ 302, 114, 5, 302, 302, 302, 59, 10, 11, 12,
149477 /* 2000 */ 13, 14, 302, 302, 17, 302, 302, 302, 71, 302,
149478 /* 2010 */ 302, 302, 302, 302, 302, 302, 302, 30, 302, 32,
149479 /* 2020 */ 302, 302, 145, 146, 147, 148, 149, 40, 302, 302,
149480 /* 2030 */ 302, 94, 302, 302, 302, 302, 302, 100, 101, 302,
149481 /* 2040 */ 302, 302, 302, 302, 107, 302, 109, 110, 111, 302,
149482 /* 2050 */ 302, 114, 302, 302, 302, 302, 302, 70, 302, 302,
149483 /* 2060 */ 302, 302, 302, 302, 302, 78, 302, 302, 81, 302,
149484 /* 2070 */ 302, 302, 302, 302, 302, 302, 302, 302, 302, 92,
149485 /* 2080 */ 302, 302, 145, 146, 147, 148, 149, 302, 302, 302,
149486 /* 2090 */ 302, 302, 302, 302, 302, 302, 302, 302, 302, 302,
149487 /* 2100 */ 302, 302, 302, 302, 302, 302, 302, 302, 302, 302,
149488 /* 2110 */ 302, 302, 302, 126, 302, 302, 302, 302, 131, 132,
149489 /* 2120 */ 302, 302, 302, 302, 302, 302, 302, 302, 302, 302,
149490 /* 2130 */ 302, 302, 302, 302, 302, 302, 302, 302, 302, 302,
149491 /* 2140 */ 302, 154, 302, 302, 302, 302, 302, 302, 302, 302,
149492 /* 2150 */ 302, 302, 302, 302, 302, 302, 302, 302, 302, 302,
149493 /* 2160 */ 302, 302, 302, 302, 302, 302, 302, 302, 302,
149494};
149495#define YY_SHIFT_COUNT(539) (539)
149496#define YY_SHIFT_MIN(0) (0)
149497#define YY_SHIFT_MAX(1987) (1987)
149498static const unsigned short int yy_shift_ofst[] = {
149499 /* 0 */ 1814, 1632, 1987, 1426, 1426, 382, 1482, 1633, 1703, 1877,
149500 /* 10 */ 1877, 1877, 85, 0, 0, 264, 1106, 1877, 1877, 1877,
149501 /* 20 */ 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
149502 /* 30 */ 226, 226, 380, 380, 294, 667, 382, 382, 382, 382,
149503 /* 40 */ 382, 382, 97, 194, 332, 429, 526, 623, 720, 817,
149504 /* 50 */ 914, 934, 1086, 1238, 1106, 1106, 1106, 1106, 1106, 1106,
149505 /* 60 */ 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106, 1106,
149506 /* 70 */ 1106, 1106, 1258, 1106, 1355, 1375, 1375, 1817, 1877, 1877,
149507 /* 80 */ 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
149508 /* 90 */ 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
149509 /* 100 */ 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
149510 /* 110 */ 1937, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877, 1877,
149511 /* 120 */ 1877, 1877, 1877, 1877, 32, 129, 129, 129, 129, 129,
149512 /* 130 */ 171, 7, 17, 593, 676, 590, 593, 205, 205, 593,
149513 /* 140 */ 318, 318, 318, 318, 50, 152, 51, 2142, 2142, 284,
149514 /* 150 */ 284, 284, 65, 145, 282, 145, 145, 574, 574, 256,
149515 /* 160 */ 348, 445, 782, 593, 593, 593, 593, 593, 593, 593,
149516 /* 170 */ 593, 593, 593, 593, 593, 593, 593, 593, 593, 593,
149517 /* 180 */ 593, 593, 593, 593, 607, 607, 593, 721, 805, 805,
149518 /* 190 */ 446, 851, 851, 446, 190, 979, 2142, 2142, 2142, 453,
149519 /* 200 */ 45, 45, 480, 490, 484, 385, 575, 502, 551, 581,
149520 /* 210 */ 593, 593, 593, 593, 593, 593, 593, 593, 593, 689,
149521 /* 220 */ 593, 593, 593, 593, 593, 593, 593, 593, 593, 593,
149522 /* 230 */ 593, 593, 582, 582, 582, 593, 593, 593, 593, 771,
149523 /* 240 */ 593, 593, 593, 59, 764, 593, 593, 863, 593, 593,
149524 /* 250 */ 593, 593, 593, 593, 593, 593, 665, 819, 580, 16,
149525 /* 260 */ 16, 16, 16, 1119, 580, 580, 967, 321, 931, 1042,
149526 /* 270 */ 1077, 783, 783, 834, 1077, 1077, 834, 1121, 1195, 401,
149527 /* 280 */ 1142, 1142, 1142, 783, 787, 787, 1074, 1191, 1092, 1205,
149528 /* 290 */ 1354, 1284, 1284, 1381, 1381, 1284, 1297, 1334, 1421, 1407,
149529 /* 300 */ 1322, 1447, 1447, 1447, 1447, 1284, 1452, 1322, 1322, 1334,
149530 /* 310 */ 1421, 1407, 1407, 1322, 1284, 1452, 1345, 1434, 1284, 1452,
149531 /* 320 */ 1483, 1284, 1452, 1284, 1452, 1483, 1404, 1404, 1404, 1451,
149532 /* 330 */ 1483, 1404, 1400, 1404, 1451, 1404, 1404, 1483, 1419, 1419,
149533 /* 340 */ 1483, 1406, 1436, 1406, 1436, 1406, 1436, 1406, 1436, 1284,
149534 /* 350 */ 1457, 1457, 1408, 1414, 1534, 1284, 1412, 1408, 1425, 1431,
149535 /* 360 */ 1322, 1555, 1559, 1574, 1574, 1588, 1588, 1588, 2142, 2142,
149536 /* 370 */ 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142, 2142,
149537 /* 380 */ 2142, 2142, 2142, 378, 777, 836, 971, 825, 775, 983,
149538 /* 390 */ 1208, 1179, 1217, 1120, 1220, 1206, 1221, 1222, 1226, 1227,
149539 /* 400 */ 1228, 1233, 937, 1147, 1261, 1149, 1207, 1248, 1249, 1253,
149540 /* 410 */ 1133, 1151, 1274, 1293, 1211, 1236, 1605, 1607, 1589, 1458,
149541 /* 420 */ 1599, 1525, 1604, 1600, 1602, 1490, 1492, 1503, 1612, 1504,
149542 /* 430 */ 1618, 1510, 1624, 1640, 1513, 1506, 1528, 1593, 1619, 1514,
149543 /* 440 */ 1606, 1608, 1610, 1613, 1535, 1551, 1631, 1533, 1667, 1665,
149544 /* 450 */ 1649, 1565, 1521, 1609, 1650, 1611, 1603, 1634, 1546, 1573,
149545 /* 460 */ 1658, 1663, 1666, 1560, 1568, 1668, 1620, 1669, 1671, 1672,
149546 /* 470 */ 1674, 1621, 1661, 1675, 1627, 1662, 1677, 1556, 1679, 1680,
149547 /* 480 */ 1548, 1683, 1684, 1682, 1686, 1689, 1688, 1691, 1694, 1693,
149548 /* 490 */ 1584, 1696, 1698, 1617, 1695, 1706, 1594, 1705, 1701, 1702,
149549 /* 500 */ 1704, 1707, 1647, 1670, 1654, 1708, 1673, 1656, 1715, 1727,
149550 /* 510 */ 1729, 1730, 1731, 1733, 1719, 1732, 1705, 1737, 1738, 1742,
149551 /* 520 */ 1743, 1741, 1745, 1734, 1758, 1748, 1749, 1750, 1752, 1754,
149552 /* 530 */ 1755, 1746, 1655, 1653, 1657, 1659, 1660, 1761, 1773, 1788,
149553};
149554#define YY_REDUCE_COUNT(382) (382)
149555#define YY_REDUCE_MIN(-260) (-260)
149556#define YY_REDUCE_MAX(1420) (1420)
149557static const short yy_reduce_ofst[] = {
149558 /* 0 */ -170, -18, -159, 309, 313, -167, -19, 75, 117, 211,
149559 /* 10 */ 315, 317, -165, -195, -168, -260, 389, 437, 475, 524,
149560 /* 20 */ 527, -169, 529, 531, -28, 80, 534, 239, 304, 412,
149561 /* 30 */ 558, 577, 37, 120, 368, -22, 460, 517, 555, 560,
149562 /* 40 */ 562, 586, -257, -257, -257, -257, -257, -257, -257, -257,
149563 /* 50 */ -257, -257, -257, -257, -257, -257, -257, -257, -257, -257,
149564 /* 60 */ -257, -257, -257, -257, -257, -257, -257, -257, -257, -257,
149565 /* 70 */ -257, -257, -257, -257, -257, -257, -257, -172, 457, 628,
149566 /* 80 */ 673, 692, 694, 702, 704, 722, 728, 740, 743, 748,
149567 /* 90 */ 767, 791, 815, 818, 820, 822, 857, 861, 864, 866,
149568 /* 100 */ 868, 870, 872, 874, 876, 881, 900, 902, 906, 908,
149569 /* 110 */ 910, 912, 915, 917, 920, 960, 962, 964, 988, 990,
149570 /* 120 */ 992, 1016, 1029, 1032, -257, -257, -257, -257, -257, -257,
149571 /* 130 */ -257, -257, -257, 271, 618, -190, 68, 60, 240, -124,
149572 /* 140 */ 603, 610, 603, 610, 12, -257, -257, -257, -257, -128,
149573 /* 150 */ -128, -128, -142, 25, 270, 281, 333, 124, 236, 648,
149574 /* 160 */ 374, 465, 465, 28, 598, 792, 839, 469, 38, 381,
149575 /* 170 */ 622, 709, 173, 699, 522, 703, 808, 811, 867, 816,
149576 /* 180 */ -104, 823, -3, 875, 649, 753, 323, -88, 882, 884,
149577 /* 190 */ 518, 43, 325, 899, 763, 604, 879, 969, 402, -193,
149578 /* 200 */ -189, -180, -151, -55, 69, 104, 141, 259, 286, 360,
149579 /* 210 */ 364, 455, 474, 481, 510, 516, 611, 653, 788, 99,
149580 /* 220 */ 871, 878, 995, 1009, 1049, 1081, 1115, 1134, 1136, 1138,
149581 /* 230 */ 1139, 1140, 733, 1110, 1112, 1144, 1145, 1146, 1148, 1084,
149582 /* 240 */ 1161, 1162, 1163, 1089, 1047, 1165, 1166, 1127, 1169, 104,
149583 /* 250 */ 1181, 1182, 1183, 1184, 1185, 1187, 1098, 1100, 1150, 1131,
149584 /* 260 */ 1132, 1135, 1141, 1084, 1150, 1150, 1152, 1173, 1196, 1097,
149585 /* 270 */ 1153, 1143, 1167, 1103, 1154, 1155, 1104, 1176, 1174, 1199,
149586 /* 280 */ 1178, 1186, 1188, 1168, 1158, 1160, 1170, 1159, 1201, 1230,
149587 /* 290 */ 1156, 1243, 1244, 1157, 1164, 1247, 1172, 1189, 1192, 1240,
149588 /* 300 */ 1231, 1241, 1242, 1256, 1257, 1278, 1294, 1251, 1252, 1232,
149589 /* 310 */ 1234, 1265, 1266, 1259, 1301, 1303, 1223, 1225, 1307, 1313,
149590 /* 320 */ 1295, 1317, 1319, 1320, 1323, 1299, 1306, 1308, 1309, 1305,
149591 /* 330 */ 1311, 1315, 1314, 1321, 1312, 1324, 1326, 1327, 1263, 1271,
149592 /* 340 */ 1331, 1296, 1298, 1300, 1302, 1304, 1310, 1316, 1318, 1357,
149593 /* 350 */ 1255, 1260, 1329, 1325, 1332, 1370, 1333, 1338, 1341, 1343,
149594 /* 360 */ 1346, 1390, 1393, 1403, 1405, 1416, 1417, 1418, 1328, 1330,
149595 /* 370 */ 1335, 1409, 1394, 1399, 1401, 1402, 1410, 1391, 1396, 1411,
149596 /* 380 */ 1420, 1413, 1415,
149597};
149598static const YYACTIONTYPEunsigned short int yy_default[] = {
149599 /* 0 */ 1537, 1537, 1537, 1377, 1159, 1266, 1159, 1159, 1159, 1377,
149600 /* 10 */ 1377, 1377, 1159, 1296, 1296, 1430, 1190, 1159, 1159, 1159,
149601 /* 20 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1376, 1159, 1159,
149602 /* 30 */ 1159, 1159, 1460, 1460, 1159, 1159, 1159, 1159, 1159, 1159,
149603 /* 40 */ 1159, 1159, 1159, 1302, 1159, 1159, 1159, 1159, 1159, 1378,
149604 /* 50 */ 1379, 1159, 1159, 1159, 1429, 1431, 1394, 1312, 1311, 1310,
149605 /* 60 */ 1309, 1412, 1283, 1307, 1300, 1304, 1372, 1373, 1371, 1375,
149606 /* 70 */ 1379, 1378, 1159, 1303, 1343, 1357, 1342, 1159, 1159, 1159,
149607 /* 80 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149608 /* 90 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149609 /* 100 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149610 /* 110 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149611 /* 120 */ 1159, 1159, 1159, 1159, 1351, 1356, 1362, 1355, 1352, 1345,
149612 /* 130 */ 1344, 1346, 1347, 1159, 1180, 1230, 1159, 1159, 1159, 1159,
149613 /* 140 */ 1448, 1447, 1159, 1159, 1190, 1348, 1349, 1359, 1358, 1437,
149614 /* 150 */ 1493, 1492, 1395, 1159, 1159, 1159, 1159, 1159, 1159, 1460,
149615 /* 160 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149616 /* 170 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149617 /* 180 */ 1159, 1159, 1159, 1159, 1460, 1460, 1159, 1190, 1460, 1460,
149618 /* 190 */ 1186, 1337, 1336, 1186, 1290, 1159, 1443, 1266, 1257, 1159,
149619 /* 200 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149620 /* 210 */ 1159, 1159, 1159, 1434, 1432, 1159, 1159, 1159, 1159, 1159,
149621 /* 220 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149622 /* 230 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149623 /* 240 */ 1159, 1159, 1159, 1262, 1159, 1159, 1159, 1159, 1159, 1159,
149624 /* 250 */ 1159, 1159, 1159, 1159, 1159, 1487, 1159, 1407, 1244, 1262,
149625 /* 260 */ 1262, 1262, 1262, 1264, 1245, 1243, 1256, 1191, 1166, 1529,
149626 /* 270 */ 1306, 1285, 1285, 1526, 1306, 1306, 1526, 1205, 1507, 1202,
149627 /* 280 */ 1296, 1296, 1296, 1285, 1290, 1290, 1374, 1263, 1256, 1159,
149628 /* 290 */ 1529, 1271, 1271, 1528, 1528, 1271, 1395, 1315, 1321, 1233,
149629 /* 300 */ 1306, 1239, 1239, 1239, 1239, 1271, 1177, 1306, 1306, 1315,
149630 /* 310 */ 1321, 1233, 1233, 1306, 1271, 1177, 1411, 1523, 1271, 1177,
149631 /* 320 */ 1385, 1271, 1177, 1271, 1177, 1385, 1231, 1231, 1231, 1220,
149632 /* 330 */ 1385, 1231, 1205, 1231, 1220, 1231, 1231, 1385, 1389, 1389,
149633 /* 340 */ 1385, 1289, 1284, 1289, 1284, 1289, 1284, 1289, 1284, 1271,
149634 /* 350 */ 1470, 1470, 1301, 1290, 1380, 1271, 1159, 1301, 1299, 1297,
149635 /* 360 */ 1306, 1183, 1223, 1490, 1490, 1486, 1486, 1486, 1534, 1534,
149636 /* 370 */ 1443, 1502, 1190, 1190, 1190, 1190, 1502, 1207, 1207, 1191,
149637 /* 380 */ 1191, 1190, 1502, 1159, 1159, 1159, 1159, 1159, 1159, 1497,
149638 /* 390 */ 1159, 1396, 1275, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149639 /* 400 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149640 /* 410 */ 1159, 1159, 1159, 1159, 1159, 1326, 1159, 1162, 1440, 1159,
149641 /* 420 */ 1159, 1438, 1159, 1159, 1159, 1159, 1159, 1159, 1276, 1159,
149642 /* 430 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149643 /* 440 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1525, 1159, 1159,
149644 /* 450 */ 1159, 1159, 1159, 1159, 1410, 1409, 1159, 1159, 1273, 1159,
149645 /* 460 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149646 /* 470 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149647 /* 480 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149648 /* 490 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1298, 1159, 1159,
149649 /* 500 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149650 /* 510 */ 1159, 1159, 1475, 1291, 1159, 1159, 1516, 1159, 1159, 1159,
149651 /* 520 */ 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159, 1159,
149652 /* 530 */ 1159, 1511, 1247, 1328, 1159, 1327, 1331, 1159, 1171, 1159,
149653};
149654/********** End of lemon-generated parsing tables *****************************/
149655
149656/* The next table maps tokens (terminal symbols) into fallback tokens.
149657** If a construct like the following:
149658**
149659** %fallback ID X Y Z.
149660**
149661** appears in the grammar, then ID becomes a fallback token for X, Y,
149662** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
149663** but it does not parse, the type of the token is changed to ID and
149664** the parse is retried before an error is thrown.
149665**
149666** This feature can be used, for example, to cause some keywords in a language
149667** to revert to identifiers if they keyword does not apply in the context where
149668** it appears.
149669*/
149670#ifdef YYFALLBACK1
149671static const YYCODETYPEunsigned short int yyFallback[] = {
149672 0, /* $ => nothing */
149673 0, /* SEMI => nothing */
149674 59, /* EXPLAIN => ID */
149675 59, /* QUERY => ID */
149676 59, /* PLAN => ID */
149677 59, /* BEGIN => ID */
149678 0, /* TRANSACTION => nothing */
149679 59, /* DEFERRED => ID */
149680 59, /* IMMEDIATE => ID */
149681 59, /* EXCLUSIVE => ID */
149682 0, /* COMMIT => nothing */
149683 59, /* END => ID */
149684 59, /* ROLLBACK => ID */
149685 59, /* SAVEPOINT => ID */
149686 59, /* RELEASE => ID */
149687 0, /* TO => nothing */
149688 0, /* TABLE => nothing */
149689 0, /* CREATE => nothing */
149690 59, /* IF => ID */
149691 0, /* NOT => nothing */
149692 0, /* EXISTS => nothing */
149693 59, /* TEMP => ID */
149694 0, /* LP => nothing */
149695 0, /* RP => nothing */
149696 0, /* AS => nothing */
149697 59, /* WITHOUT => ID */
149698 0, /* COMMA => nothing */
149699 59, /* ABORT => ID */
149700 59, /* ACTION => ID */
149701 59, /* AFTER => ID */
149702 59, /* ANALYZE => ID */
149703 59, /* ASC => ID */
149704 59, /* ATTACH => ID */
149705 59, /* BEFORE => ID */
149706 59, /* BY => ID */
149707 59, /* CASCADE => ID */
149708 59, /* CAST => ID */
149709 59, /* CONFLICT => ID */
149710 59, /* DATABASE => ID */
149711 59, /* DESC => ID */
149712 59, /* DETACH => ID */
149713 59, /* EACH => ID */
149714 59, /* FAIL => ID */
149715 0, /* OR => nothing */
149716 0, /* AND => nothing */
149717 0, /* IS => nothing */
149718 59, /* MATCH => ID */
149719 59, /* LIKE_KW => ID */
149720 0, /* BETWEEN => nothing */
149721 0, /* IN => nothing */
149722 0, /* ISNULL => nothing */
149723 0, /* NOTNULL => nothing */
149724 0, /* NE => nothing */
149725 0, /* EQ => nothing */
149726 0, /* GT => nothing */
149727 0, /* LE => nothing */
149728 0, /* LT => nothing */
149729 0, /* GE => nothing */
149730 0, /* ESCAPE => nothing */
149731 0, /* ID => nothing */
149732 59, /* COLUMNKW => ID */
149733 59, /* DO => ID */
149734 59, /* FOR => ID */
149735 59, /* IGNORE => ID */
149736 59, /* INITIALLY => ID */
149737 59, /* INSTEAD => ID */
149738 59, /* NO => ID */
149739 59, /* KEY => ID */
149740 59, /* OF => ID */
149741 59, /* OFFSET => ID */
149742 59, /* PRAGMA => ID */
149743 59, /* RAISE => ID */
149744 59, /* RECURSIVE => ID */
149745 59, /* REPLACE => ID */
149746 59, /* RESTRICT => ID */
149747 59, /* ROW => ID */
149748 59, /* ROWS => ID */
149749 59, /* TRIGGER => ID */
149750 59, /* VACUUM => ID */
149751 59, /* VIEW => ID */
149752 59, /* VIRTUAL => ID */
149753 59, /* WITH => ID */
149754 59, /* CURRENT => ID */
149755 59, /* FOLLOWING => ID */
149756 59, /* PARTITION => ID */
149757 59, /* PRECEDING => ID */
149758 59, /* RANGE => ID */
149759 59, /* UNBOUNDED => ID */
149760 59, /* EXCLUDE => ID */
149761 59, /* GROUPS => ID */
149762 59, /* OTHERS => ID */
149763 59, /* TIES => ID */
149764 59, /* REINDEX => ID */
149765 59, /* RENAME => ID */
149766 59, /* CTIME_KW => ID */
149767};
149768#endif /* YYFALLBACK */
149769
149770/* The following structure represents a single element of the
149771** parser's stack. Information stored includes:
149772**
149773** + The state number for the parser at this level of the stack.
149774**
149775** + The value of the token stored at this level of the stack.
149776** (In other words, the "major" token.)
149777**
149778** + The semantic value stored at this level of the stack. This is
149779** the information used by the action routines in the grammar.
149780** It is sometimes called the "minor" token.
149781**
149782** After the "shift" half of a SHIFTREDUCE action, the stateno field
149783** actually contains the reduce action for the second half of the
149784** SHIFTREDUCE.
149785*/
149786struct yyStackEntry {
149787 YYACTIONTYPEunsigned short int stateno; /* The state-number, or reduce action in SHIFTREDUCE */
149788 YYCODETYPEunsigned short int major; /* The major token value. This is the code
149789 ** number for the token at this stack level */
149790 YYMINORTYPE minor; /* The user-supplied minor token value. This
149791 ** is the value of the token */
149792};
149793typedef struct yyStackEntry yyStackEntry;
149794
149795/* The state of the parser is completely contained in an instance of
149796** the following structure */
149797struct yyParser {
149798 yyStackEntry *yytos; /* Pointer to top element of the stack */
149799#ifdef YYTRACKMAXSTACKDEPTH
149800 int yyhwm; /* High-water mark of the stack */
149801#endif
149802#ifndef YYNOERRORRECOVERY1
149803 int yyerrcnt; /* Shifts left before out of the error */
149804#endif
149805 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */
149806 sqlite3ParserCTX_SDECLParse *pParse; /* A place to hold %extra_context */
149807#if YYSTACKDEPTH100<=0
149808 int yystksz; /* Current side of the stack */
149809 yyStackEntry *yystack; /* The parser's stack */
149810 yyStackEntry yystk0; /* First stack entry */
149811#else
149812 yyStackEntry yystack[YYSTACKDEPTH100]; /* The parser's stack */
149813 yyStackEntry *yystackEnd; /* Last entry in the stack */
149814#endif
149815};
149816typedef struct yyParser yyParser;
149817
149818#ifndef NDEBUG1
149819/* #include <stdio.h> */
149820static FILE *yyTraceFILE = 0;
149821static char *yyTracePrompt = 0;
149822#endif /* NDEBUG */
149823
149824#ifndef NDEBUG1
149825/*
149826** Turn parser tracing on by giving a stream to which to write the trace
149827** and a prompt to preface each trace message. Tracing is turned off
149828** by making either argument NULL
149829**
149830** Inputs:
149831** <ul>
149832** <li> A FILE* to which trace output should be written.
149833** If NULL, then tracing is turned off.
149834** <li> A prefix string written at the beginning of every
149835** line of trace output. If NULL, then tracing is
149836** turned off.
149837** </ul>
149838**
149839** Outputs:
149840** None.
149841*/
149842SQLITE_PRIVATEstatic void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
149843 yyTraceFILE = TraceFILE;
149844 yyTracePrompt = zTracePrompt;
149845 if( yyTraceFILE==0 ) yyTracePrompt = 0;
149846 else if( yyTracePrompt==0 ) yyTraceFILE = 0;
149847}
149848#endif /* NDEBUG */
149849
149850#if defined(YYCOVERAGE) || !defined(NDEBUG1)
149851/* For tracing shifts, the names of all terminals and nonterminals
149852** are required. The following table supplies these names */
149853static const char *const yyTokenName[] = {
149854 /* 0 */ "$",
149855 /* 1 */ "SEMI",
149856 /* 2 */ "EXPLAIN",
149857 /* 3 */ "QUERY",
149858 /* 4 */ "PLAN",
149859 /* 5 */ "BEGIN",
149860 /* 6 */ "TRANSACTION",
149861 /* 7 */ "DEFERRED",
149862 /* 8 */ "IMMEDIATE",
149863 /* 9 */ "EXCLUSIVE",
149864 /* 10 */ "COMMIT",
149865 /* 11 */ "END",
149866 /* 12 */ "ROLLBACK",
149867 /* 13 */ "SAVEPOINT",
149868 /* 14 */ "RELEASE",
149869 /* 15 */ "TO",
149870 /* 16 */ "TABLE",
149871 /* 17 */ "CREATE",
149872 /* 18 */ "IF",
149873 /* 19 */ "NOT",
149874 /* 20 */ "EXISTS",
149875 /* 21 */ "TEMP",
149876 /* 22 */ "LP",
149877 /* 23 */ "RP",
149878 /* 24 */ "AS",
149879 /* 25 */ "WITHOUT",
149880 /* 26 */ "COMMA",
149881 /* 27 */ "ABORT",
149882 /* 28 */ "ACTION",
149883 /* 29 */ "AFTER",
149884 /* 30 */ "ANALYZE",
149885 /* 31 */ "ASC",
149886 /* 32 */ "ATTACH",
149887 /* 33 */ "BEFORE",
149888 /* 34 */ "BY",
149889 /* 35 */ "CASCADE",
149890 /* 36 */ "CAST",
149891 /* 37 */ "CONFLICT",
149892 /* 38 */ "DATABASE",
149893 /* 39 */ "DESC",
149894 /* 40 */ "DETACH",
149895 /* 41 */ "EACH",
149896 /* 42 */ "FAIL",
149897 /* 43 */ "OR",
149898 /* 44 */ "AND",
149899 /* 45 */ "IS",
149900 /* 46 */ "MATCH",
149901 /* 47 */ "LIKE_KW",
149902 /* 48 */ "BETWEEN",
149903 /* 49 */ "IN",
149904 /* 50 */ "ISNULL",
149905 /* 51 */ "NOTNULL",
149906 /* 52 */ "NE",
149907 /* 53 */ "EQ",
149908 /* 54 */ "GT",
149909 /* 55 */ "LE",
149910 /* 56 */ "LT",
149911 /* 57 */ "GE",
149912 /* 58 */ "ESCAPE",
149913 /* 59 */ "ID",
149914 /* 60 */ "COLUMNKW",
149915 /* 61 */ "DO",
149916 /* 62 */ "FOR",
149917 /* 63 */ "IGNORE",
149918 /* 64 */ "INITIALLY",
149919 /* 65 */ "INSTEAD",
149920 /* 66 */ "NO",
149921 /* 67 */ "KEY",
149922 /* 68 */ "OF",
149923 /* 69 */ "OFFSET",
149924 /* 70 */ "PRAGMA",
149925 /* 71 */ "RAISE",
149926 /* 72 */ "RECURSIVE",
149927 /* 73 */ "REPLACE",
149928 /* 74 */ "RESTRICT",
149929 /* 75 */ "ROW",
149930 /* 76 */ "ROWS",
149931 /* 77 */ "TRIGGER",
149932 /* 78 */ "VACUUM",
149933 /* 79 */ "VIEW",
149934 /* 80 */ "VIRTUAL",
149935 /* 81 */ "WITH",
149936 /* 82 */ "CURRENT",
149937 /* 83 */ "FOLLOWING",
149938 /* 84 */ "PARTITION",
149939 /* 85 */ "PRECEDING",
149940 /* 86 */ "RANGE",
149941 /* 87 */ "UNBOUNDED",
149942 /* 88 */ "EXCLUDE",
149943 /* 89 */ "GROUPS",
149944 /* 90 */ "OTHERS",
149945 /* 91 */ "TIES",
149946 /* 92 */ "REINDEX",
149947 /* 93 */ "RENAME",
149948 /* 94 */ "CTIME_KW",
149949 /* 95 */ "ANY",
149950 /* 96 */ "BITAND",
149951 /* 97 */ "BITOR",
149952 /* 98 */ "LSHIFT",
149953 /* 99 */ "RSHIFT",
149954 /* 100 */ "PLUS",
149955 /* 101 */ "MINUS",
149956 /* 102 */ "STAR",
149957 /* 103 */ "SLASH",
149958 /* 104 */ "REM",
149959 /* 105 */ "CONCAT",
149960 /* 106 */ "COLLATE",
149961 /* 107 */ "BITNOT",
149962 /* 108 */ "ON",
149963 /* 109 */ "INDEXED",
149964 /* 110 */ "STRING",
149965 /* 111 */ "JOIN_KW",
149966 /* 112 */ "CONSTRAINT",
149967 /* 113 */ "DEFAULT",
149968 /* 114 */ "NULL",
149969 /* 115 */ "PRIMARY",
149970 /* 116 */ "UNIQUE",
149971 /* 117 */ "CHECK",
149972 /* 118 */ "REFERENCES",
149973 /* 119 */ "AUTOINCR",
149974 /* 120 */ "INSERT",
149975 /* 121 */ "DELETE",
149976 /* 122 */ "UPDATE",
149977 /* 123 */ "SET",
149978 /* 124 */ "DEFERRABLE",
149979 /* 125 */ "FOREIGN",
149980 /* 126 */ "DROP",
149981 /* 127 */ "UNION",
149982 /* 128 */ "ALL",
149983 /* 129 */ "EXCEPT",
149984 /* 130 */ "INTERSECT",
149985 /* 131 */ "SELECT",
149986 /* 132 */ "VALUES",
149987 /* 133 */ "DISTINCT",
149988 /* 134 */ "DOT",
149989 /* 135 */ "FROM",
149990 /* 136 */ "JOIN",
149991 /* 137 */ "USING",
149992 /* 138 */ "ORDER",
149993 /* 139 */ "GROUP",
149994 /* 140 */ "HAVING",
149995 /* 141 */ "LIMIT",
149996 /* 142 */ "WHERE",
149997 /* 143 */ "INTO",
149998 /* 144 */ "NOTHING",
149999 /* 145 */ "FLOAT",
150000 /* 146 */ "BLOB",
150001 /* 147 */ "INTEGER",
150002 /* 148 */ "VARIABLE",
150003 /* 149 */ "CASE",
150004 /* 150 */ "WHEN",
150005 /* 151 */ "THEN",
150006 /* 152 */ "ELSE",
150007 /* 153 */ "INDEX",
150008 /* 154 */ "ALTER",
150009 /* 155 */ "ADD",
150010 /* 156 */ "WINDOW",
150011 /* 157 */ "OVER",
150012 /* 158 */ "FILTER",
150013 /* 159 */ "TRUEFALSE",
150014 /* 160 */ "ISNOT",
150015 /* 161 */ "FUNCTION",
150016 /* 162 */ "COLUMN",
150017 /* 163 */ "AGG_FUNCTION",
150018 /* 164 */ "AGG_COLUMN",
150019 /* 165 */ "UMINUS",
150020 /* 166 */ "UPLUS",
150021 /* 167 */ "TRUTH",
150022 /* 168 */ "REGISTER",
150023 /* 169 */ "VECTOR",
150024 /* 170 */ "SELECT_COLUMN",
150025 /* 171 */ "IF_NULL_ROW",
150026 /* 172 */ "ASTERISK",
150027 /* 173 */ "SPAN",
150028 /* 174 */ "SPACE",
150029 /* 175 */ "ILLEGAL",
150030 /* 176 */ "input",
150031 /* 177 */ "cmdlist",
150032 /* 178 */ "ecmd",
150033 /* 179 */ "cmdx",
150034 /* 180 */ "explain",
150035 /* 181 */ "cmd",
150036 /* 182 */ "transtype",
150037 /* 183 */ "trans_opt",
150038 /* 184 */ "nm",
150039 /* 185 */ "savepoint_opt",
150040 /* 186 */ "create_table",
150041 /* 187 */ "create_table_args",
150042 /* 188 */ "createkw",
150043 /* 189 */ "temp",
150044 /* 190 */ "ifnotexists",
150045 /* 191 */ "dbnm",
150046 /* 192 */ "columnlist",
150047 /* 193 */ "conslist_opt",
150048 /* 194 */ "table_options",
150049 /* 195 */ "select",
150050 /* 196 */ "columnname",
150051 /* 197 */ "carglist",
150052 /* 198 */ "typetoken",
150053 /* 199 */ "typename",
150054 /* 200 */ "signed",
150055 /* 201 */ "plus_num",
150056 /* 202 */ "minus_num",
150057 /* 203 */ "scanpt",
150058 /* 204 */ "scantok",
150059 /* 205 */ "ccons",
150060 /* 206 */ "term",
150061 /* 207 */ "expr",
150062 /* 208 */ "onconf",
150063 /* 209 */ "sortorder",
150064 /* 210 */ "autoinc",
150065 /* 211 */ "eidlist_opt",
150066 /* 212 */ "refargs",
150067 /* 213 */ "defer_subclause",
150068 /* 214 */ "refarg",
150069 /* 215 */ "refact",
150070 /* 216 */ "init_deferred_pred_opt",
150071 /* 217 */ "conslist",
150072 /* 218 */ "tconscomma",
150073 /* 219 */ "tcons",
150074 /* 220 */ "sortlist",
150075 /* 221 */ "eidlist",
150076 /* 222 */ "defer_subclause_opt",
150077 /* 223 */ "orconf",
150078 /* 224 */ "resolvetype",
150079 /* 225 */ "raisetype",
150080 /* 226 */ "ifexists",
150081 /* 227 */ "fullname",
150082 /* 228 */ "selectnowith",
150083 /* 229 */ "oneselect",
150084 /* 230 */ "wqlist",
150085 /* 231 */ "multiselect_op",
150086 /* 232 */ "distinct",
150087 /* 233 */ "selcollist",
150088 /* 234 */ "from",
150089 /* 235 */ "where_opt",
150090 /* 236 */ "groupby_opt",
150091 /* 237 */ "having_opt",
150092 /* 238 */ "orderby_opt",
150093 /* 239 */ "limit_opt",
150094 /* 240 */ "window_clause",
150095 /* 241 */ "values",
150096 /* 242 */ "nexprlist",
150097 /* 243 */ "sclp",
150098 /* 244 */ "as",
150099 /* 245 */ "seltablist",
150100 /* 246 */ "stl_prefix",
150101 /* 247 */ "joinop",
150102 /* 248 */ "indexed_opt",
150103 /* 249 */ "on_opt",
150104 /* 250 */ "using_opt",
150105 /* 251 */ "exprlist",
150106 /* 252 */ "xfullname",
150107 /* 253 */ "idlist",
150108 /* 254 */ "with",
150109 /* 255 */ "setlist",
150110 /* 256 */ "insert_cmd",
150111 /* 257 */ "idlist_opt",
150112 /* 258 */ "upsert",
150113 /* 259 */ "over_clause",
150114 /* 260 */ "likeop",
150115 /* 261 */ "between_op",
150116 /* 262 */ "in_op",
150117 /* 263 */ "paren_exprlist",
150118 /* 264 */ "case_operand",
150119 /* 265 */ "case_exprlist",
150120 /* 266 */ "case_else",
150121 /* 267 */ "uniqueflag",
150122 /* 268 */ "collate",
150123 /* 269 */ "vinto",
150124 /* 270 */ "nmnum",
150125 /* 271 */ "trigger_decl",
150126 /* 272 */ "trigger_cmd_list",
150127 /* 273 */ "trigger_time",
150128 /* 274 */ "trigger_event",
150129 /* 275 */ "foreach_clause",
150130 /* 276 */ "when_clause",
150131 /* 277 */ "trigger_cmd",
150132 /* 278 */ "trnm",
150133 /* 279 */ "tridxby",
150134 /* 280 */ "database_kw_opt",
150135 /* 281 */ "key_opt",
150136 /* 282 */ "add_column_fullname",
150137 /* 283 */ "kwcolumn_opt",
150138 /* 284 */ "create_vtab",
150139 /* 285 */ "vtabarglist",
150140 /* 286 */ "vtabarg",
150141 /* 287 */ "vtabargtoken",
150142 /* 288 */ "lp",
150143 /* 289 */ "anylist",
150144 /* 290 */ "windowdefn_list",
150145 /* 291 */ "windowdefn",
150146 /* 292 */ "window",
150147 /* 293 */ "frame_opt",
150148 /* 294 */ "part_opt",
150149 /* 295 */ "filter_opt",
150150 /* 296 */ "range_or_rows",
150151 /* 297 */ "frame_bound",
150152 /* 298 */ "frame_bound_s",
150153 /* 299 */ "frame_bound_e",
150154 /* 300 */ "frame_exclude_opt",
150155 /* 301 */ "frame_exclude",
150156};
150157#endif /* defined(YYCOVERAGE) || !defined(NDEBUG) */
150158
150159#ifndef NDEBUG1
150160/* For tracing reduce actions, the names of all rules are required.
150161*/
150162static const char *const yyRuleName[] = {
150163 /* 0 */ "explain ::= EXPLAIN",
150164 /* 1 */ "explain ::= EXPLAIN QUERY PLAN",
150165 /* 2 */ "cmdx ::= cmd",
150166 /* 3 */ "cmd ::= BEGIN transtype trans_opt",
150167 /* 4 */ "transtype ::=",
150168 /* 5 */ "transtype ::= DEFERRED",
150169 /* 6 */ "transtype ::= IMMEDIATE",
150170 /* 7 */ "transtype ::= EXCLUSIVE",
150171 /* 8 */ "cmd ::= COMMIT|END trans_opt",
150172 /* 9 */ "cmd ::= ROLLBACK trans_opt",
150173 /* 10 */ "cmd ::= SAVEPOINT nm",
150174 /* 11 */ "cmd ::= RELEASE savepoint_opt nm",
150175 /* 12 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
150176 /* 13 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
150177 /* 14 */ "createkw ::= CREATE",
150178 /* 15 */ "ifnotexists ::=",
150179 /* 16 */ "ifnotexists ::= IF NOT EXISTS",
150180 /* 17 */ "temp ::= TEMP",
150181 /* 18 */ "temp ::=",
150182 /* 19 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
150183 /* 20 */ "create_table_args ::= AS select",
150184 /* 21 */ "table_options ::=",
150185 /* 22 */ "table_options ::= WITHOUT nm",
150186 /* 23 */ "columnname ::= nm typetoken",
150187 /* 24 */ "typetoken ::=",
150188 /* 25 */ "typetoken ::= typename LP signed RP",
150189 /* 26 */ "typetoken ::= typename LP signed COMMA signed RP",
150190 /* 27 */ "typename ::= typename ID|STRING",
150191 /* 28 */ "scanpt ::=",
150192 /* 29 */ "scantok ::=",
150193 /* 30 */ "ccons ::= CONSTRAINT nm",
150194 /* 31 */ "ccons ::= DEFAULT scantok term",
150195 /* 32 */ "ccons ::= DEFAULT LP expr RP",
150196 /* 33 */ "ccons ::= DEFAULT PLUS scantok term",
150197 /* 34 */ "ccons ::= DEFAULT MINUS scantok term",
150198 /* 35 */ "ccons ::= DEFAULT scantok ID|INDEXED",
150199 /* 36 */ "ccons ::= NOT NULL onconf",
150200 /* 37 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
150201 /* 38 */ "ccons ::= UNIQUE onconf",
150202 /* 39 */ "ccons ::= CHECK LP expr RP",
150203 /* 40 */ "ccons ::= REFERENCES nm eidlist_opt refargs",
150204 /* 41 */ "ccons ::= defer_subclause",
150205 /* 42 */ "ccons ::= COLLATE ID|STRING",
150206 /* 43 */ "autoinc ::=",
150207 /* 44 */ "autoinc ::= AUTOINCR",
150208 /* 45 */ "refargs ::=",
150209 /* 46 */ "refargs ::= refargs refarg",
150210 /* 47 */ "refarg ::= MATCH nm",
150211 /* 48 */ "refarg ::= ON INSERT refact",
150212 /* 49 */ "refarg ::= ON DELETE refact",
150213 /* 50 */ "refarg ::= ON UPDATE refact",
150214 /* 51 */ "refact ::= SET NULL",
150215 /* 52 */ "refact ::= SET DEFAULT",
150216 /* 53 */ "refact ::= CASCADE",
150217 /* 54 */ "refact ::= RESTRICT",
150218 /* 55 */ "refact ::= NO ACTION",
150219 /* 56 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
150220 /* 57 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
150221 /* 58 */ "init_deferred_pred_opt ::=",
150222 /* 59 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
150223 /* 60 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
150224 /* 61 */ "conslist_opt ::=",
150225 /* 62 */ "tconscomma ::= COMMA",
150226 /* 63 */ "tcons ::= CONSTRAINT nm",
150227 /* 64 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf",
150228 /* 65 */ "tcons ::= UNIQUE LP sortlist RP onconf",
150229 /* 66 */ "tcons ::= CHECK LP expr RP onconf",
150230 /* 67 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt",
150231 /* 68 */ "defer_subclause_opt ::=",
150232 /* 69 */ "onconf ::=",
150233 /* 70 */ "onconf ::= ON CONFLICT resolvetype",
150234 /* 71 */ "orconf ::=",
150235 /* 72 */ "orconf ::= OR resolvetype",
150236 /* 73 */ "resolvetype ::= IGNORE",
150237 /* 74 */ "resolvetype ::= REPLACE",
150238 /* 75 */ "cmd ::= DROP TABLE ifexists fullname",
150239 /* 76 */ "ifexists ::= IF EXISTS",
150240 /* 77 */ "ifexists ::=",
150241 /* 78 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select",
150242 /* 79 */ "cmd ::= DROP VIEW ifexists fullname",
150243 /* 80 */ "cmd ::= select",
150244 /* 81 */ "select ::= WITH wqlist selectnowith",
150245 /* 82 */ "select ::= WITH RECURSIVE wqlist selectnowith",
150246 /* 83 */ "select ::= selectnowith",
150247 /* 84 */ "selectnowith ::= selectnowith multiselect_op oneselect",
150248 /* 85 */ "multiselect_op ::= UNION",
150249 /* 86 */ "multiselect_op ::= UNION ALL",
150250 /* 87 */ "multiselect_op ::= EXCEPT|INTERSECT",
150251 /* 88 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
150252 /* 89 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt",
150253 /* 90 */ "values ::= VALUES LP nexprlist RP",
150254 /* 91 */ "values ::= values COMMA LP nexprlist RP",
150255 /* 92 */ "distinct ::= DISTINCT",
150256 /* 93 */ "distinct ::= ALL",
150257 /* 94 */ "distinct ::=",
150258 /* 95 */ "sclp ::=",
150259 /* 96 */ "selcollist ::= sclp scanpt expr scanpt as",
150260 /* 97 */ "selcollist ::= sclp scanpt STAR",
150261 /* 98 */ "selcollist ::= sclp scanpt nm DOT STAR",
150262 /* 99 */ "as ::= AS nm",
150263 /* 100 */ "as ::=",
150264 /* 101 */ "from ::=",
150265 /* 102 */ "from ::= FROM seltablist",
150266 /* 103 */ "stl_prefix ::= seltablist joinop",
150267 /* 104 */ "stl_prefix ::=",
150268 /* 105 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
150269 /* 106 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt",
150270 /* 107 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
150271 /* 108 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
150272 /* 109 */ "dbnm ::=",
150273 /* 110 */ "dbnm ::= DOT nm",
150274 /* 111 */ "fullname ::= nm",
150275 /* 112 */ "fullname ::= nm DOT nm",
150276 /* 113 */ "xfullname ::= nm",
150277 /* 114 */ "xfullname ::= nm DOT nm",
150278 /* 115 */ "xfullname ::= nm DOT nm AS nm",
150279 /* 116 */ "xfullname ::= nm AS nm",
150280 /* 117 */ "joinop ::= COMMA|JOIN",
150281 /* 118 */ "joinop ::= JOIN_KW JOIN",
150282 /* 119 */ "joinop ::= JOIN_KW nm JOIN",
150283 /* 120 */ "joinop ::= JOIN_KW nm nm JOIN",
150284 /* 121 */ "on_opt ::= ON expr",
150285 /* 122 */ "on_opt ::=",
150286 /* 123 */ "indexed_opt ::=",
150287 /* 124 */ "indexed_opt ::= INDEXED BY nm",
150288 /* 125 */ "indexed_opt ::= NOT INDEXED",
150289 /* 126 */ "using_opt ::= USING LP idlist RP",
150290 /* 127 */ "using_opt ::=",
150291 /* 128 */ "orderby_opt ::=",
150292 /* 129 */ "orderby_opt ::= ORDER BY sortlist",
150293 /* 130 */ "sortlist ::= sortlist COMMA expr sortorder",
150294 /* 131 */ "sortlist ::= expr sortorder",
150295 /* 132 */ "sortorder ::= ASC",
150296 /* 133 */ "sortorder ::= DESC",
150297 /* 134 */ "sortorder ::=",
150298 /* 135 */ "groupby_opt ::=",
150299 /* 136 */ "groupby_opt ::= GROUP BY nexprlist",
150300 /* 137 */ "having_opt ::=",
150301 /* 138 */ "having_opt ::= HAVING expr",
150302 /* 139 */ "limit_opt ::=",
150303 /* 140 */ "limit_opt ::= LIMIT expr",
150304 /* 141 */ "limit_opt ::= LIMIT expr OFFSET expr",
150305 /* 142 */ "limit_opt ::= LIMIT expr COMMA expr",
150306 /* 143 */ "cmd ::= with DELETE FROM xfullname indexed_opt where_opt",
150307 /* 144 */ "where_opt ::=",
150308 /* 145 */ "where_opt ::= WHERE expr",
150309 /* 146 */ "cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt",
150310 /* 147 */ "setlist ::= setlist COMMA nm EQ expr",
150311 /* 148 */ "setlist ::= setlist COMMA LP idlist RP EQ expr",
150312 /* 149 */ "setlist ::= nm EQ expr",
150313 /* 150 */ "setlist ::= LP idlist RP EQ expr",
150314 /* 151 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert",
150315 /* 152 */ "cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES",
150316 /* 153 */ "upsert ::=",
150317 /* 154 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt",
150318 /* 155 */ "upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING",
150319 /* 156 */ "upsert ::= ON CONFLICT DO NOTHING",
150320 /* 157 */ "insert_cmd ::= INSERT orconf",
150321 /* 158 */ "insert_cmd ::= REPLACE",
150322 /* 159 */ "idlist_opt ::=",
150323 /* 160 */ "idlist_opt ::= LP idlist RP",
150324 /* 161 */ "idlist ::= idlist COMMA nm",
150325 /* 162 */ "idlist ::= nm",
150326 /* 163 */ "expr ::= LP expr RP",
150327 /* 164 */ "expr ::= ID|INDEXED",
150328 /* 165 */ "expr ::= JOIN_KW",
150329 /* 166 */ "expr ::= nm DOT nm",
150330 /* 167 */ "expr ::= nm DOT nm DOT nm",
150331 /* 168 */ "term ::= NULL|FLOAT|BLOB",
150332 /* 169 */ "term ::= STRING",
150333 /* 170 */ "term ::= INTEGER",
150334 /* 171 */ "expr ::= VARIABLE",
150335 /* 172 */ "expr ::= expr COLLATE ID|STRING",
150336 /* 173 */ "expr ::= CAST LP expr AS typetoken RP",
150337 /* 174 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
150338 /* 175 */ "expr ::= ID|INDEXED LP STAR RP",
150339 /* 176 */ "expr ::= ID|INDEXED LP distinct exprlist RP over_clause",
150340 /* 177 */ "expr ::= ID|INDEXED LP STAR RP over_clause",
150341 /* 178 */ "term ::= CTIME_KW",
150342 /* 179 */ "expr ::= LP nexprlist COMMA expr RP",
150343 /* 180 */ "expr ::= expr AND expr",
150344 /* 181 */ "expr ::= expr OR expr",
150345 /* 182 */ "expr ::= expr LT|GT|GE|LE expr",
150346 /* 183 */ "expr ::= expr EQ|NE expr",
150347 /* 184 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
150348 /* 185 */ "expr ::= expr PLUS|MINUS expr",
150349 /* 186 */ "expr ::= expr STAR|SLASH|REM expr",
150350 /* 187 */ "expr ::= expr CONCAT expr",
150351 /* 188 */ "likeop ::= NOT LIKE_KW|MATCH",
150352 /* 189 */ "expr ::= expr likeop expr",
150353 /* 190 */ "expr ::= expr likeop expr ESCAPE expr",
150354 /* 191 */ "expr ::= expr ISNULL|NOTNULL",
150355 /* 192 */ "expr ::= expr NOT NULL",
150356 /* 193 */ "expr ::= expr IS expr",
150357 /* 194 */ "expr ::= expr IS NOT expr",
150358 /* 195 */ "expr ::= NOT expr",
150359 /* 196 */ "expr ::= BITNOT expr",
150360 /* 197 */ "expr ::= PLUS|MINUS expr",
150361 /* 198 */ "between_op ::= BETWEEN",
150362 /* 199 */ "between_op ::= NOT BETWEEN",
150363 /* 200 */ "expr ::= expr between_op expr AND expr",
150364 /* 201 */ "in_op ::= IN",
150365 /* 202 */ "in_op ::= NOT IN",
150366 /* 203 */ "expr ::= expr in_op LP exprlist RP",
150367 /* 204 */ "expr ::= LP select RP",
150368 /* 205 */ "expr ::= expr in_op LP select RP",
150369 /* 206 */ "expr ::= expr in_op nm dbnm paren_exprlist",
150370 /* 207 */ "expr ::= EXISTS LP select RP",
150371 /* 208 */ "expr ::= CASE case_operand case_exprlist case_else END",
150372 /* 209 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
150373 /* 210 */ "case_exprlist ::= WHEN expr THEN expr",
150374 /* 211 */ "case_else ::= ELSE expr",
150375 /* 212 */ "case_else ::=",
150376 /* 213 */ "case_operand ::= expr",
150377 /* 214 */ "case_operand ::=",
150378 /* 215 */ "exprlist ::=",
150379 /* 216 */ "nexprlist ::= nexprlist COMMA expr",
150380 /* 217 */ "nexprlist ::= expr",
150381 /* 218 */ "paren_exprlist ::=",
150382 /* 219 */ "paren_exprlist ::= LP exprlist RP",
150383 /* 220 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt",
150384 /* 221 */ "uniqueflag ::= UNIQUE",
150385 /* 222 */ "uniqueflag ::=",
150386 /* 223 */ "eidlist_opt ::=",
150387 /* 224 */ "eidlist_opt ::= LP eidlist RP",
150388 /* 225 */ "eidlist ::= eidlist COMMA nm collate sortorder",
150389 /* 226 */ "eidlist ::= nm collate sortorder",
150390 /* 227 */ "collate ::=",
150391 /* 228 */ "collate ::= COLLATE ID|STRING",
150392 /* 229 */ "cmd ::= DROP INDEX ifexists fullname",
150393 /* 230 */ "cmd ::= VACUUM vinto",
150394 /* 231 */ "cmd ::= VACUUM nm vinto",
150395 /* 232 */ "vinto ::= INTO expr",
150396 /* 233 */ "vinto ::=",
150397 /* 234 */ "cmd ::= PRAGMA nm dbnm",
150398 /* 235 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
150399 /* 236 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
150400 /* 237 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
150401 /* 238 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
150402 /* 239 */ "plus_num ::= PLUS INTEGER|FLOAT",
150403 /* 240 */ "minus_num ::= MINUS INTEGER|FLOAT",
150404 /* 241 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
150405 /* 242 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
150406 /* 243 */ "trigger_time ::= BEFORE|AFTER",
150407 /* 244 */ "trigger_time ::= INSTEAD OF",
150408 /* 245 */ "trigger_time ::=",
150409 /* 246 */ "trigger_event ::= DELETE|INSERT",
150410 /* 247 */ "trigger_event ::= UPDATE",
150411 /* 248 */ "trigger_event ::= UPDATE OF idlist",
150412 /* 249 */ "when_clause ::=",
150413 /* 250 */ "when_clause ::= WHEN expr",
150414 /* 251 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
150415 /* 252 */ "trigger_cmd_list ::= trigger_cmd SEMI",
150416 /* 253 */ "trnm ::= nm DOT nm",
150417 /* 254 */ "tridxby ::= INDEXED BY nm",
150418 /* 255 */ "tridxby ::= NOT INDEXED",
150419 /* 256 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt",
150420 /* 257 */ "trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt",
150421 /* 258 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt",
150422 /* 259 */ "trigger_cmd ::= scanpt select scanpt",
150423 /* 260 */ "expr ::= RAISE LP IGNORE RP",
150424 /* 261 */ "expr ::= RAISE LP raisetype COMMA nm RP",
150425 /* 262 */ "raisetype ::= ROLLBACK",
150426 /* 263 */ "raisetype ::= ABORT",
150427 /* 264 */ "raisetype ::= FAIL",
150428 /* 265 */ "cmd ::= DROP TRIGGER ifexists fullname",
150429 /* 266 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
150430 /* 267 */ "cmd ::= DETACH database_kw_opt expr",
150431 /* 268 */ "key_opt ::=",
150432 /* 269 */ "key_opt ::= KEY expr",
150433 /* 270 */ "cmd ::= REINDEX",
150434 /* 271 */ "cmd ::= REINDEX nm dbnm",
150435 /* 272 */ "cmd ::= ANALYZE",
150436 /* 273 */ "cmd ::= ANALYZE nm dbnm",
150437 /* 274 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
150438 /* 275 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist",
150439 /* 276 */ "add_column_fullname ::= fullname",
150440 /* 277 */ "cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm",
150441 /* 278 */ "cmd ::= create_vtab",
150442 /* 279 */ "cmd ::= create_vtab LP vtabarglist RP",
150443 /* 280 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
150444 /* 281 */ "vtabarg ::=",
150445 /* 282 */ "vtabargtoken ::= ANY",
150446 /* 283 */ "vtabargtoken ::= lp anylist RP",
150447 /* 284 */ "lp ::= LP",
150448 /* 285 */ "with ::= WITH wqlist",
150449 /* 286 */ "with ::= WITH RECURSIVE wqlist",
150450 /* 287 */ "wqlist ::= nm eidlist_opt AS LP select RP",
150451 /* 288 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP",
150452 /* 289 */ "windowdefn_list ::= windowdefn",
150453 /* 290 */ "windowdefn_list ::= windowdefn_list COMMA windowdefn",
150454 /* 291 */ "windowdefn ::= nm AS LP window RP",
150455 /* 292 */ "window ::= PARTITION BY nexprlist orderby_opt frame_opt",
150456 /* 293 */ "window ::= nm PARTITION BY nexprlist orderby_opt frame_opt",
150457 /* 294 */ "window ::= ORDER BY sortlist frame_opt",
150458 /* 295 */ "window ::= nm ORDER BY sortlist frame_opt",
150459 /* 296 */ "window ::= frame_opt",
150460 /* 297 */ "window ::= nm frame_opt",
150461 /* 298 */ "frame_opt ::=",
150462 /* 299 */ "frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt",
150463 /* 300 */ "frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt",
150464 /* 301 */ "range_or_rows ::= RANGE|ROWS|GROUPS",
150465 /* 302 */ "frame_bound_s ::= frame_bound",
150466 /* 303 */ "frame_bound_s ::= UNBOUNDED PRECEDING",
150467 /* 304 */ "frame_bound_e ::= frame_bound",
150468 /* 305 */ "frame_bound_e ::= UNBOUNDED FOLLOWING",
150469 /* 306 */ "frame_bound ::= expr PRECEDING|FOLLOWING",
150470 /* 307 */ "frame_bound ::= CURRENT ROW",
150471 /* 308 */ "frame_exclude_opt ::=",
150472 /* 309 */ "frame_exclude_opt ::= EXCLUDE frame_exclude",
150473 /* 310 */ "frame_exclude ::= NO OTHERS",
150474 /* 311 */ "frame_exclude ::= CURRENT ROW",
150475 /* 312 */ "frame_exclude ::= GROUP|TIES",
150476 /* 313 */ "window_clause ::= WINDOW windowdefn_list",
150477 /* 314 */ "over_clause ::= filter_opt OVER LP window RP",
150478 /* 315 */ "over_clause ::= filter_opt OVER nm",
150479 /* 316 */ "filter_opt ::=",
150480 /* 317 */ "filter_opt ::= FILTER LP WHERE expr RP",
150481 /* 318 */ "input ::= cmdlist",
150482 /* 319 */ "cmdlist ::= cmdlist ecmd",
150483 /* 320 */ "cmdlist ::= ecmd",
150484 /* 321 */ "ecmd ::= SEMI",
150485 /* 322 */ "ecmd ::= cmdx SEMI",
150486 /* 323 */ "ecmd ::= explain cmdx",
150487 /* 324 */ "trans_opt ::=",
150488 /* 325 */ "trans_opt ::= TRANSACTION",
150489 /* 326 */ "trans_opt ::= TRANSACTION nm",
150490 /* 327 */ "savepoint_opt ::= SAVEPOINT",
150491 /* 328 */ "savepoint_opt ::=",
150492 /* 329 */ "cmd ::= create_table create_table_args",
150493 /* 330 */ "columnlist ::= columnlist COMMA columnname carglist",
150494 /* 331 */ "columnlist ::= columnname carglist",
150495 /* 332 */ "nm ::= ID|INDEXED",
150496 /* 333 */ "nm ::= STRING",
150497 /* 334 */ "nm ::= JOIN_KW",
150498 /* 335 */ "typetoken ::= typename",
150499 /* 336 */ "typename ::= ID|STRING",
150500 /* 337 */ "signed ::= plus_num",
150501 /* 338 */ "signed ::= minus_num",
150502 /* 339 */ "carglist ::= carglist ccons",
150503 /* 340 */ "carglist ::=",
150504 /* 341 */ "ccons ::= NULL onconf",
150505 /* 342 */ "conslist_opt ::= COMMA conslist",
150506 /* 343 */ "conslist ::= conslist tconscomma tcons",
150507 /* 344 */ "conslist ::= tcons",
150508 /* 345 */ "tconscomma ::=",
150509 /* 346 */ "defer_subclause_opt ::= defer_subclause",
150510 /* 347 */ "resolvetype ::= raisetype",
150511 /* 348 */ "selectnowith ::= oneselect",
150512 /* 349 */ "oneselect ::= values",
150513 /* 350 */ "sclp ::= selcollist COMMA",
150514 /* 351 */ "as ::= ID|STRING",
150515 /* 352 */ "expr ::= term",
150516 /* 353 */ "likeop ::= LIKE_KW|MATCH",
150517 /* 354 */ "exprlist ::= nexprlist",
150518 /* 355 */ "nmnum ::= plus_num",
150519 /* 356 */ "nmnum ::= nm",
150520 /* 357 */ "nmnum ::= ON",
150521 /* 358 */ "nmnum ::= DELETE",
150522 /* 359 */ "nmnum ::= DEFAULT",
150523 /* 360 */ "plus_num ::= INTEGER|FLOAT",
150524 /* 361 */ "foreach_clause ::=",
150525 /* 362 */ "foreach_clause ::= FOR EACH ROW",
150526 /* 363 */ "trnm ::= nm",
150527 /* 364 */ "tridxby ::=",
150528 /* 365 */ "database_kw_opt ::= DATABASE",
150529 /* 366 */ "database_kw_opt ::=",
150530 /* 367 */ "kwcolumn_opt ::=",
150531 /* 368 */ "kwcolumn_opt ::= COLUMNKW",
150532 /* 369 */ "vtabarglist ::= vtabarg",
150533 /* 370 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
150534 /* 371 */ "vtabarg ::= vtabarg vtabargtoken",
150535 /* 372 */ "anylist ::=",
150536 /* 373 */ "anylist ::= anylist LP anylist RP",
150537 /* 374 */ "anylist ::= anylist ANY",
150538 /* 375 */ "with ::=",
150539};
150540#endif /* NDEBUG */
150541
150542
150543#if YYSTACKDEPTH100<=0
150544/*
150545** Try to increase the size of the parser stack. Return the number
150546** of errors. Return 0 on success.
150547*/
150548static int yyGrowStack(yyParser *p){
150549 int newSize;
150550 int idx;
150551 yyStackEntry *pNew;
150552
150553 newSize = p->yystksz*2 + 100;
150554 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
150555 if( p->yystack==&p->yystk0 ){
150556 pNew = malloc(newSize*sizeof(pNew[0]));
150557 if( pNew ) pNew[0] = p->yystk0;
150558 }else{
150559 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
150560 }
150561 if( pNew ){
150562 p->yystack = pNew;
150563 p->yytos = &p->yystack[idx];
150564#ifndef NDEBUG1
150565 if( yyTraceFILE ){
150566 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n",
150567 yyTracePrompt, p->yystksz, newSize);
150568 }
150569#endif
150570 p->yystksz = newSize;
150571 }
150572 return pNew==0;
150573}
150574#endif
150575
150576/* Datatype of the argument to the memory allocated passed as the
150577** second argument to sqlite3ParserAlloc() below. This can be changed by
150578** putting an appropriate #define in the %include section of the input
150579** grammar.
150580*/
150581#ifndef YYMALLOCARGTYPEu64
150582# define YYMALLOCARGTYPEu64 size_t
150583#endif
150584
150585/* Initialize a new parser that has already been allocated.
150586*/
150587SQLITE_PRIVATEstatic void sqlite3ParserInit(void *yypRawParser sqlite3ParserCTX_PDECL,Parse *pParse){
150588 yyParser *yypParser = (yyParser*)yypRawParser;
150589 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
150590#ifdef YYTRACKMAXSTACKDEPTH
150591 yypParser->yyhwm = 0;
150592#endif
150593#if YYSTACKDEPTH100<=0
150594 yypParser->yytos = NULL((void*)0);
150595 yypParser->yystack = NULL((void*)0);
150596 yypParser->yystksz = 0;
150597 if( yyGrowStack(yypParser) ){
150598 yypParser->yystack = &yypParser->yystk0;
150599 yypParser->yystksz = 1;
150600 }
150601#endif
150602#ifndef YYNOERRORRECOVERY1
150603 yypParser->yyerrcnt = -1;
150604#endif
150605 yypParser->yytos = yypParser->yystack;
150606 yypParser->yystack[0].stateno = 0;
150607 yypParser->yystack[0].major = 0;
150608#if YYSTACKDEPTH100>0
150609 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH100-1];
150610#endif
150611}
150612
150613#ifndef sqlite3Parser_ENGINEALWAYSONSTACK1
150614/*
150615** This function allocates a new parser.
150616** The only argument is a pointer to a function which works like
150617** malloc.
150618**
150619** Inputs:
150620** A pointer to the function used to allocate memory.
150621**
150622** Outputs:
150623** A pointer to a parser. This pointer is used in subsequent calls
150624** to sqlite3Parser and sqlite3ParserFree.
150625*/
150626SQLITE_PRIVATEstatic void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPEu64) sqlite3ParserCTX_PDECL,Parse *pParse){
150627 yyParser *yypParser;
150628 yypParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPEu64)sizeof(yyParser) );
150629 if( yypParser ){
150630 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
150631 sqlite3ParserInit(yypParser sqlite3ParserCTX_PARAM,pParse);
150632 }
150633 return (void*)yypParser;
150634}
150635#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
150636
150637
150638/* The following function deletes the "minor type" or semantic value
150639** associated with a symbol. The symbol can be either a terminal
150640** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
150641** a pointer to the value to be deleted. The code used to do the
150642** deletions is derived from the %destructor and/or %token_destructor
150643** directives of the input grammar.
150644*/
150645static void yy_destructor(
150646 yyParser *yypParser, /* The parser */
150647 YYCODETYPEunsigned short int yymajor, /* Type code for object to destroy */
150648 YYMINORTYPE *yypminor /* The object to be destroyed */
150649){
150650 sqlite3ParserARG_FETCH
150651 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
150652 switch( yymajor ){
150653 /* Here is inserted the actions which take place when a
150654 ** terminal or non-terminal is destroyed. This can happen
150655 ** when the symbol is popped from the stack during a
150656 ** reduce or during error processing or when a parser is
150657 ** being destroyed before it is finished parsing.
150658 **
150659 ** Note: during a reduce, the only symbols destroyed are those
150660 ** which appear on the RHS of the rule, but which are *not* used
150661 ** inside the C code.
150662 */
150663/********* Begin destructor definitions ***************************************/
150664 case 195: /* select */
150665 case 228: /* selectnowith */
150666 case 229: /* oneselect */
150667 case 241: /* values */
150668{
150669sqlite3SelectDelete(pParse->db, (yypminor->yy391));
150670}
150671 break;
150672 case 206: /* term */
150673 case 207: /* expr */
150674 case 235: /* where_opt */
150675 case 237: /* having_opt */
150676 case 249: /* on_opt */
150677 case 264: /* case_operand */
150678 case 266: /* case_else */
150679 case 269: /* vinto */
150680 case 276: /* when_clause */
150681 case 281: /* key_opt */
150682 case 295: /* filter_opt */
150683{
150684sqlite3ExprDelete(pParse->db, (yypminor->yy102));
150685}
150686 break;
150687 case 211: /* eidlist_opt */
150688 case 220: /* sortlist */
150689 case 221: /* eidlist */
150690 case 233: /* selcollist */
150691 case 236: /* groupby_opt */
150692 case 238: /* orderby_opt */
150693 case 242: /* nexprlist */
150694 case 243: /* sclp */
150695 case 251: /* exprlist */
150696 case 255: /* setlist */
150697 case 263: /* paren_exprlist */
150698 case 265: /* case_exprlist */
150699 case 294: /* part_opt */
150700{
150701sqlite3ExprListDelete(pParse->db, (yypminor->yy94));
150702}
150703 break;
150704 case 227: /* fullname */
150705 case 234: /* from */
150706 case 245: /* seltablist */
150707 case 246: /* stl_prefix */
150708 case 252: /* xfullname */
150709{
150710sqlite3SrcListDelete(pParse->db, (yypminor->yy407));
150711}
150712 break;
150713 case 230: /* wqlist */
150714{
150715sqlite3WithDelete(pParse->db, (yypminor->yy243));
150716}
150717 break;
150718 case 240: /* window_clause */
150719 case 290: /* windowdefn_list */
150720{
150721sqlite3WindowListDelete(pParse->db, (yypminor->yy379));
150722}
150723 break;
150724 case 250: /* using_opt */
150725 case 253: /* idlist */
150726 case 257: /* idlist_opt */
150727{
150728sqlite3IdListDelete(pParse->db, (yypminor->yy76));
150729}
150730 break;
150731 case 259: /* over_clause */
150732 case 291: /* windowdefn */
150733 case 292: /* window */
150734 case 293: /* frame_opt */
150735{
150736sqlite3WindowDelete(pParse->db, (yypminor->yy379));
150737}
150738 break;
150739 case 272: /* trigger_cmd_list */
150740 case 277: /* trigger_cmd */
150741{
150742sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy11));
150743}
150744 break;
150745 case 274: /* trigger_event */
150746{
150747sqlite3IdListDelete(pParse->db, (yypminor->yy298).b);
150748}
150749 break;
150750 case 297: /* frame_bound */
150751 case 298: /* frame_bound_s */
150752 case 299: /* frame_bound_e */
150753{
150754sqlite3ExprDelete(pParse->db, (yypminor->yy389).pExpr);
150755}
150756 break;
150757/********* End destructor definitions *****************************************/
150758 default: break; /* If no destructor action specified: do nothing */
150759 }
150760}
150761
150762/*
150763** Pop the parser's stack once.
150764**
150765** If there is a destructor routine associated with the token which
150766** is popped from the stack, then call it.
150767*/
150768static void yy_pop_parser_stack(yyParser *pParser){
150769 yyStackEntry *yytos;
150770 assert( pParser->yytos!=0 )((void) (0));
150771 assert( pParser->yytos > pParser->yystack )((void) (0));
150772 yytos = pParser->yytos--;
150773#ifndef NDEBUG1
150774 if( yyTraceFILE ){
150775 fprintf(yyTraceFILE,"%sPopping %s\n",
150776 yyTracePrompt,
150777 yyTokenName[yytos->major]);
150778 }
150779#endif
150780 yy_destructor(pParser, yytos->major, &yytos->minor);
150781}
150782
150783/*
150784** Clear all secondary memory allocations from the parser
150785*/
150786SQLITE_PRIVATEstatic void sqlite3ParserFinalize(void *p){
150787 yyParser *pParser = (yyParser*)p;
150788 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
150789#if YYSTACKDEPTH100<=0
150790 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
150791#endif
150792}
150793
150794#ifndef sqlite3Parser_ENGINEALWAYSONSTACK1
150795/*
150796** Deallocate and destroy a parser. Destructors are called for
150797** all stack elements before shutting the parser down.
150798**
150799** If the YYPARSEFREENEVERNULL macro exists (for example because it
150800** is defined in a %include section of the input grammar) then it is
150801** assumed that the input pointer is never NULL.
150802*/
150803SQLITE_PRIVATEstatic void sqlite3ParserFree(
150804 void *p, /* The parser to be deleted */
150805 void (*freeProc)(void*) /* Function used to reclaim memory */
150806){
150807#ifndef YYPARSEFREENEVERNULL1
150808 if( p==0 ) return;
150809#endif
150810 sqlite3ParserFinalize(p);
150811 (*freeProc)(p);
150812}
150813#endif /* sqlite3Parser_ENGINEALWAYSONSTACK */
150814
150815/*
150816** Return the peak depth of the stack for a parser.
150817*/
150818#ifdef YYTRACKMAXSTACKDEPTH
150819SQLITE_PRIVATEstatic int sqlite3ParserStackPeak(void *p){
150820 yyParser *pParser = (yyParser*)p;
150821 return pParser->yyhwm;
150822}
150823#endif
150824
150825/* This array of booleans keeps track of the parser statement
150826** coverage. The element yycoverage[X][Y] is set when the parser
150827** is in state X and has a lookahead token Y. In a well-tested
150828** systems, every element of this matrix should end up being set.
150829*/
150830#if defined(YYCOVERAGE)
150831static unsigned char yycoverage[YYNSTATE540][YYNTOKEN176];
150832#endif
150833
150834/*
150835** Write into out a description of every state/lookahead combination that
150836**
150837** (1) has not been used by the parser, and
150838** (2) is not a syntax error.
150839**
150840** Return the number of missed state/lookahead combinations.
150841*/
150842#if defined(YYCOVERAGE)
150843SQLITE_PRIVATEstatic int sqlite3ParserCoverage(FILE *out){
150844 int stateno, iLookAhead, i;
150845 int nMissed = 0;
150846 for(stateno=0; stateno<YYNSTATE540; stateno++){
150847 i = yy_shift_ofst[stateno];
150848 for(iLookAhead=0; iLookAhead<YYNTOKEN176; iLookAhead++){
150849 if( yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
150850 if( yycoverage[stateno][iLookAhead]==0 ) nMissed++;
150851 if( out ){
150852 fprintf(out,"State %d lookahead %s %s\n", stateno,
150853 yyTokenName[iLookAhead],
150854 yycoverage[stateno][iLookAhead] ? "ok" : "missed");
150855 }
150856 }
150857 }
150858 return nMissed;
150859}
150860#endif
150861
150862/*
150863** Find the appropriate action for a parser given the terminal
150864** look-ahead token iLookAhead.
150865*/
150866static YYACTIONTYPEunsigned short int yy_find_shift_action(
150867 YYCODETYPEunsigned short int iLookAhead, /* The look-ahead token */
150868 YYACTIONTYPEunsigned short int stateno /* Current state number */
150869){
150870 int i;
150871
150872 if( stateno>YY_MAX_SHIFT539 ) return stateno;
150873 assert( stateno <= YY_SHIFT_COUNT )((void) (0));
150874#if defined(YYCOVERAGE)
150875 yycoverage[stateno][iLookAhead] = 1;
150876#endif
150877 do{
150878 i = yy_shift_ofst[stateno];
150879 assert( i>=0 )((void) (0));
150880 /* assert( i+YYNTOKEN<=(int)YY_NLOOKAHEAD ); */
150881 assert( iLookAhead!=YYNOCODE )((void) (0));
150882 assert( iLookAhead < YYNTOKEN )((void) (0));
150883 i += iLookAhead;
150884 if( i>=YY_NLOOKAHEAD((int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0]))) || yy_lookahead[i]!=iLookAhead ){
150885#ifdef YYFALLBACK1
150886 YYCODETYPEunsigned short int iFallback; /* Fallback token */
150887 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
150888 && (iFallback = yyFallback[iLookAhead])!=0 ){
150889#ifndef NDEBUG1
150890 if( yyTraceFILE ){
150891 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
150892 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
150893 }
150894#endif
150895 assert( yyFallback[iFallback]==0 )((void) (0)); /* Fallback loop must terminate */
150896 iLookAhead = iFallback;
150897 continue;
150898 }
150899#endif
150900#ifdef YYWILDCARD95
150901 {
150902 int j = i - iLookAhead + YYWILDCARD95;
150903 if(
150904#if YY_SHIFT_MIN(0)+YYWILDCARD95<0
150905 j>=0 &&
150906#endif
150907#if YY_SHIFT_MAX(1987)+YYWILDCARD95>=YY_ACTTAB_COUNT(2142)
150908 j<YY_ACTTAB_COUNT(2142) &&
150909#endif
150910 j<(int)(sizeof(yy_lookahead)/sizeof(yy_lookahead[0])) &&
150911 yy_lookahead[j]==YYWILDCARD95 && iLookAhead>0
150912 ){
150913#ifndef NDEBUG1
150914 if( yyTraceFILE ){
150915 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
150916 yyTracePrompt, yyTokenName[iLookAhead],
150917 yyTokenName[YYWILDCARD95]);
150918 }
150919#endif /* NDEBUG */
150920 return yy_action[j];
150921 }
150922 }
150923#endif /* YYWILDCARD */
150924 return yy_default[stateno];
150925 }else{
150926 return yy_action[i];
150927 }
150928 }while(1);
150929}
150930
150931/*
150932** Find the appropriate action for a parser given the non-terminal
150933** look-ahead token iLookAhead.
150934*/
150935static YYACTIONTYPEunsigned short int yy_find_reduce_action(
150936 YYACTIONTYPEunsigned short int stateno, /* Current state number */
150937 YYCODETYPEunsigned short int iLookAhead /* The look-ahead token */
150938){
150939 int i;
150940#ifdef YYERRORSYMBOL
150941 if( stateno>YY_REDUCE_COUNT(382) ){
150942 return yy_default[stateno];
150943 }
150944#else
150945 assert( stateno<=YY_REDUCE_COUNT )((void) (0));
150946#endif
150947 i = yy_reduce_ofst[stateno];
150948 assert( iLookAhead!=YYNOCODE )((void) (0));
150949 i += iLookAhead;
150950#ifdef YYERRORSYMBOL
150951 if( i<0 || i>=YY_ACTTAB_COUNT(2142) || yy_lookahead[i]!=iLookAhead ){
150952 return yy_default[stateno];
150953 }
150954#else
150955 assert( i>=0 && i<YY_ACTTAB_COUNT )((void) (0));
150956 assert( yy_lookahead[i]==iLookAhead )((void) (0));
150957#endif
150958 return yy_action[i];
150959}
150960
150961/*
150962** The following routine is called if the stack overflows.
150963*/
150964static void yyStackOverflow(yyParser *yypParser){
150965 sqlite3ParserARG_FETCH
150966 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
150967#ifndef NDEBUG1
150968 if( yyTraceFILE ){
150969 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
150970 }
150971#endif
150972 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
150973 /* Here code is inserted which will execute if the parser
150974 ** stack every overflows */
150975/******** Begin %stack_overflow code ******************************************/
150976
150977 sqlite3ErrorMsg(pParse, "parser stack overflow");
150978/******** End %stack_overflow code ********************************************/
150979 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument var */
150980 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
150981}
150982
150983/*
150984** Print tracing information for a SHIFT action
150985*/
150986#ifndef NDEBUG1
150987static void yyTraceShift(yyParser *yypParser, int yyNewState, const char *zTag){
150988 if( yyTraceFILE ){
150989 if( yyNewState<YYNSTATE540 ){
150990 fprintf(yyTraceFILE,"%s%s '%s', go to state %d\n",
150991 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
150992 yyNewState);
150993 }else{
150994 fprintf(yyTraceFILE,"%s%s '%s', pending reduce %d\n",
150995 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
150996 yyNewState - YY_MIN_REDUCE1162);
150997 }
150998 }
150999}
151000#else
151001# define yyTraceShift(X,Y,Z)
151002#endif
151003
151004/*
151005** Perform a shift action.
151006*/
151007static void yy_shift(
151008 yyParser *yypParser, /* The parser to be shifted */
151009 YYACTIONTYPEunsigned short int yyNewState, /* The new state to shift in */
151010 YYCODETYPEunsigned short int yyMajor, /* The major token to shift in */
151011 sqlite3ParserTOKENTYPEToken yyMinor /* The minor token to shift in */
151012){
151013 yyStackEntry *yytos;
151014 yypParser->yytos++;
151015#ifdef YYTRACKMAXSTACKDEPTH
151016 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
151017 yypParser->yyhwm++;
151018 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) )((void) (0));
151019 }
151020#endif
151021#if YYSTACKDEPTH100>0
151022 if( yypParser->yytos>yypParser->yystackEnd ){
151023 yypParser->yytos--;
151024 yyStackOverflow(yypParser);
151025 return;
151026 }
151027#else
151028 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
151029 if( yyGrowStack(yypParser) ){
151030 yypParser->yytos--;
151031 yyStackOverflow(yypParser);
151032 return;
151033 }
151034 }
151035#endif
151036 if( yyNewState > YY_MAX_SHIFT539 ){
151037 yyNewState += YY_MIN_REDUCE1162 - YY_MIN_SHIFTREDUCE783;
151038 }
151039 yytos = yypParser->yytos;
151040 yytos->stateno = yyNewState;
151041 yytos->major = yyMajor;
151042 yytos->minor.yy0 = yyMinor;
151043 yyTraceShift(yypParser, yyNewState, "Shift");
151044}
151045
151046/* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
151047** of that rule */
151048static const YYCODETYPEunsigned short int yyRuleInfoLhs[] = {
151049 180, /* (0) explain ::= EXPLAIN */
151050 180, /* (1) explain ::= EXPLAIN QUERY PLAN */
151051 179, /* (2) cmdx ::= cmd */
151052 181, /* (3) cmd ::= BEGIN transtype trans_opt */
151053 182, /* (4) transtype ::= */
151054 182, /* (5) transtype ::= DEFERRED */
151055 182, /* (6) transtype ::= IMMEDIATE */
151056 182, /* (7) transtype ::= EXCLUSIVE */
151057 181, /* (8) cmd ::= COMMIT|END trans_opt */
151058 181, /* (9) cmd ::= ROLLBACK trans_opt */
151059 181, /* (10) cmd ::= SAVEPOINT nm */
151060 181, /* (11) cmd ::= RELEASE savepoint_opt nm */
151061 181, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
151062 186, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
151063 188, /* (14) createkw ::= CREATE */
151064 190, /* (15) ifnotexists ::= */
151065 190, /* (16) ifnotexists ::= IF NOT EXISTS */
151066 189, /* (17) temp ::= TEMP */
151067 189, /* (18) temp ::= */
151068 187, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
151069 187, /* (20) create_table_args ::= AS select */
151070 194, /* (21) table_options ::= */
151071 194, /* (22) table_options ::= WITHOUT nm */
151072 196, /* (23) columnname ::= nm typetoken */
151073 198, /* (24) typetoken ::= */
151074 198, /* (25) typetoken ::= typename LP signed RP */
151075 198, /* (26) typetoken ::= typename LP signed COMMA signed RP */
151076 199, /* (27) typename ::= typename ID|STRING */
151077 203, /* (28) scanpt ::= */
151078 204, /* (29) scantok ::= */
151079 205, /* (30) ccons ::= CONSTRAINT nm */
151080 205, /* (31) ccons ::= DEFAULT scantok term */
151081 205, /* (32) ccons ::= DEFAULT LP expr RP */
151082 205, /* (33) ccons ::= DEFAULT PLUS scantok term */
151083 205, /* (34) ccons ::= DEFAULT MINUS scantok term */
151084 205, /* (35) ccons ::= DEFAULT scantok ID|INDEXED */
151085 205, /* (36) ccons ::= NOT NULL onconf */
151086 205, /* (37) ccons ::= PRIMARY KEY sortorder onconf autoinc */
151087 205, /* (38) ccons ::= UNIQUE onconf */
151088 205, /* (39) ccons ::= CHECK LP expr RP */
151089 205, /* (40) ccons ::= REFERENCES nm eidlist_opt refargs */
151090 205, /* (41) ccons ::= defer_subclause */
151091 205, /* (42) ccons ::= COLLATE ID|STRING */
151092 210, /* (43) autoinc ::= */
151093 210, /* (44) autoinc ::= AUTOINCR */
151094 212, /* (45) refargs ::= */
151095 212, /* (46) refargs ::= refargs refarg */
151096 214, /* (47) refarg ::= MATCH nm */
151097 214, /* (48) refarg ::= ON INSERT refact */
151098 214, /* (49) refarg ::= ON DELETE refact */
151099 214, /* (50) refarg ::= ON UPDATE refact */
151100 215, /* (51) refact ::= SET NULL */
151101 215, /* (52) refact ::= SET DEFAULT */
151102 215, /* (53) refact ::= CASCADE */
151103 215, /* (54) refact ::= RESTRICT */
151104 215, /* (55) refact ::= NO ACTION */
151105 213, /* (56) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
151106 213, /* (57) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
151107 216, /* (58) init_deferred_pred_opt ::= */
151108 216, /* (59) init_deferred_pred_opt ::= INITIALLY DEFERRED */
151109 216, /* (60) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
151110 193, /* (61) conslist_opt ::= */
151111 218, /* (62) tconscomma ::= COMMA */
151112 219, /* (63) tcons ::= CONSTRAINT nm */
151113 219, /* (64) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
151114 219, /* (65) tcons ::= UNIQUE LP sortlist RP onconf */
151115 219, /* (66) tcons ::= CHECK LP expr RP onconf */
151116 219, /* (67) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
151117 222, /* (68) defer_subclause_opt ::= */
151118 208, /* (69) onconf ::= */
151119 208, /* (70) onconf ::= ON CONFLICT resolvetype */
151120 223, /* (71) orconf ::= */
151121 223, /* (72) orconf ::= OR resolvetype */
151122 224, /* (73) resolvetype ::= IGNORE */
151123 224, /* (74) resolvetype ::= REPLACE */
151124 181, /* (75) cmd ::= DROP TABLE ifexists fullname */
151125 226, /* (76) ifexists ::= IF EXISTS */
151126 226, /* (77) ifexists ::= */
151127 181, /* (78) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
151128 181, /* (79) cmd ::= DROP VIEW ifexists fullname */
151129 181, /* (80) cmd ::= select */
151130 195, /* (81) select ::= WITH wqlist selectnowith */
151131 195, /* (82) select ::= WITH RECURSIVE wqlist selectnowith */
151132 195, /* (83) select ::= selectnowith */
151133 228, /* (84) selectnowith ::= selectnowith multiselect_op oneselect */
151134 231, /* (85) multiselect_op ::= UNION */
151135 231, /* (86) multiselect_op ::= UNION ALL */
151136 231, /* (87) multiselect_op ::= EXCEPT|INTERSECT */
151137 229, /* (88) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
151138 229, /* (89) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
151139 241, /* (90) values ::= VALUES LP nexprlist RP */
151140 241, /* (91) values ::= values COMMA LP nexprlist RP */
151141 232, /* (92) distinct ::= DISTINCT */
151142 232, /* (93) distinct ::= ALL */
151143 232, /* (94) distinct ::= */
151144 243, /* (95) sclp ::= */
151145 233, /* (96) selcollist ::= sclp scanpt expr scanpt as */
151146 233, /* (97) selcollist ::= sclp scanpt STAR */
151147 233, /* (98) selcollist ::= sclp scanpt nm DOT STAR */
151148 244, /* (99) as ::= AS nm */
151149 244, /* (100) as ::= */
151150 234, /* (101) from ::= */
151151 234, /* (102) from ::= FROM seltablist */
151152 246, /* (103) stl_prefix ::= seltablist joinop */
151153 246, /* (104) stl_prefix ::= */
151154 245, /* (105) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
151155 245, /* (106) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
151156 245, /* (107) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
151157 245, /* (108) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
151158 191, /* (109) dbnm ::= */
151159 191, /* (110) dbnm ::= DOT nm */
151160 227, /* (111) fullname ::= nm */
151161 227, /* (112) fullname ::= nm DOT nm */
151162 252, /* (113) xfullname ::= nm */
151163 252, /* (114) xfullname ::= nm DOT nm */
151164 252, /* (115) xfullname ::= nm DOT nm AS nm */
151165 252, /* (116) xfullname ::= nm AS nm */
151166 247, /* (117) joinop ::= COMMA|JOIN */
151167 247, /* (118) joinop ::= JOIN_KW JOIN */
151168 247, /* (119) joinop ::= JOIN_KW nm JOIN */
151169 247, /* (120) joinop ::= JOIN_KW nm nm JOIN */
151170 249, /* (121) on_opt ::= ON expr */
151171 249, /* (122) on_opt ::= */
151172 248, /* (123) indexed_opt ::= */
151173 248, /* (124) indexed_opt ::= INDEXED BY nm */
151174 248, /* (125) indexed_opt ::= NOT INDEXED */
151175 250, /* (126) using_opt ::= USING LP idlist RP */
151176 250, /* (127) using_opt ::= */
151177 238, /* (128) orderby_opt ::= */
151178 238, /* (129) orderby_opt ::= ORDER BY sortlist */
151179 220, /* (130) sortlist ::= sortlist COMMA expr sortorder */
151180 220, /* (131) sortlist ::= expr sortorder */
151181 209, /* (132) sortorder ::= ASC */
151182 209, /* (133) sortorder ::= DESC */
151183 209, /* (134) sortorder ::= */
151184 236, /* (135) groupby_opt ::= */
151185 236, /* (136) groupby_opt ::= GROUP BY nexprlist */
151186 237, /* (137) having_opt ::= */
151187 237, /* (138) having_opt ::= HAVING expr */
151188 239, /* (139) limit_opt ::= */
151189 239, /* (140) limit_opt ::= LIMIT expr */
151190 239, /* (141) limit_opt ::= LIMIT expr OFFSET expr */
151191 239, /* (142) limit_opt ::= LIMIT expr COMMA expr */
151192 181, /* (143) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
151193 235, /* (144) where_opt ::= */
151194 235, /* (145) where_opt ::= WHERE expr */
151195 181, /* (146) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
151196 255, /* (147) setlist ::= setlist COMMA nm EQ expr */
151197 255, /* (148) setlist ::= setlist COMMA LP idlist RP EQ expr */
151198 255, /* (149) setlist ::= nm EQ expr */
151199 255, /* (150) setlist ::= LP idlist RP EQ expr */
151200 181, /* (151) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
151201 181, /* (152) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
151202 258, /* (153) upsert ::= */
151203 258, /* (154) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
151204 258, /* (155) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
151205 258, /* (156) upsert ::= ON CONFLICT DO NOTHING */
151206 256, /* (157) insert_cmd ::= INSERT orconf */
151207 256, /* (158) insert_cmd ::= REPLACE */
151208 257, /* (159) idlist_opt ::= */
151209 257, /* (160) idlist_opt ::= LP idlist RP */
151210 253, /* (161) idlist ::= idlist COMMA nm */
151211 253, /* (162) idlist ::= nm */
151212 207, /* (163) expr ::= LP expr RP */
151213 207, /* (164) expr ::= ID|INDEXED */
151214 207, /* (165) expr ::= JOIN_KW */
151215 207, /* (166) expr ::= nm DOT nm */
151216 207, /* (167) expr ::= nm DOT nm DOT nm */
151217 206, /* (168) term ::= NULL|FLOAT|BLOB */
151218 206, /* (169) term ::= STRING */
151219 206, /* (170) term ::= INTEGER */
151220 207, /* (171) expr ::= VARIABLE */
151221 207, /* (172) expr ::= expr COLLATE ID|STRING */
151222 207, /* (173) expr ::= CAST LP expr AS typetoken RP */
151223 207, /* (174) expr ::= ID|INDEXED LP distinct exprlist RP */
151224 207, /* (175) expr ::= ID|INDEXED LP STAR RP */
151225 207, /* (176) expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
151226 207, /* (177) expr ::= ID|INDEXED LP STAR RP over_clause */
151227 206, /* (178) term ::= CTIME_KW */
151228 207, /* (179) expr ::= LP nexprlist COMMA expr RP */
151229 207, /* (180) expr ::= expr AND expr */
151230 207, /* (181) expr ::= expr OR expr */
151231 207, /* (182) expr ::= expr LT|GT|GE|LE expr */
151232 207, /* (183) expr ::= expr EQ|NE expr */
151233 207, /* (184) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
151234 207, /* (185) expr ::= expr PLUS|MINUS expr */
151235 207, /* (186) expr ::= expr STAR|SLASH|REM expr */
151236 207, /* (187) expr ::= expr CONCAT expr */
151237 260, /* (188) likeop ::= NOT LIKE_KW|MATCH */
151238 207, /* (189) expr ::= expr likeop expr */
151239 207, /* (190) expr ::= expr likeop expr ESCAPE expr */
151240 207, /* (191) expr ::= expr ISNULL|NOTNULL */
151241 207, /* (192) expr ::= expr NOT NULL */
151242 207, /* (193) expr ::= expr IS expr */
151243 207, /* (194) expr ::= expr IS NOT expr */
151244 207, /* (195) expr ::= NOT expr */
151245 207, /* (196) expr ::= BITNOT expr */
151246 207, /* (197) expr ::= PLUS|MINUS expr */
151247 261, /* (198) between_op ::= BETWEEN */
151248 261, /* (199) between_op ::= NOT BETWEEN */
151249 207, /* (200) expr ::= expr between_op expr AND expr */
151250 262, /* (201) in_op ::= IN */
151251 262, /* (202) in_op ::= NOT IN */
151252 207, /* (203) expr ::= expr in_op LP exprlist RP */
151253 207, /* (204) expr ::= LP select RP */
151254 207, /* (205) expr ::= expr in_op LP select RP */
151255 207, /* (206) expr ::= expr in_op nm dbnm paren_exprlist */
151256 207, /* (207) expr ::= EXISTS LP select RP */
151257 207, /* (208) expr ::= CASE case_operand case_exprlist case_else END */
151258 265, /* (209) case_exprlist ::= case_exprlist WHEN expr THEN expr */
151259 265, /* (210) case_exprlist ::= WHEN expr THEN expr */
151260 266, /* (211) case_else ::= ELSE expr */
151261 266, /* (212) case_else ::= */
151262 264, /* (213) case_operand ::= expr */
151263 264, /* (214) case_operand ::= */
151264 251, /* (215) exprlist ::= */
151265 242, /* (216) nexprlist ::= nexprlist COMMA expr */
151266 242, /* (217) nexprlist ::= expr */
151267 263, /* (218) paren_exprlist ::= */
151268 263, /* (219) paren_exprlist ::= LP exprlist RP */
151269 181, /* (220) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
151270 267, /* (221) uniqueflag ::= UNIQUE */
151271 267, /* (222) uniqueflag ::= */
151272 211, /* (223) eidlist_opt ::= */
151273 211, /* (224) eidlist_opt ::= LP eidlist RP */
151274 221, /* (225) eidlist ::= eidlist COMMA nm collate sortorder */
151275 221, /* (226) eidlist ::= nm collate sortorder */
151276 268, /* (227) collate ::= */
151277 268, /* (228) collate ::= COLLATE ID|STRING */
151278 181, /* (229) cmd ::= DROP INDEX ifexists fullname */
151279 181, /* (230) cmd ::= VACUUM vinto */
151280 181, /* (231) cmd ::= VACUUM nm vinto */
151281 269, /* (232) vinto ::= INTO expr */
151282 269, /* (233) vinto ::= */
151283 181, /* (234) cmd ::= PRAGMA nm dbnm */
151284 181, /* (235) cmd ::= PRAGMA nm dbnm EQ nmnum */
151285 181, /* (236) cmd ::= PRAGMA nm dbnm LP nmnum RP */
151286 181, /* (237) cmd ::= PRAGMA nm dbnm EQ minus_num */
151287 181, /* (238) cmd ::= PRAGMA nm dbnm LP minus_num RP */
151288 201, /* (239) plus_num ::= PLUS INTEGER|FLOAT */
151289 202, /* (240) minus_num ::= MINUS INTEGER|FLOAT */
151290 181, /* (241) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
151291 271, /* (242) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
151292 273, /* (243) trigger_time ::= BEFORE|AFTER */
151293 273, /* (244) trigger_time ::= INSTEAD OF */
151294 273, /* (245) trigger_time ::= */
151295 274, /* (246) trigger_event ::= DELETE|INSERT */
151296 274, /* (247) trigger_event ::= UPDATE */
151297 274, /* (248) trigger_event ::= UPDATE OF idlist */
151298 276, /* (249) when_clause ::= */
151299 276, /* (250) when_clause ::= WHEN expr */
151300 272, /* (251) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
151301 272, /* (252) trigger_cmd_list ::= trigger_cmd SEMI */
151302 278, /* (253) trnm ::= nm DOT nm */
151303 279, /* (254) tridxby ::= INDEXED BY nm */
151304 279, /* (255) tridxby ::= NOT INDEXED */
151305 277, /* (256) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
151306 277, /* (257) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
151307 277, /* (258) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
151308 277, /* (259) trigger_cmd ::= scanpt select scanpt */
151309 207, /* (260) expr ::= RAISE LP IGNORE RP */
151310 207, /* (261) expr ::= RAISE LP raisetype COMMA nm RP */
151311 225, /* (262) raisetype ::= ROLLBACK */
151312 225, /* (263) raisetype ::= ABORT */
151313 225, /* (264) raisetype ::= FAIL */
151314 181, /* (265) cmd ::= DROP TRIGGER ifexists fullname */
151315 181, /* (266) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
151316 181, /* (267) cmd ::= DETACH database_kw_opt expr */
151317 281, /* (268) key_opt ::= */
151318 281, /* (269) key_opt ::= KEY expr */
151319 181, /* (270) cmd ::= REINDEX */
151320 181, /* (271) cmd ::= REINDEX nm dbnm */
151321 181, /* (272) cmd ::= ANALYZE */
151322 181, /* (273) cmd ::= ANALYZE nm dbnm */
151323 181, /* (274) cmd ::= ALTER TABLE fullname RENAME TO nm */
151324 181, /* (275) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
151325 282, /* (276) add_column_fullname ::= fullname */
151326 181, /* (277) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
151327 181, /* (278) cmd ::= create_vtab */
151328 181, /* (279) cmd ::= create_vtab LP vtabarglist RP */
151329 284, /* (280) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
151330 286, /* (281) vtabarg ::= */
151331 287, /* (282) vtabargtoken ::= ANY */
151332 287, /* (283) vtabargtoken ::= lp anylist RP */
151333 288, /* (284) lp ::= LP */
151334 254, /* (285) with ::= WITH wqlist */
151335 254, /* (286) with ::= WITH RECURSIVE wqlist */
151336 230, /* (287) wqlist ::= nm eidlist_opt AS LP select RP */
151337 230, /* (288) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
151338 290, /* (289) windowdefn_list ::= windowdefn */
151339 290, /* (290) windowdefn_list ::= windowdefn_list COMMA windowdefn */
151340 291, /* (291) windowdefn ::= nm AS LP window RP */
151341 292, /* (292) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
151342 292, /* (293) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
151343 292, /* (294) window ::= ORDER BY sortlist frame_opt */
151344 292, /* (295) window ::= nm ORDER BY sortlist frame_opt */
151345 292, /* (296) window ::= frame_opt */
151346 292, /* (297) window ::= nm frame_opt */
151347 293, /* (298) frame_opt ::= */
151348 293, /* (299) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
151349 293, /* (300) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
151350 296, /* (301) range_or_rows ::= RANGE|ROWS|GROUPS */
151351 298, /* (302) frame_bound_s ::= frame_bound */
151352 298, /* (303) frame_bound_s ::= UNBOUNDED PRECEDING */
151353 299, /* (304) frame_bound_e ::= frame_bound */
151354 299, /* (305) frame_bound_e ::= UNBOUNDED FOLLOWING */
151355 297, /* (306) frame_bound ::= expr PRECEDING|FOLLOWING */
151356 297, /* (307) frame_bound ::= CURRENT ROW */
151357 300, /* (308) frame_exclude_opt ::= */
151358 300, /* (309) frame_exclude_opt ::= EXCLUDE frame_exclude */
151359 301, /* (310) frame_exclude ::= NO OTHERS */
151360 301, /* (311) frame_exclude ::= CURRENT ROW */
151361 301, /* (312) frame_exclude ::= GROUP|TIES */
151362 240, /* (313) window_clause ::= WINDOW windowdefn_list */
151363 259, /* (314) over_clause ::= filter_opt OVER LP window RP */
151364 259, /* (315) over_clause ::= filter_opt OVER nm */
151365 295, /* (316) filter_opt ::= */
151366 295, /* (317) filter_opt ::= FILTER LP WHERE expr RP */
151367 176, /* (318) input ::= cmdlist */
151368 177, /* (319) cmdlist ::= cmdlist ecmd */
151369 177, /* (320) cmdlist ::= ecmd */
151370 178, /* (321) ecmd ::= SEMI */
151371 178, /* (322) ecmd ::= cmdx SEMI */
151372 178, /* (323) ecmd ::= explain cmdx */
151373 183, /* (324) trans_opt ::= */
151374 183, /* (325) trans_opt ::= TRANSACTION */
151375 183, /* (326) trans_opt ::= TRANSACTION nm */
151376 185, /* (327) savepoint_opt ::= SAVEPOINT */
151377 185, /* (328) savepoint_opt ::= */
151378 181, /* (329) cmd ::= create_table create_table_args */
151379 192, /* (330) columnlist ::= columnlist COMMA columnname carglist */
151380 192, /* (331) columnlist ::= columnname carglist */
151381 184, /* (332) nm ::= ID|INDEXED */
151382 184, /* (333) nm ::= STRING */
151383 184, /* (334) nm ::= JOIN_KW */
151384 198, /* (335) typetoken ::= typename */
151385 199, /* (336) typename ::= ID|STRING */
151386 200, /* (337) signed ::= plus_num */
151387 200, /* (338) signed ::= minus_num */
151388 197, /* (339) carglist ::= carglist ccons */
151389 197, /* (340) carglist ::= */
151390 205, /* (341) ccons ::= NULL onconf */
151391 193, /* (342) conslist_opt ::= COMMA conslist */
151392 217, /* (343) conslist ::= conslist tconscomma tcons */
151393 217, /* (344) conslist ::= tcons */
151394 218, /* (345) tconscomma ::= */
151395 222, /* (346) defer_subclause_opt ::= defer_subclause */
151396 224, /* (347) resolvetype ::= raisetype */
151397 228, /* (348) selectnowith ::= oneselect */
151398 229, /* (349) oneselect ::= values */
151399 243, /* (350) sclp ::= selcollist COMMA */
151400 244, /* (351) as ::= ID|STRING */
151401 207, /* (352) expr ::= term */
151402 260, /* (353) likeop ::= LIKE_KW|MATCH */
151403 251, /* (354) exprlist ::= nexprlist */
151404 270, /* (355) nmnum ::= plus_num */
151405 270, /* (356) nmnum ::= nm */
151406 270, /* (357) nmnum ::= ON */
151407 270, /* (358) nmnum ::= DELETE */
151408 270, /* (359) nmnum ::= DEFAULT */
151409 201, /* (360) plus_num ::= INTEGER|FLOAT */
151410 275, /* (361) foreach_clause ::= */
151411 275, /* (362) foreach_clause ::= FOR EACH ROW */
151412 278, /* (363) trnm ::= nm */
151413 279, /* (364) tridxby ::= */
151414 280, /* (365) database_kw_opt ::= DATABASE */
151415 280, /* (366) database_kw_opt ::= */
151416 283, /* (367) kwcolumn_opt ::= */
151417 283, /* (368) kwcolumn_opt ::= COLUMNKW */
151418 285, /* (369) vtabarglist ::= vtabarg */
151419 285, /* (370) vtabarglist ::= vtabarglist COMMA vtabarg */
151420 286, /* (371) vtabarg ::= vtabarg vtabargtoken */
151421 289, /* (372) anylist ::= */
151422 289, /* (373) anylist ::= anylist LP anylist RP */
151423 289, /* (374) anylist ::= anylist ANY */
151424 254, /* (375) with ::= */
151425};
151426
151427/* For rule J, yyRuleInfoNRhs[J] contains the negative of the number
151428** of symbols on the right-hand side of that rule. */
151429static const signed char yyRuleInfoNRhs[] = {
151430 -1, /* (0) explain ::= EXPLAIN */
151431 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
151432 -1, /* (2) cmdx ::= cmd */
151433 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
151434 0, /* (4) transtype ::= */
151435 -1, /* (5) transtype ::= DEFERRED */
151436 -1, /* (6) transtype ::= IMMEDIATE */
151437 -1, /* (7) transtype ::= EXCLUSIVE */
151438 -2, /* (8) cmd ::= COMMIT|END trans_opt */
151439 -2, /* (9) cmd ::= ROLLBACK trans_opt */
151440 -2, /* (10) cmd ::= SAVEPOINT nm */
151441 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
151442 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
151443 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
151444 -1, /* (14) createkw ::= CREATE */
151445 0, /* (15) ifnotexists ::= */
151446 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
151447 -1, /* (17) temp ::= TEMP */
151448 0, /* (18) temp ::= */
151449 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
151450 -2, /* (20) create_table_args ::= AS select */
151451 0, /* (21) table_options ::= */
151452 -2, /* (22) table_options ::= WITHOUT nm */
151453 -2, /* (23) columnname ::= nm typetoken */
151454 0, /* (24) typetoken ::= */
151455 -4, /* (25) typetoken ::= typename LP signed RP */
151456 -6, /* (26) typetoken ::= typename LP signed COMMA signed RP */
151457 -2, /* (27) typename ::= typename ID|STRING */
151458 0, /* (28) scanpt ::= */
151459 0, /* (29) scantok ::= */
151460 -2, /* (30) ccons ::= CONSTRAINT nm */
151461 -3, /* (31) ccons ::= DEFAULT scantok term */
151462 -4, /* (32) ccons ::= DEFAULT LP expr RP */
151463 -4, /* (33) ccons ::= DEFAULT PLUS scantok term */
151464 -4, /* (34) ccons ::= DEFAULT MINUS scantok term */
151465 -3, /* (35) ccons ::= DEFAULT scantok ID|INDEXED */
151466 -3, /* (36) ccons ::= NOT NULL onconf */
151467 -5, /* (37) ccons ::= PRIMARY KEY sortorder onconf autoinc */
151468 -2, /* (38) ccons ::= UNIQUE onconf */
151469 -4, /* (39) ccons ::= CHECK LP expr RP */
151470 -4, /* (40) ccons ::= REFERENCES nm eidlist_opt refargs */
151471 -1, /* (41) ccons ::= defer_subclause */
151472 -2, /* (42) ccons ::= COLLATE ID|STRING */
151473 0, /* (43) autoinc ::= */
151474 -1, /* (44) autoinc ::= AUTOINCR */
151475 0, /* (45) refargs ::= */
151476 -2, /* (46) refargs ::= refargs refarg */
151477 -2, /* (47) refarg ::= MATCH nm */
151478 -3, /* (48) refarg ::= ON INSERT refact */
151479 -3, /* (49) refarg ::= ON DELETE refact */
151480 -3, /* (50) refarg ::= ON UPDATE refact */
151481 -2, /* (51) refact ::= SET NULL */
151482 -2, /* (52) refact ::= SET DEFAULT */
151483 -1, /* (53) refact ::= CASCADE */
151484 -1, /* (54) refact ::= RESTRICT */
151485 -2, /* (55) refact ::= NO ACTION */
151486 -3, /* (56) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
151487 -2, /* (57) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
151488 0, /* (58) init_deferred_pred_opt ::= */
151489 -2, /* (59) init_deferred_pred_opt ::= INITIALLY DEFERRED */
151490 -2, /* (60) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
151491 0, /* (61) conslist_opt ::= */
151492 -1, /* (62) tconscomma ::= COMMA */
151493 -2, /* (63) tcons ::= CONSTRAINT nm */
151494 -7, /* (64) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
151495 -5, /* (65) tcons ::= UNIQUE LP sortlist RP onconf */
151496 -5, /* (66) tcons ::= CHECK LP expr RP onconf */
151497 -10, /* (67) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
151498 0, /* (68) defer_subclause_opt ::= */
151499 0, /* (69) onconf ::= */
151500 -3, /* (70) onconf ::= ON CONFLICT resolvetype */
151501 0, /* (71) orconf ::= */
151502 -2, /* (72) orconf ::= OR resolvetype */
151503 -1, /* (73) resolvetype ::= IGNORE */
151504 -1, /* (74) resolvetype ::= REPLACE */
151505 -4, /* (75) cmd ::= DROP TABLE ifexists fullname */
151506 -2, /* (76) ifexists ::= IF EXISTS */
151507 0, /* (77) ifexists ::= */
151508 -9, /* (78) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
151509 -4, /* (79) cmd ::= DROP VIEW ifexists fullname */
151510 -1, /* (80) cmd ::= select */
151511 -3, /* (81) select ::= WITH wqlist selectnowith */
151512 -4, /* (82) select ::= WITH RECURSIVE wqlist selectnowith */
151513 -1, /* (83) select ::= selectnowith */
151514 -3, /* (84) selectnowith ::= selectnowith multiselect_op oneselect */
151515 -1, /* (85) multiselect_op ::= UNION */
151516 -2, /* (86) multiselect_op ::= UNION ALL */
151517 -1, /* (87) multiselect_op ::= EXCEPT|INTERSECT */
151518 -9, /* (88) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
151519 -10, /* (89) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
151520 -4, /* (90) values ::= VALUES LP nexprlist RP */
151521 -5, /* (91) values ::= values COMMA LP nexprlist RP */
151522 -1, /* (92) distinct ::= DISTINCT */
151523 -1, /* (93) distinct ::= ALL */
151524 0, /* (94) distinct ::= */
151525 0, /* (95) sclp ::= */
151526 -5, /* (96) selcollist ::= sclp scanpt expr scanpt as */
151527 -3, /* (97) selcollist ::= sclp scanpt STAR */
151528 -5, /* (98) selcollist ::= sclp scanpt nm DOT STAR */
151529 -2, /* (99) as ::= AS nm */
151530 0, /* (100) as ::= */
151531 0, /* (101) from ::= */
151532 -2, /* (102) from ::= FROM seltablist */
151533 -2, /* (103) stl_prefix ::= seltablist joinop */
151534 0, /* (104) stl_prefix ::= */
151535 -7, /* (105) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
151536 -9, /* (106) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
151537 -7, /* (107) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
151538 -7, /* (108) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
151539 0, /* (109) dbnm ::= */
151540 -2, /* (110) dbnm ::= DOT nm */
151541 -1, /* (111) fullname ::= nm */
151542 -3, /* (112) fullname ::= nm DOT nm */
151543 -1, /* (113) xfullname ::= nm */
151544 -3, /* (114) xfullname ::= nm DOT nm */
151545 -5, /* (115) xfullname ::= nm DOT nm AS nm */
151546 -3, /* (116) xfullname ::= nm AS nm */
151547 -1, /* (117) joinop ::= COMMA|JOIN */
151548 -2, /* (118) joinop ::= JOIN_KW JOIN */
151549 -3, /* (119) joinop ::= JOIN_KW nm JOIN */
151550 -4, /* (120) joinop ::= JOIN_KW nm nm JOIN */
151551 -2, /* (121) on_opt ::= ON expr */
151552 0, /* (122) on_opt ::= */
151553 0, /* (123) indexed_opt ::= */
151554 -3, /* (124) indexed_opt ::= INDEXED BY nm */
151555 -2, /* (125) indexed_opt ::= NOT INDEXED */
151556 -4, /* (126) using_opt ::= USING LP idlist RP */
151557 0, /* (127) using_opt ::= */
151558 0, /* (128) orderby_opt ::= */
151559 -3, /* (129) orderby_opt ::= ORDER BY sortlist */
151560 -4, /* (130) sortlist ::= sortlist COMMA expr sortorder */
151561 -2, /* (131) sortlist ::= expr sortorder */
151562 -1, /* (132) sortorder ::= ASC */
151563 -1, /* (133) sortorder ::= DESC */
151564 0, /* (134) sortorder ::= */
151565 0, /* (135) groupby_opt ::= */
151566 -3, /* (136) groupby_opt ::= GROUP BY nexprlist */
151567 0, /* (137) having_opt ::= */
151568 -2, /* (138) having_opt ::= HAVING expr */
151569 0, /* (139) limit_opt ::= */
151570 -2, /* (140) limit_opt ::= LIMIT expr */
151571 -4, /* (141) limit_opt ::= LIMIT expr OFFSET expr */
151572 -4, /* (142) limit_opt ::= LIMIT expr COMMA expr */
151573 -6, /* (143) cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
151574 0, /* (144) where_opt ::= */
151575 -2, /* (145) where_opt ::= WHERE expr */
151576 -8, /* (146) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
151577 -5, /* (147) setlist ::= setlist COMMA nm EQ expr */
151578 -7, /* (148) setlist ::= setlist COMMA LP idlist RP EQ expr */
151579 -3, /* (149) setlist ::= nm EQ expr */
151580 -5, /* (150) setlist ::= LP idlist RP EQ expr */
151581 -7, /* (151) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
151582 -7, /* (152) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
151583 0, /* (153) upsert ::= */
151584 -11, /* (154) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
151585 -8, /* (155) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
151586 -4, /* (156) upsert ::= ON CONFLICT DO NOTHING */
151587 -2, /* (157) insert_cmd ::= INSERT orconf */
151588 -1, /* (158) insert_cmd ::= REPLACE */
151589 0, /* (159) idlist_opt ::= */
151590 -3, /* (160) idlist_opt ::= LP idlist RP */
151591 -3, /* (161) idlist ::= idlist COMMA nm */
151592 -1, /* (162) idlist ::= nm */
151593 -3, /* (163) expr ::= LP expr RP */
151594 -1, /* (164) expr ::= ID|INDEXED */
151595 -1, /* (165) expr ::= JOIN_KW */
151596 -3, /* (166) expr ::= nm DOT nm */
151597 -5, /* (167) expr ::= nm DOT nm DOT nm */
151598 -1, /* (168) term ::= NULL|FLOAT|BLOB */
151599 -1, /* (169) term ::= STRING */
151600 -1, /* (170) term ::= INTEGER */
151601 -1, /* (171) expr ::= VARIABLE */
151602 -3, /* (172) expr ::= expr COLLATE ID|STRING */
151603 -6, /* (173) expr ::= CAST LP expr AS typetoken RP */
151604 -5, /* (174) expr ::= ID|INDEXED LP distinct exprlist RP */
151605 -4, /* (175) expr ::= ID|INDEXED LP STAR RP */
151606 -6, /* (176) expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
151607 -5, /* (177) expr ::= ID|INDEXED LP STAR RP over_clause */
151608 -1, /* (178) term ::= CTIME_KW */
151609 -5, /* (179) expr ::= LP nexprlist COMMA expr RP */
151610 -3, /* (180) expr ::= expr AND expr */
151611 -3, /* (181) expr ::= expr OR expr */
151612 -3, /* (182) expr ::= expr LT|GT|GE|LE expr */
151613 -3, /* (183) expr ::= expr EQ|NE expr */
151614 -3, /* (184) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
151615 -3, /* (185) expr ::= expr PLUS|MINUS expr */
151616 -3, /* (186) expr ::= expr STAR|SLASH|REM expr */
151617 -3, /* (187) expr ::= expr CONCAT expr */
151618 -2, /* (188) likeop ::= NOT LIKE_KW|MATCH */
151619 -3, /* (189) expr ::= expr likeop expr */
151620 -5, /* (190) expr ::= expr likeop expr ESCAPE expr */
151621 -2, /* (191) expr ::= expr ISNULL|NOTNULL */
151622 -3, /* (192) expr ::= expr NOT NULL */
151623 -3, /* (193) expr ::= expr IS expr */
151624 -4, /* (194) expr ::= expr IS NOT expr */
151625 -2, /* (195) expr ::= NOT expr */
151626 -2, /* (196) expr ::= BITNOT expr */
151627 -2, /* (197) expr ::= PLUS|MINUS expr */
151628 -1, /* (198) between_op ::= BETWEEN */
151629 -2, /* (199) between_op ::= NOT BETWEEN */
151630 -5, /* (200) expr ::= expr between_op expr AND expr */
151631 -1, /* (201) in_op ::= IN */
151632 -2, /* (202) in_op ::= NOT IN */
151633 -5, /* (203) expr ::= expr in_op LP exprlist RP */
151634 -3, /* (204) expr ::= LP select RP */
151635 -5, /* (205) expr ::= expr in_op LP select RP */
151636 -5, /* (206) expr ::= expr in_op nm dbnm paren_exprlist */
151637 -4, /* (207) expr ::= EXISTS LP select RP */
151638 -5, /* (208) expr ::= CASE case_operand case_exprlist case_else END */
151639 -5, /* (209) case_exprlist ::= case_exprlist WHEN expr THEN expr */
151640 -4, /* (210) case_exprlist ::= WHEN expr THEN expr */
151641 -2, /* (211) case_else ::= ELSE expr */
151642 0, /* (212) case_else ::= */
151643 -1, /* (213) case_operand ::= expr */
151644 0, /* (214) case_operand ::= */
151645 0, /* (215) exprlist ::= */
151646 -3, /* (216) nexprlist ::= nexprlist COMMA expr */
151647 -1, /* (217) nexprlist ::= expr */
151648 0, /* (218) paren_exprlist ::= */
151649 -3, /* (219) paren_exprlist ::= LP exprlist RP */
151650 -12, /* (220) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
151651 -1, /* (221) uniqueflag ::= UNIQUE */
151652 0, /* (222) uniqueflag ::= */
151653 0, /* (223) eidlist_opt ::= */
151654 -3, /* (224) eidlist_opt ::= LP eidlist RP */
151655 -5, /* (225) eidlist ::= eidlist COMMA nm collate sortorder */
151656 -3, /* (226) eidlist ::= nm collate sortorder */
151657 0, /* (227) collate ::= */
151658 -2, /* (228) collate ::= COLLATE ID|STRING */
151659 -4, /* (229) cmd ::= DROP INDEX ifexists fullname */
151660 -2, /* (230) cmd ::= VACUUM vinto */
151661 -3, /* (231) cmd ::= VACUUM nm vinto */
151662 -2, /* (232) vinto ::= INTO expr */
151663 0, /* (233) vinto ::= */
151664 -3, /* (234) cmd ::= PRAGMA nm dbnm */
151665 -5, /* (235) cmd ::= PRAGMA nm dbnm EQ nmnum */
151666 -6, /* (236) cmd ::= PRAGMA nm dbnm LP nmnum RP */
151667 -5, /* (237) cmd ::= PRAGMA nm dbnm EQ minus_num */
151668 -6, /* (238) cmd ::= PRAGMA nm dbnm LP minus_num RP */
151669 -2, /* (239) plus_num ::= PLUS INTEGER|FLOAT */
151670 -2, /* (240) minus_num ::= MINUS INTEGER|FLOAT */
151671 -5, /* (241) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
151672 -11, /* (242) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
151673 -1, /* (243) trigger_time ::= BEFORE|AFTER */
151674 -2, /* (244) trigger_time ::= INSTEAD OF */
151675 0, /* (245) trigger_time ::= */
151676 -1, /* (246) trigger_event ::= DELETE|INSERT */
151677 -1, /* (247) trigger_event ::= UPDATE */
151678 -3, /* (248) trigger_event ::= UPDATE OF idlist */
151679 0, /* (249) when_clause ::= */
151680 -2, /* (250) when_clause ::= WHEN expr */
151681 -3, /* (251) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
151682 -2, /* (252) trigger_cmd_list ::= trigger_cmd SEMI */
151683 -3, /* (253) trnm ::= nm DOT nm */
151684 -3, /* (254) tridxby ::= INDEXED BY nm */
151685 -2, /* (255) tridxby ::= NOT INDEXED */
151686 -8, /* (256) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
151687 -8, /* (257) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
151688 -6, /* (258) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
151689 -3, /* (259) trigger_cmd ::= scanpt select scanpt */
151690 -4, /* (260) expr ::= RAISE LP IGNORE RP */
151691 -6, /* (261) expr ::= RAISE LP raisetype COMMA nm RP */
151692 -1, /* (262) raisetype ::= ROLLBACK */
151693 -1, /* (263) raisetype ::= ABORT */
151694 -1, /* (264) raisetype ::= FAIL */
151695 -4, /* (265) cmd ::= DROP TRIGGER ifexists fullname */
151696 -6, /* (266) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
151697 -3, /* (267) cmd ::= DETACH database_kw_opt expr */
151698 0, /* (268) key_opt ::= */
151699 -2, /* (269) key_opt ::= KEY expr */
151700 -1, /* (270) cmd ::= REINDEX */
151701 -3, /* (271) cmd ::= REINDEX nm dbnm */
151702 -1, /* (272) cmd ::= ANALYZE */
151703 -3, /* (273) cmd ::= ANALYZE nm dbnm */
151704 -6, /* (274) cmd ::= ALTER TABLE fullname RENAME TO nm */
151705 -7, /* (275) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
151706 -1, /* (276) add_column_fullname ::= fullname */
151707 -8, /* (277) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
151708 -1, /* (278) cmd ::= create_vtab */
151709 -4, /* (279) cmd ::= create_vtab LP vtabarglist RP */
151710 -8, /* (280) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
151711 0, /* (281) vtabarg ::= */
151712 -1, /* (282) vtabargtoken ::= ANY */
151713 -3, /* (283) vtabargtoken ::= lp anylist RP */
151714 -1, /* (284) lp ::= LP */
151715 -2, /* (285) with ::= WITH wqlist */
151716 -3, /* (286) with ::= WITH RECURSIVE wqlist */
151717 -6, /* (287) wqlist ::= nm eidlist_opt AS LP select RP */
151718 -8, /* (288) wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
151719 -1, /* (289) windowdefn_list ::= windowdefn */
151720 -3, /* (290) windowdefn_list ::= windowdefn_list COMMA windowdefn */
151721 -5, /* (291) windowdefn ::= nm AS LP window RP */
151722 -5, /* (292) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
151723 -6, /* (293) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
151724 -4, /* (294) window ::= ORDER BY sortlist frame_opt */
151725 -5, /* (295) window ::= nm ORDER BY sortlist frame_opt */
151726 -1, /* (296) window ::= frame_opt */
151727 -2, /* (297) window ::= nm frame_opt */
151728 0, /* (298) frame_opt ::= */
151729 -3, /* (299) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
151730 -6, /* (300) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
151731 -1, /* (301) range_or_rows ::= RANGE|ROWS|GROUPS */
151732 -1, /* (302) frame_bound_s ::= frame_bound */
151733 -2, /* (303) frame_bound_s ::= UNBOUNDED PRECEDING */
151734 -1, /* (304) frame_bound_e ::= frame_bound */
151735 -2, /* (305) frame_bound_e ::= UNBOUNDED FOLLOWING */
151736 -2, /* (306) frame_bound ::= expr PRECEDING|FOLLOWING */
151737 -2, /* (307) frame_bound ::= CURRENT ROW */
151738 0, /* (308) frame_exclude_opt ::= */
151739 -2, /* (309) frame_exclude_opt ::= EXCLUDE frame_exclude */
151740 -2, /* (310) frame_exclude ::= NO OTHERS */
151741 -2, /* (311) frame_exclude ::= CURRENT ROW */
151742 -1, /* (312) frame_exclude ::= GROUP|TIES */
151743 -2, /* (313) window_clause ::= WINDOW windowdefn_list */
151744 -5, /* (314) over_clause ::= filter_opt OVER LP window RP */
151745 -3, /* (315) over_clause ::= filter_opt OVER nm */
151746 0, /* (316) filter_opt ::= */
151747 -5, /* (317) filter_opt ::= FILTER LP WHERE expr RP */
151748 -1, /* (318) input ::= cmdlist */
151749 -2, /* (319) cmdlist ::= cmdlist ecmd */
151750 -1, /* (320) cmdlist ::= ecmd */
151751 -1, /* (321) ecmd ::= SEMI */
151752 -2, /* (322) ecmd ::= cmdx SEMI */
151753 -2, /* (323) ecmd ::= explain cmdx */
151754 0, /* (324) trans_opt ::= */
151755 -1, /* (325) trans_opt ::= TRANSACTION */
151756 -2, /* (326) trans_opt ::= TRANSACTION nm */
151757 -1, /* (327) savepoint_opt ::= SAVEPOINT */
151758 0, /* (328) savepoint_opt ::= */
151759 -2, /* (329) cmd ::= create_table create_table_args */
151760 -4, /* (330) columnlist ::= columnlist COMMA columnname carglist */
151761 -2, /* (331) columnlist ::= columnname carglist */
151762 -1, /* (332) nm ::= ID|INDEXED */
151763 -1, /* (333) nm ::= STRING */
151764 -1, /* (334) nm ::= JOIN_KW */
151765 -1, /* (335) typetoken ::= typename */
151766 -1, /* (336) typename ::= ID|STRING */
151767 -1, /* (337) signed ::= plus_num */
151768 -1, /* (338) signed ::= minus_num */
151769 -2, /* (339) carglist ::= carglist ccons */
151770 0, /* (340) carglist ::= */
151771 -2, /* (341) ccons ::= NULL onconf */
151772 -2, /* (342) conslist_opt ::= COMMA conslist */
151773 -3, /* (343) conslist ::= conslist tconscomma tcons */
151774 -1, /* (344) conslist ::= tcons */
151775 0, /* (345) tconscomma ::= */
151776 -1, /* (346) defer_subclause_opt ::= defer_subclause */
151777 -1, /* (347) resolvetype ::= raisetype */
151778 -1, /* (348) selectnowith ::= oneselect */
151779 -1, /* (349) oneselect ::= values */
151780 -2, /* (350) sclp ::= selcollist COMMA */
151781 -1, /* (351) as ::= ID|STRING */
151782 -1, /* (352) expr ::= term */
151783 -1, /* (353) likeop ::= LIKE_KW|MATCH */
151784 -1, /* (354) exprlist ::= nexprlist */
151785 -1, /* (355) nmnum ::= plus_num */
151786 -1, /* (356) nmnum ::= nm */
151787 -1, /* (357) nmnum ::= ON */
151788 -1, /* (358) nmnum ::= DELETE */
151789 -1, /* (359) nmnum ::= DEFAULT */
151790 -1, /* (360) plus_num ::= INTEGER|FLOAT */
151791 0, /* (361) foreach_clause ::= */
151792 -3, /* (362) foreach_clause ::= FOR EACH ROW */
151793 -1, /* (363) trnm ::= nm */
151794 0, /* (364) tridxby ::= */
151795 -1, /* (365) database_kw_opt ::= DATABASE */
151796 0, /* (366) database_kw_opt ::= */
151797 0, /* (367) kwcolumn_opt ::= */
151798 -1, /* (368) kwcolumn_opt ::= COLUMNKW */
151799 -1, /* (369) vtabarglist ::= vtabarg */
151800 -3, /* (370) vtabarglist ::= vtabarglist COMMA vtabarg */
151801 -2, /* (371) vtabarg ::= vtabarg vtabargtoken */
151802 0, /* (372) anylist ::= */
151803 -4, /* (373) anylist ::= anylist LP anylist RP */
151804 -2, /* (374) anylist ::= anylist ANY */
151805 0, /* (375) with ::= */
151806};
151807
151808static void yy_accept(yyParser*); /* Forward Declaration */
151809
151810/*
151811** Perform a reduce action and the shift that must immediately
151812** follow the reduce.
151813**
151814** The yyLookahead and yyLookaheadToken parameters provide reduce actions
151815** access to the lookahead token (if any). The yyLookahead will be YYNOCODE
151816** if the lookahead token has already been consumed. As this procedure is
151817** only called from one place, optimizing compilers will in-line it, which
151818** means that the extra parameters have no performance impact.
151819*/
151820static YYACTIONTYPEunsigned short int yy_reduce(
151821 yyParser *yypParser, /* The parser */
151822 unsigned int yyruleno, /* Number of the rule by which to reduce */
151823 int yyLookahead, /* Lookahead token, or YYNOCODE if none */
151824 sqlite3ParserTOKENTYPEToken yyLookaheadToken /* Value of the lookahead token */
151825 sqlite3ParserCTX_PDECL,Parse *pParse /* %extra_context */
151826){
151827 int yygoto; /* The next state */
151828 YYACTIONTYPEunsigned short int yyact; /* The next action */
151829 yyStackEntry *yymsp; /* The top of the parser's stack */
151830 int yysize; /* Amount to pop the stack */
151831 sqlite3ParserARG_FETCH
151832 (void)yyLookahead;
151833 (void)yyLookaheadToken;
151834 yymsp = yypParser->yytos;
151835#ifndef NDEBUG1
151836 if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
151837 yysize = yyRuleInfoNRhs[yyruleno];
151838 if( yysize ){
151839 fprintf(yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
151840 yyTracePrompt,
151841 yyruleno, yyRuleName[yyruleno], yymsp[yysize].stateno);
151842 }else{
151843 fprintf(yyTraceFILE, "%sReduce %d [%s].\n",
151844 yyTracePrompt, yyruleno, yyRuleName[yyruleno]);
151845 }
151846 }
151847#endif /* NDEBUG */
151848
151849 /* Check that the stack is large enough to grow by a single entry
151850 ** if the RHS of the rule is empty. This ensures that there is room
151851 ** enough on the stack to push the LHS value */
151852 if( yyRuleInfoNRhs[yyruleno]==0 ){
151853#ifdef YYTRACKMAXSTACKDEPTH
151854 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
151855 yypParser->yyhwm++;
151856 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack))((void) (0));
151857 }
151858#endif
151859#if YYSTACKDEPTH100>0
151860 if( yypParser->yytos>=yypParser->yystackEnd ){
151861 yyStackOverflow(yypParser);
151862 /* The call to yyStackOverflow() above pops the stack until it is
151863 ** empty, causing the main parser loop to exit. So the return value
151864 ** is never used and does not matter. */
151865 return 0;
151866 }
151867#else
151868 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
151869 if( yyGrowStack(yypParser) ){
151870 yyStackOverflow(yypParser);
151871 /* The call to yyStackOverflow() above pops the stack until it is
151872 ** empty, causing the main parser loop to exit. So the return value
151873 ** is never used and does not matter. */
151874 return 0;
151875 }
151876 yymsp = yypParser->yytos;
151877 }
151878#endif
151879 }
151880
151881 switch( yyruleno ){
151882 /* Beginning here are the reduction cases. A typical example
151883 ** follows:
151884 ** case 0:
151885 ** #line <lineno> <grammarfile>
151886 ** { ... } // User supplied code
151887 ** #line <lineno> <thisfile>
151888 ** break;
151889 */
151890/********** Begin reduce actions **********************************************/
151891 YYMINORTYPE yylhsminor;
151892 case 0: /* explain ::= EXPLAIN */
151893{ pParse->explain = 1; }
151894 break;
151895 case 1: /* explain ::= EXPLAIN QUERY PLAN */
151896{ pParse->explain = 2; }
151897 break;
151898 case 2: /* cmdx ::= cmd */
151899{ sqlite3FinishCoding(pParse); }
151900 break;
151901 case 3: /* cmd ::= BEGIN transtype trans_opt */
151902{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy100);}
151903 break;
151904 case 4: /* transtype ::= */
151905{yymsp[1].minor.yy100 = TK_DEFERRED7;}
151906 break;
151907 case 5: /* transtype ::= DEFERRED */
151908 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6);
151909 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7);
151910 case 301: /* range_or_rows ::= RANGE|ROWS|GROUPS */ yytestcase(yyruleno==301);
151911{yymsp[0].minor.yy100 = yymsp[0].major; /*A-overwrites-X*/}
151912 break;
151913 case 8: /* cmd ::= COMMIT|END trans_opt */
151914 case 9: /* cmd ::= ROLLBACK trans_opt */ yytestcase(yyruleno==9);
151915{sqlite3EndTransaction(pParse,yymsp[-1].major);}
151916 break;
151917 case 10: /* cmd ::= SAVEPOINT nm */
151918{
151919 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN0, &yymsp[0].minor.yy0);
151920}
151921 break;
151922 case 11: /* cmd ::= RELEASE savepoint_opt nm */
151923{
151924 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE1, &yymsp[0].minor.yy0);
151925}
151926 break;
151927 case 12: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
151928{
151929 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK2, &yymsp[0].minor.yy0);
151930}
151931 break;
151932 case 13: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
151933{
151934 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy100,0,0,yymsp[-2].minor.yy100);
151935}
151936 break;
151937 case 14: /* createkw ::= CREATE */
151938{disableLookaside(pParse);}
151939 break;
151940 case 15: /* ifnotexists ::= */
151941 case 18: /* temp ::= */ yytestcase(yyruleno==18);
151942 case 21: /* table_options ::= */ yytestcase(yyruleno==21);
151943 case 43: /* autoinc ::= */ yytestcase(yyruleno==43);
151944 case 58: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==58);
151945 case 68: /* defer_subclause_opt ::= */ yytestcase(yyruleno==68);
151946 case 77: /* ifexists ::= */ yytestcase(yyruleno==77);
151947 case 94: /* distinct ::= */ yytestcase(yyruleno==94);
151948 case 227: /* collate ::= */ yytestcase(yyruleno==227);
151949{yymsp[1].minor.yy100 = 0;}
151950 break;
151951 case 16: /* ifnotexists ::= IF NOT EXISTS */
151952{yymsp[-2].minor.yy100 = 1;}
151953 break;
151954 case 17: /* temp ::= TEMP */
151955 case 44: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==44);
151956{yymsp[0].minor.yy100 = 1;}
151957 break;
151958 case 19: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
151959{
151960 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy100,0);
151961}
151962 break;
151963 case 20: /* create_table_args ::= AS select */
151964{
151965 sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy391);
151966 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy391);
151967}
151968 break;
151969 case 22: /* table_options ::= WITHOUT nm */
151970{
151971 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
151972 yymsp[-1].minor.yy100 = TF_WithoutRowid0x0020 | TF_NoVisibleRowid0x0040;
151973 }else{
151974 yymsp[-1].minor.yy100 = 0;
151975 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
151976 }
151977}
151978 break;
151979 case 23: /* columnname ::= nm typetoken */
151980{sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
151981 break;
151982 case 24: /* typetoken ::= */
151983 case 61: /* conslist_opt ::= */ yytestcase(yyruleno==61);
151984 case 100: /* as ::= */ yytestcase(yyruleno==100);
151985{yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;}
151986 break;
151987 case 25: /* typetoken ::= typename LP signed RP */
151988{
151989 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
151990}
151991 break;
151992 case 26: /* typetoken ::= typename LP signed COMMA signed RP */
151993{
151994 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
151995}
151996 break;
151997 case 27: /* typename ::= typename ID|STRING */
151998{yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
151999 break;
152000 case 28: /* scanpt ::= */
152001{
152002 assert( yyLookahead!=YYNOCODE )((void) (0));
152003 yymsp[1].minor.yy528 = yyLookaheadToken.z;
152004}
152005 break;
152006 case 29: /* scantok ::= */
152007{
152008 assert( yyLookahead!=YYNOCODE )((void) (0));
152009 yymsp[1].minor.yy0 = yyLookaheadToken;
152010}
152011 break;
152012 case 30: /* ccons ::= CONSTRAINT nm */
152013 case 63: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==63);
152014{pParse->constraintName = yymsp[0].minor.yy0;}
152015 break;
152016 case 31: /* ccons ::= DEFAULT scantok term */
152017{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy102,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
152018 break;
152019 case 32: /* ccons ::= DEFAULT LP expr RP */
152020{sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy102,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
152021 break;
152022 case 33: /* ccons ::= DEFAULT PLUS scantok term */
152023{sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy102,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);}
152024 break;
152025 case 34: /* ccons ::= DEFAULT MINUS scantok term */
152026{
152027 Expr *p = sqlite3PExpr(pParse, TK_UMINUS165, yymsp[0].minor.yy102, 0);
152028 sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]);
152029}
152030 break;
152031 case 35: /* ccons ::= DEFAULT scantok ID|INDEXED */
152032{
152033 Expr *p = tokenExpr(pParse, TK_STRING110, yymsp[0].minor.yy0);
152034 if( p ){
152035 sqlite3ExprIdToTrueFalse(p);
152036 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
152037 }
152038 sqlite3AddDefaultValue(pParse,p,yymsp[0].minor.yy0.z,yymsp[0].minor.yy0.z+yymsp[0].minor.yy0.n);
152039}
152040 break;
152041 case 36: /* ccons ::= NOT NULL onconf */
152042{sqlite3AddNotNull(pParse, yymsp[0].minor.yy100);}
152043 break;
152044 case 37: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
152045{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy100,yymsp[0].minor.yy100,yymsp[-2].minor.yy100);}
152046 break;
152047 case 38: /* ccons ::= UNIQUE onconf */
152048{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy100,0,0,0,0,
152049 SQLITE_IDXTYPE_UNIQUE1);}
152050 break;
152051 case 39: /* ccons ::= CHECK LP expr RP */
152052{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy102);}
152053 break;
152054 case 40: /* ccons ::= REFERENCES nm eidlist_opt refargs */
152055{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy94,yymsp[0].minor.yy100);}
152056 break;
152057 case 41: /* ccons ::= defer_subclause */
152058{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy100);}
152059 break;
152060 case 42: /* ccons ::= COLLATE ID|STRING */
152061{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
152062 break;
152063 case 45: /* refargs ::= */
152064{ yymsp[1].minor.yy100 = OE_None0*0x0101; /* EV: R-19803-45884 */}
152065 break;
152066 case 46: /* refargs ::= refargs refarg */
152067{ yymsp[-1].minor.yy100 = (yymsp[-1].minor.yy100 & ~yymsp[0].minor.yy199.mask) | yymsp[0].minor.yy199.value; }
152068 break;
152069 case 47: /* refarg ::= MATCH nm */
152070{ yymsp[-1].minor.yy199.value = 0; yymsp[-1].minor.yy199.mask = 0x000000; }
152071 break;
152072 case 48: /* refarg ::= ON INSERT refact */
152073{ yymsp[-2].minor.yy199.value = 0; yymsp[-2].minor.yy199.mask = 0x000000; }
152074 break;
152075 case 49: /* refarg ::= ON DELETE refact */
152076{ yymsp[-2].minor.yy199.value = yymsp[0].minor.yy100; yymsp[-2].minor.yy199.mask = 0x0000ff; }
152077 break;
152078 case 50: /* refarg ::= ON UPDATE refact */
152079{ yymsp[-2].minor.yy199.value = yymsp[0].minor.yy100<<8; yymsp[-2].minor.yy199.mask = 0x00ff00; }
152080 break;
152081 case 51: /* refact ::= SET NULL */
152082{ yymsp[-1].minor.yy100 = OE_SetNull8; /* EV: R-33326-45252 */}
152083 break;
152084 case 52: /* refact ::= SET DEFAULT */
152085{ yymsp[-1].minor.yy100 = OE_SetDflt9; /* EV: R-33326-45252 */}
152086 break;
152087 case 53: /* refact ::= CASCADE */
152088{ yymsp[0].minor.yy100 = OE_Cascade10; /* EV: R-33326-45252 */}
152089 break;
152090 case 54: /* refact ::= RESTRICT */
152091{ yymsp[0].minor.yy100 = OE_Restrict7; /* EV: R-33326-45252 */}
152092 break;
152093 case 55: /* refact ::= NO ACTION */
152094{ yymsp[-1].minor.yy100 = OE_None0; /* EV: R-33326-45252 */}
152095 break;
152096 case 56: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
152097{yymsp[-2].minor.yy100 = 0;}
152098 break;
152099 case 57: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
152100 case 72: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==72);
152101 case 157: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==157);
152102{yymsp[-1].minor.yy100 = yymsp[0].minor.yy100;}
152103 break;
152104 case 59: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */
152105 case 76: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==76);
152106 case 199: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==199);
152107 case 202: /* in_op ::= NOT IN */ yytestcase(yyruleno==202);
152108 case 228: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==228);
152109{yymsp[-1].minor.yy100 = 1;}
152110 break;
152111 case 60: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
152112{yymsp[-1].minor.yy100 = 0;}
152113 break;
152114 case 62: /* tconscomma ::= COMMA */
152115{pParse->constraintName.n = 0;}
152116 break;
152117 case 64: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
152118{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy94,yymsp[0].minor.yy100,yymsp[-2].minor.yy100,0);}
152119 break;
152120 case 65: /* tcons ::= UNIQUE LP sortlist RP onconf */
152121{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy94,yymsp[0].minor.yy100,0,0,0,0,
152122 SQLITE_IDXTYPE_UNIQUE1);}
152123 break;
152124 case 66: /* tcons ::= CHECK LP expr RP onconf */
152125{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy102);}
152126 break;
152127 case 67: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclause_opt */
152128{
152129 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy94, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy94, yymsp[-1].minor.yy100);
152130 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy100);
152131}
152132 break;
152133 case 69: /* onconf ::= */
152134 case 71: /* orconf ::= */ yytestcase(yyruleno==71);
152135{yymsp[1].minor.yy100 = OE_Default11;}
152136 break;
152137 case 70: /* onconf ::= ON CONFLICT resolvetype */
152138{yymsp[-2].minor.yy100 = yymsp[0].minor.yy100;}
152139 break;
152140 case 73: /* resolvetype ::= IGNORE */
152141{yymsp[0].minor.yy100 = OE_Ignore4;}
152142 break;
152143 case 74: /* resolvetype ::= REPLACE */
152144 case 158: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==158);
152145{yymsp[0].minor.yy100 = OE_Replace5;}
152146 break;
152147 case 75: /* cmd ::= DROP TABLE ifexists fullname */
152148{
152149 sqlite3DropTable(pParse, yymsp[0].minor.yy407, 0, yymsp[-1].minor.yy100);
152150}
152151 break;
152152 case 78: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
152153{
152154 sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy94, yymsp[0].minor.yy391, yymsp[-7].minor.yy100, yymsp[-5].minor.yy100);
152155}
152156 break;
152157 case 79: /* cmd ::= DROP VIEW ifexists fullname */
152158{
152159 sqlite3DropTable(pParse, yymsp[0].minor.yy407, 1, yymsp[-1].minor.yy100);
152160}
152161 break;
152162 case 80: /* cmd ::= select */
152163{
152164 SelectDest dest = {SRT_Output9, 0, 0, 0, 0, 0};
152165 sqlite3Select(pParse, yymsp[0].minor.yy391, &dest);
152166 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy391);
152167}
152168 break;
152169 case 81: /* select ::= WITH wqlist selectnowith */
152170{
152171 Select *p = yymsp[0].minor.yy391;
152172 if( p ){
152173 p->pWith = yymsp[-1].minor.yy243;
152174 parserDoubleLinkSelect(pParse, p);
152175 }else{
152176 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy243);
152177 }
152178 yymsp[-2].minor.yy391 = p;
152179}
152180 break;
152181 case 82: /* select ::= WITH RECURSIVE wqlist selectnowith */
152182{
152183 Select *p = yymsp[0].minor.yy391;
152184 if( p ){
152185 p->pWith = yymsp[-1].minor.yy243;
152186 parserDoubleLinkSelect(pParse, p);
152187 }else{
152188 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy243);
152189 }
152190 yymsp[-3].minor.yy391 = p;
152191}
152192 break;
152193 case 83: /* select ::= selectnowith */
152194{
152195 Select *p = yymsp[0].minor.yy391;
152196 if( p ){
152197 parserDoubleLinkSelect(pParse, p);
152198 }
152199 yymsp[0].minor.yy391 = p; /*A-overwrites-X*/
152200}
152201 break;
152202 case 84: /* selectnowith ::= selectnowith multiselect_op oneselect */
152203{
152204 Select *pRhs = yymsp[0].minor.yy391;
152205 Select *pLhs = yymsp[-2].minor.yy391;
152206 if( pRhs && pRhs->pPrior ){
152207 SrcList *pFrom;
152208 Token x;
152209 x.n = 0;
152210 parserDoubleLinkSelect(pParse, pRhs);
152211 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
152212 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
152213 }
152214 if( pRhs ){
152215 pRhs->op = (u8)yymsp[-1].minor.yy100;
152216 pRhs->pPrior = pLhs;
152217 if( ALWAYS(pLhs)(pLhs) ) pLhs->selFlags &= ~SF_MultiValue0x00400;
152218 pRhs->selFlags &= ~SF_MultiValue0x00400;
152219 if( yymsp[-1].minor.yy100!=TK_ALL128 ) pParse->hasCompound = 1;
152220 }else{
152221 sqlite3SelectDelete(pParse->db, pLhs);
152222 }
152223 yymsp[-2].minor.yy391 = pRhs;
152224}
152225 break;
152226 case 85: /* multiselect_op ::= UNION */
152227 case 87: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==87);
152228{yymsp[0].minor.yy100 = yymsp[0].major; /*A-overwrites-OP*/}
152229 break;
152230 case 86: /* multiselect_op ::= UNION ALL */
152231{yymsp[-1].minor.yy100 = TK_ALL128;}
152232 break;
152233 case 88: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
152234{
152235 yymsp[-8].minor.yy391 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy94,yymsp[-5].minor.yy407,yymsp[-4].minor.yy102,yymsp[-3].minor.yy94,yymsp[-2].minor.yy102,yymsp[-1].minor.yy94,yymsp[-7].minor.yy100,yymsp[0].minor.yy102);
152236}
152237 break;
152238 case 89: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt window_clause orderby_opt limit_opt */
152239{
152240 yymsp[-9].minor.yy391 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy94,yymsp[-6].minor.yy407,yymsp[-5].minor.yy102,yymsp[-4].minor.yy94,yymsp[-3].minor.yy102,yymsp[-1].minor.yy94,yymsp[-8].minor.yy100,yymsp[0].minor.yy102);
152241 if( yymsp[-9].minor.yy391 ){
152242 yymsp[-9].minor.yy391->pWinDefn = yymsp[-2].minor.yy379;
152243 }else{
152244 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy379);
152245 }
152246}
152247 break;
152248 case 90: /* values ::= VALUES LP nexprlist RP */
152249{
152250 yymsp[-3].minor.yy391 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy94,0,0,0,0,0,SF_Values0x00200,0);
152251}
152252 break;
152253 case 91: /* values ::= values COMMA LP nexprlist RP */
152254{
152255 Select *pRight, *pLeft = yymsp[-4].minor.yy391;
152256 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy94,0,0,0,0,0,SF_Values0x00200|SF_MultiValue0x00400,0);
152257 if( ALWAYS(pLeft)(pLeft) ) pLeft->selFlags &= ~SF_MultiValue0x00400;
152258 if( pRight ){
152259 pRight->op = TK_ALL128;
152260 pRight->pPrior = pLeft;
152261 yymsp[-4].minor.yy391 = pRight;
152262 }else{
152263 yymsp[-4].minor.yy391 = pLeft;
152264 }
152265}
152266 break;
152267 case 92: /* distinct ::= DISTINCT */
152268{yymsp[0].minor.yy100 = SF_Distinct0x00001;}
152269 break;
152270 case 93: /* distinct ::= ALL */
152271{yymsp[0].minor.yy100 = SF_All0x00002;}
152272 break;
152273 case 95: /* sclp ::= */
152274 case 128: /* orderby_opt ::= */ yytestcase(yyruleno==128);
152275 case 135: /* groupby_opt ::= */ yytestcase(yyruleno==135);
152276 case 215: /* exprlist ::= */ yytestcase(yyruleno==215);
152277 case 218: /* paren_exprlist ::= */ yytestcase(yyruleno==218);
152278 case 223: /* eidlist_opt ::= */ yytestcase(yyruleno==223);
152279{yymsp[1].minor.yy94 = 0;}
152280 break;
152281 case 96: /* selcollist ::= sclp scanpt expr scanpt as */
152282{
152283 yymsp[-4].minor.yy94 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy94, yymsp[-2].minor.yy102);
152284 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy94, &yymsp[0].minor.yy0, 1);
152285 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy94,yymsp[-3].minor.yy528,yymsp[-1].minor.yy528);
152286}
152287 break;
152288 case 97: /* selcollist ::= sclp scanpt STAR */
152289{
152290 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK172, 0);
152291 yymsp[-2].minor.yy94 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy94, p);
152292}
152293 break;
152294 case 98: /* selcollist ::= sclp scanpt nm DOT STAR */
152295{
152296 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK172, 0, 0);
152297 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID59, &yymsp[-2].minor.yy0, 1);
152298 Expr *pDot = sqlite3PExpr(pParse, TK_DOT134, pLeft, pRight);
152299 yymsp[-4].minor.yy94 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy94, pDot);
152300}
152301 break;
152302 case 99: /* as ::= AS nm */
152303 case 110: /* dbnm ::= DOT nm */ yytestcase(yyruleno==110);
152304 case 239: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==239);
152305 case 240: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==240);
152306{yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
152307 break;
152308 case 101: /* from ::= */
152309{yymsp[1].minor.yy407 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.yy407));}
152310 break;
152311 case 102: /* from ::= FROM seltablist */
152312{
152313 yymsp[-1].minor.yy407 = yymsp[0].minor.yy407;
152314 sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy407);
152315}
152316 break;
152317 case 103: /* stl_prefix ::= seltablist joinop */
152318{
152319 if( ALWAYS(yymsp[-1].minor.yy407 && yymsp[-1].minor.yy407->nSrc>0)(yymsp[-1].minor.yy407 && yymsp[-1].minor.yy407->nSrc
>0)
) yymsp[-1].minor.yy407->a[yymsp[-1].minor.yy407->nSrc-1].fg.jointype = (u8)yymsp[0].minor.yy100;
152320}
152321 break;
152322 case 104: /* stl_prefix ::= */
152323{yymsp[1].minor.yy407 = 0;}
152324 break;
152325 case 105: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
152326{
152327 yymsp[-6].minor.yy407 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy407,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy102,yymsp[0].minor.yy76);
152328 sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy407, &yymsp[-2].minor.yy0);
152329}
152330 break;
152331 case 106: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
152332{
152333 yymsp[-8].minor.yy407 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy407,&yymsp[-7].minor.yy0,&yymsp[-6].minor.yy0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy102,yymsp[0].minor.yy76);
152334 sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy407, yymsp[-4].minor.yy94);
152335}
152336 break;
152337 case 107: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
152338{
152339 yymsp[-6].minor.yy407 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy407,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy391,yymsp[-1].minor.yy102,yymsp[0].minor.yy76);
152340 }
152341 break;
152342 case 108: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
152343{
152344 if( yymsp[-6].minor.yy407==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy102==0 && yymsp[0].minor.yy76==0 ){
152345 yymsp[-6].minor.yy407 = yymsp[-4].minor.yy407;
152346 }else if( yymsp[-4].minor.yy407->nSrc==1 ){
152347 yymsp[-6].minor.yy407 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy407,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy102,yymsp[0].minor.yy76);
152348 if( yymsp[-6].minor.yy407 ){
152349 struct SrcList_item *pNew = &yymsp[-6].minor.yy407->a[yymsp[-6].minor.yy407->nSrc-1];
152350 struct SrcList_item *pOld = yymsp[-4].minor.yy407->a;
152351 pNew->zName = pOld->zName;
152352 pNew->zDatabase = pOld->zDatabase;
152353 pNew->pSelect = pOld->pSelect;
152354 if( pOld->fg.isTabFunc ){
152355 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
152356 pOld->u1.pFuncArg = 0;
152357 pOld->fg.isTabFunc = 0;
152358 pNew->fg.isTabFunc = 1;
152359 }
152360 pOld->zName = pOld->zDatabase = 0;
152361 pOld->pSelect = 0;
152362 }
152363 sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy407);
152364 }else{
152365 Select *pSubquery;
152366 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy407);
152367 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy407,0,0,0,0,SF_NestedFrom0x00800,0);
152368 yymsp[-6].minor.yy407 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy407,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy102,yymsp[0].minor.yy76);
152369 }
152370 }
152371 break;
152372 case 109: /* dbnm ::= */
152373 case 123: /* indexed_opt ::= */ yytestcase(yyruleno==123);
152374{yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
152375 break;
152376 case 111: /* fullname ::= nm */
152377{
152378 yylhsminor.yy407 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0);
152379 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && yylhsminor.yy407 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy407->a[0].zName, &yymsp[0].minor.yy0);
152380}
152381 yymsp[0].minor.yy407 = yylhsminor.yy407;
152382 break;
152383 case 112: /* fullname ::= nm DOT nm */
152384{
152385 yylhsminor.yy407 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
152386 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && yylhsminor.yy407 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy407->a[0].zName, &yymsp[0].minor.yy0);
152387}
152388 yymsp[-2].minor.yy407 = yylhsminor.yy407;
152389 break;
152390 case 113: /* xfullname ::= nm */
152391{yymsp[0].minor.yy407 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
152392 break;
152393 case 114: /* xfullname ::= nm DOT nm */
152394{yymsp[-2].minor.yy407 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-overwrites-X*/}
152395 break;
152396 case 115: /* xfullname ::= nm DOT nm AS nm */
152397{
152398 yymsp[-4].minor.yy407 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); /*A-overwrites-X*/
152399 if( yymsp[-4].minor.yy407 ) yymsp[-4].minor.yy407->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
152400}
152401 break;
152402 case 116: /* xfullname ::= nm AS nm */
152403{
152404 yymsp[-2].minor.yy407 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
152405 if( yymsp[-2].minor.yy407 ) yymsp[-2].minor.yy407->a[0].zAlias = sqlite3NameFromToken(pParse->db, &yymsp[0].minor.yy0);
152406}
152407 break;
152408 case 117: /* joinop ::= COMMA|JOIN */
152409{ yymsp[0].minor.yy100 = JT_INNER0x0001; }
152410 break;
152411 case 118: /* joinop ::= JOIN_KW JOIN */
152412{yymsp[-1].minor.yy100 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
152413 break;
152414 case 119: /* joinop ::= JOIN_KW nm JOIN */
152415{yymsp[-2].minor.yy100 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-overwrites-A*/}
152416 break;
152417 case 120: /* joinop ::= JOIN_KW nm nm JOIN */
152418{yymsp[-3].minor.yy100 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/}
152419 break;
152420 case 121: /* on_opt ::= ON expr */
152421 case 138: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==138);
152422 case 145: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==145);
152423 case 211: /* case_else ::= ELSE expr */ yytestcase(yyruleno==211);
152424 case 232: /* vinto ::= INTO expr */ yytestcase(yyruleno==232);
152425{yymsp[-1].minor.yy102 = yymsp[0].minor.yy102;}
152426 break;
152427 case 122: /* on_opt ::= */
152428 case 137: /* having_opt ::= */ yytestcase(yyruleno==137);
152429 case 139: /* limit_opt ::= */ yytestcase(yyruleno==139);
152430 case 144: /* where_opt ::= */ yytestcase(yyruleno==144);
152431 case 212: /* case_else ::= */ yytestcase(yyruleno==212);
152432 case 214: /* case_operand ::= */ yytestcase(yyruleno==214);
152433 case 233: /* vinto ::= */ yytestcase(yyruleno==233);
152434{yymsp[1].minor.yy102 = 0;}
152435 break;
152436 case 124: /* indexed_opt ::= INDEXED BY nm */
152437{yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
152438 break;
152439 case 125: /* indexed_opt ::= NOT INDEXED */
152440{yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
152441 break;
152442 case 126: /* using_opt ::= USING LP idlist RP */
152443{yymsp[-3].minor.yy76 = yymsp[-1].minor.yy76;}
152444 break;
152445 case 127: /* using_opt ::= */
152446 case 159: /* idlist_opt ::= */ yytestcase(yyruleno==159);
152447{yymsp[1].minor.yy76 = 0;}
152448 break;
152449 case 129: /* orderby_opt ::= ORDER BY sortlist */
152450 case 136: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==136);
152451{yymsp[-2].minor.yy94 = yymsp[0].minor.yy94;}
152452 break;
152453 case 130: /* sortlist ::= sortlist COMMA expr sortorder */
152454{
152455 yymsp[-3].minor.yy94 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy94,yymsp[-1].minor.yy102);
152456 sqlite3ExprListSetSortOrder(yymsp[-3].minor.yy94,yymsp[0].minor.yy100);
152457}
152458 break;
152459 case 131: /* sortlist ::= expr sortorder */
152460{
152461 yymsp[-1].minor.yy94 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy102); /*A-overwrites-Y*/
152462 sqlite3ExprListSetSortOrder(yymsp[-1].minor.yy94,yymsp[0].minor.yy100);
152463}
152464 break;
152465 case 132: /* sortorder ::= ASC */
152466{yymsp[0].minor.yy100 = SQLITE_SO_ASC0;}
152467 break;
152468 case 133: /* sortorder ::= DESC */
152469{yymsp[0].minor.yy100 = SQLITE_SO_DESC1;}
152470 break;
152471 case 134: /* sortorder ::= */
152472{yymsp[1].minor.yy100 = SQLITE_SO_UNDEFINED-1;}
152473 break;
152474 case 140: /* limit_opt ::= LIMIT expr */
152475{yymsp[-1].minor.yy102 = sqlite3PExpr(pParse,TK_LIMIT141,yymsp[0].minor.yy102,0);}
152476 break;
152477 case 141: /* limit_opt ::= LIMIT expr OFFSET expr */
152478{yymsp[-3].minor.yy102 = sqlite3PExpr(pParse,TK_LIMIT141,yymsp[-2].minor.yy102,yymsp[0].minor.yy102);}
152479 break;
152480 case 142: /* limit_opt ::= LIMIT expr COMMA expr */
152481{yymsp[-3].minor.yy102 = sqlite3PExpr(pParse,TK_LIMIT141,yymsp[0].minor.yy102,yymsp[-2].minor.yy102);}
152482 break;
152483 case 143: /* cmd ::= with DELETE FROM xfullname indexed_opt where_opt */
152484{
152485 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy407, &yymsp[-1].minor.yy0);
152486 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy407,yymsp[0].minor.yy102,0,0);
152487}
152488 break;
152489 case 146: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist where_opt */
152490{
152491 sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy407, &yymsp[-3].minor.yy0);
152492 sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy94,"set list");
152493 sqlite3Update(pParse,yymsp[-4].minor.yy407,yymsp[-1].minor.yy94,yymsp[0].minor.yy102,yymsp[-5].minor.yy100,0,0,0);
152494}
152495 break;
152496 case 147: /* setlist ::= setlist COMMA nm EQ expr */
152497{
152498 yymsp[-4].minor.yy94 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy94, yymsp[0].minor.yy102);
152499 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy94, &yymsp[-2].minor.yy0, 1);
152500}
152501 break;
152502 case 148: /* setlist ::= setlist COMMA LP idlist RP EQ expr */
152503{
152504 yymsp[-6].minor.yy94 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy94, yymsp[-3].minor.yy76, yymsp[0].minor.yy102);
152505}
152506 break;
152507 case 149: /* setlist ::= nm EQ expr */
152508{
152509 yylhsminor.yy94 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy102);
152510 sqlite3ExprListSetName(pParse, yylhsminor.yy94, &yymsp[-2].minor.yy0, 1);
152511}
152512 yymsp[-2].minor.yy94 = yylhsminor.yy94;
152513 break;
152514 case 150: /* setlist ::= LP idlist RP EQ expr */
152515{
152516 yymsp[-4].minor.yy94 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy76, yymsp[0].minor.yy102);
152517}
152518 break;
152519 case 151: /* cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
152520{
152521 sqlite3Insert(pParse, yymsp[-3].minor.yy407, yymsp[-1].minor.yy391, yymsp[-2].minor.yy76, yymsp[-5].minor.yy100, yymsp[0].minor.yy95);
152522}
152523 break;
152524 case 152: /* cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES */
152525{
152526 sqlite3Insert(pParse, yymsp[-3].minor.yy407, 0, yymsp[-2].minor.yy76, yymsp[-5].minor.yy100, 0);
152527}
152528 break;
152529 case 153: /* upsert ::= */
152530{ yymsp[1].minor.yy95 = 0; }
152531 break;
152532 case 154: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt */
152533{ yymsp[-10].minor.yy95 = sqlite3UpsertNew(pParse->db,yymsp[-7].minor.yy94,yymsp[-5].minor.yy102,yymsp[-1].minor.yy94,yymsp[0].minor.yy102);}
152534 break;
152535 case 155: /* upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING */
152536{ yymsp[-7].minor.yy95 = sqlite3UpsertNew(pParse->db,yymsp[-4].minor.yy94,yymsp[-2].minor.yy102,0,0); }
152537 break;
152538 case 156: /* upsert ::= ON CONFLICT DO NOTHING */
152539{ yymsp[-3].minor.yy95 = sqlite3UpsertNew(pParse->db,0,0,0,0); }
152540 break;
152541 case 160: /* idlist_opt ::= LP idlist RP */
152542{yymsp[-2].minor.yy76 = yymsp[-1].minor.yy76;}
152543 break;
152544 case 161: /* idlist ::= idlist COMMA nm */
152545{yymsp[-2].minor.yy76 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy76,&yymsp[0].minor.yy0);}
152546 break;
152547 case 162: /* idlist ::= nm */
152548{yymsp[0].minor.yy76 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
152549 break;
152550 case 163: /* expr ::= LP expr RP */
152551{yymsp[-2].minor.yy102 = yymsp[-1].minor.yy102;}
152552 break;
152553 case 164: /* expr ::= ID|INDEXED */
152554 case 165: /* expr ::= JOIN_KW */ yytestcase(yyruleno==165);
152555{yymsp[0].minor.yy102=tokenExpr(pParse,TK_ID59,yymsp[0].minor.yy0); /*A-overwrites-X*/}
152556 break;
152557 case 166: /* expr ::= nm DOT nm */
152558{
152559 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID59, &yymsp[-2].minor.yy0, 1);
152560 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID59, &yymsp[0].minor.yy0, 1);
152561 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
152562 sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[0].minor.yy0);
152563 sqlite3RenameTokenMap(pParse, (void*)temp1, &yymsp[-2].minor.yy0);
152564 }
152565 yylhsminor.yy102 = sqlite3PExpr(pParse, TK_DOT134, temp1, temp2);
152566}
152567 yymsp[-2].minor.yy102 = yylhsminor.yy102;
152568 break;
152569 case 167: /* expr ::= nm DOT nm DOT nm */
152570{
152571 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID59, &yymsp[-4].minor.yy0, 1);
152572 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID59, &yymsp[-2].minor.yy0, 1);
152573 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID59, &yymsp[0].minor.yy0, 1);
152574 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT134, temp2, temp3);
152575 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
152576 sqlite3RenameTokenMap(pParse, (void*)temp3, &yymsp[0].minor.yy0);
152577 sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[-2].minor.yy0);
152578 }
152579 yylhsminor.yy102 = sqlite3PExpr(pParse, TK_DOT134, temp1, temp4);
152580}
152581 yymsp[-4].minor.yy102 = yylhsminor.yy102;
152582 break;
152583 case 168: /* term ::= NULL|FLOAT|BLOB */
152584 case 169: /* term ::= STRING */ yytestcase(yyruleno==169);
152585{yymsp[0].minor.yy102=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
152586 break;
152587 case 170: /* term ::= INTEGER */
152588{
152589 yylhsminor.yy102 = sqlite3ExprAlloc(pParse->db, TK_INTEGER147, &yymsp[0].minor.yy0, 1);
152590}
152591 yymsp[0].minor.yy102 = yylhsminor.yy102;
152592 break;
152593 case 171: /* expr ::= VARIABLE */
152594{
152595 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1])(sqlite3CtypeMap[(unsigned char)(yymsp[0].minor.yy0.z[1])]&
0x04)
) ){
152596 u32 n = yymsp[0].minor.yy0.n;
152597 yymsp[0].minor.yy102 = tokenExpr(pParse, TK_VARIABLE148, yymsp[0].minor.yy0);
152598 sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy102, n);
152599 }else{
152600 /* When doing a nested parse, one can include terms in an expression
152601 ** that look like this: #1 #2 ... These terms refer to registers
152602 ** in the virtual machine. #N is the N-th register. */
152603 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
152604 assert( t.n>=2 )((void) (0));
152605 if( pParse->nested==0 ){
152606 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
152607 yymsp[0].minor.yy102 = 0;
152608 }else{
152609 yymsp[0].minor.yy102 = sqlite3PExpr(pParse, TK_REGISTER168, 0, 0);
152610 if( yymsp[0].minor.yy102 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy102->iTable);
152611 }
152612 }
152613}
152614 break;
152615 case 172: /* expr ::= expr COLLATE ID|STRING */
152616{
152617 yymsp[-2].minor.yy102 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy102, &yymsp[0].minor.yy0, 1);
152618}
152619 break;
152620 case 173: /* expr ::= CAST LP expr AS typetoken RP */
152621{
152622 yymsp[-5].minor.yy102 = sqlite3ExprAlloc(pParse->db, TK_CAST36, &yymsp[-1].minor.yy0, 1);
152623 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy102, yymsp[-3].minor.yy102, 0);
152624}
152625 break;
152626 case 174: /* expr ::= ID|INDEXED LP distinct exprlist RP */
152627{
152628 yylhsminor.yy102 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy94, &yymsp[-4].minor.yy0, yymsp[-2].minor.yy100);
152629}
152630 yymsp[-4].minor.yy102 = yylhsminor.yy102;
152631 break;
152632 case 175: /* expr ::= ID|INDEXED LP STAR RP */
152633{
152634 yylhsminor.yy102 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
152635}
152636 yymsp[-3].minor.yy102 = yylhsminor.yy102;
152637 break;
152638 case 176: /* expr ::= ID|INDEXED LP distinct exprlist RP over_clause */
152639{
152640 yylhsminor.yy102 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy94, &yymsp[-5].minor.yy0, yymsp[-3].minor.yy100);
152641 sqlite3WindowAttach(pParse, yylhsminor.yy102, yymsp[0].minor.yy379);
152642}
152643 yymsp[-5].minor.yy102 = yylhsminor.yy102;
152644 break;
152645 case 177: /* expr ::= ID|INDEXED LP STAR RP over_clause */
152646{
152647 yylhsminor.yy102 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
152648 sqlite3WindowAttach(pParse, yylhsminor.yy102, yymsp[0].minor.yy379);
152649}
152650 yymsp[-4].minor.yy102 = yylhsminor.yy102;
152651 break;
152652 case 178: /* term ::= CTIME_KW */
152653{
152654 yylhsminor.yy102 = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0, 0);
152655}
152656 yymsp[0].minor.yy102 = yylhsminor.yy102;
152657 break;
152658 case 179: /* expr ::= LP nexprlist COMMA expr RP */
152659{
152660 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy94, yymsp[-1].minor.yy102);
152661 yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_VECTOR169, 0, 0);
152662 if( yymsp[-4].minor.yy102 ){
152663 yymsp[-4].minor.yy102->x.pList = pList;
152664 }else{
152665 sqlite3ExprListDelete(pParse->db, pList);
152666 }
152667}
152668 break;
152669 case 180: /* expr ::= expr AND expr */
152670{yymsp[-2].minor.yy102=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy102,yymsp[0].minor.yy102);}
152671 break;
152672 case 181: /* expr ::= expr OR expr */
152673 case 182: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==182);
152674 case 183: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==183);
152675 case 184: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==184);
152676 case 185: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==185);
152677 case 186: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==186);
152678 case 187: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==187);
152679{yymsp[-2].minor.yy102=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy102,yymsp[0].minor.yy102);}
152680 break;
152681 case 188: /* likeop ::= NOT LIKE_KW|MATCH */
152682{yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/}
152683 break;
152684 case 189: /* expr ::= expr likeop expr */
152685{
152686 ExprList *pList;
152687 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
152688 yymsp[-1].minor.yy0.n &= 0x7fffffff;
152689 pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy102);
152690 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy102);
152691 yymsp[-2].minor.yy102 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
152692 if( bNot ) yymsp[-2].minor.yy102 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-2].minor.yy102, 0);
152693 if( yymsp[-2].minor.yy102 ) yymsp[-2].minor.yy102->flags |= EP_InfixFunc0x000080;
152694}
152695 break;
152696 case 190: /* expr ::= expr likeop expr ESCAPE expr */
152697{
152698 ExprList *pList;
152699 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
152700 yymsp[-3].minor.yy0.n &= 0x7fffffff;
152701 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy102);
152702 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy102);
152703 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy102);
152704 yymsp[-4].minor.yy102 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
152705 if( bNot ) yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy102, 0);
152706 if( yymsp[-4].minor.yy102 ) yymsp[-4].minor.yy102->flags |= EP_InfixFunc0x000080;
152707}
152708 break;
152709 case 191: /* expr ::= expr ISNULL|NOTNULL */
152710{yymsp[-1].minor.yy102 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy102,0);}
152711 break;
152712 case 192: /* expr ::= expr NOT NULL */
152713{yymsp[-2].minor.yy102 = sqlite3PExpr(pParse,TK_NOTNULL51,yymsp[-2].minor.yy102,0);}
152714 break;
152715 case 193: /* expr ::= expr IS expr */
152716{
152717 yymsp[-2].minor.yy102 = sqlite3PExpr(pParse,TK_IS45,yymsp[-2].minor.yy102,yymsp[0].minor.yy102);
152718 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy102, yymsp[-2].minor.yy102, TK_ISNULL50);
152719}
152720 break;
152721 case 194: /* expr ::= expr IS NOT expr */
152722{
152723 yymsp[-3].minor.yy102 = sqlite3PExpr(pParse,TK_ISNOT160,yymsp[-3].minor.yy102,yymsp[0].minor.yy102);
152724 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy102, yymsp[-3].minor.yy102, TK_NOTNULL51);
152725}
152726 break;
152727 case 195: /* expr ::= NOT expr */
152728 case 196: /* expr ::= BITNOT expr */ yytestcase(yyruleno==196);
152729{yymsp[-1].minor.yy102 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy102, 0);/*A-overwrites-B*/}
152730 break;
152731 case 197: /* expr ::= PLUS|MINUS expr */
152732{
152733 yymsp[-1].minor.yy102 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS100 ? TK_UPLUS166 : TK_UMINUS165, yymsp[0].minor.yy102, 0);
152734 /*A-overwrites-B*/
152735}
152736 break;
152737 case 198: /* between_op ::= BETWEEN */
152738 case 201: /* in_op ::= IN */ yytestcase(yyruleno==201);
152739{yymsp[0].minor.yy100 = 0;}
152740 break;
152741 case 200: /* expr ::= expr between_op expr AND expr */
152742{
152743 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy102);
152744 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy102);
152745 yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_BETWEEN48, yymsp[-4].minor.yy102, 0);
152746 if( yymsp[-4].minor.yy102 ){
152747 yymsp[-4].minor.yy102->x.pList = pList;
152748 }else{
152749 sqlite3ExprListDelete(pParse->db, pList);
152750 }
152751 if( yymsp[-3].minor.yy100 ) yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy102, 0);
152752}
152753 break;
152754 case 203: /* expr ::= expr in_op LP exprlist RP */
152755{
152756 if( yymsp[-1].minor.yy94==0 ){
152757 /* Expressions of the form
152758 **
152759 ** expr1 IN ()
152760 ** expr1 NOT IN ()
152761 **
152762 ** simplify to constants 0 (false) and 1 (true), respectively,
152763 ** regardless of the value of expr1.
152764 */
152765 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy102);
152766 yymsp[-4].minor.yy102 = sqlite3ExprAlloc(pParse->db, TK_INTEGER147,&sqlite3IntTokens[yymsp[-3].minor.yy100],1);
152767 }else if( yymsp[-1].minor.yy94->nExpr==1 ){
152768 /* Expressions of the form:
152769 **
152770 ** expr1 IN (?1)
152771 ** expr1 NOT IN (?2)
152772 **
152773 ** with exactly one value on the RHS can be simplified to something
152774 ** like this:
152775 **
152776 ** expr1 == ?1
152777 ** expr1 <> ?2
152778 **
152779 ** But, the RHS of the == or <> is marked with the EP_Generic flag
152780 ** so that it may not contribute to the computation of comparison
152781 ** affinity or the collating sequence to use for comparison. Otherwise,
152782 ** the semantics would be subtly different from IN or NOT IN.
152783 */
152784 Expr *pRHS = yymsp[-1].minor.yy94->a[0].pExpr;
152785 yymsp[-1].minor.yy94->a[0].pExpr = 0;
152786 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy94);
152787 /* pRHS cannot be NULL because a malloc error would have been detected
152788 ** before now and control would have never reached this point */
152789 if( ALWAYS(pRHS)(pRHS) ){
152790 pRHS->flags &= ~EP_Collate0x000100;
152791 pRHS->flags |= EP_Generic0x000200;
152792 }
152793 yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, yymsp[-3].minor.yy100 ? TK_NE52 : TK_EQ53, yymsp[-4].minor.yy102, pRHS);
152794 }else{
152795 yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_IN49, yymsp[-4].minor.yy102, 0);
152796 if( yymsp[-4].minor.yy102 ){
152797 yymsp[-4].minor.yy102->x.pList = yymsp[-1].minor.yy94;
152798 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy102);
152799 }else{
152800 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy94);
152801 }
152802 if( yymsp[-3].minor.yy100 ) yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy102, 0);
152803 }
152804 }
152805 break;
152806 case 204: /* expr ::= LP select RP */
152807{
152808 yymsp[-2].minor.yy102 = sqlite3PExpr(pParse, TK_SELECT131, 0, 0);
152809 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy102, yymsp[-1].minor.yy391);
152810 }
152811 break;
152812 case 205: /* expr ::= expr in_op LP select RP */
152813{
152814 yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_IN49, yymsp[-4].minor.yy102, 0);
152815 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy102, yymsp[-1].minor.yy391);
152816 if( yymsp[-3].minor.yy100 ) yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy102, 0);
152817 }
152818 break;
152819 case 206: /* expr ::= expr in_op nm dbnm paren_exprlist */
152820{
152821 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
152822 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
152823 if( yymsp[0].minor.yy94 ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy94);
152824 yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_IN49, yymsp[-4].minor.yy102, 0);
152825 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy102, pSelect);
152826 if( yymsp[-3].minor.yy100 ) yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_NOT19, yymsp[-4].minor.yy102, 0);
152827 }
152828 break;
152829 case 207: /* expr ::= EXISTS LP select RP */
152830{
152831 Expr *p;
152832 p = yymsp[-3].minor.yy102 = sqlite3PExpr(pParse, TK_EXISTS20, 0, 0);
152833 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy391);
152834 }
152835 break;
152836 case 208: /* expr ::= CASE case_operand case_exprlist case_else END */
152837{
152838 yymsp[-4].minor.yy102 = sqlite3PExpr(pParse, TK_CASE149, yymsp[-3].minor.yy102, 0);
152839 if( yymsp[-4].minor.yy102 ){
152840 yymsp[-4].minor.yy102->x.pList = yymsp[-1].minor.yy102 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy94,yymsp[-1].minor.yy102) : yymsp[-2].minor.yy94;
152841 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy102);
152842 }else{
152843 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy94);
152844 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy102);
152845 }
152846}
152847 break;
152848 case 209: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
152849{
152850 yymsp[-4].minor.yy94 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy94, yymsp[-2].minor.yy102);
152851 yymsp[-4].minor.yy94 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy94, yymsp[0].minor.yy102);
152852}
152853 break;
152854 case 210: /* case_exprlist ::= WHEN expr THEN expr */
152855{
152856 yymsp[-3].minor.yy94 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy102);
152857 yymsp[-3].minor.yy94 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy94, yymsp[0].minor.yy102);
152858}
152859 break;
152860 case 213: /* case_operand ::= expr */
152861{yymsp[0].minor.yy102 = yymsp[0].minor.yy102; /*A-overwrites-X*/}
152862 break;
152863 case 216: /* nexprlist ::= nexprlist COMMA expr */
152864{yymsp[-2].minor.yy94 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy94,yymsp[0].minor.yy102);}
152865 break;
152866 case 217: /* nexprlist ::= expr */
152867{yymsp[0].minor.yy94 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy102); /*A-overwrites-Y*/}
152868 break;
152869 case 219: /* paren_exprlist ::= LP exprlist RP */
152870 case 224: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==224);
152871{yymsp[-2].minor.yy94 = yymsp[-1].minor.yy94;}
152872 break;
152873 case 220: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_opt */
152874{
152875 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
152876 sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy94, yymsp[-10].minor.yy100,
152877 &yymsp[-11].minor.yy0, yymsp[0].minor.yy102, SQLITE_SO_ASC0, yymsp[-8].minor.yy100, SQLITE_IDXTYPE_APPDEF0);
152878 if( IN_RENAME_OBJECT(pParse->eParseMode>=2) && pParse->pNewIndex ){
152879 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
152880 }
152881}
152882 break;
152883 case 221: /* uniqueflag ::= UNIQUE */
152884 case 263: /* raisetype ::= ABORT */ yytestcase(yyruleno==263);
152885{yymsp[0].minor.yy100 = OE_Abort2;}
152886 break;
152887 case 222: /* uniqueflag ::= */
152888{yymsp[1].minor.yy100 = OE_None0;}
152889 break;
152890 case 225: /* eidlist ::= eidlist COMMA nm collate sortorder */
152891{
152892 yymsp[-4].minor.yy94 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy94, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy100, yymsp[0].minor.yy100);
152893}
152894 break;
152895 case 226: /* eidlist ::= nm collate sortorder */
152896{
152897 yymsp[-2].minor.yy94 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy100, yymsp[0].minor.yy100); /*A-overwrites-Y*/
152898}
152899 break;
152900 case 229: /* cmd ::= DROP INDEX ifexists fullname */
152901{sqlite3DropIndex(pParse, yymsp[0].minor.yy407, yymsp[-1].minor.yy100);}
152902 break;
152903 case 230: /* cmd ::= VACUUM vinto */
152904{sqlite3Vacuum(pParse,0,yymsp[0].minor.yy102);}
152905 break;
152906 case 231: /* cmd ::= VACUUM nm vinto */
152907{sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy102);}
152908 break;
152909 case 234: /* cmd ::= PRAGMA nm dbnm */
152910{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
152911 break;
152912 case 235: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
152913{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
152914 break;
152915 case 236: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
152916{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
152917 break;
152918 case 237: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
152919{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
152920 break;
152921 case 238: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
152922{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
152923 break;
152924 case 241: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
152925{
152926 Token all;
152927 all.z = yymsp[-3].minor.yy0.z;
152928 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
152929 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy11, &all);
152930}
152931 break;
152932 case 242: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
152933{
152934 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy100, yymsp[-4].minor.yy298.a, yymsp[-4].minor.yy298.b, yymsp[-2].minor.yy407, yymsp[0].minor.yy102, yymsp[-10].minor.yy100, yymsp[-8].minor.yy100);
152935 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-overwrites-T*/
152936}
152937 break;
152938 case 243: /* trigger_time ::= BEFORE|AFTER */
152939{ yymsp[0].minor.yy100 = yymsp[0].major; /*A-overwrites-X*/ }
152940 break;
152941 case 244: /* trigger_time ::= INSTEAD OF */
152942{ yymsp[-1].minor.yy100 = TK_INSTEAD65;}
152943 break;
152944 case 245: /* trigger_time ::= */
152945{ yymsp[1].minor.yy100 = TK_BEFORE33; }
152946 break;
152947 case 246: /* trigger_event ::= DELETE|INSERT */
152948 case 247: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==247);
152949{yymsp[0].minor.yy298.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy298.b = 0;}
152950 break;
152951 case 248: /* trigger_event ::= UPDATE OF idlist */
152952{yymsp[-2].minor.yy298.a = TK_UPDATE122; yymsp[-2].minor.yy298.b = yymsp[0].minor.yy76;}
152953 break;
152954 case 249: /* when_clause ::= */
152955 case 268: /* key_opt ::= */ yytestcase(yyruleno==268);
152956 case 316: /* filter_opt ::= */ yytestcase(yyruleno==316);
152957{ yymsp[1].minor.yy102 = 0; }
152958 break;
152959 case 250: /* when_clause ::= WHEN expr */
152960 case 269: /* key_opt ::= KEY expr */ yytestcase(yyruleno==269);
152961{ yymsp[-1].minor.yy102 = yymsp[0].minor.yy102; }
152962 break;
152963 case 251: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
152964{
152965 assert( yymsp[-2].minor.yy11!=0 )((void) (0));
152966 yymsp[-2].minor.yy11->pLast->pNext = yymsp[-1].minor.yy11;
152967 yymsp[-2].minor.yy11->pLast = yymsp[-1].minor.yy11;
152968}
152969 break;
152970 case 252: /* trigger_cmd_list ::= trigger_cmd SEMI */
152971{
152972 assert( yymsp[-1].minor.yy11!=0 )((void) (0));
152973 yymsp[-1].minor.yy11->pLast = yymsp[-1].minor.yy11;
152974}
152975 break;
152976 case 253: /* trnm ::= nm DOT nm */
152977{
152978 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
152979 sqlite3ErrorMsg(pParse,
152980 "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
152981 "statements within triggers");
152982}
152983 break;
152984 case 254: /* tridxby ::= INDEXED BY nm */
152985{
152986 sqlite3ErrorMsg(pParse,
152987 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
152988 "within triggers");
152989}
152990 break;
152991 case 255: /* tridxby ::= NOT INDEXED */
152992{
152993 sqlite3ErrorMsg(pParse,
152994 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
152995 "within triggers");
152996}
152997 break;
152998 case 256: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt scanpt */
152999{yylhsminor.yy11 = sqlite3TriggerUpdateStep(pParse, &yymsp[-5].minor.yy0, yymsp[-2].minor.yy94, yymsp[-1].minor.yy102, yymsp[-6].minor.yy100, yymsp[-7].minor.yy0.z, yymsp[0].minor.yy528);}
153000 yymsp[-7].minor.yy11 = yylhsminor.yy11;
153001 break;
153002 case 257: /* trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
153003{
153004 yylhsminor.yy11 = sqlite3TriggerInsertStep(pParse,&yymsp[-4].minor.yy0,yymsp[-3].minor.yy76,yymsp[-2].minor.yy391,yymsp[-6].minor.yy100,yymsp[-1].minor.yy95,yymsp[-7].minor.yy528,yymsp[0].minor.yy528);/*yylhsminor.yy11-overwrites-yymsp[-6].minor.yy100*/
153005}
153006 yymsp[-7].minor.yy11 = yylhsminor.yy11;
153007 break;
153008 case 258: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
153009{yylhsminor.yy11 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy102, yymsp[-5].minor.yy0.z, yymsp[0].minor.yy528);}
153010 yymsp[-5].minor.yy11 = yylhsminor.yy11;
153011 break;
153012 case 259: /* trigger_cmd ::= scanpt select scanpt */
153013{yylhsminor.yy11 = sqlite3TriggerSelectStep(pParse->db, yymsp[-1].minor.yy391, yymsp[-2].minor.yy528, yymsp[0].minor.yy528); /*yylhsminor.yy11-overwrites-yymsp[-1].minor.yy391*/}
153014 yymsp[-2].minor.yy11 = yylhsminor.yy11;
153015 break;
153016 case 260: /* expr ::= RAISE LP IGNORE RP */
153017{
153018 yymsp[-3].minor.yy102 = sqlite3PExpr(pParse, TK_RAISE71, 0, 0);
153019 if( yymsp[-3].minor.yy102 ){
153020 yymsp[-3].minor.yy102->affinity = OE_Ignore4;
153021 }
153022}
153023 break;
153024 case 261: /* expr ::= RAISE LP raisetype COMMA nm RP */
153025{
153026 yymsp[-5].minor.yy102 = sqlite3ExprAlloc(pParse->db, TK_RAISE71, &yymsp[-1].minor.yy0, 1);
153027 if( yymsp[-5].minor.yy102 ) {
153028 yymsp[-5].minor.yy102->affinity = (char)yymsp[-3].minor.yy100;
153029 }
153030}
153031 break;
153032 case 262: /* raisetype ::= ROLLBACK */
153033{yymsp[0].minor.yy100 = OE_Rollback1;}
153034 break;
153035 case 264: /* raisetype ::= FAIL */
153036{yymsp[0].minor.yy100 = OE_Fail3;}
153037 break;
153038 case 265: /* cmd ::= DROP TRIGGER ifexists fullname */
153039{
153040 sqlite3DropTrigger(pParse,yymsp[0].minor.yy407,yymsp[-1].minor.yy100);
153041}
153042 break;
153043 case 266: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
153044{
153045 sqlite3Attach(pParse, yymsp[-3].minor.yy102, yymsp[-1].minor.yy102, yymsp[0].minor.yy102);
153046}
153047 break;
153048 case 267: /* cmd ::= DETACH database_kw_opt expr */
153049{
153050 sqlite3Detach(pParse, yymsp[0].minor.yy102);
153051}
153052 break;
153053 case 270: /* cmd ::= REINDEX */
153054{sqlite3Reindex(pParse, 0, 0);}
153055 break;
153056 case 271: /* cmd ::= REINDEX nm dbnm */
153057{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
153058 break;
153059 case 272: /* cmd ::= ANALYZE */
153060{sqlite3Analyze(pParse, 0, 0);}
153061 break;
153062 case 273: /* cmd ::= ANALYZE nm dbnm */
153063{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
153064 break;
153065 case 274: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
153066{
153067 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy407,&yymsp[0].minor.yy0);
153068}
153069 break;
153070 case 275: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
153071{
153072 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
153073 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
153074}
153075 break;
153076 case 276: /* add_column_fullname ::= fullname */
153077{
153078 disableLookaside(pParse);
153079 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy407);
153080}
153081 break;
153082 case 277: /* cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
153083{
153084 sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy407, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
153085}
153086 break;
153087 case 278: /* cmd ::= create_vtab */
153088{sqlite3VtabFinishParse(pParse,0);}
153089 break;
153090 case 279: /* cmd ::= create_vtab LP vtabarglist RP */
153091{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
153092 break;
153093 case 280: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
153094{
153095 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy100);
153096}
153097 break;
153098 case 281: /* vtabarg ::= */
153099{sqlite3VtabArgInit(pParse);}
153100 break;
153101 case 282: /* vtabargtoken ::= ANY */
153102 case 283: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==283);
153103 case 284: /* lp ::= LP */ yytestcase(yyruleno==284);
153104{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
153105 break;
153106 case 285: /* with ::= WITH wqlist */
153107 case 286: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==286);
153108{ sqlite3WithPush(pParse, yymsp[0].minor.yy243, 1); }
153109 break;
153110 case 287: /* wqlist ::= nm eidlist_opt AS LP select RP */
153111{
153112 yymsp[-5].minor.yy243 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy94, yymsp[-1].minor.yy391); /*A-overwrites-X*/
153113}
153114 break;
153115 case 288: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */
153116{
153117 yymsp[-7].minor.yy243 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy243, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy94, yymsp[-1].minor.yy391);
153118}
153119 break;
153120 case 289: /* windowdefn_list ::= windowdefn */
153121{ yylhsminor.yy379 = yymsp[0].minor.yy379; }
153122 yymsp[0].minor.yy379 = yylhsminor.yy379;
153123 break;
153124 case 290: /* windowdefn_list ::= windowdefn_list COMMA windowdefn */
153125{
153126 assert( yymsp[0].minor.yy379!=0 )((void) (0));
153127 sqlite3WindowChain(pParse, yymsp[0].minor.yy379, yymsp[-2].minor.yy379);
153128 yymsp[0].minor.yy379->pNextWin = yymsp[-2].minor.yy379;
153129 yylhsminor.yy379 = yymsp[0].minor.yy379;
153130}
153131 yymsp[-2].minor.yy379 = yylhsminor.yy379;
153132 break;
153133 case 291: /* windowdefn ::= nm AS LP window RP */
153134{
153135 if( ALWAYS(yymsp[-1].minor.yy379)(yymsp[-1].minor.yy379) ){
153136 yymsp[-1].minor.yy379->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.yy0.n);
153137 }
153138 yylhsminor.yy379 = yymsp[-1].minor.yy379;
153139}
153140 yymsp[-4].minor.yy379 = yylhsminor.yy379;
153141 break;
153142 case 292: /* window ::= PARTITION BY nexprlist orderby_opt frame_opt */
153143{
153144 yymsp[-4].minor.yy379 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy379, yymsp[-2].minor.yy94, yymsp[-1].minor.yy94, 0);
153145}
153146 break;
153147 case 293: /* window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
153148{
153149 yylhsminor.yy379 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy379, yymsp[-2].minor.yy94, yymsp[-1].minor.yy94, &yymsp[-5].minor.yy0);
153150}
153151 yymsp[-5].minor.yy379 = yylhsminor.yy379;
153152 break;
153153 case 294: /* window ::= ORDER BY sortlist frame_opt */
153154{
153155 yymsp[-3].minor.yy379 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy379, 0, yymsp[-1].minor.yy94, 0);
153156}
153157 break;
153158 case 295: /* window ::= nm ORDER BY sortlist frame_opt */
153159{
153160 yylhsminor.yy379 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy379, 0, yymsp[-1].minor.yy94, &yymsp[-4].minor.yy0);
153161}
153162 yymsp[-4].minor.yy379 = yylhsminor.yy379;
153163 break;
153164 case 296: /* window ::= frame_opt */
153165{
153166 yylhsminor.yy379 = yymsp[0].minor.yy379;
153167}
153168 yymsp[0].minor.yy379 = yylhsminor.yy379;
153169 break;
153170 case 297: /* window ::= nm frame_opt */
153171{
153172 yylhsminor.yy379 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy379, 0, 0, &yymsp[-1].minor.yy0);
153173}
153174 yymsp[-1].minor.yy379 = yylhsminor.yy379;
153175 break;
153176 case 298: /* frame_opt ::= */
153177{
153178 yymsp[1].minor.yy379 = sqlite3WindowAlloc(pParse, 0, TK_UNBOUNDED87, 0, TK_CURRENT82, 0, 0);
153179}
153180 break;
153181 case 299: /* frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
153182{
153183 yylhsminor.yy379 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy100, yymsp[-1].minor.yy389.eType, yymsp[-1].minor.yy389.pExpr, TK_CURRENT82, 0, yymsp[0].minor.yy218);
153184}
153185 yymsp[-2].minor.yy379 = yylhsminor.yy379;
153186 break;
153187 case 300: /* frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_opt */
153188{
153189 yylhsminor.yy379 = sqlite3WindowAlloc(pParse, yymsp[-5].minor.yy100, yymsp[-3].minor.yy389.eType, yymsp[-3].minor.yy389.pExpr, yymsp[-1].minor.yy389.eType, yymsp[-1].minor.yy389.pExpr, yymsp[0].minor.yy218);
153190}
153191 yymsp[-5].minor.yy379 = yylhsminor.yy379;
153192 break;
153193 case 302: /* frame_bound_s ::= frame_bound */
153194 case 304: /* frame_bound_e ::= frame_bound */ yytestcase(yyruleno==304);
153195{yylhsminor.yy389 = yymsp[0].minor.yy389;}
153196 yymsp[0].minor.yy389 = yylhsminor.yy389;
153197 break;
153198 case 303: /* frame_bound_s ::= UNBOUNDED PRECEDING */
153199 case 305: /* frame_bound_e ::= UNBOUNDED FOLLOWING */ yytestcase(yyruleno==305);
153200 case 307: /* frame_bound ::= CURRENT ROW */ yytestcase(yyruleno==307);
153201{yylhsminor.yy389.eType = yymsp[-1].major; yylhsminor.yy389.pExpr = 0;}
153202 yymsp[-1].minor.yy389 = yylhsminor.yy389;
153203 break;
153204 case 306: /* frame_bound ::= expr PRECEDING|FOLLOWING */
153205{yylhsminor.yy389.eType = yymsp[0].major; yylhsminor.yy389.pExpr = yymsp[-1].minor.yy102;}
153206 yymsp[-1].minor.yy389 = yylhsminor.yy389;
153207 break;
153208 case 308: /* frame_exclude_opt ::= */
153209{yymsp[1].minor.yy218 = 0;}
153210 break;
153211 case 309: /* frame_exclude_opt ::= EXCLUDE frame_exclude */
153212{yymsp[-1].minor.yy218 = yymsp[0].minor.yy218;}
153213 break;
153214 case 310: /* frame_exclude ::= NO OTHERS */
153215 case 311: /* frame_exclude ::= CURRENT ROW */ yytestcase(yyruleno==311);
153216{yymsp[-1].minor.yy218 = yymsp[-1].major; /*A-overwrites-X*/}
153217 break;
153218 case 312: /* frame_exclude ::= GROUP|TIES */
153219{yymsp[0].minor.yy218 = yymsp[0].major; /*A-overwrites-X*/}
153220 break;
153221 case 313: /* window_clause ::= WINDOW windowdefn_list */
153222{ yymsp[-1].minor.yy379 = yymsp[0].minor.yy379; }
153223 break;
153224 case 314: /* over_clause ::= filter_opt OVER LP window RP */
153225{
153226 yylhsminor.yy379 = yymsp[-1].minor.yy379;
153227 assert( yylhsminor.yy379!=0 )((void) (0));
153228 yylhsminor.yy379->pFilter = yymsp[-4].minor.yy102;
153229}
153230 yymsp[-4].minor.yy379 = yylhsminor.yy379;
153231 break;
153232 case 315: /* over_clause ::= filter_opt OVER nm */
153233{
153234 yylhsminor.yy379 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
153235 if( yylhsminor.yy379 ){
153236 yylhsminor.yy379->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy0.n);
153237 yylhsminor.yy379->pFilter = yymsp[-2].minor.yy102;
153238 }else{
153239 sqlite3ExprDelete(pParse->db, yymsp[-2].minor.yy102);
153240 }
153241}
153242 yymsp[-2].minor.yy379 = yylhsminor.yy379;
153243 break;
153244 case 317: /* filter_opt ::= FILTER LP WHERE expr RP */
153245{ yymsp[-4].minor.yy102 = yymsp[-1].minor.yy102; }
153246 break;
153247 default:
153248 /* (318) input ::= cmdlist */ yytestcase(yyruleno==318);
153249 /* (319) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==319);
153250 /* (320) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=320)((void) (0));
153251 /* (321) ecmd ::= SEMI */ yytestcase(yyruleno==321);
153252 /* (322) ecmd ::= cmdx SEMI */ yytestcase(yyruleno==322);
153253 /* (323) ecmd ::= explain cmdx */ yytestcase(yyruleno==323);
153254 /* (324) trans_opt ::= */ yytestcase(yyruleno==324);
153255 /* (325) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==325);
153256 /* (326) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==326);
153257 /* (327) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==327);
153258 /* (328) savepoint_opt ::= */ yytestcase(yyruleno==328);
153259 /* (329) cmd ::= create_table create_table_args */ yytestcase(yyruleno==329);
153260 /* (330) columnlist ::= columnlist COMMA columnname carglist */ yytestcase(yyruleno==330);
153261 /* (331) columnlist ::= columnname carglist */ yytestcase(yyruleno==331);
153262 /* (332) nm ::= ID|INDEXED */ yytestcase(yyruleno==332);
153263 /* (333) nm ::= STRING */ yytestcase(yyruleno==333);
153264 /* (334) nm ::= JOIN_KW */ yytestcase(yyruleno==334);
153265 /* (335) typetoken ::= typename */ yytestcase(yyruleno==335);
153266 /* (336) typename ::= ID|STRING */ yytestcase(yyruleno==336);
153267 /* (337) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=337)((void) (0));
153268 /* (338) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=338)((void) (0));
153269 /* (339) carglist ::= carglist ccons */ yytestcase(yyruleno==339);
153270 /* (340) carglist ::= */ yytestcase(yyruleno==340);
153271 /* (341) ccons ::= NULL onconf */ yytestcase(yyruleno==341);
153272 /* (342) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==342);
153273 /* (343) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==343);
153274 /* (344) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=344)((void) (0));
153275 /* (345) tconscomma ::= */ yytestcase(yyruleno==345);
153276 /* (346) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert(yyruleno!=346)((void) (0));
153277 /* (347) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=347)((void) (0));
153278 /* (348) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=348)((void) (0));
153279 /* (349) oneselect ::= values */ yytestcase(yyruleno==349);
153280 /* (350) sclp ::= selcollist COMMA */ yytestcase(yyruleno==350);
153281 /* (351) as ::= ID|STRING */ yytestcase(yyruleno==351);
153282 /* (352) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=352)((void) (0));
153283 /* (353) likeop ::= LIKE_KW|MATCH */ yytestcase(yyruleno==353);
153284 /* (354) exprlist ::= nexprlist */ yytestcase(yyruleno==354);
153285 /* (355) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=355)((void) (0));
153286 /* (356) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=356)((void) (0));
153287 /* (357) nmnum ::= ON */ yytestcase(yyruleno==357);
153288 /* (358) nmnum ::= DELETE */ yytestcase(yyruleno==358);
153289 /* (359) nmnum ::= DEFAULT */ yytestcase(yyruleno==359);
153290 /* (360) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==360);
153291 /* (361) foreach_clause ::= */ yytestcase(yyruleno==361);
153292 /* (362) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==362);
153293 /* (363) trnm ::= nm */ yytestcase(yyruleno==363);
153294 /* (364) tridxby ::= */ yytestcase(yyruleno==364);
153295 /* (365) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==365);
153296 /* (366) database_kw_opt ::= */ yytestcase(yyruleno==366);
153297 /* (367) kwcolumn_opt ::= */ yytestcase(yyruleno==367);
153298 /* (368) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==368);
153299 /* (369) vtabarglist ::= vtabarg */ yytestcase(yyruleno==369);
153300 /* (370) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==370);
153301 /* (371) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==371);
153302 /* (372) anylist ::= */ yytestcase(yyruleno==372);
153303 /* (373) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==373);
153304 /* (374) anylist ::= anylist ANY */ yytestcase(yyruleno==374);
153305 /* (375) with ::= */ yytestcase(yyruleno==375);
153306 break;
153307/********** End reduce actions ************************************************/
153308 };
153309 assert( yyruleno<sizeof(yyRuleInfoLhs)/sizeof(yyRuleInfoLhs[0]) )((void) (0));
153310 yygoto = yyRuleInfoLhs[yyruleno];
153311 yysize = yyRuleInfoNRhs[yyruleno];
153312 yyact = yy_find_reduce_action(yymsp[yysize].stateno,(YYCODETYPEunsigned short int)yygoto);
153313
153314 /* There are no SHIFTREDUCE actions on nonterminals because the table
153315 ** generator has simplified them to pure REDUCE actions. */
153316 assert( !(yyact>YY_MAX_SHIFT && yyact<=YY_MAX_SHIFTREDUCE) )((void) (0));
153317
153318 /* It is not possible for a REDUCE to be followed by an error */
153319 assert( yyact!=YY_ERROR_ACTION )((void) (0));
153320
153321 yymsp += yysize+1;
153322 yypParser->yytos = yymsp;
153323 yymsp->stateno = (YYACTIONTYPEunsigned short int)yyact;
153324 yymsp->major = (YYCODETYPEunsigned short int)yygoto;
153325 yyTraceShift(yypParser, yyact, "... then shift");
153326 return yyact;
153327}
153328
153329/*
153330** The following code executes when the parse fails
153331*/
153332#ifndef YYNOERRORRECOVERY1
153333static void yy_parse_failed(
153334 yyParser *yypParser /* The parser */
153335){
153336 sqlite3ParserARG_FETCH
153337 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
153338#ifndef NDEBUG1
153339 if( yyTraceFILE ){
153340 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
153341 }
153342#endif
153343 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
153344 /* Here code is inserted which will be executed whenever the
153345 ** parser fails */
153346/************ Begin %parse_failure code ***************************************/
153347/************ End %parse_failure code *****************************************/
153348 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
153349 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
153350}
153351#endif /* YYNOERRORRECOVERY */
153352
153353/*
153354** The following code executes when a syntax error first occurs.
153355*/
153356static void yy_syntax_error(
153357 yyParser *yypParser, /* The parser */
153358 int yymajor, /* The major type of the error token */
153359 sqlite3ParserTOKENTYPEToken yyminor /* The minor type of the error token */
153360){
153361 sqlite3ParserARG_FETCH
153362 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
153363#define TOKENyyminor yyminor
153364/************ Begin %syntax_error code ****************************************/
153365
153366 UNUSED_PARAMETER(yymajor)(void)(yymajor); /* Silence some compiler warnings */
153367 if( TOKENyyminor.z[0] ){
153368 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKENyyminor);
153369 }else{
153370 sqlite3ErrorMsg(pParse, "incomplete input");
153371 }
153372/************ End %syntax_error code ******************************************/
153373 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
153374 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
153375}
153376
153377/*
153378** The following is executed when the parser accepts
153379*/
153380static void yy_accept(
153381 yyParser *yypParser /* The parser */
153382){
153383 sqlite3ParserARG_FETCH
153384 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
153385#ifndef NDEBUG1
153386 if( yyTraceFILE ){
153387 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
153388 }
153389#endif
153390#ifndef YYNOERRORRECOVERY1
153391 yypParser->yyerrcnt = -1;
153392#endif
153393 assert( yypParser->yytos==yypParser->yystack )((void) (0));
153394 /* Here code is inserted which will be executed whenever the
153395 ** parser accepts */
153396/*********** Begin %parse_accept code *****************************************/
153397/*********** End %parse_accept code *******************************************/
153398 sqlite3ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
153399 sqlite3ParserCTX_STOREyypParser->pParse=pParse;
153400}
153401
153402/* The main parser program.
153403** The first argument is a pointer to a structure obtained from
153404** "sqlite3ParserAlloc" which describes the current state of the parser.
153405** The second argument is the major token number. The third is
153406** the minor token. The fourth optional argument is whatever the
153407** user wants (and specified in the grammar) and is available for
153408** use by the action routines.
153409**
153410** Inputs:
153411** <ul>
153412** <li> A pointer to the parser (an opaque structure.)
153413** <li> The major token number.
153414** <li> The minor token number.
153415** <li> An option argument of a grammar-specified type.
153416** </ul>
153417**
153418** Outputs:
153419** None.
153420*/
153421SQLITE_PRIVATEstatic void sqlite3Parser(
153422 void *yyp, /* The parser */
153423 int yymajor, /* The major token code number */
153424 sqlite3ParserTOKENTYPEToken yyminor /* The value for the token */
153425 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
153426){
153427 YYMINORTYPE yyminorunion;
153428 YYACTIONTYPEunsigned short int yyact; /* The parser action. */
153429#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY1)
153430 int yyendofinput; /* True if we are at the end of input */
153431#endif
153432#ifdef YYERRORSYMBOL
153433 int yyerrorhit = 0; /* True if yymajor has invoked an error */
153434#endif
153435 yyParser *yypParser = (yyParser*)yyp; /* The parser */
153436 sqlite3ParserCTX_FETCHParse *pParse=yypParser->pParse;
153437 sqlite3ParserARG_STORE
153438
153439 assert( yypParser->yytos!=0 )((void) (0));
153440#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY1)
153441 yyendofinput = (yymajor==0);
153442#endif
153443
153444 yyact = yypParser->yytos->stateno;
153445#ifndef NDEBUG1
153446 if( yyTraceFILE ){
153447 if( yyact < YY_MIN_REDUCE1162 ){
153448 fprintf(yyTraceFILE,"%sInput '%s' in state %d\n",
153449 yyTracePrompt,yyTokenName[yymajor],yyact);
153450 }else{
153451 fprintf(yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
153452 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE1162);
153453 }
153454 }
153455#endif
153456
153457 do{
153458 assert( yyact==yypParser->yytos->stateno )((void) (0));
153459 yyact = yy_find_shift_action((YYCODETYPEunsigned short int)yymajor,yyact);
153460 if( yyact >= YY_MIN_REDUCE1162 ){
153461 yyact = yy_reduce(yypParser,yyact-YY_MIN_REDUCE1162,yymajor,
153462 yyminor sqlite3ParserCTX_PARAM,pParse);
153463 }else if( yyact <= YY_MAX_SHIFTREDUCE1158 ){
153464 yy_shift(yypParser,yyact,(YYCODETYPEunsigned short int)yymajor,yyminor);
153465#ifndef YYNOERRORRECOVERY1
153466 yypParser->yyerrcnt--;
153467#endif
153468 break;
153469 }else if( yyact==YY_ACCEPT_ACTION1160 ){
153470 yypParser->yytos--;
153471 yy_accept(yypParser);
153472 return;
153473 }else{
153474 assert( yyact == YY_ERROR_ACTION )((void) (0));
153475 yyminorunion.yy0 = yyminor;
153476#ifdef YYERRORSYMBOL
153477 int yymx;
153478#endif
153479#ifndef NDEBUG1
153480 if( yyTraceFILE ){
153481 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
153482 }
153483#endif
153484#ifdef YYERRORSYMBOL
153485 /* A syntax error has occurred.
153486 ** The response to an error depends upon whether or not the
153487 ** grammar defines an error token "ERROR".
153488 **
153489 ** This is what we do if the grammar does define ERROR:
153490 **
153491 ** * Call the %syntax_error function.
153492 **
153493 ** * Begin popping the stack until we enter a state where
153494 ** it is legal to shift the error symbol, then shift
153495 ** the error symbol.
153496 **
153497 ** * Set the error count to three.
153498 **
153499 ** * Begin accepting and shifting new tokens. No new error
153500 ** processing will occur until three tokens have been
153501 ** shifted successfully.
153502 **
153503 */
153504 if( yypParser->yyerrcnt<0 ){
153505 yy_syntax_error(yypParser,yymajor,yyminor);
153506 }
153507 yymx = yypParser->yytos->major;
153508 if( yymx==YYERRORSYMBOL || yyerrorhit ){
153509#ifndef NDEBUG1
153510 if( yyTraceFILE ){
153511 fprintf(yyTraceFILE,"%sDiscard input token %s\n",
153512 yyTracePrompt,yyTokenName[yymajor]);
153513 }
153514#endif
153515 yy_destructor(yypParser, (YYCODETYPEunsigned short int)yymajor, &yyminorunion);
153516 yymajor = YYNOCODE302;
153517 }else{
153518 while( yypParser->yytos >= yypParser->yystack
153519 && (yyact = yy_find_reduce_action(
153520 yypParser->yytos->stateno,
153521 YYERRORSYMBOL)) > YY_MAX_SHIFTREDUCE1158
153522 ){
153523 yy_pop_parser_stack(yypParser);
153524 }
153525 if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
153526 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
153527 yy_parse_failed(yypParser);
153528#ifndef YYNOERRORRECOVERY1
153529 yypParser->yyerrcnt = -1;
153530#endif
153531 yymajor = YYNOCODE302;
153532 }else if( yymx!=YYERRORSYMBOL ){
153533 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor);
153534 }
153535 }
153536 yypParser->yyerrcnt = 3;
153537 yyerrorhit = 1;
153538 if( yymajor==YYNOCODE302 ) break;
153539 yyact = yypParser->yytos->stateno;
153540#elif defined(YYNOERRORRECOVERY1)
153541 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
153542 ** do any kind of error recovery. Instead, simply invoke the syntax
153543 ** error routine and continue going as if nothing had happened.
153544 **
153545 ** Applications can set this macro (for example inside %include) if
153546 ** they intend to abandon the parse upon the first syntax error seen.
153547 */
153548 yy_syntax_error(yypParser,yymajor, yyminor);
153549 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
153550 break;
153551#else /* YYERRORSYMBOL is not defined */
153552 /* This is what we do if the grammar does not define ERROR:
153553 **
153554 ** * Report an error message, and throw away the input token.
153555 **
153556 ** * If the input token is $, then fail the parse.
153557 **
153558 ** As before, subsequent error messages are suppressed until
153559 ** three input tokens have been successfully shifted.
153560 */
153561 if( yypParser->yyerrcnt<=0 ){
153562 yy_syntax_error(yypParser,yymajor, yyminor);
153563 }
153564 yypParser->yyerrcnt = 3;
153565 yy_destructor(yypParser,(YYCODETYPEunsigned short int)yymajor,&yyminorunion);
153566 if( yyendofinput ){
153567 yy_parse_failed(yypParser);
153568#ifndef YYNOERRORRECOVERY1
153569 yypParser->yyerrcnt = -1;
153570#endif
153571 }
153572 break;
153573#endif
153574 }
153575 }while( yypParser->yytos>yypParser->yystack );
153576#ifndef NDEBUG1
153577 if( yyTraceFILE ){
153578 yyStackEntry *i;
153579 char cDiv = '[';
153580 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt);
153581 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
153582 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
153583 cDiv = ' ';
153584 }
153585 fprintf(yyTraceFILE,"]\n");
153586 }
153587#endif
153588 return;
153589}
153590
153591/*
153592** Return the fallback token corresponding to canonical token iToken, or
153593** 0 if iToken has no fallback.
153594*/
153595SQLITE_PRIVATEstatic int sqlite3ParserFallback(int iToken){
153596#ifdef YYFALLBACK1
153597 if( iToken<(int)(sizeof(yyFallback)/sizeof(yyFallback[0])) ){
153598 return yyFallback[iToken];
153599 }
153600#else
153601 (void)iToken;
153602#endif
153603 return 0;
153604}
153605
153606/************** End of parse.c ***********************************************/
153607/************** Begin file tokenize.c ****************************************/
153608/*
153609** 2001 September 15
153610**
153611** The author disclaims copyright to this source code. In place of
153612** a legal notice, here is a blessing:
153613**
153614** May you do good and not evil.
153615** May you find forgiveness for yourself and forgive others.
153616** May you share freely, never taking more than you give.
153617**
153618*************************************************************************
153619** An tokenizer for SQL
153620**
153621** This file contains C code that splits an SQL input string up into
153622** individual tokens and sends those tokens one-by-one over to the
153623** parser for analysis.
153624*/
153625/* #include "sqliteInt.h" */
153626/* #include <stdlib.h> */
153627
153628/* Character classes for tokenizing
153629**
153630** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
153631** using a lookup table, whereas a switch() directly on c uses a binary search.
153632** The lookup table is much faster. To maximize speed, and to ensure that
153633** a lookup table is used, all of the classes need to be small integers and
153634** all of them need to be used within the switch.
153635*/
153636#define CC_X0 0 /* The letter 'x', or start of BLOB literal */
153637#define CC_KYWD1 1 /* Alphabetics or '_'. Usable in a keyword */
153638#define CC_ID2 2 /* unicode characters usable in IDs */
153639#define CC_DIGIT3 3 /* Digits */
153640#define CC_DOLLAR4 4 /* '$' */
153641#define CC_VARALPHA5 5 /* '@', '#', ':'. Alphabetic SQL variables */
153642#define CC_VARNUM6 6 /* '?'. Numeric SQL variables */
153643#define CC_SPACE7 7 /* Space characters */
153644#define CC_QUOTE8 8 /* '"', '\'', or '`'. String literals, quoted ids */
153645#define CC_QUOTE29 9 /* '['. [...] style quoted ids */
153646#define CC_PIPE10 10 /* '|'. Bitwise OR or concatenate */
153647#define CC_MINUS11 11 /* '-'. Minus or SQL-style comment */
153648#define CC_LT12 12 /* '<'. Part of < or <= or <> */
153649#define CC_GT13 13 /* '>'. Part of > or >= */
153650#define CC_EQ14 14 /* '='. Part of = or == */
153651#define CC_BANG15 15 /* '!'. Part of != */
153652#define CC_SLASH16 16 /* '/'. / or c-style comment */
153653#define CC_LP17 17 /* '(' */
153654#define CC_RP18 18 /* ')' */
153655#define CC_SEMI19 19 /* ';' */
153656#define CC_PLUS20 20 /* '+' */
153657#define CC_STAR21 21 /* '*' */
153658#define CC_PERCENT22 22 /* '%' */
153659#define CC_COMMA23 23 /* ',' */
153660#define CC_AND24 24 /* '&' */
153661#define CC_TILDA25 25 /* '~' */
153662#define CC_DOT26 26 /* '.' */
153663#define CC_ILLEGAL27 27 /* Illegal character */
153664#define CC_NUL28 28 /* 0x00 */
153665
153666static const unsigned char aiClass[] = {
153667#ifdef SQLITE_ASCII1
153668/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
153669/* 0x */ 28, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
153670/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
153671/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
153672/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
153673/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
153674/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
153675/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
153676/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
153677/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
153678/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
153679/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
153680/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
153681/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
153682/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
153683/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
153684/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
153685#endif
153686#ifdef SQLITE_EBCDIC
153687/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
153688/* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27,
153689/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
153690/* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
153691/* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
153692/* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
153693/* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
153694/* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6,
153695/* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
153696/* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
153697/* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
153698/* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
153699/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27,
153700/* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
153701/* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
153702/* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
153703/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27,
153704#endif
153705};
153706
153707/*
153708** The charMap() macro maps alphabetic characters (only) into their
153709** lower-case ASCII equivalent. On ASCII machines, this is just
153710** an upper-to-lower case map. On EBCDIC machines we also need
153711** to adjust the encoding. The mapping is only valid for alphabetics
153712** which are the only characters for which this feature is used.
153713**
153714** Used by keywordhash.h
153715*/
153716#ifdef SQLITE_ASCII1
153717# define charMap(X)sqlite3UpperToLower[(unsigned char)X] sqlite3UpperToLower[(unsigned char)X]
153718#endif
153719#ifdef SQLITE_EBCDIC
153720# define charMap(X)sqlite3UpperToLower[(unsigned char)X] ebcdicToAscii[(unsigned char)X]
153721const unsigned char ebcdicToAscii[] = {
153722/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
153723 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
153724 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
153725 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
153726 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
153727 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
153728 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
153729 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
153730 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
153731 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
153732 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
153733 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
153734 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
153735 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
153736 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
153737 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
153738 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
153739};
153740#endif
153741
153742/*
153743** The sqlite3KeywordCode function looks up an identifier to determine if
153744** it is a keyword. If it is a keyword, the token code of that keyword is
153745** returned. If the input is not a keyword, TK_ID is returned.
153746**
153747** The implementation of this routine was generated by a program,
153748** mkkeywordhash.c, located in the tool subdirectory of the distribution.
153749** The output of the mkkeywordhash.c program is written into a file
153750** named keywordhash.h and then included into this source file by
153751** the #include below.
153752*/
153753/************** Include keywordhash.h in the middle of tokenize.c ************/
153754/************** Begin file keywordhash.h *************************************/
153755/***** This file contains automatically generated code ******
153756**
153757** The code in this file has been automatically generated by
153758**
153759** sqlite/tool/mkkeywordhash.c
153760**
153761** The code in this file implements a function that determines whether
153762** or not a given identifier is really an SQL keyword. The same thing
153763** might be implemented more directly using a hand-written hash table.
153764** But by using this automatically generated code, the size of the code
153765** is substantially reduced. This is important for embedded applications
153766** on platforms with limited memory.
153767*/
153768/* Hash score: 214 */
153769/* zKWText[] encodes 950 bytes of keyword text in 629 bytes */
153770/* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */
153771/* ABLEFTHENDEFERRABLELSEXCLUDELETEMPORARYCONSTRAINTERSECTIES */
153772/* AVEPOINTOFFSETRANSACTIONATURALTERAISEXCEPTRIGGEREFERENCES */
153773/* UNIQUERYWITHOUTERELEASEXCLUSIVEXISTSATTACHAVINGLOBEGINNERANGE */
153774/* BETWEENOTHINGROUPSCASCADETACHCASECOLLATECREATECURRENT_DATE */
153775/* IMMEDIATEJOINSERTLIKEMATCHPLANALYZEPRAGMABORTUPDATEVALUES */
153776/* VIRTUALIMITWHENOTNULLWHERECURSIVEAFTERENAMEANDEFAULT */
153777/* AUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSSCURRENT_TIMESTAMP */
153778/* ARTITIONDEFERREDISTINCTDROPRECEDINGFAILFILTEREPLACEFOLLOWING */
153779/* FROMFULLIFISNULLORDERESTRICTOTHERSOVERIGHTROLLBACKROWS */
153780/* UNBOUNDEDUNIONUSINGVACUUMVIEWINDOWBYINITIALLYPRIMARY */
153781static const char zKWText[628] = {
153782 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
153783 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
153784 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
153785 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
153786 'E','R','R','A','B','L','E','L','S','E','X','C','L','U','D','E','L','E',
153787 'T','E','M','P','O','R','A','R','Y','C','O','N','S','T','R','A','I','N',
153788 'T','E','R','S','E','C','T','I','E','S','A','V','E','P','O','I','N','T',
153789 'O','F','F','S','E','T','R','A','N','S','A','C','T','I','O','N','A','T',
153790 'U','R','A','L','T','E','R','A','I','S','E','X','C','E','P','T','R','I',
153791 'G','G','E','R','E','F','E','R','E','N','C','E','S','U','N','I','Q','U',
153792 'E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S','E',
153793 'X','C','L','U','S','I','V','E','X','I','S','T','S','A','T','T','A','C',
153794 'H','A','V','I','N','G','L','O','B','E','G','I','N','N','E','R','A','N',
153795 'G','E','B','E','T','W','E','E','N','O','T','H','I','N','G','R','O','U',
153796 'P','S','C','A','S','C','A','D','E','T','A','C','H','C','A','S','E','C',
153797 'O','L','L','A','T','E','C','R','E','A','T','E','C','U','R','R','E','N',
153798 'T','_','D','A','T','E','I','M','M','E','D','I','A','T','E','J','O','I',
153799 'N','S','E','R','T','L','I','K','E','M','A','T','C','H','P','L','A','N',
153800 'A','L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','U','P','D',
153801 'A','T','E','V','A','L','U','E','S','V','I','R','T','U','A','L','I','M',
153802 'I','T','W','H','E','N','O','T','N','U','L','L','W','H','E','R','E','C',
153803 'U','R','S','I','V','E','A','F','T','E','R','E','N','A','M','E','A','N',
153804 'D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E','M','E',
153805 'N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M','I','T',
153806 'C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R','R','E',
153807 'N','T','_','T','I','M','E','S','T','A','M','P','A','R','T','I','T','I',
153808 'O','N','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D',
153809 'R','O','P','R','E','C','E','D','I','N','G','F','A','I','L','F','I','L',
153810 'T','E','R','E','P','L','A','C','E','F','O','L','L','O','W','I','N','G',
153811 'F','R','O','M','F','U','L','L','I','F','I','S','N','U','L','L','O','R',
153812 'D','E','R','E','S','T','R','I','C','T','O','T','H','E','R','S','O','V',
153813 'E','R','I','G','H','T','R','O','L','L','B','A','C','K','R','O','W','S',
153814 'U','N','B','O','U','N','D','E','D','U','N','I','O','N','U','S','I','N',
153815 'G','V','A','C','U','U','M','V','I','E','W','I','N','D','O','W','B','Y',
153816 'I','N','I','T','I','A','L','L','Y','P','R','I','M','A','R','Y',
153817};
153818/* aKWHash[i] is the hash value for the i-th keyword */
153819static const unsigned char aKWHash[127] = {
153820 75, 111, 127, 73, 108, 29, 0, 0, 83, 0, 77, 63, 0,
153821 37, 33, 78, 15, 0, 126, 86, 57, 120, 128, 19, 0, 0,
153822 133, 0, 131, 123, 0, 22, 98, 0, 9, 0, 0, 117, 71,
153823 0, 69, 6, 0, 49, 95, 140, 0, 129, 106, 0, 0, 54,
153824 0, 109, 24, 0, 17, 0, 134, 56, 23, 26, 5, 58, 135,
153825 101, 0, 0, 139, 112, 62, 138, 59, 115, 65, 0, 96, 0,
153826 105, 45, 0, 104, 0, 0, 0, 100, 97, 102, 107, 119, 14,
153827 31, 118, 0, 81, 0, 136, 116, 137, 61, 124, 132, 80, 121,
153828 88, 30, 85, 0, 0, 99, 35, 125, 122, 0, 130, 0, 0,
153829 41, 0, 91, 89, 90, 0, 20, 87, 113, 82,
153830};
153831/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0
153832** then the i-th keyword has no more hash collisions. Otherwise,
153833** the next keyword with the same hash is aKWHash[i]-1. */
153834static const unsigned char aKWNext[140] = {
153835 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0,
153836 0, 2, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0,
153837 0, 0, 0, 21, 0, 0, 12, 0, 0, 0, 0, 0, 0,
153838 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
153839 51, 28, 0, 0, 38, 0, 0, 0, 44, 0, 0, 0, 3,
153840 0, 0, 67, 1, 66, 0, 0, 0, 36, 0, 47, 0, 0,
153841 0, 0, 0, 48, 50, 76, 0, 0, 42, 0, 60, 0, 0,
153842 0, 43, 0, 16, 55, 10, 0, 0, 0, 0, 0, 0, 0,
153843 11, 72, 93, 0, 0, 8, 0, 110, 0, 103, 40, 53, 70,
153844 0, 114, 0, 74, 52, 0, 0, 92, 39, 46, 0, 68, 32,
153845 84, 0, 34, 27, 25, 18, 94, 0, 64, 79,
153846};
153847/* aKWLen[i] is the length (in bytes) of the i-th keyword */
153848static const unsigned char aKWLen[140] = {
153849 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6,
153850 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 7,
153851 6, 9, 4, 2, 10, 9, 4, 9, 4, 6, 2, 3, 11,
153852 6, 2, 7, 5, 5, 6, 7, 10, 6, 5, 7, 4, 5,
153853 7, 9, 6, 6, 6, 4, 5, 5, 5, 7, 7, 6, 5,
153854 7, 3, 6, 4, 7, 6, 12, 9, 4, 6, 4, 5, 4,
153855 7, 6, 5, 6, 6, 7, 5, 4, 7, 3, 2, 4, 5,
153856 9, 5, 6, 3, 7, 13, 2, 2, 4, 6, 6, 8, 5,
153857 17, 12, 7, 9, 8, 8, 2, 4, 9, 4, 6, 7, 9,
153858 4, 4, 2, 6, 5, 8, 6, 4, 5, 8, 4, 3, 9,
153859 5, 5, 6, 4, 6, 2, 2, 9, 3, 7,
153860};
153861/* aKWOffset[i] is the index into zKWText[] of the start of
153862** the text for the i-th keyword. */
153863static const unsigned short int aKWOffset[140] = {
153864 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33,
153865 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81,
153866 86, 90, 90, 94, 99, 106, 114, 117, 123, 126, 126, 129, 131,
153867 136, 140, 141, 146, 150, 154, 159, 165, 175, 178, 183, 183, 187,
153868 191, 197, 205, 211, 216, 221, 224, 227, 231, 236, 242, 248, 248,
153869 254, 255, 259, 265, 269, 276, 282, 294, 303, 305, 311, 315, 320,
153870 322, 329, 334, 339, 345, 351, 357, 362, 365, 365, 365, 368, 372,
153871 375, 384, 388, 394, 396, 403, 405, 407, 416, 420, 426, 432, 440,
153872 445, 445, 445, 461, 470, 477, 478, 485, 488, 497, 501, 506, 513,
153873 522, 526, 530, 532, 538, 542, 550, 556, 559, 564, 572, 572, 576,
153874 585, 590, 595, 601, 604, 607, 610, 612, 617, 621,
153875};
153876/* aKWCode[i] is the parser symbol code for the i-th keyword */
153877static const unsigned char aKWCode[140] = {
153878 TK_REINDEX92, TK_INDEXED109, TK_INDEX153, TK_DESC39, TK_ESCAPE58,
153879 TK_EACH41, TK_CHECK117, TK_KEY67, TK_BEFORE33, TK_FOREIGN125,
153880 TK_FOR62, TK_IGNORE63, TK_LIKE_KW47, TK_EXPLAIN2, TK_INSTEAD65,
153881 TK_ADD155, TK_DATABASE38, TK_AS24, TK_SELECT131, TK_TABLE16,
153882 TK_JOIN_KW111, TK_THEN151, TK_END11, TK_DEFERRABLE124, TK_ELSE152,
153883 TK_EXCLUDE88, TK_DELETE121, TK_TEMP21, TK_TEMP21, TK_OR43,
153884 TK_CONSTRAINT112, TK_INTERSECT130, TK_TIES91, TK_SAVEPOINT13, TK_INTO143,
153885 TK_OFFSET69, TK_OF68, TK_SET123, TK_TRANSACTION6,TK_ACTION28,
153886 TK_ON108, TK_JOIN_KW111, TK_ALTER154, TK_RAISE71, TK_EXCEPT129,
153887 TK_TRIGGER77, TK_REFERENCES118, TK_UNIQUE116, TK_QUERY3, TK_WITHOUT25,
153888 TK_WITH81, TK_JOIN_KW111, TK_RELEASE14, TK_EXCLUSIVE9, TK_EXISTS20,
153889 TK_ATTACH32, TK_HAVING140, TK_LIKE_KW47, TK_BEGIN5, TK_JOIN_KW111,
153890 TK_RANGE86, TK_BETWEEN48, TK_NOTHING144, TK_GROUPS89, TK_GROUP139,
153891 TK_CASCADE35, TK_ASC31, TK_DETACH40, TK_CASE149, TK_COLLATE106,
153892 TK_CREATE17, TK_CTIME_KW94, TK_IMMEDIATE8, TK_JOIN136, TK_INSERT120,
153893 TK_LIKE_KW47, TK_MATCH46, TK_PLAN4, TK_ANALYZE30, TK_PRAGMA70,
153894 TK_ABORT27, TK_UPDATE122, TK_VALUES132, TK_VIRTUAL80, TK_LIMIT141,
153895 TK_WHEN150, TK_NOTNULL51, TK_NOT19, TK_NO66, TK_NULL114,
153896 TK_WHERE142, TK_RECURSIVE72, TK_AFTER29, TK_RENAME93, TK_AND44,
153897 TK_DEFAULT113, TK_AUTOINCR119, TK_TO15, TK_IN49, TK_CAST36,
153898 TK_COLUMNKW60, TK_COMMIT10, TK_CONFLICT37, TK_JOIN_KW111, TK_CTIME_KW94,
153899 TK_CTIME_KW94, TK_CURRENT82, TK_PARTITION84, TK_DEFERRED7, TK_DISTINCT133,
153900 TK_IS45, TK_DROP126, TK_PRECEDING85, TK_FAIL42, TK_FILTER158,
153901 TK_REPLACE73, TK_FOLLOWING83, TK_FROM135, TK_JOIN_KW111, TK_IF18,
153902 TK_ISNULL50, TK_ORDER138, TK_RESTRICT74, TK_OTHERS90, TK_OVER157,
153903 TK_JOIN_KW111, TK_ROLLBACK12, TK_ROWS76, TK_ROW75, TK_UNBOUNDED87,
153904 TK_UNION127, TK_USING137, TK_VACUUM78, TK_VIEW79, TK_WINDOW156,
153905 TK_DO61, TK_BY34, TK_INITIALLY64, TK_ALL128, TK_PRIMARY115,
153906};
153907/* Check to see if z[0..n-1] is a keyword. If it is, write the
153908** parser symbol code for that keyword into *pType. Always
153909** return the integer n (the length of the token). */
153910static int keywordCode(const char *z, int n, int *pType){
153911 int i, j;
153912 const char *zKW;
153913 if( n>=2 ){
153914 i = ((charMap(z[0])sqlite3UpperToLower[(unsigned char)z[0]]*4) ^ (charMap(z[n-1])sqlite3UpperToLower[(unsigned char)z[n-1]]*3) ^ n) % 127;
153915 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
153916 if( aKWLen[i]!=n ) continue;
153917 j = 0;
153918 zKW = &zKWText[aKWOffset[i]];
153919#ifdef SQLITE_ASCII1
153920 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
153921#endif
153922#ifdef SQLITE_EBCDIC
153923 while( j<n && toupper(z[j])==zKW[j] ){ j++; }
153924#endif
153925 if( j<n ) continue;
153926 testcase( i==0 ); /* REINDEX */
153927 testcase( i==1 ); /* INDEXED */
153928 testcase( i==2 ); /* INDEX */
153929 testcase( i==3 ); /* DESC */
153930 testcase( i==4 ); /* ESCAPE */
153931 testcase( i==5 ); /* EACH */
153932 testcase( i==6 ); /* CHECK */
153933 testcase( i==7 ); /* KEY */
153934 testcase( i==8 ); /* BEFORE */
153935 testcase( i==9 ); /* FOREIGN */
153936 testcase( i==10 ); /* FOR */
153937 testcase( i==11 ); /* IGNORE */
153938 testcase( i==12 ); /* REGEXP */
153939 testcase( i==13 ); /* EXPLAIN */
153940 testcase( i==14 ); /* INSTEAD */
153941 testcase( i==15 ); /* ADD */
153942 testcase( i==16 ); /* DATABASE */
153943 testcase( i==17 ); /* AS */
153944 testcase( i==18 ); /* SELECT */
153945 testcase( i==19 ); /* TABLE */
153946 testcase( i==20 ); /* LEFT */
153947 testcase( i==21 ); /* THEN */
153948 testcase( i==22 ); /* END */
153949 testcase( i==23 ); /* DEFERRABLE */
153950 testcase( i==24 ); /* ELSE */
153951 testcase( i==25 ); /* EXCLUDE */
153952 testcase( i==26 ); /* DELETE */
153953 testcase( i==27 ); /* TEMPORARY */
153954 testcase( i==28 ); /* TEMP */
153955 testcase( i==29 ); /* OR */
153956 testcase( i==30 ); /* CONSTRAINT */
153957 testcase( i==31 ); /* INTERSECT */
153958 testcase( i==32 ); /* TIES */
153959 testcase( i==33 ); /* SAVEPOINT */
153960 testcase( i==34 ); /* INTO */
153961 testcase( i==35 ); /* OFFSET */
153962 testcase( i==36 ); /* OF */
153963 testcase( i==37 ); /* SET */
153964 testcase( i==38 ); /* TRANSACTION */
153965 testcase( i==39 ); /* ACTION */
153966 testcase( i==40 ); /* ON */
153967 testcase( i==41 ); /* NATURAL */
153968 testcase( i==42 ); /* ALTER */
153969 testcase( i==43 ); /* RAISE */
153970 testcase( i==44 ); /* EXCEPT */
153971 testcase( i==45 ); /* TRIGGER */
153972 testcase( i==46 ); /* REFERENCES */
153973 testcase( i==47 ); /* UNIQUE */
153974 testcase( i==48 ); /* QUERY */
153975 testcase( i==49 ); /* WITHOUT */
153976 testcase( i==50 ); /* WITH */
153977 testcase( i==51 ); /* OUTER */
153978 testcase( i==52 ); /* RELEASE */
153979 testcase( i==53 ); /* EXCLUSIVE */
153980 testcase( i==54 ); /* EXISTS */
153981 testcase( i==55 ); /* ATTACH */
153982 testcase( i==56 ); /* HAVING */
153983 testcase( i==57 ); /* GLOB */
153984 testcase( i==58 ); /* BEGIN */
153985 testcase( i==59 ); /* INNER */
153986 testcase( i==60 ); /* RANGE */
153987 testcase( i==61 ); /* BETWEEN */
153988 testcase( i==62 ); /* NOTHING */
153989 testcase( i==63 ); /* GROUPS */
153990 testcase( i==64 ); /* GROUP */
153991 testcase( i==65 ); /* CASCADE */
153992 testcase( i==66 ); /* ASC */
153993 testcase( i==67 ); /* DETACH */
153994 testcase( i==68 ); /* CASE */
153995 testcase( i==69 ); /* COLLATE */
153996 testcase( i==70 ); /* CREATE */
153997 testcase( i==71 ); /* CURRENT_DATE */
153998 testcase( i==72 ); /* IMMEDIATE */
153999 testcase( i==73 ); /* JOIN */
154000 testcase( i==74 ); /* INSERT */
154001 testcase( i==75 ); /* LIKE */
154002 testcase( i==76 ); /* MATCH */
154003 testcase( i==77 ); /* PLAN */
154004 testcase( i==78 ); /* ANALYZE */
154005 testcase( i==79 ); /* PRAGMA */
154006 testcase( i==80 ); /* ABORT */
154007 testcase( i==81 ); /* UPDATE */
154008 testcase( i==82 ); /* VALUES */
154009 testcase( i==83 ); /* VIRTUAL */
154010 testcase( i==84 ); /* LIMIT */
154011 testcase( i==85 ); /* WHEN */
154012 testcase( i==86 ); /* NOTNULL */
154013 testcase( i==87 ); /* NOT */
154014 testcase( i==88 ); /* NO */
154015 testcase( i==89 ); /* NULL */
154016 testcase( i==90 ); /* WHERE */
154017 testcase( i==91 ); /* RECURSIVE */
154018 testcase( i==92 ); /* AFTER */
154019 testcase( i==93 ); /* RENAME */
154020 testcase( i==94 ); /* AND */
154021 testcase( i==95 ); /* DEFAULT */
154022 testcase( i==96 ); /* AUTOINCREMENT */
154023 testcase( i==97 ); /* TO */
154024 testcase( i==98 ); /* IN */
154025 testcase( i==99 ); /* CAST */
154026 testcase( i==100 ); /* COLUMN */
154027 testcase( i==101 ); /* COMMIT */
154028 testcase( i==102 ); /* CONFLICT */
154029 testcase( i==103 ); /* CROSS */
154030 testcase( i==104 ); /* CURRENT_TIMESTAMP */
154031 testcase( i==105 ); /* CURRENT_TIME */
154032 testcase( i==106 ); /* CURRENT */
154033 testcase( i==107 ); /* PARTITION */
154034 testcase( i==108 ); /* DEFERRED */
154035 testcase( i==109 ); /* DISTINCT */
154036 testcase( i==110 ); /* IS */
154037 testcase( i==111 ); /* DROP */
154038 testcase( i==112 ); /* PRECEDING */
154039 testcase( i==113 ); /* FAIL */
154040 testcase( i==114 ); /* FILTER */
154041 testcase( i==115 ); /* REPLACE */
154042 testcase( i==116 ); /* FOLLOWING */
154043 testcase( i==117 ); /* FROM */
154044 testcase( i==118 ); /* FULL */
154045 testcase( i==119 ); /* IF */
154046 testcase( i==120 ); /* ISNULL */
154047 testcase( i==121 ); /* ORDER */
154048 testcase( i==122 ); /* RESTRICT */
154049 testcase( i==123 ); /* OTHERS */
154050 testcase( i==124 ); /* OVER */
154051 testcase( i==125 ); /* RIGHT */
154052 testcase( i==126 ); /* ROLLBACK */
154053 testcase( i==127 ); /* ROWS */
154054 testcase( i==128 ); /* ROW */
154055 testcase( i==129 ); /* UNBOUNDED */
154056 testcase( i==130 ); /* UNION */
154057 testcase( i==131 ); /* USING */
154058 testcase( i==132 ); /* VACUUM */
154059 testcase( i==133 ); /* VIEW */
154060 testcase( i==134 ); /* WINDOW */
154061 testcase( i==135 ); /* DO */
154062 testcase( i==136 ); /* BY */
154063 testcase( i==137 ); /* INITIALLY */
154064 testcase( i==138 ); /* ALL */
154065 testcase( i==139 ); /* PRIMARY */
154066 *pType = aKWCode[i];
154067 break;
154068 }
154069 }
154070 return n;
154071}
154072SQLITE_PRIVATEstatic int sqlite3KeywordCode(const unsigned char *z, int n){
154073 int id = TK_ID59;
154074 keywordCode((char*)z, n, &id);
154075 return id;
154076}
154077#define SQLITE_N_KEYWORD140 140
154078SQLITE_API int sqlite3_keyword_name(int i,const char **pzName,int *pnName){
154079 if( i<0 || i>=SQLITE_N_KEYWORD140 ) return SQLITE_ERROR1;
154080 *pzName = zKWText + aKWOffset[i];
154081 *pnName = aKWLen[i];
154082 return SQLITE_OK0;
154083}
154084SQLITE_API int sqlite3_keyword_count(void){ return SQLITE_N_KEYWORD140; }
154085SQLITE_API int sqlite3_keyword_check(const char *zName, int nName){
154086 return TK_ID59!=sqlite3KeywordCode((const u8*)zName, nName);
154087}
154088
154089/************** End of keywordhash.h *****************************************/
154090/************** Continuing where we left off in tokenize.c *******************/
154091
154092
154093/*
154094** If X is a character that can be used in an identifier then
154095** IdChar(X) will be true. Otherwise it is false.
154096**
154097** For ASCII, any character with the high-order bit set is
154098** allowed in an identifier. For 7-bit characters,
154099** sqlite3IsIdChar[X] must be 1.
154100**
154101** For EBCDIC, the rules are more complex but have the same
154102** end result.
154103**
154104** Ticket #1066. the SQL standard does not allow '$' in the
154105** middle of identifiers. But many SQL implementations do.
154106** SQLite will allow '$' in identifiers for compatibility.
154107** But the feature is undocumented.
154108*/
154109#ifdef SQLITE_ASCII1
154110#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
154111#endif
154112#ifdef SQLITE_EBCDIC
154113SQLITE_PRIVATEstatic const char sqlite3IsEbcdicIdChar[] = {
154114/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
154115 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
154116 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
154117 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
154118 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
154119 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
154120 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
154121 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
154122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
154123 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
154124 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
154125 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
154126 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
154127};
154128#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
154129#endif
154130
154131/* Make the IdChar function accessible from ctime.c and alter.c */
154132SQLITE_PRIVATEstatic int sqlite3IsIdChar(u8 c){ return IdChar(c)((sqlite3CtypeMap[(unsigned char)c]&0x46)!=0); }
154133
154134#ifndef SQLITE_OMIT_WINDOWFUNC
154135/*
154136** Return the id of the next token in string (*pz). Before returning, set
154137** (*pz) to point to the byte following the parsed token.
154138*/
154139static int getToken(const unsigned char **pz){
154140 const unsigned char *z = *pz;
154141 int t; /* Token type to return */
154142 do {
154143 z += sqlite3GetToken(z, &t);
154144 }while( t==TK_SPACE174 );
154145 if( t==TK_ID59
154146 || t==TK_STRING110
154147 || t==TK_JOIN_KW111
154148 || t==TK_WINDOW156
154149 || t==TK_OVER157
154150 || sqlite3ParserFallback(t)==TK_ID59
154151 ){
154152 t = TK_ID59;
154153 }
154154 *pz = z;
154155 return t;
154156}
154157
154158/*
154159** The following three functions are called immediately after the tokenizer
154160** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
154161** whether the token should be treated as a keyword or an SQL identifier.
154162** This cannot be handled by the usual lemon %fallback method, due to
154163** the ambiguity in some constructions. e.g.
154164**
154165** SELECT sum(x) OVER ...
154166**
154167** In the above, "OVER" might be a keyword, or it might be an alias for the
154168** sum(x) expression. If a "%fallback ID OVER" directive were added to
154169** grammar, then SQLite would always treat "OVER" as an alias, making it
154170** impossible to call a window-function without a FILTER clause.
154171**
154172** WINDOW is treated as a keyword if:
154173**
154174** * the following token is an identifier, or a keyword that can fallback
154175** to being an identifier, and
154176** * the token after than one is TK_AS.
154177**
154178** OVER is a keyword if:
154179**
154180** * the previous token was TK_RP, and
154181** * the next token is either TK_LP or an identifier.
154182**
154183** FILTER is a keyword if:
154184**
154185** * the previous token was TK_RP, and
154186** * the next token is TK_LP.
154187*/
154188static int analyzeWindowKeyword(const unsigned char *z){
154189 int t;
154190 t = getToken(&z);
154191 if( t!=TK_ID59 ) return TK_ID59;
154192 t = getToken(&z);
154193 if( t!=TK_AS24 ) return TK_ID59;
154194 return TK_WINDOW156;
154195}
154196static int analyzeOverKeyword(const unsigned char *z, int lastToken){
154197 if( lastToken==TK_RP23 ){
154198 int t = getToken(&z);
154199 if( t==TK_LP22 || t==TK_ID59 ) return TK_OVER157;
154200 }
154201 return TK_ID59;
154202}
154203static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
154204 if( lastToken==TK_RP23 && getToken(&z)==TK_LP22 ){
154205 return TK_FILTER158;
154206 }
154207 return TK_ID59;
154208}
154209#endif /* SQLITE_OMIT_WINDOWFUNC */
154210
154211/*
154212** Return the length (in bytes) of the token that begins at z[0].
154213** Store the token type in *tokenType before returning.
154214*/
154215SQLITE_PRIVATEstatic int sqlite3GetToken(const unsigned char *z, int *tokenType){
154216 int i, c;
154217 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
154218 ** of the token. See the comment on the CC_ defines
154219 ** above. */
154220 case CC_SPACE7: {
154221 testcase( z[0]==' ' );
154222 testcase( z[0]=='\t' );
154223 testcase( z[0]=='\n' );
154224 testcase( z[0]=='\f' );
154225 testcase( z[0]=='\r' );
154226 for(i=1; sqlite3Isspace(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x01); i++){}
154227 *tokenType = TK_SPACE174;
154228 return i;
154229 }
154230 case CC_MINUS11: {
154231 if( z[1]=='-' ){
154232 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
154233 *tokenType = TK_SPACE174; /* IMP: R-22934-25134 */
154234 return i;
154235 }
154236 *tokenType = TK_MINUS101;
154237 return 1;
154238 }
154239 case CC_LP17: {
154240 *tokenType = TK_LP22;
154241 return 1;
154242 }
154243 case CC_RP18: {
154244 *tokenType = TK_RP23;
154245 return 1;
154246 }
154247 case CC_SEMI19: {
154248 *tokenType = TK_SEMI1;
154249 return 1;
154250 }
154251 case CC_PLUS20: {
154252 *tokenType = TK_PLUS100;
154253 return 1;
154254 }
154255 case CC_STAR21: {
154256 *tokenType = TK_STAR102;
154257 return 1;
154258 }
154259 case CC_SLASH16: {
154260 if( z[1]!='*' || z[2]==0 ){
154261 *tokenType = TK_SLASH103;
154262 return 1;
154263 }
154264 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
154265 if( c ) i++;
154266 *tokenType = TK_SPACE174; /* IMP: R-22934-25134 */
154267 return i;
154268 }
154269 case CC_PERCENT22: {
154270 *tokenType = TK_REM104;
154271 return 1;
154272 }
154273 case CC_EQ14: {
154274 *tokenType = TK_EQ53;
154275 return 1 + (z[1]=='=');
154276 }
154277 case CC_LT12: {
154278 if( (c=z[1])=='=' ){
154279 *tokenType = TK_LE55;
154280 return 2;
154281 }else if( c=='>' ){
154282 *tokenType = TK_NE52;
154283 return 2;
154284 }else if( c=='<' ){
154285 *tokenType = TK_LSHIFT98;
154286 return 2;
154287 }else{
154288 *tokenType = TK_LT56;
154289 return 1;
154290 }
154291 }
154292 case CC_GT13: {
154293 if( (c=z[1])=='=' ){
154294 *tokenType = TK_GE57;
154295 return 2;
154296 }else if( c=='>' ){
154297 *tokenType = TK_RSHIFT99;
154298 return 2;
154299 }else{
154300 *tokenType = TK_GT54;
154301 return 1;
154302 }
154303 }
154304 case CC_BANG15: {
154305 if( z[1]!='=' ){
154306 *tokenType = TK_ILLEGAL175;
154307 return 1;
154308 }else{
154309 *tokenType = TK_NE52;
154310 return 2;
154311 }
154312 }
154313 case CC_PIPE10: {
154314 if( z[1]!='|' ){
154315 *tokenType = TK_BITOR97;
154316 return 1;
154317 }else{
154318 *tokenType = TK_CONCAT105;
154319 return 2;
154320 }
154321 }
154322 case CC_COMMA23: {
154323 *tokenType = TK_COMMA26;
154324 return 1;
154325 }
154326 case CC_AND24: {
154327 *tokenType = TK_BITAND96;
154328 return 1;
154329 }
154330 case CC_TILDA25: {
154331 *tokenType = TK_BITNOT107;
154332 return 1;
154333 }
154334 case CC_QUOTE8: {
154335 int delim = z[0];
154336 testcase( delim=='`' );
154337 testcase( delim=='\'' );
154338 testcase( delim=='"' );
154339 for(i=1; (c=z[i])!=0; i++){
154340 if( c==delim ){
154341 if( z[i+1]==delim ){
154342 i++;
154343 }else{
154344 break;
154345 }
154346 }
154347 }
154348 if( c=='\'' ){
154349 *tokenType = TK_STRING110;
154350 return i+1;
154351 }else if( c!=0 ){
154352 *tokenType = TK_ID59;
154353 return i+1;
154354 }else{
154355 *tokenType = TK_ILLEGAL175;
154356 return i;
154357 }
154358 }
154359 case CC_DOT26: {
154360#ifndef SQLITE_OMIT_FLOATING_POINT
154361 if( !sqlite3Isdigit(z[1])(sqlite3CtypeMap[(unsigned char)(z[1])]&0x04) )
154362#endif
154363 {
154364 *tokenType = TK_DOT134;
154365 return 1;
154366 }
154367 /* If the next character is a digit, this is a floating point
154368 ** number that begins with ".". Fall thru into the next case */
154369 }
154370 case CC_DIGIT3: {
154371 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
154372 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
154373 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
154374 testcase( z[0]=='9' );
154375 *tokenType = TK_INTEGER147;
154376#ifndef SQLITE_OMIT_HEX_INTEGER
154377 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2])(sqlite3CtypeMap[(unsigned char)(z[2])]&0x08) ){
154378 for(i=3; sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08); i++){}
154379 return i;
154380 }
154381#endif
154382 for(i=0; sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04); i++){}
154383#ifndef SQLITE_OMIT_FLOATING_POINT
154384 if( z[i]=='.' ){
154385 i++;
154386 while( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04) ){ i++; }
154387 *tokenType = TK_FLOAT145;
154388 }
154389 if( (z[i]=='e' || z[i]=='E') &&
154390 ( sqlite3Isdigit(z[i+1])(sqlite3CtypeMap[(unsigned char)(z[i+1])]&0x04)
154391 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])(sqlite3CtypeMap[(unsigned char)(z[i+2])]&0x04))
154392 )
154393 ){
154394 i += 2;
154395 while( sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04) ){ i++; }
154396 *tokenType = TK_FLOAT145;
154397 }
154398#endif
154399 while( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){
154400 *tokenType = TK_ILLEGAL175;
154401 i++;
154402 }
154403 return i;
154404 }
154405 case CC_QUOTE29: {
154406 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
154407 *tokenType = c==']' ? TK_ID59 : TK_ILLEGAL175;
154408 return i;
154409 }
154410 case CC_VARNUM6: {
154411 *tokenType = TK_VARIABLE148;
154412 for(i=1; sqlite3Isdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x04); i++){}
154413 return i;
154414 }
154415 case CC_DOLLAR4:
154416 case CC_VARALPHA5: {
154417 int n = 0;
154418 testcase( z[0]=='$' ); testcase( z[0]=='@' );
154419 testcase( z[0]==':' ); testcase( z[0]=='#' );
154420 *tokenType = TK_VARIABLE148;
154421 for(i=1; (c=z[i])!=0; i++){
154422 if( IdChar(c)((sqlite3CtypeMap[(unsigned char)c]&0x46)!=0) ){
154423 n++;
154424#ifndef SQLITE_OMIT_TCL_VARIABLE
154425 }else if( c=='(' && n>0 ){
154426 do{
154427 i++;
154428 }while( (c=z[i])!=0 && !sqlite3Isspace(c)(sqlite3CtypeMap[(unsigned char)(c)]&0x01) && c!=')' );
154429 if( c==')' ){
154430 i++;
154431 }else{
154432 *tokenType = TK_ILLEGAL175;
154433 }
154434 break;
154435 }else if( c==':' && z[i+1]==':' ){
154436 i++;
154437#endif
154438 }else{
154439 break;
154440 }
154441 }
154442 if( n==0 ) *tokenType = TK_ILLEGAL175;
154443 return i;
154444 }
154445 case CC_KYWD1: {
154446 for(i=1; aiClass[z[i]]<=CC_KYWD1; i++){}
154447 if( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){
154448 /* This token started out using characters that can appear in keywords,
154449 ** but z[i] is a character not allowed within keywords, so this must
154450 ** be an identifier instead */
154451 i++;
154452 break;
154453 }
154454 *tokenType = TK_ID59;
154455 return keywordCode((char*)z, i, tokenType);
154456 }
154457 case CC_X0: {
154458#ifndef SQLITE_OMIT_BLOB_LITERAL
154459 testcase( z[0]=='x' ); testcase( z[0]=='X' );
154460 if( z[1]=='\'' ){
154461 *tokenType = TK_BLOB146;
154462 for(i=2; sqlite3Isxdigit(z[i])(sqlite3CtypeMap[(unsigned char)(z[i])]&0x08); i++){}
154463 if( z[i]!='\'' || i%2 ){
154464 *tokenType = TK_ILLEGAL175;
154465 while( z[i] && z[i]!='\'' ){ i++; }
154466 }
154467 if( z[i] ) i++;
154468 return i;
154469 }
154470#endif
154471 /* If it is not a BLOB literal, then it must be an ID, since no
154472 ** SQL keywords start with the letter 'x'. Fall through */
154473 }
154474 case CC_ID2: {
154475 i = 1;
154476 break;
154477 }
154478 case CC_NUL28: {
154479 *tokenType = TK_ILLEGAL175;
154480 return 0;
154481 }
154482 default: {
154483 *tokenType = TK_ILLEGAL175;
154484 return 1;
154485 }
154486 }
154487 while( IdChar(z[i])((sqlite3CtypeMap[(unsigned char)z[i]]&0x46)!=0) ){ i++; }
154488 *tokenType = TK_ID59;
154489 return i;
154490}
154491
154492/*
154493** Run the parser on the given SQL string. The parser structure is
154494** passed in. An SQLITE_ status code is returned. If an error occurs
154495** then an and attempt is made to write an error message into
154496** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
154497** error message.
154498*/
154499SQLITE_PRIVATEstatic int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
154500 int nErr = 0; /* Number of errors encountered */
154501 void *pEngine; /* The LEMON-generated LALR(1) parser */
154502 int n = 0; /* Length of the next token token */
154503 int tokenType; /* type of the next token */
154504 int lastTokenParsed = -1; /* type of the previous token */
154505 sqlite3 *db = pParse->db; /* The database connection */
154506 int mxSqlLen; /* Max length of an SQL string */
154507#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
154508 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
154509#endif
154510 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
154511
154512 assert( zSql!=0 )((void) (0));
154513 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH1];
154514 if( db->nVdbeActive==0 ){
154515 db->u1.isInterrupted = 0;
154516 }
154517 pParse->rc = SQLITE_OK0;
154518 pParse->zTail = zSql;
154519 assert( pzErrMsg!=0 )((void) (0));
154520#ifdef SQLITE_DEBUG
154521 if( db->flags & SQLITE_ParserTrace ){
154522 printf("parser: [[[%s]]]\n", zSql);
154523 sqlite3ParserTrace(stdoutstdout, "parser: ");
154524 }else{
154525 sqlite3ParserTrace(0, 0);
154526 }
154527#endif
154528#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
154529 pEngine = &sEngine;
154530 sqlite3ParserInit(pEngine, pParse);
154531#else
154532 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
154533 if( pEngine==0 ){
154534 sqlite3OomFault(db);
154535 return SQLITE_NOMEM_BKPT7;
154536 }
154537#endif
154538 assert( pParse->pNewTable==0 )((void) (0));
154539 assert( pParse->pNewTrigger==0 )((void) (0));
154540 assert( pParse->nVar==0 )((void) (0));
154541 assert( pParse->pVList==0 )((void) (0));
154542 pParse->pParentParse = db->pParse;
154543 db->pParse = pParse;
154544 while( 1 ){
154545 n = sqlite3GetToken((u8*)zSql, &tokenType);
154546 mxSqlLen -= n;
154547 if( mxSqlLen<0 ){
154548 pParse->rc = SQLITE_TOOBIG18;
154549 break;
154550 }
154551#ifndef SQLITE_OMIT_WINDOWFUNC
154552 if( tokenType>=TK_WINDOW156 ){
154553 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER((void) (0))
154554 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW((void) (0))
154555 )((void) (0));
154556#else
154557 if( tokenType>=TK_SPACE174 ){
154558 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL )((void) (0));
154559#endif /* SQLITE_OMIT_WINDOWFUNC */
154560 if( db->u1.isInterrupted ){
154561 pParse->rc = SQLITE_INTERRUPT9;
154562 break;
154563 }
154564 if( tokenType==TK_SPACE174 ){
154565 zSql += n;
154566 continue;
154567 }
154568 if( zSql[0]==0 ){
154569 /* Upon reaching the end of input, call the parser two more times
154570 ** with tokens TK_SEMI and 0, in that order. */
154571 if( lastTokenParsed==TK_SEMI1 ){
154572 tokenType = 0;
154573 }else if( lastTokenParsed==0 ){
154574 break;
154575 }else{
154576 tokenType = TK_SEMI1;
154577 }
154578 n = 0;
154579#ifndef SQLITE_OMIT_WINDOWFUNC
154580 }else if( tokenType==TK_WINDOW156 ){
154581 assert( n==6 )((void) (0));
154582 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
154583 }else if( tokenType==TK_OVER157 ){
154584 assert( n==4 )((void) (0));
154585 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
154586 }else if( tokenType==TK_FILTER158 ){
154587 assert( n==6 )((void) (0));
154588 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
154589#endif /* SQLITE_OMIT_WINDOWFUNC */
154590 }else{
154591 sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
154592 break;
154593 }
154594 }
154595 pParse->sLastToken.z = zSql;
154596 pParse->sLastToken.n = n;
154597 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
154598 lastTokenParsed = tokenType;
154599 zSql += n;
154600 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom )((void) (0));
154601 if( pParse->rc!=SQLITE_OK0 ) break;
154602 }
154603 assert( nErr==0 )((void) (0));
154604#ifdef YYTRACKMAXSTACKDEPTH
154605 sqlite3_mutex_enter(sqlite3MallocMutex());
154606 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK6,
154607 sqlite3ParserStackPeak(pEngine)
154608 );
154609 sqlite3_mutex_leave(sqlite3MallocMutex());
154610#endif /* YYDEBUG */
154611#ifdef sqlite3Parser_ENGINEALWAYSONSTACK1
154612 sqlite3ParserFinalize(pEngine);
154613#else
154614 sqlite3ParserFree(pEngine, sqlite3_free);
154615#endif
154616 if( db->mallocFailed ){
154617 pParse->rc = SQLITE_NOMEM_BKPT7;
154618 }
154619 if( pParse->rc!=SQLITE_OK0 && pParse->rc!=SQLITE_DONE101 && pParse->zErrMsg==0 ){
154620 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
154621 }
154622 assert( pzErrMsg!=0 )((void) (0));
154623 if( pParse->zErrMsg ){
154624 *pzErrMsg = pParse->zErrMsg;
154625 sqlite3_log(pParse->rc, "%s in \"%s\"",
154626 *pzErrMsg, pParse->zTail);
154627 pParse->zErrMsg = 0;
154628 nErr++;
154629 }
154630 pParse->zTail = zSql;
154631 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
154632 sqlite3VdbeDelete(pParse->pVdbe);
154633 pParse->pVdbe = 0;
154634 }
154635#ifndef SQLITE_OMIT_SHARED_CACHE
154636 if( pParse->nested==0 ){
154637 sqlite3DbFree(db, pParse->aTableLock);
154638 pParse->aTableLock = 0;
154639 pParse->nTableLock = 0;
154640 }
154641#endif
154642#ifndef SQLITE_OMIT_VIRTUALTABLE
154643 sqlite3_free(pParse->apVtabLock);
154644#endif
154645
154646 if( !IN_SPECIAL_PARSE(pParse->eParseMode!=0) ){
154647 /* If the pParse->declareVtab flag is set, do not delete any table
154648 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
154649 ** will take responsibility for freeing the Table structure.
154650 */
154651 sqlite3DeleteTable(db, pParse->pNewTable);
154652 }
154653 if( !IN_RENAME_OBJECT(pParse->eParseMode>=2) ){
154654 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
154655 }
154656
154657 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
154658 sqlite3DbFree(db, pParse->pVList);
154659 while( pParse->pAinc ){
154660 AutoincInfo *p = pParse->pAinc;
154661 pParse->pAinc = p->pNext;
154662 sqlite3DbFreeNN(db, p);
154663 }
154664 while( pParse->pZombieTab ){
154665 Table *p = pParse->pZombieTab;
154666 pParse->pZombieTab = p->pNextZombie;
154667 sqlite3DeleteTable(db, p);
154668 }
154669 db->pParse = pParse->pParentParse;
154670 pParse->pParentParse = 0;
154671 assert( nErr==0 || pParse->rc!=SQLITE_OK )((void) (0));
154672 return nErr;
154673}
154674
154675
154676#ifdef SQLITE_ENABLE_NORMALIZE
154677/*
154678** Insert a single space character into pStr if the current string
154679** ends with an identifier
154680*/
154681static void addSpaceSeparator(sqlite3_str *pStr){
154682 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
154683 sqlite3_str_append(pStr, " ", 1);
154684 }
154685}
154686
154687/*
154688** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
154689** the normalization in space obtained from sqlite3DbMalloc(). Or return
154690** NULL if anything goes wrong or if zSql is NULL.
154691*/
154692SQLITE_PRIVATEstatic char *sqlite3Normalize(
154693 Vdbe *pVdbe, /* VM being reprepared */
154694 const char *zSql /* The original SQL string */
154695){
154696 sqlite3 *db; /* The database connection */
154697 int i; /* Next unread byte of zSql[] */
154698 int n; /* length of current token */
154699 int tokenType; /* type of current token */
154700 int prevType = 0; /* Previous non-whitespace token */
154701 int nParen; /* Number of nested levels of parentheses */
154702 int iStartIN; /* Start of RHS of IN operator in z[] */
154703 int nParenAtIN; /* Value of nParent at start of RHS of IN operator */
154704 int j; /* Bytes of normalized SQL generated so far */
154705 sqlite3_str *pStr; /* The normalized SQL string under construction */
154706
154707 db = sqlite3VdbeDb(pVdbe);
154708 tokenType = -1;
154709 nParen = iStartIN = nParenAtIN = 0;
154710 pStr = sqlite3_str_new(db);
154711 assert( pStr!=0 )((void) (0)); /* sqlite3_str_new() never returns NULL */
154712 for(i=0; zSql[i] && pStr->accError==0; i+=n){
154713 if( tokenType!=TK_SPACE174 ){
154714 prevType = tokenType;
154715 }
154716 n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType);
154717 if( NEVER(n<=0)(n<=0) ) break;
154718 switch( tokenType ){
154719 case TK_SPACE174: {
154720 break;
154721 }
154722 case TK_NULL114: {
154723 if( prevType==TK_IS45 || prevType==TK_NOT19 ){
154724 sqlite3_str_append(pStr, " NULL", 5);
154725 break;
154726 }
154727 /* Fall through */
154728 }
154729 case TK_STRING110:
154730 case TK_INTEGER147:
154731 case TK_FLOAT145:
154732 case TK_VARIABLE148:
154733 case TK_BLOB146: {
154734 sqlite3_str_append(pStr, "?", 1);
154735 break;
154736 }
154737 case TK_LP22: {
154738 nParen++;
154739 if( prevType==TK_IN49 ){
154740 iStartIN = pStr->nChar;
154741 nParenAtIN = nParen;
154742 }
154743 sqlite3_str_append(pStr, "(", 1);
154744 break;
154745 }
154746 case TK_RP23: {
154747 if( iStartIN>0 && nParen==nParenAtIN ){
154748 assert( pStr->nChar>=iStartIN )((void) (0));
154749 pStr->nChar = iStartIN+1;
154750 sqlite3_str_append(pStr, "?,?,?", 5);
154751 iStartIN = 0;
154752 }
154753 nParen--;
154754 sqlite3_str_append(pStr, ")", 1);
154755 break;
154756 }
154757 case TK_ID59: {
154758 iStartIN = 0;
154759 j = pStr->nChar;
154760 if( sqlite3Isquote(zSql[i])(sqlite3CtypeMap[(unsigned char)(zSql[i])]&0x80) ){
154761 char *zId = sqlite3DbStrNDup(db, zSql+i, n);
154762 int nId;
154763 int eType = 0;
154764 if( zId==0 ) break;
154765 sqlite3Dequote(zId);
154766 if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){
154767 sqlite3_str_append(pStr, "?", 1);
154768 sqlite3DbFree(db, zId);
154769 break;
154770 }
154771 nId = sqlite3Strlen30(zId);
154772 if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID59 ){
154773 addSpaceSeparator(pStr);
154774 sqlite3_str_append(pStr, zId, nId);
154775 }else{
154776 sqlite3_str_appendf(pStr, "\"%w\"", zId);
154777 }
154778 sqlite3DbFree(db, zId);
154779 }else{
154780 addSpaceSeparator(pStr);
154781 sqlite3_str_append(pStr, zSql+i, n);
154782 }
154783 while( j<pStr->nChar ){
154784 pStr->zText[j] = sqlite3Tolower(pStr->zText[j])(sqlite3UpperToLower[(unsigned char)(pStr->zText[j])]);
154785 j++;
154786 }
154787 break;
154788 }
154789 case TK_SELECT131: {
154790 iStartIN = 0;
154791 /* fall through */
154792 }
154793 default: {
154794 if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr);
154795 j = pStr->nChar;
154796 sqlite3_str_append(pStr, zSql+i, n);
154797 while( j<pStr->nChar ){
154798 pStr->zText[j] = sqlite3Toupper(pStr->zText[j])((pStr->zText[j])&~(sqlite3CtypeMap[(unsigned char)(pStr
->zText[j])]&0x20))
;
154799 j++;
154800 }
154801 break;
154802 }
154803 }
154804 }
154805 if( tokenType!=TK_SEMI1 ) sqlite3_str_append(pStr, ";", 1);
154806 return sqlite3_str_finish(pStr);
154807}
154808#endif /* SQLITE_ENABLE_NORMALIZE */
154809
154810/************** End of tokenize.c ********************************************/
154811/************** Begin file complete.c ****************************************/
154812/*
154813** 2001 September 15
154814**
154815** The author disclaims copyright to this source code. In place of
154816** a legal notice, here is a blessing:
154817**
154818** May you do good and not evil.
154819** May you find forgiveness for yourself and forgive others.
154820** May you share freely, never taking more than you give.
154821**
154822*************************************************************************
154823** An tokenizer for SQL
154824**
154825** This file contains C code that implements the sqlite3_complete() API.
154826** This code used to be part of the tokenizer.c source file. But by
154827** separating it out, the code will be automatically omitted from
154828** static links that do not use it.
154829*/
154830/* #include "sqliteInt.h" */
154831#ifndef SQLITE_OMIT_COMPLETE
154832
154833/*
154834** This is defined in tokenize.c. We just have to import the definition.
154835*/
154836#ifndef SQLITE_AMALGAMATION1
154837#ifdef SQLITE_ASCII1
154838#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
154839#endif
154840#ifdef SQLITE_EBCDIC
154841SQLITE_PRIVATEstatic const char sqlite3IsEbcdicIdChar[];
154842#define IdChar(C)((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
154843#endif
154844#endif /* SQLITE_AMALGAMATION */
154845
154846
154847/*
154848** Token types used by the sqlite3_complete() routine. See the header
154849** comments on that procedure for additional information.
154850*/
154851#define tkSEMI0 0
154852#define tkWS1 1
154853#define tkOTHER2 2
154854#ifndef SQLITE_OMIT_TRIGGER
154855#define tkEXPLAIN3 3
154856#define tkCREATE4 4
154857#define tkTEMP5 5
154858#define tkTRIGGER6 6
154859#define tkEND7 7
154860#endif
154861
154862/*
154863** Return TRUE if the given SQL string ends in a semicolon.
154864**
154865** Special handling is require for CREATE TRIGGER statements.
154866** Whenever the CREATE TRIGGER keywords are seen, the statement
154867** must end with ";END;".
154868**
154869** This implementation uses a state machine with 8 states:
154870**
154871** (0) INVALID We have not yet seen a non-whitespace character.
154872**
154873** (1) START At the beginning or end of an SQL statement. This routine
154874** returns 1 if it ends in the START state and 0 if it ends
154875** in any other state.
154876**
154877** (2) NORMAL We are in the middle of statement which ends with a single
154878** semicolon.
154879**
154880** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
154881** a statement.
154882**
154883** (4) CREATE The keyword CREATE has been seen at the beginning of a
154884** statement, possibly preceded by EXPLAIN and/or followed by
154885** TEMP or TEMPORARY
154886**
154887** (5) TRIGGER We are in the middle of a trigger definition that must be
154888** ended by a semicolon, the keyword END, and another semicolon.
154889**
154890** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at
154891** the end of a trigger definition.
154892**
154893** (7) END We've seen the ";END" of the ";END;" that occurs at the end
154894** of a trigger definition.
154895**
154896** Transitions between states above are determined by tokens extracted
154897** from the input. The following tokens are significant:
154898**
154899** (0) tkSEMI A semicolon.
154900** (1) tkWS Whitespace.
154901** (2) tkOTHER Any other SQL token.
154902** (3) tkEXPLAIN The "explain" keyword.
154903** (4) tkCREATE The "create" keyword.
154904** (5) tkTEMP The "temp" or "temporary" keyword.
154905** (6) tkTRIGGER The "trigger" keyword.
154906** (7) tkEND The "end" keyword.
154907**
154908** Whitespace never causes a state transition and is always ignored.
154909** This means that a SQL string of all whitespace is invalid.
154910**
154911** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
154912** to recognize the end of a trigger can be omitted. All we have to do
154913** is look for a semicolon that is not part of an string or comment.
154914*/
154915SQLITE_API int sqlite3_complete(const char *zSql){
154916 u8 state = 0; /* Current state, using numbers defined in header comment */
154917 u8 token; /* Value of the next token */
154918
154919#ifndef SQLITE_OMIT_TRIGGER
154920 /* A complex statement machine used to detect the end of a CREATE TRIGGER
154921 ** statement. This is the normal case.
154922 */
154923 static const u8 trans[8][8] = {
154924 /* Token: */
154925 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */
154926 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, },
154927 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, },
154928 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, },
154929 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, },
154930 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, },
154931 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, },
154932 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, },
154933 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, },
154934 };
154935#else
154936 /* If triggers are not supported by this compile then the statement machine
154937 ** used to detect the end of a statement is much simpler
154938 */
154939 static const u8 trans[3][3] = {
154940 /* Token: */
154941 /* State: ** SEMI WS OTHER */
154942 /* 0 INVALID: */ { 1, 0, 2, },
154943 /* 1 START: */ { 1, 1, 2, },
154944 /* 2 NORMAL: */ { 1, 2, 2, },
154945 };
154946#endif /* SQLITE_OMIT_TRIGGER */
154947
154948#ifdef SQLITE_ENABLE_API_ARMOR
154949 if( zSql==0 ){
154950 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(154950);
154951 return 0;
154952 }
154953#endif
154954
154955 while( *zSql ){
154956 switch( *zSql ){
154957 case ';': { /* A semicolon */
154958 token = tkSEMI0;
154959 break;
154960 }
154961 case ' ':
154962 case '\r':
154963 case '\t':
154964 case '\n':
154965 case '\f': { /* White space is ignored */
154966 token = tkWS1;
154967 break;
154968 }
154969 case '/': { /* C-style comments */
154970 if( zSql[1]!='*' ){
154971 token = tkOTHER2;
154972 break;
154973 }
154974 zSql += 2;
154975 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
154976 if( zSql[0]==0 ) return 0;
154977 zSql++;
154978 token = tkWS1;
154979 break;
154980 }
154981 case '-': { /* SQL-style comments from "--" to end of line */
154982 if( zSql[1]!='-' ){
154983 token = tkOTHER2;
154984 break;
154985 }
154986 while( *zSql && *zSql!='\n' ){ zSql++; }
154987 if( *zSql==0 ) return state==1;
154988 token = tkWS1;
154989 break;
154990 }
154991 case '[': { /* Microsoft-style identifiers in [...] */
154992 zSql++;
154993 while( *zSql && *zSql!=']' ){ zSql++; }
154994 if( *zSql==0 ) return 0;
154995 token = tkOTHER2;
154996 break;
154997 }
154998 case '`': /* Grave-accent quoted symbols used by MySQL */
154999 case '"': /* single- and double-quoted strings */
155000 case '\'': {
155001 int c = *zSql;
155002 zSql++;
155003 while( *zSql && *zSql!=c ){ zSql++; }
155004 if( *zSql==0 ) return 0;
155005 token = tkOTHER2;
155006 break;
155007 }
155008 default: {
155009#ifdef SQLITE_EBCDIC
155010 unsigned char c;
155011#endif
155012 if( IdChar((u8)*zSql)((sqlite3CtypeMap[(unsigned char)(u8)*zSql]&0x46)!=0) ){
155013 /* Keywords and unquoted identifiers */
155014 int nId;
155015 for(nId=1; IdChar(zSql[nId])((sqlite3CtypeMap[(unsigned char)zSql[nId]]&0x46)!=0); nId++){}
155016#ifdef SQLITE_OMIT_TRIGGER
155017 token = tkOTHER2;
155018#else
155019 switch( *zSql ){
155020 case 'c': case 'C': {
155021 if( nId==6 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "create", 6)==0 ){
155022 token = tkCREATE4;
155023 }else{
155024 token = tkOTHER2;
155025 }
155026 break;
155027 }
155028 case 't': case 'T': {
155029 if( nId==7 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "trigger", 7)==0 ){
155030 token = tkTRIGGER6;
155031 }else if( nId==4 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "temp", 4)==0 ){
155032 token = tkTEMP5;
155033 }else if( nId==9 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "temporary", 9)==0 ){
155034 token = tkTEMP5;
155035 }else{
155036 token = tkOTHER2;
155037 }
155038 break;
155039 }
155040 case 'e': case 'E': {
155041 if( nId==3 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "end", 3)==0 ){
155042 token = tkEND7;
155043 }else
155044#ifndef SQLITE_OMIT_EXPLAIN
155045 if( nId==7 && sqlite3StrNICmpsqlite3_strnicmp(zSql, "explain", 7)==0 ){
155046 token = tkEXPLAIN3;
155047 }else
155048#endif
155049 {
155050 token = tkOTHER2;
155051 }
155052 break;
155053 }
155054 default: {
155055 token = tkOTHER2;
155056 break;
155057 }
155058 }
155059#endif /* SQLITE_OMIT_TRIGGER */
155060 zSql += nId-1;
155061 }else{
155062 /* Operators and special symbols */
155063 token = tkOTHER2;
155064 }
155065 break;
155066 }
155067 }
155068 state = trans[state][token];
155069 zSql++;
155070 }
155071 return state==1;
155072}
155073
155074#ifndef SQLITE_OMIT_UTF16
155075/*
155076** This routine is the same as the sqlite3_complete() routine described
155077** above, except that the parameter is required to be UTF-16 encoded, not
155078** UTF-8.
155079*/
155080SQLITE_API int sqlite3_complete16(const void *zSql){
155081 sqlite3_value *pVal;
155082 char const *zSql8;
155083 int rc;
155084
155085#ifndef SQLITE_OMIT_AUTOINIT
155086 rc = sqlite3_initialize();
155087 if( rc ) return rc;
155088#endif
155089 pVal = sqlite3ValueNew(0);
155090 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE2, SQLITE_STATIC((sqlite3_destructor_type)0));
155091 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF81);
155092 if( zSql8 ){
155093 rc = sqlite3_complete(zSql8);
155094 }else{
155095 rc = SQLITE_NOMEM_BKPT7;
155096 }
155097 sqlite3ValueFree(pVal);
155098 return rc & 0xff;
155099}
155100#endif /* SQLITE_OMIT_UTF16 */
155101#endif /* SQLITE_OMIT_COMPLETE */
155102
155103/************** End of complete.c ********************************************/
155104/************** Begin file main.c ********************************************/
155105/*
155106** 2001 September 15
155107**
155108** The author disclaims copyright to this source code. In place of
155109** a legal notice, here is a blessing:
155110**
155111** May you do good and not evil.
155112** May you find forgiveness for yourself and forgive others.
155113** May you share freely, never taking more than you give.
155114**
155115*************************************************************************
155116** Main file for the SQLite library. The routines in this file
155117** implement the programmer interface to the library. Routines in
155118** other files are for internal use by SQLite and should not be
155119** accessed by users of the library.
155120*/
155121/* #include "sqliteInt.h" */
155122
155123#ifdef SQLITE_ENABLE_FTS3
155124/************** Include fts3.h in the middle of main.c ***********************/
155125/************** Begin file fts3.h ********************************************/
155126/*
155127** 2006 Oct 10
155128**
155129** The author disclaims copyright to this source code. In place of
155130** a legal notice, here is a blessing:
155131**
155132** May you do good and not evil.
155133** May you find forgiveness for yourself and forgive others.
155134** May you share freely, never taking more than you give.
155135**
155136******************************************************************************
155137**
155138** This header file is used by programs that want to link against the
155139** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
155140*/
155141/* #include "sqlite3.h" */
155142
155143#if 0
155144extern "C" {
155145#endif /* __cplusplus */
155146
155147SQLITE_PRIVATEstatic int sqlite3Fts3Init(sqlite3 *db);
155148
155149#if 0
155150} /* extern "C" */
155151#endif /* __cplusplus */
155152
155153/************** End of fts3.h ************************************************/
155154/************** Continuing where we left off in main.c ***********************/
155155#endif
155156#ifdef SQLITE_ENABLE_RTREE
155157/************** Include rtree.h in the middle of main.c **********************/
155158/************** Begin file rtree.h *******************************************/
155159/*
155160** 2008 May 26
155161**
155162** The author disclaims copyright to this source code. In place of
155163** a legal notice, here is a blessing:
155164**
155165** May you do good and not evil.
155166** May you find forgiveness for yourself and forgive others.
155167** May you share freely, never taking more than you give.
155168**
155169******************************************************************************
155170**
155171** This header file is used by programs that want to link against the
155172** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
155173*/
155174/* #include "sqlite3.h" */
155175
155176#ifdef SQLITE_OMIT_VIRTUALTABLE
155177# undef SQLITE_ENABLE_RTREE
155178#endif
155179
155180#if 0
155181extern "C" {
155182#endif /* __cplusplus */
155183
155184SQLITE_PRIVATEstatic int sqlite3RtreeInit(sqlite3 *db);
155185
155186#if 0
155187} /* extern "C" */
155188#endif /* __cplusplus */
155189
155190/************** End of rtree.h ***********************************************/
155191/************** Continuing where we left off in main.c ***********************/
155192#endif
155193#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
155194/************** Include sqliteicu.h in the middle of main.c ******************/
155195/************** Begin file sqliteicu.h ***************************************/
155196/*
155197** 2008 May 26
155198**
155199** The author disclaims copyright to this source code. In place of
155200** a legal notice, here is a blessing:
155201**
155202** May you do good and not evil.
155203** May you find forgiveness for yourself and forgive others.
155204** May you share freely, never taking more than you give.
155205**
155206******************************************************************************
155207**
155208** This header file is used by programs that want to link against the
155209** ICU extension. All it does is declare the sqlite3IcuInit() interface.
155210*/
155211/* #include "sqlite3.h" */
155212
155213#if 0
155214extern "C" {
155215#endif /* __cplusplus */
155216
155217SQLITE_PRIVATEstatic int sqlite3IcuInit(sqlite3 *db);
155218
155219#if 0
155220} /* extern "C" */
155221#endif /* __cplusplus */
155222
155223
155224/************** End of sqliteicu.h *******************************************/
155225/************** Continuing where we left off in main.c ***********************/
155226#endif
155227#ifdef SQLITE_ENABLE_JSON1
155228SQLITE_PRIVATEstatic int sqlite3Json1Init(sqlite3*);
155229#endif
155230#ifdef SQLITE_ENABLE_STMTVTAB
155231SQLITE_PRIVATEstatic int sqlite3StmtVtabInit(sqlite3*);
155232#endif
155233#ifdef SQLITE_ENABLE_FTS5
155234SQLITE_PRIVATEstatic int sqlite3Fts5Init(sqlite3*);
155235#endif
155236
155237#ifndef SQLITE_AMALGAMATION1
155238/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
155239** contains the text of SQLITE_VERSION macro.
155240*/
155241SQLITE_API const char sqlite3_version[] = SQLITE_VERSION"3.29.0";
155242#endif
155243
155244/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
155245** a pointer to the to the sqlite3_version[] string constant.
155246*/
155247SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
155248
155249/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
155250** pointer to a string constant whose value is the same as the
155251** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
155252** an edited copy of the amalgamation, then the last four characters of
155253** the hash might be different from SQLITE_SOURCE_ID.
155254*/
155255/* SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } */
155256
155257/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
155258** returns an integer equal to SQLITE_VERSION_NUMBER.
155259*/
155260SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER3029000; }
155261
155262/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
155263** zero if and only if SQLite was compiled with mutexing code omitted due to
155264** the SQLITE_THREADSAFE compile-time option being set to 0.
155265*/
155266SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE1; }
155267
155268/*
155269** When compiling the test fixture or with debugging enabled (on Win32),
155270** this variable being set to non-zero will cause OSTRACE macros to emit
155271** extra diagnostic information.
155272*/
155273#ifdef SQLITE_HAVE_OS_TRACE
155274# ifndef SQLITE_DEBUG_OS_TRACE
155275# define SQLITE_DEBUG_OS_TRACE 0
155276# endif
155277 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
155278#endif
155279
155280#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
155281/*
155282** If the following function pointer is not NULL and if
155283** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
155284** I/O active are written using this function. These messages
155285** are intended for debugging activity only.
155286*/
155287SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
155288#endif
155289
155290/*
155291** If the following global variable points to a string which is the
155292** name of a directory, then that directory will be used to store
155293** temporary files.
155294**
155295** See also the "PRAGMA temp_store_directory" SQL command.
155296*/
155297SQLITE_API char *sqlite3_temp_directory = 0;
155298
155299/*
155300** If the following global variable points to a string which is the
155301** name of a directory, then that directory will be used to store
155302** all database files specified with a relative pathname.
155303**
155304** See also the "PRAGMA data_store_directory" SQL command.
155305*/
155306SQLITE_API char *sqlite3_data_directory = 0;
155307
155308/*
155309** Initialize SQLite.
155310**
155311** This routine must be called to initialize the memory allocation,
155312** VFS, and mutex subsystems prior to doing any serious work with
155313** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
155314** this routine will be called automatically by key routines such as
155315** sqlite3_open().
155316**
155317** This routine is a no-op except on its very first call for the process,
155318** or for the first call after a call to sqlite3_shutdown.
155319**
155320** The first thread to call this routine runs the initialization to
155321** completion. If subsequent threads call this routine before the first
155322** thread has finished the initialization process, then the subsequent
155323** threads must block until the first thread finishes with the initialization.
155324**
155325** The first thread might call this routine recursively. Recursive
155326** calls to this routine should not block, of course. Otherwise the
155327** initialization process would never complete.
155328**
155329** Let X be the first thread to enter this routine. Let Y be some other
155330** thread. Then while the initial invocation of this routine by X is
155331** incomplete, it is required that:
155332**
155333** * Calls to this routine from Y must block until the outer-most
155334** call by X completes.
155335**
155336** * Recursive calls to this routine from thread X return immediately
155337** without blocking.
155338*/
155339SQLITE_API int sqlite3_initialize(void){
155340 MUTEX_LOGIC( sqlite3_mutex *pMaster; )sqlite3_mutex *pMaster; /* The main static mutex */
155341 int rc; /* Result code */
155342#ifdef SQLITE_EXTRA_INIT
155343 int bRunExtraInit = 0; /* Extra initialization needed */
155344#endif
155345
155346#ifdef SQLITE_OMIT_WSD
155347 rc = sqlite3_wsd_init(4096, 24);
155348 if( rc!=SQLITE_OK0 ){
155349 return rc;
155350 }
155351#endif
155352
155353 /* If the following assert() fails on some obscure processor/compiler
155354 ** combination, the work-around is to set the correct pointer
155355 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
155356 assert( SQLITE_PTRSIZE==sizeof(char*) )((void) (0));
155357
155358 /* If SQLite is already completely initialized, then this call
155359 ** to sqlite3_initialize() should be a no-op. But the initialization
155360 ** must be complete. So isInit must not be set until the very end
155361 ** of this routine.
155362 */
155363 if( sqlite3GlobalConfigsqlite3Config.isInit ) return SQLITE_OK0;
155364
155365 /* Make sure the mutex subsystem is initialized. If unable to
155366 ** initialize the mutex subsystem, return early with the error.
155367 ** If the system is so sick that we are unable to allocate a mutex,
155368 ** there is not much SQLite is going to be able to do.
155369 **
155370 ** The mutex subsystem must take care of serializing its own
155371 ** initialization.
155372 */
155373 rc = sqlite3MutexInit();
155374 if( rc ) return rc;
155375
155376 /* Initialize the malloc() system and the recursive pInitMutex mutex.
155377 ** This operation is protected by the STATIC_MASTER mutex. Note that
155378 ** MutexAlloc() is called for a static mutex prior to initializing the
155379 ** malloc subsystem - this implies that the allocation of a static
155380 ** mutex must not require support from the malloc subsystem.
155381 */
155382 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )pMaster = sqlite3MutexAlloc(2);
155383 sqlite3_mutex_enter(pMaster);
155384 sqlite3GlobalConfigsqlite3Config.isMutexInit = 1;
155385 if( !sqlite3GlobalConfigsqlite3Config.isMallocInit ){
155386 rc = sqlite3MallocInit();
155387 }
155388 if( rc==SQLITE_OK0 ){
155389 sqlite3GlobalConfigsqlite3Config.isMallocInit = 1;
155390 if( !sqlite3GlobalConfigsqlite3Config.pInitMutex ){
155391 sqlite3GlobalConfigsqlite3Config.pInitMutex =
155392 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE1);
155393 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex && !sqlite3GlobalConfigsqlite3Config.pInitMutex ){
155394 rc = SQLITE_NOMEM_BKPT7;
155395 }
155396 }
155397 }
155398 if( rc==SQLITE_OK0 ){
155399 sqlite3GlobalConfigsqlite3Config.nRefInitMutex++;
155400 }
155401 sqlite3_mutex_leave(pMaster);
155402
155403 /* If rc is not SQLITE_OK at this point, then either the malloc
155404 ** subsystem could not be initialized or the system failed to allocate
155405 ** the pInitMutex mutex. Return an error in either case. */
155406 if( rc!=SQLITE_OK0 ){
155407 return rc;
155408 }
155409
155410 /* Do the rest of the initialization under the recursive mutex so
155411 ** that we will be able to handle recursive calls into
155412 ** sqlite3_initialize(). The recursive calls normally come through
155413 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
155414 ** recursive calls might also be possible.
155415 **
155416 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
155417 ** to the xInit method, so the xInit method need not be threadsafe.
155418 **
155419 ** The following mutex is what serializes access to the appdef pcache xInit
155420 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
155421 ** call to sqlite3PcacheInitialize().
155422 */
155423 sqlite3_mutex_enter(sqlite3GlobalConfigsqlite3Config.pInitMutex);
155424 if( sqlite3GlobalConfigsqlite3Config.isInit==0 && sqlite3GlobalConfigsqlite3Config.inProgress==0 ){
155425 sqlite3GlobalConfigsqlite3Config.inProgress = 1;
155426#ifdef SQLITE_ENABLE_SQLLOG
155427 {
155428 extern void sqlite3_init_sqllog(void);
155429 sqlite3_init_sqllog();
155430 }
155431#endif
155432 memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
155433 sqlite3RegisterBuiltinFunctions();
155434 if( sqlite3GlobalConfigsqlite3Config.isPCacheInit==0 ){
155435 rc = sqlite3PcacheInitialize();
155436 }
155437 if( rc==SQLITE_OK0 ){
155438 sqlite3GlobalConfigsqlite3Config.isPCacheInit = 1;
155439 rc = sqlite3OsInit();
155440 }
155441#ifdef SQLITE_ENABLE_DESERIALIZE
155442 if( rc==SQLITE_OK0 ){
155443 rc = sqlite3MemdbInit();
155444 }
155445#endif
155446 if( rc==SQLITE_OK0 ){
155447 sqlite3PCacheBufferSetup( sqlite3GlobalConfigsqlite3Config.pPage,
155448 sqlite3GlobalConfigsqlite3Config.szPage, sqlite3GlobalConfigsqlite3Config.nPage);
155449 sqlite3GlobalConfigsqlite3Config.isInit = 1;
155450#ifdef SQLITE_EXTRA_INIT
155451 bRunExtraInit = 1;
155452#endif
155453 }
155454 sqlite3GlobalConfigsqlite3Config.inProgress = 0;
155455 }
155456 sqlite3_mutex_leave(sqlite3GlobalConfigsqlite3Config.pInitMutex);
155457
155458 /* Go back under the static mutex and clean up the recursive
155459 ** mutex to prevent a resource leak.
155460 */
155461 sqlite3_mutex_enter(pMaster);
155462 sqlite3GlobalConfigsqlite3Config.nRefInitMutex--;
155463 if( sqlite3GlobalConfigsqlite3Config.nRefInitMutex<=0 ){
155464 assert( sqlite3GlobalConfig.nRefInitMutex==0 )((void) (0));
155465 sqlite3_mutex_free(sqlite3GlobalConfigsqlite3Config.pInitMutex);
155466 sqlite3GlobalConfigsqlite3Config.pInitMutex = 0;
155467 }
155468 sqlite3_mutex_leave(pMaster);
155469
155470 /* The following is just a sanity check to make sure SQLite has
155471 ** been compiled correctly. It is important to run this code, but
155472 ** we don't want to run it too often and soak up CPU cycles for no
155473 ** reason. So we run it once during initialization.
155474 */
155475#ifndef NDEBUG1
155476#ifndef SQLITE_OMIT_FLOATING_POINT
155477 /* This section of code's only "output" is via assert() statements. */
155478 if( rc==SQLITE_OK0 ){
155479 u64 x = (((u64)1)<<63)-1;
155480 double y;
155481 assert(sizeof(x)==8)((void) (0));
155482 assert(sizeof(x)==sizeof(y))((void) (0));
155483 memcpy(&y, &x, 8);
155484 assert( sqlite3IsNaN(y) )((void) (0));
155485 }
155486#endif
155487#endif
155488
155489 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
155490 ** compile-time option.
155491 */
155492#ifdef SQLITE_EXTRA_INIT
155493 if( bRunExtraInit ){
155494 int SQLITE_EXTRA_INIT(const char*);
155495 rc = SQLITE_EXTRA_INIT(0);
155496 }
155497#endif
155498
155499 return rc;
155500}
155501
155502/*
155503** Undo the effects of sqlite3_initialize(). Must not be called while
155504** there are outstanding database connections or memory allocations or
155505** while any part of SQLite is otherwise in use in any thread. This
155506** routine is not threadsafe. But it is safe to invoke this routine
155507** on when SQLite is already shut down. If SQLite is already shut down
155508** when this routine is invoked, then this routine is a harmless no-op.
155509*/
155510SQLITE_API int sqlite3_shutdown(void){
155511#ifdef SQLITE_OMIT_WSD
155512 int rc = sqlite3_wsd_init(4096, 24);
155513 if( rc!=SQLITE_OK0 ){
155514 return rc;
155515 }
155516#endif
155517
155518 if( sqlite3GlobalConfigsqlite3Config.isInit ){
155519#ifdef SQLITE_EXTRA_SHUTDOWN
155520 void SQLITE_EXTRA_SHUTDOWN(void);
155521 SQLITE_EXTRA_SHUTDOWN();
155522#endif
155523 sqlite3_os_end();
155524 sqlite3_reset_auto_extension();
155525 sqlite3GlobalConfigsqlite3Config.isInit = 0;
155526 }
155527 if( sqlite3GlobalConfigsqlite3Config.isPCacheInit ){
155528 sqlite3PcacheShutdown();
155529 sqlite3GlobalConfigsqlite3Config.isPCacheInit = 0;
155530 }
155531 if( sqlite3GlobalConfigsqlite3Config.isMallocInit ){
155532 sqlite3MallocEnd();
155533 sqlite3GlobalConfigsqlite3Config.isMallocInit = 0;
155534
155535#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
155536 /* The heap subsystem has now been shutdown and these values are supposed
155537 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
155538 ** which would rely on that heap subsystem; therefore, make sure these
155539 ** values cannot refer to heap memory that was just invalidated when the
155540 ** heap subsystem was shutdown. This is only done if the current call to
155541 ** this function resulted in the heap subsystem actually being shutdown.
155542 */
155543 sqlite3_data_directory = 0;
155544 sqlite3_temp_directory = 0;
155545#endif
155546 }
155547 if( sqlite3GlobalConfigsqlite3Config.isMutexInit ){
155548 sqlite3MutexEnd();
155549 sqlite3GlobalConfigsqlite3Config.isMutexInit = 0;
155550 }
155551
155552 return SQLITE_OK0;
155553}
155554
155555/*
155556** This API allows applications to modify the global configuration of
155557** the SQLite library at run-time.
155558**
155559** This routine should only be called when there are no outstanding
155560** database connections or memory allocations. This routine is not
155561** threadsafe. Failure to heed these warnings can lead to unpredictable
155562** behavior.
155563*/
155564SQLITE_API int sqlite3_config(int op, ...){
155565 va_list ap;
155566 int rc = SQLITE_OK0;
155567
155568 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
155569 ** the SQLite library is in use. */
155570 if( sqlite3GlobalConfigsqlite3Config.isInit ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(155570);
155571
155572 va_start(ap, op)__builtin_va_start(ap, op);
155573 switch( op ){
155574
155575 /* Mutex configuration options are only available in a threadsafe
155576 ** compile.
155577 */
155578#if defined(SQLITE_THREADSAFE1) && SQLITE_THREADSAFE1>0 /* IMP: R-54466-46756 */
155579 case SQLITE_CONFIG_SINGLETHREAD1: {
155580 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
155581 ** Single-thread. */
155582 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 0; /* Disable mutex on core */
155583 sqlite3GlobalConfigsqlite3Config.bFullMutex = 0; /* Disable mutex on connections */
155584 break;
155585 }
155586#endif
155587#if defined(SQLITE_THREADSAFE1) && SQLITE_THREADSAFE1>0 /* IMP: R-20520-54086 */
155588 case SQLITE_CONFIG_MULTITHREAD2: {
155589 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
155590 ** Multi-thread. */
155591 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 1; /* Enable mutex on core */
155592 sqlite3GlobalConfigsqlite3Config.bFullMutex = 0; /* Disable mutex on connections */
155593 break;
155594 }
155595#endif
155596#if defined(SQLITE_THREADSAFE1) && SQLITE_THREADSAFE1>0 /* IMP: R-59593-21810 */
155597 case SQLITE_CONFIG_SERIALIZED3: {
155598 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
155599 ** Serialized. */
155600 sqlite3GlobalConfigsqlite3Config.bCoreMutex = 1; /* Enable mutex on core */
155601 sqlite3GlobalConfigsqlite3Config.bFullMutex = 1; /* Enable mutex on connections */
155602 break;
155603 }
155604#endif
155605#if defined(SQLITE_THREADSAFE1) && SQLITE_THREADSAFE1>0 /* IMP: R-63666-48755 */
155606 case SQLITE_CONFIG_MUTEX10: {
155607 /* Specify an alternative mutex implementation */
155608 sqlite3GlobalConfigsqlite3Config.mutex = *va_arg(ap, sqlite3_mutex_methods*)__builtin_va_arg(ap, sqlite3_mutex_methods*);
155609 break;
155610 }
155611#endif
155612#if defined(SQLITE_THREADSAFE1) && SQLITE_THREADSAFE1>0 /* IMP: R-14450-37597 */
155613 case SQLITE_CONFIG_GETMUTEX11: {
155614 /* Retrieve the current mutex implementation */
155615 *va_arg(ap, sqlite3_mutex_methods*)__builtin_va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfigsqlite3Config.mutex;
155616 break;
155617 }
155618#endif
155619
155620 case SQLITE_CONFIG_MALLOC4: {
155621 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
155622 ** single argument which is a pointer to an instance of the
155623 ** sqlite3_mem_methods structure. The argument specifies alternative
155624 ** low-level memory allocation routines to be used in place of the memory
155625 ** allocation routines built into SQLite. */
155626 sqlite3GlobalConfigsqlite3Config.m = *va_arg(ap, sqlite3_mem_methods*)__builtin_va_arg(ap, sqlite3_mem_methods*);
155627 break;
155628 }
155629 case SQLITE_CONFIG_GETMALLOC5: {
155630 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
155631 ** single argument which is a pointer to an instance of the
155632 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
155633 ** filled with the currently defined memory allocation routines. */
155634 if( sqlite3GlobalConfigsqlite3Config.m.xMalloc==0 ) sqlite3MemSetDefault();
155635 *va_arg(ap, sqlite3_mem_methods*)__builtin_va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfigsqlite3Config.m;
155636 break;
155637 }
155638 case SQLITE_CONFIG_MEMSTATUS9: {
155639 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
155640 ** single argument of type int, interpreted as a boolean, which enables
155641 ** or disables the collection of memory allocation statistics. */
155642 sqlite3GlobalConfigsqlite3Config.bMemstat = va_arg(ap, int)__builtin_va_arg(ap, int);
155643 break;
155644 }
155645 case SQLITE_CONFIG_SMALL_MALLOC27: {
155646 sqlite3GlobalConfigsqlite3Config.bSmallMalloc = va_arg(ap, int)__builtin_va_arg(ap, int);
155647 break;
155648 }
155649 case SQLITE_CONFIG_PAGECACHE7: {
155650 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
155651 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
155652 ** the size of each page cache line (sz), and the number of cache lines
155653 ** (N). */
155654 sqlite3GlobalConfigsqlite3Config.pPage = va_arg(ap, void*)__builtin_va_arg(ap, void*);
155655 sqlite3GlobalConfigsqlite3Config.szPage = va_arg(ap, int)__builtin_va_arg(ap, int);
155656 sqlite3GlobalConfigsqlite3Config.nPage = va_arg(ap, int)__builtin_va_arg(ap, int);
155657 break;
155658 }
155659 case SQLITE_CONFIG_PCACHE_HDRSZ24: {
155660 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
155661 ** a single parameter which is a pointer to an integer and writes into
155662 ** that integer the number of extra bytes per page required for each page
155663 ** in SQLITE_CONFIG_PAGECACHE. */
155664 *va_arg(ap, int*)__builtin_va_arg(ap, int*) =
155665 sqlite3HeaderSizeBtree() +
155666 sqlite3HeaderSizePcache() +
155667 sqlite3HeaderSizePcache1();
155668 break;
155669 }
155670
155671 case SQLITE_CONFIG_PCACHE14: {
155672 /* no-op */
155673 break;
155674 }
155675 case SQLITE_CONFIG_GETPCACHE15: {
155676 /* now an error */
155677 rc = SQLITE_ERROR1;
155678 break;
155679 }
155680
155681 case SQLITE_CONFIG_PCACHE218: {
155682 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
155683 ** single argument which is a pointer to an sqlite3_pcache_methods2
155684 ** object. This object specifies the interface to a custom page cache
155685 ** implementation. */
155686 sqlite3GlobalConfigsqlite3Config.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*)__builtin_va_arg(ap, sqlite3_pcache_methods2*);
155687 break;
155688 }
155689 case SQLITE_CONFIG_GETPCACHE219: {
155690 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
155691 ** single argument which is a pointer to an sqlite3_pcache_methods2
155692 ** object. SQLite copies of the current page cache implementation into
155693 ** that object. */
155694 if( sqlite3GlobalConfigsqlite3Config.pcache2.xInit==0 ){
155695 sqlite3PCacheSetDefault();
155696 }
155697 *va_arg(ap, sqlite3_pcache_methods2*)__builtin_va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfigsqlite3Config.pcache2;
155698 break;
155699 }
155700
155701/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
155702** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
155703** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
155704#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
155705 case SQLITE_CONFIG_HEAP8: {
155706 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
155707 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
155708 ** number of bytes in the memory buffer, and the minimum allocation size.
155709 */
155710 sqlite3GlobalConfigsqlite3Config.pHeap = va_arg(ap, void*)__builtin_va_arg(ap, void*);
155711 sqlite3GlobalConfigsqlite3Config.nHeap = va_arg(ap, int)__builtin_va_arg(ap, int);
155712 sqlite3GlobalConfigsqlite3Config.mnReq = va_arg(ap, int)__builtin_va_arg(ap, int);
155713
155714 if( sqlite3GlobalConfigsqlite3Config.mnReq<1 ){
155715 sqlite3GlobalConfigsqlite3Config.mnReq = 1;
155716 }else if( sqlite3GlobalConfigsqlite3Config.mnReq>(1<<12) ){
155717 /* cap min request size at 2^12 */
155718 sqlite3GlobalConfigsqlite3Config.mnReq = (1<<12);
155719 }
155720
155721 if( sqlite3GlobalConfigsqlite3Config.pHeap==0 ){
155722 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
155723 ** is NULL, then SQLite reverts to using its default memory allocator
155724 ** (the system malloc() implementation), undoing any prior invocation of
155725 ** SQLITE_CONFIG_MALLOC.
155726 **
155727 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
155728 ** revert to its default implementation when sqlite3_initialize() is run
155729 */
155730 memset(&sqlite3GlobalConfigsqlite3Config.m, 0, sizeof(sqlite3GlobalConfigsqlite3Config.m));
155731 }else{
155732 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
155733 ** alternative memory allocator is engaged to handle all of SQLites
155734 ** memory allocation needs. */
155735#ifdef SQLITE_ENABLE_MEMSYS3
155736 sqlite3GlobalConfigsqlite3Config.m = *sqlite3MemGetMemsys3();
155737#endif
155738#ifdef SQLITE_ENABLE_MEMSYS5
155739 sqlite3GlobalConfigsqlite3Config.m = *sqlite3MemGetMemsys5();
155740#endif
155741 }
155742 break;
155743 }
155744#endif
155745
155746 case SQLITE_CONFIG_LOOKASIDE13: {
155747 sqlite3GlobalConfigsqlite3Config.szLookaside = va_arg(ap, int)__builtin_va_arg(ap, int);
155748 sqlite3GlobalConfigsqlite3Config.nLookaside = va_arg(ap, int)__builtin_va_arg(ap, int);
155749 break;
155750 }
155751
155752 /* Record a pointer to the logger function and its first argument.
155753 ** The default is NULL. Logging is disabled if the function pointer is
155754 ** NULL.
155755 */
155756 case SQLITE_CONFIG_LOG16: {
155757 /* MSVC is picky about pulling func ptrs from va lists.
155758 ** http://support.microsoft.com/kb/47961
155759 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
155760 */
155761 typedef void(*LOGFUNC_t)(void*,int,const char*);
155762 sqlite3GlobalConfigsqlite3Config.xLog = va_arg(ap, LOGFUNC_t)__builtin_va_arg(ap, LOGFUNC_t);
155763 sqlite3GlobalConfigsqlite3Config.pLogArg = va_arg(ap, void*)__builtin_va_arg(ap, void*);
155764 break;
155765 }
155766
155767 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
155768 ** can be changed at start-time using the
155769 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
155770 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
155771 */
155772 case SQLITE_CONFIG_URI17: {
155773 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
155774 ** argument of type int. If non-zero, then URI handling is globally
155775 ** enabled. If the parameter is zero, then URI handling is globally
155776 ** disabled. */
155777 sqlite3GlobalConfigsqlite3Config.bOpenUri = va_arg(ap, int)__builtin_va_arg(ap, int);
155778 break;
155779 }
155780
155781 case SQLITE_CONFIG_COVERING_INDEX_SCAN20: {
155782 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
155783 ** option takes a single integer argument which is interpreted as a
155784 ** boolean in order to enable or disable the use of covering indices for
155785 ** full table scans in the query optimizer. */
155786 sqlite3GlobalConfigsqlite3Config.bUseCis = va_arg(ap, int)__builtin_va_arg(ap, int);
155787 break;
155788 }
155789
155790#ifdef SQLITE_ENABLE_SQLLOG
155791 case SQLITE_CONFIG_SQLLOG21: {
155792 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
155793 sqlite3GlobalConfigsqlite3Config.xSqllog = va_arg(ap, SQLLOGFUNC_t)__builtin_va_arg(ap, SQLLOGFUNC_t);
155794 sqlite3GlobalConfigsqlite3Config.pSqllogArg = va_arg(ap, void *)__builtin_va_arg(ap, void *);
155795 break;
155796 }
155797#endif
155798
155799 case SQLITE_CONFIG_MMAP_SIZE22: {
155800 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
155801 ** integer (sqlite3_int64) values that are the default mmap size limit
155802 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
155803 ** mmap size limit. */
155804 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
155805 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
155806 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
155807 ** negative, then that argument is changed to its compile-time default.
155808 **
155809 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
155810 ** silently truncated if necessary so that it does not exceed the
155811 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
155812 ** compile-time option.
155813 */
155814 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE0x7fff0000 ){
155815 mxMmap = SQLITE_MAX_MMAP_SIZE0x7fff0000;
155816 }
155817 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE0;
155818 if( szMmap>mxMmap) szMmap = mxMmap;
155819 sqlite3GlobalConfigsqlite3Config.mxMmap = mxMmap;
155820 sqlite3GlobalConfigsqlite3Config.szMmap = szMmap;
155821 break;
155822 }
155823
155824#if SQLITE_OS_WIN0 && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
155825 case SQLITE_CONFIG_WIN32_HEAPSIZE23: {
155826 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
155827 ** unsigned integer value that specifies the maximum size of the created
155828 ** heap. */
155829 sqlite3GlobalConfigsqlite3Config.nHeap = va_arg(ap, int)__builtin_va_arg(ap, int);
155830 break;
155831 }
155832#endif
155833
155834 case SQLITE_CONFIG_PMASZ25: {
155835 sqlite3GlobalConfigsqlite3Config.szPma = va_arg(ap, unsigned int)__builtin_va_arg(ap, unsigned int);
155836 break;
155837 }
155838
155839 case SQLITE_CONFIG_STMTJRNL_SPILL26: {
155840 sqlite3GlobalConfigsqlite3Config.nStmtSpill = va_arg(ap, int)__builtin_va_arg(ap, int);
155841 break;
155842 }
155843
155844#ifdef SQLITE_ENABLE_SORTER_REFERENCES
155845 case SQLITE_CONFIG_SORTERREF_SIZE28: {
155846 int iVal = va_arg(ap, int)__builtin_va_arg(ap, int);
155847 if( iVal<0 ){
155848 iVal = SQLITE_DEFAULT_SORTERREF_SIZE0x7fffffff;
155849 }
155850 sqlite3GlobalConfigsqlite3Config.szSorterRef = (u32)iVal;
155851 break;
155852 }
155853#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
155854
155855#ifdef SQLITE_ENABLE_DESERIALIZE
155856 case SQLITE_CONFIG_MEMDB_MAXSIZE29: {
155857 sqlite3GlobalConfigsqlite3Config.mxMemdbSize = va_arg(ap, sqlite3_int64)__builtin_va_arg(ap, sqlite3_int64);
155858 break;
155859 }
155860#endif /* SQLITE_ENABLE_DESERIALIZE */
155861
155862 default: {
155863 rc = SQLITE_ERROR1;
155864 break;
155865 }
155866 }
155867 va_end(ap)__builtin_va_end(ap);
155868 return rc;
155869}
155870
155871/*
155872** Set up the lookaside buffers for a database connection.
155873** Return SQLITE_OK on success.
155874** If lookaside is already active, return SQLITE_BUSY.
155875**
155876** The sz parameter is the number of bytes in each lookaside slot.
155877** The cnt parameter is the number of slots. If pStart is NULL the
155878** space for the lookaside memory is obtained from sqlite3_malloc().
155879** If pStart is not NULL then it is sz*cnt bytes of memory to use for
155880** the lookaside memory.
155881*/
155882static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
155883#ifndef SQLITE_OMIT_LOOKASIDE
155884 void *pStart;
155885
155886 if( sqlite3LookasideUsed(db,0)>0 ){
155887 return SQLITE_BUSY5;
155888 }
155889 /* Free any existing lookaside buffer for this handle before
155890 ** allocating a new one so we don't have to have space for
155891 ** both at the same time.
155892 */
155893 if( db->lookaside.bMalloced ){
155894 sqlite3_free(db->lookaside.pStart);
155895 }
155896 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
155897 ** than a pointer to be useful.
155898 */
155899 sz = ROUNDDOWN8(sz)((sz)&~7); /* IMP: R-33038-09382 */
155900 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
155901 if( cnt<0 ) cnt = 0;
155902 if( sz==0 || cnt==0 ){
155903 sz = 0;
155904 pStart = 0;
155905 }else if( pBuf==0 ){
155906 sqlite3BeginBenignMalloc();
155907 pStart = sqlite3Malloc( sz*(sqlite3_int64)cnt ); /* IMP: R-61949-35727 */
155908 sqlite3EndBenignMalloc();
155909 if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
155910 }else{
155911 pStart = pBuf;
155912 }
155913 db->lookaside.pStart = pStart;
155914 db->lookaside.pInit = 0;
155915 db->lookaside.pFree = 0;
155916 db->lookaside.sz = (u16)sz;
155917 if( pStart ){
155918 int i;
155919 LookasideSlot *p;
155920 assert( sz > (int)sizeof(LookasideSlot*) )((void) (0));
155921 db->lookaside.nSlot = cnt;
155922 p = (LookasideSlot*)pStart;
155923 for(i=cnt-1; i>=0; i--){
155924 p->pNext = db->lookaside.pInit;
155925 db->lookaside.pInit = p;
155926 p = (LookasideSlot*)&((u8*)p)[sz];
155927 }
155928 db->lookaside.pEnd = p;
155929 db->lookaside.bDisable = 0;
155930 db->lookaside.bMalloced = pBuf==0 ?1:0;
155931 }else{
155932 db->lookaside.pStart = db;
155933 db->lookaside.pEnd = db;
155934 db->lookaside.bDisable = 1;
155935 db->lookaside.bMalloced = 0;
155936 db->lookaside.nSlot = 0;
155937 }
155938#endif /* SQLITE_OMIT_LOOKASIDE */
155939 return SQLITE_OK0;
155940}
155941
155942/*
155943** Return the mutex associated with a database connection.
155944*/
155945SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
155946#ifdef SQLITE_ENABLE_API_ARMOR
155947 if( !sqlite3SafetyCheckOk(db) ){
155948 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(155948);
155949 return 0;
155950 }
155951#endif
155952 return db->mutex;
155953}
155954
155955/*
155956** Free up as much memory as we can from the given database
155957** connection.
155958*/
155959SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
155960 int i;
155961
155962#ifdef SQLITE_ENABLE_API_ARMOR
155963 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(155963);
155964#endif
155965 sqlite3_mutex_enter(db->mutex);
155966 sqlite3BtreeEnterAll(db);
155967 for(i=0; i<db->nDb; i++){
155968 Btree *pBt = db->aDb[i].pBt;
155969 if( pBt ){
155970 Pager *pPager = sqlite3BtreePager(pBt);
155971 sqlite3PagerShrink(pPager);
155972 }
155973 }
155974 sqlite3BtreeLeaveAll(db);
155975 sqlite3_mutex_leave(db->mutex);
155976 return SQLITE_OK0;
155977}
155978
155979/*
155980** Flush any dirty pages in the pager-cache for any attached database
155981** to disk.
155982*/
155983SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){
155984 int i;
155985 int rc = SQLITE_OK0;
155986 int bSeenBusy = 0;
155987
155988#ifdef SQLITE_ENABLE_API_ARMOR
155989 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(155989);
155990#endif
155991 sqlite3_mutex_enter(db->mutex);
155992 sqlite3BtreeEnterAll(db);
155993 for(i=0; rc==SQLITE_OK0 && i<db->nDb; i++){
155994 Btree *pBt = db->aDb[i].pBt;
155995 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
155996 Pager *pPager = sqlite3BtreePager(pBt);
155997 rc = sqlite3PagerFlush(pPager);
155998 if( rc==SQLITE_BUSY5 ){
155999 bSeenBusy = 1;
156000 rc = SQLITE_OK0;
156001 }
156002 }
156003 }
156004 sqlite3BtreeLeaveAll(db);
156005 sqlite3_mutex_leave(db->mutex);
156006 return ((rc==SQLITE_OK0 && bSeenBusy) ? SQLITE_BUSY5 : rc);
156007}
156008
156009/*
156010** Configuration settings for an individual database connection
156011*/
156012SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
156013 va_list ap;
156014 int rc;
156015 va_start(ap, op)__builtin_va_start(ap, op);
156016 switch( op ){
156017 case SQLITE_DBCONFIG_MAINDBNAME1000: {
156018 /* IMP: R-06824-28531 */
156019 /* IMP: R-36257-52125 */
156020 db->aDb[0].zDbSName = va_arg(ap,char*)__builtin_va_arg(ap, char*);
156021 rc = SQLITE_OK0;
156022 break;
156023 }
156024 case SQLITE_DBCONFIG_LOOKASIDE1001: {
156025 void *pBuf = va_arg(ap, void*)__builtin_va_arg(ap, void*); /* IMP: R-26835-10964 */
156026 int sz = va_arg(ap, int)__builtin_va_arg(ap, int); /* IMP: R-47871-25994 */
156027 int cnt = va_arg(ap, int)__builtin_va_arg(ap, int); /* IMP: R-04460-53386 */
156028 rc = setupLookaside(db, pBuf, sz, cnt);
156029 break;
156030 }
156031 default: {
156032 static const struct {
156033 int op; /* The opcode */
156034 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
156035 } aFlagOp[] = {
156036 { SQLITE_DBCONFIG_ENABLE_FKEY1002, SQLITE_ForeignKeys0x00004000 },
156037 { SQLITE_DBCONFIG_ENABLE_TRIGGER1003, SQLITE_EnableTrigger0x00040000 },
156038 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004, SQLITE_Fts3Tokenizer0x00400000 },
156039 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION1005, SQLITE_LoadExtension0x00010000 },
156040 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE1006, SQLITE_NoCkptOnClose0x00000800 },
156041 { SQLITE_DBCONFIG_ENABLE_QPSG1007, SQLITE_EnableQPSG0x00800000 },
156042 { SQLITE_DBCONFIG_TRIGGER_EQP1008, SQLITE_TriggerEQP0x01000000 },
156043 { SQLITE_DBCONFIG_RESET_DATABASE1009, SQLITE_ResetDatabase0x02000000 },
156044 { SQLITE_DBCONFIG_DEFENSIVE1010, SQLITE_Defensive0x10000000 },
156045 { SQLITE_DBCONFIG_WRITABLE_SCHEMA1011, SQLITE_WriteSchema0x00000001|
156046 SQLITE_NoSchemaError0x08000000 },
156047 { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE1012, SQLITE_LegacyAlter0x04000000 },
156048 { SQLITE_DBCONFIG_DQS_DDL1014, SQLITE_DqsDDL0x20000000 },
156049 { SQLITE_DBCONFIG_DQS_DML1013, SQLITE_DqsDML0x40000000 },
156050 };
156051 unsigned int i;
156052 rc = SQLITE_ERROR1; /* IMP: R-42790-23372 */
156053 for(i=0; i<ArraySize(aFlagOp)((int)(sizeof(aFlagOp)/sizeof(aFlagOp[0]))); i++){
156054 if( aFlagOp[i].op==op ){
156055 int onoff = va_arg(ap, int)__builtin_va_arg(ap, int);
156056 int *pRes = va_arg(ap, int*)__builtin_va_arg(ap, int*);
156057 u64 oldFlags = db->flags;
156058 if( onoff>0 ){
156059 db->flags |= aFlagOp[i].mask;
156060 }else if( onoff==0 ){
156061 db->flags &= ~(u64)aFlagOp[i].mask;
156062 }
156063 if( oldFlags!=db->flags ){
156064 sqlite3ExpirePreparedStatements(db, 0);
156065 }
156066 if( pRes ){
156067 *pRes = (db->flags & aFlagOp[i].mask)!=0;
156068 }
156069 rc = SQLITE_OK0;
156070 break;
156071 }
156072 }
156073 break;
156074 }
156075 }
156076 va_end(ap)__builtin_va_end(ap);
156077 return rc;
156078}
156079
156080/*
156081** This is the default collating function named "BINARY" which is always
156082** available.
156083*/
156084static int binCollFunc(
156085 void *NotUsed,
156086 int nKey1, const void *pKey1,
156087 int nKey2, const void *pKey2
156088){
156089 int rc, n;
156090 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
156091 n = nKey1<nKey2 ? nKey1 : nKey2;
156092 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
156093 ** strings byte by byte using the memcmp() function from the standard C
156094 ** library. */
156095 assert( pKey1 && pKey2 )((void) (0));
156096 rc = memcmp(pKey1, pKey2, n);
156097 if( rc==0 ){
156098 rc = nKey1 - nKey2;
156099 }
156100 return rc;
156101}
156102
156103/*
156104** This is the collating function named "RTRIM" which is always
156105** available. Ignore trailing spaces.
156106*/
156107static int rtrimCollFunc(
156108 void *pUser,
156109 int nKey1, const void *pKey1,
156110 int nKey2, const void *pKey2
156111){
156112 const u8 *pK1 = (const u8*)pKey1;
156113 const u8 *pK2 = (const u8*)pKey2;
156114 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
156115 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
156116 return binCollFunc(pUser, nKey1, pKey1, nKey2, pKey2);
156117}
156118
156119/*
156120** Return true if CollSeq is the default built-in BINARY.
156121*/
156122SQLITE_PRIVATEstatic int sqlite3IsBinary(const CollSeq *p){
156123 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 )((void) (0));
156124 return p==0 || p->xCmp==binCollFunc;
156125}
156126
156127/*
156128** Another built-in collating sequence: NOCASE.
156129**
156130** This collating sequence is intended to be used for "case independent
156131** comparison". SQLite's knowledge of upper and lower case equivalents
156132** extends only to the 26 characters used in the English language.
156133**
156134** At the moment there is only a UTF-8 implementation.
156135*/
156136static int nocaseCollatingFunc(
156137 void *NotUsed,
156138 int nKey1, const void *pKey1,
156139 int nKey2, const void *pKey2
156140){
156141 int r = sqlite3StrNICmpsqlite3_strnicmp(
156142 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
156143 UNUSED_PARAMETER(NotUsed)(void)(NotUsed);
156144 if( 0==r ){
156145 r = nKey1-nKey2;
156146 }
156147 return r;
156148}
156149
156150/*
156151** Return the ROWID of the most recent insert
156152*/
156153SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
156154#ifdef SQLITE_ENABLE_API_ARMOR
156155 if( !sqlite3SafetyCheckOk(db) ){
156156 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(156156);
156157 return 0;
156158 }
156159#endif
156160 return db->lastRowid;
156161}
156162
156163/*
156164** Set the value returned by the sqlite3_last_insert_rowid() API function.
156165*/
156166SQLITE_API void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
156167#ifdef SQLITE_ENABLE_API_ARMOR
156168 if( !sqlite3SafetyCheckOk(db) ){
156169 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(156169);
156170 return;
156171 }
156172#endif
156173 sqlite3_mutex_enter(db->mutex);
156174 db->lastRowid = iRowid;
156175 sqlite3_mutex_leave(db->mutex);
156176}
156177
156178/*
156179** Return the number of changes in the most recent call to sqlite3_exec().
156180*/
156181SQLITE_API int sqlite3_changes(sqlite3 *db){
156182#ifdef SQLITE_ENABLE_API_ARMOR
156183 if( !sqlite3SafetyCheckOk(db) ){
156184 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(156184);
156185 return 0;
156186 }
156187#endif
156188 return db->nChange;
156189}
156190
156191/*
156192** Return the number of changes since the database handle was opened.
156193*/
156194SQLITE_API int sqlite3_total_changes(sqlite3 *db){
156195#ifdef SQLITE_ENABLE_API_ARMOR
156196 if( !sqlite3SafetyCheckOk(db) ){
156197 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(156197);
156198 return 0;
156199 }
156200#endif
156201 return db->nTotalChange;
156202}
156203
156204/*
156205** Close all open savepoints. This function only manipulates fields of the
156206** database handle object, it does not close any savepoints that may be open
156207** at the b-tree/pager level.
156208*/
156209SQLITE_PRIVATEstatic void sqlite3CloseSavepoints(sqlite3 *db){
156210 while( db->pSavepoint ){
156211 Savepoint *pTmp = db->pSavepoint;
156212 db->pSavepoint = pTmp->pNext;
156213 sqlite3DbFree(db, pTmp);
156214 }
156215 db->nSavepoint = 0;
156216 db->nStatement = 0;
156217 db->isTransactionSavepoint = 0;
156218}
156219
156220/*
156221** Invoke the destructor function associated with FuncDef p, if any. Except,
156222** if this is not the last copy of the function, do not invoke it. Multiple
156223** copies of a single function are created when create_function() is called
156224** with SQLITE_ANY as the encoding.
156225*/
156226static void functionDestroy(sqlite3 *db, FuncDef *p){
156227 FuncDestructor *pDestructor = p->u.pDestructor;
156228 if( pDestructor ){
156229 pDestructor->nRef--;
156230 if( pDestructor->nRef==0 ){
156231 pDestructor->xDestroy(pDestructor->pUserData);
156232 sqlite3DbFree(db, pDestructor);
156233 }
156234 }
156235}
156236
156237/*
156238** Disconnect all sqlite3_vtab objects that belong to database connection
156239** db. This is called when db is being closed.
156240*/
156241static void disconnectAllVtab(sqlite3 *db){
156242#ifndef SQLITE_OMIT_VIRTUALTABLE
156243 int i;
156244 HashElem *p;
156245 sqlite3BtreeEnterAll(db);
156246 for(i=0; i<db->nDb; i++){
156247 Schema *pSchema = db->aDb[i].pSchema;
156248 if( pSchema ){
156249 for(p=sqliteHashFirst(&pSchema->tblHash)((&pSchema->tblHash)->first); p; p=sqliteHashNext(p)((p)->next)){
156250 Table *pTab = (Table *)sqliteHashData(p)((p)->data);
156251 if( IsVirtual(pTab)((pTab)->nModuleArg) ) sqlite3VtabDisconnect(db, pTab);
156252 }
156253 }
156254 }
156255 for(p=sqliteHashFirst(&db->aModule)((&db->aModule)->first); p; p=sqliteHashNext(p)((p)->next)){
156256 Module *pMod = (Module *)sqliteHashData(p)((p)->data);
156257 if( pMod->pEpoTab ){
156258 sqlite3VtabDisconnect(db, pMod->pEpoTab);
156259 }
156260 }
156261 sqlite3VtabUnlockList(db);
156262 sqlite3BtreeLeaveAll(db);
156263#else
156264 UNUSED_PARAMETER(db)(void)(db);
156265#endif
156266}
156267
156268/*
156269** Return TRUE if database connection db has unfinalized prepared
156270** statements or unfinished sqlite3_backup objects.
156271*/
156272static int connectionIsBusy(sqlite3 *db){
156273 int j;
156274 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
156275 if( db->pVdbe ) return 1;
156276 for(j=0; j<db->nDb; j++){
156277 Btree *pBt = db->aDb[j].pBt;
156278 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
156279 }
156280 return 0;
156281}
156282
156283/*
156284** Close an existing SQLite database
156285*/
156286static int sqlite3Close(sqlite3 *db, int forceZombie){
156287 if( !db ){
156288 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
156289 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
156290 return SQLITE_OK0;
156291 }
156292 if( !sqlite3SafetyCheckSickOrOk(db) ){
156293 return SQLITE_MISUSE_BKPTsqlite3MisuseError(156293);
156294 }
156295 sqlite3_mutex_enter(db->mutex);
156296 if( db->mTrace & SQLITE_TRACE_CLOSE0x08 ){
156297 db->xTrace(SQLITE_TRACE_CLOSE0x08, db->pTraceArg, db, 0);
156298 }
156299
156300 /* Force xDisconnect calls on all virtual tables */
156301 disconnectAllVtab(db);
156302
156303 /* If a transaction is open, the disconnectAllVtab() call above
156304 ** will not have called the xDisconnect() method on any virtual
156305 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
156306 ** call will do so. We need to do this before the check for active
156307 ** SQL statements below, as the v-table implementation may be storing
156308 ** some prepared statements internally.
156309 */
156310 sqlite3VtabRollback(db);
156311
156312 /* Legacy behavior (sqlite3_close() behavior) is to return
156313 ** SQLITE_BUSY if the connection can not be closed immediately.
156314 */
156315 if( !forceZombie && connectionIsBusy(db) ){
156316 sqlite3ErrorWithMsg(db, SQLITE_BUSY5, "unable to close due to unfinalized "
156317 "statements or unfinished backups");
156318 sqlite3_mutex_leave(db->mutex);
156319 return SQLITE_BUSY5;
156320 }
156321
156322#ifdef SQLITE_ENABLE_SQLLOG
156323 if( sqlite3GlobalConfigsqlite3Config.xSqllog ){
156324 /* Closing the handle. Fourth parameter is passed the value 2. */
156325 sqlite3GlobalConfigsqlite3Config.xSqllog(sqlite3GlobalConfigsqlite3Config.pSqllogArg, db, 0, 2);
156326 }
156327#endif
156328
156329 /* Convert the connection into a zombie and then close it.
156330 */
156331 db->magic = SQLITE_MAGIC_ZOMBIE0x64cffc7f;
156332 sqlite3LeaveMutexAndCloseZombie(db);
156333 return SQLITE_OK0;
156334}
156335
156336/*
156337** Two variations on the public interface for closing a database
156338** connection. The sqlite3_close() version returns SQLITE_BUSY and
156339** leaves the connection option if there are unfinalized prepared
156340** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
156341** version forces the connection to become a zombie if there are
156342** unclosed resources, and arranges for deallocation when the last
156343** prepare statement or sqlite3_backup closes.
156344*/
156345SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
156346SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
156347
156348
156349/*
156350** Close the mutex on database connection db.
156351**
156352** Furthermore, if database connection db is a zombie (meaning that there
156353** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
156354** every sqlite3_stmt has now been finalized and every sqlite3_backup has
156355** finished, then free all resources.
156356*/
156357SQLITE_PRIVATEstatic void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
156358 HashElem *i; /* Hash table iterator */
156359 int j;
156360
156361 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
156362 ** or if the connection has not yet been closed by sqlite3_close_v2(),
156363 ** then just leave the mutex and return.
156364 */
156365 if( db->magic!=SQLITE_MAGIC_ZOMBIE0x64cffc7f || connectionIsBusy(db) ){
156366 sqlite3_mutex_leave(db->mutex);
156367 return;
156368 }
156369
156370 /* If we reach this point, it means that the database connection has
156371 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
156372 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
156373 ** go ahead and free all resources.
156374 */
156375
156376 /* If a transaction is open, roll it back. This also ensures that if
156377 ** any database schemas have been modified by an uncommitted transaction
156378 ** they are reset. And that the required b-tree mutex is held to make
156379 ** the pager rollback and schema reset an atomic operation. */
156380 sqlite3RollbackAll(db, SQLITE_OK0);
156381
156382 /* Free any outstanding Savepoint structures. */
156383 sqlite3CloseSavepoints(db);
156384
156385 /* Close all database connections */
156386 for(j=0; j<db->nDb; j++){
156387 struct Db *pDb = &db->aDb[j];
156388 if( pDb->pBt ){
156389 sqlite3BtreeClose(pDb->pBt);
156390 pDb->pBt = 0;
156391 if( j!=1 ){
156392 pDb->pSchema = 0;
156393 }
156394 }
156395 }
156396 /* Clear the TEMP schema separately and last */
156397 if( db->aDb[1].pSchema ){
156398 sqlite3SchemaClear(db->aDb[1].pSchema);
156399 }
156400 sqlite3VtabUnlockList(db);
156401
156402 /* Free up the array of auxiliary databases */
156403 sqlite3CollapseDatabaseArray(db);
156404 assert( db->nDb<=2 )((void) (0));
156405 assert( db->aDb==db->aDbStatic )((void) (0));
156406
156407 /* Tell the code in notify.c that the connection no longer holds any
156408 ** locks and does not require any further unlock-notify callbacks.
156409 */
156410 sqlite3ConnectionClosed(db);
156411
156412 for(i=sqliteHashFirst(&db->aFunc)((&db->aFunc)->first); i; i=sqliteHashNext(i)((i)->next)){
156413 FuncDef *pNext, *p;
156414 p = sqliteHashData(i)((i)->data);
156415 do{
156416 functionDestroy(db, p);
156417 pNext = p->pNext;
156418 sqlite3DbFree(db, p);
156419 p = pNext;
156420 }while( p );
156421 }
156422 sqlite3HashClear(&db->aFunc);
156423 for(i=sqliteHashFirst(&db->aCollSeq)((&db->aCollSeq)->first); i; i=sqliteHashNext(i)((i)->next)){
156424 CollSeq *pColl = (CollSeq *)sqliteHashData(i)((i)->data);
156425 /* Invoke any destructors registered for collation sequence user data. */
156426 for(j=0; j<3; j++){
156427 if( pColl[j].xDel ){
156428 pColl[j].xDel(pColl[j].pUser);
156429 }
156430 }
156431 sqlite3DbFree(db, pColl);
156432 }
156433 sqlite3HashClear(&db->aCollSeq);
156434#ifndef SQLITE_OMIT_VIRTUALTABLE
156435 for(i=sqliteHashFirst(&db->aModule)((&db->aModule)->first); i; i=sqliteHashNext(i)((i)->next)){
156436 Module *pMod = (Module *)sqliteHashData(i)((i)->data);
156437 if( pMod->xDestroy ){
156438 pMod->xDestroy(pMod->pAux);
156439 }
156440 sqlite3VtabEponymousTableClear(db, pMod);
156441 sqlite3DbFree(db, pMod);
156442 }
156443 sqlite3HashClear(&db->aModule);
156444#endif
156445
156446 sqlite3Error(db, SQLITE_OK0); /* Deallocates any cached error strings. */
156447 sqlite3ValueFree(db->pErr);
156448 sqlite3CloseExtensions(db);
156449#if SQLITE_USER_AUTHENTICATION
156450 sqlite3_free(db->auth.zAuthUser);
156451 sqlite3_free(db->auth.zAuthPW);
156452#endif
156453
156454 db->magic = SQLITE_MAGIC_ERROR0xb5357930;
156455
156456 /* The temp-database schema is allocated differently from the other schema
156457 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
156458 ** So it needs to be freed here. Todo: Why not roll the temp schema into
156459 ** the same sqliteMalloc() as the one that allocates the database
156460 ** structure?
156461 */
156462 sqlite3DbFree(db, db->aDb[1].pSchema);
156463 sqlite3_mutex_leave(db->mutex);
156464 db->magic = SQLITE_MAGIC_CLOSED0x9f3c2d33;
156465 sqlite3_mutex_free(db->mutex);
156466 assert( sqlite3LookasideUsed(db,0)==0 )((void) (0));
156467 if( db->lookaside.bMalloced ){
156468 sqlite3_free(db->lookaside.pStart);
156469 }
156470 sqlite3_free(db);
156471}
156472
156473/*
156474** Rollback all database files. If tripCode is not SQLITE_OK, then
156475** any write cursors are invalidated ("tripped" - as in "tripping a circuit
156476** breaker") and made to return tripCode if there are any further
156477** attempts to use that cursor. Read cursors remain open and valid
156478** but are "saved" in case the table pages are moved around.
156479*/
156480SQLITE_PRIVATEstatic void sqlite3RollbackAll(sqlite3 *db, int tripCode){
156481 int i;
156482 int inTrans = 0;
156483 int schemaChange;
156484 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
156485 sqlite3BeginBenignMalloc();
156486
156487 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
156488 ** This is important in case the transaction being rolled back has
156489 ** modified the database schema. If the b-tree mutexes are not taken
156490 ** here, then another shared-cache connection might sneak in between
156491 ** the database rollback and schema reset, which can cause false
156492 ** corruption reports in some cases. */
156493 sqlite3BtreeEnterAll(db);
156494 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange0x0001)!=0 && db->init.busy==0;
156495
156496 for(i=0; i<db->nDb; i++){
156497 Btree *p = db->aDb[i].pBt;
156498 if( p ){
156499 if( sqlite3BtreeIsInTrans(p) ){
156500 inTrans = 1;
156501 }
156502 sqlite3BtreeRollback(p, tripCode, !schemaChange);
156503 }
156504 }
156505 sqlite3VtabRollback(db);
156506 sqlite3EndBenignMalloc();
156507
156508 if( schemaChange ){
156509 sqlite3ExpirePreparedStatements(db, 0);
156510 sqlite3ResetAllSchemasOfConnection(db);
156511 }
156512 sqlite3BtreeLeaveAll(db);
156513
156514 /* Any deferred constraint violations have now been resolved. */
156515 db->nDeferredCons = 0;
156516 db->nDeferredImmCons = 0;
156517 db->flags &= ~(u64)SQLITE_DeferFKs0x00080000;
156518
156519 /* If one has been configured, invoke the rollback-hook callback */
156520 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
156521 db->xRollbackCallback(db->pRollbackArg);
156522 }
156523}
156524
156525/*
156526** Return a static string containing the name corresponding to the error code
156527** specified in the argument.
156528*/
156529#if defined(SQLITE_NEED_ERR_NAME)
156530SQLITE_PRIVATEstatic const char *sqlite3ErrName(int rc){
156531 const char *zName = 0;
156532 int i, origRc = rc;
156533 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
156534 switch( rc ){
156535 case SQLITE_OK0: zName = "SQLITE_OK"; break;
156536 case SQLITE_ERROR1: zName = "SQLITE_ERROR"; break;
156537 case SQLITE_ERROR_SNAPSHOT(1 | (3<<8)): zName = "SQLITE_ERROR_SNAPSHOT"; break;
156538 case SQLITE_INTERNAL2: zName = "SQLITE_INTERNAL"; break;
156539 case SQLITE_PERM3: zName = "SQLITE_PERM"; break;
156540 case SQLITE_ABORT4: zName = "SQLITE_ABORT"; break;
156541 case SQLITE_ABORT_ROLLBACK(4 | (2<<8)): zName = "SQLITE_ABORT_ROLLBACK"; break;
156542 case SQLITE_BUSY5: zName = "SQLITE_BUSY"; break;
156543 case SQLITE_BUSY_RECOVERY(5 | (1<<8)): zName = "SQLITE_BUSY_RECOVERY"; break;
156544 case SQLITE_BUSY_SNAPSHOT(5 | (2<<8)): zName = "SQLITE_BUSY_SNAPSHOT"; break;
156545 case SQLITE_LOCKED6: zName = "SQLITE_LOCKED"; break;
156546 case SQLITE_LOCKED_SHAREDCACHE(6 | (1<<8)): zName = "SQLITE_LOCKED_SHAREDCACHE";break;
156547 case SQLITE_NOMEM7: zName = "SQLITE_NOMEM"; break;
156548 case SQLITE_READONLY8: zName = "SQLITE_READONLY"; break;
156549 case SQLITE_READONLY_RECOVERY(8 | (1<<8)): zName = "SQLITE_READONLY_RECOVERY"; break;
156550 case SQLITE_READONLY_CANTINIT(8 | (5<<8)): zName = "SQLITE_READONLY_CANTINIT"; break;
156551 case SQLITE_READONLY_ROLLBACK(8 | (3<<8)): zName = "SQLITE_READONLY_ROLLBACK"; break;
156552 case SQLITE_READONLY_DBMOVED(8 | (4<<8)): zName = "SQLITE_READONLY_DBMOVED"; break;
156553 case SQLITE_READONLY_DIRECTORY(8 | (6<<8)): zName = "SQLITE_READONLY_DIRECTORY";break;
156554 case SQLITE_INTERRUPT9: zName = "SQLITE_INTERRUPT"; break;
156555 case SQLITE_IOERR10: zName = "SQLITE_IOERR"; break;
156556 case SQLITE_IOERR_READ(10 | (1<<8)): zName = "SQLITE_IOERR_READ"; break;
156557 case SQLITE_IOERR_SHORT_READ(10 | (2<<8)): zName = "SQLITE_IOERR_SHORT_READ"; break;
156558 case SQLITE_IOERR_WRITE(10 | (3<<8)): zName = "SQLITE_IOERR_WRITE"; break;
156559 case SQLITE_IOERR_FSYNC(10 | (4<<8)): zName = "SQLITE_IOERR_FSYNC"; break;
156560 case SQLITE_IOERR_DIR_FSYNC(10 | (5<<8)): zName = "SQLITE_IOERR_DIR_FSYNC"; break;
156561 case SQLITE_IOERR_TRUNCATE(10 | (6<<8)): zName = "SQLITE_IOERR_TRUNCATE"; break;
156562 case SQLITE_IOERR_FSTAT(10 | (7<<8)): zName = "SQLITE_IOERR_FSTAT"; break;
156563 case SQLITE_IOERR_UNLOCK(10 | (8<<8)): zName = "SQLITE_IOERR_UNLOCK"; break;
156564 case SQLITE_IOERR_RDLOCK(10 | (9<<8)): zName = "SQLITE_IOERR_RDLOCK"; break;
156565 case SQLITE_IOERR_DELETE(10 | (10<<8)): zName = "SQLITE_IOERR_DELETE"; break;
156566 case SQLITE_IOERR_NOMEM(10 | (12<<8)): zName = "SQLITE_IOERR_NOMEM"; break;
156567 case SQLITE_IOERR_ACCESS(10 | (13<<8)): zName = "SQLITE_IOERR_ACCESS"; break;
156568 case SQLITE_IOERR_CHECKRESERVEDLOCK(10 | (14<<8)):
156569 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
156570 case SQLITE_IOERR_LOCK(10 | (15<<8)): zName = "SQLITE_IOERR_LOCK"; break;
156571 case SQLITE_IOERR_CLOSE(10 | (16<<8)): zName = "SQLITE_IOERR_CLOSE"; break;
156572 case SQLITE_IOERR_DIR_CLOSE(10 | (17<<8)): zName = "SQLITE_IOERR_DIR_CLOSE"; break;
156573 case SQLITE_IOERR_SHMOPEN(10 | (18<<8)): zName = "SQLITE_IOERR_SHMOPEN"; break;
156574 case SQLITE_IOERR_SHMSIZE(10 | (19<<8)): zName = "SQLITE_IOERR_SHMSIZE"; break;
156575 case SQLITE_IOERR_SHMLOCK(10 | (20<<8)): zName = "SQLITE_IOERR_SHMLOCK"; break;
156576 case SQLITE_IOERR_SHMMAP(10 | (21<<8)): zName = "SQLITE_IOERR_SHMMAP"; break;
156577 case SQLITE_IOERR_SEEK(10 | (22<<8)): zName = "SQLITE_IOERR_SEEK"; break;
156578 case SQLITE_IOERR_DELETE_NOENT(10 | (23<<8)): zName = "SQLITE_IOERR_DELETE_NOENT";break;
156579 case SQLITE_IOERR_MMAP(10 | (24<<8)): zName = "SQLITE_IOERR_MMAP"; break;
156580 case SQLITE_IOERR_GETTEMPPATH(10 | (25<<8)): zName = "SQLITE_IOERR_GETTEMPPATH"; break;
156581 case SQLITE_IOERR_CONVPATH(10 | (26<<8)): zName = "SQLITE_IOERR_CONVPATH"; break;
156582 case SQLITE_CORRUPT11: zName = "SQLITE_CORRUPT"; break;
156583 case SQLITE_CORRUPT_VTAB(11 | (1<<8)): zName = "SQLITE_CORRUPT_VTAB"; break;
156584 case SQLITE_NOTFOUND12: zName = "SQLITE_NOTFOUND"; break;
156585 case SQLITE_FULL13: zName = "SQLITE_FULL"; break;
156586 case SQLITE_CANTOPEN14: zName = "SQLITE_CANTOPEN"; break;
156587 case SQLITE_CANTOPEN_NOTEMPDIR(14 | (1<<8)): zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
156588 case SQLITE_CANTOPEN_ISDIR(14 | (2<<8)): zName = "SQLITE_CANTOPEN_ISDIR"; break;
156589 case SQLITE_CANTOPEN_FULLPATH(14 | (3<<8)): zName = "SQLITE_CANTOPEN_FULLPATH"; break;
156590 case SQLITE_CANTOPEN_CONVPATH(14 | (4<<8)): zName = "SQLITE_CANTOPEN_CONVPATH"; break;
156591 case SQLITE_PROTOCOL15: zName = "SQLITE_PROTOCOL"; break;
156592 case SQLITE_EMPTY16: zName = "SQLITE_EMPTY"; break;
156593 case SQLITE_SCHEMA17: zName = "SQLITE_SCHEMA"; break;
156594 case SQLITE_TOOBIG18: zName = "SQLITE_TOOBIG"; break;
156595 case SQLITE_CONSTRAINT19: zName = "SQLITE_CONSTRAINT"; break;
156596 case SQLITE_CONSTRAINT_UNIQUE(19 | (8<<8)): zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
156597 case SQLITE_CONSTRAINT_TRIGGER(19 | (7<<8)): zName = "SQLITE_CONSTRAINT_TRIGGER";break;
156598 case SQLITE_CONSTRAINT_FOREIGNKEY(19 | (3<<8)):
156599 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
156600 case SQLITE_CONSTRAINT_CHECK(19 | (1<<8)): zName = "SQLITE_CONSTRAINT_CHECK"; break;
156601 case SQLITE_CONSTRAINT_PRIMARYKEY(19 | (6<<8)):
156602 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
156603 case SQLITE_CONSTRAINT_NOTNULL(19 | (5<<8)): zName = "SQLITE_CONSTRAINT_NOTNULL";break;
156604 case SQLITE_CONSTRAINT_COMMITHOOK(19 | (2<<8)):
156605 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
156606 case SQLITE_CONSTRAINT_VTAB(19 | (9<<8)): zName = "SQLITE_CONSTRAINT_VTAB"; break;
156607 case SQLITE_CONSTRAINT_FUNCTION(19 | (4<<8)):
156608 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
156609 case SQLITE_CONSTRAINT_ROWID(19 |(10<<8)): zName = "SQLITE_CONSTRAINT_ROWID"; break;
156610 case SQLITE_MISMATCH20: zName = "SQLITE_MISMATCH"; break;
156611 case SQLITE_MISUSE21: zName = "SQLITE_MISUSE"; break;
156612 case SQLITE_NOLFS22: zName = "SQLITE_NOLFS"; break;
156613 case SQLITE_AUTH23: zName = "SQLITE_AUTH"; break;
156614 case SQLITE_FORMAT24: zName = "SQLITE_FORMAT"; break;
156615 case SQLITE_RANGE25: zName = "SQLITE_RANGE"; break;
156616 case SQLITE_NOTADB26: zName = "SQLITE_NOTADB"; break;
156617 case SQLITE_ROW100: zName = "SQLITE_ROW"; break;
156618 case SQLITE_NOTICE27: zName = "SQLITE_NOTICE"; break;
156619 case SQLITE_NOTICE_RECOVER_WAL(27 | (1<<8)): zName = "SQLITE_NOTICE_RECOVER_WAL";break;
156620 case SQLITE_NOTICE_RECOVER_ROLLBACK(27 | (2<<8)):
156621 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
156622 case SQLITE_WARNING28: zName = "SQLITE_WARNING"; break;
156623 case SQLITE_WARNING_AUTOINDEX(28 | (1<<8)): zName = "SQLITE_WARNING_AUTOINDEX"; break;
156624 case SQLITE_DONE101: zName = "SQLITE_DONE"; break;
156625 }
156626 }
156627 if( zName==0 ){
156628 static char zBuf[50];
156629 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
156630 zName = zBuf;
156631 }
156632 return zName;
156633}
156634#endif
156635
156636/*
156637** Return a static string that describes the kind of error specified in the
156638** argument.
156639*/
156640SQLITE_PRIVATEstatic const char *sqlite3ErrStr(int rc){
156641 static const char* const aMsg[] = {
156642 /* SQLITE_OK */ "not an error",
156643 /* SQLITE_ERROR */ "SQL logic error",
156644 /* SQLITE_INTERNAL */ 0,
156645 /* SQLITE_PERM */ "access permission denied",
156646 /* SQLITE_ABORT */ "query aborted",
156647 /* SQLITE_BUSY */ "database is locked",
156648 /* SQLITE_LOCKED */ "database table is locked",
156649 /* SQLITE_NOMEM */ "out of memory",
156650 /* SQLITE_READONLY */ "attempt to write a readonly database",
156651 /* SQLITE_INTERRUPT */ "interrupted",
156652 /* SQLITE_IOERR */ "disk I/O error",
156653 /* SQLITE_CORRUPT */ "database disk image is malformed",
156654 /* SQLITE_NOTFOUND */ "unknown operation",
156655 /* SQLITE_FULL */ "database or disk is full",
156656 /* SQLITE_CANTOPEN */ "unable to open database file",
156657 /* SQLITE_PROTOCOL */ "locking protocol",
156658 /* SQLITE_EMPTY */ 0,
156659 /* SQLITE_SCHEMA */ "database schema has changed",
156660 /* SQLITE_TOOBIG */ "string or blob too big",
156661 /* SQLITE_CONSTRAINT */ "constraint failed",
156662 /* SQLITE_MISMATCH */ "datatype mismatch",
156663 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
156664#ifdef SQLITE_DISABLE_LFS
156665 /* SQLITE_NOLFS */ "large file support is disabled",
156666#else
156667 /* SQLITE_NOLFS */ 0,
156668#endif
156669 /* SQLITE_AUTH */ "authorization denied",
156670 /* SQLITE_FORMAT */ 0,
156671 /* SQLITE_RANGE */ "column index out of range",
156672 /* SQLITE_NOTADB */ "file is not a database",
156673 /* SQLITE_NOTICE */ "notification message",
156674 /* SQLITE_WARNING */ "warning message",
156675 };
156676 const char *zErr = "unknown error";
156677 switch( rc ){
156678 case SQLITE_ABORT_ROLLBACK(4 | (2<<8)): {
156679 zErr = "abort due to ROLLBACK";
156680 break;
156681 }
156682 case SQLITE_ROW100: {
156683 zErr = "another row available";
156684 break;
156685 }
156686 case SQLITE_DONE101: {
156687 zErr = "no more rows available";
156688 break;
156689 }
156690 default: {
156691 rc &= 0xff;
156692 if( ALWAYS(rc>=0)(rc>=0) && rc<ArraySize(aMsg)((int)(sizeof(aMsg)/sizeof(aMsg[0]))) && aMsg[rc]!=0 ){
156693 zErr = aMsg[rc];
156694 }
156695 break;
156696 }
156697 }
156698 return zErr;
156699}
156700
156701/*
156702** This routine implements a busy callback that sleeps and tries
156703** again until a timeout value is reached. The timeout value is
156704** an integer number of milliseconds passed in as the first
156705** argument.
156706**
156707** Return non-zero to retry the lock. Return zero to stop trying
156708** and cause SQLite to return SQLITE_BUSY.
156709*/
156710static int sqliteDefaultBusyCallback(
156711 void *ptr, /* Database connection */
156712 int count, /* Number of times table has been busy */
156713 sqlite3_file *pFile /* The file on which the lock occurred */
156714){
156715#if SQLITE_OS_WIN0 || HAVE_USLEEP
156716 /* This case is for systems that have support for sleeping for fractions of
156717 ** a second. Examples: All windows systems, unix systems with usleep() */
156718 static const u8 delays[] =
156719 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
156720 static const u8 totals[] =
156721 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
156722# define NDELAY ArraySize(delays)((int)(sizeof(delays)/sizeof(delays[0])))
156723 sqlite3 *db = (sqlite3 *)ptr;
156724 int tmout = db->busyTimeout;
156725 int delay, prior;
156726
156727#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
156728 if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT34,&tmout)==SQLITE_OK0 ){
156729 if( count ){
156730 tmout = 0;
156731 sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT34, &tmout);
156732 return 0;
156733 }else{
156734 return 1;
156735 }
156736 }
156737#else
156738 UNUSED_PARAMETER(pFile)(void)(pFile);
156739#endif
156740 assert( count>=0 )((void) (0));
156741 if( count < NDELAY ){
156742 delay = delays[count];
156743 prior = totals[count];
156744 }else{
156745 delay = delays[NDELAY-1];
156746 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
156747 }
156748 if( prior + delay > tmout ){
156749 delay = tmout - prior;
156750 if( delay<=0 ) return 0;
156751 }
156752 sqlite3OsSleep(db->pVfs, delay*1000);
156753 return 1;
156754#else
156755 /* This case for unix systems that lack usleep() support. Sleeping
156756 ** must be done in increments of whole seconds */
156757 sqlite3 *db = (sqlite3 *)ptr;
156758 int tmout = ((sqlite3 *)ptr)->busyTimeout;
156759 UNUSED_PARAMETER(pFile)(void)(pFile);
156760 if( (count+1)*1000 > tmout ){
156761 return 0;
156762 }
156763 sqlite3OsSleep(db->pVfs, 1000000);
156764 return 1;
156765#endif
156766}
156767
156768/*
156769** Invoke the given busy handler.
156770**
156771** This routine is called when an operation failed to acquire a
156772** lock on VFS file pFile.
156773**
156774** If this routine returns non-zero, the lock is retried. If it
156775** returns 0, the operation aborts with an SQLITE_BUSY error.
156776*/
156777SQLITE_PRIVATEstatic int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){
156778 int rc;
156779 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
156780 if( p->bExtraFileArg ){
156781 /* Add an extra parameter with the pFile pointer to the end of the
156782 ** callback argument list */
156783 int (*xTra)(void*,int,sqlite3_file*);
156784 xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler;
156785 rc = xTra(p->pBusyArg, p->nBusy, pFile);
156786 }else{
156787 /* Legacy style busy handler callback */
156788 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
156789 }
156790 if( rc==0 ){
156791 p->nBusy = -1;
156792 }else{
156793 p->nBusy++;
156794 }
156795 return rc;
156796}
156797
156798/*
156799** This routine sets the busy callback for an Sqlite database to the
156800** given callback function with the given argument.
156801*/
156802SQLITE_API int sqlite3_busy_handler(
156803 sqlite3 *db,
156804 int (*xBusy)(void*,int),
156805 void *pArg
156806){
156807#ifdef SQLITE_ENABLE_API_ARMOR
156808 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(156808);
156809#endif
156810 sqlite3_mutex_enter(db->mutex);
156811 db->busyHandler.xBusyHandler = xBusy;
156812 db->busyHandler.pBusyArg = pArg;
156813 db->busyHandler.nBusy = 0;
156814 db->busyHandler.bExtraFileArg = 0;
156815 db->busyTimeout = 0;
156816 sqlite3_mutex_leave(db->mutex);
156817 return SQLITE_OK0;
156818}
156819
156820#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
156821/*
156822** This routine sets the progress callback for an Sqlite database to the
156823** given callback function with the given argument. The progress callback will
156824** be invoked every nOps opcodes.
156825*/
156826SQLITE_API void sqlite3_progress_handler(
156827 sqlite3 *db,
156828 int nOps,
156829 int (*xProgress)(void*),
156830 void *pArg
156831){
156832#ifdef SQLITE_ENABLE_API_ARMOR
156833 if( !sqlite3SafetyCheckOk(db) ){
156834 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(156834);
156835 return;
156836 }
156837#endif
156838 sqlite3_mutex_enter(db->mutex);
156839 if( nOps>0 ){
156840 db->xProgress = xProgress;
156841 db->nProgressOps = (unsigned)nOps;
156842 db->pProgressArg = pArg;
156843 }else{
156844 db->xProgress = 0;
156845 db->nProgressOps = 0;
156846 db->pProgressArg = 0;
156847 }
156848 sqlite3_mutex_leave(db->mutex);
156849}
156850#endif
156851
156852
156853/*
156854** This routine installs a default busy handler that waits for the
156855** specified number of milliseconds before returning 0.
156856*/
156857SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
156858#ifdef SQLITE_ENABLE_API_ARMOR
156859 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(156859);
156860#endif
156861 if( ms>0 ){
156862 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
156863 (void*)db);
156864 db->busyTimeout = ms;
156865 db->busyHandler.bExtraFileArg = 1;
156866 }else{
156867 sqlite3_busy_handler(db, 0, 0);
156868 }
156869 return SQLITE_OK0;
156870}
156871
156872/*
156873** Cause any pending operation to stop at its earliest opportunity.
156874*/
156875SQLITE_API void sqlite3_interrupt(sqlite3 *db){
156876#ifdef SQLITE_ENABLE_API_ARMOR
156877 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE0x64cffc7f) ){
156878 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(156878);
156879 return;
156880 }
156881#endif
156882 db->u1.isInterrupted = 1;
156883}
156884
156885
156886/*
156887** This function is exactly the same as sqlite3_create_function(), except
156888** that it is designed to be called by internal code. The difference is
156889** that if a malloc() fails in sqlite3_create_function(), an error code
156890** is returned and the mallocFailed flag cleared.
156891*/
156892SQLITE_PRIVATEstatic int sqlite3CreateFunc(
156893 sqlite3 *db,
156894 const char *zFunctionName,
156895 int nArg,
156896 int enc,
156897 void *pUserData,
156898 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
156899 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
156900 void (*xFinal)(sqlite3_context*),
156901 void (*xValue)(sqlite3_context*),
156902 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
156903 FuncDestructor *pDestructor
156904){
156905 FuncDef *p;
156906 int nName;
156907 int extraFlags;
156908
156909 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
156910 assert( xValue==0 || xSFunc==0 )((void) (0));
156911 if( zFunctionName==0 /* Must have a valid name */
156912 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
156913 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
156914 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
156915 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG127)
156916 || (255<(nName = sqlite3Strlen30( zFunctionName)))
156917 ){
156918 return SQLITE_MISUSE_BKPTsqlite3MisuseError(156918);
156919 }
156920
156921 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC )((void) (0));
156922 extraFlags = enc & SQLITE_DETERMINISTIC0x800;
156923 enc &= (SQLITE_FUNC_ENCMASK0x0003|SQLITE_ANY5);
156924
156925#ifndef SQLITE_OMIT_UTF16
156926 /* If SQLITE_UTF16 is specified as the encoding type, transform this
156927 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
156928 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
156929 **
156930 ** If SQLITE_ANY is specified, add three versions of the function
156931 ** to the hash table.
156932 */
156933 if( enc==SQLITE_UTF164 ){
156934 enc = SQLITE_UTF16NATIVE2;
156935 }else if( enc==SQLITE_ANY5 ){
156936 int rc;
156937 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF81|extraFlags,
156938 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
156939 if( rc==SQLITE_OK0 ){
156940 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE2|extraFlags,
156941 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
156942 }
156943 if( rc!=SQLITE_OK0 ){
156944 return rc;
156945 }
156946 enc = SQLITE_UTF16BE3;
156947 }
156948#else
156949 enc = SQLITE_UTF81;
156950#endif
156951
156952 /* Check if an existing function is being overridden or deleted. If so,
156953 ** and there are active VMs, then return SQLITE_BUSY. If a function
156954 ** is being overridden/deleted but there are no active VMs, allow the
156955 ** operation to continue but invalidate all precompiled statements.
156956 */
156957 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
156958 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK0x0003)==(u32)enc && p->nArg==nArg ){
156959 if( db->nVdbeActive ){
156960 sqlite3ErrorWithMsg(db, SQLITE_BUSY5,
156961 "unable to delete/modify user-function due to active statements");
156962 assert( !db->mallocFailed )((void) (0));
156963 return SQLITE_BUSY5;
156964 }else{
156965 sqlite3ExpirePreparedStatements(db, 0);
156966 }
156967 }
156968
156969 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
156970 assert(p || db->mallocFailed)((void) (0));
156971 if( !p ){
156972 return SQLITE_NOMEM_BKPT7;
156973 }
156974
156975 /* If an older version of the function with a configured destructor is
156976 ** being replaced invoke the destructor function here. */
156977 functionDestroy(db, p);
156978
156979 if( pDestructor ){
156980 pDestructor->nRef++;
156981 }
156982 p->u.pDestructor = pDestructor;
156983 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK0x0003) | extraFlags;
156984 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
156985 p->xSFunc = xSFunc ? xSFunc : xStep;
156986 p->xFinalize = xFinal;
156987 p->xValue = xValue;
156988 p->xInverse = xInverse;
156989 p->pUserData = pUserData;
156990 p->nArg = (u16)nArg;
156991 return SQLITE_OK0;
156992}
156993
156994/*
156995** Worker function used by utf-8 APIs that create new functions:
156996**
156997** sqlite3_create_function()
156998** sqlite3_create_function_v2()
156999** sqlite3_create_window_function()
157000*/
157001static int createFunctionApi(
157002 sqlite3 *db,
157003 const char *zFunc,
157004 int nArg,
157005 int enc,
157006 void *p,
157007 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
157008 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
157009 void (*xFinal)(sqlite3_context*),
157010 void (*xValue)(sqlite3_context*),
157011 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
157012 void(*xDestroy)(void*)
157013){
157014 int rc = SQLITE_ERROR1;
157015 FuncDestructor *pArg = 0;
157016
157017#ifdef SQLITE_ENABLE_API_ARMOR
157018 if( !sqlite3SafetyCheckOk(db) ){
157019 return SQLITE_MISUSE_BKPTsqlite3MisuseError(157019);
157020 }
157021#endif
157022 sqlite3_mutex_enter(db->mutex);
157023 if( xDestroy ){
157024 pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
157025 if( !pArg ){
157026 sqlite3OomFault(db);
157027 xDestroy(p);
157028 goto out;
157029 }
157030 pArg->nRef = 0;
157031 pArg->xDestroy = xDestroy;
157032 pArg->pUserData = p;
157033 }
157034 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p,
157035 xSFunc, xStep, xFinal, xValue, xInverse, pArg
157036 );
157037 if( pArg && pArg->nRef==0 ){
157038 assert( rc!=SQLITE_OK )((void) (0));
157039 xDestroy(p);
157040 sqlite3_free(pArg);
157041 }
157042
157043 out:
157044 rc = sqlite3ApiExit(db, rc);
157045 sqlite3_mutex_leave(db->mutex);
157046 return rc;
157047}
157048
157049/*
157050** Create new user functions.
157051*/
157052SQLITE_API int sqlite3_create_function(
157053 sqlite3 *db,
157054 const char *zFunc,
157055 int nArg,
157056 int enc,
157057 void *p,
157058 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
157059 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
157060 void (*xFinal)(sqlite3_context*)
157061){
157062 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
157063 xFinal, 0, 0, 0);
157064}
157065SQLITE_API int sqlite3_create_function_v2(
157066 sqlite3 *db,
157067 const char *zFunc,
157068 int nArg,
157069 int enc,
157070 void *p,
157071 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
157072 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
157073 void (*xFinal)(sqlite3_context*),
157074 void (*xDestroy)(void *)
157075){
157076 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
157077 xFinal, 0, 0, xDestroy);
157078}
157079SQLITE_API int sqlite3_create_window_function(
157080 sqlite3 *db,
157081 const char *zFunc,
157082 int nArg,
157083 int enc,
157084 void *p,
157085 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
157086 void (*xFinal)(sqlite3_context*),
157087 void (*xValue)(sqlite3_context*),
157088 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
157089 void (*xDestroy)(void *)
157090){
157091 return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
157092 xFinal, xValue, xInverse, xDestroy);
157093}
157094
157095#ifndef SQLITE_OMIT_UTF16
157096SQLITE_API int sqlite3_create_function16(
157097 sqlite3 *db,
157098 const void *zFunctionName,
157099 int nArg,
157100 int eTextRep,
157101 void *p,
157102 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
157103 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
157104 void (*xFinal)(sqlite3_context*)
157105){
157106 int rc;
157107 char *zFunc8;
157108
157109#ifdef SQLITE_ENABLE_API_ARMOR
157110 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(157110);
157111#endif
157112 sqlite3_mutex_enter(db->mutex);
157113 assert( !db->mallocFailed )((void) (0));
157114 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE2);
157115 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
157116 sqlite3DbFree(db, zFunc8);
157117 rc = sqlite3ApiExit(db, rc);
157118 sqlite3_mutex_leave(db->mutex);
157119 return rc;
157120}
157121#endif
157122
157123
157124/*
157125** The following is the implementation of an SQL function that always
157126** fails with an error message stating that the function is used in the
157127** wrong context. The sqlite3_overload_function() API might construct
157128** SQL function that use this routine so that the functions will exist
157129** for name resolution but are actually overloaded by the xFindFunction
157130** method of virtual tables.
157131*/
157132static void sqlite3InvalidFunction(
157133 sqlite3_context *context, /* The function calling context */
157134 int NotUsed, /* Number of arguments to the function */
157135 sqlite3_value **NotUsed2 /* Value of each argument */
157136){
157137 const char *zName = (const char*)sqlite3_user_data(context);
157138 char *zErr;
157139 UNUSED_PARAMETER2(NotUsed, NotUsed2)(void)(NotUsed),(void)(NotUsed2);
157140 zErr = sqlite3_mprintf(
157141 "unable to use function %s in the requested context", zName);
157142 sqlite3_result_error(context, zErr, -1);
157143 sqlite3_free(zErr);
157144}
157145
157146/*
157147** Declare that a function has been overloaded by a virtual table.
157148**
157149** If the function already exists as a regular global function, then
157150** this routine is a no-op. If the function does not exist, then create
157151** a new one that always throws a run-time error.
157152**
157153** When virtual tables intend to provide an overloaded function, they
157154** should call this routine to make sure the global function exists.
157155** A global function must exist in order for name resolution to work
157156** properly.
157157*/
157158SQLITE_API int sqlite3_overload_function(
157159 sqlite3 *db,
157160 const char *zName,
157161 int nArg
157162){
157163 int rc;
157164 char *zCopy;
157165
157166#ifdef SQLITE_ENABLE_API_ARMOR
157167 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
157168 return SQLITE_MISUSE_BKPTsqlite3MisuseError(157168);
157169 }
157170#endif
157171 sqlite3_mutex_enter(db->mutex);
157172 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF81, 0)!=0;
157173 sqlite3_mutex_leave(db->mutex);
157174 if( rc ) return SQLITE_OK0;
157175 zCopy = sqlite3_mprintf(zName);
157176 if( zCopy==0 ) return SQLITE_NOMEM7;
157177 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF81,
157178 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
157179}
157180
157181#ifndef SQLITE_OMIT_TRACE
157182/*
157183** Register a trace function. The pArg from the previously registered trace
157184** is returned.
157185**
157186** A NULL trace function means that no tracing is executes. A non-NULL
157187** trace is a pointer to a function that is invoked at the start of each
157188** SQL statement.
157189*/
157190#ifndef SQLITE_OMIT_DEPRECATED
157191SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
157192 void *pOld;
157193
157194#ifdef SQLITE_ENABLE_API_ARMOR
157195 if( !sqlite3SafetyCheckOk(db) ){
157196 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(157196);
157197 return 0;
157198 }
157199#endif
157200 sqlite3_mutex_enter(db->mutex);
157201 pOld = db->pTraceArg;
157202 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY0x40 : 0;
157203 db->xTrace = (int(*)(u32,void*,void*,void*))xTrace;
157204 db->pTraceArg = pArg;
157205 sqlite3_mutex_leave(db->mutex);
157206 return pOld;
157207}
157208#endif /* SQLITE_OMIT_DEPRECATED */
157209
157210/* Register a trace callback using the version-2 interface.
157211*/
157212SQLITE_API int sqlite3_trace_v2(
157213 sqlite3 *db, /* Trace this connection */
157214 unsigned mTrace, /* Mask of events to be traced */
157215 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
157216 void *pArg /* Context */
157217){
157218#ifdef SQLITE_ENABLE_API_ARMOR
157219 if( !sqlite3SafetyCheckOk(db) ){
157220 return SQLITE_MISUSE_BKPTsqlite3MisuseError(157220);
157221 }
157222#endif
157223 sqlite3_mutex_enter(db->mutex);
157224 if( mTrace==0 ) xTrace = 0;
157225 if( xTrace==0 ) mTrace = 0;
157226 db->mTrace = mTrace;
157227 db->xTrace = xTrace;
157228 db->pTraceArg = pArg;
157229 sqlite3_mutex_leave(db->mutex);
157230 return SQLITE_OK0;
157231}
157232
157233#ifndef SQLITE_OMIT_DEPRECATED
157234/*
157235** Register a profile function. The pArg from the previously registered
157236** profile function is returned.
157237**
157238** A NULL profile function means that no profiling is executes. A non-NULL
157239** profile is a pointer to a function that is invoked at the conclusion of
157240** each SQL statement that is run.
157241*/
157242SQLITE_API void *sqlite3_profile(
157243 sqlite3 *db,
157244 void (*xProfile)(void*,const char*,sqlite_uint64),
157245 void *pArg
157246){
157247 void *pOld;
157248
157249#ifdef SQLITE_ENABLE_API_ARMOR
157250 if( !sqlite3SafetyCheckOk(db) ){
157251 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(157251);
157252 return 0;
157253 }
157254#endif
157255 sqlite3_mutex_enter(db->mutex);
157256 pOld = db->pProfileArg;
157257 db->xProfile = xProfile;
157258 db->pProfileArg = pArg;
157259 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK0x0f;
157260 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE0x80;
157261 sqlite3_mutex_leave(db->mutex);
157262 return pOld;
157263}
157264#endif /* SQLITE_OMIT_DEPRECATED */
157265#endif /* SQLITE_OMIT_TRACE */
157266
157267/*
157268** Register a function to be invoked when a transaction commits.
157269** If the invoked function returns non-zero, then the commit becomes a
157270** rollback.
157271*/
157272SQLITE_API void *sqlite3_commit_hook(
157273 sqlite3 *db, /* Attach the hook to this database */
157274 int (*xCallback)(void*), /* Function to invoke on each commit */
157275 void *pArg /* Argument to the function */
157276){
157277 void *pOld;
157278
157279#ifdef SQLITE_ENABLE_API_ARMOR
157280 if( !sqlite3SafetyCheckOk(db) ){
157281 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(157281);
157282 return 0;
157283 }
157284#endif
157285 sqlite3_mutex_enter(db->mutex);
157286 pOld = db->pCommitArg;
157287 db->xCommitCallback = xCallback;
157288 db->pCommitArg = pArg;
157289 sqlite3_mutex_leave(db->mutex);
157290 return pOld;
157291}
157292
157293/*
157294** Register a callback to be invoked each time a row is updated,
157295** inserted or deleted using this database connection.
157296*/
157297SQLITE_API void *sqlite3_update_hook(
157298 sqlite3 *db, /* Attach the hook to this database */
157299 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
157300 void *pArg /* Argument to the function */
157301){
157302 void *pRet;
157303
157304#ifdef SQLITE_ENABLE_API_ARMOR
157305 if( !sqlite3SafetyCheckOk(db) ){
157306 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(157306);
157307 return 0;
157308 }
157309#endif
157310 sqlite3_mutex_enter(db->mutex);
157311 pRet = db->pUpdateArg;
157312 db->xUpdateCallback = xCallback;
157313 db->pUpdateArg = pArg;
157314 sqlite3_mutex_leave(db->mutex);
157315 return pRet;
157316}
157317
157318/*
157319** Register a callback to be invoked each time a transaction is rolled
157320** back by this database connection.
157321*/
157322SQLITE_API void *sqlite3_rollback_hook(
157323 sqlite3 *db, /* Attach the hook to this database */
157324 void (*xCallback)(void*), /* Callback function */
157325 void *pArg /* Argument to the function */
157326){
157327 void *pRet;
157328
157329#ifdef SQLITE_ENABLE_API_ARMOR
157330 if( !sqlite3SafetyCheckOk(db) ){
157331 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(157331);
157332 return 0;
157333 }
157334#endif
157335 sqlite3_mutex_enter(db->mutex);
157336 pRet = db->pRollbackArg;
157337 db->xRollbackCallback = xCallback;
157338 db->pRollbackArg = pArg;
157339 sqlite3_mutex_leave(db->mutex);
157340 return pRet;
157341}
157342
157343#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
157344/*
157345** Register a callback to be invoked each time a row is updated,
157346** inserted or deleted using this database connection.
157347*/
157348SQLITE_API void *sqlite3_preupdate_hook(
157349 sqlite3 *db, /* Attach the hook to this database */
157350 void(*xCallback)( /* Callback function */
157351 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
157352 void *pArg /* First callback argument */
157353){
157354 void *pRet;
157355 sqlite3_mutex_enter(db->mutex);
157356 pRet = db->pPreUpdateArg;
157357 db->xPreUpdateCallback = xCallback;
157358 db->pPreUpdateArg = pArg;
157359 sqlite3_mutex_leave(db->mutex);
157360 return pRet;
157361}
157362#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
157363
157364#ifndef SQLITE_OMIT_WAL
157365/*
157366** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
157367** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
157368** is greater than sqlite3.pWalArg cast to an integer (the value configured by
157369** wal_autocheckpoint()).
157370*/
157371SQLITE_PRIVATEstatic int sqlite3WalDefaultHook(
157372 void *pClientData, /* Argument */
157373 sqlite3 *db, /* Connection */
157374 const char *zDb, /* Database */
157375 int nFrame /* Size of WAL */
157376){
157377 if( nFrame>=SQLITE_PTR_TO_INT(pClientData)((int)(long int)(pClientData)) ){
157378 sqlite3BeginBenignMalloc();
157379 sqlite3_wal_checkpoint(db, zDb);
157380 sqlite3EndBenignMalloc();
157381 }
157382 return SQLITE_OK0;
157383}
157384#endif /* SQLITE_OMIT_WAL */
157385
157386/*
157387** Configure an sqlite3_wal_hook() callback to automatically checkpoint
157388** a database after committing a transaction if there are nFrame or
157389** more frames in the log file. Passing zero or a negative value as the
157390** nFrame parameter disables automatic checkpoints entirely.
157391**
157392** The callback registered by this function replaces any existing callback
157393** registered using sqlite3_wal_hook(). Likewise, registering a callback
157394** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
157395** configured by this function.
157396*/
157397SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
157398#ifdef SQLITE_OMIT_WAL
157399 UNUSED_PARAMETER(db)(void)(db);
157400 UNUSED_PARAMETER(nFrame)(void)(nFrame);
157401#else
157402#ifdef SQLITE_ENABLE_API_ARMOR
157403 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(157403);
157404#endif
157405 if( nFrame>0 ){
157406 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)((void*)(long int)(nFrame)));
157407 }else{
157408 sqlite3_wal_hook(db, 0, 0);
157409 }
157410#endif
157411 return SQLITE_OK0;
157412}
157413
157414/*
157415** Register a callback to be invoked each time a transaction is written
157416** into the write-ahead-log by this database connection.
157417*/
157418SQLITE_API void *sqlite3_wal_hook(
157419 sqlite3 *db, /* Attach the hook to this db handle */
157420 int(*xCallback)(void *, sqlite3*, const char*, int),
157421 void *pArg /* First argument passed to xCallback() */
157422){
157423#ifndef SQLITE_OMIT_WAL
157424 void *pRet;
157425#ifdef SQLITE_ENABLE_API_ARMOR
157426 if( !sqlite3SafetyCheckOk(db) ){
157427 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(157427);
157428 return 0;
157429 }
157430#endif
157431 sqlite3_mutex_enter(db->mutex);
157432 pRet = db->pWalArg;
157433 db->xWalCallback = xCallback;
157434 db->pWalArg = pArg;
157435 sqlite3_mutex_leave(db->mutex);
157436 return pRet;
157437#else
157438 return 0;
157439#endif
157440}
157441
157442/*
157443** Checkpoint database zDb.
157444*/
157445SQLITE_API int sqlite3_wal_checkpoint_v2(
157446 sqlite3 *db, /* Database handle */
157447 const char *zDb, /* Name of attached database (or NULL) */
157448 int eMode, /* SQLITE_CHECKPOINT_* value */
157449 int *pnLog, /* OUT: Size of WAL log in frames */
157450 int *pnCkpt /* OUT: Total number of frames checkpointed */
157451){
157452#ifdef SQLITE_OMIT_WAL
157453 return SQLITE_OK0;
157454#else
157455 int rc; /* Return code */
157456 int iDb = SQLITE_MAX_ATTACHED10; /* sqlite3.aDb[] index of db to checkpoint */
157457
157458#ifdef SQLITE_ENABLE_API_ARMOR
157459 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(157459);
157460#endif
157461
157462 /* Initialize the output variables to -1 in case an error occurs. */
157463 if( pnLog ) *pnLog = -1;
157464 if( pnCkpt ) *pnCkpt = -1;
157465
157466 assert( SQLITE_CHECKPOINT_PASSIVE==0 )((void) (0));
157467 assert( SQLITE_CHECKPOINT_FULL==1 )((void) (0));
157468 assert( SQLITE_CHECKPOINT_RESTART==2 )((void) (0));
157469 assert( SQLITE_CHECKPOINT_TRUNCATE==3 )((void) (0));
157470 if( eMode<SQLITE_CHECKPOINT_PASSIVE0 || eMode>SQLITE_CHECKPOINT_TRUNCATE3 ){
157471 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
157472 ** mode: */
157473 return SQLITE_MISUSE21;
157474 }
157475
157476 sqlite3_mutex_enter(db->mutex);
157477 if( zDb && zDb[0] ){
157478 iDb = sqlite3FindDbName(db, zDb);
157479 }
157480 if( iDb<0 ){
157481 rc = SQLITE_ERROR1;
157482 sqlite3ErrorWithMsg(db, SQLITE_ERROR1, "unknown database: %s", zDb);
157483 }else{
157484 db->busyHandler.nBusy = 0;
157485 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
157486 sqlite3Error(db, rc);
157487 }
157488 rc = sqlite3ApiExit(db, rc);
157489
157490 /* If there are no active statements, clear the interrupt flag at this
157491 ** point. */
157492 if( db->nVdbeActive==0 ){
157493 db->u1.isInterrupted = 0;
157494 }
157495
157496 sqlite3_mutex_leave(db->mutex);
157497 return rc;
157498#endif
157499}
157500
157501
157502/*
157503** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
157504** to contains a zero-length string, all attached databases are
157505** checkpointed.
157506*/
157507SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
157508 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
157509 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
157510 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE0,0,0);
157511}
157512
157513#ifndef SQLITE_OMIT_WAL
157514/*
157515** Run a checkpoint on database iDb. This is a no-op if database iDb is
157516** not currently open in WAL mode.
157517**
157518** If a transaction is open on the database being checkpointed, this
157519** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
157520** an error occurs while running the checkpoint, an SQLite error code is
157521** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
157522**
157523** The mutex on database handle db should be held by the caller. The mutex
157524** associated with the specific b-tree being checkpointed is taken by
157525** this function while the checkpoint is running.
157526**
157527** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
157528** checkpointed. If an error is encountered it is returned immediately -
157529** no attempt is made to checkpoint any remaining databases.
157530**
157531** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
157532** or TRUNCATE.
157533*/
157534SQLITE_PRIVATEstatic int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
157535 int rc = SQLITE_OK0; /* Return code */
157536 int i; /* Used to iterate through attached dbs */
157537 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
157538
157539 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
157540 assert( !pnLog || *pnLog==-1 )((void) (0));
157541 assert( !pnCkpt || *pnCkpt==-1 )((void) (0));
157542
157543 for(i=0; i<db->nDb && rc==SQLITE_OK0; i++){
157544 if( i==iDb || iDb==SQLITE_MAX_ATTACHED10 ){
157545 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
157546 pnLog = 0;
157547 pnCkpt = 0;
157548 if( rc==SQLITE_BUSY5 ){
157549 bBusy = 1;
157550 rc = SQLITE_OK0;
157551 }
157552 }
157553 }
157554
157555 return (rc==SQLITE_OK0 && bBusy) ? SQLITE_BUSY5 : rc;
157556}
157557#endif /* SQLITE_OMIT_WAL */
157558
157559/*
157560** This function returns true if main-memory should be used instead of
157561** a temporary file for transient pager files and statement journals.
157562** The value returned depends on the value of db->temp_store (runtime
157563** parameter) and the compile time value of SQLITE_TEMP_STORE. The
157564** following table describes the relationship between these two values
157565** and this functions return value.
157566**
157567** SQLITE_TEMP_STORE db->temp_store Location of temporary database
157568** ----------------- -------------- ------------------------------
157569** 0 any file (return 0)
157570** 1 1 file (return 0)
157571** 1 2 memory (return 1)
157572** 1 0 file (return 0)
157573** 2 1 file (return 0)
157574** 2 2 memory (return 1)
157575** 2 0 memory (return 1)
157576** 3 any memory (return 1)
157577*/
157578SQLITE_PRIVATEstatic int sqlite3TempInMemory(const sqlite3 *db){
157579#if SQLITE_TEMP_STORE1==1
157580 return ( db->temp_store==2 );
157581#endif
157582#if SQLITE_TEMP_STORE1==2
157583 return ( db->temp_store!=1 );
157584#endif
157585#if SQLITE_TEMP_STORE1==3
157586 UNUSED_PARAMETER(db)(void)(db);
157587 return 1;
157588#endif
157589#if SQLITE_TEMP_STORE1<1 || SQLITE_TEMP_STORE1>3
157590 UNUSED_PARAMETER(db)(void)(db);
157591 return 0;
157592#endif
157593}
157594
157595/*
157596** Return UTF-8 encoded English language explanation of the most recent
157597** error.
157598*/
157599SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
157600 const char *z;
157601 if( !db ){
157602 return sqlite3ErrStr(SQLITE_NOMEM_BKPT7);
157603 }
157604 if( !sqlite3SafetyCheckSickOrOk(db) ){
157605 return sqlite3ErrStr(SQLITE_MISUSE_BKPTsqlite3MisuseError(157605));
157606 }
157607 sqlite3_mutex_enter(db->mutex);
157608 if( db->mallocFailed ){
157609 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT7);
157610 }else{
157611 testcase( db->pErr==0 );
157612 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
157613 assert( !db->mallocFailed )((void) (0));
157614 if( z==0 ){
157615 z = sqlite3ErrStr(db->errCode);
157616 }
157617 }
157618 sqlite3_mutex_leave(db->mutex);
157619 return z;
157620}
157621
157622#ifndef SQLITE_OMIT_UTF16
157623/*
157624** Return UTF-16 encoded English language explanation of the most recent
157625** error.
157626*/
157627SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
157628 static const u16 outOfMem[] = {
157629 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
157630 };
157631 static const u16 misuse[] = {
157632 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
157633 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
157634 'm', 'i', 's', 'u', 's', 'e', 0
157635 };
157636
157637 const void *z;
157638 if( !db ){
157639 return (void *)outOfMem;
157640 }
157641 if( !sqlite3SafetyCheckSickOrOk(db) ){
157642 return (void *)misuse;
157643 }
157644 sqlite3_mutex_enter(db->mutex);
157645 if( db->mallocFailed ){
157646 z = (void *)outOfMem;
157647 }else{
157648 z = sqlite3_value_text16(db->pErr);
157649 if( z==0 ){
157650 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
157651 z = sqlite3_value_text16(db->pErr);
157652 }
157653 /* A malloc() may have failed within the call to sqlite3_value_text16()
157654 ** above. If this is the case, then the db->mallocFailed flag needs to
157655 ** be cleared before returning. Do this directly, instead of via
157656 ** sqlite3ApiExit(), to avoid setting the database handle error message.
157657 */
157658 sqlite3OomClear(db);
157659 }
157660 sqlite3_mutex_leave(db->mutex);
157661 return z;
157662}
157663#endif /* SQLITE_OMIT_UTF16 */
157664
157665/*
157666** Return the most recent error code generated by an SQLite routine. If NULL is
157667** passed to this function, we assume a malloc() failed during sqlite3_open().
157668*/
157669SQLITE_API int sqlite3_errcode(sqlite3 *db){
157670 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
157671 return SQLITE_MISUSE_BKPTsqlite3MisuseError(157671);
157672 }
157673 if( !db || db->mallocFailed ){
157674 return SQLITE_NOMEM_BKPT7;
157675 }
157676 return db->errCode & db->errMask;
157677}
157678SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
157679 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
157680 return SQLITE_MISUSE_BKPTsqlite3MisuseError(157680);
157681 }
157682 if( !db || db->mallocFailed ){
157683 return SQLITE_NOMEM_BKPT7;
157684 }
157685 return db->errCode;
157686}
157687SQLITE_API int sqlite3_system_errno(sqlite3 *db){
157688 return db ? db->iSysErrno : 0;
157689}
157690
157691/*
157692** Return a string that describes the kind of error specified in the
157693** argument. For now, this simply calls the internal sqlite3ErrStr()
157694** function.
157695*/
157696SQLITE_API const char *sqlite3_errstr(int rc){
157697 return sqlite3ErrStr(rc);
157698}
157699
157700/*
157701** Create a new collating function for database "db". The name is zName
157702** and the encoding is enc.
157703*/
157704static int createCollation(
157705 sqlite3* db,
157706 const char *zName,
157707 u8 enc,
157708 void* pCtx,
157709 int(*xCompare)(void*,int,const void*,int,const void*),
157710 void(*xDel)(void*)
157711){
157712 CollSeq *pColl;
157713 int enc2;
157714
157715 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
157716
157717 /* If SQLITE_UTF16 is specified as the encoding type, transform this
157718 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
157719 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
157720 */
157721 enc2 = enc;
157722 testcase( enc2==SQLITE_UTF16 );
157723 testcase( enc2==SQLITE_UTF16_ALIGNED );
157724 if( enc2==SQLITE_UTF164 || enc2==SQLITE_UTF16_ALIGNED8 ){
157725 enc2 = SQLITE_UTF16NATIVE2;
157726 }
157727 if( enc2<SQLITE_UTF81 || enc2>SQLITE_UTF16BE3 ){
157728 return SQLITE_MISUSE_BKPTsqlite3MisuseError(157728);
157729 }
157730
157731 /* Check if this call is removing or replacing an existing collation
157732 ** sequence. If so, and there are active VMs, return busy. If there
157733 ** are no active VMs, invalidate any pre-compiled statements.
157734 */
157735 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
157736 if( pColl && pColl->xCmp ){
157737 if( db->nVdbeActive ){
157738 sqlite3ErrorWithMsg(db, SQLITE_BUSY5,
157739 "unable to delete/modify collation sequence due to active statements");
157740 return SQLITE_BUSY5;
157741 }
157742 sqlite3ExpirePreparedStatements(db, 0);
157743
157744 /* If collation sequence pColl was created directly by a call to
157745 ** sqlite3_create_collation, and not generated by synthCollSeq(),
157746 ** then any copies made by synthCollSeq() need to be invalidated.
157747 ** Also, collation destructor - CollSeq.xDel() - function may need
157748 ** to be called.
157749 */
157750 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED8)==enc2 ){
157751 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
157752 int j;
157753 for(j=0; j<3; j++){
157754 CollSeq *p = &aColl[j];
157755 if( p->enc==pColl->enc ){
157756 if( p->xDel ){
157757 p->xDel(p->pUser);
157758 }
157759 p->xCmp = 0;
157760 }
157761 }
157762 }
157763 }
157764
157765 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
157766 if( pColl==0 ) return SQLITE_NOMEM_BKPT7;
157767 pColl->xCmp = xCompare;
157768 pColl->pUser = pCtx;
157769 pColl->xDel = xDel;
157770 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED8));
157771 sqlite3Error(db, SQLITE_OK0);
157772 return SQLITE_OK0;
157773}
157774
157775
157776/*
157777** This array defines hard upper bounds on limit values. The
157778** initializer must be kept in sync with the SQLITE_LIMIT_*
157779** #defines in sqlite3.h.
157780*/
157781static const int aHardLimit[] = {
157782 SQLITE_MAX_LENGTH1000000000,
157783 SQLITE_MAX_SQL_LENGTH1000000000,
157784 SQLITE_MAX_COLUMN2000,
157785 SQLITE_MAX_EXPR_DEPTH1000,
157786 SQLITE_MAX_COMPOUND_SELECT500,
157787 SQLITE_MAX_VDBE_OP250000000,
157788 SQLITE_MAX_FUNCTION_ARG127,
157789 SQLITE_MAX_ATTACHED10,
157790 SQLITE_MAX_LIKE_PATTERN_LENGTH50000,
157791 SQLITE_MAX_VARIABLE_NUMBER999, /* IMP: R-38091-32352 */
157792 SQLITE_MAX_TRIGGER_DEPTH1000,
157793 SQLITE_MAX_WORKER_THREADS8,
157794};
157795
157796/*
157797** Make sure the hard limits are set to reasonable values
157798*/
157799#if SQLITE_MAX_LENGTH1000000000<100
157800# error SQLITE_MAX_LENGTH1000000000 must be at least 100
157801#endif
157802#if SQLITE_MAX_SQL_LENGTH1000000000<100
157803# error SQLITE_MAX_SQL_LENGTH1000000000 must be at least 100
157804#endif
157805#if SQLITE_MAX_SQL_LENGTH1000000000>SQLITE_MAX_LENGTH1000000000
157806# error SQLITE_MAX_SQL_LENGTH1000000000 must not be greater than SQLITE_MAX_LENGTH1000000000
157807#endif
157808#if SQLITE_MAX_COMPOUND_SELECT500<2
157809# error SQLITE_MAX_COMPOUND_SELECT500 must be at least 2
157810#endif
157811#if SQLITE_MAX_VDBE_OP250000000<40
157812# error SQLITE_MAX_VDBE_OP250000000 must be at least 40
157813#endif
157814#if SQLITE_MAX_FUNCTION_ARG127<0 || SQLITE_MAX_FUNCTION_ARG127>127
157815# error SQLITE_MAX_FUNCTION_ARG127 must be between 0 and 127
157816#endif
157817#if SQLITE_MAX_ATTACHED10<0 || SQLITE_MAX_ATTACHED10>125
157818# error SQLITE_MAX_ATTACHED10 must be between 0 and 125
157819#endif
157820#if SQLITE_MAX_LIKE_PATTERN_LENGTH50000<1
157821# error SQLITE_MAX_LIKE_PATTERN_LENGTH50000 must be at least 1
157822#endif
157823#if SQLITE_MAX_COLUMN2000>32767
157824# error SQLITE_MAX_COLUMN2000 must not exceed 32767
157825#endif
157826#if SQLITE_MAX_TRIGGER_DEPTH1000<1
157827# error SQLITE_MAX_TRIGGER_DEPTH1000 must be at least 1
157828#endif
157829#if SQLITE_MAX_WORKER_THREADS8<0 || SQLITE_MAX_WORKER_THREADS8>50
157830# error SQLITE_MAX_WORKER_THREADS8 must be between 0 and 50
157831#endif
157832
157833
157834/*
157835** Change the value of a limit. Report the old value.
157836** If an invalid limit index is supplied, report -1.
157837** Make no changes but still report the old value if the
157838** new limit is negative.
157839**
157840** A new lower limit does not shrink existing constructs.
157841** It merely prevents new constructs that exceed the limit
157842** from forming.
157843*/
157844SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
157845 int oldLimit;
157846
157847#ifdef SQLITE_ENABLE_API_ARMOR
157848 if( !sqlite3SafetyCheckOk(db) ){
157849 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(157849);
157850 return -1;
157851 }
157852#endif
157853
157854 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
157855 ** there is a hard upper bound set at compile-time by a C preprocessor
157856 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
157857 ** "_MAX_".)
157858 */
157859 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH )((void) (0));
157860 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH )((void) (0));
157861 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN )((void) (0));
157862 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH )((void) (0));
157863 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT)((void) (0));
157864 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP )((void) (0));
157865 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG )((void) (0));
157866 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED )((void) (0));
157867 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==((void) (0))
157868 SQLITE_MAX_LIKE_PATTERN_LENGTH )((void) (0));
157869 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER)((void) (0));
157870 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH )((void) (0));
157871 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS )((void) (0));
157872 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) )((void) (0));
157873
157874
157875 if( limitId<0 || limitId>=SQLITE_N_LIMIT(11 +1) ){
157876 return -1;
157877 }
157878 oldLimit = db->aLimit[limitId];
157879 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
157880 if( newLimit>aHardLimit[limitId] ){
157881 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
157882 }
157883 db->aLimit[limitId] = newLimit;
157884 }
157885 return oldLimit; /* IMP: R-53341-35419 */
157886}
157887
157888/*
157889** This function is used to parse both URIs and non-URI filenames passed by the
157890** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
157891** URIs specified as part of ATTACH statements.
157892**
157893** The first argument to this function is the name of the VFS to use (or
157894** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
157895** query parameter. The second argument contains the URI (or non-URI filename)
157896** itself. When this function is called the *pFlags variable should contain
157897** the default flags to open the database handle with. The value stored in
157898** *pFlags may be updated before returning if the URI filename contains
157899** "cache=xxx" or "mode=xxx" query parameters.
157900**
157901** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
157902** the VFS that should be used to open the database file. *pzFile is set to
157903** point to a buffer containing the name of the file to open. It is the
157904** responsibility of the caller to eventually call sqlite3_free() to release
157905** this buffer.
157906**
157907** If an error occurs, then an SQLite error code is returned and *pzErrMsg
157908** may be set to point to a buffer containing an English language error
157909** message. It is the responsibility of the caller to eventually release
157910** this buffer by calling sqlite3_free().
157911*/
157912SQLITE_PRIVATEstatic int sqlite3ParseUri(
157913 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
157914 const char *zUri, /* Nul-terminated URI to parse */
157915 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
157916 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
157917 char **pzFile, /* OUT: Filename component of URI */
157918 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
157919){
157920 int rc = SQLITE_OK0;
157921 unsigned int flags = *pFlags;
157922 const char *zVfs = zDefaultVfs;
157923 char *zFile;
157924 char c;
157925 int nUri = sqlite3Strlen30(zUri);
157926
157927 assert( *pzErrMsg==0 )((void) (0));
157928
157929 if( ((flags & SQLITE_OPEN_URI0x00000040) /* IMP: R-48725-32206 */
157930 || sqlite3GlobalConfigsqlite3Config.bOpenUri) /* IMP: R-51689-46548 */
157931 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
157932 ){
157933 char *zOpt;
157934 int eState; /* Parser state when parsing URI */
157935 int iIn; /* Input character index */
157936 int iOut = 0; /* Output character index */
157937 u64 nByte = nUri+2; /* Bytes of space to allocate */
157938
157939 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
157940 ** method that there may be extra parameters following the file-name. */
157941 flags |= SQLITE_OPEN_URI0x00000040;
157942
157943 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
157944 zFile = sqlite3_malloc64(nByte);
157945 if( !zFile ) return SQLITE_NOMEM_BKPT7;
157946
157947 iIn = 5;
157948#ifdef SQLITE_ALLOW_URI_AUTHORITY
157949 if( strncmp(zUri+5, "///", 3)==0 ){
157950 iIn = 7;
157951 /* The following condition causes URIs with five leading / characters
157952 ** like file://///host/path to be converted into UNCs like //host/path.
157953 ** The correct URI for that UNC has only two or four leading / characters
157954 ** file://host/path or file:////host/path. But 5 leading slashes is a
157955 ** common error, we are told, so we handle it as a special case. */
157956 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
157957 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
157958 iIn = 16;
157959 }
157960#else
157961 /* Discard the scheme and authority segments of the URI. */
157962 if( zUri[5]=='/' && zUri[6]=='/' ){
157963 iIn = 7;
157964 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
157965 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
157966 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
157967 iIn-7, &zUri[7]);
157968 rc = SQLITE_ERROR1;
157969 goto parse_uri_out;
157970 }
157971 }
157972#endif
157973
157974 /* Copy the filename and any query parameters into the zFile buffer.
157975 ** Decode %HH escape codes along the way.
157976 **
157977 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
157978 ** on the parsing context. As follows:
157979 **
157980 ** 0: Parsing file-name.
157981 ** 1: Parsing name section of a name=value query parameter.
157982 ** 2: Parsing value section of a name=value query parameter.
157983 */
157984 eState = 0;
157985 while( (c = zUri[iIn])!=0 && c!='#' ){
157986 iIn++;
157987 if( c=='%'
157988 && sqlite3Isxdigit(zUri[iIn])(sqlite3CtypeMap[(unsigned char)(zUri[iIn])]&0x08)
157989 && sqlite3Isxdigit(zUri[iIn+1])(sqlite3CtypeMap[(unsigned char)(zUri[iIn+1])]&0x08)
157990 ){
157991 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
157992 octet += sqlite3HexToInt(zUri[iIn++]);
157993
157994 assert( octet>=0 && octet<256 )((void) (0));
157995 if( octet==0 ){
157996#ifndef SQLITE_ENABLE_URI_00_ERROR
157997 /* This branch is taken when "%00" appears within the URI. In this
157998 ** case we ignore all text in the remainder of the path, name or
157999 ** value currently being parsed. So ignore the current character
158000 ** and skip to the next "?", "=" or "&", as appropriate. */
158001 while( (c = zUri[iIn])!=0 && c!='#'
158002 && (eState!=0 || c!='?')
158003 && (eState!=1 || (c!='=' && c!='&'))
158004 && (eState!=2 || c!='&')
158005 ){
158006 iIn++;
158007 }
158008 continue;
158009#else
158010 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
158011 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
158012 rc = SQLITE_ERROR1;
158013 goto parse_uri_out;
158014#endif
158015 }
158016 c = octet;
158017 }else if( eState==1 && (c=='&' || c=='=') ){
158018 if( zFile[iOut-1]==0 ){
158019 /* An empty option name. Ignore this option altogether. */
158020 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
158021 continue;
158022 }
158023 if( c=='&' ){
158024 zFile[iOut++] = '\0';
158025 }else{
158026 eState = 2;
158027 }
158028 c = 0;
158029 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
158030 c = 0;
158031 eState = 1;
158032 }
158033 zFile[iOut++] = c;
158034 }
158035 if( eState==1 ) zFile[iOut++] = '\0';
158036 zFile[iOut++] = '\0';
158037 zFile[iOut++] = '\0';
158038
158039 /* Check if there were any options specified that should be interpreted
158040 ** here. Options that are interpreted here include "vfs" and those that
158041 ** correspond to flags that may be passed to the sqlite3_open_v2()
158042 ** method. */
158043 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
158044 while( zOpt[0] ){
158045 int nOpt = sqlite3Strlen30(zOpt);
158046 char *zVal = &zOpt[nOpt+1];
158047 int nVal = sqlite3Strlen30(zVal);
158048
158049 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
158050 zVfs = zVal;
158051 }else{
158052 struct OpenMode {
158053 const char *z;
158054 int mode;
158055 } *aMode = 0;
158056 char *zModeType = 0;
158057 int mask = 0;
158058 int limit = 0;
158059
158060 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
158061 static struct OpenMode aCacheMode[] = {
158062 { "shared", SQLITE_OPEN_SHAREDCACHE0x00020000 },
158063 { "private", SQLITE_OPEN_PRIVATECACHE0x00040000 },
158064 { 0, 0 }
158065 };
158066
158067 mask = SQLITE_OPEN_SHAREDCACHE0x00020000|SQLITE_OPEN_PRIVATECACHE0x00040000;
158068 aMode = aCacheMode;
158069 limit = mask;
158070 zModeType = "cache";
158071 }
158072 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
158073 static struct OpenMode aOpenMode[] = {
158074 { "ro", SQLITE_OPEN_READONLY0x00000001 },
158075 { "rw", SQLITE_OPEN_READWRITE0x00000002 },
158076 { "rwc", SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 },
158077 { "memory", SQLITE_OPEN_MEMORY0x00000080 },
158078 { 0, 0 }
158079 };
158080
158081 mask = SQLITE_OPEN_READONLY0x00000001 | SQLITE_OPEN_READWRITE0x00000002
158082 | SQLITE_OPEN_CREATE0x00000004 | SQLITE_OPEN_MEMORY0x00000080;
158083 aMode = aOpenMode;
158084 limit = mask & flags;
158085 zModeType = "access";
158086 }
158087
158088 if( aMode ){
158089 int i;
158090 int mode = 0;
158091 for(i=0; aMode[i].z; i++){
158092 const char *z = aMode[i].z;
158093 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
158094 mode = aMode[i].mode;
158095 break;
158096 }
158097 }
158098 if( mode==0 ){
158099 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
158100 rc = SQLITE_ERROR1;
158101 goto parse_uri_out;
158102 }
158103 if( (mode & ~SQLITE_OPEN_MEMORY0x00000080)>limit ){
158104 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
158105 zModeType, zVal);
158106 rc = SQLITE_PERM3;
158107 goto parse_uri_out;
158108 }
158109 flags = (flags & ~mask) | mode;
158110 }
158111 }
158112
158113 zOpt = &zVal[nVal+1];
158114 }
158115
158116 }else{
158117 zFile = sqlite3_malloc64(nUri+2);
158118 if( !zFile ) return SQLITE_NOMEM_BKPT7;
158119 if( nUri ){
158120 memcpy(zFile, zUri, nUri);
158121 }
158122 zFile[nUri] = '\0';
158123 zFile[nUri+1] = '\0';
158124 flags &= ~SQLITE_OPEN_URI0x00000040;
158125 }
158126
158127 *ppVfs = sqlite3_vfs_find(zVfs);
158128 if( *ppVfs==0 ){
158129 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
158130 rc = SQLITE_ERROR1;
158131 }
158132 parse_uri_out:
158133 if( rc!=SQLITE_OK0 ){
158134 sqlite3_free(zFile);
158135 zFile = 0;
158136 }
158137 *pFlags = flags;
158138 *pzFile = zFile;
158139 return rc;
158140}
158141
158142#if defined(SQLITE_HAS_CODEC)
158143/*
158144** Process URI filename query parameters relevant to the SQLite Encryption
158145** Extension. Return true if any of the relevant query parameters are
158146** seen and return false if not.
158147*/
158148SQLITE_PRIVATEstatic int sqlite3CodecQueryParameters(0
158149 sqlite3 *db, /* Database connection */0
158150 const char *zDb, /* Which schema is being created/attached */0
158151 const char *zUri /* URI filename */0
158152)0{
158153 const char *zKey;
158154 if( (zKey = sqlite3_uri_parameter(zUri, "hexkey"))!=0 && zKey[0] ){
158155 u8 iByte;
158156 int i;
158157 char zDecoded[40];
158158 for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i])(sqlite3CtypeMap[(unsigned char)(zKey[i])]&0x08); i++){
158159 iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]);
158160 if( (i&1)!=0 ) zDecoded[i/2] = iByte;
158161 }
158162 sqlite3_key_v2(db, zDb, zDecoded, i/2);
158163 return 1;
158164 }else if( (zKey = sqlite3_uri_parameter(zUri, "key"))!=0 ){
158165 sqlite3_key_v2(db, zDb, zKey, sqlite3Strlen30(zKey));
158166 return 1;
158167 }else if( (zKey = sqlite3_uri_parameter(zUri, "textkey"))!=0 ){
158168 sqlite3_key_v2(db, zDb, zKey, -1);
158169 return 1;
158170 }else{
158171 return 0;
158172 }
158173}
158174#endif
158175
158176
158177/*
158178** This routine does the work of opening a database on behalf of
158179** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
158180** is UTF-8 encoded.
158181*/
158182static int openDatabase(
158183 const char *zFilename, /* Database filename UTF-8 encoded */
158184 sqlite3 **ppDb, /* OUT: Returned database handle */
158185 unsigned int flags, /* Operational flags */
158186 const char *zVfs /* Name of the VFS to use */
158187){
158188 sqlite3 *db; /* Store allocated handle here */
158189 int rc; /* Return code */
158190 int isThreadsafe; /* True for threadsafe connections */
158191 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
158192 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
158193
158194#ifdef SQLITE_ENABLE_API_ARMOR
158195 if( ppDb==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158195);
158196#endif
158197 *ppDb = 0;
158198#ifndef SQLITE_OMIT_AUTOINIT
158199 rc = sqlite3_initialize();
158200 if( rc ) return rc;
158201#endif
158202
158203 if( sqlite3GlobalConfigsqlite3Config.bCoreMutex==0 ){
158204 isThreadsafe = 0;
158205 }else if( flags & SQLITE_OPEN_NOMUTEX0x00008000 ){
158206 isThreadsafe = 0;
158207 }else if( flags & SQLITE_OPEN_FULLMUTEX0x00010000 ){
158208 isThreadsafe = 1;
158209 }else{
158210 isThreadsafe = sqlite3GlobalConfigsqlite3Config.bFullMutex;
158211 }
158212
158213 if( flags & SQLITE_OPEN_PRIVATECACHE0x00040000 ){
158214 flags &= ~SQLITE_OPEN_SHAREDCACHE0x00020000;
158215 }else if( sqlite3GlobalConfigsqlite3Config.sharedCacheEnabled ){
158216 flags |= SQLITE_OPEN_SHAREDCACHE0x00020000;
158217 }
158218
158219 /* Remove harmful bits from the flags parameter
158220 **
158221 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
158222 ** dealt with in the previous code block. Besides these, the only
158223 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
158224 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
158225 ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask
158226 ** off all other flags.
158227 */
158228 flags &= ~( SQLITE_OPEN_DELETEONCLOSE0x00000008 |
158229 SQLITE_OPEN_EXCLUSIVE0x00000010 |
158230 SQLITE_OPEN_MAIN_DB0x00000100 |
158231 SQLITE_OPEN_TEMP_DB0x00000200 |
158232 SQLITE_OPEN_TRANSIENT_DB0x00000400 |
158233 SQLITE_OPEN_MAIN_JOURNAL0x00000800 |
158234 SQLITE_OPEN_TEMP_JOURNAL0x00001000 |
158235 SQLITE_OPEN_SUBJOURNAL0x00002000 |
158236 SQLITE_OPEN_MASTER_JOURNAL0x00004000 |
158237 SQLITE_OPEN_NOMUTEX0x00008000 |
158238 SQLITE_OPEN_FULLMUTEX0x00010000 |
158239 SQLITE_OPEN_WAL0x00080000
158240 );
158241
158242 /* Allocate the sqlite data structure */
158243 db = sqlite3MallocZero( sizeof(sqlite3) );
158244 if( db==0 ) goto opendb_out;
158245 if( isThreadsafe
158246#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
158247 || sqlite3GlobalConfigsqlite3Config.bCoreMutex
158248#endif
158249 ){
158250 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE1);
158251 if( db->mutex==0 ){
158252 sqlite3_free(db);
158253 db = 0;
158254 goto opendb_out;
158255 }
158256 if( isThreadsafe==0 ){
158257 sqlite3MutexWarnOnContention(db->mutex);
158258 }
158259 }
158260 sqlite3_mutex_enter(db->mutex);
158261 db->errMask = 0xff;
158262 db->nDb = 2;
158263 db->magic = SQLITE_MAGIC_BUSY0xf03b7906;
158264 db->aDb = db->aDbStatic;
158265 db->lookaside.bDisable = 1;
158266
158267 assert( sizeof(db->aLimit)==sizeof(aHardLimit) )((void) (0));
158268 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
158269 db->aLimit[SQLITE_LIMIT_WORKER_THREADS11] = SQLITE_DEFAULT_WORKER_THREADS0;
158270 db->autoCommit = 1;
158271 db->nextAutovac = -1;
158272 db->szMmap = sqlite3GlobalConfigsqlite3Config.szMmap;
158273 db->nextPagesize = 0;
158274 db->nMaxSorterMmap = 0x7FFFFFFF;
158275 db->flags |= SQLITE_ShortColNames0x00000040
158276 | SQLITE_EnableTrigger0x00040000
158277 | SQLITE_CacheSpill0x00000020
158278
158279/* The SQLITE_DQS compile-time option determines the default settings
158280** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
158281**
158282** SQLITE_DQS SQLITE_DBCONFIG_DQS_DDL SQLITE_DBCONFIG_DQS_DML
158283** ---------- ----------------------- -----------------------
158284** undefined on on
158285** 3 on on
158286** 2 on off
158287** 1 off on
158288** 0 off off
158289**
158290** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
158291** and so that is the default. But developers are encouranged to use
158292** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
158293*/
158294#if !defined(SQLITE_DQS3)
158295# define SQLITE_DQS3 3
158296#endif
158297#if (SQLITE_DQS3&1)==1
158298 | SQLITE_DqsDML0x40000000
158299#endif
158300#if (SQLITE_DQS3&2)==2
158301 | SQLITE_DqsDDL0x20000000
158302#endif
158303
158304#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
158305 | SQLITE_AutoIndex0x00008000
158306#endif
158307#if SQLITE_DEFAULT_CKPTFULLFSYNC
158308 | SQLITE_CkptFullFSync0x00000010
158309#endif
158310#if SQLITE_DEFAULT_FILE_FORMAT4<4
158311 | SQLITE_LegacyFileFmt0x00000002
158312#endif
158313#ifdef SQLITE_ENABLE_LOAD_EXTENSION
158314 | SQLITE_LoadExtension0x00010000
158315#endif
158316#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS0
158317 | SQLITE_RecTriggers0x00002000
158318#endif
158319#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
158320 | SQLITE_ForeignKeys0x00004000
158321#endif
158322#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
158323 | SQLITE_ReverseOrder0x00001000
158324#endif
158325#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
158326 | SQLITE_CellSizeCk0x00200000
158327#endif
158328#if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
158329 | SQLITE_Fts3Tokenizer0x00400000
158330#endif
158331#if defined(SQLITE_ENABLE_QPSG)
158332 | SQLITE_EnableQPSG0x00800000
158333#endif
158334#if defined(SQLITE_DEFAULT_DEFENSIVE)
158335 | SQLITE_Defensive0x10000000
158336#endif
158337 ;
158338 sqlite3HashInit(&db->aCollSeq);
158339#ifndef SQLITE_OMIT_VIRTUALTABLE
158340 sqlite3HashInit(&db->aModule);
158341#endif
158342
158343 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
158344 ** and UTF-16, so add a version for each to avoid any unnecessary
158345 ** conversions. The only error that can occur here is a malloc() failure.
158346 **
158347 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
158348 ** functions:
158349 */
158350 createCollation(db, sqlite3StrBINARY, SQLITE_UTF81, 0, binCollFunc, 0);
158351 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE3, 0, binCollFunc, 0);
158352 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE2, 0, binCollFunc, 0);
158353 createCollation(db, "NOCASE", SQLITE_UTF81, 0, nocaseCollatingFunc, 0);
158354 createCollation(db, "RTRIM", SQLITE_UTF81, 0, rtrimCollFunc, 0);
158355 if( db->mallocFailed ){
158356 goto opendb_out;
158357 }
158358 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
158359 ** strings is BINARY.
158360 */
158361 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF81, sqlite3StrBINARY, 0);
158362 assert( db->pDfltColl!=0 )((void) (0));
158363
158364 /* Parse the filename/URI argument
158365 **
158366 ** Only allow sensible combinations of bits in the flags argument.
158367 ** Throw an error if any non-sense combination is used. If we
158368 ** do not block illegal combinations here, it could trigger
158369 ** assert() statements in deeper layers. Sensible combinations
158370 ** are:
158371 **
158372 ** 1: SQLITE_OPEN_READONLY
158373 ** 2: SQLITE_OPEN_READWRITE
158374 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
158375 */
158376 db->openFlags = flags;
158377 assert( SQLITE_OPEN_READONLY == 0x01 )((void) (0));
158378 assert( SQLITE_OPEN_READWRITE == 0x02 )((void) (0));
158379 assert( SQLITE_OPEN_CREATE == 0x04 )((void) (0));
158380 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
158381 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
158382 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
158383 if( ((1<<(flags&7)) & 0x46)==0 ){
158384 rc = SQLITE_MISUSE_BKPTsqlite3MisuseError(158384); /* IMP: R-65497-44594 */
158385 }else{
158386 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
158387 }
158388 if( rc!=SQLITE_OK0 ){
158389 if( rc==SQLITE_NOMEM7 ) sqlite3OomFault(db);
158390 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
158391 sqlite3_free(zErrMsg);
158392 goto opendb_out;
158393 }
158394
158395 /* Open the backend database driver */
158396 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
158397 flags | SQLITE_OPEN_MAIN_DB0x00000100);
158398 if( rc!=SQLITE_OK0 ){
158399 if( rc==SQLITE_IOERR_NOMEM(10 | (12<<8)) ){
158400 rc = SQLITE_NOMEM_BKPT7;
158401 }
158402 sqlite3Error(db, rc);
158403 goto opendb_out;
158404 }
158405 sqlite3BtreeEnter(db->aDb[0].pBt);
158406 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
158407 if( !db->mallocFailed ) ENC(db)((db)->enc) = SCHEMA_ENC(db)((db)->aDb[0].pSchema->enc);
158408 sqlite3BtreeLeave(db->aDb[0].pBt);
158409 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
158410
158411 /* The default safety_level for the main database is FULL; for the temp
158412 ** database it is OFF. This matches the pager layer defaults.
158413 */
158414 db->aDb[0].zDbSName = "main";
158415 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS2+1;
158416 db->aDb[1].zDbSName = "temp";
158417 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF0x01;
158418
158419 db->magic = SQLITE_MAGIC_OPEN0xa029a697;
158420 if( db->mallocFailed ){
158421 goto opendb_out;
158422 }
158423
158424 /* Register all built-in functions, but do not attempt to read the
158425 ** database schema yet. This is delayed until the first time the database
158426 ** is accessed.
158427 */
158428 sqlite3Error(db, SQLITE_OK0);
158429 sqlite3RegisterPerConnectionBuiltinFunctions(db);
158430 rc = sqlite3_errcode(db);
158431
158432#ifdef SQLITE_ENABLE_FTS5
158433 /* Register any built-in FTS5 module before loading the automatic
158434 ** extensions. This allows automatic extensions to register FTS5
158435 ** tokenizers and auxiliary functions. */
158436 if( !db->mallocFailed && rc==SQLITE_OK0 ){
158437 rc = sqlite3Fts5Init(db);
158438 }
158439#endif
158440
158441 /* Load automatic extensions - extensions that have been registered
158442 ** using the sqlite3_automatic_extension() API.
158443 */
158444 if( rc==SQLITE_OK0 ){
158445 sqlite3AutoLoadExtensions(db);
158446 rc = sqlite3_errcode(db);
158447 if( rc!=SQLITE_OK0 ){
158448 goto opendb_out;
158449 }
158450 }
158451
158452#ifdef SQLITE_ENABLE_FTS1
158453 if( !db->mallocFailed ){
158454 extern int sqlite3Fts1Init(sqlite3*);
158455 rc = sqlite3Fts1Init(db);
158456 }
158457#endif
158458
158459#ifdef SQLITE_ENABLE_FTS2
158460 if( !db->mallocFailed && rc==SQLITE_OK0 ){
158461 extern int sqlite3Fts2Init(sqlite3*);
158462 rc = sqlite3Fts2Init(db);
158463 }
158464#endif
158465
158466#ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */
158467 if( !db->mallocFailed && rc==SQLITE_OK0 ){
158468 rc = sqlite3Fts3Init(db);
158469 }
158470#endif
158471
158472#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
158473 if( !db->mallocFailed && rc==SQLITE_OK0 ){
158474 rc = sqlite3IcuInit(db);
158475 }
158476#endif
158477
158478#ifdef SQLITE_ENABLE_RTREE
158479 if( !db->mallocFailed && rc==SQLITE_OK0){
158480 rc = sqlite3RtreeInit(db);
158481 }
158482#endif
158483
158484#ifdef SQLITE_ENABLE_DBPAGE_VTAB
158485 if( !db->mallocFailed && rc==SQLITE_OK0){
158486 rc = sqlite3DbpageRegister(db);
158487 }
158488#endif
158489
158490#ifdef SQLITE_ENABLE_DBSTAT_VTAB
158491 if( !db->mallocFailed && rc==SQLITE_OK0){
158492 rc = sqlite3DbstatRegister(db);
158493 }
158494#endif
158495
158496#ifdef SQLITE_ENABLE_JSON1
158497 if( !db->mallocFailed && rc==SQLITE_OK0){
158498 rc = sqlite3Json1Init(db);
158499 }
158500#endif
158501
158502#ifdef SQLITE_ENABLE_STMTVTAB
158503 if( !db->mallocFailed && rc==SQLITE_OK0){
158504 rc = sqlite3StmtVtabInit(db);
158505 }
158506#endif
158507
158508 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
158509 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
158510 ** mode. Doing nothing at all also makes NORMAL the default.
158511 */
158512#ifdef SQLITE_DEFAULT_LOCKING_MODE
158513 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
158514 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
158515 SQLITE_DEFAULT_LOCKING_MODE);
158516#endif
158517
158518 if( rc ) sqlite3Error(db, rc);
158519
158520 /* Enable the lookaside-malloc subsystem */
158521 setupLookaside(db, 0, sqlite3GlobalConfigsqlite3Config.szLookaside,
158522 sqlite3GlobalConfigsqlite3Config.nLookaside);
158523
158524 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT1000);
158525
158526opendb_out:
158527 if( db ){
158528 assert( db->mutex!=0 || isThreadsafe==0((void) (0))
158529 || sqlite3GlobalConfig.bFullMutex==0 )((void) (0));
158530 sqlite3_mutex_leave(db->mutex);
158531 }
158532 rc = sqlite3_errcode(db);
158533 assert( db!=0 || rc==SQLITE_NOMEM )((void) (0));
158534 if( rc==SQLITE_NOMEM7 ){
158535 sqlite3_close(db);
158536 db = 0;
158537 }else if( rc!=SQLITE_OK0 ){
158538 db->magic = SQLITE_MAGIC_SICK0x4b771290;
158539 }
158540 *ppDb = db;
158541#ifdef SQLITE_ENABLE_SQLLOG
158542 if( sqlite3GlobalConfigsqlite3Config.xSqllog ){
158543 /* Opening a db handle. Fourth parameter is passed 0. */
158544 void *pArg = sqlite3GlobalConfigsqlite3Config.pSqllogArg;
158545 sqlite3GlobalConfigsqlite3Config.xSqllog(pArg, db, zFilename, 0);
158546 }
158547#endif
158548#if defined(SQLITE_HAS_CODEC)
158549 if( rc==SQLITE_OK0 ) sqlite3CodecQueryParameters(db, 0, zOpen)0;
158550#endif
158551 sqlite3_free(zOpen);
158552 return rc & 0xff;
158553}
158554
158555
158556/*
158557** Open a new database handle.
158558*/
158559SQLITE_API int sqlite3_open(
158560 const char *zFilename,
158561 sqlite3 **ppDb
158562){
158563 return openDatabase(zFilename, ppDb,
158564 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004, 0);
158565}
158566SQLITE_API int sqlite3_open_v2(
158567 const char *filename, /* Database filename (UTF-8) */
158568 sqlite3 **ppDb, /* OUT: SQLite db handle */
158569 int flags, /* Flags */
158570 const char *zVfs /* Name of VFS module to use */
158571){
158572 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
158573}
158574
158575#ifndef SQLITE_OMIT_UTF16
158576/*
158577** Open a new database handle.
158578*/
158579SQLITE_API int sqlite3_open16(
158580 const void *zFilename,
158581 sqlite3 **ppDb
158582){
158583 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
158584 sqlite3_value *pVal;
158585 int rc;
158586
158587#ifdef SQLITE_ENABLE_API_ARMOR
158588 if( ppDb==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158588);
158589#endif
158590 *ppDb = 0;
158591#ifndef SQLITE_OMIT_AUTOINIT
158592 rc = sqlite3_initialize();
158593 if( rc ) return rc;
158594#endif
158595 if( zFilename==0 ) zFilename = "\000\000";
158596 pVal = sqlite3ValueNew(0);
158597 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE2, SQLITE_STATIC((sqlite3_destructor_type)0));
158598 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF81);
158599 if( zFilename8 ){
158600 rc = openDatabase(zFilename8, ppDb,
158601 SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004, 0);
158602 assert( *ppDb || rc==SQLITE_NOMEM )((void) (0));
158603 if( rc==SQLITE_OK0 && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded)(((*ppDb)->aDb[0].pSchema->schemaFlags&(0x0001))==(
0x0001))
){
158604 SCHEMA_ENC(*ppDb)((*ppDb)->aDb[0].pSchema->enc) = ENC(*ppDb)((*ppDb)->enc) = SQLITE_UTF16NATIVE2;
158605 }
158606 }else{
158607 rc = SQLITE_NOMEM_BKPT7;
158608 }
158609 sqlite3ValueFree(pVal);
158610
158611 return rc & 0xff;
158612}
158613#endif /* SQLITE_OMIT_UTF16 */
158614
158615/*
158616** Register a new collation sequence with the database handle db.
158617*/
158618SQLITE_API int sqlite3_create_collation(
158619 sqlite3* db,
158620 const char *zName,
158621 int enc,
158622 void* pCtx,
158623 int(*xCompare)(void*,int,const void*,int,const void*)
158624){
158625 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
158626}
158627
158628/*
158629** Register a new collation sequence with the database handle db.
158630*/
158631SQLITE_API int sqlite3_create_collation_v2(
158632 sqlite3* db,
158633 const char *zName,
158634 int enc,
158635 void* pCtx,
158636 int(*xCompare)(void*,int,const void*,int,const void*),
158637 void(*xDel)(void*)
158638){
158639 int rc;
158640
158641#ifdef SQLITE_ENABLE_API_ARMOR
158642 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158642);
158643#endif
158644 sqlite3_mutex_enter(db->mutex);
158645 assert( !db->mallocFailed )((void) (0));
158646 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
158647 rc = sqlite3ApiExit(db, rc);
158648 sqlite3_mutex_leave(db->mutex);
158649 return rc;
158650}
158651
158652#ifndef SQLITE_OMIT_UTF16
158653/*
158654** Register a new collation sequence with the database handle db.
158655*/
158656SQLITE_API int sqlite3_create_collation16(
158657 sqlite3* db,
158658 const void *zName,
158659 int enc,
158660 void* pCtx,
158661 int(*xCompare)(void*,int,const void*,int,const void*)
158662){
158663 int rc = SQLITE_OK0;
158664 char *zName8;
158665
158666#ifdef SQLITE_ENABLE_API_ARMOR
158667 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158667);
158668#endif
158669 sqlite3_mutex_enter(db->mutex);
158670 assert( !db->mallocFailed )((void) (0));
158671 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE2);
158672 if( zName8 ){
158673 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
158674 sqlite3DbFree(db, zName8);
158675 }
158676 rc = sqlite3ApiExit(db, rc);
158677 sqlite3_mutex_leave(db->mutex);
158678 return rc;
158679}
158680#endif /* SQLITE_OMIT_UTF16 */
158681
158682/*
158683** Register a collation sequence factory callback with the database handle
158684** db. Replace any previously installed collation sequence factory.
158685*/
158686SQLITE_API int sqlite3_collation_needed(
158687 sqlite3 *db,
158688 void *pCollNeededArg,
158689 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
158690){
158691#ifdef SQLITE_ENABLE_API_ARMOR
158692 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158692);
158693#endif
158694 sqlite3_mutex_enter(db->mutex);
158695 db->xCollNeeded = xCollNeeded;
158696 db->xCollNeeded16 = 0;
158697 db->pCollNeededArg = pCollNeededArg;
158698 sqlite3_mutex_leave(db->mutex);
158699 return SQLITE_OK0;
158700}
158701
158702#ifndef SQLITE_OMIT_UTF16
158703/*
158704** Register a collation sequence factory callback with the database handle
158705** db. Replace any previously installed collation sequence factory.
158706*/
158707SQLITE_API int sqlite3_collation_needed16(
158708 sqlite3 *db,
158709 void *pCollNeededArg,
158710 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
158711){
158712#ifdef SQLITE_ENABLE_API_ARMOR
158713 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158713);
158714#endif
158715 sqlite3_mutex_enter(db->mutex);
158716 db->xCollNeeded = 0;
158717 db->xCollNeeded16 = xCollNeeded16;
158718 db->pCollNeededArg = pCollNeededArg;
158719 sqlite3_mutex_leave(db->mutex);
158720 return SQLITE_OK0;
158721}
158722#endif /* SQLITE_OMIT_UTF16 */
158723
158724#ifndef SQLITE_OMIT_DEPRECATED
158725/*
158726** This function is now an anachronism. It used to be used to recover from a
158727** malloc() failure, but SQLite now does this automatically.
158728*/
158729SQLITE_API int sqlite3_global_recover(void){
158730 return SQLITE_OK0;
158731}
158732#endif
158733
158734/*
158735** Test to see whether or not the database connection is in autocommit
158736** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
158737** by default. Autocommit is disabled by a BEGIN statement and reenabled
158738** by the next COMMIT or ROLLBACK.
158739*/
158740SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
158741#ifdef SQLITE_ENABLE_API_ARMOR
158742 if( !sqlite3SafetyCheckOk(db) ){
158743 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(158743);
158744 return 0;
158745 }
158746#endif
158747 return db->autoCommit;
158748}
158749
158750/*
158751** The following routines are substitutes for constants SQLITE_CORRUPT,
158752** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
158753** constants. They serve two purposes:
158754**
158755** 1. Serve as a convenient place to set a breakpoint in a debugger
158756** to detect when version error conditions occurs.
158757**
158758** 2. Invoke sqlite3_log() to provide the source code location where
158759** a low-level error is first detected.
158760*/
158761SQLITE_PRIVATEstatic int sqlite3ReportError(int iErr, int lineno, const char *zType){
158762 sqlite3_log(iErr, "%s at line %d of [%.10s]",
158763 zType, lineno, 20+sqlite3_sourceid());
158764 return iErr;
158765}
158766SQLITE_PRIVATEstatic int sqlite3CorruptError(int lineno){
158767 testcase( sqlite3GlobalConfig.xLog!=0 );
158768 return sqlite3ReportError(SQLITE_CORRUPT11, lineno, "database corruption");
158769}
158770SQLITE_PRIVATEstatic int sqlite3MisuseError(int lineno){
158771 testcase( sqlite3GlobalConfig.xLog!=0 );
158772 return sqlite3ReportError(SQLITE_MISUSE21, lineno, "misuse");
158773}
158774SQLITE_PRIVATEstatic int sqlite3CantopenError(int lineno){
158775 testcase( sqlite3GlobalConfig.xLog!=0 );
158776 return sqlite3ReportError(SQLITE_CANTOPEN14, lineno, "cannot open file");
158777}
158778#ifdef SQLITE_DEBUG
158779SQLITE_PRIVATEstatic int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
158780 char zMsg[100];
158781 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
158782 testcase( sqlite3GlobalConfig.xLog!=0 );
158783 return sqlite3ReportError(SQLITE_CORRUPT11, lineno, zMsg);
158784}
158785SQLITE_PRIVATEstatic int sqlite3NomemError(int lineno){
158786 testcase( sqlite3GlobalConfig.xLog!=0 );
158787 return sqlite3ReportError(SQLITE_NOMEM7, lineno, "OOM");
158788}
158789SQLITE_PRIVATEstatic int sqlite3IoerrnomemError(int lineno){
158790 testcase( sqlite3GlobalConfig.xLog!=0 );
158791 return sqlite3ReportError(SQLITE_IOERR_NOMEM(10 | (12<<8)), lineno, "I/O OOM error");
158792}
158793#endif
158794
158795#ifndef SQLITE_OMIT_DEPRECATED
158796/*
158797** This is a convenience routine that makes sure that all thread-specific
158798** data for this thread has been deallocated.
158799**
158800** SQLite no longer uses thread-specific data so this routine is now a
158801** no-op. It is retained for historical compatibility.
158802*/
158803SQLITE_API void sqlite3_thread_cleanup(void){
158804}
158805#endif
158806
158807/*
158808** Return meta information about a specific column of a database table.
158809** See comment in sqlite3.h (sqlite.h.in) for details.
158810*/
158811SQLITE_API int sqlite3_table_column_metadata(
158812 sqlite3 *db, /* Connection handle */
158813 const char *zDbName, /* Database name or NULL */
158814 const char *zTableName, /* Table name */
158815 const char *zColumnName, /* Column name */
158816 char const **pzDataType, /* OUTPUT: Declared data type */
158817 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
158818 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
158819 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
158820 int *pAutoinc /* OUTPUT: True if column is auto-increment */
158821){
158822 int rc;
158823 char *zErrMsg = 0;
158824 Table *pTab = 0;
158825 Column *pCol = 0;
158826 int iCol = 0;
158827 char const *zDataType = 0;
158828 char const *zCollSeq = 0;
158829 int notnull = 0;
158830 int primarykey = 0;
158831 int autoinc = 0;
158832
158833
158834#ifdef SQLITE_ENABLE_API_ARMOR
158835 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
158836 return SQLITE_MISUSE_BKPTsqlite3MisuseError(158836);
158837 }
158838#endif
158839
158840 /* Ensure the database schema has been loaded */
158841 sqlite3_mutex_enter(db->mutex);
158842 sqlite3BtreeEnterAll(db);
158843 rc = sqlite3Init(db, &zErrMsg);
158844 if( SQLITE_OK0!=rc ){
158845 goto error_out;
158846 }
158847
158848 /* Locate the table in question */
158849 pTab = sqlite3FindTable(db, zTableName, zDbName);
158850 if( !pTab || pTab->pSelect ){
158851 pTab = 0;
158852 goto error_out;
158853 }
158854
158855 /* Find the column for which info is requested */
158856 if( zColumnName==0 ){
158857 /* Query for existance of table only */
158858 }else{
158859 for(iCol=0; iCol<pTab->nCol; iCol++){
158860 pCol = &pTab->aCol[iCol];
158861 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
158862 break;
158863 }
158864 }
158865 if( iCol==pTab->nCol ){
158866 if( HasRowid(pTab)(((pTab)->tabFlags & 0x0020)==0) && sqlite3IsRowid(zColumnName) ){
158867 iCol = pTab->iPKey;
158868 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
158869 }else{
158870 pTab = 0;
158871 goto error_out;
158872 }
158873 }
158874 }
158875
158876 /* The following block stores the meta information that will be returned
158877 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
158878 ** and autoinc. At this point there are two possibilities:
158879 **
158880 ** 1. The specified column name was rowid", "oid" or "_rowid_"
158881 ** and there is no explicitly declared IPK column.
158882 **
158883 ** 2. The table is not a view and the column name identified an
158884 ** explicitly declared column. Copy meta information from *pCol.
158885 */
158886 if( pCol ){
158887 zDataType = sqlite3ColumnType(pCol,0);
158888 zCollSeq = pCol->zColl;
158889 notnull = pCol->notNull!=0;
158890 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY0x0001)!=0;
158891 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement0x0008)!=0;
158892 }else{
158893 zDataType = "INTEGER";
158894 primarykey = 1;
158895 }
158896 if( !zCollSeq ){
158897 zCollSeq = sqlite3StrBINARY;
158898 }
158899
158900error_out:
158901 sqlite3BtreeLeaveAll(db);
158902
158903 /* Whether the function call succeeded or failed, set the output parameters
158904 ** to whatever their local counterparts contain. If an error did occur,
158905 ** this has the effect of zeroing all output parameters.
158906 */
158907 if( pzDataType ) *pzDataType = zDataType;
158908 if( pzCollSeq ) *pzCollSeq = zCollSeq;
158909 if( pNotNull ) *pNotNull = notnull;
158910 if( pPrimaryKey ) *pPrimaryKey = primarykey;
158911 if( pAutoinc ) *pAutoinc = autoinc;
158912
158913 if( SQLITE_OK0==rc && !pTab ){
158914 sqlite3DbFree(db, zErrMsg);
158915 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
158916 zColumnName);
158917 rc = SQLITE_ERROR1;
158918 }
158919 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
158920 sqlite3DbFree(db, zErrMsg);
158921 rc = sqlite3ApiExit(db, rc);
158922 sqlite3_mutex_leave(db->mutex);
158923 return rc;
158924}
158925
158926/*
158927** Sleep for a little while. Return the amount of time slept.
158928*/
158929SQLITE_API int sqlite3_sleep(int ms){
158930 sqlite3_vfs *pVfs;
158931 int rc;
158932 pVfs = sqlite3_vfs_find(0);
158933 if( pVfs==0 ) return 0;
158934
158935 /* This function works in milliseconds, but the underlying OsSleep()
158936 ** API uses microseconds. Hence the 1000's.
158937 */
158938 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
158939 return rc;
158940}
158941
158942/*
158943** Enable or disable the extended result codes.
158944*/
158945SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
158946#ifdef SQLITE_ENABLE_API_ARMOR
158947 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158947);
158948#endif
158949 sqlite3_mutex_enter(db->mutex);
158950 db->errMask = onoff ? 0xffffffff : 0xff;
158951 sqlite3_mutex_leave(db->mutex);
158952 return SQLITE_OK0;
158953}
158954
158955/*
158956** Invoke the xFileControl method on a particular database.
158957*/
158958SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
158959 int rc = SQLITE_ERROR1;
158960 Btree *pBtree;
158961
158962#ifdef SQLITE_ENABLE_API_ARMOR
158963 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPTsqlite3MisuseError(158963);
158964#endif
158965 sqlite3_mutex_enter(db->mutex);
158966 pBtree = sqlite3DbNameToBtree(db, zDbName);
158967 if( pBtree ){
158968 Pager *pPager;
158969 sqlite3_file *fd;
158970 sqlite3BtreeEnter(pBtree);
158971 pPager = sqlite3BtreePager(pBtree);
158972 assert( pPager!=0 )((void) (0));
158973 fd = sqlite3PagerFile(pPager);
158974 assert( fd!=0 )((void) (0));
158975 if( op==SQLITE_FCNTL_FILE_POINTER7 ){
158976 *(sqlite3_file**)pArg = fd;
158977 rc = SQLITE_OK0;
158978 }else if( op==SQLITE_FCNTL_VFS_POINTER27 ){
158979 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
158980 rc = SQLITE_OK0;
158981 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER28 ){
158982 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
158983 rc = SQLITE_OK0;
158984 }else if( op==SQLITE_FCNTL_DATA_VERSION35 ){
158985 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
158986 rc = SQLITE_OK0;
158987 }else{
158988 rc = sqlite3OsFileControl(fd, op, pArg);
158989 }
158990 sqlite3BtreeLeave(pBtree);
158991 }
158992 sqlite3_mutex_leave(db->mutex);
158993 return rc;
158994}
158995
158996/*
158997** Interface to the testing logic.
158998*/
158999SQLITE_API int sqlite3_test_control(int op, ...){
159000 int rc = 0;
159001#ifdef SQLITE_UNTESTABLE
159002 UNUSED_PARAMETER(op)(void)(op);
159003#else
159004 va_list ap;
159005 va_start(ap, op)__builtin_va_start(ap, op);
159006 switch( op ){
159007
159008 /*
159009 ** Save the current state of the PRNG.
159010 */
159011 case SQLITE_TESTCTRL_PRNG_SAVE5: {
159012 sqlite3PrngSaveState();
159013 break;
159014 }
159015
159016 /*
159017 ** Restore the state of the PRNG to the last state saved using
159018 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
159019 ** this verb acts like PRNG_RESET.
159020 */
159021 case SQLITE_TESTCTRL_PRNG_RESTORE6: {
159022 sqlite3PrngRestoreState();
159023 break;
159024 }
159025
159026 /*
159027 ** Reset the PRNG back to its uninitialized state. The next call
159028 ** to sqlite3_randomness() will reseed the PRNG using a single call
159029 ** to the xRandomness method of the default VFS.
159030 */
159031 case SQLITE_TESTCTRL_PRNG_RESET7: {
159032 sqlite3_randomness(0,0);
159033 break;
159034 }
159035
159036 /*
159037 ** sqlite3_test_control(BITVEC_TEST, size, program)
159038 **
159039 ** Run a test against a Bitvec object of size. The program argument
159040 ** is an array of integers that defines the test. Return -1 on a
159041 ** memory allocation error, 0 on success, or non-zero for an error.
159042 ** See the sqlite3BitvecBuiltinTest() for additional information.
159043 */
159044 case SQLITE_TESTCTRL_BITVEC_TEST8: {
159045 int sz = va_arg(ap, int)__builtin_va_arg(ap, int);
159046 int *aProg = va_arg(ap, int*)__builtin_va_arg(ap, int*);
159047 rc = sqlite3BitvecBuiltinTest(sz, aProg);
159048 break;
159049 }
159050
159051 /*
159052 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
159053 **
159054 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
159055 ** if xCallback is not NULL.
159056 **
159057 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
159058 ** is called immediately after installing the new callback and the return
159059 ** value from sqlite3FaultSim(0) becomes the return from
159060 ** sqlite3_test_control().
159061 */
159062 case SQLITE_TESTCTRL_FAULT_INSTALL9: {
159063 /* MSVC is picky about pulling func ptrs from va lists.
159064 ** http://support.microsoft.com/kb/47961
159065 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
159066 */
159067 typedef int(*TESTCALLBACKFUNC_t)(int);
159068 sqlite3GlobalConfigsqlite3Config.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t)__builtin_va_arg(ap, TESTCALLBACKFUNC_t);
159069 rc = sqlite3FaultSim(0);
159070 break;
159071 }
159072
159073 /*
159074 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
159075 **
159076 ** Register hooks to call to indicate which malloc() failures
159077 ** are benign.
159078 */
159079 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS10: {
159080 typedef void (*void_function)(void);
159081 void_function xBenignBegin;
159082 void_function xBenignEnd;
159083 xBenignBegin = va_arg(ap, void_function)__builtin_va_arg(ap, void_function);
159084 xBenignEnd = va_arg(ap, void_function)__builtin_va_arg(ap, void_function);
159085 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
159086 break;
159087 }
159088
159089 /*
159090 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
159091 **
159092 ** Set the PENDING byte to the value in the argument, if X>0.
159093 ** Make no changes if X==0. Return the value of the pending byte
159094 ** as it existing before this routine was called.
159095 **
159096 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
159097 ** an incompatible database file format. Changing the PENDING byte
159098 ** while any database connection is open results in undefined and
159099 ** deleterious behavior.
159100 */
159101 case SQLITE_TESTCTRL_PENDING_BYTE11: {
159102 rc = PENDING_BYTEsqlite3PendingByte;
159103#ifndef SQLITE_OMIT_WSD
159104 {
159105 unsigned int newVal = va_arg(ap, unsigned int)__builtin_va_arg(ap, unsigned int);
159106 if( newVal ) sqlite3PendingByte = newVal;
159107 }
159108#endif
159109 break;
159110 }
159111
159112 /*
159113 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
159114 **
159115 ** This action provides a run-time test to see whether or not
159116 ** assert() was enabled at compile-time. If X is true and assert()
159117 ** is enabled, then the return value is true. If X is true and
159118 ** assert() is disabled, then the return value is zero. If X is
159119 ** false and assert() is enabled, then the assertion fires and the
159120 ** process aborts. If X is false and assert() is disabled, then the
159121 ** return value is zero.
159122 */
159123 case SQLITE_TESTCTRL_ASSERT12: {
159124 volatile int x = 0;
159125 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 )((void) (0));
159126 rc = x;
159127 break;
159128 }
159129
159130
159131 /*
159132 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
159133 **
159134 ** This action provides a run-time test to see how the ALWAYS and
159135 ** NEVER macros were defined at compile-time.
159136 **
159137 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
159138 **
159139 ** The recommended test is X==2. If the return value is 2, that means
159140 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
159141 ** default setting. If the return value is 1, then ALWAYS() is either
159142 ** hard-coded to true or else it asserts if its argument is false.
159143 ** The first behavior (hard-coded to true) is the case if
159144 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
159145 ** behavior (assert if the argument to ALWAYS() is false) is the case if
159146 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
159147 **
159148 ** The run-time test procedure might look something like this:
159149 **
159150 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
159151 ** // ALWAYS() and NEVER() are no-op pass-through macros
159152 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
159153 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
159154 ** }else{
159155 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
159156 ** }
159157 */
159158 case SQLITE_TESTCTRL_ALWAYS13: {
159159 int x = va_arg(ap,int)__builtin_va_arg(ap, int);
159160 rc = x ? ALWAYS(x)(x) : 0;
159161 break;
159162 }
159163
159164 /*
159165 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
159166 **
159167 ** The integer returned reveals the byte-order of the computer on which
159168 ** SQLite is running:
159169 **
159170 ** 1 big-endian, determined at run-time
159171 ** 10 little-endian, determined at run-time
159172 ** 432101 big-endian, determined at compile-time
159173 ** 123410 little-endian, determined at compile-time
159174 */
159175 case SQLITE_TESTCTRL_BYTEORDER22: {
159176 rc = SQLITE_BYTEORDER1234*100 + SQLITE_LITTLEENDIAN1*10 + SQLITE_BIGENDIAN0;
159177 break;
159178 }
159179
159180 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
159181 **
159182 ** Set the nReserve size to N for the main database on the database
159183 ** connection db.
159184 */
159185 case SQLITE_TESTCTRL_RESERVE14: {
159186 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
159187 int x = va_arg(ap,int)__builtin_va_arg(ap, int);
159188 sqlite3_mutex_enter(db->mutex);
159189 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
159190 sqlite3_mutex_leave(db->mutex);
159191 break;
159192 }
159193
159194 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
159195 **
159196 ** Enable or disable various optimizations for testing purposes. The
159197 ** argument N is a bitmask of optimizations to be disabled. For normal
159198 ** operation N should be 0. The idea is that a test program (like the
159199 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
159200 ** with various optimizations disabled to verify that the same answer
159201 ** is obtained in every case.
159202 */
159203 case SQLITE_TESTCTRL_OPTIMIZATIONS15: {
159204 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
159205 db->dbOptFlags = (u16)(va_arg(ap, int)__builtin_va_arg(ap, int) & 0xffff);
159206 break;
159207 }
159208
159209 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
159210 **
159211 ** If parameter onoff is non-zero, subsequent calls to localtime()
159212 ** and its variants fail. If onoff is zero, undo this setting.
159213 */
159214 case SQLITE_TESTCTRL_LOCALTIME_FAULT18: {
159215 sqlite3GlobalConfigsqlite3Config.bLocaltimeFault = va_arg(ap, int)__builtin_va_arg(ap, int);
159216 break;
159217 }
159218
159219 /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCS, int onoff);
159220 **
159221 ** If parameter onoff is non-zero, internal-use-only SQL functions
159222 ** are visible to ordinary SQL. This is useful for testing but is
159223 ** unsafe because invalid parameters to those internal-use-only functions
159224 ** can result in crashes or segfaults.
159225 */
159226 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS17: {
159227 sqlite3GlobalConfigsqlite3Config.bInternalFunctions = va_arg(ap, int)__builtin_va_arg(ap, int);
159228 break;
159229 }
159230
159231 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
159232 **
159233 ** Set or clear a flag that indicates that the database file is always well-
159234 ** formed and never corrupt. This flag is clear by default, indicating that
159235 ** database files might have arbitrary corruption. Setting the flag during
159236 ** testing causes certain assert() statements in the code to be activated
159237 ** that demonstrat invariants on well-formed database files.
159238 */
159239 case SQLITE_TESTCTRL_NEVER_CORRUPT20: {
159240 sqlite3GlobalConfigsqlite3Config.neverCorrupt = va_arg(ap, int)__builtin_va_arg(ap, int);
159241 break;
159242 }
159243
159244 /* Set the threshold at which OP_Once counters reset back to zero.
159245 ** By default this is 0x7ffffffe (over 2 billion), but that value is
159246 ** too big to test in a reasonable amount of time, so this control is
159247 ** provided to set a small and easily reachable reset value.
159248 */
159249 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD19: {
159250 sqlite3GlobalConfigsqlite3Config.iOnceResetThreshold = va_arg(ap, int)__builtin_va_arg(ap, int);
159251 break;
159252 }
159253
159254 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
159255 **
159256 ** Set the VDBE coverage callback function to xCallback with context
159257 ** pointer ptr.
159258 */
159259 case SQLITE_TESTCTRL_VDBE_COVERAGE21: {
159260#ifdef SQLITE_VDBE_COVERAGE
159261 typedef void (*branch_callback)(void*,unsigned int,
159262 unsigned char,unsigned char);
159263 sqlite3GlobalConfigsqlite3Config.xVdbeBranch = va_arg(ap,branch_callback)__builtin_va_arg(ap, branch_callback);
159264 sqlite3GlobalConfigsqlite3Config.pVdbeBranchArg = va_arg(ap,void*)__builtin_va_arg(ap, void*);
159265#endif
159266 break;
159267 }
159268
159269 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
159270 case SQLITE_TESTCTRL_SORTER_MMAP24: {
159271 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
159272 db->nMaxSorterMmap = va_arg(ap, int)__builtin_va_arg(ap, int);
159273 break;
159274 }
159275
159276 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
159277 **
159278 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
159279 ** not.
159280 */
159281 case SQLITE_TESTCTRL_ISINIT23: {
159282 if( sqlite3GlobalConfigsqlite3Config.isInit==0 ) rc = SQLITE_ERROR1;
159283 break;
159284 }
159285
159286 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
159287 **
159288 ** This test control is used to create imposter tables. "db" is a pointer
159289 ** to the database connection. dbName is the database name (ex: "main" or
159290 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
159291 ** or off. "tnum" is the root page of the b-tree to which the imposter
159292 ** table should connect.
159293 **
159294 ** Enable imposter mode only when the schema has already been parsed. Then
159295 ** run a single CREATE TABLE statement to construct the imposter table in
159296 ** the parsed schema. Then turn imposter mode back off again.
159297 **
159298 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
159299 ** the schema to be reparsed the next time it is needed. This has the
159300 ** effect of erasing all imposter tables.
159301 */
159302 case SQLITE_TESTCTRL_IMPOSTER25: {
159303 sqlite3 *db = va_arg(ap, sqlite3*)__builtin_va_arg(ap, sqlite3*);
159304 sqlite3_mutex_enter(db->mutex);
159305 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*)__builtin_va_arg(ap, const char*));
159306 db->init.busy = db->init.imposterTable = va_arg(ap,int)__builtin_va_arg(ap, int);
159307 db->init.newTnum = va_arg(ap,int)__builtin_va_arg(ap, int);
159308 if( db->init.busy==0 && db->init.newTnum>0 ){
159309 sqlite3ResetAllSchemasOfConnection(db);
159310 }
159311 sqlite3_mutex_leave(db->mutex);
159312 break;
159313 }
159314
159315#if defined(YYCOVERAGE)
159316 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
159317 **
159318 ** This test control (only available when SQLite is compiled with
159319 ** -DYYCOVERAGE) writes a report onto "out" that shows all
159320 ** state/lookahead combinations in the parser state machine
159321 ** which are never exercised. If any state is missed, make the
159322 ** return code SQLITE_ERROR.
159323 */
159324 case SQLITE_TESTCTRL_PARSER_COVERAGE26: {
159325 FILE *out = va_arg(ap, FILE*)__builtin_va_arg(ap, FILE*);
159326 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR1;
159327 break;
159328 }
159329#endif /* defined(YYCOVERAGE) */
159330
159331 /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
159332 **
159333 ** This test-control causes the most recent sqlite3_result_int64() value
159334 ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally,
159335 ** MEM_IntReal values only arise during an INSERT operation of integer
159336 ** values into a REAL column, so they can be challenging to test. This
159337 ** test-control enables us to write an intreal() SQL function that can
159338 ** inject an intreal() value at arbitrary places in an SQL statement,
159339 ** for testing purposes.
159340 */
159341 case SQLITE_TESTCTRL_RESULT_INTREAL27: {
159342 sqlite3_context *pCtx = va_arg(ap, sqlite3_context*)__builtin_va_arg(ap, sqlite3_context*);
159343 sqlite3ResultIntReal(pCtx);
159344 break;
159345 }
159346 }
159347 va_end(ap)__builtin_va_end(ap);
159348#endif /* SQLITE_UNTESTABLE */
159349 return rc;
159350}
159351
159352/*
159353** This is a utility routine, useful to VFS implementations, that checks
159354** to see if a database file was a URI that contained a specific query
159355** parameter, and if so obtains the value of the query parameter.
159356**
159357** The zFilename argument is the filename pointer passed into the xOpen()
159358** method of a VFS implementation. The zParam argument is the name of the
159359** query parameter we seek. This routine returns the value of the zParam
159360** parameter if it exists. If the parameter does not exist, this routine
159361** returns a NULL pointer.
159362*/
159363SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
159364 if( zFilename==0 || zParam==0 ) return 0;
159365 zFilename += sqlite3Strlen30(zFilename) + 1;
159366 while( zFilename[0] ){
159367 int x = strcmp(zFilename, zParam);
159368 zFilename += sqlite3Strlen30(zFilename) + 1;
159369 if( x==0 ) return zFilename;
159370 zFilename += sqlite3Strlen30(zFilename) + 1;
159371 }
159372 return 0;
159373}
159374
159375/*
159376** Return a boolean value for a query parameter.
159377*/
159378SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
159379 const char *z = sqlite3_uri_parameter(zFilename, zParam);
159380 bDflt = bDflt!=0;
159381 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
159382}
159383
159384/*
159385** Return a 64-bit integer value for a query parameter.
159386*/
159387SQLITE_API sqlite3_int64 sqlite3_uri_int64(
159388 const char *zFilename, /* Filename as passed to xOpen */
159389 const char *zParam, /* URI parameter sought */
159390 sqlite3_int64 bDflt /* return if parameter is missing */
159391){
159392 const char *z = sqlite3_uri_parameter(zFilename, zParam);
159393 sqlite3_int64 v;
159394 if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
159395 bDflt = v;
159396 }
159397 return bDflt;
159398}
159399
159400/*
159401** Return the Btree pointer identified by zDbName. Return NULL if not found.
159402*/
159403SQLITE_PRIVATEstatic Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
159404 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
159405 return iDb<0 ? 0 : db->aDb[iDb].pBt;
159406}
159407
159408/*
159409** Return the filename of the database associated with a database
159410** connection.
159411*/
159412SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
159413 Btree *pBt;
159414#ifdef SQLITE_ENABLE_API_ARMOR
159415 if( !sqlite3SafetyCheckOk(db) ){
159416 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(159416);
159417 return 0;
159418 }
159419#endif
159420 pBt = sqlite3DbNameToBtree(db, zDbName);
159421 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
159422}
159423
159424/*
159425** Return 1 if database is read-only or 0 if read/write. Return -1 if
159426** no such database exists.
159427*/
159428SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
159429 Btree *pBt;
159430#ifdef SQLITE_ENABLE_API_ARMOR
159431 if( !sqlite3SafetyCheckOk(db) ){
159432 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(159432);
159433 return -1;
159434 }
159435#endif
159436 pBt = sqlite3DbNameToBtree(db, zDbName);
159437 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
159438}
159439
159440#ifdef SQLITE_ENABLE_SNAPSHOT
159441/*
159442** Obtain a snapshot handle for the snapshot of database zDb currently
159443** being read by handle db.
159444*/
159445SQLITE_API int sqlite3_snapshot_get(
159446 sqlite3 *db,
159447 const char *zDb,
159448 sqlite3_snapshot **ppSnapshot
159449){
159450 int rc = SQLITE_ERROR1;
159451#ifndef SQLITE_OMIT_WAL
159452
159453#ifdef SQLITE_ENABLE_API_ARMOR
159454 if( !sqlite3SafetyCheckOk(db) ){
159455 return SQLITE_MISUSE_BKPTsqlite3MisuseError(159455);
159456 }
159457#endif
159458 sqlite3_mutex_enter(db->mutex);
159459
159460 if( db->autoCommit==0 ){
159461 int iDb = sqlite3FindDbName(db, zDb);
159462 if( iDb==0 || iDb>1 ){
159463 Btree *pBt = db->aDb[iDb].pBt;
159464 if( 0==sqlite3BtreeIsInTrans(pBt) ){
159465 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
159466 if( rc==SQLITE_OK0 ){
159467 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
159468 }
159469 }
159470 }
159471 }
159472
159473 sqlite3_mutex_leave(db->mutex);
159474#endif /* SQLITE_OMIT_WAL */
159475 return rc;
159476}
159477
159478/*
159479** Open a read-transaction on the snapshot idendified by pSnapshot.
159480*/
159481SQLITE_API int sqlite3_snapshot_open(
159482 sqlite3 *db,
159483 const char *zDb,
159484 sqlite3_snapshot *pSnapshot
159485){
159486 int rc = SQLITE_ERROR1;
159487#ifndef SQLITE_OMIT_WAL
159488
159489#ifdef SQLITE_ENABLE_API_ARMOR
159490 if( !sqlite3SafetyCheckOk(db) ){
159491 return SQLITE_MISUSE_BKPTsqlite3MisuseError(159491);
159492 }
159493#endif
159494 sqlite3_mutex_enter(db->mutex);
159495 if( db->autoCommit==0 ){
159496 int iDb;
159497 iDb = sqlite3FindDbName(db, zDb);
159498 if( iDb==0 || iDb>1 ){
159499 Btree *pBt = db->aDb[iDb].pBt;
159500 if( sqlite3BtreeIsInTrans(pBt)==0 ){
159501 Pager *pPager = sqlite3BtreePager(pBt);
159502 int bUnlock = 0;
159503 if( sqlite3BtreeIsInReadTrans(pBt) ){
159504 if( db->nVdbeActive==0 ){
159505 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
159506 if( rc==SQLITE_OK0 ){
159507 bUnlock = 1;
159508 rc = sqlite3BtreeCommit(pBt);
159509 }
159510 }
159511 }else{
159512 rc = SQLITE_OK0;
159513 }
159514 if( rc==SQLITE_OK0 ){
159515 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
159516 }
159517 if( rc==SQLITE_OK0 ){
159518 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
159519 sqlite3PagerSnapshotOpen(pPager, 0);
159520 }
159521 if( bUnlock ){
159522 sqlite3PagerSnapshotUnlock(pPager);
159523 }
159524 }
159525 }
159526 }
159527
159528 sqlite3_mutex_leave(db->mutex);
159529#endif /* SQLITE_OMIT_WAL */
159530 return rc;
159531}
159532
159533/*
159534** Recover as many snapshots as possible from the wal file associated with
159535** schema zDb of database db.
159536*/
159537SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
159538 int rc = SQLITE_ERROR1;
159539 int iDb;
159540#ifndef SQLITE_OMIT_WAL
159541
159542#ifdef SQLITE_ENABLE_API_ARMOR
159543 if( !sqlite3SafetyCheckOk(db) ){
159544 return SQLITE_MISUSE_BKPTsqlite3MisuseError(159544);
159545 }
159546#endif
159547
159548 sqlite3_mutex_enter(db->mutex);
159549 iDb = sqlite3FindDbName(db, zDb);
159550 if( iDb==0 || iDb>1 ){
159551 Btree *pBt = db->aDb[iDb].pBt;
159552 if( 0==sqlite3BtreeIsInReadTrans(pBt) ){
159553 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
159554 if( rc==SQLITE_OK0 ){
159555 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
159556 sqlite3BtreeCommit(pBt);
159557 }
159558 }
159559 }
159560 sqlite3_mutex_leave(db->mutex);
159561#endif /* SQLITE_OMIT_WAL */
159562 return rc;
159563}
159564
159565/*
159566** Free a snapshot handle obtained from sqlite3_snapshot_get().
159567*/
159568SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
159569 sqlite3_free(pSnapshot);
159570}
159571#endif /* SQLITE_ENABLE_SNAPSHOT */
159572
159573#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
159574/*
159575** Given the name of a compile-time option, return true if that option
159576** was used and false if not.
159577**
159578** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
159579** is not required for a match.
159580*/
159581SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
159582 int i, n;
159583 int nOpt;
159584 const char **azCompileOpt;
159585
159586#if SQLITE_ENABLE_API_ARMOR
159587 if( zOptName==0 ){
159588 (void)SQLITE_MISUSE_BKPTsqlite3MisuseError(159588);
159589 return 0;
159590 }
159591#endif
159592
159593 azCompileOpt = sqlite3CompileOptions(&nOpt);
159594
159595 if( sqlite3StrNICmpsqlite3_strnicmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
159596 n = sqlite3Strlen30(zOptName);
159597
159598 /* Since nOpt is normally in single digits, a linear search is
159599 ** adequate. No need for a binary search. */
159600 for(i=0; i<nOpt; i++){
159601 if( sqlite3StrNICmpsqlite3_strnicmp(zOptName, azCompileOpt[i], n)==0
159602 && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
159603 ){
159604 return 1;
159605 }
159606 }
159607 return 0;
159608}
159609
159610/*
159611** Return the N-th compile-time option string. If N is out of range,
159612** return a NULL pointer.
159613*/
159614SQLITE_API const char *sqlite3_compileoption_get(int N){
159615 int nOpt;
159616 const char **azCompileOpt;
159617 azCompileOpt = sqlite3CompileOptions(&nOpt);
159618 if( N>=0 && N<nOpt ){
159619 return azCompileOpt[N];
159620 }
159621 return 0;
159622}
159623#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
159624
159625/************** End of main.c ************************************************/
159626/************** Begin file notify.c ******************************************/
159627/*
159628** 2009 March 3
159629**
159630** The author disclaims copyright to this source code. In place of
159631** a legal notice, here is a blessing:
159632**
159633** May you do good and not evil.
159634** May you find forgiveness for yourself and forgive others.
159635** May you share freely, never taking more than you give.
159636**
159637*************************************************************************
159638**
159639** This file contains the implementation of the sqlite3_unlock_notify()
159640** API method and its associated functionality.
159641*/
159642/* #include "sqliteInt.h" */
159643/* #include "btreeInt.h" */
159644
159645/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
159646#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
159647
159648/*
159649** Public interfaces:
159650**
159651** sqlite3ConnectionBlocked()
159652** sqlite3ConnectionUnlocked()
159653** sqlite3ConnectionClosed()
159654** sqlite3_unlock_notify()
159655*/
159656
159657#define assertMutexHeld() \
159658 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )((void) (0))
159659
159660/*
159661** Head of a linked list of all sqlite3 objects created by this process
159662** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
159663** is not NULL. This variable may only accessed while the STATIC_MASTER
159664** mutex is held.
159665*/
159666static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
159667
159668#ifndef NDEBUG1
159669/*
159670** This function is a complex assert() that verifies the following
159671** properties of the blocked connections list:
159672**
159673** 1) Each entry in the list has a non-NULL value for either
159674** pUnlockConnection or pBlockingConnection, or both.
159675**
159676** 2) All entries in the list that share a common value for
159677** xUnlockNotify are grouped together.
159678**
159679** 3) If the argument db is not NULL, then none of the entries in the
159680** blocked connections list have pUnlockConnection or pBlockingConnection
159681** set to db. This is used when closing connection db.
159682*/
159683static void checkListProperties(sqlite3 *db){
159684 sqlite3 *p;
159685 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
159686 int seen = 0;
159687 sqlite3 *p2;
159688
159689 /* Verify property (1) */
159690 assert( p->pUnlockConnection || p->pBlockingConnection )((void) (0));
159691
159692 /* Verify property (2) */
159693 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
159694 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
159695 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen )((void) (0));
159696 assert( db==0 || p->pUnlockConnection!=db )((void) (0));
159697 assert( db==0 || p->pBlockingConnection!=db )((void) (0));
159698 }
159699 }
159700}
159701#else
159702# define checkListProperties(x)
159703#endif
159704
159705/*
159706** Remove connection db from the blocked connections list. If connection
159707** db is not currently a part of the list, this function is a no-op.
159708*/
159709static void removeFromBlockedList(sqlite3 *db){
159710 sqlite3 **pp;
159711 assertMutexHeld();
159712 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
159713 if( *pp==db ){
159714 *pp = (*pp)->pNextBlocked;
159715 break;
159716 }
159717 }
159718}
159719
159720/*
159721** Add connection db to the blocked connections list. It is assumed
159722** that it is not already a part of the list.
159723*/
159724static void addToBlockedList(sqlite3 *db){
159725 sqlite3 **pp;
159726 assertMutexHeld();
159727 for(
159728 pp=&sqlite3BlockedList;
159729 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
159730 pp=&(*pp)->pNextBlocked
159731 );
159732 db->pNextBlocked = *pp;
159733 *pp = db;
159734}
159735
159736/*
159737** Obtain the STATIC_MASTER mutex.
159738*/
159739static void enterMutex(void){
159740 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2));
159741 checkListProperties(0);
159742}
159743
159744/*
159745** Release the STATIC_MASTER mutex.
159746*/
159747static void leaveMutex(void){
159748 assertMutexHeld();
159749 checkListProperties(0);
159750 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER2));
159751}
159752
159753/*
159754** Register an unlock-notify callback.
159755**
159756** This is called after connection "db" has attempted some operation
159757** but has received an SQLITE_LOCKED error because another connection
159758** (call it pOther) in the same process was busy using the same shared
159759** cache. pOther is found by looking at db->pBlockingConnection.
159760**
159761** If there is no blocking connection, the callback is invoked immediately,
159762** before this routine returns.
159763**
159764** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
159765** a deadlock.
159766**
159767** Otherwise, make arrangements to invoke xNotify when pOther drops
159768** its locks.
159769**
159770** Each call to this routine overrides any prior callbacks registered
159771** on the same "db". If xNotify==0 then any prior callbacks are immediately
159772** cancelled.
159773*/
159774SQLITE_API int sqlite3_unlock_notify(
159775 sqlite3 *db,
159776 void (*xNotify)(void **, int),
159777 void *pArg
159778){
159779 int rc = SQLITE_OK0;
159780
159781 sqlite3_mutex_enter(db->mutex);
159782 enterMutex();
159783
159784 if( xNotify==0 ){
159785 removeFromBlockedList(db);
159786 db->pBlockingConnection = 0;
159787 db->pUnlockConnection = 0;
159788 db->xUnlockNotify = 0;
159789 db->pUnlockArg = 0;
159790 }else if( 0==db->pBlockingConnection ){
159791 /* The blocking transaction has been concluded. Or there never was a
159792 ** blocking transaction. In either case, invoke the notify callback
159793 ** immediately.
159794 */
159795 xNotify(&pArg, 1);
159796 }else{
159797 sqlite3 *p;
159798
159799 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
159800 if( p ){
159801 rc = SQLITE_LOCKED6; /* Deadlock detected. */
159802 }else{
159803 db->pUnlockConnection = db->pBlockingConnection;
159804 db->xUnlockNotify = xNotify;
159805 db->pUnlockArg = pArg;
159806 removeFromBlockedList(db);
159807 addToBlockedList(db);
159808 }
159809 }
159810
159811 leaveMutex();
159812 assert( !db->mallocFailed )((void) (0));
159813 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0));
159814 sqlite3_mutex_leave(db->mutex);
159815 return rc;
159816}
159817
159818/*
159819** This function is called while stepping or preparing a statement
159820** associated with connection db. The operation will return SQLITE_LOCKED
159821** to the user because it requires a lock that will not be available
159822** until connection pBlocker concludes its current transaction.
159823*/
159824SQLITE_PRIVATEstatic void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
159825 enterMutex();
159826 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
159827 addToBlockedList(db);
159828 }
159829 db->pBlockingConnection = pBlocker;
159830 leaveMutex();
159831}
159832
159833/*
159834** This function is called when
159835** the transaction opened by database db has just finished. Locks held
159836** by database connection db have been released.
159837**
159838** This function loops through each entry in the blocked connections
159839** list and does the following:
159840**
159841** 1) If the sqlite3.pBlockingConnection member of a list entry is
159842** set to db, then set pBlockingConnection=0.
159843**
159844** 2) If the sqlite3.pUnlockConnection member of a list entry is
159845** set to db, then invoke the configured unlock-notify callback and
159846** set pUnlockConnection=0.
159847**
159848** 3) If the two steps above mean that pBlockingConnection==0 and
159849** pUnlockConnection==0, remove the entry from the blocked connections
159850** list.
159851*/
159852SQLITE_PRIVATEstatic void sqlite3ConnectionUnlocked(sqlite3 *db){
159853 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
159854 int nArg = 0; /* Number of entries in aArg[] */
159855 sqlite3 **pp; /* Iterator variable */
159856 void **aArg; /* Arguments to the unlock callback */
159857 void **aDyn = 0; /* Dynamically allocated space for aArg[] */
159858 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */
159859
159860 aArg = aStatic;
159861 enterMutex(); /* Enter STATIC_MASTER mutex */
159862
159863 /* This loop runs once for each entry in the blocked-connections list. */
159864 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
159865 sqlite3 *p = *pp;
159866
159867 /* Step 1. */
159868 if( p->pBlockingConnection==db ){
159869 p->pBlockingConnection = 0;
159870 }
159871
159872 /* Step 2. */
159873 if( p->pUnlockConnection==db ){
159874 assert( p->xUnlockNotify )((void) (0));
159875 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
159876 xUnlockNotify(aArg, nArg);
159877 nArg = 0;
159878 }
159879
159880 sqlite3BeginBenignMalloc();
159881 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) )((void) (0));
159882 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn )((void) (0));
159883 if( (!aDyn && nArg==(int)ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))))
159884 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
159885 ){
159886 /* The aArg[] array needs to grow. */
159887 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
159888 if( pNew ){
159889 memcpy(pNew, aArg, nArg*sizeof(void *));
159890 sqlite3_free(aDyn);
159891 aDyn = aArg = pNew;
159892 }else{
159893 /* This occurs when the array of context pointers that need to
159894 ** be passed to the unlock-notify callback is larger than the
159895 ** aStatic[] array allocated on the stack and the attempt to
159896 ** allocate a larger array from the heap has failed.
159897 **
159898 ** This is a difficult situation to handle. Returning an error
159899 ** code to the caller is insufficient, as even if an error code
159900 ** is returned the transaction on connection db will still be
159901 ** closed and the unlock-notify callbacks on blocked connections
159902 ** will go unissued. This might cause the application to wait
159903 ** indefinitely for an unlock-notify callback that will never
159904 ** arrive.
159905 **
159906 ** Instead, invoke the unlock-notify callback with the context
159907 ** array already accumulated. We can then clear the array and
159908 ** begin accumulating any further context pointers without
159909 ** requiring any dynamic allocation. This is sub-optimal because
159910 ** it means that instead of one callback with a large array of
159911 ** context pointers the application will receive two or more
159912 ** callbacks with smaller arrays of context pointers, which will
159913 ** reduce the applications ability to prioritize multiple
159914 ** connections. But it is the best that can be done under the
159915 ** circumstances.
159916 */
159917 xUnlockNotify(aArg, nArg);
159918 nArg = 0;
159919 }
159920 }
159921 sqlite3EndBenignMalloc();
159922
159923 aArg[nArg++] = p->pUnlockArg;
159924 xUnlockNotify = p->xUnlockNotify;
159925 p->pUnlockConnection = 0;
159926 p->xUnlockNotify = 0;
159927 p->pUnlockArg = 0;
159928 }
159929
159930 /* Step 3. */
159931 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
159932 /* Remove connection p from the blocked connections list. */
159933 *pp = p->pNextBlocked;
159934 p->pNextBlocked = 0;
159935 }else{
159936 pp = &p->pNextBlocked;
159937 }
159938 }
159939
159940 if( nArg!=0 ){
159941 xUnlockNotify(aArg, nArg);
159942 }
159943 sqlite3_free(aDyn);
159944 leaveMutex(); /* Leave STATIC_MASTER mutex */
159945}
159946
159947/*
159948** This is called when the database connection passed as an argument is
159949** being closed. The connection is removed from the blocked list.
159950*/
159951SQLITE_PRIVATEstatic void sqlite3ConnectionClosed(sqlite3 *db){
159952 sqlite3ConnectionUnlocked(db);
159953 enterMutex();
159954 removeFromBlockedList(db);
159955 checkListProperties(db);
159956 leaveMutex();
159957}
159958#endif
159959
159960/************** End of notify.c **********************************************/
159961/************** Begin file fts3.c ********************************************/
159962/*
159963** 2006 Oct 10
159964**
159965** The author disclaims copyright to this source code. In place of
159966** a legal notice, here is a blessing:
159967**
159968** May you do good and not evil.
159969** May you find forgiveness for yourself and forgive others.
159970** May you share freely, never taking more than you give.
159971**
159972******************************************************************************
159973**
159974** This is an SQLite module implementing full-text search.
159975*/
159976
159977/*
159978** The code in this file is only compiled if:
159979**
159980** * The FTS3 module is being built as an extension
159981** (in which case SQLITE_CORE is not defined), or
159982**
159983** * The FTS3 module is being built into the core of
159984** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
159985*/
159986
159987/* The full-text index is stored in a series of b+tree (-like)
159988** structures called segments which map terms to doclists. The
159989** structures are like b+trees in layout, but are constructed from the
159990** bottom up in optimal fashion and are not updatable. Since trees
159991** are built from the bottom up, things will be described from the
159992** bottom up.
159993**
159994**
159995**** Varints ****
159996** The basic unit of encoding is a variable-length integer called a
159997** varint. We encode variable-length integers in little-endian order
159998** using seven bits * per byte as follows:
159999**
160000** KEY:
160001** A = 0xxxxxxx 7 bits of data and one flag bit
160002** B = 1xxxxxxx 7 bits of data and one flag bit
160003**
160004** 7 bits - A
160005** 14 bits - BA
160006** 21 bits - BBA
160007** and so on.
160008**
160009** This is similar in concept to how sqlite encodes "varints" but
160010** the encoding is not the same. SQLite varints are big-endian
160011** are are limited to 9 bytes in length whereas FTS3 varints are
160012** little-endian and can be up to 10 bytes in length (in theory).
160013**
160014** Example encodings:
160015**
160016** 1: 0x01
160017** 127: 0x7f
160018** 128: 0x81 0x00
160019**
160020**
160021**** Document lists ****
160022** A doclist (document list) holds a docid-sorted list of hits for a
160023** given term. Doclists hold docids and associated token positions.
160024** A docid is the unique integer identifier for a single document.
160025** A position is the index of a word within the document. The first
160026** word of the document has a position of 0.
160027**
160028** FTS3 used to optionally store character offsets using a compile-time
160029** option. But that functionality is no longer supported.
160030**
160031** A doclist is stored like this:
160032**
160033** array {
160034** varint docid; (delta from previous doclist)
160035** array { (position list for column 0)
160036** varint position; (2 more than the delta from previous position)
160037** }
160038** array {
160039** varint POS_COLUMN; (marks start of position list for new column)
160040** varint column; (index of new column)
160041** array {
160042** varint position; (2 more than the delta from previous position)
160043** }
160044** }
160045** varint POS_END; (marks end of positions for this document.
160046** }
160047**
160048** Here, array { X } means zero or more occurrences of X, adjacent in
160049** memory. A "position" is an index of a token in the token stream
160050** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
160051** in the same logical place as the position element, and act as sentinals
160052** ending a position list array. POS_END is 0. POS_COLUMN is 1.
160053** The positions numbers are not stored literally but rather as two more
160054** than the difference from the prior position, or the just the position plus
160055** 2 for the first position. Example:
160056**
160057** label: A B C D E F G H I J K
160058** value: 123 5 9 1 1 14 35 0 234 72 0
160059**
160060** The 123 value is the first docid. For column zero in this document
160061** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
160062** at D signals the start of a new column; the 1 at E indicates that the
160063** new column is column number 1. There are two positions at 12 and 45
160064** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
160065** 234 at I is the delta to next docid (357). It has one position 70
160066** (72-2) and then terminates with the 0 at K.
160067**
160068** A "position-list" is the list of positions for multiple columns for
160069** a single docid. A "column-list" is the set of positions for a single
160070** column. Hence, a position-list consists of one or more column-lists,
160071** a document record consists of a docid followed by a position-list and
160072** a doclist consists of one or more document records.
160073**
160074** A bare doclist omits the position information, becoming an
160075** array of varint-encoded docids.
160076**
160077**** Segment leaf nodes ****
160078** Segment leaf nodes store terms and doclists, ordered by term. Leaf
160079** nodes are written using LeafWriter, and read using LeafReader (to
160080** iterate through a single leaf node's data) and LeavesReader (to
160081** iterate through a segment's entire leaf layer). Leaf nodes have
160082** the format:
160083**
160084** varint iHeight; (height from leaf level, always 0)
160085** varint nTerm; (length of first term)
160086** char pTerm[nTerm]; (content of first term)
160087** varint nDoclist; (length of term's associated doclist)
160088** char pDoclist[nDoclist]; (content of doclist)
160089** array {
160090** (further terms are delta-encoded)
160091** varint nPrefix; (length of prefix shared with previous term)
160092** varint nSuffix; (length of unshared suffix)
160093** char pTermSuffix[nSuffix];(unshared suffix of next term)
160094** varint nDoclist; (length of term's associated doclist)
160095** char pDoclist[nDoclist]; (content of doclist)
160096** }
160097**
160098** Here, array { X } means zero or more occurrences of X, adjacent in
160099** memory.
160100**
160101** Leaf nodes are broken into blocks which are stored contiguously in
160102** the %_segments table in sorted order. This means that when the end
160103** of a node is reached, the next term is in the node with the next
160104** greater node id.
160105**
160106** New data is spilled to a new leaf node when the current node
160107** exceeds LEAF_MAX bytes (default 2048). New data which itself is
160108** larger than STANDALONE_MIN (default 1024) is placed in a standalone
160109** node (a leaf node with a single term and doclist). The goal of
160110** these settings is to pack together groups of small doclists while
160111** making it efficient to directly access large doclists. The
160112** assumption is that large doclists represent terms which are more
160113** likely to be query targets.
160114**
160115** TODO(shess) It may be useful for blocking decisions to be more
160116** dynamic. For instance, it may make more sense to have a 2.5k leaf
160117** node rather than splitting into 2k and .5k nodes. My intuition is
160118** that this might extend through 2x or 4x the pagesize.
160119**
160120**
160121**** Segment interior nodes ****
160122** Segment interior nodes store blockids for subtree nodes and terms
160123** to describe what data is stored by the each subtree. Interior
160124** nodes are written using InteriorWriter, and read using
160125** InteriorReader. InteriorWriters are created as needed when
160126** SegmentWriter creates new leaf nodes, or when an interior node
160127** itself grows too big and must be split. The format of interior
160128** nodes:
160129**
160130** varint iHeight; (height from leaf level, always >0)
160131** varint iBlockid; (block id of node's leftmost subtree)
160132** optional {
160133** varint nTerm; (length of first term)
160134** char pTerm[nTerm]; (content of first term)
160135** array {
160136** (further terms are delta-encoded)
160137** varint nPrefix; (length of shared prefix with previous term)
160138** varint nSuffix; (length of unshared suffix)
160139** char pTermSuffix[nSuffix]; (unshared suffix of next term)
160140** }
160141** }
160142**
160143** Here, optional { X } means an optional element, while array { X }
160144** means zero or more occurrences of X, adjacent in memory.
160145**
160146** An interior node encodes n terms separating n+1 subtrees. The
160147** subtree blocks are contiguous, so only the first subtree's blockid
160148** is encoded. The subtree at iBlockid will contain all terms less
160149** than the first term encoded (or all terms if no term is encoded).
160150** Otherwise, for terms greater than or equal to pTerm[i] but less
160151** than pTerm[i+1], the subtree for that term will be rooted at
160152** iBlockid+i. Interior nodes only store enough term data to
160153** distinguish adjacent children (if the rightmost term of the left
160154** child is "something", and the leftmost term of the right child is
160155** "wicked", only "w" is stored).
160156**
160157** New data is spilled to a new interior node at the same height when
160158** the current node exceeds INTERIOR_MAX bytes (default 2048).
160159** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
160160** interior nodes and making the tree too skinny. The interior nodes
160161** at a given height are naturally tracked by interior nodes at
160162** height+1, and so on.
160163**
160164**
160165**** Segment directory ****
160166** The segment directory in table %_segdir stores meta-information for
160167** merging and deleting segments, and also the root node of the
160168** segment's tree.
160169**
160170** The root node is the top node of the segment's tree after encoding
160171** the entire segment, restricted to ROOT_MAX bytes (default 1024).
160172** This could be either a leaf node or an interior node. If the top
160173** node requires more than ROOT_MAX bytes, it is flushed to %_segments
160174** and a new root interior node is generated (which should always fit
160175** within ROOT_MAX because it only needs space for 2 varints, the
160176** height and the blockid of the previous root).
160177**
160178** The meta-information in the segment directory is:
160179** level - segment level (see below)
160180** idx - index within level
160181** - (level,idx uniquely identify a segment)
160182** start_block - first leaf node
160183** leaves_end_block - last leaf node
160184** end_block - last block (including interior nodes)
160185** root - contents of root node
160186**
160187** If the root node is a leaf node, then start_block,
160188** leaves_end_block, and end_block are all 0.
160189**
160190**
160191**** Segment merging ****
160192** To amortize update costs, segments are grouped into levels and
160193** merged in batches. Each increase in level represents exponentially
160194** more documents.
160195**
160196** New documents (actually, document updates) are tokenized and
160197** written individually (using LeafWriter) to a level 0 segment, with
160198** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
160199** level 0 segments are merged into a single level 1 segment. Level 1
160200** is populated like level 0, and eventually MERGE_COUNT level 1
160201** segments are merged to a single level 2 segment (representing
160202** MERGE_COUNT^2 updates), and so on.
160203**
160204** A segment merge traverses all segments at a given level in
160205** parallel, performing a straightforward sorted merge. Since segment
160206** leaf nodes are written in to the %_segments table in order, this
160207** merge traverses the underlying sqlite disk structures efficiently.
160208** After the merge, all segment blocks from the merged level are
160209** deleted.
160210**
160211** MERGE_COUNT controls how often we merge segments. 16 seems to be
160212** somewhat of a sweet spot for insertion performance. 32 and 64 show
160213** very similar performance numbers to 16 on insertion, though they're
160214** a tiny bit slower (perhaps due to more overhead in merge-time
160215** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
160216** 16, 2 about 66% slower than 16.
160217**
160218** At query time, high MERGE_COUNT increases the number of segments
160219** which need to be scanned and merged. For instance, with 100k docs
160220** inserted:
160221**
160222** MERGE_COUNT segments
160223** 16 25
160224** 8 12
160225** 4 10
160226** 2 6
160227**
160228** This appears to have only a moderate impact on queries for very
160229** frequent terms (which are somewhat dominated by segment merge
160230** costs), and infrequent and non-existent terms still seem to be fast
160231** even with many segments.
160232**
160233** TODO(shess) That said, it would be nice to have a better query-side
160234** argument for MERGE_COUNT of 16. Also, it is possible/likely that
160235** optimizations to things like doclist merging will swing the sweet
160236** spot around.
160237**
160238**
160239**
160240**** Handling of deletions and updates ****
160241** Since we're using a segmented structure, with no docid-oriented
160242** index into the term index, we clearly cannot simply update the term
160243** index when a document is deleted or updated. For deletions, we
160244** write an empty doclist (varint(docid) varint(POS_END)), for updates
160245** we simply write the new doclist. Segment merges overwrite older
160246** data for a particular docid with newer data, so deletes or updates
160247** will eventually overtake the earlier data and knock it out. The
160248** query logic likewise merges doclists so that newer data knocks out
160249** older data.
160250*/
160251
160252/************** Include fts3Int.h in the middle of fts3.c ********************/
160253/************** Begin file fts3Int.h *****************************************/
160254/*
160255** 2009 Nov 12
160256**
160257** The author disclaims copyright to this source code. In place of
160258** a legal notice, here is a blessing:
160259**
160260** May you do good and not evil.
160261** May you find forgiveness for yourself and forgive others.
160262** May you share freely, never taking more than you give.
160263**
160264******************************************************************************
160265**
160266*/
160267#ifndef _FTSINT_H
160268#define _FTSINT_H
160269
160270#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
160271# define NDEBUG1 1
160272#endif
160273
160274/* FTS3/FTS4 require virtual tables */
160275#ifdef SQLITE_OMIT_VIRTUALTABLE
160276# undef SQLITE_ENABLE_FTS3
160277# undef SQLITE_ENABLE_FTS4
160278#endif
160279
160280/*
160281** FTS4 is really an extension for FTS3. It is enabled using the
160282** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
160283** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
160284*/
160285#if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
160286# define SQLITE_ENABLE_FTS3
160287#endif
160288
160289#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
160290
160291/* If not building as part of the core, include sqlite3ext.h. */
160292#ifndef SQLITE_CORE1
160293/* # include "sqlite3ext.h" */
160294SQLITE_EXTENSION_INIT3
160295#endif
160296
160297/* #include "sqlite3.h" */
160298/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
160299/************** Begin file fts3_tokenizer.h **********************************/
160300/*
160301** 2006 July 10
160302**
160303** The author disclaims copyright to this source code.
160304**
160305*************************************************************************
160306** Defines the interface to tokenizers used by fulltext-search. There
160307** are three basic components:
160308**
160309** sqlite3_tokenizer_module is a singleton defining the tokenizer
160310** interface functions. This is essentially the class structure for
160311** tokenizers.
160312**
160313** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
160314** including customization information defined at creation time.
160315**
160316** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
160317** tokens from a particular input.
160318*/
160319#ifndef _FTS3_TOKENIZER_H_
160320#define _FTS3_TOKENIZER_H_
160321
160322/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
160323** If tokenizers are to be allowed to call sqlite3_*() functions, then
160324** we will need a way to register the API consistently.
160325*/
160326/* #include "sqlite3.h" */
160327
160328/*
160329** Structures used by the tokenizer interface. When a new tokenizer
160330** implementation is registered, the caller provides a pointer to
160331** an sqlite3_tokenizer_module containing pointers to the callback
160332** functions that make up an implementation.
160333**
160334** When an fts3 table is created, it passes any arguments passed to
160335** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
160336** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
160337** implementation. The xCreate() function in turn returns an
160338** sqlite3_tokenizer structure representing the specific tokenizer to
160339** be used for the fts3 table (customized by the tokenizer clause arguments).
160340**
160341** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
160342** method is called. It returns an sqlite3_tokenizer_cursor object
160343** that may be used to tokenize a specific input buffer based on
160344** the tokenization rules supplied by a specific sqlite3_tokenizer
160345** object.
160346*/
160347typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
160348typedef struct sqlite3_tokenizer sqlite3_tokenizer;
160349typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
160350
160351struct sqlite3_tokenizer_module {
160352
160353 /*
160354 ** Structure version. Should always be set to 0 or 1.
160355 */
160356 int iVersion;
160357
160358 /*
160359 ** Create a new tokenizer. The values in the argv[] array are the
160360 ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
160361 ** TABLE statement that created the fts3 table. For example, if
160362 ** the following SQL is executed:
160363 **
160364 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
160365 **
160366 ** then argc is set to 2, and the argv[] array contains pointers
160367 ** to the strings "arg1" and "arg2".
160368 **
160369 ** This method should return either SQLITE_OK (0), or an SQLite error
160370 ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
160371 ** to point at the newly created tokenizer structure. The generic
160372 ** sqlite3_tokenizer.pModule variable should not be initialized by
160373 ** this callback. The caller will do so.
160374 */
160375 int (*xCreate)(
160376 int argc, /* Size of argv array */
160377 const char *const*argv, /* Tokenizer argument strings */
160378 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
160379 );
160380
160381 /*
160382 ** Destroy an existing tokenizer. The fts3 module calls this method
160383 ** exactly once for each successful call to xCreate().
160384 */
160385 int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
160386
160387 /*
160388 ** Create a tokenizer cursor to tokenize an input buffer. The caller
160389 ** is responsible for ensuring that the input buffer remains valid
160390 ** until the cursor is closed (using the xClose() method).
160391 */
160392 int (*xOpen)(
160393 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
160394 const char *pInput, int nBytes, /* Input buffer */
160395 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
160396 );
160397
160398 /*
160399 ** Destroy an existing tokenizer cursor. The fts3 module calls this
160400 ** method exactly once for each successful call to xOpen().
160401 */
160402 int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
160403
160404 /*
160405 ** Retrieve the next token from the tokenizer cursor pCursor. This
160406 ** method should either return SQLITE_OK and set the values of the
160407 ** "OUT" variables identified below, or SQLITE_DONE to indicate that
160408 ** the end of the buffer has been reached, or an SQLite error code.
160409 **
160410 ** *ppToken should be set to point at a buffer containing the
160411 ** normalized version of the token (i.e. after any case-folding and/or
160412 ** stemming has been performed). *pnBytes should be set to the length
160413 ** of this buffer in bytes. The input text that generated the token is
160414 ** identified by the byte offsets returned in *piStartOffset and
160415 ** *piEndOffset. *piStartOffset should be set to the index of the first
160416 ** byte of the token in the input buffer. *piEndOffset should be set
160417 ** to the index of the first byte just past the end of the token in
160418 ** the input buffer.
160419 **
160420 ** The buffer *ppToken is set to point at is managed by the tokenizer
160421 ** implementation. It is only required to be valid until the next call
160422 ** to xNext() or xClose().
160423 */
160424 /* TODO(shess) current implementation requires pInput to be
160425 ** nul-terminated. This should either be fixed, or pInput/nBytes
160426 ** should be converted to zInput.
160427 */
160428 int (*xNext)(
160429 sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */
160430 const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */
160431 int *piStartOffset, /* OUT: Byte offset of token in input buffer */
160432 int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */
160433 int *piPosition /* OUT: Number of tokens returned before this one */
160434 );
160435
160436 /***********************************************************************
160437 ** Methods below this point are only available if iVersion>=1.
160438 */
160439
160440 /*
160441 ** Configure the language id of a tokenizer cursor.
160442 */
160443 int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
160444};
160445
160446struct sqlite3_tokenizer {
160447 const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */
160448 /* Tokenizer implementations will typically add additional fields */
160449};
160450
160451struct sqlite3_tokenizer_cursor {
160452 sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */
160453 /* Tokenizer implementations will typically add additional fields */
160454};
160455
160456int fts3_global_term_cnt(int iTerm, int iCol);
160457int fts3_term_cnt(int iTerm, int iCol);
160458
160459
160460#endif /* _FTS3_TOKENIZER_H_ */
160461
160462/************** End of fts3_tokenizer.h **************************************/
160463/************** Continuing where we left off in fts3Int.h ********************/
160464/************** Include fts3_hash.h in the middle of fts3Int.h ***************/
160465/************** Begin file fts3_hash.h ***************************************/
160466/*
160467** 2001 September 22
160468**
160469** The author disclaims copyright to this source code. In place of
160470** a legal notice, here is a blessing:
160471**
160472** May you do good and not evil.
160473** May you find forgiveness for yourself and forgive others.
160474** May you share freely, never taking more than you give.
160475**
160476*************************************************************************
160477** This is the header file for the generic hash-table implementation
160478** used in SQLite. We've modified it slightly to serve as a standalone
160479** hash table implementation for the full-text indexing module.
160480**
160481*/
160482#ifndef _FTS3_HASH_H_
160483#define _FTS3_HASH_H_
160484
160485/* Forward declarations of structures. */
160486typedef struct Fts3Hash Fts3Hash;
160487typedef struct Fts3HashElem Fts3HashElem;
160488
160489/* A complete hash table is an instance of the following structure.
160490** The internals of this structure are intended to be opaque -- client
160491** code should not attempt to access or modify the fields of this structure
160492** directly. Change this structure only by using the routines below.
160493** However, many of the "procedures" and "functions" for modifying and
160494** accessing this structure are really macros, so we can't really make
160495** this structure opaque.
160496*/
160497struct Fts3Hash {
160498 char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */
160499 char copyKey; /* True if copy of key made on insert */
160500 int count; /* Number of entries in this table */
160501 Fts3HashElem *first; /* The first element of the array */
160502 int htsize; /* Number of buckets in the hash table */
160503 struct _fts3ht { /* the hash table */
160504 int count; /* Number of entries with this hash */
160505 Fts3HashElem *chain; /* Pointer to first entry with this hash */
160506 } *ht;
160507};
160508
160509/* Each element in the hash table is an instance of the following
160510** structure. All elements are stored on a single doubly-linked list.
160511**
160512** Again, this structure is intended to be opaque, but it can't really
160513** be opaque because it is used by macros.
160514*/
160515struct Fts3HashElem {
160516 Fts3HashElem *next, *prev; /* Next and previous elements in the table */
160517 void *data; /* Data associated with this element */
160518 void *pKey; int nKey; /* Key associated with this element */
160519};
160520
160521/*
160522** There are 2 different modes of operation for a hash table:
160523**
160524** FTS3_HASH_STRING pKey points to a string that is nKey bytes long
160525** (including the null-terminator, if any). Case
160526** is respected in comparisons.
160527**
160528** FTS3_HASH_BINARY pKey points to binary data nKey bytes long.
160529** memcmp() is used to compare keys.
160530**
160531** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
160532*/
160533#define FTS3_HASH_STRING 1
160534#define FTS3_HASH_BINARY 2
160535
160536/*
160537** Access routines. To delete, insert a NULL pointer.
160538*/
160539SQLITE_PRIVATEstatic void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
160540SQLITE_PRIVATEstatic void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
160541SQLITE_PRIVATEstatic void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
160542SQLITE_PRIVATEstatic void sqlite3Fts3HashClear(Fts3Hash*);
160543SQLITE_PRIVATEstatic Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
160544
160545/*
160546** Shorthand for the functions above
160547*/
160548#define fts3HashInit sqlite3Fts3HashInit
160549#define fts3HashInsert sqlite3Fts3HashInsert
160550#define fts3HashFind sqlite3Fts3HashFind
160551#define fts3HashClear sqlite3Fts3HashClear
160552#define fts3HashFindElem sqlite3Fts3HashFindElem
160553
160554/*
160555** Macros for looping over all elements of a hash table. The idiom is
160556** like this:
160557**
160558** Fts3Hash h;
160559** Fts3HashElem *p;
160560** ...
160561** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
160562** SomeStructure *pData = fts3HashData(p);
160563** // do something with pData
160564** }
160565*/
160566#define fts3HashFirst(H) ((H)->first)
160567#define fts3HashNext(E) ((E)->next)
160568#define fts3HashData(E) ((E)->data)
160569#define fts3HashKey(E) ((E)->pKey)
160570#define fts3HashKeysize(E) ((E)->nKey)
160571
160572/*
160573** Number of entries in a hash table
160574*/
160575#define fts3HashCount(H) ((H)->count)
160576
160577#endif /* _FTS3_HASH_H_ */
160578
160579/************** End of fts3_hash.h *******************************************/
160580/************** Continuing where we left off in fts3Int.h ********************/
160581
160582/*
160583** This constant determines the maximum depth of an FTS expression tree
160584** that the library will create and use. FTS uses recursion to perform
160585** various operations on the query tree, so the disadvantage of a large
160586** limit is that it may allow very large queries to use large amounts
160587** of stack space (perhaps causing a stack overflow).
160588*/
160589#ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
160590# define SQLITE_FTS3_MAX_EXPR_DEPTH 12
160591#endif
160592
160593
160594/*
160595** This constant controls how often segments are merged. Once there are
160596** FTS3_MERGE_COUNT segments of level N, they are merged into a single
160597** segment of level N+1.
160598*/
160599#define FTS3_MERGE_COUNT 16
160600
160601/*
160602** This is the maximum amount of data (in bytes) to store in the
160603** Fts3Table.pendingTerms hash table. Normally, the hash table is
160604** populated as documents are inserted/updated/deleted in a transaction
160605** and used to create a new segment when the transaction is committed.
160606** However if this limit is reached midway through a transaction, a new
160607** segment is created and the hash table cleared immediately.
160608*/
160609#define FTS3_MAX_PENDING_DATA (1*1024*1024)
160610
160611/*
160612** Macro to return the number of elements in an array. SQLite has a
160613** similar macro called ArraySize(). Use a different name to avoid
160614** a collision when building an amalgamation with built-in FTS3.
160615*/
160616#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
160617
160618
160619#ifndef MIN
160620# define MIN(x,y)((x)<(y)?(x):(y)) ((x)<(y)?(x):(y))
160621#endif
160622#ifndef MAX
160623# define MAX(x,y)((x)>(y)?(x):(y)) ((x)>(y)?(x):(y))
160624#endif
160625
160626/*
160627** Maximum length of a varint encoded integer. The varint format is different
160628** from that used by SQLite, so the maximum length is 10, not 9.
160629*/
160630#define FTS3_VARINT_MAX 10
160631
160632#define FTS3_BUFFER_PADDING 8
160633
160634/*
160635** FTS4 virtual tables may maintain multiple indexes - one index of all terms
160636** in the document set and zero or more prefix indexes. All indexes are stored
160637** as one or more b+-trees in the %_segments and %_segdir tables.
160638**
160639** It is possible to determine which index a b+-tree belongs to based on the
160640** value stored in the "%_segdir.level" column. Given this value L, the index
160641** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
160642** level values between 0 and 1023 (inclusive) belong to index 0, all levels
160643** between 1024 and 2047 to index 1, and so on.
160644**
160645** It is considered impossible for an index to use more than 1024 levels. In
160646** theory though this may happen, but only after at least
160647** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
160648*/
160649#define FTS3_SEGDIR_MAXLEVEL 1024
160650#define FTS3_SEGDIR_MAXLEVEL_STR "1024"
160651
160652/*
160653** The testcase() macro is only used by the amalgamation. If undefined,
160654** make it a no-op.
160655*/
160656#ifndef testcase
160657# define testcase(X)
160658#endif
160659
160660/*
160661** Terminator values for position-lists and column-lists.
160662*/
160663#define POS_COLUMN (1) /* Column-list terminator */
160664#define POS_END (0) /* Position-list terminator */
160665
160666/*
160667** The assert_fts3_nc() macro is similar to the assert() macro, except that it
160668** is used for assert() conditions that are true only if it can be
160669** guranteed that the database is not corrupt.
160670*/
160671#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
160672SQLITE_API extern int sqlite3_fts3_may_be_corrupt;
160673# define assert_fts3_nc(x) assert(sqlite3_fts3_may_be_corrupt || (x))((void) (0))
160674#else
160675# define assert_fts3_nc(x) assert(x)((void) (0))
160676#endif
160677
160678/*
160679** This section provides definitions to allow the
160680** FTS3 extension to be compiled outside of the
160681** amalgamation.
160682*/
160683#ifndef SQLITE_AMALGAMATION1
160684/*
160685** Macros indicating that conditional expressions are always true or
160686** false.
160687*/
160688#ifdef SQLITE_COVERAGE_TEST
160689# define ALWAYS(x)(x) (1)
160690# define NEVER(X)(X) (0)
160691#elif defined(SQLITE_DEBUG)
160692# define ALWAYS(x)(x) sqlite3Fts3Always((x)!=0)
160693# define NEVER(x)(x) sqlite3Fts3Never((x)!=0)
160694SQLITE_PRIVATEstatic int sqlite3Fts3Always(int b);
160695SQLITE_PRIVATEstatic int sqlite3Fts3Never(int b);
160696#else
160697# define ALWAYS(x)(x) (x)
160698# define NEVER(x)(x) (x)
160699#endif
160700
160701/*
160702** Internal types used by SQLite.
160703*/
160704typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
160705typedef short int i16; /* 2-byte (or larger) signed integer */
160706typedef unsigned int u32; /* 4-byte unsigned integer */
160707typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
160708typedef sqlite3_int64 i64; /* 8-byte signed integer */
160709
160710/*
160711** Macro used to suppress compiler warnings for unused parameters.
160712*/
160713#define UNUSED_PARAMETER(x)(void)(x) (void)(x)
160714
160715/*
160716** Activate assert() only if SQLITE_TEST is enabled.
160717*/
160718#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
160719# define NDEBUG1 1
160720#endif
160721
160722/*
160723** The TESTONLY macro is used to enclose variable declarations or
160724** other bits of code that are needed to support the arguments
160725** within testcase() and assert() macros.
160726*/
160727#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
160728# define TESTONLY(X) X
160729#else
160730# define TESTONLY(X)
160731#endif
160732
160733#endif /* SQLITE_AMALGAMATION */
160734
160735#ifdef SQLITE_DEBUG
160736SQLITE_PRIVATEstatic int sqlite3Fts3Corrupt(void);
160737# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
160738#else
160739# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB(11 | (1<<8))
160740#endif
160741
160742typedef struct Fts3Table Fts3Table;
160743typedef struct Fts3Cursor Fts3Cursor;
160744typedef struct Fts3Expr Fts3Expr;
160745typedef struct Fts3Phrase Fts3Phrase;
160746typedef struct Fts3PhraseToken Fts3PhraseToken;
160747
160748typedef struct Fts3Doclist Fts3Doclist;
160749typedef struct Fts3SegFilter Fts3SegFilter;
160750typedef struct Fts3DeferredToken Fts3DeferredToken;
160751typedef struct Fts3SegReader Fts3SegReader;
160752typedef struct Fts3MultiSegReader Fts3MultiSegReader;
160753
160754typedef struct MatchinfoBuffer MatchinfoBuffer;
160755
160756/*
160757** A connection to a fulltext index is an instance of the following
160758** structure. The xCreate and xConnect methods create an instance
160759** of this structure and xDestroy and xDisconnect free that instance.
160760** All other methods receive a pointer to the structure as one of their
160761** arguments.
160762*/
160763struct Fts3Table {
160764 sqlite3_vtab base; /* Base class used by SQLite core */
160765 sqlite3 *db; /* The database connection */
160766 const char *zDb; /* logical database name */
160767 const char *zName; /* virtual table name */
160768 int nColumn; /* number of named columns in virtual table */
160769 char **azColumn; /* column names. malloced */
160770 u8 *abNotindexed; /* True for 'notindexed' columns */
160771 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
160772 char *zContentTbl; /* content=xxx option, or NULL */
160773 char *zLanguageid; /* languageid=xxx option, or NULL */
160774 int nAutoincrmerge; /* Value configured by 'automerge' */
160775 u32 nLeafAdd; /* Number of leaf blocks added this trans */
160776
160777 /* Precompiled statements used by the implementation. Each of these
160778 ** statements is run and reset within a single virtual table API call.
160779 */
160780 sqlite3_stmt *aStmt[40];
160781 sqlite3_stmt *pSeekStmt; /* Cache for fts3CursorSeekStmt() */
160782
160783 char *zReadExprlist;
160784 char *zWriteExprlist;
160785
160786 int nNodeSize; /* Soft limit for node size */
160787 u8 bFts4; /* True for FTS4, false for FTS3 */
160788 u8 bHasStat; /* True if %_stat table exists (2==unknown) */
160789 u8 bHasDocsize; /* True if %_docsize table exists */
160790 u8 bDescIdx; /* True if doclists are in reverse order */
160791 u8 bIgnoreSavepoint; /* True to ignore xSavepoint invocations */
160792 int nPgsz; /* Page size for host database */
160793 char *zSegmentsTbl; /* Name of %_segments table */
160794 sqlite3_blob *pSegments; /* Blob handle open on %_segments table */
160795
160796 /*
160797 ** The following array of hash tables is used to buffer pending index
160798 ** updates during transactions. All pending updates buffered at any one
160799 ** time must share a common language-id (see the FTS4 langid= feature).
160800 ** The current language id is stored in variable iPrevLangid.
160801 **
160802 ** A single FTS4 table may have multiple full-text indexes. For each index
160803 ** there is an entry in the aIndex[] array. Index 0 is an index of all the
160804 ** terms that appear in the document set. Each subsequent index in aIndex[]
160805 ** is an index of prefixes of a specific length.
160806 **
160807 ** Variable nPendingData contains an estimate the memory consumed by the
160808 ** pending data structures, including hash table overhead, but not including
160809 ** malloc overhead. When nPendingData exceeds nMaxPendingData, all hash
160810 ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
160811 ** recently inserted record.
160812 */
160813 int nIndex; /* Size of aIndex[] */
160814 struct Fts3Index {
160815 int nPrefix; /* Prefix length (0 for main terms index) */
160816 Fts3Hash hPending; /* Pending terms table for this index */
160817 } *aIndex;
160818 int nMaxPendingData; /* Max pending data before flush to disk */
160819 int nPendingData; /* Current bytes of pending data */
160820 sqlite_int64 iPrevDocid; /* Docid of most recently inserted document */
160821 int iPrevLangid; /* Langid of recently inserted document */
160822 int bPrevDelete; /* True if last operation was a delete */
160823
160824#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
160825 /* State variables used for validating that the transaction control
160826 ** methods of the virtual table are called at appropriate times. These
160827 ** values do not contribute to FTS functionality; they are used for
160828 ** verifying the operation of the SQLite core.
160829 */
160830 int inTransaction; /* True after xBegin but before xCommit/xRollback */
160831 int mxSavepoint; /* Largest valid xSavepoint integer */
160832#endif
160833
160834#ifdef SQLITE_TEST
160835 /* True to disable the incremental doclist optimization. This is controled
160836 ** by special insert command 'test-no-incr-doclist'. */
160837 int bNoIncrDoclist;
160838#endif
160839};
160840
160841/*
160842** When the core wants to read from the virtual table, it creates a
160843** virtual table cursor (an instance of the following structure) using
160844** the xOpen method. Cursors are destroyed using the xClose method.
160845*/
160846struct Fts3Cursor {
160847 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
160848 i16 eSearch; /* Search strategy (see below) */
160849 u8 isEof; /* True if at End Of Results */
160850 u8 isRequireSeek; /* True if must seek pStmt to %_content row */
160851 u8 bSeekStmt; /* True if pStmt is a seek */
160852 sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */
160853 Fts3Expr *pExpr; /* Parsed MATCH query string */
160854 int iLangid; /* Language being queried for */
160855 int nPhrase; /* Number of matchable phrases in query */
160856 Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */
160857 sqlite3_int64 iPrevId; /* Previous id read from aDoclist */
160858 char *pNextId; /* Pointer into the body of aDoclist */
160859 char *aDoclist; /* List of docids for full-text queries */
160860 int nDoclist; /* Size of buffer at aDoclist */
160861 u8 bDesc; /* True to sort in descending order */
160862 int eEvalmode; /* An FTS3_EVAL_XX constant */
160863 int nRowAvg; /* Average size of database rows, in pages */
160864 sqlite3_int64 nDoc; /* Documents in table */
160865 i64 iMinDocid; /* Minimum docid to return */
160866 i64 iMaxDocid; /* Maximum docid to return */
160867 int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */
160868 MatchinfoBuffer *pMIBuffer; /* Buffer for matchinfo data */
160869};
160870
160871#define FTS3_EVAL_FILTER 0
160872#define FTS3_EVAL_NEXT 1
160873#define FTS3_EVAL_MATCHINFO 2
160874
160875/*
160876** The Fts3Cursor.eSearch member is always set to one of the following.
160877** Actualy, Fts3Cursor.eSearch can be greater than or equal to
160878** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
160879** of the column to be searched. For example, in
160880**
160881** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
160882** SELECT docid FROM ex1 WHERE b MATCH 'one two three';
160883**
160884** Because the LHS of the MATCH operator is 2nd column "b",
160885** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a,
160886** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1"
160887** indicating that all columns should be searched,
160888** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
160889*/
160890#define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */
160891#define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */
160892#define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
160893
160894/*
160895** The lower 16-bits of the sqlite3_index_info.idxNum value set by
160896** the xBestIndex() method contains the Fts3Cursor.eSearch value described
160897** above. The upper 16-bits contain a combination of the following
160898** bits, used to describe extra constraints on full-text searches.
160899*/
160900#define FTS3_HAVE_LANGID 0x00010000 /* languageid=? */
160901#define FTS3_HAVE_DOCID_GE 0x00020000 /* docid>=? */
160902#define FTS3_HAVE_DOCID_LE 0x00040000 /* docid<=? */
160903
160904struct Fts3Doclist {
160905 char *aAll; /* Array containing doclist (or NULL) */
160906 int nAll; /* Size of a[] in bytes */
160907 char *pNextDocid; /* Pointer to next docid */
160908
160909 sqlite3_int64 iDocid; /* Current docid (if pList!=0) */
160910 int bFreeList; /* True if pList should be sqlite3_free()d */
160911 char *pList; /* Pointer to position list following iDocid */
160912 int nList; /* Length of position list */
160913};
160914
160915/*
160916** A "phrase" is a sequence of one or more tokens that must match in
160917** sequence. A single token is the base case and the most common case.
160918** For a sequence of tokens contained in double-quotes (i.e. "one two three")
160919** nToken will be the number of tokens in the string.
160920*/
160921struct Fts3PhraseToken {
160922 char *z; /* Text of the token */
160923 int n; /* Number of bytes in buffer z */
160924 int isPrefix; /* True if token ends with a "*" character */
160925 int bFirst; /* True if token must appear at position 0 */
160926
160927 /* Variables above this point are populated when the expression is
160928 ** parsed (by code in fts3_expr.c). Below this point the variables are
160929 ** used when evaluating the expression. */
160930 Fts3DeferredToken *pDeferred; /* Deferred token object for this token */
160931 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
160932};
160933
160934struct Fts3Phrase {
160935 /* Cache of doclist for this phrase. */
160936 Fts3Doclist doclist;
160937 int bIncr; /* True if doclist is loaded incrementally */
160938 int iDoclistToken;
160939
160940 /* Used by sqlite3Fts3EvalPhrasePoslist() if this is a descendent of an
160941 ** OR condition. */
160942 char *pOrPoslist;
160943 i64 iOrDocid;
160944
160945 /* Variables below this point are populated by fts3_expr.c when parsing
160946 ** a MATCH expression. Everything above is part of the evaluation phase.
160947 */
160948 int nToken; /* Number of tokens in the phrase */
160949 int iColumn; /* Index of column this phrase must match */
160950 Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
160951};
160952
160953/*
160954** A tree of these objects forms the RHS of a MATCH operator.
160955**
160956** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
160957** points to a malloced buffer, size nDoclist bytes, containing the results
160958** of this phrase query in FTS3 doclist format. As usual, the initial
160959** "Length" field found in doclists stored on disk is omitted from this
160960** buffer.
160961**
160962** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
160963** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
160964** where nCol is the number of columns in the queried FTS table. The array
160965** is populated as follows:
160966**
160967** aMI[iCol*3 + 0] = Undefined
160968** aMI[iCol*3 + 1] = Number of occurrences
160969** aMI[iCol*3 + 2] = Number of rows containing at least one instance
160970**
160971** The aMI array is allocated using sqlite3_malloc(). It should be freed
160972** when the expression node is.
160973*/
160974struct Fts3Expr {
160975 int eType; /* One of the FTSQUERY_XXX values defined below */
160976 int nNear; /* Valid if eType==FTSQUERY_NEAR */
160977 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
160978 Fts3Expr *pLeft; /* Left operand */
160979 Fts3Expr *pRight; /* Right operand */
160980 Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */
160981
160982 /* The following are used by the fts3_eval.c module. */
160983 sqlite3_int64 iDocid; /* Current docid */
160984 u8 bEof; /* True this expression is at EOF already */
160985 u8 bStart; /* True if iDocid is valid */
160986 u8 bDeferred; /* True if this expression is entirely deferred */
160987
160988 /* The following are used by the fts3_snippet.c module. */
160989 int iPhrase; /* Index of this phrase in matchinfo() results */
160990 u32 *aMI; /* See above */
160991};
160992
160993/*
160994** Candidate values for Fts3Query.eType. Note that the order of the first
160995** four values is in order of precedence when parsing expressions. For
160996** example, the following:
160997**
160998** "a OR b AND c NOT d NEAR e"
160999**
161000** is equivalent to:
161001**
161002** "a OR (b AND (c NOT (d NEAR e)))"
161003*/
161004#define FTSQUERY_NEAR 1
161005#define FTSQUERY_NOT 2
161006#define FTSQUERY_AND 3
161007#define FTSQUERY_OR 4
161008#define FTSQUERY_PHRASE 5
161009
161010
161011/* fts3_write.c */
161012SQLITE_PRIVATEstatic int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
161013SQLITE_PRIVATEstatic int sqlite3Fts3PendingTermsFlush(Fts3Table *);
161014SQLITE_PRIVATEstatic void sqlite3Fts3PendingTermsClear(Fts3Table *);
161015SQLITE_PRIVATEstatic int sqlite3Fts3Optimize(Fts3Table *);
161016SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
161017 sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
161018SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderPending(
161019 Fts3Table*,int,const char*,int,int,Fts3SegReader**);
161020SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFree(Fts3SegReader *);
161021SQLITE_PRIVATEstatic int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
161022SQLITE_PRIVATEstatic int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
161023
161024SQLITE_PRIVATEstatic int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
161025SQLITE_PRIVATEstatic int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
161026
161027#ifndef SQLITE_DISABLE_FTS4_DEFERRED
161028SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
161029SQLITE_PRIVATEstatic int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
161030SQLITE_PRIVATEstatic int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
161031SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
161032SQLITE_PRIVATEstatic int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
161033#else
161034# define sqlite3Fts3FreeDeferredTokens(x)
161035# define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK0
161036# define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK0
161037# define sqlite3Fts3FreeDeferredDoclists(x)
161038# define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK0
161039#endif
161040
161041SQLITE_PRIVATEstatic void sqlite3Fts3SegmentsClose(Fts3Table *);
161042SQLITE_PRIVATEstatic int sqlite3Fts3MaxLevel(Fts3Table *, int *);
161043
161044/* Special values interpreted by sqlite3SegReaderCursor() */
161045#define FTS3_SEGCURSOR_PENDING -1
161046#define FTS3_SEGCURSOR_ALL -2
161047
161048SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
161049SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
161050SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
161051
161052SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderCursor(Fts3Table *,
161053 int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
161054
161055/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
161056#define FTS3_SEGMENT_REQUIRE_POS 0x00000001
161057#define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002
161058#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
161059#define FTS3_SEGMENT_PREFIX 0x00000008
161060#define FTS3_SEGMENT_SCAN 0x00000010
161061#define FTS3_SEGMENT_FIRST 0x00000020
161062
161063/* Type passed as 4th argument to SegmentReaderIterate() */
161064struct Fts3SegFilter {
161065 const char *zTerm;
161066 int nTerm;
161067 int iCol;
161068 int flags;
161069};
161070
161071struct Fts3MultiSegReader {
161072 /* Used internally by sqlite3Fts3SegReaderXXX() calls */
161073 Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */
161074 int nSegment; /* Size of apSegment array */
161075 int nAdvance; /* How many seg-readers to advance */
161076 Fts3SegFilter *pFilter; /* Pointer to filter object */
161077 char *aBuffer; /* Buffer to merge doclists in */
161078 int nBuffer; /* Allocated size of aBuffer[] in bytes */
161079
161080 int iColFilter; /* If >=0, filter for this column */
161081 int bRestart;
161082
161083 /* Used by fts3.c only. */
161084 int nCost; /* Cost of running iterator */
161085 int bLookup; /* True if a lookup of a single entry. */
161086
161087 /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
161088 char *zTerm; /* Pointer to term buffer */
161089 int nTerm; /* Size of zTerm in bytes */
161090 char *aDoclist; /* Pointer to doclist buffer */
161091 int nDoclist; /* Size of aDoclist[] in bytes */
161092};
161093
161094SQLITE_PRIVATEstatic int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
161095
161096#define fts3GetVarint32(p, piVal) ( \
161097 (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
161098)
161099
161100/* fts3.c */
161101SQLITE_PRIVATEstatic void sqlite3Fts3ErrMsg(char**,const char*,...);
161102SQLITE_PRIVATEstatic int sqlite3Fts3PutVarint(char *, sqlite3_int64);
161103SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
161104SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint32(const char *, int *);
161105SQLITE_PRIVATEstatic int sqlite3Fts3VarintLen(sqlite3_uint64);
161106SQLITE_PRIVATEstatic void sqlite3Fts3Dequote(char *);
161107SQLITE_PRIVATEstatic void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
161108SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
161109SQLITE_PRIVATEstatic int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
161110SQLITE_PRIVATEstatic void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
161111SQLITE_PRIVATEstatic int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc);
161112
161113/* fts3_tokenizer.c */
161114SQLITE_PRIVATEstatic const char *sqlite3Fts3NextToken(const char *, int *);
161115SQLITE_PRIVATEstatic int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
161116SQLITE_PRIVATEstatic int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
161117 sqlite3_tokenizer **, char **
161118);
161119SQLITE_PRIVATEstatic int sqlite3Fts3IsIdChar(char);
161120
161121/* fts3_snippet.c */
161122SQLITE_PRIVATEstatic void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
161123SQLITE_PRIVATEstatic void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
161124 const char *, const char *, int, int
161125);
161126SQLITE_PRIVATEstatic void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
161127SQLITE_PRIVATEstatic void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p);
161128
161129/* fts3_expr.c */
161130SQLITE_PRIVATEstatic int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
161131 char **, int, int, int, const char *, int, Fts3Expr **, char **
161132);
161133SQLITE_PRIVATEstatic void sqlite3Fts3ExprFree(Fts3Expr *);
161134#ifdef SQLITE_TEST
161135SQLITE_PRIVATEstatic int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash*);
161136SQLITE_PRIVATEstatic int sqlite3Fts3InitTerm(sqlite3 *db);
161137#endif
161138
161139SQLITE_PRIVATEstatic int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
161140 sqlite3_tokenizer_cursor **
161141);
161142
161143/* fts3_aux.c */
161144SQLITE_PRIVATEstatic int sqlite3Fts3InitAux(sqlite3 *db);
161145
161146SQLITE_PRIVATEstatic void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
161147
161148SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrStart(
161149 Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
161150SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrNext(
161151 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
161152SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
161153SQLITE_PRIVATEstatic int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
161154SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
161155
161156/* fts3_tokenize_vtab.c */
161157SQLITE_PRIVATEstatic int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
161158
161159/* fts3_unicode2.c (functions generated by parsing unicode text files) */
161160#ifndef SQLITE_DISABLE_FTS3_UNICODE
161161SQLITE_PRIVATEstatic int sqlite3FtsUnicodeFold(int, int);
161162SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsalnum(int);
161163SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsdiacritic(int);
161164#endif
161165
161166#endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
161167#endif /* _FTSINT_H */
161168
161169/************** End of fts3Int.h *********************************************/
161170/************** Continuing where we left off in fts3.c ***********************/
161171#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
161172
161173#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE1)
161174# define SQLITE_CORE1 1
161175#endif
161176
161177/* #include <assert.h> */
161178/* #include <stdlib.h> */
161179/* #include <stddef.h> */
161180/* #include <stdio.h> */
161181/* #include <string.h> */
161182/* #include <stdarg.h> */
161183
161184/* #include "fts3.h" */
161185#ifndef SQLITE_CORE1
161186/* # include "sqlite3ext.h" */
161187 SQLITE_EXTENSION_INIT1
161188#endif
161189
161190static int fts3EvalNext(Fts3Cursor *pCsr);
161191static int fts3EvalStart(Fts3Cursor *pCsr);
161192static int fts3TermSegReaderCursor(
161193 Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
161194
161195#ifndef SQLITE_AMALGAMATION1
161196# if defined(SQLITE_DEBUG)
161197SQLITE_PRIVATEstatic int sqlite3Fts3Always(int b) { assert( b )((void) (0)); return b; }
161198SQLITE_PRIVATEstatic int sqlite3Fts3Never(int b) { assert( !b )((void) (0)); return b; }
161199# endif
161200#endif
161201
161202/*
161203** This variable is set to false when running tests for which the on disk
161204** structures should not be corrupt. Otherwise, true. If it is false, extra
161205** assert() conditions in the fts3 code are activated - conditions that are
161206** only true if it is guaranteed that the fts3 database is not corrupt.
161207*/
161208SQLITE_API int sqlite3_fts3_may_be_corrupt = 1;
161209
161210/*
161211** Write a 64-bit variable-length integer to memory starting at p[0].
161212** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
161213** The number of bytes written is returned.
161214*/
161215SQLITE_PRIVATEstatic int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
161216 unsigned char *q = (unsigned char *) p;
161217 sqlite_uint64 vu = v;
161218 do{
161219 *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
161220 vu >>= 7;
161221 }while( vu!=0 );
161222 q[-1] &= 0x7f; /* turn off high bit in final byte */
161223 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX )((void) (0));
161224 return (int) (q - (unsigned char *)p);
161225}
161226
161227#define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
161228 v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift ); \
161229 if( (v & mask2)==0 ){ var = v; return ret; }
161230#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
161231 v = (*ptr++); \
161232 if( (v & mask2)==0 ){ var = v; return ret; }
161233
161234/*
161235** Read a 64-bit variable-length integer from memory starting at p[0].
161236** Return the number of bytes read, or 0 on error.
161237** The value is stored in *v.
161238*/
161239SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
161240 const unsigned char *p = (const unsigned char*)pBuf;
161241 const unsigned char *pStart = p;
161242 u32 a;
161243 u64 b;
161244 int shift;
161245
161246 GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1);
161247 GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
161248 GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
161249 GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
161250 b = (a & 0x0FFFFFFF );
161251
161252 for(shift=28; shift<=63; shift+=7){
161253 u64 c = *p++;
161254 b += (c&0x7F) << shift;
161255 if( (c & 0x80)==0 ) break;
161256 }
161257 *v = b;
161258 return (int)(p - pStart);
161259}
161260
161261/*
161262** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
161263** a non-negative 32-bit integer before it is returned.
161264*/
161265SQLITE_PRIVATEstatic int sqlite3Fts3GetVarint32(const char *p, int *pi){
161266 const unsigned char *ptr = (const unsigned char*)p;
161267 u32 a;
161268
161269#ifndef fts3GetVarint32
161270 GETVARINT_INIT(a, ptr, 0, 0x00, 0x80, *pi, 1);
161271#else
161272 a = (*ptr++);
161273 assert( a & 0x80 )((void) (0));
161274#endif
161275
161276 GETVARINT_STEP(a, ptr, 7, 0x7F, 0x4000, *pi, 2);
161277 GETVARINT_STEP(a, ptr, 14, 0x3FFF, 0x200000, *pi, 3);
161278 GETVARINT_STEP(a, ptr, 21, 0x1FFFFF, 0x10000000, *pi, 4);
161279 a = (a & 0x0FFFFFFF );
161280 *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
161281 assert( 0==(a & 0x80000000) )((void) (0));
161282 assert( *pi>=0 )((void) (0));
161283 return 5;
161284}
161285
161286/*
161287** Return the number of bytes required to encode v as a varint
161288*/
161289SQLITE_PRIVATEstatic int sqlite3Fts3VarintLen(sqlite3_uint64 v){
161290 int i = 0;
161291 do{
161292 i++;
161293 v >>= 7;
161294 }while( v!=0 );
161295 return i;
161296}
161297
161298/*
161299** Convert an SQL-style quoted string into a normal string by removing
161300** the quote characters. The conversion is done in-place. If the
161301** input does not begin with a quote character, then this routine
161302** is a no-op.
161303**
161304** Examples:
161305**
161306** "abc" becomes abc
161307** 'xyz' becomes xyz
161308** [pqr] becomes pqr
161309** `mno` becomes mno
161310**
161311*/
161312SQLITE_PRIVATEstatic void sqlite3Fts3Dequote(char *z){
161313 char quote; /* Quote character (if any ) */
161314
161315 quote = z[0];
161316 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
161317 int iIn = 1; /* Index of next byte to read from input */
161318 int iOut = 0; /* Index of next byte to write to output */
161319
161320 /* If the first byte was a '[', then the close-quote character is a ']' */
161321 if( quote=='[' ) quote = ']';
161322
161323 while( z[iIn] ){
161324 if( z[iIn]==quote ){
161325 if( z[iIn+1]!=quote ) break;
161326 z[iOut++] = quote;
161327 iIn += 2;
161328 }else{
161329 z[iOut++] = z[iIn++];
161330 }
161331 }
161332 z[iOut] = '\0';
161333 }
161334}
161335
161336/*
161337** Read a single varint from the doclist at *pp and advance *pp to point
161338** to the first byte past the end of the varint. Add the value of the varint
161339** to *pVal.
161340*/
161341static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
161342 sqlite3_int64 iVal;
161343 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
161344 *pVal += iVal;
161345}
161346
161347/*
161348** When this function is called, *pp points to the first byte following a
161349** varint that is part of a doclist (or position-list, or any other list
161350** of varints). This function moves *pp to point to the start of that varint,
161351** and sets *pVal by the varint value.
161352**
161353** Argument pStart points to the first byte of the doclist that the
161354** varint is part of.
161355*/
161356static void fts3GetReverseVarint(
161357 char **pp,
161358 char *pStart,
161359 sqlite3_int64 *pVal
161360){
161361 sqlite3_int64 iVal;
161362 char *p;
161363
161364 /* Pointer p now points at the first byte past the varint we are
161365 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
161366 ** clear on character p[-1]. */
161367 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
161368 p++;
161369 *pp = p;
161370
161371 sqlite3Fts3GetVarint(p, &iVal);
161372 *pVal = iVal;
161373}
161374
161375/*
161376** The xDisconnect() virtual table method.
161377*/
161378static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
161379 Fts3Table *p = (Fts3Table *)pVtab;
161380 int i;
161381
161382 assert( p->nPendingData==0 )((void) (0));
161383 assert( p->pSegments==0 )((void) (0));
161384
161385 /* Free any prepared statements held */
161386 sqlite3_finalize(p->pSeekStmt);
161387 for(i=0; i<SizeofArray(p->aStmt); i++){
161388 sqlite3_finalize(p->aStmt[i]);
161389 }
161390 sqlite3_free(p->zSegmentsTbl);
161391 sqlite3_free(p->zReadExprlist);
161392 sqlite3_free(p->zWriteExprlist);
161393 sqlite3_free(p->zContentTbl);
161394 sqlite3_free(p->zLanguageid);
161395
161396 /* Invoke the tokenizer destructor to free the tokenizer. */
161397 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
161398
161399 sqlite3_free(p);
161400 return SQLITE_OK0;
161401}
161402
161403/*
161404** Write an error message into *pzErr
161405*/
161406SQLITE_PRIVATEstatic void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
161407 va_list ap;
161408 sqlite3_free(*pzErr);
161409 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
161410 *pzErr = sqlite3_vmprintf(zFormat, ap);
161411 va_end(ap)__builtin_va_end(ap);
161412}
161413
161414/*
161415** Construct one or more SQL statements from the format string given
161416** and then evaluate those statements. The success code is written
161417** into *pRc.
161418**
161419** If *pRc is initially non-zero then this routine is a no-op.
161420*/
161421static void fts3DbExec(
161422 int *pRc, /* Success code */
161423 sqlite3 *db, /* Database in which to run SQL */
161424 const char *zFormat, /* Format string for SQL */
161425 ... /* Arguments to the format string */
161426){
161427 va_list ap;
161428 char *zSql;
161429 if( *pRc ) return;
161430 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
161431 zSql = sqlite3_vmprintf(zFormat, ap);
161432 va_end(ap)__builtin_va_end(ap);
161433 if( zSql==0 ){
161434 *pRc = SQLITE_NOMEM7;
161435 }else{
161436 *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
161437 sqlite3_free(zSql);
161438 }
161439}
161440
161441/*
161442** The xDestroy() virtual table method.
161443*/
161444static int fts3DestroyMethod(sqlite3_vtab *pVtab){
161445 Fts3Table *p = (Fts3Table *)pVtab;
161446 int rc = SQLITE_OK0; /* Return code */
161447 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
161448 sqlite3 *db = p->db; /* Database handle */
161449
161450 /* Drop the shadow tables */
161451 fts3DbExec(&rc, db,
161452 "DROP TABLE IF EXISTS %Q.'%q_segments';"
161453 "DROP TABLE IF EXISTS %Q.'%q_segdir';"
161454 "DROP TABLE IF EXISTS %Q.'%q_docsize';"
161455 "DROP TABLE IF EXISTS %Q.'%q_stat';"
161456 "%s DROP TABLE IF EXISTS %Q.'%q_content';",
161457 zDb, p->zName,
161458 zDb, p->zName,
161459 zDb, p->zName,
161460 zDb, p->zName,
161461 (p->zContentTbl ? "--" : ""), zDb,p->zName
161462 );
161463
161464 /* If everything has worked, invoke fts3DisconnectMethod() to free the
161465 ** memory associated with the Fts3Table structure and return SQLITE_OK.
161466 ** Otherwise, return an SQLite error code.
161467 */
161468 return (rc==SQLITE_OK0 ? fts3DisconnectMethod(pVtab) : rc);
161469}
161470
161471
161472/*
161473** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
161474** passed as the first argument. This is done as part of the xConnect()
161475** and xCreate() methods.
161476**
161477** If *pRc is non-zero when this function is called, it is a no-op.
161478** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
161479** before returning.
161480*/
161481static void fts3DeclareVtab(int *pRc, Fts3Table *p){
161482 if( *pRc==SQLITE_OK0 ){
161483 int i; /* Iterator variable */
161484 int rc; /* Return code */
161485 char *zSql; /* SQL statement passed to declare_vtab() */
161486 char *zCols; /* List of user defined columns */
161487 const char *zLanguageid;
161488
161489 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
161490 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
161491
161492 /* Create a list of user columns for the virtual table */
161493 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
161494 for(i=1; zCols && i<p->nColumn; i++){
161495 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
161496 }
161497
161498 /* Create the whole "CREATE TABLE" statement to pass to SQLite */
161499 zSql = sqlite3_mprintf(
161500 "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
161501 zCols, p->zName, zLanguageid
161502 );
161503 if( !zCols || !zSql ){
161504 rc = SQLITE_NOMEM7;
161505 }else{
161506 rc = sqlite3_declare_vtab(p->db, zSql);
161507 }
161508
161509 sqlite3_free(zSql);
161510 sqlite3_free(zCols);
161511 *pRc = rc;
161512 }
161513}
161514
161515/*
161516** Create the %_stat table if it does not already exist.
161517*/
161518SQLITE_PRIVATEstatic void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
161519 fts3DbExec(pRc, p->db,
161520 "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
161521 "(id INTEGER PRIMARY KEY, value BLOB);",
161522 p->zDb, p->zName
161523 );
161524 if( (*pRc)==SQLITE_OK0 ) p->bHasStat = 1;
161525}
161526
161527/*
161528** Create the backing store tables (%_content, %_segments and %_segdir)
161529** required by the FTS3 table passed as the only argument. This is done
161530** as part of the vtab xCreate() method.
161531**
161532** If the p->bHasDocsize boolean is true (indicating that this is an
161533** FTS4 table, not an FTS3 table) then also create the %_docsize and
161534** %_stat tables required by FTS4.
161535*/
161536static int fts3CreateTables(Fts3Table *p){
161537 int rc = SQLITE_OK0; /* Return code */
161538 int i; /* Iterator variable */
161539 sqlite3 *db = p->db; /* The database connection */
161540
161541 if( p->zContentTbl==0 ){
161542 const char *zLanguageid = p->zLanguageid;
161543 char *zContentCols; /* Columns of %_content table */
161544
161545 /* Create a list of user columns for the content table */
161546 zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
161547 for(i=0; zContentCols && i<p->nColumn; i++){
161548 char *z = p->azColumn[i];
161549 zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
161550 }
161551 if( zLanguageid && zContentCols ){
161552 zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
161553 }
161554 if( zContentCols==0 ) rc = SQLITE_NOMEM7;
161555
161556 /* Create the content table */
161557 fts3DbExec(&rc, db,
161558 "CREATE TABLE %Q.'%q_content'(%s)",
161559 p->zDb, p->zName, zContentCols
161560 );
161561 sqlite3_free(zContentCols);
161562 }
161563
161564 /* Create other tables */
161565 fts3DbExec(&rc, db,
161566 "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
161567 p->zDb, p->zName
161568 );
161569 fts3DbExec(&rc, db,
161570 "CREATE TABLE %Q.'%q_segdir'("
161571 "level INTEGER,"
161572 "idx INTEGER,"
161573 "start_block INTEGER,"
161574 "leaves_end_block INTEGER,"
161575 "end_block INTEGER,"
161576 "root BLOB,"
161577 "PRIMARY KEY(level, idx)"
161578 ");",
161579 p->zDb, p->zName
161580 );
161581 if( p->bHasDocsize ){
161582 fts3DbExec(&rc, db,
161583 "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
161584 p->zDb, p->zName
161585 );
161586 }
161587 assert( p->bHasStat==p->bFts4 )((void) (0));
161588 if( p->bHasStat ){
161589 sqlite3Fts3CreateStatTable(&rc, p);
161590 }
161591 return rc;
161592}
161593
161594/*
161595** Store the current database page-size in bytes in p->nPgsz.
161596**
161597** If *pRc is non-zero when this function is called, it is a no-op.
161598** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
161599** before returning.
161600*/
161601static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
161602 if( *pRc==SQLITE_OK0 ){
161603 int rc; /* Return code */
161604 char *zSql; /* SQL text "PRAGMA %Q.page_size" */
161605 sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
161606
161607 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
161608 if( !zSql ){
161609 rc = SQLITE_NOMEM7;
161610 }else{
161611 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
161612 if( rc==SQLITE_OK0 ){
161613 sqlite3_step(pStmt);
161614 p->nPgsz = sqlite3_column_int(pStmt, 0);
161615 rc = sqlite3_finalize(pStmt);
161616 }else if( rc==SQLITE_AUTH23 ){
161617 p->nPgsz = 1024;
161618 rc = SQLITE_OK0;
161619 }
161620 }
161621 assert( p->nPgsz>0 || rc!=SQLITE_OK )((void) (0));
161622 sqlite3_free(zSql);
161623 *pRc = rc;
161624 }
161625}
161626
161627/*
161628** "Special" FTS4 arguments are column specifications of the following form:
161629**
161630** <key> = <value>
161631**
161632** There may not be whitespace surrounding the "=" character. The <value>
161633** term may be quoted, but the <key> may not.
161634*/
161635static int fts3IsSpecialColumn(
161636 const char *z,
161637 int *pnKey,
161638 char **pzValue
161639){
161640 char *zValue;
161641 const char *zCsr = z;
161642
161643 while( *zCsr!='=' ){
161644 if( *zCsr=='\0' ) return 0;
161645 zCsr++;
161646 }
161647
161648 *pnKey = (int)(zCsr-z);
161649 zValue = sqlite3_mprintf("%s", &zCsr[1]);
161650 if( zValue ){
161651 sqlite3Fts3Dequote(zValue);
161652 }
161653 *pzValue = zValue;
161654 return 1;
161655}
161656
161657/*
161658** Append the output of a printf() style formatting to an existing string.
161659*/
161660static void fts3Appendf(
161661 int *pRc, /* IN/OUT: Error code */
161662 char **pz, /* IN/OUT: Pointer to string buffer */
161663 const char *zFormat, /* Printf format string to append */
161664 ... /* Arguments for printf format string */
161665){
161666 if( *pRc==SQLITE_OK0 ){
161667 va_list ap;
161668 char *z;
161669 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
161670 z = sqlite3_vmprintf(zFormat, ap);
161671 va_end(ap)__builtin_va_end(ap);
161672 if( z && *pz ){
161673 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
161674 sqlite3_free(z);
161675 z = z2;
161676 }
161677 if( z==0 ) *pRc = SQLITE_NOMEM7;
161678 sqlite3_free(*pz);
161679 *pz = z;
161680 }
161681}
161682
161683/*
161684** Return a copy of input string zInput enclosed in double-quotes (") and
161685** with all double quote characters escaped. For example:
161686**
161687** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
161688**
161689** The pointer returned points to memory obtained from sqlite3_malloc(). It
161690** is the callers responsibility to call sqlite3_free() to release this
161691** memory.
161692*/
161693static char *fts3QuoteId(char const *zInput){
161694 sqlite3_int64 nRet;
161695 char *zRet;
161696 nRet = 2 + (int)strlen(zInput)*2 + 1;
161697 zRet = sqlite3_malloc64(nRet);
161698 if( zRet ){
161699 int i;
161700 char *z = zRet;
161701 *(z++) = '"';
161702 for(i=0; zInput[i]; i++){
161703 if( zInput[i]=='"' ) *(z++) = '"';
161704 *(z++) = zInput[i];
161705 }
161706 *(z++) = '"';
161707 *(z++) = '\0';
161708 }
161709 return zRet;
161710}
161711
161712/*
161713** Return a list of comma separated SQL expressions and a FROM clause that
161714** could be used in a SELECT statement such as the following:
161715**
161716** SELECT <list of expressions> FROM %_content AS x ...
161717**
161718** to return the docid, followed by each column of text data in order
161719** from left to write. If parameter zFunc is not NULL, then instead of
161720** being returned directly each column of text data is passed to an SQL
161721** function named zFunc first. For example, if zFunc is "unzip" and the
161722** table has the three user-defined columns "a", "b", and "c", the following
161723** string is returned:
161724**
161725** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
161726**
161727** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
161728** is the responsibility of the caller to eventually free it.
161729**
161730** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
161731** a NULL pointer is returned). Otherwise, if an OOM error is encountered
161732** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
161733** no error occurs, *pRc is left unmodified.
161734*/
161735static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
161736 char *zRet = 0;
161737 char *zFree = 0;
161738 char *zFunction;
161739 int i;
161740
161741 if( p->zContentTbl==0 ){
161742 if( !zFunc ){
161743 zFunction = "";
161744 }else{
161745 zFree = zFunction = fts3QuoteId(zFunc);
161746 }
161747 fts3Appendf(pRc, &zRet, "docid");
161748 for(i=0; i<p->nColumn; i++){
161749 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
161750 }
161751 if( p->zLanguageid ){
161752 fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
161753 }
161754 sqlite3_free(zFree);
161755 }else{
161756 fts3Appendf(pRc, &zRet, "rowid");
161757 for(i=0; i<p->nColumn; i++){
161758 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
161759 }
161760 if( p->zLanguageid ){
161761 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
161762 }
161763 }
161764 fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
161765 p->zDb,
161766 (p->zContentTbl ? p->zContentTbl : p->zName),
161767 (p->zContentTbl ? "" : "_content")
161768 );
161769 return zRet;
161770}
161771
161772/*
161773** Return a list of N comma separated question marks, where N is the number
161774** of columns in the %_content table (one for the docid plus one for each
161775** user-defined text column).
161776**
161777** If argument zFunc is not NULL, then all but the first question mark
161778** is preceded by zFunc and an open bracket, and followed by a closed
161779** bracket. For example, if zFunc is "zip" and the FTS3 table has three
161780** user-defined text columns, the following string is returned:
161781**
161782** "?, zip(?), zip(?), zip(?)"
161783**
161784** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
161785** is the responsibility of the caller to eventually free it.
161786**
161787** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
161788** a NULL pointer is returned). Otherwise, if an OOM error is encountered
161789** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
161790** no error occurs, *pRc is left unmodified.
161791*/
161792static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
161793 char *zRet = 0;
161794 char *zFree = 0;
161795 char *zFunction;
161796 int i;
161797
161798 if( !zFunc ){
161799 zFunction = "";
161800 }else{
161801 zFree = zFunction = fts3QuoteId(zFunc);
161802 }
161803 fts3Appendf(pRc, &zRet, "?");
161804 for(i=0; i<p->nColumn; i++){
161805 fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
161806 }
161807 if( p->zLanguageid ){
161808 fts3Appendf(pRc, &zRet, ", ?");
161809 }
161810 sqlite3_free(zFree);
161811 return zRet;
161812}
161813
161814/*
161815** This function interprets the string at (*pp) as a non-negative integer
161816** value. It reads the integer and sets *pnOut to the value read, then
161817** sets *pp to point to the byte immediately following the last byte of
161818** the integer value.
161819**
161820** Only decimal digits ('0'..'9') may be part of an integer value.
161821**
161822** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
161823** the output value undefined. Otherwise SQLITE_OK is returned.
161824**
161825** This function is used when parsing the "prefix=" FTS4 parameter.
161826*/
161827static int fts3GobbleInt(const char **pp, int *pnOut){
161828 const int MAX_NPREFIX = 10000000;
161829 const char *p; /* Iterator pointer */
161830 int nInt = 0; /* Output value */
161831
161832 for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
161833 nInt = nInt * 10 + (p[0] - '0');
161834 if( nInt>MAX_NPREFIX ){
161835 nInt = 0;
161836 break;
161837 }
161838 }
161839 if( p==*pp ) return SQLITE_ERROR1;
161840 *pnOut = nInt;
161841 *pp = p;
161842 return SQLITE_OK0;
161843}
161844
161845/*
161846** This function is called to allocate an array of Fts3Index structures
161847** representing the indexes maintained by the current FTS table. FTS tables
161848** always maintain the main "terms" index, but may also maintain one or
161849** more "prefix" indexes, depending on the value of the "prefix=" parameter
161850** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
161851**
161852** Argument zParam is passed the value of the "prefix=" option if one was
161853** specified, or NULL otherwise.
161854**
161855** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
161856** the allocated array. *pnIndex is set to the number of elements in the
161857** array. If an error does occur, an SQLite error code is returned.
161858**
161859** Regardless of whether or not an error is returned, it is the responsibility
161860** of the caller to call sqlite3_free() on the output array to free it.
161861*/
161862static int fts3PrefixParameter(
161863 const char *zParam, /* ABC in prefix=ABC parameter to parse */
161864 int *pnIndex, /* OUT: size of *apIndex[] array */
161865 struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
161866){
161867 struct Fts3Index *aIndex; /* Allocated array */
161868 int nIndex = 1; /* Number of entries in array */
161869
161870 if( zParam && zParam[0] ){
161871 const char *p;
161872 nIndex++;
161873 for(p=zParam; *p; p++){
161874 if( *p==',' ) nIndex++;
161875 }
161876 }
161877
161878 aIndex = sqlite3_malloc64(sizeof(struct Fts3Index) * nIndex);
161879 *apIndex = aIndex;
161880 if( !aIndex ){
161881 return SQLITE_NOMEM7;
161882 }
161883
161884 memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
161885 if( zParam ){
161886 const char *p = zParam;
161887 int i;
161888 for(i=1; i<nIndex; i++){
161889 int nPrefix = 0;
161890 if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR1;
161891 assert( nPrefix>=0 )((void) (0));
161892 if( nPrefix==0 ){
161893 nIndex--;
161894 i--;
161895 }else{
161896 aIndex[i].nPrefix = nPrefix;
161897 }
161898 p++;
161899 }
161900 }
161901
161902 *pnIndex = nIndex;
161903 return SQLITE_OK0;
161904}
161905
161906/*
161907** This function is called when initializing an FTS4 table that uses the
161908** content=xxx option. It determines the number of and names of the columns
161909** of the new FTS4 table.
161910**
161911** The third argument passed to this function is the value passed to the
161912** config=xxx option (i.e. "xxx"). This function queries the database for
161913** a table of that name. If found, the output variables are populated
161914** as follows:
161915**
161916** *pnCol: Set to the number of columns table xxx has,
161917**
161918** *pnStr: Set to the total amount of space required to store a copy
161919** of each columns name, including the nul-terminator.
161920**
161921** *pazCol: Set to point to an array of *pnCol strings. Each string is
161922** the name of the corresponding column in table xxx. The array
161923** and its contents are allocated using a single allocation. It
161924** is the responsibility of the caller to free this allocation
161925** by eventually passing the *pazCol value to sqlite3_free().
161926**
161927** If the table cannot be found, an error code is returned and the output
161928** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
161929** returned (and the output variables are undefined).
161930*/
161931static int fts3ContentColumns(
161932 sqlite3 *db, /* Database handle */
161933 const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
161934 const char *zTbl, /* Name of content table */
161935 const char ***pazCol, /* OUT: Malloc'd array of column names */
161936 int *pnCol, /* OUT: Size of array *pazCol */
161937 int *pnStr, /* OUT: Bytes of string content */
161938 char **pzErr /* OUT: error message */
161939){
161940 int rc = SQLITE_OK0; /* Return code */
161941 char *zSql; /* "SELECT *" statement on zTbl */
161942 sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
161943
161944 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
161945 if( !zSql ){
161946 rc = SQLITE_NOMEM7;
161947 }else{
161948 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
161949 if( rc!=SQLITE_OK0 ){
161950 sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
161951 }
161952 }
161953 sqlite3_free(zSql);
161954
161955 if( rc==SQLITE_OK0 ){
161956 const char **azCol; /* Output array */
161957 sqlite3_int64 nStr = 0; /* Size of all column names (incl. 0x00) */
161958 int nCol; /* Number of table columns */
161959 int i; /* Used to iterate through columns */
161960
161961 /* Loop through the returned columns. Set nStr to the number of bytes of
161962 ** space required to store a copy of each column name, including the
161963 ** nul-terminator byte. */
161964 nCol = sqlite3_column_count(pStmt);
161965 for(i=0; i<nCol; i++){
161966 const char *zCol = sqlite3_column_name(pStmt, i);
161967 nStr += strlen(zCol) + 1;
161968 }
161969
161970 /* Allocate and populate the array to return. */
161971 azCol = (const char **)sqlite3_malloc64(sizeof(char *) * nCol + nStr);
161972 if( azCol==0 ){
161973 rc = SQLITE_NOMEM7;
161974 }else{
161975 char *p = (char *)&azCol[nCol];
161976 for(i=0; i<nCol; i++){
161977 const char *zCol = sqlite3_column_name(pStmt, i);
161978 int n = (int)strlen(zCol)+1;
161979 memcpy(p, zCol, n);
161980 azCol[i] = p;
161981 p += n;
161982 }
161983 }
161984 sqlite3_finalize(pStmt);
161985
161986 /* Set the output variables. */
161987 *pnCol = nCol;
161988 *pnStr = nStr;
161989 *pazCol = azCol;
161990 }
161991
161992 return rc;
161993}
161994
161995/*
161996** This function is the implementation of both the xConnect and xCreate
161997** methods of the FTS3 virtual table.
161998**
161999** The argv[] array contains the following:
162000**
162001** argv[0] -> module name ("fts3" or "fts4")
162002** argv[1] -> database name
162003** argv[2] -> table name
162004** argv[...] -> "column name" and other module argument fields.
162005*/
162006static int fts3InitVtab(
162007 int isCreate, /* True for xCreate, false for xConnect */
162008 sqlite3 *db, /* The SQLite database connection */
162009 void *pAux, /* Hash table containing tokenizers */
162010 int argc, /* Number of elements in argv array */
162011 const char * const *argv, /* xCreate/xConnect argument array */
162012 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
162013 char **pzErr /* Write any error message here */
162014){
162015 Fts3Hash *pHash = (Fts3Hash *)pAux;
162016 Fts3Table *p = 0; /* Pointer to allocated vtab */
162017 int rc = SQLITE_OK0; /* Return code */
162018 int i; /* Iterator variable */
162019 sqlite3_int64 nByte; /* Size of allocation used for *p */
162020 int iCol; /* Column index */
162021 int nString = 0; /* Bytes required to hold all column names */
162022 int nCol = 0; /* Number of columns in the FTS table */
162023 char *zCsr; /* Space for holding column names */
162024 int nDb; /* Bytes required to hold database name */
162025 int nName; /* Bytes required to hold table name */
162026 int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
162027 const char **aCol; /* Array of column names */
162028 sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
162029
162030 int nIndex = 0; /* Size of aIndex[] array */
162031 struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
162032
162033 /* The results of parsing supported FTS4 key=value options: */
162034 int bNoDocsize = 0; /* True to omit %_docsize table */
162035 int bDescIdx = 0; /* True to store descending indexes */
162036 char *zPrefix = 0; /* Prefix parameter value (or NULL) */
162037 char *zCompress = 0; /* compress=? parameter (or NULL) */
162038 char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
162039 char *zContent = 0; /* content=? parameter (or NULL) */
162040 char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
162041 char **azNotindexed = 0; /* The set of notindexed= columns */
162042 int nNotindexed = 0; /* Size of azNotindexed[] array */
162043
162044 assert( strlen(argv[0])==4 )((void) (0));
162045 assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)((void) (0))
162046 || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)((void) (0))
162047 )((void) (0));
162048
162049 nDb = (int)strlen(argv[1]) + 1;
162050 nName = (int)strlen(argv[2]) + 1;
162051
162052 nByte = sizeof(const char *) * (argc-2);
162053 aCol = (const char **)sqlite3_malloc64(nByte);
162054 if( aCol ){
162055 memset((void*)aCol, 0, nByte);
162056 azNotindexed = (char **)sqlite3_malloc64(nByte);
162057 }
162058 if( azNotindexed ){
162059 memset(azNotindexed, 0, nByte);
162060 }
162061 if( !aCol || !azNotindexed ){
162062 rc = SQLITE_NOMEM7;
162063 goto fts3_init_out;
162064 }
162065
162066 /* Loop through all of the arguments passed by the user to the FTS3/4
162067 ** module (i.e. all the column names and special arguments). This loop
162068 ** does the following:
162069 **
162070 ** + Figures out the number of columns the FTSX table will have, and
162071 ** the number of bytes of space that must be allocated to store copies
162072 ** of the column names.
162073 **
162074 ** + If there is a tokenizer specification included in the arguments,
162075 ** initializes the tokenizer pTokenizer.
162076 */
162077 for(i=3; rc==SQLITE_OK0 && i<argc; i++){
162078 char const *z = argv[i];
162079 int nKey;
162080 char *zVal;
162081
162082 /* Check if this is a tokenizer specification */
162083 if( !pTokenizer
162084 && strlen(z)>8
162085 && 0==sqlite3_strnicmp(z, "tokenize", 8)
162086 && 0==sqlite3Fts3IsIdChar(z[8])
162087 ){
162088 rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
162089 }
162090
162091 /* Check if it is an FTS4 special argument. */
162092 else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
162093 struct Fts4Option {
162094 const char *zOpt;
162095 int nOpt;
162096 } aFts4Opt[] = {
162097 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
162098 { "prefix", 6 }, /* 1 -> PREFIX */
162099 { "compress", 8 }, /* 2 -> COMPRESS */
162100 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
162101 { "order", 5 }, /* 4 -> ORDER */
162102 { "content", 7 }, /* 5 -> CONTENT */
162103 { "languageid", 10 }, /* 6 -> LANGUAGEID */
162104 { "notindexed", 10 } /* 7 -> NOTINDEXED */
162105 };
162106
162107 int iOpt;
162108 if( !zVal ){
162109 rc = SQLITE_NOMEM7;
162110 }else{
162111 for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
162112 struct Fts4Option *pOp = &aFts4Opt[iOpt];
162113 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
162114 break;
162115 }
162116 }
162117 switch( iOpt ){
162118 case 0: /* MATCHINFO */
162119 if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
162120 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
162121 rc = SQLITE_ERROR1;
162122 }
162123 bNoDocsize = 1;
162124 break;
162125
162126 case 1: /* PREFIX */
162127 sqlite3_free(zPrefix);
162128 zPrefix = zVal;
162129 zVal = 0;
162130 break;
162131
162132 case 2: /* COMPRESS */
162133 sqlite3_free(zCompress);
162134 zCompress = zVal;
162135 zVal = 0;
162136 break;
162137
162138 case 3: /* UNCOMPRESS */
162139 sqlite3_free(zUncompress);
162140 zUncompress = zVal;
162141 zVal = 0;
162142 break;
162143
162144 case 4: /* ORDER */
162145 if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
162146 && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
162147 ){
162148 sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
162149 rc = SQLITE_ERROR1;
162150 }
162151 bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
162152 break;
162153
162154 case 5: /* CONTENT */
162155 sqlite3_free(zContent);
162156 zContent = zVal;
162157 zVal = 0;
162158 break;
162159
162160 case 6: /* LANGUAGEID */
162161 assert( iOpt==6 )((void) (0));
162162 sqlite3_free(zLanguageid);
162163 zLanguageid = zVal;
162164 zVal = 0;
162165 break;
162166
162167 case 7: /* NOTINDEXED */
162168 azNotindexed[nNotindexed++] = zVal;
162169 zVal = 0;
162170 break;
162171
162172 default:
162173 assert( iOpt==SizeofArray(aFts4Opt) )((void) (0));
162174 sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
162175 rc = SQLITE_ERROR1;
162176 break;
162177 }
162178 sqlite3_free(zVal);
162179 }
162180 }
162181
162182 /* Otherwise, the argument is a column name. */
162183 else {
162184 nString += (int)(strlen(z) + 1);
162185 aCol[nCol++] = z;
162186 }
162187 }
162188
162189 /* If a content=xxx option was specified, the following:
162190 **
162191 ** 1. Ignore any compress= and uncompress= options.
162192 **
162193 ** 2. If no column names were specified as part of the CREATE VIRTUAL
162194 ** TABLE statement, use all columns from the content table.
162195 */
162196 if( rc==SQLITE_OK0 && zContent ){
162197 sqlite3_free(zCompress);
162198 sqlite3_free(zUncompress);
162199 zCompress = 0;
162200 zUncompress = 0;
162201 if( nCol==0 ){
162202 sqlite3_free((void*)aCol);
162203 aCol = 0;
162204 rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
162205
162206 /* If a languageid= option was specified, remove the language id
162207 ** column from the aCol[] array. */
162208 if( rc==SQLITE_OK0 && zLanguageid ){
162209 int j;
162210 for(j=0; j<nCol; j++){
162211 if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
162212 int k;
162213 for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
162214 nCol--;
162215 break;
162216 }
162217 }
162218 }
162219 }
162220 }
162221 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
162222
162223 if( nCol==0 ){
162224 assert( nString==0 )((void) (0));
162225 aCol[0] = "content";
162226 nString = 8;
162227 nCol = 1;
162228 }
162229
162230 if( pTokenizer==0 ){
162231 rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
162232 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
162233 }
162234 assert( pTokenizer )((void) (0));
162235
162236 rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
162237 if( rc==SQLITE_ERROR1 ){
162238 assert( zPrefix )((void) (0));
162239 sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
162240 }
162241 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
162242
162243 /* Allocate and populate the Fts3Table structure. */
162244 nByte = sizeof(Fts3Table) + /* Fts3Table */
162245 nCol * sizeof(char *) + /* azColumn */
162246 nIndex * sizeof(struct Fts3Index) + /* aIndex */
162247 nCol * sizeof(u8) + /* abNotindexed */
162248 nName + /* zName */
162249 nDb + /* zDb */
162250 nString; /* Space for azColumn strings */
162251 p = (Fts3Table*)sqlite3_malloc64(nByte);
162252 if( p==0 ){
162253 rc = SQLITE_NOMEM7;
162254 goto fts3_init_out;
162255 }
162256 memset(p, 0, nByte);
162257 p->db = db;
162258 p->nColumn = nCol;
162259 p->nPendingData = 0;
162260 p->azColumn = (char **)&p[1];
162261 p->pTokenizer = pTokenizer;
162262 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
162263 p->bHasDocsize = (isFts4 && bNoDocsize==0);
162264 p->bHasStat = (u8)isFts4;
162265 p->bFts4 = (u8)isFts4;
162266 p->bDescIdx = (u8)bDescIdx;
162267 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
162268 p->zContentTbl = zContent;
162269 p->zLanguageid = zLanguageid;
162270 zContent = 0;
162271 zLanguageid = 0;
162272 TESTONLY( p->inTransaction = -1 );
162273 TESTONLY( p->mxSavepoint = -1 );
162274
162275 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
162276 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
162277 p->nIndex = nIndex;
162278 for(i=0; i<nIndex; i++){
162279 fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
162280 }
162281 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
162282
162283 /* Fill in the zName and zDb fields of the vtab structure. */
162284 zCsr = (char *)&p->abNotindexed[nCol];
162285 p->zName = zCsr;
162286 memcpy(zCsr, argv[2], nName);
162287 zCsr += nName;
162288 p->zDb = zCsr;
162289 memcpy(zCsr, argv[1], nDb);
162290 zCsr += nDb;
162291
162292 /* Fill in the azColumn array */
162293 for(iCol=0; iCol<nCol; iCol++){
162294 char *z;
162295 int n = 0;
162296 z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
162297 if( n>0 ){
162298 memcpy(zCsr, z, n);
162299 }
162300 zCsr[n] = '\0';
162301 sqlite3Fts3Dequote(zCsr);
162302 p->azColumn[iCol] = zCsr;
162303 zCsr += n+1;
162304 assert( zCsr <= &((char *)p)[nByte] )((void) (0));
162305 }
162306
162307 /* Fill in the abNotindexed array */
162308 for(iCol=0; iCol<nCol; iCol++){
162309 int n = (int)strlen(p->azColumn[iCol]);
162310 for(i=0; i<nNotindexed; i++){
162311 char *zNot = azNotindexed[i];
162312 if( zNot && n==(int)strlen(zNot)
162313 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
162314 ){
162315 p->abNotindexed[iCol] = 1;
162316 sqlite3_free(zNot);
162317 azNotindexed[i] = 0;
162318 }
162319 }
162320 }
162321 for(i=0; i<nNotindexed; i++){
162322 if( azNotindexed[i] ){
162323 sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
162324 rc = SQLITE_ERROR1;
162325 }
162326 }
162327
162328 if( rc==SQLITE_OK0 && (zCompress==0)!=(zUncompress==0) ){
162329 char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
162330 rc = SQLITE_ERROR1;
162331 sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
162332 }
162333 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
162334 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
162335 if( rc!=SQLITE_OK0 ) goto fts3_init_out;
162336
162337 /* If this is an xCreate call, create the underlying tables in the
162338 ** database. TODO: For xConnect(), it could verify that said tables exist.
162339 */
162340 if( isCreate ){
162341 rc = fts3CreateTables(p);
162342 }
162343
162344 /* Check to see if a legacy fts3 table has been "upgraded" by the
162345 ** addition of a %_stat table so that it can use incremental merge.
162346 */
162347 if( !isFts4 && !isCreate ){
162348 p->bHasStat = 2;
162349 }
162350
162351 /* Figure out the page-size for the database. This is required in order to
162352 ** estimate the cost of loading large doclists from the database. */
162353 fts3DatabasePageSize(&rc, p);
162354 p->nNodeSize = p->nPgsz-35;
162355
162356 /* Declare the table schema to SQLite. */
162357 fts3DeclareVtab(&rc, p);
162358
162359fts3_init_out:
162360 sqlite3_free(zPrefix);
162361 sqlite3_free(aIndex);
162362 sqlite3_free(zCompress);
162363 sqlite3_free(zUncompress);
162364 sqlite3_free(zContent);
162365 sqlite3_free(zLanguageid);
162366 for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
162367 sqlite3_free((void *)aCol);
162368 sqlite3_free((void *)azNotindexed);
162369 if( rc!=SQLITE_OK0 ){
162370 if( p ){
162371 fts3DisconnectMethod((sqlite3_vtab *)p);
162372 }else if( pTokenizer ){
162373 pTokenizer->pModule->xDestroy(pTokenizer);
162374 }
162375 }else{
162376 assert( p->pSegments==0 )((void) (0));
162377 *ppVTab = &p->base;
162378 }
162379 return rc;
162380}
162381
162382/*
162383** The xConnect() and xCreate() methods for the virtual table. All the
162384** work is done in function fts3InitVtab().
162385*/
162386static int fts3ConnectMethod(
162387 sqlite3 *db, /* Database connection */
162388 void *pAux, /* Pointer to tokenizer hash table */
162389 int argc, /* Number of elements in argv array */
162390 const char * const *argv, /* xCreate/xConnect argument array */
162391 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
162392 char **pzErr /* OUT: sqlite3_malloc'd error message */
162393){
162394 return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
162395}
162396static int fts3CreateMethod(
162397 sqlite3 *db, /* Database connection */
162398 void *pAux, /* Pointer to tokenizer hash table */
162399 int argc, /* Number of elements in argv array */
162400 const char * const *argv, /* xCreate/xConnect argument array */
162401 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
162402 char **pzErr /* OUT: sqlite3_malloc'd error message */
162403){
162404 return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
162405}
162406
162407/*
162408** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
162409** extension is currently being used by a version of SQLite too old to
162410** support estimatedRows. In that case this function is a no-op.
162411*/
162412static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
162413#if SQLITE_VERSION_NUMBER3029000>=3008002
162414 if( sqlite3_libversion_number()>=3008002 ){
162415 pIdxInfo->estimatedRows = nRow;
162416 }
162417#endif
162418}
162419
162420/*
162421** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
162422** extension is currently being used by a version of SQLite too old to
162423** support index-info flags. In that case this function is a no-op.
162424*/
162425static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
162426#if SQLITE_VERSION_NUMBER3029000>=3008012
162427 if( sqlite3_libversion_number()>=3008012 ){
162428 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE1;
162429 }
162430#endif
162431}
162432
162433/*
162434** Implementation of the xBestIndex method for FTS3 tables. There
162435** are three possible strategies, in order of preference:
162436**
162437** 1. Direct lookup by rowid or docid.
162438** 2. Full-text search using a MATCH operator on a non-docid column.
162439** 3. Linear scan of %_content table.
162440*/
162441static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
162442 Fts3Table *p = (Fts3Table *)pVTab;
162443 int i; /* Iterator variable */
162444 int iCons = -1; /* Index of constraint to use */
162445
162446 int iLangidCons = -1; /* Index of langid=x constraint, if present */
162447 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
162448 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
162449 int iIdx;
162450
162451 /* By default use a full table scan. This is an expensive option,
162452 ** so search through the constraints to see if a more efficient
162453 ** strategy is possible.
162454 */
162455 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
162456 pInfo->estimatedCost = 5000000;
162457 for(i=0; i<pInfo->nConstraint; i++){
162458 int bDocid; /* True if this constraint is on docid */
162459 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
162460 if( pCons->usable==0 ){
162461 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH64 ){
162462 /* There exists an unusable MATCH constraint. This means that if
162463 ** the planner does elect to use the results of this call as part
162464 ** of the overall query plan the user will see an "unable to use
162465 ** function MATCH in the requested context" error. To discourage
162466 ** this, return a very high cost here. */
162467 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
162468 pInfo->estimatedCost = 1e50;
162469 fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
162470 return SQLITE_OK0;
162471 }
162472 continue;
162473 }
162474
162475 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
162476
162477 /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
162478 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ2 && bDocid ){
162479 pInfo->idxNum = FTS3_DOCID_SEARCH;
162480 pInfo->estimatedCost = 1.0;
162481 iCons = i;
162482 }
162483
162484 /* A MATCH constraint. Use a full-text search.
162485 **
162486 ** If there is more than one MATCH constraint available, use the first
162487 ** one encountered. If there is both a MATCH constraint and a direct
162488 ** rowid/docid lookup, prefer the MATCH strategy. This is done even
162489 ** though the rowid/docid lookup is faster than a MATCH query, selecting
162490 ** it would lead to an "unable to use function MATCH in the requested
162491 ** context" error.
162492 */
162493 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH64
162494 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
162495 ){
162496 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
162497 pInfo->estimatedCost = 2.0;
162498 iCons = i;
162499 }
162500
162501 /* Equality constraint on the langid column */
162502 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ2
162503 && pCons->iColumn==p->nColumn + 2
162504 ){
162505 iLangidCons = i;
162506 }
162507
162508 if( bDocid ){
162509 switch( pCons->op ){
162510 case SQLITE_INDEX_CONSTRAINT_GE32:
162511 case SQLITE_INDEX_CONSTRAINT_GT4:
162512 iDocidGe = i;
162513 break;
162514
162515 case SQLITE_INDEX_CONSTRAINT_LE8:
162516 case SQLITE_INDEX_CONSTRAINT_LT16:
162517 iDocidLe = i;
162518 break;
162519 }
162520 }
162521 }
162522
162523 /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
162524 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
162525
162526 iIdx = 1;
162527 if( iCons>=0 ){
162528 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
162529 pInfo->aConstraintUsage[iCons].omit = 1;
162530 }
162531 if( iLangidCons>=0 ){
162532 pInfo->idxNum |= FTS3_HAVE_LANGID;
162533 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
162534 }
162535 if( iDocidGe>=0 ){
162536 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
162537 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
162538 }
162539 if( iDocidLe>=0 ){
162540 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
162541 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
162542 }
162543
162544 /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
162545 ** docid) order. Both ascending and descending are possible.
162546 */
162547 if( pInfo->nOrderBy==1 ){
162548 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
162549 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
162550 if( pOrder->desc ){
162551 pInfo->idxStr = "DESC";
162552 }else{
162553 pInfo->idxStr = "ASC";
162554 }
162555 pInfo->orderByConsumed = 1;
162556 }
162557 }
162558
162559 assert( p->pSegments==0 )((void) (0));
162560 return SQLITE_OK0;
162561}
162562
162563/*
162564** Implementation of xOpen method.
162565*/
162566static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
162567 sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
162568
162569 UNUSED_PARAMETER(pVTab)(void)(pVTab);
162570
162571 /* Allocate a buffer large enough for an Fts3Cursor structure. If the
162572 ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
162573 ** if the allocation fails, return SQLITE_NOMEM.
162574 */
162575 *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
162576 if( !pCsr ){
162577 return SQLITE_NOMEM7;
162578 }
162579 memset(pCsr, 0, sizeof(Fts3Cursor));
162580 return SQLITE_OK0;
162581}
162582
162583/*
162584** Finalize the statement handle at pCsr->pStmt.
162585**
162586** Or, if that statement handle is one created by fts3CursorSeekStmt(),
162587** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
162588** pointer there instead of finalizing it.
162589*/
162590static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
162591 if( pCsr->bSeekStmt ){
162592 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
162593 if( p->pSeekStmt==0 ){
162594 p->pSeekStmt = pCsr->pStmt;
162595 sqlite3_reset(pCsr->pStmt);
162596 pCsr->pStmt = 0;
162597 }
162598 pCsr->bSeekStmt = 0;
162599 }
162600 sqlite3_finalize(pCsr->pStmt);
162601}
162602
162603/*
162604** Free all resources currently held by the cursor passed as the only
162605** argument.
162606*/
162607static void fts3ClearCursor(Fts3Cursor *pCsr){
162608 fts3CursorFinalizeStmt(pCsr);
162609 sqlite3Fts3FreeDeferredTokens(pCsr);
162610 sqlite3_free(pCsr->aDoclist);
162611 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
162612 sqlite3Fts3ExprFree(pCsr->pExpr);
162613 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
162614}
162615
162616/*
162617** Close the cursor. For additional information see the documentation
162618** on the xClose method of the virtual table interface.
162619*/
162620static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
162621 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
162622 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
162623 fts3ClearCursor(pCsr);
162624 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
162625 sqlite3_free(pCsr);
162626 return SQLITE_OK0;
162627}
162628
162629/*
162630** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
162631** compose and prepare an SQL statement of the form:
162632**
162633** "SELECT <columns> FROM %_content WHERE rowid = ?"
162634**
162635** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
162636** it. If an error occurs, return an SQLite error code.
162637*/
162638static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
162639 int rc = SQLITE_OK0;
162640 if( pCsr->pStmt==0 ){
162641 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
162642 char *zSql;
162643 if( p->pSeekStmt ){
162644 pCsr->pStmt = p->pSeekStmt;
162645 p->pSeekStmt = 0;
162646 }else{
162647 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
162648 if( !zSql ) return SQLITE_NOMEM7;
162649 rc = sqlite3_prepare_v3(p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT0x01,&pCsr->pStmt,0);
162650 sqlite3_free(zSql);
162651 }
162652 if( rc==SQLITE_OK0 ) pCsr->bSeekStmt = 1;
162653 }
162654 return rc;
162655}
162656
162657/*
162658** Position the pCsr->pStmt statement so that it is on the row
162659** of the %_content table that contains the last match. Return
162660** SQLITE_OK on success.
162661*/
162662static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
162663 int rc = SQLITE_OK0;
162664 if( pCsr->isRequireSeek ){
162665 rc = fts3CursorSeekStmt(pCsr);
162666 if( rc==SQLITE_OK0 ){
162667 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
162668 pCsr->isRequireSeek = 0;
162669 if( SQLITE_ROW100==sqlite3_step(pCsr->pStmt) ){
162670 return SQLITE_OK0;
162671 }else{
162672 rc = sqlite3_reset(pCsr->pStmt);
162673 if( rc==SQLITE_OK0 && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
162674 /* If no row was found and no error has occurred, then the %_content
162675 ** table is missing a row that is present in the full-text index.
162676 ** The data structures are corrupt. */
162677 rc = FTS_CORRUPT_VTAB;
162678 pCsr->isEof = 1;
162679 }
162680 }
162681 }
162682 }
162683
162684 if( rc!=SQLITE_OK0 && pContext ){
162685 sqlite3_result_error_code(pContext, rc);
162686 }
162687 return rc;
162688}
162689
162690/*
162691** This function is used to process a single interior node when searching
162692** a b-tree for a term or term prefix. The node data is passed to this
162693** function via the zNode/nNode parameters. The term to search for is
162694** passed in zTerm/nTerm.
162695**
162696** If piFirst is not NULL, then this function sets *piFirst to the blockid
162697** of the child node that heads the sub-tree that may contain the term.
162698**
162699** If piLast is not NULL, then *piLast is set to the right-most child node
162700** that heads a sub-tree that may contain a term for which zTerm/nTerm is
162701** a prefix.
162702**
162703** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
162704*/
162705static int fts3ScanInteriorNode(
162706 const char *zTerm, /* Term to select leaves for */
162707 int nTerm, /* Size of term zTerm in bytes */
162708 const char *zNode, /* Buffer containing segment interior node */
162709 int nNode, /* Size of buffer at zNode */
162710 sqlite3_int64 *piFirst, /* OUT: Selected child node */
162711 sqlite3_int64 *piLast /* OUT: Selected child node */
162712){
162713 int rc = SQLITE_OK0; /* Return code */
162714 const char *zCsr = zNode; /* Cursor to iterate through node */
162715 const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
162716 char *zBuffer = 0; /* Buffer to load terms into */
162717 i64 nAlloc = 0; /* Size of allocated buffer */
162718 int isFirstTerm = 1; /* True when processing first term on page */
162719 sqlite3_int64 iChild; /* Block id of child node to descend to */
162720
162721 /* Skip over the 'height' varint that occurs at the start of every
162722 ** interior node. Then load the blockid of the left-child of the b-tree
162723 ** node into variable iChild.
162724 **
162725 ** Even if the data structure on disk is corrupted, this (reading two
162726 ** varints from the buffer) does not risk an overread. If zNode is a
162727 ** root node, then the buffer comes from a SELECT statement. SQLite does
162728 ** not make this guarantee explicitly, but in practice there are always
162729 ** either more than 20 bytes of allocated space following the nNode bytes of
162730 ** contents, or two zero bytes. Or, if the node is read from the %_segments
162731 ** table, then there are always 20 bytes of zeroed padding following the
162732 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
162733 */
162734 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
162735 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
162736 if( zCsr>zEnd ){
162737 return FTS_CORRUPT_VTAB;
162738 }
162739
162740 while( zCsr<zEnd && (piFirst || piLast) ){
162741 int cmp; /* memcmp() result */
162742 int nSuffix; /* Size of term suffix */
162743 int nPrefix = 0; /* Size of term prefix */
162744 int nBuffer; /* Total term size */
162745
162746 /* Load the next term on the node into zBuffer. Use realloc() to expand
162747 ** the size of zBuffer if required. */
162748 if( !isFirstTerm ){
162749 zCsr += fts3GetVarint32(zCsr, &nPrefix);
162750 }
162751 isFirstTerm = 0;
162752 zCsr += fts3GetVarint32(zCsr, &nSuffix);
162753
162754 assert( nPrefix>=0 && nSuffix>=0 )((void) (0));
162755 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
162756 rc = FTS_CORRUPT_VTAB;
162757 goto finish_scan;
162758 }
162759 if( (i64)nPrefix+nSuffix>nAlloc ){
162760 char *zNew;
162761 nAlloc = ((i64)nPrefix+nSuffix) * 2;
162762 zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
162763 if( !zNew ){
162764 rc = SQLITE_NOMEM7;
162765 goto finish_scan;
162766 }
162767 zBuffer = zNew;
162768 }
162769 assert( zBuffer )((void) (0));
162770 memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
162771 nBuffer = nPrefix + nSuffix;
162772 zCsr += nSuffix;
162773
162774 /* Compare the term we are searching for with the term just loaded from
162775 ** the interior node. If the specified term is greater than or equal
162776 ** to the term from the interior node, then all terms on the sub-tree
162777 ** headed by node iChild are smaller than zTerm. No need to search
162778 ** iChild.
162779 **
162780 ** If the interior node term is larger than the specified term, then
162781 ** the tree headed by iChild may contain the specified term.
162782 */
162783 cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
162784 if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
162785 *piFirst = iChild;
162786 piFirst = 0;
162787 }
162788
162789 if( piLast && cmp<0 ){
162790 *piLast = iChild;
162791 piLast = 0;
162792 }
162793
162794 iChild++;
162795 };
162796
162797 if( piFirst ) *piFirst = iChild;
162798 if( piLast ) *piLast = iChild;
162799
162800 finish_scan:
162801 sqlite3_free(zBuffer);
162802 return rc;
162803}
162804
162805
162806/*
162807** The buffer pointed to by argument zNode (size nNode bytes) contains an
162808** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
162809** contains a term. This function searches the sub-tree headed by the zNode
162810** node for the range of leaf nodes that may contain the specified term
162811** or terms for which the specified term is a prefix.
162812**
162813** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
162814** left-most leaf node in the tree that may contain the specified term.
162815** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
162816** right-most leaf node that may contain a term for which the specified
162817** term is a prefix.
162818**
162819** It is possible that the range of returned leaf nodes does not contain
162820** the specified term or any terms for which it is a prefix. However, if the
162821** segment does contain any such terms, they are stored within the identified
162822** range. Because this function only inspects interior segment nodes (and
162823** never loads leaf nodes into memory), it is not possible to be sure.
162824**
162825** If an error occurs, an error code other than SQLITE_OK is returned.
162826*/
162827static int fts3SelectLeaf(
162828 Fts3Table *p, /* Virtual table handle */
162829 const char *zTerm, /* Term to select leaves for */
162830 int nTerm, /* Size of term zTerm in bytes */
162831 const char *zNode, /* Buffer containing segment interior node */
162832 int nNode, /* Size of buffer at zNode */
162833 sqlite3_int64 *piLeaf, /* Selected leaf node */
162834 sqlite3_int64 *piLeaf2 /* Selected leaf node */
162835){
162836 int rc = SQLITE_OK0; /* Return code */
162837 int iHeight; /* Height of this node in tree */
162838
162839 assert( piLeaf || piLeaf2 )((void) (0));
162840
162841 fts3GetVarint32(zNode, &iHeight);
162842 rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
162843 assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) )((void) (0));
162844
162845 if( rc==SQLITE_OK0 && iHeight>1 ){
162846 char *zBlob = 0; /* Blob read from %_segments table */
162847 int nBlob = 0; /* Size of zBlob in bytes */
162848
162849 if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
162850 rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
162851 if( rc==SQLITE_OK0 ){
162852 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
162853 }
162854 sqlite3_free(zBlob);
162855 piLeaf = 0;
162856 zBlob = 0;
162857 }
162858
162859 if( rc==SQLITE_OK0 ){
162860 rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
162861 }
162862 if( rc==SQLITE_OK0 ){
162863 rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
162864 }
162865 sqlite3_free(zBlob);
162866 }
162867
162868 return rc;
162869}
162870
162871/*
162872** This function is used to create delta-encoded serialized lists of FTS3
162873** varints. Each call to this function appends a single varint to a list.
162874*/
162875static void fts3PutDeltaVarint(
162876 char **pp, /* IN/OUT: Output pointer */
162877 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
162878 sqlite3_int64 iVal /* Write this value to the list */
162879){
162880 assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) )((void) (0));
162881 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
162882 *piPrev = iVal;
162883}
162884
162885/*
162886** When this function is called, *ppPoslist is assumed to point to the
162887** start of a position-list. After it returns, *ppPoslist points to the
162888** first byte after the position-list.
162889**
162890** A position list is list of positions (delta encoded) and columns for
162891** a single document record of a doclist. So, in other words, this
162892** routine advances *ppPoslist so that it points to the next docid in
162893** the doclist, or to the first byte past the end of the doclist.
162894**
162895** If pp is not NULL, then the contents of the position list are copied
162896** to *pp. *pp is set to point to the first byte past the last byte copied
162897** before this function returns.
162898*/
162899static void fts3PoslistCopy(char **pp, char **ppPoslist){
162900 char *pEnd = *ppPoslist;
162901 char c = 0;
162902
162903 /* The end of a position list is marked by a zero encoded as an FTS3
162904 ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
162905 ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
162906 ** of some other, multi-byte, value.
162907 **
162908 ** The following while-loop moves pEnd to point to the first byte that is not
162909 ** immediately preceded by a byte with the 0x80 bit set. Then increments
162910 ** pEnd once more so that it points to the byte immediately following the
162911 ** last byte in the position-list.
162912 */
162913 while( *pEnd | c ){
162914 c = *pEnd++ & 0x80;
162915 testcase( c!=0 && (*pEnd)==0 );
162916 }
162917 pEnd++; /* Advance past the POS_END terminator byte */
162918
162919 if( pp ){
162920 int n = (int)(pEnd - *ppPoslist);
162921 char *p = *pp;
162922 memcpy(p, *ppPoslist, n);
162923 p += n;
162924 *pp = p;
162925 }
162926 *ppPoslist = pEnd;
162927}
162928
162929/*
162930** When this function is called, *ppPoslist is assumed to point to the
162931** start of a column-list. After it returns, *ppPoslist points to the
162932** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
162933**
162934** A column-list is list of delta-encoded positions for a single column
162935** within a single document within a doclist.
162936**
162937** The column-list is terminated either by a POS_COLUMN varint (1) or
162938** a POS_END varint (0). This routine leaves *ppPoslist pointing to
162939** the POS_COLUMN or POS_END that terminates the column-list.
162940**
162941** If pp is not NULL, then the contents of the column-list are copied
162942** to *pp. *pp is set to point to the first byte past the last byte copied
162943** before this function returns. The POS_COLUMN or POS_END terminator
162944** is not copied into *pp.
162945*/
162946static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
162947 char *pEnd = *ppPoslist;
162948 char c = 0;
162949
162950 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
162951 ** not part of a multi-byte varint.
162952 */
162953 while( 0xFE & (*pEnd | c) ){
162954 c = *pEnd++ & 0x80;
162955 testcase( c!=0 && ((*pEnd)&0xfe)==0 );
162956 }
162957 if( pp ){
162958 int n = (int)(pEnd - *ppPoslist);
162959 char *p = *pp;
162960 memcpy(p, *ppPoslist, n);
162961 p += n;
162962 *pp = p;
162963 }
162964 *ppPoslist = pEnd;
162965}
162966
162967/*
162968** Value used to signify the end of an position-list. This is safe because
162969** it is not possible to have a document with 2^31 terms.
162970*/
162971#define POSITION_LIST_END 0x7fffffff
162972
162973/*
162974** This function is used to help parse position-lists. When this function is
162975** called, *pp may point to the start of the next varint in the position-list
162976** being parsed, or it may point to 1 byte past the end of the position-list
162977** (in which case **pp will be a terminator bytes POS_END (0) or
162978** (1)).
162979**
162980** If *pp points past the end of the current position-list, set *pi to
162981** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
162982** increment the current value of *pi by the value read, and set *pp to
162983** point to the next value before returning.
162984**
162985** Before calling this routine *pi must be initialized to the value of
162986** the previous position, or zero if we are reading the first position
162987** in the position-list. Because positions are delta-encoded, the value
162988** of the previous position is needed in order to compute the value of
162989** the next position.
162990*/
162991static void fts3ReadNextPos(
162992 char **pp, /* IN/OUT: Pointer into position-list buffer */
162993 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
162994){
162995 if( (**pp)&0xFE ){
162996 fts3GetDeltaVarint(pp, pi);
162997 *pi -= 2;
162998 }else{
162999 *pi = POSITION_LIST_END;
163000 }
163001}
163002
163003/*
163004** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
163005** the value of iCol encoded as a varint to *pp. This will start a new
163006** column list.
163007**
163008** Set *pp to point to the byte just after the last byte written before
163009** returning (do not modify it if iCol==0). Return the total number of bytes
163010** written (0 if iCol==0).
163011*/
163012static int fts3PutColNumber(char **pp, int iCol){
163013 int n = 0; /* Number of bytes written */
163014 if( iCol ){
163015 char *p = *pp; /* Output pointer */
163016 n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
163017 *p = 0x01;
163018 *pp = &p[n];
163019 }
163020 return n;
163021}
163022
163023/*
163024** Compute the union of two position lists. The output written
163025** into *pp contains all positions of both *pp1 and *pp2 in sorted
163026** order and with any duplicates removed. All pointers are
163027** updated appropriately. The caller is responsible for insuring
163028** that there is enough space in *pp to hold the complete output.
163029*/
163030static int fts3PoslistMerge(
163031 char **pp, /* Output buffer */
163032 char **pp1, /* Left input list */
163033 char **pp2 /* Right input list */
163034){
163035 char *p = *pp;
163036 char *p1 = *pp1;
163037 char *p2 = *pp2;
163038
163039 while( *p1 || *p2 ){
163040 int iCol1; /* The current column index in pp1 */
163041 int iCol2; /* The current column index in pp2 */
163042
163043 if( *p1==POS_COLUMN ){
163044 fts3GetVarint32(&p1[1], &iCol1);
163045 if( iCol1==0 ) return FTS_CORRUPT_VTAB;
163046 }
163047 else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
163048 else iCol1 = 0;
163049
163050 if( *p2==POS_COLUMN ){
163051 fts3GetVarint32(&p2[1], &iCol2);
163052 if( iCol2==0 ) return FTS_CORRUPT_VTAB;
163053 }
163054 else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
163055 else iCol2 = 0;
163056
163057 if( iCol1==iCol2 ){
163058 sqlite3_int64 i1 = 0; /* Last position from pp1 */
163059 sqlite3_int64 i2 = 0; /* Last position from pp2 */
163060 sqlite3_int64 iPrev = 0;
163061 int n = fts3PutColNumber(&p, iCol1);
163062 p1 += n;
163063 p2 += n;
163064
163065 /* At this point, both p1 and p2 point to the start of column-lists
163066 ** for the same column (the column with index iCol1 and iCol2).
163067 ** A column-list is a list of non-negative delta-encoded varints, each
163068 ** incremented by 2 before being stored. Each list is terminated by a
163069 ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
163070 ** and writes the results to buffer p. p is left pointing to the byte
163071 ** after the list written. No terminator (POS_END or POS_COLUMN) is
163072 ** written to the output.
163073 */
163074 fts3GetDeltaVarint(&p1, &i1);
163075 fts3GetDeltaVarint(&p2, &i2);
163076 do {
163077 fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
163078 iPrev -= 2;
163079 if( i1==i2 ){
163080 fts3ReadNextPos(&p1, &i1);
163081 fts3ReadNextPos(&p2, &i2);
163082 }else if( i1<i2 ){
163083 fts3ReadNextPos(&p1, &i1);
163084 }else{
163085 fts3ReadNextPos(&p2, &i2);
163086 }
163087 }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
163088 }else if( iCol1<iCol2 ){
163089 p1 += fts3PutColNumber(&p, iCol1);
163090 fts3ColumnlistCopy(&p, &p1);
163091 }else{
163092 p2 += fts3PutColNumber(&p, iCol2);
163093 fts3ColumnlistCopy(&p, &p2);
163094 }
163095 }
163096
163097 *p++ = POS_END;
163098 *pp = p;
163099 *pp1 = p1 + 1;
163100 *pp2 = p2 + 1;
163101 return SQLITE_OK0;
163102}
163103
163104/*
163105** This function is used to merge two position lists into one. When it is
163106** called, *pp1 and *pp2 must both point to position lists. A position-list is
163107** the part of a doclist that follows each document id. For example, if a row
163108** contains:
163109**
163110** 'a b c'|'x y z'|'a b b a'
163111**
163112** Then the position list for this row for token 'b' would consist of:
163113**
163114** 0x02 0x01 0x02 0x03 0x03 0x00
163115**
163116** When this function returns, both *pp1 and *pp2 are left pointing to the
163117** byte following the 0x00 terminator of their respective position lists.
163118**
163119** If isSaveLeft is 0, an entry is added to the output position list for
163120** each position in *pp2 for which there exists one or more positions in
163121** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
163122** when the *pp1 token appears before the *pp2 token, but not more than nToken
163123** slots before it.
163124**
163125** e.g. nToken==1 searches for adjacent positions.
163126*/
163127static int fts3PoslistPhraseMerge(
163128 char **pp, /* IN/OUT: Preallocated output buffer */
163129 int nToken, /* Maximum difference in token positions */
163130 int isSaveLeft, /* Save the left position */
163131 int isExact, /* If *pp1 is exactly nTokens before *pp2 */
163132 char **pp1, /* IN/OUT: Left input list */
163133 char **pp2 /* IN/OUT: Right input list */
163134){
163135 char *p = *pp;
163136 char *p1 = *pp1;
163137 char *p2 = *pp2;
163138 int iCol1 = 0;
163139 int iCol2 = 0;
163140
163141 /* Never set both isSaveLeft and isExact for the same invocation. */
163142 assert( isSaveLeft==0 || isExact==0 )((void) (0));
163143
163144 assert( p!=0 && *p1!=0 && *p2!=0 )((void) (0));
163145 if( *p1==POS_COLUMN ){
163146 p1++;
163147 p1 += fts3GetVarint32(p1, &iCol1);
163148 }
163149 if( *p2==POS_COLUMN ){
163150 p2++;
163151 p2 += fts3GetVarint32(p2, &iCol2);
163152 }
163153
163154 while( 1 ){
163155 if( iCol1==iCol2 ){
163156 char *pSave = p;
163157 sqlite3_int64 iPrev = 0;
163158 sqlite3_int64 iPos1 = 0;
163159 sqlite3_int64 iPos2 = 0;
163160
163161 if( iCol1 ){
163162 *p++ = POS_COLUMN;
163163 p += sqlite3Fts3PutVarint(p, iCol1);
163164 }
163165
163166 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
163167 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
163168 if( iPos1<0 || iPos2<0 ) break;
163169
163170 while( 1 ){
163171 if( iPos2==iPos1+nToken
163172 || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
163173 ){
163174 sqlite3_int64 iSave;
163175 iSave = isSaveLeft ? iPos1 : iPos2;
163176 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
163177 pSave = 0;
163178 assert( p )((void) (0));
163179 }
163180 if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
163181 if( (*p2&0xFE)==0 ) break;
163182 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
163183 }else{
163184 if( (*p1&0xFE)==0 ) break;
163185 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
163186 }
163187 }
163188
163189 if( pSave ){
163190 assert( pp && p )((void) (0));
163191 p = pSave;
163192 }
163193
163194 fts3ColumnlistCopy(0, &p1);
163195 fts3ColumnlistCopy(0, &p2);
163196 assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 )((void) (0));
163197 if( 0==*p1 || 0==*p2 ) break;
163198
163199 p1++;
163200 p1 += fts3GetVarint32(p1, &iCol1);
163201 p2++;
163202 p2 += fts3GetVarint32(p2, &iCol2);
163203 }
163204
163205 /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
163206 ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
163207 ** end of the position list, or the 0x01 that precedes the next
163208 ** column-number in the position list.
163209 */
163210 else if( iCol1<iCol2 ){
163211 fts3ColumnlistCopy(0, &p1);
163212 if( 0==*p1 ) break;
163213 p1++;
163214 p1 += fts3GetVarint32(p1, &iCol1);
163215 }else{
163216 fts3ColumnlistCopy(0, &p2);
163217 if( 0==*p2 ) break;
163218 p2++;
163219 p2 += fts3GetVarint32(p2, &iCol2);
163220 }
163221 }
163222
163223 fts3PoslistCopy(0, &p2);
163224 fts3PoslistCopy(0, &p1);
163225 *pp1 = p1;
163226 *pp2 = p2;
163227 if( *pp==p ){
163228 return 0;
163229 }
163230 *p++ = 0x00;
163231 *pp = p;
163232 return 1;
163233}
163234
163235/*
163236** Merge two position-lists as required by the NEAR operator. The argument
163237** position lists correspond to the left and right phrases of an expression
163238** like:
163239**
163240** "phrase 1" NEAR "phrase number 2"
163241**
163242** Position list *pp1 corresponds to the left-hand side of the NEAR
163243** expression and *pp2 to the right. As usual, the indexes in the position
163244** lists are the offsets of the last token in each phrase (tokens "1" and "2"
163245** in the example above).
163246**
163247** The output position list - written to *pp - is a copy of *pp2 with those
163248** entries that are not sufficiently NEAR entries in *pp1 removed.
163249*/
163250static int fts3PoslistNearMerge(
163251 char **pp, /* Output buffer */
163252 char *aTmp, /* Temporary buffer space */
163253 int nRight, /* Maximum difference in token positions */
163254 int nLeft, /* Maximum difference in token positions */
163255 char **pp1, /* IN/OUT: Left input list */
163256 char **pp2 /* IN/OUT: Right input list */
163257){
163258 char *p1 = *pp1;
163259 char *p2 = *pp2;
163260
163261 char *pTmp1 = aTmp;
163262 char *pTmp2;
163263 char *aTmp2;
163264 int res = 1;
163265
163266 fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
163267 aTmp2 = pTmp2 = pTmp1;
163268 *pp1 = p1;
163269 *pp2 = p2;
163270 fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
163271 if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
163272 fts3PoslistMerge(pp, &aTmp, &aTmp2);
163273 }else if( pTmp1!=aTmp ){
163274 fts3PoslistCopy(pp, &aTmp);
163275 }else if( pTmp2!=aTmp2 ){
163276 fts3PoslistCopy(pp, &aTmp2);
163277 }else{
163278 res = 0;
163279 }
163280
163281 return res;
163282}
163283
163284/*
163285** An instance of this function is used to merge together the (potentially
163286** large number of) doclists for each term that matches a prefix query.
163287** See function fts3TermSelectMerge() for details.
163288*/
163289typedef struct TermSelect TermSelect;
163290struct TermSelect {
163291 char *aaOutput[16]; /* Malloc'd output buffers */
163292 int anOutput[16]; /* Size each output buffer in bytes */
163293};
163294
163295/*
163296** This function is used to read a single varint from a buffer. Parameter
163297** pEnd points 1 byte past the end of the buffer. When this function is
163298** called, if *pp points to pEnd or greater, then the end of the buffer
163299** has been reached. In this case *pp is set to 0 and the function returns.
163300**
163301** If *pp does not point to or past pEnd, then a single varint is read
163302** from *pp. *pp is then set to point 1 byte past the end of the read varint.
163303**
163304** If bDescIdx is false, the value read is added to *pVal before returning.
163305** If it is true, the value read is subtracted from *pVal before this
163306** function returns.
163307*/
163308static void fts3GetDeltaVarint3(
163309 char **pp, /* IN/OUT: Point to read varint from */
163310 char *pEnd, /* End of buffer */
163311 int bDescIdx, /* True if docids are descending */
163312 sqlite3_int64 *pVal /* IN/OUT: Integer value */
163313){
163314 if( *pp>=pEnd ){
163315 *pp = 0;
163316 }else{
163317 sqlite3_int64 iVal;
163318 *pp += sqlite3Fts3GetVarint(*pp, &iVal);
163319 if( bDescIdx ){
163320 *pVal -= iVal;
163321 }else{
163322 *pVal += iVal;
163323 }
163324 }
163325}
163326
163327/*
163328** This function is used to write a single varint to a buffer. The varint
163329** is written to *pp. Before returning, *pp is set to point 1 byte past the
163330** end of the value written.
163331**
163332** If *pbFirst is zero when this function is called, the value written to
163333** the buffer is that of parameter iVal.
163334**
163335** If *pbFirst is non-zero when this function is called, then the value
163336** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
163337** (if bDescIdx is non-zero).
163338**
163339** Before returning, this function always sets *pbFirst to 1 and *piPrev
163340** to the value of parameter iVal.
163341*/
163342static void fts3PutDeltaVarint3(
163343 char **pp, /* IN/OUT: Output pointer */
163344 int bDescIdx, /* True for descending docids */
163345 sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
163346 int *pbFirst, /* IN/OUT: True after first int written */
163347 sqlite3_int64 iVal /* Write this value to the list */
163348){
163349 sqlite3_int64 iWrite;
163350 if( bDescIdx==0 || *pbFirst==0 ){
163351 iWrite = iVal - *piPrev;
163352 }else{
163353 iWrite = *piPrev - iVal;
163354 }
163355 assert( *pbFirst || *piPrev==0 )((void) (0));
163356 assert( *pbFirst==0 || iWrite>0 )((void) (0));
163357 *pp += sqlite3Fts3PutVarint(*pp, iWrite);
163358 *piPrev = iVal;
163359 *pbFirst = 1;
163360}
163361
163362
163363/*
163364** This macro is used by various functions that merge doclists. The two
163365** arguments are 64-bit docid values. If the value of the stack variable
163366** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
163367** Otherwise, (i2-i1).
163368**
163369** Using this makes it easier to write code that can merge doclists that are
163370** sorted in either ascending or descending order.
163371*/
163372#define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
163373
163374/*
163375** This function does an "OR" merge of two doclists (output contains all
163376** positions contained in either argument doclist). If the docids in the
163377** input doclists are sorted in ascending order, parameter bDescDoclist
163378** should be false. If they are sorted in ascending order, it should be
163379** passed a non-zero value.
163380**
163381** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
163382** containing the output doclist and SQLITE_OK is returned. In this case
163383** *pnOut is set to the number of bytes in the output doclist.
163384**
163385** If an error occurs, an SQLite error code is returned. The output values
163386** are undefined in this case.
163387*/
163388static int fts3DoclistOrMerge(
163389 int bDescDoclist, /* True if arguments are desc */
163390 char *a1, int n1, /* First doclist */
163391 char *a2, int n2, /* Second doclist */
163392 char **paOut, int *pnOut /* OUT: Malloc'd doclist */
163393){
163394 int rc = SQLITE_OK0;
163395 sqlite3_int64 i1 = 0;
163396 sqlite3_int64 i2 = 0;
163397 sqlite3_int64 iPrev = 0;
163398 char *pEnd1 = &a1[n1];
163399 char *pEnd2 = &a2[n2];
163400 char *p1 = a1;
163401 char *p2 = a2;
163402 char *p;
163403 char *aOut;
163404 int bFirstOut = 0;
163405
163406 *paOut = 0;
163407 *pnOut = 0;
163408
163409 /* Allocate space for the output. Both the input and output doclists
163410 ** are delta encoded. If they are in ascending order (bDescDoclist==0),
163411 ** then the first docid in each list is simply encoded as a varint. For
163412 ** each subsequent docid, the varint stored is the difference between the
163413 ** current and previous docid (a positive number - since the list is in
163414 ** ascending order).
163415 **
163416 ** The first docid written to the output is therefore encoded using the
163417 ** same number of bytes as it is in whichever of the input lists it is
163418 ** read from. And each subsequent docid read from the same input list
163419 ** consumes either the same or less bytes as it did in the input (since
163420 ** the difference between it and the previous value in the output must
163421 ** be a positive value less than or equal to the delta value read from
163422 ** the input list). The same argument applies to all but the first docid
163423 ** read from the 'other' list. And to the contents of all position lists
163424 ** that will be copied and merged from the input to the output.
163425 **
163426 ** However, if the first docid copied to the output is a negative number,
163427 ** then the encoding of the first docid from the 'other' input list may
163428 ** be larger in the output than it was in the input (since the delta value
163429 ** may be a larger positive integer than the actual docid).
163430 **
163431 ** The space required to store the output is therefore the sum of the
163432 ** sizes of the two inputs, plus enough space for exactly one of the input
163433 ** docids to grow.
163434 **
163435 ** A symetric argument may be made if the doclists are in descending
163436 ** order.
163437 */
163438 aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING);
163439 if( !aOut ) return SQLITE_NOMEM7;
163440
163441 p = aOut;
163442 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
163443 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
163444 while( p1 || p2 ){
163445 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
163446
163447 if( p2 && p1 && iDiff==0 ){
163448 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
163449 rc = fts3PoslistMerge(&p, &p1, &p2);
163450 if( rc ) break;
163451 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
163452 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
163453 }else if( !p2 || (p1 && iDiff<0) ){
163454 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
163455 fts3PoslistCopy(&p, &p1);
163456 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
163457 }else{
163458 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
163459 fts3PoslistCopy(&p, &p2);
163460 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
163461 }
163462 }
163463
163464 if( rc!=SQLITE_OK0 ){
163465 sqlite3_free(aOut);
163466 p = aOut = 0;
163467 }else{
163468 assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 )((void) (0));
163469 memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING);
163470 }
163471 *paOut = aOut;
163472 *pnOut = (int)(p-aOut);
163473 return rc;
163474}
163475
163476/*
163477** This function does a "phrase" merge of two doclists. In a phrase merge,
163478** the output contains a copy of each position from the right-hand input
163479** doclist for which there is a position in the left-hand input doclist
163480** exactly nDist tokens before it.
163481**
163482** If the docids in the input doclists are sorted in ascending order,
163483** parameter bDescDoclist should be false. If they are sorted in ascending
163484** order, it should be passed a non-zero value.
163485**
163486** The right-hand input doclist is overwritten by this function.
163487*/
163488static int fts3DoclistPhraseMerge(
163489 int bDescDoclist, /* True if arguments are desc */
163490 int nDist, /* Distance from left to right (1=adjacent) */
163491 char *aLeft, int nLeft, /* Left doclist */
163492 char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
163493){
163494 sqlite3_int64 i1 = 0;
163495 sqlite3_int64 i2 = 0;
163496 sqlite3_int64 iPrev = 0;
163497 char *aRight = *paRight;
163498 char *pEnd1 = &aLeft[nLeft];
163499 char *pEnd2 = &aRight[*pnRight];
163500 char *p1 = aLeft;
163501 char *p2 = aRight;
163502 char *p;
163503 int bFirstOut = 0;
163504 char *aOut;
163505
163506 assert( nDist>0 )((void) (0));
163507 if( bDescDoclist ){
163508 aOut = sqlite3_malloc64((sqlite3_int64)*pnRight + FTS3_VARINT_MAX);
163509 if( aOut==0 ) return SQLITE_NOMEM7;
163510 }else{
163511 aOut = aRight;
163512 }
163513 p = aOut;
163514
163515 fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
163516 fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
163517
163518 while( p1 && p2 ){
163519 sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
163520 if( iDiff==0 ){
163521 char *pSave = p;
163522 sqlite3_int64 iPrevSave = iPrev;
163523 int bFirstOutSave = bFirstOut;
163524
163525 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
163526 if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
163527 p = pSave;
163528 iPrev = iPrevSave;
163529 bFirstOut = bFirstOutSave;
163530 }
163531 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
163532 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
163533 }else if( iDiff<0 ){
163534 fts3PoslistCopy(0, &p1);
163535 fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
163536 }else{
163537 fts3PoslistCopy(0, &p2);
163538 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
163539 }
163540 }
163541
163542 *pnRight = (int)(p - aOut);
163543 if( bDescDoclist ){
163544 sqlite3_free(aRight);
163545 *paRight = aOut;
163546 }
163547
163548 return SQLITE_OK0;
163549}
163550
163551/*
163552** Argument pList points to a position list nList bytes in size. This
163553** function checks to see if the position list contains any entries for
163554** a token in position 0 (of any column). If so, it writes argument iDelta
163555** to the output buffer pOut, followed by a position list consisting only
163556** of the entries from pList at position 0, and terminated by an 0x00 byte.
163557** The value returned is the number of bytes written to pOut (if any).
163558*/
163559SQLITE_PRIVATEstatic int sqlite3Fts3FirstFilter(
163560 sqlite3_int64 iDelta, /* Varint that may be written to pOut */
163561 char *pList, /* Position list (no 0x00 term) */
163562 int nList, /* Size of pList in bytes */
163563 char *pOut /* Write output here */
163564){
163565 int nOut = 0;
163566 int bWritten = 0; /* True once iDelta has been written */
163567 char *p = pList;
163568 char *pEnd = &pList[nList];
163569
163570 if( *p!=0x01 ){
163571 if( *p==0x02 ){
163572 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
163573 pOut[nOut++] = 0x02;
163574 bWritten = 1;
163575 }
163576 fts3ColumnlistCopy(0, &p);
163577 }
163578
163579 while( p<pEnd ){
163580 sqlite3_int64 iCol;
163581 p++;
163582 p += sqlite3Fts3GetVarint(p, &iCol);
163583 if( *p==0x02 ){
163584 if( bWritten==0 ){
163585 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
163586 bWritten = 1;
163587 }
163588 pOut[nOut++] = 0x01;
163589 nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
163590 pOut[nOut++] = 0x02;
163591 }
163592 fts3ColumnlistCopy(0, &p);
163593 }
163594 if( bWritten ){
163595 pOut[nOut++] = 0x00;
163596 }
163597
163598 return nOut;
163599}
163600
163601
163602/*
163603** Merge all doclists in the TermSelect.aaOutput[] array into a single
163604** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
163605** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
163606**
163607** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
163608** the responsibility of the caller to free any doclists left in the
163609** TermSelect.aaOutput[] array.
163610*/
163611static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
163612 char *aOut = 0;
163613 int nOut = 0;
163614 int i;
163615
163616 /* Loop through the doclists in the aaOutput[] array. Merge them all
163617 ** into a single doclist.
163618 */
163619 for(i=0; i<SizeofArray(pTS->aaOutput); i++){
163620 if( pTS->aaOutput[i] ){
163621 if( !aOut ){
163622 aOut = pTS->aaOutput[i];
163623 nOut = pTS->anOutput[i];
163624 pTS->aaOutput[i] = 0;
163625 }else{
163626 int nNew;
163627 char *aNew;
163628
163629 int rc = fts3DoclistOrMerge(p->bDescIdx,
163630 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
163631 );
163632 if( rc!=SQLITE_OK0 ){
163633 sqlite3_free(aOut);
163634 return rc;
163635 }
163636
163637 sqlite3_free(pTS->aaOutput[i]);
163638 sqlite3_free(aOut);
163639 pTS->aaOutput[i] = 0;
163640 aOut = aNew;
163641 nOut = nNew;
163642 }
163643 }
163644 }
163645
163646 pTS->aaOutput[0] = aOut;
163647 pTS->anOutput[0] = nOut;
163648 return SQLITE_OK0;
163649}
163650
163651/*
163652** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
163653** as the first argument. The merge is an "OR" merge (see function
163654** fts3DoclistOrMerge() for details).
163655**
163656** This function is called with the doclist for each term that matches
163657** a queried prefix. It merges all these doclists into one, the doclist
163658** for the specified prefix. Since there can be a very large number of
163659** doclists to merge, the merging is done pair-wise using the TermSelect
163660** object.
163661**
163662** This function returns SQLITE_OK if the merge is successful, or an
163663** SQLite error code (SQLITE_NOMEM) if an error occurs.
163664*/
163665static int fts3TermSelectMerge(
163666 Fts3Table *p, /* FTS table handle */
163667 TermSelect *pTS, /* TermSelect object to merge into */
163668 char *aDoclist, /* Pointer to doclist */
163669 int nDoclist /* Size of aDoclist in bytes */
163670){
163671 if( pTS->aaOutput[0]==0 ){
163672 /* If this is the first term selected, copy the doclist to the output
163673 ** buffer using memcpy().
163674 **
163675 ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
163676 ** allocation. This is so as to ensure that the buffer is big enough
163677 ** to hold the current doclist AND'd with any other doclist. If the
163678 ** doclists are stored in order=ASC order, this padding would not be
163679 ** required (since the size of [doclistA AND doclistB] is always less
163680 ** than or equal to the size of [doclistA] in that case). But this is
163681 ** not true for order=DESC. For example, a doclist containing (1, -1)
163682 ** may be smaller than (-1), as in the first example the -1 may be stored
163683 ** as a single-byte delta, whereas in the second it must be stored as a
163684 ** FTS3_VARINT_MAX byte varint.
163685 **
163686 ** Similar padding is added in the fts3DoclistOrMerge() function.
163687 */
163688 pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
163689 pTS->anOutput[0] = nDoclist;
163690 if( pTS->aaOutput[0] ){
163691 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
163692 memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX);
163693 }else{
163694 return SQLITE_NOMEM7;
163695 }
163696 }else{
163697 char *aMerge = aDoclist;
163698 int nMerge = nDoclist;
163699 int iOut;
163700
163701 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
163702 if( pTS->aaOutput[iOut]==0 ){
163703 assert( iOut>0 )((void) (0));
163704 pTS->aaOutput[iOut] = aMerge;
163705 pTS->anOutput[iOut] = nMerge;
163706 break;
163707 }else{
163708 char *aNew;
163709 int nNew;
163710
163711 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
163712 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
163713 );
163714 if( rc!=SQLITE_OK0 ){
163715 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
163716 return rc;
163717 }
163718
163719 if( aMerge!=aDoclist ) sqlite3_free(aMerge);
163720 sqlite3_free(pTS->aaOutput[iOut]);
163721 pTS->aaOutput[iOut] = 0;
163722
163723 aMerge = aNew;
163724 nMerge = nNew;
163725 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
163726 pTS->aaOutput[iOut] = aMerge;
163727 pTS->anOutput[iOut] = nMerge;
163728 }
163729 }
163730 }
163731 }
163732 return SQLITE_OK0;
163733}
163734
163735/*
163736** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
163737*/
163738static int fts3SegReaderCursorAppend(
163739 Fts3MultiSegReader *pCsr,
163740 Fts3SegReader *pNew
163741){
163742 if( (pCsr->nSegment%16)==0 ){
163743 Fts3SegReader **apNew;
163744 sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
163745 apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte);
163746 if( !apNew ){
163747 sqlite3Fts3SegReaderFree(pNew);
163748 return SQLITE_NOMEM7;
163749 }
163750 pCsr->apSegment = apNew;
163751 }
163752 pCsr->apSegment[pCsr->nSegment++] = pNew;
163753 return SQLITE_OK0;
163754}
163755
163756/*
163757** Add seg-reader objects to the Fts3MultiSegReader object passed as the
163758** 8th argument.
163759**
163760** This function returns SQLITE_OK if successful, or an SQLite error code
163761** otherwise.
163762*/
163763static int fts3SegReaderCursor(
163764 Fts3Table *p, /* FTS3 table handle */
163765 int iLangid, /* Language id */
163766 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
163767 int iLevel, /* Level of segments to scan */
163768 const char *zTerm, /* Term to query for */
163769 int nTerm, /* Size of zTerm in bytes */
163770 int isPrefix, /* True for a prefix search */
163771 int isScan, /* True to scan from zTerm to EOF */
163772 Fts3MultiSegReader *pCsr /* Cursor object to populate */
163773){
163774 int rc = SQLITE_OK0; /* Error code */
163775 sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
163776 int rc2; /* Result of sqlite3_reset() */
163777
163778 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
163779 ** for the pending-terms. If this is a scan, then this call must be being
163780 ** made by an fts4aux module, not an FTS table. In this case calling
163781 ** Fts3SegReaderPending might segfault, as the data structures used by
163782 ** fts4aux are not completely populated. So it's easiest to filter these
163783 ** calls out here. */
163784 if( iLevel<0 && p->aIndex ){
163785 Fts3SegReader *pSeg = 0;
163786 rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
163787 if( rc==SQLITE_OK0 && pSeg ){
163788 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
163789 }
163790 }
163791
163792 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
163793 if( rc==SQLITE_OK0 ){
163794 rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
163795 }
163796
163797 while( rc==SQLITE_OK0 && SQLITE_ROW100==(rc = sqlite3_step(pStmt)) ){
163798 Fts3SegReader *pSeg = 0;
163799
163800 /* Read the values returned by the SELECT into local variables. */
163801 sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
163802 sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
163803 sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
163804 int nRoot = sqlite3_column_bytes(pStmt, 4);
163805 char const *zRoot = sqlite3_column_blob(pStmt, 4);
163806
163807 /* If zTerm is not NULL, and this segment is not stored entirely on its
163808 ** root node, the range of leaves scanned can be reduced. Do this. */
163809 if( iStartBlock && zTerm && zRoot ){
163810 sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
163811 rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
163812 if( rc!=SQLITE_OK0 ) goto finished;
163813 if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
163814 }
163815
163816 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
163817 (isPrefix==0 && isScan==0),
163818 iStartBlock, iLeavesEndBlock,
163819 iEndBlock, zRoot, nRoot, &pSeg
163820 );
163821 if( rc!=SQLITE_OK0 ) goto finished;
163822 rc = fts3SegReaderCursorAppend(pCsr, pSeg);
163823 }
163824 }
163825
163826 finished:
163827 rc2 = sqlite3_reset(pStmt);
163828 if( rc==SQLITE_DONE101 ) rc = rc2;
163829
163830 return rc;
163831}
163832
163833/*
163834** Set up a cursor object for iterating through a full-text index or a
163835** single level therein.
163836*/
163837SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderCursor(
163838 Fts3Table *p, /* FTS3 table handle */
163839 int iLangid, /* Language-id to search */
163840 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
163841 int iLevel, /* Level of segments to scan */
163842 const char *zTerm, /* Term to query for */
163843 int nTerm, /* Size of zTerm in bytes */
163844 int isPrefix, /* True for a prefix search */
163845 int isScan, /* True to scan from zTerm to EOF */
163846 Fts3MultiSegReader *pCsr /* Cursor object to populate */
163847){
163848 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
163849 assert( iLevel==FTS3_SEGCURSOR_ALL((void) (0))
163850 || iLevel==FTS3_SEGCURSOR_PENDING((void) (0))
163851 || iLevel>=0((void) (0))
163852 )((void) (0));
163853 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
163854 assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 )((void) (0));
163855 assert( isPrefix==0 || isScan==0 )((void) (0));
163856
163857 memset(pCsr, 0, sizeof(Fts3MultiSegReader));
163858 return fts3SegReaderCursor(
163859 p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
163860 );
163861}
163862
163863/*
163864** In addition to its current configuration, have the Fts3MultiSegReader
163865** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
163866**
163867** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
163868*/
163869static int fts3SegReaderCursorAddZero(
163870 Fts3Table *p, /* FTS virtual table handle */
163871 int iLangid,
163872 const char *zTerm, /* Term to scan doclist of */
163873 int nTerm, /* Number of bytes in zTerm */
163874 Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
163875){
163876 return fts3SegReaderCursor(p,
163877 iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
163878 );
163879}
163880
163881/*
163882** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
163883** if isPrefix is true, to scan the doclist for all terms for which
163884** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
163885** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
163886** an SQLite error code.
163887**
163888** It is the responsibility of the caller to free this object by eventually
163889** passing it to fts3SegReaderCursorFree()
163890**
163891** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
163892** Output parameter *ppSegcsr is set to 0 if an error occurs.
163893*/
163894static int fts3TermSegReaderCursor(
163895 Fts3Cursor *pCsr, /* Virtual table cursor handle */
163896 const char *zTerm, /* Term to query for */
163897 int nTerm, /* Size of zTerm in bytes */
163898 int isPrefix, /* True for a prefix search */
163899 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
163900){
163901 Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
163902 int rc = SQLITE_NOMEM7; /* Return code */
163903
163904 pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
163905 if( pSegcsr ){
163906 int i;
163907 int bFound = 0; /* True once an index has been found */
163908 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
163909
163910 if( isPrefix ){
163911 for(i=1; bFound==0 && i<p->nIndex; i++){
163912 if( p->aIndex[i].nPrefix==nTerm ){
163913 bFound = 1;
163914 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
163915 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
163916 );
163917 pSegcsr->bLookup = 1;
163918 }
163919 }
163920
163921 for(i=1; bFound==0 && i<p->nIndex; i++){
163922 if( p->aIndex[i].nPrefix==nTerm+1 ){
163923 bFound = 1;
163924 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
163925 i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
163926 );
163927 if( rc==SQLITE_OK0 ){
163928 rc = fts3SegReaderCursorAddZero(
163929 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
163930 );
163931 }
163932 }
163933 }
163934 }
163935
163936 if( bFound==0 ){
163937 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
163938 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
163939 );
163940 pSegcsr->bLookup = !isPrefix;
163941 }
163942 }
163943
163944 *ppSegcsr = pSegcsr;
163945 return rc;
163946}
163947
163948/*
163949** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
163950*/
163951static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
163952 sqlite3Fts3SegReaderFinish(pSegcsr);
163953 sqlite3_free(pSegcsr);
163954}
163955
163956/*
163957** This function retrieves the doclist for the specified term (or term
163958** prefix) from the database.
163959*/
163960static int fts3TermSelect(
163961 Fts3Table *p, /* Virtual table handle */
163962 Fts3PhraseToken *pTok, /* Token to query for */
163963 int iColumn, /* Column to query (or -ve for all columns) */
163964 int *pnOut, /* OUT: Size of buffer at *ppOut */
163965 char **ppOut /* OUT: Malloced result buffer */
163966){
163967 int rc; /* Return code */
163968 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
163969 TermSelect tsc; /* Object for pair-wise doclist merging */
163970 Fts3SegFilter filter; /* Segment term filter configuration */
163971
163972 pSegcsr = pTok->pSegcsr;
163973 memset(&tsc, 0, sizeof(TermSelect));
163974
163975 filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
163976 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
163977 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
163978 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
163979 filter.iCol = iColumn;
163980 filter.zTerm = pTok->z;
163981 filter.nTerm = pTok->n;
163982
163983 rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
163984 while( SQLITE_OK0==rc
163985 && SQLITE_ROW100==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
163986 ){
163987 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
163988 }
163989
163990 if( rc==SQLITE_OK0 ){
163991 rc = fts3TermSelectFinishMerge(p, &tsc);
163992 }
163993 if( rc==SQLITE_OK0 ){
163994 *ppOut = tsc.aaOutput[0];
163995 *pnOut = tsc.anOutput[0];
163996 }else{
163997 int i;
163998 for(i=0; i<SizeofArray(tsc.aaOutput); i++){
163999 sqlite3_free(tsc.aaOutput[i]);
164000 }
164001 }
164002
164003 fts3SegReaderCursorFree(pSegcsr);
164004 pTok->pSegcsr = 0;
164005 return rc;
164006}
164007
164008/*
164009** This function counts the total number of docids in the doclist stored
164010** in buffer aList[], size nList bytes.
164011**
164012** If the isPoslist argument is true, then it is assumed that the doclist
164013** contains a position-list following each docid. Otherwise, it is assumed
164014** that the doclist is simply a list of docids stored as delta encoded
164015** varints.
164016*/
164017static int fts3DoclistCountDocids(char *aList, int nList){
164018 int nDoc = 0; /* Return value */
164019 if( aList ){
164020 char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
164021 char *p = aList; /* Cursor */
164022 while( p<aEnd ){
164023 nDoc++;
164024 while( (*p++)&0x80 ); /* Skip docid varint */
164025 fts3PoslistCopy(0, &p); /* Skip over position list */
164026 }
164027 }
164028
164029 return nDoc;
164030}
164031
164032/*
164033** Advance the cursor to the next row in the %_content table that
164034** matches the search criteria. For a MATCH search, this will be
164035** the next row that matches. For a full-table scan, this will be
164036** simply the next row in the %_content table. For a docid lookup,
164037** this routine simply sets the EOF flag.
164038**
164039** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
164040** even if we reach end-of-file. The fts3EofMethod() will be called
164041** subsequently to determine whether or not an EOF was hit.
164042*/
164043static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
164044 int rc;
164045 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
164046 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
164047 if( SQLITE_ROW100!=sqlite3_step(pCsr->pStmt) ){
164048 pCsr->isEof = 1;
164049 rc = sqlite3_reset(pCsr->pStmt);
164050 }else{
164051 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
164052 rc = SQLITE_OK0;
164053 }
164054 }else{
164055 rc = fts3EvalNext((Fts3Cursor *)pCursor);
164056 }
164057 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
164058 return rc;
164059}
164060
164061/*
164062** The following are copied from sqliteInt.h.
164063**
164064** Constants for the largest and smallest possible 64-bit signed integers.
164065** These macros are designed to work correctly on both 32-bit and 64-bit
164066** compilers.
164067*/
164068#ifndef SQLITE_AMALGAMATION1
164069# define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
164070# define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((sqlite3_int64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
164071#endif
164072
164073/*
164074** If the numeric type of argument pVal is "integer", then return it
164075** converted to a 64-bit signed integer. Otherwise, return a copy of
164076** the second parameter, iDefault.
164077*/
164078static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
164079 if( pVal ){
164080 int eType = sqlite3_value_numeric_type(pVal);
164081 if( eType==SQLITE_INTEGER1 ){
164082 return sqlite3_value_int64(pVal);
164083 }
164084 }
164085 return iDefault;
164086}
164087
164088/*
164089** This is the xFilter interface for the virtual table. See
164090** the virtual table xFilter method documentation for additional
164091** information.
164092**
164093** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
164094** the %_content table.
164095**
164096** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
164097** in the %_content table.
164098**
164099** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
164100** column on the left-hand side of the MATCH operator is column
164101** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
164102** side of the MATCH operator.
164103*/
164104static int fts3FilterMethod(
164105 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
164106 int idxNum, /* Strategy index */
164107 const char *idxStr, /* Unused */
164108 int nVal, /* Number of elements in apVal */
164109 sqlite3_value **apVal /* Arguments for the indexing scheme */
164110){
164111 int rc = SQLITE_OK0;
164112 char *zSql; /* SQL statement used to access %_content */
164113 int eSearch;
164114 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
164115 Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
164116
164117 sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
164118 sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
164119 sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
164120 sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
164121 int iIdx;
164122
164123 UNUSED_PARAMETER(idxStr)(void)(idxStr);
164124 UNUSED_PARAMETER(nVal)(void)(nVal);
164125
164126 eSearch = (idxNum & 0x0000FFFF);
164127 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) )((void) (0));
164128 assert( p->pSegments==0 )((void) (0));
164129
164130 /* Collect arguments into local variables */
164131 iIdx = 0;
164132 if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
164133 if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
164134 if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
164135 if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
164136 assert( iIdx==nVal )((void) (0));
164137
164138 /* In case the cursor has been used before, clear it now. */
164139 fts3ClearCursor(pCsr);
164140
164141 /* Set the lower and upper bounds on docids to return */
164142 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
164143 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
164144
164145 if( idxStr ){
164146 pCsr->bDesc = (idxStr[0]=='D');
164147 }else{
164148 pCsr->bDesc = p->bDescIdx;
164149 }
164150 pCsr->eSearch = (i16)eSearch;
164151
164152 if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
164153 int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
164154 const char *zQuery = (const char *)sqlite3_value_text(pCons);
164155
164156 if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL5 ){
164157 return SQLITE_NOMEM7;
164158 }
164159
164160 pCsr->iLangid = 0;
164161 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
164162
164163 assert( p->base.zErrMsg==0 )((void) (0));
164164 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
164165 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
164166 &p->base.zErrMsg
164167 );
164168 if( rc!=SQLITE_OK0 ){
164169 return rc;
164170 }
164171
164172 rc = fts3EvalStart(pCsr);
164173 sqlite3Fts3SegmentsClose(p);
164174 if( rc!=SQLITE_OK0 ) return rc;
164175 pCsr->pNextId = pCsr->aDoclist;
164176 pCsr->iPrevId = 0;
164177 }
164178
164179 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
164180 ** statement loops through all rows of the %_content table. For a
164181 ** full-text query or docid lookup, the statement retrieves a single
164182 ** row by docid.
164183 */
164184 if( eSearch==FTS3_FULLSCAN_SEARCH ){
164185 if( pDocidGe || pDocidLe ){
164186 zSql = sqlite3_mprintf(
164187 "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
164188 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
164189 (pCsr->bDesc ? "DESC" : "ASC")
164190 );
164191 }else{
164192 zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
164193 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
164194 );
164195 }
164196 if( zSql ){
164197 rc = sqlite3_prepare_v3(p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT0x01,&pCsr->pStmt,0);
164198 sqlite3_free(zSql);
164199 }else{
164200 rc = SQLITE_NOMEM7;
164201 }
164202 }else if( eSearch==FTS3_DOCID_SEARCH ){
164203 rc = fts3CursorSeekStmt(pCsr);
164204 if( rc==SQLITE_OK0 ){
164205 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
164206 }
164207 }
164208 if( rc!=SQLITE_OK0 ) return rc;
164209
164210 return fts3NextMethod(pCursor);
164211}
164212
164213/*
164214** This is the xEof method of the virtual table. SQLite calls this
164215** routine to find out if it has reached the end of a result set.
164216*/
164217static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
164218 Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
164219 if( pCsr->isEof ){
164220 fts3ClearCursor(pCsr);
164221 pCsr->isEof = 1;
164222 }
164223 return pCsr->isEof;
164224}
164225
164226/*
164227** This is the xRowid method. The SQLite core calls this routine to
164228** retrieve the rowid for the current row of the result set. fts3
164229** exposes %_content.docid as the rowid for the virtual table. The
164230** rowid should be written to *pRowid.
164231*/
164232static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
164233 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
164234 *pRowid = pCsr->iPrevId;
164235 return SQLITE_OK0;
164236}
164237
164238/*
164239** This is the xColumn method, called by SQLite to request a value from
164240** the row that the supplied cursor currently points to.
164241**
164242** If:
164243**
164244** (iCol < p->nColumn) -> The value of the iCol'th user column.
164245** (iCol == p->nColumn) -> Magic column with the same name as the table.
164246** (iCol == p->nColumn+1) -> Docid column
164247** (iCol == p->nColumn+2) -> Langid column
164248*/
164249static int fts3ColumnMethod(
164250 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
164251 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
164252 int iCol /* Index of column to read value from */
164253){
164254 int rc = SQLITE_OK0; /* Return Code */
164255 Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
164256 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
164257
164258 /* The column value supplied by SQLite must be in range. */
164259 assert( iCol>=0 && iCol<=p->nColumn+2 )((void) (0));
164260
164261 switch( iCol-p->nColumn ){
164262 case 0:
164263 /* The special 'table-name' column */
164264 sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
164265 break;
164266
164267 case 1:
164268 /* The docid column */
164269 sqlite3_result_int64(pCtx, pCsr->iPrevId);
164270 break;
164271
164272 case 2:
164273 if( pCsr->pExpr ){
164274 sqlite3_result_int64(pCtx, pCsr->iLangid);
164275 break;
164276 }else if( p->zLanguageid==0 ){
164277 sqlite3_result_int(pCtx, 0);
164278 break;
164279 }else{
164280 iCol = p->nColumn;
164281 /* fall-through */
164282 }
164283
164284 default:
164285 /* A user column. Or, if this is a full-table scan, possibly the
164286 ** language-id column. Seek the cursor. */
164287 rc = fts3CursorSeek(0, pCsr);
164288 if( rc==SQLITE_OK0 && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
164289 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
164290 }
164291 break;
164292 }
164293
164294 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 )((void) (0));
164295 return rc;
164296}
164297
164298/*
164299** This function is the implementation of the xUpdate callback used by
164300** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
164301** inserted, updated or deleted.
164302*/
164303static int fts3UpdateMethod(
164304 sqlite3_vtab *pVtab, /* Virtual table handle */
164305 int nArg, /* Size of argument array */
164306 sqlite3_value **apVal, /* Array of arguments */
164307 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
164308){
164309 return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
164310}
164311
164312/*
164313** Implementation of xSync() method. Flush the contents of the pending-terms
164314** hash-table to the database.
164315*/
164316static int fts3SyncMethod(sqlite3_vtab *pVtab){
164317
164318 /* Following an incremental-merge operation, assuming that the input
164319 ** segments are not completely consumed (the usual case), they are updated
164320 ** in place to remove the entries that have already been merged. This
164321 ** involves updating the leaf block that contains the smallest unmerged
164322 ** entry and each block (if any) between the leaf and the root node. So
164323 ** if the height of the input segment b-trees is N, and input segments
164324 ** are merged eight at a time, updating the input segments at the end
164325 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
164326 ** small - often between 0 and 2. So the overhead of the incremental
164327 ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
164328 ** dwarfing the actual productive work accomplished, the incremental merge
164329 ** is only attempted if it will write at least 64 leaf blocks. Hence
164330 ** nMinMerge.
164331 **
164332 ** Of course, updating the input segments also involves deleting a bunch
164333 ** of blocks from the segments table. But this is not considered overhead
164334 ** as it would also be required by a crisis-merge that used the same input
164335 ** segments.
164336 */
164337 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
164338
164339 Fts3Table *p = (Fts3Table*)pVtab;
164340 int rc;
164341 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
164342
164343 rc = sqlite3Fts3PendingTermsFlush(p);
164344 if( rc==SQLITE_OK0
164345 && p->nLeafAdd>(nMinMerge/16)
164346 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
164347 ){
164348 int mxLevel = 0; /* Maximum relative level value in db */
164349 int A; /* Incr-merge parameter A */
164350
164351 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
164352 assert( rc==SQLITE_OK || mxLevel==0 )((void) (0));
164353 A = p->nLeafAdd * mxLevel;
164354 A += (A/2);
164355 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
164356 }
164357 sqlite3Fts3SegmentsClose(p);
164358 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
164359 return rc;
164360}
164361
164362/*
164363** If it is currently unknown whether or not the FTS table has an %_stat
164364** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
164365** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
164366** if an error occurs.
164367*/
164368static int fts3SetHasStat(Fts3Table *p){
164369 int rc = SQLITE_OK0;
164370 if( p->bHasStat==2 ){
164371 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
164372 if( zTbl ){
164373 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
164374 sqlite3_free(zTbl);
164375 p->bHasStat = (res==SQLITE_OK0);
164376 }else{
164377 rc = SQLITE_NOMEM7;
164378 }
164379 }
164380 return rc;
164381}
164382
164383/*
164384** Implementation of xBegin() method.
164385*/
164386static int fts3BeginMethod(sqlite3_vtab *pVtab){
164387 Fts3Table *p = (Fts3Table*)pVtab;
164388 UNUSED_PARAMETER(pVtab)(void)(pVtab);
164389 assert( p->pSegments==0 )((void) (0));
164390 assert( p->nPendingData==0 )((void) (0));
164391 assert( p->inTransaction!=1 )((void) (0));
164392 TESTONLY( p->inTransaction = 1 );
164393 TESTONLY( p->mxSavepoint = -1; );
164394 p->nLeafAdd = 0;
164395 return fts3SetHasStat(p);
164396}
164397
164398/*
164399** Implementation of xCommit() method. This is a no-op. The contents of
164400** the pending-terms hash-table have already been flushed into the database
164401** by fts3SyncMethod().
164402*/
164403static int fts3CommitMethod(sqlite3_vtab *pVtab){
164404 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
164405 UNUSED_PARAMETER(pVtab)(void)(pVtab);
164406 assert( p->nPendingData==0 )((void) (0));
164407 assert( p->inTransaction!=0 )((void) (0));
164408 assert( p->pSegments==0 )((void) (0));
164409 TESTONLY( p->inTransaction = 0 );
164410 TESTONLY( p->mxSavepoint = -1; );
164411 return SQLITE_OK0;
164412}
164413
164414/*
164415** Implementation of xRollback(). Discard the contents of the pending-terms
164416** hash-table. Any changes made to the database are reverted by SQLite.
164417*/
164418static int fts3RollbackMethod(sqlite3_vtab *pVtab){
164419 Fts3Table *p = (Fts3Table*)pVtab;
164420 sqlite3Fts3PendingTermsClear(p);
164421 assert( p->inTransaction!=0 )((void) (0));
164422 TESTONLY( p->inTransaction = 0 );
164423 TESTONLY( p->mxSavepoint = -1; );
164424 return SQLITE_OK0;
164425}
164426
164427/*
164428** When called, *ppPoslist must point to the byte immediately following the
164429** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
164430** moves *ppPoslist so that it instead points to the first byte of the
164431** same position list.
164432*/
164433static void fts3ReversePoslist(char *pStart, char **ppPoslist){
164434 char *p = &(*ppPoslist)[-2];
164435 char c = 0;
164436
164437 /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
164438 while( p>pStart && (c=*p--)==0 );
164439
164440 /* Search backwards for a varint with value zero (the end of the previous
164441 ** poslist). This is an 0x00 byte preceded by some byte that does not
164442 ** have the 0x80 bit set. */
164443 while( p>pStart && (*p & 0x80) | c ){
164444 c = *p--;
164445 }
164446 assert( p==pStart || c==0 )((void) (0));
164447
164448 /* At this point p points to that preceding byte without the 0x80 bit
164449 ** set. So to find the start of the poslist, skip forward 2 bytes then
164450 ** over a varint.
164451 **
164452 ** Normally. The other case is that p==pStart and the poslist to return
164453 ** is the first in the doclist. In this case do not skip forward 2 bytes.
164454 ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
164455 ** is required for cases where the first byte of a doclist and the
164456 ** doclist is empty. For example, if the first docid is 10, a doclist
164457 ** that begins with:
164458 **
164459 ** 0x0A 0x00 <next docid delta varint>
164460 */
164461 if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
164462 while( *p++&0x80 );
164463 *ppPoslist = p;
164464}
164465
164466/*
164467** Helper function used by the implementation of the overloaded snippet(),
164468** offsets() and optimize() SQL functions.
164469**
164470** If the value passed as the third argument is a blob of size
164471** sizeof(Fts3Cursor*), then the blob contents are copied to the
164472** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
164473** message is written to context pContext and SQLITE_ERROR returned. The
164474** string passed via zFunc is used as part of the error message.
164475*/
164476static int fts3FunctionArg(
164477 sqlite3_context *pContext, /* SQL function call context */
164478 const char *zFunc, /* Function name */
164479 sqlite3_value *pVal, /* argv[0] passed to function */
164480 Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
164481){
164482 int rc;
164483 *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
164484 if( (*ppCsr)!=0 ){
164485 rc = SQLITE_OK0;
164486 }else{
164487 char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
164488 sqlite3_result_error(pContext, zErr, -1);
164489 sqlite3_free(zErr);
164490 rc = SQLITE_ERROR1;
164491 }
164492 return rc;
164493}
164494
164495/*
164496** Implementation of the snippet() function for FTS3
164497*/
164498static void fts3SnippetFunc(
164499 sqlite3_context *pContext, /* SQLite function call context */
164500 int nVal, /* Size of apVal[] array */
164501 sqlite3_value **apVal /* Array of arguments */
164502){
164503 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
164504 const char *zStart = "<b>";
164505 const char *zEnd = "</b>";
164506 const char *zEllipsis = "<b>...</b>";
164507 int iCol = -1;
164508 int nToken = 15; /* Default number of tokens in snippet */
164509
164510 /* There must be at least one argument passed to this function (otherwise
164511 ** the non-overloaded version would have been called instead of this one).
164512 */
164513 assert( nVal>=1 )((void) (0));
164514
164515 if( nVal>6 ){
164516 sqlite3_result_error(pContext,
164517 "wrong number of arguments to function snippet()", -1);
164518 return;
164519 }
164520 if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
164521
164522 switch( nVal ){
164523 case 6: nToken = sqlite3_value_int(apVal[5]);
164524 case 5: iCol = sqlite3_value_int(apVal[4]);
164525 case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
164526 case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
164527 case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
164528 }
164529 if( !zEllipsis || !zEnd || !zStart ){
164530 sqlite3_result_error_nomem(pContext);
164531 }else if( nToken==0 ){
164532 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
164533 }else if( SQLITE_OK0==fts3CursorSeek(pContext, pCsr) ){
164534 sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
164535 }
164536}
164537
164538/*
164539** Implementation of the offsets() function for FTS3
164540*/
164541static void fts3OffsetsFunc(
164542 sqlite3_context *pContext, /* SQLite function call context */
164543 int nVal, /* Size of argument array */
164544 sqlite3_value **apVal /* Array of arguments */
164545){
164546 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
164547
164548 UNUSED_PARAMETER(nVal)(void)(nVal);
164549
164550 assert( nVal==1 )((void) (0));
164551 if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
164552 assert( pCsr )((void) (0));
164553 if( SQLITE_OK0==fts3CursorSeek(pContext, pCsr) ){
164554 sqlite3Fts3Offsets(pContext, pCsr);
164555 }
164556}
164557
164558/*
164559** Implementation of the special optimize() function for FTS3. This
164560** function merges all segments in the database to a single segment.
164561** Example usage is:
164562**
164563** SELECT optimize(t) FROM t LIMIT 1;
164564**
164565** where 't' is the name of an FTS3 table.
164566*/
164567static void fts3OptimizeFunc(
164568 sqlite3_context *pContext, /* SQLite function call context */
164569 int nVal, /* Size of argument array */
164570 sqlite3_value **apVal /* Array of arguments */
164571){
164572 int rc; /* Return code */
164573 Fts3Table *p; /* Virtual table handle */
164574 Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
164575
164576 UNUSED_PARAMETER(nVal)(void)(nVal);
164577
164578 assert( nVal==1 )((void) (0));
164579 if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
164580 p = (Fts3Table *)pCursor->base.pVtab;
164581 assert( p )((void) (0));
164582
164583 rc = sqlite3Fts3Optimize(p);
164584
164585 switch( rc ){
164586 case SQLITE_OK0:
164587 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
164588 break;
164589 case SQLITE_DONE101:
164590 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
164591 break;
164592 default:
164593 sqlite3_result_error_code(pContext, rc);
164594 break;
164595 }
164596}
164597
164598/*
164599** Implementation of the matchinfo() function for FTS3
164600*/
164601static void fts3MatchinfoFunc(
164602 sqlite3_context *pContext, /* SQLite function call context */
164603 int nVal, /* Size of argument array */
164604 sqlite3_value **apVal /* Array of arguments */
164605){
164606 Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
164607 assert( nVal==1 || nVal==2 )((void) (0));
164608 if( SQLITE_OK0==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
164609 const char *zArg = 0;
164610 if( nVal>1 ){
164611 zArg = (const char *)sqlite3_value_text(apVal[1]);
164612 }
164613 sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
164614 }
164615}
164616
164617/*
164618** This routine implements the xFindFunction method for the FTS3
164619** virtual table.
164620*/
164621static int fts3FindFunctionMethod(
164622 sqlite3_vtab *pVtab, /* Virtual table handle */
164623 int nArg, /* Number of SQL function arguments */
164624 const char *zName, /* Name of SQL function */
164625 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
164626 void **ppArg /* Unused */
164627){
164628 struct Overloaded {
164629 const char *zName;
164630 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
164631 } aOverload[] = {
164632 { "snippet", fts3SnippetFunc },
164633 { "offsets", fts3OffsetsFunc },
164634 { "optimize", fts3OptimizeFunc },
164635 { "matchinfo", fts3MatchinfoFunc },
164636 };
164637 int i; /* Iterator variable */
164638
164639 UNUSED_PARAMETER(pVtab)(void)(pVtab);
164640 UNUSED_PARAMETER(nArg)(void)(nArg);
164641 UNUSED_PARAMETER(ppArg)(void)(ppArg);
164642
164643 for(i=0; i<SizeofArray(aOverload); i++){
164644 if( strcmp(zName, aOverload[i].zName)==0 ){
164645 *pxFunc = aOverload[i].xFunc;
164646 return 1;
164647 }
164648 }
164649
164650 /* No function of the specified name was found. Return 0. */
164651 return 0;
164652}
164653
164654/*
164655** Implementation of FTS3 xRename method. Rename an fts3 table.
164656*/
164657static int fts3RenameMethod(
164658 sqlite3_vtab *pVtab, /* Virtual table handle */
164659 const char *zName /* New name of table */
164660){
164661 Fts3Table *p = (Fts3Table *)pVtab;
164662 sqlite3 *db = p->db; /* Database connection */
164663 int rc; /* Return Code */
164664
164665 /* At this point it must be known if the %_stat table exists or not.
164666 ** So bHasStat may not be 2. */
164667 rc = fts3SetHasStat(p);
164668
164669 /* As it happens, the pending terms table is always empty here. This is
164670 ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
164671 ** always opens a savepoint transaction. And the xSavepoint() method
164672 ** flushes the pending terms table. But leave the (no-op) call to
164673 ** PendingTermsFlush() in in case that changes.
164674 */
164675 assert( p->nPendingData==0 )((void) (0));
164676 if( rc==SQLITE_OK0 ){
164677 rc = sqlite3Fts3PendingTermsFlush(p);
164678 }
164679
164680 if( p->zContentTbl==0 ){
164681 fts3DbExec(&rc, db,
164682 "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
164683 p->zDb, p->zName, zName
164684 );
164685 }
164686
164687 if( p->bHasDocsize ){
164688 fts3DbExec(&rc, db,
164689 "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
164690 p->zDb, p->zName, zName
164691 );
164692 }
164693 if( p->bHasStat ){
164694 fts3DbExec(&rc, db,
164695 "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
164696 p->zDb, p->zName, zName
164697 );
164698 }
164699 fts3DbExec(&rc, db,
164700 "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
164701 p->zDb, p->zName, zName
164702 );
164703 fts3DbExec(&rc, db,
164704 "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
164705 p->zDb, p->zName, zName
164706 );
164707 return rc;
164708}
164709
164710/*
164711** The xSavepoint() method.
164712**
164713** Flush the contents of the pending-terms table to disk.
164714*/
164715static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
164716 int rc = SQLITE_OK0;
164717 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
164718 assert( ((Fts3Table *)pVtab)->inTransaction )((void) (0));
164719 assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint )((void) (0));
164720 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
164721 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
164722 rc = fts3SyncMethod(pVtab);
164723 }
164724 return rc;
164725}
164726
164727/*
164728** The xRelease() method.
164729**
164730** This is a no-op.
164731*/
164732static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
164733 TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
164734 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
164735 UNUSED_PARAMETER(pVtab)(void)(pVtab);
164736 assert( p->inTransaction )((void) (0));
164737 assert( p->mxSavepoint >= iSavepoint )((void) (0));
164738 TESTONLY( p->mxSavepoint = iSavepoint-1 );
164739 return SQLITE_OK0;
164740}
164741
164742/*
164743** The xRollbackTo() method.
164744**
164745** Discard the contents of the pending terms table.
164746*/
164747static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
164748 Fts3Table *p = (Fts3Table*)pVtab;
164749 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
164750 assert( p->inTransaction )((void) (0));
164751 TESTONLY( p->mxSavepoint = iSavepoint );
164752 sqlite3Fts3PendingTermsClear(p);
164753 return SQLITE_OK0;
164754}
164755
164756/*
164757** Return true if zName is the extension on one of the shadow tables used
164758** by this module.
164759*/
164760static int fts3ShadowName(const char *zName){
164761 static const char *azName[] = {
164762 "content", "docsize", "segdir", "segments", "stat",
164763 };
164764 unsigned int i;
164765 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
164766 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
164767 }
164768 return 0;
164769}
164770
164771static const sqlite3_module fts3Module = {
164772 /* iVersion */ 3,
164773 /* xCreate */ fts3CreateMethod,
164774 /* xConnect */ fts3ConnectMethod,
164775 /* xBestIndex */ fts3BestIndexMethod,
164776 /* xDisconnect */ fts3DisconnectMethod,
164777 /* xDestroy */ fts3DestroyMethod,
164778 /* xOpen */ fts3OpenMethod,
164779 /* xClose */ fts3CloseMethod,
164780 /* xFilter */ fts3FilterMethod,
164781 /* xNext */ fts3NextMethod,
164782 /* xEof */ fts3EofMethod,
164783 /* xColumn */ fts3ColumnMethod,
164784 /* xRowid */ fts3RowidMethod,
164785 /* xUpdate */ fts3UpdateMethod,
164786 /* xBegin */ fts3BeginMethod,
164787 /* xSync */ fts3SyncMethod,
164788 /* xCommit */ fts3CommitMethod,
164789 /* xRollback */ fts3RollbackMethod,
164790 /* xFindFunction */ fts3FindFunctionMethod,
164791 /* xRename */ fts3RenameMethod,
164792 /* xSavepoint */ fts3SavepointMethod,
164793 /* xRelease */ fts3ReleaseMethod,
164794 /* xRollbackTo */ fts3RollbackToMethod,
164795 /* xShadowName */ fts3ShadowName,
164796};
164797
164798/*
164799** This function is registered as the module destructor (called when an
164800** FTS3 enabled database connection is closed). It frees the memory
164801** allocated for the tokenizer hash table.
164802*/
164803static void hashDestroy(void *p){
164804 Fts3Hash *pHash = (Fts3Hash *)p;
164805 sqlite3Fts3HashClear(pHash);
164806 sqlite3_free(pHash);
164807}
164808
164809/*
164810** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
164811** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
164812** respectively. The following three forward declarations are for functions
164813** declared in these files used to retrieve the respective implementations.
164814**
164815** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
164816** to by the argument to point to the "simple" tokenizer implementation.
164817** And so on.
164818*/
164819SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
164820SQLITE_PRIVATEstatic void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
164821#ifndef SQLITE_DISABLE_FTS3_UNICODE
164822SQLITE_PRIVATEstatic void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
164823#endif
164824#ifdef SQLITE_ENABLE_ICU
164825SQLITE_PRIVATEstatic void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
164826#endif
164827
164828/*
164829** Initialize the fts3 extension. If this extension is built as part
164830** of the sqlite library, then this function is called directly by
164831** SQLite. If fts3 is built as a dynamically loadable extension, this
164832** function is called by the sqlite3_extension_init() entry point.
164833*/
164834SQLITE_PRIVATEstatic int sqlite3Fts3Init(sqlite3 *db){
164835 int rc = SQLITE_OK0;
164836 Fts3Hash *pHash = 0;
164837 const sqlite3_tokenizer_module *pSimple = 0;
164838 const sqlite3_tokenizer_module *pPorter = 0;
164839#ifndef SQLITE_DISABLE_FTS3_UNICODE
164840 const sqlite3_tokenizer_module *pUnicode = 0;
164841#endif
164842
164843#ifdef SQLITE_ENABLE_ICU
164844 const sqlite3_tokenizer_module *pIcu = 0;
164845 sqlite3Fts3IcuTokenizerModule(&pIcu);
164846#endif
164847
164848#ifndef SQLITE_DISABLE_FTS3_UNICODE
164849 sqlite3Fts3UnicodeTokenizer(&pUnicode);
164850#endif
164851
164852#ifdef SQLITE_TEST
164853 rc = sqlite3Fts3InitTerm(db);
164854 if( rc!=SQLITE_OK0 ) return rc;
164855#endif
164856
164857 rc = sqlite3Fts3InitAux(db);
164858 if( rc!=SQLITE_OK0 ) return rc;
164859
164860 sqlite3Fts3SimpleTokenizerModule(&pSimple);
164861 sqlite3Fts3PorterTokenizerModule(&pPorter);
164862
164863 /* Allocate and initialize the hash-table used to store tokenizers. */
164864 pHash = sqlite3_malloc(sizeof(Fts3Hash));
164865 if( !pHash ){
164866 rc = SQLITE_NOMEM7;
164867 }else{
164868 sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
164869 }
164870
164871 /* Load the built-in tokenizers into the hash table */
164872 if( rc==SQLITE_OK0 ){
164873 if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
164874 || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
164875
164876#ifndef SQLITE_DISABLE_FTS3_UNICODE
164877 || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
164878#endif
164879#ifdef SQLITE_ENABLE_ICU
164880 || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
164881#endif
164882 ){
164883 rc = SQLITE_NOMEM7;
164884 }
164885 }
164886
164887#ifdef SQLITE_TEST
164888 if( rc==SQLITE_OK0 ){
164889 rc = sqlite3Fts3ExprInitTestInterface(db, pHash);
164890 }
164891#endif
164892
164893 /* Create the virtual table wrapper around the hash-table and overload
164894 ** the four scalar functions. If this is successful, register the
164895 ** module with sqlite.
164896 */
164897 if( SQLITE_OK0==rc
164898 && SQLITE_OK0==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
164899 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "snippet", -1))
164900 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "offsets", 1))
164901 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "matchinfo", 1))
164902 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "matchinfo", 2))
164903 && SQLITE_OK0==(rc = sqlite3_overload_function(db, "optimize", 1))
164904 ){
164905 rc = sqlite3_create_module_v2(
164906 db, "fts3", &fts3Module, (void *)pHash, hashDestroy
164907 );
164908 if( rc==SQLITE_OK0 ){
164909 rc = sqlite3_create_module_v2(
164910 db, "fts4", &fts3Module, (void *)pHash, 0
164911 );
164912 }
164913 if( rc==SQLITE_OK0 ){
164914 rc = sqlite3Fts3InitTok(db, (void *)pHash);
164915 }
164916 return rc;
164917 }
164918
164919
164920 /* An error has occurred. Delete the hash table and return the error code. */
164921 assert( rc!=SQLITE_OK )((void) (0));
164922 if( pHash ){
164923 sqlite3Fts3HashClear(pHash);
164924 sqlite3_free(pHash);
164925 }
164926 return rc;
164927}
164928
164929/*
164930** Allocate an Fts3MultiSegReader for each token in the expression headed
164931** by pExpr.
164932**
164933** An Fts3SegReader object is a cursor that can seek or scan a range of
164934** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
164935** Fts3SegReader objects internally to provide an interface to seek or scan
164936** within the union of all segments of a b-tree. Hence the name.
164937**
164938** If the allocated Fts3MultiSegReader just seeks to a single entry in a
164939** segment b-tree (if the term is not a prefix or it is a prefix for which
164940** there exists prefix b-tree of the right length) then it may be traversed
164941** and merged incrementally. Otherwise, it has to be merged into an in-memory
164942** doclist and then traversed.
164943*/
164944static void fts3EvalAllocateReaders(
164945 Fts3Cursor *pCsr, /* FTS cursor handle */
164946 Fts3Expr *pExpr, /* Allocate readers for this expression */
164947 int *pnToken, /* OUT: Total number of tokens in phrase. */
164948 int *pnOr, /* OUT: Total number of OR nodes in expr. */
164949 int *pRc /* IN/OUT: Error code */
164950){
164951 if( pExpr && SQLITE_OK0==*pRc ){
164952 if( pExpr->eType==FTSQUERY_PHRASE ){
164953 int i;
164954 int nToken = pExpr->pPhrase->nToken;
164955 *pnToken += nToken;
164956 for(i=0; i<nToken; i++){
164957 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
164958 int rc = fts3TermSegReaderCursor(pCsr,
164959 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
164960 );
164961 if( rc!=SQLITE_OK0 ){
164962 *pRc = rc;
164963 return;
164964 }
164965 }
164966 assert( pExpr->pPhrase->iDoclistToken==0 )((void) (0));
164967 pExpr->pPhrase->iDoclistToken = -1;
164968 }else{
164969 *pnOr += (pExpr->eType==FTSQUERY_OR);
164970 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
164971 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
164972 }
164973 }
164974}
164975
164976/*
164977** Arguments pList/nList contain the doclist for token iToken of phrase p.
164978** It is merged into the main doclist stored in p->doclist.aAll/nAll.
164979**
164980** This function assumes that pList points to a buffer allocated using
164981** sqlite3_malloc(). This function takes responsibility for eventually
164982** freeing the buffer.
164983**
164984** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
164985*/
164986static int fts3EvalPhraseMergeToken(
164987 Fts3Table *pTab, /* FTS Table pointer */
164988 Fts3Phrase *p, /* Phrase to merge pList/nList into */
164989 int iToken, /* Token pList/nList corresponds to */
164990 char *pList, /* Pointer to doclist */
164991 int nList /* Number of bytes in pList */
164992){
164993 int rc = SQLITE_OK0;
164994 assert( iToken!=p->iDoclistToken )((void) (0));
164995
164996 if( pList==0 ){
164997 sqlite3_free(p->doclist.aAll);
164998 p->doclist.aAll = 0;
164999 p->doclist.nAll = 0;
165000 }
165001
165002 else if( p->iDoclistToken<0 ){
165003 p->doclist.aAll = pList;
165004 p->doclist.nAll = nList;
165005 }
165006
165007 else if( p->doclist.aAll==0 ){
165008 sqlite3_free(pList);
165009 }
165010
165011 else {
165012 char *pLeft;
165013 char *pRight;
165014 int nLeft;
165015 int nRight;
165016 int nDiff;
165017
165018 if( p->iDoclistToken<iToken ){
165019 pLeft = p->doclist.aAll;
165020 nLeft = p->doclist.nAll;
165021 pRight = pList;
165022 nRight = nList;
165023 nDiff = iToken - p->iDoclistToken;
165024 }else{
165025 pRight = p->doclist.aAll;
165026 nRight = p->doclist.nAll;
165027 pLeft = pList;
165028 nLeft = nList;
165029 nDiff = p->iDoclistToken - iToken;
165030 }
165031
165032 rc = fts3DoclistPhraseMerge(
165033 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
165034 );
165035 sqlite3_free(pLeft);
165036 p->doclist.aAll = pRight;
165037 p->doclist.nAll = nRight;
165038 }
165039
165040 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
165041 return rc;
165042}
165043
165044/*
165045** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
165046** does not take deferred tokens into account.
165047**
165048** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
165049*/
165050static int fts3EvalPhraseLoad(
165051 Fts3Cursor *pCsr, /* FTS Cursor handle */
165052 Fts3Phrase *p /* Phrase object */
165053){
165054 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
165055 int iToken;
165056 int rc = SQLITE_OK0;
165057
165058 for(iToken=0; rc==SQLITE_OK0 && iToken<p->nToken; iToken++){
165059 Fts3PhraseToken *pToken = &p->aToken[iToken];
165060 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 )((void) (0));
165061
165062 if( pToken->pSegcsr ){
165063 int nThis = 0;
165064 char *pThis = 0;
165065 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
165066 if( rc==SQLITE_OK0 ){
165067 rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
165068 }
165069 }
165070 assert( pToken->pSegcsr==0 )((void) (0));
165071 }
165072
165073 return rc;
165074}
165075
165076#ifndef SQLITE_DISABLE_FTS4_DEFERRED
165077/*
165078** This function is called on each phrase after the position lists for
165079** any deferred tokens have been loaded into memory. It updates the phrases
165080** current position list to include only those positions that are really
165081** instances of the phrase (after considering deferred tokens). If this
165082** means that the phrase does not appear in the current row, doclist.pList
165083** and doclist.nList are both zeroed.
165084**
165085** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
165086*/
165087static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
165088 int iToken; /* Used to iterate through phrase tokens */
165089 char *aPoslist = 0; /* Position list for deferred tokens */
165090 int nPoslist = 0; /* Number of bytes in aPoslist */
165091 int iPrev = -1; /* Token number of previous deferred token */
165092
165093 assert( pPhrase->doclist.bFreeList==0 )((void) (0));
165094
165095 for(iToken=0; iToken<pPhrase->nToken; iToken++){
165096 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
165097 Fts3DeferredToken *pDeferred = pToken->pDeferred;
165098
165099 if( pDeferred ){
165100 char *pList;
165101 int nList;
165102 int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
165103 if( rc!=SQLITE_OK0 ) return rc;
165104
165105 if( pList==0 ){
165106 sqlite3_free(aPoslist);
165107 pPhrase->doclist.pList = 0;
165108 pPhrase->doclist.nList = 0;
165109 return SQLITE_OK0;
165110
165111 }else if( aPoslist==0 ){
165112 aPoslist = pList;
165113 nPoslist = nList;
165114
165115 }else{
165116 char *aOut = pList;
165117 char *p1 = aPoslist;
165118 char *p2 = aOut;
165119
165120 assert( iPrev>=0 )((void) (0));
165121 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
165122 sqlite3_free(aPoslist);
165123 aPoslist = pList;
165124 nPoslist = (int)(aOut - aPoslist);
165125 if( nPoslist==0 ){
165126 sqlite3_free(aPoslist);
165127 pPhrase->doclist.pList = 0;
165128 pPhrase->doclist.nList = 0;
165129 return SQLITE_OK0;
165130 }
165131 }
165132 iPrev = iToken;
165133 }
165134 }
165135
165136 if( iPrev>=0 ){
165137 int nMaxUndeferred = pPhrase->iDoclistToken;
165138 if( nMaxUndeferred<0 ){
165139 pPhrase->doclist.pList = aPoslist;
165140 pPhrase->doclist.nList = nPoslist;
165141 pPhrase->doclist.iDocid = pCsr->iPrevId;
165142 pPhrase->doclist.bFreeList = 1;
165143 }else{
165144 int nDistance;
165145 char *p1;
165146 char *p2;
165147 char *aOut;
165148
165149 if( nMaxUndeferred>iPrev ){
165150 p1 = aPoslist;
165151 p2 = pPhrase->doclist.pList;
165152 nDistance = nMaxUndeferred - iPrev;
165153 }else{
165154 p1 = pPhrase->doclist.pList;
165155 p2 = aPoslist;
165156 nDistance = iPrev - nMaxUndeferred;
165157 }
165158
165159 aOut = (char *)sqlite3_malloc(nPoslist+8);
165160 if( !aOut ){
165161 sqlite3_free(aPoslist);
165162 return SQLITE_NOMEM7;
165163 }
165164
165165 pPhrase->doclist.pList = aOut;
165166 if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
165167 pPhrase->doclist.bFreeList = 1;
165168 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
165169 }else{
165170 sqlite3_free(aOut);
165171 pPhrase->doclist.pList = 0;
165172 pPhrase->doclist.nList = 0;
165173 }
165174 sqlite3_free(aPoslist);
165175 }
165176 }
165177
165178 return SQLITE_OK0;
165179}
165180#endif /* SQLITE_DISABLE_FTS4_DEFERRED */
165181
165182/*
165183** Maximum number of tokens a phrase may have to be considered for the
165184** incremental doclists strategy.
165185*/
165186#define MAX_INCR_PHRASE_TOKENS 4
165187
165188/*
165189** This function is called for each Fts3Phrase in a full-text query
165190** expression to initialize the mechanism for returning rows. Once this
165191** function has been called successfully on an Fts3Phrase, it may be
165192** used with fts3EvalPhraseNext() to iterate through the matching docids.
165193**
165194** If parameter bOptOk is true, then the phrase may (or may not) use the
165195** incremental loading strategy. Otherwise, the entire doclist is loaded into
165196** memory within this call.
165197**
165198** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
165199*/
165200static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
165201 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
165202 int rc = SQLITE_OK0; /* Error code */
165203 int i;
165204
165205 /* Determine if doclists may be loaded from disk incrementally. This is
165206 ** possible if the bOptOk argument is true, the FTS doclists will be
165207 ** scanned in forward order, and the phrase consists of
165208 ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
165209 ** tokens or prefix tokens that cannot use a prefix-index. */
165210 int bHaveIncr = 0;
165211 int bIncrOk = (bOptOk
165212 && pCsr->bDesc==pTab->bDescIdx
165213 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
165214#ifdef SQLITE_TEST
165215 && pTab->bNoIncrDoclist==0
165216#endif
165217 );
165218 for(i=0; bIncrOk==1 && i<p->nToken; i++){
165219 Fts3PhraseToken *pToken = &p->aToken[i];
165220 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
165221 bIncrOk = 0;
165222 }
165223 if( pToken->pSegcsr ) bHaveIncr = 1;
165224 }
165225
165226 if( bIncrOk && bHaveIncr ){
165227 /* Use the incremental approach. */
165228 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
165229 for(i=0; rc==SQLITE_OK0 && i<p->nToken; i++){
165230 Fts3PhraseToken *pToken = &p->aToken[i];
165231 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
165232 if( pSegcsr ){
165233 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
165234 }
165235 }
165236 p->bIncr = 1;
165237 }else{
165238 /* Load the full doclist for the phrase into memory. */
165239 rc = fts3EvalPhraseLoad(pCsr, p);
165240 p->bIncr = 0;
165241 }
165242
165243 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr )((void) (0));
165244 return rc;
165245}
165246
165247/*
165248** This function is used to iterate backwards (from the end to start)
165249** through doclists. It is used by this module to iterate through phrase
165250** doclists in reverse and by the fts3_write.c module to iterate through
165251** pending-terms lists when writing to databases with "order=desc".
165252**
165253** The doclist may be sorted in ascending (parameter bDescIdx==0) or
165254** descending (parameter bDescIdx==1) order of docid. Regardless, this
165255** function iterates from the end of the doclist to the beginning.
165256*/
165257SQLITE_PRIVATEstatic void sqlite3Fts3DoclistPrev(
165258 int bDescIdx, /* True if the doclist is desc */
165259 char *aDoclist, /* Pointer to entire doclist */
165260 int nDoclist, /* Length of aDoclist in bytes */
165261 char **ppIter, /* IN/OUT: Iterator pointer */
165262 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
165263 int *pnList, /* OUT: List length pointer */
165264 u8 *pbEof /* OUT: End-of-file flag */
165265){
165266 char *p = *ppIter;
165267
165268 assert( nDoclist>0 )((void) (0));
165269 assert( *pbEof==0 )((void) (0));
165270 assert( p || *piDocid==0 )((void) (0));
165271 assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) )((void) (0));
165272
165273 if( p==0 ){
165274 sqlite3_int64 iDocid = 0;
165275 char *pNext = 0;
165276 char *pDocid = aDoclist;
165277 char *pEnd = &aDoclist[nDoclist];
165278 int iMul = 1;
165279
165280 while( pDocid<pEnd ){
165281 sqlite3_int64 iDelta;
165282 pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
165283 iDocid += (iMul * iDelta);
165284 pNext = pDocid;
165285 fts3PoslistCopy(0, &pDocid);
165286 while( pDocid<pEnd && *pDocid==0 ) pDocid++;
165287 iMul = (bDescIdx ? -1 : 1);
165288 }
165289
165290 *pnList = (int)(pEnd - pNext);
165291 *ppIter = pNext;
165292 *piDocid = iDocid;
165293 }else{
165294 int iMul = (bDescIdx ? -1 : 1);
165295 sqlite3_int64 iDelta;
165296 fts3GetReverseVarint(&p, aDoclist, &iDelta);
165297 *piDocid -= (iMul * iDelta);
165298
165299 if( p==aDoclist ){
165300 *pbEof = 1;
165301 }else{
165302 char *pSave = p;
165303 fts3ReversePoslist(aDoclist, &p);
165304 *pnList = (int)(pSave - p);
165305 }
165306 *ppIter = p;
165307 }
165308}
165309
165310/*
165311** Iterate forwards through a doclist.
165312*/
165313SQLITE_PRIVATEstatic void sqlite3Fts3DoclistNext(
165314 int bDescIdx, /* True if the doclist is desc */
165315 char *aDoclist, /* Pointer to entire doclist */
165316 int nDoclist, /* Length of aDoclist in bytes */
165317 char **ppIter, /* IN/OUT: Iterator pointer */
165318 sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
165319 u8 *pbEof /* OUT: End-of-file flag */
165320){
165321 char *p = *ppIter;
165322
165323 assert( nDoclist>0 )((void) (0));
165324 assert( *pbEof==0 )((void) (0));
165325 assert( p || *piDocid==0 )((void) (0));
165326 assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) )((void) (0));
165327
165328 if( p==0 ){
165329 p = aDoclist;
165330 p += sqlite3Fts3GetVarint(p, piDocid);
165331 }else{
165332 fts3PoslistCopy(0, &p);
165333 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
165334 if( p>=&aDoclist[nDoclist] ){
165335 *pbEof = 1;
165336 }else{
165337 sqlite3_int64 iVar;
165338 p += sqlite3Fts3GetVarint(p, &iVar);
165339 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
165340 }
165341 }
165342
165343 *ppIter = p;
165344}
165345
165346/*
165347** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
165348** to true if EOF is reached.
165349*/
165350static void fts3EvalDlPhraseNext(
165351 Fts3Table *pTab,
165352 Fts3Doclist *pDL,
165353 u8 *pbEof
165354){
165355 char *pIter; /* Used to iterate through aAll */
165356 char *pEnd = &pDL->aAll[pDL->nAll]; /* 1 byte past end of aAll */
165357
165358 if( pDL->pNextDocid ){
165359 pIter = pDL->pNextDocid;
165360 }else{
165361 pIter = pDL->aAll;
165362 }
165363
165364 if( pIter>=pEnd ){
165365 /* We have already reached the end of this doclist. EOF. */
165366 *pbEof = 1;
165367 }else{
165368 sqlite3_int64 iDelta;
165369 pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
165370 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
165371 pDL->iDocid += iDelta;
165372 }else{
165373 pDL->iDocid -= iDelta;
165374 }
165375 pDL->pList = pIter;
165376 fts3PoslistCopy(0, &pIter);
165377 pDL->nList = (int)(pIter - pDL->pList);
165378
165379 /* pIter now points just past the 0x00 that terminates the position-
165380 ** list for document pDL->iDocid. However, if this position-list was
165381 ** edited in place by fts3EvalNearTrim(), then pIter may not actually
165382 ** point to the start of the next docid value. The following line deals
165383 ** with this case by advancing pIter past the zero-padding added by
165384 ** fts3EvalNearTrim(). */
165385 while( pIter<pEnd && *pIter==0 ) pIter++;
165386
165387 pDL->pNextDocid = pIter;
165388 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter )((void) (0));
165389 *pbEof = 0;
165390 }
165391}
165392
165393/*
165394** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
165395*/
165396typedef struct TokenDoclist TokenDoclist;
165397struct TokenDoclist {
165398 int bIgnore;
165399 sqlite3_int64 iDocid;
165400 char *pList;
165401 int nList;
165402};
165403
165404/*
165405** Token pToken is an incrementally loaded token that is part of a
165406** multi-token phrase. Advance it to the next matching document in the
165407** database and populate output variable *p with the details of the new
165408** entry. Or, if the iterator has reached EOF, set *pbEof to true.
165409**
165410** If an error occurs, return an SQLite error code. Otherwise, return
165411** SQLITE_OK.
165412*/
165413static int incrPhraseTokenNext(
165414 Fts3Table *pTab, /* Virtual table handle */
165415 Fts3Phrase *pPhrase, /* Phrase to advance token of */
165416 int iToken, /* Specific token to advance */
165417 TokenDoclist *p, /* OUT: Docid and doclist for new entry */
165418 u8 *pbEof /* OUT: True if iterator is at EOF */
165419){
165420 int rc = SQLITE_OK0;
165421
165422 if( pPhrase->iDoclistToken==iToken ){
165423 assert( p->bIgnore==0 )((void) (0));
165424 assert( pPhrase->aToken[iToken].pSegcsr==0 )((void) (0));
165425 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
165426 p->pList = pPhrase->doclist.pList;
165427 p->nList = pPhrase->doclist.nList;
165428 p->iDocid = pPhrase->doclist.iDocid;
165429 }else{
165430 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
165431 assert( pToken->pDeferred==0 )((void) (0));
165432 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 )((void) (0));
165433 if( pToken->pSegcsr ){
165434 assert( p->bIgnore==0 )((void) (0));
165435 rc = sqlite3Fts3MsrIncrNext(
165436 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
165437 );
165438 if( p->pList==0 ) *pbEof = 1;
165439 }else{
165440 p->bIgnore = 1;
165441 }
165442 }
165443
165444 return rc;
165445}
165446
165447
165448/*
165449** The phrase iterator passed as the second argument:
165450**
165451** * features at least one token that uses an incremental doclist, and
165452**
165453** * does not contain any deferred tokens.
165454**
165455** Advance it to the next matching documnent in the database and populate
165456** the Fts3Doclist.pList and nList fields.
165457**
165458** If there is no "next" entry and no error occurs, then *pbEof is set to
165459** 1 before returning. Otherwise, if no error occurs and the iterator is
165460** successfully advanced, *pbEof is set to 0.
165461**
165462** If an error occurs, return an SQLite error code. Otherwise, return
165463** SQLITE_OK.
165464*/
165465static int fts3EvalIncrPhraseNext(
165466 Fts3Cursor *pCsr, /* FTS Cursor handle */
165467 Fts3Phrase *p, /* Phrase object to advance to next docid */
165468 u8 *pbEof /* OUT: Set to 1 if EOF */
165469){
165470 int rc = SQLITE_OK0;
165471 Fts3Doclist *pDL = &p->doclist;
165472 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
165473 u8 bEof = 0;
165474
165475 /* This is only called if it is guaranteed that the phrase has at least
165476 ** one incremental token. In which case the bIncr flag is set. */
165477 assert( p->bIncr==1 )((void) (0));
165478
165479 if( p->nToken==1 ){
165480 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
165481 &pDL->iDocid, &pDL->pList, &pDL->nList
165482 );
165483 if( pDL->pList==0 ) bEof = 1;
165484 }else{
165485 int bDescDoclist = pCsr->bDesc;
165486 struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
165487
165488 memset(a, 0, sizeof(a));
165489 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS )((void) (0));
165490 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS )((void) (0));
165491
165492 while( bEof==0 ){
165493 int bMaxSet = 0;
165494 sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
165495 int i; /* Used to iterate through tokens */
165496
165497 /* Advance the iterator for each token in the phrase once. */
165498 for(i=0; rc==SQLITE_OK0 && i<p->nToken && bEof==0; i++){
165499 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
165500 if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
165501 iMax = a[i].iDocid;
165502 bMaxSet = 1;
165503 }
165504 }
165505 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) )((void) (0));
165506 assert( rc!=SQLITE_OK || bMaxSet )((void) (0));
165507
165508 /* Keep advancing iterators until they all point to the same document */
165509 for(i=0; i<p->nToken; i++){
165510 while( rc==SQLITE_OK0 && bEof==0
165511 && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
165512 ){
165513 rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
165514 if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
165515 iMax = a[i].iDocid;
165516 i = 0;
165517 }
165518 }
165519 }
165520
165521 /* Check if the current entries really are a phrase match */
165522 if( bEof==0 ){
165523 int nList = 0;
165524 int nByte = a[p->nToken-1].nList;
165525 char *aDoclist = sqlite3_malloc(nByte+FTS3_BUFFER_PADDING);
165526 if( !aDoclist ) return SQLITE_NOMEM7;
165527 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
165528 memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING);
165529
165530 for(i=0; i<(p->nToken-1); i++){
165531 if( a[i].bIgnore==0 ){
165532 char *pL = a[i].pList;
165533 char *pR = aDoclist;
165534 char *pOut = aDoclist;
165535 int nDist = p->nToken-1-i;
165536 int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
165537 if( res==0 ) break;
165538 nList = (int)(pOut - aDoclist);
165539 }
165540 }
165541 if( i==(p->nToken-1) ){
165542 pDL->iDocid = iMax;
165543 pDL->pList = aDoclist;
165544 pDL->nList = nList;
165545 pDL->bFreeList = 1;
165546 break;
165547 }
165548 sqlite3_free(aDoclist);
165549 }
165550 }
165551 }
165552
165553 *pbEof = bEof;
165554 return rc;
165555}
165556
165557/*
165558** Attempt to move the phrase iterator to point to the next matching docid.
165559** If an error occurs, return an SQLite error code. Otherwise, return
165560** SQLITE_OK.
165561**
165562** If there is no "next" entry and no error occurs, then *pbEof is set to
165563** 1 before returning. Otherwise, if no error occurs and the iterator is
165564** successfully advanced, *pbEof is set to 0.
165565*/
165566static int fts3EvalPhraseNext(
165567 Fts3Cursor *pCsr, /* FTS Cursor handle */
165568 Fts3Phrase *p, /* Phrase object to advance to next docid */
165569 u8 *pbEof /* OUT: Set to 1 if EOF */
165570){
165571 int rc = SQLITE_OK0;
165572 Fts3Doclist *pDL = &p->doclist;
165573 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
165574
165575 if( p->bIncr ){
165576 rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
165577 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
165578 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
165579 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
165580 );
165581 pDL->pList = pDL->pNextDocid;
165582 }else{
165583 fts3EvalDlPhraseNext(pTab, pDL, pbEof);
165584 }
165585
165586 return rc;
165587}
165588
165589/*
165590**
165591** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
165592** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
165593** expression. Also the Fts3Expr.bDeferred variable is set to true for any
165594** expressions for which all descendent tokens are deferred.
165595**
165596** If parameter bOptOk is zero, then it is guaranteed that the
165597** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
165598** each phrase in the expression (subject to deferred token processing).
165599** Or, if bOptOk is non-zero, then one or more tokens within the expression
165600** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
165601**
165602** If an error occurs within this function, *pRc is set to an SQLite error
165603** code before returning.
165604*/
165605static void fts3EvalStartReaders(
165606 Fts3Cursor *pCsr, /* FTS Cursor handle */
165607 Fts3Expr *pExpr, /* Expression to initialize phrases in */
165608 int *pRc /* IN/OUT: Error code */
165609){
165610 if( pExpr && SQLITE_OK0==*pRc ){
165611 if( pExpr->eType==FTSQUERY_PHRASE ){
165612 int nToken = pExpr->pPhrase->nToken;
165613 if( nToken ){
165614 int i;
165615 for(i=0; i<nToken; i++){
165616 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
165617 }
165618 pExpr->bDeferred = (i==nToken);
165619 }
165620 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
165621 }else{
165622 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
165623 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
165624 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
165625 }
165626 }
165627}
165628
165629/*
165630** An array of the following structures is assembled as part of the process
165631** of selecting tokens to defer before the query starts executing (as part
165632** of the xFilter() method). There is one element in the array for each
165633** token in the FTS expression.
165634**
165635** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
165636** to phrases that are connected only by AND and NEAR operators (not OR or
165637** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
165638** separately. The root of a tokens AND/NEAR cluster is stored in
165639** Fts3TokenAndCost.pRoot.
165640*/
165641typedef struct Fts3TokenAndCost Fts3TokenAndCost;
165642struct Fts3TokenAndCost {
165643 Fts3Phrase *pPhrase; /* The phrase the token belongs to */
165644 int iToken; /* Position of token in phrase */
165645 Fts3PhraseToken *pToken; /* The token itself */
165646 Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
165647 int nOvfl; /* Number of overflow pages to load doclist */
165648 int iCol; /* The column the token must match */
165649};
165650
165651/*
165652** This function is used to populate an allocated Fts3TokenAndCost array.
165653**
165654** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
165655** Otherwise, if an error occurs during execution, *pRc is set to an
165656** SQLite error code.
165657*/
165658static void fts3EvalTokenCosts(
165659 Fts3Cursor *pCsr, /* FTS Cursor handle */
165660 Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
165661 Fts3Expr *pExpr, /* Expression to consider */
165662 Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
165663 Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
165664 int *pRc /* IN/OUT: Error code */
165665){
165666 if( *pRc==SQLITE_OK0 ){
165667 if( pExpr->eType==FTSQUERY_PHRASE ){
165668 Fts3Phrase *pPhrase = pExpr->pPhrase;
165669 int i;
165670 for(i=0; *pRc==SQLITE_OK0 && i<pPhrase->nToken; i++){
165671 Fts3TokenAndCost *pTC = (*ppTC)++;
165672 pTC->pPhrase = pPhrase;
165673 pTC->iToken = i;
165674 pTC->pRoot = pRoot;
165675 pTC->pToken = &pPhrase->aToken[i];
165676 pTC->iCol = pPhrase->iColumn;
165677 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
165678 }
165679 }else if( pExpr->eType!=FTSQUERY_NOT ){
165680 assert( pExpr->eType==FTSQUERY_OR((void) (0))
165681 || pExpr->eType==FTSQUERY_AND((void) (0))
165682 || pExpr->eType==FTSQUERY_NEAR((void) (0))
165683 )((void) (0));
165684 assert( pExpr->pLeft && pExpr->pRight )((void) (0));
165685 if( pExpr->eType==FTSQUERY_OR ){
165686 pRoot = pExpr->pLeft;
165687 **ppOr = pRoot;
165688 (*ppOr)++;
165689 }
165690 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
165691 if( pExpr->eType==FTSQUERY_OR ){
165692 pRoot = pExpr->pRight;
165693 **ppOr = pRoot;
165694 (*ppOr)++;
165695 }
165696 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
165697 }
165698 }
165699}
165700
165701/*
165702** Determine the average document (row) size in pages. If successful,
165703** write this value to *pnPage and return SQLITE_OK. Otherwise, return
165704** an SQLite error code.
165705**
165706** The average document size in pages is calculated by first calculating
165707** determining the average size in bytes, B. If B is less than the amount
165708** of data that will fit on a single leaf page of an intkey table in
165709** this database, then the average docsize is 1. Otherwise, it is 1 plus
165710** the number of overflow pages consumed by a record B bytes in size.
165711*/
165712static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
165713 int rc = SQLITE_OK0;
165714 if( pCsr->nRowAvg==0 ){
165715 /* The average document size, which is required to calculate the cost
165716 ** of each doclist, has not yet been determined. Read the required
165717 ** data from the %_stat table to calculate it.
165718 **
165719 ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
165720 ** varints, where nCol is the number of columns in the FTS3 table.
165721 ** The first varint is the number of documents currently stored in
165722 ** the table. The following nCol varints contain the total amount of
165723 ** data stored in all rows of each column of the table, from left
165724 ** to right.
165725 */
165726 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
165727 sqlite3_stmt *pStmt;
165728 sqlite3_int64 nDoc = 0;
165729 sqlite3_int64 nByte = 0;
165730 const char *pEnd;
165731 const char *a;
165732
165733 rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
165734 if( rc!=SQLITE_OK0 ) return rc;
165735 a = sqlite3_column_blob(pStmt, 0);
165736 assert( a )((void) (0));
165737
165738 pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
165739 a += sqlite3Fts3GetVarint(a, &nDoc);
165740 while( a<pEnd ){
165741 a += sqlite3Fts3GetVarint(a, &nByte);
165742 }
165743 if( nDoc==0 || nByte==0 ){
165744 sqlite3_reset(pStmt);
165745 return FTS_CORRUPT_VTAB;
165746 }
165747
165748 pCsr->nDoc = nDoc;
165749 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
165750 assert( pCsr->nRowAvg>0 )((void) (0));
165751 rc = sqlite3_reset(pStmt);
165752 }
165753
165754 *pnPage = pCsr->nRowAvg;
165755 return rc;
165756}
165757
165758/*
165759** This function is called to select the tokens (if any) that will be
165760** deferred. The array aTC[] has already been populated when this is
165761** called.
165762**
165763** This function is called once for each AND/NEAR cluster in the
165764** expression. Each invocation determines which tokens to defer within
165765** the cluster with root node pRoot. See comments above the definition
165766** of struct Fts3TokenAndCost for more details.
165767**
165768** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
165769** called on each token to defer. Otherwise, an SQLite error code is
165770** returned.
165771*/
165772static int fts3EvalSelectDeferred(
165773 Fts3Cursor *pCsr, /* FTS Cursor handle */
165774 Fts3Expr *pRoot, /* Consider tokens with this root node */
165775 Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
165776 int nTC /* Number of entries in aTC[] */
165777){
165778 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
165779 int nDocSize = 0; /* Number of pages per doc loaded */
165780 int rc = SQLITE_OK0; /* Return code */
165781 int ii; /* Iterator variable for various purposes */
165782 int nOvfl = 0; /* Total overflow pages used by doclists */
165783 int nToken = 0; /* Total number of tokens in cluster */
165784
165785 int nMinEst = 0; /* The minimum count for any phrase so far. */
165786 int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
165787
165788 /* Tokens are never deferred for FTS tables created using the content=xxx
165789 ** option. The reason being that it is not guaranteed that the content
165790 ** table actually contains the same data as the index. To prevent this from
165791 ** causing any problems, the deferred token optimization is completely
165792 ** disabled for content=xxx tables. */
165793 if( pTab->zContentTbl ){
165794 return SQLITE_OK0;
165795 }
165796
165797 /* Count the tokens in this AND/NEAR cluster. If none of the doclists
165798 ** associated with the tokens spill onto overflow pages, or if there is
165799 ** only 1 token, exit early. No tokens to defer in this case. */
165800 for(ii=0; ii<nTC; ii++){
165801 if( aTC[ii].pRoot==pRoot ){
165802 nOvfl += aTC[ii].nOvfl;
165803 nToken++;
165804 }
165805 }
165806 if( nOvfl==0 || nToken<2 ) return SQLITE_OK0;
165807
165808 /* Obtain the average docsize (in pages). */
165809 rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
165810 assert( rc!=SQLITE_OK || nDocSize>0 )((void) (0));
165811
165812
165813 /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
165814 ** of the number of overflow pages that will be loaded by the pager layer
165815 ** to retrieve the entire doclist for the token from the full-text index.
165816 ** Load the doclists for tokens that are either:
165817 **
165818 ** a. The cheapest token in the entire query (i.e. the one visited by the
165819 ** first iteration of this loop), or
165820 **
165821 ** b. Part of a multi-token phrase.
165822 **
165823 ** After each token doclist is loaded, merge it with the others from the
165824 ** same phrase and count the number of documents that the merged doclist
165825 ** contains. Set variable "nMinEst" to the smallest number of documents in
165826 ** any phrase doclist for which 1 or more token doclists have been loaded.
165827 ** Let nOther be the number of other phrases for which it is certain that
165828 ** one or more tokens will not be deferred.
165829 **
165830 ** Then, for each token, defer it if loading the doclist would result in
165831 ** loading N or more overflow pages into memory, where N is computed as:
165832 **
165833 ** (nMinEst + 4^nOther - 1) / (4^nOther)
165834 */
165835 for(ii=0; ii<nToken && rc==SQLITE_OK0; ii++){
165836 int iTC; /* Used to iterate through aTC[] array. */
165837 Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
165838
165839 /* Set pTC to point to the cheapest remaining token. */
165840 for(iTC=0; iTC<nTC; iTC++){
165841 if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
165842 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
165843 ){
165844 pTC = &aTC[iTC];
165845 }
165846 }
165847 assert( pTC )((void) (0));
165848
165849 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
165850 /* The number of overflow pages to load for this (and therefore all
165851 ** subsequent) tokens is greater than the estimated number of pages
165852 ** that will be loaded if all subsequent tokens are deferred.
165853 */
165854 Fts3PhraseToken *pToken = pTC->pToken;
165855 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
165856 fts3SegReaderCursorFree(pToken->pSegcsr);
165857 pToken->pSegcsr = 0;
165858 }else{
165859 /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
165860 ** for-loop. Except, limit the value to 2^24 to prevent it from
165861 ** overflowing the 32-bit integer it is stored in. */
165862 if( ii<12 ) nLoad4 = nLoad4*4;
165863
165864 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
165865 /* Either this is the cheapest token in the entire query, or it is
165866 ** part of a multi-token phrase. Either way, the entire doclist will
165867 ** (eventually) be loaded into memory. It may as well be now. */
165868 Fts3PhraseToken *pToken = pTC->pToken;
165869 int nList = 0;
165870 char *pList = 0;
165871 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
165872 assert( rc==SQLITE_OK || pList==0 )((void) (0));
165873 if( rc==SQLITE_OK0 ){
165874 rc = fts3EvalPhraseMergeToken(
165875 pTab, pTC->pPhrase, pTC->iToken,pList,nList
165876 );
165877 }
165878 if( rc==SQLITE_OK0 ){
165879 int nCount;
165880 nCount = fts3DoclistCountDocids(
165881 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
165882 );
165883 if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
165884 }
165885 }
165886 }
165887 pTC->pToken = 0;
165888 }
165889
165890 return rc;
165891}
165892
165893/*
165894** This function is called from within the xFilter method. It initializes
165895** the full-text query currently stored in pCsr->pExpr. To iterate through
165896** the results of a query, the caller does:
165897**
165898** fts3EvalStart(pCsr);
165899** while( 1 ){
165900** fts3EvalNext(pCsr);
165901** if( pCsr->bEof ) break;
165902** ... return row pCsr->iPrevId to the caller ...
165903** }
165904*/
165905static int fts3EvalStart(Fts3Cursor *pCsr){
165906 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
165907 int rc = SQLITE_OK0;
165908 int nToken = 0;
165909 int nOr = 0;
165910
165911 /* Allocate a MultiSegReader for each token in the expression. */
165912 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
165913
165914 /* Determine which, if any, tokens in the expression should be deferred. */
165915#ifndef SQLITE_DISABLE_FTS4_DEFERRED
165916 if( rc==SQLITE_OK0 && nToken>1 && pTab->bFts4 ){
165917 Fts3TokenAndCost *aTC;
165918 Fts3Expr **apOr;
165919 aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
165920 sizeof(Fts3TokenAndCost) * nToken
165921 + sizeof(Fts3Expr *) * nOr * 2
165922 );
165923 apOr = (Fts3Expr **)&aTC[nToken];
165924
165925 if( !aTC ){
165926 rc = SQLITE_NOMEM7;
165927 }else{
165928 int ii;
165929 Fts3TokenAndCost *pTC = aTC;
165930 Fts3Expr **ppOr = apOr;
165931
165932 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
165933 nToken = (int)(pTC-aTC);
165934 nOr = (int)(ppOr-apOr);
165935
165936 if( rc==SQLITE_OK0 ){
165937 rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
165938 for(ii=0; rc==SQLITE_OK0 && ii<nOr; ii++){
165939 rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
165940 }
165941 }
165942
165943 sqlite3_free(aTC);
165944 }
165945 }
165946#endif
165947
165948 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
165949 return rc;
165950}
165951
165952/*
165953** Invalidate the current position list for phrase pPhrase.
165954*/
165955static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
165956 if( pPhrase->doclist.bFreeList ){
165957 sqlite3_free(pPhrase->doclist.pList);
165958 }
165959 pPhrase->doclist.pList = 0;
165960 pPhrase->doclist.nList = 0;
165961 pPhrase->doclist.bFreeList = 0;
165962}
165963
165964/*
165965** This function is called to edit the position list associated with
165966** the phrase object passed as the fifth argument according to a NEAR
165967** condition. For example:
165968**
165969** abc NEAR/5 "def ghi"
165970**
165971** Parameter nNear is passed the NEAR distance of the expression (5 in
165972** the example above). When this function is called, *paPoslist points to
165973** the position list, and *pnToken is the number of phrase tokens in, the
165974** phrase on the other side of the NEAR operator to pPhrase. For example,
165975** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
165976** the position list associated with phrase "abc".
165977**
165978** All positions in the pPhrase position list that are not sufficiently
165979** close to a position in the *paPoslist position list are removed. If this
165980** leaves 0 positions, zero is returned. Otherwise, non-zero.
165981**
165982** Before returning, *paPoslist is set to point to the position lsit
165983** associated with pPhrase. And *pnToken is set to the number of tokens in
165984** pPhrase.
165985*/
165986static int fts3EvalNearTrim(
165987 int nNear, /* NEAR distance. As in "NEAR/nNear". */
165988 char *aTmp, /* Temporary space to use */
165989 char **paPoslist, /* IN/OUT: Position list */
165990 int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
165991 Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
165992){
165993 int nParam1 = nNear + pPhrase->nToken;
165994 int nParam2 = nNear + *pnToken;
165995 int nNew;
165996 char *p2;
165997 char *pOut;
165998 int res;
165999
166000 assert( pPhrase->doclist.pList )((void) (0));
166001
166002 p2 = pOut = pPhrase->doclist.pList;
166003 res = fts3PoslistNearMerge(
166004 &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
166005 );
166006 if( res ){
166007 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
166008 assert( pPhrase->doclist.pList[nNew]=='\0' )((void) (0));
166009 assert( nNew<=pPhrase->doclist.nList && nNew>0 )((void) (0));
166010 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
166011 pPhrase->doclist.nList = nNew;
166012 *paPoslist = pPhrase->doclist.pList;
166013 *pnToken = pPhrase->nToken;
166014 }
166015
166016 return res;
166017}
166018
166019/*
166020** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
166021** Otherwise, it advances the expression passed as the second argument to
166022** point to the next matching row in the database. Expressions iterate through
166023** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
166024** or descending if it is non-zero.
166025**
166026** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
166027** successful, the following variables in pExpr are set:
166028**
166029** Fts3Expr.bEof (non-zero if EOF - there is no next row)
166030** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
166031**
166032** If the expression is of type FTSQUERY_PHRASE, and the expression is not
166033** at EOF, then the following variables are populated with the position list
166034** for the phrase for the visited row:
166035**
166036** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
166037** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
166038**
166039** It says above that this function advances the expression to the next
166040** matching row. This is usually true, but there are the following exceptions:
166041**
166042** 1. Deferred tokens are not taken into account. If a phrase consists
166043** entirely of deferred tokens, it is assumed to match every row in
166044** the db. In this case the position-list is not populated at all.
166045**
166046** Or, if a phrase contains one or more deferred tokens and one or
166047** more non-deferred tokens, then the expression is advanced to the
166048** next possible match, considering only non-deferred tokens. In other
166049** words, if the phrase is "A B C", and "B" is deferred, the expression
166050** is advanced to the next row that contains an instance of "A * C",
166051** where "*" may match any single token. The position list in this case
166052** is populated as for "A * C" before returning.
166053**
166054** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
166055** advanced to point to the next row that matches "x AND y".
166056**
166057** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
166058** really a match, taking into account deferred tokens and NEAR operators.
166059*/
166060static void fts3EvalNextRow(
166061 Fts3Cursor *pCsr, /* FTS Cursor handle */
166062 Fts3Expr *pExpr, /* Expr. to advance to next matching row */
166063 int *pRc /* IN/OUT: Error code */
166064){
166065 if( *pRc==SQLITE_OK0 ){
166066 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
166067 assert( pExpr->bEof==0 )((void) (0));
166068 pExpr->bStart = 1;
166069
166070 switch( pExpr->eType ){
166071 case FTSQUERY_NEAR:
166072 case FTSQUERY_AND: {
166073 Fts3Expr *pLeft = pExpr->pLeft;
166074 Fts3Expr *pRight = pExpr->pRight;
166075 assert( !pLeft->bDeferred || !pRight->bDeferred )((void) (0));
166076
166077 if( pLeft->bDeferred ){
166078 /* LHS is entirely deferred. So we assume it matches every row.
166079 ** Advance the RHS iterator to find the next row visited. */
166080 fts3EvalNextRow(pCsr, pRight, pRc);
166081 pExpr->iDocid = pRight->iDocid;
166082 pExpr->bEof = pRight->bEof;
166083 }else if( pRight->bDeferred ){
166084 /* RHS is entirely deferred. So we assume it matches every row.
166085 ** Advance the LHS iterator to find the next row visited. */
166086 fts3EvalNextRow(pCsr, pLeft, pRc);
166087 pExpr->iDocid = pLeft->iDocid;
166088 pExpr->bEof = pLeft->bEof;
166089 }else{
166090 /* Neither the RHS or LHS are deferred. */
166091 fts3EvalNextRow(pCsr, pLeft, pRc);
166092 fts3EvalNextRow(pCsr, pRight, pRc);
166093 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK0 ){
166094 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
166095 if( iDiff==0 ) break;
166096 if( iDiff<0 ){
166097 fts3EvalNextRow(pCsr, pLeft, pRc);
166098 }else{
166099 fts3EvalNextRow(pCsr, pRight, pRc);
166100 }
166101 }
166102 pExpr->iDocid = pLeft->iDocid;
166103 pExpr->bEof = (pLeft->bEof || pRight->bEof);
166104 if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
166105 assert( pRight->eType==FTSQUERY_PHRASE )((void) (0));
166106 if( pRight->pPhrase->doclist.aAll ){
166107 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
166108 while( *pRc==SQLITE_OK0 && pRight->bEof==0 ){
166109 memset(pDl->pList, 0, pDl->nList);
166110 fts3EvalNextRow(pCsr, pRight, pRc);
166111 }
166112 }
166113 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
166114 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
166115 while( *pRc==SQLITE_OK0 && pLeft->bEof==0 ){
166116 memset(pDl->pList, 0, pDl->nList);
166117 fts3EvalNextRow(pCsr, pLeft, pRc);
166118 }
166119 }
166120 }
166121 }
166122 break;
166123 }
166124
166125 case FTSQUERY_OR: {
166126 Fts3Expr *pLeft = pExpr->pLeft;
166127 Fts3Expr *pRight = pExpr->pRight;
166128 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
166129
166130 assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid )((void) (0));
166131 assert( pRight->bStart || pLeft->iDocid==pRight->iDocid )((void) (0));
166132
166133 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
166134 fts3EvalNextRow(pCsr, pLeft, pRc);
166135 }else if( pLeft->bEof || iCmp>0 ){
166136 fts3EvalNextRow(pCsr, pRight, pRc);
166137 }else{
166138 fts3EvalNextRow(pCsr, pLeft, pRc);
166139 fts3EvalNextRow(pCsr, pRight, pRc);
166140 }
166141
166142 pExpr->bEof = (pLeft->bEof && pRight->bEof);
166143 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
166144 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
166145 pExpr->iDocid = pLeft->iDocid;
166146 }else{
166147 pExpr->iDocid = pRight->iDocid;
166148 }
166149
166150 break;
166151 }
166152
166153 case FTSQUERY_NOT: {
166154 Fts3Expr *pLeft = pExpr->pLeft;
166155 Fts3Expr *pRight = pExpr->pRight;
166156
166157 if( pRight->bStart==0 ){
166158 fts3EvalNextRow(pCsr, pRight, pRc);
166159 assert( *pRc!=SQLITE_OK || pRight->bStart )((void) (0));
166160 }
166161
166162 fts3EvalNextRow(pCsr, pLeft, pRc);
166163 if( pLeft->bEof==0 ){
166164 while( !*pRc
166165 && !pRight->bEof
166166 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
166167 ){
166168 fts3EvalNextRow(pCsr, pRight, pRc);
166169 }
166170 }
166171 pExpr->iDocid = pLeft->iDocid;
166172 pExpr->bEof = pLeft->bEof;
166173 break;
166174 }
166175
166176 default: {
166177 Fts3Phrase *pPhrase = pExpr->pPhrase;
166178 fts3EvalInvalidatePoslist(pPhrase);
166179 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
166180 pExpr->iDocid = pPhrase->doclist.iDocid;
166181 break;
166182 }
166183 }
166184 }
166185}
166186
166187/*
166188** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
166189** cluster, then this function returns 1 immediately.
166190**
166191** Otherwise, it checks if the current row really does match the NEAR
166192** expression, using the data currently stored in the position lists
166193** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
166194**
166195** If the current row is a match, the position list associated with each
166196** phrase in the NEAR expression is edited in place to contain only those
166197** phrase instances sufficiently close to their peers to satisfy all NEAR
166198** constraints. In this case it returns 1. If the NEAR expression does not
166199** match the current row, 0 is returned. The position lists may or may not
166200** be edited if 0 is returned.
166201*/
166202static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
166203 int res = 1;
166204
166205 /* The following block runs if pExpr is the root of a NEAR query.
166206 ** For example, the query:
166207 **
166208 ** "w" NEAR "x" NEAR "y" NEAR "z"
166209 **
166210 ** which is represented in tree form as:
166211 **
166212 ** |
166213 ** +--NEAR--+ <-- root of NEAR query
166214 ** | |
166215 ** +--NEAR--+ "z"
166216 ** | |
166217 ** +--NEAR--+ "y"
166218 ** | |
166219 ** "w" "x"
166220 **
166221 ** The right-hand child of a NEAR node is always a phrase. The
166222 ** left-hand child may be either a phrase or a NEAR node. There are
166223 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
166224 */
166225 if( *pRc==SQLITE_OK0
166226 && pExpr->eType==FTSQUERY_NEAR
166227 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
166228 ){
166229 Fts3Expr *p;
166230 sqlite3_int64 nTmp = 0; /* Bytes of temp space */
166231 char *aTmp; /* Temp space for PoslistNearMerge() */
166232
166233 /* Allocate temporary working space. */
166234 for(p=pExpr; p->pLeft; p=p->pLeft){
166235 assert( p->pRight->pPhrase->doclist.nList>0 )((void) (0));
166236 nTmp += p->pRight->pPhrase->doclist.nList;
166237 }
166238 nTmp += p->pPhrase->doclist.nList;
166239 aTmp = sqlite3_malloc64(nTmp*2);
166240 if( !aTmp ){
166241 *pRc = SQLITE_NOMEM7;
166242 res = 0;
166243 }else{
166244 char *aPoslist = p->pPhrase->doclist.pList;
166245 int nToken = p->pPhrase->nToken;
166246
166247 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
166248 Fts3Phrase *pPhrase = p->pRight->pPhrase;
166249 int nNear = p->nNear;
166250 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
166251 }
166252
166253 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
166254 nToken = pExpr->pRight->pPhrase->nToken;
166255 for(p=pExpr->pLeft; p && res; p=p->pLeft){
166256 int nNear;
166257 Fts3Phrase *pPhrase;
166258 assert( p->pParent && p->pParent->pLeft==p )((void) (0));
166259 nNear = p->pParent->nNear;
166260 pPhrase = (
166261 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
166262 );
166263 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
166264 }
166265 }
166266
166267 sqlite3_free(aTmp);
166268 }
166269
166270 return res;
166271}
166272
166273/*
166274** This function is a helper function for sqlite3Fts3EvalTestDeferred().
166275** Assuming no error occurs or has occurred, It returns non-zero if the
166276** expression passed as the second argument matches the row that pCsr
166277** currently points to, or zero if it does not.
166278**
166279** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
166280** If an error occurs during execution of this function, *pRc is set to
166281** the appropriate SQLite error code. In this case the returned value is
166282** undefined.
166283*/
166284static int fts3EvalTestExpr(
166285 Fts3Cursor *pCsr, /* FTS cursor handle */
166286 Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
166287 int *pRc /* IN/OUT: Error code */
166288){
166289 int bHit = 1; /* Return value */
166290 if( *pRc==SQLITE_OK0 ){
166291 switch( pExpr->eType ){
166292 case FTSQUERY_NEAR:
166293 case FTSQUERY_AND:
166294 bHit = (
166295 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
166296 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
166297 && fts3EvalNearTest(pExpr, pRc)
166298 );
166299
166300 /* If the NEAR expression does not match any rows, zero the doclist for
166301 ** all phrases involved in the NEAR. This is because the snippet(),
166302 ** offsets() and matchinfo() functions are not supposed to recognize
166303 ** any instances of phrases that are part of unmatched NEAR queries.
166304 ** For example if this expression:
166305 **
166306 ** ... MATCH 'a OR (b NEAR c)'
166307 **
166308 ** is matched against a row containing:
166309 **
166310 ** 'a b d e'
166311 **
166312 ** then any snippet() should ony highlight the "a" term, not the "b"
166313 ** (as "b" is part of a non-matching NEAR clause).
166314 */
166315 if( bHit==0
166316 && pExpr->eType==FTSQUERY_NEAR
166317 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
166318 ){
166319 Fts3Expr *p;
166320 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
166321 if( p->pRight->iDocid==pCsr->iPrevId ){
166322 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
166323 }
166324 }
166325 if( p->iDocid==pCsr->iPrevId ){
166326 fts3EvalInvalidatePoslist(p->pPhrase);
166327 }
166328 }
166329
166330 break;
166331
166332 case FTSQUERY_OR: {
166333 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
166334 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
166335 bHit = bHit1 || bHit2;
166336 break;
166337 }
166338
166339 case FTSQUERY_NOT:
166340 bHit = (
166341 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
166342 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
166343 );
166344 break;
166345
166346 default: {
166347#ifndef SQLITE_DISABLE_FTS4_DEFERRED
166348 if( pCsr->pDeferred
166349 && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
166350 ){
166351 Fts3Phrase *pPhrase = pExpr->pPhrase;
166352 assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 )((void) (0));
166353 if( pExpr->bDeferred ){
166354 fts3EvalInvalidatePoslist(pPhrase);
166355 }
166356 *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
166357 bHit = (pPhrase->doclist.pList!=0);
166358 pExpr->iDocid = pCsr->iPrevId;
166359 }else
166360#endif
166361 {
166362 bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
166363 }
166364 break;
166365 }
166366 }
166367 }
166368 return bHit;
166369}
166370
166371/*
166372** This function is called as the second part of each xNext operation when
166373** iterating through the results of a full-text query. At this point the
166374** cursor points to a row that matches the query expression, with the
166375** following caveats:
166376**
166377** * Up until this point, "NEAR" operators in the expression have been
166378** treated as "AND".
166379**
166380** * Deferred tokens have not yet been considered.
166381**
166382** If *pRc is not SQLITE_OK when this function is called, it immediately
166383** returns 0. Otherwise, it tests whether or not after considering NEAR
166384** operators and deferred tokens the current row is still a match for the
166385** expression. It returns 1 if both of the following are true:
166386**
166387** 1. *pRc is SQLITE_OK when this function returns, and
166388**
166389** 2. After scanning the current FTS table row for the deferred tokens,
166390** it is determined that the row does *not* match the query.
166391**
166392** Or, if no error occurs and it seems the current row does match the FTS
166393** query, return 0.
166394*/
166395SQLITE_PRIVATEstatic int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
166396 int rc = *pRc;
166397 int bMiss = 0;
166398 if( rc==SQLITE_OK0 ){
166399
166400 /* If there are one or more deferred tokens, load the current row into
166401 ** memory and scan it to determine the position list for each deferred
166402 ** token. Then, see if this row is really a match, considering deferred
166403 ** tokens and NEAR operators (neither of which were taken into account
166404 ** earlier, by fts3EvalNextRow()).
166405 */
166406 if( pCsr->pDeferred ){
166407 rc = fts3CursorSeek(0, pCsr);
166408 if( rc==SQLITE_OK0 ){
166409 rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
166410 }
166411 }
166412 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
166413
166414 /* Free the position-lists accumulated for each deferred token above. */
166415 sqlite3Fts3FreeDeferredDoclists(pCsr);
166416 *pRc = rc;
166417 }
166418 return (rc==SQLITE_OK0 && bMiss);
166419}
166420
166421/*
166422** Advance to the next document that matches the FTS expression in
166423** Fts3Cursor.pExpr.
166424*/
166425static int fts3EvalNext(Fts3Cursor *pCsr){
166426 int rc = SQLITE_OK0; /* Return Code */
166427 Fts3Expr *pExpr = pCsr->pExpr;
166428 assert( pCsr->isEof==0 )((void) (0));
166429 if( pExpr==0 ){
166430 pCsr->isEof = 1;
166431 }else{
166432 do {
166433 if( pCsr->isRequireSeek==0 ){
166434 sqlite3_reset(pCsr->pStmt);
166435 }
166436 assert( sqlite3_data_count(pCsr->pStmt)==0 )((void) (0));
166437 fts3EvalNextRow(pCsr, pExpr, &rc);
166438 pCsr->isEof = pExpr->bEof;
166439 pCsr->isRequireSeek = 1;
166440 pCsr->isMatchinfoNeeded = 1;
166441 pCsr->iPrevId = pExpr->iDocid;
166442 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
166443 }
166444
166445 /* Check if the cursor is past the end of the docid range specified
166446 ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
166447 if( rc==SQLITE_OK0 && (
166448 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
166449 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
166450 )){
166451 pCsr->isEof = 1;
166452 }
166453
166454 return rc;
166455}
166456
166457/*
166458** Restart interation for expression pExpr so that the next call to
166459** fts3EvalNext() visits the first row. Do not allow incremental
166460** loading or merging of phrase doclists for this iteration.
166461**
166462** If *pRc is other than SQLITE_OK when this function is called, it is
166463** a no-op. If an error occurs within this function, *pRc is set to an
166464** SQLite error code before returning.
166465*/
166466static void fts3EvalRestart(
166467 Fts3Cursor *pCsr,
166468 Fts3Expr *pExpr,
166469 int *pRc
166470){
166471 if( pExpr && *pRc==SQLITE_OK0 ){
166472 Fts3Phrase *pPhrase = pExpr->pPhrase;
166473
166474 if( pPhrase ){
166475 fts3EvalInvalidatePoslist(pPhrase);
166476 if( pPhrase->bIncr ){
166477 int i;
166478 for(i=0; i<pPhrase->nToken; i++){
166479 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
166480 assert( pToken->pDeferred==0 )((void) (0));
166481 if( pToken->pSegcsr ){
166482 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
166483 }
166484 }
166485 *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
166486 }
166487 pPhrase->doclist.pNextDocid = 0;
166488 pPhrase->doclist.iDocid = 0;
166489 pPhrase->pOrPoslist = 0;
166490 }
166491
166492 pExpr->iDocid = 0;
166493 pExpr->bEof = 0;
166494 pExpr->bStart = 0;
166495
166496 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
166497 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
166498 }
166499}
166500
166501/*
166502** After allocating the Fts3Expr.aMI[] array for each phrase in the
166503** expression rooted at pExpr, the cursor iterates through all rows matched
166504** by pExpr, calling this function for each row. This function increments
166505** the values in Fts3Expr.aMI[] according to the position-list currently
166506** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
166507** expression nodes.
166508*/
166509static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
166510 if( pExpr ){
166511 Fts3Phrase *pPhrase = pExpr->pPhrase;
166512 if( pPhrase && pPhrase->doclist.pList ){
166513 int iCol = 0;
166514 char *p = pPhrase->doclist.pList;
166515
166516 do{
166517 u8 c = 0;
166518 int iCnt = 0;
166519 while( 0xFE & (*p | c) ){
166520 if( (c&0x80)==0 ) iCnt++;
166521 c = *p++ & 0x80;
166522 }
166523
166524 /* aMI[iCol*3 + 1] = Number of occurrences
166525 ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
166526 */
166527 pExpr->aMI[iCol*3 + 1] += iCnt;
166528 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
166529 if( *p==0x00 ) break;
166530 p++;
166531 p += fts3GetVarint32(p, &iCol);
166532 }while( iCol<nCol );
166533 }
166534
166535 fts3EvalUpdateCounts(pExpr->pLeft, nCol);
166536 fts3EvalUpdateCounts(pExpr->pRight, nCol);
166537 }
166538}
166539
166540/*
166541** Expression pExpr must be of type FTSQUERY_PHRASE.
166542**
166543** If it is not already allocated and populated, this function allocates and
166544** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
166545** of a NEAR expression, then it also allocates and populates the same array
166546** for all other phrases that are part of the NEAR expression.
166547**
166548** SQLITE_OK is returned if the aMI[] array is successfully allocated and
166549** populated. Otherwise, if an error occurs, an SQLite error code is returned.
166550*/
166551static int fts3EvalGatherStats(
166552 Fts3Cursor *pCsr, /* Cursor object */
166553 Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
166554){
166555 int rc = SQLITE_OK0; /* Return code */
166556
166557 assert( pExpr->eType==FTSQUERY_PHRASE )((void) (0));
166558 if( pExpr->aMI==0 ){
166559 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
166560 Fts3Expr *pRoot; /* Root of NEAR expression */
166561 Fts3Expr *p; /* Iterator used for several purposes */
166562
166563 sqlite3_int64 iPrevId = pCsr->iPrevId;
166564 sqlite3_int64 iDocid;
166565 u8 bEof;
166566
166567 /* Find the root of the NEAR expression */
166568 pRoot = pExpr;
166569 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
166570 pRoot = pRoot->pParent;
166571 }
166572 iDocid = pRoot->iDocid;
166573 bEof = pRoot->bEof;
166574 assert( pRoot->bStart )((void) (0));
166575
166576 /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
166577 for(p=pRoot; p; p=p->pLeft){
166578 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
166579 assert( pE->aMI==0 )((void) (0));
166580 pE->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32));
166581 if( !pE->aMI ) return SQLITE_NOMEM7;
166582 memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
166583 }
166584
166585 fts3EvalRestart(pCsr, pRoot, &rc);
166586
166587 while( pCsr->isEof==0 && rc==SQLITE_OK0 ){
166588
166589 do {
166590 /* Ensure the %_content statement is reset. */
166591 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
166592 assert( sqlite3_data_count(pCsr->pStmt)==0 )((void) (0));
166593
166594 /* Advance to the next document */
166595 fts3EvalNextRow(pCsr, pRoot, &rc);
166596 pCsr->isEof = pRoot->bEof;
166597 pCsr->isRequireSeek = 1;
166598 pCsr->isMatchinfoNeeded = 1;
166599 pCsr->iPrevId = pRoot->iDocid;
166600 }while( pCsr->isEof==0
166601 && pRoot->eType==FTSQUERY_NEAR
166602 && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
166603 );
166604
166605 if( rc==SQLITE_OK0 && pCsr->isEof==0 ){
166606 fts3EvalUpdateCounts(pRoot, pTab->nColumn);
166607 }
166608 }
166609
166610 pCsr->isEof = 0;
166611 pCsr->iPrevId = iPrevId;
166612
166613 if( bEof ){
166614 pRoot->bEof = bEof;
166615 }else{
166616 /* Caution: pRoot may iterate through docids in ascending or descending
166617 ** order. For this reason, even though it seems more defensive, the
166618 ** do loop can not be written:
166619 **
166620 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
166621 */
166622 fts3EvalRestart(pCsr, pRoot, &rc);
166623 do {
166624 fts3EvalNextRow(pCsr, pRoot, &rc);
166625 assert( pRoot->bEof==0 )((void) (0));
166626 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK0 );
166627 }
166628 }
166629 return rc;
166630}
166631
166632/*
166633** This function is used by the matchinfo() module to query a phrase
166634** expression node for the following information:
166635**
166636** 1. The total number of occurrences of the phrase in each column of
166637** the FTS table (considering all rows), and
166638**
166639** 2. For each column, the number of rows in the table for which the
166640** column contains at least one instance of the phrase.
166641**
166642** If no error occurs, SQLITE_OK is returned and the values for each column
166643** written into the array aiOut as follows:
166644**
166645** aiOut[iCol*3 + 1] = Number of occurrences
166646** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
166647**
166648** Caveats:
166649**
166650** * If a phrase consists entirely of deferred tokens, then all output
166651** values are set to the number of documents in the table. In other
166652** words we assume that very common tokens occur exactly once in each
166653** column of each row of the table.
166654**
166655** * If a phrase contains some deferred tokens (and some non-deferred
166656** tokens), count the potential occurrence identified by considering
166657** the non-deferred tokens instead of actual phrase occurrences.
166658**
166659** * If the phrase is part of a NEAR expression, then only phrase instances
166660** that meet the NEAR constraint are included in the counts.
166661*/
166662SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhraseStats(
166663 Fts3Cursor *pCsr, /* FTS cursor handle */
166664 Fts3Expr *pExpr, /* Phrase expression */
166665 u32 *aiOut /* Array to write results into (see above) */
166666){
166667 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
166668 int rc = SQLITE_OK0;
166669 int iCol;
166670
166671 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
166672 assert( pCsr->nDoc>0 )((void) (0));
166673 for(iCol=0; iCol<pTab->nColumn; iCol++){
166674 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
166675 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
166676 }
166677 }else{
166678 rc = fts3EvalGatherStats(pCsr, pExpr);
166679 if( rc==SQLITE_OK0 ){
166680 assert( pExpr->aMI )((void) (0));
166681 for(iCol=0; iCol<pTab->nColumn; iCol++){
166682 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
166683 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
166684 }
166685 }
166686 }
166687
166688 return rc;
166689}
166690
166691/*
166692** The expression pExpr passed as the second argument to this function
166693** must be of type FTSQUERY_PHRASE.
166694**
166695** The returned value is either NULL or a pointer to a buffer containing
166696** a position-list indicating the occurrences of the phrase in column iCol
166697** of the current row.
166698**
166699** More specifically, the returned buffer contains 1 varint for each
166700** occurrence of the phrase in the column, stored using the normal (delta+2)
166701** compression and is terminated by either an 0x01 or 0x00 byte. For example,
166702** if the requested column contains "a b X c d X X" and the position-list
166703** for 'X' is requested, the buffer returned may contain:
166704**
166705** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
166706**
166707** This function works regardless of whether or not the phrase is deferred,
166708** incremental, or neither.
166709*/
166710SQLITE_PRIVATEstatic int sqlite3Fts3EvalPhrasePoslist(
166711 Fts3Cursor *pCsr, /* FTS3 cursor object */
166712 Fts3Expr *pExpr, /* Phrase to return doclist for */
166713 int iCol, /* Column to return position list for */
166714 char **ppOut /* OUT: Pointer to position list */
166715){
166716 Fts3Phrase *pPhrase = pExpr->pPhrase;
166717 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
166718 char *pIter;
166719 int iThis;
166720 sqlite3_int64 iDocid;
166721
166722 /* If this phrase is applies specifically to some column other than
166723 ** column iCol, return a NULL pointer. */
166724 *ppOut = 0;
166725 assert( iCol>=0 && iCol<pTab->nColumn )((void) (0));
166726 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
166727 return SQLITE_OK0;
166728 }
166729
166730 iDocid = pExpr->iDocid;
166731 pIter = pPhrase->doclist.pList;
166732 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
166733 int rc = SQLITE_OK0;
166734 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
166735 int bOr = 0;
166736 u8 bTreeEof = 0;
166737 Fts3Expr *p; /* Used to iterate from pExpr to root */
166738 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
166739 int bMatch;
166740
166741 /* Check if this phrase descends from an OR expression node. If not,
166742 ** return NULL. Otherwise, the entry that corresponds to docid
166743 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
166744 ** tree that the node is part of has been marked as EOF, but the node
166745 ** itself is not EOF, then it may point to an earlier entry. */
166746 pNear = pExpr;
166747 for(p=pExpr->pParent; p; p=p->pParent){
166748 if( p->eType==FTSQUERY_OR ) bOr = 1;
166749 if( p->eType==FTSQUERY_NEAR ) pNear = p;
166750 if( p->bEof ) bTreeEof = 1;
166751 }
166752 if( bOr==0 ) return SQLITE_OK0;
166753
166754 /* This is the descendent of an OR node. In this case we cannot use
166755 ** an incremental phrase. Load the entire doclist for the phrase
166756 ** into memory in this case. */
166757 if( pPhrase->bIncr ){
166758 int bEofSave = pNear->bEof;
166759 fts3EvalRestart(pCsr, pNear, &rc);
166760 while( rc==SQLITE_OK0 && !pNear->bEof ){
166761 fts3EvalNextRow(pCsr, pNear, &rc);
166762 if( bEofSave==0 && pNear->iDocid==iDocid ) break;
166763 }
166764 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 )((void) (0));
166765 }
166766 if( bTreeEof ){
166767 while( rc==SQLITE_OK0 && !pNear->bEof ){
166768 fts3EvalNextRow(pCsr, pNear, &rc);
166769 }
166770 }
166771 if( rc!=SQLITE_OK0 ) return rc;
166772
166773 bMatch = 1;
166774 for(p=pNear; p; p=p->pLeft){
166775 u8 bEof = 0;
166776 Fts3Expr *pTest = p;
166777 Fts3Phrase *pPh;
166778 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE )((void) (0));
166779 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
166780 assert( pTest->eType==FTSQUERY_PHRASE )((void) (0));
166781 pPh = pTest->pPhrase;
166782
166783 pIter = pPh->pOrPoslist;
166784 iDocid = pPh->iOrDocid;
166785 if( pCsr->bDesc==bDescDoclist ){
166786 bEof = !pPh->doclist.nAll ||
166787 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
166788 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
166789 sqlite3Fts3DoclistNext(
166790 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
166791 &pIter, &iDocid, &bEof
166792 );
166793 }
166794 }else{
166795 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
166796 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
166797 int dummy;
166798 sqlite3Fts3DoclistPrev(
166799 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
166800 &pIter, &iDocid, &dummy, &bEof
166801 );
166802 }
166803 }
166804 pPh->pOrPoslist = pIter;
166805 pPh->iOrDocid = iDocid;
166806 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
166807 }
166808
166809 if( bMatch ){
166810 pIter = pPhrase->pOrPoslist;
166811 }else{
166812 pIter = 0;
166813 }
166814 }
166815 if( pIter==0 ) return SQLITE_OK0;
166816
166817 if( *pIter==0x01 ){
166818 pIter++;
166819 pIter += fts3GetVarint32(pIter, &iThis);
166820 }else{
166821 iThis = 0;
166822 }
166823 while( iThis<iCol ){
166824 fts3ColumnlistCopy(0, &pIter);
166825 if( *pIter==0x00 ) return SQLITE_OK0;
166826 pIter++;
166827 pIter += fts3GetVarint32(pIter, &iThis);
166828 }
166829 if( *pIter==0x00 ){
166830 pIter = 0;
166831 }
166832
166833 *ppOut = ((iCol==iThis)?pIter:0);
166834 return SQLITE_OK0;
166835}
166836
166837/*
166838** Free all components of the Fts3Phrase structure that were allocated by
166839** the eval module. Specifically, this means to free:
166840**
166841** * the contents of pPhrase->doclist, and
166842** * any Fts3MultiSegReader objects held by phrase tokens.
166843*/
166844SQLITE_PRIVATEstatic void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
166845 if( pPhrase ){
166846 int i;
166847 sqlite3_free(pPhrase->doclist.aAll);
166848 fts3EvalInvalidatePoslist(pPhrase);
166849 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
166850 for(i=0; i<pPhrase->nToken; i++){
166851 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
166852 pPhrase->aToken[i].pSegcsr = 0;
166853 }
166854 }
166855}
166856
166857
166858/*
166859** Return SQLITE_CORRUPT_VTAB.
166860*/
166861#ifdef SQLITE_DEBUG
166862SQLITE_PRIVATEstatic int sqlite3Fts3Corrupt(){
166863 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
166864}
166865#endif
166866
166867#if !SQLITE_CORE1
166868/*
166869** Initialize API pointer table, if required.
166870*/
166871#ifdef _WIN32
166872__declspec(dllexport)
166873#endif
166874SQLITE_API int sqlite3_fts3_init(
166875 sqlite3 *db,
166876 char **pzErrMsg,
166877 const sqlite3_api_routines *pApi
166878){
166879 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
166880 return sqlite3Fts3Init(db);
166881}
166882#endif
166883
166884#endif
166885
166886/************** End of fts3.c ************************************************/
166887/************** Begin file fts3_aux.c ****************************************/
166888/*
166889** 2011 Jan 27
166890**
166891** The author disclaims copyright to this source code. In place of
166892** a legal notice, here is a blessing:
166893**
166894** May you do good and not evil.
166895** May you find forgiveness for yourself and forgive others.
166896** May you share freely, never taking more than you give.
166897**
166898******************************************************************************
166899**
166900*/
166901/* #include "fts3Int.h" */
166902#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
166903
166904/* #include <string.h> */
166905/* #include <assert.h> */
166906
166907typedef struct Fts3auxTable Fts3auxTable;
166908typedef struct Fts3auxCursor Fts3auxCursor;
166909
166910struct Fts3auxTable {
166911 sqlite3_vtab base; /* Base class used by SQLite core */
166912 Fts3Table *pFts3Tab;
166913};
166914
166915struct Fts3auxCursor {
166916 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
166917 Fts3MultiSegReader csr; /* Must be right after "base" */
166918 Fts3SegFilter filter;
166919 char *zStop;
166920 int nStop; /* Byte-length of string zStop */
166921 int iLangid; /* Language id to query */
166922 int isEof; /* True if cursor is at EOF */
166923 sqlite3_int64 iRowid; /* Current rowid */
166924
166925 int iCol; /* Current value of 'col' column */
166926 int nStat; /* Size of aStat[] array */
166927 struct Fts3auxColstats {
166928 sqlite3_int64 nDoc; /* 'documents' values for current csr row */
166929 sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */
166930 } *aStat;
166931};
166932
166933/*
166934** Schema of the terms table.
166935*/
166936#define FTS3_AUX_SCHEMA \
166937 "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
166938
166939/*
166940** This function does all the work for both the xConnect and xCreate methods.
166941** These tables have no persistent representation of their own, so xConnect
166942** and xCreate are identical operations.
166943*/
166944static int fts3auxConnectMethod(
166945 sqlite3 *db, /* Database connection */
166946 void *pUnused, /* Unused */
166947 int argc, /* Number of elements in argv array */
166948 const char * const *argv, /* xCreate/xConnect argument array */
166949 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
166950 char **pzErr /* OUT: sqlite3_malloc'd error message */
166951){
166952 char const *zDb; /* Name of database (e.g. "main") */
166953 char const *zFts3; /* Name of fts3 table */
166954 int nDb; /* Result of strlen(zDb) */
166955 int nFts3; /* Result of strlen(zFts3) */
166956 sqlite3_int64 nByte; /* Bytes of space to allocate here */
166957 int rc; /* value returned by declare_vtab() */
166958 Fts3auxTable *p; /* Virtual table object to return */
166959
166960 UNUSED_PARAMETER(pUnused)(void)(pUnused);
166961
166962 /* The user should invoke this in one of two forms:
166963 **
166964 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
166965 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
166966 */
166967 if( argc!=4 && argc!=5 ) goto bad_args;
166968
166969 zDb = argv[1];
166970 nDb = (int)strlen(zDb);
166971 if( argc==5 ){
166972 if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
166973 zDb = argv[3];
166974 nDb = (int)strlen(zDb);
166975 zFts3 = argv[4];
166976 }else{
166977 goto bad_args;
166978 }
166979 }else{
166980 zFts3 = argv[3];
166981 }
166982 nFts3 = (int)strlen(zFts3);
166983
166984 rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
166985 if( rc!=SQLITE_OK0 ) return rc;
166986
166987 nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
166988 p = (Fts3auxTable *)sqlite3_malloc64(nByte);
166989 if( !p ) return SQLITE_NOMEM7;
166990 memset(p, 0, nByte);
166991
166992 p->pFts3Tab = (Fts3Table *)&p[1];
166993 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
166994 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
166995 p->pFts3Tab->db = db;
166996 p->pFts3Tab->nIndex = 1;
166997
166998 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
166999 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
167000 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
167001
167002 *ppVtab = (sqlite3_vtab *)p;
167003 return SQLITE_OK0;
167004
167005 bad_args:
167006 sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor");
167007 return SQLITE_ERROR1;
167008}
167009
167010/*
167011** This function does the work for both the xDisconnect and xDestroy methods.
167012** These tables have no persistent representation of their own, so xDisconnect
167013** and xDestroy are identical operations.
167014*/
167015static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
167016 Fts3auxTable *p = (Fts3auxTable *)pVtab;
167017 Fts3Table *pFts3 = p->pFts3Tab;
167018 int i;
167019
167020 /* Free any prepared statements held */
167021 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
167022 sqlite3_finalize(pFts3->aStmt[i]);
167023 }
167024 sqlite3_free(pFts3->zSegmentsTbl);
167025 sqlite3_free(p);
167026 return SQLITE_OK0;
167027}
167028
167029#define FTS4AUX_EQ_CONSTRAINT 1
167030#define FTS4AUX_GE_CONSTRAINT 2
167031#define FTS4AUX_LE_CONSTRAINT 4
167032
167033/*
167034** xBestIndex - Analyze a WHERE and ORDER BY clause.
167035*/
167036static int fts3auxBestIndexMethod(
167037 sqlite3_vtab *pVTab,
167038 sqlite3_index_info *pInfo
167039){
167040 int i;
167041 int iEq = -1;
167042 int iGe = -1;
167043 int iLe = -1;
167044 int iLangid = -1;
167045 int iNext = 1; /* Next free argvIndex value */
167046
167047 UNUSED_PARAMETER(pVTab)(void)(pVTab);
167048
167049 /* This vtab delivers always results in "ORDER BY term ASC" order. */
167050 if( pInfo->nOrderBy==1
167051 && pInfo->aOrderBy[0].iColumn==0
167052 && pInfo->aOrderBy[0].desc==0
167053 ){
167054 pInfo->orderByConsumed = 1;
167055 }
167056
167057 /* Search for equality and range constraints on the "term" column.
167058 ** And equality constraints on the hidden "languageid" column. */
167059 for(i=0; i<pInfo->nConstraint; i++){
167060 if( pInfo->aConstraint[i].usable ){
167061 int op = pInfo->aConstraint[i].op;
167062 int iCol = pInfo->aConstraint[i].iColumn;
167063
167064 if( iCol==0 ){
167065 if( op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iEq = i;
167066 if( op==SQLITE_INDEX_CONSTRAINT_LT16 ) iLe = i;
167067 if( op==SQLITE_INDEX_CONSTRAINT_LE8 ) iLe = i;
167068 if( op==SQLITE_INDEX_CONSTRAINT_GT4 ) iGe = i;
167069 if( op==SQLITE_INDEX_CONSTRAINT_GE32 ) iGe = i;
167070 }
167071 if( iCol==4 ){
167072 if( op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iLangid = i;
167073 }
167074 }
167075 }
167076
167077 if( iEq>=0 ){
167078 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
167079 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
167080 pInfo->estimatedCost = 5;
167081 }else{
167082 pInfo->idxNum = 0;
167083 pInfo->estimatedCost = 20000;
167084 if( iGe>=0 ){
167085 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
167086 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
167087 pInfo->estimatedCost /= 2;
167088 }
167089 if( iLe>=0 ){
167090 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
167091 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
167092 pInfo->estimatedCost /= 2;
167093 }
167094 }
167095 if( iLangid>=0 ){
167096 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
167097 pInfo->estimatedCost--;
167098 }
167099
167100 return SQLITE_OK0;
167101}
167102
167103/*
167104** xOpen - Open a cursor.
167105*/
167106static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
167107 Fts3auxCursor *pCsr; /* Pointer to cursor object to return */
167108
167109 UNUSED_PARAMETER(pVTab)(void)(pVTab);
167110
167111 pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
167112 if( !pCsr ) return SQLITE_NOMEM7;
167113 memset(pCsr, 0, sizeof(Fts3auxCursor));
167114
167115 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
167116 return SQLITE_OK0;
167117}
167118
167119/*
167120** xClose - Close a cursor.
167121*/
167122static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
167123 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
167124 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
167125
167126 sqlite3Fts3SegmentsClose(pFts3);
167127 sqlite3Fts3SegReaderFinish(&pCsr->csr);
167128 sqlite3_free((void *)pCsr->filter.zTerm);
167129 sqlite3_free(pCsr->zStop);
167130 sqlite3_free(pCsr->aStat);
167131 sqlite3_free(pCsr);
167132 return SQLITE_OK0;
167133}
167134
167135static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
167136 if( nSize>pCsr->nStat ){
167137 struct Fts3auxColstats *aNew;
167138 aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat,
167139 sizeof(struct Fts3auxColstats) * nSize
167140 );
167141 if( aNew==0 ) return SQLITE_NOMEM7;
167142 memset(&aNew[pCsr->nStat], 0,
167143 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
167144 );
167145 pCsr->aStat = aNew;
167146 pCsr->nStat = nSize;
167147 }
167148 return SQLITE_OK0;
167149}
167150
167151/*
167152** xNext - Advance the cursor to the next row, if any.
167153*/
167154static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
167155 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
167156 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
167157 int rc;
167158
167159 /* Increment our pretend rowid value. */
167160 pCsr->iRowid++;
167161
167162 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
167163 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK0;
167164 }
167165
167166 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
167167 if( rc==SQLITE_ROW100 ){
167168 int i = 0;
167169 int nDoclist = pCsr->csr.nDoclist;
167170 char *aDoclist = pCsr->csr.aDoclist;
167171 int iCol;
167172
167173 int eState = 0;
167174
167175 if( pCsr->zStop ){
167176 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
167177 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
167178 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
167179 pCsr->isEof = 1;
167180 return SQLITE_OK0;
167181 }
167182 }
167183
167184 if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM7;
167185 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
167186 iCol = 0;
167187
167188 while( i<nDoclist ){
167189 sqlite3_int64 v = 0;
167190
167191 i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
167192 switch( eState ){
167193 /* State 0. In this state the integer just read was a docid. */
167194 case 0:
167195 pCsr->aStat[0].nDoc++;
167196 eState = 1;
167197 iCol = 0;
167198 break;
167199
167200 /* State 1. In this state we are expecting either a 1, indicating
167201 ** that the following integer will be a column number, or the
167202 ** start of a position list for column 0.
167203 **
167204 ** The only difference between state 1 and state 2 is that if the
167205 ** integer encountered in state 1 is not 0 or 1, then we need to
167206 ** increment the column 0 "nDoc" count for this term.
167207 */
167208 case 1:
167209 assert( iCol==0 )((void) (0));
167210 if( v>1 ){
167211 pCsr->aStat[1].nDoc++;
167212 }
167213 eState = 2;
167214 /* fall through */
167215
167216 case 2:
167217 if( v==0 ){ /* 0x00. Next integer will be a docid. */
167218 eState = 0;
167219 }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
167220 eState = 3;
167221 }else{ /* 2 or greater. A position. */
167222 pCsr->aStat[iCol+1].nOcc++;
167223 pCsr->aStat[0].nOcc++;
167224 }
167225 break;
167226
167227 /* State 3. The integer just read is a column number. */
167228 default: assert( eState==3 )((void) (0));
167229 iCol = (int)v;
167230 if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM7;
167231 pCsr->aStat[iCol+1].nDoc++;
167232 eState = 2;
167233 break;
167234 }
167235 }
167236
167237 pCsr->iCol = 0;
167238 rc = SQLITE_OK0;
167239 }else{
167240 pCsr->isEof = 1;
167241 }
167242 return rc;
167243}
167244
167245/*
167246** xFilter - Initialize a cursor to point at the start of its data.
167247*/
167248static int fts3auxFilterMethod(
167249 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
167250 int idxNum, /* Strategy index */
167251 const char *idxStr, /* Unused */
167252 int nVal, /* Number of elements in apVal */
167253 sqlite3_value **apVal /* Arguments for the indexing scheme */
167254){
167255 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
167256 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
167257 int rc;
167258 int isScan = 0;
167259 int iLangVal = 0; /* Language id to query */
167260
167261 int iEq = -1; /* Index of term=? value in apVal */
167262 int iGe = -1; /* Index of term>=? value in apVal */
167263 int iLe = -1; /* Index of term<=? value in apVal */
167264 int iLangid = -1; /* Index of languageid=? value in apVal */
167265 int iNext = 0;
167266
167267 UNUSED_PARAMETER(nVal)(void)(nVal);
167268 UNUSED_PARAMETER(idxStr)(void)(idxStr);
167269
167270 assert( idxStr==0 )((void) (0));
167271 assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0((void) (0))
167272 || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT((void) (0))
167273 || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)((void) (0))
167274 )((void) (0));
167275
167276 if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
167277 iEq = iNext++;
167278 }else{
167279 isScan = 1;
167280 if( idxNum & FTS4AUX_GE_CONSTRAINT ){
167281 iGe = iNext++;
167282 }
167283 if( idxNum & FTS4AUX_LE_CONSTRAINT ){
167284 iLe = iNext++;
167285 }
167286 }
167287 if( iNext<nVal ){
167288 iLangid = iNext++;
167289 }
167290
167291 /* In case this cursor is being reused, close and zero it. */
167292 testcase(pCsr->filter.zTerm);
167293 sqlite3Fts3SegReaderFinish(&pCsr->csr);
167294 sqlite3_free((void *)pCsr->filter.zTerm);
167295 sqlite3_free(pCsr->aStat);
167296 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
167297
167298 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
167299 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
167300
167301 if( iEq>=0 || iGe>=0 ){
167302 const unsigned char *zStr = sqlite3_value_text(apVal[0]);
167303 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) )((void) (0));
167304 if( zStr ){
167305 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
167306 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM7;
167307 pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm);
167308 }
167309 }
167310
167311 if( iLe>=0 ){
167312 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
167313 if( pCsr->zStop==0 ) return SQLITE_NOMEM7;
167314 pCsr->nStop = (int)strlen(pCsr->zStop);
167315 }
167316
167317 if( iLangid>=0 ){
167318 iLangVal = sqlite3_value_int(apVal[iLangid]);
167319
167320 /* If the user specified a negative value for the languageid, use zero
167321 ** instead. This works, as the "languageid=?" constraint will also
167322 ** be tested by the VDBE layer. The test will always be false (since
167323 ** this module will not return a row with a negative languageid), and
167324 ** so the overall query will return zero rows. */
167325 if( iLangVal<0 ) iLangVal = 0;
167326 }
167327 pCsr->iLangid = iLangVal;
167328
167329 rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
167330 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
167331 );
167332 if( rc==SQLITE_OK0 ){
167333 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
167334 }
167335
167336 if( rc==SQLITE_OK0 ) rc = fts3auxNextMethod(pCursor);
167337 return rc;
167338}
167339
167340/*
167341** xEof - Return true if the cursor is at EOF, or false otherwise.
167342*/
167343static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
167344 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
167345 return pCsr->isEof;
167346}
167347
167348/*
167349** xColumn - Return a column value.
167350*/
167351static int fts3auxColumnMethod(
167352 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
167353 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
167354 int iCol /* Index of column to read value from */
167355){
167356 Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
167357
167358 assert( p->isEof==0 )((void) (0));
167359 switch( iCol ){
167360 case 0: /* term */
167361 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
167362 break;
167363
167364 case 1: /* col */
167365 if( p->iCol ){
167366 sqlite3_result_int(pCtx, p->iCol-1);
167367 }else{
167368 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
167369 }
167370 break;
167371
167372 case 2: /* documents */
167373 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
167374 break;
167375
167376 case 3: /* occurrences */
167377 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
167378 break;
167379
167380 default: /* languageid */
167381 assert( iCol==4 )((void) (0));
167382 sqlite3_result_int(pCtx, p->iLangid);
167383 break;
167384 }
167385
167386 return SQLITE_OK0;
167387}
167388
167389/*
167390** xRowid - Return the current rowid for the cursor.
167391*/
167392static int fts3auxRowidMethod(
167393 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
167394 sqlite_int64 *pRowid /* OUT: Rowid value */
167395){
167396 Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
167397 *pRowid = pCsr->iRowid;
167398 return SQLITE_OK0;
167399}
167400
167401/*
167402** Register the fts3aux module with database connection db. Return SQLITE_OK
167403** if successful or an error code if sqlite3_create_module() fails.
167404*/
167405SQLITE_PRIVATEstatic int sqlite3Fts3InitAux(sqlite3 *db){
167406 static const sqlite3_module fts3aux_module = {
167407 0, /* iVersion */
167408 fts3auxConnectMethod, /* xCreate */
167409 fts3auxConnectMethod, /* xConnect */
167410 fts3auxBestIndexMethod, /* xBestIndex */
167411 fts3auxDisconnectMethod, /* xDisconnect */
167412 fts3auxDisconnectMethod, /* xDestroy */
167413 fts3auxOpenMethod, /* xOpen */
167414 fts3auxCloseMethod, /* xClose */
167415 fts3auxFilterMethod, /* xFilter */
167416 fts3auxNextMethod, /* xNext */
167417 fts3auxEofMethod, /* xEof */
167418 fts3auxColumnMethod, /* xColumn */
167419 fts3auxRowidMethod, /* xRowid */
167420 0, /* xUpdate */
167421 0, /* xBegin */
167422 0, /* xSync */
167423 0, /* xCommit */
167424 0, /* xRollback */
167425 0, /* xFindFunction */
167426 0, /* xRename */
167427 0, /* xSavepoint */
167428 0, /* xRelease */
167429 0, /* xRollbackTo */
167430 0 /* xShadowName */
167431 };
167432 int rc; /* Return code */
167433
167434 rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
167435 return rc;
167436}
167437
167438#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
167439
167440/************** End of fts3_aux.c ********************************************/
167441/************** Begin file fts3_expr.c ***************************************/
167442/*
167443** 2008 Nov 28
167444**
167445** The author disclaims copyright to this source code. In place of
167446** a legal notice, here is a blessing:
167447**
167448** May you do good and not evil.
167449** May you find forgiveness for yourself and forgive others.
167450** May you share freely, never taking more than you give.
167451**
167452******************************************************************************
167453**
167454** This module contains code that implements a parser for fts3 query strings
167455** (the right-hand argument to the MATCH operator). Because the supported
167456** syntax is relatively simple, the whole tokenizer/parser system is
167457** hand-coded.
167458*/
167459/* #include "fts3Int.h" */
167460#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
167461
167462/*
167463** By default, this module parses the legacy syntax that has been
167464** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
167465** is defined, then it uses the new syntax. The differences between
167466** the new and the old syntaxes are:
167467**
167468** a) The new syntax supports parenthesis. The old does not.
167469**
167470** b) The new syntax supports the AND and NOT operators. The old does not.
167471**
167472** c) The old syntax supports the "-" token qualifier. This is not
167473** supported by the new syntax (it is replaced by the NOT operator).
167474**
167475** d) When using the old syntax, the OR operator has a greater precedence
167476** than an implicit AND. When using the new, both implicity and explicit
167477** AND operators have a higher precedence than OR.
167478**
167479** If compiled with SQLITE_TEST defined, then this module exports the
167480** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
167481** to zero causes the module to use the old syntax. If it is set to
167482** non-zero the new syntax is activated. This is so both syntaxes can
167483** be tested using a single build of testfixture.
167484**
167485** The following describes the syntax supported by the fts3 MATCH
167486** operator in a similar format to that used by the lemon parser
167487** generator. This module does not use actually lemon, it uses a
167488** custom parser.
167489**
167490** query ::= andexpr (OR andexpr)*.
167491**
167492** andexpr ::= notexpr (AND? notexpr)*.
167493**
167494** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
167495** notexpr ::= LP query RP.
167496**
167497** nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
167498**
167499** distance_opt ::= .
167500** distance_opt ::= / INTEGER.
167501**
167502** phrase ::= TOKEN.
167503** phrase ::= COLUMN:TOKEN.
167504** phrase ::= "TOKEN TOKEN TOKEN...".
167505*/
167506
167507#ifdef SQLITE_TEST
167508SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
167509#else
167510# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
167511# define sqlite3_fts3_enable_parentheses 1
167512# else
167513# define sqlite3_fts3_enable_parentheses 0
167514# endif
167515#endif
167516
167517/*
167518** Default span for NEAR operators.
167519*/
167520#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
167521
167522/* #include <string.h> */
167523/* #include <assert.h> */
167524
167525/*
167526** isNot:
167527** This variable is used by function getNextNode(). When getNextNode() is
167528** called, it sets ParseContext.isNot to true if the 'next node' is a
167529** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
167530** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
167531** zero.
167532*/
167533typedef struct ParseContext ParseContext;
167534struct ParseContext {
167535 sqlite3_tokenizer *pTokenizer; /* Tokenizer module */
167536 int iLangid; /* Language id used with tokenizer */
167537 const char **azCol; /* Array of column names for fts3 table */
167538 int bFts4; /* True to allow FTS4-only syntax */
167539 int nCol; /* Number of entries in azCol[] */
167540 int iDefaultCol; /* Default column to query */
167541 int isNot; /* True if getNextNode() sees a unary - */
167542 sqlite3_context *pCtx; /* Write error message here */
167543 int nNest; /* Number of nested brackets */
167544};
167545
167546/*
167547** This function is equivalent to the standard isspace() function.
167548**
167549** The standard isspace() can be awkward to use safely, because although it
167550** is defined to accept an argument of type int, its behavior when passed
167551** an integer that falls outside of the range of the unsigned char type
167552** is undefined (and sometimes, "undefined" means segfault). This wrapper
167553** is defined to accept an argument of type char, and always returns 0 for
167554** any values that fall outside of the range of the unsigned char type (i.e.
167555** negative values).
167556*/
167557static int fts3isspace(char c){
167558 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
167559}
167560
167561/*
167562** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
167563** zero the memory before returning a pointer to it. If unsuccessful,
167564** return NULL.
167565*/
167566static void *fts3MallocZero(sqlite3_int64 nByte){
167567 void *pRet = sqlite3_malloc64(nByte);
167568 if( pRet ) memset(pRet, 0, nByte);
167569 return pRet;
167570}
167571
167572SQLITE_PRIVATEstatic int sqlite3Fts3OpenTokenizer(
167573 sqlite3_tokenizer *pTokenizer,
167574 int iLangid,
167575 const char *z,
167576 int n,
167577 sqlite3_tokenizer_cursor **ppCsr
167578){
167579 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
167580 sqlite3_tokenizer_cursor *pCsr = 0;
167581 int rc;
167582
167583 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
167584 assert( rc==SQLITE_OK || pCsr==0 )((void) (0));
167585 if( rc==SQLITE_OK0 ){
167586 pCsr->pTokenizer = pTokenizer;
167587 if( pModule->iVersion>=1 ){
167588 rc = pModule->xLanguageid(pCsr, iLangid);
167589 if( rc!=SQLITE_OK0 ){
167590 pModule->xClose(pCsr);
167591 pCsr = 0;
167592 }
167593 }
167594 }
167595 *ppCsr = pCsr;
167596 return rc;
167597}
167598
167599/*
167600** Function getNextNode(), which is called by fts3ExprParse(), may itself
167601** call fts3ExprParse(). So this forward declaration is required.
167602*/
167603static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
167604
167605/*
167606** Extract the next token from buffer z (length n) using the tokenizer
167607** and other information (column names etc.) in pParse. Create an Fts3Expr
167608** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
167609** single token and set *ppExpr to point to it. If the end of the buffer is
167610** reached before a token is found, set *ppExpr to zero. It is the
167611** responsibility of the caller to eventually deallocate the allocated
167612** Fts3Expr structure (if any) by passing it to sqlite3_free().
167613**
167614** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
167615** fails.
167616*/
167617static int getNextToken(
167618 ParseContext *pParse, /* fts3 query parse context */
167619 int iCol, /* Value for Fts3Phrase.iColumn */
167620 const char *z, int n, /* Input string */
167621 Fts3Expr **ppExpr, /* OUT: expression */
167622 int *pnConsumed /* OUT: Number of bytes consumed */
167623){
167624 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
167625 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
167626 int rc;
167627 sqlite3_tokenizer_cursor *pCursor;
167628 Fts3Expr *pRet = 0;
167629 int i = 0;
167630
167631 /* Set variable i to the maximum number of bytes of input to tokenize. */
167632 for(i=0; i<n; i++){
167633 if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
167634 if( z[i]=='"' ) break;
167635 }
167636
167637 *pnConsumed = i;
167638 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
167639 if( rc==SQLITE_OK0 ){
167640 const char *zToken;
167641 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
167642 sqlite3_int64 nByte; /* total space to allocate */
167643
167644 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
167645 if( rc==SQLITE_OK0 ){
167646 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
167647 pRet = (Fts3Expr *)fts3MallocZero(nByte);
167648 if( !pRet ){
167649 rc = SQLITE_NOMEM7;
167650 }else{
167651 pRet->eType = FTSQUERY_PHRASE;
167652 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
167653 pRet->pPhrase->nToken = 1;
167654 pRet->pPhrase->iColumn = iCol;
167655 pRet->pPhrase->aToken[0].n = nToken;
167656 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
167657 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
167658
167659 if( iEnd<n && z[iEnd]=='*' ){
167660 pRet->pPhrase->aToken[0].isPrefix = 1;
167661 iEnd++;
167662 }
167663
167664 while( 1 ){
167665 if( !sqlite3_fts3_enable_parentheses
167666 && iStart>0 && z[iStart-1]=='-'
167667 ){
167668 pParse->isNot = 1;
167669 iStart--;
167670 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
167671 pRet->pPhrase->aToken[0].bFirst = 1;
167672 iStart--;
167673 }else{
167674 break;
167675 }
167676 }
167677
167678 }
167679 *pnConsumed = iEnd;
167680 }else if( i && rc==SQLITE_DONE101 ){
167681 rc = SQLITE_OK0;
167682 }
167683
167684 pModule->xClose(pCursor);
167685 }
167686
167687 *ppExpr = pRet;
167688 return rc;
167689}
167690
167691
167692/*
167693** Enlarge a memory allocation. If an out-of-memory allocation occurs,
167694** then free the old allocation.
167695*/
167696static void *fts3ReallocOrFree(void *pOrig, sqlite3_int64 nNew){
167697 void *pRet = sqlite3_realloc64(pOrig, nNew);
167698 if( !pRet ){
167699 sqlite3_free(pOrig);
167700 }
167701 return pRet;
167702}
167703
167704/*
167705** Buffer zInput, length nInput, contains the contents of a quoted string
167706** that appeared as part of an fts3 query expression. Neither quote character
167707** is included in the buffer. This function attempts to tokenize the entire
167708** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
167709** containing the results.
167710**
167711** If successful, SQLITE_OK is returned and *ppExpr set to point at the
167712** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
167713** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
167714** to 0.
167715*/
167716static int getNextString(
167717 ParseContext *pParse, /* fts3 query parse context */
167718 const char *zInput, int nInput, /* Input string */
167719 Fts3Expr **ppExpr /* OUT: expression */
167720){
167721 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
167722 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
167723 int rc;
167724 Fts3Expr *p = 0;
167725 sqlite3_tokenizer_cursor *pCursor = 0;
167726 char *zTemp = 0;
167727 int nTemp = 0;
167728
167729 const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
167730 int nToken = 0;
167731
167732 /* The final Fts3Expr data structure, including the Fts3Phrase,
167733 ** Fts3PhraseToken structures token buffers are all stored as a single
167734 ** allocation so that the expression can be freed with a single call to
167735 ** sqlite3_free(). Setting this up requires a two pass approach.
167736 **
167737 ** The first pass, in the block below, uses a tokenizer cursor to iterate
167738 ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
167739 ** to assemble data in two dynamic buffers:
167740 **
167741 ** Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
167742 ** structure, followed by the array of Fts3PhraseToken
167743 ** structures. This pass only populates the Fts3PhraseToken array.
167744 **
167745 ** Buffer zTemp: Contains copies of all tokens.
167746 **
167747 ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
167748 ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
167749 ** structures.
167750 */
167751 rc = sqlite3Fts3OpenTokenizer(
167752 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
167753 if( rc==SQLITE_OK0 ){
167754 int ii;
167755 for(ii=0; rc==SQLITE_OK0; ii++){
167756 const char *zByte;
167757 int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
167758 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
167759 if( rc==SQLITE_OK0 ){
167760 Fts3PhraseToken *pToken;
167761
167762 p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
167763 if( !p ) goto no_mem;
167764
167765 zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
167766 if( !zTemp ) goto no_mem;
167767
167768 assert( nToken==ii )((void) (0));
167769 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
167770 memset(pToken, 0, sizeof(Fts3PhraseToken));
167771
167772 memcpy(&zTemp[nTemp], zByte, nByte);
167773 nTemp += nByte;
167774
167775 pToken->n = nByte;
167776 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
167777 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
167778 nToken = ii+1;
167779 }
167780 }
167781
167782 pModule->xClose(pCursor);
167783 pCursor = 0;
167784 }
167785
167786 if( rc==SQLITE_DONE101 ){
167787 int jj;
167788 char *zBuf = 0;
167789
167790 p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
167791 if( !p ) goto no_mem;
167792 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
167793 p->eType = FTSQUERY_PHRASE;
167794 p->pPhrase = (Fts3Phrase *)&p[1];
167795 p->pPhrase->iColumn = pParse->iDefaultCol;
167796 p->pPhrase->nToken = nToken;
167797
167798 zBuf = (char *)&p->pPhrase->aToken[nToken];
167799 if( zTemp ){
167800 memcpy(zBuf, zTemp, nTemp);
167801 sqlite3_free(zTemp);
167802 }else{
167803 assert( nTemp==0 )((void) (0));
167804 }
167805
167806 for(jj=0; jj<p->pPhrase->nToken; jj++){
167807 p->pPhrase->aToken[jj].z = zBuf;
167808 zBuf += p->pPhrase->aToken[jj].n;
167809 }
167810 rc = SQLITE_OK0;
167811 }
167812
167813 *ppExpr = p;
167814 return rc;
167815no_mem:
167816
167817 if( pCursor ){
167818 pModule->xClose(pCursor);
167819 }
167820 sqlite3_free(zTemp);
167821 sqlite3_free(p);
167822 *ppExpr = 0;
167823 return SQLITE_NOMEM7;
167824}
167825
167826/*
167827** The output variable *ppExpr is populated with an allocated Fts3Expr
167828** structure, or set to 0 if the end of the input buffer is reached.
167829**
167830** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
167831** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
167832** If SQLITE_ERROR is returned, pContext is populated with an error message.
167833*/
167834static int getNextNode(
167835 ParseContext *pParse, /* fts3 query parse context */
167836 const char *z, int n, /* Input string */
167837 Fts3Expr **ppExpr, /* OUT: expression */
167838 int *pnConsumed /* OUT: Number of bytes consumed */
167839){
167840 static const struct Fts3Keyword {
167841 char *z; /* Keyword text */
167842 unsigned char n; /* Length of the keyword */
167843 unsigned char parenOnly; /* Only valid in paren mode */
167844 unsigned char eType; /* Keyword code */
167845 } aKeyword[] = {
167846 { "OR" , 2, 0, FTSQUERY_OR },
167847 { "AND", 3, 1, FTSQUERY_AND },
167848 { "NOT", 3, 1, FTSQUERY_NOT },
167849 { "NEAR", 4, 0, FTSQUERY_NEAR }
167850 };
167851 int ii;
167852 int iCol;
167853 int iColLen;
167854 int rc;
167855 Fts3Expr *pRet = 0;
167856
167857 const char *zInput = z;
167858 int nInput = n;
167859
167860 pParse->isNot = 0;
167861
167862 /* Skip over any whitespace before checking for a keyword, an open or
167863 ** close bracket, or a quoted string.
167864 */
167865 while( nInput>0 && fts3isspace(*zInput) ){
167866 nInput--;
167867 zInput++;
167868 }
167869 if( nInput==0 ){
167870 return SQLITE_DONE101;
167871 }
167872
167873 /* See if we are dealing with a keyword. */
167874 for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
167875 const struct Fts3Keyword *pKey = &aKeyword[ii];
167876
167877 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
167878 continue;
167879 }
167880
167881 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
167882 int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
167883 int nKey = pKey->n;
167884 char cNext;
167885
167886 /* If this is a "NEAR" keyword, check for an explicit nearness. */
167887 if( pKey->eType==FTSQUERY_NEAR ){
167888 assert( nKey==4 )((void) (0));
167889 if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
167890 nNear = 0;
167891 for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
167892 nNear = nNear * 10 + (zInput[nKey] - '0');
167893 }
167894 }
167895 }
167896
167897 /* At this point this is probably a keyword. But for that to be true,
167898 ** the next byte must contain either whitespace, an open or close
167899 ** parenthesis, a quote character, or EOF.
167900 */
167901 cNext = zInput[nKey];
167902 if( fts3isspace(cNext)
167903 || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
167904 ){
167905 pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
167906 if( !pRet ){
167907 return SQLITE_NOMEM7;
167908 }
167909 pRet->eType = pKey->eType;
167910 pRet->nNear = nNear;
167911 *ppExpr = pRet;
167912 *pnConsumed = (int)((zInput - z) + nKey);
167913 return SQLITE_OK0;
167914 }
167915
167916 /* Turns out that wasn't a keyword after all. This happens if the
167917 ** user has supplied a token such as "ORacle". Continue.
167918 */
167919 }
167920 }
167921
167922 /* See if we are dealing with a quoted phrase. If this is the case, then
167923 ** search for the closing quote and pass the whole string to getNextString()
167924 ** for processing. This is easy to do, as fts3 has no syntax for escaping
167925 ** a quote character embedded in a string.
167926 */
167927 if( *zInput=='"' ){
167928 for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
167929 *pnConsumed = (int)((zInput - z) + ii + 1);
167930 if( ii==nInput ){
167931 return SQLITE_ERROR1;
167932 }
167933 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
167934 }
167935
167936 if( sqlite3_fts3_enable_parentheses ){
167937 if( *zInput=='(' ){
167938 int nConsumed = 0;
167939 pParse->nNest++;
167940 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
167941 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
167942 return rc;
167943 }else if( *zInput==')' ){
167944 pParse->nNest--;
167945 *pnConsumed = (int)((zInput - z) + 1);
167946 *ppExpr = 0;
167947 return SQLITE_DONE101;
167948 }
167949 }
167950
167951 /* If control flows to this point, this must be a regular token, or
167952 ** the end of the input. Read a regular token using the sqlite3_tokenizer
167953 ** interface. Before doing so, figure out if there is an explicit
167954 ** column specifier for the token.
167955 **
167956 ** TODO: Strangely, it is not possible to associate a column specifier
167957 ** with a quoted phrase, only with a single token. Not sure if this was
167958 ** an implementation artifact or an intentional decision when fts3 was
167959 ** first implemented. Whichever it was, this module duplicates the
167960 ** limitation.
167961 */
167962 iCol = pParse->iDefaultCol;
167963 iColLen = 0;
167964 for(ii=0; ii<pParse->nCol; ii++){
167965 const char *zStr = pParse->azCol[ii];
167966 int nStr = (int)strlen(zStr);
167967 if( nInput>nStr && zInput[nStr]==':'
167968 && sqlite3_strnicmp(zStr, zInput, nStr)==0
167969 ){
167970 iCol = ii;
167971 iColLen = (int)((zInput - z) + nStr + 1);
167972 break;
167973 }
167974 }
167975 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
167976 *pnConsumed += iColLen;
167977 return rc;
167978}
167979
167980/*
167981** The argument is an Fts3Expr structure for a binary operator (any type
167982** except an FTSQUERY_PHRASE). Return an integer value representing the
167983** precedence of the operator. Lower values have a higher precedence (i.e.
167984** group more tightly). For example, in the C language, the == operator
167985** groups more tightly than ||, and would therefore have a higher precedence.
167986**
167987** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
167988** is defined), the order of the operators in precedence from highest to
167989** lowest is:
167990**
167991** NEAR
167992** NOT
167993** AND (including implicit ANDs)
167994** OR
167995**
167996** Note that when using the old query syntax, the OR operator has a higher
167997** precedence than the AND operator.
167998*/
167999static int opPrecedence(Fts3Expr *p){
168000 assert( p->eType!=FTSQUERY_PHRASE )((void) (0));
168001 if( sqlite3_fts3_enable_parentheses ){
168002 return p->eType;
168003 }else if( p->eType==FTSQUERY_NEAR ){
168004 return 1;
168005 }else if( p->eType==FTSQUERY_OR ){
168006 return 2;
168007 }
168008 assert( p->eType==FTSQUERY_AND )((void) (0));
168009 return 3;
168010}
168011
168012/*
168013** Argument ppHead contains a pointer to the current head of a query
168014** expression tree being parsed. pPrev is the expression node most recently
168015** inserted into the tree. This function adds pNew, which is always a binary
168016** operator node, into the expression tree based on the relative precedence
168017** of pNew and the existing nodes of the tree. This may result in the head
168018** of the tree changing, in which case *ppHead is set to the new root node.
168019*/
168020static void insertBinaryOperator(
168021 Fts3Expr **ppHead, /* Pointer to the root node of a tree */
168022 Fts3Expr *pPrev, /* Node most recently inserted into the tree */
168023 Fts3Expr *pNew /* New binary node to insert into expression tree */
168024){
168025 Fts3Expr *pSplit = pPrev;
168026 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
168027 pSplit = pSplit->pParent;
168028 }
168029
168030 if( pSplit->pParent ){
168031 assert( pSplit->pParent->pRight==pSplit )((void) (0));
168032 pSplit->pParent->pRight = pNew;
168033 pNew->pParent = pSplit->pParent;
168034 }else{
168035 *ppHead = pNew;
168036 }
168037 pNew->pLeft = pSplit;
168038 pSplit->pParent = pNew;
168039}
168040
168041/*
168042** Parse the fts3 query expression found in buffer z, length n. This function
168043** returns either when the end of the buffer is reached or an unmatched
168044** closing bracket - ')' - is encountered.
168045**
168046** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
168047** parsed form of the expression and *pnConsumed is set to the number of
168048** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
168049** (out of memory error) or SQLITE_ERROR (parse error) is returned.
168050*/
168051static int fts3ExprParse(
168052 ParseContext *pParse, /* fts3 query parse context */
168053 const char *z, int n, /* Text of MATCH query */
168054 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
168055 int *pnConsumed /* OUT: Number of bytes consumed */
168056){
168057 Fts3Expr *pRet = 0;
168058 Fts3Expr *pPrev = 0;
168059 Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */
168060 int nIn = n;
168061 const char *zIn = z;
168062 int rc = SQLITE_OK0;
168063 int isRequirePhrase = 1;
168064
168065 while( rc==SQLITE_OK0 ){
168066 Fts3Expr *p = 0;
168067 int nByte = 0;
168068
168069 rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
168070 assert( nByte>0 || (rc!=SQLITE_OK && p==0) )((void) (0));
168071 if( rc==SQLITE_OK0 ){
168072 if( p ){
168073 int isPhrase;
168074
168075 if( !sqlite3_fts3_enable_parentheses
168076 && p->eType==FTSQUERY_PHRASE && pParse->isNot
168077 ){
168078 /* Create an implicit NOT operator. */
168079 Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
168080 if( !pNot ){
168081 sqlite3Fts3ExprFree(p);
168082 rc = SQLITE_NOMEM7;
168083 goto exprparse_out;
168084 }
168085 pNot->eType = FTSQUERY_NOT;
168086 pNot->pRight = p;
168087 p->pParent = pNot;
168088 if( pNotBranch ){
168089 pNot->pLeft = pNotBranch;
168090 pNotBranch->pParent = pNot;
168091 }
168092 pNotBranch = pNot;
168093 p = pPrev;
168094 }else{
168095 int eType = p->eType;
168096 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
168097
168098 /* The isRequirePhrase variable is set to true if a phrase or
168099 ** an expression contained in parenthesis is required. If a
168100 ** binary operator (AND, OR, NOT or NEAR) is encounted when
168101 ** isRequirePhrase is set, this is a syntax error.
168102 */
168103 if( !isPhrase && isRequirePhrase ){
168104 sqlite3Fts3ExprFree(p);
168105 rc = SQLITE_ERROR1;
168106 goto exprparse_out;
168107 }
168108
168109 if( isPhrase && !isRequirePhrase ){
168110 /* Insert an implicit AND operator. */
168111 Fts3Expr *pAnd;
168112 assert( pRet && pPrev )((void) (0));
168113 pAnd = fts3MallocZero(sizeof(Fts3Expr));
168114 if( !pAnd ){
168115 sqlite3Fts3ExprFree(p);
168116 rc = SQLITE_NOMEM7;
168117 goto exprparse_out;
168118 }
168119 pAnd->eType = FTSQUERY_AND;
168120 insertBinaryOperator(&pRet, pPrev, pAnd);
168121 pPrev = pAnd;
168122 }
168123
168124 /* This test catches attempts to make either operand of a NEAR
168125 ** operator something other than a phrase. For example, either of
168126 ** the following:
168127 **
168128 ** (bracketed expression) NEAR phrase
168129 ** phrase NEAR (bracketed expression)
168130 **
168131 ** Return an error in either case.
168132 */
168133 if( pPrev && (
168134 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
168135 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
168136 )){
168137 sqlite3Fts3ExprFree(p);
168138 rc = SQLITE_ERROR1;
168139 goto exprparse_out;
168140 }
168141
168142 if( isPhrase ){
168143 if( pRet ){
168144 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 )((void) (0));
168145 pPrev->pRight = p;
168146 p->pParent = pPrev;
168147 }else{
168148 pRet = p;
168149 }
168150 }else{
168151 insertBinaryOperator(&pRet, pPrev, p);
168152 }
168153 isRequirePhrase = !isPhrase;
168154 }
168155 pPrev = p;
168156 }
168157 assert( nByte>0 )((void) (0));
168158 }
168159 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) )((void) (0));
168160 nIn -= nByte;
168161 zIn += nByte;
168162 }
168163
168164 if( rc==SQLITE_DONE101 && pRet && isRequirePhrase ){
168165 rc = SQLITE_ERROR1;
168166 }
168167
168168 if( rc==SQLITE_DONE101 ){
168169 rc = SQLITE_OK0;
168170 if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
168171 if( !pRet ){
168172 rc = SQLITE_ERROR1;
168173 }else{
168174 Fts3Expr *pIter = pNotBranch;
168175 while( pIter->pLeft ){
168176 pIter = pIter->pLeft;
168177 }
168178 pIter->pLeft = pRet;
168179 pRet->pParent = pIter;
168180 pRet = pNotBranch;
168181 }
168182 }
168183 }
168184 *pnConsumed = n - nIn;
168185
168186exprparse_out:
168187 if( rc!=SQLITE_OK0 ){
168188 sqlite3Fts3ExprFree(pRet);
168189 sqlite3Fts3ExprFree(pNotBranch);
168190 pRet = 0;
168191 }
168192 *ppExpr = pRet;
168193 return rc;
168194}
168195
168196/*
168197** Return SQLITE_ERROR if the maximum depth of the expression tree passed
168198** as the only argument is more than nMaxDepth.
168199*/
168200static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
168201 int rc = SQLITE_OK0;
168202 if( p ){
168203 if( nMaxDepth<0 ){
168204 rc = SQLITE_TOOBIG18;
168205 }else{
168206 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
168207 if( rc==SQLITE_OK0 ){
168208 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
168209 }
168210 }
168211 }
168212 return rc;
168213}
168214
168215/*
168216** This function attempts to transform the expression tree at (*pp) to
168217** an equivalent but more balanced form. The tree is modified in place.
168218** If successful, SQLITE_OK is returned and (*pp) set to point to the
168219** new root expression node.
168220**
168221** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
168222**
168223** Otherwise, if an error occurs, an SQLite error code is returned and
168224** expression (*pp) freed.
168225*/
168226static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
168227 int rc = SQLITE_OK0; /* Return code */
168228 Fts3Expr *pRoot = *pp; /* Initial root node */
168229 Fts3Expr *pFree = 0; /* List of free nodes. Linked by pParent. */
168230 int eType = pRoot->eType; /* Type of node in this tree */
168231
168232 if( nMaxDepth==0 ){
168233 rc = SQLITE_ERROR1;
168234 }
168235
168236 if( rc==SQLITE_OK0 ){
168237 if( (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
168238 Fts3Expr **apLeaf;
168239 apLeaf = (Fts3Expr **)sqlite3_malloc64(sizeof(Fts3Expr *) * nMaxDepth);
168240 if( 0==apLeaf ){
168241 rc = SQLITE_NOMEM7;
168242 }else{
168243 memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
168244 }
168245
168246 if( rc==SQLITE_OK0 ){
168247 int i;
168248 Fts3Expr *p;
168249
168250 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
168251 for(p=pRoot; p->eType==eType; p=p->pLeft){
168252 assert( p->pParent==0 || p->pParent->pLeft==p )((void) (0));
168253 assert( p->pLeft && p->pRight )((void) (0));
168254 }
168255
168256 /* This loop runs once for each leaf in the tree of eType nodes. */
168257 while( 1 ){
168258 int iLvl;
168259 Fts3Expr *pParent = p->pParent; /* Current parent of p */
168260
168261 assert( pParent==0 || pParent->pLeft==p )((void) (0));
168262 p->pParent = 0;
168263 if( pParent ){
168264 pParent->pLeft = 0;
168265 }else{
168266 pRoot = 0;
168267 }
168268 rc = fts3ExprBalance(&p, nMaxDepth-1);
168269 if( rc!=SQLITE_OK0 ) break;
168270
168271 for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
168272 if( apLeaf[iLvl]==0 ){
168273 apLeaf[iLvl] = p;
168274 p = 0;
168275 }else{
168276 assert( pFree )((void) (0));
168277 pFree->pLeft = apLeaf[iLvl];
168278 pFree->pRight = p;
168279 pFree->pLeft->pParent = pFree;
168280 pFree->pRight->pParent = pFree;
168281
168282 p = pFree;
168283 pFree = pFree->pParent;
168284 p->pParent = 0;
168285 apLeaf[iLvl] = 0;
168286 }
168287 }
168288 if( p ){
168289 sqlite3Fts3ExprFree(p);
168290 rc = SQLITE_TOOBIG18;
168291 break;
168292 }
168293
168294 /* If that was the last leaf node, break out of the loop */
168295 if( pParent==0 ) break;
168296
168297 /* Set $p to point to the next leaf in the tree of eType nodes */
168298 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
168299
168300 /* Remove pParent from the original tree. */
168301 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent )((void) (0));
168302 pParent->pRight->pParent = pParent->pParent;
168303 if( pParent->pParent ){
168304 pParent->pParent->pLeft = pParent->pRight;
168305 }else{
168306 assert( pParent==pRoot )((void) (0));
168307 pRoot = pParent->pRight;
168308 }
168309
168310 /* Link pParent into the free node list. It will be used as an
168311 ** internal node of the new tree. */
168312 pParent->pParent = pFree;
168313 pFree = pParent;
168314 }
168315
168316 if( rc==SQLITE_OK0 ){
168317 p = 0;
168318 for(i=0; i<nMaxDepth; i++){
168319 if( apLeaf[i] ){
168320 if( p==0 ){
168321 p = apLeaf[i];
168322 p->pParent = 0;
168323 }else{
168324 assert( pFree!=0 )((void) (0));
168325 pFree->pRight = p;
168326 pFree->pLeft = apLeaf[i];
168327 pFree->pLeft->pParent = pFree;
168328 pFree->pRight->pParent = pFree;
168329
168330 p = pFree;
168331 pFree = pFree->pParent;
168332 p->pParent = 0;
168333 }
168334 }
168335 }
168336 pRoot = p;
168337 }else{
168338 /* An error occurred. Delete the contents of the apLeaf[] array
168339 ** and pFree list. Everything else is cleaned up by the call to
168340 ** sqlite3Fts3ExprFree(pRoot) below. */
168341 Fts3Expr *pDel;
168342 for(i=0; i<nMaxDepth; i++){
168343 sqlite3Fts3ExprFree(apLeaf[i]);
168344 }
168345 while( (pDel=pFree)!=0 ){
168346 pFree = pDel->pParent;
168347 sqlite3_free(pDel);
168348 }
168349 }
168350
168351 assert( pFree==0 )((void) (0));
168352 sqlite3_free( apLeaf );
168353 }
168354 }else if( eType==FTSQUERY_NOT ){
168355 Fts3Expr *pLeft = pRoot->pLeft;
168356 Fts3Expr *pRight = pRoot->pRight;
168357
168358 pRoot->pLeft = 0;
168359 pRoot->pRight = 0;
168360 pLeft->pParent = 0;
168361 pRight->pParent = 0;
168362
168363 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
168364 if( rc==SQLITE_OK0 ){
168365 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
168366 }
168367
168368 if( rc!=SQLITE_OK0 ){
168369 sqlite3Fts3ExprFree(pRight);
168370 sqlite3Fts3ExprFree(pLeft);
168371 }else{
168372 assert( pLeft && pRight )((void) (0));
168373 pRoot->pLeft = pLeft;
168374 pLeft->pParent = pRoot;
168375 pRoot->pRight = pRight;
168376 pRight->pParent = pRoot;
168377 }
168378 }
168379 }
168380
168381 if( rc!=SQLITE_OK0 ){
168382 sqlite3Fts3ExprFree(pRoot);
168383 pRoot = 0;
168384 }
168385 *pp = pRoot;
168386 return rc;
168387}
168388
168389/*
168390** This function is similar to sqlite3Fts3ExprParse(), with the following
168391** differences:
168392**
168393** 1. It does not do expression rebalancing.
168394** 2. It does not check that the expression does not exceed the
168395** maximum allowable depth.
168396** 3. Even if it fails, *ppExpr may still be set to point to an
168397** expression tree. It should be deleted using sqlite3Fts3ExprFree()
168398** in this case.
168399*/
168400static int fts3ExprParseUnbalanced(
168401 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
168402 int iLangid, /* Language id for tokenizer */
168403 char **azCol, /* Array of column names for fts3 table */
168404 int bFts4, /* True to allow FTS4-only syntax */
168405 int nCol, /* Number of entries in azCol[] */
168406 int iDefaultCol, /* Default column to query */
168407 const char *z, int n, /* Text of MATCH query */
168408 Fts3Expr **ppExpr /* OUT: Parsed query structure */
168409){
168410 int nParsed;
168411 int rc;
168412 ParseContext sParse;
168413
168414 memset(&sParse, 0, sizeof(ParseContext));
168415 sParse.pTokenizer = pTokenizer;
168416 sParse.iLangid = iLangid;
168417 sParse.azCol = (const char **)azCol;
168418 sParse.nCol = nCol;
168419 sParse.iDefaultCol = iDefaultCol;
168420 sParse.bFts4 = bFts4;
168421 if( z==0 ){
168422 *ppExpr = 0;
168423 return SQLITE_OK0;
168424 }
168425 if( n<0 ){
168426 n = (int)strlen(z);
168427 }
168428 rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
168429 assert( rc==SQLITE_OK || *ppExpr==0 )((void) (0));
168430
168431 /* Check for mismatched parenthesis */
168432 if( rc==SQLITE_OK0 && sParse.nNest ){
168433 rc = SQLITE_ERROR1;
168434 }
168435
168436 return rc;
168437}
168438
168439/*
168440** Parameters z and n contain a pointer to and length of a buffer containing
168441** an fts3 query expression, respectively. This function attempts to parse the
168442** query expression and create a tree of Fts3Expr structures representing the
168443** parsed expression. If successful, *ppExpr is set to point to the head
168444** of the parsed expression tree and SQLITE_OK is returned. If an error
168445** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
168446** error) is returned and *ppExpr is set to 0.
168447**
168448** If parameter n is a negative number, then z is assumed to point to a
168449** nul-terminated string and the length is determined using strlen().
168450**
168451** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
168452** use to normalize query tokens while parsing the expression. The azCol[]
168453** array, which is assumed to contain nCol entries, should contain the names
168454** of each column in the target fts3 table, in order from left to right.
168455** Column names must be nul-terminated strings.
168456**
168457** The iDefaultCol parameter should be passed the index of the table column
168458** that appears on the left-hand-side of the MATCH operator (the default
168459** column to match against for tokens for which a column name is not explicitly
168460** specified as part of the query string), or -1 if tokens may by default
168461** match any table column.
168462*/
168463SQLITE_PRIVATEstatic int sqlite3Fts3ExprParse(
168464 sqlite3_tokenizer *pTokenizer, /* Tokenizer module */
168465 int iLangid, /* Language id for tokenizer */
168466 char **azCol, /* Array of column names for fts3 table */
168467 int bFts4, /* True to allow FTS4-only syntax */
168468 int nCol, /* Number of entries in azCol[] */
168469 int iDefaultCol, /* Default column to query */
168470 const char *z, int n, /* Text of MATCH query */
168471 Fts3Expr **ppExpr, /* OUT: Parsed query structure */
168472 char **pzErr /* OUT: Error message (sqlite3_malloc) */
168473){
168474 int rc = fts3ExprParseUnbalanced(
168475 pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
168476 );
168477
168478 /* Rebalance the expression. And check that its depth does not exceed
168479 ** SQLITE_FTS3_MAX_EXPR_DEPTH. */
168480 if( rc==SQLITE_OK0 && *ppExpr ){
168481 rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
168482 if( rc==SQLITE_OK0 ){
168483 rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
168484 }
168485 }
168486
168487 if( rc!=SQLITE_OK0 ){
168488 sqlite3Fts3ExprFree(*ppExpr);
168489 *ppExpr = 0;
168490 if( rc==SQLITE_TOOBIG18 ){
168491 sqlite3Fts3ErrMsg(pzErr,
168492 "FTS expression tree is too large (maximum depth %d)",
168493 SQLITE_FTS3_MAX_EXPR_DEPTH
168494 );
168495 rc = SQLITE_ERROR1;
168496 }else if( rc==SQLITE_ERROR1 ){
168497 sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z);
168498 }
168499 }
168500
168501 return rc;
168502}
168503
168504/*
168505** Free a single node of an expression tree.
168506*/
168507static void fts3FreeExprNode(Fts3Expr *p){
168508 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 )((void) (0));
168509 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
168510 sqlite3_free(p->aMI);
168511 sqlite3_free(p);
168512}
168513
168514/*
168515** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
168516**
168517** This function would be simpler if it recursively called itself. But
168518** that would mean passing a sufficiently large expression to ExprParse()
168519** could cause a stack overflow.
168520*/
168521SQLITE_PRIVATEstatic void sqlite3Fts3ExprFree(Fts3Expr *pDel){
168522 Fts3Expr *p;
168523 assert( pDel==0 || pDel->pParent==0 )((void) (0));
168524 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
168525 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft )((void) (0));
168526 }
168527 while( p ){
168528 Fts3Expr *pParent = p->pParent;
168529 fts3FreeExprNode(p);
168530 if( pParent && p==pParent->pLeft && pParent->pRight ){
168531 p = pParent->pRight;
168532 while( p && (p->pLeft || p->pRight) ){
168533 assert( p==p->pParent->pRight || p==p->pParent->pLeft )((void) (0));
168534 p = (p->pLeft ? p->pLeft : p->pRight);
168535 }
168536 }else{
168537 p = pParent;
168538 }
168539 }
168540}
168541
168542/****************************************************************************
168543*****************************************************************************
168544** Everything after this point is just test code.
168545*/
168546
168547#ifdef SQLITE_TEST
168548
168549/* #include <stdio.h> */
168550
168551/*
168552** Return a pointer to a buffer containing a text representation of the
168553** expression passed as the first argument. The buffer is obtained from
168554** sqlite3_malloc(). It is the responsibility of the caller to use
168555** sqlite3_free() to release the memory. If an OOM condition is encountered,
168556** NULL is returned.
168557**
168558** If the second argument is not NULL, then its contents are prepended to
168559** the returned expression text and then freed using sqlite3_free().
168560*/
168561static char *exprToString(Fts3Expr *pExpr, char *zBuf){
168562 if( pExpr==0 ){
168563 return sqlite3_mprintf("");
168564 }
168565 switch( pExpr->eType ){
168566 case FTSQUERY_PHRASE: {
168567 Fts3Phrase *pPhrase = pExpr->pPhrase;
168568 int i;
168569 zBuf = sqlite3_mprintf(
168570 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
168571 for(i=0; zBuf && i<pPhrase->nToken; i++){
168572 zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
168573 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
168574 (pPhrase->aToken[i].isPrefix?"+":"")
168575 );
168576 }
168577 return zBuf;
168578 }
168579
168580 case FTSQUERY_NEAR:
168581 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
168582 break;
168583 case FTSQUERY_NOT:
168584 zBuf = sqlite3_mprintf("%zNOT ", zBuf);
168585 break;
168586 case FTSQUERY_AND:
168587 zBuf = sqlite3_mprintf("%zAND ", zBuf);
168588 break;
168589 case FTSQUERY_OR:
168590 zBuf = sqlite3_mprintf("%zOR ", zBuf);
168591 break;
168592 }
168593
168594 if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
168595 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
168596 if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
168597
168598 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
168599 if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
168600
168601 return zBuf;
168602}
168603
168604/*
168605** This is the implementation of a scalar SQL function used to test the
168606** expression parser. It should be called as follows:
168607**
168608** fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
168609**
168610** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
168611** to parse the query expression (see README.tokenizers). The second argument
168612** is the query expression to parse. Each subsequent argument is the name
168613** of a column of the fts3 table that the query expression may refer to.
168614** For example:
168615**
168616** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
168617*/
168618static void fts3ExprTestCommon(
168619 int bRebalance,
168620 sqlite3_context *context,
168621 int argc,
168622 sqlite3_value **argv
168623){
168624 sqlite3_tokenizer *pTokenizer = 0;
168625 int rc;
168626 char **azCol = 0;
168627 const char *zExpr;
168628 int nExpr;
168629 int nCol;
168630 int ii;
168631 Fts3Expr *pExpr;
168632 char *zBuf = 0;
168633 Fts3Hash *pHash = (Fts3Hash*)sqlite3_user_data(context);
168634 const char *zTokenizer = 0;
168635 char *zErr = 0;
168636
168637 if( argc<3 ){
168638 sqlite3_result_error(context,
168639 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
168640 );
168641 return;
168642 }
168643
168644 zTokenizer = (const char*)sqlite3_value_text(argv[0]);
168645 rc = sqlite3Fts3InitTokenizer(pHash, zTokenizer, &pTokenizer, &zErr);
168646 if( rc!=SQLITE_OK0 ){
168647 if( rc==SQLITE_NOMEM7 ){
168648 sqlite3_result_error_nomem(context);
168649 }else{
168650 sqlite3_result_error(context, zErr, -1);
168651 }
168652 sqlite3_free(zErr);
168653 return;
168654 }
168655
168656 zExpr = (const char *)sqlite3_value_text(argv[1]);
168657 nExpr = sqlite3_value_bytes(argv[1]);
168658 nCol = argc-2;
168659 azCol = (char **)sqlite3_malloc64(nCol*sizeof(char *));
168660 if( !azCol ){
168661 sqlite3_result_error_nomem(context);
168662 goto exprtest_out;
168663 }
168664 for(ii=0; ii<nCol; ii++){
168665 azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
168666 }
168667
168668 if( bRebalance ){
168669 char *zDummy = 0;
168670 rc = sqlite3Fts3ExprParse(
168671 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
168672 );
168673 assert( rc==SQLITE_OK || pExpr==0 )((void) (0));
168674 sqlite3_free(zDummy);
168675 }else{
168676 rc = fts3ExprParseUnbalanced(
168677 pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
168678 );
168679 }
168680
168681 if( rc!=SQLITE_OK0 && rc!=SQLITE_NOMEM7 ){
168682 sqlite3Fts3ExprFree(pExpr);
168683 sqlite3_result_error(context, "Error parsing expression", -1);
168684 }else if( rc==SQLITE_NOMEM7 || !(zBuf = exprToString(pExpr, 0)) ){
168685 sqlite3_result_error_nomem(context);
168686 }else{
168687 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
168688 sqlite3_free(zBuf);
168689 }
168690
168691 sqlite3Fts3ExprFree(pExpr);
168692
168693exprtest_out:
168694 if( pTokenizer ){
168695 rc = pTokenizer->pModule->xDestroy(pTokenizer);
168696 }
168697 sqlite3_free(azCol);
168698}
168699
168700static void fts3ExprTest(
168701 sqlite3_context *context,
168702 int argc,
168703 sqlite3_value **argv
168704){
168705 fts3ExprTestCommon(0, context, argc, argv);
168706}
168707static void fts3ExprTestRebalance(
168708 sqlite3_context *context,
168709 int argc,
168710 sqlite3_value **argv
168711){
168712 fts3ExprTestCommon(1, context, argc, argv);
168713}
168714
168715/*
168716** Register the query expression parser test function fts3_exprtest()
168717** with database connection db.
168718*/
168719SQLITE_PRIVATEstatic int sqlite3Fts3ExprInitTestInterface(sqlite3 *db, Fts3Hash *pHash){
168720 int rc = sqlite3_create_function(
168721 db, "fts3_exprtest", -1, SQLITE_UTF81, (void*)pHash, fts3ExprTest, 0, 0
168722 );
168723 if( rc==SQLITE_OK0 ){
168724 rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
168725 -1, SQLITE_UTF81, (void*)pHash, fts3ExprTestRebalance, 0, 0
168726 );
168727 }
168728 return rc;
168729}
168730
168731#endif
168732#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
168733
168734/************** End of fts3_expr.c *******************************************/
168735/************** Begin file fts3_hash.c ***************************************/
168736/*
168737** 2001 September 22
168738**
168739** The author disclaims copyright to this source code. In place of
168740** a legal notice, here is a blessing:
168741**
168742** May you do good and not evil.
168743** May you find forgiveness for yourself and forgive others.
168744** May you share freely, never taking more than you give.
168745**
168746*************************************************************************
168747** This is the implementation of generic hash-tables used in SQLite.
168748** We've modified it slightly to serve as a standalone hash table
168749** implementation for the full-text indexing module.
168750*/
168751
168752/*
168753** The code in this file is only compiled if:
168754**
168755** * The FTS3 module is being built as an extension
168756** (in which case SQLITE_CORE is not defined), or
168757**
168758** * The FTS3 module is being built into the core of
168759** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
168760*/
168761/* #include "fts3Int.h" */
168762#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
168763
168764/* #include <assert.h> */
168765/* #include <stdlib.h> */
168766/* #include <string.h> */
168767
168768/* #include "fts3_hash.h" */
168769
168770/*
168771** Malloc and Free functions
168772*/
168773static void *fts3HashMalloc(sqlite3_int64 n){
168774 void *p = sqlite3_malloc64(n);
168775 if( p ){
168776 memset(p, 0, n);
168777 }
168778 return p;
168779}
168780static void fts3HashFree(void *p){
168781 sqlite3_free(p);
168782}
168783
168784/* Turn bulk memory into a hash table object by initializing the
168785** fields of the Hash structure.
168786**
168787** "pNew" is a pointer to the hash table that is to be initialized.
168788** keyClass is one of the constants
168789** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass
168790** determines what kind of key the hash table will use. "copyKey" is
168791** true if the hash table should make its own private copy of keys and
168792** false if it should just use the supplied pointer.
168793*/
168794SQLITE_PRIVATEstatic void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
168795 assert( pNew!=0 )((void) (0));
168796 assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY )((void) (0));
168797 pNew->keyClass = keyClass;
168798 pNew->copyKey = copyKey;
168799 pNew->first = 0;
168800 pNew->count = 0;
168801 pNew->htsize = 0;
168802 pNew->ht = 0;
168803}
168804
168805/* Remove all entries from a hash table. Reclaim all memory.
168806** Call this routine to delete a hash table or to reset a hash table
168807** to the empty state.
168808*/
168809SQLITE_PRIVATEstatic void sqlite3Fts3HashClear(Fts3Hash *pH){
168810 Fts3HashElem *elem; /* For looping over all elements of the table */
168811
168812 assert( pH!=0 )((void) (0));
168813 elem = pH->first;
168814 pH->first = 0;
168815 fts3HashFree(pH->ht);
168816 pH->ht = 0;
168817 pH->htsize = 0;
168818 while( elem ){
168819 Fts3HashElem *next_elem = elem->next;
168820 if( pH->copyKey && elem->pKey ){
168821 fts3HashFree(elem->pKey);
168822 }
168823 fts3HashFree(elem);
168824 elem = next_elem;
168825 }
168826 pH->count = 0;
168827}
168828
168829/*
168830** Hash and comparison functions when the mode is FTS3_HASH_STRING
168831*/
168832static int fts3StrHash(const void *pKey, int nKey){
168833 const char *z = (const char *)pKey;
168834 unsigned h = 0;
168835 if( nKey<=0 ) nKey = (int) strlen(z);
168836 while( nKey > 0 ){
168837 h = (h<<3) ^ h ^ *z++;
168838 nKey--;
168839 }
168840 return (int)(h & 0x7fffffff);
168841}
168842static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
168843 if( n1!=n2 ) return 1;
168844 return strncmp((const char*)pKey1,(const char*)pKey2,n1);
168845}
168846
168847/*
168848** Hash and comparison functions when the mode is FTS3_HASH_BINARY
168849*/
168850static int fts3BinHash(const void *pKey, int nKey){
168851 int h = 0;
168852 const char *z = (const char *)pKey;
168853 while( nKey-- > 0 ){
168854 h = (h<<3) ^ h ^ *(z++);
168855 }
168856 return h & 0x7fffffff;
168857}
168858static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
168859 if( n1!=n2 ) return 1;
168860 return memcmp(pKey1,pKey2,n1);
168861}
168862
168863/*
168864** Return a pointer to the appropriate hash function given the key class.
168865**
168866** The C syntax in this function definition may be unfamilar to some
168867** programmers, so we provide the following additional explanation:
168868**
168869** The name of the function is "ftsHashFunction". The function takes a
168870** single parameter "keyClass". The return value of ftsHashFunction()
168871** is a pointer to another function. Specifically, the return value
168872** of ftsHashFunction() is a pointer to a function that takes two parameters
168873** with types "const void*" and "int" and returns an "int".
168874*/
168875static int (*ftsHashFunction(int keyClass))(const void*,int){
168876 if( keyClass==FTS3_HASH_STRING ){
168877 return &fts3StrHash;
168878 }else{
168879 assert( keyClass==FTS3_HASH_BINARY )((void) (0));
168880 return &fts3BinHash;
168881 }
168882}
168883
168884/*
168885** Return a pointer to the appropriate hash function given the key class.
168886**
168887** For help in interpreted the obscure C code in the function definition,
168888** see the header comment on the previous function.
168889*/
168890static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
168891 if( keyClass==FTS3_HASH_STRING ){
168892 return &fts3StrCompare;
168893 }else{
168894 assert( keyClass==FTS3_HASH_BINARY )((void) (0));
168895 return &fts3BinCompare;
168896 }
168897}
168898
168899/* Link an element into the hash table
168900*/
168901static void fts3HashInsertElement(
168902 Fts3Hash *pH, /* The complete hash table */
168903 struct _fts3ht *pEntry, /* The entry into which pNew is inserted */
168904 Fts3HashElem *pNew /* The element to be inserted */
168905){
168906 Fts3HashElem *pHead; /* First element already in pEntry */
168907 pHead = pEntry->chain;
168908 if( pHead ){
168909 pNew->next = pHead;
168910 pNew->prev = pHead->prev;
168911 if( pHead->prev ){ pHead->prev->next = pNew; }
168912 else { pH->first = pNew; }
168913 pHead->prev = pNew;
168914 }else{
168915 pNew->next = pH->first;
168916 if( pH->first ){ pH->first->prev = pNew; }
168917 pNew->prev = 0;
168918 pH->first = pNew;
168919 }
168920 pEntry->count++;
168921 pEntry->chain = pNew;
168922}
168923
168924
168925/* Resize the hash table so that it cantains "new_size" buckets.
168926** "new_size" must be a power of 2. The hash table might fail
168927** to resize if sqliteMalloc() fails.
168928**
168929** Return non-zero if a memory allocation error occurs.
168930*/
168931static int fts3Rehash(Fts3Hash *pH, int new_size){
168932 struct _fts3ht *new_ht; /* The new hash table */
168933 Fts3HashElem *elem, *next_elem; /* For looping over existing elements */
168934 int (*xHash)(const void*,int); /* The hash function */
168935
168936 assert( (new_size & (new_size-1))==0 )((void) (0));
168937 new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
168938 if( new_ht==0 ) return 1;
168939 fts3HashFree(pH->ht);
168940 pH->ht = new_ht;
168941 pH->htsize = new_size;
168942 xHash = ftsHashFunction(pH->keyClass);
168943 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
168944 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
168945 next_elem = elem->next;
168946 fts3HashInsertElement(pH, &new_ht[h], elem);
168947 }
168948 return 0;
168949}
168950
168951/* This function (for internal use only) locates an element in an
168952** hash table that matches the given key. The hash for this key has
168953** already been computed and is passed as the 4th parameter.
168954*/
168955static Fts3HashElem *fts3FindElementByHash(
168956 const Fts3Hash *pH, /* The pH to be searched */
168957 const void *pKey, /* The key we are searching for */
168958 int nKey,
168959 int h /* The hash for this key. */
168960){
168961 Fts3HashElem *elem; /* Used to loop thru the element list */
168962 int count; /* Number of elements left to test */
168963 int (*xCompare)(const void*,int,const void*,int); /* comparison function */
168964
168965 if( pH->ht ){
168966 struct _fts3ht *pEntry = &pH->ht[h];
168967 elem = pEntry->chain;
168968 count = pEntry->count;
168969 xCompare = ftsCompareFunction(pH->keyClass);
168970 while( count-- && elem ){
168971 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
168972 return elem;
168973 }
168974 elem = elem->next;
168975 }
168976 }
168977 return 0;
168978}
168979
168980/* Remove a single entry from the hash table given a pointer to that
168981** element and a hash on the element's key.
168982*/
168983static void fts3RemoveElementByHash(
168984 Fts3Hash *pH, /* The pH containing "elem" */
168985 Fts3HashElem* elem, /* The element to be removed from the pH */
168986 int h /* Hash value for the element */
168987){
168988 struct _fts3ht *pEntry;
168989 if( elem->prev ){
168990 elem->prev->next = elem->next;
168991 }else{
168992 pH->first = elem->next;
168993 }
168994 if( elem->next ){
168995 elem->next->prev = elem->prev;
168996 }
168997 pEntry = &pH->ht[h];
168998 if( pEntry->chain==elem ){
168999 pEntry->chain = elem->next;
169000 }
169001 pEntry->count--;
169002 if( pEntry->count<=0 ){
169003 pEntry->chain = 0;
169004 }
169005 if( pH->copyKey && elem->pKey ){
169006 fts3HashFree(elem->pKey);
169007 }
169008 fts3HashFree( elem );
169009 pH->count--;
169010 if( pH->count<=0 ){
169011 assert( pH->first==0 )((void) (0));
169012 assert( pH->count==0 )((void) (0));
169013 fts3HashClear(pH);
169014 }
169015}
169016
169017SQLITE_PRIVATEstatic Fts3HashElem *sqlite3Fts3HashFindElem(
169018 const Fts3Hash *pH,
169019 const void *pKey,
169020 int nKey
169021){
169022 int h; /* A hash on key */
169023 int (*xHash)(const void*,int); /* The hash function */
169024
169025 if( pH==0 || pH->ht==0 ) return 0;
169026 xHash = ftsHashFunction(pH->keyClass);
169027 assert( xHash!=0 )((void) (0));
169028 h = (*xHash)(pKey,nKey);
169029 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
169030 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
169031}
169032
169033/*
169034** Attempt to locate an element of the hash table pH with a key
169035** that matches pKey,nKey. Return the data for this element if it is
169036** found, or NULL if there is no match.
169037*/
169038SQLITE_PRIVATEstatic void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
169039 Fts3HashElem *pElem; /* The element that matches key (if any) */
169040
169041 pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
169042 return pElem ? pElem->data : 0;
169043}
169044
169045/* Insert an element into the hash table pH. The key is pKey,nKey
169046** and the data is "data".
169047**
169048** If no element exists with a matching key, then a new
169049** element is created. A copy of the key is made if the copyKey
169050** flag is set. NULL is returned.
169051**
169052** If another element already exists with the same key, then the
169053** new data replaces the old data and the old data is returned.
169054** The key is not copied in this instance. If a malloc fails, then
169055** the new data is returned and the hash table is unchanged.
169056**
169057** If the "data" parameter to this function is NULL, then the
169058** element corresponding to "key" is removed from the hash table.
169059*/
169060SQLITE_PRIVATEstatic void *sqlite3Fts3HashInsert(
169061 Fts3Hash *pH, /* The hash table to insert into */
169062 const void *pKey, /* The key */
169063 int nKey, /* Number of bytes in the key */
169064 void *data /* The data */
169065){
169066 int hraw; /* Raw hash value of the key */
169067 int h; /* the hash of the key modulo hash table size */
169068 Fts3HashElem *elem; /* Used to loop thru the element list */
169069 Fts3HashElem *new_elem; /* New element added to the pH */
169070 int (*xHash)(const void*,int); /* The hash function */
169071
169072 assert( pH!=0 )((void) (0));
169073 xHash = ftsHashFunction(pH->keyClass);
169074 assert( xHash!=0 )((void) (0));
169075 hraw = (*xHash)(pKey, nKey);
169076 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
169077 h = hraw & (pH->htsize-1);
169078 elem = fts3FindElementByHash(pH,pKey,nKey,h);
169079 if( elem ){
169080 void *old_data = elem->data;
169081 if( data==0 ){
169082 fts3RemoveElementByHash(pH,elem,h);
169083 }else{
169084 elem->data = data;
169085 }
169086 return old_data;
169087 }
169088 if( data==0 ) return 0;
169089 if( (pH->htsize==0 && fts3Rehash(pH,8))
169090 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
169091 ){
169092 pH->count = 0;
169093 return data;
169094 }
169095 assert( pH->htsize>0 )((void) (0));
169096 new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
169097 if( new_elem==0 ) return data;
169098 if( pH->copyKey && pKey!=0 ){
169099 new_elem->pKey = fts3HashMalloc( nKey );
169100 if( new_elem->pKey==0 ){
169101 fts3HashFree(new_elem);
169102 return data;
169103 }
169104 memcpy((void*)new_elem->pKey, pKey, nKey);
169105 }else{
169106 new_elem->pKey = (void*)pKey;
169107 }
169108 new_elem->nKey = nKey;
169109 pH->count++;
169110 assert( pH->htsize>0 )((void) (0));
169111 assert( (pH->htsize & (pH->htsize-1))==0 )((void) (0));
169112 h = hraw & (pH->htsize-1);
169113 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
169114 new_elem->data = data;
169115 return 0;
169116}
169117
169118#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
169119
169120/************** End of fts3_hash.c *******************************************/
169121/************** Begin file fts3_porter.c *************************************/
169122/*
169123** 2006 September 30
169124**
169125** The author disclaims copyright to this source code. In place of
169126** a legal notice, here is a blessing:
169127**
169128** May you do good and not evil.
169129** May you find forgiveness for yourself and forgive others.
169130** May you share freely, never taking more than you give.
169131**
169132*************************************************************************
169133** Implementation of the full-text-search tokenizer that implements
169134** a Porter stemmer.
169135*/
169136
169137/*
169138** The code in this file is only compiled if:
169139**
169140** * The FTS3 module is being built as an extension
169141** (in which case SQLITE_CORE is not defined), or
169142**
169143** * The FTS3 module is being built into the core of
169144** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
169145*/
169146/* #include "fts3Int.h" */
169147#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
169148
169149/* #include <assert.h> */
169150/* #include <stdlib.h> */
169151/* #include <stdio.h> */
169152/* #include <string.h> */
169153
169154/* #include "fts3_tokenizer.h" */
169155
169156/*
169157** Class derived from sqlite3_tokenizer
169158*/
169159typedef struct porter_tokenizer {
169160 sqlite3_tokenizer base; /* Base class */
169161} porter_tokenizer;
169162
169163/*
169164** Class derived from sqlite3_tokenizer_cursor
169165*/
169166typedef struct porter_tokenizer_cursor {
169167 sqlite3_tokenizer_cursor base;
169168 const char *zInput; /* input we are tokenizing */
169169 int nInput; /* size of the input */
169170 int iOffset; /* current position in zInput */
169171 int iToken; /* index of next token to be returned */
169172 char *zToken; /* storage for current token */
169173 int nAllocated; /* space allocated to zToken buffer */
169174} porter_tokenizer_cursor;
169175
169176
169177/*
169178** Create a new tokenizer instance.
169179*/
169180static int porterCreate(
169181 int argc, const char * const *argv,
169182 sqlite3_tokenizer **ppTokenizer
169183){
169184 porter_tokenizer *t;
169185
169186 UNUSED_PARAMETER(argc)(void)(argc);
169187 UNUSED_PARAMETER(argv)(void)(argv);
169188
169189 t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
169190 if( t==NULL((void*)0) ) return SQLITE_NOMEM7;
169191 memset(t, 0, sizeof(*t));
169192 *ppTokenizer = &t->base;
169193 return SQLITE_OK0;
169194}
169195
169196/*
169197** Destroy a tokenizer
169198*/
169199static int porterDestroy(sqlite3_tokenizer *pTokenizer){
169200 sqlite3_free(pTokenizer);
169201 return SQLITE_OK0;
169202}
169203
169204/*
169205** Prepare to begin tokenizing a particular string. The input
169206** string to be tokenized is zInput[0..nInput-1]. A cursor
169207** used to incrementally tokenize this string is returned in
169208** *ppCursor.
169209*/
169210static int porterOpen(
169211 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
169212 const char *zInput, int nInput, /* String to be tokenized */
169213 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
169214){
169215 porter_tokenizer_cursor *c;
169216
169217 UNUSED_PARAMETER(pTokenizer)(void)(pTokenizer);
169218
169219 c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
169220 if( c==NULL((void*)0) ) return SQLITE_NOMEM7;
169221
169222 c->zInput = zInput;
169223 if( zInput==0 ){
169224 c->nInput = 0;
169225 }else if( nInput<0 ){
169226 c->nInput = (int)strlen(zInput);
169227 }else{
169228 c->nInput = nInput;
169229 }
169230 c->iOffset = 0; /* start tokenizing at the beginning */
169231 c->iToken = 0;
169232 c->zToken = NULL((void*)0); /* no space allocated, yet. */
169233 c->nAllocated = 0;
169234
169235 *ppCursor = &c->base;
169236 return SQLITE_OK0;
169237}
169238
169239/*
169240** Close a tokenization cursor previously opened by a call to
169241** porterOpen() above.
169242*/
169243static int porterClose(sqlite3_tokenizer_cursor *pCursor){
169244 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
169245 sqlite3_free(c->zToken);
169246 sqlite3_free(c);
169247 return SQLITE_OK0;
169248}
169249/*
169250** Vowel or consonant
169251*/
169252static const char cType[] = {
169253 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
169254 1, 1, 1, 2, 1
169255};
169256
169257/*
169258** isConsonant() and isVowel() determine if their first character in
169259** the string they point to is a consonant or a vowel, according
169260** to Porter ruls.
169261**
169262** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
169263** 'Y' is a consonant unless it follows another consonant,
169264** in which case it is a vowel.
169265**
169266** In these routine, the letters are in reverse order. So the 'y' rule
169267** is that 'y' is a consonant unless it is followed by another
169268** consonent.
169269*/
169270static int isVowel(const char*);
169271static int isConsonant(const char *z){
169272 int j;
169273 char x = *z;
169274 if( x==0 ) return 0;
169275 assert( x>='a' && x<='z' )((void) (0));
169276 j = cType[x-'a'];
169277 if( j<2 ) return j;
169278 return z[1]==0 || isVowel(z + 1);
169279}
169280static int isVowel(const char *z){
169281 int j;
169282 char x = *z;
169283 if( x==0 ) return 0;
169284 assert( x>='a' && x<='z' )((void) (0));
169285 j = cType[x-'a'];
169286 if( j<2 ) return 1-j;
169287 return isConsonant(z + 1);
169288}
169289
169290/*
169291** Let any sequence of one or more vowels be represented by V and let
169292** C be sequence of one or more consonants. Then every word can be
169293** represented as:
169294**
169295** [C] (VC){m} [V]
169296**
169297** In prose: A word is an optional consonant followed by zero or
169298** vowel-consonant pairs followed by an optional vowel. "m" is the
169299** number of vowel consonant pairs. This routine computes the value
169300** of m for the first i bytes of a word.
169301**
169302** Return true if the m-value for z is 1 or more. In other words,
169303** return true if z contains at least one vowel that is followed
169304** by a consonant.
169305**
169306** In this routine z[] is in reverse order. So we are really looking
169307** for an instance of a consonant followed by a vowel.
169308*/
169309static int m_gt_0(const char *z){
169310 while( isVowel(z) ){ z++; }
169311 if( *z==0 ) return 0;
169312 while( isConsonant(z) ){ z++; }
169313 return *z!=0;
169314}
169315
169316/* Like mgt0 above except we are looking for a value of m which is
169317** exactly 1
169318*/
169319static int m_eq_1(const char *z){
169320 while( isVowel(z) ){ z++; }
169321 if( *z==0 ) return 0;
169322 while( isConsonant(z) ){ z++; }
169323 if( *z==0 ) return 0;
169324 while( isVowel(z) ){ z++; }
169325 if( *z==0 ) return 1;
169326 while( isConsonant(z) ){ z++; }
169327 return *z==0;
169328}
169329
169330/* Like mgt0 above except we are looking for a value of m>1 instead
169331** or m>0
169332*/
169333static int m_gt_1(const char *z){
169334 while( isVowel(z) ){ z++; }
169335 if( *z==0 ) return 0;
169336 while( isConsonant(z) ){ z++; }
169337 if( *z==0 ) return 0;
169338 while( isVowel(z) ){ z++; }
169339 if( *z==0 ) return 0;
169340 while( isConsonant(z) ){ z++; }
169341 return *z!=0;
169342}
169343
169344/*
169345** Return TRUE if there is a vowel anywhere within z[0..n-1]
169346*/
169347static int hasVowel(const char *z){
169348 while( isConsonant(z) ){ z++; }
169349 return *z!=0;
169350}
169351
169352/*
169353** Return TRUE if the word ends in a double consonant.
169354**
169355** The text is reversed here. So we are really looking at
169356** the first two characters of z[].
169357*/
169358static int doubleConsonant(const char *z){
169359 return isConsonant(z) && z[0]==z[1];
169360}
169361
169362/*
169363** Return TRUE if the word ends with three letters which
169364** are consonant-vowel-consonent and where the final consonant
169365** is not 'w', 'x', or 'y'.
169366**
169367** The word is reversed here. So we are really checking the
169368** first three letters and the first one cannot be in [wxy].
169369*/
169370static int star_oh(const char *z){
169371 return
169372 isConsonant(z) &&
169373 z[0]!='w' && z[0]!='x' && z[0]!='y' &&
169374 isVowel(z+1) &&
169375 isConsonant(z+2);
169376}
169377
169378/*
169379** If the word ends with zFrom and xCond() is true for the stem
169380** of the word that preceeds the zFrom ending, then change the
169381** ending to zTo.
169382**
169383** The input word *pz and zFrom are both in reverse order. zTo
169384** is in normal order.
169385**
169386** Return TRUE if zFrom matches. Return FALSE if zFrom does not
169387** match. Not that TRUE is returned even if xCond() fails and
169388** no substitution occurs.
169389*/
169390static int stem(
169391 char **pz, /* The word being stemmed (Reversed) */
169392 const char *zFrom, /* If the ending matches this... (Reversed) */
169393 const char *zTo, /* ... change the ending to this (not reversed) */
169394 int (*xCond)(const char*) /* Condition that must be true */
169395){
169396 char *z = *pz;
169397 while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
169398 if( *zFrom!=0 ) return 0;
169399 if( xCond && !xCond(z) ) return 1;
169400 while( *zTo ){
169401 *(--z) = *(zTo++);
169402 }
169403 *pz = z;
169404 return 1;
169405}
169406
169407/*
169408** This is the fallback stemmer used when the porter stemmer is
169409** inappropriate. The input word is copied into the output with
169410** US-ASCII case folding. If the input word is too long (more
169411** than 20 bytes if it contains no digits or more than 6 bytes if
169412** it contains digits) then word is truncated to 20 or 6 bytes
169413** by taking 10 or 3 bytes from the beginning and end.
169414*/
169415static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
169416 int i, mx, j;
169417 int hasDigit = 0;
169418 for(i=0; i<nIn; i++){
169419 char c = zIn[i];
169420 if( c>='A' && c<='Z' ){
169421 zOut[i] = c - 'A' + 'a';
169422 }else{
169423 if( c>='0' && c<='9' ) hasDigit = 1;
169424 zOut[i] = c;
169425 }
169426 }
169427 mx = hasDigit ? 3 : 10;
169428 if( nIn>mx*2 ){
169429 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
169430 zOut[j] = zOut[i];
169431 }
169432 i = j;
169433 }
169434 zOut[i] = 0;
169435 *pnOut = i;
169436}
169437
169438
169439/*
169440** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
169441** zOut is at least big enough to hold nIn bytes. Write the actual
169442** size of the output word (exclusive of the '\0' terminator) into *pnOut.
169443**
169444** Any upper-case characters in the US-ASCII character set ([A-Z])
169445** are converted to lower case. Upper-case UTF characters are
169446** unchanged.
169447**
169448** Words that are longer than about 20 bytes are stemmed by retaining
169449** a few bytes from the beginning and the end of the word. If the
169450** word contains digits, 3 bytes are taken from the beginning and
169451** 3 bytes from the end. For long words without digits, 10 bytes
169452** are taken from each end. US-ASCII case folding still applies.
169453**
169454** If the input word contains not digits but does characters not
169455** in [a-zA-Z] then no stemming is attempted and this routine just
169456** copies the input into the input into the output with US-ASCII
169457** case folding.
169458**
169459** Stemming never increases the length of the word. So there is
169460** no chance of overflowing the zOut buffer.
169461*/
169462static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
169463 int i, j;
169464 char zReverse[28];
169465 char *z, *z2;
169466 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
169467 /* The word is too big or too small for the porter stemmer.
169468 ** Fallback to the copy stemmer */
169469 copy_stemmer(zIn, nIn, zOut, pnOut);
169470 return;
169471 }
169472 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
169473 char c = zIn[i];
169474 if( c>='A' && c<='Z' ){
169475 zReverse[j] = c + 'a' - 'A';
169476 }else if( c>='a' && c<='z' ){
169477 zReverse[j] = c;
169478 }else{
169479 /* The use of a character not in [a-zA-Z] means that we fallback
169480 ** to the copy stemmer */
169481 copy_stemmer(zIn, nIn, zOut, pnOut);
169482 return;
169483 }
169484 }
169485 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
169486 z = &zReverse[j+1];
169487
169488
169489 /* Step 1a */
169490 if( z[0]=='s' ){
169491 if(
169492 !stem(&z, "sess", "ss", 0) &&
169493 !stem(&z, "sei", "i", 0) &&
169494 !stem(&z, "ss", "ss", 0)
169495 ){
169496 z++;
169497 }
169498 }
169499
169500 /* Step 1b */
169501 z2 = z;
169502 if( stem(&z, "dee", "ee", m_gt_0) ){
169503 /* Do nothing. The work was all in the test */
169504 }else if(
169505 (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
169506 && z!=z2
169507 ){
169508 if( stem(&z, "ta", "ate", 0) ||
169509 stem(&z, "lb", "ble", 0) ||
169510 stem(&z, "zi", "ize", 0) ){
169511 /* Do nothing. The work was all in the test */
169512 }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
169513 z++;
169514 }else if( m_eq_1(z) && star_oh(z) ){
169515 *(--z) = 'e';
169516 }
169517 }
169518
169519 /* Step 1c */
169520 if( z[0]=='y' && hasVowel(z+1) ){
169521 z[0] = 'i';
169522 }
169523
169524 /* Step 2 */
169525 switch( z[1] ){
169526 case 'a':
169527 if( !stem(&z, "lanoita", "ate", m_gt_0) ){
169528 stem(&z, "lanoit", "tion", m_gt_0);
169529 }
169530 break;
169531 case 'c':
169532 if( !stem(&z, "icne", "ence", m_gt_0) ){
169533 stem(&z, "icna", "ance", m_gt_0);
169534 }
169535 break;
169536 case 'e':
169537 stem(&z, "rezi", "ize", m_gt_0);
169538 break;
169539 case 'g':
169540 stem(&z, "igol", "log", m_gt_0);
169541 break;
169542 case 'l':
169543 if( !stem(&z, "ilb", "ble", m_gt_0)
169544 && !stem(&z, "illa", "al", m_gt_0)
169545 && !stem(&z, "iltne", "ent", m_gt_0)
169546 && !stem(&z, "ile", "e", m_gt_0)
169547 ){
169548 stem(&z, "ilsuo", "ous", m_gt_0);
169549 }
169550 break;
169551 case 'o':
169552 if( !stem(&z, "noitazi", "ize", m_gt_0)
169553 && !stem(&z, "noita", "ate", m_gt_0)
169554 ){
169555 stem(&z, "rota", "ate", m_gt_0);
169556 }
169557 break;
169558 case 's':
169559 if( !stem(&z, "msila", "al", m_gt_0)
169560 && !stem(&z, "ssenevi", "ive", m_gt_0)
169561 && !stem(&z, "ssenluf", "ful", m_gt_0)
169562 ){
169563 stem(&z, "ssensuo", "ous", m_gt_0);
169564 }
169565 break;
169566 case 't':
169567 if( !stem(&z, "itila", "al", m_gt_0)
169568 && !stem(&z, "itivi", "ive", m_gt_0)
169569 ){
169570 stem(&z, "itilib", "ble", m_gt_0);
169571 }
169572 break;
169573 }
169574
169575 /* Step 3 */
169576 switch( z[0] ){
169577 case 'e':
169578 if( !stem(&z, "etaci", "ic", m_gt_0)
169579 && !stem(&z, "evita", "", m_gt_0)
169580 ){
169581 stem(&z, "ezila", "al", m_gt_0);
169582 }
169583 break;
169584 case 'i':
169585 stem(&z, "itici", "ic", m_gt_0);
169586 break;
169587 case 'l':
169588 if( !stem(&z, "laci", "ic", m_gt_0) ){
169589 stem(&z, "luf", "", m_gt_0);
169590 }
169591 break;
169592 case 's':
169593 stem(&z, "ssen", "", m_gt_0);
169594 break;
169595 }
169596
169597 /* Step 4 */
169598 switch( z[1] ){
169599 case 'a':
169600 if( z[0]=='l' && m_gt_1(z+2) ){
169601 z += 2;
169602 }
169603 break;
169604 case 'c':
169605 if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){
169606 z += 4;
169607 }
169608 break;
169609 case 'e':
169610 if( z[0]=='r' && m_gt_1(z+2) ){
169611 z += 2;
169612 }
169613 break;
169614 case 'i':
169615 if( z[0]=='c' && m_gt_1(z+2) ){
169616 z += 2;
169617 }
169618 break;
169619 case 'l':
169620 if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
169621 z += 4;
169622 }
169623 break;
169624 case 'n':
169625 if( z[0]=='t' ){
169626 if( z[2]=='a' ){
169627 if( m_gt_1(z+3) ){
169628 z += 3;
169629 }
169630 }else if( z[2]=='e' ){
169631 if( !stem(&z, "tneme", "", m_gt_1)
169632 && !stem(&z, "tnem", "", m_gt_1)
169633 ){
169634 stem(&z, "tne", "", m_gt_1);
169635 }
169636 }
169637 }
169638 break;
169639 case 'o':
169640 if( z[0]=='u' ){
169641 if( m_gt_1(z+2) ){
169642 z += 2;
169643 }
169644 }else if( z[3]=='s' || z[3]=='t' ){
169645 stem(&z, "noi", "", m_gt_1);
169646 }
169647 break;
169648 case 's':
169649 if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
169650 z += 3;
169651 }
169652 break;
169653 case 't':
169654 if( !stem(&z, "eta", "", m_gt_1) ){
169655 stem(&z, "iti", "", m_gt_1);
169656 }
169657 break;
169658 case 'u':
169659 if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
169660 z += 3;
169661 }
169662 break;
169663 case 'v':
169664 case 'z':
169665 if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
169666 z += 3;
169667 }
169668 break;
169669 }
169670
169671 /* Step 5a */
169672 if( z[0]=='e' ){
169673 if( m_gt_1(z+1) ){
169674 z++;
169675 }else if( m_eq_1(z+1) && !star_oh(z+1) ){
169676 z++;
169677 }
169678 }
169679
169680 /* Step 5b */
169681 if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
169682 z++;
169683 }
169684
169685 /* z[] is now the stemmed word in reverse order. Flip it back
169686 ** around into forward order and return.
169687 */
169688 *pnOut = i = (int)strlen(z);
169689 zOut[i] = 0;
169690 while( *z ){
169691 zOut[--i] = *(z++);
169692 }
169693}
169694
169695/*
169696** Characters that can be part of a token. We assume any character
169697** whose value is greater than 0x80 (any UTF character) can be
169698** part of a token. In other words, delimiters all must have
169699** values of 0x7f or lower.
169700*/
169701static const char porterIdChar[] = {
169702/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
169703 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
169704 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
169705 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
169706 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
169707 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
169708};
169709#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
169710
169711/*
169712** Extract the next token from a tokenization cursor. The cursor must
169713** have been opened by a prior call to porterOpen().
169714*/
169715static int porterNext(
169716 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */
169717 const char **pzToken, /* OUT: *pzToken is the token text */
169718 int *pnBytes, /* OUT: Number of bytes in token */
169719 int *piStartOffset, /* OUT: Starting offset of token */
169720 int *piEndOffset, /* OUT: Ending offset of token */
169721 int *piPosition /* OUT: Position integer of token */
169722){
169723 porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
169724 const char *z = c->zInput;
169725
169726 while( c->iOffset<c->nInput ){
169727 int iStartOffset, ch;
169728
169729 /* Scan past delimiter characters */
169730 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
169731 c->iOffset++;
169732 }
169733
169734 /* Count non-delimiter characters. */
169735 iStartOffset = c->iOffset;
169736 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
169737 c->iOffset++;
169738 }
169739
169740 if( c->iOffset>iStartOffset ){
169741 int n = c->iOffset-iStartOffset;
169742 if( n>c->nAllocated ){
169743 char *pNew;
169744 c->nAllocated = n+20;
169745 pNew = sqlite3_realloc(c->zToken, c->nAllocated);
169746 if( !pNew ) return SQLITE_NOMEM7;
169747 c->zToken = pNew;
169748 }
169749 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
169750 *pzToken = c->zToken;
169751 *piStartOffset = iStartOffset;
169752 *piEndOffset = c->iOffset;
169753 *piPosition = c->iToken++;
169754 return SQLITE_OK0;
169755 }
169756 }
169757 return SQLITE_DONE101;
169758}
169759
169760/*
169761** The set of routines that implement the porter-stemmer tokenizer
169762*/
169763static const sqlite3_tokenizer_module porterTokenizerModule = {
169764 0,
169765 porterCreate,
169766 porterDestroy,
169767 porterOpen,
169768 porterClose,
169769 porterNext,
169770 0
169771};
169772
169773/*
169774** Allocate a new porter tokenizer. Return a pointer to the new
169775** tokenizer in *ppModule
169776*/
169777SQLITE_PRIVATEstatic void sqlite3Fts3PorterTokenizerModule(
169778 sqlite3_tokenizer_module const**ppModule
169779){
169780 *ppModule = &porterTokenizerModule;
169781}
169782
169783#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
169784
169785/************** End of fts3_porter.c *****************************************/
169786/************** Begin file fts3_tokenizer.c **********************************/
169787/*
169788** 2007 June 22
169789**
169790** The author disclaims copyright to this source code. In place of
169791** a legal notice, here is a blessing:
169792**
169793** May you do good and not evil.
169794** May you find forgiveness for yourself and forgive others.
169795** May you share freely, never taking more than you give.
169796**
169797******************************************************************************
169798**
169799** This is part of an SQLite module implementing full-text search.
169800** This particular file implements the generic tokenizer interface.
169801*/
169802
169803/*
169804** The code in this file is only compiled if:
169805**
169806** * The FTS3 module is being built as an extension
169807** (in which case SQLITE_CORE is not defined), or
169808**
169809** * The FTS3 module is being built into the core of
169810** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
169811*/
169812/* #include "fts3Int.h" */
169813#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
169814
169815/* #include <assert.h> */
169816/* #include <string.h> */
169817
169818/*
169819** Return true if the two-argument version of fts3_tokenizer()
169820** has been activated via a prior call to sqlite3_db_config(db,
169821** SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
169822*/
169823static int fts3TokenizerEnabled(sqlite3_context *context){
169824 sqlite3 *db = sqlite3_context_db_handle(context);
169825 int isEnabled = 0;
169826 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER1004,-1,&isEnabled);
169827 return isEnabled;
169828}
169829
169830/*
169831** Implementation of the SQL scalar function for accessing the underlying
169832** hash table. This function may be called as follows:
169833**
169834** SELECT <function-name>(<key-name>);
169835** SELECT <function-name>(<key-name>, <pointer>);
169836**
169837** where <function-name> is the name passed as the second argument
169838** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
169839**
169840** If the <pointer> argument is specified, it must be a blob value
169841** containing a pointer to be stored as the hash data corresponding
169842** to the string <key-name>. If <pointer> is not specified, then
169843** the string <key-name> must already exist in the has table. Otherwise,
169844** an error is returned.
169845**
169846** Whether or not the <pointer> argument is specified, the value returned
169847** is a blob containing the pointer stored as the hash data corresponding
169848** to string <key-name> (after the hash-table is updated, if applicable).
169849*/
169850static void fts3TokenizerFunc(
169851 sqlite3_context *context,
169852 int argc,
169853 sqlite3_value **argv
169854){
169855 Fts3Hash *pHash;
169856 void *pPtr = 0;
169857 const unsigned char *zName;
169858 int nName;
169859
169860 assert( argc==1 || argc==2 )((void) (0));
169861
169862 pHash = (Fts3Hash *)sqlite3_user_data(context);
169863
169864 zName = sqlite3_value_text(argv[0]);
169865 nName = sqlite3_value_bytes(argv[0])+1;
169866
169867 if( argc==2 ){
169868 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[1]) ){
169869 void *pOld;
169870 int n = sqlite3_value_bytes(argv[1]);
169871 if( zName==0 || n!=sizeof(pPtr) ){
169872 sqlite3_result_error(context, "argument type mismatch", -1);
169873 return;
169874 }
169875 pPtr = *(void **)sqlite3_value_blob(argv[1]);
169876 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
169877 if( pOld==pPtr ){
169878 sqlite3_result_error(context, "out of memory", -1);
169879 }
169880 }else{
169881 sqlite3_result_error(context, "fts3tokenize disabled", -1);
169882 return;
169883 }
169884 }else{
169885 if( zName ){
169886 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
169887 }
169888 if( !pPtr ){
169889 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
169890 sqlite3_result_error(context, zErr, -1);
169891 sqlite3_free(zErr);
169892 return;
169893 }
169894 }
169895 if( fts3TokenizerEnabled(context) || sqlite3_value_frombind(argv[0]) ){
169896 sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
169897 }
169898}
169899
169900SQLITE_PRIVATEstatic int sqlite3Fts3IsIdChar(char c){
169901 static const char isFtsIdChar[] = {
169902 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
169903 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
169904 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
169905 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
169906 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
169907 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
169908 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
169909 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
169910 };
169911 return (c&0x80 || isFtsIdChar[(int)(c)]);
169912}
169913
169914SQLITE_PRIVATEstatic const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
169915 const char *z1;
169916 const char *z2 = 0;
169917
169918 /* Find the start of the next token. */
169919 z1 = zStr;
169920 while( z2==0 ){
169921 char c = *z1;
169922 switch( c ){
169923 case '\0': return 0; /* No more tokens here */
169924 case '\'':
169925 case '"':
169926 case '`': {
169927 z2 = z1;
169928 while( *++z2 && (*z2!=c || *++z2==c) );
169929 break;
169930 }
169931 case '[':
169932 z2 = &z1[1];
169933 while( *z2 && z2[0]!=']' ) z2++;
169934 if( *z2 ) z2++;
169935 break;
169936
169937 default:
169938 if( sqlite3Fts3IsIdChar(*z1) ){
169939 z2 = &z1[1];
169940 while( sqlite3Fts3IsIdChar(*z2) ) z2++;
169941 }else{
169942 z1++;
169943 }
169944 }
169945 }
169946
169947 *pn = (int)(z2-z1);
169948 return z1;
169949}
169950
169951SQLITE_PRIVATEstatic int sqlite3Fts3InitTokenizer(
169952 Fts3Hash *pHash, /* Tokenizer hash table */
169953 const char *zArg, /* Tokenizer name */
169954 sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */
169955 char **pzErr /* OUT: Set to malloced error message */
169956){
169957 int rc;
169958 char *z = (char *)zArg;
169959 int n = 0;
169960 char *zCopy;
169961 char *zEnd; /* Pointer to nul-term of zCopy */
169962 sqlite3_tokenizer_module *m;
169963
169964 zCopy = sqlite3_mprintf("%s", zArg);
169965 if( !zCopy ) return SQLITE_NOMEM7;
169966 zEnd = &zCopy[strlen(zCopy)];
169967
169968 z = (char *)sqlite3Fts3NextToken(zCopy, &n);
169969 if( z==0 ){
169970 assert( n==0 )((void) (0));
169971 z = zCopy;
169972 }
169973 z[n] = '\0';
169974 sqlite3Fts3Dequote(z);
169975
169976 m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
169977 if( !m ){
169978 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z);
169979 rc = SQLITE_ERROR1;
169980 }else{
169981 char const **aArg = 0;
169982 int iArg = 0;
169983 z = &z[n+1];
169984 while( z<zEnd && (NULL((void*)0)!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
169985 sqlite3_int64 nNew = sizeof(char *)*(iArg+1);
169986 char const **aNew = (const char **)sqlite3_realloc64((void *)aArg, nNew);
169987 if( !aNew ){
169988 sqlite3_free(zCopy);
169989 sqlite3_free((void *)aArg);
169990 return SQLITE_NOMEM7;
169991 }
169992 aArg = aNew;
169993 aArg[iArg++] = z;
169994 z[n] = '\0';
169995 sqlite3Fts3Dequote(z);
169996 z = &z[n+1];
169997 }
169998 rc = m->xCreate(iArg, aArg, ppTok);
169999 assert( rc!=SQLITE_OK || *ppTok )((void) (0));
170000 if( rc!=SQLITE_OK0 ){
170001 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer");
170002 }else{
170003 (*ppTok)->pModule = m;
170004 }
170005 sqlite3_free((void *)aArg);
170006 }
170007
170008 sqlite3_free(zCopy);
170009 return rc;
170010}
170011
170012
170013#ifdef SQLITE_TEST
170014
170015#if defined(INCLUDE_SQLITE_TCL_H)
170016# include "sqlite_tcl.h"
170017#else
170018# include "tcl.h"
170019#endif
170020/* #include <string.h> */
170021
170022/*
170023** Implementation of a special SQL scalar function for testing tokenizers
170024** designed to be used in concert with the Tcl testing framework. This
170025** function must be called with two or more arguments:
170026**
170027** SELECT <function-name>(<key-name>, ..., <input-string>);
170028**
170029** where <function-name> is the name passed as the second argument
170030** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
170031** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
170032**
170033** The return value is a string that may be interpreted as a Tcl
170034** list. For each token in the <input-string>, three elements are
170035** added to the returned list. The first is the token position, the
170036** second is the token text (folded, stemmed, etc.) and the third is the
170037** substring of <input-string> associated with the token. For example,
170038** using the built-in "simple" tokenizer:
170039**
170040** SELECT fts_tokenizer_test('simple', 'I don't see how');
170041**
170042** will return the string:
170043**
170044** "{0 i I 1 dont don't 2 see see 3 how how}"
170045**
170046*/
170047static void testFunc(
170048 sqlite3_context *context,
170049 int argc,
170050 sqlite3_value **argv
170051){
170052 Fts3Hash *pHash;
170053 sqlite3_tokenizer_module *p;
170054 sqlite3_tokenizer *pTokenizer = 0;
170055 sqlite3_tokenizer_cursor *pCsr = 0;
170056
170057 const char *zErr = 0;
170058
170059 const char *zName;
170060 int nName;
170061 const char *zInput;
170062 int nInput;
170063
170064 const char *azArg[64];
170065
170066 const char *zToken;
170067 int nToken = 0;
170068 int iStart = 0;
170069 int iEnd = 0;
170070 int iPos = 0;
170071 int i;
170072
170073 Tcl_Obj *pRet;
170074
170075 if( argc<2 ){
170076 sqlite3_result_error(context, "insufficient arguments", -1);
170077 return;
170078 }
170079
170080 nName = sqlite3_value_bytes(argv[0]);
170081 zName = (const char *)sqlite3_value_text(argv[0]);
170082 nInput = sqlite3_value_bytes(argv[argc-1]);
170083 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
170084
170085 pHash = (Fts3Hash *)sqlite3_user_data(context);
170086 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
170087
170088 if( !p ){
170089 char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName);
170090 sqlite3_result_error(context, zErr2, -1);
170091 sqlite3_free(zErr2);
170092 return;
170093 }
170094
170095 pRet = Tcl_NewObj();
170096 Tcl_IncrRefCount(pRet);
170097
170098 for(i=1; i<argc-1; i++){
170099 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
170100 }
170101
170102 if( SQLITE_OK0!=p->xCreate(argc-2, azArg, &pTokenizer) ){
170103 zErr = "error in xCreate()";
170104 goto finish;
170105 }
170106 pTokenizer->pModule = p;
170107 if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
170108 zErr = "error in xOpen()";
170109 goto finish;
170110 }
170111
170112 while( SQLITE_OK0==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
170113 Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
170114 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
170115 zToken = &zInput[iStart];
170116 nToken = iEnd-iStart;
170117 Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
170118 }
170119
170120 if( SQLITE_OK0!=p->xClose(pCsr) ){
170121 zErr = "error in xClose()";
170122 goto finish;
170123 }
170124 if( SQLITE_OK0!=p->xDestroy(pTokenizer) ){
170125 zErr = "error in xDestroy()";
170126 goto finish;
170127 }
170128
170129finish:
170130 if( zErr ){
170131 sqlite3_result_error(context, zErr, -1);
170132 }else{
170133 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
170134 }
170135 Tcl_DecrRefCount(pRet);
170136}
170137
170138static
170139int registerTokenizer(
170140 sqlite3 *db,
170141 char *zName,
170142 const sqlite3_tokenizer_module *p
170143){
170144 int rc;
170145 sqlite3_stmt *pStmt;
170146 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
170147
170148 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
170149 if( rc!=SQLITE_OK0 ){
170150 return rc;
170151 }
170152
170153 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
170154 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC((sqlite3_destructor_type)0));
170155 sqlite3_step(pStmt);
170156
170157 return sqlite3_finalize(pStmt);
170158}
170159
170160
170161static
170162int queryTokenizer(
170163 sqlite3 *db,
170164 char *zName,
170165 const sqlite3_tokenizer_module **pp
170166){
170167 int rc;
170168 sqlite3_stmt *pStmt;
170169 const char zSql[] = "SELECT fts3_tokenizer(?)";
170170
170171 *pp = 0;
170172 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
170173 if( rc!=SQLITE_OK0 ){
170174 return rc;
170175 }
170176
170177 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
170178 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
170179 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB4 ){
170180 memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
170181 }
170182 }
170183
170184 return sqlite3_finalize(pStmt);
170185}
170186
170187SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
170188
170189/*
170190** Implementation of the scalar function fts3_tokenizer_internal_test().
170191** This function is used for testing only, it is not included in the
170192** build unless SQLITE_TEST is defined.
170193**
170194** The purpose of this is to test that the fts3_tokenizer() function
170195** can be used as designed by the C-code in the queryTokenizer and
170196** registerTokenizer() functions above. These two functions are repeated
170197** in the README.tokenizer file as an example, so it is important to
170198** test them.
170199**
170200** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
170201** function with no arguments. An assert() will fail if a problem is
170202** detected. i.e.:
170203**
170204** SELECT fts3_tokenizer_internal_test();
170205**
170206*/
170207static void intTestFunc(
170208 sqlite3_context *context,
170209 int argc,
170210 sqlite3_value **argv
170211){
170212 int rc;
170213 const sqlite3_tokenizer_module *p1;
170214 const sqlite3_tokenizer_module *p2;
170215 sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
170216
170217 UNUSED_PARAMETER(argc)(void)(argc);
170218 UNUSED_PARAMETER(argv)(void)(argv);
170219
170220 /* Test the query function */
170221 sqlite3Fts3SimpleTokenizerModule(&p1);
170222 rc = queryTokenizer(db, "simple", &p2);
170223 assert( rc==SQLITE_OK )((void) (0));
170224 assert( p1==p2 )((void) (0));
170225 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
170226 assert( rc==SQLITE_ERROR )((void) (0));
170227 assert( p2==0 )((void) (0));
170228 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") )((void) (0));
170229
170230 /* Test the storage function */
170231 if( fts3TokenizerEnabled(context) ){
170232 rc = registerTokenizer(db, "nosuchtokenizer", p1);
170233 assert( rc==SQLITE_OK )((void) (0));
170234 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
170235 assert( rc==SQLITE_OK )((void) (0));
170236 assert( p2==p1 )((void) (0));
170237 }
170238
170239 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC((sqlite3_destructor_type)0));
170240}
170241
170242#endif
170243
170244/*
170245** Set up SQL objects in database db used to access the contents of
170246** the hash table pointed to by argument pHash. The hash table must
170247** been initialized to use string keys, and to take a private copy
170248** of the key when a value is inserted. i.e. by a call similar to:
170249**
170250** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
170251**
170252** This function adds a scalar function (see header comment above
170253** fts3TokenizerFunc() in this file for details) and, if ENABLE_TABLE is
170254** defined at compilation time, a temporary virtual table (see header
170255** comment above struct HashTableVtab) to the database schema. Both
170256** provide read/write access to the contents of *pHash.
170257**
170258** The third argument to this function, zName, is used as the name
170259** of both the scalar and, if created, the virtual table.
170260*/
170261SQLITE_PRIVATEstatic int sqlite3Fts3InitHashTable(
170262 sqlite3 *db,
170263 Fts3Hash *pHash,
170264 const char *zName
170265){
170266 int rc = SQLITE_OK0;
170267 void *p = (void *)pHash;
170268 const int any = SQLITE_ANY5;
170269
170270#ifdef SQLITE_TEST
170271 char *zTest = 0;
170272 char *zTest2 = 0;
170273 void *pdb = (void *)db;
170274 zTest = sqlite3_mprintf("%s_test", zName);
170275 zTest2 = sqlite3_mprintf("%s_internal_test", zName);
170276 if( !zTest || !zTest2 ){
170277 rc = SQLITE_NOMEM7;
170278 }
170279#endif
170280
170281 if( SQLITE_OK0==rc ){
170282 rc = sqlite3_create_function(db, zName, 1, any, p, fts3TokenizerFunc, 0, 0);
170283 }
170284 if( SQLITE_OK0==rc ){
170285 rc = sqlite3_create_function(db, zName, 2, any, p, fts3TokenizerFunc, 0, 0);
170286 }
170287#ifdef SQLITE_TEST
170288 if( SQLITE_OK0==rc ){
170289 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
170290 }
170291 if( SQLITE_OK0==rc ){
170292 rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
170293 }
170294#endif
170295
170296#ifdef SQLITE_TEST
170297 sqlite3_free(zTest);
170298 sqlite3_free(zTest2);
170299#endif
170300
170301 return rc;
170302}
170303
170304#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
170305
170306/************** End of fts3_tokenizer.c **************************************/
170307/************** Begin file fts3_tokenizer1.c *********************************/
170308/*
170309** 2006 Oct 10
170310**
170311** The author disclaims copyright to this source code. In place of
170312** a legal notice, here is a blessing:
170313**
170314** May you do good and not evil.
170315** May you find forgiveness for yourself and forgive others.
170316** May you share freely, never taking more than you give.
170317**
170318******************************************************************************
170319**
170320** Implementation of the "simple" full-text-search tokenizer.
170321*/
170322
170323/*
170324** The code in this file is only compiled if:
170325**
170326** * The FTS3 module is being built as an extension
170327** (in which case SQLITE_CORE is not defined), or
170328**
170329** * The FTS3 module is being built into the core of
170330** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
170331*/
170332/* #include "fts3Int.h" */
170333#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
170334
170335/* #include <assert.h> */
170336/* #include <stdlib.h> */
170337/* #include <stdio.h> */
170338/* #include <string.h> */
170339
170340/* #include "fts3_tokenizer.h" */
170341
170342typedef struct simple_tokenizer {
170343 sqlite3_tokenizer base;
170344 char delim[128]; /* flag ASCII delimiters */
170345} simple_tokenizer;
170346
170347typedef struct simple_tokenizer_cursor {
170348 sqlite3_tokenizer_cursor base;
170349 const char *pInput; /* input we are tokenizing */
170350 int nBytes; /* size of the input */
170351 int iOffset; /* current position in pInput */
170352 int iToken; /* index of next token to be returned */
170353 char *pToken; /* storage for current token */
170354 int nTokenAllocated; /* space allocated to zToken buffer */
170355} simple_tokenizer_cursor;
170356
170357
170358static int simpleDelim(simple_tokenizer *t, unsigned char c){
170359 return c<0x80 && t->delim[c];
170360}
170361static int fts3_isalnum(int x){
170362 return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
170363}
170364
170365/*
170366** Create a new tokenizer instance.
170367*/
170368static int simpleCreate(
170369 int argc, const char * const *argv,
170370 sqlite3_tokenizer **ppTokenizer
170371){
170372 simple_tokenizer *t;
170373
170374 t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
170375 if( t==NULL((void*)0) ) return SQLITE_NOMEM7;
170376 memset(t, 0, sizeof(*t));
170377
170378 /* TODO(shess) Delimiters need to remain the same from run to run,
170379 ** else we need to reindex. One solution would be a meta-table to
170380 ** track such information in the database, then we'd only want this
170381 ** information on the initial create.
170382 */
170383 if( argc>1 ){
170384 int i, n = (int)strlen(argv[1]);
170385 for(i=0; i<n; i++){
170386 unsigned char ch = argv[1][i];
170387 /* We explicitly don't support UTF-8 delimiters for now. */
170388 if( ch>=0x80 ){
170389 sqlite3_free(t);
170390 return SQLITE_ERROR1;
170391 }
170392 t->delim[ch] = 1;
170393 }
170394 } else {
170395 /* Mark non-alphanumeric ASCII characters as delimiters */
170396 int i;
170397 for(i=1; i<0x80; i++){
170398 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
170399 }
170400 }
170401
170402 *ppTokenizer = &t->base;
170403 return SQLITE_OK0;
170404}
170405
170406/*
170407** Destroy a tokenizer
170408*/
170409static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
170410 sqlite3_free(pTokenizer);
170411 return SQLITE_OK0;
170412}
170413
170414/*
170415** Prepare to begin tokenizing a particular string. The input
170416** string to be tokenized is pInput[0..nBytes-1]. A cursor
170417** used to incrementally tokenize this string is returned in
170418** *ppCursor.
170419*/
170420static int simpleOpen(
170421 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
170422 const char *pInput, int nBytes, /* String to be tokenized */
170423 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
170424){
170425 simple_tokenizer_cursor *c;
170426
170427 UNUSED_PARAMETER(pTokenizer)(void)(pTokenizer);
170428
170429 c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
170430 if( c==NULL((void*)0) ) return SQLITE_NOMEM7;
170431
170432 c->pInput = pInput;
170433 if( pInput==0 ){
170434 c->nBytes = 0;
170435 }else if( nBytes<0 ){
170436 c->nBytes = (int)strlen(pInput);
170437 }else{
170438 c->nBytes = nBytes;
170439 }
170440 c->iOffset = 0; /* start tokenizing at the beginning */
170441 c->iToken = 0;
170442 c->pToken = NULL((void*)0); /* no space allocated, yet. */
170443 c->nTokenAllocated = 0;
170444
170445 *ppCursor = &c->base;
170446 return SQLITE_OK0;
170447}
170448
170449/*
170450** Close a tokenization cursor previously opened by a call to
170451** simpleOpen() above.
170452*/
170453static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
170454 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
170455 sqlite3_free(c->pToken);
170456 sqlite3_free(c);
170457 return SQLITE_OK0;
170458}
170459
170460/*
170461** Extract the next token from a tokenization cursor. The cursor must
170462** have been opened by a prior call to simpleOpen().
170463*/
170464static int simpleNext(
170465 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
170466 const char **ppToken, /* OUT: *ppToken is the token text */
170467 int *pnBytes, /* OUT: Number of bytes in token */
170468 int *piStartOffset, /* OUT: Starting offset of token */
170469 int *piEndOffset, /* OUT: Ending offset of token */
170470 int *piPosition /* OUT: Position integer of token */
170471){
170472 simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
170473 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
170474 unsigned char *p = (unsigned char *)c->pInput;
170475
170476 while( c->iOffset<c->nBytes ){
170477 int iStartOffset;
170478
170479 /* Scan past delimiter characters */
170480 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
170481 c->iOffset++;
170482 }
170483
170484 /* Count non-delimiter characters. */
170485 iStartOffset = c->iOffset;
170486 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
170487 c->iOffset++;
170488 }
170489
170490 if( c->iOffset>iStartOffset ){
170491 int i, n = c->iOffset-iStartOffset;
170492 if( n>c->nTokenAllocated ){
170493 char *pNew;
170494 c->nTokenAllocated = n+20;
170495 pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
170496 if( !pNew ) return SQLITE_NOMEM7;
170497 c->pToken = pNew;
170498 }
170499 for(i=0; i<n; i++){
170500 /* TODO(shess) This needs expansion to handle UTF-8
170501 ** case-insensitivity.
170502 */
170503 unsigned char ch = p[iStartOffset+i];
170504 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
170505 }
170506 *ppToken = c->pToken;
170507 *pnBytes = n;
170508 *piStartOffset = iStartOffset;
170509 *piEndOffset = c->iOffset;
170510 *piPosition = c->iToken++;
170511
170512 return SQLITE_OK0;
170513 }
170514 }
170515 return SQLITE_DONE101;
170516}
170517
170518/*
170519** The set of routines that implement the simple tokenizer
170520*/
170521static const sqlite3_tokenizer_module simpleTokenizerModule = {
170522 0,
170523 simpleCreate,
170524 simpleDestroy,
170525 simpleOpen,
170526 simpleClose,
170527 simpleNext,
170528 0,
170529};
170530
170531/*
170532** Allocate a new simple tokenizer. Return a pointer to the new
170533** tokenizer in *ppModule
170534*/
170535SQLITE_PRIVATEstatic void sqlite3Fts3SimpleTokenizerModule(
170536 sqlite3_tokenizer_module const**ppModule
170537){
170538 *ppModule = &simpleTokenizerModule;
170539}
170540
170541#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
170542
170543/************** End of fts3_tokenizer1.c *************************************/
170544/************** Begin file fts3_tokenize_vtab.c ******************************/
170545/*
170546** 2013 Apr 22
170547**
170548** The author disclaims copyright to this source code. In place of
170549** a legal notice, here is a blessing:
170550**
170551** May you do good and not evil.
170552** May you find forgiveness for yourself and forgive others.
170553** May you share freely, never taking more than you give.
170554**
170555******************************************************************************
170556**
170557** This file contains code for the "fts3tokenize" virtual table module.
170558** An fts3tokenize virtual table is created as follows:
170559**
170560** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
170561** <tokenizer-name>, <arg-1>, ...
170562** );
170563**
170564** The table created has the following schema:
170565**
170566** CREATE TABLE <tbl>(input, token, start, end, position)
170567**
170568** When queried, the query must include a WHERE clause of type:
170569**
170570** input = <string>
170571**
170572** The virtual table module tokenizes this <string>, using the FTS3
170573** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
170574** statement and returns one row for each token in the result. With
170575** fields set as follows:
170576**
170577** input: Always set to a copy of <string>
170578** token: A token from the input.
170579** start: Byte offset of the token within the input <string>.
170580** end: Byte offset of the byte immediately following the end of the
170581** token within the input string.
170582** pos: Token offset of token within input.
170583**
170584*/
170585/* #include "fts3Int.h" */
170586#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
170587
170588/* #include <string.h> */
170589/* #include <assert.h> */
170590
170591typedef struct Fts3tokTable Fts3tokTable;
170592typedef struct Fts3tokCursor Fts3tokCursor;
170593
170594/*
170595** Virtual table structure.
170596*/
170597struct Fts3tokTable {
170598 sqlite3_vtab base; /* Base class used by SQLite core */
170599 const sqlite3_tokenizer_module *pMod;
170600 sqlite3_tokenizer *pTok;
170601};
170602
170603/*
170604** Virtual table cursor structure.
170605*/
170606struct Fts3tokCursor {
170607 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
170608 char *zInput; /* Input string */
170609 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
170610 int iRowid; /* Current 'rowid' value */
170611 const char *zToken; /* Current 'token' value */
170612 int nToken; /* Size of zToken in bytes */
170613 int iStart; /* Current 'start' value */
170614 int iEnd; /* Current 'end' value */
170615 int iPos; /* Current 'pos' value */
170616};
170617
170618/*
170619** Query FTS for the tokenizer implementation named zName.
170620*/
170621static int fts3tokQueryTokenizer(
170622 Fts3Hash *pHash,
170623 const char *zName,
170624 const sqlite3_tokenizer_module **pp,
170625 char **pzErr
170626){
170627 sqlite3_tokenizer_module *p;
170628 int nName = (int)strlen(zName);
170629
170630 p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
170631 if( !p ){
170632 sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName);
170633 return SQLITE_ERROR1;
170634 }
170635
170636 *pp = p;
170637 return SQLITE_OK0;
170638}
170639
170640/*
170641** The second argument, argv[], is an array of pointers to nul-terminated
170642** strings. This function makes a copy of the array and strings into a
170643** single block of memory. It then dequotes any of the strings that appear
170644** to be quoted.
170645**
170646** If successful, output parameter *pazDequote is set to point at the
170647** array of dequoted strings and SQLITE_OK is returned. The caller is
170648** responsible for eventually calling sqlite3_free() to free the array
170649** in this case. Or, if an error occurs, an SQLite error code is returned.
170650** The final value of *pazDequote is undefined in this case.
170651*/
170652static int fts3tokDequoteArray(
170653 int argc, /* Number of elements in argv[] */
170654 const char * const *argv, /* Input array */
170655 char ***pazDequote /* Output array */
170656){
170657 int rc = SQLITE_OK0; /* Return code */
170658 if( argc==0 ){
170659 *pazDequote = 0;
170660 }else{
170661 int i;
170662 int nByte = 0;
170663 char **azDequote;
170664
170665 for(i=0; i<argc; i++){
170666 nByte += (int)(strlen(argv[i]) + 1);
170667 }
170668
170669 *pazDequote = azDequote = sqlite3_malloc64(sizeof(char *)*argc + nByte);
170670 if( azDequote==0 ){
170671 rc = SQLITE_NOMEM7;
170672 }else{
170673 char *pSpace = (char *)&azDequote[argc];
170674 for(i=0; i<argc; i++){
170675 int n = (int)strlen(argv[i]);
170676 azDequote[i] = pSpace;
170677 memcpy(pSpace, argv[i], n+1);
170678 sqlite3Fts3Dequote(pSpace);
170679 pSpace += (n+1);
170680 }
170681 }
170682 }
170683
170684 return rc;
170685}
170686
170687/*
170688** Schema of the tokenizer table.
170689*/
170690#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
170691
170692/*
170693** This function does all the work for both the xConnect and xCreate methods.
170694** These tables have no persistent representation of their own, so xConnect
170695** and xCreate are identical operations.
170696**
170697** argv[0]: module name
170698** argv[1]: database name
170699** argv[2]: table name
170700** argv[3]: first argument (tokenizer name)
170701*/
170702static int fts3tokConnectMethod(
170703 sqlite3 *db, /* Database connection */
170704 void *pHash, /* Hash table of tokenizers */
170705 int argc, /* Number of elements in argv array */
170706 const char * const *argv, /* xCreate/xConnect argument array */
170707 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
170708 char **pzErr /* OUT: sqlite3_malloc'd error message */
170709){
170710 Fts3tokTable *pTab = 0;
170711 const sqlite3_tokenizer_module *pMod = 0;
170712 sqlite3_tokenizer *pTok = 0;
170713 int rc;
170714 char **azDequote = 0;
170715 int nDequote;
170716
170717 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
170718 if( rc!=SQLITE_OK0 ) return rc;
170719
170720 nDequote = argc-3;
170721 rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
170722
170723 if( rc==SQLITE_OK0 ){
170724 const char *zModule;
170725 if( nDequote<1 ){
170726 zModule = "simple";
170727 }else{
170728 zModule = azDequote[0];
170729 }
170730 rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
170731 }
170732
170733 assert( (rc==SQLITE_OK)==(pMod!=0) )((void) (0));
170734 if( rc==SQLITE_OK0 ){
170735 const char * const *azArg = (const char * const *)&azDequote[1];
170736 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
170737 }
170738
170739 if( rc==SQLITE_OK0 ){
170740 pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
170741 if( pTab==0 ){
170742 rc = SQLITE_NOMEM7;
170743 }
170744 }
170745
170746 if( rc==SQLITE_OK0 ){
170747 memset(pTab, 0, sizeof(Fts3tokTable));
170748 pTab->pMod = pMod;
170749 pTab->pTok = pTok;
170750 *ppVtab = &pTab->base;
170751 }else{
170752 if( pTok ){
170753 pMod->xDestroy(pTok);
170754 }
170755 }
170756
170757 sqlite3_free(azDequote);
170758 return rc;
170759}
170760
170761/*
170762** This function does the work for both the xDisconnect and xDestroy methods.
170763** These tables have no persistent representation of their own, so xDisconnect
170764** and xDestroy are identical operations.
170765*/
170766static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
170767 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
170768
170769 pTab->pMod->xDestroy(pTab->pTok);
170770 sqlite3_free(pTab);
170771 return SQLITE_OK0;
170772}
170773
170774/*
170775** xBestIndex - Analyze a WHERE and ORDER BY clause.
170776*/
170777static int fts3tokBestIndexMethod(
170778 sqlite3_vtab *pVTab,
170779 sqlite3_index_info *pInfo
170780){
170781 int i;
170782 UNUSED_PARAMETER(pVTab)(void)(pVTab);
170783
170784 for(i=0; i<pInfo->nConstraint; i++){
170785 if( pInfo->aConstraint[i].usable
170786 && pInfo->aConstraint[i].iColumn==0
170787 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ2
170788 ){
170789 pInfo->idxNum = 1;
170790 pInfo->aConstraintUsage[i].argvIndex = 1;
170791 pInfo->aConstraintUsage[i].omit = 1;
170792 pInfo->estimatedCost = 1;
170793 return SQLITE_OK0;
170794 }
170795 }
170796
170797 pInfo->idxNum = 0;
170798 assert( pInfo->estimatedCost>1000000.0 )((void) (0));
170799
170800 return SQLITE_OK0;
170801}
170802
170803/*
170804** xOpen - Open a cursor.
170805*/
170806static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
170807 Fts3tokCursor *pCsr;
170808 UNUSED_PARAMETER(pVTab)(void)(pVTab);
170809
170810 pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
170811 if( pCsr==0 ){
170812 return SQLITE_NOMEM7;
170813 }
170814 memset(pCsr, 0, sizeof(Fts3tokCursor));
170815
170816 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
170817 return SQLITE_OK0;
170818}
170819
170820/*
170821** Reset the tokenizer cursor passed as the only argument. As if it had
170822** just been returned by fts3tokOpenMethod().
170823*/
170824static void fts3tokResetCursor(Fts3tokCursor *pCsr){
170825 if( pCsr->pCsr ){
170826 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
170827 pTab->pMod->xClose(pCsr->pCsr);
170828 pCsr->pCsr = 0;
170829 }
170830 sqlite3_free(pCsr->zInput);
170831 pCsr->zInput = 0;
170832 pCsr->zToken = 0;
170833 pCsr->nToken = 0;
170834 pCsr->iStart = 0;
170835 pCsr->iEnd = 0;
170836 pCsr->iPos = 0;
170837 pCsr->iRowid = 0;
170838}
170839
170840/*
170841** xClose - Close a cursor.
170842*/
170843static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
170844 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
170845
170846 fts3tokResetCursor(pCsr);
170847 sqlite3_free(pCsr);
170848 return SQLITE_OK0;
170849}
170850
170851/*
170852** xNext - Advance the cursor to the next row, if any.
170853*/
170854static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
170855 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
170856 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
170857 int rc; /* Return code */
170858
170859 pCsr->iRowid++;
170860 rc = pTab->pMod->xNext(pCsr->pCsr,
170861 &pCsr->zToken, &pCsr->nToken,
170862 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
170863 );
170864
170865 if( rc!=SQLITE_OK0 ){
170866 fts3tokResetCursor(pCsr);
170867 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
170868 }
170869
170870 return rc;
170871}
170872
170873/*
170874** xFilter - Initialize a cursor to point at the start of its data.
170875*/
170876static int fts3tokFilterMethod(
170877 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
170878 int idxNum, /* Strategy index */
170879 const char *idxStr, /* Unused */
170880 int nVal, /* Number of elements in apVal */
170881 sqlite3_value **apVal /* Arguments for the indexing scheme */
170882){
170883 int rc = SQLITE_ERROR1;
170884 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
170885 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
170886 UNUSED_PARAMETER(idxStr)(void)(idxStr);
170887 UNUSED_PARAMETER(nVal)(void)(nVal);
170888
170889 fts3tokResetCursor(pCsr);
170890 if( idxNum==1 ){
170891 const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
170892 int nByte = sqlite3_value_bytes(apVal[0]);
170893 pCsr->zInput = sqlite3_malloc64(nByte+1);
170894 if( pCsr->zInput==0 ){
170895 rc = SQLITE_NOMEM7;
170896 }else{
170897 memcpy(pCsr->zInput, zByte, nByte);
170898 pCsr->zInput[nByte] = 0;
170899 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
170900 if( rc==SQLITE_OK0 ){
170901 pCsr->pCsr->pTokenizer = pTab->pTok;
170902 }
170903 }
170904 }
170905
170906 if( rc!=SQLITE_OK0 ) return rc;
170907 return fts3tokNextMethod(pCursor);
170908}
170909
170910/*
170911** xEof - Return true if the cursor is at EOF, or false otherwise.
170912*/
170913static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
170914 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
170915 return (pCsr->zToken==0);
170916}
170917
170918/*
170919** xColumn - Return a column value.
170920*/
170921static int fts3tokColumnMethod(
170922 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
170923 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
170924 int iCol /* Index of column to read value from */
170925){
170926 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
170927
170928 /* CREATE TABLE x(input, token, start, end, position) */
170929 switch( iCol ){
170930 case 0:
170931 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
170932 break;
170933 case 1:
170934 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
170935 break;
170936 case 2:
170937 sqlite3_result_int(pCtx, pCsr->iStart);
170938 break;
170939 case 3:
170940 sqlite3_result_int(pCtx, pCsr->iEnd);
170941 break;
170942 default:
170943 assert( iCol==4 )((void) (0));
170944 sqlite3_result_int(pCtx, pCsr->iPos);
170945 break;
170946 }
170947 return SQLITE_OK0;
170948}
170949
170950/*
170951** xRowid - Return the current rowid for the cursor.
170952*/
170953static int fts3tokRowidMethod(
170954 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
170955 sqlite_int64 *pRowid /* OUT: Rowid value */
170956){
170957 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
170958 *pRowid = (sqlite3_int64)pCsr->iRowid;
170959 return SQLITE_OK0;
170960}
170961
170962/*
170963** Register the fts3tok module with database connection db. Return SQLITE_OK
170964** if successful or an error code if sqlite3_create_module() fails.
170965*/
170966SQLITE_PRIVATEstatic int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
170967 static const sqlite3_module fts3tok_module = {
170968 0, /* iVersion */
170969 fts3tokConnectMethod, /* xCreate */
170970 fts3tokConnectMethod, /* xConnect */
170971 fts3tokBestIndexMethod, /* xBestIndex */
170972 fts3tokDisconnectMethod, /* xDisconnect */
170973 fts3tokDisconnectMethod, /* xDestroy */
170974 fts3tokOpenMethod, /* xOpen */
170975 fts3tokCloseMethod, /* xClose */
170976 fts3tokFilterMethod, /* xFilter */
170977 fts3tokNextMethod, /* xNext */
170978 fts3tokEofMethod, /* xEof */
170979 fts3tokColumnMethod, /* xColumn */
170980 fts3tokRowidMethod, /* xRowid */
170981 0, /* xUpdate */
170982 0, /* xBegin */
170983 0, /* xSync */
170984 0, /* xCommit */
170985 0, /* xRollback */
170986 0, /* xFindFunction */
170987 0, /* xRename */
170988 0, /* xSavepoint */
170989 0, /* xRelease */
170990 0, /* xRollbackTo */
170991 0 /* xShadowName */
170992 };
170993 int rc; /* Return code */
170994
170995 rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
170996 return rc;
170997}
170998
170999#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
171000
171001/************** End of fts3_tokenize_vtab.c **********************************/
171002/************** Begin file fts3_write.c **************************************/
171003/*
171004** 2009 Oct 23
171005**
171006** The author disclaims copyright to this source code. In place of
171007** a legal notice, here is a blessing:
171008**
171009** May you do good and not evil.
171010** May you find forgiveness for yourself and forgive others.
171011** May you share freely, never taking more than you give.
171012**
171013******************************************************************************
171014**
171015** This file is part of the SQLite FTS3 extension module. Specifically,
171016** this file contains code to insert, update and delete rows from FTS3
171017** tables. It also contains code to merge FTS3 b-tree segments. Some
171018** of the sub-routines used to merge segments are also used by the query
171019** code in fts3.c.
171020*/
171021
171022/* #include "fts3Int.h" */
171023#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
171024
171025/* #include <string.h> */
171026/* #include <assert.h> */
171027/* #include <stdlib.h> */
171028
171029
171030#define FTS_MAX_APPENDABLE_HEIGHT 16
171031
171032/*
171033** When full-text index nodes are loaded from disk, the buffer that they
171034** are loaded into has the following number of bytes of padding at the end
171035** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
171036** of 920 bytes is allocated for it.
171037**
171038** This means that if we have a pointer into a buffer containing node data,
171039** it is always safe to read up to two varints from it without risking an
171040** overread, even if the node data is corrupted.
171041*/
171042#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
171043
171044/*
171045** Under certain circumstances, b-tree nodes (doclists) can be loaded into
171046** memory incrementally instead of all at once. This can be a big performance
171047** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
171048** method before retrieving all query results (as may happen, for example,
171049** if a query has a LIMIT clause).
171050**
171051** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
171052** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
171053** The code is written so that the hard lower-limit for each of these values
171054** is 1. Clearly such small values would be inefficient, but can be useful
171055** for testing purposes.
171056**
171057** If this module is built with SQLITE_TEST defined, these constants may
171058** be overridden at runtime for testing purposes. File fts3_test.c contains
171059** a Tcl interface to read and write the values.
171060*/
171061#ifdef SQLITE_TEST
171062int test_fts3_node_chunksize = (4*1024);
171063int test_fts3_node_chunk_threshold = (4*1024)*4;
171064# define FTS3_NODE_CHUNKSIZE test_fts3_node_chunksize
171065# define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
171066#else
171067# define FTS3_NODE_CHUNKSIZE (4*1024)
171068# define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
171069#endif
171070
171071/*
171072** The two values that may be meaningfully bound to the :1 parameter in
171073** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
171074*/
171075#define FTS_STAT_DOCTOTAL 0
171076#define FTS_STAT_INCRMERGEHINT 1
171077#define FTS_STAT_AUTOINCRMERGE 2
171078
171079/*
171080** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
171081** and incremental merge operation that takes place. This is used for
171082** debugging FTS only, it should not usually be turned on in production
171083** systems.
171084*/
171085#ifdef FTS3_LOG_MERGES
171086static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
171087 sqlite3_log(SQLITE_OK0, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
171088}
171089#else
171090#define fts3LogMerge(x, y)
171091#endif
171092
171093
171094typedef struct PendingList PendingList;
171095typedef struct SegmentNode SegmentNode;
171096typedef struct SegmentWriter SegmentWriter;
171097
171098/*
171099** An instance of the following data structure is used to build doclists
171100** incrementally. See function fts3PendingListAppend() for details.
171101*/
171102struct PendingList {
171103 int nData;
171104 char *aData;
171105 int nSpace;
171106 sqlite3_int64 iLastDocid;
171107 sqlite3_int64 iLastCol;
171108 sqlite3_int64 iLastPos;
171109};
171110
171111
171112/*
171113** Each cursor has a (possibly empty) linked list of the following objects.
171114*/
171115struct Fts3DeferredToken {
171116 Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */
171117 int iCol; /* Column token must occur in */
171118 Fts3DeferredToken *pNext; /* Next in list of deferred tokens */
171119 PendingList *pList; /* Doclist is assembled here */
171120};
171121
171122/*
171123** An instance of this structure is used to iterate through the terms on
171124** a contiguous set of segment b-tree leaf nodes. Although the details of
171125** this structure are only manipulated by code in this file, opaque handles
171126** of type Fts3SegReader* are also used by code in fts3.c to iterate through
171127** terms when querying the full-text index. See functions:
171128**
171129** sqlite3Fts3SegReaderNew()
171130** sqlite3Fts3SegReaderFree()
171131** sqlite3Fts3SegReaderIterate()
171132**
171133** Methods used to manipulate Fts3SegReader structures:
171134**
171135** fts3SegReaderNext()
171136** fts3SegReaderFirstDocid()
171137** fts3SegReaderNextDocid()
171138*/
171139struct Fts3SegReader {
171140 int iIdx; /* Index within level, or 0x7FFFFFFF for PT */
171141 u8 bLookup; /* True for a lookup only */
171142 u8 rootOnly; /* True for a root-only reader */
171143
171144 sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */
171145 sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */
171146 sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */
171147 sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */
171148
171149 char *aNode; /* Pointer to node data (or NULL) */
171150 int nNode; /* Size of buffer at aNode (or 0) */
171151 int nPopulate; /* If >0, bytes of buffer aNode[] loaded */
171152 sqlite3_blob *pBlob; /* If not NULL, blob handle to read node */
171153
171154 Fts3HashElem **ppNextElem;
171155
171156 /* Variables set by fts3SegReaderNext(). These may be read directly
171157 ** by the caller. They are valid from the time SegmentReaderNew() returns
171158 ** until SegmentReaderNext() returns something other than SQLITE_OK
171159 ** (i.e. SQLITE_DONE).
171160 */
171161 int nTerm; /* Number of bytes in current term */
171162 char *zTerm; /* Pointer to current term */
171163 int nTermAlloc; /* Allocated size of zTerm buffer */
171164 char *aDoclist; /* Pointer to doclist of current entry */
171165 int nDoclist; /* Size of doclist in current entry */
171166
171167 /* The following variables are used by fts3SegReaderNextDocid() to iterate
171168 ** through the current doclist (aDoclist/nDoclist).
171169 */
171170 char *pOffsetList;
171171 int nOffsetList; /* For descending pending seg-readers only */
171172 sqlite3_int64 iDocid;
171173};
171174
171175#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
171176#define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
171177
171178/*
171179** An instance of this structure is used to create a segment b-tree in the
171180** database. The internal details of this type are only accessed by the
171181** following functions:
171182**
171183** fts3SegWriterAdd()
171184** fts3SegWriterFlush()
171185** fts3SegWriterFree()
171186*/
171187struct SegmentWriter {
171188 SegmentNode *pTree; /* Pointer to interior tree structure */
171189 sqlite3_int64 iFirst; /* First slot in %_segments written */
171190 sqlite3_int64 iFree; /* Next free slot in %_segments */
171191 char *zTerm; /* Pointer to previous term buffer */
171192 int nTerm; /* Number of bytes in zTerm */
171193 int nMalloc; /* Size of malloc'd buffer at zMalloc */
171194 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
171195 int nSize; /* Size of allocation at aData */
171196 int nData; /* Bytes of data in aData */
171197 char *aData; /* Pointer to block from malloc() */
171198 i64 nLeafData; /* Number of bytes of leaf data written */
171199};
171200
171201/*
171202** Type SegmentNode is used by the following three functions to create
171203** the interior part of the segment b+-tree structures (everything except
171204** the leaf nodes). These functions and type are only ever used by code
171205** within the fts3SegWriterXXX() family of functions described above.
171206**
171207** fts3NodeAddTerm()
171208** fts3NodeWrite()
171209** fts3NodeFree()
171210**
171211** When a b+tree is written to the database (either as a result of a merge
171212** or the pending-terms table being flushed), leaves are written into the
171213** database file as soon as they are completely populated. The interior of
171214** the tree is assembled in memory and written out only once all leaves have
171215** been populated and stored. This is Ok, as the b+-tree fanout is usually
171216** very large, meaning that the interior of the tree consumes relatively
171217** little memory.
171218*/
171219struct SegmentNode {
171220 SegmentNode *pParent; /* Parent node (or NULL for root node) */
171221 SegmentNode *pRight; /* Pointer to right-sibling */
171222 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
171223 int nEntry; /* Number of terms written to node so far */
171224 char *zTerm; /* Pointer to previous term buffer */
171225 int nTerm; /* Number of bytes in zTerm */
171226 int nMalloc; /* Size of malloc'd buffer at zMalloc */
171227 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
171228 int nData; /* Bytes of valid data so far */
171229 char *aData; /* Node data */
171230};
171231
171232/*
171233** Valid values for the second argument to fts3SqlStmt().
171234*/
171235#define SQL_DELETE_CONTENT 0
171236#define SQL_IS_EMPTY 1
171237#define SQL_DELETE_ALL_CONTENT 2
171238#define SQL_DELETE_ALL_SEGMENTS 3
171239#define SQL_DELETE_ALL_SEGDIR 4
171240#define SQL_DELETE_ALL_DOCSIZE 5
171241#define SQL_DELETE_ALL_STAT 6
171242#define SQL_SELECT_CONTENT_BY_ROWID 7
171243#define SQL_NEXT_SEGMENT_INDEX 8
171244#define SQL_INSERT_SEGMENTS 9
171245#define SQL_NEXT_SEGMENTS_ID 10
171246#define SQL_INSERT_SEGDIR 11
171247#define SQL_SELECT_LEVEL 12
171248#define SQL_SELECT_LEVEL_RANGE 13
171249#define SQL_SELECT_LEVEL_COUNT 14
171250#define SQL_SELECT_SEGDIR_MAX_LEVEL 15
171251#define SQL_DELETE_SEGDIR_LEVEL 16
171252#define SQL_DELETE_SEGMENTS_RANGE 17
171253#define SQL_CONTENT_INSERT 18
171254#define SQL_DELETE_DOCSIZE 19
171255#define SQL_REPLACE_DOCSIZE 20
171256#define SQL_SELECT_DOCSIZE 21
171257#define SQL_SELECT_STAT 22
171258#define SQL_REPLACE_STAT 23
171259
171260#define SQL_SELECT_ALL_PREFIX_LEVEL 24
171261#define SQL_DELETE_ALL_TERMS_SEGDIR 25
171262#define SQL_DELETE_SEGDIR_RANGE 26
171263#define SQL_SELECT_ALL_LANGID 27
171264#define SQL_FIND_MERGE_LEVEL 28
171265#define SQL_MAX_LEAF_NODE_ESTIMATE 29
171266#define SQL_DELETE_SEGDIR_ENTRY 30
171267#define SQL_SHIFT_SEGDIR_ENTRY 31
171268#define SQL_SELECT_SEGDIR 32
171269#define SQL_CHOMP_SEGDIR 33
171270#define SQL_SEGMENT_IS_APPENDABLE 34
171271#define SQL_SELECT_INDEXES 35
171272#define SQL_SELECT_MXLEVEL 36
171273
171274#define SQL_SELECT_LEVEL_RANGE2 37
171275#define SQL_UPDATE_LEVEL_IDX 38
171276#define SQL_UPDATE_LEVEL 39
171277
171278/*
171279** This function is used to obtain an SQLite prepared statement handle
171280** for the statement identified by the second argument. If successful,
171281** *pp is set to the requested statement handle and SQLITE_OK returned.
171282** Otherwise, an SQLite error code is returned and *pp is set to 0.
171283**
171284** If argument apVal is not NULL, then it must point to an array with
171285** at least as many entries as the requested statement has bound
171286** parameters. The values are bound to the statements parameters before
171287** returning.
171288*/
171289static int fts3SqlStmt(
171290 Fts3Table *p, /* Virtual table handle */
171291 int eStmt, /* One of the SQL_XXX constants above */
171292 sqlite3_stmt **pp, /* OUT: Statement handle */
171293 sqlite3_value **apVal /* Values to bind to statement */
171294){
171295 const char *azSql[] = {
171296/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
171297/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
171298/* 2 */ "DELETE FROM %Q.'%q_content'",
171299/* 3 */ "DELETE FROM %Q.'%q_segments'",
171300/* 4 */ "DELETE FROM %Q.'%q_segdir'",
171301/* 5 */ "DELETE FROM %Q.'%q_docsize'",
171302/* 6 */ "DELETE FROM %Q.'%q_stat'",
171303/* 7 */ "SELECT %s WHERE rowid=?",
171304/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
171305/* 9 */ "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
171306/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
171307/* 11 */ "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
171308
171309 /* Return segments in order from oldest to newest.*/
171310/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
171311 "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
171312/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
171313 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
171314 "ORDER BY level DESC, idx ASC",
171315
171316/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
171317/* 15 */ "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
171318
171319/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
171320/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
171321/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)",
171322/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
171323/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
171324/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
171325/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=?",
171326/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
171327/* 24 */ "",
171328/* 25 */ "",
171329
171330/* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
171331/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'",
171332
171333/* This statement is used to determine which level to read the input from
171334** when performing an incremental merge. It returns the absolute level number
171335** of the oldest level in the db that contains at least ? segments. Or,
171336** if no level in the FTS index contains more than ? segments, the statement
171337** returns zero rows. */
171338/* 28 */ "SELECT level, count(*) AS cnt FROM %Q.'%q_segdir' "
171339 " GROUP BY level HAVING cnt>=?"
171340 " ORDER BY (level %% 1024) ASC LIMIT 1",
171341
171342/* Estimate the upper limit on the number of leaf nodes in a new segment
171343** created by merging the oldest :2 segments from absolute level :1. See
171344** function sqlite3Fts3Incrmerge() for details. */
171345/* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
171346 " FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
171347
171348/* SQL_DELETE_SEGDIR_ENTRY
171349** Delete the %_segdir entry on absolute level :1 with index :2. */
171350/* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
171351
171352/* SQL_SHIFT_SEGDIR_ENTRY
171353** Modify the idx value for the segment with idx=:3 on absolute level :2
171354** to :1. */
171355/* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
171356
171357/* SQL_SELECT_SEGDIR
171358** Read a single entry from the %_segdir table. The entry from absolute
171359** level :1 with index value :2. */
171360/* 32 */ "SELECT idx, start_block, leaves_end_block, end_block, root "
171361 "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
171362
171363/* SQL_CHOMP_SEGDIR
171364** Update the start_block (:1) and root (:2) fields of the %_segdir
171365** entry located on absolute level :3 with index :4. */
171366/* 33 */ "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
171367 "WHERE level = ? AND idx = ?",
171368
171369/* SQL_SEGMENT_IS_APPENDABLE
171370** Return a single row if the segment with end_block=? is appendable. Or
171371** no rows otherwise. */
171372/* 34 */ "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
171373
171374/* SQL_SELECT_INDEXES
171375** Return the list of valid segment indexes for absolute level ? */
171376/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
171377
171378/* SQL_SELECT_MXLEVEL
171379** Return the largest relative level in the FTS index or indexes. */
171380/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
171381
171382 /* Return segments in order from oldest to newest.*/
171383/* 37 */ "SELECT level, idx, end_block "
171384 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
171385 "ORDER BY level DESC, idx ASC",
171386
171387 /* Update statements used while promoting segments */
171388/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
171389 "WHERE level=? AND idx=?",
171390/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
171391
171392 };
171393 int rc = SQLITE_OK0;
171394 sqlite3_stmt *pStmt;
171395
171396 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) )((void) (0));
171397 assert( eStmt<SizeofArray(azSql) && eStmt>=0 )((void) (0));
171398
171399 pStmt = p->aStmt[eStmt];
171400 if( !pStmt ){
171401 int f = SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04;
171402 char *zSql;
171403 if( eStmt==SQL_CONTENT_INSERT ){
171404 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
171405 }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
171406 f &= ~SQLITE_PREPARE_NO_VTAB0x04;
171407 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
171408 }else{
171409 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
171410 }
171411 if( !zSql ){
171412 rc = SQLITE_NOMEM7;
171413 }else{
171414 rc = sqlite3_prepare_v3(p->db, zSql, -1, f, &pStmt, NULL((void*)0));
171415 sqlite3_free(zSql);
171416 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
171417 p->aStmt[eStmt] = pStmt;
171418 }
171419 }
171420 if( apVal ){
171421 int i;
171422 int nParam = sqlite3_bind_parameter_count(pStmt);
171423 for(i=0; rc==SQLITE_OK0 && i<nParam; i++){
171424 rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
171425 }
171426 }
171427 *pp = pStmt;
171428 return rc;
171429}
171430
171431
171432static int fts3SelectDocsize(
171433 Fts3Table *pTab, /* FTS3 table handle */
171434 sqlite3_int64 iDocid, /* Docid to bind for SQL_SELECT_DOCSIZE */
171435 sqlite3_stmt **ppStmt /* OUT: Statement handle */
171436){
171437 sqlite3_stmt *pStmt = 0; /* Statement requested from fts3SqlStmt() */
171438 int rc; /* Return code */
171439
171440 rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
171441 if( rc==SQLITE_OK0 ){
171442 sqlite3_bind_int64(pStmt, 1, iDocid);
171443 rc = sqlite3_step(pStmt);
171444 if( rc!=SQLITE_ROW100 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB4 ){
171445 rc = sqlite3_reset(pStmt);
171446 if( rc==SQLITE_OK0 ) rc = FTS_CORRUPT_VTAB;
171447 pStmt = 0;
171448 }else{
171449 rc = SQLITE_OK0;
171450 }
171451 }
171452
171453 *ppStmt = pStmt;
171454 return rc;
171455}
171456
171457SQLITE_PRIVATEstatic int sqlite3Fts3SelectDoctotal(
171458 Fts3Table *pTab, /* Fts3 table handle */
171459 sqlite3_stmt **ppStmt /* OUT: Statement handle */
171460){
171461 sqlite3_stmt *pStmt = 0;
171462 int rc;
171463 rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
171464 if( rc==SQLITE_OK0 ){
171465 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
171466 if( sqlite3_step(pStmt)!=SQLITE_ROW100
171467 || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB4
171468 ){
171469 rc = sqlite3_reset(pStmt);
171470 if( rc==SQLITE_OK0 ) rc = FTS_CORRUPT_VTAB;
171471 pStmt = 0;
171472 }
171473 }
171474 *ppStmt = pStmt;
171475 return rc;
171476}
171477
171478SQLITE_PRIVATEstatic int sqlite3Fts3SelectDocsize(
171479 Fts3Table *pTab, /* Fts3 table handle */
171480 sqlite3_int64 iDocid, /* Docid to read size data for */
171481 sqlite3_stmt **ppStmt /* OUT: Statement handle */
171482){
171483 return fts3SelectDocsize(pTab, iDocid, ppStmt);
171484}
171485
171486/*
171487** Similar to fts3SqlStmt(). Except, after binding the parameters in
171488** array apVal[] to the SQL statement identified by eStmt, the statement
171489** is executed.
171490**
171491** Returns SQLITE_OK if the statement is successfully executed, or an
171492** SQLite error code otherwise.
171493*/
171494static void fts3SqlExec(
171495 int *pRC, /* Result code */
171496 Fts3Table *p, /* The FTS3 table */
171497 int eStmt, /* Index of statement to evaluate */
171498 sqlite3_value **apVal /* Parameters to bind */
171499){
171500 sqlite3_stmt *pStmt;
171501 int rc;
171502 if( *pRC ) return;
171503 rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
171504 if( rc==SQLITE_OK0 ){
171505 sqlite3_step(pStmt);
171506 rc = sqlite3_reset(pStmt);
171507 }
171508 *pRC = rc;
171509}
171510
171511
171512/*
171513** This function ensures that the caller has obtained an exclusive
171514** shared-cache table-lock on the %_segdir table. This is required before
171515** writing data to the fts3 table. If this lock is not acquired first, then
171516** the caller may end up attempting to take this lock as part of committing
171517** a transaction, causing SQLite to return SQLITE_LOCKED or
171518** LOCKED_SHAREDCACHEto a COMMIT command.
171519**
171520** It is best to avoid this because if FTS3 returns any error when
171521** committing a transaction, the whole transaction will be rolled back.
171522** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
171523** It can still happen if the user locks the underlying tables directly
171524** instead of accessing them via FTS.
171525*/
171526static int fts3Writelock(Fts3Table *p){
171527 int rc = SQLITE_OK0;
171528
171529 if( p->nPendingData==0 ){
171530 sqlite3_stmt *pStmt;
171531 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
171532 if( rc==SQLITE_OK0 ){
171533 sqlite3_bind_null(pStmt, 1);
171534 sqlite3_step(pStmt);
171535 rc = sqlite3_reset(pStmt);
171536 }
171537 }
171538
171539 return rc;
171540}
171541
171542/*
171543** FTS maintains a separate indexes for each language-id (a 32-bit integer).
171544** Within each language id, a separate index is maintained to store the
171545** document terms, and each configured prefix size (configured the FTS
171546** "prefix=" option). And each index consists of multiple levels ("relative
171547** levels").
171548**
171549** All three of these values (the language id, the specific index and the
171550** level within the index) are encoded in 64-bit integer values stored
171551** in the %_segdir table on disk. This function is used to convert three
171552** separate component values into the single 64-bit integer value that
171553** can be used to query the %_segdir table.
171554**
171555** Specifically, each language-id/index combination is allocated 1024
171556** 64-bit integer level values ("absolute levels"). The main terms index
171557** for language-id 0 is allocate values 0-1023. The first prefix index
171558** (if any) for language-id 0 is allocated values 1024-2047. And so on.
171559** Language 1 indexes are allocated immediately following language 0.
171560**
171561** So, for a system with nPrefix prefix indexes configured, the block of
171562** absolute levels that corresponds to language-id iLangid and index
171563** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
171564*/
171565static sqlite3_int64 getAbsoluteLevel(
171566 Fts3Table *p, /* FTS3 table handle */
171567 int iLangid, /* Language id */
171568 int iIndex, /* Index in p->aIndex[] */
171569 int iLevel /* Level of segments */
171570){
171571 sqlite3_int64 iBase; /* First absolute level for iLangid/iIndex */
171572 assert_fts3_nc( iLangid>=0 );
171573 assert( p->nIndex>0 )((void) (0));
171574 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
171575
171576 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
171577 return iBase + iLevel;
171578}
171579
171580/*
171581** Set *ppStmt to a statement handle that may be used to iterate through
171582** all rows in the %_segdir table, from oldest to newest. If successful,
171583** return SQLITE_OK. If an error occurs while preparing the statement,
171584** return an SQLite error code.
171585**
171586** There is only ever one instance of this SQL statement compiled for
171587** each FTS3 table.
171588**
171589** The statement returns the following columns from the %_segdir table:
171590**
171591** 0: idx
171592** 1: start_block
171593** 2: leaves_end_block
171594** 3: end_block
171595** 4: root
171596*/
171597SQLITE_PRIVATEstatic int sqlite3Fts3AllSegdirs(
171598 Fts3Table *p, /* FTS3 table */
171599 int iLangid, /* Language being queried */
171600 int iIndex, /* Index for p->aIndex[] */
171601 int iLevel, /* Level to select (relative level) */
171602 sqlite3_stmt **ppStmt /* OUT: Compiled statement */
171603){
171604 int rc;
171605 sqlite3_stmt *pStmt = 0;
171606
171607 assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 )((void) (0));
171608 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
171609 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
171610
171611 if( iLevel<0 ){
171612 /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
171613 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
171614 if( rc==SQLITE_OK0 ){
171615 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
171616 sqlite3_bind_int64(pStmt, 2,
171617 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
171618 );
171619 }
171620 }else{
171621 /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
171622 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
171623 if( rc==SQLITE_OK0 ){
171624 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
171625 }
171626 }
171627 *ppStmt = pStmt;
171628 return rc;
171629}
171630
171631
171632/*
171633** Append a single varint to a PendingList buffer. SQLITE_OK is returned
171634** if successful, or an SQLite error code otherwise.
171635**
171636** This function also serves to allocate the PendingList structure itself.
171637** For example, to create a new PendingList structure containing two
171638** varints:
171639**
171640** PendingList *p = 0;
171641** fts3PendingListAppendVarint(&p, 1);
171642** fts3PendingListAppendVarint(&p, 2);
171643*/
171644static int fts3PendingListAppendVarint(
171645 PendingList **pp, /* IN/OUT: Pointer to PendingList struct */
171646 sqlite3_int64 i /* Value to append to data */
171647){
171648 PendingList *p = *pp;
171649
171650 /* Allocate or grow the PendingList as required. */
171651 if( !p ){
171652 p = sqlite3_malloc(sizeof(*p) + 100);
171653 if( !p ){
171654 return SQLITE_NOMEM7;
171655 }
171656 p->nSpace = 100;
171657 p->aData = (char *)&p[1];
171658 p->nData = 0;
171659 }
171660 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
171661 int nNew = p->nSpace * 2;
171662 p = sqlite3_realloc(p, sizeof(*p) + nNew);
171663 if( !p ){
171664 sqlite3_free(*pp);
171665 *pp = 0;
171666 return SQLITE_NOMEM7;
171667 }
171668 p->nSpace = nNew;
171669 p->aData = (char *)&p[1];
171670 }
171671
171672 /* Append the new serialized varint to the end of the list. */
171673 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
171674 p->aData[p->nData] = '\0';
171675 *pp = p;
171676 return SQLITE_OK0;
171677}
171678
171679/*
171680** Add a docid/column/position entry to a PendingList structure. Non-zero
171681** is returned if the structure is sqlite3_realloced as part of adding
171682** the entry. Otherwise, zero.
171683**
171684** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
171685** Zero is always returned in this case. Otherwise, if no OOM error occurs,
171686** it is set to SQLITE_OK.
171687*/
171688static int fts3PendingListAppend(
171689 PendingList **pp, /* IN/OUT: PendingList structure */
171690 sqlite3_int64 iDocid, /* Docid for entry to add */
171691 sqlite3_int64 iCol, /* Column for entry to add */
171692 sqlite3_int64 iPos, /* Position of term for entry to add */
171693 int *pRc /* OUT: Return code */
171694){
171695 PendingList *p = *pp;
171696 int rc = SQLITE_OK0;
171697
171698 assert( !p || p->iLastDocid<=iDocid )((void) (0));
171699
171700 if( !p || p->iLastDocid!=iDocid ){
171701 sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
171702 if( p ){
171703 assert( p->nData<p->nSpace )((void) (0));
171704 assert( p->aData[p->nData]==0 )((void) (0));
171705 p->nData++;
171706 }
171707 if( SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
171708 goto pendinglistappend_out;
171709 }
171710 p->iLastCol = -1;
171711 p->iLastPos = 0;
171712 p->iLastDocid = iDocid;
171713 }
171714 if( iCol>0 && p->iLastCol!=iCol ){
171715 if( SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, 1))
171716 || SQLITE_OK0!=(rc = fts3PendingListAppendVarint(&p, iCol))
171717 ){
171718 goto pendinglistappend_out;
171719 }
171720 p->iLastCol = iCol;
171721 p->iLastPos = 0;
171722 }
171723 if( iCol>=0 ){
171724 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) )((void) (0));
171725 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
171726 if( rc==SQLITE_OK0 ){
171727 p->iLastPos = iPos;
171728 }
171729 }
171730
171731 pendinglistappend_out:
171732 *pRc = rc;
171733 if( p!=*pp ){
171734 *pp = p;
171735 return 1;
171736 }
171737 return 0;
171738}
171739
171740/*
171741** Free a PendingList object allocated by fts3PendingListAppend().
171742*/
171743static void fts3PendingListDelete(PendingList *pList){
171744 sqlite3_free(pList);
171745}
171746
171747/*
171748** Add an entry to one of the pending-terms hash tables.
171749*/
171750static int fts3PendingTermsAddOne(
171751 Fts3Table *p,
171752 int iCol,
171753 int iPos,
171754 Fts3Hash *pHash, /* Pending terms hash table to add entry to */
171755 const char *zToken,
171756 int nToken
171757){
171758 PendingList *pList;
171759 int rc = SQLITE_OK0;
171760
171761 pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
171762 if( pList ){
171763 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
171764 }
171765 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
171766 if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
171767 /* Malloc failed while inserting the new entry. This can only
171768 ** happen if there was no previous entry for this token.
171769 */
171770 assert( 0==fts3HashFind(pHash, zToken, nToken) )((void) (0));
171771 sqlite3_free(pList);
171772 rc = SQLITE_NOMEM7;
171773 }
171774 }
171775 if( rc==SQLITE_OK0 ){
171776 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
171777 }
171778 return rc;
171779}
171780
171781/*
171782** Tokenize the nul-terminated string zText and add all tokens to the
171783** pending-terms hash-table. The docid used is that currently stored in
171784** p->iPrevDocid, and the column is specified by argument iCol.
171785**
171786** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
171787*/
171788static int fts3PendingTermsAdd(
171789 Fts3Table *p, /* Table into which text will be inserted */
171790 int iLangid, /* Language id to use */
171791 const char *zText, /* Text of document to be inserted */
171792 int iCol, /* Column into which text is being inserted */
171793 u32 *pnWord /* IN/OUT: Incr. by number tokens inserted */
171794){
171795 int rc;
171796 int iStart = 0;
171797 int iEnd = 0;
171798 int iPos = 0;
171799 int nWord = 0;
171800
171801 char const *zToken;
171802 int nToken = 0;
171803
171804 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
171805 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
171806 sqlite3_tokenizer_cursor *pCsr;
171807 int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
171808 const char**,int*,int*,int*,int*);
171809
171810 assert( pTokenizer && pModule )((void) (0));
171811
171812 /* If the user has inserted a NULL value, this function may be called with
171813 ** zText==0. In this case, add zero token entries to the hash table and
171814 ** return early. */
171815 if( zText==0 ){
171816 *pnWord = 0;
171817 return SQLITE_OK0;
171818 }
171819
171820 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
171821 if( rc!=SQLITE_OK0 ){
171822 return rc;
171823 }
171824
171825 xNext = pModule->xNext;
171826 while( SQLITE_OK0==rc
171827 && SQLITE_OK0==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
171828 ){
171829 int i;
171830 if( iPos>=nWord ) nWord = iPos+1;
171831
171832 /* Positions cannot be negative; we use -1 as a terminator internally.
171833 ** Tokens must have a non-zero length.
171834 */
171835 if( iPos<0 || !zToken || nToken<=0 ){
171836 rc = SQLITE_ERROR1;
171837 break;
171838 }
171839
171840 /* Add the term to the terms index */
171841 rc = fts3PendingTermsAddOne(
171842 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
171843 );
171844
171845 /* Add the term to each of the prefix indexes that it is not too
171846 ** short for. */
171847 for(i=1; rc==SQLITE_OK0 && i<p->nIndex; i++){
171848 struct Fts3Index *pIndex = &p->aIndex[i];
171849 if( nToken<pIndex->nPrefix ) continue;
171850 rc = fts3PendingTermsAddOne(
171851 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
171852 );
171853 }
171854 }
171855
171856 pModule->xClose(pCsr);
171857 *pnWord += nWord;
171858 return (rc==SQLITE_DONE101 ? SQLITE_OK0 : rc);
171859}
171860
171861/*
171862** Calling this function indicates that subsequent calls to
171863** fts3PendingTermsAdd() are to add term/position-list pairs for the
171864** contents of the document with docid iDocid.
171865*/
171866static int fts3PendingTermsDocid(
171867 Fts3Table *p, /* Full-text table handle */
171868 int bDelete, /* True if this op is a delete */
171869 int iLangid, /* Language id of row being written */
171870 sqlite_int64 iDocid /* Docid of row being written */
171871){
171872 assert( iLangid>=0 )((void) (0));
171873 assert( bDelete==1 || bDelete==0 )((void) (0));
171874
171875 /* TODO(shess) Explore whether partially flushing the buffer on
171876 ** forced-flush would provide better performance. I suspect that if
171877 ** we ordered the doclists by size and flushed the largest until the
171878 ** buffer was half empty, that would let the less frequent terms
171879 ** generate longer doclists.
171880 */
171881 if( iDocid<p->iPrevDocid
171882 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
171883 || p->iPrevLangid!=iLangid
171884 || p->nPendingData>p->nMaxPendingData
171885 ){
171886 int rc = sqlite3Fts3PendingTermsFlush(p);
171887 if( rc!=SQLITE_OK0 ) return rc;
171888 }
171889 p->iPrevDocid = iDocid;
171890 p->iPrevLangid = iLangid;
171891 p->bPrevDelete = bDelete;
171892 return SQLITE_OK0;
171893}
171894
171895/*
171896** Discard the contents of the pending-terms hash tables.
171897*/
171898SQLITE_PRIVATEstatic void sqlite3Fts3PendingTermsClear(Fts3Table *p){
171899 int i;
171900 for(i=0; i<p->nIndex; i++){
171901 Fts3HashElem *pElem;
171902 Fts3Hash *pHash = &p->aIndex[i].hPending;
171903 for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
171904 PendingList *pList = (PendingList *)fts3HashData(pElem);
171905 fts3PendingListDelete(pList);
171906 }
171907 fts3HashClear(pHash);
171908 }
171909 p->nPendingData = 0;
171910}
171911
171912/*
171913** This function is called by the xUpdate() method as part of an INSERT
171914** operation. It adds entries for each term in the new record to the
171915** pendingTerms hash table.
171916**
171917** Argument apVal is the same as the similarly named argument passed to
171918** fts3InsertData(). Parameter iDocid is the docid of the new row.
171919*/
171920static int fts3InsertTerms(
171921 Fts3Table *p,
171922 int iLangid,
171923 sqlite3_value **apVal,
171924 u32 *aSz
171925){
171926 int i; /* Iterator variable */
171927 for(i=2; i<p->nColumn+2; i++){
171928 int iCol = i-2;
171929 if( p->abNotindexed[iCol]==0 ){
171930 const char *zText = (const char *)sqlite3_value_text(apVal[i]);
171931 int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
171932 if( rc!=SQLITE_OK0 ){
171933 return rc;
171934 }
171935 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
171936 }
171937 }
171938 return SQLITE_OK0;
171939}
171940
171941/*
171942** This function is called by the xUpdate() method for an INSERT operation.
171943** The apVal parameter is passed a copy of the apVal argument passed by
171944** SQLite to the xUpdate() method. i.e:
171945**
171946** apVal[0] Not used for INSERT.
171947** apVal[1] rowid
171948** apVal[2] Left-most user-defined column
171949** ...
171950** apVal[p->nColumn+1] Right-most user-defined column
171951** apVal[p->nColumn+2] Hidden column with same name as table
171952** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
171953** apVal[p->nColumn+4] Hidden languageid column
171954*/
171955static int fts3InsertData(
171956 Fts3Table *p, /* Full-text table */
171957 sqlite3_value **apVal, /* Array of values to insert */
171958 sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */
171959){
171960 int rc; /* Return code */
171961 sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */
171962
171963 if( p->zContentTbl ){
171964 sqlite3_value *pRowid = apVal[p->nColumn+3];
171965 if( sqlite3_value_type(pRowid)==SQLITE_NULL5 ){
171966 pRowid = apVal[1];
171967 }
171968 if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER1 ){
171969 return SQLITE_CONSTRAINT19;
171970 }
171971 *piDocid = sqlite3_value_int64(pRowid);
171972 return SQLITE_OK0;
171973 }
171974
171975 /* Locate the statement handle used to insert data into the %_content
171976 ** table. The SQL for this statement is:
171977 **
171978 ** INSERT INTO %_content VALUES(?, ?, ?, ...)
171979 **
171980 ** The statement features N '?' variables, where N is the number of user
171981 ** defined columns in the FTS3 table, plus one for the docid field.
171982 */
171983 rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
171984 if( rc==SQLITE_OK0 && p->zLanguageid ){
171985 rc = sqlite3_bind_int(
171986 pContentInsert, p->nColumn+2,
171987 sqlite3_value_int(apVal[p->nColumn+4])
171988 );
171989 }
171990 if( rc!=SQLITE_OK0 ) return rc;
171991
171992 /* There is a quirk here. The users INSERT statement may have specified
171993 ** a value for the "rowid" field, for the "docid" field, or for both.
171994 ** Which is a problem, since "rowid" and "docid" are aliases for the
171995 ** same value. For example:
171996 **
171997 ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
171998 **
171999 ** In FTS3, this is an error. It is an error to specify non-NULL values
172000 ** for both docid and some other rowid alias.
172001 */
172002 if( SQLITE_NULL5!=sqlite3_value_type(apVal[3+p->nColumn]) ){
172003 if( SQLITE_NULL5==sqlite3_value_type(apVal[0])
172004 && SQLITE_NULL5!=sqlite3_value_type(apVal[1])
172005 ){
172006 /* A rowid/docid conflict. */
172007 return SQLITE_ERROR1;
172008 }
172009 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
172010 if( rc!=SQLITE_OK0 ) return rc;
172011 }
172012
172013 /* Execute the statement to insert the record. Set *piDocid to the
172014 ** new docid value.
172015 */
172016 sqlite3_step(pContentInsert);
172017 rc = sqlite3_reset(pContentInsert);
172018
172019 *piDocid = sqlite3_last_insert_rowid(p->db);
172020 return rc;
172021}
172022
172023
172024
172025/*
172026** Remove all data from the FTS3 table. Clear the hash table containing
172027** pending terms.
172028*/
172029static int fts3DeleteAll(Fts3Table *p, int bContent){
172030 int rc = SQLITE_OK0; /* Return code */
172031
172032 /* Discard the contents of the pending-terms hash table. */
172033 sqlite3Fts3PendingTermsClear(p);
172034
172035 /* Delete everything from the shadow tables. Except, leave %_content as
172036 ** is if bContent is false. */
172037 assert( p->zContentTbl==0 || bContent==0 )((void) (0));
172038 if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
172039 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
172040 fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
172041 if( p->bHasDocsize ){
172042 fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
172043 }
172044 if( p->bHasStat ){
172045 fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
172046 }
172047 return rc;
172048}
172049
172050/*
172051**
172052*/
172053static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
172054 int iLangid = 0;
172055 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
172056 return iLangid;
172057}
172058
172059/*
172060** The first element in the apVal[] array is assumed to contain the docid
172061** (an integer) of a row about to be deleted. Remove all terms from the
172062** full-text index.
172063*/
172064static void fts3DeleteTerms(
172065 int *pRC, /* Result code */
172066 Fts3Table *p, /* The FTS table to delete from */
172067 sqlite3_value *pRowid, /* The docid to be deleted */
172068 u32 *aSz, /* Sizes of deleted document written here */
172069 int *pbFound /* OUT: Set to true if row really does exist */
172070){
172071 int rc;
172072 sqlite3_stmt *pSelect;
172073
172074 assert( *pbFound==0 )((void) (0));
172075 if( *pRC ) return;
172076 rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
172077 if( rc==SQLITE_OK0 ){
172078 if( SQLITE_ROW100==sqlite3_step(pSelect) ){
172079 int i;
172080 int iLangid = langidFromSelect(p, pSelect);
172081 i64 iDocid = sqlite3_column_int64(pSelect, 0);
172082 rc = fts3PendingTermsDocid(p, 1, iLangid, iDocid);
172083 for(i=1; rc==SQLITE_OK0 && i<=p->nColumn; i++){
172084 int iCol = i-1;
172085 if( p->abNotindexed[iCol]==0 ){
172086 const char *zText = (const char *)sqlite3_column_text(pSelect, i);
172087 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
172088 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
172089 }
172090 }
172091 if( rc!=SQLITE_OK0 ){
172092 sqlite3_reset(pSelect);
172093 *pRC = rc;
172094 return;
172095 }
172096 *pbFound = 1;
172097 }
172098 rc = sqlite3_reset(pSelect);
172099 }else{
172100 sqlite3_reset(pSelect);
172101 }
172102 *pRC = rc;
172103}
172104
172105/*
172106** Forward declaration to account for the circular dependency between
172107** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
172108*/
172109static int fts3SegmentMerge(Fts3Table *, int, int, int);
172110
172111/*
172112** This function allocates a new level iLevel index in the segdir table.
172113** Usually, indexes are allocated within a level sequentially starting
172114** with 0, so the allocated index is one greater than the value returned
172115** by:
172116**
172117** SELECT max(idx) FROM %_segdir WHERE level = :iLevel
172118**
172119** However, if there are already FTS3_MERGE_COUNT indexes at the requested
172120** level, they are merged into a single level (iLevel+1) segment and the
172121** allocated index is 0.
172122**
172123** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
172124** returned. Otherwise, an SQLite error code is returned.
172125*/
172126static int fts3AllocateSegdirIdx(
172127 Fts3Table *p,
172128 int iLangid, /* Language id */
172129 int iIndex, /* Index for p->aIndex */
172130 int iLevel,
172131 int *piIdx
172132){
172133 int rc; /* Return Code */
172134 sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */
172135 int iNext = 0; /* Result of query pNextIdx */
172136
172137 assert( iLangid>=0 )((void) (0));
172138 assert( p->nIndex>=1 )((void) (0));
172139
172140 /* Set variable iNext to the next available segdir index at level iLevel. */
172141 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
172142 if( rc==SQLITE_OK0 ){
172143 sqlite3_bind_int64(
172144 pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
172145 );
172146 if( SQLITE_ROW100==sqlite3_step(pNextIdx) ){
172147 iNext = sqlite3_column_int(pNextIdx, 0);
172148 }
172149 rc = sqlite3_reset(pNextIdx);
172150 }
172151
172152 if( rc==SQLITE_OK0 ){
172153 /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
172154 ** full, merge all segments in level iLevel into a single iLevel+1
172155 ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
172156 ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
172157 */
172158 if( iNext>=FTS3_MERGE_COUNT ){
172159 fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
172160 rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
172161 *piIdx = 0;
172162 }else{
172163 *piIdx = iNext;
172164 }
172165 }
172166
172167 return rc;
172168}
172169
172170/*
172171** The %_segments table is declared as follows:
172172**
172173** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
172174**
172175** This function reads data from a single row of the %_segments table. The
172176** specific row is identified by the iBlockid parameter. If paBlob is not
172177** NULL, then a buffer is allocated using sqlite3_malloc() and populated
172178** with the contents of the blob stored in the "block" column of the
172179** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
172180** to the size of the blob in bytes before returning.
172181**
172182** If an error occurs, or the table does not contain the specified row,
172183** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
172184** paBlob is non-NULL, then it is the responsibility of the caller to
172185** eventually free the returned buffer.
172186**
172187** This function may leave an open sqlite3_blob* handle in the
172188** Fts3Table.pSegments variable. This handle is reused by subsequent calls
172189** to this function. The handle may be closed by calling the
172190** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
172191** performance improvement, but the blob handle should always be closed
172192** before control is returned to the user (to prevent a lock being held
172193** on the database file for longer than necessary). Thus, any virtual table
172194** method (xFilter etc.) that may directly or indirectly call this function
172195** must call sqlite3Fts3SegmentsClose() before returning.
172196*/
172197SQLITE_PRIVATEstatic int sqlite3Fts3ReadBlock(
172198 Fts3Table *p, /* FTS3 table handle */
172199 sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */
172200 char **paBlob, /* OUT: Blob data in malloc'd buffer */
172201 int *pnBlob, /* OUT: Size of blob data */
172202 int *pnLoad /* OUT: Bytes actually loaded */
172203){
172204 int rc; /* Return code */
172205
172206 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
172207 assert( pnBlob )((void) (0));
172208
172209 if( p->pSegments ){
172210 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
172211 }else{
172212 if( 0==p->zSegmentsTbl ){
172213 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
172214 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM7;
172215 }
172216 rc = sqlite3_blob_open(
172217 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
172218 );
172219 }
172220
172221 if( rc==SQLITE_OK0 ){
172222 int nByte = sqlite3_blob_bytes(p->pSegments);
172223 *pnBlob = nByte;
172224 if( paBlob ){
172225 char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
172226 if( !aByte ){
172227 rc = SQLITE_NOMEM7;
172228 }else{
172229 if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
172230 nByte = FTS3_NODE_CHUNKSIZE;
172231 *pnLoad = nByte;
172232 }
172233 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
172234 memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
172235 if( rc!=SQLITE_OK0 ){
172236 sqlite3_free(aByte);
172237 aByte = 0;
172238 }
172239 }
172240 *paBlob = aByte;
172241 }
172242 }
172243
172244 return rc;
172245}
172246
172247/*
172248** Close the blob handle at p->pSegments, if it is open. See comments above
172249** the sqlite3Fts3ReadBlock() function for details.
172250*/
172251SQLITE_PRIVATEstatic void sqlite3Fts3SegmentsClose(Fts3Table *p){
172252 sqlite3_blob_close(p->pSegments);
172253 p->pSegments = 0;
172254}
172255
172256static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
172257 int nRead; /* Number of bytes to read */
172258 int rc; /* Return code */
172259
172260 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE)((pReader->nNode - pReader->nPopulate)<(FTS3_NODE_CHUNKSIZE
)?(pReader->nNode - pReader->nPopulate):(FTS3_NODE_CHUNKSIZE
))
;
172261 rc = sqlite3_blob_read(
172262 pReader->pBlob,
172263 &pReader->aNode[pReader->nPopulate],
172264 nRead,
172265 pReader->nPopulate
172266 );
172267
172268 if( rc==SQLITE_OK0 ){
172269 pReader->nPopulate += nRead;
172270 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
172271 if( pReader->nPopulate==pReader->nNode ){
172272 sqlite3_blob_close(pReader->pBlob);
172273 pReader->pBlob = 0;
172274 pReader->nPopulate = 0;
172275 }
172276 }
172277 return rc;
172278}
172279
172280static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
172281 int rc = SQLITE_OK0;
172282 assert( !pReader->pBlob((void) (0))
172283 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])((void) (0))
172284 )((void) (0));
172285 while( pReader->pBlob && rc==SQLITE_OK0
172286 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
172287 ){
172288 rc = fts3SegReaderIncrRead(pReader);
172289 }
172290 return rc;
172291}
172292
172293/*
172294** Set an Fts3SegReader cursor to point at EOF.
172295*/
172296static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
172297 if( !fts3SegReaderIsRootOnly(pSeg) ){
172298 sqlite3_free(pSeg->aNode);
172299 sqlite3_blob_close(pSeg->pBlob);
172300 pSeg->pBlob = 0;
172301 }
172302 pSeg->aNode = 0;
172303}
172304
172305/*
172306** Move the iterator passed as the first argument to the next term in the
172307** segment. If successful, SQLITE_OK is returned. If there is no next term,
172308** SQLITE_DONE. Otherwise, an SQLite error code.
172309*/
172310static int fts3SegReaderNext(
172311 Fts3Table *p,
172312 Fts3SegReader *pReader,
172313 int bIncr
172314){
172315 int rc; /* Return code of various sub-routines */
172316 char *pNext; /* Cursor variable */
172317 int nPrefix; /* Number of bytes in term prefix */
172318 int nSuffix; /* Number of bytes in term suffix */
172319
172320 if( !pReader->aDoclist ){
172321 pNext = pReader->aNode;
172322 }else{
172323 pNext = &pReader->aDoclist[pReader->nDoclist];
172324 }
172325
172326 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
172327
172328 if( fts3SegReaderIsPending(pReader) ){
172329 Fts3HashElem *pElem = *(pReader->ppNextElem);
172330 sqlite3_free(pReader->aNode);
172331 pReader->aNode = 0;
172332 if( pElem ){
172333 char *aCopy;
172334 PendingList *pList = (PendingList *)fts3HashData(pElem);
172335 int nCopy = pList->nData+1;
172336 pReader->zTerm = (char *)fts3HashKey(pElem);
172337 pReader->nTerm = fts3HashKeysize(pElem);
172338 aCopy = (char*)sqlite3_malloc(nCopy);
172339 if( !aCopy ) return SQLITE_NOMEM7;
172340 memcpy(aCopy, pList->aData, nCopy);
172341 pReader->nNode = pReader->nDoclist = nCopy;
172342 pReader->aNode = pReader->aDoclist = aCopy;
172343 pReader->ppNextElem++;
172344 assert( pReader->aNode )((void) (0));
172345 }
172346 return SQLITE_OK0;
172347 }
172348
172349 fts3SegReaderSetEof(pReader);
172350
172351 /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
172352 ** blocks have already been traversed. */
172353#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
172354 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB )((void) (0));
172355#endif
172356 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
172357 return SQLITE_OK0;
172358 }
172359
172360 rc = sqlite3Fts3ReadBlock(
172361 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
172362 (bIncr ? &pReader->nPopulate : 0)
172363 );
172364 if( rc!=SQLITE_OK0 ) return rc;
172365 assert( pReader->pBlob==0 )((void) (0));
172366 if( bIncr && pReader->nPopulate<pReader->nNode ){
172367 pReader->pBlob = p->pSegments;
172368 p->pSegments = 0;
172369 }
172370 pNext = pReader->aNode;
172371 }
172372
172373 assert( !fts3SegReaderIsPending(pReader) )((void) (0));
172374
172375 rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
172376 if( rc!=SQLITE_OK0 ) return rc;
172377
172378 /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
172379 ** safe (no risk of overread) even if the node data is corrupted. */
172380 pNext += fts3GetVarint32(pNext, &nPrefix);
172381 pNext += fts3GetVarint32(pNext, &nSuffix);
172382 if( nSuffix<=0
172383 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
172384 || nPrefix>pReader->nTermAlloc
172385 ){
172386 return FTS_CORRUPT_VTAB;
172387 }
172388
172389 /* Both nPrefix and nSuffix were read by fts3GetVarint32() and so are
172390 ** between 0 and 0x7FFFFFFF. But the sum of the two may cause integer
172391 ** overflow - hence the (i64) casts. */
172392 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
172393 i64 nNew = ((i64)nPrefix+nSuffix)*2;
172394 char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
172395 if( !zNew ){
172396 return SQLITE_NOMEM7;
172397 }
172398 pReader->zTerm = zNew;
172399 pReader->nTermAlloc = nNew;
172400 }
172401
172402 rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
172403 if( rc!=SQLITE_OK0 ) return rc;
172404
172405 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
172406 pReader->nTerm = nPrefix+nSuffix;
172407 pNext += nSuffix;
172408 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
172409 pReader->aDoclist = pNext;
172410 pReader->pOffsetList = 0;
172411
172412 /* Check that the doclist does not appear to extend past the end of the
172413 ** b-tree node. And that the final byte of the doclist is 0x00. If either
172414 ** of these statements is untrue, then the data structure is corrupt.
172415 */
172416 if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
172417 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
172418 ){
172419 return FTS_CORRUPT_VTAB;
172420 }
172421 return SQLITE_OK0;
172422}
172423
172424/*
172425** Set the SegReader to point to the first docid in the doclist associated
172426** with the current term.
172427*/
172428static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
172429 int rc = SQLITE_OK0;
172430 assert( pReader->aDoclist )((void) (0));
172431 assert( !pReader->pOffsetList )((void) (0));
172432 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
172433 u8 bEof = 0;
172434 pReader->iDocid = 0;
172435 pReader->nOffsetList = 0;
172436 sqlite3Fts3DoclistPrev(0,
172437 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
172438 &pReader->iDocid, &pReader->nOffsetList, &bEof
172439 );
172440 }else{
172441 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
172442 if( rc==SQLITE_OK0 ){
172443 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
172444 pReader->pOffsetList = &pReader->aDoclist[n];
172445 }
172446 }
172447 return rc;
172448}
172449
172450/*
172451** Advance the SegReader to point to the next docid in the doclist
172452** associated with the current term.
172453**
172454** If arguments ppOffsetList and pnOffsetList are not NULL, then
172455** *ppOffsetList is set to point to the first column-offset list
172456** in the doclist entry (i.e. immediately past the docid varint).
172457** *pnOffsetList is set to the length of the set of column-offset
172458** lists, not including the nul-terminator byte. For example:
172459*/
172460static int fts3SegReaderNextDocid(
172461 Fts3Table *pTab,
172462 Fts3SegReader *pReader, /* Reader to advance to next docid */
172463 char **ppOffsetList, /* OUT: Pointer to current position-list */
172464 int *pnOffsetList /* OUT: Length of *ppOffsetList in bytes */
172465){
172466 int rc = SQLITE_OK0;
172467 char *p = pReader->pOffsetList;
172468 char c = 0;
172469
172470 assert( p )((void) (0));
172471
172472 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
172473 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
172474 ** Pending-terms doclists are always built up in ascending order, so
172475 ** we have to iterate through them backwards here. */
172476 u8 bEof = 0;
172477 if( ppOffsetList ){
172478 *ppOffsetList = pReader->pOffsetList;
172479 *pnOffsetList = pReader->nOffsetList - 1;
172480 }
172481 sqlite3Fts3DoclistPrev(0,
172482 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
172483 &pReader->nOffsetList, &bEof
172484 );
172485 if( bEof ){
172486 pReader->pOffsetList = 0;
172487 }else{
172488 pReader->pOffsetList = p;
172489 }
172490 }else{
172491 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
172492
172493 /* Pointer p currently points at the first byte of an offset list. The
172494 ** following block advances it to point one byte past the end of
172495 ** the same offset list. */
172496 while( 1 ){
172497
172498 /* The following line of code (and the "p++" below the while() loop) is
172499 ** normally all that is required to move pointer p to the desired
172500 ** position. The exception is if this node is being loaded from disk
172501 ** incrementally and pointer "p" now points to the first byte past
172502 ** the populated part of pReader->aNode[].
172503 */
172504 while( *p | c ) c = *p++ & 0x80;
172505 assert( *p==0 )((void) (0));
172506
172507 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
172508 rc = fts3SegReaderIncrRead(pReader);
172509 if( rc!=SQLITE_OK0 ) return rc;
172510 }
172511 p++;
172512
172513 /* If required, populate the output variables with a pointer to and the
172514 ** size of the previous offset-list.
172515 */
172516 if( ppOffsetList ){
172517 *ppOffsetList = pReader->pOffsetList;
172518 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
172519 }
172520
172521 /* List may have been edited in place by fts3EvalNearTrim() */
172522 while( p<pEnd && *p==0 ) p++;
172523
172524 /* If there are no more entries in the doclist, set pOffsetList to
172525 ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
172526 ** Fts3SegReader.pOffsetList to point to the next offset list before
172527 ** returning.
172528 */
172529 if( p>=pEnd ){
172530 pReader->pOffsetList = 0;
172531 }else{
172532 rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
172533 if( rc==SQLITE_OK0 ){
172534 sqlite3_int64 iDelta;
172535 pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
172536 if( pTab->bDescIdx ){
172537 pReader->iDocid -= iDelta;
172538 }else{
172539 pReader->iDocid += iDelta;
172540 }
172541 }
172542 }
172543 }
172544
172545 return SQLITE_OK0;
172546}
172547
172548
172549SQLITE_PRIVATEstatic int sqlite3Fts3MsrOvfl(
172550 Fts3Cursor *pCsr,
172551 Fts3MultiSegReader *pMsr,
172552 int *pnOvfl
172553){
172554 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
172555 int nOvfl = 0;
172556 int ii;
172557 int rc = SQLITE_OK0;
172558 int pgsz = p->nPgsz;
172559
172560 assert( p->bFts4 )((void) (0));
172561 assert( pgsz>0 )((void) (0));
172562
172563 for(ii=0; rc==SQLITE_OK0 && ii<pMsr->nSegment; ii++){
172564 Fts3SegReader *pReader = pMsr->apSegment[ii];
172565 if( !fts3SegReaderIsPending(pReader)
172566 && !fts3SegReaderIsRootOnly(pReader)
172567 ){
172568 sqlite3_int64 jj;
172569 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
172570 int nBlob;
172571 rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
172572 if( rc!=SQLITE_OK0 ) break;
172573 if( (nBlob+35)>pgsz ){
172574 nOvfl += (nBlob + 34)/pgsz;
172575 }
172576 }
172577 }
172578 }
172579 *pnOvfl = nOvfl;
172580 return rc;
172581}
172582
172583/*
172584** Free all allocations associated with the iterator passed as the
172585** second argument.
172586*/
172587SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
172588 if( pReader ){
172589 if( !fts3SegReaderIsPending(pReader) ){
172590 sqlite3_free(pReader->zTerm);
172591 }
172592 if( !fts3SegReaderIsRootOnly(pReader) ){
172593 sqlite3_free(pReader->aNode);
172594 }
172595 sqlite3_blob_close(pReader->pBlob);
172596 }
172597 sqlite3_free(pReader);
172598}
172599
172600/*
172601** Allocate a new SegReader object.
172602*/
172603SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderNew(
172604 int iAge, /* Segment "age". */
172605 int bLookup, /* True for a lookup only */
172606 sqlite3_int64 iStartLeaf, /* First leaf to traverse */
172607 sqlite3_int64 iEndLeaf, /* Final leaf to traverse */
172608 sqlite3_int64 iEndBlock, /* Final block of segment */
172609 const char *zRoot, /* Buffer containing root node */
172610 int nRoot, /* Size of buffer containing root node */
172611 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
172612){
172613 Fts3SegReader *pReader; /* Newly allocated SegReader object */
172614 int nExtra = 0; /* Bytes to allocate segment root node */
172615
172616 assert( zRoot!=0 || nRoot==0 )((void) (0));
172617#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
172618 assert( zRoot!=0 || CORRUPT_DB )((void) (0));
172619#endif
172620
172621 if( iStartLeaf==0 ){
172622 if( iEndLeaf!=0 ) return FTS_CORRUPT_VTAB;
172623 nExtra = nRoot + FTS3_NODE_PADDING;
172624 }
172625
172626 pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
172627 if( !pReader ){
172628 return SQLITE_NOMEM7;
172629 }
172630 memset(pReader, 0, sizeof(Fts3SegReader));
172631 pReader->iIdx = iAge;
172632 pReader->bLookup = bLookup!=0;
172633 pReader->iStartBlock = iStartLeaf;
172634 pReader->iLeafEndBlock = iEndLeaf;
172635 pReader->iEndBlock = iEndBlock;
172636
172637 if( nExtra ){
172638 /* The entire segment is stored in the root node. */
172639 pReader->aNode = (char *)&pReader[1];
172640 pReader->rootOnly = 1;
172641 pReader->nNode = nRoot;
172642 if( nRoot ) memcpy(pReader->aNode, zRoot, nRoot);
172643 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
172644 }else{
172645 pReader->iCurrentBlock = iStartLeaf-1;
172646 }
172647 *ppReader = pReader;
172648 return SQLITE_OK0;
172649}
172650
172651/*
172652** This is a comparison function used as a qsort() callback when sorting
172653** an array of pending terms by term. This occurs as part of flushing
172654** the contents of the pending-terms hash table to the database.
172655*/
172656static int SQLITE_CDECL fts3CompareElemByTerm(
172657 const void *lhs,
172658 const void *rhs
172659){
172660 char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
172661 char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
172662 int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
172663 int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
172664
172665 int n = (n1<n2 ? n1 : n2);
172666 int c = memcmp(z1, z2, n);
172667 if( c==0 ){
172668 c = n1 - n2;
172669 }
172670 return c;
172671}
172672
172673/*
172674** This function is used to allocate an Fts3SegReader that iterates through
172675** a subset of the terms stored in the Fts3Table.pendingTerms array.
172676**
172677** If the isPrefixIter parameter is zero, then the returned SegReader iterates
172678** through each term in the pending-terms table. Or, if isPrefixIter is
172679** non-zero, it iterates through each term and its prefixes. For example, if
172680** the pending terms hash table contains the terms "sqlite", "mysql" and
172681** "firebird", then the iterator visits the following 'terms' (in the order
172682** shown):
172683**
172684** f fi fir fire fireb firebi firebir firebird
172685** m my mys mysq mysql
172686** s sq sql sqli sqlit sqlite
172687**
172688** Whereas if isPrefixIter is zero, the terms visited are:
172689**
172690** firebird mysql sqlite
172691*/
172692SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderPending(
172693 Fts3Table *p, /* Virtual table handle */
172694 int iIndex, /* Index for p->aIndex */
172695 const char *zTerm, /* Term to search for */
172696 int nTerm, /* Size of buffer zTerm */
172697 int bPrefix, /* True for a prefix iterator */
172698 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
172699){
172700 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
172701 Fts3HashElem *pE; /* Iterator variable */
172702 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
172703 int nElem = 0; /* Size of array at aElem */
172704 int rc = SQLITE_OK0; /* Return Code */
172705 Fts3Hash *pHash;
172706
172707 pHash = &p->aIndex[iIndex].hPending;
172708 if( bPrefix ){
172709 int nAlloc = 0; /* Size of allocated array at aElem */
172710
172711 for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
172712 char *zKey = (char *)fts3HashKey(pE);
172713 int nKey = fts3HashKeysize(pE);
172714 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
172715 if( nElem==nAlloc ){
172716 Fts3HashElem **aElem2;
172717 nAlloc += 16;
172718 aElem2 = (Fts3HashElem **)sqlite3_realloc(
172719 aElem, nAlloc*sizeof(Fts3HashElem *)
172720 );
172721 if( !aElem2 ){
172722 rc = SQLITE_NOMEM7;
172723 nElem = 0;
172724 break;
172725 }
172726 aElem = aElem2;
172727 }
172728
172729 aElem[nElem++] = pE;
172730 }
172731 }
172732
172733 /* If more than one term matches the prefix, sort the Fts3HashElem
172734 ** objects in term order using qsort(). This uses the same comparison
172735 ** callback as is used when flushing terms to disk.
172736 */
172737 if( nElem>1 ){
172738 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
172739 }
172740
172741 }else{
172742 /* The query is a simple term lookup that matches at most one term in
172743 ** the index. All that is required is a straight hash-lookup.
172744 **
172745 ** Because the stack address of pE may be accessed via the aElem pointer
172746 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
172747 ** within this entire function, not just this "else{...}" block.
172748 */
172749 pE = fts3HashFindElem(pHash, zTerm, nTerm);
172750 if( pE ){
172751 aElem = &pE;
172752 nElem = 1;
172753 }
172754 }
172755
172756 if( nElem>0 ){
172757 sqlite3_int64 nByte;
172758 nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
172759 pReader = (Fts3SegReader *)sqlite3_malloc64(nByte);
172760 if( !pReader ){
172761 rc = SQLITE_NOMEM7;
172762 }else{
172763 memset(pReader, 0, nByte);
172764 pReader->iIdx = 0x7FFFFFFF;
172765 pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
172766 memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
172767 }
172768 }
172769
172770 if( bPrefix ){
172771 sqlite3_free(aElem);
172772 }
172773 *ppReader = pReader;
172774 return rc;
172775}
172776
172777/*
172778** Compare the entries pointed to by two Fts3SegReader structures.
172779** Comparison is as follows:
172780**
172781** 1) EOF is greater than not EOF.
172782**
172783** 2) The current terms (if any) are compared using memcmp(). If one
172784** term is a prefix of another, the longer term is considered the
172785** larger.
172786**
172787** 3) By segment age. An older segment is considered larger.
172788*/
172789static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
172790 int rc;
172791 if( pLhs->aNode && pRhs->aNode ){
172792 int rc2 = pLhs->nTerm - pRhs->nTerm;
172793 if( rc2<0 ){
172794 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
172795 }else{
172796 rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
172797 }
172798 if( rc==0 ){
172799 rc = rc2;
172800 }
172801 }else{
172802 rc = (pLhs->aNode==0) - (pRhs->aNode==0);
172803 }
172804 if( rc==0 ){
172805 rc = pRhs->iIdx - pLhs->iIdx;
172806 }
172807 assert( rc!=0 )((void) (0));
172808 return rc;
172809}
172810
172811/*
172812** A different comparison function for SegReader structures. In this
172813** version, it is assumed that each SegReader points to an entry in
172814** a doclist for identical terms. Comparison is made as follows:
172815**
172816** 1) EOF (end of doclist in this case) is greater than not EOF.
172817**
172818** 2) By current docid.
172819**
172820** 3) By segment age. An older segment is considered larger.
172821*/
172822static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
172823 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
172824 if( rc==0 ){
172825 if( pLhs->iDocid==pRhs->iDocid ){
172826 rc = pRhs->iIdx - pLhs->iIdx;
172827 }else{
172828 rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
172829 }
172830 }
172831 assert( pLhs->aNode && pRhs->aNode )((void) (0));
172832 return rc;
172833}
172834static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
172835 int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
172836 if( rc==0 ){
172837 if( pLhs->iDocid==pRhs->iDocid ){
172838 rc = pRhs->iIdx - pLhs->iIdx;
172839 }else{
172840 rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
172841 }
172842 }
172843 assert( pLhs->aNode && pRhs->aNode )((void) (0));
172844 return rc;
172845}
172846
172847/*
172848** Compare the term that the Fts3SegReader object passed as the first argument
172849** points to with the term specified by arguments zTerm and nTerm.
172850**
172851** If the pSeg iterator is already at EOF, return 0. Otherwise, return
172852** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
172853** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
172854*/
172855static int fts3SegReaderTermCmp(
172856 Fts3SegReader *pSeg, /* Segment reader object */
172857 const char *zTerm, /* Term to compare to */
172858 int nTerm /* Size of term zTerm in bytes */
172859){
172860 int res = 0;
172861 if( pSeg->aNode ){
172862 if( pSeg->nTerm>nTerm ){
172863 res = memcmp(pSeg->zTerm, zTerm, nTerm);
172864 }else{
172865 res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
172866 }
172867 if( res==0 ){
172868 res = pSeg->nTerm-nTerm;
172869 }
172870 }
172871 return res;
172872}
172873
172874/*
172875** Argument apSegment is an array of nSegment elements. It is known that
172876** the final (nSegment-nSuspect) members are already in sorted order
172877** (according to the comparison function provided). This function shuffles
172878** the array around until all entries are in sorted order.
172879*/
172880static void fts3SegReaderSort(
172881 Fts3SegReader **apSegment, /* Array to sort entries of */
172882 int nSegment, /* Size of apSegment array */
172883 int nSuspect, /* Unsorted entry count */
172884 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */
172885){
172886 int i; /* Iterator variable */
172887
172888 assert( nSuspect<=nSegment )((void) (0));
172889
172890 if( nSuspect==nSegment ) nSuspect--;
172891 for(i=nSuspect-1; i>=0; i--){
172892 int j;
172893 for(j=i; j<(nSegment-1); j++){
172894 Fts3SegReader *pTmp;
172895 if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
172896 pTmp = apSegment[j+1];
172897 apSegment[j+1] = apSegment[j];
172898 apSegment[j] = pTmp;
172899 }
172900 }
172901
172902#ifndef NDEBUG1
172903 /* Check that the list really is sorted now. */
172904 for(i=0; i<(nSuspect-1); i++){
172905 assert( xCmp(apSegment[i], apSegment[i+1])<0 )((void) (0));
172906 }
172907#endif
172908}
172909
172910/*
172911** Insert a record into the %_segments table.
172912*/
172913static int fts3WriteSegment(
172914 Fts3Table *p, /* Virtual table handle */
172915 sqlite3_int64 iBlock, /* Block id for new block */
172916 char *z, /* Pointer to buffer containing block data */
172917 int n /* Size of buffer z in bytes */
172918){
172919 sqlite3_stmt *pStmt;
172920 int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
172921 if( rc==SQLITE_OK0 ){
172922 sqlite3_bind_int64(pStmt, 1, iBlock);
172923 sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC((sqlite3_destructor_type)0));
172924 sqlite3_step(pStmt);
172925 rc = sqlite3_reset(pStmt);
172926 sqlite3_bind_null(pStmt, 2);
172927 }
172928 return rc;
172929}
172930
172931/*
172932** Find the largest relative level number in the table. If successful, set
172933** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
172934** set *pnMax to zero and return an SQLite error code.
172935*/
172936SQLITE_PRIVATEstatic int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
172937 int rc;
172938 int mxLevel = 0;
172939 sqlite3_stmt *pStmt = 0;
172940
172941 rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
172942 if( rc==SQLITE_OK0 ){
172943 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
172944 mxLevel = sqlite3_column_int(pStmt, 0);
172945 }
172946 rc = sqlite3_reset(pStmt);
172947 }
172948 *pnMax = mxLevel;
172949 return rc;
172950}
172951
172952/*
172953** Insert a record into the %_segdir table.
172954*/
172955static int fts3WriteSegdir(
172956 Fts3Table *p, /* Virtual table handle */
172957 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
172958 int iIdx, /* Value for "idx" field */
172959 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
172960 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
172961 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
172962 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
172963 char *zRoot, /* Blob value for "root" field */
172964 int nRoot /* Number of bytes in buffer zRoot */
172965){
172966 sqlite3_stmt *pStmt;
172967 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
172968 if( rc==SQLITE_OK0 ){
172969 sqlite3_bind_int64(pStmt, 1, iLevel);
172970 sqlite3_bind_int(pStmt, 2, iIdx);
172971 sqlite3_bind_int64(pStmt, 3, iStartBlock);
172972 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
172973 if( nLeafData==0 ){
172974 sqlite3_bind_int64(pStmt, 5, iEndBlock);
172975 }else{
172976 char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
172977 if( !zEnd ) return SQLITE_NOMEM7;
172978 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
172979 }
172980 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC((sqlite3_destructor_type)0));
172981 sqlite3_step(pStmt);
172982 rc = sqlite3_reset(pStmt);
172983 sqlite3_bind_null(pStmt, 6);
172984 }
172985 return rc;
172986}
172987
172988/*
172989** Return the size of the common prefix (if any) shared by zPrev and
172990** zNext, in bytes. For example,
172991**
172992** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3
172993** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2
172994** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0
172995*/
172996static int fts3PrefixCompress(
172997 const char *zPrev, /* Buffer containing previous term */
172998 int nPrev, /* Size of buffer zPrev in bytes */
172999 const char *zNext, /* Buffer containing next term */
173000 int nNext /* Size of buffer zNext in bytes */
173001){
173002 int n;
173003 UNUSED_PARAMETER(nNext)(void)(nNext);
173004 for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
173005 return n;
173006}
173007
173008/*
173009** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
173010** (according to memcmp) than the previous term.
173011*/
173012static int fts3NodeAddTerm(
173013 Fts3Table *p, /* Virtual table handle */
173014 SegmentNode **ppTree, /* IN/OUT: SegmentNode handle */
173015 int isCopyTerm, /* True if zTerm/nTerm is transient */
173016 const char *zTerm, /* Pointer to buffer containing term */
173017 int nTerm /* Size of term in bytes */
173018){
173019 SegmentNode *pTree = *ppTree;
173020 int rc;
173021 SegmentNode *pNew;
173022
173023 /* First try to append the term to the current node. Return early if
173024 ** this is possible.
173025 */
173026 if( pTree ){
173027 int nData = pTree->nData; /* Current size of node in bytes */
173028 int nReq = nData; /* Required space after adding zTerm */
173029 int nPrefix; /* Number of bytes of prefix compression */
173030 int nSuffix; /* Suffix length */
173031
173032 nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
173033 nSuffix = nTerm-nPrefix;
173034
173035 nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
173036 if( nReq<=p->nNodeSize || !pTree->zTerm ){
173037
173038 if( nReq>p->nNodeSize ){
173039 /* An unusual case: this is the first term to be added to the node
173040 ** and the static node buffer (p->nNodeSize bytes) is not large
173041 ** enough. Use a separately malloced buffer instead This wastes
173042 ** p->nNodeSize bytes, but since this scenario only comes about when
173043 ** the database contain two terms that share a prefix of almost 2KB,
173044 ** this is not expected to be a serious problem.
173045 */
173046 assert( pTree->aData==(char *)&pTree[1] )((void) (0));
173047 pTree->aData = (char *)sqlite3_malloc(nReq);
173048 if( !pTree->aData ){
173049 return SQLITE_NOMEM7;
173050 }
173051 }
173052
173053 if( pTree->zTerm ){
173054 /* There is no prefix-length field for first term in a node */
173055 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
173056 }
173057
173058 nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
173059 memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
173060 pTree->nData = nData + nSuffix;
173061 pTree->nEntry++;
173062
173063 if( isCopyTerm ){
173064 if( pTree->nMalloc<nTerm ){
173065 char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
173066 if( !zNew ){
173067 return SQLITE_NOMEM7;
173068 }
173069 pTree->nMalloc = nTerm*2;
173070 pTree->zMalloc = zNew;
173071 }
173072 pTree->zTerm = pTree->zMalloc;
173073 memcpy(pTree->zTerm, zTerm, nTerm);
173074 pTree->nTerm = nTerm;
173075 }else{
173076 pTree->zTerm = (char *)zTerm;
173077 pTree->nTerm = nTerm;
173078 }
173079 return SQLITE_OK0;
173080 }
173081 }
173082
173083 /* If control flows to here, it was not possible to append zTerm to the
173084 ** current node. Create a new node (a right-sibling of the current node).
173085 ** If this is the first node in the tree, the term is added to it.
173086 **
173087 ** Otherwise, the term is not added to the new node, it is left empty for
173088 ** now. Instead, the term is inserted into the parent of pTree. If pTree
173089 ** has no parent, one is created here.
173090 */
173091 pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
173092 if( !pNew ){
173093 return SQLITE_NOMEM7;
173094 }
173095 memset(pNew, 0, sizeof(SegmentNode));
173096 pNew->nData = 1 + FTS3_VARINT_MAX;
173097 pNew->aData = (char *)&pNew[1];
173098
173099 if( pTree ){
173100 SegmentNode *pParent = pTree->pParent;
173101 rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
173102 if( pTree->pParent==0 ){
173103 pTree->pParent = pParent;
173104 }
173105 pTree->pRight = pNew;
173106 pNew->pLeftmost = pTree->pLeftmost;
173107 pNew->pParent = pParent;
173108 pNew->zMalloc = pTree->zMalloc;
173109 pNew->nMalloc = pTree->nMalloc;
173110 pTree->zMalloc = 0;
173111 }else{
173112 pNew->pLeftmost = pNew;
173113 rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
173114 }
173115
173116 *ppTree = pNew;
173117 return rc;
173118}
173119
173120/*
173121** Helper function for fts3NodeWrite().
173122*/
173123static int fts3TreeFinishNode(
173124 SegmentNode *pTree,
173125 int iHeight,
173126 sqlite3_int64 iLeftChild
173127){
173128 int nStart;
173129 assert( iHeight>=1 && iHeight<128 )((void) (0));
173130 nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
173131 pTree->aData[nStart] = (char)iHeight;
173132 sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
173133 return nStart;
173134}
173135
173136/*
173137** Write the buffer for the segment node pTree and all of its peers to the
173138** database. Then call this function recursively to write the parent of
173139** pTree and its peers to the database.
173140**
173141** Except, if pTree is a root node, do not write it to the database. Instead,
173142** set output variables *paRoot and *pnRoot to contain the root node.
173143**
173144** If successful, SQLITE_OK is returned and output variable *piLast is
173145** set to the largest blockid written to the database (or zero if no
173146** blocks were written to the db). Otherwise, an SQLite error code is
173147** returned.
173148*/
173149static int fts3NodeWrite(
173150 Fts3Table *p, /* Virtual table handle */
173151 SegmentNode *pTree, /* SegmentNode handle */
173152 int iHeight, /* Height of this node in tree */
173153 sqlite3_int64 iLeaf, /* Block id of first leaf node */
173154 sqlite3_int64 iFree, /* Block id of next free slot in %_segments */
173155 sqlite3_int64 *piLast, /* OUT: Block id of last entry written */
173156 char **paRoot, /* OUT: Data for root node */
173157 int *pnRoot /* OUT: Size of root node in bytes */
173158){
173159 int rc = SQLITE_OK0;
173160
173161 if( !pTree->pParent ){
173162 /* Root node of the tree. */
173163 int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
173164 *piLast = iFree-1;
173165 *pnRoot = pTree->nData - nStart;
173166 *paRoot = &pTree->aData[nStart];
173167 }else{
173168 SegmentNode *pIter;
173169 sqlite3_int64 iNextFree = iFree;
173170 sqlite3_int64 iNextLeaf = iLeaf;
173171 for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK0; pIter=pIter->pRight){
173172 int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
173173 int nWrite = pIter->nData - nStart;
173174
173175 rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
173176 iNextFree++;
173177 iNextLeaf += (pIter->nEntry+1);
173178 }
173179 if( rc==SQLITE_OK0 ){
173180 assert( iNextLeaf==iFree )((void) (0));
173181 rc = fts3NodeWrite(
173182 p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
173183 );
173184 }
173185 }
173186
173187 return rc;
173188}
173189
173190/*
173191** Free all memory allocations associated with the tree pTree.
173192*/
173193static void fts3NodeFree(SegmentNode *pTree){
173194 if( pTree ){
173195 SegmentNode *p = pTree->pLeftmost;
173196 fts3NodeFree(p->pParent);
173197 while( p ){
173198 SegmentNode *pRight = p->pRight;
173199 if( p->aData!=(char *)&p[1] ){
173200 sqlite3_free(p->aData);
173201 }
173202 assert( pRight==0 || p->zMalloc==0 )((void) (0));
173203 sqlite3_free(p->zMalloc);
173204 sqlite3_free(p);
173205 p = pRight;
173206 }
173207 }
173208}
173209
173210/*
173211** Add a term to the segment being constructed by the SegmentWriter object
173212** *ppWriter. When adding the first term to a segment, *ppWriter should
173213** be passed NULL. This function will allocate a new SegmentWriter object
173214** and return it via the input/output variable *ppWriter in this case.
173215**
173216** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
173217*/
173218static int fts3SegWriterAdd(
173219 Fts3Table *p, /* Virtual table handle */
173220 SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */
173221 int isCopyTerm, /* True if buffer zTerm must be copied */
173222 const char *zTerm, /* Pointer to buffer containing term */
173223 int nTerm, /* Size of term in bytes */
173224 const char *aDoclist, /* Pointer to buffer containing doclist */
173225 int nDoclist /* Size of doclist in bytes */
173226){
173227 int nPrefix; /* Size of term prefix in bytes */
173228 int nSuffix; /* Size of term suffix in bytes */
173229 int nReq; /* Number of bytes required on leaf page */
173230 int nData;
173231 SegmentWriter *pWriter = *ppWriter;
173232
173233 if( !pWriter ){
173234 int rc;
173235 sqlite3_stmt *pStmt;
173236
173237 /* Allocate the SegmentWriter structure */
173238 pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
173239 if( !pWriter ) return SQLITE_NOMEM7;
173240 memset(pWriter, 0, sizeof(SegmentWriter));
173241 *ppWriter = pWriter;
173242
173243 /* Allocate a buffer in which to accumulate data */
173244 pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
173245 if( !pWriter->aData ) return SQLITE_NOMEM7;
173246 pWriter->nSize = p->nNodeSize;
173247
173248 /* Find the next free blockid in the %_segments table */
173249 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
173250 if( rc!=SQLITE_OK0 ) return rc;
173251 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
173252 pWriter->iFree = sqlite3_column_int64(pStmt, 0);
173253 pWriter->iFirst = pWriter->iFree;
173254 }
173255 rc = sqlite3_reset(pStmt);
173256 if( rc!=SQLITE_OK0 ) return rc;
173257 }
173258 nData = pWriter->nData;
173259
173260 nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
173261 nSuffix = nTerm-nPrefix;
173262
173263 /* If nSuffix is zero or less, then zTerm/nTerm must be a prefix of
173264 ** pWriter->zTerm/pWriter->nTerm. i.e. must be equal to or less than when
173265 ** compared with BINARY collation. This indicates corruption. */
173266 if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
173267
173268 /* Figure out how many bytes are required by this new entry */
173269 nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */
173270 sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */
173271 nSuffix + /* Term suffix */
173272 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
173273 nDoclist; /* Doclist data */
173274
173275 if( nData>0 && nData+nReq>p->nNodeSize ){
173276 int rc;
173277
173278 /* The current leaf node is full. Write it out to the database. */
173279 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
173280 if( rc!=SQLITE_OK0 ) return rc;
173281 p->nLeafAdd++;
173282
173283 /* Add the current term to the interior node tree. The term added to
173284 ** the interior tree must:
173285 **
173286 ** a) be greater than the largest term on the leaf node just written
173287 ** to the database (still available in pWriter->zTerm), and
173288 **
173289 ** b) be less than or equal to the term about to be added to the new
173290 ** leaf node (zTerm/nTerm).
173291 **
173292 ** In other words, it must be the prefix of zTerm 1 byte longer than
173293 ** the common prefix (if any) of zTerm and pWriter->zTerm.
173294 */
173295 assert( nPrefix<nTerm )((void) (0));
173296 rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
173297 if( rc!=SQLITE_OK0 ) return rc;
173298
173299 nData = 0;
173300 pWriter->nTerm = 0;
173301
173302 nPrefix = 0;
173303 nSuffix = nTerm;
173304 nReq = 1 + /* varint containing prefix size */
173305 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
173306 nTerm + /* Term suffix */
173307 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
173308 nDoclist; /* Doclist data */
173309 }
173310
173311 /* Increase the total number of bytes written to account for the new entry. */
173312 pWriter->nLeafData += nReq;
173313
173314 /* If the buffer currently allocated is too small for this entry, realloc
173315 ** the buffer to make it large enough.
173316 */
173317 if( nReq>pWriter->nSize ){
173318 char *aNew = sqlite3_realloc(pWriter->aData, nReq);
173319 if( !aNew ) return SQLITE_NOMEM7;
173320 pWriter->aData = aNew;
173321 pWriter->nSize = nReq;
173322 }
173323 assert( nData+nReq<=pWriter->nSize )((void) (0));
173324
173325 /* Append the prefix-compressed term and doclist to the buffer. */
173326 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
173327 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
173328 memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
173329 nData += nSuffix;
173330 nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
173331 memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
173332 pWriter->nData = nData + nDoclist;
173333
173334 /* Save the current term so that it can be used to prefix-compress the next.
173335 ** If the isCopyTerm parameter is true, then the buffer pointed to by
173336 ** zTerm is transient, so take a copy of the term data. Otherwise, just
173337 ** store a copy of the pointer.
173338 */
173339 if( isCopyTerm ){
173340 if( nTerm>pWriter->nMalloc ){
173341 char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
173342 if( !zNew ){
173343 return SQLITE_NOMEM7;
173344 }
173345 pWriter->nMalloc = nTerm*2;
173346 pWriter->zMalloc = zNew;
173347 pWriter->zTerm = zNew;
173348 }
173349 assert( pWriter->zTerm==pWriter->zMalloc )((void) (0));
173350 memcpy(pWriter->zTerm, zTerm, nTerm);
173351 }else{
173352 pWriter->zTerm = (char *)zTerm;
173353 }
173354 pWriter->nTerm = nTerm;
173355
173356 return SQLITE_OK0;
173357}
173358
173359/*
173360** Flush all data associated with the SegmentWriter object pWriter to the
173361** database. This function must be called after all terms have been added
173362** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
173363** returned. Otherwise, an SQLite error code.
173364*/
173365static int fts3SegWriterFlush(
173366 Fts3Table *p, /* Virtual table handle */
173367 SegmentWriter *pWriter, /* SegmentWriter to flush to the db */
173368 sqlite3_int64 iLevel, /* Value for 'level' column of %_segdir */
173369 int iIdx /* Value for 'idx' column of %_segdir */
173370){
173371 int rc; /* Return code */
173372 if( pWriter->pTree ){
173373 sqlite3_int64 iLast = 0; /* Largest block id written to database */
173374 sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */
173375 char *zRoot = NULL((void*)0); /* Pointer to buffer containing root node */
173376 int nRoot = 0; /* Size of buffer zRoot */
173377
173378 iLastLeaf = pWriter->iFree;
173379 rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
173380 if( rc==SQLITE_OK0 ){
173381 rc = fts3NodeWrite(p, pWriter->pTree, 1,
173382 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
173383 }
173384 if( rc==SQLITE_OK0 ){
173385 rc = fts3WriteSegdir(p, iLevel, iIdx,
173386 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
173387 }
173388 }else{
173389 /* The entire tree fits on the root node. Write it to the segdir table. */
173390 rc = fts3WriteSegdir(p, iLevel, iIdx,
173391 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
173392 }
173393 p->nLeafAdd++;
173394 return rc;
173395}
173396
173397/*
173398** Release all memory held by the SegmentWriter object passed as the
173399** first argument.
173400*/
173401static void fts3SegWriterFree(SegmentWriter *pWriter){
173402 if( pWriter ){
173403 sqlite3_free(pWriter->aData);
173404 sqlite3_free(pWriter->zMalloc);
173405 fts3NodeFree(pWriter->pTree);
173406 sqlite3_free(pWriter);
173407 }
173408}
173409
173410/*
173411** The first value in the apVal[] array is assumed to contain an integer.
173412** This function tests if there exist any documents with docid values that
173413** are different from that integer. i.e. if deleting the document with docid
173414** pRowid would mean the FTS3 table were empty.
173415**
173416** If successful, *pisEmpty is set to true if the table is empty except for
173417** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
173418** error occurs, an SQLite error code is returned.
173419*/
173420static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
173421 sqlite3_stmt *pStmt;
173422 int rc;
173423 if( p->zContentTbl ){
173424 /* If using the content=xxx option, assume the table is never empty */
173425 *pisEmpty = 0;
173426 rc = SQLITE_OK0;
173427 }else{
173428 rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
173429 if( rc==SQLITE_OK0 ){
173430 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
173431 *pisEmpty = sqlite3_column_int(pStmt, 0);
173432 }
173433 rc = sqlite3_reset(pStmt);
173434 }
173435 }
173436 return rc;
173437}
173438
173439/*
173440** Set *pnMax to the largest segment level in the database for the index
173441** iIndex.
173442**
173443** Segment levels are stored in the 'level' column of the %_segdir table.
173444**
173445** Return SQLITE_OK if successful, or an SQLite error code if not.
173446*/
173447static int fts3SegmentMaxLevel(
173448 Fts3Table *p,
173449 int iLangid,
173450 int iIndex,
173451 sqlite3_int64 *pnMax
173452){
173453 sqlite3_stmt *pStmt;
173454 int rc;
173455 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
173456
173457 /* Set pStmt to the compiled version of:
173458 **
173459 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
173460 **
173461 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
173462 */
173463 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
173464 if( rc!=SQLITE_OK0 ) return rc;
173465 sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
173466 sqlite3_bind_int64(pStmt, 2,
173467 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
173468 );
173469 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
173470 *pnMax = sqlite3_column_int64(pStmt, 0);
173471 }
173472 return sqlite3_reset(pStmt);
173473}
173474
173475/*
173476** iAbsLevel is an absolute level that may be assumed to exist within
173477** the database. This function checks if it is the largest level number
173478** within its index. Assuming no error occurs, *pbMax is set to 1 if
173479** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
173480** is returned. If an error occurs, an error code is returned and the
173481** final value of *pbMax is undefined.
173482*/
173483static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
173484
173485 /* Set pStmt to the compiled version of:
173486 **
173487 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
173488 **
173489 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
173490 */
173491 sqlite3_stmt *pStmt;
173492 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
173493 if( rc!=SQLITE_OK0 ) return rc;
173494 sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
173495 sqlite3_bind_int64(pStmt, 2,
173496 ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
173497 );
173498
173499 *pbMax = 0;
173500 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
173501 *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL5;
173502 }
173503 return sqlite3_reset(pStmt);
173504}
173505
173506/*
173507** Delete all entries in the %_segments table associated with the segment
173508** opened with seg-reader pSeg. This function does not affect the contents
173509** of the %_segdir table.
173510*/
173511static int fts3DeleteSegment(
173512 Fts3Table *p, /* FTS table handle */
173513 Fts3SegReader *pSeg /* Segment to delete */
173514){
173515 int rc = SQLITE_OK0; /* Return code */
173516 if( pSeg->iStartBlock ){
173517 sqlite3_stmt *pDelete; /* SQL statement to delete rows */
173518 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
173519 if( rc==SQLITE_OK0 ){
173520 sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
173521 sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
173522 sqlite3_step(pDelete);
173523 rc = sqlite3_reset(pDelete);
173524 }
173525 }
173526 return rc;
173527}
173528
173529/*
173530** This function is used after merging multiple segments into a single large
173531** segment to delete the old, now redundant, segment b-trees. Specifically,
173532** it:
173533**
173534** 1) Deletes all %_segments entries for the segments associated with
173535** each of the SegReader objects in the array passed as the third
173536** argument, and
173537**
173538** 2) deletes all %_segdir entries with level iLevel, or all %_segdir
173539** entries regardless of level if (iLevel<0).
173540**
173541** SQLITE_OK is returned if successful, otherwise an SQLite error code.
173542*/
173543static int fts3DeleteSegdir(
173544 Fts3Table *p, /* Virtual table handle */
173545 int iLangid, /* Language id */
173546 int iIndex, /* Index for p->aIndex */
173547 int iLevel, /* Level of %_segdir entries to delete */
173548 Fts3SegReader **apSegment, /* Array of SegReader objects */
173549 int nReader /* Size of array apSegment */
173550){
173551 int rc = SQLITE_OK0; /* Return Code */
173552 int i; /* Iterator variable */
173553 sqlite3_stmt *pDelete = 0; /* SQL statement to delete rows */
173554
173555 for(i=0; rc==SQLITE_OK0 && i<nReader; i++){
173556 rc = fts3DeleteSegment(p, apSegment[i]);
173557 }
173558 if( rc!=SQLITE_OK0 ){
173559 return rc;
173560 }
173561
173562 assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL )((void) (0));
173563 if( iLevel==FTS3_SEGCURSOR_ALL ){
173564 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
173565 if( rc==SQLITE_OK0 ){
173566 sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
173567 sqlite3_bind_int64(pDelete, 2,
173568 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
173569 );
173570 }
173571 }else{
173572 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
173573 if( rc==SQLITE_OK0 ){
173574 sqlite3_bind_int64(
173575 pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
173576 );
173577 }
173578 }
173579
173580 if( rc==SQLITE_OK0 ){
173581 sqlite3_step(pDelete);
173582 rc = sqlite3_reset(pDelete);
173583 }
173584
173585 return rc;
173586}
173587
173588/*
173589** When this function is called, buffer *ppList (size *pnList bytes) contains
173590** a position list that may (or may not) feature multiple columns. This
173591** function adjusts the pointer *ppList and the length *pnList so that they
173592** identify the subset of the position list that corresponds to column iCol.
173593**
173594** If there are no entries in the input position list for column iCol, then
173595** *pnList is set to zero before returning.
173596**
173597** If parameter bZero is non-zero, then any part of the input list following
173598** the end of the output list is zeroed before returning.
173599*/
173600static void fts3ColumnFilter(
173601 int iCol, /* Column to filter on */
173602 int bZero, /* Zero out anything following *ppList */
173603 char **ppList, /* IN/OUT: Pointer to position list */
173604 int *pnList /* IN/OUT: Size of buffer *ppList in bytes */
173605){
173606 char *pList = *ppList;
173607 int nList = *pnList;
173608 char *pEnd = &pList[nList];
173609 int iCurrent = 0;
173610 char *p = pList;
173611
173612 assert( iCol>=0 )((void) (0));
173613 while( 1 ){
173614 char c = 0;
173615 while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
173616
173617 if( iCol==iCurrent ){
173618 nList = (int)(p - pList);
173619 break;
173620 }
173621
173622 nList -= (int)(p - pList);
173623 pList = p;
173624 if( nList<=0 ){
173625 break;
173626 }
173627 p = &pList[1];
173628 p += fts3GetVarint32(p, &iCurrent);
173629 }
173630
173631 if( bZero && (pEnd - &pList[nList])>0){
173632 memset(&pList[nList], 0, pEnd - &pList[nList]);
173633 }
173634 *ppList = pList;
173635 *pnList = nList;
173636}
173637
173638/*
173639** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
173640** existing data). Grow the buffer if required.
173641**
173642** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
173643** trying to resize the buffer, return SQLITE_NOMEM.
173644*/
173645static int fts3MsrBufferData(
173646 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
173647 char *pList,
173648 int nList
173649){
173650 if( nList>pMsr->nBuffer ){
173651 char *pNew;
173652 pMsr->nBuffer = nList*2;
173653 pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
173654 if( !pNew ) return SQLITE_NOMEM7;
173655 pMsr->aBuffer = pNew;
173656 }
173657
173658 memcpy(pMsr->aBuffer, pList, nList);
173659 return SQLITE_OK0;
173660}
173661
173662SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrNext(
173663 Fts3Table *p, /* Virtual table handle */
173664 Fts3MultiSegReader *pMsr, /* Multi-segment-reader handle */
173665 sqlite3_int64 *piDocid, /* OUT: Docid value */
173666 char **paPoslist, /* OUT: Pointer to position list */
173667 int *pnPoslist /* OUT: Size of position list in bytes */
173668){
173669 int nMerge = pMsr->nAdvance;
173670 Fts3SegReader **apSegment = pMsr->apSegment;
173671 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
173672 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
173673 );
173674
173675 if( nMerge==0 ){
173676 *paPoslist = 0;
173677 return SQLITE_OK0;
173678 }
173679
173680 while( 1 ){
173681 Fts3SegReader *pSeg;
173682 pSeg = pMsr->apSegment[0];
173683
173684 if( pSeg->pOffsetList==0 ){
173685 *paPoslist = 0;
173686 break;
173687 }else{
173688 int rc;
173689 char *pList;
173690 int nList;
173691 int j;
173692 sqlite3_int64 iDocid = apSegment[0]->iDocid;
173693
173694 rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
173695 j = 1;
173696 while( rc==SQLITE_OK0
173697 && j<nMerge
173698 && apSegment[j]->pOffsetList
173699 && apSegment[j]->iDocid==iDocid
173700 ){
173701 rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
173702 j++;
173703 }
173704 if( rc!=SQLITE_OK0 ) return rc;
173705 fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
173706
173707 if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
173708 rc = fts3MsrBufferData(pMsr, pList, nList+1);
173709 if( rc!=SQLITE_OK0 ) return rc;
173710 assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 )((void) (0));
173711 pList = pMsr->aBuffer;
173712 }
173713
173714 if( pMsr->iColFilter>=0 ){
173715 fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
173716 }
173717
173718 if( nList>0 ){
173719 *paPoslist = pList;
173720 *piDocid = iDocid;
173721 *pnPoslist = nList;
173722 break;
173723 }
173724 }
173725 }
173726
173727 return SQLITE_OK0;
173728}
173729
173730static int fts3SegReaderStart(
173731 Fts3Table *p, /* Virtual table handle */
173732 Fts3MultiSegReader *pCsr, /* Cursor object */
173733 const char *zTerm, /* Term searched for (or NULL) */
173734 int nTerm /* Length of zTerm in bytes */
173735){
173736 int i;
173737 int nSeg = pCsr->nSegment;
173738
173739 /* If the Fts3SegFilter defines a specific term (or term prefix) to search
173740 ** for, then advance each segment iterator until it points to a term of
173741 ** equal or greater value than the specified term. This prevents many
173742 ** unnecessary merge/sort operations for the case where single segment
173743 ** b-tree leaf nodes contain more than one term.
173744 */
173745 for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
173746 int res = 0;
173747 Fts3SegReader *pSeg = pCsr->apSegment[i];
173748 do {
173749 int rc = fts3SegReaderNext(p, pSeg, 0);
173750 if( rc!=SQLITE_OK0 ) return rc;
173751 }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
173752
173753 if( pSeg->bLookup && res!=0 ){
173754 fts3SegReaderSetEof(pSeg);
173755 }
173756 }
173757 fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
173758
173759 return SQLITE_OK0;
173760}
173761
173762SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStart(
173763 Fts3Table *p, /* Virtual table handle */
173764 Fts3MultiSegReader *pCsr, /* Cursor object */
173765 Fts3SegFilter *pFilter /* Restrictions on range of iteration */
173766){
173767 pCsr->pFilter = pFilter;
173768 return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
173769}
173770
173771SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrStart(
173772 Fts3Table *p, /* Virtual table handle */
173773 Fts3MultiSegReader *pCsr, /* Cursor object */
173774 int iCol, /* Column to match on. */
173775 const char *zTerm, /* Term to iterate through a doclist for */
173776 int nTerm /* Number of bytes in zTerm */
173777){
173778 int i;
173779 int rc;
173780 int nSegment = pCsr->nSegment;
173781 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
173782 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
173783 );
173784
173785 assert( pCsr->pFilter==0 )((void) (0));
173786 assert( zTerm && nTerm>0 )((void) (0));
173787
173788 /* Advance each segment iterator until it points to the term zTerm/nTerm. */
173789 rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
173790 if( rc!=SQLITE_OK0 ) return rc;
173791
173792 /* Determine how many of the segments actually point to zTerm/nTerm. */
173793 for(i=0; i<nSegment; i++){
173794 Fts3SegReader *pSeg = pCsr->apSegment[i];
173795 if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
173796 break;
173797 }
173798 }
173799 pCsr->nAdvance = i;
173800
173801 /* Advance each of the segments to point to the first docid. */
173802 for(i=0; i<pCsr->nAdvance; i++){
173803 rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
173804 if( rc!=SQLITE_OK0 ) return rc;
173805 }
173806 fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
173807
173808 assert( iCol<0 || iCol<p->nColumn )((void) (0));
173809 pCsr->iColFilter = iCol;
173810
173811 return SQLITE_OK0;
173812}
173813
173814/*
173815** This function is called on a MultiSegReader that has been started using
173816** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
173817** have been made. Calling this function puts the MultiSegReader in such
173818** a state that if the next two calls are:
173819**
173820** sqlite3Fts3SegReaderStart()
173821** sqlite3Fts3SegReaderStep()
173822**
173823** then the entire doclist for the term is available in
173824** MultiSegReader.aDoclist/nDoclist.
173825*/
173826SQLITE_PRIVATEstatic int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
173827 int i; /* Used to iterate through segment-readers */
173828
173829 assert( pCsr->zTerm==0 )((void) (0));
173830 assert( pCsr->nTerm==0 )((void) (0));
173831 assert( pCsr->aDoclist==0 )((void) (0));
173832 assert( pCsr->nDoclist==0 )((void) (0));
173833
173834 pCsr->nAdvance = 0;
173835 pCsr->bRestart = 1;
173836 for(i=0; i<pCsr->nSegment; i++){
173837 pCsr->apSegment[i]->pOffsetList = 0;
173838 pCsr->apSegment[i]->nOffsetList = 0;
173839 pCsr->apSegment[i]->iDocid = 0;
173840 }
173841
173842 return SQLITE_OK0;
173843}
173844
173845
173846SQLITE_PRIVATEstatic int sqlite3Fts3SegReaderStep(
173847 Fts3Table *p, /* Virtual table handle */
173848 Fts3MultiSegReader *pCsr /* Cursor object */
173849){
173850 int rc = SQLITE_OK0;
173851
173852 int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
173853 int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
173854 int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
173855 int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
173856 int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
173857 int isFirst = (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
173858
173859 Fts3SegReader **apSegment = pCsr->apSegment;
173860 int nSegment = pCsr->nSegment;
173861 Fts3SegFilter *pFilter = pCsr->pFilter;
173862 int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
173863 p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
173864 );
173865
173866 if( pCsr->nSegment==0 ) return SQLITE_OK0;
173867
173868 do {
173869 int nMerge;
173870 int i;
173871
173872 /* Advance the first pCsr->nAdvance entries in the apSegment[] array
173873 ** forward. Then sort the list in order of current term again.
173874 */
173875 for(i=0; i<pCsr->nAdvance; i++){
173876 Fts3SegReader *pSeg = apSegment[i];
173877 if( pSeg->bLookup ){
173878 fts3SegReaderSetEof(pSeg);
173879 }else{
173880 rc = fts3SegReaderNext(p, pSeg, 0);
173881 }
173882 if( rc!=SQLITE_OK0 ) return rc;
173883 }
173884 fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
173885 pCsr->nAdvance = 0;
173886
173887 /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
173888 assert( rc==SQLITE_OK )((void) (0));
173889 if( apSegment[0]->aNode==0 ) break;
173890
173891 pCsr->nTerm = apSegment[0]->nTerm;
173892 pCsr->zTerm = apSegment[0]->zTerm;
173893
173894 /* If this is a prefix-search, and if the term that apSegment[0] points
173895 ** to does not share a suffix with pFilter->zTerm/nTerm, then all
173896 ** required callbacks have been made. In this case exit early.
173897 **
173898 ** Similarly, if this is a search for an exact match, and the first term
173899 ** of segment apSegment[0] is not a match, exit early.
173900 */
173901 if( pFilter->zTerm && !isScan ){
173902 if( pCsr->nTerm<pFilter->nTerm
173903 || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
173904 || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
173905 ){
173906 break;
173907 }
173908 }
173909
173910 nMerge = 1;
173911 while( nMerge<nSegment
173912 && apSegment[nMerge]->aNode
173913 && apSegment[nMerge]->nTerm==pCsr->nTerm
173914 && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
173915 ){
173916 nMerge++;
173917 }
173918
173919 assert( isIgnoreEmpty || (isRequirePos && !isColFilter) )((void) (0));
173920 if( nMerge==1
173921 && !isIgnoreEmpty
173922 && !isFirst
173923 && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
173924 ){
173925 pCsr->nDoclist = apSegment[0]->nDoclist;
173926 if( fts3SegReaderIsPending(apSegment[0]) ){
173927 rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
173928 pCsr->aDoclist = pCsr->aBuffer;
173929 }else{
173930 pCsr->aDoclist = apSegment[0]->aDoclist;
173931 }
173932 if( rc==SQLITE_OK0 ) rc = SQLITE_ROW100;
173933 }else{
173934 int nDoclist = 0; /* Size of doclist */
173935 sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */
173936
173937 /* The current term of the first nMerge entries in the array
173938 ** of Fts3SegReader objects is the same. The doclists must be merged
173939 ** and a single term returned with the merged doclist.
173940 */
173941 for(i=0; i<nMerge; i++){
173942 fts3SegReaderFirstDocid(p, apSegment[i]);
173943 }
173944 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
173945 while( apSegment[0]->pOffsetList ){
173946 int j; /* Number of segments that share a docid */
173947 char *pList = 0;
173948 int nList = 0;
173949 int nByte;
173950 sqlite3_int64 iDocid = apSegment[0]->iDocid;
173951 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
173952 j = 1;
173953 while( j<nMerge
173954 && apSegment[j]->pOffsetList
173955 && apSegment[j]->iDocid==iDocid
173956 ){
173957 fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
173958 j++;
173959 }
173960
173961 if( isColFilter ){
173962 fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
173963 }
173964
173965 if( !isIgnoreEmpty || nList>0 ){
173966
173967 /* Calculate the 'docid' delta value to write into the merged
173968 ** doclist. */
173969 sqlite3_int64 iDelta;
173970 if( p->bDescIdx && nDoclist>0 ){
173971 iDelta = iPrev - iDocid;
173972 }else{
173973 iDelta = iDocid - iPrev;
173974 }
173975 if( iDelta<=0 && (nDoclist>0 || iDelta!=iDocid) ){
173976 return FTS_CORRUPT_VTAB;
173977 }
173978 assert( nDoclist>0 || iDelta==iDocid )((void) (0));
173979
173980 nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
173981 if( nDoclist+nByte>pCsr->nBuffer ){
173982 char *aNew;
173983 pCsr->nBuffer = (nDoclist+nByte)*2;
173984 aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
173985 if( !aNew ){
173986 return SQLITE_NOMEM7;
173987 }
173988 pCsr->aBuffer = aNew;
173989 }
173990
173991 if( isFirst ){
173992 char *a = &pCsr->aBuffer[nDoclist];
173993 int nWrite;
173994
173995 nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
173996 if( nWrite ){
173997 iPrev = iDocid;
173998 nDoclist += nWrite;
173999 }
174000 }else{
174001 nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
174002 iPrev = iDocid;
174003 if( isRequirePos ){
174004 memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
174005 nDoclist += nList;
174006 pCsr->aBuffer[nDoclist++] = '\0';
174007 }
174008 }
174009 }
174010
174011 fts3SegReaderSort(apSegment, nMerge, j, xCmp);
174012 }
174013 if( nDoclist>0 ){
174014 pCsr->aDoclist = pCsr->aBuffer;
174015 pCsr->nDoclist = nDoclist;
174016 rc = SQLITE_ROW100;
174017 }
174018 }
174019 pCsr->nAdvance = nMerge;
174020 }while( rc==SQLITE_OK0 );
174021
174022 return rc;
174023}
174024
174025
174026SQLITE_PRIVATEstatic void sqlite3Fts3SegReaderFinish(
174027 Fts3MultiSegReader *pCsr /* Cursor object */
174028){
174029 if( pCsr ){
174030 int i;
174031 for(i=0; i<pCsr->nSegment; i++){
174032 sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
174033 }
174034 sqlite3_free(pCsr->apSegment);
174035 sqlite3_free(pCsr->aBuffer);
174036
174037 pCsr->nSegment = 0;
174038 pCsr->apSegment = 0;
174039 pCsr->aBuffer = 0;
174040 }
174041}
174042
174043/*
174044** Decode the "end_block" field, selected by column iCol of the SELECT
174045** statement passed as the first argument.
174046**
174047** The "end_block" field may contain either an integer, or a text field
174048** containing the text representation of two non-negative integers separated
174049** by one or more space (0x20) characters. In the first case, set *piEndBlock
174050** to the integer value and *pnByte to zero before returning. In the second,
174051** set *piEndBlock to the first value and *pnByte to the second.
174052*/
174053static void fts3ReadEndBlockField(
174054 sqlite3_stmt *pStmt,
174055 int iCol,
174056 i64 *piEndBlock,
174057 i64 *pnByte
174058){
174059 const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
174060 if( zText ){
174061 int i;
174062 int iMul = 1;
174063 i64 iVal = 0;
174064 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
174065 iVal = iVal*10 + (zText[i] - '0');
174066 }
174067 *piEndBlock = iVal;
174068 while( zText[i]==' ' ) i++;
174069 iVal = 0;
174070 if( zText[i]=='-' ){
174071 i++;
174072 iMul = -1;
174073 }
174074 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
174075 iVal = iVal*10 + (zText[i] - '0');
174076 }
174077 *pnByte = (iVal * (i64)iMul);
174078 }
174079}
174080
174081
174082/*
174083** A segment of size nByte bytes has just been written to absolute level
174084** iAbsLevel. Promote any segments that should be promoted as a result.
174085*/
174086static int fts3PromoteSegments(
174087 Fts3Table *p, /* FTS table handle */
174088 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
174089 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
174090){
174091 int rc = SQLITE_OK0;
174092 sqlite3_stmt *pRange;
174093
174094 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
174095
174096 if( rc==SQLITE_OK0 ){
174097 int bOk = 0;
174098 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
174099 i64 nLimit = (nByte*3)/2;
174100
174101 /* Loop through all entries in the %_segdir table corresponding to
174102 ** segments in this index on levels greater than iAbsLevel. If there is
174103 ** at least one such segment, and it is possible to determine that all
174104 ** such segments are smaller than nLimit bytes in size, they will be
174105 ** promoted to level iAbsLevel. */
174106 sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
174107 sqlite3_bind_int64(pRange, 2, iLast);
174108 while( SQLITE_ROW100==sqlite3_step(pRange) ){
174109 i64 nSize = 0, dummy;
174110 fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
174111 if( nSize<=0 || nSize>nLimit ){
174112 /* If nSize==0, then the %_segdir.end_block field does not not
174113 ** contain a size value. This happens if it was written by an
174114 ** old version of FTS. In this case it is not possible to determine
174115 ** the size of the segment, and so segment promotion does not
174116 ** take place. */
174117 bOk = 0;
174118 break;
174119 }
174120 bOk = 1;
174121 }
174122 rc = sqlite3_reset(pRange);
174123
174124 if( bOk ){
174125 int iIdx = 0;
174126 sqlite3_stmt *pUpdate1 = 0;
174127 sqlite3_stmt *pUpdate2 = 0;
174128
174129 if( rc==SQLITE_OK0 ){
174130 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
174131 }
174132 if( rc==SQLITE_OK0 ){
174133 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
174134 }
174135
174136 if( rc==SQLITE_OK0 ){
174137
174138 /* Loop through all %_segdir entries for segments in this index with
174139 ** levels equal to or greater than iAbsLevel. As each entry is visited,
174140 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
174141 ** oldest segment in the range, 1 for the next oldest, and so on.
174142 **
174143 ** In other words, move all segments being promoted to level -1,
174144 ** setting the "idx" fields as appropriate to keep them in the same
174145 ** order. The contents of level -1 (which is never used, except
174146 ** transiently here), will be moved back to level iAbsLevel below. */
174147 sqlite3_bind_int64(pRange, 1, iAbsLevel);
174148 while( SQLITE_ROW100==sqlite3_step(pRange) ){
174149 sqlite3_bind_int(pUpdate1, 1, iIdx++);
174150 sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
174151 sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
174152 sqlite3_step(pUpdate1);
174153 rc = sqlite3_reset(pUpdate1);
174154 if( rc!=SQLITE_OK0 ){
174155 sqlite3_reset(pRange);
174156 break;
174157 }
174158 }
174159 }
174160 if( rc==SQLITE_OK0 ){
174161 rc = sqlite3_reset(pRange);
174162 }
174163
174164 /* Move level -1 to level iAbsLevel */
174165 if( rc==SQLITE_OK0 ){
174166 sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
174167 sqlite3_step(pUpdate2);
174168 rc = sqlite3_reset(pUpdate2);
174169 }
174170 }
174171 }
174172
174173
174174 return rc;
174175}
174176
174177/*
174178** Merge all level iLevel segments in the database into a single
174179** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
174180** single segment with a level equal to the numerically largest level
174181** currently present in the database.
174182**
174183** If this function is called with iLevel<0, but there is only one
174184** segment in the database, SQLITE_DONE is returned immediately.
174185** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
174186** an SQLite error code is returned.
174187*/
174188static int fts3SegmentMerge(
174189 Fts3Table *p,
174190 int iLangid, /* Language id to merge */
174191 int iIndex, /* Index in p->aIndex[] to merge */
174192 int iLevel /* Level to merge */
174193){
174194 int rc; /* Return code */
174195 int iIdx = 0; /* Index of new segment */
174196 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
174197 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
174198 Fts3SegFilter filter; /* Segment term filter condition */
174199 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
174200 int bIgnoreEmpty = 0; /* True to ignore empty segments */
174201 i64 iMaxLevel = 0; /* Max level number for this index/langid */
174202
174203 assert( iLevel==FTS3_SEGCURSOR_ALL((void) (0))
174204 || iLevel==FTS3_SEGCURSOR_PENDING((void) (0))
174205 || iLevel>=0((void) (0))
174206 )((void) (0));
174207 assert( iLevel<FTS3_SEGDIR_MAXLEVEL )((void) (0));
174208 assert( iIndex>=0 && iIndex<p->nIndex )((void) (0));
174209
174210 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
174211 if( rc!=SQLITE_OK0 || csr.nSegment==0 ) goto finished;
174212
174213 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
174214 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
174215 if( rc!=SQLITE_OK0 ) goto finished;
174216 }
174217
174218 if( iLevel==FTS3_SEGCURSOR_ALL ){
174219 /* This call is to merge all segments in the database to a single
174220 ** segment. The level of the new segment is equal to the numerically
174221 ** greatest segment level currently present in the database for this
174222 ** index. The idx of the new segment is always 0. */
174223 if( csr.nSegment==1 && 0==fts3SegReaderIsPending(csr.apSegment[0]) ){
174224 rc = SQLITE_DONE101;
174225 goto finished;
174226 }
174227 iNewLevel = iMaxLevel;
174228 bIgnoreEmpty = 1;
174229
174230 }else{
174231 /* This call is to merge all segments at level iLevel. find the next
174232 ** available segment index at level iLevel+1. The call to
174233 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
174234 ** a single iLevel+2 segment if necessary. */
174235 assert( FTS3_SEGCURSOR_PENDING==-1 )((void) (0));
174236 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
174237 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
174238 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
174239 }
174240 if( rc!=SQLITE_OK0 ) goto finished;
174241
174242 assert( csr.nSegment>0 )((void) (0));
174243 assert_fts3_nc( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
174244 assert_fts3_nc(
174245 iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL)
174246 );
174247
174248 memset(&filter, 0, sizeof(Fts3SegFilter));
174249 filter.flags = FTS3_SEGMENT_REQUIRE_POS;
174250 filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
174251
174252 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
174253 while( SQLITE_OK0==rc ){
174254 rc = sqlite3Fts3SegReaderStep(p, &csr);
174255 if( rc!=SQLITE_ROW100 ) break;
174256 rc = fts3SegWriterAdd(p, &pWriter, 1,
174257 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
174258 }
174259 if( rc!=SQLITE_OK0 ) goto finished;
174260 assert( pWriter || bIgnoreEmpty )((void) (0));
174261
174262 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
174263 rc = fts3DeleteSegdir(
174264 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
174265 );
174266 if( rc!=SQLITE_OK0 ) goto finished;
174267 }
174268 if( pWriter ){
174269 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
174270 if( rc==SQLITE_OK0 ){
174271 if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
174272 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
174273 }
174274 }
174275 }
174276
174277 finished:
174278 fts3SegWriterFree(pWriter);
174279 sqlite3Fts3SegReaderFinish(&csr);
174280 return rc;
174281}
174282
174283
174284/*
174285** Flush the contents of pendingTerms to level 0 segments.
174286*/
174287SQLITE_PRIVATEstatic int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
174288 int rc = SQLITE_OK0;
174289 int i;
174290
174291 for(i=0; rc==SQLITE_OK0 && i<p->nIndex; i++){
174292 rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
174293 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
174294 }
174295 sqlite3Fts3PendingTermsClear(p);
174296
174297 /* Determine the auto-incr-merge setting if unknown. If enabled,
174298 ** estimate the number of leaf blocks of content to be written
174299 */
174300 if( rc==SQLITE_OK0 && p->bHasStat
174301 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
174302 ){
174303 sqlite3_stmt *pStmt = 0;
174304 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
174305 if( rc==SQLITE_OK0 ){
174306 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
174307 rc = sqlite3_step(pStmt);
174308 if( rc==SQLITE_ROW100 ){
174309 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
174310 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
174311 }else if( rc==SQLITE_DONE101 ){
174312 p->nAutoincrmerge = 0;
174313 }
174314 rc = sqlite3_reset(pStmt);
174315 }
174316 }
174317 return rc;
174318}
174319
174320/*
174321** Encode N integers as varints into a blob.
174322*/
174323static void fts3EncodeIntArray(
174324 int N, /* The number of integers to encode */
174325 u32 *a, /* The integer values */
174326 char *zBuf, /* Write the BLOB here */
174327 int *pNBuf /* Write number of bytes if zBuf[] used here */
174328){
174329 int i, j;
174330 for(i=j=0; i<N; i++){
174331 j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
174332 }
174333 *pNBuf = j;
174334}
174335
174336/*
174337** Decode a blob of varints into N integers
174338*/
174339static void fts3DecodeIntArray(
174340 int N, /* The number of integers to decode */
174341 u32 *a, /* Write the integer values */
174342 const char *zBuf, /* The BLOB containing the varints */
174343 int nBuf /* size of the BLOB */
174344){
174345 int i = 0;
174346 if( nBuf && (zBuf[nBuf-1]&0x80)==0 ){
174347 int j;
174348 for(i=j=0; i<N && j<nBuf; i++){
174349 sqlite3_int64 x;
174350 j += sqlite3Fts3GetVarint(&zBuf[j], &x);
174351 a[i] = (u32)(x & 0xffffffff);
174352 }
174353 }
174354 while( i<N ) a[i++] = 0;
174355}
174356
174357/*
174358** Insert the sizes (in tokens) for each column of the document
174359** with docid equal to p->iPrevDocid. The sizes are encoded as
174360** a blob of varints.
174361*/
174362static void fts3InsertDocsize(
174363 int *pRC, /* Result code */
174364 Fts3Table *p, /* Table into which to insert */
174365 u32 *aSz /* Sizes of each column, in tokens */
174366){
174367 char *pBlob; /* The BLOB encoding of the document size */
174368 int nBlob; /* Number of bytes in the BLOB */
174369 sqlite3_stmt *pStmt; /* Statement used to insert the encoding */
174370 int rc; /* Result code from subfunctions */
174371
174372 if( *pRC ) return;
174373 pBlob = sqlite3_malloc64( 10*(sqlite3_int64)p->nColumn );
174374 if( pBlob==0 ){
174375 *pRC = SQLITE_NOMEM7;
174376 return;
174377 }
174378 fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
174379 rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
174380 if( rc ){
174381 sqlite3_free(pBlob);
174382 *pRC = rc;
174383 return;
174384 }
174385 sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
174386 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
174387 sqlite3_step(pStmt);
174388 *pRC = sqlite3_reset(pStmt);
174389}
174390
174391/*
174392** Record 0 of the %_stat table contains a blob consisting of N varints,
174393** where N is the number of user defined columns in the fts3 table plus
174394** two. If nCol is the number of user defined columns, then values of the
174395** varints are set as follows:
174396**
174397** Varint 0: Total number of rows in the table.
174398**
174399** Varint 1..nCol: For each column, the total number of tokens stored in
174400** the column for all rows of the table.
174401**
174402** Varint 1+nCol: The total size, in bytes, of all text values in all
174403** columns of all rows of the table.
174404**
174405*/
174406static void fts3UpdateDocTotals(
174407 int *pRC, /* The result code */
174408 Fts3Table *p, /* Table being updated */
174409 u32 *aSzIns, /* Size increases */
174410 u32 *aSzDel, /* Size decreases */
174411 int nChng /* Change in the number of documents */
174412){
174413 char *pBlob; /* Storage for BLOB written into %_stat */
174414 int nBlob; /* Size of BLOB written into %_stat */
174415 u32 *a; /* Array of integers that becomes the BLOB */
174416 sqlite3_stmt *pStmt; /* Statement for reading and writing */
174417 int i; /* Loop counter */
174418 int rc; /* Result code from subfunctions */
174419
174420 const int nStat = p->nColumn+2;
174421
174422 if( *pRC ) return;
174423 a = sqlite3_malloc64( (sizeof(u32)+10)*(sqlite3_int64)nStat );
174424 if( a==0 ){
174425 *pRC = SQLITE_NOMEM7;
174426 return;
174427 }
174428 pBlob = (char*)&a[nStat];
174429 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
174430 if( rc ){
174431 sqlite3_free(a);
174432 *pRC = rc;
174433 return;
174434 }
174435 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
174436 if( sqlite3_step(pStmt)==SQLITE_ROW100 ){
174437 fts3DecodeIntArray(nStat, a,
174438 sqlite3_column_blob(pStmt, 0),
174439 sqlite3_column_bytes(pStmt, 0));
174440 }else{
174441 memset(a, 0, sizeof(u32)*(nStat) );
174442 }
174443 rc = sqlite3_reset(pStmt);
174444 if( rc!=SQLITE_OK0 ){
174445 sqlite3_free(a);
174446 *pRC = rc;
174447 return;
174448 }
174449 if( nChng<0 && a[0]<(u32)(-nChng) ){
174450 a[0] = 0;
174451 }else{
174452 a[0] += nChng;
174453 }
174454 for(i=0; i<p->nColumn+1; i++){
174455 u32 x = a[i+1];
174456 if( x+aSzIns[i] < aSzDel[i] ){
174457 x = 0;
174458 }else{
174459 x = x + aSzIns[i] - aSzDel[i];
174460 }
174461 a[i+1] = x;
174462 }
174463 fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
174464 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
174465 if( rc ){
174466 sqlite3_free(a);
174467 *pRC = rc;
174468 return;
174469 }
174470 sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
174471 sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC((sqlite3_destructor_type)0));
174472 sqlite3_step(pStmt);
174473 *pRC = sqlite3_reset(pStmt);
174474 sqlite3_bind_null(pStmt, 2);
174475 sqlite3_free(a);
174476}
174477
174478/*
174479** Merge the entire database so that there is one segment for each
174480** iIndex/iLangid combination.
174481*/
174482static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
174483 int bSeenDone = 0;
174484 int rc;
174485 sqlite3_stmt *pAllLangid = 0;
174486
174487 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
174488 if( rc==SQLITE_OK0 ){
174489 int rc2;
174490 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
174491 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
174492 while( sqlite3_step(pAllLangid)==SQLITE_ROW100 ){
174493 int i;
174494 int iLangid = sqlite3_column_int(pAllLangid, 0);
174495 for(i=0; rc==SQLITE_OK0 && i<p->nIndex; i++){
174496 rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
174497 if( rc==SQLITE_DONE101 ){
174498 bSeenDone = 1;
174499 rc = SQLITE_OK0;
174500 }
174501 }
174502 }
174503 rc2 = sqlite3_reset(pAllLangid);
174504 if( rc==SQLITE_OK0 ) rc = rc2;
174505 }
174506
174507 sqlite3Fts3SegmentsClose(p);
174508 sqlite3Fts3PendingTermsClear(p);
174509
174510 return (rc==SQLITE_OK0 && bReturnDone && bSeenDone) ? SQLITE_DONE101 : rc;
174511}
174512
174513/*
174514** This function is called when the user executes the following statement:
174515**
174516** INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
174517**
174518** The entire FTS index is discarded and rebuilt. If the table is one
174519** created using the content=xxx option, then the new index is based on
174520** the current contents of the xxx table. Otherwise, it is rebuilt based
174521** on the contents of the %_content table.
174522*/
174523static int fts3DoRebuild(Fts3Table *p){
174524 int rc; /* Return Code */
174525
174526 rc = fts3DeleteAll(p, 0);
174527 if( rc==SQLITE_OK0 ){
174528 u32 *aSz = 0;
174529 u32 *aSzIns = 0;
174530 u32 *aSzDel = 0;
174531 sqlite3_stmt *pStmt = 0;
174532 int nEntry = 0;
174533
174534 /* Compose and prepare an SQL statement to loop through the content table */
174535 char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
174536 if( !zSql ){
174537 rc = SQLITE_NOMEM7;
174538 }else{
174539 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
174540 sqlite3_free(zSql);
174541 }
174542
174543 if( rc==SQLITE_OK0 ){
174544 sqlite3_int64 nByte = sizeof(u32) * ((sqlite3_int64)p->nColumn+1)*3;
174545 aSz = (u32 *)sqlite3_malloc64(nByte);
174546 if( aSz==0 ){
174547 rc = SQLITE_NOMEM7;
174548 }else{
174549 memset(aSz, 0, nByte);
174550 aSzIns = &aSz[p->nColumn+1];
174551 aSzDel = &aSzIns[p->nColumn+1];
174552 }
174553 }
174554
174555 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
174556 int iCol;
174557 int iLangid = langidFromSelect(p, pStmt);
174558 rc = fts3PendingTermsDocid(p, 0, iLangid, sqlite3_column_int64(pStmt, 0));
174559 memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
174560 for(iCol=0; rc==SQLITE_OK0 && iCol<p->nColumn; iCol++){
174561 if( p->abNotindexed[iCol]==0 ){
174562 const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
174563 rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
174564 aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
174565 }
174566 }
174567 if( p->bHasDocsize ){
174568 fts3InsertDocsize(&rc, p, aSz);
174569 }
174570 if( rc!=SQLITE_OK0 ){
174571 sqlite3_finalize(pStmt);
174572 pStmt = 0;
174573 }else{
174574 nEntry++;
174575 for(iCol=0; iCol<=p->nColumn; iCol++){
174576 aSzIns[iCol] += aSz[iCol];
174577 }
174578 }
174579 }
174580 if( p->bFts4 ){
174581 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
174582 }
174583 sqlite3_free(aSz);
174584
174585 if( pStmt ){
174586 int rc2 = sqlite3_finalize(pStmt);
174587 if( rc==SQLITE_OK0 ){
174588 rc = rc2;
174589 }
174590 }
174591 }
174592
174593 return rc;
174594}
174595
174596
174597/*
174598** This function opens a cursor used to read the input data for an
174599** incremental merge operation. Specifically, it opens a cursor to scan
174600** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
174601** level iAbsLevel.
174602*/
174603static int fts3IncrmergeCsr(
174604 Fts3Table *p, /* FTS3 table handle */
174605 sqlite3_int64 iAbsLevel, /* Absolute level to open */
174606 int nSeg, /* Number of segments to merge */
174607 Fts3MultiSegReader *pCsr /* Cursor object to populate */
174608){
174609 int rc; /* Return Code */
174610 sqlite3_stmt *pStmt = 0; /* Statement used to read %_segdir entry */
174611 sqlite3_int64 nByte; /* Bytes allocated at pCsr->apSegment[] */
174612
174613 /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
174614 memset(pCsr, 0, sizeof(*pCsr));
174615 nByte = sizeof(Fts3SegReader *) * nSeg;
174616 pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc64(nByte);
174617
174618 if( pCsr->apSegment==0 ){
174619 rc = SQLITE_NOMEM7;
174620 }else{
174621 memset(pCsr->apSegment, 0, nByte);
174622 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
174623 }
174624 if( rc==SQLITE_OK0 ){
174625 int i;
174626 int rc2;
174627 sqlite3_bind_int64(pStmt, 1, iAbsLevel);
174628 assert( pCsr->nSegment==0 )((void) (0));
174629 for(i=0; rc==SQLITE_OK0 && sqlite3_step(pStmt)==SQLITE_ROW100 && i<nSeg; i++){
174630 rc = sqlite3Fts3SegReaderNew(i, 0,
174631 sqlite3_column_int64(pStmt, 1), /* segdir.start_block */
174632 sqlite3_column_int64(pStmt, 2), /* segdir.leaves_end_block */
174633 sqlite3_column_int64(pStmt, 3), /* segdir.end_block */
174634 sqlite3_column_blob(pStmt, 4), /* segdir.root */
174635 sqlite3_column_bytes(pStmt, 4), /* segdir.root */
174636 &pCsr->apSegment[i]
174637 );
174638 pCsr->nSegment++;
174639 }
174640 rc2 = sqlite3_reset(pStmt);
174641 if( rc==SQLITE_OK0 ) rc = rc2;
174642 }
174643
174644 return rc;
174645}
174646
174647typedef struct IncrmergeWriter IncrmergeWriter;
174648typedef struct NodeWriter NodeWriter;
174649typedef struct Blob Blob;
174650typedef struct NodeReader NodeReader;
174651
174652/*
174653** An instance of the following structure is used as a dynamic buffer
174654** to build up nodes or other blobs of data in.
174655**
174656** The function blobGrowBuffer() is used to extend the allocation.
174657*/
174658struct Blob {
174659 char *a; /* Pointer to allocation */
174660 int n; /* Number of valid bytes of data in a[] */
174661 int nAlloc; /* Allocated size of a[] (nAlloc>=n) */
174662};
174663
174664/*
174665** This structure is used to build up buffers containing segment b-tree
174666** nodes (blocks).
174667*/
174668struct NodeWriter {
174669 sqlite3_int64 iBlock; /* Current block id */
174670 Blob key; /* Last key written to the current block */
174671 Blob block; /* Current block image */
174672};
174673
174674/*
174675** An object of this type contains the state required to create or append
174676** to an appendable b-tree segment.
174677*/
174678struct IncrmergeWriter {
174679 int nLeafEst; /* Space allocated for leaf blocks */
174680 int nWork; /* Number of leaf pages flushed */
174681 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
174682 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
174683 sqlite3_int64 iStart; /* Block number of first allocated block */
174684 sqlite3_int64 iEnd; /* Block number of last allocated block */
174685 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
174686 u8 bNoLeafData; /* If true, store 0 for segment size */
174687 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
174688};
174689
174690/*
174691** An object of the following type is used to read data from a single
174692** FTS segment node. See the following functions:
174693**
174694** nodeReaderInit()
174695** nodeReaderNext()
174696** nodeReaderRelease()
174697*/
174698struct NodeReader {
174699 const char *aNode;
174700 int nNode;
174701 int iOff; /* Current offset within aNode[] */
174702
174703 /* Output variables. Containing the current node entry. */
174704 sqlite3_int64 iChild; /* Pointer to child node */
174705 Blob term; /* Current term */
174706 const char *aDoclist; /* Pointer to doclist */
174707 int nDoclist; /* Size of doclist in bytes */
174708};
174709
174710/*
174711** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
174712** Otherwise, if the allocation at pBlob->a is not already at least nMin
174713** bytes in size, extend (realloc) it to be so.
174714**
174715** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
174716** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
174717** to reflect the new size of the pBlob->a[] buffer.
174718*/
174719static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
174720 if( *pRc==SQLITE_OK0 && nMin>pBlob->nAlloc ){
174721 int nAlloc = nMin;
174722 char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
174723 if( a ){
174724 pBlob->nAlloc = nAlloc;
174725 pBlob->a = a;
174726 }else{
174727 *pRc = SQLITE_NOMEM7;
174728 }
174729 }
174730}
174731
174732/*
174733** Attempt to advance the node-reader object passed as the first argument to
174734** the next entry on the node.
174735**
174736** Return an error code if an error occurs (SQLITE_NOMEM is possible).
174737** Otherwise return SQLITE_OK. If there is no next entry on the node
174738** (e.g. because the current entry is the last) set NodeReader->aNode to
174739** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
174740** variables for the new entry.
174741*/
174742static int nodeReaderNext(NodeReader *p){
174743 int bFirst = (p->term.n==0); /* True for first term on the node */
174744 int nPrefix = 0; /* Bytes to copy from previous term */
174745 int nSuffix = 0; /* Bytes to append to the prefix */
174746 int rc = SQLITE_OK0; /* Return code */
174747
174748 assert( p->aNode )((void) (0));
174749 if( p->iChild && bFirst==0 ) p->iChild++;
174750 if( p->iOff>=p->nNode ){
174751 /* EOF */
174752 p->aNode = 0;
174753 }else{
174754 if( bFirst==0 ){
174755 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
174756 }
174757 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
174758
174759 if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){
174760 return FTS_CORRUPT_VTAB;
174761 }
174762 blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
174763 if( rc==SQLITE_OK0 ){
174764 memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
174765 p->term.n = nPrefix+nSuffix;
174766 p->iOff += nSuffix;
174767 if( p->iChild==0 ){
174768 p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
174769 if( (p->nNode-p->iOff)<p->nDoclist ){
174770 return FTS_CORRUPT_VTAB;
174771 }
174772 p->aDoclist = &p->aNode[p->iOff];
174773 p->iOff += p->nDoclist;
174774 }
174775 }
174776 }
174777
174778 assert_fts3_nc( p->iOff<=p->nNode );
174779 return rc;
174780}
174781
174782/*
174783** Release all dynamic resources held by node-reader object *p.
174784*/
174785static void nodeReaderRelease(NodeReader *p){
174786 sqlite3_free(p->term.a);
174787}
174788
174789/*
174790** Initialize a node-reader object to read the node in buffer aNode/nNode.
174791**
174792** If successful, SQLITE_OK is returned and the NodeReader object set to
174793** point to the first entry on the node (if any). Otherwise, an SQLite
174794** error code is returned.
174795*/
174796static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
174797 memset(p, 0, sizeof(NodeReader));
174798 p->aNode = aNode;
174799 p->nNode = nNode;
174800
174801 /* Figure out if this is a leaf or an internal node. */
174802 if( p->aNode[0] ){
174803 /* An internal node. */
174804 p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
174805 }else{
174806 p->iOff = 1;
174807 }
174808
174809 return nodeReaderNext(p);
174810}
174811
174812/*
174813** This function is called while writing an FTS segment each time a leaf o
174814** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
174815** to be greater than the largest key on the node just written, but smaller
174816** than or equal to the first key that will be written to the next leaf
174817** node.
174818**
174819** The block id of the leaf node just written to disk may be found in
174820** (pWriter->aNodeWriter[0].iBlock) when this function is called.
174821*/
174822static int fts3IncrmergePush(
174823 Fts3Table *p, /* Fts3 table handle */
174824 IncrmergeWriter *pWriter, /* Writer object */
174825 const char *zTerm, /* Term to write to internal node */
174826 int nTerm /* Bytes at zTerm */
174827){
174828 sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
174829 int iLayer;
174830
174831 assert( nTerm>0 )((void) (0));
174832 for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT)(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
174833 sqlite3_int64 iNextPtr = 0;
174834 NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
174835 int rc = SQLITE_OK0;
174836 int nPrefix;
174837 int nSuffix;
174838 int nSpace;
174839
174840 /* Figure out how much space the key will consume if it is written to
174841 ** the current node of layer iLayer. Due to the prefix compression,
174842 ** the space required changes depending on which node the key is to
174843 ** be added to. */
174844 nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
174845 nSuffix = nTerm - nPrefix;
174846 nSpace = sqlite3Fts3VarintLen(nPrefix);
174847 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
174848
174849 if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
174850 /* If the current node of layer iLayer contains zero keys, or if adding
174851 ** the key to it will not cause it to grow to larger than nNodeSize
174852 ** bytes in size, write the key here. */
174853
174854 Blob *pBlk = &pNode->block;
174855 if( pBlk->n==0 ){
174856 blobGrowBuffer(pBlk, p->nNodeSize, &rc);
174857 if( rc==SQLITE_OK0 ){
174858 pBlk->a[0] = (char)iLayer;
174859 pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
174860 }
174861 }
174862 blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
174863 blobGrowBuffer(&pNode->key, nTerm, &rc);
174864
174865 if( rc==SQLITE_OK0 ){
174866 if( pNode->key.n ){
174867 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
174868 }
174869 pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
174870 memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
174871 pBlk->n += nSuffix;
174872
174873 memcpy(pNode->key.a, zTerm, nTerm);
174874 pNode->key.n = nTerm;
174875 }
174876 }else{
174877 /* Otherwise, flush the current node of layer iLayer to disk.
174878 ** Then allocate a new, empty sibling node. The key will be written
174879 ** into the parent of this node. */
174880 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
174881
174882 assert( pNode->block.nAlloc>=p->nNodeSize )((void) (0));
174883 pNode->block.a[0] = (char)iLayer;
174884 pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
174885
174886 iNextPtr = pNode->iBlock;
174887 pNode->iBlock++;
174888 pNode->key.n = 0;
174889 }
174890
174891 if( rc!=SQLITE_OK0 || iNextPtr==0 ) return rc;
174892 iPtr = iNextPtr;
174893 }
174894
174895 assert( 0 )((void) (0));
174896 return 0;
174897}
174898
174899/*
174900** Append a term and (optionally) doclist to the FTS segment node currently
174901** stored in blob *pNode. The node need not contain any terms, but the
174902** header must be written before this function is called.
174903**
174904** A node header is a single 0x00 byte for a leaf node, or a height varint
174905** followed by the left-hand-child varint for an internal node.
174906**
174907** The term to be appended is passed via arguments zTerm/nTerm. For a
174908** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
174909** node, both aDoclist and nDoclist must be passed 0.
174910**
174911** If the size of the value in blob pPrev is zero, then this is the first
174912** term written to the node. Otherwise, pPrev contains a copy of the
174913** previous term. Before this function returns, it is updated to contain a
174914** copy of zTerm/nTerm.
174915**
174916** It is assumed that the buffer associated with pNode is already large
174917** enough to accommodate the new entry. The buffer associated with pPrev
174918** is extended by this function if requrired.
174919**
174920** If an error (i.e. OOM condition) occurs, an SQLite error code is
174921** returned. Otherwise, SQLITE_OK.
174922*/
174923static int fts3AppendToNode(
174924 Blob *pNode, /* Current node image to append to */
174925 Blob *pPrev, /* Buffer containing previous term written */
174926 const char *zTerm, /* New term to write */
174927 int nTerm, /* Size of zTerm in bytes */
174928 const char *aDoclist, /* Doclist (or NULL) to write */
174929 int nDoclist /* Size of aDoclist in bytes */
174930){
174931 int rc = SQLITE_OK0; /* Return code */
174932 int bFirst = (pPrev->n==0); /* True if this is the first term written */
174933 int nPrefix; /* Size of term prefix in bytes */
174934 int nSuffix; /* Size of term suffix in bytes */
174935
174936 /* Node must have already been started. There must be a doclist for a
174937 ** leaf node, and there must not be a doclist for an internal node. */
174938 assert( pNode->n>0 )((void) (0));
174939 assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) );
174940
174941 blobGrowBuffer(pPrev, nTerm, &rc);
174942 if( rc!=SQLITE_OK0 ) return rc;
174943
174944 nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
174945 nSuffix = nTerm - nPrefix;
174946 memcpy(pPrev->a, zTerm, nTerm);
174947 pPrev->n = nTerm;
174948
174949 if( bFirst==0 ){
174950 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
174951 }
174952 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
174953 memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
174954 pNode->n += nSuffix;
174955
174956 if( aDoclist ){
174957 pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
174958 memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
174959 pNode->n += nDoclist;
174960 }
174961
174962 assert( pNode->n<=pNode->nAlloc )((void) (0));
174963
174964 return SQLITE_OK0;
174965}
174966
174967/*
174968** Append the current term and doclist pointed to by cursor pCsr to the
174969** appendable b-tree segment opened for writing by pWriter.
174970**
174971** Return SQLITE_OK if successful, or an SQLite error code otherwise.
174972*/
174973static int fts3IncrmergeAppend(
174974 Fts3Table *p, /* Fts3 table handle */
174975 IncrmergeWriter *pWriter, /* Writer object */
174976 Fts3MultiSegReader *pCsr /* Cursor containing term and doclist */
174977){
174978 const char *zTerm = pCsr->zTerm;
174979 int nTerm = pCsr->nTerm;
174980 const char *aDoclist = pCsr->aDoclist;
174981 int nDoclist = pCsr->nDoclist;
174982 int rc = SQLITE_OK0; /* Return code */
174983 int nSpace; /* Total space in bytes required on leaf */
174984 int nPrefix; /* Size of prefix shared with previous term */
174985 int nSuffix; /* Size of suffix (nTerm - nPrefix) */
174986 NodeWriter *pLeaf; /* Object used to write leaf nodes */
174987
174988 pLeaf = &pWriter->aNodeWriter[0];
174989 nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
174990 nSuffix = nTerm - nPrefix;
174991
174992 nSpace = sqlite3Fts3VarintLen(nPrefix);
174993 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
174994 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
174995
174996 /* If the current block is not empty, and if adding this term/doclist
174997 ** to the current block would make it larger than Fts3Table.nNodeSize
174998 ** bytes, write this block out to the database. */
174999 if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
175000 rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
175001 pWriter->nWork++;
175002
175003 /* Add the current term to the parent node. The term added to the
175004 ** parent must:
175005 **
175006 ** a) be greater than the largest term on the leaf node just written
175007 ** to the database (still available in pLeaf->key), and
175008 **
175009 ** b) be less than or equal to the term about to be added to the new
175010 ** leaf node (zTerm/nTerm).
175011 **
175012 ** In other words, it must be the prefix of zTerm 1 byte longer than
175013 ** the common prefix (if any) of zTerm and pWriter->zTerm.
175014 */
175015 if( rc==SQLITE_OK0 ){
175016 rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
175017 }
175018
175019 /* Advance to the next output block */
175020 pLeaf->iBlock++;
175021 pLeaf->key.n = 0;
175022 pLeaf->block.n = 0;
175023
175024 nSuffix = nTerm;
175025 nSpace = 1;
175026 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
175027 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
175028 }
175029
175030 pWriter->nLeafData += nSpace;
175031 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
175032 if( rc==SQLITE_OK0 ){
175033 if( pLeaf->block.n==0 ){
175034 pLeaf->block.n = 1;
175035 pLeaf->block.a[0] = '\0';
175036 }
175037 rc = fts3AppendToNode(
175038 &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
175039 );
175040 }
175041
175042 return rc;
175043}
175044
175045/*
175046** This function is called to release all dynamic resources held by the
175047** merge-writer object pWriter, and if no error has occurred, to flush
175048** all outstanding node buffers held by pWriter to disk.
175049**
175050** If *pRc is not SQLITE_OK when this function is called, then no attempt
175051** is made to write any data to disk. Instead, this function serves only
175052** to release outstanding resources.
175053**
175054** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
175055** flushing buffers to disk, *pRc is set to an SQLite error code before
175056** returning.
175057*/
175058static void fts3IncrmergeRelease(
175059 Fts3Table *p, /* FTS3 table handle */
175060 IncrmergeWriter *pWriter, /* Merge-writer object */
175061 int *pRc /* IN/OUT: Error code */
175062){
175063 int i; /* Used to iterate through non-root layers */
175064 int iRoot; /* Index of root in pWriter->aNodeWriter */
175065 NodeWriter *pRoot; /* NodeWriter for root node */
175066 int rc = *pRc; /* Error code */
175067
175068 /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
175069 ** root node. If the segment fits entirely on a single leaf node, iRoot
175070 ** will be set to 0. If the root node is the parent of the leaves, iRoot
175071 ** will be 1. And so on. */
175072 for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
175073 NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
175074 if( pNode->block.n>0 ) break;
175075 assert( *pRc || pNode->block.nAlloc==0 )((void) (0));
175076 assert( *pRc || pNode->key.nAlloc==0 )((void) (0));
175077 sqlite3_free(pNode->block.a);
175078 sqlite3_free(pNode->key.a);
175079 }
175080
175081 /* Empty output segment. This is a no-op. */
175082 if( iRoot<0 ) return;
175083
175084 /* The entire output segment fits on a single node. Normally, this means
175085 ** the node would be stored as a blob in the "root" column of the %_segdir
175086 ** table. However, this is not permitted in this case. The problem is that
175087 ** space has already been reserved in the %_segments table, and so the
175088 ** start_block and end_block fields of the %_segdir table must be populated.
175089 ** And, by design or by accident, released versions of FTS cannot handle
175090 ** segments that fit entirely on the root node with start_block!=0.
175091 **
175092 ** Instead, create a synthetic root node that contains nothing but a
175093 ** pointer to the single content node. So that the segment consists of a
175094 ** single leaf and a single interior (root) node.
175095 **
175096 ** Todo: Better might be to defer allocating space in the %_segments
175097 ** table until we are sure it is needed.
175098 */
175099 if( iRoot==0 ){
175100 Blob *pBlock = &pWriter->aNodeWriter[1].block;
175101 blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
175102 if( rc==SQLITE_OK0 ){
175103 pBlock->a[0] = 0x01;
175104 pBlock->n = 1 + sqlite3Fts3PutVarint(
175105 &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
175106 );
175107 }
175108 iRoot = 1;
175109 }
175110 pRoot = &pWriter->aNodeWriter[iRoot];
175111
175112 /* Flush all currently outstanding nodes to disk. */
175113 for(i=0; i<iRoot; i++){
175114 NodeWriter *pNode = &pWriter->aNodeWriter[i];
175115 if( pNode->block.n>0 && rc==SQLITE_OK0 ){
175116 rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
175117 }
175118 sqlite3_free(pNode->block.a);
175119 sqlite3_free(pNode->key.a);
175120 }
175121
175122 /* Write the %_segdir record. */
175123 if( rc==SQLITE_OK0 ){
175124 rc = fts3WriteSegdir(p,
175125 pWriter->iAbsLevel+1, /* level */
175126 pWriter->iIdx, /* idx */
175127 pWriter->iStart, /* start_block */
175128 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
175129 pWriter->iEnd, /* end_block */
175130 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
175131 pRoot->block.a, pRoot->block.n /* root */
175132 );
175133 }
175134 sqlite3_free(pRoot->block.a);
175135 sqlite3_free(pRoot->key.a);
175136
175137 *pRc = rc;
175138}
175139
175140/*
175141** Compare the term in buffer zLhs (size in bytes nLhs) with that in
175142** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
175143** the other, it is considered to be smaller than the other.
175144**
175145** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
175146** if it is greater.
175147*/
175148static int fts3TermCmp(
175149 const char *zLhs, int nLhs, /* LHS of comparison */
175150 const char *zRhs, int nRhs /* RHS of comparison */
175151){
175152 int nCmp = MIN(nLhs, nRhs)((nLhs)<(nRhs)?(nLhs):(nRhs));
175153 int res;
175154
175155 res = (nCmp ? memcmp(zLhs, zRhs, nCmp) : 0);
175156 if( res==0 ) res = nLhs - nRhs;
175157
175158 return res;
175159}
175160
175161
175162/*
175163** Query to see if the entry in the %_segments table with blockid iEnd is
175164** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
175165** returning. Otherwise, set *pbRes to 0.
175166**
175167** Or, if an error occurs while querying the database, return an SQLite
175168** error code. The final value of *pbRes is undefined in this case.
175169**
175170** This is used to test if a segment is an "appendable" segment. If it
175171** is, then a NULL entry has been inserted into the %_segments table
175172** with blockid %_segdir.end_block.
175173*/
175174static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
175175 int bRes = 0; /* Result to set *pbRes to */
175176 sqlite3_stmt *pCheck = 0; /* Statement to query database with */
175177 int rc; /* Return code */
175178
175179 rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
175180 if( rc==SQLITE_OK0 ){
175181 sqlite3_bind_int64(pCheck, 1, iEnd);
175182 if( SQLITE_ROW100==sqlite3_step(pCheck) ) bRes = 1;
175183 rc = sqlite3_reset(pCheck);
175184 }
175185
175186 *pbRes = bRes;
175187 return rc;
175188}
175189
175190/*
175191** This function is called when initializing an incremental-merge operation.
175192** It checks if the existing segment with index value iIdx at absolute level
175193** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
175194** merge-writer object *pWriter is initialized to write to it.
175195**
175196** An existing segment can be appended to by an incremental merge if:
175197**
175198** * It was initially created as an appendable segment (with all required
175199** space pre-allocated), and
175200**
175201** * The first key read from the input (arguments zKey and nKey) is
175202** greater than the largest key currently stored in the potential
175203** output segment.
175204*/
175205static int fts3IncrmergeLoad(
175206 Fts3Table *p, /* Fts3 table handle */
175207 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
175208 int iIdx, /* Index of candidate output segment */
175209 const char *zKey, /* First key to write */
175210 int nKey, /* Number of bytes in nKey */
175211 IncrmergeWriter *pWriter /* Populate this object */
175212){
175213 int rc; /* Return code */
175214 sqlite3_stmt *pSelect = 0; /* SELECT to read %_segdir entry */
175215
175216 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
175217 if( rc==SQLITE_OK0 ){
175218 sqlite3_int64 iStart = 0; /* Value of %_segdir.start_block */
175219 sqlite3_int64 iLeafEnd = 0; /* Value of %_segdir.leaves_end_block */
175220 sqlite3_int64 iEnd = 0; /* Value of %_segdir.end_block */
175221 const char *aRoot = 0; /* Pointer to %_segdir.root buffer */
175222 int nRoot = 0; /* Size of aRoot[] in bytes */
175223 int rc2; /* Return code from sqlite3_reset() */
175224 int bAppendable = 0; /* Set to true if segment is appendable */
175225
175226 /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
175227 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
175228 sqlite3_bind_int(pSelect, 2, iIdx);
175229 if( sqlite3_step(pSelect)==SQLITE_ROW100 ){
175230 iStart = sqlite3_column_int64(pSelect, 1);
175231 iLeafEnd = sqlite3_column_int64(pSelect, 2);
175232 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
175233 if( pWriter->nLeafData<0 ){
175234 pWriter->nLeafData = pWriter->nLeafData * -1;
175235 }
175236 pWriter->bNoLeafData = (pWriter->nLeafData==0);
175237 nRoot = sqlite3_column_bytes(pSelect, 4);
175238 aRoot = sqlite3_column_blob(pSelect, 4);
175239 }else{
175240 return sqlite3_reset(pSelect);
175241 }
175242
175243 /* Check for the zero-length marker in the %_segments table */
175244 rc = fts3IsAppendable(p, iEnd, &bAppendable);
175245
175246 /* Check that zKey/nKey is larger than the largest key the candidate */
175247 if( rc==SQLITE_OK0 && bAppendable ){
175248 char *aLeaf = 0;
175249 int nLeaf = 0;
175250
175251 rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
175252 if( rc==SQLITE_OK0 ){
175253 NodeReader reader;
175254 for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
175255 rc==SQLITE_OK0 && reader.aNode;
175256 rc = nodeReaderNext(&reader)
175257 ){
175258 assert( reader.aNode )((void) (0));
175259 }
175260 if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
175261 bAppendable = 0;
175262 }
175263 nodeReaderRelease(&reader);
175264 }
175265 sqlite3_free(aLeaf);
175266 }
175267
175268 if( rc==SQLITE_OK0 && bAppendable ){
175269 /* It is possible to append to this segment. Set up the IncrmergeWriter
175270 ** object to do so. */
175271 int i;
175272 int nHeight = (int)aRoot[0];
175273 NodeWriter *pNode;
175274
175275 pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
175276 pWriter->iStart = iStart;
175277 pWriter->iEnd = iEnd;
175278 pWriter->iAbsLevel = iAbsLevel;
175279 pWriter->iIdx = iIdx;
175280
175281 for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
175282 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
175283 }
175284
175285 pNode = &pWriter->aNodeWriter[nHeight];
175286 pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
175287 blobGrowBuffer(&pNode->block,
175288 MAX(nRoot, p->nNodeSize)((nRoot)>(p->nNodeSize)?(nRoot):(p->nNodeSize))+FTS3_NODE_PADDING, &rc
175289 );
175290 if( rc==SQLITE_OK0 ){
175291 memcpy(pNode->block.a, aRoot, nRoot);
175292 pNode->block.n = nRoot;
175293 memset(&pNode->block.a[nRoot], 0, FTS3_NODE_PADDING);
175294 }
175295
175296 for(i=nHeight; i>=0 && rc==SQLITE_OK0; i--){
175297 NodeReader reader;
175298 pNode = &pWriter->aNodeWriter[i];
175299
175300 rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
175301 if( reader.aNode ){
175302 while( reader.aNode && rc==SQLITE_OK0 ) rc = nodeReaderNext(&reader);
175303 blobGrowBuffer(&pNode->key, reader.term.n, &rc);
175304 if( rc==SQLITE_OK0 ){
175305 memcpy(pNode->key.a, reader.term.a, reader.term.n);
175306 pNode->key.n = reader.term.n;
175307 if( i>0 ){
175308 char *aBlock = 0;
175309 int nBlock = 0;
175310 pNode = &pWriter->aNodeWriter[i-1];
175311 pNode->iBlock = reader.iChild;
175312 rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
175313 blobGrowBuffer(&pNode->block,
175314 MAX(nBlock, p->nNodeSize)((nBlock)>(p->nNodeSize)?(nBlock):(p->nNodeSize))+FTS3_NODE_PADDING, &rc
175315 );
175316 if( rc==SQLITE_OK0 ){
175317 memcpy(pNode->block.a, aBlock, nBlock);
175318 pNode->block.n = nBlock;
175319 memset(&pNode->block.a[nBlock], 0, FTS3_NODE_PADDING);
175320 }
175321 sqlite3_free(aBlock);
175322 }
175323 }
175324 }
175325 nodeReaderRelease(&reader);
175326 }
175327 }
175328
175329 rc2 = sqlite3_reset(pSelect);
175330 if( rc==SQLITE_OK0 ) rc = rc2;
175331 }
175332
175333 return rc;
175334}
175335
175336/*
175337** Determine the largest segment index value that exists within absolute
175338** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
175339** one before returning SQLITE_OK. Or, if there are no segments at all
175340** within level iAbsLevel, set *piIdx to zero.
175341**
175342** If an error occurs, return an SQLite error code. The final value of
175343** *piIdx is undefined in this case.
175344*/
175345static int fts3IncrmergeOutputIdx(
175346 Fts3Table *p, /* FTS Table handle */
175347 sqlite3_int64 iAbsLevel, /* Absolute index of input segments */
175348 int *piIdx /* OUT: Next free index at iAbsLevel+1 */
175349){
175350 int rc;
175351 sqlite3_stmt *pOutputIdx = 0; /* SQL used to find output index */
175352
175353 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
175354 if( rc==SQLITE_OK0 ){
175355 sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
175356 sqlite3_step(pOutputIdx);
175357 *piIdx = sqlite3_column_int(pOutputIdx, 0);
175358 rc = sqlite3_reset(pOutputIdx);
175359 }
175360
175361 return rc;
175362}
175363
175364/*
175365** Allocate an appendable output segment on absolute level iAbsLevel+1
175366** with idx value iIdx.
175367**
175368** In the %_segdir table, a segment is defined by the values in three
175369** columns:
175370**
175371** start_block
175372** leaves_end_block
175373** end_block
175374**
175375** When an appendable segment is allocated, it is estimated that the
175376** maximum number of leaf blocks that may be required is the sum of the
175377** number of leaf blocks consumed by the input segments, plus the number
175378** of input segments, multiplied by two. This value is stored in stack
175379** variable nLeafEst.
175380**
175381** A total of 16*nLeafEst blocks are allocated when an appendable segment
175382** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
175383** array of leaf nodes starts at the first block allocated. The array
175384** of interior nodes that are parents of the leaf nodes start at block
175385** (start_block + (1 + end_block - start_block) / 16). And so on.
175386**
175387** In the actual code below, the value "16" is replaced with the
175388** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
175389*/
175390static int fts3IncrmergeWriter(
175391 Fts3Table *p, /* Fts3 table handle */
175392 sqlite3_int64 iAbsLevel, /* Absolute level of input segments */
175393 int iIdx, /* Index of new output segment */
175394 Fts3MultiSegReader *pCsr, /* Cursor that data will be read from */
175395 IncrmergeWriter *pWriter /* Populate this object */
175396){
175397 int rc; /* Return Code */
175398 int i; /* Iterator variable */
175399 int nLeafEst = 0; /* Blocks allocated for leaf nodes */
175400 sqlite3_stmt *pLeafEst = 0; /* SQL used to determine nLeafEst */
175401 sqlite3_stmt *pFirstBlock = 0; /* SQL used to determine first block */
175402
175403 /* Calculate nLeafEst. */
175404 rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
175405 if( rc==SQLITE_OK0 ){
175406 sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
175407 sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
175408 if( SQLITE_ROW100==sqlite3_step(pLeafEst) ){
175409 nLeafEst = sqlite3_column_int(pLeafEst, 0);
175410 }
175411 rc = sqlite3_reset(pLeafEst);
175412 }
175413 if( rc!=SQLITE_OK0 ) return rc;
175414
175415 /* Calculate the first block to use in the output segment */
175416 rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
175417 if( rc==SQLITE_OK0 ){
175418 if( SQLITE_ROW100==sqlite3_step(pFirstBlock) ){
175419 pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
175420 pWriter->iEnd = pWriter->iStart - 1;
175421 pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
175422 }
175423 rc = sqlite3_reset(pFirstBlock);
175424 }
175425 if( rc!=SQLITE_OK0 ) return rc;
175426
175427 /* Insert the marker in the %_segments table to make sure nobody tries
175428 ** to steal the space just allocated. This is also used to identify
175429 ** appendable segments. */
175430 rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
175431 if( rc!=SQLITE_OK0 ) return rc;
175432
175433 pWriter->iAbsLevel = iAbsLevel;
175434 pWriter->nLeafEst = nLeafEst;
175435 pWriter->iIdx = iIdx;
175436
175437 /* Set up the array of NodeWriter objects */
175438 for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
175439 pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
175440 }
175441 return SQLITE_OK0;
175442}
175443
175444/*
175445** Remove an entry from the %_segdir table. This involves running the
175446** following two statements:
175447**
175448** DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
175449** UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
175450**
175451** The DELETE statement removes the specific %_segdir level. The UPDATE
175452** statement ensures that the remaining segments have contiguously allocated
175453** idx values.
175454*/
175455static int fts3RemoveSegdirEntry(
175456 Fts3Table *p, /* FTS3 table handle */
175457 sqlite3_int64 iAbsLevel, /* Absolute level to delete from */
175458 int iIdx /* Index of %_segdir entry to delete */
175459){
175460 int rc; /* Return code */
175461 sqlite3_stmt *pDelete = 0; /* DELETE statement */
175462
175463 rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
175464 if( rc==SQLITE_OK0 ){
175465 sqlite3_bind_int64(pDelete, 1, iAbsLevel);
175466 sqlite3_bind_int(pDelete, 2, iIdx);
175467 sqlite3_step(pDelete);
175468 rc = sqlite3_reset(pDelete);
175469 }
175470
175471 return rc;
175472}
175473
175474/*
175475** One or more segments have just been removed from absolute level iAbsLevel.
175476** Update the 'idx' values of the remaining segments in the level so that
175477** the idx values are a contiguous sequence starting from 0.
175478*/
175479static int fts3RepackSegdirLevel(
175480 Fts3Table *p, /* FTS3 table handle */
175481 sqlite3_int64 iAbsLevel /* Absolute level to repack */
175482){
175483 int rc; /* Return code */
175484 int *aIdx = 0; /* Array of remaining idx values */
175485 int nIdx = 0; /* Valid entries in aIdx[] */
175486 int nAlloc = 0; /* Allocated size of aIdx[] */
175487 int i; /* Iterator variable */
175488 sqlite3_stmt *pSelect = 0; /* Select statement to read idx values */
175489 sqlite3_stmt *pUpdate = 0; /* Update statement to modify idx values */
175490
175491 rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
175492 if( rc==SQLITE_OK0 ){
175493 int rc2;
175494 sqlite3_bind_int64(pSelect, 1, iAbsLevel);
175495 while( SQLITE_ROW100==sqlite3_step(pSelect) ){
175496 if( nIdx>=nAlloc ){
175497 int *aNew;
175498 nAlloc += 16;
175499 aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
175500 if( !aNew ){
175501 rc = SQLITE_NOMEM7;
175502 break;
175503 }
175504 aIdx = aNew;
175505 }
175506 aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
175507 }
175508 rc2 = sqlite3_reset(pSelect);
175509 if( rc==SQLITE_OK0 ) rc = rc2;
175510 }
175511
175512 if( rc==SQLITE_OK0 ){
175513 rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
175514 }
175515 if( rc==SQLITE_OK0 ){
175516 sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
175517 }
175518
175519 assert( p->bIgnoreSavepoint==0 )((void) (0));
175520 p->bIgnoreSavepoint = 1;
175521 for(i=0; rc==SQLITE_OK0 && i<nIdx; i++){
175522 if( aIdx[i]!=i ){
175523 sqlite3_bind_int(pUpdate, 3, aIdx[i]);
175524 sqlite3_bind_int(pUpdate, 1, i);
175525 sqlite3_step(pUpdate);
175526 rc = sqlite3_reset(pUpdate);
175527 }
175528 }
175529 p->bIgnoreSavepoint = 0;
175530
175531 sqlite3_free(aIdx);
175532 return rc;
175533}
175534
175535static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
175536 pNode->a[0] = (char)iHeight;
175537 if( iChild ){
175538 assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) )((void) (0));
175539 pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
175540 }else{
175541 assert( pNode->nAlloc>=1 )((void) (0));
175542 pNode->n = 1;
175543 }
175544}
175545
175546/*
175547** The first two arguments are a pointer to and the size of a segment b-tree
175548** node. The node may be a leaf or an internal node.
175549**
175550** This function creates a new node image in blob object *pNew by copying
175551** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
175552** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
175553*/
175554static int fts3TruncateNode(
175555 const char *aNode, /* Current node image */
175556 int nNode, /* Size of aNode in bytes */
175557 Blob *pNew, /* OUT: Write new node image here */
175558 const char *zTerm, /* Omit all terms smaller than this */
175559 int nTerm, /* Size of zTerm in bytes */
175560 sqlite3_int64 *piBlock /* OUT: Block number in next layer down */
175561){
175562 NodeReader reader; /* Reader object */
175563 Blob prev = {0, 0, 0}; /* Previous term written to new node */
175564 int rc = SQLITE_OK0; /* Return code */
175565 int bLeaf; /* True for a leaf node */
175566
175567 if( nNode<1 ) return FTS_CORRUPT_VTAB;
175568 bLeaf = aNode[0]=='\0';
175569
175570 /* Allocate required output space */
175571 blobGrowBuffer(pNew, nNode, &rc);
175572 if( rc!=SQLITE_OK0 ) return rc;
175573 pNew->n = 0;
175574
175575 /* Populate new node buffer */
175576 for(rc = nodeReaderInit(&reader, aNode, nNode);
175577 rc==SQLITE_OK0 && reader.aNode;
175578 rc = nodeReaderNext(&reader)
175579 ){
175580 if( pNew->n==0 ){
175581 int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
175582 if( res<0 || (bLeaf==0 && res==0) ) continue;
175583 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
175584 *piBlock = reader.iChild;
175585 }
175586 rc = fts3AppendToNode(
175587 pNew, &prev, reader.term.a, reader.term.n,
175588 reader.aDoclist, reader.nDoclist
175589 );
175590 if( rc!=SQLITE_OK0 ) break;
175591 }
175592 if( pNew->n==0 ){
175593 fts3StartNode(pNew, (int)aNode[0], reader.iChild);
175594 *piBlock = reader.iChild;
175595 }
175596 assert( pNew->n<=pNew->nAlloc )((void) (0));
175597
175598 nodeReaderRelease(&reader);
175599 sqlite3_free(prev.a);
175600 return rc;
175601}
175602
175603/*
175604** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
175605** level iAbsLevel. This may involve deleting entries from the %_segments
175606** table, and modifying existing entries in both the %_segments and %_segdir
175607** tables.
175608**
175609** SQLITE_OK is returned if the segment is updated successfully. Or an
175610** SQLite error code otherwise.
175611*/
175612static int fts3TruncateSegment(
175613 Fts3Table *p, /* FTS3 table handle */
175614 sqlite3_int64 iAbsLevel, /* Absolute level of segment to modify */
175615 int iIdx, /* Index within level of segment to modify */
175616 const char *zTerm, /* Remove terms smaller than this */
175617 int nTerm /* Number of bytes in buffer zTerm */
175618){
175619 int rc = SQLITE_OK0; /* Return code */
175620 Blob root = {0,0,0}; /* New root page image */
175621 Blob block = {0,0,0}; /* Buffer used for any other block */
175622 sqlite3_int64 iBlock = 0; /* Block id */
175623 sqlite3_int64 iNewStart = 0; /* New value for iStartBlock */
175624 sqlite3_int64 iOldStart = 0; /* Old value for iStartBlock */
175625 sqlite3_stmt *pFetch = 0; /* Statement used to fetch segdir */
175626
175627 rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
175628 if( rc==SQLITE_OK0 ){
175629 int rc2; /* sqlite3_reset() return code */
175630 sqlite3_bind_int64(pFetch, 1, iAbsLevel);
175631 sqlite3_bind_int(pFetch, 2, iIdx);
175632 if( SQLITE_ROW100==sqlite3_step(pFetch) ){
175633 const char *aRoot = sqlite3_column_blob(pFetch, 4);
175634 int nRoot = sqlite3_column_bytes(pFetch, 4);
175635 iOldStart = sqlite3_column_int64(pFetch, 1);
175636 rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
175637 }
175638 rc2 = sqlite3_reset(pFetch);
175639 if( rc==SQLITE_OK0 ) rc = rc2;
175640 }
175641
175642 while( rc==SQLITE_OK0 && iBlock ){
175643 char *aBlock = 0;
175644 int nBlock = 0;
175645 iNewStart = iBlock;
175646
175647 rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
175648 if( rc==SQLITE_OK0 ){
175649 rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
175650 }
175651 if( rc==SQLITE_OK0 ){
175652 rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
175653 }
175654 sqlite3_free(aBlock);
175655 }
175656
175657 /* Variable iNewStart now contains the first valid leaf node. */
175658 if( rc==SQLITE_OK0 && iNewStart ){
175659 sqlite3_stmt *pDel = 0;
175660 rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
175661 if( rc==SQLITE_OK0 ){
175662 sqlite3_bind_int64(pDel, 1, iOldStart);
175663 sqlite3_bind_int64(pDel, 2, iNewStart-1);
175664 sqlite3_step(pDel);
175665 rc = sqlite3_reset(pDel);
175666 }
175667 }
175668
175669 if( rc==SQLITE_OK0 ){
175670 sqlite3_stmt *pChomp = 0;
175671 rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
175672 if( rc==SQLITE_OK0 ){
175673 sqlite3_bind_int64(pChomp, 1, iNewStart);
175674 sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC((sqlite3_destructor_type)0));
175675 sqlite3_bind_int64(pChomp, 3, iAbsLevel);
175676 sqlite3_bind_int(pChomp, 4, iIdx);
175677 sqlite3_step(pChomp);
175678 rc = sqlite3_reset(pChomp);
175679 sqlite3_bind_null(pChomp, 2);
175680 }
175681 }
175682
175683 sqlite3_free(root.a);
175684 sqlite3_free(block.a);
175685 return rc;
175686}
175687
175688/*
175689** This function is called after an incrmental-merge operation has run to
175690** merge (or partially merge) two or more segments from absolute level
175691** iAbsLevel.
175692**
175693** Each input segment is either removed from the db completely (if all of
175694** its data was copied to the output segment by the incrmerge operation)
175695** or modified in place so that it no longer contains those entries that
175696** have been duplicated in the output segment.
175697*/
175698static int fts3IncrmergeChomp(
175699 Fts3Table *p, /* FTS table handle */
175700 sqlite3_int64 iAbsLevel, /* Absolute level containing segments */
175701 Fts3MultiSegReader *pCsr, /* Chomp all segments opened by this cursor */
175702 int *pnRem /* Number of segments not deleted */
175703){
175704 int i;
175705 int nRem = 0;
175706 int rc = SQLITE_OK0;
175707
175708 for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK0; i--){
175709 Fts3SegReader *pSeg = 0;
175710 int j;
175711
175712 /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
175713 ** somewhere in the pCsr->apSegment[] array. */
175714 for(j=0; ALWAYS(j<pCsr->nSegment)(j<pCsr->nSegment); j++){
175715 pSeg = pCsr->apSegment[j];
175716 if( pSeg->iIdx==i ) break;
175717 }
175718 assert( j<pCsr->nSegment && pSeg->iIdx==i )((void) (0));
175719
175720 if( pSeg->aNode==0 ){
175721 /* Seg-reader is at EOF. Remove the entire input segment. */
175722 rc = fts3DeleteSegment(p, pSeg);
175723 if( rc==SQLITE_OK0 ){
175724 rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
175725 }
175726 *pnRem = 0;
175727 }else{
175728 /* The incremental merge did not copy all the data from this
175729 ** segment to the upper level. The segment is modified in place
175730 ** so that it contains no keys smaller than zTerm/nTerm. */
175731 const char *zTerm = pSeg->zTerm;
175732 int nTerm = pSeg->nTerm;
175733 rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
175734 nRem++;
175735 }
175736 }
175737
175738 if( rc==SQLITE_OK0 && nRem!=pCsr->nSegment ){
175739 rc = fts3RepackSegdirLevel(p, iAbsLevel);
175740 }
175741
175742 *pnRem = nRem;
175743 return rc;
175744}
175745
175746/*
175747** Store an incr-merge hint in the database.
175748*/
175749static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
175750 sqlite3_stmt *pReplace = 0;
175751 int rc; /* Return code */
175752
175753 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
175754 if( rc==SQLITE_OK0 ){
175755 sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
175756 sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC((sqlite3_destructor_type)0));
175757 sqlite3_step(pReplace);
175758 rc = sqlite3_reset(pReplace);
175759 sqlite3_bind_null(pReplace, 2);
175760 }
175761
175762 return rc;
175763}
175764
175765/*
175766** Load an incr-merge hint from the database. The incr-merge hint, if one
175767** exists, is stored in the rowid==1 row of the %_stat table.
175768**
175769** If successful, populate blob *pHint with the value read from the %_stat
175770** table and return SQLITE_OK. Otherwise, if an error occurs, return an
175771** SQLite error code.
175772*/
175773static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
175774 sqlite3_stmt *pSelect = 0;
175775 int rc;
175776
175777 pHint->n = 0;
175778 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
175779 if( rc==SQLITE_OK0 ){
175780 int rc2;
175781 sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
175782 if( SQLITE_ROW100==sqlite3_step(pSelect) ){
175783 const char *aHint = sqlite3_column_blob(pSelect, 0);
175784 int nHint = sqlite3_column_bytes(pSelect, 0);
175785 if( aHint ){
175786 blobGrowBuffer(pHint, nHint, &rc);
175787 if( rc==SQLITE_OK0 ){
175788 memcpy(pHint->a, aHint, nHint);
175789 pHint->n = nHint;
175790 }
175791 }
175792 }
175793 rc2 = sqlite3_reset(pSelect);
175794 if( rc==SQLITE_OK0 ) rc = rc2;
175795 }
175796
175797 return rc;
175798}
175799
175800/*
175801** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
175802** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
175803** consists of two varints, the absolute level number of the input segments
175804** and the number of input segments.
175805**
175806** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
175807** set *pRc to an SQLite error code before returning.
175808*/
175809static void fts3IncrmergeHintPush(
175810 Blob *pHint, /* Hint blob to append to */
175811 i64 iAbsLevel, /* First varint to store in hint */
175812 int nInput, /* Second varint to store in hint */
175813 int *pRc /* IN/OUT: Error code */
175814){
175815 blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
175816 if( *pRc==SQLITE_OK0 ){
175817 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
175818 pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
175819 }
175820}
175821
175822/*
175823** Read the last entry (most recently pushed) from the hint blob *pHint
175824** and then remove the entry. Write the two values read to *piAbsLevel and
175825** *pnInput before returning.
175826**
175827** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
175828** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
175829*/
175830static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
175831 const int nHint = pHint->n;
175832 int i;
175833
175834 i = pHint->n-2;
175835 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
175836 while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
175837
175838 pHint->n = i;
175839 i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
175840 i += fts3GetVarint32(&pHint->a[i], pnInput);
175841 if( i!=nHint ) return FTS_CORRUPT_VTAB;
175842
175843 return SQLITE_OK0;
175844}
175845
175846
175847/*
175848** Attempt an incremental merge that writes nMerge leaf blocks.
175849**
175850** Incremental merges happen nMin segments at a time. The segments
175851** to be merged are the nMin oldest segments (the ones with the smallest
175852** values for the _segdir.idx field) in the highest level that contains
175853** at least nMin segments. Multiple merges might occur in an attempt to
175854** write the quota of nMerge leaf blocks.
175855*/
175856SQLITE_PRIVATEstatic int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
175857 int rc; /* Return code */
175858 int nRem = nMerge; /* Number of leaf pages yet to be written */
175859 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
175860 Fts3SegFilter *pFilter; /* Filter used with cursor pCsr */
175861 IncrmergeWriter *pWriter; /* Writer object */
175862 int nSeg = 0; /* Number of input segments */
175863 sqlite3_int64 iAbsLevel = 0; /* Absolute level number to work on */
175864 Blob hint = {0, 0, 0}; /* Hint read from %_stat table */
175865 int bDirtyHint = 0; /* True if blob 'hint' has been modified */
175866
175867 /* Allocate space for the cursor, filter and writer objects */
175868 const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
175869 pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
175870 if( !pWriter ) return SQLITE_NOMEM7;
175871 pFilter = (Fts3SegFilter *)&pWriter[1];
175872 pCsr = (Fts3MultiSegReader *)&pFilter[1];
175873
175874 rc = fts3IncrmergeHintLoad(p, &hint);
175875 while( rc==SQLITE_OK0 && nRem>0 ){
175876 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
175877 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
175878 int bUseHint = 0; /* True if attempting to append */
175879 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
175880
175881 /* Search the %_segdir table for the absolute level with the smallest
175882 ** relative level number that contains at least nMin segments, if any.
175883 ** If one is found, set iAbsLevel to the absolute level number and
175884 ** nSeg to nMin. If no level with at least nMin segments can be found,
175885 ** set nSeg to -1.
175886 */
175887 rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
175888 sqlite3_bind_int(pFindLevel, 1, MAX(2, nMin)((2)>(nMin)?(2):(nMin)));
175889 if( sqlite3_step(pFindLevel)==SQLITE_ROW100 ){
175890 iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
175891 nSeg = sqlite3_column_int(pFindLevel, 1);
175892 assert( nSeg>=2 )((void) (0));
175893 }else{
175894 nSeg = -1;
175895 }
175896 rc = sqlite3_reset(pFindLevel);
175897
175898 /* If the hint read from the %_stat table is not empty, check if the
175899 ** last entry in it specifies a relative level smaller than or equal
175900 ** to the level identified by the block above (if any). If so, this
175901 ** iteration of the loop will work on merging at the hinted level.
175902 */
175903 if( rc==SQLITE_OK0 && hint.n ){
175904 int nHint = hint.n;
175905 sqlite3_int64 iHintAbsLevel = 0; /* Hint level */
175906 int nHintSeg = 0; /* Hint number of segments */
175907
175908 rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
175909 if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
175910 iAbsLevel = iHintAbsLevel;
175911 nSeg = nHintSeg;
175912 bUseHint = 1;
175913 bDirtyHint = 1;
175914 }else{
175915 /* This undoes the effect of the HintPop() above - so that no entry
175916 ** is removed from the hint blob. */
175917 hint.n = nHint;
175918 }
175919 }
175920
175921 /* If nSeg is less that zero, then there is no level with at least
175922 ** nMin segments and no hint in the %_stat table. No work to do.
175923 ** Exit early in this case. */
175924 if( nSeg<0 ) break;
175925
175926 /* Open a cursor to iterate through the contents of the oldest nSeg
175927 ** indexes of absolute level iAbsLevel. If this cursor is opened using
175928 ** the 'hint' parameters, it is possible that there are less than nSeg
175929 ** segments available in level iAbsLevel. In this case, no work is
175930 ** done on iAbsLevel - fall through to the next iteration of the loop
175931 ** to start work on some other level. */
175932 memset(pWriter, 0, nAlloc);
175933 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
175934
175935 if( rc==SQLITE_OK0 ){
175936 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
175937 assert( bUseHint==1 || bUseHint==0 )((void) (0));
175938 if( iIdx==0 || (bUseHint && iIdx==1) ){
175939 int bIgnore = 0;
175940 rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
175941 if( bIgnore ){
175942 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
175943 }
175944 }
175945 }
175946
175947 if( rc==SQLITE_OK0 ){
175948 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
175949 }
175950 if( SQLITE_OK0==rc && pCsr->nSegment==nSeg
175951 && SQLITE_OK0==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
175952 && SQLITE_ROW100==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
175953 ){
175954 if( bUseHint && iIdx>0 ){
175955 const char *zKey = pCsr->zTerm;
175956 int nKey = pCsr->nTerm;
175957 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
175958 }else{
175959 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
175960 }
175961
175962 if( rc==SQLITE_OK0 && pWriter->nLeafEst ){
175963 fts3LogMerge(nSeg, iAbsLevel);
175964 do {
175965 rc = fts3IncrmergeAppend(p, pWriter, pCsr);
175966 if( rc==SQLITE_OK0 ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
175967 if( pWriter->nWork>=nRem && rc==SQLITE_ROW100 ) rc = SQLITE_OK0;
175968 }while( rc==SQLITE_ROW100 );
175969
175970 /* Update or delete the input segments */
175971 if( rc==SQLITE_OK0 ){
175972 nRem -= (1 + pWriter->nWork);
175973 rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
175974 if( nSeg!=0 ){
175975 bDirtyHint = 1;
175976 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
175977 }
175978 }
175979 }
175980
175981 if( nSeg!=0 ){
175982 pWriter->nLeafData = pWriter->nLeafData * -1;
175983 }
175984 fts3IncrmergeRelease(p, pWriter, &rc);
175985 if( nSeg==0 && pWriter->bNoLeafData==0 ){
175986 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
175987 }
175988 }
175989
175990 sqlite3Fts3SegReaderFinish(pCsr);
175991 }
175992
175993 /* Write the hint values into the %_stat table for the next incr-merger */
175994 if( bDirtyHint && rc==SQLITE_OK0 ){
175995 rc = fts3IncrmergeHintStore(p, &hint);
175996 }
175997
175998 sqlite3_free(pWriter);
175999 sqlite3_free(hint.a);
176000 return rc;
176001}
176002
176003/*
176004** Convert the text beginning at *pz into an integer and return
176005** its value. Advance *pz to point to the first character past
176006** the integer.
176007**
176008** This function used for parameters to merge= and incrmerge=
176009** commands.
176010*/
176011static int fts3Getint(const char **pz){
176012 const char *z = *pz;
176013 int i = 0;
176014 while( (*z)>='0' && (*z)<='9' && i<214748363 ) i = 10*i + *(z++) - '0';
176015 *pz = z;
176016 return i;
176017}
176018
176019/*
176020** Process statements of the form:
176021**
176022** INSERT INTO table(table) VALUES('merge=A,B');
176023**
176024** A and B are integers that decode to be the number of leaf pages
176025** written for the merge, and the minimum number of segments on a level
176026** before it will be selected for a merge, respectively.
176027*/
176028static int fts3DoIncrmerge(
176029 Fts3Table *p, /* FTS3 table handle */
176030 const char *zParam /* Nul-terminated string containing "A,B" */
176031){
176032 int rc;
176033 int nMin = (FTS3_MERGE_COUNT / 2);
176034 int nMerge = 0;
176035 const char *z = zParam;
176036
176037 /* Read the first integer value */
176038 nMerge = fts3Getint(&z);
176039
176040 /* If the first integer value is followed by a ',', read the second
176041 ** integer value. */
176042 if( z[0]==',' && z[1]!='\0' ){
176043 z++;
176044 nMin = fts3Getint(&z);
176045 }
176046
176047 if( z[0]!='\0' || nMin<2 ){
176048 rc = SQLITE_ERROR1;
176049 }else{
176050 rc = SQLITE_OK0;
176051 if( !p->bHasStat ){
176052 assert( p->bFts4==0 )((void) (0));
176053 sqlite3Fts3CreateStatTable(&rc, p);
176054 }
176055 if( rc==SQLITE_OK0 ){
176056 rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
176057 }
176058 sqlite3Fts3SegmentsClose(p);
176059 }
176060 return rc;
176061}
176062
176063/*
176064** Process statements of the form:
176065**
176066** INSERT INTO table(table) VALUES('automerge=X');
176067**
176068** where X is an integer. X==0 means to turn automerge off. X!=0 means
176069** turn it on. The setting is persistent.
176070*/
176071static int fts3DoAutoincrmerge(
176072 Fts3Table *p, /* FTS3 table handle */
176073 const char *zParam /* Nul-terminated string containing boolean */
176074){
176075 int rc = SQLITE_OK0;
176076 sqlite3_stmt *pStmt = 0;
176077 p->nAutoincrmerge = fts3Getint(&zParam);
176078 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
176079 p->nAutoincrmerge = 8;
176080 }
176081 if( !p->bHasStat ){
176082 assert( p->bFts4==0 )((void) (0));
176083 sqlite3Fts3CreateStatTable(&rc, p);
176084 if( rc ) return rc;
176085 }
176086 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
176087 if( rc ) return rc;
176088 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
176089 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
176090 sqlite3_step(pStmt);
176091 rc = sqlite3_reset(pStmt);
176092 return rc;
176093}
176094
176095/*
176096** Return a 64-bit checksum for the FTS index entry specified by the
176097** arguments to this function.
176098*/
176099static u64 fts3ChecksumEntry(
176100 const char *zTerm, /* Pointer to buffer containing term */
176101 int nTerm, /* Size of zTerm in bytes */
176102 int iLangid, /* Language id for current row */
176103 int iIndex, /* Index (0..Fts3Table.nIndex-1) */
176104 i64 iDocid, /* Docid for current row. */
176105 int iCol, /* Column number */
176106 int iPos /* Position */
176107){
176108 int i;
176109 u64 ret = (u64)iDocid;
176110
176111 ret += (ret<<3) + iLangid;
176112 ret += (ret<<3) + iIndex;
176113 ret += (ret<<3) + iCol;
176114 ret += (ret<<3) + iPos;
176115 for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
176116
176117 return ret;
176118}
176119
176120/*
176121** Return a checksum of all entries in the FTS index that correspond to
176122** language id iLangid. The checksum is calculated by XORing the checksums
176123** of each individual entry (see fts3ChecksumEntry()) together.
176124**
176125** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
176126** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
176127** return value is undefined in this case.
176128*/
176129static u64 fts3ChecksumIndex(
176130 Fts3Table *p, /* FTS3 table handle */
176131 int iLangid, /* Language id to return cksum for */
176132 int iIndex, /* Index to cksum (0..p->nIndex-1) */
176133 int *pRc /* OUT: Return code */
176134){
176135 Fts3SegFilter filter;
176136 Fts3MultiSegReader csr;
176137 int rc;
176138 u64 cksum = 0;
176139
176140 assert( *pRc==SQLITE_OK )((void) (0));
176141
176142 memset(&filter, 0, sizeof(filter));
176143 memset(&csr, 0, sizeof(csr));
176144 filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
176145 filter.flags |= FTS3_SEGMENT_SCAN;
176146
176147 rc = sqlite3Fts3SegReaderCursor(
176148 p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
176149 );
176150 if( rc==SQLITE_OK0 ){
176151 rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
176152 }
176153
176154 if( rc==SQLITE_OK0 ){
176155 while( SQLITE_ROW100==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
176156 char *pCsr = csr.aDoclist;
176157 char *pEnd = &pCsr[csr.nDoclist];
176158
176159 i64 iDocid = 0;
176160 i64 iCol = 0;
176161 i64 iPos = 0;
176162
176163 pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
176164 while( pCsr<pEnd ){
176165 i64 iVal = 0;
176166 pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
176167 if( pCsr<pEnd ){
176168 if( iVal==0 || iVal==1 ){
176169 iCol = 0;
176170 iPos = 0;
176171 if( iVal ){
176172 pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
176173 }else{
176174 pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
176175 iDocid += iVal;
176176 }
176177 }else{
176178 iPos += (iVal - 2);
176179 cksum = cksum ^ fts3ChecksumEntry(
176180 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
176181 (int)iCol, (int)iPos
176182 );
176183 }
176184 }
176185 }
176186 }
176187 }
176188 sqlite3Fts3SegReaderFinish(&csr);
176189
176190 *pRc = rc;
176191 return cksum;
176192}
176193
176194/*
176195** Check if the contents of the FTS index match the current contents of the
176196** content table. If no error occurs and the contents do match, set *pbOk
176197** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
176198** to false before returning.
176199**
176200** If an error occurs (e.g. an OOM or IO error), return an SQLite error
176201** code. The final value of *pbOk is undefined in this case.
176202*/
176203static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
176204 int rc = SQLITE_OK0; /* Return code */
176205 u64 cksum1 = 0; /* Checksum based on FTS index contents */
176206 u64 cksum2 = 0; /* Checksum based on %_content contents */
176207 sqlite3_stmt *pAllLangid = 0; /* Statement to return all language-ids */
176208
176209 /* This block calculates the checksum according to the FTS index. */
176210 rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
176211 if( rc==SQLITE_OK0 ){
176212 int rc2;
176213 sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid);
176214 sqlite3_bind_int(pAllLangid, 2, p->nIndex);
176215 while( rc==SQLITE_OK0 && sqlite3_step(pAllLangid)==SQLITE_ROW100 ){
176216 int iLangid = sqlite3_column_int(pAllLangid, 0);
176217 int i;
176218 for(i=0; i<p->nIndex; i++){
176219 cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
176220 }
176221 }
176222 rc2 = sqlite3_reset(pAllLangid);
176223 if( rc==SQLITE_OK0 ) rc = rc2;
176224 }
176225
176226 /* This block calculates the checksum according to the %_content table */
176227 if( rc==SQLITE_OK0 ){
176228 sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
176229 sqlite3_stmt *pStmt = 0;
176230 char *zSql;
176231
176232 zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
176233 if( !zSql ){
176234 rc = SQLITE_NOMEM7;
176235 }else{
176236 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
176237 sqlite3_free(zSql);
176238 }
176239
176240 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
176241 i64 iDocid = sqlite3_column_int64(pStmt, 0);
176242 int iLang = langidFromSelect(p, pStmt);
176243 int iCol;
176244
176245 for(iCol=0; rc==SQLITE_OK0 && iCol<p->nColumn; iCol++){
176246 if( p->abNotindexed[iCol]==0 ){
176247 const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
176248 int nText = sqlite3_column_bytes(pStmt, iCol+1);
176249 sqlite3_tokenizer_cursor *pT = 0;
176250
176251 rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText,&pT);
176252 while( rc==SQLITE_OK0 ){
176253 char const *zToken; /* Buffer containing token */
176254 int nToken = 0; /* Number of bytes in token */
176255 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
176256 int iPos = 0; /* Position of token in zText */
176257
176258 rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
176259 if( rc==SQLITE_OK0 ){
176260 int i;
176261 cksum2 = cksum2 ^ fts3ChecksumEntry(
176262 zToken, nToken, iLang, 0, iDocid, iCol, iPos
176263 );
176264 for(i=1; i<p->nIndex; i++){
176265 if( p->aIndex[i].nPrefix<=nToken ){
176266 cksum2 = cksum2 ^ fts3ChecksumEntry(
176267 zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
176268 );
176269 }
176270 }
176271 }
176272 }
176273 if( pT ) pModule->xClose(pT);
176274 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
176275 }
176276 }
176277 }
176278
176279 sqlite3_finalize(pStmt);
176280 }
176281
176282 *pbOk = (cksum1==cksum2);
176283 return rc;
176284}
176285
176286/*
176287** Run the integrity-check. If no error occurs and the current contents of
176288** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
176289** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
176290**
176291** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
176292** error code.
176293**
176294** The integrity-check works as follows. For each token and indexed token
176295** prefix in the document set, a 64-bit checksum is calculated (by code
176296** in fts3ChecksumEntry()) based on the following:
176297**
176298** + The index number (0 for the main index, 1 for the first prefix
176299** index etc.),
176300** + The token (or token prefix) text itself,
176301** + The language-id of the row it appears in,
176302** + The docid of the row it appears in,
176303** + The column it appears in, and
176304** + The tokens position within that column.
176305**
176306** The checksums for all entries in the index are XORed together to create
176307** a single checksum for the entire index.
176308**
176309** The integrity-check code calculates the same checksum in two ways:
176310**
176311** 1. By scanning the contents of the FTS index, and
176312** 2. By scanning and tokenizing the content table.
176313**
176314** If the two checksums are identical, the integrity-check is deemed to have
176315** passed.
176316*/
176317static int fts3DoIntegrityCheck(
176318 Fts3Table *p /* FTS3 table handle */
176319){
176320 int rc;
176321 int bOk = 0;
176322 rc = fts3IntegrityCheck(p, &bOk);
176323 if( rc==SQLITE_OK0 && bOk==0 ) rc = FTS_CORRUPT_VTAB;
176324 return rc;
176325}
176326
176327/*
176328** Handle a 'special' INSERT of the form:
176329**
176330** "INSERT INTO tbl(tbl) VALUES(<expr>)"
176331**
176332** Argument pVal contains the result of <expr>. Currently the only
176333** meaningful value to insert is the text 'optimize'.
176334*/
176335static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
176336 int rc; /* Return Code */
176337 const char *zVal = (const char *)sqlite3_value_text(pVal);
176338 int nVal = sqlite3_value_bytes(pVal);
176339
176340 if( !zVal ){
176341 return SQLITE_NOMEM7;
176342 }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
176343 rc = fts3DoOptimize(p, 0);
176344 }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
176345 rc = fts3DoRebuild(p);
176346 }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
176347 rc = fts3DoIntegrityCheck(p);
176348 }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
176349 rc = fts3DoIncrmerge(p, &zVal[6]);
176350 }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
176351 rc = fts3DoAutoincrmerge(p, &zVal[10]);
176352#ifdef SQLITE_TEST
176353 }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
176354 p->nNodeSize = atoi(&zVal[9]);
176355 rc = SQLITE_OK0;
176356 }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
176357 p->nMaxPendingData = atoi(&zVal[11]);
176358 rc = SQLITE_OK0;
176359 }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
176360 p->bNoIncrDoclist = atoi(&zVal[21]);
176361 rc = SQLITE_OK0;
176362#endif
176363 }else{
176364 rc = SQLITE_ERROR1;
176365 }
176366
176367 return rc;
176368}
176369
176370#ifndef SQLITE_DISABLE_FTS4_DEFERRED
176371/*
176372** Delete all cached deferred doclists. Deferred doclists are cached
176373** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
176374*/
176375SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
176376 Fts3DeferredToken *pDef;
176377 for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
176378 fts3PendingListDelete(pDef->pList);
176379 pDef->pList = 0;
176380 }
176381}
176382
176383/*
176384** Free all entries in the pCsr->pDeffered list. Entries are added to
176385** this list using sqlite3Fts3DeferToken().
176386*/
176387SQLITE_PRIVATEstatic void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
176388 Fts3DeferredToken *pDef;
176389 Fts3DeferredToken *pNext;
176390 for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
176391 pNext = pDef->pNext;
176392 fts3PendingListDelete(pDef->pList);
176393 sqlite3_free(pDef);
176394 }
176395 pCsr->pDeferred = 0;
176396}
176397
176398/*
176399** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
176400** based on the row that pCsr currently points to.
176401**
176402** A deferred-doclist is like any other doclist with position information
176403** included, except that it only contains entries for a single row of the
176404** table, not for all rows.
176405*/
176406SQLITE_PRIVATEstatic int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
176407 int rc = SQLITE_OK0; /* Return code */
176408 if( pCsr->pDeferred ){
176409 int i; /* Used to iterate through table columns */
176410 sqlite3_int64 iDocid; /* Docid of the row pCsr points to */
176411 Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */
176412
176413 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
176414 sqlite3_tokenizer *pT = p->pTokenizer;
176415 sqlite3_tokenizer_module const *pModule = pT->pModule;
176416
176417 assert( pCsr->isRequireSeek==0 )((void) (0));
176418 iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
176419
176420 for(i=0; i<p->nColumn && rc==SQLITE_OK0; i++){
176421 if( p->abNotindexed[i]==0 ){
176422 const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
176423 sqlite3_tokenizer_cursor *pTC = 0;
176424
176425 rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
176426 while( rc==SQLITE_OK0 ){
176427 char const *zToken; /* Buffer containing token */
176428 int nToken = 0; /* Number of bytes in token */
176429 int iDum1 = 0, iDum2 = 0; /* Dummy variables */
176430 int iPos = 0; /* Position of token in zText */
176431
176432 rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
176433 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK0; pDef=pDef->pNext){
176434 Fts3PhraseToken *pPT = pDef->pToken;
176435 if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
176436 && (pPT->bFirst==0 || iPos==0)
176437 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
176438 && (0==memcmp(zToken, pPT->z, pPT->n))
176439 ){
176440 fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
176441 }
176442 }
176443 }
176444 if( pTC ) pModule->xClose(pTC);
176445 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
176446 }
176447 }
176448
176449 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK0; pDef=pDef->pNext){
176450 if( pDef->pList ){
176451 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
176452 }
176453 }
176454 }
176455
176456 return rc;
176457}
176458
176459SQLITE_PRIVATEstatic int sqlite3Fts3DeferredTokenList(
176460 Fts3DeferredToken *p,
176461 char **ppData,
176462 int *pnData
176463){
176464 char *pRet;
176465 int nSkip;
176466 sqlite3_int64 dummy;
176467
176468 *ppData = 0;
176469 *pnData = 0;
176470
176471 if( p->pList==0 ){
176472 return SQLITE_OK0;
176473 }
176474
176475 pRet = (char *)sqlite3_malloc(p->pList->nData);
176476 if( !pRet ) return SQLITE_NOMEM7;
176477
176478 nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
176479 *pnData = p->pList->nData - nSkip;
176480 *ppData = pRet;
176481
176482 memcpy(pRet, &p->pList->aData[nSkip], *pnData);
176483 return SQLITE_OK0;
176484}
176485
176486/*
176487** Add an entry for token pToken to the pCsr->pDeferred list.
176488*/
176489SQLITE_PRIVATEstatic int sqlite3Fts3DeferToken(
176490 Fts3Cursor *pCsr, /* Fts3 table cursor */
176491 Fts3PhraseToken *pToken, /* Token to defer */
176492 int iCol /* Column that token must appear in (or -1) */
176493){
176494 Fts3DeferredToken *pDeferred;
176495 pDeferred = sqlite3_malloc(sizeof(*pDeferred));
176496 if( !pDeferred ){
176497 return SQLITE_NOMEM7;
176498 }
176499 memset(pDeferred, 0, sizeof(*pDeferred));
176500 pDeferred->pToken = pToken;
176501 pDeferred->pNext = pCsr->pDeferred;
176502 pDeferred->iCol = iCol;
176503 pCsr->pDeferred = pDeferred;
176504
176505 assert( pToken->pDeferred==0 )((void) (0));
176506 pToken->pDeferred = pDeferred;
176507
176508 return SQLITE_OK0;
176509}
176510#endif
176511
176512/*
176513** SQLite value pRowid contains the rowid of a row that may or may not be
176514** present in the FTS3 table. If it is, delete it and adjust the contents
176515** of subsiduary data structures accordingly.
176516*/
176517static int fts3DeleteByRowid(
176518 Fts3Table *p,
176519 sqlite3_value *pRowid,
176520 int *pnChng, /* IN/OUT: Decrement if row is deleted */
176521 u32 *aSzDel
176522){
176523 int rc = SQLITE_OK0; /* Return code */
176524 int bFound = 0; /* True if *pRowid really is in the table */
176525
176526 fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
176527 if( bFound && rc==SQLITE_OK0 ){
176528 int isEmpty = 0; /* Deleting *pRowid leaves the table empty */
176529 rc = fts3IsEmpty(p, pRowid, &isEmpty);
176530 if( rc==SQLITE_OK0 ){
176531 if( isEmpty ){
176532 /* Deleting this row means the whole table is empty. In this case
176533 ** delete the contents of all three tables and throw away any
176534 ** data in the pendingTerms hash table. */
176535 rc = fts3DeleteAll(p, 1);
176536 *pnChng = 0;
176537 memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
176538 }else{
176539 *pnChng = *pnChng - 1;
176540 if( p->zContentTbl==0 ){
176541 fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
176542 }
176543 if( p->bHasDocsize ){
176544 fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
176545 }
176546 }
176547 }
176548 }
176549
176550 return rc;
176551}
176552
176553/*
176554** This function does the work for the xUpdate method of FTS3 virtual
176555** tables. The schema of the virtual table being:
176556**
176557** CREATE TABLE <table name>(
176558** <user columns>,
176559** <table name> HIDDEN,
176560** docid HIDDEN,
176561** <langid> HIDDEN
176562** );
176563**
176564**
176565*/
176566SQLITE_PRIVATEstatic int sqlite3Fts3UpdateMethod(
176567 sqlite3_vtab *pVtab, /* FTS3 vtab object */
176568 int nArg, /* Size of argument array */
176569 sqlite3_value **apVal, /* Array of arguments */
176570 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
176571){
176572 Fts3Table *p = (Fts3Table *)pVtab;
176573 int rc = SQLITE_OK0; /* Return Code */
176574 u32 *aSzIns = 0; /* Sizes of inserted documents */
176575 u32 *aSzDel = 0; /* Sizes of deleted documents */
176576 int nChng = 0; /* Net change in number of documents */
176577 int bInsertDone = 0;
176578
176579 /* At this point it must be known if the %_stat table exists or not.
176580 ** So bHasStat may not be 2. */
176581 assert( p->bHasStat==0 || p->bHasStat==1 )((void) (0));
176582
176583 assert( p->pSegments==0 )((void) (0));
176584 assert(((void) (0))
176585 nArg==1 /* DELETE operations */((void) (0))
176586 || nArg==(2 + p->nColumn + 3) /* INSERT or UPDATE operations */((void) (0))
176587 )((void) (0));
176588
176589 /* Check for a "special" INSERT operation. One of the form:
176590 **
176591 ** INSERT INTO xyz(xyz) VALUES('command');
176592 */
176593 if( nArg>1
176594 && sqlite3_value_type(apVal[0])==SQLITE_NULL5
176595 && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL5
176596 ){
176597 rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
176598 goto update_out;
176599 }
176600
176601 if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
176602 rc = SQLITE_CONSTRAINT19;
176603 goto update_out;
176604 }
176605
176606 /* Allocate space to hold the change in document sizes */
176607 aSzDel = sqlite3_malloc64(sizeof(aSzDel[0])*((sqlite3_int64)p->nColumn+1)*2);
176608 if( aSzDel==0 ){
176609 rc = SQLITE_NOMEM7;
176610 goto update_out;
176611 }
176612 aSzIns = &aSzDel[p->nColumn+1];
176613 memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
176614
176615 rc = fts3Writelock(p);
176616 if( rc!=SQLITE_OK0 ) goto update_out;
176617
176618 /* If this is an INSERT operation, or an UPDATE that modifies the rowid
176619 ** value, then this operation requires constraint handling.
176620 **
176621 ** If the on-conflict mode is REPLACE, this means that the existing row
176622 ** should be deleted from the database before inserting the new row. Or,
176623 ** if the on-conflict mode is other than REPLACE, then this method must
176624 ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
176625 ** modify the database file.
176626 */
176627 if( nArg>1 && p->zContentTbl==0 ){
176628 /* Find the value object that holds the new rowid value. */
176629 sqlite3_value *pNewRowid = apVal[3+p->nColumn];
176630 if( sqlite3_value_type(pNewRowid)==SQLITE_NULL5 ){
176631 pNewRowid = apVal[1];
176632 }
176633
176634 if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL5 && (
176635 sqlite3_value_type(apVal[0])==SQLITE_NULL5
176636 || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
176637 )){
176638 /* The new rowid is not NULL (in this case the rowid will be
176639 ** automatically assigned and there is no chance of a conflict), and
176640 ** the statement is either an INSERT or an UPDATE that modifies the
176641 ** rowid column. So if the conflict mode is REPLACE, then delete any
176642 ** existing row with rowid=pNewRowid.
176643 **
176644 ** Or, if the conflict mode is not REPLACE, insert the new record into
176645 ** the %_content table. If we hit the duplicate rowid constraint (or any
176646 ** other error) while doing so, return immediately.
176647 **
176648 ** This branch may also run if pNewRowid contains a value that cannot
176649 ** be losslessly converted to an integer. In this case, the eventual
176650 ** call to fts3InsertData() (either just below or further on in this
176651 ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
176652 ** invoked, it will delete zero rows (since no row will have
176653 ** docid=$pNewRowid if $pNewRowid is not an integer value).
176654 */
176655 if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE5 ){
176656 rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
176657 }else{
176658 rc = fts3InsertData(p, apVal, pRowid);
176659 bInsertDone = 1;
176660 }
176661 }
176662 }
176663 if( rc!=SQLITE_OK0 ){
176664 goto update_out;
176665 }
176666
176667 /* If this is a DELETE or UPDATE operation, remove the old record. */
176668 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL5 ){
176669 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER )((void) (0));
176670 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
176671 }
176672
176673 /* If this is an INSERT or UPDATE operation, insert the new record. */
176674 if( nArg>1 && rc==SQLITE_OK0 ){
176675 int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
176676 if( bInsertDone==0 ){
176677 rc = fts3InsertData(p, apVal, pRowid);
176678 if( rc==SQLITE_CONSTRAINT19 && p->zContentTbl==0 ){
176679 rc = FTS_CORRUPT_VTAB;
176680 }
176681 }
176682 if( rc==SQLITE_OK0 ){
176683 rc = fts3PendingTermsDocid(p, 0, iLangid, *pRowid);
176684 }
176685 if( rc==SQLITE_OK0 ){
176686 assert( p->iPrevDocid==*pRowid )((void) (0));
176687 rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
176688 }
176689 if( p->bHasDocsize ){
176690 fts3InsertDocsize(&rc, p, aSzIns);
176691 }
176692 nChng++;
176693 }
176694
176695 if( p->bFts4 ){
176696 fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
176697 }
176698
176699 update_out:
176700 sqlite3_free(aSzDel);
176701 sqlite3Fts3SegmentsClose(p);
176702 return rc;
176703}
176704
176705/*
176706** Flush any data in the pending-terms hash table to disk. If successful,
176707** merge all segments in the database (including the new segment, if
176708** there was any data to flush) into a single segment.
176709*/
176710SQLITE_PRIVATEstatic int sqlite3Fts3Optimize(Fts3Table *p){
176711 int rc;
176712 rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
176713 if( rc==SQLITE_OK0 ){
176714 rc = fts3DoOptimize(p, 1);
176715 if( rc==SQLITE_OK0 || rc==SQLITE_DONE101 ){
176716 int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
176717 if( rc2!=SQLITE_OK0 ) rc = rc2;
176718 }else{
176719 sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
176720 sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
176721 }
176722 }
176723 sqlite3Fts3SegmentsClose(p);
176724 return rc;
176725}
176726
176727#endif
176728
176729/************** End of fts3_write.c ******************************************/
176730/************** Begin file fts3_snippet.c ************************************/
176731/*
176732** 2009 Oct 23
176733**
176734** The author disclaims copyright to this source code. In place of
176735** a legal notice, here is a blessing:
176736**
176737** May you do good and not evil.
176738** May you find forgiveness for yourself and forgive others.
176739** May you share freely, never taking more than you give.
176740**
176741******************************************************************************
176742*/
176743
176744/* #include "fts3Int.h" */
176745#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
176746
176747/* #include <string.h> */
176748/* #include <assert.h> */
176749
176750/*
176751** Characters that may appear in the second argument to matchinfo().
176752*/
176753#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
176754#define FTS3_MATCHINFO_NCOL 'c' /* 1 value */
176755#define FTS3_MATCHINFO_NDOC 'n' /* 1 value */
176756#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */
176757#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */
176758#define FTS3_MATCHINFO_LCS 's' /* nCol values */
176759#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */
176760#define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */
176761#define FTS3_MATCHINFO_LHITS_BM 'b' /* nCol*nPhrase values */
176762
176763/*
176764** The default value for the second argument to matchinfo().
176765*/
176766#define FTS3_MATCHINFO_DEFAULT "pcx"
176767
176768
176769/*
176770** Used as an fts3ExprIterate() context when loading phrase doclists to
176771** Fts3Expr.aDoclist[]/nDoclist.
176772*/
176773typedef struct LoadDoclistCtx LoadDoclistCtx;
176774struct LoadDoclistCtx {
176775 Fts3Cursor *pCsr; /* FTS3 Cursor */
176776 int nPhrase; /* Number of phrases seen so far */
176777 int nToken; /* Number of tokens seen so far */
176778};
176779
176780/*
176781** The following types are used as part of the implementation of the
176782** fts3BestSnippet() routine.
176783*/
176784typedef struct SnippetIter SnippetIter;
176785typedef struct SnippetPhrase SnippetPhrase;
176786typedef struct SnippetFragment SnippetFragment;
176787
176788struct SnippetIter {
176789 Fts3Cursor *pCsr; /* Cursor snippet is being generated from */
176790 int iCol; /* Extract snippet from this column */
176791 int nSnippet; /* Requested snippet length (in tokens) */
176792 int nPhrase; /* Number of phrases in query */
176793 SnippetPhrase *aPhrase; /* Array of size nPhrase */
176794 int iCurrent; /* First token of current snippet */
176795};
176796
176797struct SnippetPhrase {
176798 int nToken; /* Number of tokens in phrase */
176799 char *pList; /* Pointer to start of phrase position list */
176800 int iHead; /* Next value in position list */
176801 char *pHead; /* Position list data following iHead */
176802 int iTail; /* Next value in trailing position list */
176803 char *pTail; /* Position list data following iTail */
176804};
176805
176806struct SnippetFragment {
176807 int iCol; /* Column snippet is extracted from */
176808 int iPos; /* Index of first token in snippet */
176809 u64 covered; /* Mask of query phrases covered */
176810 u64 hlmask; /* Mask of snippet terms to highlight */
176811};
176812
176813/*
176814** This type is used as an fts3ExprIterate() context object while
176815** accumulating the data returned by the matchinfo() function.
176816*/
176817typedef struct MatchInfo MatchInfo;
176818struct MatchInfo {
176819 Fts3Cursor *pCursor; /* FTS3 Cursor */
176820 int nCol; /* Number of columns in table */
176821 int nPhrase; /* Number of matchable phrases in query */
176822 sqlite3_int64 nDoc; /* Number of docs in database */
176823 char flag;
176824 u32 *aMatchinfo; /* Pre-allocated buffer */
176825};
176826
176827/*
176828** An instance of this structure is used to manage a pair of buffers, each
176829** (nElem * sizeof(u32)) bytes in size. See the MatchinfoBuffer code below
176830** for details.
176831*/
176832struct MatchinfoBuffer {
176833 u8 aRef[3];
176834 int nElem;
176835 int bGlobal; /* Set if global data is loaded */
176836 char *zMatchinfo;
176837 u32 aMatchinfo[1];
176838};
176839
176840
176841/*
176842** The snippet() and offsets() functions both return text values. An instance
176843** of the following structure is used to accumulate those values while the
176844** functions are running. See fts3StringAppend() for details.
176845*/
176846typedef struct StrBuffer StrBuffer;
176847struct StrBuffer {
176848 char *z; /* Pointer to buffer containing string */
176849 int n; /* Length of z in bytes (excl. nul-term) */
176850 int nAlloc; /* Allocated size of buffer z in bytes */
176851};
176852
176853
176854/*************************************************************************
176855** Start of MatchinfoBuffer code.
176856*/
176857
176858/*
176859** Allocate a two-slot MatchinfoBuffer object.
176860*/
176861static MatchinfoBuffer *fts3MIBufferNew(size_t nElem, const char *zMatchinfo){
176862 MatchinfoBuffer *pRet;
176863 sqlite3_int64 nByte = sizeof(u32) * (2*(sqlite3_int64)nElem + 1)
176864 + sizeof(MatchinfoBuffer);
176865 sqlite3_int64 nStr = strlen(zMatchinfo);
176866
176867 pRet = sqlite3_malloc64(nByte + nStr+1);
176868 if( pRet ){
176869 memset(pRet, 0, nByte);
176870 pRet->aMatchinfo[0] = (u8*)(&pRet->aMatchinfo[1]) - (u8*)pRet;
176871 pRet->aMatchinfo[1+nElem] = pRet->aMatchinfo[0]
176872 + sizeof(u32)*((int)nElem+1);
176873 pRet->nElem = (int)nElem;
176874 pRet->zMatchinfo = ((char*)pRet) + nByte;
176875 memcpy(pRet->zMatchinfo, zMatchinfo, nStr+1);
176876 pRet->aRef[0] = 1;
176877 }
176878
176879 return pRet;
176880}
176881
176882static void fts3MIBufferFree(void *p){
176883 MatchinfoBuffer *pBuf = (MatchinfoBuffer*)((u8*)p - ((u32*)p)[-1]);
176884
176885 assert( (u32*)p==&pBuf->aMatchinfo[1]((void) (0))
176886 || (u32*)p==&pBuf->aMatchinfo[pBuf->nElem+2]((void) (0))
176887 )((void) (0));
176888 if( (u32*)p==&pBuf->aMatchinfo[1] ){
176889 pBuf->aRef[1] = 0;
176890 }else{
176891 pBuf->aRef[2] = 0;
176892 }
176893
176894 if( pBuf->aRef[0]==0 && pBuf->aRef[1]==0 && pBuf->aRef[2]==0 ){
176895 sqlite3_free(pBuf);
176896 }
176897}
176898
176899static void (*fts3MIBufferAlloc(MatchinfoBuffer *p, u32 **paOut))(void*){
176900 void (*xRet)(void*) = 0;
176901 u32 *aOut = 0;
176902
176903 if( p->aRef[1]==0 ){
176904 p->aRef[1] = 1;
176905 aOut = &p->aMatchinfo[1];
176906 xRet = fts3MIBufferFree;
176907 }
176908 else if( p->aRef[2]==0 ){
176909 p->aRef[2] = 1;
176910 aOut = &p->aMatchinfo[p->nElem+2];
176911 xRet = fts3MIBufferFree;
176912 }else{
176913 aOut = (u32*)sqlite3_malloc64(p->nElem * sizeof(u32));
176914 if( aOut ){
176915 xRet = sqlite3_free;
176916 if( p->bGlobal ) memcpy(aOut, &p->aMatchinfo[1], p->nElem*sizeof(u32));
176917 }
176918 }
176919
176920 *paOut = aOut;
176921 return xRet;
176922}
176923
176924static void fts3MIBufferSetGlobal(MatchinfoBuffer *p){
176925 p->bGlobal = 1;
176926 memcpy(&p->aMatchinfo[2+p->nElem], &p->aMatchinfo[1], p->nElem*sizeof(u32));
176927}
176928
176929/*
176930** Free a MatchinfoBuffer object allocated using fts3MIBufferNew()
176931*/
176932SQLITE_PRIVATEstatic void sqlite3Fts3MIBufferFree(MatchinfoBuffer *p){
176933 if( p ){
176934 assert( p->aRef[0]==1 )((void) (0));
176935 p->aRef[0] = 0;
176936 if( p->aRef[0]==0 && p->aRef[1]==0 && p->aRef[2]==0 ){
176937 sqlite3_free(p);
176938 }
176939 }
176940}
176941
176942/*
176943** End of MatchinfoBuffer code.
176944*************************************************************************/
176945
176946
176947/*
176948** This function is used to help iterate through a position-list. A position
176949** list is a list of unique integers, sorted from smallest to largest. Each
176950** element of the list is represented by an FTS3 varint that takes the value
176951** of the difference between the current element and the previous one plus
176952** two. For example, to store the position-list:
176953**
176954** 4 9 113
176955**
176956** the three varints:
176957**
176958** 6 7 106
176959**
176960** are encoded.
176961**
176962** When this function is called, *pp points to the start of an element of
176963** the list. *piPos contains the value of the previous entry in the list.
176964** After it returns, *piPos contains the value of the next element of the
176965** list and *pp is advanced to the following varint.
176966*/
176967static void fts3GetDeltaPosition(char **pp, int *piPos){
176968 int iVal;
176969 *pp += fts3GetVarint32(*pp, &iVal);
176970 *piPos += (iVal-2);
176971}
176972
176973/*
176974** Helper function for fts3ExprIterate() (see below).
176975*/
176976static int fts3ExprIterate2(
176977 Fts3Expr *pExpr, /* Expression to iterate phrases of */
176978 int *piPhrase, /* Pointer to phrase counter */
176979 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
176980 void *pCtx /* Second argument to pass to callback */
176981){
176982 int rc; /* Return code */
176983 int eType = pExpr->eType; /* Type of expression node pExpr */
176984
176985 if( eType!=FTSQUERY_PHRASE ){
176986 assert( pExpr->pLeft && pExpr->pRight )((void) (0));
176987 rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
176988 if( rc==SQLITE_OK0 && eType!=FTSQUERY_NOT ){
176989 rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
176990 }
176991 }else{
176992 rc = x(pExpr, *piPhrase, pCtx);
176993 (*piPhrase)++;
176994 }
176995 return rc;
176996}
176997
176998/*
176999** Iterate through all phrase nodes in an FTS3 query, except those that
177000** are part of a sub-tree that is the right-hand-side of a NOT operator.
177001** For each phrase node found, the supplied callback function is invoked.
177002**
177003** If the callback function returns anything other than SQLITE_OK,
177004** the iteration is abandoned and the error code returned immediately.
177005** Otherwise, SQLITE_OK is returned after a callback has been made for
177006** all eligible phrase nodes.
177007*/
177008static int fts3ExprIterate(
177009 Fts3Expr *pExpr, /* Expression to iterate phrases of */
177010 int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */
177011 void *pCtx /* Second argument to pass to callback */
177012){
177013 int iPhrase = 0; /* Variable used as the phrase counter */
177014 return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
177015}
177016
177017
177018/*
177019** This is an fts3ExprIterate() callback used while loading the doclists
177020** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
177021** fts3ExprLoadDoclists().
177022*/
177023static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
177024 int rc = SQLITE_OK0;
177025 Fts3Phrase *pPhrase = pExpr->pPhrase;
177026 LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
177027
177028 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
177029
177030 p->nPhrase++;
177031 p->nToken += pPhrase->nToken;
177032
177033 return rc;
177034}
177035
177036/*
177037** Load the doclists for each phrase in the query associated with FTS3 cursor
177038** pCsr.
177039**
177040** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
177041** phrases in the expression (all phrases except those directly or
177042** indirectly descended from the right-hand-side of a NOT operator). If
177043** pnToken is not NULL, then it is set to the number of tokens in all
177044** matchable phrases of the expression.
177045*/
177046static int fts3ExprLoadDoclists(
177047 Fts3Cursor *pCsr, /* Fts3 cursor for current query */
177048 int *pnPhrase, /* OUT: Number of phrases in query */
177049 int *pnToken /* OUT: Number of tokens in query */
177050){
177051 int rc; /* Return Code */
177052 LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */
177053 sCtx.pCsr = pCsr;
177054 rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
177055 if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
177056 if( pnToken ) *pnToken = sCtx.nToken;
177057 return rc;
177058}
177059
177060static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
177061 (*(int *)ctx)++;
177062 pExpr->iPhrase = iPhrase;
177063 return SQLITE_OK0;
177064}
177065static int fts3ExprPhraseCount(Fts3Expr *pExpr){
177066 int nPhrase = 0;
177067 (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
177068 return nPhrase;
177069}
177070
177071/*
177072** Advance the position list iterator specified by the first two
177073** arguments so that it points to the first element with a value greater
177074** than or equal to parameter iNext.
177075*/
177076static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
177077 char *pIter = *ppIter;
177078 if( pIter ){
177079 int iIter = *piIter;
177080
177081 while( iIter<iNext ){
177082 if( 0==(*pIter & 0xFE) ){
177083 iIter = -1;
177084 pIter = 0;
177085 break;
177086 }
177087 fts3GetDeltaPosition(&pIter, &iIter);
177088 }
177089
177090 *piIter = iIter;
177091 *ppIter = pIter;
177092 }
177093}
177094
177095/*
177096** Advance the snippet iterator to the next candidate snippet.
177097*/
177098static int fts3SnippetNextCandidate(SnippetIter *pIter){
177099 int i; /* Loop counter */
177100
177101 if( pIter->iCurrent<0 ){
177102 /* The SnippetIter object has just been initialized. The first snippet
177103 ** candidate always starts at offset 0 (even if this candidate has a
177104 ** score of 0.0).
177105 */
177106 pIter->iCurrent = 0;
177107
177108 /* Advance the 'head' iterator of each phrase to the first offset that
177109 ** is greater than or equal to (iNext+nSnippet).
177110 */
177111 for(i=0; i<pIter->nPhrase; i++){
177112 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
177113 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
177114 }
177115 }else{
177116 int iStart;
177117 int iEnd = 0x7FFFFFFF;
177118
177119 for(i=0; i<pIter->nPhrase; i++){
177120 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
177121 if( pPhrase->pHead && pPhrase->iHead<iEnd ){
177122 iEnd = pPhrase->iHead;
177123 }
177124 }
177125 if( iEnd==0x7FFFFFFF ){
177126 return 1;
177127 }
177128
177129 pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
177130 for(i=0; i<pIter->nPhrase; i++){
177131 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
177132 fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
177133 fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
177134 }
177135 }
177136
177137 return 0;
177138}
177139
177140/*
177141** Retrieve information about the current candidate snippet of snippet
177142** iterator pIter.
177143*/
177144static void fts3SnippetDetails(
177145 SnippetIter *pIter, /* Snippet iterator */
177146 u64 mCovered, /* Bitmask of phrases already covered */
177147 int *piToken, /* OUT: First token of proposed snippet */
177148 int *piScore, /* OUT: "Score" for this snippet */
177149 u64 *pmCover, /* OUT: Bitmask of phrases covered */
177150 u64 *pmHighlight /* OUT: Bitmask of terms to highlight */
177151){
177152 int iStart = pIter->iCurrent; /* First token of snippet */
177153 int iScore = 0; /* Score of this snippet */
177154 int i; /* Loop counter */
177155 u64 mCover = 0; /* Mask of phrases covered by this snippet */
177156 u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */
177157
177158 for(i=0; i<pIter->nPhrase; i++){
177159 SnippetPhrase *pPhrase = &pIter->aPhrase[i];
177160 if( pPhrase->pTail ){
177161 char *pCsr = pPhrase->pTail;
177162 int iCsr = pPhrase->iTail;
177163
177164 while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
177165 int j;
177166 u64 mPhrase = (u64)1 << i;
177167 u64 mPos = (u64)1 << (iCsr - iStart);
177168 assert( iCsr>=iStart && (iCsr - iStart)<=64 )((void) (0));
177169 assert( i>=0 && i<=64 )((void) (0));
177170 if( (mCover|mCovered)&mPhrase ){
177171 iScore++;
177172 }else{
177173 iScore += 1000;
177174 }
177175 mCover |= mPhrase;
177176
177177 for(j=0; j<pPhrase->nToken; j++){
177178 mHighlight |= (mPos>>j);
177179 }
177180
177181 if( 0==(*pCsr & 0x0FE) ) break;
177182 fts3GetDeltaPosition(&pCsr, &iCsr);
177183 }
177184 }
177185 }
177186
177187 /* Set the output variables before returning. */
177188 *piToken = iStart;
177189 *piScore = iScore;
177190 *pmCover = mCover;
177191 *pmHighlight = mHighlight;
177192}
177193
177194/*
177195** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
177196** Each invocation populates an element of the SnippetIter.aPhrase[] array.
177197*/
177198static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
177199 SnippetIter *p = (SnippetIter *)ctx;
177200 SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
177201 char *pCsr;
177202 int rc;
177203
177204 pPhrase->nToken = pExpr->pPhrase->nToken;
177205 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
177206 assert( rc==SQLITE_OK || pCsr==0 )((void) (0));
177207 if( pCsr ){
177208 int iFirst = 0;
177209 pPhrase->pList = pCsr;
177210 fts3GetDeltaPosition(&pCsr, &iFirst);
177211 if( iFirst<0 ){
177212 rc = FTS_CORRUPT_VTAB;
177213 }else{
177214 pPhrase->pHead = pCsr;
177215 pPhrase->pTail = pCsr;
177216 pPhrase->iHead = iFirst;
177217 pPhrase->iTail = iFirst;
177218 }
177219 }else{
177220 assert( rc!=SQLITE_OK || (((void) (0))
177221 pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0((void) (0))
177222 ))((void) (0));
177223 }
177224
177225 return rc;
177226}
177227
177228/*
177229** Select the fragment of text consisting of nFragment contiguous tokens
177230** from column iCol that represent the "best" snippet. The best snippet
177231** is the snippet with the highest score, where scores are calculated
177232** by adding:
177233**
177234** (a) +1 point for each occurrence of a matchable phrase in the snippet.
177235**
177236** (b) +1000 points for the first occurrence of each matchable phrase in
177237** the snippet for which the corresponding mCovered bit is not set.
177238**
177239** The selected snippet parameters are stored in structure *pFragment before
177240** returning. The score of the selected snippet is stored in *piScore
177241** before returning.
177242*/
177243static int fts3BestSnippet(
177244 int nSnippet, /* Desired snippet length */
177245 Fts3Cursor *pCsr, /* Cursor to create snippet for */
177246 int iCol, /* Index of column to create snippet from */
177247 u64 mCovered, /* Mask of phrases already covered */
177248 u64 *pmSeen, /* IN/OUT: Mask of phrases seen */
177249 SnippetFragment *pFragment, /* OUT: Best snippet found */
177250 int *piScore /* OUT: Score of snippet pFragment */
177251){
177252 int rc; /* Return Code */
177253 int nList; /* Number of phrases in expression */
177254 SnippetIter sIter; /* Iterates through snippet candidates */
177255 sqlite3_int64 nByte; /* Number of bytes of space to allocate */
177256 int iBestScore = -1; /* Best snippet score found so far */
177257 int i; /* Loop counter */
177258
177259 memset(&sIter, 0, sizeof(sIter));
177260
177261 /* Iterate through the phrases in the expression to count them. The same
177262 ** callback makes sure the doclists are loaded for each phrase.
177263 */
177264 rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
177265 if( rc!=SQLITE_OK0 ){
177266 return rc;
177267 }
177268
177269 /* Now that it is known how many phrases there are, allocate and zero
177270 ** the required space using malloc().
177271 */
177272 nByte = sizeof(SnippetPhrase) * nList;
177273 sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc64(nByte);
177274 if( !sIter.aPhrase ){
177275 return SQLITE_NOMEM7;
177276 }
177277 memset(sIter.aPhrase, 0, nByte);
177278
177279 /* Initialize the contents of the SnippetIter object. Then iterate through
177280 ** the set of phrases in the expression to populate the aPhrase[] array.
177281 */
177282 sIter.pCsr = pCsr;
177283 sIter.iCol = iCol;
177284 sIter.nSnippet = nSnippet;
177285 sIter.nPhrase = nList;
177286 sIter.iCurrent = -1;
177287 rc = fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void*)&sIter);
177288 if( rc==SQLITE_OK0 ){
177289
177290 /* Set the *pmSeen output variable. */
177291 for(i=0; i<nList; i++){
177292 if( sIter.aPhrase[i].pHead ){
177293 *pmSeen |= (u64)1 << i;
177294 }
177295 }
177296
177297 /* Loop through all candidate snippets. Store the best snippet in
177298 ** *pFragment. Store its associated 'score' in iBestScore.
177299 */
177300 pFragment->iCol = iCol;
177301 while( !fts3SnippetNextCandidate(&sIter) ){
177302 int iPos;
177303 int iScore;
177304 u64 mCover;
177305 u64 mHighlite;
177306 fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover,&mHighlite);
177307 assert( iScore>=0 )((void) (0));
177308 if( iScore>iBestScore ){
177309 pFragment->iPos = iPos;
177310 pFragment->hlmask = mHighlite;
177311 pFragment->covered = mCover;
177312 iBestScore = iScore;
177313 }
177314 }
177315
177316 *piScore = iBestScore;
177317 }
177318 sqlite3_free(sIter.aPhrase);
177319 return rc;
177320}
177321
177322
177323/*
177324** Append a string to the string-buffer passed as the first argument.
177325**
177326** If nAppend is negative, then the length of the string zAppend is
177327** determined using strlen().
177328*/
177329static int fts3StringAppend(
177330 StrBuffer *pStr, /* Buffer to append to */
177331 const char *zAppend, /* Pointer to data to append to buffer */
177332 int nAppend /* Size of zAppend in bytes (or -1) */
177333){
177334 if( nAppend<0 ){
177335 nAppend = (int)strlen(zAppend);
177336 }
177337
177338 /* If there is insufficient space allocated at StrBuffer.z, use realloc()
177339 ** to grow the buffer until so that it is big enough to accomadate the
177340 ** appended data.
177341 */
177342 if( pStr->n+nAppend+1>=pStr->nAlloc ){
177343 sqlite3_int64 nAlloc = pStr->nAlloc+(sqlite3_int64)nAppend+100;
177344 char *zNew = sqlite3_realloc64(pStr->z, nAlloc);
177345 if( !zNew ){
177346 return SQLITE_NOMEM7;
177347 }
177348 pStr->z = zNew;
177349 pStr->nAlloc = nAlloc;
177350 }
177351 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) )((void) (0));
177352
177353 /* Append the data to the string buffer. */
177354 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
177355 pStr->n += nAppend;
177356 pStr->z[pStr->n] = '\0';
177357
177358 return SQLITE_OK0;
177359}
177360
177361/*
177362** The fts3BestSnippet() function often selects snippets that end with a
177363** query term. That is, the final term of the snippet is always a term
177364** that requires highlighting. For example, if 'X' is a highlighted term
177365** and '.' is a non-highlighted term, BestSnippet() may select:
177366**
177367** ........X.....X
177368**
177369** This function "shifts" the beginning of the snippet forward in the
177370** document so that there are approximately the same number of
177371** non-highlighted terms to the right of the final highlighted term as there
177372** are to the left of the first highlighted term. For example, to this:
177373**
177374** ....X.....X....
177375**
177376** This is done as part of extracting the snippet text, not when selecting
177377** the snippet. Snippet selection is done based on doclists only, so there
177378** is no way for fts3BestSnippet() to know whether or not the document
177379** actually contains terms that follow the final highlighted term.
177380*/
177381static int fts3SnippetShift(
177382 Fts3Table *pTab, /* FTS3 table snippet comes from */
177383 int iLangid, /* Language id to use in tokenizing */
177384 int nSnippet, /* Number of tokens desired for snippet */
177385 const char *zDoc, /* Document text to extract snippet from */
177386 int nDoc, /* Size of buffer zDoc in bytes */
177387 int *piPos, /* IN/OUT: First token of snippet */
177388 u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */
177389){
177390 u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */
177391
177392 if( hlmask ){
177393 int nLeft; /* Tokens to the left of first highlight */
177394 int nRight; /* Tokens to the right of last highlight */
177395 int nDesired; /* Ideal number of tokens to shift forward */
177396
177397 for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
177398 for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
177399 assert( (nSnippet-1-nRight)<=63 && (nSnippet-1-nRight)>=0 )((void) (0));
177400 nDesired = (nLeft-nRight)/2;
177401
177402 /* Ideally, the start of the snippet should be pushed forward in the
177403 ** document nDesired tokens. This block checks if there are actually
177404 ** nDesired tokens to the right of the snippet. If so, *piPos and
177405 ** *pHlMask are updated to shift the snippet nDesired tokens to the
177406 ** right. Otherwise, the snippet is shifted by the number of tokens
177407 ** available.
177408 */
177409 if( nDesired>0 ){
177410 int nShift; /* Number of tokens to shift snippet by */
177411 int iCurrent = 0; /* Token counter */
177412 int rc; /* Return Code */
177413 sqlite3_tokenizer_module *pMod;
177414 sqlite3_tokenizer_cursor *pC;
177415 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
177416
177417 /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
177418 ** or more tokens in zDoc/nDoc.
177419 */
177420 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
177421 if( rc!=SQLITE_OK0 ){
177422 return rc;
177423 }
177424 while( rc==SQLITE_OK0 && iCurrent<(nSnippet+nDesired) ){
177425 const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
177426 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
177427 }
177428 pMod->xClose(pC);
177429 if( rc!=SQLITE_OK0 && rc!=SQLITE_DONE101 ){ return rc; }
177430
177431 nShift = (rc==SQLITE_DONE101)+iCurrent-nSnippet;
177432 assert( nShift<=nDesired )((void) (0));
177433 if( nShift>0 ){
177434 *piPos += nShift;
177435 *pHlmask = hlmask >> nShift;
177436 }
177437 }
177438 }
177439 return SQLITE_OK0;
177440}
177441
177442/*
177443** Extract the snippet text for fragment pFragment from cursor pCsr and
177444** append it to string buffer pOut.
177445*/
177446static int fts3SnippetText(
177447 Fts3Cursor *pCsr, /* FTS3 Cursor */
177448 SnippetFragment *pFragment, /* Snippet to extract */
177449 int iFragment, /* Fragment number */
177450 int isLast, /* True for final fragment in snippet */
177451 int nSnippet, /* Number of tokens in extracted snippet */
177452 const char *zOpen, /* String inserted before highlighted term */
177453 const char *zClose, /* String inserted after highlighted term */
177454 const char *zEllipsis, /* String inserted between snippets */
177455 StrBuffer *pOut /* Write output here */
177456){
177457 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
177458 int rc; /* Return code */
177459 const char *zDoc; /* Document text to extract snippet from */
177460 int nDoc; /* Size of zDoc in bytes */
177461 int iCurrent = 0; /* Current token number of document */
177462 int iEnd = 0; /* Byte offset of end of current token */
177463 int isShiftDone = 0; /* True after snippet is shifted */
177464 int iPos = pFragment->iPos; /* First token of snippet */
177465 u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
177466 int iCol = pFragment->iCol+1; /* Query column to extract text from */
177467 sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
177468 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */
177469
177470 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
177471 if( zDoc==0 ){
177472 if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL5 ){
177473 return SQLITE_NOMEM7;
177474 }
177475 return SQLITE_OK0;
177476 }
177477 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
177478
177479 /* Open a token cursor on the document. */
177480 pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
177481 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
177482 if( rc!=SQLITE_OK0 ){
177483 return rc;
177484 }
177485
177486 while( rc==SQLITE_OK0 ){
177487 const char *ZDUMMY; /* Dummy argument used with tokenizer */
177488 int DUMMY1 = -1; /* Dummy argument used with tokenizer */
177489 int iBegin = 0; /* Offset in zDoc of start of token */
177490 int iFin = 0; /* Offset in zDoc of end of token */
177491 int isHighlight = 0; /* True for highlighted terms */
177492
177493 /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
177494 ** in the FTS code the variable that the third argument to xNext points to
177495 ** is initialized to zero before the first (*but not necessarily
177496 ** subsequent*) call to xNext(). This is done for a particular application
177497 ** that needs to know whether or not the tokenizer is being used for
177498 ** snippet generation or for some other purpose.
177499 **
177500 ** Extreme care is required when writing code to depend on this
177501 ** initialization. It is not a documented part of the tokenizer interface.
177502 ** If a tokenizer is used directly by any code outside of FTS, this
177503 ** convention might not be respected. */
177504 rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
177505 if( rc!=SQLITE_OK0 ){
177506 if( rc==SQLITE_DONE101 ){
177507 /* Special case - the last token of the snippet is also the last token
177508 ** of the column. Append any punctuation that occurred between the end
177509 ** of the previous token and the end of the document to the output.
177510 ** Then break out of the loop. */
177511 rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
177512 }
177513 break;
177514 }
177515 if( iCurrent<iPos ){ continue; }
177516
177517 if( !isShiftDone ){
177518 int n = nDoc - iBegin;
177519 rc = fts3SnippetShift(
177520 pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
177521 );
177522 isShiftDone = 1;
177523
177524 /* Now that the shift has been done, check if the initial "..." are
177525 ** required. They are required if (a) this is not the first fragment,
177526 ** or (b) this fragment does not begin at position 0 of its column.
177527 */
177528 if( rc==SQLITE_OK0 ){
177529 if( iPos>0 || iFragment>0 ){
177530 rc = fts3StringAppend(pOut, zEllipsis, -1);
177531 }else if( iBegin ){
177532 rc = fts3StringAppend(pOut, zDoc, iBegin);
177533 }
177534 }
177535 if( rc!=SQLITE_OK0 || iCurrent<iPos ) continue;
177536 }
177537
177538 if( iCurrent>=(iPos+nSnippet) ){
177539 if( isLast ){
177540 rc = fts3StringAppend(pOut, zEllipsis, -1);
177541 }
177542 break;
177543 }
177544
177545 /* Set isHighlight to true if this term should be highlighted. */
177546 isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
177547
177548 if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
177549 if( rc==SQLITE_OK0 && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
177550 if( rc==SQLITE_OK0 ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
177551 if( rc==SQLITE_OK0 && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
177552
177553 iEnd = iFin;
177554 }
177555
177556 pMod->xClose(pC);
177557 return rc;
177558}
177559
177560
177561/*
177562** This function is used to count the entries in a column-list (a
177563** delta-encoded list of term offsets within a single column of a single
177564** row). When this function is called, *ppCollist should point to the
177565** beginning of the first varint in the column-list (the varint that
177566** contains the position of the first matching term in the column data).
177567** Before returning, *ppCollist is set to point to the first byte after
177568** the last varint in the column-list (either the 0x00 signifying the end
177569** of the position-list, or the 0x01 that precedes the column number of
177570** the next column in the position-list).
177571**
177572** The number of elements in the column-list is returned.
177573*/
177574static int fts3ColumnlistCount(char **ppCollist){
177575 char *pEnd = *ppCollist;
177576 char c = 0;
177577 int nEntry = 0;
177578
177579 /* A column-list is terminated by either a 0x01 or 0x00. */
177580 while( 0xFE & (*pEnd | c) ){
177581 c = *pEnd++ & 0x80;
177582 if( !c ) nEntry++;
177583 }
177584
177585 *ppCollist = pEnd;
177586 return nEntry;
177587}
177588
177589/*
177590** This function gathers 'y' or 'b' data for a single phrase.
177591*/
177592static int fts3ExprLHits(
177593 Fts3Expr *pExpr, /* Phrase expression node */
177594 MatchInfo *p /* Matchinfo context */
177595){
177596 Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab;
177597 int iStart;
177598 Fts3Phrase *pPhrase = pExpr->pPhrase;
177599 char *pIter = pPhrase->doclist.pList;
177600 int iCol = 0;
177601
177602 assert( p->flag==FTS3_MATCHINFO_LHITS_BM || p->flag==FTS3_MATCHINFO_LHITS )((void) (0));
177603 if( p->flag==FTS3_MATCHINFO_LHITS ){
177604 iStart = pExpr->iPhrase * p->nCol;
177605 }else{
177606 iStart = pExpr->iPhrase * ((p->nCol + 31) / 32);
177607 }
177608
177609 while( 1 ){
177610 int nHit = fts3ColumnlistCount(&pIter);
177611 if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){
177612 if( p->flag==FTS3_MATCHINFO_LHITS ){
177613 p->aMatchinfo[iStart + iCol] = (u32)nHit;
177614 }else if( nHit ){
177615 p->aMatchinfo[iStart + (iCol+1)/32] |= (1 << (iCol&0x1F));
177616 }
177617 }
177618 assert( *pIter==0x00 || *pIter==0x01 )((void) (0));
177619 if( *pIter!=0x01 ) break;
177620 pIter++;
177621 pIter += fts3GetVarint32(pIter, &iCol);
177622 if( iCol>=p->nCol ) return FTS_CORRUPT_VTAB;
177623 }
177624 return SQLITE_OK0;
177625}
177626
177627/*
177628** Gather the results for matchinfo directives 'y' and 'b'.
177629*/
177630static int fts3ExprLHitGather(
177631 Fts3Expr *pExpr,
177632 MatchInfo *p
177633){
177634 int rc = SQLITE_OK0;
177635 assert( (pExpr->pLeft==0)==(pExpr->pRight==0) )((void) (0));
177636 if( pExpr->bEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){
177637 if( pExpr->pLeft ){
177638 rc = fts3ExprLHitGather(pExpr->pLeft, p);
177639 if( rc==SQLITE_OK0 ) rc = fts3ExprLHitGather(pExpr->pRight, p);
177640 }else{
177641 rc = fts3ExprLHits(pExpr, p);
177642 }
177643 }
177644 return rc;
177645}
177646
177647/*
177648** fts3ExprIterate() callback used to collect the "global" matchinfo stats
177649** for a single query.
177650**
177651** fts3ExprIterate() callback to load the 'global' elements of a
177652** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
177653** of the matchinfo array that are constant for all rows returned by the
177654** current query.
177655**
177656** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
177657** function populates Matchinfo.aMatchinfo[] as follows:
177658**
177659** for(iCol=0; iCol<nCol; iCol++){
177660** aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
177661** aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
177662** }
177663**
177664** where X is the number of matches for phrase iPhrase is column iCol of all
177665** rows of the table. Y is the number of rows for which column iCol contains
177666** at least one instance of phrase iPhrase.
177667**
177668** If the phrase pExpr consists entirely of deferred tokens, then all X and
177669** Y values are set to nDoc, where nDoc is the number of documents in the
177670** file system. This is done because the full-text index doclist is required
177671** to calculate these values properly, and the full-text index doclist is
177672** not available for deferred tokens.
177673*/
177674static int fts3ExprGlobalHitsCb(
177675 Fts3Expr *pExpr, /* Phrase expression node */
177676 int iPhrase, /* Phrase number (numbered from zero) */
177677 void *pCtx /* Pointer to MatchInfo structure */
177678){
177679 MatchInfo *p = (MatchInfo *)pCtx;
177680 return sqlite3Fts3EvalPhraseStats(
177681 p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
177682 );
177683}
177684
177685/*
177686** fts3ExprIterate() callback used to collect the "local" part of the
177687** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
177688** array that are different for each row returned by the query.
177689*/
177690static int fts3ExprLocalHitsCb(
177691 Fts3Expr *pExpr, /* Phrase expression node */
177692 int iPhrase, /* Phrase number */
177693 void *pCtx /* Pointer to MatchInfo structure */
177694){
177695 int rc = SQLITE_OK0;
177696 MatchInfo *p = (MatchInfo *)pCtx;
177697 int iStart = iPhrase * p->nCol * 3;
177698 int i;
177699
177700 for(i=0; i<p->nCol && rc==SQLITE_OK0; i++){
177701 char *pCsr;
177702 rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
177703 if( pCsr ){
177704 p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
177705 }else{
177706 p->aMatchinfo[iStart+i*3] = 0;
177707 }
177708 }
177709
177710 return rc;
177711}
177712
177713static int fts3MatchinfoCheck(
177714 Fts3Table *pTab,
177715 char cArg,
177716 char **pzErr
177717){
177718 if( (cArg==FTS3_MATCHINFO_NPHRASE)
177719 || (cArg==FTS3_MATCHINFO_NCOL)
177720 || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
177721 || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
177722 || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
177723 || (cArg==FTS3_MATCHINFO_LCS)
177724 || (cArg==FTS3_MATCHINFO_HITS)
177725 || (cArg==FTS3_MATCHINFO_LHITS)
177726 || (cArg==FTS3_MATCHINFO_LHITS_BM)
177727 ){
177728 return SQLITE_OK0;
177729 }
177730 sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg);
177731 return SQLITE_ERROR1;
177732}
177733
177734static size_t fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
177735 size_t nVal; /* Number of integers output by cArg */
177736
177737 switch( cArg ){
177738 case FTS3_MATCHINFO_NDOC:
177739 case FTS3_MATCHINFO_NPHRASE:
177740 case FTS3_MATCHINFO_NCOL:
177741 nVal = 1;
177742 break;
177743
177744 case FTS3_MATCHINFO_AVGLENGTH:
177745 case FTS3_MATCHINFO_LENGTH:
177746 case FTS3_MATCHINFO_LCS:
177747 nVal = pInfo->nCol;
177748 break;
177749
177750 case FTS3_MATCHINFO_LHITS:
177751 nVal = pInfo->nCol * pInfo->nPhrase;
177752 break;
177753
177754 case FTS3_MATCHINFO_LHITS_BM:
177755 nVal = pInfo->nPhrase * ((pInfo->nCol + 31) / 32);
177756 break;
177757
177758 default:
177759 assert( cArg==FTS3_MATCHINFO_HITS )((void) (0));
177760 nVal = pInfo->nCol * pInfo->nPhrase * 3;
177761 break;
177762 }
177763
177764 return nVal;
177765}
177766
177767static int fts3MatchinfoSelectDoctotal(
177768 Fts3Table *pTab,
177769 sqlite3_stmt **ppStmt,
177770 sqlite3_int64 *pnDoc,
177771 const char **paLen
177772){
177773 sqlite3_stmt *pStmt;
177774 const char *a;
177775 sqlite3_int64 nDoc;
177776
177777 if( !*ppStmt ){
177778 int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
177779 if( rc!=SQLITE_OK0 ) return rc;
177780 }
177781 pStmt = *ppStmt;
177782 assert( sqlite3_data_count(pStmt)==1 )((void) (0));
177783
177784 a = sqlite3_column_blob(pStmt, 0);
177785 a += sqlite3Fts3GetVarint(a, &nDoc);
177786 if( nDoc==0 ) return FTS_CORRUPT_VTAB;
177787 *pnDoc = (u32)nDoc;
177788
177789 if( paLen ) *paLen = a;
177790 return SQLITE_OK0;
177791}
177792
177793/*
177794** An instance of the following structure is used to store state while
177795** iterating through a multi-column position-list corresponding to the
177796** hits for a single phrase on a single row in order to calculate the
177797** values for a matchinfo() FTS3_MATCHINFO_LCS request.
177798*/
177799typedef struct LcsIterator LcsIterator;
177800struct LcsIterator {
177801 Fts3Expr *pExpr; /* Pointer to phrase expression */
177802 int iPosOffset; /* Tokens count up to end of this phrase */
177803 char *pRead; /* Cursor used to iterate through aDoclist */
177804 int iPos; /* Current position */
177805};
177806
177807/*
177808** If LcsIterator.iCol is set to the following value, the iterator has
177809** finished iterating through all offsets for all columns.
177810*/
177811#define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
177812
177813static int fts3MatchinfoLcsCb(
177814 Fts3Expr *pExpr, /* Phrase expression node */
177815 int iPhrase, /* Phrase number (numbered from zero) */
177816 void *pCtx /* Pointer to MatchInfo structure */
177817){
177818 LcsIterator *aIter = (LcsIterator *)pCtx;
177819 aIter[iPhrase].pExpr = pExpr;
177820 return SQLITE_OK0;
177821}
177822
177823/*
177824** Advance the iterator passed as an argument to the next position. Return
177825** 1 if the iterator is at EOF or if it now points to the start of the
177826** position list for the next column.
177827*/
177828static int fts3LcsIteratorAdvance(LcsIterator *pIter){
177829 char *pRead = pIter->pRead;
177830 sqlite3_int64 iRead;
177831 int rc = 0;
177832
177833 pRead += sqlite3Fts3GetVarint(pRead, &iRead);
177834 if( iRead==0 || iRead==1 ){
177835 pRead = 0;
177836 rc = 1;
177837 }else{
177838 pIter->iPos += (int)(iRead-2);
177839 }
177840
177841 pIter->pRead = pRead;
177842 return rc;
177843}
177844
177845/*
177846** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
177847**
177848** If the call is successful, the longest-common-substring lengths for each
177849** column are written into the first nCol elements of the pInfo->aMatchinfo[]
177850** array before returning. SQLITE_OK is returned in this case.
177851**
177852** Otherwise, if an error occurs, an SQLite error code is returned and the
177853** data written to the first nCol elements of pInfo->aMatchinfo[] is
177854** undefined.
177855*/
177856static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
177857 LcsIterator *aIter;
177858 int i;
177859 int iCol;
177860 int nToken = 0;
177861 int rc = SQLITE_OK0;
177862
177863 /* Allocate and populate the array of LcsIterator objects. The array
177864 ** contains one element for each matchable phrase in the query.
177865 **/
177866 aIter = sqlite3_malloc64(sizeof(LcsIterator) * pCsr->nPhrase);
177867 if( !aIter ) return SQLITE_NOMEM7;
177868 memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
177869 (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
177870
177871 for(i=0; i<pInfo->nPhrase; i++){
177872 LcsIterator *pIter = &aIter[i];
177873 nToken -= pIter->pExpr->pPhrase->nToken;
177874 pIter->iPosOffset = nToken;
177875 }
177876
177877 for(iCol=0; iCol<pInfo->nCol; iCol++){
177878 int nLcs = 0; /* LCS value for this column */
177879 int nLive = 0; /* Number of iterators in aIter not at EOF */
177880
177881 for(i=0; i<pInfo->nPhrase; i++){
177882 LcsIterator *pIt = &aIter[i];
177883 rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
177884 if( rc!=SQLITE_OK0 ) goto matchinfo_lcs_out;
177885 if( pIt->pRead ){
177886 pIt->iPos = pIt->iPosOffset;
177887 fts3LcsIteratorAdvance(pIt);
177888 if( pIt->pRead==0 ){
177889 rc = FTS_CORRUPT_VTAB;
177890 goto matchinfo_lcs_out;
177891 }
177892 nLive++;
177893 }
177894 }
177895
177896 while( nLive>0 ){
177897 LcsIterator *pAdv = 0; /* The iterator to advance by one position */
177898 int nThisLcs = 0; /* LCS for the current iterator positions */
177899
177900 for(i=0; i<pInfo->nPhrase; i++){
177901 LcsIterator *pIter = &aIter[i];
177902 if( pIter->pRead==0 ){
177903 /* This iterator is already at EOF for this column. */
177904 nThisLcs = 0;
177905 }else{
177906 if( pAdv==0 || pIter->iPos<pAdv->iPos ){
177907 pAdv = pIter;
177908 }
177909 if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
177910 nThisLcs++;
177911 }else{
177912 nThisLcs = 1;
177913 }
177914 if( nThisLcs>nLcs ) nLcs = nThisLcs;
177915 }
177916 }
177917 if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
177918 }
177919
177920 pInfo->aMatchinfo[iCol] = nLcs;
177921 }
177922
177923 matchinfo_lcs_out:
177924 sqlite3_free(aIter);
177925 return rc;
177926}
177927
177928/*
177929** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
177930** be returned by the matchinfo() function. Argument zArg contains the
177931** format string passed as the second argument to matchinfo (or the
177932** default value "pcx" if no second argument was specified). The format
177933** string has already been validated and the pInfo->aMatchinfo[] array
177934** is guaranteed to be large enough for the output.
177935**
177936** If bGlobal is true, then populate all fields of the matchinfo() output.
177937** If it is false, then assume that those fields that do not change between
177938** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
177939** have already been populated.
177940**
177941** Return SQLITE_OK if successful, or an SQLite error code if an error
177942** occurs. If a value other than SQLITE_OK is returned, the state the
177943** pInfo->aMatchinfo[] buffer is left in is undefined.
177944*/
177945static int fts3MatchinfoValues(
177946 Fts3Cursor *pCsr, /* FTS3 cursor object */
177947 int bGlobal, /* True to grab the global stats */
177948 MatchInfo *pInfo, /* Matchinfo context object */
177949 const char *zArg /* Matchinfo format string */
177950){
177951 int rc = SQLITE_OK0;
177952 int i;
177953 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
177954 sqlite3_stmt *pSelect = 0;
177955
177956 for(i=0; rc==SQLITE_OK0 && zArg[i]; i++){
177957 pInfo->flag = zArg[i];
177958 switch( zArg[i] ){
177959 case FTS3_MATCHINFO_NPHRASE:
177960 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
177961 break;
177962
177963 case FTS3_MATCHINFO_NCOL:
177964 if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
177965 break;
177966
177967 case FTS3_MATCHINFO_NDOC:
177968 if( bGlobal ){
177969 sqlite3_int64 nDoc = 0;
177970 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
177971 pInfo->aMatchinfo[0] = (u32)nDoc;
177972 }
177973 break;
177974
177975 case FTS3_MATCHINFO_AVGLENGTH:
177976 if( bGlobal ){
177977 sqlite3_int64 nDoc; /* Number of rows in table */
177978 const char *a; /* Aggregate column length array */
177979
177980 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
177981 if( rc==SQLITE_OK0 ){
177982 int iCol;
177983 for(iCol=0; iCol<pInfo->nCol; iCol++){
177984 u32 iVal;
177985 sqlite3_int64 nToken;
177986 a += sqlite3Fts3GetVarint(a, &nToken);
177987 iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
177988 pInfo->aMatchinfo[iCol] = iVal;
177989 }
177990 }
177991 }
177992 break;
177993
177994 case FTS3_MATCHINFO_LENGTH: {
177995 sqlite3_stmt *pSelectDocsize = 0;
177996 rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
177997 if( rc==SQLITE_OK0 ){
177998 int iCol;
177999 const char *a = sqlite3_column_blob(pSelectDocsize, 0);
178000 for(iCol=0; iCol<pInfo->nCol; iCol++){
178001 sqlite3_int64 nToken;
178002 a += sqlite3Fts3GetVarint(a, &nToken);
178003 pInfo->aMatchinfo[iCol] = (u32)nToken;
178004 }
178005 }
178006 sqlite3_reset(pSelectDocsize);
178007 break;
178008 }
178009
178010 case FTS3_MATCHINFO_LCS:
178011 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
178012 if( rc==SQLITE_OK0 ){
178013 rc = fts3MatchinfoLcs(pCsr, pInfo);
178014 }
178015 break;
178016
178017 case FTS3_MATCHINFO_LHITS_BM:
178018 case FTS3_MATCHINFO_LHITS: {
178019 size_t nZero = fts3MatchinfoSize(pInfo, zArg[i]) * sizeof(u32);
178020 memset(pInfo->aMatchinfo, 0, nZero);
178021 rc = fts3ExprLHitGather(pCsr->pExpr, pInfo);
178022 break;
178023 }
178024
178025 default: {
178026 Fts3Expr *pExpr;
178027 assert( zArg[i]==FTS3_MATCHINFO_HITS )((void) (0));
178028 pExpr = pCsr->pExpr;
178029 rc = fts3ExprLoadDoclists(pCsr, 0, 0);
178030 if( rc!=SQLITE_OK0 ) break;
178031 if( bGlobal ){
178032 if( pCsr->pDeferred ){
178033 rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
178034 if( rc!=SQLITE_OK0 ) break;
178035 }
178036 rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
178037 sqlite3Fts3EvalTestDeferred(pCsr, &rc);
178038 if( rc!=SQLITE_OK0 ) break;
178039 }
178040 (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
178041 break;
178042 }
178043 }
178044
178045 pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
178046 }
178047
178048 sqlite3_reset(pSelect);
178049 return rc;
178050}
178051
178052
178053/*
178054** Populate pCsr->aMatchinfo[] with data for the current row. The
178055** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
178056*/
178057static void fts3GetMatchinfo(
178058 sqlite3_context *pCtx, /* Return results here */
178059 Fts3Cursor *pCsr, /* FTS3 Cursor object */
178060 const char *zArg /* Second argument to matchinfo() function */
178061){
178062 MatchInfo sInfo;
178063 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
178064 int rc = SQLITE_OK0;
178065 int bGlobal = 0; /* Collect 'global' stats as well as local */
178066
178067 u32 *aOut = 0;
178068 void (*xDestroyOut)(void*) = 0;
178069
178070 memset(&sInfo, 0, sizeof(MatchInfo));
178071 sInfo.pCursor = pCsr;
178072 sInfo.nCol = pTab->nColumn;
178073
178074 /* If there is cached matchinfo() data, but the format string for the
178075 ** cache does not match the format string for this request, discard
178076 ** the cached data. */
178077 if( pCsr->pMIBuffer && strcmp(pCsr->pMIBuffer->zMatchinfo, zArg) ){
178078 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
178079 pCsr->pMIBuffer = 0;
178080 }
178081
178082 /* If Fts3Cursor.pMIBuffer is NULL, then this is the first time the
178083 ** matchinfo function has been called for this query. In this case
178084 ** allocate the array used to accumulate the matchinfo data and
178085 ** initialize those elements that are constant for every row.
178086 */
178087 if( pCsr->pMIBuffer==0 ){
178088 size_t nMatchinfo = 0; /* Number of u32 elements in match-info */
178089 int i; /* Used to iterate through zArg */
178090
178091 /* Determine the number of phrases in the query */
178092 pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
178093 sInfo.nPhrase = pCsr->nPhrase;
178094
178095 /* Determine the number of integers in the buffer returned by this call. */
178096 for(i=0; zArg[i]; i++){
178097 char *zErr = 0;
178098 if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
178099 sqlite3_result_error(pCtx, zErr, -1);
178100 sqlite3_free(zErr);
178101 return;
178102 }
178103 nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
178104 }
178105
178106 /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
178107 pCsr->pMIBuffer = fts3MIBufferNew(nMatchinfo, zArg);
178108 if( !pCsr->pMIBuffer ) rc = SQLITE_NOMEM7;
178109
178110 pCsr->isMatchinfoNeeded = 1;
178111 bGlobal = 1;
178112 }
178113
178114 if( rc==SQLITE_OK0 ){
178115 xDestroyOut = fts3MIBufferAlloc(pCsr->pMIBuffer, &aOut);
178116 if( xDestroyOut==0 ){
178117 rc = SQLITE_NOMEM7;
178118 }
178119 }
178120
178121 if( rc==SQLITE_OK0 ){
178122 sInfo.aMatchinfo = aOut;
178123 sInfo.nPhrase = pCsr->nPhrase;
178124 rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
178125 if( bGlobal ){
178126 fts3MIBufferSetGlobal(pCsr->pMIBuffer);
178127 }
178128 }
178129
178130 if( rc!=SQLITE_OK0 ){
178131 sqlite3_result_error_code(pCtx, rc);
178132 if( xDestroyOut ) xDestroyOut(aOut);
178133 }else{
178134 int n = pCsr->pMIBuffer->nElem * sizeof(u32);
178135 sqlite3_result_blob(pCtx, aOut, n, xDestroyOut);
178136 }
178137}
178138
178139/*
178140** Implementation of snippet() function.
178141*/
178142SQLITE_PRIVATEstatic void sqlite3Fts3Snippet(
178143 sqlite3_context *pCtx, /* SQLite function call context */
178144 Fts3Cursor *pCsr, /* Cursor object */
178145 const char *zStart, /* Snippet start text - "<b>" */
178146 const char *zEnd, /* Snippet end text - "</b>" */
178147 const char *zEllipsis, /* Snippet ellipsis text - "<b>...</b>" */
178148 int iCol, /* Extract snippet from this column */
178149 int nToken /* Approximate number of tokens in snippet */
178150){
178151 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
178152 int rc = SQLITE_OK0;
178153 int i;
178154 StrBuffer res = {0, 0, 0};
178155
178156 /* The returned text includes up to four fragments of text extracted from
178157 ** the data in the current row. The first iteration of the for(...) loop
178158 ** below attempts to locate a single fragment of text nToken tokens in
178159 ** size that contains at least one instance of all phrases in the query
178160 ** expression that appear in the current row. If such a fragment of text
178161 ** cannot be found, the second iteration of the loop attempts to locate
178162 ** a pair of fragments, and so on.
178163 */
178164 int nSnippet = 0; /* Number of fragments in this snippet */
178165 SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */
178166 int nFToken = -1; /* Number of tokens in each fragment */
178167
178168 if( !pCsr->pExpr ){
178169 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
178170 return;
178171 }
178172
178173 /* Limit the snippet length to 64 tokens. */
178174 if( nToken<-64 ) nToken = -64;
178175 if( nToken>+64 ) nToken = +64;
178176
178177 for(nSnippet=1; 1; nSnippet++){
178178
178179 int iSnip; /* Loop counter 0..nSnippet-1 */
178180 u64 mCovered = 0; /* Bitmask of phrases covered by snippet */
178181 u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */
178182
178183 if( nToken>=0 ){
178184 nFToken = (nToken+nSnippet-1) / nSnippet;
178185 }else{
178186 nFToken = -1 * nToken;
178187 }
178188
178189 for(iSnip=0; iSnip<nSnippet; iSnip++){
178190 int iBestScore = -1; /* Best score of columns checked so far */
178191 int iRead; /* Used to iterate through columns */
178192 SnippetFragment *pFragment = &aSnippet[iSnip];
178193
178194 memset(pFragment, 0, sizeof(*pFragment));
178195
178196 /* Loop through all columns of the table being considered for snippets.
178197 ** If the iCol argument to this function was negative, this means all
178198 ** columns of the FTS3 table. Otherwise, only column iCol is considered.
178199 */
178200 for(iRead=0; iRead<pTab->nColumn; iRead++){
178201 SnippetFragment sF = {0, 0, 0, 0};
178202 int iS = 0;
178203 if( iCol>=0 && iRead!=iCol ) continue;
178204
178205 /* Find the best snippet of nFToken tokens in column iRead. */
178206 rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
178207 if( rc!=SQLITE_OK0 ){
178208 goto snippet_out;
178209 }
178210 if( iS>iBestScore ){
178211 *pFragment = sF;
178212 iBestScore = iS;
178213 }
178214 }
178215
178216 mCovered |= pFragment->covered;
178217 }
178218
178219 /* If all query phrases seen by fts3BestSnippet() are present in at least
178220 ** one of the nSnippet snippet fragments, break out of the loop.
178221 */
178222 assert( (mCovered&mSeen)==mCovered )((void) (0));
178223 if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
178224 }
178225
178226 assert( nFToken>0 )((void) (0));
178227
178228 for(i=0; i<nSnippet && rc==SQLITE_OK0; i++){
178229 rc = fts3SnippetText(pCsr, &aSnippet[i],
178230 i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
178231 );
178232 }
178233
178234 snippet_out:
178235 sqlite3Fts3SegmentsClose(pTab);
178236 if( rc!=SQLITE_OK0 ){
178237 sqlite3_result_error_code(pCtx, rc);
178238 sqlite3_free(res.z);
178239 }else{
178240 sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
178241 }
178242}
178243
178244
178245typedef struct TermOffset TermOffset;
178246typedef struct TermOffsetCtx TermOffsetCtx;
178247
178248struct TermOffset {
178249 char *pList; /* Position-list */
178250 int iPos; /* Position just read from pList */
178251 int iOff; /* Offset of this term from read positions */
178252};
178253
178254struct TermOffsetCtx {
178255 Fts3Cursor *pCsr;
178256 int iCol; /* Column of table to populate aTerm for */
178257 int iTerm;
178258 sqlite3_int64 iDocid;
178259 TermOffset *aTerm;
178260};
178261
178262/*
178263** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
178264*/
178265static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
178266 TermOffsetCtx *p = (TermOffsetCtx *)ctx;
178267 int nTerm; /* Number of tokens in phrase */
178268 int iTerm; /* For looping through nTerm phrase terms */
178269 char *pList; /* Pointer to position list for phrase */
178270 int iPos = 0; /* First position in position-list */
178271 int rc;
178272
178273 UNUSED_PARAMETER(iPhrase)(void)(iPhrase);
178274 rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
178275 nTerm = pExpr->pPhrase->nToken;
178276 if( pList ){
178277 fts3GetDeltaPosition(&pList, &iPos);
178278 assert_fts3_nc( iPos>=0 );
178279 }
178280
178281 for(iTerm=0; iTerm<nTerm; iTerm++){
178282 TermOffset *pT = &p->aTerm[p->iTerm++];
178283 pT->iOff = nTerm-iTerm-1;
178284 pT->pList = pList;
178285 pT->iPos = iPos;
178286 }
178287
178288 return rc;
178289}
178290
178291/*
178292** Implementation of offsets() function.
178293*/
178294SQLITE_PRIVATEstatic void sqlite3Fts3Offsets(
178295 sqlite3_context *pCtx, /* SQLite function call context */
178296 Fts3Cursor *pCsr /* Cursor object */
178297){
178298 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
178299 sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
178300 int rc; /* Return Code */
178301 int nToken; /* Number of tokens in query */
178302 int iCol; /* Column currently being processed */
178303 StrBuffer res = {0, 0, 0}; /* Result string */
178304 TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */
178305
178306 if( !pCsr->pExpr ){
178307 sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
178308 return;
178309 }
178310
178311 memset(&sCtx, 0, sizeof(sCtx));
178312 assert( pCsr->isRequireSeek==0 )((void) (0));
178313
178314 /* Count the number of terms in the query */
178315 rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
178316 if( rc!=SQLITE_OK0 ) goto offsets_out;
178317
178318 /* Allocate the array of TermOffset iterators. */
178319 sCtx.aTerm = (TermOffset *)sqlite3_malloc64(sizeof(TermOffset)*nToken);
178320 if( 0==sCtx.aTerm ){
178321 rc = SQLITE_NOMEM7;
178322 goto offsets_out;
178323 }
178324 sCtx.iDocid = pCsr->iPrevId;
178325 sCtx.pCsr = pCsr;
178326
178327 /* Loop through the table columns, appending offset information to
178328 ** string-buffer res for each column.
178329 */
178330 for(iCol=0; iCol<pTab->nColumn; iCol++){
178331 sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
178332 const char *ZDUMMY; /* Dummy argument used with xNext() */
178333 int NDUMMY = 0; /* Dummy argument used with xNext() */
178334 int iStart = 0;
178335 int iEnd = 0;
178336 int iCurrent = 0;
178337 const char *zDoc;
178338 int nDoc;
178339
178340 /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
178341 ** no way that this operation can fail, so the return code from
178342 ** fts3ExprIterate() can be discarded.
178343 */
178344 sCtx.iCol = iCol;
178345 sCtx.iTerm = 0;
178346 (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void*)&sCtx);
178347
178348 /* Retreive the text stored in column iCol. If an SQL NULL is stored
178349 ** in column iCol, jump immediately to the next iteration of the loop.
178350 ** If an OOM occurs while retrieving the data (this can happen if SQLite
178351 ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
178352 ** to the caller.
178353 */
178354 zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
178355 nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
178356 if( zDoc==0 ){
178357 if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL5 ){
178358 continue;
178359 }
178360 rc = SQLITE_NOMEM7;
178361 goto offsets_out;
178362 }
178363
178364 /* Initialize a tokenizer iterator to iterate through column iCol. */
178365 rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
178366 zDoc, nDoc, &pC
178367 );
178368 if( rc!=SQLITE_OK0 ) goto offsets_out;
178369
178370 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
178371 while( rc==SQLITE_OK0 ){
178372 int i; /* Used to loop through terms */
178373 int iMinPos = 0x7FFFFFFF; /* Position of next token */
178374 TermOffset *pTerm = 0; /* TermOffset associated with next token */
178375
178376 for(i=0; i<nToken; i++){
178377 TermOffset *pT = &sCtx.aTerm[i];
178378 if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
178379 iMinPos = pT->iPos-pT->iOff;
178380 pTerm = pT;
178381 }
178382 }
178383
178384 if( !pTerm ){
178385 /* All offsets for this column have been gathered. */
178386 rc = SQLITE_DONE101;
178387 }else{
178388 assert_fts3_nc( iCurrent<=iMinPos );
178389 if( 0==(0xFE&*pTerm->pList) ){
178390 pTerm->pList = 0;
178391 }else{
178392 fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
178393 }
178394 while( rc==SQLITE_OK0 && iCurrent<iMinPos ){
178395 rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
178396 }
178397 if( rc==SQLITE_OK0 ){
178398 char aBuffer[64];
178399 sqlite3_snprintf(sizeof(aBuffer), aBuffer,
178400 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
178401 );
178402 rc = fts3StringAppend(&res, aBuffer, -1);
178403 }else if( rc==SQLITE_DONE101 && pTab->zContentTbl==0 ){
178404 rc = FTS_CORRUPT_VTAB;
178405 }
178406 }
178407 }
178408 if( rc==SQLITE_DONE101 ){
178409 rc = SQLITE_OK0;
178410 }
178411
178412 pMod->xClose(pC);
178413 if( rc!=SQLITE_OK0 ) goto offsets_out;
178414 }
178415
178416 offsets_out:
178417 sqlite3_free(sCtx.aTerm);
178418 assert( rc!=SQLITE_DONE )((void) (0));
178419 sqlite3Fts3SegmentsClose(pTab);
178420 if( rc!=SQLITE_OK0 ){
178421 sqlite3_result_error_code(pCtx, rc);
178422 sqlite3_free(res.z);
178423 }else{
178424 sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
178425 }
178426 return;
178427}
178428
178429/*
178430** Implementation of matchinfo() function.
178431*/
178432SQLITE_PRIVATEstatic void sqlite3Fts3Matchinfo(
178433 sqlite3_context *pContext, /* Function call context */
178434 Fts3Cursor *pCsr, /* FTS3 table cursor */
178435 const char *zArg /* Second arg to matchinfo() function */
178436){
178437 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
178438 const char *zFormat;
178439
178440 if( zArg ){
178441 zFormat = zArg;
178442 }else{
178443 zFormat = FTS3_MATCHINFO_DEFAULT;
178444 }
178445
178446 if( !pCsr->pExpr ){
178447 sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
178448 return;
178449 }else{
178450 /* Retrieve matchinfo() data. */
178451 fts3GetMatchinfo(pContext, pCsr, zFormat);
178452 sqlite3Fts3SegmentsClose(pTab);
178453 }
178454}
178455
178456#endif
178457
178458/************** End of fts3_snippet.c ****************************************/
178459/************** Begin file fts3_unicode.c ************************************/
178460/*
178461** 2012 May 24
178462**
178463** The author disclaims copyright to this source code. In place of
178464** a legal notice, here is a blessing:
178465**
178466** May you do good and not evil.
178467** May you find forgiveness for yourself and forgive others.
178468** May you share freely, never taking more than you give.
178469**
178470******************************************************************************
178471**
178472** Implementation of the "unicode" full-text-search tokenizer.
178473*/
178474
178475#ifndef SQLITE_DISABLE_FTS3_UNICODE
178476
178477/* #include "fts3Int.h" */
178478#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
178479
178480/* #include <assert.h> */
178481/* #include <stdlib.h> */
178482/* #include <stdio.h> */
178483/* #include <string.h> */
178484
178485/* #include "fts3_tokenizer.h" */
178486
178487/*
178488** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
178489** from the sqlite3 source file utf.c. If this file is compiled as part
178490** of the amalgamation, they are not required.
178491*/
178492#ifndef SQLITE_AMALGAMATION1
178493
178494static const unsigned char sqlite3Utf8Trans1[] = {
178495 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
178496 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
178497 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
178498 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
178499 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
178500 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
178501 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
178502 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
178503};
178504
178505#define READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ c =
(c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (c&
0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD
; } }
\
178506 c = *(zIn++); \
178507 if( c>=0xc0 ){ \
178508 c = sqlite3Utf8Trans1[c-0xc0]; \
178509 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
178510 c = (c<<6) + (0x3f & *(zIn++)); \
178511 } \
178512 if( c<0x80 \
178513 || (c&0xFFFFF800)==0xD800 \
178514 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
178515 }
178516
178517#define WRITE_UTF8(zOut, c){ if( c<0x00080 ){ *zOut++ = (u8)(c&0xFF); } else if( c
<0x00800 ){ *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); *
zOut++ = 0x80 + (u8)(c & 0x3F); } else if( c<0x10000 )
{ *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); *zOut++ = 0x80
+ (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(c &
0x3F); }else{ *zOut++ = 0xF0 + (u8)((c>>18) & 0x07
); *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); *zOut++ =
0x80 + (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(
c & 0x3F); } }
{ \
178518 if( c<0x00080 ){ \
178519 *zOut++ = (u8)(c&0xFF); \
178520 } \
178521 else if( c<0x00800 ){ \
178522 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
178523 *zOut++ = 0x80 + (u8)(c & 0x3F); \
178524 } \
178525 else if( c<0x10000 ){ \
178526 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
178527 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
178528 *zOut++ = 0x80 + (u8)(c & 0x3F); \
178529 }else{ \
178530 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
178531 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
178532 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
178533 *zOut++ = 0x80 + (u8)(c & 0x3F); \
178534 } \
178535}
178536
178537#endif /* ifndef SQLITE_AMALGAMATION */
178538
178539typedef struct unicode_tokenizer unicode_tokenizer;
178540typedef struct unicode_cursor unicode_cursor;
178541
178542struct unicode_tokenizer {
178543 sqlite3_tokenizer base;
178544 int eRemoveDiacritic;
178545 int nException;
178546 int *aiException;
178547};
178548
178549struct unicode_cursor {
178550 sqlite3_tokenizer_cursor base;
178551 const unsigned char *aInput; /* Input text being tokenized */
178552 int nInput; /* Size of aInput[] in bytes */
178553 int iOff; /* Current offset within aInput[] */
178554 int iToken; /* Index of next token to be returned */
178555 char *zToken; /* storage for current token */
178556 int nAlloc; /* space allocated at zToken */
178557};
178558
178559
178560/*
178561** Destroy a tokenizer allocated by unicodeCreate().
178562*/
178563static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
178564 if( pTokenizer ){
178565 unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
178566 sqlite3_free(p->aiException);
178567 sqlite3_free(p);
178568 }
178569 return SQLITE_OK0;
178570}
178571
178572/*
178573** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
178574** statement has specified that the tokenizer for this table shall consider
178575** all characters in string zIn/nIn to be separators (if bAlnum==0) or
178576** token characters (if bAlnum==1).
178577**
178578** For each codepoint in the zIn/nIn string, this function checks if the
178579** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
178580** If so, no action is taken. Otherwise, the codepoint is added to the
178581** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
178582** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
178583** codepoints in the aiException[] array.
178584**
178585** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
178586** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
178587** It is not possible to change the behavior of the tokenizer with respect
178588** to these codepoints.
178589*/
178590static int unicodeAddExceptions(
178591 unicode_tokenizer *p, /* Tokenizer to add exceptions to */
178592 int bAlnum, /* Replace Isalnum() return value with this */
178593 const char *zIn, /* Array of characters to make exceptions */
178594 int nIn /* Length of z in bytes */
178595){
178596 const unsigned char *z = (const unsigned char *)zIn;
178597 const unsigned char *zTerm = &z[nIn];
178598 unsigned int iCode;
178599 int nEntry = 0;
178600
178601 assert( bAlnum==0 || bAlnum==1 )((void) (0));
178602
178603 while( z<zTerm ){
178604 READ_UTF8(z, zTerm, iCode)iCode = *(z++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( z!=zTerm && (*z & 0xc0)==0x80
){ iCode = (iCode<<6) + (0x3f & *(z++)); } if( iCode
<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode&0xFFFFFFFE
)==0xFFFE ){ iCode = 0xFFFD; } }
;
178605 assert( (sqlite3FtsUnicodeIsalnum((int)iCode) & 0xFFFFFFFE)==0 )((void) (0));
178606 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
178607 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
178608 ){
178609 nEntry++;
178610 }
178611 }
178612
178613 if( nEntry ){
178614 int *aNew; /* New aiException[] array */
178615 int nNew; /* Number of valid entries in array aNew[] */
178616
178617 aNew = sqlite3_realloc64(p->aiException,(p->nException+nEntry)*sizeof(int));
178618 if( aNew==0 ) return SQLITE_NOMEM7;
178619 nNew = p->nException;
178620
178621 z = (const unsigned char *)zIn;
178622 while( z<zTerm ){
178623 READ_UTF8(z, zTerm, iCode)iCode = *(z++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( z!=zTerm && (*z & 0xc0)==0x80
){ iCode = (iCode<<6) + (0x3f & *(z++)); } if( iCode
<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode&0xFFFFFFFE
)==0xFFFE ){ iCode = 0xFFFD; } }
;
178624 if( sqlite3FtsUnicodeIsalnum((int)iCode)!=bAlnum
178625 && sqlite3FtsUnicodeIsdiacritic((int)iCode)==0
178626 ){
178627 int i, j;
178628 for(i=0; i<nNew && aNew[i]<(int)iCode; i++);
178629 for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
178630 aNew[i] = (int)iCode;
178631 nNew++;
178632 }
178633 }
178634 p->aiException = aNew;
178635 p->nException = nNew;
178636 }
178637
178638 return SQLITE_OK0;
178639}
178640
178641/*
178642** Return true if the p->aiException[] array contains the value iCode.
178643*/
178644static int unicodeIsException(unicode_tokenizer *p, int iCode){
178645 if( p->nException>0 ){
178646 int *a = p->aiException;
178647 int iLo = 0;
178648 int iHi = p->nException-1;
178649
178650 while( iHi>=iLo ){
178651 int iTest = (iHi + iLo) / 2;
178652 if( iCode==a[iTest] ){
178653 return 1;
178654 }else if( iCode>a[iTest] ){
178655 iLo = iTest+1;
178656 }else{
178657 iHi = iTest-1;
178658 }
178659 }
178660 }
178661
178662 return 0;
178663}
178664
178665/*
178666** Return true if, for the purposes of tokenization, codepoint iCode is
178667** considered a token character (not a separator).
178668*/
178669static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
178670 assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 )((void) (0));
178671 return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
178672}
178673
178674/*
178675** Create a new tokenizer instance.
178676*/
178677static int unicodeCreate(
178678 int nArg, /* Size of array argv[] */
178679 const char * const *azArg, /* Tokenizer creation arguments */
178680 sqlite3_tokenizer **pp /* OUT: New tokenizer handle */
178681){
178682 unicode_tokenizer *pNew; /* New tokenizer object */
178683 int i;
178684 int rc = SQLITE_OK0;
178685
178686 pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
178687 if( pNew==NULL((void*)0) ) return SQLITE_NOMEM7;
178688 memset(pNew, 0, sizeof(unicode_tokenizer));
178689 pNew->eRemoveDiacritic = 1;
178690
178691 for(i=0; rc==SQLITE_OK0 && i<nArg; i++){
178692 const char *z = azArg[i];
178693 int n = (int)strlen(z);
178694
178695 if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
178696 pNew->eRemoveDiacritic = 1;
178697 }
178698 else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
178699 pNew->eRemoveDiacritic = 0;
178700 }
178701 else if( n==19 && memcmp("remove_diacritics=2", z, 19)==0 ){
178702 pNew->eRemoveDiacritic = 2;
178703 }
178704 else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
178705 rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
178706 }
178707 else if( n>=11 && memcmp("separators=", z, 11)==0 ){
178708 rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
178709 }
178710 else{
178711 /* Unrecognized argument */
178712 rc = SQLITE_ERROR1;
178713 }
178714 }
178715
178716 if( rc!=SQLITE_OK0 ){
178717 unicodeDestroy((sqlite3_tokenizer *)pNew);
178718 pNew = 0;
178719 }
178720 *pp = (sqlite3_tokenizer *)pNew;
178721 return rc;
178722}
178723
178724/*
178725** Prepare to begin tokenizing a particular string. The input
178726** string to be tokenized is pInput[0..nBytes-1]. A cursor
178727** used to incrementally tokenize this string is returned in
178728** *ppCursor.
178729*/
178730static int unicodeOpen(
178731 sqlite3_tokenizer *p, /* The tokenizer */
178732 const char *aInput, /* Input string */
178733 int nInput, /* Size of string aInput in bytes */
178734 sqlite3_tokenizer_cursor **pp /* OUT: New cursor object */
178735){
178736 unicode_cursor *pCsr;
178737
178738 pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
178739 if( pCsr==0 ){
178740 return SQLITE_NOMEM7;
178741 }
178742 memset(pCsr, 0, sizeof(unicode_cursor));
178743
178744 pCsr->aInput = (const unsigned char *)aInput;
178745 if( aInput==0 ){
178746 pCsr->nInput = 0;
178747 }else if( nInput<0 ){
178748 pCsr->nInput = (int)strlen(aInput);
178749 }else{
178750 pCsr->nInput = nInput;
178751 }
178752
178753 *pp = &pCsr->base;
178754 UNUSED_PARAMETER(p)(void)(p);
178755 return SQLITE_OK0;
178756}
178757
178758/*
178759** Close a tokenization cursor previously opened by a call to
178760** simpleOpen() above.
178761*/
178762static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
178763 unicode_cursor *pCsr = (unicode_cursor *) pCursor;
178764 sqlite3_free(pCsr->zToken);
178765 sqlite3_free(pCsr);
178766 return SQLITE_OK0;
178767}
178768
178769/*
178770** Extract the next token from a tokenization cursor. The cursor must
178771** have been opened by a prior call to simpleOpen().
178772*/
178773static int unicodeNext(
178774 sqlite3_tokenizer_cursor *pC, /* Cursor returned by simpleOpen */
178775 const char **paToken, /* OUT: Token text */
178776 int *pnToken, /* OUT: Number of bytes at *paToken */
178777 int *piStart, /* OUT: Starting offset of token */
178778 int *piEnd, /* OUT: Ending offset of token */
178779 int *piPos /* OUT: Position integer of token */
178780){
178781 unicode_cursor *pCsr = (unicode_cursor *)pC;
178782 unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
178783 unsigned int iCode = 0;
178784 char *zOut;
178785 const unsigned char *z = &pCsr->aInput[pCsr->iOff];
178786 const unsigned char *zStart = z;
178787 const unsigned char *zEnd;
178788 const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
178789
178790 /* Scan past any delimiter characters before the start of the next token.
178791 ** Return SQLITE_DONE early if this takes us all the way to the end of
178792 ** the input. */
178793 while( z<zTerm ){
178794 READ_UTF8(z, zTerm, iCode)iCode = *(z++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( z!=zTerm && (*z & 0xc0)==0x80
){ iCode = (iCode<<6) + (0x3f & *(z++)); } if( iCode
<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode&0xFFFFFFFE
)==0xFFFE ){ iCode = 0xFFFD; } }
;
178795 if( unicodeIsAlnum(p, (int)iCode) ) break;
178796 zStart = z;
178797 }
178798 if( zStart>=zTerm ) return SQLITE_DONE101;
178799
178800 zOut = pCsr->zToken;
178801 do {
178802 int iOut;
178803
178804 /* Grow the output buffer if required. */
178805 if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
178806 char *zNew = sqlite3_realloc64(pCsr->zToken, pCsr->nAlloc+64);
178807 if( !zNew ) return SQLITE_NOMEM7;
178808 zOut = &zNew[zOut - pCsr->zToken];
178809 pCsr->zToken = zNew;
178810 pCsr->nAlloc += 64;
178811 }
178812
178813 /* Write the folded case of the last character read to the output */
178814 zEnd = z;
178815 iOut = sqlite3FtsUnicodeFold((int)iCode, p->eRemoveDiacritic);
178816 if( iOut ){
178817 WRITE_UTF8(zOut, iOut){ if( iOut<0x00080 ){ *zOut++ = (u8)(iOut&0xFF); } else
if( iOut<0x00800 ){ *zOut++ = 0xC0 + (u8)((iOut>>6)
&0x1F); *zOut++ = 0x80 + (u8)(iOut & 0x3F); } else if
( iOut<0x10000 ){ *zOut++ = 0xE0 + (u8)((iOut>>12)&
0x0F); *zOut++ = 0x80 + (u8)((iOut>>6) & 0x3F); *zOut
++ = 0x80 + (u8)(iOut & 0x3F); }else{ *zOut++ = 0xF0 + (u8
)((iOut>>18) & 0x07); *zOut++ = 0x80 + (u8)((iOut>>
12) & 0x3F); *zOut++ = 0x80 + (u8)((iOut>>6) & 0x3F
); *zOut++ = 0x80 + (u8)(iOut & 0x3F); } }
;
178818 }
178819
178820 /* If the cursor is not at EOF, read the next character */
178821 if( z>=zTerm ) break;
178822 READ_UTF8(z, zTerm, iCode)iCode = *(z++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( z!=zTerm && (*z & 0xc0)==0x80
){ iCode = (iCode<<6) + (0x3f & *(z++)); } if( iCode
<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode&0xFFFFFFFE
)==0xFFFE ){ iCode = 0xFFFD; } }
;
178823 }while( unicodeIsAlnum(p, (int)iCode)
178824 || sqlite3FtsUnicodeIsdiacritic((int)iCode)
178825 );
178826
178827 /* Set the output variables and return. */
178828 pCsr->iOff = (int)(z - pCsr->aInput);
178829 *paToken = pCsr->zToken;
178830 *pnToken = (int)(zOut - pCsr->zToken);
178831 *piStart = (int)(zStart - pCsr->aInput);
178832 *piEnd = (int)(zEnd - pCsr->aInput);
178833 *piPos = pCsr->iToken++;
178834 return SQLITE_OK0;
178835}
178836
178837/*
178838** Set *ppModule to a pointer to the sqlite3_tokenizer_module
178839** structure for the unicode tokenizer.
178840*/
178841SQLITE_PRIVATEstatic void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
178842 static const sqlite3_tokenizer_module module = {
178843 0,
178844 unicodeCreate,
178845 unicodeDestroy,
178846 unicodeOpen,
178847 unicodeClose,
178848 unicodeNext,
178849 0,
178850 };
178851 *ppModule = &module;
178852}
178853
178854#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
178855#endif /* ifndef SQLITE_DISABLE_FTS3_UNICODE */
178856
178857/************** End of fts3_unicode.c ****************************************/
178858/************** Begin file fts3_unicode2.c ***********************************/
178859/*
178860** 2012-05-25
178861**
178862** The author disclaims copyright to this source code. In place of
178863** a legal notice, here is a blessing:
178864**
178865** May you do good and not evil.
178866** May you find forgiveness for yourself and forgive others.
178867** May you share freely, never taking more than you give.
178868**
178869******************************************************************************
178870*/
178871
178872/*
178873** DO NOT EDIT THIS MACHINE GENERATED FILE.
178874*/
178875
178876#ifndef SQLITE_DISABLE_FTS3_UNICODE
178877#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
178878
178879/* #include <assert.h> */
178880
178881/*
178882** Return true if the argument corresponds to a unicode codepoint
178883** classified as either a letter or a number. Otherwise false.
178884**
178885** The results are undefined if the value passed to this function
178886** is less than zero.
178887*/
178888SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsalnum(int c){
178889 /* Each unsigned integer in the following array corresponds to a contiguous
178890 ** range of unicode codepoints that are not either letters or numbers (i.e.
178891 ** codepoints for which this function should return 0).
178892 **
178893 ** The most significant 22 bits in each 32-bit value contain the first
178894 ** codepoint in the range. The least significant 10 bits are used to store
178895 ** the size of the range (always at least 1). In other words, the value
178896 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
178897 ** C. It is not possible to represent a range larger than 1023 codepoints
178898 ** using this format.
178899 */
178900 static const unsigned int aEntry[] = {
178901 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
178902 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
178903 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
178904 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
178905 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
178906 0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
178907 0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
178908 0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
178909 0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
178910 0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
178911 0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
178912 0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
178913 0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
178914 0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
178915 0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
178916 0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
178917 0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
178918 0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
178919 0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
178920 0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
178921 0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
178922 0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
178923 0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
178924 0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
178925 0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
178926 0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
178927 0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
178928 0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
178929 0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
178930 0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
178931 0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
178932 0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
178933 0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
178934 0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
178935 0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
178936 0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
178937 0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
178938 0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
178939 0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
178940 0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
178941 0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
178942 0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
178943 0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
178944 0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
178945 0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
178946 0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
178947 0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
178948 0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
178949 0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
178950 0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
178951 0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
178952 0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
178953 0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
178954 0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
178955 0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
178956 0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
178957 0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
178958 0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
178959 0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
178960 0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
178961 0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
178962 0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
178963 0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
178964 0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
178965 0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
178966 0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
178967 0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
178968 0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
178969 0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
178970 0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
178971 0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
178972 0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
178973 0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
178974 0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
178975 0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
178976 0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
178977 0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
178978 0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
178979 0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
178980 0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
178981 0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
178982 0x380400F0,
178983 };
178984 static const unsigned int aAscii[4] = {
178985 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
178986 };
178987
178988 if( (unsigned int)c<128 ){
178989 return ( (aAscii[c >> 5] & ((unsigned int)1 << (c & 0x001F)))==0 );
178990 }else if( (unsigned int)c<(1<<22) ){
178991 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
178992 int iRes = 0;
178993 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
178994 int iLo = 0;
178995 while( iHi>=iLo ){
178996 int iTest = (iHi + iLo) / 2;
178997 if( key >= aEntry[iTest] ){
178998 iRes = iTest;
178999 iLo = iTest+1;
179000 }else{
179001 iHi = iTest-1;
179002 }
179003 }
179004 assert( aEntry[0]<key )((void) (0));
179005 assert( key>=aEntry[iRes] )((void) (0));
179006 return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
179007 }
179008 return 1;
179009}
179010
179011
179012/*
179013** If the argument is a codepoint corresponding to a lowercase letter
179014** in the ASCII range with a diacritic added, return the codepoint
179015** of the ASCII letter only. For example, if passed 235 - "LATIN
179016** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
179017** E"). The resuls of passing a codepoint that corresponds to an
179018** uppercase letter are undefined.
179019*/
179020static int remove_diacritic(int c, int bComplex){
179021 unsigned short aDia[] = {
179022 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
179023 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
179024 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
179025 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
179026 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
179027 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
179028 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
179029 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
179030 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
179031 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
179032 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
179033 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
179034 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
179035 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
179036 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
179037 63182, 63242, 63274, 63310, 63368, 63390,
179038 };
179039#define HIBIT ((unsigned char)0x80)
179040 unsigned char aChar[] = {
179041 '\0', 'a', 'c', 'e', 'i', 'n',
179042 'o', 'u', 'y', 'y', 'a', 'c',
179043 'd', 'e', 'e', 'g', 'h', 'i',
179044 'j', 'k', 'l', 'n', 'o', 'r',
179045 's', 't', 'u', 'u', 'w', 'y',
179046 'z', 'o', 'u', 'a', 'i', 'o',
179047 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o',
179048 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a',
179049 'e', 'i', 'o', 'r', 'u', 's',
179050 't', 'h', 'a', 'e', 'o'|HIBIT, 'o',
179051 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0',
179052 '\0', '\0', '\0', '\0', 'a', 'b',
179053 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT,
179054 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT,
179055 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n',
179056 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's',
179057 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w',
179058 'w', 'x', 'y', 'z', 'h', 't',
179059 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
179060 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT,
179061 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y',
179062 };
179063
179064 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
179065 int iRes = 0;
179066 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
179067 int iLo = 0;
179068 while( iHi>=iLo ){
179069 int iTest = (iHi + iLo) / 2;
179070 if( key >= aDia[iTest] ){
179071 iRes = iTest;
179072 iLo = iTest+1;
179073 }else{
179074 iHi = iTest-1;
179075 }
179076 }
179077 assert( key>=aDia[iRes] )((void) (0));
179078 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
179079 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
179080}
179081
179082
179083/*
179084** Return true if the argument interpreted as a unicode codepoint
179085** is a diacritical modifier character.
179086*/
179087SQLITE_PRIVATEstatic int sqlite3FtsUnicodeIsdiacritic(int c){
179088 unsigned int mask0 = 0x08029FDF;
179089 unsigned int mask1 = 0x000361F8;
179090 if( c<768 || c>817 ) return 0;
179091 return (c < 768+32) ?
179092 (mask0 & ((unsigned int)1 << (c-768))) :
179093 (mask1 & ((unsigned int)1 << (c-768-32)));
179094}
179095
179096
179097/*
179098** Interpret the argument as a unicode codepoint. If the codepoint
179099** is an upper case character that has a lower case equivalent,
179100** return the codepoint corresponding to the lower case version.
179101** Otherwise, return a copy of the argument.
179102**
179103** The results are undefined if the value passed to this function
179104** is less than zero.
179105*/
179106SQLITE_PRIVATEstatic int sqlite3FtsUnicodeFold(int c, int eRemoveDiacritic){
179107 /* Each entry in the following array defines a rule for folding a range
179108 ** of codepoints to lower case. The rule applies to a range of nRange
179109 ** codepoints starting at codepoint iCode.
179110 **
179111 ** If the least significant bit in flags is clear, then the rule applies
179112 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
179113 ** need to be folded). Or, if it is set, then the rule only applies to
179114 ** every second codepoint in the range, starting with codepoint C.
179115 **
179116 ** The 7 most significant bits in flags are an index into the aiOff[]
179117 ** array. If a specific codepoint C does require folding, then its lower
179118 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
179119 **
179120 ** The contents of this array are generated by parsing the CaseFolding.txt
179121 ** file distributed as part of the "Unicode Character Database". See
179122 ** http://www.unicode.org for details.
179123 */
179124 static const struct TableEntry {
179125 unsigned short iCode;
179126 unsigned char flags;
179127 unsigned char nRange;
179128 } aEntry[] = {
179129 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
179130 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
179131 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
179132 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
179133 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
179134 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
179135 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
179136 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
179137 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
179138 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
179139 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
179140 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
179141 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
179142 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
179143 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
179144 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
179145 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
179146 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
179147 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
179148 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
179149 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
179150 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
179151 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
179152 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
179153 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
179154 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
179155 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
179156 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
179157 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
179158 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
179159 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
179160 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
179161 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
179162 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
179163 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
179164 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
179165 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
179166 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
179167 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
179168 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
179169 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
179170 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
179171 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
179172 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
179173 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
179174 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
179175 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
179176 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
179177 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
179178 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
179179 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
179180 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
179181 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
179182 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
179183 {65313, 14, 26},
179184 };
179185 static const unsigned short aiOff[] = {
179186 1, 2, 8, 15, 16, 26, 28, 32,
179187 37, 38, 40, 48, 63, 64, 69, 71,
179188 79, 80, 116, 202, 203, 205, 206, 207,
179189 209, 210, 211, 213, 214, 217, 218, 219,
179190 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
179191 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
179192 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
179193 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
179194 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
179195 65514, 65521, 65527, 65528, 65529,
179196 };
179197
179198 int ret = c;
179199
179200 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 )((void) (0));
179201
179202 if( c<128 ){
179203 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
179204 }else if( c<65536 ){
179205 const struct TableEntry *p;
179206 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
179207 int iLo = 0;
179208 int iRes = -1;
179209
179210 assert( c>aEntry[0].iCode )((void) (0));
179211 while( iHi>=iLo ){
179212 int iTest = (iHi + iLo) / 2;
179213 int cmp = (c - aEntry[iTest].iCode);
179214 if( cmp>=0 ){
179215 iRes = iTest;
179216 iLo = iTest+1;
179217 }else{
179218 iHi = iTest-1;
179219 }
179220 }
179221
179222 assert( iRes>=0 && c>=aEntry[iRes].iCode )((void) (0));
179223 p = &aEntry[iRes];
179224 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
179225 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
179226 assert( ret>0 )((void) (0));
179227 }
179228
179229 if( eRemoveDiacritic ){
179230 ret = remove_diacritic(ret, eRemoveDiacritic==2);
179231 }
179232 }
179233
179234 else if( c>=66560 && c<66600 ){
179235 ret = c + 40;
179236 }
179237
179238 return ret;
179239}
179240#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
179241#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */
179242
179243/************** End of fts3_unicode2.c ***************************************/
179244/************** Begin file json1.c *******************************************/
179245/*
179246** 2015-08-12
179247**
179248** The author disclaims copyright to this source code. In place of
179249** a legal notice, here is a blessing:
179250**
179251** May you do good and not evil.
179252** May you find forgiveness for yourself and forgive others.
179253** May you share freely, never taking more than you give.
179254**
179255******************************************************************************
179256**
179257** This SQLite extension implements JSON functions. The interface is
179258** modeled after MySQL JSON functions:
179259**
179260** https://dev.mysql.com/doc/refman/5.7/en/json.html
179261**
179262** For the time being, all JSON is stored as pure text. (We might add
179263** a JSONB type in the future which stores a binary encoding of JSON in
179264** a BLOB, but there is no support for JSONB in the current implementation.
179265** This implementation parses JSON text at 250 MB/s, so it is hard to see
179266** how JSONB might improve on that.)
179267*/
179268#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_JSON1)
179269#if !defined(SQLITEINT_H)
179270/* #include "sqlite3ext.h" */
179271#endif
179272SQLITE_EXTENSION_INIT1
179273/* #include <assert.h> */
179274/* #include <string.h> */
179275/* #include <stdlib.h> */
179276/* #include <stdarg.h> */
179277
179278/* Mark a function parameter as unused, to suppress nuisance compiler
179279** warnings. */
179280#ifndef UNUSED_PARAM
179281# define UNUSED_PARAM(X) (void)(X)
179282#endif
179283
179284#ifndef LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))
179285# define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
179286# define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((sqlite3_int64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
179287#endif
179288
179289/*
179290** Versions of isspace(), isalnum() and isdigit() to which it is safe
179291** to pass signed char values.
179292*/
179293#ifdef sqlite3Isdigit
179294 /* Use the SQLite core versions if this routine is part of the
179295 ** SQLite amalgamation */
179296# define safe_isdigit(x) sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04)
179297# define safe_isalnum(x) sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06)
179298# define safe_isxdigit(x) sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08)
179299#else
179300 /* Use the standard library for separate compilation */
179301#include <ctype.h> /* amalgamator: keep */
179302# define safe_isdigit(x) isdigit((unsigned char)(x))
179303# define safe_isalnum(x) isalnum((unsigned char)(x))
179304# define safe_isxdigit(x) isxdigit((unsigned char)(x))
179305#endif
179306
179307/*
179308** Growing our own isspace() routine this way is twice as fast as
179309** the library isspace() function, resulting in a 7% overall performance
179310** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
179311*/
179312static const char jsonIsSpace[] = {
179313 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
179314 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179315 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179316 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179317 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179318 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179319 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179320 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179321 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179322 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179323 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179324 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179325 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179326 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179327 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179328 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179329};
179330#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
179331
179332#ifndef SQLITE_AMALGAMATION1
179333 /* Unsigned integer types. These are already defined in the sqliteInt.h,
179334 ** but the definitions need to be repeated for separate compilation. */
179335 typedef sqlite3_uint64 u64;
179336 typedef unsigned int u32;
179337 typedef unsigned short int u16;
179338 typedef unsigned char u8;
179339#endif
179340
179341/* Objects */
179342typedef struct JsonString JsonString;
179343typedef struct JsonNode JsonNode;
179344typedef struct JsonParse JsonParse;
179345
179346/* An instance of this object represents a JSON string
179347** under construction. Really, this is a generic string accumulator
179348** that can be and is used to create strings other than JSON.
179349*/
179350struct JsonString {
179351 sqlite3_context *pCtx; /* Function context - put error messages here */
179352 char *zBuf; /* Append JSON content here */
179353 u64 nAlloc; /* Bytes of storage available in zBuf[] */
179354 u64 nUsed; /* Bytes of zBuf[] currently used */
179355 u8 bStatic; /* True if zBuf is static space */
179356 u8 bErr; /* True if an error has been encountered */
179357 char zSpace[100]; /* Initial static space */
179358};
179359
179360/* JSON type values
179361*/
179362#define JSON_NULL 0
179363#define JSON_TRUE 1
179364#define JSON_FALSE 2
179365#define JSON_INT 3
179366#define JSON_REAL 4
179367#define JSON_STRING 5
179368#define JSON_ARRAY 6
179369#define JSON_OBJECT 7
179370
179371/* The "subtype" set for JSON values */
179372#define JSON_SUBTYPE 74 /* Ascii for "J" */
179373
179374/*
179375** Names of the various JSON types:
179376*/
179377static const char * const jsonType[] = {
179378 "null", "true", "false", "integer", "real", "text", "array", "object"
179379};
179380
179381/* Bit values for the JsonNode.jnFlag field
179382*/
179383#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
179384#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
179385#define JNODE_REMOVE 0x04 /* Do not output */
179386#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
179387#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
179388#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
179389#define JNODE_LABEL 0x40 /* Is a label of an object */
179390
179391
179392/* A single node of parsed JSON
179393*/
179394struct JsonNode {
179395 u8 eType; /* One of the JSON_ type values */
179396 u8 jnFlags; /* JNODE flags */
179397 u32 n; /* Bytes of content, or number of sub-nodes */
179398 union {
179399 const char *zJContent; /* Content for INT, REAL, and STRING */
179400 u32 iAppend; /* More terms for ARRAY and OBJECT */
179401 u32 iKey; /* Key for ARRAY objects in json_tree() */
179402 u32 iReplace; /* Replacement content for JNODE_REPLACE */
179403 JsonNode *pPatch; /* Node chain of patch for JNODE_PATCH */
179404 } u;
179405};
179406
179407/* A completely parsed JSON string
179408*/
179409struct JsonParse {
179410 u32 nNode; /* Number of slots of aNode[] used */
179411 u32 nAlloc; /* Number of slots of aNode[] allocated */
179412 JsonNode *aNode; /* Array of nodes containing the parse */
179413 const char *zJson; /* Original JSON string */
179414 u32 *aUp; /* Index of parent of each node */
179415 u8 oom; /* Set to true if out of memory */
179416 u8 nErr; /* Number of errors seen */
179417 u16 iDepth; /* Nesting depth */
179418 int nJson; /* Length of the zJson string in bytes */
179419 u32 iHold; /* Replace cache line with the lowest iHold value */
179420};
179421
179422/*
179423** Maximum nesting depth of JSON for this implementation.
179424**
179425** This limit is needed to avoid a stack overflow in the recursive
179426** descent parser. A depth of 2000 is far deeper than any sane JSON
179427** should go.
179428*/
179429#define JSON_MAX_DEPTH 2000
179430
179431/**************************************************************************
179432** Utility routines for dealing with JsonString objects
179433**************************************************************************/
179434
179435/* Set the JsonString object to an empty string
179436*/
179437static void jsonZero(JsonString *p){
179438 p->zBuf = p->zSpace;
179439 p->nAlloc = sizeof(p->zSpace);
179440 p->nUsed = 0;
179441 p->bStatic = 1;
179442}
179443
179444/* Initialize the JsonString object
179445*/
179446static void jsonInit(JsonString *p, sqlite3_context *pCtx){
179447 p->pCtx = pCtx;
179448 p->bErr = 0;
179449 jsonZero(p);
179450}
179451
179452
179453/* Free all allocated memory and reset the JsonString object back to its
179454** initial state.
179455*/
179456static void jsonReset(JsonString *p){
179457 if( !p->bStatic ) sqlite3_free(p->zBuf);
179458 jsonZero(p);
179459}
179460
179461
179462/* Report an out-of-memory (OOM) condition
179463*/
179464static void jsonOom(JsonString *p){
179465 p->bErr = 1;
179466 sqlite3_result_error_nomem(p->pCtx);
179467 jsonReset(p);
179468}
179469
179470/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
179471** Return zero on success. Return non-zero on an OOM error
179472*/
179473static int jsonGrow(JsonString *p, u32 N){
179474 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
179475 char *zNew;
179476 if( p->bStatic ){
179477 if( p->bErr ) return 1;
179478 zNew = sqlite3_malloc64(nTotal);
179479 if( zNew==0 ){
179480 jsonOom(p);
179481 return SQLITE_NOMEM7;
179482 }
179483 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
179484 p->zBuf = zNew;
179485 p->bStatic = 0;
179486 }else{
179487 zNew = sqlite3_realloc64(p->zBuf, nTotal);
179488 if( zNew==0 ){
179489 jsonOom(p);
179490 return SQLITE_NOMEM7;
179491 }
179492 p->zBuf = zNew;
179493 }
179494 p->nAlloc = nTotal;
179495 return SQLITE_OK0;
179496}
179497
179498/* Append N bytes from zIn onto the end of the JsonString string.
179499*/
179500static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
179501 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
179502 memcpy(p->zBuf+p->nUsed, zIn, N);
179503 p->nUsed += N;
179504}
179505
179506/* Append formatted text (not to exceed N bytes) to the JsonString.
179507*/
179508static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
179509 va_list ap;
179510 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
179511 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
179512 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
179513 va_end(ap)__builtin_va_end(ap);
179514 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
179515}
179516
179517/* Append a single character
179518*/
179519static void jsonAppendChar(JsonString *p, char c){
179520 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
179521 p->zBuf[p->nUsed++] = c;
179522}
179523
179524/* Append a comma separator to the output buffer, if the previous
179525** character is not '[' or '{'.
179526*/
179527static void jsonAppendSeparator(JsonString *p){
179528 char c;
179529 if( p->nUsed==0 ) return;
179530 c = p->zBuf[p->nUsed-1];
179531 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
179532}
179533
179534/* Append the N-byte string in zIn to the end of the JsonString string
179535** under construction. Enclose the string in "..." and escape
179536** any double-quotes or backslash characters contained within the
179537** string.
179538*/
179539static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
179540 u32 i;
179541 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
179542 p->zBuf[p->nUsed++] = '"';
179543 for(i=0; i<N; i++){
179544 unsigned char c = ((unsigned const char*)zIn)[i];
179545 if( c=='"' || c=='\\' ){
179546 json_simple_escape:
179547 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
179548 p->zBuf[p->nUsed++] = '\\';
179549 }else if( c<=0x1f ){
179550 static const char aSpecial[] = {
179551 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
179552 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
179553 };
179554 assert( sizeof(aSpecial)==32 )((void) (0));
179555 assert( aSpecial['\b']=='b' )((void) (0));
179556 assert( aSpecial['\f']=='f' )((void) (0));
179557 assert( aSpecial['\n']=='n' )((void) (0));
179558 assert( aSpecial['\r']=='r' )((void) (0));
179559 assert( aSpecial['\t']=='t' )((void) (0));
179560 if( aSpecial[c] ){
179561 c = aSpecial[c];
179562 goto json_simple_escape;
179563 }
179564 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
179565 p->zBuf[p->nUsed++] = '\\';
179566 p->zBuf[p->nUsed++] = 'u';
179567 p->zBuf[p->nUsed++] = '0';
179568 p->zBuf[p->nUsed++] = '0';
179569 p->zBuf[p->nUsed++] = '0' + (c>>4);
179570 c = "0123456789abcdef"[c&0xf];
179571 }
179572 p->zBuf[p->nUsed++] = c;
179573 }
179574 p->zBuf[p->nUsed++] = '"';
179575 assert( p->nUsed<p->nAlloc )((void) (0));
179576}
179577
179578/*
179579** Append a function parameter value to the JSON string under
179580** construction.
179581*/
179582static void jsonAppendValue(
179583 JsonString *p, /* Append to this JSON string */
179584 sqlite3_value *pValue /* Value to append */
179585){
179586 switch( sqlite3_value_type(pValue) ){
179587 case SQLITE_NULL5: {
179588 jsonAppendRaw(p, "null", 4);
179589 break;
179590 }
179591 case SQLITE_INTEGER1:
179592 case SQLITE_FLOAT2: {
179593 const char *z = (const char*)sqlite3_value_text(pValue);
179594 u32 n = (u32)sqlite3_value_bytes(pValue);
179595 jsonAppendRaw(p, z, n);
179596 break;
179597 }
179598 case SQLITE_TEXT3: {
179599 const char *z = (const char*)sqlite3_value_text(pValue);
179600 u32 n = (u32)sqlite3_value_bytes(pValue);
179601 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
179602 jsonAppendRaw(p, z, n);
179603 }else{
179604 jsonAppendString(p, z, n);
179605 }
179606 break;
179607 }
179608 default: {
179609 if( p->bErr==0 ){
179610 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
179611 p->bErr = 2;
179612 jsonReset(p);
179613 }
179614 break;
179615 }
179616 }
179617}
179618
179619
179620/* Make the JSON in p the result of the SQL function.
179621*/
179622static void jsonResult(JsonString *p){
179623 if( p->bErr==0 ){
179624 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
179625 p->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) : sqlite3_free,
179626 SQLITE_UTF81);
179627 jsonZero(p);
179628 }
179629 assert( p->bStatic )((void) (0));
179630}
179631
179632/**************************************************************************
179633** Utility routines for dealing with JsonNode and JsonParse objects
179634**************************************************************************/
179635
179636/*
179637** Return the number of consecutive JsonNode slots need to represent
179638** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
179639** OBJECT types, the number might be larger.
179640**
179641** Appended elements are not counted. The value returned is the number
179642** by which the JsonNode counter should increment in order to go to the
179643** next peer value.
179644*/
179645static u32 jsonNodeSize(JsonNode *pNode){
179646 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
179647}
179648
179649/*
179650** Reclaim all memory allocated by a JsonParse object. But do not
179651** delete the JsonParse object itself.
179652*/
179653static void jsonParseReset(JsonParse *pParse){
179654 sqlite3_free(pParse->aNode);
179655 pParse->aNode = 0;
179656 pParse->nNode = 0;
179657 pParse->nAlloc = 0;
179658 sqlite3_free(pParse->aUp);
179659 pParse->aUp = 0;
179660}
179661
179662/*
179663** Free a JsonParse object that was obtained from sqlite3_malloc().
179664*/
179665static void jsonParseFree(JsonParse *pParse){
179666 jsonParseReset(pParse);
179667 sqlite3_free(pParse);
179668}
179669
179670/*
179671** Convert the JsonNode pNode into a pure JSON string and
179672** append to pOut. Subsubstructure is also included. Return
179673** the number of JsonNode objects that are encoded.
179674*/
179675static void jsonRenderNode(
179676 JsonNode *pNode, /* The node to render */
179677 JsonString *pOut, /* Write JSON here */
179678 sqlite3_value **aReplace /* Replacement values */
179679){
179680 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
179681 if( pNode->jnFlags & JNODE_REPLACE ){
179682 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
179683 return;
179684 }
179685 pNode = pNode->u.pPatch;
179686 }
179687 switch( pNode->eType ){
179688 default: {
179689 assert( pNode->eType==JSON_NULL )((void) (0));
179690 jsonAppendRaw(pOut, "null", 4);
179691 break;
179692 }
179693 case JSON_TRUE: {
179694 jsonAppendRaw(pOut, "true", 4);
179695 break;
179696 }
179697 case JSON_FALSE: {
179698 jsonAppendRaw(pOut, "false", 5);
179699 break;
179700 }
179701 case JSON_STRING: {
179702 if( pNode->jnFlags & JNODE_RAW ){
179703 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
179704 break;
179705 }
179706 /* Fall through into the next case */
179707 }
179708 case JSON_REAL:
179709 case JSON_INT: {
179710 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
179711 break;
179712 }
179713 case JSON_ARRAY: {
179714 u32 j = 1;
179715 jsonAppendChar(pOut, '[');
179716 for(;;){
179717 while( j<=pNode->n ){
179718 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
179719 jsonAppendSeparator(pOut);
179720 jsonRenderNode(&pNode[j], pOut, aReplace);
179721 }
179722 j += jsonNodeSize(&pNode[j]);
179723 }
179724 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
179725 pNode = &pNode[pNode->u.iAppend];
179726 j = 1;
179727 }
179728 jsonAppendChar(pOut, ']');
179729 break;
179730 }
179731 case JSON_OBJECT: {
179732 u32 j = 1;
179733 jsonAppendChar(pOut, '{');
179734 for(;;){
179735 while( j<=pNode->n ){
179736 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
179737 jsonAppendSeparator(pOut);
179738 jsonRenderNode(&pNode[j], pOut, aReplace);
179739 jsonAppendChar(pOut, ':');
179740 jsonRenderNode(&pNode[j+1], pOut, aReplace);
179741 }
179742 j += 1 + jsonNodeSize(&pNode[j+1]);
179743 }
179744 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
179745 pNode = &pNode[pNode->u.iAppend];
179746 j = 1;
179747 }
179748 jsonAppendChar(pOut, '}');
179749 break;
179750 }
179751 }
179752}
179753
179754/*
179755** Return a JsonNode and all its descendents as a JSON string.
179756*/
179757static void jsonReturnJson(
179758 JsonNode *pNode, /* Node to return */
179759 sqlite3_context *pCtx, /* Return value for this function */
179760 sqlite3_value **aReplace /* Array of replacement values */
179761){
179762 JsonString s;
179763 jsonInit(&s, pCtx);
179764 jsonRenderNode(pNode, &s, aReplace);
179765 jsonResult(&s);
179766 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
179767}
179768
179769/*
179770** Make the JsonNode the return value of the function.
179771*/
179772static void jsonReturn(
179773 JsonNode *pNode, /* Node to return */
179774 sqlite3_context *pCtx, /* Return value for this function */
179775 sqlite3_value **aReplace /* Array of replacement values */
179776){
179777 switch( pNode->eType ){
179778 default: {
179779 assert( pNode->eType==JSON_NULL )((void) (0));
179780 sqlite3_result_null(pCtx);
179781 break;
179782 }
179783 case JSON_TRUE: {
179784 sqlite3_result_int(pCtx, 1);
179785 break;
179786 }
179787 case JSON_FALSE: {
179788 sqlite3_result_int(pCtx, 0);
179789 break;
179790 }
179791 case JSON_INT: {
179792 sqlite3_int64 i = 0;
179793 const char *z = pNode->u.zJContent;
179794 if( z[0]=='-' ){ z++; }
179795 while( z[0]>='0' && z[0]<='9' ){
179796 unsigned v = *(z++) - '0';
179797 if( i>=LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/10 ){
179798 if( i>LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32))/10 ) goto int_as_real;
179799 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
179800 if( v==9 ) goto int_as_real;
179801 if( v==8 ){
179802 if( pNode->u.zJContent[0]=='-' ){
179803 sqlite3_result_int64(pCtx, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
179804 goto int_done;
179805 }else{
179806 goto int_as_real;
179807 }
179808 }
179809 }
179810 i = i*10 + v;
179811 }
179812 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
179813 sqlite3_result_int64(pCtx, i);
179814 int_done:
179815 break;
179816 int_as_real: /* fall through to real */;
179817 }
179818 case JSON_REAL: {
179819 double r;
179820#ifdef SQLITE_AMALGAMATION1
179821 const char *z = pNode->u.zJContent;
179822 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF81);
179823#else
179824 r = strtod(pNode->u.zJContent, 0);
179825#endif
179826 sqlite3_result_double(pCtx, r);
179827 break;
179828 }
179829 case JSON_STRING: {
179830#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
179831 ** json_insert() and json_replace() and those routines do not
179832 ** call jsonReturn() */
179833 if( pNode->jnFlags & JNODE_RAW ){
179834 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
179835 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
179836 }else
179837#endif
179838 assert( (pNode->jnFlags & JNODE_RAW)==0 )((void) (0));
179839 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
179840 /* JSON formatted without any backslash-escapes */
179841 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
179842 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
179843 }else{
179844 /* Translate JSON formatted string into raw text */
179845 u32 i;
179846 u32 n = pNode->n;
179847 const char *z = pNode->u.zJContent;
179848 char *zOut;
179849 u32 j;
179850 zOut = sqlite3_malloc( n+1 );
179851 if( zOut==0 ){
179852 sqlite3_result_error_nomem(pCtx);
179853 break;
179854 }
179855 for(i=1, j=0; i<n-1; i++){
179856 char c = z[i];
179857 if( c!='\\' ){
179858 zOut[j++] = c;
179859 }else{
179860 c = z[++i];
179861 if( c=='u' ){
179862 u32 v = 0, k;
179863 for(k=0; k<4; i++, k++){
179864 assert( i<n-2 )((void) (0));
179865 c = z[i+1];
179866 assert( safe_isxdigit(c) )((void) (0));
179867 if( c<='9' ) v = v*16 + c - '0';
179868 else if( c<='F' ) v = v*16 + c - 'A' + 10;
179869 else v = v*16 + c - 'a' + 10;
179870 }
179871 if( v==0 ) break;
179872 if( v<=0x7f ){
179873 zOut[j++] = (char)v;
179874 }else if( v<=0x7ff ){
179875 zOut[j++] = (char)(0xc0 | (v>>6));
179876 zOut[j++] = 0x80 | (v&0x3f);
179877 }else{
179878 zOut[j++] = (char)(0xe0 | (v>>12));
179879 zOut[j++] = 0x80 | ((v>>6)&0x3f);
179880 zOut[j++] = 0x80 | (v&0x3f);
179881 }
179882 }else{
179883 if( c=='b' ){
179884 c = '\b';
179885 }else if( c=='f' ){
179886 c = '\f';
179887 }else if( c=='n' ){
179888 c = '\n';
179889 }else if( c=='r' ){
179890 c = '\r';
179891 }else if( c=='t' ){
179892 c = '\t';
179893 }
179894 zOut[j++] = c;
179895 }
179896 }
179897 }
179898 zOut[j] = 0;
179899 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
179900 }
179901 break;
179902 }
179903 case JSON_ARRAY:
179904 case JSON_OBJECT: {
179905 jsonReturnJson(pNode, pCtx, aReplace);
179906 break;
179907 }
179908 }
179909}
179910
179911/* Forward reference */
179912static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
179913
179914/*
179915** A macro to hint to the compiler that a function should not be
179916** inlined.
179917*/
179918#if defined(__GNUC__4)
179919# define JSON_NOINLINE __attribute__((noinline))
179920#elif defined(_MSC_VER) && _MSC_VER>=1310
179921# define JSON_NOINLINE __declspec(noinline)
179922#else
179923# define JSON_NOINLINE
179924#endif
179925
179926
179927static JSON_NOINLINE int jsonParseAddNodeExpand(
179928 JsonParse *pParse, /* Append the node to this object */
179929 u32 eType, /* Node type */
179930 u32 n, /* Content size or sub-node count */
179931 const char *zContent /* Content */
179932){
179933 u32 nNew;
179934 JsonNode *pNew;
179935 assert( pParse->nNode>=pParse->nAlloc )((void) (0));
179936 if( pParse->oom ) return -1;
179937 nNew = pParse->nAlloc*2 + 10;
179938 pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
179939 if( pNew==0 ){
179940 pParse->oom = 1;
179941 return -1;
179942 }
179943 pParse->nAlloc = nNew;
179944 pParse->aNode = pNew;
179945 assert( pParse->nNode<pParse->nAlloc )((void) (0));
179946 return jsonParseAddNode(pParse, eType, n, zContent);
179947}
179948
179949/*
179950** Create a new JsonNode instance based on the arguments and append that
179951** instance to the JsonParse. Return the index in pParse->aNode[] of the
179952** new node, or -1 if a memory allocation fails.
179953*/
179954static int jsonParseAddNode(
179955 JsonParse *pParse, /* Append the node to this object */
179956 u32 eType, /* Node type */
179957 u32 n, /* Content size or sub-node count */
179958 const char *zContent /* Content */
179959){
179960 JsonNode *p;
179961 if( pParse->nNode>=pParse->nAlloc ){
179962 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
179963 }
179964 p = &pParse->aNode[pParse->nNode];
179965 p->eType = (u8)eType;
179966 p->jnFlags = 0;
179967 p->n = n;
179968 p->u.zJContent = zContent;
179969 return pParse->nNode++;
179970}
179971
179972/*
179973** Return true if z[] begins with 4 (or more) hexadecimal digits
179974*/
179975static int jsonIs4Hex(const char *z){
179976 int i;
179977 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
179978 return 1;
179979}
179980
179981/*
179982** Parse a single JSON value which begins at pParse->zJson[i]. Return the
179983** index of the first character past the end of the value parsed.
179984**
179985** Return negative for a syntax error. Special cases: return -2 if the
179986** first non-whitespace character is '}' and return -3 if the first
179987** non-whitespace character is ']'.
179988*/
179989static int jsonParseValue(JsonParse *pParse, u32 i){
179990 char c;
179991 u32 j;
179992 int iThis;
179993 int x;
179994 JsonNode *pNode;
179995 const char *z = pParse->zJson;
179996 while( safe_isspace(z[i]) ){ i++; }
179997 if( (c = z[i])=='{' ){
179998 /* Parse object */
179999 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
180000 if( iThis<0 ) return -1;
180001 for(j=i+1;;j++){
180002 while( safe_isspace(z[j]) ){ j++; }
180003 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
180004 x = jsonParseValue(pParse, j);
180005 if( x<0 ){
180006 pParse->iDepth--;
180007 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
180008 return -1;
180009 }
180010 if( pParse->oom ) return -1;
180011 pNode = &pParse->aNode[pParse->nNode-1];
180012 if( pNode->eType!=JSON_STRING ) return -1;
180013 pNode->jnFlags |= JNODE_LABEL;
180014 j = x;
180015 while( safe_isspace(z[j]) ){ j++; }
180016 if( z[j]!=':' ) return -1;
180017 j++;
180018 x = jsonParseValue(pParse, j);
180019 pParse->iDepth--;
180020 if( x<0 ) return -1;
180021 j = x;
180022 while( safe_isspace(z[j]) ){ j++; }
180023 c = z[j];
180024 if( c==',' ) continue;
180025 if( c!='}' ) return -1;
180026 break;
180027 }
180028 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
180029 return j+1;
180030 }else if( c=='[' ){
180031 /* Parse array */
180032 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
180033 if( iThis<0 ) return -1;
180034 for(j=i+1;;j++){
180035 while( safe_isspace(z[j]) ){ j++; }
180036 if( ++pParse->iDepth > JSON_MAX_DEPTH ) return -1;
180037 x = jsonParseValue(pParse, j);
180038 pParse->iDepth--;
180039 if( x<0 ){
180040 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
180041 return -1;
180042 }
180043 j = x;
180044 while( safe_isspace(z[j]) ){ j++; }
180045 c = z[j];
180046 if( c==',' ) continue;
180047 if( c!=']' ) return -1;
180048 break;
180049 }
180050 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
180051 return j+1;
180052 }else if( c=='"' ){
180053 /* Parse string */
180054 u8 jnFlags = 0;
180055 j = i+1;
180056 for(;;){
180057 c = z[j];
180058 if( (c & ~0x1f)==0 ){
180059 /* Control characters are not allowed in strings */
180060 return -1;
180061 }
180062 if( c=='\\' ){
180063 c = z[++j];
180064 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
180065 || c=='n' || c=='r' || c=='t'
180066 || (c=='u' && jsonIs4Hex(z+j+1)) ){
180067 jnFlags = JNODE_ESCAPE;
180068 }else{
180069 return -1;
180070 }
180071 }else if( c=='"' ){
180072 break;
180073 }
180074 j++;
180075 }
180076 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &z[i]);
180077 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
180078 return j+1;
180079 }else if( c=='n'
180080 && strncmp(z+i,"null",4)==0
180081 && !safe_isalnum(z[i+4]) ){
180082 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
180083 return i+4;
180084 }else if( c=='t'
180085 && strncmp(z+i,"true",4)==0
180086 && !safe_isalnum(z[i+4]) ){
180087 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
180088 return i+4;
180089 }else if( c=='f'
180090 && strncmp(z+i,"false",5)==0
180091 && !safe_isalnum(z[i+5]) ){
180092 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
180093 return i+5;
180094 }else if( c=='-' || (c>='0' && c<='9') ){
180095 /* Parse number */
180096 u8 seenDP = 0;
180097 u8 seenE = 0;
180098 assert( '-' < '0' )((void) (0));
180099 if( c<='0' ){
180100 j = c=='-' ? i+1 : i;
180101 if( z[j]=='0' && z[j+1]>='0' && z[j+1]<='9' ) return -1;
180102 }
180103 j = i+1;
180104 for(;; j++){
180105 c = z[j];
180106 if( c>='0' && c<='9' ) continue;
180107 if( c=='.' ){
180108 if( z[j-1]=='-' ) return -1;
180109 if( seenDP ) return -1;
180110 seenDP = 1;
180111 continue;
180112 }
180113 if( c=='e' || c=='E' ){
180114 if( z[j-1]<'0' ) return -1;
180115 if( seenE ) return -1;
180116 seenDP = seenE = 1;
180117 c = z[j+1];
180118 if( c=='+' || c=='-' ){
180119 j++;
180120 c = z[j+1];
180121 }
180122 if( c<'0' || c>'9' ) return -1;
180123 continue;
180124 }
180125 break;
180126 }
180127 if( z[j-1]<'0' ) return -1;
180128 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
180129 j - i, &z[i]);
180130 return j;
180131 }else if( c=='}' ){
180132 return -2; /* End of {...} */
180133 }else if( c==']' ){
180134 return -3; /* End of [...] */
180135 }else if( c==0 ){
180136 return 0; /* End of file */
180137 }else{
180138 return -1; /* Syntax error */
180139 }
180140}
180141
180142/*
180143** Parse a complete JSON string. Return 0 on success or non-zero if there
180144** are any errors. If an error occurs, free all memory associated with
180145** pParse.
180146**
180147** pParse is uninitialized when this routine is called.
180148*/
180149static int jsonParse(
180150 JsonParse *pParse, /* Initialize and fill this JsonParse object */
180151 sqlite3_context *pCtx, /* Report errors here */
180152 const char *zJson /* Input JSON text to be parsed */
180153){
180154 int i;
180155 memset(pParse, 0, sizeof(*pParse));
180156 if( zJson==0 ) return 1;
180157 pParse->zJson = zJson;
180158 i = jsonParseValue(pParse, 0);
180159 if( pParse->oom ) i = -1;
180160 if( i>0 ){
180161 assert( pParse->iDepth==0 )((void) (0));
180162 while( safe_isspace(zJson[i]) ) i++;
180163 if( zJson[i] ) i = -1;
180164 }
180165 if( i<=0 ){
180166 if( pCtx!=0 ){
180167 if( pParse->oom ){
180168 sqlite3_result_error_nomem(pCtx);
180169 }else{
180170 sqlite3_result_error(pCtx, "malformed JSON", -1);
180171 }
180172 }
180173 jsonParseReset(pParse);
180174 return 1;
180175 }
180176 return 0;
180177}
180178
180179/* Mark node i of pParse as being a child of iParent. Call recursively
180180** to fill in all the descendants of node i.
180181*/
180182static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
180183 JsonNode *pNode = &pParse->aNode[i];
180184 u32 j;
180185 pParse->aUp[i] = iParent;
180186 switch( pNode->eType ){
180187 case JSON_ARRAY: {
180188 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
180189 jsonParseFillInParentage(pParse, i+j, i);
180190 }
180191 break;
180192 }
180193 case JSON_OBJECT: {
180194 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
180195 pParse->aUp[i+j] = i;
180196 jsonParseFillInParentage(pParse, i+j+1, i);
180197 }
180198 break;
180199 }
180200 default: {
180201 break;
180202 }
180203 }
180204}
180205
180206/*
180207** Compute the parentage of all nodes in a completed parse.
180208*/
180209static int jsonParseFindParents(JsonParse *pParse){
180210 u32 *aUp;
180211 assert( pParse->aUp==0 )((void) (0));
180212 aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
180213 if( aUp==0 ){
180214 pParse->oom = 1;
180215 return SQLITE_NOMEM7;
180216 }
180217 jsonParseFillInParentage(pParse, 0, 0);
180218 return SQLITE_OK0;
180219}
180220
180221/*
180222** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
180223*/
180224#define JSON_CACHE_ID (-429938) /* First cache entry */
180225#define JSON_CACHE_SZ 4 /* Max number of cache entries */
180226
180227/*
180228** Obtain a complete parse of the JSON found in the first argument
180229** of the argv array. Use the sqlite3_get_auxdata() cache for this
180230** parse if it is available. If the cache is not available or if it
180231** is no longer valid, parse the JSON again and return the new parse,
180232** and also register the new parse so that it will be available for
180233** future sqlite3_get_auxdata() calls.
180234*/
180235static JsonParse *jsonParseCached(
180236 sqlite3_context *pCtx,
180237 sqlite3_value **argv,
180238 sqlite3_context *pErrCtx
180239){
180240 const char *zJson = (const char*)sqlite3_value_text(argv[0]);
180241 int nJson = sqlite3_value_bytes(argv[0]);
180242 JsonParse *p;
180243 JsonParse *pMatch = 0;
180244 int iKey;
180245 int iMinKey = 0;
180246 u32 iMinHold = 0xffffffff;
180247 u32 iMaxHold = 0;
180248 if( zJson==0 ) return 0;
180249 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
180250 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
180251 if( p==0 ){
180252 iMinKey = iKey;
180253 break;
180254 }
180255 if( pMatch==0
180256 && p->nJson==nJson
180257 && memcmp(p->zJson,zJson,nJson)==0
180258 ){
180259 p->nErr = 0;
180260 pMatch = p;
180261 }else if( p->iHold<iMinHold ){
180262 iMinHold = p->iHold;
180263 iMinKey = iKey;
180264 }
180265 if( p->iHold>iMaxHold ){
180266 iMaxHold = p->iHold;
180267 }
180268 }
180269 if( pMatch ){
180270 pMatch->nErr = 0;
180271 pMatch->iHold = iMaxHold+1;
180272 return pMatch;
180273 }
180274 p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
180275 if( p==0 ){
180276 sqlite3_result_error_nomem(pCtx);
180277 return 0;
180278 }
180279 memset(p, 0, sizeof(*p));
180280 p->zJson = (char*)&p[1];
180281 memcpy((char*)p->zJson, zJson, nJson+1);
180282 if( jsonParse(p, pErrCtx, p->zJson) ){
180283 sqlite3_free(p);
180284 return 0;
180285 }
180286 p->nJson = nJson;
180287 p->iHold = iMaxHold+1;
180288 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
180289 (void(*)(void*))jsonParseFree);
180290 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
180291}
180292
180293/*
180294** Compare the OBJECT label at pNode against zKey,nKey. Return true on
180295** a match.
180296*/
180297static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
180298 if( pNode->jnFlags & JNODE_RAW ){
180299 if( pNode->n!=nKey ) return 0;
180300 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
180301 }else{
180302 if( pNode->n!=nKey+2 ) return 0;
180303 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
180304 }
180305}
180306
180307/* forward declaration */
180308static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
180309
180310/*
180311** Search along zPath to find the node specified. Return a pointer
180312** to that node, or NULL if zPath is malformed or if there is no such
180313** node.
180314**
180315** If pApnd!=0, then try to append new nodes to complete zPath if it is
180316** possible to do so and if no existing node corresponds to zPath. If
180317** new nodes are appended *pApnd is set to 1.
180318*/
180319static JsonNode *jsonLookupStep(
180320 JsonParse *pParse, /* The JSON to search */
180321 u32 iRoot, /* Begin the search at this node */
180322 const char *zPath, /* The path to search */
180323 int *pApnd, /* Append nodes to complete path if not NULL */
180324 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
180325){
180326 u32 i, j, nKey;
180327 const char *zKey;
180328 JsonNode *pRoot = &pParse->aNode[iRoot];
180329 if( zPath[0]==0 ) return pRoot;
180330 if( zPath[0]=='.' ){
180331 if( pRoot->eType!=JSON_OBJECT ) return 0;
180332 zPath++;
180333 if( zPath[0]=='"' ){
180334 zKey = zPath + 1;
180335 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
180336 nKey = i-1;
180337 if( zPath[i] ){
180338 i++;
180339 }else{
180340 *pzErr = zPath;
180341 return 0;
180342 }
180343 }else{
180344 zKey = zPath;
180345 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
180346 nKey = i;
180347 }
180348 if( nKey==0 ){
180349 *pzErr = zPath;
180350 return 0;
180351 }
180352 j = 1;
180353 for(;;){
180354 while( j<=pRoot->n ){
180355 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
180356 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
180357 }
180358 j++;
180359 j += jsonNodeSize(&pRoot[j]);
180360 }
180361 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
180362 iRoot += pRoot->u.iAppend;
180363 pRoot = &pParse->aNode[iRoot];
180364 j = 1;
180365 }
180366 if( pApnd ){
180367 u32 iStart, iLabel;
180368 JsonNode *pNode;
180369 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
180370 iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
180371 zPath += i;
180372 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
180373 if( pParse->oom ) return 0;
180374 if( pNode ){
180375 pRoot = &pParse->aNode[iRoot];
180376 pRoot->u.iAppend = iStart - iRoot;
180377 pRoot->jnFlags |= JNODE_APPEND;
180378 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
180379 }
180380 return pNode;
180381 }
180382 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
180383 if( pRoot->eType!=JSON_ARRAY ) return 0;
180384 i = 0;
180385 j = 1;
180386 while( safe_isdigit(zPath[j]) ){
180387 i = i*10 + zPath[j] - '0';
180388 j++;
180389 }
180390 if( zPath[j]!=']' ){
180391 *pzErr = zPath;
180392 return 0;
180393 }
180394 zPath += j + 1;
180395 j = 1;
180396 for(;;){
180397 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
180398 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
180399 j += jsonNodeSize(&pRoot[j]);
180400 }
180401 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
180402 iRoot += pRoot->u.iAppend;
180403 pRoot = &pParse->aNode[iRoot];
180404 j = 1;
180405 }
180406 if( j<=pRoot->n ){
180407 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
180408 }
180409 if( i==0 && pApnd ){
180410 u32 iStart;
180411 JsonNode *pNode;
180412 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
180413 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
180414 if( pParse->oom ) return 0;
180415 if( pNode ){
180416 pRoot = &pParse->aNode[iRoot];
180417 pRoot->u.iAppend = iStart - iRoot;
180418 pRoot->jnFlags |= JNODE_APPEND;
180419 }
180420 return pNode;
180421 }
180422 }else{
180423 *pzErr = zPath;
180424 }
180425 return 0;
180426}
180427
180428/*
180429** Append content to pParse that will complete zPath. Return a pointer
180430** to the inserted node, or return NULL if the append fails.
180431*/
180432static JsonNode *jsonLookupAppend(
180433 JsonParse *pParse, /* Append content to the JSON parse */
180434 const char *zPath, /* Description of content to append */
180435 int *pApnd, /* Set this flag to 1 */
180436 const char **pzErr /* Make this point to any syntax error */
180437){
180438 *pApnd = 1;
180439 if( zPath[0]==0 ){
180440 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
180441 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
180442 }
180443 if( zPath[0]=='.' ){
180444 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
180445 }else if( strncmp(zPath,"[0]",3)==0 ){
180446 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
180447 }else{
180448 return 0;
180449 }
180450 if( pParse->oom ) return 0;
180451 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
180452}
180453
180454/*
180455** Return the text of a syntax error message on a JSON path. Space is
180456** obtained from sqlite3_malloc().
180457*/
180458static char *jsonPathSyntaxError(const char *zErr){
180459 return sqlite3_mprintf("JSON path error near '%q'", zErr);
180460}
180461
180462/*
180463** Do a node lookup using zPath. Return a pointer to the node on success.
180464** Return NULL if not found or if there is an error.
180465**
180466** On an error, write an error message into pCtx and increment the
180467** pParse->nErr counter.
180468**
180469** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
180470** nodes are appended.
180471*/
180472static JsonNode *jsonLookup(
180473 JsonParse *pParse, /* The JSON to search */
180474 const char *zPath, /* The path to search */
180475 int *pApnd, /* Append nodes to complete path if not NULL */
180476 sqlite3_context *pCtx /* Report errors here, if not NULL */
180477){
180478 const char *zErr = 0;
180479 JsonNode *pNode = 0;
180480 char *zMsg;
180481
180482 if( zPath==0 ) return 0;
180483 if( zPath[0]!='$' ){
180484 zErr = zPath;
180485 goto lookup_err;
180486 }
180487 zPath++;
180488 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
180489 if( zErr==0 ) return pNode;
180490
180491lookup_err:
180492 pParse->nErr++;
180493 assert( zErr!=0 && pCtx!=0 )((void) (0));
180494 zMsg = jsonPathSyntaxError(zErr);
180495 if( zMsg ){
180496 sqlite3_result_error(pCtx, zMsg, -1);
180497 sqlite3_free(zMsg);
180498 }else{
180499 sqlite3_result_error_nomem(pCtx);
180500 }
180501 return 0;
180502}
180503
180504
180505/*
180506** Report the wrong number of arguments for json_insert(), json_replace()
180507** or json_set().
180508*/
180509static void jsonWrongNumArgs(
180510 sqlite3_context *pCtx,
180511 const char *zFuncName
180512){
180513 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
180514 zFuncName);
180515 sqlite3_result_error(pCtx, zMsg, -1);
180516 sqlite3_free(zMsg);
180517}
180518
180519/*
180520** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
180521*/
180522static void jsonRemoveAllNulls(JsonNode *pNode){
180523 int i, n;
180524 assert( pNode->eType==JSON_OBJECT )((void) (0));
180525 n = pNode->n;
180526 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
180527 switch( pNode[i].eType ){
180528 case JSON_NULL:
180529 pNode[i].jnFlags |= JNODE_REMOVE;
180530 break;
180531 case JSON_OBJECT:
180532 jsonRemoveAllNulls(&pNode[i]);
180533 break;
180534 }
180535 }
180536}
180537
180538
180539/****************************************************************************
180540** SQL functions used for testing and debugging
180541****************************************************************************/
180542
180543#ifdef SQLITE_DEBUG
180544/*
180545** The json_parse(JSON) function returns a string which describes
180546** a parse of the JSON provided. Or it returns NULL if JSON is not
180547** well-formed.
180548*/
180549static void jsonParseFunc(
180550 sqlite3_context *ctx,
180551 int argc,
180552 sqlite3_value **argv
180553){
180554 JsonString s; /* Output string - not real JSON */
180555 JsonParse x; /* The parse */
180556 u32 i;
180557
180558 assert( argc==1 )((void) (0));
180559 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
180560 jsonParseFindParents(&x);
180561 jsonInit(&s, ctx);
180562 for(i=0; i<x.nNode; i++){
180563 const char *zType;
180564 if( x.aNode[i].jnFlags & JNODE_LABEL ){
180565 assert( x.aNode[i].eType==JSON_STRING )((void) (0));
180566 zType = "label";
180567 }else{
180568 zType = jsonType[x.aNode[i].eType];
180569 }
180570 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
180571 i, zType, x.aNode[i].n, x.aUp[i]);
180572 if( x.aNode[i].u.zJContent!=0 ){
180573 jsonAppendRaw(&s, " ", 1);
180574 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
180575 }
180576 jsonAppendRaw(&s, "\n", 1);
180577 }
180578 jsonParseReset(&x);
180579 jsonResult(&s);
180580}
180581
180582/*
180583** The json_test1(JSON) function return true (1) if the input is JSON
180584** text generated by another json function. It returns (0) if the input
180585** is not known to be JSON.
180586*/
180587static void jsonTest1Func(
180588 sqlite3_context *ctx,
180589 int argc,
180590 sqlite3_value **argv
180591){
180592 UNUSED_PARAM(argc);
180593 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
180594}
180595#endif /* SQLITE_DEBUG */
180596
180597/****************************************************************************
180598** Scalar SQL function implementations
180599****************************************************************************/
180600
180601/*
180602** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
180603** corresponding to the SQL value input. Mostly this means putting
180604** double-quotes around strings and returning the unquoted string "null"
180605** when given a NULL input.
180606*/
180607static void jsonQuoteFunc(
180608 sqlite3_context *ctx,
180609 int argc,
180610 sqlite3_value **argv
180611){
180612 JsonString jx;
180613 UNUSED_PARAM(argc);
180614
180615 jsonInit(&jx, ctx);
180616 jsonAppendValue(&jx, argv[0]);
180617 jsonResult(&jx);
180618 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
180619}
180620
180621/*
180622** Implementation of the json_array(VALUE,...) function. Return a JSON
180623** array that contains all values given in arguments. Or if any argument
180624** is a BLOB, throw an error.
180625*/
180626static void jsonArrayFunc(
180627 sqlite3_context *ctx,
180628 int argc,
180629 sqlite3_value **argv
180630){
180631 int i;
180632 JsonString jx;
180633
180634 jsonInit(&jx, ctx);
180635 jsonAppendChar(&jx, '[');
180636 for(i=0; i<argc; i++){
180637 jsonAppendSeparator(&jx);
180638 jsonAppendValue(&jx, argv[i]);
180639 }
180640 jsonAppendChar(&jx, ']');
180641 jsonResult(&jx);
180642 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
180643}
180644
180645
180646/*
180647** json_array_length(JSON)
180648** json_array_length(JSON, PATH)
180649**
180650** Return the number of elements in the top-level JSON array.
180651** Return 0 if the input is not a well-formed JSON array.
180652*/
180653static void jsonArrayLengthFunc(
180654 sqlite3_context *ctx,
180655 int argc,
180656 sqlite3_value **argv
180657){
180658 JsonParse *p; /* The parse */
180659 sqlite3_int64 n = 0;
180660 u32 i;
180661 JsonNode *pNode;
180662
180663 p = jsonParseCached(ctx, argv, ctx);
180664 if( p==0 ) return;
180665 assert( p->nNode )((void) (0));
180666 if( argc==2 ){
180667 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
180668 pNode = jsonLookup(p, zPath, 0, ctx);
180669 }else{
180670 pNode = p->aNode;
180671 }
180672 if( pNode==0 ){
180673 return;
180674 }
180675 if( pNode->eType==JSON_ARRAY ){
180676 assert( (pNode->jnFlags & JNODE_APPEND)==0 )((void) (0));
180677 for(i=1; i<=pNode->n; n++){
180678 i += jsonNodeSize(&pNode[i]);
180679 }
180680 }
180681 sqlite3_result_int64(ctx, n);
180682}
180683
180684/*
180685** json_extract(JSON, PATH, ...)
180686**
180687** Return the element described by PATH. Return NULL if there is no
180688** PATH element. If there are multiple PATHs, then return a JSON array
180689** with the result from each path. Throw an error if the JSON or any PATH
180690** is malformed.
180691*/
180692static void jsonExtractFunc(
180693 sqlite3_context *ctx,
180694 int argc,
180695 sqlite3_value **argv
180696){
180697 JsonParse *p; /* The parse */
180698 JsonNode *pNode;
180699 const char *zPath;
180700 JsonString jx;
180701 int i;
180702
180703 if( argc<2 ) return;
180704 p = jsonParseCached(ctx, argv, ctx);
180705 if( p==0 ) return;
180706 jsonInit(&jx, ctx);
180707 jsonAppendChar(&jx, '[');
180708 for(i=1; i<argc; i++){
180709 zPath = (const char*)sqlite3_value_text(argv[i]);
180710 pNode = jsonLookup(p, zPath, 0, ctx);
180711 if( p->nErr ) break;
180712 if( argc>2 ){
180713 jsonAppendSeparator(&jx);
180714 if( pNode ){
180715 jsonRenderNode(pNode, &jx, 0);
180716 }else{
180717 jsonAppendRaw(&jx, "null", 4);
180718 }
180719 }else if( pNode ){
180720 jsonReturn(pNode, ctx, 0);
180721 }
180722 }
180723 if( argc>2 && i==argc ){
180724 jsonAppendChar(&jx, ']');
180725 jsonResult(&jx);
180726 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
180727 }
180728 jsonReset(&jx);
180729}
180730
180731/* This is the RFC 7396 MergePatch algorithm.
180732*/
180733static JsonNode *jsonMergePatch(
180734 JsonParse *pParse, /* The JSON parser that contains the TARGET */
180735 u32 iTarget, /* Node of the TARGET in pParse */
180736 JsonNode *pPatch /* The PATCH */
180737){
180738 u32 i, j;
180739 u32 iRoot;
180740 JsonNode *pTarget;
180741 if( pPatch->eType!=JSON_OBJECT ){
180742 return pPatch;
180743 }
180744 assert( iTarget>=0 && iTarget<pParse->nNode )((void) (0));
180745 pTarget = &pParse->aNode[iTarget];
180746 assert( (pPatch->jnFlags & JNODE_APPEND)==0 )((void) (0));
180747 if( pTarget->eType!=JSON_OBJECT ){
180748 jsonRemoveAllNulls(pPatch);
180749 return pPatch;
180750 }
180751 iRoot = iTarget;
180752 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
180753 u32 nKey;
180754 const char *zKey;
180755 assert( pPatch[i].eType==JSON_STRING )((void) (0));
180756 assert( pPatch[i].jnFlags & JNODE_LABEL )((void) (0));
180757 nKey = pPatch[i].n;
180758 zKey = pPatch[i].u.zJContent;
180759 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 )((void) (0));
180760 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
180761 assert( pTarget[j].eType==JSON_STRING )((void) (0));
180762 assert( pTarget[j].jnFlags & JNODE_LABEL )((void) (0));
180763 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 )((void) (0));
180764 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
180765 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
180766 if( pPatch[i+1].eType==JSON_NULL ){
180767 pTarget[j+1].jnFlags |= JNODE_REMOVE;
180768 }else{
180769 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
180770 if( pNew==0 ) return 0;
180771 pTarget = &pParse->aNode[iTarget];
180772 if( pNew!=&pTarget[j+1] ){
180773 pTarget[j+1].u.pPatch = pNew;
180774 pTarget[j+1].jnFlags |= JNODE_PATCH;
180775 }
180776 }
180777 break;
180778 }
180779 }
180780 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
180781 int iStart, iPatch;
180782 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
180783 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
180784 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
180785 if( pParse->oom ) return 0;
180786 jsonRemoveAllNulls(pPatch);
180787 pTarget = &pParse->aNode[iTarget];
180788 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
180789 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
180790 iRoot = iStart;
180791 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
180792 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
180793 }
180794 }
180795 return pTarget;
180796}
180797
180798/*
180799** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
180800** object that is the result of running the RFC 7396 MergePatch() algorithm
180801** on the two arguments.
180802*/
180803static void jsonPatchFunc(
180804 sqlite3_context *ctx,
180805 int argc,
180806 sqlite3_value **argv
180807){
180808 JsonParse x; /* The JSON that is being patched */
180809 JsonParse y; /* The patch */
180810 JsonNode *pResult; /* The result of the merge */
180811
180812 UNUSED_PARAM(argc);
180813 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
180814 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
180815 jsonParseReset(&x);
180816 return;
180817 }
180818 pResult = jsonMergePatch(&x, 0, y.aNode);
180819 assert( pResult!=0 || x.oom )((void) (0));
180820 if( pResult ){
180821 jsonReturnJson(pResult, ctx, 0);
180822 }else{
180823 sqlite3_result_error_nomem(ctx);
180824 }
180825 jsonParseReset(&x);
180826 jsonParseReset(&y);
180827}
180828
180829
180830/*
180831** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
180832** object that contains all name/value given in arguments. Or if any name
180833** is not a string or if any value is a BLOB, throw an error.
180834*/
180835static void jsonObjectFunc(
180836 sqlite3_context *ctx,
180837 int argc,
180838 sqlite3_value **argv
180839){
180840 int i;
180841 JsonString jx;
180842 const char *z;
180843 u32 n;
180844
180845 if( argc&1 ){
180846 sqlite3_result_error(ctx, "json_object() requires an even number "
180847 "of arguments", -1);
180848 return;
180849 }
180850 jsonInit(&jx, ctx);
180851 jsonAppendChar(&jx, '{');
180852 for(i=0; i<argc; i+=2){
180853 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT3 ){
180854 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
180855 jsonReset(&jx);
180856 return;
180857 }
180858 jsonAppendSeparator(&jx);
180859 z = (const char*)sqlite3_value_text(argv[i]);
180860 n = (u32)sqlite3_value_bytes(argv[i]);
180861 jsonAppendString(&jx, z, n);
180862 jsonAppendChar(&jx, ':');
180863 jsonAppendValue(&jx, argv[i+1]);
180864 }
180865 jsonAppendChar(&jx, '}');
180866 jsonResult(&jx);
180867 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
180868}
180869
180870
180871/*
180872** json_remove(JSON, PATH, ...)
180873**
180874** Remove the named elements from JSON and return the result. malformed
180875** JSON or PATH arguments result in an error.
180876*/
180877static void jsonRemoveFunc(
180878 sqlite3_context *ctx,
180879 int argc,
180880 sqlite3_value **argv
180881){
180882 JsonParse x; /* The parse */
180883 JsonNode *pNode;
180884 const char *zPath;
180885 u32 i;
180886
180887 if( argc<1 ) return;
180888 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
180889 assert( x.nNode )((void) (0));
180890 for(i=1; i<(u32)argc; i++){
180891 zPath = (const char*)sqlite3_value_text(argv[i]);
180892 if( zPath==0 ) goto remove_done;
180893 pNode = jsonLookup(&x, zPath, 0, ctx);
180894 if( x.nErr ) goto remove_done;
180895 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
180896 }
180897 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
180898 jsonReturnJson(x.aNode, ctx, 0);
180899 }
180900remove_done:
180901 jsonParseReset(&x);
180902}
180903
180904/*
180905** json_replace(JSON, PATH, VALUE, ...)
180906**
180907** Replace the value at PATH with VALUE. If PATH does not already exist,
180908** this routine is a no-op. If JSON or PATH is malformed, throw an error.
180909*/
180910static void jsonReplaceFunc(
180911 sqlite3_context *ctx,
180912 int argc,
180913 sqlite3_value **argv
180914){
180915 JsonParse x; /* The parse */
180916 JsonNode *pNode;
180917 const char *zPath;
180918 u32 i;
180919
180920 if( argc<1 ) return;
180921 if( (argc&1)==0 ) {
180922 jsonWrongNumArgs(ctx, "replace");
180923 return;
180924 }
180925 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
180926 assert( x.nNode )((void) (0));
180927 for(i=1; i<(u32)argc; i+=2){
180928 zPath = (const char*)sqlite3_value_text(argv[i]);
180929 pNode = jsonLookup(&x, zPath, 0, ctx);
180930 if( x.nErr ) goto replace_err;
180931 if( pNode ){
180932 pNode->jnFlags |= (u8)JNODE_REPLACE;
180933 pNode->u.iReplace = i + 1;
180934 }
180935 }
180936 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
180937 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
180938 }else{
180939 jsonReturnJson(x.aNode, ctx, argv);
180940 }
180941replace_err:
180942 jsonParseReset(&x);
180943}
180944
180945/*
180946** json_set(JSON, PATH, VALUE, ...)
180947**
180948** Set the value at PATH to VALUE. Create the PATH if it does not already
180949** exist. Overwrite existing values that do exist.
180950** If JSON or PATH is malformed, throw an error.
180951**
180952** json_insert(JSON, PATH, VALUE, ...)
180953**
180954** Create PATH and initialize it to VALUE. If PATH already exists, this
180955** routine is a no-op. If JSON or PATH is malformed, throw an error.
180956*/
180957static void jsonSetFunc(
180958 sqlite3_context *ctx,
180959 int argc,
180960 sqlite3_value **argv
180961){
180962 JsonParse x; /* The parse */
180963 JsonNode *pNode;
180964 const char *zPath;
180965 u32 i;
180966 int bApnd;
180967 int bIsSet = *(int*)sqlite3_user_data(ctx);
180968
180969 if( argc<1 ) return;
180970 if( (argc&1)==0 ) {
180971 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
180972 return;
180973 }
180974 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
180975 assert( x.nNode )((void) (0));
180976 for(i=1; i<(u32)argc; i+=2){
180977 zPath = (const char*)sqlite3_value_text(argv[i]);
180978 bApnd = 0;
180979 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
180980 if( x.oom ){
180981 sqlite3_result_error_nomem(ctx);
180982 goto jsonSetDone;
180983 }else if( x.nErr ){
180984 goto jsonSetDone;
180985 }else if( pNode && (bApnd || bIsSet) ){
180986 pNode->jnFlags |= (u8)JNODE_REPLACE;
180987 pNode->u.iReplace = i + 1;
180988 }
180989 }
180990 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
180991 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
180992 }else{
180993 jsonReturnJson(x.aNode, ctx, argv);
180994 }
180995jsonSetDone:
180996 jsonParseReset(&x);
180997}
180998
180999/*
181000** json_type(JSON)
181001** json_type(JSON, PATH)
181002**
181003** Return the top-level "type" of a JSON string. Throw an error if
181004** either the JSON or PATH inputs are not well-formed.
181005*/
181006static void jsonTypeFunc(
181007 sqlite3_context *ctx,
181008 int argc,
181009 sqlite3_value **argv
181010){
181011 JsonParse *p; /* The parse */
181012 const char *zPath;
181013 JsonNode *pNode;
181014
181015 p = jsonParseCached(ctx, argv, ctx);
181016 if( p==0 ) return;
181017 if( argc==2 ){
181018 zPath = (const char*)sqlite3_value_text(argv[1]);
181019 pNode = jsonLookup(p, zPath, 0, ctx);
181020 }else{
181021 pNode = p->aNode;
181022 }
181023 if( pNode ){
181024 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
181025 }
181026}
181027
181028/*
181029** json_valid(JSON)
181030**
181031** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
181032** Return 0 otherwise.
181033*/
181034static void jsonValidFunc(
181035 sqlite3_context *ctx,
181036 int argc,
181037 sqlite3_value **argv
181038){
181039 JsonParse *p; /* The parse */
181040 UNUSED_PARAM(argc);
181041 p = jsonParseCached(ctx, argv, 0);
181042 sqlite3_result_int(ctx, p!=0);
181043}
181044
181045
181046/****************************************************************************
181047** Aggregate SQL function implementations
181048****************************************************************************/
181049/*
181050** json_group_array(VALUE)
181051**
181052** Return a JSON array composed of all values in the aggregate.
181053*/
181054static void jsonArrayStep(
181055 sqlite3_context *ctx,
181056 int argc,
181057 sqlite3_value **argv
181058){
181059 JsonString *pStr;
181060 UNUSED_PARAM(argc);
181061 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
181062 if( pStr ){
181063 if( pStr->zBuf==0 ){
181064 jsonInit(pStr, ctx);
181065 jsonAppendChar(pStr, '[');
181066 }else{
181067 jsonAppendChar(pStr, ',');
181068 pStr->pCtx = ctx;
181069 }
181070 jsonAppendValue(pStr, argv[0]);
181071 }
181072}
181073static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
181074 JsonString *pStr;
181075 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
181076 if( pStr ){
181077 pStr->pCtx = ctx;
181078 jsonAppendChar(pStr, ']');
181079 if( pStr->bErr ){
181080 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
181081 assert( pStr->bStatic )((void) (0));
181082 }else if( isFinal ){
181083 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
181084 pStr->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) : sqlite3_free);
181085 pStr->bStatic = 1;
181086 }else{
181087 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
181088 pStr->nUsed--;
181089 }
181090 }else{
181091 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC((sqlite3_destructor_type)0));
181092 }
181093 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
181094}
181095static void jsonArrayValue(sqlite3_context *ctx){
181096 jsonArrayCompute(ctx, 0);
181097}
181098static void jsonArrayFinal(sqlite3_context *ctx){
181099 jsonArrayCompute(ctx, 1);
181100}
181101
181102#ifndef SQLITE_OMIT_WINDOWFUNC
181103/*
181104** This method works for both json_group_array() and json_group_object().
181105** It works by removing the first element of the group by searching forward
181106** to the first comma (",") that is not within a string and deleting all
181107** text through that comma.
181108*/
181109static void jsonGroupInverse(
181110 sqlite3_context *ctx,
181111 int argc,
181112 sqlite3_value **argv
181113){
181114 int i;
181115 int inStr = 0;
181116 char *z;
181117 JsonString *pStr;
181118 UNUSED_PARAM(argc);
181119 UNUSED_PARAM(argv);
181120 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
181121#ifdef NEVER
181122 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
181123 ** always have been called to initalize it */
181124 if( NEVER(!pStr)(!pStr) ) return;
181125#endif
181126 z = pStr->zBuf;
181127 for(i=1; z[i]!=',' || inStr; i++){
181128 assert( i<pStr->nUsed )((void) (0));
181129 if( z[i]=='"' ){
181130 inStr = !inStr;
181131 }else if( z[i]=='\\' ){
181132 i++;
181133 }
181134 }
181135 pStr->nUsed -= i;
181136 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
181137}
181138#else
181139# define jsonGroupInverse 0
181140#endif
181141
181142
181143/*
181144** json_group_obj(NAME,VALUE)
181145**
181146** Return a JSON object composed of all names and values in the aggregate.
181147*/
181148static void jsonObjectStep(
181149 sqlite3_context *ctx,
181150 int argc,
181151 sqlite3_value **argv
181152){
181153 JsonString *pStr;
181154 const char *z;
181155 u32 n;
181156 UNUSED_PARAM(argc);
181157 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
181158 if( pStr ){
181159 if( pStr->zBuf==0 ){
181160 jsonInit(pStr, ctx);
181161 jsonAppendChar(pStr, '{');
181162 }else{
181163 jsonAppendChar(pStr, ',');
181164 pStr->pCtx = ctx;
181165 }
181166 z = (const char*)sqlite3_value_text(argv[0]);
181167 n = (u32)sqlite3_value_bytes(argv[0]);
181168 jsonAppendString(pStr, z, n);
181169 jsonAppendChar(pStr, ':');
181170 jsonAppendValue(pStr, argv[1]);
181171 }
181172}
181173static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
181174 JsonString *pStr;
181175 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
181176 if( pStr ){
181177 jsonAppendChar(pStr, '}');
181178 if( pStr->bErr ){
181179 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
181180 assert( pStr->bStatic )((void) (0));
181181 }else if( isFinal ){
181182 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
181183 pStr->bStatic ? SQLITE_TRANSIENT((sqlite3_destructor_type)-1) : sqlite3_free);
181184 pStr->bStatic = 1;
181185 }else{
181186 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
181187 pStr->nUsed--;
181188 }
181189 }else{
181190 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC((sqlite3_destructor_type)0));
181191 }
181192 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
181193}
181194static void jsonObjectValue(sqlite3_context *ctx){
181195 jsonObjectCompute(ctx, 0);
181196}
181197static void jsonObjectFinal(sqlite3_context *ctx){
181198 jsonObjectCompute(ctx, 1);
181199}
181200
181201
181202
181203#ifndef SQLITE_OMIT_VIRTUALTABLE
181204/****************************************************************************
181205** The json_each virtual table
181206****************************************************************************/
181207typedef struct JsonEachCursor JsonEachCursor;
181208struct JsonEachCursor {
181209 sqlite3_vtab_cursor base; /* Base class - must be first */
181210 u32 iRowid; /* The rowid */
181211 u32 iBegin; /* The first node of the scan */
181212 u32 i; /* Index in sParse.aNode[] of current row */
181213 u32 iEnd; /* EOF when i equals or exceeds this value */
181214 u8 eType; /* Type of top-level element */
181215 u8 bRecursive; /* True for json_tree(). False for json_each() */
181216 char *zJson; /* Input JSON */
181217 char *zRoot; /* Path by which to filter zJson */
181218 JsonParse sParse; /* Parse of the input JSON */
181219};
181220
181221/* Constructor for the json_each virtual table */
181222static int jsonEachConnect(
181223 sqlite3 *db,
181224 void *pAux,
181225 int argc, const char *const*argv,
181226 sqlite3_vtab **ppVtab,
181227 char **pzErr
181228){
181229 sqlite3_vtab *pNew;
181230 int rc;
181231
181232/* Column numbers */
181233#define JEACH_KEY 0
181234#define JEACH_VALUE 1
181235#define JEACH_TYPE 2
181236#define JEACH_ATOM 3
181237#define JEACH_ID 4
181238#define JEACH_PARENT 5
181239#define JEACH_FULLKEY 6
181240#define JEACH_PATH 7
181241/* The xBestIndex method assumes that the JSON and ROOT columns are
181242** the last two columns in the table. Should this ever changes, be
181243** sure to update the xBestIndex method. */
181244#define JEACH_JSON 8
181245#define JEACH_ROOT 9
181246
181247 UNUSED_PARAM(pzErr);
181248 UNUSED_PARAM(argv);
181249 UNUSED_PARAM(argc);
181250 UNUSED_PARAM(pAux);
181251 rc = sqlite3_declare_vtab(db,
181252 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
181253 "json HIDDEN,root HIDDEN)");
181254 if( rc==SQLITE_OK0 ){
181255 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
181256 if( pNew==0 ) return SQLITE_NOMEM7;
181257 memset(pNew, 0, sizeof(*pNew));
181258 }
181259 return rc;
181260}
181261
181262/* destructor for json_each virtual table */
181263static int jsonEachDisconnect(sqlite3_vtab *pVtab){
181264 sqlite3_free(pVtab);
181265 return SQLITE_OK0;
181266}
181267
181268/* constructor for a JsonEachCursor object for json_each(). */
181269static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
181270 JsonEachCursor *pCur;
181271
181272 UNUSED_PARAM(p);
181273 pCur = sqlite3_malloc( sizeof(*pCur) );
181274 if( pCur==0 ) return SQLITE_NOMEM7;
181275 memset(pCur, 0, sizeof(*pCur));
181276 *ppCursor = &pCur->base;
181277 return SQLITE_OK0;
181278}
181279
181280/* constructor for a JsonEachCursor object for json_tree(). */
181281static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
181282 int rc = jsonEachOpenEach(p, ppCursor);
181283 if( rc==SQLITE_OK0 ){
181284 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
181285 pCur->bRecursive = 1;
181286 }
181287 return rc;
181288}
181289
181290/* Reset a JsonEachCursor back to its original state. Free any memory
181291** held. */
181292static void jsonEachCursorReset(JsonEachCursor *p){
181293 sqlite3_free(p->zJson);
181294 sqlite3_free(p->zRoot);
181295 jsonParseReset(&p->sParse);
181296 p->iRowid = 0;
181297 p->i = 0;
181298 p->iEnd = 0;
181299 p->eType = 0;
181300 p->zJson = 0;
181301 p->zRoot = 0;
181302}
181303
181304/* Destructor for a jsonEachCursor object */
181305static int jsonEachClose(sqlite3_vtab_cursor *cur){
181306 JsonEachCursor *p = (JsonEachCursor*)cur;
181307 jsonEachCursorReset(p);
181308 sqlite3_free(cur);
181309 return SQLITE_OK0;
181310}
181311
181312/* Return TRUE if the jsonEachCursor object has been advanced off the end
181313** of the JSON object */
181314static int jsonEachEof(sqlite3_vtab_cursor *cur){
181315 JsonEachCursor *p = (JsonEachCursor*)cur;
181316 return p->i >= p->iEnd;
181317}
181318
181319/* Advance the cursor to the next element for json_tree() */
181320static int jsonEachNext(sqlite3_vtab_cursor *cur){
181321 JsonEachCursor *p = (JsonEachCursor*)cur;
181322 if( p->bRecursive ){
181323 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
181324 p->i++;
181325 p->iRowid++;
181326 if( p->i<p->iEnd ){
181327 u32 iUp = p->sParse.aUp[p->i];
181328 JsonNode *pUp = &p->sParse.aNode[iUp];
181329 p->eType = pUp->eType;
181330 if( pUp->eType==JSON_ARRAY ){
181331 if( iUp==p->i-1 ){
181332 pUp->u.iKey = 0;
181333 }else{
181334 pUp->u.iKey++;
181335 }
181336 }
181337 }
181338 }else{
181339 switch( p->eType ){
181340 case JSON_ARRAY: {
181341 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
181342 p->iRowid++;
181343 break;
181344 }
181345 case JSON_OBJECT: {
181346 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
181347 p->iRowid++;
181348 break;
181349 }
181350 default: {
181351 p->i = p->iEnd;
181352 break;
181353 }
181354 }
181355 }
181356 return SQLITE_OK0;
181357}
181358
181359/* Append the name of the path for element i to pStr
181360*/
181361static void jsonEachComputePath(
181362 JsonEachCursor *p, /* The cursor */
181363 JsonString *pStr, /* Write the path here */
181364 u32 i /* Path to this element */
181365){
181366 JsonNode *pNode, *pUp;
181367 u32 iUp;
181368 if( i==0 ){
181369 jsonAppendChar(pStr, '$');
181370 return;
181371 }
181372 iUp = p->sParse.aUp[i];
181373 jsonEachComputePath(p, pStr, iUp);
181374 pNode = &p->sParse.aNode[i];
181375 pUp = &p->sParse.aNode[iUp];
181376 if( pUp->eType==JSON_ARRAY ){
181377 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
181378 }else{
181379 assert( pUp->eType==JSON_OBJECT )((void) (0));
181380 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
181381 assert( pNode->eType==JSON_STRING )((void) (0));
181382 assert( pNode->jnFlags & JNODE_LABEL )((void) (0));
181383 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
181384 }
181385}
181386
181387/* Return the value of a column */
181388static int jsonEachColumn(
181389 sqlite3_vtab_cursor *cur, /* The cursor */
181390 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
181391 int i /* Which column to return */
181392){
181393 JsonEachCursor *p = (JsonEachCursor*)cur;
181394 JsonNode *pThis = &p->sParse.aNode[p->i];
181395 switch( i ){
181396 case JEACH_KEY: {
181397 if( p->i==0 ) break;
181398 if( p->eType==JSON_OBJECT ){
181399 jsonReturn(pThis, ctx, 0);
181400 }else if( p->eType==JSON_ARRAY ){
181401 u32 iKey;
181402 if( p->bRecursive ){
181403 if( p->iRowid==0 ) break;
181404 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
181405 }else{
181406 iKey = p->iRowid;
181407 }
181408 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
181409 }
181410 break;
181411 }
181412 case JEACH_VALUE: {
181413 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
181414 jsonReturn(pThis, ctx, 0);
181415 break;
181416 }
181417 case JEACH_TYPE: {
181418 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
181419 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
181420 break;
181421 }
181422 case JEACH_ATOM: {
181423 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
181424 if( pThis->eType>=JSON_ARRAY ) break;
181425 jsonReturn(pThis, ctx, 0);
181426 break;
181427 }
181428 case JEACH_ID: {
181429 sqlite3_result_int64(ctx,
181430 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
181431 break;
181432 }
181433 case JEACH_PARENT: {
181434 if( p->i>p->iBegin && p->bRecursive ){
181435 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
181436 }
181437 break;
181438 }
181439 case JEACH_FULLKEY: {
181440 JsonString x;
181441 jsonInit(&x, ctx);
181442 if( p->bRecursive ){
181443 jsonEachComputePath(p, &x, p->i);
181444 }else{
181445 if( p->zRoot ){
181446 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
181447 }else{
181448 jsonAppendChar(&x, '$');
181449 }
181450 if( p->eType==JSON_ARRAY ){
181451 jsonPrintf(30, &x, "[%d]", p->iRowid);
181452 }else if( p->eType==JSON_OBJECT ){
181453 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
181454 }
181455 }
181456 jsonResult(&x);
181457 break;
181458 }
181459 case JEACH_PATH: {
181460 if( p->bRecursive ){
181461 JsonString x;
181462 jsonInit(&x, ctx);
181463 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
181464 jsonResult(&x);
181465 break;
181466 }
181467 /* For json_each() path and root are the same so fall through
181468 ** into the root case */
181469 }
181470 default: {
181471 const char *zRoot = p->zRoot;
181472 if( zRoot==0 ) zRoot = "$";
181473 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
181474 break;
181475 }
181476 case JEACH_JSON: {
181477 assert( i==JEACH_JSON )((void) (0));
181478 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
181479 break;
181480 }
181481 }
181482 return SQLITE_OK0;
181483}
181484
181485/* Return the current rowid value */
181486static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
181487 JsonEachCursor *p = (JsonEachCursor*)cur;
181488 *pRowid = p->iRowid;
181489 return SQLITE_OK0;
181490}
181491
181492/* The query strategy is to look for an equality constraint on the json
181493** column. Without such a constraint, the table cannot operate. idxNum is
181494** 1 if the constraint is found, 3 if the constraint and zRoot are found,
181495** and 0 otherwise.
181496*/
181497static int jsonEachBestIndex(
181498 sqlite3_vtab *tab,
181499 sqlite3_index_info *pIdxInfo
181500){
181501 int i; /* Loop counter or computed array index */
181502 int aIdx[2]; /* Index of constraints for JSON and ROOT */
181503 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
181504 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
181505 const struct sqlite3_index_constraint *pConstraint;
181506
181507 /* This implementation assumes that JSON and ROOT are the last two
181508 ** columns in the table */
181509 assert( JEACH_ROOT == JEACH_JSON+1 )((void) (0));
181510 UNUSED_PARAM(tab);
181511 aIdx[0] = aIdx[1] = -1;
181512 pConstraint = pIdxInfo->aConstraint;
181513 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
181514 int iCol;
181515 int iMask;
181516 if( pConstraint->iColumn < JEACH_JSON ) continue;
181517 iCol = pConstraint->iColumn - JEACH_JSON;
181518 assert( iCol==0 || iCol==1 )((void) (0));
181519 iMask = 1 << iCol;
181520 if( pConstraint->usable==0 ){
181521 unusableMask |= iMask;
181522 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
181523 aIdx[iCol] = i;
181524 idxMask |= iMask;
181525 }
181526 }
181527 if( (unusableMask & ~idxMask)!=0 ){
181528 /* If there are any unusable constraints on JSON or ROOT, then reject
181529 ** this entire plan */
181530 return SQLITE_CONSTRAINT19;
181531 }
181532 if( aIdx[0]<0 ){
181533 /* No JSON input. Leave estimatedCost at the huge value that it was
181534 ** initialized to to discourage the query planner from selecting this
181535 ** plan. */
181536 pIdxInfo->idxNum = 0;
181537 }else{
181538 pIdxInfo->estimatedCost = 1.0;
181539 i = aIdx[0];
181540 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
181541 pIdxInfo->aConstraintUsage[i].omit = 1;
181542 if( aIdx[1]<0 ){
181543 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
181544 }else{
181545 i = aIdx[1];
181546 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
181547 pIdxInfo->aConstraintUsage[i].omit = 1;
181548 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
181549 }
181550 }
181551 return SQLITE_OK0;
181552}
181553
181554/* Start a search on a new JSON string */
181555static int jsonEachFilter(
181556 sqlite3_vtab_cursor *cur,
181557 int idxNum, const char *idxStr,
181558 int argc, sqlite3_value **argv
181559){
181560 JsonEachCursor *p = (JsonEachCursor*)cur;
181561 const char *z;
181562 const char *zRoot = 0;
181563 sqlite3_int64 n;
181564
181565 UNUSED_PARAM(idxStr);
181566 UNUSED_PARAM(argc);
181567 jsonEachCursorReset(p);
181568 if( idxNum==0 ) return SQLITE_OK0;
181569 z = (const char*)sqlite3_value_text(argv[0]);
181570 if( z==0 ) return SQLITE_OK0;
181571 n = sqlite3_value_bytes(argv[0]);
181572 p->zJson = sqlite3_malloc64( n+1 );
181573 if( p->zJson==0 ) return SQLITE_NOMEM7;
181574 memcpy(p->zJson, z, (size_t)n+1);
181575 if( jsonParse(&p->sParse, 0, p->zJson) ){
181576 int rc = SQLITE_NOMEM7;
181577 if( p->sParse.oom==0 ){
181578 sqlite3_free(cur->pVtab->zErrMsg);
181579 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
181580 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR1;
181581 }
181582 jsonEachCursorReset(p);
181583 return rc;
181584 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
181585 jsonEachCursorReset(p);
181586 return SQLITE_NOMEM7;
181587 }else{
181588 JsonNode *pNode = 0;
181589 if( idxNum==3 ){
181590 const char *zErr = 0;
181591 zRoot = (const char*)sqlite3_value_text(argv[1]);
181592 if( zRoot==0 ) return SQLITE_OK0;
181593 n = sqlite3_value_bytes(argv[1]);
181594 p->zRoot = sqlite3_malloc64( n+1 );
181595 if( p->zRoot==0 ) return SQLITE_NOMEM7;
181596 memcpy(p->zRoot, zRoot, (size_t)n+1);
181597 if( zRoot[0]!='$' ){
181598 zErr = zRoot;
181599 }else{
181600 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
181601 }
181602 if( zErr ){
181603 sqlite3_free(cur->pVtab->zErrMsg);
181604 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
181605 jsonEachCursorReset(p);
181606 return cur->pVtab->zErrMsg ? SQLITE_ERROR1 : SQLITE_NOMEM7;
181607 }else if( pNode==0 ){
181608 return SQLITE_OK0;
181609 }
181610 }else{
181611 pNode = p->sParse.aNode;
181612 }
181613 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
181614 p->eType = pNode->eType;
181615 if( p->eType>=JSON_ARRAY ){
181616 pNode->u.iKey = 0;
181617 p->iEnd = p->i + pNode->n + 1;
181618 if( p->bRecursive ){
181619 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
181620 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
181621 p->i--;
181622 }
181623 }else{
181624 p->i++;
181625 }
181626 }else{
181627 p->iEnd = p->i+1;
181628 }
181629 }
181630 return SQLITE_OK0;
181631}
181632
181633/* The methods of the json_each virtual table */
181634static sqlite3_module jsonEachModule = {
181635 0, /* iVersion */
181636 0, /* xCreate */
181637 jsonEachConnect, /* xConnect */
181638 jsonEachBestIndex, /* xBestIndex */
181639 jsonEachDisconnect, /* xDisconnect */
181640 0, /* xDestroy */
181641 jsonEachOpenEach, /* xOpen - open a cursor */
181642 jsonEachClose, /* xClose - close a cursor */
181643 jsonEachFilter, /* xFilter - configure scan constraints */
181644 jsonEachNext, /* xNext - advance a cursor */
181645 jsonEachEof, /* xEof - check for end of scan */
181646 jsonEachColumn, /* xColumn - read data */
181647 jsonEachRowid, /* xRowid - read data */
181648 0, /* xUpdate */
181649 0, /* xBegin */
181650 0, /* xSync */
181651 0, /* xCommit */
181652 0, /* xRollback */
181653 0, /* xFindMethod */
181654 0, /* xRename */
181655 0, /* xSavepoint */
181656 0, /* xRelease */
181657 0, /* xRollbackTo */
181658 0 /* xShadowName */
181659};
181660
181661/* The methods of the json_tree virtual table. */
181662static sqlite3_module jsonTreeModule = {
181663 0, /* iVersion */
181664 0, /* xCreate */
181665 jsonEachConnect, /* xConnect */
181666 jsonEachBestIndex, /* xBestIndex */
181667 jsonEachDisconnect, /* xDisconnect */
181668 0, /* xDestroy */
181669 jsonEachOpenTree, /* xOpen - open a cursor */
181670 jsonEachClose, /* xClose - close a cursor */
181671 jsonEachFilter, /* xFilter - configure scan constraints */
181672 jsonEachNext, /* xNext - advance a cursor */
181673 jsonEachEof, /* xEof - check for end of scan */
181674 jsonEachColumn, /* xColumn - read data */
181675 jsonEachRowid, /* xRowid - read data */
181676 0, /* xUpdate */
181677 0, /* xBegin */
181678 0, /* xSync */
181679 0, /* xCommit */
181680 0, /* xRollback */
181681 0, /* xFindMethod */
181682 0, /* xRename */
181683 0, /* xSavepoint */
181684 0, /* xRelease */
181685 0, /* xRollbackTo */
181686 0 /* xShadowName */
181687};
181688#endif /* SQLITE_OMIT_VIRTUALTABLE */
181689
181690/****************************************************************************
181691** The following routines are the only publically visible identifiers in this
181692** file. Call the following routines in order to register the various SQL
181693** functions and the virtual table implemented by this file.
181694****************************************************************************/
181695
181696SQLITE_PRIVATEstatic int sqlite3Json1Init(sqlite3 *db){
181697 int rc = SQLITE_OK0;
181698 unsigned int i;
181699 static const struct {
181700 const char *zName;
181701 int nArg;
181702 int flag;
181703 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
181704 } aFunc[] = {
181705 { "json", 1, 0, jsonRemoveFunc },
181706 { "json_array", -1, 0, jsonArrayFunc },
181707 { "json_array_length", 1, 0, jsonArrayLengthFunc },
181708 { "json_array_length", 2, 0, jsonArrayLengthFunc },
181709 { "json_extract", -1, 0, jsonExtractFunc },
181710 { "json_insert", -1, 0, jsonSetFunc },
181711 { "json_object", -1, 0, jsonObjectFunc },
181712 { "json_patch", 2, 0, jsonPatchFunc },
181713 { "json_quote", 1, 0, jsonQuoteFunc },
181714 { "json_remove", -1, 0, jsonRemoveFunc },
181715 { "json_replace", -1, 0, jsonReplaceFunc },
181716 { "json_set", -1, 1, jsonSetFunc },
181717 { "json_type", 1, 0, jsonTypeFunc },
181718 { "json_type", 2, 0, jsonTypeFunc },
181719 { "json_valid", 1, 0, jsonValidFunc },
181720
181721#if SQLITE_DEBUG
181722 /* DEBUG and TESTING functions */
181723 { "json_parse", 1, 0, jsonParseFunc },
181724 { "json_test1", 1, 0, jsonTest1Func },
181725#endif
181726 };
181727 static const struct {
181728 const char *zName;
181729 int nArg;
181730 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
181731 void (*xFinal)(sqlite3_context*);
181732 void (*xValue)(sqlite3_context*);
181733 } aAgg[] = {
181734 { "json_group_array", 1,
181735 jsonArrayStep, jsonArrayFinal, jsonArrayValue },
181736 { "json_group_object", 2,
181737 jsonObjectStep, jsonObjectFinal, jsonObjectValue },
181738 };
181739#ifndef SQLITE_OMIT_VIRTUALTABLE
181740 static const struct {
181741 const char *zName;
181742 sqlite3_module *pModule;
181743 } aMod[] = {
181744 { "json_each", &jsonEachModule },
181745 { "json_tree", &jsonTreeModule },
181746 };
181747#endif
181748 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK0; i++){
181749 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
181750 SQLITE_UTF81 | SQLITE_DETERMINISTIC0x800,
181751 (void*)&aFunc[i].flag,
181752 aFunc[i].xFunc, 0, 0);
181753 }
181754#ifndef SQLITE_OMIT_WINDOWFUNC
181755 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK0; i++){
181756 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
181757 SQLITE_UTF81 | SQLITE_DETERMINISTIC0x800, 0,
181758 aAgg[i].xStep, aAgg[i].xFinal,
181759 aAgg[i].xValue, jsonGroupInverse, 0);
181760 }
181761#endif
181762#ifndef SQLITE_OMIT_VIRTUALTABLE
181763 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK0; i++){
181764 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
181765 }
181766#endif
181767 return rc;
181768}
181769
181770
181771#ifndef SQLITE_CORE1
181772#ifdef _WIN32
181773__declspec(dllexport)
181774#endif
181775SQLITE_API int sqlite3_json_init(
181776 sqlite3 *db,
181777 char **pzErrMsg,
181778 const sqlite3_api_routines *pApi
181779){
181780 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
181781 (void)pzErrMsg; /* Unused parameter */
181782 return sqlite3Json1Init(db);
181783}
181784#endif
181785#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */
181786
181787/************** End of json1.c ***********************************************/
181788/************** Begin file rtree.c *******************************************/
181789/*
181790** 2001 September 15
181791**
181792** The author disclaims copyright to this source code. In place of
181793** a legal notice, here is a blessing:
181794**
181795** May you do good and not evil.
181796** May you find forgiveness for yourself and forgive others.
181797** May you share freely, never taking more than you give.
181798**
181799*************************************************************************
181800** This file contains code for implementations of the r-tree and r*-tree
181801** algorithms packaged as an SQLite virtual table module.
181802*/
181803
181804/*
181805** Database Format of R-Tree Tables
181806** --------------------------------
181807**
181808** The data structure for a single virtual r-tree table is stored in three
181809** native SQLite tables declared as follows. In each case, the '%' character
181810** in the table name is replaced with the user-supplied name of the r-tree
181811** table.
181812**
181813** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
181814** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
181815** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
181816**
181817** The data for each node of the r-tree structure is stored in the %_node
181818** table. For each node that is not the root node of the r-tree, there is
181819** an entry in the %_parent table associating the node with its parent.
181820** And for each row of data in the table, there is an entry in the %_rowid
181821** table that maps from the entries rowid to the id of the node that it
181822** is stored on. If the r-tree contains auxiliary columns, those are stored
181823** on the end of the %_rowid table.
181824**
181825** The root node of an r-tree always exists, even if the r-tree table is
181826** empty. The nodeno of the root node is always 1. All other nodes in the
181827** table must be the same size as the root node. The content of each node
181828** is formatted as follows:
181829**
181830** 1. If the node is the root node (node 1), then the first 2 bytes
181831** of the node contain the tree depth as a big-endian integer.
181832** For non-root nodes, the first 2 bytes are left unused.
181833**
181834** 2. The next 2 bytes contain the number of entries currently
181835** stored in the node.
181836**
181837** 3. The remainder of the node contains the node entries. Each entry
181838** consists of a single 8-byte integer followed by an even number
181839** of 4-byte coordinates. For leaf nodes the integer is the rowid
181840** of a record. For internal nodes it is the node number of a
181841** child page.
181842*/
181843
181844#if !defined(SQLITE_CORE1) \
181845 || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
181846
181847#ifndef SQLITE_CORE1
181848/* #include "sqlite3ext.h" */
181849 SQLITE_EXTENSION_INIT1
181850#else
181851/* #include "sqlite3.h" */
181852#endif
181853
181854#ifndef SQLITE_AMALGAMATION1
181855#include "sqlite3rtree.h"
181856typedef sqlite3_int64 i64;
181857typedef sqlite3_uint64 u64;
181858typedef unsigned char u8;
181859typedef unsigned short u16;
181860typedef unsigned int u32;
181861#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
181862# define NDEBUG1 1
181863#endif
181864#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
181865# undef NDEBUG1
181866#endif
181867#endif
181868
181869/* #include <string.h> */
181870/* #include <stdio.h> */
181871/* #include <assert.h> */
181872
181873/* The following macro is used to suppress compiler warnings.
181874*/
181875#ifndef UNUSED_PARAMETER
181876# define UNUSED_PARAMETER(x)(void)(x) (void)(x)
181877#endif
181878
181879typedef struct Rtree Rtree;
181880typedef struct RtreeCursor RtreeCursor;
181881typedef struct RtreeNode RtreeNode;
181882typedef struct RtreeCell RtreeCell;
181883typedef struct RtreeConstraint RtreeConstraint;
181884typedef struct RtreeMatchArg RtreeMatchArg;
181885typedef struct RtreeGeomCallback RtreeGeomCallback;
181886typedef union RtreeCoord RtreeCoord;
181887typedef struct RtreeSearchPoint RtreeSearchPoint;
181888
181889/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
181890#define RTREE_MAX_DIMENSIONS 5
181891
181892/* Maximum number of auxiliary columns */
181893#define RTREE_MAX_AUX_COLUMN 100
181894
181895/* Size of hash table Rtree.aHash. This hash table is not expected to
181896** ever contain very many entries, so a fixed number of buckets is
181897** used.
181898*/
181899#define HASHSIZE 97
181900
181901/* The xBestIndex method of this virtual table requires an estimate of
181902** the number of rows in the virtual table to calculate the costs of
181903** various strategies. If possible, this estimate is loaded from the
181904** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
181905** Otherwise, if no sqlite_stat1 entry is available, use
181906** RTREE_DEFAULT_ROWEST.
181907*/
181908#define RTREE_DEFAULT_ROWEST 1048576
181909#define RTREE_MIN_ROWEST 100
181910
181911/*
181912** An rtree virtual-table object.
181913*/
181914struct Rtree {
181915 sqlite3_vtab base; /* Base class. Must be first */
181916 sqlite3 *db; /* Host database connection */
181917 int iNodeSize; /* Size in bytes of each node in the node table */
181918 u8 nDim; /* Number of dimensions */
181919 u8 nDim2; /* Twice the number of dimensions */
181920 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
181921 u8 nBytesPerCell; /* Bytes consumed per cell */
181922 u8 inWrTrans; /* True if inside write transaction */
181923 u8 nAux; /* # of auxiliary columns in %_rowid */
181924 u8 nAuxNotNull; /* Number of initial not-null aux columns */
181925#ifdef SQLITE_DEBUG
181926 u8 bCorrupt; /* Shadow table corruption detected */
181927#endif
181928 int iDepth; /* Current depth of the r-tree structure */
181929 char *zDb; /* Name of database containing r-tree table */
181930 char *zName; /* Name of r-tree table */
181931 u32 nBusy; /* Current number of users of this structure */
181932 i64 nRowEst; /* Estimated number of rows in this table */
181933 u32 nCursor; /* Number of open cursors */
181934 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
181935 char *zReadAuxSql; /* SQL for statement to read aux data */
181936
181937 /* List of nodes removed during a CondenseTree operation. List is
181938 ** linked together via the pointer normally used for hash chains -
181939 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
181940 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
181941 */
181942 RtreeNode *pDeleted;
181943 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */
181944
181945 /* Blob I/O on xxx_node */
181946 sqlite3_blob *pNodeBlob;
181947
181948 /* Statements to read/write/delete a record from xxx_node */
181949 sqlite3_stmt *pWriteNode;
181950 sqlite3_stmt *pDeleteNode;
181951
181952 /* Statements to read/write/delete a record from xxx_rowid */
181953 sqlite3_stmt *pReadRowid;
181954 sqlite3_stmt *pWriteRowid;
181955 sqlite3_stmt *pDeleteRowid;
181956
181957 /* Statements to read/write/delete a record from xxx_parent */
181958 sqlite3_stmt *pReadParent;
181959 sqlite3_stmt *pWriteParent;
181960 sqlite3_stmt *pDeleteParent;
181961
181962 /* Statement for writing to the "aux:" fields, if there are any */
181963 sqlite3_stmt *pWriteAux;
181964
181965 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
181966};
181967
181968/* Possible values for Rtree.eCoordType: */
181969#define RTREE_COORD_REAL32 0
181970#define RTREE_COORD_INT32 1
181971
181972/*
181973** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
181974** only deal with integer coordinates. No floating point operations
181975** will be done.
181976*/
181977#ifdef SQLITE_RTREE_INT_ONLY
181978 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
181979 typedef int RtreeValue; /* Low accuracy coordinate */
181980# define RTREE_ZERO 0
181981#else
181982 typedef double RtreeDValue; /* High accuracy coordinate */
181983 typedef float RtreeValue; /* Low accuracy coordinate */
181984# define RTREE_ZERO 0.0
181985#endif
181986
181987/*
181988** Set the Rtree.bCorrupt flag
181989*/
181990#ifdef SQLITE_DEBUG
181991# define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
181992#else
181993# define RTREE_IS_CORRUPT(X)
181994#endif
181995
181996/*
181997** When doing a search of an r-tree, instances of the following structure
181998** record intermediate results from the tree walk.
181999**
182000** The id is always a node-id. For iLevel>=1 the id is the node-id of
182001** the node that the RtreeSearchPoint represents. When iLevel==0, however,
182002** the id is of the parent node and the cell that RtreeSearchPoint
182003** represents is the iCell-th entry in the parent node.
182004*/
182005struct RtreeSearchPoint {
182006 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
182007 sqlite3_int64 id; /* Node ID */
182008 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
182009 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
182010 u8 iCell; /* Cell index within the node */
182011};
182012
182013/*
182014** The minimum number of cells allowed for a node is a third of the
182015** maximum. In Gutman's notation:
182016**
182017** m = M/3
182018**
182019** If an R*-tree "Reinsert" operation is required, the same number of
182020** cells are removed from the overfull node and reinserted into the tree.
182021*/
182022#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
182023#define RTREE_REINSERT(p) RTREE_MINCELLS(p)
182024#define RTREE_MAXCELLS 51
182025
182026/*
182027** The smallest possible node-size is (512-64)==448 bytes. And the largest
182028** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
182029** Therefore all non-root nodes must contain at least 3 entries. Since
182030** 3^40 is greater than 2^64, an r-tree structure always has a depth of
182031** 40 or less.
182032*/
182033#define RTREE_MAX_DEPTH 40
182034
182035
182036/*
182037** Number of entries in the cursor RtreeNode cache. The first entry is
182038** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
182039** entries cache the RtreeNode for the first elements of the priority queue.
182040*/
182041#define RTREE_CACHE_SZ 5
182042
182043/*
182044** An rtree cursor object.
182045*/
182046struct RtreeCursor {
182047 sqlite3_vtab_cursor base; /* Base class. Must be first */
182048 u8 atEOF; /* True if at end of search */
182049 u8 bPoint; /* True if sPoint is valid */
182050 u8 bAuxValid; /* True if pReadAux is valid */
182051 int iStrategy; /* Copy of idxNum search parameter */
182052 int nConstraint; /* Number of entries in aConstraint */
182053 RtreeConstraint *aConstraint; /* Search constraints. */
182054 int nPointAlloc; /* Number of slots allocated for aPoint[] */
182055 int nPoint; /* Number of slots used in aPoint[] */
182056 int mxLevel; /* iLevel value for root of the tree */
182057 RtreeSearchPoint *aPoint; /* Priority queue for search points */
182058 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
182059 RtreeSearchPoint sPoint; /* Cached next search point */
182060 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
182061 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
182062};
182063
182064/* Return the Rtree of a RtreeCursor */
182065#define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
182066
182067/*
182068** A coordinate can be either a floating point number or a integer. All
182069** coordinates within a single R-Tree are always of the same time.
182070*/
182071union RtreeCoord {
182072 RtreeValue f; /* Floating point value */
182073 int i; /* Integer value */
182074 u32 u; /* Unsigned for byte-order conversions */
182075};
182076
182077/*
182078** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
182079** formatted as a RtreeDValue (double or int64). This macro assumes that local
182080** variable pRtree points to the Rtree structure associated with the
182081** RtreeCoord.
182082*/
182083#ifdef SQLITE_RTREE_INT_ONLY
182084# define DCOORD(coord) ((RtreeDValue)coord.i)
182085#else
182086# define DCOORD(coord) ( \
182087 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
182088 ((double)coord.f) : \
182089 ((double)coord.i) \
182090 )
182091#endif
182092
182093/*
182094** A search constraint.
182095*/
182096struct RtreeConstraint {
182097 int iCoord; /* Index of constrained coordinate */
182098 int op; /* Constraining operation */
182099 union {
182100 RtreeDValue rValue; /* Constraint value. */
182101 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
182102 int (*xQueryFunc)(sqlite3_rtree_query_info*);
182103 } u;
182104 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
182105};
182106
182107/* Possible values for RtreeConstraint.op */
182108#define RTREE_EQ 0x41 /* A */
182109#define RTREE_LE 0x42 /* B */
182110#define RTREE_LT 0x43 /* C */
182111#define RTREE_GE 0x44 /* D */
182112#define RTREE_GT 0x45 /* E */
182113#define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
182114#define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
182115
182116
182117/*
182118** An rtree structure node.
182119*/
182120struct RtreeNode {
182121 RtreeNode *pParent; /* Parent node */
182122 i64 iNode; /* The node number */
182123 int nRef; /* Number of references to this node */
182124 int isDirty; /* True if the node needs to be written to disk */
182125 u8 *zData; /* Content of the node, as should be on disk */
182126 RtreeNode *pNext; /* Next node in this hash collision chain */
182127};
182128
182129/* Return the number of cells in a node */
182130#define NCELL(pNode) readInt16(&(pNode)->zData[2])
182131
182132/*
182133** A single cell from a node, deserialized
182134*/
182135struct RtreeCell {
182136 i64 iRowid; /* Node or entry ID */
182137 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */
182138};
182139
182140
182141/*
182142** This object becomes the sqlite3_user_data() for the SQL functions
182143** that are created by sqlite3_rtree_geometry_callback() and
182144** sqlite3_rtree_query_callback() and which appear on the right of MATCH
182145** operators in order to constrain a search.
182146**
182147** xGeom and xQueryFunc are the callback functions. Exactly one of
182148** xGeom and xQueryFunc fields is non-NULL, depending on whether the
182149** SQL function was created using sqlite3_rtree_geometry_callback() or
182150** sqlite3_rtree_query_callback().
182151**
182152** This object is deleted automatically by the destructor mechanism in
182153** sqlite3_create_function_v2().
182154*/
182155struct RtreeGeomCallback {
182156 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
182157 int (*xQueryFunc)(sqlite3_rtree_query_info*);
182158 void (*xDestructor)(void*);
182159 void *pContext;
182160};
182161
182162/*
182163** An instance of this structure (in the form of a BLOB) is returned by
182164** the SQL functions that sqlite3_rtree_geometry_callback() and
182165** sqlite3_rtree_query_callback() create, and is read as the right-hand
182166** operand to the MATCH operator of an R-Tree.
182167*/
182168struct RtreeMatchArg {
182169 u32 iSize; /* Size of this object */
182170 RtreeGeomCallback cb; /* Info about the callback functions */
182171 int nParam; /* Number of parameters to the SQL function */
182172 sqlite3_value **apSqlParam; /* Original SQL parameter values */
182173 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
182174};
182175
182176#ifndef MAX
182177# define MAX(x,y)((x)>(y)?(x):(y)) ((x) < (y) ? (y) : (x))
182178#endif
182179#ifndef MIN
182180# define MIN(x,y)((x)<(y)?(x):(y)) ((x) > (y) ? (y) : (x))
182181#endif
182182
182183/* What version of GCC is being used. 0 means GCC is not being used .
182184** Note that the GCC_VERSION macro will also be set correctly when using
182185** clang, since clang works hard to be gcc compatible. So the gcc
182186** optimizations will also work when compiling with clang.
182187*/
182188#ifndef GCC_VERSION(4*1000000+2*1000+1)
182189#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
182190# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
182191#else
182192# define GCC_VERSION(4*1000000+2*1000+1) 0
182193#endif
182194#endif
182195
182196/* The testcase() macro should already be defined in the amalgamation. If
182197** it is not, make it a no-op.
182198*/
182199#ifndef SQLITE_AMALGAMATION1
182200# define testcase(X)
182201#endif
182202
182203/*
182204** Macros to determine whether the machine is big or little endian,
182205** and whether or not that determination is run-time or compile-time.
182206**
182207** For best performance, an attempt is made to guess at the byte-order
182208** using C-preprocessor macros. If that is unsuccessful, or if
182209** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
182210** at run-time.
182211*/
182212#ifndef SQLITE_BYTEORDER1234
182213#if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
182214 defined(__x86_641) || defined(__x86_64__1) || defined(_M_X64) || \
182215 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
182216 defined(__arm__)
182217# define SQLITE_BYTEORDER1234 1234
182218#elif defined(sparc) || defined(__ppc__)
182219# define SQLITE_BYTEORDER1234 4321
182220#else
182221# define SQLITE_BYTEORDER1234 0 /* 0 means "unknown at compile-time" */
182222#endif
182223#endif
182224
182225
182226/* What version of MSVC is being used. 0 means MSVC is not being used */
182227#ifndef MSVC_VERSION0
182228#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
182229# define MSVC_VERSION0 _MSC_VER
182230#else
182231# define MSVC_VERSION0 0
182232#endif
182233#endif
182234
182235/*
182236** Functions to deserialize a 16 bit integer, 32 bit real number and
182237** 64 bit integer. The deserialized value is returned.
182238*/
182239static int readInt16(u8 *p){
182240 return (p[0]<<8) + p[1];
182241}
182242static void readCoord(u8 *p, RtreeCoord *pCoord){
182243 assert( ((((char*)p) - (char*)0)&3)==0 )((void) (0)); /* p is always 4-byte aligned */
182244#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
182245 pCoord->u = _byteswap_ulong(*(u32*)p);
182246#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
182247 pCoord->u = __builtin_bswap32(*(u32*)p);
182248#elif SQLITE_BYTEORDER1234==4321
182249 pCoord->u = *(u32*)p;
182250#else
182251 pCoord->u = (
182252 (((u32)p[0]) << 24) +
182253 (((u32)p[1]) << 16) +
182254 (((u32)p[2]) << 8) +
182255 (((u32)p[3]) << 0)
182256 );
182257#endif
182258}
182259static i64 readInt64(u8 *p){
182260#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
182261 u64 x;
182262 memcpy(&x, p, 8);
182263 return (i64)_byteswap_uint64(x);
182264#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
182265 u64 x;
182266 memcpy(&x, p, 8);
182267 return (i64)__builtin_bswap64(x);
182268#elif SQLITE_BYTEORDER1234==4321
182269 i64 x;
182270 memcpy(&x, p, 8);
182271 return x;
182272#else
182273 return (i64)(
182274 (((u64)p[0]) << 56) +
182275 (((u64)p[1]) << 48) +
182276 (((u64)p[2]) << 40) +
182277 (((u64)p[3]) << 32) +
182278 (((u64)p[4]) << 24) +
182279 (((u64)p[5]) << 16) +
182280 (((u64)p[6]) << 8) +
182281 (((u64)p[7]) << 0)
182282 );
182283#endif
182284}
182285
182286/*
182287** Functions to serialize a 16 bit integer, 32 bit real number and
182288** 64 bit integer. The value returned is the number of bytes written
182289** to the argument buffer (always 2, 4 and 8 respectively).
182290*/
182291static void writeInt16(u8 *p, int i){
182292 p[0] = (i>> 8)&0xFF;
182293 p[1] = (i>> 0)&0xFF;
182294}
182295static int writeCoord(u8 *p, RtreeCoord *pCoord){
182296 u32 i;
182297 assert( ((((char*)p) - (char*)0)&3)==0 )((void) (0)); /* p is always 4-byte aligned */
182298 assert( sizeof(RtreeCoord)==4 )((void) (0));
182299 assert( sizeof(u32)==4 )((void) (0));
182300#if SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
182301 i = __builtin_bswap32(pCoord->u);
182302 memcpy(p, &i, 4);
182303#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
182304 i = _byteswap_ulong(pCoord->u);
182305 memcpy(p, &i, 4);
182306#elif SQLITE_BYTEORDER1234==4321
182307 i = pCoord->u;
182308 memcpy(p, &i, 4);
182309#else
182310 i = pCoord->u;
182311 p[0] = (i>>24)&0xFF;
182312 p[1] = (i>>16)&0xFF;
182313 p[2] = (i>> 8)&0xFF;
182314 p[3] = (i>> 0)&0xFF;
182315#endif
182316 return 4;
182317}
182318static int writeInt64(u8 *p, i64 i){
182319#if SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
182320 i = (i64)__builtin_bswap64((u64)i);
182321 memcpy(p, &i, 8);
182322#elif SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
182323 i = (i64)_byteswap_uint64((u64)i);
182324 memcpy(p, &i, 8);
182325#elif SQLITE_BYTEORDER1234==4321
182326 memcpy(p, &i, 8);
182327#else
182328 p[0] = (i>>56)&0xFF;
182329 p[1] = (i>>48)&0xFF;
182330 p[2] = (i>>40)&0xFF;
182331 p[3] = (i>>32)&0xFF;
182332 p[4] = (i>>24)&0xFF;
182333 p[5] = (i>>16)&0xFF;
182334 p[6] = (i>> 8)&0xFF;
182335 p[7] = (i>> 0)&0xFF;
182336#endif
182337 return 8;
182338}
182339
182340/*
182341** Increment the reference count of node p.
182342*/
182343static void nodeReference(RtreeNode *p){
182344 if( p ){
182345 assert( p->nRef>0 )((void) (0));
182346 p->nRef++;
182347 }
182348}
182349
182350/*
182351** Clear the content of node p (set all bytes to 0x00).
182352*/
182353static void nodeZero(Rtree *pRtree, RtreeNode *p){
182354 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
182355 p->isDirty = 1;
182356}
182357
182358/*
182359** Given a node number iNode, return the corresponding key to use
182360** in the Rtree.aHash table.
182361*/
182362static unsigned int nodeHash(i64 iNode){
182363 return ((unsigned)iNode) % HASHSIZE;
182364}
182365
182366/*
182367** Search the node hash table for node iNode. If found, return a pointer
182368** to it. Otherwise, return 0.
182369*/
182370static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
182371 RtreeNode *p;
182372 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
182373 return p;
182374}
182375
182376/*
182377** Add node pNode to the node hash table.
182378*/
182379static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
182380 int iHash;
182381 assert( pNode->pNext==0 )((void) (0));
182382 iHash = nodeHash(pNode->iNode);
182383 pNode->pNext = pRtree->aHash[iHash];
182384 pRtree->aHash[iHash] = pNode;
182385}
182386
182387/*
182388** Remove node pNode from the node hash table.
182389*/
182390static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
182391 RtreeNode **pp;
182392 if( pNode->iNode!=0 ){
182393 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
182394 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp)((void) (0)); }
182395 *pp = pNode->pNext;
182396 pNode->pNext = 0;
182397 }
182398}
182399
182400/*
182401** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
182402** indicating that node has not yet been assigned a node number. It is
182403** assigned a node number when nodeWrite() is called to write the
182404** node contents out to the database.
182405*/
182406static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
182407 RtreeNode *pNode;
182408 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode) + pRtree->iNodeSize);
182409 if( pNode ){
182410 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
182411 pNode->zData = (u8 *)&pNode[1];
182412 pNode->nRef = 1;
182413 pRtree->nNodeRef++;
182414 pNode->pParent = pParent;
182415 pNode->isDirty = 1;
182416 nodeReference(pParent);
182417 }
182418 return pNode;
182419}
182420
182421/*
182422** Clear the Rtree.pNodeBlob object
182423*/
182424static void nodeBlobReset(Rtree *pRtree){
182425 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){
182426 sqlite3_blob *pBlob = pRtree->pNodeBlob;
182427 pRtree->pNodeBlob = 0;
182428 sqlite3_blob_close(pBlob);
182429 }
182430}
182431
182432/*
182433** Check to see if pNode is the same as pParent or any of the parents
182434** of pParent.
182435*/
182436static int nodeInParentChain(const RtreeNode *pNode, const RtreeNode *pParent){
182437 do{
182438 if( pNode==pParent ) return 1;
182439 pParent = pParent->pParent;
182440 }while( pParent );
182441 return 0;
182442}
182443
182444/*
182445** Obtain a reference to an r-tree node.
182446*/
182447static int nodeAcquire(
182448 Rtree *pRtree, /* R-tree structure */
182449 i64 iNode, /* Node number to load */
182450 RtreeNode *pParent, /* Either the parent node or NULL */
182451 RtreeNode **ppNode /* OUT: Acquired node */
182452){
182453 int rc = SQLITE_OK0;
182454 RtreeNode *pNode = 0;
182455
182456 /* Check if the requested node is already in the hash table. If so,
182457 ** increase its reference count and return it.
182458 */
182459 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
182460 assert( !pParent || !pNode->pParent || pNode->pParent==pParent )((void) (0));
182461 if( pParent && !pNode->pParent ){
182462 if( nodeInParentChain(pNode, pParent) ){
182463 RTREE_IS_CORRUPT(pRtree);
182464 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
182465 }
182466 pParent->nRef++;
182467 pNode->pParent = pParent;
182468 }
182469 pNode->nRef++;
182470 *ppNode = pNode;
182471 return SQLITE_OK0;
182472 }
182473
182474 if( pRtree->pNodeBlob ){
182475 sqlite3_blob *pBlob = pRtree->pNodeBlob;
182476 pRtree->pNodeBlob = 0;
182477 rc = sqlite3_blob_reopen(pBlob, iNode);
182478 pRtree->pNodeBlob = pBlob;
182479 if( rc ){
182480 nodeBlobReset(pRtree);
182481 if( rc==SQLITE_NOMEM7 ) return SQLITE_NOMEM7;
182482 }
182483 }
182484 if( pRtree->pNodeBlob==0 ){
182485 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName);
182486 if( zTab==0 ) return SQLITE_NOMEM7;
182487 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0,
182488 &pRtree->pNodeBlob);
182489 sqlite3_free(zTab);
182490 }
182491 if( rc ){
182492 nodeBlobReset(pRtree);
182493 *ppNode = 0;
182494 /* If unable to open an sqlite3_blob on the desired row, that can only
182495 ** be because the shadow tables hold erroneous data. */
182496 if( rc==SQLITE_ERROR1 ){
182497 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
182498 RTREE_IS_CORRUPT(pRtree);
182499 }
182500 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
182501 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode)+pRtree->iNodeSize);
182502 if( !pNode ){
182503 rc = SQLITE_NOMEM7;
182504 }else{
182505 pNode->pParent = pParent;
182506 pNode->zData = (u8 *)&pNode[1];
182507 pNode->nRef = 1;
182508 pRtree->nNodeRef++;
182509 pNode->iNode = iNode;
182510 pNode->isDirty = 0;
182511 pNode->pNext = 0;
182512 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
182513 pRtree->iNodeSize, 0);
182514 }
182515 }
182516
182517 /* If the root node was just loaded, set pRtree->iDepth to the height
182518 ** of the r-tree structure. A height of zero means all data is stored on
182519 ** the root node. A height of one means the children of the root node
182520 ** are the leaves, and so on. If the depth as specified on the root node
182521 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
182522 */
182523 if( pNode && iNode==1 ){
182524 pRtree->iDepth = readInt16(pNode->zData);
182525 if( pRtree->iDepth>RTREE_MAX_DEPTH ){
182526 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
182527 RTREE_IS_CORRUPT(pRtree);
182528 }
182529 }
182530
182531 /* If no error has occurred so far, check if the "number of entries"
182532 ** field on the node is too large. If so, set the return code to
182533 ** SQLITE_CORRUPT_VTAB.
182534 */
182535 if( pNode && rc==SQLITE_OK0 ){
182536 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
182537 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
182538 RTREE_IS_CORRUPT(pRtree);
182539 }
182540 }
182541
182542 if( rc==SQLITE_OK0 ){
182543 if( pNode!=0 ){
182544 nodeReference(pParent);
182545 nodeHashInsert(pRtree, pNode);
182546 }else{
182547 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
182548 RTREE_IS_CORRUPT(pRtree);
182549 }
182550 *ppNode = pNode;
182551 }else{
182552 if( pNode ){
182553 pRtree->nNodeRef--;
182554 sqlite3_free(pNode);
182555 }
182556 *ppNode = 0;
182557 }
182558
182559 return rc;
182560}
182561
182562/*
182563** Overwrite cell iCell of node pNode with the contents of pCell.
182564*/
182565static void nodeOverwriteCell(
182566 Rtree *pRtree, /* The overall R-Tree */
182567 RtreeNode *pNode, /* The node into which the cell is to be written */
182568 RtreeCell *pCell, /* The cell to write */
182569 int iCell /* Index into pNode into which pCell is written */
182570){
182571 int ii;
182572 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
182573 p += writeInt64(p, pCell->iRowid);
182574 for(ii=0; ii<pRtree->nDim2; ii++){
182575 p += writeCoord(p, &pCell->aCoord[ii]);
182576 }
182577 pNode->isDirty = 1;
182578}
182579
182580/*
182581** Remove the cell with index iCell from node pNode.
182582*/
182583static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
182584 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
182585 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
182586 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
182587 memmove(pDst, pSrc, nByte);
182588 writeInt16(&pNode->zData[2], NCELL(pNode)-1);
182589 pNode->isDirty = 1;
182590}
182591
182592/*
182593** Insert the contents of cell pCell into node pNode. If the insert
182594** is successful, return SQLITE_OK.
182595**
182596** If there is not enough free space in pNode, return SQLITE_FULL.
182597*/
182598static int nodeInsertCell(
182599 Rtree *pRtree, /* The overall R-Tree */
182600 RtreeNode *pNode, /* Write new cell into this node */
182601 RtreeCell *pCell /* The cell to be inserted */
182602){
182603 int nCell; /* Current number of cells in pNode */
182604 int nMaxCell; /* Maximum number of cells for pNode */
182605
182606 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
182607 nCell = NCELL(pNode);
182608
182609 assert( nCell<=nMaxCell )((void) (0));
182610 if( nCell<nMaxCell ){
182611 nodeOverwriteCell(pRtree, pNode, pCell, nCell);
182612 writeInt16(&pNode->zData[2], nCell+1);
182613 pNode->isDirty = 1;
182614 }
182615
182616 return (nCell==nMaxCell);
182617}
182618
182619/*
182620** If the node is dirty, write it out to the database.
182621*/
182622static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
182623 int rc = SQLITE_OK0;
182624 if( pNode->isDirty ){
182625 sqlite3_stmt *p = pRtree->pWriteNode;
182626 if( pNode->iNode ){
182627 sqlite3_bind_int64(p, 1, pNode->iNode);
182628 }else{
182629 sqlite3_bind_null(p, 1);
182630 }
182631 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC((sqlite3_destructor_type)0));
182632 sqlite3_step(p);
182633 pNode->isDirty = 0;
182634 rc = sqlite3_reset(p);
182635 sqlite3_bind_null(p, 2);
182636 if( pNode->iNode==0 && rc==SQLITE_OK0 ){
182637 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
182638 nodeHashInsert(pRtree, pNode);
182639 }
182640 }
182641 return rc;
182642}
182643
182644/*
182645** Release a reference to a node. If the node is dirty and the reference
182646** count drops to zero, the node data is written to the database.
182647*/
182648static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
182649 int rc = SQLITE_OK0;
182650 if( pNode ){
182651 assert( pNode->nRef>0 )((void) (0));
182652 assert( pRtree->nNodeRef>0 )((void) (0));
182653 pNode->nRef--;
182654 if( pNode->nRef==0 ){
182655 pRtree->nNodeRef--;
182656 if( pNode->iNode==1 ){
182657 pRtree->iDepth = -1;
182658 }
182659 if( pNode->pParent ){
182660 rc = nodeRelease(pRtree, pNode->pParent);
182661 }
182662 if( rc==SQLITE_OK0 ){
182663 rc = nodeWrite(pRtree, pNode);
182664 }
182665 nodeHashDelete(pRtree, pNode);
182666 sqlite3_free(pNode);
182667 }
182668 }
182669 return rc;
182670}
182671
182672/*
182673** Return the 64-bit integer value associated with cell iCell of
182674** node pNode. If pNode is a leaf node, this is a rowid. If it is
182675** an internal node, then the 64-bit integer is a child page number.
182676*/
182677static i64 nodeGetRowid(
182678 Rtree *pRtree, /* The overall R-Tree */
182679 RtreeNode *pNode, /* The node from which to extract the ID */
182680 int iCell /* The cell index from which to extract the ID */
182681){
182682 assert( iCell<NCELL(pNode) )((void) (0));
182683 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
182684}
182685
182686/*
182687** Return coordinate iCoord from cell iCell in node pNode.
182688*/
182689static void nodeGetCoord(
182690 Rtree *pRtree, /* The overall R-Tree */
182691 RtreeNode *pNode, /* The node from which to extract a coordinate */
182692 int iCell, /* The index of the cell within the node */
182693 int iCoord, /* Which coordinate to extract */
182694 RtreeCoord *pCoord /* OUT: Space to write result to */
182695){
182696 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
182697}
182698
182699/*
182700** Deserialize cell iCell of node pNode. Populate the structure pointed
182701** to by pCell with the results.
182702*/
182703static void nodeGetCell(
182704 Rtree *pRtree, /* The overall R-Tree */
182705 RtreeNode *pNode, /* The node containing the cell to be read */
182706 int iCell, /* Index of the cell within the node */
182707 RtreeCell *pCell /* OUT: Write the cell contents here */
182708){
182709 u8 *pData;
182710 RtreeCoord *pCoord;
182711 int ii = 0;
182712 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
182713 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
182714 pCoord = pCell->aCoord;
182715 do{
182716 readCoord(pData, &pCoord[ii]);
182717 readCoord(pData+4, &pCoord[ii+1]);
182718 pData += 8;
182719 ii += 2;
182720 }while( ii<pRtree->nDim2 );
182721}
182722
182723
182724/* Forward declaration for the function that does the work of
182725** the virtual table module xCreate() and xConnect() methods.
182726*/
182727static int rtreeInit(
182728 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
182729);
182730
182731/*
182732** Rtree virtual table module xCreate method.
182733*/
182734static int rtreeCreate(
182735 sqlite3 *db,
182736 void *pAux,
182737 int argc, const char *const*argv,
182738 sqlite3_vtab **ppVtab,
182739 char **pzErr
182740){
182741 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
182742}
182743
182744/*
182745** Rtree virtual table module xConnect method.
182746*/
182747static int rtreeConnect(
182748 sqlite3 *db,
182749 void *pAux,
182750 int argc, const char *const*argv,
182751 sqlite3_vtab **ppVtab,
182752 char **pzErr
182753){
182754 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
182755}
182756
182757/*
182758** Increment the r-tree reference count.
182759*/
182760static void rtreeReference(Rtree *pRtree){
182761 pRtree->nBusy++;
182762}
182763
182764/*
182765** Decrement the r-tree reference count. When the reference count reaches
182766** zero the structure is deleted.
182767*/
182768static void rtreeRelease(Rtree *pRtree){
182769 pRtree->nBusy--;
182770 if( pRtree->nBusy==0 ){
182771 pRtree->inWrTrans = 0;
182772 assert( pRtree->nCursor==0 )((void) (0));
182773 nodeBlobReset(pRtree);
182774 assert( pRtree->nNodeRef==0 || pRtree->bCorrupt )((void) (0));
182775 sqlite3_finalize(pRtree->pWriteNode);
182776 sqlite3_finalize(pRtree->pDeleteNode);
182777 sqlite3_finalize(pRtree->pReadRowid);
182778 sqlite3_finalize(pRtree->pWriteRowid);
182779 sqlite3_finalize(pRtree->pDeleteRowid);
182780 sqlite3_finalize(pRtree->pReadParent);
182781 sqlite3_finalize(pRtree->pWriteParent);
182782 sqlite3_finalize(pRtree->pDeleteParent);
182783 sqlite3_finalize(pRtree->pWriteAux);
182784 sqlite3_free(pRtree->zReadAuxSql);
182785 sqlite3_free(pRtree);
182786 }
182787}
182788
182789/*
182790** Rtree virtual table module xDisconnect method.
182791*/
182792static int rtreeDisconnect(sqlite3_vtab *pVtab){
182793 rtreeRelease((Rtree *)pVtab);
182794 return SQLITE_OK0;
182795}
182796
182797/*
182798** Rtree virtual table module xDestroy method.
182799*/
182800static int rtreeDestroy(sqlite3_vtab *pVtab){
182801 Rtree *pRtree = (Rtree *)pVtab;
182802 int rc;
182803 char *zCreate = sqlite3_mprintf(
182804 "DROP TABLE '%q'.'%q_node';"
182805 "DROP TABLE '%q'.'%q_rowid';"
182806 "DROP TABLE '%q'.'%q_parent';",
182807 pRtree->zDb, pRtree->zName,
182808 pRtree->zDb, pRtree->zName,
182809 pRtree->zDb, pRtree->zName
182810 );
182811 if( !zCreate ){
182812 rc = SQLITE_NOMEM7;
182813 }else{
182814 nodeBlobReset(pRtree);
182815 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
182816 sqlite3_free(zCreate);
182817 }
182818 if( rc==SQLITE_OK0 ){
182819 rtreeRelease(pRtree);
182820 }
182821
182822 return rc;
182823}
182824
182825/*
182826** Rtree virtual table module xOpen method.
182827*/
182828static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
182829 int rc = SQLITE_NOMEM7;
182830 Rtree *pRtree = (Rtree *)pVTab;
182831 RtreeCursor *pCsr;
182832
182833 pCsr = (RtreeCursor *)sqlite3_malloc64(sizeof(RtreeCursor));
182834 if( pCsr ){
182835 memset(pCsr, 0, sizeof(RtreeCursor));
182836 pCsr->base.pVtab = pVTab;
182837 rc = SQLITE_OK0;
182838 pRtree->nCursor++;
182839 }
182840 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
182841
182842 return rc;
182843}
182844
182845
182846/*
182847** Free the RtreeCursor.aConstraint[] array and its contents.
182848*/
182849static void freeCursorConstraints(RtreeCursor *pCsr){
182850 if( pCsr->aConstraint ){
182851 int i; /* Used to iterate through constraint array */
182852 for(i=0; i<pCsr->nConstraint; i++){
182853 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
182854 if( pInfo ){
182855 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
182856 sqlite3_free(pInfo);
182857 }
182858 }
182859 sqlite3_free(pCsr->aConstraint);
182860 pCsr->aConstraint = 0;
182861 }
182862}
182863
182864/*
182865** Rtree virtual table module xClose method.
182866*/
182867static int rtreeClose(sqlite3_vtab_cursor *cur){
182868 Rtree *pRtree = (Rtree *)(cur->pVtab);
182869 int ii;
182870 RtreeCursor *pCsr = (RtreeCursor *)cur;
182871 assert( pRtree->nCursor>0 )((void) (0));
182872 freeCursorConstraints(pCsr);
182873 sqlite3_finalize(pCsr->pReadAux);
182874 sqlite3_free(pCsr->aPoint);
182875 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
182876 sqlite3_free(pCsr);
182877 pRtree->nCursor--;
182878 nodeBlobReset(pRtree);
182879 return SQLITE_OK0;
182880}
182881
182882/*
182883** Rtree virtual table module xEof method.
182884**
182885** Return non-zero if the cursor does not currently point to a valid
182886** record (i.e if the scan has finished), or zero otherwise.
182887*/
182888static int rtreeEof(sqlite3_vtab_cursor *cur){
182889 RtreeCursor *pCsr = (RtreeCursor *)cur;
182890 return pCsr->atEOF;
182891}
182892
182893/*
182894** Convert raw bits from the on-disk RTree record into a coordinate value.
182895** The on-disk format is big-endian and needs to be converted for little-
182896** endian platforms. The on-disk record stores integer coordinates if
182897** eInt is true and it stores 32-bit floating point records if eInt is
182898** false. a[] is the four bytes of the on-disk record to be decoded.
182899** Store the results in "r".
182900**
182901** There are five versions of this macro. The last one is generic. The
182902** other four are various architectures-specific optimizations.
182903*/
182904#if SQLITE_BYTEORDER1234==1234 && MSVC_VERSION0>=1300
182905#define RTREE_DECODE_COORD(eInt, a, r) { \
182906 RtreeCoord c; /* Coordinate decoded */ \
182907 c.u = _byteswap_ulong(*(u32*)a); \
182908 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
182909}
182910#elif SQLITE_BYTEORDER1234==1234 && GCC_VERSION(4*1000000+2*1000+1)>=4003000
182911#define RTREE_DECODE_COORD(eInt, a, r) { \
182912 RtreeCoord c; /* Coordinate decoded */ \
182913 c.u = __builtin_bswap32(*(u32*)a); \
182914 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
182915}
182916#elif SQLITE_BYTEORDER1234==1234
182917#define RTREE_DECODE_COORD(eInt, a, r) { \
182918 RtreeCoord c; /* Coordinate decoded */ \
182919 memcpy(&c.u,a,4); \
182920 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
182921 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
182922 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
182923}
182924#elif SQLITE_BYTEORDER1234==4321
182925#define RTREE_DECODE_COORD(eInt, a, r) { \
182926 RtreeCoord c; /* Coordinate decoded */ \
182927 memcpy(&c.u,a,4); \
182928 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
182929}
182930#else
182931#define RTREE_DECODE_COORD(eInt, a, r) { \
182932 RtreeCoord c; /* Coordinate decoded */ \
182933 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
182934 +((u32)a[2]<<8) + a[3]; \
182935 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
182936}
182937#endif
182938
182939/*
182940** Check the RTree node or entry given by pCellData and p against the MATCH
182941** constraint pConstraint.
182942*/
182943static int rtreeCallbackConstraint(
182944 RtreeConstraint *pConstraint, /* The constraint to test */
182945 int eInt, /* True if RTree holding integer coordinates */
182946 u8 *pCellData, /* Raw cell content */
182947 RtreeSearchPoint *pSearch, /* Container of this cell */
182948 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
182949 int *peWithin /* OUT: visibility of the cell */
182950){
182951 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
182952 int nCoord = pInfo->nCoord; /* No. of coordinates */
182953 int rc; /* Callback return code */
182954 RtreeCoord c; /* Translator union */
182955 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
182956
182957 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY )((void) (0));
182958 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 )((void) (0));
182959
182960 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
182961 pInfo->iRowid = readInt64(pCellData);
182962 }
182963 pCellData += 8;
182964#ifndef SQLITE_RTREE_INT_ONLY
182965 if( eInt==0 ){
182966 switch( nCoord ){
182967 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
182968 readCoord(pCellData+32, &c); aCoord[8] = c.f;
182969 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
182970 readCoord(pCellData+24, &c); aCoord[6] = c.f;
182971 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
182972 readCoord(pCellData+16, &c); aCoord[4] = c.f;
182973 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
182974 readCoord(pCellData+8, &c); aCoord[2] = c.f;
182975 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
182976 readCoord(pCellData, &c); aCoord[0] = c.f;
182977 }
182978 }else
182979#endif
182980 {
182981 switch( nCoord ){
182982 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
182983 readCoord(pCellData+32, &c); aCoord[8] = c.i;
182984 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
182985 readCoord(pCellData+24, &c); aCoord[6] = c.i;
182986 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
182987 readCoord(pCellData+16, &c); aCoord[4] = c.i;
182988 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
182989 readCoord(pCellData+8, &c); aCoord[2] = c.i;
182990 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
182991 readCoord(pCellData, &c); aCoord[0] = c.i;
182992 }
182993 }
182994 if( pConstraint->op==RTREE_MATCH ){
182995 int eWithin = 0;
182996 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
182997 nCoord, aCoord, &eWithin);
182998 if( eWithin==0 ) *peWithin = NOT_WITHIN0;
182999 *prScore = RTREE_ZERO;
183000 }else{
183001 pInfo->aCoord = aCoord;
183002 pInfo->iLevel = pSearch->iLevel - 1;
183003 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
183004 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
183005 rc = pConstraint->u.xQueryFunc(pInfo);
183006 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
183007 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
183008 *prScore = pInfo->rScore;
183009 }
183010 }
183011 return rc;
183012}
183013
183014/*
183015** Check the internal RTree node given by pCellData against constraint p.
183016** If this constraint cannot be satisfied by any child within the node,
183017** set *peWithin to NOT_WITHIN.
183018*/
183019static void rtreeNonleafConstraint(
183020 RtreeConstraint *p, /* The constraint to test */
183021 int eInt, /* True if RTree holds integer coordinates */
183022 u8 *pCellData, /* Raw cell content as appears on disk */
183023 int *peWithin /* Adjust downward, as appropriate */
183024){
183025 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
183026
183027 /* p->iCoord might point to either a lower or upper bound coordinate
183028 ** in a coordinate pair. But make pCellData point to the lower bound.
183029 */
183030 pCellData += 8 + 4*(p->iCoord&0xfe);
183031
183032 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE((void) (0))
183033 || p->op==RTREE_GT || p->op==RTREE_EQ )((void) (0));
183034 assert( ((((char*)pCellData) - (char*)0)&3)==0 )((void) (0)); /* 4-byte aligned */
183035 switch( p->op ){
183036 case RTREE_LE:
183037 case RTREE_LT:
183038 case RTREE_EQ:
183039 RTREE_DECODE_COORD(eInt, pCellData, val);
183040 /* val now holds the lower bound of the coordinate pair */
183041 if( p->u.rValue>=val ) return;
183042 if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */
183043 /* Fall through for the RTREE_EQ case */
183044
183045 default: /* RTREE_GT or RTREE_GE, or fallthrough of RTREE_EQ */
183046 pCellData += 4;
183047 RTREE_DECODE_COORD(eInt, pCellData, val);
183048 /* val now holds the upper bound of the coordinate pair */
183049 if( p->u.rValue<=val ) return;
183050 }
183051 *peWithin = NOT_WITHIN0;
183052}
183053
183054/*
183055** Check the leaf RTree cell given by pCellData against constraint p.
183056** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
183057** If the constraint is satisfied, leave *peWithin unchanged.
183058**
183059** The constraint is of the form: xN op $val
183060**
183061** The op is given by p->op. The xN is p->iCoord-th coordinate in
183062** pCellData. $val is given by p->u.rValue.
183063*/
183064static void rtreeLeafConstraint(
183065 RtreeConstraint *p, /* The constraint to test */
183066 int eInt, /* True if RTree holds integer coordinates */
183067 u8 *pCellData, /* Raw cell content as appears on disk */
183068 int *peWithin /* Adjust downward, as appropriate */
183069){
183070 RtreeDValue xN; /* Coordinate value converted to a double */
183071
183072 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE((void) (0))
183073 || p->op==RTREE_GT || p->op==RTREE_EQ )((void) (0));
183074 pCellData += 8 + p->iCoord*4;
183075 assert( ((((char*)pCellData) - (char*)0)&3)==0 )((void) (0)); /* 4-byte aligned */
183076 RTREE_DECODE_COORD(eInt, pCellData, xN);
183077 switch( p->op ){
183078 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
183079 case RTREE_LT: if( xN < p->u.rValue ) return; break;
183080 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
183081 case RTREE_GT: if( xN > p->u.rValue ) return; break;
183082 default: if( xN == p->u.rValue ) return; break;
183083 }
183084 *peWithin = NOT_WITHIN0;
183085}
183086
183087/*
183088** One of the cells in node pNode is guaranteed to have a 64-bit
183089** integer value equal to iRowid. Return the index of this cell.
183090*/
183091static int nodeRowidIndex(
183092 Rtree *pRtree,
183093 RtreeNode *pNode,
183094 i64 iRowid,
183095 int *piIndex
183096){
183097 int ii;
183098 int nCell = NCELL(pNode);
183099 assert( nCell<200 )((void) (0));
183100 for(ii=0; ii<nCell; ii++){
183101 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
183102 *piIndex = ii;
183103 return SQLITE_OK0;
183104 }
183105 }
183106 RTREE_IS_CORRUPT(pRtree);
183107 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
183108}
183109
183110/*
183111** Return the index of the cell containing a pointer to node pNode
183112** in its parent. If pNode is the root node, return -1.
183113*/
183114static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
183115 RtreeNode *pParent = pNode->pParent;
183116 if( pParent ){
183117 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
183118 }
183119 *piIndex = -1;
183120 return SQLITE_OK0;
183121}
183122
183123/*
183124** Compare two search points. Return negative, zero, or positive if the first
183125** is less than, equal to, or greater than the second.
183126**
183127** The rScore is the primary key. Smaller rScore values come first.
183128** If the rScore is a tie, then use iLevel as the tie breaker with smaller
183129** iLevel values coming first. In this way, if rScore is the same for all
183130** SearchPoints, then iLevel becomes the deciding factor and the result
183131** is a depth-first search, which is the desired default behavior.
183132*/
183133static int rtreeSearchPointCompare(
183134 const RtreeSearchPoint *pA,
183135 const RtreeSearchPoint *pB
183136){
183137 if( pA->rScore<pB->rScore ) return -1;
183138 if( pA->rScore>pB->rScore ) return +1;
183139 if( pA->iLevel<pB->iLevel ) return -1;
183140 if( pA->iLevel>pB->iLevel ) return +1;
183141 return 0;
183142}
183143
183144/*
183145** Interchange two search points in a cursor.
183146*/
183147static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
183148 RtreeSearchPoint t = p->aPoint[i];
183149 assert( i<j )((void) (0));
183150 p->aPoint[i] = p->aPoint[j];
183151 p->aPoint[j] = t;
183152 i++; j++;
183153 if( i<RTREE_CACHE_SZ ){
183154 if( j>=RTREE_CACHE_SZ ){
183155 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
183156 p->aNode[i] = 0;
183157 }else{
183158 RtreeNode *pTemp = p->aNode[i];
183159 p->aNode[i] = p->aNode[j];
183160 p->aNode[j] = pTemp;
183161 }
183162 }
183163}
183164
183165/*
183166** Return the search point with the lowest current score.
183167*/
183168static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
183169 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
183170}
183171
183172/*
183173** Get the RtreeNode for the search point with the lowest score.
183174*/
183175static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
183176 sqlite3_int64 id;
183177 int ii = 1 - pCur->bPoint;
183178 assert( ii==0 || ii==1 )((void) (0));
183179 assert( pCur->bPoint || pCur->nPoint )((void) (0));
183180 if( pCur->aNode[ii]==0 ){
183181 assert( pRC!=0 )((void) (0));
183182 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
183183 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
183184 }
183185 return pCur->aNode[ii];
183186}
183187
183188/*
183189** Push a new element onto the priority queue
183190*/
183191static RtreeSearchPoint *rtreeEnqueue(
183192 RtreeCursor *pCur, /* The cursor */
183193 RtreeDValue rScore, /* Score for the new search point */
183194 u8 iLevel /* Level for the new search point */
183195){
183196 int i, j;
183197 RtreeSearchPoint *pNew;
183198 if( pCur->nPoint>=pCur->nPointAlloc ){
183199 int nNew = pCur->nPointAlloc*2 + 8;
183200 pNew = sqlite3_realloc64(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
183201 if( pNew==0 ) return 0;
183202 pCur->aPoint = pNew;
183203 pCur->nPointAlloc = nNew;
183204 }
183205 i = pCur->nPoint++;
183206 pNew = pCur->aPoint + i;
183207 pNew->rScore = rScore;
183208 pNew->iLevel = iLevel;
183209 assert( iLevel<=RTREE_MAX_DEPTH )((void) (0));
183210 while( i>0 ){
183211 RtreeSearchPoint *pParent;
183212 j = (i-1)/2;
183213 pParent = pCur->aPoint + j;
183214 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
183215 rtreeSearchPointSwap(pCur, j, i);
183216 i = j;
183217 pNew = pParent;
183218 }
183219 return pNew;
183220}
183221
183222/*
183223** Allocate a new RtreeSearchPoint and return a pointer to it. Return
183224** NULL if malloc fails.
183225*/
183226static RtreeSearchPoint *rtreeSearchPointNew(
183227 RtreeCursor *pCur, /* The cursor */
183228 RtreeDValue rScore, /* Score for the new search point */
183229 u8 iLevel /* Level for the new search point */
183230){
183231 RtreeSearchPoint *pNew, *pFirst;
183232 pFirst = rtreeSearchPointFirst(pCur);
183233 pCur->anQueue[iLevel]++;
183234 if( pFirst==0
183235 || pFirst->rScore>rScore
183236 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
183237 ){
183238 if( pCur->bPoint ){
183239 int ii;
183240 pNew = rtreeEnqueue(pCur, rScore, iLevel);
183241 if( pNew==0 ) return 0;
183242 ii = (int)(pNew - pCur->aPoint) + 1;
183243 if( ii<RTREE_CACHE_SZ ){
183244 assert( pCur->aNode[ii]==0 )((void) (0));
183245 pCur->aNode[ii] = pCur->aNode[0];
183246 }else{
183247 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
183248 }
183249 pCur->aNode[0] = 0;
183250 *pNew = pCur->sPoint;
183251 }
183252 pCur->sPoint.rScore = rScore;
183253 pCur->sPoint.iLevel = iLevel;
183254 pCur->bPoint = 1;
183255 return &pCur->sPoint;
183256 }else{
183257 return rtreeEnqueue(pCur, rScore, iLevel);
183258 }
183259}
183260
183261#if 0
183262/* Tracing routines for the RtreeSearchPoint queue */
183263static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
183264 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
183265 printf(" %d.%05lld.%02d %g %d",
183266 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
183267 );
183268 idx++;
183269 if( idx<RTREE_CACHE_SZ ){
183270 printf(" %p\n", pCur->aNode[idx]);
183271 }else{
183272 printf("\n");
183273 }
183274}
183275static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
183276 int ii;
183277 printf("=== %9s ", zPrefix);
183278 if( pCur->bPoint ){
183279 tracePoint(&pCur->sPoint, -1, pCur);
183280 }
183281 for(ii=0; ii<pCur->nPoint; ii++){
183282 if( ii>0 || pCur->bPoint ) printf(" ");
183283 tracePoint(&pCur->aPoint[ii], ii, pCur);
183284 }
183285}
183286# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
183287#else
183288# define RTREE_QUEUE_TRACE(A,B) /* no-op */
183289#endif
183290
183291/* Remove the search point with the lowest current score.
183292*/
183293static void rtreeSearchPointPop(RtreeCursor *p){
183294 int i, j, k, n;
183295 i = 1 - p->bPoint;
183296 assert( i==0 || i==1 )((void) (0));
183297 if( p->aNode[i] ){
183298 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
183299 p->aNode[i] = 0;
183300 }
183301 if( p->bPoint ){
183302 p->anQueue[p->sPoint.iLevel]--;
183303 p->bPoint = 0;
183304 }else if( p->nPoint ){
183305 p->anQueue[p->aPoint[0].iLevel]--;
183306 n = --p->nPoint;
183307 p->aPoint[0] = p->aPoint[n];
183308 if( n<RTREE_CACHE_SZ-1 ){
183309 p->aNode[1] = p->aNode[n+1];
183310 p->aNode[n+1] = 0;
183311 }
183312 i = 0;
183313 while( (j = i*2+1)<n ){
183314 k = j+1;
183315 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
183316 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
183317 rtreeSearchPointSwap(p, i, k);
183318 i = k;
183319 }else{
183320 break;
183321 }
183322 }else{
183323 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
183324 rtreeSearchPointSwap(p, i, j);
183325 i = j;
183326 }else{
183327 break;
183328 }
183329 }
183330 }
183331 }
183332}
183333
183334
183335/*
183336** Continue the search on cursor pCur until the front of the queue
183337** contains an entry suitable for returning as a result-set row,
183338** or until the RtreeSearchPoint queue is empty, indicating that the
183339** query has completed.
183340*/
183341static int rtreeStepToLeaf(RtreeCursor *pCur){
183342 RtreeSearchPoint *p;
183343 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
183344 RtreeNode *pNode;
183345 int eWithin;
183346 int rc = SQLITE_OK0;
183347 int nCell;
183348 int nConstraint = pCur->nConstraint;
183349 int ii;
183350 int eInt;
183351 RtreeSearchPoint x;
183352
183353 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
183354 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
183355 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
183356 if( rc ) return rc;
183357 nCell = NCELL(pNode);
183358 assert( nCell<200 )((void) (0));
183359 while( p->iCell<nCell ){
183360 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
183361 u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
183362 eWithin = FULLY_WITHIN2;
183363 for(ii=0; ii<nConstraint; ii++){
183364 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
183365 if( pConstraint->op>=RTREE_MATCH ){
183366 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
183367 &rScore, &eWithin);
183368 if( rc ) return rc;
183369 }else if( p->iLevel==1 ){
183370 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
183371 }else{
183372 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
183373 }
183374 if( eWithin==NOT_WITHIN0 ) break;
183375 }
183376 p->iCell++;
183377 if( eWithin==NOT_WITHIN0 ) continue;
183378 x.iLevel = p->iLevel - 1;
183379 if( x.iLevel ){
183380 x.id = readInt64(pCellData);
183381 x.iCell = 0;
183382 }else{
183383 x.id = p->id;
183384 x.iCell = p->iCell - 1;
183385 }
183386 if( p->iCell>=nCell ){
183387 RTREE_QUEUE_TRACE(pCur, "POP-S:");
183388 rtreeSearchPointPop(pCur);
183389 }
183390 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
183391 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
183392 if( p==0 ) return SQLITE_NOMEM7;
183393 p->eWithin = (u8)eWithin;
183394 p->id = x.id;
183395 p->iCell = x.iCell;
183396 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
183397 break;
183398 }
183399 if( p->iCell>=nCell ){
183400 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
183401 rtreeSearchPointPop(pCur);
183402 }
183403 }
183404 pCur->atEOF = p==0;
183405 return SQLITE_OK0;
183406}
183407
183408/*
183409** Rtree virtual table module xNext method.
183410*/
183411static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
183412 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
183413 int rc = SQLITE_OK0;
183414
183415 /* Move to the next entry that matches the configured constraints. */
183416 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
183417 if( pCsr->bAuxValid ){
183418 pCsr->bAuxValid = 0;
183419 sqlite3_reset(pCsr->pReadAux);
183420 }
183421 rtreeSearchPointPop(pCsr);
183422 rc = rtreeStepToLeaf(pCsr);
183423 return rc;
183424}
183425
183426/*
183427** Rtree virtual table module xRowid method.
183428*/
183429static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
183430 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
183431 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
183432 int rc = SQLITE_OK0;
183433 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
183434 if( rc==SQLITE_OK0 && p ){
183435 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
183436 }
183437 return rc;
183438}
183439
183440/*
183441** Rtree virtual table module xColumn method.
183442*/
183443static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
183444 Rtree *pRtree = (Rtree *)cur->pVtab;
183445 RtreeCursor *pCsr = (RtreeCursor *)cur;
183446 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
183447 RtreeCoord c;
183448 int rc = SQLITE_OK0;
183449 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
183450
183451 if( rc ) return rc;
183452 if( p==0 ) return SQLITE_OK0;
183453 if( i==0 ){
183454 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
183455 }else if( i<=pRtree->nDim2 ){
183456 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
183457#ifndef SQLITE_RTREE_INT_ONLY
183458 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
183459 sqlite3_result_double(ctx, c.f);
183460 }else
183461#endif
183462 {
183463 assert( pRtree->eCoordType==RTREE_COORD_INT32 )((void) (0));
183464 sqlite3_result_int(ctx, c.i);
183465 }
183466 }else{
183467 if( !pCsr->bAuxValid ){
183468 if( pCsr->pReadAux==0 ){
183469 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
183470 &pCsr->pReadAux, 0);
183471 if( rc ) return rc;
183472 }
183473 sqlite3_bind_int64(pCsr->pReadAux, 1,
183474 nodeGetRowid(pRtree, pNode, p->iCell));
183475 rc = sqlite3_step(pCsr->pReadAux);
183476 if( rc==SQLITE_ROW100 ){
183477 pCsr->bAuxValid = 1;
183478 }else{
183479 sqlite3_reset(pCsr->pReadAux);
183480 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
183481 return rc;
183482 }
183483 }
183484 sqlite3_result_value(ctx,
183485 sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
183486 }
183487 return SQLITE_OK0;
183488}
183489
183490/*
183491** Use nodeAcquire() to obtain the leaf node containing the record with
183492** rowid iRowid. If successful, set *ppLeaf to point to the node and
183493** return SQLITE_OK. If there is no such record in the table, set
183494** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
183495** to zero and return an SQLite error code.
183496*/
183497static int findLeafNode(
183498 Rtree *pRtree, /* RTree to search */
183499 i64 iRowid, /* The rowid searching for */
183500 RtreeNode **ppLeaf, /* Write the node here */
183501 sqlite3_int64 *piNode /* Write the node-id here */
183502){
183503 int rc;
183504 *ppLeaf = 0;
183505 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
183506 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW100 ){
183507 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
183508 if( piNode ) *piNode = iNode;
183509 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
183510 sqlite3_reset(pRtree->pReadRowid);
183511 }else{
183512 rc = sqlite3_reset(pRtree->pReadRowid);
183513 }
183514 return rc;
183515}
183516
183517/*
183518** This function is called to configure the RtreeConstraint object passed
183519** as the second argument for a MATCH constraint. The value passed as the
183520** first argument to this function is the right-hand operand to the MATCH
183521** operator.
183522*/
183523static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
183524 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
183525 sqlite3_rtree_query_info *pInfo; /* Callback information */
183526
183527 pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
183528 if( pSrc==0 ) return SQLITE_ERROR1;
183529 pInfo = (sqlite3_rtree_query_info*)
183530 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
183531 if( !pInfo ) return SQLITE_NOMEM7;
183532 memset(pInfo, 0, sizeof(*pInfo));
183533 pBlob = (RtreeMatchArg*)&pInfo[1];
183534 memcpy(pBlob, pSrc, pSrc->iSize);
183535 pInfo->pContext = pBlob->cb.pContext;
183536 pInfo->nParam = pBlob->nParam;
183537 pInfo->aParam = pBlob->aParam;
183538 pInfo->apSqlParam = pBlob->apSqlParam;
183539
183540 if( pBlob->cb.xGeom ){
183541 pCons->u.xGeom = pBlob->cb.xGeom;
183542 }else{
183543 pCons->op = RTREE_QUERY;
183544 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
183545 }
183546 pCons->pInfo = pInfo;
183547 return SQLITE_OK0;
183548}
183549
183550/*
183551** Rtree virtual table module xFilter method.
183552*/
183553static int rtreeFilter(
183554 sqlite3_vtab_cursor *pVtabCursor,
183555 int idxNum, const char *idxStr,
183556 int argc, sqlite3_value **argv
183557){
183558 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
183559 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
183560 RtreeNode *pRoot = 0;
183561 int ii;
183562 int rc = SQLITE_OK0;
183563 int iCell = 0;
183564 sqlite3_stmt *pStmt;
183565
183566 rtreeReference(pRtree);
183567
183568 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
183569 freeCursorConstraints(pCsr);
183570 sqlite3_free(pCsr->aPoint);
183571 pStmt = pCsr->pReadAux;
183572 memset(pCsr, 0, sizeof(RtreeCursor));
183573 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
183574 pCsr->pReadAux = pStmt;
183575
183576 pCsr->iStrategy = idxNum;
183577 if( idxNum==1 ){
183578 /* Special case - lookup by rowid. */
183579 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
183580 RtreeSearchPoint *p; /* Search point for the leaf */
183581 i64 iRowid = sqlite3_value_int64(argv[0]);
183582 i64 iNode = 0;
183583 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
183584 if( rc==SQLITE_OK0 && pLeaf!=0 ){
183585 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
183586 assert( p!=0 )((void) (0)); /* Always returns pCsr->sPoint */
183587 pCsr->aNode[0] = pLeaf;
183588 p->id = iNode;
183589 p->eWithin = PARTLY_WITHIN1;
183590 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
183591 p->iCell = (u8)iCell;
183592 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
183593 }else{
183594 pCsr->atEOF = 1;
183595 }
183596 }else{
183597 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
183598 ** with the configured constraints.
183599 */
183600 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
183601 if( rc==SQLITE_OK0 && argc>0 ){
183602 pCsr->aConstraint = sqlite3_malloc64(sizeof(RtreeConstraint)*argc);
183603 pCsr->nConstraint = argc;
183604 if( !pCsr->aConstraint ){
183605 rc = SQLITE_NOMEM7;
183606 }else{
183607 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
183608 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
183609 assert( (idxStr==0 && argc==0)((void) (0))
183610 || (idxStr && (int)strlen(idxStr)==argc*2) )((void) (0));
183611 for(ii=0; ii<argc; ii++){
183612 RtreeConstraint *p = &pCsr->aConstraint[ii];
183613 p->op = idxStr[ii*2];
183614 p->iCoord = idxStr[ii*2+1]-'0';
183615 if( p->op>=RTREE_MATCH ){
183616 /* A MATCH operator. The right-hand-side must be a blob that
183617 ** can be cast into an RtreeMatchArg object. One created using
183618 ** an sqlite3_rtree_geometry_callback() SQL user function.
183619 */
183620 rc = deserializeGeometry(argv[ii], p);
183621 if( rc!=SQLITE_OK0 ){
183622 break;
183623 }
183624 p->pInfo->nCoord = pRtree->nDim2;
183625 p->pInfo->anQueue = pCsr->anQueue;
183626 p->pInfo->mxLevel = pRtree->iDepth + 1;
183627 }else{
183628#ifdef SQLITE_RTREE_INT_ONLY
183629 p->u.rValue = sqlite3_value_int64(argv[ii]);
183630#else
183631 p->u.rValue = sqlite3_value_double(argv[ii]);
183632#endif
183633 }
183634 }
183635 }
183636 }
183637 if( rc==SQLITE_OK0 ){
183638 RtreeSearchPoint *pNew;
183639 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
183640 if( pNew==0 ) return SQLITE_NOMEM7;
183641 pNew->id = 1;
183642 pNew->iCell = 0;
183643 pNew->eWithin = PARTLY_WITHIN1;
183644 assert( pCsr->bPoint==1 )((void) (0));
183645 pCsr->aNode[0] = pRoot;
183646 pRoot = 0;
183647 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
183648 rc = rtreeStepToLeaf(pCsr);
183649 }
183650 }
183651
183652 nodeRelease(pRtree, pRoot);
183653 rtreeRelease(pRtree);
183654 return rc;
183655}
183656
183657/*
183658** Rtree virtual table module xBestIndex method. There are three
183659** table scan strategies to choose from (in order from most to
183660** least desirable):
183661**
183662** idxNum idxStr Strategy
183663** ------------------------------------------------
183664** 1 Unused Direct lookup by rowid.
183665** 2 See below R-tree query or full-table scan.
183666** ------------------------------------------------
183667**
183668** If strategy 1 is used, then idxStr is not meaningful. If strategy
183669** 2 is used, idxStr is formatted to contain 2 bytes for each
183670** constraint used. The first two bytes of idxStr correspond to
183671** the constraint in sqlite3_index_info.aConstraintUsage[] with
183672** (argvIndex==1) etc.
183673**
183674** The first of each pair of bytes in idxStr identifies the constraint
183675** operator as follows:
183676**
183677** Operator Byte Value
183678** ----------------------
183679** = 0x41 ('A')
183680** <= 0x42 ('B')
183681** < 0x43 ('C')
183682** >= 0x44 ('D')
183683** > 0x45 ('E')
183684** MATCH 0x46 ('F')
183685** ----------------------
183686**
183687** The second of each pair of bytes identifies the coordinate column
183688** to which the constraint applies. The leftmost coordinate column
183689** is 'a', the second from the left 'b' etc.
183690*/
183691static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
183692 Rtree *pRtree = (Rtree*)tab;
183693 int rc = SQLITE_OK0;
183694 int ii;
183695 int bMatch = 0; /* True if there exists a MATCH constraint */
183696 i64 nRow; /* Estimated rows returned by this scan */
183697
183698 int iIdx = 0;
183699 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
183700 memset(zIdxStr, 0, sizeof(zIdxStr));
183701
183702 /* Check if there exists a MATCH constraint - even an unusable one. If there
183703 ** is, do not consider the lookup-by-rowid plan as using such a plan would
183704 ** require the VDBE to evaluate the MATCH constraint, which is not currently
183705 ** possible. */
183706 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
183707 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH64 ){
183708 bMatch = 1;
183709 }
183710 }
183711
183712 assert( pIdxInfo->idxStr==0 )((void) (0));
183713 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
183714 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
183715
183716 if( bMatch==0 && p->usable
183717 && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2
183718 ){
183719 /* We have an equality constraint on the rowid. Use strategy 1. */
183720 int jj;
183721 for(jj=0; jj<ii; jj++){
183722 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
183723 pIdxInfo->aConstraintUsage[jj].omit = 0;
183724 }
183725 pIdxInfo->idxNum = 1;
183726 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
183727 pIdxInfo->aConstraintUsage[jj].omit = 1;
183728
183729 /* This strategy involves a two rowid lookups on an B-Tree structures
183730 ** and then a linear search of an R-Tree node. This should be
183731 ** considered almost as quick as a direct rowid lookup (for which
183732 ** sqlite uses an internal cost of 0.0). It is expected to return
183733 ** a single row.
183734 */
183735 pIdxInfo->estimatedCost = 30.0;
183736 pIdxInfo->estimatedRows = 1;
183737 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE1;
183738 return SQLITE_OK0;
183739 }
183740
183741 if( p->usable
183742 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
183743 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH64)
183744 ){
183745 u8 op;
183746 switch( p->op ){
183747 case SQLITE_INDEX_CONSTRAINT_EQ2: op = RTREE_EQ; break;
183748 case SQLITE_INDEX_CONSTRAINT_GT4: op = RTREE_GT; break;
183749 case SQLITE_INDEX_CONSTRAINT_LE8: op = RTREE_LE; break;
183750 case SQLITE_INDEX_CONSTRAINT_LT16: op = RTREE_LT; break;
183751 case SQLITE_INDEX_CONSTRAINT_GE32: op = RTREE_GE; break;
183752 case SQLITE_INDEX_CONSTRAINT_MATCH64: op = RTREE_MATCH; break;
183753 default: op = 0; break;
183754 }
183755 if( op ){
183756 zIdxStr[iIdx++] = op;
183757 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
183758 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
183759 pIdxInfo->aConstraintUsage[ii].omit = 1;
183760 }
183761 }
183762 }
183763
183764 pIdxInfo->idxNum = 2;
183765 pIdxInfo->needToFreeIdxStr = 1;
183766 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
183767 return SQLITE_NOMEM7;
183768 }
183769
183770 nRow = pRtree->nRowEst >> (iIdx/2);
183771 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
183772 pIdxInfo->estimatedRows = nRow;
183773
183774 return rc;
183775}
183776
183777/*
183778** Return the N-dimensional volumn of the cell stored in *p.
183779*/
183780static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
183781 RtreeDValue area = (RtreeDValue)1;
183782 assert( pRtree->nDim>=1 && pRtree->nDim<=5 )((void) (0));
183783#ifndef SQLITE_RTREE_INT_ONLY
183784 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
183785 switch( pRtree->nDim ){
183786 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
183787 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
183788 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
183789 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
183790 default: area *= p->aCoord[1].f - p->aCoord[0].f;
183791 }
183792 }else
183793#endif
183794 {
183795 switch( pRtree->nDim ){
183796 case 5: area = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
183797 case 4: area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
183798 case 3: area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
183799 case 2: area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
183800 default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
183801 }
183802 }
183803 return area;
183804}
183805
183806/*
183807** Return the margin length of cell p. The margin length is the sum
183808** of the objects size in each dimension.
183809*/
183810static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
183811 RtreeDValue margin = 0;
183812 int ii = pRtree->nDim2 - 2;
183813 do{
183814 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
183815 ii -= 2;
183816 }while( ii>=0 );
183817 return margin;
183818}
183819
183820/*
183821** Store the union of cells p1 and p2 in p1.
183822*/
183823static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
183824 int ii = 0;
183825 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
183826 do{
183827 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f)((p1->aCoord[ii].f)<(p2->aCoord[ii].f)?(p1->aCoord
[ii].f):(p2->aCoord[ii].f))
;
183828 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f)((p1->aCoord[ii+1].f)>(p2->aCoord[ii+1].f)?(p1->aCoord
[ii+1].f):(p2->aCoord[ii+1].f))
;
183829 ii += 2;
183830 }while( ii<pRtree->nDim2 );
183831 }else{
183832 do{
183833 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i)((p1->aCoord[ii].i)<(p2->aCoord[ii].i)?(p1->aCoord
[ii].i):(p2->aCoord[ii].i))
;
183834 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i)((p1->aCoord[ii+1].i)>(p2->aCoord[ii+1].i)?(p1->aCoord
[ii+1].i):(p2->aCoord[ii+1].i))
;
183835 ii += 2;
183836 }while( ii<pRtree->nDim2 );
183837 }
183838}
183839
183840/*
183841** Return true if the area covered by p2 is a subset of the area covered
183842** by p1. False otherwise.
183843*/
183844static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
183845 int ii;
183846 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
183847 for(ii=0; ii<pRtree->nDim2; ii+=2){
183848 RtreeCoord *a1 = &p1->aCoord[ii];
183849 RtreeCoord *a2 = &p2->aCoord[ii];
183850 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
183851 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
183852 ){
183853 return 0;
183854 }
183855 }
183856 return 1;
183857}
183858
183859/*
183860** Return the amount cell p would grow by if it were unioned with pCell.
183861*/
183862static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
183863 RtreeDValue area;
183864 RtreeCell cell;
183865 memcpy(&cell, p, sizeof(RtreeCell));
183866 area = cellArea(pRtree, &cell);
183867 cellUnion(pRtree, &cell, pCell);
183868 return (cellArea(pRtree, &cell)-area);
183869}
183870
183871static RtreeDValue cellOverlap(
183872 Rtree *pRtree,
183873 RtreeCell *p,
183874 RtreeCell *aCell,
183875 int nCell
183876){
183877 int ii;
183878 RtreeDValue overlap = RTREE_ZERO;
183879 for(ii=0; ii<nCell; ii++){
183880 int jj;
183881 RtreeDValue o = (RtreeDValue)1;
183882 for(jj=0; jj<pRtree->nDim2; jj+=2){
183883 RtreeDValue x1, x2;
183884 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]))((DCOORD(p->aCoord[jj]))>(DCOORD(aCell[ii].aCoord[jj]))
?(DCOORD(p->aCoord[jj])):(DCOORD(aCell[ii].aCoord[jj])))
;
183885 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]))((DCOORD(p->aCoord[jj+1]))<(DCOORD(aCell[ii].aCoord[jj+
1]))?(DCOORD(p->aCoord[jj+1])):(DCOORD(aCell[ii].aCoord[jj
+1])))
;
183886 if( x2<x1 ){
183887 o = (RtreeDValue)0;
183888 break;
183889 }else{
183890 o = o * (x2-x1);
183891 }
183892 }
183893 overlap += o;
183894 }
183895 return overlap;
183896}
183897
183898
183899/*
183900** This function implements the ChooseLeaf algorithm from Gutman[84].
183901** ChooseSubTree in r*tree terminology.
183902*/
183903static int ChooseLeaf(
183904 Rtree *pRtree, /* Rtree table */
183905 RtreeCell *pCell, /* Cell to insert into rtree */
183906 int iHeight, /* Height of sub-tree rooted at pCell */
183907 RtreeNode **ppLeaf /* OUT: Selected leaf page */
183908){
183909 int rc;
183910 int ii;
183911 RtreeNode *pNode = 0;
183912 rc = nodeAcquire(pRtree, 1, 0, &pNode);
183913
183914 for(ii=0; rc==SQLITE_OK0 && ii<(pRtree->iDepth-iHeight); ii++){
183915 int iCell;
183916 sqlite3_int64 iBest = 0;
183917
183918 RtreeDValue fMinGrowth = RTREE_ZERO;
183919 RtreeDValue fMinArea = RTREE_ZERO;
183920
183921 int nCell = NCELL(pNode);
183922 RtreeCell cell;
183923 RtreeNode *pChild;
183924
183925 RtreeCell *aCell = 0;
183926
183927 /* Select the child node which will be enlarged the least if pCell
183928 ** is inserted into it. Resolve ties by choosing the entry with
183929 ** the smallest area.
183930 */
183931 for(iCell=0; iCell<nCell; iCell++){
183932 int bBest = 0;
183933 RtreeDValue growth;
183934 RtreeDValue area;
183935 nodeGetCell(pRtree, pNode, iCell, &cell);
183936 growth = cellGrowth(pRtree, &cell, pCell);
183937 area = cellArea(pRtree, &cell);
183938 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
183939 bBest = 1;
183940 }
183941 if( bBest ){
183942 fMinGrowth = growth;
183943 fMinArea = area;
183944 iBest = cell.iRowid;
183945 }
183946 }
183947
183948 sqlite3_free(aCell);
183949 rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
183950 nodeRelease(pRtree, pNode);
183951 pNode = pChild;
183952 }
183953
183954 *ppLeaf = pNode;
183955 return rc;
183956}
183957
183958/*
183959** A cell with the same content as pCell has just been inserted into
183960** the node pNode. This function updates the bounding box cells in
183961** all ancestor elements.
183962*/
183963static int AdjustTree(
183964 Rtree *pRtree, /* Rtree table */
183965 RtreeNode *pNode, /* Adjust ancestry of this node. */
183966 RtreeCell *pCell /* This cell was just inserted */
183967){
183968 RtreeNode *p = pNode;
183969 int cnt = 0;
183970 while( p->pParent ){
183971 RtreeNode *pParent = p->pParent;
183972 RtreeCell cell;
183973 int iCell;
183974
183975 if( (++cnt)>1000 || nodeParentIndex(pRtree, p, &iCell) ){
183976 RTREE_IS_CORRUPT(pRtree);
183977 return SQLITE_CORRUPT_VTAB(11 | (1<<8));
183978 }
183979
183980 nodeGetCell(pRtree, pParent, iCell, &cell);
183981 if( !cellContains(pRtree, &cell, pCell) ){
183982 cellUnion(pRtree, &cell, pCell);
183983 nodeOverwriteCell(pRtree, pParent, &cell, iCell);
183984 }
183985
183986 p = pParent;
183987 }
183988 return SQLITE_OK0;
183989}
183990
183991/*
183992** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
183993*/
183994static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
183995 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
183996 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
183997 sqlite3_step(pRtree->pWriteRowid);
183998 return sqlite3_reset(pRtree->pWriteRowid);
183999}
184000
184001/*
184002** Write mapping (iNode->iPar) to the <rtree>_parent table.
184003*/
184004static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
184005 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
184006 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
184007 sqlite3_step(pRtree->pWriteParent);
184008 return sqlite3_reset(pRtree->pWriteParent);
184009}
184010
184011static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
184012
184013
184014/*
184015** Arguments aIdx, aDistance and aSpare all point to arrays of size
184016** nIdx. The aIdx array contains the set of integers from 0 to
184017** (nIdx-1) in no particular order. This function sorts the values
184018** in aIdx according to the indexed values in aDistance. For
184019** example, assuming the inputs:
184020**
184021** aIdx = { 0, 1, 2, 3 }
184022** aDistance = { 5.0, 2.0, 7.0, 6.0 }
184023**
184024** this function sets the aIdx array to contain:
184025**
184026** aIdx = { 0, 1, 2, 3 }
184027**
184028** The aSpare array is used as temporary working space by the
184029** sorting algorithm.
184030*/
184031static void SortByDistance(
184032 int *aIdx,
184033 int nIdx,
184034 RtreeDValue *aDistance,
184035 int *aSpare
184036){
184037 if( nIdx>1 ){
184038 int iLeft = 0;
184039 int iRight = 0;
184040
184041 int nLeft = nIdx/2;
184042 int nRight = nIdx-nLeft;
184043 int *aLeft = aIdx;
184044 int *aRight = &aIdx[nLeft];
184045
184046 SortByDistance(aLeft, nLeft, aDistance, aSpare);
184047 SortByDistance(aRight, nRight, aDistance, aSpare);
184048
184049 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
184050 aLeft = aSpare;
184051
184052 while( iLeft<nLeft || iRight<nRight ){
184053 if( iLeft==nLeft ){
184054 aIdx[iLeft+iRight] = aRight[iRight];
184055 iRight++;
184056 }else if( iRight==nRight ){
184057 aIdx[iLeft+iRight] = aLeft[iLeft];
184058 iLeft++;
184059 }else{
184060 RtreeDValue fLeft = aDistance[aLeft[iLeft]];
184061 RtreeDValue fRight = aDistance[aRight[iRight]];
184062 if( fLeft<fRight ){
184063 aIdx[iLeft+iRight] = aLeft[iLeft];
184064 iLeft++;
184065 }else{
184066 aIdx[iLeft+iRight] = aRight[iRight];
184067 iRight++;
184068 }
184069 }
184070 }
184071
184072#if 0
184073 /* Check that the sort worked */
184074 {
184075 int jj;
184076 for(jj=1; jj<nIdx; jj++){
184077 RtreeDValue left = aDistance[aIdx[jj-1]];
184078 RtreeDValue right = aDistance[aIdx[jj]];
184079 assert( left<=right )((void) (0));
184080 }
184081 }
184082#endif
184083 }
184084}
184085
184086/*
184087** Arguments aIdx, aCell and aSpare all point to arrays of size
184088** nIdx. The aIdx array contains the set of integers from 0 to
184089** (nIdx-1) in no particular order. This function sorts the values
184090** in aIdx according to dimension iDim of the cells in aCell. The
184091** minimum value of dimension iDim is considered first, the
184092** maximum used to break ties.
184093**
184094** The aSpare array is used as temporary working space by the
184095** sorting algorithm.
184096*/
184097static void SortByDimension(
184098 Rtree *pRtree,
184099 int *aIdx,
184100 int nIdx,
184101 int iDim,
184102 RtreeCell *aCell,
184103 int *aSpare
184104){
184105 if( nIdx>1 ){
184106
184107 int iLeft = 0;
184108 int iRight = 0;
184109
184110 int nLeft = nIdx/2;
184111 int nRight = nIdx-nLeft;
184112 int *aLeft = aIdx;
184113 int *aRight = &aIdx[nLeft];
184114
184115 SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
184116 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
184117
184118 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
184119 aLeft = aSpare;
184120 while( iLeft<nLeft || iRight<nRight ){
184121 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
184122 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
184123 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
184124 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
184125 if( (iLeft!=nLeft) && ((iRight==nRight)
184126 || (xleft1<xright1)
184127 || (xleft1==xright1 && xleft2<xright2)
184128 )){
184129 aIdx[iLeft+iRight] = aLeft[iLeft];
184130 iLeft++;
184131 }else{
184132 aIdx[iLeft+iRight] = aRight[iRight];
184133 iRight++;
184134 }
184135 }
184136
184137#if 0
184138 /* Check that the sort worked */
184139 {
184140 int jj;
184141 for(jj=1; jj<nIdx; jj++){
184142 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
184143 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
184144 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
184145 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
184146 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) )((void) (0));
184147 }
184148 }
184149#endif
184150 }
184151}
184152
184153/*
184154** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
184155*/
184156static int splitNodeStartree(
184157 Rtree *pRtree,
184158 RtreeCell *aCell,
184159 int nCell,
184160 RtreeNode *pLeft,
184161 RtreeNode *pRight,
184162 RtreeCell *pBboxLeft,
184163 RtreeCell *pBboxRight
184164){
184165 int **aaSorted;
184166 int *aSpare;
184167 int ii;
184168
184169 int iBestDim = 0;
184170 int iBestSplit = 0;
184171 RtreeDValue fBestMargin = RTREE_ZERO;
184172
184173 sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
184174
184175 aaSorted = (int **)sqlite3_malloc64(nByte);
184176 if( !aaSorted ){
184177 return SQLITE_NOMEM7;
184178 }
184179
184180 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
184181 memset(aaSorted, 0, nByte);
184182 for(ii=0; ii<pRtree->nDim; ii++){
184183 int jj;
184184 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
184185 for(jj=0; jj<nCell; jj++){
184186 aaSorted[ii][jj] = jj;
184187 }
184188 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
184189 }
184190
184191 for(ii=0; ii<pRtree->nDim; ii++){
184192 RtreeDValue margin = RTREE_ZERO;
184193 RtreeDValue fBestOverlap = RTREE_ZERO;
184194 RtreeDValue fBestArea = RTREE_ZERO;
184195 int iBestLeft = 0;
184196 int nLeft;
184197
184198 for(
184199 nLeft=RTREE_MINCELLS(pRtree);
184200 nLeft<=(nCell-RTREE_MINCELLS(pRtree));
184201 nLeft++
184202 ){
184203 RtreeCell left;
184204 RtreeCell right;
184205 int kk;
184206 RtreeDValue overlap;
184207 RtreeDValue area;
184208
184209 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
184210 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
184211 for(kk=1; kk<(nCell-1); kk++){
184212 if( kk<nLeft ){
184213 cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
184214 }else{
184215 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
184216 }
184217 }
184218 margin += cellMargin(pRtree, &left);
184219 margin += cellMargin(pRtree, &right);
184220 overlap = cellOverlap(pRtree, &left, &right, 1);
184221 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
184222 if( (nLeft==RTREE_MINCELLS(pRtree))
184223 || (overlap<fBestOverlap)
184224 || (overlap==fBestOverlap && area<fBestArea)
184225 ){
184226 iBestLeft = nLeft;
184227 fBestOverlap = overlap;
184228 fBestArea = area;
184229 }
184230 }
184231
184232 if( ii==0 || margin<fBestMargin ){
184233 iBestDim = ii;
184234 fBestMargin = margin;
184235 iBestSplit = iBestLeft;
184236 }
184237 }
184238
184239 memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
184240 memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
184241 for(ii=0; ii<nCell; ii++){
184242 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
184243 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
184244 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
184245 nodeInsertCell(pRtree, pTarget, pCell);
184246 cellUnion(pRtree, pBbox, pCell);
184247 }
184248
184249 sqlite3_free(aaSorted);
184250 return SQLITE_OK0;
184251}
184252
184253
184254static int updateMapping(
184255 Rtree *pRtree,
184256 i64 iRowid,
184257 RtreeNode *pNode,
184258 int iHeight
184259){
184260 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
184261 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
184262 if( iHeight>0 ){
184263 RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
184264 if( pChild ){
184265 nodeRelease(pRtree, pChild->pParent);
184266 nodeReference(pNode);
184267 pChild->pParent = pNode;
184268 }
184269 }
184270 return xSetMapping(pRtree, iRowid, pNode->iNode);
184271}
184272
184273static int SplitNode(
184274 Rtree *pRtree,
184275 RtreeNode *pNode,
184276 RtreeCell *pCell,
184277 int iHeight
184278){
184279 int i;
184280 int newCellIsRight = 0;
184281
184282 int rc = SQLITE_OK0;
184283 int nCell = NCELL(pNode);
184284 RtreeCell *aCell;
184285 int *aiUsed;
184286
184287 RtreeNode *pLeft = 0;
184288 RtreeNode *pRight = 0;
184289
184290 RtreeCell leftbbox;
184291 RtreeCell rightbbox;
184292
184293 /* Allocate an array and populate it with a copy of pCell and
184294 ** all cells from node pLeft. Then zero the original node.
184295 */
184296 aCell = sqlite3_malloc64((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
184297 if( !aCell ){
184298 rc = SQLITE_NOMEM7;
184299 goto splitnode_out;
184300 }
184301 aiUsed = (int *)&aCell[nCell+1];
184302 memset(aiUsed, 0, sizeof(int)*(nCell+1));
184303 for(i=0; i<nCell; i++){
184304 nodeGetCell(pRtree, pNode, i, &aCell[i]);
184305 }
184306 nodeZero(pRtree, pNode);
184307 memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
184308 nCell++;
184309
184310 if( pNode->iNode==1 ){
184311 pRight = nodeNew(pRtree, pNode);
184312 pLeft = nodeNew(pRtree, pNode);
184313 pRtree->iDepth++;
184314 pNode->isDirty = 1;
184315 writeInt16(pNode->zData, pRtree->iDepth);
184316 }else{
184317 pLeft = pNode;
184318 pRight = nodeNew(pRtree, pLeft->pParent);
184319 pLeft->nRef++;
184320 }
184321
184322 if( !pLeft || !pRight ){
184323 rc = SQLITE_NOMEM7;
184324 goto splitnode_out;
184325 }
184326
184327 memset(pLeft->zData, 0, pRtree->iNodeSize);
184328 memset(pRight->zData, 0, pRtree->iNodeSize);
184329
184330 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
184331 &leftbbox, &rightbbox);
184332 if( rc!=SQLITE_OK0 ){
184333 goto splitnode_out;
184334 }
184335
184336 /* Ensure both child nodes have node numbers assigned to them by calling
184337 ** nodeWrite(). Node pRight always needs a node number, as it was created
184338 ** by nodeNew() above. But node pLeft sometimes already has a node number.
184339 ** In this case avoid the all to nodeWrite().
184340 */
184341 if( SQLITE_OK0!=(rc = nodeWrite(pRtree, pRight))
184342 || (0==pLeft->iNode && SQLITE_OK0!=(rc = nodeWrite(pRtree, pLeft)))
184343 ){
184344 goto splitnode_out;
184345 }
184346
184347 rightbbox.iRowid = pRight->iNode;
184348 leftbbox.iRowid = pLeft->iNode;
184349
184350 if( pNode->iNode==1 ){
184351 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
184352 if( rc!=SQLITE_OK0 ){
184353 goto splitnode_out;
184354 }
184355 }else{
184356 RtreeNode *pParent = pLeft->pParent;
184357 int iCell;
184358 rc = nodeParentIndex(pRtree, pLeft, &iCell);
184359 if( rc==SQLITE_OK0 ){
184360 nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
184361 rc = AdjustTree(pRtree, pParent, &leftbbox);
184362 }
184363 if( rc!=SQLITE_OK0 ){
184364 goto splitnode_out;
184365 }
184366 }
184367 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
184368 goto splitnode_out;
184369 }
184370
184371 for(i=0; i<NCELL(pRight); i++){
184372 i64 iRowid = nodeGetRowid(pRtree, pRight, i);
184373 rc = updateMapping(pRtree, iRowid, pRight, iHeight);
184374 if( iRowid==pCell->iRowid ){
184375 newCellIsRight = 1;
184376 }
184377 if( rc!=SQLITE_OK0 ){
184378 goto splitnode_out;
184379 }
184380 }
184381 if( pNode->iNode==1 ){
184382 for(i=0; i<NCELL(pLeft); i++){
184383 i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
184384 rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
184385 if( rc!=SQLITE_OK0 ){
184386 goto splitnode_out;
184387 }
184388 }
184389 }else if( newCellIsRight==0 ){
184390 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
184391 }
184392
184393 if( rc==SQLITE_OK0 ){
184394 rc = nodeRelease(pRtree, pRight);
184395 pRight = 0;
184396 }
184397 if( rc==SQLITE_OK0 ){
184398 rc = nodeRelease(pRtree, pLeft);
184399 pLeft = 0;
184400 }
184401
184402splitnode_out:
184403 nodeRelease(pRtree, pRight);
184404 nodeRelease(pRtree, pLeft);
184405 sqlite3_free(aCell);
184406 return rc;
184407}
184408
184409/*
184410** If node pLeaf is not the root of the r-tree and its pParent pointer is
184411** still NULL, load all ancestor nodes of pLeaf into memory and populate
184412** the pLeaf->pParent chain all the way up to the root node.
184413**
184414** This operation is required when a row is deleted (or updated - an update
184415** is implemented as a delete followed by an insert). SQLite provides the
184416** rowid of the row to delete, which can be used to find the leaf on which
184417** the entry resides (argument pLeaf). Once the leaf is located, this
184418** function is called to determine its ancestry.
184419*/
184420static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
184421 int rc = SQLITE_OK0;
184422 RtreeNode *pChild = pLeaf;
184423 while( rc==SQLITE_OK0 && pChild->iNode!=1 && pChild->pParent==0 ){
184424 int rc2 = SQLITE_OK0; /* sqlite3_reset() return code */
184425 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
184426 rc = sqlite3_step(pRtree->pReadParent);
184427 if( rc==SQLITE_ROW100 ){
184428 RtreeNode *pTest; /* Used to test for reference loops */
184429 i64 iNode; /* Node number of parent node */
184430
184431 /* Before setting pChild->pParent, test that we are not creating a
184432 ** loop of references (as we would if, say, pChild==pParent). We don't
184433 ** want to do this as it leads to a memory leak when trying to delete
184434 ** the referenced counted node structures.
184435 */
184436 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
184437 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
184438 if( !pTest ){
184439 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
184440 }
184441 }
184442 rc = sqlite3_reset(pRtree->pReadParent);
184443 if( rc==SQLITE_OK0 ) rc = rc2;
184444 if( rc==SQLITE_OK0 && !pChild->pParent ){
184445 RTREE_IS_CORRUPT(pRtree);
184446 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
184447 }
184448 pChild = pChild->pParent;
184449 }
184450 return rc;
184451}
184452
184453static int deleteCell(Rtree *, RtreeNode *, int, int);
184454
184455static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
184456 int rc;
184457 int rc2;
184458 RtreeNode *pParent = 0;
184459 int iCell;
184460
184461 assert( pNode->nRef==1 )((void) (0));
184462
184463 /* Remove the entry in the parent cell. */
184464 rc = nodeParentIndex(pRtree, pNode, &iCell);
184465 if( rc==SQLITE_OK0 ){
184466 pParent = pNode->pParent;
184467 pNode->pParent = 0;
184468 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
184469 }
184470 rc2 = nodeRelease(pRtree, pParent);
184471 if( rc==SQLITE_OK0 ){
184472 rc = rc2;
184473 }
184474 if( rc!=SQLITE_OK0 ){
184475 return rc;
184476 }
184477
184478 /* Remove the xxx_node entry. */
184479 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
184480 sqlite3_step(pRtree->pDeleteNode);
184481 if( SQLITE_OK0!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
184482 return rc;
184483 }
184484
184485 /* Remove the xxx_parent entry. */
184486 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
184487 sqlite3_step(pRtree->pDeleteParent);
184488 if( SQLITE_OK0!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
184489 return rc;
184490 }
184491
184492 /* Remove the node from the in-memory hash table and link it into
184493 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
184494 */
184495 nodeHashDelete(pRtree, pNode);
184496 pNode->iNode = iHeight;
184497 pNode->pNext = pRtree->pDeleted;
184498 pNode->nRef++;
184499 pRtree->pDeleted = pNode;
184500
184501 return SQLITE_OK0;
184502}
184503
184504static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
184505 RtreeNode *pParent = pNode->pParent;
184506 int rc = SQLITE_OK0;
184507 if( pParent ){
184508 int ii;
184509 int nCell = NCELL(pNode);
184510 RtreeCell box; /* Bounding box for pNode */
184511 nodeGetCell(pRtree, pNode, 0, &box);
184512 for(ii=1; ii<nCell; ii++){
184513 RtreeCell cell;
184514 nodeGetCell(pRtree, pNode, ii, &cell);
184515 cellUnion(pRtree, &box, &cell);
184516 }
184517 box.iRowid = pNode->iNode;
184518 rc = nodeParentIndex(pRtree, pNode, &ii);
184519 if( rc==SQLITE_OK0 ){
184520 nodeOverwriteCell(pRtree, pParent, &box, ii);
184521 rc = fixBoundingBox(pRtree, pParent);
184522 }
184523 }
184524 return rc;
184525}
184526
184527/*
184528** Delete the cell at index iCell of node pNode. After removing the
184529** cell, adjust the r-tree data structure if required.
184530*/
184531static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
184532 RtreeNode *pParent;
184533 int rc;
184534
184535 if( SQLITE_OK0!=(rc = fixLeafParent(pRtree, pNode)) ){
184536 return rc;
184537 }
184538
184539 /* Remove the cell from the node. This call just moves bytes around
184540 ** the in-memory node image, so it cannot fail.
184541 */
184542 nodeDeleteCell(pRtree, pNode, iCell);
184543
184544 /* If the node is not the tree root and now has less than the minimum
184545 ** number of cells, remove it from the tree. Otherwise, update the
184546 ** cell in the parent node so that it tightly contains the updated
184547 ** node.
184548 */
184549 pParent = pNode->pParent;
184550 assert( pParent || pNode->iNode==1 )((void) (0));
184551 if( pParent ){
184552 if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
184553 rc = removeNode(pRtree, pNode, iHeight);
184554 }else{
184555 rc = fixBoundingBox(pRtree, pNode);
184556 }
184557 }
184558
184559 return rc;
184560}
184561
184562static int Reinsert(
184563 Rtree *pRtree,
184564 RtreeNode *pNode,
184565 RtreeCell *pCell,
184566 int iHeight
184567){
184568 int *aOrder;
184569 int *aSpare;
184570 RtreeCell *aCell;
184571 RtreeDValue *aDistance;
184572 int nCell;
184573 RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
184574 int iDim;
184575 int ii;
184576 int rc = SQLITE_OK0;
184577 int n;
184578
184579 memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
184580
184581 nCell = NCELL(pNode)+1;
184582 n = (nCell+1)&(~1);
184583
184584 /* Allocate the buffers used by this operation. The allocation is
184585 ** relinquished before this function returns.
184586 */
184587 aCell = (RtreeCell *)sqlite3_malloc64(n * (
184588 sizeof(RtreeCell) + /* aCell array */
184589 sizeof(int) + /* aOrder array */
184590 sizeof(int) + /* aSpare array */
184591 sizeof(RtreeDValue) /* aDistance array */
184592 ));
184593 if( !aCell ){
184594 return SQLITE_NOMEM7;
184595 }
184596 aOrder = (int *)&aCell[n];
184597 aSpare = (int *)&aOrder[n];
184598 aDistance = (RtreeDValue *)&aSpare[n];
184599
184600 for(ii=0; ii<nCell; ii++){
184601 if( ii==(nCell-1) ){
184602 memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
184603 }else{
184604 nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
184605 }
184606 aOrder[ii] = ii;
184607 for(iDim=0; iDim<pRtree->nDim; iDim++){
184608 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
184609 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
184610 }
184611 }
184612 for(iDim=0; iDim<pRtree->nDim; iDim++){
184613 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
184614 }
184615
184616 for(ii=0; ii<nCell; ii++){
184617 aDistance[ii] = RTREE_ZERO;
184618 for(iDim=0; iDim<pRtree->nDim; iDim++){
184619 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
184620 DCOORD(aCell[ii].aCoord[iDim*2]));
184621 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
184622 }
184623 }
184624
184625 SortByDistance(aOrder, nCell, aDistance, aSpare);
184626 nodeZero(pRtree, pNode);
184627
184628 for(ii=0; rc==SQLITE_OK0 && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
184629 RtreeCell *p = &aCell[aOrder[ii]];
184630 nodeInsertCell(pRtree, pNode, p);
184631 if( p->iRowid==pCell->iRowid ){
184632 if( iHeight==0 ){
184633 rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
184634 }else{
184635 rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
184636 }
184637 }
184638 }
184639 if( rc==SQLITE_OK0 ){
184640 rc = fixBoundingBox(pRtree, pNode);
184641 }
184642 for(; rc==SQLITE_OK0 && ii<nCell; ii++){
184643 /* Find a node to store this cell in. pNode->iNode currently contains
184644 ** the height of the sub-tree headed by the cell.
184645 */
184646 RtreeNode *pInsert;
184647 RtreeCell *p = &aCell[aOrder[ii]];
184648 rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
184649 if( rc==SQLITE_OK0 ){
184650 int rc2;
184651 rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
184652 rc2 = nodeRelease(pRtree, pInsert);
184653 if( rc==SQLITE_OK0 ){
184654 rc = rc2;
184655 }
184656 }
184657 }
184658
184659 sqlite3_free(aCell);
184660 return rc;
184661}
184662
184663/*
184664** Insert cell pCell into node pNode. Node pNode is the head of a
184665** subtree iHeight high (leaf nodes have iHeight==0).
184666*/
184667static int rtreeInsertCell(
184668 Rtree *pRtree,
184669 RtreeNode *pNode,
184670 RtreeCell *pCell,
184671 int iHeight
184672){
184673 int rc = SQLITE_OK0;
184674 if( iHeight>0 ){
184675 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
184676 if( pChild ){
184677 nodeRelease(pRtree, pChild->pParent);
184678 nodeReference(pNode);
184679 pChild->pParent = pNode;
184680 }
184681 }
184682 if( nodeInsertCell(pRtree, pNode, pCell) ){
184683 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
184684 rc = SplitNode(pRtree, pNode, pCell, iHeight);
184685 }else{
184686 pRtree->iReinsertHeight = iHeight;
184687 rc = Reinsert(pRtree, pNode, pCell, iHeight);
184688 }
184689 }else{
184690 rc = AdjustTree(pRtree, pNode, pCell);
184691 if( rc==SQLITE_OK0 ){
184692 if( iHeight==0 ){
184693 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
184694 }else{
184695 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
184696 }
184697 }
184698 }
184699 return rc;
184700}
184701
184702static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
184703 int ii;
184704 int rc = SQLITE_OK0;
184705 int nCell = NCELL(pNode);
184706
184707 for(ii=0; rc==SQLITE_OK0 && ii<nCell; ii++){
184708 RtreeNode *pInsert;
184709 RtreeCell cell;
184710 nodeGetCell(pRtree, pNode, ii, &cell);
184711
184712 /* Find a node to store this cell in. pNode->iNode currently contains
184713 ** the height of the sub-tree headed by the cell.
184714 */
184715 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
184716 if( rc==SQLITE_OK0 ){
184717 int rc2;
184718 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
184719 rc2 = nodeRelease(pRtree, pInsert);
184720 if( rc==SQLITE_OK0 ){
184721 rc = rc2;
184722 }
184723 }
184724 }
184725 return rc;
184726}
184727
184728/*
184729** Select a currently unused rowid for a new r-tree record.
184730*/
184731static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
184732 int rc;
184733 sqlite3_bind_null(pRtree->pWriteRowid, 1);
184734 sqlite3_bind_null(pRtree->pWriteRowid, 2);
184735 sqlite3_step(pRtree->pWriteRowid);
184736 rc = sqlite3_reset(pRtree->pWriteRowid);
184737 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
184738 return rc;
184739}
184740
184741/*
184742** Remove the entry with rowid=iDelete from the r-tree structure.
184743*/
184744static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
184745 int rc; /* Return code */
184746 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
184747 int iCell; /* Index of iDelete cell in pLeaf */
184748 RtreeNode *pRoot = 0; /* Root node of rtree structure */
184749
184750
184751 /* Obtain a reference to the root node to initialize Rtree.iDepth */
184752 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
184753
184754 /* Obtain a reference to the leaf node that contains the entry
184755 ** about to be deleted.
184756 */
184757 if( rc==SQLITE_OK0 ){
184758 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
184759 }
184760
184761#ifdef CORRUPT_DB(sqlite3Config.neverCorrupt==0)
184762 assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB )((void) (0));
184763#endif
184764
184765 /* Delete the cell in question from the leaf node. */
184766 if( rc==SQLITE_OK0 && pLeaf ){
184767 int rc2;
184768 rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
184769 if( rc==SQLITE_OK0 ){
184770 rc = deleteCell(pRtree, pLeaf, iCell, 0);
184771 }
184772 rc2 = nodeRelease(pRtree, pLeaf);
184773 if( rc==SQLITE_OK0 ){
184774 rc = rc2;
184775 }
184776 }
184777
184778 /* Delete the corresponding entry in the <rtree>_rowid table. */
184779 if( rc==SQLITE_OK0 ){
184780 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
184781 sqlite3_step(pRtree->pDeleteRowid);
184782 rc = sqlite3_reset(pRtree->pDeleteRowid);
184783 }
184784
184785 /* Check if the root node now has exactly one child. If so, remove
184786 ** it, schedule the contents of the child for reinsertion and
184787 ** reduce the tree height by one.
184788 **
184789 ** This is equivalent to copying the contents of the child into
184790 ** the root node (the operation that Gutman's paper says to perform
184791 ** in this scenario).
184792 */
184793 if( rc==SQLITE_OK0 && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
184794 int rc2;
184795 RtreeNode *pChild = 0;
184796 i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
184797 rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
184798 if( rc==SQLITE_OK0 ){
184799 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
184800 }
184801 rc2 = nodeRelease(pRtree, pChild);
184802 if( rc==SQLITE_OK0 ) rc = rc2;
184803 if( rc==SQLITE_OK0 ){
184804 pRtree->iDepth--;
184805 writeInt16(pRoot->zData, pRtree->iDepth);
184806 pRoot->isDirty = 1;
184807 }
184808 }
184809
184810 /* Re-insert the contents of any underfull nodes removed from the tree. */
184811 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
184812 if( rc==SQLITE_OK0 ){
184813 rc = reinsertNodeContent(pRtree, pLeaf);
184814 }
184815 pRtree->pDeleted = pLeaf->pNext;
184816 pRtree->nNodeRef--;
184817 sqlite3_free(pLeaf);
184818 }
184819
184820 /* Release the reference to the root node. */
184821 if( rc==SQLITE_OK0 ){
184822 rc = nodeRelease(pRtree, pRoot);
184823 }else{
184824 nodeRelease(pRtree, pRoot);
184825 }
184826
184827 return rc;
184828}
184829
184830/*
184831** Rounding constants for float->double conversion.
184832*/
184833#define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */
184834#define RNDAWAY (1.0 + 1.0/8388608.0) /* Round away from zero */
184835
184836#if !defined(SQLITE_RTREE_INT_ONLY)
184837/*
184838** Convert an sqlite3_value into an RtreeValue (presumably a float)
184839** while taking care to round toward negative or positive, respectively.
184840*/
184841static RtreeValue rtreeValueDown(sqlite3_value *v){
184842 double d = sqlite3_value_double(v);
184843 float f = (float)d;
184844 if( f>d ){
184845 f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
184846 }
184847 return f;
184848}
184849static RtreeValue rtreeValueUp(sqlite3_value *v){
184850 double d = sqlite3_value_double(v);
184851 float f = (float)d;
184852 if( f<d ){
184853 f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
184854 }
184855 return f;
184856}
184857#endif /* !defined(SQLITE_RTREE_INT_ONLY) */
184858
184859/*
184860** A constraint has failed while inserting a row into an rtree table.
184861** Assuming no OOM error occurs, this function sets the error message
184862** (at pRtree->base.zErrMsg) to an appropriate value and returns
184863** SQLITE_CONSTRAINT.
184864**
184865** Parameter iCol is the index of the leftmost column involved in the
184866** constraint failure. If it is 0, then the constraint that failed is
184867** the unique constraint on the id column. Otherwise, it is the rtree
184868** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
184869**
184870** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
184871*/
184872static int rtreeConstraintError(Rtree *pRtree, int iCol){
184873 sqlite3_stmt *pStmt = 0;
184874 char *zSql;
184875 int rc;
184876
184877 assert( iCol==0 || iCol%2 )((void) (0));
184878 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
184879 if( zSql ){
184880 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
184881 }else{
184882 rc = SQLITE_NOMEM7;
184883 }
184884 sqlite3_free(zSql);
184885
184886 if( rc==SQLITE_OK0 ){
184887 if( iCol==0 ){
184888 const char *zCol = sqlite3_column_name(pStmt, 0);
184889 pRtree->base.zErrMsg = sqlite3_mprintf(
184890 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
184891 );
184892 }else{
184893 const char *zCol1 = sqlite3_column_name(pStmt, iCol);
184894 const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
184895 pRtree->base.zErrMsg = sqlite3_mprintf(
184896 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
184897 );
184898 }
184899 }
184900
184901 sqlite3_finalize(pStmt);
184902 return (rc==SQLITE_OK0 ? SQLITE_CONSTRAINT19 : rc);
184903}
184904
184905
184906
184907/*
184908** The xUpdate method for rtree module virtual tables.
184909*/
184910static int rtreeUpdate(
184911 sqlite3_vtab *pVtab,
184912 int nData,
184913 sqlite3_value **aData,
184914 sqlite_int64 *pRowid
184915){
184916 Rtree *pRtree = (Rtree *)pVtab;
184917 int rc = SQLITE_OK0;
184918 RtreeCell cell; /* New cell to insert if nData>1 */
184919 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
184920
184921 if( pRtree->nNodeRef ){
184922 /* Unable to write to the btree while another cursor is reading from it,
184923 ** since the write might do a rebalance which would disrupt the read
184924 ** cursor. */
184925 return SQLITE_LOCKED_VTAB(6 | (2<<8));
184926 }
184927 rtreeReference(pRtree);
184928 assert(nData>=1)((void) (0));
184929
184930 cell.iRowid = 0; /* Used only to suppress a compiler warning */
184931
184932 /* Constraint handling. A write operation on an r-tree table may return
184933 ** SQLITE_CONSTRAINT for two reasons:
184934 **
184935 ** 1. A duplicate rowid value, or
184936 ** 2. The supplied data violates the "x2>=x1" constraint.
184937 **
184938 ** In the first case, if the conflict-handling mode is REPLACE, then
184939 ** the conflicting row can be removed before proceeding. In the second
184940 ** case, SQLITE_CONSTRAINT must be returned regardless of the
184941 ** conflict-handling mode specified by the user.
184942 */
184943 if( nData>1 ){
184944 int ii;
184945 int nn = nData - 4;
184946
184947 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
184948 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
184949 **
184950 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
184951 ** with "column" that are interpreted as table constraints.
184952 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
184953 ** This problem was discovered after years of use, so we silently ignore
184954 ** these kinds of misdeclared tables to avoid breaking any legacy.
184955 */
184956
184957#ifndef SQLITE_RTREE_INT_ONLY
184958 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
184959 for(ii=0; ii<nn; ii+=2){
184960 cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
184961 cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
184962 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
184963 rc = rtreeConstraintError(pRtree, ii+1);
184964 goto constraint;
184965 }
184966 }
184967 }else
184968#endif
184969 {
184970 for(ii=0; ii<nn; ii+=2){
184971 cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
184972 cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
184973 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
184974 rc = rtreeConstraintError(pRtree, ii+1);
184975 goto constraint;
184976 }
184977 }
184978 }
184979
184980 /* If a rowid value was supplied, check if it is already present in
184981 ** the table. If so, the constraint has failed. */
184982 if( sqlite3_value_type(aData[2])!=SQLITE_NULL5 ){
184983 cell.iRowid = sqlite3_value_int64(aData[2]);
184984 if( sqlite3_value_type(aData[0])==SQLITE_NULL5
184985 || sqlite3_value_int64(aData[0])!=cell.iRowid
184986 ){
184987 int steprc;
184988 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
184989 steprc = sqlite3_step(pRtree->pReadRowid);
184990 rc = sqlite3_reset(pRtree->pReadRowid);
184991 if( SQLITE_ROW100==steprc ){
184992 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE5 ){
184993 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
184994 }else{
184995 rc = rtreeConstraintError(pRtree, 0);
184996 goto constraint;
184997 }
184998 }
184999 }
185000 bHaveRowid = 1;
185001 }
185002 }
185003
185004 /* If aData[0] is not an SQL NULL value, it is the rowid of a
185005 ** record to delete from the r-tree table. The following block does
185006 ** just that.
185007 */
185008 if( sqlite3_value_type(aData[0])!=SQLITE_NULL5 ){
185009 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
185010 }
185011
185012 /* If the aData[] array contains more than one element, elements
185013 ** (aData[2]..aData[argc-1]) contain a new record to insert into
185014 ** the r-tree structure.
185015 */
185016 if( rc==SQLITE_OK0 && nData>1 ){
185017 /* Insert the new record into the r-tree */
185018 RtreeNode *pLeaf = 0;
185019
185020 /* Figure out the rowid of the new row. */
185021 if( bHaveRowid==0 ){
185022 rc = rtreeNewRowid(pRtree, &cell.iRowid);
185023 }
185024 *pRowid = cell.iRowid;
185025
185026 if( rc==SQLITE_OK0 ){
185027 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
185028 }
185029 if( rc==SQLITE_OK0 ){
185030 int rc2;
185031 pRtree->iReinsertHeight = -1;
185032 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
185033 rc2 = nodeRelease(pRtree, pLeaf);
185034 if( rc==SQLITE_OK0 ){
185035 rc = rc2;
185036 }
185037 }
185038 if( rc==SQLITE_OK0 && pRtree->nAux ){
185039 sqlite3_stmt *pUp = pRtree->pWriteAux;
185040 int jj;
185041 sqlite3_bind_int64(pUp, 1, *pRowid);
185042 for(jj=0; jj<pRtree->nAux; jj++){
185043 sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
185044 }
185045 sqlite3_step(pUp);
185046 rc = sqlite3_reset(pUp);
185047 }
185048 }
185049
185050constraint:
185051 rtreeRelease(pRtree);
185052 return rc;
185053}
185054
185055/*
185056** Called when a transaction starts.
185057*/
185058static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
185059 Rtree *pRtree = (Rtree *)pVtab;
185060 assert( pRtree->inWrTrans==0 )((void) (0));
185061 pRtree->inWrTrans++;
185062 return SQLITE_OK0;
185063}
185064
185065/*
185066** Called when a transaction completes (either by COMMIT or ROLLBACK).
185067** The sqlite3_blob object should be released at this point.
185068*/
185069static int rtreeEndTransaction(sqlite3_vtab *pVtab){
185070 Rtree *pRtree = (Rtree *)pVtab;
185071 pRtree->inWrTrans = 0;
185072 nodeBlobReset(pRtree);
185073 return SQLITE_OK0;
185074}
185075
185076/*
185077** The xRename method for rtree module virtual tables.
185078*/
185079static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
185080 Rtree *pRtree = (Rtree *)pVtab;
185081 int rc = SQLITE_NOMEM7;
185082 char *zSql = sqlite3_mprintf(
185083 "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
185084 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
185085 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
185086 , pRtree->zDb, pRtree->zName, zNewName
185087 , pRtree->zDb, pRtree->zName, zNewName
185088 , pRtree->zDb, pRtree->zName, zNewName
185089 );
185090 if( zSql ){
185091 nodeBlobReset(pRtree);
185092 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
185093 sqlite3_free(zSql);
185094 }
185095 return rc;
185096}
185097
185098/*
185099** The xSavepoint method.
185100**
185101** This module does not need to do anything to support savepoints. However,
185102** it uses this hook to close any open blob handle. This is done because a
185103** DROP TABLE command - which fortunately always opens a savepoint - cannot
185104** succeed if there are any open blob handles. i.e. if the blob handle were
185105** not closed here, the following would fail:
185106**
185107** BEGIN;
185108** INSERT INTO rtree...
185109** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
185110** COMMIT;
185111*/
185112static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
185113 Rtree *pRtree = (Rtree *)pVtab;
185114 u8 iwt = pRtree->inWrTrans;
185115 UNUSED_PARAMETER(iSavepoint)(void)(iSavepoint);
185116 pRtree->inWrTrans = 0;
185117 nodeBlobReset(pRtree);
185118 pRtree->inWrTrans = iwt;
185119 return SQLITE_OK0;
185120}
185121
185122/*
185123** This function populates the pRtree->nRowEst variable with an estimate
185124** of the number of rows in the virtual table. If possible, this is based
185125** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
185126*/
185127static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
185128 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
185129 char *zSql;
185130 sqlite3_stmt *p;
185131 int rc;
185132 i64 nRow = 0;
185133
185134 rc = sqlite3_table_column_metadata(
185135 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
185136 );
185137 if( rc!=SQLITE_OK0 ){
185138 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
185139 return rc==SQLITE_ERROR1 ? SQLITE_OK0 : rc;
185140 }
185141 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
185142 if( zSql==0 ){
185143 rc = SQLITE_NOMEM7;
185144 }else{
185145 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
185146 if( rc==SQLITE_OK0 ){
185147 if( sqlite3_step(p)==SQLITE_ROW100 ) nRow = sqlite3_column_int64(p, 0);
185148 rc = sqlite3_finalize(p);
185149 }else if( rc!=SQLITE_NOMEM7 ){
185150 rc = SQLITE_OK0;
185151 }
185152
185153 if( rc==SQLITE_OK0 ){
185154 if( nRow==0 ){
185155 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
185156 }else{
185157 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST)((nRow)>(RTREE_MIN_ROWEST)?(nRow):(RTREE_MIN_ROWEST));
185158 }
185159 }
185160 sqlite3_free(zSql);
185161 }
185162
185163 return rc;
185164}
185165
185166
185167/*
185168** Return true if zName is the extension on one of the shadow tables used
185169** by this module.
185170*/
185171static int rtreeShadowName(const char *zName){
185172 static const char *azName[] = {
185173 "node", "parent", "rowid"
185174 };
185175 unsigned int i;
185176 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
185177 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
185178 }
185179 return 0;
185180}
185181
185182static sqlite3_module rtreeModule = {
185183 3, /* iVersion */
185184 rtreeCreate, /* xCreate - create a table */
185185 rtreeConnect, /* xConnect - connect to an existing table */
185186 rtreeBestIndex, /* xBestIndex - Determine search strategy */
185187 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
185188 rtreeDestroy, /* xDestroy - Drop a table */
185189 rtreeOpen, /* xOpen - open a cursor */
185190 rtreeClose, /* xClose - close a cursor */
185191 rtreeFilter, /* xFilter - configure scan constraints */
185192 rtreeNext, /* xNext - advance a cursor */
185193 rtreeEof, /* xEof */
185194 rtreeColumn, /* xColumn - read data */
185195 rtreeRowid, /* xRowid - read data */
185196 rtreeUpdate, /* xUpdate - write data */
185197 rtreeBeginTransaction, /* xBegin - begin transaction */
185198 rtreeEndTransaction, /* xSync - sync transaction */
185199 rtreeEndTransaction, /* xCommit - commit transaction */
185200 rtreeEndTransaction, /* xRollback - rollback transaction */
185201 0, /* xFindFunction - function overloading */
185202 rtreeRename, /* xRename - rename the table */
185203 rtreeSavepoint, /* xSavepoint */
185204 0, /* xRelease */
185205 0, /* xRollbackTo */
185206 rtreeShadowName /* xShadowName */
185207};
185208
185209static int rtreeSqlInit(
185210 Rtree *pRtree,
185211 sqlite3 *db,
185212 const char *zDb,
185213 const char *zPrefix,
185214 int isCreate
185215){
185216 int rc = SQLITE_OK0;
185217
185218 #define N_STATEMENT 8
185219 static const char *azSql[N_STATEMENT] = {
185220 /* Write the xxx_node table */
185221 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
185222 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
185223
185224 /* Read and write the xxx_rowid table */
185225 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
185226 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
185227 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
185228
185229 /* Read and write the xxx_parent table */
185230 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
185231 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
185232 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
185233 };
185234 sqlite3_stmt **appStmt[N_STATEMENT];
185235 int i;
185236 const int f = SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04;
185237
185238 pRtree->db = db;
185239
185240 if( isCreate ){
185241 char *zCreate;
185242 sqlite3_str *p = sqlite3_str_new(db);
185243 int ii;
185244 sqlite3_str_appendf(p,
185245 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
185246 zDb, zPrefix);
185247 for(ii=0; ii<pRtree->nAux; ii++){
185248 sqlite3_str_appendf(p,",a%d",ii);
185249 }
185250 sqlite3_str_appendf(p,
185251 ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
185252 zDb, zPrefix);
185253 sqlite3_str_appendf(p,
185254 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
185255 zDb, zPrefix);
185256 sqlite3_str_appendf(p,
185257 "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
185258 zDb, zPrefix, pRtree->iNodeSize);
185259 zCreate = sqlite3_str_finish(p);
185260 if( !zCreate ){
185261 return SQLITE_NOMEM7;
185262 }
185263 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
185264 sqlite3_free(zCreate);
185265 if( rc!=SQLITE_OK0 ){
185266 return rc;
185267 }
185268 }
185269
185270 appStmt[0] = &pRtree->pWriteNode;
185271 appStmt[1] = &pRtree->pDeleteNode;
185272 appStmt[2] = &pRtree->pReadRowid;
185273 appStmt[3] = &pRtree->pWriteRowid;
185274 appStmt[4] = &pRtree->pDeleteRowid;
185275 appStmt[5] = &pRtree->pReadParent;
185276 appStmt[6] = &pRtree->pWriteParent;
185277 appStmt[7] = &pRtree->pDeleteParent;
185278
185279 rc = rtreeQueryStat1(db, pRtree);
185280 for(i=0; i<N_STATEMENT && rc==SQLITE_OK0; i++){
185281 char *zSql;
185282 const char *zFormat;
185283 if( i!=3 || pRtree->nAux==0 ){
185284 zFormat = azSql[i];
185285 }else {
185286 /* An UPSERT is very slightly slower than REPLACE, but it is needed
185287 ** if there are auxiliary columns */
185288 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
185289 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
185290 }
185291 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
185292 if( zSql ){
185293 rc = sqlite3_prepare_v3(db, zSql, -1, f, appStmt[i], 0);
185294 }else{
185295 rc = SQLITE_NOMEM7;
185296 }
185297 sqlite3_free(zSql);
185298 }
185299 if( pRtree->nAux ){
185300 pRtree->zReadAuxSql = sqlite3_mprintf(
185301 "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
185302 zDb, zPrefix);
185303 if( pRtree->zReadAuxSql==0 ){
185304 rc = SQLITE_NOMEM7;
185305 }else{
185306 sqlite3_str *p = sqlite3_str_new(db);
185307 int ii;
185308 char *zSql;
185309 sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
185310 for(ii=0; ii<pRtree->nAux; ii++){
185311 if( ii ) sqlite3_str_append(p, ",", 1);
185312 if( ii<pRtree->nAuxNotNull ){
185313 sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
185314 }else{
185315 sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
185316 }
185317 }
185318 sqlite3_str_appendf(p, " WHERE rowid=?1");
185319 zSql = sqlite3_str_finish(p);
185320 if( zSql==0 ){
185321 rc = SQLITE_NOMEM7;
185322 }else{
185323 rc = sqlite3_prepare_v3(db, zSql, -1, f, &pRtree->pWriteAux, 0);
185324 sqlite3_free(zSql);
185325 }
185326 }
185327 }
185328
185329 return rc;
185330}
185331
185332/*
185333** The second argument to this function contains the text of an SQL statement
185334** that returns a single integer value. The statement is compiled and executed
185335** using database connection db. If successful, the integer value returned
185336** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
185337** code is returned and the value of *piVal after returning is not defined.
185338*/
185339static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
185340 int rc = SQLITE_NOMEM7;
185341 if( zSql ){
185342 sqlite3_stmt *pStmt = 0;
185343 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
185344 if( rc==SQLITE_OK0 ){
185345 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
185346 *piVal = sqlite3_column_int(pStmt, 0);
185347 }
185348 rc = sqlite3_finalize(pStmt);
185349 }
185350 }
185351 return rc;
185352}
185353
185354/*
185355** This function is called from within the xConnect() or xCreate() method to
185356** determine the node-size used by the rtree table being created or connected
185357** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
185358** Otherwise, an SQLite error code is returned.
185359**
185360** If this function is being called as part of an xConnect(), then the rtree
185361** table already exists. In this case the node-size is determined by inspecting
185362** the root node of the tree.
185363**
185364** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
185365** This ensures that each node is stored on a single database page. If the
185366** database page-size is so large that more than RTREE_MAXCELLS entries
185367** would fit in a single node, use a smaller node-size.
185368*/
185369static int getNodeSize(
185370 sqlite3 *db, /* Database handle */
185371 Rtree *pRtree, /* Rtree handle */
185372 int isCreate, /* True for xCreate, false for xConnect */
185373 char **pzErr /* OUT: Error message, if any */
185374){
185375 int rc;
185376 char *zSql;
185377 if( isCreate ){
185378 int iPageSize = 0;
185379 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
185380 rc = getIntFromStmt(db, zSql, &iPageSize);
185381 if( rc==SQLITE_OK0 ){
185382 pRtree->iNodeSize = iPageSize-64;
185383 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
185384 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
185385 }
185386 }else{
185387 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
185388 }
185389 }else{
185390 zSql = sqlite3_mprintf(
185391 "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
185392 pRtree->zDb, pRtree->zName
185393 );
185394 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
185395 if( rc!=SQLITE_OK0 ){
185396 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
185397 }else if( pRtree->iNodeSize<(512-64) ){
185398 rc = SQLITE_CORRUPT_VTAB(11 | (1<<8));
185399 RTREE_IS_CORRUPT(pRtree);
185400 *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
185401 pRtree->zName);
185402 }
185403 }
185404
185405 sqlite3_free(zSql);
185406 return rc;
185407}
185408
185409/*
185410** This function is the implementation of both the xConnect and xCreate
185411** methods of the r-tree virtual table.
185412**
185413** argv[0] -> module name
185414** argv[1] -> database name
185415** argv[2] -> table name
185416** argv[...] -> column names...
185417*/
185418static int rtreeInit(
185419 sqlite3 *db, /* Database connection */
185420 void *pAux, /* One of the RTREE_COORD_* constants */
185421 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
185422 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
185423 char **pzErr, /* OUT: Error message, if any */
185424 int isCreate /* True for xCreate, false for xConnect */
185425){
185426 int rc = SQLITE_OK0;
185427 Rtree *pRtree;
185428 int nDb; /* Length of string argv[1] */
185429 int nName; /* Length of string argv[2] */
185430 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
185431 sqlite3_str *pSql;
185432 char *zSql;
185433 int ii = 4;
185434 int iErr;
185435
185436 const char *aErrMsg[] = {
185437 0, /* 0 */
185438 "Wrong number of columns for an rtree table", /* 1 */
185439 "Too few columns for an rtree table", /* 2 */
185440 "Too many columns for an rtree table", /* 3 */
185441 "Auxiliary rtree columns must be last" /* 4 */
185442 };
185443
185444 assert( RTREE_MAX_AUX_COLUMN<256 )((void) (0)); /* Aux columns counted by a u8 */
185445 if( argc>RTREE_MAX_AUX_COLUMN+3 ){
185446 *pzErr = sqlite3_mprintf("%s", aErrMsg[3]);
185447 return SQLITE_ERROR1;
185448 }
185449
185450 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
185451
185452 /* Allocate the sqlite3_vtab structure */
185453 nDb = (int)strlen(argv[1]);
185454 nName = (int)strlen(argv[2]);
185455 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
185456 if( !pRtree ){
185457 return SQLITE_NOMEM7;
185458 }
185459 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
185460 pRtree->nBusy = 1;
185461 pRtree->base.pModule = &rtreeModule;
185462 pRtree->zDb = (char *)&pRtree[1];
185463 pRtree->zName = &pRtree->zDb[nDb+1];
185464 pRtree->eCoordType = (u8)eCoordType;
185465 memcpy(pRtree->zDb, argv[1], nDb);
185466 memcpy(pRtree->zName, argv[2], nName);
185467
185468
185469 /* Create/Connect to the underlying relational database schema. If
185470 ** that is successful, call sqlite3_declare_vtab() to configure
185471 ** the r-tree table schema.
185472 */
185473 pSql = sqlite3_str_new(db);
185474 sqlite3_str_appendf(pSql, "CREATE TABLE x(%s", argv[3]);
185475 for(ii=4; ii<argc; ii++){
185476 if( argv[ii][0]=='+' ){
185477 pRtree->nAux++;
185478 sqlite3_str_appendf(pSql, ",%s", argv[ii]+1);
185479 }else if( pRtree->nAux>0 ){
185480 break;
185481 }else{
185482 pRtree->nDim2++;
185483 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
185484 }
185485 }
185486 sqlite3_str_appendf(pSql, ");");
185487 zSql = sqlite3_str_finish(pSql);
185488 if( !zSql ){
185489 rc = SQLITE_NOMEM7;
185490 }else if( ii<argc ){
185491 *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
185492 rc = SQLITE_ERROR1;
185493 }else if( SQLITE_OK0!=(rc = sqlite3_declare_vtab(db, zSql)) ){
185494 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
185495 }
185496 sqlite3_free(zSql);
185497 if( rc ) goto rtreeInit_fail;
185498 pRtree->nDim = pRtree->nDim2/2;
185499 if( pRtree->nDim<1 ){
185500 iErr = 2;
185501 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
185502 iErr = 3;
185503 }else if( pRtree->nDim2 % 2 ){
185504 iErr = 1;
185505 }else{
185506 iErr = 0;
185507 }
185508 if( iErr ){
185509 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
185510 goto rtreeInit_fail;
185511 }
185512 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
185513
185514 /* Figure out the node size to use. */
185515 rc = getNodeSize(db, pRtree, isCreate, pzErr);
185516 if( rc ) goto rtreeInit_fail;
185517 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
185518 if( rc ){
185519 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
185520 goto rtreeInit_fail;
185521 }
185522
185523 *ppVtab = (sqlite3_vtab *)pRtree;
185524 return SQLITE_OK0;
185525
185526rtreeInit_fail:
185527 if( rc==SQLITE_OK0 ) rc = SQLITE_ERROR1;
185528 assert( *ppVtab==0 )((void) (0));
185529 assert( pRtree->nBusy==1 )((void) (0));
185530 rtreeRelease(pRtree);
185531 return rc;
185532}
185533
185534
185535/*
185536** Implementation of a scalar function that decodes r-tree nodes to
185537** human readable strings. This can be used for debugging and analysis.
185538**
185539** The scalar function takes two arguments: (1) the number of dimensions
185540** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
185541** an r-tree node. For a two-dimensional r-tree structure called "rt", to
185542** deserialize all nodes, a statement like:
185543**
185544** SELECT rtreenode(2, data) FROM rt_node;
185545**
185546** The human readable string takes the form of a Tcl list with one
185547** entry for each cell in the r-tree node. Each entry is itself a
185548** list, containing the 8-byte rowid/pageno followed by the
185549** <num-dimension>*2 coordinates.
185550*/
185551static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
185552 RtreeNode node;
185553 Rtree tree;
185554 int ii;
185555 int nData;
185556 int errCode;
185557 sqlite3_str *pOut;
185558
185559 UNUSED_PARAMETER(nArg)(void)(nArg);
185560 memset(&node, 0, sizeof(RtreeNode));
185561 memset(&tree, 0, sizeof(Rtree));
185562 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
185563 if( tree.nDim<1 || tree.nDim>5 ) return;
185564 tree.nDim2 = tree.nDim*2;
185565 tree.nBytesPerCell = 8 + 8 * tree.nDim;
185566 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
185567 nData = sqlite3_value_bytes(apArg[1]);
185568 if( nData<4 ) return;
185569 if( nData<NCELL(&node)*tree.nBytesPerCell ) return;
185570
185571 pOut = sqlite3_str_new(0);
185572 for(ii=0; ii<NCELL(&node); ii++){
185573 RtreeCell cell;
185574 int jj;
185575
185576 nodeGetCell(&tree, &node, ii, &cell);
185577 if( ii>0 ) sqlite3_str_append(pOut, " ", 1);
185578 sqlite3_str_appendf(pOut, "{%lld", cell.iRowid);
185579 for(jj=0; jj<tree.nDim2; jj++){
185580#ifndef SQLITE_RTREE_INT_ONLY
185581 sqlite3_str_appendf(pOut, " %g", (double)cell.aCoord[jj].f);
185582#else
185583 sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
185584#endif
185585 }
185586 sqlite3_str_append(pOut, "}", 1);
185587 }
185588 errCode = sqlite3_str_errcode(pOut);
185589 sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
185590 sqlite3_result_error_code(ctx, errCode);
185591}
185592
185593/* This routine implements an SQL function that returns the "depth" parameter
185594** from the front of a blob that is an r-tree node. For example:
185595**
185596** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
185597**
185598** The depth value is 0 for all nodes other than the root node, and the root
185599** node always has nodeno=1, so the example above is the primary use for this
185600** routine. This routine is intended for testing and analysis only.
185601*/
185602static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
185603 UNUSED_PARAMETER(nArg)(void)(nArg);
185604 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB4
185605 || sqlite3_value_bytes(apArg[0])<2
185606 ){
185607 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
185608 }else{
185609 u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
185610 sqlite3_result_int(ctx, readInt16(zBlob));
185611 }
185612}
185613
185614/*
185615** Context object passed between the various routines that make up the
185616** implementation of integrity-check function rtreecheck().
185617*/
185618typedef struct RtreeCheck RtreeCheck;
185619struct RtreeCheck {
185620 sqlite3 *db; /* Database handle */
185621 const char *zDb; /* Database containing rtree table */
185622 const char *zTab; /* Name of rtree table */
185623 int bInt; /* True for rtree_i32 table */
185624 int nDim; /* Number of dimensions for this rtree tbl */
185625 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
185626 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
185627 int nLeaf; /* Number of leaf cells in table */
185628 int nNonLeaf; /* Number of non-leaf cells in table */
185629 int rc; /* Return code */
185630 char *zReport; /* Message to report */
185631 int nErr; /* Number of lines in zReport */
185632};
185633
185634#define RTREE_CHECK_MAX_ERROR 100
185635
185636/*
185637** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
185638** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
185639*/
185640static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
185641 int rc = sqlite3_reset(pStmt);
185642 if( pCheck->rc==SQLITE_OK0 ) pCheck->rc = rc;
185643}
185644
185645/*
185646** The second and subsequent arguments to this function are a format string
185647** and printf style arguments. This function formats the string and attempts
185648** to compile it as an SQL statement.
185649**
185650** If successful, a pointer to the new SQL statement is returned. Otherwise,
185651** NULL is returned and an error code left in RtreeCheck.rc.
185652*/
185653static sqlite3_stmt *rtreeCheckPrepare(
185654 RtreeCheck *pCheck, /* RtreeCheck object */
185655 const char *zFmt, ... /* Format string and trailing args */
185656){
185657 va_list ap;
185658 char *z;
185659 sqlite3_stmt *pRet = 0;
185660
185661 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
185662 z = sqlite3_vmprintf(zFmt, ap);
185663
185664 if( pCheck->rc==SQLITE_OK0 ){
185665 if( z==0 ){
185666 pCheck->rc = SQLITE_NOMEM7;
185667 }else{
185668 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
185669 }
185670 }
185671
185672 sqlite3_free(z);
185673 va_end(ap)__builtin_va_end(ap);
185674 return pRet;
185675}
185676
185677/*
185678** The second and subsequent arguments to this function are a printf()
185679** style format string and arguments. This function formats the string and
185680** appends it to the report being accumuated in pCheck.
185681*/
185682static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
185683 va_list ap;
185684 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
185685 if( pCheck->rc==SQLITE_OK0 && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
185686 char *z = sqlite3_vmprintf(zFmt, ap);
185687 if( z==0 ){
185688 pCheck->rc = SQLITE_NOMEM7;
185689 }else{
185690 pCheck->zReport = sqlite3_mprintf("%z%s%z",
185691 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
185692 );
185693 if( pCheck->zReport==0 ){
185694 pCheck->rc = SQLITE_NOMEM7;
185695 }
185696 }
185697 pCheck->nErr++;
185698 }
185699 va_end(ap)__builtin_va_end(ap);
185700}
185701
185702/*
185703** This function is a no-op if there is already an error code stored
185704** in the RtreeCheck object indicated by the first argument. NULL is
185705** returned in this case.
185706**
185707** Otherwise, the contents of rtree table node iNode are loaded from
185708** the database and copied into a buffer obtained from sqlite3_malloc().
185709** If no error occurs, a pointer to the buffer is returned and (*pnNode)
185710** is set to the size of the buffer in bytes.
185711**
185712** Or, if an error does occur, NULL is returned and an error code left
185713** in the RtreeCheck object. The final value of *pnNode is undefined in
185714** this case.
185715*/
185716static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
185717 u8 *pRet = 0; /* Return value */
185718
185719 if( pCheck->rc==SQLITE_OK0 && pCheck->pGetNode==0 ){
185720 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
185721 "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
185722 pCheck->zDb, pCheck->zTab
185723 );
185724 }
185725
185726 if( pCheck->rc==SQLITE_OK0 ){
185727 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
185728 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW100 ){
185729 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
185730 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
185731 pRet = sqlite3_malloc64(nNode);
185732 if( pRet==0 ){
185733 pCheck->rc = SQLITE_NOMEM7;
185734 }else{
185735 memcpy(pRet, pNode, nNode);
185736 *pnNode = nNode;
185737 }
185738 }
185739 rtreeCheckReset(pCheck, pCheck->pGetNode);
185740 if( pCheck->rc==SQLITE_OK0 && pRet==0 ){
185741 rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
185742 }
185743 }
185744
185745 return pRet;
185746}
185747
185748/*
185749** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
185750** (if bLeaf==1) table contains a specified entry. The schemas of the
185751** two tables are:
185752**
185753** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
185754** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
185755**
185756** In both cases, this function checks that there exists an entry with
185757** IPK value iKey and the second column set to iVal.
185758**
185759*/
185760static void rtreeCheckMapping(
185761 RtreeCheck *pCheck, /* RtreeCheck object */
185762 int bLeaf, /* True for a leaf cell, false for interior */
185763 i64 iKey, /* Key for mapping */
185764 i64 iVal /* Expected value for mapping */
185765){
185766 int rc;
185767 sqlite3_stmt *pStmt;
185768 const char *azSql[2] = {
185769 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
185770 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
185771 };
185772
185773 assert( bLeaf==0 || bLeaf==1 )((void) (0));
185774 if( pCheck->aCheckMapping[bLeaf]==0 ){
185775 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
185776 azSql[bLeaf], pCheck->zDb, pCheck->zTab
185777 );
185778 }
185779 if( pCheck->rc!=SQLITE_OK0 ) return;
185780
185781 pStmt = pCheck->aCheckMapping[bLeaf];
185782 sqlite3_bind_int64(pStmt, 1, iKey);
185783 rc = sqlite3_step(pStmt);
185784 if( rc==SQLITE_DONE101 ){
185785 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
185786 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
185787 );
185788 }else if( rc==SQLITE_ROW100 ){
185789 i64 ii = sqlite3_column_int64(pStmt, 0);
185790 if( ii!=iVal ){
185791 rtreeCheckAppendMsg(pCheck,
185792 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
185793 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
185794 );
185795 }
185796 }
185797 rtreeCheckReset(pCheck, pStmt);
185798}
185799
185800/*
185801** Argument pCell points to an array of coordinates stored on an rtree page.
185802** This function checks that the coordinates are internally consistent (no
185803** x1>x2 conditions) and adds an error message to the RtreeCheck object
185804** if they are not.
185805**
185806** Additionally, if pParent is not NULL, then it is assumed to point to
185807** the array of coordinates on the parent page that bound the page
185808** containing pCell. In this case it is also verified that the two
185809** sets of coordinates are mutually consistent and an error message added
185810** to the RtreeCheck object if they are not.
185811*/
185812static void rtreeCheckCellCoord(
185813 RtreeCheck *pCheck,
185814 i64 iNode, /* Node id to use in error messages */
185815 int iCell, /* Cell number to use in error messages */
185816 u8 *pCell, /* Pointer to cell coordinates */
185817 u8 *pParent /* Pointer to parent coordinates */
185818){
185819 RtreeCoord c1, c2;
185820 RtreeCoord p1, p2;
185821 int i;
185822
185823 for(i=0; i<pCheck->nDim; i++){
185824 readCoord(&pCell[4*2*i], &c1);
185825 readCoord(&pCell[4*(2*i + 1)], &c2);
185826
185827 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
185828 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
185829 rtreeCheckAppendMsg(pCheck,
185830 "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
185831 );
185832 }
185833
185834 if( pParent ){
185835 readCoord(&pParent[4*2*i], &p1);
185836 readCoord(&pParent[4*(2*i + 1)], &p2);
185837
185838 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
185839 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
185840 ){
185841 rtreeCheckAppendMsg(pCheck,
185842 "Dimension %d of cell %d on node %lld is corrupt relative to parent"
185843 , i, iCell, iNode
185844 );
185845 }
185846 }
185847 }
185848}
185849
185850/*
185851** Run rtreecheck() checks on node iNode, which is at depth iDepth within
185852** the r-tree structure. Argument aParent points to the array of coordinates
185853** that bound node iNode on the parent node.
185854**
185855** If any problems are discovered, an error message is appended to the
185856** report accumulated in the RtreeCheck object.
185857*/
185858static void rtreeCheckNode(
185859 RtreeCheck *pCheck,
185860 int iDepth, /* Depth of iNode (0==leaf) */
185861 u8 *aParent, /* Buffer containing parent coords */
185862 i64 iNode /* Node to check */
185863){
185864 u8 *aNode = 0;
185865 int nNode = 0;
185866
185867 assert( iNode==1 || aParent!=0 )((void) (0));
185868 assert( pCheck->nDim>0 )((void) (0));
185869
185870 aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
185871 if( aNode ){
185872 if( nNode<4 ){
185873 rtreeCheckAppendMsg(pCheck,
185874 "Node %lld is too small (%d bytes)", iNode, nNode
185875 );
185876 }else{
185877 int nCell; /* Number of cells on page */
185878 int i; /* Used to iterate through cells */
185879 if( aParent==0 ){
185880 iDepth = readInt16(aNode);
185881 if( iDepth>RTREE_MAX_DEPTH ){
185882 rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
185883 sqlite3_free(aNode);
185884 return;
185885 }
185886 }
185887 nCell = readInt16(&aNode[2]);
185888 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
185889 rtreeCheckAppendMsg(pCheck,
185890 "Node %lld is too small for cell count of %d (%d bytes)",
185891 iNode, nCell, nNode
185892 );
185893 }else{
185894 for(i=0; i<nCell; i++){
185895 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
185896 i64 iVal = readInt64(pCell);
185897 rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
185898
185899 if( iDepth>0 ){
185900 rtreeCheckMapping(pCheck, 0, iVal, iNode);
185901 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
185902 pCheck->nNonLeaf++;
185903 }else{
185904 rtreeCheckMapping(pCheck, 1, iVal, iNode);
185905 pCheck->nLeaf++;
185906 }
185907 }
185908 }
185909 }
185910 sqlite3_free(aNode);
185911 }
185912}
185913
185914/*
185915** The second argument to this function must be either "_rowid" or
185916** "_parent". This function checks that the number of entries in the
185917** %_rowid or %_parent table is exactly nExpect. If not, it adds
185918** an error message to the report in the RtreeCheck object indicated
185919** by the first argument.
185920*/
185921static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
185922 if( pCheck->rc==SQLITE_OK0 ){
185923 sqlite3_stmt *pCount;
185924 pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
185925 pCheck->zDb, pCheck->zTab, zTbl
185926 );
185927 if( pCount ){
185928 if( sqlite3_step(pCount)==SQLITE_ROW100 ){
185929 i64 nActual = sqlite3_column_int64(pCount, 0);
185930 if( nActual!=nExpect ){
185931 rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
185932 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
185933 );
185934 }
185935 }
185936 pCheck->rc = sqlite3_finalize(pCount);
185937 }
185938 }
185939}
185940
185941/*
185942** This function does the bulk of the work for the rtree integrity-check.
185943** It is called by rtreecheck(), which is the SQL function implementation.
185944*/
185945static int rtreeCheckTable(
185946 sqlite3 *db, /* Database handle to access db through */
185947 const char *zDb, /* Name of db ("main", "temp" etc.) */
185948 const char *zTab, /* Name of rtree table to check */
185949 char **pzReport /* OUT: sqlite3_malloc'd report text */
185950){
185951 RtreeCheck check; /* Common context for various routines */
185952 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
185953 int bEnd = 0; /* True if transaction should be closed */
185954 int nAux = 0; /* Number of extra columns. */
185955
185956 /* Initialize the context object */
185957 memset(&check, 0, sizeof(check));
185958 check.db = db;
185959 check.zDb = zDb;
185960 check.zTab = zTab;
185961
185962 /* If there is not already an open transaction, open one now. This is
185963 ** to ensure that the queries run as part of this integrity-check operate
185964 ** on a consistent snapshot. */
185965 if( sqlite3_get_autocommit(db) ){
185966 check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
185967 bEnd = 1;
185968 }
185969
185970 /* Find the number of auxiliary columns */
185971 if( check.rc==SQLITE_OK0 ){
185972 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
185973 if( pStmt ){
185974 nAux = sqlite3_column_count(pStmt) - 2;
185975 sqlite3_finalize(pStmt);
185976 }
185977 check.rc = SQLITE_OK0;
185978 }
185979
185980 /* Find number of dimensions in the rtree table. */
185981 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
185982 if( pStmt ){
185983 int rc;
185984 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
185985 if( check.nDim<1 ){
185986 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
185987 }else if( SQLITE_ROW100==sqlite3_step(pStmt) ){
185988 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER1);
185989 }
185990 rc = sqlite3_finalize(pStmt);
185991 if( rc!=SQLITE_CORRUPT11 ) check.rc = rc;
185992 }
185993
185994 /* Do the actual integrity-check */
185995 if( check.nDim>=1 ){
185996 if( check.rc==SQLITE_OK0 ){
185997 rtreeCheckNode(&check, 0, 0, 1);
185998 }
185999 rtreeCheckCount(&check, "_rowid", check.nLeaf);
186000 rtreeCheckCount(&check, "_parent", check.nNonLeaf);
186001 }
186002
186003 /* Finalize SQL statements used by the integrity-check */
186004 sqlite3_finalize(check.pGetNode);
186005 sqlite3_finalize(check.aCheckMapping[0]);
186006 sqlite3_finalize(check.aCheckMapping[1]);
186007
186008 /* If one was opened, close the transaction */
186009 if( bEnd ){
186010 int rc = sqlite3_exec(db, "END", 0, 0, 0);
186011 if( check.rc==SQLITE_OK0 ) check.rc = rc;
186012 }
186013 *pzReport = check.zReport;
186014 return check.rc;
186015}
186016
186017/*
186018** Usage:
186019**
186020** rtreecheck(<rtree-table>);
186021** rtreecheck(<database>, <rtree-table>);
186022**
186023** Invoking this SQL function runs an integrity-check on the named rtree
186024** table. The integrity-check verifies the following:
186025**
186026** 1. For each cell in the r-tree structure (%_node table), that:
186027**
186028** a) for each dimension, (coord1 <= coord2).
186029**
186030** b) unless the cell is on the root node, that the cell is bounded
186031** by the parent cell on the parent node.
186032**
186033** c) for leaf nodes, that there is an entry in the %_rowid
186034** table corresponding to the cell's rowid value that
186035** points to the correct node.
186036**
186037** d) for cells on non-leaf nodes, that there is an entry in the
186038** %_parent table mapping from the cell's child node to the
186039** node that it resides on.
186040**
186041** 2. That there are the same number of entries in the %_rowid table
186042** as there are leaf cells in the r-tree structure, and that there
186043** is a leaf cell that corresponds to each entry in the %_rowid table.
186044**
186045** 3. That there are the same number of entries in the %_parent table
186046** as there are non-leaf cells in the r-tree structure, and that
186047** there is a non-leaf cell that corresponds to each entry in the
186048** %_parent table.
186049*/
186050static void rtreecheck(
186051 sqlite3_context *ctx,
186052 int nArg,
186053 sqlite3_value **apArg
186054){
186055 if( nArg!=1 && nArg!=2 ){
186056 sqlite3_result_error(ctx,
186057 "wrong number of arguments to function rtreecheck()", -1
186058 );
186059 }else{
186060 int rc;
186061 char *zReport = 0;
186062 const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
186063 const char *zTab;
186064 if( nArg==1 ){
186065 zTab = zDb;
186066 zDb = "main";
186067 }else{
186068 zTab = (const char*)sqlite3_value_text(apArg[1]);
186069 }
186070 rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
186071 if( rc==SQLITE_OK0 ){
186072 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186073 }else{
186074 sqlite3_result_error_code(ctx, rc);
186075 }
186076 sqlite3_free(zReport);
186077 }
186078}
186079
186080/* Conditionally include the geopoly code */
186081#ifdef SQLITE_ENABLE_GEOPOLY
186082/************** Include geopoly.c in the middle of rtree.c *******************/
186083/************** Begin file geopoly.c *****************************************/
186084/*
186085** 2018-05-25
186086**
186087** The author disclaims copyright to this source code. In place of
186088** a legal notice, here is a blessing:
186089**
186090** May you do good and not evil.
186091** May you find forgiveness for yourself and forgive others.
186092** May you share freely, never taking more than you give.
186093**
186094******************************************************************************
186095**
186096** This file implements an alternative R-Tree virtual table that
186097** uses polygons to express the boundaries of 2-dimensional objects.
186098**
186099** This file is #include-ed onto the end of "rtree.c" so that it has
186100** access to all of the R-Tree internals.
186101*/
186102/* #include <stdlib.h> */
186103
186104/* Enable -DGEOPOLY_ENABLE_DEBUG for debugging facilities */
186105#ifdef GEOPOLY_ENABLE_DEBUG
186106 static int geo_debug = 0;
186107# define GEODEBUG(X) if(geo_debug)printf X
186108#else
186109# define GEODEBUG(X)
186110#endif
186111
186112#ifndef JSON_NULL /* The following stuff repeats things found in json1 */
186113/*
186114** Versions of isspace(), isalnum() and isdigit() to which it is safe
186115** to pass signed char values.
186116*/
186117#ifdef sqlite3Isdigit
186118 /* Use the SQLite core versions if this routine is part of the
186119 ** SQLite amalgamation */
186120# define safe_isdigit(x) sqlite3Isdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x04)
186121# define safe_isalnum(x) sqlite3Isalnum(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x06)
186122# define safe_isxdigit(x) sqlite3Isxdigit(x)(sqlite3CtypeMap[(unsigned char)(x)]&0x08)
186123#else
186124 /* Use the standard library for separate compilation */
186125#include <ctype.h> /* amalgamator: keep */
186126# define safe_isdigit(x) isdigit((unsigned char)(x))
186127# define safe_isalnum(x) isalnum((unsigned char)(x))
186128# define safe_isxdigit(x) isxdigit((unsigned char)(x))
186129#endif
186130
186131/*
186132** Growing our own isspace() routine this way is twice as fast as
186133** the library isspace() function.
186134*/
186135static const char geopolyIsSpace[] = {
186136 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
186137 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186138 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186139 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186140 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186144 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186145 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186152};
186153#define safe_isspace(x) (geopolyIsSpace[(unsigned char)x])
186154#endif /* JSON NULL - back to original code */
186155
186156/* Compiler and version */
186157#ifndef GCC_VERSION(4*1000000+2*1000+1)
186158#if defined(__GNUC__4) && !defined(SQLITE_DISABLE_INTRINSIC)
186159# define GCC_VERSION(4*1000000+2*1000+1) (__GNUC__4*1000000+__GNUC_MINOR__2*1000+__GNUC_PATCHLEVEL__1)
186160#else
186161# define GCC_VERSION(4*1000000+2*1000+1) 0
186162#endif
186163#endif
186164#ifndef MSVC_VERSION0
186165#if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
186166# define MSVC_VERSION0 _MSC_VER
186167#else
186168# define MSVC_VERSION0 0
186169#endif
186170#endif
186171
186172/* Datatype for coordinates
186173*/
186174typedef float GeoCoord;
186175
186176/*
186177** Internal representation of a polygon.
186178**
186179** The polygon consists of a sequence of vertexes. There is a line
186180** segment between each pair of vertexes, and one final segment from
186181** the last vertex back to the first. (This differs from the GeoJSON
186182** standard in which the final vertex is a repeat of the first.)
186183**
186184** The polygon follows the right-hand rule. The area to the right of
186185** each segment is "outside" and the area to the left is "inside".
186186**
186187** The on-disk representation consists of a 4-byte header followed by
186188** the values. The 4-byte header is:
186189**
186190** encoding (1 byte) 0=big-endian, 1=little-endian
186191** nvertex (3 bytes) Number of vertexes as a big-endian integer
186192**
186193** Enough space is allocated for 4 coordinates, to work around over-zealous
186194** warnings coming from some compiler (notably, clang). In reality, the size
186195** of each GeoPoly memory allocate is adjusted as necessary so that the
186196** GeoPoly.a[] array at the end is the appropriate size.
186197*/
186198typedef struct GeoPoly GeoPoly;
186199struct GeoPoly {
186200 int nVertex; /* Number of vertexes */
186201 unsigned char hdr[4]; /* Header for on-disk representation */
186202 GeoCoord a[8]; /* 2*nVertex values. X (longitude) first, then Y */
186203};
186204
186205/* The size of a memory allocation needed for a GeoPoly object sufficient
186206** to hold N coordinate pairs.
186207*/
186208#define GEOPOLY_SZ(N) (sizeof(GeoPoly) + sizeof(GeoCoord)*2*((N)-4))
186209
186210/* Macros to access coordinates of a GeoPoly.
186211** We have to use these macros, rather than just say p->a[i] in order
186212** to silence (incorrect) UBSAN warnings if the array index is too large.
186213*/
186214#define GeoX(P,I) (((GeoCoord*)(P)->a)[(I)*2])
186215#define GeoY(P,I) (((GeoCoord*)(P)->a)[(I)*2+1])
186216
186217
186218/*
186219** State of a parse of a GeoJSON input.
186220*/
186221typedef struct GeoParse GeoParse;
186222struct GeoParse {
186223 const unsigned char *z; /* Unparsed input */
186224 int nVertex; /* Number of vertexes in a[] */
186225 int nAlloc; /* Space allocated to a[] */
186226 int nErr; /* Number of errors encountered */
186227 GeoCoord *a; /* Array of vertexes. From sqlite3_malloc64() */
186228};
186229
186230/* Do a 4-byte byte swap */
186231static void geopolySwab32(unsigned char *a){
186232 unsigned char t = a[0];
186233 a[0] = a[3];
186234 a[3] = t;
186235 t = a[1];
186236 a[1] = a[2];
186237 a[2] = t;
186238}
186239
186240/* Skip whitespace. Return the next non-whitespace character. */
186241static char geopolySkipSpace(GeoParse *p){
186242 while( safe_isspace(p->z[0]) ) p->z++;
186243 return p->z[0];
186244}
186245
186246/* Parse out a number. Write the value into *pVal if pVal!=0.
186247** return non-zero on success and zero if the next token is not a number.
186248*/
186249static int geopolyParseNumber(GeoParse *p, GeoCoord *pVal){
186250 char c = geopolySkipSpace(p);
186251 const unsigned char *z = p->z;
186252 int j = 0;
186253 int seenDP = 0;
186254 int seenE = 0;
186255 if( c=='-' ){
186256 j = 1;
186257 c = z[j];
186258 }
186259 if( c=='0' && z[j+1]>='0' && z[j+1]<='9' ) return 0;
186260 for(;; j++){
186261 c = z[j];
186262 if( safe_isdigit(c) ) continue;
186263 if( c=='.' ){
186264 if( z[j-1]=='-' ) return 0;
186265 if( seenDP ) return 0;
186266 seenDP = 1;
186267 continue;
186268 }
186269 if( c=='e' || c=='E' ){
186270 if( z[j-1]<'0' ) return 0;
186271 if( seenE ) return -1;
186272 seenDP = seenE = 1;
186273 c = z[j+1];
186274 if( c=='+' || c=='-' ){
186275 j++;
186276 c = z[j+1];
186277 }
186278 if( c<'0' || c>'9' ) return 0;
186279 continue;
186280 }
186281 break;
186282 }
186283 if( z[j-1]<'0' ) return 0;
186284 if( pVal ){
186285#ifdef SQLITE_AMALGAMATION1
186286 /* The sqlite3AtoF() routine is much much faster than atof(), if it
186287 ** is available */
186288 double r;
186289 (void)sqlite3AtoF((const char*)p->z, &r, j, SQLITE_UTF81);
186290 *pVal = r;
186291#else
186292 *pVal = (GeoCoord)atof((const char*)p->z);
186293#endif
186294 }
186295 p->z += j;
186296 return 1;
186297}
186298
186299/*
186300** If the input is a well-formed JSON array of coordinates with at least
186301** four coordinates and where each coordinate is itself a two-value array,
186302** then convert the JSON into a GeoPoly object and return a pointer to
186303** that object.
186304**
186305** If any error occurs, return NULL.
186306*/
186307static GeoPoly *geopolyParseJson(const unsigned char *z, int *pRc){
186308 GeoParse s;
186309 int rc = SQLITE_OK0;
186310 memset(&s, 0, sizeof(s));
186311 s.z = z;
186312 if( geopolySkipSpace(&s)=='[' ){
186313 s.z++;
186314 while( geopolySkipSpace(&s)=='[' ){
186315 int ii = 0;
186316 char c;
186317 s.z++;
186318 if( s.nVertex>=s.nAlloc ){
186319 GeoCoord *aNew;
186320 s.nAlloc = s.nAlloc*2 + 16;
186321 aNew = sqlite3_realloc64(s.a, s.nAlloc*sizeof(GeoCoord)*2 );
186322 if( aNew==0 ){
186323 rc = SQLITE_NOMEM7;
186324 s.nErr++;
186325 break;
186326 }
186327 s.a = aNew;
186328 }
186329 while( geopolyParseNumber(&s, ii<=1 ? &s.a[s.nVertex*2+ii] : 0) ){
186330 ii++;
186331 if( ii==2 ) s.nVertex++;
186332 c = geopolySkipSpace(&s);
186333 s.z++;
186334 if( c==',' ) continue;
186335 if( c==']' && ii>=2 ) break;
186336 s.nErr++;
186337 rc = SQLITE_ERROR1;
186338 goto parse_json_err;
186339 }
186340 if( geopolySkipSpace(&s)==',' ){
186341 s.z++;
186342 continue;
186343 }
186344 break;
186345 }
186346 if( geopolySkipSpace(&s)==']'
186347 && s.nVertex>=4
186348 && s.a[0]==s.a[s.nVertex*2-2]
186349 && s.a[1]==s.a[s.nVertex*2-1]
186350 && (s.z++, geopolySkipSpace(&s)==0)
186351 ){
186352 GeoPoly *pOut;
186353 int x = 1;
186354 s.nVertex--; /* Remove the redundant vertex at the end */
186355 pOut = sqlite3_malloc64( GEOPOLY_SZ((sqlite3_int64)s.nVertex) );
186356 x = 1;
186357 if( pOut==0 ) goto parse_json_err;
186358 pOut->nVertex = s.nVertex;
186359 memcpy(pOut->a, s.a, s.nVertex*2*sizeof(GeoCoord));
186360 pOut->hdr[0] = *(unsigned char*)&x;
186361 pOut->hdr[1] = (s.nVertex>>16)&0xff;
186362 pOut->hdr[2] = (s.nVertex>>8)&0xff;
186363 pOut->hdr[3] = s.nVertex&0xff;
186364 sqlite3_free(s.a);
186365 if( pRc ) *pRc = SQLITE_OK0;
186366 return pOut;
186367 }else{
186368 s.nErr++;
186369 rc = SQLITE_ERROR1;
186370 }
186371 }
186372parse_json_err:
186373 if( pRc ) *pRc = rc;
186374 sqlite3_free(s.a);
186375 return 0;
186376}
186377
186378/*
186379** Given a function parameter, try to interpret it as a polygon, either
186380** in the binary format or JSON text. Compute a GeoPoly object and
186381** return a pointer to that object. Or if the input is not a well-formed
186382** polygon, put an error message in sqlite3_context and return NULL.
186383*/
186384static GeoPoly *geopolyFuncParam(
186385 sqlite3_context *pCtx, /* Context for error messages */
186386 sqlite3_value *pVal, /* The value to decode */
186387 int *pRc /* Write error here */
186388){
186389 GeoPoly *p = 0;
186390 int nByte;
186391 if( sqlite3_value_type(pVal)==SQLITE_BLOB4
186392 && (nByte = sqlite3_value_bytes(pVal))>=(4+6*sizeof(GeoCoord))
186393 ){
186394 const unsigned char *a = sqlite3_value_blob(pVal);
186395 int nVertex;
186396 nVertex = (a[1]<<16) + (a[2]<<8) + a[3];
186397 if( (a[0]==0 || a[0]==1)
186398 && (nVertex*2*sizeof(GeoCoord) + 4)==(unsigned int)nByte
186399 ){
186400 p = sqlite3_malloc64( sizeof(*p) + (nVertex-1)*2*sizeof(GeoCoord) );
186401 if( p==0 ){
186402 if( pRc ) *pRc = SQLITE_NOMEM7;
186403 if( pCtx ) sqlite3_result_error_nomem(pCtx);
186404 }else{
186405 int x = 1;
186406 p->nVertex = nVertex;
186407 memcpy(p->hdr, a, nByte);
186408 if( a[0] != *(unsigned char*)&x ){
186409 int ii;
186410 for(ii=0; ii<nVertex; ii++){
186411 geopolySwab32((unsigned char*)&GeoX(p,ii));
186412 geopolySwab32((unsigned char*)&GeoY(p,ii));
186413 }
186414 p->hdr[0] ^= 1;
186415 }
186416 }
186417 }
186418 if( pRc ) *pRc = SQLITE_OK0;
186419 return p;
186420 }else if( sqlite3_value_type(pVal)==SQLITE_TEXT3 ){
186421 const unsigned char *zJson = sqlite3_value_text(pVal);
186422 if( zJson==0 ){
186423 if( pRc ) *pRc = SQLITE_NOMEM7;
186424 return 0;
186425 }
186426 return geopolyParseJson(zJson, pRc);
186427 }else{
186428 if( pRc ) *pRc = SQLITE_ERROR1;
186429 return 0;
186430 }
186431}
186432
186433/*
186434** Implementation of the geopoly_blob(X) function.
186435**
186436** If the input is a well-formed Geopoly BLOB or JSON string
186437** then return the BLOB representation of the polygon. Otherwise
186438** return NULL.
186439*/
186440static void geopolyBlobFunc(
186441 sqlite3_context *context,
186442 int argc,
186443 sqlite3_value **argv
186444){
186445 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
186446 if( p ){
186447 sqlite3_result_blob(context, p->hdr,
186448 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186449 sqlite3_free(p);
186450 }
186451}
186452
186453/*
186454** SQL function: geopoly_json(X)
186455**
186456** Interpret X as a polygon and render it as a JSON array
186457** of coordinates. Or, if X is not a valid polygon, return NULL.
186458*/
186459static void geopolyJsonFunc(
186460 sqlite3_context *context,
186461 int argc,
186462 sqlite3_value **argv
186463){
186464 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
186465 if( p ){
186466 sqlite3 *db = sqlite3_context_db_handle(context);
186467 sqlite3_str *x = sqlite3_str_new(db);
186468 int i;
186469 sqlite3_str_append(x, "[", 1);
186470 for(i=0; i<p->nVertex; i++){
186471 sqlite3_str_appendf(x, "[%!g,%!g],", GeoX(p,i), GeoY(p,i));
186472 }
186473 sqlite3_str_appendf(x, "[%!g,%!g]]", GeoX(p,0), GeoY(p,0));
186474 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
186475 sqlite3_free(p);
186476 }
186477}
186478
186479/*
186480** SQL function: geopoly_svg(X, ....)
186481**
186482** Interpret X as a polygon and render it as a SVG <polyline>.
186483** Additional arguments are added as attributes to the <polyline>.
186484*/
186485static void geopolySvgFunc(
186486 sqlite3_context *context,
186487 int argc,
186488 sqlite3_value **argv
186489){
186490 GeoPoly *p;
186491 if( argc<1 ) return;
186492 p = geopolyFuncParam(context, argv[0], 0);
186493 if( p ){
186494 sqlite3 *db = sqlite3_context_db_handle(context);
186495 sqlite3_str *x = sqlite3_str_new(db);
186496 int i;
186497 char cSep = '\'';
186498 sqlite3_str_appendf(x, "<polyline points=");
186499 for(i=0; i<p->nVertex; i++){
186500 sqlite3_str_appendf(x, "%c%g,%g", cSep, GeoX(p,i), GeoY(p,i));
186501 cSep = ' ';
186502 }
186503 sqlite3_str_appendf(x, " %g,%g'", GeoX(p,0), GeoY(p,0));
186504 for(i=1; i<argc; i++){
186505 const char *z = (const char*)sqlite3_value_text(argv[i]);
186506 if( z && z[0] ){
186507 sqlite3_str_appendf(x, " %s", z);
186508 }
186509 }
186510 sqlite3_str_appendf(x, "></polyline>");
186511 sqlite3_result_text(context, sqlite3_str_finish(x), -1, sqlite3_free);
186512 sqlite3_free(p);
186513 }
186514}
186515
186516/*
186517** SQL Function: geopoly_xform(poly, A, B, C, D, E, F)
186518**
186519** Transform and/or translate a polygon as follows:
186520**
186521** x1 = A*x0 + B*y0 + E
186522** y1 = C*x0 + D*y0 + F
186523**
186524** For a translation:
186525**
186526** geopoly_xform(poly, 1, 0, 0, 1, x-offset, y-offset)
186527**
186528** Rotate by R around the point (0,0):
186529**
186530** geopoly_xform(poly, cos(R), sin(R), -sin(R), cos(R), 0, 0)
186531*/
186532static void geopolyXformFunc(
186533 sqlite3_context *context,
186534 int argc,
186535 sqlite3_value **argv
186536){
186537 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
186538 double A = sqlite3_value_double(argv[1]);
186539 double B = sqlite3_value_double(argv[2]);
186540 double C = sqlite3_value_double(argv[3]);
186541 double D = sqlite3_value_double(argv[4]);
186542 double E = sqlite3_value_double(argv[5]);
186543 double F = sqlite3_value_double(argv[6]);
186544 GeoCoord x1, y1, x0, y0;
186545 int ii;
186546 if( p ){
186547 for(ii=0; ii<p->nVertex; ii++){
186548 x0 = GeoX(p,ii);
186549 y0 = GeoY(p,ii);
186550 x1 = (GeoCoord)(A*x0 + B*y0 + E);
186551 y1 = (GeoCoord)(C*x0 + D*y0 + F);
186552 GeoX(p,ii) = x1;
186553 GeoY(p,ii) = y1;
186554 }
186555 sqlite3_result_blob(context, p->hdr,
186556 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186557 sqlite3_free(p);
186558 }
186559}
186560
186561/*
186562** Compute the area enclosed by the polygon.
186563**
186564** This routine can also be used to detect polygons that rotate in
186565** the wrong direction. Polygons are suppose to be counter-clockwise (CCW).
186566** This routine returns a negative value for clockwise (CW) polygons.
186567*/
186568static double geopolyArea(GeoPoly *p){
186569 double rArea = 0.0;
186570 int ii;
186571 for(ii=0; ii<p->nVertex-1; ii++){
186572 rArea += (GeoX(p,ii) - GeoX(p,ii+1)) /* (x0 - x1) */
186573 * (GeoY(p,ii) + GeoY(p,ii+1)) /* (y0 + y1) */
186574 * 0.5;
186575 }
186576 rArea += (GeoX(p,ii) - GeoX(p,0)) /* (xN - x0) */
186577 * (GeoY(p,ii) + GeoY(p,0)) /* (yN + y0) */
186578 * 0.5;
186579 return rArea;
186580}
186581
186582/*
186583** Implementation of the geopoly_area(X) function.
186584**
186585** If the input is a well-formed Geopoly BLOB then return the area
186586** enclosed by the polygon. If the polygon circulates clockwise instead
186587** of counterclockwise (as it should) then return the negative of the
186588** enclosed area. Otherwise return NULL.
186589*/
186590static void geopolyAreaFunc(
186591 sqlite3_context *context,
186592 int argc,
186593 sqlite3_value **argv
186594){
186595 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
186596 if( p ){
186597 sqlite3_result_double(context, geopolyArea(p));
186598 sqlite3_free(p);
186599 }
186600}
186601
186602/*
186603** Implementation of the geopoly_ccw(X) function.
186604**
186605** If the rotation of polygon X is clockwise (incorrect) instead of
186606** counter-clockwise (the correct winding order according to RFC7946)
186607** then reverse the order of the vertexes in polygon X.
186608**
186609** In other words, this routine returns a CCW polygon regardless of the
186610** winding order of its input.
186611**
186612** Use this routine to sanitize historical inputs that that sometimes
186613** contain polygons that wind in the wrong direction.
186614*/
186615static void geopolyCcwFunc(
186616 sqlite3_context *context,
186617 int argc,
186618 sqlite3_value **argv
186619){
186620 GeoPoly *p = geopolyFuncParam(context, argv[0], 0);
186621 if( p ){
186622 if( geopolyArea(p)<0.0 ){
186623 int ii, jj;
186624 for(ii=1, jj=p->nVertex-1; ii<jj; ii++, jj--){
186625 GeoCoord t = GeoX(p,ii);
186626 GeoX(p,ii) = GeoX(p,jj);
186627 GeoX(p,jj) = t;
186628 t = GeoY(p,ii);
186629 GeoY(p,ii) = GeoY(p,jj);
186630 GeoY(p,jj) = t;
186631 }
186632 }
186633 sqlite3_result_blob(context, p->hdr,
186634 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186635 sqlite3_free(p);
186636 }
186637}
186638
186639#define GEOPOLY_PI 3.1415926535897932385
186640
186641/* Fast approximation for sine(X) for X between -0.5*pi and 2*pi
186642*/
186643static double geopolySine(double r){
186644 assert( r>=-0.5*GEOPOLY_PI && r<=2.0*GEOPOLY_PI )((void) (0));
186645 if( r>=1.5*GEOPOLY_PI ){
186646 r -= 2.0*GEOPOLY_PI;
186647 }
186648 if( r>=0.5*GEOPOLY_PI ){
186649 return -geopolySine(r-GEOPOLY_PI);
186650 }else{
186651 double r2 = r*r;
186652 double r3 = r2*r;
186653 double r5 = r3*r2;
186654 return 0.9996949*r - 0.1656700*r3 + 0.0075134*r5;
186655 }
186656}
186657
186658/*
186659** Function: geopoly_regular(X,Y,R,N)
186660**
186661** Construct a simple, convex, regular polygon centered at X, Y
186662** with circumradius R and with N sides.
186663*/
186664static void geopolyRegularFunc(
186665 sqlite3_context *context,
186666 int argc,
186667 sqlite3_value **argv
186668){
186669 double x = sqlite3_value_double(argv[0]);
186670 double y = sqlite3_value_double(argv[1]);
186671 double r = sqlite3_value_double(argv[2]);
186672 int n = sqlite3_value_int(argv[3]);
186673 int i;
186674 GeoPoly *p;
186675
186676 if( n<3 || r<=0.0 ) return;
186677 if( n>1000 ) n = 1000;
186678 p = sqlite3_malloc64( sizeof(*p) + (n-1)*2*sizeof(GeoCoord) );
186679 if( p==0 ){
186680 sqlite3_result_error_nomem(context);
186681 return;
186682 }
186683 i = 1;
186684 p->hdr[0] = *(unsigned char*)&i;
186685 p->hdr[1] = 0;
186686 p->hdr[2] = (n>>8)&0xff;
186687 p->hdr[3] = n&0xff;
186688 for(i=0; i<n; i++){
186689 double rAngle = 2.0*GEOPOLY_PI*i/n;
186690 GeoX(p,i) = x - r*geopolySine(rAngle-0.5*GEOPOLY_PI);
186691 GeoY(p,i) = y + r*geopolySine(rAngle);
186692 }
186693 sqlite3_result_blob(context, p->hdr, 4+8*n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186694 sqlite3_free(p);
186695}
186696
186697/*
186698** If pPoly is a polygon, compute its bounding box. Then:
186699**
186700** (1) if aCoord!=0 store the bounding box in aCoord, returning NULL
186701** (2) otherwise, compute a GeoPoly for the bounding box and return the
186702** new GeoPoly
186703**
186704** If pPoly is NULL but aCoord is not NULL, then compute a new GeoPoly from
186705** the bounding box in aCoord and return a pointer to that GeoPoly.
186706*/
186707static GeoPoly *geopolyBBox(
186708 sqlite3_context *context, /* For recording the error */
186709 sqlite3_value *pPoly, /* The polygon */
186710 RtreeCoord *aCoord, /* Results here */
186711 int *pRc /* Error code here */
186712){
186713 GeoPoly *pOut = 0;
186714 GeoPoly *p;
186715 float mnX, mxX, mnY, mxY;
186716 if( pPoly==0 && aCoord!=0 ){
186717 p = 0;
186718 mnX = aCoord[0].f;
186719 mxX = aCoord[1].f;
186720 mnY = aCoord[2].f;
186721 mxY = aCoord[3].f;
186722 goto geopolyBboxFill;
186723 }else{
186724 p = geopolyFuncParam(context, pPoly, pRc);
186725 }
186726 if( p ){
186727 int ii;
186728 mnX = mxX = GeoX(p,0);
186729 mnY = mxY = GeoY(p,0);
186730 for(ii=1; ii<p->nVertex; ii++){
186731 double r = GeoX(p,ii);
186732 if( r<mnX ) mnX = (float)r;
186733 else if( r>mxX ) mxX = (float)r;
186734 r = GeoY(p,ii);
186735 if( r<mnY ) mnY = (float)r;
186736 else if( r>mxY ) mxY = (float)r;
186737 }
186738 if( pRc ) *pRc = SQLITE_OK0;
186739 if( aCoord==0 ){
186740 geopolyBboxFill:
186741 pOut = sqlite3_realloc64(p, GEOPOLY_SZ(4));
186742 if( pOut==0 ){
186743 sqlite3_free(p);
186744 if( context ) sqlite3_result_error_nomem(context);
186745 if( pRc ) *pRc = SQLITE_NOMEM7;
186746 return 0;
186747 }
186748 pOut->nVertex = 4;
186749 ii = 1;
186750 pOut->hdr[0] = *(unsigned char*)&ii;
186751 pOut->hdr[1] = 0;
186752 pOut->hdr[2] = 0;
186753 pOut->hdr[3] = 4;
186754 GeoX(pOut,0) = mnX;
186755 GeoY(pOut,0) = mnY;
186756 GeoX(pOut,1) = mxX;
186757 GeoY(pOut,1) = mnY;
186758 GeoX(pOut,2) = mxX;
186759 GeoY(pOut,2) = mxY;
186760 GeoX(pOut,3) = mnX;
186761 GeoY(pOut,3) = mxY;
186762 }else{
186763 sqlite3_free(p);
186764 aCoord[0].f = mnX;
186765 aCoord[1].f = mxX;
186766 aCoord[2].f = mnY;
186767 aCoord[3].f = mxY;
186768 }
186769 }
186770 return pOut;
186771}
186772
186773/*
186774** Implementation of the geopoly_bbox(X) SQL function.
186775*/
186776static void geopolyBBoxFunc(
186777 sqlite3_context *context,
186778 int argc,
186779 sqlite3_value **argv
186780){
186781 GeoPoly *p = geopolyBBox(context, argv[0], 0, 0);
186782 if( p ){
186783 sqlite3_result_blob(context, p->hdr,
186784 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186785 sqlite3_free(p);
186786 }
186787}
186788
186789/*
186790** State vector for the geopoly_group_bbox() aggregate function.
186791*/
186792typedef struct GeoBBox GeoBBox;
186793struct GeoBBox {
186794 int isInit;
186795 RtreeCoord a[4];
186796};
186797
186798
186799/*
186800** Implementation of the geopoly_group_bbox(X) aggregate SQL function.
186801*/
186802static void geopolyBBoxStep(
186803 sqlite3_context *context,
186804 int argc,
186805 sqlite3_value **argv
186806){
186807 RtreeCoord a[4];
186808 int rc = SQLITE_OK0;
186809 (void)geopolyBBox(context, argv[0], a, &rc);
186810 if( rc==SQLITE_OK0 ){
186811 GeoBBox *pBBox;
186812 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, sizeof(*pBBox));
186813 if( pBBox==0 ) return;
186814 if( pBBox->isInit==0 ){
186815 pBBox->isInit = 1;
186816 memcpy(pBBox->a, a, sizeof(RtreeCoord)*4);
186817 }else{
186818 if( a[0].f < pBBox->a[0].f ) pBBox->a[0] = a[0];
186819 if( a[1].f > pBBox->a[1].f ) pBBox->a[1] = a[1];
186820 if( a[2].f < pBBox->a[2].f ) pBBox->a[2] = a[2];
186821 if( a[3].f > pBBox->a[3].f ) pBBox->a[3] = a[3];
186822 }
186823 }
186824}
186825static void geopolyBBoxFinal(
186826 sqlite3_context *context
186827){
186828 GeoPoly *p;
186829 GeoBBox *pBBox;
186830 pBBox = (GeoBBox*)sqlite3_aggregate_context(context, 0);
186831 if( pBBox==0 ) return;
186832 p = geopolyBBox(context, 0, pBBox->a, 0);
186833 if( p ){
186834 sqlite3_result_blob(context, p->hdr,
186835 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
186836 sqlite3_free(p);
186837 }
186838}
186839
186840
186841/*
186842** Determine if point (x0,y0) is beneath line segment (x1,y1)->(x2,y2).
186843** Returns:
186844**
186845** +2 x0,y0 is on the line segement
186846**
186847** +1 x0,y0 is beneath line segment
186848**
186849** 0 x0,y0 is not on or beneath the line segment or the line segment
186850** is vertical and x0,y0 is not on the line segment
186851**
186852** The left-most coordinate min(x1,x2) is not considered to be part of
186853** the line segment for the purposes of this analysis.
186854*/
186855static int pointBeneathLine(
186856 double x0, double y0,
186857 double x1, double y1,
186858 double x2, double y2
186859){
186860 double y;
186861 if( x0==x1 && y0==y1 ) return 2;
186862 if( x1<x2 ){
186863 if( x0<=x1 || x0>x2 ) return 0;
186864 }else if( x1>x2 ){
186865 if( x0<=x2 || x0>x1 ) return 0;
186866 }else{
186867 /* Vertical line segment */
186868 if( x0!=x1 ) return 0;
186869 if( y0<y1 && y0<y2 ) return 0;
186870 if( y0>y1 && y0>y2 ) return 0;
186871 return 2;
186872 }
186873 y = y1 + (y2-y1)*(x0-x1)/(x2-x1);
186874 if( y0==y ) return 2;
186875 if( y0<y ) return 1;
186876 return 0;
186877}
186878
186879/*
186880** SQL function: geopoly_contains_point(P,X,Y)
186881**
186882** Return +2 if point X,Y is within polygon P.
186883** Return +1 if point X,Y is on the polygon boundary.
186884** Return 0 if point X,Y is outside the polygon
186885*/
186886static void geopolyContainsPointFunc(
186887 sqlite3_context *context,
186888 int argc,
186889 sqlite3_value **argv
186890){
186891 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
186892 double x0 = sqlite3_value_double(argv[1]);
186893 double y0 = sqlite3_value_double(argv[2]);
186894 int v = 0;
186895 int cnt = 0;
186896 int ii;
186897 if( p1==0 ) return;
186898 for(ii=0; ii<p1->nVertex-1; ii++){
186899 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
186900 GeoX(p1,ii+1),GeoY(p1,ii+1));
186901 if( v==2 ) break;
186902 cnt += v;
186903 }
186904 if( v!=2 ){
186905 v = pointBeneathLine(x0,y0,GeoX(p1,ii), GeoY(p1,ii),
186906 GeoX(p1,0), GeoY(p1,0));
186907 }
186908 if( v==2 ){
186909 sqlite3_result_int(context, 1);
186910 }else if( ((v+cnt)&1)==0 ){
186911 sqlite3_result_int(context, 0);
186912 }else{
186913 sqlite3_result_int(context, 2);
186914 }
186915 sqlite3_free(p1);
186916}
186917
186918/* Forward declaration */
186919static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2);
186920
186921/*
186922** SQL function: geopoly_within(P1,P2)
186923**
186924** Return +2 if P1 and P2 are the same polygon
186925** Return +1 if P2 is contained within P1
186926** Return 0 if any part of P2 is on the outside of P1
186927**
186928*/
186929static void geopolyWithinFunc(
186930 sqlite3_context *context,
186931 int argc,
186932 sqlite3_value **argv
186933){
186934 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
186935 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
186936 if( p1 && p2 ){
186937 int x = geopolyOverlap(p1, p2);
186938 if( x<0 ){
186939 sqlite3_result_error_nomem(context);
186940 }else{
186941 sqlite3_result_int(context, x==2 ? 1 : x==4 ? 2 : 0);
186942 }
186943 }
186944 sqlite3_free(p1);
186945 sqlite3_free(p2);
186946}
186947
186948/* Objects used by the overlap algorihm. */
186949typedef struct GeoEvent GeoEvent;
186950typedef struct GeoSegment GeoSegment;
186951typedef struct GeoOverlap GeoOverlap;
186952struct GeoEvent {
186953 double x; /* X coordinate at which event occurs */
186954 int eType; /* 0 for ADD, 1 for REMOVE */
186955 GeoSegment *pSeg; /* The segment to be added or removed */
186956 GeoEvent *pNext; /* Next event in the sorted list */
186957};
186958struct GeoSegment {
186959 double C, B; /* y = C*x + B */
186960 double y; /* Current y value */
186961 float y0; /* Initial y value */
186962 unsigned char side; /* 1 for p1, 2 for p2 */
186963 unsigned int idx; /* Which segment within the side */
186964 GeoSegment *pNext; /* Next segment in a list sorted by y */
186965};
186966struct GeoOverlap {
186967 GeoEvent *aEvent; /* Array of all events */
186968 GeoSegment *aSegment; /* Array of all segments */
186969 int nEvent; /* Number of events */
186970 int nSegment; /* Number of segments */
186971};
186972
186973/*
186974** Add a single segment and its associated events.
186975*/
186976static void geopolyAddOneSegment(
186977 GeoOverlap *p,
186978 GeoCoord x0,
186979 GeoCoord y0,
186980 GeoCoord x1,
186981 GeoCoord y1,
186982 unsigned char side,
186983 unsigned int idx
186984){
186985 GeoSegment *pSeg;
186986 GeoEvent *pEvent;
186987 if( x0==x1 ) return; /* Ignore vertical segments */
186988 if( x0>x1 ){
186989 GeoCoord t = x0;
186990 x0 = x1;
186991 x1 = t;
186992 t = y0;
186993 y0 = y1;
186994 y1 = t;
186995 }
186996 pSeg = p->aSegment + p->nSegment;
186997 p->nSegment++;
186998 pSeg->C = (y1-y0)/(x1-x0);
186999 pSeg->B = y1 - x1*pSeg->C;
187000 pSeg->y0 = y0;
187001 pSeg->side = side;
187002 pSeg->idx = idx;
187003 pEvent = p->aEvent + p->nEvent;
187004 p->nEvent++;
187005 pEvent->x = x0;
187006 pEvent->eType = 0;
187007 pEvent->pSeg = pSeg;
187008 pEvent = p->aEvent + p->nEvent;
187009 p->nEvent++;
187010 pEvent->x = x1;
187011 pEvent->eType = 1;
187012 pEvent->pSeg = pSeg;
187013}
187014
187015
187016
187017/*
187018** Insert all segments and events for polygon pPoly.
187019*/
187020static void geopolyAddSegments(
187021 GeoOverlap *p, /* Add segments to this Overlap object */
187022 GeoPoly *pPoly, /* Take all segments from this polygon */
187023 unsigned char side /* The side of pPoly */
187024){
187025 unsigned int i;
187026 GeoCoord *x;
187027 for(i=0; i<(unsigned)pPoly->nVertex-1; i++){
187028 x = &GeoX(pPoly,i);
187029 geopolyAddOneSegment(p, x[0], x[1], x[2], x[3], side, i);
187030 }
187031 x = &GeoX(pPoly,i);
187032 geopolyAddOneSegment(p, x[0], x[1], pPoly->a[0], pPoly->a[1], side, i);
187033}
187034
187035/*
187036** Merge two lists of sorted events by X coordinate
187037*/
187038static GeoEvent *geopolyEventMerge(GeoEvent *pLeft, GeoEvent *pRight){
187039 GeoEvent head, *pLast;
187040 head.pNext = 0;
187041 pLast = &head;
187042 while( pRight && pLeft ){
187043 if( pRight->x <= pLeft->x ){
187044 pLast->pNext = pRight;
187045 pLast = pRight;
187046 pRight = pRight->pNext;
187047 }else{
187048 pLast->pNext = pLeft;
187049 pLast = pLeft;
187050 pLeft = pLeft->pNext;
187051 }
187052 }
187053 pLast->pNext = pRight ? pRight : pLeft;
187054 return head.pNext;
187055}
187056
187057/*
187058** Sort an array of nEvent event objects into a list.
187059*/
187060static GeoEvent *geopolySortEventsByX(GeoEvent *aEvent, int nEvent){
187061 int mx = 0;
187062 int i, j;
187063 GeoEvent *p;
187064 GeoEvent *a[50];
187065 for(i=0; i<nEvent; i++){
187066 p = &aEvent[i];
187067 p->pNext = 0;
187068 for(j=0; j<mx && a[j]; j++){
187069 p = geopolyEventMerge(a[j], p);
187070 a[j] = 0;
187071 }
187072 a[j] = p;
187073 if( j>=mx ) mx = j+1;
187074 }
187075 p = 0;
187076 for(i=0; i<mx; i++){
187077 p = geopolyEventMerge(a[i], p);
187078 }
187079 return p;
187080}
187081
187082/*
187083** Merge two lists of sorted segments by Y, and then by C.
187084*/
187085static GeoSegment *geopolySegmentMerge(GeoSegment *pLeft, GeoSegment *pRight){
187086 GeoSegment head, *pLast;
187087 head.pNext = 0;
187088 pLast = &head;
187089 while( pRight && pLeft ){
187090 double r = pRight->y - pLeft->y;
187091 if( r==0.0 ) r = pRight->C - pLeft->C;
187092 if( r<0.0 ){
187093 pLast->pNext = pRight;
187094 pLast = pRight;
187095 pRight = pRight->pNext;
187096 }else{
187097 pLast->pNext = pLeft;
187098 pLast = pLeft;
187099 pLeft = pLeft->pNext;
187100 }
187101 }
187102 pLast->pNext = pRight ? pRight : pLeft;
187103 return head.pNext;
187104}
187105
187106/*
187107** Sort a list of GeoSegments in order of increasing Y and in the event of
187108** a tie, increasing C (slope).
187109*/
187110static GeoSegment *geopolySortSegmentsByYAndC(GeoSegment *pList){
187111 int mx = 0;
187112 int i;
187113 GeoSegment *p;
187114 GeoSegment *a[50];
187115 while( pList ){
187116 p = pList;
187117 pList = pList->pNext;
187118 p->pNext = 0;
187119 for(i=0; i<mx && a[i]; i++){
187120 p = geopolySegmentMerge(a[i], p);
187121 a[i] = 0;
187122 }
187123 a[i] = p;
187124 if( i>=mx ) mx = i+1;
187125 }
187126 p = 0;
187127 for(i=0; i<mx; i++){
187128 p = geopolySegmentMerge(a[i], p);
187129 }
187130 return p;
187131}
187132
187133/*
187134** Determine the overlap between two polygons
187135*/
187136static int geopolyOverlap(GeoPoly *p1, GeoPoly *p2){
187137 sqlite3_int64 nVertex = p1->nVertex + p2->nVertex + 2;
187138 GeoOverlap *p;
187139 sqlite3_int64 nByte;
187140 GeoEvent *pThisEvent;
187141 double rX;
187142 int rc = 0;
187143 int needSort = 0;
187144 GeoSegment *pActive = 0;
187145 GeoSegment *pSeg;
187146 unsigned char aOverlap[4];
187147
187148 nByte = sizeof(GeoEvent)*nVertex*2
187149 + sizeof(GeoSegment)*nVertex
187150 + sizeof(GeoOverlap);
187151 p = sqlite3_malloc64( nByte );
187152 if( p==0 ) return -1;
187153 p->aEvent = (GeoEvent*)&p[1];
187154 p->aSegment = (GeoSegment*)&p->aEvent[nVertex*2];
187155 p->nEvent = p->nSegment = 0;
187156 geopolyAddSegments(p, p1, 1);
187157 geopolyAddSegments(p, p2, 2);
187158 pThisEvent = geopolySortEventsByX(p->aEvent, p->nEvent);
187159 rX = pThisEvent->x==0.0 ? -1.0 : 0.0;
187160 memset(aOverlap, 0, sizeof(aOverlap));
187161 while( pThisEvent ){
187162 if( pThisEvent->x!=rX ){
187163 GeoSegment *pPrev = 0;
187164 int iMask = 0;
187165 GEODEBUG(("Distinct X: %g\n", pThisEvent->x));
187166 rX = pThisEvent->x;
187167 if( needSort ){
187168 GEODEBUG(("SORT\n"));
187169 pActive = geopolySortSegmentsByYAndC(pActive);
187170 needSort = 0;
187171 }
187172 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
187173 if( pPrev ){
187174 if( pPrev->y!=pSeg->y ){
187175 GEODEBUG(("MASK: %d\n", iMask));
187176 aOverlap[iMask] = 1;
187177 }
187178 }
187179 iMask ^= pSeg->side;
187180 pPrev = pSeg;
187181 }
187182 pPrev = 0;
187183 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
187184 double y = pSeg->C*rX + pSeg->B;
187185 GEODEBUG(("Segment %d.%d %g->%g\n", pSeg->side, pSeg->idx, pSeg->y, y));
187186 pSeg->y = y;
187187 if( pPrev ){
187188 if( pPrev->y>pSeg->y && pPrev->side!=pSeg->side ){
187189 rc = 1;
187190 GEODEBUG(("Crossing: %d.%d and %d.%d\n",
187191 pPrev->side, pPrev->idx,
187192 pSeg->side, pSeg->idx));
187193 goto geopolyOverlapDone;
187194 }else if( pPrev->y!=pSeg->y ){
187195 GEODEBUG(("MASK: %d\n", iMask));
187196 aOverlap[iMask] = 1;
187197 }
187198 }
187199 iMask ^= pSeg->side;
187200 pPrev = pSeg;
187201 }
187202 }
187203 GEODEBUG(("%s %d.%d C=%g B=%g\n",
187204 pThisEvent->eType ? "RM " : "ADD",
187205 pThisEvent->pSeg->side, pThisEvent->pSeg->idx,
187206 pThisEvent->pSeg->C,
187207 pThisEvent->pSeg->B));
187208 if( pThisEvent->eType==0 ){
187209 /* Add a segment */
187210 pSeg = pThisEvent->pSeg;
187211 pSeg->y = pSeg->y0;
187212 pSeg->pNext = pActive;
187213 pActive = pSeg;
187214 needSort = 1;
187215 }else{
187216 /* Remove a segment */
187217 if( pActive==pThisEvent->pSeg ){
187218 pActive = pActive->pNext;
187219 }else{
187220 for(pSeg=pActive; pSeg; pSeg=pSeg->pNext){
187221 if( pSeg->pNext==pThisEvent->pSeg ){
187222 pSeg->pNext = pSeg->pNext->pNext;
187223 break;
187224 }
187225 }
187226 }
187227 }
187228 pThisEvent = pThisEvent->pNext;
187229 }
187230 if( aOverlap[3]==0 ){
187231 rc = 0;
187232 }else if( aOverlap[1]!=0 && aOverlap[2]==0 ){
187233 rc = 3;
187234 }else if( aOverlap[1]==0 && aOverlap[2]!=0 ){
187235 rc = 2;
187236 }else if( aOverlap[1]==0 && aOverlap[2]==0 ){
187237 rc = 4;
187238 }else{
187239 rc = 1;
187240 }
187241
187242geopolyOverlapDone:
187243 sqlite3_free(p);
187244 return rc;
187245}
187246
187247/*
187248** SQL function: geopoly_overlap(P1,P2)
187249**
187250** Determine whether or not P1 and P2 overlap. Return value:
187251**
187252** 0 The two polygons are disjoint
187253** 1 They overlap
187254** 2 P1 is completely contained within P2
187255** 3 P2 is completely contained within P1
187256** 4 P1 and P2 are the same polygon
187257** NULL Either P1 or P2 or both are not valid polygons
187258*/
187259static void geopolyOverlapFunc(
187260 sqlite3_context *context,
187261 int argc,
187262 sqlite3_value **argv
187263){
187264 GeoPoly *p1 = geopolyFuncParam(context, argv[0], 0);
187265 GeoPoly *p2 = geopolyFuncParam(context, argv[1], 0);
187266 if( p1 && p2 ){
187267 int x = geopolyOverlap(p1, p2);
187268 if( x<0 ){
187269 sqlite3_result_error_nomem(context);
187270 }else{
187271 sqlite3_result_int(context, x);
187272 }
187273 }
187274 sqlite3_free(p1);
187275 sqlite3_free(p2);
187276}
187277
187278/*
187279** Enable or disable debugging output
187280*/
187281static void geopolyDebugFunc(
187282 sqlite3_context *context,
187283 int argc,
187284 sqlite3_value **argv
187285){
187286#ifdef GEOPOLY_ENABLE_DEBUG
187287 geo_debug = sqlite3_value_int(argv[0]);
187288#endif
187289}
187290
187291/*
187292** This function is the implementation of both the xConnect and xCreate
187293** methods of the geopoly virtual table.
187294**
187295** argv[0] -> module name
187296** argv[1] -> database name
187297** argv[2] -> table name
187298** argv[...] -> column names...
187299*/
187300static int geopolyInit(
187301 sqlite3 *db, /* Database connection */
187302 void *pAux, /* One of the RTREE_COORD_* constants */
187303 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
187304 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
187305 char **pzErr, /* OUT: Error message, if any */
187306 int isCreate /* True for xCreate, false for xConnect */
187307){
187308 int rc = SQLITE_OK0;
187309 Rtree *pRtree;
187310 sqlite3_int64 nDb; /* Length of string argv[1] */
187311 sqlite3_int64 nName; /* Length of string argv[2] */
187312 sqlite3_str *pSql;
187313 char *zSql;
187314 int ii;
187315
187316 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT1, 1);
187317
187318 /* Allocate the sqlite3_vtab structure */
187319 nDb = strlen(argv[1]);
187320 nName = strlen(argv[2]);
187321 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName+2);
187322 if( !pRtree ){
187323 return SQLITE_NOMEM7;
187324 }
187325 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
187326 pRtree->nBusy = 1;
187327 pRtree->base.pModule = &rtreeModule;
187328 pRtree->zDb = (char *)&pRtree[1];
187329 pRtree->zName = &pRtree->zDb[nDb+1];
187330 pRtree->eCoordType = RTREE_COORD_REAL32;
187331 pRtree->nDim = 2;
187332 pRtree->nDim2 = 4;
187333 memcpy(pRtree->zDb, argv[1], nDb);
187334 memcpy(pRtree->zName, argv[2], nName);
187335
187336
187337 /* Create/Connect to the underlying relational database schema. If
187338 ** that is successful, call sqlite3_declare_vtab() to configure
187339 ** the r-tree table schema.
187340 */
187341 pSql = sqlite3_str_new(db);
187342 sqlite3_str_appendf(pSql, "CREATE TABLE x(_shape");
187343 pRtree->nAux = 1; /* Add one for _shape */
187344 pRtree->nAuxNotNull = 1; /* The _shape column is always not-null */
187345 for(ii=3; ii<argc; ii++){
187346 pRtree->nAux++;
187347 sqlite3_str_appendf(pSql, ",%s", argv[ii]);
187348 }
187349 sqlite3_str_appendf(pSql, ");");
187350 zSql = sqlite3_str_finish(pSql);
187351 if( !zSql ){
187352 rc = SQLITE_NOMEM7;
187353 }else if( SQLITE_OK0!=(rc = sqlite3_declare_vtab(db, zSql)) ){
187354 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
187355 }
187356 sqlite3_free(zSql);
187357 if( rc ) goto geopolyInit_fail;
187358 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
187359
187360 /* Figure out the node size to use. */
187361 rc = getNodeSize(db, pRtree, isCreate, pzErr);
187362 if( rc ) goto geopolyInit_fail;
187363 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
187364 if( rc ){
187365 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
187366 goto geopolyInit_fail;
187367 }
187368
187369 *ppVtab = (sqlite3_vtab *)pRtree;
187370 return SQLITE_OK0;
187371
187372geopolyInit_fail:
187373 if( rc==SQLITE_OK0 ) rc = SQLITE_ERROR1;
187374 assert( *ppVtab==0 )((void) (0));
187375 assert( pRtree->nBusy==1 )((void) (0));
187376 rtreeRelease(pRtree);
187377 return rc;
187378}
187379
187380
187381/*
187382** GEOPOLY virtual table module xCreate method.
187383*/
187384static int geopolyCreate(
187385 sqlite3 *db,
187386 void *pAux,
187387 int argc, const char *const*argv,
187388 sqlite3_vtab **ppVtab,
187389 char **pzErr
187390){
187391 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
187392}
187393
187394/*
187395** GEOPOLY virtual table module xConnect method.
187396*/
187397static int geopolyConnect(
187398 sqlite3 *db,
187399 void *pAux,
187400 int argc, const char *const*argv,
187401 sqlite3_vtab **ppVtab,
187402 char **pzErr
187403){
187404 return geopolyInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
187405}
187406
187407
187408/*
187409** GEOPOLY virtual table module xFilter method.
187410**
187411** Query plans:
187412**
187413** 1 rowid lookup
187414** 2 search for objects overlapping the same bounding box
187415** that contains polygon argv[0]
187416** 3 search for objects overlapping the same bounding box
187417** that contains polygon argv[0]
187418** 4 full table scan
187419*/
187420static int geopolyFilter(
187421 sqlite3_vtab_cursor *pVtabCursor, /* The cursor to initialize */
187422 int idxNum, /* Query plan */
187423 const char *idxStr, /* Not Used */
187424 int argc, sqlite3_value **argv /* Parameters to the query plan */
187425){
187426 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
187427 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
187428 RtreeNode *pRoot = 0;
187429 int rc = SQLITE_OK0;
187430 int iCell = 0;
187431 sqlite3_stmt *pStmt;
187432
187433 rtreeReference(pRtree);
187434
187435 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
187436 freeCursorConstraints(pCsr);
187437 sqlite3_free(pCsr->aPoint);
187438 pStmt = pCsr->pReadAux;
187439 memset(pCsr, 0, sizeof(RtreeCursor));
187440 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
187441 pCsr->pReadAux = pStmt;
187442
187443 pCsr->iStrategy = idxNum;
187444 if( idxNum==1 ){
187445 /* Special case - lookup by rowid. */
187446 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
187447 RtreeSearchPoint *p; /* Search point for the leaf */
187448 i64 iRowid = sqlite3_value_int64(argv[0]);
187449 i64 iNode = 0;
187450 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
187451 if( rc==SQLITE_OK0 && pLeaf!=0 ){
187452 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
187453 assert( p!=0 )((void) (0)); /* Always returns pCsr->sPoint */
187454 pCsr->aNode[0] = pLeaf;
187455 p->id = iNode;
187456 p->eWithin = PARTLY_WITHIN1;
187457 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
187458 p->iCell = (u8)iCell;
187459 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
187460 }else{
187461 pCsr->atEOF = 1;
187462 }
187463 }else{
187464 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
187465 ** with the configured constraints.
187466 */
187467 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
187468 if( rc==SQLITE_OK0 && idxNum<=3 ){
187469 RtreeCoord bbox[4];
187470 RtreeConstraint *p;
187471 assert( argc==1 )((void) (0));
187472 geopolyBBox(0, argv[0], bbox, &rc);
187473 if( rc ){
187474 goto geopoly_filter_end;
187475 }
187476 pCsr->aConstraint = p = sqlite3_malloc(sizeof(RtreeConstraint)*4);
187477 pCsr->nConstraint = 4;
187478 if( p==0 ){
187479 rc = SQLITE_NOMEM7;
187480 }else{
187481 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*4);
187482 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
187483 if( idxNum==2 ){
187484 /* Overlap query */
187485 p->op = 'B';
187486 p->iCoord = 0;
187487 p->u.rValue = bbox[1].f;
187488 p++;
187489 p->op = 'D';
187490 p->iCoord = 1;
187491 p->u.rValue = bbox[0].f;
187492 p++;
187493 p->op = 'B';
187494 p->iCoord = 2;
187495 p->u.rValue = bbox[3].f;
187496 p++;
187497 p->op = 'D';
187498 p->iCoord = 3;
187499 p->u.rValue = bbox[2].f;
187500 }else{
187501 /* Within query */
187502 p->op = 'D';
187503 p->iCoord = 0;
187504 p->u.rValue = bbox[0].f;
187505 p++;
187506 p->op = 'B';
187507 p->iCoord = 1;
187508 p->u.rValue = bbox[1].f;
187509 p++;
187510 p->op = 'D';
187511 p->iCoord = 2;
187512 p->u.rValue = bbox[2].f;
187513 p++;
187514 p->op = 'B';
187515 p->iCoord = 3;
187516 p->u.rValue = bbox[3].f;
187517 }
187518 }
187519 }
187520 if( rc==SQLITE_OK0 ){
187521 RtreeSearchPoint *pNew;
187522 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
187523 if( pNew==0 ){
187524 rc = SQLITE_NOMEM7;
187525 goto geopoly_filter_end;
187526 }
187527 pNew->id = 1;
187528 pNew->iCell = 0;
187529 pNew->eWithin = PARTLY_WITHIN1;
187530 assert( pCsr->bPoint==1 )((void) (0));
187531 pCsr->aNode[0] = pRoot;
187532 pRoot = 0;
187533 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
187534 rc = rtreeStepToLeaf(pCsr);
187535 }
187536 }
187537
187538geopoly_filter_end:
187539 nodeRelease(pRtree, pRoot);
187540 rtreeRelease(pRtree);
187541 return rc;
187542}
187543
187544/*
187545** Rtree virtual table module xBestIndex method. There are three
187546** table scan strategies to choose from (in order from most to
187547** least desirable):
187548**
187549** idxNum idxStr Strategy
187550** ------------------------------------------------
187551** 1 "rowid" Direct lookup by rowid.
187552** 2 "rtree" R-tree overlap query using geopoly_overlap()
187553** 3 "rtree" R-tree within query using geopoly_within()
187554** 4 "fullscan" full-table scan.
187555** ------------------------------------------------
187556*/
187557static int geopolyBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
187558 int ii;
187559 int iRowidTerm = -1;
187560 int iFuncTerm = -1;
187561 int idxNum = 0;
187562
187563 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
187564 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
187565 if( !p->usable ) continue;
187566 if( p->iColumn<0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
187567 iRowidTerm = ii;
187568 break;
187569 }
187570 if( p->iColumn==0 && p->op>=SQLITE_INDEX_CONSTRAINT_FUNCTION150 ){
187571 /* p->op==SQLITE_INDEX_CONSTRAINT_FUNCTION for geopoly_overlap()
187572 ** p->op==(SQLITE_INDEX_CONTRAINT_FUNCTION+1) for geopoly_within().
187573 ** See geopolyFindFunction() */
187574 iFuncTerm = ii;
187575 idxNum = p->op - SQLITE_INDEX_CONSTRAINT_FUNCTION150 + 2;
187576 }
187577 }
187578
187579 if( iRowidTerm>=0 ){
187580 pIdxInfo->idxNum = 1;
187581 pIdxInfo->idxStr = "rowid";
187582 pIdxInfo->aConstraintUsage[iRowidTerm].argvIndex = 1;
187583 pIdxInfo->aConstraintUsage[iRowidTerm].omit = 1;
187584 pIdxInfo->estimatedCost = 30.0;
187585 pIdxInfo->estimatedRows = 1;
187586 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE1;
187587 return SQLITE_OK0;
187588 }
187589 if( iFuncTerm>=0 ){
187590 pIdxInfo->idxNum = idxNum;
187591 pIdxInfo->idxStr = "rtree";
187592 pIdxInfo->aConstraintUsage[iFuncTerm].argvIndex = 1;
187593 pIdxInfo->aConstraintUsage[iFuncTerm].omit = 0;
187594 pIdxInfo->estimatedCost = 300.0;
187595 pIdxInfo->estimatedRows = 10;
187596 return SQLITE_OK0;
187597 }
187598 pIdxInfo->idxNum = 4;
187599 pIdxInfo->idxStr = "fullscan";
187600 pIdxInfo->estimatedCost = 3000000.0;
187601 pIdxInfo->estimatedRows = 100000;
187602 return SQLITE_OK0;
187603}
187604
187605
187606/*
187607** GEOPOLY virtual table module xColumn method.
187608*/
187609static int geopolyColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
187610 Rtree *pRtree = (Rtree *)cur->pVtab;
187611 RtreeCursor *pCsr = (RtreeCursor *)cur;
187612 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
187613 int rc = SQLITE_OK0;
187614 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
187615
187616 if( rc ) return rc;
187617 if( p==0 ) return SQLITE_OK0;
187618 if( i==0 && sqlite3_vtab_nochange(ctx) ) return SQLITE_OK0;
187619 if( i<=pRtree->nAux ){
187620 if( !pCsr->bAuxValid ){
187621 if( pCsr->pReadAux==0 ){
187622 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
187623 &pCsr->pReadAux, 0);
187624 if( rc ) return rc;
187625 }
187626 sqlite3_bind_int64(pCsr->pReadAux, 1,
187627 nodeGetRowid(pRtree, pNode, p->iCell));
187628 rc = sqlite3_step(pCsr->pReadAux);
187629 if( rc==SQLITE_ROW100 ){
187630 pCsr->bAuxValid = 1;
187631 }else{
187632 sqlite3_reset(pCsr->pReadAux);
187633 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
187634 return rc;
187635 }
187636 }
187637 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pReadAux, i+2));
187638 }
187639 return SQLITE_OK0;
187640}
187641
187642
187643/*
187644** The xUpdate method for GEOPOLY module virtual tables.
187645**
187646** For DELETE:
187647**
187648** argv[0] = the rowid to be deleted
187649**
187650** For INSERT:
187651**
187652** argv[0] = SQL NULL
187653** argv[1] = rowid to insert, or an SQL NULL to select automatically
187654** argv[2] = _shape column
187655** argv[3] = first application-defined column....
187656**
187657** For UPDATE:
187658**
187659** argv[0] = rowid to modify. Never NULL
187660** argv[1] = rowid after the change. Never NULL
187661** argv[2] = new value for _shape
187662** argv[3] = new value for first application-defined column....
187663*/
187664static int geopolyUpdate(
187665 sqlite3_vtab *pVtab,
187666 int nData,
187667 sqlite3_value **aData,
187668 sqlite_int64 *pRowid
187669){
187670 Rtree *pRtree = (Rtree *)pVtab;
187671 int rc = SQLITE_OK0;
187672 RtreeCell cell; /* New cell to insert if nData>1 */
187673 i64 oldRowid; /* The old rowid */
187674 int oldRowidValid; /* True if oldRowid is valid */
187675 i64 newRowid; /* The new rowid */
187676 int newRowidValid; /* True if newRowid is valid */
187677 int coordChange = 0; /* Change in coordinates */
187678
187679 if( pRtree->nNodeRef ){
187680 /* Unable to write to the btree while another cursor is reading from it,
187681 ** since the write might do a rebalance which would disrupt the read
187682 ** cursor. */
187683 return SQLITE_LOCKED_VTAB(6 | (2<<8));
187684 }
187685 rtreeReference(pRtree);
187686 assert(nData>=1)((void) (0));
187687
187688 oldRowidValid = sqlite3_value_type(aData[0])!=SQLITE_NULL5;;
187689 oldRowid = oldRowidValid ? sqlite3_value_int64(aData[0]) : 0;
187690 newRowidValid = nData>1 && sqlite3_value_type(aData[1])!=SQLITE_NULL5;
187691 newRowid = newRowidValid ? sqlite3_value_int64(aData[1]) : 0;
187692 cell.iRowid = newRowid;
187693
187694 if( nData>1 /* not a DELETE */
187695 && (!oldRowidValid /* INSERT */
187696 || !sqlite3_value_nochange(aData[2]) /* UPDATE _shape */
187697 || oldRowid!=newRowid) /* Rowid change */
187698 ){
187699 geopolyBBox(0, aData[2], cell.aCoord, &rc);
187700 if( rc ){
187701 if( rc==SQLITE_ERROR1 ){
187702 pVtab->zErrMsg =
187703 sqlite3_mprintf("_shape does not contain a valid polygon");
187704 }
187705 goto geopoly_update_end;
187706 }
187707 coordChange = 1;
187708
187709 /* If a rowid value was supplied, check if it is already present in
187710 ** the table. If so, the constraint has failed. */
187711 if( newRowidValid && (!oldRowidValid || oldRowid!=newRowid) ){
187712 int steprc;
187713 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
187714 steprc = sqlite3_step(pRtree->pReadRowid);
187715 rc = sqlite3_reset(pRtree->pReadRowid);
187716 if( SQLITE_ROW100==steprc ){
187717 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE5 ){
187718 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
187719 }else{
187720 rc = rtreeConstraintError(pRtree, 0);
187721 }
187722 }
187723 }
187724 }
187725
187726 /* If aData[0] is not an SQL NULL value, it is the rowid of a
187727 ** record to delete from the r-tree table. The following block does
187728 ** just that.
187729 */
187730 if( rc==SQLITE_OK0 && (nData==1 || (coordChange && oldRowidValid)) ){
187731 rc = rtreeDeleteRowid(pRtree, oldRowid);
187732 }
187733
187734 /* If the aData[] array contains more than one element, elements
187735 ** (aData[2]..aData[argc-1]) contain a new record to insert into
187736 ** the r-tree structure.
187737 */
187738 if( rc==SQLITE_OK0 && nData>1 && coordChange ){
187739 /* Insert the new record into the r-tree */
187740 RtreeNode *pLeaf = 0;
187741 if( !newRowidValid ){
187742 rc = rtreeNewRowid(pRtree, &cell.iRowid);
187743 }
187744 *pRowid = cell.iRowid;
187745 if( rc==SQLITE_OK0 ){
187746 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
187747 }
187748 if( rc==SQLITE_OK0 ){
187749 int rc2;
187750 pRtree->iReinsertHeight = -1;
187751 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
187752 rc2 = nodeRelease(pRtree, pLeaf);
187753 if( rc==SQLITE_OK0 ){
187754 rc = rc2;
187755 }
187756 }
187757 }
187758
187759 /* Change the data */
187760 if( rc==SQLITE_OK0 && nData>1 ){
187761 sqlite3_stmt *pUp = pRtree->pWriteAux;
187762 int jj;
187763 int nChange = 0;
187764 sqlite3_bind_int64(pUp, 1, cell.iRowid);
187765 assert( pRtree->nAux>=1 )((void) (0));
187766 if( sqlite3_value_nochange(aData[2]) ){
187767 sqlite3_bind_null(pUp, 2);
187768 }else{
187769 GeoPoly *p = 0;
187770 if( sqlite3_value_type(aData[2])==SQLITE_TEXT3
187771 && (p = geopolyFuncParam(0, aData[2], &rc))!=0
187772 && rc==SQLITE_OK0
187773 ){
187774 sqlite3_bind_blob(pUp, 2, p->hdr, 4+8*p->nVertex, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
187775 }else{
187776 sqlite3_bind_value(pUp, 2, aData[2]);
187777 }
187778 sqlite3_free(p);
187779 nChange = 1;
187780 }
187781 for(jj=1; jj<pRtree->nAux; jj++){
187782 nChange++;
187783 sqlite3_bind_value(pUp, jj+2, aData[jj+2]);
187784 }
187785 if( nChange ){
187786 sqlite3_step(pUp);
187787 rc = sqlite3_reset(pUp);
187788 }
187789 }
187790
187791geopoly_update_end:
187792 rtreeRelease(pRtree);
187793 return rc;
187794}
187795
187796/*
187797** Report that geopoly_overlap() is an overloaded function suitable
187798** for use in xBestIndex.
187799*/
187800static int geopolyFindFunction(
187801 sqlite3_vtab *pVtab,
187802 int nArg,
187803 const char *zName,
187804 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
187805 void **ppArg
187806){
187807 if( sqlite3_stricmp(zName, "geopoly_overlap")==0 ){
187808 *pxFunc = geopolyOverlapFunc;
187809 *ppArg = 0;
187810 return SQLITE_INDEX_CONSTRAINT_FUNCTION150;
187811 }
187812 if( sqlite3_stricmp(zName, "geopoly_within")==0 ){
187813 *pxFunc = geopolyWithinFunc;
187814 *ppArg = 0;
187815 return SQLITE_INDEX_CONSTRAINT_FUNCTION150+1;
187816 }
187817 return 0;
187818}
187819
187820
187821static sqlite3_module geopolyModule = {
187822 3, /* iVersion */
187823 geopolyCreate, /* xCreate - create a table */
187824 geopolyConnect, /* xConnect - connect to an existing table */
187825 geopolyBestIndex, /* xBestIndex - Determine search strategy */
187826 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
187827 rtreeDestroy, /* xDestroy - Drop a table */
187828 rtreeOpen, /* xOpen - open a cursor */
187829 rtreeClose, /* xClose - close a cursor */
187830 geopolyFilter, /* xFilter - configure scan constraints */
187831 rtreeNext, /* xNext - advance a cursor */
187832 rtreeEof, /* xEof */
187833 geopolyColumn, /* xColumn - read data */
187834 rtreeRowid, /* xRowid - read data */
187835 geopolyUpdate, /* xUpdate - write data */
187836 rtreeBeginTransaction, /* xBegin - begin transaction */
187837 rtreeEndTransaction, /* xSync - sync transaction */
187838 rtreeEndTransaction, /* xCommit - commit transaction */
187839 rtreeEndTransaction, /* xRollback - rollback transaction */
187840 geopolyFindFunction, /* xFindFunction - function overloading */
187841 rtreeRename, /* xRename - rename the table */
187842 rtreeSavepoint, /* xSavepoint */
187843 0, /* xRelease */
187844 0, /* xRollbackTo */
187845 rtreeShadowName /* xShadowName */
187846};
187847
187848static int sqlite3_geopoly_init(sqlite3 *db){
187849 int rc = SQLITE_OK0;
187850 static const struct {
187851 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
187852 signed char nArg;
187853 unsigned char bPure;
187854 const char *zName;
187855 } aFunc[] = {
187856 { geopolyAreaFunc, 1, 1, "geopoly_area" },
187857 { geopolyBlobFunc, 1, 1, "geopoly_blob" },
187858 { geopolyJsonFunc, 1, 1, "geopoly_json" },
187859 { geopolySvgFunc, -1, 1, "geopoly_svg" },
187860 { geopolyWithinFunc, 2, 1, "geopoly_within" },
187861 { geopolyContainsPointFunc, 3, 1, "geopoly_contains_point" },
187862 { geopolyOverlapFunc, 2, 1, "geopoly_overlap" },
187863 { geopolyDebugFunc, 1, 0, "geopoly_debug" },
187864 { geopolyBBoxFunc, 1, 1, "geopoly_bbox" },
187865 { geopolyXformFunc, 7, 1, "geopoly_xform" },
187866 { geopolyRegularFunc, 4, 1, "geopoly_regular" },
187867 { geopolyCcwFunc, 1, 1, "geopoly_ccw" },
187868 };
187869 static const struct {
187870 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
187871 void (*xFinal)(sqlite3_context*);
187872 const char *zName;
187873 } aAgg[] = {
187874 { geopolyBBoxStep, geopolyBBoxFinal, "geopoly_group_bbox" },
187875 };
187876 int i;
187877 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK0; i++){
187878 int enc = aFunc[i].bPure ? SQLITE_UTF81|SQLITE_DETERMINISTIC0x800 : SQLITE_UTF81;
187879 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
187880 enc, 0,
187881 aFunc[i].xFunc, 0, 0);
187882 }
187883 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK0; i++){
187884 rc = sqlite3_create_function(db, aAgg[i].zName, 1, SQLITE_UTF81, 0,
187885 0, aAgg[i].xStep, aAgg[i].xFinal);
187886 }
187887 if( rc==SQLITE_OK0 ){
187888 rc = sqlite3_create_module_v2(db, "geopoly", &geopolyModule, 0, 0);
187889 }
187890 return rc;
187891}
187892
187893/************** End of geopoly.c *********************************************/
187894/************** Continuing where we left off in rtree.c **********************/
187895#endif
187896
187897/*
187898** Register the r-tree module with database handle db. This creates the
187899** virtual table module "rtree" and the debugging/analysis scalar
187900** function "rtreenode".
187901*/
187902SQLITE_PRIVATEstatic int sqlite3RtreeInit(sqlite3 *db){
187903 const int utf8 = SQLITE_UTF81;
187904 int rc;
187905
187906 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
187907 if( rc==SQLITE_OK0 ){
187908 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
187909 }
187910 if( rc==SQLITE_OK0 ){
187911 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
187912 }
187913 if( rc==SQLITE_OK0 ){
187914#ifdef SQLITE_RTREE_INT_ONLY
187915 void *c = (void *)RTREE_COORD_INT32;
187916#else
187917 void *c = (void *)RTREE_COORD_REAL32;
187918#endif
187919 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
187920 }
187921 if( rc==SQLITE_OK0 ){
187922 void *c = (void *)RTREE_COORD_INT32;
187923 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
187924 }
187925#ifdef SQLITE_ENABLE_GEOPOLY
187926 if( rc==SQLITE_OK0 ){
187927 rc = sqlite3_geopoly_init(db);
187928 }
187929#endif
187930
187931 return rc;
187932}
187933
187934/*
187935** This routine deletes the RtreeGeomCallback object that was attached
187936** one of the SQL functions create by sqlite3_rtree_geometry_callback()
187937** or sqlite3_rtree_query_callback(). In other words, this routine is the
187938** destructor for an RtreeGeomCallback objecct. This routine is called when
187939** the corresponding SQL function is deleted.
187940*/
187941static void rtreeFreeCallback(void *p){
187942 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
187943 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
187944 sqlite3_free(p);
187945}
187946
187947/*
187948** This routine frees the BLOB that is returned by geomCallback().
187949*/
187950static void rtreeMatchArgFree(void *pArg){
187951 int i;
187952 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
187953 for(i=0; i<p->nParam; i++){
187954 sqlite3_value_free(p->apSqlParam[i]);
187955 }
187956 sqlite3_free(p);
187957}
187958
187959/*
187960** Each call to sqlite3_rtree_geometry_callback() or
187961** sqlite3_rtree_query_callback() creates an ordinary SQLite
187962** scalar function that is implemented by this routine.
187963**
187964** All this function does is construct an RtreeMatchArg object that
187965** contains the geometry-checking callback routines and a list of
187966** parameters to this function, then return that RtreeMatchArg object
187967** as a BLOB.
187968**
187969** The R-Tree MATCH operator will read the returned BLOB, deserialize
187970** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
187971** out which elements of the R-Tree should be returned by the query.
187972*/
187973static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
187974 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
187975 RtreeMatchArg *pBlob;
187976 sqlite3_int64 nBlob;
187977 int memErr = 0;
187978
187979 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
187980 + nArg*sizeof(sqlite3_value*);
187981 pBlob = (RtreeMatchArg *)sqlite3_malloc64(nBlob);
187982 if( !pBlob ){
187983 sqlite3_result_error_nomem(ctx);
187984 }else{
187985 int i;
187986 pBlob->iSize = nBlob;
187987 pBlob->cb = pGeomCtx[0];
187988 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
187989 pBlob->nParam = nArg;
187990 for(i=0; i<nArg; i++){
187991 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
187992 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
187993#ifdef SQLITE_RTREE_INT_ONLY
187994 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
187995#else
187996 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
187997#endif
187998 }
187999 if( memErr ){
188000 sqlite3_result_error_nomem(ctx);
188001 rtreeMatchArgFree(pBlob);
188002 }else{
188003 sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
188004 }
188005 }
188006}
188007
188008/*
188009** Register a new geometry function for use with the r-tree MATCH operator.
188010*/
188011SQLITE_API int sqlite3_rtree_geometry_callback(
188012 sqlite3 *db, /* Register SQL function on this connection */
188013 const char *zGeom, /* Name of the new SQL function */
188014 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
188015 void *pContext /* Extra data associated with the callback */
188016){
188017 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
188018
188019 /* Allocate and populate the context object. */
188020 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
188021 if( !pGeomCtx ) return SQLITE_NOMEM7;
188022 pGeomCtx->xGeom = xGeom;
188023 pGeomCtx->xQueryFunc = 0;
188024 pGeomCtx->xDestructor = 0;
188025 pGeomCtx->pContext = pContext;
188026 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY5,
188027 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
188028 );
188029}
188030
188031/*
188032** Register a new 2nd-generation geometry function for use with the
188033** r-tree MATCH operator.
188034*/
188035SQLITE_API int sqlite3_rtree_query_callback(
188036 sqlite3 *db, /* Register SQL function on this connection */
188037 const char *zQueryFunc, /* Name of new SQL function */
188038 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
188039 void *pContext, /* Extra data passed into the callback */
188040 void (*xDestructor)(void*) /* Destructor for the extra data */
188041){
188042 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
188043
188044 /* Allocate and populate the context object. */
188045 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
188046 if( !pGeomCtx ) return SQLITE_NOMEM7;
188047 pGeomCtx->xGeom = 0;
188048 pGeomCtx->xQueryFunc = xQueryFunc;
188049 pGeomCtx->xDestructor = xDestructor;
188050 pGeomCtx->pContext = pContext;
188051 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY5,
188052 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
188053 );
188054}
188055
188056#if !SQLITE_CORE1
188057#ifdef _WIN32
188058__declspec(dllexport)
188059#endif
188060SQLITE_API int sqlite3_rtree_init(
188061 sqlite3 *db,
188062 char **pzErrMsg,
188063 const sqlite3_api_routines *pApi
188064){
188065 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
188066 return sqlite3RtreeInit(db);
188067}
188068#endif
188069
188070#endif
188071
188072/************** End of rtree.c ***********************************************/
188073/************** Begin file icu.c *********************************************/
188074/*
188075** 2007 May 6
188076**
188077** The author disclaims copyright to this source code. In place of
188078** a legal notice, here is a blessing:
188079**
188080** May you do good and not evil.
188081** May you find forgiveness for yourself and forgive others.
188082** May you share freely, never taking more than you give.
188083**
188084*************************************************************************
188085** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
188086**
188087** This file implements an integration between the ICU library
188088** ("International Components for Unicode", an open-source library
188089** for handling unicode data) and SQLite. The integration uses
188090** ICU to provide the following to SQLite:
188091**
188092** * An implementation of the SQL regexp() function (and hence REGEXP
188093** operator) using the ICU uregex_XX() APIs.
188094**
188095** * Implementations of the SQL scalar upper() and lower() functions
188096** for case mapping.
188097**
188098** * Integration of ICU and SQLite collation sequences.
188099**
188100** * An implementation of the LIKE operator that uses ICU to
188101** provide case-independent matching.
188102*/
188103
188104#if !defined(SQLITE_CORE1) \
188105 || defined(SQLITE_ENABLE_ICU) \
188106 || defined(SQLITE_ENABLE_ICU_COLLATIONS)
188107
188108/* Include ICU headers */
188109#include <unicode/utypes.h>
188110#include <unicode/uregex.h>
188111#include <unicode/ustring.h>
188112#include <unicode/ucol.h>
188113
188114/* #include <assert.h> */
188115
188116#ifndef SQLITE_CORE1
188117/* #include "sqlite3ext.h" */
188118 SQLITE_EXTENSION_INIT1
188119#else
188120/* #include "sqlite3.h" */
188121#endif
188122
188123/*
188124** This function is called when an ICU function called from within
188125** the implementation of an SQL scalar function returns an error.
188126**
188127** The scalar function context passed as the first argument is
188128** loaded with an error message based on the following two args.
188129*/
188130static void icuFunctionError(
188131 sqlite3_context *pCtx, /* SQLite scalar function context */
188132 const char *zName, /* Name of ICU function that failed */
188133 UErrorCode e /* Error code returned by ICU function */
188134){
188135 char zBuf[128];
188136 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
188137 zBuf[127] = '\0';
188138 sqlite3_result_error(pCtx, zBuf, -1);
188139}
188140
188141#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_ICU)
188142
188143/*
188144** Maximum length (in bytes) of the pattern in a LIKE or GLOB
188145** operator.
188146*/
188147#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH50000
188148# define SQLITE_MAX_LIKE_PATTERN_LENGTH50000 50000
188149#endif
188150
188151/*
188152** Version of sqlite3_free() that is always a function, never a macro.
188153*/
188154static void xFree(void *p){
188155 sqlite3_free(p);
188156}
188157
188158/*
188159** This lookup table is used to help decode the first byte of
188160** a multi-byte UTF8 character. It is copied here from SQLite source
188161** code file utf8.c.
188162*/
188163static const unsigned char icuUtf8Trans1[] = {
188164 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
188165 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
188166 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
188167 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
188168 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
188169 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
188170 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
188171 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
188172};
188173
188174#define SQLITE_ICU_READ_UTF8(zIn, c) \
188175 c = *(zIn++); \
188176 if( c>=0xc0 ){ \
188177 c = icuUtf8Trans1[c-0xc0]; \
188178 while( (*zIn & 0xc0)==0x80 ){ \
188179 c = (c<<6) + (0x3f & *(zIn++)); \
188180 } \
188181 }
188182
188183#define SQLITE_ICU_SKIP_UTF8(zIn) \
188184 assert( *zIn )((void) (0)); \
188185 if( *(zIn++)>=0xc0 ){ \
188186 while( (*zIn & 0xc0)==0x80 ){zIn++;} \
188187 }
188188
188189
188190/*
188191** Compare two UTF-8 strings for equality where the first string is
188192** a "LIKE" expression. Return true (1) if they are the same and
188193** false (0) if they are different.
188194*/
188195static int icuLikeCompare(
188196 const uint8_t *zPattern, /* LIKE pattern */
188197 const uint8_t *zString, /* The UTF-8 string to compare against */
188198 const UChar32 uEsc /* The escape character */
188199){
188200 static const uint32_t MATCH_ONE = (uint32_t)'_';
188201 static const uint32_t MATCH_ALL = (uint32_t)'%';
188202
188203 int prevEscape = 0; /* True if the previous character was uEsc */
188204
188205 while( 1 ){
188206
188207 /* Read (and consume) the next character from the input pattern. */
188208 uint32_t uPattern;
188209 SQLITE_ICU_READ_UTF8(zPattern, uPattern);
188210 if( uPattern==0 ) break;
188211
188212 /* There are now 4 possibilities:
188213 **
188214 ** 1. uPattern is an unescaped match-all character "%",
188215 ** 2. uPattern is an unescaped match-one character "_",
188216 ** 3. uPattern is an unescaped escape character, or
188217 ** 4. uPattern is to be handled as an ordinary character
188218 */
188219 if( !prevEscape && uPattern==MATCH_ALL ){
188220 /* Case 1. */
188221 uint8_t c;
188222
188223 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
188224 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
188225 ** test string.
188226 */
188227 while( (c=*zPattern) == MATCH_ALL || c == MATCH_ONE ){
188228 if( c==MATCH_ONE ){
188229 if( *zString==0 ) return 0;
188230 SQLITE_ICU_SKIP_UTF8(zString);
188231 }
188232 zPattern++;
188233 }
188234
188235 if( *zPattern==0 ) return 1;
188236
188237 while( *zString ){
188238 if( icuLikeCompare(zPattern, zString, uEsc) ){
188239 return 1;
188240 }
188241 SQLITE_ICU_SKIP_UTF8(zString);
188242 }
188243 return 0;
188244
188245 }else if( !prevEscape && uPattern==MATCH_ONE ){
188246 /* Case 2. */
188247 if( *zString==0 ) return 0;
188248 SQLITE_ICU_SKIP_UTF8(zString);
188249
188250 }else if( !prevEscape && uPattern==(uint32_t)uEsc){
188251 /* Case 3. */
188252 prevEscape = 1;
188253
188254 }else{
188255 /* Case 4. */
188256 uint32_t uString;
188257 SQLITE_ICU_READ_UTF8(zString, uString);
188258 uString = (uint32_t)u_foldCase((UChar32)uString, U_FOLD_CASE_DEFAULT);
188259 uPattern = (uint32_t)u_foldCase((UChar32)uPattern, U_FOLD_CASE_DEFAULT);
188260 if( uString!=uPattern ){
188261 return 0;
188262 }
188263 prevEscape = 0;
188264 }
188265 }
188266
188267 return *zString==0;
188268}
188269
188270/*
188271** Implementation of the like() SQL function. This function implements
188272** the build-in LIKE operator. The first argument to the function is the
188273** pattern and the second argument is the string. So, the SQL statements:
188274**
188275** A LIKE B
188276**
188277** is implemented as like(B, A). If there is an escape character E,
188278**
188279** A LIKE B ESCAPE E
188280**
188281** is mapped to like(B, A, E).
188282*/
188283static void icuLikeFunc(
188284 sqlite3_context *context,
188285 int argc,
188286 sqlite3_value **argv
188287){
188288 const unsigned char *zA = sqlite3_value_text(argv[0]);
188289 const unsigned char *zB = sqlite3_value_text(argv[1]);
188290 UChar32 uEsc = 0;
188291
188292 /* Limit the length of the LIKE or GLOB pattern to avoid problems
188293 ** of deep recursion and N*N behavior in patternCompare().
188294 */
188295 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH50000 ){
188296 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
188297 return;
188298 }
188299
188300
188301 if( argc==3 ){
188302 /* The escape character string must consist of a single UTF-8 character.
188303 ** Otherwise, return an error.
188304 */
188305 int nE= sqlite3_value_bytes(argv[2]);
188306 const unsigned char *zE = sqlite3_value_text(argv[2]);
188307 int i = 0;
188308 if( zE==0 ) return;
188309 U8_NEXT(zE, i, nE, uEsc);
188310 if( i!=nE){
188311 sqlite3_result_error(context,
188312 "ESCAPE expression must be a single character", -1);
188313 return;
188314 }
188315 }
188316
188317 if( zA && zB ){
188318 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
188319 }
188320}
188321
188322/*
188323** Function to delete compiled regexp objects. Registered as
188324** a destructor function with sqlite3_set_auxdata().
188325*/
188326static void icuRegexpDelete(void *p){
188327 URegularExpression *pExpr = (URegularExpression *)p;
188328 uregex_close(pExpr);
188329}
188330
188331/*
188332** Implementation of SQLite REGEXP operator. This scalar function takes
188333** two arguments. The first is a regular expression pattern to compile
188334** the second is a string to match against that pattern. If either
188335** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
188336** is 1 if the string matches the pattern, or 0 otherwise.
188337**
188338** SQLite maps the regexp() function to the regexp() operator such
188339** that the following two are equivalent:
188340**
188341** zString REGEXP zPattern
188342** regexp(zPattern, zString)
188343**
188344** Uses the following ICU regexp APIs:
188345**
188346** uregex_open()
188347** uregex_matches()
188348** uregex_close()
188349*/
188350static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
188351 UErrorCode status = U_ZERO_ERROR;
188352 URegularExpression *pExpr;
188353 UBool res;
188354 const UChar *zString = sqlite3_value_text16(apArg[1]);
188355
188356 (void)nArg; /* Unused parameter */
188357
188358 /* If the left hand side of the regexp operator is NULL,
188359 ** then the result is also NULL.
188360 */
188361 if( !zString ){
188362 return;
188363 }
188364
188365 pExpr = sqlite3_get_auxdata(p, 0);
188366 if( !pExpr ){
188367 const UChar *zPattern = sqlite3_value_text16(apArg[0]);
188368 if( !zPattern ){
188369 return;
188370 }
188371 pExpr = uregex_open(zPattern, -1, 0, 0, &status);
188372
188373 if( U_SUCCESS(status) ){
188374 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
188375 }else{
188376 assert(!pExpr)((void) (0));
188377 icuFunctionError(p, "uregex_open", status);
188378 return;
188379 }
188380 }
188381
188382 /* Configure the text that the regular expression operates on. */
188383 uregex_setText(pExpr, zString, -1, &status);
188384 if( !U_SUCCESS(status) ){
188385 icuFunctionError(p, "uregex_setText", status);
188386 return;
188387 }
188388
188389 /* Attempt the match */
188390 res = uregex_matches(pExpr, 0, &status);
188391 if( !U_SUCCESS(status) ){
188392 icuFunctionError(p, "uregex_matches", status);
188393 return;
188394 }
188395
188396 /* Set the text that the regular expression operates on to a NULL
188397 ** pointer. This is not really necessary, but it is tidier than
188398 ** leaving the regular expression object configured with an invalid
188399 ** pointer after this function returns.
188400 */
188401 uregex_setText(pExpr, 0, 0, &status);
188402
188403 /* Return 1 or 0. */
188404 sqlite3_result_int(p, res ? 1 : 0);
188405}
188406
188407/*
188408** Implementations of scalar functions for case mapping - upper() and
188409** lower(). Function upper() converts its input to upper-case (ABC).
188410** Function lower() converts to lower-case (abc).
188411**
188412** ICU provides two types of case mapping, "general" case mapping and
188413** "language specific". Refer to ICU documentation for the differences
188414** between the two.
188415**
188416** To utilise "general" case mapping, the upper() or lower() scalar
188417** functions are invoked with one argument:
188418**
188419** upper('ABC') -> 'abc'
188420** lower('abc') -> 'ABC'
188421**
188422** To access ICU "language specific" case mapping, upper() or lower()
188423** should be invoked with two arguments. The second argument is the name
188424** of the locale to use. Passing an empty string ("") or SQL NULL value
188425** as the second argument is the same as invoking the 1 argument version
188426** of upper() or lower().
188427**
188428** lower('I', 'en_us') -> 'i'
188429** lower('I', 'tr_tr') -> '\u131' (small dotless i)
188430**
188431** http://www.icu-project.org/userguide/posix.html#case_mappings
188432*/
188433static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
188434 const UChar *zInput; /* Pointer to input string */
188435 UChar *zOutput = 0; /* Pointer to output buffer */
188436 int nInput; /* Size of utf-16 input string in bytes */
188437 int nOut; /* Size of output buffer in bytes */
188438 int cnt;
188439 int bToUpper; /* True for toupper(), false for tolower() */
188440 UErrorCode status;
188441 const char *zLocale = 0;
188442
188443 assert(nArg==1 || nArg==2)((void) (0));
188444 bToUpper = (sqlite3_user_data(p)!=0);
188445 if( nArg==2 ){
188446 zLocale = (const char *)sqlite3_value_text(apArg[1]);
188447 }
188448
188449 zInput = sqlite3_value_text16(apArg[0]);
188450 if( !zInput ){
188451 return;
188452 }
188453 nOut = nInput = sqlite3_value_bytes16(apArg[0]);
188454 if( nOut==0 ){
188455 sqlite3_result_text16(p, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
188456 return;
188457 }
188458
188459 for(cnt=0; cnt<2; cnt++){
188460 UChar *zNew = sqlite3_realloc(zOutput, nOut);
188461 if( zNew==0 ){
188462 sqlite3_free(zOutput);
188463 sqlite3_result_error_nomem(p);
188464 return;
188465 }
188466 zOutput = zNew;
188467 status = U_ZERO_ERROR;
188468 if( bToUpper ){
188469 nOut = 2*u_strToUpper(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
188470 }else{
188471 nOut = 2*u_strToLower(zOutput,nOut/2,zInput,nInput/2,zLocale,&status);
188472 }
188473
188474 if( U_SUCCESS(status) ){
188475 sqlite3_result_text16(p, zOutput, nOut, xFree);
188476 }else if( status==U_BUFFER_OVERFLOW_ERROR ){
188477 assert( cnt==0 )((void) (0));
188478 continue;
188479 }else{
188480 icuFunctionError(p, bToUpper ? "u_strToUpper" : "u_strToLower", status);
188481 }
188482 return;
188483 }
188484 assert( 0 )((void) (0)); /* Unreachable */
188485}
188486
188487#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
188488
188489/*
188490** Collation sequence destructor function. The pCtx argument points to
188491** a UCollator structure previously allocated using ucol_open().
188492*/
188493static void icuCollationDel(void *pCtx){
188494 UCollator *p = (UCollator *)pCtx;
188495 ucol_close(p);
188496}
188497
188498/*
188499** Collation sequence comparison function. The pCtx argument points to
188500** a UCollator structure previously allocated using ucol_open().
188501*/
188502static int icuCollationColl(
188503 void *pCtx,
188504 int nLeft,
188505 const void *zLeft,
188506 int nRight,
188507 const void *zRight
188508){
188509 UCollationResult res;
188510 UCollator *p = (UCollator *)pCtx;
188511 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
188512 switch( res ){
188513 case UCOL_LESS: return -1;
188514 case UCOL_GREATER: return +1;
188515 case UCOL_EQUAL: return 0;
188516 }
188517 assert(!"Unexpected return value from ucol_strcoll()")((void) (0));
188518 return 0;
188519}
188520
188521/*
188522** Implementation of the scalar function icu_load_collation().
188523**
188524** This scalar function is used to add ICU collation based collation
188525** types to an SQLite database connection. It is intended to be called
188526** as follows:
188527**
188528** SELECT icu_load_collation(<locale>, <collation-name>);
188529**
188530** Where <locale> is a string containing an ICU locale identifier (i.e.
188531** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
188532** collation sequence to create.
188533*/
188534static void icuLoadCollation(
188535 sqlite3_context *p,
188536 int nArg,
188537 sqlite3_value **apArg
188538){
188539 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
188540 UErrorCode status = U_ZERO_ERROR;
188541 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
188542 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
188543 UCollator *pUCollator; /* ICU library collation object */
188544 int rc; /* Return code from sqlite3_create_collation_x() */
188545
188546 assert(nArg==2)((void) (0));
188547 (void)nArg; /* Unused parameter */
188548 zLocale = (const char *)sqlite3_value_text(apArg[0]);
188549 zName = (const char *)sqlite3_value_text(apArg[1]);
188550
188551 if( !zLocale || !zName ){
188552 return;
188553 }
188554
188555 pUCollator = ucol_open(zLocale, &status);
188556 if( !U_SUCCESS(status) ){
188557 icuFunctionError(p, "ucol_open", status);
188558 return;
188559 }
188560 assert(p)((void) (0));
188561
188562 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF164, (void *)pUCollator,
188563 icuCollationColl, icuCollationDel
188564 );
188565 if( rc!=SQLITE_OK0 ){
188566 ucol_close(pUCollator);
188567 sqlite3_result_error(p, "Error registering collation function", -1);
188568 }
188569}
188570
188571/*
188572** Register the ICU extension functions with database db.
188573*/
188574SQLITE_PRIVATEstatic int sqlite3IcuInit(sqlite3 *db){
188575 static const struct IcuScalar {
188576 const char *zName; /* Function name */
188577 unsigned char nArg; /* Number of arguments */
188578 unsigned short enc; /* Optimal text encoding */
188579 unsigned char iContext; /* sqlite3_user_data() context */
188580 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
188581 } scalars[] = {
188582 {"icu_load_collation", 2, SQLITE_UTF81, 1, icuLoadCollation},
188583#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_ICU)
188584 {"regexp", 2, SQLITE_ANY5|SQLITE_DETERMINISTIC0x800, 0, icuRegexpFunc},
188585 {"lower", 1, SQLITE_UTF164|SQLITE_DETERMINISTIC0x800, 0, icuCaseFunc16},
188586 {"lower", 2, SQLITE_UTF164|SQLITE_DETERMINISTIC0x800, 0, icuCaseFunc16},
188587 {"upper", 1, SQLITE_UTF164|SQLITE_DETERMINISTIC0x800, 1, icuCaseFunc16},
188588 {"upper", 2, SQLITE_UTF164|SQLITE_DETERMINISTIC0x800, 1, icuCaseFunc16},
188589 {"lower", 1, SQLITE_UTF81|SQLITE_DETERMINISTIC0x800, 0, icuCaseFunc16},
188590 {"lower", 2, SQLITE_UTF81|SQLITE_DETERMINISTIC0x800, 0, icuCaseFunc16},
188591 {"upper", 1, SQLITE_UTF81|SQLITE_DETERMINISTIC0x800, 1, icuCaseFunc16},
188592 {"upper", 2, SQLITE_UTF81|SQLITE_DETERMINISTIC0x800, 1, icuCaseFunc16},
188593 {"like", 2, SQLITE_UTF81|SQLITE_DETERMINISTIC0x800, 0, icuLikeFunc},
188594 {"like", 3, SQLITE_UTF81|SQLITE_DETERMINISTIC0x800, 0, icuLikeFunc},
188595#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) */
188596 };
188597 int rc = SQLITE_OK0;
188598 int i;
188599
188600 for(i=0; rc==SQLITE_OK0 && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
188601 const struct IcuScalar *p = &scalars[i];
188602 rc = sqlite3_create_function(
188603 db, p->zName, p->nArg, p->enc,
188604 p->iContext ? (void*)db : (void*)0,
188605 p->xFunc, 0, 0
188606 );
188607 }
188608
188609 return rc;
188610}
188611
188612#if !SQLITE_CORE1
188613#ifdef _WIN32
188614__declspec(dllexport)
188615#endif
188616SQLITE_API int sqlite3_icu_init(
188617 sqlite3 *db,
188618 char **pzErrMsg,
188619 const sqlite3_api_routines *pApi
188620){
188621 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;
188622 return sqlite3IcuInit(db);
188623}
188624#endif
188625
188626#endif
188627
188628/************** End of icu.c *************************************************/
188629/************** Begin file fts3_icu.c ****************************************/
188630/*
188631** 2007 June 22
188632**
188633** The author disclaims copyright to this source code. In place of
188634** a legal notice, here is a blessing:
188635**
188636** May you do good and not evil.
188637** May you find forgiveness for yourself and forgive others.
188638** May you share freely, never taking more than you give.
188639**
188640*************************************************************************
188641** This file implements a tokenizer for fts3 based on the ICU library.
188642*/
188643/* #include "fts3Int.h" */
188644#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS3)
188645#ifdef SQLITE_ENABLE_ICU
188646
188647/* #include <assert.h> */
188648/* #include <string.h> */
188649/* #include "fts3_tokenizer.h" */
188650
188651#include <unicode/ubrk.h>
188652/* #include <unicode/ucol.h> */
188653/* #include <unicode/ustring.h> */
188654#include <unicode/utf16.h>
188655
188656typedef struct IcuTokenizer IcuTokenizer;
188657typedef struct IcuCursor IcuCursor;
188658
188659struct IcuTokenizer {
188660 sqlite3_tokenizer base;
188661 char *zLocale;
188662};
188663
188664struct IcuCursor {
188665 sqlite3_tokenizer_cursor base;
188666
188667 UBreakIterator *pIter; /* ICU break-iterator object */
188668 int nChar; /* Number of UChar elements in pInput */
188669 UChar *aChar; /* Copy of input using utf-16 encoding */
188670 int *aOffset; /* Offsets of each character in utf-8 input */
188671
188672 int nBuffer;
188673 char *zBuffer;
188674
188675 int iToken;
188676};
188677
188678/*
188679** Create a new tokenizer instance.
188680*/
188681static int icuCreate(
188682 int argc, /* Number of entries in argv[] */
188683 const char * const *argv, /* Tokenizer creation arguments */
188684 sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */
188685){
188686 IcuTokenizer *p;
188687 int n = 0;
188688
188689 if( argc>0 ){
188690 n = strlen(argv[0])+1;
188691 }
188692 p = (IcuTokenizer *)sqlite3_malloc64(sizeof(IcuTokenizer)+n);
188693 if( !p ){
188694 return SQLITE_NOMEM7;
188695 }
188696 memset(p, 0, sizeof(IcuTokenizer));
188697
188698 if( n ){
188699 p->zLocale = (char *)&p[1];
188700 memcpy(p->zLocale, argv[0], n);
188701 }
188702
188703 *ppTokenizer = (sqlite3_tokenizer *)p;
188704
188705 return SQLITE_OK0;
188706}
188707
188708/*
188709** Destroy a tokenizer
188710*/
188711static int icuDestroy(sqlite3_tokenizer *pTokenizer){
188712 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
188713 sqlite3_free(p);
188714 return SQLITE_OK0;
188715}
188716
188717/*
188718** Prepare to begin tokenizing a particular string. The input
188719** string to be tokenized is pInput[0..nBytes-1]. A cursor
188720** used to incrementally tokenize this string is returned in
188721** *ppCursor.
188722*/
188723static int icuOpen(
188724 sqlite3_tokenizer *pTokenizer, /* The tokenizer */
188725 const char *zInput, /* Input string */
188726 int nInput, /* Length of zInput in bytes */
188727 sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */
188728){
188729 IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
188730 IcuCursor *pCsr;
188731
188732 const int32_t opt = U_FOLD_CASE_DEFAULT;
188733 UErrorCode status = U_ZERO_ERROR;
188734 int nChar;
188735
188736 UChar32 c;
188737 int iInput = 0;
188738 int iOut = 0;
188739
188740 *ppCursor = 0;
188741
188742 if( zInput==0 ){
188743 nInput = 0;
188744 zInput = "";
188745 }else if( nInput<0 ){
188746 nInput = strlen(zInput);
188747 }
188748 nChar = nInput+1;
188749 pCsr = (IcuCursor *)sqlite3_malloc64(
188750 sizeof(IcuCursor) + /* IcuCursor */
188751 ((nChar+3)&~3) * sizeof(UChar) + /* IcuCursor.aChar[] */
188752 (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */
188753 );
188754 if( !pCsr ){
188755 return SQLITE_NOMEM7;
188756 }
188757 memset(pCsr, 0, sizeof(IcuCursor));
188758 pCsr->aChar = (UChar *)&pCsr[1];
188759 pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
188760
188761 pCsr->aOffset[iOut] = iInput;
188762 U8_NEXT(zInput, iInput, nInput, c);
188763 while( c>0 ){
188764 int isError = 0;
188765 c = u_foldCase(c, opt);
188766 U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
188767 if( isError ){
188768 sqlite3_free(pCsr);
188769 return SQLITE_ERROR1;
188770 }
188771 pCsr->aOffset[iOut] = iInput;
188772
188773 if( iInput<nInput ){
188774 U8_NEXT(zInput, iInput, nInput, c);
188775 }else{
188776 c = 0;
188777 }
188778 }
188779
188780 pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
188781 if( !U_SUCCESS(status) ){
188782 sqlite3_free(pCsr);
188783 return SQLITE_ERROR1;
188784 }
188785 pCsr->nChar = iOut;
188786
188787 ubrk_first(pCsr->pIter);
188788 *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
188789 return SQLITE_OK0;
188790}
188791
188792/*
188793** Close a tokenization cursor previously opened by a call to icuOpen().
188794*/
188795static int icuClose(sqlite3_tokenizer_cursor *pCursor){
188796 IcuCursor *pCsr = (IcuCursor *)pCursor;
188797 ubrk_close(pCsr->pIter);
188798 sqlite3_free(pCsr->zBuffer);
188799 sqlite3_free(pCsr);
188800 return SQLITE_OK0;
188801}
188802
188803/*
188804** Extract the next token from a tokenization cursor.
188805*/
188806static int icuNext(
188807 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */
188808 const char **ppToken, /* OUT: *ppToken is the token text */
188809 int *pnBytes, /* OUT: Number of bytes in token */
188810 int *piStartOffset, /* OUT: Starting offset of token */
188811 int *piEndOffset, /* OUT: Ending offset of token */
188812 int *piPosition /* OUT: Position integer of token */
188813){
188814 IcuCursor *pCsr = (IcuCursor *)pCursor;
188815
188816 int iStart = 0;
188817 int iEnd = 0;
188818 int nByte = 0;
188819
188820 while( iStart==iEnd ){
188821 UChar32 c;
188822
188823 iStart = ubrk_current(pCsr->pIter);
188824 iEnd = ubrk_next(pCsr->pIter);
188825 if( iEnd==UBRK_DONE ){
188826 return SQLITE_DONE101;
188827 }
188828
188829 while( iStart<iEnd ){
188830 int iWhite = iStart;
188831 U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
188832 if( u_isspace(c) ){
188833 iStart = iWhite;
188834 }else{
188835 break;
188836 }
188837 }
188838 assert(iStart<=iEnd)((void) (0));
188839 }
188840
188841 do {
188842 UErrorCode status = U_ZERO_ERROR;
188843 if( nByte ){
188844 char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
188845 if( !zNew ){
188846 return SQLITE_NOMEM7;
188847 }
188848 pCsr->zBuffer = zNew;
188849 pCsr->nBuffer = nByte;
188850 }
188851
188852 u_strToUTF8(
188853 pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */
188854 &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */
188855 &status /* Output success/failure */
188856 );
188857 } while( nByte>pCsr->nBuffer );
188858
188859 *ppToken = pCsr->zBuffer;
188860 *pnBytes = nByte;
188861 *piStartOffset = pCsr->aOffset[iStart];
188862 *piEndOffset = pCsr->aOffset[iEnd];
188863 *piPosition = pCsr->iToken++;
188864
188865 return SQLITE_OK0;
188866}
188867
188868/*
188869** The set of routines that implement the simple tokenizer
188870*/
188871static const sqlite3_tokenizer_module icuTokenizerModule = {
188872 0, /* iVersion */
188873 icuCreate, /* xCreate */
188874 icuDestroy, /* xCreate */
188875 icuOpen, /* xOpen */
188876 icuClose, /* xClose */
188877 icuNext, /* xNext */
188878 0, /* xLanguageid */
188879};
188880
188881/*
188882** Set *ppModule to point at the implementation of the ICU tokenizer.
188883*/
188884SQLITE_PRIVATEstatic void sqlite3Fts3IcuTokenizerModule(
188885 sqlite3_tokenizer_module const**ppModule
188886){
188887 *ppModule = &icuTokenizerModule;
188888}
188889
188890#endif /* defined(SQLITE_ENABLE_ICU) */
188891#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
188892
188893/************** End of fts3_icu.c ********************************************/
188894/************** Begin file sqlite3rbu.c **************************************/
188895/*
188896** 2014 August 30
188897**
188898** The author disclaims copyright to this source code. In place of
188899** a legal notice, here is a blessing:
188900**
188901** May you do good and not evil.
188902** May you find forgiveness for yourself and forgive others.
188903** May you share freely, never taking more than you give.
188904**
188905*************************************************************************
188906**
188907**
188908** OVERVIEW
188909**
188910** The RBU extension requires that the RBU update be packaged as an
188911** SQLite database. The tables it expects to find are described in
188912** sqlite3rbu.h. Essentially, for each table xyz in the target database
188913** that the user wishes to write to, a corresponding data_xyz table is
188914** created in the RBU database and populated with one row for each row to
188915** update, insert or delete from the target table.
188916**
188917** The update proceeds in three stages:
188918**
188919** 1) The database is updated. The modified database pages are written
188920** to a *-oal file. A *-oal file is just like a *-wal file, except
188921** that it is named "<database>-oal" instead of "<database>-wal".
188922** Because regular SQLite clients do not look for file named
188923** "<database>-oal", they go on using the original database in
188924** rollback mode while the *-oal file is being generated.
188925**
188926** During this stage RBU does not update the database by writing
188927** directly to the target tables. Instead it creates "imposter"
188928** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
188929** to update each b-tree individually. All updates required by each
188930** b-tree are completed before moving on to the next, and all
188931** updates are done in sorted key order.
188932**
188933** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
188934** location using a call to rename(2). Before doing this the RBU
188935** module takes an EXCLUSIVE lock on the database file, ensuring
188936** that there are no other active readers.
188937**
188938** Once the EXCLUSIVE lock is released, any other database readers
188939** detect the new *-wal file and read the database in wal mode. At
188940** this point they see the new version of the database - including
188941** the updates made as part of the RBU update.
188942**
188943** 3) The new *-wal file is checkpointed. This proceeds in the same way
188944** as a regular database checkpoint, except that a single frame is
188945** checkpointed each time sqlite3rbu_step() is called. If the RBU
188946** handle is closed before the entire *-wal file is checkpointed,
188947** the checkpoint progress is saved in the RBU database and the
188948** checkpoint can be resumed by another RBU client at some point in
188949** the future.
188950**
188951** POTENTIAL PROBLEMS
188952**
188953** The rename() call might not be portable. And RBU is not currently
188954** syncing the directory after renaming the file.
188955**
188956** When state is saved, any commit to the *-oal file and the commit to
188957** the RBU update database are not atomic. So if the power fails at the
188958** wrong moment they might get out of sync. As the main database will be
188959** committed before the RBU update database this will likely either just
188960** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
188961** constraint violations).
188962**
188963** If some client does modify the target database mid RBU update, or some
188964** other error occurs, the RBU extension will keep throwing errors. It's
188965** not really clear how to get out of this state. The system could just
188966** by delete the RBU update database and *-oal file and have the device
188967** download the update again and start over.
188968**
188969** At present, for an UPDATE, both the new.* and old.* records are
188970** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
188971** fields are collected. This means we're probably writing a lot more
188972** data to disk when saving the state of an ongoing update to the RBU
188973** update database than is strictly necessary.
188974**
188975*/
188976
188977/* #include <assert.h> */
188978/* #include <string.h> */
188979/* #include <stdio.h> */
188980
188981/* #include "sqlite3.h" */
188982
188983#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_RBU)
188984/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
188985/************** Begin file sqlite3rbu.h **************************************/
188986/*
188987** 2014 August 30
188988**
188989** The author disclaims copyright to this source code. In place of
188990** a legal notice, here is a blessing:
188991**
188992** May you do good and not evil.
188993** May you find forgiveness for yourself and forgive others.
188994** May you share freely, never taking more than you give.
188995**
188996*************************************************************************
188997**
188998** This file contains the public interface for the RBU extension.
188999*/
189000
189001/*
189002** SUMMARY
189003**
189004** Writing a transaction containing a large number of operations on
189005** b-tree indexes that are collectively larger than the available cache
189006** memory can be very inefficient.
189007**
189008** The problem is that in order to update a b-tree, the leaf page (at least)
189009** containing the entry being inserted or deleted must be modified. If the
189010** working set of leaves is larger than the available cache memory, then a
189011** single leaf that is modified more than once as part of the transaction
189012** may be loaded from or written to the persistent media multiple times.
189013** Additionally, because the index updates are likely to be applied in
189014** random order, access to pages within the database is also likely to be in
189015** random order, which is itself quite inefficient.
189016**
189017** One way to improve the situation is to sort the operations on each index
189018** by index key before applying them to the b-tree. This leads to an IO
189019** pattern that resembles a single linear scan through the index b-tree,
189020** and all but guarantees each modified leaf page is loaded and stored
189021** exactly once. SQLite uses this trick to improve the performance of
189022** CREATE INDEX commands. This extension allows it to be used to improve
189023** the performance of large transactions on existing databases.
189024**
189025** Additionally, this extension allows the work involved in writing the
189026** large transaction to be broken down into sub-transactions performed
189027** sequentially by separate processes. This is useful if the system cannot
189028** guarantee that a single update process will run for long enough to apply
189029** the entire update, for example because the update is being applied on a
189030** mobile device that is frequently rebooted. Even after the writer process
189031** has committed one or more sub-transactions, other database clients continue
189032** to read from the original database snapshot. In other words, partially
189033** applied transactions are not visible to other clients.
189034**
189035** "RBU" stands for "Resumable Bulk Update". As in a large database update
189036** transmitted via a wireless network to a mobile device. A transaction
189037** applied using this extension is hence refered to as an "RBU update".
189038**
189039**
189040** LIMITATIONS
189041**
189042** An "RBU update" transaction is subject to the following limitations:
189043**
189044** * The transaction must consist of INSERT, UPDATE and DELETE operations
189045** only.
189046**
189047** * INSERT statements may not use any default values.
189048**
189049** * UPDATE and DELETE statements must identify their target rows by
189050** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
189051** KEY fields may not be updated or deleted. If the table being written
189052** has no PRIMARY KEY, affected rows must be identified by rowid.
189053**
189054** * UPDATE statements may not modify PRIMARY KEY columns.
189055**
189056** * No triggers will be fired.
189057**
189058** * No foreign key violations are detected or reported.
189059**
189060** * CHECK constraints are not enforced.
189061**
189062** * No constraint handling mode except for "OR ROLLBACK" is supported.
189063**
189064**
189065** PREPARATION
189066**
189067** An "RBU update" is stored as a separate SQLite database. A database
189068** containing an RBU update is an "RBU database". For each table in the
189069** target database to be updated, the RBU database should contain a table
189070** named "data_<target name>" containing the same set of columns as the
189071** target table, and one more - "rbu_control". The data_% table should
189072** have no PRIMARY KEY or UNIQUE constraints, but each column should have
189073** the same type as the corresponding column in the target database.
189074** The "rbu_control" column should have no type at all. For example, if
189075** the target database contains:
189076**
189077** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
189078**
189079** Then the RBU database should contain:
189080**
189081** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
189082**
189083** The order of the columns in the data_% table does not matter.
189084**
189085** Instead of a regular table, the RBU database may also contain virtual
189086** tables or view named using the data_<target> naming scheme.
189087**
189088** Instead of the plain data_<target> naming scheme, RBU database tables
189089** may also be named data<integer>_<target>, where <integer> is any sequence
189090** of zero or more numeric characters (0-9). This can be significant because
189091** tables within the RBU database are always processed in order sorted by
189092** name. By judicious selection of the <integer> portion of the names
189093** of the RBU tables the user can therefore control the order in which they
189094** are processed. This can be useful, for example, to ensure that "external
189095** content" FTS4 tables are updated before their underlying content tables.
189096**
189097** If the target database table is a virtual table or a table that has no
189098** PRIMARY KEY declaration, the data_% table must also contain a column
189099** named "rbu_rowid". This column is mapped to the tables implicit primary
189100** key column - "rowid". Virtual tables for which the "rowid" column does
189101** not function like a primary key value cannot be updated using RBU. For
189102** example, if the target db contains either of the following:
189103**
189104** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
189105** CREATE TABLE x1(a, b)
189106**
189107** then the RBU database should contain:
189108**
189109** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
189110**
189111** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
189112** target table must be present in the input table. For virtual tables,
189113** hidden columns are optional - they are updated by RBU if present in
189114** the input table, or not otherwise. For example, to write to an fts4
189115** table with a hidden languageid column such as:
189116**
189117** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
189118**
189119** Either of the following input table schemas may be used:
189120**
189121** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
189122** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
189123**
189124** For each row to INSERT into the target database as part of the RBU
189125** update, the corresponding data_% table should contain a single record
189126** with the "rbu_control" column set to contain integer value 0. The
189127** other columns should be set to the values that make up the new record
189128** to insert.
189129**
189130** If the target database table has an INTEGER PRIMARY KEY, it is not
189131** possible to insert a NULL value into the IPK column. Attempting to
189132** do so results in an SQLITE_MISMATCH error.
189133**
189134** For each row to DELETE from the target database as part of the RBU
189135** update, the corresponding data_% table should contain a single record
189136** with the "rbu_control" column set to contain integer value 1. The
189137** real primary key values of the row to delete should be stored in the
189138** corresponding columns of the data_% table. The values stored in the
189139** other columns are not used.
189140**
189141** For each row to UPDATE from the target database as part of the RBU
189142** update, the corresponding data_% table should contain a single record
189143** with the "rbu_control" column set to contain a value of type text.
189144** The real primary key values identifying the row to update should be
189145** stored in the corresponding columns of the data_% table row, as should
189146** the new values of all columns being update. The text value in the
189147** "rbu_control" column must contain the same number of characters as
189148** there are columns in the target database table, and must consist entirely
189149** of 'x' and '.' characters (or in some special cases 'd' - see below). For
189150** each column that is being updated, the corresponding character is set to
189151** 'x'. For those that remain as they are, the corresponding character of the
189152** rbu_control value should be set to '.'. For example, given the tables
189153** above, the update statement:
189154**
189155** UPDATE t1 SET c = 'usa' WHERE a = 4;
189156**
189157** is represented by the data_t1 row created by:
189158**
189159** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
189160**
189161** Instead of an 'x' character, characters of the rbu_control value specified
189162** for UPDATEs may also be set to 'd'. In this case, instead of updating the
189163** target table with the value stored in the corresponding data_% column, the
189164** user-defined SQL function "rbu_delta()" is invoked and the result stored in
189165** the target table column. rbu_delta() is invoked with two arguments - the
189166** original value currently stored in the target table column and the
189167** value specified in the data_xxx table.
189168**
189169** For example, this row:
189170**
189171** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
189172**
189173** is similar to an UPDATE statement such as:
189174**
189175** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
189176**
189177** Finally, if an 'f' character appears in place of a 'd' or 's' in an
189178** ota_control string, the contents of the data_xxx table column is assumed
189179** to be a "fossil delta" - a patch to be applied to a blob value in the
189180** format used by the fossil source-code management system. In this case
189181** the existing value within the target database table must be of type BLOB.
189182** It is replaced by the result of applying the specified fossil delta to
189183** itself.
189184**
189185** If the target database table is a virtual table or a table with no PRIMARY
189186** KEY, the rbu_control value should not include a character corresponding
189187** to the rbu_rowid value. For example, this:
189188**
189189** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
189190** VALUES(NULL, 'usa', 12, '.x');
189191**
189192** causes a result similar to:
189193**
189194** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
189195**
189196** The data_xxx tables themselves should have no PRIMARY KEY declarations.
189197** However, RBU is more efficient if reading the rows in from each data_xxx
189198** table in "rowid" order is roughly the same as reading them sorted by
189199** the PRIMARY KEY of the corresponding target database table. In other
189200** words, rows should be sorted using the destination table PRIMARY KEY
189201** fields before they are inserted into the data_xxx tables.
189202**
189203** USAGE
189204**
189205** The API declared below allows an application to apply an RBU update
189206** stored on disk to an existing target database. Essentially, the
189207** application:
189208**
189209** 1) Opens an RBU handle using the sqlite3rbu_open() function.
189210**
189211** 2) Registers any required virtual table modules with the database
189212** handle returned by sqlite3rbu_db(). Also, if required, register
189213** the rbu_delta() implementation.
189214**
189215** 3) Calls the sqlite3rbu_step() function one or more times on
189216** the new handle. Each call to sqlite3rbu_step() performs a single
189217** b-tree operation, so thousands of calls may be required to apply
189218** a complete update.
189219**
189220** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
189221** sqlite3rbu_step() has been called enough times to completely
189222** apply the update to the target database, then the RBU database
189223** is marked as fully applied. Otherwise, the state of the RBU
189224** update application is saved in the RBU database for later
189225** resumption.
189226**
189227** See comments below for more detail on APIs.
189228**
189229** If an update is only partially applied to the target database by the
189230** time sqlite3rbu_close() is called, various state information is saved
189231** within the RBU database. This allows subsequent processes to automatically
189232** resume the RBU update from where it left off.
189233**
189234** To remove all RBU extension state information, returning an RBU database
189235** to its original contents, it is sufficient to drop all tables that begin
189236** with the prefix "rbu_"
189237**
189238** DATABASE LOCKING
189239**
189240** An RBU update may not be applied to a database in WAL mode. Attempting
189241** to do so is an error (SQLITE_ERROR).
189242**
189243** While an RBU handle is open, a SHARED lock may be held on the target
189244** database file. This means it is possible for other clients to read the
189245** database, but not to write it.
189246**
189247** If an RBU update is started and then suspended before it is completed,
189248** then an external client writes to the database, then attempting to resume
189249** the suspended RBU update is also an error (SQLITE_BUSY).
189250*/
189251
189252#ifndef _SQLITE3RBU_H
189253#define _SQLITE3RBU_H
189254
189255/* #include "sqlite3.h" ** Required for error code definitions ** */
189256
189257#if 0
189258extern "C" {
189259#endif
189260
189261typedef struct sqlite3rbu sqlite3rbu;
189262
189263/*
189264** Open an RBU handle.
189265**
189266** Argument zTarget is the path to the target database. Argument zRbu is
189267** the path to the RBU database. Each call to this function must be matched
189268** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
189269** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
189270** or zRbu begin with "file:", it will be interpreted as an SQLite
189271** database URI, not a regular file name.
189272**
189273** If the zState argument is passed a NULL value, the RBU extension stores
189274** the current state of the update (how many rows have been updated, which
189275** indexes are yet to be updated etc.) within the RBU database itself. This
189276** can be convenient, as it means that the RBU application does not need to
189277** organize removing a separate state file after the update is concluded.
189278** Or, if zState is non-NULL, it must be a path to a database file in which
189279** the RBU extension can store the state of the update.
189280**
189281** When resuming an RBU update, the zState argument must be passed the same
189282** value as when the RBU update was started.
189283**
189284** Once the RBU update is finished, the RBU extension does not
189285** automatically remove any zState database file, even if it created it.
189286**
189287** By default, RBU uses the default VFS to access the files on disk. To
189288** use a VFS other than the default, an SQLite "file:" URI containing a
189289** "vfs=..." option may be passed as the zTarget option.
189290**
189291** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
189292** SQLite's built-in VFSs, including the multiplexor VFS. However it does
189293** not work out of the box with zipvfs. Refer to the comment describing
189294** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
189295*/
189296SQLITE_API sqlite3rbu *sqlite3rbu_open(
189297 const char *zTarget,
189298 const char *zRbu,
189299 const char *zState
189300);
189301
189302/*
189303** Open an RBU handle to perform an RBU vacuum on database file zTarget.
189304** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
189305** that it can be suspended and resumed like an RBU update.
189306**
189307** The second argument to this function identifies a database in which
189308** to store the state of the RBU vacuum operation if it is suspended. The
189309** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
189310** operation, the state database should either not exist or be empty
189311** (contain no tables). If an RBU vacuum is suspended by calling
189312** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
189313** returned SQLITE_DONE, the vacuum state is stored in the state database.
189314** The vacuum can be resumed by calling this function to open a new RBU
189315** handle specifying the same target and state databases.
189316**
189317** If the second argument passed to this function is NULL, then the
189318** name of the state database is "<database>-vacuum", where <database>
189319** is the name of the target database file. In this case, on UNIX, if the
189320** state database is not already present in the file-system, it is created
189321** with the same permissions as the target db is made.
189322**
189323** With an RBU vacuum, it is an SQLITE_MISUSE error if the name of the
189324** state database ends with "-vactmp". This name is reserved for internal
189325** use.
189326**
189327** This function does not delete the state database after an RBU vacuum
189328** is completed, even if it created it. However, if the call to
189329** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
189330** of the state tables within the state database are zeroed. This way,
189331** the next call to sqlite3rbu_vacuum() opens a handle that starts a
189332** new RBU vacuum operation.
189333**
189334** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
189335** describing the sqlite3rbu_create_vfs() API function below for
189336** a description of the complications associated with using RBU with
189337** zipvfs databases.
189338*/
189339SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
189340 const char *zTarget,
189341 const char *zState
189342);
189343
189344/*
189345** Configure a limit for the amount of temp space that may be used by
189346** the RBU handle passed as the first argument. The new limit is specified
189347** in bytes by the second parameter. If it is positive, the limit is updated.
189348** If the second parameter to this function is passed zero, then the limit
189349** is removed entirely. If the second parameter is negative, the limit is
189350** not modified (this is useful for querying the current limit).
189351**
189352** In all cases the returned value is the current limit in bytes (zero
189353** indicates unlimited).
189354**
189355** If the temp space limit is exceeded during operation, an SQLITE_FULL
189356** error is returned.
189357*/
189358SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu*, sqlite3_int64);
189359
189360/*
189361** Return the current amount of temp file space, in bytes, currently used by
189362** the RBU handle passed as the only argument.
189363*/
189364SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu*);
189365
189366/*
189367** Internally, each RBU connection uses a separate SQLite database
189368** connection to access the target and rbu update databases. This
189369** API allows the application direct access to these database handles.
189370**
189371** The first argument passed to this function must be a valid, open, RBU
189372** handle. The second argument should be passed zero to access the target
189373** database handle, or non-zero to access the rbu update database handle.
189374** Accessing the underlying database handles may be useful in the
189375** following scenarios:
189376**
189377** * If any target tables are virtual tables, it may be necessary to
189378** call sqlite3_create_module() on the target database handle to
189379** register the required virtual table implementations.
189380**
189381** * If the data_xxx tables in the RBU source database are virtual
189382** tables, the application may need to call sqlite3_create_module() on
189383** the rbu update db handle to any required virtual table
189384** implementations.
189385**
189386** * If the application uses the "rbu_delta()" feature described above,
189387** it must use sqlite3_create_function() or similar to register the
189388** rbu_delta() implementation with the target database handle.
189389**
189390** If an error has occurred, either while opening or stepping the RBU object,
189391** this function may return NULL. The error code and message may be collected
189392** when sqlite3rbu_close() is called.
189393**
189394** Database handles returned by this function remain valid until the next
189395** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
189396*/
189397SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
189398
189399/*
189400** Do some work towards applying the RBU update to the target db.
189401**
189402** Return SQLITE_DONE if the update has been completely applied, or
189403** SQLITE_OK if no error occurs but there remains work to do to apply
189404** the RBU update. If an error does occur, some other error code is
189405** returned.
189406**
189407** Once a call to sqlite3rbu_step() has returned a value other than
189408** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
189409** that immediately return the same value.
189410*/
189411SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
189412
189413/*
189414** Force RBU to save its state to disk.
189415**
189416** If a power failure or application crash occurs during an update, following
189417** system recovery RBU may resume the update from the point at which the state
189418** was last saved. In other words, from the most recent successful call to
189419** sqlite3rbu_close() or this function.
189420**
189421** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
189422*/
189423SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
189424
189425/*
189426** Close an RBU handle.
189427**
189428** If the RBU update has been completely applied, mark the RBU database
189429** as fully applied. Otherwise, assuming no error has occurred, save the
189430** current state of the RBU update appliation to the RBU database.
189431**
189432** If an error has already occurred as part of an sqlite3rbu_step()
189433** or sqlite3rbu_open() call, or if one occurs within this function, an
189434** SQLite error code is returned. Additionally, if pzErrmsg is not NULL,
189435** *pzErrmsg may be set to point to a buffer containing a utf-8 formatted
189436** English language error message. It is the responsibility of the caller to
189437** eventually free any such buffer using sqlite3_free().
189438**
189439** Otherwise, if no error occurs, this function returns SQLITE_OK if the
189440** update has been partially applied, or SQLITE_DONE if it has been
189441** completely applied.
189442*/
189443SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
189444
189445/*
189446** Return the total number of key-value operations (inserts, deletes or
189447** updates) that have been performed on the target database since the
189448** current RBU update was started.
189449*/
189450SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
189451
189452/*
189453** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
189454** progress indications for the two stages of an RBU update. This API may
189455** be useful for driving GUI progress indicators and similar.
189456**
189457** An RBU update is divided into two stages:
189458**
189459** * Stage 1, in which changes are accumulated in an oal/wal file, and
189460** * Stage 2, in which the contents of the wal file are copied into the
189461** main database.
189462**
189463** The update is visible to non-RBU clients during stage 2. During stage 1
189464** non-RBU reader clients may see the original database.
189465**
189466** If this API is called during stage 2 of the update, output variable
189467** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
189468** to a value between 0 and 10000 to indicate the permyriadage progress of
189469** stage 2. A value of 5000 indicates that stage 2 is half finished,
189470** 9000 indicates that it is 90% finished, and so on.
189471**
189472** If this API is called during stage 1 of the update, output variable
189473** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
189474** value to which (*pnOne) is set depends on whether or not the RBU
189475** database contains an "rbu_count" table. The rbu_count table, if it
189476** exists, must contain the same columns as the following:
189477**
189478** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
189479**
189480** There must be one row in the table for each source (data_xxx) table within
189481** the RBU database. The 'tbl' column should contain the name of the source
189482** table. The 'cnt' column should contain the number of rows within the
189483** source table.
189484**
189485** If the rbu_count table is present and populated correctly and this
189486** API is called during stage 1, the *pnOne output variable is set to the
189487** permyriadage progress of the same stage. If the rbu_count table does
189488** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
189489** table exists but is not correctly populated, the value of the *pnOne
189490** output variable during stage 1 is undefined.
189491*/
189492SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int*pnTwo);
189493
189494/*
189495** Obtain an indication as to the current stage of an RBU update or vacuum.
189496** This function always returns one of the SQLITE_RBU_STATE_XXX constants
189497** defined in this file. Return values should be interpreted as follows:
189498**
189499** SQLITE_RBU_STATE_OAL:
189500** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
189501** may either add further data to the *-oal file, or compute data that will
189502** be added by a subsequent call.
189503**
189504** SQLITE_RBU_STATE_MOVE:
189505** RBU has finished building the *-oal file. The next call to sqlite3rbu_step()
189506** will move the *-oal file to the equivalent *-wal path. If the current
189507** operation is an RBU update, then the updated version of the database
189508** file will become visible to ordinary SQLite clients following the next
189509** call to sqlite3rbu_step().
189510**
189511** SQLITE_RBU_STATE_CHECKPOINT:
189512** RBU is currently performing an incremental checkpoint. The next call to
189513** sqlite3rbu_step() will copy a page of data from the *-wal file into
189514** the target database file.
189515**
189516** SQLITE_RBU_STATE_DONE:
189517** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
189518** will immediately return SQLITE_DONE.
189519**
189520** SQLITE_RBU_STATE_ERROR:
189521** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
189522** immediately return the SQLite error code associated with the error.
189523*/
189524#define SQLITE_RBU_STATE_OAL 1
189525#define SQLITE_RBU_STATE_MOVE 2
189526#define SQLITE_RBU_STATE_CHECKPOINT 3
189527#define SQLITE_RBU_STATE_DONE 4
189528#define SQLITE_RBU_STATE_ERROR 5
189529
189530SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
189531
189532/*
189533** Create an RBU VFS named zName that accesses the underlying file-system
189534** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
189535** then the new RBU VFS uses the default system VFS to access the file-system.
189536** The new object is registered as a non-default VFS with SQLite before
189537** returning.
189538**
189539** Part of the RBU implementation uses a custom VFS object. Usually, this
189540** object is created and deleted automatically by RBU.
189541**
189542** The exception is for applications that also use zipvfs. In this case,
189543** the custom VFS must be explicitly created by the user before the RBU
189544** handle is opened. The RBU VFS should be installed so that the zipvfs
189545** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
189546** (for example multiplexor) to access the file-system. For example,
189547** to assemble an RBU enabled VFS stack that uses both zipvfs and
189548** multiplexor (error checking omitted):
189549**
189550** // Create a VFS named "multiplex" (not the default).
189551** sqlite3_multiplex_initialize(0, 0);
189552**
189553** // Create an rbu VFS named "rbu" that uses multiplexor. If the
189554** // second argument were replaced with NULL, the "rbu" VFS would
189555** // access the file-system via the system default VFS, bypassing the
189556** // multiplexor.
189557** sqlite3rbu_create_vfs("rbu", "multiplex");
189558**
189559** // Create a zipvfs VFS named "zipvfs" that uses rbu.
189560** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
189561**
189562** // Make zipvfs the default VFS.
189563** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
189564**
189565** Because the default VFS created above includes a RBU functionality, it
189566** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
189567** that does not include the RBU layer results in an error.
189568**
189569** The overhead of adding the "rbu" VFS to the system is negligible for
189570** non-RBU users. There is no harm in an application accessing the
189571** file-system via "rbu" all the time, even if it only uses RBU functionality
189572** occasionally.
189573*/
189574SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
189575
189576/*
189577** Deregister and destroy an RBU vfs created by an earlier call to
189578** sqlite3rbu_create_vfs().
189579**
189580** VFS objects are not reference counted. If a VFS object is destroyed
189581** before all database handles that use it have been closed, the results
189582** are undefined.
189583*/
189584SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
189585
189586#if 0
189587} /* end of the 'extern "C"' block */
189588#endif
189589
189590#endif /* _SQLITE3RBU_H */
189591
189592/************** End of sqlite3rbu.h ******************************************/
189593/************** Continuing where we left off in sqlite3rbu.c *****************/
189594
189595#if defined(_WIN32_WCE)
189596/* #include "windows.h" */
189597#endif
189598
189599/* Maximum number of prepared UPDATE statements held by this module */
189600#define SQLITE_RBU_UPDATE_CACHESIZE 16
189601
189602/* Delta checksums disabled by default. Compile with -DRBU_ENABLE_DELTA_CKSUM
189603** to enable checksum verification.
189604*/
189605#ifndef RBU_ENABLE_DELTA_CKSUM
189606# define RBU_ENABLE_DELTA_CKSUM 0
189607#endif
189608
189609/*
189610** Swap two objects of type TYPE.
189611*/
189612#if !defined(SQLITE_AMALGAMATION1)
189613# define SWAP(TYPE,A,B){TYPE t=A; A=B; B=t;} {TYPE t=A; A=B; B=t;}
189614#endif
189615
189616/*
189617** The rbu_state table is used to save the state of a partially applied
189618** update so that it can be resumed later. The table consists of integer
189619** keys mapped to values as follows:
189620**
189621** RBU_STATE_STAGE:
189622** May be set to integer values 1, 2, 4 or 5. As follows:
189623** 1: the *-rbu file is currently under construction.
189624** 2: the *-rbu file has been constructed, but not yet moved
189625** to the *-wal path.
189626** 4: the checkpoint is underway.
189627** 5: the rbu update has been checkpointed.
189628**
189629** RBU_STATE_TBL:
189630** Only valid if STAGE==1. The target database name of the table
189631** currently being written.
189632**
189633** RBU_STATE_IDX:
189634** Only valid if STAGE==1. The target database name of the index
189635** currently being written, or NULL if the main table is currently being
189636** updated.
189637**
189638** RBU_STATE_ROW:
189639** Only valid if STAGE==1. Number of rows already processed for the current
189640** table/index.
189641**
189642** RBU_STATE_PROGRESS:
189643** Trbul number of sqlite3rbu_step() calls made so far as part of this
189644** rbu update.
189645**
189646** RBU_STATE_CKPT:
189647** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
189648** header created by recovering the *-wal file. This is used to detect
189649** cases when another client appends frames to the *-wal file in the
189650** middle of an incremental checkpoint (an incremental checkpoint cannot
189651** be continued if this happens).
189652**
189653** RBU_STATE_COOKIE:
189654** Valid if STAGE==1. The current change-counter cookie value in the
189655** target db file.
189656**
189657** RBU_STATE_OALSZ:
189658** Valid if STAGE==1. The size in bytes of the *-oal file.
189659**
189660** RBU_STATE_DATATBL:
189661** Only valid if STAGE==1. The RBU database name of the table
189662** currently being read.
189663*/
189664#define RBU_STATE_STAGE 1
189665#define RBU_STATE_TBL 2
189666#define RBU_STATE_IDX 3
189667#define RBU_STATE_ROW 4
189668#define RBU_STATE_PROGRESS 5
189669#define RBU_STATE_CKPT 6
189670#define RBU_STATE_COOKIE 7
189671#define RBU_STATE_OALSZ 8
189672#define RBU_STATE_PHASEONESTEP 9
189673#define RBU_STATE_DATATBL 10
189674
189675#define RBU_STAGE_OAL 1
189676#define RBU_STAGE_MOVE 2
189677#define RBU_STAGE_CAPTURE 3
189678#define RBU_STAGE_CKPT 4
189679#define RBU_STAGE_DONE 5
189680
189681
189682#define RBU_CREATE_STATE \
189683 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
189684
189685typedef struct RbuFrame RbuFrame;
189686typedef struct RbuObjIter RbuObjIter;
189687typedef struct RbuState RbuState;
189688typedef struct rbu_vfs rbu_vfs;
189689typedef struct rbu_file rbu_file;
189690typedef struct RbuUpdateStmt RbuUpdateStmt;
189691
189692#if !defined(SQLITE_AMALGAMATION1)
189693typedef unsigned int u32;
189694typedef unsigned short u16;
189695typedef unsigned char u8;
189696typedef sqlite3_int64 i64;
189697#endif
189698
189699/*
189700** These values must match the values defined in wal.c for the equivalent
189701** locks. These are not magic numbers as they are part of the SQLite file
189702** format.
189703*/
189704#define WAL_LOCK_WRITE 0
189705#define WAL_LOCK_CKPT 1
189706#define WAL_LOCK_READ0 3
189707
189708#define SQLITE_FCNTL_RBUCNT 5149216
189709
189710/*
189711** A structure to store values read from the rbu_state table in memory.
189712*/
189713struct RbuState {
189714 int eStage;
189715 char *zTbl;
189716 char *zDataTbl;
189717 char *zIdx;
189718 i64 iWalCksum;
189719 int nRow;
189720 i64 nProgress;
189721 u32 iCookie;
189722 i64 iOalSz;
189723 i64 nPhaseOneStep;
189724};
189725
189726struct RbuUpdateStmt {
189727 char *zMask; /* Copy of update mask used with pUpdate */
189728 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
189729 RbuUpdateStmt *pNext;
189730};
189731
189732/*
189733** An iterator of this type is used to iterate through all objects in
189734** the target database that require updating. For each such table, the
189735** iterator visits, in order:
189736**
189737** * the table itself,
189738** * each index of the table (zero or more points to visit), and
189739** * a special "cleanup table" state.
189740**
189741** abIndexed:
189742** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
189743** it points to an array of flags nTblCol elements in size. The flag is
189744** set for each column that is either a part of the PK or a part of an
189745** index. Or clear otherwise.
189746**
189747** If there are one or more partial indexes on the table, all fields of
189748** this array set set to 1. This is because in that case, the module has
189749** no way to tell which fields will be required to add and remove entries
189750** from the partial indexes.
189751**
189752*/
189753struct RbuObjIter {
189754 sqlite3_stmt *pTblIter; /* Iterate through tables */
189755 sqlite3_stmt *pIdxIter; /* Index iterator */
189756 int nTblCol; /* Size of azTblCol[] array */
189757 char **azTblCol; /* Array of unquoted target column names */
189758 char **azTblType; /* Array of target column types */
189759 int *aiSrcOrder; /* src table col -> target table col */
189760 u8 *abTblPk; /* Array of flags, set on target PK columns */
189761 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
189762 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
189763 int eType; /* Table type - an RBU_PK_XXX value */
189764
189765 /* Output variables. zTbl==0 implies EOF. */
189766 int bCleanup; /* True in "cleanup" state */
189767 const char *zTbl; /* Name of target db table */
189768 const char *zDataTbl; /* Name of rbu db table (or null) */
189769 const char *zIdx; /* Name of target db index (or null) */
189770 int iTnum; /* Root page of current object */
189771 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
189772 int bUnique; /* Current index is unique */
189773 int nIndex; /* Number of aux. indexes on table zTbl */
189774
189775 /* Statements created by rbuObjIterPrepareAll() */
189776 int nCol; /* Number of columns in current object */
189777 sqlite3_stmt *pSelect; /* Source data */
189778 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
189779 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
189780 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
189781
189782 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
189783 RbuUpdateStmt *pRbuUpdate;
189784};
189785
189786/*
189787** Values for RbuObjIter.eType
189788**
189789** 0: Table does not exist (error)
189790** 1: Table has an implicit rowid.
189791** 2: Table has an explicit IPK column.
189792** 3: Table has an external PK index.
189793** 4: Table is WITHOUT ROWID.
189794** 5: Table is a virtual table.
189795*/
189796#define RBU_PK_NOTABLE 0
189797#define RBU_PK_NONE 1
189798#define RBU_PK_IPK 2
189799#define RBU_PK_EXTERNAL 3
189800#define RBU_PK_WITHOUT_ROWID 4
189801#define RBU_PK_VTAB 5
189802
189803
189804/*
189805** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
189806** one of the following operations.
189807*/
189808#define RBU_INSERT 1 /* Insert on a main table b-tree */
189809#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
189810#define RBU_REPLACE 3 /* Delete and then insert a row */
189811#define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
189812#define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
189813
189814#define RBU_UPDATE 6 /* Update a row in a main table b-tree */
189815
189816/*
189817** A single step of an incremental checkpoint - frame iWalFrame of the wal
189818** file should be copied to page iDbPage of the database file.
189819*/
189820struct RbuFrame {
189821 u32 iDbPage;
189822 u32 iWalFrame;
189823};
189824
189825/*
189826** RBU handle.
189827**
189828** nPhaseOneStep:
189829** If the RBU database contains an rbu_count table, this value is set to
189830** a running estimate of the number of b-tree operations required to
189831** finish populating the *-oal file. This allows the sqlite3_bp_progress()
189832** API to calculate the permyriadage progress of populating the *-oal file
189833** using the formula:
189834**
189835** permyriadage = (10000 * nProgress) / nPhaseOneStep
189836**
189837** nPhaseOneStep is initialized to the sum of:
189838**
189839** nRow * (nIndex + 1)
189840**
189841** for all source tables in the RBU database, where nRow is the number
189842** of rows in the source table and nIndex the number of indexes on the
189843** corresponding target database table.
189844**
189845** This estimate is accurate if the RBU update consists entirely of
189846** INSERT operations. However, it is inaccurate if:
189847**
189848** * the RBU update contains any UPDATE operations. If the PK specified
189849** for an UPDATE operation does not exist in the target table, then
189850** no b-tree operations are required on index b-trees. Or if the
189851** specified PK does exist, then (nIndex*2) such operations are
189852** required (one delete and one insert on each index b-tree).
189853**
189854** * the RBU update contains any DELETE operations for which the specified
189855** PK does not exist. In this case no operations are required on index
189856** b-trees.
189857**
189858** * the RBU update contains REPLACE operations. These are similar to
189859** UPDATE operations.
189860**
189861** nPhaseOneStep is updated to account for the conditions above during the
189862** first pass of each source table. The updated nPhaseOneStep value is
189863** stored in the rbu_state table if the RBU update is suspended.
189864*/
189865struct sqlite3rbu {
189866 int eStage; /* Value of RBU_STATE_STAGE field */
189867 sqlite3 *dbMain; /* target database handle */
189868 sqlite3 *dbRbu; /* rbu database handle */
189869 char *zTarget; /* Path to target db */
189870 char *zRbu; /* Path to rbu db */
189871 char *zState; /* Path to state db (or NULL if zRbu) */
189872 char zStateDb[5]; /* Db name for state ("stat" or "main") */
189873 int rc; /* Value returned by last rbu_step() call */
189874 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
189875 int nStep; /* Rows processed for current object */
189876 int nProgress; /* Rows processed for all objects */
189877 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
189878 const char *zVfsName; /* Name of automatically created rbu vfs */
189879 rbu_file *pTargetFd; /* File handle open on target db */
189880 int nPagePerSector; /* Pages per sector for pTargetFd */
189881 i64 iOalSz;
189882 i64 nPhaseOneStep;
189883
189884 /* The following state variables are used as part of the incremental
189885 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
189886 ** function rbuSetupCheckpoint() for details. */
189887 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
189888 u32 mLock;
189889 int nFrame; /* Entries in aFrame[] array */
189890 int nFrameAlloc; /* Allocated size of aFrame[] array */
189891 RbuFrame *aFrame;
189892 int pgsz;
189893 u8 *aBuf;
189894 i64 iWalCksum;
189895 i64 szTemp; /* Current size of all temp files in use */
189896 i64 szTempLimit; /* Total size limit for temp files */
189897
189898 /* Used in RBU vacuum mode only */
189899 int nRbu; /* Number of RBU VFS in the stack */
189900 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
189901};
189902
189903/*
189904** An rbu VFS is implemented using an instance of this structure.
189905**
189906** Variable pRbu is only non-NULL for automatically created RBU VFS objects.
189907** It is NULL for RBU VFS objects created explicitly using
189908** sqlite3rbu_create_vfs(). It is used to track the total amount of temp
189909** space used by the RBU handle.
189910*/
189911struct rbu_vfs {
189912 sqlite3_vfs base; /* rbu VFS shim methods */
189913 sqlite3_vfs *pRealVfs; /* Underlying VFS */
189914 sqlite3_mutex *mutex; /* Mutex to protect pMain */
189915 sqlite3rbu *pRbu; /* Owner RBU object */
189916 rbu_file *pMain; /* List of main db files */
189917 rbu_file *pMainRbu; /* List of main db files with pRbu!=0 */
189918};
189919
189920/*
189921** Each file opened by an rbu VFS is represented by an instance of
189922** the following structure.
189923**
189924** If this is a temporary file (pRbu!=0 && flags&DELETE_ON_CLOSE), variable
189925** "sz" is set to the current size of the database file.
189926*/
189927struct rbu_file {
189928 sqlite3_file base; /* sqlite3_file methods */
189929 sqlite3_file *pReal; /* Underlying file handle */
189930 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
189931 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
189932 i64 sz; /* Size of file in bytes (temp only) */
189933
189934 int openFlags; /* Flags this file was opened with */
189935 u32 iCookie; /* Cookie value for main db files */
189936 u8 iWriteVer; /* "write-version" value for main db files */
189937 u8 bNolock; /* True to fail EXCLUSIVE locks */
189938
189939 int nShm; /* Number of entries in apShm[] array */
189940 char **apShm; /* Array of mmap'd *-shm regions */
189941 char *zDel; /* Delete this when closing file */
189942
189943 const char *zWal; /* Wal filename for this main db file */
189944 rbu_file *pWalFd; /* Wal file descriptor for this main db */
189945 rbu_file *pMainNext; /* Next MAIN_DB file */
189946 rbu_file *pMainRbuNext; /* Next MAIN_DB file with pRbu!=0 */
189947};
189948
189949/*
189950** True for an RBU vacuum handle, or false otherwise.
189951*/
189952#define rbuIsVacuum(p) ((p)->zTarget==0)
189953
189954
189955/*************************************************************************
189956** The following three functions, found below:
189957**
189958** rbuDeltaGetInt()
189959** rbuDeltaChecksum()
189960** rbuDeltaApply()
189961**
189962** are lifted from the fossil source code (http://fossil-scm.org). They
189963** are used to implement the scalar SQL function rbu_fossil_delta().
189964*/
189965
189966/*
189967** Read bytes from *pz and convert them into a positive integer. When
189968** finished, leave *pz pointing to the first character past the end of
189969** the integer. The *pLen parameter holds the length of the string
189970** in *pz and is decremented once for each character in the integer.
189971*/
189972static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
189973 static const signed char zValue[] = {
189974 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
189975 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
189976 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
189977 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
189978 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
189979 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
189980 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
189981 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
189982 };
189983 unsigned int v = 0;
189984 int c;
189985 unsigned char *z = (unsigned char*)*pz;
189986 unsigned char *zStart = z;
189987 while( (c = zValue[0x7f&*(z++)])>=0 ){
189988 v = (v<<6) + c;
189989 }
189990 z--;
189991 *pLen -= z - zStart;
189992 *pz = (char*)z;
189993 return v;
189994}
189995
189996#if RBU_ENABLE_DELTA_CKSUM
189997/*
189998** Compute a 32-bit checksum on the N-byte buffer. Return the result.
189999*/
190000static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
190001 const unsigned char *z = (const unsigned char *)zIn;
190002 unsigned sum0 = 0;
190003 unsigned sum1 = 0;
190004 unsigned sum2 = 0;
190005 unsigned sum3 = 0;
190006 while(N >= 16){
190007 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
190008 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
190009 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
190010 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
190011 z += 16;
190012 N -= 16;
190013 }
190014 while(N >= 4){
190015 sum0 += z[0];
190016 sum1 += z[1];
190017 sum2 += z[2];
190018 sum3 += z[3];
190019 z += 4;
190020 N -= 4;
190021 }
190022 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
190023 switch(N){
190024 case 3: sum3 += (z[2] << 8);
190025 case 2: sum3 += (z[1] << 16);
190026 case 1: sum3 += (z[0] << 24);
190027 default: ;
190028 }
190029 return sum3;
190030}
190031#endif
190032
190033/*
190034** Apply a delta.
190035**
190036** The output buffer should be big enough to hold the whole output
190037** file and a NUL terminator at the end. The delta_output_size()
190038** routine will determine this size for you.
190039**
190040** The delta string should be null-terminated. But the delta string
190041** may contain embedded NUL characters (if the input and output are
190042** binary files) so we also have to pass in the length of the delta in
190043** the lenDelta parameter.
190044**
190045** This function returns the size of the output file in bytes (excluding
190046** the final NUL terminator character). Except, if the delta string is
190047** malformed or intended for use with a source file other than zSrc,
190048** then this routine returns -1.
190049**
190050** Refer to the delta_create() documentation above for a description
190051** of the delta file format.
190052*/
190053static int rbuDeltaApply(
190054 const char *zSrc, /* The source or pattern file */
190055 int lenSrc, /* Length of the source file */
190056 const char *zDelta, /* Delta to apply to the pattern */
190057 int lenDelta, /* Length of the delta */
190058 char *zOut /* Write the output into this preallocated buffer */
190059){
190060 unsigned int limit;
190061 unsigned int total = 0;
190062#if RBU_ENABLE_DELTA_CKSUM
190063 char *zOrigOut = zOut;
190064#endif
190065
190066 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
190067 if( *zDelta!='\n' ){
190068 /* ERROR: size integer not terminated by "\n" */
190069 return -1;
190070 }
190071 zDelta++; lenDelta--;
190072 while( *zDelta && lenDelta>0 ){
190073 unsigned int cnt, ofst;
190074 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
190075 switch( zDelta[0] ){
190076 case '@': {
190077 zDelta++; lenDelta--;
190078 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
190079 if( lenDelta>0 && zDelta[0]!=',' ){
190080 /* ERROR: copy command not terminated by ',' */
190081 return -1;
190082 }
190083 zDelta++; lenDelta--;
190084 total += cnt;
190085 if( total>limit ){
190086 /* ERROR: copy exceeds output file size */
190087 return -1;
190088 }
190089 if( (int)(ofst+cnt) > lenSrc ){
190090 /* ERROR: copy extends past end of input */
190091 return -1;
190092 }
190093 memcpy(zOut, &zSrc[ofst], cnt);
190094 zOut += cnt;
190095 break;
190096 }
190097 case ':': {
190098 zDelta++; lenDelta--;
190099 total += cnt;
190100 if( total>limit ){
190101 /* ERROR: insert command gives an output larger than predicted */
190102 return -1;
190103 }
190104 if( (int)cnt>lenDelta ){
190105 /* ERROR: insert count exceeds size of delta */
190106 return -1;
190107 }
190108 memcpy(zOut, zDelta, cnt);
190109 zOut += cnt;
190110 zDelta += cnt;
190111 lenDelta -= cnt;
190112 break;
190113 }
190114 case ';': {
190115 zDelta++; lenDelta--;
190116 zOut[0] = 0;
190117#if RBU_ENABLE_DELTA_CKSUM
190118 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
190119 /* ERROR: bad checksum */
190120 return -1;
190121 }
190122#endif
190123 if( total!=limit ){
190124 /* ERROR: generated size does not match predicted size */
190125 return -1;
190126 }
190127 return total;
190128 }
190129 default: {
190130 /* ERROR: unknown delta operator */
190131 return -1;
190132 }
190133 }
190134 }
190135 /* ERROR: unterminated delta */
190136 return -1;
190137}
190138
190139static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
190140 int size;
190141 size = rbuDeltaGetInt(&zDelta, &lenDelta);
190142 if( *zDelta!='\n' ){
190143 /* ERROR: size integer not terminated by "\n" */
190144 return -1;
190145 }
190146 return size;
190147}
190148
190149/*
190150** End of code taken from fossil.
190151*************************************************************************/
190152
190153/*
190154** Implementation of SQL scalar function rbu_fossil_delta().
190155**
190156** This function applies a fossil delta patch to a blob. Exactly two
190157** arguments must be passed to this function. The first is the blob to
190158** patch and the second the patch to apply. If no error occurs, this
190159** function returns the patched blob.
190160*/
190161static void rbuFossilDeltaFunc(
190162 sqlite3_context *context,
190163 int argc,
190164 sqlite3_value **argv
190165){
190166 const char *aDelta;
190167 int nDelta;
190168 const char *aOrig;
190169 int nOrig;
190170
190171 int nOut;
190172 int nOut2;
190173 char *aOut;
190174
190175 assert( argc==2 )((void) (0));
190176
190177 nOrig = sqlite3_value_bytes(argv[0]);
190178 aOrig = (const char*)sqlite3_value_blob(argv[0]);
190179 nDelta = sqlite3_value_bytes(argv[1]);
190180 aDelta = (const char*)sqlite3_value_blob(argv[1]);
190181
190182 /* Figure out the size of the output */
190183 nOut = rbuDeltaOutputSize(aDelta, nDelta);
190184 if( nOut<0 ){
190185 sqlite3_result_error(context, "corrupt fossil delta", -1);
190186 return;
190187 }
190188
190189 aOut = sqlite3_malloc(nOut+1);
190190 if( aOut==0 ){
190191 sqlite3_result_error_nomem(context);
190192 }else{
190193 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
190194 if( nOut2!=nOut ){
190195 sqlite3_free(aOut);
190196 sqlite3_result_error(context, "corrupt fossil delta", -1);
190197 }else{
190198 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
190199 }
190200 }
190201}
190202
190203
190204/*
190205** Prepare the SQL statement in buffer zSql against database handle db.
190206** If successful, set *ppStmt to point to the new statement and return
190207** SQLITE_OK.
190208**
190209** Otherwise, if an error does occur, set *ppStmt to NULL and return
190210** an SQLite error code. Additionally, set output variable *pzErrmsg to
190211** point to a buffer containing an error message. It is the responsibility
190212** of the caller to (eventually) free this buffer using sqlite3_free().
190213*/
190214static int prepareAndCollectError(
190215 sqlite3 *db,
190216 sqlite3_stmt **ppStmt,
190217 char **pzErrmsg,
190218 const char *zSql
190219){
190220 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
190221 if( rc!=SQLITE_OK0 ){
190222 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
190223 *ppStmt = 0;
190224 }
190225 return rc;
190226}
190227
190228/*
190229** Reset the SQL statement passed as the first argument. Return a copy
190230** of the value returned by sqlite3_reset().
190231**
190232** If an error has occurred, then set *pzErrmsg to point to a buffer
190233** containing an error message. It is the responsibility of the caller
190234** to eventually free this buffer using sqlite3_free().
190235*/
190236static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
190237 int rc = sqlite3_reset(pStmt);
190238 if( rc!=SQLITE_OK0 ){
190239 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
190240 }
190241 return rc;
190242}
190243
190244/*
190245** Unless it is NULL, argument zSql points to a buffer allocated using
190246** sqlite3_malloc containing an SQL statement. This function prepares the SQL
190247** statement against database db and frees the buffer. If statement
190248** compilation is successful, *ppStmt is set to point to the new statement
190249** handle and SQLITE_OK is returned.
190250**
190251** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
190252** returned. In this case, *pzErrmsg may also be set to point to an error
190253** message. It is the responsibility of the caller to free this error message
190254** buffer using sqlite3_free().
190255**
190256** If argument zSql is NULL, this function assumes that an OOM has occurred.
190257** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
190258*/
190259static int prepareFreeAndCollectError(
190260 sqlite3 *db,
190261 sqlite3_stmt **ppStmt,
190262 char **pzErrmsg,
190263 char *zSql
190264){
190265 int rc;
190266 assert( *pzErrmsg==0 )((void) (0));
190267 if( zSql==0 ){
190268 rc = SQLITE_NOMEM7;
190269 *ppStmt = 0;
190270 }else{
190271 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
190272 sqlite3_free(zSql);
190273 }
190274 return rc;
190275}
190276
190277/*
190278** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
190279** by an earlier call to rbuObjIterCacheTableInfo().
190280*/
190281static void rbuObjIterFreeCols(RbuObjIter *pIter){
190282 int i;
190283 for(i=0; i<pIter->nTblCol; i++){
190284 sqlite3_free(pIter->azTblCol[i]);
190285 sqlite3_free(pIter->azTblType[i]);
190286 }
190287 sqlite3_free(pIter->azTblCol);
190288 pIter->azTblCol = 0;
190289 pIter->azTblType = 0;
190290 pIter->aiSrcOrder = 0;
190291 pIter->abTblPk = 0;
190292 pIter->abNotNull = 0;
190293 pIter->nTblCol = 0;
190294 pIter->eType = 0; /* Invalid value */
190295}
190296
190297/*
190298** Finalize all statements and free all allocations that are specific to
190299** the current object (table/index pair).
190300*/
190301static void rbuObjIterClearStatements(RbuObjIter *pIter){
190302 RbuUpdateStmt *pUp;
190303
190304 sqlite3_finalize(pIter->pSelect);
190305 sqlite3_finalize(pIter->pInsert);
190306 sqlite3_finalize(pIter->pDelete);
190307 sqlite3_finalize(pIter->pTmpInsert);
190308 pUp = pIter->pRbuUpdate;
190309 while( pUp ){
190310 RbuUpdateStmt *pTmp = pUp->pNext;
190311 sqlite3_finalize(pUp->pUpdate);
190312 sqlite3_free(pUp);
190313 pUp = pTmp;
190314 }
190315
190316 pIter->pSelect = 0;
190317 pIter->pInsert = 0;
190318 pIter->pDelete = 0;
190319 pIter->pRbuUpdate = 0;
190320 pIter->pTmpInsert = 0;
190321 pIter->nCol = 0;
190322}
190323
190324/*
190325** Clean up any resources allocated as part of the iterator object passed
190326** as the only argument.
190327*/
190328static void rbuObjIterFinalize(RbuObjIter *pIter){
190329 rbuObjIterClearStatements(pIter);
190330 sqlite3_finalize(pIter->pTblIter);
190331 sqlite3_finalize(pIter->pIdxIter);
190332 rbuObjIterFreeCols(pIter);
190333 memset(pIter, 0, sizeof(RbuObjIter));
190334}
190335
190336/*
190337** Advance the iterator to the next position.
190338**
190339** If no error occurs, SQLITE_OK is returned and the iterator is left
190340** pointing to the next entry. Otherwise, an error code and message is
190341** left in the RBU handle passed as the first argument. A copy of the
190342** error code is returned.
190343*/
190344static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
190345 int rc = p->rc;
190346 if( rc==SQLITE_OK0 ){
190347
190348 /* Free any SQLite statements used while processing the previous object */
190349 rbuObjIterClearStatements(pIter);
190350 if( pIter->zIdx==0 ){
190351 rc = sqlite3_exec(p->dbMain,
190352 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
190353 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
190354 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
190355 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
190356 , 0, 0, &p->zErrmsg
190357 );
190358 }
190359
190360 if( rc==SQLITE_OK0 ){
190361 if( pIter->bCleanup ){
190362 rbuObjIterFreeCols(pIter);
190363 pIter->bCleanup = 0;
190364 rc = sqlite3_step(pIter->pTblIter);
190365 if( rc!=SQLITE_ROW100 ){
190366 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
190367 pIter->zTbl = 0;
190368 }else{
190369 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
190370 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
190371 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK0 : SQLITE_NOMEM7;
190372 }
190373 }else{
190374 if( pIter->zIdx==0 ){
190375 sqlite3_stmt *pIdx = pIter->pIdxIter;
190376 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
190377 }
190378 if( rc==SQLITE_OK0 ){
190379 rc = sqlite3_step(pIter->pIdxIter);
190380 if( rc!=SQLITE_ROW100 ){
190381 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
190382 pIter->bCleanup = 1;
190383 pIter->zIdx = 0;
190384 }else{
190385 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
190386 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
190387 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
190388 rc = pIter->zIdx ? SQLITE_OK0 : SQLITE_NOMEM7;
190389 }
190390 }
190391 }
190392 }
190393 }
190394
190395 if( rc!=SQLITE_OK0 ){
190396 rbuObjIterFinalize(pIter);
190397 p->rc = rc;
190398 }
190399 return rc;
190400}
190401
190402
190403/*
190404** The implementation of the rbu_target_name() SQL function. This function
190405** accepts one or two arguments. The first argument is the name of a table -
190406** the name of a table in the RBU database. The second, if it is present, is 1
190407** for a view or 0 for a table.
190408**
190409** For a non-vacuum RBU handle, if the table name matches the pattern:
190410**
190411** data[0-9]_<name>
190412**
190413** where <name> is any sequence of 1 or more characters, <name> is returned.
190414** Otherwise, if the only argument does not match the above pattern, an SQL
190415** NULL is returned.
190416**
190417** "data_t1" -> "t1"
190418** "data0123_t2" -> "t2"
190419** "dataAB_t3" -> NULL
190420**
190421** For an rbu vacuum handle, a copy of the first argument is returned if
190422** the second argument is either missing or 0 (not a view).
190423*/
190424static void rbuTargetNameFunc(
190425 sqlite3_context *pCtx,
190426 int argc,
190427 sqlite3_value **argv
190428){
190429 sqlite3rbu *p = sqlite3_user_data(pCtx);
190430 const char *zIn;
190431 assert( argc==1 || argc==2 )((void) (0));
190432
190433 zIn = (const char*)sqlite3_value_text(argv[0]);
190434 if( zIn ){
190435 if( rbuIsVacuum(p) ){
190436 assert( argc==2 )((void) (0));
190437 if( 0==sqlite3_value_int(argv[1]) ){
190438 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
190439 }
190440 }else{
190441 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
190442 int i;
190443 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
190444 if( zIn[i]=='_' && zIn[i+1] ){
190445 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC((sqlite3_destructor_type)0));
190446 }
190447 }
190448 }
190449 }
190450}
190451
190452/*
190453** Initialize the iterator structure passed as the second argument.
190454**
190455** If no error occurs, SQLITE_OK is returned and the iterator is left
190456** pointing to the first entry. Otherwise, an error code and message is
190457** left in the RBU handle passed as the first argument. A copy of the
190458** error code is returned.
190459*/
190460static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
190461 int rc;
190462 memset(pIter, 0, sizeof(RbuObjIter));
190463
190464 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
190465 sqlite3_mprintf(
190466 "SELECT rbu_target_name(name, type='view') AS target, name "
190467 "FROM sqlite_master "
190468 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
190469 " %s "
190470 "ORDER BY name"
190471 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
190472
190473 if( rc==SQLITE_OK0 ){
190474 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
190475 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
190476 " FROM main.sqlite_master "
190477 " WHERE type='index' AND tbl_name = ?"
190478 );
190479 }
190480
190481 pIter->bCleanup = 1;
190482 p->rc = rc;
190483 return rbuObjIterNext(p, pIter);
190484}
190485
190486/*
190487** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
190488** an error code is stored in the RBU handle passed as the first argument.
190489**
190490** If an error has already occurred (p->rc is already set to something other
190491** than SQLITE_OK), then this function returns NULL without modifying the
190492** stored error code. In this case it still calls sqlite3_free() on any
190493** printf() parameters associated with %z conversions.
190494*/
190495static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
190496 char *zSql = 0;
190497 va_list ap;
190498 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
190499 zSql = sqlite3_vmprintf(zFmt, ap);
190500 if( p->rc==SQLITE_OK0 ){
190501 if( zSql==0 ) p->rc = SQLITE_NOMEM7;
190502 }else{
190503 sqlite3_free(zSql);
190504 zSql = 0;
190505 }
190506 va_end(ap)__builtin_va_end(ap);
190507 return zSql;
190508}
190509
190510/*
190511** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
190512** arguments are the usual subsitution values. This function performs
190513** the printf() style substitutions and executes the result as an SQL
190514** statement on the RBU handles database.
190515**
190516** If an error occurs, an error code and error message is stored in the
190517** RBU handle. If an error has already occurred when this function is
190518** called, it is a no-op.
190519*/
190520static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
190521 va_list ap;
190522 char *zSql;
190523 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
190524 zSql = sqlite3_vmprintf(zFmt, ap);
190525 if( p->rc==SQLITE_OK0 ){
190526 if( zSql==0 ){
190527 p->rc = SQLITE_NOMEM7;
190528 }else{
190529 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
190530 }
190531 }
190532 sqlite3_free(zSql);
190533 va_end(ap)__builtin_va_end(ap);
190534 return p->rc;
190535}
190536
190537/*
190538** Attempt to allocate and return a pointer to a zeroed block of nByte
190539** bytes.
190540**
190541** If an error (i.e. an OOM condition) occurs, return NULL and leave an
190542** error code in the rbu handle passed as the first argument. Or, if an
190543** error has already occurred when this function is called, return NULL
190544** immediately without attempting the allocation or modifying the stored
190545** error code.
190546*/
190547static void *rbuMalloc(sqlite3rbu *p, sqlite3_int64 nByte){
190548 void *pRet = 0;
190549 if( p->rc==SQLITE_OK0 ){
190550 assert( nByte>0 )((void) (0));
190551 pRet = sqlite3_malloc64(nByte);
190552 if( pRet==0 ){
190553 p->rc = SQLITE_NOMEM7;
190554 }else{
190555 memset(pRet, 0, nByte);
190556 }
190557 }
190558 return pRet;
190559}
190560
190561
190562/*
190563** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
190564** there is room for at least nCol elements. If an OOM occurs, store an
190565** error code in the RBU handle passed as the first argument.
190566*/
190567static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
190568 sqlite3_int64 nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
190569 char **azNew;
190570
190571 azNew = (char**)rbuMalloc(p, nByte);
190572 if( azNew ){
190573 pIter->azTblCol = azNew;
190574 pIter->azTblType = &azNew[nCol];
190575 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
190576 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
190577 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
190578 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
190579 }
190580}
190581
190582/*
190583** The first argument must be a nul-terminated string. This function
190584** returns a copy of the string in memory obtained from sqlite3_malloc().
190585** It is the responsibility of the caller to eventually free this memory
190586** using sqlite3_free().
190587**
190588** If an OOM condition is encountered when attempting to allocate memory,
190589** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
190590** if the allocation succeeds, (*pRc) is left unchanged.
190591*/
190592static char *rbuStrndup(const char *zStr, int *pRc){
190593 char *zRet = 0;
190594
190595 assert( *pRc==SQLITE_OK )((void) (0));
190596 if( zStr ){
190597 size_t nCopy = strlen(zStr) + 1;
190598 zRet = (char*)sqlite3_malloc64(nCopy);
190599 if( zRet ){
190600 memcpy(zRet, zStr, nCopy);
190601 }else{
190602 *pRc = SQLITE_NOMEM7;
190603 }
190604 }
190605
190606 return zRet;
190607}
190608
190609/*
190610** Finalize the statement passed as the second argument.
190611**
190612** If the sqlite3_finalize() call indicates that an error occurs, and the
190613** rbu handle error code is not already set, set the error code and error
190614** message accordingly.
190615*/
190616static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
190617 sqlite3 *db = sqlite3_db_handle(pStmt);
190618 int rc = sqlite3_finalize(pStmt);
190619 if( p->rc==SQLITE_OK0 && rc!=SQLITE_OK0 ){
190620 p->rc = rc;
190621 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
190622 }
190623}
190624
190625/* Determine the type of a table.
190626**
190627** peType is of type (int*), a pointer to an output parameter of type
190628** (int). This call sets the output parameter as follows, depending
190629** on the type of the table specified by parameters dbName and zTbl.
190630**
190631** RBU_PK_NOTABLE: No such table.
190632** RBU_PK_NONE: Table has an implicit rowid.
190633** RBU_PK_IPK: Table has an explicit IPK column.
190634** RBU_PK_EXTERNAL: Table has an external PK index.
190635** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
190636** RBU_PK_VTAB: Table is a virtual table.
190637**
190638** Argument *piPk is also of type (int*), and also points to an output
190639** parameter. Unless the table has an external primary key index
190640** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
190641** if the table does have an external primary key index, then *piPk
190642** is set to the root page number of the primary key index before
190643** returning.
190644**
190645** ALGORITHM:
190646**
190647** if( no entry exists in sqlite_master ){
190648** return RBU_PK_NOTABLE
190649** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
190650** return RBU_PK_VTAB
190651** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
190652** if( the index that is the pk exists in sqlite_master ){
190653** *piPK = rootpage of that index.
190654** return RBU_PK_EXTERNAL
190655** }else{
190656** return RBU_PK_WITHOUT_ROWID
190657** }
190658** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
190659** return RBU_PK_IPK
190660** }else{
190661** return RBU_PK_NONE
190662** }
190663*/
190664static void rbuTableType(
190665 sqlite3rbu *p,
190666 const char *zTab,
190667 int *peType,
190668 int *piTnum,
190669 int *piPk
190670){
190671 /*
190672 ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
190673 ** 1) PRAGMA index_list = ?
190674 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
190675 ** 3) PRAGMA table_info = ?
190676 */
190677 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
190678
190679 *peType = RBU_PK_NOTABLE;
190680 *piPk = 0;
190681
190682 assert( p->rc==SQLITE_OK )((void) (0));
190683 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
190684 sqlite3_mprintf(
190685 "SELECT (sql LIKE 'create virtual%%'), rootpage"
190686 " FROM sqlite_master"
190687 " WHERE name=%Q", zTab
190688 ));
190689 if( p->rc!=SQLITE_OK0 || sqlite3_step(aStmt[0])!=SQLITE_ROW100 ){
190690 /* Either an error, or no such table. */
190691 goto rbuTableType_end;
190692 }
190693 if( sqlite3_column_int(aStmt[0], 0) ){
190694 *peType = RBU_PK_VTAB; /* virtual table */
190695 goto rbuTableType_end;
190696 }
190697 *piTnum = sqlite3_column_int(aStmt[0], 1);
190698
190699 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
190700 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
190701 );
190702 if( p->rc ) goto rbuTableType_end;
190703 while( sqlite3_step(aStmt[1])==SQLITE_ROW100 ){
190704 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
190705 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
190706 if( zOrig && zIdx && zOrig[0]=='p' ){
190707 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
190708 sqlite3_mprintf(
190709 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
190710 ));
190711 if( p->rc==SQLITE_OK0 ){
190712 if( sqlite3_step(aStmt[2])==SQLITE_ROW100 ){
190713 *piPk = sqlite3_column_int(aStmt[2], 0);
190714 *peType = RBU_PK_EXTERNAL;
190715 }else{
190716 *peType = RBU_PK_WITHOUT_ROWID;
190717 }
190718 }
190719 goto rbuTableType_end;
190720 }
190721 }
190722
190723 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
190724 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
190725 );
190726 if( p->rc==SQLITE_OK0 ){
190727 while( sqlite3_step(aStmt[3])==SQLITE_ROW100 ){
190728 if( sqlite3_column_int(aStmt[3],5)>0 ){
190729 *peType = RBU_PK_IPK; /* explicit IPK column */
190730 goto rbuTableType_end;
190731 }
190732 }
190733 *peType = RBU_PK_NONE;
190734 }
190735
190736rbuTableType_end: {
190737 unsigned int i;
190738 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
190739 rbuFinalize(p, aStmt[i]);
190740 }
190741 }
190742}
190743
190744/*
190745** This is a helper function for rbuObjIterCacheTableInfo(). It populates
190746** the pIter->abIndexed[] array.
190747*/
190748static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
190749 sqlite3_stmt *pList = 0;
190750 int bIndex = 0;
190751
190752 if( p->rc==SQLITE_OK0 ){
190753 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
190754 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
190755 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
190756 );
190757 }
190758
190759 pIter->nIndex = 0;
190760 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pList) ){
190761 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
190762 int bPartial = sqlite3_column_int(pList, 4);
190763 sqlite3_stmt *pXInfo = 0;
190764 if( zIdx==0 ) break;
190765 if( bPartial ){
190766 memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
190767 }
190768 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
190769 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
190770 );
190771 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
190772 int iCid = sqlite3_column_int(pXInfo, 1);
190773 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
190774 }
190775 rbuFinalize(p, pXInfo);
190776 bIndex = 1;
190777 pIter->nIndex++;
190778 }
190779
190780 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
190781 /* "PRAGMA index_list" includes the main PK b-tree */
190782 pIter->nIndex--;
190783 }
190784
190785 rbuFinalize(p, pList);
190786 if( bIndex==0 ) pIter->abIndexed = 0;
190787}
190788
190789
190790/*
190791** If they are not already populated, populate the pIter->azTblCol[],
190792** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
190793** the table (not index) that the iterator currently points to.
190794**
190795** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
190796** an error does occur, an error code and error message are also left in
190797** the RBU handle.
190798*/
190799static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
190800 if( pIter->azTblCol==0 ){
190801 sqlite3_stmt *pStmt = 0;
190802 int nCol = 0;
190803 int i; /* for() loop iterator variable */
190804 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
190805 int iOrder = 0;
190806 int iTnum = 0;
190807
190808 /* Figure out the type of table this step will deal with. */
190809 assert( pIter->eType==0 )((void) (0));
190810 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
190811 if( p->rc==SQLITE_OK0 && pIter->eType==RBU_PK_NOTABLE ){
190812 p->rc = SQLITE_ERROR1;
190813 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
190814 }
190815 if( p->rc ) return p->rc;
190816 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
190817
190818 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK((void) (0))
190819 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID((void) (0))
190820 || pIter->eType==RBU_PK_VTAB((void) (0))
190821 )((void) (0));
190822
190823 /* Populate the azTblCol[] and nTblCol variables based on the columns
190824 ** of the input table. Ignore any input table columns that begin with
190825 ** "rbu_". */
190826 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
190827 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
190828 );
190829 if( p->rc==SQLITE_OK0 ){
190830 nCol = sqlite3_column_count(pStmt);
190831 rbuAllocateIterArrays(p, pIter, nCol);
190832 }
190833 for(i=0; p->rc==SQLITE_OK0 && i<nCol; i++){
190834 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
190835 if( sqlite3_strnicmp("rbu_", zName, 4) ){
190836 char *zCopy = rbuStrndup(zName, &p->rc);
190837 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
190838 pIter->azTblCol[pIter->nTblCol++] = zCopy;
190839 }
190840 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
190841 bRbuRowid = 1;
190842 }
190843 }
190844 sqlite3_finalize(pStmt);
190845 pStmt = 0;
190846
190847 if( p->rc==SQLITE_OK0
190848 && rbuIsVacuum(p)==0
190849 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
190850 ){
190851 p->rc = SQLITE_ERROR1;
190852 p->zErrmsg = sqlite3_mprintf(
190853 "table %q %s rbu_rowid column", pIter->zDataTbl,
190854 (bRbuRowid ? "may not have" : "requires")
190855 );
190856 }
190857
190858 /* Check that all non-HIDDEN columns in the destination table are also
190859 ** present in the input table. Populate the abTblPk[], azTblType[] and
190860 ** aiTblOrder[] arrays at the same time. */
190861 if( p->rc==SQLITE_OK0 ){
190862 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
190863 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
190864 );
190865 }
190866 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
190867 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
190868 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
190869 for(i=iOrder; i<pIter->nTblCol; i++){
190870 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
190871 }
190872 if( i==pIter->nTblCol ){
190873 p->rc = SQLITE_ERROR1;
190874 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
190875 pIter->zDataTbl, zName
190876 );
190877 }else{
190878 int iPk = sqlite3_column_int(pStmt, 5);
190879 int bNotNull = sqlite3_column_int(pStmt, 3);
190880 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
190881
190882 if( i!=iOrder ){
190883 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]){int t=pIter->aiSrcOrder[i]; pIter->aiSrcOrder[i]=pIter
->aiSrcOrder[iOrder]; pIter->aiSrcOrder[iOrder]=t;}
;
190884 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]){char* t=pIter->azTblCol[i]; pIter->azTblCol[i]=pIter->
azTblCol[iOrder]; pIter->azTblCol[iOrder]=t;}
;
190885 }
190886
190887 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
190888 assert( iPk>=0 )((void) (0));
190889 pIter->abTblPk[iOrder] = (u8)iPk;
190890 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
190891 iOrder++;
190892 }
190893 }
190894
190895 rbuFinalize(p, pStmt);
190896 rbuObjIterCacheIndexedCols(p, pIter);
190897 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 )((void) (0));
190898 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 )((void) (0));
190899 }
190900
190901 return p->rc;
190902}
190903
190904/*
190905** This function constructs and returns a pointer to a nul-terminated
190906** string containing some SQL clause or list based on one or more of the
190907** column names currently stored in the pIter->azTblCol[] array.
190908*/
190909static char *rbuObjIterGetCollist(
190910 sqlite3rbu *p, /* RBU object */
190911 RbuObjIter *pIter /* Object iterator for column names */
190912){
190913 char *zList = 0;
190914 const char *zSep = "";
190915 int i;
190916 for(i=0; i<pIter->nTblCol; i++){
190917 const char *z = pIter->azTblCol[i];
190918 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
190919 zSep = ", ";
190920 }
190921 return zList;
190922}
190923
190924/*
190925** Return a comma separated list of the quoted PRIMARY KEY column names,
190926** in order, for the current table. Before each column name, add the text
190927** zPre. After each column name, add the zPost text. Use zSeparator as
190928** the separator text (usually ", ").
190929*/
190930static char *rbuObjIterGetPkList(
190931 sqlite3rbu *p, /* RBU object */
190932 RbuObjIter *pIter, /* Object iterator for column names */
190933 const char *zPre, /* Before each quoted column name */
190934 const char *zSeparator, /* Separator to use between columns */
190935 const char *zPost /* After each quoted column name */
190936){
190937 int iPk = 1;
190938 char *zRet = 0;
190939 const char *zSep = "";
190940 while( 1 ){
190941 int i;
190942 for(i=0; i<pIter->nTblCol; i++){
190943 if( (int)pIter->abTblPk[i]==iPk ){
190944 const char *zCol = pIter->azTblCol[i];
190945 zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
190946 zSep = zSeparator;
190947 break;
190948 }
190949 }
190950 if( i==pIter->nTblCol ) break;
190951 iPk++;
190952 }
190953 return zRet;
190954}
190955
190956/*
190957** This function is called as part of restarting an RBU vacuum within
190958** stage 1 of the process (while the *-oal file is being built) while
190959** updating a table (not an index). The table may be a rowid table or
190960** a WITHOUT ROWID table. It queries the target database to find the
190961** largest key that has already been written to the target table and
190962** constructs a WHERE clause that can be used to extract the remaining
190963** rows from the source table. For a rowid table, the WHERE clause
190964** is of the form:
190965**
190966** "WHERE _rowid_ > ?"
190967**
190968** and for WITHOUT ROWID tables:
190969**
190970** "WHERE (key1, key2) > (?, ?)"
190971**
190972** Instead of "?" placeholders, the actual WHERE clauses created by
190973** this function contain literal SQL values.
190974*/
190975static char *rbuVacuumTableStart(
190976 sqlite3rbu *p, /* RBU handle */
190977 RbuObjIter *pIter, /* RBU iterator object */
190978 int bRowid, /* True for a rowid table */
190979 const char *zWrite /* Target table name prefix */
190980){
190981 sqlite3_stmt *pMax = 0;
190982 char *zRet = 0;
190983 if( bRowid ){
190984 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
190985 sqlite3_mprintf(
190986 "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
190987 )
190988 );
190989 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pMax) ){
190990 sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
190991 zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
190992 }
190993 rbuFinalize(p, pMax);
190994 }else{
190995 char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
190996 char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
190997 char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
190998
190999 if( p->rc==SQLITE_OK0 ){
191000 p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg,
191001 sqlite3_mprintf(
191002 "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1",
191003 zSelect, zWrite, pIter->zTbl, zOrder
191004 )
191005 );
191006 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pMax) ){
191007 const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
191008 zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
191009 }
191010 rbuFinalize(p, pMax);
191011 }
191012
191013 sqlite3_free(zOrder);
191014 sqlite3_free(zSelect);
191015 sqlite3_free(zList);
191016 }
191017 return zRet;
191018}
191019
191020/*
191021** This function is called as part of restating an RBU vacuum when the
191022** current operation is writing content to an index. If possible, it
191023** queries the target index b-tree for the largest key already written to
191024** it, then composes and returns an expression that can be used in a WHERE
191025** clause to select the remaining required rows from the source table.
191026** It is only possible to return such an expression if:
191027**
191028** * The index contains no DESC columns, and
191029** * The last key written to the index before the operation was
191030** suspended does not contain any NULL values.
191031**
191032** The expression is of the form:
191033**
191034** (index-field1, index-field2, ...) > (?, ?, ...)
191035**
191036** except that the "?" placeholders are replaced with literal values.
191037**
191038** If the expression cannot be created, NULL is returned. In this case,
191039** the caller has to use an OFFSET clause to extract only the required
191040** rows from the sourct table, just as it does for an RBU update operation.
191041*/
191042char *rbuVacuumIndexStart(
191043 sqlite3rbu *p, /* RBU handle */
191044 RbuObjIter *pIter /* RBU iterator object */
191045){
191046 char *zOrder = 0;
191047 char *zLhs = 0;
191048 char *zSelect = 0;
191049 char *zVector = 0;
191050 char *zRet = 0;
191051 int bFailed = 0;
191052 const char *zSep = "";
191053 int iCol = 0;
191054 sqlite3_stmt *pXInfo = 0;
191055
191056 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
191057 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
191058 );
191059 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
191060 int iCid = sqlite3_column_int(pXInfo, 1);
191061 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
191062 const char *zCol;
191063 if( sqlite3_column_int(pXInfo, 3) ){
191064 bFailed = 1;
191065 break;
191066 }
191067
191068 if( iCid<0 ){
191069 if( pIter->eType==RBU_PK_IPK ){
191070 int i;
191071 for(i=0; pIter->abTblPk[i]==0; i++);
191072 assert( i<pIter->nTblCol )((void) (0));
191073 zCol = pIter->azTblCol[i];
191074 }else{
191075 zCol = "_rowid_";
191076 }
191077 }else{
191078 zCol = pIter->azTblCol[iCid];
191079 }
191080
191081 zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
191082 zLhs, zSep, zCol, zCollate
191083 );
191084 zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
191085 zOrder, zSep, iCol, zCol, zCollate
191086 );
191087 zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
191088 zSelect, zSep, iCol, zCol
191089 );
191090 zSep = ", ";
191091 iCol++;
191092 }
191093 rbuFinalize(p, pXInfo);
191094 if( bFailed ) goto index_start_out;
191095
191096 if( p->rc==SQLITE_OK0 ){
191097 sqlite3_stmt *pSel = 0;
191098
191099 p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
191100 sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
191101 zSelect, pIter->zTbl, zOrder
191102 )
191103 );
191104 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pSel) ){
191105 zSep = "";
191106 for(iCol=0; iCol<pIter->nCol; iCol++){
191107 const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
191108 if( zQuoted[0]=='N' ){
191109 bFailed = 1;
191110 break;
191111 }
191112 zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
191113 zSep = ", ";
191114 }
191115
191116 if( !bFailed ){
191117 zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
191118 }
191119 }
191120 rbuFinalize(p, pSel);
191121 }
191122
191123 index_start_out:
191124 sqlite3_free(zOrder);
191125 sqlite3_free(zSelect);
191126 sqlite3_free(zVector);
191127 sqlite3_free(zLhs);
191128 return zRet;
191129}
191130
191131/*
191132** This function is used to create a SELECT list (the list of SQL
191133** expressions that follows a SELECT keyword) for a SELECT statement
191134** used to read from an data_xxx or rbu_tmp_xxx table while updating the
191135** index object currently indicated by the iterator object passed as the
191136** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
191137** to obtain the required information.
191138**
191139** If the index is of the following form:
191140**
191141** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
191142**
191143** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
191144** "ipk", the returned string is:
191145**
191146** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
191147**
191148** As well as the returned string, three other malloc'd strings are
191149** returned via output parameters. As follows:
191150**
191151** pzImposterCols: ...
191152** pzImposterPk: ...
191153** pzWhere: ...
191154*/
191155static char *rbuObjIterGetIndexCols(
191156 sqlite3rbu *p, /* RBU object */
191157 RbuObjIter *pIter, /* Object iterator for column names */
191158 char **pzImposterCols, /* OUT: Columns for imposter table */
191159 char **pzImposterPk, /* OUT: Imposter PK clause */
191160 char **pzWhere, /* OUT: WHERE clause */
191161 int *pnBind /* OUT: Trbul number of columns */
191162){
191163 int rc = p->rc; /* Error code */
191164 int rc2; /* sqlite3_finalize() return code */
191165 char *zRet = 0; /* String to return */
191166 char *zImpCols = 0; /* String to return via *pzImposterCols */
191167 char *zImpPK = 0; /* String to return via *pzImposterPK */
191168 char *zWhere = 0; /* String to return via *pzWhere */
191169 int nBind = 0; /* Value to return via *pnBind */
191170 const char *zCom = ""; /* Set to ", " later on */
191171 const char *zAnd = ""; /* Set to " AND " later on */
191172 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
191173
191174 if( rc==SQLITE_OK0 ){
191175 assert( p->zErrmsg==0 )((void) (0));
191176 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
191177 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
191178 );
191179 }
191180
191181 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
191182 int iCid = sqlite3_column_int(pXInfo, 1);
191183 int bDesc = sqlite3_column_int(pXInfo, 3);
191184 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
191185 const char *zCol;
191186 const char *zType;
191187
191188 if( iCid<0 ){
191189 /* An integer primary key. If the table has an explicit IPK, use
191190 ** its name. Otherwise, use "rbu_rowid". */
191191 if( pIter->eType==RBU_PK_IPK ){
191192 int i;
191193 for(i=0; pIter->abTblPk[i]==0; i++);
191194 assert( i<pIter->nTblCol )((void) (0));
191195 zCol = pIter->azTblCol[i];
191196 }else if( rbuIsVacuum(p) ){
191197 zCol = "_rowid_";
191198 }else{
191199 zCol = "rbu_rowid";
191200 }
191201 zType = "INTEGER";
191202 }else{
191203 zCol = pIter->azTblCol[iCid];
191204 zType = pIter->azTblType[iCid];
191205 }
191206
191207 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
191208 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
191209 const char *zOrder = (bDesc ? " DESC" : "");
191210 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
191211 zImpPK, zCom, nBind, zCol, zOrder
191212 );
191213 }
191214 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
191215 zImpCols, zCom, nBind, zCol, zType, zCollate
191216 );
191217 zWhere = sqlite3_mprintf(
191218 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
191219 );
191220 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM7;
191221 zCom = ", ";
191222 zAnd = " AND ";
191223 nBind++;
191224 }
191225
191226 rc2 = sqlite3_finalize(pXInfo);
191227 if( rc==SQLITE_OK0 ) rc = rc2;
191228
191229 if( rc!=SQLITE_OK0 ){
191230 sqlite3_free(zRet);
191231 sqlite3_free(zImpCols);
191232 sqlite3_free(zImpPK);
191233 sqlite3_free(zWhere);
191234 zRet = 0;
191235 zImpCols = 0;
191236 zImpPK = 0;
191237 zWhere = 0;
191238 p->rc = rc;
191239 }
191240
191241 *pzImposterCols = zImpCols;
191242 *pzImposterPk = zImpPK;
191243 *pzWhere = zWhere;
191244 *pnBind = nBind;
191245 return zRet;
191246}
191247
191248/*
191249** Assuming the current table columns are "a", "b" and "c", and the zObj
191250** paramter is passed "old", return a string of the form:
191251**
191252** "old.a, old.b, old.b"
191253**
191254** With the column names escaped.
191255**
191256** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
191257** the text ", old._rowid_" to the returned value.
191258*/
191259static char *rbuObjIterGetOldlist(
191260 sqlite3rbu *p,
191261 RbuObjIter *pIter,
191262 const char *zObj
191263){
191264 char *zList = 0;
191265 if( p->rc==SQLITE_OK0 && pIter->abIndexed ){
191266 const char *zS = "";
191267 int i;
191268 for(i=0; i<pIter->nTblCol; i++){
191269 if( pIter->abIndexed[i] ){
191270 const char *zCol = pIter->azTblCol[i];
191271 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
191272 }else{
191273 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
191274 }
191275 zS = ", ";
191276 if( zList==0 ){
191277 p->rc = SQLITE_NOMEM7;
191278 break;
191279 }
191280 }
191281
191282 /* For a table with implicit rowids, append "old._rowid_" to the list. */
191283 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
191284 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
191285 }
191286 }
191287 return zList;
191288}
191289
191290/*
191291** Return an expression that can be used in a WHERE clause to match the
191292** primary key of the current table. For example, if the table is:
191293**
191294** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
191295**
191296** Return the string:
191297**
191298** "b = ?1 AND c = ?2"
191299*/
191300static char *rbuObjIterGetWhere(
191301 sqlite3rbu *p,
191302 RbuObjIter *pIter
191303){
191304 char *zList = 0;
191305 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
191306 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
191307 }else if( pIter->eType==RBU_PK_EXTERNAL ){
191308 const char *zSep = "";
191309 int i;
191310 for(i=0; i<pIter->nTblCol; i++){
191311 if( pIter->abTblPk[i] ){
191312 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
191313 zSep = " AND ";
191314 }
191315 }
191316 zList = rbuMPrintf(p,
191317 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
191318 );
191319
191320 }else{
191321 const char *zSep = "";
191322 int i;
191323 for(i=0; i<pIter->nTblCol; i++){
191324 if( pIter->abTblPk[i] ){
191325 const char *zCol = pIter->azTblCol[i];
191326 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
191327 zSep = " AND ";
191328 }
191329 }
191330 }
191331 return zList;
191332}
191333
191334/*
191335** The SELECT statement iterating through the keys for the current object
191336** (p->objiter.pSelect) currently points to a valid row. However, there
191337** is something wrong with the rbu_control value in the rbu_control value
191338** stored in the (p->nCol+1)'th column. Set the error code and error message
191339** of the RBU handle to something reflecting this.
191340*/
191341static void rbuBadControlError(sqlite3rbu *p){
191342 p->rc = SQLITE_ERROR1;
191343 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
191344}
191345
191346
191347/*
191348** Return a nul-terminated string containing the comma separated list of
191349** assignments that should be included following the "SET" keyword of
191350** an UPDATE statement used to update the table object that the iterator
191351** passed as the second argument currently points to if the rbu_control
191352** column of the data_xxx table entry is set to zMask.
191353**
191354** The memory for the returned string is obtained from sqlite3_malloc().
191355** It is the responsibility of the caller to eventually free it using
191356** sqlite3_free().
191357**
191358** If an OOM error is encountered when allocating space for the new
191359** string, an error code is left in the rbu handle passed as the first
191360** argument and NULL is returned. Or, if an error has already occurred
191361** when this function is called, NULL is returned immediately, without
191362** attempting the allocation or modifying the stored error code.
191363*/
191364static char *rbuObjIterGetSetlist(
191365 sqlite3rbu *p,
191366 RbuObjIter *pIter,
191367 const char *zMask
191368){
191369 char *zList = 0;
191370 if( p->rc==SQLITE_OK0 ){
191371 int i;
191372
191373 if( (int)strlen(zMask)!=pIter->nTblCol ){
191374 rbuBadControlError(p);
191375 }else{
191376 const char *zSep = "";
191377 for(i=0; i<pIter->nTblCol; i++){
191378 char c = zMask[pIter->aiSrcOrder[i]];
191379 if( c=='x' ){
191380 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
191381 zList, zSep, pIter->azTblCol[i], i+1
191382 );
191383 zSep = ", ";
191384 }
191385 else if( c=='d' ){
191386 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
191387 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
191388 );
191389 zSep = ", ";
191390 }
191391 else if( c=='f' ){
191392 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
191393 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
191394 );
191395 zSep = ", ";
191396 }
191397 }
191398 }
191399 }
191400 return zList;
191401}
191402
191403/*
191404** Return a nul-terminated string consisting of nByte comma separated
191405** "?" expressions. For example, if nByte is 3, return a pointer to
191406** a buffer containing the string "?,?,?".
191407**
191408** The memory for the returned string is obtained from sqlite3_malloc().
191409** It is the responsibility of the caller to eventually free it using
191410** sqlite3_free().
191411**
191412** If an OOM error is encountered when allocating space for the new
191413** string, an error code is left in the rbu handle passed as the first
191414** argument and NULL is returned. Or, if an error has already occurred
191415** when this function is called, NULL is returned immediately, without
191416** attempting the allocation or modifying the stored error code.
191417*/
191418static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
191419 char *zRet = 0;
191420 sqlite3_int64 nByte = 2*(sqlite3_int64)nBind + 1;
191421
191422 zRet = (char*)rbuMalloc(p, nByte);
191423 if( zRet ){
191424 int i;
191425 for(i=0; i<nBind; i++){
191426 zRet[i*2] = '?';
191427 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
191428 }
191429 }
191430 return zRet;
191431}
191432
191433/*
191434** The iterator currently points to a table (not index) of type
191435** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
191436** declaration for the corresponding imposter table. For example,
191437** if the iterator points to a table created as:
191438**
191439** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
191440**
191441** this function returns:
191442**
191443** PRIMARY KEY("b", "a" DESC)
191444*/
191445static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
191446 char *z = 0;
191447 assert( pIter->zIdx==0 )((void) (0));
191448 if( p->rc==SQLITE_OK0 ){
191449 const char *zSep = "PRIMARY KEY(";
191450 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
191451 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
191452
191453 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
191454 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
191455 );
191456 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXList) ){
191457 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
191458 if( zOrig && strcmp(zOrig, "pk")==0 ){
191459 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
191460 if( zIdx ){
191461 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
191462 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
191463 );
191464 }
191465 break;
191466 }
191467 }
191468 rbuFinalize(p, pXList);
191469
191470 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
191471 if( sqlite3_column_int(pXInfo, 5) ){
191472 /* int iCid = sqlite3_column_int(pXInfo, 0); */
191473 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
191474 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
191475 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
191476 zSep = ", ";
191477 }
191478 }
191479 z = rbuMPrintf(p, "%z)", z);
191480 rbuFinalize(p, pXInfo);
191481 }
191482 return z;
191483}
191484
191485/*
191486** This function creates the second imposter table used when writing to
191487** a table b-tree where the table has an external primary key. If the
191488** iterator passed as the second argument does not currently point to
191489** a table (not index) with an external primary key, this function is a
191490** no-op.
191491**
191492** Assuming the iterator does point to a table with an external PK, this
191493** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
191494** used to access that PK index. For example, if the target table is
191495** declared as follows:
191496**
191497** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
191498**
191499** then the imposter table schema is:
191500**
191501** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
191502**
191503*/
191504static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
191505 if( p->rc==SQLITE_OK0 && pIter->eType==RBU_PK_EXTERNAL ){
191506 int tnum = pIter->iPkTnum; /* Root page of PK index */
191507 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
191508 const char *zIdx = 0; /* Name of PK index */
191509 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
191510 const char *zComma = "";
191511 char *zCols = 0; /* Used to build up list of table cols */
191512 char *zPk = 0; /* Used to build up table PK declaration */
191513
191514 /* Figure out the name of the primary key index for the current table.
191515 ** This is needed for the argument to "PRAGMA index_xinfo". Set
191516 ** zIdx to point to a nul-terminated string containing this name. */
191517 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
191518 "SELECT name FROM sqlite_master WHERE rootpage = ?"
191519 );
191520 if( p->rc==SQLITE_OK0 ){
191521 sqlite3_bind_int(pQuery, 1, tnum);
191522 if( SQLITE_ROW100==sqlite3_step(pQuery) ){
191523 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
191524 }
191525 }
191526 if( zIdx ){
191527 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
191528 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
191529 );
191530 }
191531 rbuFinalize(p, pQuery);
191532
191533 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pXInfo) ){
191534 int bKey = sqlite3_column_int(pXInfo, 5);
191535 if( bKey ){
191536 int iCid = sqlite3_column_int(pXInfo, 1);
191537 int bDesc = sqlite3_column_int(pXInfo, 3);
191538 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
191539 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %Q", zCols, zComma,
191540 iCid, pIter->azTblType[iCid], zCollate
191541 );
191542 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
191543 zComma = ", ";
191544 }
191545 }
191546 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
191547 rbuFinalize(p, pXInfo);
191548
191549 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1, tnum);
191550 rbuMPrintfExec(p, p->dbMain,
191551 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
191552 zCols, zPk
191553 );
191554 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
191555 }
191556}
191557
191558/*
191559** If an error has already occurred when this function is called, it
191560** immediately returns zero (without doing any work). Or, if an error
191561** occurs during the execution of this function, it sets the error code
191562** in the sqlite3rbu object indicated by the first argument and returns
191563** zero.
191564**
191565** The iterator passed as the second argument is guaranteed to point to
191566** a table (not an index) when this function is called. This function
191567** attempts to create any imposter table required to write to the main
191568** table b-tree of the table before returning. Non-zero is returned if
191569** an imposter table are created, or zero otherwise.
191570**
191571** An imposter table is required in all cases except RBU_PK_VTAB. Only
191572** virtual tables are written to directly. The imposter table has the
191573** same schema as the actual target table (less any UNIQUE constraints).
191574** More precisely, the "same schema" means the same columns, types,
191575** collation sequences. For tables that do not have an external PRIMARY
191576** KEY, it also means the same PRIMARY KEY declaration.
191577*/
191578static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
191579 if( p->rc==SQLITE_OK0 && pIter->eType!=RBU_PK_VTAB ){
191580 int tnum = pIter->iTnum;
191581 const char *zComma = "";
191582 char *zSql = 0;
191583 int iCol;
191584 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 1);
191585
191586 for(iCol=0; p->rc==SQLITE_OK0 && iCol<pIter->nTblCol; iCol++){
191587 const char *zPk = "";
191588 const char *zCol = pIter->azTblCol[iCol];
191589 const char *zColl = 0;
191590
191591 p->rc = sqlite3_table_column_metadata(
191592 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
191593 );
191594
191595 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
191596 /* If the target table column is an "INTEGER PRIMARY KEY", add
191597 ** "PRIMARY KEY" to the imposter table column declaration. */
191598 zPk = "PRIMARY KEY ";
191599 }
191600 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %Q%s",
191601 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
191602 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
191603 );
191604 zComma = ", ";
191605 }
191606
191607 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
191608 char *zPk = rbuWithoutRowidPK(p, pIter);
191609 if( zPk ){
191610 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
191611 }
191612 }
191613
191614 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1, tnum);
191615 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
191616 pIter->zTbl, zSql,
191617 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
191618 );
191619 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
191620 }
191621}
191622
191623/*
191624** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
191625** Specifically a statement of the form:
191626**
191627** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
191628**
191629** The number of bound variables is equal to the number of columns in
191630** the target table, plus one (for the rbu_control column), plus one more
191631** (for the rbu_rowid column) if the target table is an implicit IPK or
191632** virtual table.
191633*/
191634static void rbuObjIterPrepareTmpInsert(
191635 sqlite3rbu *p,
191636 RbuObjIter *pIter,
191637 const char *zCollist,
191638 const char *zRbuRowid
191639){
191640 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
191641 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
191642 if( zBind ){
191643 assert( pIter->pTmpInsert==0 )((void) (0));
191644 p->rc = prepareFreeAndCollectError(
191645 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
191646 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
191647 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
191648 ));
191649 }
191650}
191651
191652static void rbuTmpInsertFunc(
191653 sqlite3_context *pCtx,
191654 int nVal,
191655 sqlite3_value **apVal
191656){
191657 sqlite3rbu *p = sqlite3_user_data(pCtx);
191658 int rc = SQLITE_OK0;
191659 int i;
191660
191661 assert( sqlite3_value_int(apVal[0])!=0((void) (0))
191662 || p->objiter.eType==RBU_PK_EXTERNAL((void) (0))
191663 || p->objiter.eType==RBU_PK_NONE((void) (0))
191664 )((void) (0));
191665 if( sqlite3_value_int(apVal[0])!=0 ){
191666 p->nPhaseOneStep += p->objiter.nIndex;
191667 }
191668
191669 for(i=0; rc==SQLITE_OK0 && i<nVal; i++){
191670 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
191671 }
191672 if( rc==SQLITE_OK0 ){
191673 sqlite3_step(p->objiter.pTmpInsert);
191674 rc = sqlite3_reset(p->objiter.pTmpInsert);
191675 }
191676
191677 if( rc!=SQLITE_OK0 ){
191678 sqlite3_result_error_code(pCtx, rc);
191679 }
191680}
191681
191682static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
191683 sqlite3_stmt *pStmt = 0;
191684 int rc = p->rc;
191685 char *zRet = 0;
191686
191687 if( rc==SQLITE_OK0 ){
191688 rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
191689 "SELECT trim(sql) FROM sqlite_master WHERE type='index' AND name=?"
191690 );
191691 }
191692 if( rc==SQLITE_OK0 ){
191693 int rc2;
191694 rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
191695 if( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
191696 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
191697 if( zSql ){
191698 int nParen = 0; /* Number of open parenthesis */
191699 int i;
191700 for(i=0; zSql[i]; i++){
191701 char c = zSql[i];
191702 if( c=='(' ){
191703 nParen++;
191704 }
191705 else if( c==')' ){
191706 nParen--;
191707 if( nParen==0 ){
191708 i++;
191709 break;
191710 }
191711 }else if( c=='"' || c=='\'' || c=='`' ){
191712 for(i++; 1; i++){
191713 if( zSql[i]==c ){
191714 if( zSql[i+1]!=c ) break;
191715 i++;
191716 }
191717 }
191718 }else if( c=='[' ){
191719 for(i++; 1; i++){
191720 if( zSql[i]==']' ) break;
191721 }
191722 }
191723 }
191724 if( zSql[i] ){
191725 zRet = rbuStrndup(&zSql[i], &rc);
191726 }
191727 }
191728 }
191729
191730 rc2 = sqlite3_finalize(pStmt);
191731 if( rc==SQLITE_OK0 ) rc = rc2;
191732 }
191733
191734 p->rc = rc;
191735 return zRet;
191736}
191737
191738/*
191739** Ensure that the SQLite statement handles required to update the
191740** target database object currently indicated by the iterator passed
191741** as the second argument are available.
191742*/
191743static int rbuObjIterPrepareAll(
191744 sqlite3rbu *p,
191745 RbuObjIter *pIter,
191746 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
191747){
191748 assert( pIter->bCleanup==0 )((void) (0));
191749 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK0 ){
191750 const int tnum = pIter->iTnum;
191751 char *zCollist = 0; /* List of indexed columns */
191752 char **pz = &p->zErrmsg;
191753 const char *zIdx = pIter->zIdx;
191754 char *zLimit = 0;
191755
191756 if( nOffset ){
191757 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
191758 if( !zLimit ) p->rc = SQLITE_NOMEM7;
191759 }
191760
191761 if( zIdx ){
191762 const char *zTbl = pIter->zTbl;
191763 char *zImposterCols = 0; /* Columns for imposter table */
191764 char *zImposterPK = 0; /* Primary key declaration for imposter */
191765 char *zWhere = 0; /* WHERE clause on PK columns */
191766 char *zBind = 0;
191767 char *zPart = 0;
191768 int nBind = 0;
191769
191770 assert( pIter->eType!=RBU_PK_VTAB )((void) (0));
191771 zCollist = rbuObjIterGetIndexCols(
191772 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
191773 );
191774 zBind = rbuObjIterGetBindlist(p, nBind);
191775 zPart = rbuObjIterGetIndexWhere(p, pIter);
191776
191777 /* Create the imposter table used to write to this index. */
191778 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 1);
191779 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 1,tnum);
191780 rbuMPrintfExec(p, p->dbMain,
191781 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
191782 zTbl, zImposterCols, zImposterPK
191783 );
191784 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER25, p->dbMain, "main", 0, 0);
191785
191786 /* Create the statement to insert index entries */
191787 pIter->nCol = nBind;
191788 if( p->rc==SQLITE_OK0 ){
191789 p->rc = prepareFreeAndCollectError(
191790 p->dbMain, &pIter->pInsert, &p->zErrmsg,
191791 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
191792 );
191793 }
191794
191795 /* And to delete index entries */
191796 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK0 ){
191797 p->rc = prepareFreeAndCollectError(
191798 p->dbMain, &pIter->pDelete, &p->zErrmsg,
191799 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
191800 );
191801 }
191802
191803 /* Create the SELECT statement to read keys in sorted order */
191804 if( p->rc==SQLITE_OK0 ){
191805 char *zSql;
191806 if( rbuIsVacuum(p) ){
191807 char *zStart = 0;
191808 if( nOffset ){
191809 zStart = rbuVacuumIndexStart(p, pIter);
191810 if( zStart ){
191811 sqlite3_free(zLimit);
191812 zLimit = 0;
191813 }
191814 }
191815
191816 zSql = sqlite3_mprintf(
191817 "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
191818 zCollist,
191819 pIter->zDataTbl,
191820 zPart,
191821 (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
191822 zCollist, zLimit
191823 );
191824 sqlite3_free(zStart);
191825 }else
191826
191827 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
191828 zSql = sqlite3_mprintf(
191829 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
191830 zCollist, p->zStateDb, pIter->zDataTbl,
191831 zPart, zCollist, zLimit
191832 );
191833 }else{
191834 zSql = sqlite3_mprintf(
191835 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s "
191836 "UNION ALL "
191837 "SELECT %s, rbu_control FROM '%q' "
191838 "%s %s typeof(rbu_control)='integer' AND rbu_control!=1 "
191839 "ORDER BY %s%s",
191840 zCollist, p->zStateDb, pIter->zDataTbl, zPart,
191841 zCollist, pIter->zDataTbl,
191842 zPart,
191843 (zPart ? "AND" : "WHERE"),
191844 zCollist, zLimit
191845 );
191846 }
191847 if( p->rc==SQLITE_OK0 ){
191848 p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
191849 }else{
191850 sqlite3_free(zSql);
191851 }
191852 }
191853
191854 sqlite3_free(zImposterCols);
191855 sqlite3_free(zImposterPK);
191856 sqlite3_free(zWhere);
191857 sqlite3_free(zBind);
191858 sqlite3_free(zPart);
191859 }else{
191860 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
191861 ||(pIter->eType==RBU_PK_NONE)
191862 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
191863 const char *zTbl = pIter->zTbl; /* Table this step applies to */
191864 const char *zWrite; /* Imposter table name */
191865
191866 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
191867 char *zWhere = rbuObjIterGetWhere(p, pIter);
191868 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
191869 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
191870
191871 zCollist = rbuObjIterGetCollist(p, pIter);
191872 pIter->nCol = pIter->nTblCol;
191873
191874 /* Create the imposter table or tables (if required). */
191875 rbuCreateImposterTable(p, pIter);
191876 rbuCreateImposterTable2(p, pIter);
191877 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
191878
191879 /* Create the INSERT statement to write to the target PK b-tree */
191880 if( p->rc==SQLITE_OK0 ){
191881 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
191882 sqlite3_mprintf(
191883 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
191884 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
191885 )
191886 );
191887 }
191888
191889 /* Create the DELETE statement to write to the target PK b-tree.
191890 ** Because it only performs INSERT operations, this is not required for
191891 ** an rbu vacuum handle. */
191892 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK0 ){
191893 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
191894 sqlite3_mprintf(
191895 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
191896 )
191897 );
191898 }
191899
191900 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
191901 const char *zRbuRowid = "";
191902 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
191903 zRbuRowid = ", rbu_rowid";
191904 }
191905
191906 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
191907 rbuMPrintfExec(p, p->dbRbu,
191908 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
191909 "SELECT *%s FROM '%q' WHERE 0;"
191910 , p->zStateDb, pIter->zDataTbl
191911 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
191912 , pIter->zDataTbl
191913 );
191914
191915 rbuMPrintfExec(p, p->dbMain,
191916 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
191917 "BEGIN "
191918 " SELECT rbu_tmp_insert(3, %s);"
191919 "END;"
191920
191921 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
191922 "BEGIN "
191923 " SELECT rbu_tmp_insert(3, %s);"
191924 "END;"
191925
191926 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
191927 "BEGIN "
191928 " SELECT rbu_tmp_insert(4, %s);"
191929 "END;",
191930 zWrite, zTbl, zOldlist,
191931 zWrite, zTbl, zOldlist,
191932 zWrite, zTbl, zNewlist
191933 );
191934
191935 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
191936 rbuMPrintfExec(p, p->dbMain,
191937 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
191938 "BEGIN "
191939 " SELECT rbu_tmp_insert(0, %s);"
191940 "END;",
191941 zWrite, zTbl, zNewlist
191942 );
191943 }
191944
191945 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
191946 }
191947
191948 /* Create the SELECT statement to read keys from data_xxx */
191949 if( p->rc==SQLITE_OK0 ){
191950 const char *zRbuRowid = "";
191951 char *zStart = 0;
191952 char *zOrder = 0;
191953 if( bRbuRowid ){
191954 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
191955 }
191956
191957 if( rbuIsVacuum(p) ){
191958 if( nOffset ){
191959 zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
191960 if( zStart ){
191961 sqlite3_free(zLimit);
191962 zLimit = 0;
191963 }
191964 }
191965 if( bRbuRowid ){
191966 zOrder = rbuMPrintf(p, "_rowid_");
191967 }else{
191968 zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
191969 }
191970 }
191971
191972 if( p->rc==SQLITE_OK0 ){
191973 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
191974 sqlite3_mprintf(
191975 "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
191976 zCollist,
191977 (rbuIsVacuum(p) ? "0 AS " : ""),
191978 zRbuRowid,
191979 pIter->zDataTbl, (zStart ? zStart : ""),
191980 (zOrder ? "ORDER BY" : ""), zOrder,
191981 zLimit
191982 )
191983 );
191984 }
191985 sqlite3_free(zStart);
191986 sqlite3_free(zOrder);
191987 }
191988
191989 sqlite3_free(zWhere);
191990 sqlite3_free(zOldlist);
191991 sqlite3_free(zNewlist);
191992 sqlite3_free(zBindings);
191993 }
191994 sqlite3_free(zCollist);
191995 sqlite3_free(zLimit);
191996 }
191997
191998 return p->rc;
191999}
192000
192001/*
192002** Set output variable *ppStmt to point to an UPDATE statement that may
192003** be used to update the imposter table for the main table b-tree of the
192004** table object that pIter currently points to, assuming that the
192005** rbu_control column of the data_xyz table contains zMask.
192006**
192007** If the zMask string does not specify any columns to update, then this
192008** is not an error. Output variable *ppStmt is set to NULL in this case.
192009*/
192010static int rbuGetUpdateStmt(
192011 sqlite3rbu *p, /* RBU handle */
192012 RbuObjIter *pIter, /* Object iterator */
192013 const char *zMask, /* rbu_control value ('x.x.') */
192014 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
192015){
192016 RbuUpdateStmt **pp;
192017 RbuUpdateStmt *pUp = 0;
192018 int nUp = 0;
192019
192020 /* In case an error occurs */
192021 *ppStmt = 0;
192022
192023 /* Search for an existing statement. If one is found, shift it to the front
192024 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
192025 ** to the number of statements currently in the cache and pUp to the
192026 ** last object in the list. */
192027 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
192028 pUp = *pp;
192029 if( strcmp(pUp->zMask, zMask)==0 ){
192030 *pp = pUp->pNext;
192031 pUp->pNext = pIter->pRbuUpdate;
192032 pIter->pRbuUpdate = pUp;
192033 *ppStmt = pUp->pUpdate;
192034 return SQLITE_OK0;
192035 }
192036 nUp++;
192037 }
192038 assert( pUp==0 || pUp->pNext==0 )((void) (0));
192039
192040 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
192041 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
192042 *pp = 0;
192043 sqlite3_finalize(pUp->pUpdate);
192044 pUp->pUpdate = 0;
192045 }else{
192046 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
192047 }
192048
192049 if( pUp ){
192050 char *zWhere = rbuObjIterGetWhere(p, pIter);
192051 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
192052 char *zUpdate = 0;
192053
192054 pUp->zMask = (char*)&pUp[1];
192055 memcpy(pUp->zMask, zMask, pIter->nTblCol);
192056 pUp->pNext = pIter->pRbuUpdate;
192057 pIter->pRbuUpdate = pUp;
192058
192059 if( zSet ){
192060 const char *zPrefix = "";
192061
192062 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
192063 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
192064 zPrefix, pIter->zTbl, zSet, zWhere
192065 );
192066 p->rc = prepareFreeAndCollectError(
192067 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
192068 );
192069 *ppStmt = pUp->pUpdate;
192070 }
192071 sqlite3_free(zWhere);
192072 sqlite3_free(zSet);
192073 }
192074
192075 return p->rc;
192076}
192077
192078static sqlite3 *rbuOpenDbhandle(
192079 sqlite3rbu *p,
192080 const char *zName,
192081 int bUseVfs
192082){
192083 sqlite3 *db = 0;
192084 if( p->rc==SQLITE_OK0 ){
192085 const int flags = SQLITE_OPEN_READWRITE0x00000002|SQLITE_OPEN_CREATE0x00000004|SQLITE_OPEN_URI0x00000040;
192086 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
192087 if( p->rc ){
192088 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
192089 sqlite3_close(db);
192090 db = 0;
192091 }
192092 }
192093 return db;
192094}
192095
192096/*
192097** Free an RbuState object allocated by rbuLoadState().
192098*/
192099static void rbuFreeState(RbuState *p){
192100 if( p ){
192101 sqlite3_free(p->zTbl);
192102 sqlite3_free(p->zDataTbl);
192103 sqlite3_free(p->zIdx);
192104 sqlite3_free(p);
192105 }
192106}
192107
192108/*
192109** Allocate an RbuState object and load the contents of the rbu_state
192110** table into it. Return a pointer to the new object. It is the
192111** responsibility of the caller to eventually free the object using
192112** sqlite3_free().
192113**
192114** If an error occurs, leave an error code and message in the rbu handle
192115** and return NULL.
192116*/
192117static RbuState *rbuLoadState(sqlite3rbu *p){
192118 RbuState *pRet = 0;
192119 sqlite3_stmt *pStmt = 0;
192120 int rc;
192121 int rc2;
192122
192123 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
192124 if( pRet==0 ) return 0;
192125
192126 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
192127 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
192128 );
192129 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
192130 switch( sqlite3_column_int(pStmt, 0) ){
192131 case RBU_STATE_STAGE:
192132 pRet->eStage = sqlite3_column_int(pStmt, 1);
192133 if( pRet->eStage!=RBU_STAGE_OAL
192134 && pRet->eStage!=RBU_STAGE_MOVE
192135 && pRet->eStage!=RBU_STAGE_CKPT
192136 ){
192137 p->rc = SQLITE_CORRUPT11;
192138 }
192139 break;
192140
192141 case RBU_STATE_TBL:
192142 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
192143 break;
192144
192145 case RBU_STATE_IDX:
192146 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
192147 break;
192148
192149 case RBU_STATE_ROW:
192150 pRet->nRow = sqlite3_column_int(pStmt, 1);
192151 break;
192152
192153 case RBU_STATE_PROGRESS:
192154 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
192155 break;
192156
192157 case RBU_STATE_CKPT:
192158 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
192159 break;
192160
192161 case RBU_STATE_COOKIE:
192162 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
192163 break;
192164
192165 case RBU_STATE_OALSZ:
192166 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
192167 break;
192168
192169 case RBU_STATE_PHASEONESTEP:
192170 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
192171 break;
192172
192173 case RBU_STATE_DATATBL:
192174 pRet->zDataTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
192175 break;
192176
192177 default:
192178 rc = SQLITE_CORRUPT11;
192179 break;
192180 }
192181 }
192182 rc2 = sqlite3_finalize(pStmt);
192183 if( rc==SQLITE_OK0 ) rc = rc2;
192184
192185 p->rc = rc;
192186 return pRet;
192187}
192188
192189
192190/*
192191** Open the database handle and attach the RBU database as "rbu". If an
192192** error occurs, leave an error code and message in the RBU handle.
192193*/
192194static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
192195 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) )((void) (0));
192196 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 )((void) (0));
192197
192198 /* Open the RBU database */
192199 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
192200
192201 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
192202 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
192203 if( p->zState==0 ){
192204 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
192205 p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
192206 }
192207 }
192208
192209 /* If using separate RBU and state databases, attach the state database to
192210 ** the RBU db handle now. */
192211 if( p->zState ){
192212 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
192213 memcpy(p->zStateDb, "stat", 4);
192214 }else{
192215 memcpy(p->zStateDb, "main", 4);
192216 }
192217
192218#if 0
192219 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
192220 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
192221 }
192222#endif
192223
192224 /* If it has not already been created, create the rbu_state table */
192225 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
192226
192227#if 0
192228 if( rbuIsVacuum(p) ){
192229 if( p->rc==SQLITE_OK0 ){
192230 int rc2;
192231 int bOk = 0;
192232 sqlite3_stmt *pCnt = 0;
192233 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
192234 "SELECT count(*) FROM stat.sqlite_master"
192235 );
192236 if( p->rc==SQLITE_OK0
192237 && sqlite3_step(pCnt)==SQLITE_ROW100
192238 && 1==sqlite3_column_int(pCnt, 0)
192239 ){
192240 bOk = 1;
192241 }
192242 rc2 = sqlite3_finalize(pCnt);
192243 if( p->rc==SQLITE_OK0 ) p->rc = rc2;
192244
192245 if( p->rc==SQLITE_OK0 && bOk==0 ){
192246 p->rc = SQLITE_ERROR1;
192247 p->zErrmsg = sqlite3_mprintf("invalid state database");
192248 }
192249
192250 if( p->rc==SQLITE_OK0 ){
192251 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
192252 }
192253 }
192254 }
192255#endif
192256
192257 if( p->rc==SQLITE_OK0 && rbuIsVacuum(p) ){
192258 int bOpen = 0;
192259 int rc;
192260 p->nRbu = 0;
192261 p->pRbuFd = 0;
192262 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
192263 if( rc!=SQLITE_NOTFOUND12 ) p->rc = rc;
192264 if( p->eStage>=RBU_STAGE_MOVE ){
192265 bOpen = 1;
192266 }else{
192267 RbuState *pState = rbuLoadState(p);
192268 if( pState ){
192269 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
192270 rbuFreeState(pState);
192271 }
192272 }
192273 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
192274 }
192275
192276 p->eStage = 0;
192277 if( p->rc==SQLITE_OK0 && p->dbMain==0 ){
192278 if( !rbuIsVacuum(p) ){
192279 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
192280 }else if( p->pRbuFd->pWalFd ){
192281 if( pbRetry ){
192282 p->pRbuFd->bNolock = 0;
192283 sqlite3_close(p->dbRbu);
192284 sqlite3_close(p->dbMain);
192285 p->dbMain = 0;
192286 p->dbRbu = 0;
192287 *pbRetry = 1;
192288 return;
192289 }
192290 p->rc = SQLITE_ERROR1;
192291 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
192292 }else{
192293 char *zTarget;
192294 char *zExtra = 0;
192295 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
192296 zExtra = &p->zRbu[5];
192297 while( *zExtra ){
192298 if( *zExtra++=='?' ) break;
192299 }
192300 if( *zExtra=='\0' ) zExtra = 0;
192301 }
192302
192303 zTarget = sqlite3_mprintf("file:%s-vactmp?rbu_memory=1%s%s",
192304 sqlite3_db_filename(p->dbRbu, "main"),
192305 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
192306 );
192307
192308 if( zTarget==0 ){
192309 p->rc = SQLITE_NOMEM7;
192310 return;
192311 }
192312 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
192313 sqlite3_free(zTarget);
192314 }
192315 }
192316
192317 if( p->rc==SQLITE_OK0 ){
192318 p->rc = sqlite3_create_function(p->dbMain,
192319 "rbu_tmp_insert", -1, SQLITE_UTF81, (void*)p, rbuTmpInsertFunc, 0, 0
192320 );
192321 }
192322
192323 if( p->rc==SQLITE_OK0 ){
192324 p->rc = sqlite3_create_function(p->dbMain,
192325 "rbu_fossil_delta", 2, SQLITE_UTF81, 0, rbuFossilDeltaFunc, 0, 0
192326 );
192327 }
192328
192329 if( p->rc==SQLITE_OK0 ){
192330 p->rc = sqlite3_create_function(p->dbRbu,
192331 "rbu_target_name", -1, SQLITE_UTF81, (void*)p, rbuTargetNameFunc, 0, 0
192332 );
192333 }
192334
192335 if( p->rc==SQLITE_OK0 ){
192336 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU26, (void*)p);
192337 }
192338 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
192339
192340 /* Mark the database file just opened as an RBU target database. If
192341 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
192342 ** This is an error. */
192343 if( p->rc==SQLITE_OK0 ){
192344 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU26, (void*)p);
192345 }
192346
192347 if( p->rc==SQLITE_NOTFOUND12 ){
192348 p->rc = SQLITE_ERROR1;
192349 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
192350 }
192351}
192352
192353/*
192354** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
192355** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
192356**
192357** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
192358** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
192359** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
192360** three characters, then shorten the suffix on z[] to be the last three
192361** characters of the original suffix.
192362**
192363** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
192364** do the suffix shortening regardless of URI parameter.
192365**
192366** Examples:
192367**
192368** test.db-journal => test.nal
192369** test.db-wal => test.wal
192370** test.db-shm => test.shm
192371** test.db-mj7f3319fa => test.9fa
192372*/
192373static void rbuFileSuffix3(const char *zBase, char *z){
192374#ifdef SQLITE_ENABLE_8_3_NAMES
192375#if SQLITE_ENABLE_8_3_NAMES<2
192376 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
192377#endif
192378 {
192379 int i, sz;
192380 sz = (int)strlen(z)&0xffffff;
192381 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
192382 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
192383 }
192384#endif
192385}
192386
192387/*
192388** Return the current wal-index header checksum for the target database
192389** as a 64-bit integer.
192390**
192391** The checksum is store in the first page of xShmMap memory as an 8-byte
192392** blob starting at byte offset 40.
192393*/
192394static i64 rbuShmChecksum(sqlite3rbu *p){
192395 i64 iRet = 0;
192396 if( p->rc==SQLITE_OK0 ){
192397 sqlite3_file *pDb = p->pTargetFd->pReal;
192398 u32 volatile *ptr;
192399 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
192400 if( p->rc==SQLITE_OK0 ){
192401 iRet = ((i64)ptr[10] << 32) + ptr[11];
192402 }
192403 }
192404 return iRet;
192405}
192406
192407/*
192408** This function is called as part of initializing or reinitializing an
192409** incremental checkpoint.
192410**
192411** It populates the sqlite3rbu.aFrame[] array with the set of
192412** (wal frame -> db page) copy operations required to checkpoint the
192413** current wal file, and obtains the set of shm locks required to safely
192414** perform the copy operations directly on the file-system.
192415**
192416** If argument pState is not NULL, then the incremental checkpoint is
192417** being resumed. In this case, if the checksum of the wal-index-header
192418** following recovery is not the same as the checksum saved in the RbuState
192419** object, then the rbu handle is set to DONE state. This occurs if some
192420** other client appends a transaction to the wal file in the middle of
192421** an incremental checkpoint.
192422*/
192423static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
192424
192425 /* If pState is NULL, then the wal file may not have been opened and
192426 ** recovered. Running a read-statement here to ensure that doing so
192427 ** does not interfere with the "capture" process below. */
192428 if( pState==0 ){
192429 p->eStage = 0;
192430 if( p->rc==SQLITE_OK0 ){
192431 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
192432 }
192433 }
192434
192435 /* Assuming no error has occurred, run a "restart" checkpoint with the
192436 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
192437 ** special behaviour in the rbu VFS:
192438 **
192439 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
192440 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
192441 ** proceed with running a passive checkpoint instead of failing).
192442 **
192443 ** * Attempts to read from the *-wal file or write to the database file
192444 ** do not perform any IO. Instead, the frame/page combinations that
192445 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
192446 ** array.
192447 **
192448 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
192449 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
192450 ** no-ops. These locks will not be released until the connection
192451 ** is closed.
192452 **
192453 ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
192454 ** error.
192455 **
192456 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
192457 ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
192458 ** array populated with a set of (frame -> page) mappings. Because the
192459 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
192460 ** data from the wal file into the database file according to the
192461 ** contents of aFrame[].
192462 */
192463 if( p->rc==SQLITE_OK0 ){
192464 int rc2;
192465 p->eStage = RBU_STAGE_CAPTURE;
192466 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
192467 if( rc2!=SQLITE_INTERNAL2 ) p->rc = rc2;
192468 }
192469
192470 if( p->rc==SQLITE_OK0 && p->nFrame>0 ){
192471 p->eStage = RBU_STAGE_CKPT;
192472 p->nStep = (pState ? pState->nRow : 0);
192473 p->aBuf = rbuMalloc(p, p->pgsz);
192474 p->iWalCksum = rbuShmChecksum(p);
192475 }
192476
192477 if( p->rc==SQLITE_OK0 ){
192478 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
192479 p->rc = SQLITE_DONE101;
192480 p->eStage = RBU_STAGE_DONE;
192481 }else{
192482 int nSectorSize;
192483 sqlite3_file *pDb = p->pTargetFd->pReal;
192484 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
192485 assert( p->nPagePerSector==0 )((void) (0));
192486 nSectorSize = pDb->pMethods->xSectorSize(pDb);
192487 if( nSectorSize>p->pgsz ){
192488 p->nPagePerSector = nSectorSize / p->pgsz;
192489 }else{
192490 p->nPagePerSector = 1;
192491 }
192492
192493 /* Call xSync() on the wal file. This causes SQLite to sync the
192494 ** directory in which the target database and the wal file reside, in
192495 ** case it has not been synced since the rename() call in
192496 ** rbuMoveOalFile(). */
192497 p->rc = pWal->pMethods->xSync(pWal, SQLITE_SYNC_NORMAL0x00002);
192498 }
192499 }
192500}
192501
192502/*
192503** Called when iAmt bytes are read from offset iOff of the wal file while
192504** the rbu object is in capture mode. Record the frame number of the frame
192505** being read in the aFrame[] array.
192506*/
192507static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
192508 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
192509 u32 iFrame;
192510
192511 if( pRbu->mLock!=mReq ){
192512 pRbu->rc = SQLITE_BUSY5;
192513 return SQLITE_INTERNAL2;
192514 }
192515
192516 pRbu->pgsz = iAmt;
192517 if( pRbu->nFrame==pRbu->nFrameAlloc ){
192518 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
192519 RbuFrame *aNew;
192520 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
192521 if( aNew==0 ) return SQLITE_NOMEM7;
192522 pRbu->aFrame = aNew;
192523 pRbu->nFrameAlloc = nNew;
192524 }
192525
192526 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
192527 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
192528 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
192529 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
192530 pRbu->nFrame++;
192531 return SQLITE_OK0;
192532}
192533
192534/*
192535** Called when a page of data is written to offset iOff of the database
192536** file while the rbu handle is in capture mode. Record the page number
192537** of the page being written in the aFrame[] array.
192538*/
192539static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
192540 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
192541 return SQLITE_OK0;
192542}
192543
192544/*
192545** This is called as part of an incremental checkpoint operation. Copy
192546** a single frame of data from the wal file into the database file, as
192547** indicated by the RbuFrame object.
192548*/
192549static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
192550 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
192551 sqlite3_file *pDb = p->pTargetFd->pReal;
192552 i64 iOff;
192553
192554 assert( p->rc==SQLITE_OK )((void) (0));
192555 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
192556 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
192557 if( p->rc ) return;
192558
192559 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
192560 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
192561}
192562
192563
192564/*
192565** Take an EXCLUSIVE lock on the database file.
192566*/
192567static void rbuLockDatabase(sqlite3rbu *p){
192568 sqlite3_file *pReal = p->pTargetFd->pReal;
192569 assert( p->rc==SQLITE_OK )((void) (0));
192570 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED1);
192571 if( p->rc==SQLITE_OK0 ){
192572 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE4);
192573 }
192574}
192575
192576#if defined(_WIN32_WCE)
192577static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
192578 int nChar;
192579 LPWSTR zWideFilename;
192580
192581 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL((void*)0), 0);
192582 if( nChar==0 ){
192583 return 0;
192584 }
192585 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
192586 if( zWideFilename==0 ){
192587 return 0;
192588 }
192589 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
192590 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
192591 nChar);
192592 if( nChar==0 ){
192593 sqlite3_free(zWideFilename);
192594 zWideFilename = 0;
192595 }
192596 return zWideFilename;
192597}
192598#endif
192599
192600/*
192601** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
192602** on the database file. This proc moves the *-oal file to the *-wal path,
192603** then reopens the database file (this time in vanilla, non-oal, WAL mode).
192604** If an error occurs, leave an error code and error message in the rbu
192605** handle.
192606*/
192607static void rbuMoveOalFile(sqlite3rbu *p){
192608 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
192609 const char *zMove = zBase;
192610 char *zOal;
192611 char *zWal;
192612
192613 if( rbuIsVacuum(p) ){
192614 zMove = sqlite3_db_filename(p->dbRbu, "main");
192615 }
192616 zOal = sqlite3_mprintf("%s-oal", zMove);
192617 zWal = sqlite3_mprintf("%s-wal", zMove);
192618
192619 assert( p->eStage==RBU_STAGE_MOVE )((void) (0));
192620 assert( p->rc==SQLITE_OK && p->zErrmsg==0 )((void) (0));
192621 if( zWal==0 || zOal==0 ){
192622 p->rc = SQLITE_NOMEM7;
192623 }else{
192624 /* Move the *-oal file to *-wal. At this point connection p->db is
192625 ** holding a SHARED lock on the target database file (because it is
192626 ** in WAL mode). So no other connection may be writing the db.
192627 **
192628 ** In order to ensure that there are no database readers, an EXCLUSIVE
192629 ** lock is obtained here before the *-oal is moved to *-wal.
192630 */
192631 rbuLockDatabase(p);
192632 if( p->rc==SQLITE_OK0 ){
192633 rbuFileSuffix3(zBase, zWal);
192634 rbuFileSuffix3(zBase, zOal);
192635
192636 /* Re-open the databases. */
192637 rbuObjIterFinalize(&p->objiter);
192638 sqlite3_close(p->dbRbu);
192639 sqlite3_close(p->dbMain);
192640 p->dbMain = 0;
192641 p->dbRbu = 0;
192642
192643#if defined(_WIN32_WCE)
192644 {
192645 LPWSTR zWideOal;
192646 LPWSTR zWideWal;
192647
192648 zWideOal = rbuWinUtf8ToUnicode(zOal);
192649 if( zWideOal ){
192650 zWideWal = rbuWinUtf8ToUnicode(zWal);
192651 if( zWideWal ){
192652 if( MoveFileW(zWideOal, zWideWal) ){
192653 p->rc = SQLITE_OK0;
192654 }else{
192655 p->rc = SQLITE_IOERR10;
192656 }
192657 sqlite3_free(zWideWal);
192658 }else{
192659 p->rc = SQLITE_IOERR_NOMEM(10 | (12<<8));
192660 }
192661 sqlite3_free(zWideOal);
192662 }else{
192663 p->rc = SQLITE_IOERR_NOMEM(10 | (12<<8));
192664 }
192665 }
192666#else
192667 p->rc = rename(zOal, zWal) ? SQLITE_IOERR10 : SQLITE_OK0;
192668#endif
192669
192670 if( p->rc==SQLITE_OK0 ){
192671 rbuOpenDatabase(p, 0);
192672 rbuSetupCheckpoint(p, 0);
192673 }
192674 }
192675 }
192676
192677 sqlite3_free(zWal);
192678 sqlite3_free(zOal);
192679}
192680
192681/*
192682** The SELECT statement iterating through the keys for the current object
192683** (p->objiter.pSelect) currently points to a valid row. This function
192684** determines the type of operation requested by this row and returns
192685** one of the following values to indicate the result:
192686**
192687** * RBU_INSERT
192688** * RBU_DELETE
192689** * RBU_IDX_DELETE
192690** * RBU_UPDATE
192691**
192692** If RBU_UPDATE is returned, then output variable *pzMask is set to
192693** point to the text value indicating the columns to update.
192694**
192695** If the rbu_control field contains an invalid value, an error code and
192696** message are left in the RBU handle and zero returned.
192697*/
192698static int rbuStepType(sqlite3rbu *p, const char **pzMask){
192699 int iCol = p->objiter.nCol; /* Index of rbu_control column */
192700 int res = 0; /* Return value */
192701
192702 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
192703 case SQLITE_INTEGER1: {
192704 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
192705 switch( iVal ){
192706 case 0: res = RBU_INSERT; break;
192707 case 1: res = RBU_DELETE; break;
192708 case 2: res = RBU_REPLACE; break;
192709 case 3: res = RBU_IDX_DELETE; break;
192710 case 4: res = RBU_IDX_INSERT; break;
192711 }
192712 break;
192713 }
192714
192715 case SQLITE_TEXT3: {
192716 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
192717 if( z==0 ){
192718 p->rc = SQLITE_NOMEM7;
192719 }else{
192720 *pzMask = (const char*)z;
192721 }
192722 res = RBU_UPDATE;
192723
192724 break;
192725 }
192726
192727 default:
192728 break;
192729 }
192730
192731 if( res==0 ){
192732 rbuBadControlError(p);
192733 }
192734 return res;
192735}
192736
192737#ifdef SQLITE_DEBUG
192738/*
192739** Assert that column iCol of statement pStmt is named zName.
192740*/
192741static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
192742 const char *zCol = sqlite3_column_name(pStmt, iCol);
192743 assert( 0==sqlite3_stricmp(zName, zCol) )((void) (0));
192744}
192745#else
192746# define assertColumnName(x,y,z)
192747#endif
192748
192749/*
192750** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
192751** RBU_IDX_DELETE. This function performs the work of a single
192752** sqlite3rbu_step() call for the type of operation specified by eType.
192753*/
192754static void rbuStepOneOp(sqlite3rbu *p, int eType){
192755 RbuObjIter *pIter = &p->objiter;
192756 sqlite3_value *pVal;
192757 sqlite3_stmt *pWriter;
192758 int i;
192759
192760 assert( p->rc==SQLITE_OK )((void) (0));
192761 assert( eType!=RBU_DELETE || pIter->zIdx==0 )((void) (0));
192762 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE((void) (0))
192763 || eType==RBU_INSERT || eType==RBU_IDX_INSERT((void) (0))
192764 )((void) (0));
192765
192766 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
192767 ** statement below does actually delete a row, nPhaseOneStep will be
192768 ** incremented by the same amount when SQL function rbu_tmp_insert()
192769 ** is invoked by the trigger. */
192770 if( eType==RBU_DELETE ){
192771 p->nPhaseOneStep -= p->objiter.nIndex;
192772 }
192773
192774 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
192775 pWriter = pIter->pDelete;
192776 }else{
192777 pWriter = pIter->pInsert;
192778 }
192779
192780 for(i=0; i<pIter->nCol; i++){
192781 /* If this is an INSERT into a table b-tree and the table has an
192782 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
192783 ** to write a NULL into the IPK column. That is not permitted. */
192784 if( eType==RBU_INSERT
192785 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
192786 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL5
192787 ){
192788 p->rc = SQLITE_MISMATCH20;
192789 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
192790 return;
192791 }
192792
192793 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
192794 continue;
192795 }
192796
192797 pVal = sqlite3_column_value(pIter->pSelect, i);
192798 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
192799 if( p->rc ) return;
192800 }
192801 if( pIter->zIdx==0 ){
192802 if( pIter->eType==RBU_PK_VTAB
192803 || pIter->eType==RBU_PK_NONE
192804 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
192805 ){
192806 /* For a virtual table, or a table with no primary key, the
192807 ** SELECT statement is:
192808 **
192809 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
192810 **
192811 ** Hence column_value(pIter->nCol+1).
192812 */
192813 assertColumnName(pIter->pSelect, pIter->nCol+1,
192814 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
192815 );
192816 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
192817 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
192818 }
192819 }
192820 if( p->rc==SQLITE_OK0 ){
192821 sqlite3_step(pWriter);
192822 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
192823 }
192824}
192825
192826/*
192827** This function does the work for an sqlite3rbu_step() call.
192828**
192829** The object-iterator (p->objiter) currently points to a valid object,
192830** and the input cursor (p->objiter.pSelect) currently points to a valid
192831** input row. Perform whatever processing is required and return.
192832**
192833** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
192834** and message is left in the RBU handle and a copy of the error code
192835** returned.
192836*/
192837static int rbuStep(sqlite3rbu *p){
192838 RbuObjIter *pIter = &p->objiter;
192839 const char *zMask = 0;
192840 int eType = rbuStepType(p, &zMask);
192841
192842 if( eType ){
192843 assert( eType==RBU_INSERT || eType==RBU_DELETE((void) (0))
192844 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE((void) (0))
192845 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE((void) (0))
192846 )((void) (0));
192847 assert( eType!=RBU_UPDATE || pIter->zIdx==0 )((void) (0));
192848
192849 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
192850 rbuBadControlError(p);
192851 }
192852 else if( eType==RBU_REPLACE ){
192853 if( pIter->zIdx==0 ){
192854 p->nPhaseOneStep += p->objiter.nIndex;
192855 rbuStepOneOp(p, RBU_DELETE);
192856 }
192857 if( p->rc==SQLITE_OK0 ) rbuStepOneOp(p, RBU_INSERT);
192858 }
192859 else if( eType!=RBU_UPDATE ){
192860 rbuStepOneOp(p, eType);
192861 }
192862 else{
192863 sqlite3_value *pVal;
192864 sqlite3_stmt *pUpdate = 0;
192865 assert( eType==RBU_UPDATE )((void) (0));
192866 p->nPhaseOneStep -= p->objiter.nIndex;
192867 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
192868 if( pUpdate ){
192869 int i;
192870 for(i=0; p->rc==SQLITE_OK0 && i<pIter->nCol; i++){
192871 char c = zMask[pIter->aiSrcOrder[i]];
192872 pVal = sqlite3_column_value(pIter->pSelect, i);
192873 if( pIter->abTblPk[i] || c!='.' ){
192874 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
192875 }
192876 }
192877 if( p->rc==SQLITE_OK0
192878 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
192879 ){
192880 /* Bind the rbu_rowid value to column _rowid_ */
192881 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
192882 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
192883 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
192884 }
192885 if( p->rc==SQLITE_OK0 ){
192886 sqlite3_step(pUpdate);
192887 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
192888 }
192889 }
192890 }
192891 }
192892 return p->rc;
192893}
192894
192895/*
192896** Increment the schema cookie of the main database opened by p->dbMain.
192897**
192898** Or, if this is an RBU vacuum, set the schema cookie of the main db
192899** opened by p->dbMain to one more than the schema cookie of the main
192900** db opened by p->dbRbu.
192901*/
192902static void rbuIncrSchemaCookie(sqlite3rbu *p){
192903 if( p->rc==SQLITE_OK0 ){
192904 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
192905 int iCookie = 1000000;
192906 sqlite3_stmt *pStmt;
192907
192908 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
192909 "PRAGMA schema_version"
192910 );
192911 if( p->rc==SQLITE_OK0 ){
192912 /* Coverage: it may be that this sqlite3_step() cannot fail. There
192913 ** is already a transaction open, so the prepared statement cannot
192914 ** throw an SQLITE_SCHEMA exception. The only database page the
192915 ** statement reads is page 1, which is guaranteed to be in the cache.
192916 ** And no memory allocations are required. */
192917 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
192918 iCookie = sqlite3_column_int(pStmt, 0);
192919 }
192920 rbuFinalize(p, pStmt);
192921 }
192922 if( p->rc==SQLITE_OK0 ){
192923 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
192924 }
192925 }
192926}
192927
192928/*
192929** Update the contents of the rbu_state table within the rbu database. The
192930** value stored in the RBU_STATE_STAGE column is eStage. All other values
192931** are determined by inspecting the rbu handle passed as the first argument.
192932*/
192933static void rbuSaveState(sqlite3rbu *p, int eStage){
192934 if( p->rc==SQLITE_OK0 || p->rc==SQLITE_DONE101 ){
192935 sqlite3_stmt *pInsert = 0;
192936 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
192937 int rc;
192938
192939 assert( p->zErrmsg==0 )((void) (0));
192940 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
192941 sqlite3_mprintf(
192942 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
192943 "(%d, %d), "
192944 "(%d, %Q), "
192945 "(%d, %Q), "
192946 "(%d, %d), "
192947 "(%d, %d), "
192948 "(%d, %lld), "
192949 "(%d, %lld), "
192950 "(%d, %lld), "
192951 "(%d, %lld), "
192952 "(%d, %Q) ",
192953 p->zStateDb,
192954 RBU_STATE_STAGE, eStage,
192955 RBU_STATE_TBL, p->objiter.zTbl,
192956 RBU_STATE_IDX, p->objiter.zIdx,
192957 RBU_STATE_ROW, p->nStep,
192958 RBU_STATE_PROGRESS, p->nProgress,
192959 RBU_STATE_CKPT, p->iWalCksum,
192960 RBU_STATE_COOKIE, (i64)pFd->iCookie,
192961 RBU_STATE_OALSZ, p->iOalSz,
192962 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep,
192963 RBU_STATE_DATATBL, p->objiter.zDataTbl
192964 )
192965 );
192966 assert( pInsert==0 || rc==SQLITE_OK )((void) (0));
192967
192968 if( rc==SQLITE_OK0 ){
192969 sqlite3_step(pInsert);
192970 rc = sqlite3_finalize(pInsert);
192971 }
192972 if( rc!=SQLITE_OK0 ) p->rc = rc;
192973 }
192974}
192975
192976
192977/*
192978** The second argument passed to this function is the name of a PRAGMA
192979** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
192980** This function executes the following on sqlite3rbu.dbRbu:
192981**
192982** "PRAGMA main.$zPragma"
192983**
192984** where $zPragma is the string passed as the second argument, then
192985** on sqlite3rbu.dbMain:
192986**
192987** "PRAGMA main.$zPragma = $val"
192988**
192989** where $val is the value returned by the first PRAGMA invocation.
192990**
192991** In short, it copies the value of the specified PRAGMA setting from
192992** dbRbu to dbMain.
192993*/
192994static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
192995 if( p->rc==SQLITE_OK0 ){
192996 sqlite3_stmt *pPragma = 0;
192997 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
192998 sqlite3_mprintf("PRAGMA main.%s", zPragma)
192999 );
193000 if( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pPragma) ){
193001 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
193002 zPragma, sqlite3_column_int(pPragma, 0)
193003 );
193004 }
193005 rbuFinalize(p, pPragma);
193006 }
193007}
193008
193009/*
193010** The RBU handle passed as the only argument has just been opened and
193011** the state database is empty. If this RBU handle was opened for an
193012** RBU vacuum operation, create the schema in the target db.
193013*/
193014static void rbuCreateTargetSchema(sqlite3rbu *p){
193015 sqlite3_stmt *pSql = 0;
193016 sqlite3_stmt *pInsert = 0;
193017
193018 assert( rbuIsVacuum(p) )((void) (0));
193019 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
193020 if( p->rc==SQLITE_OK0 ){
193021 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
193022 "SELECT sql FROM sqlite_master WHERE sql!='' AND rootpage!=0"
193023 " AND name!='sqlite_sequence' "
193024 " ORDER BY type DESC"
193025 );
193026 }
193027
193028 while( p->rc==SQLITE_OK0 && sqlite3_step(pSql)==SQLITE_ROW100 ){
193029 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
193030 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
193031 }
193032 rbuFinalize(p, pSql);
193033 if( p->rc!=SQLITE_OK0 ) return;
193034
193035 if( p->rc==SQLITE_OK0 ){
193036 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
193037 "SELECT * FROM sqlite_master WHERE rootpage=0 OR rootpage IS NULL"
193038 );
193039 }
193040
193041 if( p->rc==SQLITE_OK0 ){
193042 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
193043 "INSERT INTO sqlite_master VALUES(?,?,?,?,?)"
193044 );
193045 }
193046
193047 while( p->rc==SQLITE_OK0 && sqlite3_step(pSql)==SQLITE_ROW100 ){
193048 int i;
193049 for(i=0; i<5; i++){
193050 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
193051 }
193052 sqlite3_step(pInsert);
193053 p->rc = sqlite3_reset(pInsert);
193054 }
193055 if( p->rc==SQLITE_OK0 ){
193056 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
193057 }
193058
193059 rbuFinalize(p, pSql);
193060 rbuFinalize(p, pInsert);
193061}
193062
193063/*
193064** Step the RBU object.
193065*/
193066SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
193067 if( p ){
193068 switch( p->eStage ){
193069 case RBU_STAGE_OAL: {
193070 RbuObjIter *pIter = &p->objiter;
193071
193072 /* If this is an RBU vacuum operation and the state table was empty
193073 ** when this handle was opened, create the target database schema. */
193074 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK0 ){
193075 rbuCreateTargetSchema(p);
193076 rbuCopyPragma(p, "user_version");
193077 rbuCopyPragma(p, "application_id");
193078 }
193079
193080 while( p->rc==SQLITE_OK0 && pIter->zTbl ){
193081
193082 if( pIter->bCleanup ){
193083 /* Clean up the rbu_tmp_xxx table for the previous table. It
193084 ** cannot be dropped as there are currently active SQL statements.
193085 ** But the contents can be deleted. */
193086 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
193087 rbuMPrintfExec(p, p->dbRbu,
193088 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
193089 );
193090 }
193091 }else{
193092 rbuObjIterPrepareAll(p, pIter, 0);
193093
193094 /* Advance to the next row to process. */
193095 if( p->rc==SQLITE_OK0 ){
193096 int rc = sqlite3_step(pIter->pSelect);
193097 if( rc==SQLITE_ROW100 ){
193098 p->nProgress++;
193099 p->nStep++;
193100 return rbuStep(p);
193101 }
193102 p->rc = sqlite3_reset(pIter->pSelect);
193103 p->nStep = 0;
193104 }
193105 }
193106
193107 rbuObjIterNext(p, pIter);
193108 }
193109
193110 if( p->rc==SQLITE_OK0 ){
193111 assert( pIter->zTbl==0 )((void) (0));
193112 rbuSaveState(p, RBU_STAGE_MOVE);
193113 rbuIncrSchemaCookie(p);
193114 if( p->rc==SQLITE_OK0 ){
193115 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
193116 }
193117 if( p->rc==SQLITE_OK0 ){
193118 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
193119 }
193120 p->eStage = RBU_STAGE_MOVE;
193121 }
193122 break;
193123 }
193124
193125 case RBU_STAGE_MOVE: {
193126 if( p->rc==SQLITE_OK0 ){
193127 rbuMoveOalFile(p);
193128 p->nProgress++;
193129 }
193130 break;
193131 }
193132
193133 case RBU_STAGE_CKPT: {
193134 if( p->rc==SQLITE_OK0 ){
193135 if( p->nStep>=p->nFrame ){
193136 sqlite3_file *pDb = p->pTargetFd->pReal;
193137
193138 /* Sync the db file */
193139 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
193140
193141 /* Update nBackfill */
193142 if( p->rc==SQLITE_OK0 ){
193143 void volatile *ptr;
193144 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
193145 if( p->rc==SQLITE_OK0 ){
193146 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
193147 }
193148 }
193149
193150 if( p->rc==SQLITE_OK0 ){
193151 p->eStage = RBU_STAGE_DONE;
193152 p->rc = SQLITE_DONE101;
193153 }
193154 }else{
193155 /* At one point the following block copied a single frame from the
193156 ** wal file to the database file. So that one call to sqlite3rbu_step()
193157 ** checkpointed a single frame.
193158 **
193159 ** However, if the sector-size is larger than the page-size, and the
193160 ** application calls sqlite3rbu_savestate() or close() immediately
193161 ** after this step, then rbu_step() again, then a power failure occurs,
193162 ** then the database page written here may be damaged. Work around
193163 ** this by checkpointing frames until the next page in the aFrame[]
193164 ** lies on a different disk sector to the current one. */
193165 u32 iSector;
193166 do{
193167 RbuFrame *pFrame = &p->aFrame[p->nStep];
193168 iSector = (pFrame->iDbPage-1) / p->nPagePerSector;
193169 rbuCheckpointFrame(p, pFrame);
193170 p->nStep++;
193171 }while( p->nStep<p->nFrame
193172 && iSector==((p->aFrame[p->nStep].iDbPage-1) / p->nPagePerSector)
193173 && p->rc==SQLITE_OK0
193174 );
193175 }
193176 p->nProgress++;
193177 }
193178 break;
193179 }
193180
193181 default:
193182 break;
193183 }
193184 return p->rc;
193185 }else{
193186 return SQLITE_NOMEM7;
193187 }
193188}
193189
193190/*
193191** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
193192** otherwise. Either or both argument may be NULL. Two NULL values are
193193** considered equal, and NULL is considered distinct from all other values.
193194*/
193195static int rbuStrCompare(const char *z1, const char *z2){
193196 if( z1==0 && z2==0 ) return 0;
193197 if( z1==0 || z2==0 ) return 1;
193198 return (sqlite3_stricmp(z1, z2)!=0);
193199}
193200
193201/*
193202** This function is called as part of sqlite3rbu_open() when initializing
193203** an rbu handle in OAL stage. If the rbu update has not started (i.e.
193204** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
193205** things so that the next call to sqlite3rbu_step() continues on from
193206** where the previous rbu handle left off.
193207**
193208** If an error occurs, an error code and error message are left in the
193209** rbu handle passed as the first argument.
193210*/
193211static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
193212 assert( p->rc==SQLITE_OK )((void) (0));
193213 if( pState->zTbl ){
193214 RbuObjIter *pIter = &p->objiter;
193215 int rc = SQLITE_OK0;
193216
193217 while( rc==SQLITE_OK0 && pIter->zTbl && (pIter->bCleanup
193218 || rbuStrCompare(pIter->zIdx, pState->zIdx)
193219 || (pState->zDataTbl==0 && rbuStrCompare(pIter->zTbl, pState->zTbl))
193220 || (pState->zDataTbl && rbuStrCompare(pIter->zDataTbl, pState->zDataTbl))
193221 )){
193222 rc = rbuObjIterNext(p, pIter);
193223 }
193224
193225 if( rc==SQLITE_OK0 && !pIter->zTbl ){
193226 rc = SQLITE_ERROR1;
193227 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
193228 }
193229
193230 if( rc==SQLITE_OK0 ){
193231 p->nStep = pState->nRow;
193232 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
193233 }
193234
193235 p->rc = rc;
193236 }
193237}
193238
193239/*
193240** If there is a "*-oal" file in the file-system corresponding to the
193241** target database in the file-system, delete it. If an error occurs,
193242** leave an error code and error message in the rbu handle.
193243*/
193244static void rbuDeleteOalFile(sqlite3rbu *p){
193245 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
193246 if( zOal ){
193247 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
193248 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 )((void) (0));
193249 pVfs->xDelete(pVfs, zOal, 0);
193250 sqlite3_free(zOal);
193251 }
193252}
193253
193254/*
193255** Allocate a private rbu VFS for the rbu handle passed as the only
193256** argument. This VFS will be used unless the call to sqlite3rbu_open()
193257** specified a URI with a vfs=? option in place of a target database
193258** file name.
193259*/
193260static void rbuCreateVfs(sqlite3rbu *p){
193261 int rnd;
193262 char zRnd[64];
193263
193264 assert( p->rc==SQLITE_OK )((void) (0));
193265 sqlite3_randomness(sizeof(int), (void*)&rnd);
193266 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
193267 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
193268 if( p->rc==SQLITE_OK0 ){
193269 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
193270 assert( pVfs )((void) (0));
193271 p->zVfsName = pVfs->zName;
193272 ((rbu_vfs*)pVfs)->pRbu = p;
193273 }
193274}
193275
193276/*
193277** Destroy the private VFS created for the rbu handle passed as the only
193278** argument by an earlier call to rbuCreateVfs().
193279*/
193280static void rbuDeleteVfs(sqlite3rbu *p){
193281 if( p->zVfsName ){
193282 sqlite3rbu_destroy_vfs(p->zVfsName);
193283 p->zVfsName = 0;
193284 }
193285}
193286
193287/*
193288** This user-defined SQL function is invoked with a single argument - the
193289** name of a table expected to appear in the target database. It returns
193290** the number of auxilliary indexes on the table.
193291*/
193292static void rbuIndexCntFunc(
193293 sqlite3_context *pCtx,
193294 int nVal,
193295 sqlite3_value **apVal
193296){
193297 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
193298 sqlite3_stmt *pStmt = 0;
193299 char *zErrmsg = 0;
193300 int rc;
193301
193302 assert( nVal==1 )((void) (0));
193303
193304 rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &zErrmsg,
193305 sqlite3_mprintf("SELECT count(*) FROM sqlite_master "
193306 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
193307 );
193308 if( rc!=SQLITE_OK0 ){
193309 sqlite3_result_error(pCtx, zErrmsg, -1);
193310 }else{
193311 int nIndex = 0;
193312 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
193313 nIndex = sqlite3_column_int(pStmt, 0);
193314 }
193315 rc = sqlite3_finalize(pStmt);
193316 if( rc==SQLITE_OK0 ){
193317 sqlite3_result_int(pCtx, nIndex);
193318 }else{
193319 sqlite3_result_error(pCtx, sqlite3_errmsg(p->dbMain), -1);
193320 }
193321 }
193322
193323 sqlite3_free(zErrmsg);
193324}
193325
193326/*
193327** If the RBU database contains the rbu_count table, use it to initialize
193328** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
193329** is assumed to contain the same columns as:
193330**
193331** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
193332**
193333** There should be one row in the table for each data_xxx table in the
193334** database. The 'tbl' column should contain the name of a data_xxx table,
193335** and the cnt column the number of rows it contains.
193336**
193337** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
193338** for all rows in the rbu_count table, where nIndex is the number of
193339** indexes on the corresponding target database table.
193340*/
193341static void rbuInitPhaseOneSteps(sqlite3rbu *p){
193342 if( p->rc==SQLITE_OK0 ){
193343 sqlite3_stmt *pStmt = 0;
193344 int bExists = 0; /* True if rbu_count exists */
193345
193346 p->nPhaseOneStep = -1;
193347
193348 p->rc = sqlite3_create_function(p->dbRbu,
193349 "rbu_index_cnt", 1, SQLITE_UTF81, (void*)p, rbuIndexCntFunc, 0, 0
193350 );
193351
193352 /* Check for the rbu_count table. If it does not exist, or if an error
193353 ** occurs, nPhaseOneStep will be left set to -1. */
193354 if( p->rc==SQLITE_OK0 ){
193355 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
193356 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'rbu_count'"
193357 );
193358 }
193359 if( p->rc==SQLITE_OK0 ){
193360 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
193361 bExists = 1;
193362 }
193363 p->rc = sqlite3_finalize(pStmt);
193364 }
193365
193366 if( p->rc==SQLITE_OK0 && bExists ){
193367 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
193368 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
193369 "FROM rbu_count"
193370 );
193371 if( p->rc==SQLITE_OK0 ){
193372 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
193373 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
193374 }
193375 p->rc = sqlite3_finalize(pStmt);
193376 }
193377 }
193378 }
193379}
193380
193381
193382static sqlite3rbu *openRbuHandle(
193383 const char *zTarget,
193384 const char *zRbu,
193385 const char *zState
193386){
193387 sqlite3rbu *p;
193388 size_t nTarget = zTarget ? strlen(zTarget) : 0;
193389 size_t nRbu = strlen(zRbu);
193390 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
193391
193392 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
193393 if( p ){
193394 RbuState *pState = 0;
193395
193396 /* Create the custom VFS. */
193397 memset(p, 0, sizeof(sqlite3rbu));
193398 rbuCreateVfs(p);
193399
193400 /* Open the target, RBU and state databases */
193401 if( p->rc==SQLITE_OK0 ){
193402 char *pCsr = (char*)&p[1];
193403 int bRetry = 0;
193404 if( zTarget ){
193405 p->zTarget = pCsr;
193406 memcpy(p->zTarget, zTarget, nTarget+1);
193407 pCsr += nTarget+1;
193408 }
193409 p->zRbu = pCsr;
193410 memcpy(p->zRbu, zRbu, nRbu+1);
193411 pCsr += nRbu+1;
193412 if( zState ){
193413 p->zState = rbuMPrintf(p, "%s", zState);
193414 }
193415
193416 /* If the first attempt to open the database file fails and the bRetry
193417 ** flag it set, this means that the db was not opened because it seemed
193418 ** to be a wal-mode db. But, this may have happened due to an earlier
193419 ** RBU vacuum operation leaving an old wal file in the directory.
193420 ** If this is the case, it will have been checkpointed and deleted
193421 ** when the handle was closed and a second attempt to open the
193422 ** database may succeed. */
193423 rbuOpenDatabase(p, &bRetry);
193424 if( bRetry ){
193425 rbuOpenDatabase(p, 0);
193426 }
193427 }
193428
193429 if( p->rc==SQLITE_OK0 ){
193430 pState = rbuLoadState(p);
193431 assert( pState || p->rc!=SQLITE_OK )((void) (0));
193432 if( p->rc==SQLITE_OK0 ){
193433
193434 if( pState->eStage==0 ){
193435 rbuDeleteOalFile(p);
193436 rbuInitPhaseOneSteps(p);
193437 p->eStage = RBU_STAGE_OAL;
193438 }else{
193439 p->eStage = pState->eStage;
193440 p->nPhaseOneStep = pState->nPhaseOneStep;
193441 }
193442 p->nProgress = pState->nProgress;
193443 p->iOalSz = pState->iOalSz;
193444 }
193445 }
193446 assert( p->rc!=SQLITE_OK || p->eStage!=0 )((void) (0));
193447
193448 if( p->rc==SQLITE_OK0 && p->pTargetFd->pWalFd ){
193449 if( p->eStage==RBU_STAGE_OAL ){
193450 p->rc = SQLITE_ERROR1;
193451 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
193452 }else if( p->eStage==RBU_STAGE_MOVE ){
193453 p->eStage = RBU_STAGE_CKPT;
193454 p->nStep = 0;
193455 }
193456 }
193457
193458 if( p->rc==SQLITE_OK0
193459 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
193460 && pState->eStage!=0
193461 ){
193462 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
193463 if( pFd->iCookie!=pState->iCookie ){
193464 /* At this point (pTargetFd->iCookie) contains the value of the
193465 ** change-counter cookie (the thing that gets incremented when a
193466 ** transaction is committed in rollback mode) currently stored on
193467 ** page 1 of the database file. */
193468 p->rc = SQLITE_BUSY5;
193469 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
193470 (rbuIsVacuum(p) ? "vacuum" : "update")
193471 );
193472 }
193473 }
193474
193475 if( p->rc==SQLITE_OK0 ){
193476 if( p->eStage==RBU_STAGE_OAL ){
193477 sqlite3 *db = p->dbMain;
193478 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
193479
193480 /* Point the object iterator at the first object */
193481 if( p->rc==SQLITE_OK0 ){
193482 p->rc = rbuObjIterFirst(p, &p->objiter);
193483 }
193484
193485 /* If the RBU database contains no data_xxx tables, declare the RBU
193486 ** update finished. */
193487 if( p->rc==SQLITE_OK0 && p->objiter.zTbl==0 ){
193488 p->rc = SQLITE_DONE101;
193489 p->eStage = RBU_STAGE_DONE;
193490 }else{
193491 if( p->rc==SQLITE_OK0 && pState->eStage==0 && rbuIsVacuum(p) ){
193492 rbuCopyPragma(p, "page_size");
193493 rbuCopyPragma(p, "auto_vacuum");
193494 }
193495
193496 /* Open transactions both databases. The *-oal file is opened or
193497 ** created at this point. */
193498 if( p->rc==SQLITE_OK0 ){
193499 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
193500 }
193501
193502 /* Check if the main database is a zipvfs db. If it is, set the upper
193503 ** level pager to use "journal_mode=off". This prevents it from
193504 ** generating a large journal using a temp file. */
193505 if( p->rc==SQLITE_OK0 ){
193506 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS25, 0);
193507 if( frc==SQLITE_OK0 ){
193508 p->rc = sqlite3_exec(
193509 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
193510 }
193511 }
193512
193513 if( p->rc==SQLITE_OK0 ){
193514 rbuSetupOal(p, pState);
193515 }
193516 }
193517 }else if( p->eStage==RBU_STAGE_MOVE ){
193518 /* no-op */
193519 }else if( p->eStage==RBU_STAGE_CKPT ){
193520 rbuSetupCheckpoint(p, pState);
193521 }else if( p->eStage==RBU_STAGE_DONE ){
193522 p->rc = SQLITE_DONE101;
193523 }else{
193524 p->rc = SQLITE_CORRUPT11;
193525 }
193526 }
193527
193528 rbuFreeState(pState);
193529 }
193530
193531 return p;
193532}
193533
193534/*
193535** Allocate and return an RBU handle with all fields zeroed except for the
193536** error code, which is set to SQLITE_MISUSE.
193537*/
193538static sqlite3rbu *rbuMisuseError(void){
193539 sqlite3rbu *pRet;
193540 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
193541 if( pRet ){
193542 memset(pRet, 0, sizeof(sqlite3rbu));
193543 pRet->rc = SQLITE_MISUSE21;
193544 }
193545 return pRet;
193546}
193547
193548/*
193549** Open and return a new RBU handle.
193550*/
193551SQLITE_API sqlite3rbu *sqlite3rbu_open(
193552 const char *zTarget,
193553 const char *zRbu,
193554 const char *zState
193555){
193556 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
193557 /* TODO: Check that zTarget and zRbu are non-NULL */
193558 return openRbuHandle(zTarget, zRbu, zState);
193559}
193560
193561/*
193562** Open a handle to begin or resume an RBU VACUUM operation.
193563*/
193564SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
193565 const char *zTarget,
193566 const char *zState
193567){
193568 if( zTarget==0 ){ return rbuMisuseError(); }
193569 if( zState ){
193570 int n = strlen(zState);
193571 if( n>=7 && 0==memcmp("-vactmp", &zState[n-7], 7) ){
193572 return rbuMisuseError();
193573 }
193574 }
193575 /* TODO: Check that both arguments are non-NULL */
193576 return openRbuHandle(0, zTarget, zState);
193577}
193578
193579/*
193580** Return the database handle used by pRbu.
193581*/
193582SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
193583 sqlite3 *db = 0;
193584 if( pRbu ){
193585 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
193586 }
193587 return db;
193588}
193589
193590
193591/*
193592** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
193593** then edit any error message string so as to remove all occurrences of
193594** the pattern "rbu_imp_[0-9]*".
193595*/
193596static void rbuEditErrmsg(sqlite3rbu *p){
193597 if( p->rc==SQLITE_CONSTRAINT19 && p->zErrmsg ){
193598 unsigned int i;
193599 size_t nErrmsg = strlen(p->zErrmsg);
193600 for(i=0; i<(nErrmsg-8); i++){
193601 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
193602 int nDel = 8;
193603 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
193604 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
193605 nErrmsg -= nDel;
193606 }
193607 }
193608 }
193609}
193610
193611/*
193612** Close the RBU handle.
193613*/
193614SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
193615 int rc;
193616 if( p ){
193617
193618 /* Commit the transaction to the *-oal file. */
193619 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_OAL ){
193620 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
193621 }
193622
193623 /* Sync the db file if currently doing an incremental checkpoint */
193624 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_CKPT ){
193625 sqlite3_file *pDb = p->pTargetFd->pReal;
193626 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
193627 }
193628
193629 rbuSaveState(p, p->eStage);
193630
193631 if( p->rc==SQLITE_OK0 && p->eStage==RBU_STAGE_OAL ){
193632 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
193633 }
193634
193635 /* Close any open statement handles. */
193636 rbuObjIterFinalize(&p->objiter);
193637
193638 /* If this is an RBU vacuum handle and the vacuum has either finished
193639 ** successfully or encountered an error, delete the contents of the
193640 ** state table. This causes the next call to sqlite3rbu_vacuum()
193641 ** specifying the current target and state databases to start a new
193642 ** vacuum from scratch. */
193643 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK0 && p->dbRbu ){
193644 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
193645 if( p->rc==SQLITE_DONE101 && rc2!=SQLITE_OK0 ) p->rc = rc2;
193646 }
193647
193648 /* Close the open database handle and VFS object. */
193649 sqlite3_close(p->dbRbu);
193650 sqlite3_close(p->dbMain);
193651 assert( p->szTemp==0 )((void) (0));
193652 rbuDeleteVfs(p);
193653 sqlite3_free(p->aBuf);
193654 sqlite3_free(p->aFrame);
193655
193656 rbuEditErrmsg(p);
193657 rc = p->rc;
193658 if( pzErrmsg ){
193659 *pzErrmsg = p->zErrmsg;
193660 }else{
193661 sqlite3_free(p->zErrmsg);
193662 }
193663 sqlite3_free(p->zState);
193664 sqlite3_free(p);
193665 }else{
193666 rc = SQLITE_NOMEM7;
193667 *pzErrmsg = 0;
193668 }
193669 return rc;
193670}
193671
193672/*
193673** Return the total number of key-value operations (inserts, deletes or
193674** updates) that have been performed on the target database since the
193675** current RBU update was started.
193676*/
193677SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
193678 return pRbu->nProgress;
193679}
193680
193681/*
193682** Return permyriadage progress indications for the two main stages of
193683** an RBU update.
193684*/
193685SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
193686 const int MAX_PROGRESS = 10000;
193687 switch( p->eStage ){
193688 case RBU_STAGE_OAL:
193689 if( p->nPhaseOneStep>0 ){
193690 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
193691 }else{
193692 *pnOne = -1;
193693 }
193694 *pnTwo = 0;
193695 break;
193696
193697 case RBU_STAGE_MOVE:
193698 *pnOne = MAX_PROGRESS;
193699 *pnTwo = 0;
193700 break;
193701
193702 case RBU_STAGE_CKPT:
193703 *pnOne = MAX_PROGRESS;
193704 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
193705 break;
193706
193707 case RBU_STAGE_DONE:
193708 *pnOne = MAX_PROGRESS;
193709 *pnTwo = MAX_PROGRESS;
193710 break;
193711
193712 default:
193713 assert( 0 )((void) (0));
193714 }
193715}
193716
193717/*
193718** Return the current state of the RBU vacuum or update operation.
193719*/
193720SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
193721 int aRes[] = {
193722 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
193723 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
193724 };
193725
193726 assert( RBU_STAGE_OAL==1 )((void) (0));
193727 assert( RBU_STAGE_MOVE==2 )((void) (0));
193728 assert( RBU_STAGE_CKPT==4 )((void) (0));
193729 assert( RBU_STAGE_DONE==5 )((void) (0));
193730 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL )((void) (0));
193731 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE )((void) (0));
193732 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT )((void) (0));
193733 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE )((void) (0));
193734
193735 if( p->rc!=SQLITE_OK0 && p->rc!=SQLITE_DONE101 ){
193736 return SQLITE_RBU_STATE_ERROR;
193737 }else{
193738 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE )((void) (0));
193739 assert( p->eStage==RBU_STAGE_OAL((void) (0))
193740 || p->eStage==RBU_STAGE_MOVE((void) (0))
193741 || p->eStage==RBU_STAGE_CKPT((void) (0))
193742 || p->eStage==RBU_STAGE_DONE((void) (0))
193743 )((void) (0));
193744 return aRes[p->eStage];
193745 }
193746}
193747
193748SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
193749 int rc = p->rc;
193750 if( rc==SQLITE_DONE101 ) return SQLITE_OK0;
193751
193752 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE )((void) (0));
193753 if( p->eStage==RBU_STAGE_OAL ){
193754 assert( rc!=SQLITE_DONE )((void) (0));
193755 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
193756 }
193757
193758 /* Sync the db file */
193759 if( rc==SQLITE_OK0 && p->eStage==RBU_STAGE_CKPT ){
193760 sqlite3_file *pDb = p->pTargetFd->pReal;
193761 rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL0x00002);
193762 }
193763
193764 p->rc = rc;
193765 rbuSaveState(p, p->eStage);
193766 rc = p->rc;
193767
193768 if( p->eStage==RBU_STAGE_OAL ){
193769 assert( rc!=SQLITE_DONE )((void) (0));
193770 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
193771 if( rc==SQLITE_OK0 ){
193772 const char *zBegin = rbuIsVacuum(p) ? "BEGIN" : "BEGIN IMMEDIATE";
193773 rc = sqlite3_exec(p->dbRbu, zBegin, 0, 0, 0);
193774 }
193775 if( rc==SQLITE_OK0 ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
193776 }
193777
193778 p->rc = rc;
193779 return rc;
193780}
193781
193782/**************************************************************************
193783** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
193784** of a standard VFS in the following ways:
193785**
193786** 1. Whenever the first page of a main database file is read or
193787** written, the value of the change-counter cookie is stored in
193788** rbu_file.iCookie. Similarly, the value of the "write-version"
193789** database header field is stored in rbu_file.iWriteVer. This ensures
193790** that the values are always trustworthy within an open transaction.
193791**
193792** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
193793** member variable of the associated database file descriptor is set
193794** to point to the new file. A mutex protected linked list of all main
193795** db fds opened using a particular RBU VFS is maintained at
193796** rbu_vfs.pMain to facilitate this.
193797**
193798** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
193799** object can be marked as the target database of an RBU update. This
193800** turns on the following extra special behaviour:
193801**
193802** 3a. If xAccess() is called to check if there exists a *-wal file
193803** associated with an RBU target database currently in RBU_STAGE_OAL
193804** stage (preparing the *-oal file), the following special handling
193805** applies:
193806**
193807** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
193808** target database may not be in wal mode already.
193809**
193810** * if the *-wal file does not exist, set the output parameter to
193811** non-zero (to tell SQLite that it does exist) anyway.
193812**
193813** Then, when xOpen() is called to open the *-wal file associated with
193814** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
193815** file, the rbu vfs opens the corresponding *-oal file instead.
193816**
193817** 3b. The *-shm pages returned by xShmMap() for a target db file in
193818** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
193819** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
193820** are no-ops on target database files in RBU_STAGE_OAL mode. This is
193821** because assert() statements in some VFS implementations fail if
193822** xShmLock() is called before xShmMap().
193823**
193824** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
193825** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
193826** fails with an SQLITE_BUSY error. This is to stop RBU connections
193827** from automatically checkpointing a *-wal (or *-oal) file from within
193828** sqlite3_close().
193829**
193830** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
193831** all xWrite() calls on the target database file perform no IO.
193832** Instead the frame and page numbers that would be read and written
193833** are recorded. Additionally, successful attempts to obtain exclusive
193834** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
193835** database file are recorded. xShmLock() calls to unlock the same
193836** locks are no-ops (so that once obtained, these locks are never
193837** relinquished). Finally, calls to xSync() on the target database
193838** file fail with SQLITE_INTERNAL errors.
193839*/
193840
193841static void rbuUnlockShm(rbu_file *p){
193842 assert( p->openFlags & SQLITE_OPEN_MAIN_DB )((void) (0));
193843 if( p->pRbu ){
193844 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
193845 int i;
193846 for(i=0; i<SQLITE_SHM_NLOCK8;i++){
193847 if( (1<<i) & p->pRbu->mLock ){
193848 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK1|SQLITE_SHM_EXCLUSIVE8);
193849 }
193850 }
193851 p->pRbu->mLock = 0;
193852 }
193853}
193854
193855/*
193856*/
193857static int rbuUpdateTempSize(rbu_file *pFd, sqlite3_int64 nNew){
193858 sqlite3rbu *pRbu = pFd->pRbu;
193859 i64 nDiff = nNew - pFd->sz;
193860 pRbu->szTemp += nDiff;
193861 pFd->sz = nNew;
193862 assert( pRbu->szTemp>=0 )((void) (0));
193863 if( pRbu->szTempLimit && pRbu->szTemp>pRbu->szTempLimit ) return SQLITE_FULL13;
193864 return SQLITE_OK0;
193865}
193866
193867/*
193868** Add an item to the main-db lists, if it is not already present.
193869**
193870** There are two main-db lists. One for all file descriptors, and one
193871** for all file descriptors with rbu_file.pDb!=0. If the argument has
193872** rbu_file.pDb!=0, then it is assumed to already be present on the
193873** main list and is only added to the pDb!=0 list.
193874*/
193875static void rbuMainlistAdd(rbu_file *p){
193876 rbu_vfs *pRbuVfs = p->pRbuVfs;
193877 rbu_file *pIter;
193878 assert( (p->openFlags & SQLITE_OPEN_MAIN_DB) )((void) (0));
193879 sqlite3_mutex_enter(pRbuVfs->mutex);
193880 if( p->pRbu==0 ){
193881 for(pIter=pRbuVfs->pMain; pIter; pIter=pIter->pMainNext);
193882 p->pMainNext = pRbuVfs->pMain;
193883 pRbuVfs->pMain = p;
193884 }else{
193885 for(pIter=pRbuVfs->pMainRbu; pIter && pIter!=p; pIter=pIter->pMainRbuNext){}
193886 if( pIter==0 ){
193887 p->pMainRbuNext = pRbuVfs->pMainRbu;
193888 pRbuVfs->pMainRbu = p;
193889 }
193890 }
193891 sqlite3_mutex_leave(pRbuVfs->mutex);
193892}
193893
193894/*
193895** Remove an item from the main-db lists.
193896*/
193897static void rbuMainlistRemove(rbu_file *p){
193898 rbu_file **pp;
193899 sqlite3_mutex_enter(p->pRbuVfs->mutex);
193900 for(pp=&p->pRbuVfs->pMain; *pp && *pp!=p; pp=&((*pp)->pMainNext)){}
193901 if( *pp ) *pp = p->pMainNext;
193902 p->pMainNext = 0;
193903 for(pp=&p->pRbuVfs->pMainRbu; *pp && *pp!=p; pp=&((*pp)->pMainRbuNext)){}
193904 if( *pp ) *pp = p->pMainRbuNext;
193905 p->pMainRbuNext = 0;
193906 sqlite3_mutex_leave(p->pRbuVfs->mutex);
193907}
193908
193909/*
193910** Given that zWal points to a buffer containing a wal file name passed to
193911** either the xOpen() or xAccess() VFS method, search the main-db list for
193912** a file-handle opened by the same database connection on the corresponding
193913** database file.
193914**
193915** If parameter bRbu is true, only search for file-descriptors with
193916** rbu_file.pDb!=0.
193917*/
193918static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal, int bRbu){
193919 rbu_file *pDb;
193920 sqlite3_mutex_enter(pRbuVfs->mutex);
193921 if( bRbu ){
193922 for(pDb=pRbuVfs->pMainRbu; pDb && pDb->zWal!=zWal; pDb=pDb->pMainRbuNext){}
193923 }else{
193924 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
193925 }
193926 sqlite3_mutex_leave(pRbuVfs->mutex);
193927 return pDb;
193928}
193929
193930/*
193931** Close an rbu file.
193932*/
193933static int rbuVfsClose(sqlite3_file *pFile){
193934 rbu_file *p = (rbu_file*)pFile;
193935 int rc;
193936 int i;
193937
193938 /* Free the contents of the apShm[] array. And the array itself. */
193939 for(i=0; i<p->nShm; i++){
193940 sqlite3_free(p->apShm[i]);
193941 }
193942 sqlite3_free(p->apShm);
193943 p->apShm = 0;
193944 sqlite3_free(p->zDel);
193945
193946 if( p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100 ){
193947 rbuMainlistRemove(p);
193948 rbuUnlockShm(p);
193949 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
193950 }
193951 else if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008) && p->pRbu ){
193952 rbuUpdateTempSize(p, 0);
193953 }
193954 assert( p->pMainNext==0 && p->pRbuVfs->pMain!=p )((void) (0));
193955
193956 /* Close the underlying file handle */
193957 rc = p->pReal->pMethods->xClose(p->pReal);
193958 return rc;
193959}
193960
193961
193962/*
193963** Read and return an unsigned 32-bit big-endian integer from the buffer
193964** passed as the only argument.
193965*/
193966static u32 rbuGetU32(u8 *aBuf){
193967 return ((u32)aBuf[0] << 24)
193968 + ((u32)aBuf[1] << 16)
193969 + ((u32)aBuf[2] << 8)
193970 + ((u32)aBuf[3]);
193971}
193972
193973/*
193974** Write an unsigned 32-bit value in big-endian format to the supplied
193975** buffer.
193976*/
193977static void rbuPutU32(u8 *aBuf, u32 iVal){
193978 aBuf[0] = (iVal >> 24) & 0xFF;
193979 aBuf[1] = (iVal >> 16) & 0xFF;
193980 aBuf[2] = (iVal >> 8) & 0xFF;
193981 aBuf[3] = (iVal >> 0) & 0xFF;
193982}
193983
193984static void rbuPutU16(u8 *aBuf, u16 iVal){
193985 aBuf[0] = (iVal >> 8) & 0xFF;
193986 aBuf[1] = (iVal >> 0) & 0xFF;
193987}
193988
193989/*
193990** Read data from an rbuVfs-file.
193991*/
193992static int rbuVfsRead(
193993 sqlite3_file *pFile,
193994 void *zBuf,
193995 int iAmt,
193996 sqlite_int64 iOfst
193997){
193998 rbu_file *p = (rbu_file*)pFile;
193999 sqlite3rbu *pRbu = p->pRbu;
194000 int rc;
194001
194002 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
194003 assert( p->openFlags & SQLITE_OPEN_WAL )((void) (0));
194004 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
194005 }else{
194006 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
194007 && (p->openFlags & SQLITE_OPEN_WAL0x00080000)
194008 && iOfst>=pRbu->iOalSz
194009 ){
194010 rc = SQLITE_OK0;
194011 memset(zBuf, 0, iAmt);
194012 }else{
194013 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
194014#if 1
194015 /* If this is being called to read the first page of the target
194016 ** database as part of an rbu vacuum operation, synthesize the
194017 ** contents of the first page if it does not yet exist. Otherwise,
194018 ** SQLite will not check for a *-wal file. */
194019 if( pRbu && rbuIsVacuum(pRbu)
194020 && rc==SQLITE_IOERR_SHORT_READ(10 | (2<<8)) && iOfst==0
194021 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100)
194022 && pRbu->rc==SQLITE_OK0
194023 ){
194024 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
194025 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
194026 if( rc==SQLITE_OK0 ){
194027 u8 *aBuf = (u8*)zBuf;
194028 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
194029 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
194030 rbuPutU32(&aBuf[36], 0); /* number of free pages */
194031 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
194032 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
194033 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
194034
194035 if( iAmt>100 ){
194036 memset(&aBuf[100], 0, iAmt-100);
194037 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
194038 aBuf[100] = 0x0D;
194039 }
194040 }
194041 }
194042#endif
194043 }
194044 if( rc==SQLITE_OK0 && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100) ){
194045 /* These look like magic numbers. But they are stable, as they are part
194046 ** of the definition of the SQLite file format, which may not change. */
194047 u8 *pBuf = (u8*)zBuf;
194048 p->iCookie = rbuGetU32(&pBuf[24]);
194049 p->iWriteVer = pBuf[19];
194050 }
194051 }
194052 return rc;
194053}
194054
194055/*
194056** Write data to an rbuVfs-file.
194057*/
194058static int rbuVfsWrite(
194059 sqlite3_file *pFile,
194060 const void *zBuf,
194061 int iAmt,
194062 sqlite_int64 iOfst
194063){
194064 rbu_file *p = (rbu_file*)pFile;
194065 sqlite3rbu *pRbu = p->pRbu;
194066 int rc;
194067
194068 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
194069 assert( p->openFlags & SQLITE_OPEN_MAIN_DB )((void) (0));
194070 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
194071 }else{
194072 if( pRbu ){
194073 if( pRbu->eStage==RBU_STAGE_OAL
194074 && (p->openFlags & SQLITE_OPEN_WAL0x00080000)
194075 && iOfst>=pRbu->iOalSz
194076 ){
194077 pRbu->iOalSz = iAmt + iOfst;
194078 }else if( p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008 ){
194079 i64 szNew = iAmt+iOfst;
194080 if( szNew>p->sz ){
194081 rc = rbuUpdateTempSize(p, szNew);
194082 if( rc!=SQLITE_OK0 ) return rc;
194083 }
194084 }
194085 }
194086 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
194087 if( rc==SQLITE_OK0 && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100) ){
194088 /* These look like magic numbers. But they are stable, as they are part
194089 ** of the definition of the SQLite file format, which may not change. */
194090 u8 *pBuf = (u8*)zBuf;
194091 p->iCookie = rbuGetU32(&pBuf[24]);
194092 p->iWriteVer = pBuf[19];
194093 }
194094 }
194095 return rc;
194096}
194097
194098/*
194099** Truncate an rbuVfs-file.
194100*/
194101static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
194102 rbu_file *p = (rbu_file*)pFile;
194103 if( (p->openFlags & SQLITE_OPEN_DELETEONCLOSE0x00000008) && p->pRbu ){
194104 int rc = rbuUpdateTempSize(p, size);
194105 if( rc!=SQLITE_OK0 ) return rc;
194106 }
194107 return p->pReal->pMethods->xTruncate(p->pReal, size);
194108}
194109
194110/*
194111** Sync an rbuVfs-file.
194112*/
194113static int rbuVfsSync(sqlite3_file *pFile, int flags){
194114 rbu_file *p = (rbu_file *)pFile;
194115 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
194116 if( p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100 ){
194117 return SQLITE_INTERNAL2;
194118 }
194119 return SQLITE_OK0;
194120 }
194121 return p->pReal->pMethods->xSync(p->pReal, flags);
194122}
194123
194124/*
194125** Return the current file-size of an rbuVfs-file.
194126*/
194127static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
194128 rbu_file *p = (rbu_file *)pFile;
194129 int rc;
194130 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
194131
194132 /* If this is an RBU vacuum operation and this is the target database,
194133 ** pretend that it has at least one page. Otherwise, SQLite will not
194134 ** check for the existance of a *-wal file. rbuVfsRead() contains
194135 ** similar logic. */
194136 if( rc==SQLITE_OK0 && *pSize==0
194137 && p->pRbu && rbuIsVacuum(p->pRbu)
194138 && (p->openFlags & SQLITE_OPEN_MAIN_DB0x00000100)
194139 ){
194140 *pSize = 1024;
194141 }
194142 return rc;
194143}
194144
194145/*
194146** Lock an rbuVfs-file.
194147*/
194148static int rbuVfsLock(sqlite3_file *pFile, int eLock){
194149 rbu_file *p = (rbu_file*)pFile;
194150 sqlite3rbu *pRbu = p->pRbu;
194151 int rc = SQLITE_OK0;
194152
194153 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
194154 if( eLock==SQLITE_LOCK_EXCLUSIVE4
194155 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
194156 ){
194157 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
194158 ** prevents it from checkpointing the database from sqlite3_close(). */
194159 rc = SQLITE_BUSY5;
194160 }else{
194161 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
194162 }
194163
194164 return rc;
194165}
194166
194167/*
194168** Unlock an rbuVfs-file.
194169*/
194170static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
194171 rbu_file *p = (rbu_file *)pFile;
194172 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
194173}
194174
194175/*
194176** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
194177*/
194178static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
194179 rbu_file *p = (rbu_file *)pFile;
194180 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
194181}
194182
194183/*
194184** File control method. For custom operations on an rbuVfs-file.
194185*/
194186static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
194187 rbu_file *p = (rbu_file *)pFile;
194188 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
194189 int rc;
194190
194191 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)((void) (0))
194192 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)((void) (0))
194193 )((void) (0));
194194 if( op==SQLITE_FCNTL_RBU26 ){
194195 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
194196
194197 /* First try to find another RBU vfs lower down in the vfs stack. If
194198 ** one is found, this vfs will operate in pass-through mode. The lower
194199 ** level vfs will do the special RBU handling. */
194200 rc = xControl(p->pReal, op, pArg);
194201
194202 if( rc==SQLITE_NOTFOUND12 ){
194203 /* Now search for a zipvfs instance lower down in the VFS stack. If
194204 ** one is found, this is an error. */
194205 void *dummy = 0;
194206 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS25, &dummy);
194207 if( rc==SQLITE_OK0 ){
194208 rc = SQLITE_ERROR1;
194209 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
194210 }else if( rc==SQLITE_NOTFOUND12 ){
194211 pRbu->pTargetFd = p;
194212 p->pRbu = pRbu;
194213 rbuMainlistAdd(p);
194214 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
194215 rc = SQLITE_OK0;
194216 }
194217 }
194218 return rc;
194219 }
194220 else if( op==SQLITE_FCNTL_RBUCNT ){
194221 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
194222 pRbu->nRbu++;
194223 pRbu->pRbuFd = p;
194224 p->bNolock = 1;
194225 }
194226
194227 rc = xControl(p->pReal, op, pArg);
194228 if( rc==SQLITE_OK0 && op==SQLITE_FCNTL_VFSNAME12 ){
194229 rbu_vfs *pRbuVfs = p->pRbuVfs;
194230 char *zIn = *(char**)pArg;
194231 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
194232 *(char**)pArg = zOut;
194233 if( zOut==0 ) rc = SQLITE_NOMEM7;
194234 }
194235
194236 return rc;
194237}
194238
194239/*
194240** Return the sector-size in bytes for an rbuVfs-file.
194241*/
194242static int rbuVfsSectorSize(sqlite3_file *pFile){
194243 rbu_file *p = (rbu_file *)pFile;
194244 return p->pReal->pMethods->xSectorSize(p->pReal);
194245}
194246
194247/*
194248** Return the device characteristic flags supported by an rbuVfs-file.
194249*/
194250static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
194251 rbu_file *p = (rbu_file *)pFile;
194252 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
194253}
194254
194255/*
194256** Take or release a shared-memory lock.
194257*/
194258static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
194259 rbu_file *p = (rbu_file*)pFile;
194260 sqlite3rbu *pRbu = p->pRbu;
194261 int rc = SQLITE_OK0;
194262
194263#ifdef SQLITE_AMALGAMATION1
194264 assert( WAL_CKPT_LOCK==1 )((void) (0));
194265#endif
194266
194267 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
194268 if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
194269 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
194270 ** taking this lock also prevents any checkpoints from occurring.
194271 ** todo: really, it's not clear why this might occur, as
194272 ** wal_autocheckpoint ought to be turned off. */
194273 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY5;
194274 }else{
194275 int bCapture = 0;
194276 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
194277 bCapture = 1;
194278 }
194279
194280 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK1) ){
194281 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
194282 if( bCapture && rc==SQLITE_OK0 ){
194283 pRbu->mLock |= (1 << ofst);
194284 }
194285 }
194286 }
194287
194288 return rc;
194289}
194290
194291/*
194292** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
194293*/
194294static int rbuVfsShmMap(
194295 sqlite3_file *pFile,
194296 int iRegion,
194297 int szRegion,
194298 int isWrite,
194299 void volatile **pp
194300){
194301 rbu_file *p = (rbu_file*)pFile;
194302 int rc = SQLITE_OK0;
194303 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
194304
194305 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
194306 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
194307 ** instead of a file on disk. */
194308 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
194309 if( eStage==RBU_STAGE_OAL ){
194310 sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
194311 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
194312
194313 /* This is an RBU connection that uses its own heap memory for the
194314 ** pages of the *-shm file. Since no other process can have run
194315 ** recovery, the connection must request *-shm pages in order
194316 ** from start to finish. */
194317 assert( iRegion==p->nShm )((void) (0));
194318 if( apNew==0 ){
194319 rc = SQLITE_NOMEM7;
194320 }else{
194321 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
194322 p->apShm = apNew;
194323 p->nShm = iRegion+1;
194324 }
194325
194326 if( rc==SQLITE_OK0 ){
194327 char *pNew = (char*)sqlite3_malloc64(szRegion);
194328 if( pNew==0 ){
194329 rc = SQLITE_NOMEM7;
194330 }else{
194331 memset(pNew, 0, szRegion);
194332 p->apShm[iRegion] = pNew;
194333 }
194334 }
194335
194336 if( rc==SQLITE_OK0 ){
194337 *pp = p->apShm[iRegion];
194338 }else{
194339 *pp = 0;
194340 }
194341 }else{
194342 assert( p->apShm==0 )((void) (0));
194343 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
194344 }
194345
194346 return rc;
194347}
194348
194349/*
194350** Memory barrier.
194351*/
194352static void rbuVfsShmBarrier(sqlite3_file *pFile){
194353 rbu_file *p = (rbu_file *)pFile;
194354 p->pReal->pMethods->xShmBarrier(p->pReal);
194355}
194356
194357/*
194358** The xShmUnmap method.
194359*/
194360static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
194361 rbu_file *p = (rbu_file*)pFile;
194362 int rc = SQLITE_OK0;
194363 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
194364
194365 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) )((void) (0));
194366 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
194367 /* no-op */
194368 }else{
194369 /* Release the checkpointer and writer locks */
194370 rbuUnlockShm(p);
194371 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
194372 }
194373 return rc;
194374}
194375
194376/*
194377** A main database named zName has just been opened. The following
194378** function returns a pointer to a buffer owned by SQLite that contains
194379** the name of the *-wal file this db connection will use. SQLite
194380** happens to pass a pointer to this buffer when using xAccess()
194381** or xOpen() to operate on the *-wal file.
194382*/
194383static const char *rbuMainToWal(const char *zName, int flags){
194384 int n = (int)strlen(zName);
194385 const char *z = &zName[n];
194386 if( flags & SQLITE_OPEN_URI0x00000040 ){
194387 int odd = 0;
194388 while( 1 ){
194389 if( z[0]==0 ){
194390 odd = 1 - odd;
194391 if( odd && z[1]==0 ) break;
194392 }
194393 z++;
194394 }
194395 z += 2;
194396 }else{
194397 while( *z==0 ) z++;
194398 }
194399 z += (n + 8 + 1);
194400 return z;
194401}
194402
194403/*
194404** Open an rbu file handle.
194405*/
194406static int rbuVfsOpen(
194407 sqlite3_vfs *pVfs,
194408 const char *zName,
194409 sqlite3_file *pFile,
194410 int flags,
194411 int *pOutFlags
194412){
194413 static sqlite3_io_methods rbuvfs_io_methods = {
194414 2, /* iVersion */
194415 rbuVfsClose, /* xClose */
194416 rbuVfsRead, /* xRead */
194417 rbuVfsWrite, /* xWrite */
194418 rbuVfsTruncate, /* xTruncate */
194419 rbuVfsSync, /* xSync */
194420 rbuVfsFileSize, /* xFileSize */
194421 rbuVfsLock, /* xLock */
194422 rbuVfsUnlock, /* xUnlock */
194423 rbuVfsCheckReservedLock, /* xCheckReservedLock */
194424 rbuVfsFileControl, /* xFileControl */
194425 rbuVfsSectorSize, /* xSectorSize */
194426 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
194427 rbuVfsShmMap, /* xShmMap */
194428 rbuVfsShmLock, /* xShmLock */
194429 rbuVfsShmBarrier, /* xShmBarrier */
194430 rbuVfsShmUnmap, /* xShmUnmap */
194431 0, 0 /* xFetch, xUnfetch */
194432 };
194433 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
194434 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
194435 rbu_file *pFd = (rbu_file *)pFile;
194436 int rc = SQLITE_OK0;
194437 const char *zOpen = zName;
194438 int oflags = flags;
194439
194440 memset(pFd, 0, sizeof(rbu_file));
194441 pFd->pReal = (sqlite3_file*)&pFd[1];
194442 pFd->pRbuVfs = pRbuVfs;
194443 pFd->openFlags = flags;
194444 if( zName ){
194445 if( flags & SQLITE_OPEN_MAIN_DB0x00000100 ){
194446 /* A main database has just been opened. The following block sets
194447 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
194448 ** the name of the *-wal file this db connection will use. SQLite
194449 ** happens to pass a pointer to this buffer when using xAccess()
194450 ** or xOpen() to operate on the *-wal file. */
194451 pFd->zWal = rbuMainToWal(zName, flags);
194452 }
194453 else if( flags & SQLITE_OPEN_WAL0x00080000 ){
194454 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName, 0);
194455 if( pDb ){
194456 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
194457 /* This call is to open a *-wal file. Intead, open the *-oal. This
194458 ** code ensures that the string passed to xOpen() is terminated by a
194459 ** pair of '\0' bytes in case the VFS attempts to extract a URI
194460 ** parameter from it. */
194461 const char *zBase = zName;
194462 size_t nCopy;
194463 char *zCopy;
194464 if( rbuIsVacuum(pDb->pRbu) ){
194465 zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
194466 zBase = rbuMainToWal(zBase, SQLITE_OPEN_URI0x00000040);
194467 }
194468 nCopy = strlen(zBase);
194469 zCopy = sqlite3_malloc64(nCopy+2);
194470 if( zCopy ){
194471 memcpy(zCopy, zBase, nCopy);
194472 zCopy[nCopy-3] = 'o';
194473 zCopy[nCopy] = '\0';
194474 zCopy[nCopy+1] = '\0';
194475 zOpen = (const char*)(pFd->zDel = zCopy);
194476 }else{
194477 rc = SQLITE_NOMEM7;
194478 }
194479 pFd->pRbu = pDb->pRbu;
194480 }
194481 pDb->pWalFd = pFd;
194482 }
194483 }
194484 }else{
194485 pFd->pRbu = pRbuVfs->pRbu;
194486 }
194487
194488 if( oflags & SQLITE_OPEN_MAIN_DB0x00000100
194489 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
194490 ){
194491 assert( oflags & SQLITE_OPEN_MAIN_DB )((void) (0));
194492 oflags = SQLITE_OPEN_TEMP_DB0x00000200 | SQLITE_OPEN_READWRITE0x00000002 | SQLITE_OPEN_CREATE0x00000004 |
194493 SQLITE_OPEN_EXCLUSIVE0x00000010 | SQLITE_OPEN_DELETEONCLOSE0x00000008;
194494 zOpen = 0;
194495 }
194496
194497 if( rc==SQLITE_OK0 ){
194498 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
194499 }
194500 if( pFd->pReal->pMethods ){
194501 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
194502 ** pointer and, if the file is a main database file, link it into the
194503 ** mutex protected linked list of all such files. */
194504 pFile->pMethods = &rbuvfs_io_methods;
194505 if( flags & SQLITE_OPEN_MAIN_DB0x00000100 ){
194506 rbuMainlistAdd(pFd);
194507 }
194508 }else{
194509 sqlite3_free(pFd->zDel);
194510 }
194511
194512 return rc;
194513}
194514
194515/*
194516** Delete the file located at zPath.
194517*/
194518static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
194519 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
194520 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
194521}
194522
194523/*
194524** Test for access permissions. Return true if the requested permission
194525** is available, or false otherwise.
194526*/
194527static int rbuVfsAccess(
194528 sqlite3_vfs *pVfs,
194529 const char *zPath,
194530 int flags,
194531 int *pResOut
194532){
194533 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
194534 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
194535 int rc;
194536
194537 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
194538
194539 /* If this call is to check if a *-wal file associated with an RBU target
194540 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
194541 ** the following special handling is activated:
194542 **
194543 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
194544 ** ensures that the RBU extension never tries to update a database
194545 ** in wal mode, even if the first page of the database file has
194546 ** been damaged.
194547 **
194548 ** b) if the *-wal file does not exist, claim that it does anyway,
194549 ** causing SQLite to call xOpen() to open it. This call will also
194550 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
194551 ** file opened instead.
194552 */
194553 if( rc==SQLITE_OK0 && flags==SQLITE_ACCESS_EXISTS0 ){
194554 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
194555 if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
194556 assert( pDb->pRbu )((void) (0));
194557 if( *pResOut ){
194558 rc = SQLITE_CANTOPEN14;
194559 }else{
194560 sqlite3_int64 sz = 0;
194561 rc = rbuVfsFileSize(&pDb->base, &sz);
194562 *pResOut = (sz>0);
194563 }
194564 }
194565 }
194566
194567 return rc;
194568}
194569
194570/*
194571** Populate buffer zOut with the full canonical pathname corresponding
194572** to the pathname in zPath. zOut is guaranteed to point to a buffer
194573** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
194574*/
194575static int rbuVfsFullPathname(
194576 sqlite3_vfs *pVfs,
194577 const char *zPath,
194578 int nOut,
194579 char *zOut
194580){
194581 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
194582 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
194583}
194584
194585#ifndef SQLITE_OMIT_LOAD_EXTENSION
194586/*
194587** Open the dynamic library located at zPath and return a handle.
194588*/
194589static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
194590 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
194591 return pRealVfs->xDlOpen(pRealVfs, zPath);
194592}
194593
194594/*
194595** Populate the buffer zErrMsg (size nByte bytes) with a human readable
194596** utf-8 string describing the most recent error encountered associated
194597** with dynamic libraries.
194598*/
194599static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
194600 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
194601 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
194602}
194603
194604/*
194605** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
194606*/
194607static void (*rbuVfsDlSym(
194608 sqlite3_vfs *pVfs,
194609 void *pArg,
194610 const char *zSym
194611))(void){
194612 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
194613 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
194614}
194615
194616/*
194617** Close the dynamic library handle pHandle.
194618*/
194619static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
194620 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
194621 pRealVfs->xDlClose(pRealVfs, pHandle);
194622}
194623#endif /* SQLITE_OMIT_LOAD_EXTENSION */
194624
194625/*
194626** Populate the buffer pointed to by zBufOut with nByte bytes of
194627** random data.
194628*/
194629static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
194630 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
194631 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
194632}
194633
194634/*
194635** Sleep for nMicro microseconds. Return the number of microseconds
194636** actually slept.
194637*/
194638static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
194639 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
194640 return pRealVfs->xSleep(pRealVfs, nMicro);
194641}
194642
194643/*
194644** Return the current time as a Julian Day number in *pTimeOut.
194645*/
194646static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
194647 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
194648 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
194649}
194650
194651/*
194652** No-op.
194653*/
194654static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
194655 return 0;
194656}
194657
194658/*
194659** Deregister and destroy an RBU vfs created by an earlier call to
194660** sqlite3rbu_create_vfs().
194661*/
194662SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
194663 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
194664 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
194665 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
194666 sqlite3_vfs_unregister(pVfs);
194667 sqlite3_free(pVfs);
194668 }
194669}
194670
194671/*
194672** Create an RBU VFS named zName that accesses the underlying file-system
194673** via existing VFS zParent. The new object is registered as a non-default
194674** VFS with SQLite before returning.
194675*/
194676SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
194677
194678 /* Template for VFS */
194679 static sqlite3_vfs vfs_template = {
194680 1, /* iVersion */
194681 0, /* szOsFile */
194682 0, /* mxPathname */
194683 0, /* pNext */
194684 0, /* zName */
194685 0, /* pAppData */
194686 rbuVfsOpen, /* xOpen */
194687 rbuVfsDelete, /* xDelete */
194688 rbuVfsAccess, /* xAccess */
194689 rbuVfsFullPathname, /* xFullPathname */
194690
194691#ifndef SQLITE_OMIT_LOAD_EXTENSION
194692 rbuVfsDlOpen, /* xDlOpen */
194693 rbuVfsDlError, /* xDlError */
194694 rbuVfsDlSym, /* xDlSym */
194695 rbuVfsDlClose, /* xDlClose */
194696#else
194697 0, 0, 0, 0,
194698#endif
194699
194700 rbuVfsRandomness, /* xRandomness */
194701 rbuVfsSleep, /* xSleep */
194702 rbuVfsCurrentTime, /* xCurrentTime */
194703 rbuVfsGetLastError, /* xGetLastError */
194704 0, /* xCurrentTimeInt64 (version 2) */
194705 0, 0, 0 /* Unimplemented version 3 methods */
194706 };
194707
194708 rbu_vfs *pNew = 0; /* Newly allocated VFS */
194709 int rc = SQLITE_OK0;
194710 size_t nName;
194711 size_t nByte;
194712
194713 nName = strlen(zName);
194714 nByte = sizeof(rbu_vfs) + nName + 1;
194715 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
194716 if( pNew==0 ){
194717 rc = SQLITE_NOMEM7;
194718 }else{
194719 sqlite3_vfs *pParent; /* Parent VFS */
194720 memset(pNew, 0, nByte);
194721 pParent = sqlite3_vfs_find(zParent);
194722 if( pParent==0 ){
194723 rc = SQLITE_NOTFOUND12;
194724 }else{
194725 char *zSpace;
194726 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
194727 pNew->base.mxPathname = pParent->mxPathname;
194728 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
194729 pNew->pRealVfs = pParent;
194730 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
194731 memcpy(zSpace, zName, nName);
194732
194733 /* Allocate the mutex and register the new VFS (not as the default) */
194734 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE1);
194735 if( pNew->mutex==0 ){
194736 rc = SQLITE_NOMEM7;
194737 }else{
194738 rc = sqlite3_vfs_register(&pNew->base, 0);
194739 }
194740 }
194741
194742 if( rc!=SQLITE_OK0 ){
194743 sqlite3_mutex_free(pNew->mutex);
194744 sqlite3_free(pNew);
194745 }
194746 }
194747
194748 return rc;
194749}
194750
194751/*
194752** Configure the aggregate temp file size limit for this RBU handle.
194753*/
194754SQLITE_API sqlite3_int64 sqlite3rbu_temp_size_limit(sqlite3rbu *pRbu, sqlite3_int64 n){
194755 if( n>=0 ){
194756 pRbu->szTempLimit = n;
194757 }
194758 return pRbu->szTempLimit;
194759}
194760
194761SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu){
194762 return pRbu->szTemp;
194763}
194764
194765
194766/**************************************************************************/
194767
194768#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
194769
194770/************** End of sqlite3rbu.c ******************************************/
194771/************** Begin file dbstat.c ******************************************/
194772/*
194773** 2010 July 12
194774**
194775** The author disclaims copyright to this source code. In place of
194776** a legal notice, here is a blessing:
194777**
194778** May you do good and not evil.
194779** May you find forgiveness for yourself and forgive others.
194780** May you share freely, never taking more than you give.
194781**
194782******************************************************************************
194783**
194784** This file contains an implementation of the "dbstat" virtual table.
194785**
194786** The dbstat virtual table is used to extract low-level formatting
194787** information from an SQLite database in order to implement the
194788** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
194789** for an example implementation.
194790**
194791** Additional information is available on the "dbstat.html" page of the
194792** official SQLite documentation.
194793*/
194794
194795/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
194796#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
194797 && !defined(SQLITE_OMIT_VIRTUALTABLE)
194798
194799/*
194800** Page paths:
194801**
194802** The value of the 'path' column describes the path taken from the
194803** root-node of the b-tree structure to each page. The value of the
194804** root-node path is '/'.
194805**
194806** The value of the path for the left-most child page of the root of
194807** a b-tree is '/000/'. (Btrees store content ordered from left to right
194808** so the pages to the left have smaller keys than the pages to the right.)
194809** The next to left-most child of the root page is
194810** '/001', and so on, each sibling page identified by a 3-digit hex
194811** value. The children of the 451st left-most sibling have paths such
194812** as '/1c2/000/, '/1c2/001/' etc.
194813**
194814** Overflow pages are specified by appending a '+' character and a
194815** six-digit hexadecimal value to the path to the cell they are linked
194816** from. For example, the three overflow pages in a chain linked from
194817** the left-most cell of the 450th child of the root page are identified
194818** by the paths:
194819**
194820** '/1c2/000+000000' // First page in overflow chain
194821** '/1c2/000+000001' // Second page in overflow chain
194822** '/1c2/000+000002' // Third page in overflow chain
194823**
194824** If the paths are sorted using the BINARY collation sequence, then
194825** the overflow pages associated with a cell will appear earlier in the
194826** sort-order than its child page:
194827**
194828** '/1c2/000/' // Left-most child of 451st child of root
194829*/
194830#define VTAB_SCHEMA \
194831 "CREATE TABLE xx( " \
194832 " name TEXT, /* Name of table or index */" \
194833 " path TEXT, /* Path to page from root */" \
194834 " pageno INTEGER, /* Page number */" \
194835 " pagetype TEXT, /* 'internal', 'leaf' or 'overflow' */" \
194836 " ncell INTEGER, /* Cells on page (0 for overflow) */" \
194837 " payload INTEGER, /* Bytes of payload on this page */" \
194838 " unused INTEGER, /* Bytes of unused space on this page */" \
194839 " mx_payload INTEGER, /* Largest payload size of all cells */" \
194840 " pgoffset INTEGER, /* Offset of page in file */" \
194841 " pgsize INTEGER, /* Size of the page */" \
194842 " schema TEXT HIDDEN /* Database schema being analyzed */" \
194843 ");"
194844
194845
194846typedef struct StatTable StatTable;
194847typedef struct StatCursor StatCursor;
194848typedef struct StatPage StatPage;
194849typedef struct StatCell StatCell;
194850
194851struct StatCell {
194852 int nLocal; /* Bytes of local payload */
194853 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
194854 int nOvfl; /* Entries in aOvfl[] */
194855 u32 *aOvfl; /* Array of overflow page numbers */
194856 int nLastOvfl; /* Bytes of payload on final overflow page */
194857 int iOvfl; /* Iterates through aOvfl[] */
194858};
194859
194860struct StatPage {
194861 u32 iPgno;
194862 DbPage *pPg;
194863 int iCell;
194864
194865 char *zPath; /* Path to this page */
194866
194867 /* Variables populated by statDecodePage(): */
194868 u8 flags; /* Copy of flags byte */
194869 int nCell; /* Number of cells on page */
194870 int nUnused; /* Number of unused bytes on page */
194871 StatCell *aCell; /* Array of parsed cells */
194872 u32 iRightChildPg; /* Right-child page number (or 0) */
194873 int nMxPayload; /* Largest payload of any cell on this page */
194874};
194875
194876struct StatCursor {
194877 sqlite3_vtab_cursor base;
194878 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
194879 int isEof; /* After pStmt has returned SQLITE_DONE */
194880 int iDb; /* Schema used for this query */
194881
194882 StatPage aPage[32];
194883 int iPage; /* Current entry in aPage[] */
194884
194885 /* Values to return. */
194886 char *zName; /* Value of 'name' column */
194887 char *zPath; /* Value of 'path' column */
194888 u32 iPageno; /* Value of 'pageno' column */
194889 char *zPagetype; /* Value of 'pagetype' column */
194890 int nCell; /* Value of 'ncell' column */
194891 int nPayload; /* Value of 'payload' column */
194892 int nUnused; /* Value of 'unused' column */
194893 int nMxPayload; /* Value of 'mx_payload' column */
194894 i64 iOffset; /* Value of 'pgOffset' column */
194895 int szPage; /* Value of 'pgSize' column */
194896};
194897
194898struct StatTable {
194899 sqlite3_vtab base;
194900 sqlite3 *db;
194901 int iDb; /* Index of database to analyze */
194902};
194903
194904#ifndef get2byte
194905# define get2byte(x)((x)[0]<<8 | (x)[1]) ((x)[0]<<8 | (x)[1])
194906#endif
194907
194908/*
194909** Connect to or create a statvfs virtual table.
194910*/
194911static int statConnect(
194912 sqlite3 *db,
194913 void *pAux,
194914 int argc, const char *const*argv,
194915 sqlite3_vtab **ppVtab,
194916 char **pzErr
194917){
194918 StatTable *pTab = 0;
194919 int rc = SQLITE_OK0;
194920 int iDb;
194921
194922 if( argc>=4 ){
194923 Token nm;
194924 sqlite3TokenInit(&nm, (char*)argv[3]);
194925 iDb = sqlite3FindDb(db, &nm);
194926 if( iDb<0 ){
194927 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
194928 return SQLITE_ERROR1;
194929 }
194930 }else{
194931 iDb = 0;
194932 }
194933 rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
194934 if( rc==SQLITE_OK0 ){
194935 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
194936 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT7;
194937 }
194938
194939 assert( rc==SQLITE_OK || pTab==0 )((void) (0));
194940 if( rc==SQLITE_OK0 ){
194941 memset(pTab, 0, sizeof(StatTable));
194942 pTab->db = db;
194943 pTab->iDb = iDb;
194944 }
194945
194946 *ppVtab = (sqlite3_vtab*)pTab;
194947 return rc;
194948}
194949
194950/*
194951** Disconnect from or destroy a statvfs virtual table.
194952*/
194953static int statDisconnect(sqlite3_vtab *pVtab){
194954 sqlite3_free(pVtab);
194955 return SQLITE_OK0;
194956}
194957
194958/*
194959** There is no "best-index". This virtual table always does a linear
194960** scan. However, a schema=? constraint should cause this table to
194961** operate on a different database schema, so check for it.
194962**
194963** idxNum is normally 0, but will be 1 if a schema=? constraint exists.
194964*/
194965static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
194966 int i;
194967
194968 /* Look for a valid schema=? constraint. If found, change the idxNum to
194969 ** 1 and request the value of that constraint be sent to xFilter. And
194970 ** lower the cost estimate to encourage the constrained version to be
194971 ** used.
194972 */
194973 for(i=0; i<pIdxInfo->nConstraint; i++){
194974 if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
194975 if( pIdxInfo->aConstraint[i].usable==0 ) return SQLITE_CONSTRAINT19;
194976 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
194977 pIdxInfo->idxNum = 1;
194978 pIdxInfo->estimatedCost = 1.0;
194979 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
194980 pIdxInfo->aConstraintUsage[i].omit = 1;
194981 break;
194982 }
194983
194984
194985 /* Records are always returned in ascending order of (name, path).
194986 ** If this will satisfy the client, set the orderByConsumed flag so that
194987 ** SQLite does not do an external sort.
194988 */
194989 if( ( pIdxInfo->nOrderBy==1
194990 && pIdxInfo->aOrderBy[0].iColumn==0
194991 && pIdxInfo->aOrderBy[0].desc==0
194992 ) ||
194993 ( pIdxInfo->nOrderBy==2
194994 && pIdxInfo->aOrderBy[0].iColumn==0
194995 && pIdxInfo->aOrderBy[0].desc==0
194996 && pIdxInfo->aOrderBy[1].iColumn==1
194997 && pIdxInfo->aOrderBy[1].desc==0
194998 )
194999 ){
195000 pIdxInfo->orderByConsumed = 1;
195001 }
195002
195003 return SQLITE_OK0;
195004}
195005
195006/*
195007** Open a new statvfs cursor.
195008*/
195009static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
195010 StatTable *pTab = (StatTable *)pVTab;
195011 StatCursor *pCsr;
195012
195013 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
195014 if( pCsr==0 ){
195015 return SQLITE_NOMEM_BKPT7;
195016 }else{
195017 memset(pCsr, 0, sizeof(StatCursor));
195018 pCsr->base.pVtab = pVTab;
195019 pCsr->iDb = pTab->iDb;
195020 }
195021
195022 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
195023 return SQLITE_OK0;
195024}
195025
195026static void statClearCells(StatPage *p){
195027 int i;
195028 if( p->aCell ){
195029 for(i=0; i<p->nCell; i++){
195030 sqlite3_free(p->aCell[i].aOvfl);
195031 }
195032 sqlite3_free(p->aCell);
195033 }
195034 p->nCell = 0;
195035 p->aCell = 0;
195036}
195037
195038static void statClearPage(StatPage *p){
195039 statClearCells(p);
195040 sqlite3PagerUnref(p->pPg);
195041 sqlite3_free(p->zPath);
195042 memset(p, 0, sizeof(StatPage));
195043}
195044
195045static void statResetCsr(StatCursor *pCsr){
195046 int i;
195047 sqlite3_reset(pCsr->pStmt);
195048 for(i=0; i<ArraySize(pCsr->aPage)((int)(sizeof(pCsr->aPage)/sizeof(pCsr->aPage[0]))); i++){
195049 statClearPage(&pCsr->aPage[i]);
195050 }
195051 pCsr->iPage = 0;
195052 sqlite3_free(pCsr->zPath);
195053 pCsr->zPath = 0;
195054 pCsr->isEof = 0;
195055}
195056
195057/*
195058** Close a statvfs cursor.
195059*/
195060static int statClose(sqlite3_vtab_cursor *pCursor){
195061 StatCursor *pCsr = (StatCursor *)pCursor;
195062 statResetCsr(pCsr);
195063 sqlite3_finalize(pCsr->pStmt);
195064 sqlite3_free(pCsr);
195065 return SQLITE_OK0;
195066}
195067
195068static void getLocalPayload(
195069 int nUsable, /* Usable bytes per page */
195070 u8 flags, /* Page flags */
195071 int nTotal, /* Total record (payload) size */
195072 int *pnLocal /* OUT: Bytes stored locally */
195073){
195074 int nLocal;
195075 int nMinLocal;
195076 int nMaxLocal;
195077
195078 if( flags==0x0D ){ /* Table leaf node */
195079 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
195080 nMaxLocal = nUsable - 35;
195081 }else{ /* Index interior and leaf nodes */
195082 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
195083 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
195084 }
195085
195086 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
195087 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
195088 *pnLocal = nLocal;
195089}
195090
195091static int statDecodePage(Btree *pBt, StatPage *p){
195092 int nUnused;
195093 int iOff;
195094 int nHdr;
195095 int isLeaf;
195096 int szPage;
195097
195098 u8 *aData = sqlite3PagerGetData(p->pPg);
195099 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
195100
195101 p->flags = aHdr[0];
195102 if( p->flags==0x0A || p->flags==0x0D ){
195103 isLeaf = 1;
195104 nHdr = 8;
195105 }else if( p->flags==0x05 || p->flags==0x02 ){
195106 isLeaf = 0;
195107 nHdr = 12;
195108 }else{
195109 goto statPageIsCorrupt;
195110 }
195111 if( p->iPgno==1 ) nHdr += 100;
195112 p->nCell = get2byte(&aHdr[3])((&aHdr[3])[0]<<8 | (&aHdr[3])[1]);
195113 p->nMxPayload = 0;
195114 szPage = sqlite3BtreeGetPageSize(pBt);
195115
195116 nUnused = get2byte(&aHdr[5])((&aHdr[5])[0]<<8 | (&aHdr[5])[1]) - nHdr - 2*p->nCell;
195117 nUnused += (int)aHdr[7];
195118 iOff = get2byte(&aHdr[1])((&aHdr[1])[0]<<8 | (&aHdr[1])[1]);
195119 while( iOff ){
195120 int iNext;
195121 if( iOff>=szPage ) goto statPageIsCorrupt;
195122 nUnused += get2byte(&aData[iOff+2])((&aData[iOff+2])[0]<<8 | (&aData[iOff+2])[1]);
195123 iNext = get2byte(&aData[iOff])((&aData[iOff])[0]<<8 | (&aData[iOff])[1]);
195124 if( iNext<iOff+4 && iNext>0 ) goto statPageIsCorrupt;
195125 iOff = iNext;
195126 }
195127 p->nUnused = nUnused;
195128 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
195129
195130 if( p->nCell ){
195131 int i; /* Used to iterate through cells */
195132 int nUsable; /* Usable bytes per page */
195133
195134 sqlite3BtreeEnter(pBt);
195135 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
195136 sqlite3BtreeLeave(pBt);
195137 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
195138 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT7;
195139 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
195140
195141 for(i=0; i<p->nCell; i++){
195142 StatCell *pCell = &p->aCell[i];
195143
195144 iOff = get2byte(&aData[nHdr+i*2])((&aData[nHdr+i*2])[0]<<8 | (&aData[nHdr+i*2])[
1])
;
195145 if( iOff<nHdr || iOff>=szPage ) goto statPageIsCorrupt;
195146 if( !isLeaf ){
195147 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
195148 iOff += 4;
195149 }
195150 if( p->flags==0x05 ){
195151 /* A table interior node. nPayload==0. */
195152 }else{
195153 u32 nPayload; /* Bytes of payload total (local+overflow) */
195154 int nLocal; /* Bytes of payload stored locally */
195155 iOff += getVarint32(&aData[iOff], nPayload)(u8)((*(&aData[iOff])<(u8)0x80)?((nPayload)=(u32)*(&
aData[iOff])),1:sqlite3GetVarint32((&aData[iOff]),(u32 *)
&(nPayload)))
;
195156 if( p->flags==0x0D ){
195157 u64 dummy;
195158 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
195159 }
195160 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
195161 getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
195162 if( nLocal<0 ) goto statPageIsCorrupt;
195163 pCell->nLocal = nLocal;
195164 assert( nPayload>=(u32)nLocal )((void) (0));
195165 assert( nLocal<=(nUsable-35) )((void) (0));
195166 if( nPayload>(u32)nLocal ){
195167 int j;
195168 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
195169 if( iOff+nLocal>nUsable ) goto statPageIsCorrupt;
195170 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
195171 pCell->nOvfl = nOvfl;
195172 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
195173 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT7;
195174 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
195175 for(j=1; j<nOvfl; j++){
195176 int rc;
195177 u32 iPrev = pCell->aOvfl[j-1];
195178 DbPage *pPg = 0;
195179 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
195180 if( rc!=SQLITE_OK0 ){
195181 assert( pPg==0 )((void) (0));
195182 return rc;
195183 }
195184 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
195185 sqlite3PagerUnref(pPg);
195186 }
195187 }
195188 }
195189 }
195190 }
195191
195192 return SQLITE_OK0;
195193
195194statPageIsCorrupt:
195195 p->flags = 0;
195196 statClearCells(p);
195197 return SQLITE_OK0;
195198}
195199
195200/*
195201** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
195202** the current value of pCsr->iPageno.
195203*/
195204static void statSizeAndOffset(StatCursor *pCsr){
195205 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
195206 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
195207 Pager *pPager = sqlite3BtreePager(pBt);
195208 sqlite3_file *fd;
195209 sqlite3_int64 x[2];
195210
195211 /* The default page size and offset */
195212 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
195213 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
195214
195215 /* If connected to a ZIPVFS backend, override the page size and
195216 ** offset with actual values obtained from ZIPVFS.
195217 */
195218 fd = sqlite3PagerFile(pPager);
195219 x[0] = pCsr->iPageno;
195220 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK0 ){
195221 pCsr->iOffset = x[0];
195222 pCsr->szPage = (int)x[1];
195223 }
195224}
195225
195226/*
195227** Move a statvfs cursor to the next entry in the file.
195228*/
195229static int statNext(sqlite3_vtab_cursor *pCursor){
195230 int rc;
195231 int nPayload;
195232 char *z;
195233 StatCursor *pCsr = (StatCursor *)pCursor;
195234 StatTable *pTab = (StatTable *)pCursor->pVtab;
195235 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
195236 Pager *pPager = sqlite3BtreePager(pBt);
195237
195238 sqlite3_free(pCsr->zPath);
195239 pCsr->zPath = 0;
195240
195241statNextRestart:
195242 if( pCsr->aPage[0].pPg==0 ){
195243 rc = sqlite3_step(pCsr->pStmt);
195244 if( rc==SQLITE_ROW100 ){
195245 int nPage;
195246 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
195247 sqlite3PagerPagecount(pPager, &nPage);
195248 if( nPage==0 ){
195249 pCsr->isEof = 1;
195250 return sqlite3_reset(pCsr->pStmt);
195251 }
195252 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
195253 pCsr->aPage[0].iPgno = iRoot;
195254 pCsr->aPage[0].iCell = 0;
195255 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
195256 pCsr->iPage = 0;
195257 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
195258 }else{
195259 pCsr->isEof = 1;
195260 return sqlite3_reset(pCsr->pStmt);
195261 }
195262 }else{
195263
195264 /* Page p itself has already been visited. */
195265 StatPage *p = &pCsr->aPage[pCsr->iPage];
195266
195267 while( p->iCell<p->nCell ){
195268 StatCell *pCell = &p->aCell[p->iCell];
195269 if( pCell->iOvfl<pCell->nOvfl ){
195270 int nUsable;
195271 sqlite3BtreeEnter(pBt);
195272 nUsable = sqlite3BtreeGetPageSize(pBt) -
195273 sqlite3BtreeGetReserveNoMutex(pBt);
195274 sqlite3BtreeLeave(pBt);
195275 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
195276 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
195277 pCsr->zPagetype = "overflow";
195278 pCsr->nCell = 0;
195279 pCsr->nMxPayload = 0;
195280 pCsr->zPath = z = sqlite3_mprintf(
195281 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
195282 );
195283 if( pCell->iOvfl<pCell->nOvfl-1 ){
195284 pCsr->nUnused = 0;
195285 pCsr->nPayload = nUsable - 4;
195286 }else{
195287 pCsr->nPayload = pCell->nLastOvfl;
195288 pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
195289 }
195290 pCell->iOvfl++;
195291 statSizeAndOffset(pCsr);
195292 return z==0 ? SQLITE_NOMEM_BKPT7 : SQLITE_OK0;
195293 }
195294 if( p->iRightChildPg ) break;
195295 p->iCell++;
195296 }
195297
195298 if( !p->iRightChildPg || p->iCell>p->nCell ){
195299 statClearPage(p);
195300 if( pCsr->iPage==0 ) return statNext(pCursor);
195301 pCsr->iPage--;
195302 goto statNextRestart; /* Tail recursion */
195303 }
195304 pCsr->iPage++;
195305 if( pCsr->iPage>=ArraySize(pCsr->aPage)((int)(sizeof(pCsr->aPage)/sizeof(pCsr->aPage[0]))) ){
195306 statResetCsr(pCsr);
195307 return SQLITE_CORRUPT_BKPTsqlite3CorruptError(195307);
195308 }
195309 assert( p==&pCsr->aPage[pCsr->iPage-1] )((void) (0));
195310
195311 if( p->iCell==p->nCell ){
195312 p[1].iPgno = p->iRightChildPg;
195313 }else{
195314 p[1].iPgno = p->aCell[p->iCell].iChildPg;
195315 }
195316 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
195317 p[1].iCell = 0;
195318 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
195319 p->iCell++;
195320 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
195321 }
195322
195323
195324 /* Populate the StatCursor fields with the values to be returned
195325 ** by the xColumn() and xRowid() methods.
195326 */
195327 if( rc==SQLITE_OK0 ){
195328 int i;
195329 StatPage *p = &pCsr->aPage[pCsr->iPage];
195330 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
195331 pCsr->iPageno = p->iPgno;
195332
195333 rc = statDecodePage(pBt, p);
195334 if( rc==SQLITE_OK0 ){
195335 statSizeAndOffset(pCsr);
195336
195337 switch( p->flags ){
195338 case 0x05: /* table internal */
195339 case 0x02: /* index internal */
195340 pCsr->zPagetype = "internal";
195341 break;
195342 case 0x0D: /* table leaf */
195343 case 0x0A: /* index leaf */
195344 pCsr->zPagetype = "leaf";
195345 break;
195346 default:
195347 pCsr->zPagetype = "corrupted";
195348 break;
195349 }
195350 pCsr->nCell = p->nCell;
195351 pCsr->nUnused = p->nUnused;
195352 pCsr->nMxPayload = p->nMxPayload;
195353 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
195354 if( z==0 ) rc = SQLITE_NOMEM_BKPT7;
195355 nPayload = 0;
195356 for(i=0; i<p->nCell; i++){
195357 nPayload += p->aCell[i].nLocal;
195358 }
195359 pCsr->nPayload = nPayload;
195360 }
195361 }
195362
195363 return rc;
195364}
195365
195366static int statEof(sqlite3_vtab_cursor *pCursor){
195367 StatCursor *pCsr = (StatCursor *)pCursor;
195368 return pCsr->isEof;
195369}
195370
195371static int statFilter(
195372 sqlite3_vtab_cursor *pCursor,
195373 int idxNum, const char *idxStr,
195374 int argc, sqlite3_value **argv
195375){
195376 StatCursor *pCsr = (StatCursor *)pCursor;
195377 StatTable *pTab = (StatTable*)(pCursor->pVtab);
195378 char *zSql;
195379 int rc = SQLITE_OK0;
195380
195381 if( idxNum==1 ){
195382 const char *zDbase = (const char*)sqlite3_value_text(argv[0]);
195383 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
195384 if( pCsr->iDb<0 ){
195385 sqlite3_free(pCursor->pVtab->zErrMsg);
195386 pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase);
195387 return pCursor->pVtab->zErrMsg ? SQLITE_ERROR1 : SQLITE_NOMEM_BKPT7;
195388 }
195389 }else{
195390 pCsr->iDb = pTab->iDb;
195391 }
195392 statResetCsr(pCsr);
195393 sqlite3_finalize(pCsr->pStmt);
195394 pCsr->pStmt = 0;
195395 zSql = sqlite3_mprintf(
195396 "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
195397 " UNION ALL "
195398 "SELECT name, rootpage, type"
195399 " FROM \"%w\".sqlite_master WHERE rootpage!=0"
195400 " ORDER BY name", pTab->db->aDb[pCsr->iDb].zDbSName);
195401 if( zSql==0 ){
195402 return SQLITE_NOMEM_BKPT7;
195403 }else{
195404 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
195405 sqlite3_free(zSql);
195406 }
195407
195408 if( rc==SQLITE_OK0 ){
195409 rc = statNext(pCursor);
195410 }
195411 return rc;
195412}
195413
195414static int statColumn(
195415 sqlite3_vtab_cursor *pCursor,
195416 sqlite3_context *ctx,
195417 int i
195418){
195419 StatCursor *pCsr = (StatCursor *)pCursor;
195420 switch( i ){
195421 case 0: /* name */
195422 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
195423 break;
195424 case 1: /* path */
195425 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
195426 break;
195427 case 2: /* pageno */
195428 sqlite3_result_int64(ctx, pCsr->iPageno);
195429 break;
195430 case 3: /* pagetype */
195431 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
195432 break;
195433 case 4: /* ncell */
195434 sqlite3_result_int(ctx, pCsr->nCell);
195435 break;
195436 case 5: /* payload */
195437 sqlite3_result_int(ctx, pCsr->nPayload);
195438 break;
195439 case 6: /* unused */
195440 sqlite3_result_int(ctx, pCsr->nUnused);
195441 break;
195442 case 7: /* mx_payload */
195443 sqlite3_result_int(ctx, pCsr->nMxPayload);
195444 break;
195445 case 8: /* pgoffset */
195446 sqlite3_result_int64(ctx, pCsr->iOffset);
195447 break;
195448 case 9: /* pgsize */
195449 sqlite3_result_int(ctx, pCsr->szPage);
195450 break;
195451 default: { /* schema */
195452 sqlite3 *db = sqlite3_context_db_handle(ctx);
195453 int iDb = pCsr->iDb;
195454 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
195455 break;
195456 }
195457 }
195458 return SQLITE_OK0;
195459}
195460
195461static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
195462 StatCursor *pCsr = (StatCursor *)pCursor;
195463 *pRowid = pCsr->iPageno;
195464 return SQLITE_OK0;
195465}
195466
195467/*
195468** Invoke this routine to register the "dbstat" virtual table module
195469*/
195470SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3 *db){
195471 static sqlite3_module dbstat_module = {
195472 0, /* iVersion */
195473 statConnect, /* xCreate */
195474 statConnect, /* xConnect */
195475 statBestIndex, /* xBestIndex */
195476 statDisconnect, /* xDisconnect */
195477 statDisconnect, /* xDestroy */
195478 statOpen, /* xOpen - open a cursor */
195479 statClose, /* xClose - close a cursor */
195480 statFilter, /* xFilter - configure scan constraints */
195481 statNext, /* xNext - advance a cursor */
195482 statEof, /* xEof - check for end of scan */
195483 statColumn, /* xColumn - read data */
195484 statRowid, /* xRowid - read data */
195485 0, /* xUpdate */
195486 0, /* xBegin */
195487 0, /* xSync */
195488 0, /* xCommit */
195489 0, /* xRollback */
195490 0, /* xFindMethod */
195491 0, /* xRename */
195492 0, /* xSavepoint */
195493 0, /* xRelease */
195494 0, /* xRollbackTo */
195495 0 /* xShadowName */
195496 };
195497 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
195498}
195499#elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
195500SQLITE_PRIVATEstatic int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK0; }
195501#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
195502
195503/************** End of dbstat.c **********************************************/
195504/************** Begin file dbpage.c ******************************************/
195505/*
195506** 2017-10-11
195507**
195508** The author disclaims copyright to this source code. In place of
195509** a legal notice, here is a blessing:
195510**
195511** May you do good and not evil.
195512** May you find forgiveness for yourself and forgive others.
195513** May you share freely, never taking more than you give.
195514**
195515******************************************************************************
195516**
195517** This file contains an implementation of the "sqlite_dbpage" virtual table.
195518**
195519** The sqlite_dbpage virtual table is used to read or write whole raw
195520** pages of the database file. The pager interface is used so that
195521** uncommitted changes and changes recorded in the WAL file are correctly
195522** retrieved.
195523**
195524** Usage example:
195525**
195526** SELECT data FROM sqlite_dbpage('aux1') WHERE pgno=123;
195527**
195528** This is an eponymous virtual table so it does not need to be created before
195529** use. The optional argument to the sqlite_dbpage() table name is the
195530** schema for the database file that is to be read. The default schema is
195531** "main".
195532**
195533** The data field of sqlite_dbpage table can be updated. The new
195534** value must be a BLOB which is the correct page size, otherwise the
195535** update fails. Rows may not be deleted or inserted.
195536*/
195537
195538/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
195539#if (defined(SQLITE_ENABLE_DBPAGE_VTAB) || defined(SQLITE_TEST)) \
195540 && !defined(SQLITE_OMIT_VIRTUALTABLE)
195541
195542typedef struct DbpageTable DbpageTable;
195543typedef struct DbpageCursor DbpageCursor;
195544
195545struct DbpageCursor {
195546 sqlite3_vtab_cursor base; /* Base class. Must be first */
195547 int pgno; /* Current page number */
195548 int mxPgno; /* Last page to visit on this scan */
195549 Pager *pPager; /* Pager being read/written */
195550 DbPage *pPage1; /* Page 1 of the database */
195551 int iDb; /* Index of database to analyze */
195552 int szPage; /* Size of each page in bytes */
195553};
195554
195555struct DbpageTable {
195556 sqlite3_vtab base; /* Base class. Must be first */
195557 sqlite3 *db; /* The database */
195558};
195559
195560/* Columns */
195561#define DBPAGE_COLUMN_PGNO 0
195562#define DBPAGE_COLUMN_DATA 1
195563#define DBPAGE_COLUMN_SCHEMA 2
195564
195565
195566
195567/*
195568** Connect to or create a dbpagevfs virtual table.
195569*/
195570static int dbpageConnect(
195571 sqlite3 *db,
195572 void *pAux,
195573 int argc, const char *const*argv,
195574 sqlite3_vtab **ppVtab,
195575 char **pzErr
195576){
195577 DbpageTable *pTab = 0;
195578 int rc = SQLITE_OK0;
195579
195580 rc = sqlite3_declare_vtab(db,
195581 "CREATE TABLE x(pgno INTEGER PRIMARY KEY, data BLOB, schema HIDDEN)");
195582 if( rc==SQLITE_OK0 ){
195583 pTab = (DbpageTable *)sqlite3_malloc64(sizeof(DbpageTable));
195584 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT7;
195585 }
195586
195587 assert( rc==SQLITE_OK || pTab==0 )((void) (0));
195588 if( rc==SQLITE_OK0 ){
195589 memset(pTab, 0, sizeof(DbpageTable));
195590 pTab->db = db;
195591 }
195592
195593 *ppVtab = (sqlite3_vtab*)pTab;
195594 return rc;
195595}
195596
195597/*
195598** Disconnect from or destroy a dbpagevfs virtual table.
195599*/
195600static int dbpageDisconnect(sqlite3_vtab *pVtab){
195601 sqlite3_free(pVtab);
195602 return SQLITE_OK0;
195603}
195604
195605/*
195606** idxNum:
195607**
195608** 0 schema=main, full table scan
195609** 1 schema=main, pgno=?1
195610** 2 schema=?1, full table scan
195611** 3 schema=?1, pgno=?2
195612*/
195613static int dbpageBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
195614 int i;
195615 int iPlan = 0;
195616
195617 /* If there is a schema= constraint, it must be honored. Report a
195618 ** ridiculously large estimated cost if the schema= constraint is
195619 ** unavailable
195620 */
195621 for(i=0; i<pIdxInfo->nConstraint; i++){
195622 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
195623 if( p->iColumn!=DBPAGE_COLUMN_SCHEMA ) continue;
195624 if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ2 ) continue;
195625 if( !p->usable ){
195626 /* No solution. */
195627 return SQLITE_CONSTRAINT19;
195628 }
195629 iPlan = 2;
195630 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
195631 pIdxInfo->aConstraintUsage[i].omit = 1;
195632 break;
195633 }
195634
195635 /* If we reach this point, it means that either there is no schema=
195636 ** constraint (in which case we use the "main" schema) or else the
195637 ** schema constraint was accepted. Lower the estimated cost accordingly
195638 */
195639 pIdxInfo->estimatedCost = 1.0e6;
195640
195641 /* Check for constraints against pgno */
195642 for(i=0; i<pIdxInfo->nConstraint; i++){
195643 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i];
195644 if( p->usable && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ){
195645 pIdxInfo->estimatedRows = 1;
195646 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE1;
195647 pIdxInfo->estimatedCost = 1.0;
195648 pIdxInfo->aConstraintUsage[i].argvIndex = iPlan ? 2 : 1;
195649 pIdxInfo->aConstraintUsage[i].omit = 1;
195650 iPlan |= 1;
195651 break;
195652 }
195653 }
195654 pIdxInfo->idxNum = iPlan;
195655
195656 if( pIdxInfo->nOrderBy>=1
195657 && pIdxInfo->aOrderBy[0].iColumn<=0
195658 && pIdxInfo->aOrderBy[0].desc==0
195659 ){
195660 pIdxInfo->orderByConsumed = 1;
195661 }
195662 return SQLITE_OK0;
195663}
195664
195665/*
195666** Open a new dbpagevfs cursor.
195667*/
195668static int dbpageOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
195669 DbpageCursor *pCsr;
195670
195671 pCsr = (DbpageCursor *)sqlite3_malloc64(sizeof(DbpageCursor));
195672 if( pCsr==0 ){
195673 return SQLITE_NOMEM_BKPT7;
195674 }else{
195675 memset(pCsr, 0, sizeof(DbpageCursor));
195676 pCsr->base.pVtab = pVTab;
195677 pCsr->pgno = -1;
195678 }
195679
195680 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
195681 return SQLITE_OK0;
195682}
195683
195684/*
195685** Close a dbpagevfs cursor.
195686*/
195687static int dbpageClose(sqlite3_vtab_cursor *pCursor){
195688 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
195689 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
195690 sqlite3_free(pCsr);
195691 return SQLITE_OK0;
195692}
195693
195694/*
195695** Move a dbpagevfs cursor to the next entry in the file.
195696*/
195697static int dbpageNext(sqlite3_vtab_cursor *pCursor){
195698 int rc = SQLITE_OK0;
195699 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
195700 pCsr->pgno++;
195701 return rc;
195702}
195703
195704static int dbpageEof(sqlite3_vtab_cursor *pCursor){
195705 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
195706 return pCsr->pgno > pCsr->mxPgno;
195707}
195708
195709/*
195710** idxNum:
195711**
195712** 0 schema=main, full table scan
195713** 1 schema=main, pgno=?1
195714** 2 schema=?1, full table scan
195715** 3 schema=?1, pgno=?2
195716**
195717** idxStr is not used
195718*/
195719static int dbpageFilter(
195720 sqlite3_vtab_cursor *pCursor,
195721 int idxNum, const char *idxStr,
195722 int argc, sqlite3_value **argv
195723){
195724 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
195725 DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
195726 int rc;
195727 sqlite3 *db = pTab->db;
195728 Btree *pBt;
195729
195730 /* Default setting is no rows of result */
195731 pCsr->pgno = 1;
195732 pCsr->mxPgno = 0;
195733
195734 if( idxNum & 2 ){
195735 const char *zSchema;
195736 assert( argc>=1 )((void) (0));
195737 zSchema = (const char*)sqlite3_value_text(argv[0]);
195738 pCsr->iDb = sqlite3FindDbName(db, zSchema);
195739 if( pCsr->iDb<0 ) return SQLITE_OK0;
195740 }else{
195741 pCsr->iDb = 0;
195742 }
195743 pBt = db->aDb[pCsr->iDb].pBt;
195744 if( pBt==0 ) return SQLITE_OK0;
195745 pCsr->pPager = sqlite3BtreePager(pBt);
195746 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
195747 pCsr->mxPgno = sqlite3BtreeLastPage(pBt);
195748 if( idxNum & 1 ){
195749 assert( argc>(idxNum>>1) )((void) (0));
195750 pCsr->pgno = sqlite3_value_int(argv[idxNum>>1]);
195751 if( pCsr->pgno<1 || pCsr->pgno>pCsr->mxPgno ){
195752 pCsr->pgno = 1;
195753 pCsr->mxPgno = 0;
195754 }else{
195755 pCsr->mxPgno = pCsr->pgno;
195756 }
195757 }else{
195758 assert( pCsr->pgno==1 )((void) (0));
195759 }
195760 if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
195761 rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
195762 return rc;
195763}
195764
195765static int dbpageColumn(
195766 sqlite3_vtab_cursor *pCursor,
195767 sqlite3_context *ctx,
195768 int i
195769){
195770 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
195771 int rc = SQLITE_OK0;
195772 switch( i ){
195773 case 0: { /* pgno */
195774 sqlite3_result_int(ctx, pCsr->pgno);
195775 break;
195776 }
195777 case 1: { /* data */
195778 DbPage *pDbPage = 0;
195779 rc = sqlite3PagerGet(pCsr->pPager, pCsr->pgno, (DbPage**)&pDbPage, 0);
195780 if( rc==SQLITE_OK0 ){
195781 sqlite3_result_blob(ctx, sqlite3PagerGetData(pDbPage), pCsr->szPage,
195782 SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
195783 }
195784 sqlite3PagerUnref(pDbPage);
195785 break;
195786 }
195787 default: { /* schema */
195788 sqlite3 *db = sqlite3_context_db_handle(ctx);
195789 sqlite3_result_text(ctx, db->aDb[pCsr->iDb].zDbSName, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
195790 break;
195791 }
195792 }
195793 return SQLITE_OK0;
195794}
195795
195796static int dbpageRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
195797 DbpageCursor *pCsr = (DbpageCursor *)pCursor;
195798 *pRowid = pCsr->pgno;
195799 return SQLITE_OK0;
195800}
195801
195802static int dbpageUpdate(
195803 sqlite3_vtab *pVtab,
195804 int argc,
195805 sqlite3_value **argv,
195806 sqlite_int64 *pRowid
195807){
195808 DbpageTable *pTab = (DbpageTable *)pVtab;
195809 Pgno pgno;
195810 DbPage *pDbPage = 0;
195811 int rc = SQLITE_OK0;
195812 char *zErr = 0;
195813 const char *zSchema;
195814 int iDb;
195815 Btree *pBt;
195816 Pager *pPager;
195817 int szPage;
195818
195819 if( pTab->db->flags & SQLITE_Defensive0x10000000 ){
195820 zErr = "read-only";
195821 goto update_fail;
195822 }
195823 if( argc==1 ){
195824 zErr = "cannot delete";
195825 goto update_fail;
195826 }
195827 pgno = sqlite3_value_int(argv[0]);
195828 if( (Pgno)sqlite3_value_int(argv[1])!=pgno ){
195829 zErr = "cannot insert";
195830 goto update_fail;
195831 }
195832 zSchema = (const char*)sqlite3_value_text(argv[4]);
195833 iDb = zSchema ? sqlite3FindDbName(pTab->db, zSchema) : -1;
195834 if( iDb<0 ){
195835 zErr = "no such schema";
195836 goto update_fail;
195837 }
195838 pBt = pTab->db->aDb[iDb].pBt;
195839 if( pgno<1 || pBt==0 || pgno>(int)sqlite3BtreeLastPage(pBt) ){
195840 zErr = "bad page number";
195841 goto update_fail;
195842 }
195843 szPage = sqlite3BtreeGetPageSize(pBt);
195844 if( sqlite3_value_type(argv[3])!=SQLITE_BLOB4
195845 || sqlite3_value_bytes(argv[3])!=szPage
195846 ){
195847 zErr = "bad page value";
195848 goto update_fail;
195849 }
195850 pPager = sqlite3BtreePager(pBt);
195851 rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pDbPage, 0);
195852 if( rc==SQLITE_OK0 ){
195853 rc = sqlite3PagerWrite(pDbPage);
195854 if( rc==SQLITE_OK0 ){
195855 memcpy(sqlite3PagerGetData(pDbPage),
195856 sqlite3_value_blob(argv[3]),
195857 szPage);
195858 }
195859 }
195860 sqlite3PagerUnref(pDbPage);
195861 return rc;
195862
195863update_fail:
195864 sqlite3_free(pVtab->zErrMsg);
195865 pVtab->zErrMsg = sqlite3_mprintf("%s", zErr);
195866 return SQLITE_ERROR1;
195867}
195868
195869/* Since we do not know in advance which database files will be
195870** written by the sqlite_dbpage virtual table, start a write transaction
195871** on them all.
195872*/
195873static int dbpageBegin(sqlite3_vtab *pVtab){
195874 DbpageTable *pTab = (DbpageTable *)pVtab;
195875 sqlite3 *db = pTab->db;
195876 int i;
195877 for(i=0; i<db->nDb; i++){
195878 Btree *pBt = db->aDb[i].pBt;
195879 if( pBt ) sqlite3BtreeBeginTrans(pBt, 1, 0);
195880 }
195881 return SQLITE_OK0;
195882}
195883
195884
195885/*
195886** Invoke this routine to register the "dbpage" virtual table module
195887*/
195888SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3 *db){
195889 static sqlite3_module dbpage_module = {
195890 0, /* iVersion */
195891 dbpageConnect, /* xCreate */
195892 dbpageConnect, /* xConnect */
195893 dbpageBestIndex, /* xBestIndex */
195894 dbpageDisconnect, /* xDisconnect */
195895 dbpageDisconnect, /* xDestroy */
195896 dbpageOpen, /* xOpen - open a cursor */
195897 dbpageClose, /* xClose - close a cursor */
195898 dbpageFilter, /* xFilter - configure scan constraints */
195899 dbpageNext, /* xNext - advance a cursor */
195900 dbpageEof, /* xEof - check for end of scan */
195901 dbpageColumn, /* xColumn - read data */
195902 dbpageRowid, /* xRowid - read data */
195903 dbpageUpdate, /* xUpdate */
195904 dbpageBegin, /* xBegin */
195905 0, /* xSync */
195906 0, /* xCommit */
195907 0, /* xRollback */
195908 0, /* xFindMethod */
195909 0, /* xRename */
195910 0, /* xSavepoint */
195911 0, /* xRelease */
195912 0, /* xRollbackTo */
195913 0 /* xShadowName */
195914 };
195915 return sqlite3_create_module(db, "sqlite_dbpage", &dbpage_module, 0);
195916}
195917#elif defined(SQLITE_ENABLE_DBPAGE_VTAB)
195918SQLITE_PRIVATEstatic int sqlite3DbpageRegister(sqlite3 *db){ return SQLITE_OK0; }
195919#endif /* SQLITE_ENABLE_DBSTAT_VTAB */
195920
195921/************** End of dbpage.c **********************************************/
195922/************** Begin file sqlite3session.c **********************************/
195923
195924#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
195925/* #include "sqlite3session.h" */
195926/* #include <assert.h> */
195927/* #include <string.h> */
195928
195929#ifndef SQLITE_AMALGAMATION1
195930/* # include "sqliteInt.h" */
195931/* # include "vdbeInt.h" */
195932#endif
195933
195934typedef struct SessionTable SessionTable;
195935typedef struct SessionChange SessionChange;
195936typedef struct SessionBuffer SessionBuffer;
195937typedef struct SessionInput SessionInput;
195938
195939/*
195940** Minimum chunk size used by streaming versions of functions.
195941*/
195942#ifndef SESSIONS_STRM_CHUNK_SIZE
195943# ifdef SQLITE_TEST
195944# define SESSIONS_STRM_CHUNK_SIZE 64
195945# else
195946# define SESSIONS_STRM_CHUNK_SIZE 1024
195947# endif
195948#endif
195949
195950static int sessions_strm_chunk_size = SESSIONS_STRM_CHUNK_SIZE;
195951
195952typedef struct SessionHook SessionHook;
195953struct SessionHook {
195954 void *pCtx;
195955 int (*xOld)(void*,int,sqlite3_value**);
195956 int (*xNew)(void*,int,sqlite3_value**);
195957 int (*xCount)(void*);
195958 int (*xDepth)(void*);
195959};
195960
195961/*
195962** Session handle structure.
195963*/
195964struct sqlite3_session {
195965 sqlite3 *db; /* Database handle session is attached to */
195966 char *zDb; /* Name of database session is attached to */
195967 int bEnable; /* True if currently recording */
195968 int bIndirect; /* True if all changes are indirect */
195969 int bAutoAttach; /* True to auto-attach tables */
195970 int rc; /* Non-zero if an error has occurred */
195971 void *pFilterCtx; /* First argument to pass to xTableFilter */
195972 int (*xTableFilter)(void *pCtx, const char *zTab);
195973 sqlite3_value *pZeroBlob; /* Value containing X'' */
195974 sqlite3_session *pNext; /* Next session object on same db. */
195975 SessionTable *pTable; /* List of attached tables */
195976 SessionHook hook; /* APIs to grab new and old data with */
195977};
195978
195979/*
195980** Instances of this structure are used to build strings or binary records.
195981*/
195982struct SessionBuffer {
195983 u8 *aBuf; /* Pointer to changeset buffer */
195984 int nBuf; /* Size of buffer aBuf */
195985 int nAlloc; /* Size of allocation containing aBuf */
195986};
195987
195988/*
195989** An object of this type is used internally as an abstraction for
195990** input data. Input data may be supplied either as a single large buffer
195991** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
195992** sqlite3changeset_start_strm()).
195993*/
195994struct SessionInput {
195995 int bNoDiscard; /* If true, do not discard in InputBuffer() */
195996 int iCurrent; /* Offset in aData[] of current change */
195997 int iNext; /* Offset in aData[] of next change */
195998 u8 *aData; /* Pointer to buffer containing changeset */
195999 int nData; /* Number of bytes in aData */
196000
196001 SessionBuffer buf; /* Current read buffer */
196002 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
196003 void *pIn; /* First argument to xInput */
196004 int bEof; /* Set to true after xInput finished */
196005};
196006
196007/*
196008** Structure for changeset iterators.
196009*/
196010struct sqlite3_changeset_iter {
196011 SessionInput in; /* Input buffer or stream */
196012 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
196013 int bPatchset; /* True if this is a patchset */
196014 int bInvert; /* True to invert changeset */
196015 int rc; /* Iterator error code */
196016 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
196017 char *zTab; /* Current table */
196018 int nCol; /* Number of columns in zTab */
196019 int op; /* Current operation */
196020 int bIndirect; /* True if current change was indirect */
196021 u8 *abPK; /* Primary key array */
196022 sqlite3_value **apValue; /* old.* and new.* values */
196023};
196024
196025/*
196026** Each session object maintains a set of the following structures, one
196027** for each table the session object is monitoring. The structures are
196028** stored in a linked list starting at sqlite3_session.pTable.
196029**
196030** The keys of the SessionTable.aChange[] hash table are all rows that have
196031** been modified in any way since the session object was attached to the
196032** table.
196033**
196034** The data associated with each hash-table entry is a structure containing
196035** a subset of the initial values that the modified row contained at the
196036** start of the session. Or no initial values if the row was inserted.
196037*/
196038struct SessionTable {
196039 SessionTable *pNext;
196040 char *zName; /* Local name of table */
196041 int nCol; /* Number of columns in table zName */
196042 int bStat1; /* True if this is sqlite_stat1 */
196043 const char **azCol; /* Column names */
196044 u8 *abPK; /* Array of primary key flags */
196045 int nEntry; /* Total number of entries in hash table */
196046 int nChange; /* Size of apChange[] array */
196047 SessionChange **apChange; /* Hash table buckets */
196048};
196049
196050/*
196051** RECORD FORMAT:
196052**
196053** The following record format is similar to (but not compatible with) that
196054** used in SQLite database files. This format is used as part of the
196055** change-set binary format, and so must be architecture independent.
196056**
196057** Unlike the SQLite database record format, each field is self-contained -
196058** there is no separation of header and data. Each field begins with a
196059** single byte describing its type, as follows:
196060**
196061** 0x00: Undefined value.
196062** 0x01: Integer value.
196063** 0x02: Real value.
196064** 0x03: Text value.
196065** 0x04: Blob value.
196066** 0x05: SQL NULL value.
196067**
196068** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
196069** and so on in sqlite3.h. For undefined and NULL values, the field consists
196070** only of the single type byte. For other types of values, the type byte
196071** is followed by:
196072**
196073** Text values:
196074** A varint containing the number of bytes in the value (encoded using
196075** UTF-8). Followed by a buffer containing the UTF-8 representation
196076** of the text value. There is no nul terminator.
196077**
196078** Blob values:
196079** A varint containing the number of bytes in the value, followed by
196080** a buffer containing the value itself.
196081**
196082** Integer values:
196083** An 8-byte big-endian integer value.
196084**
196085** Real values:
196086** An 8-byte big-endian IEEE 754-2008 real value.
196087**
196088** Varint values are encoded in the same way as varints in the SQLite
196089** record format.
196090**
196091** CHANGESET FORMAT:
196092**
196093** A changeset is a collection of DELETE, UPDATE and INSERT operations on
196094** one or more tables. Operations on a single table are grouped together,
196095** but may occur in any order (i.e. deletes, updates and inserts are all
196096** mixed together).
196097**
196098** Each group of changes begins with a table header:
196099**
196100** 1 byte: Constant 0x54 (capital 'T')
196101** Varint: Number of columns in the table.
196102** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
196103** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
196104**
196105** Followed by one or more changes to the table.
196106**
196107** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
196108** 1 byte: The "indirect-change" flag.
196109** old.* record: (delete and update only)
196110** new.* record: (insert and update only)
196111**
196112** The "old.*" and "new.*" records, if present, are N field records in the
196113** format described above under "RECORD FORMAT", where N is the number of
196114** columns in the table. The i'th field of each record is associated with
196115** the i'th column of the table, counting from left to right in the order
196116** in which columns were declared in the CREATE TABLE statement.
196117**
196118** The new.* record that is part of each INSERT change contains the values
196119** that make up the new row. Similarly, the old.* record that is part of each
196120** DELETE change contains the values that made up the row that was deleted
196121** from the database. In the changeset format, the records that are part
196122** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
196123** fields.
196124**
196125** Within the old.* record associated with an UPDATE change, all fields
196126** associated with table columns that are not PRIMARY KEY columns and are
196127** not modified by the UPDATE change are set to "undefined". Other fields
196128** are set to the values that made up the row before the UPDATE that the
196129** change records took place. Within the new.* record, fields associated
196130** with table columns modified by the UPDATE change contain the new
196131** values. Fields associated with table columns that are not modified
196132** are set to "undefined".
196133**
196134** PATCHSET FORMAT:
196135**
196136** A patchset is also a collection of changes. It is similar to a changeset,
196137** but leaves undefined those fields that are not useful if no conflict
196138** resolution is required when applying the changeset.
196139**
196140** Each group of changes begins with a table header:
196141**
196142** 1 byte: Constant 0x50 (capital 'P')
196143** Varint: Number of columns in the table.
196144** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
196145** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
196146**
196147** Followed by one or more changes to the table.
196148**
196149** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
196150** 1 byte: The "indirect-change" flag.
196151** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
196152** full record for INSERT).
196153**
196154** As in the changeset format, each field of the single record that is part
196155** of a patchset change is associated with the correspondingly positioned
196156** table column, counting from left to right within the CREATE TABLE
196157** statement.
196158**
196159** For a DELETE change, all fields within the record except those associated
196160** with PRIMARY KEY columns are omitted. The PRIMARY KEY fields contain the
196161** values identifying the row to delete.
196162**
196163** For an UPDATE change, all fields except those associated with PRIMARY KEY
196164** columns and columns that are modified by the UPDATE are set to "undefined".
196165** PRIMARY KEY fields contain the values identifying the table row to update,
196166** and fields associated with modified columns contain the new column values.
196167**
196168** The records associated with INSERT changes are in the same format as for
196169** changesets. It is not possible for a record associated with an INSERT
196170** change to contain a field set to "undefined".
196171**
196172** REBASE BLOB FORMAT:
196173**
196174** A rebase blob may be output by sqlite3changeset_apply_v2() and its
196175** streaming equivalent for use with the sqlite3_rebaser APIs to rebase
196176** existing changesets. A rebase blob contains one entry for each conflict
196177** resolved using either the OMIT or REPLACE strategies within the apply_v2()
196178** call.
196179**
196180** The format used for a rebase blob is very similar to that used for
196181** changesets. All entries related to a single table are grouped together.
196182**
196183** Each group of entries begins with a table header in changeset format:
196184**
196185** 1 byte: Constant 0x54 (capital 'T')
196186** Varint: Number of columns in the table.
196187** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
196188** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
196189**
196190** Followed by one or more entries associated with the table.
196191**
196192** 1 byte: Either SQLITE_INSERT (0x12), DELETE (0x09).
196193** 1 byte: Flag. 0x01 for REPLACE, 0x00 for OMIT.
196194** record: (in the record format defined above).
196195**
196196** In a rebase blob, the first field is set to SQLITE_INSERT if the change
196197** that caused the conflict was an INSERT or UPDATE, or to SQLITE_DELETE if
196198** it was a DELETE. The second field is set to 0x01 if the conflict
196199** resolution strategy was REPLACE, or 0x00 if it was OMIT.
196200**
196201** If the change that caused the conflict was a DELETE, then the single
196202** record is a copy of the old.* record from the original changeset. If it
196203** was an INSERT, then the single record is a copy of the new.* record. If
196204** the conflicting change was an UPDATE, then the single record is a copy
196205** of the new.* record with the PK fields filled in based on the original
196206** old.* record.
196207*/
196208
196209/*
196210** For each row modified during a session, there exists a single instance of
196211** this structure stored in a SessionTable.aChange[] hash table.
196212*/
196213struct SessionChange {
196214 int op; /* One of UPDATE, DELETE, INSERT */
196215 int bIndirect; /* True if this change is "indirect" */
196216 int nRecord; /* Number of bytes in buffer aRecord[] */
196217 u8 *aRecord; /* Buffer containing old.* record */
196218 SessionChange *pNext; /* For hash-table collisions */
196219};
196220
196221/*
196222** Write a varint with value iVal into the buffer at aBuf. Return the
196223** number of bytes written.
196224*/
196225static int sessionVarintPut(u8 *aBuf, int iVal){
196226 return putVarint32(aBuf, iVal)(u8)(((u32)(iVal)<(u32)0x80)?(*(aBuf)=(unsigned char)(iVal
)),1: sqlite3PutVarint((aBuf),(iVal)))
;
196227}
196228
196229/*
196230** Return the number of bytes required to store value iVal as a varint.
196231*/
196232static int sessionVarintLen(int iVal){
196233 return sqlite3VarintLen(iVal);
196234}
196235
196236/*
196237** Read a varint value from aBuf[] into *piVal. Return the number of
196238** bytes read.
196239*/
196240static int sessionVarintGet(u8 *aBuf, int *piVal){
196241 return getVarint32(aBuf, *piVal)(u8)((*(aBuf)<(u8)0x80)?((*piVal)=(u32)*(aBuf)),1:sqlite3GetVarint32
((aBuf),(u32 *)&(*piVal)))
;
196242}
196243
196244/* Load an unaligned and unsigned 32-bit integer */
196245#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
196246
196247/*
196248** Read a 64-bit big-endian integer value from buffer aRec[]. Return
196249** the value read.
196250*/
196251static sqlite3_int64 sessionGetI64(u8 *aRec){
196252 u64 x = SESSION_UINT32(aRec);
196253 u32 y = SESSION_UINT32(aRec+4);
196254 x = (x<<32) + y;
196255 return (sqlite3_int64)x;
196256}
196257
196258/*
196259** Write a 64-bit big-endian integer value to the buffer aBuf[].
196260*/
196261static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
196262 aBuf[0] = (i>>56) & 0xFF;
196263 aBuf[1] = (i>>48) & 0xFF;
196264 aBuf[2] = (i>>40) & 0xFF;
196265 aBuf[3] = (i>>32) & 0xFF;
196266 aBuf[4] = (i>>24) & 0xFF;
196267 aBuf[5] = (i>>16) & 0xFF;
196268 aBuf[6] = (i>> 8) & 0xFF;
196269 aBuf[7] = (i>> 0) & 0xFF;
196270}
196271
196272/*
196273** This function is used to serialize the contents of value pValue (see
196274** comment titled "RECORD FORMAT" above).
196275**
196276** If it is non-NULL, the serialized form of the value is written to
196277** buffer aBuf. *pnWrite is set to the number of bytes written before
196278** returning. Or, if aBuf is NULL, the only thing this function does is
196279** set *pnWrite.
196280**
196281** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
196282** within a call to sqlite3_value_text() (may fail if the db is utf-16))
196283** SQLITE_NOMEM is returned.
196284*/
196285static int sessionSerializeValue(
196286 u8 *aBuf, /* If non-NULL, write serialized value here */
196287 sqlite3_value *pValue, /* Value to serialize */
196288 sqlite3_int64 *pnWrite /* IN/OUT: Increment by bytes written */
196289){
196290 int nByte; /* Size of serialized value in bytes */
196291
196292 if( pValue ){
196293 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
196294
196295 eType = sqlite3_value_type(pValue);
196296 if( aBuf ) aBuf[0] = eType;
196297
196298 switch( eType ){
196299 case SQLITE_NULL5:
196300 nByte = 1;
196301 break;
196302
196303 case SQLITE_INTEGER1:
196304 case SQLITE_FLOAT2:
196305 if( aBuf ){
196306 /* TODO: SQLite does something special to deal with mixed-endian
196307 ** floating point values (e.g. ARM7). This code probably should
196308 ** too. */
196309 u64 i;
196310 if( eType==SQLITE_INTEGER1 ){
196311 i = (u64)sqlite3_value_int64(pValue);
196312 }else{
196313 double r;
196314 assert( sizeof(double)==8 && sizeof(u64)==8 )((void) (0));
196315 r = sqlite3_value_double(pValue);
196316 memcpy(&i, &r, 8);
196317 }
196318 sessionPutI64(&aBuf[1], i);
196319 }
196320 nByte = 9;
196321 break;
196322
196323 default: {
196324 u8 *z;
196325 int n;
196326 int nVarint;
196327
196328 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
196329 if( eType==SQLITE_TEXT3 ){
196330 z = (u8 *)sqlite3_value_text(pValue);
196331 }else{
196332 z = (u8 *)sqlite3_value_blob(pValue);
196333 }
196334 n = sqlite3_value_bytes(pValue);
196335 if( z==0 && (eType!=SQLITE_BLOB4 || n>0) ) return SQLITE_NOMEM7;
196336 nVarint = sessionVarintLen(n);
196337
196338 if( aBuf ){
196339 sessionVarintPut(&aBuf[1], n);
196340 if( n ) memcpy(&aBuf[nVarint + 1], z, n);
196341 }
196342
196343 nByte = 1 + nVarint + n;
196344 break;
196345 }
196346 }
196347 }else{
196348 nByte = 1;
196349 if( aBuf ) aBuf[0] = '\0';
196350 }
196351
196352 if( pnWrite ) *pnWrite += nByte;
196353 return SQLITE_OK0;
196354}
196355
196356
196357/*
196358** This macro is used to calculate hash key values for data structures. In
196359** order to use this macro, the entire data structure must be represented
196360** as a series of unsigned integers. In order to calculate a hash-key value
196361** for a data structure represented as three such integers, the macro may
196362** then be used as follows:
196363**
196364** int hash_key_value;
196365** hash_key_value = HASH_APPEND(0, <value 1>);
196366** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
196367** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
196368**
196369** In practice, the data structures this macro is used for are the primary
196370** key values of modified rows.
196371*/
196372#define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
196373
196374/*
196375** Append the hash of the 64-bit integer passed as the second argument to the
196376** hash-key value passed as the first. Return the new hash-key value.
196377*/
196378static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
196379 h = HASH_APPEND(h, i & 0xFFFFFFFF);
196380 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
196381}
196382
196383/*
196384** Append the hash of the blob passed via the second and third arguments to
196385** the hash-key value passed as the first. Return the new hash-key value.
196386*/
196387static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
196388 int i;
196389 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
196390 return h;
196391}
196392
196393/*
196394** Append the hash of the data type passed as the second argument to the
196395** hash-key value passed as the first. Return the new hash-key value.
196396*/
196397static unsigned int sessionHashAppendType(unsigned int h, int eType){
196398 return HASH_APPEND(h, eType);
196399}
196400
196401/*
196402** This function may only be called from within a pre-update callback.
196403** It calculates a hash based on the primary key values of the old.* or
196404** new.* row currently available and, assuming no error occurs, writes it to
196405** *piHash before returning. If the primary key contains one or more NULL
196406** values, *pbNullPK is set to true before returning.
196407**
196408** If an error occurs, an SQLite error code is returned and the final values
196409** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
196410** and the output variables are set as described above.
196411*/
196412static int sessionPreupdateHash(
196413 sqlite3_session *pSession, /* Session object that owns pTab */
196414 SessionTable *pTab, /* Session table handle */
196415 int bNew, /* True to hash the new.* PK */
196416 int *piHash, /* OUT: Hash value */
196417 int *pbNullPK /* OUT: True if there are NULL values in PK */
196418){
196419 unsigned int h = 0; /* Hash value to return */
196420 int i; /* Used to iterate through columns */
196421
196422 assert( *pbNullPK==0 )((void) (0));
196423 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) )((void) (0));
196424 for(i=0; i<pTab->nCol; i++){
196425 if( pTab->abPK[i] ){
196426 int rc;
196427 int eType;
196428 sqlite3_value *pVal;
196429
196430 if( bNew ){
196431 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
196432 }else{
196433 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
196434 }
196435 if( rc!=SQLITE_OK0 ) return rc;
196436
196437 eType = sqlite3_value_type(pVal);
196438 h = sessionHashAppendType(h, eType);
196439 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
196440 i64 iVal;
196441 if( eType==SQLITE_INTEGER1 ){
196442 iVal = sqlite3_value_int64(pVal);
196443 }else{
196444 double rVal = sqlite3_value_double(pVal);
196445 assert( sizeof(iVal)==8 && sizeof(rVal)==8 )((void) (0));
196446 memcpy(&iVal, &rVal, 8);
196447 }
196448 h = sessionHashAppendI64(h, iVal);
196449 }else if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
196450 const u8 *z;
196451 int n;
196452 if( eType==SQLITE_TEXT3 ){
196453 z = (const u8 *)sqlite3_value_text(pVal);
196454 }else{
196455 z = (const u8 *)sqlite3_value_blob(pVal);
196456 }
196457 n = sqlite3_value_bytes(pVal);
196458 if( !z && (eType!=SQLITE_BLOB4 || n>0) ) return SQLITE_NOMEM7;
196459 h = sessionHashAppendBlob(h, n, z);
196460 }else{
196461 assert( eType==SQLITE_NULL )((void) (0));
196462 assert( pTab->bStat1==0 || i!=1 )((void) (0));
196463 *pbNullPK = 1;
196464 }
196465 }
196466 }
196467
196468 *piHash = (h % pTab->nChange);
196469 return SQLITE_OK0;
196470}
196471
196472/*
196473** The buffer that the argument points to contains a serialized SQL value.
196474** Return the number of bytes of space occupied by the value (including
196475** the type byte).
196476*/
196477static int sessionSerialLen(u8 *a){
196478 int e = *a;
196479 int n;
196480 if( e==0 || e==0xFF ) return 1;
196481 if( e==SQLITE_NULL5 ) return 1;
196482 if( e==SQLITE_INTEGER1 || e==SQLITE_FLOAT2 ) return 9;
196483 return sessionVarintGet(&a[1], &n) + 1 + n;
196484}
196485
196486/*
196487** Based on the primary key values stored in change aRecord, calculate a
196488** hash key. Assume the has table has nBucket buckets. The hash keys
196489** calculated by this function are compatible with those calculated by
196490** sessionPreupdateHash().
196491**
196492** The bPkOnly argument is non-zero if the record at aRecord[] is from
196493** a patchset DELETE. In this case the non-PK fields are omitted entirely.
196494*/
196495static unsigned int sessionChangeHash(
196496 SessionTable *pTab, /* Table handle */
196497 int bPkOnly, /* Record consists of PK fields only */
196498 u8 *aRecord, /* Change record */
196499 int nBucket /* Assume this many buckets in hash table */
196500){
196501 unsigned int h = 0; /* Value to return */
196502 int i; /* Used to iterate through columns */
196503 u8 *a = aRecord; /* Used to iterate through change record */
196504
196505 for(i=0; i<pTab->nCol; i++){
196506 int eType = *a;
196507 int isPK = pTab->abPK[i];
196508 if( bPkOnly && isPK==0 ) continue;
196509
196510 /* It is not possible for eType to be SQLITE_NULL here. The session
196511 ** module does not record changes for rows with NULL values stored in
196512 ** primary key columns. */
196513 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT((void) (0))
196514 || eType==SQLITE_TEXT || eType==SQLITE_BLOB((void) (0))
196515 || eType==SQLITE_NULL || eType==0((void) (0))
196516 )((void) (0));
196517 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) )((void) (0));
196518
196519 if( isPK ){
196520 a++;
196521 h = sessionHashAppendType(h, eType);
196522 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
196523 h = sessionHashAppendI64(h, sessionGetI64(a));
196524 a += 8;
196525 }else{
196526 int n;
196527 a += sessionVarintGet(a, &n);
196528 h = sessionHashAppendBlob(h, n, a);
196529 a += n;
196530 }
196531 }else{
196532 a += sessionSerialLen(a);
196533 }
196534 }
196535 return (h % nBucket);
196536}
196537
196538/*
196539** Arguments aLeft and aRight are pointers to change records for table pTab.
196540** This function returns true if the two records apply to the same row (i.e.
196541** have the same values stored in the primary key columns), or false
196542** otherwise.
196543*/
196544static int sessionChangeEqual(
196545 SessionTable *pTab, /* Table used for PK definition */
196546 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
196547 u8 *aLeft, /* Change record */
196548 int bRightPkOnly, /* True if aRight[] contains PK fields only */
196549 u8 *aRight /* Change record */
196550){
196551 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
196552 u8 *a2 = aRight; /* Cursor to iterate through aRight */
196553 int iCol; /* Used to iterate through table columns */
196554
196555 for(iCol=0; iCol<pTab->nCol; iCol++){
196556 if( pTab->abPK[iCol] ){
196557 int n1 = sessionSerialLen(a1);
196558 int n2 = sessionSerialLen(a2);
196559
196560 if( n1!=n2 || memcmp(a1, a2, n1) ){
196561 return 0;
196562 }
196563 a1 += n1;
196564 a2 += n2;
196565 }else{
196566 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
196567 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
196568 }
196569 }
196570
196571 return 1;
196572}
196573
196574/*
196575** Arguments aLeft and aRight both point to buffers containing change
196576** records with nCol columns. This function "merges" the two records into
196577** a single records which is written to the buffer at *paOut. *paOut is
196578** then set to point to one byte after the last byte written before
196579** returning.
196580**
196581** The merging of records is done as follows: For each column, if the
196582** aRight record contains a value for the column, copy the value from
196583** their. Otherwise, if aLeft contains a value, copy it. If neither
196584** record contains a value for a given column, then neither does the
196585** output record.
196586*/
196587static void sessionMergeRecord(
196588 u8 **paOut,
196589 int nCol,
196590 u8 *aLeft,
196591 u8 *aRight
196592){
196593 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
196594 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
196595 u8 *aOut = *paOut; /* Output cursor */
196596 int iCol; /* Used to iterate from 0 to nCol */
196597
196598 for(iCol=0; iCol<nCol; iCol++){
196599 int n1 = sessionSerialLen(a1);
196600 int n2 = sessionSerialLen(a2);
196601 if( *a2 ){
196602 memcpy(aOut, a2, n2);
196603 aOut += n2;
196604 }else{
196605 memcpy(aOut, a1, n1);
196606 aOut += n1;
196607 }
196608 a1 += n1;
196609 a2 += n2;
196610 }
196611
196612 *paOut = aOut;
196613}
196614
196615/*
196616** This is a helper function used by sessionMergeUpdate().
196617**
196618** When this function is called, both *paOne and *paTwo point to a value
196619** within a change record. Before it returns, both have been advanced so
196620** as to point to the next value in the record.
196621**
196622** If, when this function is called, *paTwo points to a valid value (i.e.
196623** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
196624** pointer is returned and *pnVal is set to the number of bytes in the
196625** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
196626** set to the number of bytes in the value at *paOne. If *paOne points
196627** to the "no value" placeholder, *pnVal is set to 1. In other words:
196628**
196629** if( *paTwo is valid ) return *paTwo;
196630** return *paOne;
196631**
196632*/
196633static u8 *sessionMergeValue(
196634 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
196635 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
196636 int *pnVal /* OUT: Bytes in returned value */
196637){
196638 u8 *a1 = *paOne;
196639 u8 *a2 = *paTwo;
196640 u8 *pRet = 0;
196641 int n1;
196642
196643 assert( a1 )((void) (0));
196644 if( a2 ){
196645 int n2 = sessionSerialLen(a2);
196646 if( *a2 ){
196647 *pnVal = n2;
196648 pRet = a2;
196649 }
196650 *paTwo = &a2[n2];
196651 }
196652
196653 n1 = sessionSerialLen(a1);
196654 if( pRet==0 ){
196655 *pnVal = n1;
196656 pRet = a1;
196657 }
196658 *paOne = &a1[n1];
196659
196660 return pRet;
196661}
196662
196663/*
196664** This function is used by changeset_concat() to merge two UPDATE changes
196665** on the same row.
196666*/
196667static int sessionMergeUpdate(
196668 u8 **paOut, /* IN/OUT: Pointer to output buffer */
196669 SessionTable *pTab, /* Table change pertains to */
196670 int bPatchset, /* True if records are patchset records */
196671 u8 *aOldRecord1, /* old.* record for first change */
196672 u8 *aOldRecord2, /* old.* record for second change */
196673 u8 *aNewRecord1, /* new.* record for first change */
196674 u8 *aNewRecord2 /* new.* record for second change */
196675){
196676 u8 *aOld1 = aOldRecord1;
196677 u8 *aOld2 = aOldRecord2;
196678 u8 *aNew1 = aNewRecord1;
196679 u8 *aNew2 = aNewRecord2;
196680
196681 u8 *aOut = *paOut;
196682 int i;
196683
196684 if( bPatchset==0 ){
196685 int bRequired = 0;
196686
196687 assert( aOldRecord1 && aNewRecord1 )((void) (0));
196688
196689 /* Write the old.* vector first. */
196690 for(i=0; i<pTab->nCol; i++){
196691 int nOld;
196692 u8 *aOld;
196693 int nNew;
196694 u8 *aNew;
196695
196696 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
196697 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
196698 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
196699 if( pTab->abPK[i]==0 ) bRequired = 1;
196700 memcpy(aOut, aOld, nOld);
196701 aOut += nOld;
196702 }else{
196703 *(aOut++) = '\0';
196704 }
196705 }
196706
196707 if( !bRequired ) return 0;
196708 }
196709
196710 /* Write the new.* vector */
196711 aOld1 = aOldRecord1;
196712 aOld2 = aOldRecord2;
196713 aNew1 = aNewRecord1;
196714 aNew2 = aNewRecord2;
196715 for(i=0; i<pTab->nCol; i++){
196716 int nOld;
196717 u8 *aOld;
196718 int nNew;
196719 u8 *aNew;
196720
196721 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
196722 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
196723 if( bPatchset==0
196724 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
196725 ){
196726 *(aOut++) = '\0';
196727 }else{
196728 memcpy(aOut, aNew, nNew);
196729 aOut += nNew;
196730 }
196731 }
196732
196733 *paOut = aOut;
196734 return 1;
196735}
196736
196737/*
196738** This function is only called from within a pre-update-hook callback.
196739** It determines if the current pre-update-hook change affects the same row
196740** as the change stored in argument pChange. If so, it returns true. Otherwise
196741** if the pre-update-hook does not affect the same row as pChange, it returns
196742** false.
196743*/
196744static int sessionPreupdateEqual(
196745 sqlite3_session *pSession, /* Session object that owns SessionTable */
196746 SessionTable *pTab, /* Table associated with change */
196747 SessionChange *pChange, /* Change to compare to */
196748 int op /* Current pre-update operation */
196749){
196750 int iCol; /* Used to iterate through columns */
196751 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
196752
196753 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE )((void) (0));
196754 for(iCol=0; iCol<pTab->nCol; iCol++){
196755 if( !pTab->abPK[iCol] ){
196756 a += sessionSerialLen(a);
196757 }else{
196758 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
196759 int rc; /* Error code from preupdate_new/old */
196760 int eType = *a++; /* Type of value from change record */
196761
196762 /* The following calls to preupdate_new() and preupdate_old() can not
196763 ** fail. This is because they cache their return values, and by the
196764 ** time control flows to here they have already been called once from
196765 ** within sessionPreupdateHash(). The first two asserts below verify
196766 ** this (that the method has already been called). */
196767 if( op==SQLITE_INSERT18 ){
196768 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
196769 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
196770 }else{
196771 /* assert( db->pPreUpdate->pUnpacked ); */
196772 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
196773 }
196774 assert( rc==SQLITE_OK )((void) (0));
196775 if( sqlite3_value_type(pVal)!=eType ) return 0;
196776
196777 /* A SessionChange object never has a NULL value in a PK column */
196778 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT((void) (0))
196779 || eType==SQLITE_BLOB || eType==SQLITE_TEXT((void) (0))
196780 )((void) (0));
196781
196782 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
196783 i64 iVal = sessionGetI64(a);
196784 a += 8;
196785 if( eType==SQLITE_INTEGER1 ){
196786 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
196787 }else{
196788 double rVal;
196789 assert( sizeof(iVal)==8 && sizeof(rVal)==8 )((void) (0));
196790 memcpy(&rVal, &iVal, 8);
196791 if( sqlite3_value_double(pVal)!=rVal ) return 0;
196792 }
196793 }else{
196794 int n;
196795 const u8 *z;
196796 a += sessionVarintGet(a, &n);
196797 if( sqlite3_value_bytes(pVal)!=n ) return 0;
196798 if( eType==SQLITE_TEXT3 ){
196799 z = sqlite3_value_text(pVal);
196800 }else{
196801 z = sqlite3_value_blob(pVal);
196802 }
196803 if( n>0 && memcmp(a, z, n) ) return 0;
196804 a += n;
196805 }
196806 }
196807 }
196808
196809 return 1;
196810}
196811
196812/*
196813** If required, grow the hash table used to store changes on table pTab
196814** (part of the session pSession). If a fatal OOM error occurs, set the
196815** session object to failed and return SQLITE_ERROR. Otherwise, return
196816** SQLITE_OK.
196817**
196818** It is possible that a non-fatal OOM error occurs in this function. In
196819** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
196820** Growing the hash table in this case is a performance optimization only,
196821** it is not required for correct operation.
196822*/
196823static int sessionGrowHash(int bPatchset, SessionTable *pTab){
196824 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
196825 int i;
196826 SessionChange **apNew;
196827 sqlite3_int64 nNew = 2*(sqlite3_int64)(pTab->nChange ? pTab->nChange : 128);
196828
196829 apNew = (SessionChange **)sqlite3_malloc64(sizeof(SessionChange *) * nNew);
196830 if( apNew==0 ){
196831 if( pTab->nChange==0 ){
196832 return SQLITE_ERROR1;
196833 }
196834 return SQLITE_OK0;
196835 }
196836 memset(apNew, 0, sizeof(SessionChange *) * nNew);
196837
196838 for(i=0; i<pTab->nChange; i++){
196839 SessionChange *p;
196840 SessionChange *pNext;
196841 for(p=pTab->apChange[i]; p; p=pNext){
196842 int bPkOnly = (p->op==SQLITE_DELETE9 && bPatchset);
196843 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
196844 pNext = p->pNext;
196845 p->pNext = apNew[iHash];
196846 apNew[iHash] = p;
196847 }
196848 }
196849
196850 sqlite3_free(pTab->apChange);
196851 pTab->nChange = nNew;
196852 pTab->apChange = apNew;
196853 }
196854
196855 return SQLITE_OK0;
196856}
196857
196858/*
196859** This function queries the database for the names of the columns of table
196860** zThis, in schema zDb.
196861**
196862** Otherwise, if they are not NULL, variable *pnCol is set to the number
196863** of columns in the database table and variable *pzTab is set to point to a
196864** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
196865** point to an array of pointers to column names. And *pabPK (again, if not
196866** NULL) is set to point to an array of booleans - true if the corresponding
196867** column is part of the primary key.
196868**
196869** For example, if the table is declared as:
196870**
196871** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
196872**
196873** Then the four output variables are populated as follows:
196874**
196875** *pnCol = 4
196876** *pzTab = "tbl1"
196877** *pazCol = {"w", "x", "y", "z"}
196878** *pabPK = {1, 0, 0, 1}
196879**
196880** All returned buffers are part of the same single allocation, which must
196881** be freed using sqlite3_free() by the caller
196882*/
196883static int sessionTableInfo(
196884 sqlite3 *db, /* Database connection */
196885 const char *zDb, /* Name of attached database (e.g. "main") */
196886 const char *zThis, /* Table name */
196887 int *pnCol, /* OUT: number of columns */
196888 const char **pzTab, /* OUT: Copy of zThis */
196889 const char ***pazCol, /* OUT: Array of column names for table */
196890 u8 **pabPK /* OUT: Array of booleans - true for PK col */
196891){
196892 char *zPragma;
196893 sqlite3_stmt *pStmt;
196894 int rc;
196895 sqlite3_int64 nByte;
196896 int nDbCol = 0;
196897 int nThis;
196898 int i;
196899 u8 *pAlloc = 0;
196900 char **azCol = 0;
196901 u8 *abPK = 0;
196902
196903 assert( pazCol && pabPK )((void) (0));
196904
196905 nThis = sqlite3Strlen30(zThis);
196906 if( nThis==12 && 0==sqlite3_stricmp("sqlite_stat1", zThis) ){
196907 rc = sqlite3_table_column_metadata(db, zDb, zThis, 0, 0, 0, 0, 0, 0);
196908 if( rc==SQLITE_OK0 ){
196909 /* For sqlite_stat1, pretend that (tbl,idx) is the PRIMARY KEY. */
196910 zPragma = sqlite3_mprintf(
196911 "SELECT 0, 'tbl', '', 0, '', 1 UNION ALL "
196912 "SELECT 1, 'idx', '', 0, '', 2 UNION ALL "
196913 "SELECT 2, 'stat', '', 0, '', 0"
196914 );
196915 }else if( rc==SQLITE_ERROR1 ){
196916 zPragma = sqlite3_mprintf("");
196917 }else{
196918 return rc;
196919 }
196920 }else{
196921 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
196922 }
196923 if( !zPragma ) return SQLITE_NOMEM7;
196924
196925 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
196926 sqlite3_free(zPragma);
196927 if( rc!=SQLITE_OK0 ) return rc;
196928
196929 nByte = nThis + 1;
196930 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
196931 nByte += sqlite3_column_bytes(pStmt, 1);
196932 nDbCol++;
196933 }
196934 rc = sqlite3_reset(pStmt);
196935
196936 if( rc==SQLITE_OK0 ){
196937 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
196938 pAlloc = sqlite3_malloc64(nByte);
196939 if( pAlloc==0 ){
196940 rc = SQLITE_NOMEM7;
196941 }
196942 }
196943 if( rc==SQLITE_OK0 ){
196944 azCol = (char **)pAlloc;
196945 pAlloc = (u8 *)&azCol[nDbCol];
196946 abPK = (u8 *)pAlloc;
196947 pAlloc = &abPK[nDbCol];
196948 if( pzTab ){
196949 memcpy(pAlloc, zThis, nThis+1);
196950 *pzTab = (char *)pAlloc;
196951 pAlloc += nThis+1;
196952 }
196953
196954 i = 0;
196955 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
196956 int nName = sqlite3_column_bytes(pStmt, 1);
196957 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
196958 if( zName==0 ) break;
196959 memcpy(pAlloc, zName, nName+1);
196960 azCol[i] = (char *)pAlloc;
196961 pAlloc += nName+1;
196962 abPK[i] = sqlite3_column_int(pStmt, 5);
196963 i++;
196964 }
196965 rc = sqlite3_reset(pStmt);
196966
196967 }
196968
196969 /* If successful, populate the output variables. Otherwise, zero them and
196970 ** free any allocation made. An error code will be returned in this case.
196971 */
196972 if( rc==SQLITE_OK0 ){
196973 *pazCol = (const char **)azCol;
196974 *pabPK = abPK;
196975 *pnCol = nDbCol;
196976 }else{
196977 *pazCol = 0;
196978 *pabPK = 0;
196979 *pnCol = 0;
196980 if( pzTab ) *pzTab = 0;
196981 sqlite3_free(azCol);
196982 }
196983 sqlite3_finalize(pStmt);
196984 return rc;
196985}
196986
196987/*
196988** This function is only called from within a pre-update handler for a
196989** write to table pTab, part of session pSession. If this is the first
196990** write to this table, initalize the SessionTable.nCol, azCol[] and
196991** abPK[] arrays accordingly.
196992**
196993** If an error occurs, an error code is stored in sqlite3_session.rc and
196994** non-zero returned. Or, if no error occurs but the table has no primary
196995** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
196996** indicate that updates on this table should be ignored. SessionTable.abPK
196997** is set to NULL in this case.
196998*/
196999static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
197000 if( pTab->nCol==0 ){
197001 u8 *abPK;
197002 assert( pTab->azCol==0 || pTab->abPK==0 )((void) (0));
197003 pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
197004 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
197005 );
197006 if( pSession->rc==SQLITE_OK0 ){
197007 int i;
197008 for(i=0; i<pTab->nCol; i++){
197009 if( abPK[i] ){
197010 pTab->abPK = abPK;
197011 break;
197012 }
197013 }
197014 if( 0==sqlite3_stricmp("sqlite_stat1", pTab->zName) ){
197015 pTab->bStat1 = 1;
197016 }
197017 }
197018 }
197019 return (pSession->rc || pTab->abPK==0);
197020}
197021
197022/*
197023** Versions of the four methods in object SessionHook for use with the
197024** sqlite_stat1 table. The purpose of this is to substitute a zero-length
197025** blob each time a NULL value is read from the "idx" column of the
197026** sqlite_stat1 table.
197027*/
197028typedef struct SessionStat1Ctx SessionStat1Ctx;
197029struct SessionStat1Ctx {
197030 SessionHook hook;
197031 sqlite3_session *pSession;
197032};
197033static int sessionStat1Old(void *pCtx, int iCol, sqlite3_value **ppVal){
197034 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
197035 sqlite3_value *pVal = 0;
197036 int rc = p->hook.xOld(p->hook.pCtx, iCol, &pVal);
197037 if( rc==SQLITE_OK0 && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL5 ){
197038 pVal = p->pSession->pZeroBlob;
197039 }
197040 *ppVal = pVal;
197041 return rc;
197042}
197043static int sessionStat1New(void *pCtx, int iCol, sqlite3_value **ppVal){
197044 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
197045 sqlite3_value *pVal = 0;
197046 int rc = p->hook.xNew(p->hook.pCtx, iCol, &pVal);
197047 if( rc==SQLITE_OK0 && iCol==1 && sqlite3_value_type(pVal)==SQLITE_NULL5 ){
197048 pVal = p->pSession->pZeroBlob;
197049 }
197050 *ppVal = pVal;
197051 return rc;
197052}
197053static int sessionStat1Count(void *pCtx){
197054 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
197055 return p->hook.xCount(p->hook.pCtx);
197056}
197057static int sessionStat1Depth(void *pCtx){
197058 SessionStat1Ctx *p = (SessionStat1Ctx*)pCtx;
197059 return p->hook.xDepth(p->hook.pCtx);
197060}
197061
197062
197063/*
197064** This function is only called from with a pre-update-hook reporting a
197065** change on table pTab (attached to session pSession). The type of change
197066** (UPDATE, INSERT, DELETE) is specified by the first argument.
197067**
197068** Unless one is already present or an error occurs, an entry is added
197069** to the changed-rows hash table associated with table pTab.
197070*/
197071static void sessionPreupdateOneChange(
197072 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
197073 sqlite3_session *pSession, /* Session object pTab is attached to */
197074 SessionTable *pTab /* Table that change applies to */
197075){
197076 int iHash;
197077 int bNull = 0;
197078 int rc = SQLITE_OK0;
197079 SessionStat1Ctx stat1 = {{0,0,0,0,0},0};
197080
197081 if( pSession->rc ) return;
197082
197083 /* Load table details if required */
197084 if( sessionInitTable(pSession, pTab) ) return;
197085
197086 /* Check the number of columns in this xPreUpdate call matches the
197087 ** number of columns in the table. */
197088 if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
197089 pSession->rc = SQLITE_SCHEMA17;
197090 return;
197091 }
197092
197093 /* Grow the hash table if required */
197094 if( sessionGrowHash(0, pTab) ){
197095 pSession->rc = SQLITE_NOMEM7;
197096 return;
197097 }
197098
197099 if( pTab->bStat1 ){
197100 stat1.hook = pSession->hook;
197101 stat1.pSession = pSession;
197102 pSession->hook.pCtx = (void*)&stat1;
197103 pSession->hook.xNew = sessionStat1New;
197104 pSession->hook.xOld = sessionStat1Old;
197105 pSession->hook.xCount = sessionStat1Count;
197106 pSession->hook.xDepth = sessionStat1Depth;
197107 if( pSession->pZeroBlob==0 ){
197108 sqlite3_value *p = sqlite3ValueNew(0);
197109 if( p==0 ){
197110 rc = SQLITE_NOMEM7;
197111 goto error_out;
197112 }
197113 sqlite3ValueSetStr(p, 0, "", 0, SQLITE_STATIC((sqlite3_destructor_type)0));
197114 pSession->pZeroBlob = p;
197115 }
197116 }
197117
197118 /* Calculate the hash-key for this change. If the primary key of the row
197119 ** includes a NULL value, exit early. Such changes are ignored by the
197120 ** session module. */
197121 rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT18, &iHash, &bNull);
197122 if( rc!=SQLITE_OK0 ) goto error_out;
197123
197124 if( bNull==0 ){
197125 /* Search the hash table for an existing record for this row. */
197126 SessionChange *pC;
197127 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
197128 if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
197129 }
197130
197131 if( pC==0 ){
197132 /* Create a new change object containing all the old values (if
197133 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
197134 ** values (if this is an INSERT). */
197135 SessionChange *pChange; /* New change object */
197136 sqlite3_int64 nByte; /* Number of bytes to allocate */
197137 int i; /* Used to iterate through columns */
197138
197139 assert( rc==SQLITE_OK )((void) (0));
197140 pTab->nEntry++;
197141
197142 /* Figure out how large an allocation is required */
197143 nByte = sizeof(SessionChange);
197144 for(i=0; i<pTab->nCol; i++){
197145 sqlite3_value *p = 0;
197146 if( op!=SQLITE_INSERT18 ){
197147 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
197148 assert( trc==SQLITE_OK )((void) (0));
197149 }else if( pTab->abPK[i] ){
197150 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
197151 assert( trc==SQLITE_OK )((void) (0));
197152 }
197153
197154 /* This may fail if SQLite value p contains a utf-16 string that must
197155 ** be converted to utf-8 and an OOM error occurs while doing so. */
197156 rc = sessionSerializeValue(0, p, &nByte);
197157 if( rc!=SQLITE_OK0 ) goto error_out;
197158 }
197159
197160 /* Allocate the change object */
197161 pChange = (SessionChange *)sqlite3_malloc64(nByte);
197162 if( !pChange ){
197163 rc = SQLITE_NOMEM7;
197164 goto error_out;
197165 }else{
197166 memset(pChange, 0, sizeof(SessionChange));
197167 pChange->aRecord = (u8 *)&pChange[1];
197168 }
197169
197170 /* Populate the change object. None of the preupdate_old(),
197171 ** preupdate_new() or SerializeValue() calls below may fail as all
197172 ** required values and encodings have already been cached in memory.
197173 ** It is not possible for an OOM to occur in this block. */
197174 nByte = 0;
197175 for(i=0; i<pTab->nCol; i++){
197176 sqlite3_value *p = 0;
197177 if( op!=SQLITE_INSERT18 ){
197178 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
197179 }else if( pTab->abPK[i] ){
197180 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
197181 }
197182 sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
197183 }
197184
197185 /* Add the change to the hash-table */
197186 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
197187 pChange->bIndirect = 1;
197188 }
197189 pChange->nRecord = nByte;
197190 pChange->op = op;
197191 pChange->pNext = pTab->apChange[iHash];
197192 pTab->apChange[iHash] = pChange;
197193
197194 }else if( pC->bIndirect ){
197195 /* If the existing change is considered "indirect", but this current
197196 ** change is "direct", mark the change object as direct. */
197197 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
197198 && pSession->bIndirect==0
197199 ){
197200 pC->bIndirect = 0;
197201 }
197202 }
197203 }
197204
197205 /* If an error has occurred, mark the session object as failed. */
197206 error_out:
197207 if( pTab->bStat1 ){
197208 pSession->hook = stat1.hook;
197209 }
197210 if( rc!=SQLITE_OK0 ){
197211 pSession->rc = rc;
197212 }
197213}
197214
197215static int sessionFindTable(
197216 sqlite3_session *pSession,
197217 const char *zName,
197218 SessionTable **ppTab
197219){
197220 int rc = SQLITE_OK0;
197221 int nName = sqlite3Strlen30(zName);
197222 SessionTable *pRet;
197223
197224 /* Search for an existing table */
197225 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
197226 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
197227 }
197228
197229 if( pRet==0 && pSession->bAutoAttach ){
197230 /* If there is a table-filter configured, invoke it. If it returns 0,
197231 ** do not automatically add the new table. */
197232 if( pSession->xTableFilter==0
197233 || pSession->xTableFilter(pSession->pFilterCtx, zName)
197234 ){
197235 rc = sqlite3session_attach(pSession, zName);
197236 if( rc==SQLITE_OK0 ){
197237 for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
197238 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) )((void) (0));
197239 }
197240 }
197241 }
197242
197243 assert( rc==SQLITE_OK || pRet==0 )((void) (0));
197244 *ppTab = pRet;
197245 return rc;
197246}
197247
197248/*
197249** The 'pre-update' hook registered by this module with SQLite databases.
197250*/
197251static void xPreUpdate(
197252 void *pCtx, /* Copy of third arg to preupdate_hook() */
197253 sqlite3 *db, /* Database handle */
197254 int op, /* SQLITE_UPDATE, DELETE or INSERT */
197255 char const *zDb, /* Database name */
197256 char const *zName, /* Table name */
197257 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
197258 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
197259){
197260 sqlite3_session *pSession;
197261 int nDb = sqlite3Strlen30(zDb);
197262
197263 assert( sqlite3_mutex_held(db->mutex) )((void) (0));
197264
197265 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
197266 SessionTable *pTab;
197267
197268 /* If this session is attached to a different database ("main", "temp"
197269 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
197270 ** to the next session object attached to this database. */
197271 if( pSession->bEnable==0 ) continue;
197272 if( pSession->rc ) continue;
197273 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
197274
197275 pSession->rc = sessionFindTable(pSession, zName, &pTab);
197276 if( pTab ){
197277 assert( pSession->rc==SQLITE_OK )((void) (0));
197278 sessionPreupdateOneChange(op, pSession, pTab);
197279 if( op==SQLITE_UPDATE23 ){
197280 sessionPreupdateOneChange(SQLITE_INSERT18, pSession, pTab);
197281 }
197282 }
197283 }
197284}
197285
197286/*
197287** The pre-update hook implementations.
197288*/
197289static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
197290 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
197291}
197292static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
197293 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
197294}
197295static int sessionPreupdateCount(void *pCtx){
197296 return sqlite3_preupdate_count((sqlite3*)pCtx);
197297}
197298static int sessionPreupdateDepth(void *pCtx){
197299 return sqlite3_preupdate_depth((sqlite3*)pCtx);
197300}
197301
197302/*
197303** Install the pre-update hooks on the session object passed as the only
197304** argument.
197305*/
197306static void sessionPreupdateHooks(
197307 sqlite3_session *pSession
197308){
197309 pSession->hook.pCtx = (void*)pSession->db;
197310 pSession->hook.xOld = sessionPreupdateOld;
197311 pSession->hook.xNew = sessionPreupdateNew;
197312 pSession->hook.xCount = sessionPreupdateCount;
197313 pSession->hook.xDepth = sessionPreupdateDepth;
197314}
197315
197316typedef struct SessionDiffCtx SessionDiffCtx;
197317struct SessionDiffCtx {
197318 sqlite3_stmt *pStmt;
197319 int nOldOff;
197320};
197321
197322/*
197323** The diff hook implementations.
197324*/
197325static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
197326 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
197327 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
197328 return SQLITE_OK0;
197329}
197330static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
197331 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
197332 *ppVal = sqlite3_column_value(p->pStmt, iVal);
197333 return SQLITE_OK0;
197334}
197335static int sessionDiffCount(void *pCtx){
197336 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
197337 return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
197338}
197339static int sessionDiffDepth(void *pCtx){
197340 return 0;
197341}
197342
197343/*
197344** Install the diff hooks on the session object passed as the only
197345** argument.
197346*/
197347static void sessionDiffHooks(
197348 sqlite3_session *pSession,
197349 SessionDiffCtx *pDiffCtx
197350){
197351 pSession->hook.pCtx = (void*)pDiffCtx;
197352 pSession->hook.xOld = sessionDiffOld;
197353 pSession->hook.xNew = sessionDiffNew;
197354 pSession->hook.xCount = sessionDiffCount;
197355 pSession->hook.xDepth = sessionDiffDepth;
197356}
197357
197358static char *sessionExprComparePK(
197359 int nCol,
197360 const char *zDb1, const char *zDb2,
197361 const char *zTab,
197362 const char **azCol, u8 *abPK
197363){
197364 int i;
197365 const char *zSep = "";
197366 char *zRet = 0;
197367
197368 for(i=0; i<nCol; i++){
197369 if( abPK[i] ){
197370 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
197371 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
197372 );
197373 zSep = " AND ";
197374 if( zRet==0 ) break;
197375 }
197376 }
197377
197378 return zRet;
197379}
197380
197381static char *sessionExprCompareOther(
197382 int nCol,
197383 const char *zDb1, const char *zDb2,
197384 const char *zTab,
197385 const char **azCol, u8 *abPK
197386){
197387 int i;
197388 const char *zSep = "";
197389 char *zRet = 0;
197390 int bHave = 0;
197391
197392 for(i=0; i<nCol; i++){
197393 if( abPK[i]==0 ){
197394 bHave = 1;
197395 zRet = sqlite3_mprintf(
197396 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
197397 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
197398 );
197399 zSep = " OR ";
197400 if( zRet==0 ) break;
197401 }
197402 }
197403
197404 if( bHave==0 ){
197405 assert( zRet==0 )((void) (0));
197406 zRet = sqlite3_mprintf("0");
197407 }
197408
197409 return zRet;
197410}
197411
197412static char *sessionSelectFindNew(
197413 int nCol,
197414 const char *zDb1, /* Pick rows in this db only */
197415 const char *zDb2, /* But not in this one */
197416 const char *zTbl, /* Table name */
197417 const char *zExpr
197418){
197419 char *zRet = sqlite3_mprintf(
197420 "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
197421 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
197422 ")",
197423 zDb1, zTbl, zDb2, zTbl, zExpr
197424 );
197425 return zRet;
197426}
197427
197428static int sessionDiffFindNew(
197429 int op,
197430 sqlite3_session *pSession,
197431 SessionTable *pTab,
197432 const char *zDb1,
197433 const char *zDb2,
197434 char *zExpr
197435){
197436 int rc = SQLITE_OK0;
197437 char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
197438
197439 if( zStmt==0 ){
197440 rc = SQLITE_NOMEM7;
197441 }else{
197442 sqlite3_stmt *pStmt;
197443 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
197444 if( rc==SQLITE_OK0 ){
197445 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
197446 pDiffCtx->pStmt = pStmt;
197447 pDiffCtx->nOldOff = 0;
197448 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
197449 sessionPreupdateOneChange(op, pSession, pTab);
197450 }
197451 rc = sqlite3_finalize(pStmt);
197452 }
197453 sqlite3_free(zStmt);
197454 }
197455
197456 return rc;
197457}
197458
197459static int sessionDiffFindModified(
197460 sqlite3_session *pSession,
197461 SessionTable *pTab,
197462 const char *zFrom,
197463 const char *zExpr
197464){
197465 int rc = SQLITE_OK0;
197466
197467 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
197468 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
197469 );
197470 if( zExpr2==0 ){
197471 rc = SQLITE_NOMEM7;
197472 }else{
197473 char *zStmt = sqlite3_mprintf(
197474 "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
197475 pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
197476 );
197477 if( zStmt==0 ){
197478 rc = SQLITE_NOMEM7;
197479 }else{
197480 sqlite3_stmt *pStmt;
197481 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
197482
197483 if( rc==SQLITE_OK0 ){
197484 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
197485 pDiffCtx->pStmt = pStmt;
197486 pDiffCtx->nOldOff = pTab->nCol;
197487 while( SQLITE_ROW100==sqlite3_step(pStmt) ){
197488 sessionPreupdateOneChange(SQLITE_UPDATE23, pSession, pTab);
197489 }
197490 rc = sqlite3_finalize(pStmt);
197491 }
197492 sqlite3_free(zStmt);
197493 }
197494 }
197495
197496 return rc;
197497}
197498
197499SQLITE_API int sqlite3session_diff(
197500 sqlite3_session *pSession,
197501 const char *zFrom,
197502 const char *zTbl,
197503 char **pzErrMsg
197504){
197505 const char *zDb = pSession->zDb;
197506 int rc = pSession->rc;
197507 SessionDiffCtx d;
197508
197509 memset(&d, 0, sizeof(d));
197510 sessionDiffHooks(pSession, &d);
197511
197512 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
197513 if( pzErrMsg ) *pzErrMsg = 0;
197514 if( rc==SQLITE_OK0 ){
197515 char *zExpr = 0;
197516 sqlite3 *db = pSession->db;
197517 SessionTable *pTo; /* Table zTbl */
197518
197519 /* Locate and if necessary initialize the target table object */
197520 rc = sessionFindTable(pSession, zTbl, &pTo);
197521 if( pTo==0 ) goto diff_out;
197522 if( sessionInitTable(pSession, pTo) ){
197523 rc = pSession->rc;
197524 goto diff_out;
197525 }
197526
197527 /* Check the table schemas match */
197528 if( rc==SQLITE_OK0 ){
197529 int bHasPk = 0;
197530 int bMismatch = 0;
197531 int nCol; /* Columns in zFrom.zTbl */
197532 u8 *abPK;
197533 const char **azCol = 0;
197534 rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
197535 if( rc==SQLITE_OK0 ){
197536 if( pTo->nCol!=nCol ){
197537 bMismatch = 1;
197538 }else{
197539 int i;
197540 for(i=0; i<nCol; i++){
197541 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
197542 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
197543 if( abPK[i] ) bHasPk = 1;
197544 }
197545 }
197546 }
197547 sqlite3_free((char*)azCol);
197548 if( bMismatch ){
197549 if( pzErrMsg ){
197550 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
197551 }
197552 rc = SQLITE_SCHEMA17;
197553 }
197554 if( bHasPk==0 ){
197555 /* Ignore tables with no primary keys */
197556 goto diff_out;
197557 }
197558 }
197559
197560 if( rc==SQLITE_OK0 ){
197561 zExpr = sessionExprComparePK(pTo->nCol,
197562 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
197563 );
197564 }
197565
197566 /* Find new rows */
197567 if( rc==SQLITE_OK0 ){
197568 rc = sessionDiffFindNew(SQLITE_INSERT18, pSession, pTo, zDb, zFrom, zExpr);
197569 }
197570
197571 /* Find old rows */
197572 if( rc==SQLITE_OK0 ){
197573 rc = sessionDiffFindNew(SQLITE_DELETE9, pSession, pTo, zFrom, zDb, zExpr);
197574 }
197575
197576 /* Find modified rows */
197577 if( rc==SQLITE_OK0 ){
197578 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
197579 }
197580
197581 sqlite3_free(zExpr);
197582 }
197583
197584 diff_out:
197585 sessionPreupdateHooks(pSession);
197586 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
197587 return rc;
197588}
197589
197590/*
197591** Create a session object. This session object will record changes to
197592** database zDb attached to connection db.
197593*/
197594SQLITE_API int sqlite3session_create(
197595 sqlite3 *db, /* Database handle */
197596 const char *zDb, /* Name of db (e.g. "main") */
197597 sqlite3_session **ppSession /* OUT: New session object */
197598){
197599 sqlite3_session *pNew; /* Newly allocated session object */
197600 sqlite3_session *pOld; /* Session object already attached to db */
197601 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
197602
197603 /* Zero the output value in case an error occurs. */
197604 *ppSession = 0;
197605
197606 /* Allocate and populate the new session object. */
197607 pNew = (sqlite3_session *)sqlite3_malloc64(sizeof(sqlite3_session) + nDb + 1);
197608 if( !pNew ) return SQLITE_NOMEM7;
197609 memset(pNew, 0, sizeof(sqlite3_session));
197610 pNew->db = db;
197611 pNew->zDb = (char *)&pNew[1];
197612 pNew->bEnable = 1;
197613 memcpy(pNew->zDb, zDb, nDb+1);
197614 sessionPreupdateHooks(pNew);
197615
197616 /* Add the new session object to the linked list of session objects
197617 ** attached to database handle $db. Do this under the cover of the db
197618 ** handle mutex. */
197619 sqlite3_mutex_enter(sqlite3_db_mutex(db));
197620 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
197621 pNew->pNext = pOld;
197622 sqlite3_mutex_leave(sqlite3_db_mutex(db));
197623
197624 *ppSession = pNew;
197625 return SQLITE_OK0;
197626}
197627
197628/*
197629** Free the list of table objects passed as the first argument. The contents
197630** of the changed-rows hash tables are also deleted.
197631*/
197632static void sessionDeleteTable(SessionTable *pList){
197633 SessionTable *pNext;
197634 SessionTable *pTab;
197635
197636 for(pTab=pList; pTab; pTab=pNext){
197637 int i;
197638 pNext = pTab->pNext;
197639 for(i=0; i<pTab->nChange; i++){
197640 SessionChange *p;
197641 SessionChange *pNextChange;
197642 for(p=pTab->apChange[i]; p; p=pNextChange){
197643 pNextChange = p->pNext;
197644 sqlite3_free(p);
197645 }
197646 }
197647 sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */
197648 sqlite3_free(pTab->apChange);
197649 sqlite3_free(pTab);
197650 }
197651}
197652
197653/*
197654** Delete a session object previously allocated using sqlite3session_create().
197655*/
197656SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
197657 sqlite3 *db = pSession->db;
197658 sqlite3_session *pHead;
197659 sqlite3_session **pp;
197660
197661 /* Unlink the session from the linked list of sessions attached to the
197662 ** database handle. Hold the db mutex while doing so. */
197663 sqlite3_mutex_enter(sqlite3_db_mutex(db));
197664 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
197665 for(pp=&pHead; ALWAYS((*pp)!=0)((*pp)!=0); pp=&((*pp)->pNext)){
197666 if( (*pp)==pSession ){
197667 *pp = (*pp)->pNext;
197668 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
197669 break;
197670 }
197671 }
197672 sqlite3_mutex_leave(sqlite3_db_mutex(db));
197673 sqlite3ValueFree(pSession->pZeroBlob);
197674
197675 /* Delete all attached table objects. And the contents of their
197676 ** associated hash-tables. */
197677 sessionDeleteTable(pSession->pTable);
197678
197679 /* Free the session object itself. */
197680 sqlite3_free(pSession);
197681}
197682
197683/*
197684** Set a table filter on a Session Object.
197685*/
197686SQLITE_API void sqlite3session_table_filter(
197687 sqlite3_session *pSession,
197688 int(*xFilter)(void*, const char*),
197689 void *pCtx /* First argument passed to xFilter */
197690){
197691 pSession->bAutoAttach = 1;
197692 pSession->pFilterCtx = pCtx;
197693 pSession->xTableFilter = xFilter;
197694}
197695
197696/*
197697** Attach a table to a session. All subsequent changes made to the table
197698** while the session object is enabled will be recorded.
197699**
197700** Only tables that have a PRIMARY KEY defined may be attached. It does
197701** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
197702** or not.
197703*/
197704SQLITE_API int sqlite3session_attach(
197705 sqlite3_session *pSession, /* Session object */
197706 const char *zName /* Table name */
197707){
197708 int rc = SQLITE_OK0;
197709 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
197710
197711 if( !zName ){
197712 pSession->bAutoAttach = 1;
197713 }else{
197714 SessionTable *pTab; /* New table object (if required) */
197715 int nName; /* Number of bytes in string zName */
197716
197717 /* First search for an existing entry. If one is found, this call is
197718 ** a no-op. Return early. */
197719 nName = sqlite3Strlen30(zName);
197720 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
197721 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
197722 }
197723
197724 if( !pTab ){
197725 /* Allocate new SessionTable object. */
197726 pTab = (SessionTable *)sqlite3_malloc64(sizeof(SessionTable) + nName + 1);
197727 if( !pTab ){
197728 rc = SQLITE_NOMEM7;
197729 }else{
197730 /* Populate the new SessionTable object and link it into the list.
197731 ** The new object must be linked onto the end of the list, not
197732 ** simply added to the start of it in order to ensure that tables
197733 ** appear in the correct order when a changeset or patchset is
197734 ** eventually generated. */
197735 SessionTable **ppTab;
197736 memset(pTab, 0, sizeof(SessionTable));
197737 pTab->zName = (char *)&pTab[1];
197738 memcpy(pTab->zName, zName, nName+1);
197739 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
197740 *ppTab = pTab;
197741 }
197742 }
197743 }
197744
197745 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
197746 return rc;
197747}
197748
197749/*
197750** Ensure that there is room in the buffer to append nByte bytes of data.
197751** If not, use sqlite3_realloc() to grow the buffer so that there is.
197752**
197753** If successful, return zero. Otherwise, if an OOM condition is encountered,
197754** set *pRc to SQLITE_NOMEM and return non-zero.
197755*/
197756static int sessionBufferGrow(SessionBuffer *p, size_t nByte, int *pRc){
197757 if( *pRc==SQLITE_OK0 && (size_t)(p->nAlloc-p->nBuf)<nByte ){
197758 u8 *aNew;
197759 i64 nNew = p->nAlloc ? p->nAlloc : 128;
197760 do {
197761 nNew = nNew*2;
197762 }while( (nNew-p->nBuf)<nByte );
197763
197764 aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
197765 if( 0==aNew ){
197766 *pRc = SQLITE_NOMEM7;
197767 }else{
197768 p->aBuf = aNew;
197769 p->nAlloc = nNew;
197770 }
197771 }
197772 return (*pRc!=SQLITE_OK0);
197773}
197774
197775/*
197776** Append the value passed as the second argument to the buffer passed
197777** as the first.
197778**
197779** This function is a no-op if *pRc is non-zero when it is called.
197780** Otherwise, if an error occurs, *pRc is set to an SQLite error code
197781** before returning.
197782*/
197783static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
197784 int rc = *pRc;
197785 if( rc==SQLITE_OK0 ){
197786 sqlite3_int64 nByte = 0;
197787 rc = sessionSerializeValue(0, pVal, &nByte);
197788 sessionBufferGrow(p, nByte, &rc);
197789 if( rc==SQLITE_OK0 ){
197790 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
197791 p->nBuf += nByte;
197792 }else{
197793 *pRc = rc;
197794 }
197795 }
197796}
197797
197798/*
197799** This function is a no-op if *pRc is other than SQLITE_OK when it is
197800** called. Otherwise, append a single byte to the buffer.
197801**
197802** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
197803** returning.
197804*/
197805static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
197806 if( 0==sessionBufferGrow(p, 1, pRc) ){
197807 p->aBuf[p->nBuf++] = v;
197808 }
197809}
197810
197811/*
197812** This function is a no-op if *pRc is other than SQLITE_OK when it is
197813** called. Otherwise, append a single varint to the buffer.
197814**
197815** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
197816** returning.
197817*/
197818static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
197819 if( 0==sessionBufferGrow(p, 9, pRc) ){
197820 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
197821 }
197822}
197823
197824/*
197825** This function is a no-op if *pRc is other than SQLITE_OK when it is
197826** called. Otherwise, append a blob of data to the buffer.
197827**
197828** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
197829** returning.
197830*/
197831static void sessionAppendBlob(
197832 SessionBuffer *p,
197833 const u8 *aBlob,
197834 int nBlob,
197835 int *pRc
197836){
197837 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
197838 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
197839 p->nBuf += nBlob;
197840 }
197841}
197842
197843/*
197844** This function is a no-op if *pRc is other than SQLITE_OK when it is
197845** called. Otherwise, append a string to the buffer. All bytes in the string
197846** up to (but not including) the nul-terminator are written to the buffer.
197847**
197848** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
197849** returning.
197850*/
197851static void sessionAppendStr(
197852 SessionBuffer *p,
197853 const char *zStr,
197854 int *pRc
197855){
197856 int nStr = sqlite3Strlen30(zStr);
197857 if( 0==sessionBufferGrow(p, nStr, pRc) ){
197858 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
197859 p->nBuf += nStr;
197860 }
197861}
197862
197863/*
197864** This function is a no-op if *pRc is other than SQLITE_OK when it is
197865** called. Otherwise, append the string representation of integer iVal
197866** to the buffer. No nul-terminator is written.
197867**
197868** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
197869** returning.
197870*/
197871static void sessionAppendInteger(
197872 SessionBuffer *p, /* Buffer to append to */
197873 int iVal, /* Value to write the string rep. of */
197874 int *pRc /* IN/OUT: Error code */
197875){
197876 char aBuf[24];
197877 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
197878 sessionAppendStr(p, aBuf, pRc);
197879}
197880
197881/*
197882** This function is a no-op if *pRc is other than SQLITE_OK when it is
197883** called. Otherwise, append the string zStr enclosed in quotes (") and
197884** with any embedded quote characters escaped to the buffer. No
197885** nul-terminator byte is written.
197886**
197887** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
197888** returning.
197889*/
197890static void sessionAppendIdent(
197891 SessionBuffer *p, /* Buffer to a append to */
197892 const char *zStr, /* String to quote, escape and append */
197893 int *pRc /* IN/OUT: Error code */
197894){
197895 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
197896 if( 0==sessionBufferGrow(p, nStr, pRc) ){
197897 char *zOut = (char *)&p->aBuf[p->nBuf];
197898 const char *zIn = zStr;
197899 *zOut++ = '"';
197900 while( *zIn ){
197901 if( *zIn=='"' ) *zOut++ = '"';
197902 *zOut++ = *(zIn++);
197903 }
197904 *zOut++ = '"';
197905 p->nBuf = (int)((u8 *)zOut - p->aBuf);
197906 }
197907}
197908
197909/*
197910** This function is a no-op if *pRc is other than SQLITE_OK when it is
197911** called. Otherwse, it appends the serialized version of the value stored
197912** in column iCol of the row that SQL statement pStmt currently points
197913** to to the buffer.
197914*/
197915static void sessionAppendCol(
197916 SessionBuffer *p, /* Buffer to append to */
197917 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
197918 int iCol, /* Column to read value from */
197919 int *pRc /* IN/OUT: Error code */
197920){
197921 if( *pRc==SQLITE_OK0 ){
197922 int eType = sqlite3_column_type(pStmt, iCol);
197923 sessionAppendByte(p, (u8)eType, pRc);
197924 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
197925 sqlite3_int64 i;
197926 u8 aBuf[8];
197927 if( eType==SQLITE_INTEGER1 ){
197928 i = sqlite3_column_int64(pStmt, iCol);
197929 }else{
197930 double r = sqlite3_column_double(pStmt, iCol);
197931 memcpy(&i, &r, 8);
197932 }
197933 sessionPutI64(aBuf, i);
197934 sessionAppendBlob(p, aBuf, 8, pRc);
197935 }
197936 if( eType==SQLITE_BLOB4 || eType==SQLITE_TEXT3 ){
197937 u8 *z;
197938 int nByte;
197939 if( eType==SQLITE_BLOB4 ){
197940 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
197941 }else{
197942 z = (u8 *)sqlite3_column_text(pStmt, iCol);
197943 }
197944 nByte = sqlite3_column_bytes(pStmt, iCol);
197945 if( z || (eType==SQLITE_BLOB4 && nByte==0) ){
197946 sessionAppendVarint(p, nByte, pRc);
197947 sessionAppendBlob(p, z, nByte, pRc);
197948 }else{
197949 *pRc = SQLITE_NOMEM7;
197950 }
197951 }
197952 }
197953}
197954
197955/*
197956**
197957** This function appends an update change to the buffer (see the comments
197958** under "CHANGESET FORMAT" at the top of the file). An update change
197959** consists of:
197960**
197961** 1 byte: SQLITE_UPDATE (0x17)
197962** n bytes: old.* record (see RECORD FORMAT)
197963** m bytes: new.* record (see RECORD FORMAT)
197964**
197965** The SessionChange object passed as the third argument contains the
197966** values that were stored in the row when the session began (the old.*
197967** values). The statement handle passed as the second argument points
197968** at the current version of the row (the new.* values).
197969**
197970** If all of the old.* values are equal to their corresponding new.* value
197971** (i.e. nothing has changed), then no data at all is appended to the buffer.
197972**
197973** Otherwise, the old.* record contains all primary key values and the
197974** original values of any fields that have been modified. The new.* record
197975** contains the new values of only those fields that have been modified.
197976*/
197977static int sessionAppendUpdate(
197978 SessionBuffer *pBuf, /* Buffer to append to */
197979 int bPatchset, /* True for "patchset", 0 for "changeset" */
197980 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
197981 SessionChange *p, /* Object containing old values */
197982 u8 *abPK /* Boolean array - true for PK columns */
197983){
197984 int rc = SQLITE_OK0;
197985 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
197986 int bNoop = 1; /* Set to zero if any values are modified */
197987 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
197988 int i; /* Used to iterate through columns */
197989 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
197990
197991 sessionAppendByte(pBuf, SQLITE_UPDATE23, &rc);
197992 sessionAppendByte(pBuf, p->bIndirect, &rc);
197993 for(i=0; i<sqlite3_column_count(pStmt); i++){
197994 int bChanged = 0;
197995 int nAdvance;
197996 int eType = *pCsr;
197997 switch( eType ){
197998 case SQLITE_NULL5:
197999 nAdvance = 1;
198000 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL5 ){
198001 bChanged = 1;
198002 }
198003 break;
198004
198005 case SQLITE_FLOAT2:
198006 case SQLITE_INTEGER1: {
198007 nAdvance = 9;
198008 if( eType==sqlite3_column_type(pStmt, i) ){
198009 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
198010 if( eType==SQLITE_INTEGER1 ){
198011 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
198012 }else{
198013 double dVal;
198014 memcpy(&dVal, &iVal, 8);
198015 if( dVal==sqlite3_column_double(pStmt, i) ) break;
198016 }
198017 }
198018 bChanged = 1;
198019 break;
198020 }
198021
198022 default: {
198023 int n;
198024 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
198025 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB )((void) (0));
198026 nAdvance = nHdr + n;
198027 if( eType==sqlite3_column_type(pStmt, i)
198028 && n==sqlite3_column_bytes(pStmt, i)
198029 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
198030 ){
198031 break;
198032 }
198033 bChanged = 1;
198034 }
198035 }
198036
198037 /* If at least one field has been modified, this is not a no-op. */
198038 if( bChanged ) bNoop = 0;
198039
198040 /* Add a field to the old.* record. This is omitted if this modules is
198041 ** currently generating a patchset. */
198042 if( bPatchset==0 ){
198043 if( bChanged || abPK[i] ){
198044 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
198045 }else{
198046 sessionAppendByte(pBuf, 0, &rc);
198047 }
198048 }
198049
198050 /* Add a field to the new.* record. Or the only record if currently
198051 ** generating a patchset. */
198052 if( bChanged || (bPatchset && abPK[i]) ){
198053 sessionAppendCol(&buf2, pStmt, i, &rc);
198054 }else{
198055 sessionAppendByte(&buf2, 0, &rc);
198056 }
198057
198058 pCsr += nAdvance;
198059 }
198060
198061 if( bNoop ){
198062 pBuf->nBuf = nRewind;
198063 }else{
198064 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
198065 }
198066 sqlite3_free(buf2.aBuf);
198067
198068 return rc;
198069}
198070
198071/*
198072** Append a DELETE change to the buffer passed as the first argument. Use
198073** the changeset format if argument bPatchset is zero, or the patchset
198074** format otherwise.
198075*/
198076static int sessionAppendDelete(
198077 SessionBuffer *pBuf, /* Buffer to append to */
198078 int bPatchset, /* True for "patchset", 0 for "changeset" */
198079 SessionChange *p, /* Object containing old values */
198080 int nCol, /* Number of columns in table */
198081 u8 *abPK /* Boolean array - true for PK columns */
198082){
198083 int rc = SQLITE_OK0;
198084
198085 sessionAppendByte(pBuf, SQLITE_DELETE9, &rc);
198086 sessionAppendByte(pBuf, p->bIndirect, &rc);
198087
198088 if( bPatchset==0 ){
198089 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
198090 }else{
198091 int i;
198092 u8 *a = p->aRecord;
198093 for(i=0; i<nCol; i++){
198094 u8 *pStart = a;
198095 int eType = *a++;
198096
198097 switch( eType ){
198098 case 0:
198099 case SQLITE_NULL5:
198100 assert( abPK[i]==0 )((void) (0));
198101 break;
198102
198103 case SQLITE_FLOAT2:
198104 case SQLITE_INTEGER1:
198105 a += 8;
198106 break;
198107
198108 default: {
198109 int n;
198110 a += sessionVarintGet(a, &n);
198111 a += n;
198112 break;
198113 }
198114 }
198115 if( abPK[i] ){
198116 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
198117 }
198118 }
198119 assert( (a - p->aRecord)==p->nRecord )((void) (0));
198120 }
198121
198122 return rc;
198123}
198124
198125/*
198126** Formulate and prepare a SELECT statement to retrieve a row from table
198127** zTab in database zDb based on its primary key. i.e.
198128**
198129** SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
198130*/
198131static int sessionSelectStmt(
198132 sqlite3 *db, /* Database handle */
198133 const char *zDb, /* Database name */
198134 const char *zTab, /* Table name */
198135 int nCol, /* Number of columns in table */
198136 const char **azCol, /* Names of table columns */
198137 u8 *abPK, /* PRIMARY KEY array */
198138 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
198139){
198140 int rc = SQLITE_OK0;
198141 char *zSql = 0;
198142 int nSql = -1;
198143
198144 if( 0==sqlite3_stricmp("sqlite_stat1", zTab) ){
198145 zSql = sqlite3_mprintf(
198146 "SELECT tbl, ?2, stat FROM %Q.sqlite_stat1 WHERE tbl IS ?1 AND "
198147 "idx IS (CASE WHEN ?2=X'' THEN NULL ELSE ?2 END)", zDb
198148 );
198149 if( zSql==0 ) rc = SQLITE_NOMEM7;
198150 }else{
198151 int i;
198152 const char *zSep = "";
198153 SessionBuffer buf = {0, 0, 0};
198154
198155 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
198156 sessionAppendIdent(&buf, zDb, &rc);
198157 sessionAppendStr(&buf, ".", &rc);
198158 sessionAppendIdent(&buf, zTab, &rc);
198159 sessionAppendStr(&buf, " WHERE ", &rc);
198160 for(i=0; i<nCol; i++){
198161 if( abPK[i] ){
198162 sessionAppendStr(&buf, zSep, &rc);
198163 sessionAppendIdent(&buf, azCol[i], &rc);
198164 sessionAppendStr(&buf, " IS ?", &rc);
198165 sessionAppendInteger(&buf, i+1, &rc);
198166 zSep = " AND ";
198167 }
198168 }
198169 zSql = (char*)buf.aBuf;
198170 nSql = buf.nBuf;
198171 }
198172
198173 if( rc==SQLITE_OK0 ){
198174 rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, 0);
198175 }
198176 sqlite3_free(zSql);
198177 return rc;
198178}
198179
198180/*
198181** Bind the PRIMARY KEY values from the change passed in argument pChange
198182** to the SELECT statement passed as the first argument. The SELECT statement
198183** is as prepared by function sessionSelectStmt().
198184**
198185** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
198186** error code (e.g. SQLITE_NOMEM) otherwise.
198187*/
198188static int sessionSelectBind(
198189 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
198190 int nCol, /* Number of columns in table */
198191 u8 *abPK, /* PRIMARY KEY array */
198192 SessionChange *pChange /* Change structure */
198193){
198194 int i;
198195 int rc = SQLITE_OK0;
198196 u8 *a = pChange->aRecord;
198197
198198 for(i=0; i<nCol && rc==SQLITE_OK0; i++){
198199 int eType = *a++;
198200
198201 switch( eType ){
198202 case 0:
198203 case SQLITE_NULL5:
198204 assert( abPK[i]==0 )((void) (0));
198205 break;
198206
198207 case SQLITE_INTEGER1: {
198208 if( abPK[i] ){
198209 i64 iVal = sessionGetI64(a);
198210 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
198211 }
198212 a += 8;
198213 break;
198214 }
198215
198216 case SQLITE_FLOAT2: {
198217 if( abPK[i] ){
198218 double rVal;
198219 i64 iVal = sessionGetI64(a);
198220 memcpy(&rVal, &iVal, 8);
198221 rc = sqlite3_bind_double(pSelect, i+1, rVal);
198222 }
198223 a += 8;
198224 break;
198225 }
198226
198227 case SQLITE_TEXT3: {
198228 int n;
198229 a += sessionVarintGet(a, &n);
198230 if( abPK[i] ){
198231 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
198232 }
198233 a += n;
198234 break;
198235 }
198236
198237 default: {
198238 int n;
198239 assert( eType==SQLITE_BLOB )((void) (0));
198240 a += sessionVarintGet(a, &n);
198241 if( abPK[i] ){
198242 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
198243 }
198244 a += n;
198245 break;
198246 }
198247 }
198248 }
198249
198250 return rc;
198251}
198252
198253/*
198254** This function is a no-op if *pRc is set to other than SQLITE_OK when it
198255** is called. Otherwise, append a serialized table header (part of the binary
198256** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
198257** SQLite error code before returning.
198258*/
198259static void sessionAppendTableHdr(
198260 SessionBuffer *pBuf, /* Append header to this buffer */
198261 int bPatchset, /* Use the patchset format if true */
198262 SessionTable *pTab, /* Table object to append header for */
198263 int *pRc /* IN/OUT: Error code */
198264){
198265 /* Write a table header */
198266 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
198267 sessionAppendVarint(pBuf, pTab->nCol, pRc);
198268 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
198269 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
198270}
198271
198272/*
198273** Generate either a changeset (if argument bPatchset is zero) or a patchset
198274** (if it is non-zero) based on the current contents of the session object
198275** passed as the first argument.
198276**
198277** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
198278** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
198279** occurs, an SQLite error code is returned and both output variables set
198280** to 0.
198281*/
198282static int sessionGenerateChangeset(
198283 sqlite3_session *pSession, /* Session object */
198284 int bPatchset, /* True for patchset, false for changeset */
198285 int (*xOutput)(void *pOut, const void *pData, int nData),
198286 void *pOut, /* First argument for xOutput */
198287 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
198288 void **ppChangeset /* OUT: Buffer containing changeset */
198289){
198290 sqlite3 *db = pSession->db; /* Source database handle */
198291 SessionTable *pTab; /* Used to iterate through attached tables */
198292 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
198293 int rc; /* Return code */
198294
198295 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) )((void) (0));
198296
198297 /* Zero the output variables in case an error occurs. If this session
198298 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
198299 ** this call will be a no-op. */
198300 if( xOutput==0 ){
198301 *pnChangeset = 0;
198302 *ppChangeset = 0;
198303 }
198304
198305 if( pSession->rc ) return pSession->rc;
198306 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
198307 if( rc!=SQLITE_OK0 ) return rc;
198308
198309 sqlite3_mutex_enter(sqlite3_db_mutex(db));
198310
198311 for(pTab=pSession->pTable; rc==SQLITE_OK0 && pTab; pTab=pTab->pNext){
198312 if( pTab->nEntry ){
198313 const char *zName = pTab->zName;
198314 int nCol; /* Number of columns in table */
198315 u8 *abPK; /* Primary key array */
198316 const char **azCol = 0; /* Table columns */
198317 int i; /* Used to iterate through hash buckets */
198318 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
198319 int nRewind = buf.nBuf; /* Initial size of write buffer */
198320 int nNoop; /* Size of buffer after writing tbl header */
198321
198322 /* Check the table schema is still Ok. */
198323 rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
198324 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
198325 rc = SQLITE_SCHEMA17;
198326 }
198327
198328 /* Write a table header */
198329 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
198330
198331 /* Build and compile a statement to execute: */
198332 if( rc==SQLITE_OK0 ){
198333 rc = sessionSelectStmt(
198334 db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
198335 }
198336
198337 nNoop = buf.nBuf;
198338 for(i=0; i<pTab->nChange && rc==SQLITE_OK0; i++){
198339 SessionChange *p; /* Used to iterate through changes */
198340
198341 for(p=pTab->apChange[i]; rc==SQLITE_OK0 && p; p=p->pNext){
198342 rc = sessionSelectBind(pSel, nCol, abPK, p);
198343 if( rc!=SQLITE_OK0 ) continue;
198344 if( sqlite3_step(pSel)==SQLITE_ROW100 ){
198345 if( p->op==SQLITE_INSERT18 ){
198346 int iCol;
198347 sessionAppendByte(&buf, SQLITE_INSERT18, &rc);
198348 sessionAppendByte(&buf, p->bIndirect, &rc);
198349 for(iCol=0; iCol<nCol; iCol++){
198350 sessionAppendCol(&buf, pSel, iCol, &rc);
198351 }
198352 }else{
198353 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
198354 }
198355 }else if( p->op!=SQLITE_INSERT18 ){
198356 rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
198357 }
198358 if( rc==SQLITE_OK0 ){
198359 rc = sqlite3_reset(pSel);
198360 }
198361
198362 /* If the buffer is now larger than sessions_strm_chunk_size, pass
198363 ** its contents to the xOutput() callback. */
198364 if( xOutput
198365 && rc==SQLITE_OK0
198366 && buf.nBuf>nNoop
198367 && buf.nBuf>sessions_strm_chunk_size
198368 ){
198369 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
198370 nNoop = -1;
198371 buf.nBuf = 0;
198372 }
198373
198374 }
198375 }
198376
198377 sqlite3_finalize(pSel);
198378 if( buf.nBuf==nNoop ){
198379 buf.nBuf = nRewind;
198380 }
198381 sqlite3_free((char*)azCol); /* cast works around VC++ bug */
198382 }
198383 }
198384
198385 if( rc==SQLITE_OK0 ){
198386 if( xOutput==0 ){
198387 *pnChangeset = buf.nBuf;
198388 *ppChangeset = buf.aBuf;
198389 buf.aBuf = 0;
198390 }else if( buf.nBuf>0 ){
198391 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
198392 }
198393 }
198394
198395 sqlite3_free(buf.aBuf);
198396 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
198397 sqlite3_mutex_leave(sqlite3_db_mutex(db));
198398 return rc;
198399}
198400
198401/*
198402** Obtain a changeset object containing all changes recorded by the
198403** session object passed as the first argument.
198404**
198405** It is the responsibility of the caller to eventually free the buffer
198406** using sqlite3_free().
198407*/
198408SQLITE_API int sqlite3session_changeset(
198409 sqlite3_session *pSession, /* Session object */
198410 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
198411 void **ppChangeset /* OUT: Buffer containing changeset */
198412){
198413 return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
198414}
198415
198416/*
198417** Streaming version of sqlite3session_changeset().
198418*/
198419SQLITE_API int sqlite3session_changeset_strm(
198420 sqlite3_session *pSession,
198421 int (*xOutput)(void *pOut, const void *pData, int nData),
198422 void *pOut
198423){
198424 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
198425}
198426
198427/*
198428** Streaming version of sqlite3session_patchset().
198429*/
198430SQLITE_API int sqlite3session_patchset_strm(
198431 sqlite3_session *pSession,
198432 int (*xOutput)(void *pOut, const void *pData, int nData),
198433 void *pOut
198434){
198435 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
198436}
198437
198438/*
198439** Obtain a patchset object containing all changes recorded by the
198440** session object passed as the first argument.
198441**
198442** It is the responsibility of the caller to eventually free the buffer
198443** using sqlite3_free().
198444*/
198445SQLITE_API int sqlite3session_patchset(
198446 sqlite3_session *pSession, /* Session object */
198447 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
198448 void **ppPatchset /* OUT: Buffer containing changeset */
198449){
198450 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
198451}
198452
198453/*
198454** Enable or disable the session object passed as the first argument.
198455*/
198456SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
198457 int ret;
198458 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
198459 if( bEnable>=0 ){
198460 pSession->bEnable = bEnable;
198461 }
198462 ret = pSession->bEnable;
198463 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
198464 return ret;
198465}
198466
198467/*
198468** Enable or disable the session object passed as the first argument.
198469*/
198470SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect){
198471 int ret;
198472 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
198473 if( bIndirect>=0 ){
198474 pSession->bIndirect = bIndirect;
198475 }
198476 ret = pSession->bIndirect;
198477 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
198478 return ret;
198479}
198480
198481/*
198482** Return true if there have been no changes to monitored tables recorded
198483** by the session object passed as the only argument.
198484*/
198485SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
198486 int ret = 0;
198487 SessionTable *pTab;
198488
198489 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
198490 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
198491 ret = (pTab->nEntry>0);
198492 }
198493 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
198494
198495 return (ret==0);
198496}
198497
198498/*
198499** Do the work for either sqlite3changeset_start() or start_strm().
198500*/
198501static int sessionChangesetStart(
198502 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
198503 int (*xInput)(void *pIn, void *pData, int *pnData),
198504 void *pIn,
198505 int nChangeset, /* Size of buffer pChangeset in bytes */
198506 void *pChangeset, /* Pointer to buffer containing changeset */
198507 int bInvert /* True to invert changeset */
198508){
198509 sqlite3_changeset_iter *pRet; /* Iterator to return */
198510 int nByte; /* Number of bytes to allocate for iterator */
198511
198512 assert( xInput==0 || (pChangeset==0 && nChangeset==0) )((void) (0));
198513
198514 /* Zero the output variable in case an error occurs. */
198515 *pp = 0;
198516
198517 /* Allocate and initialize the iterator structure. */
198518 nByte = sizeof(sqlite3_changeset_iter);
198519 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
198520 if( !pRet ) return SQLITE_NOMEM7;
198521 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
198522 pRet->in.aData = (u8 *)pChangeset;
198523 pRet->in.nData = nChangeset;
198524 pRet->in.xInput = xInput;
198525 pRet->in.pIn = pIn;
198526 pRet->in.bEof = (xInput ? 0 : 1);
198527 pRet->bInvert = bInvert;
198528
198529 /* Populate the output variable and return success. */
198530 *pp = pRet;
198531 return SQLITE_OK0;
198532}
198533
198534/*
198535** Create an iterator used to iterate through the contents of a changeset.
198536*/
198537SQLITE_API int sqlite3changeset_start(
198538 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
198539 int nChangeset, /* Size of buffer pChangeset in bytes */
198540 void *pChangeset /* Pointer to buffer containing changeset */
198541){
198542 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, 0);
198543}
198544SQLITE_API int sqlite3changeset_start_v2(
198545 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
198546 int nChangeset, /* Size of buffer pChangeset in bytes */
198547 void *pChangeset, /* Pointer to buffer containing changeset */
198548 int flags
198549){
198550 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
198551 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset, bInvert);
198552}
198553
198554/*
198555** Streaming version of sqlite3changeset_start().
198556*/
198557SQLITE_API int sqlite3changeset_start_strm(
198558 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
198559 int (*xInput)(void *pIn, void *pData, int *pnData),
198560 void *pIn
198561){
198562 return sessionChangesetStart(pp, xInput, pIn, 0, 0, 0);
198563}
198564SQLITE_API int sqlite3changeset_start_v2_strm(
198565 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
198566 int (*xInput)(void *pIn, void *pData, int *pnData),
198567 void *pIn,
198568 int flags
198569){
198570 int bInvert = !!(flags & SQLITE_CHANGESETSTART_INVERT);
198571 return sessionChangesetStart(pp, xInput, pIn, 0, 0, bInvert);
198572}
198573
198574/*
198575** If the SessionInput object passed as the only argument is a streaming
198576** object and the buffer is full, discard some data to free up space.
198577*/
198578static void sessionDiscardData(SessionInput *pIn){
198579 if( pIn->xInput && pIn->iNext>=sessions_strm_chunk_size ){
198580 int nMove = pIn->buf.nBuf - pIn->iNext;
198581 assert( nMove>=0 )((void) (0));
198582 if( nMove>0 ){
198583 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
198584 }
198585 pIn->buf.nBuf -= pIn->iNext;
198586 pIn->iNext = 0;
198587 pIn->nData = pIn->buf.nBuf;
198588 }
198589}
198590
198591/*
198592** Ensure that there are at least nByte bytes available in the buffer. Or,
198593** if there are not nByte bytes remaining in the input, that all available
198594** data is in the buffer.
198595**
198596** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
198597*/
198598static int sessionInputBuffer(SessionInput *pIn, int nByte){
198599 int rc = SQLITE_OK0;
198600 if( pIn->xInput ){
198601 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK0 ){
198602 int nNew = sessions_strm_chunk_size;
198603
198604 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
198605 if( SQLITE_OK0==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
198606 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
198607 if( nNew==0 ){
198608 pIn->bEof = 1;
198609 }else{
198610 pIn->buf.nBuf += nNew;
198611 }
198612 }
198613
198614 pIn->aData = pIn->buf.aBuf;
198615 pIn->nData = pIn->buf.nBuf;
198616 }
198617 }
198618 return rc;
198619}
198620
198621/*
198622** When this function is called, *ppRec points to the start of a record
198623** that contains nCol values. This function advances the pointer *ppRec
198624** until it points to the byte immediately following that record.
198625*/
198626static void sessionSkipRecord(
198627 u8 **ppRec, /* IN/OUT: Record pointer */
198628 int nCol /* Number of values in record */
198629){
198630 u8 *aRec = *ppRec;
198631 int i;
198632 for(i=0; i<nCol; i++){
198633 int eType = *aRec++;
198634 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
198635 int nByte;
198636 aRec += sessionVarintGet((u8*)aRec, &nByte);
198637 aRec += nByte;
198638 }else if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
198639 aRec += 8;
198640 }
198641 }
198642
198643 *ppRec = aRec;
198644}
198645
198646/*
198647** This function sets the value of the sqlite3_value object passed as the
198648** first argument to a copy of the string or blob held in the aData[]
198649** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
198650** error occurs.
198651*/
198652static int sessionValueSetStr(
198653 sqlite3_value *pVal, /* Set the value of this object */
198654 u8 *aData, /* Buffer containing string or blob data */
198655 int nData, /* Size of buffer aData[] in bytes */
198656 u8 enc /* String encoding (0 for blobs) */
198657){
198658 /* In theory this code could just pass SQLITE_TRANSIENT as the final
198659 ** argument to sqlite3ValueSetStr() and have the copy created
198660 ** automatically. But doing so makes it difficult to detect any OOM
198661 ** error. Hence the code to create the copy externally. */
198662 u8 *aCopy = sqlite3_malloc64((sqlite3_int64)nData+1);
198663 if( aCopy==0 ) return SQLITE_NOMEM7;
198664 memcpy(aCopy, aData, nData);
198665 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
198666 return SQLITE_OK0;
198667}
198668
198669/*
198670** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
198671** for details.
198672**
198673** When this function is called, *paChange points to the start of the record
198674** to deserialize. Assuming no error occurs, *paChange is set to point to
198675** one byte after the end of the same record before this function returns.
198676** If the argument abPK is NULL, then the record contains nCol values. Or,
198677** if abPK is other than NULL, then the record contains only the PK fields
198678** (in other words, it is a patchset DELETE record).
198679**
198680** If successful, each element of the apOut[] array (allocated by the caller)
198681** is set to point to an sqlite3_value object containing the value read
198682** from the corresponding position in the record. If that value is not
198683** included in the record (i.e. because the record is part of an UPDATE change
198684** and the field was not modified), the corresponding element of apOut[] is
198685** set to NULL.
198686**
198687** It is the responsibility of the caller to free all sqlite_value structures
198688** using sqlite3_free().
198689**
198690** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
198691** The apOut[] array may have been partially populated in this case.
198692*/
198693static int sessionReadRecord(
198694 SessionInput *pIn, /* Input data */
198695 int nCol, /* Number of values in record */
198696 u8 *abPK, /* Array of primary key flags, or NULL */
198697 sqlite3_value **apOut /* Write values to this array */
198698){
198699 int i; /* Used to iterate through columns */
198700 int rc = SQLITE_OK0;
198701
198702 for(i=0; i<nCol && rc==SQLITE_OK0; i++){
198703 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
198704 if( abPK && abPK[i]==0 ) continue;
198705 rc = sessionInputBuffer(pIn, 9);
198706 if( rc==SQLITE_OK0 ){
198707 if( pIn->iNext>=pIn->nData ){
198708 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(198708);
198709 }else{
198710 eType = pIn->aData[pIn->iNext++];
198711 assert( apOut[i]==0 )((void) (0));
198712 if( eType ){
198713 apOut[i] = sqlite3ValueNew(0);
198714 if( !apOut[i] ) rc = SQLITE_NOMEM7;
198715 }
198716 }
198717 }
198718
198719 if( rc==SQLITE_OK0 ){
198720 u8 *aVal = &pIn->aData[pIn->iNext];
198721 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
198722 int nByte;
198723 pIn->iNext += sessionVarintGet(aVal, &nByte);
198724 rc = sessionInputBuffer(pIn, nByte);
198725 if( rc==SQLITE_OK0 ){
198726 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
198727 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(198727);
198728 }else{
198729 u8 enc = (eType==SQLITE_TEXT3 ? SQLITE_UTF81 : 0);
198730 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
198731 pIn->iNext += nByte;
198732 }
198733 }
198734 }
198735 if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
198736 sqlite3_int64 v = sessionGetI64(aVal);
198737 if( eType==SQLITE_INTEGER1 ){
198738 sqlite3VdbeMemSetInt64(apOut[i], v);
198739 }else{
198740 double d;
198741 memcpy(&d, &v, 8);
198742 sqlite3VdbeMemSetDouble(apOut[i], d);
198743 }
198744 pIn->iNext += 8;
198745 }
198746 }
198747 }
198748
198749 return rc;
198750}
198751
198752/*
198753** The input pointer currently points to the second byte of a table-header.
198754** Specifically, to the following:
198755**
198756** + number of columns in table (varint)
198757** + array of PK flags (1 byte per column),
198758** + table name (nul terminated).
198759**
198760** This function ensures that all of the above is present in the input
198761** buffer (i.e. that it can be accessed without any calls to xInput()).
198762** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
198763** The input pointer is not moved.
198764*/
198765static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
198766 int rc = SQLITE_OK0;
198767 int nCol = 0;
198768 int nRead = 0;
198769
198770 rc = sessionInputBuffer(pIn, 9);
198771 if( rc==SQLITE_OK0 ){
198772 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
198773 /* The hard upper limit for the number of columns in an SQLite
198774 ** database table is, according to sqliteLimit.h, 32676. So
198775 ** consider any table-header that purports to have more than 65536
198776 ** columns to be corrupt. This is convenient because otherwise,
198777 ** if the (nCol>65536) condition below were omitted, a sufficiently
198778 ** large value for nCol may cause nRead to wrap around and become
198779 ** negative. Leading to a crash. */
198780 if( nCol<0 || nCol>65536 ){
198781 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(198781);
198782 }else{
198783 rc = sessionInputBuffer(pIn, nRead+nCol+100);
198784 nRead += nCol;
198785 }
198786 }
198787
198788 while( rc==SQLITE_OK0 ){
198789 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
198790 nRead++;
198791 }
198792 if( (pIn->iNext + nRead)<pIn->nData ) break;
198793 rc = sessionInputBuffer(pIn, nRead + 100);
198794 }
198795 *pnByte = nRead+1;
198796 return rc;
198797}
198798
198799/*
198800** The input pointer currently points to the first byte of the first field
198801** of a record consisting of nCol columns. This function ensures the entire
198802** record is buffered. It does not move the input pointer.
198803**
198804** If successful, SQLITE_OK is returned and *pnByte is set to the size of
198805** the record in bytes. Otherwise, an SQLite error code is returned. The
198806** final value of *pnByte is undefined in this case.
198807*/
198808static int sessionChangesetBufferRecord(
198809 SessionInput *pIn, /* Input data */
198810 int nCol, /* Number of columns in record */
198811 int *pnByte /* OUT: Size of record in bytes */
198812){
198813 int rc = SQLITE_OK0;
198814 int nByte = 0;
198815 int i;
198816 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
198817 int eType;
198818 rc = sessionInputBuffer(pIn, nByte + 10);
198819 if( rc==SQLITE_OK0 ){
198820 eType = pIn->aData[pIn->iNext + nByte++];
198821 if( eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4 ){
198822 int n;
198823 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
198824 nByte += n;
198825 rc = sessionInputBuffer(pIn, nByte);
198826 }else if( eType==SQLITE_INTEGER1 || eType==SQLITE_FLOAT2 ){
198827 nByte += 8;
198828 }
198829 }
198830 }
198831 *pnByte = nByte;
198832 return rc;
198833}
198834
198835/*
198836** The input pointer currently points to the second byte of a table-header.
198837** Specifically, to the following:
198838**
198839** + number of columns in table (varint)
198840** + array of PK flags (1 byte per column),
198841** + table name (nul terminated).
198842**
198843** This function decodes the table-header and populates the p->nCol,
198844** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
198845** also allocated or resized according to the new value of p->nCol. The
198846** input pointer is left pointing to the byte following the table header.
198847**
198848** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
198849** is returned and the final values of the various fields enumerated above
198850** are undefined.
198851*/
198852static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
198853 int rc;
198854 int nCopy;
198855 assert( p->rc==SQLITE_OK )((void) (0));
198856
198857 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
198858 if( rc==SQLITE_OK0 ){
198859 int nByte;
198860 int nVarint;
198861 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
198862 if( p->nCol>0 ){
198863 nCopy -= nVarint;
198864 p->in.iNext += nVarint;
198865 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
198866 p->tblhdr.nBuf = 0;
198867 sessionBufferGrow(&p->tblhdr, nByte, &rc);
198868 }else{
198869 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(198869);
198870 }
198871 }
198872
198873 if( rc==SQLITE_OK0 ){
198874 size_t iPK = sizeof(sqlite3_value*)*p->nCol*2;
198875 memset(p->tblhdr.aBuf, 0, iPK);
198876 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
198877 p->in.iNext += nCopy;
198878 }
198879
198880 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
198881 p->abPK = (u8*)&p->apValue[p->nCol*2];
198882 p->zTab = (char*)&p->abPK[p->nCol];
198883 return (p->rc = rc);
198884}
198885
198886/*
198887** Advance the changeset iterator to the next change.
198888**
198889** If both paRec and pnRec are NULL, then this function works like the public
198890** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
198891** sqlite3changeset_new() and old() APIs may be used to query for values.
198892**
198893** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
198894** record is written to *paRec before returning and the number of bytes in
198895** the record to *pnRec.
198896**
198897** Either way, this function returns SQLITE_ROW if the iterator is
198898** successfully advanced to the next change in the changeset, an SQLite
198899** error code if an error occurs, or SQLITE_DONE if there are no further
198900** changes in the changeset.
198901*/
198902static int sessionChangesetNext(
198903 sqlite3_changeset_iter *p, /* Changeset iterator */
198904 u8 **paRec, /* If non-NULL, store record pointer here */
198905 int *pnRec, /* If non-NULL, store size of record here */
198906 int *pbNew /* If non-NULL, true if new table */
198907){
198908 int i;
198909 u8 op;
198910
198911 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) )((void) (0));
198912
198913 /* If the iterator is in the error-state, return immediately. */
198914 if( p->rc!=SQLITE_OK0 ) return p->rc;
198915
198916 /* Free the current contents of p->apValue[], if any. */
198917 if( p->apValue ){
198918 for(i=0; i<p->nCol*2; i++){
198919 sqlite3ValueFree(p->apValue[i]);
198920 }
198921 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
198922 }
198923
198924 /* Make sure the buffer contains at least 10 bytes of input data, or all
198925 ** remaining data if there are less than 10 bytes available. This is
198926 ** sufficient either for the 'T' or 'P' byte and the varint that follows
198927 ** it, or for the two single byte values otherwise. */
198928 p->rc = sessionInputBuffer(&p->in, 2);
198929 if( p->rc!=SQLITE_OK0 ) return p->rc;
198930
198931 /* If the iterator is already at the end of the changeset, return DONE. */
198932 if( p->in.iNext>=p->in.nData ){
198933 return SQLITE_DONE101;
198934 }
198935
198936 sessionDiscardData(&p->in);
198937 p->in.iCurrent = p->in.iNext;
198938
198939 op = p->in.aData[p->in.iNext++];
198940 while( op=='T' || op=='P' ){
198941 if( pbNew ) *pbNew = 1;
198942 p->bPatchset = (op=='P');
198943 if( sessionChangesetReadTblhdr(p) ) return p->rc;
198944 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
198945 p->in.iCurrent = p->in.iNext;
198946 if( p->in.iNext>=p->in.nData ) return SQLITE_DONE101;
198947 op = p->in.aData[p->in.iNext++];
198948 }
198949
198950 if( p->zTab==0 || (p->bPatchset && p->bInvert) ){
198951 /* The first record in the changeset is not a table header. Must be a
198952 ** corrupt changeset. */
198953 assert( p->in.iNext==1 || p->zTab )((void) (0));
198954 return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(198954));
198955 }
198956
198957 p->op = op;
198958 p->bIndirect = p->in.aData[p->in.iNext++];
198959 if( p->op!=SQLITE_UPDATE23 && p->op!=SQLITE_DELETE9 && p->op!=SQLITE_INSERT18 ){
198960 return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(198960));
198961 }
198962
198963 if( paRec ){
198964 int nVal; /* Number of values to buffer */
198965 if( p->bPatchset==0 && op==SQLITE_UPDATE23 ){
198966 nVal = p->nCol * 2;
198967 }else if( p->bPatchset && op==SQLITE_DELETE9 ){
198968 nVal = 0;
198969 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
198970 }else{
198971 nVal = p->nCol;
198972 }
198973 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
198974 if( p->rc!=SQLITE_OK0 ) return p->rc;
198975 *paRec = &p->in.aData[p->in.iNext];
198976 p->in.iNext += *pnRec;
198977 }else{
198978 sqlite3_value **apOld = (p->bInvert ? &p->apValue[p->nCol] : p->apValue);
198979 sqlite3_value **apNew = (p->bInvert ? p->apValue : &p->apValue[p->nCol]);
198980
198981 /* If this is an UPDATE or DELETE, read the old.* record. */
198982 if( p->op!=SQLITE_INSERT18 && (p->bPatchset==0 || p->op==SQLITE_DELETE9) ){
198983 u8 *abPK = p->bPatchset ? p->abPK : 0;
198984 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, apOld);
198985 if( p->rc!=SQLITE_OK0 ) return p->rc;
198986 }
198987
198988 /* If this is an INSERT or UPDATE, read the new.* record. */
198989 if( p->op!=SQLITE_DELETE9 ){
198990 p->rc = sessionReadRecord(&p->in, p->nCol, 0, apNew);
198991 if( p->rc!=SQLITE_OK0 ) return p->rc;
198992 }
198993
198994 if( (p->bPatchset || p->bInvert) && p->op==SQLITE_UPDATE23 ){
198995 /* If this is an UPDATE that is part of a patchset, then all PK and
198996 ** modified fields are present in the new.* record. The old.* record
198997 ** is currently completely empty. This block shifts the PK fields from
198998 ** new.* to old.*, to accommodate the code that reads these arrays. */
198999 for(i=0; i<p->nCol; i++){
199000 assert( p->bPatchset==0 || p->apValue[i]==0 )((void) (0));
199001 if( p->abPK[i] ){
199002 assert( p->apValue[i]==0 )((void) (0));
199003 p->apValue[i] = p->apValue[i+p->nCol];
199004 if( p->apValue[i]==0 ) return (p->rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(199004));
199005 p->apValue[i+p->nCol] = 0;
199006 }
199007 }
199008 }else if( p->bInvert ){
199009 if( p->op==SQLITE_INSERT18 ) p->op = SQLITE_DELETE9;
199010 else if( p->op==SQLITE_DELETE9 ) p->op = SQLITE_INSERT18;
199011 }
199012 }
199013
199014 return SQLITE_ROW100;
199015}
199016
199017/*
199018** Advance an iterator created by sqlite3changeset_start() to the next
199019** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
199020** or SQLITE_CORRUPT.
199021**
199022** This function may not be called on iterators passed to a conflict handler
199023** callback by changeset_apply().
199024*/
199025SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
199026 return sessionChangesetNext(p, 0, 0, 0);
199027}
199028
199029/*
199030** The following function extracts information on the current change
199031** from a changeset iterator. It may only be called after changeset_next()
199032** has returned SQLITE_ROW.
199033*/
199034SQLITE_API int sqlite3changeset_op(
199035 sqlite3_changeset_iter *pIter, /* Iterator handle */
199036 const char **pzTab, /* OUT: Pointer to table name */
199037 int *pnCol, /* OUT: Number of columns in table */
199038 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
199039 int *pbIndirect /* OUT: True if change is indirect */
199040){
199041 *pOp = pIter->op;
199042 *pnCol = pIter->nCol;
199043 *pzTab = pIter->zTab;
199044 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
199045 return SQLITE_OK0;
199046}
199047
199048/*
199049** Return information regarding the PRIMARY KEY and number of columns in
199050** the database table affected by the change that pIter currently points
199051** to. This function may only be called after changeset_next() returns
199052** SQLITE_ROW.
199053*/
199054SQLITE_API int sqlite3changeset_pk(
199055 sqlite3_changeset_iter *pIter, /* Iterator object */
199056 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
199057 int *pnCol /* OUT: Number of entries in output array */
199058){
199059 *pabPK = pIter->abPK;
199060 if( pnCol ) *pnCol = pIter->nCol;
199061 return SQLITE_OK0;
199062}
199063
199064/*
199065** This function may only be called while the iterator is pointing to an
199066** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
199067** Otherwise, SQLITE_MISUSE is returned.
199068**
199069** It sets *ppValue to point to an sqlite3_value structure containing the
199070** iVal'th value in the old.* record. Or, if that particular value is not
199071** included in the record (because the change is an UPDATE and the field
199072** was not modified and is not a PK column), set *ppValue to NULL.
199073**
199074** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
199075** not modified. Otherwise, SQLITE_OK.
199076*/
199077SQLITE_API int sqlite3changeset_old(
199078 sqlite3_changeset_iter *pIter, /* Changeset iterator */
199079 int iVal, /* Index of old.* value to retrieve */
199080 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
199081){
199082 if( pIter->op!=SQLITE_UPDATE23 && pIter->op!=SQLITE_DELETE9 ){
199083 return SQLITE_MISUSE21;
199084 }
199085 if( iVal<0 || iVal>=pIter->nCol ){
199086 return SQLITE_RANGE25;
199087 }
199088 *ppValue = pIter->apValue[iVal];
199089 return SQLITE_OK0;
199090}
199091
199092/*
199093** This function may only be called while the iterator is pointing to an
199094** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
199095** Otherwise, SQLITE_MISUSE is returned.
199096**
199097** It sets *ppValue to point to an sqlite3_value structure containing the
199098** iVal'th value in the new.* record. Or, if that particular value is not
199099** included in the record (because the change is an UPDATE and the field
199100** was not modified), set *ppValue to NULL.
199101**
199102** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
199103** not modified. Otherwise, SQLITE_OK.
199104*/
199105SQLITE_API int sqlite3changeset_new(
199106 sqlite3_changeset_iter *pIter, /* Changeset iterator */
199107 int iVal, /* Index of new.* value to retrieve */
199108 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
199109){
199110 if( pIter->op!=SQLITE_UPDATE23 && pIter->op!=SQLITE_INSERT18 ){
199111 return SQLITE_MISUSE21;
199112 }
199113 if( iVal<0 || iVal>=pIter->nCol ){
199114 return SQLITE_RANGE25;
199115 }
199116 *ppValue = pIter->apValue[pIter->nCol+iVal];
199117 return SQLITE_OK0;
199118}
199119
199120/*
199121** The following two macros are used internally. They are similar to the
199122** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
199123** they omit all error checking and return a pointer to the requested value.
199124*/
199125#define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
199126#define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
199127
199128/*
199129** This function may only be called with a changeset iterator that has been
199130** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
199131** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
199132**
199133** If successful, *ppValue is set to point to an sqlite3_value structure
199134** containing the iVal'th value of the conflicting record.
199135**
199136** If value iVal is out-of-range or some other error occurs, an SQLite error
199137** code is returned. Otherwise, SQLITE_OK.
199138*/
199139SQLITE_API int sqlite3changeset_conflict(
199140 sqlite3_changeset_iter *pIter, /* Changeset iterator */
199141 int iVal, /* Index of conflict record value to fetch */
199142 sqlite3_value **ppValue /* OUT: Value from conflicting row */
199143){
199144 if( !pIter->pConflict ){
199145 return SQLITE_MISUSE21;
199146 }
199147 if( iVal<0 || iVal>=pIter->nCol ){
199148 return SQLITE_RANGE25;
199149 }
199150 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
199151 return SQLITE_OK0;
199152}
199153
199154/*
199155** This function may only be called with an iterator passed to an
199156** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
199157** it sets the output variable to the total number of known foreign key
199158** violations in the destination database and returns SQLITE_OK.
199159**
199160** In all other cases this function returns SQLITE_MISUSE.
199161*/
199162SQLITE_API int sqlite3changeset_fk_conflicts(
199163 sqlite3_changeset_iter *pIter, /* Changeset iterator */
199164 int *pnOut /* OUT: Number of FK violations */
199165){
199166 if( pIter->pConflict || pIter->apValue ){
199167 return SQLITE_MISUSE21;
199168 }
199169 *pnOut = pIter->nCol;
199170 return SQLITE_OK0;
199171}
199172
199173
199174/*
199175** Finalize an iterator allocated with sqlite3changeset_start().
199176**
199177** This function may not be called on iterators passed to a conflict handler
199178** callback by changeset_apply().
199179*/
199180SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
199181 int rc = SQLITE_OK0;
199182 if( p ){
199183 int i; /* Used to iterate through p->apValue[] */
199184 rc = p->rc;
199185 if( p->apValue ){
199186 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
199187 }
199188 sqlite3_free(p->tblhdr.aBuf);
199189 sqlite3_free(p->in.buf.aBuf);
199190 sqlite3_free(p);
199191 }
199192 return rc;
199193}
199194
199195static int sessionChangesetInvert(
199196 SessionInput *pInput, /* Input changeset */
199197 int (*xOutput)(void *pOut, const void *pData, int nData),
199198 void *pOut,
199199 int *pnInverted, /* OUT: Number of bytes in output changeset */
199200 void **ppInverted /* OUT: Inverse of pChangeset */
199201){
199202 int rc = SQLITE_OK0; /* Return value */
199203 SessionBuffer sOut; /* Output buffer */
199204 int nCol = 0; /* Number of cols in current table */
199205 u8 *abPK = 0; /* PK array for current table */
199206 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
199207 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
199208
199209 /* Initialize the output buffer */
199210 memset(&sOut, 0, sizeof(SessionBuffer));
199211
199212 /* Zero the output variables in case an error occurs. */
199213 if( ppInverted ){
199214 *ppInverted = 0;
199215 *pnInverted = 0;
199216 }
199217
199218 while( 1 ){
199219 u8 eType;
199220
199221 /* Test for EOF. */
199222 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
199223 if( pInput->iNext>=pInput->nData ) break;
199224 eType = pInput->aData[pInput->iNext];
199225
199226 switch( eType ){
199227 case 'T': {
199228 /* A 'table' record consists of:
199229 **
199230 ** * A constant 'T' character,
199231 ** * Number of columns in said table (a varint),
199232 ** * An array of nCol bytes (sPK),
199233 ** * A nul-terminated table name.
199234 */
199235 int nByte;
199236 int nVar;
199237 pInput->iNext++;
199238 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
199239 goto finished_invert;
199240 }
199241 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
199242 sPK.nBuf = 0;
199243 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
199244 sessionAppendByte(&sOut, eType, &rc);
199245 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
199246 if( rc ) goto finished_invert;
199247
199248 pInput->iNext += nByte;
199249 sqlite3_free(apVal);
199250 apVal = 0;
199251 abPK = sPK.aBuf;
199252 break;
199253 }
199254
199255 case SQLITE_INSERT18:
199256 case SQLITE_DELETE9: {
199257 int nByte;
199258 int bIndirect = pInput->aData[pInput->iNext+1];
199259 int eType2 = (eType==SQLITE_DELETE9 ? SQLITE_INSERT18 : SQLITE_DELETE9);
199260 pInput->iNext += 2;
199261 assert( rc==SQLITE_OK )((void) (0));
199262 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
199263 sessionAppendByte(&sOut, eType2, &rc);
199264 sessionAppendByte(&sOut, bIndirect, &rc);
199265 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
199266 pInput->iNext += nByte;
199267 if( rc ) goto finished_invert;
199268 break;
199269 }
199270
199271 case SQLITE_UPDATE23: {
199272 int iCol;
199273
199274 if( 0==apVal ){
199275 apVal = (sqlite3_value **)sqlite3_malloc64(sizeof(apVal[0])*nCol*2);
199276 if( 0==apVal ){
199277 rc = SQLITE_NOMEM7;
199278 goto finished_invert;
199279 }
199280 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
199281 }
199282
199283 /* Write the header for the new UPDATE change. Same as the original. */
199284 sessionAppendByte(&sOut, eType, &rc);
199285 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
199286
199287 /* Read the old.* and new.* records for the update change. */
199288 pInput->iNext += 2;
199289 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
199290 if( rc==SQLITE_OK0 ){
199291 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
199292 }
199293
199294 /* Write the new old.* record. Consists of the PK columns from the
199295 ** original old.* record, and the other values from the original
199296 ** new.* record. */
199297 for(iCol=0; iCol<nCol; iCol++){
199298 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
199299 sessionAppendValue(&sOut, pVal, &rc);
199300 }
199301
199302 /* Write the new new.* record. Consists of a copy of all values
199303 ** from the original old.* record, except for the PK columns, which
199304 ** are set to "undefined". */
199305 for(iCol=0; iCol<nCol; iCol++){
199306 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
199307 sessionAppendValue(&sOut, pVal, &rc);
199308 }
199309
199310 for(iCol=0; iCol<nCol*2; iCol++){
199311 sqlite3ValueFree(apVal[iCol]);
199312 }
199313 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
199314 if( rc!=SQLITE_OK0 ){
199315 goto finished_invert;
199316 }
199317
199318 break;
199319 }
199320
199321 default:
199322 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(199322);
199323 goto finished_invert;
199324 }
199325
199326 assert( rc==SQLITE_OK )((void) (0));
199327 if( xOutput && sOut.nBuf>=sessions_strm_chunk_size ){
199328 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
199329 sOut.nBuf = 0;
199330 if( rc!=SQLITE_OK0 ) goto finished_invert;
199331 }
199332 }
199333
199334 assert( rc==SQLITE_OK )((void) (0));
199335 if( pnInverted ){
199336 *pnInverted = sOut.nBuf;
199337 *ppInverted = sOut.aBuf;
199338 sOut.aBuf = 0;
199339 }else if( sOut.nBuf>0 ){
199340 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
199341 }
199342
199343 finished_invert:
199344 sqlite3_free(sOut.aBuf);
199345 sqlite3_free(apVal);
199346 sqlite3_free(sPK.aBuf);
199347 return rc;
199348}
199349
199350
199351/*
199352** Invert a changeset object.
199353*/
199354SQLITE_API int sqlite3changeset_invert(
199355 int nChangeset, /* Number of bytes in input */
199356 const void *pChangeset, /* Input changeset */
199357 int *pnInverted, /* OUT: Number of bytes in output changeset */
199358 void **ppInverted /* OUT: Inverse of pChangeset */
199359){
199360 SessionInput sInput;
199361
199362 /* Set up the input stream */
199363 memset(&sInput, 0, sizeof(SessionInput));
199364 sInput.nData = nChangeset;
199365 sInput.aData = (u8*)pChangeset;
199366
199367 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
199368}
199369
199370/*
199371** Streaming version of sqlite3changeset_invert().
199372*/
199373SQLITE_API int sqlite3changeset_invert_strm(
199374 int (*xInput)(void *pIn, void *pData, int *pnData),
199375 void *pIn,
199376 int (*xOutput)(void *pOut, const void *pData, int nData),
199377 void *pOut
199378){
199379 SessionInput sInput;
199380 int rc;
199381
199382 /* Set up the input stream */
199383 memset(&sInput, 0, sizeof(SessionInput));
199384 sInput.xInput = xInput;
199385 sInput.pIn = pIn;
199386
199387 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
199388 sqlite3_free(sInput.buf.aBuf);
199389 return rc;
199390}
199391
199392typedef struct SessionApplyCtx SessionApplyCtx;
199393struct SessionApplyCtx {
199394 sqlite3 *db;
199395 sqlite3_stmt *pDelete; /* DELETE statement */
199396 sqlite3_stmt *pUpdate; /* UPDATE statement */
199397 sqlite3_stmt *pInsert; /* INSERT statement */
199398 sqlite3_stmt *pSelect; /* SELECT statement */
199399 int nCol; /* Size of azCol[] and abPK[] arrays */
199400 const char **azCol; /* Array of column names */
199401 u8 *abPK; /* Boolean array - true if column is in PK */
199402 int bStat1; /* True if table is sqlite_stat1 */
199403 int bDeferConstraints; /* True to defer constraints */
199404 SessionBuffer constraints; /* Deferred constraints are stored here */
199405 SessionBuffer rebase; /* Rebase information (if any) here */
199406 u8 bRebaseStarted; /* If table header is already in rebase */
199407 u8 bRebase; /* True to collect rebase information */
199408};
199409
199410/*
199411** Formulate a statement to DELETE a row from database db. Assuming a table
199412** structure like this:
199413**
199414** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
199415**
199416** The DELETE statement looks like this:
199417**
199418** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
199419**
199420** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
199421** matching b and d values, or 1 otherwise. The second case comes up if the
199422** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
199423**
199424** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
199425** pointing to the prepared version of the SQL statement.
199426*/
199427static int sessionDeleteRow(
199428 sqlite3 *db, /* Database handle */
199429 const char *zTab, /* Table name */
199430 SessionApplyCtx *p /* Session changeset-apply context */
199431){
199432 int i;
199433 const char *zSep = "";
199434 int rc = SQLITE_OK0;
199435 SessionBuffer buf = {0, 0, 0};
199436 int nPk = 0;
199437
199438 sessionAppendStr(&buf, "DELETE FROM ", &rc);
199439 sessionAppendIdent(&buf, zTab, &rc);
199440 sessionAppendStr(&buf, " WHERE ", &rc);
199441
199442 for(i=0; i<p->nCol; i++){
199443 if( p->abPK[i] ){
199444 nPk++;
199445 sessionAppendStr(&buf, zSep, &rc);
199446 sessionAppendIdent(&buf, p->azCol[i], &rc);
199447 sessionAppendStr(&buf, " = ?", &rc);
199448 sessionAppendInteger(&buf, i+1, &rc);
199449 zSep = " AND ";
199450 }
199451 }
199452
199453 if( nPk<p->nCol ){
199454 sessionAppendStr(&buf, " AND (?", &rc);
199455 sessionAppendInteger(&buf, p->nCol+1, &rc);
199456 sessionAppendStr(&buf, " OR ", &rc);
199457
199458 zSep = "";
199459 for(i=0; i<p->nCol; i++){
199460 if( !p->abPK[i] ){
199461 sessionAppendStr(&buf, zSep, &rc);
199462 sessionAppendIdent(&buf, p->azCol[i], &rc);
199463 sessionAppendStr(&buf, " IS ?", &rc);
199464 sessionAppendInteger(&buf, i+1, &rc);
199465 zSep = "AND ";
199466 }
199467 }
199468 sessionAppendStr(&buf, ")", &rc);
199469 }
199470
199471 if( rc==SQLITE_OK0 ){
199472 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
199473 }
199474 sqlite3_free(buf.aBuf);
199475
199476 return rc;
199477}
199478
199479/*
199480** Formulate and prepare a statement to UPDATE a row from database db.
199481** Assuming a table structure like this:
199482**
199483** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
199484**
199485** The UPDATE statement looks like this:
199486**
199487** UPDATE x SET
199488** a = CASE WHEN ?2 THEN ?3 ELSE a END,
199489** b = CASE WHEN ?5 THEN ?6 ELSE b END,
199490** c = CASE WHEN ?8 THEN ?9 ELSE c END,
199491** d = CASE WHEN ?11 THEN ?12 ELSE d END
199492** WHERE a = ?1 AND c = ?7 AND (?13 OR
199493** (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
199494** )
199495**
199496** For each column in the table, there are three variables to bind:
199497**
199498** ?(i*3+1) The old.* value of the column, if any.
199499** ?(i*3+2) A boolean flag indicating that the value is being modified.
199500** ?(i*3+3) The new.* value of the column, if any.
199501**
199502** Also, a boolean flag that, if set to true, causes the statement to update
199503** a row even if the non-PK values do not match. This is required if the
199504** conflict-handler is invoked with CHANGESET_DATA and returns
199505** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
199506**
199507** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
199508** pointing to the prepared version of the SQL statement.
199509*/
199510static int sessionUpdateRow(
199511 sqlite3 *db, /* Database handle */
199512 const char *zTab, /* Table name */
199513 SessionApplyCtx *p /* Session changeset-apply context */
199514){
199515 int rc = SQLITE_OK0;
199516 int i;
199517 const char *zSep = "";
199518 SessionBuffer buf = {0, 0, 0};
199519
199520 /* Append "UPDATE tbl SET " */
199521 sessionAppendStr(&buf, "UPDATE ", &rc);
199522 sessionAppendIdent(&buf, zTab, &rc);
199523 sessionAppendStr(&buf, " SET ", &rc);
199524
199525 /* Append the assignments */
199526 for(i=0; i<p->nCol; i++){
199527 sessionAppendStr(&buf, zSep, &rc);
199528 sessionAppendIdent(&buf, p->azCol[i], &rc);
199529 sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
199530 sessionAppendInteger(&buf, i*3+2, &rc);
199531 sessionAppendStr(&buf, " THEN ?", &rc);
199532 sessionAppendInteger(&buf, i*3+3, &rc);
199533 sessionAppendStr(&buf, " ELSE ", &rc);
199534 sessionAppendIdent(&buf, p->azCol[i], &rc);
199535 sessionAppendStr(&buf, " END", &rc);
199536 zSep = ", ";
199537 }
199538
199539 /* Append the PK part of the WHERE clause */
199540 sessionAppendStr(&buf, " WHERE ", &rc);
199541 for(i=0; i<p->nCol; i++){
199542 if( p->abPK[i] ){
199543 sessionAppendIdent(&buf, p->azCol[i], &rc);
199544 sessionAppendStr(&buf, " = ?", &rc);
199545 sessionAppendInteger(&buf, i*3+1, &rc);
199546 sessionAppendStr(&buf, " AND ", &rc);
199547 }
199548 }
199549
199550 /* Append the non-PK part of the WHERE clause */
199551 sessionAppendStr(&buf, " (?", &rc);
199552 sessionAppendInteger(&buf, p->nCol*3+1, &rc);
199553 sessionAppendStr(&buf, " OR 1", &rc);
199554 for(i=0; i<p->nCol; i++){
199555 if( !p->abPK[i] ){
199556 sessionAppendStr(&buf, " AND (?", &rc);
199557 sessionAppendInteger(&buf, i*3+2, &rc);
199558 sessionAppendStr(&buf, "=0 OR ", &rc);
199559 sessionAppendIdent(&buf, p->azCol[i], &rc);
199560 sessionAppendStr(&buf, " IS ?", &rc);
199561 sessionAppendInteger(&buf, i*3+1, &rc);
199562 sessionAppendStr(&buf, ")", &rc);
199563 }
199564 }
199565 sessionAppendStr(&buf, ")", &rc);
199566
199567 if( rc==SQLITE_OK0 ){
199568 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
199569 }
199570 sqlite3_free(buf.aBuf);
199571
199572 return rc;
199573}
199574
199575
199576/*
199577** Formulate and prepare an SQL statement to query table zTab by primary
199578** key. Assuming the following table structure:
199579**
199580** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
199581**
199582** The SELECT statement looks like this:
199583**
199584** SELECT * FROM x WHERE a = ?1 AND c = ?3
199585**
199586** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
199587** pointing to the prepared version of the SQL statement.
199588*/
199589static int sessionSelectRow(
199590 sqlite3 *db, /* Database handle */
199591 const char *zTab, /* Table name */
199592 SessionApplyCtx *p /* Session changeset-apply context */
199593){
199594 return sessionSelectStmt(
199595 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
199596}
199597
199598/*
199599** Formulate and prepare an INSERT statement to add a record to table zTab.
199600** For example:
199601**
199602** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
199603**
199604** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
199605** pointing to the prepared version of the SQL statement.
199606*/
199607static int sessionInsertRow(
199608 sqlite3 *db, /* Database handle */
199609 const char *zTab, /* Table name */
199610 SessionApplyCtx *p /* Session changeset-apply context */
199611){
199612 int rc = SQLITE_OK0;
199613 int i;
199614 SessionBuffer buf = {0, 0, 0};
199615
199616 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
199617 sessionAppendIdent(&buf, zTab, &rc);
199618 sessionAppendStr(&buf, "(", &rc);
199619 for(i=0; i<p->nCol; i++){
199620 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
199621 sessionAppendIdent(&buf, p->azCol[i], &rc);
199622 }
199623
199624 sessionAppendStr(&buf, ") VALUES(?", &rc);
199625 for(i=1; i<p->nCol; i++){
199626 sessionAppendStr(&buf, ", ?", &rc);
199627 }
199628 sessionAppendStr(&buf, ")", &rc);
199629
199630 if( rc==SQLITE_OK0 ){
199631 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
199632 }
199633 sqlite3_free(buf.aBuf);
199634 return rc;
199635}
199636
199637static int sessionPrepare(sqlite3 *db, sqlite3_stmt **pp, const char *zSql){
199638 return sqlite3_prepare_v2(db, zSql, -1, pp, 0);
199639}
199640
199641/*
199642** Prepare statements for applying changes to the sqlite_stat1 table.
199643** These are similar to those created by sessionSelectRow(),
199644** sessionInsertRow(), sessionUpdateRow() and sessionDeleteRow() for
199645** other tables.
199646*/
199647static int sessionStat1Sql(sqlite3 *db, SessionApplyCtx *p){
199648 int rc = sessionSelectRow(db, "sqlite_stat1", p);
199649 if( rc==SQLITE_OK0 ){
199650 rc = sessionPrepare(db, &p->pInsert,
199651 "INSERT INTO main.sqlite_stat1 VALUES(?1, "
199652 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END, "
199653 "?3)"
199654 );
199655 }
199656 if( rc==SQLITE_OK0 ){
199657 rc = sessionPrepare(db, &p->pUpdate,
199658 "UPDATE main.sqlite_stat1 SET "
199659 "tbl = CASE WHEN ?2 THEN ?3 ELSE tbl END, "
199660 "idx = CASE WHEN ?5 THEN ?6 ELSE idx END, "
199661 "stat = CASE WHEN ?8 THEN ?9 ELSE stat END "
199662 "WHERE tbl=?1 AND idx IS "
199663 "CASE WHEN length(?4)=0 AND typeof(?4)='blob' THEN NULL ELSE ?4 END "
199664 "AND (?10 OR ?8=0 OR stat IS ?7)"
199665 );
199666 }
199667 if( rc==SQLITE_OK0 ){
199668 rc = sessionPrepare(db, &p->pDelete,
199669 "DELETE FROM main.sqlite_stat1 WHERE tbl=?1 AND idx IS "
199670 "CASE WHEN length(?2)=0 AND typeof(?2)='blob' THEN NULL ELSE ?2 END "
199671 "AND (?4 OR stat IS ?3)"
199672 );
199673 }
199674 return rc;
199675}
199676
199677/*
199678** A wrapper around sqlite3_bind_value() that detects an extra problem.
199679** See comments in the body of this function for details.
199680*/
199681static int sessionBindValue(
199682 sqlite3_stmt *pStmt, /* Statement to bind value to */
199683 int i, /* Parameter number to bind to */
199684 sqlite3_value *pVal /* Value to bind */
199685){
199686 int eType = sqlite3_value_type(pVal);
199687 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
199688 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
199689 ** the (pVal->z) variable remains as it was or the type of the value is
199690 ** set to SQLITE_NULL. */
199691 if( (eType==SQLITE_TEXT3 || eType==SQLITE_BLOB4) && pVal->z==0 ){
199692 /* This condition occurs when an earlier OOM in a call to
199693 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
199694 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
199695 return SQLITE_NOMEM7;
199696 }
199697 return sqlite3_bind_value(pStmt, i, pVal);
199698}
199699
199700/*
199701** Iterator pIter must point to an SQLITE_INSERT entry. This function
199702** transfers new.* values from the current iterator entry to statement
199703** pStmt. The table being inserted into has nCol columns.
199704**
199705** New.* value $i from the iterator is bound to variable ($i+1) of
199706** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
199707** are transfered to the statement. Otherwise, if abPK is not NULL, it points
199708** to an array nCol elements in size. In this case only those values for
199709** which abPK[$i] is true are read from the iterator and bound to the
199710** statement.
199711**
199712** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
199713*/
199714static int sessionBindRow(
199715 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
199716 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
199717 int nCol, /* Number of columns */
199718 u8 *abPK, /* If not NULL, bind only if true */
199719 sqlite3_stmt *pStmt /* Bind values to this statement */
199720){
199721 int i;
199722 int rc = SQLITE_OK0;
199723
199724 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
199725 ** argument iterator points to a suitable entry. Make sure that xValue
199726 ** is one of these to guarantee that it is safe to ignore the return
199727 ** in the code below. */
199728 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new )((void) (0));
199729
199730 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
199731 if( !abPK || abPK[i] ){
199732 sqlite3_value *pVal;
199733 (void)xValue(pIter, i, &pVal);
199734 if( pVal==0 ){
199735 /* The value in the changeset was "undefined". This indicates a
199736 ** corrupt changeset blob. */
199737 rc = SQLITE_CORRUPT_BKPTsqlite3CorruptError(199737);
199738 }else{
199739 rc = sessionBindValue(pStmt, i+1, pVal);
199740 }
199741 }
199742 }
199743 return rc;
199744}
199745
199746/*
199747** SQL statement pSelect is as generated by the sessionSelectRow() function.
199748** This function binds the primary key values from the change that changeset
199749** iterator pIter points to to the SELECT and attempts to seek to the table
199750** entry. If a row is found, the SELECT statement left pointing at the row
199751** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
199752** has occured, the statement is reset and SQLITE_OK is returned. If an
199753** error occurs, the statement is reset and an SQLite error code is returned.
199754**
199755** If this function returns SQLITE_ROW, the caller must eventually reset()
199756** statement pSelect. If any other value is returned, the statement does
199757** not require a reset().
199758**
199759** If the iterator currently points to an INSERT record, bind values from the
199760** new.* record to the SELECT statement. Or, if it points to a DELETE or
199761** UPDATE, bind values from the old.* record.
199762*/
199763static int sessionSeekToRow(
199764 sqlite3 *db, /* Database handle */
199765 sqlite3_changeset_iter *pIter, /* Changeset iterator */
199766 u8 *abPK, /* Primary key flags array */
199767 sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */
199768){
199769 int rc; /* Return code */
199770 int nCol; /* Number of columns in table */
199771 int op; /* Changset operation (SQLITE_UPDATE etc.) */
199772 const char *zDummy; /* Unused */
199773
199774 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
199775 rc = sessionBindRow(pIter,
199776 op==SQLITE_INSERT18 ? sqlite3changeset_new : sqlite3changeset_old,
199777 nCol, abPK, pSelect
199778 );
199779
199780 if( rc==SQLITE_OK0 ){
199781 rc = sqlite3_step(pSelect);
199782 if( rc!=SQLITE_ROW100 ) rc = sqlite3_reset(pSelect);
199783 }
199784
199785 return rc;
199786}
199787
199788/*
199789** This function is called from within sqlite3changeset_apply_v2() when
199790** a conflict is encountered and resolved using conflict resolution
199791** mode eType (either SQLITE_CHANGESET_OMIT or SQLITE_CHANGESET_REPLACE)..
199792** It adds a conflict resolution record to the buffer in
199793** SessionApplyCtx.rebase, which will eventually be returned to the caller
199794** of apply_v2() as the "rebase" buffer.
199795**
199796** Return SQLITE_OK if successful, or an SQLite error code otherwise.
199797*/
199798static int sessionRebaseAdd(
199799 SessionApplyCtx *p, /* Apply context */
199800 int eType, /* Conflict resolution (OMIT or REPLACE) */
199801 sqlite3_changeset_iter *pIter /* Iterator pointing at current change */
199802){
199803 int rc = SQLITE_OK0;
199804 if( p->bRebase ){
199805 int i;
199806 int eOp = pIter->op;
199807 if( p->bRebaseStarted==0 ){
199808 /* Append a table-header to the rebase buffer */
199809 const char *zTab = pIter->zTab;
199810 sessionAppendByte(&p->rebase, 'T', &rc);
199811 sessionAppendVarint(&p->rebase, p->nCol, &rc);
199812 sessionAppendBlob(&p->rebase, p->abPK, p->nCol, &rc);
199813 sessionAppendBlob(&p->rebase, (u8*)zTab, (int)strlen(zTab)+1, &rc);
199814 p->bRebaseStarted = 1;
199815 }
199816
199817 assert( eType==SQLITE_CHANGESET_REPLACE||eType==SQLITE_CHANGESET_OMIT )((void) (0));
199818 assert( eOp==SQLITE_DELETE || eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE )((void) (0));
199819
199820 sessionAppendByte(&p->rebase,
199821 (eOp==SQLITE_DELETE9 ? SQLITE_DELETE9 : SQLITE_INSERT18), &rc
199822 );
199823 sessionAppendByte(&p->rebase, (eType==SQLITE_CHANGESET_REPLACE), &rc);
199824 for(i=0; i<p->nCol; i++){
199825 sqlite3_value *pVal = 0;
199826 if( eOp==SQLITE_DELETE9 || (eOp==SQLITE_UPDATE23 && p->abPK[i]) ){
199827 sqlite3changeset_old(pIter, i, &pVal);
199828 }else{
199829 sqlite3changeset_new(pIter, i, &pVal);
199830 }
199831 sessionAppendValue(&p->rebase, pVal, &rc);
199832 }
199833 }
199834 return rc;
199835}
199836
199837/*
199838** Invoke the conflict handler for the change that the changeset iterator
199839** currently points to.
199840**
199841** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
199842** If argument pbReplace is NULL, then the type of conflict handler invoked
199843** depends solely on eType, as follows:
199844**
199845** eType value Value passed to xConflict
199846** -------------------------------------------------
199847** CHANGESET_DATA CHANGESET_NOTFOUND
199848** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
199849**
199850** Or, if pbReplace is not NULL, then an attempt is made to find an existing
199851** record with the same primary key as the record about to be deleted, updated
199852** or inserted. If such a record can be found, it is available to the conflict
199853** handler as the "conflicting" record. In this case the type of conflict
199854** handler invoked is as follows:
199855**
199856** eType value PK Record found? Value passed to xConflict
199857** ----------------------------------------------------------------
199858** CHANGESET_DATA Yes CHANGESET_DATA
199859** CHANGESET_DATA No CHANGESET_NOTFOUND
199860** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
199861** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
199862**
199863** If pbReplace is not NULL, and a record with a matching PK is found, and
199864** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
199865** is set to non-zero before returning SQLITE_OK.
199866**
199867** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
199868** returned. Or, if the conflict handler returns an invalid value,
199869** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
199870** this function returns SQLITE_OK.
199871*/
199872static int sessionConflictHandler(
199873 int eType, /* Either CHANGESET_DATA or CONFLICT */
199874 SessionApplyCtx *p, /* changeset_apply() context */
199875 sqlite3_changeset_iter *pIter, /* Changeset iterator */
199876 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
199877 void *pCtx, /* First argument for conflict handler */
199878 int *pbReplace /* OUT: Set to true if PK row is found */
199879){
199880 int res = 0; /* Value returned by conflict handler */
199881 int rc;
199882 int nCol;
199883 int op;
199884 const char *zDummy;
199885
199886 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
199887
199888 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA )((void) (0));
199889 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT )((void) (0));
199890 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND )((void) (0));
199891
199892 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
199893 if( pbReplace ){
199894 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
199895 }else{
199896 rc = SQLITE_OK0;
199897 }
199898
199899 if( rc==SQLITE_ROW100 ){
199900 /* There exists another row with the new.* primary key. */
199901 pIter->pConflict = p->pSelect;
199902 res = xConflict(pCtx, eType, pIter);
199903 pIter->pConflict = 0;
199904 rc = sqlite3_reset(p->pSelect);
199905 }else if( rc==SQLITE_OK0 ){
199906 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
199907 /* Instead of invoking the conflict handler, append the change blob
199908 ** to the SessionApplyCtx.constraints buffer. */
199909 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
199910 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
199911 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
199912 return SQLITE_OK0;
199913 }else{
199914 /* No other row with the new.* primary key. */
199915 res = xConflict(pCtx, eType+1, pIter);
199916 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE21;
199917 }
199918 }
199919
199920 if( rc==SQLITE_OK0 ){
199921 switch( res ){
199922 case SQLITE_CHANGESET_REPLACE:
199923 assert( pbReplace )((void) (0));
199924 *pbReplace = 1;
199925 break;
199926
199927 case SQLITE_CHANGESET_OMIT:
199928 break;
199929
199930 case SQLITE_CHANGESET_ABORT:
199931 rc = SQLITE_ABORT4;
199932 break;
199933
199934 default:
199935 rc = SQLITE_MISUSE21;
199936 break;
199937 }
199938 if( rc==SQLITE_OK0 ){
199939 rc = sessionRebaseAdd(p, res, pIter);
199940 }
199941 }
199942
199943 return rc;
199944}
199945
199946/*
199947** Attempt to apply the change that the iterator passed as the first argument
199948** currently points to to the database. If a conflict is encountered, invoke
199949** the conflict handler callback.
199950**
199951** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
199952** one is encountered, update or delete the row with the matching primary key
199953** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
199954** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
199955** to true before returning. In this case the caller will invoke this function
199956** again, this time with pbRetry set to NULL.
199957**
199958** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
199959** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
199960** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
199961** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
199962** before retrying. In this case the caller attempts to remove the conflicting
199963** row before invoking this function again, this time with pbReplace set
199964** to NULL.
199965**
199966** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
199967** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
199968** returned.
199969*/
199970static int sessionApplyOneOp(
199971 sqlite3_changeset_iter *pIter, /* Changeset iterator */
199972 SessionApplyCtx *p, /* changeset_apply() context */
199973 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
199974 void *pCtx, /* First argument for the conflict handler */
199975 int *pbReplace, /* OUT: True to remove PK row and retry */
199976 int *pbRetry /* OUT: True to retry. */
199977){
199978 const char *zDummy;
199979 int op;
199980 int nCol;
199981 int rc = SQLITE_OK0;
199982
199983 assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect )((void) (0));
199984 assert( p->azCol && p->abPK )((void) (0));
199985 assert( !pbReplace || *pbReplace==0 )((void) (0));
199986
199987 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
199988
199989 if( op==SQLITE_DELETE9 ){
199990
199991 /* Bind values to the DELETE statement. If conflict handling is required,
199992 ** bind values for all columns and set bound variable (nCol+1) to true.
199993 ** Or, if conflict handling is not required, bind just the PK column
199994 ** values and, if it exists, set (nCol+1) to false. Conflict handling
199995 ** is not required if:
199996 **
199997 ** * this is a patchset, or
199998 ** * (pbRetry==0), or
199999 ** * all columns of the table are PK columns (in this case there is
200000 ** no (nCol+1) variable to bind to).
200001 */
200002 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
200003 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
200004 if( rc==SQLITE_OK0 && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
200005 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
200006 }
200007 if( rc!=SQLITE_OK0 ) return rc;
200008
200009 sqlite3_step(p->pDelete);
200010 rc = sqlite3_reset(p->pDelete);
200011 if( rc==SQLITE_OK0 && sqlite3_changes(p->db)==0 ){
200012 rc = sessionConflictHandler(
200013 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
200014 );
200015 }else if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
200016 rc = sessionConflictHandler(
200017 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
200018 );
200019 }
200020
200021 }else if( op==SQLITE_UPDATE23 ){
200022 int i;
200023
200024 /* Bind values to the UPDATE statement. */
200025 for(i=0; rc==SQLITE_OK0 && i<nCol; i++){
200026 sqlite3_value *pOld = sessionChangesetOld(pIter, i);
200027 sqlite3_value *pNew = sessionChangesetNew(pIter, i);
200028
200029 sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
200030 if( pOld ){
200031 rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
200032 }
200033 if( rc==SQLITE_OK0 && pNew ){
200034 rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
200035 }
200036 }
200037 if( rc==SQLITE_OK0 ){
200038 sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
200039 }
200040 if( rc!=SQLITE_OK0 ) return rc;
200041
200042 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
200043 ** the result will be SQLITE_OK with 0 rows modified. */
200044 sqlite3_step(p->pUpdate);
200045 rc = sqlite3_reset(p->pUpdate);
200046
200047 if( rc==SQLITE_OK0 && sqlite3_changes(p->db)==0 ){
200048 /* A NOTFOUND or DATA error. Search the table to see if it contains
200049 ** a row with a matching primary key. If so, this is a DATA conflict.
200050 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
200051
200052 rc = sessionConflictHandler(
200053 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
200054 );
200055
200056 }else if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
200057 /* This is always a CONSTRAINT conflict. */
200058 rc = sessionConflictHandler(
200059 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
200060 );
200061 }
200062
200063 }else{
200064 assert( op==SQLITE_INSERT )((void) (0));
200065 if( p->bStat1 ){
200066 /* Check if there is a conflicting row. For sqlite_stat1, this needs
200067 ** to be done using a SELECT, as there is no PRIMARY KEY in the
200068 ** database schema to throw an exception if a duplicate is inserted. */
200069 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
200070 if( rc==SQLITE_ROW100 ){
200071 rc = SQLITE_CONSTRAINT19;
200072 sqlite3_reset(p->pSelect);
200073 }
200074 }
200075
200076 if( rc==SQLITE_OK0 ){
200077 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
200078 if( rc!=SQLITE_OK0 ) return rc;
200079
200080 sqlite3_step(p->pInsert);
200081 rc = sqlite3_reset(p->pInsert);
200082 }
200083
200084 if( (rc&0xff)==SQLITE_CONSTRAINT19 ){
200085 rc = sessionConflictHandler(
200086 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
200087 );
200088 }
200089 }
200090
200091 return rc;
200092}
200093
200094/*
200095** Attempt to apply the change that the iterator passed as the first argument
200096** currently points to to the database. If a conflict is encountered, invoke
200097** the conflict handler callback.
200098**
200099** The difference between this function and sessionApplyOne() is that this
200100** function handles the case where the conflict-handler is invoked and
200101** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
200102** retried in some manner.
200103*/
200104static int sessionApplyOneWithRetry(
200105 sqlite3 *db, /* Apply change to "main" db of this handle */
200106 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
200107 SessionApplyCtx *pApply, /* Apply context */
200108 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
200109 void *pCtx /* First argument passed to xConflict */
200110){
200111 int bReplace = 0;
200112 int bRetry = 0;
200113 int rc;
200114
200115 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
200116 if( rc==SQLITE_OK0 ){
200117 /* If the bRetry flag is set, the change has not been applied due to an
200118 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
200119 ** a row with the correct PK is present in the db, but one or more other
200120 ** fields do not contain the expected values) and the conflict handler
200121 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
200122 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
200123 ** the SQLITE_CHANGESET_DATA problem. */
200124 if( bRetry ){
200125 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE )((void) (0));
200126 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
200127 }
200128
200129 /* If the bReplace flag is set, the change is an INSERT that has not
200130 ** been performed because the database already contains a row with the
200131 ** specified primary key and the conflict handler returned
200132 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
200133 ** before reattempting the INSERT. */
200134 else if( bReplace ){
200135 assert( pIter->op==SQLITE_INSERT )((void) (0));
200136 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
200137 if( rc==SQLITE_OK0 ){
200138 rc = sessionBindRow(pIter,
200139 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
200140 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
200141 }
200142 if( rc==SQLITE_OK0 ){
200143 sqlite3_step(pApply->pDelete);
200144 rc = sqlite3_reset(pApply->pDelete);
200145 }
200146 if( rc==SQLITE_OK0 ){
200147 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
200148 }
200149 if( rc==SQLITE_OK0 ){
200150 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
200151 }
200152 }
200153 }
200154
200155 return rc;
200156}
200157
200158/*
200159** Retry the changes accumulated in the pApply->constraints buffer.
200160*/
200161static int sessionRetryConstraints(
200162 sqlite3 *db,
200163 int bPatchset,
200164 const char *zTab,
200165 SessionApplyCtx *pApply,
200166 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
200167 void *pCtx /* First argument passed to xConflict */
200168){
200169 int rc = SQLITE_OK0;
200170
200171 while( pApply->constraints.nBuf ){
200172 sqlite3_changeset_iter *pIter2 = 0;
200173 SessionBuffer cons = pApply->constraints;
200174 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
200175
200176 rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf, 0);
200177 if( rc==SQLITE_OK0 ){
200178 size_t nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
200179 int rc2;
200180 pIter2->bPatchset = bPatchset;
200181 pIter2->zTab = (char*)zTab;
200182 pIter2->nCol = pApply->nCol;
200183 pIter2->abPK = pApply->abPK;
200184 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
200185 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
200186 if( rc==SQLITE_OK0 ) memset(pIter2->apValue, 0, nByte);
200187
200188 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3changeset_next(pIter2) ){
200189 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
200190 }
200191
200192 rc2 = sqlite3changeset_finalize(pIter2);
200193 if( rc==SQLITE_OK0 ) rc = rc2;
200194 }
200195 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 )((void) (0));
200196
200197 sqlite3_free(cons.aBuf);
200198 if( rc!=SQLITE_OK0 ) break;
200199 if( pApply->constraints.nBuf>=cons.nBuf ){
200200 /* No progress was made on the last round. */
200201 pApply->bDeferConstraints = 0;
200202 }
200203 }
200204
200205 return rc;
200206}
200207
200208/*
200209** Argument pIter is a changeset iterator that has been initialized, but
200210** not yet passed to sqlite3changeset_next(). This function applies the
200211** changeset to the main database attached to handle "db". The supplied
200212** conflict handler callback is invoked to resolve any conflicts encountered
200213** while applying the change.
200214*/
200215static int sessionChangesetApply(
200216 sqlite3 *db, /* Apply change to "main" db of this handle */
200217 sqlite3_changeset_iter *pIter, /* Changeset to apply */
200218 int(*xFilter)(
200219 void *pCtx, /* Copy of sixth arg to _apply() */
200220 const char *zTab /* Table name */
200221 ),
200222 int(*xConflict)(
200223 void *pCtx, /* Copy of fifth arg to _apply() */
200224 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
200225 sqlite3_changeset_iter *p /* Handle describing change and conflict */
200226 ),
200227 void *pCtx, /* First argument passed to xConflict */
200228 void **ppRebase, int *pnRebase, /* OUT: Rebase information */
200229 int flags /* SESSION_APPLY_XXX flags */
200230){
200231 int schemaMismatch = 0;
200232 int rc = SQLITE_OK0; /* Return code */
200233 const char *zTab = 0; /* Name of current table */
200234 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
200235 SessionApplyCtx sApply; /* changeset_apply() context object */
200236 int bPatchset;
200237
200238 assert( xConflict!=0 )((void) (0));
200239
200240 pIter->in.bNoDiscard = 1;
200241 memset(&sApply, 0, sizeof(sApply));
200242 sApply.bRebase = (ppRebase && pnRebase);
200243 sqlite3_mutex_enter(sqlite3_db_mutex(db));
200244 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
200245 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
200246 }
200247 if( rc==SQLITE_OK0 ){
200248 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
200249 }
200250 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3changeset_next(pIter) ){
200251 int nCol;
200252 int op;
200253 const char *zNew;
200254
200255 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
200256
200257 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
200258 u8 *abPK;
200259
200260 rc = sessionRetryConstraints(
200261 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
200262 );
200263 if( rc!=SQLITE_OK0 ) break;
200264
200265 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
200266 sqlite3_finalize(sApply.pDelete);
200267 sqlite3_finalize(sApply.pUpdate);
200268 sqlite3_finalize(sApply.pInsert);
200269 sqlite3_finalize(sApply.pSelect);
200270 sApply.db = db;
200271 sApply.pDelete = 0;
200272 sApply.pUpdate = 0;
200273 sApply.pInsert = 0;
200274 sApply.pSelect = 0;
200275 sApply.nCol = 0;
200276 sApply.azCol = 0;
200277 sApply.abPK = 0;
200278 sApply.bStat1 = 0;
200279 sApply.bDeferConstraints = 1;
200280 sApply.bRebaseStarted = 0;
200281 memset(&sApply.constraints, 0, sizeof(SessionBuffer));
200282
200283 /* If an xFilter() callback was specified, invoke it now. If the
200284 ** xFilter callback returns zero, skip this table. If it returns
200285 ** non-zero, proceed. */
200286 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
200287 if( schemaMismatch ){
200288 zTab = sqlite3_mprintf("%s", zNew);
200289 if( zTab==0 ){
200290 rc = SQLITE_NOMEM7;
200291 break;
200292 }
200293 nTab = (int)strlen(zTab);
200294 sApply.azCol = (const char **)zTab;
200295 }else{
200296 int nMinCol = 0;
200297 int i;
200298
200299 sqlite3changeset_pk(pIter, &abPK, 0);
200300 rc = sessionTableInfo(
200301 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
200302 );
200303 if( rc!=SQLITE_OK0 ) break;
200304 for(i=0; i<sApply.nCol; i++){
200305 if( sApply.abPK[i] ) nMinCol = i+1;
200306 }
200307
200308 if( sApply.nCol==0 ){
200309 schemaMismatch = 1;
200310 sqlite3_log(SQLITE_SCHEMA17,
200311 "sqlite3changeset_apply(): no such table: %s", zTab
200312 );
200313 }
200314 else if( sApply.nCol<nCol ){
200315 schemaMismatch = 1;
200316 sqlite3_log(SQLITE_SCHEMA17,
200317 "sqlite3changeset_apply(): table %s has %d columns, "
200318 "expected %d or more",
200319 zTab, sApply.nCol, nCol
200320 );
200321 }
200322 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
200323 schemaMismatch = 1;
200324 sqlite3_log(SQLITE_SCHEMA17, "sqlite3changeset_apply(): "
200325 "primary key mismatch for table %s", zTab
200326 );
200327 }
200328 else{
200329 sApply.nCol = nCol;
200330 if( 0==sqlite3_stricmp(zTab, "sqlite_stat1") ){
200331 if( (rc = sessionStat1Sql(db, &sApply) ) ){
200332 break;
200333 }
200334 sApply.bStat1 = 1;
200335 }else{
200336 if((rc = sessionSelectRow(db, zTab, &sApply))
200337 || (rc = sessionUpdateRow(db, zTab, &sApply))
200338 || (rc = sessionDeleteRow(db, zTab, &sApply))
200339 || (rc = sessionInsertRow(db, zTab, &sApply))
200340 ){
200341 break;
200342 }
200343 sApply.bStat1 = 0;
200344 }
200345 }
200346 nTab = sqlite3Strlen30(zTab);
200347 }
200348 }
200349
200350 /* If there is a schema mismatch on the current table, proceed to the
200351 ** next change. A log message has already been issued. */
200352 if( schemaMismatch ) continue;
200353
200354 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
200355 }
200356
200357 bPatchset = pIter->bPatchset;
200358 if( rc==SQLITE_OK0 ){
200359 rc = sqlite3changeset_finalize(pIter);
200360 }else{
200361 sqlite3changeset_finalize(pIter);
200362 }
200363
200364 if( rc==SQLITE_OK0 ){
200365 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
200366 }
200367
200368 if( rc==SQLITE_OK0 ){
200369 int nFk, notUsed;
200370 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS10, &nFk, &notUsed, 0);
200371 if( nFk!=0 ){
200372 int res = SQLITE_CHANGESET_ABORT;
200373 sqlite3_changeset_iter sIter;
200374 memset(&sIter, 0, sizeof(sIter));
200375 sIter.nCol = nFk;
200376 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
200377 if( res!=SQLITE_CHANGESET_OMIT ){
200378 rc = SQLITE_CONSTRAINT19;
200379 }
200380 }
200381 }
200382 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
200383
200384 if( (flags & SQLITE_CHANGESETAPPLY_NOSAVEPOINT)==0 ){
200385 if( rc==SQLITE_OK0 ){
200386 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
200387 }else{
200388 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
200389 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
200390 }
200391 }
200392
200393 assert( sApply.bRebase || sApply.rebase.nBuf==0 )((void) (0));
200394 if( rc==SQLITE_OK0 && bPatchset==0 && sApply.bRebase ){
200395 *ppRebase = (void*)sApply.rebase.aBuf;
200396 *pnRebase = sApply.rebase.nBuf;
200397 sApply.rebase.aBuf = 0;
200398 }
200399 sqlite3_finalize(sApply.pInsert);
200400 sqlite3_finalize(sApply.pDelete);
200401 sqlite3_finalize(sApply.pUpdate);
200402 sqlite3_finalize(sApply.pSelect);
200403 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
200404 sqlite3_free((char*)sApply.constraints.aBuf);
200405 sqlite3_free((char*)sApply.rebase.aBuf);
200406 sqlite3_mutex_leave(sqlite3_db_mutex(db));
200407 return rc;
200408}
200409
200410/*
200411** Apply the changeset passed via pChangeset/nChangeset to the main
200412** database attached to handle "db".
200413*/
200414SQLITE_API int sqlite3changeset_apply_v2(
200415 sqlite3 *db, /* Apply change to "main" db of this handle */
200416 int nChangeset, /* Size of changeset in bytes */
200417 void *pChangeset, /* Changeset blob */
200418 int(*xFilter)(
200419 void *pCtx, /* Copy of sixth arg to _apply() */
200420 const char *zTab /* Table name */
200421 ),
200422 int(*xConflict)(
200423 void *pCtx, /* Copy of sixth arg to _apply() */
200424 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
200425 sqlite3_changeset_iter *p /* Handle describing change and conflict */
200426 ),
200427 void *pCtx, /* First argument passed to xConflict */
200428 void **ppRebase, int *pnRebase,
200429 int flags
200430){
200431 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
200432 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
200433 int rc = sessionChangesetStart(&pIter, 0, 0, nChangeset, pChangeset,bInverse);
200434 if( rc==SQLITE_OK0 ){
200435 rc = sessionChangesetApply(
200436 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
200437 );
200438 }
200439 return rc;
200440}
200441
200442/*
200443** Apply the changeset passed via pChangeset/nChangeset to the main database
200444** attached to handle "db". Invoke the supplied conflict handler callback
200445** to resolve any conflicts encountered while applying the change.
200446*/
200447SQLITE_API int sqlite3changeset_apply(
200448 sqlite3 *db, /* Apply change to "main" db of this handle */
200449 int nChangeset, /* Size of changeset in bytes */
200450 void *pChangeset, /* Changeset blob */
200451 int(*xFilter)(
200452 void *pCtx, /* Copy of sixth arg to _apply() */
200453 const char *zTab /* Table name */
200454 ),
200455 int(*xConflict)(
200456 void *pCtx, /* Copy of fifth arg to _apply() */
200457 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
200458 sqlite3_changeset_iter *p /* Handle describing change and conflict */
200459 ),
200460 void *pCtx /* First argument passed to xConflict */
200461){
200462 return sqlite3changeset_apply_v2(
200463 db, nChangeset, pChangeset, xFilter, xConflict, pCtx, 0, 0, 0
200464 );
200465}
200466
200467/*
200468** Apply the changeset passed via xInput/pIn to the main database
200469** attached to handle "db". Invoke the supplied conflict handler callback
200470** to resolve any conflicts encountered while applying the change.
200471*/
200472SQLITE_API int sqlite3changeset_apply_v2_strm(
200473 sqlite3 *db, /* Apply change to "main" db of this handle */
200474 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
200475 void *pIn, /* First arg for xInput */
200476 int(*xFilter)(
200477 void *pCtx, /* Copy of sixth arg to _apply() */
200478 const char *zTab /* Table name */
200479 ),
200480 int(*xConflict)(
200481 void *pCtx, /* Copy of sixth arg to _apply() */
200482 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
200483 sqlite3_changeset_iter *p /* Handle describing change and conflict */
200484 ),
200485 void *pCtx, /* First argument passed to xConflict */
200486 void **ppRebase, int *pnRebase,
200487 int flags
200488){
200489 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
200490 int bInverse = !!(flags & SQLITE_CHANGESETAPPLY_INVERT);
200491 int rc = sessionChangesetStart(&pIter, xInput, pIn, 0, 0, bInverse);
200492 if( rc==SQLITE_OK0 ){
200493 rc = sessionChangesetApply(
200494 db, pIter, xFilter, xConflict, pCtx, ppRebase, pnRebase, flags
200495 );
200496 }
200497 return rc;
200498}
200499SQLITE_API int sqlite3changeset_apply_strm(
200500 sqlite3 *db, /* Apply change to "main" db of this handle */
200501 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
200502 void *pIn, /* First arg for xInput */
200503 int(*xFilter)(
200504 void *pCtx, /* Copy of sixth arg to _apply() */
200505 const char *zTab /* Table name */
200506 ),
200507 int(*xConflict)(
200508 void *pCtx, /* Copy of sixth arg to _apply() */
200509 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
200510 sqlite3_changeset_iter *p /* Handle describing change and conflict */
200511 ),
200512 void *pCtx /* First argument passed to xConflict */
200513){
200514 return sqlite3changeset_apply_v2_strm(
200515 db, xInput, pIn, xFilter, xConflict, pCtx, 0, 0, 0
200516 );
200517}
200518
200519/*
200520** sqlite3_changegroup handle.
200521*/
200522struct sqlite3_changegroup {
200523 int rc; /* Error code */
200524 int bPatch; /* True to accumulate patchsets */
200525 SessionTable *pList; /* List of tables in current patch */
200526};
200527
200528/*
200529** This function is called to merge two changes to the same row together as
200530** part of an sqlite3changeset_concat() operation. A new change object is
200531** allocated and a pointer to it stored in *ppNew.
200532*/
200533static int sessionChangeMerge(
200534 SessionTable *pTab, /* Table structure */
200535 int bRebase, /* True for a rebase hash-table */
200536 int bPatchset, /* True for patchsets */
200537 SessionChange *pExist, /* Existing change */
200538 int op2, /* Second change operation */
200539 int bIndirect, /* True if second change is indirect */
200540 u8 *aRec, /* Second change record */
200541 int nRec, /* Number of bytes in aRec */
200542 SessionChange **ppNew /* OUT: Merged change */
200543){
200544 SessionChange *pNew = 0;
200545 int rc = SQLITE_OK0;
200546
200547 if( !pExist ){
200548 pNew = (SessionChange *)sqlite3_malloc64(sizeof(SessionChange) + nRec);
200549 if( !pNew ){
200550 return SQLITE_NOMEM7;
200551 }
200552 memset(pNew, 0, sizeof(SessionChange));
200553 pNew->op = op2;
200554 pNew->bIndirect = bIndirect;
200555 pNew->aRecord = (u8*)&pNew[1];
200556 if( bIndirect==0 || bRebase==0 ){
200557 pNew->nRecord = nRec;
200558 memcpy(pNew->aRecord, aRec, nRec);
200559 }else{
200560 int i;
200561 u8 *pIn = aRec;
200562 u8 *pOut = pNew->aRecord;
200563 for(i=0; i<pTab->nCol; i++){
200564 int nIn = sessionSerialLen(pIn);
200565 if( *pIn==0 ){
200566 *pOut++ = 0;
200567 }else if( pTab->abPK[i]==0 ){
200568 *pOut++ = 0xFF;
200569 }else{
200570 memcpy(pOut, pIn, nIn);
200571 pOut += nIn;
200572 }
200573 pIn += nIn;
200574 }
200575 pNew->nRecord = pOut - pNew->aRecord;
200576 }
200577 }else if( bRebase ){
200578 if( pExist->op==SQLITE_DELETE9 && pExist->bIndirect ){
200579 *ppNew = pExist;
200580 }else{
200581 sqlite3_int64 nByte = nRec + pExist->nRecord + sizeof(SessionChange);
200582 pNew = (SessionChange*)sqlite3_malloc64(nByte);
200583 if( pNew==0 ){
200584 rc = SQLITE_NOMEM7;
200585 }else{
200586 int i;
200587 u8 *a1 = pExist->aRecord;
200588 u8 *a2 = aRec;
200589 u8 *pOut;
200590
200591 memset(pNew, 0, nByte);
200592 pNew->bIndirect = bIndirect || pExist->bIndirect;
200593 pNew->op = op2;
200594 pOut = pNew->aRecord = (u8*)&pNew[1];
200595
200596 for(i=0; i<pTab->nCol; i++){
200597 int n1 = sessionSerialLen(a1);
200598 int n2 = sessionSerialLen(a2);
200599 if( *a1==0xFF || (pTab->abPK[i]==0 && bIndirect) ){
200600 *pOut++ = 0xFF;
200601 }else if( *a2==0 ){
200602 memcpy(pOut, a1, n1);
200603 pOut += n1;
200604 }else{
200605 memcpy(pOut, a2, n2);
200606 pOut += n2;
200607 }
200608 a1 += n1;
200609 a2 += n2;
200610 }
200611 pNew->nRecord = pOut - pNew->aRecord;
200612 }
200613 sqlite3_free(pExist);
200614 }
200615 }else{
200616 int op1 = pExist->op;
200617
200618 /*
200619 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
200620 ** op1=INSERT, op2=UPDATE -> INSERT.
200621 ** op1=INSERT, op2=DELETE -> (none)
200622 **
200623 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
200624 ** op1=UPDATE, op2=UPDATE -> UPDATE.
200625 ** op1=UPDATE, op2=DELETE -> DELETE.
200626 **
200627 ** op1=DELETE, op2=INSERT -> UPDATE.
200628 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
200629 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
200630 */
200631 if( (op1==SQLITE_INSERT18 && op2==SQLITE_INSERT18)
200632 || (op1==SQLITE_UPDATE23 && op2==SQLITE_INSERT18)
200633 || (op1==SQLITE_DELETE9 && op2==SQLITE_UPDATE23)
200634 || (op1==SQLITE_DELETE9 && op2==SQLITE_DELETE9)
200635 ){
200636 pNew = pExist;
200637 }else if( op1==SQLITE_INSERT18 && op2==SQLITE_DELETE9 ){
200638 sqlite3_free(pExist);
200639 assert( pNew==0 )((void) (0));
200640 }else{
200641 u8 *aExist = pExist->aRecord;
200642 sqlite3_int64 nByte;
200643 u8 *aCsr;
200644
200645 /* Allocate a new SessionChange object. Ensure that the aRecord[]
200646 ** buffer of the new object is large enough to hold any record that
200647 ** may be generated by combining the input records. */
200648 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
200649 pNew = (SessionChange *)sqlite3_malloc64(nByte);
200650 if( !pNew ){
200651 sqlite3_free(pExist);
200652 return SQLITE_NOMEM7;
200653 }
200654 memset(pNew, 0, sizeof(SessionChange));
200655 pNew->bIndirect = (bIndirect && pExist->bIndirect);
200656 aCsr = pNew->aRecord = (u8 *)&pNew[1];
200657
200658 if( op1==SQLITE_INSERT18 ){ /* INSERT + UPDATE */
200659 u8 *a1 = aRec;
200660 assert( op2==SQLITE_UPDATE )((void) (0));
200661 pNew->op = SQLITE_INSERT18;
200662 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
200663 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
200664 }else if( op1==SQLITE_DELETE9 ){ /* DELETE + INSERT */
200665 assert( op2==SQLITE_INSERT )((void) (0));
200666 pNew->op = SQLITE_UPDATE23;
200667 if( bPatchset ){
200668 memcpy(aCsr, aRec, nRec);
200669 aCsr += nRec;
200670 }else{
200671 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
200672 sqlite3_free(pNew);
200673 pNew = 0;
200674 }
200675 }
200676 }else if( op2==SQLITE_UPDATE23 ){ /* UPDATE + UPDATE */
200677 u8 *a1 = aExist;
200678 u8 *a2 = aRec;
200679 assert( op1==SQLITE_UPDATE )((void) (0));
200680 if( bPatchset==0 ){
200681 sessionSkipRecord(&a1, pTab->nCol);
200682 sessionSkipRecord(&a2, pTab->nCol);
200683 }
200684 pNew->op = SQLITE_UPDATE23;
200685 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
200686 sqlite3_free(pNew);
200687 pNew = 0;
200688 }
200689 }else{ /* UPDATE + DELETE */
200690 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE )((void) (0));
200691 pNew->op = SQLITE_DELETE9;
200692 if( bPatchset ){
200693 memcpy(aCsr, aRec, nRec);
200694 aCsr += nRec;
200695 }else{
200696 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
200697 }
200698 }
200699
200700 if( pNew ){
200701 pNew->nRecord = (int)(aCsr - pNew->aRecord);
200702 }
200703 sqlite3_free(pExist);
200704 }
200705 }
200706
200707 *ppNew = pNew;
200708 return rc;
200709}
200710
200711/*
200712** Add all changes in the changeset traversed by the iterator passed as
200713** the first argument to the changegroup hash tables.
200714*/
200715static int sessionChangesetToHash(
200716 sqlite3_changeset_iter *pIter, /* Iterator to read from */
200717 sqlite3_changegroup *pGrp, /* Changegroup object to add changeset to */
200718 int bRebase /* True if hash table is for rebasing */
200719){
200720 u8 *aRec;
200721 int nRec;
200722 int rc = SQLITE_OK0;
200723 SessionTable *pTab = 0;
200724
200725 while( SQLITE_ROW100==sessionChangesetNext(pIter, &aRec, &nRec, 0) ){
200726 const char *zNew;
200727 int nCol;
200728 int op;
200729 int iHash;
200730 int bIndirect;
200731 SessionChange *pChange;
200732 SessionChange *pExist = 0;
200733 SessionChange **pp;
200734
200735 if( pGrp->pList==0 ){
200736 pGrp->bPatch = pIter->bPatchset;
200737 }else if( pIter->bPatchset!=pGrp->bPatch ){
200738 rc = SQLITE_ERROR1;
200739 break;
200740 }
200741
200742 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
200743 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
200744 /* Search the list for a matching table */
200745 int nNew = (int)strlen(zNew);
200746 u8 *abPK;
200747
200748 sqlite3changeset_pk(pIter, &abPK, 0);
200749 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
200750 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
200751 }
200752 if( !pTab ){
200753 SessionTable **ppTab;
200754
200755 pTab = sqlite3_malloc64(sizeof(SessionTable) + nCol + nNew+1);
200756 if( !pTab ){
200757 rc = SQLITE_NOMEM7;
200758 break;
200759 }
200760 memset(pTab, 0, sizeof(SessionTable));
200761 pTab->nCol = nCol;
200762 pTab->abPK = (u8*)&pTab[1];
200763 memcpy(pTab->abPK, abPK, nCol);
200764 pTab->zName = (char*)&pTab->abPK[nCol];
200765 memcpy(pTab->zName, zNew, nNew+1);
200766
200767 /* The new object must be linked on to the end of the list, not
200768 ** simply added to the start of it. This is to ensure that the
200769 ** tables within the output of sqlite3changegroup_output() are in
200770 ** the right order. */
200771 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
200772 *ppTab = pTab;
200773 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
200774 rc = SQLITE_SCHEMA17;
200775 break;
200776 }
200777 }
200778
200779 if( sessionGrowHash(pIter->bPatchset, pTab) ){
200780 rc = SQLITE_NOMEM7;
200781 break;
200782 }
200783 iHash = sessionChangeHash(
200784 pTab, (pIter->bPatchset && op==SQLITE_DELETE9), aRec, pTab->nChange
200785 );
200786
200787 /* Search for existing entry. If found, remove it from the hash table.
200788 ** Code below may link it back in.
200789 */
200790 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
200791 int bPkOnly1 = 0;
200792 int bPkOnly2 = 0;
200793 if( pIter->bPatchset ){
200794 bPkOnly1 = (*pp)->op==SQLITE_DELETE9;
200795 bPkOnly2 = op==SQLITE_DELETE9;
200796 }
200797 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
200798 pExist = *pp;
200799 *pp = (*pp)->pNext;
200800 pTab->nEntry--;
200801 break;
200802 }
200803 }
200804
200805 rc = sessionChangeMerge(pTab, bRebase,
200806 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
200807 );
200808 if( rc ) break;
200809 if( pChange ){
200810 pChange->pNext = pTab->apChange[iHash];
200811 pTab->apChange[iHash] = pChange;
200812 pTab->nEntry++;
200813 }
200814 }
200815
200816 if( rc==SQLITE_OK0 ) rc = pIter->rc;
200817 return rc;
200818}
200819
200820/*
200821** Serialize a changeset (or patchset) based on all changesets (or patchsets)
200822** added to the changegroup object passed as the first argument.
200823**
200824** If xOutput is not NULL, then the changeset/patchset is returned to the
200825** user via one or more calls to xOutput, as with the other streaming
200826** interfaces.
200827**
200828** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
200829** buffer containing the output changeset before this function returns. In
200830** this case (*pnOut) is set to the size of the output buffer in bytes. It
200831** is the responsibility of the caller to free the output buffer using
200832** sqlite3_free() when it is no longer required.
200833**
200834** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
200835** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
200836** are both set to 0 before returning.
200837*/
200838static int sessionChangegroupOutput(
200839 sqlite3_changegroup *pGrp,
200840 int (*xOutput)(void *pOut, const void *pData, int nData),
200841 void *pOut,
200842 int *pnOut,
200843 void **ppOut
200844){
200845 int rc = SQLITE_OK0;
200846 SessionBuffer buf = {0, 0, 0};
200847 SessionTable *pTab;
200848 assert( xOutput==0 || (ppOut==0 && pnOut==0) )((void) (0));
200849
200850 /* Create the serialized output changeset based on the contents of the
200851 ** hash tables attached to the SessionTable objects in list p->pList.
200852 */
200853 for(pTab=pGrp->pList; rc==SQLITE_OK0 && pTab; pTab=pTab->pNext){
200854 int i;
200855 if( pTab->nEntry==0 ) continue;
200856
200857 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
200858 for(i=0; i<pTab->nChange; i++){
200859 SessionChange *p;
200860 for(p=pTab->apChange[i]; p; p=p->pNext){
200861 sessionAppendByte(&buf, p->op, &rc);
200862 sessionAppendByte(&buf, p->bIndirect, &rc);
200863 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
200864 if( rc==SQLITE_OK0 && xOutput && buf.nBuf>=sessions_strm_chunk_size ){
200865 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
200866 buf.nBuf = 0;
200867 }
200868 }
200869 }
200870 }
200871
200872 if( rc==SQLITE_OK0 ){
200873 if( xOutput ){
200874 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
200875 }else{
200876 *ppOut = buf.aBuf;
200877 *pnOut = buf.nBuf;
200878 buf.aBuf = 0;
200879 }
200880 }
200881 sqlite3_free(buf.aBuf);
200882
200883 return rc;
200884}
200885
200886/*
200887** Allocate a new, empty, sqlite3_changegroup.
200888*/
200889SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
200890 int rc = SQLITE_OK0; /* Return code */
200891 sqlite3_changegroup *p; /* New object */
200892 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
200893 if( p==0 ){
200894 rc = SQLITE_NOMEM7;
200895 }else{
200896 memset(p, 0, sizeof(sqlite3_changegroup));
200897 }
200898 *pp = p;
200899 return rc;
200900}
200901
200902/*
200903** Add the changeset currently stored in buffer pData, size nData bytes,
200904** to changeset-group p.
200905*/
200906SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
200907 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
200908 int rc; /* Return code */
200909
200910 rc = sqlite3changeset_start(&pIter, nData, pData);
200911 if( rc==SQLITE_OK0 ){
200912 rc = sessionChangesetToHash(pIter, pGrp, 0);
200913 }
200914 sqlite3changeset_finalize(pIter);
200915 return rc;
200916}
200917
200918/*
200919** Obtain a buffer containing a changeset representing the concatenation
200920** of all changesets added to the group so far.
200921*/
200922SQLITE_API int sqlite3changegroup_output(
200923 sqlite3_changegroup *pGrp,
200924 int *pnData,
200925 void **ppData
200926){
200927 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
200928}
200929
200930/*
200931** Streaming versions of changegroup_add().
200932*/
200933SQLITE_API int sqlite3changegroup_add_strm(
200934 sqlite3_changegroup *pGrp,
200935 int (*xInput)(void *pIn, void *pData, int *pnData),
200936 void *pIn
200937){
200938 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
200939 int rc; /* Return code */
200940
200941 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
200942 if( rc==SQLITE_OK0 ){
200943 rc = sessionChangesetToHash(pIter, pGrp, 0);
200944 }
200945 sqlite3changeset_finalize(pIter);
200946 return rc;
200947}
200948
200949/*
200950** Streaming versions of changegroup_output().
200951*/
200952SQLITE_API int sqlite3changegroup_output_strm(
200953 sqlite3_changegroup *pGrp,
200954 int (*xOutput)(void *pOut, const void *pData, int nData),
200955 void *pOut
200956){
200957 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
200958}
200959
200960/*
200961** Delete a changegroup object.
200962*/
200963SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
200964 if( pGrp ){
200965 sessionDeleteTable(pGrp->pList);
200966 sqlite3_free(pGrp);
200967 }
200968}
200969
200970/*
200971** Combine two changesets together.
200972*/
200973SQLITE_API int sqlite3changeset_concat(
200974 int nLeft, /* Number of bytes in lhs input */
200975 void *pLeft, /* Lhs input changeset */
200976 int nRight /* Number of bytes in rhs input */,
200977 void *pRight, /* Rhs input changeset */
200978 int *pnOut, /* OUT: Number of bytes in output changeset */
200979 void **ppOut /* OUT: changeset (left <concat> right) */
200980){
200981 sqlite3_changegroup *pGrp;
200982 int rc;
200983
200984 rc = sqlite3changegroup_new(&pGrp);
200985 if( rc==SQLITE_OK0 ){
200986 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
200987 }
200988 if( rc==SQLITE_OK0 ){
200989 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
200990 }
200991 if( rc==SQLITE_OK0 ){
200992 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
200993 }
200994 sqlite3changegroup_delete(pGrp);
200995
200996 return rc;
200997}
200998
200999/*
201000** Streaming version of sqlite3changeset_concat().
201001*/
201002SQLITE_API int sqlite3changeset_concat_strm(
201003 int (*xInputA)(void *pIn, void *pData, int *pnData),
201004 void *pInA,
201005 int (*xInputB)(void *pIn, void *pData, int *pnData),
201006 void *pInB,
201007 int (*xOutput)(void *pOut, const void *pData, int nData),
201008 void *pOut
201009){
201010 sqlite3_changegroup *pGrp;
201011 int rc;
201012
201013 rc = sqlite3changegroup_new(&pGrp);
201014 if( rc==SQLITE_OK0 ){
201015 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
201016 }
201017 if( rc==SQLITE_OK0 ){
201018 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
201019 }
201020 if( rc==SQLITE_OK0 ){
201021 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
201022 }
201023 sqlite3changegroup_delete(pGrp);
201024
201025 return rc;
201026}
201027
201028/*
201029** Changeset rebaser handle.
201030*/
201031struct sqlite3_rebaser {
201032 sqlite3_changegroup grp; /* Hash table */
201033};
201034
201035/*
201036** Buffers a1 and a2 must both contain a sessions module record nCol
201037** fields in size. This function appends an nCol sessions module
201038** record to buffer pBuf that is a copy of a1, except that for
201039** each field that is undefined in a1[], swap in the field from a2[].
201040*/
201041static void sessionAppendRecordMerge(
201042 SessionBuffer *pBuf, /* Buffer to append to */
201043 int nCol, /* Number of columns in each record */
201044 u8 *a1, int n1, /* Record 1 */
201045 u8 *a2, int n2, /* Record 2 */
201046 int *pRc /* IN/OUT: error code */
201047){
201048 sessionBufferGrow(pBuf, n1+n2, pRc);
201049 if( *pRc==SQLITE_OK0 ){
201050 int i;
201051 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
201052 for(i=0; i<nCol; i++){
201053 int nn1 = sessionSerialLen(a1);
201054 int nn2 = sessionSerialLen(a2);
201055 if( *a1==0 || *a1==0xFF ){
201056 memcpy(pOut, a2, nn2);
201057 pOut += nn2;
201058 }else{
201059 memcpy(pOut, a1, nn1);
201060 pOut += nn1;
201061 }
201062 a1 += nn1;
201063 a2 += nn2;
201064 }
201065
201066 pBuf->nBuf = pOut-pBuf->aBuf;
201067 assert( pBuf->nBuf<=pBuf->nAlloc )((void) (0));
201068 }
201069}
201070
201071/*
201072** This function is called when rebasing a local UPDATE change against one
201073** or more remote UPDATE changes. The aRec/nRec buffer contains the current
201074** old.* and new.* records for the change. The rebase buffer (a single
201075** record) is in aChange/nChange. The rebased change is appended to buffer
201076** pBuf.
201077**
201078** Rebasing the UPDATE involves:
201079**
201080** * Removing any changes to fields for which the corresponding field
201081** in the rebase buffer is set to "replaced" (type 0xFF). If this
201082** means the UPDATE change updates no fields, nothing is appended
201083** to the output buffer.
201084**
201085** * For each field modified by the local change for which the
201086** corresponding field in the rebase buffer is not "undefined" (0x00)
201087** or "replaced" (0xFF), the old.* value is replaced by the value
201088** in the rebase buffer.
201089*/
201090static void sessionAppendPartialUpdate(
201091 SessionBuffer *pBuf, /* Append record here */
201092 sqlite3_changeset_iter *pIter, /* Iterator pointed at local change */
201093 u8 *aRec, int nRec, /* Local change */
201094 u8 *aChange, int nChange, /* Record to rebase against */
201095 int *pRc /* IN/OUT: Return Code */
201096){
201097 sessionBufferGrow(pBuf, 2+nRec+nChange, pRc);
201098 if( *pRc==SQLITE_OK0 ){
201099 int bData = 0;
201100 u8 *pOut = &pBuf->aBuf[pBuf->nBuf];
201101 int i;
201102 u8 *a1 = aRec;
201103 u8 *a2 = aChange;
201104
201105 *pOut++ = SQLITE_UPDATE23;
201106 *pOut++ = pIter->bIndirect;
201107 for(i=0; i<pIter->nCol; i++){
201108 int n1 = sessionSerialLen(a1);
201109 int n2 = sessionSerialLen(a2);
201110 if( pIter->abPK[i] || a2[0]==0 ){
201111 if( !pIter->abPK[i] ) bData = 1;
201112 memcpy(pOut, a1, n1);
201113 pOut += n1;
201114 }else if( a2[0]!=0xFF ){
201115 bData = 1;
201116 memcpy(pOut, a2, n2);
201117 pOut += n2;
201118 }else{
201119 *pOut++ = '\0';
201120 }
201121 a1 += n1;
201122 a2 += n2;
201123 }
201124 if( bData ){
201125 a2 = aChange;
201126 for(i=0; i<pIter->nCol; i++){
201127 int n1 = sessionSerialLen(a1);
201128 int n2 = sessionSerialLen(a2);
201129 if( pIter->abPK[i] || a2[0]!=0xFF ){
201130 memcpy(pOut, a1, n1);
201131 pOut += n1;
201132 }else{
201133 *pOut++ = '\0';
201134 }
201135 a1 += n1;
201136 a2 += n2;
201137 }
201138 pBuf->nBuf = (pOut - pBuf->aBuf);
201139 }
201140 }
201141}
201142
201143/*
201144** pIter is configured to iterate through a changeset. This function rebases
201145** that changeset according to the current configuration of the rebaser
201146** object passed as the first argument. If no error occurs and argument xOutput
201147** is not NULL, then the changeset is returned to the caller by invoking
201148** xOutput zero or more times and SQLITE_OK returned. Or, if xOutput is NULL,
201149** then (*ppOut) is set to point to a buffer containing the rebased changeset
201150** before this function returns. In this case (*pnOut) is set to the size of
201151** the buffer in bytes. It is the responsibility of the caller to eventually
201152** free the (*ppOut) buffer using sqlite3_free().
201153**
201154** If an error occurs, an SQLite error code is returned. If ppOut and
201155** pnOut are not NULL, then the two output parameters are set to 0 before
201156** returning.
201157*/
201158static int sessionRebase(
201159 sqlite3_rebaser *p, /* Rebaser hash table */
201160 sqlite3_changeset_iter *pIter, /* Input data */
201161 int (*xOutput)(void *pOut, const void *pData, int nData),
201162 void *pOut, /* Context for xOutput callback */
201163 int *pnOut, /* OUT: Number of bytes in output changeset */
201164 void **ppOut /* OUT: Inverse of pChangeset */
201165){
201166 int rc = SQLITE_OK0;
201167 u8 *aRec = 0;
201168 int nRec = 0;
201169 int bNew = 0;
201170 SessionTable *pTab = 0;
201171 SessionBuffer sOut = {0,0,0};
201172
201173 while( SQLITE_ROW100==sessionChangesetNext(pIter, &aRec, &nRec, &bNew) ){
201174 SessionChange *pChange = 0;
201175 int bDone = 0;
201176
201177 if( bNew ){
201178 const char *zTab = pIter->zTab;
201179 for(pTab=p->grp.pList; pTab; pTab=pTab->pNext){
201180 if( 0==sqlite3_stricmp(pTab->zName, zTab) ) break;
201181 }
201182 bNew = 0;
201183
201184 /* A patchset may not be rebased */
201185 if( pIter->bPatchset ){
201186 rc = SQLITE_ERROR1;
201187 }
201188
201189 /* Append a table header to the output for this new table */
201190 sessionAppendByte(&sOut, pIter->bPatchset ? 'P' : 'T', &rc);
201191 sessionAppendVarint(&sOut, pIter->nCol, &rc);
201192 sessionAppendBlob(&sOut, pIter->abPK, pIter->nCol, &rc);
201193 sessionAppendBlob(&sOut,(u8*)pIter->zTab,(int)strlen(pIter->zTab)+1,&rc);
201194 }
201195
201196 if( pTab && rc==SQLITE_OK0 ){
201197 int iHash = sessionChangeHash(pTab, 0, aRec, pTab->nChange);
201198
201199 for(pChange=pTab->apChange[iHash]; pChange; pChange=pChange->pNext){
201200 if( sessionChangeEqual(pTab, 0, aRec, 0, pChange->aRecord) ){
201201 break;
201202 }
201203 }
201204 }
201205
201206 if( pChange ){
201207 assert( pChange->op==SQLITE_DELETE || pChange->op==SQLITE_INSERT )((void) (0));
201208 switch( pIter->op ){
201209 case SQLITE_INSERT18:
201210 if( pChange->op==SQLITE_INSERT18 ){
201211 bDone = 1;
201212 if( pChange->bIndirect==0 ){
201213 sessionAppendByte(&sOut, SQLITE_UPDATE23, &rc);
201214 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
201215 sessionAppendBlob(&sOut, pChange->aRecord, pChange->nRecord, &rc);
201216 sessionAppendBlob(&sOut, aRec, nRec, &rc);
201217 }
201218 }
201219 break;
201220
201221 case SQLITE_UPDATE23:
201222 bDone = 1;
201223 if( pChange->op==SQLITE_DELETE9 ){
201224 if( pChange->bIndirect==0 ){
201225 u8 *pCsr = aRec;
201226 sessionSkipRecord(&pCsr, pIter->nCol);
201227 sessionAppendByte(&sOut, SQLITE_INSERT18, &rc);
201228 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
201229 sessionAppendRecordMerge(&sOut, pIter->nCol,
201230 pCsr, nRec-(pCsr-aRec),
201231 pChange->aRecord, pChange->nRecord, &rc
201232 );
201233 }
201234 }else{
201235 sessionAppendPartialUpdate(&sOut, pIter,
201236 aRec, nRec, pChange->aRecord, pChange->nRecord, &rc
201237 );
201238 }
201239 break;
201240
201241 default:
201242 assert( pIter->op==SQLITE_DELETE )((void) (0));
201243 bDone = 1;
201244 if( pChange->op==SQLITE_INSERT18 ){
201245 sessionAppendByte(&sOut, SQLITE_DELETE9, &rc);
201246 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
201247 sessionAppendRecordMerge(&sOut, pIter->nCol,
201248 pChange->aRecord, pChange->nRecord, aRec, nRec, &rc
201249 );
201250 }
201251 break;
201252 }
201253 }
201254
201255 if( bDone==0 ){
201256 sessionAppendByte(&sOut, pIter->op, &rc);
201257 sessionAppendByte(&sOut, pIter->bIndirect, &rc);
201258 sessionAppendBlob(&sOut, aRec, nRec, &rc);
201259 }
201260 if( rc==SQLITE_OK0 && xOutput && sOut.nBuf>sessions_strm_chunk_size ){
201261 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
201262 sOut.nBuf = 0;
201263 }
201264 if( rc ) break;
201265 }
201266
201267 if( rc!=SQLITE_OK0 ){
201268 sqlite3_free(sOut.aBuf);
201269 memset(&sOut, 0, sizeof(sOut));
201270 }
201271
201272 if( rc==SQLITE_OK0 ){
201273 if( xOutput ){
201274 if( sOut.nBuf>0 ){
201275 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
201276 }
201277 }else{
201278 *ppOut = (void*)sOut.aBuf;
201279 *pnOut = sOut.nBuf;
201280 sOut.aBuf = 0;
201281 }
201282 }
201283 sqlite3_free(sOut.aBuf);
201284 return rc;
201285}
201286
201287/*
201288** Create a new rebaser object.
201289*/
201290SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew){
201291 int rc = SQLITE_OK0;
201292 sqlite3_rebaser *pNew;
201293
201294 pNew = sqlite3_malloc(sizeof(sqlite3_rebaser));
201295 if( pNew==0 ){
201296 rc = SQLITE_NOMEM7;
201297 }else{
201298 memset(pNew, 0, sizeof(sqlite3_rebaser));
201299 }
201300 *ppNew = pNew;
201301 return rc;
201302}
201303
201304/*
201305** Call this one or more times to configure a rebaser.
201306*/
201307SQLITE_API int sqlite3rebaser_configure(
201308 sqlite3_rebaser *p,
201309 int nRebase, const void *pRebase
201310){
201311 sqlite3_changeset_iter *pIter = 0; /* Iterator opened on pData/nData */
201312 int rc; /* Return code */
201313 rc = sqlite3changeset_start(&pIter, nRebase, (void*)pRebase);
201314 if( rc==SQLITE_OK0 ){
201315 rc = sessionChangesetToHash(pIter, &p->grp, 1);
201316 }
201317 sqlite3changeset_finalize(pIter);
201318 return rc;
201319}
201320
201321/*
201322** Rebase a changeset according to current rebaser configuration
201323*/
201324SQLITE_API int sqlite3rebaser_rebase(
201325 sqlite3_rebaser *p,
201326 int nIn, const void *pIn,
201327 int *pnOut, void **ppOut
201328){
201329 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
201330 int rc = sqlite3changeset_start(&pIter, nIn, (void*)pIn);
201331
201332 if( rc==SQLITE_OK0 ){
201333 rc = sessionRebase(p, pIter, 0, 0, pnOut, ppOut);
201334 sqlite3changeset_finalize(pIter);
201335 }
201336
201337 return rc;
201338}
201339
201340/*
201341** Rebase a changeset according to current rebaser configuration
201342*/
201343SQLITE_API int sqlite3rebaser_rebase_strm(
201344 sqlite3_rebaser *p,
201345 int (*xInput)(void *pIn, void *pData, int *pnData),
201346 void *pIn,
201347 int (*xOutput)(void *pOut, const void *pData, int nData),
201348 void *pOut
201349){
201350 sqlite3_changeset_iter *pIter = 0; /* Iterator to skip through input */
201351 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
201352
201353 if( rc==SQLITE_OK0 ){
201354 rc = sessionRebase(p, pIter, xOutput, pOut, 0, 0);
201355 sqlite3changeset_finalize(pIter);
201356 }
201357
201358 return rc;
201359}
201360
201361/*
201362** Destroy a rebaser object
201363*/
201364SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p){
201365 if( p ){
201366 sessionDeleteTable(p->grp.pList);
201367 sqlite3_free(p);
201368 }
201369}
201370
201371/*
201372** Global configuration
201373*/
201374SQLITE_API int sqlite3session_config(int op, void *pArg){
201375 int rc = SQLITE_OK0;
201376 switch( op ){
201377 case SQLITE_SESSION_CONFIG_STRMSIZE: {
201378 int *pInt = (int*)pArg;
201379 if( *pInt>0 ){
201380 sessions_strm_chunk_size = *pInt;
201381 }
201382 *pInt = sessions_strm_chunk_size;
201383 break;
201384 }
201385 default:
201386 rc = SQLITE_MISUSE21;
201387 break;
201388 }
201389 return rc;
201390}
201391
201392#endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
201393
201394/************** End of sqlite3session.c **************************************/
201395/************** Begin file fts5.c ********************************************/
201396
201397
201398#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_FTS5)
201399
201400#if !defined(NDEBUG1) && !defined(SQLITE_DEBUG)
201401# define NDEBUG1 1
201402#endif
201403#if defined(NDEBUG1) && defined(SQLITE_DEBUG)
201404# undef NDEBUG1
201405#endif
201406
201407/*
201408** 2014 May 31
201409**
201410** The author disclaims copyright to this source code. In place of
201411** a legal notice, here is a blessing:
201412**
201413** May you do good and not evil.
201414** May you find forgiveness for yourself and forgive others.
201415** May you share freely, never taking more than you give.
201416**
201417******************************************************************************
201418**
201419** Interfaces to extend FTS5. Using the interfaces defined in this file,
201420** FTS5 may be extended with:
201421**
201422** * custom tokenizers, and
201423** * custom auxiliary functions.
201424*/
201425
201426
201427#ifndef _FTS5_H
201428#define _FTS5_H
201429
201430/* #include "sqlite3.h" */
201431
201432#if 0
201433extern "C" {
201434#endif
201435
201436/*************************************************************************
201437** CUSTOM AUXILIARY FUNCTIONS
201438**
201439** Virtual table implementations may overload SQL functions by implementing
201440** the sqlite3_module.xFindFunction() method.
201441*/
201442
201443typedef struct Fts5ExtensionApi Fts5ExtensionApi;
201444typedef struct Fts5Context Fts5Context;
201445typedef struct Fts5PhraseIter Fts5PhraseIter;
201446
201447typedef void (*fts5_extension_function)(
201448 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
201449 Fts5Context *pFts, /* First arg to pass to pApi functions */
201450 sqlite3_context *pCtx, /* Context for returning result/error */
201451 int nVal, /* Number of values in apVal[] array */
201452 sqlite3_value **apVal /* Array of trailing arguments */
201453);
201454
201455struct Fts5PhraseIter {
201456 const unsigned char *a;
201457 const unsigned char *b;
201458};
201459
201460/*
201461** EXTENSION API FUNCTIONS
201462**
201463** xUserData(pFts):
201464** Return a copy of the context pointer the extension function was
201465** registered with.
201466**
201467** xColumnTotalSize(pFts, iCol, pnToken):
201468** If parameter iCol is less than zero, set output variable *pnToken
201469** to the total number of tokens in the FTS5 table. Or, if iCol is
201470** non-negative but less than the number of columns in the table, return
201471** the total number of tokens in column iCol, considering all rows in
201472** the FTS5 table.
201473**
201474** If parameter iCol is greater than or equal to the number of columns
201475** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
201476** an OOM condition or IO error), an appropriate SQLite error code is
201477** returned.
201478**
201479** xColumnCount(pFts):
201480** Return the number of columns in the table.
201481**
201482** xColumnSize(pFts, iCol, pnToken):
201483** If parameter iCol is less than zero, set output variable *pnToken
201484** to the total number of tokens in the current row. Or, if iCol is
201485** non-negative but less than the number of columns in the table, set
201486** *pnToken to the number of tokens in column iCol of the current row.
201487**
201488** If parameter iCol is greater than or equal to the number of columns
201489** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
201490** an OOM condition or IO error), an appropriate SQLite error code is
201491** returned.
201492**
201493** This function may be quite inefficient if used with an FTS5 table
201494** created with the "columnsize=0" option.
201495**
201496** xColumnText:
201497** This function attempts to retrieve the text of column iCol of the
201498** current document. If successful, (*pz) is set to point to a buffer
201499** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
201500** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
201501** if an error occurs, an SQLite error code is returned and the final values
201502** of (*pz) and (*pn) are undefined.
201503**
201504** xPhraseCount:
201505** Returns the number of phrases in the current query expression.
201506**
201507** xPhraseSize:
201508** Returns the number of tokens in phrase iPhrase of the query. Phrases
201509** are numbered starting from zero.
201510**
201511** xInstCount:
201512** Set *pnInst to the total number of occurrences of all phrases within
201513** the query within the current row. Return SQLITE_OK if successful, or
201514** an error code (i.e. SQLITE_NOMEM) if an error occurs.
201515**
201516** This API can be quite slow if used with an FTS5 table created with the
201517** "detail=none" or "detail=column" option. If the FTS5 table is created
201518** with either "detail=none" or "detail=column" and "content=" option
201519** (i.e. if it is a contentless table), then this API always returns 0.
201520**
201521** xInst:
201522** Query for the details of phrase match iIdx within the current row.
201523** Phrase matches are numbered starting from zero, so the iIdx argument
201524** should be greater than or equal to zero and smaller than the value
201525** output by xInstCount().
201526**
201527** Usually, output parameter *piPhrase is set to the phrase number, *piCol
201528** to the column in which it occurs and *piOff the token offset of the
201529** first token of the phrase. Returns SQLITE_OK if successful, or an error
201530** code (i.e. SQLITE_NOMEM) if an error occurs.
201531**
201532** This API can be quite slow if used with an FTS5 table created with the
201533** "detail=none" or "detail=column" option.
201534**
201535** xRowid:
201536** Returns the rowid of the current row.
201537**
201538** xTokenize:
201539** Tokenize text using the tokenizer belonging to the FTS5 table.
201540**
201541** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
201542** This API function is used to query the FTS table for phrase iPhrase
201543** of the current query. Specifically, a query equivalent to:
201544**
201545** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
201546**
201547** with $p set to a phrase equivalent to the phrase iPhrase of the
201548** current query is executed. Any column filter that applies to
201549** phrase iPhrase of the current query is included in $p. For each
201550** row visited, the callback function passed as the fourth argument
201551** is invoked. The context and API objects passed to the callback
201552** function may be used to access the properties of each matched row.
201553** Invoking Api.xUserData() returns a copy of the pointer passed as
201554** the third argument to pUserData.
201555**
201556** If the callback function returns any value other than SQLITE_OK, the
201557** query is abandoned and the xQueryPhrase function returns immediately.
201558** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
201559** Otherwise, the error code is propagated upwards.
201560**
201561** If the query runs to completion without incident, SQLITE_OK is returned.
201562** Or, if some error occurs before the query completes or is aborted by
201563** the callback, an SQLite error code is returned.
201564**
201565**
201566** xSetAuxdata(pFts5, pAux, xDelete)
201567**
201568** Save the pointer passed as the second argument as the extension functions
201569** "auxiliary data". The pointer may then be retrieved by the current or any
201570** future invocation of the same fts5 extension function made as part of
201571** the same MATCH query using the xGetAuxdata() API.
201572**
201573** Each extension function is allocated a single auxiliary data slot for
201574** each FTS query (MATCH expression). If the extension function is invoked
201575** more than once for a single FTS query, then all invocations share a
201576** single auxiliary data context.
201577**
201578** If there is already an auxiliary data pointer when this function is
201579** invoked, then it is replaced by the new pointer. If an xDelete callback
201580** was specified along with the original pointer, it is invoked at this
201581** point.
201582**
201583** The xDelete callback, if one is specified, is also invoked on the
201584** auxiliary data pointer after the FTS5 query has finished.
201585**
201586** If an error (e.g. an OOM condition) occurs within this function,
201587** the auxiliary data is set to NULL and an error code returned. If the
201588** xDelete parameter was not NULL, it is invoked on the auxiliary data
201589** pointer before returning.
201590**
201591**
201592** xGetAuxdata(pFts5, bClear)
201593**
201594** Returns the current auxiliary data pointer for the fts5 extension
201595** function. See the xSetAuxdata() method for details.
201596**
201597** If the bClear argument is non-zero, then the auxiliary data is cleared
201598** (set to NULL) before this function returns. In this case the xDelete,
201599** if any, is not invoked.
201600**
201601**
201602** xRowCount(pFts5, pnRow)
201603**
201604** This function is used to retrieve the total number of rows in the table.
201605** In other words, the same value that would be returned by:
201606**
201607** SELECT count(*) FROM ftstable;
201608**
201609** xPhraseFirst()
201610** This function is used, along with type Fts5PhraseIter and the xPhraseNext
201611** method, to iterate through all instances of a single query phrase within
201612** the current row. This is the same information as is accessible via the
201613** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
201614** to use, this API may be faster under some circumstances. To iterate
201615** through instances of phrase iPhrase, use the following code:
201616**
201617** Fts5PhraseIter iter;
201618** int iCol, iOff;
201619** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
201620** iCol>=0;
201621** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
201622** ){
201623** // An instance of phrase iPhrase at offset iOff of column iCol
201624** }
201625**
201626** The Fts5PhraseIter structure is defined above. Applications should not
201627** modify this structure directly - it should only be used as shown above
201628** with the xPhraseFirst() and xPhraseNext() API methods (and by
201629** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
201630**
201631** This API can be quite slow if used with an FTS5 table created with the
201632** "detail=none" or "detail=column" option. If the FTS5 table is created
201633** with either "detail=none" or "detail=column" and "content=" option
201634** (i.e. if it is a contentless table), then this API always iterates
201635** through an empty set (all calls to xPhraseFirst() set iCol to -1).
201636**
201637** xPhraseNext()
201638** See xPhraseFirst above.
201639**
201640** xPhraseFirstColumn()
201641** This function and xPhraseNextColumn() are similar to the xPhraseFirst()
201642** and xPhraseNext() APIs described above. The difference is that instead
201643** of iterating through all instances of a phrase in the current row, these
201644** APIs are used to iterate through the set of columns in the current row
201645** that contain one or more instances of a specified phrase. For example:
201646**
201647** Fts5PhraseIter iter;
201648** int iCol;
201649** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
201650** iCol>=0;
201651** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
201652** ){
201653** // Column iCol contains at least one instance of phrase iPhrase
201654** }
201655**
201656** This API can be quite slow if used with an FTS5 table created with the
201657** "detail=none" option. If the FTS5 table is created with either
201658** "detail=none" "content=" option (i.e. if it is a contentless table),
201659** then this API always iterates through an empty set (all calls to
201660** xPhraseFirstColumn() set iCol to -1).
201661**
201662** The information accessed using this API and its companion
201663** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
201664** (or xInst/xInstCount). The chief advantage of this API is that it is
201665** significantly more efficient than those alternatives when used with
201666** "detail=column" tables.
201667**
201668** xPhraseNextColumn()
201669** See xPhraseFirstColumn above.
201670*/
201671struct Fts5ExtensionApi {
201672 int iVersion; /* Currently always set to 3 */
201673
201674 void *(*xUserData)(Fts5Context*);
201675
201676 int (*xColumnCount)(Fts5Context*);
201677 int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
201678 int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
201679
201680 int (*xTokenize)(Fts5Context*,
201681 const char *pText, int nText, /* Text to tokenize */
201682 void *pCtx, /* Context passed to xToken() */
201683 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
201684 );
201685
201686 int (*xPhraseCount)(Fts5Context*);
201687 int (*xPhraseSize)(Fts5Context*, int iPhrase);
201688
201689 int (*xInstCount)(Fts5Context*, int *pnInst);
201690 int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
201691
201692 sqlite3_int64 (*xRowid)(Fts5Context*);
201693 int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
201694 int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
201695
201696 int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
201697 int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
201698 );
201699 int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
201700 void *(*xGetAuxdata)(Fts5Context*, int bClear);
201701
201702 int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
201703 void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
201704
201705 int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
201706 void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
201707};
201708
201709/*
201710** CUSTOM AUXILIARY FUNCTIONS
201711*************************************************************************/
201712
201713/*************************************************************************
201714** CUSTOM TOKENIZERS
201715**
201716** Applications may also register custom tokenizer types. A tokenizer
201717** is registered by providing fts5 with a populated instance of the
201718** following structure. All structure methods must be defined, setting
201719** any member of the fts5_tokenizer struct to NULL leads to undefined
201720** behaviour. The structure methods are expected to function as follows:
201721**
201722** xCreate:
201723** This function is used to allocate and initialize a tokenizer instance.
201724** A tokenizer instance is required to actually tokenize text.
201725**
201726** The first argument passed to this function is a copy of the (void*)
201727** pointer provided by the application when the fts5_tokenizer object
201728** was registered with FTS5 (the third argument to xCreateTokenizer()).
201729** The second and third arguments are an array of nul-terminated strings
201730** containing the tokenizer arguments, if any, specified following the
201731** tokenizer name as part of the CREATE VIRTUAL TABLE statement used
201732** to create the FTS5 table.
201733**
201734** The final argument is an output variable. If successful, (*ppOut)
201735** should be set to point to the new tokenizer handle and SQLITE_OK
201736** returned. If an error occurs, some value other than SQLITE_OK should
201737** be returned. In this case, fts5 assumes that the final value of *ppOut
201738** is undefined.
201739**
201740** xDelete:
201741** This function is invoked to delete a tokenizer handle previously
201742** allocated using xCreate(). Fts5 guarantees that this function will
201743** be invoked exactly once for each successful call to xCreate().
201744**
201745** xTokenize:
201746** This function is expected to tokenize the nText byte string indicated
201747** by argument pText. pText may or may not be nul-terminated. The first
201748** argument passed to this function is a pointer to an Fts5Tokenizer object
201749** returned by an earlier call to xCreate().
201750**
201751** The second argument indicates the reason that FTS5 is requesting
201752** tokenization of the supplied text. This is always one of the following
201753** four values:
201754**
201755** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
201756** or removed from the FTS table. The tokenizer is being invoked to
201757** determine the set of tokens to add to (or delete from) the
201758** FTS index.
201759**
201760** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
201761** against the FTS index. The tokenizer is being called to tokenize
201762** a bareword or quoted string specified as part of the query.
201763**
201764** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
201765** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
201766** followed by a "*" character, indicating that the last token
201767** returned by the tokenizer will be treated as a token prefix.
201768**
201769** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
201770** satisfy an fts5_api.xTokenize() request made by an auxiliary
201771** function. Or an fts5_api.xColumnSize() request made by the same
201772** on a columnsize=0 database.
201773** </ul>
201774**
201775** For each token in the input string, the supplied callback xToken() must
201776** be invoked. The first argument to it should be a copy of the pointer
201777** passed as the second argument to xTokenize(). The third and fourth
201778** arguments are a pointer to a buffer containing the token text, and the
201779** size of the token in bytes. The 4th and 5th arguments are the byte offsets
201780** of the first byte of and first byte immediately following the text from
201781** which the token is derived within the input.
201782**
201783** The second argument passed to the xToken() callback ("tflags") should
201784** normally be set to 0. The exception is if the tokenizer supports
201785** synonyms. In this case see the discussion below for details.
201786**
201787** FTS5 assumes the xToken() callback is invoked for each token in the
201788** order that they occur within the input text.
201789**
201790** If an xToken() callback returns any value other than SQLITE_OK, then
201791** the tokenization should be abandoned and the xTokenize() method should
201792** immediately return a copy of the xToken() return value. Or, if the
201793** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
201794** if an error occurs with the xTokenize() implementation itself, it
201795** may abandon the tokenization and return any error code other than
201796** SQLITE_OK or SQLITE_DONE.
201797**
201798** SYNONYM SUPPORT
201799**
201800** Custom tokenizers may also support synonyms. Consider a case in which a
201801** user wishes to query for a phrase such as "first place". Using the
201802** built-in tokenizers, the FTS5 query 'first + place' will match instances
201803** of "first place" within the document set, but not alternative forms
201804** such as "1st place". In some applications, it would be better to match
201805** all instances of "first place" or "1st place" regardless of which form
201806** the user specified in the MATCH query text.
201807**
201808** There are several ways to approach this in FTS5:
201809**
201810** <ol><li> By mapping all synonyms to a single token. In this case, the
201811** In the above example, this means that the tokenizer returns the
201812** same token for inputs "first" and "1st". Say that token is in
201813** fact "first", so that when the user inserts the document "I won
201814** 1st place" entries are added to the index for tokens "i", "won",
201815** "first" and "place". If the user then queries for '1st + place',
201816** the tokenizer substitutes "first" for "1st" and the query works
201817** as expected.
201818**
201819** <li> By querying the index for all synonyms of each query term
201820** separately. In this case, when tokenizing query text, the
201821** tokenizer may provide multiple synonyms for a single term
201822** within the document. FTS5 then queries the index for each
201823** synonym individually. For example, faced with the query:
201824**
201825** <codeblock>
201826** ... MATCH 'first place'</codeblock>
201827**
201828** the tokenizer offers both "1st" and "first" as synonyms for the
201829** first token in the MATCH query and FTS5 effectively runs a query
201830** similar to:
201831**
201832** <codeblock>
201833** ... MATCH '(first OR 1st) place'</codeblock>
201834**
201835** except that, for the purposes of auxiliary functions, the query
201836** still appears to contain just two phrases - "(first OR 1st)"
201837** being treated as a single phrase.
201838**
201839** <li> By adding multiple synonyms for a single term to the FTS index.
201840** Using this method, when tokenizing document text, the tokenizer
201841** provides multiple synonyms for each token. So that when a
201842** document such as "I won first place" is tokenized, entries are
201843** added to the FTS index for "i", "won", "first", "1st" and
201844** "place".
201845**
201846** This way, even if the tokenizer does not provide synonyms
201847** when tokenizing query text (it should not - to do so would be
201848** inefficient), it doesn't matter if the user queries for
201849** 'first + place' or '1st + place', as there are entries in the
201850** FTS index corresponding to both forms of the first token.
201851** </ol>
201852**
201853** Whether it is parsing document or query text, any call to xToken that
201854** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
201855** is considered to supply a synonym for the previous token. For example,
201856** when parsing the document "I won first place", a tokenizer that supports
201857** synonyms would call xToken() 5 times, as follows:
201858**
201859** <codeblock>
201860** xToken(pCtx, 0, "i", 1, 0, 1);
201861** xToken(pCtx, 0, "won", 3, 2, 5);
201862** xToken(pCtx, 0, "first", 5, 6, 11);
201863** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11);
201864** xToken(pCtx, 0, "place", 5, 12, 17);
201865**</codeblock>
201866**
201867** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
201868** xToken() is called. Multiple synonyms may be specified for a single token
201869** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
201870** There is no limit to the number of synonyms that may be provided for a
201871** single token.
201872**
201873** In many cases, method (1) above is the best approach. It does not add
201874** extra data to the FTS index or require FTS5 to query for multiple terms,
201875** so it is efficient in terms of disk space and query speed. However, it
201876** does not support prefix queries very well. If, as suggested above, the
201877** token "first" is substituted for "1st" by the tokenizer, then the query:
201878**
201879** <codeblock>
201880** ... MATCH '1s*'</codeblock>
201881**
201882** will not match documents that contain the token "1st" (as the tokenizer
201883** will probably not map "1s" to any prefix of "first").
201884**
201885** For full prefix support, method (3) may be preferred. In this case,
201886** because the index contains entries for both "first" and "1st", prefix
201887** queries such as 'fi*' or '1s*' will match correctly. However, because
201888** extra entries are added to the FTS index, this method uses more space
201889** within the database.
201890**
201891** Method (2) offers a midpoint between (1) and (3). Using this method,
201892** a query such as '1s*' will match documents that contain the literal
201893** token "1st", but not "first" (assuming the tokenizer is not able to
201894** provide synonyms for prefixes). However, a non-prefix query like '1st'
201895** will match against "1st" and "first". This method does not require
201896** extra disk space, as no extra entries are added to the FTS index.
201897** On the other hand, it may require more CPU cycles to run MATCH queries,
201898** as separate queries of the FTS index are required for each synonym.
201899**
201900** When using methods (2) or (3), it is important that the tokenizer only
201901** provide synonyms when tokenizing document text (method (2)) or query
201902** text (method (3)), not both. Doing so will not cause any errors, but is
201903** inefficient.
201904*/
201905typedef struct Fts5Tokenizer Fts5Tokenizer;
201906typedef struct fts5_tokenizer fts5_tokenizer;
201907struct fts5_tokenizer {
201908 int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
201909 void (*xDelete)(Fts5Tokenizer*);
201910 int (*xTokenize)(Fts5Tokenizer*,
201911 void *pCtx,
201912 int flags, /* Mask of FTS5_TOKENIZE_* flags */
201913 const char *pText, int nText,
201914 int (*xToken)(
201915 void *pCtx, /* Copy of 2nd argument to xTokenize() */
201916 int tflags, /* Mask of FTS5_TOKEN_* flags */
201917 const char *pToken, /* Pointer to buffer containing token */
201918 int nToken, /* Size of token in bytes */
201919 int iStart, /* Byte offset of token within input text */
201920 int iEnd /* Byte offset of end of token within input text */
201921 )
201922 );
201923};
201924
201925/* Flags that may be passed as the third argument to xTokenize() */
201926#define FTS5_TOKENIZE_QUERY0x0001 0x0001
201927#define FTS5_TOKENIZE_PREFIX0x0002 0x0002
201928#define FTS5_TOKENIZE_DOCUMENT0x0004 0x0004
201929#define FTS5_TOKENIZE_AUX0x0008 0x0008
201930
201931/* Flags that may be passed by the tokenizer implementation back to FTS5
201932** as the third argument to the supplied xToken callback. */
201933#define FTS5_TOKEN_COLOCATED0x0001 0x0001 /* Same position as prev. token */
201934
201935/*
201936** END OF CUSTOM TOKENIZERS
201937*************************************************************************/
201938
201939/*************************************************************************
201940** FTS5 EXTENSION REGISTRATION API
201941*/
201942typedef struct fts5_api fts5_api;
201943struct fts5_api {
201944 int iVersion; /* Currently always set to 2 */
201945
201946 /* Create a new tokenizer */
201947 int (*xCreateTokenizer)(
201948 fts5_api *pApi,
201949 const char *zName,
201950 void *pContext,
201951 fts5_tokenizer *pTokenizer,
201952 void (*xDestroy)(void*)
201953 );
201954
201955 /* Find an existing tokenizer */
201956 int (*xFindTokenizer)(
201957 fts5_api *pApi,
201958 const char *zName,
201959 void **ppContext,
201960 fts5_tokenizer *pTokenizer
201961 );
201962
201963 /* Create a new auxiliary function */
201964 int (*xCreateFunction)(
201965 fts5_api *pApi,
201966 const char *zName,
201967 void *pContext,
201968 fts5_extension_function xFunction,
201969 void (*xDestroy)(void*)
201970 );
201971};
201972
201973/*
201974** END OF REGISTRATION API
201975*************************************************************************/
201976
201977#if 0
201978} /* end of the 'extern "C"' block */
201979#endif
201980
201981#endif /* _FTS5_H */
201982
201983/*
201984** 2014 May 31
201985**
201986** The author disclaims copyright to this source code. In place of
201987** a legal notice, here is a blessing:
201988**
201989** May you do good and not evil.
201990** May you find forgiveness for yourself and forgive others.
201991** May you share freely, never taking more than you give.
201992**
201993******************************************************************************
201994**
201995*/
201996#ifndef _FTS5INT_H
201997#define _FTS5INT_H
201998
201999/* #include "fts5.h" */
202000/* #include "sqlite3ext.h" */
202001SQLITE_EXTENSION_INIT1
202002
202003/* #include <string.h> */
202004/* #include <assert.h> */
202005
202006#ifndef SQLITE_AMALGAMATION1
202007
202008typedef unsigned char u8;
202009typedef unsigned int u32;
202010typedef unsigned short u16;
202011typedef short i16;
202012typedef sqlite3_int64 i64;
202013typedef sqlite3_uint64 u64;
202014
202015#ifndef ArraySize
202016# define ArraySize(x)((int)(sizeof(x)/sizeof(x[0]))) ((int)(sizeof(x) / sizeof(x[0])))
202017#endif
202018
202019#define testcase(x)
202020#define ALWAYS(x)(x) 1
202021#define NEVER(x)(x) 0
202022
202023#define MIN(x,y)((x)<(y)?(x):(y)) (((x) < (y)) ? (x) : (y))
202024#define MAX(x,y)((x)>(y)?(x):(y)) (((x) > (y)) ? (x) : (y))
202025
202026/*
202027** Constants for the largest and smallest possible 64-bit signed integers.
202028*/
202029# define LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)) (0xffffffff|(((i64)0x7fffffff)<<32))
202030# define SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) (((i64)-1) - LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)))
202031
202032#endif
202033
202034/* Truncate very long tokens to this many bytes. Hard limit is
202035** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
202036** field that occurs at the start of each leaf page (see fts5_index.c). */
202037#define FTS5_MAX_TOKEN_SIZE 32768
202038
202039/*
202040** Maximum number of prefix indexes on single FTS5 table. This must be
202041** less than 32. If it is set to anything large than that, an #error
202042** directive in fts5_index.c will cause the build to fail.
202043*/
202044#define FTS5_MAX_PREFIX_INDEXES 31
202045
202046#define FTS5_DEFAULT_NEARDIST 10
202047#define FTS5_DEFAULT_RANK "bm25"
202048
202049/* Name of rank and rowid columns */
202050#define FTS5_RANK_NAME "rank"
202051#define FTS5_ROWID_NAME "rowid"
202052
202053#ifdef SQLITE_DEBUG
202054# define FTS5_CORRUPT sqlite3Fts5Corrupt()
202055static int sqlite3Fts5Corrupt(void);
202056#else
202057# define FTS5_CORRUPT SQLITE_CORRUPT_VTAB(11 | (1<<8))
202058#endif
202059
202060/*
202061** The assert_nc() macro is similar to the assert() macro, except that it
202062** is used for assert() conditions that are true only if it can be
202063** guranteed that the database is not corrupt.
202064*/
202065#ifdef SQLITE_DEBUG
202066SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
202067# define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))((void) (0))
202068#else
202069# define assert_nc(x) assert(x)((void) (0))
202070#endif
202071
202072/*
202073** A version of memcmp() that does not cause asan errors if one of the pointer
202074** parameters is NULL and the number of bytes to compare is zero.
202075*/
202076#define fts5Memcmp(s1, s2, n) ((n)==0 ? 0 : memcmp((s1), (s2), (n)))
202077
202078/* Mark a function parameter as unused, to suppress nuisance compiler
202079** warnings. */
202080#ifndef UNUSED_PARAM
202081# define UNUSED_PARAM(X) (void)(X)
202082#endif
202083
202084#ifndef UNUSED_PARAM2
202085# define UNUSED_PARAM2(X, Y) (void)(X), (void)(Y)
202086#endif
202087
202088typedef struct Fts5Global Fts5Global;
202089typedef struct Fts5Colset Fts5Colset;
202090
202091/* If a NEAR() clump or phrase may only match a specific set of columns,
202092** then an object of the following type is used to record the set of columns.
202093** Each entry in the aiCol[] array is a column that may be matched.
202094**
202095** This object is used by fts5_expr.c and fts5_index.c.
202096*/
202097struct Fts5Colset {
202098 int nCol;
202099 int aiCol[1];
202100};
202101
202102
202103
202104/**************************************************************************
202105** Interface to code in fts5_config.c. fts5_config.c contains contains code
202106** to parse the arguments passed to the CREATE VIRTUAL TABLE statement.
202107*/
202108
202109typedef struct Fts5Config Fts5Config;
202110
202111/*
202112** An instance of the following structure encodes all information that can
202113** be gleaned from the CREATE VIRTUAL TABLE statement.
202114**
202115** And all information loaded from the %_config table.
202116**
202117** nAutomerge:
202118** The minimum number of segments that an auto-merge operation should
202119** attempt to merge together. A value of 1 sets the object to use the
202120** compile time default. Zero disables auto-merge altogether.
202121**
202122** zContent:
202123**
202124** zContentRowid:
202125** The value of the content_rowid= option, if one was specified. Or
202126** the string "rowid" otherwise. This text is not quoted - if it is
202127** used as part of an SQL statement it needs to be quoted appropriately.
202128**
202129** zContentExprlist:
202130**
202131** pzErrmsg:
202132** This exists in order to allow the fts5_index.c module to return a
202133** decent error message if it encounters a file-format version it does
202134** not understand.
202135**
202136** bColumnsize:
202137** True if the %_docsize table is created.
202138**
202139** bPrefixIndex:
202140** This is only used for debugging. If set to false, any prefix indexes
202141** are ignored. This value is configured using:
202142**
202143** INSERT INTO tbl(tbl, rank) VALUES('prefix-index', $bPrefixIndex);
202144**
202145*/
202146struct Fts5Config {
202147 sqlite3 *db; /* Database handle */
202148 char *zDb; /* Database holding FTS index (e.g. "main") */
202149 char *zName; /* Name of FTS index */
202150 int nCol; /* Number of columns */
202151 char **azCol; /* Column names */
202152 u8 *abUnindexed; /* True for unindexed columns */
202153 int nPrefix; /* Number of prefix indexes */
202154 int *aPrefix; /* Sizes in bytes of nPrefix prefix indexes */
202155 int eContent; /* An FTS5_CONTENT value */
202156 char *zContent; /* content table */
202157 char *zContentRowid; /* "content_rowid=" option value */
202158 int bColumnsize; /* "columnsize=" option value (dflt==1) */
202159 int eDetail; /* FTS5_DETAIL_XXX value */
202160 char *zContentExprlist;
202161 Fts5Tokenizer *pTok;
202162 fts5_tokenizer *pTokApi;
202163
202164 /* Values loaded from the %_config table */
202165 int iCookie; /* Incremented when %_config is modified */
202166 int pgsz; /* Approximate page size used in %_data */
202167 int nAutomerge; /* 'automerge' setting */
202168 int nCrisisMerge; /* Maximum allowed segments per level */
202169 int nUsermerge; /* 'usermerge' setting */
202170 int nHashSize; /* Bytes of memory for in-memory hash */
202171 char *zRank; /* Name of rank function */
202172 char *zRankArgs; /* Arguments to rank function */
202173
202174 /* If non-NULL, points to sqlite3_vtab.base.zErrmsg. Often NULL. */
202175 char **pzErrmsg;
202176
202177#ifdef SQLITE_DEBUG
202178 int bPrefixIndex; /* True to use prefix-indexes */
202179#endif
202180};
202181
202182/* Current expected value of %_config table 'version' field */
202183#define FTS5_CURRENT_VERSION 4
202184
202185#define FTS5_CONTENT_NORMAL 0
202186#define FTS5_CONTENT_NONE 1
202187#define FTS5_CONTENT_EXTERNAL 2
202188
202189#define FTS5_DETAIL_FULL 0
202190#define FTS5_DETAIL_NONE 1
202191#define FTS5_DETAIL_COLUMNS 2
202192
202193
202194
202195static int sqlite3Fts5ConfigParse(
202196 Fts5Global*, sqlite3*, int, const char **, Fts5Config**, char**
202197);
202198static void sqlite3Fts5ConfigFree(Fts5Config*);
202199
202200static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig);
202201
202202static int sqlite3Fts5Tokenize(
202203 Fts5Config *pConfig, /* FTS5 Configuration object */
202204 int flags, /* FTS5_TOKENIZE_* flags */
202205 const char *pText, int nText, /* Text to tokenize */
202206 void *pCtx, /* Context passed to xToken() */
202207 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
202208);
202209
202210static void sqlite3Fts5Dequote(char *z);
202211
202212/* Load the contents of the %_config table */
202213static int sqlite3Fts5ConfigLoad(Fts5Config*, int);
202214
202215/* Set the value of a single config attribute */
202216static int sqlite3Fts5ConfigSetValue(Fts5Config*, const char*, sqlite3_value*, int*);
202217
202218static int sqlite3Fts5ConfigParseRank(const char*, char**, char**);
202219
202220/*
202221** End of interface to code in fts5_config.c.
202222**************************************************************************/
202223
202224/**************************************************************************
202225** Interface to code in fts5_buffer.c.
202226*/
202227
202228/*
202229** Buffer object for the incremental building of string data.
202230*/
202231typedef struct Fts5Buffer Fts5Buffer;
202232struct Fts5Buffer {
202233 u8 *p;
202234 int n;
202235 int nSpace;
202236};
202237
202238static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
202239static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
202240static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
202241static void sqlite3Fts5BufferAppendString(int *, Fts5Buffer*, const char*);
202242static void sqlite3Fts5BufferFree(Fts5Buffer*);
202243static void sqlite3Fts5BufferZero(Fts5Buffer*);
202244static void sqlite3Fts5BufferSet(int*, Fts5Buffer*, int, const u8*);
202245static void sqlite3Fts5BufferAppendPrintf(int *, Fts5Buffer*, char *zFmt, ...);
202246
202247static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...);
202248
202249#define fts5BufferZero(x) sqlite3Fts5BufferZero(x)
202250#define fts5BufferAppendVarint(a,b,c) sqlite3Fts5BufferAppendVarint(a,b,c)
202251#define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
202252#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
202253#define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
202254
202255#define fts5BufferGrow(pRc,pBuf,nn) ( \
202256 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
202257 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
202258)
202259
202260/* Write and decode big-endian 32-bit integer values */
202261static void sqlite3Fts5Put32(u8*, int);
202262static int sqlite3Fts5Get32(const u8*);
202263
202264#define FTS5_POS2COLUMN(iPos) (int)(iPos >> 32)
202265#define FTS5_POS2OFFSET(iPos) (int)(iPos & 0x7FFFFFFF)
202266
202267typedef struct Fts5PoslistReader Fts5PoslistReader;
202268struct Fts5PoslistReader {
202269 /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */
202270 const u8 *a; /* Position list to iterate through */
202271 int n; /* Size of buffer at a[] in bytes */
202272 int i; /* Current offset in a[] */
202273
202274 u8 bFlag; /* For client use (any custom purpose) */
202275
202276 /* Output variables */
202277 u8 bEof; /* Set to true at EOF */
202278 i64 iPos; /* (iCol<<32) + iPos */
202279};
202280static int sqlite3Fts5PoslistReaderInit(
202281 const u8 *a, int n, /* Poslist buffer to iterate through */
202282 Fts5PoslistReader *pIter /* Iterator object to initialize */
202283);
202284static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader*);
202285
202286typedef struct Fts5PoslistWriter Fts5PoslistWriter;
202287struct Fts5PoslistWriter {
202288 i64 iPrev;
202289};
202290static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
202291static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
202292
202293static int sqlite3Fts5PoslistNext64(
202294 const u8 *a, int n, /* Buffer containing poslist */
202295 int *pi, /* IN/OUT: Offset within a[] */
202296 i64 *piOff /* IN/OUT: Current offset */
202297);
202298
202299/* Malloc utility */
202300static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte);
202301static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn);
202302
202303/* Character set tests (like isspace(), isalpha() etc.) */
202304static int sqlite3Fts5IsBareword(char t);
202305
202306
202307/* Bucket of terms object used by the integrity-check in offsets=0 mode. */
202308typedef struct Fts5Termset Fts5Termset;
202309static int sqlite3Fts5TermsetNew(Fts5Termset**);
202310static int sqlite3Fts5TermsetAdd(Fts5Termset*, int, const char*, int, int *pbPresent);
202311static void sqlite3Fts5TermsetFree(Fts5Termset*);
202312
202313/*
202314** End of interface to code in fts5_buffer.c.
202315**************************************************************************/
202316
202317/**************************************************************************
202318** Interface to code in fts5_index.c. fts5_index.c contains contains code
202319** to access the data stored in the %_data table.
202320*/
202321
202322typedef struct Fts5Index Fts5Index;
202323typedef struct Fts5IndexIter Fts5IndexIter;
202324
202325struct Fts5IndexIter {
202326 i64 iRowid;
202327 const u8 *pData;
202328 int nData;
202329 u8 bEof;
202330};
202331
202332#define sqlite3Fts5IterEof(x) ((x)->bEof)
202333
202334/*
202335** Values used as part of the flags argument passed to IndexQuery().
202336*/
202337#define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */
202338#define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */
202339#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */
202340#define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */
202341
202342/* The following are used internally by the fts5_index.c module. They are
202343** defined here only to make it easier to avoid clashes with the flags
202344** above. */
202345#define FTS5INDEX_QUERY_SKIPEMPTY 0x0010
202346#define FTS5INDEX_QUERY_NOOUTPUT 0x0020
202347
202348/*
202349** Create/destroy an Fts5Index object.
202350*/
202351static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
202352static int sqlite3Fts5IndexClose(Fts5Index *p);
202353
202354/*
202355** Return a simple checksum value based on the arguments.
202356*/
202357static u64 sqlite3Fts5IndexEntryCksum(
202358 i64 iRowid,
202359 int iCol,
202360 int iPos,
202361 int iIdx,
202362 const char *pTerm,
202363 int nTerm
202364);
202365
202366/*
202367** Argument p points to a buffer containing utf-8 text that is n bytes in
202368** size. Return the number of bytes in the nChar character prefix of the
202369** buffer, or 0 if there are less than nChar characters in total.
202370*/
202371static int sqlite3Fts5IndexCharlenToBytelen(
202372 const char *p,
202373 int nByte,
202374 int nChar
202375);
202376
202377/*
202378** Open a new iterator to iterate though all rowids that match the
202379** specified token or token prefix.
202380*/
202381static int sqlite3Fts5IndexQuery(
202382 Fts5Index *p, /* FTS index to query */
202383 const char *pToken, int nToken, /* Token (or prefix) to query for */
202384 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
202385 Fts5Colset *pColset, /* Match these columns only */
202386 Fts5IndexIter **ppIter /* OUT: New iterator object */
202387);
202388
202389/*
202390** The various operations on open token or token prefix iterators opened
202391** using sqlite3Fts5IndexQuery().
202392*/
202393static int sqlite3Fts5IterNext(Fts5IndexIter*);
202394static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
202395
202396/*
202397** Close an iterator opened by sqlite3Fts5IndexQuery().
202398*/
202399static void sqlite3Fts5IterClose(Fts5IndexIter*);
202400
202401/*
202402** This interface is used by the fts5vocab module.
202403*/
202404static const char *sqlite3Fts5IterTerm(Fts5IndexIter*, int*);
202405static int sqlite3Fts5IterNextScan(Fts5IndexIter*);
202406
202407
202408/*
202409** Insert or remove data to or from the index. Each time a document is
202410** added to or removed from the index, this function is called one or more
202411** times.
202412**
202413** For an insert, it must be called once for each token in the new document.
202414** If the operation is a delete, it must be called (at least) once for each
202415** unique token in the document with an iCol value less than zero. The iPos
202416** argument is ignored for a delete.
202417*/
202418static int sqlite3Fts5IndexWrite(
202419 Fts5Index *p, /* Index to write to */
202420 int iCol, /* Column token appears in (-ve -> delete) */
202421 int iPos, /* Position of token within column */
202422 const char *pToken, int nToken /* Token to add or remove to or from index */
202423);
202424
202425/*
202426** Indicate that subsequent calls to sqlite3Fts5IndexWrite() pertain to
202427** document iDocid.
202428*/
202429static int sqlite3Fts5IndexBeginWrite(
202430 Fts5Index *p, /* Index to write to */
202431 int bDelete, /* True if current operation is a delete */
202432 i64 iDocid /* Docid to add or remove data from */
202433);
202434
202435/*
202436** Flush any data stored in the in-memory hash tables to the database.
202437** Also close any open blob handles.
202438*/
202439static int sqlite3Fts5IndexSync(Fts5Index *p);
202440
202441/*
202442** Discard any data stored in the in-memory hash tables. Do not write it
202443** to the database. Additionally, assume that the contents of the %_data
202444** table may have changed on disk. So any in-memory caches of %_data
202445** records must be invalidated.
202446*/
202447static int sqlite3Fts5IndexRollback(Fts5Index *p);
202448
202449/*
202450** Get or set the "averages" values.
202451*/
202452static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize);
202453static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8*, int);
202454
202455/*
202456** Functions called by the storage module as part of integrity-check.
202457*/
202458static int sqlite3Fts5IndexIntegrityCheck(Fts5Index*, u64 cksum);
202459
202460/*
202461** Called during virtual module initialization to register UDF
202462** fts5_decode() with SQLite
202463*/
202464static int sqlite3Fts5IndexInit(sqlite3*);
202465
202466static int sqlite3Fts5IndexSetCookie(Fts5Index*, int);
202467
202468/*
202469** Return the total number of entries read from the %_data table by
202470** this connection since it was created.
202471*/
202472static int sqlite3Fts5IndexReads(Fts5Index *p);
202473
202474static int sqlite3Fts5IndexReinit(Fts5Index *p);
202475static int sqlite3Fts5IndexOptimize(Fts5Index *p);
202476static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
202477static int sqlite3Fts5IndexReset(Fts5Index *p);
202478
202479static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
202480
202481/*
202482** End of interface to code in fts5_index.c.
202483**************************************************************************/
202484
202485/**************************************************************************
202486** Interface to code in fts5_varint.c.
202487*/
202488static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v);
202489static int sqlite3Fts5GetVarintLen(u32 iVal);
202490static u8 sqlite3Fts5GetVarint(const unsigned char*, u64*);
202491static int sqlite3Fts5PutVarint(unsigned char *p, u64 v);
202492
202493#define fts5GetVarint32(a,b) sqlite3Fts5GetVarint32(a,(u32*)&b)
202494#define fts5GetVarint sqlite3Fts5GetVarint
202495
202496#define fts5FastGetVarint32(a, iOff, nVal) { \
202497 nVal = (a)[iOff++]; \
202498 if( nVal & 0x80 ){ \
202499 iOff--; \
202500 iOff += fts5GetVarint32(&(a)[iOff], nVal); \
202501 } \
202502}
202503
202504
202505/*
202506** End of interface to code in fts5_varint.c.
202507**************************************************************************/
202508
202509
202510/**************************************************************************
202511** Interface to code in fts5_main.c.
202512*/
202513
202514/*
202515** Virtual-table object.
202516*/
202517typedef struct Fts5Table Fts5Table;
202518struct Fts5Table {
202519 sqlite3_vtab base; /* Base class used by SQLite core */
202520 Fts5Config *pConfig; /* Virtual table configuration */
202521 Fts5Index *pIndex; /* Full-text index */
202522};
202523
202524static int sqlite3Fts5GetTokenizer(
202525 Fts5Global*,
202526 const char **azArg,
202527 int nArg,
202528 Fts5Tokenizer**,
202529 fts5_tokenizer**,
202530 char **pzErr
202531);
202532
202533static Fts5Table *sqlite3Fts5TableFromCsrid(Fts5Global*, i64);
202534
202535static int sqlite3Fts5FlushToDisk(Fts5Table*);
202536
202537/*
202538** End of interface to code in fts5.c.
202539**************************************************************************/
202540
202541/**************************************************************************
202542** Interface to code in fts5_hash.c.
202543*/
202544typedef struct Fts5Hash Fts5Hash;
202545
202546/*
202547** Create a hash table, free a hash table.
202548*/
202549static int sqlite3Fts5HashNew(Fts5Config*, Fts5Hash**, int *pnSize);
202550static void sqlite3Fts5HashFree(Fts5Hash*);
202551
202552static int sqlite3Fts5HashWrite(
202553 Fts5Hash*,
202554 i64 iRowid, /* Rowid for this entry */
202555 int iCol, /* Column token appears in (-ve -> delete) */
202556 int iPos, /* Position of token within column */
202557 char bByte,
202558 const char *pToken, int nToken /* Token to add or remove to or from index */
202559);
202560
202561/*
202562** Empty (but do not delete) a hash table.
202563*/
202564static void sqlite3Fts5HashClear(Fts5Hash*);
202565
202566static int sqlite3Fts5HashQuery(
202567 Fts5Hash*, /* Hash table to query */
202568 int nPre,
202569 const char *pTerm, int nTerm, /* Query term */
202570 void **ppObj, /* OUT: Pointer to doclist for pTerm */
202571 int *pnDoclist /* OUT: Size of doclist in bytes */
202572);
202573
202574static int sqlite3Fts5HashScanInit(
202575 Fts5Hash*, /* Hash table to query */
202576 const char *pTerm, int nTerm /* Query prefix */
202577);
202578static void sqlite3Fts5HashScanNext(Fts5Hash*);
202579static int sqlite3Fts5HashScanEof(Fts5Hash*);
202580static void sqlite3Fts5HashScanEntry(Fts5Hash *,
202581 const char **pzTerm, /* OUT: term (nul-terminated) */
202582 const u8 **ppDoclist, /* OUT: pointer to doclist */
202583 int *pnDoclist /* OUT: size of doclist in bytes */
202584);
202585
202586
202587/*
202588** End of interface to code in fts5_hash.c.
202589**************************************************************************/
202590
202591/**************************************************************************
202592** Interface to code in fts5_storage.c. fts5_storage.c contains contains
202593** code to access the data stored in the %_content and %_docsize tables.
202594*/
202595
202596#define FTS5_STMT_SCAN_ASC 0 /* SELECT rowid, * FROM ... ORDER BY 1 ASC */
202597#define FTS5_STMT_SCAN_DESC 1 /* SELECT rowid, * FROM ... ORDER BY 1 DESC */
202598#define FTS5_STMT_LOOKUP 2 /* SELECT rowid, * FROM ... WHERE rowid=? */
202599
202600typedef struct Fts5Storage Fts5Storage;
202601
202602static int sqlite3Fts5StorageOpen(Fts5Config*, Fts5Index*, int, Fts5Storage**, char**);
202603static int sqlite3Fts5StorageClose(Fts5Storage *p);
202604static int sqlite3Fts5StorageRename(Fts5Storage*, const char *zName);
202605
202606static int sqlite3Fts5DropAll(Fts5Config*);
202607static int sqlite3Fts5CreateTable(Fts5Config*, const char*, const char*, int, char **);
202608
202609static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64, sqlite3_value**);
202610static int sqlite3Fts5StorageContentInsert(Fts5Storage *p, sqlite3_value**, i64*);
202611static int sqlite3Fts5StorageIndexInsert(Fts5Storage *p, sqlite3_value**, i64);
202612
202613static int sqlite3Fts5StorageIntegrity(Fts5Storage *p);
202614
202615static int sqlite3Fts5StorageStmt(Fts5Storage *p, int eStmt, sqlite3_stmt**, char**);
202616static void sqlite3Fts5StorageStmtRelease(Fts5Storage *p, int eStmt, sqlite3_stmt*);
202617
202618static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
202619static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
202620static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
202621
202622static int sqlite3Fts5StorageSync(Fts5Storage *p);
202623static int sqlite3Fts5StorageRollback(Fts5Storage *p);
202624
202625static int sqlite3Fts5StorageConfigValue(
202626 Fts5Storage *p, const char*, sqlite3_value*, int
202627);
202628
202629static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p);
202630static int sqlite3Fts5StorageRebuild(Fts5Storage *p);
202631static int sqlite3Fts5StorageOptimize(Fts5Storage *p);
202632static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge);
202633static int sqlite3Fts5StorageReset(Fts5Storage *p);
202634
202635/*
202636** End of interface to code in fts5_storage.c.
202637**************************************************************************/
202638
202639
202640/**************************************************************************
202641** Interface to code in fts5_expr.c.
202642*/
202643typedef struct Fts5Expr Fts5Expr;
202644typedef struct Fts5ExprNode Fts5ExprNode;
202645typedef struct Fts5Parse Fts5Parse;
202646typedef struct Fts5Token Fts5Token;
202647typedef struct Fts5ExprPhrase Fts5ExprPhrase;
202648typedef struct Fts5ExprNearset Fts5ExprNearset;
202649
202650struct Fts5Token {
202651 const char *p; /* Token text (not NULL terminated) */
202652 int n; /* Size of buffer p in bytes */
202653};
202654
202655/* Parse a MATCH expression. */
202656static int sqlite3Fts5ExprNew(
202657 Fts5Config *pConfig,
202658 int iCol, /* Column on LHS of MATCH operator */
202659 const char *zExpr,
202660 Fts5Expr **ppNew,
202661 char **pzErr
202662);
202663
202664/*
202665** for(rc = sqlite3Fts5ExprFirst(pExpr, pIdx, bDesc);
202666** rc==SQLITE_OK && 0==sqlite3Fts5ExprEof(pExpr);
202667** rc = sqlite3Fts5ExprNext(pExpr)
202668** ){
202669** // The document with rowid iRowid matches the expression!
202670** i64 iRowid = sqlite3Fts5ExprRowid(pExpr);
202671** }
202672*/
202673static int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
202674static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
202675static int sqlite3Fts5ExprEof(Fts5Expr*);
202676static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
202677
202678static void sqlite3Fts5ExprFree(Fts5Expr*);
202679
202680/* Called during startup to register a UDF with SQLite */
202681static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
202682
202683static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
202684static int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
202685static int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);
202686
202687typedef struct Fts5PoslistPopulator Fts5PoslistPopulator;
202688static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr*, int);
202689static int sqlite3Fts5ExprPopulatePoslists(
202690 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
202691);
202692static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
202693
202694static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
202695
202696static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
202697
202698/*******************************************
202699** The fts5_expr.c API above this point is used by the other hand-written
202700** C code in this module. The interfaces below this point are called by
202701** the parser code in fts5parse.y. */
202702
202703static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...);
202704
202705static Fts5ExprNode *sqlite3Fts5ParseNode(
202706 Fts5Parse *pParse,
202707 int eType,
202708 Fts5ExprNode *pLeft,
202709 Fts5ExprNode *pRight,
202710 Fts5ExprNearset *pNear
202711);
202712
202713static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
202714 Fts5Parse *pParse,
202715 Fts5ExprNode *pLeft,
202716 Fts5ExprNode *pRight
202717);
202718
202719static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
202720 Fts5Parse *pParse,
202721 Fts5ExprPhrase *pPhrase,
202722 Fts5Token *pToken,
202723 int bPrefix
202724);
202725
202726static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
202727
202728static Fts5ExprNearset *sqlite3Fts5ParseNearset(
202729 Fts5Parse*,
202730 Fts5ExprNearset*,
202731 Fts5ExprPhrase*
202732);
202733
202734static Fts5Colset *sqlite3Fts5ParseColset(
202735 Fts5Parse*,
202736 Fts5Colset*,
202737 Fts5Token *
202738);
202739
202740static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase*);
202741static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset*);
202742static void sqlite3Fts5ParseNodeFree(Fts5ExprNode*);
202743
202744static void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*);
202745static void sqlite3Fts5ParseSetColset(Fts5Parse*, Fts5ExprNode*, Fts5Colset*);
202746static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse*, Fts5Colset*);
202747static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p);
202748static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*);
202749
202750/*
202751** End of interface to code in fts5_expr.c.
202752**************************************************************************/
202753
202754
202755
202756/**************************************************************************
202757** Interface to code in fts5_aux.c.
202758*/
202759
202760static int sqlite3Fts5AuxInit(fts5_api*);
202761/*
202762** End of interface to code in fts5_aux.c.
202763**************************************************************************/
202764
202765/**************************************************************************
202766** Interface to code in fts5_tokenizer.c.
202767*/
202768
202769static int sqlite3Fts5TokenizerInit(fts5_api*);
202770/*
202771** End of interface to code in fts5_tokenizer.c.
202772**************************************************************************/
202773
202774/**************************************************************************
202775** Interface to code in fts5_vocab.c.
202776*/
202777
202778static int sqlite3Fts5VocabInit(Fts5Global*, sqlite3*);
202779
202780/*
202781** End of interface to code in fts5_vocab.c.
202782**************************************************************************/
202783
202784
202785/**************************************************************************
202786** Interface to automatically generated code in fts5_unicode2.c.
202787*/
202788static int sqlite3Fts5UnicodeIsdiacritic(int c);
202789static int sqlite3Fts5UnicodeFold(int c, int bRemoveDiacritic);
202790
202791static int sqlite3Fts5UnicodeCatParse(const char*, u8*);
202792static int sqlite3Fts5UnicodeCategory(u32 iCode);
202793static void sqlite3Fts5UnicodeAscii(u8*, u8*);
202794/*
202795** End of interface to code in fts5_unicode2.c.
202796**************************************************************************/
202797
202798#endif
202799
202800#define FTS5_OR 1
202801#define FTS5_AND 2
202802#define FTS5_NOT 3
202803#define FTS5_TERM 4
202804#define FTS5_COLON 5
202805#define FTS5_MINUS 6
202806#define FTS5_LCP 7
202807#define FTS5_RCP 8
202808#define FTS5_STRING 9
202809#define FTS5_LP 10
202810#define FTS5_RP 11
202811#define FTS5_CARET 12
202812#define FTS5_COMMA 13
202813#define FTS5_PLUS 14
202814#define FTS5_STAR 15
202815
202816/*
202817** 2000-05-29
202818**
202819** The author disclaims copyright to this source code. In place of
202820** a legal notice, here is a blessing:
202821**
202822** May you do good and not evil.
202823** May you find forgiveness for yourself and forgive others.
202824** May you share freely, never taking more than you give.
202825**
202826*************************************************************************
202827** Driver template for the LEMON parser generator.
202828**
202829** The "lemon" program processes an LALR(1) input grammar file, then uses
202830** this template to construct a parser. The "lemon" program inserts text
202831** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
202832** interstitial "-" characters) contained in this template is changed into
202833** the value of the %name directive from the grammar. Otherwise, the content
202834** of this template is copied straight through into the generate parser
202835** source file.
202836**
202837** The following is the concatenation of all %include directives from the
202838** input grammar file:
202839*/
202840/* #include <stdio.h> */
202841/* #include <assert.h> */
202842/************ Begin %include sections from the grammar ************************/
202843
202844/* #include "fts5Int.h" */
202845/* #include "fts5parse.h" */
202846
202847/*
202848** Disable all error recovery processing in the parser push-down
202849** automaton.
202850*/
202851#define fts5YYNOERRORRECOVERY 1
202852
202853/*
202854** Make fts5yytestcase() the same as testcase()
202855*/
202856#define fts5yytestcase(X) testcase(X)
202857
202858/*
202859** Indicate that sqlite3ParserFree() will never be called with a null
202860** pointer.
202861*/
202862#define fts5YYPARSEFREENOTNULL 1
202863
202864/*
202865** Alternative datatype for the argument to the malloc() routine passed
202866** into sqlite3ParserAlloc(). The default is size_t.
202867*/
202868#define fts5YYMALLOCARGTYPE u64
202869
202870/**************** End of %include directives **********************************/
202871/* These constants specify the various numeric values for terminal symbols
202872** in a format understandable to "makeheaders". This section is blank unless
202873** "lemon" is run with the "-m" command-line option.
202874***************** Begin makeheaders token definitions *************************/
202875/**************** End makeheaders token definitions ***************************/
202876
202877/* The next sections is a series of control #defines.
202878** various aspects of the generated parser.
202879** fts5YYCODETYPE is the data type used to store the integer codes
202880** that represent terminal and non-terminal symbols.
202881** "unsigned char" is used if there are fewer than
202882** 256 symbols. Larger types otherwise.
202883** fts5YYNOCODE is a number of type fts5YYCODETYPE that is not used for
202884** any terminal or nonterminal symbol.
202885** fts5YYFALLBACK If defined, this indicates that one or more tokens
202886** (also known as: "terminal symbols") have fall-back
202887** values which should be used if the original symbol
202888** would not parse. This permits keywords to sometimes
202889** be used as identifiers, for example.
202890** fts5YYACTIONTYPE is the data type used for "action codes" - numbers
202891** that indicate what to do in response to the next
202892** token.
202893** sqlite3Fts5ParserFTS5TOKENTYPE is the data type used for minor type for terminal
202894** symbols. Background: A "minor type" is a semantic
202895** value associated with a terminal or non-terminal
202896** symbols. For example, for an "ID" terminal symbol,
202897** the minor type might be the name of the identifier.
202898** Each non-terminal can have a different minor type.
202899** Terminal symbols all have the same minor type, though.
202900** This macros defines the minor type for terminal
202901** symbols.
202902** fts5YYMINORTYPE is the data type used for all minor types.
202903** This is typically a union of many types, one of
202904** which is sqlite3Fts5ParserFTS5TOKENTYPE. The entry in the union
202905** for terminal symbols is called "fts5yy0".
202906** fts5YYSTACKDEPTH is the maximum depth of the parser's stack. If
202907** zero the stack is dynamically sized using realloc()
202908** sqlite3Fts5ParserARG_SDECL A static variable declaration for the %extra_argument
202909** sqlite3Fts5ParserARG_PDECL A parameter declaration for the %extra_argument
202910** sqlite3Fts5ParserARG_PARAM Code to pass %extra_argument as a subroutine parameter
202911** sqlite3Fts5ParserARG_STORE Code to store %extra_argument into fts5yypParser
202912** sqlite3Fts5ParserARG_FETCH Code to extract %extra_argument from fts5yypParser
202913** sqlite3Fts5ParserCTX_* As sqlite3Fts5ParserARG_ except for %extra_context
202914** fts5YYERRORSYMBOL is the code number of the error symbol. If not
202915** defined, then do no error processing.
202916** fts5YYNSTATE the combined number of states.
202917** fts5YYNRULE the number of rules in the grammar
202918** fts5YYNFTS5TOKEN Number of terminal symbols
202919** fts5YY_MAX_SHIFT Maximum value for shift actions
202920** fts5YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
202921** fts5YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
202922** fts5YY_ERROR_ACTION The fts5yy_action[] code for syntax error
202923** fts5YY_ACCEPT_ACTION The fts5yy_action[] code for accept
202924** fts5YY_NO_ACTION The fts5yy_action[] code for no-op
202925** fts5YY_MIN_REDUCE Minimum value for reduce actions
202926** fts5YY_MAX_REDUCE Maximum value for reduce actions
202927*/
202928#ifndef INTERFACE1
202929# define INTERFACE1 1
202930#endif
202931/************* Begin control #defines *****************************************/
202932#define fts5YYCODETYPE unsigned char
202933#define fts5YYNOCODE 27
202934#define fts5YYACTIONTYPE unsigned char
202935#define sqlite3Fts5ParserFTS5TOKENTYPE Fts5Token
202936typedef union {
202937 int fts5yyinit;
202938 sqlite3Fts5ParserFTS5TOKENTYPE fts5yy0;
202939 int fts5yy4;
202940 Fts5Colset* fts5yy11;
202941 Fts5ExprNode* fts5yy24;
202942 Fts5ExprNearset* fts5yy46;
202943 Fts5ExprPhrase* fts5yy53;
202944} fts5YYMINORTYPE;
202945#ifndef fts5YYSTACKDEPTH
202946#define fts5YYSTACKDEPTH 100
202947#endif
202948#define sqlite3Fts5ParserARG_SDECL Fts5Parse *pParse;
202949#define sqlite3Fts5ParserARG_PDECL ,Fts5Parse *pParse
202950#define sqlite3Fts5ParserARG_PARAM ,pParse
202951#define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse=fts5yypParser->pParse;
202952#define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse=pParse;
202953#define sqlite3Fts5ParserCTX_SDECL
202954#define sqlite3Fts5ParserCTX_PDECL
202955#define sqlite3Fts5ParserCTX_PARAM
202956#define sqlite3Fts5ParserCTX_FETCH
202957#define sqlite3Fts5ParserCTX_STORE
202958#define fts5YYNSTATE 35
202959#define fts5YYNRULE 28
202960#define fts5YYNFTS5TOKEN 16
202961#define fts5YY_MAX_SHIFT 34
202962#define fts5YY_MIN_SHIFTREDUCE 52
202963#define fts5YY_MAX_SHIFTREDUCE 79
202964#define fts5YY_ERROR_ACTION 80
202965#define fts5YY_ACCEPT_ACTION 81
202966#define fts5YY_NO_ACTION 82
202967#define fts5YY_MIN_REDUCE 83
202968#define fts5YY_MAX_REDUCE 110
202969/************* End control #defines *******************************************/
202970#define fts5YY_NLOOKAHEAD ((int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])))
202971
202972/* Define the fts5yytestcase() macro to be a no-op if is not already defined
202973** otherwise.
202974**
202975** Applications can choose to define fts5yytestcase() in the %include section
202976** to a macro that can assist in verifying code coverage. For production
202977** code the fts5yytestcase() macro should be turned off. But it is useful
202978** for testing.
202979*/
202980#ifndef fts5yytestcase
202981# define fts5yytestcase(X)
202982#endif
202983
202984
202985/* Next are the tables used to determine what action to take based on the
202986** current state and lookahead token. These tables are used to implement
202987** functions that take a state number and lookahead value and return an
202988** action integer.
202989**
202990** Suppose the action integer is N. Then the action is determined as
202991** follows
202992**
202993** 0 <= N <= fts5YY_MAX_SHIFT Shift N. That is, push the lookahead
202994** token onto the stack and goto state N.
202995**
202996** N between fts5YY_MIN_SHIFTREDUCE Shift to an arbitrary state then
202997** and fts5YY_MAX_SHIFTREDUCE reduce by rule N-fts5YY_MIN_SHIFTREDUCE.
202998**
202999** N == fts5YY_ERROR_ACTION A syntax error has occurred.
203000**
203001** N == fts5YY_ACCEPT_ACTION The parser accepts its input.
203002**
203003** N == fts5YY_NO_ACTION No such action. Denotes unused
203004** slots in the fts5yy_action[] table.
203005**
203006** N between fts5YY_MIN_REDUCE Reduce by rule N-fts5YY_MIN_REDUCE
203007** and fts5YY_MAX_REDUCE
203008**
203009** The action table is constructed as a single large table named fts5yy_action[].
203010** Given state S and lookahead X, the action is computed as either:
203011**
203012** (A) N = fts5yy_action[ fts5yy_shift_ofst[S] + X ]
203013** (B) N = fts5yy_default[S]
203014**
203015** The (A) formula is preferred. The B formula is used instead if
203016** fts5yy_lookahead[fts5yy_shift_ofst[S]+X] is not equal to X.
203017**
203018** The formulas above are for computing the action when the lookahead is
203019** a terminal symbol. If the lookahead is a non-terminal (as occurs after
203020** a reduce action) then the fts5yy_reduce_ofst[] array is used in place of
203021** the fts5yy_shift_ofst[] array.
203022**
203023** The following are the tables generated in this section:
203024**
203025** fts5yy_action[] A single table containing all actions.
203026** fts5yy_lookahead[] A table containing the lookahead for each entry in
203027** fts5yy_action. Used to detect hash collisions.
203028** fts5yy_shift_ofst[] For each state, the offset into fts5yy_action for
203029** shifting terminals.
203030** fts5yy_reduce_ofst[] For each state, the offset into fts5yy_action for
203031** shifting non-terminals after a reduce.
203032** fts5yy_default[] Default action for each state.
203033**
203034*********** Begin parsing tables **********************************************/
203035#define fts5YY_ACTTAB_COUNT (105)
203036static const fts5YYACTIONTYPE fts5yy_action[] = {
203037 /* 0 */ 81, 20, 96, 6, 28, 99, 98, 26, 26, 18,
203038 /* 10 */ 96, 6, 28, 17, 98, 56, 26, 19, 96, 6,
203039 /* 20 */ 28, 14, 98, 14, 26, 31, 92, 96, 6, 28,
203040 /* 30 */ 108, 98, 25, 26, 21, 96, 6, 28, 78, 98,
203041 /* 40 */ 58, 26, 29, 96, 6, 28, 107, 98, 22, 26,
203042 /* 50 */ 24, 16, 12, 11, 1, 13, 13, 24, 16, 23,
203043 /* 60 */ 11, 33, 34, 13, 97, 8, 27, 32, 98, 7,
203044 /* 70 */ 26, 3, 4, 5, 3, 4, 5, 3, 83, 4,
203045 /* 80 */ 5, 3, 63, 5, 3, 62, 12, 2, 86, 13,
203046 /* 90 */ 9, 30, 10, 10, 54, 57, 75, 78, 78, 53,
203047 /* 100 */ 57, 15, 82, 82, 71,
203048};
203049static const fts5YYCODETYPE fts5yy_lookahead[] = {
203050 /* 0 */ 16, 17, 18, 19, 20, 22, 22, 24, 24, 17,
203051 /* 10 */ 18, 19, 20, 7, 22, 9, 24, 17, 18, 19,
203052 /* 20 */ 20, 9, 22, 9, 24, 13, 17, 18, 19, 20,
203053 /* 30 */ 26, 22, 24, 24, 17, 18, 19, 20, 15, 22,
203054 /* 40 */ 9, 24, 17, 18, 19, 20, 26, 22, 21, 24,
203055 /* 50 */ 6, 7, 9, 9, 10, 12, 12, 6, 7, 21,
203056 /* 60 */ 9, 24, 25, 12, 18, 5, 20, 14, 22, 5,
203057 /* 70 */ 24, 3, 1, 2, 3, 1, 2, 3, 0, 1,
203058 /* 80 */ 2, 3, 11, 2, 3, 11, 9, 10, 5, 12,
203059 /* 90 */ 23, 24, 10, 10, 8, 9, 9, 15, 15, 8,
203060 /* 100 */ 9, 9, 27, 27, 11, 27, 27, 27, 27, 27,
203061 /* 110 */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
203062 /* 120 */ 27,
203063};
203064#define fts5YY_SHIFT_COUNT (34)
203065#define fts5YY_SHIFT_MIN (0)
203066#define fts5YY_SHIFT_MAX (93)
203067static const unsigned char fts5yy_shift_ofst[] = {
203068 /* 0 */ 44, 44, 44, 44, 44, 44, 51, 77, 43, 12,
203069 /* 10 */ 14, 83, 82, 14, 23, 23, 31, 31, 71, 74,
203070 /* 20 */ 78, 81, 86, 91, 6, 53, 53, 60, 64, 68,
203071 /* 30 */ 53, 87, 92, 53, 93,
203072};
203073#define fts5YY_REDUCE_COUNT (17)
203074#define fts5YY_REDUCE_MIN (-17)
203075#define fts5YY_REDUCE_MAX (67)
203076static const signed char fts5yy_reduce_ofst[] = {
203077 /* 0 */ -16, -8, 0, 9, 17, 25, 46, -17, -17, 37,
203078 /* 10 */ 67, 4, 4, 8, 4, 20, 27, 38,
203079};
203080static const fts5YYACTIONTYPE fts5yy_default[] = {
203081 /* 0 */ 80, 80, 80, 80, 80, 80, 95, 80, 80, 105,
203082 /* 10 */ 80, 110, 110, 80, 110, 110, 80, 80, 80, 80,
203083 /* 20 */ 80, 91, 80, 80, 80, 101, 100, 80, 80, 90,
203084 /* 30 */ 103, 80, 80, 104, 80,
203085};
203086/********** End of lemon-generated parsing tables *****************************/
203087
203088/* The next table maps tokens (terminal symbols) into fallback tokens.
203089** If a construct like the following:
203090**
203091** %fallback ID X Y Z.
203092**
203093** appears in the grammar, then ID becomes a fallback token for X, Y,
203094** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
203095** but it does not parse, the type of the token is changed to ID and
203096** the parse is retried before an error is thrown.
203097**
203098** This feature can be used, for example, to cause some keywords in a language
203099** to revert to identifiers if they keyword does not apply in the context where
203100** it appears.
203101*/
203102#ifdef fts5YYFALLBACK
203103static const fts5YYCODETYPE fts5yyFallback[] = {
203104};
203105#endif /* fts5YYFALLBACK */
203106
203107/* The following structure represents a single element of the
203108** parser's stack. Information stored includes:
203109**
203110** + The state number for the parser at this level of the stack.
203111**
203112** + The value of the token stored at this level of the stack.
203113** (In other words, the "major" token.)
203114**
203115** + The semantic value stored at this level of the stack. This is
203116** the information used by the action routines in the grammar.
203117** It is sometimes called the "minor" token.
203118**
203119** After the "shift" half of a SHIFTREDUCE action, the stateno field
203120** actually contains the reduce action for the second half of the
203121** SHIFTREDUCE.
203122*/
203123struct fts5yyStackEntry {
203124 fts5YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
203125 fts5YYCODETYPE major; /* The major token value. This is the code
203126 ** number for the token at this stack level */
203127 fts5YYMINORTYPE minor; /* The user-supplied minor token value. This
203128 ** is the value of the token */
203129};
203130typedef struct fts5yyStackEntry fts5yyStackEntry;
203131
203132/* The state of the parser is completely contained in an instance of
203133** the following structure */
203134struct fts5yyParser {
203135 fts5yyStackEntry *fts5yytos; /* Pointer to top element of the stack */
203136#ifdef fts5YYTRACKMAXSTACKDEPTH
203137 int fts5yyhwm; /* High-water mark of the stack */
203138#endif
203139#ifndef fts5YYNOERRORRECOVERY
203140 int fts5yyerrcnt; /* Shifts left before out of the error */
203141#endif
203142 sqlite3Fts5ParserARG_SDECL /* A place to hold %extra_argument */
203143 sqlite3Fts5ParserCTX_SDECL /* A place to hold %extra_context */
203144#if fts5YYSTACKDEPTH<=0
203145 int fts5yystksz; /* Current side of the stack */
203146 fts5yyStackEntry *fts5yystack; /* The parser's stack */
203147 fts5yyStackEntry fts5yystk0; /* First stack entry */
203148#else
203149 fts5yyStackEntry fts5yystack[fts5YYSTACKDEPTH]; /* The parser's stack */
203150 fts5yyStackEntry *fts5yystackEnd; /* Last entry in the stack */
203151#endif
203152};
203153typedef struct fts5yyParser fts5yyParser;
203154
203155#ifndef NDEBUG1
203156/* #include <stdio.h> */
203157static FILE *fts5yyTraceFILE = 0;
203158static char *fts5yyTracePrompt = 0;
203159#endif /* NDEBUG */
203160
203161#ifndef NDEBUG1
203162/*
203163** Turn parser tracing on by giving a stream to which to write the trace
203164** and a prompt to preface each trace message. Tracing is turned off
203165** by making either argument NULL
203166**
203167** Inputs:
203168** <ul>
203169** <li> A FILE* to which trace output should be written.
203170** If NULL, then tracing is turned off.
203171** <li> A prefix string written at the beginning of every
203172** line of trace output. If NULL, then tracing is
203173** turned off.
203174** </ul>
203175**
203176** Outputs:
203177** None.
203178*/
203179static void sqlite3Fts5ParserTrace(FILE *TraceFILE, char *zTracePrompt){
203180 fts5yyTraceFILE = TraceFILE;
203181 fts5yyTracePrompt = zTracePrompt;
203182 if( fts5yyTraceFILE==0 ) fts5yyTracePrompt = 0;
203183 else if( fts5yyTracePrompt==0 ) fts5yyTraceFILE = 0;
203184}
203185#endif /* NDEBUG */
203186
203187#if defined(fts5YYCOVERAGE) || !defined(NDEBUG1)
203188/* For tracing shifts, the names of all terminals and nonterminals
203189** are required. The following table supplies these names */
203190static const char *const fts5yyTokenName[] = {
203191 /* 0 */ "$",
203192 /* 1 */ "OR",
203193 /* 2 */ "AND",
203194 /* 3 */ "NOT",
203195 /* 4 */ "TERM",
203196 /* 5 */ "COLON",
203197 /* 6 */ "MINUS",
203198 /* 7 */ "LCP",
203199 /* 8 */ "RCP",
203200 /* 9 */ "STRING",
203201 /* 10 */ "LP",
203202 /* 11 */ "RP",
203203 /* 12 */ "CARET",
203204 /* 13 */ "COMMA",
203205 /* 14 */ "PLUS",
203206 /* 15 */ "STAR",
203207 /* 16 */ "input",
203208 /* 17 */ "expr",
203209 /* 18 */ "cnearset",
203210 /* 19 */ "exprlist",
203211 /* 20 */ "colset",
203212 /* 21 */ "colsetlist",
203213 /* 22 */ "nearset",
203214 /* 23 */ "nearphrases",
203215 /* 24 */ "phrase",
203216 /* 25 */ "neardist_opt",
203217 /* 26 */ "star_opt",
203218};
203219#endif /* defined(fts5YYCOVERAGE) || !defined(NDEBUG) */
203220
203221#ifndef NDEBUG1
203222/* For tracing reduce actions, the names of all rules are required.
203223*/
203224static const char *const fts5yyRuleName[] = {
203225 /* 0 */ "input ::= expr",
203226 /* 1 */ "colset ::= MINUS LCP colsetlist RCP",
203227 /* 2 */ "colset ::= LCP colsetlist RCP",
203228 /* 3 */ "colset ::= STRING",
203229 /* 4 */ "colset ::= MINUS STRING",
203230 /* 5 */ "colsetlist ::= colsetlist STRING",
203231 /* 6 */ "colsetlist ::= STRING",
203232 /* 7 */ "expr ::= expr AND expr",
203233 /* 8 */ "expr ::= expr OR expr",
203234 /* 9 */ "expr ::= expr NOT expr",
203235 /* 10 */ "expr ::= colset COLON LP expr RP",
203236 /* 11 */ "expr ::= LP expr RP",
203237 /* 12 */ "expr ::= exprlist",
203238 /* 13 */ "exprlist ::= cnearset",
203239 /* 14 */ "exprlist ::= exprlist cnearset",
203240 /* 15 */ "cnearset ::= nearset",
203241 /* 16 */ "cnearset ::= colset COLON nearset",
203242 /* 17 */ "nearset ::= phrase",
203243 /* 18 */ "nearset ::= CARET phrase",
203244 /* 19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
203245 /* 20 */ "nearphrases ::= phrase",
203246 /* 21 */ "nearphrases ::= nearphrases phrase",
203247 /* 22 */ "neardist_opt ::=",
203248 /* 23 */ "neardist_opt ::= COMMA STRING",
203249 /* 24 */ "phrase ::= phrase PLUS STRING star_opt",
203250 /* 25 */ "phrase ::= STRING star_opt",
203251 /* 26 */ "star_opt ::= STAR",
203252 /* 27 */ "star_opt ::=",
203253};
203254#endif /* NDEBUG */
203255
203256
203257#if fts5YYSTACKDEPTH<=0
203258/*
203259** Try to increase the size of the parser stack. Return the number
203260** of errors. Return 0 on success.
203261*/
203262static int fts5yyGrowStack(fts5yyParser *p){
203263 int newSize;
203264 int idx;
203265 fts5yyStackEntry *pNew;
203266
203267 newSize = p->fts5yystksz*2 + 100;
203268 idx = p->fts5yytos ? (int)(p->fts5yytos - p->fts5yystack) : 0;
203269 if( p->fts5yystack==&p->fts5yystk0 ){
203270 pNew = malloc(newSize*sizeof(pNew[0]));
203271 if( pNew ) pNew[0] = p->fts5yystk0;
203272 }else{
203273 pNew = realloc(p->fts5yystack, newSize*sizeof(pNew[0]));
203274 }
203275 if( pNew ){
203276 p->fts5yystack = pNew;
203277 p->fts5yytos = &p->fts5yystack[idx];
203278#ifndef NDEBUG1
203279 if( fts5yyTraceFILE ){
203280 fprintf(fts5yyTraceFILE,"%sStack grows from %d to %d entries.\n",
203281 fts5yyTracePrompt, p->fts5yystksz, newSize);
203282 }
203283#endif
203284 p->fts5yystksz = newSize;
203285 }
203286 return pNew==0;
203287}
203288#endif
203289
203290/* Datatype of the argument to the memory allocated passed as the
203291** second argument to sqlite3Fts5ParserAlloc() below. This can be changed by
203292** putting an appropriate #define in the %include section of the input
203293** grammar.
203294*/
203295#ifndef fts5YYMALLOCARGTYPE
203296# define fts5YYMALLOCARGTYPE size_t
203297#endif
203298
203299/* Initialize a new parser that has already been allocated.
203300*/
203301static void sqlite3Fts5ParserInit(void *fts5yypRawParser sqlite3Fts5ParserCTX_PDECL){
203302 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yypRawParser;
203303 sqlite3Fts5ParserCTX_STORE
203304#ifdef fts5YYTRACKMAXSTACKDEPTH
203305 fts5yypParser->fts5yyhwm = 0;
203306#endif
203307#if fts5YYSTACKDEPTH<=0
203308 fts5yypParser->fts5yytos = NULL((void*)0);
203309 fts5yypParser->fts5yystack = NULL((void*)0);
203310 fts5yypParser->fts5yystksz = 0;
203311 if( fts5yyGrowStack(fts5yypParser) ){
203312 fts5yypParser->fts5yystack = &fts5yypParser->fts5yystk0;
203313 fts5yypParser->fts5yystksz = 1;
203314 }
203315#endif
203316#ifndef fts5YYNOERRORRECOVERY
203317 fts5yypParser->fts5yyerrcnt = -1;
203318#endif
203319 fts5yypParser->fts5yytos = fts5yypParser->fts5yystack;
203320 fts5yypParser->fts5yystack[0].stateno = 0;
203321 fts5yypParser->fts5yystack[0].major = 0;
203322#if fts5YYSTACKDEPTH>0
203323 fts5yypParser->fts5yystackEnd = &fts5yypParser->fts5yystack[fts5YYSTACKDEPTH-1];
203324#endif
203325}
203326
203327#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
203328/*
203329** This function allocates a new parser.
203330** The only argument is a pointer to a function which works like
203331** malloc.
203332**
203333** Inputs:
203334** A pointer to the function used to allocate memory.
203335**
203336** Outputs:
203337** A pointer to a parser. This pointer is used in subsequent calls
203338** to sqlite3Fts5Parser and sqlite3Fts5ParserFree.
203339*/
203340static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(fts5YYMALLOCARGTYPE) sqlite3Fts5ParserCTX_PDECL){
203341 fts5yyParser *fts5yypParser;
203342 fts5yypParser = (fts5yyParser*)(*mallocProc)( (fts5YYMALLOCARGTYPE)sizeof(fts5yyParser) );
203343 if( fts5yypParser ){
203344 sqlite3Fts5ParserCTX_STORE
203345 sqlite3Fts5ParserInit(fts5yypParser sqlite3Fts5ParserCTX_PARAM);
203346 }
203347 return (void*)fts5yypParser;
203348}
203349#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
203350
203351
203352/* The following function deletes the "minor type" or semantic value
203353** associated with a symbol. The symbol can be either a terminal
203354** or nonterminal. "fts5yymajor" is the symbol code, and "fts5yypminor" is
203355** a pointer to the value to be deleted. The code used to do the
203356** deletions is derived from the %destructor and/or %token_destructor
203357** directives of the input grammar.
203358*/
203359static void fts5yy_destructor(
203360 fts5yyParser *fts5yypParser, /* The parser */
203361 fts5YYCODETYPE fts5yymajor, /* Type code for object to destroy */
203362 fts5YYMINORTYPE *fts5yypminor /* The object to be destroyed */
203363){
203364 sqlite3Fts5ParserARG_FETCH
203365 sqlite3Fts5ParserCTX_FETCH
203366 switch( fts5yymajor ){
203367 /* Here is inserted the actions which take place when a
203368 ** terminal or non-terminal is destroyed. This can happen
203369 ** when the symbol is popped from the stack during a
203370 ** reduce or during error processing or when a parser is
203371 ** being destroyed before it is finished parsing.
203372 **
203373 ** Note: during a reduce, the only symbols destroyed are those
203374 ** which appear on the RHS of the rule, but which are *not* used
203375 ** inside the C code.
203376 */
203377/********* Begin destructor definitions ***************************************/
203378 case 16: /* input */
203379{
203380 (void)pParse;
203381}
203382 break;
203383 case 17: /* expr */
203384 case 18: /* cnearset */
203385 case 19: /* exprlist */
203386{
203387 sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24));
203388}
203389 break;
203390 case 20: /* colset */
203391 case 21: /* colsetlist */
203392{
203393 sqlite3_free((fts5yypminor->fts5yy11));
203394}
203395 break;
203396 case 22: /* nearset */
203397 case 23: /* nearphrases */
203398{
203399 sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46));
203400}
203401 break;
203402 case 24: /* phrase */
203403{
203404 sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53));
203405}
203406 break;
203407/********* End destructor definitions *****************************************/
203408 default: break; /* If no destructor action specified: do nothing */
203409 }
203410}
203411
203412/*
203413** Pop the parser's stack once.
203414**
203415** If there is a destructor routine associated with the token which
203416** is popped from the stack, then call it.
203417*/
203418static void fts5yy_pop_parser_stack(fts5yyParser *pParser){
203419 fts5yyStackEntry *fts5yytos;
203420 assert( pParser->fts5yytos!=0 )((void) (0));
203421 assert( pParser->fts5yytos > pParser->fts5yystack )((void) (0));
203422 fts5yytos = pParser->fts5yytos--;
203423#ifndef NDEBUG1
203424 if( fts5yyTraceFILE ){
203425 fprintf(fts5yyTraceFILE,"%sPopping %s\n",
203426 fts5yyTracePrompt,
203427 fts5yyTokenName[fts5yytos->major]);
203428 }
203429#endif
203430 fts5yy_destructor(pParser, fts5yytos->major, &fts5yytos->minor);
203431}
203432
203433/*
203434** Clear all secondary memory allocations from the parser
203435*/
203436static void sqlite3Fts5ParserFinalize(void *p){
203437 fts5yyParser *pParser = (fts5yyParser*)p;
203438 while( pParser->fts5yytos>pParser->fts5yystack ) fts5yy_pop_parser_stack(pParser);
203439#if fts5YYSTACKDEPTH<=0
203440 if( pParser->fts5yystack!=&pParser->fts5yystk0 ) free(pParser->fts5yystack);
203441#endif
203442}
203443
203444#ifndef sqlite3Fts5Parser_ENGINEALWAYSONSTACK
203445/*
203446** Deallocate and destroy a parser. Destructors are called for
203447** all stack elements before shutting the parser down.
203448**
203449** If the fts5YYPARSEFREENEVERNULL macro exists (for example because it
203450** is defined in a %include section of the input grammar) then it is
203451** assumed that the input pointer is never NULL.
203452*/
203453static void sqlite3Fts5ParserFree(
203454 void *p, /* The parser to be deleted */
203455 void (*freeProc)(void*) /* Function used to reclaim memory */
203456){
203457#ifndef fts5YYPARSEFREENEVERNULL
203458 if( p==0 ) return;
203459#endif
203460 sqlite3Fts5ParserFinalize(p);
203461 (*freeProc)(p);
203462}
203463#endif /* sqlite3Fts5Parser_ENGINEALWAYSONSTACK */
203464
203465/*
203466** Return the peak depth of the stack for a parser.
203467*/
203468#ifdef fts5YYTRACKMAXSTACKDEPTH
203469static int sqlite3Fts5ParserStackPeak(void *p){
203470 fts5yyParser *pParser = (fts5yyParser*)p;
203471 return pParser->fts5yyhwm;
203472}
203473#endif
203474
203475/* This array of booleans keeps track of the parser statement
203476** coverage. The element fts5yycoverage[X][Y] is set when the parser
203477** is in state X and has a lookahead token Y. In a well-tested
203478** systems, every element of this matrix should end up being set.
203479*/
203480#if defined(fts5YYCOVERAGE)
203481static unsigned char fts5yycoverage[fts5YYNSTATE][fts5YYNFTS5TOKEN];
203482#endif
203483
203484/*
203485** Write into out a description of every state/lookahead combination that
203486**
203487** (1) has not been used by the parser, and
203488** (2) is not a syntax error.
203489**
203490** Return the number of missed state/lookahead combinations.
203491*/
203492#if defined(fts5YYCOVERAGE)
203493static int sqlite3Fts5ParserCoverage(FILE *out){
203494 int stateno, iLookAhead, i;
203495 int nMissed = 0;
203496 for(stateno=0; stateno<fts5YYNSTATE; stateno++){
203497 i = fts5yy_shift_ofst[stateno];
203498 for(iLookAhead=0; iLookAhead<fts5YYNFTS5TOKEN; iLookAhead++){
203499 if( fts5yy_lookahead[i+iLookAhead]!=iLookAhead ) continue;
203500 if( fts5yycoverage[stateno][iLookAhead]==0 ) nMissed++;
203501 if( out ){
203502 fprintf(out,"State %d lookahead %s %s\n", stateno,
203503 fts5yyTokenName[iLookAhead],
203504 fts5yycoverage[stateno][iLookAhead] ? "ok" : "missed");
203505 }
203506 }
203507 }
203508 return nMissed;
203509}
203510#endif
203511
203512/*
203513** Find the appropriate action for a parser given the terminal
203514** look-ahead token iLookAhead.
203515*/
203516static fts5YYACTIONTYPE fts5yy_find_shift_action(
203517 fts5YYCODETYPE iLookAhead, /* The look-ahead token */
203518 fts5YYACTIONTYPE stateno /* Current state number */
203519){
203520 int i;
203521
203522 if( stateno>fts5YY_MAX_SHIFT ) return stateno;
203523 assert( stateno <= fts5YY_SHIFT_COUNT )((void) (0));
203524#if defined(fts5YYCOVERAGE)
203525 fts5yycoverage[stateno][iLookAhead] = 1;
203526#endif
203527 do{
203528 i = fts5yy_shift_ofst[stateno];
203529 assert( i>=0 )((void) (0));
203530 /* assert( i+fts5YYNFTS5TOKEN<=(int)fts5YY_NLOOKAHEAD ); */
203531 assert( iLookAhead!=fts5YYNOCODE )((void) (0));
203532 assert( iLookAhead < fts5YYNFTS5TOKEN )((void) (0));
203533 i += iLookAhead;
203534 if( i>=fts5YY_NLOOKAHEAD || fts5yy_lookahead[i]!=iLookAhead ){
203535#ifdef fts5YYFALLBACK
203536 fts5YYCODETYPE iFallback; /* Fallback token */
203537 if( iLookAhead<sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])
203538 && (iFallback = fts5yyFallback[iLookAhead])!=0 ){
203539#ifndef NDEBUG1
203540 if( fts5yyTraceFILE ){
203541 fprintf(fts5yyTraceFILE, "%sFALLBACK %s => %s\n",
203542 fts5yyTracePrompt, fts5yyTokenName[iLookAhead], fts5yyTokenName[iFallback]);
203543 }
203544#endif
203545 assert( fts5yyFallback[iFallback]==0 )((void) (0)); /* Fallback loop must terminate */
203546 iLookAhead = iFallback;
203547 continue;
203548 }
203549#endif
203550#ifdef fts5YYWILDCARD
203551 {
203552 int j = i - iLookAhead + fts5YYWILDCARD;
203553 if(
203554#if fts5YY_SHIFT_MIN+fts5YYWILDCARD<0
203555 j>=0 &&
203556#endif
203557#if fts5YY_SHIFT_MAX+fts5YYWILDCARD>=fts5YY_ACTTAB_COUNT
203558 j<fts5YY_ACTTAB_COUNT &&
203559#endif
203560 j<(int)(sizeof(fts5yy_lookahead)/sizeof(fts5yy_lookahead[0])) &&
203561 fts5yy_lookahead[j]==fts5YYWILDCARD && iLookAhead>0
203562 ){
203563#ifndef NDEBUG1
203564 if( fts5yyTraceFILE ){
203565 fprintf(fts5yyTraceFILE, "%sWILDCARD %s => %s\n",
203566 fts5yyTracePrompt, fts5yyTokenName[iLookAhead],
203567 fts5yyTokenName[fts5YYWILDCARD]);
203568 }
203569#endif /* NDEBUG */
203570 return fts5yy_action[j];
203571 }
203572 }
203573#endif /* fts5YYWILDCARD */
203574 return fts5yy_default[stateno];
203575 }else{
203576 return fts5yy_action[i];
203577 }
203578 }while(1);
203579}
203580
203581/*
203582** Find the appropriate action for a parser given the non-terminal
203583** look-ahead token iLookAhead.
203584*/
203585static fts5YYACTIONTYPE fts5yy_find_reduce_action(
203586 fts5YYACTIONTYPE stateno, /* Current state number */
203587 fts5YYCODETYPE iLookAhead /* The look-ahead token */
203588){
203589 int i;
203590#ifdef fts5YYERRORSYMBOL
203591 if( stateno>fts5YY_REDUCE_COUNT ){
203592 return fts5yy_default[stateno];
203593 }
203594#else
203595 assert( stateno<=fts5YY_REDUCE_COUNT )((void) (0));
203596#endif
203597 i = fts5yy_reduce_ofst[stateno];
203598 assert( iLookAhead!=fts5YYNOCODE )((void) (0));
203599 i += iLookAhead;
203600#ifdef fts5YYERRORSYMBOL
203601 if( i<0 || i>=fts5YY_ACTTAB_COUNT || fts5yy_lookahead[i]!=iLookAhead ){
203602 return fts5yy_default[stateno];
203603 }
203604#else
203605 assert( i>=0 && i<fts5YY_ACTTAB_COUNT )((void) (0));
203606 assert( fts5yy_lookahead[i]==iLookAhead )((void) (0));
203607#endif
203608 return fts5yy_action[i];
203609}
203610
203611/*
203612** The following routine is called if the stack overflows.
203613*/
203614static void fts5yyStackOverflow(fts5yyParser *fts5yypParser){
203615 sqlite3Fts5ParserARG_FETCH
203616 sqlite3Fts5ParserCTX_FETCH
203617#ifndef NDEBUG1
203618 if( fts5yyTraceFILE ){
203619 fprintf(fts5yyTraceFILE,"%sStack Overflow!\n",fts5yyTracePrompt);
203620 }
203621#endif
203622 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
203623 /* Here code is inserted which will execute if the parser
203624 ** stack every overflows */
203625/******** Begin %stack_overflow code ******************************************/
203626
203627 sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
203628/******** End %stack_overflow code ********************************************/
203629 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument var */
203630 sqlite3Fts5ParserCTX_STORE
203631}
203632
203633/*
203634** Print tracing information for a SHIFT action
203635*/
203636#ifndef NDEBUG1
203637static void fts5yyTraceShift(fts5yyParser *fts5yypParser, int fts5yyNewState, const char *zTag){
203638 if( fts5yyTraceFILE ){
203639 if( fts5yyNewState<fts5YYNSTATE ){
203640 fprintf(fts5yyTraceFILE,"%s%s '%s', go to state %d\n",
203641 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
203642 fts5yyNewState);
203643 }else{
203644 fprintf(fts5yyTraceFILE,"%s%s '%s', pending reduce %d\n",
203645 fts5yyTracePrompt, zTag, fts5yyTokenName[fts5yypParser->fts5yytos->major],
203646 fts5yyNewState - fts5YY_MIN_REDUCE);
203647 }
203648 }
203649}
203650#else
203651# define fts5yyTraceShift(X,Y,Z)
203652#endif
203653
203654/*
203655** Perform a shift action.
203656*/
203657static void fts5yy_shift(
203658 fts5yyParser *fts5yypParser, /* The parser to be shifted */
203659 fts5YYACTIONTYPE fts5yyNewState, /* The new state to shift in */
203660 fts5YYCODETYPE fts5yyMajor, /* The major token to shift in */
203661 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyMinor /* The minor token to shift in */
203662){
203663 fts5yyStackEntry *fts5yytos;
203664 fts5yypParser->fts5yytos++;
203665#ifdef fts5YYTRACKMAXSTACKDEPTH
203666 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
203667 fts5yypParser->fts5yyhwm++;
203668 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack) )((void) (0));
203669 }
203670#endif
203671#if fts5YYSTACKDEPTH>0
203672 if( fts5yypParser->fts5yytos>fts5yypParser->fts5yystackEnd ){
203673 fts5yypParser->fts5yytos--;
203674 fts5yyStackOverflow(fts5yypParser);
203675 return;
203676 }
203677#else
203678 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz] ){
203679 if( fts5yyGrowStack(fts5yypParser) ){
203680 fts5yypParser->fts5yytos--;
203681 fts5yyStackOverflow(fts5yypParser);
203682 return;
203683 }
203684 }
203685#endif
203686 if( fts5yyNewState > fts5YY_MAX_SHIFT ){
203687 fts5yyNewState += fts5YY_MIN_REDUCE - fts5YY_MIN_SHIFTREDUCE;
203688 }
203689 fts5yytos = fts5yypParser->fts5yytos;
203690 fts5yytos->stateno = fts5yyNewState;
203691 fts5yytos->major = fts5yyMajor;
203692 fts5yytos->minor.fts5yy0 = fts5yyMinor;
203693 fts5yyTraceShift(fts5yypParser, fts5yyNewState, "Shift");
203694}
203695
203696/* For rule J, fts5yyRuleInfoLhs[J] contains the symbol on the left-hand side
203697** of that rule */
203698static const fts5YYCODETYPE fts5yyRuleInfoLhs[] = {
203699 16, /* (0) input ::= expr */
203700 20, /* (1) colset ::= MINUS LCP colsetlist RCP */
203701 20, /* (2) colset ::= LCP colsetlist RCP */
203702 20, /* (3) colset ::= STRING */
203703 20, /* (4) colset ::= MINUS STRING */
203704 21, /* (5) colsetlist ::= colsetlist STRING */
203705 21, /* (6) colsetlist ::= STRING */
203706 17, /* (7) expr ::= expr AND expr */
203707 17, /* (8) expr ::= expr OR expr */
203708 17, /* (9) expr ::= expr NOT expr */
203709 17, /* (10) expr ::= colset COLON LP expr RP */
203710 17, /* (11) expr ::= LP expr RP */
203711 17, /* (12) expr ::= exprlist */
203712 19, /* (13) exprlist ::= cnearset */
203713 19, /* (14) exprlist ::= exprlist cnearset */
203714 18, /* (15) cnearset ::= nearset */
203715 18, /* (16) cnearset ::= colset COLON nearset */
203716 22, /* (17) nearset ::= phrase */
203717 22, /* (18) nearset ::= CARET phrase */
203718 22, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
203719 23, /* (20) nearphrases ::= phrase */
203720 23, /* (21) nearphrases ::= nearphrases phrase */
203721 25, /* (22) neardist_opt ::= */
203722 25, /* (23) neardist_opt ::= COMMA STRING */
203723 24, /* (24) phrase ::= phrase PLUS STRING star_opt */
203724 24, /* (25) phrase ::= STRING star_opt */
203725 26, /* (26) star_opt ::= STAR */
203726 26, /* (27) star_opt ::= */
203727};
203728
203729/* For rule J, fts5yyRuleInfoNRhs[J] contains the negative of the number
203730** of symbols on the right-hand side of that rule. */
203731static const signed char fts5yyRuleInfoNRhs[] = {
203732 -1, /* (0) input ::= expr */
203733 -4, /* (1) colset ::= MINUS LCP colsetlist RCP */
203734 -3, /* (2) colset ::= LCP colsetlist RCP */
203735 -1, /* (3) colset ::= STRING */
203736 -2, /* (4) colset ::= MINUS STRING */
203737 -2, /* (5) colsetlist ::= colsetlist STRING */
203738 -1, /* (6) colsetlist ::= STRING */
203739 -3, /* (7) expr ::= expr AND expr */
203740 -3, /* (8) expr ::= expr OR expr */
203741 -3, /* (9) expr ::= expr NOT expr */
203742 -5, /* (10) expr ::= colset COLON LP expr RP */
203743 -3, /* (11) expr ::= LP expr RP */
203744 -1, /* (12) expr ::= exprlist */
203745 -1, /* (13) exprlist ::= cnearset */
203746 -2, /* (14) exprlist ::= exprlist cnearset */
203747 -1, /* (15) cnearset ::= nearset */
203748 -3, /* (16) cnearset ::= colset COLON nearset */
203749 -1, /* (17) nearset ::= phrase */
203750 -2, /* (18) nearset ::= CARET phrase */
203751 -5, /* (19) nearset ::= STRING LP nearphrases neardist_opt RP */
203752 -1, /* (20) nearphrases ::= phrase */
203753 -2, /* (21) nearphrases ::= nearphrases phrase */
203754 0, /* (22) neardist_opt ::= */
203755 -2, /* (23) neardist_opt ::= COMMA STRING */
203756 -4, /* (24) phrase ::= phrase PLUS STRING star_opt */
203757 -2, /* (25) phrase ::= STRING star_opt */
203758 -1, /* (26) star_opt ::= STAR */
203759 0, /* (27) star_opt ::= */
203760};
203761
203762static void fts5yy_accept(fts5yyParser*); /* Forward Declaration */
203763
203764/*
203765** Perform a reduce action and the shift that must immediately
203766** follow the reduce.
203767**
203768** The fts5yyLookahead and fts5yyLookaheadToken parameters provide reduce actions
203769** access to the lookahead token (if any). The fts5yyLookahead will be fts5YYNOCODE
203770** if the lookahead token has already been consumed. As this procedure is
203771** only called from one place, optimizing compilers will in-line it, which
203772** means that the extra parameters have no performance impact.
203773*/
203774static fts5YYACTIONTYPE fts5yy_reduce(
203775 fts5yyParser *fts5yypParser, /* The parser */
203776 unsigned int fts5yyruleno, /* Number of the rule by which to reduce */
203777 int fts5yyLookahead, /* Lookahead token, or fts5YYNOCODE if none */
203778 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyLookaheadToken /* Value of the lookahead token */
203779 sqlite3Fts5ParserCTX_PDECL /* %extra_context */
203780){
203781 int fts5yygoto; /* The next state */
203782 fts5YYACTIONTYPE fts5yyact; /* The next action */
203783 fts5yyStackEntry *fts5yymsp; /* The top of the parser's stack */
203784 int fts5yysize; /* Amount to pop the stack */
203785 sqlite3Fts5ParserARG_FETCH
203786 (void)fts5yyLookahead;
203787 (void)fts5yyLookaheadToken;
203788 fts5yymsp = fts5yypParser->fts5yytos;
203789#ifndef NDEBUG1
203790 if( fts5yyTraceFILE && fts5yyruleno<(int)(sizeof(fts5yyRuleName)/sizeof(fts5yyRuleName[0])) ){
203791 fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
203792 if( fts5yysize ){
203793 fprintf(fts5yyTraceFILE, "%sReduce %d [%s], go to state %d.\n",
203794 fts5yyTracePrompt,
203795 fts5yyruleno, fts5yyRuleName[fts5yyruleno], fts5yymsp[fts5yysize].stateno);
203796 }else{
203797 fprintf(fts5yyTraceFILE, "%sReduce %d [%s].\n",
203798 fts5yyTracePrompt, fts5yyruleno, fts5yyRuleName[fts5yyruleno]);
203799 }
203800 }
203801#endif /* NDEBUG */
203802
203803 /* Check that the stack is large enough to grow by a single entry
203804 ** if the RHS of the rule is empty. This ensures that there is room
203805 ** enough on the stack to push the LHS value */
203806 if( fts5yyRuleInfoNRhs[fts5yyruleno]==0 ){
203807#ifdef fts5YYTRACKMAXSTACKDEPTH
203808 if( (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack)>fts5yypParser->fts5yyhwm ){
203809 fts5yypParser->fts5yyhwm++;
203810 assert( fts5yypParser->fts5yyhwm == (int)(fts5yypParser->fts5yytos - fts5yypParser->fts5yystack))((void) (0));
203811 }
203812#endif
203813#if fts5YYSTACKDEPTH>0
203814 if( fts5yypParser->fts5yytos>=fts5yypParser->fts5yystackEnd ){
203815 fts5yyStackOverflow(fts5yypParser);
203816 /* The call to fts5yyStackOverflow() above pops the stack until it is
203817 ** empty, causing the main parser loop to exit. So the return value
203818 ** is never used and does not matter. */
203819 return 0;
203820 }
203821#else
203822 if( fts5yypParser->fts5yytos>=&fts5yypParser->fts5yystack[fts5yypParser->fts5yystksz-1] ){
203823 if( fts5yyGrowStack(fts5yypParser) ){
203824 fts5yyStackOverflow(fts5yypParser);
203825 /* The call to fts5yyStackOverflow() above pops the stack until it is
203826 ** empty, causing the main parser loop to exit. So the return value
203827 ** is never used and does not matter. */
203828 return 0;
203829 }
203830 fts5yymsp = fts5yypParser->fts5yytos;
203831 }
203832#endif
203833 }
203834
203835 switch( fts5yyruleno ){
203836 /* Beginning here are the reduction cases. A typical example
203837 ** follows:
203838 ** case 0:
203839 ** #line <lineno> <grammarfile>
203840 ** { ... } // User supplied code
203841 ** #line <lineno> <thisfile>
203842 ** break;
203843 */
203844/********** Begin reduce actions **********************************************/
203845 fts5YYMINORTYPE fts5yylhsminor;
203846 case 0: /* input ::= expr */
203847{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
203848 break;
203849 case 1: /* colset ::= MINUS LCP colsetlist RCP */
203850{
203851 fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
203852}
203853 break;
203854 case 2: /* colset ::= LCP colsetlist RCP */
203855{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
203856 break;
203857 case 3: /* colset ::= STRING */
203858{
203859 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
203860}
203861 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
203862 break;
203863 case 4: /* colset ::= MINUS STRING */
203864{
203865 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
203866 fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
203867}
203868 break;
203869 case 5: /* colsetlist ::= colsetlist STRING */
203870{
203871 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
203872 fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
203873 break;
203874 case 6: /* colsetlist ::= STRING */
203875{
203876 fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
203877}
203878 fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
203879 break;
203880 case 7: /* expr ::= expr AND expr */
203881{
203882 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
203883}
203884 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
203885 break;
203886 case 8: /* expr ::= expr OR expr */
203887{
203888 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
203889}
203890 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
203891 break;
203892 case 9: /* expr ::= expr NOT expr */
203893{
203894 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
203895}
203896 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
203897 break;
203898 case 10: /* expr ::= colset COLON LP expr RP */
203899{
203900 sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
203901 fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
203902}
203903 fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
203904 break;
203905 case 11: /* expr ::= LP expr RP */
203906{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
203907 break;
203908 case 12: /* expr ::= exprlist */
203909 case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
203910{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
203911 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
203912 break;
203913 case 14: /* exprlist ::= exprlist cnearset */
203914{
203915 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
203916}
203917 fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
203918 break;
203919 case 15: /* cnearset ::= nearset */
203920{
203921 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
203922}
203923 fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
203924 break;
203925 case 16: /* cnearset ::= colset COLON nearset */
203926{
203927 fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46);
203928 sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
203929}
203930 fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
203931 break;
203932 case 17: /* nearset ::= phrase */
203933{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
203934 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
203935 break;
203936 case 18: /* nearset ::= CARET phrase */
203937{
203938 sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy53);
203939 fts5yymsp[-1].minor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
203940}
203941 break;
203942 case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
203943{
203944 sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
203945 sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
203946 fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
203947}
203948 fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
203949 break;
203950 case 20: /* nearphrases ::= phrase */
203951{
203952 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53);
203953}
203954 fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
203955 break;
203956 case 21: /* nearphrases ::= nearphrases phrase */
203957{
203958 fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
203959}
203960 fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
203961 break;
203962 case 22: /* neardist_opt ::= */
203963{ fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
203964 break;
203965 case 23: /* neardist_opt ::= COMMA STRING */
203966{ fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
203967 break;
203968 case 24: /* phrase ::= phrase PLUS STRING star_opt */
203969{
203970 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
203971}
203972 fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
203973 break;
203974 case 25: /* phrase ::= STRING star_opt */
203975{
203976 fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
203977}
203978 fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
203979 break;
203980 case 26: /* star_opt ::= STAR */
203981{ fts5yymsp[0].minor.fts5yy4 = 1; }
203982 break;
203983 case 27: /* star_opt ::= */
203984{ fts5yymsp[1].minor.fts5yy4 = 0; }
203985 break;
203986 default:
203987 break;
203988/********** End reduce actions ************************************************/
203989 };
203990 assert( fts5yyruleno<sizeof(fts5yyRuleInfoLhs)/sizeof(fts5yyRuleInfoLhs[0]) )((void) (0));
203991 fts5yygoto = fts5yyRuleInfoLhs[fts5yyruleno];
203992 fts5yysize = fts5yyRuleInfoNRhs[fts5yyruleno];
203993 fts5yyact = fts5yy_find_reduce_action(fts5yymsp[fts5yysize].stateno,(fts5YYCODETYPE)fts5yygoto);
203994
203995 /* There are no SHIFTREDUCE actions on nonterminals because the table
203996 ** generator has simplified them to pure REDUCE actions. */
203997 assert( !(fts5yyact>fts5YY_MAX_SHIFT && fts5yyact<=fts5YY_MAX_SHIFTREDUCE) )((void) (0));
203998
203999 /* It is not possible for a REDUCE to be followed by an error */
204000 assert( fts5yyact!=fts5YY_ERROR_ACTION )((void) (0));
204001
204002 fts5yymsp += fts5yysize+1;
204003 fts5yypParser->fts5yytos = fts5yymsp;
204004 fts5yymsp->stateno = (fts5YYACTIONTYPE)fts5yyact;
204005 fts5yymsp->major = (fts5YYCODETYPE)fts5yygoto;
204006 fts5yyTraceShift(fts5yypParser, fts5yyact, "... then shift");
204007 return fts5yyact;
204008}
204009
204010/*
204011** The following code executes when the parse fails
204012*/
204013#ifndef fts5YYNOERRORRECOVERY
204014static void fts5yy_parse_failed(
204015 fts5yyParser *fts5yypParser /* The parser */
204016){
204017 sqlite3Fts5ParserARG_FETCH
204018 sqlite3Fts5ParserCTX_FETCH
204019#ifndef NDEBUG1
204020 if( fts5yyTraceFILE ){
204021 fprintf(fts5yyTraceFILE,"%sFail!\n",fts5yyTracePrompt);
204022 }
204023#endif
204024 while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack ) fts5yy_pop_parser_stack(fts5yypParser);
204025 /* Here code is inserted which will be executed whenever the
204026 ** parser fails */
204027/************ Begin %parse_failure code ***************************************/
204028/************ End %parse_failure code *****************************************/
204029 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
204030 sqlite3Fts5ParserCTX_STORE
204031}
204032#endif /* fts5YYNOERRORRECOVERY */
204033
204034/*
204035** The following code executes when a syntax error first occurs.
204036*/
204037static void fts5yy_syntax_error(
204038 fts5yyParser *fts5yypParser, /* The parser */
204039 int fts5yymajor, /* The major type of the error token */
204040 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The minor type of the error token */
204041){
204042 sqlite3Fts5ParserARG_FETCH
204043 sqlite3Fts5ParserCTX_FETCH
204044#define FTS5TOKEN fts5yyminor
204045/************ Begin %syntax_error code ****************************************/
204046
204047 UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
204048 sqlite3Fts5ParseError(
204049 pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
204050 );
204051/************ End %syntax_error code ******************************************/
204052 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
204053 sqlite3Fts5ParserCTX_STORE
204054}
204055
204056/*
204057** The following is executed when the parser accepts
204058*/
204059static void fts5yy_accept(
204060 fts5yyParser *fts5yypParser /* The parser */
204061){
204062 sqlite3Fts5ParserARG_FETCH
204063 sqlite3Fts5ParserCTX_FETCH
204064#ifndef NDEBUG1
204065 if( fts5yyTraceFILE ){
204066 fprintf(fts5yyTraceFILE,"%sAccept!\n",fts5yyTracePrompt);
204067 }
204068#endif
204069#ifndef fts5YYNOERRORRECOVERY
204070 fts5yypParser->fts5yyerrcnt = -1;
204071#endif
204072 assert( fts5yypParser->fts5yytos==fts5yypParser->fts5yystack )((void) (0));
204073 /* Here code is inserted which will be executed whenever the
204074 ** parser accepts */
204075/*********** Begin %parse_accept code *****************************************/
204076/*********** End %parse_accept code *******************************************/
204077 sqlite3Fts5ParserARG_STORE /* Suppress warning about unused %extra_argument variable */
204078 sqlite3Fts5ParserCTX_STORE
204079}
204080
204081/* The main parser program.
204082** The first argument is a pointer to a structure obtained from
204083** "sqlite3Fts5ParserAlloc" which describes the current state of the parser.
204084** The second argument is the major token number. The third is
204085** the minor token. The fourth optional argument is whatever the
204086** user wants (and specified in the grammar) and is available for
204087** use by the action routines.
204088**
204089** Inputs:
204090** <ul>
204091** <li> A pointer to the parser (an opaque structure.)
204092** <li> The major token number.
204093** <li> The minor token number.
204094** <li> An option argument of a grammar-specified type.
204095** </ul>
204096**
204097** Outputs:
204098** None.
204099*/
204100static void sqlite3Fts5Parser(
204101 void *fts5yyp, /* The parser */
204102 int fts5yymajor, /* The major token code number */
204103 sqlite3Fts5ParserFTS5TOKENTYPE fts5yyminor /* The value for the token */
204104 sqlite3Fts5ParserARG_PDECL /* Optional %extra_argument parameter */
204105){
204106 fts5YYMINORTYPE fts5yyminorunion;
204107 fts5YYACTIONTYPE fts5yyact; /* The parser action. */
204108#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
204109 int fts5yyendofinput; /* True if we are at the end of input */
204110#endif
204111#ifdef fts5YYERRORSYMBOL
204112 int fts5yyerrorhit = 0; /* True if fts5yymajor has invoked an error */
204113#endif
204114 fts5yyParser *fts5yypParser = (fts5yyParser*)fts5yyp; /* The parser */
204115 sqlite3Fts5ParserCTX_FETCH
204116 sqlite3Fts5ParserARG_STORE
204117
204118 assert( fts5yypParser->fts5yytos!=0 )((void) (0));
204119#if !defined(fts5YYERRORSYMBOL) && !defined(fts5YYNOERRORRECOVERY)
204120 fts5yyendofinput = (fts5yymajor==0);
204121#endif
204122
204123 fts5yyact = fts5yypParser->fts5yytos->stateno;
204124#ifndef NDEBUG1
204125 if( fts5yyTraceFILE ){
204126 if( fts5yyact < fts5YY_MIN_REDUCE ){
204127 fprintf(fts5yyTraceFILE,"%sInput '%s' in state %d\n",
204128 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact);
204129 }else{
204130 fprintf(fts5yyTraceFILE,"%sInput '%s' with pending reduce %d\n",
204131 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor],fts5yyact-fts5YY_MIN_REDUCE);
204132 }
204133 }
204134#endif
204135
204136 do{
204137 assert( fts5yyact==fts5yypParser->fts5yytos->stateno )((void) (0));
204138 fts5yyact = fts5yy_find_shift_action((fts5YYCODETYPE)fts5yymajor,fts5yyact);
204139 if( fts5yyact >= fts5YY_MIN_REDUCE ){
204140 fts5yyact = fts5yy_reduce(fts5yypParser,fts5yyact-fts5YY_MIN_REDUCE,fts5yymajor,
204141 fts5yyminor sqlite3Fts5ParserCTX_PARAM);
204142 }else if( fts5yyact <= fts5YY_MAX_SHIFTREDUCE ){
204143 fts5yy_shift(fts5yypParser,fts5yyact,(fts5YYCODETYPE)fts5yymajor,fts5yyminor);
204144#ifndef fts5YYNOERRORRECOVERY
204145 fts5yypParser->fts5yyerrcnt--;
204146#endif
204147 break;
204148 }else if( fts5yyact==fts5YY_ACCEPT_ACTION ){
204149 fts5yypParser->fts5yytos--;
204150 fts5yy_accept(fts5yypParser);
204151 return;
204152 }else{
204153 assert( fts5yyact == fts5YY_ERROR_ACTION )((void) (0));
204154 fts5yyminorunion.fts5yy0 = fts5yyminor;
204155#ifdef fts5YYERRORSYMBOL
204156 int fts5yymx;
204157#endif
204158#ifndef NDEBUG1
204159 if( fts5yyTraceFILE ){
204160 fprintf(fts5yyTraceFILE,"%sSyntax Error!\n",fts5yyTracePrompt);
204161 }
204162#endif
204163#ifdef fts5YYERRORSYMBOL
204164 /* A syntax error has occurred.
204165 ** The response to an error depends upon whether or not the
204166 ** grammar defines an error token "ERROR".
204167 **
204168 ** This is what we do if the grammar does define ERROR:
204169 **
204170 ** * Call the %syntax_error function.
204171 **
204172 ** * Begin popping the stack until we enter a state where
204173 ** it is legal to shift the error symbol, then shift
204174 ** the error symbol.
204175 **
204176 ** * Set the error count to three.
204177 **
204178 ** * Begin accepting and shifting new tokens. No new error
204179 ** processing will occur until three tokens have been
204180 ** shifted successfully.
204181 **
204182 */
204183 if( fts5yypParser->fts5yyerrcnt<0 ){
204184 fts5yy_syntax_error(fts5yypParser,fts5yymajor,fts5yyminor);
204185 }
204186 fts5yymx = fts5yypParser->fts5yytos->major;
204187 if( fts5yymx==fts5YYERRORSYMBOL || fts5yyerrorhit ){
204188#ifndef NDEBUG1
204189 if( fts5yyTraceFILE ){
204190 fprintf(fts5yyTraceFILE,"%sDiscard input token %s\n",
204191 fts5yyTracePrompt,fts5yyTokenName[fts5yymajor]);
204192 }
204193#endif
204194 fts5yy_destructor(fts5yypParser, (fts5YYCODETYPE)fts5yymajor, &fts5yyminorunion);
204195 fts5yymajor = fts5YYNOCODE;
204196 }else{
204197 while( fts5yypParser->fts5yytos >= fts5yypParser->fts5yystack
204198 && (fts5yyact = fts5yy_find_reduce_action(
204199 fts5yypParser->fts5yytos->stateno,
204200 fts5YYERRORSYMBOL)) > fts5YY_MAX_SHIFTREDUCE
204201 ){
204202 fts5yy_pop_parser_stack(fts5yypParser);
204203 }
204204 if( fts5yypParser->fts5yytos < fts5yypParser->fts5yystack || fts5yymajor==0 ){
204205 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
204206 fts5yy_parse_failed(fts5yypParser);
204207#ifndef fts5YYNOERRORRECOVERY
204208 fts5yypParser->fts5yyerrcnt = -1;
204209#endif
204210 fts5yymajor = fts5YYNOCODE;
204211 }else if( fts5yymx!=fts5YYERRORSYMBOL ){
204212 fts5yy_shift(fts5yypParser,fts5yyact,fts5YYERRORSYMBOL,fts5yyminor);
204213 }
204214 }
204215 fts5yypParser->fts5yyerrcnt = 3;
204216 fts5yyerrorhit = 1;
204217 if( fts5yymajor==fts5YYNOCODE ) break;
204218 fts5yyact = fts5yypParser->fts5yytos->stateno;
204219#elif defined(fts5YYNOERRORRECOVERY)
204220 /* If the fts5YYNOERRORRECOVERY macro is defined, then do not attempt to
204221 ** do any kind of error recovery. Instead, simply invoke the syntax
204222 ** error routine and continue going as if nothing had happened.
204223 **
204224 ** Applications can set this macro (for example inside %include) if
204225 ** they intend to abandon the parse upon the first syntax error seen.
204226 */
204227 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
204228 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
204229 break;
204230#else /* fts5YYERRORSYMBOL is not defined */
204231 /* This is what we do if the grammar does not define ERROR:
204232 **
204233 ** * Report an error message, and throw away the input token.
204234 **
204235 ** * If the input token is $, then fail the parse.
204236 **
204237 ** As before, subsequent error messages are suppressed until
204238 ** three input tokens have been successfully shifted.
204239 */
204240 if( fts5yypParser->fts5yyerrcnt<=0 ){
204241 fts5yy_syntax_error(fts5yypParser,fts5yymajor, fts5yyminor);
204242 }
204243 fts5yypParser->fts5yyerrcnt = 3;
204244 fts5yy_destructor(fts5yypParser,(fts5YYCODETYPE)fts5yymajor,&fts5yyminorunion);
204245 if( fts5yyendofinput ){
204246 fts5yy_parse_failed(fts5yypParser);
204247#ifndef fts5YYNOERRORRECOVERY
204248 fts5yypParser->fts5yyerrcnt = -1;
204249#endif
204250 }
204251 break;
204252#endif
204253 }
204254 }while( fts5yypParser->fts5yytos>fts5yypParser->fts5yystack );
204255#ifndef NDEBUG1
204256 if( fts5yyTraceFILE ){
204257 fts5yyStackEntry *i;
204258 char cDiv = '[';
204259 fprintf(fts5yyTraceFILE,"%sReturn. Stack=",fts5yyTracePrompt);
204260 for(i=&fts5yypParser->fts5yystack[1]; i<=fts5yypParser->fts5yytos; i++){
204261 fprintf(fts5yyTraceFILE,"%c%s", cDiv, fts5yyTokenName[i->major]);
204262 cDiv = ' ';
204263 }
204264 fprintf(fts5yyTraceFILE,"]\n");
204265 }
204266#endif
204267 return;
204268}
204269
204270/*
204271** Return the fallback token corresponding to canonical token iToken, or
204272** 0 if iToken has no fallback.
204273*/
204274static int sqlite3Fts5ParserFallback(int iToken){
204275#ifdef fts5YYFALLBACK
204276 if( iToken<(int)(sizeof(fts5yyFallback)/sizeof(fts5yyFallback[0])) ){
204277 return fts5yyFallback[iToken];
204278 }
204279#else
204280 (void)iToken;
204281#endif
204282 return 0;
204283}
204284
204285/*
204286** 2014 May 31
204287**
204288** The author disclaims copyright to this source code. In place of
204289** a legal notice, here is a blessing:
204290**
204291** May you do good and not evil.
204292** May you find forgiveness for yourself and forgive others.
204293** May you share freely, never taking more than you give.
204294**
204295******************************************************************************
204296*/
204297
204298
204299/* #include "fts5Int.h" */
204300#include <math.h> /* amalgamator: keep */
204301
204302/*
204303** Object used to iterate through all "coalesced phrase instances" in
204304** a single column of the current row. If the phrase instances in the
204305** column being considered do not overlap, this object simply iterates
204306** through them. Or, if they do overlap (share one or more tokens in
204307** common), each set of overlapping instances is treated as a single
204308** match. See documentation for the highlight() auxiliary function for
204309** details.
204310**
204311** Usage is:
204312**
204313** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
204314** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
204315** rc = fts5CInstIterNext(&iter)
204316** ){
204317** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
204318** }
204319**
204320*/
204321typedef struct CInstIter CInstIter;
204322struct CInstIter {
204323 const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
204324 Fts5Context *pFts; /* First arg to pass to pApi functions */
204325 int iCol; /* Column to search */
204326 int iInst; /* Next phrase instance index */
204327 int nInst; /* Total number of phrase instances */
204328
204329 /* Output variables */
204330 int iStart; /* First token in coalesced phrase instance */
204331 int iEnd; /* Last token in coalesced phrase instance */
204332};
204333
204334/*
204335** Advance the iterator to the next coalesced phrase instance. Return
204336** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
204337*/
204338static int fts5CInstIterNext(CInstIter *pIter){
204339 int rc = SQLITE_OK0;
204340 pIter->iStart = -1;
204341 pIter->iEnd = -1;
204342
204343 while( rc==SQLITE_OK0 && pIter->iInst<pIter->nInst ){
204344 int ip; int ic; int io;
204345 rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
204346 if( rc==SQLITE_OK0 ){
204347 if( ic==pIter->iCol ){
204348 int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
204349 if( pIter->iStart<0 ){
204350 pIter->iStart = io;
204351 pIter->iEnd = iEnd;
204352 }else if( io<=pIter->iEnd ){
204353 if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
204354 }else{
204355 break;
204356 }
204357 }
204358 pIter->iInst++;
204359 }
204360 }
204361
204362 return rc;
204363}
204364
204365/*
204366** Initialize the iterator object indicated by the final parameter to
204367** iterate through coalesced phrase instances in column iCol.
204368*/
204369static int fts5CInstIterInit(
204370 const Fts5ExtensionApi *pApi,
204371 Fts5Context *pFts,
204372 int iCol,
204373 CInstIter *pIter
204374){
204375 int rc;
204376
204377 memset(pIter, 0, sizeof(CInstIter));
204378 pIter->pApi = pApi;
204379 pIter->pFts = pFts;
204380 pIter->iCol = iCol;
204381 rc = pApi->xInstCount(pFts, &pIter->nInst);
204382
204383 if( rc==SQLITE_OK0 ){
204384 rc = fts5CInstIterNext(pIter);
204385 }
204386
204387 return rc;
204388}
204389
204390
204391
204392/*************************************************************************
204393** Start of highlight() implementation.
204394*/
204395typedef struct HighlightContext HighlightContext;
204396struct HighlightContext {
204397 CInstIter iter; /* Coalesced Instance Iterator */
204398 int iPos; /* Current token offset in zIn[] */
204399 int iRangeStart; /* First token to include */
204400 int iRangeEnd; /* If non-zero, last token to include */
204401 const char *zOpen; /* Opening highlight */
204402 const char *zClose; /* Closing highlight */
204403 const char *zIn; /* Input text */
204404 int nIn; /* Size of input text in bytes */
204405 int iOff; /* Current offset within zIn[] */
204406 char *zOut; /* Output value */
204407};
204408
204409/*
204410** Append text to the HighlightContext output string - p->zOut. Argument
204411** z points to a buffer containing n bytes of text to append. If n is
204412** negative, everything up until the first '\0' is appended to the output.
204413**
204414** If *pRc is set to any value other than SQLITE_OK when this function is
204415** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
204416** *pRc is set to an error code before returning.
204417*/
204418static void fts5HighlightAppend(
204419 int *pRc,
204420 HighlightContext *p,
204421 const char *z, int n
204422){
204423 if( *pRc==SQLITE_OK0 && z ){
204424 if( n<0 ) n = (int)strlen(z);
204425 p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
204426 if( p->zOut==0 ) *pRc = SQLITE_NOMEM7;
204427 }
204428}
204429
204430/*
204431** Tokenizer callback used by implementation of highlight() function.
204432*/
204433static int fts5HighlightCb(
204434 void *pContext, /* Pointer to HighlightContext object */
204435 int tflags, /* Mask of FTS5_TOKEN_* flags */
204436 const char *pToken, /* Buffer containing token */
204437 int nToken, /* Size of token in bytes */
204438 int iStartOff, /* Start offset of token */
204439 int iEndOff /* End offset of token */
204440){
204441 HighlightContext *p = (HighlightContext*)pContext;
204442 int rc = SQLITE_OK0;
204443 int iPos;
204444
204445 UNUSED_PARAM2(pToken, nToken);
204446
204447 if( tflags & FTS5_TOKEN_COLOCATED0x0001 ) return SQLITE_OK0;
204448 iPos = p->iPos++;
204449
204450 if( p->iRangeEnd>0 ){
204451 if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK0;
204452 if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
204453 }
204454
204455 if( iPos==p->iter.iStart ){
204456 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
204457 fts5HighlightAppend(&rc, p, p->zOpen, -1);
204458 p->iOff = iStartOff;
204459 }
204460
204461 if( iPos==p->iter.iEnd ){
204462 if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
204463 fts5HighlightAppend(&rc, p, p->zOpen, -1);
204464 }
204465 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
204466 fts5HighlightAppend(&rc, p, p->zClose, -1);
204467 p->iOff = iEndOff;
204468 if( rc==SQLITE_OK0 ){
204469 rc = fts5CInstIterNext(&p->iter);
204470 }
204471 }
204472
204473 if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
204474 fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
204475 p->iOff = iEndOff;
204476 if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
204477 fts5HighlightAppend(&rc, p, p->zClose, -1);
204478 }
204479 }
204480
204481 return rc;
204482}
204483
204484/*
204485** Implementation of highlight() function.
204486*/
204487static void fts5HighlightFunction(
204488 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
204489 Fts5Context *pFts, /* First arg to pass to pApi functions */
204490 sqlite3_context *pCtx, /* Context for returning result/error */
204491 int nVal, /* Number of values in apVal[] array */
204492 sqlite3_value **apVal /* Array of trailing arguments */
204493){
204494 HighlightContext ctx;
204495 int rc;
204496 int iCol;
204497
204498 if( nVal!=3 ){
204499 const char *zErr = "wrong number of arguments to function highlight()";
204500 sqlite3_result_error(pCtx, zErr, -1);
204501 return;
204502 }
204503
204504 iCol = sqlite3_value_int(apVal[0]);
204505 memset(&ctx, 0, sizeof(HighlightContext));
204506 ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
204507 ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
204508 rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
204509
204510 if( ctx.zIn ){
204511 if( rc==SQLITE_OK0 ){
204512 rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
204513 }
204514
204515 if( rc==SQLITE_OK0 ){
204516 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
204517 }
204518 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
204519
204520 if( rc==SQLITE_OK0 ){
204521 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
204522 }
204523 sqlite3_free(ctx.zOut);
204524 }
204525 if( rc!=SQLITE_OK0 ){
204526 sqlite3_result_error_code(pCtx, rc);
204527 }
204528}
204529/*
204530** End of highlight() implementation.
204531**************************************************************************/
204532
204533/*
204534** Context object passed to the fts5SentenceFinderCb() function.
204535*/
204536typedef struct Fts5SFinder Fts5SFinder;
204537struct Fts5SFinder {
204538 int iPos; /* Current token position */
204539 int nFirstAlloc; /* Allocated size of aFirst[] */
204540 int nFirst; /* Number of entries in aFirst[] */
204541 int *aFirst; /* Array of first token in each sentence */
204542 const char *zDoc; /* Document being tokenized */
204543};
204544
204545/*
204546** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
204547** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
204548** error occurs.
204549*/
204550static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
204551 if( p->nFirstAlloc==p->nFirst ){
204552 int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
204553 int *aNew;
204554
204555 aNew = (int*)sqlite3_realloc64(p->aFirst, nNew*sizeof(int));
204556 if( aNew==0 ) return SQLITE_NOMEM7;
204557 p->aFirst = aNew;
204558 p->nFirstAlloc = nNew;
204559 }
204560 p->aFirst[p->nFirst++] = iAdd;
204561 return SQLITE_OK0;
204562}
204563
204564/*
204565** This function is an xTokenize() callback used by the auxiliary snippet()
204566** function. Its job is to identify tokens that are the first in a sentence.
204567** For each such token, an entry is added to the SFinder.aFirst[] array.
204568*/
204569static int fts5SentenceFinderCb(
204570 void *pContext, /* Pointer to HighlightContext object */
204571 int tflags, /* Mask of FTS5_TOKEN_* flags */
204572 const char *pToken, /* Buffer containing token */
204573 int nToken, /* Size of token in bytes */
204574 int iStartOff, /* Start offset of token */
204575 int iEndOff /* End offset of token */
204576){
204577 int rc = SQLITE_OK0;
204578
204579 UNUSED_PARAM2(pToken, nToken);
204580 UNUSED_PARAM(iEndOff);
204581
204582 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ){
204583 Fts5SFinder *p = (Fts5SFinder*)pContext;
204584 if( p->iPos>0 ){
204585 int i;
204586 char c = 0;
204587 for(i=iStartOff-1; i>=0; i--){
204588 c = p->zDoc[i];
204589 if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
204590 }
204591 if( i!=iStartOff-1 && (c=='.' || c==':') ){
204592 rc = fts5SentenceFinderAdd(p, p->iPos);
204593 }
204594 }else{
204595 rc = fts5SentenceFinderAdd(p, 0);
204596 }
204597 p->iPos++;
204598 }
204599 return rc;
204600}
204601
204602static int fts5SnippetScore(
204603 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
204604 Fts5Context *pFts, /* First arg to pass to pApi functions */
204605 int nDocsize, /* Size of column in tokens */
204606 unsigned char *aSeen, /* Array with one element per query phrase */
204607 int iCol, /* Column to score */
204608 int iPos, /* Starting offset to score */
204609 int nToken, /* Max tokens per snippet */
204610 int *pnScore, /* OUT: Score */
204611 int *piPos /* OUT: Adjusted offset */
204612){
204613 int rc;
204614 int i;
204615 int ip = 0;
204616 int ic = 0;
204617 int iOff = 0;
204618 int iFirst = -1;
204619 int nInst;
204620 int nScore = 0;
204621 int iLast = 0;
204622 sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
204623
204624 rc = pApi->xInstCount(pFts, &nInst);
204625 for(i=0; i<nInst && rc==SQLITE_OK0; i++){
204626 rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
204627 if( rc==SQLITE_OK0 && ic==iCol && iOff>=iPos && iOff<iEnd ){
204628 nScore += (aSeen[ip] ? 1 : 1000);
204629 aSeen[ip] = 1;
204630 if( iFirst<0 ) iFirst = iOff;
204631 iLast = iOff + pApi->xPhraseSize(pFts, ip);
204632 }
204633 }
204634
204635 *pnScore = nScore;
204636 if( piPos ){
204637 sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
204638 if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
204639 if( iAdj<0 ) iAdj = 0;
204640 *piPos = (int)iAdj;
204641 }
204642
204643 return rc;
204644}
204645
204646/*
204647** Return the value in pVal interpreted as utf-8 text. Except, if pVal
204648** contains a NULL value, return a pointer to a static string zero
204649** bytes in length instead of a NULL pointer.
204650*/
204651static const char *fts5ValueToText(sqlite3_value *pVal){
204652 const char *zRet = (const char*)sqlite3_value_text(pVal);
204653 return zRet ? zRet : "";
204654}
204655
204656/*
204657** Implementation of snippet() function.
204658*/
204659static void fts5SnippetFunction(
204660 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
204661 Fts5Context *pFts, /* First arg to pass to pApi functions */
204662 sqlite3_context *pCtx, /* Context for returning result/error */
204663 int nVal, /* Number of values in apVal[] array */
204664 sqlite3_value **apVal /* Array of trailing arguments */
204665){
204666 HighlightContext ctx;
204667 int rc = SQLITE_OK0; /* Return code */
204668 int iCol; /* 1st argument to snippet() */
204669 const char *zEllips; /* 4th argument to snippet() */
204670 int nToken; /* 5th argument to snippet() */
204671 int nInst = 0; /* Number of instance matches this row */
204672 int i; /* Used to iterate through instances */
204673 int nPhrase; /* Number of phrases in query */
204674 unsigned char *aSeen; /* Array of "seen instance" flags */
204675 int iBestCol; /* Column containing best snippet */
204676 int iBestStart = 0; /* First token of best snippet */
204677 int nBestScore = 0; /* Score of best snippet */
204678 int nColSize = 0; /* Total size of iBestCol in tokens */
204679 Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
204680 int nCol;
204681
204682 if( nVal!=5 ){
204683 const char *zErr = "wrong number of arguments to function snippet()";
204684 sqlite3_result_error(pCtx, zErr, -1);
204685 return;
204686 }
204687
204688 nCol = pApi->xColumnCount(pFts);
204689 memset(&ctx, 0, sizeof(HighlightContext));
204690 iCol = sqlite3_value_int(apVal[0]);
204691 ctx.zOpen = fts5ValueToText(apVal[1]);
204692 ctx.zClose = fts5ValueToText(apVal[2]);
204693 zEllips = fts5ValueToText(apVal[3]);
204694 nToken = sqlite3_value_int(apVal[4]);
204695
204696 iBestCol = (iCol>=0 ? iCol : 0);
204697 nPhrase = pApi->xPhraseCount(pFts);
204698 aSeen = sqlite3_malloc(nPhrase);
204699 if( aSeen==0 ){
204700 rc = SQLITE_NOMEM7;
204701 }
204702 if( rc==SQLITE_OK0 ){
204703 rc = pApi->xInstCount(pFts, &nInst);
204704 }
204705
204706 memset(&sFinder, 0, sizeof(Fts5SFinder));
204707 for(i=0; i<nCol; i++){
204708 if( iCol<0 || iCol==i ){
204709 int nDoc;
204710 int nDocsize;
204711 int ii;
204712 sFinder.iPos = 0;
204713 sFinder.nFirst = 0;
204714 rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
204715 if( rc!=SQLITE_OK0 ) break;
204716 rc = pApi->xTokenize(pFts,
204717 sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
204718 );
204719 if( rc!=SQLITE_OK0 ) break;
204720 rc = pApi->xColumnSize(pFts, i, &nDocsize);
204721 if( rc!=SQLITE_OK0 ) break;
204722
204723 for(ii=0; rc==SQLITE_OK0 && ii<nInst; ii++){
204724 int ip, ic, io;
204725 int iAdj;
204726 int nScore;
204727 int jj;
204728
204729 rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
204730 if( ic!=i ) continue;
204731 if( io>nDocsize ) rc = FTS5_CORRUPT;
204732 if( rc!=SQLITE_OK0 ) continue;
204733 memset(aSeen, 0, nPhrase);
204734 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
204735 io, nToken, &nScore, &iAdj
204736 );
204737 if( rc==SQLITE_OK0 && nScore>nBestScore ){
204738 nBestScore = nScore;
204739 iBestCol = i;
204740 iBestStart = iAdj;
204741 nColSize = nDocsize;
204742 }
204743
204744 if( rc==SQLITE_OK0 && sFinder.nFirst && nDocsize>nToken ){
204745 for(jj=0; jj<(sFinder.nFirst-1); jj++){
204746 if( sFinder.aFirst[jj+1]>io ) break;
204747 }
204748
204749 if( sFinder.aFirst[jj]<io ){
204750 memset(aSeen, 0, nPhrase);
204751 rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
204752 sFinder.aFirst[jj], nToken, &nScore, 0
204753 );
204754
204755 nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
204756 if( rc==SQLITE_OK0 && nScore>nBestScore ){
204757 nBestScore = nScore;
204758 iBestCol = i;
204759 iBestStart = sFinder.aFirst[jj];
204760 nColSize = nDocsize;
204761 }
204762 }
204763 }
204764 }
204765 }
204766 }
204767
204768 if( rc==SQLITE_OK0 ){
204769 rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
204770 }
204771 if( rc==SQLITE_OK0 && nColSize==0 ){
204772 rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
204773 }
204774 if( ctx.zIn ){
204775 if( rc==SQLITE_OK0 ){
204776 rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
204777 }
204778
204779 ctx.iRangeStart = iBestStart;
204780 ctx.iRangeEnd = iBestStart + nToken - 1;
204781
204782 if( iBestStart>0 ){
204783 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
204784 }
204785
204786 /* Advance iterator ctx.iter so that it points to the first coalesced
204787 ** phrase instance at or following position iBestStart. */
204788 while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK0 ){
204789 rc = fts5CInstIterNext(&ctx.iter);
204790 }
204791
204792 if( rc==SQLITE_OK0 ){
204793 rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
204794 }
204795 if( ctx.iRangeEnd>=(nColSize-1) ){
204796 fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
204797 }else{
204798 fts5HighlightAppend(&rc, &ctx, zEllips, -1);
204799 }
204800 }
204801 if( rc==SQLITE_OK0 ){
204802 sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
204803 }else{
204804 sqlite3_result_error_code(pCtx, rc);
204805 }
204806 sqlite3_free(ctx.zOut);
204807 sqlite3_free(aSeen);
204808 sqlite3_free(sFinder.aFirst);
204809}
204810
204811/************************************************************************/
204812
204813/*
204814** The first time the bm25() function is called for a query, an instance
204815** of the following structure is allocated and populated.
204816*/
204817typedef struct Fts5Bm25Data Fts5Bm25Data;
204818struct Fts5Bm25Data {
204819 int nPhrase; /* Number of phrases in query */
204820 double avgdl; /* Average number of tokens in each row */
204821 double *aIDF; /* IDF for each phrase */
204822 double *aFreq; /* Array used to calculate phrase freq. */
204823};
204824
204825/*
204826** Callback used by fts5Bm25GetData() to count the number of rows in the
204827** table matched by each individual phrase within the query.
204828*/
204829static int fts5CountCb(
204830 const Fts5ExtensionApi *pApi,
204831 Fts5Context *pFts,
204832 void *pUserData /* Pointer to sqlite3_int64 variable */
204833){
204834 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
204835 UNUSED_PARAM2(pApi, pFts);
204836 (*pn)++;
204837 return SQLITE_OK0;
204838}
204839
204840/*
204841** Set *ppData to point to the Fts5Bm25Data object for the current query.
204842** If the object has not already been allocated, allocate and populate it
204843** now.
204844*/
204845static int fts5Bm25GetData(
204846 const Fts5ExtensionApi *pApi,
204847 Fts5Context *pFts,
204848 Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
204849){
204850 int rc = SQLITE_OK0; /* Return code */
204851 Fts5Bm25Data *p; /* Object to return */
204852
204853 p = pApi->xGetAuxdata(pFts, 0);
204854 if( p==0 ){
204855 int nPhrase; /* Number of phrases in query */
204856 sqlite3_int64 nRow = 0; /* Number of rows in table */
204857 sqlite3_int64 nToken = 0; /* Number of tokens in table */
204858 sqlite3_int64 nByte; /* Bytes of space to allocate */
204859 int i;
204860
204861 /* Allocate the Fts5Bm25Data object */
204862 nPhrase = pApi->xPhraseCount(pFts);
204863 nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
204864 p = (Fts5Bm25Data*)sqlite3_malloc64(nByte);
204865 if( p==0 ){
204866 rc = SQLITE_NOMEM7;
204867 }else{
204868 memset(p, 0, (size_t)nByte);
204869 p->nPhrase = nPhrase;
204870 p->aIDF = (double*)&p[1];
204871 p->aFreq = &p->aIDF[nPhrase];
204872 }
204873
204874 /* Calculate the average document length for this FTS5 table */
204875 if( rc==SQLITE_OK0 ) rc = pApi->xRowCount(pFts, &nRow);
204876 assert( rc!=SQLITE_OK || nRow>0 )((void) (0));
204877 if( rc==SQLITE_OK0 ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
204878 if( rc==SQLITE_OK0 ) p->avgdl = (double)nToken / (double)nRow;
204879
204880 /* Calculate an IDF for each phrase in the query */
204881 for(i=0; rc==SQLITE_OK0 && i<nPhrase; i++){
204882 sqlite3_int64 nHit = 0;
204883 rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
204884 if( rc==SQLITE_OK0 ){
204885 /* Calculate the IDF (Inverse Document Frequency) for phrase i.
204886 ** This is done using the standard BM25 formula as found on wikipedia:
204887 **
204888 ** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
204889 **
204890 ** where "N" is the total number of documents in the set and nHit
204891 ** is the number that contain at least one instance of the phrase
204892 ** under consideration.
204893 **
204894 ** The problem with this is that if (N < 2*nHit), the IDF is
204895 ** negative. Which is undesirable. So the mimimum allowable IDF is
204896 ** (1e-6) - roughly the same as a term that appears in just over
204897 ** half of set of 5,000,000 documents. */
204898 double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
204899 if( idf<=0.0 ) idf = 1e-6;
204900 p->aIDF[i] = idf;
204901 }
204902 }
204903
204904 if( rc!=SQLITE_OK0 ){
204905 sqlite3_free(p);
204906 }else{
204907 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
204908 }
204909 if( rc!=SQLITE_OK0 ) p = 0;
204910 }
204911 *ppData = p;
204912 return rc;
204913}
204914
204915/*
204916** Implementation of bm25() function.
204917*/
204918static void fts5Bm25Function(
204919 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
204920 Fts5Context *pFts, /* First arg to pass to pApi functions */
204921 sqlite3_context *pCtx, /* Context for returning result/error */
204922 int nVal, /* Number of values in apVal[] array */
204923 sqlite3_value **apVal /* Array of trailing arguments */
204924){
204925 const double k1 = 1.2; /* Constant "k1" from BM25 formula */
204926 const double b = 0.75; /* Constant "b" from BM25 formula */
204927 int rc = SQLITE_OK0; /* Error code */
204928 double score = 0.0; /* SQL function return value */
204929 Fts5Bm25Data *pData; /* Values allocated/calculated once only */
204930 int i; /* Iterator variable */
204931 int nInst = 0; /* Value returned by xInstCount() */
204932 double D = 0.0; /* Total number of tokens in row */
204933 double *aFreq = 0; /* Array of phrase freq. for current row */
204934
204935 /* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
204936 ** for each phrase in the query for the current row. */
204937 rc = fts5Bm25GetData(pApi, pFts, &pData);
204938 if( rc==SQLITE_OK0 ){
204939 aFreq = pData->aFreq;
204940 memset(aFreq, 0, sizeof(double) * pData->nPhrase);
204941 rc = pApi->xInstCount(pFts, &nInst);
204942 }
204943 for(i=0; rc==SQLITE_OK0 && i<nInst; i++){
204944 int ip; int ic; int io;
204945 rc = pApi->xInst(pFts, i, &ip, &ic, &io);
204946 if( rc==SQLITE_OK0 ){
204947 double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
204948 aFreq[ip] += w;
204949 }
204950 }
204951
204952 /* Figure out the total size of the current row in tokens. */
204953 if( rc==SQLITE_OK0 ){
204954 int nTok;
204955 rc = pApi->xColumnSize(pFts, -1, &nTok);
204956 D = (double)nTok;
204957 }
204958
204959 /* Determine the BM25 score for the current row. */
204960 for(i=0; rc==SQLITE_OK0 && i<pData->nPhrase; i++){
204961 score += pData->aIDF[i] * (
204962 ( aFreq[i] * (k1 + 1.0) ) /
204963 ( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
204964 );
204965 }
204966
204967 /* If no error has occurred, return the calculated score. Otherwise,
204968 ** throw an SQL exception. */
204969 if( rc==SQLITE_OK0 ){
204970 sqlite3_result_double(pCtx, -1.0 * score);
204971 }else{
204972 sqlite3_result_error_code(pCtx, rc);
204973 }
204974}
204975
204976static int sqlite3Fts5AuxInit(fts5_api *pApi){
204977 struct Builtin {
204978 const char *zFunc; /* Function name (nul-terminated) */
204979 void *pUserData; /* User-data pointer */
204980 fts5_extension_function xFunc;/* Callback function */
204981 void (*xDestroy)(void*); /* Destructor function */
204982 } aBuiltin [] = {
204983 { "snippet", 0, fts5SnippetFunction, 0 },
204984 { "highlight", 0, fts5HighlightFunction, 0 },
204985 { "bm25", 0, fts5Bm25Function, 0 },
204986 };
204987 int rc = SQLITE_OK0; /* Return code */
204988 int i; /* To iterate through builtin functions */
204989
204990 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aBuiltin)((int)(sizeof(aBuiltin)/sizeof(aBuiltin[0]))); i++){
204991 rc = pApi->xCreateFunction(pApi,
204992 aBuiltin[i].zFunc,
204993 aBuiltin[i].pUserData,
204994 aBuiltin[i].xFunc,
204995 aBuiltin[i].xDestroy
204996 );
204997 }
204998
204999 return rc;
205000}
205001
205002/*
205003** 2014 May 31
205004**
205005** The author disclaims copyright to this source code. In place of
205006** a legal notice, here is a blessing:
205007**
205008** May you do good and not evil.
205009** May you find forgiveness for yourself and forgive others.
205010** May you share freely, never taking more than you give.
205011**
205012******************************************************************************
205013*/
205014
205015
205016
205017/* #include "fts5Int.h" */
205018
205019static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
205020 if( (u32)pBuf->nSpace<nByte ){
205021 u64 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
205022 u8 *pNew;
205023 while( nNew<nByte ){
205024 nNew = nNew * 2;
205025 }
205026 pNew = sqlite3_realloc64(pBuf->p, nNew);
205027 if( pNew==0 ){
205028 *pRc = SQLITE_NOMEM7;
205029 return 1;
205030 }else{
205031 pBuf->nSpace = (int)nNew;
205032 pBuf->p = pNew;
205033 }
205034 }
205035 return 0;
205036}
205037
205038
205039/*
205040** Encode value iVal as an SQLite varint and append it to the buffer object
205041** pBuf. If an OOM error occurs, set the error code in p.
205042*/
205043static void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){
205044 if( fts5BufferGrow(pRc, pBuf, 9) ) return;
205045 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal);
205046}
205047
205048static void sqlite3Fts5Put32(u8 *aBuf, int iVal){
205049 aBuf[0] = (iVal>>24) & 0x00FF;
205050 aBuf[1] = (iVal>>16) & 0x00FF;
205051 aBuf[2] = (iVal>> 8) & 0x00FF;
205052 aBuf[3] = (iVal>> 0) & 0x00FF;
205053}
205054
205055static int sqlite3Fts5Get32(const u8 *aBuf){
205056 return (int)((((u32)aBuf[0])<<24) + (aBuf[1]<<16) + (aBuf[2]<<8) + aBuf[3]);
205057}
205058
205059/*
205060** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set
205061** the error code in p. If an error has already occurred when this function
205062** is called, it is a no-op.
205063*/
205064static void sqlite3Fts5BufferAppendBlob(
205065 int *pRc,
205066 Fts5Buffer *pBuf,
205067 u32 nData,
205068 const u8 *pData
205069){
205070 assert_nc( *pRc || nData>=0 );
205071 if( nData ){
205072 if( fts5BufferGrow(pRc, pBuf, nData) ) return;
205073 memcpy(&pBuf->p[pBuf->n], pData, nData);
205074 pBuf->n += nData;
205075 }
205076}
205077
205078/*
205079** Append the nul-terminated string zStr to the buffer pBuf. This function
205080** ensures that the byte following the buffer data is set to 0x00, even
205081** though this byte is not included in the pBuf->n count.
205082*/
205083static void sqlite3Fts5BufferAppendString(
205084 int *pRc,
205085 Fts5Buffer *pBuf,
205086 const char *zStr
205087){
205088 int nStr = (int)strlen(zStr);
205089 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr);
205090 pBuf->n--;
205091}
205092
205093/*
205094** Argument zFmt is a printf() style format string. This function performs
205095** the printf() style processing, then appends the results to buffer pBuf.
205096**
205097** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte
205098** following the buffer data is set to 0x00, even though this byte is not
205099** included in the pBuf->n count.
205100*/
205101static void sqlite3Fts5BufferAppendPrintf(
205102 int *pRc,
205103 Fts5Buffer *pBuf,
205104 char *zFmt, ...
205105){
205106 if( *pRc==SQLITE_OK0 ){
205107 char *zTmp;
205108 va_list ap;
205109 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
205110 zTmp = sqlite3_vmprintf(zFmt, ap);
205111 va_end(ap)__builtin_va_end(ap);
205112
205113 if( zTmp==0 ){
205114 *pRc = SQLITE_NOMEM7;
205115 }else{
205116 sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp);
205117 sqlite3_free(zTmp);
205118 }
205119 }
205120}
205121
205122static char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){
205123 char *zRet = 0;
205124 if( *pRc==SQLITE_OK0 ){
205125 va_list ap;
205126 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
205127 zRet = sqlite3_vmprintf(zFmt, ap);
205128 va_end(ap)__builtin_va_end(ap);
205129 if( zRet==0 ){
205130 *pRc = SQLITE_NOMEM7;
205131 }
205132 }
205133 return zRet;
205134}
205135
205136
205137/*
205138** Free any buffer allocated by pBuf. Zero the structure before returning.
205139*/
205140static void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){
205141 sqlite3_free(pBuf->p);
205142 memset(pBuf, 0, sizeof(Fts5Buffer));
205143}
205144
205145/*
205146** Zero the contents of the buffer object. But do not free the associated
205147** memory allocation.
205148*/
205149static void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){
205150 pBuf->n = 0;
205151}
205152
205153/*
205154** Set the buffer to contain nData/pData. If an OOM error occurs, leave an
205155** the error code in p. If an error has already occurred when this function
205156** is called, it is a no-op.
205157*/
205158static void sqlite3Fts5BufferSet(
205159 int *pRc,
205160 Fts5Buffer *pBuf,
205161 int nData,
205162 const u8 *pData
205163){
205164 pBuf->n = 0;
205165 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData);
205166}
205167
205168static int sqlite3Fts5PoslistNext64(
205169 const u8 *a, int n, /* Buffer containing poslist */
205170 int *pi, /* IN/OUT: Offset within a[] */
205171 i64 *piOff /* IN/OUT: Current offset */
205172){
205173 int i = *pi;
205174 if( i>=n ){
205175 /* EOF */
205176 *piOff = -1;
205177 return 1;
205178 }else{
205179 i64 iOff = *piOff;
205180 int iVal;
205181 fts5FastGetVarint32(a, i, iVal);
205182 if( iVal<=1 ){
205183 if( iVal==0 ){
205184 *pi = i;
205185 return 0;
205186 }
205187 fts5FastGetVarint32(a, i, iVal);
205188 iOff = ((i64)iVal) << 32;
205189 fts5FastGetVarint32(a, i, iVal);
205190 if( iVal<2 ){
205191 /* This is a corrupt record. So stop parsing it here. */
205192 *piOff = -1;
205193 return 1;
205194 }
205195 }
205196 *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
205197 *pi = i;
205198 return 0;
205199 }
205200}
205201
205202
205203/*
205204** Advance the iterator object passed as the only argument. Return true
205205** if the iterator reaches EOF, or false otherwise.
205206*/
205207static int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){
205208 if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){
205209 pIter->bEof = 1;
205210 }
205211 return pIter->bEof;
205212}
205213
205214static int sqlite3Fts5PoslistReaderInit(
205215 const u8 *a, int n, /* Poslist buffer to iterate through */
205216 Fts5PoslistReader *pIter /* Iterator object to initialize */
205217){
205218 memset(pIter, 0, sizeof(*pIter));
205219 pIter->a = a;
205220 pIter->n = n;
205221 sqlite3Fts5PoslistReaderNext(pIter);
205222 return pIter->bEof;
205223}
205224
205225/*
205226** Append position iPos to the position list being accumulated in buffer
205227** pBuf, which must be already be large enough to hold the new data.
205228** The previous position written to this list is *piPrev. *piPrev is set
205229** to iPos before returning.
205230*/
205231static void sqlite3Fts5PoslistSafeAppend(
205232 Fts5Buffer *pBuf,
205233 i64 *piPrev,
205234 i64 iPos
205235){
205236 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
205237 if( (iPos & colmask) != (*piPrev & colmask) ){
205238 pBuf->p[pBuf->n++] = 1;
205239 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
205240 *piPrev = (iPos & colmask);
205241 }
205242 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
205243 *piPrev = iPos;
205244}
205245
205246static int sqlite3Fts5PoslistWriterAppend(
205247 Fts5Buffer *pBuf,
205248 Fts5PoslistWriter *pWriter,
205249 i64 iPos
205250){
205251 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
205252 if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
205253 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
205254 return SQLITE_OK0;
205255}
205256
205257static void *sqlite3Fts5MallocZero(int *pRc, sqlite3_int64 nByte){
205258 void *pRet = 0;
205259 if( *pRc==SQLITE_OK0 ){
205260 pRet = sqlite3_malloc64(nByte);
205261 if( pRet==0 ){
205262 if( nByte>0 ) *pRc = SQLITE_NOMEM7;
205263 }else{
205264 memset(pRet, 0, (size_t)nByte);
205265 }
205266 }
205267 return pRet;
205268}
205269
205270/*
205271** Return a nul-terminated copy of the string indicated by pIn. If nIn
205272** is non-negative, then it is the length of the string in bytes. Otherwise,
205273** the length of the string is determined using strlen().
205274**
205275** It is the responsibility of the caller to eventually free the returned
205276** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned.
205277*/
205278static char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){
205279 char *zRet = 0;
205280 if( *pRc==SQLITE_OK0 ){
205281 if( nIn<0 ){
205282 nIn = (int)strlen(pIn);
205283 }
205284 zRet = (char*)sqlite3_malloc(nIn+1);
205285 if( zRet ){
205286 memcpy(zRet, pIn, nIn);
205287 zRet[nIn] = '\0';
205288 }else{
205289 *pRc = SQLITE_NOMEM7;
205290 }
205291 }
205292 return zRet;
205293}
205294
205295
205296/*
205297** Return true if character 't' may be part of an FTS5 bareword, or false
205298** otherwise. Characters that may be part of barewords:
205299**
205300** * All non-ASCII characters,
205301** * The 52 upper and lower case ASCII characters, and
205302** * The 10 integer ASCII characters.
205303** * The underscore character "_" (0x5F).
205304** * The unicode "subsitute" character (0x1A).
205305*/
205306static int sqlite3Fts5IsBareword(char t){
205307 u8 aBareword[128] = {
205308 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */
205309 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */
205310 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */
205311 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */
205312 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */
205313 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */
205314 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */
205315 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */
205316 };
205317
205318 return (t & 0x80) || aBareword[(int)t];
205319}
205320
205321
205322/*************************************************************************
205323*/
205324typedef struct Fts5TermsetEntry Fts5TermsetEntry;
205325struct Fts5TermsetEntry {
205326 char *pTerm;
205327 int nTerm;
205328 int iIdx; /* Index (main or aPrefix[] entry) */
205329 Fts5TermsetEntry *pNext;
205330};
205331
205332struct Fts5Termset {
205333 Fts5TermsetEntry *apHash[512];
205334};
205335
205336static int sqlite3Fts5TermsetNew(Fts5Termset **pp){
205337 int rc = SQLITE_OK0;
205338 *pp = sqlite3Fts5MallocZero(&rc, sizeof(Fts5Termset));
205339 return rc;
205340}
205341
205342static int sqlite3Fts5TermsetAdd(
205343 Fts5Termset *p,
205344 int iIdx,
205345 const char *pTerm, int nTerm,
205346 int *pbPresent
205347){
205348 int rc = SQLITE_OK0;
205349 *pbPresent = 0;
205350 if( p ){
205351 int i;
205352 u32 hash = 13;
205353 Fts5TermsetEntry *pEntry;
205354
205355 /* Calculate a hash value for this term. This is the same hash checksum
205356 ** used by the fts5_hash.c module. This is not important for correct
205357 ** operation of the module, but is necessary to ensure that some tests
205358 ** designed to produce hash table collisions really do work. */
205359 for(i=nTerm-1; i>=0; i--){
205360 hash = (hash << 3) ^ hash ^ pTerm[i];
205361 }
205362 hash = (hash << 3) ^ hash ^ iIdx;
205363 hash = hash % ArraySize(p->apHash)((int)(sizeof(p->apHash)/sizeof(p->apHash[0])));
205364
205365 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
205366 if( pEntry->iIdx==iIdx
205367 && pEntry->nTerm==nTerm
205368 && memcmp(pEntry->pTerm, pTerm, nTerm)==0
205369 ){
205370 *pbPresent = 1;
205371 break;
205372 }
205373 }
205374
205375 if( pEntry==0 ){
205376 pEntry = sqlite3Fts5MallocZero(&rc, sizeof(Fts5TermsetEntry) + nTerm);
205377 if( pEntry ){
205378 pEntry->pTerm = (char*)&pEntry[1];
205379 pEntry->nTerm = nTerm;
205380 pEntry->iIdx = iIdx;
205381 memcpy(pEntry->pTerm, pTerm, nTerm);
205382 pEntry->pNext = p->apHash[hash];
205383 p->apHash[hash] = pEntry;
205384 }
205385 }
205386 }
205387
205388 return rc;
205389}
205390
205391static void sqlite3Fts5TermsetFree(Fts5Termset *p){
205392 if( p ){
205393 u32 i;
205394 for(i=0; i<ArraySize(p->apHash)((int)(sizeof(p->apHash)/sizeof(p->apHash[0]))); i++){
205395 Fts5TermsetEntry *pEntry = p->apHash[i];
205396 while( pEntry ){
205397 Fts5TermsetEntry *pDel = pEntry;
205398 pEntry = pEntry->pNext;
205399 sqlite3_free(pDel);
205400 }
205401 }
205402 sqlite3_free(p);
205403 }
205404}
205405
205406/*
205407** 2014 Jun 09
205408**
205409** The author disclaims copyright to this source code. In place of
205410** a legal notice, here is a blessing:
205411**
205412** May you do good and not evil.
205413** May you find forgiveness for yourself and forgive others.
205414** May you share freely, never taking more than you give.
205415**
205416******************************************************************************
205417**
205418** This is an SQLite module implementing full-text search.
205419*/
205420
205421
205422/* #include "fts5Int.h" */
205423
205424#define FTS5_DEFAULT_PAGE_SIZE 4050
205425#define FTS5_DEFAULT_AUTOMERGE 4
205426#define FTS5_DEFAULT_USERMERGE 4
205427#define FTS5_DEFAULT_CRISISMERGE 16
205428#define FTS5_DEFAULT_HASHSIZE (1024*1024)
205429
205430/* Maximum allowed page size */
205431#define FTS5_MAX_PAGE_SIZE (128*1024)
205432
205433static int fts5_iswhitespace(char x){
205434 return (x==' ');
205435}
205436
205437static int fts5_isopenquote(char x){
205438 return (x=='"' || x=='\'' || x=='[' || x=='`');
205439}
205440
205441/*
205442** Argument pIn points to a character that is part of a nul-terminated
205443** string. Return a pointer to the first character following *pIn in
205444** the string that is not a white-space character.
205445*/
205446static const char *fts5ConfigSkipWhitespace(const char *pIn){
205447 const char *p = pIn;
205448 if( p ){
205449 while( fts5_iswhitespace(*p) ){ p++; }
205450 }
205451 return p;
205452}
205453
205454/*
205455** Argument pIn points to a character that is part of a nul-terminated
205456** string. Return a pointer to the first character following *pIn in
205457** the string that is not a "bareword" character.
205458*/
205459static const char *fts5ConfigSkipBareword(const char *pIn){
205460 const char *p = pIn;
205461 while ( sqlite3Fts5IsBareword(*p) ) p++;
205462 if( p==pIn ) p = 0;
205463 return p;
205464}
205465
205466static int fts5_isdigit(char a){
205467 return (a>='0' && a<='9');
205468}
205469
205470
205471
205472static const char *fts5ConfigSkipLiteral(const char *pIn){
205473 const char *p = pIn;
205474 switch( *p ){
205475 case 'n': case 'N':
205476 if( sqlite3_strnicmp("null", p, 4)==0 ){
205477 p = &p[4];
205478 }else{
205479 p = 0;
205480 }
205481 break;
205482
205483 case 'x': case 'X':
205484 p++;
205485 if( *p=='\'' ){
205486 p++;
205487 while( (*p>='a' && *p<='f')
205488 || (*p>='A' && *p<='F')
205489 || (*p>='0' && *p<='9')
205490 ){
205491 p++;
205492 }
205493 if( *p=='\'' && 0==((p-pIn)%2) ){
205494 p++;
205495 }else{
205496 p = 0;
205497 }
205498 }else{
205499 p = 0;
205500 }
205501 break;
205502
205503 case '\'':
205504 p++;
205505 while( p ){
205506 if( *p=='\'' ){
205507 p++;
205508 if( *p!='\'' ) break;
205509 }
205510 p++;
205511 if( *p==0 ) p = 0;
205512 }
205513 break;
205514
205515 default:
205516 /* maybe a number */
205517 if( *p=='+' || *p=='-' ) p++;
205518 while( fts5_isdigit(*p) ) p++;
205519
205520 /* At this point, if the literal was an integer, the parse is
205521 ** finished. Or, if it is a floating point value, it may continue
205522 ** with either a decimal point or an 'E' character. */
205523 if( *p=='.' && fts5_isdigit(p[1]) ){
205524 p += 2;
205525 while( fts5_isdigit(*p) ) p++;
205526 }
205527 if( p==pIn ) p = 0;
205528
205529 break;
205530 }
205531
205532 return p;
205533}
205534
205535/*
205536** The first character of the string pointed to by argument z is guaranteed
205537** to be an open-quote character (see function fts5_isopenquote()).
205538**
205539** This function searches for the corresponding close-quote character within
205540** the string and, if found, dequotes the string in place and adds a new
205541** nul-terminator byte.
205542**
205543** If the close-quote is found, the value returned is the byte offset of
205544** the character immediately following it. Or, if the close-quote is not
205545** found, -1 is returned. If -1 is returned, the buffer is left in an
205546** undefined state.
205547*/
205548static int fts5Dequote(char *z){
205549 char q;
205550 int iIn = 1;
205551 int iOut = 0;
205552 q = z[0];
205553
205554 /* Set stack variable q to the close-quote character */
205555 assert( q=='[' || q=='\'' || q=='"' || q=='`' )((void) (0));
205556 if( q=='[' ) q = ']';
205557
205558 while( ALWAYS(z[iIn])(z[iIn]) ){
205559 if( z[iIn]==q ){
205560 if( z[iIn+1]!=q ){
205561 /* Character iIn was the close quote. */
205562 iIn++;
205563 break;
205564 }else{
205565 /* Character iIn and iIn+1 form an escaped quote character. Skip
205566 ** the input cursor past both and copy a single quote character
205567 ** to the output buffer. */
205568 iIn += 2;
205569 z[iOut++] = q;
205570 }
205571 }else{
205572 z[iOut++] = z[iIn++];
205573 }
205574 }
205575
205576 z[iOut] = '\0';
205577 return iIn;
205578}
205579
205580/*
205581** Convert an SQL-style quoted string into a normal string by removing
205582** the quote characters. The conversion is done in-place. If the
205583** input does not begin with a quote character, then this routine
205584** is a no-op.
205585**
205586** Examples:
205587**
205588** "abc" becomes abc
205589** 'xyz' becomes xyz
205590** [pqr] becomes pqr
205591** `mno` becomes mno
205592*/
205593static void sqlite3Fts5Dequote(char *z){
205594 char quote; /* Quote character (if any ) */
205595
205596 assert( 0==fts5_iswhitespace(z[0]) )((void) (0));
205597 quote = z[0];
205598 if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
205599 fts5Dequote(z);
205600 }
205601}
205602
205603
205604struct Fts5Enum {
205605 const char *zName;
205606 int eVal;
205607};
205608typedef struct Fts5Enum Fts5Enum;
205609
205610static int fts5ConfigSetEnum(
205611 const Fts5Enum *aEnum,
205612 const char *zEnum,
205613 int *peVal
205614){
205615 int nEnum = (int)strlen(zEnum);
205616 int i;
205617 int iVal = -1;
205618
205619 for(i=0; aEnum[i].zName; i++){
205620 if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
205621 if( iVal>=0 ) return SQLITE_ERROR1;
205622 iVal = aEnum[i].eVal;
205623 }
205624 }
205625
205626 *peVal = iVal;
205627 return iVal<0 ? SQLITE_ERROR1 : SQLITE_OK0;
205628}
205629
205630/*
205631** Parse a "special" CREATE VIRTUAL TABLE directive and update
205632** configuration object pConfig as appropriate.
205633**
205634** If successful, object pConfig is updated and SQLITE_OK returned. If
205635** an error occurs, an SQLite error code is returned and an error message
205636** may be left in *pzErr. It is the responsibility of the caller to
205637** eventually free any such error message using sqlite3_free().
205638*/
205639static int fts5ConfigParseSpecial(
205640 Fts5Global *pGlobal,
205641 Fts5Config *pConfig, /* Configuration object to update */
205642 const char *zCmd, /* Special command to parse */
205643 const char *zArg, /* Argument to parse */
205644 char **pzErr /* OUT: Error message */
205645){
205646 int rc = SQLITE_OK0;
205647 int nCmd = (int)strlen(zCmd);
205648 if( sqlite3_strnicmp("prefix", zCmd, nCmd)==0 ){
205649 const int nByte = sizeof(int) * FTS5_MAX_PREFIX_INDEXES;
205650 const char *p;
205651 int bFirst = 1;
205652 if( pConfig->aPrefix==0 ){
205653 pConfig->aPrefix = sqlite3Fts5MallocZero(&rc, nByte);
205654 if( rc ) return rc;
205655 }
205656
205657 p = zArg;
205658 while( 1 ){
205659 int nPre = 0;
205660
205661 while( p[0]==' ' ) p++;
205662 if( bFirst==0 && p[0]==',' ){
205663 p++;
205664 while( p[0]==' ' ) p++;
205665 }else if( p[0]=='\0' ){
205666 break;
205667 }
205668 if( p[0]<'0' || p[0]>'9' ){
205669 *pzErr = sqlite3_mprintf("malformed prefix=... directive");
205670 rc = SQLITE_ERROR1;
205671 break;
205672 }
205673
205674 if( pConfig->nPrefix==FTS5_MAX_PREFIX_INDEXES ){
205675 *pzErr = sqlite3_mprintf(
205676 "too many prefix indexes (max %d)", FTS5_MAX_PREFIX_INDEXES
205677 );
205678 rc = SQLITE_ERROR1;
205679 break;
205680 }
205681
205682 while( p[0]>='0' && p[0]<='9' && nPre<1000 ){
205683 nPre = nPre*10 + (p[0] - '0');
205684 p++;
205685 }
205686
205687 if( nPre<=0 || nPre>=1000 ){
205688 *pzErr = sqlite3_mprintf("prefix length out of range (max 999)");
205689 rc = SQLITE_ERROR1;
205690 break;
205691 }
205692
205693 pConfig->aPrefix[pConfig->nPrefix] = nPre;
205694 pConfig->nPrefix++;
205695 bFirst = 0;
205696 }
205697 assert( pConfig->nPrefix<=FTS5_MAX_PREFIX_INDEXES )((void) (0));
205698 return rc;
205699 }
205700
205701 if( sqlite3_strnicmp("tokenize", zCmd, nCmd)==0 ){
205702 const char *p = (const char*)zArg;
205703 sqlite3_int64 nArg = strlen(zArg) + 1;
205704 char **azArg = sqlite3Fts5MallocZero(&rc, sizeof(char*) * nArg);
205705 char *pDel = sqlite3Fts5MallocZero(&rc, nArg * 2);
205706 char *pSpace = pDel;
205707
205708 if( azArg && pSpace ){
205709 if( pConfig->pTok ){
205710 *pzErr = sqlite3_mprintf("multiple tokenize=... directives");
205711 rc = SQLITE_ERROR1;
205712 }else{
205713 for(nArg=0; p && *p; nArg++){
205714 const char *p2 = fts5ConfigSkipWhitespace(p);
205715 if( *p2=='\'' ){
205716 p = fts5ConfigSkipLiteral(p2);
205717 }else{
205718 p = fts5ConfigSkipBareword(p2);
205719 }
205720 if( p ){
205721 memcpy(pSpace, p2, p-p2);
205722 azArg[nArg] = pSpace;
205723 sqlite3Fts5Dequote(pSpace);
205724 pSpace += (p - p2) + 1;
205725 p = fts5ConfigSkipWhitespace(p);
205726 }
205727 }
205728 if( p==0 ){
205729 *pzErr = sqlite3_mprintf("parse error in tokenize directive");
205730 rc = SQLITE_ERROR1;
205731 }else{
205732 rc = sqlite3Fts5GetTokenizer(pGlobal,
205733 (const char**)azArg, (int)nArg, &pConfig->pTok, &pConfig->pTokApi,
205734 pzErr
205735 );
205736 }
205737 }
205738 }
205739
205740 sqlite3_free(azArg);
205741 sqlite3_free(pDel);
205742 return rc;
205743 }
205744
205745 if( sqlite3_strnicmp("content", zCmd, nCmd)==0 ){
205746 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
205747 *pzErr = sqlite3_mprintf("multiple content=... directives");
205748 rc = SQLITE_ERROR1;
205749 }else{
205750 if( zArg[0] ){
205751 pConfig->eContent = FTS5_CONTENT_EXTERNAL;
205752 pConfig->zContent = sqlite3Fts5Mprintf(&rc, "%Q.%Q", pConfig->zDb,zArg);
205753 }else{
205754 pConfig->eContent = FTS5_CONTENT_NONE;
205755 }
205756 }
205757 return rc;
205758 }
205759
205760 if( sqlite3_strnicmp("content_rowid", zCmd, nCmd)==0 ){
205761 if( pConfig->zContentRowid ){
205762 *pzErr = sqlite3_mprintf("multiple content_rowid=... directives");
205763 rc = SQLITE_ERROR1;
205764 }else{
205765 pConfig->zContentRowid = sqlite3Fts5Strndup(&rc, zArg, -1);
205766 }
205767 return rc;
205768 }
205769
205770 if( sqlite3_strnicmp("columnsize", zCmd, nCmd)==0 ){
205771 if( (zArg[0]!='0' && zArg[0]!='1') || zArg[1]!='\0' ){
205772 *pzErr = sqlite3_mprintf("malformed columnsize=... directive");
205773 rc = SQLITE_ERROR1;
205774 }else{
205775 pConfig->bColumnsize = (zArg[0]=='1');
205776 }
205777 return rc;
205778 }
205779
205780 if( sqlite3_strnicmp("detail", zCmd, nCmd)==0 ){
205781 const Fts5Enum aDetail[] = {
205782 { "none", FTS5_DETAIL_NONE },
205783 { "full", FTS5_DETAIL_FULL },
205784 { "columns", FTS5_DETAIL_COLUMNS },
205785 { 0, 0 }
205786 };
205787
205788 if( (rc = fts5ConfigSetEnum(aDetail, zArg, &pConfig->eDetail)) ){
205789 *pzErr = sqlite3_mprintf("malformed detail=... directive");
205790 }
205791 return rc;
205792 }
205793
205794 *pzErr = sqlite3_mprintf("unrecognized option: \"%.*s\"", nCmd, zCmd);
205795 return SQLITE_ERROR1;
205796}
205797
205798/*
205799** Allocate an instance of the default tokenizer ("simple") at
205800** Fts5Config.pTokenizer. Return SQLITE_OK if successful, or an SQLite error
205801** code if an error occurs.
205802*/
205803static int fts5ConfigDefaultTokenizer(Fts5Global *pGlobal, Fts5Config *pConfig){
205804 assert( pConfig->pTok==0 && pConfig->pTokApi==0 )((void) (0));
205805 return sqlite3Fts5GetTokenizer(
205806 pGlobal, 0, 0, &pConfig->pTok, &pConfig->pTokApi, 0
205807 );
205808}
205809
205810/*
205811** Gobble up the first bareword or quoted word from the input buffer zIn.
205812** Return a pointer to the character immediately following the last in
205813** the gobbled word if successful, or a NULL pointer otherwise (failed
205814** to find close-quote character).
205815**
205816** Before returning, set pzOut to point to a new buffer containing a
205817** nul-terminated, dequoted copy of the gobbled word. If the word was
205818** quoted, *pbQuoted is also set to 1 before returning.
205819**
205820** If *pRc is other than SQLITE_OK when this function is called, it is
205821** a no-op (NULL is returned). Otherwise, if an OOM occurs within this
205822** function, *pRc is set to SQLITE_NOMEM before returning. *pRc is *not*
205823** set if a parse error (failed to find close quote) occurs.
205824*/
205825static const char *fts5ConfigGobbleWord(
205826 int *pRc, /* IN/OUT: Error code */
205827 const char *zIn, /* Buffer to gobble string/bareword from */
205828 char **pzOut, /* OUT: malloc'd buffer containing str/bw */
205829 int *pbQuoted /* OUT: Set to true if dequoting required */
205830){
205831 const char *zRet = 0;
205832
205833 sqlite3_int64 nIn = strlen(zIn);
205834 char *zOut = sqlite3_malloc64(nIn+1);
205835
205836 assert( *pRc==SQLITE_OK )((void) (0));
205837 *pbQuoted = 0;
205838 *pzOut = 0;
205839
205840 if( zOut==0 ){
205841 *pRc = SQLITE_NOMEM7;
205842 }else{
205843 memcpy(zOut, zIn, (size_t)(nIn+1));
205844 if( fts5_isopenquote(zOut[0]) ){
205845 int ii = fts5Dequote(zOut);
205846 zRet = &zIn[ii];
205847 *pbQuoted = 1;
205848 }else{
205849 zRet = fts5ConfigSkipBareword(zIn);
205850 if( zRet ){
205851 zOut[zRet-zIn] = '\0';
205852 }
205853 }
205854 }
205855
205856 if( zRet==0 ){
205857 sqlite3_free(zOut);
205858 }else{
205859 *pzOut = zOut;
205860 }
205861
205862 return zRet;
205863}
205864
205865static int fts5ConfigParseColumn(
205866 Fts5Config *p,
205867 char *zCol,
205868 char *zArg,
205869 char **pzErr
205870){
205871 int rc = SQLITE_OK0;
205872 if( 0==sqlite3_stricmp(zCol, FTS5_RANK_NAME)
205873 || 0==sqlite3_stricmp(zCol, FTS5_ROWID_NAME)
205874 ){
205875 *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zCol);
205876 rc = SQLITE_ERROR1;
205877 }else if( zArg ){
205878 if( 0==sqlite3_stricmp(zArg, "unindexed") ){
205879 p->abUnindexed[p->nCol] = 1;
205880 }else{
205881 *pzErr = sqlite3_mprintf("unrecognized column option: %s", zArg);
205882 rc = SQLITE_ERROR1;
205883 }
205884 }
205885
205886 p->azCol[p->nCol++] = zCol;
205887 return rc;
205888}
205889
205890/*
205891** Populate the Fts5Config.zContentExprlist string.
205892*/
205893static int fts5ConfigMakeExprlist(Fts5Config *p){
205894 int i;
205895 int rc = SQLITE_OK0;
205896 Fts5Buffer buf = {0, 0, 0};
205897
205898 sqlite3Fts5BufferAppendPrintf(&rc, &buf, "T.%Q", p->zContentRowid);
205899 if( p->eContent!=FTS5_CONTENT_NONE ){
205900 for(i=0; i<p->nCol; i++){
205901 if( p->eContent==FTS5_CONTENT_EXTERNAL ){
205902 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.%Q", p->azCol[i]);
205903 }else{
205904 sqlite3Fts5BufferAppendPrintf(&rc, &buf, ", T.c%d", i);
205905 }
205906 }
205907 }
205908
205909 assert( p->zContentExprlist==0 )((void) (0));
205910 p->zContentExprlist = (char*)buf.p;
205911 return rc;
205912}
205913
205914/*
205915** Arguments nArg/azArg contain the string arguments passed to the xCreate
205916** or xConnect method of the virtual table. This function attempts to
205917** allocate an instance of Fts5Config containing the results of parsing
205918** those arguments.
205919**
205920** If successful, SQLITE_OK is returned and *ppOut is set to point to the
205921** new Fts5Config object. If an error occurs, an SQLite error code is
205922** returned, *ppOut is set to NULL and an error message may be left in
205923** *pzErr. It is the responsibility of the caller to eventually free any
205924** such error message using sqlite3_free().
205925*/
205926static int sqlite3Fts5ConfigParse(
205927 Fts5Global *pGlobal,
205928 sqlite3 *db,
205929 int nArg, /* Number of arguments */
205930 const char **azArg, /* Array of nArg CREATE VIRTUAL TABLE args */
205931 Fts5Config **ppOut, /* OUT: Results of parse */
205932 char **pzErr /* OUT: Error message */
205933){
205934 int rc = SQLITE_OK0; /* Return code */
205935 Fts5Config *pRet; /* New object to return */
205936 int i;
205937 sqlite3_int64 nByte;
205938
205939 *ppOut = pRet = (Fts5Config*)sqlite3_malloc(sizeof(Fts5Config));
205940 if( pRet==0 ) return SQLITE_NOMEM7;
205941 memset(pRet, 0, sizeof(Fts5Config));
205942 pRet->db = db;
205943 pRet->iCookie = -1;
205944
205945 nByte = nArg * (sizeof(char*) + sizeof(u8));
205946 pRet->azCol = (char**)sqlite3Fts5MallocZero(&rc, nByte);
205947 pRet->abUnindexed = (u8*)&pRet->azCol[nArg];
205948 pRet->zDb = sqlite3Fts5Strndup(&rc, azArg[1], -1);
205949 pRet->zName = sqlite3Fts5Strndup(&rc, azArg[2], -1);
205950 pRet->bColumnsize = 1;
205951 pRet->eDetail = FTS5_DETAIL_FULL;
205952#ifdef SQLITE_DEBUG
205953 pRet->bPrefixIndex = 1;
205954#endif
205955 if( rc==SQLITE_OK0 && sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME)==0 ){
205956 *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName);
205957 rc = SQLITE_ERROR1;
205958 }
205959
205960 for(i=3; rc==SQLITE_OK0 && i<nArg; i++){
205961 const char *zOrig = azArg[i];
205962 const char *z;
205963 char *zOne = 0;
205964 char *zTwo = 0;
205965 int bOption = 0;
205966 int bMustBeCol = 0;
205967
205968 z = fts5ConfigGobbleWord(&rc, zOrig, &zOne, &bMustBeCol);
205969 z = fts5ConfigSkipWhitespace(z);
205970 if( z && *z=='=' ){
205971 bOption = 1;
205972 z++;
205973 if( bMustBeCol ) z = 0;
205974 }
205975 z = fts5ConfigSkipWhitespace(z);
205976 if( z && z[0] ){
205977 int bDummy;
205978 z = fts5ConfigGobbleWord(&rc, z, &zTwo, &bDummy);
205979 if( z && z[0] ) z = 0;
205980 }
205981
205982 if( rc==SQLITE_OK0 ){
205983 if( z==0 ){
205984 *pzErr = sqlite3_mprintf("parse error in \"%s\"", zOrig);
205985 rc = SQLITE_ERROR1;
205986 }else{
205987 if( bOption ){
205988 rc = fts5ConfigParseSpecial(pGlobal, pRet, zOne, zTwo?zTwo:"", pzErr);
205989 }else{
205990 rc = fts5ConfigParseColumn(pRet, zOne, zTwo, pzErr);
205991 zOne = 0;
205992 }
205993 }
205994 }
205995
205996 sqlite3_free(zOne);
205997 sqlite3_free(zTwo);
205998 }
205999
206000 /* If a tokenizer= option was successfully parsed, the tokenizer has
206001 ** already been allocated. Otherwise, allocate an instance of the default
206002 ** tokenizer (unicode61) now. */
206003 if( rc==SQLITE_OK0 && pRet->pTok==0 ){
206004 rc = fts5ConfigDefaultTokenizer(pGlobal, pRet);
206005 }
206006
206007 /* If no zContent option was specified, fill in the default values. */
206008 if( rc==SQLITE_OK0 && pRet->zContent==0 ){
206009 const char *zTail = 0;
206010 assert( pRet->eContent==FTS5_CONTENT_NORMAL((void) (0))
206011 || pRet->eContent==FTS5_CONTENT_NONE((void) (0))
206012 )((void) (0));
206013 if( pRet->eContent==FTS5_CONTENT_NORMAL ){
206014 zTail = "content";
206015 }else if( pRet->bColumnsize ){
206016 zTail = "docsize";
206017 }
206018
206019 if( zTail ){
206020 pRet->zContent = sqlite3Fts5Mprintf(
206021 &rc, "%Q.'%q_%s'", pRet->zDb, pRet->zName, zTail
206022 );
206023 }
206024 }
206025
206026 if( rc==SQLITE_OK0 && pRet->zContentRowid==0 ){
206027 pRet->zContentRowid = sqlite3Fts5Strndup(&rc, "rowid", -1);
206028 }
206029
206030 /* Formulate the zContentExprlist text */
206031 if( rc==SQLITE_OK0 ){
206032 rc = fts5ConfigMakeExprlist(pRet);
206033 }
206034
206035 if( rc!=SQLITE_OK0 ){
206036 sqlite3Fts5ConfigFree(pRet);
206037 *ppOut = 0;
206038 }
206039 return rc;
206040}
206041
206042/*
206043** Free the configuration object passed as the only argument.
206044*/
206045static void sqlite3Fts5ConfigFree(Fts5Config *pConfig){
206046 if( pConfig ){
206047 int i;
206048 if( pConfig->pTok ){
206049 pConfig->pTokApi->xDelete(pConfig->pTok);
206050 }
206051 sqlite3_free(pConfig->zDb);
206052 sqlite3_free(pConfig->zName);
206053 for(i=0; i<pConfig->nCol; i++){
206054 sqlite3_free(pConfig->azCol[i]);
206055 }
206056 sqlite3_free(pConfig->azCol);
206057 sqlite3_free(pConfig->aPrefix);
206058 sqlite3_free(pConfig->zRank);
206059 sqlite3_free(pConfig->zRankArgs);
206060 sqlite3_free(pConfig->zContent);
206061 sqlite3_free(pConfig->zContentRowid);
206062 sqlite3_free(pConfig->zContentExprlist);
206063 sqlite3_free(pConfig);
206064 }
206065}
206066
206067/*
206068** Call sqlite3_declare_vtab() based on the contents of the configuration
206069** object passed as the only argument. Return SQLITE_OK if successful, or
206070** an SQLite error code if an error occurs.
206071*/
206072static int sqlite3Fts5ConfigDeclareVtab(Fts5Config *pConfig){
206073 int i;
206074 int rc = SQLITE_OK0;
206075 char *zSql;
206076
206077 zSql = sqlite3Fts5Mprintf(&rc, "CREATE TABLE x(");
206078 for(i=0; zSql && i<pConfig->nCol; i++){
206079 const char *zSep = (i==0?"":", ");
206080 zSql = sqlite3Fts5Mprintf(&rc, "%z%s%Q", zSql, zSep, pConfig->azCol[i]);
206081 }
206082 zSql = sqlite3Fts5Mprintf(&rc, "%z, %Q HIDDEN, %s HIDDEN)",
206083 zSql, pConfig->zName, FTS5_RANK_NAME
206084 );
206085
206086 assert( zSql || rc==SQLITE_NOMEM )((void) (0));
206087 if( zSql ){
206088 rc = sqlite3_declare_vtab(pConfig->db, zSql);
206089 sqlite3_free(zSql);
206090 }
206091
206092 return rc;
206093}
206094
206095/*
206096** Tokenize the text passed via the second and third arguments.
206097**
206098** The callback is invoked once for each token in the input text. The
206099** arguments passed to it are, in order:
206100**
206101** void *pCtx // Copy of 4th argument to sqlite3Fts5Tokenize()
206102** const char *pToken // Pointer to buffer containing token
206103** int nToken // Size of token in bytes
206104** int iStart // Byte offset of start of token within input text
206105** int iEnd // Byte offset of end of token within input text
206106** int iPos // Position of token in input (first token is 0)
206107**
206108** If the callback returns a non-zero value the tokenization is abandoned
206109** and no further callbacks are issued.
206110**
206111** This function returns SQLITE_OK if successful or an SQLite error code
206112** if an error occurs. If the tokenization was abandoned early because
206113** the callback returned SQLITE_DONE, this is not an error and this function
206114** still returns SQLITE_OK. Or, if the tokenization was abandoned early
206115** because the callback returned another non-zero value, it is assumed
206116** to be an SQLite error code and returned to the caller.
206117*/
206118static int sqlite3Fts5Tokenize(
206119 Fts5Config *pConfig, /* FTS5 Configuration object */
206120 int flags, /* FTS5_TOKENIZE_* flags */
206121 const char *pText, int nText, /* Text to tokenize */
206122 void *pCtx, /* Context passed to xToken() */
206123 int (*xToken)(void*, int, const char*, int, int, int) /* Callback */
206124){
206125 if( pText==0 ) return SQLITE_OK0;
206126 return pConfig->pTokApi->xTokenize(
206127 pConfig->pTok, pCtx, flags, pText, nText, xToken
206128 );
206129}
206130
206131/*
206132** Argument pIn points to the first character in what is expected to be
206133** a comma-separated list of SQL literals followed by a ')' character.
206134** If it actually is this, return a pointer to the ')'. Otherwise, return
206135** NULL to indicate a parse error.
206136*/
206137static const char *fts5ConfigSkipArgs(const char *pIn){
206138 const char *p = pIn;
206139
206140 while( 1 ){
206141 p = fts5ConfigSkipWhitespace(p);
206142 p = fts5ConfigSkipLiteral(p);
206143 p = fts5ConfigSkipWhitespace(p);
206144 if( p==0 || *p==')' ) break;
206145 if( *p!=',' ){
206146 p = 0;
206147 break;
206148 }
206149 p++;
206150 }
206151
206152 return p;
206153}
206154
206155/*
206156** Parameter zIn contains a rank() function specification. The format of
206157** this is:
206158**
206159** + Bareword (function name)
206160** + Open parenthesis - "("
206161** + Zero or more SQL literals in a comma separated list
206162** + Close parenthesis - ")"
206163*/
206164static int sqlite3Fts5ConfigParseRank(
206165 const char *zIn, /* Input string */
206166 char **pzRank, /* OUT: Rank function name */
206167 char **pzRankArgs /* OUT: Rank function arguments */
206168){
206169 const char *p = zIn;
206170 const char *pRank;
206171 char *zRank = 0;
206172 char *zRankArgs = 0;
206173 int rc = SQLITE_OK0;
206174
206175 *pzRank = 0;
206176 *pzRankArgs = 0;
206177
206178 if( p==0 ){
206179 rc = SQLITE_ERROR1;
206180 }else{
206181 p = fts5ConfigSkipWhitespace(p);
206182 pRank = p;
206183 p = fts5ConfigSkipBareword(p);
206184
206185 if( p ){
206186 zRank = sqlite3Fts5MallocZero(&rc, 1 + p - pRank);
206187 if( zRank ) memcpy(zRank, pRank, p-pRank);
206188 }else{
206189 rc = SQLITE_ERROR1;
206190 }
206191
206192 if( rc==SQLITE_OK0 ){
206193 p = fts5ConfigSkipWhitespace(p);
206194 if( *p!='(' ) rc = SQLITE_ERROR1;
206195 p++;
206196 }
206197 if( rc==SQLITE_OK0 ){
206198 const char *pArgs;
206199 p = fts5ConfigSkipWhitespace(p);
206200 pArgs = p;
206201 if( *p!=')' ){
206202 p = fts5ConfigSkipArgs(p);
206203 if( p==0 ){
206204 rc = SQLITE_ERROR1;
206205 }else{
206206 zRankArgs = sqlite3Fts5MallocZero(&rc, 1 + p - pArgs);
206207 if( zRankArgs ) memcpy(zRankArgs, pArgs, p-pArgs);
206208 }
206209 }
206210 }
206211 }
206212
206213 if( rc!=SQLITE_OK0 ){
206214 sqlite3_free(zRank);
206215 assert( zRankArgs==0 )((void) (0));
206216 }else{
206217 *pzRank = zRank;
206218 *pzRankArgs = zRankArgs;
206219 }
206220 return rc;
206221}
206222
206223static int sqlite3Fts5ConfigSetValue(
206224 Fts5Config *pConfig,
206225 const char *zKey,
206226 sqlite3_value *pVal,
206227 int *pbBadkey
206228){
206229 int rc = SQLITE_OK0;
206230
206231 if( 0==sqlite3_stricmp(zKey, "pgsz") ){
206232 int pgsz = 0;
206233 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
206234 pgsz = sqlite3_value_int(pVal);
206235 }
206236 if( pgsz<=0 || pgsz>FTS5_MAX_PAGE_SIZE ){
206237 *pbBadkey = 1;
206238 }else{
206239 pConfig->pgsz = pgsz;
206240 }
206241 }
206242
206243 else if( 0==sqlite3_stricmp(zKey, "hashsize") ){
206244 int nHashSize = -1;
206245 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
206246 nHashSize = sqlite3_value_int(pVal);
206247 }
206248 if( nHashSize<=0 ){
206249 *pbBadkey = 1;
206250 }else{
206251 pConfig->nHashSize = nHashSize;
206252 }
206253 }
206254
206255 else if( 0==sqlite3_stricmp(zKey, "automerge") ){
206256 int nAutomerge = -1;
206257 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
206258 nAutomerge = sqlite3_value_int(pVal);
206259 }
206260 if( nAutomerge<0 || nAutomerge>64 ){
206261 *pbBadkey = 1;
206262 }else{
206263 if( nAutomerge==1 ) nAutomerge = FTS5_DEFAULT_AUTOMERGE;
206264 pConfig->nAutomerge = nAutomerge;
206265 }
206266 }
206267
206268 else if( 0==sqlite3_stricmp(zKey, "usermerge") ){
206269 int nUsermerge = -1;
206270 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
206271 nUsermerge = sqlite3_value_int(pVal);
206272 }
206273 if( nUsermerge<2 || nUsermerge>16 ){
206274 *pbBadkey = 1;
206275 }else{
206276 pConfig->nUsermerge = nUsermerge;
206277 }
206278 }
206279
206280 else if( 0==sqlite3_stricmp(zKey, "crisismerge") ){
206281 int nCrisisMerge = -1;
206282 if( SQLITE_INTEGER1==sqlite3_value_numeric_type(pVal) ){
206283 nCrisisMerge = sqlite3_value_int(pVal);
206284 }
206285 if( nCrisisMerge<0 ){
206286 *pbBadkey = 1;
206287 }else{
206288 if( nCrisisMerge<=1 ) nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
206289 pConfig->nCrisisMerge = nCrisisMerge;
206290 }
206291 }
206292
206293 else if( 0==sqlite3_stricmp(zKey, "rank") ){
206294 const char *zIn = (const char*)sqlite3_value_text(pVal);
206295 char *zRank;
206296 char *zRankArgs;
206297 rc = sqlite3Fts5ConfigParseRank(zIn, &zRank, &zRankArgs);
206298 if( rc==SQLITE_OK0 ){
206299 sqlite3_free(pConfig->zRank);
206300 sqlite3_free(pConfig->zRankArgs);
206301 pConfig->zRank = zRank;
206302 pConfig->zRankArgs = zRankArgs;
206303 }else if( rc==SQLITE_ERROR1 ){
206304 rc = SQLITE_OK0;
206305 *pbBadkey = 1;
206306 }
206307 }else{
206308 *pbBadkey = 1;
206309 }
206310 return rc;
206311}
206312
206313/*
206314** Load the contents of the %_config table into memory.
206315*/
206316static int sqlite3Fts5ConfigLoad(Fts5Config *pConfig, int iCookie){
206317 const char *zSelect = "SELECT k, v FROM %Q.'%q_config'";
206318 char *zSql;
206319 sqlite3_stmt *p = 0;
206320 int rc = SQLITE_OK0;
206321 int iVersion = 0;
206322
206323 /* Set default values */
206324 pConfig->pgsz = FTS5_DEFAULT_PAGE_SIZE;
206325 pConfig->nAutomerge = FTS5_DEFAULT_AUTOMERGE;
206326 pConfig->nUsermerge = FTS5_DEFAULT_USERMERGE;
206327 pConfig->nCrisisMerge = FTS5_DEFAULT_CRISISMERGE;
206328 pConfig->nHashSize = FTS5_DEFAULT_HASHSIZE;
206329
206330 zSql = sqlite3Fts5Mprintf(&rc, zSelect, pConfig->zDb, pConfig->zName);
206331 if( zSql ){
206332 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &p, 0);
206333 sqlite3_free(zSql);
206334 }
206335
206336 assert( rc==SQLITE_OK || p==0 )((void) (0));
206337 if( rc==SQLITE_OK0 ){
206338 while( SQLITE_ROW100==sqlite3_step(p) ){
206339 const char *zK = (const char*)sqlite3_column_text(p, 0);
206340 sqlite3_value *pVal = sqlite3_column_value(p, 1);
206341 if( 0==sqlite3_stricmp(zK, "version") ){
206342 iVersion = sqlite3_value_int(pVal);
206343 }else{
206344 int bDummy = 0;
206345 sqlite3Fts5ConfigSetValue(pConfig, zK, pVal, &bDummy);
206346 }
206347 }
206348 rc = sqlite3_finalize(p);
206349 }
206350
206351 if( rc==SQLITE_OK0 && iVersion!=FTS5_CURRENT_VERSION ){
206352 rc = SQLITE_ERROR1;
206353 if( pConfig->pzErrmsg ){
206354 assert( 0==*pConfig->pzErrmsg )((void) (0));
206355 *pConfig->pzErrmsg = sqlite3_mprintf(
206356 "invalid fts5 file format (found %d, expected %d) - run 'rebuild'",
206357 iVersion, FTS5_CURRENT_VERSION
206358 );
206359 }
206360 }
206361
206362 if( rc==SQLITE_OK0 ){
206363 pConfig->iCookie = iCookie;
206364 }
206365 return rc;
206366}
206367
206368/*
206369** 2014 May 31
206370**
206371** The author disclaims copyright to this source code. In place of
206372** a legal notice, here is a blessing:
206373**
206374** May you do good and not evil.
206375** May you find forgiveness for yourself and forgive others.
206376** May you share freely, never taking more than you give.
206377**
206378******************************************************************************
206379**
206380*/
206381
206382
206383
206384/* #include "fts5Int.h" */
206385/* #include "fts5parse.h" */
206386
206387/*
206388** All token types in the generated fts5parse.h file are greater than 0.
206389*/
206390#define FTS5_EOF 0
206391
206392#define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
206393
206394typedef struct Fts5ExprTerm Fts5ExprTerm;
206395
206396/*
206397** Functions generated by lemon from fts5parse.y.
206398*/
206399static void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
206400static void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
206401static void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
206402#ifndef NDEBUG1
206403/* #include <stdio.h> */
206404static void sqlite3Fts5ParserTrace(FILE*, char*);
206405#endif
206406static int sqlite3Fts5ParserFallback(int);
206407
206408
206409struct Fts5Expr {
206410 Fts5Index *pIndex;
206411 Fts5Config *pConfig;
206412 Fts5ExprNode *pRoot;
206413 int bDesc; /* Iterate in descending rowid order */
206414 int nPhrase; /* Number of phrases in expression */
206415 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
206416};
206417
206418/*
206419** eType:
206420** Expression node type. Always one of:
206421**
206422** FTS5_AND (nChild, apChild valid)
206423** FTS5_OR (nChild, apChild valid)
206424** FTS5_NOT (nChild, apChild valid)
206425** FTS5_STRING (pNear valid)
206426** FTS5_TERM (pNear valid)
206427*/
206428struct Fts5ExprNode {
206429 int eType; /* Node type */
206430 int bEof; /* True at EOF */
206431 int bNomatch; /* True if entry is not a match */
206432
206433 /* Next method for this node. */
206434 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
206435
206436 i64 iRowid; /* Current rowid */
206437 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
206438
206439 /* Child nodes. For a NOT node, this array always contains 2 entries. For
206440 ** AND or OR nodes, it contains 2 or more entries. */
206441 int nChild; /* Number of child nodes */
206442 Fts5ExprNode *apChild[1]; /* Array of child nodes */
206443};
206444
206445#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
206446
206447/*
206448** Invoke the xNext method of an Fts5ExprNode object. This macro should be
206449** used as if it has the same signature as the xNext() methods themselves.
206450*/
206451#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
206452
206453/*
206454** An instance of the following structure represents a single search term
206455** or term prefix.
206456*/
206457struct Fts5ExprTerm {
206458 u8 bPrefix; /* True for a prefix term */
206459 u8 bFirst; /* True if token must be first in column */
206460 char *zTerm; /* nul-terminated term */
206461 Fts5IndexIter *pIter; /* Iterator for this term */
206462 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
206463};
206464
206465/*
206466** A phrase. One or more terms that must appear in a contiguous sequence
206467** within a document for it to match.
206468*/
206469struct Fts5ExprPhrase {
206470 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
206471 Fts5Buffer poslist; /* Current position list */
206472 int nTerm; /* Number of entries in aTerm[] */
206473 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
206474};
206475
206476/*
206477** One or more phrases that must appear within a certain token distance of
206478** each other within each matching document.
206479*/
206480struct Fts5ExprNearset {
206481 int nNear; /* NEAR parameter */
206482 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
206483 int nPhrase; /* Number of entries in aPhrase[] array */
206484 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
206485};
206486
206487
206488/*
206489** Parse context.
206490*/
206491struct Fts5Parse {
206492 Fts5Config *pConfig;
206493 char *zErr;
206494 int rc;
206495 int nPhrase; /* Size of apPhrase array */
206496 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
206497 Fts5ExprNode *pExpr; /* Result of a successful parse */
206498};
206499
206500static void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
206501 va_list ap;
206502 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
206503 if( pParse->rc==SQLITE_OK0 ){
206504 pParse->zErr = sqlite3_vmprintf(zFmt, ap);
206505 pParse->rc = SQLITE_ERROR1;
206506 }
206507 va_end(ap)__builtin_va_end(ap);
206508}
206509
206510static int fts5ExprIsspace(char t){
206511 return t==' ' || t=='\t' || t=='\n' || t=='\r';
206512}
206513
206514/*
206515** Read the first token from the nul-terminated string at *pz.
206516*/
206517static int fts5ExprGetToken(
206518 Fts5Parse *pParse,
206519 const char **pz, /* IN/OUT: Pointer into buffer */
206520 Fts5Token *pToken
206521){
206522 const char *z = *pz;
206523 int tok;
206524
206525 /* Skip past any whitespace */
206526 while( fts5ExprIsspace(*z) ) z++;
206527
206528 pToken->p = z;
206529 pToken->n = 1;
206530 switch( *z ){
206531 case '(': tok = FTS5_LP; break;
206532 case ')': tok = FTS5_RP; break;
206533 case '{': tok = FTS5_LCP; break;
206534 case '}': tok = FTS5_RCP; break;
206535 case ':': tok = FTS5_COLON; break;
206536 case ',': tok = FTS5_COMMA; break;
206537 case '+': tok = FTS5_PLUS; break;
206538 case '*': tok = FTS5_STAR; break;
206539 case '-': tok = FTS5_MINUS; break;
206540 case '^': tok = FTS5_CARET; break;
206541 case '\0': tok = FTS5_EOF; break;
206542
206543 case '"': {
206544 const char *z2;
206545 tok = FTS5_STRING;
206546
206547 for(z2=&z[1]; 1; z2++){
206548 if( z2[0]=='"' ){
206549 z2++;
206550 if( z2[0]!='"' ) break;
206551 }
206552 if( z2[0]=='\0' ){
206553 sqlite3Fts5ParseError(pParse, "unterminated string");
206554 return FTS5_EOF;
206555 }
206556 }
206557 pToken->n = (z2 - z);
206558 break;
206559 }
206560
206561 default: {
206562 const char *z2;
206563 if( sqlite3Fts5IsBareword(z[0])==0 ){
206564 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
206565 return FTS5_EOF;
206566 }
206567 tok = FTS5_STRING;
206568 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
206569 pToken->n = (z2 - z);
206570 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR;
206571 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
206572 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
206573 break;
206574 }
206575 }
206576
206577 *pz = &pToken->p[pToken->n];
206578 return tok;
206579}
206580
206581static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);}
206582static void fts5ParseFree(void *p){ sqlite3_free(p); }
206583
206584static int sqlite3Fts5ExprNew(
206585 Fts5Config *pConfig, /* FTS5 Configuration */
206586 int iCol,
206587 const char *zExpr, /* Expression text */
206588 Fts5Expr **ppNew,
206589 char **pzErr
206590){
206591 Fts5Parse sParse;
206592 Fts5Token token;
206593 const char *z = zExpr;
206594 int t; /* Next token type */
206595 void *pEngine;
206596 Fts5Expr *pNew;
206597
206598 *ppNew = 0;
206599 *pzErr = 0;
206600 memset(&sParse, 0, sizeof(sParse));
206601 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
206602 if( pEngine==0 ){ return SQLITE_NOMEM7; }
206603 sParse.pConfig = pConfig;
206604
206605 do {
206606 t = fts5ExprGetToken(&sParse, &z, &token);
206607 sqlite3Fts5Parser(pEngine, t, token, &sParse);
206608 }while( sParse.rc==SQLITE_OK0 && t!=FTS5_EOF );
206609 sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
206610
206611 /* If the LHS of the MATCH expression was a user column, apply the
206612 ** implicit column-filter. */
206613 if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK0 ){
206614 int n = sizeof(Fts5Colset);
206615 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
206616 if( pColset ){
206617 pColset->nCol = 1;
206618 pColset->aiCol[0] = iCol;
206619 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
206620 }
206621 }
206622
206623 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 )((void) (0));
206624 if( sParse.rc==SQLITE_OK0 ){
206625 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
206626 if( pNew==0 ){
206627 sParse.rc = SQLITE_NOMEM7;
206628 sqlite3Fts5ParseNodeFree(sParse.pExpr);
206629 }else{
206630 if( !sParse.pExpr ){
206631 const int nByte = sizeof(Fts5ExprNode);
206632 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
206633 if( pNew->pRoot ){
206634 pNew->pRoot->bEof = 1;
206635 }
206636 }else{
206637 pNew->pRoot = sParse.pExpr;
206638 }
206639 pNew->pIndex = 0;
206640 pNew->pConfig = pConfig;
206641 pNew->apExprPhrase = sParse.apPhrase;
206642 pNew->nPhrase = sParse.nPhrase;
206643 sParse.apPhrase = 0;
206644 }
206645 }else{
206646 sqlite3Fts5ParseNodeFree(sParse.pExpr);
206647 }
206648
206649 sqlite3_free(sParse.apPhrase);
206650 *pzErr = sParse.zErr;
206651 return sParse.rc;
206652}
206653
206654/*
206655** Free the expression node object passed as the only argument.
206656*/
206657static void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
206658 if( p ){
206659 int i;
206660 for(i=0; i<p->nChild; i++){
206661 sqlite3Fts5ParseNodeFree(p->apChild[i]);
206662 }
206663 sqlite3Fts5ParseNearsetFree(p->pNear);
206664 sqlite3_free(p);
206665 }
206666}
206667
206668/*
206669** Free the expression object passed as the only argument.
206670*/
206671static void sqlite3Fts5ExprFree(Fts5Expr *p){
206672 if( p ){
206673 sqlite3Fts5ParseNodeFree(p->pRoot);
206674 sqlite3_free(p->apExprPhrase);
206675 sqlite3_free(p);
206676 }
206677}
206678
206679/*
206680** Argument pTerm must be a synonym iterator. Return the current rowid
206681** that it points to.
206682*/
206683static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
206684 i64 iRet = 0;
206685 int bRetValid = 0;
206686 Fts5ExprTerm *p;
206687
206688 assert( pTerm->pSynonym )((void) (0));
206689 assert( bDesc==0 || bDesc==1 )((void) (0));
206690 for(p=pTerm; p; p=p->pSynonym){
206691 if( 0==sqlite3Fts5IterEof(p->pIter) ){
206692 i64 iRowid = p->pIter->iRowid;
206693 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
206694 iRet = iRowid;
206695 bRetValid = 1;
206696 }
206697 }
206698 }
206699
206700 if( pbEof && bRetValid==0 ) *pbEof = 1;
206701 return iRet;
206702}
206703
206704/*
206705** Argument pTerm must be a synonym iterator.
206706*/
206707static int fts5ExprSynonymList(
206708 Fts5ExprTerm *pTerm,
206709 i64 iRowid,
206710 Fts5Buffer *pBuf, /* Use this buffer for space if required */
206711 u8 **pa, int *pn
206712){
206713 Fts5PoslistReader aStatic[4];
206714 Fts5PoslistReader *aIter = aStatic;
206715 int nIter = 0;
206716 int nAlloc = 4;
206717 int rc = SQLITE_OK0;
206718 Fts5ExprTerm *p;
206719
206720 assert( pTerm->pSynonym )((void) (0));
206721 for(p=pTerm; p; p=p->pSynonym){
206722 Fts5IndexIter *pIter = p->pIter;
206723 if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
206724 if( pIter->nData==0 ) continue;
206725 if( nIter==nAlloc ){
206726 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
206727 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
206728 if( aNew==0 ){
206729 rc = SQLITE_NOMEM7;
206730 goto synonym_poslist_out;
206731 }
206732 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
206733 nAlloc = nAlloc*2;
206734 if( aIter!=aStatic ) sqlite3_free(aIter);
206735 aIter = aNew;
206736 }
206737 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
206738 assert( aIter[nIter].bEof==0 )((void) (0));
206739 nIter++;
206740 }
206741 }
206742
206743 if( nIter==1 ){
206744 *pa = (u8*)aIter[0].a;
206745 *pn = aIter[0].n;
206746 }else{
206747 Fts5PoslistWriter writer = {0};
206748 i64 iPrev = -1;
206749 fts5BufferZero(pBuf);
206750 while( 1 ){
206751 int i;
206752 i64 iMin = FTS5_LARGEST_INT64;
206753 for(i=0; i<nIter; i++){
206754 if( aIter[i].bEof==0 ){
206755 if( aIter[i].iPos==iPrev ){
206756 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
206757 }
206758 if( aIter[i].iPos<iMin ){
206759 iMin = aIter[i].iPos;
206760 }
206761 }
206762 }
206763 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK0 ) break;
206764 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
206765 iPrev = iMin;
206766 }
206767 if( rc==SQLITE_OK0 ){
206768 *pa = pBuf->p;
206769 *pn = pBuf->n;
206770 }
206771 }
206772
206773 synonym_poslist_out:
206774 if( aIter!=aStatic ) sqlite3_free(aIter);
206775 return rc;
206776}
206777
206778
206779/*
206780** All individual term iterators in pPhrase are guaranteed to be valid and
206781** pointing to the same rowid when this function is called. This function
206782** checks if the current rowid really is a match, and if so populates
206783** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
206784** is set to true if this is really a match, or false otherwise.
206785**
206786** SQLITE_OK is returned if an error occurs, or an SQLite error code
206787** otherwise. It is not considered an error code if the current rowid is
206788** not a match.
206789*/
206790static int fts5ExprPhraseIsMatch(
206791 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
206792 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
206793 int *pbMatch /* OUT: Set to true if really a match */
206794){
206795 Fts5PoslistWriter writer = {0};
206796 Fts5PoslistReader aStatic[4];
206797 Fts5PoslistReader *aIter = aStatic;
206798 int i;
206799 int rc = SQLITE_OK0;
206800 int bFirst = pPhrase->aTerm[0].bFirst;
206801
206802 fts5BufferZero(&pPhrase->poslist);
206803
206804 /* If the aStatic[] array is not large enough, allocate a large array
206805 ** using sqlite3_malloc(). This approach could be improved upon. */
206806 if( pPhrase->nTerm>ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
206807 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
206808 aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
206809 if( !aIter ) return SQLITE_NOMEM7;
206810 }
206811 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
206812
206813 /* Initialize a term iterator for each term in the phrase */
206814 for(i=0; i<pPhrase->nTerm; i++){
206815 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
206816 int n = 0;
206817 int bFlag = 0;
206818 u8 *a = 0;
206819 if( pTerm->pSynonym ){
206820 Fts5Buffer buf = {0, 0, 0};
206821 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
206822 if( rc ){
206823 sqlite3_free(a);
206824 goto ismatch_out;
206825 }
206826 if( a==buf.p ) bFlag = 1;
206827 }else{
206828 a = (u8*)pTerm->pIter->pData;
206829 n = pTerm->pIter->nData;
206830 }
206831 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
206832 aIter[i].bFlag = (u8)bFlag;
206833 if( aIter[i].bEof ) goto ismatch_out;
206834 }
206835
206836 while( 1 ){
206837 int bMatch;
206838 i64 iPos = aIter[0].iPos;
206839 do {
206840 bMatch = 1;
206841 for(i=0; i<pPhrase->nTerm; i++){
206842 Fts5PoslistReader *pPos = &aIter[i];
206843 i64 iAdj = iPos + i;
206844 if( pPos->iPos!=iAdj ){
206845 bMatch = 0;
206846 while( pPos->iPos<iAdj ){
206847 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
206848 }
206849 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
206850 }
206851 }
206852 }while( bMatch==0 );
206853
206854 /* Append position iPos to the output */
206855 if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
206856 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
206857 if( rc!=SQLITE_OK0 ) goto ismatch_out;
206858 }
206859
206860 for(i=0; i<pPhrase->nTerm; i++){
206861 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
206862 }
206863 }
206864
206865 ismatch_out:
206866 *pbMatch = (pPhrase->poslist.n>0);
206867 for(i=0; i<pPhrase->nTerm; i++){
206868 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
206869 }
206870 if( aIter!=aStatic ) sqlite3_free(aIter);
206871 return rc;
206872}
206873
206874typedef struct Fts5LookaheadReader Fts5LookaheadReader;
206875struct Fts5LookaheadReader {
206876 const u8 *a; /* Buffer containing position list */
206877 int n; /* Size of buffer a[] in bytes */
206878 int i; /* Current offset in position list */
206879 i64 iPos; /* Current position */
206880 i64 iLookahead; /* Next position */
206881};
206882
206883#define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
206884
206885static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
206886 p->iPos = p->iLookahead;
206887 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
206888 p->iLookahead = FTS5_LOOKAHEAD_EOF;
206889 }
206890 return (p->iPos==FTS5_LOOKAHEAD_EOF);
206891}
206892
206893static int fts5LookaheadReaderInit(
206894 const u8 *a, int n, /* Buffer to read position list from */
206895 Fts5LookaheadReader *p /* Iterator object to initialize */
206896){
206897 memset(p, 0, sizeof(Fts5LookaheadReader));
206898 p->a = a;
206899 p->n = n;
206900 fts5LookaheadReaderNext(p);
206901 return fts5LookaheadReaderNext(p);
206902}
206903
206904typedef struct Fts5NearTrimmer Fts5NearTrimmer;
206905struct Fts5NearTrimmer {
206906 Fts5LookaheadReader reader; /* Input iterator */
206907 Fts5PoslistWriter writer; /* Writer context */
206908 Fts5Buffer *pOut; /* Output poslist */
206909};
206910
206911/*
206912** The near-set object passed as the first argument contains more than
206913** one phrase. All phrases currently point to the same row. The
206914** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
206915** tests if the current row contains instances of each phrase sufficiently
206916** close together to meet the NEAR constraint. Non-zero is returned if it
206917** does, or zero otherwise.
206918**
206919** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
206920** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
206921** occurs within this function (*pRc) is set accordingly before returning.
206922** The return value is undefined in both these cases.
206923**
206924** If no error occurs and non-zero (a match) is returned, the position-list
206925** of each phrase object is edited to contain only those entries that
206926** meet the constraint before returning.
206927*/
206928static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
206929 Fts5NearTrimmer aStatic[4];
206930 Fts5NearTrimmer *a = aStatic;
206931 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
206932
206933 int i;
206934 int rc = *pRc;
206935 int bMatch;
206936
206937 assert( pNear->nPhrase>1 )((void) (0));
206938
206939 /* If the aStatic[] array is not large enough, allocate a large array
206940 ** using sqlite3_malloc(). This approach could be improved upon. */
206941 if( pNear->nPhrase>ArraySize(aStatic)((int)(sizeof(aStatic)/sizeof(aStatic[0]))) ){
206942 sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
206943 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
206944 }else{
206945 memset(aStatic, 0, sizeof(aStatic));
206946 }
206947 if( rc!=SQLITE_OK0 ){
206948 *pRc = rc;
206949 return 0;
206950 }
206951
206952 /* Initialize a lookahead iterator for each phrase. After passing the
206953 ** buffer and buffer size to the lookaside-reader init function, zero
206954 ** the phrase poslist buffer. The new poslist for the phrase (containing
206955 ** the same entries as the original with some entries removed on account
206956 ** of the NEAR constraint) is written over the original even as it is
206957 ** being read. This is safe as the entries for the new poslist are a
206958 ** subset of the old, so it is not possible for data yet to be read to
206959 ** be overwritten. */
206960 for(i=0; i<pNear->nPhrase; i++){
206961 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
206962 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
206963 pPoslist->n = 0;
206964 a[i].pOut = pPoslist;
206965 }
206966
206967 while( 1 ){
206968 int iAdv;
206969 i64 iMin;
206970 i64 iMax;
206971
206972 /* This block advances the phrase iterators until they point to a set of
206973 ** entries that together comprise a match. */
206974 iMax = a[0].reader.iPos;
206975 do {
206976 bMatch = 1;
206977 for(i=0; i<pNear->nPhrase; i++){
206978 Fts5LookaheadReader *pPos = &a[i].reader;
206979 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
206980 if( pPos->iPos<iMin || pPos->iPos>iMax ){
206981 bMatch = 0;
206982 while( pPos->iPos<iMin ){
206983 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
206984 }
206985 if( pPos->iPos>iMax ) iMax = pPos->iPos;
206986 }
206987 }
206988 }while( bMatch==0 );
206989
206990 /* Add an entry to each output position list */
206991 for(i=0; i<pNear->nPhrase; i++){
206992 i64 iPos = a[i].reader.iPos;
206993 Fts5PoslistWriter *pWriter = &a[i].writer;
206994 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
206995 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
206996 }
206997 }
206998
206999 iAdv = 0;
207000 iMin = a[0].reader.iLookahead;
207001 for(i=0; i<pNear->nPhrase; i++){
207002 if( a[i].reader.iLookahead < iMin ){
207003 iMin = a[i].reader.iLookahead;
207004 iAdv = i;
207005 }
207006 }
207007 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
207008 }
207009
207010 ismatch_out: {
207011 int bRet = a[0].pOut->n>0;
207012 *pRc = rc;
207013 if( a!=aStatic ) sqlite3_free(a);
207014 return bRet;
207015 }
207016}
207017
207018/*
207019** Advance iterator pIter until it points to a value equal to or laster
207020** than the initial value of *piLast. If this means the iterator points
207021** to a value laster than *piLast, update *piLast to the new lastest value.
207022**
207023** If the iterator reaches EOF, set *pbEof to true before returning. If
207024** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
207025** are set, return a non-zero value. Otherwise, return zero.
207026*/
207027static int fts5ExprAdvanceto(
207028 Fts5IndexIter *pIter, /* Iterator to advance */
207029 int bDesc, /* True if iterator is "rowid DESC" */
207030 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
207031 int *pRc, /* OUT: Error code */
207032 int *pbEof /* OUT: Set to true if EOF */
207033){
207034 i64 iLast = *piLast;
207035 i64 iRowid;
207036
207037 iRowid = pIter->iRowid;
207038 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
207039 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
207040 if( rc || sqlite3Fts5IterEof(pIter) ){
207041 *pRc = rc;
207042 *pbEof = 1;
207043 return 1;
207044 }
207045 iRowid = pIter->iRowid;
207046 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) )((void) (0));
207047 }
207048 *piLast = iRowid;
207049
207050 return 0;
207051}
207052
207053static int fts5ExprSynonymAdvanceto(
207054 Fts5ExprTerm *pTerm, /* Term iterator to advance */
207055 int bDesc, /* True if iterator is "rowid DESC" */
207056 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
207057 int *pRc /* OUT: Error code */
207058){
207059 int rc = SQLITE_OK0;
207060 i64 iLast = *piLast;
207061 Fts5ExprTerm *p;
207062 int bEof = 0;
207063
207064 for(p=pTerm; rc==SQLITE_OK0 && p; p=p->pSynonym){
207065 if( sqlite3Fts5IterEof(p->pIter)==0 ){
207066 i64 iRowid = p->pIter->iRowid;
207067 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
207068 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
207069 }
207070 }
207071 }
207072
207073 if( rc!=SQLITE_OK0 ){
207074 *pRc = rc;
207075 bEof = 1;
207076 }else{
207077 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
207078 }
207079 return bEof;
207080}
207081
207082
207083static int fts5ExprNearTest(
207084 int *pRc,
207085 Fts5Expr *pExpr, /* Expression that pNear is a part of */
207086 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
207087){
207088 Fts5ExprNearset *pNear = pNode->pNear;
207089 int rc = *pRc;
207090
207091 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
207092 Fts5ExprTerm *pTerm;
207093 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
207094 pPhrase->poslist.n = 0;
207095 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
207096 Fts5IndexIter *pIter = pTerm->pIter;
207097 if( sqlite3Fts5IterEof(pIter)==0 ){
207098 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
207099 pPhrase->poslist.n = 1;
207100 }
207101 }
207102 }
207103 return pPhrase->poslist.n;
207104 }else{
207105 int i;
207106
207107 /* Check that each phrase in the nearset matches the current row.
207108 ** Populate the pPhrase->poslist buffers at the same time. If any
207109 ** phrase is not a match, break out of the loop early. */
207110 for(i=0; rc==SQLITE_OK0 && i<pNear->nPhrase; i++){
207111 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
207112 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
207113 || pNear->pColset || pPhrase->aTerm[0].bFirst
207114 ){
207115 int bMatch = 0;
207116 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
207117 if( bMatch==0 ) break;
207118 }else{
207119 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
207120 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
207121 }
207122 }
207123
207124 *pRc = rc;
207125 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
207126 return 1;
207127 }
207128 return 0;
207129 }
207130}
207131
207132
207133/*
207134** Initialize all term iterators in the pNear object. If any term is found
207135** to match no documents at all, return immediately without initializing any
207136** further iterators.
207137**
207138** If an error occurs, return an SQLite error code. Otherwise, return
207139** SQLITE_OK. It is not considered an error if some term matches zero
207140** documents.
207141*/
207142static int fts5ExprNearInitAll(
207143 Fts5Expr *pExpr,
207144 Fts5ExprNode *pNode
207145){
207146 Fts5ExprNearset *pNear = pNode->pNear;
207147 int i;
207148
207149 assert( pNode->bNomatch==0 )((void) (0));
207150 for(i=0; i<pNear->nPhrase; i++){
207151 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
207152 if( pPhrase->nTerm==0 ){
207153 pNode->bEof = 1;
207154 return SQLITE_OK0;
207155 }else{
207156 int j;
207157 for(j=0; j<pPhrase->nTerm; j++){
207158 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
207159 Fts5ExprTerm *p;
207160 int bHit = 0;
207161
207162 for(p=pTerm; p; p=p->pSynonym){
207163 int rc;
207164 if( p->pIter ){
207165 sqlite3Fts5IterClose(p->pIter);
207166 p->pIter = 0;
207167 }
207168 rc = sqlite3Fts5IndexQuery(
207169 pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
207170 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
207171 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
207172 pNear->pColset,
207173 &p->pIter
207174 );
207175 assert( (rc==SQLITE_OK)==(p->pIter!=0) )((void) (0));
207176 if( rc!=SQLITE_OK0 ) return rc;
207177 if( 0==sqlite3Fts5IterEof(p->pIter) ){
207178 bHit = 1;
207179 }
207180 }
207181
207182 if( bHit==0 ){
207183 pNode->bEof = 1;
207184 return SQLITE_OK0;
207185 }
207186 }
207187 }
207188 }
207189
207190 pNode->bEof = 0;
207191 return SQLITE_OK0;
207192}
207193
207194/*
207195** If pExpr is an ASC iterator, this function returns a value with the
207196** same sign as:
207197**
207198** (iLhs - iRhs)
207199**
207200** Otherwise, if this is a DESC iterator, the opposite is returned:
207201**
207202** (iRhs - iLhs)
207203*/
207204static int fts5RowidCmp(
207205 Fts5Expr *pExpr,
207206 i64 iLhs,
207207 i64 iRhs
207208){
207209 assert( pExpr->bDesc==0 || pExpr->bDesc==1 )((void) (0));
207210 if( pExpr->bDesc==0 ){
207211 if( iLhs<iRhs ) return -1;
207212 return (iLhs > iRhs);
207213 }else{
207214 if( iLhs>iRhs ) return -1;
207215 return (iLhs < iRhs);
207216 }
207217}
207218
207219static void fts5ExprSetEof(Fts5ExprNode *pNode){
207220 int i;
207221 pNode->bEof = 1;
207222 pNode->bNomatch = 0;
207223 for(i=0; i<pNode->nChild; i++){
207224 fts5ExprSetEof(pNode->apChild[i]);
207225 }
207226}
207227
207228static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
207229 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
207230 Fts5ExprNearset *pNear = pNode->pNear;
207231 int i;
207232 for(i=0; i<pNear->nPhrase; i++){
207233 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
207234 pPhrase->poslist.n = 0;
207235 }
207236 }else{
207237 int i;
207238 for(i=0; i<pNode->nChild; i++){
207239 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
207240 }
207241 }
207242}
207243
207244
207245
207246/*
207247** Compare the values currently indicated by the two nodes as follows:
207248**
207249** res = (*p1) - (*p2)
207250**
207251** Nodes that point to values that come later in the iteration order are
207252** considered to be larger. Nodes at EOF are the largest of all.
207253**
207254** This means that if the iteration order is ASC, then numerically larger
207255** rowids are considered larger. Or if it is the default DESC, numerically
207256** smaller rowids are larger.
207257*/
207258static int fts5NodeCompare(
207259 Fts5Expr *pExpr,
207260 Fts5ExprNode *p1,
207261 Fts5ExprNode *p2
207262){
207263 if( p2->bEof ) return -1;
207264 if( p1->bEof ) return +1;
207265 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
207266}
207267
207268/*
207269** All individual term iterators in pNear are guaranteed to be valid when
207270** this function is called. This function checks if all term iterators
207271** point to the same rowid, and if not, advances them until they do.
207272** If an EOF is reached before this happens, *pbEof is set to true before
207273** returning.
207274**
207275** SQLITE_OK is returned if an error occurs, or an SQLite error code
207276** otherwise. It is not considered an error code if an iterator reaches
207277** EOF.
207278*/
207279static int fts5ExprNodeTest_STRING(
207280 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
207281 Fts5ExprNode *pNode
207282){
207283 Fts5ExprNearset *pNear = pNode->pNear;
207284 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
207285 int rc = SQLITE_OK0;
207286 i64 iLast; /* Lastest rowid any iterator points to */
207287 int i, j; /* Phrase and token index, respectively */
207288 int bMatch; /* True if all terms are at the same rowid */
207289 const int bDesc = pExpr->bDesc;
207290
207291 /* Check that this node should not be FTS5_TERM */
207292 assert( pNear->nPhrase>1((void) (0))
207293 || pNear->apPhrase[0]->nTerm>1((void) (0))
207294 || pNear->apPhrase[0]->aTerm[0].pSynonym((void) (0))
207295 || pNear->apPhrase[0]->aTerm[0].bFirst((void) (0))
207296 )((void) (0));
207297
207298 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
207299 ** iterator skips through rowids in the default ascending order, this means
207300 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
207301 ** means the minimum rowid. */
207302 if( pLeft->aTerm[0].pSynonym ){
207303 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
207304 }else{
207305 iLast = pLeft->aTerm[0].pIter->iRowid;
207306 }
207307
207308 do {
207309 bMatch = 1;
207310 for(i=0; i<pNear->nPhrase; i++){
207311 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
207312 for(j=0; j<pPhrase->nTerm; j++){
207313 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
207314 if( pTerm->pSynonym ){
207315 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
207316 if( iRowid==iLast ) continue;
207317 bMatch = 0;
207318 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
207319 pNode->bNomatch = 0;
207320 pNode->bEof = 1;
207321 return rc;
207322 }
207323 }else{
207324 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
207325 if( pIter->iRowid==iLast || pIter->bEof ) continue;
207326 bMatch = 0;
207327 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
207328 return rc;
207329 }
207330 }
207331 }
207332 }
207333 }while( bMatch==0 );
207334
207335 pNode->iRowid = iLast;
207336 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK0);
207337 assert( pNode->bEof==0 || pNode->bNomatch==0 )((void) (0));
207338
207339 return rc;
207340}
207341
207342/*
207343** Advance the first term iterator in the first phrase of pNear. Set output
207344** variable *pbEof to true if it reaches EOF or if an error occurs.
207345**
207346** Return SQLITE_OK if successful, or an SQLite error code if an error
207347** occurs.
207348*/
207349static int fts5ExprNodeNext_STRING(
207350 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
207351 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
207352 int bFromValid,
207353 i64 iFrom
207354){
207355 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
207356 int rc = SQLITE_OK0;
207357
207358 pNode->bNomatch = 0;
207359 if( pTerm->pSynonym ){
207360 int bEof = 1;
207361 Fts5ExprTerm *p;
207362
207363 /* Find the firstest rowid any synonym points to. */
207364 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
207365
207366 /* Advance each iterator that currently points to iRowid. Or, if iFrom
207367 ** is valid - each iterator that points to a rowid before iFrom. */
207368 for(p=pTerm; p; p=p->pSynonym){
207369 if( sqlite3Fts5IterEof(p->pIter)==0 ){
207370 i64 ii = p->pIter->iRowid;
207371 if( ii==iRowid
207372 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
207373 ){
207374 if( bFromValid ){
207375 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
207376 }else{
207377 rc = sqlite3Fts5IterNext(p->pIter);
207378 }
207379 if( rc!=SQLITE_OK0 ) break;
207380 if( sqlite3Fts5IterEof(p->pIter)==0 ){
207381 bEof = 0;
207382 }
207383 }else{
207384 bEof = 0;
207385 }
207386 }
207387 }
207388
207389 /* Set the EOF flag if either all synonym iterators are at EOF or an
207390 ** error has occurred. */
207391 pNode->bEof = (rc || bEof);
207392 }else{
207393 Fts5IndexIter *pIter = pTerm->pIter;
207394
207395 assert( Fts5NodeIsString(pNode) )((void) (0));
207396 if( bFromValid ){
207397 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
207398 }else{
207399 rc = sqlite3Fts5IterNext(pIter);
207400 }
207401
207402 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
207403 }
207404
207405 if( pNode->bEof==0 ){
207406 assert( rc==SQLITE_OK )((void) (0));
207407 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
207408 }
207409
207410 return rc;
207411}
207412
207413
207414static int fts5ExprNodeTest_TERM(
207415 Fts5Expr *pExpr, /* Expression that pNear is a part of */
207416 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
207417){
207418 /* As this "NEAR" object is actually a single phrase that consists
207419 ** of a single term only, grab pointers into the poslist managed by the
207420 ** fts5_index.c iterator object. This is much faster than synthesizing
207421 ** a new poslist the way we have to for more complicated phrase or NEAR
207422 ** expressions. */
207423 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
207424 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
207425
207426 assert( pNode->eType==FTS5_TERM )((void) (0));
207427 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 )((void) (0));
207428 assert( pPhrase->aTerm[0].pSynonym==0 )((void) (0));
207429
207430 pPhrase->poslist.n = pIter->nData;
207431 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
207432 pPhrase->poslist.p = (u8*)pIter->pData;
207433 }
207434 pNode->iRowid = pIter->iRowid;
207435 pNode->bNomatch = (pPhrase->poslist.n==0);
207436 return SQLITE_OK0;
207437}
207438
207439/*
207440** xNext() method for a node of type FTS5_TERM.
207441*/
207442static int fts5ExprNodeNext_TERM(
207443 Fts5Expr *pExpr,
207444 Fts5ExprNode *pNode,
207445 int bFromValid,
207446 i64 iFrom
207447){
207448 int rc;
207449 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
207450
207451 assert( pNode->bEof==0 )((void) (0));
207452 if( bFromValid ){
207453 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
207454 }else{
207455 rc = sqlite3Fts5IterNext(pIter);
207456 }
207457 if( rc==SQLITE_OK0 && sqlite3Fts5IterEof(pIter)==0 ){
207458 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
207459 }else{
207460 pNode->bEof = 1;
207461 pNode->bNomatch = 0;
207462 }
207463 return rc;
207464}
207465
207466static void fts5ExprNodeTest_OR(
207467 Fts5Expr *pExpr, /* Expression of which pNode is a part */
207468 Fts5ExprNode *pNode /* Expression node to test */
207469){
207470 Fts5ExprNode *pNext = pNode->apChild[0];
207471 int i;
207472
207473 for(i=1; i<pNode->nChild; i++){
207474 Fts5ExprNode *pChild = pNode->apChild[i];
207475 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
207476 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
207477 pNext = pChild;
207478 }
207479 }
207480 pNode->iRowid = pNext->iRowid;
207481 pNode->bEof = pNext->bEof;
207482 pNode->bNomatch = pNext->bNomatch;
207483}
207484
207485static int fts5ExprNodeNext_OR(
207486 Fts5Expr *pExpr,
207487 Fts5ExprNode *pNode,
207488 int bFromValid,
207489 i64 iFrom
207490){
207491 int i;
207492 i64 iLast = pNode->iRowid;
207493
207494 for(i=0; i<pNode->nChild; i++){
207495 Fts5ExprNode *p1 = pNode->apChild[i];
207496 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 )((void) (0));
207497 if( p1->bEof==0 ){
207498 if( (p1->iRowid==iLast)
207499 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
207500 ){
207501 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
207502 if( rc!=SQLITE_OK0 ){
207503 pNode->bNomatch = 0;
207504 return rc;
207505 }
207506 }
207507 }
207508 }
207509
207510 fts5ExprNodeTest_OR(pExpr, pNode);
207511 return SQLITE_OK0;
207512}
207513
207514/*
207515** Argument pNode is an FTS5_AND node.
207516*/
207517static int fts5ExprNodeTest_AND(
207518 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
207519 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
207520){
207521 int iChild;
207522 i64 iLast = pAnd->iRowid;
207523 int rc = SQLITE_OK0;
207524 int bMatch;
207525
207526 assert( pAnd->bEof==0 )((void) (0));
207527 do {
207528 pAnd->bNomatch = 0;
207529 bMatch = 1;
207530 for(iChild=0; iChild<pAnd->nChild; iChild++){
207531 Fts5ExprNode *pChild = pAnd->apChild[iChild];
207532 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
207533 if( cmp>0 ){
207534 /* Advance pChild until it points to iLast or laster */
207535 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
207536 if( rc!=SQLITE_OK0 ){
207537 pAnd->bNomatch = 0;
207538 return rc;
207539 }
207540 }
207541
207542 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
207543 ** the child node is guaranteed to have advanced at least as far as
207544 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
207545 ** new lastest rowid seen so far. */
207546 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 )((void) (0));
207547 if( pChild->bEof ){
207548 fts5ExprSetEof(pAnd);
207549 bMatch = 1;
207550 break;
207551 }else if( iLast!=pChild->iRowid ){
207552 bMatch = 0;
207553 iLast = pChild->iRowid;
207554 }
207555
207556 if( pChild->bNomatch ){
207557 pAnd->bNomatch = 1;
207558 }
207559 }
207560 }while( bMatch==0 );
207561
207562 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
207563 fts5ExprNodeZeroPoslist(pAnd);
207564 }
207565 pAnd->iRowid = iLast;
207566 return SQLITE_OK0;
207567}
207568
207569static int fts5ExprNodeNext_AND(
207570 Fts5Expr *pExpr,
207571 Fts5ExprNode *pNode,
207572 int bFromValid,
207573 i64 iFrom
207574){
207575 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
207576 if( rc==SQLITE_OK0 ){
207577 rc = fts5ExprNodeTest_AND(pExpr, pNode);
207578 }else{
207579 pNode->bNomatch = 0;
207580 }
207581 return rc;
207582}
207583
207584static int fts5ExprNodeTest_NOT(
207585 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
207586 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
207587){
207588 int rc = SQLITE_OK0;
207589 Fts5ExprNode *p1 = pNode->apChild[0];
207590 Fts5ExprNode *p2 = pNode->apChild[1];
207591 assert( pNode->nChild==2 )((void) (0));
207592
207593 while( rc==SQLITE_OK0 && p1->bEof==0 ){
207594 int cmp = fts5NodeCompare(pExpr, p1, p2);
207595 if( cmp>0 ){
207596 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
207597 cmp = fts5NodeCompare(pExpr, p1, p2);
207598 }
207599 assert( rc!=SQLITE_OK || cmp<=0 )((void) (0));
207600 if( cmp || p2->bNomatch ) break;
207601 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
207602 }
207603 pNode->bEof = p1->bEof;
207604 pNode->bNomatch = p1->bNomatch;
207605 pNode->iRowid = p1->iRowid;
207606 if( p1->bEof ){
207607 fts5ExprNodeZeroPoslist(p2);
207608 }
207609 return rc;
207610}
207611
207612static int fts5ExprNodeNext_NOT(
207613 Fts5Expr *pExpr,
207614 Fts5ExprNode *pNode,
207615 int bFromValid,
207616 i64 iFrom
207617){
207618 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
207619 if( rc==SQLITE_OK0 ){
207620 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
207621 }
207622 if( rc!=SQLITE_OK0 ){
207623 pNode->bNomatch = 0;
207624 }
207625 return rc;
207626}
207627
207628/*
207629** If pNode currently points to a match, this function returns SQLITE_OK
207630** without modifying it. Otherwise, pNode is advanced until it does point
207631** to a match or EOF is reached.
207632*/
207633static int fts5ExprNodeTest(
207634 Fts5Expr *pExpr, /* Expression of which pNode is a part */
207635 Fts5ExprNode *pNode /* Expression node to test */
207636){
207637 int rc = SQLITE_OK0;
207638 if( pNode->bEof==0 ){
207639 switch( pNode->eType ){
207640
207641 case FTS5_STRING: {
207642 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
207643 break;
207644 }
207645
207646 case FTS5_TERM: {
207647 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
207648 break;
207649 }
207650
207651 case FTS5_AND: {
207652 rc = fts5ExprNodeTest_AND(pExpr, pNode);
207653 break;
207654 }
207655
207656 case FTS5_OR: {
207657 fts5ExprNodeTest_OR(pExpr, pNode);
207658 break;
207659 }
207660
207661 default: assert( pNode->eType==FTS5_NOT )((void) (0)); {
207662 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
207663 break;
207664 }
207665 }
207666 }
207667 return rc;
207668}
207669
207670
207671/*
207672** Set node pNode, which is part of expression pExpr, to point to the first
207673** match. If there are no matches, set the Node.bEof flag to indicate EOF.
207674**
207675** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
207676** It is not an error if there are no matches.
207677*/
207678static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
207679 int rc = SQLITE_OK0;
207680 pNode->bEof = 0;
207681 pNode->bNomatch = 0;
207682
207683 if( Fts5NodeIsString(pNode) ){
207684 /* Initialize all term iterators in the NEAR object. */
207685 rc = fts5ExprNearInitAll(pExpr, pNode);
207686 }else if( pNode->xNext==0 ){
207687 pNode->bEof = 1;
207688 }else{
207689 int i;
207690 int nEof = 0;
207691 for(i=0; i<pNode->nChild && rc==SQLITE_OK0; i++){
207692 Fts5ExprNode *pChild = pNode->apChild[i];
207693 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
207694 assert( pChild->bEof==0 || pChild->bEof==1 )((void) (0));
207695 nEof += pChild->bEof;
207696 }
207697 pNode->iRowid = pNode->apChild[0]->iRowid;
207698
207699 switch( pNode->eType ){
207700 case FTS5_AND:
207701 if( nEof>0 ) fts5ExprSetEof(pNode);
207702 break;
207703
207704 case FTS5_OR:
207705 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
207706 break;
207707
207708 default:
207709 assert( pNode->eType==FTS5_NOT )((void) (0));
207710 pNode->bEof = pNode->apChild[0]->bEof;
207711 break;
207712 }
207713 }
207714
207715 if( rc==SQLITE_OK0 ){
207716 rc = fts5ExprNodeTest(pExpr, pNode);
207717 }
207718 return rc;
207719}
207720
207721
207722/*
207723** Begin iterating through the set of documents in index pIdx matched by
207724** the MATCH expression passed as the first argument. If the "bDesc"
207725** parameter is passed a non-zero value, iteration is in descending rowid
207726** order. Or, if it is zero, in ascending order.
207727**
207728** If iterating in ascending rowid order (bDesc==0), the first document
207729** visited is that with the smallest rowid that is larger than or equal
207730** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
207731** then the first document visited must have a rowid smaller than or
207732** equal to iFirst.
207733**
207734** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
207735** is not considered an error if the query does not match any documents.
207736*/
207737static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
207738 Fts5ExprNode *pRoot = p->pRoot;
207739 int rc; /* Return code */
207740
207741 p->pIndex = pIdx;
207742 p->bDesc = bDesc;
207743 rc = fts5ExprNodeFirst(p, pRoot);
207744
207745 /* If not at EOF but the current rowid occurs earlier than iFirst in
207746 ** the iteration order, move to document iFirst or later. */
207747 if( rc==SQLITE_OK0
207748 && 0==pRoot->bEof
207749 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
207750 ){
207751 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
207752 }
207753
207754 /* If the iterator is not at a real match, skip forward until it is. */
207755 while( pRoot->bNomatch ){
207756 assert( pRoot->bEof==0 && rc==SQLITE_OK )((void) (0));
207757 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
207758 }
207759 return rc;
207760}
207761
207762/*
207763** Move to the next document
207764**
207765** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
207766** is not considered an error if the query does not match any documents.
207767*/
207768static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
207769 int rc;
207770 Fts5ExprNode *pRoot = p->pRoot;
207771 assert( pRoot->bEof==0 && pRoot->bNomatch==0 )((void) (0));
207772 do {
207773 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
207774 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) )((void) (0));
207775 }while( pRoot->bNomatch );
207776 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
207777 pRoot->bEof = 1;
207778 }
207779 return rc;
207780}
207781
207782static int sqlite3Fts5ExprEof(Fts5Expr *p){
207783 return p->pRoot->bEof;
207784}
207785
207786static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
207787 return p->pRoot->iRowid;
207788}
207789
207790static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
207791 int rc = SQLITE_OK0;
207792 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
207793 return rc;
207794}
207795
207796/*
207797** Free the phrase object passed as the only argument.
207798*/
207799static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
207800 if( pPhrase ){
207801 int i;
207802 for(i=0; i<pPhrase->nTerm; i++){
207803 Fts5ExprTerm *pSyn;
207804 Fts5ExprTerm *pNext;
207805 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
207806 sqlite3_free(pTerm->zTerm);
207807 sqlite3Fts5IterClose(pTerm->pIter);
207808 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
207809 pNext = pSyn->pSynonym;
207810 sqlite3Fts5IterClose(pSyn->pIter);
207811 fts5BufferFree((Fts5Buffer*)&pSyn[1]);
207812 sqlite3_free(pSyn);
207813 }
207814 }
207815 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
207816 sqlite3_free(pPhrase);
207817 }
207818}
207819
207820/*
207821** Set the "bFirst" flag on the first token of the phrase passed as the
207822** only argument.
207823*/
207824static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
207825 if( pPhrase && pPhrase->nTerm ){
207826 pPhrase->aTerm[0].bFirst = 1;
207827 }
207828}
207829
207830/*
207831** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
207832** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
207833** appended to it and the results returned.
207834**
207835** If an OOM error occurs, both the pNear and pPhrase objects are freed and
207836** NULL returned.
207837*/
207838static Fts5ExprNearset *sqlite3Fts5ParseNearset(
207839 Fts5Parse *pParse, /* Parse context */
207840 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
207841 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
207842){
207843 const int SZALLOC = 8;
207844 Fts5ExprNearset *pRet = 0;
207845
207846 if( pParse->rc==SQLITE_OK0 ){
207847 if( pPhrase==0 ){
207848 return pNear;
207849 }
207850 if( pNear==0 ){
207851 sqlite3_int64 nByte;
207852 nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
207853 pRet = sqlite3_malloc64(nByte);
207854 if( pRet==0 ){
207855 pParse->rc = SQLITE_NOMEM7;
207856 }else{
207857 memset(pRet, 0, (size_t)nByte);
207858 }
207859 }else if( (pNear->nPhrase % SZALLOC)==0 ){
207860 int nNew = pNear->nPhrase + SZALLOC;
207861 sqlite3_int64 nByte;
207862
207863 nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
207864 pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte);
207865 if( pRet==0 ){
207866 pParse->rc = SQLITE_NOMEM7;
207867 }
207868 }else{
207869 pRet = pNear;
207870 }
207871 }
207872
207873 if( pRet==0 ){
207874 assert( pParse->rc!=SQLITE_OK )((void) (0));
207875 sqlite3Fts5ParseNearsetFree(pNear);
207876 sqlite3Fts5ParsePhraseFree(pPhrase);
207877 }else{
207878 if( pRet->nPhrase>0 ){
207879 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
207880 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] )((void) (0));
207881 if( pPhrase->nTerm==0 ){
207882 fts5ExprPhraseFree(pPhrase);
207883 pRet->nPhrase--;
207884 pParse->nPhrase--;
207885 pPhrase = pLast;
207886 }else if( pLast->nTerm==0 ){
207887 fts5ExprPhraseFree(pLast);
207888 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
207889 pParse->nPhrase--;
207890 pRet->nPhrase--;
207891 }
207892 }
207893 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
207894 }
207895 return pRet;
207896}
207897
207898typedef struct TokenCtx TokenCtx;
207899struct TokenCtx {
207900 Fts5ExprPhrase *pPhrase;
207901 int rc;
207902};
207903
207904/*
207905** Callback for tokenizing terms used by ParseTerm().
207906*/
207907static int fts5ParseTokenize(
207908 void *pContext, /* Pointer to Fts5InsertCtx object */
207909 int tflags, /* Mask of FTS5_TOKEN_* flags */
207910 const char *pToken, /* Buffer containing token */
207911 int nToken, /* Size of token in bytes */
207912 int iUnused1, /* Start offset of token */
207913 int iUnused2 /* End offset of token */
207914){
207915 int rc = SQLITE_OK0;
207916 const int SZALLOC = 8;
207917 TokenCtx *pCtx = (TokenCtx*)pContext;
207918 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
207919
207920 UNUSED_PARAM2(iUnused1, iUnused2);
207921
207922 /* If an error has already occurred, this is a no-op */
207923 if( pCtx->rc!=SQLITE_OK0 ) return pCtx->rc;
207924 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
207925
207926 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED0x0001) ){
207927 Fts5ExprTerm *pSyn;
207928 sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
207929 pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte);
207930 if( pSyn==0 ){
207931 rc = SQLITE_NOMEM7;
207932 }else{
207933 memset(pSyn, 0, (size_t)nByte);
207934 pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
207935 memcpy(pSyn->zTerm, pToken, nToken);
207936 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
207937 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
207938 }
207939 }else{
207940 Fts5ExprTerm *pTerm;
207941 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
207942 Fts5ExprPhrase *pNew;
207943 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
207944
207945 pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase,
207946 sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
207947 );
207948 if( pNew==0 ){
207949 rc = SQLITE_NOMEM7;
207950 }else{
207951 if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
207952 pCtx->pPhrase = pPhrase = pNew;
207953 pNew->nTerm = nNew - SZALLOC;
207954 }
207955 }
207956
207957 if( rc==SQLITE_OK0 ){
207958 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
207959 memset(pTerm, 0, sizeof(Fts5ExprTerm));
207960 pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
207961 }
207962 }
207963
207964 pCtx->rc = rc;
207965 return rc;
207966}
207967
207968
207969/*
207970** Free the phrase object passed as the only argument.
207971*/
207972static void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
207973 fts5ExprPhraseFree(pPhrase);
207974}
207975
207976/*
207977** Free the phrase object passed as the second argument.
207978*/
207979static void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
207980 if( pNear ){
207981 int i;
207982 for(i=0; i<pNear->nPhrase; i++){
207983 fts5ExprPhraseFree(pNear->apPhrase[i]);
207984 }
207985 sqlite3_free(pNear->pColset);
207986 sqlite3_free(pNear);
207987 }
207988}
207989
207990static void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
207991 assert( pParse->pExpr==0 )((void) (0));
207992 pParse->pExpr = p;
207993}
207994
207995/*
207996** This function is called by the parser to process a string token. The
207997** string may or may not be quoted. In any case it is tokenized and a
207998** phrase object consisting of all tokens returned.
207999*/
208000static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
208001 Fts5Parse *pParse, /* Parse context */
208002 Fts5ExprPhrase *pAppend, /* Phrase to append to */
208003 Fts5Token *pToken, /* String to tokenize */
208004 int bPrefix /* True if there is a trailing "*" */
208005){
208006 Fts5Config *pConfig = pParse->pConfig;
208007 TokenCtx sCtx; /* Context object passed to callback */
208008 int rc; /* Tokenize return code */
208009 char *z = 0;
208010
208011 memset(&sCtx, 0, sizeof(TokenCtx));
208012 sCtx.pPhrase = pAppend;
208013
208014 rc = fts5ParseStringFromToken(pToken, &z);
208015 if( rc==SQLITE_OK0 ){
208016 int flags = FTS5_TOKENIZE_QUERY0x0001 | (bPrefix ? FTS5_TOKENIZE_PREFIX0x0002 : 0);
208017 int n;
208018 sqlite3Fts5Dequote(z);
208019 n = (int)strlen(z);
208020 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
208021 }
208022 sqlite3_free(z);
208023 if( rc || (rc = sCtx.rc) ){
208024 pParse->rc = rc;
208025 fts5ExprPhraseFree(sCtx.pPhrase);
208026 sCtx.pPhrase = 0;
208027 }else{
208028
208029 if( pAppend==0 ){
208030 if( (pParse->nPhrase % 8)==0 ){
208031 sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
208032 Fts5ExprPhrase **apNew;
208033 apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte);
208034 if( apNew==0 ){
208035 pParse->rc = SQLITE_NOMEM7;
208036 fts5ExprPhraseFree(sCtx.pPhrase);
208037 return 0;
208038 }
208039 pParse->apPhrase = apNew;
208040 }
208041 pParse->nPhrase++;
208042 }
208043
208044 if( sCtx.pPhrase==0 ){
208045 /* This happens when parsing a token or quoted phrase that contains
208046 ** no token characters at all. (e.g ... MATCH '""'). */
208047 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
208048 }else if( sCtx.pPhrase->nTerm ){
208049 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
208050 }
208051 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
208052 }
208053
208054 return sCtx.pPhrase;
208055}
208056
208057/*
208058** Create a new FTS5 expression by cloning phrase iPhrase of the
208059** expression passed as the second argument.
208060*/
208061static int sqlite3Fts5ExprClonePhrase(
208062 Fts5Expr *pExpr,
208063 int iPhrase,
208064 Fts5Expr **ppNew
208065){
208066 int rc = SQLITE_OK0; /* Return code */
208067 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
208068 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
208069 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
208070
208071 pOrig = pExpr->apExprPhrase[iPhrase];
208072 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
208073 if( rc==SQLITE_OK0 ){
208074 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
208075 sizeof(Fts5ExprPhrase*));
208076 }
208077 if( rc==SQLITE_OK0 ){
208078 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc,
208079 sizeof(Fts5ExprNode));
208080 }
208081 if( rc==SQLITE_OK0 ){
208082 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
208083 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
208084 }
208085 if( rc==SQLITE_OK0 ){
208086 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
208087 if( pColsetOrig ){
208088 sqlite3_int64 nByte;
208089 Fts5Colset *pColset;
208090 nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
208091 pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
208092 if( pColset ){
208093 memcpy(pColset, pColsetOrig, (size_t)nByte);
208094 }
208095 pNew->pRoot->pNear->pColset = pColset;
208096 }
208097 }
208098
208099 if( pOrig->nTerm ){
208100 int i; /* Used to iterate through phrase terms */
208101 for(i=0; rc==SQLITE_OK0 && i<pOrig->nTerm; i++){
208102 int tflags = 0;
208103 Fts5ExprTerm *p;
208104 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK0; p=p->pSynonym){
208105 const char *zTerm = p->zTerm;
208106 rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
208107 0, 0);
208108 tflags = FTS5_TOKEN_COLOCATED0x0001;
208109 }
208110 if( rc==SQLITE_OK0 ){
208111 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
208112 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
208113 }
208114 }
208115 }else{
208116 /* This happens when parsing a token or quoted phrase that contains
208117 ** no token characters at all. (e.g ... MATCH '""'). */
208118 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
208119 }
208120
208121 if( rc==SQLITE_OK0 ){
208122 /* All the allocations succeeded. Put the expression object together. */
208123 pNew->pIndex = pExpr->pIndex;
208124 pNew->pConfig = pExpr->pConfig;
208125 pNew->nPhrase = 1;
208126 pNew->apExprPhrase[0] = sCtx.pPhrase;
208127 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
208128 pNew->pRoot->pNear->nPhrase = 1;
208129 sCtx.pPhrase->pNode = pNew->pRoot;
208130
208131 if( pOrig->nTerm==1
208132 && pOrig->aTerm[0].pSynonym==0
208133 && pOrig->aTerm[0].bFirst==0
208134 ){
208135 pNew->pRoot->eType = FTS5_TERM;
208136 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
208137 }else{
208138 pNew->pRoot->eType = FTS5_STRING;
208139 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
208140 }
208141 }else{
208142 sqlite3Fts5ExprFree(pNew);
208143 fts5ExprPhraseFree(sCtx.pPhrase);
208144 pNew = 0;
208145 }
208146
208147 *ppNew = pNew;
208148 return rc;
208149}
208150
208151
208152/*
208153** Token pTok has appeared in a MATCH expression where the NEAR operator
208154** is expected. If token pTok does not contain "NEAR", store an error
208155** in the pParse object.
208156*/
208157static void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
208158 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
208159 sqlite3Fts5ParseError(
208160 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
208161 );
208162 }
208163}
208164
208165static void sqlite3Fts5ParseSetDistance(
208166 Fts5Parse *pParse,
208167 Fts5ExprNearset *pNear,
208168 Fts5Token *p
208169){
208170 if( pNear ){
208171 int nNear = 0;
208172 int i;
208173 if( p->n ){
208174 for(i=0; i<p->n; i++){
208175 char c = (char)p->p[i];
208176 if( c<'0' || c>'9' ){
208177 sqlite3Fts5ParseError(
208178 pParse, "expected integer, got \"%.*s\"", p->n, p->p
208179 );
208180 return;
208181 }
208182 nNear = nNear * 10 + (p->p[i] - '0');
208183 }
208184 }else{
208185 nNear = FTS5_DEFAULT_NEARDIST;
208186 }
208187 pNear->nNear = nNear;
208188 }
208189}
208190
208191/*
208192** The second argument passed to this function may be NULL, or it may be
208193** an existing Fts5Colset object. This function returns a pointer to
208194** a new colset object containing the contents of (p) with new value column
208195** number iCol appended.
208196**
208197** If an OOM error occurs, store an error code in pParse and return NULL.
208198** The old colset object (if any) is not freed in this case.
208199*/
208200static Fts5Colset *fts5ParseColset(
208201 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
208202 Fts5Colset *p, /* Existing colset object */
208203 int iCol /* New column to add to colset object */
208204){
208205 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
208206 Fts5Colset *pNew; /* New colset object to return */
208207
208208 assert( pParse->rc==SQLITE_OK )((void) (0));
208209 assert( iCol>=0 && iCol<pParse->pConfig->nCol )((void) (0));
208210
208211 pNew = sqlite3_realloc64(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
208212 if( pNew==0 ){
208213 pParse->rc = SQLITE_NOMEM7;
208214 }else{
208215 int *aiCol = pNew->aiCol;
208216 int i, j;
208217 for(i=0; i<nCol; i++){
208218 if( aiCol[i]==iCol ) return pNew;
208219 if( aiCol[i]>iCol ) break;
208220 }
208221 for(j=nCol; j>i; j--){
208222 aiCol[j] = aiCol[j-1];
208223 }
208224 aiCol[i] = iCol;
208225 pNew->nCol = nCol+1;
208226
208227#ifndef NDEBUG1
208228 /* Check that the array is in order and contains no duplicate entries. */
208229 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] )((void) (0));
208230#endif
208231 }
208232
208233 return pNew;
208234}
208235
208236/*
208237** Allocate and return an Fts5Colset object specifying the inverse of
208238** the colset passed as the second argument. Free the colset passed
208239** as the second argument before returning.
208240*/
208241static Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
208242 Fts5Colset *pRet;
208243 int nCol = pParse->pConfig->nCol;
208244
208245 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
208246 sizeof(Fts5Colset) + sizeof(int)*nCol
208247 );
208248 if( pRet ){
208249 int i;
208250 int iOld = 0;
208251 for(i=0; i<nCol; i++){
208252 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
208253 pRet->aiCol[pRet->nCol++] = i;
208254 }else{
208255 iOld++;
208256 }
208257 }
208258 }
208259
208260 sqlite3_free(p);
208261 return pRet;
208262}
208263
208264static Fts5Colset *sqlite3Fts5ParseColset(
208265 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
208266 Fts5Colset *pColset, /* Existing colset object */
208267 Fts5Token *p
208268){
208269 Fts5Colset *pRet = 0;
208270 int iCol;
208271 char *z; /* Dequoted copy of token p */
208272
208273 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
208274 if( pParse->rc==SQLITE_OK0 ){
208275 Fts5Config *pConfig = pParse->pConfig;
208276 sqlite3Fts5Dequote(z);
208277 for(iCol=0; iCol<pConfig->nCol; iCol++){
208278 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
208279 }
208280 if( iCol==pConfig->nCol ){
208281 sqlite3Fts5ParseError(pParse, "no such column: %s", z);
208282 }else{
208283 pRet = fts5ParseColset(pParse, pColset, iCol);
208284 }
208285 sqlite3_free(z);
208286 }
208287
208288 if( pRet==0 ){
208289 assert( pParse->rc!=SQLITE_OK )((void) (0));
208290 sqlite3_free(pColset);
208291 }
208292
208293 return pRet;
208294}
208295
208296/*
208297** If argument pOrig is NULL, or if (*pRc) is set to anything other than
208298** SQLITE_OK when this function is called, NULL is returned.
208299**
208300** Otherwise, a copy of (*pOrig) is made into memory obtained from
208301** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
208302** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
208303*/
208304static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
208305 Fts5Colset *pRet;
208306 if( pOrig ){
208307 sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
208308 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
208309 if( pRet ){
208310 memcpy(pRet, pOrig, (size_t)nByte);
208311 }
208312 }else{
208313 pRet = 0;
208314 }
208315 return pRet;
208316}
208317
208318/*
208319** Remove from colset pColset any columns that are not also in colset pMerge.
208320*/
208321static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
208322 int iIn = 0; /* Next input in pColset */
208323 int iMerge = 0; /* Next input in pMerge */
208324 int iOut = 0; /* Next output slot in pColset */
208325
208326 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
208327 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
208328 if( iDiff==0 ){
208329 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
208330 iMerge++;
208331 iIn++;
208332 }else if( iDiff>0 ){
208333 iMerge++;
208334 }else{
208335 iIn++;
208336 }
208337 }
208338 pColset->nCol = iOut;
208339}
208340
208341/*
208342** Recursively apply colset pColset to expression node pNode and all of
208343** its decendents. If (*ppFree) is not NULL, it contains a spare copy
208344** of pColset. This function may use the spare copy and set (*ppFree) to
208345** zero, or it may create copies of pColset using fts5CloneColset().
208346*/
208347static void fts5ParseSetColset(
208348 Fts5Parse *pParse,
208349 Fts5ExprNode *pNode,
208350 Fts5Colset *pColset,
208351 Fts5Colset **ppFree
208352){
208353 if( pParse->rc==SQLITE_OK0 ){
208354 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING((void) (0))
208355 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR((void) (0))
208356 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF((void) (0))
208357 )((void) (0));
208358 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
208359 Fts5ExprNearset *pNear = pNode->pNear;
208360 if( pNear->pColset ){
208361 fts5MergeColset(pNear->pColset, pColset);
208362 if( pNear->pColset->nCol==0 ){
208363 pNode->eType = FTS5_EOF;
208364 pNode->xNext = 0;
208365 }
208366 }else if( *ppFree ){
208367 pNear->pColset = pColset;
208368 *ppFree = 0;
208369 }else{
208370 pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
208371 }
208372 }else{
208373 int i;
208374 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 )((void) (0));
208375 for(i=0; i<pNode->nChild; i++){
208376 fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
208377 }
208378 }
208379 }
208380}
208381
208382/*
208383** Apply colset pColset to expression node pExpr and all of its descendents.
208384*/
208385static void sqlite3Fts5ParseSetColset(
208386 Fts5Parse *pParse,
208387 Fts5ExprNode *pExpr,
208388 Fts5Colset *pColset
208389){
208390 Fts5Colset *pFree = pColset;
208391 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
208392 pParse->rc = SQLITE_ERROR1;
208393 pParse->zErr = sqlite3_mprintf(
208394 "fts5: column queries are not supported (detail=none)"
208395 );
208396 }else{
208397 fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
208398 }
208399 sqlite3_free(pFree);
208400}
208401
208402static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
208403 switch( pNode->eType ){
208404 case FTS5_STRING: {
208405 Fts5ExprNearset *pNear = pNode->pNear;
208406 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
208407 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
208408 && pNear->apPhrase[0]->aTerm[0].bFirst==0
208409 ){
208410 pNode->eType = FTS5_TERM;
208411 pNode->xNext = fts5ExprNodeNext_TERM;
208412 }else{
208413 pNode->xNext = fts5ExprNodeNext_STRING;
208414 }
208415 break;
208416 };
208417
208418 case FTS5_OR: {
208419 pNode->xNext = fts5ExprNodeNext_OR;
208420 break;
208421 };
208422
208423 case FTS5_AND: {
208424 pNode->xNext = fts5ExprNodeNext_AND;
208425 break;
208426 };
208427
208428 default: assert( pNode->eType==FTS5_NOT )((void) (0)); {
208429 pNode->xNext = fts5ExprNodeNext_NOT;
208430 break;
208431 };
208432 }
208433}
208434
208435static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
208436 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
208437 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
208438 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
208439 p->nChild += pSub->nChild;
208440 sqlite3_free(pSub);
208441 }else{
208442 p->apChild[p->nChild++] = pSub;
208443 }
208444}
208445
208446/*
208447** Allocate and return a new expression object. If anything goes wrong (i.e.
208448** OOM error), leave an error code in pParse and return NULL.
208449*/
208450static Fts5ExprNode *sqlite3Fts5ParseNode(
208451 Fts5Parse *pParse, /* Parse context */
208452 int eType, /* FTS5_STRING, AND, OR or NOT */
208453 Fts5ExprNode *pLeft, /* Left hand child expression */
208454 Fts5ExprNode *pRight, /* Right hand child expression */
208455 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
208456){
208457 Fts5ExprNode *pRet = 0;
208458
208459 if( pParse->rc==SQLITE_OK0 ){
208460 int nChild = 0; /* Number of children of returned node */
208461 sqlite3_int64 nByte; /* Bytes of space to allocate for this node */
208462
208463 assert( (eType!=FTS5_STRING && !pNear)((void) (0))
208464 || (eType==FTS5_STRING && !pLeft && !pRight)((void) (0))
208465 )((void) (0));
208466 if( eType==FTS5_STRING && pNear==0 ) return 0;
208467 if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
208468 if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
208469
208470 if( eType==FTS5_NOT ){
208471 nChild = 2;
208472 }else if( eType==FTS5_AND || eType==FTS5_OR ){
208473 nChild = 2;
208474 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
208475 if( pRight->eType==eType ) nChild += pRight->nChild-1;
208476 }
208477
208478 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
208479 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
208480
208481 if( pRet ){
208482 pRet->eType = eType;
208483 pRet->pNear = pNear;
208484 fts5ExprAssignXNext(pRet);
208485 if( eType==FTS5_STRING ){
208486 int iPhrase;
208487 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
208488 pNear->apPhrase[iPhrase]->pNode = pRet;
208489 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
208490 pRet->xNext = 0;
208491 pRet->eType = FTS5_EOF;
208492 }
208493 }
208494
208495 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
208496 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
208497 if( pNear->nPhrase!=1
208498 || pPhrase->nTerm>1
208499 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
208500 ){
208501 assert( pParse->rc==SQLITE_OK )((void) (0));
208502 pParse->rc = SQLITE_ERROR1;
208503 assert( pParse->zErr==0 )((void) (0));
208504 pParse->zErr = sqlite3_mprintf(
208505 "fts5: %s queries are not supported (detail!=full)",
208506 pNear->nPhrase==1 ? "phrase": "NEAR"
208507 );
208508 sqlite3_free(pRet);
208509 pRet = 0;
208510 }
208511 }
208512 }else{
208513 fts5ExprAddChildren(pRet, pLeft);
208514 fts5ExprAddChildren(pRet, pRight);
208515 }
208516 }
208517 }
208518
208519 if( pRet==0 ){
208520 assert( pParse->rc!=SQLITE_OK )((void) (0));
208521 sqlite3Fts5ParseNodeFree(pLeft);
208522 sqlite3Fts5ParseNodeFree(pRight);
208523 sqlite3Fts5ParseNearsetFree(pNear);
208524 }
208525 return pRet;
208526}
208527
208528static Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
208529 Fts5Parse *pParse, /* Parse context */
208530 Fts5ExprNode *pLeft, /* Left hand child expression */
208531 Fts5ExprNode *pRight /* Right hand child expression */
208532){
208533 Fts5ExprNode *pRet = 0;
208534 Fts5ExprNode *pPrev;
208535
208536 if( pParse->rc ){
208537 sqlite3Fts5ParseNodeFree(pLeft);
208538 sqlite3Fts5ParseNodeFree(pRight);
208539 }else{
208540
208541 assert( pLeft->eType==FTS5_STRING((void) (0))
208542 || pLeft->eType==FTS5_TERM((void) (0))
208543 || pLeft->eType==FTS5_EOF((void) (0))
208544 || pLeft->eType==FTS5_AND((void) (0))
208545 )((void) (0));
208546 assert( pRight->eType==FTS5_STRING((void) (0))
208547 || pRight->eType==FTS5_TERM((void) (0))
208548 || pRight->eType==FTS5_EOF((void) (0))
208549 )((void) (0));
208550
208551 if( pLeft->eType==FTS5_AND ){
208552 pPrev = pLeft->apChild[pLeft->nChild-1];
208553 }else{
208554 pPrev = pLeft;
208555 }
208556 assert( pPrev->eType==FTS5_STRING((void) (0))
208557 || pPrev->eType==FTS5_TERM((void) (0))
208558 || pPrev->eType==FTS5_EOF((void) (0))
208559 )((void) (0));
208560
208561 if( pRight->eType==FTS5_EOF ){
208562 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] )((void) (0));
208563 sqlite3Fts5ParseNodeFree(pRight);
208564 pRet = pLeft;
208565 pParse->nPhrase--;
208566 }
208567 else if( pPrev->eType==FTS5_EOF ){
208568 Fts5ExprPhrase **ap;
208569
208570 if( pPrev==pLeft ){
208571 pRet = pRight;
208572 }else{
208573 pLeft->apChild[pLeft->nChild-1] = pRight;
208574 pRet = pLeft;
208575 }
208576
208577 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
208578 assert( ap[0]==pPrev->pNear->apPhrase[0] )((void) (0));
208579 memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
208580 pParse->nPhrase--;
208581
208582 sqlite3Fts5ParseNodeFree(pPrev);
208583 }
208584 else{
208585 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0);
208586 }
208587 }
208588
208589 return pRet;
208590}
208591
208592static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
208593 sqlite3_int64 nByte = 0;
208594 Fts5ExprTerm *p;
208595 char *zQuoted;
208596
208597 /* Determine the maximum amount of space required. */
208598 for(p=pTerm; p; p=p->pSynonym){
208599 nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
208600 }
208601 zQuoted = sqlite3_malloc64(nByte);
208602
208603 if( zQuoted ){
208604 int i = 0;
208605 for(p=pTerm; p; p=p->pSynonym){
208606 char *zIn = p->zTerm;
208607 zQuoted[i++] = '"';
208608 while( *zIn ){
208609 if( *zIn=='"' ) zQuoted[i++] = '"';
208610 zQuoted[i++] = *zIn++;
208611 }
208612 zQuoted[i++] = '"';
208613 if( p->pSynonym ) zQuoted[i++] = '|';
208614 }
208615 if( pTerm->bPrefix ){
208616 zQuoted[i++] = ' ';
208617 zQuoted[i++] = '*';
208618 }
208619 zQuoted[i++] = '\0';
208620 }
208621 return zQuoted;
208622}
208623
208624static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
208625 char *zNew;
208626 va_list ap;
208627 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
208628 zNew = sqlite3_vmprintf(zFmt, ap);
208629 va_end(ap)__builtin_va_end(ap);
208630 if( zApp && zNew ){
208631 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
208632 sqlite3_free(zNew);
208633 zNew = zNew2;
208634 }
208635 sqlite3_free(zApp);
208636 return zNew;
208637}
208638
208639/*
208640** Compose a tcl-readable representation of expression pExpr. Return a
208641** pointer to a buffer containing that representation. It is the
208642** responsibility of the caller to at some point free the buffer using
208643** sqlite3_free().
208644*/
208645static char *fts5ExprPrintTcl(
208646 Fts5Config *pConfig,
208647 const char *zNearsetCmd,
208648 Fts5ExprNode *pExpr
208649){
208650 char *zRet = 0;
208651 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
208652 Fts5ExprNearset *pNear = pExpr->pNear;
208653 int i;
208654 int iTerm;
208655
208656 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
208657 if( zRet==0 ) return 0;
208658 if( pNear->pColset ){
208659 int *aiCol = pNear->pColset->aiCol;
208660 int nCol = pNear->pColset->nCol;
208661 if( nCol==1 ){
208662 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
208663 }else{
208664 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
208665 for(i=1; i<pNear->pColset->nCol; i++){
208666 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
208667 }
208668 zRet = fts5PrintfAppend(zRet, "} ");
208669 }
208670 if( zRet==0 ) return 0;
208671 }
208672
208673 if( pNear->nPhrase>1 ){
208674 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
208675 if( zRet==0 ) return 0;
208676 }
208677
208678 zRet = fts5PrintfAppend(zRet, "--");
208679 if( zRet==0 ) return 0;
208680
208681 for(i=0; i<pNear->nPhrase; i++){
208682 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
208683
208684 zRet = fts5PrintfAppend(zRet, " {");
208685 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
208686 char *zTerm = pPhrase->aTerm[iTerm].zTerm;
208687 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
208688 if( pPhrase->aTerm[iTerm].bPrefix ){
208689 zRet = fts5PrintfAppend(zRet, "*");
208690 }
208691 }
208692
208693 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
208694 if( zRet==0 ) return 0;
208695 }
208696
208697 }else{
208698 char const *zOp = 0;
208699 int i;
208700 switch( pExpr->eType ){
208701 case FTS5_AND: zOp = "AND"; break;
208702 case FTS5_NOT: zOp = "NOT"; break;
208703 default:
208704 assert( pExpr->eType==FTS5_OR )((void) (0));
208705 zOp = "OR";
208706 break;
208707 }
208708
208709 zRet = sqlite3_mprintf("%s", zOp);
208710 for(i=0; zRet && i<pExpr->nChild; i++){
208711 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
208712 if( !z ){
208713 sqlite3_free(zRet);
208714 zRet = 0;
208715 }else{
208716 zRet = fts5PrintfAppend(zRet, " [%z]", z);
208717 }
208718 }
208719 }
208720
208721 return zRet;
208722}
208723
208724static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
208725 char *zRet = 0;
208726 if( pExpr->eType==0 ){
208727 return sqlite3_mprintf("\"\"");
208728 }else
208729 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
208730 Fts5ExprNearset *pNear = pExpr->pNear;
208731 int i;
208732 int iTerm;
208733
208734 if( pNear->pColset ){
208735 int iCol = pNear->pColset->aiCol[0];
208736 zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[iCol]);
208737 if( zRet==0 ) return 0;
208738 }
208739
208740 if( pNear->nPhrase>1 ){
208741 zRet = fts5PrintfAppend(zRet, "NEAR(");
208742 if( zRet==0 ) return 0;
208743 }
208744
208745 for(i=0; i<pNear->nPhrase; i++){
208746 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
208747 if( i!=0 ){
208748 zRet = fts5PrintfAppend(zRet, " ");
208749 if( zRet==0 ) return 0;
208750 }
208751 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
208752 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
208753 if( zTerm ){
208754 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
208755 sqlite3_free(zTerm);
208756 }
208757 if( zTerm==0 || zRet==0 ){
208758 sqlite3_free(zRet);
208759 return 0;
208760 }
208761 }
208762 }
208763
208764 if( pNear->nPhrase>1 ){
208765 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
208766 if( zRet==0 ) return 0;
208767 }
208768
208769 }else{
208770 char const *zOp = 0;
208771 int i;
208772
208773 switch( pExpr->eType ){
208774 case FTS5_AND: zOp = " AND "; break;
208775 case FTS5_NOT: zOp = " NOT "; break;
208776 default:
208777 assert( pExpr->eType==FTS5_OR )((void) (0));
208778 zOp = " OR ";
208779 break;
208780 }
208781
208782 for(i=0; i<pExpr->nChild; i++){
208783 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
208784 if( z==0 ){
208785 sqlite3_free(zRet);
208786 zRet = 0;
208787 }else{
208788 int e = pExpr->apChild[i]->eType;
208789 int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
208790 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
208791 (i==0 ? "" : zOp),
208792 (b?"(":""), z, (b?")":"")
208793 );
208794 }
208795 if( zRet==0 ) break;
208796 }
208797 }
208798
208799 return zRet;
208800}
208801
208802/*
208803** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
208804** and fts5_expr_tcl() (bTcl!=0).
208805*/
208806static void fts5ExprFunction(
208807 sqlite3_context *pCtx, /* Function call context */
208808 int nArg, /* Number of args */
208809 sqlite3_value **apVal, /* Function arguments */
208810 int bTcl
208811){
208812 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
208813 sqlite3 *db = sqlite3_context_db_handle(pCtx);
208814 const char *zExpr = 0;
208815 char *zErr = 0;
208816 Fts5Expr *pExpr = 0;
208817 int rc;
208818 int i;
208819
208820 const char **azConfig; /* Array of arguments for Fts5Config */
208821 const char *zNearsetCmd = "nearset";
208822 int nConfig; /* Size of azConfig[] */
208823 Fts5Config *pConfig = 0;
208824 int iArg = 1;
208825
208826 if( nArg<1 ){
208827 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
208828 bTcl ? "fts5_expr_tcl" : "fts5_expr"
208829 );
208830 sqlite3_result_error(pCtx, zErr, -1);
208831 sqlite3_free(zErr);
208832 return;
208833 }
208834
208835 if( bTcl && nArg>1 ){
208836 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
208837 iArg = 2;
208838 }
208839
208840 nConfig = 3 + (nArg-iArg);
208841 azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
208842 if( azConfig==0 ){
208843 sqlite3_result_error_nomem(pCtx);
208844 return;
208845 }
208846 azConfig[0] = 0;
208847 azConfig[1] = "main";
208848 azConfig[2] = "tbl";
208849 for(i=3; iArg<nArg; iArg++){
208850 azConfig[i++] = (const char*)sqlite3_value_text(apVal[iArg]);
208851 }
208852
208853 zExpr = (const char*)sqlite3_value_text(apVal[0]);
208854
208855 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
208856 if( rc==SQLITE_OK0 ){
208857 rc = sqlite3Fts5ExprNew(pConfig, pConfig->nCol, zExpr, &pExpr, &zErr);
208858 }
208859 if( rc==SQLITE_OK0 ){
208860 char *zText;
208861 if( pExpr->pRoot->xNext==0 ){
208862 zText = sqlite3_mprintf("");
208863 }else if( bTcl ){
208864 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
208865 }else{
208866 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
208867 }
208868 if( zText==0 ){
208869 rc = SQLITE_NOMEM7;
208870 }else{
208871 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
208872 sqlite3_free(zText);
208873 }
208874 }
208875
208876 if( rc!=SQLITE_OK0 ){
208877 if( zErr ){
208878 sqlite3_result_error(pCtx, zErr, -1);
208879 sqlite3_free(zErr);
208880 }else{
208881 sqlite3_result_error_code(pCtx, rc);
208882 }
208883 }
208884 sqlite3_free((void *)azConfig);
208885 sqlite3Fts5ConfigFree(pConfig);
208886 sqlite3Fts5ExprFree(pExpr);
208887}
208888
208889static void fts5ExprFunctionHr(
208890 sqlite3_context *pCtx, /* Function call context */
208891 int nArg, /* Number of args */
208892 sqlite3_value **apVal /* Function arguments */
208893){
208894 fts5ExprFunction(pCtx, nArg, apVal, 0);
208895}
208896static void fts5ExprFunctionTcl(
208897 sqlite3_context *pCtx, /* Function call context */
208898 int nArg, /* Number of args */
208899 sqlite3_value **apVal /* Function arguments */
208900){
208901 fts5ExprFunction(pCtx, nArg, apVal, 1);
208902}
208903
208904/*
208905** The implementation of an SQLite user-defined-function that accepts a
208906** single integer as an argument. If the integer is an alpha-numeric
208907** unicode code point, 1 is returned. Otherwise 0.
208908*/
208909static void fts5ExprIsAlnum(
208910 sqlite3_context *pCtx, /* Function call context */
208911 int nArg, /* Number of args */
208912 sqlite3_value **apVal /* Function arguments */
208913){
208914 int iCode;
208915 u8 aArr[32];
208916 if( nArg!=1 ){
208917 sqlite3_result_error(pCtx,
208918 "wrong number of arguments to function fts5_isalnum", -1
208919 );
208920 return;
208921 }
208922 memset(aArr, 0, sizeof(aArr));
208923 sqlite3Fts5UnicodeCatParse("L*", aArr);
208924 sqlite3Fts5UnicodeCatParse("N*", aArr);
208925 sqlite3Fts5UnicodeCatParse("Co", aArr);
208926 iCode = sqlite3_value_int(apVal[0]);
208927 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
208928}
208929
208930static void fts5ExprFold(
208931 sqlite3_context *pCtx, /* Function call context */
208932 int nArg, /* Number of args */
208933 sqlite3_value **apVal /* Function arguments */
208934){
208935 if( nArg!=1 && nArg!=2 ){
208936 sqlite3_result_error(pCtx,
208937 "wrong number of arguments to function fts5_fold", -1
208938 );
208939 }else{
208940 int iCode;
208941 int bRemoveDiacritics = 0;
208942 iCode = sqlite3_value_int(apVal[0]);
208943 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
208944 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
208945 }
208946}
208947
208948/*
208949** This is called during initialization to register the fts5_expr() scalar
208950** UDF with the SQLite handle passed as the only argument.
208951*/
208952static int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
208953 struct Fts5ExprFunc {
208954 const char *z;
208955 void (*x)(sqlite3_context*,int,sqlite3_value**);
208956 } aFunc[] = {
208957 { "fts5_expr", fts5ExprFunctionHr },
208958 { "fts5_expr_tcl", fts5ExprFunctionTcl },
208959 { "fts5_isalnum", fts5ExprIsAlnum },
208960 { "fts5_fold", fts5ExprFold },
208961 };
208962 int i;
208963 int rc = SQLITE_OK0;
208964 void *pCtx = (void*)pGlobal;
208965
208966 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aFunc)((int)(sizeof(aFunc)/sizeof(aFunc[0]))); i++){
208967 struct Fts5ExprFunc *p = &aFunc[i];
208968 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF81, pCtx, p->x, 0, 0);
208969 }
208970
208971 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
208972 ** sqlite3Fts5ParserFallback() are unused */
208973#ifndef NDEBUG1
208974 (void)sqlite3Fts5ParserTrace;
208975#endif
208976 (void)sqlite3Fts5ParserFallback;
208977
208978 return rc;
208979}
208980
208981/*
208982** Return the number of phrases in expression pExpr.
208983*/
208984static int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
208985 return (pExpr ? pExpr->nPhrase : 0);
208986}
208987
208988/*
208989** Return the number of terms in the iPhrase'th phrase in pExpr.
208990*/
208991static int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
208992 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
208993 return pExpr->apExprPhrase[iPhrase]->nTerm;
208994}
208995
208996/*
208997** This function is used to access the current position list for phrase
208998** iPhrase.
208999*/
209000static int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
209001 int nRet;
209002 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
209003 Fts5ExprNode *pNode = pPhrase->pNode;
209004 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
209005 *pa = pPhrase->poslist.p;
209006 nRet = pPhrase->poslist.n;
209007 }else{
209008 *pa = 0;
209009 nRet = 0;
209010 }
209011 return nRet;
209012}
209013
209014struct Fts5PoslistPopulator {
209015 Fts5PoslistWriter writer;
209016 int bOk; /* True if ok to populate */
209017 int bMiss;
209018};
209019
209020static Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
209021 Fts5PoslistPopulator *pRet;
209022 pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
209023 if( pRet ){
209024 int i;
209025 memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
209026 for(i=0; i<pExpr->nPhrase; i++){
209027 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
209028 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
209029 assert( pExpr->apExprPhrase[i]->nTerm==1 )((void) (0));
209030 if( bLive &&
209031 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
209032 ){
209033 pRet[i].bMiss = 1;
209034 }else{
209035 pBuf->n = 0;
209036 }
209037 }
209038 }
209039 return pRet;
209040}
209041
209042struct Fts5ExprCtx {
209043 Fts5Expr *pExpr;
209044 Fts5PoslistPopulator *aPopulator;
209045 i64 iOff;
209046};
209047typedef struct Fts5ExprCtx Fts5ExprCtx;
209048
209049/*
209050** TODO: Make this more efficient!
209051*/
209052static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
209053 int i;
209054 for(i=0; i<pColset->nCol; i++){
209055 if( pColset->aiCol[i]==iCol ) return 1;
209056 }
209057 return 0;
209058}
209059
209060static int fts5ExprPopulatePoslistsCb(
209061 void *pCtx, /* Copy of 2nd argument to xTokenize() */
209062 int tflags, /* Mask of FTS5_TOKEN_* flags */
209063 const char *pToken, /* Pointer to buffer containing token */
209064 int nToken, /* Size of token in bytes */
209065 int iUnused1, /* Byte offset of token within input text */
209066 int iUnused2 /* Byte offset of end of token within input text */
209067){
209068 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
209069 Fts5Expr *pExpr = p->pExpr;
209070 int i;
209071
209072 UNUSED_PARAM2(iUnused1, iUnused2);
209073
209074 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
209075 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ) p->iOff++;
209076 for(i=0; i<pExpr->nPhrase; i++){
209077 Fts5ExprTerm *pTerm;
209078 if( p->aPopulator[i].bOk==0 ) continue;
209079 for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
209080 int nTerm = (int)strlen(pTerm->zTerm);
209081 if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
209082 && memcmp(pTerm->zTerm, pToken, nTerm)==0
209083 ){
209084 int rc = sqlite3Fts5PoslistWriterAppend(
209085 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
209086 );
209087 if( rc ) return rc;
209088 break;
209089 }
209090 }
209091 }
209092 return SQLITE_OK0;
209093}
209094
209095static int sqlite3Fts5ExprPopulatePoslists(
209096 Fts5Config *pConfig,
209097 Fts5Expr *pExpr,
209098 Fts5PoslistPopulator *aPopulator,
209099 int iCol,
209100 const char *z, int n
209101){
209102 int i;
209103 Fts5ExprCtx sCtx;
209104 sCtx.pExpr = pExpr;
209105 sCtx.aPopulator = aPopulator;
209106 sCtx.iOff = (((i64)iCol) << 32) - 1;
209107
209108 for(i=0; i<pExpr->nPhrase; i++){
209109 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
209110 Fts5Colset *pColset = pNode->pNear->pColset;
209111 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
209112 || aPopulator[i].bMiss
209113 ){
209114 aPopulator[i].bOk = 0;
209115 }else{
209116 aPopulator[i].bOk = 1;
209117 }
209118 }
209119
209120 return sqlite3Fts5Tokenize(pConfig,
209121 FTS5_TOKENIZE_DOCUMENT0x0004, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
209122 );
209123}
209124
209125static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
209126 if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
209127 pNode->pNear->apPhrase[0]->poslist.n = 0;
209128 }else{
209129 int i;
209130 for(i=0; i<pNode->nChild; i++){
209131 fts5ExprClearPoslists(pNode->apChild[i]);
209132 }
209133 }
209134}
209135
209136static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
209137 pNode->iRowid = iRowid;
209138 pNode->bEof = 0;
209139 switch( pNode->eType ){
209140 case FTS5_TERM:
209141 case FTS5_STRING:
209142 return (pNode->pNear->apPhrase[0]->poslist.n>0);
209143
209144 case FTS5_AND: {
209145 int i;
209146 for(i=0; i<pNode->nChild; i++){
209147 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
209148 fts5ExprClearPoslists(pNode);
209149 return 0;
209150 }
209151 }
209152 break;
209153 }
209154
209155 case FTS5_OR: {
209156 int i;
209157 int bRet = 0;
209158 for(i=0; i<pNode->nChild; i++){
209159 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
209160 bRet = 1;
209161 }
209162 }
209163 return bRet;
209164 }
209165
209166 default: {
209167 assert( pNode->eType==FTS5_NOT )((void) (0));
209168 if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
209169 || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
209170 ){
209171 fts5ExprClearPoslists(pNode);
209172 return 0;
209173 }
209174 break;
209175 }
209176 }
209177 return 1;
209178}
209179
209180static void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
209181 fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
209182}
209183
209184/*
209185** This function is only called for detail=columns tables.
209186*/
209187static int sqlite3Fts5ExprPhraseCollist(
209188 Fts5Expr *pExpr,
209189 int iPhrase,
209190 const u8 **ppCollist,
209191 int *pnCollist
209192){
209193 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
209194 Fts5ExprNode *pNode = pPhrase->pNode;
209195 int rc = SQLITE_OK0;
209196
209197 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase )((void) (0));
209198 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
209199
209200 if( pNode->bEof==0
209201 && pNode->iRowid==pExpr->pRoot->iRowid
209202 && pPhrase->poslist.n>0
209203 ){
209204 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
209205 if( pTerm->pSynonym ){
209206 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
209207 rc = fts5ExprSynonymList(
209208 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
209209 );
209210 }else{
209211 *ppCollist = pPhrase->aTerm[0].pIter->pData;
209212 *pnCollist = pPhrase->aTerm[0].pIter->nData;
209213 }
209214 }else{
209215 *ppCollist = 0;
209216 *pnCollist = 0;
209217 }
209218
209219 return rc;
209220}
209221
209222/*
209223** 2014 August 11
209224**
209225** The author disclaims copyright to this source code. In place of
209226** a legal notice, here is a blessing:
209227**
209228** May you do good and not evil.
209229** May you find forgiveness for yourself and forgive others.
209230** May you share freely, never taking more than you give.
209231**
209232******************************************************************************
209233**
209234*/
209235
209236
209237
209238/* #include "fts5Int.h" */
209239
209240typedef struct Fts5HashEntry Fts5HashEntry;
209241
209242/*
209243** This file contains the implementation of an in-memory hash table used
209244** to accumuluate "term -> doclist" content before it is flused to a level-0
209245** segment.
209246*/
209247
209248
209249struct Fts5Hash {
209250 int eDetail; /* Copy of Fts5Config.eDetail */
209251 int *pnByte; /* Pointer to bytes counter */
209252 int nEntry; /* Number of entries currently in hash */
209253 int nSlot; /* Size of aSlot[] array */
209254 Fts5HashEntry *pScan; /* Current ordered scan item */
209255 Fts5HashEntry **aSlot; /* Array of hash slots */
209256};
209257
209258/*
209259** Each entry in the hash table is represented by an object of the
209260** following type. Each object, its key (a nul-terminated string) and
209261** its current data are stored in a single memory allocation. The
209262** key immediately follows the object in memory. The position list
209263** data immediately follows the key data in memory.
209264**
209265** The data that follows the key is in a similar, but not identical format
209266** to the doclist data stored in the database. It is:
209267**
209268** * Rowid, as a varint
209269** * Position list, without 0x00 terminator.
209270** * Size of previous position list and rowid, as a 4 byte
209271** big-endian integer.
209272**
209273** iRowidOff:
209274** Offset of last rowid written to data area. Relative to first byte of
209275** structure.
209276**
209277** nData:
209278** Bytes of data written since iRowidOff.
209279*/
209280struct Fts5HashEntry {
209281 Fts5HashEntry *pHashNext; /* Next hash entry with same hash-key */
209282 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
209283
209284 int nAlloc; /* Total size of allocation */
209285 int iSzPoslist; /* Offset of space for 4-byte poslist size */
209286 int nData; /* Total bytes of data (incl. structure) */
209287 int nKey; /* Length of key in bytes */
209288 u8 bDel; /* Set delete-flag @ iSzPoslist */
209289 u8 bContent; /* Set content-flag (detail=none mode) */
209290 i16 iCol; /* Column of last value written */
209291 int iPos; /* Position of last value written */
209292 i64 iRowid; /* Rowid of last value written */
209293};
209294
209295/*
209296** Eqivalent to:
209297**
209298** char *fts5EntryKey(Fts5HashEntry *pEntry){ return zKey; }
209299*/
209300#define fts5EntryKey(p) ( ((char *)(&(p)[1])) )
209301
209302
209303/*
209304** Allocate a new hash table.
209305*/
209306static int sqlite3Fts5HashNew(Fts5Config *pConfig, Fts5Hash **ppNew, int *pnByte){
209307 int rc = SQLITE_OK0;
209308 Fts5Hash *pNew;
209309
209310 *ppNew = pNew = (Fts5Hash*)sqlite3_malloc(sizeof(Fts5Hash));
209311 if( pNew==0 ){
209312 rc = SQLITE_NOMEM7;
209313 }else{
209314 sqlite3_int64 nByte;
209315 memset(pNew, 0, sizeof(Fts5Hash));
209316 pNew->pnByte = pnByte;
209317 pNew->eDetail = pConfig->eDetail;
209318
209319 pNew->nSlot = 1024;
209320 nByte = sizeof(Fts5HashEntry*) * pNew->nSlot;
209321 pNew->aSlot = (Fts5HashEntry**)sqlite3_malloc64(nByte);
209322 if( pNew->aSlot==0 ){
209323 sqlite3_free(pNew);
209324 *ppNew = 0;
209325 rc = SQLITE_NOMEM7;
209326 }else{
209327 memset(pNew->aSlot, 0, (size_t)nByte);
209328 }
209329 }
209330 return rc;
209331}
209332
209333/*
209334** Free a hash table object.
209335*/
209336static void sqlite3Fts5HashFree(Fts5Hash *pHash){
209337 if( pHash ){
209338 sqlite3Fts5HashClear(pHash);
209339 sqlite3_free(pHash->aSlot);
209340 sqlite3_free(pHash);
209341 }
209342}
209343
209344/*
209345** Empty (but do not delete) a hash table.
209346*/
209347static void sqlite3Fts5HashClear(Fts5Hash *pHash){
209348 int i;
209349 for(i=0; i<pHash->nSlot; i++){
209350 Fts5HashEntry *pNext;
209351 Fts5HashEntry *pSlot;
209352 for(pSlot=pHash->aSlot[i]; pSlot; pSlot=pNext){
209353 pNext = pSlot->pHashNext;
209354 sqlite3_free(pSlot);
209355 }
209356 }
209357 memset(pHash->aSlot, 0, pHash->nSlot * sizeof(Fts5HashEntry*));
209358 pHash->nEntry = 0;
209359}
209360
209361static unsigned int fts5HashKey(int nSlot, const u8 *p, int n){
209362 int i;
209363 unsigned int h = 13;
209364 for(i=n-1; i>=0; i--){
209365 h = (h << 3) ^ h ^ p[i];
209366 }
209367 return (h % nSlot);
209368}
209369
209370static unsigned int fts5HashKey2(int nSlot, u8 b, const u8 *p, int n){
209371 int i;
209372 unsigned int h = 13;
209373 for(i=n-1; i>=0; i--){
209374 h = (h << 3) ^ h ^ p[i];
209375 }
209376 h = (h << 3) ^ h ^ b;
209377 return (h % nSlot);
209378}
209379
209380/*
209381** Resize the hash table by doubling the number of slots.
209382*/
209383static int fts5HashResize(Fts5Hash *pHash){
209384 int nNew = pHash->nSlot*2;
209385 int i;
209386 Fts5HashEntry **apNew;
209387 Fts5HashEntry **apOld = pHash->aSlot;
209388
209389 apNew = (Fts5HashEntry**)sqlite3_malloc64(nNew*sizeof(Fts5HashEntry*));
209390 if( !apNew ) return SQLITE_NOMEM7;
209391 memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
209392
209393 for(i=0; i<pHash->nSlot; i++){
209394 while( apOld[i] ){
209395 unsigned int iHash;
209396 Fts5HashEntry *p = apOld[i];
209397 apOld[i] = p->pHashNext;
209398 iHash = fts5HashKey(nNew, (u8*)fts5EntryKey(p),
209399 (int)strlen(fts5EntryKey(p)));
209400 p->pHashNext = apNew[iHash];
209401 apNew[iHash] = p;
209402 }
209403 }
209404
209405 sqlite3_free(apOld);
209406 pHash->nSlot = nNew;
209407 pHash->aSlot = apNew;
209408 return SQLITE_OK0;
209409}
209410
209411static int fts5HashAddPoslistSize(
209412 Fts5Hash *pHash,
209413 Fts5HashEntry *p,
209414 Fts5HashEntry *p2
209415){
209416 int nRet = 0;
209417 if( p->iSzPoslist ){
209418 u8 *pPtr = p2 ? (u8*)p2 : (u8*)p;
209419 int nData = p->nData;
209420 if( pHash->eDetail==FTS5_DETAIL_NONE ){
209421 assert( nData==p->iSzPoslist )((void) (0));
209422 if( p->bDel ){
209423 pPtr[nData++] = 0x00;
209424 if( p->bContent ){
209425 pPtr[nData++] = 0x00;
209426 }
209427 }
209428 }else{
209429 int nSz = (nData - p->iSzPoslist - 1); /* Size in bytes */
209430 int nPos = nSz*2 + p->bDel; /* Value of nPos field */
209431
209432 assert( p->bDel==0 || p->bDel==1 )((void) (0));
209433 if( nPos<=127 ){
209434 pPtr[p->iSzPoslist] = (u8)nPos;
209435 }else{
209436 int nByte = sqlite3Fts5GetVarintLen((u32)nPos);
209437 memmove(&pPtr[p->iSzPoslist + nByte], &pPtr[p->iSzPoslist + 1], nSz);
209438 sqlite3Fts5PutVarint(&pPtr[p->iSzPoslist], nPos);
209439 nData += (nByte-1);
209440 }
209441 }
209442
209443 nRet = nData - p->nData;
209444 if( p2==0 ){
209445 p->iSzPoslist = 0;
209446 p->bDel = 0;
209447 p->bContent = 0;
209448 p->nData = nData;
209449 }
209450 }
209451 return nRet;
209452}
209453
209454/*
209455** Add an entry to the in-memory hash table. The key is the concatenation
209456** of bByte and (pToken/nToken). The value is (iRowid/iCol/iPos).
209457**
209458** (bByte || pToken) -> (iRowid,iCol,iPos)
209459**
209460** Or, if iCol is negative, then the value is a delete marker.
209461*/
209462static int sqlite3Fts5HashWrite(
209463 Fts5Hash *pHash,
209464 i64 iRowid, /* Rowid for this entry */
209465 int iCol, /* Column token appears in (-ve -> delete) */
209466 int iPos, /* Position of token within column */
209467 char bByte, /* First byte of token */
209468 const char *pToken, int nToken /* Token to add or remove to or from index */
209469){
209470 unsigned int iHash;
209471 Fts5HashEntry *p;
209472 u8 *pPtr;
209473 int nIncr = 0; /* Amount to increment (*pHash->pnByte) by */
209474 int bNew; /* If non-delete entry should be written */
209475
209476 bNew = (pHash->eDetail==FTS5_DETAIL_FULL);
209477
209478 /* Attempt to locate an existing hash entry */
209479 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
209480 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
209481 char *zKey = fts5EntryKey(p);
209482 if( zKey[0]==bByte
209483 && p->nKey==nToken
209484 && memcmp(&zKey[1], pToken, nToken)==0
209485 ){
209486 break;
209487 }
209488 }
209489
209490 /* If an existing hash entry cannot be found, create a new one. */
209491 if( p==0 ){
209492 /* Figure out how much space to allocate */
209493 char *zKey;
209494 sqlite3_int64 nByte = sizeof(Fts5HashEntry) + (nToken+1) + 1 + 64;
209495 if( nByte<128 ) nByte = 128;
209496
209497 /* Grow the Fts5Hash.aSlot[] array if necessary. */
209498 if( (pHash->nEntry*2)>=pHash->nSlot ){
209499 int rc = fts5HashResize(pHash);
209500 if( rc!=SQLITE_OK0 ) return rc;
209501 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
209502 }
209503
209504 /* Allocate new Fts5HashEntry and add it to the hash table. */
209505 p = (Fts5HashEntry*)sqlite3_malloc64(nByte);
209506 if( !p ) return SQLITE_NOMEM7;
209507 memset(p, 0, sizeof(Fts5HashEntry));
209508 p->nAlloc = (int)nByte;
209509 zKey = fts5EntryKey(p);
209510 zKey[0] = bByte;
209511 memcpy(&zKey[1], pToken, nToken);
209512 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)zKey, nToken+1) )((void) (0));
209513 p->nKey = nToken;
209514 zKey[nToken+1] = '\0';
209515 p->nData = nToken+1 + 1 + sizeof(Fts5HashEntry);
209516 p->pHashNext = pHash->aSlot[iHash];
209517 pHash->aSlot[iHash] = p;
209518 pHash->nEntry++;
209519
209520 /* Add the first rowid field to the hash-entry */
209521 p->nData += sqlite3Fts5PutVarint(&((u8*)p)[p->nData], iRowid);
209522 p->iRowid = iRowid;
209523
209524 p->iSzPoslist = p->nData;
209525 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
209526 p->nData += 1;
209527 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
209528 }
209529
209530 nIncr += p->nData;
209531 }else{
209532
209533 /* Appending to an existing hash-entry. Check that there is enough
209534 ** space to append the largest possible new entry. Worst case scenario
209535 ** is:
209536 **
209537 ** + 9 bytes for a new rowid,
209538 ** + 4 byte reserved for the "poslist size" varint.
209539 ** + 1 byte for a "new column" byte,
209540 ** + 3 bytes for a new column number (16-bit max) as a varint,
209541 ** + 5 bytes for the new position offset (32-bit max).
209542 */
209543 if( (p->nAlloc - p->nData) < (9 + 4 + 1 + 3 + 5) ){
209544 sqlite3_int64 nNew = p->nAlloc * 2;
209545 Fts5HashEntry *pNew;
209546 Fts5HashEntry **pp;
209547 pNew = (Fts5HashEntry*)sqlite3_realloc64(p, nNew);
209548 if( pNew==0 ) return SQLITE_NOMEM7;
209549 pNew->nAlloc = (int)nNew;
209550 for(pp=&pHash->aSlot[iHash]; *pp!=p; pp=&(*pp)->pHashNext);
209551 *pp = pNew;
209552 p = pNew;
209553 }
209554 nIncr -= p->nData;
209555 }
209556 assert( (p->nAlloc - p->nData) >= (9 + 4 + 1 + 3 + 5) )((void) (0));
209557
209558 pPtr = (u8*)p;
209559
209560 /* If this is a new rowid, append the 4-byte size field for the previous
209561 ** entry, and the new rowid for this entry. */
209562 if( iRowid!=p->iRowid ){
209563 fts5HashAddPoslistSize(pHash, p, 0);
209564 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iRowid - p->iRowid);
209565 p->iRowid = iRowid;
209566 bNew = 1;
209567 p->iSzPoslist = p->nData;
209568 if( pHash->eDetail!=FTS5_DETAIL_NONE ){
209569 p->nData += 1;
209570 p->iCol = (pHash->eDetail==FTS5_DETAIL_FULL ? 0 : -1);
209571 p->iPos = 0;
209572 }
209573 }
209574
209575 if( iCol>=0 ){
209576 if( pHash->eDetail==FTS5_DETAIL_NONE ){
209577 p->bContent = 1;
209578 }else{
209579 /* Append a new column value, if necessary */
209580 assert( iCol>=p->iCol )((void) (0));
209581 if( iCol!=p->iCol ){
209582 if( pHash->eDetail==FTS5_DETAIL_FULL ){
209583 pPtr[p->nData++] = 0x01;
209584 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iCol);
209585 p->iCol = (i16)iCol;
209586 p->iPos = 0;
209587 }else{
209588 bNew = 1;
209589 p->iCol = (i16)(iPos = iCol);
209590 }
209591 }
209592
209593 /* Append the new position offset, if necessary */
209594 if( bNew ){
209595 p->nData += sqlite3Fts5PutVarint(&pPtr[p->nData], iPos - p->iPos + 2);
209596 p->iPos = iPos;
209597 }
209598 }
209599 }else{
209600 /* This is a delete. Set the delete flag. */
209601 p->bDel = 1;
209602 }
209603
209604 nIncr += p->nData;
209605 *pHash->pnByte += nIncr;
209606 return SQLITE_OK0;
209607}
209608
209609
209610/*
209611** Arguments pLeft and pRight point to linked-lists of hash-entry objects,
209612** each sorted in key order. This function merges the two lists into a
209613** single list and returns a pointer to its first element.
209614*/
209615static Fts5HashEntry *fts5HashEntryMerge(
209616 Fts5HashEntry *pLeft,
209617 Fts5HashEntry *pRight
209618){
209619 Fts5HashEntry *p1 = pLeft;
209620 Fts5HashEntry *p2 = pRight;
209621 Fts5HashEntry *pRet = 0;
209622 Fts5HashEntry **ppOut = &pRet;
209623
209624 while( p1 || p2 ){
209625 if( p1==0 ){
209626 *ppOut = p2;
209627 p2 = 0;
209628 }else if( p2==0 ){
209629 *ppOut = p1;
209630 p1 = 0;
209631 }else{
209632 int i = 0;
209633 char *zKey1 = fts5EntryKey(p1);
209634 char *zKey2 = fts5EntryKey(p2);
209635 while( zKey1[i]==zKey2[i] ) i++;
209636
209637 if( ((u8)zKey1[i])>((u8)zKey2[i]) ){
209638 /* p2 is smaller */
209639 *ppOut = p2;
209640 ppOut = &p2->pScanNext;
209641 p2 = p2->pScanNext;
209642 }else{
209643 /* p1 is smaller */
209644 *ppOut = p1;
209645 ppOut = &p1->pScanNext;
209646 p1 = p1->pScanNext;
209647 }
209648 *ppOut = 0;
209649 }
209650 }
209651
209652 return pRet;
209653}
209654
209655/*
209656** Extract all tokens from hash table iHash and link them into a list
209657** in sorted order. The hash table is cleared before returning. It is
209658** the responsibility of the caller to free the elements of the returned
209659** list.
209660*/
209661static int fts5HashEntrySort(
209662 Fts5Hash *pHash,
209663 const char *pTerm, int nTerm, /* Query prefix, if any */
209664 Fts5HashEntry **ppSorted
209665){
209666 const int nMergeSlot = 32;
209667 Fts5HashEntry **ap;
209668 Fts5HashEntry *pList;
209669 int iSlot;
209670 int i;
209671
209672 *ppSorted = 0;
209673 ap = sqlite3_malloc64(sizeof(Fts5HashEntry*) * nMergeSlot);
209674 if( !ap ) return SQLITE_NOMEM7;
209675 memset(ap, 0, sizeof(Fts5HashEntry*) * nMergeSlot);
209676
209677 for(iSlot=0; iSlot<pHash->nSlot; iSlot++){
209678 Fts5HashEntry *pIter;
209679 for(pIter=pHash->aSlot[iSlot]; pIter; pIter=pIter->pHashNext){
209680 if( pTerm==0
209681 || (pIter->nKey+1>=nTerm && 0==memcmp(fts5EntryKey(pIter), pTerm, nTerm))
209682 ){
209683 Fts5HashEntry *pEntry = pIter;
209684 pEntry->pScanNext = 0;
209685 for(i=0; ap[i]; i++){
209686 pEntry = fts5HashEntryMerge(pEntry, ap[i]);
209687 ap[i] = 0;
209688 }
209689 ap[i] = pEntry;
209690 }
209691 }
209692 }
209693
209694 pList = 0;
209695 for(i=0; i<nMergeSlot; i++){
209696 pList = fts5HashEntryMerge(pList, ap[i]);
209697 }
209698
209699 pHash->nEntry = 0;
209700 sqlite3_free(ap);
209701 *ppSorted = pList;
209702 return SQLITE_OK0;
209703}
209704
209705/*
209706** Query the hash table for a doclist associated with term pTerm/nTerm.
209707*/
209708static int sqlite3Fts5HashQuery(
209709 Fts5Hash *pHash, /* Hash table to query */
209710 int nPre,
209711 const char *pTerm, int nTerm, /* Query term */
209712 void **ppOut, /* OUT: Pointer to new object */
209713 int *pnDoclist /* OUT: Size of doclist in bytes */
209714){
209715 unsigned int iHash = fts5HashKey(pHash->nSlot, (const u8*)pTerm, nTerm);
209716 char *zKey = 0;
209717 Fts5HashEntry *p;
209718
209719 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
209720 zKey = fts5EntryKey(p);
209721 assert( p->nKey+1==(int)strlen(zKey) )((void) (0));
209722 if( nTerm==p->nKey+1 && memcmp(zKey, pTerm, nTerm)==0 ) break;
209723 }
209724
209725 if( p ){
209726 int nHashPre = sizeof(Fts5HashEntry) + nTerm + 1;
209727 int nList = p->nData - nHashPre;
209728 u8 *pRet = (u8*)(*ppOut = sqlite3_malloc64(nPre + nList + 10));
209729 if( pRet ){
209730 Fts5HashEntry *pFaux = (Fts5HashEntry*)&pRet[nPre-nHashPre];
209731 memcpy(&pRet[nPre], &((u8*)p)[nHashPre], nList);
209732 nList += fts5HashAddPoslistSize(pHash, p, pFaux);
209733 *pnDoclist = nList;
209734 }else{
209735 *pnDoclist = 0;
209736 return SQLITE_NOMEM7;
209737 }
209738 }else{
209739 *ppOut = 0;
209740 *pnDoclist = 0;
209741 }
209742
209743 return SQLITE_OK0;
209744}
209745
209746static int sqlite3Fts5HashScanInit(
209747 Fts5Hash *p, /* Hash table to query */
209748 const char *pTerm, int nTerm /* Query prefix */
209749){
209750 return fts5HashEntrySort(p, pTerm, nTerm, &p->pScan);
209751}
209752
209753static void sqlite3Fts5HashScanNext(Fts5Hash *p){
209754 assert( !sqlite3Fts5HashScanEof(p) )((void) (0));
209755 p->pScan = p->pScan->pScanNext;
209756}
209757
209758static int sqlite3Fts5HashScanEof(Fts5Hash *p){
209759 return (p->pScan==0);
209760}
209761
209762static void sqlite3Fts5HashScanEntry(
209763 Fts5Hash *pHash,
209764 const char **pzTerm, /* OUT: term (nul-terminated) */
209765 const u8 **ppDoclist, /* OUT: pointer to doclist */
209766 int *pnDoclist /* OUT: size of doclist in bytes */
209767){
209768 Fts5HashEntry *p;
209769 if( (p = pHash->pScan) ){
209770 char *zKey = fts5EntryKey(p);
209771 int nTerm = (int)strlen(zKey);
209772 fts5HashAddPoslistSize(pHash, p, 0);
209773 *pzTerm = zKey;
209774 *ppDoclist = (const u8*)&zKey[nTerm+1];
209775 *pnDoclist = p->nData - (sizeof(Fts5HashEntry) + nTerm + 1);
209776 }else{
209777 *pzTerm = 0;
209778 *ppDoclist = 0;
209779 *pnDoclist = 0;
209780 }
209781}
209782
209783/*
209784** 2014 May 31
209785**
209786** The author disclaims copyright to this source code. In place of
209787** a legal notice, here is a blessing:
209788**
209789** May you do good and not evil.
209790** May you find forgiveness for yourself and forgive others.
209791** May you share freely, never taking more than you give.
209792**
209793******************************************************************************
209794**
209795** Low level access to the FTS index stored in the database file. The
209796** routines in this file file implement all read and write access to the
209797** %_data table. Other parts of the system access this functionality via
209798** the interface defined in fts5Int.h.
209799*/
209800
209801
209802/* #include "fts5Int.h" */
209803
209804/*
209805** Overview:
209806**
209807** The %_data table contains all the FTS indexes for an FTS5 virtual table.
209808** As well as the main term index, there may be up to 31 prefix indexes.
209809** The format is similar to FTS3/4, except that:
209810**
209811** * all segment b-tree leaf data is stored in fixed size page records
209812** (e.g. 1000 bytes). A single doclist may span multiple pages. Care is
209813** taken to ensure it is possible to iterate in either direction through
209814** the entries in a doclist, or to seek to a specific entry within a
209815** doclist, without loading it into memory.
209816**
209817** * large doclists that span many pages have associated "doclist index"
209818** records that contain a copy of the first rowid on each page spanned by
209819** the doclist. This is used to speed up seek operations, and merges of
209820** large doclists with very small doclists.
209821**
209822** * extra fields in the "structure record" record the state of ongoing
209823** incremental merge operations.
209824**
209825*/
209826
209827
209828#define FTS5_OPT_WORK_UNIT 1000 /* Number of leaf pages per optimize step */
209829#define FTS5_WORK_UNIT 64 /* Number of leaf pages in unit of work */
209830
209831#define FTS5_MIN_DLIDX_SIZE 4 /* Add dlidx if this many empty pages */
209832
209833#define FTS5_MAIN_PREFIX '0'
209834
209835#if FTS5_MAX_PREFIX_INDEXES > 31
209836# error "FTS5_MAX_PREFIX_INDEXES is too large"
209837#endif
209838
209839/*
209840** Details:
209841**
209842** The %_data table managed by this module,
209843**
209844** CREATE TABLE %_data(id INTEGER PRIMARY KEY, block BLOB);
209845**
209846** , contains the following 5 types of records. See the comments surrounding
209847** the FTS5_*_ROWID macros below for a description of how %_data rowids are
209848** assigned to each fo them.
209849**
209850** 1. Structure Records:
209851**
209852** The set of segments that make up an index - the index structure - are
209853** recorded in a single record within the %_data table. The record consists
209854** of a single 32-bit configuration cookie value followed by a list of
209855** SQLite varints. If the FTS table features more than one index (because
209856** there are one or more prefix indexes), it is guaranteed that all share
209857** the same cookie value.
209858**
209859** Immediately following the configuration cookie, the record begins with
209860** three varints:
209861**
209862** + number of levels,
209863** + total number of segments on all levels,
209864** + value of write counter.
209865**
209866** Then, for each level from 0 to nMax:
209867**
209868** + number of input segments in ongoing merge.
209869** + total number of segments in level.
209870** + for each segment from oldest to newest:
209871** + segment id (always > 0)
209872** + first leaf page number (often 1, always greater than 0)
209873** + final leaf page number
209874**
209875** 2. The Averages Record:
209876**
209877** A single record within the %_data table. The data is a list of varints.
209878** The first value is the number of rows in the index. Then, for each column
209879** from left to right, the total number of tokens in the column for all
209880** rows of the table.
209881**
209882** 3. Segment leaves:
209883**
209884** TERM/DOCLIST FORMAT:
209885**
209886** Most of each segment leaf is taken up by term/doclist data. The
209887** general format of term/doclist, starting with the first term
209888** on the leaf page, is:
209889**
209890** varint : size of first term
209891** blob: first term data
209892** doclist: first doclist
209893** zero-or-more {
209894** varint: number of bytes in common with previous term
209895** varint: number of bytes of new term data (nNew)
209896** blob: nNew bytes of new term data
209897** doclist: next doclist
209898** }
209899**
209900** doclist format:
209901**
209902** varint: first rowid
209903** poslist: first poslist
209904** zero-or-more {
209905** varint: rowid delta (always > 0)
209906** poslist: next poslist
209907** }
209908**
209909** poslist format:
209910**
209911** varint: size of poslist in bytes multiplied by 2, not including
209912** this field. Plus 1 if this entry carries the "delete" flag.
209913** collist: collist for column 0
209914** zero-or-more {
209915** 0x01 byte
209916** varint: column number (I)
209917** collist: collist for column I
209918** }
209919**
209920** collist format:
209921**
209922** varint: first offset + 2
209923** zero-or-more {
209924** varint: offset delta + 2
209925** }
209926**
209927** PAGE FORMAT
209928**
209929** Each leaf page begins with a 4-byte header containing 2 16-bit
209930** unsigned integer fields in big-endian format. They are:
209931**
209932** * The byte offset of the first rowid on the page, if it exists
209933** and occurs before the first term (otherwise 0).
209934**
209935** * The byte offset of the start of the page footer. If the page
209936** footer is 0 bytes in size, then this field is the same as the
209937** size of the leaf page in bytes.
209938**
209939** The page footer consists of a single varint for each term located
209940** on the page. Each varint is the byte offset of the current term
209941** within the page, delta-compressed against the previous value. In
209942** other words, the first varint in the footer is the byte offset of
209943** the first term, the second is the byte offset of the second less that
209944** of the first, and so on.
209945**
209946** The term/doclist format described above is accurate if the entire
209947** term/doclist data fits on a single leaf page. If this is not the case,
209948** the format is changed in two ways:
209949**
209950** + if the first rowid on a page occurs before the first term, it
209951** is stored as a literal value:
209952**
209953** varint: first rowid
209954**
209955** + the first term on each page is stored in the same way as the
209956** very first term of the segment:
209957**
209958** varint : size of first term
209959** blob: first term data
209960**
209961** 5. Segment doclist indexes:
209962**
209963** Doclist indexes are themselves b-trees, however they usually consist of
209964** a single leaf record only. The format of each doclist index leaf page
209965** is:
209966**
209967** * Flags byte. Bits are:
209968** 0x01: Clear if leaf is also the root page, otherwise set.
209969**
209970** * Page number of fts index leaf page. As a varint.
209971**
209972** * First rowid on page indicated by previous field. As a varint.
209973**
209974** * A list of varints, one for each subsequent termless page. A
209975** positive delta if the termless page contains at least one rowid,
209976** or an 0x00 byte otherwise.
209977**
209978** Internal doclist index nodes are:
209979**
209980** * Flags byte. Bits are:
209981** 0x01: Clear for root page, otherwise set.
209982**
209983** * Page number of first child page. As a varint.
209984**
209985** * Copy of first rowid on page indicated by previous field. As a varint.
209986**
209987** * A list of delta-encoded varints - the first rowid on each subsequent
209988** child page.
209989**
209990*/
209991
209992/*
209993** Rowids for the averages and structure records in the %_data table.
209994*/
209995#define FTS5_AVERAGES_ROWID 1 /* Rowid used for the averages record */
209996#define FTS5_STRUCTURE_ROWID 10 /* The structure record */
209997
209998/*
209999** Macros determining the rowids used by segment leaves and dlidx leaves
210000** and nodes. All nodes and leaves are stored in the %_data table with large
210001** positive rowids.
210002**
210003** Each segment has a unique non-zero 16-bit id.
210004**
210005** The rowid for each segment leaf is found by passing the segment id and
210006** the leaf page number to the FTS5_SEGMENT_ROWID macro. Leaves are numbered
210007** sequentially starting from 1.
210008*/
210009#define FTS5_DATA_ID_B 16 /* Max seg id number 65535 */
210010#define FTS5_DATA_DLI_B 1 /* Doclist-index flag (1 bit) */
210011#define FTS5_DATA_HEIGHT_B 5 /* Max dlidx tree height of 32 */
210012#define FTS5_DATA_PAGE_B 31 /* Max page number of 2147483648 */
210013
210014#define fts5_dri(segid, dlidx, height, pgno) ( \
210015 ((i64)(segid) << (FTS5_DATA_PAGE_B+FTS5_DATA_HEIGHT_B+FTS5_DATA_DLI_B)) + \
210016 ((i64)(dlidx) << (FTS5_DATA_PAGE_B + FTS5_DATA_HEIGHT_B)) + \
210017 ((i64)(height) << (FTS5_DATA_PAGE_B)) + \
210018 ((i64)(pgno)) \
210019)
210020
210021#define FTS5_SEGMENT_ROWID(segid, pgno) fts5_dri(segid, 0, 0, pgno)
210022#define FTS5_DLIDX_ROWID(segid, height, pgno) fts5_dri(segid, 1, height, pgno)
210023
210024/*
210025** Maximum segments permitted in a single index
210026*/
210027#define FTS5_MAX_SEGMENT 2000
210028
210029#ifdef SQLITE_DEBUG
210030static int sqlite3Fts5Corrupt() { return SQLITE_CORRUPT_VTAB(11 | (1<<8)); }
210031#endif
210032
210033
210034/*
210035** Each time a blob is read from the %_data table, it is padded with this
210036** many zero bytes. This makes it easier to decode the various record formats
210037** without overreading if the records are corrupt.
210038*/
210039#define FTS5_DATA_ZERO_PADDING 8
210040#define FTS5_DATA_PADDING 20
210041
210042typedef struct Fts5Data Fts5Data;
210043typedef struct Fts5DlidxIter Fts5DlidxIter;
210044typedef struct Fts5DlidxLvl Fts5DlidxLvl;
210045typedef struct Fts5DlidxWriter Fts5DlidxWriter;
210046typedef struct Fts5Iter Fts5Iter;
210047typedef struct Fts5PageWriter Fts5PageWriter;
210048typedef struct Fts5SegIter Fts5SegIter;
210049typedef struct Fts5DoclistIter Fts5DoclistIter;
210050typedef struct Fts5SegWriter Fts5SegWriter;
210051typedef struct Fts5Structure Fts5Structure;
210052typedef struct Fts5StructureLevel Fts5StructureLevel;
210053typedef struct Fts5StructureSegment Fts5StructureSegment;
210054
210055struct Fts5Data {
210056 u8 *p; /* Pointer to buffer containing record */
210057 int nn; /* Size of record in bytes */
210058 int szLeaf; /* Size of leaf without page-index */
210059};
210060
210061/*
210062** One object per %_data table.
210063*/
210064struct Fts5Index {
210065 Fts5Config *pConfig; /* Virtual table configuration */
210066 char *zDataTbl; /* Name of %_data table */
210067 int nWorkUnit; /* Leaf pages in a "unit" of work */
210068
210069 /*
210070 ** Variables related to the accumulation of tokens and doclists within the
210071 ** in-memory hash tables before they are flushed to disk.
210072 */
210073 Fts5Hash *pHash; /* Hash table for in-memory data */
210074 int nPendingData; /* Current bytes of pending data */
210075 i64 iWriteRowid; /* Rowid for current doc being written */
210076 int bDelete; /* Current write is a delete */
210077
210078 /* Error state. */
210079 int rc; /* Current error code */
210080
210081 /* State used by the fts5DataXXX() functions. */
210082 sqlite3_blob *pReader; /* RO incr-blob open on %_data table */
210083 sqlite3_stmt *pWriter; /* "INSERT ... %_data VALUES(?,?)" */
210084 sqlite3_stmt *pDeleter; /* "DELETE FROM %_data ... id>=? AND id<=?" */
210085 sqlite3_stmt *pIdxWriter; /* "INSERT ... %_idx VALUES(?,?,?,?)" */
210086 sqlite3_stmt *pIdxDeleter; /* "DELETE FROM %_idx WHERE segid=? */
210087 sqlite3_stmt *pIdxSelect;
210088 int nRead; /* Total number of blocks read */
210089
210090 sqlite3_stmt *pDataVersion;
210091 i64 iStructVersion; /* data_version when pStruct read */
210092 Fts5Structure *pStruct; /* Current db structure (or NULL) */
210093};
210094
210095struct Fts5DoclistIter {
210096 u8 *aEof; /* Pointer to 1 byte past end of doclist */
210097
210098 /* Output variables. aPoslist==0 at EOF */
210099 i64 iRowid;
210100 u8 *aPoslist;
210101 int nPoslist;
210102 int nSize;
210103};
210104
210105/*
210106** The contents of the "structure" record for each index are represented
210107** using an Fts5Structure record in memory. Which uses instances of the
210108** other Fts5StructureXXX types as components.
210109*/
210110struct Fts5StructureSegment {
210111 int iSegid; /* Segment id */
210112 int pgnoFirst; /* First leaf page number in segment */
210113 int pgnoLast; /* Last leaf page number in segment */
210114};
210115struct Fts5StructureLevel {
210116 int nMerge; /* Number of segments in incr-merge */
210117 int nSeg; /* Total number of segments on level */
210118 Fts5StructureSegment *aSeg; /* Array of segments. aSeg[0] is oldest. */
210119};
210120struct Fts5Structure {
210121 int nRef; /* Object reference count */
210122 u64 nWriteCounter; /* Total leaves written to level 0 */
210123 int nSegment; /* Total segments in this structure */
210124 int nLevel; /* Number of levels in this index */
210125 Fts5StructureLevel aLevel[1]; /* Array of nLevel level objects */
210126};
210127
210128/*
210129** An object of type Fts5SegWriter is used to write to segments.
210130*/
210131struct Fts5PageWriter {
210132 int pgno; /* Page number for this page */
210133 int iPrevPgidx; /* Previous value written into pgidx */
210134 Fts5Buffer buf; /* Buffer containing leaf data */
210135 Fts5Buffer pgidx; /* Buffer containing page-index */
210136 Fts5Buffer term; /* Buffer containing previous term on page */
210137};
210138struct Fts5DlidxWriter {
210139 int pgno; /* Page number for this page */
210140 int bPrevValid; /* True if iPrev is valid */
210141 i64 iPrev; /* Previous rowid value written to page */
210142 Fts5Buffer buf; /* Buffer containing page data */
210143};
210144struct Fts5SegWriter {
210145 int iSegid; /* Segid to write to */
210146 Fts5PageWriter writer; /* PageWriter object */
210147 i64 iPrevRowid; /* Previous rowid written to current leaf */
210148 u8 bFirstRowidInDoclist; /* True if next rowid is first in doclist */
210149 u8 bFirstRowidInPage; /* True if next rowid is first in page */
210150 /* TODO1: Can use (writer.pgidx.n==0) instead of bFirstTermInPage */
210151 u8 bFirstTermInPage; /* True if next term will be first in leaf */
210152 int nLeafWritten; /* Number of leaf pages written */
210153 int nEmpty; /* Number of contiguous term-less nodes */
210154
210155 int nDlidx; /* Allocated size of aDlidx[] array */
210156 Fts5DlidxWriter *aDlidx; /* Array of Fts5DlidxWriter objects */
210157
210158 /* Values to insert into the %_idx table */
210159 Fts5Buffer btterm; /* Next term to insert into %_idx table */
210160 int iBtPage; /* Page number corresponding to btterm */
210161};
210162
210163typedef struct Fts5CResult Fts5CResult;
210164struct Fts5CResult {
210165 u16 iFirst; /* aSeg[] index of firstest iterator */
210166 u8 bTermEq; /* True if the terms are equal */
210167};
210168
210169/*
210170** Object for iterating through a single segment, visiting each term/rowid
210171** pair in the segment.
210172**
210173** pSeg:
210174** The segment to iterate through.
210175**
210176** iLeafPgno:
210177** Current leaf page number within segment.
210178**
210179** iLeafOffset:
210180** Byte offset within the current leaf that is the first byte of the
210181** position list data (one byte passed the position-list size field).
210182** rowid field of the current entry. Usually this is the size field of the
210183** position list data. The exception is if the rowid for the current entry
210184** is the last thing on the leaf page.
210185**
210186** pLeaf:
210187** Buffer containing current leaf page data. Set to NULL at EOF.
210188**
210189** iTermLeafPgno, iTermLeafOffset:
210190** Leaf page number containing the last term read from the segment. And
210191** the offset immediately following the term data.
210192**
210193** flags:
210194** Mask of FTS5_SEGITER_XXX values. Interpreted as follows:
210195**
210196** FTS5_SEGITER_ONETERM:
210197** If set, set the iterator to point to EOF after the current doclist
210198** has been exhausted. Do not proceed to the next term in the segment.
210199**
210200** FTS5_SEGITER_REVERSE:
210201** This flag is only ever set if FTS5_SEGITER_ONETERM is also set. If
210202** it is set, iterate through rowid in descending order instead of the
210203** default ascending order.
210204**
210205** iRowidOffset/nRowidOffset/aRowidOffset:
210206** These are used if the FTS5_SEGITER_REVERSE flag is set.
210207**
210208** For each rowid on the page corresponding to the current term, the
210209** corresponding aRowidOffset[] entry is set to the byte offset of the
210210** start of the "position-list-size" field within the page.
210211**
210212** iTermIdx:
210213** Index of current term on iTermLeafPgno.
210214*/
210215struct Fts5SegIter {
210216 Fts5StructureSegment *pSeg; /* Segment to iterate through */
210217 int flags; /* Mask of configuration flags */
210218 int iLeafPgno; /* Current leaf page number */
210219 Fts5Data *pLeaf; /* Current leaf data */
210220 Fts5Data *pNextLeaf; /* Leaf page (iLeafPgno+1) */
210221 int iLeafOffset; /* Byte offset within current leaf */
210222
210223 /* Next method */
210224 void (*xNext)(Fts5Index*, Fts5SegIter*, int*);
210225
210226 /* The page and offset from which the current term was read. The offset
210227 ** is the offset of the first rowid in the current doclist. */
210228 int iTermLeafPgno;
210229 int iTermLeafOffset;
210230
210231 int iPgidxOff; /* Next offset in pgidx */
210232 int iEndofDoclist;
210233
210234 /* The following are only used if the FTS5_SEGITER_REVERSE flag is set. */
210235 int iRowidOffset; /* Current entry in aRowidOffset[] */
210236 int nRowidOffset; /* Allocated size of aRowidOffset[] array */
210237 int *aRowidOffset; /* Array of offset to rowid fields */
210238
210239 Fts5DlidxIter *pDlidx; /* If there is a doclist-index */
210240
210241 /* Variables populated based on current entry. */
210242 Fts5Buffer term; /* Current term */
210243 i64 iRowid; /* Current rowid */
210244 int nPos; /* Number of bytes in current position list */
210245 u8 bDel; /* True if the delete flag is set */
210246};
210247
210248/*
210249** Argument is a pointer to an Fts5Data structure that contains a
210250** leaf page.
210251*/
210252#define ASSERT_SZLEAF_OK(x) assert( \((void) (0))
210253 (x)->szLeaf==(x)->nn || (x)->szLeaf==fts5GetU16(&(x)->p[2]) \((void) (0))
210254)((void) (0))
210255
210256#define FTS5_SEGITER_ONETERM 0x01
210257#define FTS5_SEGITER_REVERSE 0x02
210258
210259/*
210260** Argument is a pointer to an Fts5Data structure that contains a leaf
210261** page. This macro evaluates to true if the leaf contains no terms, or
210262** false if it contains at least one term.
210263*/
210264#define fts5LeafIsTermless(x) ((x)->szLeaf >= (x)->nn)
210265
210266#define fts5LeafTermOff(x, i) (fts5GetU16(&(x)->p[(x)->szLeaf + (i)*2]))
210267
210268#define fts5LeafFirstRowidOff(x) (fts5GetU16((x)->p))
210269
210270/*
210271** Object for iterating through the merged results of one or more segments,
210272** visiting each term/rowid pair in the merged data.
210273**
210274** nSeg is always a power of two greater than or equal to the number of
210275** segments that this object is merging data from. Both the aSeg[] and
210276** aFirst[] arrays are sized at nSeg entries. The aSeg[] array is padded
210277** with zeroed objects - these are handled as if they were iterators opened
210278** on empty segments.
210279**
210280** The results of comparing segments aSeg[N] and aSeg[N+1], where N is an
210281** even number, is stored in aFirst[(nSeg+N)/2]. The "result" of the
210282** comparison in this context is the index of the iterator that currently
210283** points to the smaller term/rowid combination. Iterators at EOF are
210284** considered to be greater than all other iterators.
210285**
210286** aFirst[1] contains the index in aSeg[] of the iterator that points to
210287** the smallest key overall. aFirst[0] is unused.
210288**
210289** poslist:
210290** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
210291** There is no way to tell if this is populated or not.
210292*/
210293struct Fts5Iter {
210294 Fts5IndexIter base; /* Base class containing output vars */
210295
210296 Fts5Index *pIndex; /* Index that owns this iterator */
210297 Fts5Buffer poslist; /* Buffer containing current poslist */
210298 Fts5Colset *pColset; /* Restrict matches to these columns */
210299
210300 /* Invoked to set output variables. */
210301 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
210302
210303 int nSeg; /* Size of aSeg[] array */
210304 int bRev; /* True to iterate in reverse order */
210305 u8 bSkipEmpty; /* True to skip deleted entries */
210306
210307 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
210308 Fts5CResult *aFirst; /* Current merge state (see above) */
210309 Fts5SegIter aSeg[1]; /* Array of segment iterators */
210310};
210311
210312
210313/*
210314** An instance of the following type is used to iterate through the contents
210315** of a doclist-index record.
210316**
210317** pData:
210318** Record containing the doclist-index data.
210319**
210320** bEof:
210321** Set to true once iterator has reached EOF.
210322**
210323** iOff:
210324** Set to the current offset within record pData.
210325*/
210326struct Fts5DlidxLvl {
210327 Fts5Data *pData; /* Data for current page of this level */
210328 int iOff; /* Current offset into pData */
210329 int bEof; /* At EOF already */
210330 int iFirstOff; /* Used by reverse iterators */
210331
210332 /* Output variables */
210333 int iLeafPgno; /* Page number of current leaf page */
210334 i64 iRowid; /* First rowid on leaf iLeafPgno */
210335};
210336struct Fts5DlidxIter {
210337 int nLvl;
210338 int iSegid;
210339 Fts5DlidxLvl aLvl[1];
210340};
210341
210342static void fts5PutU16(u8 *aOut, u16 iVal){
210343 aOut[0] = (iVal>>8);
210344 aOut[1] = (iVal&0xFF);
210345}
210346
210347static u16 fts5GetU16(const u8 *aIn){
210348 return ((u16)aIn[0] << 8) + aIn[1];
210349}
210350
210351/*
210352** Allocate and return a buffer at least nByte bytes in size.
210353**
210354** If an OOM error is encountered, return NULL and set the error code in
210355** the Fts5Index handle passed as the first argument.
210356*/
210357static void *fts5IdxMalloc(Fts5Index *p, sqlite3_int64 nByte){
210358 return sqlite3Fts5MallocZero(&p->rc, nByte);
210359}
210360
210361/*
210362** Compare the contents of the pLeft buffer with the pRight/nRight blob.
210363**
210364** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
210365** +ve if pRight is smaller than pLeft. In other words:
210366**
210367** res = *pLeft - *pRight
210368*/
210369#ifdef SQLITE_DEBUG
210370static int fts5BufferCompareBlob(
210371 Fts5Buffer *pLeft, /* Left hand side of comparison */
210372 const u8 *pRight, int nRight /* Right hand side of comparison */
210373){
210374 int nCmp = MIN(pLeft->n, nRight)((pLeft->n)<(nRight)?(pLeft->n):(nRight));
210375 int res = memcmp(pLeft->p, pRight, nCmp);
210376 return (res==0 ? (pLeft->n - nRight) : res);
210377}
210378#endif
210379
210380/*
210381** Compare the contents of the two buffers using memcmp(). If one buffer
210382** is a prefix of the other, it is considered the lesser.
210383**
210384** Return -ve if pLeft is smaller than pRight, 0 if they are equal or
210385** +ve if pRight is smaller than pLeft. In other words:
210386**
210387** res = *pLeft - *pRight
210388*/
210389static int fts5BufferCompare(Fts5Buffer *pLeft, Fts5Buffer *pRight){
210390 int nCmp = MIN(pLeft->n, pRight->n)((pLeft->n)<(pRight->n)?(pLeft->n):(pRight->n)
)
;
210391 int res = fts5Memcmp(pLeft->p, pRight->p, nCmp);
210392 return (res==0 ? (pLeft->n - pRight->n) : res);
210393}
210394
210395static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
210396 int ret;
210397 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
210398 return ret;
210399}
210400
210401/*
210402** Close the read-only blob handle, if it is open.
210403*/
210404static void fts5CloseReader(Fts5Index *p){
210405 if( p->pReader ){
210406 sqlite3_blob *pReader = p->pReader;
210407 p->pReader = 0;
210408 sqlite3_blob_close(pReader);
210409 }
210410}
210411
210412/*
210413** Retrieve a record from the %_data table.
210414**
210415** If an error occurs, NULL is returned and an error left in the
210416** Fts5Index object.
210417*/
210418static Fts5Data *fts5DataRead(Fts5Index *p, i64 iRowid){
210419 Fts5Data *pRet = 0;
210420 if( p->rc==SQLITE_OK0 ){
210421 int rc = SQLITE_OK0;
210422
210423 if( p->pReader ){
210424 /* This call may return SQLITE_ABORT if there has been a savepoint
210425 ** rollback since it was last used. In this case a new blob handle
210426 ** is required. */
210427 sqlite3_blob *pBlob = p->pReader;
210428 p->pReader = 0;
210429 rc = sqlite3_blob_reopen(pBlob, iRowid);
210430 assert( p->pReader==0 )((void) (0));
210431 p->pReader = pBlob;
210432 if( rc!=SQLITE_OK0 ){
210433 fts5CloseReader(p);
210434 }
210435 if( rc==SQLITE_ABORT4 ) rc = SQLITE_OK0;
210436 }
210437
210438 /* If the blob handle is not open at this point, open it and seek
210439 ** to the requested entry. */
210440 if( p->pReader==0 && rc==SQLITE_OK0 ){
210441 Fts5Config *pConfig = p->pConfig;
210442 rc = sqlite3_blob_open(pConfig->db,
210443 pConfig->zDb, p->zDataTbl, "block", iRowid, 0, &p->pReader
210444 );
210445 }
210446
210447 /* If either of the sqlite3_blob_open() or sqlite3_blob_reopen() calls
210448 ** above returned SQLITE_ERROR, return SQLITE_CORRUPT_VTAB instead.
210449 ** All the reasons those functions might return SQLITE_ERROR - missing
210450 ** table, missing row, non-blob/text in block column - indicate
210451 ** backing store corruption. */
210452 if( rc==SQLITE_ERROR1 ) rc = FTS5_CORRUPT;
210453
210454 if( rc==SQLITE_OK0 ){
210455 u8 *aOut = 0; /* Read blob data into this buffer */
210456 int nByte = sqlite3_blob_bytes(p->pReader);
210457 sqlite3_int64 nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
210458 pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
210459 if( pRet ){
210460 pRet->nn = nByte;
210461 aOut = pRet->p = (u8*)&pRet[1];
210462 }else{
210463 rc = SQLITE_NOMEM7;
210464 }
210465
210466 if( rc==SQLITE_OK0 ){
210467 rc = sqlite3_blob_read(p->pReader, aOut, nByte, 0);
210468 }
210469 if( rc!=SQLITE_OK0 ){
210470 sqlite3_free(pRet);
210471 pRet = 0;
210472 }else{
210473 /* TODO1: Fix this */
210474 pRet->p[nByte] = 0x00;
210475 pRet->szLeaf = fts5GetU16(&pRet->p[2]);
210476 }
210477 }
210478 p->rc = rc;
210479 p->nRead++;
210480 }
210481
210482 assert( (pRet==0)==(p->rc!=SQLITE_OK) )((void) (0));
210483 return pRet;
210484}
210485
210486/*
210487** Release a reference to data record returned by an earlier call to
210488** fts5DataRead().
210489*/
210490static void fts5DataRelease(Fts5Data *pData){
210491 sqlite3_free(pData);
210492}
210493
210494static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
210495 Fts5Data *pRet = fts5DataRead(p, iRowid);
210496 if( pRet ){
210497 if( pRet->szLeaf>pRet->nn ){
210498 p->rc = FTS5_CORRUPT;
210499 fts5DataRelease(pRet);
210500 pRet = 0;
210501 }
210502 }
210503 return pRet;
210504}
210505
210506static int fts5IndexPrepareStmt(
210507 Fts5Index *p,
210508 sqlite3_stmt **ppStmt,
210509 char *zSql
210510){
210511 if( p->rc==SQLITE_OK0 ){
210512 if( zSql ){
210513 p->rc = sqlite3_prepare_v3(p->pConfig->db, zSql, -1,
210514 SQLITE_PREPARE_PERSISTENT0x01|SQLITE_PREPARE_NO_VTAB0x04,
210515 ppStmt, 0);
210516 }else{
210517 p->rc = SQLITE_NOMEM7;
210518 }
210519 }
210520 sqlite3_free(zSql);
210521 return p->rc;
210522}
210523
210524
210525/*
210526** INSERT OR REPLACE a record into the %_data table.
210527*/
210528static void fts5DataWrite(Fts5Index *p, i64 iRowid, const u8 *pData, int nData){
210529 if( p->rc!=SQLITE_OK0 ) return;
210530
210531 if( p->pWriter==0 ){
210532 Fts5Config *pConfig = p->pConfig;
210533 fts5IndexPrepareStmt(p, &p->pWriter, sqlite3_mprintf(
210534 "REPLACE INTO '%q'.'%q_data'(id, block) VALUES(?,?)",
210535 pConfig->zDb, pConfig->zName
210536 ));
210537 if( p->rc ) return;
210538 }
210539
210540 sqlite3_bind_int64(p->pWriter, 1, iRowid);
210541 sqlite3_bind_blob(p->pWriter, 2, pData, nData, SQLITE_STATIC((sqlite3_destructor_type)0));
210542 sqlite3_step(p->pWriter);
210543 p->rc = sqlite3_reset(p->pWriter);
210544 sqlite3_bind_null(p->pWriter, 2);
210545}
210546
210547/*
210548** Execute the following SQL:
210549**
210550** DELETE FROM %_data WHERE id BETWEEN $iFirst AND $iLast
210551*/
210552static void fts5DataDelete(Fts5Index *p, i64 iFirst, i64 iLast){
210553 if( p->rc!=SQLITE_OK0 ) return;
210554
210555 if( p->pDeleter==0 ){
210556 Fts5Config *pConfig = p->pConfig;
210557 char *zSql = sqlite3_mprintf(
210558 "DELETE FROM '%q'.'%q_data' WHERE id>=? AND id<=?",
210559 pConfig->zDb, pConfig->zName
210560 );
210561 if( fts5IndexPrepareStmt(p, &p->pDeleter, zSql) ) return;
210562 }
210563
210564 sqlite3_bind_int64(p->pDeleter, 1, iFirst);
210565 sqlite3_bind_int64(p->pDeleter, 2, iLast);
210566 sqlite3_step(p->pDeleter);
210567 p->rc = sqlite3_reset(p->pDeleter);
210568}
210569
210570/*
210571** Remove all records associated with segment iSegid.
210572*/
210573static void fts5DataRemoveSegment(Fts5Index *p, int iSegid){
210574 i64 iFirst = FTS5_SEGMENT_ROWID(iSegid, 0);
210575 i64 iLast = FTS5_SEGMENT_ROWID(iSegid+1, 0)-1;
210576 fts5DataDelete(p, iFirst, iLast);
210577 if( p->pIdxDeleter==0 ){
210578 Fts5Config *pConfig = p->pConfig;
210579 fts5IndexPrepareStmt(p, &p->pIdxDeleter, sqlite3_mprintf(
210580 "DELETE FROM '%q'.'%q_idx' WHERE segid=?",
210581 pConfig->zDb, pConfig->zName
210582 ));
210583 }
210584 if( p->rc==SQLITE_OK0 ){
210585 sqlite3_bind_int(p->pIdxDeleter, 1, iSegid);
210586 sqlite3_step(p->pIdxDeleter);
210587 p->rc = sqlite3_reset(p->pIdxDeleter);
210588 }
210589}
210590
210591/*
210592** Release a reference to an Fts5Structure object returned by an earlier
210593** call to fts5StructureRead() or fts5StructureDecode().
210594*/
210595static void fts5StructureRelease(Fts5Structure *pStruct){
210596 if( pStruct && 0>=(--pStruct->nRef) ){
210597 int i;
210598 assert( pStruct->nRef==0 )((void) (0));
210599 for(i=0; i<pStruct->nLevel; i++){
210600 sqlite3_free(pStruct->aLevel[i].aSeg);
210601 }
210602 sqlite3_free(pStruct);
210603 }
210604}
210605
210606static void fts5StructureRef(Fts5Structure *pStruct){
210607 pStruct->nRef++;
210608}
210609
210610/*
210611** Deserialize and return the structure record currently stored in serialized
210612** form within buffer pData/nData.
210613**
210614** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
210615** are over-allocated by one slot. This allows the structure contents
210616** to be more easily edited.
210617**
210618** If an error occurs, *ppOut is set to NULL and an SQLite error code
210619** returned. Otherwise, *ppOut is set to point to the new object and
210620** SQLITE_OK returned.
210621*/
210622static int fts5StructureDecode(
210623 const u8 *pData, /* Buffer containing serialized structure */
210624 int nData, /* Size of buffer pData in bytes */
210625 int *piCookie, /* Configuration cookie value */
210626 Fts5Structure **ppOut /* OUT: Deserialized object */
210627){
210628 int rc = SQLITE_OK0;
210629 int i = 0;
210630 int iLvl;
210631 int nLevel = 0;
210632 int nSegment = 0;
210633 sqlite3_int64 nByte; /* Bytes of space to allocate at pRet */
210634 Fts5Structure *pRet = 0; /* Structure object to return */
210635
210636 /* Grab the cookie value */
210637 if( piCookie ) *piCookie = sqlite3Fts5Get32(pData);
210638 i = 4;
210639
210640 /* Read the total number of levels and segments from the start of the
210641 ** structure record. */
210642 i += fts5GetVarint32(&pData[i], nLevel);
210643 i += fts5GetVarint32(&pData[i], nSegment);
210644 if( nLevel>FTS5_MAX_SEGMENT || nLevel<0
210645 || nSegment>FTS5_MAX_SEGMENT || nSegment<0
210646 ){
210647 return FTS5_CORRUPT;
210648 }
210649 nByte = (
210650 sizeof(Fts5Structure) + /* Main structure */
210651 sizeof(Fts5StructureLevel) * (nLevel-1) /* aLevel[] array */
210652 );
210653 pRet = (Fts5Structure*)sqlite3Fts5MallocZero(&rc, nByte);
210654
210655 if( pRet ){
210656 pRet->nRef = 1;
210657 pRet->nLevel = nLevel;
210658 pRet->nSegment = nSegment;
210659 i += sqlite3Fts5GetVarint(&pData[i], &pRet->nWriteCounter);
210660
210661 for(iLvl=0; rc==SQLITE_OK0 && iLvl<nLevel; iLvl++){
210662 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
210663 int nTotal = 0;
210664 int iSeg;
210665
210666 if( i>=nData ){
210667 rc = FTS5_CORRUPT;
210668 }else{
210669 i += fts5GetVarint32(&pData[i], pLvl->nMerge);
210670 i += fts5GetVarint32(&pData[i], nTotal);
210671 if( nTotal<pLvl->nMerge ) rc = FTS5_CORRUPT;
210672 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
210673 nTotal * sizeof(Fts5StructureSegment)
210674 );
210675 nSegment -= nTotal;
210676 }
210677
210678 if( rc==SQLITE_OK0 ){
210679 pLvl->nSeg = nTotal;
210680 for(iSeg=0; iSeg<nTotal; iSeg++){
210681 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
210682 if( i>=nData ){
210683 rc = FTS5_CORRUPT;
210684 break;
210685 }
210686 i += fts5GetVarint32(&pData[i], pSeg->iSegid);
210687 i += fts5GetVarint32(&pData[i], pSeg->pgnoFirst);
210688 i += fts5GetVarint32(&pData[i], pSeg->pgnoLast);
210689 if( pSeg->pgnoLast<pSeg->pgnoFirst ){
210690 rc = FTS5_CORRUPT;
210691 break;
210692 }
210693 }
210694 if( iLvl>0 && pLvl[-1].nMerge && nTotal==0 ) rc = FTS5_CORRUPT;
210695 if( iLvl==nLevel-1 && pLvl->nMerge ) rc = FTS5_CORRUPT;
210696 }
210697 }
210698 if( nSegment!=0 && rc==SQLITE_OK0 ) rc = FTS5_CORRUPT;
210699
210700 if( rc!=SQLITE_OK0 ){
210701 fts5StructureRelease(pRet);
210702 pRet = 0;
210703 }
210704 }
210705
210706 *ppOut = pRet;
210707 return rc;
210708}
210709
210710/*
210711**
210712*/
210713static void fts5StructureAddLevel(int *pRc, Fts5Structure **ppStruct){
210714 if( *pRc==SQLITE_OK0 ){
210715 Fts5Structure *pStruct = *ppStruct;
210716 int nLevel = pStruct->nLevel;
210717 sqlite3_int64 nByte = (
210718 sizeof(Fts5Structure) + /* Main structure */
210719 sizeof(Fts5StructureLevel) * (nLevel+1) /* aLevel[] array */
210720 );
210721
210722 pStruct = sqlite3_realloc64(pStruct, nByte);
210723 if( pStruct ){
210724 memset(&pStruct->aLevel[nLevel], 0, sizeof(Fts5StructureLevel));
210725 pStruct->nLevel++;
210726 *ppStruct = pStruct;
210727 }else{
210728 *pRc = SQLITE_NOMEM7;
210729 }
210730 }
210731}
210732
210733/*
210734** Extend level iLvl so that there is room for at least nExtra more
210735** segments.
210736*/
210737static void fts5StructureExtendLevel(
210738 int *pRc,
210739 Fts5Structure *pStruct,
210740 int iLvl,
210741 int nExtra,
210742 int bInsert
210743){
210744 if( *pRc==SQLITE_OK0 ){
210745 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
210746 Fts5StructureSegment *aNew;
210747 sqlite3_int64 nByte;
210748
210749 nByte = (pLvl->nSeg + nExtra) * sizeof(Fts5StructureSegment);
210750 aNew = sqlite3_realloc64(pLvl->aSeg, nByte);
210751 if( aNew ){
210752 if( bInsert==0 ){
210753 memset(&aNew[pLvl->nSeg], 0, sizeof(Fts5StructureSegment) * nExtra);
210754 }else{
210755 int nMove = pLvl->nSeg * sizeof(Fts5StructureSegment);
210756 memmove(&aNew[nExtra], aNew, nMove);
210757 memset(aNew, 0, sizeof(Fts5StructureSegment) * nExtra);
210758 }
210759 pLvl->aSeg = aNew;
210760 }else{
210761 *pRc = SQLITE_NOMEM7;
210762 }
210763 }
210764}
210765
210766static Fts5Structure *fts5StructureReadUncached(Fts5Index *p){
210767 Fts5Structure *pRet = 0;
210768 Fts5Config *pConfig = p->pConfig;
210769 int iCookie; /* Configuration cookie */
210770 Fts5Data *pData;
210771
210772 pData = fts5DataRead(p, FTS5_STRUCTURE_ROWID);
210773 if( p->rc==SQLITE_OK0 ){
210774 /* TODO: Do we need this if the leaf-index is appended? Probably... */
210775 memset(&pData->p[pData->nn], 0, FTS5_DATA_PADDING);
210776 p->rc = fts5StructureDecode(pData->p, pData->nn, &iCookie, &pRet);
210777 if( p->rc==SQLITE_OK0 && (pConfig->pgsz==0 || pConfig->iCookie!=iCookie) ){
210778 p->rc = sqlite3Fts5ConfigLoad(pConfig, iCookie);
210779 }
210780 fts5DataRelease(pData);
210781 if( p->rc!=SQLITE_OK0 ){
210782 fts5StructureRelease(pRet);
210783 pRet = 0;
210784 }
210785 }
210786
210787 return pRet;
210788}
210789
210790static i64 fts5IndexDataVersion(Fts5Index *p){
210791 i64 iVersion = 0;
210792
210793 if( p->rc==SQLITE_OK0 ){
210794 if( p->pDataVersion==0 ){
210795 p->rc = fts5IndexPrepareStmt(p, &p->pDataVersion,
210796 sqlite3_mprintf("PRAGMA %Q.data_version", p->pConfig->zDb)
210797 );
210798 if( p->rc ) return 0;
210799 }
210800
210801 if( SQLITE_ROW100==sqlite3_step(p->pDataVersion) ){
210802 iVersion = sqlite3_column_int64(p->pDataVersion, 0);
210803 }
210804 p->rc = sqlite3_reset(p->pDataVersion);
210805 }
210806
210807 return iVersion;
210808}
210809
210810/*
210811** Read, deserialize and return the structure record.
210812**
210813** The Fts5Structure.aLevel[] and each Fts5StructureLevel.aSeg[] array
210814** are over-allocated as described for function fts5StructureDecode()
210815** above.
210816**
210817** If an error occurs, NULL is returned and an error code left in the
210818** Fts5Index handle. If an error has already occurred when this function
210819** is called, it is a no-op.
210820*/
210821static Fts5Structure *fts5StructureRead(Fts5Index *p){
210822
210823 if( p->pStruct==0 ){
210824 p->iStructVersion = fts5IndexDataVersion(p);
210825 if( p->rc==SQLITE_OK0 ){
210826 p->pStruct = fts5StructureReadUncached(p);
210827 }
210828 }
210829
210830#if 0
210831 else{
210832 Fts5Structure *pTest = fts5StructureReadUncached(p);
210833 if( pTest ){
210834 int i, j;
210835 assert_nc( p->pStruct->nSegment==pTest->nSegment );
210836 assert_nc( p->pStruct->nLevel==pTest->nLevel );
210837 for(i=0; i<pTest->nLevel; i++){
210838 assert_nc( p->pStruct->aLevel[i].nMerge==pTest->aLevel[i].nMerge );
210839 assert_nc( p->pStruct->aLevel[i].nSeg==pTest->aLevel[i].nSeg );
210840 for(j=0; j<pTest->aLevel[i].nSeg; j++){
210841 Fts5StructureSegment *p1 = &pTest->aLevel[i].aSeg[j];
210842 Fts5StructureSegment *p2 = &p->pStruct->aLevel[i].aSeg[j];
210843 assert_nc( p1->iSegid==p2->iSegid );
210844 assert_nc( p1->pgnoFirst==p2->pgnoFirst );
210845 assert_nc( p1->pgnoLast==p2->pgnoLast );
210846 }
210847 }
210848 fts5StructureRelease(pTest);
210849 }
210850 }
210851#endif
210852
210853 if( p->rc!=SQLITE_OK0 ) return 0;
210854 assert( p->iStructVersion!=0 )((void) (0));
210855 assert( p->pStruct!=0 )((void) (0));
210856 fts5StructureRef(p->pStruct);
210857 return p->pStruct;
210858}
210859
210860static void fts5StructureInvalidate(Fts5Index *p){
210861 if( p->pStruct ){
210862 fts5StructureRelease(p->pStruct);
210863 p->pStruct = 0;
210864 }
210865}
210866
210867/*
210868** Return the total number of segments in index structure pStruct. This
210869** function is only ever used as part of assert() conditions.
210870*/
210871#ifdef SQLITE_DEBUG
210872static int fts5StructureCountSegments(Fts5Structure *pStruct){
210873 int nSegment = 0; /* Total number of segments */
210874 if( pStruct ){
210875 int iLvl; /* Used to iterate through levels */
210876 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
210877 nSegment += pStruct->aLevel[iLvl].nSeg;
210878 }
210879 }
210880
210881 return nSegment;
210882}
210883#endif
210884
210885#define fts5BufferSafeAppendBlob(pBuf, pBlob, nBlob) { \
210886 assert( (pBuf)->nSpace>=((pBuf)->n+nBlob) )((void) (0)); \
210887 memcpy(&(pBuf)->p[(pBuf)->n], pBlob, nBlob); \
210888 (pBuf)->n += nBlob; \
210889}
210890
210891#define fts5BufferSafeAppendVarint(pBuf, iVal) { \
210892 (pBuf)->n += sqlite3Fts5PutVarint(&(pBuf)->p[(pBuf)->n], (iVal)); \
210893 assert( (pBuf)->nSpace>=(pBuf)->n )((void) (0)); \
210894}
210895
210896
210897/*
210898** Serialize and store the "structure" record.
210899**
210900** If an error occurs, leave an error code in the Fts5Index object. If an
210901** error has already occurred, this function is a no-op.
210902*/
210903static void fts5StructureWrite(Fts5Index *p, Fts5Structure *pStruct){
210904 if( p->rc==SQLITE_OK0 ){
210905 Fts5Buffer buf; /* Buffer to serialize record into */
210906 int iLvl; /* Used to iterate through levels */
210907 int iCookie; /* Cookie value to store */
210908
210909 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) )((void) (0));
210910 memset(&buf, 0, sizeof(Fts5Buffer));
210911
210912 /* Append the current configuration cookie */
210913 iCookie = p->pConfig->iCookie;
210914 if( iCookie<0 ) iCookie = 0;
210915
210916 if( 0==sqlite3Fts5BufferSize(&p->rc, &buf, 4+9+9+9) ){
210917 sqlite3Fts5Put32(buf.p, iCookie);
210918 buf.n = 4;
210919 fts5BufferSafeAppendVarint(&buf, pStruct->nLevel);
210920 fts5BufferSafeAppendVarint(&buf, pStruct->nSegment);
210921 fts5BufferSafeAppendVarint(&buf, (i64)pStruct->nWriteCounter);
210922 }
210923
210924 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
210925 int iSeg; /* Used to iterate through segments */
210926 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
210927 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nMerge);
210928 fts5BufferAppendVarint(&p->rc, &buf, pLvl->nSeg);
210929 assert( pLvl->nMerge<=pLvl->nSeg )((void) (0));
210930
210931 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
210932 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].iSegid);
210933 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoFirst);
210934 fts5BufferAppendVarint(&p->rc, &buf, pLvl->aSeg[iSeg].pgnoLast);
210935 }
210936 }
210937
210938 fts5DataWrite(p, FTS5_STRUCTURE_ROWID, buf.p, buf.n);
210939 fts5BufferFree(&buf);
210940 }
210941}
210942
210943#if 0
210944static void fts5DebugStructure(int*,Fts5Buffer*,Fts5Structure*);
210945static void fts5PrintStructure(const char *zCaption, Fts5Structure *pStruct){
210946 int rc = SQLITE_OK0;
210947 Fts5Buffer buf;
210948 memset(&buf, 0, sizeof(buf));
210949 fts5DebugStructure(&rc, &buf, pStruct);
210950 fprintf(stdoutstdout, "%s: %s\n", zCaption, buf.p);
210951 fflush(stdoutstdout);
210952 fts5BufferFree(&buf);
210953}
210954#else
210955# define fts5PrintStructure(x,y)
210956#endif
210957
210958static int fts5SegmentSize(Fts5StructureSegment *pSeg){
210959 return 1 + pSeg->pgnoLast - pSeg->pgnoFirst;
210960}
210961
210962/*
210963** Return a copy of index structure pStruct. Except, promote as many
210964** segments as possible to level iPromote. If an OOM occurs, NULL is
210965** returned.
210966*/
210967static void fts5StructurePromoteTo(
210968 Fts5Index *p,
210969 int iPromote,
210970 int szPromote,
210971 Fts5Structure *pStruct
210972){
210973 int il, is;
210974 Fts5StructureLevel *pOut = &pStruct->aLevel[iPromote];
210975
210976 if( pOut->nMerge==0 ){
210977 for(il=iPromote+1; il<pStruct->nLevel; il++){
210978 Fts5StructureLevel *pLvl = &pStruct->aLevel[il];
210979 if( pLvl->nMerge ) return;
210980 for(is=pLvl->nSeg-1; is>=0; is--){
210981 int sz = fts5SegmentSize(&pLvl->aSeg[is]);
210982 if( sz>szPromote ) return;
210983 fts5StructureExtendLevel(&p->rc, pStruct, iPromote, 1, 1);
210984 if( p->rc ) return;
210985 memcpy(pOut->aSeg, &pLvl->aSeg[is], sizeof(Fts5StructureSegment));
210986 pOut->nSeg++;
210987 pLvl->nSeg--;
210988 }
210989 }
210990 }
210991}
210992
210993/*
210994** A new segment has just been written to level iLvl of index structure
210995** pStruct. This function determines if any segments should be promoted
210996** as a result. Segments are promoted in two scenarios:
210997**
210998** a) If the segment just written is smaller than one or more segments
210999** within the previous populated level, it is promoted to the previous
211000** populated level.
211001**
211002** b) If the segment just written is larger than the newest segment on
211003** the next populated level, then that segment, and any other adjacent
211004** segments that are also smaller than the one just written, are
211005** promoted.
211006**
211007** If one or more segments are promoted, the structure object is updated
211008** to reflect this.
211009*/
211010static void fts5StructurePromote(
211011 Fts5Index *p, /* FTS5 backend object */
211012 int iLvl, /* Index level just updated */
211013 Fts5Structure *pStruct /* Index structure */
211014){
211015 if( p->rc==SQLITE_OK0 ){
211016 int iTst;
211017 int iPromote = -1;
211018 int szPromote = 0; /* Promote anything this size or smaller */
211019 Fts5StructureSegment *pSeg; /* Segment just written */
211020 int szSeg; /* Size of segment just written */
211021 int nSeg = pStruct->aLevel[iLvl].nSeg;
211022
211023 if( nSeg==0 ) return;
211024 pSeg = &pStruct->aLevel[iLvl].aSeg[pStruct->aLevel[iLvl].nSeg-1];
211025 szSeg = (1 + pSeg->pgnoLast - pSeg->pgnoFirst);
211026
211027 /* Check for condition (a) */
211028 for(iTst=iLvl-1; iTst>=0 && pStruct->aLevel[iTst].nSeg==0; iTst--);
211029 if( iTst>=0 ){
211030 int i;
211031 int szMax = 0;
211032 Fts5StructureLevel *pTst = &pStruct->aLevel[iTst];
211033 assert( pTst->nMerge==0 )((void) (0));
211034 for(i=0; i<pTst->nSeg; i++){
211035 int sz = pTst->aSeg[i].pgnoLast - pTst->aSeg[i].pgnoFirst + 1;
211036 if( sz>szMax ) szMax = sz;
211037 }
211038 if( szMax>=szSeg ){
211039 /* Condition (a) is true. Promote the newest segment on level
211040 ** iLvl to level iTst. */
211041 iPromote = iTst;
211042 szPromote = szMax;
211043 }
211044 }
211045
211046 /* If condition (a) is not met, assume (b) is true. StructurePromoteTo()
211047 ** is a no-op if it is not. */
211048 if( iPromote<0 ){
211049 iPromote = iLvl;
211050 szPromote = szSeg;
211051 }
211052 fts5StructurePromoteTo(p, iPromote, szPromote, pStruct);
211053 }
211054}
211055
211056
211057/*
211058** Advance the iterator passed as the only argument. If the end of the
211059** doclist-index page is reached, return non-zero.
211060*/
211061static int fts5DlidxLvlNext(Fts5DlidxLvl *pLvl){
211062 Fts5Data *pData = pLvl->pData;
211063
211064 if( pLvl->iOff==0 ){
211065 assert( pLvl->bEof==0 )((void) (0));
211066 pLvl->iOff = 1;
211067 pLvl->iOff += fts5GetVarint32(&pData->p[1], pLvl->iLeafPgno);
211068 pLvl->iOff += fts5GetVarint(&pData->p[pLvl->iOff], (u64*)&pLvl->iRowid);
211069 pLvl->iFirstOff = pLvl->iOff;
211070 }else{
211071 int iOff;
211072 for(iOff=pLvl->iOff; iOff<pData->nn; iOff++){
211073 if( pData->p[iOff] ) break;
211074 }
211075
211076 if( iOff<pData->nn ){
211077 i64 iVal;
211078 pLvl->iLeafPgno += (iOff - pLvl->iOff) + 1;
211079 iOff += fts5GetVarint(&pData->p[iOff], (u64*)&iVal);
211080 pLvl->iRowid += iVal;
211081 pLvl->iOff = iOff;
211082 }else{
211083 pLvl->bEof = 1;
211084 }
211085 }
211086
211087 return pLvl->bEof;
211088}
211089
211090/*
211091** Advance the iterator passed as the only argument.
211092*/
211093static int fts5DlidxIterNextR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
211094 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
211095
211096 assert( iLvl<pIter->nLvl )((void) (0));
211097 if( fts5DlidxLvlNext(pLvl) ){
211098 if( (iLvl+1) < pIter->nLvl ){
211099 fts5DlidxIterNextR(p, pIter, iLvl+1);
211100 if( pLvl[1].bEof==0 ){
211101 fts5DataRelease(pLvl->pData);
211102 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
211103 pLvl->pData = fts5DataRead(p,
211104 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
211105 );
211106 if( pLvl->pData ) fts5DlidxLvlNext(pLvl);
211107 }
211108 }
211109 }
211110
211111 return pIter->aLvl[0].bEof;
211112}
211113static int fts5DlidxIterNext(Fts5Index *p, Fts5DlidxIter *pIter){
211114 return fts5DlidxIterNextR(p, pIter, 0);
211115}
211116
211117/*
211118** The iterator passed as the first argument has the following fields set
211119** as follows. This function sets up the rest of the iterator so that it
211120** points to the first rowid in the doclist-index.
211121**
211122** pData:
211123** pointer to doclist-index record,
211124**
211125** When this function is called pIter->iLeafPgno is the page number the
211126** doclist is associated with (the one featuring the term).
211127*/
211128static int fts5DlidxIterFirst(Fts5DlidxIter *pIter){
211129 int i;
211130 for(i=0; i<pIter->nLvl; i++){
211131 fts5DlidxLvlNext(&pIter->aLvl[i]);
211132 }
211133 return pIter->aLvl[0].bEof;
211134}
211135
211136
211137static int fts5DlidxIterEof(Fts5Index *p, Fts5DlidxIter *pIter){
211138 return p->rc!=SQLITE_OK0 || pIter->aLvl[0].bEof;
211139}
211140
211141static void fts5DlidxIterLast(Fts5Index *p, Fts5DlidxIter *pIter){
211142 int i;
211143
211144 /* Advance each level to the last entry on the last page */
211145 for(i=pIter->nLvl-1; p->rc==SQLITE_OK0 && i>=0; i--){
211146 Fts5DlidxLvl *pLvl = &pIter->aLvl[i];
211147 while( fts5DlidxLvlNext(pLvl)==0 );
211148 pLvl->bEof = 0;
211149
211150 if( i>0 ){
211151 Fts5DlidxLvl *pChild = &pLvl[-1];
211152 fts5DataRelease(pChild->pData);
211153 memset(pChild, 0, sizeof(Fts5DlidxLvl));
211154 pChild->pData = fts5DataRead(p,
211155 FTS5_DLIDX_ROWID(pIter->iSegid, i-1, pLvl->iLeafPgno)
211156 );
211157 }
211158 }
211159}
211160
211161/*
211162** Move the iterator passed as the only argument to the previous entry.
211163*/
211164static int fts5DlidxLvlPrev(Fts5DlidxLvl *pLvl){
211165 int iOff = pLvl->iOff;
211166
211167 assert( pLvl->bEof==0 )((void) (0));
211168 if( iOff<=pLvl->iFirstOff ){
211169 pLvl->bEof = 1;
211170 }else{
211171 u8 *a = pLvl->pData->p;
211172 i64 iVal;
211173 int iLimit;
211174 int ii;
211175 int nZero = 0;
211176
211177 /* Currently iOff points to the first byte of a varint. This block
211178 ** decrements iOff until it points to the first byte of the previous
211179 ** varint. Taking care not to read any memory locations that occur
211180 ** before the buffer in memory. */
211181 iLimit = (iOff>9 ? iOff-9 : 0);
211182 for(iOff--; iOff>iLimit; iOff--){
211183 if( (a[iOff-1] & 0x80)==0 ) break;
211184 }
211185
211186 fts5GetVarint(&a[iOff], (u64*)&iVal);
211187 pLvl->iRowid -= iVal;
211188 pLvl->iLeafPgno--;
211189
211190 /* Skip backwards past any 0x00 varints. */
211191 for(ii=iOff-1; ii>=pLvl->iFirstOff && a[ii]==0x00; ii--){
211192 nZero++;
211193 }
211194 if( ii>=pLvl->iFirstOff && (a[ii] & 0x80) ){
211195 /* The byte immediately before the last 0x00 byte has the 0x80 bit
211196 ** set. So the last 0x00 is only a varint 0 if there are 8 more 0x80
211197 ** bytes before a[ii]. */
211198 int bZero = 0; /* True if last 0x00 counts */
211199 if( (ii-8)>=pLvl->iFirstOff ){
211200 int j;
211201 for(j=1; j<=8 && (a[ii-j] & 0x80); j++);
211202 bZero = (j>8);
211203 }
211204 if( bZero==0 ) nZero--;
211205 }
211206 pLvl->iLeafPgno -= nZero;
211207 pLvl->iOff = iOff - nZero;
211208 }
211209
211210 return pLvl->bEof;
211211}
211212
211213static int fts5DlidxIterPrevR(Fts5Index *p, Fts5DlidxIter *pIter, int iLvl){
211214 Fts5DlidxLvl *pLvl = &pIter->aLvl[iLvl];
211215
211216 assert( iLvl<pIter->nLvl )((void) (0));
211217 if( fts5DlidxLvlPrev(pLvl) ){
211218 if( (iLvl+1) < pIter->nLvl ){
211219 fts5DlidxIterPrevR(p, pIter, iLvl+1);
211220 if( pLvl[1].bEof==0 ){
211221 fts5DataRelease(pLvl->pData);
211222 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
211223 pLvl->pData = fts5DataRead(p,
211224 FTS5_DLIDX_ROWID(pIter->iSegid, iLvl, pLvl[1].iLeafPgno)
211225 );
211226 if( pLvl->pData ){
211227 while( fts5DlidxLvlNext(pLvl)==0 );
211228 pLvl->bEof = 0;
211229 }
211230 }
211231 }
211232 }
211233
211234 return pIter->aLvl[0].bEof;
211235}
211236static int fts5DlidxIterPrev(Fts5Index *p, Fts5DlidxIter *pIter){
211237 return fts5DlidxIterPrevR(p, pIter, 0);
211238}
211239
211240/*
211241** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
211242*/
211243static void fts5DlidxIterFree(Fts5DlidxIter *pIter){
211244 if( pIter ){
211245 int i;
211246 for(i=0; i<pIter->nLvl; i++){
211247 fts5DataRelease(pIter->aLvl[i].pData);
211248 }
211249 sqlite3_free(pIter);
211250 }
211251}
211252
211253static Fts5DlidxIter *fts5DlidxIterInit(
211254 Fts5Index *p, /* Fts5 Backend to iterate within */
211255 int bRev, /* True for ORDER BY ASC */
211256 int iSegid, /* Segment id */
211257 int iLeafPg /* Leaf page number to load dlidx for */
211258){
211259 Fts5DlidxIter *pIter = 0;
211260 int i;
211261 int bDone = 0;
211262
211263 for(i=0; p->rc==SQLITE_OK0 && bDone==0; i++){
211264 sqlite3_int64 nByte = sizeof(Fts5DlidxIter) + i * sizeof(Fts5DlidxLvl);
211265 Fts5DlidxIter *pNew;
211266
211267 pNew = (Fts5DlidxIter*)sqlite3_realloc64(pIter, nByte);
211268 if( pNew==0 ){
211269 p->rc = SQLITE_NOMEM7;
211270 }else{
211271 i64 iRowid = FTS5_DLIDX_ROWID(iSegid, i, iLeafPg);
211272 Fts5DlidxLvl *pLvl = &pNew->aLvl[i];
211273 pIter = pNew;
211274 memset(pLvl, 0, sizeof(Fts5DlidxLvl));
211275 pLvl->pData = fts5DataRead(p, iRowid);
211276 if( pLvl->pData && (pLvl->pData->p[0] & 0x0001)==0 ){
211277 bDone = 1;
211278 }
211279 pIter->nLvl = i+1;
211280 }
211281 }
211282
211283 if( p->rc==SQLITE_OK0 ){
211284 pIter->iSegid = iSegid;
211285 if( bRev==0 ){
211286 fts5DlidxIterFirst(pIter);
211287 }else{
211288 fts5DlidxIterLast(p, pIter);
211289 }
211290 }
211291
211292 if( p->rc!=SQLITE_OK0 ){
211293 fts5DlidxIterFree(pIter);
211294 pIter = 0;
211295 }
211296
211297 return pIter;
211298}
211299
211300static i64 fts5DlidxIterRowid(Fts5DlidxIter *pIter){
211301 return pIter->aLvl[0].iRowid;
211302}
211303static int fts5DlidxIterPgno(Fts5DlidxIter *pIter){
211304 return pIter->aLvl[0].iLeafPgno;
211305}
211306
211307/*
211308** Load the next leaf page into the segment iterator.
211309*/
211310static void fts5SegIterNextPage(
211311 Fts5Index *p, /* FTS5 backend object */
211312 Fts5SegIter *pIter /* Iterator to advance to next page */
211313){
211314 Fts5Data *pLeaf;
211315 Fts5StructureSegment *pSeg = pIter->pSeg;
211316 fts5DataRelease(pIter->pLeaf);
211317 pIter->iLeafPgno++;
211318 if( pIter->pNextLeaf ){
211319 pIter->pLeaf = pIter->pNextLeaf;
211320 pIter->pNextLeaf = 0;
211321 }else if( pIter->iLeafPgno<=pSeg->pgnoLast ){
211322 pIter->pLeaf = fts5LeafRead(p,
211323 FTS5_SEGMENT_ROWID(pSeg->iSegid, pIter->iLeafPgno)
211324 );
211325 }else{
211326 pIter->pLeaf = 0;
211327 }
211328 pLeaf = pIter->pLeaf;
211329
211330 if( pLeaf ){
211331 pIter->iPgidxOff = pLeaf->szLeaf;
211332 if( fts5LeafIsTermless(pLeaf) ){
211333 pIter->iEndofDoclist = pLeaf->nn+1;
211334 }else{
211335 pIter->iPgidxOff += fts5GetVarint32(&pLeaf->p[pIter->iPgidxOff],
211336 pIter->iEndofDoclist
211337 );
211338 }
211339 }
211340}
211341
211342/*
211343** Argument p points to a buffer containing a varint to be interpreted as a
211344** position list size field. Read the varint and return the number of bytes
211345** read. Before returning, set *pnSz to the number of bytes in the position
211346** list, and *pbDel to true if the delete flag is set, or false otherwise.
211347*/
211348static int fts5GetPoslistSize(const u8 *p, int *pnSz, int *pbDel){
211349 int nSz;
211350 int n = 0;
211351 fts5FastGetVarint32(p, n, nSz);
211352 assert_nc( nSz>=0 );
211353 *pnSz = nSz/2;
211354 *pbDel = nSz & 0x0001;
211355 return n;
211356}
211357
211358/*
211359** Fts5SegIter.iLeafOffset currently points to the first byte of a
211360** position-list size field. Read the value of the field and store it
211361** in the following variables:
211362**
211363** Fts5SegIter.nPos
211364** Fts5SegIter.bDel
211365**
211366** Leave Fts5SegIter.iLeafOffset pointing to the first byte of the
211367** position list content (if any).
211368*/
211369static void fts5SegIterLoadNPos(Fts5Index *p, Fts5SegIter *pIter){
211370 if( p->rc==SQLITE_OK0 ){
211371 int iOff = pIter->iLeafOffset; /* Offset to read at */
211372 ASSERT_SZLEAF_OK(pIter->pLeaf);
211373 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
211374 int iEod = MIN(pIter->iEndofDoclist, pIter->pLeaf->szLeaf)((pIter->iEndofDoclist)<(pIter->pLeaf->szLeaf)?(pIter
->iEndofDoclist):(pIter->pLeaf->szLeaf))
;
211375 pIter->bDel = 0;
211376 pIter->nPos = 1;
211377 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
211378 pIter->bDel = 1;
211379 iOff++;
211380 if( iOff<iEod && pIter->pLeaf->p[iOff]==0 ){
211381 pIter->nPos = 1;
211382 iOff++;
211383 }else{
211384 pIter->nPos = 0;
211385 }
211386 }
211387 }else{
211388 int nSz;
211389 fts5FastGetVarint32(pIter->pLeaf->p, iOff, nSz);
211390 pIter->bDel = (nSz & 0x0001);
211391 pIter->nPos = nSz>>1;
211392 assert_nc( pIter->nPos>=0 );
211393 }
211394 pIter->iLeafOffset = iOff;
211395 }
211396}
211397
211398static void fts5SegIterLoadRowid(Fts5Index *p, Fts5SegIter *pIter){
211399 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
211400 int iOff = pIter->iLeafOffset;
211401
211402 ASSERT_SZLEAF_OK(pIter->pLeaf);
211403 if( iOff>=pIter->pLeaf->szLeaf ){
211404 fts5SegIterNextPage(p, pIter);
211405 if( pIter->pLeaf==0 ){
211406 if( p->rc==SQLITE_OK0 ) p->rc = FTS5_CORRUPT;
211407 return;
211408 }
211409 iOff = 4;
211410 a = pIter->pLeaf->p;
211411 }
211412 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
211413 pIter->iLeafOffset = iOff;
211414}
211415
211416/*
211417** Fts5SegIter.iLeafOffset currently points to the first byte of the
211418** "nSuffix" field of a term. Function parameter nKeep contains the value
211419** of the "nPrefix" field (if there was one - it is passed 0 if this is
211420** the first term in the segment).
211421**
211422** This function populates:
211423**
211424** Fts5SegIter.term
211425** Fts5SegIter.rowid
211426**
211427** accordingly and leaves (Fts5SegIter.iLeafOffset) set to the content of
211428** the first position list. The position list belonging to document
211429** (Fts5SegIter.iRowid).
211430*/
211431static void fts5SegIterLoadTerm(Fts5Index *p, Fts5SegIter *pIter, int nKeep){
211432 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
211433 int iOff = pIter->iLeafOffset; /* Offset to read at */
211434 int nNew; /* Bytes of new data */
211435
211436 iOff += fts5GetVarint32(&a[iOff], nNew);
211437 if( iOff+nNew>pIter->pLeaf->szLeaf || nKeep>pIter->term.n || nNew==0 ){
211438 p->rc = FTS5_CORRUPT;
211439 return;
211440 }
211441 pIter->term.n = nKeep;
211442 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
211443 assert( pIter->term.n<=pIter->term.nSpace )((void) (0));
211444 iOff += nNew;
211445 pIter->iTermLeafOffset = iOff;
211446 pIter->iTermLeafPgno = pIter->iLeafPgno;
211447 pIter->iLeafOffset = iOff;
211448
211449 if( pIter->iPgidxOff>=pIter->pLeaf->nn ){
211450 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
211451 }else{
211452 int nExtra;
211453 pIter->iPgidxOff += fts5GetVarint32(&a[pIter->iPgidxOff], nExtra);
211454 pIter->iEndofDoclist += nExtra;
211455 }
211456
211457 fts5SegIterLoadRowid(p, pIter);
211458}
211459
211460static void fts5SegIterNext(Fts5Index*, Fts5SegIter*, int*);
211461static void fts5SegIterNext_Reverse(Fts5Index*, Fts5SegIter*, int*);
211462static void fts5SegIterNext_None(Fts5Index*, Fts5SegIter*, int*);
211463
211464static void fts5SegIterSetNext(Fts5Index *p, Fts5SegIter *pIter){
211465 if( pIter->flags & FTS5_SEGITER_REVERSE ){
211466 pIter->xNext = fts5SegIterNext_Reverse;
211467 }else if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
211468 pIter->xNext = fts5SegIterNext_None;
211469 }else{
211470 pIter->xNext = fts5SegIterNext;
211471 }
211472}
211473
211474/*
211475** Initialize the iterator object pIter to iterate through the entries in
211476** segment pSeg. The iterator is left pointing to the first entry when
211477** this function returns.
211478**
211479** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
211480** an error has already occurred when this function is called, it is a no-op.
211481*/
211482static void fts5SegIterInit(
211483 Fts5Index *p, /* FTS index object */
211484 Fts5StructureSegment *pSeg, /* Description of segment */
211485 Fts5SegIter *pIter /* Object to populate */
211486){
211487 if( pSeg->pgnoFirst==0 ){
211488 /* This happens if the segment is being used as an input to an incremental
211489 ** merge and all data has already been "trimmed". See function
211490 ** fts5TrimSegments() for details. In this case leave the iterator empty.
211491 ** The caller will see the (pIter->pLeaf==0) and assume the iterator is
211492 ** at EOF already. */
211493 assert( pIter->pLeaf==0 )((void) (0));
211494 return;
211495 }
211496
211497 if( p->rc==SQLITE_OK0 ){
211498 memset(pIter, 0, sizeof(*pIter));
211499 fts5SegIterSetNext(p, pIter);
211500 pIter->pSeg = pSeg;
211501 pIter->iLeafPgno = pSeg->pgnoFirst-1;
211502 fts5SegIterNextPage(p, pIter);
211503 }
211504
211505 if( p->rc==SQLITE_OK0 ){
211506 pIter->iLeafOffset = 4;
211507 assert_nc( pIter->pLeaf->nn>4 );
211508 assert_nc( fts5LeafFirstTermOff(pIter->pLeaf)==4 );
211509 pIter->iPgidxOff = pIter->pLeaf->szLeaf+1;
211510 fts5SegIterLoadTerm(p, pIter, 0);
211511 fts5SegIterLoadNPos(p, pIter);
211512 }
211513}
211514
211515/*
211516** This function is only ever called on iterators created by calls to
211517** Fts5IndexQuery() with the FTS5INDEX_QUERY_DESC flag set.
211518**
211519** The iterator is in an unusual state when this function is called: the
211520** Fts5SegIter.iLeafOffset variable is set to the offset of the start of
211521** the position-list size field for the first relevant rowid on the page.
211522** Fts5SegIter.rowid is set, but nPos and bDel are not.
211523**
211524** This function advances the iterator so that it points to the last
211525** relevant rowid on the page and, if necessary, initializes the
211526** aRowidOffset[] and iRowidOffset variables. At this point the iterator
211527** is in its regular state - Fts5SegIter.iLeafOffset points to the first
211528** byte of the position list content associated with said rowid.
211529*/
211530static void fts5SegIterReverseInitPage(Fts5Index *p, Fts5SegIter *pIter){
211531 int eDetail = p->pConfig->eDetail;
211532 int n = pIter->pLeaf->szLeaf;
211533 int i = pIter->iLeafOffset;
211534 u8 *a = pIter->pLeaf->p;
211535 int iRowidOffset = 0;
211536
211537 if( n>pIter->iEndofDoclist ){
211538 n = pIter->iEndofDoclist;
211539 }
211540
211541 ASSERT_SZLEAF_OK(pIter->pLeaf);
211542 while( 1 ){
211543 i64 iDelta = 0;
211544
211545 if( eDetail==FTS5_DETAIL_NONE ){
211546 /* todo */
211547 if( i<n && a[i]==0 ){
211548 i++;
211549 if( i<n && a[i]==0 ) i++;
211550 }
211551 }else{
211552 int nPos;
211553 int bDummy;
211554 i += fts5GetPoslistSize(&a[i], &nPos, &bDummy);
211555 i += nPos;
211556 }
211557 if( i>=n ) break;
211558 i += fts5GetVarint(&a[i], (u64*)&iDelta);
211559 pIter->iRowid += iDelta;
211560
211561 /* If necessary, grow the pIter->aRowidOffset[] array. */
211562 if( iRowidOffset>=pIter->nRowidOffset ){
211563 int nNew = pIter->nRowidOffset + 8;
211564 int *aNew = (int*)sqlite3_realloc64(pIter->aRowidOffset,nNew*sizeof(int));
211565 if( aNew==0 ){
211566 p->rc = SQLITE_NOMEM7;
211567 break;
211568 }
211569 pIter->aRowidOffset = aNew;
211570 pIter->nRowidOffset = nNew;
211571 }
211572
211573 pIter->aRowidOffset[iRowidOffset++] = pIter->iLeafOffset;
211574 pIter->iLeafOffset = i;
211575 }
211576 pIter->iRowidOffset = iRowidOffset;
211577 fts5SegIterLoadNPos(p, pIter);
211578}
211579
211580/*
211581**
211582*/
211583static void fts5SegIterReverseNewPage(Fts5Index *p, Fts5SegIter *pIter){
211584 assert( pIter->flags & FTS5_SEGITER_REVERSE )((void) (0));
211585 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
211586
211587 fts5DataRelease(pIter->pLeaf);
211588 pIter->pLeaf = 0;
211589 while( p->rc==SQLITE_OK0 && pIter->iLeafPgno>pIter->iTermLeafPgno ){
211590 Fts5Data *pNew;
211591 pIter->iLeafPgno--;
211592 pNew = fts5DataRead(p, FTS5_SEGMENT_ROWID(
211593 pIter->pSeg->iSegid, pIter->iLeafPgno
211594 ));
211595 if( pNew ){
211596 /* iTermLeafOffset may be equal to szLeaf if the term is the last
211597 ** thing on the page - i.e. the first rowid is on the following page.
211598 ** In this case leave pIter->pLeaf==0, this iterator is at EOF. */
211599 if( pIter->iLeafPgno==pIter->iTermLeafPgno ){
211600 assert( pIter->pLeaf==0 )((void) (0));
211601 if( pIter->iTermLeafOffset<pNew->szLeaf ){
211602 pIter->pLeaf = pNew;
211603 pIter->iLeafOffset = pIter->iTermLeafOffset;
211604 }
211605 }else{
211606 int iRowidOff;
211607 iRowidOff = fts5LeafFirstRowidOff(pNew);
211608 if( iRowidOff ){
211609 pIter->pLeaf = pNew;
211610 pIter->iLeafOffset = iRowidOff;
211611 }
211612 }
211613
211614 if( pIter->pLeaf ){
211615 u8 *a = &pIter->pLeaf->p[pIter->iLeafOffset];
211616 pIter->iLeafOffset += fts5GetVarint(a, (u64*)&pIter->iRowid);
211617 break;
211618 }else{
211619 fts5DataRelease(pNew);
211620 }
211621 }
211622 }
211623
211624 if( pIter->pLeaf ){
211625 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
211626 fts5SegIterReverseInitPage(p, pIter);
211627 }
211628}
211629
211630/*
211631** Return true if the iterator passed as the second argument currently
211632** points to a delete marker. A delete marker is an entry with a 0 byte
211633** position-list.
211634*/
211635static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
211636 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
211637 return (p->rc==SQLITE_OK0 && pSeg->pLeaf && pSeg->nPos==0);
211638}
211639
211640/*
211641** Advance iterator pIter to the next entry.
211642**
211643** This version of fts5SegIterNext() is only used by reverse iterators.
211644*/
211645static void fts5SegIterNext_Reverse(
211646 Fts5Index *p, /* FTS5 backend object */
211647 Fts5SegIter *pIter, /* Iterator to advance */
211648 int *pbUnused /* Unused */
211649){
211650 assert( pIter->flags & FTS5_SEGITER_REVERSE )((void) (0));
211651 assert( pIter->pNextLeaf==0 )((void) (0));
211652 UNUSED_PARAM(pbUnused);
211653
211654 if( pIter->iRowidOffset>0 ){
211655 u8 *a = pIter->pLeaf->p;
211656 int iOff;
211657 i64 iDelta;
211658
211659 pIter->iRowidOffset--;
211660 pIter->iLeafOffset = pIter->aRowidOffset[pIter->iRowidOffset];
211661 fts5SegIterLoadNPos(p, pIter);
211662 iOff = pIter->iLeafOffset;
211663 if( p->pConfig->eDetail!=FTS5_DETAIL_NONE ){
211664 iOff += pIter->nPos;
211665 }
211666 fts5GetVarint(&a[iOff], (u64*)&iDelta);
211667 pIter->iRowid -= iDelta;
211668 }else{
211669 fts5SegIterReverseNewPage(p, pIter);
211670 }
211671}
211672
211673/*
211674** Advance iterator pIter to the next entry.
211675**
211676** This version of fts5SegIterNext() is only used if detail=none and the
211677** iterator is not a reverse direction iterator.
211678*/
211679static void fts5SegIterNext_None(
211680 Fts5Index *p, /* FTS5 backend object */
211681 Fts5SegIter *pIter, /* Iterator to advance */
211682 int *pbNewTerm /* OUT: Set for new term */
211683){
211684 int iOff;
211685
211686 assert( p->rc==SQLITE_OK )((void) (0));
211687 assert( (pIter->flags & FTS5_SEGITER_REVERSE)==0 )((void) (0));
211688 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
211689
211690 ASSERT_SZLEAF_OK(pIter->pLeaf);
211691 iOff = pIter->iLeafOffset;
211692
211693 /* Next entry is on the next page */
211694 if( pIter->pSeg && iOff>=pIter->pLeaf->szLeaf ){
211695 fts5SegIterNextPage(p, pIter);
211696 if( p->rc || pIter->pLeaf==0 ) return;
211697 pIter->iRowid = 0;
211698 iOff = 4;
211699 }
211700
211701 if( iOff<pIter->iEndofDoclist ){
211702 /* Next entry is on the current page */
211703 i64 iDelta;
211704 iOff += sqlite3Fts5GetVarint(&pIter->pLeaf->p[iOff], (u64*)&iDelta);
211705 pIter->iLeafOffset = iOff;
211706 pIter->iRowid += iDelta;
211707 }else if( (pIter->flags & FTS5_SEGITER_ONETERM)==0 ){
211708 if( pIter->pSeg ){
211709 int nKeep = 0;
211710 if( iOff!=fts5LeafFirstTermOff(pIter->pLeaf) ){
211711 iOff += fts5GetVarint32(&pIter->pLeaf->p[iOff], nKeep);
211712 }
211713 pIter->iLeafOffset = iOff;
211714 fts5SegIterLoadTerm(p, pIter, nKeep);
211715 }else{
211716 const u8 *pList = 0;
211717 const char *zTerm = 0;
211718 int nList;
211719 sqlite3Fts5HashScanNext(p->pHash);
211720 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
211721 if( pList==0 ) goto next_none_eof;
211722 pIter->pLeaf->p = (u8*)pList;
211723 pIter->pLeaf->nn = nList;
211724 pIter->pLeaf->szLeaf = nList;
211725 pIter->iEndofDoclist = nList;
211726 sqlite3Fts5BufferSet(&p->rc,&pIter->term, (int)strlen(zTerm), (u8*)zTerm);
211727 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
211728 }
211729
211730 if( pbNewTerm ) *pbNewTerm = 1;
211731 }else{
211732 goto next_none_eof;
211733 }
211734
211735 fts5SegIterLoadNPos(p, pIter);
211736
211737 return;
211738 next_none_eof:
211739 fts5DataRelease(pIter->pLeaf);
211740 pIter->pLeaf = 0;
211741}
211742
211743
211744/*
211745** Advance iterator pIter to the next entry.
211746**
211747** If an error occurs, Fts5Index.rc is set to an appropriate error code. It
211748** is not considered an error if the iterator reaches EOF. If an error has
211749** already occurred when this function is called, it is a no-op.
211750*/
211751static void fts5SegIterNext(
211752 Fts5Index *p, /* FTS5 backend object */
211753 Fts5SegIter *pIter, /* Iterator to advance */
211754 int *pbNewTerm /* OUT: Set for new term */
211755){
211756 Fts5Data *pLeaf = pIter->pLeaf;
211757 int iOff;
211758 int bNewTerm = 0;
211759 int nKeep = 0;
211760 u8 *a;
211761 int n;
211762
211763 assert( pbNewTerm==0 || *pbNewTerm==0 )((void) (0));
211764 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
211765
211766 /* Search for the end of the position list within the current page. */
211767 a = pLeaf->p;
211768 n = pLeaf->szLeaf;
211769
211770 ASSERT_SZLEAF_OK(pLeaf);
211771 iOff = pIter->iLeafOffset + pIter->nPos;
211772
211773 if( iOff<n ){
211774 /* The next entry is on the current page. */
211775 assert_nc( iOff<=pIter->iEndofDoclist );
211776 if( iOff>=pIter->iEndofDoclist ){
211777 bNewTerm = 1;
211778 if( iOff!=fts5LeafFirstTermOff(pLeaf) ){
211779 iOff += fts5GetVarint32(&a[iOff], nKeep);
211780 }
211781 }else{
211782 u64 iDelta;
211783 iOff += sqlite3Fts5GetVarint(&a[iOff], &iDelta);
211784 pIter->iRowid += iDelta;
211785 assert_nc( iDelta>0 );
211786 }
211787 pIter->iLeafOffset = iOff;
211788
211789 }else if( pIter->pSeg==0 ){
211790 const u8 *pList = 0;
211791 const char *zTerm = 0;
211792 int nList = 0;
211793 assert( (pIter->flags & FTS5_SEGITER_ONETERM) || pbNewTerm )((void) (0));
211794 if( 0==(pIter->flags & FTS5_SEGITER_ONETERM) ){
211795 sqlite3Fts5HashScanNext(p->pHash);
211796 sqlite3Fts5HashScanEntry(p->pHash, &zTerm, &pList, &nList);
211797 }
211798 if( pList==0 ){
211799 fts5DataRelease(pIter->pLeaf);
211800 pIter->pLeaf = 0;
211801 }else{
211802 pIter->pLeaf->p = (u8*)pList;
211803 pIter->pLeaf->nn = nList;
211804 pIter->pLeaf->szLeaf = nList;
211805 pIter->iEndofDoclist = nList+1;
211806 sqlite3Fts5BufferSet(&p->rc, &pIter->term, (int)strlen(zTerm),
211807 (u8*)zTerm);
211808 pIter->iLeafOffset = fts5GetVarint(pList, (u64*)&pIter->iRowid);
211809 *pbNewTerm = 1;
211810 }
211811 }else{
211812 iOff = 0;
211813 /* Next entry is not on the current page */
211814 while( iOff==0 ){
211815 fts5SegIterNextPage(p, pIter);
211816 pLeaf = pIter->pLeaf;
211817 if( pLeaf==0 ) break;
211818 ASSERT_SZLEAF_OK(pLeaf);
211819 if( (iOff = fts5LeafFirstRowidOff(pLeaf)) && iOff<pLeaf->szLeaf ){
211820 iOff += sqlite3Fts5GetVarint(&pLeaf->p[iOff], (u64*)&pIter->iRowid);
211821 pIter->iLeafOffset = iOff;
211822
211823 if( pLeaf->nn>pLeaf->szLeaf ){
211824 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
211825 &pLeaf->p[pLeaf->szLeaf], pIter->iEndofDoclist
211826 );
211827 }
211828 }
211829 else if( pLeaf->nn>pLeaf->szLeaf ){
211830 pIter->iPgidxOff = pLeaf->szLeaf + fts5GetVarint32(
211831 &pLeaf->p[pLeaf->szLeaf], iOff
211832 );
211833 pIter->iLeafOffset = iOff;
211834 pIter->iEndofDoclist = iOff;
211835 bNewTerm = 1;
211836 }
211837 assert_nc( iOff<pLeaf->szLeaf );
211838 if( iOff>pLeaf->szLeaf ){
211839 p->rc = FTS5_CORRUPT;
211840 return;
211841 }
211842 }
211843 }
211844
211845 /* Check if the iterator is now at EOF. If so, return early. */
211846 if( pIter->pLeaf ){
211847 if( bNewTerm ){
211848 if( pIter->flags & FTS5_SEGITER_ONETERM ){
211849 fts5DataRelease(pIter->pLeaf);
211850 pIter->pLeaf = 0;
211851 }else{
211852 fts5SegIterLoadTerm(p, pIter, nKeep);
211853 fts5SegIterLoadNPos(p, pIter);
211854 if( pbNewTerm ) *pbNewTerm = 1;
211855 }
211856 }else{
211857 /* The following could be done by calling fts5SegIterLoadNPos(). But
211858 ** this block is particularly performance critical, so equivalent
211859 ** code is inlined.
211860 **
211861 ** Later: Switched back to fts5SegIterLoadNPos() because it supports
211862 ** detail=none mode. Not ideal.
211863 */
211864 int nSz;
211865 assert( p->rc==SQLITE_OK )((void) (0));
211866 assert( pIter->iLeafOffset<=pIter->pLeaf->nn )((void) (0));
211867 fts5FastGetVarint32(pIter->pLeaf->p, pIter->iLeafOffset, nSz);
211868 pIter->bDel = (nSz & 0x0001);
211869 pIter->nPos = nSz>>1;
211870 assert_nc( pIter->nPos>=0 );
211871 }
211872 }
211873}
211874
211875#define SWAPVAL(T, a, b) { T tmp; tmp=a; a=b; b=tmp; }
211876
211877#define fts5IndexSkipVarint(a, iOff) { \
211878 int iEnd = iOff+9; \
211879 while( (a[iOff++] & 0x80) && iOff<iEnd ); \
211880}
211881
211882/*
211883** Iterator pIter currently points to the first rowid in a doclist. This
211884** function sets the iterator up so that iterates in reverse order through
211885** the doclist.
211886*/
211887static void fts5SegIterReverse(Fts5Index *p, Fts5SegIter *pIter){
211888 Fts5DlidxIter *pDlidx = pIter->pDlidx;
211889 Fts5Data *pLast = 0;
211890 int pgnoLast = 0;
211891
211892 if( pDlidx ){
211893 int iSegid = pIter->pSeg->iSegid;
211894 pgnoLast = fts5DlidxIterPgno(pDlidx);
211895 pLast = fts5DataRead(p, FTS5_SEGMENT_ROWID(iSegid, pgnoLast));
211896 }else{
211897 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
211898
211899 /* Currently, Fts5SegIter.iLeafOffset points to the first byte of
211900 ** position-list content for the current rowid. Back it up so that it
211901 ** points to the start of the position-list size field. */
211902 int iPoslist;
211903 if( pIter->iTermLeafPgno==pIter->iLeafPgno ){
211904 iPoslist = pIter->iTermLeafOffset;
211905 }else{
211906 iPoslist = 4;
211907 }
211908 fts5IndexSkipVarint(pLeaf->p, iPoslist);
211909 pIter->iLeafOffset = iPoslist;
211910
211911 /* If this condition is true then the largest rowid for the current
211912 ** term may not be stored on the current page. So search forward to
211913 ** see where said rowid really is. */
211914 if( pIter->iEndofDoclist>=pLeaf->szLeaf ){
211915 int pgno;
211916 Fts5StructureSegment *pSeg = pIter->pSeg;
211917
211918 /* The last rowid in the doclist may not be on the current page. Search
211919 ** forward to find the page containing the last rowid. */
211920 for(pgno=pIter->iLeafPgno+1; !p->rc && pgno<=pSeg->pgnoLast; pgno++){
211921 i64 iAbs = FTS5_SEGMENT_ROWID(pSeg->iSegid, pgno);
211922 Fts5Data *pNew = fts5DataRead(p, iAbs);
211923 if( pNew ){
211924 int iRowid, bTermless;
211925 iRowid = fts5LeafFirstRowidOff(pNew);
211926 bTermless = fts5LeafIsTermless(pNew);
211927 if( iRowid ){
211928 SWAPVAL(Fts5Data*, pNew, pLast);
211929 pgnoLast = pgno;
211930 }
211931 fts5DataRelease(pNew);
211932 if( bTermless==0 ) break;
211933 }
211934 }
211935 }
211936 }
211937
211938 /* If pLast is NULL at this point, then the last rowid for this doclist
211939 ** lies on the page currently indicated by the iterator. In this case
211940 ** pIter->iLeafOffset is already set to point to the position-list size
211941 ** field associated with the first relevant rowid on the page.
211942 **
211943 ** Or, if pLast is non-NULL, then it is the page that contains the last
211944 ** rowid. In this case configure the iterator so that it points to the
211945 ** first rowid on this page.
211946 */
211947 if( pLast ){
211948 int iOff;
211949 fts5DataRelease(pIter->pLeaf);
211950 pIter->pLeaf = pLast;
211951 pIter->iLeafPgno = pgnoLast;
211952 iOff = fts5LeafFirstRowidOff(pLast);
211953 iOff += fts5GetVarint(&pLast->p[iOff], (u64*)&pIter->iRowid);
211954 pIter->iLeafOffset = iOff;
211955
211956 if( fts5LeafIsTermless(pLast) ){
211957 pIter->iEndofDoclist = pLast->nn+1;
211958 }else{
211959 pIter->iEndofDoclist = fts5LeafFirstTermOff(pLast);
211960 }
211961
211962 }
211963
211964 fts5SegIterReverseInitPage(p, pIter);
211965}
211966
211967/*
211968** Iterator pIter currently points to the first rowid of a doclist.
211969** There is a doclist-index associated with the final term on the current
211970** page. If the current term is the last term on the page, load the
211971** doclist-index from disk and initialize an iterator at (pIter->pDlidx).
211972*/
211973static void fts5SegIterLoadDlidx(Fts5Index *p, Fts5SegIter *pIter){
211974 int iSeg = pIter->pSeg->iSegid;
211975 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
211976 Fts5Data *pLeaf = pIter->pLeaf; /* Current leaf data */
211977
211978 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
211979 assert( pIter->pDlidx==0 )((void) (0));
211980
211981 /* Check if the current doclist ends on this page. If it does, return
211982 ** early without loading the doclist-index (as it belongs to a different
211983 ** term. */
211984 if( pIter->iTermLeafPgno==pIter->iLeafPgno
211985 && pIter->iEndofDoclist<pLeaf->szLeaf
211986 ){
211987 return;
211988 }
211989
211990 pIter->pDlidx = fts5DlidxIterInit(p, bRev, iSeg, pIter->iTermLeafPgno);
211991}
211992
211993/*
211994** The iterator object passed as the second argument currently contains
211995** no valid values except for the Fts5SegIter.pLeaf member variable. This
211996** function searches the leaf page for a term matching (pTerm/nTerm).
211997**
211998** If the specified term is found on the page, then the iterator is left
211999** pointing to it. If argument bGe is zero and the term is not found,
212000** the iterator is left pointing at EOF.
212001**
212002** If bGe is non-zero and the specified term is not found, then the
212003** iterator is left pointing to the smallest term in the segment that
212004** is larger than the specified term, even if this term is not on the
212005** current page.
212006*/
212007static void fts5LeafSeek(
212008 Fts5Index *p, /* Leave any error code here */
212009 int bGe, /* True for a >= search */
212010 Fts5SegIter *pIter, /* Iterator to seek */
212011 const u8 *pTerm, int nTerm /* Term to search for */
212012){
212013 int iOff;
212014 const u8 *a = pIter->pLeaf->p;
212015 int szLeaf = pIter->pLeaf->szLeaf;
212016 int n = pIter->pLeaf->nn;
212017
212018 u32 nMatch = 0;
212019 u32 nKeep = 0;
212020 u32 nNew = 0;
212021 u32 iTermOff;
212022 int iPgidx; /* Current offset in pgidx */
212023 int bEndOfPage = 0;
212024
212025 assert( p->rc==SQLITE_OK )((void) (0));
212026
212027 iPgidx = szLeaf;
212028 iPgidx += fts5GetVarint32(&a[iPgidx], iTermOff);
212029 iOff = iTermOff;
212030 if( iOff>n ){
212031 p->rc = FTS5_CORRUPT;
212032 return;
212033 }
212034
212035 while( 1 ){
212036
212037 /* Figure out how many new bytes are in this term */
212038 fts5FastGetVarint32(a, iOff, nNew);
212039 if( nKeep<nMatch ){
212040 goto search_failed;
212041 }
212042
212043 assert( nKeep>=nMatch )((void) (0));
212044 if( nKeep==nMatch ){
212045 u32 nCmp;
212046 u32 i;
212047 nCmp = (u32)MIN(nNew, nTerm-nMatch)((nNew)<(nTerm-nMatch)?(nNew):(nTerm-nMatch));
212048 for(i=0; i<nCmp; i++){
212049 if( a[iOff+i]!=pTerm[nMatch+i] ) break;
212050 }
212051 nMatch += i;
212052
212053 if( (u32)nTerm==nMatch ){
212054 if( i==nNew ){
212055 goto search_success;
212056 }else{
212057 goto search_failed;
212058 }
212059 }else if( i<nNew && a[iOff+i]>pTerm[nMatch] ){
212060 goto search_failed;
212061 }
212062 }
212063
212064 if( iPgidx>=n ){
212065 bEndOfPage = 1;
212066 break;
212067 }
212068
212069 iPgidx += fts5GetVarint32(&a[iPgidx], nKeep);
212070 iTermOff += nKeep;
212071 iOff = iTermOff;
212072
212073 if( iOff>=n ){
212074 p->rc = FTS5_CORRUPT;
212075 return;
212076 }
212077
212078 /* Read the nKeep field of the next term. */
212079 fts5FastGetVarint32(a, iOff, nKeep);
212080 }
212081
212082 search_failed:
212083 if( bGe==0 ){
212084 fts5DataRelease(pIter->pLeaf);
212085 pIter->pLeaf = 0;
212086 return;
212087 }else if( bEndOfPage ){
212088 do {
212089 fts5SegIterNextPage(p, pIter);
212090 if( pIter->pLeaf==0 ) return;
212091 a = pIter->pLeaf->p;
212092 if( fts5LeafIsTermless(pIter->pLeaf)==0 ){
212093 iPgidx = pIter->pLeaf->szLeaf;
212094 iPgidx += fts5GetVarint32(&pIter->pLeaf->p[iPgidx], iOff);
212095 if( iOff<4 || iOff>=pIter->pLeaf->szLeaf ){
212096 p->rc = FTS5_CORRUPT;
212097 return;
212098 }else{
212099 nKeep = 0;
212100 iTermOff = iOff;
212101 n = pIter->pLeaf->nn;
212102 iOff += fts5GetVarint32(&a[iOff], nNew);
212103 break;
212104 }
212105 }
212106 }while( 1 );
212107 }
212108
212109 search_success:
212110 pIter->iLeafOffset = iOff + nNew;
212111 if( pIter->iLeafOffset>n || nNew<1 ){
212112 p->rc = FTS5_CORRUPT;
212113 return;
212114 }
212115 pIter->iTermLeafOffset = pIter->iLeafOffset;
212116 pIter->iTermLeafPgno = pIter->iLeafPgno;
212117
212118 fts5BufferSet(&p->rc, &pIter->term, nKeep, pTerm);
212119 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
212120
212121 if( iPgidx>=n ){
212122 pIter->iEndofDoclist = pIter->pLeaf->nn+1;
212123 }else{
212124 int nExtra;
212125 iPgidx += fts5GetVarint32(&a[iPgidx], nExtra);
212126 pIter->iEndofDoclist = iTermOff + nExtra;
212127 }
212128 pIter->iPgidxOff = iPgidx;
212129
212130 fts5SegIterLoadRowid(p, pIter);
212131 fts5SegIterLoadNPos(p, pIter);
212132}
212133
212134static sqlite3_stmt *fts5IdxSelectStmt(Fts5Index *p){
212135 if( p->pIdxSelect==0 ){
212136 Fts5Config *pConfig = p->pConfig;
212137 fts5IndexPrepareStmt(p, &p->pIdxSelect, sqlite3_mprintf(
212138 "SELECT pgno FROM '%q'.'%q_idx' WHERE "
212139 "segid=? AND term<=? ORDER BY term DESC LIMIT 1",
212140 pConfig->zDb, pConfig->zName
212141 ));
212142 }
212143 return p->pIdxSelect;
212144}
212145
212146/*
212147** Initialize the object pIter to point to term pTerm/nTerm within segment
212148** pSeg. If there is no such term in the index, the iterator is set to EOF.
212149**
212150** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
212151** an error has already occurred when this function is called, it is a no-op.
212152*/
212153static void fts5SegIterSeekInit(
212154 Fts5Index *p, /* FTS5 backend */
212155 const u8 *pTerm, int nTerm, /* Term to seek to */
212156 int flags, /* Mask of FTS5INDEX_XXX flags */
212157 Fts5StructureSegment *pSeg, /* Description of segment */
212158 Fts5SegIter *pIter /* Object to populate */
212159){
212160 int iPg = 1;
212161 int bGe = (flags & FTS5INDEX_QUERY_SCAN);
212162 int bDlidx = 0; /* True if there is a doclist-index */
212163 sqlite3_stmt *pIdxSelect = 0;
212164
212165 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 )((void) (0));
212166 assert( pTerm && nTerm )((void) (0));
212167 memset(pIter, 0, sizeof(*pIter));
212168 pIter->pSeg = pSeg;
212169
212170 /* This block sets stack variable iPg to the leaf page number that may
212171 ** contain term (pTerm/nTerm), if it is present in the segment. */
212172 pIdxSelect = fts5IdxSelectStmt(p);
212173 if( p->rc ) return;
212174 sqlite3_bind_int(pIdxSelect, 1, pSeg->iSegid);
212175 sqlite3_bind_blob(pIdxSelect, 2, pTerm, nTerm, SQLITE_STATIC((sqlite3_destructor_type)0));
212176 if( SQLITE_ROW100==sqlite3_step(pIdxSelect) ){
212177 i64 val = sqlite3_column_int(pIdxSelect, 0);
212178 iPg = (int)(val>>1);
212179 bDlidx = (val & 0x0001);
212180 }
212181 p->rc = sqlite3_reset(pIdxSelect);
212182 sqlite3_bind_null(pIdxSelect, 2);
212183
212184 if( iPg<pSeg->pgnoFirst ){
212185 iPg = pSeg->pgnoFirst;
212186 bDlidx = 0;
212187 }
212188
212189 pIter->iLeafPgno = iPg - 1;
212190 fts5SegIterNextPage(p, pIter);
212191
212192 if( pIter->pLeaf ){
212193 fts5LeafSeek(p, bGe, pIter, pTerm, nTerm);
212194 }
212195
212196 if( p->rc==SQLITE_OK0 && bGe==0 ){
212197 pIter->flags |= FTS5_SEGITER_ONETERM;
212198 if( pIter->pLeaf ){
212199 if( flags & FTS5INDEX_QUERY_DESC ){
212200 pIter->flags |= FTS5_SEGITER_REVERSE;
212201 }
212202 if( bDlidx ){
212203 fts5SegIterLoadDlidx(p, pIter);
212204 }
212205 if( flags & FTS5INDEX_QUERY_DESC ){
212206 fts5SegIterReverse(p, pIter);
212207 }
212208 }
212209 }
212210
212211 fts5SegIterSetNext(p, pIter);
212212
212213 /* Either:
212214 **
212215 ** 1) an error has occurred, or
212216 ** 2) the iterator points to EOF, or
212217 ** 3) the iterator points to an entry with term (pTerm/nTerm), or
212218 ** 4) the FTS5INDEX_QUERY_SCAN flag was set and the iterator points
212219 ** to an entry with a term greater than or equal to (pTerm/nTerm).
212220 */
212221 assert_nc( p->rc!=SQLITE_OK0 /* 1 */
212222 || pIter->pLeaf==0 /* 2 */
212223 || fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)==0 /* 3 */
212224 || (bGe && fts5BufferCompareBlob(&pIter->term, pTerm, nTerm)>0) /* 4 */
212225 );
212226}
212227
212228/*
212229** Initialize the object pIter to point to term pTerm/nTerm within the
212230** in-memory hash table. If there is no such term in the hash-table, the
212231** iterator is set to EOF.
212232**
212233** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
212234** an error has already occurred when this function is called, it is a no-op.
212235*/
212236static void fts5SegIterHashInit(
212237 Fts5Index *p, /* FTS5 backend */
212238 const u8 *pTerm, int nTerm, /* Term to seek to */
212239 int flags, /* Mask of FTS5INDEX_XXX flags */
212240 Fts5SegIter *pIter /* Object to populate */
212241){
212242 int nList = 0;
212243 const u8 *z = 0;
212244 int n = 0;
212245 Fts5Data *pLeaf = 0;
212246
212247 assert( p->pHash )((void) (0));
212248 assert( p->rc==SQLITE_OK )((void) (0));
212249
212250 if( pTerm==0 || (flags & FTS5INDEX_QUERY_SCAN) ){
212251 const u8 *pList = 0;
212252
212253 p->rc = sqlite3Fts5HashScanInit(p->pHash, (const char*)pTerm, nTerm);
212254 sqlite3Fts5HashScanEntry(p->pHash, (const char**)&z, &pList, &nList);
212255 n = (z ? (int)strlen((const char*)z) : 0);
212256 if( pList ){
212257 pLeaf = fts5IdxMalloc(p, sizeof(Fts5Data));
212258 if( pLeaf ){
212259 pLeaf->p = (u8*)pList;
212260 }
212261 }
212262 }else{
212263 p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
212264 (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
212265 );
212266 if( pLeaf ){
212267 pLeaf->p = (u8*)&pLeaf[1];
212268 }
212269 z = pTerm;
212270 n = nTerm;
212271 pIter->flags |= FTS5_SEGITER_ONETERM;
212272 }
212273
212274 if( pLeaf ){
212275 sqlite3Fts5BufferSet(&p->rc, &pIter->term, n, z);
212276 pLeaf->nn = pLeaf->szLeaf = nList;
212277 pIter->pLeaf = pLeaf;
212278 pIter->iLeafOffset = fts5GetVarint(pLeaf->p, (u64*)&pIter->iRowid);
212279 pIter->iEndofDoclist = pLeaf->nn;
212280
212281 if( flags & FTS5INDEX_QUERY_DESC ){
212282 pIter->flags |= FTS5_SEGITER_REVERSE;
212283 fts5SegIterReverseInitPage(p, pIter);
212284 }else{
212285 fts5SegIterLoadNPos(p, pIter);
212286 }
212287 }
212288
212289 fts5SegIterSetNext(p, pIter);
212290}
212291
212292/*
212293** Zero the iterator passed as the only argument.
212294*/
212295static void fts5SegIterClear(Fts5SegIter *pIter){
212296 fts5BufferFree(&pIter->term);
212297 fts5DataRelease(pIter->pLeaf);
212298 fts5DataRelease(pIter->pNextLeaf);
212299 fts5DlidxIterFree(pIter->pDlidx);
212300 sqlite3_free(pIter->aRowidOffset);
212301 memset(pIter, 0, sizeof(Fts5SegIter));
212302}
212303
212304#ifdef SQLITE_DEBUG
212305
212306/*
212307** This function is used as part of the big assert() procedure implemented by
212308** fts5AssertMultiIterSetup(). It ensures that the result currently stored
212309** in *pRes is the correct result of comparing the current positions of the
212310** two iterators.
212311*/
212312static void fts5AssertComparisonResult(
212313 Fts5Iter *pIter,
212314 Fts5SegIter *p1,
212315 Fts5SegIter *p2,
212316 Fts5CResult *pRes
212317){
212318 int i1 = p1 - pIter->aSeg;
212319 int i2 = p2 - pIter->aSeg;
212320
212321 if( p1->pLeaf || p2->pLeaf ){
212322 if( p1->pLeaf==0 ){
212323 assert( pRes->iFirst==i2 )((void) (0));
212324 }else if( p2->pLeaf==0 ){
212325 assert( pRes->iFirst==i1 )((void) (0));
212326 }else{
212327 int nMin = MIN(p1->term.n, p2->term.n)((p1->term.n)<(p2->term.n)?(p1->term.n):(p2->term
.n))
;
212328 int res = fts5Memcmp(p1->term.p, p2->term.p, nMin);
212329 if( res==0 ) res = p1->term.n - p2->term.n;
212330
212331 if( res==0 ){
212332 assert( pRes->bTermEq==1 )((void) (0));
212333 assert( p1->iRowid!=p2->iRowid )((void) (0));
212334 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : 1;
212335 }else{
212336 assert( pRes->bTermEq==0 )((void) (0));
212337 }
212338
212339 if( res<0 ){
212340 assert( pRes->iFirst==i1 )((void) (0));
212341 }else{
212342 assert( pRes->iFirst==i2 )((void) (0));
212343 }
212344 }
212345 }
212346}
212347
212348/*
212349** This function is a no-op unless SQLITE_DEBUG is defined when this module
212350** is compiled. In that case, this function is essentially an assert()
212351** statement used to verify that the contents of the pIter->aFirst[] array
212352** are correct.
212353*/
212354static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
212355 if( p->rc==SQLITE_OK0 ){
212356 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
212357 int i;
212358
212359 assert( (pFirst->pLeaf==0)==pIter->base.bEof )((void) (0));
212360
212361 /* Check that pIter->iSwitchRowid is set correctly. */
212362 for(i=0; i<pIter->nSeg; i++){
212363 Fts5SegIter *p1 = &pIter->aSeg[i];
212364 assert( p1==pFirst((void) (0))
212365 || p1->pLeaf==0((void) (0))
212366 || fts5BufferCompare(&pFirst->term, &p1->term)((void) (0))
212367 || p1->iRowid==pIter->iSwitchRowid((void) (0))
212368 || (p1->iRowid<pIter->iSwitchRowid)==pIter->bRev((void) (0))
212369 )((void) (0));
212370 }
212371
212372 for(i=0; i<pIter->nSeg; i+=2){
212373 Fts5SegIter *p1 = &pIter->aSeg[i];
212374 Fts5SegIter *p2 = &pIter->aSeg[i+1];
212375 Fts5CResult *pRes = &pIter->aFirst[(pIter->nSeg + i) / 2];
212376 fts5AssertComparisonResult(pIter, p1, p2, pRes);
212377 }
212378
212379 for(i=1; i<(pIter->nSeg / 2); i+=2){
212380 Fts5SegIter *p1 = &pIter->aSeg[ pIter->aFirst[i*2].iFirst ];
212381 Fts5SegIter *p2 = &pIter->aSeg[ pIter->aFirst[i*2+1].iFirst ];
212382 Fts5CResult *pRes = &pIter->aFirst[i];
212383 fts5AssertComparisonResult(pIter, p1, p2, pRes);
212384 }
212385 }
212386}
212387#else
212388# define fts5AssertMultiIterSetup(x,y)
212389#endif
212390
212391/*
212392** Do the comparison necessary to populate pIter->aFirst[iOut].
212393**
212394** If the returned value is non-zero, then it is the index of an entry
212395** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
212396** to a key that is a duplicate of another, higher priority,
212397** segment-iterator in the pSeg->aSeg[] array.
212398*/
212399static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
212400 int i1; /* Index of left-hand Fts5SegIter */
212401 int i2; /* Index of right-hand Fts5SegIter */
212402 int iRes;
212403 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
212404 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
212405 Fts5CResult *pRes = &pIter->aFirst[iOut];
212406
212407 assert( iOut<pIter->nSeg && iOut>0 )((void) (0));
212408 assert( pIter->bRev==0 || pIter->bRev==1 )((void) (0));
212409
212410 if( iOut>=(pIter->nSeg/2) ){
212411 i1 = (iOut - pIter->nSeg/2) * 2;
212412 i2 = i1 + 1;
212413 }else{
212414 i1 = pIter->aFirst[iOut*2].iFirst;
212415 i2 = pIter->aFirst[iOut*2+1].iFirst;
212416 }
212417 p1 = &pIter->aSeg[i1];
212418 p2 = &pIter->aSeg[i2];
212419
212420 pRes->bTermEq = 0;
212421 if( p1->pLeaf==0 ){ /* If p1 is at EOF */
212422 iRes = i2;
212423 }else if( p2->pLeaf==0 ){ /* If p2 is at EOF */
212424 iRes = i1;
212425 }else{
212426 int res = fts5BufferCompare(&p1->term, &p2->term);
212427 if( res==0 ){
212428 assert_nc( i2>i1 );
212429 assert_nc( i2!=0 );
212430 pRes->bTermEq = 1;
212431 if( p1->iRowid==p2->iRowid ){
212432 p1->bDel = p2->bDel;
212433 return i2;
212434 }
212435 res = ((p1->iRowid > p2->iRowid)==pIter->bRev) ? -1 : +1;
212436 }
212437 assert( res!=0 )((void) (0));
212438 if( res<0 ){
212439 iRes = i1;
212440 }else{
212441 iRes = i2;
212442 }
212443 }
212444
212445 pRes->iFirst = (u16)iRes;
212446 return 0;
212447}
212448
212449/*
212450** Move the seg-iter so that it points to the first rowid on page iLeafPgno.
212451** It is an error if leaf iLeafPgno does not exist or contains no rowids.
212452*/
212453static void fts5SegIterGotoPage(
212454 Fts5Index *p, /* FTS5 backend object */
212455 Fts5SegIter *pIter, /* Iterator to advance */
212456 int iLeafPgno
212457){
212458 assert( iLeafPgno>pIter->iLeafPgno )((void) (0));
212459
212460 if( iLeafPgno>pIter->pSeg->pgnoLast ){
212461 p->rc = FTS5_CORRUPT;
212462 }else{
212463 fts5DataRelease(pIter->pNextLeaf);
212464 pIter->pNextLeaf = 0;
212465 pIter->iLeafPgno = iLeafPgno-1;
212466 fts5SegIterNextPage(p, pIter);
212467 assert( p->rc!=SQLITE_OK || pIter->iLeafPgno==iLeafPgno )((void) (0));
212468
212469 if( p->rc==SQLITE_OK0 ){
212470 int iOff;
212471 u8 *a = pIter->pLeaf->p;
212472 int n = pIter->pLeaf->szLeaf;
212473
212474 iOff = fts5LeafFirstRowidOff(pIter->pLeaf);
212475 if( iOff<4 || iOff>=n ){
212476 p->rc = FTS5_CORRUPT;
212477 }else{
212478 iOff += fts5GetVarint(&a[iOff], (u64*)&pIter->iRowid);
212479 pIter->iLeafOffset = iOff;
212480 fts5SegIterLoadNPos(p, pIter);
212481 }
212482 }
212483 }
212484}
212485
212486/*
212487** Advance the iterator passed as the second argument until it is at or
212488** past rowid iFrom. Regardless of the value of iFrom, the iterator is
212489** always advanced at least once.
212490*/
212491static void fts5SegIterNextFrom(
212492 Fts5Index *p, /* FTS5 backend object */
212493 Fts5SegIter *pIter, /* Iterator to advance */
212494 i64 iMatch /* Advance iterator at least this far */
212495){
212496 int bRev = (pIter->flags & FTS5_SEGITER_REVERSE);
212497 Fts5DlidxIter *pDlidx = pIter->pDlidx;
212498 int iLeafPgno = pIter->iLeafPgno;
212499 int bMove = 1;
212500
212501 assert( pIter->flags & FTS5_SEGITER_ONETERM )((void) (0));
212502 assert( pIter->pDlidx )((void) (0));
212503 assert( pIter->pLeaf )((void) (0));
212504
212505 if( bRev==0 ){
212506 while( !fts5DlidxIterEof(p, pDlidx) && iMatch>fts5DlidxIterRowid(pDlidx) ){
212507 iLeafPgno = fts5DlidxIterPgno(pDlidx);
212508 fts5DlidxIterNext(p, pDlidx);
212509 }
212510 assert_nc( iLeafPgno>=pIter->iLeafPgno || p->rc );
212511 if( iLeafPgno>pIter->iLeafPgno ){
212512 fts5SegIterGotoPage(p, pIter, iLeafPgno);
212513 bMove = 0;
212514 }
212515 }else{
212516 assert( pIter->pNextLeaf==0 )((void) (0));
212517 assert( iMatch<pIter->iRowid )((void) (0));
212518 while( !fts5DlidxIterEof(p, pDlidx) && iMatch<fts5DlidxIterRowid(pDlidx) ){
212519 fts5DlidxIterPrev(p, pDlidx);
212520 }
212521 iLeafPgno = fts5DlidxIterPgno(pDlidx);
212522
212523 assert( fts5DlidxIterEof(p, pDlidx) || iLeafPgno<=pIter->iLeafPgno )((void) (0));
212524
212525 if( iLeafPgno<pIter->iLeafPgno ){
212526 pIter->iLeafPgno = iLeafPgno+1;
212527 fts5SegIterReverseNewPage(p, pIter);
212528 bMove = 0;
212529 }
212530 }
212531
212532 do{
212533 if( bMove && p->rc==SQLITE_OK0 ) pIter->xNext(p, pIter, 0);
212534 if( pIter->pLeaf==0 ) break;
212535 if( bRev==0 && pIter->iRowid>=iMatch ) break;
212536 if( bRev!=0 && pIter->iRowid<=iMatch ) break;
212537 bMove = 1;
212538 }while( p->rc==SQLITE_OK0 );
212539}
212540
212541
212542/*
212543** Free the iterator object passed as the second argument.
212544*/
212545static void fts5MultiIterFree(Fts5Iter *pIter){
212546 if( pIter ){
212547 int i;
212548 for(i=0; i<pIter->nSeg; i++){
212549 fts5SegIterClear(&pIter->aSeg[i]);
212550 }
212551 fts5BufferFree(&pIter->poslist);
212552 sqlite3_free(pIter);
212553 }
212554}
212555
212556static void fts5MultiIterAdvanced(
212557 Fts5Index *p, /* FTS5 backend to iterate within */
212558 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
212559 int iChanged, /* Index of sub-iterator just advanced */
212560 int iMinset /* Minimum entry in aFirst[] to set */
212561){
212562 int i;
212563 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK0; i=i/2){
212564 int iEq;
212565 if( (iEq = fts5MultiIterDoCompare(pIter, i)) ){
212566 Fts5SegIter *pSeg = &pIter->aSeg[iEq];
212567 assert( p->rc==SQLITE_OK )((void) (0));
212568 pSeg->xNext(p, pSeg, 0);
212569 i = pIter->nSeg + iEq;
212570 }
212571 }
212572}
212573
212574/*
212575** Sub-iterator iChanged of iterator pIter has just been advanced. It still
212576** points to the same term though - just a different rowid. This function
212577** attempts to update the contents of the pIter->aFirst[] accordingly.
212578** If it does so successfully, 0 is returned. Otherwise 1.
212579**
212580** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
212581** on the iterator instead. That function does the same as this one, except
212582** that it deals with more complicated cases as well.
212583*/
212584static int fts5MultiIterAdvanceRowid(
212585 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
212586 int iChanged, /* Index of sub-iterator just advanced */
212587 Fts5SegIter **ppFirst
212588){
212589 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
212590
212591 if( pNew->iRowid==pIter->iSwitchRowid
212592 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
212593 ){
212594 int i;
212595 Fts5SegIter *pOther = &pIter->aSeg[iChanged ^ 0x0001];
212596 pIter->iSwitchRowid = pIter->bRev ? SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))) : LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
212597 for(i=(pIter->nSeg+iChanged)/2; 1; i=i/2){
212598 Fts5CResult *pRes = &pIter->aFirst[i];
212599
212600 assert( pNew->pLeaf )((void) (0));
212601 assert( pRes->bTermEq==0 || pOther->pLeaf )((void) (0));
212602
212603 if( pRes->bTermEq ){
212604 if( pNew->iRowid==pOther->iRowid ){
212605 return 1;
212606 }else if( (pOther->iRowid>pNew->iRowid)==pIter->bRev ){
212607 pIter->iSwitchRowid = pOther->iRowid;
212608 pNew = pOther;
212609 }else if( (pOther->iRowid>pIter->iSwitchRowid)==pIter->bRev ){
212610 pIter->iSwitchRowid = pOther->iRowid;
212611 }
212612 }
212613 pRes->iFirst = (u16)(pNew - pIter->aSeg);
212614 if( i==1 ) break;
212615
212616 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
212617 }
212618 }
212619
212620 *ppFirst = pNew;
212621 return 0;
212622}
212623
212624/*
212625** Set the pIter->bEof variable based on the state of the sub-iterators.
212626*/
212627static void fts5MultiIterSetEof(Fts5Iter *pIter){
212628 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
212629 pIter->base.bEof = pSeg->pLeaf==0;
212630 pIter->iSwitchRowid = pSeg->iRowid;
212631}
212632
212633/*
212634** Move the iterator to the next entry.
212635**
212636** If an error occurs, an error code is left in Fts5Index.rc. It is not
212637** considered an error if the iterator reaches EOF, or if it is already at
212638** EOF when this function is called.
212639*/
212640static void fts5MultiIterNext(
212641 Fts5Index *p,
212642 Fts5Iter *pIter,
212643 int bFrom, /* True if argument iFrom is valid */
212644 i64 iFrom /* Advance at least as far as this */
212645){
212646 int bUseFrom = bFrom;
212647 assert( pIter->base.bEof==0 )((void) (0));
212648 while( p->rc==SQLITE_OK0 ){
212649 int iFirst = pIter->aFirst[1].iFirst;
212650 int bNewTerm = 0;
212651 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
212652 assert( p->rc==SQLITE_OK )((void) (0));
212653 if( bUseFrom && pSeg->pDlidx ){
212654 fts5SegIterNextFrom(p, pSeg, iFrom);
212655 }else{
212656 pSeg->xNext(p, pSeg, &bNewTerm);
212657 }
212658
212659 if( pSeg->pLeaf==0 || bNewTerm
212660 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
212661 ){
212662 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
212663 fts5MultiIterSetEof(pIter);
212664 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
212665 if( pSeg->pLeaf==0 ) return;
212666 }
212667
212668 fts5AssertMultiIterSetup(p, pIter);
212669 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf )((void) (0));
212670 if( pIter->bSkipEmpty==0 || pSeg->nPos ){
212671 pIter->xSetOutputs(pIter, pSeg);
212672 return;
212673 }
212674 bUseFrom = 0;
212675 }
212676}
212677
212678static void fts5MultiIterNext2(
212679 Fts5Index *p,
212680 Fts5Iter *pIter,
212681 int *pbNewTerm /* OUT: True if *might* be new term */
212682){
212683 assert( pIter->bSkipEmpty )((void) (0));
212684 if( p->rc==SQLITE_OK0 ){
212685 *pbNewTerm = 0;
212686 do{
212687 int iFirst = pIter->aFirst[1].iFirst;
212688 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
212689 int bNewTerm = 0;
212690
212691 assert( p->rc==SQLITE_OK )((void) (0));
212692 pSeg->xNext(p, pSeg, &bNewTerm);
212693 if( pSeg->pLeaf==0 || bNewTerm
212694 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
212695 ){
212696 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
212697 fts5MultiIterSetEof(pIter);
212698 *pbNewTerm = 1;
212699 }
212700 fts5AssertMultiIterSetup(p, pIter);
212701
212702 }while( fts5MultiIterIsEmpty(p, pIter) );
212703 }
212704}
212705
212706static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
212707 UNUSED_PARAM2(pUnused1, pUnused2);
212708}
212709
212710static Fts5Iter *fts5MultiIterAlloc(
212711 Fts5Index *p, /* FTS5 backend to iterate within */
212712 int nSeg
212713){
212714 Fts5Iter *pNew;
212715 int nSlot; /* Power of two >= nSeg */
212716
212717 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
212718 pNew = fts5IdxMalloc(p,
212719 sizeof(Fts5Iter) + /* pNew */
212720 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
212721 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
212722 );
212723 if( pNew ){
212724 pNew->nSeg = nSlot;
212725 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
212726 pNew->pIndex = p;
212727 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
212728 }
212729 return pNew;
212730}
212731
212732static void fts5PoslistCallback(
212733 Fts5Index *pUnused,
212734 void *pContext,
212735 const u8 *pChunk, int nChunk
212736){
212737 UNUSED_PARAM(pUnused);
212738 assert_nc( nChunk>=0 );
212739 if( nChunk>0 ){
212740 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
212741 }
212742}
212743
212744typedef struct PoslistCallbackCtx PoslistCallbackCtx;
212745struct PoslistCallbackCtx {
212746 Fts5Buffer *pBuf; /* Append to this buffer */
212747 Fts5Colset *pColset; /* Restrict matches to this column */
212748 int eState; /* See above */
212749};
212750
212751typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
212752struct PoslistOffsetsCtx {
212753 Fts5Buffer *pBuf; /* Append to this buffer */
212754 Fts5Colset *pColset; /* Restrict matches to this column */
212755 int iRead;
212756 int iWrite;
212757};
212758
212759/*
212760** TODO: Make this more efficient!
212761*/
212762static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
212763 int i;
212764 for(i=0; i<pColset->nCol; i++){
212765 if( pColset->aiCol[i]==iCol ) return 1;
212766 }
212767 return 0;
212768}
212769
212770static void fts5PoslistOffsetsCallback(
212771 Fts5Index *pUnused,
212772 void *pContext,
212773 const u8 *pChunk, int nChunk
212774){
212775 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
212776 UNUSED_PARAM(pUnused);
212777 assert_nc( nChunk>=0 );
212778 if( nChunk>0 ){
212779 int i = 0;
212780 while( i<nChunk ){
212781 int iVal;
212782 i += fts5GetVarint32(&pChunk[i], iVal);
212783 iVal += pCtx->iRead - 2;
212784 pCtx->iRead = iVal;
212785 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
212786 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
212787 pCtx->iWrite = iVal;
212788 }
212789 }
212790 }
212791}
212792
212793static void fts5PoslistFilterCallback(
212794 Fts5Index *pUnused,
212795 void *pContext,
212796 const u8 *pChunk, int nChunk
212797){
212798 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
212799 UNUSED_PARAM(pUnused);
212800 assert_nc( nChunk>=0 );
212801 if( nChunk>0 ){
212802 /* Search through to find the first varint with value 1. This is the
212803 ** start of the next columns hits. */
212804 int i = 0;
212805 int iStart = 0;
212806
212807 if( pCtx->eState==2 ){
212808 int iCol;
212809 fts5FastGetVarint32(pChunk, i, iCol);
212810 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
212811 pCtx->eState = 1;
212812 fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
212813 }else{
212814 pCtx->eState = 0;
212815 }
212816 }
212817
212818 do {
212819 while( i<nChunk && pChunk[i]!=0x01 ){
212820 while( pChunk[i] & 0x80 ) i++;
212821 i++;
212822 }
212823 if( pCtx->eState ){
212824 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
212825 }
212826 if( i<nChunk ){
212827 int iCol;
212828 iStart = i;
212829 i++;
212830 if( i>=nChunk ){
212831 pCtx->eState = 2;
212832 }else{
212833 fts5FastGetVarint32(pChunk, i, iCol);
212834 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
212835 if( pCtx->eState ){
212836 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
212837 iStart = i;
212838 }
212839 }
212840 }
212841 }while( i<nChunk );
212842 }
212843}
212844
212845static void fts5ChunkIterate(
212846 Fts5Index *p, /* Index object */
212847 Fts5SegIter *pSeg, /* Poslist of this iterator */
212848 void *pCtx, /* Context pointer for xChunk callback */
212849 void (*xChunk)(Fts5Index*, void*, const u8*, int)
212850){
212851 int nRem = pSeg->nPos; /* Number of bytes still to come */
212852 Fts5Data *pData = 0;
212853 u8 *pChunk = &pSeg->pLeaf->p[pSeg->iLeafOffset];
212854 int nChunk = MIN(nRem, pSeg->pLeaf->szLeaf - pSeg->iLeafOffset)((nRem)<(pSeg->pLeaf->szLeaf - pSeg->iLeafOffset)
?(nRem):(pSeg->pLeaf->szLeaf - pSeg->iLeafOffset))
;
212855 int pgno = pSeg->iLeafPgno;
212856 int pgnoSave = 0;
212857
212858 /* This function does notmwork with detail=none databases. */
212859 assert( p->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
212860
212861 if( (pSeg->flags & FTS5_SEGITER_REVERSE)==0 ){
212862 pgnoSave = pgno+1;
212863 }
212864
212865 while( 1 ){
212866 xChunk(p, pCtx, pChunk, nChunk);
212867 nRem -= nChunk;
212868 fts5DataRelease(pData);
212869 if( nRem<=0 ){
212870 break;
212871 }else{
212872 pgno++;
212873 pData = fts5LeafRead(p, FTS5_SEGMENT_ROWID(pSeg->pSeg->iSegid, pgno));
212874 if( pData==0 ) break;
212875 pChunk = &pData->p[4];
212876 nChunk = MIN(nRem, pData->szLeaf - 4)((nRem)<(pData->szLeaf - 4)?(nRem):(pData->szLeaf - 4
))
;
212877 if( pgno==pgnoSave ){
212878 assert( pSeg->pNextLeaf==0 )((void) (0));
212879 pSeg->pNextLeaf = pData;
212880 pData = 0;
212881 }
212882 }
212883 }
212884}
212885
212886/*
212887** Iterator pIter currently points to a valid entry (not EOF). This
212888** function appends the position list data for the current entry to
212889** buffer pBuf. It does not make a copy of the position-list size
212890** field.
212891*/
212892static void fts5SegiterPoslist(
212893 Fts5Index *p,
212894 Fts5SegIter *pSeg,
212895 Fts5Colset *pColset,
212896 Fts5Buffer *pBuf
212897){
212898 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+FTS5_DATA_ZERO_PADDING) ){
212899 memset(&pBuf->p[pBuf->n+pSeg->nPos], 0, FTS5_DATA_ZERO_PADDING);
212900 if( pColset==0 ){
212901 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
212902 }else{
212903 if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
212904 PoslistCallbackCtx sCtx;
212905 sCtx.pBuf = pBuf;
212906 sCtx.pColset = pColset;
212907 sCtx.eState = fts5IndexColsetTest(pColset, 0);
212908 assert( sCtx.eState==0 || sCtx.eState==1 )((void) (0));
212909 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
212910 }else{
212911 PoslistOffsetsCtx sCtx;
212912 memset(&sCtx, 0, sizeof(sCtx));
212913 sCtx.pBuf = pBuf;
212914 sCtx.pColset = pColset;
212915 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
212916 }
212917 }
212918 }
212919}
212920
212921/*
212922** IN/OUT parameter (*pa) points to a position list n bytes in size. If
212923** the position list contains entries for column iCol, then (*pa) is set
212924** to point to the sub-position-list for that column and the number of
212925** bytes in it returned. Or, if the argument position list does not
212926** contain any entries for column iCol, return 0.
212927*/
212928static int fts5IndexExtractCol(
212929 const u8 **pa, /* IN/OUT: Pointer to poslist */
212930 int n, /* IN: Size of poslist in bytes */
212931 int iCol /* Column to extract from poslist */
212932){
212933 int iCurrent = 0; /* Anything before the first 0x01 is col 0 */
212934 const u8 *p = *pa;
212935 const u8 *pEnd = &p[n]; /* One byte past end of position list */
212936
212937 while( iCol>iCurrent ){
212938 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
212939 ** not part of a varint. Note that it is not possible for a negative
212940 ** or extremely large varint to occur within an uncorrupted position
212941 ** list. So the last byte of each varint may be assumed to have a clear
212942 ** 0x80 bit. */
212943 while( *p!=0x01 ){
212944 while( *p++ & 0x80 );
212945 if( p>=pEnd ) return 0;
212946 }
212947 *pa = p++;
212948 iCurrent = *p++;
212949 if( iCurrent & 0x80 ){
212950 p--;
212951 p += fts5GetVarint32(p, iCurrent);
212952 }
212953 }
212954 if( iCol!=iCurrent ) return 0;
212955
212956 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
212957 ** not part of a varint */
212958 while( p<pEnd && *p!=0x01 ){
212959 while( *p++ & 0x80 );
212960 }
212961
212962 return p - (*pa);
212963}
212964
212965static void fts5IndexExtractColset(
212966 int *pRc,
212967 Fts5Colset *pColset, /* Colset to filter on */
212968 const u8 *pPos, int nPos, /* Position list */
212969 Fts5Buffer *pBuf /* Output buffer */
212970){
212971 if( *pRc==SQLITE_OK0 ){
212972 int i;
212973 fts5BufferZero(pBuf);
212974 for(i=0; i<pColset->nCol; i++){
212975 const u8 *pSub = pPos;
212976 int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
212977 if( nSub ){
212978 fts5BufferAppendBlob(pRc, pBuf, nSub, pSub);
212979 }
212980 }
212981 }
212982}
212983
212984/*
212985** xSetOutputs callback used by detail=none tables.
212986*/
212987static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
212988 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
212989 pIter->base.iRowid = pSeg->iRowid;
212990 pIter->base.nData = pSeg->nPos;
212991}
212992
212993/*
212994** xSetOutputs callback used by detail=full and detail=col tables when no
212995** column filters are specified.
212996*/
212997static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
212998 pIter->base.iRowid = pSeg->iRowid;
212999 pIter->base.nData = pSeg->nPos;
213000
213001 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE )((void) (0));
213002 assert( pIter->pColset==0 )((void) (0));
213003
213004 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
213005 /* All data is stored on the current page. Populate the output
213006 ** variables to point into the body of the page object. */
213007 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
213008 }else{
213009 /* The data is distributed over two or more pages. Copy it into the
213010 ** Fts5Iter.poslist buffer and then set the output pointer to point
213011 ** to this buffer. */
213012 fts5BufferZero(&pIter->poslist);
213013 fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
213014 pIter->base.pData = pIter->poslist.p;
213015 }
213016}
213017
213018/*
213019** xSetOutputs callback used when the Fts5Colset object has nCol==0 (match
213020** against no columns at all).
213021*/
213022static void fts5IterSetOutputs_ZeroColset(Fts5Iter *pIter, Fts5SegIter *pSeg){
213023 UNUSED_PARAM(pSeg);
213024 pIter->base.nData = 0;
213025}
213026
213027/*
213028** xSetOutputs callback used by detail=col when there is a column filter
213029** and there are 100 or more columns. Also called as a fallback from
213030** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
213031*/
213032static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
213033 fts5BufferZero(&pIter->poslist);
213034 fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
213035 pIter->base.iRowid = pSeg->iRowid;
213036 pIter->base.pData = pIter->poslist.p;
213037 pIter->base.nData = pIter->poslist.n;
213038}
213039
213040/*
213041** xSetOutputs callback used when:
213042**
213043** * detail=col,
213044** * there is a column filter, and
213045** * the table contains 100 or fewer columns.
213046**
213047** The last point is to ensure all column numbers are stored as
213048** single-byte varints.
213049*/
213050static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
213051
213052 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
213053 assert( pIter->pColset )((void) (0));
213054
213055 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
213056 fts5IterSetOutputs_Col(pIter, pSeg);
213057 }else{
213058 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
213059 u8 *pEnd = (u8*)&a[pSeg->nPos];
213060 int iPrev = 0;
213061 int *aiCol = pIter->pColset->aiCol;
213062 int *aiColEnd = &aiCol[pIter->pColset->nCol];
213063
213064 u8 *aOut = pIter->poslist.p;
213065 int iPrevOut = 0;
213066
213067 pIter->base.iRowid = pSeg->iRowid;
213068
213069 while( a<pEnd ){
213070 iPrev += (int)a++[0] - 2;
213071 while( *aiCol<iPrev ){
213072 aiCol++;
213073 if( aiCol==aiColEnd ) goto setoutputs_col_out;
213074 }
213075 if( *aiCol==iPrev ){
213076 *aOut++ = (u8)((iPrev - iPrevOut) + 2);
213077 iPrevOut = iPrev;
213078 }
213079 }
213080
213081setoutputs_col_out:
213082 pIter->base.pData = pIter->poslist.p;
213083 pIter->base.nData = aOut - pIter->poslist.p;
213084 }
213085}
213086
213087/*
213088** xSetOutputs callback used by detail=full when there is a column filter.
213089*/
213090static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
213091 Fts5Colset *pColset = pIter->pColset;
213092 pIter->base.iRowid = pSeg->iRowid;
213093
213094 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL )((void) (0));
213095 assert( pColset )((void) (0));
213096
213097 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
213098 /* All data is stored on the current page. Populate the output
213099 ** variables to point into the body of the page object. */
213100 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
213101 if( pColset->nCol==1 ){
213102 pIter->base.nData = fts5IndexExtractCol(&a, pSeg->nPos,pColset->aiCol[0]);
213103 pIter->base.pData = a;
213104 }else{
213105 int *pRc = &pIter->pIndex->rc;
213106 fts5BufferZero(&pIter->poslist);
213107 fts5IndexExtractColset(pRc, pColset, a, pSeg->nPos, &pIter->poslist);
213108 pIter->base.pData = pIter->poslist.p;
213109 pIter->base.nData = pIter->poslist.n;
213110 }
213111 }else{
213112 /* The data is distributed over two or more pages. Copy it into the
213113 ** Fts5Iter.poslist buffer and then set the output pointer to point
213114 ** to this buffer. */
213115 fts5BufferZero(&pIter->poslist);
213116 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
213117 pIter->base.pData = pIter->poslist.p;
213118 pIter->base.nData = pIter->poslist.n;
213119 }
213120}
213121
213122static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
213123 if( *pRc==SQLITE_OK0 ){
213124 Fts5Config *pConfig = pIter->pIndex->pConfig;
213125 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
213126 pIter->xSetOutputs = fts5IterSetOutputs_None;
213127 }
213128
213129 else if( pIter->pColset==0 ){
213130 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
213131 }
213132
213133 else if( pIter->pColset->nCol==0 ){
213134 pIter->xSetOutputs = fts5IterSetOutputs_ZeroColset;
213135 }
213136
213137 else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
213138 pIter->xSetOutputs = fts5IterSetOutputs_Full;
213139 }
213140
213141 else{
213142 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS )((void) (0));
213143 if( pConfig->nCol<=100 ){
213144 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
213145 sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
213146 }else{
213147 pIter->xSetOutputs = fts5IterSetOutputs_Col;
213148 }
213149 }
213150 }
213151}
213152
213153
213154/*
213155** Allocate a new Fts5Iter object.
213156**
213157** The new object will be used to iterate through data in structure pStruct.
213158** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
213159** is zero or greater, data from the first nSegment segments on level iLevel
213160** is merged.
213161**
213162** The iterator initially points to the first term/rowid entry in the
213163** iterated data.
213164*/
213165static void fts5MultiIterNew(
213166 Fts5Index *p, /* FTS5 backend to iterate within */
213167 Fts5Structure *pStruct, /* Structure of specific index */
213168 int flags, /* FTS5INDEX_QUERY_XXX flags */
213169 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
213170 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
213171 int iLevel, /* Level to iterate (-1 for all) */
213172 int nSegment, /* Number of segments to merge (iLevel>=0) */
213173 Fts5Iter **ppOut /* New object */
213174){
213175 int nSeg = 0; /* Number of segment-iters in use */
213176 int iIter = 0; /* */
213177 int iSeg; /* Used to iterate through segments */
213178 Fts5StructureLevel *pLvl;
213179 Fts5Iter *pNew;
213180
213181 assert( (pTerm==0 && nTerm==0) || iLevel<0 )((void) (0));
213182
213183 /* Allocate space for the new multi-seg-iterator. */
213184 if( p->rc==SQLITE_OK0 ){
213185 if( iLevel<0 ){
213186 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) )((void) (0));
213187 nSeg = pStruct->nSegment;
213188 nSeg += (p->pHash ? 1 : 0);
213189 }else{
213190 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment)((pStruct->aLevel[iLevel].nSeg)<(nSegment)?(pStruct->
aLevel[iLevel].nSeg):(nSegment))
;
213191 }
213192 }
213193 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
213194 if( pNew==0 ) return;
213195 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
213196 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
213197 pNew->pColset = pColset;
213198 if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
213199 fts5IterSetOutputCb(&p->rc, pNew);
213200 }
213201
213202 /* Initialize each of the component segment iterators. */
213203 if( p->rc==SQLITE_OK0 ){
213204 if( iLevel<0 ){
213205 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
213206 if( p->pHash ){
213207 /* Add a segment iterator for the current contents of the hash table. */
213208 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
213209 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
213210 }
213211 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
213212 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
213213 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
213214 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
213215 if( pTerm==0 ){
213216 fts5SegIterInit(p, pSeg, pIter);
213217 }else{
213218 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
213219 }
213220 }
213221 }
213222 }else{
213223 pLvl = &pStruct->aLevel[iLevel];
213224 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
213225 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
213226 }
213227 }
213228 assert( iIter==nSeg )((void) (0));
213229 }
213230
213231 /* If the above was successful, each component iterators now points
213232 ** to the first entry in its segment. In this case initialize the
213233 ** aFirst[] array. Or, if an error has occurred, free the iterator
213234 ** object and set the output variable to NULL. */
213235 if( p->rc==SQLITE_OK0 ){
213236 for(iIter=pNew->nSeg-1; iIter>0; iIter--){
213237 int iEq;
213238 if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
213239 Fts5SegIter *pSeg = &pNew->aSeg[iEq];
213240 if( p->rc==SQLITE_OK0 ) pSeg->xNext(p, pSeg, 0);
213241 fts5MultiIterAdvanced(p, pNew, iEq, iIter);
213242 }
213243 }
213244 fts5MultiIterSetEof(pNew);
213245 fts5AssertMultiIterSetup(p, pNew);
213246
213247 if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
213248 fts5MultiIterNext(p, pNew, 0, 0);
213249 }else if( pNew->base.bEof==0 ){
213250 Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
213251 pNew->xSetOutputs(pNew, pSeg);
213252 }
213253
213254 }else{
213255 fts5MultiIterFree(pNew);
213256 *ppOut = 0;
213257 }
213258}
213259
213260/*
213261** Create an Fts5Iter that iterates through the doclist provided
213262** as the second argument.
213263*/
213264static void fts5MultiIterNew2(
213265 Fts5Index *p, /* FTS5 backend to iterate within */
213266 Fts5Data *pData, /* Doclist to iterate through */
213267 int bDesc, /* True for descending rowid order */
213268 Fts5Iter **ppOut /* New object */
213269){
213270 Fts5Iter *pNew;
213271 pNew = fts5MultiIterAlloc(p, 2);
213272 if( pNew ){
213273 Fts5SegIter *pIter = &pNew->aSeg[1];
213274
213275 pIter->flags = FTS5_SEGITER_ONETERM;
213276 if( pData->szLeaf>0 ){
213277 pIter->pLeaf = pData;
213278 pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
213279 pIter->iEndofDoclist = pData->nn;
213280 pNew->aFirst[1].iFirst = 1;
213281 if( bDesc ){
213282 pNew->bRev = 1;
213283 pIter->flags |= FTS5_SEGITER_REVERSE;
213284 fts5SegIterReverseInitPage(p, pIter);
213285 }else{
213286 fts5SegIterLoadNPos(p, pIter);
213287 }
213288 pData = 0;
213289 }else{
213290 pNew->base.bEof = 1;
213291 }
213292 fts5SegIterSetNext(p, pIter);
213293
213294 *ppOut = pNew;
213295 }
213296
213297 fts5DataRelease(pData);
213298}
213299
213300/*
213301** Return true if the iterator is at EOF or if an error has occurred.
213302** False otherwise.
213303*/
213304static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
213305 assert( p->rc((void) (0))
213306 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof((void) (0))
213307 )((void) (0));
213308 return (p->rc || pIter->base.bEof);
213309}
213310
213311/*
213312** Return the rowid of the entry that the iterator currently points
213313** to. If the iterator points to EOF when this function is called the
213314** results are undefined.
213315*/
213316static i64 fts5MultiIterRowid(Fts5Iter *pIter){
213317 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf )((void) (0));
213318 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
213319}
213320
213321/*
213322** Move the iterator to the next entry at or following iMatch.
213323*/
213324static void fts5MultiIterNextFrom(
213325 Fts5Index *p,
213326 Fts5Iter *pIter,
213327 i64 iMatch
213328){
213329 while( 1 ){
213330 i64 iRowid;
213331 fts5MultiIterNext(p, pIter, 1, iMatch);
213332 if( fts5MultiIterEof(p, pIter) ) break;
213333 iRowid = fts5MultiIterRowid(pIter);
213334 if( pIter->bRev==0 && iRowid>=iMatch ) break;
213335 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
213336 }
213337}
213338
213339/*
213340** Return a pointer to a buffer containing the term associated with the
213341** entry that the iterator currently points to.
213342*/
213343static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
213344 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
213345 *pn = p->term.n;
213346 return p->term.p;
213347}
213348
213349/*
213350** Allocate a new segment-id for the structure pStruct. The new segment
213351** id must be between 1 and 65335 inclusive, and must not be used by
213352** any currently existing segment. If a free segment id cannot be found,
213353** SQLITE_FULL is returned.
213354**
213355** If an error has already occurred, this function is a no-op. 0 is
213356** returned in this case.
213357*/
213358static int fts5AllocateSegid(Fts5Index *p, Fts5Structure *pStruct){
213359 int iSegid = 0;
213360
213361 if( p->rc==SQLITE_OK0 ){
213362 if( pStruct->nSegment>=FTS5_MAX_SEGMENT ){
213363 p->rc = SQLITE_FULL13;
213364 }else{
213365 /* FTS5_MAX_SEGMENT is currently defined as 2000. So the following
213366 ** array is 63 elements, or 252 bytes, in size. */
213367 u32 aUsed[(FTS5_MAX_SEGMENT+31) / 32];
213368 int iLvl, iSeg;
213369 int i;
213370 u32 mask;
213371 memset(aUsed, 0, sizeof(aUsed));
213372 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
213373 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
213374 int iId = pStruct->aLevel[iLvl].aSeg[iSeg].iSegid;
213375 if( iId<=FTS5_MAX_SEGMENT && iId>0 ){
213376 aUsed[(iId-1) / 32] |= (u32)1 << ((iId-1) % 32);
213377 }
213378 }
213379 }
213380
213381 for(i=0; aUsed[i]==0xFFFFFFFF; i++);
213382 mask = aUsed[i];
213383 for(iSegid=0; mask & ((u32)1 << iSegid); iSegid++);
213384 iSegid += 1 + i*32;
213385
213386#ifdef SQLITE_DEBUG
213387 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
213388 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
213389 assert_nc( iSegid!=pStruct->aLevel[iLvl].aSeg[iSeg].iSegid );
213390 }
213391 }
213392 assert_nc( iSegid>0 && iSegid<=FTS5_MAX_SEGMENT );
213393
213394 {
213395 sqlite3_stmt *pIdxSelect = fts5IdxSelectStmt(p);
213396 if( p->rc==SQLITE_OK0 ){
213397 u8 aBlob[2] = {0xff, 0xff};
213398 sqlite3_bind_int(pIdxSelect, 1, iSegid);
213399 sqlite3_bind_blob(pIdxSelect, 2, aBlob, 2, SQLITE_STATIC((sqlite3_destructor_type)0));
213400 assert_nc( sqlite3_step(pIdxSelect)!=SQLITE_ROW100 );
213401 p->rc = sqlite3_reset(pIdxSelect);
213402 sqlite3_bind_null(pIdxSelect, 2);
213403 }
213404 }
213405#endif
213406 }
213407 }
213408
213409 return iSegid;
213410}
213411
213412/*
213413** Discard all data currently cached in the hash-tables.
213414*/
213415static void fts5IndexDiscardData(Fts5Index *p){
213416 assert( p->pHash || p->nPendingData==0 )((void) (0));
213417 if( p->pHash ){
213418 sqlite3Fts5HashClear(p->pHash);
213419 p->nPendingData = 0;
213420 }
213421}
213422
213423/*
213424** Return the size of the prefix, in bytes, that buffer
213425** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
213426**
213427** Buffer (pNew/<length-unknown>) is guaranteed to be greater
213428** than buffer (pOld/nOld).
213429*/
213430static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
213431 int i;
213432 for(i=0; i<nOld; i++){
213433 if( pOld[i]!=pNew[i] ) break;
213434 }
213435 return i;
213436}
213437
213438static void fts5WriteDlidxClear(
213439 Fts5Index *p,
213440 Fts5SegWriter *pWriter,
213441 int bFlush /* If true, write dlidx to disk */
213442){
213443 int i;
213444 assert( bFlush==0 || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n>0) )((void) (0));
213445 for(i=0; i<pWriter->nDlidx; i++){
213446 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
213447 if( pDlidx->buf.n==0 ) break;
213448 if( bFlush ){
213449 assert( pDlidx->pgno!=0 )((void) (0));
213450 fts5DataWrite(p,
213451 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
213452 pDlidx->buf.p, pDlidx->buf.n
213453 );
213454 }
213455 sqlite3Fts5BufferZero(&pDlidx->buf);
213456 pDlidx->bPrevValid = 0;
213457 }
213458}
213459
213460/*
213461** Grow the pWriter->aDlidx[] array to at least nLvl elements in size.
213462** Any new array elements are zeroed before returning.
213463*/
213464static int fts5WriteDlidxGrow(
213465 Fts5Index *p,
213466 Fts5SegWriter *pWriter,
213467 int nLvl
213468){
213469 if( p->rc==SQLITE_OK0 && nLvl>=pWriter->nDlidx ){
213470 Fts5DlidxWriter *aDlidx = (Fts5DlidxWriter*)sqlite3_realloc64(
213471 pWriter->aDlidx, sizeof(Fts5DlidxWriter) * nLvl
213472 );
213473 if( aDlidx==0 ){
213474 p->rc = SQLITE_NOMEM7;
213475 }else{
213476 size_t nByte = sizeof(Fts5DlidxWriter) * (nLvl - pWriter->nDlidx);
213477 memset(&aDlidx[pWriter->nDlidx], 0, nByte);
213478 pWriter->aDlidx = aDlidx;
213479 pWriter->nDlidx = nLvl;
213480 }
213481 }
213482 return p->rc;
213483}
213484
213485/*
213486** If the current doclist-index accumulating in pWriter->aDlidx[] is large
213487** enough, flush it to disk and return 1. Otherwise discard it and return
213488** zero.
213489*/
213490static int fts5WriteFlushDlidx(Fts5Index *p, Fts5SegWriter *pWriter){
213491 int bFlag = 0;
213492
213493 /* If there were FTS5_MIN_DLIDX_SIZE or more empty leaf pages written
213494 ** to the database, also write the doclist-index to disk. */
213495 if( pWriter->aDlidx[0].buf.n>0 && pWriter->nEmpty>=FTS5_MIN_DLIDX_SIZE ){
213496 bFlag = 1;
213497 }
213498 fts5WriteDlidxClear(p, pWriter, bFlag);
213499 pWriter->nEmpty = 0;
213500 return bFlag;
213501}
213502
213503/*
213504** This function is called whenever processing of the doclist for the
213505** last term on leaf page (pWriter->iBtPage) is completed.
213506**
213507** The doclist-index for that term is currently stored in-memory within the
213508** Fts5SegWriter.aDlidx[] array. If it is large enough, this function
213509** writes it out to disk. Or, if it is too small to bother with, discards
213510** it.
213511**
213512** Fts5SegWriter.btterm currently contains the first term on page iBtPage.
213513*/
213514static void fts5WriteFlushBtree(Fts5Index *p, Fts5SegWriter *pWriter){
213515 int bFlag;
213516
213517 assert( pWriter->iBtPage || pWriter->nEmpty==0 )((void) (0));
213518 if( pWriter->iBtPage==0 ) return;
213519 bFlag = fts5WriteFlushDlidx(p, pWriter);
213520
213521 if( p->rc==SQLITE_OK0 ){
213522 const char *z = (pWriter->btterm.n>0?(const char*)pWriter->btterm.p:"");
213523 /* The following was already done in fts5WriteInit(): */
213524 /* sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid); */
213525 sqlite3_bind_blob(p->pIdxWriter, 2, z, pWriter->btterm.n, SQLITE_STATIC((sqlite3_destructor_type)0));
213526 sqlite3_bind_int64(p->pIdxWriter, 3, bFlag + ((i64)pWriter->iBtPage<<1));
213527 sqlite3_step(p->pIdxWriter);
213528 p->rc = sqlite3_reset(p->pIdxWriter);
213529 sqlite3_bind_null(p->pIdxWriter, 2);
213530 }
213531 pWriter->iBtPage = 0;
213532}
213533
213534/*
213535** This is called once for each leaf page except the first that contains
213536** at least one term. Argument (nTerm/pTerm) is the split-key - a term that
213537** is larger than all terms written to earlier leaves, and equal to or
213538** smaller than the first term on the new leaf.
213539**
213540** If an error occurs, an error code is left in Fts5Index.rc. If an error
213541** has already occurred when this function is called, it is a no-op.
213542*/
213543static void fts5WriteBtreeTerm(
213544 Fts5Index *p, /* FTS5 backend object */
213545 Fts5SegWriter *pWriter, /* Writer object */
213546 int nTerm, const u8 *pTerm /* First term on new page */
213547){
213548 fts5WriteFlushBtree(p, pWriter);
213549 if( p->rc==SQLITE_OK0 ){
213550 fts5BufferSet(&p->rc, &pWriter->btterm, nTerm, pTerm);
213551 pWriter->iBtPage = pWriter->writer.pgno;
213552 }
213553}
213554
213555/*
213556** This function is called when flushing a leaf page that contains no
213557** terms at all to disk.
213558*/
213559static void fts5WriteBtreeNoTerm(
213560 Fts5Index *p, /* FTS5 backend object */
213561 Fts5SegWriter *pWriter /* Writer object */
213562){
213563 /* If there were no rowids on the leaf page either and the doclist-index
213564 ** has already been started, append an 0x00 byte to it. */
213565 if( pWriter->bFirstRowidInPage && pWriter->aDlidx[0].buf.n>0 ){
213566 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[0];
213567 assert( pDlidx->bPrevValid )((void) (0));
213568 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, 0);
213569 }
213570
213571 /* Increment the "number of sequential leaves without a term" counter. */
213572 pWriter->nEmpty++;
213573}
213574
213575static i64 fts5DlidxExtractFirstRowid(Fts5Buffer *pBuf){
213576 i64 iRowid;
213577 int iOff;
213578
213579 iOff = 1 + fts5GetVarint(&pBuf->p[1], (u64*)&iRowid);
213580 fts5GetVarint(&pBuf->p[iOff], (u64*)&iRowid);
213581 return iRowid;
213582}
213583
213584/*
213585** Rowid iRowid has just been appended to the current leaf page. It is the
213586** first on the page. This function appends an appropriate entry to the current
213587** doclist-index.
213588*/
213589static void fts5WriteDlidxAppend(
213590 Fts5Index *p,
213591 Fts5SegWriter *pWriter,
213592 i64 iRowid
213593){
213594 int i;
213595 int bDone = 0;
213596
213597 for(i=0; p->rc==SQLITE_OK0 && bDone==0; i++){
213598 i64 iVal;
213599 Fts5DlidxWriter *pDlidx = &pWriter->aDlidx[i];
213600
213601 if( pDlidx->buf.n>=p->pConfig->pgsz ){
213602 /* The current doclist-index page is full. Write it to disk and push
213603 ** a copy of iRowid (which will become the first rowid on the next
213604 ** doclist-index leaf page) up into the next level of the b-tree
213605 ** hierarchy. If the node being flushed is currently the root node,
213606 ** also push its first rowid upwards. */
213607 pDlidx->buf.p[0] = 0x01; /* Not the root node */
213608 fts5DataWrite(p,
213609 FTS5_DLIDX_ROWID(pWriter->iSegid, i, pDlidx->pgno),
213610 pDlidx->buf.p, pDlidx->buf.n
213611 );
213612 fts5WriteDlidxGrow(p, pWriter, i+2);
213613 pDlidx = &pWriter->aDlidx[i];
213614 if( p->rc==SQLITE_OK0 && pDlidx[1].buf.n==0 ){
213615 i64 iFirst = fts5DlidxExtractFirstRowid(&pDlidx->buf);
213616
213617 /* This was the root node. Push its first rowid up to the new root. */
213618 pDlidx[1].pgno = pDlidx->pgno;
213619 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, 0);
213620 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, pDlidx->pgno);
213621 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx[1].buf, iFirst);
213622 pDlidx[1].bPrevValid = 1;
213623 pDlidx[1].iPrev = iFirst;
213624 }
213625
213626 sqlite3Fts5BufferZero(&pDlidx->buf);
213627 pDlidx->bPrevValid = 0;
213628 pDlidx->pgno++;
213629 }else{
213630 bDone = 1;
213631 }
213632
213633 if( pDlidx->bPrevValid ){
213634 iVal = iRowid - pDlidx->iPrev;
213635 }else{
213636 i64 iPgno = (i==0 ? pWriter->writer.pgno : pDlidx[-1].pgno);
213637 assert( pDlidx->buf.n==0 )((void) (0));
213638 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, !bDone);
213639 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iPgno);
213640 iVal = iRowid;
213641 }
213642
213643 sqlite3Fts5BufferAppendVarint(&p->rc, &pDlidx->buf, iVal);
213644 pDlidx->bPrevValid = 1;
213645 pDlidx->iPrev = iRowid;
213646 }
213647}
213648
213649static void fts5WriteFlushLeaf(Fts5Index *p, Fts5SegWriter *pWriter){
213650 static const u8 zero[] = { 0x00, 0x00, 0x00, 0x00 };
213651 Fts5PageWriter *pPage = &pWriter->writer;
213652 i64 iRowid;
213653
213654 assert( (pPage->pgidx.n==0)==(pWriter->bFirstTermInPage) )((void) (0));
213655
213656 /* Set the szLeaf header field. */
213657 assert( 0==fts5GetU16(&pPage->buf.p[2]) )((void) (0));
213658 fts5PutU16(&pPage->buf.p[2], (u16)pPage->buf.n);
213659
213660 if( pWriter->bFirstTermInPage ){
213661 /* No term was written to this page. */
213662 assert( pPage->pgidx.n==0 )((void) (0));
213663 fts5WriteBtreeNoTerm(p, pWriter);
213664 }else{
213665 /* Append the pgidx to the page buffer. Set the szLeaf header field. */
213666 fts5BufferAppendBlob(&p->rc, &pPage->buf, pPage->pgidx.n, pPage->pgidx.p);
213667 }
213668
213669 /* Write the page out to disk */
213670 iRowid = FTS5_SEGMENT_ROWID(pWriter->iSegid, pPage->pgno);
213671 fts5DataWrite(p, iRowid, pPage->buf.p, pPage->buf.n);
213672
213673 /* Initialize the next page. */
213674 fts5BufferZero(&pPage->buf);
213675 fts5BufferZero(&pPage->pgidx);
213676 fts5BufferAppendBlob(&p->rc, &pPage->buf, 4, zero);
213677 pPage->iPrevPgidx = 0;
213678 pPage->pgno++;
213679
213680 /* Increase the leaves written counter */
213681 pWriter->nLeafWritten++;
213682
213683 /* The new leaf holds no terms or rowids */
213684 pWriter->bFirstTermInPage = 1;
213685 pWriter->bFirstRowidInPage = 1;
213686}
213687
213688/*
213689** Append term pTerm/nTerm to the segment being written by the writer passed
213690** as the second argument.
213691**
213692** If an error occurs, set the Fts5Index.rc error code. If an error has
213693** already occurred, this function is a no-op.
213694*/
213695static void fts5WriteAppendTerm(
213696 Fts5Index *p,
213697 Fts5SegWriter *pWriter,
213698 int nTerm, const u8 *pTerm
213699){
213700 int nPrefix; /* Bytes of prefix compression for term */
213701 Fts5PageWriter *pPage = &pWriter->writer;
213702 Fts5Buffer *pPgidx = &pWriter->writer.pgidx;
213703 int nMin = MIN(pPage->term.n, nTerm)((pPage->term.n)<(nTerm)?(pPage->term.n):(nTerm));
213704
213705 assert( p->rc==SQLITE_OK )((void) (0));
213706 assert( pPage->buf.n>=4 )((void) (0));
213707 assert( pPage->buf.n>4 || pWriter->bFirstTermInPage )((void) (0));
213708
213709 /* If the current leaf page is full, flush it to disk. */
213710 if( (pPage->buf.n + pPgidx->n + nTerm + 2)>=p->pConfig->pgsz ){
213711 if( pPage->buf.n>4 ){
213712 fts5WriteFlushLeaf(p, pWriter);
213713 if( p->rc!=SQLITE_OK0 ) return;
213714 }
213715 fts5BufferGrow(&p->rc, &pPage->buf, nTerm+FTS5_DATA_PADDING);
213716 }
213717
213718 /* TODO1: Updating pgidx here. */
213719 pPgidx->n += sqlite3Fts5PutVarint(
213720 &pPgidx->p[pPgidx->n], pPage->buf.n - pPage->iPrevPgidx
213721 );
213722 pPage->iPrevPgidx = pPage->buf.n;
213723#if 0
213724 fts5PutU16(&pPgidx->p[pPgidx->n], pPage->buf.n);
213725 pPgidx->n += 2;
213726#endif
213727
213728 if( pWriter->bFirstTermInPage ){
213729 nPrefix = 0;
213730 if( pPage->pgno!=1 ){
213731 /* This is the first term on a leaf that is not the leftmost leaf in
213732 ** the segment b-tree. In this case it is necessary to add a term to
213733 ** the b-tree hierarchy that is (a) larger than the largest term
213734 ** already written to the segment and (b) smaller than or equal to
213735 ** this term. In other words, a prefix of (pTerm/nTerm) that is one
213736 ** byte longer than the longest prefix (pTerm/nTerm) shares with the
213737 ** previous term.
213738 **
213739 ** Usually, the previous term is available in pPage->term. The exception
213740 ** is if this is the first term written in an incremental-merge step.
213741 ** In this case the previous term is not available, so just write a
213742 ** copy of (pTerm/nTerm) into the parent node. This is slightly
213743 ** inefficient, but still correct. */
213744 int n = nTerm;
213745 if( pPage->term.n ){
213746 n = 1 + fts5PrefixCompress(nMin, pPage->term.p, pTerm);
213747 }
213748 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
213749 if( p->rc!=SQLITE_OK0 ) return;
213750 pPage = &pWriter->writer;
213751 }
213752 }else{
213753 nPrefix = fts5PrefixCompress(nMin, pPage->term.p, pTerm);
213754 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
213755 }
213756
213757 /* Append the number of bytes of new data, then the term data itself
213758 ** to the page. */
213759 fts5BufferAppendVarint(&p->rc, &pPage->buf, nTerm - nPrefix);
213760 fts5BufferAppendBlob(&p->rc, &pPage->buf, nTerm - nPrefix, &pTerm[nPrefix]);
213761
213762 /* Update the Fts5PageWriter.term field. */
213763 fts5BufferSet(&p->rc, &pPage->term, nTerm, pTerm);
213764 pWriter->bFirstTermInPage = 0;
213765
213766 pWriter->bFirstRowidInPage = 0;
213767 pWriter->bFirstRowidInDoclist = 1;
213768
213769 assert( p->rc || (pWriter->nDlidx>0 && pWriter->aDlidx[0].buf.n==0) )((void) (0));
213770 pWriter->aDlidx[0].pgno = pPage->pgno;
213771}
213772
213773/*
213774** Append a rowid and position-list size field to the writers output.
213775*/
213776static void fts5WriteAppendRowid(
213777 Fts5Index *p,
213778 Fts5SegWriter *pWriter,
213779 i64 iRowid
213780){
213781 if( p->rc==SQLITE_OK0 ){
213782 Fts5PageWriter *pPage = &pWriter->writer;
213783
213784 if( (pPage->buf.n + pPage->pgidx.n)>=p->pConfig->pgsz ){
213785 fts5WriteFlushLeaf(p, pWriter);
213786 }
213787
213788 /* If this is to be the first rowid written to the page, set the
213789 ** rowid-pointer in the page-header. Also append a value to the dlidx
213790 ** buffer, in case a doclist-index is required. */
213791 if( pWriter->bFirstRowidInPage ){
213792 fts5PutU16(pPage->buf.p, (u16)pPage->buf.n);
213793 fts5WriteDlidxAppend(p, pWriter, iRowid);
213794 }
213795
213796 /* Write the rowid. */
213797 if( pWriter->bFirstRowidInDoclist || pWriter->bFirstRowidInPage ){
213798 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid);
213799 }else{
213800 assert_nc( p->rc || iRowid>pWriter->iPrevRowid );
213801 fts5BufferAppendVarint(&p->rc, &pPage->buf, iRowid - pWriter->iPrevRowid);
213802 }
213803 pWriter->iPrevRowid = iRowid;
213804 pWriter->bFirstRowidInDoclist = 0;
213805 pWriter->bFirstRowidInPage = 0;
213806 }
213807}
213808
213809static void fts5WriteAppendPoslistData(
213810 Fts5Index *p,
213811 Fts5SegWriter *pWriter,
213812 const u8 *aData,
213813 int nData
213814){
213815 Fts5PageWriter *pPage = &pWriter->writer;
213816 const u8 *a = aData;
213817 int n = nData;
213818
213819 assert( p->pConfig->pgsz>0 )((void) (0));
213820 while( p->rc==SQLITE_OK0
213821 && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
213822 ){
213823 int nReq = p->pConfig->pgsz - pPage->buf.n - pPage->pgidx.n;
213824 int nCopy = 0;
213825 while( nCopy<nReq ){
213826 i64 dummy;
213827 nCopy += fts5GetVarint(&a[nCopy], (u64*)&dummy);
213828 }
213829 fts5BufferAppendBlob(&p->rc, &pPage->buf, nCopy, a);
213830 a += nCopy;
213831 n -= nCopy;
213832 fts5WriteFlushLeaf(p, pWriter);
213833 }
213834 if( n>0 ){
213835 fts5BufferAppendBlob(&p->rc, &pPage->buf, n, a);
213836 }
213837}
213838
213839/*
213840** Flush any data cached by the writer object to the database. Free any
213841** allocations associated with the writer.
213842*/
213843static void fts5WriteFinish(
213844 Fts5Index *p,
213845 Fts5SegWriter *pWriter, /* Writer object */
213846 int *pnLeaf /* OUT: Number of leaf pages in b-tree */
213847){
213848 int i;
213849 Fts5PageWriter *pLeaf = &pWriter->writer;
213850 if( p->rc==SQLITE_OK0 ){
213851 assert( pLeaf->pgno>=1 )((void) (0));
213852 if( pLeaf->buf.n>4 ){
213853 fts5WriteFlushLeaf(p, pWriter);
213854 }
213855 *pnLeaf = pLeaf->pgno-1;
213856 if( pLeaf->pgno>1 ){
213857 fts5WriteFlushBtree(p, pWriter);
213858 }
213859 }
213860 fts5BufferFree(&pLeaf->term);
213861 fts5BufferFree(&pLeaf->buf);
213862 fts5BufferFree(&pLeaf->pgidx);
213863 fts5BufferFree(&pWriter->btterm);
213864
213865 for(i=0; i<pWriter->nDlidx; i++){
213866 sqlite3Fts5BufferFree(&pWriter->aDlidx[i].buf);
213867 }
213868 sqlite3_free(pWriter->aDlidx);
213869}
213870
213871static void fts5WriteInit(
213872 Fts5Index *p,
213873 Fts5SegWriter *pWriter,
213874 int iSegid
213875){
213876 const int nBuffer = p->pConfig->pgsz + FTS5_DATA_PADDING;
213877
213878 memset(pWriter, 0, sizeof(Fts5SegWriter));
213879 pWriter->iSegid = iSegid;
213880
213881 fts5WriteDlidxGrow(p, pWriter, 1);
213882 pWriter->writer.pgno = 1;
213883 pWriter->bFirstTermInPage = 1;
213884 pWriter->iBtPage = 1;
213885
213886 assert( pWriter->writer.buf.n==0 )((void) (0));
213887 assert( pWriter->writer.pgidx.n==0 )((void) (0));
213888
213889 /* Grow the two buffers to pgsz + padding bytes in size. */
213890 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.pgidx, nBuffer);
213891 sqlite3Fts5BufferSize(&p->rc, &pWriter->writer.buf, nBuffer);
213892
213893 if( p->pIdxWriter==0 ){
213894 Fts5Config *pConfig = p->pConfig;
213895 fts5IndexPrepareStmt(p, &p->pIdxWriter, sqlite3_mprintf(
213896 "INSERT INTO '%q'.'%q_idx'(segid,term,pgno) VALUES(?,?,?)",
213897 pConfig->zDb, pConfig->zName
213898 ));
213899 }
213900
213901 if( p->rc==SQLITE_OK0 ){
213902 /* Initialize the 4-byte leaf-page header to 0x00. */
213903 memset(pWriter->writer.buf.p, 0, 4);
213904 pWriter->writer.buf.n = 4;
213905
213906 /* Bind the current output segment id to the index-writer. This is an
213907 ** optimization over binding the same value over and over as rows are
213908 ** inserted into %_idx by the current writer. */
213909 sqlite3_bind_int(p->pIdxWriter, 1, pWriter->iSegid);
213910 }
213911}
213912
213913/*
213914** Iterator pIter was used to iterate through the input segments of on an
213915** incremental merge operation. This function is called if the incremental
213916** merge step has finished but the input has not been completely exhausted.
213917*/
213918static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
213919 int i;
213920 Fts5Buffer buf;
213921 memset(&buf, 0, sizeof(Fts5Buffer));
213922 for(i=0; i<pIter->nSeg && p->rc==SQLITE_OK0; i++){
213923 Fts5SegIter *pSeg = &pIter->aSeg[i];
213924 if( pSeg->pSeg==0 ){
213925 /* no-op */
213926 }else if( pSeg->pLeaf==0 ){
213927 /* All keys from this input segment have been transfered to the output.
213928 ** Set both the first and last page-numbers to 0 to indicate that the
213929 ** segment is now empty. */
213930 pSeg->pSeg->pgnoLast = 0;
213931 pSeg->pSeg->pgnoFirst = 0;
213932 }else{
213933 int iOff = pSeg->iTermLeafOffset; /* Offset on new first leaf page */
213934 i64 iLeafRowid;
213935 Fts5Data *pData;
213936 int iId = pSeg->pSeg->iSegid;
213937 u8 aHdr[4] = {0x00, 0x00, 0x00, 0x00};
213938
213939 iLeafRowid = FTS5_SEGMENT_ROWID(iId, pSeg->iTermLeafPgno);
213940 pData = fts5LeafRead(p, iLeafRowid);
213941 if( pData ){
213942 if( iOff>pData->szLeaf ){
213943 /* This can occur if the pages that the segments occupy overlap - if
213944 ** a single page has been assigned to more than one segment. In
213945 ** this case a prior iteration of this loop may have corrupted the
213946 ** segment currently being trimmed. */
213947 p->rc = FTS5_CORRUPT;
213948 }else{
213949 fts5BufferZero(&buf);
213950 fts5BufferGrow(&p->rc, &buf, pData->nn);
213951 fts5BufferAppendBlob(&p->rc, &buf, sizeof(aHdr), aHdr);
213952 fts5BufferAppendVarint(&p->rc, &buf, pSeg->term.n);
213953 fts5BufferAppendBlob(&p->rc, &buf, pSeg->term.n, pSeg->term.p);
213954 fts5BufferAppendBlob(&p->rc, &buf, pData->szLeaf-iOff,&pData->p[iOff]);
213955 if( p->rc==SQLITE_OK0 ){
213956 /* Set the szLeaf field */
213957 fts5PutU16(&buf.p[2], (u16)buf.n);
213958 }
213959
213960 /* Set up the new page-index array */
213961 fts5BufferAppendVarint(&p->rc, &buf, 4);
213962 if( pSeg->iLeafPgno==pSeg->iTermLeafPgno
213963 && pSeg->iEndofDoclist<pData->szLeaf
213964 && pSeg->iPgidxOff<=pData->nn
213965 ){
213966 int nDiff = pData->szLeaf - pSeg->iEndofDoclist;
213967 fts5BufferAppendVarint(&p->rc, &buf, buf.n - 1 - nDiff - 4);
213968 fts5BufferAppendBlob(&p->rc, &buf,
213969 pData->nn - pSeg->iPgidxOff, &pData->p[pSeg->iPgidxOff]
213970 );
213971 }
213972
213973 pSeg->pSeg->pgnoFirst = pSeg->iTermLeafPgno;
213974 fts5DataDelete(p, FTS5_SEGMENT_ROWID(iId, 1), iLeafRowid);
213975 fts5DataWrite(p, iLeafRowid, buf.p, buf.n);
213976 }
213977 fts5DataRelease(pData);
213978 }
213979 }
213980 }
213981 fts5BufferFree(&buf);
213982}
213983
213984static void fts5MergeChunkCallback(
213985 Fts5Index *p,
213986 void *pCtx,
213987 const u8 *pChunk, int nChunk
213988){
213989 Fts5SegWriter *pWriter = (Fts5SegWriter*)pCtx;
213990 fts5WriteAppendPoslistData(p, pWriter, pChunk, nChunk);
213991}
213992
213993/*
213994**
213995*/
213996static void fts5IndexMergeLevel(
213997 Fts5Index *p, /* FTS5 backend object */
213998 Fts5Structure **ppStruct, /* IN/OUT: Stucture of index */
213999 int iLvl, /* Level to read input from */
214000 int *pnRem /* Write up to this many output leaves */
214001){
214002 Fts5Structure *pStruct = *ppStruct;
214003 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
214004 Fts5StructureLevel *pLvlOut;
214005 Fts5Iter *pIter = 0; /* Iterator to read input data */
214006 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
214007 int nInput; /* Number of input segments */
214008 Fts5SegWriter writer; /* Writer object */
214009 Fts5StructureSegment *pSeg; /* Output segment */
214010 Fts5Buffer term;
214011 int bOldest; /* True if the output segment is the oldest */
214012 int eDetail = p->pConfig->eDetail;
214013 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
214014 int bTermWritten = 0; /* True if current term already output */
214015
214016 assert( iLvl<pStruct->nLevel )((void) (0));
214017 assert( pLvl->nMerge<=pLvl->nSeg )((void) (0));
214018
214019 memset(&writer, 0, sizeof(Fts5SegWriter));
214020 memset(&term, 0, sizeof(Fts5Buffer));
214021 if( pLvl->nMerge ){
214022 pLvlOut = &pStruct->aLevel[iLvl+1];
214023 assert( pLvlOut->nSeg>0 )((void) (0));
214024 nInput = pLvl->nMerge;
214025 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg-1];
214026
214027 fts5WriteInit(p, &writer, pSeg->iSegid);
214028 writer.writer.pgno = pSeg->pgnoLast+1;
214029 writer.iBtPage = 0;
214030 }else{
214031 int iSegid = fts5AllocateSegid(p, pStruct);
214032
214033 /* Extend the Fts5Structure object as required to ensure the output
214034 ** segment exists. */
214035 if( iLvl==pStruct->nLevel-1 ){
214036 fts5StructureAddLevel(&p->rc, ppStruct);
214037 pStruct = *ppStruct;
214038 }
214039 fts5StructureExtendLevel(&p->rc, pStruct, iLvl+1, 1, 0);
214040 if( p->rc ) return;
214041 pLvl = &pStruct->aLevel[iLvl];
214042 pLvlOut = &pStruct->aLevel[iLvl+1];
214043
214044 fts5WriteInit(p, &writer, iSegid);
214045
214046 /* Add the new segment to the output level */
214047 pSeg = &pLvlOut->aSeg[pLvlOut->nSeg];
214048 pLvlOut->nSeg++;
214049 pSeg->pgnoFirst = 1;
214050 pSeg->iSegid = iSegid;
214051 pStruct->nSegment++;
214052
214053 /* Read input from all segments in the input level */
214054 nInput = pLvl->nSeg;
214055 }
214056 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
214057
214058 assert( iLvl>=0 )((void) (0));
214059 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
214060 fts5MultiIterEof(p, pIter)==0;
214061 fts5MultiIterNext(p, pIter, 0, 0)
214062 ){
214063 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
214064 int nPos; /* position-list size field value */
214065 int nTerm;
214066 const u8 *pTerm;
214067
214068 pTerm = fts5MultiIterTerm(pIter, &nTerm);
214069 if( nTerm!=term.n || fts5Memcmp(pTerm, term.p, nTerm) ){
214070 if( pnRem && writer.nLeafWritten>nRem ){
214071 break;
214072 }
214073 fts5BufferSet(&p->rc, &term, nTerm, pTerm);
214074 bTermWritten =0;
214075 }
214076
214077 /* Check for key annihilation. */
214078 if( pSegIter->nPos==0 && (bOldest || pSegIter->bDel==0) ) continue;
214079
214080 if( p->rc==SQLITE_OK0 && bTermWritten==0 ){
214081 /* This is a new term. Append a term to the output segment. */
214082 fts5WriteAppendTerm(p, &writer, nTerm, pTerm);
214083 bTermWritten = 1;
214084 }
214085
214086 /* Append the rowid to the output */
214087 /* WRITEPOSLISTSIZE */
214088 fts5WriteAppendRowid(p, &writer, fts5MultiIterRowid(pIter));
214089
214090 if( eDetail==FTS5_DETAIL_NONE ){
214091 if( pSegIter->bDel ){
214092 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
214093 if( pSegIter->nPos>0 ){
214094 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, 0);
214095 }
214096 }
214097 }else{
214098 /* Append the position-list data to the output */
214099 nPos = pSegIter->nPos*2 + pSegIter->bDel;
214100 fts5BufferAppendVarint(&p->rc, &writer.writer.buf, nPos);
214101 fts5ChunkIterate(p, pSegIter, (void*)&writer, fts5MergeChunkCallback);
214102 }
214103 }
214104
214105 /* Flush the last leaf page to disk. Set the output segment b-tree height
214106 ** and last leaf page number at the same time. */
214107 fts5WriteFinish(p, &writer, &pSeg->pgnoLast);
214108
214109 if( fts5MultiIterEof(p, pIter) ){
214110 int i;
214111
214112 /* Remove the redundant segments from the %_data table */
214113 for(i=0; i<nInput; i++){
214114 fts5DataRemoveSegment(p, pLvl->aSeg[i].iSegid);
214115 }
214116
214117 /* Remove the redundant segments from the input level */
214118 if( pLvl->nSeg!=nInput ){
214119 int nMove = (pLvl->nSeg - nInput) * sizeof(Fts5StructureSegment);
214120 memmove(pLvl->aSeg, &pLvl->aSeg[nInput], nMove);
214121 }
214122 pStruct->nSegment -= nInput;
214123 pLvl->nSeg -= nInput;
214124 pLvl->nMerge = 0;
214125 if( pSeg->pgnoLast==0 ){
214126 pLvlOut->nSeg--;
214127 pStruct->nSegment--;
214128 }
214129 }else{
214130 assert( pSeg->pgnoLast>0 )((void) (0));
214131 fts5TrimSegments(p, pIter);
214132 pLvl->nMerge = nInput;
214133 }
214134
214135 fts5MultiIterFree(pIter);
214136 fts5BufferFree(&term);
214137 if( pnRem ) *pnRem -= writer.nLeafWritten;
214138}
214139
214140/*
214141** Do up to nPg pages of automerge work on the index.
214142**
214143** Return true if any changes were actually made, or false otherwise.
214144*/
214145static int fts5IndexMerge(
214146 Fts5Index *p, /* FTS5 backend object */
214147 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
214148 int nPg, /* Pages of work to do */
214149 int nMin /* Minimum number of segments to merge */
214150){
214151 int nRem = nPg;
214152 int bRet = 0;
214153 Fts5Structure *pStruct = *ppStruct;
214154 while( nRem>0 && p->rc==SQLITE_OK0 ){
214155 int iLvl; /* To iterate through levels */
214156 int iBestLvl = 0; /* Level offering the most input segments */
214157 int nBest = 0; /* Number of input segments on best level */
214158
214159 /* Set iBestLvl to the level to read input segments from. */
214160 assert( pStruct->nLevel>0 )((void) (0));
214161 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
214162 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
214163 if( pLvl->nMerge ){
214164 if( pLvl->nMerge>nBest ){
214165 iBestLvl = iLvl;
214166 nBest = pLvl->nMerge;
214167 }
214168 break;
214169 }
214170 if( pLvl->nSeg>nBest ){
214171 nBest = pLvl->nSeg;
214172 iBestLvl = iLvl;
214173 }
214174 }
214175
214176 /* If nBest is still 0, then the index must be empty. */
214177#ifdef SQLITE_DEBUG
214178 for(iLvl=0; nBest==0 && iLvl<pStruct->nLevel; iLvl++){
214179 assert( pStruct->aLevel[iLvl].nSeg==0 )((void) (0));
214180 }
214181#endif
214182
214183 if( nBest<nMin && pStruct->aLevel[iBestLvl].nMerge==0 ){
214184 break;
214185 }
214186 bRet = 1;
214187 fts5IndexMergeLevel(p, &pStruct, iBestLvl, &nRem);
214188 if( p->rc==SQLITE_OK0 && pStruct->aLevel[iBestLvl].nMerge==0 ){
214189 fts5StructurePromote(p, iBestLvl+1, pStruct);
214190 }
214191 }
214192 *ppStruct = pStruct;
214193 return bRet;
214194}
214195
214196/*
214197** A total of nLeaf leaf pages of data has just been flushed to a level-0
214198** segment. This function updates the write-counter accordingly and, if
214199** necessary, performs incremental merge work.
214200**
214201** If an error occurs, set the Fts5Index.rc error code. If an error has
214202** already occurred, this function is a no-op.
214203*/
214204static void fts5IndexAutomerge(
214205 Fts5Index *p, /* FTS5 backend object */
214206 Fts5Structure **ppStruct, /* IN/OUT: Current structure of index */
214207 int nLeaf /* Number of output leaves just written */
214208){
214209 if( p->rc==SQLITE_OK0 && p->pConfig->nAutomerge>0 ){
214210 Fts5Structure *pStruct = *ppStruct;
214211 u64 nWrite; /* Initial value of write-counter */
214212 int nWork; /* Number of work-quanta to perform */
214213 int nRem; /* Number of leaf pages left to write */
214214
214215 /* Update the write-counter. While doing so, set nWork. */
214216 nWrite = pStruct->nWriteCounter;
214217 nWork = (int)(((nWrite + nLeaf) / p->nWorkUnit) - (nWrite / p->nWorkUnit));
214218 pStruct->nWriteCounter += nLeaf;
214219 nRem = (int)(p->nWorkUnit * nWork * pStruct->nLevel);
214220
214221 fts5IndexMerge(p, ppStruct, nRem, p->pConfig->nAutomerge);
214222 }
214223}
214224
214225static void fts5IndexCrisismerge(
214226 Fts5Index *p, /* FTS5 backend object */
214227 Fts5Structure **ppStruct /* IN/OUT: Current structure of index */
214228){
214229 const int nCrisis = p->pConfig->nCrisisMerge;
214230 Fts5Structure *pStruct = *ppStruct;
214231 int iLvl = 0;
214232
214233 assert( p->rc!=SQLITE_OK || pStruct->nLevel>0 )((void) (0));
214234 while( p->rc==SQLITE_OK0 && pStruct->aLevel[iLvl].nSeg>=nCrisis ){
214235 fts5IndexMergeLevel(p, &pStruct, iLvl, 0);
214236 assert( p->rc!=SQLITE_OK || pStruct->nLevel>(iLvl+1) )((void) (0));
214237 fts5StructurePromote(p, iLvl+1, pStruct);
214238 iLvl++;
214239 }
214240 *ppStruct = pStruct;
214241}
214242
214243static int fts5IndexReturn(Fts5Index *p){
214244 int rc = p->rc;
214245 p->rc = SQLITE_OK0;
214246 return rc;
214247}
214248
214249typedef struct Fts5FlushCtx Fts5FlushCtx;
214250struct Fts5FlushCtx {
214251 Fts5Index *pIdx;
214252 Fts5SegWriter writer;
214253};
214254
214255/*
214256** Buffer aBuf[] contains a list of varints, all small enough to fit
214257** in a 32-bit integer. Return the size of the largest prefix of this
214258** list nMax bytes or less in size.
214259*/
214260static int fts5PoslistPrefix(const u8 *aBuf, int nMax){
214261 int ret;
214262 u32 dummy;
214263 ret = fts5GetVarint32(aBuf, dummy);
214264 if( ret<nMax ){
214265 while( 1 ){
214266 int i = fts5GetVarint32(&aBuf[ret], dummy);
214267 if( (ret + i) > nMax ) break;
214268 ret += i;
214269 }
214270 }
214271 return ret;
214272}
214273
214274/*
214275** Flush the contents of in-memory hash table iHash to a new level-0
214276** segment on disk. Also update the corresponding structure record.
214277**
214278** If an error occurs, set the Fts5Index.rc error code. If an error has
214279** already occurred, this function is a no-op.
214280*/
214281static void fts5FlushOneHash(Fts5Index *p){
214282 Fts5Hash *pHash = p->pHash;
214283 Fts5Structure *pStruct;
214284 int iSegid;
214285 int pgnoLast = 0; /* Last leaf page number in segment */
214286
214287 /* Obtain a reference to the index structure and allocate a new segment-id
214288 ** for the new level-0 segment. */
214289 pStruct = fts5StructureRead(p);
214290 iSegid = fts5AllocateSegid(p, pStruct);
214291 fts5StructureInvalidate(p);
214292
214293 if( iSegid ){
214294 const int pgsz = p->pConfig->pgsz;
214295 int eDetail = p->pConfig->eDetail;
214296 Fts5StructureSegment *pSeg; /* New segment within pStruct */
214297 Fts5Buffer *pBuf; /* Buffer in which to assemble leaf page */
214298 Fts5Buffer *pPgidx; /* Buffer in which to assemble pgidx */
214299
214300 Fts5SegWriter writer;
214301 fts5WriteInit(p, &writer, iSegid);
214302
214303 pBuf = &writer.writer.buf;
214304 pPgidx = &writer.writer.pgidx;
214305
214306 /* fts5WriteInit() should have initialized the buffers to (most likely)
214307 ** the maximum space required. */
214308 assert( p->rc || pBuf->nSpace>=(pgsz + FTS5_DATA_PADDING) )((void) (0));
214309 assert( p->rc || pPgidx->nSpace>=(pgsz + FTS5_DATA_PADDING) )((void) (0));
214310
214311 /* Begin scanning through hash table entries. This loop runs once for each
214312 ** term/doclist currently stored within the hash table. */
214313 if( p->rc==SQLITE_OK0 ){
214314 p->rc = sqlite3Fts5HashScanInit(pHash, 0, 0);
214315 }
214316 while( p->rc==SQLITE_OK0 && 0==sqlite3Fts5HashScanEof(pHash) ){
214317 const char *zTerm; /* Buffer containing term */
214318 const u8 *pDoclist; /* Pointer to doclist for this term */
214319 int nDoclist; /* Size of doclist in bytes */
214320
214321 /* Write the term for this entry to disk. */
214322 sqlite3Fts5HashScanEntry(pHash, &zTerm, &pDoclist, &nDoclist);
214323 fts5WriteAppendTerm(p, &writer, (int)strlen(zTerm), (const u8*)zTerm);
214324 if( p->rc!=SQLITE_OK0 ) break;
214325
214326 assert( writer.bFirstRowidInPage==0 )((void) (0));
214327 if( pgsz>=(pBuf->n + pPgidx->n + nDoclist + 1) ){
214328 /* The entire doclist will fit on the current leaf. */
214329 fts5BufferSafeAppendBlob(pBuf, pDoclist, nDoclist);
214330 }else{
214331 i64 iRowid = 0;
214332 i64 iDelta = 0;
214333 int iOff = 0;
214334
214335 /* The entire doclist will not fit on this leaf. The following
214336 ** loop iterates through the poslists that make up the current
214337 ** doclist. */
214338 while( p->rc==SQLITE_OK0 && iOff<nDoclist ){
214339 iOff += fts5GetVarint(&pDoclist[iOff], (u64*)&iDelta);
214340 iRowid += iDelta;
214341
214342 if( writer.bFirstRowidInPage ){
214343 fts5PutU16(&pBuf->p[0], (u16)pBuf->n); /* first rowid on page */
214344 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iRowid);
214345 writer.bFirstRowidInPage = 0;
214346 fts5WriteDlidxAppend(p, &writer, iRowid);
214347 if( p->rc!=SQLITE_OK0 ) break;
214348 }else{
214349 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iDelta);
214350 }
214351 assert( pBuf->n<=pBuf->nSpace )((void) (0));
214352
214353 if( eDetail==FTS5_DETAIL_NONE ){
214354 if( iOff<nDoclist && pDoclist[iOff]==0 ){
214355 pBuf->p[pBuf->n++] = 0;
214356 iOff++;
214357 if( iOff<nDoclist && pDoclist[iOff]==0 ){
214358 pBuf->p[pBuf->n++] = 0;
214359 iOff++;
214360 }
214361 }
214362 if( (pBuf->n + pPgidx->n)>=pgsz ){
214363 fts5WriteFlushLeaf(p, &writer);
214364 }
214365 }else{
214366 int bDummy;
214367 int nPos;
214368 int nCopy = fts5GetPoslistSize(&pDoclist[iOff], &nPos, &bDummy);
214369 nCopy += nPos;
214370 if( (pBuf->n + pPgidx->n + nCopy) <= pgsz ){
214371 /* The entire poslist will fit on the current leaf. So copy
214372 ** it in one go. */
214373 fts5BufferSafeAppendBlob(pBuf, &pDoclist[iOff], nCopy);
214374 }else{
214375 /* The entire poslist will not fit on this leaf. So it needs
214376 ** to be broken into sections. The only qualification being
214377 ** that each varint must be stored contiguously. */
214378 const u8 *pPoslist = &pDoclist[iOff];
214379 int iPos = 0;
214380 while( p->rc==SQLITE_OK0 ){
214381 int nSpace = pgsz - pBuf->n - pPgidx->n;
214382 int n = 0;
214383 if( (nCopy - iPos)<=nSpace ){
214384 n = nCopy - iPos;
214385 }else{
214386 n = fts5PoslistPrefix(&pPoslist[iPos], nSpace);
214387 }
214388 assert( n>0 )((void) (0));
214389 fts5BufferSafeAppendBlob(pBuf, &pPoslist[iPos], n);
214390 iPos += n;
214391 if( (pBuf->n + pPgidx->n)>=pgsz ){
214392 fts5WriteFlushLeaf(p, &writer);
214393 }
214394 if( iPos>=nCopy ) break;
214395 }
214396 }
214397 iOff += nCopy;
214398 }
214399 }
214400 }
214401
214402 /* TODO2: Doclist terminator written here. */
214403 /* pBuf->p[pBuf->n++] = '\0'; */
214404 assert( pBuf->n<=pBuf->nSpace )((void) (0));
214405 if( p->rc==SQLITE_OK0 ) sqlite3Fts5HashScanNext(pHash);
214406 }
214407 sqlite3Fts5HashClear(pHash);
214408 fts5WriteFinish(p, &writer, &pgnoLast);
214409
214410 /* Update the Fts5Structure. It is written back to the database by the
214411 ** fts5StructureRelease() call below. */
214412 if( pStruct->nLevel==0 ){
214413 fts5StructureAddLevel(&p->rc, &pStruct);
214414 }
214415 fts5StructureExtendLevel(&p->rc, pStruct, 0, 1, 0);
214416 if( p->rc==SQLITE_OK0 ){
214417 pSeg = &pStruct->aLevel[0].aSeg[ pStruct->aLevel[0].nSeg++ ];
214418 pSeg->iSegid = iSegid;
214419 pSeg->pgnoFirst = 1;
214420 pSeg->pgnoLast = pgnoLast;
214421 pStruct->nSegment++;
214422 }
214423 fts5StructurePromote(p, 0, pStruct);
214424 }
214425
214426 fts5IndexAutomerge(p, &pStruct, pgnoLast);
214427 fts5IndexCrisismerge(p, &pStruct);
214428 fts5StructureWrite(p, pStruct);
214429 fts5StructureRelease(pStruct);
214430}
214431
214432/*
214433** Flush any data stored in the in-memory hash tables to the database.
214434*/
214435static void fts5IndexFlush(Fts5Index *p){
214436 /* Unless it is empty, flush the hash table to disk */
214437 if( p->nPendingData ){
214438 assert( p->pHash )((void) (0));
214439 p->nPendingData = 0;
214440 fts5FlushOneHash(p);
214441 }
214442}
214443
214444static Fts5Structure *fts5IndexOptimizeStruct(
214445 Fts5Index *p,
214446 Fts5Structure *pStruct
214447){
214448 Fts5Structure *pNew = 0;
214449 sqlite3_int64 nByte = sizeof(Fts5Structure);
214450 int nSeg = pStruct->nSegment;
214451 int i;
214452
214453 /* Figure out if this structure requires optimization. A structure does
214454 ** not require optimization if either:
214455 **
214456 ** + it consists of fewer than two segments, or
214457 ** + all segments are on the same level, or
214458 ** + all segments except one are currently inputs to a merge operation.
214459 **
214460 ** In the first case, return NULL. In the second, increment the ref-count
214461 ** on *pStruct and return a copy of the pointer to it.
214462 */
214463 if( nSeg<2 ) return 0;
214464 for(i=0; i<pStruct->nLevel; i++){
214465 int nThis = pStruct->aLevel[i].nSeg;
214466 if( nThis==nSeg || (nThis==nSeg-1 && pStruct->aLevel[i].nMerge==nThis) ){
214467 fts5StructureRef(pStruct);
214468 return pStruct;
214469 }
214470 assert( pStruct->aLevel[i].nMerge<=nThis )((void) (0));
214471 }
214472
214473 nByte += (pStruct->nLevel+1) * sizeof(Fts5StructureLevel);
214474 pNew = (Fts5Structure*)sqlite3Fts5MallocZero(&p->rc, nByte);
214475
214476 if( pNew ){
214477 Fts5StructureLevel *pLvl;
214478 nByte = nSeg * sizeof(Fts5StructureSegment);
214479 pNew->nLevel = pStruct->nLevel+1;
214480 pNew->nRef = 1;
214481 pNew->nWriteCounter = pStruct->nWriteCounter;
214482 pLvl = &pNew->aLevel[pStruct->nLevel];
214483 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&p->rc, nByte);
214484 if( pLvl->aSeg ){
214485 int iLvl, iSeg;
214486 int iSegOut = 0;
214487 /* Iterate through all segments, from oldest to newest. Add them to
214488 ** the new Fts5Level object so that pLvl->aSeg[0] is the oldest
214489 ** segment in the data structure. */
214490 for(iLvl=pStruct->nLevel-1; iLvl>=0; iLvl--){
214491 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
214492 pLvl->aSeg[iSegOut] = pStruct->aLevel[iLvl].aSeg[iSeg];
214493 iSegOut++;
214494 }
214495 }
214496 pNew->nSegment = pLvl->nSeg = nSeg;
214497 }else{
214498 sqlite3_free(pNew);
214499 pNew = 0;
214500 }
214501 }
214502
214503 return pNew;
214504}
214505
214506static int sqlite3Fts5IndexOptimize(Fts5Index *p){
214507 Fts5Structure *pStruct;
214508 Fts5Structure *pNew = 0;
214509
214510 assert( p->rc==SQLITE_OK )((void) (0));
214511 fts5IndexFlush(p);
214512 pStruct = fts5StructureRead(p);
214513 fts5StructureInvalidate(p);
214514
214515 if( pStruct ){
214516 pNew = fts5IndexOptimizeStruct(p, pStruct);
214517 }
214518 fts5StructureRelease(pStruct);
214519
214520 assert( pNew==0 || pNew->nSegment>0 )((void) (0));
214521 if( pNew ){
214522 int iLvl;
214523 for(iLvl=0; pNew->aLevel[iLvl].nSeg==0; iLvl++){}
214524 while( p->rc==SQLITE_OK0 && pNew->aLevel[iLvl].nSeg>0 ){
214525 int nRem = FTS5_OPT_WORK_UNIT;
214526 fts5IndexMergeLevel(p, &pNew, iLvl, &nRem);
214527 }
214528
214529 fts5StructureWrite(p, pNew);
214530 fts5StructureRelease(pNew);
214531 }
214532
214533 return fts5IndexReturn(p);
214534}
214535
214536/*
214537** This is called to implement the special "VALUES('merge', $nMerge)"
214538** INSERT command.
214539*/
214540static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge){
214541 Fts5Structure *pStruct = fts5StructureRead(p);
214542 if( pStruct ){
214543 int nMin = p->pConfig->nUsermerge;
214544 fts5StructureInvalidate(p);
214545 if( nMerge<0 ){
214546 Fts5Structure *pNew = fts5IndexOptimizeStruct(p, pStruct);
214547 fts5StructureRelease(pStruct);
214548 pStruct = pNew;
214549 nMin = 2;
214550 nMerge = nMerge*-1;
214551 }
214552 if( pStruct && pStruct->nLevel ){
214553 if( fts5IndexMerge(p, &pStruct, nMerge, nMin) ){
214554 fts5StructureWrite(p, pStruct);
214555 }
214556 }
214557 fts5StructureRelease(pStruct);
214558 }
214559 return fts5IndexReturn(p);
214560}
214561
214562static void fts5AppendRowid(
214563 Fts5Index *p,
214564 i64 iDelta,
214565 Fts5Iter *pUnused,
214566 Fts5Buffer *pBuf
214567){
214568 UNUSED_PARAM(pUnused);
214569 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
214570}
214571
214572static void fts5AppendPoslist(
214573 Fts5Index *p,
214574 i64 iDelta,
214575 Fts5Iter *pMulti,
214576 Fts5Buffer *pBuf
214577){
214578 int nData = pMulti->base.nData;
214579 int nByte = nData + 9 + 9 + FTS5_DATA_ZERO_PADDING;
214580 assert( nData>0 )((void) (0));
214581 if( p->rc==SQLITE_OK0 && 0==fts5BufferGrow(&p->rc, pBuf, nByte) ){
214582 fts5BufferSafeAppendVarint(pBuf, iDelta);
214583 fts5BufferSafeAppendVarint(pBuf, nData*2);
214584 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
214585 memset(&pBuf->p[pBuf->n], 0, FTS5_DATA_ZERO_PADDING);
214586 }
214587}
214588
214589
214590static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
214591 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
214592
214593 assert( pIter->aPoslist )((void) (0));
214594 if( p>=pIter->aEof ){
214595 pIter->aPoslist = 0;
214596 }else{
214597 i64 iDelta;
214598
214599 p += fts5GetVarint(p, (u64*)&iDelta);
214600 pIter->iRowid += iDelta;
214601
214602 /* Read position list size */
214603 if( p[0] & 0x80 ){
214604 int nPos;
214605 pIter->nSize = fts5GetVarint32(p, nPos);
214606 pIter->nPoslist = (nPos>>1);
214607 }else{
214608 pIter->nPoslist = ((int)(p[0])) >> 1;
214609 pIter->nSize = 1;
214610 }
214611
214612 pIter->aPoslist = p;
214613 }
214614}
214615
214616static void fts5DoclistIterInit(
214617 Fts5Buffer *pBuf,
214618 Fts5DoclistIter *pIter
214619){
214620 memset(pIter, 0, sizeof(*pIter));
214621 pIter->aPoslist = pBuf->p;
214622 pIter->aEof = &pBuf->p[pBuf->n];
214623 fts5DoclistIterNext(pIter);
214624}
214625
214626#if 0
214627/*
214628** Append a doclist to buffer pBuf.
214629**
214630** This function assumes that space within the buffer has already been
214631** allocated.
214632*/
214633static void fts5MergeAppendDocid(
214634 Fts5Buffer *pBuf, /* Buffer to write to */
214635 i64 *piLastRowid, /* IN/OUT: Previous rowid written (if any) */
214636 i64 iRowid /* Rowid to append */
214637){
214638 assert( pBuf->n!=0 || (*piLastRowid)==0 )((void) (0));
214639 fts5BufferSafeAppendVarint(pBuf, iRowid - *piLastRowid);
214640 *piLastRowid = iRowid;
214641}
214642#endif
214643
214644#define fts5MergeAppendDocid(pBuf, iLastRowid, iRowid) { \
214645 assert( (pBuf)->n!=0 || (iLastRowid)==0 )((void) (0)); \
214646 fts5BufferSafeAppendVarint((pBuf), (iRowid) - (iLastRowid)); \
214647 (iLastRowid) = (iRowid); \
214648}
214649
214650/*
214651** Swap the contents of buffer *p1 with that of *p2.
214652*/
214653static void fts5BufferSwap(Fts5Buffer *p1, Fts5Buffer *p2){
214654 Fts5Buffer tmp = *p1;
214655 *p1 = *p2;
214656 *p2 = tmp;
214657}
214658
214659static void fts5NextRowid(Fts5Buffer *pBuf, int *piOff, i64 *piRowid){
214660 int i = *piOff;
214661 if( i>=pBuf->n ){
214662 *piOff = -1;
214663 }else{
214664 u64 iVal;
214665 *piOff = i + sqlite3Fts5GetVarint(&pBuf->p[i], &iVal);
214666 *piRowid += iVal;
214667 }
214668}
214669
214670/*
214671** This is the equivalent of fts5MergePrefixLists() for detail=none mode.
214672** In this case the buffers consist of a delta-encoded list of rowids only.
214673*/
214674static void fts5MergeRowidLists(
214675 Fts5Index *p, /* FTS5 backend object */
214676 Fts5Buffer *p1, /* First list to merge */
214677 Fts5Buffer *p2 /* Second list to merge */
214678){
214679 int i1 = 0;
214680 int i2 = 0;
214681 i64 iRowid1 = 0;
214682 i64 iRowid2 = 0;
214683 i64 iOut = 0;
214684
214685 Fts5Buffer out;
214686 memset(&out, 0, sizeof(out));
214687 sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
214688 if( p->rc ) return;
214689
214690 fts5NextRowid(p1, &i1, &iRowid1);
214691 fts5NextRowid(p2, &i2, &iRowid2);
214692 while( i1>=0 || i2>=0 ){
214693 if( i1>=0 && (i2<0 || iRowid1<iRowid2) ){
214694 assert( iOut==0 || iRowid1>iOut )((void) (0));
214695 fts5BufferSafeAppendVarint(&out, iRowid1 - iOut);
214696 iOut = iRowid1;
214697 fts5NextRowid(p1, &i1, &iRowid1);
214698 }else{
214699 assert( iOut==0 || iRowid2>iOut )((void) (0));
214700 fts5BufferSafeAppendVarint(&out, iRowid2 - iOut);
214701 iOut = iRowid2;
214702 if( i1>=0 && iRowid1==iRowid2 ){
214703 fts5NextRowid(p1, &i1, &iRowid1);
214704 }
214705 fts5NextRowid(p2, &i2, &iRowid2);
214706 }
214707 }
214708
214709 fts5BufferSwap(&out, p1);
214710 fts5BufferFree(&out);
214711}
214712
214713/*
214714** Buffers p1 and p2 contain doclists. This function merges the content
214715** of the two doclists together and sets buffer p1 to the result before
214716** returning.
214717**
214718** If an error occurs, an error code is left in p->rc. If an error has
214719** already occurred, this function is a no-op.
214720*/
214721static void fts5MergePrefixLists(
214722 Fts5Index *p, /* FTS5 backend object */
214723 Fts5Buffer *p1, /* First list to merge */
214724 Fts5Buffer *p2 /* Second list to merge */
214725){
214726 if( p2->n ){
214727 i64 iLastRowid = 0;
214728 Fts5DoclistIter i1;
214729 Fts5DoclistIter i2;
214730 Fts5Buffer out = {0, 0, 0};
214731 Fts5Buffer tmp = {0, 0, 0};
214732
214733 /* The maximum size of the output is equal to the sum of the two
214734 ** input sizes + 1 varint (9 bytes). The extra varint is because if the
214735 ** first rowid in one input is a large negative number, and the first in
214736 ** the other a non-negative number, the delta for the non-negative
214737 ** number will be larger on disk than the literal integer value
214738 ** was.
214739 **
214740 ** Or, if the input position-lists are corrupt, then the output might
214741 ** include up to 2 extra 10-byte positions created by interpreting -1
214742 ** (the value PoslistNext64() uses for EOF) as a position and appending
214743 ** it to the output. This can happen at most once for each input
214744 ** position-list, hence two 10 byte paddings. */
214745 if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n + 9+10+10) ) return;
214746 fts5DoclistIterInit(p1, &i1);
214747 fts5DoclistIterInit(p2, &i2);
214748
214749 while( 1 ){
214750 if( i1.iRowid<i2.iRowid ){
214751 /* Copy entry from i1 */
214752 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
214753 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
214754 fts5DoclistIterNext(&i1);
214755 if( i1.aPoslist==0 ) break;
214756 assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) )((void) (0));
214757 }
214758 else if( i2.iRowid!=i1.iRowid ){
214759 /* Copy entry from i2 */
214760 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
214761 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
214762 fts5DoclistIterNext(&i2);
214763 if( i2.aPoslist==0 ) break;
214764 assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) )((void) (0));
214765 }
214766 else{
214767 /* Merge the two position lists. */
214768 i64 iPos1 = 0;
214769 i64 iPos2 = 0;
214770 int iOff1 = 0;
214771 int iOff2 = 0;
214772 u8 *a1 = &i1.aPoslist[i1.nSize];
214773 u8 *a2 = &i2.aPoslist[i2.nSize];
214774 int nCopy;
214775 u8 *aCopy;
214776
214777 i64 iPrev = 0;
214778 Fts5PoslistWriter writer;
214779 memset(&writer, 0, sizeof(writer));
214780
214781 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
214782 fts5BufferZero(&tmp);
214783 sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist);
214784 if( p->rc ) break;
214785
214786 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
214787 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
214788 assert_nc( iPos1>=0 && iPos2>=0 );
214789
214790 if( iPos1<iPos2 ){
214791 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
214792 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
214793 }else{
214794 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
214795 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
214796 }
214797 if( iPos1>=0 && iPos2>=0 ){
214798 while( 1 ){
214799 if( iPos1<iPos2 ){
214800 if( iPos1!=iPrev ){
214801 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
214802 }
214803 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
214804 if( iPos1<0 ) break;
214805 }else{
214806 assert_nc( iPos2!=iPrev );
214807 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
214808 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
214809 if( iPos2<0 ) break;
214810 }
214811 }
214812 }
214813
214814 if( iPos1>=0 ){
214815 if( iPos1!=iPrev ){
214816 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
214817 }
214818 aCopy = &a1[iOff1];
214819 nCopy = i1.nPoslist - iOff1;
214820 }else{
214821 assert_nc( iPos2>=0 && iPos2!=iPrev );
214822 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
214823 aCopy = &a2[iOff2];
214824 nCopy = i2.nPoslist - iOff2;
214825 }
214826 if( nCopy>0 ){
214827 fts5BufferSafeAppendBlob(&tmp, aCopy, nCopy);
214828 }
214829
214830 /* WRITEPOSLISTSIZE */
214831 fts5BufferSafeAppendVarint(&out, tmp.n * 2);
214832 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
214833 fts5DoclistIterNext(&i1);
214834 fts5DoclistIterNext(&i2);
214835 assert_nc( out.n<=(p1->n+p2->n+9) );
214836 if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
214837 assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) )((void) (0));
214838 }
214839 }
214840
214841 if( i1.aPoslist ){
214842 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
214843 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist);
214844 }
214845 else if( i2.aPoslist ){
214846 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
214847 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist);
214848 }
214849 assert_nc( out.n<=(p1->n+p2->n+9) );
214850
214851 fts5BufferSet(&p->rc, p1, out.n, out.p);
214852 fts5BufferFree(&tmp);
214853 fts5BufferFree(&out);
214854 }
214855}
214856
214857static void fts5SetupPrefixIter(
214858 Fts5Index *p, /* Index to read from */
214859 int bDesc, /* True for "ORDER BY rowid DESC" */
214860 const u8 *pToken, /* Buffer containing prefix to match */
214861 int nToken, /* Size of buffer pToken in bytes */
214862 Fts5Colset *pColset, /* Restrict matches to these columns */
214863 Fts5Iter **ppIter /* OUT: New iterator */
214864){
214865 Fts5Structure *pStruct;
214866 Fts5Buffer *aBuf;
214867 const int nBuf = 32;
214868
214869 void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*);
214870 void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
214871 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
214872 xMerge = fts5MergeRowidLists;
214873 xAppend = fts5AppendRowid;
214874 }else{
214875 xMerge = fts5MergePrefixLists;
214876 xAppend = fts5AppendPoslist;
214877 }
214878
214879 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
214880 pStruct = fts5StructureRead(p);
214881
214882 if( aBuf && pStruct ){
214883 const int flags = FTS5INDEX_QUERY_SCAN
214884 | FTS5INDEX_QUERY_SKIPEMPTY
214885 | FTS5INDEX_QUERY_NOOUTPUT;
214886 int i;
214887 i64 iLastRowid = 0;
214888 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
214889 Fts5Data *pData;
214890 Fts5Buffer doclist;
214891 int bNewTerm = 1;
214892
214893 memset(&doclist, 0, sizeof(doclist));
214894 fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
214895 fts5IterSetOutputCb(&p->rc, p1);
214896 for( /* no-op */ ;
214897 fts5MultiIterEof(p, p1)==0;
214898 fts5MultiIterNext2(p, p1, &bNewTerm)
214899 ){
214900 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
214901 int nTerm = pSeg->term.n;
214902 const u8 *pTerm = pSeg->term.p;
214903 p1->xSetOutputs(p1, pSeg);
214904
214905 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm)((nToken)<(nTerm)?(nToken):(nTerm)))<=0 );
214906 if( bNewTerm ){
214907 if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
214908 }
214909
214910 if( p1->base.nData==0 ) continue;
214911
214912 if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
214913 for(i=0; p->rc==SQLITE_OK0 && doclist.n; i++){
214914 assert( i<nBuf )((void) (0));
214915 if( aBuf[i].n==0 ){
214916 fts5BufferSwap(&doclist, &aBuf[i]);
214917 fts5BufferZero(&doclist);
214918 }else{
214919 xMerge(p, &doclist, &aBuf[i]);
214920 fts5BufferZero(&aBuf[i]);
214921 }
214922 }
214923 iLastRowid = 0;
214924 }
214925
214926 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
214927 iLastRowid = p1->base.iRowid;
214928 }
214929
214930 for(i=0; i<nBuf; i++){
214931 if( p->rc==SQLITE_OK0 ){
214932 xMerge(p, &doclist, &aBuf[i]);
214933 }
214934 fts5BufferFree(&aBuf[i]);
214935 }
214936 fts5MultiIterFree(p1);
214937
214938 pData = fts5IdxMalloc(p, sizeof(Fts5Data)+doclist.n+FTS5_DATA_ZERO_PADDING);
214939 if( pData ){
214940 pData->p = (u8*)&pData[1];
214941 pData->nn = pData->szLeaf = doclist.n;
214942 if( doclist.n ) memcpy(pData->p, doclist.p, doclist.n);
214943 fts5MultiIterNew2(p, pData, bDesc, ppIter);
214944 }
214945 fts5BufferFree(&doclist);
214946 }
214947
214948 fts5StructureRelease(pStruct);
214949 sqlite3_free(aBuf);
214950}
214951
214952
214953/*
214954** Indicate that all subsequent calls to sqlite3Fts5IndexWrite() pertain
214955** to the document with rowid iRowid.
214956*/
214957static int sqlite3Fts5IndexBeginWrite(Fts5Index *p, int bDelete, i64 iRowid){
214958 assert( p->rc==SQLITE_OK )((void) (0));
214959
214960 /* Allocate the hash table if it has not already been allocated */
214961 if( p->pHash==0 ){
214962 p->rc = sqlite3Fts5HashNew(p->pConfig, &p->pHash, &p->nPendingData);
214963 }
214964
214965 /* Flush the hash table to disk if required */
214966 if( iRowid<p->iWriteRowid
214967 || (iRowid==p->iWriteRowid && p->bDelete==0)
214968 || (p->nPendingData > p->pConfig->nHashSize)
214969 ){
214970 fts5IndexFlush(p);
214971 }
214972
214973 p->iWriteRowid = iRowid;
214974 p->bDelete = bDelete;
214975 return fts5IndexReturn(p);
214976}
214977
214978/*
214979** Commit data to disk.
214980*/
214981static int sqlite3Fts5IndexSync(Fts5Index *p){
214982 assert( p->rc==SQLITE_OK )((void) (0));
214983 fts5IndexFlush(p);
214984 fts5CloseReader(p);
214985 return fts5IndexReturn(p);
214986}
214987
214988/*
214989** Discard any data stored in the in-memory hash tables. Do not write it
214990** to the database. Additionally, assume that the contents of the %_data
214991** table may have changed on disk. So any in-memory caches of %_data
214992** records must be invalidated.
214993*/
214994static int sqlite3Fts5IndexRollback(Fts5Index *p){
214995 fts5CloseReader(p);
214996 fts5IndexDiscardData(p);
214997 fts5StructureInvalidate(p);
214998 /* assert( p->rc==SQLITE_OK ); */
214999 return SQLITE_OK0;
215000}
215001
215002/*
215003** The %_data table is completely empty when this function is called. This
215004** function populates it with the initial structure objects for each index,
215005** and the initial version of the "averages" record (a zero-byte blob).
215006*/
215007static int sqlite3Fts5IndexReinit(Fts5Index *p){
215008 Fts5Structure s;
215009 fts5StructureInvalidate(p);
215010 memset(&s, 0, sizeof(Fts5Structure));
215011 fts5DataWrite(p, FTS5_AVERAGES_ROWID, (const u8*)"", 0);
215012 fts5StructureWrite(p, &s);
215013 return fts5IndexReturn(p);
215014}
215015
215016/*
215017** Open a new Fts5Index handle. If the bCreate argument is true, create
215018** and initialize the underlying %_data table.
215019**
215020** If successful, set *pp to point to the new object and return SQLITE_OK.
215021** Otherwise, set *pp to NULL and return an SQLite error code.
215022*/
215023static int sqlite3Fts5IndexOpen(
215024 Fts5Config *pConfig,
215025 int bCreate,
215026 Fts5Index **pp,
215027 char **pzErr
215028){
215029 int rc = SQLITE_OK0;
215030 Fts5Index *p; /* New object */
215031
215032 *pp = p = (Fts5Index*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Index));
215033 if( rc==SQLITE_OK0 ){
215034 p->pConfig = pConfig;
215035 p->nWorkUnit = FTS5_WORK_UNIT;
215036 p->zDataTbl = sqlite3Fts5Mprintf(&rc, "%s_data", pConfig->zName);
215037 if( p->zDataTbl && bCreate ){
215038 rc = sqlite3Fts5CreateTable(
215039 pConfig, "data", "id INTEGER PRIMARY KEY, block BLOB", 0, pzErr
215040 );
215041 if( rc==SQLITE_OK0 ){
215042 rc = sqlite3Fts5CreateTable(pConfig, "idx",
215043 "segid, term, pgno, PRIMARY KEY(segid, term)",
215044 1, pzErr
215045 );
215046 }
215047 if( rc==SQLITE_OK0 ){
215048 rc = sqlite3Fts5IndexReinit(p);
215049 }
215050 }
215051 }
215052
215053 assert( rc!=SQLITE_OK || p->rc==SQLITE_OK )((void) (0));
215054 if( rc ){
215055 sqlite3Fts5IndexClose(p);
215056 *pp = 0;
215057 }
215058 return rc;
215059}
215060
215061/*
215062** Close a handle opened by an earlier call to sqlite3Fts5IndexOpen().
215063*/
215064static int sqlite3Fts5IndexClose(Fts5Index *p){
215065 int rc = SQLITE_OK0;
215066 if( p ){
215067 assert( p->pReader==0 )((void) (0));
215068 fts5StructureInvalidate(p);
215069 sqlite3_finalize(p->pWriter);
215070 sqlite3_finalize(p->pDeleter);
215071 sqlite3_finalize(p->pIdxWriter);
215072 sqlite3_finalize(p->pIdxDeleter);
215073 sqlite3_finalize(p->pIdxSelect);
215074 sqlite3_finalize(p->pDataVersion);
215075 sqlite3Fts5HashFree(p->pHash);
215076 sqlite3_free(p->zDataTbl);
215077 sqlite3_free(p);
215078 }
215079 return rc;
215080}
215081
215082/*
215083** Argument p points to a buffer containing utf-8 text that is n bytes in
215084** size. Return the number of bytes in the nChar character prefix of the
215085** buffer, or 0 if there are less than nChar characters in total.
215086*/
215087static int sqlite3Fts5IndexCharlenToBytelen(
215088 const char *p,
215089 int nByte,
215090 int nChar
215091){
215092 int n = 0;
215093 int i;
215094 for(i=0; i<nChar; i++){
215095 if( n>=nByte ) return 0; /* Input contains fewer than nChar chars */
215096 if( (unsigned char)p[n++]>=0xc0 ){
215097 while( (p[n] & 0xc0)==0x80 ){
215098 n++;
215099 if( n>=nByte ) break;
215100 }
215101 }
215102 }
215103 return n;
215104}
215105
215106/*
215107** pIn is a UTF-8 encoded string, nIn bytes in size. Return the number of
215108** unicode characters in the string.
215109*/
215110static int fts5IndexCharlen(const char *pIn, int nIn){
215111 int nChar = 0;
215112 int i = 0;
215113 while( i<nIn ){
215114 if( (unsigned char)pIn[i++]>=0xc0 ){
215115 while( i<nIn && (pIn[i] & 0xc0)==0x80 ) i++;
215116 }
215117 nChar++;
215118 }
215119 return nChar;
215120}
215121
215122/*
215123** Insert or remove data to or from the index. Each time a document is
215124** added to or removed from the index, this function is called one or more
215125** times.
215126**
215127** For an insert, it must be called once for each token in the new document.
215128** If the operation is a delete, it must be called (at least) once for each
215129** unique token in the document with an iCol value less than zero. The iPos
215130** argument is ignored for a delete.
215131*/
215132static int sqlite3Fts5IndexWrite(
215133 Fts5Index *p, /* Index to write to */
215134 int iCol, /* Column token appears in (-ve -> delete) */
215135 int iPos, /* Position of token within column */
215136 const char *pToken, int nToken /* Token to add or remove to or from index */
215137){
215138 int i; /* Used to iterate through indexes */
215139 int rc = SQLITE_OK0; /* Return code */
215140 Fts5Config *pConfig = p->pConfig;
215141
215142 assert( p->rc==SQLITE_OK )((void) (0));
215143 assert( (iCol<0)==p->bDelete )((void) (0));
215144
215145 /* Add the entry to the main terms index. */
215146 rc = sqlite3Fts5HashWrite(
215147 p->pHash, p->iWriteRowid, iCol, iPos, FTS5_MAIN_PREFIX, pToken, nToken
215148 );
215149
215150 for(i=0; i<pConfig->nPrefix && rc==SQLITE_OK0; i++){
215151 const int nChar = pConfig->aPrefix[i];
215152 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
215153 if( nByte ){
215154 rc = sqlite3Fts5HashWrite(p->pHash,
215155 p->iWriteRowid, iCol, iPos, (char)(FTS5_MAIN_PREFIX+i+1), pToken,
215156 nByte
215157 );
215158 }
215159 }
215160
215161 return rc;
215162}
215163
215164/*
215165** Open a new iterator to iterate though all rowid that match the
215166** specified token or token prefix.
215167*/
215168static int sqlite3Fts5IndexQuery(
215169 Fts5Index *p, /* FTS index to query */
215170 const char *pToken, int nToken, /* Token (or prefix) to query for */
215171 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
215172 Fts5Colset *pColset, /* Match these columns only */
215173 Fts5IndexIter **ppIter /* OUT: New iterator object */
215174){
215175 Fts5Config *pConfig = p->pConfig;
215176 Fts5Iter *pRet = 0;
215177 Fts5Buffer buf = {0, 0, 0};
215178
215179 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
215180 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN )((void) (0));
215181
215182 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
215183 int iIdx = 0; /* Index to search */
215184 if( nToken ) memcpy(&buf.p[1], pToken, nToken);
215185
215186 /* Figure out which index to search and set iIdx accordingly. If this
215187 ** is a prefix query for which there is no prefix index, set iIdx to
215188 ** greater than pConfig->nPrefix to indicate that the query will be
215189 ** satisfied by scanning multiple terms in the main index.
215190 **
215191 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
215192 ** prefix-query. Instead of using a prefix-index (if one exists),
215193 ** evaluate the prefix query using the main FTS index. This is used
215194 ** for internal sanity checking by the integrity-check in debug
215195 ** mode only. */
215196#ifdef SQLITE_DEBUG
215197 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
215198 assert( flags & FTS5INDEX_QUERY_PREFIX )((void) (0));
215199 iIdx = 1+pConfig->nPrefix;
215200 }else
215201#endif
215202 if( flags & FTS5INDEX_QUERY_PREFIX ){
215203 int nChar = fts5IndexCharlen(pToken, nToken);
215204 for(iIdx=1; iIdx<=pConfig->nPrefix; iIdx++){
215205 if( pConfig->aPrefix[iIdx-1]==nChar ) break;
215206 }
215207 }
215208
215209 if( iIdx<=pConfig->nPrefix ){
215210 /* Straight index lookup */
215211 Fts5Structure *pStruct = fts5StructureRead(p);
215212 buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
215213 if( pStruct ){
215214 fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY,
215215 pColset, buf.p, nToken+1, -1, 0, &pRet
215216 );
215217 fts5StructureRelease(pStruct);
215218 }
215219 }else{
215220 /* Scan multiple terms in the main index */
215221 int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
215222 buf.p[0] = FTS5_MAIN_PREFIX;
215223 fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet);
215224 assert( p->rc!=SQLITE_OK || pRet->pColset==0 )((void) (0));
215225 fts5IterSetOutputCb(&p->rc, pRet);
215226 if( p->rc==SQLITE_OK0 ){
215227 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
215228 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
215229 }
215230 }
215231
215232 if( p->rc ){
215233 sqlite3Fts5IterClose((Fts5IndexIter*)pRet);
215234 pRet = 0;
215235 fts5CloseReader(p);
215236 }
215237
215238 *ppIter = (Fts5IndexIter*)pRet;
215239 sqlite3Fts5BufferFree(&buf);
215240 }
215241 return fts5IndexReturn(p);
215242}
215243
215244/*
215245** Return true if the iterator passed as the only argument is at EOF.
215246*/
215247/*
215248** Move to the next matching rowid.
215249*/
215250static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
215251 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
215252 assert( pIter->pIndex->rc==SQLITE_OK )((void) (0));
215253 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
215254 return fts5IndexReturn(pIter->pIndex);
215255}
215256
215257/*
215258** Move to the next matching term/rowid. Used by the fts5vocab module.
215259*/
215260static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
215261 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
215262 Fts5Index *p = pIter->pIndex;
215263
215264 assert( pIter->pIndex->rc==SQLITE_OK )((void) (0));
215265
215266 fts5MultiIterNext(p, pIter, 0, 0);
215267 if( p->rc==SQLITE_OK0 ){
215268 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
215269 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
215270 fts5DataRelease(pSeg->pLeaf);
215271 pSeg->pLeaf = 0;
215272 pIter->base.bEof = 1;
215273 }
215274 }
215275
215276 return fts5IndexReturn(pIter->pIndex);
215277}
215278
215279/*
215280** Move to the next matching rowid that occurs at or after iMatch. The
215281** definition of "at or after" depends on whether this iterator iterates
215282** in ascending or descending rowid order.
215283*/
215284static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
215285 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
215286 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
215287 return fts5IndexReturn(pIter->pIndex);
215288}
215289
215290/*
215291** Return the current term.
215292*/
215293static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
215294 int n;
215295 const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
215296 *pn = n-1;
215297 return &z[1];
215298}
215299
215300/*
215301** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
215302*/
215303static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
215304 if( pIndexIter ){
215305 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
215306 Fts5Index *pIndex = pIter->pIndex;
215307 fts5MultiIterFree(pIter);
215308 fts5CloseReader(pIndex);
215309 }
215310}
215311
215312/*
215313** Read and decode the "averages" record from the database.
215314**
215315** Parameter anSize must point to an array of size nCol, where nCol is
215316** the number of user defined columns in the FTS table.
215317*/
215318static int sqlite3Fts5IndexGetAverages(Fts5Index *p, i64 *pnRow, i64 *anSize){
215319 int nCol = p->pConfig->nCol;
215320 Fts5Data *pData;
215321
215322 *pnRow = 0;
215323 memset(anSize, 0, sizeof(i64) * nCol);
215324 pData = fts5DataRead(p, FTS5_AVERAGES_ROWID);
215325 if( p->rc==SQLITE_OK0 && pData->nn ){
215326 int i = 0;
215327 int iCol;
215328 i += fts5GetVarint(&pData->p[i], (u64*)pnRow);
215329 for(iCol=0; i<pData->nn && iCol<nCol; iCol++){
215330 i += fts5GetVarint(&pData->p[i], (u64*)&anSize[iCol]);
215331 }
215332 }
215333
215334 fts5DataRelease(pData);
215335 return fts5IndexReturn(p);
215336}
215337
215338/*
215339** Replace the current "averages" record with the contents of the buffer
215340** supplied as the second argument.
215341*/
215342static int sqlite3Fts5IndexSetAverages(Fts5Index *p, const u8 *pData, int nData){
215343 assert( p->rc==SQLITE_OK )((void) (0));
215344 fts5DataWrite(p, FTS5_AVERAGES_ROWID, pData, nData);
215345 return fts5IndexReturn(p);
215346}
215347
215348/*
215349** Return the total number of blocks this module has read from the %_data
215350** table since it was created.
215351*/
215352static int sqlite3Fts5IndexReads(Fts5Index *p){
215353 return p->nRead;
215354}
215355
215356/*
215357** Set the 32-bit cookie value stored at the start of all structure
215358** records to the value passed as the second argument.
215359**
215360** Return SQLITE_OK if successful, or an SQLite error code if an error
215361** occurs.
215362*/
215363static int sqlite3Fts5IndexSetCookie(Fts5Index *p, int iNew){
215364 int rc; /* Return code */
215365 Fts5Config *pConfig = p->pConfig; /* Configuration object */
215366 u8 aCookie[4]; /* Binary representation of iNew */
215367 sqlite3_blob *pBlob = 0;
215368
215369 assert( p->rc==SQLITE_OK )((void) (0));
215370 sqlite3Fts5Put32(aCookie, iNew);
215371
215372 rc = sqlite3_blob_open(pConfig->db, pConfig->zDb, p->zDataTbl,
215373 "block", FTS5_STRUCTURE_ROWID, 1, &pBlob
215374 );
215375 if( rc==SQLITE_OK0 ){
215376 sqlite3_blob_write(pBlob, aCookie, 4, 0);
215377 rc = sqlite3_blob_close(pBlob);
215378 }
215379
215380 return rc;
215381}
215382
215383static int sqlite3Fts5IndexLoadConfig(Fts5Index *p){
215384 Fts5Structure *pStruct;
215385 pStruct = fts5StructureRead(p);
215386 fts5StructureRelease(pStruct);
215387 return fts5IndexReturn(p);
215388}
215389
215390
215391/*************************************************************************
215392**************************************************************************
215393** Below this point is the implementation of the integrity-check
215394** functionality.
215395*/
215396
215397/*
215398** Return a simple checksum value based on the arguments.
215399*/
215400static u64 sqlite3Fts5IndexEntryCksum(
215401 i64 iRowid,
215402 int iCol,
215403 int iPos,
215404 int iIdx,
215405 const char *pTerm,
215406 int nTerm
215407){
215408 int i;
215409 u64 ret = iRowid;
215410 ret += (ret<<3) + iCol;
215411 ret += (ret<<3) + iPos;
215412 if( iIdx>=0 ) ret += (ret<<3) + (FTS5_MAIN_PREFIX + iIdx);
215413 for(i=0; i<nTerm; i++) ret += (ret<<3) + pTerm[i];
215414 return ret;
215415}
215416
215417#ifdef SQLITE_DEBUG
215418/*
215419** This function is purely an internal test. It does not contribute to
215420** FTS functionality, or even the integrity-check, in any way.
215421**
215422** Instead, it tests that the same set of pgno/rowid combinations are
215423** visited regardless of whether the doclist-index identified by parameters
215424** iSegid/iLeaf is iterated in forwards or reverse order.
215425*/
215426static void fts5TestDlidxReverse(
215427 Fts5Index *p,
215428 int iSegid, /* Segment id to load from */
215429 int iLeaf /* Load doclist-index for this leaf */
215430){
215431 Fts5DlidxIter *pDlidx = 0;
215432 u64 cksum1 = 13;
215433 u64 cksum2 = 13;
215434
215435 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iLeaf);
215436 fts5DlidxIterEof(p, pDlidx)==0;
215437 fts5DlidxIterNext(p, pDlidx)
215438 ){
215439 i64 iRowid = fts5DlidxIterRowid(pDlidx);
215440 int pgno = fts5DlidxIterPgno(pDlidx);
215441 assert( pgno>iLeaf )((void) (0));
215442 cksum1 += iRowid + ((i64)pgno<<32);
215443 }
215444 fts5DlidxIterFree(pDlidx);
215445 pDlidx = 0;
215446
215447 for(pDlidx=fts5DlidxIterInit(p, 1, iSegid, iLeaf);
215448 fts5DlidxIterEof(p, pDlidx)==0;
215449 fts5DlidxIterPrev(p, pDlidx)
215450 ){
215451 i64 iRowid = fts5DlidxIterRowid(pDlidx);
215452 int pgno = fts5DlidxIterPgno(pDlidx);
215453 assert( fts5DlidxIterPgno(pDlidx)>iLeaf )((void) (0));
215454 cksum2 += iRowid + ((i64)pgno<<32);
215455 }
215456 fts5DlidxIterFree(pDlidx);
215457 pDlidx = 0;
215458
215459 if( p->rc==SQLITE_OK0 && cksum1!=cksum2 ) p->rc = FTS5_CORRUPT;
215460}
215461
215462static int fts5QueryCksum(
215463 Fts5Index *p, /* Fts5 index object */
215464 int iIdx,
215465 const char *z, /* Index key to query for */
215466 int n, /* Size of index key in bytes */
215467 int flags, /* Flags for Fts5IndexQuery */
215468 u64 *pCksum /* IN/OUT: Checksum value */
215469){
215470 int eDetail = p->pConfig->eDetail;
215471 u64 cksum = *pCksum;
215472 Fts5IndexIter *pIter = 0;
215473 int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
215474
215475 while( rc==SQLITE_OK0 && 0==sqlite3Fts5IterEof(pIter) ){
215476 i64 rowid = pIter->iRowid;
215477
215478 if( eDetail==FTS5_DETAIL_NONE ){
215479 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
215480 }else{
215481 Fts5PoslistReader sReader;
215482 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
215483 sReader.bEof==0;
215484 sqlite3Fts5PoslistReaderNext(&sReader)
215485 ){
215486 int iCol = FTS5_POS2COLUMN(sReader.iPos);
215487 int iOff = FTS5_POS2OFFSET(sReader.iPos);
215488 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
215489 }
215490 }
215491 if( rc==SQLITE_OK0 ){
215492 rc = sqlite3Fts5IterNext(pIter);
215493 }
215494 }
215495 sqlite3Fts5IterClose(pIter);
215496
215497 *pCksum = cksum;
215498 return rc;
215499}
215500
215501
215502/*
215503** This function is also purely an internal test. It does not contribute to
215504** FTS functionality, or even the integrity-check, in any way.
215505*/
215506static void fts5TestTerm(
215507 Fts5Index *p,
215508 Fts5Buffer *pPrev, /* Previous term */
215509 const char *z, int n, /* Possibly new term to test */
215510 u64 expected,
215511 u64 *pCksum
215512){
215513 int rc = p->rc;
215514 if( pPrev->n==0 ){
215515 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
215516 }else
215517 if( rc==SQLITE_OK0 && (pPrev->n!=n || memcmp(pPrev->p, z, n)) ){
215518 u64 cksum3 = *pCksum;
215519 const char *zTerm = (const char*)&pPrev->p[1]; /* term sans prefix-byte */
215520 int nTerm = pPrev->n-1; /* Size of zTerm in bytes */
215521 int iIdx = (pPrev->p[0] - FTS5_MAIN_PREFIX);
215522 int flags = (iIdx==0 ? 0 : FTS5INDEX_QUERY_PREFIX);
215523 u64 ck1 = 0;
215524 u64 ck2 = 0;
215525
215526 /* Check that the results returned for ASC and DESC queries are
215527 ** the same. If not, call this corruption. */
215528 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, flags, &ck1);
215529 if( rc==SQLITE_OK0 ){
215530 int f = flags|FTS5INDEX_QUERY_DESC;
215531 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
215532 }
215533 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT;
215534
215535 /* If this is a prefix query, check that the results returned if the
215536 ** the index is disabled are the same. In both ASC and DESC order.
215537 **
215538 ** This check may only be performed if the hash table is empty. This
215539 ** is because the hash table only supports a single scan query at
215540 ** a time, and the multi-iter loop from which this function is called
215541 ** is already performing such a scan. */
215542 if( p->nPendingData==0 ){
215543 if( iIdx>0 && rc==SQLITE_OK0 ){
215544 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX;
215545 ck2 = 0;
215546 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
215547 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT;
215548 }
215549 if( iIdx>0 && rc==SQLITE_OK0 ){
215550 int f = flags|FTS5INDEX_QUERY_TEST_NOIDX|FTS5INDEX_QUERY_DESC;
215551 ck2 = 0;
215552 rc = fts5QueryCksum(p, iIdx, zTerm, nTerm, f, &ck2);
215553 if( rc==SQLITE_OK0 && ck1!=ck2 ) rc = FTS5_CORRUPT;
215554 }
215555 }
215556
215557 cksum3 ^= ck1;
215558 fts5BufferSet(&rc, pPrev, n, (const u8*)z);
215559
215560 if( rc==SQLITE_OK0 && cksum3!=expected ){
215561 rc = FTS5_CORRUPT;
215562 }
215563 *pCksum = cksum3;
215564 }
215565 p->rc = rc;
215566}
215567
215568#else
215569# define fts5TestDlidxReverse(x,y,z)
215570# define fts5TestTerm(u,v,w,x,y,z)
215571#endif
215572
215573/*
215574** Check that:
215575**
215576** 1) All leaves of pSeg between iFirst and iLast (inclusive) exist and
215577** contain zero terms.
215578** 2) All leaves of pSeg between iNoRowid and iLast (inclusive) exist and
215579** contain zero rowids.
215580*/
215581static void fts5IndexIntegrityCheckEmpty(
215582 Fts5Index *p,
215583 Fts5StructureSegment *pSeg, /* Segment to check internal consistency */
215584 int iFirst,
215585 int iNoRowid,
215586 int iLast
215587){
215588 int i;
215589
215590 /* Now check that the iter.nEmpty leaves following the current leaf
215591 ** (a) exist and (b) contain no terms. */
215592 for(i=iFirst; p->rc==SQLITE_OK0 && i<=iLast; i++){
215593 Fts5Data *pLeaf = fts5DataRead(p, FTS5_SEGMENT_ROWID(pSeg->iSegid, i));
215594 if( pLeaf ){
215595 if( !fts5LeafIsTermless(pLeaf) ) p->rc = FTS5_CORRUPT;
215596 if( i>=iNoRowid && 0!=fts5LeafFirstRowidOff(pLeaf) ) p->rc = FTS5_CORRUPT;
215597 }
215598 fts5DataRelease(pLeaf);
215599 }
215600}
215601
215602static void fts5IntegrityCheckPgidx(Fts5Index *p, Fts5Data *pLeaf){
215603 int iTermOff = 0;
215604 int ii;
215605
215606 Fts5Buffer buf1 = {0,0,0};
215607 Fts5Buffer buf2 = {0,0,0};
215608
215609 ii = pLeaf->szLeaf;
215610 while( ii<pLeaf->nn && p->rc==SQLITE_OK0 ){
215611 int res;
215612 int iOff;
215613 int nIncr;
215614
215615 ii += fts5GetVarint32(&pLeaf->p[ii], nIncr);
215616 iTermOff += nIncr;
215617 iOff = iTermOff;
215618
215619 if( iOff>=pLeaf->szLeaf ){
215620 p->rc = FTS5_CORRUPT;
215621 }else if( iTermOff==nIncr ){
215622 int nByte;
215623 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
215624 if( (iOff+nByte)>pLeaf->szLeaf ){
215625 p->rc = FTS5_CORRUPT;
215626 }else{
215627 fts5BufferSet(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
215628 }
215629 }else{
215630 int nKeep, nByte;
215631 iOff += fts5GetVarint32(&pLeaf->p[iOff], nKeep);
215632 iOff += fts5GetVarint32(&pLeaf->p[iOff], nByte);
215633 if( nKeep>buf1.n || (iOff+nByte)>pLeaf->szLeaf ){
215634 p->rc = FTS5_CORRUPT;
215635 }else{
215636 buf1.n = nKeep;
215637 fts5BufferAppendBlob(&p->rc, &buf1, nByte, &pLeaf->p[iOff]);
215638 }
215639
215640 if( p->rc==SQLITE_OK0 ){
215641 res = fts5BufferCompare(&buf1, &buf2);
215642 if( res<=0 ) p->rc = FTS5_CORRUPT;
215643 }
215644 }
215645 fts5BufferSet(&p->rc, &buf2, buf1.n, buf1.p);
215646 }
215647
215648 fts5BufferFree(&buf1);
215649 fts5BufferFree(&buf2);
215650}
215651
215652static void fts5IndexIntegrityCheckSegment(
215653 Fts5Index *p, /* FTS5 backend object */
215654 Fts5StructureSegment *pSeg /* Segment to check internal consistency */
215655){
215656 Fts5Config *pConfig = p->pConfig;
215657 sqlite3_stmt *pStmt = 0;
215658 int rc2;
215659 int iIdxPrevLeaf = pSeg->pgnoFirst-1;
215660 int iDlidxPrevLeaf = pSeg->pgnoLast;
215661
215662 if( pSeg->pgnoFirst==0 ) return;
215663
215664 fts5IndexPrepareStmt(p, &pStmt, sqlite3_mprintf(
215665 "SELECT segid, term, (pgno>>1), (pgno&1) FROM %Q.'%q_idx' WHERE segid=%d",
215666 pConfig->zDb, pConfig->zName, pSeg->iSegid
215667 ));
215668
215669 /* Iterate through the b-tree hierarchy. */
215670 while( p->rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pStmt) ){
215671 i64 iRow; /* Rowid for this leaf */
215672 Fts5Data *pLeaf; /* Data for this leaf */
215673
215674 int nIdxTerm = sqlite3_column_bytes(pStmt, 1);
215675 const char *zIdxTerm = (const char*)sqlite3_column_text(pStmt, 1);
215676 int iIdxLeaf = sqlite3_column_int(pStmt, 2);
215677 int bIdxDlidx = sqlite3_column_int(pStmt, 3);
215678
215679 /* If the leaf in question has already been trimmed from the segment,
215680 ** ignore this b-tree entry. Otherwise, load it into memory. */
215681 if( iIdxLeaf<pSeg->pgnoFirst ) continue;
215682 iRow = FTS5_SEGMENT_ROWID(pSeg->iSegid, iIdxLeaf);
215683 pLeaf = fts5LeafRead(p, iRow);
215684 if( pLeaf==0 ) break;
215685
215686 /* Check that the leaf contains at least one term, and that it is equal
215687 ** to or larger than the split-key in zIdxTerm. Also check that if there
215688 ** is also a rowid pointer within the leaf page header, it points to a
215689 ** location before the term. */
215690 if( pLeaf->nn<=pLeaf->szLeaf ){
215691 p->rc = FTS5_CORRUPT;
215692 }else{
215693 int iOff; /* Offset of first term on leaf */
215694 int iRowidOff; /* Offset of first rowid on leaf */
215695 int nTerm; /* Size of term on leaf in bytes */
215696 int res; /* Comparison of term and split-key */
215697
215698 iOff = fts5LeafFirstTermOff(pLeaf);
215699 iRowidOff = fts5LeafFirstRowidOff(pLeaf);
215700 if( iRowidOff>=iOff || iOff>=pLeaf->szLeaf ){
215701 p->rc = FTS5_CORRUPT;
215702 }else{
215703 iOff += fts5GetVarint32(&pLeaf->p[iOff], nTerm);
215704 res = fts5Memcmp(&pLeaf->p[iOff], zIdxTerm, MIN(nTerm, nIdxTerm)((nTerm)<(nIdxTerm)?(nTerm):(nIdxTerm)));
215705 if( res==0 ) res = nTerm - nIdxTerm;
215706 if( res<0 ) p->rc = FTS5_CORRUPT;
215707 }
215708
215709 fts5IntegrityCheckPgidx(p, pLeaf);
215710 }
215711 fts5DataRelease(pLeaf);
215712 if( p->rc ) break;
215713
215714 /* Now check that the iter.nEmpty leaves following the current leaf
215715 ** (a) exist and (b) contain no terms. */
215716 fts5IndexIntegrityCheckEmpty(
215717 p, pSeg, iIdxPrevLeaf+1, iDlidxPrevLeaf+1, iIdxLeaf-1
215718 );
215719 if( p->rc ) break;
215720
215721 /* If there is a doclist-index, check that it looks right. */
215722 if( bIdxDlidx ){
215723 Fts5DlidxIter *pDlidx = 0; /* For iterating through doclist index */
215724 int iPrevLeaf = iIdxLeaf;
215725 int iSegid = pSeg->iSegid;
215726 int iPg = 0;
215727 i64 iKey;
215728
215729 for(pDlidx=fts5DlidxIterInit(p, 0, iSegid, iIdxLeaf);
215730 fts5DlidxIterEof(p, pDlidx)==0;
215731 fts5DlidxIterNext(p, pDlidx)
215732 ){
215733
215734 /* Check any rowid-less pages that occur before the current leaf. */
215735 for(iPg=iPrevLeaf+1; iPg<fts5DlidxIterPgno(pDlidx); iPg++){
215736 iKey = FTS5_SEGMENT_ROWID(iSegid, iPg);
215737 pLeaf = fts5DataRead(p, iKey);
215738 if( pLeaf ){
215739 if( fts5LeafFirstRowidOff(pLeaf)!=0 ) p->rc = FTS5_CORRUPT;
215740 fts5DataRelease(pLeaf);
215741 }
215742 }
215743 iPrevLeaf = fts5DlidxIterPgno(pDlidx);
215744
215745 /* Check that the leaf page indicated by the iterator really does
215746 ** contain the rowid suggested by the same. */
215747 iKey = FTS5_SEGMENT_ROWID(iSegid, iPrevLeaf);
215748 pLeaf = fts5DataRead(p, iKey);
215749 if( pLeaf ){
215750 i64 iRowid;
215751 int iRowidOff = fts5LeafFirstRowidOff(pLeaf);
215752 ASSERT_SZLEAF_OK(pLeaf);
215753 if( iRowidOff>=pLeaf->szLeaf ){
215754 p->rc = FTS5_CORRUPT;
215755 }else{
215756 fts5GetVarint(&pLeaf->p[iRowidOff], (u64*)&iRowid);
215757 if( iRowid!=fts5DlidxIterRowid(pDlidx) ) p->rc = FTS5_CORRUPT;
215758 }
215759 fts5DataRelease(pLeaf);
215760 }
215761 }
215762
215763 iDlidxPrevLeaf = iPg;
215764 fts5DlidxIterFree(pDlidx);
215765 fts5TestDlidxReverse(p, iSegid, iIdxLeaf);
215766 }else{
215767 iDlidxPrevLeaf = pSeg->pgnoLast;
215768 /* TODO: Check there is no doclist index */
215769 }
215770
215771 iIdxPrevLeaf = iIdxLeaf;
215772 }
215773
215774 rc2 = sqlite3_finalize(pStmt);
215775 if( p->rc==SQLITE_OK0 ) p->rc = rc2;
215776
215777 /* Page iter.iLeaf must now be the rightmost leaf-page in the segment */
215778#if 0
215779 if( p->rc==SQLITE_OK0 && iter.iLeaf!=pSeg->pgnoLast ){
215780 p->rc = FTS5_CORRUPT;
215781 }
215782#endif
215783}
215784
215785
215786/*
215787** Run internal checks to ensure that the FTS index (a) is internally
215788** consistent and (b) contains entries for which the XOR of the checksums
215789** as calculated by sqlite3Fts5IndexEntryCksum() is cksum.
215790**
215791** Return SQLITE_CORRUPT if any of the internal checks fail, or if the
215792** checksum does not match. Return SQLITE_OK if all checks pass without
215793** error, or some other SQLite error code if another error (e.g. OOM)
215794** occurs.
215795*/
215796static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){
215797 int eDetail = p->pConfig->eDetail;
215798 u64 cksum2 = 0; /* Checksum based on contents of indexes */
215799 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
215800 Fts5Iter *pIter; /* Used to iterate through entire index */
215801 Fts5Structure *pStruct; /* Index structure */
215802
215803#ifdef SQLITE_DEBUG
215804 /* Used by extra internal tests only run if NDEBUG is not defined */
215805 u64 cksum3 = 0; /* Checksum based on contents of indexes */
215806 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
215807#endif
215808 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
215809
215810 /* Load the FTS index structure */
215811 pStruct = fts5StructureRead(p);
215812
215813 /* Check that the internal nodes of each segment match the leaves */
215814 if( pStruct ){
215815 int iLvl, iSeg;
215816 for(iLvl=0; iLvl<pStruct->nLevel; iLvl++){
215817 for(iSeg=0; iSeg<pStruct->aLevel[iLvl].nSeg; iSeg++){
215818 Fts5StructureSegment *pSeg = &pStruct->aLevel[iLvl].aSeg[iSeg];
215819 fts5IndexIntegrityCheckSegment(p, pSeg);
215820 }
215821 }
215822 }
215823
215824 /* The cksum argument passed to this function is a checksum calculated
215825 ** based on all expected entries in the FTS index (including prefix index
215826 ** entries). This block checks that a checksum calculated based on the
215827 ** actual contents of FTS index is identical.
215828 **
215829 ** Two versions of the same checksum are calculated. The first (stack
215830 ** variable cksum2) based on entries extracted from the full-text index
215831 ** while doing a linear scan of each individual index in turn.
215832 **
215833 ** As each term visited by the linear scans, a separate query for the
215834 ** same term is performed. cksum3 is calculated based on the entries
215835 ** extracted by these queries.
215836 */
215837 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
215838 fts5MultiIterEof(p, pIter)==0;
215839 fts5MultiIterNext(p, pIter, 0, 0)
215840 ){
215841 int n; /* Size of term in bytes */
215842 i64 iPos = 0; /* Position read from poslist */
215843 int iOff = 0; /* Offset within poslist */
215844 i64 iRowid = fts5MultiIterRowid(pIter);
215845 char *z = (char*)fts5MultiIterTerm(pIter, &n);
215846
215847 /* If this is a new term, query for it. Update cksum3 with the results. */
215848 fts5TestTerm(p, &term, z, n, cksum2, &cksum3);
215849
215850 if( eDetail==FTS5_DETAIL_NONE ){
215851 if( 0==fts5MultiIterIsEmpty(p, pIter) ){
215852 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, 0, 0, -1, z, n);
215853 }
215854 }else{
215855 poslist.n = 0;
215856 fts5SegiterPoslist(p, &pIter->aSeg[pIter->aFirst[1].iFirst], 0, &poslist);
215857 while( 0==sqlite3Fts5PoslistNext64(poslist.p, poslist.n, &iOff, &iPos) ){
215858 int iCol = FTS5_POS2COLUMN(iPos);
215859 int iTokOff = FTS5_POS2OFFSET(iPos);
215860 cksum2 ^= sqlite3Fts5IndexEntryCksum(iRowid, iCol, iTokOff, -1, z, n);
215861 }
215862 }
215863 }
215864 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
215865
215866 fts5MultiIterFree(pIter);
215867 if( p->rc==SQLITE_OK0 && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
215868
215869 fts5StructureRelease(pStruct);
215870#ifdef SQLITE_DEBUG
215871 fts5BufferFree(&term);
215872#endif
215873 fts5BufferFree(&poslist);
215874 return fts5IndexReturn(p);
215875}
215876
215877/*************************************************************************
215878**************************************************************************
215879** Below this point is the implementation of the fts5_decode() scalar
215880** function only.
215881*/
215882
215883/*
215884** Decode a segment-data rowid from the %_data table. This function is
215885** the opposite of macro FTS5_SEGMENT_ROWID().
215886*/
215887static void fts5DecodeRowid(
215888 i64 iRowid, /* Rowid from %_data table */
215889 int *piSegid, /* OUT: Segment id */
215890 int *pbDlidx, /* OUT: Dlidx flag */
215891 int *piHeight, /* OUT: Height */
215892 int *piPgno /* OUT: Page number */
215893){
215894 *piPgno = (int)(iRowid & (((i64)1 << FTS5_DATA_PAGE_B) - 1));
215895 iRowid >>= FTS5_DATA_PAGE_B;
215896
215897 *piHeight = (int)(iRowid & (((i64)1 << FTS5_DATA_HEIGHT_B) - 1));
215898 iRowid >>= FTS5_DATA_HEIGHT_B;
215899
215900 *pbDlidx = (int)(iRowid & 0x0001);
215901 iRowid >>= FTS5_DATA_DLI_B;
215902
215903 *piSegid = (int)(iRowid & (((i64)1 << FTS5_DATA_ID_B) - 1));
215904}
215905
215906static void fts5DebugRowid(int *pRc, Fts5Buffer *pBuf, i64 iKey){
215907 int iSegid, iHeight, iPgno, bDlidx; /* Rowid compenents */
215908 fts5DecodeRowid(iKey, &iSegid, &bDlidx, &iHeight, &iPgno);
215909
215910 if( iSegid==0 ){
215911 if( iKey==FTS5_AVERAGES_ROWID ){
215912 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{averages} ");
215913 }else{
215914 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{structure}");
215915 }
215916 }
215917 else{
215918 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "{%ssegid=%d h=%d pgno=%d}",
215919 bDlidx ? "dlidx " : "", iSegid, iHeight, iPgno
215920 );
215921 }
215922}
215923
215924static void fts5DebugStructure(
215925 int *pRc, /* IN/OUT: error code */
215926 Fts5Buffer *pBuf,
215927 Fts5Structure *p
215928){
215929 int iLvl, iSeg; /* Iterate through levels, segments */
215930
215931 for(iLvl=0; iLvl<p->nLevel; iLvl++){
215932 Fts5StructureLevel *pLvl = &p->aLevel[iLvl];
215933 sqlite3Fts5BufferAppendPrintf(pRc, pBuf,
215934 " {lvl=%d nMerge=%d nSeg=%d", iLvl, pLvl->nMerge, pLvl->nSeg
215935 );
215936 for(iSeg=0; iSeg<pLvl->nSeg; iSeg++){
215937 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
215938 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " {id=%d leaves=%d..%d}",
215939 pSeg->iSegid, pSeg->pgnoFirst, pSeg->pgnoLast
215940 );
215941 }
215942 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "}");
215943 }
215944}
215945
215946/*
215947** This is part of the fts5_decode() debugging aid.
215948**
215949** Arguments pBlob/nBlob contain a serialized Fts5Structure object. This
215950** function appends a human-readable representation of the same object
215951** to the buffer passed as the second argument.
215952*/
215953static void fts5DecodeStructure(
215954 int *pRc, /* IN/OUT: error code */
215955 Fts5Buffer *pBuf,
215956 const u8 *pBlob, int nBlob
215957){
215958 int rc; /* Return code */
215959 Fts5Structure *p = 0; /* Decoded structure object */
215960
215961 rc = fts5StructureDecode(pBlob, nBlob, 0, &p);
215962 if( rc!=SQLITE_OK0 ){
215963 *pRc = rc;
215964 return;
215965 }
215966
215967 fts5DebugStructure(pRc, pBuf, p);
215968 fts5StructureRelease(p);
215969}
215970
215971/*
215972** This is part of the fts5_decode() debugging aid.
215973**
215974** Arguments pBlob/nBlob contain an "averages" record. This function
215975** appends a human-readable representation of record to the buffer passed
215976** as the second argument.
215977*/
215978static void fts5DecodeAverages(
215979 int *pRc, /* IN/OUT: error code */
215980 Fts5Buffer *pBuf,
215981 const u8 *pBlob, int nBlob
215982){
215983 int i = 0;
215984 const char *zSpace = "";
215985
215986 while( i<nBlob ){
215987 u64 iVal;
215988 i += sqlite3Fts5GetVarint(&pBlob[i], &iVal);
215989 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, "%s%d", zSpace, (int)iVal);
215990 zSpace = " ";
215991 }
215992}
215993
215994/*
215995** Buffer (a/n) is assumed to contain a list of serialized varints. Read
215996** each varint and append its string representation to buffer pBuf. Return
215997** after either the input buffer is exhausted or a 0 value is read.
215998**
215999** The return value is the number of bytes read from the input buffer.
216000*/
216001static int fts5DecodePoslist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
216002 int iOff = 0;
216003 while( iOff<n ){
216004 int iVal;
216005 iOff += fts5GetVarint32(&a[iOff], iVal);
216006 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %d", iVal);
216007 }
216008 return iOff;
216009}
216010
216011/*
216012** The start of buffer (a/n) contains the start of a doclist. The doclist
216013** may or may not finish within the buffer. This function appends a text
216014** representation of the part of the doclist that is present to buffer
216015** pBuf.
216016**
216017** The return value is the number of bytes read from the input buffer.
216018*/
216019static int fts5DecodeDoclist(int *pRc, Fts5Buffer *pBuf, const u8 *a, int n){
216020 i64 iDocid = 0;
216021 int iOff = 0;
216022
216023 if( n>0 ){
216024 iOff = sqlite3Fts5GetVarint(a, (u64*)&iDocid);
216025 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
216026 }
216027 while( iOff<n ){
216028 int nPos;
216029 int bDel;
216030 iOff += fts5GetPoslistSize(&a[iOff], &nPos, &bDel);
216031 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " nPos=%d%s", nPos, bDel?"*":"");
216032 iOff += fts5DecodePoslist(pRc, pBuf, &a[iOff], MIN(n-iOff, nPos)((n-iOff)<(nPos)?(n-iOff):(nPos)));
216033 if( iOff<n ){
216034 i64 iDelta;
216035 iOff += sqlite3Fts5GetVarint(&a[iOff], (u64*)&iDelta);
216036 iDocid += iDelta;
216037 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " id=%lld", iDocid);
216038 }
216039 }
216040
216041 return iOff;
216042}
216043
216044/*
216045** This function is part of the fts5_decode() debugging function. It is
216046** only ever used with detail=none tables.
216047**
216048** Buffer (pData/nData) contains a doclist in the format used by detail=none
216049** tables. This function appends a human-readable version of that list to
216050** buffer pBuf.
216051**
216052** If *pRc is other than SQLITE_OK when this function is called, it is a
216053** no-op. If an OOM or other error occurs within this function, *pRc is
216054** set to an SQLite error code before returning. The final state of buffer
216055** pBuf is undefined in this case.
216056*/
216057static void fts5DecodeRowidList(
216058 int *pRc, /* IN/OUT: Error code */
216059 Fts5Buffer *pBuf, /* Buffer to append text to */
216060 const u8 *pData, int nData /* Data to decode list-of-rowids from */
216061){
216062 int i = 0;
216063 i64 iRowid = 0;
216064
216065 while( i<nData ){
216066 const char *zApp = "";
216067 u64 iVal;
216068 i += sqlite3Fts5GetVarint(&pData[i], &iVal);
216069 iRowid += iVal;
216070
216071 if( i<nData && pData[i]==0x00 ){
216072 i++;
216073 if( i<nData && pData[i]==0x00 ){
216074 i++;
216075 zApp = "+";
216076 }else{
216077 zApp = "*";
216078 }
216079 }
216080
216081 sqlite3Fts5BufferAppendPrintf(pRc, pBuf, " %lld%s", iRowid, zApp);
216082 }
216083}
216084
216085/*
216086** The implementation of user-defined scalar function fts5_decode().
216087*/
216088static void fts5DecodeFunction(
216089 sqlite3_context *pCtx, /* Function call context */
216090 int nArg, /* Number of args (always 2) */
216091 sqlite3_value **apVal /* Function arguments */
216092){
216093 i64 iRowid; /* Rowid for record being decoded */
216094 int iSegid,iHeight,iPgno,bDlidx;/* Rowid components */
216095 const u8 *aBlob; int n; /* Record to decode */
216096 u8 *a = 0;
216097 Fts5Buffer s; /* Build up text to return here */
216098 int rc = SQLITE_OK0; /* Return code */
216099 sqlite3_int64 nSpace = 0;
216100 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
216101
216102 assert( nArg==2 )((void) (0));
216103 UNUSED_PARAM(nArg);
216104 memset(&s, 0, sizeof(Fts5Buffer));
216105 iRowid = sqlite3_value_int64(apVal[0]);
216106
216107 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
216108 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
216109 ** buffer overreads even if the record is corrupt. */
216110 n = sqlite3_value_bytes(apVal[1]);
216111 aBlob = sqlite3_value_blob(apVal[1]);
216112 nSpace = n + FTS5_DATA_ZERO_PADDING;
216113 a = (u8*)sqlite3Fts5MallocZero(&rc, nSpace);
216114 if( a==0 ) goto decode_out;
216115 if( n>0 ) memcpy(a, aBlob, n);
216116
216117 fts5DecodeRowid(iRowid, &iSegid, &bDlidx, &iHeight, &iPgno);
216118
216119 fts5DebugRowid(&rc, &s, iRowid);
216120 if( bDlidx ){
216121 Fts5Data dlidx;
216122 Fts5DlidxLvl lvl;
216123
216124 dlidx.p = a;
216125 dlidx.nn = n;
216126
216127 memset(&lvl, 0, sizeof(Fts5DlidxLvl));
216128 lvl.pData = &dlidx;
216129 lvl.iLeafPgno = iPgno;
216130
216131 for(fts5DlidxLvlNext(&lvl); lvl.bEof==0; fts5DlidxLvlNext(&lvl)){
216132 sqlite3Fts5BufferAppendPrintf(&rc, &s,
216133 " %d(%lld)", lvl.iLeafPgno, lvl.iRowid
216134 );
216135 }
216136 }else if( iSegid==0 ){
216137 if( iRowid==FTS5_AVERAGES_ROWID ){
216138 fts5DecodeAverages(&rc, &s, a, n);
216139 }else{
216140 fts5DecodeStructure(&rc, &s, a, n);
216141 }
216142 }else if( eDetailNone ){
216143 Fts5Buffer term; /* Current term read from page */
216144 int szLeaf;
216145 int iPgidxOff = szLeaf = fts5GetU16(&a[2]);
216146 int iTermOff;
216147 int nKeep = 0;
216148 int iOff;
216149
216150 memset(&term, 0, sizeof(Fts5Buffer));
216151
216152 /* Decode any entries that occur before the first term. */
216153 if( szLeaf<n ){
216154 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], iTermOff);
216155 }else{
216156 iTermOff = szLeaf;
216157 }
216158 fts5DecodeRowidList(&rc, &s, &a[4], iTermOff-4);
216159
216160 iOff = iTermOff;
216161 while( iOff<szLeaf ){
216162 int nAppend;
216163
216164 /* Read the term data for the next term*/
216165 iOff += fts5GetVarint32(&a[iOff], nAppend);
216166 term.n = nKeep;
216167 fts5BufferAppendBlob(&rc, &term, nAppend, &a[iOff]);
216168 sqlite3Fts5BufferAppendPrintf(
216169 &rc, &s, " term=%.*s", term.n, (const char*)term.p
216170 );
216171 iOff += nAppend;
216172
216173 /* Figure out where the doclist for this term ends */
216174 if( iPgidxOff<n ){
216175 int nIncr;
216176 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nIncr);
216177 iTermOff += nIncr;
216178 }else{
216179 iTermOff = szLeaf;
216180 }
216181
216182 fts5DecodeRowidList(&rc, &s, &a[iOff], iTermOff-iOff);
216183 iOff = iTermOff;
216184 if( iOff<szLeaf ){
216185 iOff += fts5GetVarint32(&a[iOff], nKeep);
216186 }
216187 }
216188
216189 fts5BufferFree(&term);
216190 }else{
216191 Fts5Buffer term; /* Current term read from page */
216192 int szLeaf; /* Offset of pgidx in a[] */
216193 int iPgidxOff;
216194 int iPgidxPrev = 0; /* Previous value read from pgidx */
216195 int iTermOff = 0;
216196 int iRowidOff = 0;
216197 int iOff;
216198 int nDoclist;
216199
216200 memset(&term, 0, sizeof(Fts5Buffer));
216201
216202 if( n<4 ){
216203 sqlite3Fts5BufferSet(&rc, &s, 7, (const u8*)"corrupt");
216204 goto decode_out;
216205 }else{
216206 iRowidOff = fts5GetU16(&a[0]);
216207 iPgidxOff = szLeaf = fts5GetU16(&a[2]);
216208 if( iPgidxOff<n ){
216209 fts5GetVarint32(&a[iPgidxOff], iTermOff);
216210 }else if( iPgidxOff>n ){
216211 rc = FTS5_CORRUPT;
216212 goto decode_out;
216213 }
216214 }
216215
216216 /* Decode the position list tail at the start of the page */
216217 if( iRowidOff!=0 ){
216218 iOff = iRowidOff;
216219 }else if( iTermOff!=0 ){
216220 iOff = iTermOff;
216221 }else{
216222 iOff = szLeaf;
216223 }
216224 if( iOff>n ){
216225 rc = FTS5_CORRUPT;
216226 goto decode_out;
216227 }
216228 fts5DecodePoslist(&rc, &s, &a[4], iOff-4);
216229
216230 /* Decode any more doclist data that appears on the page before the
216231 ** first term. */
216232 nDoclist = (iTermOff ? iTermOff : szLeaf) - iOff;
216233 if( nDoclist+iOff>n ){
216234 rc = FTS5_CORRUPT;
216235 goto decode_out;
216236 }
216237 fts5DecodeDoclist(&rc, &s, &a[iOff], nDoclist);
216238
216239 while( iPgidxOff<n && rc==SQLITE_OK0 ){
216240 int bFirst = (iPgidxOff==szLeaf); /* True for first term on page */
216241 int nByte; /* Bytes of data */
216242 int iEnd;
216243
216244 iPgidxOff += fts5GetVarint32(&a[iPgidxOff], nByte);
216245 iPgidxPrev += nByte;
216246 iOff = iPgidxPrev;
216247
216248 if( iPgidxOff<n ){
216249 fts5GetVarint32(&a[iPgidxOff], nByte);
216250 iEnd = iPgidxPrev + nByte;
216251 }else{
216252 iEnd = szLeaf;
216253 }
216254 if( iEnd>szLeaf ){
216255 rc = FTS5_CORRUPT;
216256 break;
216257 }
216258
216259 if( bFirst==0 ){
216260 iOff += fts5GetVarint32(&a[iOff], nByte);
216261 if( nByte>term.n ){
216262 rc = FTS5_CORRUPT;
216263 break;
216264 }
216265 term.n = nByte;
216266 }
216267 iOff += fts5GetVarint32(&a[iOff], nByte);
216268 if( iOff+nByte>n ){
216269 rc = FTS5_CORRUPT;
216270 break;
216271 }
216272 fts5BufferAppendBlob(&rc, &term, nByte, &a[iOff]);
216273 iOff += nByte;
216274
216275 sqlite3Fts5BufferAppendPrintf(
216276 &rc, &s, " term=%.*s", term.n, (const char*)term.p
216277 );
216278 iOff += fts5DecodeDoclist(&rc, &s, &a[iOff], iEnd-iOff);
216279 }
216280
216281 fts5BufferFree(&term);
216282 }
216283
216284 decode_out:
216285 sqlite3_free(a);
216286 if( rc==SQLITE_OK0 ){
216287 sqlite3_result_text(pCtx, (const char*)s.p, s.n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
216288 }else{
216289 sqlite3_result_error_code(pCtx, rc);
216290 }
216291 fts5BufferFree(&s);
216292}
216293
216294/*
216295** The implementation of user-defined scalar function fts5_rowid().
216296*/
216297static void fts5RowidFunction(
216298 sqlite3_context *pCtx, /* Function call context */
216299 int nArg, /* Number of args (always 2) */
216300 sqlite3_value **apVal /* Function arguments */
216301){
216302 const char *zArg;
216303 if( nArg==0 ){
216304 sqlite3_result_error(pCtx, "should be: fts5_rowid(subject, ....)", -1);
216305 }else{
216306 zArg = (const char*)sqlite3_value_text(apVal[0]);
216307 if( 0==sqlite3_stricmp(zArg, "segment") ){
216308 i64 iRowid;
216309 int segid, pgno;
216310 if( nArg!=3 ){
216311 sqlite3_result_error(pCtx,
216312 "should be: fts5_rowid('segment', segid, pgno))", -1
216313 );
216314 }else{
216315 segid = sqlite3_value_int(apVal[1]);
216316 pgno = sqlite3_value_int(apVal[2]);
216317 iRowid = FTS5_SEGMENT_ROWID(segid, pgno);
216318 sqlite3_result_int64(pCtx, iRowid);
216319 }
216320 }else{
216321 sqlite3_result_error(pCtx,
216322 "first arg to fts5_rowid() must be 'segment'" , -1
216323 );
216324 }
216325 }
216326}
216327
216328/*
216329** This is called as part of registering the FTS5 module with database
216330** connection db. It registers several user-defined scalar functions useful
216331** with FTS5.
216332**
216333** If successful, SQLITE_OK is returned. If an error occurs, some other
216334** SQLite error code is returned instead.
216335*/
216336static int sqlite3Fts5IndexInit(sqlite3 *db){
216337 int rc = sqlite3_create_function(
216338 db, "fts5_decode", 2, SQLITE_UTF81, 0, fts5DecodeFunction, 0, 0
216339 );
216340
216341 if( rc==SQLITE_OK0 ){
216342 rc = sqlite3_create_function(
216343 db, "fts5_decode_none", 2,
216344 SQLITE_UTF81, (void*)db, fts5DecodeFunction, 0, 0
216345 );
216346 }
216347
216348 if( rc==SQLITE_OK0 ){
216349 rc = sqlite3_create_function(
216350 db, "fts5_rowid", -1, SQLITE_UTF81, 0, fts5RowidFunction, 0, 0
216351 );
216352 }
216353 return rc;
216354}
216355
216356
216357static int sqlite3Fts5IndexReset(Fts5Index *p){
216358 assert( p->pStruct==0 || p->iStructVersion!=0 )((void) (0));
216359 if( fts5IndexDataVersion(p)!=p->iStructVersion ){
216360 fts5StructureInvalidate(p);
216361 }
216362 return fts5IndexReturn(p);
216363}
216364
216365/*
216366** 2014 Jun 09
216367**
216368** The author disclaims copyright to this source code. In place of
216369** a legal notice, here is a blessing:
216370**
216371** May you do good and not evil.
216372** May you find forgiveness for yourself and forgive others.
216373** May you share freely, never taking more than you give.
216374**
216375******************************************************************************
216376**
216377** This is an SQLite module implementing full-text search.
216378*/
216379
216380
216381/* #include "fts5Int.h" */
216382
216383/*
216384** This variable is set to false when running tests for which the on disk
216385** structures should not be corrupt. Otherwise, true. If it is false, extra
216386** assert() conditions in the fts5 code are activated - conditions that are
216387** only true if it is guaranteed that the fts5 database is not corrupt.
216388*/
216389SQLITE_API int sqlite3_fts5_may_be_corrupt = 1;
216390
216391
216392typedef struct Fts5Auxdata Fts5Auxdata;
216393typedef struct Fts5Auxiliary Fts5Auxiliary;
216394typedef struct Fts5Cursor Fts5Cursor;
216395typedef struct Fts5FullTable Fts5FullTable;
216396typedef struct Fts5Sorter Fts5Sorter;
216397typedef struct Fts5TokenizerModule Fts5TokenizerModule;
216398
216399/*
216400** NOTES ON TRANSACTIONS:
216401**
216402** SQLite invokes the following virtual table methods as transactions are
216403** opened and closed by the user:
216404**
216405** xBegin(): Start of a new transaction.
216406** xSync(): Initial part of two-phase commit.
216407** xCommit(): Final part of two-phase commit.
216408** xRollback(): Rollback the transaction.
216409**
216410** Anything that is required as part of a commit that may fail is performed
216411** in the xSync() callback. Current versions of SQLite ignore any errors
216412** returned by xCommit().
216413**
216414** And as sub-transactions are opened/closed:
216415**
216416** xSavepoint(int S): Open savepoint S.
216417** xRelease(int S): Commit and close savepoint S.
216418** xRollbackTo(int S): Rollback to start of savepoint S.
216419**
216420** During a write-transaction the fts5_index.c module may cache some data
216421** in-memory. It is flushed to disk whenever xSync(), xRelease() or
216422** xSavepoint() is called. And discarded whenever xRollback() or xRollbackTo()
216423** is called.
216424**
216425** Additionally, if SQLITE_DEBUG is defined, an instance of the following
216426** structure is used to record the current transaction state. This information
216427** is not required, but it is used in the assert() statements executed by
216428** function fts5CheckTransactionState() (see below).
216429*/
216430struct Fts5TransactionState {
216431 int eState; /* 0==closed, 1==open, 2==synced */
216432 int iSavepoint; /* Number of open savepoints (0 -> none) */
216433};
216434
216435/*
216436** A single object of this type is allocated when the FTS5 module is
216437** registered with a database handle. It is used to store pointers to
216438** all registered FTS5 extensions - tokenizers and auxiliary functions.
216439*/
216440struct Fts5Global {
216441 fts5_api api; /* User visible part of object (see fts5.h) */
216442 sqlite3 *db; /* Associated database connection */
216443 i64 iNextId; /* Used to allocate unique cursor ids */
216444 Fts5Auxiliary *pAux; /* First in list of all aux. functions */
216445 Fts5TokenizerModule *pTok; /* First in list of all tokenizer modules */
216446 Fts5TokenizerModule *pDfltTok; /* Default tokenizer module */
216447 Fts5Cursor *pCsr; /* First in list of all open cursors */
216448};
216449
216450/*
216451** Each auxiliary function registered with the FTS5 module is represented
216452** by an object of the following type. All such objects are stored as part
216453** of the Fts5Global.pAux list.
216454*/
216455struct Fts5Auxiliary {
216456 Fts5Global *pGlobal; /* Global context for this function */
216457 char *zFunc; /* Function name (nul-terminated) */
216458 void *pUserData; /* User-data pointer */
216459 fts5_extension_function xFunc; /* Callback function */
216460 void (*xDestroy)(void*); /* Destructor function */
216461 Fts5Auxiliary *pNext; /* Next registered auxiliary function */
216462};
216463
216464/*
216465** Each tokenizer module registered with the FTS5 module is represented
216466** by an object of the following type. All such objects are stored as part
216467** of the Fts5Global.pTok list.
216468*/
216469struct Fts5TokenizerModule {
216470 char *zName; /* Name of tokenizer */
216471 void *pUserData; /* User pointer passed to xCreate() */
216472 fts5_tokenizer x; /* Tokenizer functions */
216473 void (*xDestroy)(void*); /* Destructor function */
216474 Fts5TokenizerModule *pNext; /* Next registered tokenizer module */
216475};
216476
216477struct Fts5FullTable {
216478 Fts5Table p; /* Public class members from fts5Int.h */
216479 Fts5Storage *pStorage; /* Document store */
216480 Fts5Global *pGlobal; /* Global (connection wide) data */
216481 Fts5Cursor *pSortCsr; /* Sort data from this cursor */
216482#ifdef SQLITE_DEBUG
216483 struct Fts5TransactionState ts;
216484#endif
216485};
216486
216487struct Fts5MatchPhrase {
216488 Fts5Buffer *pPoslist; /* Pointer to current poslist */
216489 int nTerm; /* Size of phrase in terms */
216490};
216491
216492/*
216493** pStmt:
216494** SELECT rowid, <fts> FROM <fts> ORDER BY +rank;
216495**
216496** aIdx[]:
216497** There is one entry in the aIdx[] array for each phrase in the query,
216498** the value of which is the offset within aPoslist[] following the last
216499** byte of the position list for the corresponding phrase.
216500*/
216501struct Fts5Sorter {
216502 sqlite3_stmt *pStmt;
216503 i64 iRowid; /* Current rowid */
216504 const u8 *aPoslist; /* Position lists for current row */
216505 int nIdx; /* Number of entries in aIdx[] */
216506 int aIdx[1]; /* Offsets into aPoslist for current row */
216507};
216508
216509
216510/*
216511** Virtual-table cursor object.
216512**
216513** iSpecial:
216514** If this is a 'special' query (refer to function fts5SpecialMatch()),
216515** then this variable contains the result of the query.
216516**
216517** iFirstRowid, iLastRowid:
216518** These variables are only used for FTS5_PLAN_MATCH cursors. Assuming the
216519** cursor iterates in ascending order of rowids, iFirstRowid is the lower
216520** limit of rowids to return, and iLastRowid the upper. In other words, the
216521** WHERE clause in the user's query might have been:
216522**
216523** <tbl> MATCH <expr> AND rowid BETWEEN $iFirstRowid AND $iLastRowid
216524**
216525** If the cursor iterates in descending order of rowid, iFirstRowid
216526** is the upper limit (i.e. the "first" rowid visited) and iLastRowid
216527** the lower.
216528*/
216529struct Fts5Cursor {
216530 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
216531 Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */
216532 int *aColumnSize; /* Values for xColumnSize() */
216533 i64 iCsrId; /* Cursor id */
216534
216535 /* Zero from this point onwards on cursor reset */
216536 int ePlan; /* FTS5_PLAN_XXX value */
216537 int bDesc; /* True for "ORDER BY rowid DESC" queries */
216538 i64 iFirstRowid; /* Return no rowids earlier than this */
216539 i64 iLastRowid; /* Return no rowids later than this */
216540 sqlite3_stmt *pStmt; /* Statement used to read %_content */
216541 Fts5Expr *pExpr; /* Expression for MATCH queries */
216542 Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */
216543 int csrflags; /* Mask of cursor flags (see below) */
216544 i64 iSpecial; /* Result of special query */
216545
216546 /* "rank" function. Populated on demand from vtab.xColumn(). */
216547 char *zRank; /* Custom rank function */
216548 char *zRankArgs; /* Custom rank function args */
216549 Fts5Auxiliary *pRank; /* Rank callback (or NULL) */
216550 int nRankArg; /* Number of trailing arguments for rank() */
216551 sqlite3_value **apRankArg; /* Array of trailing arguments */
216552 sqlite3_stmt *pRankArgStmt; /* Origin of objects in apRankArg[] */
216553
216554 /* Auxiliary data storage */
216555 Fts5Auxiliary *pAux; /* Currently executing extension function */
216556 Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */
216557
216558 /* Cache used by auxiliary functions xInst() and xInstCount() */
216559 Fts5PoslistReader *aInstIter; /* One for each phrase */
216560 int nInstAlloc; /* Size of aInst[] array (entries / 3) */
216561 int nInstCount; /* Number of phrase instances */
216562 int *aInst; /* 3 integers per phrase instance */
216563};
216564
216565/*
216566** Bits that make up the "idxNum" parameter passed indirectly by
216567** xBestIndex() to xFilter().
216568*/
216569#define FTS5_BI_MATCH 0x0001 /* <tbl> MATCH ? */
216570#define FTS5_BI_RANK 0x0002 /* rank MATCH ? */
216571#define FTS5_BI_ROWID_EQ 0x0004 /* rowid == ? */
216572#define FTS5_BI_ROWID_LE 0x0008 /* rowid <= ? */
216573#define FTS5_BI_ROWID_GE 0x0010 /* rowid >= ? */
216574
216575#define FTS5_BI_ORDER_RANK 0x0020
216576#define FTS5_BI_ORDER_ROWID 0x0040
216577#define FTS5_BI_ORDER_DESC 0x0080
216578
216579/*
216580** Values for Fts5Cursor.csrflags
216581*/
216582#define FTS5CSR_EOF 0x01
216583#define FTS5CSR_REQUIRE_CONTENT 0x02
216584#define FTS5CSR_REQUIRE_DOCSIZE 0x04
216585#define FTS5CSR_REQUIRE_INST 0x08
216586#define FTS5CSR_FREE_ZRANK 0x10
216587#define FTS5CSR_REQUIRE_RESEEK 0x20
216588#define FTS5CSR_REQUIRE_POSLIST 0x40
216589
216590#define BitFlagAllTest(x,y) (((x) & (y))==(y))
216591#define BitFlagTest(x,y) (((x) & (y))!=0)
216592
216593
216594/*
216595** Macros to Set(), Clear() and Test() cursor flags.
216596*/
216597#define CsrFlagSet(pCsr, flag) ((pCsr)->csrflags |= (flag))
216598#define CsrFlagClear(pCsr, flag) ((pCsr)->csrflags &= ~(flag))
216599#define CsrFlagTest(pCsr, flag) ((pCsr)->csrflags & (flag))
216600
216601struct Fts5Auxdata {
216602 Fts5Auxiliary *pAux; /* Extension to which this belongs */
216603 void *pPtr; /* Pointer value */
216604 void(*xDelete)(void*); /* Destructor */
216605 Fts5Auxdata *pNext; /* Next object in linked list */
216606};
216607
216608#ifdef SQLITE_DEBUG
216609#define FTS5_BEGIN 1
216610#define FTS5_SYNC 2
216611#define FTS5_COMMIT 3
216612#define FTS5_ROLLBACK 4
216613#define FTS5_SAVEPOINT 5
216614#define FTS5_RELEASE 6
216615#define FTS5_ROLLBACKTO 7
216616static void fts5CheckTransactionState(Fts5FullTable *p, int op, int iSavepoint){
216617 switch( op ){
216618 case FTS5_BEGIN:
216619 assert( p->ts.eState==0 )((void) (0));
216620 p->ts.eState = 1;
216621 p->ts.iSavepoint = -1;
216622 break;
216623
216624 case FTS5_SYNC:
216625 assert( p->ts.eState==1 )((void) (0));
216626 p->ts.eState = 2;
216627 break;
216628
216629 case FTS5_COMMIT:
216630 assert( p->ts.eState==2 )((void) (0));
216631 p->ts.eState = 0;
216632 break;
216633
216634 case FTS5_ROLLBACK:
216635 assert( p->ts.eState==1 || p->ts.eState==2 || p->ts.eState==0 )((void) (0));
216636 p->ts.eState = 0;
216637 break;
216638
216639 case FTS5_SAVEPOINT:
216640 assert( p->ts.eState==1 )((void) (0));
216641 assert( iSavepoint>=0 )((void) (0));
216642 assert( iSavepoint>=p->ts.iSavepoint )((void) (0));
216643 p->ts.iSavepoint = iSavepoint;
216644 break;
216645
216646 case FTS5_RELEASE:
216647 assert( p->ts.eState==1 )((void) (0));
216648 assert( iSavepoint>=0 )((void) (0));
216649 assert( iSavepoint<=p->ts.iSavepoint )((void) (0));
216650 p->ts.iSavepoint = iSavepoint-1;
216651 break;
216652
216653 case FTS5_ROLLBACKTO:
216654 assert( p->ts.eState==1 )((void) (0));
216655 assert( iSavepoint>=-1 )((void) (0));
216656 assert( iSavepoint<=p->ts.iSavepoint )((void) (0));
216657 p->ts.iSavepoint = iSavepoint;
216658 break;
216659 }
216660}
216661#else
216662# define fts5CheckTransactionState(x,y,z)
216663#endif
216664
216665/*
216666** Return true if pTab is a contentless table.
216667*/
216668static int fts5IsContentless(Fts5FullTable *pTab){
216669 return pTab->p.pConfig->eContent==FTS5_CONTENT_NONE;
216670}
216671
216672/*
216673** Delete a virtual table handle allocated by fts5InitVtab().
216674*/
216675static void fts5FreeVtab(Fts5FullTable *pTab){
216676 if( pTab ){
216677 sqlite3Fts5IndexClose(pTab->p.pIndex);
216678 sqlite3Fts5StorageClose(pTab->pStorage);
216679 sqlite3Fts5ConfigFree(pTab->p.pConfig);
216680 sqlite3_free(pTab);
216681 }
216682}
216683
216684/*
216685** The xDisconnect() virtual table method.
216686*/
216687static int fts5DisconnectMethod(sqlite3_vtab *pVtab){
216688 fts5FreeVtab((Fts5FullTable*)pVtab);
216689 return SQLITE_OK0;
216690}
216691
216692/*
216693** The xDestroy() virtual table method.
216694*/
216695static int fts5DestroyMethod(sqlite3_vtab *pVtab){
216696 Fts5Table *pTab = (Fts5Table*)pVtab;
216697 int rc = sqlite3Fts5DropAll(pTab->pConfig);
216698 if( rc==SQLITE_OK0 ){
216699 fts5FreeVtab((Fts5FullTable*)pVtab);
216700 }
216701 return rc;
216702}
216703
216704/*
216705** This function is the implementation of both the xConnect and xCreate
216706** methods of the FTS3 virtual table.
216707**
216708** The argv[] array contains the following:
216709**
216710** argv[0] -> module name ("fts5")
216711** argv[1] -> database name
216712** argv[2] -> table name
216713** argv[...] -> "column name" and other module argument fields.
216714*/
216715static int fts5InitVtab(
216716 int bCreate, /* True for xCreate, false for xConnect */
216717 sqlite3 *db, /* The SQLite database connection */
216718 void *pAux, /* Hash table containing tokenizers */
216719 int argc, /* Number of elements in argv array */
216720 const char * const *argv, /* xCreate/xConnect argument array */
216721 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
216722 char **pzErr /* Write any error message here */
216723){
216724 Fts5Global *pGlobal = (Fts5Global*)pAux;
216725 const char **azConfig = (const char**)argv;
216726 int rc = SQLITE_OK0; /* Return code */
216727 Fts5Config *pConfig = 0; /* Results of parsing argc/argv */
216728 Fts5FullTable *pTab = 0; /* New virtual table object */
216729
216730 /* Allocate the new vtab object and parse the configuration */
216731 pTab = (Fts5FullTable*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5FullTable));
216732 if( rc==SQLITE_OK0 ){
216733 rc = sqlite3Fts5ConfigParse(pGlobal, db, argc, azConfig, &pConfig, pzErr);
216734 assert( (rc==SQLITE_OK && *pzErr==0) || pConfig==0 )((void) (0));
216735 }
216736 if( rc==SQLITE_OK0 ){
216737 pTab->p.pConfig = pConfig;
216738 pTab->pGlobal = pGlobal;
216739 }
216740
216741 /* Open the index sub-system */
216742 if( rc==SQLITE_OK0 ){
216743 rc = sqlite3Fts5IndexOpen(pConfig, bCreate, &pTab->p.pIndex, pzErr);
216744 }
216745
216746 /* Open the storage sub-system */
216747 if( rc==SQLITE_OK0 ){
216748 rc = sqlite3Fts5StorageOpen(
216749 pConfig, pTab->p.pIndex, bCreate, &pTab->pStorage, pzErr
216750 );
216751 }
216752
216753 /* Call sqlite3_declare_vtab() */
216754 if( rc==SQLITE_OK0 ){
216755 rc = sqlite3Fts5ConfigDeclareVtab(pConfig);
216756 }
216757
216758 /* Load the initial configuration */
216759 if( rc==SQLITE_OK0 ){
216760 assert( pConfig->pzErrmsg==0 )((void) (0));
216761 pConfig->pzErrmsg = pzErr;
216762 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
216763 sqlite3Fts5IndexRollback(pTab->p.pIndex);
216764 pConfig->pzErrmsg = 0;
216765 }
216766
216767 if( rc!=SQLITE_OK0 ){
216768 fts5FreeVtab(pTab);
216769 pTab = 0;
216770 }else if( bCreate ){
216771 fts5CheckTransactionState(pTab, FTS5_BEGIN, 0);
216772 }
216773 *ppVTab = (sqlite3_vtab*)pTab;
216774 return rc;
216775}
216776
216777/*
216778** The xConnect() and xCreate() methods for the virtual table. All the
216779** work is done in function fts5InitVtab().
216780*/
216781static int fts5ConnectMethod(
216782 sqlite3 *db, /* Database connection */
216783 void *pAux, /* Pointer to tokenizer hash table */
216784 int argc, /* Number of elements in argv array */
216785 const char * const *argv, /* xCreate/xConnect argument array */
216786 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
216787 char **pzErr /* OUT: sqlite3_malloc'd error message */
216788){
216789 return fts5InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
216790}
216791static int fts5CreateMethod(
216792 sqlite3 *db, /* Database connection */
216793 void *pAux, /* Pointer to tokenizer hash table */
216794 int argc, /* Number of elements in argv array */
216795 const char * const *argv, /* xCreate/xConnect argument array */
216796 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
216797 char **pzErr /* OUT: sqlite3_malloc'd error message */
216798){
216799 return fts5InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
216800}
216801
216802/*
216803** The different query plans.
216804*/
216805#define FTS5_PLAN_MATCH 1 /* (<tbl> MATCH ?) */
216806#define FTS5_PLAN_SOURCE 2 /* A source cursor for SORTED_MATCH */
216807#define FTS5_PLAN_SPECIAL 3 /* An internal query */
216808#define FTS5_PLAN_SORTED_MATCH 4 /* (<tbl> MATCH ? ORDER BY rank) */
216809#define FTS5_PLAN_SCAN 5 /* No usable constraint */
216810#define FTS5_PLAN_ROWID 6 /* (rowid = ?) */
216811
216812/*
216813** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
216814** extension is currently being used by a version of SQLite too old to
216815** support index-info flags. In that case this function is a no-op.
216816*/
216817static void fts5SetUniqueFlag(sqlite3_index_info *pIdxInfo){
216818#if SQLITE_VERSION_NUMBER3029000>=3008012
216819#ifndef SQLITE_CORE1
216820 if( sqlite3_libversion_number()>=3008012 )
216821#endif
216822 {
216823 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE1;
216824 }
216825#endif
216826}
216827
216828/*
216829** Implementation of the xBestIndex method for FTS5 tables. Within the
216830** WHERE constraint, it searches for the following:
216831**
216832** 1. A MATCH constraint against the special column.
216833** 2. A MATCH constraint against the "rank" column.
216834** 3. An == constraint against the rowid column.
216835** 4. A < or <= constraint against the rowid column.
216836** 5. A > or >= constraint against the rowid column.
216837**
216838** Within the ORDER BY, either:
216839**
216840** 5. ORDER BY rank [ASC|DESC]
216841** 6. ORDER BY rowid [ASC|DESC]
216842**
216843** Costs are assigned as follows:
216844**
216845** a) If an unusable MATCH operator is present in the WHERE clause, the
216846** cost is unconditionally set to 1e50 (a really big number).
216847**
216848** a) If a MATCH operator is present, the cost depends on the other
216849** constraints also present. As follows:
216850**
216851** * No other constraints: cost=1000.0
216852** * One rowid range constraint: cost=750.0
216853** * Both rowid range constraints: cost=500.0
216854** * An == rowid constraint: cost=100.0
216855**
216856** b) Otherwise, if there is no MATCH:
216857**
216858** * No other constraints: cost=1000000.0
216859** * One rowid range constraint: cost=750000.0
216860** * Both rowid range constraints: cost=250000.0
216861** * An == rowid constraint: cost=10.0
216862**
216863** Costs are not modified by the ORDER BY clause.
216864*/
216865static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
216866 Fts5Table *pTab = (Fts5Table*)pVTab;
216867 Fts5Config *pConfig = pTab->pConfig;
216868 const int nCol = pConfig->nCol;
216869 int idxFlags = 0; /* Parameter passed through to xFilter() */
216870 int bHasMatch;
216871 int iNext;
216872 int i;
216873
216874 struct Constraint {
216875 int op; /* Mask against sqlite3_index_constraint.op */
216876 int fts5op; /* FTS5 mask for idxFlags */
216877 int iCol; /* 0==rowid, 1==tbl, 2==rank */
216878 int omit; /* True to omit this if found */
216879 int iConsIndex; /* Index in pInfo->aConstraint[] */
216880 } aConstraint[] = {
216881 {SQLITE_INDEX_CONSTRAINT_MATCH64|SQLITE_INDEX_CONSTRAINT_EQ2,
216882 FTS5_BI_MATCH, 1, 1, -1},
216883 {SQLITE_INDEX_CONSTRAINT_MATCH64|SQLITE_INDEX_CONSTRAINT_EQ2,
216884 FTS5_BI_RANK, 2, 1, -1},
216885 {SQLITE_INDEX_CONSTRAINT_EQ2, FTS5_BI_ROWID_EQ, 0, 0, -1},
216886 {SQLITE_INDEX_CONSTRAINT_LT16|SQLITE_INDEX_CONSTRAINT_LE8,
216887 FTS5_BI_ROWID_LE, 0, 0, -1},
216888 {SQLITE_INDEX_CONSTRAINT_GT4|SQLITE_INDEX_CONSTRAINT_GE32,
216889 FTS5_BI_ROWID_GE, 0, 0, -1},
216890 };
216891
216892 int aColMap[3];
216893 aColMap[0] = -1;
216894 aColMap[1] = nCol;
216895 aColMap[2] = nCol+1;
216896
216897 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
216898 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
216899 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
216900 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
216901 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH )((void) (0));
216902
216903 /* Set idxFlags flags for all WHERE clause terms that will be used. */
216904 for(i=0; i<pInfo->nConstraint; i++){
216905 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
216906 int iCol = p->iColumn;
216907
216908 if( (p->op==SQLITE_INDEX_CONSTRAINT_MATCH64 && iCol>=0 && iCol<=nCol)
216909 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ2 && iCol==nCol)
216910 ){
216911 /* A MATCH operator or equivalent */
216912 if( p->usable ){
216913 idxFlags = (idxFlags & 0xFFFF) | FTS5_BI_MATCH | (iCol << 16);
216914 aConstraint[0].iConsIndex = i;
216915 }else{
216916 /* As there exists an unusable MATCH constraint this is an
216917 ** unusable plan. Set a prohibitively high cost. */
216918 pInfo->estimatedCost = 1e50;
216919 return SQLITE_OK0;
216920 }
216921 }else if( p->op<=SQLITE_INDEX_CONSTRAINT_MATCH64 ){
216922 int j;
216923 for(j=1; j<ArraySize(aConstraint)((int)(sizeof(aConstraint)/sizeof(aConstraint[0]))); j++){
216924 struct Constraint *pC = &aConstraint[j];
216925 if( iCol==aColMap[pC->iCol] && (p->op & pC->op) && p->usable ){
216926 pC->iConsIndex = i;
216927 idxFlags |= pC->fts5op;
216928 }
216929 }
216930 }
216931 }
216932
216933 /* Set idxFlags flags for the ORDER BY clause */
216934 if( pInfo->nOrderBy==1 ){
216935 int iSort = pInfo->aOrderBy[0].iColumn;
216936 if( iSort==(pConfig->nCol+1) && BitFlagTest(idxFlags, FTS5_BI_MATCH) ){
216937 idxFlags |= FTS5_BI_ORDER_RANK;
216938 }else if( iSort==-1 ){
216939 idxFlags |= FTS5_BI_ORDER_ROWID;
216940 }
216941 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
216942 pInfo->orderByConsumed = 1;
216943 if( pInfo->aOrderBy[0].desc ){
216944 idxFlags |= FTS5_BI_ORDER_DESC;
216945 }
216946 }
216947 }
216948
216949 /* Calculate the estimated cost based on the flags set in idxFlags. */
216950 bHasMatch = BitFlagTest(idxFlags, FTS5_BI_MATCH);
216951 if( BitFlagTest(idxFlags, FTS5_BI_ROWID_EQ) ){
216952 pInfo->estimatedCost = bHasMatch ? 100.0 : 10.0;
216953 if( bHasMatch==0 ) fts5SetUniqueFlag(pInfo);
216954 }else if( BitFlagAllTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
216955 pInfo->estimatedCost = bHasMatch ? 500.0 : 250000.0;
216956 }else if( BitFlagTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
216957 pInfo->estimatedCost = bHasMatch ? 750.0 : 750000.0;
216958 }else{
216959 pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
216960 }
216961
216962 /* Assign argvIndex values to each constraint in use. */
216963 iNext = 1;
216964 for(i=0; i<ArraySize(aConstraint)((int)(sizeof(aConstraint)/sizeof(aConstraint[0]))); i++){
216965 struct Constraint *pC = &aConstraint[i];
216966 if( pC->iConsIndex>=0 ){
216967 pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
216968 pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
216969 }
216970 }
216971
216972 pInfo->idxNum = idxFlags;
216973 return SQLITE_OK0;
216974}
216975
216976static int fts5NewTransaction(Fts5FullTable *pTab){
216977 Fts5Cursor *pCsr;
216978 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
216979 if( pCsr->base.pVtab==(sqlite3_vtab*)pTab ) return SQLITE_OK0;
216980 }
216981 return sqlite3Fts5StorageReset(pTab->pStorage);
216982}
216983
216984/*
216985** Implementation of xOpen method.
216986*/
216987static int fts5OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
216988 Fts5FullTable *pTab = (Fts5FullTable*)pVTab;
216989 Fts5Config *pConfig = pTab->p.pConfig;
216990 Fts5Cursor *pCsr = 0; /* New cursor object */
216991 sqlite3_int64 nByte; /* Bytes of space to allocate */
216992 int rc; /* Return code */
216993
216994 rc = fts5NewTransaction(pTab);
216995 if( rc==SQLITE_OK0 ){
216996 nByte = sizeof(Fts5Cursor) + pConfig->nCol * sizeof(int);
216997 pCsr = (Fts5Cursor*)sqlite3_malloc64(nByte);
216998 if( pCsr ){
216999 Fts5Global *pGlobal = pTab->pGlobal;
217000 memset(pCsr, 0, (size_t)nByte);
217001 pCsr->aColumnSize = (int*)&pCsr[1];
217002 pCsr->pNext = pGlobal->pCsr;
217003 pGlobal->pCsr = pCsr;
217004 pCsr->iCsrId = ++pGlobal->iNextId;
217005 }else{
217006 rc = SQLITE_NOMEM7;
217007 }
217008 }
217009 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
217010 return rc;
217011}
217012
217013static int fts5StmtType(Fts5Cursor *pCsr){
217014 if( pCsr->ePlan==FTS5_PLAN_SCAN ){
217015 return (pCsr->bDesc) ? FTS5_STMT_SCAN_DESC : FTS5_STMT_SCAN_ASC;
217016 }
217017 return FTS5_STMT_LOOKUP;
217018}
217019
217020/*
217021** This function is called after the cursor passed as the only argument
217022** is moved to point at a different row. It clears all cached data
217023** specific to the previous row stored by the cursor object.
217024*/
217025static void fts5CsrNewrow(Fts5Cursor *pCsr){
217026 CsrFlagSet(pCsr,
217027 FTS5CSR_REQUIRE_CONTENT
217028 | FTS5CSR_REQUIRE_DOCSIZE
217029 | FTS5CSR_REQUIRE_INST
217030 | FTS5CSR_REQUIRE_POSLIST
217031 );
217032}
217033
217034static void fts5FreeCursorComponents(Fts5Cursor *pCsr){
217035 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
217036 Fts5Auxdata *pData;
217037 Fts5Auxdata *pNext;
217038
217039 sqlite3_free(pCsr->aInstIter);
217040 sqlite3_free(pCsr->aInst);
217041 if( pCsr->pStmt ){
217042 int eStmt = fts5StmtType(pCsr);
217043 sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt);
217044 }
217045 if( pCsr->pSorter ){
217046 Fts5Sorter *pSorter = pCsr->pSorter;
217047 sqlite3_finalize(pSorter->pStmt);
217048 sqlite3_free(pSorter);
217049 }
217050
217051 if( pCsr->ePlan!=FTS5_PLAN_SOURCE ){
217052 sqlite3Fts5ExprFree(pCsr->pExpr);
217053 }
217054
217055 for(pData=pCsr->pAuxdata; pData; pData=pNext){
217056 pNext = pData->pNext;
217057 if( pData->xDelete ) pData->xDelete(pData->pPtr);
217058 sqlite3_free(pData);
217059 }
217060
217061 sqlite3_finalize(pCsr->pRankArgStmt);
217062 sqlite3_free(pCsr->apRankArg);
217063
217064 if( CsrFlagTest(pCsr, FTS5CSR_FREE_ZRANK) ){
217065 sqlite3_free(pCsr->zRank);
217066 sqlite3_free(pCsr->zRankArgs);
217067 }
217068
217069 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan - (u8*)pCsr));
217070}
217071
217072
217073/*
217074** Close the cursor. For additional information see the documentation
217075** on the xClose method of the virtual table interface.
217076*/
217077static int fts5CloseMethod(sqlite3_vtab_cursor *pCursor){
217078 if( pCursor ){
217079 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
217080 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
217081 Fts5Cursor **pp;
217082
217083 fts5FreeCursorComponents(pCsr);
217084 /* Remove the cursor from the Fts5Global.pCsr list */
217085 for(pp=&pTab->pGlobal->pCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
217086 *pp = pCsr->pNext;
217087
217088 sqlite3_free(pCsr);
217089 }
217090 return SQLITE_OK0;
217091}
217092
217093static int fts5SorterNext(Fts5Cursor *pCsr){
217094 Fts5Sorter *pSorter = pCsr->pSorter;
217095 int rc;
217096
217097 rc = sqlite3_step(pSorter->pStmt);
217098 if( rc==SQLITE_DONE101 ){
217099 rc = SQLITE_OK0;
217100 CsrFlagSet(pCsr, FTS5CSR_EOF);
217101 }else if( rc==SQLITE_ROW100 ){
217102 const u8 *a;
217103 const u8 *aBlob;
217104 int nBlob;
217105 int i;
217106 int iOff = 0;
217107 rc = SQLITE_OK0;
217108
217109 pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0);
217110 nBlob = sqlite3_column_bytes(pSorter->pStmt, 1);
217111 aBlob = a = sqlite3_column_blob(pSorter->pStmt, 1);
217112
217113 /* nBlob==0 in detail=none mode. */
217114 if( nBlob>0 ){
217115 for(i=0; i<(pSorter->nIdx-1); i++){
217116 int iVal;
217117 a += fts5GetVarint32(a, iVal);
217118 iOff += iVal;
217119 pSorter->aIdx[i] = iOff;
217120 }
217121 pSorter->aIdx[i] = &aBlob[nBlob] - a;
217122 pSorter->aPoslist = a;
217123 }
217124
217125 fts5CsrNewrow(pCsr);
217126 }
217127
217128 return rc;
217129}
217130
217131
217132/*
217133** Set the FTS5CSR_REQUIRE_RESEEK flag on all FTS5_PLAN_MATCH cursors
217134** open on table pTab.
217135*/
217136static void fts5TripCursors(Fts5FullTable *pTab){
217137 Fts5Cursor *pCsr;
217138 for(pCsr=pTab->pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
217139 if( pCsr->ePlan==FTS5_PLAN_MATCH
217140 && pCsr->base.pVtab==(sqlite3_vtab*)pTab
217141 ){
217142 CsrFlagSet(pCsr, FTS5CSR_REQUIRE_RESEEK);
217143 }
217144 }
217145}
217146
217147/*
217148** If the REQUIRE_RESEEK flag is set on the cursor passed as the first
217149** argument, close and reopen all Fts5IndexIter iterators that the cursor
217150** is using. Then attempt to move the cursor to a rowid equal to or laster
217151** (in the cursors sort order - ASC or DESC) than the current rowid.
217152**
217153** If the new rowid is not equal to the old, set output parameter *pbSkip
217154** to 1 before returning. Otherwise, leave it unchanged.
217155**
217156** Return SQLITE_OK if successful or if no reseek was required, or an
217157** error code if an error occurred.
217158*/
217159static int fts5CursorReseek(Fts5Cursor *pCsr, int *pbSkip){
217160 int rc = SQLITE_OK0;
217161 assert( *pbSkip==0 )((void) (0));
217162 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_RESEEK) ){
217163 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
217164 int bDesc = pCsr->bDesc;
217165 i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
217166
217167 rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->p.pIndex, iRowid, bDesc);
217168 if( rc==SQLITE_OK0 && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
217169 *pbSkip = 1;
217170 }
217171
217172 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
217173 fts5CsrNewrow(pCsr);
217174 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
217175 CsrFlagSet(pCsr, FTS5CSR_EOF);
217176 *pbSkip = 1;
217177 }
217178 }
217179 return rc;
217180}
217181
217182
217183/*
217184** Advance the cursor to the next row in the table that matches the
217185** search criteria.
217186**
217187** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
217188** even if we reach end-of-file. The fts5EofMethod() will be called
217189** subsequently to determine whether or not an EOF was hit.
217190*/
217191static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
217192 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
217193 int rc;
217194
217195 assert( (pCsr->ePlan<3)==((void) (0))
217196 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)((void) (0))
217197 )((void) (0));
217198 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) )((void) (0));
217199
217200 if( pCsr->ePlan<3 ){
217201 int bSkip = 0;
217202 if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
217203 rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
217204 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
217205 fts5CsrNewrow(pCsr);
217206 }else{
217207 switch( pCsr->ePlan ){
217208 case FTS5_PLAN_SPECIAL: {
217209 CsrFlagSet(pCsr, FTS5CSR_EOF);
217210 rc = SQLITE_OK0;
217211 break;
217212 }
217213
217214 case FTS5_PLAN_SORTED_MATCH: {
217215 rc = fts5SorterNext(pCsr);
217216 break;
217217 }
217218
217219 default:
217220 rc = sqlite3_step(pCsr->pStmt);
217221 if( rc!=SQLITE_ROW100 ){
217222 CsrFlagSet(pCsr, FTS5CSR_EOF);
217223 rc = sqlite3_reset(pCsr->pStmt);
217224 }else{
217225 rc = SQLITE_OK0;
217226 }
217227 break;
217228 }
217229 }
217230
217231 return rc;
217232}
217233
217234
217235static int fts5PrepareStatement(
217236 sqlite3_stmt **ppStmt,
217237 Fts5Config *pConfig,
217238 const char *zFmt,
217239 ...
217240){
217241 sqlite3_stmt *pRet = 0;
217242 int rc;
217243 char *zSql;
217244 va_list ap;
217245
217246 va_start(ap, zFmt)__builtin_va_start(ap, zFmt);
217247 zSql = sqlite3_vmprintf(zFmt, ap);
217248 if( zSql==0 ){
217249 rc = SQLITE_NOMEM7;
217250 }else{
217251 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
217252 SQLITE_PREPARE_PERSISTENT0x01, &pRet, 0);
217253 if( rc!=SQLITE_OK0 ){
217254 *pConfig->pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(pConfig->db));
217255 }
217256 sqlite3_free(zSql);
217257 }
217258
217259 va_end(ap)__builtin_va_end(ap);
217260 *ppStmt = pRet;
217261 return rc;
217262}
217263
217264static int fts5CursorFirstSorted(
217265 Fts5FullTable *pTab,
217266 Fts5Cursor *pCsr,
217267 int bDesc
217268){
217269 Fts5Config *pConfig = pTab->p.pConfig;
217270 Fts5Sorter *pSorter;
217271 int nPhrase;
217272 sqlite3_int64 nByte;
217273 int rc;
217274 const char *zRank = pCsr->zRank;
217275 const char *zRankArgs = pCsr->zRankArgs;
217276
217277 nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
217278 nByte = sizeof(Fts5Sorter) + sizeof(int) * (nPhrase-1);
217279 pSorter = (Fts5Sorter*)sqlite3_malloc64(nByte);
217280 if( pSorter==0 ) return SQLITE_NOMEM7;
217281 memset(pSorter, 0, (size_t)nByte);
217282 pSorter->nIdx = nPhrase;
217283
217284 /* TODO: It would be better to have some system for reusing statement
217285 ** handles here, rather than preparing a new one for each query. But that
217286 ** is not possible as SQLite reference counts the virtual table objects.
217287 ** And since the statement required here reads from this very virtual
217288 ** table, saving it creates a circular reference.
217289 **
217290 ** If SQLite a built-in statement cache, this wouldn't be a problem. */
217291 rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
217292 "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(%s%s%s) %s",
217293 pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
217294 (zRankArgs ? ", " : ""),
217295 (zRankArgs ? zRankArgs : ""),
217296 bDesc ? "DESC" : "ASC"
217297 );
217298
217299 pCsr->pSorter = pSorter;
217300 if( rc==SQLITE_OK0 ){
217301 assert( pTab->pSortCsr==0 )((void) (0));
217302 pTab->pSortCsr = pCsr;
217303 rc = fts5SorterNext(pCsr);
217304 pTab->pSortCsr = 0;
217305 }
217306
217307 if( rc!=SQLITE_OK0 ){
217308 sqlite3_finalize(pSorter->pStmt);
217309 sqlite3_free(pSorter);
217310 pCsr->pSorter = 0;
217311 }
217312
217313 return rc;
217314}
217315
217316static int fts5CursorFirst(Fts5FullTable *pTab, Fts5Cursor *pCsr, int bDesc){
217317 int rc;
217318 Fts5Expr *pExpr = pCsr->pExpr;
217319 rc = sqlite3Fts5ExprFirst(pExpr, pTab->p.pIndex, pCsr->iFirstRowid, bDesc);
217320 if( sqlite3Fts5ExprEof(pExpr) ){
217321 CsrFlagSet(pCsr, FTS5CSR_EOF);
217322 }
217323 fts5CsrNewrow(pCsr);
217324 return rc;
217325}
217326
217327/*
217328** Process a "special" query. A special query is identified as one with a
217329** MATCH expression that begins with a '*' character. The remainder of
217330** the text passed to the MATCH operator are used as the special query
217331** parameters.
217332*/
217333static int fts5SpecialMatch(
217334 Fts5FullTable *pTab,
217335 Fts5Cursor *pCsr,
217336 const char *zQuery
217337){
217338 int rc = SQLITE_OK0; /* Return code */
217339 const char *z = zQuery; /* Special query text */
217340 int n; /* Number of bytes in text at z */
217341
217342 while( z[0]==' ' ) z++;
217343 for(n=0; z[n] && z[n]!=' '; n++);
217344
217345 assert( pTab->p.base.zErrMsg==0 )((void) (0));
217346 pCsr->ePlan = FTS5_PLAN_SPECIAL;
217347
217348 if( 0==sqlite3_strnicmp("reads", z, n) ){
217349 pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->p.pIndex);
217350 }
217351 else if( 0==sqlite3_strnicmp("id", z, n) ){
217352 pCsr->iSpecial = pCsr->iCsrId;
217353 }
217354 else{
217355 /* An unrecognized directive. Return an error message. */
217356 pTab->p.base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
217357 rc = SQLITE_ERROR1;
217358 }
217359
217360 return rc;
217361}
217362
217363/*
217364** Search for an auxiliary function named zName that can be used with table
217365** pTab. If one is found, return a pointer to the corresponding Fts5Auxiliary
217366** structure. Otherwise, if no such function exists, return NULL.
217367*/
217368static Fts5Auxiliary *fts5FindAuxiliary(Fts5FullTable *pTab, const char *zName){
217369 Fts5Auxiliary *pAux;
217370
217371 for(pAux=pTab->pGlobal->pAux; pAux; pAux=pAux->pNext){
217372 if( sqlite3_stricmp(zName, pAux->zFunc)==0 ) return pAux;
217373 }
217374
217375 /* No function of the specified name was found. Return 0. */
217376 return 0;
217377}
217378
217379
217380static int fts5FindRankFunction(Fts5Cursor *pCsr){
217381 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
217382 Fts5Config *pConfig = pTab->p.pConfig;
217383 int rc = SQLITE_OK0;
217384 Fts5Auxiliary *pAux = 0;
217385 const char *zRank = pCsr->zRank;
217386 const char *zRankArgs = pCsr->zRankArgs;
217387
217388 if( zRankArgs ){
217389 char *zSql = sqlite3Fts5Mprintf(&rc, "SELECT %s", zRankArgs);
217390 if( zSql ){
217391 sqlite3_stmt *pStmt = 0;
217392 rc = sqlite3_prepare_v3(pConfig->db, zSql, -1,
217393 SQLITE_PREPARE_PERSISTENT0x01, &pStmt, 0);
217394 sqlite3_free(zSql);
217395 assert( rc==SQLITE_OK || pCsr->pRankArgStmt==0 )((void) (0));
217396 if( rc==SQLITE_OK0 ){
217397 if( SQLITE_ROW100==sqlite3_step(pStmt) ){
217398 sqlite3_int64 nByte;
217399 pCsr->nRankArg = sqlite3_column_count(pStmt);
217400 nByte = sizeof(sqlite3_value*)*pCsr->nRankArg;
217401 pCsr->apRankArg = (sqlite3_value**)sqlite3Fts5MallocZero(&rc, nByte);
217402 if( rc==SQLITE_OK0 ){
217403 int i;
217404 for(i=0; i<pCsr->nRankArg; i++){
217405 pCsr->apRankArg[i] = sqlite3_column_value(pStmt, i);
217406 }
217407 }
217408 pCsr->pRankArgStmt = pStmt;
217409 }else{
217410 rc = sqlite3_finalize(pStmt);
217411 assert( rc!=SQLITE_OK )((void) (0));
217412 }
217413 }
217414 }
217415 }
217416
217417 if( rc==SQLITE_OK0 ){
217418 pAux = fts5FindAuxiliary(pTab, zRank);
217419 if( pAux==0 ){
217420 assert( pTab->p.base.zErrMsg==0 )((void) (0));
217421 pTab->p.base.zErrMsg = sqlite3_mprintf("no such function: %s", zRank);
217422 rc = SQLITE_ERROR1;
217423 }
217424 }
217425
217426 pCsr->pRank = pAux;
217427 return rc;
217428}
217429
217430
217431static int fts5CursorParseRank(
217432 Fts5Config *pConfig,
217433 Fts5Cursor *pCsr,
217434 sqlite3_value *pRank
217435){
217436 int rc = SQLITE_OK0;
217437 if( pRank ){
217438 const char *z = (const char*)sqlite3_value_text(pRank);
217439 char *zRank = 0;
217440 char *zRankArgs = 0;
217441
217442 if( z==0 ){
217443 if( sqlite3_value_type(pRank)==SQLITE_NULL5 ) rc = SQLITE_ERROR1;
217444 }else{
217445 rc = sqlite3Fts5ConfigParseRank(z, &zRank, &zRankArgs);
217446 }
217447 if( rc==SQLITE_OK0 ){
217448 pCsr->zRank = zRank;
217449 pCsr->zRankArgs = zRankArgs;
217450 CsrFlagSet(pCsr, FTS5CSR_FREE_ZRANK);
217451 }else if( rc==SQLITE_ERROR1 ){
217452 pCsr->base.pVtab->zErrMsg = sqlite3_mprintf(
217453 "parse error in rank function: %s", z
217454 );
217455 }
217456 }else{
217457 if( pConfig->zRank ){
217458 pCsr->zRank = (char*)pConfig->zRank;
217459 pCsr->zRankArgs = (char*)pConfig->zRankArgs;
217460 }else{
217461 pCsr->zRank = (char*)FTS5_DEFAULT_RANK;
217462 pCsr->zRankArgs = 0;
217463 }
217464 }
217465 return rc;
217466}
217467
217468static i64 fts5GetRowidLimit(sqlite3_value *pVal, i64 iDefault){
217469 if( pVal ){
217470 int eType = sqlite3_value_numeric_type(pVal);
217471 if( eType==SQLITE_INTEGER1 ){
217472 return sqlite3_value_int64(pVal);
217473 }
217474 }
217475 return iDefault;
217476}
217477
217478/*
217479** This is the xFilter interface for the virtual table. See
217480** the virtual table xFilter method documentation for additional
217481** information.
217482**
217483** There are three possible query strategies:
217484**
217485** 1. Full-text search using a MATCH operator.
217486** 2. A by-rowid lookup.
217487** 3. A full-table scan.
217488*/
217489static int fts5FilterMethod(
217490 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
217491 int idxNum, /* Strategy index */
217492 const char *zUnused, /* Unused */
217493 int nVal, /* Number of elements in apVal */
217494 sqlite3_value **apVal /* Arguments for the indexing scheme */
217495){
217496 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
217497 Fts5Config *pConfig = pTab->p.pConfig;
217498 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
217499 int rc = SQLITE_OK0; /* Error code */
217500 int iVal = 0; /* Counter for apVal[] */
217501 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
217502 int bOrderByRank; /* True if ORDER BY rank */
217503 sqlite3_value *pMatch = 0; /* <tbl> MATCH ? expression (or NULL) */
217504 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
217505 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
217506 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
217507 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
217508 int iCol; /* Column on LHS of MATCH operator */
217509 char **pzErrmsg = pConfig->pzErrmsg;
217510
217511 UNUSED_PARAM(zUnused);
217512 UNUSED_PARAM(nVal);
217513
217514 if( pCsr->ePlan ){
217515 fts5FreeCursorComponents(pCsr);
217516 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
217517 }
217518
217519 assert( pCsr->pStmt==0 )((void) (0));
217520 assert( pCsr->pExpr==0 )((void) (0));
217521 assert( pCsr->csrflags==0 )((void) (0));
217522 assert( pCsr->pRank==0 )((void) (0));
217523 assert( pCsr->zRank==0 )((void) (0));
217524 assert( pCsr->zRankArgs==0 )((void) (0));
217525
217526 assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg )((void) (0));
217527 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
217528
217529 /* Decode the arguments passed through to this function.
217530 **
217531 ** Note: The following set of if(...) statements must be in the same
217532 ** order as the corresponding entries in the struct at the top of
217533 ** fts5BestIndexMethod(). */
217534 if( BitFlagTest(idxNum, FTS5_BI_MATCH) ) pMatch = apVal[iVal++];
217535 if( BitFlagTest(idxNum, FTS5_BI_RANK) ) pRank = apVal[iVal++];
217536 if( BitFlagTest(idxNum, FTS5_BI_ROWID_EQ) ) pRowidEq = apVal[iVal++];
217537 if( BitFlagTest(idxNum, FTS5_BI_ROWID_LE) ) pRowidLe = apVal[iVal++];
217538 if( BitFlagTest(idxNum, FTS5_BI_ROWID_GE) ) pRowidGe = apVal[iVal++];
217539 iCol = (idxNum>>16);
217540 assert( iCol>=0 && iCol<=pConfig->nCol )((void) (0));
217541 assert( iVal==nVal )((void) (0));
217542 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
217543 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
217544
217545 /* Set the cursor upper and lower rowid limits. Only some strategies
217546 ** actually use them. This is ok, as the xBestIndex() method leaves the
217547 ** sqlite3_index_constraint.omit flag clear for range constraints
217548 ** on the rowid field. */
217549 if( pRowidEq ){
217550 pRowidLe = pRowidGe = pRowidEq;
217551 }
217552 if( bDesc ){
217553 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
217554 pCsr->iLastRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
217555 }else{
217556 pCsr->iLastRowid = fts5GetRowidLimit(pRowidLe, LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32)));
217557 pCsr->iFirstRowid = fts5GetRowidLimit(pRowidGe, SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32))));
217558 }
217559
217560 if( pTab->pSortCsr ){
217561 /* If pSortCsr is non-NULL, then this call is being made as part of
217562 ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
217563 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
217564 ** return results to the user for this query. The current cursor
217565 ** (pCursor) is used to execute the query issued by function
217566 ** fts5CursorFirstSorted() above. */
217567 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 )((void) (0));
217568 assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 )((void) (0));
217569 assert( pCsr->iLastRowid==LARGEST_INT64 )((void) (0));
217570 assert( pCsr->iFirstRowid==SMALLEST_INT64 )((void) (0));
217571 if( pTab->pSortCsr->bDesc ){
217572 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
217573 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
217574 }else{
217575 pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
217576 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
217577 }
217578 pCsr->ePlan = FTS5_PLAN_SOURCE;
217579 pCsr->pExpr = pTab->pSortCsr->pExpr;
217580 rc = fts5CursorFirst(pTab, pCsr, bDesc);
217581 }else if( pMatch ){
217582 const char *zExpr = (const char*)sqlite3_value_text(apVal[0]);
217583 if( zExpr==0 ) zExpr = "";
217584
217585 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
217586 if( rc==SQLITE_OK0 ){
217587 if( zExpr[0]=='*' ){
217588 /* The user has issued a query of the form "MATCH '*...'". This
217589 ** indicates that the MATCH expression is not a full text query,
217590 ** but a request for an internal parameter. */
217591 rc = fts5SpecialMatch(pTab, pCsr, &zExpr[1]);
217592 }else{
217593 char **pzErr = &pTab->p.base.zErrMsg;
217594 rc = sqlite3Fts5ExprNew(pConfig, iCol, zExpr, &pCsr->pExpr, pzErr);
217595 if( rc==SQLITE_OK0 ){
217596 if( bOrderByRank ){
217597 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
217598 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
217599 }else{
217600 pCsr->ePlan = FTS5_PLAN_MATCH;
217601 rc = fts5CursorFirst(pTab, pCsr, bDesc);
217602 }
217603 }
217604 }
217605 }
217606 }else if( pConfig->zContent==0 ){
217607 *pConfig->pzErrmsg = sqlite3_mprintf(
217608 "%s: table does not support scanning", pConfig->zName
217609 );
217610 rc = SQLITE_ERROR1;
217611 }else{
217612 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
217613 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
217614 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
217615 rc = sqlite3Fts5StorageStmt(
217616 pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
217617 );
217618 if( rc==SQLITE_OK0 ){
217619 if( pCsr->ePlan==FTS5_PLAN_ROWID ){
217620 sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
217621 }else{
217622 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
217623 sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
217624 }
217625 rc = fts5NextMethod(pCursor);
217626 }
217627 }
217628
217629 pConfig->pzErrmsg = pzErrmsg;
217630 return rc;
217631}
217632
217633/*
217634** This is the xEof method of the virtual table. SQLite calls this
217635** routine to find out if it has reached the end of a result set.
217636*/
217637static int fts5EofMethod(sqlite3_vtab_cursor *pCursor){
217638 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
217639 return (CsrFlagTest(pCsr, FTS5CSR_EOF) ? 1 : 0);
217640}
217641
217642/*
217643** Return the rowid that the cursor currently points to.
217644*/
217645static i64 fts5CursorRowid(Fts5Cursor *pCsr){
217646 assert( pCsr->ePlan==FTS5_PLAN_MATCH((void) (0))
217647 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH((void) (0))
217648 || pCsr->ePlan==FTS5_PLAN_SOURCE((void) (0))
217649 )((void) (0));
217650 if( pCsr->pSorter ){
217651 return pCsr->pSorter->iRowid;
217652 }else{
217653 return sqlite3Fts5ExprRowid(pCsr->pExpr);
217654 }
217655}
217656
217657/*
217658** This is the xRowid method. The SQLite core calls this routine to
217659** retrieve the rowid for the current row of the result set. fts5
217660** exposes %_content.rowid as the rowid for the virtual table. The
217661** rowid should be written to *pRowid.
217662*/
217663static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
217664 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
217665 int ePlan = pCsr->ePlan;
217666
217667 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 )((void) (0));
217668 switch( ePlan ){
217669 case FTS5_PLAN_SPECIAL:
217670 *pRowid = 0;
217671 break;
217672
217673 case FTS5_PLAN_SOURCE:
217674 case FTS5_PLAN_MATCH:
217675 case FTS5_PLAN_SORTED_MATCH:
217676 *pRowid = fts5CursorRowid(pCsr);
217677 break;
217678
217679 default:
217680 *pRowid = sqlite3_column_int64(pCsr->pStmt, 0);
217681 break;
217682 }
217683
217684 return SQLITE_OK0;
217685}
217686
217687/*
217688** If the cursor requires seeking (bSeekRequired flag is set), seek it.
217689** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
217690**
217691** If argument bErrormsg is true and an error occurs, an error message may
217692** be left in sqlite3_vtab.zErrMsg.
217693*/
217694static int fts5SeekCursor(Fts5Cursor *pCsr, int bErrormsg){
217695 int rc = SQLITE_OK0;
217696
217697 /* If the cursor does not yet have a statement handle, obtain one now. */
217698 if( pCsr->pStmt==0 ){
217699 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
217700 int eStmt = fts5StmtType(pCsr);
217701 rc = sqlite3Fts5StorageStmt(
217702 pTab->pStorage, eStmt, &pCsr->pStmt, (bErrormsg?&pTab->p.base.zErrMsg:0)
217703 );
217704 assert( rc!=SQLITE_OK || pTab->p.base.zErrMsg==0 )((void) (0));
217705 assert( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) )((void) (0));
217706 }
217707
217708 if( rc==SQLITE_OK0 && CsrFlagTest(pCsr, FTS5CSR_REQUIRE_CONTENT) ){
217709 assert( pCsr->pExpr )((void) (0));
217710 sqlite3_reset(pCsr->pStmt);
217711 sqlite3_bind_int64(pCsr->pStmt, 1, fts5CursorRowid(pCsr));
217712 rc = sqlite3_step(pCsr->pStmt);
217713 if( rc==SQLITE_ROW100 ){
217714 rc = SQLITE_OK0;
217715 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
217716 }else{
217717 rc = sqlite3_reset(pCsr->pStmt);
217718 if( rc==SQLITE_OK0 ){
217719 rc = FTS5_CORRUPT;
217720 }
217721 }
217722 }
217723 return rc;
217724}
217725
217726static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
217727 va_list ap; /* ... printf arguments */
217728 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
217729 assert( p->p.base.zErrMsg==0 )((void) (0));
217730 p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
217731 va_end(ap)__builtin_va_end(ap);
217732}
217733
217734/*
217735** This function is called to handle an FTS INSERT command. In other words,
217736** an INSERT statement of the form:
217737**
217738** INSERT INTO fts(fts) VALUES($pCmd)
217739** INSERT INTO fts(fts, rank) VALUES($pCmd, $pVal)
217740**
217741** Argument pVal is the value assigned to column "fts" by the INSERT
217742** statement. This function returns SQLITE_OK if successful, or an SQLite
217743** error code if an error occurs.
217744**
217745** The commands implemented by this function are documented in the "Special
217746** INSERT Directives" section of the documentation. It should be updated if
217747** more commands are added to this function.
217748*/
217749static int fts5SpecialInsert(
217750 Fts5FullTable *pTab, /* Fts5 table object */
217751 const char *zCmd, /* Text inserted into table-name column */
217752 sqlite3_value *pVal /* Value inserted into rank column */
217753){
217754 Fts5Config *pConfig = pTab->p.pConfig;
217755 int rc = SQLITE_OK0;
217756 int bError = 0;
217757
217758 if( 0==sqlite3_stricmp("delete-all", zCmd) ){
217759 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
217760 fts5SetVtabError(pTab,
217761 "'delete-all' may only be used with a "
217762 "contentless or external content fts5 table"
217763 );
217764 rc = SQLITE_ERROR1;
217765 }else{
217766 rc = sqlite3Fts5StorageDeleteAll(pTab->pStorage);
217767 }
217768 }else if( 0==sqlite3_stricmp("rebuild", zCmd) ){
217769 if( pConfig->eContent==FTS5_CONTENT_NONE ){
217770 fts5SetVtabError(pTab,
217771 "'rebuild' may not be used with a contentless fts5 table"
217772 );
217773 rc = SQLITE_ERROR1;
217774 }else{
217775 rc = sqlite3Fts5StorageRebuild(pTab->pStorage);
217776 }
217777 }else if( 0==sqlite3_stricmp("optimize", zCmd) ){
217778 rc = sqlite3Fts5StorageOptimize(pTab->pStorage);
217779 }else if( 0==sqlite3_stricmp("merge", zCmd) ){
217780 int nMerge = sqlite3_value_int(pVal);
217781 rc = sqlite3Fts5StorageMerge(pTab->pStorage, nMerge);
217782 }else if( 0==sqlite3_stricmp("integrity-check", zCmd) ){
217783 rc = sqlite3Fts5StorageIntegrity(pTab->pStorage);
217784#ifdef SQLITE_DEBUG
217785 }else if( 0==sqlite3_stricmp("prefix-index", zCmd) ){
217786 pConfig->bPrefixIndex = sqlite3_value_int(pVal);
217787#endif
217788 }else{
217789 rc = sqlite3Fts5IndexLoadConfig(pTab->p.pIndex);
217790 if( rc==SQLITE_OK0 ){
217791 rc = sqlite3Fts5ConfigSetValue(pTab->p.pConfig, zCmd, pVal, &bError);
217792 }
217793 if( rc==SQLITE_OK0 ){
217794 if( bError ){
217795 rc = SQLITE_ERROR1;
217796 }else{
217797 rc = sqlite3Fts5StorageConfigValue(pTab->pStorage, zCmd, pVal, 0);
217798 }
217799 }
217800 }
217801 return rc;
217802}
217803
217804static int fts5SpecialDelete(
217805 Fts5FullTable *pTab,
217806 sqlite3_value **apVal
217807){
217808 int rc = SQLITE_OK0;
217809 int eType1 = sqlite3_value_type(apVal[1]);
217810 if( eType1==SQLITE_INTEGER1 ){
217811 sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
217812 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, &apVal[2]);
217813 }
217814 return rc;
217815}
217816
217817static void fts5StorageInsert(
217818 int *pRc,
217819 Fts5FullTable *pTab,
217820 sqlite3_value **apVal,
217821 i64 *piRowid
217822){
217823 int rc = *pRc;
217824 if( rc==SQLITE_OK0 ){
217825 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, piRowid);
217826 }
217827 if( rc==SQLITE_OK0 ){
217828 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal, *piRowid);
217829 }
217830 *pRc = rc;
217831}
217832
217833/*
217834** This function is the implementation of the xUpdate callback used by
217835** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
217836** inserted, updated or deleted.
217837**
217838** A delete specifies a single argument - the rowid of the row to remove.
217839**
217840** Update and insert operations pass:
217841**
217842** 1. The "old" rowid, or NULL.
217843** 2. The "new" rowid.
217844** 3. Values for each of the nCol matchable columns.
217845** 4. Values for the two hidden columns (<tablename> and "rank").
217846*/
217847static int fts5UpdateMethod(
217848 sqlite3_vtab *pVtab, /* Virtual table handle */
217849 int nArg, /* Size of argument array */
217850 sqlite3_value **apVal, /* Array of arguments */
217851 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
217852){
217853 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
217854 Fts5Config *pConfig = pTab->p.pConfig;
217855 int eType0; /* value_type() of apVal[0] */
217856 int rc = SQLITE_OK0; /* Return code */
217857
217858 /* A transaction must be open when this is called. */
217859 assert( pTab->ts.eState==1 )((void) (0));
217860
217861 assert( pVtab->zErrMsg==0 )((void) (0));
217862 assert( nArg==1 || nArg==(2+pConfig->nCol+2) )((void) (0));
217863 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER((void) (0))
217864 || sqlite3_value_type(apVal[0])==SQLITE_NULL((void) (0))
217865 )((void) (0));
217866 assert( pTab->p.pConfig->pzErrmsg==0 )((void) (0));
217867 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
217868
217869 /* Put any active cursors into REQUIRE_SEEK state. */
217870 fts5TripCursors(pTab);
217871
217872 eType0 = sqlite3_value_type(apVal[0]);
217873 if( eType0==SQLITE_NULL5
217874 && sqlite3_value_type(apVal[2+pConfig->nCol])!=SQLITE_NULL5
217875 ){
217876 /* A "special" INSERT op. These are handled separately. */
217877 const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
217878 if( pConfig->eContent!=FTS5_CONTENT_NORMAL
217879 && 0==sqlite3_stricmp("delete", z)
217880 ){
217881 rc = fts5SpecialDelete(pTab, apVal);
217882 }else{
217883 rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
217884 }
217885 }else{
217886 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
217887 ** any conflict on the rowid value must be detected before any
217888 ** modifications are made to the database file. There are 4 cases:
217889 **
217890 ** 1) DELETE
217891 ** 2) UPDATE (rowid not modified)
217892 ** 3) UPDATE (rowid modified)
217893 ** 4) INSERT
217894 **
217895 ** Cases 3 and 4 may violate the rowid constraint.
217896 */
217897 int eConflict = SQLITE_ABORT4;
217898 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
217899 eConflict = sqlite3_vtab_on_conflict(pConfig->db);
217900 }
217901
217902 assert( eType0==SQLITE_INTEGER || eType0==SQLITE_NULL )((void) (0));
217903 assert( nArg!=1 || eType0==SQLITE_INTEGER )((void) (0));
217904
217905 /* Filter out attempts to run UPDATE or DELETE on contentless tables.
217906 ** This is not suported. */
217907 if( eType0==SQLITE_INTEGER1 && fts5IsContentless(pTab) ){
217908 pTab->p.base.zErrMsg = sqlite3_mprintf(
217909 "cannot %s contentless fts5 table: %s",
217910 (nArg>1 ? "UPDATE" : "DELETE from"), pConfig->zName
217911 );
217912 rc = SQLITE_ERROR1;
217913 }
217914
217915 /* DELETE */
217916 else if( nArg==1 ){
217917 i64 iDel = sqlite3_value_int64(apVal[0]); /* Rowid to delete */
217918 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iDel, 0);
217919 }
217920
217921 /* INSERT or UPDATE */
217922 else{
217923 int eType1 = sqlite3_value_numeric_type(apVal[1]);
217924
217925 if( eType1!=SQLITE_INTEGER1 && eType1!=SQLITE_NULL5 ){
217926 rc = SQLITE_MISMATCH20;
217927 }
217928
217929 else if( eType0!=SQLITE_INTEGER1 ){
217930 /* If this is a REPLACE, first remove the current entry (if any) */
217931 if( eConflict==SQLITE_REPLACE5 && eType1==SQLITE_INTEGER1 ){
217932 i64 iNew = sqlite3_value_int64(apVal[1]); /* Rowid to delete */
217933 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
217934 }
217935 fts5StorageInsert(&rc, pTab, apVal, pRowid);
217936 }
217937
217938 /* UPDATE */
217939 else{
217940 i64 iOld = sqlite3_value_int64(apVal[0]); /* Old rowid */
217941 i64 iNew = sqlite3_value_int64(apVal[1]); /* New rowid */
217942 if( eType1==SQLITE_INTEGER1 && iOld!=iNew ){
217943 if( eConflict==SQLITE_REPLACE5 ){
217944 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
217945 if( rc==SQLITE_OK0 ){
217946 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iNew, 0);
217947 }
217948 fts5StorageInsert(&rc, pTab, apVal, pRowid);
217949 }else{
217950 rc = sqlite3Fts5StorageContentInsert(pTab->pStorage, apVal, pRowid);
217951 if( rc==SQLITE_OK0 ){
217952 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
217953 }
217954 if( rc==SQLITE_OK0 ){
217955 rc = sqlite3Fts5StorageIndexInsert(pTab->pStorage, apVal,*pRowid);
217956 }
217957 }
217958 }else{
217959 rc = sqlite3Fts5StorageDelete(pTab->pStorage, iOld, 0);
217960 fts5StorageInsert(&rc, pTab, apVal, pRowid);
217961 }
217962 }
217963 }
217964 }
217965
217966 pTab->p.pConfig->pzErrmsg = 0;
217967 return rc;
217968}
217969
217970/*
217971** Implementation of xSync() method.
217972*/
217973static int fts5SyncMethod(sqlite3_vtab *pVtab){
217974 int rc;
217975 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
217976 fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
217977 pTab->p.pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
217978 fts5TripCursors(pTab);
217979 rc = sqlite3Fts5StorageSync(pTab->pStorage);
217980 pTab->p.pConfig->pzErrmsg = 0;
217981 return rc;
217982}
217983
217984/*
217985** Implementation of xBegin() method.
217986*/
217987static int fts5BeginMethod(sqlite3_vtab *pVtab){
217988 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_BEGIN, 0);
217989 fts5NewTransaction((Fts5FullTable*)pVtab);
217990 return SQLITE_OK0;
217991}
217992
217993/*
217994** Implementation of xCommit() method. This is a no-op. The contents of
217995** the pending-terms hash-table have already been flushed into the database
217996** by fts5SyncMethod().
217997*/
217998static int fts5CommitMethod(sqlite3_vtab *pVtab){
217999 UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
218000 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_COMMIT, 0);
218001 return SQLITE_OK0;
218002}
218003
218004/*
218005** Implementation of xRollback(). Discard the contents of the pending-terms
218006** hash-table. Any changes made to the database are reverted by SQLite.
218007*/
218008static int fts5RollbackMethod(sqlite3_vtab *pVtab){
218009 int rc;
218010 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
218011 fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
218012 rc = sqlite3Fts5StorageRollback(pTab->pStorage);
218013 return rc;
218014}
218015
218016static int fts5CsrPoslist(Fts5Cursor*, int, const u8**, int*);
218017
218018static void *fts5ApiUserData(Fts5Context *pCtx){
218019 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218020 return pCsr->pAux->pUserData;
218021}
218022
218023static int fts5ApiColumnCount(Fts5Context *pCtx){
218024 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218025 return ((Fts5Table*)(pCsr->base.pVtab))->pConfig->nCol;
218026}
218027
218028static int fts5ApiColumnTotalSize(
218029 Fts5Context *pCtx,
218030 int iCol,
218031 sqlite3_int64 *pnToken
218032){
218033 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218034 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
218035 return sqlite3Fts5StorageSize(pTab->pStorage, iCol, pnToken);
218036}
218037
218038static int fts5ApiRowCount(Fts5Context *pCtx, i64 *pnRow){
218039 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218040 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
218041 return sqlite3Fts5StorageRowCount(pTab->pStorage, pnRow);
218042}
218043
218044static int fts5ApiTokenize(
218045 Fts5Context *pCtx,
218046 const char *pText, int nText,
218047 void *pUserData,
218048 int (*xToken)(void*, int, const char*, int, int, int)
218049){
218050 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218051 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
218052 return sqlite3Fts5Tokenize(
218053 pTab->pConfig, FTS5_TOKENIZE_AUX0x0008, pText, nText, pUserData, xToken
218054 );
218055}
218056
218057static int fts5ApiPhraseCount(Fts5Context *pCtx){
218058 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218059 return sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
218060}
218061
218062static int fts5ApiPhraseSize(Fts5Context *pCtx, int iPhrase){
218063 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218064 return sqlite3Fts5ExprPhraseSize(pCsr->pExpr, iPhrase);
218065}
218066
218067static int fts5ApiColumnText(
218068 Fts5Context *pCtx,
218069 int iCol,
218070 const char **pz,
218071 int *pn
218072){
218073 int rc = SQLITE_OK0;
218074 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218075 if( fts5IsContentless((Fts5FullTable*)(pCsr->base.pVtab))
218076 || pCsr->ePlan==FTS5_PLAN_SPECIAL
218077 ){
218078 *pz = 0;
218079 *pn = 0;
218080 }else{
218081 rc = fts5SeekCursor(pCsr, 0);
218082 if( rc==SQLITE_OK0 ){
218083 *pz = (const char*)sqlite3_column_text(pCsr->pStmt, iCol+1);
218084 *pn = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
218085 }
218086 }
218087 return rc;
218088}
218089
218090static int fts5CsrPoslist(
218091 Fts5Cursor *pCsr,
218092 int iPhrase,
218093 const u8 **pa,
218094 int *pn
218095){
218096 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
218097 int rc = SQLITE_OK0;
218098 int bLive = (pCsr->pSorter==0);
218099
218100 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_POSLIST) ){
218101
218102 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
218103 Fts5PoslistPopulator *aPopulator;
218104 int i;
218105 aPopulator = sqlite3Fts5ExprClearPoslists(pCsr->pExpr, bLive);
218106 if( aPopulator==0 ) rc = SQLITE_NOMEM7;
218107 for(i=0; i<pConfig->nCol && rc==SQLITE_OK0; i++){
218108 int n; const char *z;
218109 rc = fts5ApiColumnText((Fts5Context*)pCsr, i, &z, &n);
218110 if( rc==SQLITE_OK0 ){
218111 rc = sqlite3Fts5ExprPopulatePoslists(
218112 pConfig, pCsr->pExpr, aPopulator, i, z, n
218113 );
218114 }
218115 }
218116 sqlite3_free(aPopulator);
218117
218118 if( pCsr->pSorter ){
218119 sqlite3Fts5ExprCheckPoslists(pCsr->pExpr, pCsr->pSorter->iRowid);
218120 }
218121 }
218122 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_POSLIST);
218123 }
218124
218125 if( pCsr->pSorter && pConfig->eDetail==FTS5_DETAIL_FULL ){
218126 Fts5Sorter *pSorter = pCsr->pSorter;
218127 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
218128 *pn = pSorter->aIdx[iPhrase] - i1;
218129 *pa = &pSorter->aPoslist[i1];
218130 }else{
218131 *pn = sqlite3Fts5ExprPoslist(pCsr->pExpr, iPhrase, pa);
218132 }
218133
218134 return rc;
218135}
218136
218137/*
218138** Ensure that the Fts5Cursor.nInstCount and aInst[] variables are populated
218139** correctly for the current view. Return SQLITE_OK if successful, or an
218140** SQLite error code otherwise.
218141*/
218142static int fts5CacheInstArray(Fts5Cursor *pCsr){
218143 int rc = SQLITE_OK0;
218144 Fts5PoslistReader *aIter; /* One iterator for each phrase */
218145 int nIter; /* Number of iterators/phrases */
218146 int nCol = ((Fts5Table*)pCsr->base.pVtab)->pConfig->nCol;
218147
218148 nIter = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
218149 if( pCsr->aInstIter==0 ){
218150 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nIter;
218151 pCsr->aInstIter = (Fts5PoslistReader*)sqlite3Fts5MallocZero(&rc, nByte);
218152 }
218153 aIter = pCsr->aInstIter;
218154
218155 if( aIter ){
218156 int nInst = 0; /* Number instances seen so far */
218157 int i;
218158
218159 /* Initialize all iterators */
218160 for(i=0; i<nIter && rc==SQLITE_OK0; i++){
218161 const u8 *a;
218162 int n;
218163 rc = fts5CsrPoslist(pCsr, i, &a, &n);
218164 if( rc==SQLITE_OK0 ){
218165 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
218166 }
218167 }
218168
218169 if( rc==SQLITE_OK0 ){
218170 while( 1 ){
218171 int *aInst;
218172 int iBest = -1;
218173 for(i=0; i<nIter; i++){
218174 if( (aIter[i].bEof==0)
218175 && (iBest<0 || aIter[i].iPos<aIter[iBest].iPos)
218176 ){
218177 iBest = i;
218178 }
218179 }
218180 if( iBest<0 ) break;
218181
218182 nInst++;
218183 if( nInst>=pCsr->nInstAlloc ){
218184 pCsr->nInstAlloc = pCsr->nInstAlloc ? pCsr->nInstAlloc*2 : 32;
218185 aInst = (int*)sqlite3_realloc64(
218186 pCsr->aInst, pCsr->nInstAlloc*sizeof(int)*3
218187 );
218188 if( aInst ){
218189 pCsr->aInst = aInst;
218190 }else{
218191 rc = SQLITE_NOMEM7;
218192 break;
218193 }
218194 }
218195
218196 aInst = &pCsr->aInst[3 * (nInst-1)];
218197 aInst[0] = iBest;
218198 aInst[1] = FTS5_POS2COLUMN(aIter[iBest].iPos);
218199 aInst[2] = FTS5_POS2OFFSET(aIter[iBest].iPos);
218200 if( aInst[1]<0 || aInst[1]>=nCol ){
218201 rc = FTS5_CORRUPT;
218202 break;
218203 }
218204 sqlite3Fts5PoslistReaderNext(&aIter[iBest]);
218205 }
218206 }
218207
218208 pCsr->nInstCount = nInst;
218209 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_INST);
218210 }
218211 return rc;
218212}
218213
218214static int fts5ApiInstCount(Fts5Context *pCtx, int *pnInst){
218215 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218216 int rc = SQLITE_OK0;
218217 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
218218 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr)) ){
218219 *pnInst = pCsr->nInstCount;
218220 }
218221 return rc;
218222}
218223
218224static int fts5ApiInst(
218225 Fts5Context *pCtx,
218226 int iIdx,
218227 int *piPhrase,
218228 int *piCol,
218229 int *piOff
218230){
218231 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218232 int rc = SQLITE_OK0;
218233 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_INST)==0
218234 || SQLITE_OK0==(rc = fts5CacheInstArray(pCsr))
218235 ){
218236 if( iIdx<0 || iIdx>=pCsr->nInstCount ){
218237 rc = SQLITE_RANGE25;
218238#if 0
218239 }else if( fts5IsOffsetless((Fts5Table*)pCsr->base.pVtab) ){
218240 *piPhrase = pCsr->aInst[iIdx*3];
218241 *piCol = pCsr->aInst[iIdx*3 + 2];
218242 *piOff = -1;
218243#endif
218244 }else{
218245 *piPhrase = pCsr->aInst[iIdx*3];
218246 *piCol = pCsr->aInst[iIdx*3 + 1];
218247 *piOff = pCsr->aInst[iIdx*3 + 2];
218248 }
218249 }
218250 return rc;
218251}
218252
218253static sqlite3_int64 fts5ApiRowid(Fts5Context *pCtx){
218254 return fts5CursorRowid((Fts5Cursor*)pCtx);
218255}
218256
218257static int fts5ColumnSizeCb(
218258 void *pContext, /* Pointer to int */
218259 int tflags,
218260 const char *pUnused, /* Buffer containing token */
218261 int nUnused, /* Size of token in bytes */
218262 int iUnused1, /* Start offset of token */
218263 int iUnused2 /* End offset of token */
218264){
218265 int *pCnt = (int*)pContext;
218266 UNUSED_PARAM2(pUnused, nUnused);
218267 UNUSED_PARAM2(iUnused1, iUnused2);
218268 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 ){
218269 (*pCnt)++;
218270 }
218271 return SQLITE_OK0;
218272}
218273
218274static int fts5ApiColumnSize(Fts5Context *pCtx, int iCol, int *pnToken){
218275 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218276 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
218277 Fts5Config *pConfig = pTab->p.pConfig;
218278 int rc = SQLITE_OK0;
218279
218280 if( CsrFlagTest(pCsr, FTS5CSR_REQUIRE_DOCSIZE) ){
218281 if( pConfig->bColumnsize ){
218282 i64 iRowid = fts5CursorRowid(pCsr);
218283 rc = sqlite3Fts5StorageDocsize(pTab->pStorage, iRowid, pCsr->aColumnSize);
218284 }else if( pConfig->zContent==0 ){
218285 int i;
218286 for(i=0; i<pConfig->nCol; i++){
218287 if( pConfig->abUnindexed[i]==0 ){
218288 pCsr->aColumnSize[i] = -1;
218289 }
218290 }
218291 }else{
218292 int i;
218293 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
218294 if( pConfig->abUnindexed[i]==0 ){
218295 const char *z; int n;
218296 void *p = (void*)(&pCsr->aColumnSize[i]);
218297 pCsr->aColumnSize[i] = 0;
218298 rc = fts5ApiColumnText(pCtx, i, &z, &n);
218299 if( rc==SQLITE_OK0 ){
218300 rc = sqlite3Fts5Tokenize(
218301 pConfig, FTS5_TOKENIZE_AUX0x0008, z, n, p, fts5ColumnSizeCb
218302 );
218303 }
218304 }
218305 }
218306 }
218307 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_DOCSIZE);
218308 }
218309 if( iCol<0 ){
218310 int i;
218311 *pnToken = 0;
218312 for(i=0; i<pConfig->nCol; i++){
218313 *pnToken += pCsr->aColumnSize[i];
218314 }
218315 }else if( iCol<pConfig->nCol ){
218316 *pnToken = pCsr->aColumnSize[iCol];
218317 }else{
218318 *pnToken = 0;
218319 rc = SQLITE_RANGE25;
218320 }
218321 return rc;
218322}
218323
218324/*
218325** Implementation of the xSetAuxdata() method.
218326*/
218327static int fts5ApiSetAuxdata(
218328 Fts5Context *pCtx, /* Fts5 context */
218329 void *pPtr, /* Pointer to save as auxdata */
218330 void(*xDelete)(void*) /* Destructor for pPtr (or NULL) */
218331){
218332 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218333 Fts5Auxdata *pData;
218334
218335 /* Search through the cursors list of Fts5Auxdata objects for one that
218336 ** corresponds to the currently executing auxiliary function. */
218337 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
218338 if( pData->pAux==pCsr->pAux ) break;
218339 }
218340
218341 if( pData ){
218342 if( pData->xDelete ){
218343 pData->xDelete(pData->pPtr);
218344 }
218345 }else{
218346 int rc = SQLITE_OK0;
218347 pData = (Fts5Auxdata*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Auxdata));
218348 if( pData==0 ){
218349 if( xDelete ) xDelete(pPtr);
218350 return rc;
218351 }
218352 pData->pAux = pCsr->pAux;
218353 pData->pNext = pCsr->pAuxdata;
218354 pCsr->pAuxdata = pData;
218355 }
218356
218357 pData->xDelete = xDelete;
218358 pData->pPtr = pPtr;
218359 return SQLITE_OK0;
218360}
218361
218362static void *fts5ApiGetAuxdata(Fts5Context *pCtx, int bClear){
218363 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218364 Fts5Auxdata *pData;
218365 void *pRet = 0;
218366
218367 for(pData=pCsr->pAuxdata; pData; pData=pData->pNext){
218368 if( pData->pAux==pCsr->pAux ) break;
218369 }
218370
218371 if( pData ){
218372 pRet = pData->pPtr;
218373 if( bClear ){
218374 pData->pPtr = 0;
218375 pData->xDelete = 0;
218376 }
218377 }
218378
218379 return pRet;
218380}
218381
218382static void fts5ApiPhraseNext(
218383 Fts5Context *pUnused,
218384 Fts5PhraseIter *pIter,
218385 int *piCol, int *piOff
218386){
218387 UNUSED_PARAM(pUnused);
218388 if( pIter->a>=pIter->b ){
218389 *piCol = -1;
218390 *piOff = -1;
218391 }else{
218392 int iVal;
218393 pIter->a += fts5GetVarint32(pIter->a, iVal);
218394 if( iVal==1 ){
218395 pIter->a += fts5GetVarint32(pIter->a, iVal);
218396 *piCol = iVal;
218397 *piOff = 0;
218398 pIter->a += fts5GetVarint32(pIter->a, iVal);
218399 }
218400 *piOff += (iVal-2);
218401 }
218402}
218403
218404static int fts5ApiPhraseFirst(
218405 Fts5Context *pCtx,
218406 int iPhrase,
218407 Fts5PhraseIter *pIter,
218408 int *piCol, int *piOff
218409){
218410 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218411 int n;
218412 int rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
218413 if( rc==SQLITE_OK0 ){
218414 pIter->b = &pIter->a[n];
218415 *piCol = 0;
218416 *piOff = 0;
218417 fts5ApiPhraseNext(pCtx, pIter, piCol, piOff);
218418 }
218419 return rc;
218420}
218421
218422static void fts5ApiPhraseNextColumn(
218423 Fts5Context *pCtx,
218424 Fts5PhraseIter *pIter,
218425 int *piCol
218426){
218427 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218428 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
218429
218430 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
218431 if( pIter->a>=pIter->b ){
218432 *piCol = -1;
218433 }else{
218434 int iIncr;
218435 pIter->a += fts5GetVarint32(&pIter->a[0], iIncr);
218436 *piCol += (iIncr-2);
218437 }
218438 }else{
218439 while( 1 ){
218440 int dummy;
218441 if( pIter->a>=pIter->b ){
218442 *piCol = -1;
218443 return;
218444 }
218445 if( pIter->a[0]==0x01 ) break;
218446 pIter->a += fts5GetVarint32(pIter->a, dummy);
218447 }
218448 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
218449 }
218450}
218451
218452static int fts5ApiPhraseFirstColumn(
218453 Fts5Context *pCtx,
218454 int iPhrase,
218455 Fts5PhraseIter *pIter,
218456 int *piCol
218457){
218458 int rc = SQLITE_OK0;
218459 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218460 Fts5Config *pConfig = ((Fts5Table*)(pCsr->base.pVtab))->pConfig;
218461
218462 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
218463 Fts5Sorter *pSorter = pCsr->pSorter;
218464 int n;
218465 if( pSorter ){
218466 int i1 = (iPhrase==0 ? 0 : pSorter->aIdx[iPhrase-1]);
218467 n = pSorter->aIdx[iPhrase] - i1;
218468 pIter->a = &pSorter->aPoslist[i1];
218469 }else{
218470 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, iPhrase, &pIter->a, &n);
218471 }
218472 if( rc==SQLITE_OK0 ){
218473 pIter->b = &pIter->a[n];
218474 *piCol = 0;
218475 fts5ApiPhraseNextColumn(pCtx, pIter, piCol);
218476 }
218477 }else{
218478 int n;
218479 rc = fts5CsrPoslist(pCsr, iPhrase, &pIter->a, &n);
218480 if( rc==SQLITE_OK0 ){
218481 pIter->b = &pIter->a[n];
218482 if( n<=0 ){
218483 *piCol = -1;
218484 }else if( pIter->a[0]==0x01 ){
218485 pIter->a += 1 + fts5GetVarint32(&pIter->a[1], *piCol);
218486 }else{
218487 *piCol = 0;
218488 }
218489 }
218490 }
218491
218492 return rc;
218493}
218494
218495
218496static int fts5ApiQueryPhrase(Fts5Context*, int, void*,
218497 int(*)(const Fts5ExtensionApi*, Fts5Context*, void*)
218498);
218499
218500static const Fts5ExtensionApi sFts5Api = {
218501 2, /* iVersion */
218502 fts5ApiUserData,
218503 fts5ApiColumnCount,
218504 fts5ApiRowCount,
218505 fts5ApiColumnTotalSize,
218506 fts5ApiTokenize,
218507 fts5ApiPhraseCount,
218508 fts5ApiPhraseSize,
218509 fts5ApiInstCount,
218510 fts5ApiInst,
218511 fts5ApiRowid,
218512 fts5ApiColumnText,
218513 fts5ApiColumnSize,
218514 fts5ApiQueryPhrase,
218515 fts5ApiSetAuxdata,
218516 fts5ApiGetAuxdata,
218517 fts5ApiPhraseFirst,
218518 fts5ApiPhraseNext,
218519 fts5ApiPhraseFirstColumn,
218520 fts5ApiPhraseNextColumn,
218521};
218522
218523/*
218524** Implementation of API function xQueryPhrase().
218525*/
218526static int fts5ApiQueryPhrase(
218527 Fts5Context *pCtx,
218528 int iPhrase,
218529 void *pUserData,
218530 int(*xCallback)(const Fts5ExtensionApi*, Fts5Context*, void*)
218531){
218532 Fts5Cursor *pCsr = (Fts5Cursor*)pCtx;
218533 Fts5FullTable *pTab = (Fts5FullTable*)(pCsr->base.pVtab);
218534 int rc;
218535 Fts5Cursor *pNew = 0;
218536
218537 rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
218538 if( rc==SQLITE_OK0 ){
218539 pNew->ePlan = FTS5_PLAN_MATCH;
218540 pNew->iFirstRowid = SMALLEST_INT64(((i64)-1) - (0xffffffff|(((i64)0x7fffffff)<<32)));
218541 pNew->iLastRowid = LARGEST_INT64(0xffffffff|(((i64)0x7fffffff)<<32));
218542 pNew->base.pVtab = (sqlite3_vtab*)pTab;
218543 rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
218544 }
218545
218546 if( rc==SQLITE_OK0 ){
218547 for(rc = fts5CursorFirst(pTab, pNew, 0);
218548 rc==SQLITE_OK0 && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
218549 rc = fts5NextMethod((sqlite3_vtab_cursor*)pNew)
218550 ){
218551 rc = xCallback(&sFts5Api, (Fts5Context*)pNew, pUserData);
218552 if( rc!=SQLITE_OK0 ){
218553 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
218554 break;
218555 }
218556 }
218557 }
218558
218559 fts5CloseMethod((sqlite3_vtab_cursor*)pNew);
218560 return rc;
218561}
218562
218563static void fts5ApiInvoke(
218564 Fts5Auxiliary *pAux,
218565 Fts5Cursor *pCsr,
218566 sqlite3_context *context,
218567 int argc,
218568 sqlite3_value **argv
218569){
218570 assert( pCsr->pAux==0 )((void) (0));
218571 pCsr->pAux = pAux;
218572 pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv);
218573 pCsr->pAux = 0;
218574}
218575
218576static Fts5Cursor *fts5CursorFromCsrid(Fts5Global *pGlobal, i64 iCsrId){
218577 Fts5Cursor *pCsr;
218578 for(pCsr=pGlobal->pCsr; pCsr; pCsr=pCsr->pNext){
218579 if( pCsr->iCsrId==iCsrId ) break;
218580 }
218581 return pCsr;
218582}
218583
218584static void fts5ApiCallback(
218585 sqlite3_context *context,
218586 int argc,
218587 sqlite3_value **argv
218588){
218589
218590 Fts5Auxiliary *pAux;
218591 Fts5Cursor *pCsr;
218592 i64 iCsrId;
218593
218594 assert( argc>=1 )((void) (0));
218595 pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
218596 iCsrId = sqlite3_value_int64(argv[0]);
218597
218598 pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
218599 if( pCsr==0 ){
218600 char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
218601 sqlite3_result_error(context, zErr, -1);
218602 sqlite3_free(zErr);
218603 }else{
218604 fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
218605 }
218606}
218607
218608
218609/*
218610** Given cursor id iId, return a pointer to the corresponding Fts5Table
218611** object. Or NULL If the cursor id does not exist.
218612*/
218613static Fts5Table *sqlite3Fts5TableFromCsrid(
218614 Fts5Global *pGlobal, /* FTS5 global context for db handle */
218615 i64 iCsrId /* Id of cursor to find */
218616){
218617 Fts5Cursor *pCsr;
218618 pCsr = fts5CursorFromCsrid(pGlobal, iCsrId);
218619 if( pCsr ){
218620 return (Fts5Table*)pCsr->base.pVtab;
218621 }
218622 return 0;
218623}
218624
218625/*
218626** Return a "position-list blob" corresponding to the current position of
218627** cursor pCsr via sqlite3_result_blob(). A position-list blob contains
218628** the current position-list for each phrase in the query associated with
218629** cursor pCsr.
218630**
218631** A position-list blob begins with (nPhrase-1) varints, where nPhrase is
218632** the number of phrases in the query. Following the varints are the
218633** concatenated position lists for each phrase, in order.
218634**
218635** The first varint (if it exists) contains the size of the position list
218636** for phrase 0. The second (same disclaimer) contains the size of position
218637** list 1. And so on. There is no size field for the final position list,
218638** as it can be derived from the total size of the blob.
218639*/
218640static int fts5PoslistBlob(sqlite3_context *pCtx, Fts5Cursor *pCsr){
218641 int i;
218642 int rc = SQLITE_OK0;
218643 int nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr);
218644 Fts5Buffer val;
218645
218646 memset(&val, 0, sizeof(Fts5Buffer));
218647 switch( ((Fts5Table*)(pCsr->base.pVtab))->pConfig->eDetail ){
218648 case FTS5_DETAIL_FULL:
218649
218650 /* Append the varints */
218651 for(i=0; i<(nPhrase-1); i++){
218652 const u8 *dummy;
218653 int nByte = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &dummy);
218654 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
218655 }
218656
218657 /* Append the position lists */
218658 for(i=0; i<nPhrase; i++){
218659 const u8 *pPoslist;
218660 int nPoslist;
218661 nPoslist = sqlite3Fts5ExprPoslist(pCsr->pExpr, i, &pPoslist);
218662 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
218663 }
218664 break;
218665
218666 case FTS5_DETAIL_COLUMNS:
218667
218668 /* Append the varints */
218669 for(i=0; rc==SQLITE_OK0 && i<(nPhrase-1); i++){
218670 const u8 *dummy;
218671 int nByte;
218672 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &dummy, &nByte);
218673 sqlite3Fts5BufferAppendVarint(&rc, &val, nByte);
218674 }
218675
218676 /* Append the position lists */
218677 for(i=0; rc==SQLITE_OK0 && i<nPhrase; i++){
218678 const u8 *pPoslist;
218679 int nPoslist;
218680 rc = sqlite3Fts5ExprPhraseCollist(pCsr->pExpr, i, &pPoslist, &nPoslist);
218681 sqlite3Fts5BufferAppendBlob(&rc, &val, nPoslist, pPoslist);
218682 }
218683 break;
218684
218685 default:
218686 break;
218687 }
218688
218689 sqlite3_result_blob(pCtx, val.p, val.n, sqlite3_free);
218690 return rc;
218691}
218692
218693/*
218694** This is the xColumn method, called by SQLite to request a value from
218695** the row that the supplied cursor currently points to.
218696*/
218697static int fts5ColumnMethod(
218698 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
218699 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
218700 int iCol /* Index of column to read value from */
218701){
218702 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
218703 Fts5Config *pConfig = pTab->p.pConfig;
218704 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
218705 int rc = SQLITE_OK0;
218706
218707 assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 )((void) (0));
218708
218709 if( pCsr->ePlan==FTS5_PLAN_SPECIAL ){
218710 if( iCol==pConfig->nCol ){
218711 sqlite3_result_int64(pCtx, pCsr->iSpecial);
218712 }
218713 }else
218714
218715 if( iCol==pConfig->nCol ){
218716 /* User is requesting the value of the special column with the same name
218717 ** as the table. Return the cursor integer id number. This value is only
218718 ** useful in that it may be passed as the first argument to an FTS5
218719 ** auxiliary function. */
218720 sqlite3_result_int64(pCtx, pCsr->iCsrId);
218721 }else if( iCol==pConfig->nCol+1 ){
218722
218723 /* The value of the "rank" column. */
218724 if( pCsr->ePlan==FTS5_PLAN_SOURCE ){
218725 fts5PoslistBlob(pCtx, pCsr);
218726 }else if(
218727 pCsr->ePlan==FTS5_PLAN_MATCH
218728 || pCsr->ePlan==FTS5_PLAN_SORTED_MATCH
218729 ){
218730 if( pCsr->pRank || SQLITE_OK0==(rc = fts5FindRankFunction(pCsr)) ){
218731 fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, pCsr->nRankArg, pCsr->apRankArg);
218732 }
218733 }
218734 }else if( !fts5IsContentless(pTab) ){
218735 rc = fts5SeekCursor(pCsr, 1);
218736 if( rc==SQLITE_OK0 ){
218737 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
218738 }
218739 }
218740 return rc;
218741}
218742
218743
218744/*
218745** This routine implements the xFindFunction method for the FTS3
218746** virtual table.
218747*/
218748static int fts5FindFunctionMethod(
218749 sqlite3_vtab *pVtab, /* Virtual table handle */
218750 int nUnused, /* Number of SQL function arguments */
218751 const char *zName, /* Name of SQL function */
218752 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
218753 void **ppArg /* OUT: User data for *pxFunc */
218754){
218755 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
218756 Fts5Auxiliary *pAux;
218757
218758 UNUSED_PARAM(nUnused);
218759 pAux = fts5FindAuxiliary(pTab, zName);
218760 if( pAux ){
218761 *pxFunc = fts5ApiCallback;
218762 *ppArg = (void*)pAux;
218763 return 1;
218764 }
218765
218766 /* No function of the specified name was found. Return 0. */
218767 return 0;
218768}
218769
218770/*
218771** Implementation of FTS5 xRename method. Rename an fts5 table.
218772*/
218773static int fts5RenameMethod(
218774 sqlite3_vtab *pVtab, /* Virtual table handle */
218775 const char *zName /* New name of table */
218776){
218777 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
218778 return sqlite3Fts5StorageRename(pTab->pStorage, zName);
218779}
218780
218781static int sqlite3Fts5FlushToDisk(Fts5Table *pTab){
218782 fts5TripCursors((Fts5FullTable*)pTab);
218783 return sqlite3Fts5StorageSync(((Fts5FullTable*)pTab)->pStorage);
218784}
218785
218786/*
218787** The xSavepoint() method.
218788**
218789** Flush the contents of the pending-terms table to disk.
218790*/
218791static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
218792 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
218793 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_SAVEPOINT, iSavepoint);
218794 return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
218795}
218796
218797/*
218798** The xRelease() method.
218799**
218800** This is a no-op.
218801*/
218802static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
218803 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
218804 fts5CheckTransactionState((Fts5FullTable*)pVtab, FTS5_RELEASE, iSavepoint);
218805 return sqlite3Fts5FlushToDisk((Fts5Table*)pVtab);
218806}
218807
218808/*
218809** The xRollbackTo() method.
218810**
218811** Discard the contents of the pending terms table.
218812*/
218813static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
218814 Fts5FullTable *pTab = (Fts5FullTable*)pVtab;
218815 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
218816 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
218817 fts5TripCursors(pTab);
218818 return sqlite3Fts5StorageRollback(pTab->pStorage);
218819}
218820
218821/*
218822** Register a new auxiliary function with global context pGlobal.
218823*/
218824static int fts5CreateAux(
218825 fts5_api *pApi, /* Global context (one per db handle) */
218826 const char *zName, /* Name of new function */
218827 void *pUserData, /* User data for aux. function */
218828 fts5_extension_function xFunc, /* Aux. function implementation */
218829 void(*xDestroy)(void*) /* Destructor for pUserData */
218830){
218831 Fts5Global *pGlobal = (Fts5Global*)pApi;
218832 int rc = sqlite3_overload_function(pGlobal->db, zName, -1);
218833 if( rc==SQLITE_OK0 ){
218834 Fts5Auxiliary *pAux;
218835 sqlite3_int64 nName; /* Size of zName in bytes, including \0 */
218836 sqlite3_int64 nByte; /* Bytes of space to allocate */
218837
218838 nName = strlen(zName) + 1;
218839 nByte = sizeof(Fts5Auxiliary) + nName;
218840 pAux = (Fts5Auxiliary*)sqlite3_malloc64(nByte);
218841 if( pAux ){
218842 memset(pAux, 0, (size_t)nByte);
218843 pAux->zFunc = (char*)&pAux[1];
218844 memcpy(pAux->zFunc, zName, nName);
218845 pAux->pGlobal = pGlobal;
218846 pAux->pUserData = pUserData;
218847 pAux->xFunc = xFunc;
218848 pAux->xDestroy = xDestroy;
218849 pAux->pNext = pGlobal->pAux;
218850 pGlobal->pAux = pAux;
218851 }else{
218852 rc = SQLITE_NOMEM7;
218853 }
218854 }
218855
218856 return rc;
218857}
218858
218859/*
218860** Register a new tokenizer. This is the implementation of the
218861** fts5_api.xCreateTokenizer() method.
218862*/
218863static int fts5CreateTokenizer(
218864 fts5_api *pApi, /* Global context (one per db handle) */
218865 const char *zName, /* Name of new function */
218866 void *pUserData, /* User data for aux. function */
218867 fts5_tokenizer *pTokenizer, /* Tokenizer implementation */
218868 void(*xDestroy)(void*) /* Destructor for pUserData */
218869){
218870 Fts5Global *pGlobal = (Fts5Global*)pApi;
218871 Fts5TokenizerModule *pNew;
218872 sqlite3_int64 nName; /* Size of zName and its \0 terminator */
218873 sqlite3_int64 nByte; /* Bytes of space to allocate */
218874 int rc = SQLITE_OK0;
218875
218876 nName = strlen(zName) + 1;
218877 nByte = sizeof(Fts5TokenizerModule) + nName;
218878 pNew = (Fts5TokenizerModule*)sqlite3_malloc64(nByte);
218879 if( pNew ){
218880 memset(pNew, 0, (size_t)nByte);
218881 pNew->zName = (char*)&pNew[1];
218882 memcpy(pNew->zName, zName, nName);
218883 pNew->pUserData = pUserData;
218884 pNew->x = *pTokenizer;
218885 pNew->xDestroy = xDestroy;
218886 pNew->pNext = pGlobal->pTok;
218887 pGlobal->pTok = pNew;
218888 if( pNew->pNext==0 ){
218889 pGlobal->pDfltTok = pNew;
218890 }
218891 }else{
218892 rc = SQLITE_NOMEM7;
218893 }
218894
218895 return rc;
218896}
218897
218898static Fts5TokenizerModule *fts5LocateTokenizer(
218899 Fts5Global *pGlobal,
218900 const char *zName
218901){
218902 Fts5TokenizerModule *pMod = 0;
218903
218904 if( zName==0 ){
218905 pMod = pGlobal->pDfltTok;
218906 }else{
218907 for(pMod=pGlobal->pTok; pMod; pMod=pMod->pNext){
218908 if( sqlite3_stricmp(zName, pMod->zName)==0 ) break;
218909 }
218910 }
218911
218912 return pMod;
218913}
218914
218915/*
218916** Find a tokenizer. This is the implementation of the
218917** fts5_api.xFindTokenizer() method.
218918*/
218919static int fts5FindTokenizer(
218920 fts5_api *pApi, /* Global context (one per db handle) */
218921 const char *zName, /* Name of new function */
218922 void **ppUserData,
218923 fts5_tokenizer *pTokenizer /* Populate this object */
218924){
218925 int rc = SQLITE_OK0;
218926 Fts5TokenizerModule *pMod;
218927
218928 pMod = fts5LocateTokenizer((Fts5Global*)pApi, zName);
218929 if( pMod ){
218930 *pTokenizer = pMod->x;
218931 *ppUserData = pMod->pUserData;
218932 }else{
218933 memset(pTokenizer, 0, sizeof(fts5_tokenizer));
218934 rc = SQLITE_ERROR1;
218935 }
218936
218937 return rc;
218938}
218939
218940static int sqlite3Fts5GetTokenizer(
218941 Fts5Global *pGlobal,
218942 const char **azArg,
218943 int nArg,
218944 Fts5Tokenizer **ppTok,
218945 fts5_tokenizer **ppTokApi,
218946 char **pzErr
218947){
218948 Fts5TokenizerModule *pMod;
218949 int rc = SQLITE_OK0;
218950
218951 pMod = fts5LocateTokenizer(pGlobal, nArg==0 ? 0 : azArg[0]);
218952 if( pMod==0 ){
218953 assert( nArg>0 )((void) (0));
218954 rc = SQLITE_ERROR1;
218955 *pzErr = sqlite3_mprintf("no such tokenizer: %s", azArg[0]);
218956 }else{
218957 rc = pMod->x.xCreate(pMod->pUserData, &azArg[1], (nArg?nArg-1:0), ppTok);
218958 *ppTokApi = &pMod->x;
218959 if( rc!=SQLITE_OK0 && pzErr ){
218960 *pzErr = sqlite3_mprintf("error in tokenizer constructor");
218961 }
218962 }
218963
218964 if( rc!=SQLITE_OK0 ){
218965 *ppTokApi = 0;
218966 *ppTok = 0;
218967 }
218968
218969 return rc;
218970}
218971
218972static void fts5ModuleDestroy(void *pCtx){
218973 Fts5TokenizerModule *pTok, *pNextTok;
218974 Fts5Auxiliary *pAux, *pNextAux;
218975 Fts5Global *pGlobal = (Fts5Global*)pCtx;
218976
218977 for(pAux=pGlobal->pAux; pAux; pAux=pNextAux){
218978 pNextAux = pAux->pNext;
218979 if( pAux->xDestroy ) pAux->xDestroy(pAux->pUserData);
218980 sqlite3_free(pAux);
218981 }
218982
218983 for(pTok=pGlobal->pTok; pTok; pTok=pNextTok){
218984 pNextTok = pTok->pNext;
218985 if( pTok->xDestroy ) pTok->xDestroy(pTok->pUserData);
218986 sqlite3_free(pTok);
218987 }
218988
218989 sqlite3_free(pGlobal);
218990}
218991
218992static void fts5Fts5Func(
218993 sqlite3_context *pCtx, /* Function call context */
218994 int nArg, /* Number of args */
218995 sqlite3_value **apArg /* Function arguments */
218996){
218997 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
218998 fts5_api **ppApi;
218999 UNUSED_PARAM(nArg);
219000 assert( nArg==1 )((void) (0));
219001 ppApi = (fts5_api**)sqlite3_value_pointer(apArg[0], "fts5_api_ptr");
219002 if( ppApi ) *ppApi = &pGlobal->api;
219003}
219004
219005/*
219006** Implementation of fts5_source_id() function.
219007*/
219008static void fts5SourceIdFunc(
219009 sqlite3_context *pCtx, /* Function call context */
219010 int nArg, /* Number of args */
219011 sqlite3_value **apUnused /* Function arguments */
219012){
219013 assert( nArg==0 )((void) (0));
219014 UNUSED_PARAM2(nArg, apUnused);
219015 sqlite3_result_text(pCtx, "fts5: 2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88bfa6", -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
219016}
219017
219018/*
219019** Return true if zName is the extension on one of the shadow tables used
219020** by this module.
219021*/
219022static int fts5ShadowName(const char *zName){
219023 static const char *azName[] = {
219024 "config", "content", "data", "docsize", "idx"
219025 };
219026 unsigned int i;
219027 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
219028 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
219029 }
219030 return 0;
219031}
219032
219033static int fts5Init(sqlite3 *db){
219034 static const sqlite3_module fts5Mod = {
219035 /* iVersion */ 3,
219036 /* xCreate */ fts5CreateMethod,
219037 /* xConnect */ fts5ConnectMethod,
219038 /* xBestIndex */ fts5BestIndexMethod,
219039 /* xDisconnect */ fts5DisconnectMethod,
219040 /* xDestroy */ fts5DestroyMethod,
219041 /* xOpen */ fts5OpenMethod,
219042 /* xClose */ fts5CloseMethod,
219043 /* xFilter */ fts5FilterMethod,
219044 /* xNext */ fts5NextMethod,
219045 /* xEof */ fts5EofMethod,
219046 /* xColumn */ fts5ColumnMethod,
219047 /* xRowid */ fts5RowidMethod,
219048 /* xUpdate */ fts5UpdateMethod,
219049 /* xBegin */ fts5BeginMethod,
219050 /* xSync */ fts5SyncMethod,
219051 /* xCommit */ fts5CommitMethod,
219052 /* xRollback */ fts5RollbackMethod,
219053 /* xFindFunction */ fts5FindFunctionMethod,
219054 /* xRename */ fts5RenameMethod,
219055 /* xSavepoint */ fts5SavepointMethod,
219056 /* xRelease */ fts5ReleaseMethod,
219057 /* xRollbackTo */ fts5RollbackToMethod,
219058 /* xShadowName */ fts5ShadowName
219059 };
219060
219061 int rc;
219062 Fts5Global *pGlobal = 0;
219063
219064 pGlobal = (Fts5Global*)sqlite3_malloc(sizeof(Fts5Global));
219065 if( pGlobal==0 ){
219066 rc = SQLITE_NOMEM7;
219067 }else{
219068 void *p = (void*)pGlobal;
219069 memset(pGlobal, 0, sizeof(Fts5Global));
219070 pGlobal->db = db;
219071 pGlobal->api.iVersion = 2;
219072 pGlobal->api.xCreateFunction = fts5CreateAux;
219073 pGlobal->api.xCreateTokenizer = fts5CreateTokenizer;
219074 pGlobal->api.xFindTokenizer = fts5FindTokenizer;
219075 rc = sqlite3_create_module_v2(db, "fts5", &fts5Mod, p, fts5ModuleDestroy);
219076 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5IndexInit(db);
219077 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5ExprInit(pGlobal, db);
219078 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5AuxInit(&pGlobal->api);
219079 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5TokenizerInit(&pGlobal->api);
219080 if( rc==SQLITE_OK0 ) rc = sqlite3Fts5VocabInit(pGlobal, db);
219081 if( rc==SQLITE_OK0 ){
219082 rc = sqlite3_create_function(
219083 db, "fts5", 1, SQLITE_UTF81, p, fts5Fts5Func, 0, 0
219084 );
219085 }
219086 if( rc==SQLITE_OK0 ){
219087 rc = sqlite3_create_function(
219088 db, "fts5_source_id", 0, SQLITE_UTF81, p, fts5SourceIdFunc, 0, 0
219089 );
219090 }
219091 }
219092
219093 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
219094 ** fts5_test_mi.c is compiled and linked into the executable. And call
219095 ** its entry point to enable the matchinfo() demo. */
219096#ifdef SQLITE_FTS5_ENABLE_TEST_MI
219097 if( rc==SQLITE_OK0 ){
219098 extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
219099 rc = sqlite3Fts5TestRegisterMatchinfo(db);
219100 }
219101#endif
219102
219103 return rc;
219104}
219105
219106/*
219107** The following functions are used to register the module with SQLite. If
219108** this module is being built as part of the SQLite core (SQLITE_CORE is
219109** defined), then sqlite3_open() will call sqlite3Fts5Init() directly.
219110**
219111** Or, if this module is being built as a loadable extension,
219112** sqlite3Fts5Init() is omitted and the two standard entry points
219113** sqlite3_fts_init() and sqlite3_fts5_init() defined instead.
219114*/
219115#ifndef SQLITE_CORE1
219116#ifdef _WIN32
219117__declspec(dllexport)
219118#endif
219119SQLITE_API int sqlite3_fts_init(
219120 sqlite3 *db,
219121 char **pzErrMsg,
219122 const sqlite3_api_routines *pApi
219123){
219124 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
219125 (void)pzErrMsg; /* Unused parameter */
219126 return fts5Init(db);
219127}
219128
219129#ifdef _WIN32
219130__declspec(dllexport)
219131#endif
219132SQLITE_API int sqlite3_fts5_init(
219133 sqlite3 *db,
219134 char **pzErrMsg,
219135 const sqlite3_api_routines *pApi
219136){
219137 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
219138 (void)pzErrMsg; /* Unused parameter */
219139 return fts5Init(db);
219140}
219141#else
219142SQLITE_PRIVATEstatic int sqlite3Fts5Init(sqlite3 *db){
219143 return fts5Init(db);
219144}
219145#endif
219146
219147/*
219148** 2014 May 31
219149**
219150** The author disclaims copyright to this source code. In place of
219151** a legal notice, here is a blessing:
219152**
219153** May you do good and not evil.
219154** May you find forgiveness for yourself and forgive others.
219155** May you share freely, never taking more than you give.
219156**
219157******************************************************************************
219158**
219159*/
219160
219161
219162
219163/* #include "fts5Int.h" */
219164
219165struct Fts5Storage {
219166 Fts5Config *pConfig;
219167 Fts5Index *pIndex;
219168 int bTotalsValid; /* True if nTotalRow/aTotalSize[] are valid */
219169 i64 nTotalRow; /* Total number of rows in FTS table */
219170 i64 *aTotalSize; /* Total sizes of each column */
219171 sqlite3_stmt *aStmt[11];
219172};
219173
219174
219175#if FTS5_STMT_SCAN_ASC!=0
219176# error "FTS5_STMT_SCAN_ASC mismatch"
219177#endif
219178#if FTS5_STMT_SCAN_DESC!=1
219179# error "FTS5_STMT_SCAN_DESC mismatch"
219180#endif
219181#if FTS5_STMT_LOOKUP!=2
219182# error "FTS5_STMT_LOOKUP mismatch"
219183#endif
219184
219185#define FTS5_STMT_INSERT_CONTENT 3
219186#define FTS5_STMT_REPLACE_CONTENT 4
219187#define FTS5_STMT_DELETE_CONTENT 5
219188#define FTS5_STMT_REPLACE_DOCSIZE 6
219189#define FTS5_STMT_DELETE_DOCSIZE 7
219190#define FTS5_STMT_LOOKUP_DOCSIZE 8
219191#define FTS5_STMT_REPLACE_CONFIG 9
219192#define FTS5_STMT_SCAN 10
219193
219194/*
219195** Prepare the two insert statements - Fts5Storage.pInsertContent and
219196** Fts5Storage.pInsertDocsize - if they have not already been prepared.
219197** Return SQLITE_OK if successful, or an SQLite error code if an error
219198** occurs.
219199*/
219200static int fts5StorageGetStmt(
219201 Fts5Storage *p, /* Storage handle */
219202 int eStmt, /* FTS5_STMT_XXX constant */
219203 sqlite3_stmt **ppStmt, /* OUT: Prepared statement handle */
219204 char **pzErrMsg /* OUT: Error message (if any) */
219205){
219206 int rc = SQLITE_OK0;
219207
219208 /* If there is no %_docsize table, there should be no requests for
219209 ** statements to operate on it. */
219210 assert( p->pConfig->bColumnsize || (((void) (0))
219211 eStmt!=FTS5_STMT_REPLACE_DOCSIZE((void) (0))
219212 && eStmt!=FTS5_STMT_DELETE_DOCSIZE((void) (0))
219213 && eStmt!=FTS5_STMT_LOOKUP_DOCSIZE((void) (0))
219214 ))((void) (0));
219215
219216 assert( eStmt>=0 && eStmt<ArraySize(p->aStmt) )((void) (0));
219217 if( p->aStmt[eStmt]==0 ){
219218 const char *azStmt[] = {
219219 "SELECT %s FROM %s T WHERE T.%Q >= ? AND T.%Q <= ? ORDER BY T.%Q ASC",
219220 "SELECT %s FROM %s T WHERE T.%Q <= ? AND T.%Q >= ? ORDER BY T.%Q DESC",
219221 "SELECT %s FROM %s T WHERE T.%Q=?", /* LOOKUP */
219222
219223 "INSERT INTO %Q.'%q_content' VALUES(%s)", /* INSERT_CONTENT */
219224 "REPLACE INTO %Q.'%q_content' VALUES(%s)", /* REPLACE_CONTENT */
219225 "DELETE FROM %Q.'%q_content' WHERE id=?", /* DELETE_CONTENT */
219226 "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)", /* REPLACE_DOCSIZE */
219227 "DELETE FROM %Q.'%q_docsize' WHERE id=?", /* DELETE_DOCSIZE */
219228
219229 "SELECT sz FROM %Q.'%q_docsize' WHERE id=?", /* LOOKUP_DOCSIZE */
219230
219231 "REPLACE INTO %Q.'%q_config' VALUES(?,?)", /* REPLACE_CONFIG */
219232 "SELECT %s FROM %s AS T", /* SCAN */
219233 };
219234 Fts5Config *pC = p->pConfig;
219235 char *zSql = 0;
219236
219237 switch( eStmt ){
219238 case FTS5_STMT_SCAN:
219239 zSql = sqlite3_mprintf(azStmt[eStmt],
219240 pC->zContentExprlist, pC->zContent
219241 );
219242 break;
219243
219244 case FTS5_STMT_SCAN_ASC:
219245 case FTS5_STMT_SCAN_DESC:
219246 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zContentExprlist,
219247 pC->zContent, pC->zContentRowid, pC->zContentRowid,
219248 pC->zContentRowid
219249 );
219250 break;
219251
219252 case FTS5_STMT_LOOKUP:
219253 zSql = sqlite3_mprintf(azStmt[eStmt],
219254 pC->zContentExprlist, pC->zContent, pC->zContentRowid
219255 );
219256 break;
219257
219258 case FTS5_STMT_INSERT_CONTENT:
219259 case FTS5_STMT_REPLACE_CONTENT: {
219260 int nCol = pC->nCol + 1;
219261 char *zBind;
219262 int i;
219263
219264 zBind = sqlite3_malloc64(1 + nCol*2);
219265 if( zBind ){
219266 for(i=0; i<nCol; i++){
219267 zBind[i*2] = '?';
219268 zBind[i*2 + 1] = ',';
219269 }
219270 zBind[i*2-1] = '\0';
219271 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName, zBind);
219272 sqlite3_free(zBind);
219273 }
219274 break;
219275 }
219276
219277 default:
219278 zSql = sqlite3_mprintf(azStmt[eStmt], pC->zDb, pC->zName);
219279 break;
219280 }
219281
219282 if( zSql==0 ){
219283 rc = SQLITE_NOMEM7;
219284 }else{
219285 int f = SQLITE_PREPARE_PERSISTENT0x01;
219286 if( eStmt>FTS5_STMT_LOOKUP ) f |= SQLITE_PREPARE_NO_VTAB0x04;
219287 rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
219288 sqlite3_free(zSql);
219289 if( rc!=SQLITE_OK0 && pzErrMsg ){
219290 *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
219291 }
219292 }
219293 }
219294
219295 *ppStmt = p->aStmt[eStmt];
219296 sqlite3_reset(*ppStmt);
219297 return rc;
219298}
219299
219300
219301static int fts5ExecPrintf(
219302 sqlite3 *db,
219303 char **pzErr,
219304 const char *zFormat,
219305 ...
219306){
219307 int rc;
219308 va_list ap; /* ... printf arguments */
219309 char *zSql;
219310
219311 va_start(ap, zFormat)__builtin_va_start(ap, zFormat);
219312 zSql = sqlite3_vmprintf(zFormat, ap);
219313
219314 if( zSql==0 ){
219315 rc = SQLITE_NOMEM7;
219316 }else{
219317 rc = sqlite3_exec(db, zSql, 0, 0, pzErr);
219318 sqlite3_free(zSql);
219319 }
219320
219321 va_end(ap)__builtin_va_end(ap);
219322 return rc;
219323}
219324
219325/*
219326** Drop all shadow tables. Return SQLITE_OK if successful or an SQLite error
219327** code otherwise.
219328*/
219329static int sqlite3Fts5DropAll(Fts5Config *pConfig){
219330 int rc = fts5ExecPrintf(pConfig->db, 0,
219331 "DROP TABLE IF EXISTS %Q.'%q_data';"
219332 "DROP TABLE IF EXISTS %Q.'%q_idx';"
219333 "DROP TABLE IF EXISTS %Q.'%q_config';",
219334 pConfig->zDb, pConfig->zName,
219335 pConfig->zDb, pConfig->zName,
219336 pConfig->zDb, pConfig->zName
219337 );
219338 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
219339 rc = fts5ExecPrintf(pConfig->db, 0,
219340 "DROP TABLE IF EXISTS %Q.'%q_docsize';",
219341 pConfig->zDb, pConfig->zName
219342 );
219343 }
219344 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL ){
219345 rc = fts5ExecPrintf(pConfig->db, 0,
219346 "DROP TABLE IF EXISTS %Q.'%q_content';",
219347 pConfig->zDb, pConfig->zName
219348 );
219349 }
219350 return rc;
219351}
219352
219353static void fts5StorageRenameOne(
219354 Fts5Config *pConfig, /* Current FTS5 configuration */
219355 int *pRc, /* IN/OUT: Error code */
219356 const char *zTail, /* Tail of table name e.g. "data", "config" */
219357 const char *zName /* New name of FTS5 table */
219358){
219359 if( *pRc==SQLITE_OK0 ){
219360 *pRc = fts5ExecPrintf(pConfig->db, 0,
219361 "ALTER TABLE %Q.'%q_%s' RENAME TO '%q_%s';",
219362 pConfig->zDb, pConfig->zName, zTail, zName, zTail
219363 );
219364 }
219365}
219366
219367static int sqlite3Fts5StorageRename(Fts5Storage *pStorage, const char *zName){
219368 Fts5Config *pConfig = pStorage->pConfig;
219369 int rc = sqlite3Fts5StorageSync(pStorage);
219370
219371 fts5StorageRenameOne(pConfig, &rc, "data", zName);
219372 fts5StorageRenameOne(pConfig, &rc, "idx", zName);
219373 fts5StorageRenameOne(pConfig, &rc, "config", zName);
219374 if( pConfig->bColumnsize ){
219375 fts5StorageRenameOne(pConfig, &rc, "docsize", zName);
219376 }
219377 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
219378 fts5StorageRenameOne(pConfig, &rc, "content", zName);
219379 }
219380 return rc;
219381}
219382
219383/*
219384** Create the shadow table named zPost, with definition zDefn. Return
219385** SQLITE_OK if successful, or an SQLite error code otherwise.
219386*/
219387static int sqlite3Fts5CreateTable(
219388 Fts5Config *pConfig, /* FTS5 configuration */
219389 const char *zPost, /* Shadow table to create (e.g. "content") */
219390 const char *zDefn, /* Columns etc. for shadow table */
219391 int bWithout, /* True for without rowid */
219392 char **pzErr /* OUT: Error message */
219393){
219394 int rc;
219395 char *zErr = 0;
219396
219397 rc = fts5ExecPrintf(pConfig->db, &zErr, "CREATE TABLE %Q.'%q_%q'(%s)%s",
219398 pConfig->zDb, pConfig->zName, zPost, zDefn,
219399#ifndef SQLITE_FTS5_NO_WITHOUT_ROWID
219400 bWithout?" WITHOUT ROWID":
219401#endif
219402 ""
219403 );
219404 if( zErr ){
219405 *pzErr = sqlite3_mprintf(
219406 "fts5: error creating shadow table %q_%s: %s",
219407 pConfig->zName, zPost, zErr
219408 );
219409 sqlite3_free(zErr);
219410 }
219411
219412 return rc;
219413}
219414
219415/*
219416** Open a new Fts5Index handle. If the bCreate argument is true, create
219417** and initialize the underlying tables
219418**
219419** If successful, set *pp to point to the new object and return SQLITE_OK.
219420** Otherwise, set *pp to NULL and return an SQLite error code.
219421*/
219422static int sqlite3Fts5StorageOpen(
219423 Fts5Config *pConfig,
219424 Fts5Index *pIndex,
219425 int bCreate,
219426 Fts5Storage **pp,
219427 char **pzErr /* OUT: Error message */
219428){
219429 int rc = SQLITE_OK0;
219430 Fts5Storage *p; /* New object */
219431 sqlite3_int64 nByte; /* Bytes of space to allocate */
219432
219433 nByte = sizeof(Fts5Storage) /* Fts5Storage object */
219434 + pConfig->nCol * sizeof(i64); /* Fts5Storage.aTotalSize[] */
219435 *pp = p = (Fts5Storage*)sqlite3_malloc64(nByte);
219436 if( !p ) return SQLITE_NOMEM7;
219437
219438 memset(p, 0, (size_t)nByte);
219439 p->aTotalSize = (i64*)&p[1];
219440 p->pConfig = pConfig;
219441 p->pIndex = pIndex;
219442
219443 if( bCreate ){
219444 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
219445 int nDefn = 32 + pConfig->nCol*10;
219446 char *zDefn = sqlite3_malloc64(32 + (sqlite3_int64)pConfig->nCol * 10);
219447 if( zDefn==0 ){
219448 rc = SQLITE_NOMEM7;
219449 }else{
219450 int i;
219451 int iOff;
219452 sqlite3_snprintf(nDefn, zDefn, "id INTEGER PRIMARY KEY");
219453 iOff = (int)strlen(zDefn);
219454 for(i=0; i<pConfig->nCol; i++){
219455 sqlite3_snprintf(nDefn-iOff, &zDefn[iOff], ", c%d", i);
219456 iOff += (int)strlen(&zDefn[iOff]);
219457 }
219458 rc = sqlite3Fts5CreateTable(pConfig, "content", zDefn, 0, pzErr);
219459 }
219460 sqlite3_free(zDefn);
219461 }
219462
219463 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
219464 rc = sqlite3Fts5CreateTable(
219465 pConfig, "docsize", "id INTEGER PRIMARY KEY, sz BLOB", 0, pzErr
219466 );
219467 }
219468 if( rc==SQLITE_OK0 ){
219469 rc = sqlite3Fts5CreateTable(
219470 pConfig, "config", "k PRIMARY KEY, v", 1, pzErr
219471 );
219472 }
219473 if( rc==SQLITE_OK0 ){
219474 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
219475 }
219476 }
219477
219478 if( rc ){
219479 sqlite3Fts5StorageClose(p);
219480 *pp = 0;
219481 }
219482 return rc;
219483}
219484
219485/*
219486** Close a handle opened by an earlier call to sqlite3Fts5StorageOpen().
219487*/
219488static int sqlite3Fts5StorageClose(Fts5Storage *p){
219489 int rc = SQLITE_OK0;
219490 if( p ){
219491 int i;
219492
219493 /* Finalize all SQL statements */
219494 for(i=0; i<ArraySize(p->aStmt)((int)(sizeof(p->aStmt)/sizeof(p->aStmt[0]))); i++){
219495 sqlite3_finalize(p->aStmt[i]);
219496 }
219497
219498 sqlite3_free(p);
219499 }
219500 return rc;
219501}
219502
219503typedef struct Fts5InsertCtx Fts5InsertCtx;
219504struct Fts5InsertCtx {
219505 Fts5Storage *pStorage;
219506 int iCol;
219507 int szCol; /* Size of column value in tokens */
219508};
219509
219510/*
219511** Tokenization callback used when inserting tokens into the FTS index.
219512*/
219513static int fts5StorageInsertCallback(
219514 void *pContext, /* Pointer to Fts5InsertCtx object */
219515 int tflags,
219516 const char *pToken, /* Buffer containing token */
219517 int nToken, /* Size of token in bytes */
219518 int iUnused1, /* Start offset of token */
219519 int iUnused2 /* End offset of token */
219520){
219521 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
219522 Fts5Index *pIdx = pCtx->pStorage->pIndex;
219523 UNUSED_PARAM2(iUnused1, iUnused2);
219524 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
219525 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 || pCtx->szCol==0 ){
219526 pCtx->szCol++;
219527 }
219528 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
219529}
219530
219531/*
219532** If a row with rowid iDel is present in the %_content table, add the
219533** delete-markers to the FTS index necessary to delete it. Do not actually
219534** remove the %_content row at this time though.
219535*/
219536static int fts5StorageDeleteFromIndex(
219537 Fts5Storage *p,
219538 i64 iDel,
219539 sqlite3_value **apVal
219540){
219541 Fts5Config *pConfig = p->pConfig;
219542 sqlite3_stmt *pSeek = 0; /* SELECT to read row iDel from %_data */
219543 int rc; /* Return code */
219544 int rc2; /* sqlite3_reset() return code */
219545 int iCol;
219546 Fts5InsertCtx ctx;
219547
219548 if( apVal==0 ){
219549 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP, &pSeek, 0);
219550 if( rc!=SQLITE_OK0 ) return rc;
219551 sqlite3_bind_int64(pSeek, 1, iDel);
219552 if( sqlite3_step(pSeek)!=SQLITE_ROW100 ){
219553 return sqlite3_reset(pSeek);
219554 }
219555 }
219556
219557 ctx.pStorage = p;
219558 ctx.iCol = -1;
219559 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 1, iDel);
219560 for(iCol=1; rc==SQLITE_OK0 && iCol<=pConfig->nCol; iCol++){
219561 if( pConfig->abUnindexed[iCol-1]==0 ){
219562 const char *zText;
219563 int nText;
219564 if( pSeek ){
219565 zText = (const char*)sqlite3_column_text(pSeek, iCol);
219566 nText = sqlite3_column_bytes(pSeek, iCol);
219567 }else{
219568 zText = (const char*)sqlite3_value_text(apVal[iCol-1]);
219569 nText = sqlite3_value_bytes(apVal[iCol-1]);
219570 }
219571 ctx.szCol = 0;
219572 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT0x0004,
219573 zText, nText, (void*)&ctx, fts5StorageInsertCallback
219574 );
219575 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
219576 }
219577 }
219578 p->nTotalRow--;
219579
219580 rc2 = sqlite3_reset(pSeek);
219581 if( rc==SQLITE_OK0 ) rc = rc2;
219582 return rc;
219583}
219584
219585
219586/*
219587** Insert a record into the %_docsize table. Specifically, do:
219588**
219589** INSERT OR REPLACE INTO %_docsize(id, sz) VALUES(iRowid, pBuf);
219590**
219591** If there is no %_docsize table (as happens if the columnsize=0 option
219592** is specified when the FTS5 table is created), this function is a no-op.
219593*/
219594static int fts5StorageInsertDocsize(
219595 Fts5Storage *p, /* Storage module to write to */
219596 i64 iRowid, /* id value */
219597 Fts5Buffer *pBuf /* sz value */
219598){
219599 int rc = SQLITE_OK0;
219600 if( p->pConfig->bColumnsize ){
219601 sqlite3_stmt *pReplace = 0;
219602 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
219603 if( rc==SQLITE_OK0 ){
219604 sqlite3_bind_int64(pReplace, 1, iRowid);
219605 sqlite3_bind_blob(pReplace, 2, pBuf->p, pBuf->n, SQLITE_STATIC((sqlite3_destructor_type)0));
219606 sqlite3_step(pReplace);
219607 rc = sqlite3_reset(pReplace);
219608 sqlite3_bind_null(pReplace, 2);
219609 }
219610 }
219611 return rc;
219612}
219613
219614/*
219615** Load the contents of the "averages" record from disk into the
219616** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
219617** argument bCache is true, set the p->bTotalsValid flag to indicate
219618** that the contents of aTotalSize[] and nTotalRow are valid until
219619** further notice.
219620**
219621** Return SQLITE_OK if successful, or an SQLite error code if an error
219622** occurs.
219623*/
219624static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
219625 int rc = SQLITE_OK0;
219626 if( p->bTotalsValid==0 ){
219627 rc = sqlite3Fts5IndexGetAverages(p->pIndex, &p->nTotalRow, p->aTotalSize);
219628 p->bTotalsValid = bCache;
219629 }
219630 return rc;
219631}
219632
219633/*
219634** Store the current contents of the p->nTotalRow and p->aTotalSize[]
219635** variables in the "averages" record on disk.
219636**
219637** Return SQLITE_OK if successful, or an SQLite error code if an error
219638** occurs.
219639*/
219640static int fts5StorageSaveTotals(Fts5Storage *p){
219641 int nCol = p->pConfig->nCol;
219642 int i;
219643 Fts5Buffer buf;
219644 int rc = SQLITE_OK0;
219645 memset(&buf, 0, sizeof(buf));
219646
219647 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->nTotalRow);
219648 for(i=0; i<nCol; i++){
219649 sqlite3Fts5BufferAppendVarint(&rc, &buf, p->aTotalSize[i]);
219650 }
219651 if( rc==SQLITE_OK0 ){
219652 rc = sqlite3Fts5IndexSetAverages(p->pIndex, buf.p, buf.n);
219653 }
219654 sqlite3_free(buf.p);
219655
219656 return rc;
219657}
219658
219659/*
219660** Remove a row from the FTS table.
219661*/
219662static int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel, sqlite3_value **apVal){
219663 Fts5Config *pConfig = p->pConfig;
219664 int rc;
219665 sqlite3_stmt *pDel = 0;
219666
219667 assert( pConfig->eContent!=FTS5_CONTENT_NORMAL || apVal==0 )((void) (0));
219668 rc = fts5StorageLoadTotals(p, 1);
219669
219670 /* Delete the index records */
219671 if( rc==SQLITE_OK0 ){
219672 rc = fts5StorageDeleteFromIndex(p, iDel, apVal);
219673 }
219674
219675 /* Delete the %_docsize record */
219676 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
219677 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_DOCSIZE, &pDel, 0);
219678 if( rc==SQLITE_OK0 ){
219679 sqlite3_bind_int64(pDel, 1, iDel);
219680 sqlite3_step(pDel);
219681 rc = sqlite3_reset(pDel);
219682 }
219683 }
219684
219685 /* Delete the %_content record */
219686 if( pConfig->eContent==FTS5_CONTENT_NORMAL ){
219687 if( rc==SQLITE_OK0 ){
219688 rc = fts5StorageGetStmt(p, FTS5_STMT_DELETE_CONTENT, &pDel, 0);
219689 }
219690 if( rc==SQLITE_OK0 ){
219691 sqlite3_bind_int64(pDel, 1, iDel);
219692 sqlite3_step(pDel);
219693 rc = sqlite3_reset(pDel);
219694 }
219695 }
219696
219697 return rc;
219698}
219699
219700/*
219701** Delete all entries in the FTS5 index.
219702*/
219703static int sqlite3Fts5StorageDeleteAll(Fts5Storage *p){
219704 Fts5Config *pConfig = p->pConfig;
219705 int rc;
219706
219707 /* Delete the contents of the %_data and %_docsize tables. */
219708 rc = fts5ExecPrintf(pConfig->db, 0,
219709 "DELETE FROM %Q.'%q_data';"
219710 "DELETE FROM %Q.'%q_idx';",
219711 pConfig->zDb, pConfig->zName,
219712 pConfig->zDb, pConfig->zName
219713 );
219714 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
219715 rc = fts5ExecPrintf(pConfig->db, 0,
219716 "DELETE FROM %Q.'%q_docsize';",
219717 pConfig->zDb, pConfig->zName
219718 );
219719 }
219720
219721 /* Reinitialize the %_data table. This call creates the initial structure
219722 ** and averages records. */
219723 if( rc==SQLITE_OK0 ){
219724 rc = sqlite3Fts5IndexReinit(p->pIndex);
219725 }
219726 if( rc==SQLITE_OK0 ){
219727 rc = sqlite3Fts5StorageConfigValue(p, "version", 0, FTS5_CURRENT_VERSION);
219728 }
219729 return rc;
219730}
219731
219732static int sqlite3Fts5StorageRebuild(Fts5Storage *p){
219733 Fts5Buffer buf = {0,0,0};
219734 Fts5Config *pConfig = p->pConfig;
219735 sqlite3_stmt *pScan = 0;
219736 Fts5InsertCtx ctx;
219737 int rc, rc2;
219738
219739 memset(&ctx, 0, sizeof(Fts5InsertCtx));
219740 ctx.pStorage = p;
219741 rc = sqlite3Fts5StorageDeleteAll(p);
219742 if( rc==SQLITE_OK0 ){
219743 rc = fts5StorageLoadTotals(p, 1);
219744 }
219745
219746 if( rc==SQLITE_OK0 ){
219747 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
219748 }
219749
219750 while( rc==SQLITE_OK0 && SQLITE_ROW100==sqlite3_step(pScan) ){
219751 i64 iRowid = sqlite3_column_int64(pScan, 0);
219752
219753 sqlite3Fts5BufferZero(&buf);
219754 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
219755 for(ctx.iCol=0; rc==SQLITE_OK0 && ctx.iCol<pConfig->nCol; ctx.iCol++){
219756 ctx.szCol = 0;
219757 if( pConfig->abUnindexed[ctx.iCol]==0 ){
219758 rc = sqlite3Fts5Tokenize(pConfig,
219759 FTS5_TOKENIZE_DOCUMENT0x0004,
219760 (const char*)sqlite3_column_text(pScan, ctx.iCol+1),
219761 sqlite3_column_bytes(pScan, ctx.iCol+1),
219762 (void*)&ctx,
219763 fts5StorageInsertCallback
219764 );
219765 }
219766 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
219767 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
219768 }
219769 p->nTotalRow++;
219770
219771 if( rc==SQLITE_OK0 ){
219772 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
219773 }
219774 }
219775 sqlite3_free(buf.p);
219776 rc2 = sqlite3_reset(pScan);
219777 if( rc==SQLITE_OK0 ) rc = rc2;
219778
219779 /* Write the averages record */
219780 if( rc==SQLITE_OK0 ){
219781 rc = fts5StorageSaveTotals(p);
219782 }
219783 return rc;
219784}
219785
219786static int sqlite3Fts5StorageOptimize(Fts5Storage *p){
219787 return sqlite3Fts5IndexOptimize(p->pIndex);
219788}
219789
219790static int sqlite3Fts5StorageMerge(Fts5Storage *p, int nMerge){
219791 return sqlite3Fts5IndexMerge(p->pIndex, nMerge);
219792}
219793
219794static int sqlite3Fts5StorageReset(Fts5Storage *p){
219795 return sqlite3Fts5IndexReset(p->pIndex);
219796}
219797
219798/*
219799** Allocate a new rowid. This is used for "external content" tables when
219800** a NULL value is inserted into the rowid column. The new rowid is allocated
219801** by inserting a dummy row into the %_docsize table. The dummy will be
219802** overwritten later.
219803**
219804** If the %_docsize table does not exist, SQLITE_MISMATCH is returned. In
219805** this case the user is required to provide a rowid explicitly.
219806*/
219807static int fts5StorageNewRowid(Fts5Storage *p, i64 *piRowid){
219808 int rc = SQLITE_MISMATCH20;
219809 if( p->pConfig->bColumnsize ){
219810 sqlite3_stmt *pReplace = 0;
219811 rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_DOCSIZE, &pReplace, 0);
219812 if( rc==SQLITE_OK0 ){
219813 sqlite3_bind_null(pReplace, 1);
219814 sqlite3_bind_null(pReplace, 2);
219815 sqlite3_step(pReplace);
219816 rc = sqlite3_reset(pReplace);
219817 }
219818 if( rc==SQLITE_OK0 ){
219819 *piRowid = sqlite3_last_insert_rowid(p->pConfig->db);
219820 }
219821 }
219822 return rc;
219823}
219824
219825/*
219826** Insert a new row into the FTS content table.
219827*/
219828static int sqlite3Fts5StorageContentInsert(
219829 Fts5Storage *p,
219830 sqlite3_value **apVal,
219831 i64 *piRowid
219832){
219833 Fts5Config *pConfig = p->pConfig;
219834 int rc = SQLITE_OK0;
219835
219836 /* Insert the new row into the %_content table. */
219837 if( pConfig->eContent!=FTS5_CONTENT_NORMAL ){
219838 if( sqlite3_value_type(apVal[1])==SQLITE_INTEGER1 ){
219839 *piRowid = sqlite3_value_int64(apVal[1]);
219840 }else{
219841 rc = fts5StorageNewRowid(p, piRowid);
219842 }
219843 }else{
219844 sqlite3_stmt *pInsert = 0; /* Statement to write %_content table */
219845 int i; /* Counter variable */
219846 rc = fts5StorageGetStmt(p, FTS5_STMT_INSERT_CONTENT, &pInsert, 0);
219847 for(i=1; rc==SQLITE_OK0 && i<=pConfig->nCol+1; i++){
219848 rc = sqlite3_bind_value(pInsert, i, apVal[i]);
219849 }
219850 if( rc==SQLITE_OK0 ){
219851 sqlite3_step(pInsert);
219852 rc = sqlite3_reset(pInsert);
219853 }
219854 *piRowid = sqlite3_last_insert_rowid(pConfig->db);
219855 }
219856
219857 return rc;
219858}
219859
219860/*
219861** Insert new entries into the FTS index and %_docsize table.
219862*/
219863static int sqlite3Fts5StorageIndexInsert(
219864 Fts5Storage *p,
219865 sqlite3_value **apVal,
219866 i64 iRowid
219867){
219868 Fts5Config *pConfig = p->pConfig;
219869 int rc = SQLITE_OK0; /* Return code */
219870 Fts5InsertCtx ctx; /* Tokenization callback context object */
219871 Fts5Buffer buf; /* Buffer used to build up %_docsize blob */
219872
219873 memset(&buf, 0, sizeof(Fts5Buffer));
219874 ctx.pStorage = p;
219875 rc = fts5StorageLoadTotals(p, 1);
219876
219877 if( rc==SQLITE_OK0 ){
219878 rc = sqlite3Fts5IndexBeginWrite(p->pIndex, 0, iRowid);
219879 }
219880 for(ctx.iCol=0; rc==SQLITE_OK0 && ctx.iCol<pConfig->nCol; ctx.iCol++){
219881 ctx.szCol = 0;
219882 if( pConfig->abUnindexed[ctx.iCol]==0 ){
219883 rc = sqlite3Fts5Tokenize(pConfig,
219884 FTS5_TOKENIZE_DOCUMENT0x0004,
219885 (const char*)sqlite3_value_text(apVal[ctx.iCol+2]),
219886 sqlite3_value_bytes(apVal[ctx.iCol+2]),
219887 (void*)&ctx,
219888 fts5StorageInsertCallback
219889 );
219890 }
219891 sqlite3Fts5BufferAppendVarint(&rc, &buf, ctx.szCol);
219892 p->aTotalSize[ctx.iCol] += (i64)ctx.szCol;
219893 }
219894 p->nTotalRow++;
219895
219896 /* Write the %_docsize record */
219897 if( rc==SQLITE_OK0 ){
219898 rc = fts5StorageInsertDocsize(p, iRowid, &buf);
219899 }
219900 sqlite3_free(buf.p);
219901
219902 return rc;
219903}
219904
219905static int fts5StorageCount(Fts5Storage *p, const char *zSuffix, i64 *pnRow){
219906 Fts5Config *pConfig = p->pConfig;
219907 char *zSql;
219908 int rc;
219909
219910 zSql = sqlite3_mprintf("SELECT count(*) FROM %Q.'%q_%s'",
219911 pConfig->zDb, pConfig->zName, zSuffix
219912 );
219913 if( zSql==0 ){
219914 rc = SQLITE_NOMEM7;
219915 }else{
219916 sqlite3_stmt *pCnt = 0;
219917 rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pCnt, 0);
219918 if( rc==SQLITE_OK0 ){
219919 if( SQLITE_ROW100==sqlite3_step(pCnt) ){
219920 *pnRow = sqlite3_column_int64(pCnt, 0);
219921 }
219922 rc = sqlite3_finalize(pCnt);
219923 }
219924 }
219925
219926 sqlite3_free(zSql);
219927 return rc;
219928}
219929
219930/*
219931** Context object used by sqlite3Fts5StorageIntegrity().
219932*/
219933typedef struct Fts5IntegrityCtx Fts5IntegrityCtx;
219934struct Fts5IntegrityCtx {
219935 i64 iRowid;
219936 int iCol;
219937 int szCol;
219938 u64 cksum;
219939 Fts5Termset *pTermset;
219940 Fts5Config *pConfig;
219941};
219942
219943
219944/*
219945** Tokenization callback used by integrity check.
219946*/
219947static int fts5StorageIntegrityCallback(
219948 void *pContext, /* Pointer to Fts5IntegrityCtx object */
219949 int tflags,
219950 const char *pToken, /* Buffer containing token */
219951 int nToken, /* Size of token in bytes */
219952 int iUnused1, /* Start offset of token */
219953 int iUnused2 /* End offset of token */
219954){
219955 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
219956 Fts5Termset *pTermset = pCtx->pTermset;
219957 int bPresent;
219958 int ii;
219959 int rc = SQLITE_OK0;
219960 int iPos;
219961 int iCol;
219962
219963 UNUSED_PARAM2(iUnused1, iUnused2);
219964 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
219965
219966 if( (tflags & FTS5_TOKEN_COLOCATED0x0001)==0 || pCtx->szCol==0 ){
219967 pCtx->szCol++;
219968 }
219969
219970 switch( pCtx->pConfig->eDetail ){
219971 case FTS5_DETAIL_FULL:
219972 iPos = pCtx->szCol-1;
219973 iCol = pCtx->iCol;
219974 break;
219975
219976 case FTS5_DETAIL_COLUMNS:
219977 iPos = pCtx->iCol;
219978 iCol = 0;
219979 break;
219980
219981 default:
219982 assert( pCtx->pConfig->eDetail==FTS5_DETAIL_NONE )((void) (0));
219983 iPos = 0;
219984 iCol = 0;
219985 break;
219986 }
219987
219988 rc = sqlite3Fts5TermsetAdd(pTermset, 0, pToken, nToken, &bPresent);
219989 if( rc==SQLITE_OK0 && bPresent==0 ){
219990 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
219991 pCtx->iRowid, iCol, iPos, 0, pToken, nToken
219992 );
219993 }
219994
219995 for(ii=0; rc==SQLITE_OK0 && ii<pCtx->pConfig->nPrefix; ii++){
219996 const int nChar = pCtx->pConfig->aPrefix[ii];
219997 int nByte = sqlite3Fts5IndexCharlenToBytelen(pToken, nToken, nChar);
219998 if( nByte ){
219999 rc = sqlite3Fts5TermsetAdd(pTermset, ii+1, pToken, nByte, &bPresent);
220000 if( bPresent==0 ){
220001 pCtx->cksum ^= sqlite3Fts5IndexEntryCksum(
220002 pCtx->iRowid, iCol, iPos, ii+1, pToken, nByte
220003 );
220004 }
220005 }
220006 }
220007
220008 return rc;
220009}
220010
220011/*
220012** Check that the contents of the FTS index match that of the %_content
220013** table. Return SQLITE_OK if they do, or SQLITE_CORRUPT if not. Return
220014** some other SQLite error code if an error occurs while attempting to
220015** determine this.
220016*/
220017static int sqlite3Fts5StorageIntegrity(Fts5Storage *p){
220018 Fts5Config *pConfig = p->pConfig;
220019 int rc; /* Return code */
220020 int *aColSize; /* Array of size pConfig->nCol */
220021 i64 *aTotalSize; /* Array of size pConfig->nCol */
220022 Fts5IntegrityCtx ctx;
220023 sqlite3_stmt *pScan;
220024
220025 memset(&ctx, 0, sizeof(Fts5IntegrityCtx));
220026 ctx.pConfig = p->pConfig;
220027 aTotalSize = (i64*)sqlite3_malloc64(pConfig->nCol*(sizeof(int)+sizeof(i64)));
220028 if( !aTotalSize ) return SQLITE_NOMEM7;
220029 aColSize = (int*)&aTotalSize[pConfig->nCol];
220030 memset(aTotalSize, 0, sizeof(i64) * pConfig->nCol);
220031
220032 /* Generate the expected index checksum based on the contents of the
220033 ** %_content table. This block stores the checksum in ctx.cksum. */
220034 rc = fts5StorageGetStmt(p, FTS5_STMT_SCAN, &pScan, 0);
220035 if( rc==SQLITE_OK0 ){
220036 int rc2;
220037 while( SQLITE_ROW100==sqlite3_step(pScan) ){
220038 int i;
220039 ctx.iRowid = sqlite3_column_int64(pScan, 0);
220040 ctx.szCol = 0;
220041 if( pConfig->bColumnsize ){
220042 rc = sqlite3Fts5StorageDocsize(p, ctx.iRowid, aColSize);
220043 }
220044 if( rc==SQLITE_OK0 && pConfig->eDetail==FTS5_DETAIL_NONE ){
220045 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
220046 }
220047 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
220048 if( pConfig->abUnindexed[i] ) continue;
220049 ctx.iCol = i;
220050 ctx.szCol = 0;
220051 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
220052 rc = sqlite3Fts5TermsetNew(&ctx.pTermset);
220053 }
220054 if( rc==SQLITE_OK0 ){
220055 rc = sqlite3Fts5Tokenize(pConfig,
220056 FTS5_TOKENIZE_DOCUMENT0x0004,
220057 (const char*)sqlite3_column_text(pScan, i+1),
220058 sqlite3_column_bytes(pScan, i+1),
220059 (void*)&ctx,
220060 fts5StorageIntegrityCallback
220061 );
220062 }
220063 if( rc==SQLITE_OK0 && pConfig->bColumnsize && ctx.szCol!=aColSize[i] ){
220064 rc = FTS5_CORRUPT;
220065 }
220066 aTotalSize[i] += ctx.szCol;
220067 if( pConfig->eDetail==FTS5_DETAIL_COLUMNS ){
220068 sqlite3Fts5TermsetFree(ctx.pTermset);
220069 ctx.pTermset = 0;
220070 }
220071 }
220072 sqlite3Fts5TermsetFree(ctx.pTermset);
220073 ctx.pTermset = 0;
220074
220075 if( rc!=SQLITE_OK0 ) break;
220076 }
220077 rc2 = sqlite3_reset(pScan);
220078 if( rc==SQLITE_OK0 ) rc = rc2;
220079 }
220080
220081 /* Test that the "totals" (sometimes called "averages") record looks Ok */
220082 if( rc==SQLITE_OK0 ){
220083 int i;
220084 rc = fts5StorageLoadTotals(p, 0);
220085 for(i=0; rc==SQLITE_OK0 && i<pConfig->nCol; i++){
220086 if( p->aTotalSize[i]!=aTotalSize[i] ) rc = FTS5_CORRUPT;
220087 }
220088 }
220089
220090 /* Check that the %_docsize and %_content tables contain the expected
220091 ** number of rows. */
220092 if( rc==SQLITE_OK0 && pConfig->eContent==FTS5_CONTENT_NORMAL ){
220093 i64 nRow = 0;
220094 rc = fts5StorageCount(p, "content", &nRow);
220095 if( rc==SQLITE_OK0 && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
220096 }
220097 if( rc==SQLITE_OK0 && pConfig->bColumnsize ){
220098 i64 nRow = 0;
220099 rc = fts5StorageCount(p, "docsize", &nRow);
220100 if( rc==SQLITE_OK0 && nRow!=p->nTotalRow ) rc = FTS5_CORRUPT;
220101 }
220102
220103 /* Pass the expected checksum down to the FTS index module. It will
220104 ** verify, amongst other things, that it matches the checksum generated by
220105 ** inspecting the index itself. */
220106 if( rc==SQLITE_OK0 ){
220107 rc = sqlite3Fts5IndexIntegrityCheck(p->pIndex, ctx.cksum);
220108 }
220109
220110 sqlite3_free(aTotalSize);
220111 return rc;
220112}
220113
220114/*
220115** Obtain an SQLite statement handle that may be used to read data from the
220116** %_content table.
220117*/
220118static int sqlite3Fts5StorageStmt(
220119 Fts5Storage *p,
220120 int eStmt,
220121 sqlite3_stmt **pp,
220122 char **pzErrMsg
220123){
220124 int rc;
220125 assert( eStmt==FTS5_STMT_SCAN_ASC((void) (0))
220126 || eStmt==FTS5_STMT_SCAN_DESC((void) (0))
220127 || eStmt==FTS5_STMT_LOOKUP((void) (0))
220128 )((void) (0));
220129 rc = fts5StorageGetStmt(p, eStmt, pp, pzErrMsg);
220130 if( rc==SQLITE_OK0 ){
220131 assert( p->aStmt[eStmt]==*pp )((void) (0));
220132 p->aStmt[eStmt] = 0;
220133 }
220134 return rc;
220135}
220136
220137/*
220138** Release an SQLite statement handle obtained via an earlier call to
220139** sqlite3Fts5StorageStmt(). The eStmt parameter passed to this function
220140** must match that passed to the sqlite3Fts5StorageStmt() call.
220141*/
220142static void sqlite3Fts5StorageStmtRelease(
220143 Fts5Storage *p,
220144 int eStmt,
220145 sqlite3_stmt *pStmt
220146){
220147 assert( eStmt==FTS5_STMT_SCAN_ASC((void) (0))
220148 || eStmt==FTS5_STMT_SCAN_DESC((void) (0))
220149 || eStmt==FTS5_STMT_LOOKUP((void) (0))
220150 )((void) (0));
220151 if( p->aStmt[eStmt]==0 ){
220152 sqlite3_reset(pStmt);
220153 p->aStmt[eStmt] = pStmt;
220154 }else{
220155 sqlite3_finalize(pStmt);
220156 }
220157}
220158
220159static int fts5StorageDecodeSizeArray(
220160 int *aCol, int nCol, /* Array to populate */
220161 const u8 *aBlob, int nBlob /* Record to read varints from */
220162){
220163 int i;
220164 int iOff = 0;
220165 for(i=0; i<nCol; i++){
220166 if( iOff>=nBlob ) return 1;
220167 iOff += fts5GetVarint32(&aBlob[iOff], aCol[i]);
220168 }
220169 return (iOff!=nBlob);
220170}
220171
220172/*
220173** Argument aCol points to an array of integers containing one entry for
220174** each table column. This function reads the %_docsize record for the
220175** specified rowid and populates aCol[] with the results.
220176**
220177** An SQLite error code is returned if an error occurs, or SQLITE_OK
220178** otherwise.
220179*/
220180static int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
220181 int nCol = p->pConfig->nCol; /* Number of user columns in table */
220182 sqlite3_stmt *pLookup = 0; /* Statement to query %_docsize */
220183 int rc; /* Return Code */
220184
220185 assert( p->pConfig->bColumnsize )((void) (0));
220186 rc = fts5StorageGetStmt(p, FTS5_STMT_LOOKUP_DOCSIZE, &pLookup, 0);
220187 if( rc==SQLITE_OK0 ){
220188 int bCorrupt = 1;
220189 sqlite3_bind_int64(pLookup, 1, iRowid);
220190 if( SQLITE_ROW100==sqlite3_step(pLookup) ){
220191 const u8 *aBlob = sqlite3_column_blob(pLookup, 0);
220192 int nBlob = sqlite3_column_bytes(pLookup, 0);
220193 if( 0==fts5StorageDecodeSizeArray(aCol, nCol, aBlob, nBlob) ){
220194 bCorrupt = 0;
220195 }
220196 }
220197 rc = sqlite3_reset(pLookup);
220198 if( bCorrupt && rc==SQLITE_OK0 ){
220199 rc = FTS5_CORRUPT;
220200 }
220201 }
220202
220203 return rc;
220204}
220205
220206static int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
220207 int rc = fts5StorageLoadTotals(p, 0);
220208 if( rc==SQLITE_OK0 ){
220209 *pnToken = 0;
220210 if( iCol<0 ){
220211 int i;
220212 for(i=0; i<p->pConfig->nCol; i++){
220213 *pnToken += p->aTotalSize[i];
220214 }
220215 }else if( iCol<p->pConfig->nCol ){
220216 *pnToken = p->aTotalSize[iCol];
220217 }else{
220218 rc = SQLITE_RANGE25;
220219 }
220220 }
220221 return rc;
220222}
220223
220224static int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
220225 int rc = fts5StorageLoadTotals(p, 0);
220226 if( rc==SQLITE_OK0 ){
220227 /* nTotalRow being zero does not necessarily indicate a corrupt
220228 ** database - it might be that the FTS5 table really does contain zero
220229 ** rows. However this function is only called from the xRowCount() API,
220230 ** and there is no way for that API to be invoked if the table contains
220231 ** no rows. Hence the FTS5_CORRUPT return. */
220232 *pnRow = p->nTotalRow;
220233 if( p->nTotalRow<=0 ) rc = FTS5_CORRUPT;
220234 }
220235 return rc;
220236}
220237
220238/*
220239** Flush any data currently held in-memory to disk.
220240*/
220241static int sqlite3Fts5StorageSync(Fts5Storage *p){
220242 int rc = SQLITE_OK0;
220243 i64 iLastRowid = sqlite3_last_insert_rowid(p->pConfig->db);
220244 if( p->bTotalsValid ){
220245 rc = fts5StorageSaveTotals(p);
220246 p->bTotalsValid = 0;
220247 }
220248 if( rc==SQLITE_OK0 ){
220249 rc = sqlite3Fts5IndexSync(p->pIndex);
220250 }
220251 sqlite3_set_last_insert_rowid(p->pConfig->db, iLastRowid);
220252 return rc;
220253}
220254
220255static int sqlite3Fts5StorageRollback(Fts5Storage *p){
220256 p->bTotalsValid = 0;
220257 return sqlite3Fts5IndexRollback(p->pIndex);
220258}
220259
220260static int sqlite3Fts5StorageConfigValue(
220261 Fts5Storage *p,
220262 const char *z,
220263 sqlite3_value *pVal,
220264 int iVal
220265){
220266 sqlite3_stmt *pReplace = 0;
220267 int rc = fts5StorageGetStmt(p, FTS5_STMT_REPLACE_CONFIG, &pReplace, 0);
220268 if( rc==SQLITE_OK0 ){
220269 sqlite3_bind_text(pReplace, 1, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
220270 if( pVal ){
220271 sqlite3_bind_value(pReplace, 2, pVal);
220272 }else{
220273 sqlite3_bind_int(pReplace, 2, iVal);
220274 }
220275 sqlite3_step(pReplace);
220276 rc = sqlite3_reset(pReplace);
220277 sqlite3_bind_null(pReplace, 1);
220278 }
220279 if( rc==SQLITE_OK0 && pVal ){
220280 int iNew = p->pConfig->iCookie + 1;
220281 rc = sqlite3Fts5IndexSetCookie(p->pIndex, iNew);
220282 if( rc==SQLITE_OK0 ){
220283 p->pConfig->iCookie = iNew;
220284 }
220285 }
220286 return rc;
220287}
220288
220289/*
220290** 2014 May 31
220291**
220292** The author disclaims copyright to this source code. In place of
220293** a legal notice, here is a blessing:
220294**
220295** May you do good and not evil.
220296** May you find forgiveness for yourself and forgive others.
220297** May you share freely, never taking more than you give.
220298**
220299******************************************************************************
220300*/
220301
220302
220303/* #include "fts5Int.h" */
220304
220305/**************************************************************************
220306** Start of ascii tokenizer implementation.
220307*/
220308
220309/*
220310** For tokenizers with no "unicode" modifier, the set of token characters
220311** is the same as the set of ASCII range alphanumeric characters.
220312*/
220313static unsigned char aAsciiTokenChar[128] = {
220314 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00..0x0F */
220315 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10..0x1F */
220316 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20..0x2F */
220317 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30..0x3F */
220318 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40..0x4F */
220319 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50..0x5F */
220320 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60..0x6F */
220321 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x70..0x7F */
220322};
220323
220324typedef struct AsciiTokenizer AsciiTokenizer;
220325struct AsciiTokenizer {
220326 unsigned char aTokenChar[128];
220327};
220328
220329static void fts5AsciiAddExceptions(
220330 AsciiTokenizer *p,
220331 const char *zArg,
220332 int bTokenChars
220333){
220334 int i;
220335 for(i=0; zArg[i]; i++){
220336 if( (zArg[i] & 0x80)==0 ){
220337 p->aTokenChar[(int)zArg[i]] = (unsigned char)bTokenChars;
220338 }
220339 }
220340}
220341
220342/*
220343** Delete a "ascii" tokenizer.
220344*/
220345static void fts5AsciiDelete(Fts5Tokenizer *p){
220346 sqlite3_free(p);
220347}
220348
220349/*
220350** Create an "ascii" tokenizer.
220351*/
220352static int fts5AsciiCreate(
220353 void *pUnused,
220354 const char **azArg, int nArg,
220355 Fts5Tokenizer **ppOut
220356){
220357 int rc = SQLITE_OK0;
220358 AsciiTokenizer *p = 0;
220359 UNUSED_PARAM(pUnused);
220360 if( nArg%2 ){
220361 rc = SQLITE_ERROR1;
220362 }else{
220363 p = sqlite3_malloc(sizeof(AsciiTokenizer));
220364 if( p==0 ){
220365 rc = SQLITE_NOMEM7;
220366 }else{
220367 int i;
220368 memset(p, 0, sizeof(AsciiTokenizer));
220369 memcpy(p->aTokenChar, aAsciiTokenChar, sizeof(aAsciiTokenChar));
220370 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
220371 const char *zArg = azArg[i+1];
220372 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
220373 fts5AsciiAddExceptions(p, zArg, 1);
220374 }else
220375 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
220376 fts5AsciiAddExceptions(p, zArg, 0);
220377 }else{
220378 rc = SQLITE_ERROR1;
220379 }
220380 }
220381 if( rc!=SQLITE_OK0 ){
220382 fts5AsciiDelete((Fts5Tokenizer*)p);
220383 p = 0;
220384 }
220385 }
220386 }
220387
220388 *ppOut = (Fts5Tokenizer*)p;
220389 return rc;
220390}
220391
220392
220393static void asciiFold(char *aOut, const char *aIn, int nByte){
220394 int i;
220395 for(i=0; i<nByte; i++){
220396 char c = aIn[i];
220397 if( c>='A' && c<='Z' ) c += 32;
220398 aOut[i] = c;
220399 }
220400}
220401
220402/*
220403** Tokenize some text using the ascii tokenizer.
220404*/
220405static int fts5AsciiTokenize(
220406 Fts5Tokenizer *pTokenizer,
220407 void *pCtx,
220408 int iUnused,
220409 const char *pText, int nText,
220410 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
220411){
220412 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
220413 int rc = SQLITE_OK0;
220414 int ie;
220415 int is = 0;
220416
220417 char aFold[64];
220418 int nFold = sizeof(aFold);
220419 char *pFold = aFold;
220420 unsigned char *a = p->aTokenChar;
220421
220422 UNUSED_PARAM(iUnused);
220423
220424 while( is<nText && rc==SQLITE_OK0 ){
220425 int nByte;
220426
220427 /* Skip any leading divider characters. */
220428 while( is<nText && ((pText[is]&0x80)==0 && a[(int)pText[is]]==0) ){
220429 is++;
220430 }
220431 if( is==nText ) break;
220432
220433 /* Count the token characters */
220434 ie = is+1;
220435 while( ie<nText && ((pText[ie]&0x80) || a[(int)pText[ie]] ) ){
220436 ie++;
220437 }
220438
220439 /* Fold to lower case */
220440 nByte = ie-is;
220441 if( nByte>nFold ){
220442 if( pFold!=aFold ) sqlite3_free(pFold);
220443 pFold = sqlite3_malloc64((sqlite3_int64)nByte*2);
220444 if( pFold==0 ){
220445 rc = SQLITE_NOMEM7;
220446 break;
220447 }
220448 nFold = nByte*2;
220449 }
220450 asciiFold(pFold, &pText[is], nByte);
220451
220452 /* Invoke the token callback */
220453 rc = xToken(pCtx, 0, pFold, nByte, is, ie);
220454 is = ie+1;
220455 }
220456
220457 if( pFold!=aFold ) sqlite3_free(pFold);
220458 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
220459 return rc;
220460}
220461
220462/**************************************************************************
220463** Start of unicode61 tokenizer implementation.
220464*/
220465
220466
220467/*
220468** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
220469** from the sqlite3 source file utf.c. If this file is compiled as part
220470** of the amalgamation, they are not required.
220471*/
220472#ifndef SQLITE_AMALGAMATION1
220473
220474static const unsigned char sqlite3Utf8Trans1[] = {
220475 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
220476 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
220477 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
220478 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
220479 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
220480 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
220481 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
220482 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
220483};
220484
220485#define READ_UTF8(zIn, zTerm, c)c = *(zIn++); if( c>=0xc0 ){ c = sqlite3Utf8Trans1[c-0xc0]
; while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ c =
(c<<6) + (0x3f & *(zIn++)); } if( c<0x80 || (c&
0xFFFFF800)==0xD800 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD
; } }
\
220486 c = *(zIn++); \
220487 if( c>=0xc0 ){ \
220488 c = sqlite3Utf8Trans1[c-0xc0]; \
220489 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
220490 c = (c<<6) + (0x3f & *(zIn++)); \
220491 } \
220492 if( c<0x80 \
220493 || (c&0xFFFFF800)==0xD800 \
220494 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
220495 }
220496
220497
220498#define WRITE_UTF8(zOut, c){ if( c<0x00080 ){ *zOut++ = (u8)(c&0xFF); } else if( c
<0x00800 ){ *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); *
zOut++ = 0x80 + (u8)(c & 0x3F); } else if( c<0x10000 )
{ *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); *zOut++ = 0x80
+ (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(c &
0x3F); }else{ *zOut++ = 0xF0 + (u8)((c>>18) & 0x07
); *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); *zOut++ =
0x80 + (u8)((c>>6) & 0x3F); *zOut++ = 0x80 + (u8)(
c & 0x3F); } }
{ \
220499 if( c<0x00080 ){ \
220500 *zOut++ = (unsigned char)(c&0xFF); \
220501 } \
220502 else if( c<0x00800 ){ \
220503 *zOut++ = 0xC0 + (unsigned char)((c>>6)&0x1F); \
220504 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
220505 } \
220506 else if( c<0x10000 ){ \
220507 *zOut++ = 0xE0 + (unsigned char)((c>>12)&0x0F); \
220508 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
220509 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
220510 }else{ \
220511 *zOut++ = 0xF0 + (unsigned char)((c>>18) & 0x07); \
220512 *zOut++ = 0x80 + (unsigned char)((c>>12) & 0x3F); \
220513 *zOut++ = 0x80 + (unsigned char)((c>>6) & 0x3F); \
220514 *zOut++ = 0x80 + (unsigned char)(c & 0x3F); \
220515 } \
220516}
220517
220518#endif /* ifndef SQLITE_AMALGAMATION */
220519
220520typedef struct Unicode61Tokenizer Unicode61Tokenizer;
220521struct Unicode61Tokenizer {
220522 unsigned char aTokenChar[128]; /* ASCII range token characters */
220523 char *aFold; /* Buffer to fold text into */
220524 int nFold; /* Size of aFold[] in bytes */
220525 int eRemoveDiacritic; /* True if remove_diacritics=1 is set */
220526 int nException;
220527 int *aiException;
220528
220529 unsigned char aCategory[32]; /* True for token char categories */
220530};
220531
220532/* Values for eRemoveDiacritic (must match internals of fts5_unicode2.c) */
220533#define FTS5_REMOVE_DIACRITICS_NONE 0
220534#define FTS5_REMOVE_DIACRITICS_SIMPLE 1
220535#define FTS5_REMOVE_DIACRITICS_COMPLEX 2
220536
220537static int fts5UnicodeAddExceptions(
220538 Unicode61Tokenizer *p, /* Tokenizer object */
220539 const char *z, /* Characters to treat as exceptions */
220540 int bTokenChars /* 1 for 'tokenchars', 0 for 'separators' */
220541){
220542 int rc = SQLITE_OK0;
220543 int n = (int)strlen(z);
220544 int *aNew;
220545
220546 if( n>0 ){
220547 aNew = (int*)sqlite3_realloc64(p->aiException,
220548 (n+p->nException)*sizeof(int));
220549 if( aNew ){
220550 int nNew = p->nException;
220551 const unsigned char *zCsr = (const unsigned char*)z;
220552 const unsigned char *zTerm = (const unsigned char*)&z[n];
220553 while( zCsr<zTerm ){
220554 u32 iCode;
220555 int bToken;
220556 READ_UTF8(zCsr, zTerm, iCode)iCode = *(zCsr++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( zCsr!=zTerm && (*zCsr & 0xc0
)==0x80 ){ iCode = (iCode<<6) + (0x3f & *(zCsr++));
} if( iCode<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode
&0xFFFFFFFE)==0xFFFE ){ iCode = 0xFFFD; } }
;
220557 if( iCode<128 ){
220558 p->aTokenChar[iCode] = (unsigned char)bTokenChars;
220559 }else{
220560 bToken = p->aCategory[sqlite3Fts5UnicodeCategory(iCode)];
220561 assert( (bToken==0 || bToken==1) )((void) (0));
220562 assert( (bTokenChars==0 || bTokenChars==1) )((void) (0));
220563 if( bToken!=bTokenChars && sqlite3Fts5UnicodeIsdiacritic(iCode)==0 ){
220564 int i;
220565 for(i=0; i<nNew; i++){
220566 if( (u32)aNew[i]>iCode ) break;
220567 }
220568 memmove(&aNew[i+1], &aNew[i], (nNew-i)*sizeof(int));
220569 aNew[i] = iCode;
220570 nNew++;
220571 }
220572 }
220573 }
220574 p->aiException = aNew;
220575 p->nException = nNew;
220576 }else{
220577 rc = SQLITE_NOMEM7;
220578 }
220579 }
220580
220581 return rc;
220582}
220583
220584/*
220585** Return true if the p->aiException[] array contains the value iCode.
220586*/
220587static int fts5UnicodeIsException(Unicode61Tokenizer *p, int iCode){
220588 if( p->nException>0 ){
220589 int *a = p->aiException;
220590 int iLo = 0;
220591 int iHi = p->nException-1;
220592
220593 while( iHi>=iLo ){
220594 int iTest = (iHi + iLo) / 2;
220595 if( iCode==a[iTest] ){
220596 return 1;
220597 }else if( iCode>a[iTest] ){
220598 iLo = iTest+1;
220599 }else{
220600 iHi = iTest-1;
220601 }
220602 }
220603 }
220604
220605 return 0;
220606}
220607
220608/*
220609** Delete a "unicode61" tokenizer.
220610*/
220611static void fts5UnicodeDelete(Fts5Tokenizer *pTok){
220612 if( pTok ){
220613 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTok;
220614 sqlite3_free(p->aiException);
220615 sqlite3_free(p->aFold);
220616 sqlite3_free(p);
220617 }
220618 return;
220619}
220620
220621static int unicodeSetCategories(Unicode61Tokenizer *p, const char *zCat){
220622 const char *z = zCat;
220623
220624 while( *z ){
220625 while( *z==' ' || *z=='\t' ) z++;
220626 if( *z && sqlite3Fts5UnicodeCatParse(z, p->aCategory) ){
220627 return SQLITE_ERROR1;
220628 }
220629 while( *z!=' ' && *z!='\t' && *z!='\0' ) z++;
220630 }
220631
220632 sqlite3Fts5UnicodeAscii(p->aCategory, p->aTokenChar);
220633 return SQLITE_OK0;
220634}
220635
220636/*
220637** Create a "unicode61" tokenizer.
220638*/
220639static int fts5UnicodeCreate(
220640 void *pUnused,
220641 const char **azArg, int nArg,
220642 Fts5Tokenizer **ppOut
220643){
220644 int rc = SQLITE_OK0; /* Return code */
220645 Unicode61Tokenizer *p = 0; /* New tokenizer object */
220646
220647 UNUSED_PARAM(pUnused);
220648
220649 if( nArg%2 ){
220650 rc = SQLITE_ERROR1;
220651 }else{
220652 p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
220653 if( p ){
220654 const char *zCat = "L* N* Co";
220655 int i;
220656 memset(p, 0, sizeof(Unicode61Tokenizer));
220657
220658 p->eRemoveDiacritic = FTS5_REMOVE_DIACRITICS_SIMPLE;
220659 p->nFold = 64;
220660 p->aFold = sqlite3_malloc64(p->nFold * sizeof(char));
220661 if( p->aFold==0 ){
220662 rc = SQLITE_NOMEM7;
220663 }
220664
220665 /* Search for a "categories" argument */
220666 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
220667 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
220668 zCat = azArg[i+1];
220669 }
220670 }
220671
220672 if( rc==SQLITE_OK0 ){
220673 rc = unicodeSetCategories(p, zCat);
220674 }
220675
220676 for(i=0; rc==SQLITE_OK0 && i<nArg; i+=2){
220677 const char *zArg = azArg[i+1];
220678 if( 0==sqlite3_stricmp(azArg[i], "remove_diacritics") ){
220679 if( (zArg[0]!='0' && zArg[0]!='1' && zArg[0]!='2') || zArg[1] ){
220680 rc = SQLITE_ERROR1;
220681 }else{
220682 p->eRemoveDiacritic = (zArg[0] - '0');
220683 assert( p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_NONE((void) (0))
220684 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_SIMPLE((void) (0))
220685 || p->eRemoveDiacritic==FTS5_REMOVE_DIACRITICS_COMPLEX((void) (0))
220686 )((void) (0));
220687 }
220688 }else
220689 if( 0==sqlite3_stricmp(azArg[i], "tokenchars") ){
220690 rc = fts5UnicodeAddExceptions(p, zArg, 1);
220691 }else
220692 if( 0==sqlite3_stricmp(azArg[i], "separators") ){
220693 rc = fts5UnicodeAddExceptions(p, zArg, 0);
220694 }else
220695 if( 0==sqlite3_stricmp(azArg[i], "categories") ){
220696 /* no-op */
220697 }else{
220698 rc = SQLITE_ERROR1;
220699 }
220700 }
220701
220702 }else{
220703 rc = SQLITE_NOMEM7;
220704 }
220705 if( rc!=SQLITE_OK0 ){
220706 fts5UnicodeDelete((Fts5Tokenizer*)p);
220707 p = 0;
220708 }
220709 *ppOut = (Fts5Tokenizer*)p;
220710 }
220711 return rc;
220712}
220713
220714/*
220715** Return true if, for the purposes of tokenizing with the tokenizer
220716** passed as the first argument, codepoint iCode is considered a token
220717** character (not a separator).
220718*/
220719static int fts5UnicodeIsAlnum(Unicode61Tokenizer *p, int iCode){
220720 return (
220721 p->aCategory[sqlite3Fts5UnicodeCategory((u32)iCode)]
220722 ^ fts5UnicodeIsException(p, iCode)
220723 );
220724}
220725
220726static int fts5UnicodeTokenize(
220727 Fts5Tokenizer *pTokenizer,
220728 void *pCtx,
220729 int iUnused,
220730 const char *pText, int nText,
220731 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
220732){
220733 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
220734 int rc = SQLITE_OK0;
220735 unsigned char *a = p->aTokenChar;
220736
220737 unsigned char *zTerm = (unsigned char*)&pText[nText];
220738 unsigned char *zCsr = (unsigned char *)pText;
220739
220740 /* Output buffer */
220741 char *aFold = p->aFold;
220742 int nFold = p->nFold;
220743 const char *pEnd = &aFold[nFold-6];
220744
220745 UNUSED_PARAM(iUnused);
220746
220747 /* Each iteration of this loop gobbles up a contiguous run of separators,
220748 ** then the next token. */
220749 while( rc==SQLITE_OK0 ){
220750 u32 iCode; /* non-ASCII codepoint read from input */
220751 char *zOut = aFold;
220752 int is;
220753 int ie;
220754
220755 /* Skip any separator characters. */
220756 while( 1 ){
220757 if( zCsr>=zTerm ) goto tokenize_done;
220758 if( *zCsr & 0x80 ) {
220759 /* A character outside of the ascii range. Skip past it if it is
220760 ** a separator character. Or break out of the loop if it is not. */
220761 is = zCsr - (unsigned char*)pText;
220762 READ_UTF8(zCsr, zTerm, iCode)iCode = *(zCsr++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( zCsr!=zTerm && (*zCsr & 0xc0
)==0x80 ){ iCode = (iCode<<6) + (0x3f & *(zCsr++));
} if( iCode<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode
&0xFFFFFFFE)==0xFFFE ){ iCode = 0xFFFD; } }
;
220763 if( fts5UnicodeIsAlnum(p, iCode) ){
220764 goto non_ascii_tokenchar;
220765 }
220766 }else{
220767 if( a[*zCsr] ){
220768 is = zCsr - (unsigned char*)pText;
220769 goto ascii_tokenchar;
220770 }
220771 zCsr++;
220772 }
220773 }
220774
220775 /* Run through the tokenchars. Fold them into the output buffer along
220776 ** the way. */
220777 while( zCsr<zTerm ){
220778
220779 /* Grow the output buffer so that there is sufficient space to fit the
220780 ** largest possible utf-8 character. */
220781 if( zOut>pEnd ){
220782 aFold = sqlite3_malloc64((sqlite3_int64)nFold*2);
220783 if( aFold==0 ){
220784 rc = SQLITE_NOMEM7;
220785 goto tokenize_done;
220786 }
220787 zOut = &aFold[zOut - p->aFold];
220788 memcpy(aFold, p->aFold, nFold);
220789 sqlite3_free(p->aFold);
220790 p->aFold = aFold;
220791 p->nFold = nFold = nFold*2;
220792 pEnd = &aFold[nFold-6];
220793 }
220794
220795 if( *zCsr & 0x80 ){
220796 /* An non-ascii-range character. Fold it into the output buffer if
220797 ** it is a token character, or break out of the loop if it is not. */
220798 READ_UTF8(zCsr, zTerm, iCode)iCode = *(zCsr++); if( iCode>=0xc0 ){ iCode = sqlite3Utf8Trans1
[iCode-0xc0]; while( zCsr!=zTerm && (*zCsr & 0xc0
)==0x80 ){ iCode = (iCode<<6) + (0x3f & *(zCsr++));
} if( iCode<0x80 || (iCode&0xFFFFF800)==0xD800 || (iCode
&0xFFFFFFFE)==0xFFFE ){ iCode = 0xFFFD; } }
;
220799 if( fts5UnicodeIsAlnum(p,iCode)||sqlite3Fts5UnicodeIsdiacritic(iCode) ){
220800 non_ascii_tokenchar:
220801 iCode = sqlite3Fts5UnicodeFold(iCode, p->eRemoveDiacritic);
220802 if( iCode ) WRITE_UTF8(zOut, iCode){ if( iCode<0x00080 ){ *zOut++ = (u8)(iCode&0xFF); } else
if( iCode<0x00800 ){ *zOut++ = 0xC0 + (u8)((iCode>>
6)&0x1F); *zOut++ = 0x80 + (u8)(iCode & 0x3F); } else
if( iCode<0x10000 ){ *zOut++ = 0xE0 + (u8)((iCode>>
12)&0x0F); *zOut++ = 0x80 + (u8)((iCode>>6) & 0x3F
); *zOut++ = 0x80 + (u8)(iCode & 0x3F); }else{ *zOut++ = 0xF0
+ (u8)((iCode>>18) & 0x07); *zOut++ = 0x80 + (u8)(
(iCode>>12) & 0x3F); *zOut++ = 0x80 + (u8)((iCode>>
6) & 0x3F); *zOut++ = 0x80 + (u8)(iCode & 0x3F); } }
;
220803 }else{
220804 break;
220805 }
220806 }else if( a[*zCsr]==0 ){
220807 /* An ascii-range separator character. End of token. */
220808 break;
220809 }else{
220810 ascii_tokenchar:
220811 if( *zCsr>='A' && *zCsr<='Z' ){
220812 *zOut++ = *zCsr + 32;
220813 }else{
220814 *zOut++ = *zCsr;
220815 }
220816 zCsr++;
220817 }
220818 ie = zCsr - (unsigned char*)pText;
220819 }
220820
220821 /* Invoke the token callback */
220822 rc = xToken(pCtx, 0, aFold, zOut-aFold, is, ie);
220823 }
220824
220825 tokenize_done:
220826 if( rc==SQLITE_DONE101 ) rc = SQLITE_OK0;
220827 return rc;
220828}
220829
220830/**************************************************************************
220831** Start of porter stemmer implementation.
220832*/
220833
220834/* Any tokens larger than this (in bytes) are passed through without
220835** stemming. */
220836#define FTS5_PORTER_MAX_TOKEN 64
220837
220838typedef struct PorterTokenizer PorterTokenizer;
220839struct PorterTokenizer {
220840 fts5_tokenizer tokenizer; /* Parent tokenizer module */
220841 Fts5Tokenizer *pTokenizer; /* Parent tokenizer instance */
220842 char aBuf[FTS5_PORTER_MAX_TOKEN + 64];
220843};
220844
220845/*
220846** Delete a "porter" tokenizer.
220847*/
220848static void fts5PorterDelete(Fts5Tokenizer *pTok){
220849 if( pTok ){
220850 PorterTokenizer *p = (PorterTokenizer*)pTok;
220851 if( p->pTokenizer ){
220852 p->tokenizer.xDelete(p->pTokenizer);
220853 }
220854 sqlite3_free(p);
220855 }
220856}
220857
220858/*
220859** Create a "porter" tokenizer.
220860*/
220861static int fts5PorterCreate(
220862 void *pCtx,
220863 const char **azArg, int nArg,
220864 Fts5Tokenizer **ppOut
220865){
220866 fts5_api *pApi = (fts5_api*)pCtx;
220867 int rc = SQLITE_OK0;
220868 PorterTokenizer *pRet;
220869 void *pUserdata = 0;
220870 const char *zBase = "unicode61";
220871
220872 if( nArg>0 ){
220873 zBase = azArg[0];
220874 }
220875
220876 pRet = (PorterTokenizer*)sqlite3_malloc(sizeof(PorterTokenizer));
220877 if( pRet ){
220878 memset(pRet, 0, sizeof(PorterTokenizer));
220879 rc = pApi->xFindTokenizer(pApi, zBase, &pUserdata, &pRet->tokenizer);
220880 }else{
220881 rc = SQLITE_NOMEM7;
220882 }
220883 if( rc==SQLITE_OK0 ){
220884 int nArg2 = (nArg>0 ? nArg-1 : 0);
220885 const char **azArg2 = (nArg2 ? &azArg[1] : 0);
220886 rc = pRet->tokenizer.xCreate(pUserdata, azArg2, nArg2, &pRet->pTokenizer);
220887 }
220888
220889 if( rc!=SQLITE_OK0 ){
220890 fts5PorterDelete((Fts5Tokenizer*)pRet);
220891 pRet = 0;
220892 }
220893 *ppOut = (Fts5Tokenizer*)pRet;
220894 return rc;
220895}
220896
220897typedef struct PorterContext PorterContext;
220898struct PorterContext {
220899 void *pCtx;
220900 int (*xToken)(void*, int, const char*, int, int, int);
220901 char *aBuf;
220902};
220903
220904typedef struct PorterRule PorterRule;
220905struct PorterRule {
220906 const char *zSuffix;
220907 int nSuffix;
220908 int (*xCond)(char *zStem, int nStem);
220909 const char *zOutput;
220910 int nOutput;
220911};
220912
220913#if 0
220914static int fts5PorterApply(char *aBuf, int *pnBuf, PorterRule *aRule){
220915 int ret = -1;
220916 int nBuf = *pnBuf;
220917 PorterRule *p;
220918
220919 for(p=aRule; p->zSuffix; p++){
220920 assert( strlen(p->zSuffix)==p->nSuffix )((void) (0));
220921 assert( strlen(p->zOutput)==p->nOutput )((void) (0));
220922 if( nBuf<p->nSuffix ) continue;
220923 if( 0==memcmp(&aBuf[nBuf - p->nSuffix], p->zSuffix, p->nSuffix) ) break;
220924 }
220925
220926 if( p->zSuffix ){
220927 int nStem = nBuf - p->nSuffix;
220928 if( p->xCond==0 || p->xCond(aBuf, nStem) ){
220929 memcpy(&aBuf[nStem], p->zOutput, p->nOutput);
220930 *pnBuf = nStem + p->nOutput;
220931 ret = p - aRule;
220932 }
220933 }
220934
220935 return ret;
220936}
220937#endif
220938
220939static int fts5PorterIsVowel(char c, int bYIsVowel){
220940 return (
220941 c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || (bYIsVowel && c=='y')
220942 );
220943}
220944
220945static int fts5PorterGobbleVC(char *zStem, int nStem, int bPrevCons){
220946 int i;
220947 int bCons = bPrevCons;
220948
220949 /* Scan for a vowel */
220950 for(i=0; i<nStem; i++){
220951 if( 0==(bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) break;
220952 }
220953
220954 /* Scan for a consonent */
220955 for(i++; i<nStem; i++){
220956 if( (bCons = !fts5PorterIsVowel(zStem[i], bCons)) ) return i+1;
220957 }
220958 return 0;
220959}
220960
220961/* porter rule condition: (m > 0) */
220962static int fts5Porter_MGt0(char *zStem, int nStem){
220963 return !!fts5PorterGobbleVC(zStem, nStem, 0);
220964}
220965
220966/* porter rule condition: (m > 1) */
220967static int fts5Porter_MGt1(char *zStem, int nStem){
220968 int n;
220969 n = fts5PorterGobbleVC(zStem, nStem, 0);
220970 if( n && fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
220971 return 1;
220972 }
220973 return 0;
220974}
220975
220976/* porter rule condition: (m = 1) */
220977static int fts5Porter_MEq1(char *zStem, int nStem){
220978 int n;
220979 n = fts5PorterGobbleVC(zStem, nStem, 0);
220980 if( n && 0==fts5PorterGobbleVC(&zStem[n], nStem-n, 1) ){
220981 return 1;
220982 }
220983 return 0;
220984}
220985
220986/* porter rule condition: (*o) */
220987static int fts5Porter_Ostar(char *zStem, int nStem){
220988 if( zStem[nStem-1]=='w' || zStem[nStem-1]=='x' || zStem[nStem-1]=='y' ){
220989 return 0;
220990 }else{
220991 int i;
220992 int mask = 0;
220993 int bCons = 0;
220994 for(i=0; i<nStem; i++){
220995 bCons = !fts5PorterIsVowel(zStem[i], bCons);
220996 assert( bCons==0 || bCons==1 )((void) (0));
220997 mask = (mask << 1) + bCons;
220998 }
220999 return ((mask & 0x0007)==0x0005);
221000 }
221001}
221002
221003/* porter rule condition: (m > 1 and (*S or *T)) */
221004static int fts5Porter_MGt1_and_S_or_T(char *zStem, int nStem){
221005 assert( nStem>0 )((void) (0));
221006 return (zStem[nStem-1]=='s' || zStem[nStem-1]=='t')
221007 && fts5Porter_MGt1(zStem, nStem);
221008}
221009
221010/* porter rule condition: (*v*) */
221011static int fts5Porter_Vowel(char *zStem, int nStem){
221012 int i;
221013 for(i=0; i<nStem; i++){
221014 if( fts5PorterIsVowel(zStem[i], i>0) ){
221015 return 1;
221016 }
221017 }
221018 return 0;
221019}
221020
221021
221022/**************************************************************************
221023***************************************************************************
221024** GENERATED CODE STARTS HERE (mkportersteps.tcl)
221025*/
221026
221027static int fts5PorterStep4(char *aBuf, int *pnBuf){
221028 int ret = 0;
221029 int nBuf = *pnBuf;
221030 switch( aBuf[nBuf-2] ){
221031
221032 case 'a':
221033 if( nBuf>2 && 0==memcmp("al", &aBuf[nBuf-2], 2) ){
221034 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
221035 *pnBuf = nBuf - 2;
221036 }
221037 }
221038 break;
221039
221040 case 'c':
221041 if( nBuf>4 && 0==memcmp("ance", &aBuf[nBuf-4], 4) ){
221042 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
221043 *pnBuf = nBuf - 4;
221044 }
221045 }else if( nBuf>4 && 0==memcmp("ence", &aBuf[nBuf-4], 4) ){
221046 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
221047 *pnBuf = nBuf - 4;
221048 }
221049 }
221050 break;
221051
221052 case 'e':
221053 if( nBuf>2 && 0==memcmp("er", &aBuf[nBuf-2], 2) ){
221054 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
221055 *pnBuf = nBuf - 2;
221056 }
221057 }
221058 break;
221059
221060 case 'i':
221061 if( nBuf>2 && 0==memcmp("ic", &aBuf[nBuf-2], 2) ){
221062 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
221063 *pnBuf = nBuf - 2;
221064 }
221065 }
221066 break;
221067
221068 case 'l':
221069 if( nBuf>4 && 0==memcmp("able", &aBuf[nBuf-4], 4) ){
221070 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
221071 *pnBuf = nBuf - 4;
221072 }
221073 }else if( nBuf>4 && 0==memcmp("ible", &aBuf[nBuf-4], 4) ){
221074 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
221075 *pnBuf = nBuf - 4;
221076 }
221077 }
221078 break;
221079
221080 case 'n':
221081 if( nBuf>3 && 0==memcmp("ant", &aBuf[nBuf-3], 3) ){
221082 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
221083 *pnBuf = nBuf - 3;
221084 }
221085 }else if( nBuf>5 && 0==memcmp("ement", &aBuf[nBuf-5], 5) ){
221086 if( fts5Porter_MGt1(aBuf, nBuf-5) ){
221087 *pnBuf = nBuf - 5;
221088 }
221089 }else if( nBuf>4 && 0==memcmp("ment", &aBuf[nBuf-4], 4) ){
221090 if( fts5Porter_MGt1(aBuf, nBuf-4) ){
221091 *pnBuf = nBuf - 4;
221092 }
221093 }else if( nBuf>3 && 0==memcmp("ent", &aBuf[nBuf-3], 3) ){
221094 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
221095 *pnBuf = nBuf - 3;
221096 }
221097 }
221098 break;
221099
221100 case 'o':
221101 if( nBuf>3 && 0==memcmp("ion", &aBuf[nBuf-3], 3) ){
221102 if( fts5Porter_MGt1_and_S_or_T(aBuf, nBuf-3) ){
221103 *pnBuf = nBuf - 3;
221104 }
221105 }else if( nBuf>2 && 0==memcmp("ou", &aBuf[nBuf-2], 2) ){
221106 if( fts5Porter_MGt1(aBuf, nBuf-2) ){
221107 *pnBuf = nBuf - 2;
221108 }
221109 }
221110 break;
221111
221112 case 's':
221113 if( nBuf>3 && 0==memcmp("ism", &aBuf[nBuf-3], 3) ){
221114 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
221115 *pnBuf = nBuf - 3;
221116 }
221117 }
221118 break;
221119
221120 case 't':
221121 if( nBuf>3 && 0==memcmp("ate", &aBuf[nBuf-3], 3) ){
221122 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
221123 *pnBuf = nBuf - 3;
221124 }
221125 }else if( nBuf>3 && 0==memcmp("iti", &aBuf[nBuf-3], 3) ){
221126 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
221127 *pnBuf = nBuf - 3;
221128 }
221129 }
221130 break;
221131
221132 case 'u':
221133 if( nBuf>3 && 0==memcmp("ous", &aBuf[nBuf-3], 3) ){
221134 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
221135 *pnBuf = nBuf - 3;
221136 }
221137 }
221138 break;
221139
221140 case 'v':
221141 if( nBuf>3 && 0==memcmp("ive", &aBuf[nBuf-3], 3) ){
221142 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
221143 *pnBuf = nBuf - 3;
221144 }
221145 }
221146 break;
221147
221148 case 'z':
221149 if( nBuf>3 && 0==memcmp("ize", &aBuf[nBuf-3], 3) ){
221150 if( fts5Porter_MGt1(aBuf, nBuf-3) ){
221151 *pnBuf = nBuf - 3;
221152 }
221153 }
221154 break;
221155
221156 }
221157 return ret;
221158}
221159
221160
221161static int fts5PorterStep1B2(char *aBuf, int *pnBuf){
221162 int ret = 0;
221163 int nBuf = *pnBuf;
221164 switch( aBuf[nBuf-2] ){
221165
221166 case 'a':
221167 if( nBuf>2 && 0==memcmp("at", &aBuf[nBuf-2], 2) ){
221168 memcpy(&aBuf[nBuf-2], "ate", 3);
221169 *pnBuf = nBuf - 2 + 3;
221170 ret = 1;
221171 }
221172 break;
221173
221174 case 'b':
221175 if( nBuf>2 && 0==memcmp("bl", &aBuf[nBuf-2], 2) ){
221176 memcpy(&aBuf[nBuf-2], "ble", 3);
221177 *pnBuf = nBuf - 2 + 3;
221178 ret = 1;
221179 }
221180 break;
221181
221182 case 'i':
221183 if( nBuf>2 && 0==memcmp("iz", &aBuf[nBuf-2], 2) ){
221184 memcpy(&aBuf[nBuf-2], "ize", 3);
221185 *pnBuf = nBuf - 2 + 3;
221186 ret = 1;
221187 }
221188 break;
221189
221190 }
221191 return ret;
221192}
221193
221194
221195static int fts5PorterStep2(char *aBuf, int *pnBuf){
221196 int ret = 0;
221197 int nBuf = *pnBuf;
221198 switch( aBuf[nBuf-2] ){
221199
221200 case 'a':
221201 if( nBuf>7 && 0==memcmp("ational", &aBuf[nBuf-7], 7) ){
221202 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
221203 memcpy(&aBuf[nBuf-7], "ate", 3);
221204 *pnBuf = nBuf - 7 + 3;
221205 }
221206 }else if( nBuf>6 && 0==memcmp("tional", &aBuf[nBuf-6], 6) ){
221207 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
221208 memcpy(&aBuf[nBuf-6], "tion", 4);
221209 *pnBuf = nBuf - 6 + 4;
221210 }
221211 }
221212 break;
221213
221214 case 'c':
221215 if( nBuf>4 && 0==memcmp("enci", &aBuf[nBuf-4], 4) ){
221216 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
221217 memcpy(&aBuf[nBuf-4], "ence", 4);
221218 *pnBuf = nBuf - 4 + 4;
221219 }
221220 }else if( nBuf>4 && 0==memcmp("anci", &aBuf[nBuf-4], 4) ){
221221 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
221222 memcpy(&aBuf[nBuf-4], "ance", 4);
221223 *pnBuf = nBuf - 4 + 4;
221224 }
221225 }
221226 break;
221227
221228 case 'e':
221229 if( nBuf>4 && 0==memcmp("izer", &aBuf[nBuf-4], 4) ){
221230 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
221231 memcpy(&aBuf[nBuf-4], "ize", 3);
221232 *pnBuf = nBuf - 4 + 3;
221233 }
221234 }
221235 break;
221236
221237 case 'g':
221238 if( nBuf>4 && 0==memcmp("logi", &aBuf[nBuf-4], 4) ){
221239 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
221240 memcpy(&aBuf[nBuf-4], "log", 3);
221241 *pnBuf = nBuf - 4 + 3;
221242 }
221243 }
221244 break;
221245
221246 case 'l':
221247 if( nBuf>3 && 0==memcmp("bli", &aBuf[nBuf-3], 3) ){
221248 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
221249 memcpy(&aBuf[nBuf-3], "ble", 3);
221250 *pnBuf = nBuf - 3 + 3;
221251 }
221252 }else if( nBuf>4 && 0==memcmp("alli", &aBuf[nBuf-4], 4) ){
221253 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
221254 memcpy(&aBuf[nBuf-4], "al", 2);
221255 *pnBuf = nBuf - 4 + 2;
221256 }
221257 }else if( nBuf>5 && 0==memcmp("entli", &aBuf[nBuf-5], 5) ){
221258 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221259 memcpy(&aBuf[nBuf-5], "ent", 3);
221260 *pnBuf = nBuf - 5 + 3;
221261 }
221262 }else if( nBuf>3 && 0==memcmp("eli", &aBuf[nBuf-3], 3) ){
221263 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
221264 memcpy(&aBuf[nBuf-3], "e", 1);
221265 *pnBuf = nBuf - 3 + 1;
221266 }
221267 }else if( nBuf>5 && 0==memcmp("ousli", &aBuf[nBuf-5], 5) ){
221268 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221269 memcpy(&aBuf[nBuf-5], "ous", 3);
221270 *pnBuf = nBuf - 5 + 3;
221271 }
221272 }
221273 break;
221274
221275 case 'o':
221276 if( nBuf>7 && 0==memcmp("ization", &aBuf[nBuf-7], 7) ){
221277 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
221278 memcpy(&aBuf[nBuf-7], "ize", 3);
221279 *pnBuf = nBuf - 7 + 3;
221280 }
221281 }else if( nBuf>5 && 0==memcmp("ation", &aBuf[nBuf-5], 5) ){
221282 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221283 memcpy(&aBuf[nBuf-5], "ate", 3);
221284 *pnBuf = nBuf - 5 + 3;
221285 }
221286 }else if( nBuf>4 && 0==memcmp("ator", &aBuf[nBuf-4], 4) ){
221287 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
221288 memcpy(&aBuf[nBuf-4], "ate", 3);
221289 *pnBuf = nBuf - 4 + 3;
221290 }
221291 }
221292 break;
221293
221294 case 's':
221295 if( nBuf>5 && 0==memcmp("alism", &aBuf[nBuf-5], 5) ){
221296 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221297 memcpy(&aBuf[nBuf-5], "al", 2);
221298 *pnBuf = nBuf - 5 + 2;
221299 }
221300 }else if( nBuf>7 && 0==memcmp("iveness", &aBuf[nBuf-7], 7) ){
221301 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
221302 memcpy(&aBuf[nBuf-7], "ive", 3);
221303 *pnBuf = nBuf - 7 + 3;
221304 }
221305 }else if( nBuf>7 && 0==memcmp("fulness", &aBuf[nBuf-7], 7) ){
221306 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
221307 memcpy(&aBuf[nBuf-7], "ful", 3);
221308 *pnBuf = nBuf - 7 + 3;
221309 }
221310 }else if( nBuf>7 && 0==memcmp("ousness", &aBuf[nBuf-7], 7) ){
221311 if( fts5Porter_MGt0(aBuf, nBuf-7) ){
221312 memcpy(&aBuf[nBuf-7], "ous", 3);
221313 *pnBuf = nBuf - 7 + 3;
221314 }
221315 }
221316 break;
221317
221318 case 't':
221319 if( nBuf>5 && 0==memcmp("aliti", &aBuf[nBuf-5], 5) ){
221320 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221321 memcpy(&aBuf[nBuf-5], "al", 2);
221322 *pnBuf = nBuf - 5 + 2;
221323 }
221324 }else if( nBuf>5 && 0==memcmp("iviti", &aBuf[nBuf-5], 5) ){
221325 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221326 memcpy(&aBuf[nBuf-5], "ive", 3);
221327 *pnBuf = nBuf - 5 + 3;
221328 }
221329 }else if( nBuf>6 && 0==memcmp("biliti", &aBuf[nBuf-6], 6) ){
221330 if( fts5Porter_MGt0(aBuf, nBuf-6) ){
221331 memcpy(&aBuf[nBuf-6], "ble", 3);
221332 *pnBuf = nBuf - 6 + 3;
221333 }
221334 }
221335 break;
221336
221337 }
221338 return ret;
221339}
221340
221341
221342static int fts5PorterStep3(char *aBuf, int *pnBuf){
221343 int ret = 0;
221344 int nBuf = *pnBuf;
221345 switch( aBuf[nBuf-2] ){
221346
221347 case 'a':
221348 if( nBuf>4 && 0==memcmp("ical", &aBuf[nBuf-4], 4) ){
221349 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
221350 memcpy(&aBuf[nBuf-4], "ic", 2);
221351 *pnBuf = nBuf - 4 + 2;
221352 }
221353 }
221354 break;
221355
221356 case 's':
221357 if( nBuf>4 && 0==memcmp("ness", &aBuf[nBuf-4], 4) ){
221358 if( fts5Porter_MGt0(aBuf, nBuf-4) ){
221359 *pnBuf = nBuf - 4;
221360 }
221361 }
221362 break;
221363
221364 case 't':
221365 if( nBuf>5 && 0==memcmp("icate", &aBuf[nBuf-5], 5) ){
221366 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221367 memcpy(&aBuf[nBuf-5], "ic", 2);
221368 *pnBuf = nBuf - 5 + 2;
221369 }
221370 }else if( nBuf>5 && 0==memcmp("iciti", &aBuf[nBuf-5], 5) ){
221371 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221372 memcpy(&aBuf[nBuf-5], "ic", 2);
221373 *pnBuf = nBuf - 5 + 2;
221374 }
221375 }
221376 break;
221377
221378 case 'u':
221379 if( nBuf>3 && 0==memcmp("ful", &aBuf[nBuf-3], 3) ){
221380 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
221381 *pnBuf = nBuf - 3;
221382 }
221383 }
221384 break;
221385
221386 case 'v':
221387 if( nBuf>5 && 0==memcmp("ative", &aBuf[nBuf-5], 5) ){
221388 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221389 *pnBuf = nBuf - 5;
221390 }
221391 }
221392 break;
221393
221394 case 'z':
221395 if( nBuf>5 && 0==memcmp("alize", &aBuf[nBuf-5], 5) ){
221396 if( fts5Porter_MGt0(aBuf, nBuf-5) ){
221397 memcpy(&aBuf[nBuf-5], "al", 2);
221398 *pnBuf = nBuf - 5 + 2;
221399 }
221400 }
221401 break;
221402
221403 }
221404 return ret;
221405}
221406
221407
221408static int fts5PorterStep1B(char *aBuf, int *pnBuf){
221409 int ret = 0;
221410 int nBuf = *pnBuf;
221411 switch( aBuf[nBuf-2] ){
221412
221413 case 'e':
221414 if( nBuf>3 && 0==memcmp("eed", &aBuf[nBuf-3], 3) ){
221415 if( fts5Porter_MGt0(aBuf, nBuf-3) ){
221416 memcpy(&aBuf[nBuf-3], "ee", 2);
221417 *pnBuf = nBuf - 3 + 2;
221418 }
221419 }else if( nBuf>2 && 0==memcmp("ed", &aBuf[nBuf-2], 2) ){
221420 if( fts5Porter_Vowel(aBuf, nBuf-2) ){
221421 *pnBuf = nBuf - 2;
221422 ret = 1;
221423 }
221424 }
221425 break;
221426
221427 case 'n':
221428 if( nBuf>3 && 0==memcmp("ing", &aBuf[nBuf-3], 3) ){
221429 if( fts5Porter_Vowel(aBuf, nBuf-3) ){
221430 *pnBuf = nBuf - 3;
221431 ret = 1;
221432 }
221433 }
221434 break;
221435
221436 }
221437 return ret;
221438}
221439
221440/*
221441** GENERATED CODE ENDS HERE (mkportersteps.tcl)
221442***************************************************************************
221443**************************************************************************/
221444
221445static void fts5PorterStep1A(char *aBuf, int *pnBuf){
221446 int nBuf = *pnBuf;
221447 if( aBuf[nBuf-1]=='s' ){
221448 if( aBuf[nBuf-2]=='e' ){
221449 if( (nBuf>4 && aBuf[nBuf-4]=='s' && aBuf[nBuf-3]=='s')
221450 || (nBuf>3 && aBuf[nBuf-3]=='i' )
221451 ){
221452 *pnBuf = nBuf-2;
221453 }else{
221454 *pnBuf = nBuf-1;
221455 }
221456 }
221457 else if( aBuf[nBuf-2]!='s' ){
221458 *pnBuf = nBuf-1;
221459 }
221460 }
221461}
221462
221463static int fts5PorterCb(
221464 void *pCtx,
221465 int tflags,
221466 const char *pToken,
221467 int nToken,
221468 int iStart,
221469 int iEnd
221470){
221471 PorterContext *p = (PorterContext*)pCtx;
221472
221473 char *aBuf;
221474 int nBuf;
221475
221476 if( nToken>FTS5_PORTER_MAX_TOKEN || nToken<3 ) goto pass_through;
221477 aBuf = p->aBuf;
221478 nBuf = nToken;
221479 memcpy(aBuf, pToken, nBuf);
221480
221481 /* Step 1. */
221482 fts5PorterStep1A(aBuf, &nBuf);
221483 if( fts5PorterStep1B(aBuf, &nBuf) ){
221484 if( fts5PorterStep1B2(aBuf, &nBuf)==0 ){
221485 char c = aBuf[nBuf-1];
221486 if( fts5PorterIsVowel(c, 0)==0
221487 && c!='l' && c!='s' && c!='z' && c==aBuf[nBuf-2]
221488 ){
221489 nBuf--;
221490 }else if( fts5Porter_MEq1(aBuf, nBuf) && fts5Porter_Ostar(aBuf, nBuf) ){
221491 aBuf[nBuf++] = 'e';
221492 }
221493 }
221494 }
221495
221496 /* Step 1C. */
221497 if( aBuf[nBuf-1]=='y' && fts5Porter_Vowel(aBuf, nBuf-1) ){
221498 aBuf[nBuf-1] = 'i';
221499 }
221500
221501 /* Steps 2 through 4. */
221502 fts5PorterStep2(aBuf, &nBuf);
221503 fts5PorterStep3(aBuf, &nBuf);
221504 fts5PorterStep4(aBuf, &nBuf);
221505
221506 /* Step 5a. */
221507 assert( nBuf>0 )((void) (0));
221508 if( aBuf[nBuf-1]=='e' ){
221509 if( fts5Porter_MGt1(aBuf, nBuf-1)
221510 || (fts5Porter_MEq1(aBuf, nBuf-1) && !fts5Porter_Ostar(aBuf, nBuf-1))
221511 ){
221512 nBuf--;
221513 }
221514 }
221515
221516 /* Step 5b. */
221517 if( nBuf>1 && aBuf[nBuf-1]=='l'
221518 && aBuf[nBuf-2]=='l' && fts5Porter_MGt1(aBuf, nBuf-1)
221519 ){
221520 nBuf--;
221521 }
221522
221523 return p->xToken(p->pCtx, tflags, aBuf, nBuf, iStart, iEnd);
221524
221525 pass_through:
221526 return p->xToken(p->pCtx, tflags, pToken, nToken, iStart, iEnd);
221527}
221528
221529/*
221530** Tokenize using the porter tokenizer.
221531*/
221532static int fts5PorterTokenize(
221533 Fts5Tokenizer *pTokenizer,
221534 void *pCtx,
221535 int flags,
221536 const char *pText, int nText,
221537 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
221538){
221539 PorterTokenizer *p = (PorterTokenizer*)pTokenizer;
221540 PorterContext sCtx;
221541 sCtx.xToken = xToken;
221542 sCtx.pCtx = pCtx;
221543 sCtx.aBuf = p->aBuf;
221544 return p->tokenizer.xTokenize(
221545 p->pTokenizer, (void*)&sCtx, flags, pText, nText, fts5PorterCb
221546 );
221547}
221548
221549/*
221550** Register all built-in tokenizers with FTS5.
221551*/
221552static int sqlite3Fts5TokenizerInit(fts5_api *pApi){
221553 struct BuiltinTokenizer {
221554 const char *zName;
221555 fts5_tokenizer x;
221556 } aBuiltin[] = {
221557 { "unicode61", {fts5UnicodeCreate, fts5UnicodeDelete, fts5UnicodeTokenize}},
221558 { "ascii", {fts5AsciiCreate, fts5AsciiDelete, fts5AsciiTokenize }},
221559 { "porter", {fts5PorterCreate, fts5PorterDelete, fts5PorterTokenize }},
221560 };
221561
221562 int rc = SQLITE_OK0; /* Return code */
221563 int i; /* To iterate through builtin functions */
221564
221565 for(i=0; rc==SQLITE_OK0 && i<ArraySize(aBuiltin)((int)(sizeof(aBuiltin)/sizeof(aBuiltin[0]))); i++){
221566 rc = pApi->xCreateTokenizer(pApi,
221567 aBuiltin[i].zName,
221568 (void*)pApi,
221569 &aBuiltin[i].x,
221570 0
221571 );
221572 }
221573
221574 return rc;
221575}
221576
221577/*
221578** 2012-05-25
221579**
221580** The author disclaims copyright to this source code. In place of
221581** a legal notice, here is a blessing:
221582**
221583** May you do good and not evil.
221584** May you find forgiveness for yourself and forgive others.
221585** May you share freely, never taking more than you give.
221586**
221587******************************************************************************
221588*/
221589
221590/*
221591** DO NOT EDIT THIS MACHINE GENERATED FILE.
221592*/
221593
221594
221595/* #include <assert.h> */
221596
221597
221598
221599/*
221600** If the argument is a codepoint corresponding to a lowercase letter
221601** in the ASCII range with a diacritic added, return the codepoint
221602** of the ASCII letter only. For example, if passed 235 - "LATIN
221603** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
221604** E"). The resuls of passing a codepoint that corresponds to an
221605** uppercase letter are undefined.
221606*/
221607static int fts5_remove_diacritic(int c, int bComplex){
221608 unsigned short aDia[] = {
221609 0, 1797, 1848, 1859, 1891, 1928, 1940, 1995,
221610 2024, 2040, 2060, 2110, 2168, 2206, 2264, 2286,
221611 2344, 2383, 2472, 2488, 2516, 2596, 2668, 2732,
221612 2782, 2842, 2894, 2954, 2984, 3000, 3028, 3336,
221613 3456, 3696, 3712, 3728, 3744, 3766, 3832, 3896,
221614 3912, 3928, 3944, 3968, 4008, 4040, 4056, 4106,
221615 4138, 4170, 4202, 4234, 4266, 4296, 4312, 4344,
221616 4408, 4424, 4442, 4472, 4488, 4504, 6148, 6198,
221617 6264, 6280, 6360, 6429, 6505, 6529, 61448, 61468,
221618 61512, 61534, 61592, 61610, 61642, 61672, 61688, 61704,
221619 61726, 61784, 61800, 61816, 61836, 61880, 61896, 61914,
221620 61948, 61998, 62062, 62122, 62154, 62184, 62200, 62218,
221621 62252, 62302, 62364, 62410, 62442, 62478, 62536, 62554,
221622 62584, 62604, 62640, 62648, 62656, 62664, 62730, 62766,
221623 62830, 62890, 62924, 62974, 63032, 63050, 63082, 63118,
221624 63182, 63242, 63274, 63310, 63368, 63390,
221625 };
221626#define HIBIT ((unsigned char)0x80)
221627 unsigned char aChar[] = {
221628 '\0', 'a', 'c', 'e', 'i', 'n',
221629 'o', 'u', 'y', 'y', 'a', 'c',
221630 'd', 'e', 'e', 'g', 'h', 'i',
221631 'j', 'k', 'l', 'n', 'o', 'r',
221632 's', 't', 'u', 'u', 'w', 'y',
221633 'z', 'o', 'u', 'a', 'i', 'o',
221634 'u', 'u'|HIBIT, 'a'|HIBIT, 'g', 'k', 'o',
221635 'o'|HIBIT, 'j', 'g', 'n', 'a'|HIBIT, 'a',
221636 'e', 'i', 'o', 'r', 'u', 's',
221637 't', 'h', 'a', 'e', 'o'|HIBIT, 'o',
221638 'o'|HIBIT, 'y', '\0', '\0', '\0', '\0',
221639 '\0', '\0', '\0', '\0', 'a', 'b',
221640 'c'|HIBIT, 'd', 'd', 'e'|HIBIT, 'e', 'e'|HIBIT,
221641 'f', 'g', 'h', 'h', 'i', 'i'|HIBIT,
221642 'k', 'l', 'l'|HIBIT, 'l', 'm', 'n',
221643 'o'|HIBIT, 'p', 'r', 'r'|HIBIT, 'r', 's',
221644 's'|HIBIT, 't', 'u', 'u'|HIBIT, 'v', 'w',
221645 'w', 'x', 'y', 'z', 'h', 't',
221646 'w', 'y', 'a', 'a'|HIBIT, 'a'|HIBIT, 'a'|HIBIT,
221647 'e', 'e'|HIBIT, 'e'|HIBIT, 'i', 'o', 'o'|HIBIT,
221648 'o'|HIBIT, 'o'|HIBIT, 'u', 'u'|HIBIT, 'u'|HIBIT, 'y',
221649 };
221650
221651 unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
221652 int iRes = 0;
221653 int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
221654 int iLo = 0;
221655 while( iHi>=iLo ){
221656 int iTest = (iHi + iLo) / 2;
221657 if( key >= aDia[iTest] ){
221658 iRes = iTest;
221659 iLo = iTest+1;
221660 }else{
221661 iHi = iTest-1;
221662 }
221663 }
221664 assert( key>=aDia[iRes] )((void) (0));
221665 if( bComplex==0 && (aChar[iRes] & 0x80) ) return c;
221666 return (c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : ((int)aChar[iRes] & 0x7F);
221667}
221668
221669
221670/*
221671** Return true if the argument interpreted as a unicode codepoint
221672** is a diacritical modifier character.
221673*/
221674static int sqlite3Fts5UnicodeIsdiacritic(int c){
221675 unsigned int mask0 = 0x08029FDF;
221676 unsigned int mask1 = 0x000361F8;
221677 if( c<768 || c>817 ) return 0;
221678 return (c < 768+32) ?
221679 (mask0 & ((unsigned int)1 << (c-768))) :
221680 (mask1 & ((unsigned int)1 << (c-768-32)));
221681}
221682
221683
221684/*
221685** Interpret the argument as a unicode codepoint. If the codepoint
221686** is an upper case character that has a lower case equivalent,
221687** return the codepoint corresponding to the lower case version.
221688** Otherwise, return a copy of the argument.
221689**
221690** The results are undefined if the value passed to this function
221691** is less than zero.
221692*/
221693static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){
221694 /* Each entry in the following array defines a rule for folding a range
221695 ** of codepoints to lower case. The rule applies to a range of nRange
221696 ** codepoints starting at codepoint iCode.
221697 **
221698 ** If the least significant bit in flags is clear, then the rule applies
221699 ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
221700 ** need to be folded). Or, if it is set, then the rule only applies to
221701 ** every second codepoint in the range, starting with codepoint C.
221702 **
221703 ** The 7 most significant bits in flags are an index into the aiOff[]
221704 ** array. If a specific codepoint C does require folding, then its lower
221705 ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
221706 **
221707 ** The contents of this array are generated by parsing the CaseFolding.txt
221708 ** file distributed as part of the "Unicode Character Database". See
221709 ** http://www.unicode.org for details.
221710 */
221711 static const struct TableEntry {
221712 unsigned short iCode;
221713 unsigned char flags;
221714 unsigned char nRange;
221715 } aEntry[] = {
221716 {65, 14, 26}, {181, 64, 1}, {192, 14, 23},
221717 {216, 14, 7}, {256, 1, 48}, {306, 1, 6},
221718 {313, 1, 16}, {330, 1, 46}, {376, 116, 1},
221719 {377, 1, 6}, {383, 104, 1}, {385, 50, 1},
221720 {386, 1, 4}, {390, 44, 1}, {391, 0, 1},
221721 {393, 42, 2}, {395, 0, 1}, {398, 32, 1},
221722 {399, 38, 1}, {400, 40, 1}, {401, 0, 1},
221723 {403, 42, 1}, {404, 46, 1}, {406, 52, 1},
221724 {407, 48, 1}, {408, 0, 1}, {412, 52, 1},
221725 {413, 54, 1}, {415, 56, 1}, {416, 1, 6},
221726 {422, 60, 1}, {423, 0, 1}, {425, 60, 1},
221727 {428, 0, 1}, {430, 60, 1}, {431, 0, 1},
221728 {433, 58, 2}, {435, 1, 4}, {439, 62, 1},
221729 {440, 0, 1}, {444, 0, 1}, {452, 2, 1},
221730 {453, 0, 1}, {455, 2, 1}, {456, 0, 1},
221731 {458, 2, 1}, {459, 1, 18}, {478, 1, 18},
221732 {497, 2, 1}, {498, 1, 4}, {502, 122, 1},
221733 {503, 134, 1}, {504, 1, 40}, {544, 110, 1},
221734 {546, 1, 18}, {570, 70, 1}, {571, 0, 1},
221735 {573, 108, 1}, {574, 68, 1}, {577, 0, 1},
221736 {579, 106, 1}, {580, 28, 1}, {581, 30, 1},
221737 {582, 1, 10}, {837, 36, 1}, {880, 1, 4},
221738 {886, 0, 1}, {902, 18, 1}, {904, 16, 3},
221739 {908, 26, 1}, {910, 24, 2}, {913, 14, 17},
221740 {931, 14, 9}, {962, 0, 1}, {975, 4, 1},
221741 {976, 140, 1}, {977, 142, 1}, {981, 146, 1},
221742 {982, 144, 1}, {984, 1, 24}, {1008, 136, 1},
221743 {1009, 138, 1}, {1012, 130, 1}, {1013, 128, 1},
221744 {1015, 0, 1}, {1017, 152, 1}, {1018, 0, 1},
221745 {1021, 110, 3}, {1024, 34, 16}, {1040, 14, 32},
221746 {1120, 1, 34}, {1162, 1, 54}, {1216, 6, 1},
221747 {1217, 1, 14}, {1232, 1, 88}, {1329, 22, 38},
221748 {4256, 66, 38}, {4295, 66, 1}, {4301, 66, 1},
221749 {7680, 1, 150}, {7835, 132, 1}, {7838, 96, 1},
221750 {7840, 1, 96}, {7944, 150, 8}, {7960, 150, 6},
221751 {7976, 150, 8}, {7992, 150, 8}, {8008, 150, 6},
221752 {8025, 151, 8}, {8040, 150, 8}, {8072, 150, 8},
221753 {8088, 150, 8}, {8104, 150, 8}, {8120, 150, 2},
221754 {8122, 126, 2}, {8124, 148, 1}, {8126, 100, 1},
221755 {8136, 124, 4}, {8140, 148, 1}, {8152, 150, 2},
221756 {8154, 120, 2}, {8168, 150, 2}, {8170, 118, 2},
221757 {8172, 152, 1}, {8184, 112, 2}, {8186, 114, 2},
221758 {8188, 148, 1}, {8486, 98, 1}, {8490, 92, 1},
221759 {8491, 94, 1}, {8498, 12, 1}, {8544, 8, 16},
221760 {8579, 0, 1}, {9398, 10, 26}, {11264, 22, 47},
221761 {11360, 0, 1}, {11362, 88, 1}, {11363, 102, 1},
221762 {11364, 90, 1}, {11367, 1, 6}, {11373, 84, 1},
221763 {11374, 86, 1}, {11375, 80, 1}, {11376, 82, 1},
221764 {11378, 0, 1}, {11381, 0, 1}, {11390, 78, 2},
221765 {11392, 1, 100}, {11499, 1, 4}, {11506, 0, 1},
221766 {42560, 1, 46}, {42624, 1, 24}, {42786, 1, 14},
221767 {42802, 1, 62}, {42873, 1, 4}, {42877, 76, 1},
221768 {42878, 1, 10}, {42891, 0, 1}, {42893, 74, 1},
221769 {42896, 1, 4}, {42912, 1, 10}, {42922, 72, 1},
221770 {65313, 14, 26},
221771 };
221772 static const unsigned short aiOff[] = {
221773 1, 2, 8, 15, 16, 26, 28, 32,
221774 37, 38, 40, 48, 63, 64, 69, 71,
221775 79, 80, 116, 202, 203, 205, 206, 207,
221776 209, 210, 211, 213, 214, 217, 218, 219,
221777 775, 7264, 10792, 10795, 23228, 23256, 30204, 54721,
221778 54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
221779 57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
221780 65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
221781 65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
221782 65514, 65521, 65527, 65528, 65529,
221783 };
221784
221785 int ret = c;
221786
221787 assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 )((void) (0));
221788
221789 if( c<128 ){
221790 if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
221791 }else if( c<65536 ){
221792 const struct TableEntry *p;
221793 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
221794 int iLo = 0;
221795 int iRes = -1;
221796
221797 assert( c>aEntry[0].iCode )((void) (0));
221798 while( iHi>=iLo ){
221799 int iTest = (iHi + iLo) / 2;
221800 int cmp = (c - aEntry[iTest].iCode);
221801 if( cmp>=0 ){
221802 iRes = iTest;
221803 iLo = iTest+1;
221804 }else{
221805 iHi = iTest-1;
221806 }
221807 }
221808
221809 assert( iRes>=0 && c>=aEntry[iRes].iCode )((void) (0));
221810 p = &aEntry[iRes];
221811 if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
221812 ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
221813 assert( ret>0 )((void) (0));
221814 }
221815
221816 if( eRemoveDiacritic ){
221817 ret = fts5_remove_diacritic(ret, eRemoveDiacritic==2);
221818 }
221819 }
221820
221821 else if( c>=66560 && c<66600 ){
221822 ret = c + 40;
221823 }
221824
221825 return ret;
221826}
221827
221828
221829static int sqlite3Fts5UnicodeCatParse(const char *zCat, u8 *aArray){
221830 aArray[0] = 1;
221831 switch( zCat[0] ){
221832 case 'C':
221833 switch( zCat[1] ){
221834 case 'c': aArray[1] = 1; break;
221835 case 'f': aArray[2] = 1; break;
221836 case 'n': aArray[3] = 1; break;
221837 case 's': aArray[4] = 1; break;
221838 case 'o': aArray[31] = 1; break;
221839 case '*':
221840 aArray[1] = 1;
221841 aArray[2] = 1;
221842 aArray[3] = 1;
221843 aArray[4] = 1;
221844 aArray[31] = 1;
221845 break;
221846 default: return 1; }
221847 break;
221848
221849 case 'L':
221850 switch( zCat[1] ){
221851 case 'l': aArray[5] = 1; break;
221852 case 'm': aArray[6] = 1; break;
221853 case 'o': aArray[7] = 1; break;
221854 case 't': aArray[8] = 1; break;
221855 case 'u': aArray[9] = 1; break;
221856 case 'C': aArray[30] = 1; break;
221857 case '*':
221858 aArray[5] = 1;
221859 aArray[6] = 1;
221860 aArray[7] = 1;
221861 aArray[8] = 1;
221862 aArray[9] = 1;
221863 aArray[30] = 1;
221864 break;
221865 default: return 1; }
221866 break;
221867
221868 case 'M':
221869 switch( zCat[1] ){
221870 case 'c': aArray[10] = 1; break;
221871 case 'e': aArray[11] = 1; break;
221872 case 'n': aArray[12] = 1; break;
221873 case '*':
221874 aArray[10] = 1;
221875 aArray[11] = 1;
221876 aArray[12] = 1;
221877 break;
221878 default: return 1; }
221879 break;
221880
221881 case 'N':
221882 switch( zCat[1] ){
221883 case 'd': aArray[13] = 1; break;
221884 case 'l': aArray[14] = 1; break;
221885 case 'o': aArray[15] = 1; break;
221886 case '*':
221887 aArray[13] = 1;
221888 aArray[14] = 1;
221889 aArray[15] = 1;
221890 break;
221891 default: return 1; }
221892 break;
221893
221894 case 'P':
221895 switch( zCat[1] ){
221896 case 'c': aArray[16] = 1; break;
221897 case 'd': aArray[17] = 1; break;
221898 case 'e': aArray[18] = 1; break;
221899 case 'f': aArray[19] = 1; break;
221900 case 'i': aArray[20] = 1; break;
221901 case 'o': aArray[21] = 1; break;
221902 case 's': aArray[22] = 1; break;
221903 case '*':
221904 aArray[16] = 1;
221905 aArray[17] = 1;
221906 aArray[18] = 1;
221907 aArray[19] = 1;
221908 aArray[20] = 1;
221909 aArray[21] = 1;
221910 aArray[22] = 1;
221911 break;
221912 default: return 1; }
221913 break;
221914
221915 case 'S':
221916 switch( zCat[1] ){
221917 case 'c': aArray[23] = 1; break;
221918 case 'k': aArray[24] = 1; break;
221919 case 'm': aArray[25] = 1; break;
221920 case 'o': aArray[26] = 1; break;
221921 case '*':
221922 aArray[23] = 1;
221923 aArray[24] = 1;
221924 aArray[25] = 1;
221925 aArray[26] = 1;
221926 break;
221927 default: return 1; }
221928 break;
221929
221930 case 'Z':
221931 switch( zCat[1] ){
221932 case 'l': aArray[27] = 1; break;
221933 case 'p': aArray[28] = 1; break;
221934 case 's': aArray[29] = 1; break;
221935 case '*':
221936 aArray[27] = 1;
221937 aArray[28] = 1;
221938 aArray[29] = 1;
221939 break;
221940 default: return 1; }
221941 break;
221942
221943 }
221944 return 0;
221945}
221946
221947static u16 aFts5UnicodeBlock[] = {
221948 0, 1471, 1753, 1760, 1760, 1760, 1760, 1760, 1760, 1760,
221949 1760, 1760, 1760, 1760, 1760, 1763, 1765,
221950 };
221951static u16 aFts5UnicodeMap[] = {
221952 0, 32, 33, 36, 37, 40, 41, 42, 43, 44,
221953 45, 46, 48, 58, 60, 63, 65, 91, 92, 93,
221954 94, 95, 96, 97, 123, 124, 125, 126, 127, 160,
221955 161, 162, 166, 167, 168, 169, 170, 171, 172, 173,
221956 174, 175, 176, 177, 178, 180, 181, 182, 184, 185,
221957 186, 187, 188, 191, 192, 215, 216, 223, 247, 248,
221958 256, 312, 313, 329, 330, 377, 383, 385, 387, 388,
221959 391, 394, 396, 398, 402, 403, 405, 406, 409, 412,
221960 414, 415, 417, 418, 423, 427, 428, 431, 434, 436,
221961 437, 440, 442, 443, 444, 446, 448, 452, 453, 454,
221962 455, 456, 457, 458, 459, 460, 461, 477, 478, 496,
221963 497, 498, 499, 500, 503, 505, 506, 564, 570, 572,
221964 573, 575, 577, 580, 583, 584, 592, 660, 661, 688,
221965 706, 710, 722, 736, 741, 748, 749, 750, 751, 768,
221966 880, 884, 885, 886, 890, 891, 894, 900, 902, 903,
221967 904, 908, 910, 912, 913, 931, 940, 975, 977, 978,
221968 981, 984, 1008, 1012, 1014, 1015, 1018, 1020, 1021, 1072,
221969 1120, 1154, 1155, 1160, 1162, 1217, 1231, 1232, 1329, 1369,
221970 1370, 1377, 1417, 1418, 1423, 1425, 1470, 1471, 1472, 1473,
221971 1475, 1476, 1478, 1479, 1488, 1520, 1523, 1536, 1542, 1545,
221972 1547, 1548, 1550, 1552, 1563, 1566, 1568, 1600, 1601, 1611,
221973 1632, 1642, 1646, 1648, 1649, 1748, 1749, 1750, 1757, 1758,
221974 1759, 1765, 1767, 1769, 1770, 1774, 1776, 1786, 1789, 1791,
221975 1792, 1807, 1808, 1809, 1810, 1840, 1869, 1958, 1969, 1984,
221976 1994, 2027, 2036, 2038, 2039, 2042, 2048, 2070, 2074, 2075,
221977 2084, 2085, 2088, 2089, 2096, 2112, 2137, 2142, 2208, 2210,
221978 2276, 2304, 2307, 2308, 2362, 2363, 2364, 2365, 2366, 2369,
221979 2377, 2381, 2382, 2384, 2385, 2392, 2402, 2404, 2406, 2416,
221980 2417, 2418, 2425, 2433, 2434, 2437, 2447, 2451, 2474, 2482,
221981 2486, 2492, 2493, 2494, 2497, 2503, 2507, 2509, 2510, 2519,
221982 2524, 2527, 2530, 2534, 2544, 2546, 2548, 2554, 2555, 2561,
221983 2563, 2565, 2575, 2579, 2602, 2610, 2613, 2616, 2620, 2622,
221984 2625, 2631, 2635, 2641, 2649, 2654, 2662, 2672, 2674, 2677,
221985 2689, 2691, 2693, 2703, 2707, 2730, 2738, 2741, 2748, 2749,
221986 2750, 2753, 2759, 2761, 2763, 2765, 2768, 2784, 2786, 2790,
221987 2800, 2801, 2817, 2818, 2821, 2831, 2835, 2858, 2866, 2869,
221988 2876, 2877, 2878, 2879, 2880, 2881, 2887, 2891, 2893, 2902,
221989 2903, 2908, 2911, 2914, 2918, 2928, 2929, 2930, 2946, 2947,
221990 2949, 2958, 2962, 2969, 2972, 2974, 2979, 2984, 2990, 3006,
221991 3008, 3009, 3014, 3018, 3021, 3024, 3031, 3046, 3056, 3059,
221992 3065, 3066, 3073, 3077, 3086, 3090, 3114, 3125, 3133, 3134,
221993 3137, 3142, 3146, 3157, 3160, 3168, 3170, 3174, 3192, 3199,
221994 3202, 3205, 3214, 3218, 3242, 3253, 3260, 3261, 3262, 3263,
221995 3264, 3270, 3271, 3274, 3276, 3285, 3294, 3296, 3298, 3302,
221996 3313, 3330, 3333, 3342, 3346, 3389, 3390, 3393, 3398, 3402,
221997 3405, 3406, 3415, 3424, 3426, 3430, 3440, 3449, 3450, 3458,
221998 3461, 3482, 3507, 3517, 3520, 3530, 3535, 3538, 3542, 3544,
221999 3570, 3572, 3585, 3633, 3634, 3636, 3647, 3648, 3654, 3655,
222000 3663, 3664, 3674, 3713, 3716, 3719, 3722, 3725, 3732, 3737,
222001 3745, 3749, 3751, 3754, 3757, 3761, 3762, 3764, 3771, 3773,
222002 3776, 3782, 3784, 3792, 3804, 3840, 3841, 3844, 3859, 3860,
222003 3861, 3864, 3866, 3872, 3882, 3892, 3893, 3894, 3895, 3896,
222004 3897, 3898, 3899, 3900, 3901, 3902, 3904, 3913, 3953, 3967,
222005 3968, 3973, 3974, 3976, 3981, 3993, 4030, 4038, 4039, 4046,
222006 4048, 4053, 4057, 4096, 4139, 4141, 4145, 4146, 4152, 4153,
222007 4155, 4157, 4159, 4160, 4170, 4176, 4182, 4184, 4186, 4190,
222008 4193, 4194, 4197, 4199, 4206, 4209, 4213, 4226, 4227, 4229,
222009 4231, 4237, 4238, 4239, 4240, 4250, 4253, 4254, 4256, 4295,
222010 4301, 4304, 4347, 4348, 4349, 4682, 4688, 4696, 4698, 4704,
222011 4746, 4752, 4786, 4792, 4800, 4802, 4808, 4824, 4882, 4888,
222012 4957, 4960, 4969, 4992, 5008, 5024, 5120, 5121, 5741, 5743,
222013 5760, 5761, 5787, 5788, 5792, 5867, 5870, 5888, 5902, 5906,
222014 5920, 5938, 5941, 5952, 5970, 5984, 5998, 6002, 6016, 6068,
222015 6070, 6071, 6078, 6086, 6087, 6089, 6100, 6103, 6104, 6107,
222016 6108, 6109, 6112, 6128, 6144, 6150, 6151, 6155, 6158, 6160,
222017 6176, 6211, 6212, 6272, 6313, 6314, 6320, 6400, 6432, 6435,
222018 6439, 6441, 6448, 6450, 6451, 6457, 6464, 6468, 6470, 6480,
222019 6512, 6528, 6576, 6593, 6600, 6608, 6618, 6622, 6656, 6679,
222020 6681, 6686, 6688, 6741, 6742, 6743, 6744, 6752, 6753, 6754,
222021 6755, 6757, 6765, 6771, 6783, 6784, 6800, 6816, 6823, 6824,
222022 6912, 6916, 6917, 6964, 6965, 6966, 6971, 6972, 6973, 6978,
222023 6979, 6981, 6992, 7002, 7009, 7019, 7028, 7040, 7042, 7043,
222024 7073, 7074, 7078, 7080, 7082, 7083, 7084, 7086, 7088, 7098,
222025 7142, 7143, 7144, 7146, 7149, 7150, 7151, 7154, 7164, 7168,
222026 7204, 7212, 7220, 7222, 7227, 7232, 7245, 7248, 7258, 7288,
222027 7294, 7360, 7376, 7379, 7380, 7393, 7394, 7401, 7405, 7406,
222028 7410, 7412, 7413, 7424, 7468, 7531, 7544, 7545, 7579, 7616,
222029 7676, 7680, 7830, 7838, 7936, 7944, 7952, 7960, 7968, 7976,
222030 7984, 7992, 8000, 8008, 8016, 8025, 8027, 8029, 8031, 8033,
222031 8040, 8048, 8064, 8072, 8080, 8088, 8096, 8104, 8112, 8118,
222032 8120, 8124, 8125, 8126, 8127, 8130, 8134, 8136, 8140, 8141,
222033 8144, 8150, 8152, 8157, 8160, 8168, 8173, 8178, 8182, 8184,
222034 8188, 8189, 8192, 8203, 8208, 8214, 8216, 8217, 8218, 8219,
222035 8221, 8222, 8223, 8224, 8232, 8233, 8234, 8239, 8240, 8249,
222036 8250, 8251, 8255, 8257, 8260, 8261, 8262, 8263, 8274, 8275,
222037 8276, 8277, 8287, 8288, 8298, 8304, 8305, 8308, 8314, 8317,
222038 8318, 8319, 8320, 8330, 8333, 8334, 8336, 8352, 8400, 8413,
222039 8417, 8418, 8421, 8448, 8450, 8451, 8455, 8456, 8458, 8459,
222040 8462, 8464, 8467, 8468, 8469, 8470, 8472, 8473, 8478, 8484,
222041 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8496, 8500,
222042 8501, 8505, 8506, 8508, 8510, 8512, 8517, 8519, 8522, 8523,
222043 8524, 8526, 8527, 8528, 8544, 8579, 8581, 8585, 8592, 8597,
222044 8602, 8604, 8608, 8609, 8611, 8612, 8614, 8615, 8622, 8623,
222045 8654, 8656, 8658, 8659, 8660, 8661, 8692, 8960, 8968, 8972,
222046 8992, 8994, 9001, 9002, 9003, 9084, 9085, 9115, 9140, 9180,
222047 9186, 9216, 9280, 9312, 9372, 9450, 9472, 9655, 9656, 9665,
222048 9666, 9720, 9728, 9839, 9840, 9985, 10088, 10089, 10090, 10091,
222049 10092, 10093, 10094, 10095, 10096, 10097, 10098, 10099, 10100, 10101,
222050 10102, 10132, 10176, 10181, 10182, 10183, 10214, 10215, 10216, 10217,
222051 10218, 10219, 10220, 10221, 10222, 10223, 10224, 10240, 10496, 10627,
222052 10628, 10629, 10630, 10631, 10632, 10633, 10634, 10635, 10636, 10637,
222053 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647,
222054 10648, 10649, 10712, 10713, 10714, 10715, 10716, 10748, 10749, 10750,
222055 11008, 11056, 11077, 11079, 11088, 11264, 11312, 11360, 11363, 11365,
222056 11367, 11374, 11377, 11378, 11380, 11381, 11383, 11388, 11390, 11393,
222057 11394, 11492, 11493, 11499, 11503, 11506, 11513, 11517, 11518, 11520,
222058 11559, 11565, 11568, 11631, 11632, 11647, 11648, 11680, 11688, 11696,
222059 11704, 11712, 11720, 11728, 11736, 11744, 11776, 11778, 11779, 11780,
222060 11781, 11782, 11785, 11786, 11787, 11788, 11789, 11790, 11799, 11800,
222061 11802, 11803, 11804, 11805, 11806, 11808, 11809, 11810, 11811, 11812,
222062 11813, 11814, 11815, 11816, 11817, 11818, 11823, 11824, 11834, 11904,
222063 11931, 12032, 12272, 12288, 12289, 12292, 12293, 12294, 12295, 12296,
222064 12297, 12298, 12299, 12300, 12301, 12302, 12303, 12304, 12305, 12306,
222065 12308, 12309, 12310, 12311, 12312, 12313, 12314, 12315, 12316, 12317,
222066 12318, 12320, 12321, 12330, 12334, 12336, 12337, 12342, 12344, 12347,
222067 12348, 12349, 12350, 12353, 12441, 12443, 12445, 12447, 12448, 12449,
222068 12539, 12540, 12543, 12549, 12593, 12688, 12690, 12694, 12704, 12736,
222069 12784, 12800, 12832, 12842, 12872, 12880, 12881, 12896, 12928, 12938,
222070 12977, 12992, 13056, 13312, 19893, 19904, 19968, 40908, 40960, 40981,
222071 40982, 42128, 42192, 42232, 42238, 42240, 42508, 42509, 42512, 42528,
222072 42538, 42560, 42606, 42607, 42608, 42611, 42612, 42622, 42623, 42624,
222073 42655, 42656, 42726, 42736, 42738, 42752, 42775, 42784, 42786, 42800,
222074 42802, 42864, 42865, 42873, 42878, 42888, 42889, 42891, 42896, 42912,
222075 43000, 43002, 43003, 43010, 43011, 43014, 43015, 43019, 43020, 43043,
222076 43045, 43047, 43048, 43056, 43062, 43064, 43065, 43072, 43124, 43136,
222077 43138, 43188, 43204, 43214, 43216, 43232, 43250, 43256, 43259, 43264,
222078 43274, 43302, 43310, 43312, 43335, 43346, 43359, 43360, 43392, 43395,
222079 43396, 43443, 43444, 43446, 43450, 43452, 43453, 43457, 43471, 43472,
222080 43486, 43520, 43561, 43567, 43569, 43571, 43573, 43584, 43587, 43588,
222081 43596, 43597, 43600, 43612, 43616, 43632, 43633, 43639, 43642, 43643,
222082 43648, 43696, 43697, 43698, 43701, 43703, 43705, 43710, 43712, 43713,
222083 43714, 43739, 43741, 43742, 43744, 43755, 43756, 43758, 43760, 43762,
222084 43763, 43765, 43766, 43777, 43785, 43793, 43808, 43816, 43968, 44003,
222085 44005, 44006, 44008, 44009, 44011, 44012, 44013, 44016, 44032, 55203,
222086 55216, 55243, 55296, 56191, 56319, 57343, 57344, 63743, 63744, 64112,
222087 64256, 64275, 64285, 64286, 64287, 64297, 64298, 64312, 64318, 64320,
222088 64323, 64326, 64434, 64467, 64830, 64831, 64848, 64914, 65008, 65020,
222089 65021, 65024, 65040, 65047, 65048, 65049, 65056, 65072, 65073, 65075,
222090 65077, 65078, 65079, 65080, 65081, 65082, 65083, 65084, 65085, 65086,
222091 65087, 65088, 65089, 65090, 65091, 65092, 65093, 65095, 65096, 65097,
222092 65101, 65104, 65108, 65112, 65113, 65114, 65115, 65116, 65117, 65118,
222093 65119, 65122, 65123, 65124, 65128, 65129, 65130, 65136, 65142, 65279,
222094 65281, 65284, 65285, 65288, 65289, 65290, 65291, 65292, 65293, 65294,
222095 65296, 65306, 65308, 65311, 65313, 65339, 65340, 65341, 65342, 65343,
222096 65344, 65345, 65371, 65372, 65373, 65374, 65375, 65376, 65377, 65378,
222097 65379, 65380, 65382, 65392, 65393, 65438, 65440, 65474, 65482, 65490,
222098 65498, 65504, 65506, 65507, 65508, 65509, 65512, 65513, 65517, 65529,
222099 65532, 0, 13, 40, 60, 63, 80, 128, 256, 263,
222100 311, 320, 373, 377, 394, 400, 464, 509, 640, 672,
222101 768, 800, 816, 833, 834, 842, 896, 927, 928, 968,
222102 976, 977, 1024, 1064, 1104, 1184, 2048, 2056, 2058, 2103,
222103 2108, 2111, 2135, 2136, 2304, 2326, 2335, 2336, 2367, 2432,
222104 2494, 2560, 2561, 2565, 2572, 2576, 2581, 2585, 2616, 2623,
222105 2624, 2640, 2656, 2685, 2687, 2816, 2873, 2880, 2904, 2912,
222106 2936, 3072, 3680, 4096, 4097, 4098, 4099, 4152, 4167, 4178,
222107 4198, 4224, 4226, 4227, 4272, 4275, 4279, 4281, 4283, 4285,
222108 4286, 4304, 4336, 4352, 4355, 4391, 4396, 4397, 4406, 4416,
222109 4480, 4482, 4483, 4531, 4534, 4543, 4545, 4549, 4560, 5760,
222110 5803, 5804, 5805, 5806, 5808, 5814, 5815, 5824, 8192, 9216,
222111 9328, 12288, 26624, 28416, 28496, 28497, 28559, 28563, 45056, 53248,
222112 53504, 53545, 53605, 53607, 53610, 53613, 53619, 53627, 53635, 53637,
222113 53644, 53674, 53678, 53760, 53826, 53829, 54016, 54112, 54272, 54298,
222114 54324, 54350, 54358, 54376, 54402, 54428, 54430, 54434, 54437, 54441,
222115 54446, 54454, 54459, 54461, 54469, 54480, 54506, 54532, 54535, 54541,
222116 54550, 54558, 54584, 54587, 54592, 54598, 54602, 54610, 54636, 54662,
222117 54688, 54714, 54740, 54766, 54792, 54818, 54844, 54870, 54896, 54922,
222118 54952, 54977, 54978, 55003, 55004, 55010, 55035, 55036, 55061, 55062,
222119 55068, 55093, 55094, 55119, 55120, 55126, 55151, 55152, 55177, 55178,
222120 55184, 55209, 55210, 55235, 55236, 55242, 55246, 60928, 60933, 60961,
222121 60964, 60967, 60969, 60980, 60985, 60987, 60994, 60999, 61001, 61003,
222122 61005, 61009, 61012, 61015, 61017, 61019, 61021, 61023, 61025, 61028,
222123 61031, 61036, 61044, 61049, 61054, 61056, 61067, 61089, 61093, 61099,
222124 61168, 61440, 61488, 61600, 61617, 61633, 61649, 61696, 61712, 61744,
222125 61808, 61926, 61968, 62016, 62032, 62208, 62256, 62263, 62336, 62368,
222126 62406, 62432, 62464, 62528, 62530, 62713, 62720, 62784, 62800, 62971,
222127 63045, 63104, 63232, 0, 42710, 42752, 46900, 46912, 47133, 63488,
222128 1, 32, 256, 0, 65533,
222129 };
222130static u16 aFts5UnicodeData[] = {
222131 1025, 61, 117, 55, 117, 54, 50, 53, 57, 53,
222132 49, 85, 333, 85, 121, 85, 841, 54, 53, 50,
222133 56, 48, 56, 837, 54, 57, 50, 57, 1057, 61,
222134 53, 151, 58, 53, 56, 58, 39, 52, 57, 34,
222135 58, 56, 58, 57, 79, 56, 37, 85, 56, 47,
222136 39, 51, 111, 53, 745, 57, 233, 773, 57, 261,
222137 1822, 37, 542, 37, 1534, 222, 69, 73, 37, 126,
222138 126, 73, 69, 137, 37, 73, 37, 105, 101, 73,
222139 37, 73, 37, 190, 158, 37, 126, 126, 73, 37,
222140 126, 94, 37, 39, 94, 69, 135, 41, 40, 37,
222141 41, 40, 37, 41, 40, 37, 542, 37, 606, 37,
222142 41, 40, 37, 126, 73, 37, 1886, 197, 73, 37,
222143 73, 69, 126, 105, 37, 286, 2181, 39, 869, 582,
222144 152, 390, 472, 166, 248, 38, 56, 38, 568, 3596,
222145 158, 38, 56, 94, 38, 101, 53, 88, 41, 53,
222146 105, 41, 73, 37, 553, 297, 1125, 94, 37, 105,
222147 101, 798, 133, 94, 57, 126, 94, 37, 1641, 1541,
222148 1118, 58, 172, 75, 1790, 478, 37, 2846, 1225, 38,
222149 213, 1253, 53, 49, 55, 1452, 49, 44, 53, 76,
222150 53, 76, 53, 44, 871, 103, 85, 162, 121, 85,
222151 55, 85, 90, 364, 53, 85, 1031, 38, 327, 684,
222152 333, 149, 71, 44, 3175, 53, 39, 236, 34, 58,
222153 204, 70, 76, 58, 140, 71, 333, 103, 90, 39,
222154 469, 34, 39, 44, 967, 876, 2855, 364, 39, 333,
222155 1063, 300, 70, 58, 117, 38, 711, 140, 38, 300,
222156 38, 108, 38, 172, 501, 807, 108, 53, 39, 359,
222157 876, 108, 42, 1735, 44, 42, 44, 39, 106, 268,
222158 138, 44, 74, 39, 236, 327, 76, 85, 333, 53,
222159 38, 199, 231, 44, 74, 263, 71, 711, 231, 39,
222160 135, 44, 39, 106, 140, 74, 74, 44, 39, 42,
222161 71, 103, 76, 333, 71, 87, 207, 58, 55, 76,
222162 42, 199, 71, 711, 231, 71, 71, 71, 44, 106,
222163 76, 76, 108, 44, 135, 39, 333, 76, 103, 44,
222164 76, 42, 295, 103, 711, 231, 71, 167, 44, 39,
222165 106, 172, 76, 42, 74, 44, 39, 71, 76, 333,
222166 53, 55, 44, 74, 263, 71, 711, 231, 71, 167,
222167 44, 39, 42, 44, 42, 140, 74, 74, 44, 44,
222168 42, 71, 103, 76, 333, 58, 39, 207, 44, 39,
222169 199, 103, 135, 71, 39, 71, 71, 103, 391, 74,
222170 44, 74, 106, 106, 44, 39, 42, 333, 111, 218,
222171 55, 58, 106, 263, 103, 743, 327, 167, 39, 108,
222172 138, 108, 140, 76, 71, 71, 76, 333, 239, 58,
222173 74, 263, 103, 743, 327, 167, 44, 39, 42, 44,
222174 170, 44, 74, 74, 76, 74, 39, 71, 76, 333,
222175 71, 74, 263, 103, 1319, 39, 106, 140, 106, 106,
222176 44, 39, 42, 71, 76, 333, 207, 58, 199, 74,
222177 583, 775, 295, 39, 231, 44, 106, 108, 44, 266,
222178 74, 53, 1543, 44, 71, 236, 55, 199, 38, 268,
222179 53, 333, 85, 71, 39, 71, 39, 39, 135, 231,
222180 103, 39, 39, 71, 135, 44, 71, 204, 76, 39,
222181 167, 38, 204, 333, 135, 39, 122, 501, 58, 53,
222182 122, 76, 218, 333, 335, 58, 44, 58, 44, 58,
222183 44, 54, 50, 54, 50, 74, 263, 1159, 460, 42,
222184 172, 53, 76, 167, 364, 1164, 282, 44, 218, 90,
222185 181, 154, 85, 1383, 74, 140, 42, 204, 42, 76,
222186 74, 76, 39, 333, 213, 199, 74, 76, 135, 108,
222187 39, 106, 71, 234, 103, 140, 423, 44, 74, 76,
222188 202, 44, 39, 42, 333, 106, 44, 90, 1225, 41,
222189 41, 1383, 53, 38, 10631, 135, 231, 39, 135, 1319,
222190 135, 1063, 135, 231, 39, 135, 487, 1831, 135, 2151,
222191 108, 309, 655, 519, 346, 2727, 49, 19847, 85, 551,
222192 61, 839, 54, 50, 2407, 117, 110, 423, 135, 108,
222193 583, 108, 85, 583, 76, 423, 103, 76, 1671, 76,
222194 42, 236, 266, 44, 74, 364, 117, 38, 117, 55,
222195 39, 44, 333, 335, 213, 49, 149, 108, 61, 333,
222196 1127, 38, 1671, 1319, 44, 39, 2247, 935, 108, 138,
222197 76, 106, 74, 44, 202, 108, 58, 85, 333, 967,
222198 167, 1415, 554, 231, 74, 333, 47, 1114, 743, 76,
222199 106, 85, 1703, 42, 44, 42, 236, 44, 42, 44,
222200 74, 268, 202, 332, 44, 333, 333, 245, 38, 213,
222201 140, 42, 1511, 44, 42, 172, 42, 44, 170, 44,
222202 74, 231, 333, 245, 346, 300, 314, 76, 42, 967,
222203 42, 140, 74, 76, 42, 44, 74, 71, 333, 1415,
222204 44, 42, 76, 106, 44, 42, 108, 74, 149, 1159,
222205 266, 268, 74, 76, 181, 333, 103, 333, 967, 198,
222206 85, 277, 108, 53, 428, 42, 236, 135, 44, 135,
222207 74, 44, 71, 1413, 2022, 421, 38, 1093, 1190, 1260,
222208 140, 4830, 261, 3166, 261, 265, 197, 201, 261, 265,
222209 261, 265, 197, 201, 261, 41, 41, 41, 94, 229,
222210 265, 453, 261, 264, 261, 264, 261, 264, 165, 69,
222211 137, 40, 56, 37, 120, 101, 69, 137, 40, 120,
222212 133, 69, 137, 120, 261, 169, 120, 101, 69, 137,
222213 40, 88, 381, 162, 209, 85, 52, 51, 54, 84,
222214 51, 54, 52, 277, 59, 60, 162, 61, 309, 52,
222215 51, 149, 80, 117, 57, 54, 50, 373, 57, 53,
222216 48, 341, 61, 162, 194, 47, 38, 207, 121, 54,
222217 50, 38, 335, 121, 54, 50, 422, 855, 428, 139,
222218 44, 107, 396, 90, 41, 154, 41, 90, 37, 105,
222219 69, 105, 37, 58, 41, 90, 57, 169, 218, 41,
222220 58, 41, 58, 41, 58, 137, 58, 37, 137, 37,
222221 135, 37, 90, 69, 73, 185, 94, 101, 58, 57,
222222 90, 37, 58, 527, 1134, 94, 142, 47, 185, 186,
222223 89, 154, 57, 90, 57, 90, 57, 250, 57, 1018,
222224 89, 90, 57, 58, 57, 1018, 8601, 282, 153, 666,
222225 89, 250, 54, 50, 2618, 57, 986, 825, 1306, 217,
222226 602, 1274, 378, 1935, 2522, 719, 5882, 57, 314, 57,
222227 1754, 281, 3578, 57, 4634, 3322, 54, 50, 54, 50,
222228 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
222229 975, 1434, 185, 54, 50, 1017, 54, 50, 54, 50,
222230 54, 50, 54, 50, 54, 50, 537, 8218, 4217, 54,
222231 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
222232 50, 54, 50, 54, 50, 54, 50, 54, 50, 54,
222233 50, 2041, 54, 50, 54, 50, 1049, 54, 50, 8281,
222234 1562, 697, 90, 217, 346, 1513, 1509, 126, 73, 69,
222235 254, 105, 37, 94, 37, 94, 165, 70, 105, 37,
222236 3166, 37, 218, 158, 108, 94, 149, 47, 85, 1221,
222237 37, 37, 1799, 38, 53, 44, 743, 231, 231, 231,
222238 231, 231, 231, 231, 231, 1036, 85, 52, 51, 52,
222239 51, 117, 52, 51, 53, 52, 51, 309, 49, 85,
222240 49, 53, 52, 51, 85, 52, 51, 54, 50, 54,
222241 50, 54, 50, 54, 50, 181, 38, 341, 81, 858,
222242 2874, 6874, 410, 61, 117, 58, 38, 39, 46, 54,
222243 50, 54, 50, 54, 50, 54, 50, 54, 50, 90,
222244 54, 50, 54, 50, 54, 50, 54, 50, 49, 54,
222245 82, 58, 302, 140, 74, 49, 166, 90, 110, 38,
222246 39, 53, 90, 2759, 76, 88, 70, 39, 49, 2887,
222247 53, 102, 39, 1319, 3015, 90, 143, 346, 871, 1178,
222248 519, 1018, 335, 986, 271, 58, 495, 1050, 335, 1274,
222249 495, 2042, 8218, 39, 39, 2074, 39, 39, 679, 38,
222250 36583, 1786, 1287, 198, 85, 8583, 38, 117, 519, 333,
222251 71, 1502, 39, 44, 107, 53, 332, 53, 38, 798,
222252 44, 2247, 334, 76, 213, 760, 294, 88, 478, 69,
222253 2014, 38, 261, 190, 350, 38, 88, 158, 158, 382,
222254 70, 37, 231, 44, 103, 44, 135, 44, 743, 74,
222255 76, 42, 154, 207, 90, 55, 58, 1671, 149, 74,
222256 1607, 522, 44, 85, 333, 588, 199, 117, 39, 333,
222257 903, 268, 85, 743, 364, 74, 53, 935, 108, 42,
222258 1511, 44, 74, 140, 74, 44, 138, 437, 38, 333,
222259 85, 1319, 204, 74, 76, 74, 76, 103, 44, 263,
222260 44, 42, 333, 149, 519, 38, 199, 122, 39, 42,
222261 1543, 44, 39, 108, 71, 76, 167, 76, 39, 44,
222262 39, 71, 38, 85, 359, 42, 76, 74, 85, 39,
222263 70, 42, 44, 199, 199, 199, 231, 231, 1127, 74,
222264 44, 74, 44, 74, 53, 42, 44, 333, 39, 39,
222265 743, 1575, 36, 68, 68, 36, 63, 63, 11719, 3399,
222266 229, 165, 39, 44, 327, 57, 423, 167, 39, 71,
222267 71, 3463, 536, 11623, 54, 50, 2055, 1735, 391, 55,
222268 58, 524, 245, 54, 50, 53, 236, 53, 81, 80,
222269 54, 50, 54, 50, 54, 50, 54, 50, 54, 50,
222270 54, 50, 54, 50, 54, 50, 85, 54, 50, 149,
222271 112, 117, 149, 49, 54, 50, 54, 50, 54, 50,
222272 117, 57, 49, 121, 53, 55, 85, 167, 4327, 34,
222273 117, 55, 117, 54, 50, 53, 57, 53, 49, 85,
222274 333, 85, 121, 85, 841, 54, 53, 50, 56, 48,
222275 56, 837, 54, 57, 50, 57, 54, 50, 53, 54,
222276 50, 85, 327, 38, 1447, 70, 999, 199, 199, 199,
222277 103, 87, 57, 56, 58, 87, 58, 153, 90, 98,
222278 90, 391, 839, 615, 71, 487, 455, 3943, 117, 1455,
222279 314, 1710, 143, 570, 47, 410, 1466, 44, 935, 1575,
222280 999, 143, 551, 46, 263, 46, 967, 53, 1159, 263,
222281 53, 174, 1289, 1285, 2503, 333, 199, 39, 1415, 71,
222282 39, 743, 53, 271, 711, 207, 53, 839, 53, 1799,
222283 71, 39, 108, 76, 140, 135, 103, 871, 108, 44,
222284 271, 309, 935, 79, 53, 1735, 245, 711, 271, 615,
222285 271, 2343, 1007, 42, 44, 42, 1703, 492, 245, 655,
222286 333, 76, 42, 1447, 106, 140, 74, 76, 85, 34,
222287 149, 807, 333, 108, 1159, 172, 42, 268, 333, 149,
222288 76, 42, 1543, 106, 300, 74, 135, 149, 333, 1383,
222289 44, 42, 44, 74, 204, 42, 44, 333, 28135, 3182,
222290 149, 34279, 18215, 2215, 39, 1482, 140, 422, 71, 7898,
222291 1274, 1946, 74, 108, 122, 202, 258, 268, 90, 236,
222292 986, 140, 1562, 2138, 108, 58, 2810, 591, 841, 837,
222293 841, 229, 581, 841, 837, 41, 73, 41, 73, 137,
222294 265, 133, 37, 229, 357, 841, 837, 73, 137, 265,
222295 233, 837, 73, 137, 169, 41, 233, 837, 841, 837,
222296 841, 837, 841, 837, 841, 837, 841, 837, 841, 901,
222297 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
222298 809, 57, 805, 57, 197, 809, 57, 805, 57, 197,
222299 809, 57, 805, 57, 197, 94, 1613, 135, 871, 71,
222300 39, 39, 327, 135, 39, 39, 39, 39, 39, 39,
222301 103, 71, 39, 39, 39, 39, 39, 39, 71, 39,
222302 135, 231, 135, 135, 39, 327, 551, 103, 167, 551,
222303 89, 1434, 3226, 506, 474, 506, 506, 367, 1018, 1946,
222304 1402, 954, 1402, 314, 90, 1082, 218, 2266, 666, 1210,
222305 186, 570, 2042, 58, 5850, 154, 2010, 154, 794, 2266,
222306 378, 2266, 3738, 39, 39, 39, 39, 39, 39, 17351,
222307 34, 3074, 7692, 63, 63,
222308 };
222309
222310static int sqlite3Fts5UnicodeCategory(u32 iCode) {
222311 int iRes = -1;
222312 int iHi;
222313 int iLo;
222314 int ret;
222315 u16 iKey;
222316
222317 if( iCode>=(1<<20) ){
222318 return 0;
222319 }
222320 iLo = aFts5UnicodeBlock[(iCode>>16)];
222321 iHi = aFts5UnicodeBlock[1+(iCode>>16)];
222322 iKey = (iCode & 0xFFFF);
222323 while( iHi>iLo ){
222324 int iTest = (iHi + iLo) / 2;
222325 assert( iTest>=iLo && iTest<iHi )((void) (0));
222326 if( iKey>=aFts5UnicodeMap[iTest] ){
222327 iRes = iTest;
222328 iLo = iTest+1;
222329 }else{
222330 iHi = iTest;
222331 }
222332 }
222333
222334 if( iRes<0 ) return 0;
222335 if( iKey>=(aFts5UnicodeMap[iRes]+(aFts5UnicodeData[iRes]>>5)) ) return 0;
222336 ret = aFts5UnicodeData[iRes] & 0x1F;
222337 if( ret!=30 ) return ret;
222338 return ((iKey - aFts5UnicodeMap[iRes]) & 0x01) ? 5 : 9;
222339}
222340
222341static void sqlite3Fts5UnicodeAscii(u8 *aArray, u8 *aAscii){
222342 int i = 0;
222343 int iTbl = 0;
222344 while( i<128 ){
222345 int bToken = aArray[ aFts5UnicodeData[iTbl] & 0x1F ];
222346 int n = (aFts5UnicodeData[iTbl] >> 5) + i;
222347 for(; i<128 && i<n; i++){
222348 aAscii[i] = (u8)bToken;
222349 }
222350 iTbl++;
222351 }
222352}
222353
222354/*
222355** 2015 May 30
222356**
222357** The author disclaims copyright to this source code. In place of
222358** a legal notice, here is a blessing:
222359**
222360** May you do good and not evil.
222361** May you find forgiveness for yourself and forgive others.
222362** May you share freely, never taking more than you give.
222363**
222364******************************************************************************
222365**
222366** Routines for varint serialization and deserialization.
222367*/
222368
222369
222370/* #include "fts5Int.h" */
222371
222372/*
222373** This is a copy of the sqlite3GetVarint32() routine from the SQLite core.
222374** Except, this version does handle the single byte case that the core
222375** version depends on being handled before its function is called.
222376*/
222377static int sqlite3Fts5GetVarint32(const unsigned char *p, u32 *v){
222378 u32 a,b;
222379
222380 /* The 1-byte case. Overwhelmingly the most common. */
222381 a = *p;
222382 /* a: p0 (unmasked) */
222383 if (!(a&0x80))
222384 {
222385 /* Values between 0 and 127 */
222386 *v = a;
222387 return 1;
222388 }
222389
222390 /* The 2-byte case */
222391 p++;
222392 b = *p;
222393 /* b: p1 (unmasked) */
222394 if (!(b&0x80))
222395 {
222396 /* Values between 128 and 16383 */
222397 a &= 0x7f;
222398 a = a<<7;
222399 *v = a | b;
222400 return 2;
222401 }
222402
222403 /* The 3-byte case */
222404 p++;
222405 a = a<<14;
222406 a |= *p;
222407 /* a: p0<<14 | p2 (unmasked) */
222408 if (!(a&0x80))
222409 {
222410 /* Values between 16384 and 2097151 */
222411 a &= (0x7f<<14)|(0x7f);
222412 b &= 0x7f;
222413 b = b<<7;
222414 *v = a | b;
222415 return 3;
222416 }
222417
222418 /* A 32-bit varint is used to store size information in btrees.
222419 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
222420 ** A 3-byte varint is sufficient, for example, to record the size
222421 ** of a 1048569-byte BLOB or string.
222422 **
222423 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
222424 ** rare larger cases can be handled by the slower 64-bit varint
222425 ** routine.
222426 */
222427 {
222428 u64 v64;
222429 u8 n;
222430 p -= 2;
222431 n = sqlite3Fts5GetVarint(p, &v64);
222432 *v = ((u32)v64) & 0x7FFFFFFF;
222433 assert( n>3 && n<=9 )((void) (0));
222434 return n;
222435 }
222436}
222437
222438
222439/*
222440** Bitmasks used by sqlite3GetVarint(). These precomputed constants
222441** are defined here rather than simply putting the constant expressions
222442** inline in order to work around bugs in the RVT compiler.
222443**
222444** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
222445**
222446** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
222447*/
222448#define SLOT_2_00x001fc07f 0x001fc07f
222449#define SLOT_4_2_00xf01fc07f 0xf01fc07f
222450
222451/*
222452** Read a 64-bit variable-length integer from memory starting at p[0].
222453** Return the number of bytes read. The value is stored in *v.
222454*/
222455static u8 sqlite3Fts5GetVarint(const unsigned char *p, u64 *v){
222456 u32 a,b,s;
222457
222458 a = *p;
222459 /* a: p0 (unmasked) */
222460 if (!(a&0x80))
222461 {
222462 *v = a;
222463 return 1;
222464 }
222465
222466 p++;
222467 b = *p;
222468 /* b: p1 (unmasked) */
222469 if (!(b&0x80))
222470 {
222471 a &= 0x7f;
222472 a = a<<7;
222473 a |= b;
222474 *v = a;
222475 return 2;
222476 }
222477
222478 /* Verify that constants are precomputed correctly */
222479 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) )((void) (0));
222480 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) )((void) (0));
222481
222482 p++;
222483 a = a<<14;
222484 a |= *p;
222485 /* a: p0<<14 | p2 (unmasked) */
222486 if (!(a&0x80))
222487 {
222488 a &= SLOT_2_00x001fc07f;
222489 b &= 0x7f;
222490 b = b<<7;
222491 a |= b;
222492 *v = a;
222493 return 3;
222494 }
222495
222496 /* CSE1 from below */
222497 a &= SLOT_2_00x001fc07f;
222498 p++;
222499 b = b<<14;
222500 b |= *p;
222501 /* b: p1<<14 | p3 (unmasked) */
222502 if (!(b&0x80))
222503 {
222504 b &= SLOT_2_00x001fc07f;
222505 /* moved CSE1 up */
222506 /* a &= (0x7f<<14)|(0x7f); */
222507 a = a<<7;
222508 a |= b;
222509 *v = a;
222510 return 4;
222511 }
222512
222513 /* a: p0<<14 | p2 (masked) */
222514 /* b: p1<<14 | p3 (unmasked) */
222515 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
222516 /* moved CSE1 up */
222517 /* a &= (0x7f<<14)|(0x7f); */
222518 b &= SLOT_2_00x001fc07f;
222519 s = a;
222520 /* s: p0<<14 | p2 (masked) */
222521
222522 p++;
222523 a = a<<14;
222524 a |= *p;
222525 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
222526 if (!(a&0x80))
222527 {
222528 /* we can skip these cause they were (effectively) done above in calc'ing s */
222529 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
222530 /* b &= (0x7f<<14)|(0x7f); */
222531 b = b<<7;
222532 a |= b;
222533 s = s>>18;
222534 *v = ((u64)s)<<32 | a;
222535 return 5;
222536 }
222537
222538 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
222539 s = s<<7;
222540 s |= b;
222541 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
222542
222543 p++;
222544 b = b<<14;
222545 b |= *p;
222546 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
222547 if (!(b&0x80))
222548 {
222549 /* we can skip this cause it was (effectively) done above in calc'ing s */
222550 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
222551 a &= SLOT_2_00x001fc07f;
222552 a = a<<7;
222553 a |= b;
222554 s = s>>18;
222555 *v = ((u64)s)<<32 | a;
222556 return 6;
222557 }
222558
222559 p++;
222560 a = a<<14;
222561 a |= *p;
222562 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
222563 if (!(a&0x80))
222564 {
222565 a &= SLOT_4_2_00xf01fc07f;
222566 b &= SLOT_2_00x001fc07f;
222567 b = b<<7;
222568 a |= b;
222569 s = s>>11;
222570 *v = ((u64)s)<<32 | a;
222571 return 7;
222572 }
222573
222574 /* CSE2 from below */
222575 a &= SLOT_2_00x001fc07f;
222576 p++;
222577 b = b<<14;
222578 b |= *p;
222579 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
222580 if (!(b&0x80))
222581 {
222582 b &= SLOT_4_2_00xf01fc07f;
222583 /* moved CSE2 up */
222584 /* a &= (0x7f<<14)|(0x7f); */
222585 a = a<<7;
222586 a |= b;
222587 s = s>>4;
222588 *v = ((u64)s)<<32 | a;
222589 return 8;
222590 }
222591
222592 p++;
222593 a = a<<15;
222594 a |= *p;
222595 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
222596
222597 /* moved CSE2 up */
222598 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
222599 b &= SLOT_2_00x001fc07f;
222600 b = b<<8;
222601 a |= b;
222602
222603 s = s<<4;
222604 b = p[-4];
222605 b &= 0x7f;
222606 b = b>>3;
222607 s |= b;
222608
222609 *v = ((u64)s)<<32 | a;
222610
222611 return 9;
222612}
222613
222614/*
222615** The variable-length integer encoding is as follows:
222616**
222617** KEY:
222618** A = 0xxxxxxx 7 bits of data and one flag bit
222619** B = 1xxxxxxx 7 bits of data and one flag bit
222620** C = xxxxxxxx 8 bits of data
222621**
222622** 7 bits - A
222623** 14 bits - BA
222624** 21 bits - BBA
222625** 28 bits - BBBA
222626** 35 bits - BBBBA
222627** 42 bits - BBBBBA
222628** 49 bits - BBBBBBA
222629** 56 bits - BBBBBBBA
222630** 64 bits - BBBBBBBBC
222631*/
222632
222633#ifdef SQLITE_NOINLINE__attribute__((noinline))
222634# define FTS5_NOINLINE SQLITE_NOINLINE__attribute__((noinline))
222635#else
222636# define FTS5_NOINLINE
222637#endif
222638
222639/*
222640** Write a 64-bit variable-length integer to memory starting at p[0].
222641** The length of data write will be between 1 and 9 bytes. The number
222642** of bytes written is returned.
222643**
222644** A variable-length integer consists of the lower 7 bits of each byte
222645** for all bytes that have the 8th bit set and one byte with the 8th
222646** bit clear. Except, if we get to the 9th byte, it stores the full
222647** 8 bits and is the last byte.
222648*/
222649static int FTS5_NOINLINE fts5PutVarint64(unsigned char *p, u64 v){
222650 int i, j, n;
222651 u8 buf[10];
222652 if( v & (((u64)0xff000000)<<32) ){
222653 p[8] = (u8)v;
222654 v >>= 8;
222655 for(i=7; i>=0; i--){
222656 p[i] = (u8)((v & 0x7f) | 0x80);
222657 v >>= 7;
222658 }
222659 return 9;
222660 }
222661 n = 0;
222662 do{
222663 buf[n++] = (u8)((v & 0x7f) | 0x80);
222664 v >>= 7;
222665 }while( v!=0 );
222666 buf[0] &= 0x7f;
222667 assert( n<=9 )((void) (0));
222668 for(i=0, j=n-1; j>=0; j--, i++){
222669 p[i] = buf[j];
222670 }
222671 return n;
222672}
222673
222674static int sqlite3Fts5PutVarint(unsigned char *p, u64 v){
222675 if( v<=0x7f ){
222676 p[0] = v&0x7f;
222677 return 1;
222678 }
222679 if( v<=0x3fff ){
222680 p[0] = ((v>>7)&0x7f)|0x80;
222681 p[1] = v&0x7f;
222682 return 2;
222683 }
222684 return fts5PutVarint64(p,v);
222685}
222686
222687
222688static int sqlite3Fts5GetVarintLen(u32 iVal){
222689#if 0
222690 if( iVal<(1 << 7 ) ) return 1;
222691#endif
222692 assert( iVal>=(1 << 7) )((void) (0));
222693 if( iVal<(1 << 14) ) return 2;
222694 if( iVal<(1 << 21) ) return 3;
222695 if( iVal<(1 << 28) ) return 4;
222696 return 5;
222697}
222698
222699/*
222700** 2015 May 08
222701**
222702** The author disclaims copyright to this source code. In place of
222703** a legal notice, here is a blessing:
222704**
222705** May you do good and not evil.
222706** May you find forgiveness for yourself and forgive others.
222707** May you share freely, never taking more than you give.
222708**
222709******************************************************************************
222710**
222711** This is an SQLite virtual table module implementing direct access to an
222712** existing FTS5 index. The module may create several different types of
222713** tables:
222714**
222715** col:
222716** CREATE TABLE vocab(term, col, doc, cnt, PRIMARY KEY(term, col));
222717**
222718** One row for each term/column combination. The value of $doc is set to
222719** the number of fts5 rows that contain at least one instance of term
222720** $term within column $col. Field $cnt is set to the total number of
222721** instances of term $term in column $col (in any row of the fts5 table).
222722**
222723** row:
222724** CREATE TABLE vocab(term, doc, cnt, PRIMARY KEY(term));
222725**
222726** One row for each term in the database. The value of $doc is set to
222727** the number of fts5 rows that contain at least one instance of term
222728** $term. Field $cnt is set to the total number of instances of term
222729** $term in the database.
222730**
222731** instance:
222732** CREATE TABLE vocab(term, doc, col, offset, PRIMARY KEY(<all-fields>));
222733**
222734** One row for each term instance in the database.
222735*/
222736
222737
222738/* #include "fts5Int.h" */
222739
222740
222741typedef struct Fts5VocabTable Fts5VocabTable;
222742typedef struct Fts5VocabCursor Fts5VocabCursor;
222743
222744struct Fts5VocabTable {
222745 sqlite3_vtab base;
222746 char *zFts5Tbl; /* Name of fts5 table */
222747 char *zFts5Db; /* Db containing fts5 table */
222748 sqlite3 *db; /* Database handle */
222749 Fts5Global *pGlobal; /* FTS5 global object for this database */
222750 int eType; /* FTS5_VOCAB_COL, ROW or INSTANCE */
222751};
222752
222753struct Fts5VocabCursor {
222754 sqlite3_vtab_cursor base;
222755 sqlite3_stmt *pStmt; /* Statement holding lock on pIndex */
222756 Fts5Table *pFts5; /* Associated FTS5 table */
222757
222758 int bEof; /* True if this cursor is at EOF */
222759 Fts5IndexIter *pIter; /* Term/rowid iterator object */
222760
222761 int nLeTerm; /* Size of zLeTerm in bytes */
222762 char *zLeTerm; /* (term <= $zLeTerm) paramater, or NULL */
222763
222764 /* These are used by 'col' tables only */
222765 int iCol;
222766 i64 *aCnt;
222767 i64 *aDoc;
222768
222769 /* Output values used by all tables. */
222770 i64 rowid; /* This table's current rowid value */
222771 Fts5Buffer term; /* Current value of 'term' column */
222772
222773 /* Output values Used by 'instance' tables only */
222774 i64 iInstPos;
222775 int iInstOff;
222776};
222777
222778#define FTS5_VOCAB_COL 0
222779#define FTS5_VOCAB_ROW 1
222780#define FTS5_VOCAB_INSTANCE 2
222781
222782#define FTS5_VOCAB_COL_SCHEMA "term, col, doc, cnt"
222783#define FTS5_VOCAB_ROW_SCHEMA "term, doc, cnt"
222784#define FTS5_VOCAB_INST_SCHEMA "term, doc, col, offset"
222785
222786/*
222787** Bits for the mask used as the idxNum value by xBestIndex/xFilter.
222788*/
222789#define FTS5_VOCAB_TERM_EQ 0x01
222790#define FTS5_VOCAB_TERM_GE 0x02
222791#define FTS5_VOCAB_TERM_LE 0x04
222792
222793
222794/*
222795** Translate a string containing an fts5vocab table type to an
222796** FTS5_VOCAB_XXX constant. If successful, set *peType to the output
222797** value and return SQLITE_OK. Otherwise, set *pzErr to an error message
222798** and return SQLITE_ERROR.
222799*/
222800static int fts5VocabTableType(const char *zType, char **pzErr, int *peType){
222801 int rc = SQLITE_OK0;
222802 char *zCopy = sqlite3Fts5Strndup(&rc, zType, -1);
222803 if( rc==SQLITE_OK0 ){
222804 sqlite3Fts5Dequote(zCopy);
222805 if( sqlite3_stricmp(zCopy, "col")==0 ){
222806 *peType = FTS5_VOCAB_COL;
222807 }else
222808
222809 if( sqlite3_stricmp(zCopy, "row")==0 ){
222810 *peType = FTS5_VOCAB_ROW;
222811 }else
222812 if( sqlite3_stricmp(zCopy, "instance")==0 ){
222813 *peType = FTS5_VOCAB_INSTANCE;
222814 }else
222815 {
222816 *pzErr = sqlite3_mprintf("fts5vocab: unknown table type: %Q", zCopy);
222817 rc = SQLITE_ERROR1;
222818 }
222819 sqlite3_free(zCopy);
222820 }
222821
222822 return rc;
222823}
222824
222825
222826/*
222827** The xDisconnect() virtual table method.
222828*/
222829static int fts5VocabDisconnectMethod(sqlite3_vtab *pVtab){
222830 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
222831 sqlite3_free(pTab);
222832 return SQLITE_OK0;
222833}
222834
222835/*
222836** The xDestroy() virtual table method.
222837*/
222838static int fts5VocabDestroyMethod(sqlite3_vtab *pVtab){
222839 Fts5VocabTable *pTab = (Fts5VocabTable*)pVtab;
222840 sqlite3_free(pTab);
222841 return SQLITE_OK0;
222842}
222843
222844/*
222845** This function is the implementation of both the xConnect and xCreate
222846** methods of the FTS3 virtual table.
222847**
222848** The argv[] array contains the following:
222849**
222850** argv[0] -> module name ("fts5vocab")
222851** argv[1] -> database name
222852** argv[2] -> table name
222853**
222854** then:
222855**
222856** argv[3] -> name of fts5 table
222857** argv[4] -> type of fts5vocab table
222858**
222859** or, for tables in the TEMP schema only.
222860**
222861** argv[3] -> name of fts5 tables database
222862** argv[4] -> name of fts5 table
222863** argv[5] -> type of fts5vocab table
222864*/
222865static int fts5VocabInitVtab(
222866 sqlite3 *db, /* The SQLite database connection */
222867 void *pAux, /* Pointer to Fts5Global object */
222868 int argc, /* Number of elements in argv array */
222869 const char * const *argv, /* xCreate/xConnect argument array */
222870 sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
222871 char **pzErr /* Write any error message here */
222872){
222873 const char *azSchema[] = {
222874 "CREATE TABlE vocab(" FTS5_VOCAB_COL_SCHEMA ")",
222875 "CREATE TABlE vocab(" FTS5_VOCAB_ROW_SCHEMA ")",
222876 "CREATE TABlE vocab(" FTS5_VOCAB_INST_SCHEMA ")"
222877 };
222878
222879 Fts5VocabTable *pRet = 0;
222880 int rc = SQLITE_OK0; /* Return code */
222881 int bDb;
222882
222883 bDb = (argc==6 && strlen(argv[1])==4 && memcmp("temp", argv[1], 4)==0);
222884
222885 if( argc!=5 && bDb==0 ){
222886 *pzErr = sqlite3_mprintf("wrong number of vtable arguments");
222887 rc = SQLITE_ERROR1;
222888 }else{
222889 int nByte; /* Bytes of space to allocate */
222890 const char *zDb = bDb ? argv[3] : argv[1];
222891 const char *zTab = bDb ? argv[4] : argv[3];
222892 const char *zType = bDb ? argv[5] : argv[4];
222893 int nDb = (int)strlen(zDb)+1;
222894 int nTab = (int)strlen(zTab)+1;
222895 int eType = 0;
222896
222897 rc = fts5VocabTableType(zType, pzErr, &eType);
222898 if( rc==SQLITE_OK0 ){
222899 assert( eType>=0 && eType<ArraySize(azSchema) )((void) (0));
222900 rc = sqlite3_declare_vtab(db, azSchema[eType]);
222901 }
222902
222903 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
222904 pRet = sqlite3Fts5MallocZero(&rc, nByte);
222905 if( pRet ){
222906 pRet->pGlobal = (Fts5Global*)pAux;
222907 pRet->eType = eType;
222908 pRet->db = db;
222909 pRet->zFts5Tbl = (char*)&pRet[1];
222910 pRet->zFts5Db = &pRet->zFts5Tbl[nTab];
222911 memcpy(pRet->zFts5Tbl, zTab, nTab);
222912 memcpy(pRet->zFts5Db, zDb, nDb);
222913 sqlite3Fts5Dequote(pRet->zFts5Tbl);
222914 sqlite3Fts5Dequote(pRet->zFts5Db);
222915 }
222916 }
222917
222918 *ppVTab = (sqlite3_vtab*)pRet;
222919 return rc;
222920}
222921
222922
222923/*
222924** The xConnect() and xCreate() methods for the virtual table. All the
222925** work is done in function fts5VocabInitVtab().
222926*/
222927static int fts5VocabConnectMethod(
222928 sqlite3 *db, /* Database connection */
222929 void *pAux, /* Pointer to tokenizer hash table */
222930 int argc, /* Number of elements in argv array */
222931 const char * const *argv, /* xCreate/xConnect argument array */
222932 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
222933 char **pzErr /* OUT: sqlite3_malloc'd error message */
222934){
222935 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
222936}
222937static int fts5VocabCreateMethod(
222938 sqlite3 *db, /* Database connection */
222939 void *pAux, /* Pointer to tokenizer hash table */
222940 int argc, /* Number of elements in argv array */
222941 const char * const *argv, /* xCreate/xConnect argument array */
222942 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
222943 char **pzErr /* OUT: sqlite3_malloc'd error message */
222944){
222945 return fts5VocabInitVtab(db, pAux, argc, argv, ppVtab, pzErr);
222946}
222947
222948/*
222949** Implementation of the xBestIndex method.
222950**
222951** Only constraints of the form:
222952**
222953** term <= ?
222954** term == ?
222955** term >= ?
222956**
222957** are interpreted. Less-than and less-than-or-equal are treated
222958** identically, as are greater-than and greater-than-or-equal.
222959*/
222960static int fts5VocabBestIndexMethod(
222961 sqlite3_vtab *pUnused,
222962 sqlite3_index_info *pInfo
222963){
222964 int i;
222965 int iTermEq = -1;
222966 int iTermGe = -1;
222967 int iTermLe = -1;
222968 int idxNum = 0;
222969 int nArg = 0;
222970
222971 UNUSED_PARAM(pUnused);
222972
222973 for(i=0; i<pInfo->nConstraint; i++){
222974 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
222975 if( p->usable==0 ) continue;
222976 if( p->iColumn==0 ){ /* term column */
222977 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ2 ) iTermEq = i;
222978 if( p->op==SQLITE_INDEX_CONSTRAINT_LE8 ) iTermLe = i;
222979 if( p->op==SQLITE_INDEX_CONSTRAINT_LT16 ) iTermLe = i;
222980 if( p->op==SQLITE_INDEX_CONSTRAINT_GE32 ) iTermGe = i;
222981 if( p->op==SQLITE_INDEX_CONSTRAINT_GT4 ) iTermGe = i;
222982 }
222983 }
222984
222985 if( iTermEq>=0 ){
222986 idxNum |= FTS5_VOCAB_TERM_EQ;
222987 pInfo->aConstraintUsage[iTermEq].argvIndex = ++nArg;
222988 pInfo->estimatedCost = 100;
222989 }else{
222990 pInfo->estimatedCost = 1000000;
222991 if( iTermGe>=0 ){
222992 idxNum |= FTS5_VOCAB_TERM_GE;
222993 pInfo->aConstraintUsage[iTermGe].argvIndex = ++nArg;
222994 pInfo->estimatedCost = pInfo->estimatedCost / 2;
222995 }
222996 if( iTermLe>=0 ){
222997 idxNum |= FTS5_VOCAB_TERM_LE;
222998 pInfo->aConstraintUsage[iTermLe].argvIndex = ++nArg;
222999 pInfo->estimatedCost = pInfo->estimatedCost / 2;
223000 }
223001 }
223002
223003 /* This virtual table always delivers results in ascending order of
223004 ** the "term" column (column 0). So if the user has requested this
223005 ** specifically - "ORDER BY term" or "ORDER BY term ASC" - set the
223006 ** sqlite3_index_info.orderByConsumed flag to tell the core the results
223007 ** are already in sorted order. */
223008 if( pInfo->nOrderBy==1
223009 && pInfo->aOrderBy[0].iColumn==0
223010 && pInfo->aOrderBy[0].desc==0
223011 ){
223012 pInfo->orderByConsumed = 1;
223013 }
223014
223015 pInfo->idxNum = idxNum;
223016 return SQLITE_OK0;
223017}
223018
223019/*
223020** Implementation of xOpen method.
223021*/
223022static int fts5VocabOpenMethod(
223023 sqlite3_vtab *pVTab,
223024 sqlite3_vtab_cursor **ppCsr
223025){
223026 Fts5VocabTable *pTab = (Fts5VocabTable*)pVTab;
223027 Fts5Table *pFts5 = 0;
223028 Fts5VocabCursor *pCsr = 0;
223029 int rc = SQLITE_OK0;
223030 sqlite3_stmt *pStmt = 0;
223031 char *zSql = 0;
223032
223033 zSql = sqlite3Fts5Mprintf(&rc,
223034 "SELECT t.%Q FROM %Q.%Q AS t WHERE t.%Q MATCH '*id'",
223035 pTab->zFts5Tbl, pTab->zFts5Db, pTab->zFts5Tbl, pTab->zFts5Tbl
223036 );
223037 if( zSql ){
223038 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
223039 }
223040 sqlite3_free(zSql);
223041 assert( rc==SQLITE_OK || pStmt==0 )((void) (0));
223042 if( rc==SQLITE_ERROR1 ) rc = SQLITE_OK0;
223043
223044 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW100 ){
223045 i64 iId = sqlite3_column_int64(pStmt, 0);
223046 pFts5 = sqlite3Fts5TableFromCsrid(pTab->pGlobal, iId);
223047 }
223048
223049 if( rc==SQLITE_OK0 ){
223050 if( pFts5==0 ){
223051 rc = sqlite3_finalize(pStmt);
223052 pStmt = 0;
223053 if( rc==SQLITE_OK0 ){
223054 pVTab->zErrMsg = sqlite3_mprintf(
223055 "no such fts5 table: %s.%s", pTab->zFts5Db, pTab->zFts5Tbl
223056 );
223057 rc = SQLITE_ERROR1;
223058 }
223059 }else{
223060 rc = sqlite3Fts5FlushToDisk(pFts5);
223061 }
223062 }
223063
223064 if( rc==SQLITE_OK0 ){
223065 int nByte = pFts5->pConfig->nCol * sizeof(i64)*2 + sizeof(Fts5VocabCursor);
223066 pCsr = (Fts5VocabCursor*)sqlite3Fts5MallocZero(&rc, nByte);
223067 }
223068
223069 if( pCsr ){
223070 pCsr->pFts5 = pFts5;
223071 pCsr->pStmt = pStmt;
223072 pCsr->aCnt = (i64*)&pCsr[1];
223073 pCsr->aDoc = &pCsr->aCnt[pFts5->pConfig->nCol];
223074 }else{
223075 sqlite3_finalize(pStmt);
223076 }
223077
223078 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
223079 return rc;
223080}
223081
223082static void fts5VocabResetCursor(Fts5VocabCursor *pCsr){
223083 pCsr->rowid = 0;
223084 sqlite3Fts5IterClose(pCsr->pIter);
223085 pCsr->pIter = 0;
223086 sqlite3_free(pCsr->zLeTerm);
223087 pCsr->nLeTerm = -1;
223088 pCsr->zLeTerm = 0;
223089 pCsr->bEof = 0;
223090}
223091
223092/*
223093** Close the cursor. For additional information see the documentation
223094** on the xClose method of the virtual table interface.
223095*/
223096static int fts5VocabCloseMethod(sqlite3_vtab_cursor *pCursor){
223097 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
223098 fts5VocabResetCursor(pCsr);
223099 sqlite3Fts5BufferFree(&pCsr->term);
223100 sqlite3_finalize(pCsr->pStmt);
223101 sqlite3_free(pCsr);
223102 return SQLITE_OK0;
223103}
223104
223105static int fts5VocabInstanceNewTerm(Fts5VocabCursor *pCsr){
223106 int rc = SQLITE_OK0;
223107
223108 if( sqlite3Fts5IterEof(pCsr->pIter) ){
223109 pCsr->bEof = 1;
223110 }else{
223111 const char *zTerm;
223112 int nTerm;
223113 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
223114 if( pCsr->nLeTerm>=0 ){
223115 int nCmp = MIN(nTerm, pCsr->nLeTerm)((nTerm)<(pCsr->nLeTerm)?(nTerm):(pCsr->nLeTerm));
223116 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
223117 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
223118 pCsr->bEof = 1;
223119 }
223120 }
223121
223122 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
223123 }
223124 return rc;
223125}
223126
223127static int fts5VocabInstanceNext(Fts5VocabCursor *pCsr){
223128 int eDetail = pCsr->pFts5->pConfig->eDetail;
223129 int rc = SQLITE_OK0;
223130 Fts5IndexIter *pIter = pCsr->pIter;
223131 i64 *pp = &pCsr->iInstPos;
223132 int *po = &pCsr->iInstOff;
223133
223134 assert( sqlite3Fts5IterEof(pIter)==0 )((void) (0));
223135 assert( pCsr->bEof==0 )((void) (0));
223136 while( eDetail==FTS5_DETAIL_NONE
223137 || sqlite3Fts5PoslistNext64(pIter->pData, pIter->nData, po, pp)
223138 ){
223139 pCsr->iInstPos = 0;
223140 pCsr->iInstOff = 0;
223141
223142 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
223143 if( rc==SQLITE_OK0 ){
223144 rc = fts5VocabInstanceNewTerm(pCsr);
223145 if( pCsr->bEof || eDetail==FTS5_DETAIL_NONE ) break;
223146 }
223147 if( rc ){
223148 pCsr->bEof = 1;
223149 break;
223150 }
223151 }
223152
223153 return rc;
223154}
223155
223156/*
223157** Advance the cursor to the next row in the table.
223158*/
223159static int fts5VocabNextMethod(sqlite3_vtab_cursor *pCursor){
223160 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
223161 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
223162 int rc = SQLITE_OK0;
223163 int nCol = pCsr->pFts5->pConfig->nCol;
223164
223165 pCsr->rowid++;
223166
223167 if( pTab->eType==FTS5_VOCAB_INSTANCE ){
223168 return fts5VocabInstanceNext(pCsr);
223169 }
223170
223171 if( pTab->eType==FTS5_VOCAB_COL ){
223172 for(pCsr->iCol++; pCsr->iCol<nCol; pCsr->iCol++){
223173 if( pCsr->aDoc[pCsr->iCol] ) break;
223174 }
223175 }
223176
223177 if( pTab->eType!=FTS5_VOCAB_COL || pCsr->iCol>=nCol ){
223178 if( sqlite3Fts5IterEof(pCsr->pIter) ){
223179 pCsr->bEof = 1;
223180 }else{
223181 const char *zTerm;
223182 int nTerm;
223183
223184 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
223185 assert( nTerm>=0 )((void) (0));
223186 if( pCsr->nLeTerm>=0 ){
223187 int nCmp = MIN(nTerm, pCsr->nLeTerm)((nTerm)<(pCsr->nLeTerm)?(nTerm):(pCsr->nLeTerm));
223188 int bCmp = memcmp(pCsr->zLeTerm, zTerm, nCmp);
223189 if( bCmp<0 || (bCmp==0 && pCsr->nLeTerm<nTerm) ){
223190 pCsr->bEof = 1;
223191 return SQLITE_OK0;
223192 }
223193 }
223194
223195 sqlite3Fts5BufferSet(&rc, &pCsr->term, nTerm, (const u8*)zTerm);
223196 memset(pCsr->aCnt, 0, nCol * sizeof(i64));
223197 memset(pCsr->aDoc, 0, nCol * sizeof(i64));
223198 pCsr->iCol = 0;
223199
223200 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW )((void) (0));
223201 while( rc==SQLITE_OK0 ){
223202 int eDetail = pCsr->pFts5->pConfig->eDetail;
223203 const u8 *pPos; int nPos; /* Position list */
223204 i64 iPos = 0; /* 64-bit position read from poslist */
223205 int iOff = 0; /* Current offset within position list */
223206
223207 pPos = pCsr->pIter->pData;
223208 nPos = pCsr->pIter->nData;
223209
223210 switch( pTab->eType ){
223211 case FTS5_VOCAB_ROW:
223212 if( eDetail==FTS5_DETAIL_FULL ){
223213 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
223214 pCsr->aCnt[0]++;
223215 }
223216 }
223217 pCsr->aDoc[0]++;
223218 break;
223219
223220 case FTS5_VOCAB_COL:
223221 if( eDetail==FTS5_DETAIL_FULL ){
223222 int iCol = -1;
223223 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
223224 int ii = FTS5_POS2COLUMN(iPos);
223225 if( iCol!=ii ){
223226 if( ii>=nCol ){
223227 rc = FTS5_CORRUPT;
223228 break;
223229 }
223230 pCsr->aDoc[ii]++;
223231 iCol = ii;
223232 }
223233 pCsr->aCnt[ii]++;
223234 }
223235 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
223236 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
223237 assert_nc( iPos>=0 && iPos<nCol );
223238 if( iPos>=nCol ){
223239 rc = FTS5_CORRUPT;
223240 break;
223241 }
223242 pCsr->aDoc[iPos]++;
223243 }
223244 }else{
223245 assert( eDetail==FTS5_DETAIL_NONE )((void) (0));
223246 pCsr->aDoc[0]++;
223247 }
223248 break;
223249
223250 default:
223251 assert( pTab->eType==FTS5_VOCAB_INSTANCE )((void) (0));
223252 break;
223253 }
223254
223255 if( rc==SQLITE_OK0 ){
223256 rc = sqlite3Fts5IterNextScan(pCsr->pIter);
223257 }
223258 if( pTab->eType==FTS5_VOCAB_INSTANCE ) break;
223259
223260 if( rc==SQLITE_OK0 ){
223261 zTerm = sqlite3Fts5IterTerm(pCsr->pIter, &nTerm);
223262 if( nTerm!=pCsr->term.n
223263 || (nTerm>0 && memcmp(zTerm, pCsr->term.p, nTerm))
223264 ){
223265 break;
223266 }
223267 if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
223268 }
223269 }
223270 }
223271 }
223272
223273 if( rc==SQLITE_OK0 && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
223274 for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++);
223275 if( pCsr->iCol==nCol ){
223276 rc = FTS5_CORRUPT;
223277 }
223278 }
223279 return rc;
223280}
223281
223282/*
223283** This is the xFilter implementation for the virtual table.
223284*/
223285static int fts5VocabFilterMethod(
223286 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
223287 int idxNum, /* Strategy index */
223288 const char *zUnused, /* Unused */
223289 int nUnused, /* Number of elements in apVal */
223290 sqlite3_value **apVal /* Arguments for the indexing scheme */
223291){
223292 Fts5VocabTable *pTab = (Fts5VocabTable*)pCursor->pVtab;
223293 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
223294 int eType = pTab->eType;
223295 int rc = SQLITE_OK0;
223296
223297 int iVal = 0;
223298 int f = FTS5INDEX_QUERY_SCAN;
223299 const char *zTerm = 0;
223300 int nTerm = 0;
223301
223302 sqlite3_value *pEq = 0;
223303 sqlite3_value *pGe = 0;
223304 sqlite3_value *pLe = 0;
223305
223306 UNUSED_PARAM2(zUnused, nUnused);
223307
223308 fts5VocabResetCursor(pCsr);
223309 if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
223310 if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
223311 if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
223312
223313 if( pEq ){
223314 zTerm = (const char *)sqlite3_value_text(pEq);
223315 nTerm = sqlite3_value_bytes(pEq);
223316 f = 0;
223317 }else{
223318 if( pGe ){
223319 zTerm = (const char *)sqlite3_value_text(pGe);
223320 nTerm = sqlite3_value_bytes(pGe);
223321 }
223322 if( pLe ){
223323 const char *zCopy = (const char *)sqlite3_value_text(pLe);
223324 if( zCopy==0 ) zCopy = "";
223325 pCsr->nLeTerm = sqlite3_value_bytes(pLe);
223326 pCsr->zLeTerm = sqlite3_malloc(pCsr->nLeTerm+1);
223327 if( pCsr->zLeTerm==0 ){
223328 rc = SQLITE_NOMEM7;
223329 }else{
223330 memcpy(pCsr->zLeTerm, zCopy, pCsr->nLeTerm+1);
223331 }
223332 }
223333 }
223334
223335 if( rc==SQLITE_OK0 ){
223336 Fts5Index *pIndex = pCsr->pFts5->pIndex;
223337 rc = sqlite3Fts5IndexQuery(pIndex, zTerm, nTerm, f, 0, &pCsr->pIter);
223338 }
223339 if( rc==SQLITE_OK0 && eType==FTS5_VOCAB_INSTANCE ){
223340 rc = fts5VocabInstanceNewTerm(pCsr);
223341 }
223342 if( rc==SQLITE_OK0 && !pCsr->bEof
223343 && (eType!=FTS5_VOCAB_INSTANCE
223344 || pCsr->pFts5->pConfig->eDetail!=FTS5_DETAIL_NONE)
223345 ){
223346 rc = fts5VocabNextMethod(pCursor);
223347 }
223348
223349 return rc;
223350}
223351
223352/*
223353** This is the xEof method of the virtual table. SQLite calls this
223354** routine to find out if it has reached the end of a result set.
223355*/
223356static int fts5VocabEofMethod(sqlite3_vtab_cursor *pCursor){
223357 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
223358 return pCsr->bEof;
223359}
223360
223361static int fts5VocabColumnMethod(
223362 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
223363 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
223364 int iCol /* Index of column to read value from */
223365){
223366 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
223367 int eDetail = pCsr->pFts5->pConfig->eDetail;
223368 int eType = ((Fts5VocabTable*)(pCursor->pVtab))->eType;
223369 i64 iVal = 0;
223370
223371 if( iCol==0 ){
223372 sqlite3_result_text(
223373 pCtx, (const char*)pCsr->term.p, pCsr->term.n, SQLITE_TRANSIENT((sqlite3_destructor_type)-1)
223374 );
223375 }else if( eType==FTS5_VOCAB_COL ){
223376 assert( iCol==1 || iCol==2 || iCol==3 )((void) (0));
223377 if( iCol==1 ){
223378 if( eDetail!=FTS5_DETAIL_NONE ){
223379 const char *z = pCsr->pFts5->pConfig->azCol[pCsr->iCol];
223380 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
223381 }
223382 }else if( iCol==2 ){
223383 iVal = pCsr->aDoc[pCsr->iCol];
223384 }else{
223385 iVal = pCsr->aCnt[pCsr->iCol];
223386 }
223387 }else if( eType==FTS5_VOCAB_ROW ){
223388 assert( iCol==1 || iCol==2 )((void) (0));
223389 if( iCol==1 ){
223390 iVal = pCsr->aDoc[0];
223391 }else{
223392 iVal = pCsr->aCnt[0];
223393 }
223394 }else{
223395 assert( eType==FTS5_VOCAB_INSTANCE )((void) (0));
223396 switch( iCol ){
223397 case 1:
223398 sqlite3_result_int64(pCtx, pCsr->pIter->iRowid);
223399 break;
223400 case 2: {
223401 int ii = -1;
223402 if( eDetail==FTS5_DETAIL_FULL ){
223403 ii = FTS5_POS2COLUMN(pCsr->iInstPos);
223404 }else if( eDetail==FTS5_DETAIL_COLUMNS ){
223405 ii = (int)pCsr->iInstPos;
223406 }
223407 if( ii>=0 && ii<pCsr->pFts5->pConfig->nCol ){
223408 const char *z = pCsr->pFts5->pConfig->azCol[ii];
223409 sqlite3_result_text(pCtx, z, -1, SQLITE_STATIC((sqlite3_destructor_type)0));
223410 }
223411 break;
223412 }
223413 default: {
223414 assert( iCol==3 )((void) (0));
223415 if( eDetail==FTS5_DETAIL_FULL ){
223416 int ii = FTS5_POS2OFFSET(pCsr->iInstPos);
223417 sqlite3_result_int(pCtx, ii);
223418 }
223419 break;
223420 }
223421 }
223422 }
223423
223424 if( iVal>0 ) sqlite3_result_int64(pCtx, iVal);
223425 return SQLITE_OK0;
223426}
223427
223428/*
223429** This is the xRowid method. The SQLite core calls this routine to
223430** retrieve the rowid for the current row of the result set. The
223431** rowid should be written to *pRowid.
223432*/
223433static int fts5VocabRowidMethod(
223434 sqlite3_vtab_cursor *pCursor,
223435 sqlite_int64 *pRowid
223436){
223437 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
223438 *pRowid = pCsr->rowid;
223439 return SQLITE_OK0;
223440}
223441
223442static int sqlite3Fts5VocabInit(Fts5Global *pGlobal, sqlite3 *db){
223443 static const sqlite3_module fts5Vocab = {
223444 /* iVersion */ 2,
223445 /* xCreate */ fts5VocabCreateMethod,
223446 /* xConnect */ fts5VocabConnectMethod,
223447 /* xBestIndex */ fts5VocabBestIndexMethod,
223448 /* xDisconnect */ fts5VocabDisconnectMethod,
223449 /* xDestroy */ fts5VocabDestroyMethod,
223450 /* xOpen */ fts5VocabOpenMethod,
223451 /* xClose */ fts5VocabCloseMethod,
223452 /* xFilter */ fts5VocabFilterMethod,
223453 /* xNext */ fts5VocabNextMethod,
223454 /* xEof */ fts5VocabEofMethod,
223455 /* xColumn */ fts5VocabColumnMethod,
223456 /* xRowid */ fts5VocabRowidMethod,
223457 /* xUpdate */ 0,
223458 /* xBegin */ 0,
223459 /* xSync */ 0,
223460 /* xCommit */ 0,
223461 /* xRollback */ 0,
223462 /* xFindFunction */ 0,
223463 /* xRename */ 0,
223464 /* xSavepoint */ 0,
223465 /* xRelease */ 0,
223466 /* xRollbackTo */ 0,
223467 /* xShadowName */ 0
223468 };
223469 void *p = (void*)pGlobal;
223470
223471 return sqlite3_create_module_v2(db, "fts5vocab", &fts5Vocab, p, 0);
223472}
223473
223474
223475
223476#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
223477
223478/************** End of fts5.c ************************************************/
223479/************** Begin file stmt.c ********************************************/
223480/*
223481** 2017-05-31
223482**
223483** The author disclaims copyright to this source code. In place of
223484** a legal notice, here is a blessing:
223485**
223486** May you do good and not evil.
223487** May you find forgiveness for yourself and forgive others.
223488** May you share freely, never taking more than you give.
223489**
223490*************************************************************************
223491**
223492** This file demonstrates an eponymous virtual table that returns information
223493** about all prepared statements for the database connection.
223494**
223495** Usage example:
223496**
223497** .load ./stmt
223498** .mode line
223499** .header on
223500** SELECT * FROM stmt;
223501*/
223502#if !defined(SQLITE_CORE1) || defined(SQLITE_ENABLE_STMTVTAB)
223503#if !defined(SQLITEINT_H)
223504/* #include "sqlite3ext.h" */
223505#endif
223506SQLITE_EXTENSION_INIT1
223507/* #include <assert.h> */
223508/* #include <string.h> */
223509
223510#ifndef SQLITE_OMIT_VIRTUALTABLE
223511
223512/* stmt_vtab is a subclass of sqlite3_vtab which will
223513** serve as the underlying representation of a stmt virtual table
223514*/
223515typedef struct stmt_vtab stmt_vtab;
223516struct stmt_vtab {
223517 sqlite3_vtab base; /* Base class - must be first */
223518 sqlite3 *db; /* Database connection for this stmt vtab */
223519};
223520
223521/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
223522** serve as the underlying representation of a cursor that scans
223523** over rows of the result
223524*/
223525typedef struct stmt_cursor stmt_cursor;
223526struct stmt_cursor {
223527 sqlite3_vtab_cursor base; /* Base class - must be first */
223528 sqlite3 *db; /* Database connection for this cursor */
223529 sqlite3_stmt *pStmt; /* Statement cursor is currently pointing at */
223530 sqlite3_int64 iRowid; /* The rowid */
223531};
223532
223533/*
223534** The stmtConnect() method is invoked to create a new
223535** stmt_vtab that describes the stmt virtual table.
223536**
223537** Think of this routine as the constructor for stmt_vtab objects.
223538**
223539** All this routine needs to do is:
223540**
223541** (1) Allocate the stmt_vtab object and initialize all fields.
223542**
223543** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
223544** result set of queries against stmt will look like.
223545*/
223546static int stmtConnect(
223547 sqlite3 *db,
223548 void *pAux,
223549 int argc, const char *const*argv,
223550 sqlite3_vtab **ppVtab,
223551 char **pzErr
223552){
223553 stmt_vtab *pNew;
223554 int rc;
223555
223556/* Column numbers */
223557#define STMT_COLUMN_SQL 0 /* SQL for the statement */
223558#define STMT_COLUMN_NCOL 1 /* Number of result columns */
223559#define STMT_COLUMN_RO 2 /* True if read-only */
223560#define STMT_COLUMN_BUSY 3 /* True if currently busy */
223561#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
223562#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
223563#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
223564#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
223565#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
223566#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
223567#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
223568
223569
223570 rc = sqlite3_declare_vtab(db,
223571 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
223572 "reprep,run,mem)");
223573 if( rc==SQLITE_OK0 ){
223574 pNew = sqlite3_malloc( sizeof(*pNew) );
223575 *ppVtab = (sqlite3_vtab*)pNew;
223576 if( pNew==0 ) return SQLITE_NOMEM7;
223577 memset(pNew, 0, sizeof(*pNew));
223578 pNew->db = db;
223579 }
223580 return rc;
223581}
223582
223583/*
223584** This method is the destructor for stmt_cursor objects.
223585*/
223586static int stmtDisconnect(sqlite3_vtab *pVtab){
223587 sqlite3_free(pVtab);
223588 return SQLITE_OK0;
223589}
223590
223591/*
223592** Constructor for a new stmt_cursor object.
223593*/
223594static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
223595 stmt_cursor *pCur;
223596 pCur = sqlite3_malloc( sizeof(*pCur) );
223597 if( pCur==0 ) return SQLITE_NOMEM7;
223598 memset(pCur, 0, sizeof(*pCur));
223599 pCur->db = ((stmt_vtab*)p)->db;
223600 *ppCursor = &pCur->base;
223601 return SQLITE_OK0;
223602}
223603
223604/*
223605** Destructor for a stmt_cursor.
223606*/
223607static int stmtClose(sqlite3_vtab_cursor *cur){
223608 sqlite3_free(cur);
223609 return SQLITE_OK0;
223610}
223611
223612
223613/*
223614** Advance a stmt_cursor to its next row of output.
223615*/
223616static int stmtNext(sqlite3_vtab_cursor *cur){
223617 stmt_cursor *pCur = (stmt_cursor*)cur;
223618 pCur->iRowid++;
223619 pCur->pStmt = sqlite3_next_stmt(pCur->db, pCur->pStmt);
223620 return SQLITE_OK0;
223621}
223622
223623/*
223624** Return values of columns for the row at which the stmt_cursor
223625** is currently pointing.
223626*/
223627static int stmtColumn(
223628 sqlite3_vtab_cursor *cur, /* The cursor */
223629 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
223630 int i /* Which column to return */
223631){
223632 stmt_cursor *pCur = (stmt_cursor*)cur;
223633 switch( i ){
223634 case STMT_COLUMN_SQL: {
223635 sqlite3_result_text(ctx, sqlite3_sql(pCur->pStmt), -1, SQLITE_TRANSIENT((sqlite3_destructor_type)-1));
223636 break;
223637 }
223638 case STMT_COLUMN_NCOL: {
223639 sqlite3_result_int(ctx, sqlite3_column_count(pCur->pStmt));
223640 break;
223641 }
223642 case STMT_COLUMN_RO: {
223643 sqlite3_result_int(ctx, sqlite3_stmt_readonly(pCur->pStmt));
223644 break;
223645 }
223646 case STMT_COLUMN_BUSY: {
223647 sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
223648 break;
223649 }
223650 case STMT_COLUMN_MEM: {
223651 i = SQLITE_STMTSTATUS_MEMUSED99 +
223652 STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP1;
223653 /* Fall thru */
223654 }
223655 case STMT_COLUMN_NSCAN:
223656 case STMT_COLUMN_NSORT:
223657 case STMT_COLUMN_NAIDX:
223658 case STMT_COLUMN_NSTEP:
223659 case STMT_COLUMN_REPREP:
223660 case STMT_COLUMN_RUN: {
223661 sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
223662 i-STMT_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP1, 0));
223663 break;
223664 }
223665 }
223666 return SQLITE_OK0;
223667}
223668
223669/*
223670** Return the rowid for the current row. In this implementation, the
223671** rowid is the same as the output value.
223672*/
223673static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
223674 stmt_cursor *pCur = (stmt_cursor*)cur;
223675 *pRowid = pCur->iRowid;
223676 return SQLITE_OK0;
223677}
223678
223679/*
223680** Return TRUE if the cursor has been moved off of the last
223681** row of output.
223682*/
223683static int stmtEof(sqlite3_vtab_cursor *cur){
223684 stmt_cursor *pCur = (stmt_cursor*)cur;
223685 return pCur->pStmt==0;
223686}
223687
223688/*
223689** This method is called to "rewind" the stmt_cursor object back
223690** to the first row of output. This method is always called at least
223691** once prior to any call to stmtColumn() or stmtRowid() or
223692** stmtEof().
223693*/
223694static int stmtFilter(
223695 sqlite3_vtab_cursor *pVtabCursor,
223696 int idxNum, const char *idxStr,
223697 int argc, sqlite3_value **argv
223698){
223699 stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
223700 pCur->pStmt = 0;
223701 pCur->iRowid = 0;
223702 return stmtNext(pVtabCursor);
223703}
223704
223705/*
223706** SQLite will invoke this method one or more times while planning a query
223707** that uses the stmt virtual table. This routine needs to create
223708** a query plan for each invocation and compute an estimated cost for that
223709** plan.
223710*/
223711static int stmtBestIndex(
223712 sqlite3_vtab *tab,
223713 sqlite3_index_info *pIdxInfo
223714){
223715 pIdxInfo->estimatedCost = (double)500;
223716 pIdxInfo->estimatedRows = 500;
223717 return SQLITE_OK0;
223718}
223719
223720/*
223721** This following structure defines all the methods for the
223722** stmt virtual table.
223723*/
223724static sqlite3_module stmtModule = {
223725 0, /* iVersion */
223726 0, /* xCreate */
223727 stmtConnect, /* xConnect */
223728 stmtBestIndex, /* xBestIndex */
223729 stmtDisconnect, /* xDisconnect */
223730 0, /* xDestroy */
223731 stmtOpen, /* xOpen - open a cursor */
223732 stmtClose, /* xClose - close a cursor */
223733 stmtFilter, /* xFilter - configure scan constraints */
223734 stmtNext, /* xNext - advance a cursor */
223735 stmtEof, /* xEof - check for end of scan */
223736 stmtColumn, /* xColumn - read data */
223737 stmtRowid, /* xRowid - read data */
223738 0, /* xUpdate */
223739 0, /* xBegin */
223740 0, /* xSync */
223741 0, /* xCommit */
223742 0, /* xRollback */
223743 0, /* xFindMethod */
223744 0, /* xRename */
223745 0, /* xSavepoint */
223746 0, /* xRelease */
223747 0, /* xRollbackTo */
223748 0, /* xShadowName */
223749};
223750
223751#endif /* SQLITE_OMIT_VIRTUALTABLE */
223752
223753SQLITE_PRIVATEstatic int sqlite3StmtVtabInit(sqlite3 *db){
223754 int rc = SQLITE_OK0;
223755#ifndef SQLITE_OMIT_VIRTUALTABLE
223756 rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
223757#endif
223758 return rc;
223759}
223760
223761#ifndef SQLITE_CORE1
223762#ifdef _WIN32
223763__declspec(dllexport)
223764#endif
223765SQLITE_API int sqlite3_stmt_init(
223766 sqlite3 *db,
223767 char **pzErrMsg,
223768 const sqlite3_api_routines *pApi
223769){
223770 int rc = SQLITE_OK0;
223771 SQLITE_EXTENSION_INIT2(pApi)(void)pApi;;
223772#ifndef SQLITE_OMIT_VIRTUALTABLE
223773 rc = sqlite3StmtVtabInit(db);
223774#endif
223775 return rc;
223776}
223777#endif /* SQLITE_CORE */
223778#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
223779
223780/************** End of stmt.c ************************************************/
223781#if __LINE__223781!=223781
223782#undef SQLITE_SOURCE_ID"2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88bfa6"
223783#define SQLITE_SOURCE_ID"2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88bfa6" "2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88alt2"
223784#endif
223785/* Return the source-id for this library */
223786SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID"2019-07-10 17:32:03 fc82b73eaac8b36950e527f12c4b5dc1e147e6f4ad2217ae43ad82882a88bfa6"; }
223787/************************** End of sqlite3.c ******************************/